threads / patch / 12020

patchFix 'git cvsexportcommit -w $cvsdir ...' when used with relative $GIT_DIR

Subject: [PATCH] Fix 'git cvsexportcommit -w $cvsdir ...' when used with relative $GIT_DIR

## tl;dr

8 messages between Feb 11, 2008 and Feb 13, 2008. Diffs are folded; open one to read it.

replies: 7people: 4as markdown or json

Johan Herland· Feb 11, 2008, 01:28 UTC · lore

When using the '-w $cvsdir' option to cvsexportcommit, it will chdir into $cvsdir before executing several other git commands. If $GIT_DIR is set to a relative path (e.g. '.'), the git commands executed by cvsexportcommit will naturally fail with "fatal: Not a git repository".

Therefore, if $GIT_DIR is relative, prepend $PWD to $GIT_DIR before the chdir to $cvsdir.

Signed-off-by: Johan Herland <johan@herland.net>
---

I'm working on an update hook script for automatically propagating commits to a CVS server. My script calls 'git cvsexportcommit -w $cvsdir ...'. However, the hooks infrastructure invokes the update hook script with the (bare, in my case) Git repo as $PWD and sets up GIT_DIR="." in the script's environment. For now, I work around this bug in my script by forcing $GIT_DIR to $PWD, but this really seems like a bug in cvsexportcommit.

In my case, this bug can also be fixed by making sure the hook infrastructure set up an _absolute_ GIT_DIR for the update hook, something I'd consider more polite than the current GIT_DIR=".". Whether we want to fix this as well should be discussed. But the patch below is needed in any case, since cvsexportcommit may not only be called from hook scripts.

Have fun! :)
...Johan
 git-cvsexportcommit.perl |   15 +++++++++------
 1 files changed, 9 insertions(+), 6 deletions(-)
Show changes to git-cvsexportcommit.perl +9 −6
diff --git a/git-cvsexportcommit.perl b/git-cvsexportcommit.perl
index d2e50c3..fd4fbfb 100755
--- a/git-cvsexportcommit.perl
+++ b/git-cvsexportcommit.perl
@@ -15,15 +15,18 @@ $opt_h && usage();
 die "Need at least one commit identifier!" unless @ARGV;
 
 if ($opt_w) {
+	# Remember where GIT_DIR is before changing to CVS checkout
 	unless ($ENV{GIT_DIR}) {
-		# Remember where our GIT_DIR is before changing to CVS checkout
+		# Oops no GIT_DIR set. Figure out for ourselves
 		my $gd =`git-rev-parse --git-dir`;
 		chomp($gd);
-		if ($gd eq '.git') {
-			my $wd = `pwd`;
-			chomp($wd);
-			$gd = $wd."/.git"	;
-		}
+		$ENV{GIT_DIR} = $gd;
+	}
+	unless ($ENV{GIT_DIR} =~ m[^/]) {
+		# GIT_DIR is relative. Prepend $PWD
+		my $wd = `pwd`;
+		chomp($wd);
+		my $gd = $wd."/".$ENV{GIT_DIR};
 		$ENV{GIT_DIR} = $gd;
 	}
 
-- 
1.5.4.2.g41ac4
Junio C Hamano· Feb 11, 2008, 20:10 UTC · re: Johan Herland · lore

Re: [PATCH] Fix 'git cvsexportcommit -w $cvsdir ...' when used with relative $GIT_DIR

Johan Herland <johan@herland.net> writes:
Show 7 quoted lines
> When using the '-w $cvsdir' option to cvsexportcommit, it will chdir into
> $cvsdir before executing several other git commands. If $GIT_DIR is set to
> a relative path (e.g. '.'), the git commands executed by cvsexportcommit
> will naturally fail with "fatal: Not a git repository".
>
> Therefore, if $GIT_DIR is relative, prepend $PWD to $GIT_DIR before the
> chdir to $cvsdir.
That sounds like a correct thing to do.  Robin?
Robin Rosenberg· Feb 11, 2008, 20:58 UTC · re: Johan Herland · lore

Re: [PATCH] Fix 'git cvsexportcommit -w $cvsdir ...' when used with relative $GIT_DIR

måndagen den 11 februari 2008 skrev Johan Herland:
> When using the '-w $cvsdir' option to cvsexportcommit, it will chdir into
> $cvsdir before executing several other git commands. If $GIT_DIR is set to
> a relative path (e.g. '.'), the git commands executed by cvsexportcommit
> will naturally fail with "fatal: Not a git repository".
[...]
Show 7 quoted lines
>  die "Need at least one commit identifier!" unless @ARGV;
>  
>  if ($opt_w) {
> +	# Remember where GIT_DIR is before changing to CVS checkout
>  	unless ($ENV{GIT_DIR}) {
> -		# Remember where our GIT_DIR is before changing to CVS checkout
> +		# Oops no GIT_DIR set. Figure out for ourselves
That's not an "Oops". It's perfectly normal not to have GIT_DIR set.
Show 11 quoted lines
>  		my $gd =`git-rev-parse --git-dir`;
>  		chomp($gd);
> -		if ($gd eq '.git') {
> -			my $wd = `pwd`;Kopia
> -			chomp($wd);
> -			$gd = $wd."/.git"	;
> -		}
> +		$ENV{GIT_DIR} = $gd;
> +	}
> +	unless ($ENV{GIT_DIR} =~ m[^/]) {
> +		# GIT_DIR is relative. Prepend $PWD

Hmm. C:/foo? You should probably use rel2abs in the File::Spec module.

-- robin
Johan Herland· Feb 11, 2008, 23:43 UTC · re: Robin Rosenberg · lore

When using the '-w $cvsdir' option to cvsexportcommit, it will chdir into $cvsdir before executing several other git commands. If $GIT_DIR is set to a relative path (e.g. '.'), the git commands executed by cvsexportcommit will naturally fail.

Therefore, ensure that $GIT_DIR is absolute before the chdir to $cvsdir.
Signed-off-by: Johan Herland <johan@herland.net>
---
On Monday 11 February 2008, Robin Rosenberg wrote:
Show 5 quoted lines
> måndagen den 11 februari 2008 skrev Johan Herland:
> > -		# Remember where our GIT_DIR is before changing to CVS checkout
> > +		# Oops no GIT_DIR set. Figure out for ourselves
> 
> That's not an "Oops". It's perfectly normal not to have GIT_DIR set.
Of course not. Fixed.
> > +	unless ($ENV{GIT_DIR} =~ m[^/]) {
> 
> Hmm. C:/foo? You should probably use rel2abs in the File::Spec 
> module.
Thanks. Fixed.
Have fun! :)
...Johan
 git-cvsexportcommit.perl |   11 +++++------
 1 files changed, 5 insertions(+), 6 deletions(-)
Show changes to git-cvsexportcommit.perl +5 −6
diff --git a/git-cvsexportcommit.perl b/git-cvsexportcommit.perl
index d2e50c3..2a8ad1e 100755
--- a/git-cvsexportcommit.perl
+++ b/git-cvsexportcommit.perl
@@ -5,6 +5,7 @@ use Getopt::Std;
 use File::Temp qw(tempdir);
 use Data::Dumper;
 use File::Basename qw(basename dirname);
+use File::Spec;
 
 our ($opt_h, $opt_P, $opt_p, $opt_v, $opt_c, $opt_f, $opt_a, $opt_m, $opt_d, $opt_u, $opt_w);
 
@@ -15,17 +16,15 @@ $opt_h && usage();
 die "Need at least one commit identifier!" unless @ARGV;
 
 if ($opt_w) {
+	# Remember where GIT_DIR is before changing to CVS checkout
 	unless ($ENV{GIT_DIR}) {
-		# Remember where our GIT_DIR is before changing to CVS checkout
+		# No GIT_DIR set. Figure it out for ourselves
 		my $gd =`git-rev-parse --git-dir`;
 		chomp($gd);
-		if ($gd eq '.git') {
-			my $wd = `pwd`;
-			chomp($wd);
-			$gd = $wd."/.git"	;
-		}
 		$ENV{GIT_DIR} = $gd;
 	}
+	# Make sure GIT_DIR is absolute
+	$ENV{GIT_DIR} = File::Spec->rel2abs($ENV{GIT_DIR});
 
 	if (! -d $opt_w."/CVS" ) {
 		die "$opt_w is not a CVS checkout";
-- 
1.5.4.2.g41ac4
Johan Herland· Feb 13, 2008, 03:11 UTC · re: Robin Rosenberg · lore

[PATCH] Add testcase for 'git cvsexportcommit -w $cvsdir ...' with relative $GIT_DIR

The testcase verifies that 'git cvsexportcommit' functions correctly when the '-w' option is used, and GIT_DIR is set to a relative path (e.g. '.').

Signed-off-by: Johan Herland <johan@herland.net>
---
On Tuesday 12 February 2008, Robin Rosenberg wrote:
> Looks ok. Something for the test suite?
Like this?
Have fun! :)
...Johan
 t/t9200-git-cvsexportcommit.sh |   18 +++++++++++++++++-
 1 files changed, 17 insertions(+), 1 deletions(-)
Show changes to t/t9200-git-cvsexportcommit.sh +17 −1
diff --git a/t/t9200-git-cvsexportcommit.sh b/t/t9200-git-cvsexportcommit.sh
index a15222c..5dae88c 100755
--- a/t/t9200-git-cvsexportcommit.sh
+++ b/t/t9200-git-cvsexportcommit.sh
@@ -2,7 +2,7 @@
 #
 # Copyright (c) Robin Rosenberg
 #
-test_description='CVS export comit. '
+test_description='Test export of commits to CVS'
 
 . ./test-lib.sh
 
@@ -246,4 +246,20 @@ test_expect_success \
 	;;
 esac
 
+test_expect_success \
+     '-w option should work with relative GIT_DIR' \
+     'mkdir W &&
+      echo foobar >W/file1.txt &&
+      echo bazzle >W/file2.txt &&
+      git add W/file1.txt &&
+      git add W/file2.txt &&
+      git commit -m "More updates" &&
+      id=$(git rev-list --max-count=1 HEAD) &&
+      (cd "$GIT_DIR" &&
+      GIT_DIR=. git cvsexportcommit -w "$CVSWORK" -c $id &&
+      check_entries "$CVSWORK/W" "file1.txt/1.1/|file2.txt/1.1/" &&
+      diff -u "$CVSWORK/W/file1.txt" ../W/file1.txt &&
+      diff -u "$CVSWORK/W/file2.txt" ../W/file2.txt
+      )'
+
 test_done
-- 
1.5.4.2.g41ac4
Junio C Hamano· Feb 13, 2008, 07:58 UTC · re: Johan Herland · lore

Re: [PATCH] Add testcase for 'git cvsexportcommit -w $cvsdir ...' with relative $GIT_DIR

Johan Herland <johan@herland.net> writes:
Show 10 quoted lines
> The testcase verifies that 'git cvsexportcommit' functions correctly when
> the '-w' option is used, and GIT_DIR is set to a relative path (e.g. '.').
>
> Signed-off-by: Johan Herland <johan@herland.net>
> ---
>
> On Tuesday 12 February 2008, Robin Rosenberg wrote:
>> Looks ok. Something for the test suite?
>
> Like this?
Yeah, looks good.
Except that I'll reorder the patches so that
 (1) this test comes first, but with test_expect_failure instead;
 (2) then your earlier fix patch, but with a change that changes
     the test_expect_failure to test_expect_success.
Thanks.

Perhaps it would be a good idea to require fix-up patches to always come with a test case to prevent future regression.

Jakub Narebski· Feb 13, 2008, 09:26 UTC · re: Junio C Hamano · lore

Re: [PATCH] Add testcase for 'git cvsexportcommit -w $cvsdir ...' with relative $GIT_DIR

Junio C Hamano <gitster@pobox.com> writes:
Show 11 quoted lines
> Except that I'll reorder the patches so that
> 
>  (1) this test comes first, but with test_expect_failure instead;
> 
>  (2) then your earlier fix patch, but with a change that changes
>      the test_expect_failure to test_expect_success.
> 
> Thanks.
> 
> Perhaps it would be a good idea to require fix-up patches to
> always come with a test case to prevent future regression.

Perhaps we should add this hint to SubmittingPatches, on top of your additions? And to t/README?

-- 
Jakub Narebski
Poland
ShadeHawk on #git

← back to recent threads