Introducing contrib/git-svn.
[git.git] / contrib / git-svn / git-svn
1 #!/usr/bin/env perl
2 use warnings;
3 use strict;
4 use vars qw/    $AUTHOR $VERSION
5                 $SVN_URL $SVN_INFO $SVN_WC
6                 $GIT_SVN_INDEX $GIT_SVN
7                 $GIT_DIR $REV_DIR/;
8 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
9 $VERSION = '0.9.0';
10 $GIT_DIR = $ENV{GIT_DIR} || "$ENV{PWD}/.git";
11 $GIT_SVN = $ENV{GIT_SVN_ID} || 'git-svn';
12 $GIT_SVN_INDEX = "$GIT_DIR/$GIT_SVN/index";
13 $ENV{GIT_DIR} ||= $GIT_DIR;
14 $SVN_URL = undef;
15 $REV_DIR = "$GIT_DIR/$GIT_SVN/revs";
16 $SVN_WC = "$GIT_DIR/$GIT_SVN/tree";
17
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.
24 # See what I do with XML::Simple to make the dependency optional.
25 use Carp qw/croak/;
26 use IO::File qw//;
27 use File::Basename qw/dirname basename/;
28 use File::Path qw/mkpath/;
29 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
30 use File::Spec qw//;
31 my $sha1 = qr/[a-f\d]{40}/;
32 my $sha1_short = qr/[a-f\d]{6,40}/;
33 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit);
34
35 GetOptions(     'revision|r=s' => \$_revision,
36                 'no-ignore-externals' => \$_no_ignore_ext,
37                 'stdin|' => \$_stdin,
38                 'edit|e' => \$_edit,
39                 'rmdir' => \$_rmdir,
40                 'help|H|h' => \$_help,
41                 'no-stop-copy' => \$_no_stop_copy );
42 my %cmd = (
43         fetch => [ \&fetch, "Download new revisions from SVN" ],
44         init => [ \&init, "Initialize and fetch (import)"],
45         commit => [ \&commit, "Commit git revisions to SVN" ],
46         rebuild => [ \&rebuild, "Rebuild git-svn metadata (after git clone)" ],
47         help => [ \&usage, "Show help" ],
48 );
49 my $cmd;
50 for (my $i = 0; $i < @ARGV; $i++) {
51         if (defined $cmd{$ARGV[$i]}) {
52                 $cmd = $ARGV[$i];
53                 splice @ARGV, $i, 1;
54                 last;
55         }
56 };
57
58 # we may be called as git-svn-(command), or git-svn(command).
59 foreach (keys %cmd) {
60         if (/git\-svn\-?($_)(?:\.\w+)?$/) {
61                 $cmd = $1;
62                 last;
63         }
64 }
65 usage(0) if $_help;
66 usage(1) unless (defined $cmd);
67 svn_check_ignore_externals();
68 $cmd{$cmd}->[0]->(@ARGV);
69 exit 0;
70
71 ####################### primary functions ######################
72 sub usage {
73         my $exit = shift || 0;
74         my $fd = $exit ? \*STDERR : \*STDOUT;
75         print $fd <<"";
76 git-svn - bidirectional operations between a single Subversion tree and git
77 Usage: $0 <command> [options] [arguments]\n
78 Available commands:
79
80         foreach (sort keys %cmd) {
81                 print $fd '  ',pack('A10',$_),$cmd{$_}->[1],"\n";
82         }
83         print $fd <<"";
84 \nGIT_SVN_ID may be set in the environment to an arbitrary identifier if
85 you're tracking multiple SVN branches/repositories in one git repository
86 and want to keep them separate.
87
88         exit $exit;
89 }
90
91 sub rebuild {
92         $SVN_URL = shift or undef;
93         my $repo_uuid;
94         my $newest_rev = 0;
95
96         my $pid = open(my $rev_list,'-|');
97         defined $pid or croak $!;
98         if ($pid == 0) {
99                 exec("git-rev-list","$GIT_SVN-HEAD") or croak $!;
100         }
101         my $first;
102         while (<$rev_list>) {
103                 chomp;
104                 my $c = $_;
105                 croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
106                 my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
107                 next if (!@commit); # skip merges
108                 my $id = $commit[$#commit];
109                 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
110                                                 \s([a-f\d\-]+)$/x);
111                 if (!$rev || !$uuid || !$url) {
112                         # some of the original repositories I made had
113                         # indentifiers like this:
114                         ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)
115                                                         \@([a-f\d\-]+)/x);
116                         if (!$rev || !$uuid) {
117                                 croak "Unable to extract revision or UUID from ",
118                                         "$c, $id\n";
119                         }
120                 }
121                 print "r$rev = $c\n";
122                 unless (defined $first) {
123                         if (!$SVN_URL && !$url) {
124                                 croak "SVN repository location required: $url\n";
125                         }
126                         $SVN_URL ||= $url;
127                         $repo_uuid = setup_git_svn();
128                         $first = $rev;
129                 }
130                 if ($uuid ne $repo_uuid) {
131                         croak "Repository UUIDs do not match!\ngot: $uuid\n",
132                                                 "expected: $repo_uuid\n";
133                 }
134                 assert_revision_eq_or_unknown($rev, $c);
135                 sys('git-update-ref',"$GIT_SVN/revs/$rev",$c);
136                 $newest_rev = $rev if ($rev > $newest_rev);
137         }
138         close $rev_list or croak $?;
139         if (!chdir $SVN_WC) {
140                 my @svn_co = ('svn','co',"-r$first");
141                 push @svn_co, '--ignore-externals' unless $_no_ignore_ext;
142                 sys(@svn_co, $SVN_URL, $SVN_WC);
143                 chdir $SVN_WC or croak $!;
144         }
145
146         $pid = fork;
147         defined $pid or croak $!;
148         if ($pid == 0) {
149                 my @svn_up = qw(svn up);
150                 push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
151                 sys(@svn_up,"-r$newest_rev");
152                 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
153                 git_addremove();
154                 exec('git-write-tree');
155         }
156         waitpid $pid, 0;
157 }
158
159 sub init {
160         $SVN_URL = shift or croak "SVN repository location required\n";
161         unless (-d $GIT_DIR) {
162                 sys('git-init-db');
163         }
164         setup_git_svn();
165 }
166
167 sub fetch {
168         my (@parents) = @_;
169         $SVN_URL ||= file_to_s("$GIT_DIR/$GIT_SVN/info/url");
170         my @log_args = -d $SVN_WC ? ($SVN_WC) : ($SVN_URL);
171         if (-d $SVN_WC && !$_revision) {
172                 $_revision = 'BASE:HEAD';
173         }
174         push @log_args, "-r$_revision" if $_revision;
175         push @log_args, '--stop-on-copy' unless $_no_stop_copy;
176
177         eval { require XML::Simple or croak $! };
178         my $svn_log = $@ ? svn_log_raw(@log_args) : svn_log_xml(@log_args);
179
180         my $base = shift @$svn_log or croak "No base revision!\n";
181         my $last_commit = undef;
182         unless (-d $SVN_WC) {
183                 my @svn_co = ('svn','co',"-r$base->{revision}");
184                 push @svn_co,'--ignore-externals' unless $_no_ignore_ext;
185                 sys(@svn_co, $SVN_URL, $SVN_WC);
186                 chdir $SVN_WC or croak $!;
187                 $last_commit = git_commit($base, @parents);
188                 unless (-f "$GIT_DIR/refs/heads/master") {
189                         sys(qw(git-update-ref refs/heads/master),$last_commit);
190                 }
191                 assert_svn_wc_clean($base->{revision}, $last_commit);
192         } else {
193                 chdir $SVN_WC or croak $!;
194                 $last_commit = file_to_s("$REV_DIR/$base->{revision}");
195         }
196         my @svn_up = qw(svn up);
197         push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
198         my $last_rev = $base->{revision};
199         foreach my $log_msg (@$svn_log) {
200                 assert_svn_wc_clean($last_rev, $last_commit);
201                 $last_rev = $log_msg->{revision};
202                 sys(@svn_up,"-r$last_rev");
203                 $last_commit = git_commit($log_msg, $last_commit, @parents);
204         }
205         assert_svn_wc_clean($last_rev, $last_commit);
206         return pop @$svn_log;
207 }
208
209 sub commit {
210         my (@commits) = @_;
211         if ($_stdin || !@commits) {
212                 print "Reading from stdin...\n";
213                 @commits = ();
214                 while (<STDIN>) {
215                         if (/^([a-f\d]{6,40})\b/) {
216                                 unshift @commits, $1;
217                         }
218                 }
219         }
220         my @revs;
221         foreach (@commits) {
222                 push @revs, (safe_qx('git-rev-parse',$_));
223         }
224         chomp @revs;
225
226         fetch();
227         chdir $SVN_WC or croak $!;
228         my $svn_current_rev =  svn_info('.')->{'Last Changed Rev'};
229         foreach my $c (@revs) {
230                 print "Committing $c\n";
231                 svn_checkout_tree($svn_current_rev, $c);
232                 $svn_current_rev = svn_commit_tree($svn_current_rev, $c);
233         }
234         print "Done committing ",scalar @revs," revisions to SVN\n";
235
236 }
237
238 ########################### utility functions #########################
239
240 sub setup_git_svn {
241         defined $SVN_URL or croak "SVN repository location required\n";
242         unless (-d $GIT_DIR) {
243                 croak "GIT_DIR=$GIT_DIR does not exist!\n";
244         }
245         mkpath(["$GIT_DIR/$GIT_SVN"]);
246         mkpath(["$GIT_DIR/$GIT_SVN/info"]);
247         mkpath([$REV_DIR]);
248         s_to_file($SVN_URL,"$GIT_DIR/$GIT_SVN/info/url");
249         my $uuid = svn_info($SVN_URL)->{'Repository UUID'} or
250                                         croak "Repository UUID unreadable\n";
251         s_to_file($uuid,"$GIT_DIR/$GIT_SVN/info/uuid");
252
253         open my $fd, '>>', "$GIT_DIR/$GIT_SVN/info/exclude" or croak $!;
254         print $fd '.svn',"\n";
255         close $fd or croak $!;
256         return $uuid;
257 }
258
259 sub assert_svn_wc_clean {
260         my ($svn_rev, $commit) = @_;
261         croak "$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
262         croak "$commit is not a sha1!\n" unless ($commit =~ /^$sha1$/o);
263         my $svn_info = svn_info('.');
264         if ($svn_rev != $svn_info->{'Last Changed Rev'}) {
265                 croak "Expected r$svn_rev, got r",
266                                 $svn_info->{'Last Changed Rev'},"\n";
267         }
268         my @status = grep(!/^Performing status on external/,(`svn status`));
269         @status = grep(!/^\s*$/,@status);
270         if (scalar @status) {
271                 print STDERR "Tree ($SVN_WC) is not clean:\n";
272                 print STDERR $_ foreach @status;
273                 croak;
274         }
275         my ($tree_a) = grep(/^tree $sha1$/o,`git-cat-file commit $commit`);
276         $tree_a =~ s/^tree //;
277         chomp $tree_a;
278         chomp(my $tree_b = `GIT_INDEX_FILE=$GIT_SVN_INDEX git-write-tree`);
279         if ($tree_a ne $tree_b) {
280                 croak "$svn_rev != $commit, $tree_a != $tree_b\n";
281         }
282 }
283
284 sub parse_diff_tree {
285         my $diff_fh = shift;
286         local $/ = "\0";
287         my $state = 'meta';
288         my @mods;
289         while (<$diff_fh>) {
290                 chomp $_; # this gets rid of the trailing "\0"
291                 print $_,"\n";
292                 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
293                                         $sha1\s($sha1)\s([MTCRAD])\d*$/xo) {
294                         push @mods, {   mode_a => $1, mode_b => $2,
295                                         sha1_b => $3, chg => $4 };
296                         if ($4 =~ /^(?:C|R)$/) {
297                                 $state = 'file_a';
298                         } else {
299                                 $state = 'file_b';
300                         }
301                 } elsif ($state eq 'file_a') {
302                         my $x = $mods[$#mods] or croak __LINE__,": Empty array\n";
303                         if ($x->{chg} !~ /^(?:C|R)$/) {
304                                 croak __LINE__,": Error parsing $_, $x->{chg}\n";
305                         }
306                         $x->{file_a} = $_;
307                         $state = 'file_b';
308                 } elsif ($state eq 'file_b') {
309                         my $x = $mods[$#mods] or croak __LINE__,": Empty array\n";
310                         if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
311                                 croak __LINE__,": Error parsing $_, $x->{chg}\n";
312                         }
313                         if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
314                                 croak __LINE__,": Error parsing $_, $x->{chg}\n";
315                         }
316                         $x->{file_b} = $_;
317                         $state = 'meta';
318                 } else {
319                         croak __LINE__,": Error parsing $_\n";
320                 }
321         }
322         close $diff_fh or croak $!;
323         return \@mods;
324 }
325
326 sub svn_check_prop_executable {
327         my $m = shift;
328         if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
329                 sys(qw(svn propset svn:executable 1), $m->{file_b});
330         } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
331                 sys(qw(svn propdel svn:executable), $m->{file_b});
332         }
333 }
334
335 sub svn_ensure_parent_path {
336         my $dir_b = dirname(shift);
337         svn_ensure_parent_path($dir_b) if ($dir_b ne File::Spec->curdir);
338         mkpath([$dir_b]) unless (-d $dir_b);
339         sys(qw(svn add -N), $dir_b) unless (-d "$dir_b/.svn");
340 }
341
342 sub svn_checkout_tree {
343         my ($svn_rev, $commit) = @_;
344         my $from = file_to_s("$REV_DIR/$svn_rev");
345         assert_svn_wc_clean($svn_rev,$from);
346         print "diff-tree '$from' '$commit'\n";
347         my $pid = open my $diff_fh, '-|';
348         defined $pid or croak $!;
349         if ($pid == 0) {
350                 exec(qw(git-diff-tree -z -r -C), $from, $commit) or croak $!;
351         }
352         my $mods = parse_diff_tree($diff_fh);
353         unless (@$mods) {
354                 # git can do empty commits, SVN doesn't allow it...
355                 return $svn_rev;
356         }
357         my %rm;
358         foreach my $m (@$mods) {
359                 if ($m->{chg} eq 'C') {
360                         svn_ensure_parent_path( $m->{file_b} );
361                         sys(qw(svn cp),         $m->{file_a}, $m->{file_b});
362                         blob_to_file(           $m->{sha1_b}, $m->{file_b});
363                         svn_check_prop_executable($m);
364                 } elsif ($m->{chg} eq 'D') {
365                         $rm{dirname $m->{file_b}}->{basename $m->{file_b}} = 1;
366                         sys(qw(svn rm --force), $m->{file_b});
367                 } elsif ($m->{chg} eq 'R') {
368                         svn_ensure_parent_path( $m->{file_b} );
369                         sys(qw(svn mv --force), $m->{file_a}, $m->{file_b});
370                         blob_to_file(           $m->{sha1_b}, $m->{file_b});
371                         svn_check_prop_executable($m);
372                         $rm{dirname $m->{file_a}}->{basename $m->{file_a}} = 1;
373                 } elsif ($m->{chg} eq 'M') {
374                         if ($m->{mode_b} =~ /^120/ && $m->{mode_a} =~ /^120/) {
375                                 unlink $m->{file_b} or croak $!;
376                                 blob_to_symlink($m->{sha1_b}, $m->{file_b});
377                         } else {
378                                 blob_to_file($m->{sha1_b}, $m->{file_b});
379                         }
380                         svn_check_prop_executable($m);
381                 } elsif ($m->{chg} eq 'T') {
382                         sys(qw(svn rm --force),$m->{file_b});
383                         if ($m->{mode_b} =~ /^120/ && $m->{mode_a} =~ /^100/) {
384                                 blob_to_symlink($m->{sha1_b}, $m->{file_b});
385                         } else {
386                                 blob_to_file($m->{sha1_b}, $m->{file_b});
387                         }
388                         svn_check_prop_executable($m);
389                         sys(qw(svn add --force), $m->{file_b});
390                 } elsif ($m->{chg} eq 'A') {
391                         svn_ensure_parent_path( $m->{file_b} );
392                         blob_to_file(           $m->{sha1_b}, $m->{file_b});
393                         if ($m->{mode_b} =~ /755$/) {
394                                 chmod 0755, $m->{file_b};
395                         }
396                         sys(qw(svn add --force), $m->{file_b});
397                 } else {
398                         croak "Invalid chg: $m->{chg}\n";
399                 }
400         }
401         if ($_rmdir) {
402                 my $old_index = $ENV{GIT_INDEX_FILE};
403                 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
404                 foreach my $dir (keys %rm) {
405                         my $files = $rm{$dir};
406                         my @files;
407                         foreach (safe_qx('svn','ls',$dir)) {
408                                 chomp;
409                                 push @files, $_ unless $files->{$_};
410                         }
411                         sys(qw(svn rm),$dir) unless @files;
412                 }
413                 if ($old_index) {
414                         $ENV{GIT_INDEX_FILE} = $old_index;
415                 } else {
416                         delete $ENV{GIT_INDEX_FILE};
417                 }
418         }
419 }
420
421 sub svn_commit_tree {
422         my ($svn_rev, $commit) = @_;
423         my $commit_msg = "$GIT_DIR/$GIT_SVN/.svn-commit.tmp.$$";
424         open my $msg, '>', $commit_msg  or croak $!;
425
426         chomp(my $type = `git-cat-file -t $commit`);
427         if ($type eq 'commit') {
428                 my $pid = open my $msg_fh, '-|';
429                 defined $pid or croak $!;
430
431                 if ($pid == 0) {
432                         exec(qw(git-cat-file commit), $commit) or croak $!;
433                 }
434                 my $in_msg = 0;
435                 while (<$msg_fh>) {
436                         if (!$in_msg) {
437                                 $in_msg = 1 if (/^\s*$/);
438                         } else {
439                                 print $msg $_ or croak $!;
440                         }
441                 }
442                 close $msg_fh or croak $!;
443         }
444         close $msg or croak $!;
445
446         if ($_edit || ($type eq 'tree')) {
447                 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
448                 system($editor, $commit_msg);
449         }
450         my @ci_output = safe_qx(qw(svn commit -F),$commit_msg);
451         my ($committed) = grep(/^Committed revision \d+\./,@ci_output);
452         unlink $commit_msg;
453         defined $committed or croak
454                         "Commit output failed to parse committed revision!\n",
455                         join("\n",@ci_output),"\n";
456         my ($rev_committed) = ($committed =~ /^Committed revision (\d+)\./);
457
458         # resync immediately
459         my @svn_up = (qw(svn up), "-r$svn_rev");
460         push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
461         sys(@svn_up);
462         return fetch("$rev_committed=$commit")->{revision};
463 }
464
465 sub svn_log_xml {
466         my (@log_args) = @_;
467         my $log_fh = IO::File->new_tmpfile or croak $!;
468
469         my $pid = fork;
470         defined $pid or croak $!;
471
472         if ($pid == 0) {
473                 open STDOUT, '>&', $log_fh or croak $!;
474                 exec (qw(svn log --xml), @log_args) or croak $!
475         }
476
477         waitpid $pid, 0;
478         croak $? if $?;
479
480         seek $log_fh, 0, 0;
481         my @svn_log;
482         my $log = XML::Simple::XMLin( $log_fh,
483                                 ForceArray => ['path','revision','logentry'],
484                                 KeepRoot => 0,
485                                 KeyAttr => {    logentry => '+revision',
486                                                 paths => '+path' },
487                         )->{logentry};
488         foreach my $r (sort {$a <=> $b} keys %$log) {
489                 my $log_msg = $log->{$r};
490                 my ($Y,$m,$d,$H,$M,$S) = ($log_msg->{date} =~
491                                         /(\d{4})\-(\d\d)\-(\d\d)T
492                                          (\d\d)\:(\d\d)\:(\d\d)\.\d+Z$/x)
493                                          or croak "Failed to parse date: ",
494                                                  $log->{$r}->{date};
495                 $log_msg->{date} = "+0000 $Y-$m-$d $H:$M:$S";
496
497                 # XML::Simple can't handle <msg></msg> as a string:
498                 if (ref $log_msg->{msg} eq 'HASH') {
499                         $log_msg->{msg} = "\n";
500                 } else {
501                         $log_msg->{msg} .= "\n";
502                 }
503                 push @svn_log, $log->{$r};
504         }
505         return \@svn_log;
506 }
507
508 sub svn_log_raw {
509         my (@log_args) = @_;
510         my $pid = open my $log_fh,'-|';
511         defined $pid or croak $!;
512
513         if ($pid == 0) {
514                 exec (qw(svn log), @log_args) or croak $!
515         }
516
517         my @svn_log;
518         my $state;
519         while (<$log_fh>) {
520                 chomp;
521                 if (/^\-{72}$/) {
522                         $state = 'rev';
523
524                         # if we have an empty log message, put something there:
525                         if (@svn_log) {
526                                 $svn_log[0]->{msg} ||= "\n";
527                         }
528                         next;
529                 }
530                 if ($state eq 'rev' && s/^r(\d+)\s*\|\s*//) {
531                         my $rev = $1;
532                         my ($author, $date) = split(/\s*\|\s*/, $_, 2);
533                         my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
534                                         /(\d{4})\-(\d\d)\-(\d\d)\s
535                                          (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
536                                          or croak "Failed to parse date: $date\n";
537                         my %log_msg = ( revision => $rev,
538                                         date => "$tz $Y-$m-$d $H:$M:$S",
539                                         author => $author,
540                                         msg => '' );
541                         unshift @svn_log, \%log_msg;
542                         $state = 'msg_start';
543                         next;
544                 }
545                 # skip the first blank line of the message:
546                 if ($state eq 'msg_start' && /^$/) {
547                         $state = 'msg';
548                 } elsif ($state eq 'msg') {
549                         $svn_log[0]->{msg} .= $_."\n";
550                 }
551         }
552         close $log_fh or croak $?;
553         return \@svn_log;
554 }
555
556 sub svn_info {
557         my $url = shift || $SVN_URL;
558
559         my $pid = open my $info_fh, '-|';
560         defined $pid or croak $!;
561
562         if ($pid == 0) {
563                 exec(qw(svn info),$url) or croak $!;
564         }
565
566         my $ret = {};
567         # only single-lines seem to exist in svn info output
568         while (<$info_fh>) {
569                 chomp $_;
570                 if (m#^([^:]+)\s*:\s*(\S*)$#) {
571                         $ret->{$1} = $2;
572                         push @{$ret->{-order}}, $1;
573                 }
574         }
575         close $info_fh or croak $!;
576         return $ret;
577 }
578
579 sub sys { system(@_) == 0 or croak $? }
580
581 sub git_addremove {
582         system( "git-ls-files -z --others ".
583                         "'--exclude-from=$GIT_DIR/$GIT_SVN/info/exclude'".
584                                 "| git-update-index --add -z --stdin; ".
585                 "git-ls-files -z --deleted ".
586                                 "| git-update-index --remove -z --stdin; ".
587                 "git-ls-files -z --modified".
588                                 "| git-update-index -z --stdin") == 0 or croak $?
589 }
590
591 sub s_to_file {
592         my ($str, $file, $mode) = @_;
593         open my $fd,'>',$file or croak $!;
594         print $fd $str,"\n" or croak $!;
595         close $fd or croak $!;
596         chmod ($mode &~ umask, $file) if (defined $mode);
597 }
598
599 sub file_to_s {
600         my $file = shift;
601         open my $fd,'<',$file or croak "$!: file: $file\n";
602         local $/;
603         my $ret = <$fd>;
604         close $fd or croak $!;
605         $ret =~ s/\s*$//s;
606         return $ret;
607 }
608
609 sub assert_revision_unknown {
610         my $revno = shift;
611         if (-f "$REV_DIR/$revno") {
612                 croak "$REV_DIR/$revno already exists! ",
613                                 "Why are we refetching it?";
614         }
615 }
616
617 sub assert_revision_eq_or_unknown {
618         my ($revno, $commit) = @_;
619         if (-f "$REV_DIR/$revno") {
620                 my $current = file_to_s("$REV_DIR/$revno");
621                 if ($commit ne $current) {
622                         croak "$REV_DIR/$revno already exists!\n",
623                                 "current: $current\nexpected: $commit\n";
624                 }
625                 return;
626         }
627 }
628
629 sub git_commit {
630         my ($log_msg, @parents) = @_;
631         assert_revision_unknown($log_msg->{revision});
632         my $out_fh = IO::File->new_tmpfile or croak $!;
633         my $info = svn_info('.');
634         my $uuid = $info->{'Repository UUID'};
635         defined $uuid or croak "Unable to get Repository UUID\n";
636
637         # commit parents can be conditionally bound to a particular
638         # svn revision via: "svn_revno=commit_sha1", filter them out here:
639         my @exec_parents;
640         foreach my $p (@parents) {
641                 next unless defined $p;
642                 if ($p =~ /^(\d+)=($sha1_short)$/o) {
643                         if ($1 == $log_msg->{revision}) {
644                                 push @exec_parents, $2;
645                         }
646                 } else {
647                         push @exec_parents, $p if $p =~ /$sha1_short/o;
648                 }
649         }
650
651         my $pid = fork;
652         defined $pid or croak $!;
653         if ($pid == 0) {
654                 $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
655                 git_addremove();
656                 chomp(my $tree = `git-write-tree`);
657                 croak if $?;
658                 my $msg_fh = IO::File->new_tmpfile or croak $!;
659                 print $msg_fh $log_msg->{msg}, "\ngit-svn-id: ",
660                                         "$SVN_URL\@$log_msg->{revision}",
661                                         " $uuid\n" or croak $!;
662                 $msg_fh->flush == 0 or croak $!;
663                 seek $msg_fh, 0, 0 or croak $!;
664
665                 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} =
666                                                 $log_msg->{author};
667                 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} =
668                                                 $log_msg->{author}."\@$uuid";
669                 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} =
670                                                 $log_msg->{date};
671                 my @exec = ('git-commit-tree',$tree);
672                 push @exec, '-p', $_  foreach @exec_parents;
673                 open STDIN, '<&', $msg_fh or croak $!;
674                 open STDOUT, '>&', $out_fh or croak $!;
675                 exec @exec or croak $!;
676         }
677         waitpid($pid,0);
678         croak if $?;
679
680         $out_fh->flush == 0 or croak $!;
681         seek $out_fh, 0, 0 or croak $!;
682         chomp(my $commit = do { local $/; <$out_fh> });
683         if ($commit !~ /^$sha1$/o) {
684                 croak "Failed to commit, invalid sha1: $commit\n";
685         }
686         my @update_ref = ('git-update-ref',"refs/heads/$GIT_SVN-HEAD",$commit);
687         if (my $primary_parent = shift @exec_parents) {
688                 push @update_ref, $primary_parent;
689         }
690         sys(@update_ref);
691         sys('git-update-ref',"$GIT_SVN/revs/$log_msg->{revision}",$commit);
692         print "r$log_msg->{revision} = $commit\n";
693         return $commit;
694 }
695
696 sub blob_to_symlink {
697         my ($blob, $link) = @_;
698         defined $link or croak "\$link not defined!\n";
699         croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
700         my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
701         symlink $dest, $link or croak $!;
702 }
703
704 sub blob_to_file {
705         my ($blob, $file) = @_;
706         defined $file or croak "\$file not defined!\n";
707         croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
708         open my $blob_fh, '>', $file or croak "$!: $file\n";
709         my $pid = fork;
710         defined $pid or croak $!;
711
712         if ($pid == 0) {
713                 open STDOUT, '>&', $blob_fh or croak $!;
714                 exec('git-cat-file','blob',$blob);
715         }
716         waitpid $pid, 0;
717         croak $? if $?;
718
719         close $blob_fh or croak $!;
720 }
721
722 sub safe_qx {
723         my $pid = open my $child, '-|';
724         defined $pid or croak $!;
725         if ($pid == 0) {
726                 exec(@_) or croak $?;
727         }
728         my @ret = (<$child>);
729         close $child or croak $?;
730         die $? if $?; # just in case close didn't error out
731         return wantarray ? @ret : join('',@ret);
732 }
733
734 sub svn_check_ignore_externals {
735         return if $_no_ignore_ext;
736         unless (grep /ignore-externals/,(safe_qx(qw(svn co -h)))) {
737                 print STDERR "W: Installed svn version does not support ",
738                                 "--ignore-externals\n";
739                 $_no_ignore_ext = 1;
740         }
741 }
742 __END__
743
744 Data structures:
745
746 @svn_log = array of log_msg hashes
747
748 $log_msg hash
749 {
750         msg => 'whitespace-formatted log entry
751 ',                                              # trailing newline is preserved
752         revision => '8',                        # integer
753         date => '2004-02-24T17:01:44.108345Z',  # commit date
754         author => 'committer name'
755 };
756
757
758 @mods = array of diff-index line hashes, each element represents one line
759         of diff-index output
760
761 diff-index line ($m hash)
762 {
763         mode_a => first column of diff-index output, no leading ':',
764         mode_b => second column of diff-index output,
765         sha1_b => sha1sum of the final blob,
766         chg => change type [MCRAD],
767         file_a => original file name of a file (iff chg is 'C' or 'R')
768         file_b => new/current file name of a file (any chg)
769 }
770 ;