@matheusbsbs / matheus-bagliano / commits / da159c7759
Fork 0

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>

Frank Lichtenheld committed May 7, 2009 at 15:41 UTC da159c7759418bb14af655968dfa7a98bdcb8661
3 files changed +19
perl/Git.pm
+2
@@ -1280,6 +1280,8 @@ sub _cmd_exec {
1280 my ($self, @args) = @_;
1281 if ($self) {
1282 $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();
1285 $self->wc_path() and chdir($self->wc_path());
1286 $self->wc_subdir() and chdir($self->wc_subdir());
1287 }
t/t9700-perl-git.sh
+4
@@ -29,6 +29,10 @@ test_expect_success \
29 git add . &&
30 git commit -m "first commit" &&
31
32 + echo "new file in subdir 2" > directory2/file2 &&
33 + git add . &&
34 + git commit -m "commit in directory2" &&
35 +
36 echo "changed file 1" > file1 &&
37 git commit -a -m "second commit" &&
38
t/t9700/test.pl
+13
@@ -98,3 +98,16 @@ TODO: {
98 todo_skip 'config after wc_chdir', 1;
99 is($r->config("color.string"), "value", "config after wc_chdir");
100 }
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');