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

[PATCH v2] git-svn: Simplify calculation of GIT_DIR

From
Barry Wardell <barry.wardell@gmail.com>
Date
Mar 3, 2012, 19:53 UTC
Message-ID
<1330804397-43062-1-git-send-email-barry.wardell@gmail.com>

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.
---
 git-svn.perl |   33 +++++++++++----------------------
 1 file changed, 11 insertions(+), 22 deletions(-)
diff --git a/git-svn.perl b/git-svn.perl
index 4334b95..bbfd351 100755
--- a/git-svn.perl
+++ b/git-svn.perl
@@ -15,8 +15,6 @@ my $cmd_dir_prefix = eval {
 	command_oneline([qw/rev-parse --show-prefix/], STDERR => 0)
 } || '';
 
-my $git_dir_user_set = 1 if defined $ENV{GIT_DIR};
-$ENV{GIT_DIR} ||= '.git';
 $Git::SVN::default_repo_id = 'svn';
 $Git::SVN::default_ref_id = $ENV{GIT_SVN_ID} || 'git-svn';
 $Git::SVN::Ra::_log_window_size = 100;
@@ -292,26 +290,17 @@ for (my $i = 0; $i < @ARGV; $i++) {
 
 # make sure we're always running at the top-level working directory
 unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
-	unless (-d $ENV{GIT_DIR}) {
-		if ($git_dir_user_set) {
-			die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
-			    "but it is not a directory\n";
-		}
-		my $git_dir = delete $ENV{GIT_DIR};
-		my $cdup = undef;
-		git_cmd_try {
-			$cdup = command_oneline(qw/rev-parse --show-cdup/);
-			$git_dir = '.' unless ($cdup);
-			chomp $cdup if ($cdup);
-			$cdup = "." unless ($cdup && length $cdup);
-		} "Already at toplevel, but $git_dir not found\n";
-		chdir $cdup or die "Unable to chdir up to '$cdup'\n";
-		unless (-d $git_dir) {
-			die "$git_dir still not found after going to ",
-			    "'$cdup'\n";
-		}
-		$ENV{GIT_DIR} = $git_dir;
-	}
+	my $toplevel = undef;
+
+	git_cmd_try {
+		$toplevel = command_oneline([qw/rev-parse --show-toplevel/]);
+	} "Unable to find toplevel directory\n";
+
+	git_cmd_try {
+		$ENV{GIT_DIR} = command_oneline([qw/rev-parse --git-dir/]);
+	} "Unable to find .git directory\n";
+
+	chdir $toplevel or die "Unable to chdir to '$toplevel'\n";
 	$_repository = Git->repository(Repository => $ENV{GIT_DIR});
 }
 
-- 
1.7.9.2
Next: Eric Wong
Message 1 of 15 in “git-svn: Simplify calculation of GIT_DIR”
  1. git-svn: Simplify calculation of GIT_DIRBarry Wardell, Mar 3, 2012
  2. Eric WongMar 8, 2012
  3. 0/2 Make git-svn work with gitdir linksBarry Wardell, Jan 21, 2013
  4. 1/2 git-svn: Add test for git-svn repositories with a gitdir linkBarry Wardell, Jan 21, 2013
  5. 2/2 git-svn: Simplify calculation of GIT_DIRBarry Wardell, Jan 21, 2013
  6. Junio C HamanoJan 21, 2013
  7. Joachim SchmitzJan 21, 2013
  8. Philip OakleyJan 21, 2013
  9. Junio C HamanoJan 21, 2013
  10. Eric WongJan 23, 2013
  11. Junio C HamanoJan 23, 2013
  12. Barry WardellJan 23, 2013
  13. Junio C HamanoJan 24, 2013
  14. Eric WongJan 24, 2013
  15. Eric WongJan 24, 2013

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.