]> git.ipfire.org Git - thirdparty/git.git/blame - contrib/git-svn/git-svn.perl
git-svn: add 'log' command, a facsimile of basic `svn log'
[thirdparty/git.git] / contrib / git-svn / git-svn.perl
CommitLineData
3397f9df 1#!/usr/bin/env perl
551ce28f
EW
2# Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3# License: GPL v2 or later
3397f9df
EW
4use warnings;
5use strict;
6use vars qw/ $AUTHOR $VERSION
1ca72aef 7 $SVN_URL $SVN_INFO $SVN_WC $SVN_UUID
3397f9df 8 $GIT_SVN_INDEX $GIT_SVN
883d0a78 9 $GIT_DIR $REV_DIR $GIT_SVN_DIR/;
3397f9df 10$AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
304dac15 11$VERSION = '1.1.0-pre';
13ccd6d4
EW
12
13use Cwd qw/abs_path/;
14$GIT_DIR = abs_path($ENV{GIT_DIR} || '.git');
15$ENV{GIT_DIR} = $GIT_DIR;
16
ce475dfc 17my $LC_ALL = $ENV{LC_ALL};
79bb8d88 18my $TZ = $ENV{TZ};
3397f9df
EW
19# make sure the svn binary gives consistent output between locales and TZs:
20$ENV{TZ} = 'UTC';
21$ENV{LC_ALL} = 'C';
22
23# If SVN:: library support is added, please make the dependencies
24# optional and preserve the capability to use the command-line client.
ce6f3519 25# use eval { require SVN::... } to make it lazy load
eeb0abe0 26# We don't use any modules not in the standard Perl distribution:
3397f9df
EW
27use Carp qw/croak/;
28use IO::File qw//;
29use File::Basename qw/dirname basename/;
30use File::Path qw/mkpath/;
79bb8d88 31use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev pass_through/;
3397f9df 32use File::Spec qw//;
e17512f3 33use POSIX qw/strftime/;
3397f9df 34my $sha1 = qr/[a-f\d]{40}/;
ac8e0b91 35my $sha1_short = qr/[a-f\d]{4,40}/;
72942938 36my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
162f4129 37 $_find_copies_harder, $_l, $_cp_similarity,
dc5869c0 38 $_repack, $_repack_nr, $_repack_flags,
9d55b41a 39 $_template, $_shared, $_no_default_regex, $_no_graft_copy,
79bb8d88 40 $_limit, $_verbose, $_incremental, $_oneline, $_l_fmt, $_show_commit,
9d55b41a 41 $_version, $_upgrade, $_authors, $_branch_all_refs, @_opt_m);
79bb8d88 42my (@_branch_from, %tree_map, %users, %rusers);
8a97e368 43my ($_svn_co_url_revs, $_svn_pg_peg_revs);
883d0a78 44my @repo_path_split_cache;
3397f9df 45
eeb0abe0 46my %fc_opts = ( 'no-ignore-externals' => \$_no_ignore_ext,
69f0d91e 47 'branch|b=s' => \@_branch_from,
bf78b1d8 48 'branch-all-refs|B' => \$_branch_all_refs,
dc5869c0
EW
49 'authors-file|A=s' => \$_authors,
50 'repack:i' => \$_repack,
51 'repack-flags|repack-args|repack-opts=s' => \$_repack_flags);
36f5b1f0 52
9d55b41a
EW
53my ($_trunk, $_tags, $_branches);
54my %multi_opts = ( 'trunk|T=s' => \$_trunk,
55 'tags|t=s' => \$_tags,
56 'branches|b=s' => \$_branches );
57my %init_opts = ( 'template=s' => \$_template, 'shared' => \$_shared );
58
36f5b1f0
EW
59# yes, 'native' sets "\n". Patches to fix this for non-*nix systems welcome:
60my %EOL = ( CR => "\015", LF => "\012", CRLF => "\015\012", native => "\012" );
61
3397f9df 62my %cmd = (
eeb0abe0
EW
63 fetch => [ \&fetch, "Download new revisions from SVN",
64 { 'revision|r=s' => \$_revision, %fc_opts } ],
81c5a0e6 65 init => [ \&init, "Initialize a repo for tracking" .
f8ab6b73 66 " (requires URL argument)",
9d55b41a 67 \%init_opts ],
eeb0abe0
EW
68 commit => [ \&commit, "Commit git revisions to SVN",
69 { 'stdin|' => \$_stdin,
70 'edit|e' => \$_edit,
71 'rmdir' => \$_rmdir,
72 'find-copies-harder' => \$_find_copies_harder,
73 'l=i' => \$_l,
162f4129 74 'copy-similarity|C=i'=> \$_cp_similarity,
eeb0abe0
EW
75 %fc_opts,
76 } ],
77 'show-ignore' => [ \&show_ignore, "Show svn:ignore listings", { } ],
78 rebuild => [ \&rebuild, "Rebuild git-svn metadata (after git clone)",
79 { 'no-ignore-externals' => \$_no_ignore_ext,
80 'upgrade' => \$_upgrade } ],
9d55b41a
EW
81 'graft-branches' => [ \&graft_branches,
82 'Detect merges/branches from already imported history',
83 { 'merge-rx|m' => \@_opt_m,
84 'no-default-regex' => \$_no_default_regex,
85 'no-graft-copy' => \$_no_graft_copy } ],
86 'multi-init' => [ \&multi_init,
87 'Initialize multiple trees (like git-svnimport)',
88 { %multi_opts, %fc_opts } ],
89 'multi-fetch' => [ \&multi_fetch,
90 'Fetch multiple trees (like git-svnimport)',
91 \%fc_opts ],
79bb8d88
EW
92 'log' => [ \&show_log, 'Show commit logs',
93 { 'limit=i' => \$_limit,
94 'revision|r=s' => \$_revision,
95 'verbose|v' => \$_verbose,
96 'incremental' => \$_incremental,
97 'oneline' => \$_oneline,
98 'show-commit' => \$_show_commit,
99 'authors-file|A=s' => \$_authors,
100 } ],
3397f9df 101);
9d55b41a 102
3397f9df
EW
103my $cmd;
104for (my $i = 0; $i < @ARGV; $i++) {
105 if (defined $cmd{$ARGV[$i]}) {
106 $cmd = $ARGV[$i];
107 splice @ARGV, $i, 1;
108 last;
109 }
110};
111
448c81b4 112my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
a9612be2 113
b8c92cad 114read_repo_config(\%opts);
79bb8d88
EW
115my $rv = GetOptions(%opts, 'help|H|h' => \$_help,
116 'version|V' => \$_version,
117 'id|i=s' => \$GIT_SVN);
118exit 1 if (!$rv && $cmd ne 'log');
6f0783cf 119
dc5869c0 120set_default_vals();
3397f9df 121usage(0) if $_help;
551ce28f 122version() if $_version;
eeb0abe0 123usage(1) unless defined $cmd;
b8c92cad 124init_vars();
eeb0abe0 125load_authors() if $_authors;
bf78b1d8 126load_all_refs() if $_branch_all_refs;
1d52aba8 127svn_compat_check();
9d55b41a 128migration_check() unless $cmd =~ /^(?:init|multi-init)$/;
3397f9df
EW
129$cmd{$cmd}->[0]->(@ARGV);
130exit 0;
131
132####################### primary functions ######################
133sub usage {
134 my $exit = shift || 0;
135 my $fd = $exit ? \*STDERR : \*STDOUT;
136 print $fd <<"";
137git-svn - bidirectional operations between a single Subversion tree and git
138Usage: $0 <command> [options] [arguments]\n
448c81b4
EW
139
140 print $fd "Available commands:\n" unless $cmd;
3397f9df
EW
141
142 foreach (sort keys %cmd) {
448c81b4 143 next if $cmd && $cmd ne $_;
ac8e0b91 144 print $fd ' ',pack('A13',$_),$cmd{$_}->[1],"\n";
448c81b4
EW
145 foreach (keys %{$cmd{$_}->[2]}) {
146 # prints out arguments as they should be passed:
b8c92cad 147 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
448c81b4
EW
148 print $fd ' ' x 17, join(', ', map { length $_ > 1 ?
149 "--$_" : "-$_" }
150 split /\|/,$_)," $x\n";
151 }
3397f9df
EW
152 }
153 print $fd <<"";
448c81b4
EW
154\nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
155arbitrary identifier if you're tracking multiple SVN branches/repositories in
156one git repository and want to keep them separate. See git-svn(1) for more
157information.
3397f9df
EW
158
159 exit $exit;
160}
161
551ce28f
EW
162sub version {
163 print "git-svn version $VERSION\n";
164 exit 0;
165}
166
3397f9df
EW
167sub rebuild {
168 $SVN_URL = shift or undef;
3397f9df 169 my $newest_rev = 0;
2beb3cdd
EW
170 if ($_upgrade) {
171 sys('git-update-ref',"refs/remotes/$GIT_SVN","$GIT_SVN-HEAD");
172 } else {
173 check_upgrade_needed();
174 }
3397f9df
EW
175
176 my $pid = open(my $rev_list,'-|');
177 defined $pid or croak $!;
178 if ($pid == 0) {
2beb3cdd 179 exec("git-rev-list","refs/remotes/$GIT_SVN") or croak $!;
3397f9df 180 }
2beb3cdd 181 my $latest;
3397f9df
EW
182 while (<$rev_list>) {
183 chomp;
184 my $c = $_;
185 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
186 my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
187 next if (!@commit); # skip merges
79bb8d88
EW
188 my ($url, $rev, $uuid) = extract_metadata($commit[$#commit]);
189 if (!$rev || !$uuid) {
190 croak "Unable to extract revision or UUID from ",
191 "$c, $commit[$#commit]\n";
3397f9df 192 }
2beb3cdd
EW
193
194 # if we merged or otherwise started elsewhere, this is
195 # how we break out of it
1ca72aef 196 next if (defined $SVN_UUID && ($uuid ne $SVN_UUID));
779b1446 197 next if (defined $SVN_URL && defined $url && ($url ne $SVN_URL));
2beb3cdd 198
3397f9df 199 print "r$rev = $c\n";
2beb3cdd 200 unless (defined $latest) {
3397f9df
EW
201 if (!$SVN_URL && !$url) {
202 croak "SVN repository location required: $url\n";
203 }
204 $SVN_URL ||= $url;
1d52aba8
EW
205 $SVN_UUID ||= $uuid;
206 setup_git_svn();
2beb3cdd 207 $latest = $rev;
3397f9df
EW
208 }
209 assert_revision_eq_or_unknown($rev, $c);
883d0a78 210 sys('git-update-ref',"svn/$GIT_SVN/revs/$rev",$c);
3397f9df
EW
211 $newest_rev = $rev if ($rev > $newest_rev);
212 }
213 close $rev_list or croak $?;
214 if (!chdir $SVN_WC) {
1d52aba8 215 svn_cmd_checkout($SVN_URL, $latest, $SVN_WC);
3397f9df
EW
216 chdir $SVN_WC or croak $!;
217 }
218
219 $pid = fork;
220 defined $pid or croak $!;
221 if ($pid == 0) {
222 my @svn_up = qw(svn up);
223 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
224 sys(@svn_up,"-r$newest_rev");
225 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
36f5b1f0 226 index_changes();
b8c92cad 227 exec('git-write-tree') or croak $!;
3397f9df
EW
228 }
229 waitpid $pid, 0;
b8c92cad 230 croak $? if $?;
2beb3cdd
EW
231
232 if ($_upgrade) {
233 print STDERR <<"";
234Keeping deprecated refs/head/$GIT_SVN-HEAD for now. Please remove it
235when you have upgraded your tools and habits to use refs/remotes/$GIT_SVN
236
237 }
3397f9df
EW
238}
239
240sub init {
81c5a0e6
EW
241 $SVN_URL = shift or die "SVN repository location required " .
242 "as a command-line argument\n";
9d55b41a 243 $SVN_URL =~ s!/+$!!; # strip trailing slash
3397f9df 244 unless (-d $GIT_DIR) {
f8ab6b73
EW
245 my @init_db = ('git-init-db');
246 push @init_db, "--template=$_template" if defined $_template;
247 push @init_db, "--shared" if defined $_shared;
248 sys(@init_db);
3397f9df
EW
249 }
250 setup_git_svn();
251}
252
253sub fetch {
254 my (@parents) = @_;
2beb3cdd 255 check_upgrade_needed();
883d0a78 256 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
3397f9df 257 my @log_args = -d $SVN_WC ? ($SVN_WC) : ($SVN_URL);
defc6492
EW
258 unless ($_revision) {
259 $_revision = -d $SVN_WC ? 'BASE:HEAD' : '0:HEAD';
3397f9df 260 }
defc6492 261 push @log_args, "-r$_revision";
3397f9df
EW
262 push @log_args, '--stop-on-copy' unless $_no_stop_copy;
263
ce6f3519 264 my $svn_log = svn_log_raw(@log_args);
3397f9df 265
03823184 266 my $base = next_log_entry($svn_log) or croak "No base revision!\n";
3397f9df
EW
267 my $last_commit = undef;
268 unless (-d $SVN_WC) {
1d52aba8 269 svn_cmd_checkout($SVN_URL,$base->{revision},$SVN_WC);
3397f9df 270 chdir $SVN_WC or croak $!;
1d52aba8 271 read_uuid();
3397f9df 272 $last_commit = git_commit($base, @parents);
36f5b1f0 273 assert_tree($last_commit);
3397f9df
EW
274 } else {
275 chdir $SVN_WC or croak $!;
1d52aba8 276 read_uuid();
6dfbe516
EW
277 eval { $last_commit = file_to_s("$REV_DIR/$base->{revision}") };
278 # looks like a user manually cp'd and svn switch'ed
279 unless ($last_commit) {
280 sys(qw/svn revert -R ./);
281 assert_svn_wc_clean($base->{revision});
282 $last_commit = git_commit($base, @parents);
283 assert_tree($last_commit);
284 }
3397f9df
EW
285 }
286 my @svn_up = qw(svn up);
287 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
03823184
EW
288 my $last = $base;
289 while (my $log_msg = next_log_entry($svn_log)) {
36f5b1f0 290 assert_tree($last_commit);
03823184
EW
291 if ($last->{revision} >= $log_msg->{revision}) {
292 croak "Out of order: last >= current: ",
293 "$last->{revision} >= $log_msg->{revision}\n";
294 }
36f5b1f0
EW
295 # Revert is needed for cases like:
296 # https://svn.musicpd.org/Jamming/trunk (r166:167), but
297 # I can't seem to reproduce something like that on a test...
298 sys(qw/svn revert -R ./);
299 assert_svn_wc_clean($last->{revision});
03823184 300 sys(@svn_up,"-r$log_msg->{revision}");
3397f9df 301 $last_commit = git_commit($log_msg, $last_commit, @parents);
03823184 302 $last = $log_msg;
3397f9df 303 }
7f60b228
EW
304 unless (-e "$GIT_DIR/refs/heads/master") {
305 sys(qw(git-update-ref refs/heads/master),$last_commit);
306 }
b8c92cad 307 close $svn_log->{fh};
03823184 308 return $last;
3397f9df
EW
309}
310
311sub commit {
312 my (@commits) = @_;
2beb3cdd 313 check_upgrade_needed();
3397f9df
EW
314 if ($_stdin || !@commits) {
315 print "Reading from stdin...\n";
316 @commits = ();
317 while (<STDIN>) {
1ca72aef 318 if (/\b($sha1_short)\b/o) {
3397f9df
EW
319 unshift @commits, $1;
320 }
321 }
322 }
323 my @revs;
8de010ad
EW
324 foreach my $c (@commits) {
325 chomp(my @tmp = safe_qx('git-rev-parse',$c));
326 if (scalar @tmp == 1) {
327 push @revs, $tmp[0];
328 } elsif (scalar @tmp > 1) {
329 push @revs, reverse (safe_qx('git-rev-list',@tmp));
330 } else {
331 die "Failed to rev-parse $c\n";
332 }
3397f9df
EW
333 }
334 chomp @revs;
335
b63af9b3 336 chdir $SVN_WC or croak "Unable to chdir $SVN_WC: $!\n";
1d52aba8 337 my $info = svn_info('.');
b63af9b3
EW
338 my $fetched = fetch();
339 if ($info->{Revision} != $fetched->{revision}) {
340 print STDERR "There are new revisions that were fetched ",
341 "and need to be merged (or acknowledged) ",
342 "before committing.\n";
343 exit 1;
344 }
345 $info = svn_info('.');
1d52aba8
EW
346 read_uuid($info);
347 my $svn_current_rev = $info->{'Last Changed Rev'};
3397f9df 348 foreach my $c (@revs) {
cf52b8f0
EW
349 my $mods = svn_checkout_tree($svn_current_rev, $c);
350 if (scalar @$mods == 0) {
351 print "Skipping, no changes detected\n";
352 next;
353 }
3397f9df
EW
354 $svn_current_rev = svn_commit_tree($svn_current_rev, $c);
355 }
356 print "Done committing ",scalar @revs," revisions to SVN\n";
3397f9df
EW
357}
358
8f22562c
EW
359sub show_ignore {
360 require File::Find or die $!;
361 my $exclude_file = "$GIT_DIR/info/exclude";
362 open my $fh, '<', $exclude_file or croak $!;
363 chomp(my @excludes = (<$fh>));
364 close $fh or croak $!;
365
883d0a78 366 $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url");
8f22562c
EW
367 chdir $SVN_WC or croak $!;
368 my %ign;
369 File::Find::find({wanted=>sub{if(lstat $_ && -d _ && -d "$_/.svn"){
370 s#^\./##;
8a97e368 371 @{$ign{$_}} = svn_propget_base('svn:ignore', $_);
8f22562c
EW
372 }}, no_chdir=>1},'.');
373
374 print "\n# /\n";
375 foreach (@{$ign{'.'}}) { print '/',$_ if /\S/ }
376 delete $ign{'.'};
377 foreach my $i (sort keys %ign) {
378 print "\n# ",$i,"\n";
379 foreach (@{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ }
380 }
381}
382
9d55b41a
EW
383sub graft_branches {
384 my $gr_file = "$GIT_DIR/info/grafts";
385 my ($grafts, $comments) = read_grafts($gr_file);
386 my $gr_sha1;
387
388 if (%$grafts) {
389 # temporarily disable our grafts file to make this idempotent
390 chomp($gr_sha1 = safe_qx(qw/git-hash-object -w/,$gr_file));
391 rename $gr_file, "$gr_file~$gr_sha1" or croak $!;
392 }
393
394 my $l_map = read_url_paths();
395 my @re = map { qr/$_/is } @_opt_m if @_opt_m;
396 unless ($_no_default_regex) {
397 push @re, ( qr/\b(?:merge|merging|merged)\s+(\S.+)/is,
398 qr/\b(?:from|of)\s+(\S.+)/is );
399 }
400 foreach my $u (keys %$l_map) {
401 if (@re) {
402 foreach my $p (keys %{$l_map->{$u}}) {
403 graft_merge_msg($grafts,$l_map,$u,$p);
404 }
405 }
406 graft_file_copy($grafts,$l_map,$u) unless $_no_graft_copy;
407 }
408
409 write_grafts($grafts, $comments, $gr_file);
410 unlink "$gr_file~$gr_sha1" if $gr_sha1;
411}
412
413sub multi_init {
414 my $url = shift;
415 $_trunk ||= 'trunk';
416 $_trunk =~ s#/+$##;
417 $url =~ s#/+$## if $url;
418 if ($_trunk !~ m#^[a-z\+]+://#) {
419 $_trunk = '/' . $_trunk if ($_trunk !~ m#^/#);
420 unless ($url) {
421 print STDERR "E: '$_trunk' is not a complete URL ",
422 "and a separate URL is not specified\n";
423 exit 1;
424 }
425 $_trunk = $url . $_trunk;
426 }
427 if ($GIT_SVN eq 'git-svn') {
428 print "GIT_SVN_ID set to 'trunk' for $_trunk\n";
429 $GIT_SVN = $ENV{GIT_SVN_ID} = 'trunk';
430 }
431 init_vars();
432 init($_trunk);
433 complete_url_ls_init($url, $_branches, '--branches/-b', '');
434 complete_url_ls_init($url, $_tags, '--tags/-t', 'tags/');
435}
436
437sub multi_fetch {
438 # try to do trunk first, since branches/tags
439 # may be descended from it.
440 if (-d "$GIT_DIR/svn/trunk") {
441 print "Fetching trunk\n";
442 defined(my $pid = fork) or croak $!;
443 if (!$pid) {
444 $GIT_SVN = $ENV{GIT_SVN_ID} = 'trunk';
445 init_vars();
446 fetch(@_);
447 exit 0;
448 }
449 waitpid $pid, 0;
450 croak $? if $?;
451 }
452 rec_fetch('', "$GIT_DIR/svn", @_);
453}
454
79bb8d88
EW
455sub show_log {
456 my (@args) = @_;
457 my ($r_min, $r_max);
458 my $r_last = -1; # prevent dupes
459 rload_authors() if $_authors;
460 if (defined $TZ) {
461 $ENV{TZ} = $TZ;
462 } else {
463 delete $ENV{TZ};
464 }
465 if (defined $_revision) {
466 if ($_revision =~ /^(\d+):(\d+)$/) {
467 ($r_min, $r_max) = ($1, $2);
468 } elsif ($_revision =~ /^\d+$/) {
469 $r_min = $r_max = $_revision;
470 } else {
471 print STDERR "-r$_revision is not supported, use ",
472 "standard \'git log\' arguments instead\n";
473 exit 1;
474 }
475 }
476
477 my $pid = open(my $log,'-|');
478 defined $pid or croak $!;
479 if (!$pid) {
480 my @rl = (qw/git-log --abbrev-commit --pretty=raw
481 --default/, "remotes/$GIT_SVN");
482 push @rl, '--raw' if $_verbose;
483 exec(@rl, @args) or croak $!;
484 }
485 setup_pager();
486 my (@k, $c, $d);
487 while (<$log>) {
488 if (/^commit ($sha1_short)/o) {
489 my $cmt = $1;
490 if ($c && defined $c->{r} && $c->{r} != $r_last) {
491 $r_last = $c->{r};
492 process_commit($c, $r_min, $r_max, \@k) or
493 goto out;
494 }
495 $d = undef;
496 $c = { c => $cmt };
497 } elsif (/^author (.+) (\d+) ([\-\+]?\d+)$/) {
498 get_author_info($c, $1, $2, $3);
499 } elsif (/^(?:tree|parent|committer) /) {
500 # ignore
501 } elsif (/^:\d{6} \d{6} $sha1_short/o) {
502 push @{$c->{raw}}, $_;
503 } elsif (/^diff /) {
504 $d = 1;
505 push @{$c->{diff}}, $_;
506 } elsif ($d) {
507 push @{$c->{diff}}, $_;
508 } elsif (/^ (git-svn-id:.+)$/) {
509 my ($url, $rev, $uuid) = extract_metadata($1);
510 $c->{r} = $rev;
511 } elsif (s/^ //) {
512 push @{$c->{l}}, $_;
513 }
514 }
515 if ($c && defined $c->{r} && $c->{r} != $r_last) {
516 $r_last = $c->{r};
517 process_commit($c, $r_min, $r_max, \@k);
518 }
519 if (@k) {
520 my $swap = $r_max;
521 $r_max = $r_min;
522 $r_min = $swap;
523 process_commit($_, $r_min, $r_max) foreach reverse @k;
524 }
525out:
526 close $log;
527 print '-' x72,"\n" unless $_incremental || $_oneline;
528}
529
3397f9df
EW
530########################### utility functions #########################
531
9d55b41a
EW
532sub rec_fetch {
533 my ($pfx, $p, @args) = @_;
534 my @dir;
535 foreach (sort <$p/*>) {
536 if (-r "$_/info/url") {
537 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
538 my $id = $pfx . basename $_;
539 next if $id eq 'trunk';
540 print "Fetching $id\n";
541 defined(my $pid = fork) or croak $!;
542 if (!$pid) {
543 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
544 init_vars();
545 fetch(@args);
546 exit 0;
547 }
548 waitpid $pid, 0;
549 croak $? if $?;
550 } elsif (-d $_) {
551 push @dir, $_;
552 }
553 }
554 foreach (@dir) {
555 my $x = $_;
556 $x =~ s!^\Q$GIT_DIR\E/svn/!!;
557 rec_fetch($x, $_);
558 }
559}
560
561sub complete_url_ls_init {
562 my ($url, $var, $switch, $pfx) = @_;
563 unless ($var) {
564 print STDERR "W: $switch not specified\n";
565 return;
566 }
567 $var =~ s#/+$##;
568 if ($var !~ m#^[a-z\+]+://#) {
569 $var = '/' . $var if ($var !~ m#^/#);
570 unless ($url) {
571 print STDERR "E: '$var' is not a complete URL ",
572 "and a separate URL is not specified\n";
573 exit 1;
574 }
575 $var = $url . $var;
576 }
577 chomp(my @ls = safe_qx(qw/svn ls --non-interactive/, $var));
578 my $old = $GIT_SVN;
579 defined(my $pid = fork) or croak $!;
580 if (!$pid) {
581 foreach my $u (map { "$var/$_" } (grep m!/$!, @ls)) {
582 $u =~ s#/+$##;
583 if ($u !~ m!\Q$var\E/(.+)$!) {
584 print STDERR "W: Unrecognized URL: $u\n";
585 die "This should never happen\n";
586 }
587 my $id = $pfx.$1;
588 print "init $u => $id\n";
589 $GIT_SVN = $ENV{GIT_SVN_ID} = $id;
590 init_vars();
591 init($u);
592 }
593 exit 0;
594 }
595 waitpid $pid, 0;
596 croak $? if $?;
597}
598
599sub common_prefix {
600 my $paths = shift;
601 my %common;
602 foreach (@$paths) {
603 my @tmp = split m#/#, $_;
604 my $p = '';
605 while (my $x = shift @tmp) {
606 $p .= "/$x";
607 $common{$p} ||= 0;
608 $common{$p}++;
609 }
610 }
611 foreach (sort {length $b <=> length $a} keys %common) {
612 if ($common{$_} == @$paths) {
613 return $_;
614 }
615 }
616 return '';
617}
618
619# this isn't funky-filename safe, but good enough for now...
620sub graft_file_copy {
621 my ($grafts, $l_map, $u) = @_;
622 my $paths = $l_map->{$u};
623 my $pfx = common_prefix([keys %$paths]);
624
625 my $pid = open my $fh, '-|';
626 defined $pid or croak $!;
627 unless ($pid) {
628 exec(qw/svn log -v/, $u.$pfx) or croak $!;
629 }
630 my ($r, $mp) = (undef, undef);
631 while (<$fh>) {
632 chomp;
633 if (/^\-{72}$/) {
634 $mp = $r = undef;
635 } elsif (/^r(\d+) \| /) {
636 $r = $1 unless defined $r;
637 } elsif (/^Changed paths:/) {
638 $mp = 1;
639 } elsif ($mp && m#^ [AR] /(\S.*?) \(from /(\S+?):(\d+)\)$#) {
640 my $dbg = "r$r | $_";
641 my ($p1, $p0, $r0) = ($1, $2, $3);
642 my $c;
643 foreach my $x (keys %$paths) {
644 next unless ($p1 =~ /^\Q$x\E/);
645 my $i = $paths->{$x};
646 my $f = "$GIT_DIR/svn/$i/revs/$r";
647 unless (-r $f) {
648 print STDERR "r$r of $i not imported,",
649 " $dbg\n";
650 next;
651 }
652 $c = file_to_s($f);
653 }
654 next unless $c;
655 foreach my $x (keys %$paths) {
656 next unless ($p0 =~ /^\Q$x\E/);
657 my $i = $paths->{$x};
658 my $f = "$GIT_DIR/svn/$i/revs/$r0";
659 while ($r0 && !-r $f) {
660 # could be an older revision, too...
661 $r0--;
662 $f = "$GIT_DIR/svn/$i/revs/$r0";
663 }
664 unless (-r $f) {
665 print STDERR "r$r0 of $i not imported,",
666 " $dbg\n";
667 next;
668 }
669 my $r1 = file_to_s($f);
670 $grafts->{$c}->{$r1} = 1;
671 }
672 }
673 }
674}
675
676sub process_merge_msg_matches {
677 my ($grafts, $l_map, $u, $p, $c, @matches) = @_;
678 my (@strong, @weak);
679 foreach (@matches) {
680 # merging with ourselves is not interesting
681 next if $_ eq $p;
682 if ($l_map->{$u}->{$_}) {
683 push @strong, $_;
684 } else {
685 push @weak, $_;
686 }
687 }
688 foreach my $w (@weak) {
689 last if @strong;
690 # no exact match, use branch name as regexp.
691 my $re = qr/\Q$w\E/i;
692 foreach (keys %{$l_map->{$u}}) {
693 if (/$re/) {
694 push @strong, $_;
695 last;
696 }
697 }
698 last if @strong;
699 $w = basename($w);
700 $re = qr/\Q$w\E/i;
701 foreach (keys %{$l_map->{$u}}) {
702 if (/$re/) {
703 push @strong, $_;
704 last;
705 }
706 }
707 }
708 my ($rev) = ($c->{m} =~ /^git-svn-id:\s(?:\S+?)\@(\d+)
709 \s(?:[a-f\d\-]+)$/xsm);
710 unless (defined $rev) {
711 ($rev) = ($c->{m} =~/^git-svn-id:\s(\d+)
712 \@(?:[a-f\d\-]+)/xsm);
713 return unless defined $rev;
714 }
715 foreach my $m (@strong) {
716 my ($r0, $s0) = find_rev_before($rev, $m);
717 $grafts->{$c->{c}}->{$s0} = 1 if defined $s0;
718 }
719}
720
721sub graft_merge_msg {
722 my ($grafts, $l_map, $u, $p, @re) = @_;
723
724 my $x = $l_map->{$u}->{$p};
725 my $rl = rev_list_raw($x);
726 while (my $c = next_rev_list_entry($rl)) {
727 foreach my $re (@re) {
728 my (@br) = ($c->{m} =~ /$re/g);
729 next unless @br;
730 process_merge_msg_matches($grafts,$l_map,$u,$p,$c,@br);
731 }
732 }
733}
734
1d52aba8
EW
735sub read_uuid {
736 return if $SVN_UUID;
737 my $info = shift || svn_info('.');
738 $SVN_UUID = $info->{'Repository UUID'} or
739 croak "Repository UUID unreadable\n";
883d0a78
EW
740 s_to_file($SVN_UUID,"$GIT_SVN_DIR/info/uuid");
741}
742
743sub quiet_run {
744 my $pid = fork;
745 defined $pid or croak $!;
746 if (!$pid) {
747 open my $null, '>', '/dev/null' or croak $!;
748 open STDERR, '>&', $null or croak $!;
749 open STDOUT, '>&', $null or croak $!;
750 exec @_ or croak $!;
751 }
752 waitpid $pid, 0;
753 return $?;
754}
755
756sub repo_path_split {
757 my $full_url = shift;
758 $full_url =~ s#/+$##;
759
760 foreach (@repo_path_split_cache) {
761 if ($full_url =~ s#$_##) {
762 my $u = $1;
763 $full_url =~ s#^/+##;
764 return ($u, $full_url);
765 }
766 }
767
768 my ($url, $path) = ($full_url =~ m!^([a-z\+]+://[^/]*)(.*)$!i);
769 $path =~ s#^/+##;
770 my @paths = split(m#/+#, $path);
771
772 while (quiet_run(qw/svn ls --non-interactive/, $url)) {
773 my $n = shift @paths || last;
774 $url .= "/$n";
775 }
776 push @repo_path_split_cache, qr/^(\Q$url\E)/;
9d55b41a 777 $path = join('/',@paths);
883d0a78 778 return ($url, $path);
1d52aba8
EW
779}
780
3397f9df
EW
781sub setup_git_svn {
782 defined $SVN_URL or croak "SVN repository location required\n";
783 unless (-d $GIT_DIR) {
784 croak "GIT_DIR=$GIT_DIR does not exist!\n";
785 }
883d0a78
EW
786 mkpath([$GIT_SVN_DIR]);
787 mkpath(["$GIT_SVN_DIR/info"]);
3397f9df 788 mkpath([$REV_DIR]);
883d0a78 789 s_to_file($SVN_URL,"$GIT_SVN_DIR/info/url");
3397f9df 790
883d0a78 791 open my $fd, '>>', "$GIT_SVN_DIR/info/exclude" or croak $!;
3397f9df
EW
792 print $fd '.svn',"\n";
793 close $fd or croak $!;
883d0a78
EW
794 my ($url, $path) = repo_path_split($SVN_URL);
795 s_to_file($url, "$GIT_SVN_DIR/info/repo_url");
796 s_to_file($path, "$GIT_SVN_DIR/info/repo_path");
3397f9df
EW
797}
798
799sub assert_svn_wc_clean {
36f5b1f0 800 my ($svn_rev) = @_;
3397f9df 801 croak "$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
ce4c8b24
EW
802 my $lcr = svn_info('.')->{'Last Changed Rev'};
803 if ($svn_rev != $lcr) {
804 print STDERR "Checking for copy-tree ... ";
ce4c8b24
EW
805 my @diff = grep(/^Index: /,(safe_qx(qw(svn diff),
806 "-r$lcr:$svn_rev")));
807 if (@diff) {
808 croak "Nope! Expected r$svn_rev, got r$lcr\n";
809 } else {
810 print STDERR "OK!\n";
811 }
3397f9df
EW
812 }
813 my @status = grep(!/^Performing status on external/,(`svn status`));
814 @status = grep(!/^\s*$/,@status);
815 if (scalar @status) {
816 print STDERR "Tree ($SVN_WC) is not clean:\n";
817 print STDERR $_ foreach @status;
818 croak;
819 }
cf52b8f0
EW
820}
821
822sub assert_tree {
823 my ($treeish) = @_;
824 croak "Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
825 chomp(my $type = `git-cat-file -t $treeish`);
826 my $expected;
827 while ($type eq 'tag') {
828 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
829 }
830 if ($type eq 'commit') {
831 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
832 ($expected) = ($expected =~ /^tree ($sha1)$/);
833 die "Unable to get tree from $treeish\n" unless $expected;
834 } elsif ($type eq 'tree') {
835 $expected = $treeish;
836 } else {
837 die "$treeish is a $type, expected tree, tag or commit\n";
838 }
839
840 my $old_index = $ENV{GIT_INDEX_FILE};
841 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
842 if (-e $tmpindex) {
843 unlink $tmpindex or croak $!;
844 }
845 $ENV{GIT_INDEX_FILE} = $tmpindex;
36f5b1f0 846 index_changes(1);
cf52b8f0
EW
847 chomp(my $tree = `git-write-tree`);
848 if ($old_index) {
849 $ENV{GIT_INDEX_FILE} = $old_index;
850 } else {
851 delete $ENV{GIT_INDEX_FILE};
852 }
853 if ($tree ne $expected) {
854 croak "Tree mismatch, Got: $tree, Expected: $expected\n";
3397f9df 855 }
36f5b1f0 856 unlink $tmpindex;
3397f9df
EW
857}
858
859sub parse_diff_tree {
860 my $diff_fh = shift;
861 local $/ = "\0";
862 my $state = 'meta';
863 my @mods;
864 while (<$diff_fh>) {
865 chomp $_; # this gets rid of the trailing "\0"
3397f9df
EW
866 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
867 $sha1\s($sha1)\s([MTCRAD])\d*$/xo) {
868 push @mods, { mode_a => $1, mode_b => $2,
869 sha1_b => $3, chg => $4 };
870 if ($4 =~ /^(?:C|R)$/) {
871 $state = 'file_a';
872 } else {
873 $state = 'file_b';
874 }
875 } elsif ($state eq 'file_a') {
cf52b8f0 876 my $x = $mods[$#mods] or croak "Empty array\n";
3397f9df 877 if ($x->{chg} !~ /^(?:C|R)$/) {
cf52b8f0 878 croak "Error parsing $_, $x->{chg}\n";
3397f9df
EW
879 }
880 $x->{file_a} = $_;
881 $state = 'file_b';
882 } elsif ($state eq 'file_b') {
cf52b8f0 883 my $x = $mods[$#mods] or croak "Empty array\n";
3397f9df 884 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
cf52b8f0 885 croak "Error parsing $_, $x->{chg}\n";
3397f9df
EW
886 }
887 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
cf52b8f0 888 croak "Error parsing $_, $x->{chg}\n";
3397f9df
EW
889 }
890 $x->{file_b} = $_;
891 $state = 'meta';
892 } else {
cf52b8f0 893 croak "Error parsing $_\n";
3397f9df
EW
894 }
895 }
896 close $diff_fh or croak $!;
cf52b8f0 897
3397f9df
EW
898 return \@mods;
899}
900
901sub svn_check_prop_executable {
902 my $m = shift;
cf52b8f0
EW
903 return if -l $m->{file_b};
904 if ($m->{mode_b} =~ /755$/) {
905 chmod((0755 &~ umask),$m->{file_b}) or croak $!;
906 if ($m->{mode_a} !~ /755$/) {
907 sys(qw(svn propset svn:executable 1), $m->{file_b});
908 }
909 -x $m->{file_b} or croak "$m->{file_b} is not executable!\n";
3397f9df
EW
910 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
911 sys(qw(svn propdel svn:executable), $m->{file_b});
cf52b8f0
EW
912 chmod((0644 &~ umask),$m->{file_b}) or croak $!;
913 -x $m->{file_b} and croak "$m->{file_b} is executable!\n";
3397f9df
EW
914 }
915}
916
917sub svn_ensure_parent_path {
918 my $dir_b = dirname(shift);
919 svn_ensure_parent_path($dir_b) if ($dir_b ne File::Spec->curdir);
920 mkpath([$dir_b]) unless (-d $dir_b);
921 sys(qw(svn add -N), $dir_b) unless (-d "$dir_b/.svn");
922}
923
cf52b8f0
EW
924sub precommit_check {
925 my $mods = shift;
926 my (%rm_file, %rmdir_check, %added_check);
927
928 my %o = ( D => 0, R => 1, C => 2, A => 3, M => 3, T => 3 );
929 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
930 if ($m->{chg} eq 'R') {
931 if (-d $m->{file_b}) {
932 err_dir_to_file("$m->{file_a} => $m->{file_b}");
933 }
934 # dir/$file => dir/file/$file
935 my $dirname = dirname($m->{file_b});
936 while ($dirname ne File::Spec->curdir) {
937 if ($dirname ne $m->{file_a}) {
938 $dirname = dirname($dirname);
939 next;
940 }
941 err_file_to_dir("$m->{file_a} => $m->{file_b}");
942 }
943 # baz/zzz => baz (baz is a file)
944 $dirname = dirname($m->{file_a});
945 while ($dirname ne File::Spec->curdir) {
946 if ($dirname ne $m->{file_b}) {
947 $dirname = dirname($dirname);
948 next;
949 }
950 err_dir_to_file("$m->{file_a} => $m->{file_b}");
951 }
952 }
953 if ($m->{chg} =~ /^(D|R)$/) {
954 my $t = $1 eq 'D' ? 'file_b' : 'file_a';
955 $rm_file{ $m->{$t} } = 1;
956 my $dirname = dirname( $m->{$t} );
957 my $basename = basename( $m->{$t} );
958 $rmdir_check{$dirname}->{$basename} = 1;
959 } elsif ($m->{chg} =~ /^(?:A|C)$/) {
960 if (-d $m->{file_b}) {
961 err_dir_to_file($m->{file_b});
962 }
963 my $dirname = dirname( $m->{file_b} );
964 my $basename = basename( $m->{file_b} );
965 $added_check{$dirname}->{$basename} = 1;
966 while ($dirname ne File::Spec->curdir) {
967 if ($rm_file{$dirname}) {
968 err_file_to_dir($m->{file_b});
969 }
970 $dirname = dirname $dirname;
971 }
972 }
973 }
974 return (\%rmdir_check, \%added_check);
975
976 sub err_dir_to_file {
977 my $file = shift;
978 print STDERR "Node change from directory to file ",
979 "is not supported by Subversion: ",$file,"\n";
980 exit 1;
981 }
982 sub err_file_to_dir {
983 my $file = shift;
984 print STDERR "Node change from file to directory ",
985 "is not supported by Subversion: ",$file,"\n";
986 exit 1;
987 }
988}
989
3397f9df 990sub svn_checkout_tree {
cf52b8f0 991 my ($svn_rev, $treeish) = @_;
3397f9df 992 my $from = file_to_s("$REV_DIR/$svn_rev");
36f5b1f0 993 assert_tree($from);
ac8e0b91 994 print "diff-tree $from $treeish\n";
3397f9df
EW
995 my $pid = open my $diff_fh, '-|';
996 defined $pid or croak $!;
997 if ($pid == 0) {
162f4129
EW
998 my @diff_tree = qw(git-diff-tree -z -r);
999 if ($_cp_similarity) {
1000 push @diff_tree, "-C$_cp_similarity";
1001 } else {
1002 push @diff_tree, '-C';
1003 }
72942938
EW
1004 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
1005 push @diff_tree, "-l$_l" if defined $_l;
cf52b8f0 1006 exec(@diff_tree, $from, $treeish) or croak $!;
3397f9df
EW
1007 }
1008 my $mods = parse_diff_tree($diff_fh);
1009 unless (@$mods) {
ac8e0b91 1010 # git can do empty commits, but SVN doesn't allow it...
cf52b8f0 1011 return $mods;
3397f9df 1012 }
cf52b8f0
EW
1013 my ($rm, $add) = precommit_check($mods);
1014
1015 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
1016 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
3397f9df
EW
1017 if ($m->{chg} eq 'C') {
1018 svn_ensure_parent_path( $m->{file_b} );
1019 sys(qw(svn cp), $m->{file_a}, $m->{file_b});
cf52b8f0 1020 apply_mod_line_blob($m);
3397f9df
EW
1021 svn_check_prop_executable($m);
1022 } elsif ($m->{chg} eq 'D') {
3397f9df
EW
1023 sys(qw(svn rm --force), $m->{file_b});
1024 } elsif ($m->{chg} eq 'R') {
1025 svn_ensure_parent_path( $m->{file_b} );
1026 sys(qw(svn mv --force), $m->{file_a}, $m->{file_b});
cf52b8f0 1027 apply_mod_line_blob($m);
3397f9df 1028 svn_check_prop_executable($m);
3397f9df 1029 } elsif ($m->{chg} eq 'M') {
cf52b8f0 1030 apply_mod_line_blob($m);
3397f9df
EW
1031 svn_check_prop_executable($m);
1032 } elsif ($m->{chg} eq 'T') {
1033 sys(qw(svn rm --force),$m->{file_b});
cf52b8f0 1034 apply_mod_line_blob($m);
3397f9df 1035 sys(qw(svn add --force), $m->{file_b});
cf52b8f0 1036 svn_check_prop_executable($m);
3397f9df
EW
1037 } elsif ($m->{chg} eq 'A') {
1038 svn_ensure_parent_path( $m->{file_b} );
cf52b8f0 1039 apply_mod_line_blob($m);
3397f9df 1040 sys(qw(svn add --force), $m->{file_b});
cf52b8f0 1041 svn_check_prop_executable($m);
3397f9df
EW
1042 } else {
1043 croak "Invalid chg: $m->{chg}\n";
1044 }
1045 }
cf52b8f0
EW
1046
1047 assert_tree($treeish);
1048 if ($_rmdir) { # remove empty directories
1049 handle_rmdir($rm, $add);
1050 }
1051 assert_tree($treeish);
1052 return $mods;
1053}
1054
1055# svn ls doesn't work with respect to the current working tree, but what's
1056# in the repository. There's not even an option for it... *sigh*
1057# (added files don't show up and removed files remain in the ls listing)
1058sub svn_ls_current {
1059 my ($dir, $rm, $add) = @_;
1060 chomp(my @ls = safe_qx('svn','ls',$dir));
1061 my @ret = ();
1062 foreach (@ls) {
1063 s#/$##; # trailing slashes are evil
1064 push @ret, $_ unless $rm->{$dir}->{$_};
1065 }
1066 if (exists $add->{$dir}) {
1067 push @ret, keys %{$add->{$dir}};
1068 }
1069 return \@ret;
1070}
1071
1072sub handle_rmdir {
1073 my ($rm, $add) = @_;
1074
1075 foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
1076 my $ls = svn_ls_current($dir, $rm, $add);
1077 next if (scalar @$ls);
1078 sys(qw(svn rm --force),$dir);
1079
1080 my $dn = dirname $dir;
1081 $rm->{ $dn }->{ basename $dir } = 1;
1082 $ls = svn_ls_current($dn, $rm, $add);
1083 while (scalar @$ls == 0 && $dn ne File::Spec->curdir) {
1084 sys(qw(svn rm --force),$dn);
1085 $dir = basename $dn;
1086 $dn = dirname $dn;
1087 $rm->{ $dn }->{ $dir } = 1;
1088 $ls = svn_ls_current($dn, $rm, $add);
3397f9df
EW
1089 }
1090 }
1091}
1092
1093sub svn_commit_tree {
1094 my ($svn_rev, $commit) = @_;
883d0a78 1095 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
e17512f3 1096 my %log_msg = ( msg => '' );
ac8e0b91 1097 open my $msg, '>', $commit_msg or croak $!;
3397f9df
EW
1098
1099 chomp(my $type = `git-cat-file -t $commit`);
1100 if ($type eq 'commit') {
1101 my $pid = open my $msg_fh, '-|';
1102 defined $pid or croak $!;
1103
1104 if ($pid == 0) {
1105 exec(qw(git-cat-file commit), $commit) or croak $!;
1106 }
1107 my $in_msg = 0;
1108 while (<$msg_fh>) {
1109 if (!$in_msg) {
1110 $in_msg = 1 if (/^\s*$/);
df746c5a
EW
1111 } elsif (/^git-svn-id: /) {
1112 # skip this, we regenerate the correct one
1113 # on re-fetch anyways
3397f9df
EW
1114 } else {
1115 print $msg $_ or croak $!;
1116 }
1117 }
1118 close $msg_fh or croak $!;
1119 }
1120 close $msg or croak $!;
1121
1122 if ($_edit || ($type eq 'tree')) {
1123 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
1124 system($editor, $commit_msg);
1125 }
ac8e0b91
EW
1126
1127 # file_to_s removes all trailing newlines, so just use chomp() here:
1128 open $msg, '<', $commit_msg or croak $!;
1129 { local $/; chomp($log_msg{msg} = <$msg>); }
1130 close $msg or croak $!;
1131
1132 my ($oneline) = ($log_msg{msg} =~ /([^\n\r]+)/);
1133 print "Committing $commit: $oneline\n";
1134
ce475dfc
EW
1135 if (defined $LC_ALL) {
1136 $ENV{LC_ALL} = $LC_ALL;
1137 } else {
1138 delete $ENV{LC_ALL};
1139 }
3397f9df 1140 my @ci_output = safe_qx(qw(svn commit -F),$commit_msg);
ce475dfc 1141 $ENV{LC_ALL} = 'C';
3397f9df 1142 unlink $commit_msg;
ce475dfc
EW
1143 my ($committed) = ($ci_output[$#ci_output] =~ /(\d+)/);
1144 if (!defined $committed) {
1145 my $out = join("\n",@ci_output);
1146 print STDERR "W: Trouble parsing \`svn commit' output:\n\n",
1147 $out, "\n\nAssuming English locale...";
1148 ($committed) = ($out =~ /^Committed revision \d+\./sm);
1149 defined $committed or die " FAILED!\n",
3397f9df 1150 "Commit output failed to parse committed revision!\n",
ce475dfc
EW
1151 print STDERR " OK\n";
1152 }
3397f9df 1153
e17512f3 1154 my @svn_up = qw(svn up);
3397f9df 1155 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
ce475dfc
EW
1156 if ($committed == ($svn_rev + 1)) {
1157 push @svn_up, "-r$committed";
e17512f3
EW
1158 sys(@svn_up);
1159 my $info = svn_info('.');
1160 my $date = $info->{'Last Changed Date'} or die "Missing date\n";
ce475dfc
EW
1161 if ($info->{'Last Changed Rev'} != $committed) {
1162 croak "$info->{'Last Changed Rev'} != $committed\n"
e17512f3
EW
1163 }
1164 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
1165 /(\d{4})\-(\d\d)\-(\d\d)\s
1166 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
1167 or croak "Failed to parse date: $date\n";
1168 $log_msg{date} = "$tz $Y-$m-$d $H:$M:$S";
1169 $log_msg{author} = $info->{'Last Changed Author'};
ce475dfc 1170 $log_msg{revision} = $committed;
e17512f3
EW
1171 $log_msg{msg} .= "\n";
1172 my $parent = file_to_s("$REV_DIR/$svn_rev");
1173 git_commit(\%log_msg, $parent, $commit);
ce475dfc 1174 return $committed;
e17512f3
EW
1175 }
1176 # resync immediately
1177 push @svn_up, "-r$svn_rev";
3397f9df 1178 sys(@svn_up);
ce475dfc 1179 return fetch("$committed=$commit")->{revision};
3397f9df
EW
1180}
1181
9d55b41a
EW
1182sub rev_list_raw {
1183 my (@args) = @_;
1184 my $pid = open my $fh, '-|';
1185 defined $pid or croak $!;
1186 if (!$pid) {
1187 exec(qw/git-rev-list --pretty=raw/, @args) or croak $!;
1188 }
1189 return { fh => $fh, t => { } };
1190}
1191
1192sub next_rev_list_entry {
1193 my $rl = shift;
1194 my $fh = $rl->{fh};
1195 my $x = $rl->{t};
1196 while (<$fh>) {
1197 if (/^commit ($sha1)$/o) {
1198 if ($x->{c}) {
1199 $rl->{t} = { c => $1 };
1200 return $x;
1201 } else {
1202 $x->{c} = $1;
1203 }
1204 } elsif (/^parent ($sha1)$/o) {
1205 $x->{p}->{$1} = 1;
1206 } elsif (s/^ //) {
1207 $x->{m} ||= '';
1208 $x->{m} .= $_;
1209 }
1210 }
1211 return ($x != $rl->{t}) ? $x : undef;
1212}
1213
03823184
EW
1214# read the entire log into a temporary file (which is removed ASAP)
1215# and store the file handle + parser state
3397f9df
EW
1216sub svn_log_raw {
1217 my (@log_args) = @_;
03823184
EW
1218 my $log_fh = IO::File->new_tmpfile or croak $!;
1219 my $pid = fork;
3397f9df 1220 defined $pid or croak $!;
03823184
EW
1221 if (!$pid) {
1222 open STDOUT, '>&', $log_fh or croak $!;
3397f9df
EW
1223 exec (qw(svn log), @log_args) or croak $!
1224 }
03823184 1225 waitpid $pid, 0;
b8c92cad 1226 croak $? if $?;
03823184
EW
1227 seek $log_fh, 0, 0 or croak $!;
1228 return { state => 'sep', fh => $log_fh };
1229}
1230
1231sub next_log_entry {
1232 my $log = shift; # retval of svn_log_raw()
1233 my $ret = undef;
1234 my $fh = $log->{fh};
3397f9df 1235
03823184 1236 while (<$fh>) {
3397f9df
EW
1237 chomp;
1238 if (/^\-{72}$/) {
03823184
EW
1239 if ($log->{state} eq 'msg') {
1240 if ($ret->{lines}) {
1241 $ret->{msg} .= $_."\n";
1242 unless(--$ret->{lines}) {
1243 $log->{state} = 'sep';
ce6f3519
EW
1244 }
1245 } else {
1246 croak "Log parse error at: $_\n",
03823184 1247 $ret->{revision},
ce6f3519
EW
1248 "\n";
1249 }
1250 next;
1251 }
03823184 1252 if ($log->{state} ne 'sep') {
ce6f3519 1253 croak "Log parse error at: $_\n",
03823184
EW
1254 "state: $log->{state}\n",
1255 $ret->{revision},
ce6f3519
EW
1256 "\n";
1257 }
03823184 1258 $log->{state} = 'rev';
3397f9df
EW
1259
1260 # if we have an empty log message, put something there:
03823184
EW
1261 if ($ret) {
1262 $ret->{msg} ||= "\n";
1263 delete $ret->{lines};
1264 return $ret;
3397f9df
EW
1265 }
1266 next;
1267 }
03823184 1268 if ($log->{state} eq 'rev' && s/^r(\d+)\s*\|\s*//) {
3397f9df 1269 my $rev = $1;
ce6f3519
EW
1270 my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
1271 ($lines) = ($lines =~ /(\d+)/);
3397f9df
EW
1272 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
1273 /(\d{4})\-(\d\d)\-(\d\d)\s
1274 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
1275 or croak "Failed to parse date: $date\n";
03823184 1276 $ret = { revision => $rev,
3397f9df
EW
1277 date => "$tz $Y-$m-$d $H:$M:$S",
1278 author => $author,
ce6f3519 1279 lines => $lines,
03823184 1280 msg => '' };
a9612be2
EW
1281 if (defined $_authors && ! defined $users{$author}) {
1282 die "Author: $author not defined in ",
1283 "$_authors file\n";
1284 }
03823184 1285 $log->{state} = 'msg_start';
3397f9df
EW
1286 next;
1287 }
1288 # skip the first blank line of the message:
03823184
EW
1289 if ($log->{state} eq 'msg_start' && /^$/) {
1290 $log->{state} = 'msg';
1291 } elsif ($log->{state} eq 'msg') {
1292 if ($ret->{lines}) {
1293 $ret->{msg} .= $_."\n";
1294 unless (--$ret->{lines}) {
1295 $log->{state} = 'sep';
ce6f3519
EW
1296 }
1297 } else {
1298 croak "Log parse error at: $_\n",
03823184 1299 $ret->{revision},"\n";
ce6f3519 1300 }
3397f9df
EW
1301 }
1302 }
03823184 1303 return $ret;
3397f9df
EW
1304}
1305
1306sub svn_info {
1307 my $url = shift || $SVN_URL;
1308
1309 my $pid = open my $info_fh, '-|';
1310 defined $pid or croak $!;
1311
1312 if ($pid == 0) {
1313 exec(qw(svn info),$url) or croak $!;
1314 }
1315
1316 my $ret = {};
1317 # only single-lines seem to exist in svn info output
1318 while (<$info_fh>) {
1319 chomp $_;
e17512f3 1320 if (m#^([^:]+)\s*:\s*(\S.*)$#) {
3397f9df
EW
1321 $ret->{$1} = $2;
1322 push @{$ret->{-order}}, $1;
1323 }
1324 }
1325 close $info_fh or croak $!;
1326 return $ret;
1327}
1328
1329sub sys { system(@_) == 0 or croak $? }
1330
36f5b1f0
EW
1331sub eol_cp {
1332 my ($from, $to) = @_;
8a97e368 1333 my $es = svn_propget_base('svn:eol-style', $to);
36f5b1f0
EW
1334 open my $rfd, '<', $from or croak $!;
1335 binmode $rfd or croak $!;
1336 open my $wfd, '>', $to or croak $!;
1337 binmode $wfd or croak $!;
1338
1339 my $eol = $EOL{$es} or undef;
36f5b1f0 1340 my $buf;
4a393f2b 1341 use bytes;
36f5b1f0
EW
1342 while (1) {
1343 my ($r, $w, $t);
1344 defined($r = sysread($rfd, $buf, 4096)) or croak $!;
1345 return unless $r;
4a393f2b
EW
1346 if ($eol) {
1347 if ($buf =~ /\015$/) {
1348 my $c;
1349 defined($r = sysread($rfd,$c,1)) or croak $!;
1350 $buf .= $c if $r > 0;
1351 }
1352 $buf =~ s/(?:\015\012|\015|\012)/$eol/gs;
1353 $r = length($buf);
1354 }
36f5b1f0
EW
1355 for ($w = 0; $w < $r; $w += $t) {
1356 $t = syswrite($wfd, $buf, $r - $w, $w) or croak $!;
1357 }
1358 }
4a393f2b 1359 no bytes;
36f5b1f0
EW
1360}
1361
1362sub do_update_index {
1363 my ($z_cmd, $cmd, $no_text_base) = @_;
1364
1365 my $z = open my $p, '-|';
1366 defined $z or croak $!;
1367 unless ($z) { exec @$z_cmd or croak $! }
1368
1369 my $pid = open my $ui, '|-';
1370 defined $pid or croak $!;
1371 unless ($pid) {
1372 exec('git-update-index',"--$cmd",'-z','--stdin') or croak $!;
1373 }
1374 local $/ = "\0";
1375 while (my $x = <$p>) {
1376 chomp $x;
1377 if (!$no_text_base && lstat $x && ! -l _ &&
8a97e368 1378 svn_propget_base('svn:keywords', $x)) {
36f5b1f0
EW
1379 my $mode = -x _ ? 0755 : 0644;
1380 my ($v,$d,$f) = File::Spec->splitpath($x);
1381 my $tb = File::Spec->catfile($d, '.svn', 'tmp',
1382 'text-base',"$f.svn-base");
1383 $tb =~ s#^/##;
1384 unless (-f $tb) {
1385 $tb = File::Spec->catfile($d, '.svn',
1386 'text-base',"$f.svn-base");
1387 $tb =~ s#^/##;
1388 }
1389 unlink $x or croak $!;
1390 eol_cp($tb, $x);
1391 chmod(($mode &~ umask), $x) or croak $!;
1392 }
1393 print $ui $x,"\0";
1394 }
1395 close $ui or croak $!;
1396}
1397
1398sub index_changes {
1399 my $no_text_base = shift;
1400 do_update_index([qw/git-diff-files --name-only -z/],
1401 'remove',
1402 $no_text_base);
1403 do_update_index([qw/git-ls-files -z --others/,
883d0a78 1404 "--exclude-from=$GIT_SVN_DIR/info/exclude"],
36f5b1f0
EW
1405 'add',
1406 $no_text_base);
3397f9df
EW
1407}
1408
1409sub s_to_file {
1410 my ($str, $file, $mode) = @_;
1411 open my $fd,'>',$file or croak $!;
1412 print $fd $str,"\n" or croak $!;
1413 close $fd or croak $!;
1414 chmod ($mode &~ umask, $file) if (defined $mode);
1415}
1416
1417sub file_to_s {
1418 my $file = shift;
1419 open my $fd,'<',$file or croak "$!: file: $file\n";
1420 local $/;
1421 my $ret = <$fd>;
1422 close $fd or croak $!;
1423 $ret =~ s/\s*$//s;
1424 return $ret;
1425}
1426
1427sub assert_revision_unknown {
1428 my $revno = shift;
1429 if (-f "$REV_DIR/$revno") {
1430 croak "$REV_DIR/$revno already exists! ",
1431 "Why are we refetching it?";
1432 }
1433}
1434
ac749050
EW
1435sub trees_eq {
1436 my ($x, $y) = @_;
1437 my @x = safe_qx('git-cat-file','commit',$x);
1438 my @y = safe_qx('git-cat-file','commit',$y);
1439 if (($y[0] ne $x[0]) || $x[0] !~ /^tree $sha1\n$/
1440 || $y[0] !~ /^tree $sha1\n$/) {
1441 print STDERR "Trees not equal: $y[0] != $x[0]\n";
1442 return 0
1443 }
1444 return 1;
1445}
1446
3397f9df
EW
1447sub assert_revision_eq_or_unknown {
1448 my ($revno, $commit) = @_;
1449 if (-f "$REV_DIR/$revno") {
1450 my $current = file_to_s("$REV_DIR/$revno");
ac749050 1451 if (($commit ne $current) && !trees_eq($commit, $current)) {
3397f9df
EW
1452 croak "$REV_DIR/$revno already exists!\n",
1453 "current: $current\nexpected: $commit\n";
1454 }
1455 return;
1456 }
1457}
1458
1459sub git_commit {
1460 my ($log_msg, @parents) = @_;
1461 assert_revision_unknown($log_msg->{revision});
1462 my $out_fh = IO::File->new_tmpfile or croak $!;
3397f9df 1463
69f0d91e
EW
1464 map_tree_joins() if (@_branch_from && !%tree_map);
1465
3397f9df
EW
1466 # commit parents can be conditionally bound to a particular
1467 # svn revision via: "svn_revno=commit_sha1", filter them out here:
1468 my @exec_parents;
1469 foreach my $p (@parents) {
1470 next unless defined $p;
1471 if ($p =~ /^(\d+)=($sha1_short)$/o) {
1472 if ($1 == $log_msg->{revision}) {
1473 push @exec_parents, $2;
1474 }
1475 } else {
1476 push @exec_parents, $p if $p =~ /$sha1_short/o;
1477 }
1478 }
1479
1480 my $pid = fork;
1481 defined $pid or croak $!;
1482 if ($pid == 0) {
1483 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
36f5b1f0 1484 index_changes();
3397f9df 1485 chomp(my $tree = `git-write-tree`);
b8c92cad 1486 croak $? if $?;
69f0d91e
EW
1487 if (exists $tree_map{$tree}) {
1488 my %seen_parent = map { $_ => 1 } @exec_parents;
1489 foreach (@{$tree_map{$tree}}) {
1490 # MAXPARENT is defined to 16 in commit-tree.c:
1491 if ($seen_parent{$_} || @exec_parents > 16) {
1492 next;
1493 }
1494 push @exec_parents, $_;
1495 $seen_parent{$_} = 1;
1496 }
1497 }
3397f9df
EW
1498 my $msg_fh = IO::File->new_tmpfile or croak $!;
1499 print $msg_fh $log_msg->{msg}, "\ngit-svn-id: ",
1500 "$SVN_URL\@$log_msg->{revision}",
1ca72aef 1501 " $SVN_UUID\n" or croak $!;
3397f9df
EW
1502 $msg_fh->flush == 0 or croak $!;
1503 seek $msg_fh, 0, 0 or croak $!;
1504
1ca72aef 1505 set_commit_env($log_msg);
a9612be2 1506
3397f9df
EW
1507 my @exec = ('git-commit-tree',$tree);
1508 push @exec, '-p', $_ foreach @exec_parents;
1509 open STDIN, '<&', $msg_fh or croak $!;
1510 open STDOUT, '>&', $out_fh or croak $!;
1511 exec @exec or croak $!;
1512 }
1513 waitpid($pid,0);
b8c92cad 1514 croak $? if $?;
3397f9df
EW
1515
1516 $out_fh->flush == 0 or croak $!;
1517 seek $out_fh, 0, 0 or croak $!;
1518 chomp(my $commit = do { local $/; <$out_fh> });
1519 if ($commit !~ /^$sha1$/o) {
1520 croak "Failed to commit, invalid sha1: $commit\n";
1521 }
2beb3cdd 1522 my @update_ref = ('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
3397f9df 1523 if (my $primary_parent = shift @exec_parents) {
1d52aba8
EW
1524 $pid = fork;
1525 defined $pid or croak $!;
1526 if (!$pid) {
1527 close STDERR;
1528 close STDOUT;
1529 exec 'git-rev-parse','--verify',
b8c92cad 1530 "refs/remotes/$GIT_SVN^0" or croak $!;
1d52aba8
EW
1531 }
1532 waitpid $pid, 0;
1533 push @update_ref, $primary_parent unless $?;
3397f9df
EW
1534 }
1535 sys(@update_ref);
883d0a78 1536 sys('git-update-ref',"svn/$GIT_SVN/revs/$log_msg->{revision}",$commit);
3397f9df 1537 print "r$log_msg->{revision} = $commit\n";
dc5869c0
EW
1538 if ($_repack && (--$_repack_nr == 0)) {
1539 $_repack_nr = $_repack;
1540 sys("git repack $_repack_flags");
1541 }
3397f9df
EW
1542 return $commit;
1543}
1544
a9612be2 1545sub set_commit_env {
1ca72aef 1546 my ($log_msg) = @_;
a9612be2
EW
1547 my $author = $log_msg->{author};
1548 my ($name,$email) = defined $users{$author} ? @{$users{$author}}
1ca72aef 1549 : ($author,"$author\@$SVN_UUID");
a9612be2
EW
1550 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $name;
1551 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} = $email;
1552 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_msg->{date};
1553}
1554
cf52b8f0
EW
1555sub apply_mod_line_blob {
1556 my $m = shift;
1557 if ($m->{mode_b} =~ /^120/) {
1558 blob_to_symlink($m->{sha1_b}, $m->{file_b});
1559 } else {
1560 blob_to_file($m->{sha1_b}, $m->{file_b});
1561 }
1562}
1563
3397f9df
EW
1564sub blob_to_symlink {
1565 my ($blob, $link) = @_;
1566 defined $link or croak "\$link not defined!\n";
1567 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
cf52b8f0
EW
1568 if (-l $link || -f _) {
1569 unlink $link or croak $!;
1570 }
1571
3397f9df
EW
1572 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
1573 symlink $dest, $link or croak $!;
1574}
1575
1576sub blob_to_file {
1577 my ($blob, $file) = @_;
1578 defined $file or croak "\$file not defined!\n";
1579 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
cf52b8f0
EW
1580 if (-l $file || -f _) {
1581 unlink $file or croak $!;
1582 }
1583
3397f9df
EW
1584 open my $blob_fh, '>', $file or croak "$!: $file\n";
1585 my $pid = fork;
1586 defined $pid or croak $!;
1587
1588 if ($pid == 0) {
1589 open STDOUT, '>&', $blob_fh or croak $!;
b8c92cad 1590 exec('git-cat-file','blob',$blob) or croak $!;
3397f9df
EW
1591 }
1592 waitpid $pid, 0;
1593 croak $? if $?;
1594
1595 close $blob_fh or croak $!;
1596}
1597
1598sub safe_qx {
1599 my $pid = open my $child, '-|';
1600 defined $pid or croak $!;
1601 if ($pid == 0) {
b8c92cad 1602 exec(@_) or croak $!;
3397f9df
EW
1603 }
1604 my @ret = (<$child>);
1605 close $child or croak $?;
1606 die $? if $?; # just in case close didn't error out
1607 return wantarray ? @ret : join('',@ret);
1608}
1609
1d52aba8
EW
1610sub svn_compat_check {
1611 my @co_help = safe_qx(qw(svn co -h));
1612 unless (grep /ignore-externals/,@co_help) {
3397f9df
EW
1613 print STDERR "W: Installed svn version does not support ",
1614 "--ignore-externals\n";
1615 $_no_ignore_ext = 1;
1616 }
1d52aba8
EW
1617 if (grep /usage: checkout URL\[\@REV\]/,@co_help) {
1618 $_svn_co_url_revs = 1;
1619 }
8a97e368
EW
1620 if (grep /\[TARGET\[\@REV\]\.\.\.\]/, `svn propget -h`) {
1621 $_svn_pg_peg_revs = 1;
1622 }
7317ed90
EW
1623
1624 # I really, really hope nobody hits this...
1625 unless (grep /stop-on-copy/, (safe_qx(qw(svn log -h)))) {
1626 print STDERR <<'';
1627W: The installed svn version does not support the --stop-on-copy flag in
1628 the log command.
1629 Lets hope the directory you're tracking is not a branch or tag
1630 and was never moved within the repository...
1631
1632 $_no_stop_copy = 1;
1633 }
1d52aba8
EW
1634}
1635
1636# *sigh*, new versions of svn won't honor -r<rev> without URL@<rev>,
1637# (and they won't honor URL@<rev> without -r<rev>, too!)
1638sub svn_cmd_checkout {
1639 my ($url, $rev, $dir) = @_;
1640 my @cmd = ('svn','co', "-r$rev");
1641 push @cmd, '--ignore-externals' unless $_no_ignore_ext;
1642 $url .= "\@$rev" if $_svn_co_url_revs;
1643 sys(@cmd, $url, $dir);
3397f9df 1644}
2beb3cdd
EW
1645
1646sub check_upgrade_needed {
1647 my $old = eval {
1648 my $pid = open my $child, '-|';
1649 defined $pid or croak $!;
1650 if ($pid == 0) {
1651 close STDERR;
b8c92cad 1652 exec('git-rev-parse',"$GIT_SVN-HEAD") or croak $!;
2beb3cdd
EW
1653 }
1654 my @ret = (<$child>);
1655 close $child or croak $?;
1656 die $? if $?; # just in case close didn't error out
1657 return wantarray ? @ret : join('',@ret);
1658 };
1659 return unless $old;
1660 my $head = eval { safe_qx('git-rev-parse',"refs/remotes/$GIT_SVN") };
1661 if ($@ || !$head) {
1662 print STDERR "Please run: $0 rebuild --upgrade\n";
1663 exit 1;
1664 }
1665}
1666
69f0d91e
EW
1667# fills %tree_map with a reverse mapping of trees to commits. Useful
1668# for finding parents to commit on.
1669sub map_tree_joins {
098749d9 1670 my %seen;
69f0d91e
EW
1671 foreach my $br (@_branch_from) {
1672 my $pid = open my $pipe, '-|';
1673 defined $pid or croak $!;
1674 if ($pid == 0) {
098749d9 1675 exec(qw(git-rev-list --topo-order --pretty=raw), $br)
b8c92cad 1676 or croak $!;
69f0d91e
EW
1677 }
1678 while (<$pipe>) {
1679 if (/^commit ($sha1)$/o) {
1680 my $commit = $1;
098749d9
EW
1681
1682 # if we've seen a commit,
1683 # we've seen its parents
1684 last if $seen{$commit};
69f0d91e
EW
1685 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
1686 unless (defined $tree) {
1687 die "Failed to parse commit $commit\n";
1688 }
1689 push @{$tree_map{$tree}}, $commit;
098749d9 1690 $seen{$commit} = 1;
69f0d91e
EW
1691 }
1692 }
098749d9 1693 close $pipe; # we could be breaking the pipe early
69f0d91e
EW
1694 }
1695}
1696
bf78b1d8
EW
1697sub load_all_refs {
1698 if (@_branch_from) {
1699 print STDERR '--branch|-b parameters are ignored when ',
1700 "--branch-all-refs|-B is passed\n";
1701 }
1702
1703 # don't worry about rev-list on non-commit objects/tags,
1704 # it shouldn't blow up if a ref is a blob or tree...
1705 chomp(@_branch_from = `git-rev-parse --symbolic --all`);
1706}
1707
eeb0abe0
EW
1708# '<svn username> = real-name <email address>' mapping based on git-svnimport:
1709sub load_authors {
1710 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1711 while (<$authors>) {
1712 chomp;
1713 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
1714 my ($user, $name, $email) = ($1, $2, $3);
1715 $users{$user} = [$name, $email];
1716 }
1717 close $authors or croak $!;
1718}
1719
79bb8d88
EW
1720sub rload_authors {
1721 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1722 while (<$authors>) {
1723 chomp;
1724 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
1725 my ($user, $name, $email) = ($1, $2, $3);
1726 $rusers{"$name <$email>"} = $user;
1727 }
1728 close $authors or croak $!;
1729}
1730
8a97e368
EW
1731sub svn_propget_base {
1732 my ($p, $f) = @_;
1733 $f .= '@BASE' if $_svn_pg_peg_revs;
1734 return safe_qx(qw/svn propget/, $p, $f);
1735}
1736
9d55b41a
EW
1737sub git_svn_each {
1738 my $sub = shift;
1739 foreach (`git-rev-parse --symbolic --all`) {
1740 next unless s#^refs/remotes/##;
1741 chomp $_;
1742 next unless -f "$GIT_DIR/svn/$_/info/url";
1743 &$sub($_);
1744 }
1745}
1746
883d0a78
EW
1747sub migration_check {
1748 return if (-d "$GIT_DIR/svn" || !-d $GIT_DIR);
1749 print "Upgrading repository...\n";
1750 unless (-d "$GIT_DIR/svn") {
1751 mkdir "$GIT_DIR/svn" or croak $!;
1752 }
1753 print "Data from a previous version of git-svn exists, but\n\t",
1754 "$GIT_SVN_DIR\n\t(required for this version ",
1755 "($VERSION) of git-svn) does not.\n";
1756
1757 foreach my $x (`git-rev-parse --symbolic --all`) {
1758 next unless $x =~ s#^refs/remotes/##;
1759 chomp $x;
1760 next unless -f "$GIT_DIR/$x/info/url";
1761 my $u = eval { file_to_s("$GIT_DIR/$x/info/url") };
1762 next unless $u;
1763 my $dn = dirname("$GIT_DIR/svn/$x");
1764 mkpath([$dn]) unless -d $dn;
1765 rename "$GIT_DIR/$x", "$GIT_DIR/svn/$x" or croak "$!: $x";
1766 my ($url, $path) = repo_path_split($u);
1767 s_to_file($url, "$GIT_DIR/svn/$x/info/repo_url");
1768 s_to_file($path, "$GIT_DIR/svn/$x/info/repo_path");
1769 }
1770 print "Done upgrading.\n";
1771}
1772
9d55b41a
EW
1773sub find_rev_before {
1774 my ($r, $git_svn_id) = @_;
1775 my @revs = map { basename $_ } <$GIT_DIR/svn/$git_svn_id/revs/*>;
1776 foreach my $r0 (sort { $b <=> $a } @revs) {
1777 next if $r0 >= $r;
1778 return ($r0, file_to_s("$GIT_DIR/svn/$git_svn_id/revs/$r0"));
1779 }
1780 return (undef, undef);
1781}
1782
b8c92cad
EW
1783sub init_vars {
1784 $GIT_SVN ||= $ENV{GIT_SVN_ID} || 'git-svn';
1785 $GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN";
1786 $GIT_SVN_INDEX = "$GIT_SVN_DIR/index";
1787 $SVN_URL = undef;
1788 $REV_DIR = "$GIT_SVN_DIR/revs";
1789 $SVN_WC = "$GIT_SVN_DIR/tree";
1790}
1791
1792# convert GetOpt::Long specs for use by git-repo-config
1793sub read_repo_config {
1794 return unless -d $GIT_DIR;
1795 my $opts = shift;
1796 foreach my $o (keys %$opts) {
1797 my $v = $opts->{$o};
1798 my ($key) = ($o =~ /^([a-z\-]+)/);
1799 $key =~ s/-//g;
1800 my $arg = 'git-repo-config';
1801 $arg .= ' --int' if ($o =~ /[:=]i$/);
1802 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1803 if (ref $v eq 'ARRAY') {
1804 chomp(my @tmp = `$arg --get-all svn.$key`);
1805 @$v = @tmp if @tmp;
1806 } else {
1807 chomp(my $tmp = `$arg --get svn.$key`);
1808 if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) {
1809 $$v = $tmp;
1810 }
1811 }
1812 }
1813}
1814
dc5869c0
EW
1815sub set_default_vals {
1816 if (defined $_repack) {
1817 $_repack = 1000 if ($_repack <= 0);
1818 $_repack_nr = $_repack;
1819 $_repack_flags ||= '';
1820 }
1821}
1822
9d55b41a
EW
1823sub read_grafts {
1824 my $gr_file = shift;
1825 my ($grafts, $comments) = ({}, {});
1826 if (open my $fh, '<', $gr_file) {
1827 my @tmp;
1828 while (<$fh>) {
1829 if (/^($sha1)\s+/) {
1830 my $c = $1;
1831 if (@tmp) {
1832 @{$comments->{$c}} = @tmp;
1833 @tmp = ();
1834 }
1835 foreach my $p (split /\s+/, $_) {
1836 $grafts->{$c}->{$p} = 1;
1837 }
1838 } else {
1839 push @tmp, $_;
1840 }
1841 }
1842 close $fh or croak $!;
1843 @{$comments->{'END'}} = @tmp if @tmp;
1844 }
1845 return ($grafts, $comments);
1846}
1847
1848sub write_grafts {
1849 my ($grafts, $comments, $gr_file) = @_;
1850
1851 open my $fh, '>', $gr_file or croak $!;
1852 foreach my $c (sort keys %$grafts) {
1853 if ($comments->{$c}) {
1854 print $fh $_ foreach @{$comments->{$c}};
1855 }
1856 my $p = $grafts->{$c};
1857 delete $p->{$c}; # commits are not self-reproducing...
1858 my $pid = open my $ch, '-|';
1859 defined $pid or croak $!;
1860 if (!$pid) {
1861 exec(qw/git-cat-file commit/, $c) or croak $!;
1862 }
1863 while (<$ch>) {
1864 if (/^parent ([a-f\d]{40})/) {
1865 $p->{$1} = 1;
1866 } else {
1867 last unless /^\S/i;
1868 }
1869 }
1870 close $ch; # breaking the pipe
1871 print $fh $c, ' ', join(' ', sort keys %$p),"\n";
1872 }
1873 if ($comments->{'END'}) {
1874 print $fh $_ foreach @{$comments->{'END'}};
1875 }
1876 close $fh or croak $!;
1877}
1878
1879sub read_url_paths {
1880 my $l_map = {};
1881 git_svn_each(sub { my $x = shift;
1882 my $u = file_to_s("$GIT_DIR/svn/$x/info/repo_url");
1883 my $p = file_to_s("$GIT_DIR/svn/$x/info/repo_path");
1884 # we hate trailing slashes
1885 if ($u =~ s#(?:^\/+|\/+$)##g) {
1886 s_to_file($u,"$GIT_DIR/svn/$x/info/repo_url");
1887 }
1888 if ($p =~ s#(?:^\/+|\/+$)##g) {
1889 s_to_file($p,"$GIT_DIR/svn/$x/info/repo_path");
1890 }
1891 $l_map->{$u}->{$p} = $x;
1892 });
1893 return $l_map;
1894}
1895
79bb8d88
EW
1896sub extract_metadata {
1897 my $id = shift;
1898 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
1899 \s([a-f\d\-]+)$/x);
1900 if (!$rev || !$uuid || !$url) {
1901 # some of the original repositories I made had
1902 # indentifiers like this:
1903 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
1904 }
1905 return ($url, $rev, $uuid);
1906}
1907
1908sub tz_to_s_offset {
1909 my ($tz) = @_;
1910 $tz =~ s/(\d\d)$//;
1911 return ($1 * 60) + ($tz * 3600);
1912}
1913
1914sub setup_pager { # translated to Perl from pager.c
1915 return unless (-t *STDOUT);
1916 my $pager = $ENV{PAGER};
1917 if (!defined $pager) {
1918 $pager = 'less';
1919 } elsif (length $pager == 0 || $pager eq 'cat') {
1920 return;
1921 }
1922 pipe my $rfd, my $wfd or return;
1923 defined(my $pid = fork) or croak $!;
1924 if (!$pid) {
1925 open STDOUT, '>&', $wfd or croak $!;
1926 return;
1927 }
1928 open STDIN, '<&', $rfd or croak $!;
1929 $ENV{LESS} ||= '-S';
1930 exec $pager or croak "Can't run pager: $!\n";;
1931}
1932
1933sub get_author_info {
1934 my ($dest, $author, $t, $tz) = @_;
1935 $author =~ s/(?:^\s*|\s*$)//g;
1936 my $_a;
1937 if ($_authors) {
1938 $_a = $rusers{$author} || undef;
1939 }
1940 if (!$_a) {
1941 ($_a) = ($author =~ /<([^>]+)\@[^>]+>$/);
1942 }
1943 $dest->{t} = $t;
1944 $dest->{tz} = $tz;
1945 $dest->{a} = $_a;
1946 # Date::Parse isn't in the standard Perl distro :(
1947 if ($tz =~ s/^\+//) {
1948 $t += tz_to_s_offset($tz);
1949 } elsif ($tz =~ s/^\-//) {
1950 $t -= tz_to_s_offset($tz);
1951 }
1952 $dest->{t_utc} = $t;
1953}
1954
1955sub process_commit {
1956 my ($c, $r_min, $r_max, $defer) = @_;
1957 if (defined $r_min && defined $r_max) {
1958 if ($r_min == $c->{r} && $r_min == $r_max) {
1959 show_commit($c);
1960 return 0;
1961 }
1962 return 1 if $r_min == $r_max;
1963 if ($r_min < $r_max) {
1964 # we need to reverse the print order
1965 return 0 if (defined $_limit && --$_limit < 0);
1966 push @$defer, $c;
1967 return 1;
1968 }
1969 if ($r_min != $r_max) {
1970 return 1 if ($r_min < $c->{r});
1971 return 1 if ($r_max > $c->{r});
1972 }
1973 }
1974 return 0 if (defined $_limit && --$_limit < 0);
1975 show_commit($c);
1976 return 1;
1977}
1978
1979sub show_commit {
1980 my $c = shift;
1981 if ($_oneline) {
1982 my $x = "\n";
1983 if (my $l = $c->{l}) {
1984 while ($l->[0] =~ /^\s*$/) { shift @$l }
1985 $x = $l->[0];
1986 }
1987 $_l_fmt ||= 'A' . length($c->{r});
1988 print 'r',pack($_l_fmt, $c->{r}),' | ';
1989 print "$c->{c} | " if $_show_commit;
1990 print $x;
1991 } else {
1992 show_commit_normal($c);
1993 }
1994}
1995
1996sub show_commit_normal {
1997 my ($c) = @_;
1998 print '-' x72, "\nr$c->{r} | ";
1999 print "$c->{c} | " if $_show_commit;
2000 print "$c->{a} | ", strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
2001 localtime($c->{t_utc})), ' | ';
2002 my $nr_line = 0;
2003
2004 if (my $l = $c->{l}) {
2005 while ($l->[$#$l] eq "\n" && $l->[($#$l - 1)] eq "\n") {
2006 pop @$l;
2007 }
2008 $nr_line = scalar @$l;
2009 if (!$nr_line) {
2010 print "1 line\n\n\n";
2011 } else {
2012 if ($nr_line == 1) {
2013 $nr_line = '1 line';
2014 } else {
2015 $nr_line .= ' lines';
2016 }
2017 print $nr_line, "\n\n";
2018 print $_ foreach @$l;
2019 }
2020 } else {
2021 print "1 line\n\n";
2022
2023 }
2024 foreach my $x (qw/raw diff/) {
2025 if ($c->{$x}) {
2026 print "\n";
2027 print $_ foreach @{$c->{$x}}
2028 }
2029 }
2030}
2031
3397f9df
EW
2032__END__
2033
2034Data structures:
2035
03823184
EW
2036$svn_log hashref (as returned by svn_log_raw)
2037{
2038 fh => file handle of the log file,
2039 state => state of the log file parser (sep/msg/rev/msg_start...)
2040}
3397f9df 2041
03823184 2042$log_msg hashref as returned by next_log_entry($svn_log)
3397f9df
EW
2043{
2044 msg => 'whitespace-formatted log entry
2045', # trailing newline is preserved
2046 revision => '8', # integer
2047 date => '2004-02-24T17:01:44.108345Z', # commit date
2048 author => 'committer name'
2049};
2050
2051
2052@mods = array of diff-index line hashes, each element represents one line
2053 of diff-index output
2054
2055diff-index line ($m hash)
2056{
2057 mode_a => first column of diff-index output, no leading ':',
2058 mode_b => second column of diff-index output,
2059 sha1_b => sha1sum of the final blob,
ac8e0b91 2060 chg => change type [MCRADT],
3397f9df
EW
2061 file_a => original file name of a file (iff chg is 'C' or 'R')
2062 file_b => new/current file name of a file (any chg)
2063}
2064;