Skip to contentSkip to conversation

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

PATCH In review Johan Herland 4 participants 8 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix 'git cvsexportcommit -w $cvsdir ...' when used with relative $GIT_DIR

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.

---

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(-)
git-cvsexportcommit.perl +9 -6
index d2e50c3..fd4fbfb 100755
@@ -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;
 	}
 
Signature · 1 line
1.5.4.2.g41ac4
#1 lore raw

måndagen den 11 februari 2008 skrev Johan Herland:

Quoted 4 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".

[...]

git-cvsexportcommit.perl $opt_h && usage(); · line 20

Quoted 7 lines

die "Need at least one commit identifier!" unless @ARGV;

if ($opt_w) {

git-cvsexportcommit.perl +2 -1
+	# 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.

git-cvsexportcommit.perl $opt_h && usage(); · line 26

Quoted 11 lines
git-cvsexportcommit.perl +4 -5
 		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

#2 lore raw

PATCH 1/1 Fix 'git cvsexportcommit -w $cvsdir ...' when used with relative $GIT_DIR

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.

---

Quoted 5 lines · Robin Rosenberg

måndagen den 11 februari 2008 skrev Johan Herland:

Review

Quoted 2 lines
(quoted diff) +1 -1
-		# 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.

Quoted 4 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	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(-)
git-cvsexportcommit.perl +5 -6
index d2e50c3..2a8ad1e 100755
@@ -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";
Signature · 1 line
1.5.4.2.g41ac4
#4 lore raw

PATCH 1/1 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. '.').

---

Quoted 1 line · Robin Rosenberg

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(-)
t/t9200-git-cvsexportcommit.sh +17 -1
index a15222c..5dae88c 100755
@@ -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
Signature · 1 line
1.5.4.2.g41ac4
#5 lore raw

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

Quoted 10 lines · Johan Herland

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. '.').

---

Quoted 1 line · Robin Rosenberg

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.

#6 lore raw

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

Quoted 11 lines · Junio C Hamano

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?

Signature · 3 lines
Jakub Narebski
Poland
ShadeHawk on #git
#7 lore raw
In reply to Johan Herland
Quoted 7 lines · 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".

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?