contrib/git-svn: several small bug fixes and changes
[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
7 $SVN_URL $SVN_INFO $SVN_WC
8 $GIT_SVN_INDEX $GIT_SVN
9 $GIT_DIR $REV_DIR/;
10$AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
3c0b7511 11$VERSION = '0.10.0';
3397f9df
EW
12$GIT_DIR = $ENV{GIT_DIR} || "$ENV{PWD}/.git";
13$GIT_SVN = $ENV{GIT_SVN_ID} || 'git-svn';
14$GIT_SVN_INDEX = "$GIT_DIR/$GIT_SVN/index";
15$ENV{GIT_DIR} ||= $GIT_DIR;
16$SVN_URL = undef;
17$REV_DIR = "$GIT_DIR/$GIT_SVN/revs";
18$SVN_WC = "$GIT_DIR/$GIT_SVN/tree";
19
20# make sure the svn binary gives consistent output between locales and TZs:
21$ENV{TZ} = 'UTC';
22$ENV{LC_ALL} = 'C';
23
24# If SVN:: library support is added, please make the dependencies
25# optional and preserve the capability to use the command-line client.
ce6f3519 26# use eval { require SVN::... } to make it lazy load
3397f9df
EW
27use Carp qw/croak/;
28use IO::File qw//;
29use File::Basename qw/dirname basename/;
30use File::Path qw/mkpath/;
31use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
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,
2beb3cdd 37 $_find_copies_harder, $_l, $_version, $_upgrade);
69f0d91e 38my (@_branch_from, %tree_map);
3397f9df
EW
39
40GetOptions( 'revision|r=s' => \$_revision,
41 'no-ignore-externals' => \$_no_ignore_ext,
42 'stdin|' => \$_stdin,
43 'edit|e' => \$_edit,
44 'rmdir' => \$_rmdir,
2beb3cdd 45 'upgrade' => \$_upgrade,
3397f9df 46 'help|H|h' => \$_help,
69f0d91e 47 'branch|b=s' => \@_branch_from,
72942938
EW
48 'find-copies-harder' => \$_find_copies_harder,
49 'l=i' => \$_l,
551ce28f 50 'version|V' => \$_version,
a18b6327 51 'no-stop-on-copy' => \$_no_stop_copy );
3397f9df
EW
52my %cmd = (
53 fetch => [ \&fetch, "Download new revisions from SVN" ],
54 init => [ \&init, "Initialize and fetch (import)"],
55 commit => [ \&commit, "Commit git revisions to SVN" ],
8f22562c 56 'show-ignore' => [ \&show_ignore, "Show svn:ignore listings" ],
3397f9df
EW
57 rebuild => [ \&rebuild, "Rebuild git-svn metadata (after git clone)" ],
58 help => [ \&usage, "Show help" ],
59);
60my $cmd;
61for (my $i = 0; $i < @ARGV; $i++) {
62 if (defined $cmd{$ARGV[$i]}) {
63 $cmd = $ARGV[$i];
64 splice @ARGV, $i, 1;
65 last;
66 }
67};
68
69# we may be called as git-svn-(command), or git-svn(command).
70foreach (keys %cmd) {
71 if (/git\-svn\-?($_)(?:\.\w+)?$/) {
72 $cmd = $1;
73 last;
74 }
75}
76usage(0) if $_help;
551ce28f 77version() if $_version;
3397f9df
EW
78usage(1) unless (defined $cmd);
79svn_check_ignore_externals();
80$cmd{$cmd}->[0]->(@ARGV);
81exit 0;
82
83####################### primary functions ######################
84sub usage {
85 my $exit = shift || 0;
86 my $fd = $exit ? \*STDERR : \*STDOUT;
87 print $fd <<"";
88git-svn - bidirectional operations between a single Subversion tree and git
89Usage: $0 <command> [options] [arguments]\n
90Available commands:
91
92 foreach (sort keys %cmd) {
ac8e0b91 93 print $fd ' ',pack('A13',$_),$cmd{$_}->[1],"\n";
3397f9df
EW
94 }
95 print $fd <<"";
96\nGIT_SVN_ID may be set in the environment to an arbitrary identifier if
97you're tracking multiple SVN branches/repositories in one git repository
ac8e0b91 98and want to keep them separate. See git-svn(1) for more information.
3397f9df
EW
99
100 exit $exit;
101}
102
551ce28f
EW
103sub version {
104 print "git-svn version $VERSION\n";
105 exit 0;
106}
107
3397f9df
EW
108sub rebuild {
109 $SVN_URL = shift or undef;
110 my $repo_uuid;
111 my $newest_rev = 0;
2beb3cdd
EW
112 if ($_upgrade) {
113 sys('git-update-ref',"refs/remotes/$GIT_SVN","$GIT_SVN-HEAD");
114 } else {
115 check_upgrade_needed();
116 }
3397f9df
EW
117
118 my $pid = open(my $rev_list,'-|');
119 defined $pid or croak $!;
120 if ($pid == 0) {
2beb3cdd 121 exec("git-rev-list","refs/remotes/$GIT_SVN") or croak $!;
3397f9df 122 }
2beb3cdd 123 my $latest;
3397f9df
EW
124 while (<$rev_list>) {
125 chomp;
126 my $c = $_;
127 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
128 my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
129 next if (!@commit); # skip merges
130 my $id = $commit[$#commit];
131 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
132 \s([a-f\d\-]+)$/x);
133 if (!$rev || !$uuid || !$url) {
134 # some of the original repositories I made had
135 # indentifiers like this:
136 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)
137 \@([a-f\d\-]+)/x);
138 if (!$rev || !$uuid) {
139 croak "Unable to extract revision or UUID from ",
140 "$c, $id\n";
141 }
142 }
2beb3cdd
EW
143
144 # if we merged or otherwise started elsewhere, this is
145 # how we break out of it
146 next if (defined $repo_uuid && ($uuid ne $repo_uuid));
147 next if (defined $SVN_URL && ($url ne $SVN_URL));
148
3397f9df 149 print "r$rev = $c\n";
2beb3cdd 150 unless (defined $latest) {
3397f9df
EW
151 if (!$SVN_URL && !$url) {
152 croak "SVN repository location required: $url\n";
153 }
154 $SVN_URL ||= $url;
2beb3cdd
EW
155 $repo_uuid ||= setup_git_svn();
156 $latest = $rev;
3397f9df
EW
157 }
158 assert_revision_eq_or_unknown($rev, $c);
159 sys('git-update-ref',"$GIT_SVN/revs/$rev",$c);
160 $newest_rev = $rev if ($rev > $newest_rev);
161 }
162 close $rev_list or croak $?;
163 if (!chdir $SVN_WC) {
2beb3cdd 164 my @svn_co = ('svn','co',"-r$latest");
3397f9df
EW
165 push @svn_co, '--ignore-externals' unless $_no_ignore_ext;
166 sys(@svn_co, $SVN_URL, $SVN_WC);
167 chdir $SVN_WC or croak $!;
168 }
169
170 $pid = fork;
171 defined $pid or croak $!;
172 if ($pid == 0) {
173 my @svn_up = qw(svn up);
174 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
175 sys(@svn_up,"-r$newest_rev");
176 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
177 git_addremove();
178 exec('git-write-tree');
179 }
180 waitpid $pid, 0;
2beb3cdd
EW
181
182 if ($_upgrade) {
183 print STDERR <<"";
184Keeping deprecated refs/head/$GIT_SVN-HEAD for now. Please remove it
185when you have upgraded your tools and habits to use refs/remotes/$GIT_SVN
186
187 }
3397f9df
EW
188}
189
190sub init {
191 $SVN_URL = shift or croak "SVN repository location required\n";
192 unless (-d $GIT_DIR) {
193 sys('git-init-db');
194 }
195 setup_git_svn();
196}
197
198sub fetch {
199 my (@parents) = @_;
2beb3cdd 200 check_upgrade_needed();
3397f9df
EW
201 $SVN_URL ||= file_to_s("$GIT_DIR/$GIT_SVN/info/url");
202 my @log_args = -d $SVN_WC ? ($SVN_WC) : ($SVN_URL);
defc6492
EW
203 unless ($_revision) {
204 $_revision = -d $SVN_WC ? 'BASE:HEAD' : '0:HEAD';
3397f9df 205 }
defc6492 206 push @log_args, "-r$_revision";
3397f9df
EW
207 push @log_args, '--stop-on-copy' unless $_no_stop_copy;
208
ce6f3519 209 my $svn_log = svn_log_raw(@log_args);
defc6492 210 @$svn_log = sort { $a->{revision} <=> $b->{revision} } @$svn_log;
3397f9df
EW
211
212 my $base = shift @$svn_log or croak "No base revision!\n";
213 my $last_commit = undef;
214 unless (-d $SVN_WC) {
215 my @svn_co = ('svn','co',"-r$base->{revision}");
216 push @svn_co,'--ignore-externals' unless $_no_ignore_ext;
217 sys(@svn_co, $SVN_URL, $SVN_WC);
218 chdir $SVN_WC or croak $!;
219 $last_commit = git_commit($base, @parents);
220 unless (-f "$GIT_DIR/refs/heads/master") {
221 sys(qw(git-update-ref refs/heads/master),$last_commit);
222 }
223 assert_svn_wc_clean($base->{revision}, $last_commit);
224 } else {
225 chdir $SVN_WC or croak $!;
226 $last_commit = file_to_s("$REV_DIR/$base->{revision}");
227 }
228 my @svn_up = qw(svn up);
229 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
230 my $last_rev = $base->{revision};
231 foreach my $log_msg (@$svn_log) {
232 assert_svn_wc_clean($last_rev, $last_commit);
233 $last_rev = $log_msg->{revision};
234 sys(@svn_up,"-r$last_rev");
235 $last_commit = git_commit($log_msg, $last_commit, @parents);
236 }
237 assert_svn_wc_clean($last_rev, $last_commit);
238 return pop @$svn_log;
239}
240
241sub commit {
242 my (@commits) = @_;
2beb3cdd 243 check_upgrade_needed();
3397f9df
EW
244 if ($_stdin || !@commits) {
245 print "Reading from stdin...\n";
246 @commits = ();
247 while (<STDIN>) {
ac8e0b91 248 if (/\b($sha1_short)\b/) {
3397f9df
EW
249 unshift @commits, $1;
250 }
251 }
252 }
253 my @revs;
8de010ad
EW
254 foreach my $c (@commits) {
255 chomp(my @tmp = safe_qx('git-rev-parse',$c));
256 if (scalar @tmp == 1) {
257 push @revs, $tmp[0];
258 } elsif (scalar @tmp > 1) {
259 push @revs, reverse (safe_qx('git-rev-list',@tmp));
260 } else {
261 die "Failed to rev-parse $c\n";
262 }
3397f9df
EW
263 }
264 chomp @revs;
265
266 fetch();
267 chdir $SVN_WC or croak $!;
268 my $svn_current_rev = svn_info('.')->{'Last Changed Rev'};
269 foreach my $c (@revs) {
cf52b8f0
EW
270 my $mods = svn_checkout_tree($svn_current_rev, $c);
271 if (scalar @$mods == 0) {
272 print "Skipping, no changes detected\n";
273 next;
274 }
3397f9df
EW
275 $svn_current_rev = svn_commit_tree($svn_current_rev, $c);
276 }
277 print "Done committing ",scalar @revs," revisions to SVN\n";
278
279}
280
8f22562c
EW
281sub show_ignore {
282 require File::Find or die $!;
283 my $exclude_file = "$GIT_DIR/info/exclude";
284 open my $fh, '<', $exclude_file or croak $!;
285 chomp(my @excludes = (<$fh>));
286 close $fh or croak $!;
287
288 $SVN_URL ||= file_to_s("$GIT_DIR/$GIT_SVN/info/url");
289 chdir $SVN_WC or croak $!;
290 my %ign;
291 File::Find::find({wanted=>sub{if(lstat $_ && -d _ && -d "$_/.svn"){
292 s#^\./##;
293 @{$ign{$_}} = safe_qx(qw(svn propget svn:ignore),$_);
294 }}, no_chdir=>1},'.');
295
296 print "\n# /\n";
297 foreach (@{$ign{'.'}}) { print '/',$_ if /\S/ }
298 delete $ign{'.'};
299 foreach my $i (sort keys %ign) {
300 print "\n# ",$i,"\n";
301 foreach (@{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ }
302 }
303}
304
3397f9df
EW
305########################### utility functions #########################
306
307sub setup_git_svn {
308 defined $SVN_URL or croak "SVN repository location required\n";
309 unless (-d $GIT_DIR) {
310 croak "GIT_DIR=$GIT_DIR does not exist!\n";
311 }
312 mkpath(["$GIT_DIR/$GIT_SVN"]);
313 mkpath(["$GIT_DIR/$GIT_SVN/info"]);
314 mkpath([$REV_DIR]);
315 s_to_file($SVN_URL,"$GIT_DIR/$GIT_SVN/info/url");
316 my $uuid = svn_info($SVN_URL)->{'Repository UUID'} or
317 croak "Repository UUID unreadable\n";
318 s_to_file($uuid,"$GIT_DIR/$GIT_SVN/info/uuid");
319
320 open my $fd, '>>', "$GIT_DIR/$GIT_SVN/info/exclude" or croak $!;
321 print $fd '.svn',"\n";
322 close $fd or croak $!;
323 return $uuid;
324}
325
326sub assert_svn_wc_clean {
cf52b8f0 327 my ($svn_rev, $treeish) = @_;
3397f9df 328 croak "$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
cf52b8f0 329 croak "$treeish is not a sha1!\n" unless ($treeish =~ /^$sha1$/o);
3397f9df
EW
330 my $svn_info = svn_info('.');
331 if ($svn_rev != $svn_info->{'Last Changed Rev'}) {
332 croak "Expected r$svn_rev, got r",
333 $svn_info->{'Last Changed Rev'},"\n";
334 }
335 my @status = grep(!/^Performing status on external/,(`svn status`));
336 @status = grep(!/^\s*$/,@status);
337 if (scalar @status) {
338 print STDERR "Tree ($SVN_WC) is not clean:\n";
339 print STDERR $_ foreach @status;
340 croak;
341 }
cf52b8f0
EW
342 assert_tree($treeish);
343}
344
345sub assert_tree {
346 my ($treeish) = @_;
347 croak "Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
348 chomp(my $type = `git-cat-file -t $treeish`);
349 my $expected;
350 while ($type eq 'tag') {
351 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
352 }
353 if ($type eq 'commit') {
354 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
355 ($expected) = ($expected =~ /^tree ($sha1)$/);
356 die "Unable to get tree from $treeish\n" unless $expected;
357 } elsif ($type eq 'tree') {
358 $expected = $treeish;
359 } else {
360 die "$treeish is a $type, expected tree, tag or commit\n";
361 }
362
363 my $old_index = $ENV{GIT_INDEX_FILE};
364 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
365 if (-e $tmpindex) {
366 unlink $tmpindex or croak $!;
367 }
368 $ENV{GIT_INDEX_FILE} = $tmpindex;
369 git_addremove();
370 chomp(my $tree = `git-write-tree`);
371 if ($old_index) {
372 $ENV{GIT_INDEX_FILE} = $old_index;
373 } else {
374 delete $ENV{GIT_INDEX_FILE};
375 }
376 if ($tree ne $expected) {
377 croak "Tree mismatch, Got: $tree, Expected: $expected\n";
3397f9df
EW
378 }
379}
380
381sub parse_diff_tree {
382 my $diff_fh = shift;
383 local $/ = "\0";
384 my $state = 'meta';
385 my @mods;
386 while (<$diff_fh>) {
387 chomp $_; # this gets rid of the trailing "\0"
3397f9df
EW
388 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
389 $sha1\s($sha1)\s([MTCRAD])\d*$/xo) {
390 push @mods, { mode_a => $1, mode_b => $2,
391 sha1_b => $3, chg => $4 };
392 if ($4 =~ /^(?:C|R)$/) {
393 $state = 'file_a';
394 } else {
395 $state = 'file_b';
396 }
397 } elsif ($state eq 'file_a') {
cf52b8f0 398 my $x = $mods[$#mods] or croak "Empty array\n";
3397f9df 399 if ($x->{chg} !~ /^(?:C|R)$/) {
cf52b8f0 400 croak "Error parsing $_, $x->{chg}\n";
3397f9df
EW
401 }
402 $x->{file_a} = $_;
403 $state = 'file_b';
404 } elsif ($state eq 'file_b') {
cf52b8f0 405 my $x = $mods[$#mods] or croak "Empty array\n";
3397f9df 406 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
cf52b8f0 407 croak "Error parsing $_, $x->{chg}\n";
3397f9df
EW
408 }
409 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
cf52b8f0 410 croak "Error parsing $_, $x->{chg}\n";
3397f9df
EW
411 }
412 $x->{file_b} = $_;
413 $state = 'meta';
414 } else {
cf52b8f0 415 croak "Error parsing $_\n";
3397f9df
EW
416 }
417 }
418 close $diff_fh or croak $!;
cf52b8f0 419
3397f9df
EW
420 return \@mods;
421}
422
423sub svn_check_prop_executable {
424 my $m = shift;
cf52b8f0
EW
425 return if -l $m->{file_b};
426 if ($m->{mode_b} =~ /755$/) {
427 chmod((0755 &~ umask),$m->{file_b}) or croak $!;
428 if ($m->{mode_a} !~ /755$/) {
429 sys(qw(svn propset svn:executable 1), $m->{file_b});
430 }
431 -x $m->{file_b} or croak "$m->{file_b} is not executable!\n";
3397f9df
EW
432 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
433 sys(qw(svn propdel svn:executable), $m->{file_b});
cf52b8f0
EW
434 chmod((0644 &~ umask),$m->{file_b}) or croak $!;
435 -x $m->{file_b} and croak "$m->{file_b} is executable!\n";
3397f9df
EW
436 }
437}
438
439sub svn_ensure_parent_path {
440 my $dir_b = dirname(shift);
441 svn_ensure_parent_path($dir_b) if ($dir_b ne File::Spec->curdir);
442 mkpath([$dir_b]) unless (-d $dir_b);
443 sys(qw(svn add -N), $dir_b) unless (-d "$dir_b/.svn");
444}
445
cf52b8f0
EW
446sub precommit_check {
447 my $mods = shift;
448 my (%rm_file, %rmdir_check, %added_check);
449
450 my %o = ( D => 0, R => 1, C => 2, A => 3, M => 3, T => 3 );
451 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
452 if ($m->{chg} eq 'R') {
453 if (-d $m->{file_b}) {
454 err_dir_to_file("$m->{file_a} => $m->{file_b}");
455 }
456 # dir/$file => dir/file/$file
457 my $dirname = dirname($m->{file_b});
458 while ($dirname ne File::Spec->curdir) {
459 if ($dirname ne $m->{file_a}) {
460 $dirname = dirname($dirname);
461 next;
462 }
463 err_file_to_dir("$m->{file_a} => $m->{file_b}");
464 }
465 # baz/zzz => baz (baz is a file)
466 $dirname = dirname($m->{file_a});
467 while ($dirname ne File::Spec->curdir) {
468 if ($dirname ne $m->{file_b}) {
469 $dirname = dirname($dirname);
470 next;
471 }
472 err_dir_to_file("$m->{file_a} => $m->{file_b}");
473 }
474 }
475 if ($m->{chg} =~ /^(D|R)$/) {
476 my $t = $1 eq 'D' ? 'file_b' : 'file_a';
477 $rm_file{ $m->{$t} } = 1;
478 my $dirname = dirname( $m->{$t} );
479 my $basename = basename( $m->{$t} );
480 $rmdir_check{$dirname}->{$basename} = 1;
481 } elsif ($m->{chg} =~ /^(?:A|C)$/) {
482 if (-d $m->{file_b}) {
483 err_dir_to_file($m->{file_b});
484 }
485 my $dirname = dirname( $m->{file_b} );
486 my $basename = basename( $m->{file_b} );
487 $added_check{$dirname}->{$basename} = 1;
488 while ($dirname ne File::Spec->curdir) {
489 if ($rm_file{$dirname}) {
490 err_file_to_dir($m->{file_b});
491 }
492 $dirname = dirname $dirname;
493 }
494 }
495 }
496 return (\%rmdir_check, \%added_check);
497
498 sub err_dir_to_file {
499 my $file = shift;
500 print STDERR "Node change from directory to file ",
501 "is not supported by Subversion: ",$file,"\n";
502 exit 1;
503 }
504 sub err_file_to_dir {
505 my $file = shift;
506 print STDERR "Node change from file to directory ",
507 "is not supported by Subversion: ",$file,"\n";
508 exit 1;
509 }
510}
511
3397f9df 512sub svn_checkout_tree {
cf52b8f0 513 my ($svn_rev, $treeish) = @_;
3397f9df
EW
514 my $from = file_to_s("$REV_DIR/$svn_rev");
515 assert_svn_wc_clean($svn_rev,$from);
ac8e0b91 516 print "diff-tree $from $treeish\n";
3397f9df
EW
517 my $pid = open my $diff_fh, '-|';
518 defined $pid or croak $!;
519 if ($pid == 0) {
72942938
EW
520 my @diff_tree = qw(git-diff-tree -z -r -C);
521 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
522 push @diff_tree, "-l$_l" if defined $_l;
cf52b8f0 523 exec(@diff_tree, $from, $treeish) or croak $!;
3397f9df
EW
524 }
525 my $mods = parse_diff_tree($diff_fh);
526 unless (@$mods) {
ac8e0b91 527 # git can do empty commits, but SVN doesn't allow it...
cf52b8f0 528 return $mods;
3397f9df 529 }
cf52b8f0
EW
530 my ($rm, $add) = precommit_check($mods);
531
532 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
533 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
3397f9df
EW
534 if ($m->{chg} eq 'C') {
535 svn_ensure_parent_path( $m->{file_b} );
536 sys(qw(svn cp), $m->{file_a}, $m->{file_b});
cf52b8f0 537 apply_mod_line_blob($m);
3397f9df
EW
538 svn_check_prop_executable($m);
539 } elsif ($m->{chg} eq 'D') {
3397f9df
EW
540 sys(qw(svn rm --force), $m->{file_b});
541 } elsif ($m->{chg} eq 'R') {
542 svn_ensure_parent_path( $m->{file_b} );
543 sys(qw(svn mv --force), $m->{file_a}, $m->{file_b});
cf52b8f0 544 apply_mod_line_blob($m);
3397f9df 545 svn_check_prop_executable($m);
3397f9df 546 } elsif ($m->{chg} eq 'M') {
cf52b8f0 547 apply_mod_line_blob($m);
3397f9df
EW
548 svn_check_prop_executable($m);
549 } elsif ($m->{chg} eq 'T') {
550 sys(qw(svn rm --force),$m->{file_b});
cf52b8f0 551 apply_mod_line_blob($m);
3397f9df 552 sys(qw(svn add --force), $m->{file_b});
cf52b8f0 553 svn_check_prop_executable($m);
3397f9df
EW
554 } elsif ($m->{chg} eq 'A') {
555 svn_ensure_parent_path( $m->{file_b} );
cf52b8f0 556 apply_mod_line_blob($m);
3397f9df 557 sys(qw(svn add --force), $m->{file_b});
cf52b8f0 558 svn_check_prop_executable($m);
3397f9df
EW
559 } else {
560 croak "Invalid chg: $m->{chg}\n";
561 }
562 }
cf52b8f0
EW
563
564 assert_tree($treeish);
565 if ($_rmdir) { # remove empty directories
566 handle_rmdir($rm, $add);
567 }
568 assert_tree($treeish);
569 return $mods;
570}
571
572# svn ls doesn't work with respect to the current working tree, but what's
573# in the repository. There's not even an option for it... *sigh*
574# (added files don't show up and removed files remain in the ls listing)
575sub svn_ls_current {
576 my ($dir, $rm, $add) = @_;
577 chomp(my @ls = safe_qx('svn','ls',$dir));
578 my @ret = ();
579 foreach (@ls) {
580 s#/$##; # trailing slashes are evil
581 push @ret, $_ unless $rm->{$dir}->{$_};
582 }
583 if (exists $add->{$dir}) {
584 push @ret, keys %{$add->{$dir}};
585 }
586 return \@ret;
587}
588
589sub handle_rmdir {
590 my ($rm, $add) = @_;
591
592 foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
593 my $ls = svn_ls_current($dir, $rm, $add);
594 next if (scalar @$ls);
595 sys(qw(svn rm --force),$dir);
596
597 my $dn = dirname $dir;
598 $rm->{ $dn }->{ basename $dir } = 1;
599 $ls = svn_ls_current($dn, $rm, $add);
600 while (scalar @$ls == 0 && $dn ne File::Spec->curdir) {
601 sys(qw(svn rm --force),$dn);
602 $dir = basename $dn;
603 $dn = dirname $dn;
604 $rm->{ $dn }->{ $dir } = 1;
605 $ls = svn_ls_current($dn, $rm, $add);
3397f9df
EW
606 }
607 }
608}
609
610sub svn_commit_tree {
611 my ($svn_rev, $commit) = @_;
612 my $commit_msg = "$GIT_DIR/$GIT_SVN/.svn-commit.tmp.$$";
e17512f3 613 my %log_msg = ( msg => '' );
ac8e0b91 614 open my $msg, '>', $commit_msg or croak $!;
3397f9df
EW
615
616 chomp(my $type = `git-cat-file -t $commit`);
617 if ($type eq 'commit') {
618 my $pid = open my $msg_fh, '-|';
619 defined $pid or croak $!;
620
621 if ($pid == 0) {
622 exec(qw(git-cat-file commit), $commit) or croak $!;
623 }
624 my $in_msg = 0;
625 while (<$msg_fh>) {
626 if (!$in_msg) {
627 $in_msg = 1 if (/^\s*$/);
628 } else {
629 print $msg $_ or croak $!;
630 }
631 }
632 close $msg_fh or croak $!;
633 }
634 close $msg or croak $!;
635
636 if ($_edit || ($type eq 'tree')) {
637 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
638 system($editor, $commit_msg);
639 }
ac8e0b91
EW
640
641 # file_to_s removes all trailing newlines, so just use chomp() here:
642 open $msg, '<', $commit_msg or croak $!;
643 { local $/; chomp($log_msg{msg} = <$msg>); }
644 close $msg or croak $!;
645
646 my ($oneline) = ($log_msg{msg} =~ /([^\n\r]+)/);
647 print "Committing $commit: $oneline\n";
648
3397f9df
EW
649 my @ci_output = safe_qx(qw(svn commit -F),$commit_msg);
650 my ($committed) = grep(/^Committed revision \d+\./,@ci_output);
651 unlink $commit_msg;
652 defined $committed or croak
653 "Commit output failed to parse committed revision!\n",
654 join("\n",@ci_output),"\n";
655 my ($rev_committed) = ($committed =~ /^Committed revision (\d+)\./);
656
e17512f3 657 my @svn_up = qw(svn up);
3397f9df 658 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
e17512f3
EW
659 if ($rev_committed == ($svn_rev + 1)) {
660 push @svn_up, "-r$rev_committed";
661 sys(@svn_up);
662 my $info = svn_info('.');
663 my $date = $info->{'Last Changed Date'} or die "Missing date\n";
664 if ($info->{'Last Changed Rev'} != $rev_committed) {
665 croak "$info->{'Last Changed Rev'} != $rev_committed\n"
666 }
667 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
668 /(\d{4})\-(\d\d)\-(\d\d)\s
669 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
670 or croak "Failed to parse date: $date\n";
671 $log_msg{date} = "$tz $Y-$m-$d $H:$M:$S";
672 $log_msg{author} = $info->{'Last Changed Author'};
673 $log_msg{revision} = $rev_committed;
674 $log_msg{msg} .= "\n";
675 my $parent = file_to_s("$REV_DIR/$svn_rev");
676 git_commit(\%log_msg, $parent, $commit);
677 return $rev_committed;
678 }
679 # resync immediately
680 push @svn_up, "-r$svn_rev";
3397f9df
EW
681 sys(@svn_up);
682 return fetch("$rev_committed=$commit")->{revision};
683}
684
3397f9df
EW
685sub svn_log_raw {
686 my (@log_args) = @_;
687 my $pid = open my $log_fh,'-|';
688 defined $pid or croak $!;
689
690 if ($pid == 0) {
691 exec (qw(svn log), @log_args) or croak $!
692 }
693
694 my @svn_log;
ce6f3519 695 my $state = 'sep';
3397f9df
EW
696 while (<$log_fh>) {
697 chomp;
698 if (/^\-{72}$/) {
ce6f3519
EW
699 if ($state eq 'msg') {
700 if ($svn_log[$#svn_log]->{lines}) {
701 $svn_log[$#svn_log]->{msg} .= $_."\n";
702 unless(--$svn_log[$#svn_log]->{lines}) {
703 $state = 'sep';
704 }
705 } else {
706 croak "Log parse error at: $_\n",
707 $svn_log[$#svn_log]->{revision},
708 "\n";
709 }
710 next;
711 }
712 if ($state ne 'sep') {
713 croak "Log parse error at: $_\n",
714 "state: $state\n",
715 $svn_log[$#svn_log]->{revision},
716 "\n";
717 }
3397f9df
EW
718 $state = 'rev';
719
720 # if we have an empty log message, put something there:
721 if (@svn_log) {
1c6bbbf3 722 $svn_log[$#svn_log]->{msg} ||= "\n";
ce6f3519 723 delete $svn_log[$#svn_log]->{lines};
3397f9df
EW
724 }
725 next;
726 }
727 if ($state eq 'rev' && s/^r(\d+)\s*\|\s*//) {
728 my $rev = $1;
ce6f3519
EW
729 my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
730 ($lines) = ($lines =~ /(\d+)/);
3397f9df
EW
731 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
732 /(\d{4})\-(\d\d)\-(\d\d)\s
733 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
734 or croak "Failed to parse date: $date\n";
735 my %log_msg = ( revision => $rev,
736 date => "$tz $Y-$m-$d $H:$M:$S",
737 author => $author,
ce6f3519 738 lines => $lines,
3397f9df 739 msg => '' );
1c6bbbf3 740 push @svn_log, \%log_msg;
3397f9df
EW
741 $state = 'msg_start';
742 next;
743 }
744 # skip the first blank line of the message:
745 if ($state eq 'msg_start' && /^$/) {
746 $state = 'msg';
747 } elsif ($state eq 'msg') {
ce6f3519
EW
748 if ($svn_log[$#svn_log]->{lines}) {
749 $svn_log[$#svn_log]->{msg} .= $_."\n";
750 unless (--$svn_log[$#svn_log]->{lines}) {
751 $state = 'sep';
752 }
753 } else {
754 croak "Log parse error at: $_\n",
755 $svn_log[$#svn_log]->{revision},"\n";
756 }
3397f9df
EW
757 }
758 }
759 close $log_fh or croak $?;
760 return \@svn_log;
761}
762
763sub svn_info {
764 my $url = shift || $SVN_URL;
765
766 my $pid = open my $info_fh, '-|';
767 defined $pid or croak $!;
768
769 if ($pid == 0) {
770 exec(qw(svn info),$url) or croak $!;
771 }
772
773 my $ret = {};
774 # only single-lines seem to exist in svn info output
775 while (<$info_fh>) {
776 chomp $_;
e17512f3 777 if (m#^([^:]+)\s*:\s*(\S.*)$#) {
3397f9df
EW
778 $ret->{$1} = $2;
779 push @{$ret->{-order}}, $1;
780 }
781 }
782 close $info_fh or croak $!;
783 return $ret;
784}
785
786sub sys { system(@_) == 0 or croak $? }
787
788sub git_addremove {
08703215 789 system( "git-diff-files --name-only -z ".
472ee9e3 790 " | git-update-index --remove -z --stdin && ".
08703215 791 "git-ls-files -z --others ".
3397f9df 792 "'--exclude-from=$GIT_DIR/$GIT_SVN/info/exclude'".
472ee9e3 793 " | git-update-index --add -z --stdin"
08703215 794 ) == 0 or croak $?
3397f9df
EW
795}
796
797sub s_to_file {
798 my ($str, $file, $mode) = @_;
799 open my $fd,'>',$file or croak $!;
800 print $fd $str,"\n" or croak $!;
801 close $fd or croak $!;
802 chmod ($mode &~ umask, $file) if (defined $mode);
803}
804
805sub file_to_s {
806 my $file = shift;
807 open my $fd,'<',$file or croak "$!: file: $file\n";
808 local $/;
809 my $ret = <$fd>;
810 close $fd or croak $!;
811 $ret =~ s/\s*$//s;
812 return $ret;
813}
814
815sub assert_revision_unknown {
816 my $revno = shift;
817 if (-f "$REV_DIR/$revno") {
818 croak "$REV_DIR/$revno already exists! ",
819 "Why are we refetching it?";
820 }
821}
822
823sub assert_revision_eq_or_unknown {
824 my ($revno, $commit) = @_;
825 if (-f "$REV_DIR/$revno") {
826 my $current = file_to_s("$REV_DIR/$revno");
827 if ($commit ne $current) {
828 croak "$REV_DIR/$revno already exists!\n",
829 "current: $current\nexpected: $commit\n";
830 }
831 return;
832 }
833}
834
835sub git_commit {
836 my ($log_msg, @parents) = @_;
837 assert_revision_unknown($log_msg->{revision});
838 my $out_fh = IO::File->new_tmpfile or croak $!;
839 my $info = svn_info('.');
840 my $uuid = $info->{'Repository UUID'};
841 defined $uuid or croak "Unable to get Repository UUID\n";
842
69f0d91e
EW
843 map_tree_joins() if (@_branch_from && !%tree_map);
844
3397f9df
EW
845 # commit parents can be conditionally bound to a particular
846 # svn revision via: "svn_revno=commit_sha1", filter them out here:
847 my @exec_parents;
848 foreach my $p (@parents) {
849 next unless defined $p;
850 if ($p =~ /^(\d+)=($sha1_short)$/o) {
851 if ($1 == $log_msg->{revision}) {
852 push @exec_parents, $2;
853 }
854 } else {
855 push @exec_parents, $p if $p =~ /$sha1_short/o;
856 }
857 }
858
859 my $pid = fork;
860 defined $pid or croak $!;
861 if ($pid == 0) {
862 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
863 git_addremove();
864 chomp(my $tree = `git-write-tree`);
865 croak if $?;
69f0d91e
EW
866 if (exists $tree_map{$tree}) {
867 my %seen_parent = map { $_ => 1 } @exec_parents;
868 foreach (@{$tree_map{$tree}}) {
869 # MAXPARENT is defined to 16 in commit-tree.c:
870 if ($seen_parent{$_} || @exec_parents > 16) {
871 next;
872 }
873 push @exec_parents, $_;
874 $seen_parent{$_} = 1;
875 }
876 }
3397f9df
EW
877 my $msg_fh = IO::File->new_tmpfile or croak $!;
878 print $msg_fh $log_msg->{msg}, "\ngit-svn-id: ",
879 "$SVN_URL\@$log_msg->{revision}",
880 " $uuid\n" or croak $!;
881 $msg_fh->flush == 0 or croak $!;
882 seek $msg_fh, 0, 0 or croak $!;
883
884 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} =
885 $log_msg->{author};
886 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} =
887 $log_msg->{author}."\@$uuid";
888 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} =
889 $log_msg->{date};
890 my @exec = ('git-commit-tree',$tree);
891 push @exec, '-p', $_ foreach @exec_parents;
892 open STDIN, '<&', $msg_fh or croak $!;
893 open STDOUT, '>&', $out_fh or croak $!;
894 exec @exec or croak $!;
895 }
896 waitpid($pid,0);
897 croak if $?;
898
899 $out_fh->flush == 0 or croak $!;
900 seek $out_fh, 0, 0 or croak $!;
901 chomp(my $commit = do { local $/; <$out_fh> });
902 if ($commit !~ /^$sha1$/o) {
903 croak "Failed to commit, invalid sha1: $commit\n";
904 }
2beb3cdd 905 my @update_ref = ('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
3397f9df
EW
906 if (my $primary_parent = shift @exec_parents) {
907 push @update_ref, $primary_parent;
908 }
909 sys(@update_ref);
910 sys('git-update-ref',"$GIT_SVN/revs/$log_msg->{revision}",$commit);
911 print "r$log_msg->{revision} = $commit\n";
912 return $commit;
913}
914
cf52b8f0
EW
915sub apply_mod_line_blob {
916 my $m = shift;
917 if ($m->{mode_b} =~ /^120/) {
918 blob_to_symlink($m->{sha1_b}, $m->{file_b});
919 } else {
920 blob_to_file($m->{sha1_b}, $m->{file_b});
921 }
922}
923
3397f9df
EW
924sub blob_to_symlink {
925 my ($blob, $link) = @_;
926 defined $link or croak "\$link not defined!\n";
927 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
cf52b8f0
EW
928 if (-l $link || -f _) {
929 unlink $link or croak $!;
930 }
931
3397f9df
EW
932 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
933 symlink $dest, $link or croak $!;
934}
935
936sub blob_to_file {
937 my ($blob, $file) = @_;
938 defined $file or croak "\$file not defined!\n";
939 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
cf52b8f0
EW
940 if (-l $file || -f _) {
941 unlink $file or croak $!;
942 }
943
3397f9df
EW
944 open my $blob_fh, '>', $file or croak "$!: $file\n";
945 my $pid = fork;
946 defined $pid or croak $!;
947
948 if ($pid == 0) {
949 open STDOUT, '>&', $blob_fh or croak $!;
950 exec('git-cat-file','blob',$blob);
951 }
952 waitpid $pid, 0;
953 croak $? if $?;
954
955 close $blob_fh or croak $!;
956}
957
958sub safe_qx {
959 my $pid = open my $child, '-|';
960 defined $pid or croak $!;
961 if ($pid == 0) {
962 exec(@_) or croak $?;
963 }
964 my @ret = (<$child>);
965 close $child or croak $?;
966 die $? if $?; # just in case close didn't error out
967 return wantarray ? @ret : join('',@ret);
968}
969
970sub svn_check_ignore_externals {
971 return if $_no_ignore_ext;
972 unless (grep /ignore-externals/,(safe_qx(qw(svn co -h)))) {
973 print STDERR "W: Installed svn version does not support ",
974 "--ignore-externals\n";
975 $_no_ignore_ext = 1;
976 }
977}
2beb3cdd
EW
978
979sub check_upgrade_needed {
980 my $old = eval {
981 my $pid = open my $child, '-|';
982 defined $pid or croak $!;
983 if ($pid == 0) {
984 close STDERR;
985 exec('git-rev-parse',"$GIT_SVN-HEAD") or croak $?;
986 }
987 my @ret = (<$child>);
988 close $child or croak $?;
989 die $? if $?; # just in case close didn't error out
990 return wantarray ? @ret : join('',@ret);
991 };
992 return unless $old;
993 my $head = eval { safe_qx('git-rev-parse',"refs/remotes/$GIT_SVN") };
994 if ($@ || !$head) {
995 print STDERR "Please run: $0 rebuild --upgrade\n";
996 exit 1;
997 }
998}
999
69f0d91e
EW
1000# fills %tree_map with a reverse mapping of trees to commits. Useful
1001# for finding parents to commit on.
1002sub map_tree_joins {
1003 foreach my $br (@_branch_from) {
1004 my $pid = open my $pipe, '-|';
1005 defined $pid or croak $!;
1006 if ($pid == 0) {
1007 exec(qw(git-rev-list --pretty=raw), $br) or croak $?;
1008 }
1009 while (<$pipe>) {
1010 if (/^commit ($sha1)$/o) {
1011 my $commit = $1;
1012 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
1013 unless (defined $tree) {
1014 die "Failed to parse commit $commit\n";
1015 }
1016 push @{$tree_map{$tree}}, $commit;
1017 }
1018 }
1019 close $pipe or croak $?;
1020 }
1021}
1022
3397f9df
EW
1023__END__
1024
1025Data structures:
1026
1027@svn_log = array of log_msg hashes
1028
1029$log_msg hash
1030{
1031 msg => 'whitespace-formatted log entry
1032', # trailing newline is preserved
1033 revision => '8', # integer
1034 date => '2004-02-24T17:01:44.108345Z', # commit date
1035 author => 'committer name'
1036};
1037
1038
1039@mods = array of diff-index line hashes, each element represents one line
1040 of diff-index output
1041
1042diff-index line ($m hash)
1043{
1044 mode_a => first column of diff-index output, no leading ':',
1045 mode_b => second column of diff-index output,
1046 sha1_b => sha1sum of the final blob,
ac8e0b91 1047 chg => change type [MCRADT],
3397f9df
EW
1048 file_a => original file name of a file (iff chg is 'C' or 'R')
1049 file_b => new/current file name of a file (any chg)
1050}
1051;