git-svn: minor cleanups, extra error-checking
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>
Eric Wong committed
May 24, 2006 at 01:40 UTC
b8c92caddac61e556254bf93c3a4bd744de94320
1 file changed
+46
-36
contrib/git-svn/git-svn.perl
+46
-36
@@ -77,39 +77,15 @@ for (my $i = 0; $i < @ARGV; $i++) {
77
78
my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
79
80
-# convert GetOpt::Long specs for use by git-repo-config
81
-foreach my $o (keys %opts) {
82
- my $v = $opts{$o};
83
- my ($key) = ($o =~ /^([a-z\-]+)/);
84
- $key =~ s/-//g;
85
- my $arg = 'git-repo-config';
86
- $arg .= ' --int' if ($o =~ /=i$/);
87
- $arg .= ' --bool' if ($o !~ /=[sfi]$/);
88
- if (ref $v eq 'ARRAY') {
89
- chomp(my @tmp = `$arg --get-all svn.$key`);
90
- @$v = @tmp if @tmp;
91
- } else {
92
- chomp(my $tmp = `$arg --get svn.$key`);
93
- if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) {
94
- $$v = $tmp;
95
- }
96
- }
97
-}
98
-
80
+read_repo_config(\%opts);
81
GetOptions(%opts, 'help|H|h' => \$_help,
82
'version|V' => \$_version,
83
'id|i=s' => \$GIT_SVN) or exit 1;
84
103
-$GIT_SVN ||= $ENV{GIT_SVN_ID} || 'git-svn';
104
-$GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
105
-$GIT_SVN_INDEX = "$GIT_SVN_DIR/index";
106
-$SVN_URL = undef;
107
-$REV_DIR = "$GIT_SVN_DIR/revs";
108
-$SVN_WC = "$GIT_SVN_DIR/tree";
109
-
85
usage(0) if $_help;
86
version() if $_version;
87
usage(1) unless defined $cmd;
88
+init_vars();
89
load_authors() if $_authors;
90
load_all_refs() if $_branch_all_refs;
91
svn_compat_check();
@@ -132,7 +108,7 @@ Usage: $0 <command> [options] [arguments]\n
108
print $fd ' ',pack('A13',$_),$cmd{$_}->[1],"\n";
109
foreach (keys %{$cmd{$_}->[2]}) {
110
# prints out arguments as they should be passed:
135
- my $x = s#=s$## ? '<arg>' : s#=i$## ? '<num>' : '';
111
+ my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
112
print $fd ' ' x 17, join(', ', map { length $_ > 1 ?
113
"--$_" : "-$_" }
114
split /\|/,$_)," $x\n";
@@ -220,9 +196,10 @@ sub rebuild {
196
sys(@svn_up,"-r$newest_rev");
197
$ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
198
index_changes();
223
- exec('git-write-tree');
199
+ exec('git-write-tree') or croak $!;
200
}
201
waitpid $pid, 0;
202
+ croak $? if $?;
203
204
if ($_upgrade) {
205
print STDERR <<"";
@@ -295,6 +272,7 @@ sub fetch {
272
unless (-e "$GIT_DIR/refs/heads/master") {
273
sys(qw(git-update-ref refs/heads/master),$last_commit);
274
}
275
+ close $svn_log->{fh};
276
return $last;
277
}
278
@@ -830,7 +808,7 @@ sub svn_log_raw {
808
exec (qw(svn log), @log_args) or croak $!
809
}
810
waitpid $pid, 0;
833
- croak if $?;
811
+ croak $? if $?;
812
seek $log_fh, 0, 0 or croak $!;
813
return { state => 'sep', fh => $log_fh };
814
}
@@ -1090,7 +1068,7 @@ sub git_commit {
1068
$ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
1069
index_changes();
1070
chomp(my $tree = `git-write-tree`);
1093
- croak if $?;
1071
+ croak $? if $?;
1072
if (exists $tree_map{$tree}) {
1073
my %seen_parent = map { $_ => 1 } @exec_parents;
1074
foreach (@{$tree_map{$tree}}) {
@@ -1118,7 +1096,7 @@ sub git_commit {
1096
exec @exec or croak $!;
1097
}
1098
waitpid($pid,0);
1121
- croak if $?;
1099
+ croak $? if $?;
1100
1101
$out_fh->flush == 0 or croak $!;
1102
seek $out_fh, 0, 0 or croak $!;
@@ -1134,7 +1112,7 @@ sub git_commit {
1112
close STDERR;
1113
close STDOUT;
1114
exec 'git-rev-parse','--verify',
1137
- "refs/remotes/$GIT_SVN^0";
1115
+ "refs/remotes/$GIT_SVN^0" or croak $!;
1116
}
1117
waitpid $pid, 0;
1118
push @update_ref, $primary_parent unless $?;
@@ -1190,7 +1168,7 @@ sub blob_to_file {
1168
1169
if ($pid == 0) {
1170
open STDOUT, '>&', $blob_fh or croak $!;
1193
- exec('git-cat-file','blob',$blob);
1171
+ exec('git-cat-file','blob',$blob) or croak $!;
1172
}
1173
waitpid $pid, 0;
1174
croak $? if $?;
@@ -1202,7 +1180,7 @@ sub safe_qx {
1180
my $pid = open my $child, '-|';
1181
defined $pid or croak $!;
1182
if ($pid == 0) {
1205
- exec(@_) or croak $?;
1183
+ exec(@_) or croak $!;
1184
}
1185
my @ret = (<$child>);
1186
close $child or croak $?;
@@ -1252,7 +1230,7 @@ sub check_upgrade_needed {
1230
defined $pid or croak $!;
1231
if ($pid == 0) {
1232
close STDERR;
1255
- exec('git-rev-parse',"$GIT_SVN-HEAD") or croak $?;
1233
+ exec('git-rev-parse',"$GIT_SVN-HEAD") or croak $!;
1234
}
1235
my @ret = (<$child>);
1236
close $child or croak $?;
@@ -1276,7 +1254,7 @@ sub map_tree_joins {
1254
defined $pid or croak $!;
1255
if ($pid == 0) {
1256
exec(qw(git-rev-list --topo-order --pretty=raw), $br)
1279
- or croak $?;
1257
+ or croak $!;
1258
}
1259
while (<$pipe>) {
1260
if (/^commit ($sha1)$/o) {
@@ -1352,6 +1330,38 @@ sub migration_check {
1330
print "Done upgrading.\n";
1331
}
1332
1333
+sub init_vars {
1334
+ $GIT_SVN ||= $ENV{GIT_SVN_ID} || 'git-svn';
1335
+ $GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
1336
+ $GIT_SVN_INDEX = "$GIT_SVN_DIR/index";
1337
+ $SVN_URL = undef;
1338
+ $REV_DIR = "$GIT_SVN_DIR/revs";
1339
+ $SVN_WC = "$GIT_SVN_DIR/tree";
1340
+}
1341
+
1342
+# convert GetOpt::Long specs for use by git-repo-config
1343
+sub read_repo_config {
1344
+ return unless -d $GIT_DIR;
1345
+ my $opts = shift;
1346
+ foreach my $o (keys %$opts) {
1347
+ my $v = $opts->{$o};
1348
+ my ($key) = ($o =~ /^([a-z\-]+)/);
1349
+ $key =~ s/-//g;
1350
+ my $arg = 'git-repo-config';
1351
+ $arg .= ' --int' if ($o =~ /[:=]i$/);
1352
+ $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1353
+ if (ref $v eq 'ARRAY') {
1354
+ chomp(my @tmp = `$arg --get-all svn.$key`);
1355
+ @$v = @tmp if @tmp;
1356
+ } else {
1357
+ chomp(my $tmp = `$arg --get svn.$key`);
1358
+ if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) {
1359
+ $$v = $tmp;
1360
+ }
1361
+ }
1362
+ }
1363
+}
1364
+
1365
__END__
1366
1367
Data structures: