contrib/git-svn: add -b/--branch switch for branch detection
[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
EW
34my $sha1 = qr/[a-f\d]{40}/;
35my $sha1_short = qr/[a-f\d]{6,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) {
93 print $fd ' ',pack('A10',$_),$cmd{$_}->[1],"\n";
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
98and want to keep them separate.
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>) {
8de010ad 248 if (/\b([a-f\d]{6,40})\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) {
270 print "Committing $c\n";
cf52b8f0
EW
271 my $mods = svn_checkout_tree($svn_current_rev, $c);
272 if (scalar @$mods == 0) {
273 print "Skipping, no changes detected\n";
274 next;
275 }
3397f9df
EW
276 $svn_current_rev = svn_commit_tree($svn_current_rev, $c);
277 }
278 print "Done committing ",scalar @revs," revisions to SVN\n";
279
280}
281
8f22562c
EW
282sub show_ignore {
283 require File::Find or die $!;
284 my $exclude_file = "$GIT_DIR/info/exclude";
285 open my $fh, '<', $exclude_file or croak $!;
286 chomp(my @excludes = (<$fh>));
287 close $fh or croak $!;
288
289 $SVN_URL ||= file_to_s("$GIT_DIR/$GIT_SVN/info/url");
290 chdir $SVN_WC or croak $!;
291 my %ign;
292 File::Find::find({wanted=>sub{if(lstat $_ && -d _ && -d "$_/.svn"){
293 s#^\./##;
294 @{$ign{$_}} = safe_qx(qw(svn propget svn:ignore),$_);
295 }}, no_chdir=>1},'.');
296
297 print "\n# /\n";
298 foreach (@{$ign{'.'}}) { print '/',$_ if /\S/ }
299 delete $ign{'.'};
300 foreach my $i (sort keys %ign) {
301 print "\n# ",$i,"\n";
302 foreach (@{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ }
303 }
304}
305
3397f9df
EW
306########################### utility functions #########################
307
308sub setup_git_svn {
309 defined $SVN_URL or croak "SVN repository location required\n";
310 unless (-d $GIT_DIR) {
311 croak "GIT_DIR=$GIT_DIR does not exist!\n";
312 }
313 mkpath(["$GIT_DIR/$GIT_SVN"]);
314 mkpath(["$GIT_DIR/$GIT_SVN/info"]);
315 mkpath([$REV_DIR]);
316 s_to_file($SVN_URL,"$GIT_DIR/$GIT_SVN/info/url");
317 my $uuid = svn_info($SVN_URL)->{'Repository UUID'} or
318 croak "Repository UUID unreadable\n";
319 s_to_file($uuid,"$GIT_DIR/$GIT_SVN/info/uuid");
320
321 open my $fd, '>>', "$GIT_DIR/$GIT_SVN/info/exclude" or croak $!;
322 print $fd '.svn',"\n";
323 close $fd or croak $!;
324 return $uuid;
325}
326
327sub assert_svn_wc_clean {
cf52b8f0 328 my ($svn_rev, $treeish) = @_;
3397f9df 329 croak "$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
cf52b8f0 330 croak "$treeish is not a sha1!\n" unless ($treeish =~ /^$sha1$/o);
3397f9df
EW
331 my $svn_info = svn_info('.');
332 if ($svn_rev != $svn_info->{'Last Changed Rev'}) {
333 croak "Expected r$svn_rev, got r",
334 $svn_info->{'Last Changed Rev'},"\n";
335 }
336 my @status = grep(!/^Performing status on external/,(`svn status`));
337 @status = grep(!/^\s*$/,@status);
338 if (scalar @status) {
339 print STDERR "Tree ($SVN_WC) is not clean:\n";
340 print STDERR $_ foreach @status;
341 croak;
342 }
cf52b8f0
EW
343 assert_tree($treeish);
344}
345
346sub assert_tree {
347 my ($treeish) = @_;
348 croak "Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o;
349 chomp(my $type = `git-cat-file -t $treeish`);
350 my $expected;
351 while ($type eq 'tag') {
352 chomp(($treeish, $type) = `git-cat-file tag $treeish`);
353 }
354 if ($type eq 'commit') {
355 $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0];
356 ($expected) = ($expected =~ /^tree ($sha1)$/);
357 die "Unable to get tree from $treeish\n" unless $expected;
358 } elsif ($type eq 'tree') {
359 $expected = $treeish;
360 } else {
361 die "$treeish is a $type, expected tree, tag or commit\n";
362 }
363
364 my $old_index = $ENV{GIT_INDEX_FILE};
365 my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp';
366 if (-e $tmpindex) {
367 unlink $tmpindex or croak $!;
368 }
369 $ENV{GIT_INDEX_FILE} = $tmpindex;
370 git_addremove();
371 chomp(my $tree = `git-write-tree`);
372 if ($old_index) {
373 $ENV{GIT_INDEX_FILE} = $old_index;
374 } else {
375 delete $ENV{GIT_INDEX_FILE};
376 }
377 if ($tree ne $expected) {
378 croak "Tree mismatch, Got: $tree, Expected: $expected\n";
3397f9df
EW
379 }
380}
381
382sub parse_diff_tree {
383 my $diff_fh = shift;
384 local $/ = "\0";
385 my $state = 'meta';
386 my @mods;
387 while (<$diff_fh>) {
388 chomp $_; # this gets rid of the trailing "\0"
3397f9df
EW
389 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
390 $sha1\s($sha1)\s([MTCRAD])\d*$/xo) {
391 push @mods, { mode_a => $1, mode_b => $2,
392 sha1_b => $3, chg => $4 };
393 if ($4 =~ /^(?:C|R)$/) {
394 $state = 'file_a';
395 } else {
396 $state = 'file_b';
397 }
398 } elsif ($state eq 'file_a') {
cf52b8f0 399 my $x = $mods[$#mods] or croak "Empty array\n";
3397f9df 400 if ($x->{chg} !~ /^(?:C|R)$/) {
cf52b8f0 401 croak "Error parsing $_, $x->{chg}\n";
3397f9df
EW
402 }
403 $x->{file_a} = $_;
404 $state = 'file_b';
405 } elsif ($state eq 'file_b') {
cf52b8f0 406 my $x = $mods[$#mods] or croak "Empty array\n";
3397f9df 407 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
cf52b8f0 408 croak "Error parsing $_, $x->{chg}\n";
3397f9df
EW
409 }
410 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
cf52b8f0 411 croak "Error parsing $_, $x->{chg}\n";
3397f9df
EW
412 }
413 $x->{file_b} = $_;
414 $state = 'meta';
415 } else {
cf52b8f0 416 croak "Error parsing $_\n";
3397f9df
EW
417 }
418 }
419 close $diff_fh or croak $!;
cf52b8f0 420
3397f9df
EW
421 return \@mods;
422}
423
424sub svn_check_prop_executable {
425 my $m = shift;
cf52b8f0
EW
426 return if -l $m->{file_b};
427 if ($m->{mode_b} =~ /755$/) {
428 chmod((0755 &~ umask),$m->{file_b}) or croak $!;
429 if ($m->{mode_a} !~ /755$/) {
430 sys(qw(svn propset svn:executable 1), $m->{file_b});
431 }
432 -x $m->{file_b} or croak "$m->{file_b} is not executable!\n";
3397f9df
EW
433 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
434 sys(qw(svn propdel svn:executable), $m->{file_b});
cf52b8f0
EW
435 chmod((0644 &~ umask),$m->{file_b}) or croak $!;
436 -x $m->{file_b} and croak "$m->{file_b} is executable!\n";
3397f9df
EW
437 }
438}
439
440sub svn_ensure_parent_path {
441 my $dir_b = dirname(shift);
442 svn_ensure_parent_path($dir_b) if ($dir_b ne File::Spec->curdir);
443 mkpath([$dir_b]) unless (-d $dir_b);
444 sys(qw(svn add -N), $dir_b) unless (-d "$dir_b/.svn");
445}
446
cf52b8f0
EW
447sub precommit_check {
448 my $mods = shift;
449 my (%rm_file, %rmdir_check, %added_check);
450
451 my %o = ( D => 0, R => 1, C => 2, A => 3, M => 3, T => 3 );
452 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
453 if ($m->{chg} eq 'R') {
454 if (-d $m->{file_b}) {
455 err_dir_to_file("$m->{file_a} => $m->{file_b}");
456 }
457 # dir/$file => dir/file/$file
458 my $dirname = dirname($m->{file_b});
459 while ($dirname ne File::Spec->curdir) {
460 if ($dirname ne $m->{file_a}) {
461 $dirname = dirname($dirname);
462 next;
463 }
464 err_file_to_dir("$m->{file_a} => $m->{file_b}");
465 }
466 # baz/zzz => baz (baz is a file)
467 $dirname = dirname($m->{file_a});
468 while ($dirname ne File::Spec->curdir) {
469 if ($dirname ne $m->{file_b}) {
470 $dirname = dirname($dirname);
471 next;
472 }
473 err_dir_to_file("$m->{file_a} => $m->{file_b}");
474 }
475 }
476 if ($m->{chg} =~ /^(D|R)$/) {
477 my $t = $1 eq 'D' ? 'file_b' : 'file_a';
478 $rm_file{ $m->{$t} } = 1;
479 my $dirname = dirname( $m->{$t} );
480 my $basename = basename( $m->{$t} );
481 $rmdir_check{$dirname}->{$basename} = 1;
482 } elsif ($m->{chg} =~ /^(?:A|C)$/) {
483 if (-d $m->{file_b}) {
484 err_dir_to_file($m->{file_b});
485 }
486 my $dirname = dirname( $m->{file_b} );
487 my $basename = basename( $m->{file_b} );
488 $added_check{$dirname}->{$basename} = 1;
489 while ($dirname ne File::Spec->curdir) {
490 if ($rm_file{$dirname}) {
491 err_file_to_dir($m->{file_b});
492 }
493 $dirname = dirname $dirname;
494 }
495 }
496 }
497 return (\%rmdir_check, \%added_check);
498
499 sub err_dir_to_file {
500 my $file = shift;
501 print STDERR "Node change from directory to file ",
502 "is not supported by Subversion: ",$file,"\n";
503 exit 1;
504 }
505 sub err_file_to_dir {
506 my $file = shift;
507 print STDERR "Node change from file to directory ",
508 "is not supported by Subversion: ",$file,"\n";
509 exit 1;
510 }
511}
512
3397f9df 513sub svn_checkout_tree {
cf52b8f0 514 my ($svn_rev, $treeish) = @_;
3397f9df
EW
515 my $from = file_to_s("$REV_DIR/$svn_rev");
516 assert_svn_wc_clean($svn_rev,$from);
cf52b8f0 517 print "diff-tree '$from' '$treeish'\n";
3397f9df
EW
518 my $pid = open my $diff_fh, '-|';
519 defined $pid or croak $!;
520 if ($pid == 0) {
72942938
EW
521 my @diff_tree = qw(git-diff-tree -z -r -C);
522 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
523 push @diff_tree, "-l$_l" if defined $_l;
cf52b8f0 524 exec(@diff_tree, $from, $treeish) or croak $!;
3397f9df
EW
525 }
526 my $mods = parse_diff_tree($diff_fh);
527 unless (@$mods) {
528 # git can do empty commits, SVN doesn't allow it...
cf52b8f0 529 return $mods;
3397f9df 530 }
cf52b8f0
EW
531 my ($rm, $add) = precommit_check($mods);
532
533 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
534 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
3397f9df
EW
535 if ($m->{chg} eq 'C') {
536 svn_ensure_parent_path( $m->{file_b} );
537 sys(qw(svn cp), $m->{file_a}, $m->{file_b});
cf52b8f0 538 apply_mod_line_blob($m);
3397f9df
EW
539 svn_check_prop_executable($m);
540 } elsif ($m->{chg} eq 'D') {
3397f9df
EW
541 sys(qw(svn rm --force), $m->{file_b});
542 } elsif ($m->{chg} eq 'R') {
543 svn_ensure_parent_path( $m->{file_b} );
544 sys(qw(svn mv --force), $m->{file_a}, $m->{file_b});
cf52b8f0 545 apply_mod_line_blob($m);
3397f9df 546 svn_check_prop_executable($m);
3397f9df 547 } elsif ($m->{chg} eq 'M') {
cf52b8f0 548 apply_mod_line_blob($m);
3397f9df
EW
549 svn_check_prop_executable($m);
550 } elsif ($m->{chg} eq 'T') {
551 sys(qw(svn rm --force),$m->{file_b});
cf52b8f0 552 apply_mod_line_blob($m);
3397f9df 553 sys(qw(svn add --force), $m->{file_b});
cf52b8f0 554 svn_check_prop_executable($m);
3397f9df
EW
555 } elsif ($m->{chg} eq 'A') {
556 svn_ensure_parent_path( $m->{file_b} );
cf52b8f0 557 apply_mod_line_blob($m);
3397f9df 558 sys(qw(svn add --force), $m->{file_b});
cf52b8f0 559 svn_check_prop_executable($m);
3397f9df
EW
560 } else {
561 croak "Invalid chg: $m->{chg}\n";
562 }
563 }
cf52b8f0
EW
564
565 assert_tree($treeish);
566 if ($_rmdir) { # remove empty directories
567 handle_rmdir($rm, $add);
568 }
569 assert_tree($treeish);
570 return $mods;
571}
572
573# svn ls doesn't work with respect to the current working tree, but what's
574# in the repository. There's not even an option for it... *sigh*
575# (added files don't show up and removed files remain in the ls listing)
576sub svn_ls_current {
577 my ($dir, $rm, $add) = @_;
578 chomp(my @ls = safe_qx('svn','ls',$dir));
579 my @ret = ();
580 foreach (@ls) {
581 s#/$##; # trailing slashes are evil
582 push @ret, $_ unless $rm->{$dir}->{$_};
583 }
584 if (exists $add->{$dir}) {
585 push @ret, keys %{$add->{$dir}};
586 }
587 return \@ret;
588}
589
590sub handle_rmdir {
591 my ($rm, $add) = @_;
592
593 foreach my $dir (sort {length $b <=> length $a} keys %$rm) {
594 my $ls = svn_ls_current($dir, $rm, $add);
595 next if (scalar @$ls);
596 sys(qw(svn rm --force),$dir);
597
598 my $dn = dirname $dir;
599 $rm->{ $dn }->{ basename $dir } = 1;
600 $ls = svn_ls_current($dn, $rm, $add);
601 while (scalar @$ls == 0 && $dn ne File::Spec->curdir) {
602 sys(qw(svn rm --force),$dn);
603 $dir = basename $dn;
604 $dn = dirname $dn;
605 $rm->{ $dn }->{ $dir } = 1;
606 $ls = svn_ls_current($dn, $rm, $add);
3397f9df
EW
607 }
608 }
609}
610
611sub svn_commit_tree {
612 my ($svn_rev, $commit) = @_;
613 my $commit_msg = "$GIT_DIR/$GIT_SVN/.svn-commit.tmp.$$";
e17512f3 614 my %log_msg = ( msg => '' );
3397f9df
EW
615 open my $msg, '>', $commit_msg or croak $!;
616
617 chomp(my $type = `git-cat-file -t $commit`);
618 if ($type eq 'commit') {
619 my $pid = open my $msg_fh, '-|';
620 defined $pid or croak $!;
621
622 if ($pid == 0) {
623 exec(qw(git-cat-file commit), $commit) or croak $!;
624 }
625 my $in_msg = 0;
626 while (<$msg_fh>) {
627 if (!$in_msg) {
628 $in_msg = 1 if (/^\s*$/);
629 } else {
e17512f3 630 $log_msg{msg} .= $_;
3397f9df
EW
631 print $msg $_ or croak $!;
632 }
633 }
634 close $msg_fh or croak $!;
635 }
636 close $msg or croak $!;
637
638 if ($_edit || ($type eq 'tree')) {
639 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
640 system($editor, $commit_msg);
641 }
642 my @ci_output = safe_qx(qw(svn commit -F),$commit_msg);
643 my ($committed) = grep(/^Committed revision \d+\./,@ci_output);
644 unlink $commit_msg;
645 defined $committed or croak
646 "Commit output failed to parse committed revision!\n",
647 join("\n",@ci_output),"\n";
648 my ($rev_committed) = ($committed =~ /^Committed revision (\d+)\./);
649
e17512f3 650 my @svn_up = qw(svn up);
3397f9df 651 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
e17512f3
EW
652 if ($rev_committed == ($svn_rev + 1)) {
653 push @svn_up, "-r$rev_committed";
654 sys(@svn_up);
655 my $info = svn_info('.');
656 my $date = $info->{'Last Changed Date'} or die "Missing date\n";
657 if ($info->{'Last Changed Rev'} != $rev_committed) {
658 croak "$info->{'Last Changed Rev'} != $rev_committed\n"
659 }
660 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
661 /(\d{4})\-(\d\d)\-(\d\d)\s
662 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
663 or croak "Failed to parse date: $date\n";
664 $log_msg{date} = "$tz $Y-$m-$d $H:$M:$S";
665 $log_msg{author} = $info->{'Last Changed Author'};
666 $log_msg{revision} = $rev_committed;
667 $log_msg{msg} .= "\n";
668 my $parent = file_to_s("$REV_DIR/$svn_rev");
669 git_commit(\%log_msg, $parent, $commit);
670 return $rev_committed;
671 }
672 # resync immediately
673 push @svn_up, "-r$svn_rev";
3397f9df
EW
674 sys(@svn_up);
675 return fetch("$rev_committed=$commit")->{revision};
676}
677
3397f9df
EW
678sub svn_log_raw {
679 my (@log_args) = @_;
680 my $pid = open my $log_fh,'-|';
681 defined $pid or croak $!;
682
683 if ($pid == 0) {
684 exec (qw(svn log), @log_args) or croak $!
685 }
686
687 my @svn_log;
ce6f3519 688 my $state = 'sep';
3397f9df
EW
689 while (<$log_fh>) {
690 chomp;
691 if (/^\-{72}$/) {
ce6f3519
EW
692 if ($state eq 'msg') {
693 if ($svn_log[$#svn_log]->{lines}) {
694 $svn_log[$#svn_log]->{msg} .= $_."\n";
695 unless(--$svn_log[$#svn_log]->{lines}) {
696 $state = 'sep';
697 }
698 } else {
699 croak "Log parse error at: $_\n",
700 $svn_log[$#svn_log]->{revision},
701 "\n";
702 }
703 next;
704 }
705 if ($state ne 'sep') {
706 croak "Log parse error at: $_\n",
707 "state: $state\n",
708 $svn_log[$#svn_log]->{revision},
709 "\n";
710 }
3397f9df
EW
711 $state = 'rev';
712
713 # if we have an empty log message, put something there:
714 if (@svn_log) {
1c6bbbf3 715 $svn_log[$#svn_log]->{msg} ||= "\n";
ce6f3519 716 delete $svn_log[$#svn_log]->{lines};
3397f9df
EW
717 }
718 next;
719 }
720 if ($state eq 'rev' && s/^r(\d+)\s*\|\s*//) {
721 my $rev = $1;
ce6f3519
EW
722 my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3);
723 ($lines) = ($lines =~ /(\d+)/);
3397f9df
EW
724 my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
725 /(\d{4})\-(\d\d)\-(\d\d)\s
726 (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
727 or croak "Failed to parse date: $date\n";
728 my %log_msg = ( revision => $rev,
729 date => "$tz $Y-$m-$d $H:$M:$S",
730 author => $author,
ce6f3519 731 lines => $lines,
3397f9df 732 msg => '' );
1c6bbbf3 733 push @svn_log, \%log_msg;
3397f9df
EW
734 $state = 'msg_start';
735 next;
736 }
737 # skip the first blank line of the message:
738 if ($state eq 'msg_start' && /^$/) {
739 $state = 'msg';
740 } elsif ($state eq 'msg') {
ce6f3519
EW
741 if ($svn_log[$#svn_log]->{lines}) {
742 $svn_log[$#svn_log]->{msg} .= $_."\n";
743 unless (--$svn_log[$#svn_log]->{lines}) {
744 $state = 'sep';
745 }
746 } else {
747 croak "Log parse error at: $_\n",
748 $svn_log[$#svn_log]->{revision},"\n";
749 }
3397f9df
EW
750 }
751 }
752 close $log_fh or croak $?;
753 return \@svn_log;
754}
755
756sub svn_info {
757 my $url = shift || $SVN_URL;
758
759 my $pid = open my $info_fh, '-|';
760 defined $pid or croak $!;
761
762 if ($pid == 0) {
763 exec(qw(svn info),$url) or croak $!;
764 }
765
766 my $ret = {};
767 # only single-lines seem to exist in svn info output
768 while (<$info_fh>) {
769 chomp $_;
e17512f3 770 if (m#^([^:]+)\s*:\s*(\S.*)$#) {
3397f9df
EW
771 $ret->{$1} = $2;
772 push @{$ret->{-order}}, $1;
773 }
774 }
775 close $info_fh or croak $!;
776 return $ret;
777}
778
779sub sys { system(@_) == 0 or croak $? }
780
781sub git_addremove {
08703215 782 system( "git-diff-files --name-only -z ".
472ee9e3 783 " | git-update-index --remove -z --stdin && ".
08703215 784 "git-ls-files -z --others ".
3397f9df 785 "'--exclude-from=$GIT_DIR/$GIT_SVN/info/exclude'".
472ee9e3 786 " | git-update-index --add -z --stdin"
08703215 787 ) == 0 or croak $?
3397f9df
EW
788}
789
790sub s_to_file {
791 my ($str, $file, $mode) = @_;
792 open my $fd,'>',$file or croak $!;
793 print $fd $str,"\n" or croak $!;
794 close $fd or croak $!;
795 chmod ($mode &~ umask, $file) if (defined $mode);
796}
797
798sub file_to_s {
799 my $file = shift;
800 open my $fd,'<',$file or croak "$!: file: $file\n";
801 local $/;
802 my $ret = <$fd>;
803 close $fd or croak $!;
804 $ret =~ s/\s*$//s;
805 return $ret;
806}
807
808sub assert_revision_unknown {
809 my $revno = shift;
810 if (-f "$REV_DIR/$revno") {
811 croak "$REV_DIR/$revno already exists! ",
812 "Why are we refetching it?";
813 }
814}
815
816sub assert_revision_eq_or_unknown {
817 my ($revno, $commit) = @_;
818 if (-f "$REV_DIR/$revno") {
819 my $current = file_to_s("$REV_DIR/$revno");
820 if ($commit ne $current) {
821 croak "$REV_DIR/$revno already exists!\n",
822 "current: $current\nexpected: $commit\n";
823 }
824 return;
825 }
826}
827
828sub git_commit {
829 my ($log_msg, @parents) = @_;
830 assert_revision_unknown($log_msg->{revision});
831 my $out_fh = IO::File->new_tmpfile or croak $!;
832 my $info = svn_info('.');
833 my $uuid = $info->{'Repository UUID'};
834 defined $uuid or croak "Unable to get Repository UUID\n";
835
69f0d91e
EW
836 map_tree_joins() if (@_branch_from && !%tree_map);
837
3397f9df
EW
838 # commit parents can be conditionally bound to a particular
839 # svn revision via: "svn_revno=commit_sha1", filter them out here:
840 my @exec_parents;
841 foreach my $p (@parents) {
842 next unless defined $p;
843 if ($p =~ /^(\d+)=($sha1_short)$/o) {
844 if ($1 == $log_msg->{revision}) {
845 push @exec_parents, $2;
846 }
847 } else {
848 push @exec_parents, $p if $p =~ /$sha1_short/o;
849 }
850 }
851
852 my $pid = fork;
853 defined $pid or croak $!;
854 if ($pid == 0) {
855 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
856 git_addremove();
857 chomp(my $tree = `git-write-tree`);
858 croak if $?;
69f0d91e
EW
859 if (exists $tree_map{$tree}) {
860 my %seen_parent = map { $_ => 1 } @exec_parents;
861 foreach (@{$tree_map{$tree}}) {
862 # MAXPARENT is defined to 16 in commit-tree.c:
863 if ($seen_parent{$_} || @exec_parents > 16) {
864 next;
865 }
866 push @exec_parents, $_;
867 $seen_parent{$_} = 1;
868 }
869 }
3397f9df
EW
870 my $msg_fh = IO::File->new_tmpfile or croak $!;
871 print $msg_fh $log_msg->{msg}, "\ngit-svn-id: ",
872 "$SVN_URL\@$log_msg->{revision}",
873 " $uuid\n" or croak $!;
874 $msg_fh->flush == 0 or croak $!;
875 seek $msg_fh, 0, 0 or croak $!;
876
877 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} =
878 $log_msg->{author};
879 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} =
880 $log_msg->{author}."\@$uuid";
881 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} =
882 $log_msg->{date};
883 my @exec = ('git-commit-tree',$tree);
884 push @exec, '-p', $_ foreach @exec_parents;
885 open STDIN, '<&', $msg_fh or croak $!;
886 open STDOUT, '>&', $out_fh or croak $!;
887 exec @exec or croak $!;
888 }
889 waitpid($pid,0);
890 croak if $?;
891
892 $out_fh->flush == 0 or croak $!;
893 seek $out_fh, 0, 0 or croak $!;
894 chomp(my $commit = do { local $/; <$out_fh> });
895 if ($commit !~ /^$sha1$/o) {
896 croak "Failed to commit, invalid sha1: $commit\n";
897 }
2beb3cdd 898 my @update_ref = ('git-update-ref',"refs/remotes/$GIT_SVN",$commit);
3397f9df
EW
899 if (my $primary_parent = shift @exec_parents) {
900 push @update_ref, $primary_parent;
901 }
902 sys(@update_ref);
903 sys('git-update-ref',"$GIT_SVN/revs/$log_msg->{revision}",$commit);
904 print "r$log_msg->{revision} = $commit\n";
905 return $commit;
906}
907
cf52b8f0
EW
908sub apply_mod_line_blob {
909 my $m = shift;
910 if ($m->{mode_b} =~ /^120/) {
911 blob_to_symlink($m->{sha1_b}, $m->{file_b});
912 } else {
913 blob_to_file($m->{sha1_b}, $m->{file_b});
914 }
915}
916
3397f9df
EW
917sub blob_to_symlink {
918 my ($blob, $link) = @_;
919 defined $link or croak "\$link not defined!\n";
920 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
cf52b8f0
EW
921 if (-l $link || -f _) {
922 unlink $link or croak $!;
923 }
924
3397f9df
EW
925 my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
926 symlink $dest, $link or croak $!;
927}
928
929sub blob_to_file {
930 my ($blob, $file) = @_;
931 defined $file or croak "\$file not defined!\n";
932 croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
cf52b8f0
EW
933 if (-l $file || -f _) {
934 unlink $file or croak $!;
935 }
936
3397f9df
EW
937 open my $blob_fh, '>', $file or croak "$!: $file\n";
938 my $pid = fork;
939 defined $pid or croak $!;
940
941 if ($pid == 0) {
942 open STDOUT, '>&', $blob_fh or croak $!;
943 exec('git-cat-file','blob',$blob);
944 }
945 waitpid $pid, 0;
946 croak $? if $?;
947
948 close $blob_fh or croak $!;
949}
950
951sub safe_qx {
952 my $pid = open my $child, '-|';
953 defined $pid or croak $!;
954 if ($pid == 0) {
955 exec(@_) or croak $?;
956 }
957 my @ret = (<$child>);
958 close $child or croak $?;
959 die $? if $?; # just in case close didn't error out
960 return wantarray ? @ret : join('',@ret);
961}
962
963sub svn_check_ignore_externals {
964 return if $_no_ignore_ext;
965 unless (grep /ignore-externals/,(safe_qx(qw(svn co -h)))) {
966 print STDERR "W: Installed svn version does not support ",
967 "--ignore-externals\n";
968 $_no_ignore_ext = 1;
969 }
970}
2beb3cdd
EW
971
972sub check_upgrade_needed {
973 my $old = eval {
974 my $pid = open my $child, '-|';
975 defined $pid or croak $!;
976 if ($pid == 0) {
977 close STDERR;
978 exec('git-rev-parse',"$GIT_SVN-HEAD") or croak $?;
979 }
980 my @ret = (<$child>);
981 close $child or croak $?;
982 die $? if $?; # just in case close didn't error out
983 return wantarray ? @ret : join('',@ret);
984 };
985 return unless $old;
986 my $head = eval { safe_qx('git-rev-parse',"refs/remotes/$GIT_SVN") };
987 if ($@ || !$head) {
988 print STDERR "Please run: $0 rebuild --upgrade\n";
989 exit 1;
990 }
991}
992
69f0d91e
EW
993# fills %tree_map with a reverse mapping of trees to commits. Useful
994# for finding parents to commit on.
995sub map_tree_joins {
996 foreach my $br (@_branch_from) {
997 my $pid = open my $pipe, '-|';
998 defined $pid or croak $!;
999 if ($pid == 0) {
1000 exec(qw(git-rev-list --pretty=raw), $br) or croak $?;
1001 }
1002 while (<$pipe>) {
1003 if (/^commit ($sha1)$/o) {
1004 my $commit = $1;
1005 my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o);
1006 unless (defined $tree) {
1007 die "Failed to parse commit $commit\n";
1008 }
1009 push @{$tree_map{$tree}}, $commit;
1010 }
1011 }
1012 close $pipe or croak $?;
1013 }
1014}
1015
3397f9df
EW
1016__END__
1017
1018Data structures:
1019
1020@svn_log = array of log_msg hashes
1021
1022$log_msg hash
1023{
1024 msg => 'whitespace-formatted log entry
1025', # trailing newline is preserved
1026 revision => '8', # integer
1027 date => '2004-02-24T17:01:44.108345Z', # commit date
1028 author => 'committer name'
1029};
1030
1031
1032@mods = array of diff-index line hashes, each element represents one line
1033 of diff-index output
1034
1035diff-index line ($m hash)
1036{
1037 mode_a => first column of diff-index output, no leading ':',
1038 mode_b => second column of diff-index output,
1039 sha1_b => sha1sum of the final blob,
1040 chg => change type [MCRAD],
1041 file_a => original file name of a file (iff chg is 'C' or 'R')
1042 file_b => new/current file name of a file (any chg)
1043}
1044;