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 CVS connection
11 # The head revision is on branch "origin" by default.
12 # You can change that with the '-o' option.
14 require v5.8.0; # for shell-safe open("-|",LIST)
19 use File::Temp qw(tempfile);
20 use File::Path qw(mkpath);
21 use File::Basename qw(basename dirname);
24 use POSIX qw(strftime dup2);
29 $SIG{'PIPE'}="IGNORE";
32 our($opt_h,$opt_o,$opt_v,$opt_u,$opt_C,$opt_i,$opt_m,$opt_M,$opt_t,$opt_T,$opt_b);
36 Usage: ${\basename $0} # fetch/update GIT from CVS
37 [-o branch-for-HEAD] [-h] [-v]
38 [-C GIT_repository] [-t tagname] [-T trunkname] [-b branchname]
39 [-i] [-u] [-s subst] [-m] [-M regex] [SVN_URL]
44 getopts("b:C:hivmM:o:t:T:u") or usage();
47 my $tag_name = $opt_t || "tags";
48 my $trunk_name = $opt_T || "trunk";
49 my $branch_name = $opt_b || "branches";
51 @ARGV <= 1 or usage();
54 my $git_tree = $opt_C;
60 } elsif (-f 'CVS/Repository') {
61 open my $f, '<', 'CVS/Repository' or
62 die 'Failed to open CVS/Repository';
72 @mergerx = ( qr/\W(?:from|of|merge|merging|merged) (\w+)/i );
75 push (@mergerx, qr/$opt_M/);
78 select(STDERR); $|=1; select(STDOUT);
82 # Basic SVN connection.
83 # We're only interested in connecting and downloading, so ...
86 use File::Temp qw(tempfile);
87 use POSIX qw(strftime dup2);
91 $what=ref($what) if ref($what);
94 $self->{'buffer'} = "";
98 $self->{'fullrep'} = $repo;
101 $self->{'lines'} = undef;
108 my $repo = $self->{'fullrep'};
109 my $s = SVN::Ra->new($repo);
111 die "SVN connection to $repo: $!\n" unless defined $s;
113 $self->{'repo'} = $repo;
114 $self->{'maxrev'} = $s->get_latest_revnum();
118 my($self,$path,$rev) = @_;
121 my ($fh, $name) = tempfile('gitsvn.XXXXXX',
122 DIR => File::Spec->tmpdir(), UNLINK => 1);
124 $self->{'svn'}->get_file($path,$rev,$fh) or do {
127 $self->{'svn'}->get_file($path,$rev,$fh)
128 or die "$rev: No file $path at $rev\n";
132 return ($name, $res);
138 my $svn = SVNconn->new($cvs_tree);
143 $d =~ m#(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)#
144 or die "Unparseable date: $d\n";
145 my $y=$1; $y-=1900 if $y>1900;
146 return timegm($6||0,$5,$4,$3,$2-1,$y);
154 for my $x(split(//,$mode)) {
159 } elsif($x eq "u") { $um |= 0700;
160 } elsif($x eq "g") { $um |= 0070;
161 } elsif($x eq "o") { $um |= 0007;
162 } elsif($x eq "r") { $mm |= 0444;
163 } elsif($x eq "w") { $mm |= 0222;
164 } elsif($x eq "x") { $mm |= 0111;
165 } elsif($x eq "=") { # do nothing
166 } else { die "Unknown mode: $mode\n";
180 sub get_headref($$) {
185 if (open(C,"$git_dir/refs/heads/$name")) {
189 or die "Cannot get head id for $name ($sha): $!\n";
196 or mkdir($git_tree,0777)
197 or die "Could not create $git_tree: $!";
200 my $orig_branch = "";
201 my $forward_master = 0;
204 my $git_dir = $ENV{"GIT_DIR"} || ".git";
205 $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#;
206 $ENV{"GIT_DIR"} = $git_dir;
208 $orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE};
209 my ($git_ih, $git_index) = tempfile('gitXXXXXX', SUFFIX => '.idx',
210 DIR => File::Spec->tmpdir());
212 $ENV{GIT_INDEX_FILE} = $git_index;
217 unless(-d $git_dir) {
218 system("git-init-db");
219 die "Cannot init the GIT db at $git_tree: $?\n" if $?;
220 system("git-read-tree");
221 die "Cannot init an empty tree: $?\n" if $?;
223 $last_branch = $opt_o;
226 -f "$git_dir/refs/heads/$opt_o"
227 or die "Branch '$opt_o' does not exist.\n".
228 "Either use the correct '-o branch' option,\n".
229 "or import to a new repository.\n";
231 -f "$git_dir/svn2git"
232 or die "'$git_dir/svn2git' does not exist.\n".
233 "You need that file for incremental imports.\n";
234 $last_branch = basename(readlink("$git_dir/HEAD"));
235 unless($last_branch) {
236 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
237 $last_branch = "master";
239 $orig_branch = $last_branch;
240 $last_rev = get_headref($orig_branch, $git_dir);
241 if (-f "$git_dir/SVN2GIT_HEAD") {
244 Make sure your working directory corresponds to HEAD and remove SVN2GIT_HEAD.
247 git-read-tree -m -u SVN2GIT_HEAD HEAD
250 system('cp', "$git_dir/HEAD", "$git_dir/SVN2GIT_HEAD");
253 $opt_o ne 'master' && -f "$git_dir/refs/heads/master" &&
254 system('cmp', '-s', "$git_dir/refs/heads/master",
255 "$git_dir/refs/heads/$opt_o") == 0;
258 system('git-read-tree', $last_rev);
259 die "read-tree failed: $?\n" if $?;
261 # Get the last import timestamps
262 open my $B,"<", "$git_dir/svn2git";
265 my($num,$branch,$ref) = split;
266 $branches{$branch}{$num} = $ref;
267 $branches{$branch}{"LAST"} = $ref;
268 $current_rev = $num+1 if $current_rev < $num+1;
273 or die "Could not create git subdir ($git_dir).\n";
275 open BRANCHES,">>", "$git_dir/svn2git";
279 #---------------------
281 #Date: 1999/09/18 13:03:59
283 #Branch: STABLE-BRANCH-1-0
284 #Ancestor branch: HEAD
287 # See ChangeLog: Sat Sep 18 13:03:28 CEST 1999 Werner Koch
289 # README:1.57->1.57.2.1
290 # VERSION:1.96->1.96.2.1
292 #---------------------
297 my($rev,$branch,$path) = @_;
299 # revert split_path(), below
301 $path = "" if $path eq "/"; # this should not happen, but ...
303 $svnpath = "/$trunk_name/$path";
304 } elsif($branch =~ m#^/#) {
305 $svnpath = "/$tag_name$branch/$path";
307 $svnpath = "/$branch_name/$branch/$path";
311 my ($name, $res) = $svn->file($svnpath,$rev);
313 open my $F, '-|', "git-hash-object -w $name"
314 or die "Cannot create object: $!\n";
318 # my $mode = pmode($cvs->{'mode'});
319 my $mode = "0644"; # SV does not seem to store any file modes
320 return [$mode, $sha, $path];
327 if($path =~ s#^/\Q$tag_name\E/([^/]+)/?##) {
329 } elsif($path =~ s#^/\Q$trunk_name\E/?##) {
331 } elsif($path =~ s#^/\Q$branch_name\E/([^/]+)/?##) {
334 print STDERR "$rev: Unrecognized path: $path\n";
337 $path = "/" if $path eq "";
338 return ($branch,$path);
342 my($branch, $changed_paths, $revision, $author, $date, $message) = @_;
343 my($author_name,$author_email,$dest);
346 if ($author =~ /^(.*?)\s+<(.*)>$/) {
347 ($author_name, $author_email) = ($1, $2);
349 $author =~ s/^<(.*)>$/$1/;
350 $author_name = $author_email = $author;
352 $date = pdate($date);
356 if($branch eq "/") { # trunk
358 } elsif($branch =~ m#^/(.+)#) { # tag
361 } else { # "normal" branch
367 my $prev = $changed_paths->{"/"};
368 if($prev and $prev->action eq "A") {
369 delete $changed_paths->{"/"};
370 my $oldpath = $prev->copyfrom_path;
372 if(defined $oldpath) {
374 ($parent,$p) = split_path($revision,$oldpath);
378 $parent =~ s#^/##; # if it's a tag
386 if(defined $parent) {
387 open(H,"git-rev-parse --verify $parent |");
390 print STDERR "$revision: cannot find commit '$parent'!\n";
394 if(length($rev) != 40) {
395 print STDERR "$revision: cannot find commit '$parent'!\n";
398 $rev = $branches{($parent eq $opt_o) ? "/" : $parent}{"LAST"};
399 if($revision != 1 and not $rev) {
400 print STDERR "$revision: do not know ancestor for '$parent'!\n";
407 # if($prev and $prev->action eq "A") {
409 # unless(open(H,"> $git_dir/refs/heads/$branch")) {
410 # print STDERR "$revision: Could not create branch $branch: $!\n";
415 # or die "Could not write branch $branch: $!";
417 # or die "Could not write branch $branch: $!";
420 if(not defined $rev) {
422 } elsif ($rev ne $last_rev) {
423 print "Switching from $last_rev to $rev ($branch)\n" if $opt_v;
424 system("git-read-tree", $rev);
425 die "read-tree failed for $rev: $?\n" if $?;
429 while(my($path,$action) = each %$changed_paths) {
430 if ($action->action eq "A") {
431 my $f = get_file($revision,$branch,$path);
433 } elsif ($action->action eq "D") {
435 } elsif ($action->action eq "M") {
436 my $f = get_file($revision,$branch,$path);
438 } elsif ($action->action eq "R") {
439 # refer to a file/tree in an earlier commit
440 push(@old,$path); # remove any old stuff
442 # ... and add any new stuff
443 my($b,$p) = split_path($revision,$action->oldpath);
444 open my $F,"-|","git-ls-tree","-r","-z", $branches{$b}{$action->oldrev}, $p;
448 my($m,$p) = split(/\t/,$_,2);
449 my($mode,$type,$sha1) = split(/ /,$m);
450 next if $type ne "blob";
451 push(@new,[$mode,$sha1,$p]);
454 die "$revision: unknown action '".$action->action."' for $path\n";
459 open F, "-│", "git-ls-files", "-z", @old or die $!;
471 @o2 = splice(@old,0,50);
476 system("git-update-index","--force-remove","--",@o2);
477 die "Cannot remove files: $?\n" if $?;
483 @n2 = splice(@new,0,10);
488 system("git-update-index","--add",
489 (map { ('--cacheinfo', @$_) } @n2));
490 die "Cannot add files: $?\n" if $?;
493 my $pid = open(C,"-|");
494 die "Cannot fork: $!" unless defined $pid;
496 exec("git-write-tree");
497 die "Cannot exec git-write-tree: $!\n";
499 chomp(my $tree = <C>);
501 or die "Cannot get tree id ($tree): $!\n";
503 or die "Error running git-write-tree: $?\n";
504 print "Tree ID $tree\n" if $opt_v;
506 my $pr = IO::Pipe->new() or die "Cannot open pipe: $!\n";
507 my $pw = IO::Pipe->new() or die "Cannot open pipe: $!\n";
509 die "Fork: $!\n" unless defined $pid;
513 open(OUT,">&STDOUT");
514 dup2($pw->fileno(),0);
515 dup2($pr->fileno(),1);
520 @par = ("-p",$rev) if defined $rev;
522 # loose detection of merges
523 # based on the commit msg
524 foreach my $rx (@mergerx) {
525 if ($message =~ $rx) {
527 if ($mparent eq 'HEAD') { $mparent = $opt_o };
528 if ( -e "$git_dir/refs/heads/$mparent") {
529 $mparent = get_headref($mparent, $git_dir);
530 push @par, '-p', $mparent;
531 print OUT "Merge parent branch: $mparent\n" if $opt_v;
537 "GIT_AUTHOR_NAME=$author_name",
538 "GIT_AUTHOR_EMAIL=$author_email",
539 "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
540 "GIT_COMMITTER_NAME=$author_name",
541 "GIT_COMMITTER_EMAIL=$author_email",
542 "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
543 "git-commit-tree", $tree,@par);
544 die "Cannot exec git-commit-tree: $!\n";
549 $message =~ s/[\s\n]+\z//;
551 print $pw "$message\n"
552 or die "Error writing to git-commit-tree: $!\n";
555 print "Committed change $revision:$branch ".strftime("%Y-%m-%d %H:%M:%S",gmtime($date)).")\n" if $opt_v;
556 chomp(my $cid = <$pr>);
558 or die "Cannot get commit id ($cid): $!\n";
559 print "Commit ID $cid\n" if $opt_v;
563 die "Error running git-commit-tree: $?\n" if $?;
566 print "Writing to refs/heads/$dest\n" if $opt_v;
567 open(C,">$git_dir/refs/heads/$dest") and
568 print C ("$cid\n") and
570 or die "Cannot write branch $dest for update: $!\n";
572 print "... no known parent\n" if $opt_v;
574 $branches{$branch}{"LAST"} = $cid;
575 $branches{$branch}{$revision} = $cid;
577 print BRANCHES "$revision $branch $cid\n";
578 print "DONE: $revision $dest $cid\n" if $opt_v;
581 my($in, $out) = ('','');
582 $last_rev = "-" if %$changed_paths;
583 # the tag was 'complex', i.e. did not refer to a "real" revision
585 $tag =~ tr/_/\./ if $opt_u;
587 my $pid = open2($in, $out, 'git-mktag');
588 print $out ("object $cid\n".
591 "tagger $author_name <$author_email>\n") and
593 or die "Cannot create tag object $tag: $!\n";
598 if ( !close($in) or waitpid($pid, 0) != $pid or
599 $? != 0 or $tagobj !~ /^[0123456789abcdef]{40}$/ ) {
600 die "Cannot create tag object $tag: $!\n";
604 open(C,">$git_dir/refs/tags/$tag")
605 or die "Cannot create tag $tag: $!\n";
607 or die "Cannot write tag $tag: $!\n";
609 or die "Cannot write tag $tag: $!\n";
611 print "Created tag '$tag' on '$branch'\n" if $opt_v;
615 my ($changed_paths, $revision, $author, $date, $message, $pool) = @_;
617 ($changed_paths, $revision, $author, $date, $message, $pool) = @_;
625 while(my($path,$action) = each %$changed_paths) {
626 ($branch,$path) = split_path($revision,$path);
627 next if not defined $branch;
628 $done{$branch}{$path} = $action;
630 while(($branch,$changed_paths) = each %done) {
631 commit($branch, $changed_paths, $revision, $author, $date, $message);
635 while(++$current_rev < $svn->{'maxrev'}) {
636 $svn->{'svn'}->get_log("/",$current_rev,$current_rev,$current_rev,1,1,\&_commit_all,"");
643 if (defined $orig_git_index) {
644 $ENV{GIT_INDEX_FILE} = $orig_git_index;
646 delete $ENV{GIT_INDEX_FILE};
649 # Now switch back to the branch we were in before all of this happened
651 print "DONE\n" if $opt_v;
652 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
655 system('git-read-tree', '-m', '-u', 'SVN2GIT_HEAD', 'HEAD');
656 die "read-tree failed: $?\n" if $?;
659 $orig_branch = "master";
660 print "DONE; creating $orig_branch branch\n" if $opt_v;
661 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
662 unless -f "$git_dir/refs/heads/master";
663 unlink("$git_dir/HEAD");
664 symlink("refs/heads/$orig_branch","$git_dir/HEAD");
666 system('git checkout');
667 die "checkout failed: $?\n" if $?;
670 unlink("$git_dir/SVN2GIT_HEAD");