git-svn: support manually placed initial trees from fetch
[git/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
EW
8 $GIT_SVN_INDEX $GIT_SVN
9 $GIT_DIR $REV_DIR/;
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};
3397f9df
EW
18# make sure the svn binary gives consistent output between locales and TZs:
19$ENV{TZ} = 'UTC';
20$ENV{LC_ALL} = 'C';
21
22# If SVN:: library support is added, please make the dependencies
23# optional and preserve the capability to use the command-line client.
ce6f3519 24# use eval { require SVN::... } to make it lazy load
eeb0abe0 25# We don't use any modules not in the standard Perl distribution:
3397f9df
EW
26use Carp qw/croak/;
27use IO::File qw//;
28use File::Basename qw/dirname basename/;
29use File::Path qw/mkpath/;
30use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
31use File::Spec qw//;
e17512f3 32use POSIX qw/strftime/;
3397f9df 33my $sha1 = qr/[a-f\d]{40}/;
ac8e0b91 34my $sha1_short = qr/[a-f\d]{4,40}/;
72942938 35my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
162f4129 36 $_find_copies_harder, $_l, $_cp_similarity,
bf78b1d8 37 $_version, $_upgrade, $_authors, $_branch_all_refs);
a9612be2 38my (@_branch_from, %tree_map, %users);
8a97e368 39my ($_svn_co_url_revs, $_svn_pg_peg_revs);
3397f9df 40
eeb0abe0 41my %fc_opts = ( 'no-ignore-externals' => \$_no_ignore_ext,
69f0d91e 42 'branch|b=s' => \@_branch_from,
bf78b1d8 43 'branch-all-refs|B' => \$_branch_all_refs,
eeb0abe0 44 'authors-file|A=s' => \$_authors );
36f5b1f0
EW
45
46# yes, 'native' sets "\n". Patches to fix this for non-*nix systems welcome:
47my %EOL = ( CR => "\015", LF => "\012", CRLF => "\015\012", native => "\012" );
48
3397f9df 49my %cmd = (
eeb0abe0
EW
50 fetch => [ \&fetch, "Download new revisions from SVN",
51 { 'revision|r=s' => \$_revision, %fc_opts } ],
81c5a0e6
EW
52 init => [ \&init, "Initialize a repo for tracking" .
53 " (requires URL argument)", { } ],
eeb0abe0
EW
54 commit => [ \&commit, "Commit git revisions to SVN",
55 { 'stdin|' => \$_stdin,
56 'edit|e' => \$_edit,
57 'rmdir' => \$_rmdir,
58 'find-copies-harder' => \$_find_copies_harder,
59 'l=i' => \$_l,
162f4129 60 'copy-similarity|C=i'=> \$_cp_similarity,
eeb0abe0
EW
61 %fc_opts,
62 } ],
63 'show-ignore' => [ \&show_ignore, "Show svn:ignore listings", { } ],
64 rebuild => [ \&rebuild, "Rebuild git-svn metadata (after git clone)",
65 { 'no-ignore-externals' => \$_no_ignore_ext,
66 'upgrade' => \$_upgrade } ],
3397f9df
EW
67);
68my $cmd;
69for (my $i = 0; $i < @ARGV; $i++) {
70 if (defined $cmd{$ARGV[$i]}) {
71 $cmd = $ARGV[$i];
72 splice @ARGV, $i, 1;
73 last;
74 }
75};
76
448c81b4 77my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
a9612be2 78
53909056
EW
79# convert GetOpt::Long specs for use by git-repo-config
80foreach my $o (keys %opts) {
81 my $v = $opts{$o};
82 my ($key) = ($o =~ /^([a-z\-]+)/);
83 $key =~ s/-//g;
84 my $arg = 'git-repo-config';
85 $arg .= ' --int' if ($o =~ /=i$/);
86 $arg .= ' --bool' if ($o !~ /=[sfi]$/);
53909056 87 if (ref $v eq 'ARRAY') {
fc9957b0 88 chomp(my @tmp = `$arg --get-all svn.$key`);
5941a9e9 89 @$v = @tmp if @tmp;
53909056 90 } else {
fc9957b0 91 chomp(my $tmp = `$arg --get svn.$key`);
5941a9e9
EW
92 if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) {
93 $$v = $tmp;
94 }
53909056
EW
95 }
96}
97
6f0783cf
EW
98GetOptions(%opts, 'help|H|h' => \$_help,
99 'version|V' => \$_version,
100 'id|i=s' => \$GIT_SVN) or exit 1;
101
102$GIT_SVN ||= $ENV{GIT_SVN_ID} || 'git-svn';
103$GIT_SVN_INDEX = "$GIT_DIR/$GIT_SVN/index";
6f0783cf
EW
104$SVN_URL = undef;
105$REV_DIR = "$GIT_DIR/$GIT_SVN/revs";
106$SVN_WC = "$GIT_DIR/$GIT_SVN/tree";
107
3397f9df 108usage(0) if $_help;
551ce28f 109version() if $_version;
eeb0abe0
EW
110usage(1) unless defined $cmd;
111load_authors() if $_authors;
bf78b1d8 112load_all_refs() if $_branch_all_refs;
1d52aba8 113svn_compat_check();
3397f9df
EW
114$cmd{$cmd}->[0]->(@ARGV);
115exit 0;
116
117####################### primary functions ######################
118sub usage {
119 my $exit = shift || 0;
120 my $fd = $exit ? \*STDERR : \*STDOUT;
121 print $fd <<"";
122git-svn - bidirectional operations between a single Subversion tree and git
123Usage: $0 <command> [options] [arguments]\n
448c81b4
EW
124
125 print $fd "Available commands:\n" unless $cmd;
3397f9df
EW
126
127 foreach (sort keys %cmd) {
448c81b4 128 next if $cmd && $cmd ne $_;
ac8e0b91 129 print $fd ' ',pack('A13',$_),$cmd{$_}->[1],"\n";
448c81b4
EW
130 foreach (keys %{$cmd{$_}->[2]}) {
131 # prints out arguments as they should be passed:
132 my $x = s#=s$## ? '<arg>' : s#=i$## ? '<num>' : '';
133 print $fd ' ' x 17, join(', ', map { length $_ > 1 ?
134 "--$_" : "-$_" }
135 split /\|/,$_)," $x\n";
136 }
3397f9df
EW
137 }
138 print $fd <<"";
448c81b4
EW
139\nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
140arbitrary identifier if you're tracking multiple SVN branches/repositories in
141one git repository and want to keep them separate. See git-svn(1) for more
142information.
3397f9df
EW
143
144 exit $exit;
145}
146
551ce28f
EW
147sub version {
148 print "git-svn version $VERSION\n";
149 exit 0;
150}
151
3397f9df
EW
152sub rebuild {
153 $SVN_URL = shift or undef;
3397f9df 154 my $newest_rev = 0;
2beb3cdd
EW
155 if ($_upgrade) {
156 sys('git-update-ref',"refs/remotes/$GIT_SVN","$GIT_SVN-HEAD");
157 } else {
158 check_upgrade_needed();
159 }
3397f9df
EW
160
161 my $pid = open(my $rev_list,'-|');
162 defined $pid or croak $!;
163 if ($pid == 0) {
2beb3cdd 164 exec("git-rev-list","refs/remotes/$GIT_SVN") or croak $!;
3397f9df 165 }
2beb3cdd 166 my $latest;
3397f9df
EW
167 while (<$rev_list>) {
168 chomp;
169 my $c = $_;
170 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
171 my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
172 next if (!@commit); # skip merges
173 my $id = $commit[$#commit];
174 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
175 \s([a-f\d\-]+)$/x);
176 if (!$rev || !$uuid || !$url) {
177 # some of the original repositories I made had
178 # indentifiers like this:
179 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)
180 \@([a-f\d\-]+)/x);
181 if (!$rev || !$uuid) {
182 croak "Unable to extract revision or UUID from ",
183 "$c, $id\n";
184 }
185 }
2beb3cdd
EW
186
187 # if we merged or otherwise started elsewhere, this is
188 # how we break out of it
1ca72aef 189 next if (defined $SVN_UUID && ($uuid ne $SVN_UUID));
779b1446 190 next if (defined $SVN_URL && defined $url && ($url ne $SVN_URL));
2beb3cdd 191
3397f9df 192 print "r$rev = $c\n";
2beb3cdd 193 unless (defined $latest) {
3397f9df
EW
194 if (!$SVN_URL && !$url) {
195 croak "SVN repository location required: $url\n";
196 }
197 $SVN_URL ||= $url;
1d52aba8
EW
198 $SVN_UUID ||= $uuid;
199 setup_git_svn();
2beb3cdd 200 $latest = $rev;
3397f9df
EW
201 }
202 assert_revision_eq_or_unknown($rev, $c);
203 sys('git-update-ref',"$GIT_SVN/revs/$rev",$c);
204 $newest_rev = $rev if ($rev > $newest_rev);
205 }
206 close $rev_list or croak $?;
207 if (!chdir $SVN_WC) {
1d52aba8 208 svn_cmd_checkout($SVN_URL, $latest, $SVN_WC);
3397f9df
EW
209 chdir $SVN_WC or croak $!;
210 }
211
212 $pid = fork;
213 defined $pid or croak $!;
214 if ($pid == 0) {
215 my @svn_up = qw(svn up);
216 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
217 sys(@svn_up,"-r$newest_rev");
218 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
36f5b1f0 219 index_changes();
3397f9df
EW
220 exec('git-write-tree');
221 }
222 waitpid $pid, 0;
2beb3cdd
EW
223
224 if ($_upgrade) {
225 print STDERR <<"";
226Keeping deprecated refs/head/$GIT_SVN-HEAD for now. Please remove it
227when you have upgraded your tools and habits to use refs/remotes/$GIT_SVN
228
229 }
3397f9df
EW
230}
231
232sub init {
81c5a0e6
EW
233 $SVN_URL = shift or die "SVN repository location required " .
234 "as a command-line argument\n";
3397f9df
EW
235 unless (-d $GIT_DIR) {
236 sys('git-init-db');
237 }
238 setup_git_svn();
239}
240
241sub fetch {
242 my (@parents) = @_;
2beb3cdd 243 check_upgrade_needed();
3397f9df
EW
244 $SVN_URL ||= file_to_s("$GIT_DIR/$GIT_SVN/info/url");
245 my @log_args = -d $SVN_WC ? ($SVN_WC) : ($SVN_URL);
defc6492
EW
246 unless ($_revision) {
247 $_revision = -d $SVN_WC ? 'BASE:HEAD' : '0:HEAD';
3397f9df 248 }
defc6492 249 push @log_args, "-r$_revision";
3397f9df
EW
250 push @log_args, '--stop-on-copy' unless $_no_stop_copy;
251
ce6f3519 252 my $svn_log = svn_log_raw(@log_args);
3397f9df 253
03823184 254 my $base = next_log_entry($svn_log) or croak "No base revision!\n";
3397f9df
EW
255 my $last_commit = undef;
256 unless (-d $SVN_WC) {
1d52aba8 257 svn_cmd_checkout($SVN_URL,$base->{revision},$SVN_WC);
3397f9df 258 chdir $SVN_WC or croak $!;
1d52aba8 259 read_uuid();
3397f9df 260 $last_commit = git_commit($base, @parents);
36f5b1f0 261 assert_tree($last_commit);
3397f9df
EW
262 } else {
263 chdir $SVN_WC or croak $!;
1d52aba8 264 read_uuid();
6dfbe516
EW
265 eval { $last_commit = file_to_s("$REV_DIR/$base->{revision}") };
266 # looks like a user manually cp'd and svn switch'ed
267 unless ($last_commit) {
268 sys(qw/svn revert -R ./);
269 assert_svn_wc_clean($base->{revision});
270 $last_commit = git_commit($base, @parents);
271 assert_tree($last_commit);
272 }
3397f9df
EW
273 }
274 my @svn_up = qw(svn up);
275 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
03823184
EW
276 my $last = $base;
277 while (my $log_msg = next_log_entry($svn_log)) {
36f5b1f0 278 assert_tree($last_commit);
03823184
EW
279 if ($last->{revision} >= $log_msg->{revision}) {
280 croak "Out of order: last >= current: ",
281 "$last->{revision} >= $log_msg->{revision}\n";
282 }
36f5b1f0
EW
283 # Revert is needed for cases like:
284 # https://svn.musicpd.org/Jamming/trunk (r166:167), but
285 # I can't seem to reproduce something like that on a test...
286 sys(qw/svn revert -R ./);
287 assert_svn_wc_clean($last->{revision});
03823184 288 sys(@svn_up,"-r$log_msg->{revision}");
3397f9df 289 $last_commit = git_commit($log_msg, $last_commit, @parents);
03823184 290 $last = $log_msg;
3397f9df 291 }
7f60b228
EW
292 unless (-e "$GIT_DIR/refs/heads/master") {
293 sys(qw(git-update-ref refs/heads/master),$last_commit);
294 }
03823184 295 return $last;
3397f9df
EW
296}
297
298sub commit {
299 my (@commits) = @_;
2beb3cdd 300 check_upgrade_needed();
3397f9df
EW
301 if ($_stdin || !@commits) {
302 print "Reading from stdin...\n";
303 @commits = ();
304 while (<STDIN>) {
1ca72aef 305 if (/\b($sha1_short)\b/o) {
3397f9df
EW
306 unshift @commits, $1;
307 }
308 }
309 }
310 my @revs;
8de010ad
EW
311 foreach my $c (@commits) {
312 chomp(my @tmp = safe_qx('git-rev-parse',$c));
313 if (scalar @tmp == 1) {
314 push @revs, $tmp[0];
315 } elsif (scalar @tmp > 1) {
316 push @revs, reverse (safe_qx('git-rev-list',@tmp));
317 } else {
318 die "Failed to rev-parse $c\n";
319 }
3397f9df
EW
320 }
321 chomp @revs;
322
b63af9b3 323 chdir $SVN_WC or croak "Unable to chdir $SVN_WC: $!\n";
1d52aba8 324 my $info = svn_info('.');
b63af9b3
EW
325 my $fetched = fetch();
326 if ($info->{Revision} != $fetched->{revision}) {
327 print STDERR "There are new revisions that were fetched ",
328 "and need to be merged (or acknowledged) ",
329 "before committing.\n";
330 exit 1;
331 }
332 $info = svn_info('.');
1d52aba8
EW
333 read_uuid($info);
334 my $svn_current_rev = $info->{'Last Changed Rev'};
3397f9df 335 foreach my $c (@revs) {
cf52b8f0
EW
336 my $mods = svn_checkout_tree($svn_current_rev, $c);
337 if (scalar @$mods == 0) {
338 print "Skipping, no changes detected\n";
339 next;
340 }
3397f9df
EW
341 $svn_current_rev = svn_commit_tree($svn_current_rev, $c);
342 }
343 print "Done committing ",scalar @revs," revisions to SVN\n";
3397f9df
EW
344}
345
8f22562c
EW
346sub show_ignore {
347 require File::Find or die $!;
348 my $exclude_file = "$GIT_DIR/info/exclude";
349 open my $fh, '<', $exclude_file or croak $!;
350 chomp(my @excludes = (<$fh>));
351 close $fh or croak $!;
352
353 $SVN_URL ||= file_to_s("$GIT_DIR/$GIT_SVN/info/url");
354 chdir $SVN_WC or croak $!;
355 my %ign;
356 File::Find::find({wanted=>sub{if(lstat $_ && -d _ && -d "$_/.svn"){
357 s#^\./##;
8a97e368 358 @{$ign{$_}} = svn_propget_base('svn:ignore', $_);
8f22562c
EW
359 }}, no_chdir=>1},'.');
360
361 print "\n# /\n";
362 foreach (@{$ign{'.'}}) { print '/',$_ if /\S/ }
363 delete $ign{'.'};
364 foreach my $i (sort keys %ign) {
365 print "\n# ",$i,"\n";
366 foreach (@{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ }
367 }
368}
369
3397f9df
EW
370########################### utility functions #########################
371
1d52aba8
EW
372sub read_uuid {
373 return if $SVN_UUID;
374 my $info = shift || svn_info('.');
375 $SVN_UUID = $info->{'Repository UUID'} or
376 croak "Repository UUID unreadable\n";
377 s_to_file($SVN_UUID,"$GIT_DIR/$GIT_SVN/info/uuid");
378}
379
3397f9df
EW
380sub setup_git_svn {
381 defined $SVN_URL or croak "SVN repository location required\n";
382 unless (-d $GIT_DIR) {
383 croak "GIT_DIR=$GIT_DIR does not exist!\n";
384 }
385 mkpath(["$GIT_DIR/$GIT_SVN"]);
386 mkpath(["$GIT_DIR/$GIT_SVN/info"]);
387 mkpath([$REV_DIR]);
388 s_to_file($SVN_URL,"$GIT_DIR/$GIT_SVN/info/url");
3397f9df
EW
389
390 open my $fd, '>>', "$GIT_DIR/$GIT_SVN/info/exclude" or croak $!;
391 print $fd '.svn',"\n";
392 close $fd or croak $!;
3397f9df
EW
393}
394
395sub assert_svn_wc_clean {
36f5b1f0 396 my ($svn_rev) = @_;
3397f9df 397 croak "$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
ce4c8b24
EW
398 my $lcr = svn_info('.')->{'Last Changed Rev'};
399 if ($svn_rev != $lcr) {
400 print STDERR "Checking for copy-tree ... ";
ce4c8b24
EW
401 my @diff = grep(/^Index: /,(safe_qx(qw(svn diff),
402 "-r$lcr:$svn_rev")));
403 if (@diff) {
404 croak "Nope! Expected r$svn_rev, got r$lcr\n";
405 } else {
406 print STDERR "OK!\n";
407 }
3397f9df
EW
408 }
409 my @status = grep(!/^Performing status on external/,(`svn status`));
410 @status = grep(!/^\s*$/,@status);
411 if (scalar @status) {
412 print STDERR "Tree ($SVN_WC) is not clean:\n";
413 print STDERR $_ foreach @status;
414 croak;
415 }
cf52b8f0
EW
416}
417
418sub assert_tree {
419 my ($treeish) = @_;
420 croak "Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
421 chomp(my $type = `git-cat-file -t $treeish`);
422 my $expected;
423 while ($type eq 'tag') {
424 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
425 }
426 if ($type eq 'commit') {
427 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
428 ($expected) = ($expected =~ /^tree ($sha1)$/);
429 die "Unable to get tree from $treeish\n" unless $expected;
430 } elsif ($type eq 'tree') {
431 $expected = $treeish;
432 } else {
433 die "$treeish is a $type, expected tree, tag or commit\n";
434 }
435
436 my $old_index = $ENV{GIT_INDEX_FILE};
437 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
438 if (-e $tmpindex) {
439 unlink $tmpindex or croak $!;
440 }
441 $ENV{GIT_INDEX_FILE} = $tmpindex;
36f5b1f0 442 index_changes(1);
cf52b8f0
EW
443 chomp(my $tree = `git-write-tree`);
444 if ($old_index) {
445 $ENV{GIT_INDEX_FILE} = $old_index;
446 } else {
447 delete $ENV{GIT_INDEX_FILE};
448 }
449 if ($tree ne $expected) {
450 croak "Tree mismatch, Got: $tree, Expected: $expected\n";
3397f9df 451 }
36f5b1f0 452 unlink $tmpindex;
3397f9df
EW
453}
454
455sub parse_diff_tree {
456 my $diff_fh = shift;
457 local $/ = "\0";
458 my $state = 'meta';
459 my @mods;
460 while (<$diff_fh>) {
461 chomp $_; # this gets rid of the trailing "\0"
3397f9df
EW
462 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
463 $sha1\s($sha1)\s([MTCRAD])\d*$/xo) {
464 push @mods, { mode_a => $1, mode_b => $2,
465 sha1_b => $3, chg => $4 };
466 if ($4 =~ /^(?:C|R)$/) {
467 $state = 'file_a';
468 } else {
469 $state = 'file_b';
470 }
471 } elsif ($state eq 'file_a') {
cf52b8f0 472 my $x = $mods[$#mods] or croak "Empty array\n";
3397f9df 473 if ($x->{chg} !~ /^(?:C|R)$/) {
cf52b8f0 474 croak "Error parsing $_, $x->{chg}\n";
3397f9df
EW
475 }
476 $x->{file_a} = $_;
477 $state = 'file_b';
478 } elsif ($state eq 'file_b') {
cf52b8f0 479 my $x = $mods[$#mods] or croak "Empty array\n";
3397f9df 480 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
cf52b8f0 481 croak "Error parsing $_, $x->{chg}\n";
3397f9df
EW
482 }
483 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
cf52b8f0 484 croak "Error parsing $_, $x->{chg}\n";
3397f9df
EW
485 }
486 $x->{file_b} = $_;
487 $state = 'meta';
488 } else {
cf52b8f0 489 croak "Error parsing $_\n";
3397f9df
EW
490 }
491 }
492 close $diff_fh or croak $!;
cf52b8f0 493
3397f9df
EW
494 return \@mods;
495}
496
497sub svn_check_prop_executable {
498 my $m = shift;
cf52b8f0
EW
499 return if -l $m->{file_b};
500 if ($m->{mode_b} =~ /755$/) {
501 chmod((0755 &~ umask),$m->{file_b}) or croak $!;
502 if ($m->{mode_a} !~ /755$/) {
503 sys(qw(svn propset svn:executable 1), $m->{file_b});
504 }
505 -x $m->{file_b} or croak "$m->{file_b} is not executable!\n";
3397f9df
EW
506 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
507 sys(qw(svn propdel svn:executable), $m->{file_b});
cf52b8f0
EW
508 chmod((0644 &~ umask),$m->{file_b}) or croak $!;
509 -x $m->{file_b} and croak "$m->{file_b} is executable!\n";
3397f9df
EW
510 }
511}
512
513sub svn_ensure_parent_path {
514 my $dir_b = dirname(shift);
515 svn_ensure_parent_path($dir_b) if ($dir_b ne File::Spec->curdir);
516 mkpath([$dir_b]) unless (-d $dir_b);
517 sys(qw(svn add -N), $dir_b) unless (-d "$dir_b/.svn");
518}
519
cf52b8f0
EW
520sub precommit_check {
521 my $mods = shift;
522 my (%rm_file, %rmdir_check, %added_check);
523
524 my %o = ( D => 0, R => 1, C => 2, A => 3, M => 3, T => 3 );
525 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
526 if ($m->{chg} eq 'R') {
527 if (-d $m->{file_b}) {
528 err_dir_to_file("$m->{file_a} => $m->{file_b}");
529 }
530 # dir/$file => dir/file/$file
531 my $dirname = dirname($m->{file_b});
532 while ($dirname ne File::Spec->curdir) {
533 if ($dirname ne $m->{file_a}) {
534 $dirname = dirname($dirname);
535 next;
536 }
537 err_file_to_dir("$m->{file_a} => $m->{file_b}");
538 }
539 # baz/zzz => baz (baz is a file)
540 $dirname = dirname($m->{file_a});
541 while ($dirname ne File::Spec->curdir) {
542 if ($dirname ne $m->{file_b}) {
543 $dirname = dirname($dirname);
544 next;
545 }
546 err_dir_to_file("$m->{file_a} => $m->{file_b}");
547 }
548 }
549 if ($m->{chg} =~ /^(D|R)$/) {
550 my $t = $1 eq 'D' ? 'file_b' : 'file_a';
551 $rm_file{ $m->{$t} } = 1;
552 my $dirname = dirname( $m->{$t} );
553 my $basename = basename( $m->{$t} );
554 $rmdir_check{$dirname}->{$basename} = 1;
555 } elsif ($m->{chg} =~ /^(?:A|C)$/) {
556 if (-d $m->{file_b}) {
557 err_dir_to_file($m->{file_b});
558 }
559 my $dirname = dirname( $m->{file_b} );
560 my $basename = basename( $m->{file_b} );
561 $added_check{$dirname}->{$basename} = 1;
562 while ($dirname ne File::Spec->curdir) {
563 if ($rm_file{$dirname}) {
564 err_file_to_dir($m->{file_b});
565 }
566 $dirname = dirname $dirname;
567 }
568 }
569 }
570 return (\%rmdir_check, \%added_check);
571
572 sub err_dir_to_file {
573 my $file = shift;
574 print STDERR "Node change from directory to file ",
575 "is not supported by Subversion: ",$file,"\n";
576 exit 1;
577 }
578 sub err_file_to_dir {
579 my $file = shift;
580 print STDERR "Node change from file to directory ",
581 "is not supported by Subversion: ",$file,"\n";
582 exit 1;
583 }
584}
585
3397f9df 586sub svn_checkout_tree {
cf52b8f0 587 my ($svn_rev, $treeish) = @_;
3397f9df 588 my $from = file_to_s("$REV_DIR/$svn_rev");
36f5b1f0 589 assert_tree($from);
ac8e0b91 590 print "diff-tree $from $treeish\n";
3397f9df
EW
591 my $pid = open my $diff_fh, '-|';
592 defined $pid or croak $!;
593 if ($pid == 0) {
162f4129
EW
594 my @diff_tree = qw(git-diff-tree -z -r);
595 if ($_cp_similarity) {
596 push @diff_tree, "-C$_cp_similarity";
597 } else {
598 push @diff_tree, '-C';
599 }
72942938
EW
600 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
601 push @diff_tree, "-l$_l" if defined $_l;
cf52b8f0 602 exec(@diff_tree, $from, $treeish) or croak $!;
3397f9df
EW
603 }
604 my $mods = parse_diff_tree($diff_fh);
605 unless (@$mods) {
ac8e0b91 606 # git can do empty commits, but SVN doesn't allow it...
cf52b8f0 607 return $mods;
3397f9df 608 }
cf52b8f0
EW
609 my ($rm, $add) = precommit_check($mods);
610
611 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
612 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
3397f9df
EW
613 if ($m->{chg} eq 'C') {
614 svn_ensure_parent_path( $m->{file_b} );
615 sys(qw(svn cp), $m->{file_a}, $m->{file_b});
cf52b8f0 616 apply_mod_line_blob($m);
3397f9df
EW
617 svn_check_prop_executable($m);
618 } elsif ($m->{chg} eq 'D') {
3397f9df
EW
619 sys(qw(svn rm --force), $m->{file_b});
620 } elsif ($m->{chg} eq 'R') {
621 svn_ensure_parent_path( $m->{file_b} );
622 sys(qw(svn mv --force), $m->{file_a}, $m->{file_b});
cf52b8f0 623 apply_mod_line_blob($m);
3397f9df 624 svn_check_prop_executable($m);
3397f9df 625 } elsif ($m->{chg} eq 'M') {
cf52b8f0 626 apply_mod_line_blob($m);
3397f9df
EW
627 svn_check_prop_executable($m);
628 } elsif ($m->{chg} eq 'T') {
629 sys(qw(svn rm --force),$m->{file_b});
cf52b8f0 630 apply_mod_line_blob($m);
3397f9df 631 sys(qw(svn add --force), $m->{file_b});
cf52b8f0 632 svn_check_prop_executable($m);
3397f9df
EW
633 } elsif ($m->{chg} eq 'A') {
634 svn_ensure_parent_path( $m->{file_b} );
cf52b8f0 635 apply_mod_line_blob($m);
3397f9df 636 sys(qw(svn add --force), $m->{file_b});
cf52b8f0 637 svn_check_prop_executable($m);
3397f9df
EW
638 } else {
639 croak "Invalid chg: $m->{chg}\n";
640 }
641 }
cf52b8f0
EW
642
643 assert_tree($treeish);
644 if ($_rmdir) { # remove empty directories
645 handle_rmdir($rm, $add);
646 }
647 assert_tree($treeish);
648 return $mods;
649}
650
651# svn ls doesn't work with respect to the current working tree, but what's
652# in the repository. There's not even an option for it... *sigh*
653# (added files don't show up and removed files remain in the ls listing)
654sub svn_ls_current {
655 my ($dir, $rm, $add) = @_;
656 chomp(my @ls = safe_qx('svn','ls',$dir));
657 my @ret = ();
658 foreach (@ls) {
659 s#/$##; # trailing slashes are evil
660 push @ret, $_ unless $rm->{$dir}->{$_};
661 }
662 if (exists $add->{$dir}) {
663 push @ret, keys %{$add->{$dir}};
664 }
665 return \@ret;
666}
667
668sub handle_rmdir {
669 my ($rm, $add) = @_;
670
671 foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
672 my $ls = svn_ls_current($dir, $rm, $add);
673 next if (scalar @$ls);
674 sys(qw(svn rm --force),$dir);
675
676 my $dn = dirname $dir;
677 $rm->{ $dn }->{ basename $dir } = 1;
678 $ls = svn_ls_current($dn, $rm, $add);
679 while (scalar @$ls == 0 && $dn ne File::Spec->curdir) {
680 sys(qw(svn rm --force),$dn);
681 $dir = basename $dn;
682 $dn = dirname $dn;
683 $rm->{ $dn }->{ $dir } = 1;
684 $ls = svn_ls_current($dn, $rm, $add);
3397f9df
EW
685 }
686 }
687}
688
689sub svn_commit_tree {
690 my ($svn_rev, $commit) = @_;
691 my $commit_msg = "$GIT_DIR/$GIT_SVN/.svn-commit.tmp.$$";
e17512f3 692 my %log_msg = ( msg => '' );
ac8e0b91 693 open my $msg, '>', $commit_msg or croak $!;
3397f9df
EW
694
695 chomp(my $type = `git-cat-file -t $commit`);
696 if ($type eq 'commit') {
697 my $pid = open my $msg_fh, '-|';
698 defined $pid or croak $!;
699
700 if ($pid == 0) {
701 exec(qw(git-cat-file commit), $commit) or croak $!;
702 }
703 my $in_msg = 0;
704 while (<$msg_fh>) {
705 if (!$in_msg) {
706 $in_msg = 1 if (/^\s*$/);
df746c5a
EW
707 } elsif (/^git-svn-id: /) {
708 # skip this, we regenerate the correct one
709 # on re-fetch anyways
3397f9df
EW
710 } else {
711 print $msg $_ or croak $!;
712 }
713 }
714 close $msg_fh or croak $!;
715 }
716 close $msg or croak $!;
717
718 if ($_edit || ($type eq 'tree')) {
719 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
720 system($editor, $commit_msg);
721 }
ac8e0b91
EW
722
723 # file_to_s removes all trailing newlines, so just use chomp() here:
724 open $msg, '<', $commit_msg or croak $!;
725 { local $/; chomp($log_msg{msg} = <$msg>); }
726 close $msg or croak $!;
727
728 my ($oneline) = ($log_msg{msg} =~ /([^\n\r]+)/);
729 print "Committing $commit: $oneline\n";
730
ce475dfc
EW
731 if (defined $LC_ALL) {
732 $ENV{LC_ALL} = $LC_ALL;
733 } else {
734 delete $ENV{LC_ALL};
735 }
3397f9df 736 my @ci_output = safe_qx(qw(svn commit -F),$commit_msg);
ce475dfc 737 $ENV{LC_ALL} = 'C';
3397f9df 738 unlink $commit_msg;
ce475dfc
EW
739 my ($committed) = ($ci_output[$#ci_output] =~ /(\d+)/);
740 if (!defined $committed) {
741 my $out = join("\n",@ci_output);
742 print STDERR "W: Trouble parsing \`svn commit' output:\n\n",
743 $out, "\n\nAssuming English locale...";
744 ($committed) = ($out =~ /^Committed revision \d+\./sm);
745 defined $committed or die " FAILED!\n",
3397f9df 746 "Commit output failed to parse committed revision!\n",
ce475dfc
EW
747 print STDERR " OK\n";
748 }
3397f9df 749
e17512f3 750 my @svn_up = qw(svn up);
3397f9df 751 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
ce475dfc
EW
752 if ($committed == ($svn_rev + 1)) {
753 push @svn_up, "-r$committed";
e17512f3
EW
754 sys(@svn_up);
755 my $info = svn_info('.');
756 my $date = $info->{'Last Changed Date'} or die "Missing date\n";
ce475dfc
EW
757 if ($info->{'Last Changed Rev'} != $committed) {
758 croak "$info->{'Last Changed Rev'} != $committed\n"
e17512f3
EW
759 }
760 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
761 /(\d{4})\-(\d\d)\-(\d\d)\s
762 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
763 or croak "Failed to parse date: $date\n";
764 $log_msg{date} = "$tz $Y-$m-$d $H:$M:$S";
765 $log_msg{author} = $info->{'Last Changed Author'};
ce475dfc 766 $log_msg{revision} = $committed;
e17512f3
EW
767 $log_msg{msg} .= "\n";
768 my $parent = file_to_s("$REV_DIR/$svn_rev");
769 git_commit(\%log_msg, $parent, $commit);
ce475dfc 770 return $committed;
e17512f3
EW
771 }
772 # resync immediately
773 push @svn_up, "-r$svn_rev";
3397f9df 774 sys(@svn_up);
ce475dfc 775 return fetch("$committed=$commit")->{revision};
3397f9df
EW
776}
777
03823184
EW
778# read the entire log into a temporary file (which is removed ASAP)
779# and store the file handle + parser state
3397f9df
EW
780sub svn_log_raw {
781 my (@log_args) = @_;
03823184
EW
782 my $log_fh = IO::File->new_tmpfile or croak $!;
783 my $pid = fork;
3397f9df 784 defined $pid or croak $!;
03823184
EW
785 if (!$pid) {
786 open STDOUT, '>&', $log_fh or croak $!;
3397f9df
EW
787 exec (qw(svn log), @log_args) or croak $!
788 }
03823184
EW
789 waitpid $pid, 0;
790 croak if $?;
791 seek $log_fh, 0, 0 or croak $!;
792 return { state => 'sep', fh => $log_fh };
793}
794
795sub next_log_entry {
796 my $log = shift; # retval of svn_log_raw()
797 my $ret = undef;
798 my $fh = $log->{fh};
3397f9df 799
03823184 800 while (<$fh>) {
3397f9df
EW
801 chomp;
802 if (/^\-{72}$/) {
03823184
EW
803 if ($log->{state} eq 'msg') {
804 if ($ret->{lines}) {
805 $ret->{msg} .= $_."\n";
806 unless(--$ret->{lines}) {
807 $log->{state} = 'sep';
ce6f3519
EW
808 }
809 } else {
810 croak "Log parse error at: $_\n",
03823184 811 $ret->{revision},
ce6f3519
EW
812 "\n";
813 }
814 next;
815 }
03823184 816 if ($log->{state} ne 'sep') {
ce6f3519 817 croak "Log parse error at: $_\n",
03823184
EW
818 "state: $log->{state}\n",
819 $ret->{revision},
ce6f3519
EW
820 "\n";
821 }
03823184 822 $log->{state} = 'rev';
3397f9df
EW
823
824 # if we have an empty log message, put something there:
03823184
EW
825 if ($ret) {
826 $ret->{msg} ||= "\n";
827 delete $ret->{lines};
828 return $ret;
3397f9df
EW
829 }
830 next;
831 }
03823184 832 if ($log->{state} eq 'rev' && s/^r(\d+)\s*\|\s*//) {
3397f9df 833 my $rev = $1;
ce6f3519
EW
834 my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
835 ($lines) = ($lines =~ /(\d+)/);
3397f9df
EW
836 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
837 /(\d{4})\-(\d\d)\-(\d\d)\s
838 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
839 or croak "Failed to parse date: $date\n";
03823184 840 $ret = { revision => $rev,
3397f9df
EW
841 date => "$tz $Y-$m-$d $H:$M:$S",
842 author => $author,
ce6f3519 843 lines => $lines,
03823184 844 msg => '' };
a9612be2
EW
845 if (defined $_authors && ! defined $users{$author}) {
846 die "Author: $author not defined in ",
847 "$_authors file\n";
848 }
03823184 849 $log->{state} = 'msg_start';
3397f9df
EW
850 next;
851 }
852 # skip the first blank line of the message:
03823184
EW
853 if ($log->{state} eq 'msg_start' && /^$/) {
854 $log->{state} = 'msg';
855 } elsif ($log->{state} eq 'msg') {
856 if ($ret->{lines}) {
857 $ret->{msg} .= $_."\n";
858 unless (--$ret->{lines}) {
859 $log->{state} = 'sep';
ce6f3519
EW
860 }
861 } else {
862 croak "Log parse error at: $_\n",
03823184 863 $ret->{revision},"\n";
ce6f3519 864 }
3397f9df
EW
865 }
866 }
03823184 867 return $ret;
3397f9df
EW
868}
869
870sub svn_info {
871 my $url = shift || $SVN_URL;
872
873 my $pid = open my $info_fh, '-|';
874 defined $pid or croak $!;
875
876 if ($pid == 0) {
877 exec(qw(svn info),$url) or croak $!;
878 }
879
880 my $ret = {};
881 # only single-lines seem to exist in svn info output
882 while (<$info_fh>) {
883 chomp $_;
e17512f3 884 if (m#^([^:]+)\s*:\s*(\S.*)$#) {
3397f9df
EW
885 $ret->{$1} = $2;
886 push @{$ret->{-order}}, $1;
887 }
888 }
889 close $info_fh or croak $!;
890 return $ret;
891}
892
893sub sys { system(@_) == 0 or croak $? }
894
36f5b1f0
EW
895sub eol_cp {
896 my ($from, $to) = @_;
8a97e368 897 my $es = svn_propget_base('svn:eol-style', $to);
36f5b1f0
EW
898 open my $rfd, '<', $from or croak $!;
899 binmode $rfd or croak $!;
900 open my $wfd, '>', $to or croak $!;
901 binmode $wfd or croak $!;
902
903 my $eol = $EOL{$es} or undef;
36f5b1f0 904 my $buf;
4a393f2b 905 use bytes;
36f5b1f0
EW
906 while (1) {
907 my ($r, $w, $t);
908 defined($r = sysread($rfd, $buf, 4096)) or croak $!;
909 return unless $r;
4a393f2b
EW
910 if ($eol) {
911 if ($buf =~ /\015$/) {
912 my $c;
913 defined($r = sysread($rfd,$c,1)) or croak $!;
914 $buf .= $c if $r > 0;
915 }
916 $buf =~ s/(?:\015\012|\015|\012)/$eol/gs;
917 $r = length($buf);
918 }
36f5b1f0
EW
919 for ($w = 0; $w < $r; $w += $t) {
920 $t = syswrite($wfd, $buf, $r - $w, $w) or croak $!;
921 }
922 }
4a393f2b 923 no bytes;
36f5b1f0
EW
924}
925
926sub do_update_index {
927 my ($z_cmd, $cmd, $no_text_base) = @_;
928
929 my $z = open my $p, '-|';
930 defined $z or croak $!;
931 unless ($z) { exec @$z_cmd or croak $! }
932
933 my $pid = open my $ui, '|-';
934 defined $pid or croak $!;
935 unless ($pid) {
936 exec('git-update-index',"--$cmd",'-z','--stdin') or croak $!;
937 }
938 local $/ = "\0";
939 while (my $x = <$p>) {
940 chomp $x;
941 if (!$no_text_base && lstat $x && ! -l _ &&
8a97e368 942 svn_propget_base('svn:keywords', $x)) {
36f5b1f0
EW
943 my $mode = -x _ ? 0755 : 0644;
944 my ($v,$d,$f) = File::Spec->splitpath($x);
945 my $tb = File::Spec->catfile($d, '.svn', 'tmp',
946 'text-base',"$f.svn-base");
947 $tb =~ s#^/##;
948 unless (-f $tb) {
949 $tb = File::Spec->catfile($d, '.svn',
950 'text-base',"$f.svn-base");
951 $tb =~ s#^/##;
952 }
953 unlink $x or croak $!;
954 eol_cp($tb, $x);
955 chmod(($mode &~ umask), $x) or croak $!;
956 }
957 print $ui $x,"\0";
958 }
959 close $ui or croak $!;
960}
961
962sub index_changes {
963 my $no_text_base = shift;
964 do_update_index([qw/git-diff-files --name-only -z/],
965 'remove',
966 $no_text_base);
967 do_update_index([qw/git-ls-files -z --others/,
968 "--exclude-from=$GIT_DIR/$GIT_SVN/info/exclude"],
969 'add',
970 $no_text_base);
3397f9df
EW
971}
972
973sub s_to_file {
974 my ($str, $file, $mode) = @_;
975 open my $fd,'>',$file or croak $!;
976 print $fd $str,"\n" or croak $!;
977 close $fd or croak $!;
978 chmod ($mode &~ umask, $file) if (defined $mode);
979}
980
981sub file_to_s {
982 my $file = shift;
983 open my $fd,'<',$file or croak "$!: file: $file\n";
984 local $/;
985 my $ret = <$fd>;
986 close $fd or croak $!;
987 $ret =~ s/\s*$//s;
988 return $ret;
989}
990
991sub assert_revision_unknown {
992 my $revno = shift;
993 if (-f "$REV_DIR/$revno") {
994 croak "$REV_DIR/$revno already exists! ",
995 "Why are we refetching it?";
996 }
997}
998
ac749050
EW
999sub trees_eq {
1000 my ($x, $y) = @_;
1001 my @x = safe_qx('git-cat-file','commit',$x);
1002 my @y = safe_qx('git-cat-file','commit',$y);
1003 if (($y[0] ne $x[0]) || $x[0] !~ /^tree $sha1\n$/
1004 || $y[0] !~ /^tree $sha1\n$/) {
1005 print STDERR "Trees not equal: $y[0] != $x[0]\n";
1006 return 0
1007 }
1008 return 1;
1009}
1010
3397f9df
EW
1011sub assert_revision_eq_or_unknown {
1012 my ($revno, $commit) = @_;
1013 if (-f "$REV_DIR/$revno") {
1014 my $current = file_to_s("$REV_DIR/$revno");
ac749050 1015 if (($commit ne $current) && !trees_eq($commit, $current)) {
3397f9df
EW
1016 croak "$REV_DIR/$revno already exists!\n",
1017 "current: $current\nexpected: $commit\n";
1018 }
1019 return;
1020 }
1021}
1022
1023sub git_commit {
1024 my ($log_msg, @parents) = @_;
1025 assert_revision_unknown($log_msg->{revision});
1026 my $out_fh = IO::File->new_tmpfile or croak $!;
3397f9df 1027
69f0d91e
EW
1028 map_tree_joins() if (@_branch_from && !%tree_map);
1029
3397f9df
EW
1030 # commit parents can be conditionally bound to a particular
1031 # svn revision via: "svn_revno=commit_sha1", filter them out here:
1032 my @exec_parents;
1033 foreach my $p (@parents) {
1034 next unless defined $p;
1035 if ($p =~ /^(\d+)=($sha1_short)$/o) {
1036 if ($1 == $log_msg->{revision}) {
1037 push @exec_parents, $2;
1038 }
1039 } else {
1040 push @exec_parents, $p if $p =~ /$sha1_short/o;
1041 }
1042 }
1043
1044 my $pid = fork;
1045 defined $pid or croak $!;
1046 if ($pid == 0) {
1047 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
36f5b1f0 1048 index_changes();
3397f9df
EW
1049 chomp(my $tree = `git-write-tree`);
1050 croak if $?;
69f0d91e
EW
1051 if (exists $tree_map{$tree}) {
1052 my %seen_parent = map { $_ => 1 } @exec_parents;
1053 foreach (@{$tree_map{$tree}}) {
1054 # MAXPARENT is defined to 16 in commit-tree.c:
1055 if ($seen_parent{$_} || @exec_parents > 16) {
1056 next;
1057 }
1058 push @exec_parents, $_;
1059 $seen_parent{$_} = 1;
1060 }
1061 }
3397f9df
EW
1062 my $msg_fh = IO::File->new_tmpfile or croak $!;
1063 print $msg_fh $log_msg->{msg}, "\ngit-svn-id: ",
1064 "$SVN_URL\@$log_msg->{revision}",
1ca72aef 1065 " $SVN_UUID\n" or croak $!;
3397f9df
EW
1066 $msg_fh->flush == 0 or croak $!;
1067 seek $msg_fh, 0, 0 or croak $!;
1068
1ca72aef 1069 set_commit_env($log_msg);
a9612be2 1070
3397f9df
EW
1071 my @exec = ('git-commit-tree',$tree);
1072 push @exec, '-p', $_ foreach @exec_parents;
1073 open STDIN, '<&', $msg_fh or croak $!;
1074 open STDOUT, '>&', $out_fh or croak $!;
1075 exec @exec or croak $!;
1076 }
1077 waitpid($pid,0);
1078 croak if $?;
1079
1080 $out_fh->flush == 0 or croak $!;
1081 seek $out_fh, 0, 0 or croak $!;
1082 chomp(my $commit = do { local $/; <$out_fh> });
1083 if ($commit !~ /^$sha1$/o) {
1084 croak "Failed to commit, invalid sha1: $commit\n";
1085 }
2beb3cdd 1086 my @update_ref = ('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
3397f9df 1087 if (my $primary_parent = shift @exec_parents) {
1d52aba8
EW
1088 $pid = fork;
1089 defined $pid or croak $!;
1090 if (!$pid) {
1091 close STDERR;
1092 close STDOUT;
1093 exec 'git-rev-parse','--verify',
1094 "refs/remotes/$GIT_SVN^0";
1095 }
1096 waitpid $pid, 0;
1097 push @update_ref, $primary_parent unless $?;
3397f9df
EW
1098 }
1099 sys(@update_ref);
1100 sys('git-update-ref',"$GIT_SVN/revs/$log_msg->{revision}",$commit);
1101 print "r$log_msg->{revision} = $commit\n";
1102 return $commit;
1103}
1104
a9612be2 1105sub set_commit_env {
1ca72aef 1106 my ($log_msg) = @_;
a9612be2
EW
1107 my $author = $log_msg->{author};
1108 my ($name,$email) = defined $users{$author} ? @{$users{$author}}
1ca72aef 1109 : ($author,"$author\@$SVN_UUID");
a9612be2
EW
1110 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $name;
1111 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} = $email;
1112 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_msg->{date};
1113}
1114
cf52b8f0
EW
1115sub apply_mod_line_blob {
1116 my $m = shift;
1117 if ($m->{mode_b} =~ /^120/) {
1118 blob_to_symlink($m->{sha1_b}, $m->{file_b});
1119 } else {
1120 blob_to_file($m->{sha1_b}, $m->{file_b});
1121 }
1122}
1123
3397f9df
EW
1124sub blob_to_symlink {
1125 my ($blob, $link) = @_;
1126 defined $link or croak "\$link not defined!\n";
1127 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
cf52b8f0
EW
1128 if (-l $link || -f _) {
1129 unlink $link or croak $!;
1130 }
1131
3397f9df
EW
1132 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
1133 symlink $dest, $link or croak $!;
1134}
1135
1136sub blob_to_file {
1137 my ($blob, $file) = @_;
1138 defined $file or croak "\$file not defined!\n";
1139 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
cf52b8f0
EW
1140 if (-l $file || -f _) {
1141 unlink $file or croak $!;
1142 }
1143
3397f9df
EW
1144 open my $blob_fh, '>', $file or croak "$!: $file\n";
1145 my $pid = fork;
1146 defined $pid or croak $!;
1147
1148 if ($pid == 0) {
1149 open STDOUT, '>&', $blob_fh or croak $!;
1150 exec('git-cat-file','blob',$blob);
1151 }
1152 waitpid $pid, 0;
1153 croak $? if $?;
1154
1155 close $blob_fh or croak $!;
1156}
1157
1158sub safe_qx {
1159 my $pid = open my $child, '-|';
1160 defined $pid or croak $!;
1161 if ($pid == 0) {
1162 exec(@_) or croak $?;
1163 }
1164 my @ret = (<$child>);
1165 close $child or croak $?;
1166 die $? if $?; # just in case close didn't error out
1167 return wantarray ? @ret : join('',@ret);
1168}
1169
1d52aba8
EW
1170sub svn_compat_check {
1171 my @co_help = safe_qx(qw(svn co -h));
1172 unless (grep /ignore-externals/,@co_help) {
3397f9df
EW
1173 print STDERR "W: Installed svn version does not support ",
1174 "--ignore-externals\n";
1175 $_no_ignore_ext = 1;
1176 }
1d52aba8
EW
1177 if (grep /usage: checkout URL\[\@REV\]/,@co_help) {
1178 $_svn_co_url_revs = 1;
1179 }
8a97e368
EW
1180 if (grep /\[TARGET\[\@REV\]\.\.\.\]/, `svn propget -h`) {
1181 $_svn_pg_peg_revs = 1;
1182 }
7317ed90
EW
1183
1184 # I really, really hope nobody hits this...
1185 unless (grep /stop-on-copy/, (safe_qx(qw(svn log -h)))) {
1186 print STDERR <<'';
1187W: The installed svn version does not support the --stop-on-copy flag in
1188 the log command.
1189 Lets hope the directory you're tracking is not a branch or tag
1190 and was never moved within the repository...
1191
1192 $_no_stop_copy = 1;
1193 }
1d52aba8
EW
1194}
1195
1196# *sigh*, new versions of svn won't honor -r<rev> without URL@<rev>,
1197# (and they won't honor URL@<rev> without -r<rev>, too!)
1198sub svn_cmd_checkout {
1199 my ($url, $rev, $dir) = @_;
1200 my @cmd = ('svn','co', "-r$rev");
1201 push @cmd, '--ignore-externals' unless $_no_ignore_ext;
1202 $url .= "\@$rev" if $_svn_co_url_revs;
1203 sys(@cmd, $url, $dir);
3397f9df 1204}
2beb3cdd
EW
1205
1206sub check_upgrade_needed {
1207 my $old = eval {
1208 my $pid = open my $child, '-|';
1209 defined $pid or croak $!;
1210 if ($pid == 0) {
1211 close STDERR;
1212 exec('git-rev-parse',"$GIT_SVN-HEAD") or croak $?;
1213 }
1214 my @ret = (<$child>);
1215 close $child or croak $?;
1216 die $? if $?; # just in case close didn't error out
1217 return wantarray ? @ret : join('',@ret);
1218 };
1219 return unless $old;
1220 my $head = eval { safe_qx('git-rev-parse',"refs/remotes/$GIT_SVN") };
1221 if ($@ || !$head) {
1222 print STDERR "Please run: $0 rebuild --upgrade\n";
1223 exit 1;
1224 }
1225}
1226
69f0d91e
EW
1227# fills %tree_map with a reverse mapping of trees to commits. Useful
1228# for finding parents to commit on.
1229sub map_tree_joins {
098749d9 1230 my %seen;
69f0d91e
EW
1231 foreach my $br (@_branch_from) {
1232 my $pid = open my $pipe, '-|';
1233 defined $pid or croak $!;
1234 if ($pid == 0) {
098749d9
EW
1235 exec(qw(git-rev-list --topo-order --pretty=raw), $br)
1236 or croak $?;
69f0d91e
EW
1237 }
1238 while (<$pipe>) {
1239 if (/^commit ($sha1)$/o) {
1240 my $commit = $1;
098749d9
EW
1241
1242 # if we've seen a commit,
1243 # we've seen its parents
1244 last if $seen{$commit};
69f0d91e
EW
1245 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
1246 unless (defined $tree) {
1247 die "Failed to parse commit $commit\n";
1248 }
1249 push @{$tree_map{$tree}}, $commit;
098749d9 1250 $seen{$commit} = 1;
69f0d91e
EW
1251 }
1252 }
098749d9 1253 close $pipe; # we could be breaking the pipe early
69f0d91e
EW
1254 }
1255}
1256
bf78b1d8
EW
1257sub load_all_refs {
1258 if (@_branch_from) {
1259 print STDERR '--branch|-b parameters are ignored when ',
1260 "--branch-all-refs|-B is passed\n";
1261 }
1262
1263 # don't worry about rev-list on non-commit objects/tags,
1264 # it shouldn't blow up if a ref is a blob or tree...
1265 chomp(@_branch_from = `git-rev-parse --symbolic --all`);
1266}
1267
eeb0abe0
EW
1268# '<svn username> = real-name <email address>' mapping based on git-svnimport:
1269sub load_authors {
1270 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1271 while (<$authors>) {
1272 chomp;
1273 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
1274 my ($user, $name, $email) = ($1, $2, $3);
1275 $users{$user} = [$name, $email];
1276 }
1277 close $authors or croak $!;
1278}
1279
8a97e368
EW
1280sub svn_propget_base {
1281 my ($p, $f) = @_;
1282 $f .= '@BASE' if $_svn_pg_peg_revs;
1283 return safe_qx(qw/svn propget/, $p, $f);
1284}
1285
3397f9df
EW
1286__END__
1287
1288Data structures:
1289
03823184
EW
1290$svn_log hashref (as returned by svn_log_raw)
1291{
1292 fh => file handle of the log file,
1293 state => state of the log file parser (sep/msg/rev/msg_start...)
1294}
3397f9df 1295
03823184 1296$log_msg hashref as returned by next_log_entry($svn_log)
3397f9df
EW
1297{
1298 msg => 'whitespace-formatted log entry
1299', # trailing newline is preserved
1300 revision => '8', # integer
1301 date => '2004-02-24T17:01:44.108345Z', # commit date
1302 author => 'committer name'
1303};
1304
1305
1306@mods = array of diff-index line hashes, each element represents one line
1307 of diff-index output
1308
1309diff-index line ($m hash)
1310{
1311 mode_a => first column of diff-index output, no leading ':',
1312 mode_b => second column of diff-index output,
1313 sha1_b => sha1sum of the final blob,
ac8e0b91 1314 chg => change type [MCRADT],
3397f9df
EW
1315 file_a => original file name of a file (iff chg is 'C' or 'R')
1316 file_b => new/current file name of a file (any chg)
1317}
1318;