Merge branch 'master' into jc/web
[git.git] / git-svnimport.perl
blobf6eff8e32adc92a072b33de9040e2154efd13a8a
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,$opt_S,$opt_F);
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] [-S] [-F] [SVN_URL]
43 END
44 exit(1);
47 getopts("A:b:C:dDFhiI:l:mM:o:rs:t:T:Suv") 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 my $branch_esc = quotemeta ($branch_name);
67 my $trunk_esc = quotemeta ($trunk_name);
68 @mergerx =
70 qr!\b(?:merg(?:ed?|ing))\b.*?\b((?:(?<=$branch_esc/)[\w\.\-]+)|(?:$trunk_esc))\b!i,
71 qr!\b(?:from|of)\W+((?:(?<=$branch_esc/)[\w\.\-]+)|(?:$trunk_esc))\b!i,
72 qr!\b(?:from|of)\W+(?:the )?([\w\.\-]+)[-\s]branch\b!i
75 if ($opt_M) {
76 unshift (@mergerx, qr/$opt_M/);
79 # Absolutize filename now, since we will have chdir'ed by the time we
80 # get around to opening it.
81 $opt_A = File::Spec->rel2abs($opt_A) if $opt_A;
83 our %users = ();
84 our $users_file = undef;
85 sub read_users($) {
86 $users_file = File::Spec->rel2abs(@_);
87 die "Cannot open $users_file\n" unless -f $users_file;
88 open(my $authors,$users_file);
89 while(<$authors>) {
90 chomp;
91 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
92 (my $user,my $name,my $email) = ($1,$2,$3);
93 $users{$user} = [$name,$email];
95 close($authors);
98 select(STDERR); $|=1; select(STDOUT);
101 package SVNconn;
102 # Basic SVN connection.
103 # We're only interested in connecting and downloading, so ...
105 use File::Spec;
106 use File::Temp qw(tempfile);
107 use POSIX qw(strftime dup2);
108 use Fcntl qw(SEEK_SET);
110 sub new {
111 my($what,$repo) = @_;
112 $what=ref($what) if ref($what);
114 my $self = {};
115 $self->{'buffer'} = "";
116 bless($self,$what);
118 $repo =~ s#/+$##;
119 $self->{'fullrep'} = $repo;
120 $self->conn();
122 return $self;
125 sub conn {
126 my $self = shift;
127 my $repo = $self->{'fullrep'};
128 my $auth = SVN::Core::auth_open ([SVN::Client::get_simple_provider,
129 SVN::Client::get_ssl_server_trust_file_provider,
130 SVN::Client::get_username_provider]);
131 my $s = SVN::Ra->new(url => $repo, auth => $auth);
132 die "SVN connection to $repo: $!\n" unless defined $s;
133 $self->{'svn'} = $s;
134 $self->{'repo'} = $repo;
135 $self->{'maxrev'} = $s->get_latest_revnum();
138 sub file {
139 my($self,$path,$rev) = @_;
141 my ($fh, $name) = tempfile('gitsvn.XXXXXX',
142 DIR => File::Spec->tmpdir(), UNLINK => 1);
144 print "... $rev $path ...\n" if $opt_v;
145 my (undef, $properties);
146 my $pool = SVN::Pool->new();
147 eval { (undef, $properties)
148 = $self->{'svn'}->get_file($path,$rev,$fh,$pool); };
149 $pool->clear;
150 if($@) {
151 return undef if $@ =~ /Attempted to get checksum/;
152 die $@;
154 my $mode;
155 if (exists $properties->{'svn:executable'}) {
156 $mode = '100755';
157 } elsif (exists $properties->{'svn:special'}) {
158 my ($special_content, $filesize);
159 $filesize = tell $fh;
160 seek $fh, 0, SEEK_SET;
161 read $fh, $special_content, $filesize;
162 if ($special_content =~ s/^link //) {
163 $mode = '120000';
164 seek $fh, 0, SEEK_SET;
165 truncate $fh, 0;
166 print $fh $special_content;
167 } else {
168 die "unexpected svn:special file encountered";
170 } else {
171 $mode = '100644';
173 close ($fh);
175 return ($name, $mode);
178 sub ignore {
179 my($self,$path,$rev) = @_;
181 print "... $rev $path ...\n" if $opt_v;
182 my (undef,undef,$properties)
183 = $self->{'svn'}->get_dir($path,$rev,undef);
184 if (exists $properties->{'svn:ignore'}) {
185 my ($fh, $name) = tempfile('gitsvn.XXXXXX',
186 DIR => File::Spec->tmpdir(),
187 UNLINK => 1);
188 print $fh $properties->{'svn:ignore'};
189 close($fh);
190 return $name;
191 } else {
192 return undef;
196 sub dir_list {
197 my($self,$path,$rev) = @_;
198 my ($dirents,undef,$properties)
199 = $self->{'svn'}->get_dir($path,$rev,undef);
200 return $dirents;
203 package main;
204 use URI;
206 our $svn = $svn_url;
207 $svn .= "/$svn_dir" if defined $svn_dir;
208 my $svn2 = SVNconn->new($svn);
209 $svn = SVNconn->new($svn);
211 my $lwp_ua;
212 if($opt_d or $opt_D) {
213 $svn_url = URI->new($svn_url)->canonical;
214 if($opt_D) {
215 $svn_dir =~ s#/*$#/#;
216 } else {
217 $svn_dir = "";
219 if ($svn_url->scheme eq "http") {
220 use LWP::UserAgent;
221 $lwp_ua = LWP::UserAgent->new(keep_alive => 1, requests_redirectable => []);
222 } else {
223 print STDERR "Warning: not HTTP; turning off direct file access\n";
224 $opt_d=0;
228 sub pdate($) {
229 my($d) = @_;
230 $d =~ m#(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)#
231 or die "Unparseable date: $d\n";
232 my $y=$1; $y-=1900 if $y>1900;
233 return timegm($6||0,$5,$4,$3,$2-1,$y);
236 sub getwd() {
237 my $pwd = `pwd`;
238 chomp $pwd;
239 return $pwd;
243 sub get_headref($$) {
244 my $name = shift;
245 my $git_dir = shift;
246 my $sha;
248 if (open(C,"$git_dir/refs/heads/$name")) {
249 chomp($sha = <C>);
250 close(C);
251 length($sha) == 40
252 or die "Cannot get head id for $name ($sha): $!\n";
254 return $sha;
258 -d $git_tree
259 or mkdir($git_tree,0777)
260 or die "Could not create $git_tree: $!";
261 chdir($git_tree);
263 my $orig_branch = "";
264 my $forward_master = 0;
265 my %branches;
267 my $git_dir = $ENV{"GIT_DIR"} || ".git";
268 $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#;
269 $ENV{"GIT_DIR"} = $git_dir;
270 my $orig_git_index;
271 $orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE};
272 my ($git_ih, $git_index) = tempfile('gitXXXXXX', SUFFIX => '.idx',
273 DIR => File::Spec->tmpdir());
274 close ($git_ih);
275 $ENV{GIT_INDEX_FILE} = $git_index;
276 my $maxnum = 0;
277 my $last_rev = "";
278 my $last_branch;
279 my $current_rev = $opt_s || 1;
280 unless(-d $git_dir) {
281 system("git-init-db");
282 die "Cannot init the GIT db at $git_tree: $?\n" if $?;
283 system("git-read-tree");
284 die "Cannot init an empty tree: $?\n" if $?;
286 $last_branch = $opt_o;
287 $orig_branch = "";
288 } else {
289 -f "$git_dir/refs/heads/$opt_o"
290 or die "Branch '$opt_o' does not exist.\n".
291 "Either use the correct '-o branch' option,\n".
292 "or import to a new repository.\n";
294 -f "$git_dir/svn2git"
295 or die "'$git_dir/svn2git' does not exist.\n".
296 "You need that file for incremental imports.\n";
297 open(F, "git-symbolic-ref HEAD |") or
298 die "Cannot run git-symbolic-ref: $!\n";
299 chomp ($last_branch = <F>);
300 $last_branch = basename($last_branch);
301 close(F);
302 unless($last_branch) {
303 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
304 $last_branch = "master";
306 $orig_branch = $last_branch;
307 $last_rev = get_headref($orig_branch, $git_dir);
308 if (-f "$git_dir/SVN2GIT_HEAD") {
309 die <<EOM;
310 SVN2GIT_HEAD exists.
311 Make sure your working directory corresponds to HEAD and remove SVN2GIT_HEAD.
312 You may need to run
314 git-read-tree -m -u SVN2GIT_HEAD HEAD
317 system('cp', "$git_dir/HEAD", "$git_dir/SVN2GIT_HEAD");
319 $forward_master =
320 $opt_o ne 'master' && -f "$git_dir/refs/heads/master" &&
321 system('cmp', '-s', "$git_dir/refs/heads/master",
322 "$git_dir/refs/heads/$opt_o") == 0;
324 # populate index
325 system('git-read-tree', $last_rev);
326 die "read-tree failed: $?\n" if $?;
328 # Get the last import timestamps
329 open my $B,"<", "$git_dir/svn2git";
330 while(<$B>) {
331 chomp;
332 my($num,$branch,$ref) = split;
333 $branches{$branch}{$num} = $ref;
334 $branches{$branch}{"LAST"} = $ref;
335 $current_rev = $num+1 if $current_rev <= $num;
337 close($B);
339 -d $git_dir
340 or die "Could not create git subdir ($git_dir).\n";
342 my $default_authors = "$git_dir/svn-authors";
343 if ($opt_A) {
344 read_users($opt_A);
345 copy($opt_A,$default_authors) or die "Copy failed: $!";
346 } else {
347 read_users($default_authors) if -f $default_authors;
350 open BRANCHES,">>", "$git_dir/svn2git";
352 sub node_kind($$) {
353 my ($svnpath, $revision) = @_;
354 my $pool=SVN::Pool->new;
355 my $kind = $svn->{'svn'}->check_path($svnpath,$revision,$pool);
356 $pool->clear;
357 return $kind;
360 sub get_file($$$) {
361 my($svnpath,$rev,$path) = @_;
363 # now get it
364 my ($name,$mode);
365 if($opt_d) {
366 my($req,$res);
368 # /svn/!svn/bc/2/django/trunk/django-docs/build.py
369 my $url=$svn_url->clone();
370 $url->path($url->path."/!svn/bc/$rev/$svn_dir$svnpath");
371 print "... $path...\n" if $opt_v;
372 $req = HTTP::Request->new(GET => $url);
373 $res = $lwp_ua->request($req);
374 if ($res->is_success) {
375 my $fh;
376 ($fh, $name) = tempfile('gitsvn.XXXXXX',
377 DIR => File::Spec->tmpdir(), UNLINK => 1);
378 print $fh $res->content;
379 close($fh) or die "Could not write $name: $!\n";
380 } else {
381 return undef if $res->code == 301; # directory?
382 die $res->status_line." at $url\n";
384 $mode = '0644'; # can't obtain mode via direct http request?
385 } else {
386 ($name,$mode) = $svn->file("$svnpath",$rev);
387 return undef unless defined $name;
390 my $pid = open(my $F, '-|');
391 die $! unless defined $pid;
392 if (!$pid) {
393 exec("git-hash-object", "-w", $name)
394 or die "Cannot create object: $!\n";
396 my $sha = <$F>;
397 chomp $sha;
398 close $F;
399 unlink $name;
400 return [$mode, $sha, $path];
403 sub get_ignore($$$$$) {
404 my($new,$old,$rev,$path,$svnpath) = @_;
406 return unless $opt_I;
407 my $name = $svn->ignore("$svnpath",$rev);
408 if ($path eq '/') {
409 $path = $opt_I;
410 } else {
411 $path = File::Spec->catfile($path,$opt_I);
413 if (defined $name) {
414 my $pid = open(my $F, '-|');
415 die $! unless defined $pid;
416 if (!$pid) {
417 exec("git-hash-object", "-w", $name)
418 or die "Cannot create object: $!\n";
420 my $sha = <$F>;
421 chomp $sha;
422 close $F;
423 unlink $name;
424 push(@$new,['0644',$sha,$path]);
425 } elsif (defined $old) {
426 push(@$old,$path);
430 sub split_path($$) {
431 my($rev,$path) = @_;
432 my $branch;
434 if($path =~ s#^/\Q$tag_name\E/([^/]+)/?##) {
435 $branch = "/$1";
436 } elsif($path =~ s#^/\Q$trunk_name\E/?##) {
437 $branch = "/";
438 } elsif($path =~ s#^/\Q$branch_name\E/([^/]+)/?##) {
439 $branch = $1;
440 } else {
441 my %no_error = (
442 "/" => 1,
443 "/$tag_name" => 1,
444 "/$branch_name" => 1
446 print STDERR "$rev: Unrecognized path: $path\n" unless (defined $no_error{$path});
447 return ()
449 $path = "/" if $path eq "";
450 return ($branch,$path);
453 sub branch_rev($$) {
455 my ($srcbranch,$uptorev) = @_;
457 my $bbranches = $branches{$srcbranch};
458 my @revs = reverse sort { ($a eq 'LAST' ? 0 : $a) <=> ($b eq 'LAST' ? 0 : $b) } keys %$bbranches;
459 my $therev;
460 foreach my $arev(@revs) {
461 next if ($arev eq 'LAST');
462 if ($arev <= $uptorev) {
463 $therev = $arev;
464 last;
467 return $therev;
470 sub expand_svndir($$$);
472 sub expand_svndir($$$)
474 my ($svnpath, $rev, $path) = @_;
475 my @list;
476 get_ignore(\@list, undef, $rev, $path, $svnpath);
477 my $dirents = $svn->dir_list($svnpath, $rev);
478 foreach my $p(keys %$dirents) {
479 my $kind = node_kind($svnpath.'/'.$p, $rev);
480 if ($kind eq $SVN::Node::file) {
481 my $f = get_file($svnpath.'/'.$p, $rev, $path.'/'.$p);
482 push(@list, $f) if $f;
483 } elsif ($kind eq $SVN::Node::dir) {
484 push(@list,
485 expand_svndir($svnpath.'/'.$p, $rev, $path.'/'.$p));
488 return @list;
491 sub copy_path($$$$$$$$) {
492 # Somebody copied a whole subdirectory.
493 # We need to find the index entries from the old version which the
494 # SVN log entry points to, and add them to the new place.
496 my($newrev,$newbranch,$path,$oldpath,$rev,$node_kind,$new,$parents) = @_;
498 my($srcbranch,$srcpath) = split_path($rev,$oldpath);
499 unless(defined $srcbranch && defined $srcpath) {
500 print "Path not found when copying from $oldpath @ $rev.\n".
501 "Will try to copy from original SVN location...\n"
502 if $opt_v;
503 push (@$new, expand_svndir($oldpath, $rev, $path));
504 return;
506 my $therev = branch_rev($srcbranch, $rev);
507 my $gitrev = $branches{$srcbranch}{$therev};
508 unless($gitrev) {
509 print STDERR "$newrev:$newbranch: could not find $oldpath \@ $rev\n";
510 return;
512 if ($srcbranch ne $newbranch) {
513 push(@$parents, $branches{$srcbranch}{'LAST'});
515 print "$newrev:$newbranch:$path: copying from $srcbranch:$srcpath @ $rev\n" if $opt_v;
516 if ($node_kind eq $SVN::Node::dir) {
517 $srcpath =~ s#/*$#/#;
520 my $pid = open my $f,'-|';
521 die $! unless defined $pid;
522 if (!$pid) {
523 exec("git-ls-tree","-r","-z",$gitrev,$srcpath)
524 or die $!;
526 local $/ = "\0";
527 while(<$f>) {
528 chomp;
529 my($m,$p) = split(/\t/,$_,2);
530 my($mode,$type,$sha1) = split(/ /,$m);
531 next if $type ne "blob";
532 if ($node_kind eq $SVN::Node::dir) {
533 $p = $path . substr($p,length($srcpath)-1);
534 } else {
535 $p = $path;
537 push(@$new,[$mode,$sha1,$p]);
539 close($f) or
540 print STDERR "$newrev:$newbranch: could not list files in $oldpath \@ $rev\n";
543 sub commit {
544 my($branch, $changed_paths, $revision, $author, $date, $message) = @_;
545 my($committer_name,$committer_email,$dest);
546 my($author_name,$author_email);
547 my(@old,@new,@parents);
549 if (not defined $author or $author eq "") {
550 $committer_name = $committer_email = "unknown";
551 } elsif (defined $users_file) {
552 die "User $author is not listed in $users_file\n"
553 unless exists $users{$author};
554 ($committer_name,$committer_email) = @{$users{$author}};
555 } elsif ($author =~ /^(.*?)\s+<(.*)>$/) {
556 ($committer_name, $committer_email) = ($1, $2);
557 } else {
558 $author =~ s/^<(.*)>$/$1/;
559 $committer_name = $committer_email = $author;
562 if ($opt_F && $message =~ /From:\s+(.*?)\s+<(.*)>\s*\n/) {
563 ($author_name, $author_email) = ($1, $2);
564 print "Author from From: $1 <$2>\n" if ($opt_v);;
565 } elsif ($opt_S && $message =~ /Signed-off-by:\s+(.*?)\s+<(.*)>\s*\n/) {
566 ($author_name, $author_email) = ($1, $2);
567 print "Author from Signed-off-by: $1 <$2>\n" if ($opt_v);;
568 } else {
569 $author_name = $committer_name;
570 $author_email = $committer_email;
573 $date = pdate($date);
575 my $tag;
576 my $parent;
577 if($branch eq "/") { # trunk
578 $parent = $opt_o;
579 } elsif($branch =~ m#^/(.+)#) { # tag
580 $tag = 1;
581 $parent = $1;
582 } else { # "normal" branch
583 # nothing to do
584 $parent = $branch;
586 $dest = $parent;
588 my $prev = $changed_paths->{"/"};
589 if($prev and $prev->[0] eq "A") {
590 delete $changed_paths->{"/"};
591 my $oldpath = $prev->[1];
592 my $rev;
593 if(defined $oldpath) {
594 my $p;
595 ($parent,$p) = split_path($revision,$oldpath);
596 if(defined $parent) {
597 if($parent eq "/") {
598 $parent = $opt_o;
599 } else {
600 $parent =~ s#^/##; # if it's a tag
603 } else {
604 $parent = undef;
608 my $rev;
609 if($revision > $opt_s and defined $parent) {
610 open(H,"git-rev-parse --verify $parent |");
611 $rev = <H>;
612 close(H) or do {
613 print STDERR "$revision: cannot find commit '$parent'!\n";
614 return;
616 chop $rev;
617 if(length($rev) != 40) {
618 print STDERR "$revision: cannot find commit '$parent'!\n";
619 return;
621 $rev = $branches{($parent eq $opt_o) ? "/" : $parent}{"LAST"};
622 if($revision != $opt_s and not $rev) {
623 print STDERR "$revision: do not know ancestor for '$parent'!\n";
624 return;
626 } else {
627 $rev = undef;
630 # if($prev and $prev->[0] eq "A") {
631 # if(not $tag) {
632 # unless(open(H,"> $git_dir/refs/heads/$branch")) {
633 # print STDERR "$revision: Could not create branch $branch: $!\n";
634 # $state=11;
635 # next;
637 # print H "$rev\n"
638 # or die "Could not write branch $branch: $!";
639 # close(H)
640 # or die "Could not write branch $branch: $!";
643 if(not defined $rev) {
644 unlink($git_index);
645 } elsif ($rev ne $last_rev) {
646 print "Switching from $last_rev to $rev ($branch)\n" if $opt_v;
647 system("git-read-tree", $rev);
648 die "read-tree failed for $rev: $?\n" if $?;
649 $last_rev = $rev;
652 push (@parents, $rev) if defined $rev;
654 my $cid;
655 if($tag and not %$changed_paths) {
656 $cid = $rev;
657 } else {
658 my @paths = sort keys %$changed_paths;
659 foreach my $path(@paths) {
660 my $action = $changed_paths->{$path};
662 if ($action->[0] eq "R") {
663 # refer to a file/tree in an earlier commit
664 push(@old,$path); # remove any old stuff
666 if(($action->[0] eq "A") || ($action->[0] eq "R")) {
667 my $node_kind = node_kind($action->[3], $revision);
668 if ($node_kind eq $SVN::Node::file) {
669 my $f = get_file($action->[3],
670 $revision, $path);
671 if ($f) {
672 push(@new,$f) if $f;
673 } else {
674 my $opath = $action->[3];
675 print STDERR "$revision: $branch: could not fetch '$opath'\n";
677 } elsif ($node_kind eq $SVN::Node::dir) {
678 if($action->[1]) {
679 copy_path($revision, $branch,
680 $path, $action->[1],
681 $action->[2], $node_kind,
682 \@new, \@parents);
683 } else {
684 get_ignore(\@new, \@old, $revision,
685 $path, $action->[3]);
688 } elsif ($action->[0] eq "D") {
689 push(@old,$path);
690 } elsif ($action->[0] eq "M") {
691 my $node_kind = node_kind($action->[3], $revision);
692 if ($node_kind eq $SVN::Node::file) {
693 my $f = get_file($action->[3],
694 $revision, $path);
695 push(@new,$f) if $f;
696 } elsif ($node_kind eq $SVN::Node::dir) {
697 get_ignore(\@new, \@old, $revision,
698 $path, $action->[3]);
700 } else {
701 die "$revision: unknown action '".$action->[0]."' for $path\n";
705 while(@old) {
706 my @o1;
707 if(@old > 55) {
708 @o1 = splice(@old,0,50);
709 } else {
710 @o1 = @old;
711 @old = ();
713 my $pid = open my $F, "-|";
714 die "$!" unless defined $pid;
715 if (!$pid) {
716 exec("git-ls-files", "-z", @o1) or die $!;
718 @o1 = ();
719 local $/ = "\0";
720 while(<$F>) {
721 chomp;
722 push(@o1,$_);
724 close($F);
726 while(@o1) {
727 my @o2;
728 if(@o1 > 55) {
729 @o2 = splice(@o1,0,50);
730 } else {
731 @o2 = @o1;
732 @o1 = ();
734 system("git-update-index","--force-remove","--",@o2);
735 die "Cannot remove files: $?\n" if $?;
738 while(@new) {
739 my @n2;
740 if(@new > 12) {
741 @n2 = splice(@new,0,10);
742 } else {
743 @n2 = @new;
744 @new = ();
746 system("git-update-index","--add",
747 (map { ('--cacheinfo', @$_) } @n2));
748 die "Cannot add files: $?\n" if $?;
751 my $pid = open(C,"-|");
752 die "Cannot fork: $!" unless defined $pid;
753 unless($pid) {
754 exec("git-write-tree");
755 die "Cannot exec git-write-tree: $!\n";
757 chomp(my $tree = <C>);
758 length($tree) == 40
759 or die "Cannot get tree id ($tree): $!\n";
760 close(C)
761 or die "Error running git-write-tree: $?\n";
762 print "Tree ID $tree\n" if $opt_v;
764 my $pr = IO::Pipe->new() or die "Cannot open pipe: $!\n";
765 my $pw = IO::Pipe->new() or die "Cannot open pipe: $!\n";
766 $pid = fork();
767 die "Fork: $!\n" unless defined $pid;
768 unless($pid) {
769 $pr->writer();
770 $pw->reader();
771 open(OUT,">&STDOUT");
772 dup2($pw->fileno(),0);
773 dup2($pr->fileno(),1);
774 $pr->close();
775 $pw->close();
777 my @par = ();
779 # loose detection of merges
780 # based on the commit msg
781 foreach my $rx (@mergerx) {
782 if ($message =~ $rx) {
783 my $mparent = $1;
784 if ($mparent eq 'HEAD') { $mparent = $opt_o };
785 if ( -e "$git_dir/refs/heads/$mparent") {
786 $mparent = get_headref($mparent, $git_dir);
787 push (@parents, $mparent);
788 print OUT "Merge parent branch: $mparent\n" if $opt_v;
792 my %seen_parents = ();
793 my @unique_parents = grep { ! $seen_parents{$_} ++ } @parents;
794 foreach my $bparent (@unique_parents) {
795 push @par, '-p', $bparent;
796 print OUT "Merge parent branch: $bparent\n" if $opt_v;
799 exec("env",
800 "GIT_AUTHOR_NAME=$author_name",
801 "GIT_AUTHOR_EMAIL=$author_email",
802 "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
803 "GIT_COMMITTER_NAME=$committer_name",
804 "GIT_COMMITTER_EMAIL=$committer_email",
805 "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
806 "git-commit-tree", $tree,@par);
807 die "Cannot exec git-commit-tree: $!\n";
809 $pw->writer();
810 $pr->reader();
812 $message =~ s/[\s\n]+\z//;
813 $message = "r$revision: $message" if $opt_r;
815 print $pw "$message\n"
816 or die "Error writing to git-commit-tree: $!\n";
817 $pw->close();
819 print "Committed change $revision:$branch ".strftime("%Y-%m-%d %H:%M:%S",gmtime($date)).")\n" if $opt_v;
820 chomp($cid = <$pr>);
821 length($cid) == 40
822 or die "Cannot get commit id ($cid): $!\n";
823 print "Commit ID $cid\n" if $opt_v;
824 $pr->close();
826 waitpid($pid,0);
827 die "Error running git-commit-tree: $?\n" if $?;
830 if (not defined $cid) {
831 $cid = $branches{"/"}{"LAST"};
834 if(not defined $dest) {
835 print "... no known parent\n" if $opt_v;
836 } elsif(not $tag) {
837 print "Writing to refs/heads/$dest\n" if $opt_v;
838 open(C,">$git_dir/refs/heads/$dest") and
839 print C ("$cid\n") and
840 close(C)
841 or die "Cannot write branch $dest for update: $!\n";
844 if($tag) {
845 my($in, $out) = ('','');
846 $last_rev = "-" if %$changed_paths;
847 # the tag was 'complex', i.e. did not refer to a "real" revision
849 $dest =~ tr/_/\./ if $opt_u;
850 $branch = $dest;
852 my $pid = open2($in, $out, 'git-mktag');
853 print $out ("object $cid\n".
854 "type commit\n".
855 "tag $dest\n".
856 "tagger $committer_name <$committer_email> 0 +0000\n") and
857 close($out)
858 or die "Cannot create tag object $dest: $!\n";
860 my $tagobj = <$in>;
861 chomp $tagobj;
863 if ( !close($in) or waitpid($pid, 0) != $pid or
864 $? != 0 or $tagobj !~ /^[0123456789abcdef]{40}$/ ) {
865 die "Cannot create tag object $dest: $!\n";
868 open(C,">$git_dir/refs/tags/$dest") and
869 print C ("$tagobj\n") and
870 close(C)
871 or die "Cannot create tag $branch: $!\n";
873 print "Created tag '$dest' on '$branch'\n" if $opt_v;
875 $branches{$branch}{"LAST"} = $cid;
876 $branches{$branch}{$revision} = $cid;
877 $last_rev = $cid;
878 print BRANCHES "$revision $branch $cid\n";
879 print "DONE: $revision $dest $cid\n" if $opt_v;
882 sub commit_all {
883 # Recursive use of the SVN connection does not work
884 local $svn = $svn2;
886 my ($changed_paths, $revision, $author, $date, $message, $pool) = @_;
887 my %p;
888 while(my($path,$action) = each %$changed_paths) {
889 $p{$path} = [ $action->action,$action->copyfrom_path, $action->copyfrom_rev, $path ];
891 $changed_paths = \%p;
893 my %done;
894 my @col;
895 my $pref;
896 my $branch;
898 while(my($path,$action) = each %$changed_paths) {
899 ($branch,$path) = split_path($revision,$path);
900 next if not defined $branch;
901 $done{$branch}{$path} = $action;
903 while(($branch,$changed_paths) = each %done) {
904 commit($branch, $changed_paths, $revision, $author, $date, $message);
908 $opt_l = $svn->{'maxrev'} if not defined $opt_l or $opt_l > $svn->{'maxrev'};
910 if ($opt_l < $current_rev) {
911 print "Up to date: no new revisions to fetch!\n" if $opt_v;
912 unlink("$git_dir/SVN2GIT_HEAD");
913 exit;
916 print "Fetching from $current_rev to $opt_l ...\n" if $opt_v;
918 my $pool=SVN::Pool->new;
919 $svn->{'svn'}->get_log("/",$current_rev,$opt_l,0,1,1,\&commit_all,$pool);
920 $pool->clear;
923 unlink($git_index);
925 if (defined $orig_git_index) {
926 $ENV{GIT_INDEX_FILE} = $orig_git_index;
927 } else {
928 delete $ENV{GIT_INDEX_FILE};
931 # Now switch back to the branch we were in before all of this happened
932 if($orig_branch) {
933 print "DONE\n" if $opt_v and (not defined $opt_l or $opt_l > 0);
934 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
935 if $forward_master;
936 unless ($opt_i) {
937 system('git-read-tree', '-m', '-u', 'SVN2GIT_HEAD', 'HEAD');
938 die "read-tree failed: $?\n" if $?;
940 } else {
941 $orig_branch = "master";
942 print "DONE; creating $orig_branch branch\n" if $opt_v and (not defined $opt_l or $opt_l > 0);
943 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
944 unless -f "$git_dir/refs/heads/master";
945 system('git-update-ref', 'HEAD', "$orig_branch");
946 unless ($opt_i) {
947 system('git checkout');
948 die "checkout failed: $?\n" if $?;
951 unlink("$git_dir/SVN2GIT_HEAD");
952 close(BRANCHES);