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