@matheusbsbs / matheus-bagliano / commits / 818f477c40
Fork 0

Display an error from update-ref if target ref name is invalid.

Alex Riesen (raa.lkml@gmail.com) recently observed that git branch would fail with no error message due to unexpected situations with regards to refs. For example, if .git/refs/heads/gu is a file but "git branch -b refs/heads/gu/fixa HEAD" was invoked by the user it would fail silently due to refs/heads/gu being a file and not a directory. This change adds a test for trying to create a ref within a directory that is actually currently a file, and adds error printing within the ref locking routine should the resolve operation fail. The error printing code probably belongs at this level of the library as other failures within the ref locking, writing and logging code are also currently at this level of the code. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Shawn Pearce committed Jul 28, 2006 at 23:44 UTC 818f477c40fee62ab8ea5b493c51bb357f38957c
2 files changed +17
refs.c
+5
@@ -294,6 +294,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *path,
294 int plen,
295 const unsigned char *old_sha1, int mustexist)
296 {
297 + const char *orig_path = path;
298 struct ref_lock *lock;
299 struct stat st;
300
@@ -303,7 +304,11 @@ static struct ref_lock *lock_ref_sha1_basic(const char *path,
304 plen = strlen(path) - plen;
305 path = resolve_ref(path, lock->old_sha1, mustexist);
306 if (!path) {
307 + int last_errno = errno;
308 + error("unable to resolve reference %s: %s",
309 + orig_path, strerror(errno));
310 unlock_ref(lock);
311 + errno = last_errno;
312 return NULL;
313 }
314 lock->lk = xcalloc(1, sizeof(struct lock_file));
t/t1400-update-ref.sh
+12
@@ -14,6 +14,8 @@ D=4444444444444444444444444444444444444444
14 E=5555555555555555555555555555555555555555
15 F=6666666666666666666666666666666666666666
16 m=refs/heads/master
17 +n_dir=refs/heads/gu
18 +n=$n_dir/fixes
19
20 test_expect_success \
21 "create $m" \
@@ -25,6 +27,16 @@ test_expect_success \
27 test $B = $(cat .git/$m)'
28 rm -f .git/$m
29
30 +test_expect_success \
31 + "fail to create $n" \
32 + 'touch .git/$n_dir
33 + git-update-ref $n $A >out 2>err
34 + test $? = 1 &&
35 + test "" = "$(cat out)" &&
36 + grep "error: unable to resolve reference" err &&
37 + grep $n err'
38 +rm -f .git/$n_dir out err
39 +
40 test_expect_success \
41 "create $m (by HEAD)" \
42 'git-update-ref HEAD $A &&