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 require v5.8.0; # for shell-safe open("-|",LIST)
18 use File::Temp qw(tempfile);
19 use File::Path qw(mkpath);
20 use File::Basename qw(basename dirname);
23 use POSIX qw(strftime dup2);
28 die "Need CVN:Core 1.2.1 or better" if $SVN::Core::VERSION lt "1.2.1";
30 $SIG{'PIPE'}="IGNORE";
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);
37 Usage: ${\basename $0} # fetch/update GIT from CVS
38 [-o branch-for-HEAD] [-h] [-v] [-l max_num_changes]
39 [-C GIT_repository] [-t tagname] [-T trunkname] [-b branchname]
40 [-i] [-u] [-s start_chg] [-m] [-M regex] [SVN_URL]
45 getopts("b:C:hil:mM:o:s:t:T:uv") or usage();
48 my $tag_name = $opt_t || "tags";
49 my $trunk_name = $opt_T || "trunk";
50 my $branch_name = $opt_b || "branches";
52 @ARGV == 1 or usage();
56 $opt_l = 100 unless defined $opt_l;
57 my $git_tree = $opt_C;
60 my $svn_url = $ARGV[0];
64 @mergerx = ( qr/\W(?:from|of|merge|merging|merged) (\w+)/i );
67 push (@mergerx, qr/$opt_M/);
70 select(STDERR); $|=1; select(STDOUT);
74 # Basic SVN connection.
75 # We're only interested in connecting and downloading, so ...
78 use File::Temp qw(tempfile);
79 use POSIX qw(strftime dup2);
83 $what=ref($what) if ref($what);
86 $self->{'buffer'} = "";
90 $self->{'fullrep'} = $repo;
98 my $repo = $self->{'fullrep'};
99 my $s = SVN::Ra->new($repo);
101 die "SVN connection to $repo: $!\n" unless defined $s;
103 $self->{'repo'} = $repo;
104 $self->{'maxrev'} = $s->get_latest_revnum();
108 my($self,$path,$rev) = @_;
111 my ($fh, $name) = tempfile('gitsvn.XXXXXX',
112 DIR => File::Spec->tmpdir(), UNLINK => 1);
114 print "... $rev $path ...\n" if $opt_v;
115 eval { $self->{'svn'}->get_file($path,$rev,$fh); };
116 if ($@ and $@ !~ /Attempted to get checksum/) {
119 eval { $self->{'svn'}->get_file($path,$rev,$fh); };
121 return () if $@ and $@ !~ /Attempted to get checksum/;
125 return ($name, $res);
131 my $svn = SVNconn->new($svn_url);
136 $d =~ m#(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)#
137 or die "Unparseable date: $d\n";
138 my $y=$1; $y-=1900 if $y>1900;
139 return timegm($6||0,$5,$4,$3,$2-1,$y);
149 sub get_headref($$) {
154 if (open(C,"$git_dir/refs/heads/$name")) {
158 or die "Cannot get head id for $name ($sha): $!\n";
165 or mkdir($git_tree,0777)
166 or die "Could not create $git_tree: $!";
169 my $orig_branch = "";
170 my $forward_master = 0;
173 my $git_dir = $ENV{"GIT_DIR"} || ".git";
174 $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#;
175 $ENV{"GIT_DIR"} = $git_dir;
177 $orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE};
178 my ($git_ih, $git_index) = tempfile('gitXXXXXX', SUFFIX => '.idx',
179 DIR => File::Spec->tmpdir());
181 $ENV{GIT_INDEX_FILE} = $git_index;
185 my $current_rev = $opt_s-1;
186 unless(-d $git_dir) {
187 system("git-init-db");
188 die "Cannot init the GIT db at $git_tree: $?\n" if $?;
189 system("git-read-tree");
190 die "Cannot init an empty tree: $?\n" if $?;
192 $last_branch = $opt_o;
195 -f "$git_dir/refs/heads/$opt_o"
196 or die "Branch '$opt_o' does not exist.\n".
197 "Either use the correct '-o branch' option,\n".
198 "or import to a new repository.\n";
200 -f "$git_dir/svn2git"
201 or die "'$git_dir/svn2git' does not exist.\n".
202 "You need that file for incremental imports.\n";
203 $last_branch = basename(readlink("$git_dir/HEAD"));
204 unless($last_branch) {
205 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
206 $last_branch = "master";
208 $orig_branch = $last_branch;
209 $last_rev = get_headref($orig_branch, $git_dir);
210 if (-f "$git_dir/SVN2GIT_HEAD") {
213 Make sure your working directory corresponds to HEAD and remove SVN2GIT_HEAD.
216 git-read-tree -m -u SVN2GIT_HEAD HEAD
219 system('cp', "$git_dir/HEAD", "$git_dir/SVN2GIT_HEAD");
222 $opt_o ne 'master' && -f "$git_dir/refs/heads/master" &&
223 system('cmp', '-s', "$git_dir/refs/heads/master",
224 "$git_dir/refs/heads/$opt_o") == 0;
227 system('git-read-tree', $last_rev);
228 die "read-tree failed: $?\n" if $?;
230 # Get the last import timestamps
231 open my $B,"<", "$git_dir/svn2git";
234 my($num,$branch,$ref) = split;
235 $branches{$branch}{$num} = $ref;
236 $branches{$branch}{"LAST"} = $ref;
237 $current_rev = $num if $current_rev < $num;
242 or die "Could not create git subdir ($git_dir).\n";
244 open BRANCHES,">>", "$git_dir/svn2git";
247 my($rev,$branch,$path) = @_;
249 # revert split_path(), below
251 $path = "" if $path eq "/"; # this should not happen, but ...
253 $svnpath = "/$trunk_name/$path";
254 } elsif($branch =~ m#^/#) {
255 $svnpath = "/$tag_name$branch/$path";
257 $svnpath = "/$branch_name/$branch/$path";
261 my ($name, $res) = eval { $svn->file($svnpath,$rev); };
262 return () unless defined $name;
264 open my $F, '-|', "git-hash-object", "-w", $name
265 or die "Cannot create object: $!\n";
270 my $mode = "0644"; # SV does not seem to store any file modes
271 return [$mode, $sha, $path];
278 if($path =~ s#^/\Q$tag_name\E/([^/]+)/?##) {
280 } elsif($path =~ s#^/\Q$trunk_name\E/?##) {
282 } elsif($path =~ s#^/\Q$branch_name\E/([^/]+)/?##) {
285 print STDERR "$rev: Unrecognized path: $path\n";
288 $path = "/" if $path eq "";
289 return ($branch,$path);
293 my($branch, $changed_paths, $revision, $author, $date, $message) = @_;
294 my($author_name,$author_email,$dest);
297 if (not defined $author) {
298 $author_name = $author_email = "unknown";
299 } elsif ($author =~ /^(.*?)\s+<(.*)>$/) {
300 ($author_name, $author_email) = ($1, $2);
302 $author =~ s/^<(.*)>$/$1/;
303 $author_name = $author_email = $author;
305 $date = pdate($date);
309 if($branch eq "/") { # trunk
311 } elsif($branch =~ m#^/(.+)#) { # tag
314 } else { # "normal" branch
320 my $prev = $changed_paths->{"/"};
321 if($prev and $prev->[0] eq "A") {
322 delete $changed_paths->{"/"};
323 my $oldpath = $prev->[1];
325 if(defined $oldpath) {
327 ($parent,$p) = split_path($revision,$oldpath);
331 $parent =~ s#^/##; # if it's a tag
339 if($revision > $opt_s and defined $parent) {
340 open(H,"git-rev-parse --verify $parent |");
343 print STDERR "$revision: cannot find commit '$parent'!\n";
347 if(length($rev) != 40) {
348 print STDERR "$revision: cannot find commit '$parent'!\n";
351 $rev = $branches{($parent eq $opt_o) ? "/" : $parent}{"LAST"};
352 if($revision != $opt_s and not $rev) {
353 print STDERR "$revision: do not know ancestor for '$parent'!\n";
360 # if($prev and $prev->[0] eq "A") {
362 # unless(open(H,"> $git_dir/refs/heads/$branch")) {
363 # print STDERR "$revision: Could not create branch $branch: $!\n";
368 # or die "Could not write branch $branch: $!";
370 # or die "Could not write branch $branch: $!";
373 if(not defined $rev) {
375 } elsif ($rev ne $last_rev) {
376 print "Switching from $last_rev to $rev ($branch)\n" if $opt_v;
377 system("git-read-tree", $rev);
378 die "read-tree failed for $rev: $?\n" if $?;
383 if($tag and not %$changed_paths) {
386 while(my($path,$action) = each %$changed_paths) {
387 if ($action->[0] eq "A") {
388 my $f = get_file($revision,$branch,$path);
390 } elsif ($action->[0] eq "D") {
392 } elsif ($action->[0] eq "M") {
393 my $f = get_file($revision,$branch,$path);
395 } elsif ($action->[0] eq "R") {
396 # refer to a file/tree in an earlier commit
397 push(@old,$path); # remove any old stuff
399 # ... and add any new stuff
400 my($b,$p) = split_path($revision,$action->[1]);
401 open my $F,"-|","git-ls-tree","-r","-z", $branches{$b}{$action->[2]}, $p;
405 my($m,$p) = split(/\t/,$_,2);
406 my($mode,$type,$sha1) = split(/ /,$m);
407 next if $type ne "blob";
408 push(@new,[$mode,$sha1,$p]);
411 die "$revision: unknown action '".$action->[0]."' for $path\n";
416 open my $F, "-|", "git-ls-files", "-z", @old or die $!;
428 @o2 = splice(@old,0,50);
433 system("git-update-index","--force-remove","--",@o2);
434 die "Cannot remove files: $?\n" if $?;
440 @n2 = splice(@new,0,10);
445 system("git-update-index","--add",
446 (map { ('--cacheinfo', @$_) } @n2));
447 die "Cannot add files: $?\n" if $?;
450 my $pid = open(C,"-|");
451 die "Cannot fork: $!" unless defined $pid;
453 exec("git-write-tree");
454 die "Cannot exec git-write-tree: $!\n";
456 chomp(my $tree = <C>);
458 or die "Cannot get tree id ($tree): $!\n";
460 or die "Error running git-write-tree: $?\n";
461 print "Tree ID $tree\n" if $opt_v;
463 my $pr = IO::Pipe->new() or die "Cannot open pipe: $!\n";
464 my $pw = IO::Pipe->new() or die "Cannot open pipe: $!\n";
466 die "Fork: $!\n" unless defined $pid;
470 open(OUT,">&STDOUT");
471 dup2($pw->fileno(),0);
472 dup2($pr->fileno(),1);
477 @par = ("-p",$rev) if defined $rev;
479 # loose detection of merges
480 # based on the commit msg
481 foreach my $rx (@mergerx) {
482 if ($message =~ $rx) {
484 if ($mparent eq 'HEAD') { $mparent = $opt_o };
485 if ( -e "$git_dir/refs/heads/$mparent") {
486 $mparent = get_headref($mparent, $git_dir);
487 push @par, '-p', $mparent;
488 print OUT "Merge parent branch: $mparent\n" if $opt_v;
494 "GIT_AUTHOR_NAME=$author_name",
495 "GIT_AUTHOR_EMAIL=$author_email",
496 "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
497 "GIT_COMMITTER_NAME=$author_name",
498 "GIT_COMMITTER_EMAIL=$author_email",
499 "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
500 "git-commit-tree", $tree,@par);
501 die "Cannot exec git-commit-tree: $!\n";
506 $message =~ s/[\s\n]+\z//;
508 print $pw "$message\n"
509 or die "Error writing to git-commit-tree: $!\n";
512 print "Committed change $revision:$branch ".strftime("%Y-%m-%d %H:%M:%S",gmtime($date)).")\n" if $opt_v;
515 or die "Cannot get commit id ($cid): $!\n";
516 print "Commit ID $cid\n" if $opt_v;
520 die "Error running git-commit-tree: $?\n" if $?;
523 if(not defined $dest) {
524 print "... no known parent\n" if $opt_v;
526 print "Writing to refs/heads/$dest\n" if $opt_v;
527 open(C,">$git_dir/refs/heads/$dest") and
528 print C ("$cid\n") and
530 or die "Cannot write branch $dest for update: $!\n";
534 my($in, $out) = ('','');
535 $last_rev = "-" if %$changed_paths;
536 # the tag was 'complex', i.e. did not refer to a "real" revision
538 $dest =~ tr/_/\./ if $opt_u;
540 my $pid = open2($in, $out, 'git-mktag');
541 print $out ("object $cid\n".
544 "tagger $author_name <$author_email>\n") and
546 or die "Cannot create tag object $dest: $!\n";
551 if ( !close($in) or waitpid($pid, 0) != $pid or
552 $? != 0 or $tagobj !~ /^[0123456789abcdef]{40}$/ ) {
553 die "Cannot create tag object $dest: $!\n";
556 open(C,">$git_dir/refs/tags/$dest") and
557 print C ("$tagobj\n") and
559 or die "Cannot create tag $branch: $!\n";
561 print "Created tag '$dest' on '$branch'\n" if $opt_v;
563 $branches{$branch}{"LAST"} = $cid;
564 $branches{$branch}{$revision} = $cid;
566 print BRANCHES "$revision $branch $cid\n";
567 print "DONE: $revision $dest $cid\n" if $opt_v;
570 my ($changed_paths, $revision, $author, $date, $message, $pool) = @_;
572 ($changed_paths, $revision, $author, $date, $message, $pool) = @_;
574 while(my($path,$action) = each %$changed_paths) {
575 $p{$path} = [ $action->action,$action->copyfrom_path, $action->copyfrom_rev ];
577 $changed_paths = \%p;
586 while(my($path,$action) = each %$changed_paths) {
587 ($branch,$path) = split_path($revision,$path);
588 next if not defined $branch;
589 $done{$branch}{$path} = $action;
591 while(($branch,$changed_paths) = each %done) {
592 commit($branch, $changed_paths, $revision, $author, $date, $message);
596 while(++$current_rev <= $svn->{'maxrev'}) {
597 $svn->{'svn'}->get_log("/",$current_rev,$current_rev,$current_rev,1,1,\&_commit_all,"");
599 if($opt_l and not --$opt_l) {
600 print STDERR "Exiting due to a memory leak. Repeat, please.\n";
608 if (defined $orig_git_index) {
609 $ENV{GIT_INDEX_FILE} = $orig_git_index;
611 delete $ENV{GIT_INDEX_FILE};
614 # Now switch back to the branch we were in before all of this happened
616 print "DONE\n" if $opt_v and (not defined $opt_l or $opt_l > 0);
617 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
620 system('git-read-tree', '-m', '-u', 'SVN2GIT_HEAD', 'HEAD');
621 die "read-tree failed: $?\n" if $?;
624 $orig_branch = "master";
625 print "DONE; creating $orig_branch branch\n" if $opt_v and (not defined $opt_l or $opt_l > 0);
626 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
627 unless -f "$git_dir/refs/heads/master";
628 unlink("$git_dir/HEAD");
629 symlink("refs/heads/$orig_branch","$git_dir/HEAD");
631 system('git checkout');
632 die "checkout failed: $?\n" if $?;
635 unlink("$git_dir/SVN2GIT_HEAD");