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

8 messages from 2008-02-11 to 2008-02-13. Participants: Johan Herland, Junio C Hamano, Robin Rosenberg, Jakub Narebski.
Thread: https://gitlist.dev/t/12020

## Johan Herland, 2008-02-11 01:28

Subject: [PATCH] Fix 'git cvsexportcommit -w $cvsdir ...' when used with relative $GIT_DIR
Message-ID: <200802110228.05233.johan@herland.net>
URL: https://gitlist.dev/e/200802110228.05233.johan%40herland.net

```
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(-)

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, 2008-02-11 20:10

Subject: Re: [PATCH] Fix 'git cvsexportcommit -w $cvsdir ...' when used with relative $GIT_DIR
Message-ID: <7vhcgf8da6.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vhcgf8da6.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <200802110228.05233.johan@herland.net>

```
Johan Herland <johan@herland.net> writes:

> 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, 2008-02-11 20:58

Subject: Re: [PATCH] Fix 'git cvsexportcommit -w $cvsdir ...' when used with relative $GIT_DIR
Message-ID: <200802112158.16830.robin.rosenberg.lists@dewire.com>
URL: https://gitlist.dev/e/200802112158.16830.robin.rosenberg.lists%40dewire.com
In-Reply-To: <200802110228.05233.johan@herland.net>

```
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".
[...]

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

>  		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, 2008-02-11 23:43

Subject: [PATCH] Fix 'git cvsexportcommit -w $cvsdir ...' when used with relative $GIT_DIR
Message-ID: <200802120043.41610.johan@herland.net>
URL: https://gitlist.dev/e/200802120043.41610.johan%40herland.net
In-Reply-To: <200802112158.16830.robin.rosenberg.lists@dewire.com>

```
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:
> 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(-)

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

```

## Robin Rosenberg, 2008-02-12 20:41

Subject: Re: [PATCH] Fix 'git cvsexportcommit -w $cvsdir ...' when used with relative $GIT_DIR
Message-ID: <200802122141.35594.robin.rosenberg.lists@dewire.com>
URL: https://gitlist.dev/e/200802122141.35594.robin.rosenberg.lists%40dewire.com
In-Reply-To: <200802120043.41610.johan@herland.net>

```

Looks ok. Something for the test suite?

-- robin

```

## Johan Herland, 2008-02-13 03:11

Subject: [PATCH] Add testcase for 'git cvsexportcommit -w $cvsdir ...' with relative $GIT_DIR
Message-ID: <200802130411.22093.johan@herland.net>
URL: https://gitlist.dev/e/200802130411.22093.johan%40herland.net
In-Reply-To: <200802122141.35594.robin.rosenberg.lists@dewire.com>

```
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(-)

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, 2008-02-13 07:58

Subject: Re: [PATCH] Add testcase for 'git cvsexportcommit -w $cvsdir ...' with relative $GIT_DIR
Message-ID: <7vabm5s2xs.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vabm5s2xs.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <200802130411.22093.johan@herland.net>

```
Johan Herland <johan@herland.net> writes:

> 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, 2008-02-13 09:26

Subject: Re: [PATCH] Add testcase for 'git cvsexportcommit -w $cvsdir ...' with relative $GIT_DIR
Message-ID: <m3odalcill.fsf@localhost.localdomain>
URL: https://gitlist.dev/e/m3odalcill.fsf%40localhost.localdomain
In-Reply-To: <7vabm5s2xs.fsf@gitster.siamese.dyndns.org>

```
Junio C Hamano <gitster@pobox.com> writes:

> 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

```
