mirror of
https://github.com/git/git.git
synced 2024-11-28 20:44:04 +08:00
a1c1d8170d
If our call to refs_read_raw_ref() fails, we check errno to
see if the ref is simply missing, or if we encountered a
more serious error. If it's just missing, then in "write"
mode (i.e., when RESOLVE_REFS_READING is not set), this is
perfectly fine.
However, checking for ENOENT isn't sufficient to catch all
missing-ref cases. In the filesystem backend, we may also
see EISDIR when we try to resolve "a" and "a/b" exists.
Likewise, we may see ENOTDIR if we try to resolve "a/b" and
"a" exists. In both of those cases, we know that our
resolved ref doesn't exist, but we return an error (rather
than reporting the refname and returning a null sha1).
This has been broken for a long time, but nobody really
noticed because the next step after resolving without the
READING flag is usually to lock the ref and write it. But in
both of those cases, the write will fail with the same
errno due to the directory/file conflict.
There are two cases where we can notice this, though:
1. If we try to write "a" and there's a leftover directory
already at "a", even though there is no ref "a/b". The
actual write is smart enough to move the empty "a" out
of the way.
This is reasonably rare, if only because the writing
code has to do an independent resolution before trying
its write (because the actual update_ref() code handles
this case fine). The notes-merge code does this, and
before the fix in the prior commit t3308 erroneously
expected this case to fail.
2. When resolving symbolic refs, we typically do not use
the READING flag because we want to resolve even
symrefs that point to unborn refs. Even if those unborn
refs could not actually be written because of d/f
conflicts with existing refs.
You can see this by asking "git symbolic-ref" to report
the target of a symref pointing past a d/f conflict.
We can fix the problem by recognizing the other "missing"
errnos and treating them like ENOENT. This should be safe to
do even for callers who are then going to actually write the
ref, because the actual writing process will fail if the d/f
conflict is a real one (and t1404 checks these cases).
Arguably this should be the responsibility of the
files-backend to normalize all "missing ref" errors into
ENOENT (since something like EISDIR may not be meaningful at
all to a database backend). However other callers of
refs_read_raw_ref() may actually care about the distinction;
putting this into resolve_ref() is the minimal fix for now.
The new tests in t1401 use git-symbolic-ref, which is the
most direct way to check the resolution by itself.
Interestingly we actually had a test that setup this case
already, but we only used it to verify that the funny state
could be overwritten, not that it could be resolved.
We also add a new test in t3200, as "branch -m" was the
original motivation for looking into this. What happens is
this:
0. HEAD is pointing to branch "a"
1. The user asks to rename "a" to "a/b".
2. We create "a/b" and delete "a".
3. We then try to update any worktree HEADs that point to
the renamed ref (including the main repo HEAD). To do
that, we have to resolve each HEAD. But now our HEAD is
pointing at "a", and we get EISDIR due to the loose
"a/b". As a result, we think there is no HEAD, and we
do not update it. It now points to the bogus "a".
Interestingly this case used to work, but only accidentally.
Before 31824d180d
(branch: fix branch renaming not updating
HEADs correctly, 2017-08-24), we'd update any HEAD which we
couldn't resolve. That was wrong, but it papered over the
fact that we were incorrectly failing to resolve HEAD.
So while the bug demonstrated by the git-symbolic-ref is
quite old, the regression to "branch -m" is recent.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
164 lines
4.7 KiB
Bash
Executable File
164 lines
4.7 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
test_description='basic symbolic-ref tests'
|
|
. ./test-lib.sh
|
|
|
|
# If the tests munging HEAD fail, they can break detection of
|
|
# the git repo, meaning that further tests will operate on
|
|
# the surrounding git repo instead of the trash directory.
|
|
reset_to_sane() {
|
|
echo ref: refs/heads/foo >.git/HEAD
|
|
}
|
|
|
|
test_expect_success 'symbolic-ref writes HEAD' '
|
|
git symbolic-ref HEAD refs/heads/foo &&
|
|
echo ref: refs/heads/foo >expect &&
|
|
test_cmp expect .git/HEAD
|
|
'
|
|
|
|
test_expect_success 'symbolic-ref reads HEAD' '
|
|
echo refs/heads/foo >expect &&
|
|
git symbolic-ref HEAD >actual &&
|
|
test_cmp expect actual
|
|
'
|
|
|
|
test_expect_success 'symbolic-ref refuses non-ref for HEAD' '
|
|
test_must_fail git symbolic-ref HEAD foo
|
|
'
|
|
reset_to_sane
|
|
|
|
test_expect_success 'symbolic-ref refuses bare sha1' '
|
|
echo content >file && git add file && git commit -m one &&
|
|
test_must_fail git symbolic-ref HEAD $(git rev-parse HEAD)
|
|
'
|
|
reset_to_sane
|
|
|
|
test_expect_success 'HEAD cannot be removed' '
|
|
test_must_fail git symbolic-ref -d HEAD
|
|
'
|
|
|
|
reset_to_sane
|
|
|
|
test_expect_success 'symbolic-ref can be deleted' '
|
|
git symbolic-ref NOTHEAD refs/heads/foo &&
|
|
git symbolic-ref -d NOTHEAD &&
|
|
test_path_is_file .git/refs/heads/foo &&
|
|
test_path_is_missing .git/NOTHEAD
|
|
'
|
|
reset_to_sane
|
|
|
|
test_expect_success 'symbolic-ref can delete dangling symref' '
|
|
git symbolic-ref NOTHEAD refs/heads/missing &&
|
|
git symbolic-ref -d NOTHEAD &&
|
|
test_path_is_missing .git/refs/heads/missing &&
|
|
test_path_is_missing .git/NOTHEAD
|
|
'
|
|
reset_to_sane
|
|
|
|
test_expect_success 'symbolic-ref fails to delete missing FOO' '
|
|
echo "fatal: Cannot delete FOO, not a symbolic ref" >expect &&
|
|
test_must_fail git symbolic-ref -d FOO >actual 2>&1 &&
|
|
test_cmp expect actual
|
|
'
|
|
reset_to_sane
|
|
|
|
test_expect_success 'symbolic-ref fails to delete real ref' '
|
|
echo "fatal: Cannot delete refs/heads/foo, not a symbolic ref" >expect &&
|
|
test_must_fail git symbolic-ref -d refs/heads/foo >actual 2>&1 &&
|
|
test_path_is_file .git/refs/heads/foo &&
|
|
test_cmp expect actual
|
|
'
|
|
reset_to_sane
|
|
|
|
test_expect_success 'create large ref name' '
|
|
# make 256+ character ref; some systems may not handle that,
|
|
# so be gentle
|
|
long=0123456789abcdef &&
|
|
long=$long/$long/$long/$long &&
|
|
long=$long/$long/$long/$long &&
|
|
long_ref=refs/heads/$long &&
|
|
tree=$(git write-tree) &&
|
|
commit=$(echo foo | git commit-tree $tree) &&
|
|
if git update-ref $long_ref $commit; then
|
|
test_set_prereq LONG_REF
|
|
else
|
|
echo >&2 "long refs not supported"
|
|
fi
|
|
'
|
|
|
|
test_expect_success LONG_REF 'symbolic-ref can point to large ref name' '
|
|
git symbolic-ref HEAD $long_ref &&
|
|
echo $long_ref >expect &&
|
|
git symbolic-ref HEAD >actual &&
|
|
test_cmp expect actual
|
|
'
|
|
|
|
test_expect_success LONG_REF 'we can parse long symbolic ref' '
|
|
echo $commit >expect &&
|
|
git rev-parse --verify HEAD >actual &&
|
|
test_cmp expect actual
|
|
'
|
|
|
|
test_expect_success 'symbolic-ref reports failure in exit code' '
|
|
test_when_finished "rm -f .git/HEAD.lock" &&
|
|
>.git/HEAD.lock &&
|
|
test_must_fail git symbolic-ref HEAD refs/heads/whatever
|
|
'
|
|
|
|
test_expect_success 'symbolic-ref writes reflog entry' '
|
|
git checkout -b log1 &&
|
|
test_commit one &&
|
|
git checkout -b log2 &&
|
|
test_commit two &&
|
|
git checkout --orphan orphan &&
|
|
git symbolic-ref -m create HEAD refs/heads/log1 &&
|
|
git symbolic-ref -m update HEAD refs/heads/log2 &&
|
|
cat >expect <<-\EOF &&
|
|
update
|
|
create
|
|
EOF
|
|
git log --format=%gs -g -2 >actual &&
|
|
test_cmp expect actual
|
|
'
|
|
|
|
test_expect_success 'symbolic-ref does not create ref d/f conflicts' '
|
|
git checkout -b df &&
|
|
test_commit df &&
|
|
test_must_fail git symbolic-ref refs/heads/df/conflict refs/heads/df &&
|
|
git pack-refs --all --prune &&
|
|
test_must_fail git symbolic-ref refs/heads/df/conflict refs/heads/df
|
|
'
|
|
|
|
test_expect_success 'symbolic-ref can overwrite pointer to invalid name' '
|
|
test_when_finished reset_to_sane &&
|
|
head=$(git rev-parse HEAD) &&
|
|
git symbolic-ref HEAD refs/heads/outer &&
|
|
test_when_finished "git update-ref -d refs/heads/outer/inner" &&
|
|
git update-ref refs/heads/outer/inner $head &&
|
|
git symbolic-ref HEAD refs/heads/unrelated
|
|
'
|
|
|
|
test_expect_success 'symbolic-ref can resolve d/f name (EISDIR)' '
|
|
test_when_finished reset_to_sane &&
|
|
head=$(git rev-parse HEAD) &&
|
|
git symbolic-ref HEAD refs/heads/outer/inner &&
|
|
test_when_finished "git update-ref -d refs/heads/outer" &&
|
|
git update-ref refs/heads/outer $head &&
|
|
echo refs/heads/outer/inner >expect &&
|
|
git symbolic-ref HEAD >actual &&
|
|
test_cmp expect actual
|
|
'
|
|
|
|
test_expect_success 'symbolic-ref can resolve d/f name (ENOTDIR)' '
|
|
test_when_finished reset_to_sane &&
|
|
head=$(git rev-parse HEAD) &&
|
|
git symbolic-ref HEAD refs/heads/outer &&
|
|
test_when_finished "git update-ref -d refs/heads/outer/inner" &&
|
|
git update-ref refs/heads/outer/inner $head &&
|
|
echo refs/heads/outer >expect &&
|
|
git symbolic-ref HEAD >actual &&
|
|
test_cmp expect actual
|
|
'
|
|
|
|
test_done
|