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 print "... $rev $path ...\n" if $opt_v;
125 eval { $self->{'svn'}->get_file($path,$rev,$fh); };
126 if (defined $@
and $@
!~ /Attempted to get checksum/) {
129 eval { $self->{'svn'}->get_file($path,$rev,$fh); };
131 return () if defined $@
and $@
!~ /Attempted to get checksum/;
135 return ($name, $res);
141 my $svn = SVNconn
->new($cvs_tree);
146 $d =~ m
#(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)#
147 or die "Unparseable date: $d\n";
148 my $y=$1; $y-=1900 if $y>1900;
149 return timegm
($6||0,$5,$4,$3,$2-1,$y);
157 for my $x(split(//,$mode)) {
162 } elsif($x eq "u") { $um |= 0700;
163 } elsif($x eq "g") { $um |= 0070;
164 } elsif($x eq "o") { $um |= 0007;
165 } elsif($x eq "r") { $mm |= 0444;
166 } elsif($x eq "w") { $mm |= 0222;
167 } elsif($x eq "x") { $mm |= 0111;
168 } elsif($x eq "=") { # do nothing
169 } else { die "Unknown mode: $mode\n";
183 sub get_headref
($$) {
188 if (open(C
,"$git_dir/refs/heads/$name")) {
192 or die "Cannot get head id for $name ($sha): $!\n";
199 or mkdir($git_tree,0777)
200 or die "Could not create $git_tree: $!";
203 my $orig_branch = "";
204 my $forward_master = 0;
207 my $git_dir = $ENV{"GIT_DIR"} || ".git";
208 $git_dir = getwd
()."/".$git_dir unless $git_dir =~ m
#^/#;
209 $ENV{"GIT_DIR"} = $git_dir;
211 $orig_git_index = $ENV{GIT_INDEX_FILE
} if exists $ENV{GIT_INDEX_FILE
};
212 my ($git_ih, $git_index) = tempfile
('gitXXXXXX', SUFFIX
=> '.idx',
213 DIR
=> File
::Spec
->tmpdir());
215 $ENV{GIT_INDEX_FILE
} = $git_index;
220 unless(-d
$git_dir) {
221 system("git-init-db");
222 die "Cannot init the GIT db at $git_tree: $?\n" if $?
;
223 system("git-read-tree");
224 die "Cannot init an empty tree: $?\n" if $?
;
226 $last_branch = $opt_o;
229 -f
"$git_dir/refs/heads/$opt_o"
230 or die "Branch '$opt_o' does not exist.\n".
231 "Either use the correct '-o branch' option,\n".
232 "or import to a new repository.\n";
234 -f
"$git_dir/svn2git"
235 or die "'$git_dir/svn2git' does not exist.\n".
236 "You need that file for incremental imports.\n";
237 $last_branch = basename
(readlink("$git_dir/HEAD"));
238 unless($last_branch) {
239 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
240 $last_branch = "master";
242 $orig_branch = $last_branch;
243 $last_rev = get_headref
($orig_branch, $git_dir);
244 if (-f
"$git_dir/SVN2GIT_HEAD") {
247 Make sure your working directory corresponds to HEAD and remove SVN2GIT_HEAD.
250 git-read-tree -m -u SVN2GIT_HEAD HEAD
253 system('cp', "$git_dir/HEAD", "$git_dir/SVN2GIT_HEAD");
256 $opt_o ne 'master' && -f
"$git_dir/refs/heads/master" &&
257 system('cmp', '-s', "$git_dir/refs/heads/master",
258 "$git_dir/refs/heads/$opt_o") == 0;
261 system('git-read-tree', $last_rev);
262 die "read-tree failed: $?\n" if $?
;
264 # Get the last import timestamps
265 open my $B,"<", "$git_dir/svn2git";
268 my($num,$branch,$ref) = split;
269 $branches{$branch}{$num} = $ref;
270 $branches{$branch}{"LAST"} = $ref;
271 $current_rev = $num+1 if $current_rev < $num+1;
276 or die "Could not create git subdir ($git_dir).\n";
278 open BRANCHES
,">>", "$git_dir/svn2git";
282 #---------------------
284 #Date: 1999/09/18 13:03:59
286 #Branch: STABLE-BRANCH-1-0
287 #Ancestor branch: HEAD
290 # See ChangeLog: Sat Sep 18 13:03:28 CEST 1999 Werner Koch
292 # README:1.57->1.57.2.1
293 # VERSION:1.96->1.96.2.1
295 #---------------------
300 my($rev,$branch,$path) = @_;
302 # revert split_path(), below
304 $path = "" if $path eq "/"; # this should not happen, but ...
306 $svnpath = "/$trunk_name/$path";
307 } elsif($branch =~ m
#^/#) {
308 $svnpath = "/$tag_name$branch/$path";
310 $svnpath = "/$branch_name/$branch/$path";
314 my ($name, $res) = eval { $svn->file($svnpath,$rev); };
315 return () unless defined $name;
317 open my $F, '-|', "git-hash-object -w $name"
318 or die "Cannot create object: $!\n";
322 # my $mode = pmode($cvs->{'mode'});
323 my $mode = "0644"; # SV does not seem to store any file modes
324 return [$mode, $sha, $path];
331 if($path =~ s
#^/\Q$tag_name\E/([^/]+)/?##) {
333 } elsif($path =~ s
#^/\Q$trunk_name\E/?##) {
335 } elsif($path =~ s
#^/\Q$branch_name\E/([^/]+)/?##) {
338 print STDERR
"$rev: Unrecognized path: $path\n";
341 $path = "/" if $path eq "";
342 return ($branch,$path);
346 my($branch, $changed_paths, $revision, $author, $date, $message) = @_;
347 my($author_name,$author_email,$dest);
350 if (not defined $author) {
351 $author_name = $author_email = "unknown";
352 } elsif ($author =~ /^(.*?)\s+<(.*)>$/) {
353 ($author_name, $author_email) = ($1, $2);
355 $author =~ s/^<(.*)>$/$1/;
356 $author_name = $author_email = $author;
358 $date = pdate
($date);
362 if($branch eq "/") { # trunk
364 } elsif($branch =~ m
#^/(.+)#) { # tag
367 } else { # "normal" branch
373 my $prev = $changed_paths->{"/"};
374 if($prev and $prev->action eq "A") {
375 delete $changed_paths->{"/"};
376 my $oldpath = $prev->copyfrom_path;
378 if(defined $oldpath) {
380 ($parent,$p) = split_path
($revision,$oldpath);
384 $parent =~ s
#^/##; # if it's a tag
392 if(defined $parent) {
393 open(H
,"git-rev-parse --verify $parent |");
396 print STDERR
"$revision: cannot find commit '$parent'!\n";
400 if(length($rev) != 40) {
401 print STDERR
"$revision: cannot find commit '$parent'!\n";
404 $rev = $branches{($parent eq $opt_o) ?
"/" : $parent}{"LAST"};
405 if($revision != 1 and not $rev) {
406 print STDERR
"$revision: do not know ancestor for '$parent'!\n";
413 # if($prev and $prev->action eq "A") {
415 # unless(open(H,"> $git_dir/refs/heads/$branch")) {
416 # print STDERR "$revision: Could not create branch $branch: $!\n";
421 # or die "Could not write branch $branch: $!";
423 # or die "Could not write branch $branch: $!";
426 if(not defined $rev) {
428 } elsif ($rev ne $last_rev) {
429 print "Switching from $last_rev to $rev ($branch)\n" if $opt_v;
430 system("git-read-tree", $rev);
431 die "read-tree failed for $rev: $?\n" if $?
;
435 while(my($path,$action) = each %$changed_paths) {
436 if ($action->action eq "A") {
437 my $f = get_file
($revision,$branch,$path);
439 } elsif ($action->action eq "D") {
441 } elsif ($action->action eq "M") {
442 my $f = get_file
($revision,$branch,$path);
444 } elsif ($action->action eq "R") {
445 # refer to a file/tree in an earlier commit
446 push(@old,$path); # remove any old stuff
448 # ... and add any new stuff
449 my($b,$p) = split_path
($revision,$action->oldpath);
450 open my $F,"-|","git-ls-tree","-r","-z", $branches{$b}{$action->oldrev}, $p;
454 my($m,$p) = split(/\t/,$_,2);
455 my($mode,$type,$sha1) = split(/ /,$m);
456 next if $type ne "blob";
457 push(@new,[$mode,$sha1,$p]);
460 die "$revision: unknown action '".$action->action."' for $path\n";
465 open F
, "-│", "git-ls-files", "-z", @old or die $!;
477 @o2 = splice(@old,0,50);
482 system("git-update-index","--force-remove","--",@o2);
483 die "Cannot remove files: $?\n" if $?
;
489 @n2 = splice(@new,0,10);
494 system("git-update-index","--add",
495 (map { ('--cacheinfo', @
$_) } @n2));
496 die "Cannot add files: $?\n" if $?
;
499 my $pid = open(C
,"-|");
500 die "Cannot fork: $!" unless defined $pid;
502 exec("git-write-tree");
503 die "Cannot exec git-write-tree: $!\n";
505 chomp(my $tree = <C
>);
507 or die "Cannot get tree id ($tree): $!\n";
509 or die "Error running git-write-tree: $?\n";
510 print "Tree ID $tree\n" if $opt_v;
512 my $pr = IO
::Pipe
->new() or die "Cannot open pipe: $!\n";
513 my $pw = IO
::Pipe
->new() or die "Cannot open pipe: $!\n";
515 die "Fork: $!\n" unless defined $pid;
519 open(OUT
,">&STDOUT");
520 dup2
($pw->fileno(),0);
521 dup2
($pr->fileno(),1);
526 @par = ("-p",$rev) if defined $rev;
528 # loose detection of merges
529 # based on the commit msg
530 foreach my $rx (@mergerx) {
531 if ($message =~ $rx) {
533 if ($mparent eq 'HEAD') { $mparent = $opt_o };
534 if ( -e
"$git_dir/refs/heads/$mparent") {
535 $mparent = get_headref
($mparent, $git_dir);
536 push @par, '-p', $mparent;
537 print OUT
"Merge parent branch: $mparent\n" if $opt_v;
543 "GIT_AUTHOR_NAME=$author_name",
544 "GIT_AUTHOR_EMAIL=$author_email",
545 "GIT_AUTHOR_DATE=".strftime
("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
546 "GIT_COMMITTER_NAME=$author_name",
547 "GIT_COMMITTER_EMAIL=$author_email",
548 "GIT_COMMITTER_DATE=".strftime
("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
549 "git-commit-tree", $tree,@par);
550 die "Cannot exec git-commit-tree: $!\n";
555 $message =~ s/[\s\n]+\z//;
557 print $pw "$message\n"
558 or die "Error writing to git-commit-tree: $!\n";
561 print "Committed change $revision:$branch ".strftime
("%Y-%m-%d %H:%M:%S",gmtime($date)).")\n" if $opt_v;
562 chomp(my $cid = <$pr>);
564 or die "Cannot get commit id ($cid): $!\n";
565 print "Commit ID $cid\n" if $opt_v;
569 die "Error running git-commit-tree: $?\n" if $?
;
572 print "Writing to refs/heads/$dest\n" if $opt_v;
573 open(C
,">$git_dir/refs/heads/$dest") and
574 print C
("$cid\n") and
576 or die "Cannot write branch $dest for update: $!\n";
578 print "... no known parent\n" if $opt_v;
580 $branches{$branch}{"LAST"} = $cid;
581 $branches{$branch}{$revision} = $cid;
583 print BRANCHES
"$revision $branch $cid\n";
584 print "DONE: $revision $dest $cid\n" if $opt_v;
587 my($in, $out) = ('','');
588 $last_rev = "-" if %$changed_paths;
589 # the tag was 'complex', i.e. did not refer to a "real" revision
591 $tag =~ tr/_/\./ if $opt_u;
593 my $pid = open2
($in, $out, 'git-mktag');
594 print $out ("object $cid\n".
597 "tagger $author_name <$author_email>\n") and
599 or die "Cannot create tag object $tag: $!\n";
604 if ( !close($in) or waitpid($pid, 0) != $pid or
605 $?
!= 0 or $tagobj !~ /^[0123456789abcdef]{40}$/ ) {
606 die "Cannot create tag object $tag: $!\n";
610 open(C
,">$git_dir/refs/tags/$tag")
611 or die "Cannot create tag $tag: $!\n";
613 or die "Cannot write tag $tag: $!\n";
615 or die "Cannot write tag $tag: $!\n";
617 print "Created tag '$tag' on '$branch'\n" if $opt_v;
621 my ($changed_paths, $revision, $author, $date, $message, $pool) = @_;
623 ($changed_paths, $revision, $author, $date, $message, $pool) = @_;
631 while(my($path,$action) = each %$changed_paths) {
632 ($branch,$path) = split_path
($revision,$path);
633 next if not defined $branch;
634 $done{$branch}{$path} = $action;
636 while(($branch,$changed_paths) = each %done) {
637 commit
($branch, $changed_paths, $revision, $author, $date, $message);
641 while(++$current_rev < $svn->{'maxrev'}) {
642 $svn->{'svn'}->get_log("/",$current_rev,$current_rev,$current_rev,1,1,\
&_commit_all
,"");
649 if (defined $orig_git_index) {
650 $ENV{GIT_INDEX_FILE
} = $orig_git_index;
652 delete $ENV{GIT_INDEX_FILE
};
655 # Now switch back to the branch we were in before all of this happened
657 print "DONE\n" if $opt_v;
658 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
661 system('git-read-tree', '-m', '-u', 'SVN2GIT_HEAD', 'HEAD');
662 die "read-tree failed: $?\n" if $?
;
665 $orig_branch = "master";
666 print "DONE; creating $orig_branch branch\n" if $opt_v;
667 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
668 unless -f
"$git_dir/refs/heads/master";
669 unlink("$git_dir/HEAD");
670 symlink("refs/heads/$orig_branch","$git_dir/HEAD");
672 system('git checkout');
673 die "checkout failed: $?\n" if $?
;
676 unlink("$git_dir/SVN2GIT_HEAD");