git-svn: Simplify calculation of GIT_DIR

Since git-rev-parse already checks for the $GIT_DIR environment variable and that it returns an actual git repository, there is no need to repeat the checks again here. This also fixes a problem where git-svn did not work in cases where .git was a file with a gitdir: link. [ew: squashed test case, delay setting GIT_DIR until after `git rev-parse --cdup` to fix t9101, (thanks to Junio)] Signed-off-by: Barry Wardell <barry.wardell@gmail.com> Signed-off-by: Eric Wong <normalperson@yhbt.net>

Barry Wardell committed Jan 21, 2013 at 01:22 UTC bc93ceb7c5ec959ad275f575a3c49ff491337a8b
2 files changed +22 -23
git-svn.perl
+14 -23
@@ -61,8 +61,6 @@ my $cmd_dir_prefix = eval {
61 command_oneline([qw/rev-parse --show-prefix/], STDERR => 0)
62 } || '';
63
64 -my $git_dir_user_set = 1 if defined $ENV{GIT_DIR};
65 -$ENV{GIT_DIR} ||= '.git';
64 $Git::SVN::Ra::_log_window_size = 100;
65
66 if (! exists $ENV{SVN_SSH} && exists $ENV{GIT_SSH}) {
@@ -327,27 +325,20 @@ for (my $i = 0; $i < @ARGV; $i++) {
325 };
326
327 # make sure we're always running at the top-level working directory
330 -unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
331 - unless (-d $ENV{GIT_DIR}) {
332 - if ($git_dir_user_set) {
333 - die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
334 - "but it is not a directory\n";
335 - }
336 - my $git_dir = delete $ENV{GIT_DIR};
337 - my $cdup = undef;
338 - git_cmd_try {
339 - $cdup = command_oneline(qw/rev-parse --show-cdup/);
340 - $git_dir = '.' unless ($cdup);
341 - chomp $cdup if ($cdup);
342 - $cdup = "." unless ($cdup && length $cdup);
343 - } "Already at toplevel, but $git_dir not found\n";
344 - chdir $cdup or die "Unable to chdir up to '$cdup'\n";
345 - unless (-d $git_dir) {
346 - die "$git_dir still not found after going to ",
347 - "'$cdup'\n";
348 - }
349 - $ENV{GIT_DIR} = $git_dir;
350 - }
328 +if ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
329 + $ENV{GIT_DIR} ||= ".git";
330 +} else {
331 + my ($git_dir, $cdup);
332 + git_cmd_try {
333 + $git_dir = command_oneline([qw/rev-parse --git-dir/]);
334 + } "Unable to find .git directory\n";
335 + git_cmd_try {
336 + $cdup = command_oneline(qw/rev-parse --show-cdup/);
337 + chomp $cdup if ($cdup);
338 + $cdup = "." unless ($cdup && length $cdup);
339 + } "Already at toplevel, but $git_dir not found\n";
340 + $ENV{GIT_DIR} = $git_dir;
341 + chdir $cdup or die "Unable to chdir up to '$cdup'\n";
342 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
343 }
344
t/t9100-git-svn-basic.sh
+8
@@ -306,5 +306,13 @@ test_expect_success 'git-svn works in a bare repository' '
306 git svn fetch ) &&
307 rm -rf bare-repo
308 '
309 +test_expect_success 'git-svn works in in a repository with a gitdir: link' '
310 + mkdir worktree gitdir &&
311 + ( cd worktree &&
312 + git svn init "$svnrepo" &&
313 + git init --separate-git-dir ../gitdir &&
314 + git svn fetch ) &&
315 + rm -rf worktree gitdir
316 + '
317
318 test_done