git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] Git.pm: Set GIT_WORK_TREE if we set GIT_DIR

From
Frank Lichtenheld <frank@lichtenheld.de>
Date
Apr 18, 2009, 15:58 UTC
Message-ID
<1240070330-31446-1-git-send-email-frank@lichtenheld.de>
From: Frank Lichtenheld <flichtenheld@astaro.com>

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>
---
 perl/Git.pm         |    2 ++
 t/t9700-perl-git.sh |    4 ++++
 t/t9700/test.pl     |   13 +++++++++++++
 3 files changed, 19 insertions(+), 0 deletions(-)

One example for the unexpected results is that "git log ." doesn't work correctly, as demonstrated by my test.

diff --git a/perl/Git.pm b/perl/Git.pm
index 291ff5b..4313db7 100644
--- a/perl/Git.pm
+++ b/perl/Git.pm
@@ -1280,6 +1280,8 @@ sub _cmd_exec {
 	my ($self, @args) = @_;
 	if ($self) {
 		$self->repo_path() and $ENV{'GIT_DIR'} = $self->repo_path();
+		$self->repo_path() and $self->wc_path()
+			and $ENV{'GIT_WORK_TREE'} = $self->wc_path();
 		$self->wc_path() and chdir($self->wc_path());
 		$self->wc_subdir() and chdir($self->wc_subdir());
 	}
diff --git a/t/t9700-perl-git.sh b/t/t9700-perl-git.sh
index b4ca244..4eb7d3f 100755
--- a/t/t9700-perl-git.sh
+++ b/t/t9700-perl-git.sh
@@ -29,6 +29,10 @@ test_expect_success \
      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" &&
 
diff --git a/t/t9700/test.pl b/t/t9700/test.pl
index 697daf3..d9b29ea 100755
--- a/t/t9700/test.pl
+++ b/t/t9700/test.pl
@@ -98,3 +98,16 @@ TODO: {
 	todo_skip 'config after wc_chdir', 1;
 	is($r->config("color.string"), "value", "config after wc_chdir");
 }
+
+# Object generation in sub directory
+chdir("directory2");
+my $r2 = Git->repository();
+is($r2->repo_path, $abs_repo_dir . "/.git", "repo_path (2)");
+is($r2->wc_path, $abs_repo_dir . "/", "wc_path (2)");
+is($r2->wc_subdir, "directory2/", "wc_subdir initial (2)");
+
+# commands in sub directory
+my $last_commit = $r2->command_oneline(qw(rev-parse --verify HEAD));
+like($last_commit, qr/^[0-9a-fA-F]{40}$/, 'rev-parse returned hash');
+my $dir_commit = $r2->command_oneline('log', '-n1', '--pretty=format:%H', '.');
+isnt($last_commit, $dir_commit, 'log . does not show last commit');
-- 
1.6.2.1
Next: Frank Lichtenheld
Message 1 of 4 in “Git.pm: Set GIT_WORK_TREE if we set GIT_DIR”
  1. Git.pm: Set GIT_WORK_TREE if we set GIT_DIRFrank Lichtenheld, Apr 18, 2009
  2. Git.pm: Always set Repository to absolute path if autodetectingFrank Lichtenheld, Apr 18, 2009
  3. Johannes SchindelinApr 18, 2009
  4. Frank LichtenheldApr 18, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.