Skip to content

Commit

Permalink
Display an error from update-ref if target ref name is invalid.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Shawn Pearce authored and Junio C Hamano committed Jul 29, 2006
1 parent 1b03dfe commit 818f477
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *path,
int plen,
const unsigned char *old_sha1, int mustexist)
{
const char *orig_path = path;
struct ref_lock *lock;
struct stat st;

Expand All @@ -303,7 +304,11 @@ static struct ref_lock *lock_ref_sha1_basic(const char *path,
plen = strlen(path) - plen;
path = resolve_ref(path, lock->old_sha1, mustexist);
if (!path) {
int last_errno = errno;
error("unable to resolve reference %s: %s",
orig_path, strerror(errno));
unlock_ref(lock);
errno = last_errno;
return NULL;
}
lock->lk = xcalloc(1, sizeof(struct lock_file));
Expand Down
12 changes: 12 additions & 0 deletions t/t1400-update-ref.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ D=4444444444444444444444444444444444444444
E=5555555555555555555555555555555555555555
F=6666666666666666666666666666666666666666
m=refs/heads/master
n_dir=refs/heads/gu
n=$n_dir/fixes

test_expect_success \
"create $m" \
Expand All @@ -25,6 +27,16 @@ test_expect_success \
test $B = $(cat .git/$m)'
rm -f .git/$m

test_expect_success \
"fail to create $n" \
'touch .git/$n_dir
git-update-ref $n $A >out 2>err
test $? = 1 &&
test "" = "$(cat out)" &&
grep "error: unable to resolve reference" err &&
grep $n err'
rm -f .git/$n_dir out err

test_expect_success \
"create $m (by HEAD)" \
'git-update-ref HEAD $A &&
Expand Down

0 comments on commit 818f477

Please sign in to comment.