Add test case for git-config-set
[git.git] / git-svnimport.perl
1 #!/usr/bin/perl -w
2
3 # This tool is copyright (c) 2005, Matthias Urlichs.
4 # It is released under the Gnu Public License, version 2.
5 #
6 # The basic idea is to pull and analyze SVN changes.
7 #
8 # Checking out the files is done by a single long-running SVN connection.
9 #
10 # The head revision is on branch "origin" by default.
11 # You can change that with the '-o' option.
12
13 require 5.008; # for shell-safe open("-|",LIST)
14 use strict;
15 use warnings;
16 use Getopt::Std;
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;
27
28 die "Need SVN:Core 1.2.1 or better" if $SVN::Core::VERSION lt "1.2.1";
29
30 $SIG{'PIPE'}="IGNORE";
31 $ENV{'TZ'}="UTC";
32
33 our($opt_h,$opt_o,$opt_v,$opt_u,$opt_C,$opt_i,$opt_m,$opt_M,$opt_t,$opt_T,$opt_b,$opt_s,$opt_l,$opt_d,$opt_D);
34
35 sub usage() {
36         print STDERR <<END;
37 Usage: ${\basename $0}     # fetch/update GIT from SVN
38        [-o branch-for-HEAD] [-h] [-v] [-l max_num_changes]
39        [-C GIT_repository] [-t tagname] [-T trunkname] [-b branchname]
40        [-d|-D] [-i] [-u] [-s start_chg] [-m] [-M regex] [SVN_URL]
41 END
42         exit(1);
43 }
44
45 getopts("b:C:dDhil:mM:o:s:t:T:uv") or usage();
46 usage if $opt_h;
47
48 my $tag_name = $opt_t || "tags";
49 my $trunk_name = $opt_T || "trunk";
50 my $branch_name = $opt_b || "branches";
51
52 @ARGV == 1 or @ARGV == 2 or usage();
53
54 $opt_o ||= "origin";
55 $opt_s ||= 1;
56 my $git_tree = $opt_C;
57 $git_tree ||= ".";
58
59 my $svn_url = $ARGV[0];
60 my $svn_dir = $ARGV[1];
61
62 our @mergerx = ();
63 if ($opt_m) {
64         @mergerx = ( qr/\W(?:from|of|merge|merging|merged) (\w+)/i );
65 }
66 if ($opt_M) {
67         push (@mergerx, qr/$opt_M/);
68 }
69
70 select(STDERR); $|=1; select(STDOUT);
71
72
73 package SVNconn;
74 # Basic SVN connection.
75 # We're only interested in connecting and downloading, so ...
76
77 use File::Spec;
78 use File::Temp qw(tempfile);
79 use POSIX qw(strftime dup2);
80
81 sub new {
82         my($what,$repo) = @_;
83         $what=ref($what) if ref($what);
84
85         my $self = {};
86         $self->{'buffer'} = "";
87         bless($self,$what);
88
89         $repo =~ s#/+$##;
90         $self->{'fullrep'} = $repo;
91         $self->conn();
92
93         return $self;
94 }
95
96 sub conn {
97         my $self = shift;
98         my $repo = $self->{'fullrep'};
99         my $s = SVN::Ra->new($repo);
100
101         die "SVN connection to $repo: $!\n" unless defined $s;
102         $self->{'svn'} = $s;
103         $self->{'repo'} = $repo;
104         $self->{'maxrev'} = $s->get_latest_revnum();
105 }
106
107 sub file {
108         my($self,$path,$rev) = @_;
109
110         my ($fh, $name) = tempfile('gitsvn.XXXXXX',
111                     DIR => File::Spec->tmpdir(), UNLINK => 1);
112
113         print "... $rev $path ...\n" if $opt_v;
114         my $pool = SVN::Pool->new();
115         eval { $self->{'svn'}->get_file($path,$rev,$fh,$pool); };
116         $pool->clear;
117         if($@) {
118                 return undef if $@ =~ /Attempted to get checksum/;
119                 die $@;
120         }
121         close ($fh);
122
123         return $name;
124 }
125
126 package main;
127 use URI;
128
129 my $svn = $svn_url;
130 $svn .= "/$svn_dir" if defined $svn_dir;
131 $svn = SVNconn->new($svn);
132
133 my $lwp_ua;
134 if($opt_d or $opt_D) {
135         $svn_url = URI->new($svn_url)->canonical;
136         if($opt_D) {
137                 $svn_dir =~ s#/*$#/#;
138         } else {
139                 $svn_dir = "";
140         }
141         if ($svn_url->scheme eq "http") {
142                 use LWP::UserAgent;
143                 $lwp_ua = LWP::UserAgent->new(keep_alive => 1, requests_redirectable => []);
144         } else {
145                 print STDERR "Warning: not HTTP; turning off direct file access\n";
146                 $opt_d=0;
147         }
148 }
149
150 sub pdate($) {
151         my($d) = @_;
152         $d =~ m#(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)#
153                 or die "Unparseable date: $d\n";
154         my $y=$1; $y-=1900 if $y>1900;
155         return timegm($6||0,$5,$4,$3,$2-1,$y);
156 }
157
158 sub getwd() {
159         my $pwd = `pwd`;
160         chomp $pwd;
161         return $pwd;
162 }
163
164
165 sub get_headref($$) {
166     my $name    = shift;
167     my $git_dir = shift;
168     my $sha;
169
170     if (open(C,"$git_dir/refs/heads/$name")) {
171         chomp($sha = <C>);
172         close(C);
173         length($sha) == 40
174             or die "Cannot get head id for $name ($sha): $!\n";
175     }
176     return $sha;
177 }
178
179
180 -d $git_tree
181         or mkdir($git_tree,0777)
182         or die "Could not create $git_tree: $!";
183 chdir($git_tree);
184
185 my $orig_branch = "";
186 my $forward_master = 0;
187 my %branches;
188
189 my $git_dir = $ENV{"GIT_DIR"} || ".git";
190 $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#;
191 $ENV{"GIT_DIR"} = $git_dir;
192 my $orig_git_index;
193 $orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE};
194 my ($git_ih, $git_index) = tempfile('gitXXXXXX', SUFFIX => '.idx',
195                                     DIR => File::Spec->tmpdir());
196 close ($git_ih);
197 $ENV{GIT_INDEX_FILE} = $git_index;
198 my $maxnum = 0;
199 my $last_rev = "";
200 my $last_branch;
201 my $current_rev = $opt_s-1;
202 unless(-d $git_dir) {
203         system("git-init-db");
204         die "Cannot init the GIT db at $git_tree: $?\n" if $?;
205         system("git-read-tree");
206         die "Cannot init an empty tree: $?\n" if $?;
207
208         $last_branch = $opt_o;
209         $orig_branch = "";
210 } else {
211         -f "$git_dir/refs/heads/$opt_o"
212                 or die "Branch '$opt_o' does not exist.\n".
213                        "Either use the correct '-o branch' option,\n".
214                        "or import to a new repository.\n";
215
216         -f "$git_dir/svn2git"
217                 or die "'$git_dir/svn2git' does not exist.\n".
218                        "You need that file for incremental imports.\n";
219         open(F, "git-symbolic-ref HEAD |") or
220                 die "Cannot run git-symbolic-ref: $!\n";
221         chomp ($last_branch = <F>);
222         $last_branch = basename($last_branch);
223         close(F);
224         unless($last_branch) {
225                 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
226                 $last_branch = "master";
227         }
228         $orig_branch = $last_branch;
229         $last_rev = get_headref($orig_branch, $git_dir);
230         if (-f "$git_dir/SVN2GIT_HEAD") {
231                 die <<EOM;
232 SVN2GIT_HEAD exists.
233 Make sure your working directory corresponds to HEAD and remove SVN2GIT_HEAD.
234 You may need to run
235
236     git-read-tree -m -u SVN2GIT_HEAD HEAD
237 EOM
238         }
239         system('cp', "$git_dir/HEAD", "$git_dir/SVN2GIT_HEAD");
240
241         $forward_master =
242             $opt_o ne 'master' && -f "$git_dir/refs/heads/master" &&
243             system('cmp', '-s', "$git_dir/refs/heads/master",
244                                 "$git_dir/refs/heads/$opt_o") == 0;
245
246         # populate index
247         system('git-read-tree', $last_rev);
248         die "read-tree failed: $?\n" if $?;
249
250         # Get the last import timestamps
251         open my $B,"<", "$git_dir/svn2git";
252         while(<$B>) {
253                 chomp;
254                 my($num,$branch,$ref) = split;
255                 $branches{$branch}{$num} = $ref;
256                 $branches{$branch}{"LAST"} = $ref;
257                 $current_rev = $num if $current_rev < $num;
258         }
259         close($B);
260 }
261 -d $git_dir
262         or die "Could not create git subdir ($git_dir).\n";
263
264 open BRANCHES,">>", "$git_dir/svn2git";
265
266 sub node_kind($$$) {
267         my ($branch, $path, $revision) = @_;
268         my $pool=SVN::Pool->new;
269         my $kind = $svn->{'svn'}->check_path(revert_split_path($branch,$path),$revision,$pool);
270         $pool->clear;
271         return $kind;
272 }
273
274 sub revert_split_path($$) {
275         my($branch,$path) = @_;
276
277         my $svnpath;
278         $path = "" if $path eq "/"; # this should not happen, but ...
279         if($branch eq "/") {
280                 $svnpath = "$trunk_name/$path";
281         } elsif($branch =~ m#^/#) {
282                 $svnpath = "$tag_name$branch/$path";
283         } else {
284                 $svnpath = "$branch_name/$branch/$path";
285         }
286
287         $svnpath =~ s#/+$##;
288         return $svnpath;
289 }
290
291 sub get_file($$$) {
292         my($rev,$branch,$path) = @_;
293
294         my $svnpath = revert_split_path($branch,$path);
295
296         # now get it
297         my $name;
298         if($opt_d) {
299                 my($req,$res);
300
301                 # /svn/!svn/bc/2/django/trunk/django-docs/build.py
302                 my $url=$svn_url->clone();
303                 $url->path($url->path."/!svn/bc/$rev/$svn_dir$svnpath");
304                 print "... $path...\n" if $opt_v;
305                 $req = HTTP::Request->new(GET => $url);
306                 $res = $lwp_ua->request($req);
307                 if ($res->is_success) {
308                         my $fh;
309                         ($fh, $name) = tempfile('gitsvn.XXXXXX',
310                         DIR => File::Spec->tmpdir(), UNLINK => 1);
311                         print $fh $res->content;
312                         close($fh) or die "Could not write $name: $!\n";
313                 } else {
314                         return undef if $res->code == 301; # directory?
315                         die $res->status_line." at $url\n";
316                 }
317         } else {
318                 $name = $svn->file("/$svnpath",$rev);
319                 return undef unless defined $name;
320         }
321
322         open my $F, '-|', "git-hash-object", "-w", $name
323                 or die "Cannot create object: $!\n";
324         my $sha = <$F>;
325         chomp $sha;
326         close $F;
327         unlink $name;
328         my $mode = "0644"; # SV does not seem to store any file modes
329         return [$mode, $sha, $path];
330 }
331
332 sub split_path($$) {
333         my($rev,$path) = @_;
334         my $branch;
335
336         if($path =~ s#^/\Q$tag_name\E/([^/]+)/?##) {
337                 $branch = "/$1";
338         } elsif($path =~ s#^/\Q$trunk_name\E/?##) {
339                 $branch = "/";
340         } elsif($path =~ s#^/\Q$branch_name\E/([^/]+)/?##) {
341                 $branch = $1;
342         } else {
343                 my %no_error = (
344                         "/" => 1,
345                         "/$tag_name" => 1,
346                         "/$branch_name" => 1
347                 );
348                 print STDERR "$rev: Unrecognized path: $path\n" unless (defined $no_error{$path});
349                 return ()
350         }
351         $path = "/" if $path eq "";
352         return ($branch,$path);
353 }
354
355 sub branch_rev($$) {
356
357         my ($srcbranch,$uptorev) = @_;
358
359         my $bbranches = $branches{$srcbranch};
360         my @revs = reverse sort { ($a eq 'LAST' ? 0 : $a) <=> ($b eq 'LAST' ? 0 : $b) } keys %$bbranches;
361         my $therev;
362         foreach my $arev(@revs) {
363                 next if  ($arev eq 'LAST');
364                 if ($arev <= $uptorev) {
365                         $therev = $arev;
366                         last;
367                 }
368         }
369         return $therev;
370 }
371
372 sub copy_path($$$$$$$$) {
373         # Somebody copied a whole subdirectory.
374         # We need to find the index entries from the old version which the
375         # SVN log entry points to, and add them to the new place.
376
377         my($newrev,$newbranch,$path,$oldpath,$rev,$node_kind,$new,$parents) = @_;
378
379         my($srcbranch,$srcpath) = split_path($rev,$oldpath);
380         unless(defined $srcbranch) {
381                 print "Path not found when copying from $oldpath @ $rev\n";
382                 return;
383         }
384         my $therev = branch_rev($srcbranch, $rev);
385         my $gitrev = $branches{$srcbranch}{$therev};
386         unless($gitrev) {
387                 print STDERR "$newrev:$newbranch: could not find $oldpath \@ $rev\n";
388                 return;
389         }
390         if ($srcbranch ne $newbranch) {
391                 push(@$parents, $branches{$srcbranch}{'LAST'});
392         }
393         print "$newrev:$newbranch:$path: copying from $srcbranch:$srcpath @ $rev\n" if $opt_v;
394         if ($node_kind eq $SVN::Node::dir) {
395                         $srcpath =~ s#/*$#/#;
396         }
397         
398         open my $f,"-|","git-ls-tree","-r","-z",$gitrev,$srcpath;
399         local $/ = "\0";
400         while(<$f>) {
401                 chomp;
402                 my($m,$p) = split(/\t/,$_,2);
403                 my($mode,$type,$sha1) = split(/ /,$m);
404                 next if $type ne "blob";
405                 if ($node_kind eq $SVN::Node::dir) {
406                         $p = $path . substr($p,length($srcpath)-1);
407                 } else {
408                         $p = $path;
409                 }
410                 push(@$new,[$mode,$sha1,$p]);   
411         }
412         close($f) or
413                 print STDERR "$newrev:$newbranch: could not list files in $oldpath \@ $rev\n";
414 }
415
416 sub commit {
417         my($branch, $changed_paths, $revision, $author, $date, $message) = @_;
418         my($author_name,$author_email,$dest);
419         my(@old,@new,@parents);
420
421         if (not defined $author) {
422                 $author_name = $author_email = "unknown";
423         } elsif ($author =~ /^(.*?)\s+<(.*)>$/) {
424                 ($author_name, $author_email) = ($1, $2);
425         } else {
426                 $author =~ s/^<(.*)>$/$1/;
427                 $author_name = $author_email = $author;
428         }
429         $date = pdate($date);
430
431         my $tag;
432         my $parent;
433         if($branch eq "/") { # trunk
434                 $parent = $opt_o;
435         } elsif($branch =~ m#^/(.+)#) { # tag
436                 $tag = 1;
437                 $parent = $1;
438         } else { # "normal" branch
439                 # nothing to do
440                 $parent = $branch;
441         }
442         $dest = $parent;
443
444         my $prev = $changed_paths->{"/"};
445         if($prev and $prev->[0] eq "A") {
446                 delete $changed_paths->{"/"};
447                 my $oldpath = $prev->[1];
448                 my $rev;
449                 if(defined $oldpath) {
450                         my $p;
451                         ($parent,$p) = split_path($revision,$oldpath);
452                         if($parent eq "/") {
453                                 $parent = $opt_o;
454                         } else {
455                                 $parent =~ s#^/##; # if it's a tag
456                         }
457                 } else {
458                         $parent = undef;
459                 }
460         }
461
462         my $rev;
463         if($revision > $opt_s and defined $parent) {
464                 open(H,"git-rev-parse --verify $parent |");
465                 $rev = <H>;
466                 close(H) or do {
467                         print STDERR "$revision: cannot find commit '$parent'!\n";
468                         return;
469                 };
470                 chop $rev;
471                 if(length($rev) != 40) {
472                         print STDERR "$revision: cannot find commit '$parent'!\n";
473                         return;
474                 }
475                 $rev = $branches{($parent eq $opt_o) ? "/" : $parent}{"LAST"};
476                 if($revision != $opt_s and not $rev) {
477                         print STDERR "$revision: do not know ancestor for '$parent'!\n";
478                         return;
479                 }
480         } else {
481                 $rev = undef;
482         }
483
484 #       if($prev and $prev->[0] eq "A") {
485 #               if(not $tag) {
486 #                       unless(open(H,"> $git_dir/refs/heads/$branch")) {
487 #                               print STDERR "$revision: Could not create branch $branch: $!\n";
488 #                               $state=11;
489 #                               next;
490 #                       }
491 #                       print H "$rev\n"
492 #                               or die "Could not write branch $branch: $!";
493 #                       close(H)
494 #                               or die "Could not write branch $branch: $!";
495 #               }
496 #       }
497         if(not defined $rev) {
498                 unlink($git_index);
499         } elsif ($rev ne $last_rev) {
500                 print "Switching from $last_rev to $rev ($branch)\n" if $opt_v;
501                 system("git-read-tree", $rev);
502                 die "read-tree failed for $rev: $?\n" if $?;
503                 $last_rev = $rev;
504         }
505
506         push (@parents, $rev) if defined $rev;
507
508         my $cid;
509         if($tag and not %$changed_paths) {
510                 $cid = $rev;
511         } else {
512                 my @paths = sort keys %$changed_paths;
513                 foreach my $path(@paths) {
514                         my $action = $changed_paths->{$path};
515
516                         if ($action->[0] eq "R") {
517                                 # refer to a file/tree in an earlier commit
518                                 push(@old,$path); # remove any old stuff
519                         }
520                         if(($action->[0] eq "A") || ($action->[0] eq "R")) {
521                                 my $node_kind = node_kind($branch,$path,$revision);
522                                 if($action->[1]) {
523                                         copy_path($revision,$branch,$path,$action->[1],$action->[2],$node_kind,\@new,\@parents);
524                                 } elsif ($node_kind eq $SVN::Node::file) {
525                                         my $f = get_file($revision,$branch,$path);
526                                         if ($f) {
527                                                 push(@new,$f) if $f;
528                                         } else {
529                                                 my $opath = $action->[3];
530                                                 print STDERR "$revision: $branch: could not fetch '$opath'\n";
531                                         }
532                                 }
533                         } elsif ($action->[0] eq "D") {
534                                 push(@old,$path);
535                         } elsif ($action->[0] eq "M") {
536                                 my $node_kind = node_kind($branch,$path,$revision);
537                                 if ($node_kind eq $SVN::Node::file) {
538                                         my $f = get_file($revision,$branch,$path);
539                                         push(@new,$f) if $f;
540                                 }
541                         } else {
542                                 die "$revision: unknown action '".$action->[0]."' for $path\n";
543                         }
544                 }
545
546                 if(@old) {
547                         open my $F, "-|", "git-ls-files", "-z", @old or die $!;
548                         @old = ();
549                         local $/ = "\0";
550                         while(<$F>) {
551                                 chomp;
552                                 push(@old,$_);
553                         }
554                         close($F);
555
556                         while(@old) {
557                                 my @o2;
558                                 if(@old > 55) {
559                                         @o2 = splice(@old,0,50);
560                                 } else {
561                                         @o2 = @old;
562                                         @old = ();
563                                 }
564                                 system("git-update-index","--force-remove","--",@o2);
565                                 die "Cannot remove files: $?\n" if $?;
566                         }
567                 }
568                 while(@new) {
569                         my @n2;
570                         if(@new > 12) {
571                                 @n2 = splice(@new,0,10);
572                         } else {
573                                 @n2 = @new;
574                                 @new = ();
575                         }
576                         system("git-update-index","--add",
577                                 (map { ('--cacheinfo', @$_) } @n2));
578                         die "Cannot add files: $?\n" if $?;
579                 }
580
581                 my $pid = open(C,"-|");
582                 die "Cannot fork: $!" unless defined $pid;
583                 unless($pid) {
584                         exec("git-write-tree");
585                         die "Cannot exec git-write-tree: $!\n";
586                 }
587                 chomp(my $tree = <C>);
588                 length($tree) == 40
589                         or die "Cannot get tree id ($tree): $!\n";
590                 close(C)
591                         or die "Error running git-write-tree: $?\n";
592                 print "Tree ID $tree\n" if $opt_v;
593
594                 my $pr = IO::Pipe->new() or die "Cannot open pipe: $!\n";
595                 my $pw = IO::Pipe->new() or die "Cannot open pipe: $!\n";
596                 $pid = fork();
597                 die "Fork: $!\n" unless defined $pid;
598                 unless($pid) {
599                         $pr->writer();
600                         $pw->reader();
601                         open(OUT,">&STDOUT");
602                         dup2($pw->fileno(),0);
603                         dup2($pr->fileno(),1);
604                         $pr->close();
605                         $pw->close();
606
607                         my @par = ();
608
609                         # loose detection of merges
610                         # based on the commit msg
611                         foreach my $rx (@mergerx) {
612                                 if ($message =~ $rx) {
613                                         my $mparent = $1;
614                                         if ($mparent eq 'HEAD') { $mparent = $opt_o };
615                                         if ( -e "$git_dir/refs/heads/$mparent") {
616                                                 $mparent = get_headref($mparent, $git_dir);
617                                                 push (@parents, $mparent);
618                                                 print OUT "Merge parent branch: $mparent\n" if $opt_v;
619                                         }
620                                 }
621                         }
622                         my %seen_parents = ();
623                         my @unique_parents = grep { ! $seen_parents{$_} ++ } @parents;
624                         foreach my $bparent (@unique_parents) {
625                                 push @par, '-p', $bparent;
626                                 print OUT "Merge parent branch: $bparent\n" if $opt_v;
627                         }
628
629                         exec("env",
630                                 "GIT_AUTHOR_NAME=$author_name",
631                                 "GIT_AUTHOR_EMAIL=$author_email",
632                                 "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
633                                 "GIT_COMMITTER_NAME=$author_name",
634                                 "GIT_COMMITTER_EMAIL=$author_email",
635                                 "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
636                                 "git-commit-tree", $tree,@par);
637                         die "Cannot exec git-commit-tree: $!\n";
638                 }
639                 $pw->writer();
640                 $pr->reader();
641
642                 $message =~ s/[\s\n]+\z//;
643
644                 print $pw "$message\n"
645                         or die "Error writing to git-commit-tree: $!\n";
646                 $pw->close();
647
648                 print "Committed change $revision:$branch ".strftime("%Y-%m-%d %H:%M:%S",gmtime($date)).")\n" if $opt_v;
649                 chomp($cid = <$pr>);
650                 length($cid) == 40
651                         or die "Cannot get commit id ($cid): $!\n";
652                 print "Commit ID $cid\n" if $opt_v;
653                 $pr->close();
654
655                 waitpid($pid,0);
656                 die "Error running git-commit-tree: $?\n" if $?;
657         }
658
659         if (not defined $cid) {
660                 $cid = $branches{"/"}{"LAST"};
661         }
662
663         if(not defined $dest) {
664                 print "... no known parent\n" if $opt_v;
665         } elsif(not $tag) {
666                 print "Writing to refs/heads/$dest\n" if $opt_v;
667                 open(C,">$git_dir/refs/heads/$dest") and
668                 print C ("$cid\n") and
669                 close(C)
670                         or die "Cannot write branch $dest for update: $!\n";
671         }
672
673         if($tag) {
674                 my($in, $out) = ('','');
675                 $last_rev = "-" if %$changed_paths;
676                 # the tag was 'complex', i.e. did not refer to a "real" revision
677
678                 $dest =~ tr/_/\./ if $opt_u;
679                 $branch = $dest;
680
681                 my $pid = open2($in, $out, 'git-mktag');
682                 print $out ("object $cid\n".
683                     "type commit\n".
684                     "tag $dest\n".
685                     "tagger $author_name <$author_email>\n") and
686                 close($out)
687                     or die "Cannot create tag object $dest: $!\n";
688
689                 my $tagobj = <$in>;
690                 chomp $tagobj;
691
692                 if ( !close($in) or waitpid($pid, 0) != $pid or
693                                 $? != 0 or $tagobj !~ /^[0123456789abcdef]{40}$/ ) {
694                         die "Cannot create tag object $dest: $!\n";
695                 }
696
697                 open(C,">$git_dir/refs/tags/$dest") and
698                 print C ("$tagobj\n") and
699                 close(C)
700                         or die "Cannot create tag $branch: $!\n";
701
702                 print "Created tag '$dest' on '$branch'\n" if $opt_v;
703         }
704         $branches{$branch}{"LAST"} = $cid;
705         $branches{$branch}{$revision} = $cid;
706         $last_rev = $cid;
707         print BRANCHES "$revision $branch $cid\n";
708         print "DONE: $revision $dest $cid\n" if $opt_v;
709 }
710
711 my ($changed_paths, $revision, $author, $date, $message, $pool) = @_;
712 sub _commit_all {
713         ($changed_paths, $revision, $author, $date, $message, $pool) = @_;
714         my %p;
715         while(my($path,$action) = each %$changed_paths) {
716                 $p{$path} = [ $action->action,$action->copyfrom_path, $action->copyfrom_rev, $path ];
717         }
718         $changed_paths = \%p;
719 }
720
721 sub commit_all {
722         my %done;
723         my @col;
724         my $pref;
725         my $branch;
726
727         while(my($path,$action) = each %$changed_paths) {
728                 ($branch,$path) = split_path($revision,$path);
729                 next if not defined $branch;
730                 $done{$branch}{$path} = $action;
731         }
732         while(($branch,$changed_paths) = each %done) {
733                 commit($branch, $changed_paths, $revision, $author, $date, $message);
734         }
735 }
736
737 while(++$current_rev <= $svn->{'maxrev'}) {
738         if (defined $opt_l) {
739                 $opt_l--;
740                 if ($opt_l < 0) {
741                         last;
742                 }
743         }
744         my $pool=SVN::Pool->new;
745         $svn->{'svn'}->get_log("/",$current_rev,$current_rev,1,1,1,\&_commit_all,$pool);
746         $pool->clear;
747         commit_all();
748 }
749
750
751 unlink($git_index);
752
753 if (defined $orig_git_index) {
754         $ENV{GIT_INDEX_FILE} = $orig_git_index;
755 } else {
756         delete $ENV{GIT_INDEX_FILE};
757 }
758
759 # Now switch back to the branch we were in before all of this happened
760 if($orig_branch) {
761         print "DONE\n" if $opt_v and (not defined $opt_l or $opt_l > 0);
762         system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
763                 if $forward_master;
764         unless ($opt_i) {
765                 system('git-read-tree', '-m', '-u', 'SVN2GIT_HEAD', 'HEAD');
766                 die "read-tree failed: $?\n" if $?;
767         }
768 } else {
769         $orig_branch = "master";
770         print "DONE; creating $orig_branch branch\n" if $opt_v and (not defined $opt_l or $opt_l > 0);
771         system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
772                 unless -f "$git_dir/refs/heads/master";
773         system('git-update-ref', 'HEAD', "$orig_branch");
774         unless ($opt_i) {
775                 system('git checkout');
776                 die "checkout failed: $?\n" if $?;
777         }
778 }
779 unlink("$git_dir/SVN2GIT_HEAD");
780 close(BRANCHES);