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 die "Need CVN:COre 1.2.1 or better" if $SVN::Core
::VERSION
lt "1.2.1";
31 $SIG{'PIPE'}="IGNORE";
34 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);
38 Usage: ${\basename $0} # fetch/update GIT from CVS
39 [-o branch-for-HEAD] [-h] [-v] [-l max_num_changes]
40 [-C GIT_repository] [-t tagname] [-T trunkname] [-b branchname]
41 [-i] [-u] [-s start_chg] [-m] [-M regex] [SVN_URL]
46 getopts
("b:C:hil:mM:o:s:t:T:uv") or usage
();
49 my $tag_name = $opt_t || "tags";
50 my $trunk_name = $opt_T || "trunk";
51 my $branch_name = $opt_b || "branches";
53 @ARGV <= 1 or usage
();
56 $opt_l = 100 unless defined $opt_l;
57 my $git_tree = $opt_C;
63 } elsif (-f
'CVS/Repository') {
64 open my $f, '<', 'CVS/Repository' or
65 die 'Failed to open CVS/Repository';
75 @mergerx = ( qr/\W(?:from|of|merge|merging|merged) (\w+)/i );
78 push (@mergerx, qr/$opt_M/);
81 select(STDERR
); $|=1; select(STDOUT
);
85 # Basic SVN connection.
86 # We're only interested in connecting and downloading, so ...
89 use File
::Temp
qw(tempfile);
90 use POSIX
qw(strftime dup2);
94 $what=ref($what) if ref($what);
97 $self->{'buffer'} = "";
101 $self->{'fullrep'} = $repo;
109 my $repo = $self->{'fullrep'};
110 my $s = SVN
::Ra
->new($repo);
112 die "SVN connection to $repo: $!\n" unless defined $s;
114 $self->{'repo'} = $repo;
115 $self->{'maxrev'} = $s->get_latest_revnum();
119 my($self,$path,$rev) = @_;
122 my ($fh, $name) = tempfile
('gitsvn.XXXXXX',
123 DIR
=> File
::Spec
->tmpdir(), UNLINK
=> 1);
125 print "... $rev $path ...\n" if $opt_v;
126 eval { $self->{'svn'}->get_file($path,$rev,$fh); };
127 if ($@
and $@
!~ /Attempted to get checksum/) {
130 eval { $self->{'svn'}->get_file($path,$rev,$fh); };
132 return () if $@
and $@
!~ /Attempted to get checksum/;
136 return ($name, $res);
142 my $svn = SVNconn
->new($cvs_tree);
147 $d =~ m
#(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)#
148 or die "Unparseable date: $d\n";
149 my $y=$1; $y-=1900 if $y>1900;
150 return timegm
($6||0,$5,$4,$3,$2-1,$y);
160 sub get_headref
($$) {
165 if (open(C
,"$git_dir/refs/heads/$name")) {
169 or die "Cannot get head id for $name ($sha): $!\n";
176 or mkdir($git_tree,0777)
177 or die "Could not create $git_tree: $!";
180 my $orig_branch = "";
181 my $forward_master = 0;
184 my $git_dir = $ENV{"GIT_DIR"} || ".git";
185 $git_dir = getwd
()."/".$git_dir unless $git_dir =~ m
#^/#;
186 $ENV{"GIT_DIR"} = $git_dir;
188 $orig_git_index = $ENV{GIT_INDEX_FILE
} if exists $ENV{GIT_INDEX_FILE
};
189 my ($git_ih, $git_index) = tempfile
('gitXXXXXX', SUFFIX
=> '.idx',
190 DIR
=> File
::Spec
->tmpdir());
192 $ENV{GIT_INDEX_FILE
} = $git_index;
196 my $current_rev = $opt_s ?
($opt_s-1) : 0;
197 unless(-d
$git_dir) {
198 system("git-init-db");
199 die "Cannot init the GIT db at $git_tree: $?\n" if $?
;
200 system("git-read-tree");
201 die "Cannot init an empty tree: $?\n" if $?
;
203 $last_branch = $opt_o;
206 -f
"$git_dir/refs/heads/$opt_o"
207 or die "Branch '$opt_o' does not exist.\n".
208 "Either use the correct '-o branch' option,\n".
209 "or import to a new repository.\n";
211 -f
"$git_dir/svn2git"
212 or die "'$git_dir/svn2git' does not exist.\n".
213 "You need that file for incremental imports.\n";
214 $last_branch = basename
(readlink("$git_dir/HEAD"));
215 unless($last_branch) {
216 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
217 $last_branch = "master";
219 $orig_branch = $last_branch;
220 $last_rev = get_headref
($orig_branch, $git_dir);
221 if (-f
"$git_dir/SVN2GIT_HEAD") {
224 Make sure your working directory corresponds to HEAD and remove SVN2GIT_HEAD.
227 git-read-tree -m -u SVN2GIT_HEAD HEAD
230 system('cp', "$git_dir/HEAD", "$git_dir/SVN2GIT_HEAD");
233 $opt_o ne 'master' && -f
"$git_dir/refs/heads/master" &&
234 system('cmp', '-s', "$git_dir/refs/heads/master",
235 "$git_dir/refs/heads/$opt_o") == 0;
238 system('git-read-tree', $last_rev);
239 die "read-tree failed: $?\n" if $?
;
241 # Get the last import timestamps
242 open my $B,"<", "$git_dir/svn2git";
245 my($num,$branch,$ref) = split;
246 $branches{$branch}{$num} = $ref;
247 $branches{$branch}{"LAST"} = $ref;
248 $current_rev = $num if $current_rev < $num;
253 or die "Could not create git subdir ($git_dir).\n";
255 open BRANCHES
,">>", "$git_dir/svn2git";
259 #---------------------
261 #Date: 1999/09/18 13:03:59
263 #Branch: STABLE-BRANCH-1-0
264 #Ancestor branch: HEAD
267 # See ChangeLog: Sat Sep 18 13:03:28 CEST 1999 Werner Koch
269 # README:1.57->1.57.2.1
270 # VERSION:1.96->1.96.2.1
272 #---------------------
277 my($rev,$branch,$path) = @_;
279 # revert split_path(), below
281 $path = "" if $path eq "/"; # this should not happen, but ...
283 $svnpath = "/$trunk_name/$path";
284 } elsif($branch =~ m
#^/#) {
285 $svnpath = "/$tag_name$branch/$path";
287 $svnpath = "/$branch_name/$branch/$path";
291 my ($name, $res) = eval { $svn->file($svnpath,$rev); };
292 return () unless defined $name;
294 open my $F, '-|', "git-hash-object", "-w", $name
295 or die "Cannot create object: $!\n";
300 my $mode = "0644"; # SV does not seem to store any file modes
301 return [$mode, $sha, $path];
308 if($path =~ s
#^/\Q$tag_name\E/([^/]+)/?##) {
310 } elsif($path =~ s
#^/\Q$trunk_name\E/?##) {
312 } elsif($path =~ s
#^/\Q$branch_name\E/([^/]+)/?##) {
315 print STDERR
"$rev: Unrecognized path: $path\n";
318 $path = "/" if $path eq "";
319 return ($branch,$path);
323 my($branch, $changed_paths, $revision, $author, $date, $message) = @_;
324 my($author_name,$author_email,$dest);
327 if (not defined $author) {
328 $author_name = $author_email = "unknown";
329 } elsif ($author =~ /^(.*?)\s+<(.*)>$/) {
330 ($author_name, $author_email) = ($1, $2);
332 $author =~ s/^<(.*)>$/$1/;
333 $author_name = $author_email = $author;
335 $date = pdate
($date);
339 if($branch eq "/") { # trunk
341 } elsif($branch =~ m
#^/(.+)#) { # tag
344 } else { # "normal" branch
350 my $prev = $changed_paths->{"/"};
351 if($prev and $prev->[0] eq "A") {
352 delete $changed_paths->{"/"};
353 my $oldpath = $prev->[1];
355 if(defined $oldpath) {
357 ($parent,$p) = split_path
($revision,$oldpath);
361 $parent =~ s
#^/##; # if it's a tag
369 if($revision > $opt_s and defined $parent) {
370 open(H
,"git-rev-parse --verify $parent |");
373 print STDERR
"$revision: cannot find commit '$parent'!\n";
377 if(length($rev) != 40) {
378 print STDERR
"$revision: cannot find commit '$parent'!\n";
381 $rev = $branches{($parent eq $opt_o) ?
"/" : $parent}{"LAST"};
382 if($revision != $opt_s and not $rev) {
383 print STDERR
"$revision: do not know ancestor for '$parent'!\n";
390 # if($prev and $prev->[0] eq "A") {
392 # unless(open(H,"> $git_dir/refs/heads/$branch")) {
393 # print STDERR "$revision: Could not create branch $branch: $!\n";
398 # or die "Could not write branch $branch: $!";
400 # or die "Could not write branch $branch: $!";
403 if(not defined $rev) {
405 } elsif ($rev ne $last_rev) {
406 print "Switching from $last_rev to $rev ($branch)\n" if $opt_v;
407 system("git-read-tree", $rev);
408 die "read-tree failed for $rev: $?\n" if $?
;
413 if($tag and not %$changed_paths) {
416 while(my($path,$action) = each %$changed_paths) {
417 if ($action->[0] eq "A") {
418 my $f = get_file
($revision,$branch,$path);
420 } elsif ($action->[0] eq "D") {
422 } elsif ($action->[0] eq "M") {
423 my $f = get_file
($revision,$branch,$path);
425 } elsif ($action->[0] eq "R") {
426 # refer to a file/tree in an earlier commit
427 push(@old,$path); # remove any old stuff
429 # ... and add any new stuff
430 my($b,$p) = split_path
($revision,$action->[1]);
431 open my $F,"-|","git-ls-tree","-r","-z", $branches{$b}{$action->[2]}, $p;
435 my($m,$p) = split(/\t/,$_,2);
436 my($mode,$type,$sha1) = split(/ /,$m);
437 next if $type ne "blob";
438 push(@new,[$mode,$sha1,$p]);
441 die "$revision: unknown action '".$action->[0]."' for $path\n";
446 open my $F, "-|", "git-ls-files", "-z", @old or die $!;
458 @o2 = splice(@old,0,50);
463 system("git-update-index","--force-remove","--",@o2);
464 die "Cannot remove files: $?\n" if $?
;
470 @n2 = splice(@new,0,10);
475 system("git-update-index","--add",
476 (map { ('--cacheinfo', @
$_) } @n2));
477 die "Cannot add files: $?\n" if $?
;
480 my $pid = open(C
,"-|");
481 die "Cannot fork: $!" unless defined $pid;
483 exec("git-write-tree");
484 die "Cannot exec git-write-tree: $!\n";
486 chomp(my $tree = <C
>);
488 or die "Cannot get tree id ($tree): $!\n";
490 or die "Error running git-write-tree: $?\n";
491 print "Tree ID $tree\n" if $opt_v;
493 my $pr = IO
::Pipe
->new() or die "Cannot open pipe: $!\n";
494 my $pw = IO
::Pipe
->new() or die "Cannot open pipe: $!\n";
496 die "Fork: $!\n" unless defined $pid;
500 open(OUT
,">&STDOUT");
501 dup2
($pw->fileno(),0);
502 dup2
($pr->fileno(),1);
507 @par = ("-p",$rev) if defined $rev;
509 # loose detection of merges
510 # based on the commit msg
511 foreach my $rx (@mergerx) {
512 if ($message =~ $rx) {
514 if ($mparent eq 'HEAD') { $mparent = $opt_o };
515 if ( -e
"$git_dir/refs/heads/$mparent") {
516 $mparent = get_headref
($mparent, $git_dir);
517 push @par, '-p', $mparent;
518 print OUT
"Merge parent branch: $mparent\n" if $opt_v;
524 "GIT_AUTHOR_NAME=$author_name",
525 "GIT_AUTHOR_EMAIL=$author_email",
526 "GIT_AUTHOR_DATE=".strftime
("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
527 "GIT_COMMITTER_NAME=$author_name",
528 "GIT_COMMITTER_EMAIL=$author_email",
529 "GIT_COMMITTER_DATE=".strftime
("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
530 "git-commit-tree", $tree,@par);
531 die "Cannot exec git-commit-tree: $!\n";
536 $message =~ s/[\s\n]+\z//;
538 print $pw "$message\n"
539 or die "Error writing to git-commit-tree: $!\n";
542 print "Committed change $revision:$branch ".strftime
("%Y-%m-%d %H:%M:%S",gmtime($date)).")\n" if $opt_v;
545 or die "Cannot get commit id ($cid): $!\n";
546 print "Commit ID $cid\n" if $opt_v;
550 die "Error running git-commit-tree: $?\n" if $?
;
553 if(not defined $dest) {
554 print "... no known parent\n" if $opt_v;
556 print "Writing to refs/heads/$dest\n" if $opt_v;
557 open(C
,">$git_dir/refs/heads/$dest") and
558 print C
("$cid\n") and
560 or die "Cannot write branch $dest for update: $!\n";
564 my($in, $out) = ('','');
565 $last_rev = "-" if %$changed_paths;
566 # the tag was 'complex', i.e. did not refer to a "real" revision
568 $dest =~ tr/_/\./ if $opt_u;
570 my $pid = open2
($in, $out, 'git-mktag');
571 print $out ("object $cid\n".
574 "tagger $author_name <$author_email>\n") and
576 or die "Cannot create tag object $dest: $!\n";
581 if ( !close($in) or waitpid($pid, 0) != $pid or
582 $?
!= 0 or $tagobj !~ /^[0123456789abcdef]{40}$/ ) {
583 die "Cannot create tag object $dest: $!\n";
586 open(C
,">$git_dir/refs/tags/$dest") and
587 print C
("$tagobj\n") and
589 or die "Cannot create tag $branch: $!\n";
591 print "Created tag '$dest' on '$branch'\n" if $opt_v;
593 $branches{$branch}{"LAST"} = $cid;
594 $branches{$branch}{$revision} = $cid;
596 print BRANCHES
"$revision $branch $cid\n";
597 print "DONE: $revision $dest $cid\n" if $opt_v;
600 my ($changed_paths, $revision, $author, $date, $message, $pool) = @_;
602 ($changed_paths, $revision, $author, $date, $message, $pool) = @_;
604 while(my($path,$action) = each %$changed_paths) {
605 $p{$path} = [ $action->action,$action->copyfrom_path, $action->copyfrom_rev ];
607 $changed_paths = \
%p;
616 while(my($path,$action) = each %$changed_paths) {
617 ($branch,$path) = split_path
($revision,$path);
618 next if not defined $branch;
619 $done{$branch}{$path} = $action;
621 while(($branch,$changed_paths) = each %done) {
622 commit
($branch, $changed_paths, $revision, $author, $date, $message);
626 while(++$current_rev < $svn->{'maxrev'}) {
627 $svn->{'svn'}->get_log("/",$current_rev,$current_rev,$current_rev,1,1,\
&_commit_all
,"");
629 if($opt_l and not --$opt_l) {
630 print STDERR
"Exiting due to a memory leak. Repeat, please.\n";
638 if (defined $orig_git_index) {
639 $ENV{GIT_INDEX_FILE
} = $orig_git_index;
641 delete $ENV{GIT_INDEX_FILE
};
644 # Now switch back to the branch we were in before all of this happened
646 print "DONE\n" if $opt_v and (not defined $opt_l or $opt_l > 0);
647 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
650 system('git-read-tree', '-m', '-u', 'SVN2GIT_HEAD', 'HEAD');
651 die "read-tree failed: $?\n" if $?
;
654 $orig_branch = "master";
655 print "DONE; creating $orig_branch branch\n" if $opt_v and (not defined $opt_l or $opt_l > 0);
656 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
657 unless -f
"$git_dir/refs/heads/master";
658 unlink("$git_dir/HEAD");
659 symlink("refs/heads/$orig_branch","$git_dir/HEAD");
661 system('git checkout');
662 die "checkout failed: $?\n" if $?
;
665 unlink("$git_dir/SVN2GIT_HEAD");