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

[PATCH 6/13] git-svn: minor cleanups, extra error-checking

From
EWEric Wong <normalperson@yhbt.net>
Date
Jun 13, 2006, 18:02 UTC
Message-ID
<11502217433396-git-send-email-normalperson@yhbt.net>
In-Reply-To
<11502217352245-git-send-email-normalperson@yhbt.net>

While we're at it, read_repo_config has been added and expanded to handle case where command-line arguments are optional to Getopt::Long

Signed-off-by: Eric Wong <normalperson@yhbt.net>
---
 contrib/git-svn/git-svn.perl |   82 ++++++++++++++++++++++++------------------
 1 files changed, 46 insertions(+), 36 deletions(-)
diff --git a/contrib/git-svn/git-svn.perl b/contrib/git-svn/git-svn.perl
index 2dce4e7..a243060 100755
--- a/contrib/git-svn/git-svn.perl
+++ b/contrib/git-svn/git-svn.perl
@@ -77,39 +77,15 @@ for (my $i = 0; $i < @ARGV; $i++) {
 
 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
 
-# convert GetOpt::Long specs for use by git-repo-config
-foreach my $o (keys %opts) {
-	my $v = $opts{$o};
-	my ($key) = ($o =~ /^([a-z\-]+)/);
-	$key =~ s/-//g;
-	my $arg = 'git-repo-config';
-	$arg .= ' --int' if ($o =~ /=i$/);
-	$arg .= ' --bool' if ($o !~ /=[sfi]$/);
-	if (ref $v eq 'ARRAY') {
-		chomp(my @tmp = `$arg --get-all svn.$key`);
-		@$v = @tmp if @tmp;
-	} else {
-		chomp(my $tmp = `$arg --get svn.$key`);
-		if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) {
-			$$v = $tmp;
-		}
-	}
-}
-
+read_repo_config(\%opts);
 GetOptions(%opts, 'help|H|h' => \$_help,
 		'version|V' => \$_version,
 		'id|i=s' => \$GIT_SVN) or exit 1;
 
-$GIT_SVN ||= $ENV{GIT_SVN_ID} || 'git-svn';
-$GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
-$GIT_SVN_INDEX = "$GIT_SVN_DIR/index";
-$SVN_URL = undef;
-$REV_DIR = "$GIT_SVN_DIR/revs";
-$SVN_WC = "$GIT_SVN_DIR/tree";
-
 usage(0) if $_help;
 version() if $_version;
 usage(1) unless defined $cmd;
+init_vars();
 load_authors() if $_authors;
 load_all_refs() if $_branch_all_refs;
 svn_compat_check();
@@ -132,7 +108,7 @@ Usage: $0 <command> [options] [arguments
 		print $fd '  ',pack('A13',$_),$cmd{$_}->[1],"\n";
 		foreach (keys %{$cmd{$_}->[2]}) {
 			# prints out arguments as they should be passed:
-			my $x = s#=s$## ? '<arg>' : s#=i$## ? '<num>' : '';
+			my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
 			print $fd ' ' x 17, join(', ', map { length $_ > 1 ?
 							"--$_" : "-$_" }
 						split /\|/,$_)," $x\n";
@@ -220,9 +196,10 @@ sub rebuild {
 		sys(@svn_up,"-r$newest_rev");
 		$ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
 		index_changes();
-		exec('git-write-tree');
+		exec('git-write-tree') or croak $!;
 	}
 	waitpid $pid, 0;
+	croak $? if $?;
 
 	if ($_upgrade) {
 		print STDERR <<"";
@@ -295,6 +272,7 @@ sub fetch {
 	unless (-e "$GIT_DIR/refs/heads/master") {
 		sys(qw(git-update-ref refs/heads/master),$last_commit);
 	}
+	close $svn_log->{fh};
 	return $last;
 }
 
@@ -830,7 +808,7 @@ sub svn_log_raw {
 		exec (qw(svn log), @log_args) or croak $!
 	}
 	waitpid $pid, 0;
-	croak if $?;
+	croak $? if $?;
 	seek $log_fh, 0, 0 or croak $!;
 	return { state => 'sep', fh => $log_fh };
 }
@@ -1090,7 +1068,7 @@ sub git_commit {
 		$ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
 		index_changes();
 		chomp(my $tree = `git-write-tree`);
-		croak if $?;
+		croak $? if $?;
 		if (exists $tree_map{$tree}) {
 			my %seen_parent = map { $_ => 1 } @exec_parents;
 			foreach (@{$tree_map{$tree}}) {
@@ -1118,7 +1096,7 @@ sub git_commit {
 		exec @exec or croak $!;
 	}
 	waitpid($pid,0);
-	croak if $?;
+	croak $? if $?;
 
 	$out_fh->flush == 0 or croak $!;
 	seek $out_fh, 0, 0 or croak $!;
@@ -1134,7 +1112,7 @@ sub git_commit {
 			close STDERR;
 			close STDOUT;
 			exec 'git-rev-parse','--verify',
-						"refs/remotes/$GIT_SVN^0";
+					"refs/remotes/$GIT_SVN^0" or croak $!;
 		}
 		waitpid $pid, 0;
 		push @update_ref, $primary_parent unless $?;
@@ -1190,7 +1168,7 @@ sub blob_to_file {
 
 	if ($pid == 0) {
 		open STDOUT, '>&', $blob_fh or croak $!;
-		exec('git-cat-file','blob',$blob);
+		exec('git-cat-file','blob',$blob) or croak $!;
 	}
 	waitpid $pid, 0;
 	croak $? if $?;
@@ -1202,7 +1180,7 @@ sub safe_qx {
 	my $pid = open my $child, '-|';
 	defined $pid or croak $!;
 	if ($pid == 0) {
-		exec(@_) or croak $?;
+		exec(@_) or croak $!;
 	}
 	my @ret = (<$child>);
 	close $child or croak $?;
@@ -1252,7 +1230,7 @@ sub check_upgrade_needed {
 		defined $pid or croak $!;
 		if ($pid == 0) {
 			close STDERR;
-			exec('git-rev-parse',"$GIT_SVN-HEAD") or croak $?;
+			exec('git-rev-parse',"$GIT_SVN-HEAD") or croak $!;
 		}
 		my @ret = (<$child>);
 		close $child or croak $?;
@@ -1276,7 +1254,7 @@ sub map_tree_joins {
 		defined $pid or croak $!;
 		if ($pid == 0) {
 			exec(qw(git-rev-list --topo-order --pretty=raw), $br)
-								or croak $?;
+								or croak $!;
 		}
 		while (<$pipe>) {
 			if (/^commit ($sha1)$/o) {
@@ -1352,6 +1330,38 @@ sub migration_check {
 	print "Done upgrading.\n";
 }
 
+sub init_vars {
+	$GIT_SVN ||= $ENV{GIT_SVN_ID} || 'git-svn';
+	$GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
+	$GIT_SVN_INDEX = "$GIT_SVN_DIR/index";
+	$SVN_URL = undef;
+	$REV_DIR = "$GIT_SVN_DIR/revs";
+	$SVN_WC = "$GIT_SVN_DIR/tree";
+}
+
+# convert GetOpt::Long specs for use by git-repo-config
+sub read_repo_config {
+	return unless -d $GIT_DIR;
+	my $opts = shift;
+	foreach my $o (keys %$opts) {
+		my $v = $opts->{$o};
+		my ($key) = ($o =~ /^([a-z\-]+)/);
+		$key =~ s/-//g;
+		my $arg = 'git-repo-config';
+		$arg .= ' --int' if ($o =~ /[:=]i$/);
+		$arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
+		if (ref $v eq 'ARRAY') {
+			chomp(my @tmp = `$arg --get-all svn.$key`);
+			@$v = @tmp if @tmp;
+		} else {
+			chomp(my $tmp = `$arg --get svn.$key`);
+			if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) {
+				$$v = $tmp;
+			}
+		}
+	}
+}
+
 __END__
 
 Data structures:
-- 
1.4.0
Previous: Eric WongNext: Eric Wong
Message 7 of 14 in “git-svn: better branch support, SVN:: lib usage, feature additions”
  1. 0/13 git-svn: better branch support, SVN:: lib usage, feature additionsEric Wong, Jun 13, 2006
  2. 1/13 git-svn: support -C<num> passing to git-diff-treeEric Wong, Jun 13, 2006
  3. 2/13 git-svn: --branch-all-refs / -B supportEric Wong, Jun 13, 2006
  4. 3/13 git-svn: optimize --branch and --branch-all-refEric Wong, Jun 13, 2006
  5. 4/13 git-svn: support manually placed initial trees from fetchEric Wong, Jun 13, 2006
  6. 5/13 git-svn: Move all git-svn-related paths into $GIT_DIR/svnEric Wong, Jun 13, 2006
  7. 6/13 git-svn: minor cleanups, extra error-checkingEric Wong, Jun 13, 2006
  8. 7/13 git-svn: add --repack and --repack-flags= optionsEric Wong, Jun 13, 2006
  9. 8/13 git-svn: add --shared and --template= options to pass to init-dbEric Wong, Jun 13, 2006
  10. 9/13 git-svn: add some functionality to better support branches in svnEric Wong, Jun 13, 2006
  11. 10/13 git-svn: add UTF-8 message testEric Wong, Jun 13, 2006
  12. 11/13 git-svn: add 'log' command, a facsimile of basic `svn log'Eric Wong, Jun 13, 2006
  13. 12/13 git-svn: add support for Perl SVN::* librariesEric Wong, Jun 13, 2006
  14. 13/13 git-svn: make the $GIT_DIR/svn/*/revs directory obsoleteEric Wong, Jun 13, 2006

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.