git-svn: fix segfaults from accessing svn_log_changed_path_t
svn_log_changed_path_t structs were being used out of scope outside of svn_ra_get_log (because I wanted to eventually be able to use git-svn with only a single connection to the repository). So now we dup them into a hash. This was fixed while making --follow-parent fetches more efficient. I've moved parsing of the command-line --revision argument outside of the Git::SVN module so Git::SVN::fetch() can be used in more places (such as find_parent_branch). Signed-off-by: Eric Wong <normalperson@yhbt.net>
Eric Wong committed
Jan 25, 2007 at 17:35 UTC
3ebe8df7f690281c21e330eec156098c14f4e685
2 files changed
+68
-37
git-svn.perl
+68
-36
@@ -283,7 +283,7 @@ sub cmd_fetch {
283
instead.\n";
284
}
285
my $gs = Git::SVN->new;
286
- $gs->fetch;
286
+ $gs->fetch(parse_revision_argument());
287
if ($gs->{last_commit} && !verify_ref('refs/heads/master^0')) {
288
command_noisy(qw(update-ref refs/heads/master),
289
$gs->{last_commit});
@@ -482,6 +482,18 @@ sub cmd_commit_diff {
482
483
########################### utility functions #########################
484
485
+sub parse_revision_argument {
486
+ if (!defined $_revision || $_revision eq 'BASE:HEAD') {
487
+ return (undef, undef);
488
+ }
489
+ return ($1, $2) if ($_revision =~ /^(\d+):(\d+)$/);
490
+ return ($_revision, $_revision) if ($_revision =~ /^\d+$/);
491
+ return (undef, $1) if ($_revision =~ /^BASE:(\d+)$/);
492
+ return ($1, undef) if ($_revision =~ /^(\d+):HEAD$/);
493
+ die "revision argument: $_revision not understood by git-svn\n",
494
+ "Try using the command-line svn client instead\n";
495
+}
496
+
497
sub complete_svn_url {
498
my ($url, $path) = @_;
499
$path =~ s#/+$##;
@@ -914,6 +926,9 @@ sub traverse_ignore {
926
}
927
}
928
929
+sub last_rev { ($_[0]->last_rev_commit)[0] }
930
+sub last_commit { ($_[0]->last_rev_commit)[1] }
931
+
932
# returns the newest SVN revision number and newest commit SHA1
933
sub last_rev_commit {
934
my ($self) = @_;
@@ -951,22 +966,11 @@ sub last_rev_commit {
966
return ($rev, $c);
967
}
968
954
-sub parse_revision {
955
- my ($self, $base) = @_;
956
- my $head = $self->ra->get_latest_revnum;
957
- if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
958
- return ($base + 1, $head) if (defined $base);
959
- return (0, $head);
960
- }
961
- return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
962
- return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
963
- if ($::_revision =~ /^BASE:(\d+)$/) {
964
- return ($base + 1, $1) if (defined $base);
965
- return (0, $head);
966
- }
967
- return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
968
- die "revision argument: $::_revision not understood by git-svn\n",
969
- "Try using the command-line svn client instead\n";
969
+sub get_fetch_range {
970
+ my ($self, $min, $max) = @_;
971
+ $max ||= $self->ra->get_latest_revnum;
972
+ $min ||= $self->last_rev || 0;
973
+ (++$min, $max);
974
}
975
976
sub tmp_index_do {
@@ -1101,8 +1105,8 @@ sub find_parent_branch {
1105
my ($self, $paths, $rev) = @_;
1106
return undef unless $::_follow_parent;
1107
unless (defined $paths) {
1104
- $self->ra->get_log([''], $rev, $rev, 0, 1, 1,
1105
- sub { $paths = $_[0] });
1108
+ $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1,
1109
+ sub { $paths = dup_changed_paths($_[0]) });
1110
}
1111
return undef unless defined $paths;
1112
@@ -1116,13 +1120,13 @@ sub find_parent_branch {
1120
unshift(@a_path_components, pop(@b_path_components));
1121
}
1122
goto not_found unless defined $i;
1119
- my $branch_from = $i->copyfrom_path or goto not_found;
1123
+ my $branch_from = $i->{copyfrom_path} or goto not_found;
1124
if (@a_path_components) {
1125
print STDERR "branch_from: $branch_from => ";
1126
$branch_from .= '/'.join('/', @a_path_components);
1127
print STDERR $branch_from, "\n";
1128
}
1125
- my $r = $i->copyfrom_rev;
1129
+ my $r = $i->{copyfrom_rev};
1130
my $repos_root = $self->ra->{repos_root};
1131
my $url = $self->ra->{url};
1132
my $new_url = $repos_root . $branch_from;
@@ -1151,11 +1155,7 @@ sub find_parent_branch {
1155
}
1156
my ($r0, $parent) = $gs->find_rev_before($r, 1);
1157
if ($::_follow_parent && (!defined $r0 || !defined $parent)) {
1154
- foreach (1 .. $r) {
1155
- if (my $log_entry = $gs->do_fetch(undef, $_)) {
1156
- $gs->do_git_commit($log_entry);
1157
- }
1158
- }
1158
+ $gs->fetch(0, $r);
1159
($r0, $parent) = $gs->last_rev_commit;
1160
}
1161
if (defined $r0 && defined $parent && $gs->revisions_eq($r0, $r)) {
@@ -1183,8 +1183,19 @@ sub find_parent_branch {
1183
}
1184
not_found:
1185
print STDERR "Branch parent for path: '/",
1186
- $self->rel_path, "' @ $rev not found\n";
1187
- print STDERR ' ', $_, "\n" foreach (sort keys %$paths);
1186
+ $self->rel_path, "' @ r$rev not found:\n";
1187
+ return undef unless $paths;
1188
+ print STDERR "Changed paths:\n";
1189
+ foreach my $x (sort keys %$paths) {
1190
+ my $p = $paths->{$x};
1191
+ print STDERR "\t$p->{action}\t$x";
1192
+ if ($p->{copyfrom_path}) {
1193
+ print STDERR "(from $p->{copyfrom_path}: ",
1194
+ "$p->{copyfrom_rev})";
1195
+ }
1196
+ print STDERR "\n";
1197
+ }
1198
+ print STDERR '-'x72, "\n";
1199
return undef;
1200
}
1201
@@ -1302,9 +1313,9 @@ sub make_log_entry {
1313
}
1314
1315
sub fetch {
1305
- my ($self, @parents) = @_;
1316
+ my ($self, $min_rev, $max_rev, @parents) = @_;
1317
my ($last_rev, $last_commit) = $self->last_rev_commit;
1307
- my ($base, $head) = $self->parse_revision($last_rev);
1318
+ my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
1319
return if ($base > $head);
1320
if (defined $last_commit) {
1321
$self->assert_index_clean($last_commit);
@@ -1316,13 +1327,16 @@ sub fetch {
1327
$SVN::Error::handler = sub { ($err) = @_; skip_unknown_revs($err); } ;
1328
while (1) {
1329
my @revs;
1319
- $self->ra->get_log([$self->{path}], $min, $max, 0, 1, 1, sub {
1320
- my ($paths, $rev, $author, $date, $log) = @_;
1321
- push @revs, [ $paths, $rev ] });
1322
- if (! @revs && $err) {
1330
+ $self->ra->get_log([$self->{path}], $min, $max, 0, 1, 1,
1331
+ sub {
1332
+ my ($paths, $rev) = @_;
1333
+ push @revs, [ dup_changed_paths($paths), $rev ];
1334
+ });
1335
+ if (! @revs && $err && $max >= $head) {
1336
print STDERR "Branch probably deleted:\n ",
1337
$err->expanded_message,
1338
"\nWill attempt to follow revisions ",
1339
+ "r$min .. r$max",
1340
"committed before the deletion\n";
1341
@revs = map { [ undef, $_ ] } ($min .. $max);
1342
}
@@ -1331,7 +1345,7 @@ sub fetch {
1345
$self->do_git_commit($log_entry, @parents);
1346
}
1347
}
1334
- last if $max >= $head || $err;
1348
+ last if $max >= $head;
1349
$min = $max + 1;
1350
$max += $inc;
1351
$max = $head if ($max > $head);
@@ -1347,7 +1361,7 @@ sub set_tree_cb {
1361
$log_entry->{author} = $author;
1362
$self->do_git_commit($log_entry, "$rev=$tree");
1363
} else {
1350
- $self->fetch("$rev=$tree");
1364
+ $self->fetch(undef, undef, "$rev=$tree");
1365
}
1366
}
1367
@@ -1393,6 +1407,24 @@ sub skip_unknown_revs {
1407
croak "Error from SVN, ($errno): ", $err->expanded_message,"\n";
1408
}
1409
1410
+# svn_log_changed_path_t objects passed to get_log are likely to be
1411
+# overwritten even if only the refs are copied to an external variable,
1412
+# so we should dup the structures in their entirety. Using an externally
1413
+# passed pool (instead of our temporary and quickly cleared pool in
1414
+# Git::SVN::Ra) does not help matters at all...
1415
+sub dup_changed_paths {
1416
+ my ($paths) = @_;
1417
+ return undef unless $paths;
1418
+ my %ret;
1419
+ foreach my $p (keys %$paths) {
1420
+ my $i = $paths->{$p};
1421
+ my %s = map { $_ => $i->$_ }
1422
+ qw/copyfrom_path copyfrom_rev action/;
1423
+ $ret{$p} = \%s;
1424
+ }
1425
+ \%ret;
1426
+}
1427
+
1428
# rev_db:
1429
# Tie::File seems to be prone to offset errors if revisions get sparse,
1430
# it's not that fast, either. Tie::File is also not in Perl 5.6. So
t/t9104-git-svn-follow-parent.sh
-1
@@ -78,7 +78,6 @@ test_expect_success 'follow larger parent' "
78
true
79
"
80
81
-# This seems to cause segfaults over HTTP...
81
test_expect_success 'follow higher-level parent' "
82
svn mkdir -m 'follow higher-level parent' $svnrepo/blob &&
83
svn co $svnrepo/blob blob &&