Skip to content

Commit da159c7

Browse files
flichtenheldgitster
authored andcommitted
Git.pm: Set GIT_WORK_TREE if we set GIT_DIR
Otherwise git will use the current directory as work tree which will lead to unexpected results if we operate in sub directory of the work tree. Signed-off-by: Frank Lichtenheld <flichtenheld@astaro.com> Acked-by: Petr Baudis <pasky@suse.cz> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent f01f109 commit da159c7

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

perl/Git.pm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,8 @@ sub _cmd_exec {
12801280
my ($self, @args) = @_;
12811281
if ($self) {
12821282
$self->repo_path() and $ENV{'GIT_DIR'} = $self->repo_path();
1283+
$self->repo_path() and $self->wc_path()
1284+
and $ENV{'GIT_WORK_TREE'} = $self->wc_path();
12831285
$self->wc_path() and chdir($self->wc_path());
12841286
$self->wc_subdir() and chdir($self->wc_subdir());
12851287
}

t/t9700-perl-git.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ test_expect_success \
2929
git add . &&
3030
git commit -m "first commit" &&
3131
32+
echo "new file in subdir 2" > directory2/file2 &&
33+
git add . &&
34+
git commit -m "commit in directory2" &&
35+
3236
echo "changed file 1" > file1 &&
3337
git commit -a -m "second commit" &&
3438

t/t9700/test.pl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,16 @@
9898
todo_skip 'config after wc_chdir', 1;
9999
is($r->config("color.string"), "value", "config after wc_chdir");
100100
}
101+
102+
# Object generation in sub directory
103+
chdir("directory2");
104+
my $r2 = Git->repository();
105+
is($r2->repo_path, $abs_repo_dir . "/.git", "repo_path (2)");
106+
is($r2->wc_path, $abs_repo_dir . "/", "wc_path (2)");
107+
is($r2->wc_subdir, "directory2/", "wc_subdir initial (2)");
108+
109+
# commands in sub directory
110+
my $last_commit = $r2->command_oneline(qw(rev-parse --verify HEAD));
111+
like($last_commit, qr/^[0-9a-fA-F]{40}$/, 'rev-parse returned hash');
112+
my $dir_commit = $r2->command_oneline('log', '-n1', '--pretty=format:%H', '.');
113+
isnt($last_commit, $dir_commit, 'log . does not show last commit');

0 commit comments

Comments
 (0)