git/t/t9700-perl-git.sh
Jeff King e4b353d0a1 Git.pm: fix bare repository search with Directory option
When opening a bare repository like:

  Git->repository(Directory => '/path/to/bare.git');

we will incorrectly point the repository object at the _current_
directory, not the one specified by the option.

The bug was introduced by 20da61f25f (Git.pm: trust rev-parse to find
bare repositories, 2022-10-22). Before then, we'd ask "rev-parse
--git-dir" if it was a Git repo, and if it returned anything, we'd
correctly convert that result to an absolute path using File::Spec and
Cwd::abs_path(). If it didn't, we'd guess it might be a bare repository
and find it ourselves, which was wrong (rev-parse should find even a
bare repo, and our search circumvented some of its rules).

That commit dropped most of the custom bare-repo search code in favor of
using "rev-parse --is-bare-repository" and trusting the "--git-dir" it
returned. But it mistakenly left some of the bare-repo code path in
place, which was now broken. That code calls Cwd::abs_path($dir); prior
to 20da61f25f $dir contained the "Directory" option the user passed in.
But afterwards, it contains the output of "rev-parse --git-dir". And
since our tentative rev-parse command is invoked after changing
directory, it will always be the relative path "."! So we'll end up with
the absolute path of the process's current directory, not the Directory
option the caller asked for.

So the non-bare case is correct, but the bare one is broken. Our tests
only check the non-bare one, so we didn't notice. We can fix this by
running the same absolute-path fixup code for both sides.

Helped-by: Rodrigo <rodrigolive@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-09-13 10:42:19 -07:00

58 lines
1.4 KiB
Bash
Executable file

#!/bin/sh
#
# Copyright (c) 2008 Lea Wiemann
#
test_description='perl interface (Git.pm)'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
. "$TEST_DIRECTORY"/lib-perl.sh
skip_all_if_no_Test_More
# set up test repository
test_expect_success 'set up test repository' '
echo "test file 1" >file1 &&
echo "test file 2" >file2 &&
mkdir directory1 &&
echo "in directory1" >>directory1/file &&
mkdir directory2 &&
echo "in directory2" >>directory2/file &&
git add . &&
git commit -m "first commit" &&
echo "new file in subdir 2" >directory2/file2 &&
git add . &&
git commit -m "commit in directory2" &&
echo "changed file 1" >file1 &&
git commit -a -m "second commit" &&
git config --add color.test.slot1 green &&
git config --add test.string value &&
git config --add test.dupstring value1 &&
git config --add test.dupstring value2 &&
git config --add test.booltrue true &&
git config --add test.boolfalse no &&
git config --add test.boolother other &&
git config --add test.int 2k &&
git config --add test.path "~/foo" &&
git config --add test.pathexpanded "$HOME/foo" &&
git config --add test.pathmulti foo &&
git config --add test.pathmulti bar
'
test_expect_success 'set up bare repository' '
git init --bare bare.git &&
git -C bare.git --work-tree=. commit --allow-empty -m "bare commit"
'
test_expect_success 'use t9700/test.pl to test Git.pm' '
"$PERL_PATH" "$TEST_DIRECTORY"/t9700/test.pl 2>stderr &&
test_must_be_empty stderr
'
test_done