alternates: re-allow relative paths from environment

Commit 670c359da (link_alt_odb_entry: handle normalize_path
errors, 2016-10-03) regressed the handling of relative paths
in the GIT_ALTERNATE_OBJECT_DIRECTORIES variable. It's not
entirely clear this was ever meant to work, but it _has_
worked for several years, so this commit restores the
original behavior.

When we get a path in GIT_ALTERNATE_OBJECT_DIRECTORIES, we
add it the path to the list of alternate object directories
as if it were found in objects/info/alternates, but with one
difference: we do not provide the link_alt_odb_entry()
function with a base for relative paths. That function
doesn't turn it into an absolute path, and we end up feeding
the relative path to the strbuf_normalize_path() function.

Most relative paths break out of the top-level directory
(e.g., "../foo.git/objects"), and thus normalizing fails.
Prior to 670c359da, we simply ignored the error, and due to
the way normalize_path_copy() was implemented it happened to
return the original path in this case. We then accessed the
alternate objects using this relative path.

By storing the relative path in the alt_odb list, the path
is relative to wherever we happen to be at the time we do an
object lookup. That means we look from $GIT_DIR in a bare
repository, and from the top of the worktree in a non-bare
repository.

If this were being designed from scratch, it would make
sense to pick a stable location (probably $GIT_DIR, or even
the object directory) and use that as the relative base,
turning the result into an absolute path.  However, given
the history, at this point the minimal fix is to match the
pre-670c359da behavior.

We can do this simply by ignoring the error when we have no
relative base and using the original value (which we now
reliably have, thanks to strbuf_normalize_path()).

That still leaves us with a relative path that foils our
duplicate detection, and may act strangely if we ever
chdir() later in the process. We could solve that by storing
an absolute path based on getcwd(). That may be a good
future direction; for now we'll do just the minimum to fix
the regression.

The new t5615 script demonstrates the fix in its final three
tests. Since we didn't have any tests of the alternates
environment variable at all, it also adds some tests of
absolute paths.

Reported-by: Bryan Turner <bturner@atlassian.com>
Signed-off-by: Jeff King <peff@peff.net>
This commit is contained in:
Jeff King 2016-11-07 23:50:17 -05:00
Родитель ea0fc3b417
Коммит 37a95862c6
2 изменённых файлов: 72 добавлений и 1 удалений

Просмотреть файл

@ -296,7 +296,7 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base,
} }
strbuf_addstr(&pathbuf, entry); strbuf_addstr(&pathbuf, entry);
if (strbuf_normalize_path(&pathbuf) < 0) { if (strbuf_normalize_path(&pathbuf) < 0 && relative_base) {
error("unable to normalize alternate object path: %s", error("unable to normalize alternate object path: %s",
pathbuf.buf); pathbuf.buf);
strbuf_release(&pathbuf); strbuf_release(&pathbuf);

71
t/t5615-alternate-env.sh Executable file
Просмотреть файл

@ -0,0 +1,71 @@
#!/bin/sh
test_description='handling of alternates in environment variables'
. ./test-lib.sh
check_obj () {
alt=$1; shift
while read obj expect
do
echo "$obj" >&3 &&
echo "$obj $expect" >&4
done 3>input 4>expect &&
GIT_ALTERNATE_OBJECT_DIRECTORIES=$alt \
git "$@" cat-file --batch-check='%(objectname) %(objecttype)' \
<input >actual &&
test_cmp expect actual
}
test_expect_success 'create alternate repositories' '
git init --bare one.git &&
one=$(echo one | git -C one.git hash-object -w --stdin) &&
git init --bare two.git &&
two=$(echo two | git -C two.git hash-object -w --stdin)
'
test_expect_success 'objects inaccessible without alternates' '
check_obj "" <<-EOF
$one missing
$two missing
EOF
'
test_expect_success 'access alternate via absolute path' '
check_obj "$(pwd)/one.git/objects" <<-EOF
$one blob
$two missing
EOF
'
test_expect_success 'access multiple alternates' '
check_obj "$(pwd)/one.git/objects:$(pwd)/two.git/objects" <<-EOF
$one blob
$two blob
EOF
'
# bare paths are relative from $GIT_DIR
test_expect_success 'access alternate via relative path (bare)' '
git init --bare bare.git &&
check_obj "../one.git/objects" -C bare.git <<-EOF
$one blob
EOF
'
# non-bare paths are relative to top of worktree
test_expect_success 'access alternate via relative path (worktree)' '
git init worktree &&
check_obj "../one.git/objects" -C worktree <<-EOF
$one blob
EOF
'
# path is computed after moving to top-level of worktree
test_expect_success 'access alternate via relative path (subdir)' '
mkdir subdir &&
check_obj "one.git/objects" -C subdir <<-EOF
$one blob
EOF
'
test_done