Extract Git::SVN::GlobSpec from git-svn.
[git/mingw.git] / perl / Git / SVN.pm
blobb8b34744ea49df95196cfb7ef0566f6ced57ba6b
1 package Git::SVN;
2 use strict;
3 use warnings;
4 use Fcntl qw/:DEFAULT :seek/;
5 use constant rev_map_fmt => 'NH40';
6 use vars qw/$_no_metadata
7 $_repack $_repack_flags $_use_svm_props $_head
8 $_use_svnsync_props $no_reuse_existing
9 $_use_log_author $_add_author_from $_localtime/;
10 use Carp qw/croak/;
11 use File::Path qw/mkpath/;
12 use File::Copy qw/copy/;
13 use IPC::Open3;
14 use Time::Local;
15 use Memoize; # core since 5.8.0, Jul 2002
16 use Memoize::Storable;
17 use POSIX qw(:signal_h);
19 use Git qw(
20 command
21 command_oneline
22 command_noisy
23 command_output_pipe
24 command_close_pipe
26 use Git::SVN::Utils qw(fatal can_compress);
28 my $can_use_yaml;
29 BEGIN {
30 $can_use_yaml = eval { require Git::SVN::Memoize::YAML; 1};
33 our $_follow_parent = 1;
34 our $_minimize_url = 'unset';
35 our $default_repo_id = 'svn';
36 our $default_ref_id = $ENV{GIT_SVN_ID} || 'git-svn';
38 my ($_gc_nr, $_gc_period);
40 # properties that we do not log:
41 my %SKIP_PROP;
42 BEGIN {
43 %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
44 svn:special svn:executable
45 svn:entry:committed-rev
46 svn:entry:last-author
47 svn:entry:uuid
48 svn:entry:committed-date/;
50 # some options are read globally, but can be overridden locally
51 # per [svn-remote "..."] section. Command-line options will *NOT*
52 # override options set in an [svn-remote "..."] section
53 no strict 'refs';
54 for my $option (qw/follow_parent no_metadata use_svm_props
55 use_svnsync_props/) {
56 my $key = $option;
57 $key =~ tr/_//d;
58 my $prop = "-$option";
59 *$option = sub {
60 my ($self) = @_;
61 return $self->{$prop} if exists $self->{$prop};
62 my $k = "svn-remote.$self->{repo_id}.$key";
63 eval { command_oneline(qw/config --get/, $k) };
64 if ($@) {
65 $self->{$prop} = ${"Git::SVN::_$option"};
66 } else {
67 my $v = command_oneline(qw/config --bool/,$k);
68 $self->{$prop} = $v eq 'false' ? 0 : 1;
70 return $self->{$prop};
76 my (%LOCKFILES, %INDEX_FILES);
77 END {
78 unlink keys %LOCKFILES if %LOCKFILES;
79 unlink keys %INDEX_FILES if %INDEX_FILES;
82 sub resolve_local_globs {
83 my ($url, $fetch, $glob_spec) = @_;
84 return unless defined $glob_spec;
85 my $ref = $glob_spec->{ref};
86 my $path = $glob_spec->{path};
87 foreach (command(qw#for-each-ref --format=%(refname) refs/#)) {
88 next unless m#^$ref->{regex}$#;
89 my $p = $1;
90 my $pathname = desanitize_refname($path->full_path($p));
91 my $refname = desanitize_refname($ref->full_path($p));
92 if (my $existing = $fetch->{$pathname}) {
93 if ($existing ne $refname) {
94 die "Refspec conflict:\n",
95 "existing: $existing\n",
96 " globbed: $refname\n";
98 my $u = (::cmt_metadata("$refname"))[0];
99 $u =~ s!^\Q$url\E(/|$)!! or die
100 "$refname: '$url' not found in '$u'\n";
101 if ($pathname ne $u) {
102 warn "W: Refspec glob conflict ",
103 "(ref: $refname):\n",
104 "expected path: $pathname\n",
105 " real path: $u\n",
106 "Continuing ahead with $u\n";
107 next;
109 } else {
110 $fetch->{$pathname} = $refname;
115 sub parse_revision_argument {
116 my ($base, $head) = @_;
117 if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
118 return ($base, $head);
120 return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
121 return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
122 return ($head, $head) if ($::_revision eq 'HEAD');
123 return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/);
124 return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
125 die "revision argument: $::_revision not understood by git-svn\n";
128 sub fetch_all {
129 my ($repo_id, $remotes) = @_;
130 if (ref $repo_id) {
131 my $gs = $repo_id;
132 $repo_id = undef;
133 $repo_id = $gs->{repo_id};
135 $remotes ||= read_all_remotes();
136 my $remote = $remotes->{$repo_id} or
137 die "[svn-remote \"$repo_id\"] unknown\n";
138 my $fetch = $remote->{fetch};
139 my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n";
140 my (@gs, @globs);
141 my $ra = Git::SVN::Ra->new($url);
142 my $uuid = $ra->get_uuid;
143 my $head = $ra->get_latest_revnum;
145 # ignore errors, $head revision may not even exist anymore
146 eval { $ra->get_log("", $head, 0, 1, 0, 1, sub { $head = $_[1] }) };
147 warn "W: $@\n" if $@;
149 my $base = defined $fetch ? $head : 0;
151 # read the max revs for wildcard expansion (branches/*, tags/*)
152 foreach my $t (qw/branches tags/) {
153 defined $remote->{$t} or next;
154 push @globs, @{$remote->{$t}};
156 my $max_rev = eval { tmp_config(qw/--int --get/,
157 "svn-remote.$repo_id.${t}-maxRev") };
158 if (defined $max_rev && ($max_rev < $base)) {
159 $base = $max_rev;
160 } elsif (!defined $max_rev) {
161 $base = 0;
165 if ($fetch) {
166 foreach my $p (sort keys %$fetch) {
167 my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
168 my $lr = $gs->rev_map_max;
169 if (defined $lr) {
170 $base = $lr if ($lr < $base);
172 push @gs, $gs;
176 ($base, $head) = parse_revision_argument($base, $head);
177 $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs);
180 sub read_all_remotes {
181 my $r = {};
182 my $use_svm_props = eval { command_oneline(qw/config --bool
183 svn.useSvmProps/) };
184 $use_svm_props = $use_svm_props eq 'true' if $use_svm_props;
185 my $svn_refspec = qr{\s*(.*?)\s*:\s*(.+?)\s*};
186 foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) {
187 if (m!^(.+)\.fetch=$svn_refspec$!) {
188 my ($remote, $local_ref, $remote_ref) = ($1, $2, $3);
189 die("svn-remote.$remote: remote ref '$remote_ref' "
190 . "must start with 'refs/'\n")
191 unless $remote_ref =~ m{^refs/};
192 $local_ref = uri_decode($local_ref);
193 $r->{$remote}->{fetch}->{$local_ref} = $remote_ref;
194 $r->{$remote}->{svm} = {} if $use_svm_props;
195 } elsif (m!^(.+)\.usesvmprops=\s*(.*)\s*$!) {
196 $r->{$1}->{svm} = {};
197 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
198 $r->{$1}->{url} = $2;
199 } elsif (m!^(.+)\.pushurl=\s*(.*)\s*$!) {
200 $r->{$1}->{pushurl} = $2;
201 } elsif (m!^(.+)\.ignore-refs=\s*(.*)\s*$!) {
202 $r->{$1}->{ignore_refs_regex} = $2;
203 } elsif (m!^(.+)\.(branches|tags)=$svn_refspec$!) {
204 my ($remote, $t, $local_ref, $remote_ref) =
205 ($1, $2, $3, $4);
206 die("svn-remote.$remote: remote ref '$remote_ref' ($t) "
207 . "must start with 'refs/'\n")
208 unless $remote_ref =~ m{^refs/};
209 $local_ref = uri_decode($local_ref);
211 require Git::SVN::GlobSpec;
212 my $rs = {
213 t => $t,
214 remote => $remote,
215 path => Git::SVN::GlobSpec->new($local_ref, 1),
216 ref => Git::SVN::GlobSpec->new($remote_ref, 0) };
217 if (length($rs->{ref}->{right}) != 0) {
218 die "The '*' glob character must be the last ",
219 "character of '$remote_ref'\n";
221 push @{ $r->{$remote}->{$t} }, $rs;
225 map {
226 if (defined $r->{$_}->{svm}) {
227 my $svm;
228 eval {
229 my $section = "svn-remote.$_";
230 $svm = {
231 source => tmp_config('--get',
232 "$section.svm-source"),
233 replace => tmp_config('--get',
234 "$section.svm-replace"),
237 $r->{$_}->{svm} = $svm;
239 } keys %$r;
241 foreach my $remote (keys %$r) {
242 foreach ( grep { defined $_ }
243 map { $r->{$remote}->{$_} } qw(branches tags) ) {
244 foreach my $rs ( @$_ ) {
245 $rs->{ignore_refs_regex} =
246 $r->{$remote}->{ignore_refs_regex};
254 sub init_vars {
255 $_gc_nr = $_gc_period = 1000;
256 if (defined $_repack || defined $_repack_flags) {
257 warn "Repack options are obsolete; they have no effect.\n";
261 sub verify_remotes_sanity {
262 return unless -d $ENV{GIT_DIR};
263 my %seen;
264 foreach (command(qw/config -l/)) {
265 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
266 if ($seen{$1}) {
267 die "Remote ref refs/remote/$1 is tracked by",
268 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
269 "Please resolve this ambiguity in ",
270 "your git configuration file before ",
271 "continuing\n";
273 $seen{$1} = $_;
278 sub find_existing_remote {
279 my ($url, $remotes) = @_;
280 return undef if $no_reuse_existing;
281 my $existing;
282 foreach my $repo_id (keys %$remotes) {
283 my $u = $remotes->{$repo_id}->{url} or next;
284 next if $u ne $url;
285 $existing = $repo_id;
286 last;
288 $existing;
291 sub init_remote_config {
292 my ($self, $url, $no_write) = @_;
293 $url =~ s!/+$!!; # strip trailing slash
294 my $r = read_all_remotes();
295 my $existing = find_existing_remote($url, $r);
296 if ($existing) {
297 unless ($no_write) {
298 print STDERR "Using existing ",
299 "[svn-remote \"$existing\"]\n";
301 $self->{repo_id} = $existing;
302 } elsif ($_minimize_url) {
303 my $min_url = Git::SVN::Ra->new($url)->minimize_url;
304 $existing = find_existing_remote($min_url, $r);
305 if ($existing) {
306 unless ($no_write) {
307 print STDERR "Using existing ",
308 "[svn-remote \"$existing\"]\n";
310 $self->{repo_id} = $existing;
312 if ($min_url ne $url) {
313 unless ($no_write) {
314 print STDERR "Using higher level of URL: ",
315 "$url => $min_url\n";
317 my $old_path = $self->{path};
318 $self->{path} = $url;
319 $self->{path} =~ s!^\Q$min_url\E(/|$)!!;
320 if (length $old_path) {
321 $self->{path} .= "/$old_path";
323 $url = $min_url;
326 my $orig_url;
327 if (!$existing) {
328 # verify that we aren't overwriting anything:
329 $orig_url = eval {
330 command_oneline('config', '--get',
331 "svn-remote.$self->{repo_id}.url")
333 if ($orig_url && ($orig_url ne $url)) {
334 die "svn-remote.$self->{repo_id}.url already set: ",
335 "$orig_url\nwanted to set to: $url\n";
338 my ($xrepo_id, $xpath) = find_ref($self->refname);
339 if (!$no_write && defined $xpath) {
340 die "svn-remote.$xrepo_id.fetch already set to track ",
341 "$xpath:", $self->refname, "\n";
343 unless ($no_write) {
344 command_noisy('config',
345 "svn-remote.$self->{repo_id}.url", $url);
346 $self->{path} =~ s{^/}{};
347 $self->{path} =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg;
348 command_noisy('config', '--add',
349 "svn-remote.$self->{repo_id}.fetch",
350 "$self->{path}:".$self->refname);
352 $self->{url} = $url;
355 sub find_by_url { # repos_root and, path are optional
356 my ($class, $full_url, $repos_root, $path) = @_;
358 return undef unless defined $full_url;
359 remove_username($full_url);
360 remove_username($repos_root) if defined $repos_root;
361 my $remotes = read_all_remotes();
362 if (defined $full_url && defined $repos_root && !defined $path) {
363 $path = $full_url;
364 $path =~ s#^\Q$repos_root\E(?:/|$)##;
366 foreach my $repo_id (keys %$remotes) {
367 my $u = $remotes->{$repo_id}->{url} or next;
368 remove_username($u);
369 next if defined $repos_root && $repos_root ne $u;
371 my $fetch = $remotes->{$repo_id}->{fetch} || {};
372 foreach my $t (qw/branches tags/) {
373 foreach my $globspec (@{$remotes->{$repo_id}->{$t}}) {
374 resolve_local_globs($u, $fetch, $globspec);
377 my $p = $path;
378 my $rwr = rewrite_root({repo_id => $repo_id});
379 my $svm = $remotes->{$repo_id}->{svm}
380 if defined $remotes->{$repo_id}->{svm};
381 unless (defined $p) {
382 $p = $full_url;
383 my $z = $u;
384 my $prefix = '';
385 if ($rwr) {
386 $z = $rwr;
387 remove_username($z);
388 } elsif (defined $svm) {
389 $z = $svm->{source};
390 $prefix = $svm->{replace};
391 $prefix =~ s#^\Q$u\E(?:/|$)##;
392 $prefix =~ s#/$##;
394 $p =~ s#^\Q$z\E(?:/|$)#$prefix# or next;
396 foreach my $f (keys %$fetch) {
397 next if $f ne $p;
398 return Git::SVN->new($fetch->{$f}, $repo_id, $f);
401 undef;
404 sub init {
405 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
406 my $self = _new($class, $repo_id, $ref_id, $path);
407 if (defined $url) {
408 $self->init_remote_config($url, $no_write);
410 $self;
413 sub find_ref {
414 my ($ref_id) = @_;
415 foreach (command(qw/config -l/)) {
416 next unless m!^svn-remote\.(.+)\.fetch=
417 \s*(.*?)\s*:\s*(.+?)\s*$!x;
418 my ($repo_id, $path, $ref) = ($1, $2, $3);
419 if ($ref eq $ref_id) {
420 $path = '' if ($path =~ m#^\./?#);
421 return ($repo_id, $path);
424 (undef, undef, undef);
427 sub new {
428 my ($class, $ref_id, $repo_id, $path) = @_;
429 if (defined $ref_id && !defined $repo_id && !defined $path) {
430 ($repo_id, $path) = find_ref($ref_id);
431 if (!defined $repo_id) {
432 die "Could not find a \"svn-remote.*.fetch\" key ",
433 "in the repository configuration matching: ",
434 "$ref_id\n";
437 my $self = _new($class, $repo_id, $ref_id, $path);
438 if (!defined $self->{path} || !length $self->{path}) {
439 my $fetch = command_oneline('config', '--get',
440 "svn-remote.$repo_id.fetch",
441 ":$ref_id\$") or
442 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
443 "\":$ref_id\$\" in config\n";
444 ($self->{path}, undef) = split(/\s*:\s*/, $fetch);
446 $self->{path} =~ s{/+}{/}g;
447 $self->{path} =~ s{\A/}{};
448 $self->{path} =~ s{/\z}{};
449 $self->{url} = command_oneline('config', '--get',
450 "svn-remote.$repo_id.url") or
451 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
452 $self->{pushurl} = eval { command_oneline('config', '--get',
453 "svn-remote.$repo_id.pushurl") };
454 $self->rebuild;
455 $self;
458 sub refname {
459 my ($refname) = $_[0]->{ref_id} ;
461 # It cannot end with a slash /, we'll throw up on this because
462 # SVN can't have directories with a slash in their name, either:
463 if ($refname =~ m{/$}) {
464 die "ref: '$refname' ends with a trailing slash, this is ",
465 "not permitted by git nor Subversion\n";
468 # It cannot have ASCII control character space, tilde ~, caret ^,
469 # colon :, question-mark ?, asterisk *, space, or open bracket [
470 # anywhere.
472 # Additionally, % must be escaped because it is used for escaping
473 # and we want our escaped refname to be reversible
474 $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;
476 # no slash-separated component can begin with a dot .
477 # /.* becomes /%2E*
478 $refname =~ s{/\.}{/%2E}g;
480 # It cannot have two consecutive dots .. anywhere
481 # .. becomes %2E%2E
482 $refname =~ s{\.\.}{%2E%2E}g;
484 # trailing dots and .lock are not allowed
485 # .$ becomes %2E and .lock becomes %2Elock
486 $refname =~ s{\.(?=$|lock$)}{%2E};
488 # the sequence @{ is used to access the reflog
489 # @{ becomes %40{
490 $refname =~ s{\@\{}{%40\{}g;
492 return $refname;
495 sub desanitize_refname {
496 my ($refname) = @_;
497 $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
498 return $refname;
501 sub svm_uuid {
502 my ($self) = @_;
503 return $self->{svm}->{uuid} if $self->svm;
504 $self->ra;
505 unless ($self->{svm}) {
506 die "SVM UUID not cached, and reading remotely failed\n";
508 $self->{svm}->{uuid};
511 sub svm {
512 my ($self) = @_;
513 return $self->{svm} if $self->{svm};
514 my $svm;
515 # see if we have it in our config, first:
516 eval {
517 my $section = "svn-remote.$self->{repo_id}";
518 $svm = {
519 source => tmp_config('--get', "$section.svm-source"),
520 uuid => tmp_config('--get', "$section.svm-uuid"),
521 replace => tmp_config('--get', "$section.svm-replace"),
524 if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) {
525 $self->{svm} = $svm;
527 $self->{svm};
530 sub _set_svm_vars {
531 my ($self, $ra) = @_;
532 return $ra if $self->svm;
534 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
535 "(svm:source, svm:uuid) ",
536 "from the following URLs:\n" );
537 sub read_svm_props {
538 my ($self, $ra, $path, $r) = @_;
539 my $props = ($ra->get_dir($path, $r))[2];
540 my $src = $props->{'svm:source'};
541 my $uuid = $props->{'svm:uuid'};
542 return undef if (!$src || !$uuid);
544 chomp($src, $uuid);
546 $uuid =~ m{^[0-9a-f\-]{30,}$}i
547 or die "doesn't look right - svm:uuid is '$uuid'\n";
549 # the '!' is used to mark the repos_root!/relative/path
550 $src =~ s{/?!/?}{/};
551 $src =~ s{/+$}{}; # no trailing slashes please
552 # username is of no interest
553 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
555 my $replace = $ra->{url};
556 $replace .= "/$path" if length $path;
558 my $section = "svn-remote.$self->{repo_id}";
559 tmp_config("$section.svm-source", $src);
560 tmp_config("$section.svm-replace", $replace);
561 tmp_config("$section.svm-uuid", $uuid);
562 $self->{svm} = {
563 source => $src,
564 uuid => $uuid,
565 replace => $replace
569 my $r = $ra->get_latest_revnum;
570 my $path = $self->{path};
571 my %tried;
572 while (length $path) {
573 unless ($tried{"$self->{url}/$path"}) {
574 return $ra if $self->read_svm_props($ra, $path, $r);
575 $tried{"$self->{url}/$path"} = 1;
577 $path =~ s#/?[^/]+$##;
579 die "Path: '$path' should be ''\n" if $path ne '';
580 return $ra if $self->read_svm_props($ra, $path, $r);
581 $tried{"$self->{url}/$path"} = 1;
583 if ($ra->{repos_root} eq $self->{url}) {
584 die @err, (map { " $_\n" } keys %tried), "\n";
587 # nope, make sure we're connected to the repository root:
588 my $ok;
589 my @tried_b;
590 $path = $ra->{svn_path};
591 $ra = Git::SVN::Ra->new($ra->{repos_root});
592 while (length $path) {
593 unless ($tried{"$ra->{url}/$path"}) {
594 $ok = $self->read_svm_props($ra, $path, $r);
595 last if $ok;
596 $tried{"$ra->{url}/$path"} = 1;
598 $path =~ s#/?[^/]+$##;
600 die "Path: '$path' should be ''\n" if $path ne '';
601 $ok ||= $self->read_svm_props($ra, $path, $r);
602 $tried{"$ra->{url}/$path"} = 1;
603 if (!$ok) {
604 die @err, (map { " $_\n" } keys %tried), "\n";
606 Git::SVN::Ra->new($self->{url});
609 sub svnsync {
610 my ($self) = @_;
611 return $self->{svnsync} if $self->{svnsync};
613 if ($self->no_metadata) {
614 die "Can't have both 'noMetadata' and ",
615 "'useSvnsyncProps' options set!\n";
617 if ($self->rewrite_root) {
618 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
619 "options set!\n";
621 if ($self->rewrite_uuid) {
622 die "Can't have both 'useSvnsyncProps' and 'rewriteUUID' ",
623 "options set!\n";
626 my $svnsync;
627 # see if we have it in our config, first:
628 eval {
629 my $section = "svn-remote.$self->{repo_id}";
631 my $url = tmp_config('--get', "$section.svnsync-url");
632 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
633 die "doesn't look right - svn:sync-from-url is '$url'\n";
635 my $uuid = tmp_config('--get', "$section.svnsync-uuid");
636 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or
637 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
639 $svnsync = { url => $url, uuid => $uuid }
641 if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) {
642 return $self->{svnsync} = $svnsync;
645 my $err = "useSvnsyncProps set, but failed to read " .
646 "svnsync property: svn:sync-from-";
647 my $rp = $self->ra->rev_proplist(0);
649 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
650 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
651 die "doesn't look right - svn:sync-from-url is '$url'\n";
653 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
654 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or
655 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
657 my $section = "svn-remote.$self->{repo_id}";
658 tmp_config('--add', "$section.svnsync-uuid", $uuid);
659 tmp_config('--add', "$section.svnsync-url", $url);
660 return $self->{svnsync} = { url => $url, uuid => $uuid };
663 # this allows us to memoize our SVN::Ra UUID locally and avoid a
664 # remote lookup (useful for 'git svn log').
665 sub ra_uuid {
666 my ($self) = @_;
667 unless ($self->{ra_uuid}) {
668 my $key = "svn-remote.$self->{repo_id}.uuid";
669 my $uuid = eval { tmp_config('--get', $key) };
670 if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/i) {
671 $self->{ra_uuid} = $uuid;
672 } else {
673 die "ra_uuid called without URL\n" unless $self->{url};
674 $self->{ra_uuid} = $self->ra->get_uuid;
675 tmp_config('--add', $key, $self->{ra_uuid});
678 $self->{ra_uuid};
681 sub _set_repos_root {
682 my ($self, $repos_root) = @_;
683 my $k = "svn-remote.$self->{repo_id}.reposRoot";
684 $repos_root ||= $self->ra->{repos_root};
685 tmp_config($k, $repos_root);
686 $repos_root;
689 sub repos_root {
690 my ($self) = @_;
691 my $k = "svn-remote.$self->{repo_id}.reposRoot";
692 eval { tmp_config('--get', $k) } || $self->_set_repos_root;
695 sub ra {
696 my ($self) = shift;
697 my $ra = Git::SVN::Ra->new($self->{url});
698 $self->_set_repos_root($ra->{repos_root});
699 if ($self->use_svm_props && !$self->{svm}) {
700 if ($self->no_metadata) {
701 die "Can't have both 'noMetadata' and ",
702 "'useSvmProps' options set!\n";
703 } elsif ($self->use_svnsync_props) {
704 die "Can't have both 'useSvnsyncProps' and ",
705 "'useSvmProps' options set!\n";
707 $ra = $self->_set_svm_vars($ra);
708 $self->{-want_revprops} = 1;
710 $ra;
713 # prop_walk(PATH, REV, SUB)
714 # -------------------------
715 # Recursively traverse PATH at revision REV and invoke SUB for each
716 # directory that contains a SVN property. SUB will be invoked as
717 # follows: &SUB(gs, path, props); where `gs' is this instance of
718 # Git::SVN, `path' the path to the directory where the properties
719 # `props' were found. The `path' will be relative to point of checkout,
720 # that is, if url://repo/trunk is the current Git branch, and that
721 # directory contains a sub-directory `d', SUB will be invoked with `/d/'
722 # as `path' (note the trailing `/').
723 sub prop_walk {
724 my ($self, $path, $rev, $sub) = @_;
726 $path =~ s#^/##;
727 my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
728 $path =~ s#^/*#/#g;
729 my $p = $path;
730 # Strip the irrelevant part of the path.
731 $p =~ s#^/+\Q$self->{path}\E(/|$)#/#;
732 # Ensure the path is terminated by a `/'.
733 $p =~ s#/*$#/#;
735 # The properties contain all the internal SVN stuff nobody
736 # (usually) cares about.
737 my $interesting_props = 0;
738 foreach (keys %{$props}) {
739 # If it doesn't start with `svn:', it must be a
740 # user-defined property.
741 ++$interesting_props and next if $_ !~ /^svn:/;
742 # FIXME: Fragile, if SVN adds new public properties,
743 # this needs to be updated.
744 ++$interesting_props if /^svn:(?:ignore|keywords|executable
745 |eol-style|mime-type
746 |externals|needs-lock)$/x;
748 &$sub($self, $p, $props) if $interesting_props;
750 foreach (sort keys %$dirent) {
751 next if $dirent->{$_}->{kind} != $SVN::Node::dir;
752 $self->prop_walk($self->{path} . $p . $_, $rev, $sub);
756 sub last_rev { ($_[0]->last_rev_commit)[0] }
757 sub last_commit { ($_[0]->last_rev_commit)[1] }
759 # returns the newest SVN revision number and newest commit SHA1
760 sub last_rev_commit {
761 my ($self) = @_;
762 if (defined $self->{last_rev} && defined $self->{last_commit}) {
763 return ($self->{last_rev}, $self->{last_commit});
765 my $c = ::verify_ref($self->refname.'^0');
766 if ($c && !$self->use_svm_props && !$self->no_metadata) {
767 my $rev = (::cmt_metadata($c))[1];
768 if (defined $rev) {
769 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
770 return ($rev, $c);
773 my $map_path = $self->map_path;
774 unless (-e $map_path) {
775 ($self->{last_rev}, $self->{last_commit}) = (undef, undef);
776 return (undef, undef);
778 my ($rev, $commit) = $self->rev_map_max(1);
779 ($self->{last_rev}, $self->{last_commit}) = ($rev, $commit);
780 return ($rev, $commit);
783 sub get_fetch_range {
784 my ($self, $min, $max) = @_;
785 $max ||= $self->ra->get_latest_revnum;
786 $min ||= $self->rev_map_max;
787 (++$min, $max);
790 sub tmp_config {
791 my (@args) = @_;
792 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
793 my $config = "$ENV{GIT_DIR}/svn/.metadata";
794 if (! -f $config && -f $old_def_config) {
795 rename $old_def_config, $config or
796 die "Failed rename $old_def_config => $config: $!\n";
798 my $old_config = $ENV{GIT_CONFIG};
799 $ENV{GIT_CONFIG} = $config;
800 $@ = undef;
801 my @ret = eval {
802 unless (-f $config) {
803 mkfile($config);
804 open my $fh, '>', $config or
805 die "Can't open $config: $!\n";
806 print $fh "; This file is used internally by ",
807 "git-svn\n" or die
808 "Couldn't write to $config: $!\n";
809 print $fh "; You should not have to edit it\n" or
810 die "Couldn't write to $config: $!\n";
811 close $fh or die "Couldn't close $config: $!\n";
813 command('config', @args);
815 my $err = $@;
816 if (defined $old_config) {
817 $ENV{GIT_CONFIG} = $old_config;
818 } else {
819 delete $ENV{GIT_CONFIG};
821 die $err if $err;
822 wantarray ? @ret : $ret[0];
825 sub tmp_index_do {
826 my ($self, $sub) = @_;
827 my $old_index = $ENV{GIT_INDEX_FILE};
828 $ENV{GIT_INDEX_FILE} = $self->{index};
829 $@ = undef;
830 my @ret = eval {
831 my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#);
832 mkpath([$dir]) unless -d $dir;
833 &$sub;
835 my $err = $@;
836 if (defined $old_index) {
837 $ENV{GIT_INDEX_FILE} = $old_index;
838 } else {
839 delete $ENV{GIT_INDEX_FILE};
841 die $err if $err;
842 wantarray ? @ret : $ret[0];
845 sub assert_index_clean {
846 my ($self, $treeish) = @_;
848 $self->tmp_index_do(sub {
849 command_noisy('read-tree', $treeish) unless -e $self->{index};
850 my $x = command_oneline('write-tree');
851 my ($y) = (command(qw/cat-file commit/, $treeish) =~
852 /^tree ($::sha1)/mo);
853 return if $y eq $x;
855 warn "Index mismatch: $y != $x\nrereading $treeish\n";
856 unlink $self->{index} or die "unlink $self->{index}: $!\n";
857 command_noisy('read-tree', $treeish);
858 $x = command_oneline('write-tree');
859 if ($y ne $x) {
860 fatal "trees ($treeish) $y != $x\n",
861 "Something is seriously wrong...";
866 sub get_commit_parents {
867 my ($self, $log_entry) = @_;
868 my (%seen, @ret, @tmp);
869 # legacy support for 'set-tree'; this is only used by set_tree_cb:
870 if (my $ip = $self->{inject_parents}) {
871 if (my $commit = delete $ip->{$log_entry->{revision}}) {
872 push @tmp, $commit;
875 if (my $cur = ::verify_ref($self->refname.'^0')) {
876 push @tmp, $cur;
878 if (my $ipd = $self->{inject_parents_dcommit}) {
879 if (my $commit = delete $ipd->{$log_entry->{revision}}) {
880 push @tmp, @$commit;
883 push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp);
884 while (my $p = shift @tmp) {
885 next if $seen{$p};
886 $seen{$p} = 1;
887 push @ret, $p;
889 @ret;
892 sub rewrite_root {
893 my ($self) = @_;
894 return $self->{-rewrite_root} if exists $self->{-rewrite_root};
895 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
896 my $rwr = eval { command_oneline(qw/config --get/, $k) };
897 if ($rwr) {
898 $rwr =~ s#/+$##;
899 if ($rwr !~ m#^[a-z\+]+://#) {
900 die "$rwr is not a valid URL (key: $k)\n";
903 $self->{-rewrite_root} = $rwr;
906 sub rewrite_uuid {
907 my ($self) = @_;
908 return $self->{-rewrite_uuid} if exists $self->{-rewrite_uuid};
909 my $k = "svn-remote.$self->{repo_id}.rewriteUUID";
910 my $rwid = eval { command_oneline(qw/config --get/, $k) };
911 if ($rwid) {
912 $rwid =~ s#/+$##;
913 if ($rwid !~ m#^[a-f0-9]{8}-(?:[a-f0-9]{4}-){3}[a-f0-9]{12}$#) {
914 die "$rwid is not a valid UUID (key: $k)\n";
917 $self->{-rewrite_uuid} = $rwid;
920 sub metadata_url {
921 my ($self) = @_;
922 ($self->rewrite_root || $self->{url}) .
923 (length $self->{path} ? '/' . $self->{path} : '');
926 sub full_url {
927 my ($self) = @_;
928 $self->{url} . (length $self->{path} ? '/' . $self->{path} : '');
931 sub full_pushurl {
932 my ($self) = @_;
933 if ($self->{pushurl}) {
934 return $self->{pushurl} . (length $self->{path} ? '/' .
935 $self->{path} : '');
936 } else {
937 return $self->full_url;
941 sub set_commit_header_env {
942 my ($log_entry) = @_;
943 my %env;
944 foreach my $ned (qw/NAME EMAIL DATE/) {
945 foreach my $ac (qw/AUTHOR COMMITTER/) {
946 $env{"GIT_${ac}_${ned}"} = $ENV{"GIT_${ac}_${ned}"};
950 $ENV{GIT_AUTHOR_NAME} = $log_entry->{name};
951 $ENV{GIT_AUTHOR_EMAIL} = $log_entry->{email};
952 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
954 $ENV{GIT_COMMITTER_NAME} = (defined $log_entry->{commit_name})
955 ? $log_entry->{commit_name}
956 : $log_entry->{name};
957 $ENV{GIT_COMMITTER_EMAIL} = (defined $log_entry->{commit_email})
958 ? $log_entry->{commit_email}
959 : $log_entry->{email};
960 \%env;
963 sub restore_commit_header_env {
964 my ($env) = @_;
965 foreach my $ned (qw/NAME EMAIL DATE/) {
966 foreach my $ac (qw/AUTHOR COMMITTER/) {
967 my $k = "GIT_${ac}_${ned}";
968 if (defined $env->{$k}) {
969 $ENV{$k} = $env->{$k};
970 } else {
971 delete $ENV{$k};
977 sub gc {
978 command_noisy('gc', '--auto');
981 sub do_git_commit {
982 my ($self, $log_entry) = @_;
983 my $lr = $self->last_rev;
984 if (defined $lr && $lr >= $log_entry->{revision}) {
985 die "Last fetched revision of ", $self->refname,
986 " was r$lr, but we are about to fetch: ",
987 "r$log_entry->{revision}!\n";
989 if (my $c = $self->rev_map_get($log_entry->{revision})) {
990 croak "$log_entry->{revision} = $c already exists! ",
991 "Why are we refetching it?\n";
993 my $old_env = set_commit_header_env($log_entry);
994 my $tree = $log_entry->{tree};
995 if (!defined $tree) {
996 $tree = $self->tmp_index_do(sub {
997 command_oneline('write-tree') });
999 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
1001 my @exec = ('git', 'commit-tree', $tree);
1002 foreach ($self->get_commit_parents($log_entry)) {
1003 push @exec, '-p', $_;
1005 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
1006 or croak $!;
1007 binmode $msg_fh;
1009 # we always get UTF-8 from SVN, but we may want our commits in
1010 # a different encoding.
1011 if (my $enc = Git::config('i18n.commitencoding')) {
1012 require Encode;
1013 Encode::from_to($log_entry->{log}, 'UTF-8', $enc);
1015 print $msg_fh $log_entry->{log} or croak $!;
1016 restore_commit_header_env($old_env);
1017 unless ($self->no_metadata) {
1018 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
1019 or croak $!;
1021 $msg_fh->flush == 0 or croak $!;
1022 close $msg_fh or croak $!;
1023 chomp(my $commit = do { local $/; <$out_fh> });
1024 close $out_fh or croak $!;
1025 waitpid $pid, 0;
1026 croak $? if $?;
1027 if ($commit !~ /^$::sha1$/o) {
1028 die "Failed to commit, invalid sha1: $commit\n";
1031 $self->rev_map_set($log_entry->{revision}, $commit, 1);
1033 $self->{last_rev} = $log_entry->{revision};
1034 $self->{last_commit} = $commit;
1035 print "r$log_entry->{revision}" unless $::_q > 1;
1036 if (defined $log_entry->{svm_revision}) {
1037 print " (\@$log_entry->{svm_revision})" unless $::_q > 1;
1038 $self->rev_map_set($log_entry->{svm_revision}, $commit,
1039 0, $self->svm_uuid);
1041 print " = $commit ($self->{ref_id})\n" unless $::_q > 1;
1042 if (--$_gc_nr == 0) {
1043 $_gc_nr = $_gc_period;
1044 gc();
1046 return $commit;
1049 sub match_paths {
1050 my ($self, $paths, $r) = @_;
1051 return 1 if $self->{path} eq '';
1052 if (my $path = $paths->{"/$self->{path}"}) {
1053 return ($path->{action} eq 'D') ? 0 : 1;
1055 $self->{path_regex} ||= qr/^\/\Q$self->{path}\E\//;
1056 if (grep /$self->{path_regex}/, keys %$paths) {
1057 return 1;
1059 my $c = '';
1060 foreach (split m#/#, $self->{path}) {
1061 $c .= "/$_";
1062 next unless ($paths->{$c} &&
1063 ($paths->{$c}->{action} =~ /^[AR]$/));
1064 if ($self->ra->check_path($self->{path}, $r) ==
1065 $SVN::Node::dir) {
1066 return 1;
1069 return 0;
1072 sub find_parent_branch {
1073 my ($self, $paths, $rev) = @_;
1074 return undef unless $self->follow_parent;
1075 unless (defined $paths) {
1076 my $err_handler = $SVN::Error::handler;
1077 $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs;
1078 $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1,
1079 sub { $paths = $_[0] });
1080 $SVN::Error::handler = $err_handler;
1082 return undef unless defined $paths;
1084 # look for a parent from another branch:
1085 my @b_path_components = split m#/#, $self->{path};
1086 my @a_path_components;
1087 my $i;
1088 while (@b_path_components) {
1089 $i = $paths->{'/'.join('/', @b_path_components)};
1090 last if $i && defined $i->{copyfrom_path};
1091 unshift(@a_path_components, pop(@b_path_components));
1093 return undef unless defined $i && defined $i->{copyfrom_path};
1094 my $branch_from = $i->{copyfrom_path};
1095 if (@a_path_components) {
1096 print STDERR "branch_from: $branch_from => ";
1097 $branch_from .= '/'.join('/', @a_path_components);
1098 print STDERR $branch_from, "\n";
1100 my $r = $i->{copyfrom_rev};
1101 my $repos_root = $self->ra->{repos_root};
1102 my $url = $self->ra->{url};
1103 my $new_url = $url . $branch_from;
1104 print STDERR "Found possible branch point: ",
1105 "$new_url => ", $self->full_url, ", $r\n"
1106 unless $::_q > 1;
1107 $branch_from =~ s#^/##;
1108 my $gs = $self->other_gs($new_url, $url,
1109 $branch_from, $r, $self->{ref_id});
1110 my ($r0, $parent) = $gs->find_rev_before($r, 1);
1112 my ($base, $head);
1113 if (!defined $r0 || !defined $parent) {
1114 ($base, $head) = parse_revision_argument(0, $r);
1115 } else {
1116 if ($r0 < $r) {
1117 $gs->ra->get_log([$gs->{path}], $r0 + 1, $r, 1,
1118 0, 1, sub { $base = $_[1] - 1 });
1121 if (defined $base && $base <= $r) {
1122 $gs->fetch($base, $r);
1124 ($r0, $parent) = $gs->find_rev_before($r, 1);
1126 if (defined $r0 && defined $parent) {
1127 print STDERR "Found branch parent: ($self->{ref_id}) $parent\n"
1128 unless $::_q > 1;
1129 my $ed;
1130 if ($self->ra->can_do_switch) {
1131 $self->assert_index_clean($parent);
1132 print STDERR "Following parent with do_switch\n"
1133 unless $::_q > 1;
1134 # do_switch works with svn/trunk >= r22312, but that
1135 # is not included with SVN 1.4.3 (the latest version
1136 # at the moment), so we can't rely on it
1137 $self->{last_rev} = $r0;
1138 $self->{last_commit} = $parent;
1139 $ed = Git::SVN::Fetcher->new($self, $gs->{path});
1140 $gs->ra->gs_do_switch($r0, $rev, $gs,
1141 $self->full_url, $ed)
1142 or die "SVN connection failed somewhere...\n";
1143 } elsif ($self->ra->trees_match($new_url, $r0,
1144 $self->full_url, $rev)) {
1145 print STDERR "Trees match:\n",
1146 " $new_url\@$r0\n",
1147 " ${\$self->full_url}\@$rev\n",
1148 "Following parent with no changes\n"
1149 unless $::_q > 1;
1150 $self->tmp_index_do(sub {
1151 command_noisy('read-tree', $parent);
1153 $self->{last_commit} = $parent;
1154 } else {
1155 print STDERR "Following parent with do_update\n"
1156 unless $::_q > 1;
1157 $ed = Git::SVN::Fetcher->new($self);
1158 $self->ra->gs_do_update($rev, $rev, $self, $ed)
1159 or die "SVN connection failed somewhere...\n";
1161 print STDERR "Successfully followed parent\n" unless $::_q > 1;
1162 return $self->make_log_entry($rev, [$parent], $ed);
1164 return undef;
1167 sub do_fetch {
1168 my ($self, $paths, $rev) = @_;
1169 my $ed;
1170 my ($last_rev, @parents);
1171 if (my $lc = $self->last_commit) {
1172 # we can have a branch that was deleted, then re-added
1173 # under the same name but copied from another path, in
1174 # which case we'll have multiple parents (we don't
1175 # want to break the original ref, nor lose copypath info):
1176 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
1177 push @{$log_entry->{parents}}, $lc;
1178 return $log_entry;
1180 $ed = Git::SVN::Fetcher->new($self);
1181 $last_rev = $self->{last_rev};
1182 $ed->{c} = $lc;
1183 @parents = ($lc);
1184 } else {
1185 $last_rev = $rev;
1186 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
1187 return $log_entry;
1189 $ed = Git::SVN::Fetcher->new($self);
1191 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
1192 die "SVN connection failed somewhere...\n";
1194 $self->make_log_entry($rev, \@parents, $ed);
1197 sub mkemptydirs {
1198 my ($self, $r) = @_;
1200 sub scan {
1201 my ($r, $empty_dirs, $line) = @_;
1202 if (defined $r && $line =~ /^r(\d+)$/) {
1203 return 0 if $1 > $r;
1204 } elsif ($line =~ /^ \+empty_dir: (.+)$/) {
1205 $empty_dirs->{$1} = 1;
1206 } elsif ($line =~ /^ \-empty_dir: (.+)$/) {
1207 my @d = grep {m[^\Q$1\E(/|$)]} (keys %$empty_dirs);
1208 delete @$empty_dirs{@d};
1210 1; # continue
1213 my %empty_dirs = ();
1214 my $gz_file = "$self->{dir}/unhandled.log.gz";
1215 if (-f $gz_file) {
1216 if (!can_compress()) {
1217 warn "Compress::Zlib could not be found; ",
1218 "empty directories in $gz_file will not be read\n";
1219 } else {
1220 my $gz = Compress::Zlib::gzopen($gz_file, "rb") or
1221 die "Unable to open $gz_file: $!\n";
1222 my $line;
1223 while ($gz->gzreadline($line) > 0) {
1224 scan($r, \%empty_dirs, $line) or last;
1226 $gz->gzclose;
1230 if (open my $fh, '<', "$self->{dir}/unhandled.log") {
1231 binmode $fh or croak "binmode: $!";
1232 while (<$fh>) {
1233 scan($r, \%empty_dirs, $_) or last;
1235 close $fh;
1238 my $strip = qr/\A\Q$self->{path}\E(?:\/|$)/;
1239 foreach my $d (sort keys %empty_dirs) {
1240 $d = uri_decode($d);
1241 $d =~ s/$strip//;
1242 next unless length($d);
1243 next if -d $d;
1244 if (-e $d) {
1245 warn "$d exists but is not a directory\n";
1246 } else {
1247 print "creating empty directory: $d\n";
1248 mkpath([$d]);
1253 sub get_untracked {
1254 my ($self, $ed) = @_;
1255 my @out;
1256 my $h = $ed->{empty};
1257 foreach (sort keys %$h) {
1258 my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
1259 push @out, " $act: " . uri_encode($_);
1260 warn "W: $act: $_\n";
1262 foreach my $t (qw/dir_prop file_prop/) {
1263 $h = $ed->{$t} or next;
1264 foreach my $path (sort keys %$h) {
1265 my $ppath = $path eq '' ? '.' : $path;
1266 foreach my $prop (sort keys %{$h->{$path}}) {
1267 next if $SKIP_PROP{$prop};
1268 my $v = $h->{$path}->{$prop};
1269 my $t_ppath_prop = "$t: " .
1270 uri_encode($ppath) . ' ' .
1271 uri_encode($prop);
1272 if (defined $v) {
1273 push @out, " +$t_ppath_prop " .
1274 uri_encode($v);
1275 } else {
1276 push @out, " -$t_ppath_prop";
1281 foreach my $t (qw/absent_file absent_directory/) {
1282 $h = $ed->{$t} or next;
1283 foreach my $parent (sort keys %$h) {
1284 foreach my $path (sort @{$h->{$parent}}) {
1285 push @out, " $t: " .
1286 uri_encode("$parent/$path");
1287 warn "W: $t: $parent/$path ",
1288 "Insufficient permissions?\n";
1292 \@out;
1295 sub get_tz {
1296 # some systmes don't handle or mishandle %z, so be creative.
1297 my $t = shift || time;
1298 my $gm = timelocal(gmtime($t));
1299 my $sign = qw( + + - )[ $t <=> $gm ];
1300 return sprintf("%s%02d%02d", $sign, (gmtime(abs($t - $gm)))[2,1]);
1303 # parse_svn_date(DATE)
1304 # --------------------
1305 # Given a date (in UTC) from Subversion, return a string in the format
1306 # "<TZ Offset> <local date/time>" that Git will use.
1308 # By default the parsed date will be in UTC; if $Git::SVN::_localtime
1309 # is true we'll convert it to the local timezone instead.
1310 sub parse_svn_date {
1311 my $date = shift || return '+0000 1970-01-01 00:00:00';
1312 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
1313 (\d\d)\:(\d\d)\:(\d\d)\.\d*Z$/x) or
1314 croak "Unable to parse date: $date\n";
1315 my $parsed_date; # Set next.
1317 if ($Git::SVN::_localtime) {
1318 # Translate the Subversion datetime to an epoch time.
1319 # Begin by switching ourselves to $date's timezone, UTC.
1320 my $old_env_TZ = $ENV{TZ};
1321 $ENV{TZ} = 'UTC';
1323 my $epoch_in_UTC =
1324 POSIX::strftime('%s', $S, $M, $H, $d, $m - 1, $Y - 1900);
1326 # Determine our local timezone (including DST) at the
1327 # time of $epoch_in_UTC. $Git::SVN::Log::TZ stored the
1328 # value of TZ, if any, at the time we were run.
1329 if (defined $Git::SVN::Log::TZ) {
1330 $ENV{TZ} = $Git::SVN::Log::TZ;
1331 } else {
1332 delete $ENV{TZ};
1335 my $our_TZ = get_tz();
1337 # This converts $epoch_in_UTC into our local timezone.
1338 my ($sec, $min, $hour, $mday, $mon, $year,
1339 $wday, $yday, $isdst) = localtime($epoch_in_UTC);
1341 $parsed_date = sprintf('%s %04d-%02d-%02d %02d:%02d:%02d',
1342 $our_TZ, $year + 1900, $mon + 1,
1343 $mday, $hour, $min, $sec);
1345 # Reset us to the timezone in effect when we entered
1346 # this routine.
1347 if (defined $old_env_TZ) {
1348 $ENV{TZ} = $old_env_TZ;
1349 } else {
1350 delete $ENV{TZ};
1352 } else {
1353 $parsed_date = "+0000 $Y-$m-$d $H:$M:$S";
1356 return $parsed_date;
1359 sub other_gs {
1360 my ($self, $new_url, $url,
1361 $branch_from, $r, $old_ref_id) = @_;
1362 my $gs = Git::SVN->find_by_url($new_url, $url, $branch_from);
1363 unless ($gs) {
1364 my $ref_id = $old_ref_id;
1365 $ref_id =~ s/\@\d+-*$//;
1366 $ref_id .= "\@$r";
1367 # just grow a tail if we're not unique enough :x
1368 $ref_id .= '-' while find_ref($ref_id);
1369 my ($u, $p, $repo_id) = ($new_url, '', $ref_id);
1370 if ($u =~ s#^\Q$url\E(/|$)##) {
1371 $p = $u;
1372 $u = $url;
1373 $repo_id = $self->{repo_id};
1375 while (1) {
1376 # It is possible to tag two different subdirectories at
1377 # the same revision. If the url for an existing ref
1378 # does not match, we must either find a ref with a
1379 # matching url or create a new ref by growing a tail.
1380 $gs = Git::SVN->init($u, $p, $repo_id, $ref_id, 1);
1381 my (undef, $max_commit) = $gs->rev_map_max(1);
1382 last if (!$max_commit);
1383 my ($url) = ::cmt_metadata($max_commit);
1384 last if ($url eq $gs->metadata_url);
1385 $ref_id .= '-';
1387 print STDERR "Initializing parent: $ref_id\n" unless $::_q > 1;
1392 sub call_authors_prog {
1393 my ($orig_author) = @_;
1394 $orig_author = command_oneline('rev-parse', '--sq-quote', $orig_author);
1395 my $author = `$::_authors_prog $orig_author`;
1396 if ($? != 0) {
1397 die "$::_authors_prog failed with exit code $?\n"
1399 if ($author =~ /^\s*(.+?)\s*<(.*)>\s*$/) {
1400 my ($name, $email) = ($1, $2);
1401 $email = undef if length $2 == 0;
1402 return [$name, $email];
1403 } else {
1404 die "Author: $orig_author: $::_authors_prog returned "
1405 . "invalid author format: $author\n";
1409 sub check_author {
1410 my ($author) = @_;
1411 if (!defined $author || length $author == 0) {
1412 $author = '(no author)';
1414 if (!defined $::users{$author}) {
1415 if (defined $::_authors_prog) {
1416 $::users{$author} = call_authors_prog($author);
1417 } elsif (defined $::_authors) {
1418 die "Author: $author not defined in $::_authors file\n";
1421 $author;
1424 sub find_extra_svk_parents {
1425 my ($self, $ed, $tickets, $parents) = @_;
1426 # aha! svk:merge property changed...
1427 my @tickets = split "\n", $tickets;
1428 my @known_parents;
1429 for my $ticket ( @tickets ) {
1430 my ($uuid, $path, $rev) = split /:/, $ticket;
1431 if ( $uuid eq $self->ra_uuid ) {
1432 my $url = $self->{url};
1433 my $repos_root = $url;
1434 my $branch_from = $path;
1435 $branch_from =~ s{^/}{};
1436 my $gs = $self->other_gs($repos_root."/".$branch_from,
1437 $url,
1438 $branch_from,
1439 $rev,
1440 $self->{ref_id});
1441 if ( my $commit = $gs->rev_map_get($rev, $uuid) ) {
1442 # wahey! we found it, but it might be
1443 # an old one (!)
1444 push @known_parents, [ $rev, $commit ];
1448 # Ordering matters; highest-numbered commit merge tickets
1449 # first, as they may account for later merge ticket additions
1450 # or changes.
1451 @known_parents = map {$_->[1]} sort {$b->[0] <=> $a->[0]} @known_parents;
1452 for my $parent ( @known_parents ) {
1453 my @cmd = ('rev-list', $parent, map { "^$_" } @$parents );
1454 my ($msg_fh, $ctx) = command_output_pipe(@cmd);
1455 my $new;
1456 while ( <$msg_fh> ) {
1457 $new=1;last;
1459 command_close_pipe($msg_fh, $ctx);
1460 if ( $new ) {
1461 print STDERR
1462 "Found merge parent (svk:merge ticket): $parent\n";
1463 push @$parents, $parent;
1468 sub lookup_svn_merge {
1469 my $uuid = shift;
1470 my $url = shift;
1471 my $merge = shift;
1473 my ($source, $revs) = split ":", $merge;
1474 my $path = $source;
1475 $path =~ s{^/}{};
1476 my $gs = Git::SVN->find_by_url($url.$source, $url, $path);
1477 if ( !$gs ) {
1478 warn "Couldn't find revmap for $url$source\n";
1479 return;
1481 my @ranges = split ",", $revs;
1482 my ($tip, $tip_commit);
1483 my @merged_commit_ranges;
1484 # find the tip
1485 for my $range ( @ranges ) {
1486 my ($bottom, $top) = split "-", $range;
1487 $top ||= $bottom;
1488 my $bottom_commit = $gs->find_rev_after( $bottom, 1, $top );
1489 my $top_commit = $gs->find_rev_before( $top, 1, $bottom );
1491 unless ($top_commit and $bottom_commit) {
1492 warn "W:unknown path/rev in svn:mergeinfo "
1493 ."dirprop: $source:$range\n";
1494 next;
1497 if (scalar(command('rev-parse', "$bottom_commit^@"))) {
1498 push @merged_commit_ranges,
1499 "$bottom_commit^..$top_commit";
1500 } else {
1501 push @merged_commit_ranges, "$top_commit";
1504 if ( !defined $tip or $top > $tip ) {
1505 $tip = $top;
1506 $tip_commit = $top_commit;
1509 return ($tip_commit, @merged_commit_ranges);
1512 sub _rev_list {
1513 my ($msg_fh, $ctx) = command_output_pipe(
1514 "rev-list", @_,
1516 my @rv;
1517 while ( <$msg_fh> ) {
1518 chomp;
1519 push @rv, $_;
1521 command_close_pipe($msg_fh, $ctx);
1522 @rv;
1525 sub check_cherry_pick {
1526 my $base = shift;
1527 my $tip = shift;
1528 my $parents = shift;
1529 my @ranges = @_;
1530 my %commits = map { $_ => 1 }
1531 _rev_list("--no-merges", $tip, "--not", $base, @$parents, "--");
1532 for my $range ( @ranges ) {
1533 delete @commits{_rev_list($range, "--")};
1535 for my $commit (keys %commits) {
1536 if (has_no_changes($commit)) {
1537 delete $commits{$commit};
1540 return (keys %commits);
1543 sub has_no_changes {
1544 my $commit = shift;
1546 my @revs = split / /, command_oneline(
1547 qw(rev-list --parents -1 -m), $commit);
1549 # Commits with no parents, e.g. the start of a partial branch,
1550 # have changes by definition.
1551 return 1 if (@revs < 2);
1553 # Commits with multiple parents, e.g a merge, have no changes
1554 # by definition.
1555 return 0 if (@revs > 2);
1557 return (command_oneline("rev-parse", "$commit^{tree}") eq
1558 command_oneline("rev-parse", "$commit~1^{tree}"));
1561 sub tie_for_persistent_memoization {
1562 my $hash = shift;
1563 my $path = shift;
1565 if ($can_use_yaml) {
1566 tie %$hash => 'Git::SVN::Memoize::YAML', "$path.yaml";
1567 } else {
1568 tie %$hash => 'Memoize::Storable', "$path.db", 'nstore';
1572 # The GIT_DIR environment variable is not always set until after the command
1573 # line arguments are processed, so we can't memoize in a BEGIN block.
1575 my $memoized = 0;
1577 sub memoize_svn_mergeinfo_functions {
1578 return if $memoized;
1579 $memoized = 1;
1581 my $cache_path = "$ENV{GIT_DIR}/svn/.caches/";
1582 mkpath([$cache_path]) unless -d $cache_path;
1584 my %lookup_svn_merge_cache;
1585 my %check_cherry_pick_cache;
1586 my %has_no_changes_cache;
1588 tie_for_persistent_memoization(\%lookup_svn_merge_cache,
1589 "$cache_path/lookup_svn_merge");
1590 memoize 'lookup_svn_merge',
1591 SCALAR_CACHE => 'FAULT',
1592 LIST_CACHE => ['HASH' => \%lookup_svn_merge_cache],
1595 tie_for_persistent_memoization(\%check_cherry_pick_cache,
1596 "$cache_path/check_cherry_pick");
1597 memoize 'check_cherry_pick',
1598 SCALAR_CACHE => 'FAULT',
1599 LIST_CACHE => ['HASH' => \%check_cherry_pick_cache],
1602 tie_for_persistent_memoization(\%has_no_changes_cache,
1603 "$cache_path/has_no_changes");
1604 memoize 'has_no_changes',
1605 SCALAR_CACHE => ['HASH' => \%has_no_changes_cache],
1606 LIST_CACHE => 'FAULT',
1610 sub unmemoize_svn_mergeinfo_functions {
1611 return if not $memoized;
1612 $memoized = 0;
1614 Memoize::unmemoize 'lookup_svn_merge';
1615 Memoize::unmemoize 'check_cherry_pick';
1616 Memoize::unmemoize 'has_no_changes';
1619 Memoize::memoize 'Git::SVN::repos_root';
1622 END {
1623 # Force cache writeout explicitly instead of waiting for
1624 # global destruction to avoid segfault in Storable:
1625 # http://rt.cpan.org/Public/Bug/Display.html?id=36087
1626 unmemoize_svn_mergeinfo_functions();
1629 sub parents_exclude {
1630 my $parents = shift;
1631 my @commits = @_;
1632 return unless @commits;
1634 my @excluded;
1635 my $excluded;
1636 do {
1637 my @cmd = ('rev-list', "-1", @commits, "--not", @$parents );
1638 $excluded = command_oneline(@cmd);
1639 if ( $excluded ) {
1640 my @new;
1641 my $found;
1642 for my $commit ( @commits ) {
1643 if ( $commit eq $excluded ) {
1644 push @excluded, $commit;
1645 $found++;
1646 last;
1648 else {
1649 push @new, $commit;
1652 die "saw commit '$excluded' in rev-list output, "
1653 ."but we didn't ask for that commit (wanted: @commits --not @$parents)"
1654 unless $found;
1655 @commits = @new;
1658 while ($excluded and @commits);
1660 return @excluded;
1664 # note: this function should only be called if the various dirprops
1665 # have actually changed
1666 sub find_extra_svn_parents {
1667 my ($self, $ed, $mergeinfo, $parents) = @_;
1668 # aha! svk:merge property changed...
1670 memoize_svn_mergeinfo_functions();
1672 # We first search for merged tips which are not in our
1673 # history. Then, we figure out which git revisions are in
1674 # that tip, but not this revision. If all of those revisions
1675 # are now marked as merge, we can add the tip as a parent.
1676 my @merges = split "\n", $mergeinfo;
1677 my @merge_tips;
1678 my $url = $self->{url};
1679 my $uuid = $self->ra_uuid;
1680 my %ranges;
1681 for my $merge ( @merges ) {
1682 my ($tip_commit, @ranges) =
1683 lookup_svn_merge( $uuid, $url, $merge );
1684 unless (!$tip_commit or
1685 grep { $_ eq $tip_commit } @$parents ) {
1686 push @merge_tips, $tip_commit;
1687 $ranges{$tip_commit} = \@ranges;
1688 } else {
1689 push @merge_tips, undef;
1693 my %excluded = map { $_ => 1 }
1694 parents_exclude($parents, grep { defined } @merge_tips);
1696 # check merge tips for new parents
1697 my @new_parents;
1698 for my $merge_tip ( @merge_tips ) {
1699 my $spec = shift @merges;
1700 next unless $merge_tip and $excluded{$merge_tip};
1702 my $ranges = $ranges{$merge_tip};
1704 # check out 'new' tips
1705 my $merge_base;
1706 eval {
1707 $merge_base = command_oneline(
1708 "merge-base",
1709 @$parents, $merge_tip,
1712 if ($@) {
1713 die "An error occurred during merge-base"
1714 unless $@->isa("Git::Error::Command");
1716 warn "W: Cannot find common ancestor between ".
1717 "@$parents and $merge_tip. Ignoring merge info.\n";
1718 next;
1721 # double check that there are no missing non-merge commits
1722 my (@incomplete) = check_cherry_pick(
1723 $merge_base, $merge_tip,
1724 $parents,
1725 @$ranges,
1728 if ( @incomplete ) {
1729 warn "W:svn cherry-pick ignored ($spec) - missing "
1730 .@incomplete." commit(s) (eg $incomplete[0])\n";
1731 } else {
1732 warn
1733 "Found merge parent (svn:mergeinfo prop): ",
1734 $merge_tip, "\n";
1735 push @new_parents, $merge_tip;
1739 # cater for merges which merge commits from multiple branches
1740 if ( @new_parents > 1 ) {
1741 for ( my $i = 0; $i <= $#new_parents; $i++ ) {
1742 for ( my $j = 0; $j <= $#new_parents; $j++ ) {
1743 next if $i == $j;
1744 next unless $new_parents[$i];
1745 next unless $new_parents[$j];
1746 my $revs = command_oneline(
1747 "rev-list", "-1",
1748 "$new_parents[$i]..$new_parents[$j]",
1750 if ( !$revs ) {
1751 undef($new_parents[$j]);
1756 push @$parents, grep { defined } @new_parents;
1759 sub make_log_entry {
1760 my ($self, $rev, $parents, $ed) = @_;
1761 my $untracked = $self->get_untracked($ed);
1763 my @parents = @$parents;
1764 my $ps = $ed->{path_strip} || "";
1765 for my $path ( grep { m/$ps/ } %{$ed->{dir_prop}} ) {
1766 my $props = $ed->{dir_prop}{$path};
1767 if ( $props->{"svk:merge"} ) {
1768 $self->find_extra_svk_parents
1769 ($ed, $props->{"svk:merge"}, \@parents);
1771 if ( $props->{"svn:mergeinfo"} ) {
1772 $self->find_extra_svn_parents
1773 ($ed,
1774 $props->{"svn:mergeinfo"},
1775 \@parents);
1779 open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
1780 print $un "r$rev\n" or croak $!;
1781 print $un $_, "\n" foreach @$untracked;
1782 my %log_entry = ( parents => \@parents, revision => $rev,
1783 log => '');
1785 my $headrev;
1786 my $logged = delete $self->{logged_rev_props};
1787 if (!$logged || $self->{-want_revprops}) {
1788 my $rp = $self->ra->rev_proplist($rev);
1789 foreach (sort keys %$rp) {
1790 my $v = $rp->{$_};
1791 if (/^svn:(author|date|log)$/) {
1792 $log_entry{$1} = $v;
1793 } elsif ($_ eq 'svm:headrev') {
1794 $headrev = $v;
1795 } else {
1796 print $un " rev_prop: ", uri_encode($_), ' ',
1797 uri_encode($v), "\n";
1800 } else {
1801 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
1803 close $un or croak $!;
1805 $log_entry{date} = parse_svn_date($log_entry{date});
1806 $log_entry{log} .= "\n";
1807 my $author = $log_entry{author} = check_author($log_entry{author});
1808 my ($name, $email) = defined $::users{$author} ? @{$::users{$author}}
1809 : ($author, undef);
1811 my ($commit_name, $commit_email) = ($name, $email);
1812 if ($_use_log_author) {
1813 my $name_field;
1814 if ($log_entry{log} =~ /From:\s+(.*\S)\s*\n/i) {
1815 $name_field = $1;
1816 } elsif ($log_entry{log} =~ /Signed-off-by:\s+(.*\S)\s*\n/i) {
1817 $name_field = $1;
1819 if (!defined $name_field) {
1820 if (!defined $email) {
1821 $email = $name;
1823 } elsif ($name_field =~ /(.*?)\s+<(.*)>/) {
1824 ($name, $email) = ($1, $2);
1825 } elsif ($name_field =~ /(.*)@/) {
1826 ($name, $email) = ($1, $name_field);
1827 } else {
1828 ($name, $email) = ($name_field, $name_field);
1831 if (defined $headrev && $self->use_svm_props) {
1832 if ($self->rewrite_root) {
1833 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
1834 "options set!\n";
1836 if ($self->rewrite_uuid) {
1837 die "Can't have both 'useSvmProps' and 'rewriteUUID' ",
1838 "options set!\n";
1840 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d+)$}i;
1841 # we don't want "SVM: initializing mirror for junk" ...
1842 return undef if $r == 0;
1843 my $svm = $self->svm;
1844 if ($uuid ne $svm->{uuid}) {
1845 die "UUID mismatch on SVM path:\n",
1846 "expected: $svm->{uuid}\n",
1847 " got: $uuid\n";
1849 my $full_url = $self->full_url;
1850 $full_url =~ s#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
1851 die "Failed to replace '$svm->{replace}' with ",
1852 "'$svm->{source}' in $full_url\n";
1853 # throw away username for storing in records
1854 remove_username($full_url);
1855 $log_entry{metadata} = "$full_url\@$r $uuid";
1856 $log_entry{svm_revision} = $r;
1857 $email ||= "$author\@$uuid";
1858 $commit_email ||= "$author\@$uuid";
1859 } elsif ($self->use_svnsync_props) {
1860 my $full_url = $self->svnsync->{url};
1861 $full_url .= "/$self->{path}" if length $self->{path};
1862 remove_username($full_url);
1863 my $uuid = $self->svnsync->{uuid};
1864 $log_entry{metadata} = "$full_url\@$rev $uuid";
1865 $email ||= "$author\@$uuid";
1866 $commit_email ||= "$author\@$uuid";
1867 } else {
1868 my $url = $self->metadata_url;
1869 remove_username($url);
1870 my $uuid = $self->rewrite_uuid || $self->ra->get_uuid;
1871 $log_entry{metadata} = "$url\@$rev " . $uuid;
1872 $email ||= "$author\@" . $uuid;
1873 $commit_email ||= "$author\@" . $uuid;
1875 $log_entry{name} = $name;
1876 $log_entry{email} = $email;
1877 $log_entry{commit_name} = $commit_name;
1878 $log_entry{commit_email} = $commit_email;
1879 \%log_entry;
1882 sub fetch {
1883 my ($self, $min_rev, $max_rev, @parents) = @_;
1884 my ($last_rev, $last_commit) = $self->last_rev_commit;
1885 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
1886 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
1889 sub set_tree_cb {
1890 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
1891 $self->{inject_parents} = { $rev => $tree };
1892 $self->fetch(undef, undef);
1895 sub set_tree {
1896 my ($self, $tree) = (shift, shift);
1897 my $log_entry = ::get_commit_entry($tree);
1898 unless ($self->{last_rev}) {
1899 fatal("Must have an existing revision to commit");
1901 my %ed_opts = ( r => $self->{last_rev},
1902 log => $log_entry->{log},
1903 ra => $self->ra,
1904 tree_a => $self->{last_commit},
1905 tree_b => $tree,
1906 editor_cb => sub {
1907 $self->set_tree_cb($log_entry, $tree, @_) },
1908 svn_path => $self->{path} );
1909 if (!Git::SVN::Editor->new(\%ed_opts)->apply_diff) {
1910 print "No changes\nr$self->{last_rev} = $tree\n";
1914 sub rebuild_from_rev_db {
1915 my ($self, $path) = @_;
1916 my $r = -1;
1917 open my $fh, '<', $path or croak "open: $!";
1918 binmode $fh or croak "binmode: $!";
1919 while (<$fh>) {
1920 length($_) == 41 or croak "inconsistent size in ($_) != 41";
1921 chomp($_);
1922 ++$r;
1923 next if $_ eq ('0' x 40);
1924 $self->rev_map_set($r, $_);
1925 print "r$r = $_\n";
1927 close $fh or croak "close: $!";
1928 unlink $path or croak "unlink: $!";
1931 sub rebuild {
1932 my ($self) = @_;
1933 my $map_path = $self->map_path;
1934 my $partial = (-e $map_path && ! -z $map_path);
1935 return unless ::verify_ref($self->refname.'^0');
1936 if (!$partial && ($self->use_svm_props || $self->no_metadata)) {
1937 my $rev_db = $self->rev_db_path;
1938 $self->rebuild_from_rev_db($rev_db);
1939 if ($self->use_svm_props) {
1940 my $svm_rev_db = $self->rev_db_path($self->svm_uuid);
1941 $self->rebuild_from_rev_db($svm_rev_db);
1943 $self->unlink_rev_db_symlink;
1944 return;
1946 print "Rebuilding $map_path ...\n" if (!$partial);
1947 my ($base_rev, $head) = ($partial ? $self->rev_map_max_norebuild(1) :
1948 (undef, undef));
1949 my ($log, $ctx) =
1950 command_output_pipe(qw/rev-list --pretty=raw --reverse/,
1951 ($head ? "$head.." : "") . $self->refname,
1952 '--');
1953 my $metadata_url = $self->metadata_url;
1954 remove_username($metadata_url);
1955 my $svn_uuid = $self->rewrite_uuid || $self->ra_uuid;
1956 my $c;
1957 while (<$log>) {
1958 if ( m{^commit ($::sha1)$} ) {
1959 $c = $1;
1960 next;
1962 next unless s{^\s*(git-svn-id:)}{$1};
1963 my ($url, $rev, $uuid) = ::extract_metadata($_);
1964 remove_username($url);
1966 # ignore merges (from set-tree)
1967 next if (!defined $rev || !$uuid);
1969 # if we merged or otherwise started elsewhere, this is
1970 # how we break out of it
1971 if (($uuid ne $svn_uuid) ||
1972 ($metadata_url && $url && ($url ne $metadata_url))) {
1973 next;
1975 if ($partial && $head) {
1976 print "Partial-rebuilding $map_path ...\n";
1977 print "Currently at $base_rev = $head\n";
1978 $head = undef;
1981 $self->rev_map_set($rev, $c);
1982 print "r$rev = $c\n";
1984 command_close_pipe($log, $ctx);
1985 print "Done rebuilding $map_path\n" if (!$partial || !$head);
1986 my $rev_db_path = $self->rev_db_path;
1987 if (-f $self->rev_db_path) {
1988 unlink $self->rev_db_path or croak "unlink: $!";
1990 $self->unlink_rev_db_symlink;
1993 # rev_map:
1994 # Tie::File seems to be prone to offset errors if revisions get sparse,
1995 # it's not that fast, either. Tie::File is also not in Perl 5.6. So
1996 # one of my favorite modules is out :< Next up would be one of the DBM
1997 # modules, but I'm not sure which is most portable...
1999 # This is the replacement for the rev_db format, which was too big
2000 # and inefficient for large repositories with a lot of sparse history
2001 # (mainly tags)
2003 # The format is this:
2004 # - 24 bytes for every record,
2005 # * 4 bytes for the integer representing an SVN revision number
2006 # * 20 bytes representing the sha1 of a git commit
2007 # - No empty padding records like the old format
2008 # (except the last record, which can be overwritten)
2009 # - new records are written append-only since SVN revision numbers
2010 # increase monotonically
2011 # - lookups on SVN revision number are done via a binary search
2012 # - Piping the file to xxd -c24 is a good way of dumping it for
2013 # viewing or editing (piped back through xxd -r), should the need
2014 # ever arise.
2015 # - The last record can be padding revision with an all-zero sha1
2016 # This is used to optimize fetch performance when using multiple
2017 # "fetch" directives in .git/config
2019 # These files are disposable unless noMetadata or useSvmProps is set
2021 sub _rev_map_set {
2022 my ($fh, $rev, $commit) = @_;
2024 binmode $fh or croak "binmode: $!";
2025 my $size = (stat($fh))[7];
2026 ($size % 24) == 0 or croak "inconsistent size: $size";
2028 my $wr_offset = 0;
2029 if ($size > 0) {
2030 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
2031 my $read = sysread($fh, my $buf, 24) or croak "read: $!";
2032 $read == 24 or croak "read only $read bytes (!= 24)";
2033 my ($last_rev, $last_commit) = unpack(rev_map_fmt, $buf);
2034 if ($last_commit eq ('0' x40)) {
2035 if ($size >= 48) {
2036 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
2037 $read = sysread($fh, $buf, 24) or
2038 croak "read: $!";
2039 $read == 24 or
2040 croak "read only $read bytes (!= 24)";
2041 ($last_rev, $last_commit) =
2042 unpack(rev_map_fmt, $buf);
2043 if ($last_commit eq ('0' x40)) {
2044 croak "inconsistent .rev_map\n";
2047 if ($last_rev >= $rev) {
2048 croak "last_rev is higher!: $last_rev >= $rev";
2050 $wr_offset = -24;
2053 sysseek($fh, $wr_offset, SEEK_END) or croak "seek: $!";
2054 syswrite($fh, pack(rev_map_fmt, $rev, $commit), 24) == 24 or
2055 croak "write: $!";
2058 sub _rev_map_reset {
2059 my ($fh, $rev, $commit) = @_;
2060 my $c = _rev_map_get($fh, $rev);
2061 $c eq $commit or die "_rev_map_reset(@_) commit $c does not match!\n";
2062 my $offset = sysseek($fh, 0, SEEK_CUR) or croak "seek: $!";
2063 truncate $fh, $offset or croak "truncate: $!";
2066 sub mkfile {
2067 my ($path) = @_;
2068 unless (-e $path) {
2069 my ($dir, $base) = ($path =~ m#^(.*?)/?([^/]+)$#);
2070 mkpath([$dir]) unless -d $dir;
2071 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
2072 close $fh or die "Couldn't close (create) $path: $!\n";
2076 sub rev_map_set {
2077 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
2078 defined $commit or die "missing arg3\n";
2079 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
2080 my $db = $self->map_path($uuid);
2081 my $db_lock = "$db.lock";
2082 my $sigmask;
2083 $update_ref ||= 0;
2084 if ($update_ref) {
2085 $sigmask = POSIX::SigSet->new();
2086 my $signew = POSIX::SigSet->new(SIGINT, SIGHUP, SIGTERM,
2087 SIGALRM, SIGUSR1, SIGUSR2);
2088 sigprocmask(SIG_BLOCK, $signew, $sigmask) or
2089 croak "Can't block signals: $!";
2091 mkfile($db);
2093 $LOCKFILES{$db_lock} = 1;
2094 my $sync;
2095 # both of these options make our .rev_db file very, very important
2096 # and we can't afford to lose it because rebuild() won't work
2097 if ($self->use_svm_props || $self->no_metadata) {
2098 $sync = 1;
2099 copy($db, $db_lock) or die "rev_map_set(@_): ",
2100 "Failed to copy: ",
2101 "$db => $db_lock ($!)\n";
2102 } else {
2103 rename $db, $db_lock or die "rev_map_set(@_): ",
2104 "Failed to rename: ",
2105 "$db => $db_lock ($!)\n";
2108 sysopen(my $fh, $db_lock, O_RDWR | O_CREAT)
2109 or croak "Couldn't open $db_lock: $!\n";
2110 $update_ref eq 'reset' ? _rev_map_reset($fh, $rev, $commit) :
2111 _rev_map_set($fh, $rev, $commit);
2112 if ($sync) {
2113 $fh->flush or die "Couldn't flush $db_lock: $!\n";
2114 $fh->sync or die "Couldn't sync $db_lock: $!\n";
2116 close $fh or croak $!;
2117 if ($update_ref) {
2118 $_head = $self;
2119 my $note = "";
2120 $note = " ($update_ref)" if ($update_ref !~ /^\d*$/);
2121 command_noisy('update-ref', '-m', "r$rev$note",
2122 $self->refname, $commit);
2124 rename $db_lock, $db or die "rev_map_set(@_): ", "Failed to rename: ",
2125 "$db_lock => $db ($!)\n";
2126 delete $LOCKFILES{$db_lock};
2127 if ($update_ref) {
2128 sigprocmask(SIG_SETMASK, $sigmask) or
2129 croak "Can't restore signal mask: $!";
2133 # If want_commit, this will return an array of (rev, commit) where
2134 # commit _must_ be a valid commit in the archive.
2135 # Otherwise, it'll return the max revision (whether or not the
2136 # commit is valid or just a 0x40 placeholder).
2137 sub rev_map_max {
2138 my ($self, $want_commit) = @_;
2139 $self->rebuild;
2140 my ($r, $c) = $self->rev_map_max_norebuild($want_commit);
2141 $want_commit ? ($r, $c) : $r;
2144 sub rev_map_max_norebuild {
2145 my ($self, $want_commit) = @_;
2146 my $map_path = $self->map_path;
2147 stat $map_path or return $want_commit ? (0, undef) : 0;
2148 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
2149 binmode $fh or croak "binmode: $!";
2150 my $size = (stat($fh))[7];
2151 ($size % 24) == 0 or croak "inconsistent size: $size";
2153 if ($size == 0) {
2154 close $fh or croak "close: $!";
2155 return $want_commit ? (0, undef) : 0;
2158 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
2159 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
2160 my ($r, $c) = unpack(rev_map_fmt, $buf);
2161 if ($want_commit && $c eq ('0' x40)) {
2162 if ($size < 48) {
2163 return $want_commit ? (0, undef) : 0;
2165 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
2166 sysread($fh, $buf, 24) == 24 or croak "read: $!";
2167 ($r, $c) = unpack(rev_map_fmt, $buf);
2168 if ($c eq ('0'x40)) {
2169 croak "Penultimate record is all-zeroes in $map_path";
2172 close $fh or croak "close: $!";
2173 $want_commit ? ($r, $c) : $r;
2176 sub rev_map_get {
2177 my ($self, $rev, $uuid) = @_;
2178 my $map_path = $self->map_path($uuid);
2179 return undef unless -e $map_path;
2181 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
2182 my $c = _rev_map_get($fh, $rev);
2183 close($fh) or croak "close: $!";
2187 sub _rev_map_get {
2188 my ($fh, $rev) = @_;
2190 binmode $fh or croak "binmode: $!";
2191 my $size = (stat($fh))[7];
2192 ($size % 24) == 0 or croak "inconsistent size: $size";
2194 if ($size == 0) {
2195 return undef;
2198 my ($l, $u) = (0, $size - 24);
2199 my ($r, $c, $buf);
2201 while ($l <= $u) {
2202 my $i = int(($l/24 + $u/24) / 2) * 24;
2203 sysseek($fh, $i, SEEK_SET) or croak "seek: $!";
2204 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
2205 my ($r, $c) = unpack(rev_map_fmt, $buf);
2207 if ($r < $rev) {
2208 $l = $i + 24;
2209 } elsif ($r > $rev) {
2210 $u = $i - 24;
2211 } else { # $r == $rev
2212 return $c eq ('0' x 40) ? undef : $c;
2215 undef;
2218 # Finds the first svn revision that exists on (if $eq_ok is true) or
2219 # before $rev for the current branch. It will not search any lower
2220 # than $min_rev. Returns the git commit hash and svn revision number
2221 # if found, else (undef, undef).
2222 sub find_rev_before {
2223 my ($self, $rev, $eq_ok, $min_rev) = @_;
2224 --$rev unless $eq_ok;
2225 $min_rev ||= 1;
2226 my $max_rev = $self->rev_map_max;
2227 $rev = $max_rev if ($rev > $max_rev);
2228 while ($rev >= $min_rev) {
2229 if (my $c = $self->rev_map_get($rev)) {
2230 return ($rev, $c);
2232 --$rev;
2234 return (undef, undef);
2237 # Finds the first svn revision that exists on (if $eq_ok is true) or
2238 # after $rev for the current branch. It will not search any higher
2239 # than $max_rev. Returns the git commit hash and svn revision number
2240 # if found, else (undef, undef).
2241 sub find_rev_after {
2242 my ($self, $rev, $eq_ok, $max_rev) = @_;
2243 ++$rev unless $eq_ok;
2244 $max_rev ||= $self->rev_map_max;
2245 while ($rev <= $max_rev) {
2246 if (my $c = $self->rev_map_get($rev)) {
2247 return ($rev, $c);
2249 ++$rev;
2251 return (undef, undef);
2254 sub _new {
2255 my ($class, $repo_id, $ref_id, $path) = @_;
2256 unless (defined $repo_id && length $repo_id) {
2257 $repo_id = $default_repo_id;
2259 unless (defined $ref_id && length $ref_id) {
2260 # Access the prefix option from the git-svn main program if it's loaded.
2261 my $prefix = defined &::opt_prefix ? ::opt_prefix() : "";
2262 $_[2] = $ref_id =
2263 "refs/remotes/$prefix$default_ref_id";
2265 $_[1] = $repo_id;
2266 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
2268 # Older repos imported by us used $GIT_DIR/svn/foo instead of
2269 # $GIT_DIR/svn/refs/remotes/foo when tracking refs/remotes/foo
2270 if ($ref_id =~ m{^refs/remotes/(.*)}) {
2271 my $old_dir = "$ENV{GIT_DIR}/svn/$1";
2272 if (-d $old_dir && ! -d $dir) {
2273 $dir = $old_dir;
2277 $_[3] = $path = '' unless (defined $path);
2278 mkpath([$dir]);
2279 bless {
2280 ref_id => $ref_id, dir => $dir, index => "$dir/index",
2281 path => $path, config => "$ENV{GIT_DIR}/svn/config",
2282 map_root => "$dir/.rev_map", repo_id => $repo_id }, $class;
2285 # for read-only access of old .rev_db formats
2286 sub unlink_rev_db_symlink {
2287 my ($self) = @_;
2288 my $link = $self->rev_db_path;
2289 $link =~ s/\.[\w-]+$// or croak "missing UUID at the end of $link";
2290 if (-l $link) {
2291 unlink $link or croak "unlink: $link failed!";
2295 sub rev_db_path {
2296 my ($self, $uuid) = @_;
2297 my $db_path = $self->map_path($uuid);
2298 $db_path =~ s{/\.rev_map\.}{/\.rev_db\.}
2299 or croak "map_path: $db_path does not contain '/.rev_map.' !";
2300 $db_path;
2303 # the new replacement for .rev_db
2304 sub map_path {
2305 my ($self, $uuid) = @_;
2306 $uuid ||= $self->ra_uuid;
2307 "$self->{map_root}.$uuid";
2310 sub uri_encode {
2311 my ($f) = @_;
2312 $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
2316 sub uri_decode {
2317 my ($f) = @_;
2318 $f =~ s#%([0-9a-fA-F]{2})#chr(hex($1))#eg;
2322 sub remove_username {
2323 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};