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