Builtin git-read-tree.
[debian-git.git] / git-svnimport.perl
blob61f559f0a8ae69c04cfe9d8700591cedda41989c
1 #!/usr/bin/perl -w
3 # This tool is copyright (c) 2005, Matthias Urlichs.
4 # It is released under the Gnu Public License, version 2.
6 # The basic idea is to pull and analyze SVN changes.
8 # Checking out the files is done by a single long-running SVN connection.
10 # The head revision is on branch "origin" by default.
11 # You can change that with the '-o' option.
13 use strict;
14 use warnings;
15 use Getopt::Std;
16 use File::Copy;
17 use File::Spec;
18 use File::Temp qw(tempfile);
19 use File::Path qw(mkpath);
20 use File::Basename qw(basename dirname);
21 use Time::Local;
22 use IO::Pipe;
23 use POSIX qw(strftime dup2);
24 use IPC::Open2;
25 use SVN::Core;
26 use SVN::Ra;
28 die "Need SVN:Core 1.2.1 or better" if $SVN::Core::VERSION lt "1.2.1";
30 $SIG{'PIPE'}="IGNORE";
31 $ENV{'TZ'}="UTC";
33 our($opt_h,$opt_o,$opt_v,$opt_u,$opt_C,$opt_i,$opt_m,$opt_M,$opt_t,$opt_T,
34 $opt_b,$opt_r,$opt_I,$opt_A,$opt_s,$opt_l,$opt_d,$opt_D);
36 sub usage() {
37 print STDERR <<END;
38 Usage: ${\basename $0} # fetch/update GIT from SVN
39 [-o branch-for-HEAD] [-h] [-v] [-l max_rev]
40 [-C GIT_repository] [-t tagname] [-T trunkname] [-b branchname]
41 [-d|-D] [-i] [-u] [-r] [-I ignorefilename] [-s start_chg]
42 [-m] [-M regex] [-A author_file] [SVN_URL]
43 END
44 exit(1);
47 getopts("A:b:C:dDhiI:l:mM:o:rs:t:T:uv") or usage();
48 usage if $opt_h;
50 my $tag_name = $opt_t || "tags";
51 my $trunk_name = $opt_T || "trunk";
52 my $branch_name = $opt_b || "branches";
54 @ARGV == 1 or @ARGV == 2 or usage();
56 $opt_o ||= "origin";
57 $opt_s ||= 1;
58 my $git_tree = $opt_C;
59 $git_tree ||= ".";
61 my $svn_url = $ARGV[0];
62 my $svn_dir = $ARGV[1];
64 our @mergerx = ();
65 if ($opt_m) {
66 @mergerx = ( qr/\W(?:from|of|merge|merging|merged) (\w+)/i );
68 if ($opt_M) {
69 push (@mergerx, qr/$opt_M/);
72 # Absolutize filename now, since we will have chdir'ed by the time we
73 # get around to opening it.
74 $opt_A = File::Spec->rel2abs($opt_A) if $opt_A;
76 our %users = ();
77 our $users_file = undef;
78 sub read_users($) {
79 $users_file = File::Spec->rel2abs(@_);
80 die "Cannot open $users_file\n" unless -f $users_file;
81 open(my $authors,$users_file);
82 while(<$authors>) {
83 chomp;
84 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
85 (my $user,my $name,my $email) = ($1,$2,$3);
86 $users{$user} = [$name,$email];
88 close($authors);
91 select(STDERR); $|=1; select(STDOUT);
94 package SVNconn;
95 # Basic SVN connection.
96 # We're only interested in connecting and downloading, so ...
98 use File::Spec;
99 use File::Temp qw(tempfile);
100 use POSIX qw(strftime dup2);
101 use Fcntl qw(SEEK_SET);
103 sub new {
104 my($what,$repo) = @_;
105 $what=ref($what) if ref($what);
107 my $self = {};
108 $self->{'buffer'} = "";
109 bless($self,$what);
111 $repo =~ s#/+$##;
112 $self->{'fullrep'} = $repo;
113 $self->conn();
115 return $self;
118 sub conn {
119 my $self = shift;
120 my $repo = $self->{'fullrep'};
121 my $auth = SVN::Core::auth_open ([SVN::Client::get_simple_provider,
122 SVN::Client::get_ssl_server_trust_file_provider,
123 SVN::Client::get_username_provider]);
124 my $s = SVN::Ra->new(url => $repo, auth => $auth);
125 die "SVN connection to $repo: $!\n" unless defined $s;
126 $self->{'svn'} = $s;
127 $self->{'repo'} = $repo;
128 $self->{'maxrev'} = $s->get_latest_revnum();
131 sub file {
132 my($self,$path,$rev) = @_;
134 my ($fh, $name) = tempfile('gitsvn.XXXXXX',
135 DIR => File::Spec->tmpdir(), UNLINK => 1);
137 print "... $rev $path ...\n" if $opt_v;
138 my (undef, $properties);
139 my $pool = SVN::Pool->new();
140 eval { (undef, $properties)
141 = $self->{'svn'}->get_file($path,$rev,$fh,$pool); };
142 $pool->clear;
143 if($@) {
144 return undef if $@ =~ /Attempted to get checksum/;
145 die $@;
147 my $mode;
148 if (exists $properties->{'svn:executable'}) {
149 $mode = '100755';
150 } elsif (exists $properties->{'svn:special'}) {
151 my ($special_content, $filesize);
152 $filesize = tell $fh;
153 seek $fh, 0, SEEK_SET;
154 read $fh, $special_content, $filesize;
155 if ($special_content =~ s/^link //) {
156 $mode = '120000';
157 seek $fh, 0, SEEK_SET;
158 truncate $fh, 0;
159 print $fh $special_content;
160 } else {
161 die "unexpected svn:special file encountered";
163 } else {
164 $mode = '100644';
166 close ($fh);
168 return ($name, $mode);
171 sub ignore {
172 my($self,$path,$rev) = @_;
174 print "... $rev $path ...\n" if $opt_v;
175 my (undef,undef,$properties)
176 = $self->{'svn'}->get_dir($path,$rev,undef);
177 if (exists $properties->{'svn:ignore'}) {
178 my ($fh, $name) = tempfile('gitsvn.XXXXXX',
179 DIR => File::Spec->tmpdir(),
180 UNLINK => 1);
181 print $fh $properties->{'svn:ignore'};
182 close($fh);
183 return $name;
184 } else {
185 return undef;
189 package main;
190 use URI;
192 our $svn = $svn_url;
193 $svn .= "/$svn_dir" if defined $svn_dir;
194 my $svn2 = SVNconn->new($svn);
195 $svn = SVNconn->new($svn);
197 my $lwp_ua;
198 if($opt_d or $opt_D) {
199 $svn_url = URI->new($svn_url)->canonical;
200 if($opt_D) {
201 $svn_dir =~ s#/*$#/#;
202 } else {
203 $svn_dir = "";
205 if ($svn_url->scheme eq "http") {
206 use LWP::UserAgent;
207 $lwp_ua = LWP::UserAgent->new(keep_alive => 1, requests_redirectable => []);
208 } else {
209 print STDERR "Warning: not HTTP; turning off direct file access\n";
210 $opt_d=0;
214 sub pdate($) {
215 my($d) = @_;
216 $d =~ m#(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)#
217 or die "Unparseable date: $d\n";
218 my $y=$1; $y-=1900 if $y>1900;
219 return timegm($6||0,$5,$4,$3,$2-1,$y);
222 sub getwd() {
223 my $pwd = `pwd`;
224 chomp $pwd;
225 return $pwd;
229 sub get_headref($$) {
230 my $name = shift;
231 my $git_dir = shift;
232 my $sha;
234 if (open(C,"$git_dir/refs/heads/$name")) {
235 chomp($sha = <C>);
236 close(C);
237 length($sha) == 40
238 or die "Cannot get head id for $name ($sha): $!\n";
240 return $sha;
244 -d $git_tree
245 or mkdir($git_tree,0777)
246 or die "Could not create $git_tree: $!";
247 chdir($git_tree);
249 my $orig_branch = "";
250 my $forward_master = 0;
251 my %branches;
253 my $git_dir = $ENV{"GIT_DIR"} || ".git";
254 $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#;
255 $ENV{"GIT_DIR"} = $git_dir;
256 my $orig_git_index;
257 $orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE};
258 my ($git_ih, $git_index) = tempfile('gitXXXXXX', SUFFIX => '.idx',
259 DIR => File::Spec->tmpdir());
260 close ($git_ih);
261 $ENV{GIT_INDEX_FILE} = $git_index;
262 my $maxnum = 0;
263 my $last_rev = "";
264 my $last_branch;
265 my $current_rev = $opt_s || 1;
266 unless(-d $git_dir) {
267 system("git-init-db");
268 die "Cannot init the GIT db at $git_tree: $?\n" if $?;
269 system("git-read-tree");
270 die "Cannot init an empty tree: $?\n" if $?;
272 $last_branch = $opt_o;
273 $orig_branch = "";
274 } else {
275 -f "$git_dir/refs/heads/$opt_o"
276 or die "Branch '$opt_o' does not exist.\n".
277 "Either use the correct '-o branch' option,\n".
278 "or import to a new repository.\n";
280 -f "$git_dir/svn2git"
281 or die "'$git_dir/svn2git' does not exist.\n".
282 "You need that file for incremental imports.\n";
283 open(F, "git-symbolic-ref HEAD |") or
284 die "Cannot run git-symbolic-ref: $!\n";
285 chomp ($last_branch = <F>);
286 $last_branch = basename($last_branch);
287 close(F);
288 unless($last_branch) {
289 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
290 $last_branch = "master";
292 $orig_branch = $last_branch;
293 $last_rev = get_headref($orig_branch, $git_dir);
294 if (-f "$git_dir/SVN2GIT_HEAD") {
295 die <<EOM;
296 SVN2GIT_HEAD exists.
297 Make sure your working directory corresponds to HEAD and remove SVN2GIT_HEAD.
298 You may need to run
300 git-read-tree -m -u SVN2GIT_HEAD HEAD
303 system('cp', "$git_dir/HEAD", "$git_dir/SVN2GIT_HEAD");
305 $forward_master =
306 $opt_o ne 'master' && -f "$git_dir/refs/heads/master" &&
307 system('cmp', '-s', "$git_dir/refs/heads/master",
308 "$git_dir/refs/heads/$opt_o") == 0;
310 # populate index
311 system('git-read-tree', $last_rev);
312 die "read-tree failed: $?\n" if $?;
314 # Get the last import timestamps
315 open my $B,"<", "$git_dir/svn2git";
316 while(<$B>) {
317 chomp;
318 my($num,$branch,$ref) = split;
319 $branches{$branch}{$num} = $ref;
320 $branches{$branch}{"LAST"} = $ref;
321 $current_rev = $num+1 if $current_rev <= $num;
323 close($B);
325 -d $git_dir
326 or die "Could not create git subdir ($git_dir).\n";
328 my $default_authors = "$git_dir/svn-authors";
329 if ($opt_A) {
330 read_users($opt_A);
331 copy($opt_A,$default_authors) or die "Copy failed: $!";
332 } else {
333 read_users($default_authors) if -f $default_authors;
336 open BRANCHES,">>", "$git_dir/svn2git";
338 sub node_kind($$$) {
339 my ($branch, $path, $revision) = @_;
340 my $pool=SVN::Pool->new;
341 my $kind = $svn->{'svn'}->check_path(revert_split_path($branch,$path),$revision,$pool);
342 $pool->clear;
343 return $kind;
346 sub revert_split_path($$) {
347 my($branch,$path) = @_;
349 my $svnpath;
350 $path = "" if $path eq "/"; # this should not happen, but ...
351 if($branch eq "/") {
352 $svnpath = "$trunk_name/$path";
353 } elsif($branch =~ m#^/#) {
354 $svnpath = "$tag_name$branch/$path";
355 } else {
356 $svnpath = "$branch_name/$branch/$path";
359 $svnpath =~ s#/+$##;
360 return $svnpath;
363 sub get_file($$$) {
364 my($rev,$branch,$path) = @_;
366 my $svnpath = revert_split_path($branch,$path);
368 # now get it
369 my ($name,$mode);
370 if($opt_d) {
371 my($req,$res);
373 # /svn/!svn/bc/2/django/trunk/django-docs/build.py
374 my $url=$svn_url->clone();
375 $url->path($url->path."/!svn/bc/$rev/$svn_dir$svnpath");
376 print "... $path...\n" if $opt_v;
377 $req = HTTP::Request->new(GET => $url);
378 $res = $lwp_ua->request($req);
379 if ($res->is_success) {
380 my $fh;
381 ($fh, $name) = tempfile('gitsvn.XXXXXX',
382 DIR => File::Spec->tmpdir(), UNLINK => 1);
383 print $fh $res->content;
384 close($fh) or die "Could not write $name: $!\n";
385 } else {
386 return undef if $res->code == 301; # directory?
387 die $res->status_line." at $url\n";
389 $mode = '0644'; # can't obtain mode via direct http request?
390 } else {
391 ($name,$mode) = $svn->file("$svnpath",$rev);
392 return undef unless defined $name;
395 my $pid = open(my $F, '-|');
396 die $! unless defined $pid;
397 if (!$pid) {
398 exec("git-hash-object", "-w", $name)
399 or die "Cannot create object: $!\n";
401 my $sha = <$F>;
402 chomp $sha;
403 close $F;
404 unlink $name;
405 return [$mode, $sha, $path];
408 sub get_ignore($$$$$) {
409 my($new,$old,$rev,$branch,$path) = @_;
411 return unless $opt_I;
412 my $svnpath = revert_split_path($branch,$path);
413 my $name = $svn->ignore("$svnpath",$rev);
414 if ($path eq '/') {
415 $path = $opt_I;
416 } else {
417 $path = File::Spec->catfile($path,$opt_I);
419 if (defined $name) {
420 my $pid = open(my $F, '-|');
421 die $! unless defined $pid;
422 if (!$pid) {
423 exec("git-hash-object", "-w", $name)
424 or die "Cannot create object: $!\n";
426 my $sha = <$F>;
427 chomp $sha;
428 close $F;
429 unlink $name;
430 push(@$new,['0644',$sha,$path]);
431 } else {
432 push(@$old,$path);
436 sub split_path($$) {
437 my($rev,$path) = @_;
438 my $branch;
440 if($path =~ s#^/\Q$tag_name\E/([^/]+)/?##) {
441 $branch = "/$1";
442 } elsif($path =~ s#^/\Q$trunk_name\E/?##) {
443 $branch = "/";
444 } elsif($path =~ s#^/\Q$branch_name\E/([^/]+)/?##) {
445 $branch = $1;
446 } else {
447 my %no_error = (
448 "/" => 1,
449 "/$tag_name" => 1,
450 "/$branch_name" => 1
452 print STDERR "$rev: Unrecognized path: $path\n" unless (defined $no_error{$path});
453 return ()
455 $path = "/" if $path eq "";
456 return ($branch,$path);
459 sub branch_rev($$) {
461 my ($srcbranch,$uptorev) = @_;
463 my $bbranches = $branches{$srcbranch};
464 my @revs = reverse sort { ($a eq 'LAST' ? 0 : $a) <=> ($b eq 'LAST' ? 0 : $b) } keys %$bbranches;
465 my $therev;
466 foreach my $arev(@revs) {
467 next if ($arev eq 'LAST');
468 if ($arev <= $uptorev) {
469 $therev = $arev;
470 last;
473 return $therev;
476 sub copy_path($$$$$$$$) {
477 # Somebody copied a whole subdirectory.
478 # We need to find the index entries from the old version which the
479 # SVN log entry points to, and add them to the new place.
481 my($newrev,$newbranch,$path,$oldpath,$rev,$node_kind,$new,$parents) = @_;
483 my($srcbranch,$srcpath) = split_path($rev,$oldpath);
484 unless(defined $srcbranch) {
485 print "Path not found when copying from $oldpath @ $rev\n";
486 return;
488 my $therev = branch_rev($srcbranch, $rev);
489 my $gitrev = $branches{$srcbranch}{$therev};
490 unless($gitrev) {
491 print STDERR "$newrev:$newbranch: could not find $oldpath \@ $rev\n";
492 return;
494 if ($srcbranch ne $newbranch) {
495 push(@$parents, $branches{$srcbranch}{'LAST'});
497 print "$newrev:$newbranch:$path: copying from $srcbranch:$srcpath @ $rev\n" if $opt_v;
498 if ($node_kind eq $SVN::Node::dir) {
499 $srcpath =~ s#/*$#/#;
502 my $pid = open my $f,'-|';
503 die $! unless defined $pid;
504 if (!$pid) {
505 exec("git-ls-tree","-r","-z",$gitrev,$srcpath)
506 or die $!;
508 local $/ = "\0";
509 while(<$f>) {
510 chomp;
511 my($m,$p) = split(/\t/,$_,2);
512 my($mode,$type,$sha1) = split(/ /,$m);
513 next if $type ne "blob";
514 if ($node_kind eq $SVN::Node::dir) {
515 $p = $path . substr($p,length($srcpath)-1);
516 } else {
517 $p = $path;
519 push(@$new,[$mode,$sha1,$p]);
521 close($f) or
522 print STDERR "$newrev:$newbranch: could not list files in $oldpath \@ $rev\n";
525 sub commit {
526 my($branch, $changed_paths, $revision, $author, $date, $message) = @_;
527 my($author_name,$author_email,$dest);
528 my(@old,@new,@parents);
530 if (not defined $author) {
531 $author_name = $author_email = "unknown";
532 } elsif (defined $users_file) {
533 die "User $author is not listed in $users_file\n"
534 unless exists $users{$author};
535 ($author_name,$author_email) = @{$users{$author}};
536 } elsif ($author =~ /^(.*?)\s+<(.*)>$/) {
537 ($author_name, $author_email) = ($1, $2);
538 } else {
539 $author =~ s/^<(.*)>$/$1/;
540 $author_name = $author_email = $author;
542 $date = pdate($date);
544 my $tag;
545 my $parent;
546 if($branch eq "/") { # trunk
547 $parent = $opt_o;
548 } elsif($branch =~ m#^/(.+)#) { # tag
549 $tag = 1;
550 $parent = $1;
551 } else { # "normal" branch
552 # nothing to do
553 $parent = $branch;
555 $dest = $parent;
557 my $prev = $changed_paths->{"/"};
558 if($prev and $prev->[0] eq "A") {
559 delete $changed_paths->{"/"};
560 my $oldpath = $prev->[1];
561 my $rev;
562 if(defined $oldpath) {
563 my $p;
564 ($parent,$p) = split_path($revision,$oldpath);
565 if($parent eq "/") {
566 $parent = $opt_o;
567 } else {
568 $parent =~ s#^/##; # if it's a tag
570 } else {
571 $parent = undef;
575 my $rev;
576 if($revision > $opt_s and defined $parent) {
577 open(H,"git-rev-parse --verify $parent |");
578 $rev = <H>;
579 close(H) or do {
580 print STDERR "$revision: cannot find commit '$parent'!\n";
581 return;
583 chop $rev;
584 if(length($rev) != 40) {
585 print STDERR "$revision: cannot find commit '$parent'!\n";
586 return;
588 $rev = $branches{($parent eq $opt_o) ? "/" : $parent}{"LAST"};
589 if($revision != $opt_s and not $rev) {
590 print STDERR "$revision: do not know ancestor for '$parent'!\n";
591 return;
593 } else {
594 $rev = undef;
597 # if($prev and $prev->[0] eq "A") {
598 # if(not $tag) {
599 # unless(open(H,"> $git_dir/refs/heads/$branch")) {
600 # print STDERR "$revision: Could not create branch $branch: $!\n";
601 # $state=11;
602 # next;
604 # print H "$rev\n"
605 # or die "Could not write branch $branch: $!";
606 # close(H)
607 # or die "Could not write branch $branch: $!";
610 if(not defined $rev) {
611 unlink($git_index);
612 } elsif ($rev ne $last_rev) {
613 print "Switching from $last_rev to $rev ($branch)\n" if $opt_v;
614 system("git-read-tree", $rev);
615 die "read-tree failed for $rev: $?\n" if $?;
616 $last_rev = $rev;
619 push (@parents, $rev) if defined $rev;
621 my $cid;
622 if($tag and not %$changed_paths) {
623 $cid = $rev;
624 } else {
625 my @paths = sort keys %$changed_paths;
626 foreach my $path(@paths) {
627 my $action = $changed_paths->{$path};
629 if ($action->[0] eq "R") {
630 # refer to a file/tree in an earlier commit
631 push(@old,$path); # remove any old stuff
633 if(($action->[0] eq "A") || ($action->[0] eq "R")) {
634 my $node_kind = node_kind($branch,$path,$revision);
635 if ($node_kind eq $SVN::Node::file) {
636 my $f = get_file($revision,$branch,$path);
637 if ($f) {
638 push(@new,$f) if $f;
639 } else {
640 my $opath = $action->[3];
641 print STDERR "$revision: $branch: could not fetch '$opath'\n";
643 } elsif ($node_kind eq $SVN::Node::dir) {
644 if($action->[1]) {
645 copy_path($revision, $branch,
646 $path, $action->[1],
647 $action->[2], $node_kind,
648 \@new, \@parents);
649 } else {
650 get_ignore(\@new, \@old, $revision,
651 $branch, $path);
654 } elsif ($action->[0] eq "D") {
655 push(@old,$path);
656 } elsif ($action->[0] eq "M") {
657 my $node_kind = node_kind($branch,$path,$revision);
658 if ($node_kind eq $SVN::Node::file) {
659 my $f = get_file($revision,$branch,$path);
660 push(@new,$f) if $f;
661 } elsif ($node_kind eq $SVN::Node::dir) {
662 get_ignore(\@new, \@old, $revision,
663 $branch,$path);
665 } else {
666 die "$revision: unknown action '".$action->[0]."' for $path\n";
670 while(@old) {
671 my @o1;
672 if(@old > 55) {
673 @o1 = splice(@old,0,50);
674 } else {
675 @o1 = @old;
676 @old = ();
678 my $pid = open my $F, "-|";
679 die "$!" unless defined $pid;
680 if (!$pid) {
681 exec("git-ls-files", "-z", @o1) or die $!;
683 @o1 = ();
684 local $/ = "\0";
685 while(<$F>) {
686 chomp;
687 push(@o1,$_);
689 close($F);
691 while(@o1) {
692 my @o2;
693 if(@o1 > 55) {
694 @o2 = splice(@o1,0,50);
695 } else {
696 @o2 = @o1;
697 @o1 = ();
699 system("git-update-index","--force-remove","--",@o2);
700 die "Cannot remove files: $?\n" if $?;
703 while(@new) {
704 my @n2;
705 if(@new > 12) {
706 @n2 = splice(@new,0,10);
707 } else {
708 @n2 = @new;
709 @new = ();
711 system("git-update-index","--add",
712 (map { ('--cacheinfo', @$_) } @n2));
713 die "Cannot add files: $?\n" if $?;
716 my $pid = open(C,"-|");
717 die "Cannot fork: $!" unless defined $pid;
718 unless($pid) {
719 exec("git-write-tree");
720 die "Cannot exec git-write-tree: $!\n";
722 chomp(my $tree = <C>);
723 length($tree) == 40
724 or die "Cannot get tree id ($tree): $!\n";
725 close(C)
726 or die "Error running git-write-tree: $?\n";
727 print "Tree ID $tree\n" if $opt_v;
729 my $pr = IO::Pipe->new() or die "Cannot open pipe: $!\n";
730 my $pw = IO::Pipe->new() or die "Cannot open pipe: $!\n";
731 $pid = fork();
732 die "Fork: $!\n" unless defined $pid;
733 unless($pid) {
734 $pr->writer();
735 $pw->reader();
736 open(OUT,">&STDOUT");
737 dup2($pw->fileno(),0);
738 dup2($pr->fileno(),1);
739 $pr->close();
740 $pw->close();
742 my @par = ();
744 # loose detection of merges
745 # based on the commit msg
746 foreach my $rx (@mergerx) {
747 if ($message =~ $rx) {
748 my $mparent = $1;
749 if ($mparent eq 'HEAD') { $mparent = $opt_o };
750 if ( -e "$git_dir/refs/heads/$mparent") {
751 $mparent = get_headref($mparent, $git_dir);
752 push (@parents, $mparent);
753 print OUT "Merge parent branch: $mparent\n" if $opt_v;
757 my %seen_parents = ();
758 my @unique_parents = grep { ! $seen_parents{$_} ++ } @parents;
759 foreach my $bparent (@unique_parents) {
760 push @par, '-p', $bparent;
761 print OUT "Merge parent branch: $bparent\n" if $opt_v;
764 exec("env",
765 "GIT_AUTHOR_NAME=$author_name",
766 "GIT_AUTHOR_EMAIL=$author_email",
767 "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
768 "GIT_COMMITTER_NAME=$author_name",
769 "GIT_COMMITTER_EMAIL=$author_email",
770 "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
771 "git-commit-tree", $tree,@par);
772 die "Cannot exec git-commit-tree: $!\n";
774 $pw->writer();
775 $pr->reader();
777 $message =~ s/[\s\n]+\z//;
778 $message = "r$revision: $message" if $opt_r;
780 print $pw "$message\n"
781 or die "Error writing to git-commit-tree: $!\n";
782 $pw->close();
784 print "Committed change $revision:$branch ".strftime("%Y-%m-%d %H:%M:%S",gmtime($date)).")\n" if $opt_v;
785 chomp($cid = <$pr>);
786 length($cid) == 40
787 or die "Cannot get commit id ($cid): $!\n";
788 print "Commit ID $cid\n" if $opt_v;
789 $pr->close();
791 waitpid($pid,0);
792 die "Error running git-commit-tree: $?\n" if $?;
795 if (not defined $cid) {
796 $cid = $branches{"/"}{"LAST"};
799 if(not defined $dest) {
800 print "... no known parent\n" if $opt_v;
801 } elsif(not $tag) {
802 print "Writing to refs/heads/$dest\n" if $opt_v;
803 open(C,">$git_dir/refs/heads/$dest") and
804 print C ("$cid\n") and
805 close(C)
806 or die "Cannot write branch $dest for update: $!\n";
809 if($tag) {
810 my($in, $out) = ('','');
811 $last_rev = "-" if %$changed_paths;
812 # the tag was 'complex', i.e. did not refer to a "real" revision
814 $dest =~ tr/_/\./ if $opt_u;
815 $branch = $dest;
817 my $pid = open2($in, $out, 'git-mktag');
818 print $out ("object $cid\n".
819 "type commit\n".
820 "tag $dest\n".
821 "tagger $author_name <$author_email>\n") and
822 close($out)
823 or die "Cannot create tag object $dest: $!\n";
825 my $tagobj = <$in>;
826 chomp $tagobj;
828 if ( !close($in) or waitpid($pid, 0) != $pid or
829 $? != 0 or $tagobj !~ /^[0123456789abcdef]{40}$/ ) {
830 die "Cannot create tag object $dest: $!\n";
833 open(C,">$git_dir/refs/tags/$dest") and
834 print C ("$tagobj\n") and
835 close(C)
836 or die "Cannot create tag $branch: $!\n";
838 print "Created tag '$dest' on '$branch'\n" if $opt_v;
840 $branches{$branch}{"LAST"} = $cid;
841 $branches{$branch}{$revision} = $cid;
842 $last_rev = $cid;
843 print BRANCHES "$revision $branch $cid\n";
844 print "DONE: $revision $dest $cid\n" if $opt_v;
847 sub commit_all {
848 # Recursive use of the SVN connection does not work
849 local $svn = $svn2;
851 my ($changed_paths, $revision, $author, $date, $message, $pool) = @_;
852 my %p;
853 while(my($path,$action) = each %$changed_paths) {
854 $p{$path} = [ $action->action,$action->copyfrom_path, $action->copyfrom_rev, $path ];
856 $changed_paths = \%p;
858 my %done;
859 my @col;
860 my $pref;
861 my $branch;
863 while(my($path,$action) = each %$changed_paths) {
864 ($branch,$path) = split_path($revision,$path);
865 next if not defined $branch;
866 $done{$branch}{$path} = $action;
868 while(($branch,$changed_paths) = each %done) {
869 commit($branch, $changed_paths, $revision, $author, $date, $message);
873 $opt_l = $svn->{'maxrev'} if not defined $opt_l or $opt_l > $svn->{'maxrev'};
875 if ($opt_l < $current_rev) {
876 print "Up to date: no new revisions to fetch!\n" if $opt_v;
877 unlink("$git_dir/SVN2GIT_HEAD");
878 exit;
881 print "Fetching from $current_rev to $opt_l ...\n" if $opt_v;
883 my $pool=SVN::Pool->new;
884 $svn->{'svn'}->get_log("/",$current_rev,$opt_l,0,1,1,\&commit_all,$pool);
885 $pool->clear;
888 unlink($git_index);
890 if (defined $orig_git_index) {
891 $ENV{GIT_INDEX_FILE} = $orig_git_index;
892 } else {
893 delete $ENV{GIT_INDEX_FILE};
896 # Now switch back to the branch we were in before all of this happened
897 if($orig_branch) {
898 print "DONE\n" if $opt_v and (not defined $opt_l or $opt_l > 0);
899 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
900 if $forward_master;
901 unless ($opt_i) {
902 system('git-read-tree', '-m', '-u', 'SVN2GIT_HEAD', 'HEAD');
903 die "read-tree failed: $?\n" if $?;
905 } else {
906 $orig_branch = "master";
907 print "DONE; creating $orig_branch branch\n" if $opt_v and (not defined $opt_l or $opt_l > 0);
908 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
909 unless -f "$git_dir/refs/heads/master";
910 system('git-update-ref', 'HEAD', "$orig_branch");
911 unless ($opt_i) {
912 system('git checkout');
913 die "checkout failed: $?\n" if $?;
916 unlink("$git_dir/SVN2GIT_HEAD");
917 close(BRANCHES);