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.
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 SVN: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,
34 $opt_b,$opt_r,$opt_I,$opt_A,$opt_s,$opt_l,$opt_d,$opt_D);
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] [SVN_URL]
47 getopts
("A:b:C:dDhiI:l:mM:o:rs:t:T:uv") or usage
();
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
();
58 my $git_tree = $opt_C;
61 my $svn_url = $ARGV[0];
62 my $svn_dir = $ARGV[1];
66 @mergerx = ( qr/\W(?:from|of|merge|merging|merged) (\w+)/i );
69 push (@mergerx, qr/$opt_M/);
72 # Absolutize filename now, since we will have chdir'ed by the time we
73 # get around to opening it.
74 $opt_A = File
::Spec
->rel2abs($opt_A) if $opt_A;
77 our $users_file = undef;
79 $users_file = File
::Spec
->rel2abs(@_);
80 die "Cannot open $users_file\n" unless -f
$users_file;
81 open(my $authors,$users_file);
84 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
85 (my $user,my $name,my $email) = ($1,$2,$3);
86 $users{$user} = [$name,$email];
91 select(STDERR
); $|=1; select(STDOUT
);
95 # Basic SVN connection.
96 # We're only interested in connecting and downloading, so ...
99 use File
::Temp
qw(tempfile);
100 use POSIX
qw(strftime dup2);
103 my($what,$repo) = @_;
104 $what=ref($what) if ref($what);
107 $self->{'buffer'} = "";
111 $self->{'fullrep'} = $repo;
119 my $repo = $self->{'fullrep'};
120 my $auth = SVN
::Core
::auth_open
([SVN
::Client
::get_simple_provider
,
121 SVN
::Client
::get_ssl_server_trust_file_provider
,
122 SVN
::Client
::get_username_provider
]);
123 my $s = SVN
::Ra
->new(url
=> $repo, auth
=> $auth);
124 die "SVN connection to $repo: $!\n" unless defined $s;
126 $self->{'repo'} = $repo;
127 $self->{'maxrev'} = $s->get_latest_revnum();
131 my($self,$path,$rev) = @_;
133 my ($fh, $name) = tempfile
('gitsvn.XXXXXX',
134 DIR
=> File
::Spec
->tmpdir(), UNLINK
=> 1);
136 print "... $rev $path ...\n" if $opt_v;
137 my (undef, $properties);
138 eval { (undef, $properties)
139 = $self->{'svn'}->get_file($path,$rev,$fh); };
141 return undef if $@
=~ /Attempted to get checksum/;
145 if (exists $properties->{'svn:executable'}) {
152 return ($name, $mode);
156 my($self,$path,$rev) = @_;
158 print "... $rev $path ...\n" if $opt_v;
159 my (undef,undef,$properties)
160 = $self->{'svn'}->get_dir($path,$rev,undef);
161 if (exists $properties->{'svn:ignore'}) {
162 my ($fh, $name) = tempfile
('gitsvn.XXXXXX',
163 DIR
=> File
::Spec
->tmpdir(),
165 print $fh $properties->{'svn:ignore'};
177 $svn .= "/$svn_dir" if defined $svn_dir;
178 my $svn2 = SVNconn
->new($svn);
179 $svn = SVNconn
->new($svn);
182 if($opt_d or $opt_D) {
183 $svn_url = URI
->new($svn_url)->canonical;
185 $svn_dir =~ s
#/*$#/#;
189 if ($svn_url->scheme eq "http") {
191 $lwp_ua = LWP
::UserAgent
->new(keep_alive
=> 1, requests_redirectable
=> []);
193 print STDERR
"Warning: not HTTP; turning off direct file access\n";
200 $d =~ m
#(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)#
201 or die "Unparseable date: $d\n";
202 my $y=$1; $y-=1900 if $y>1900;
203 return timegm
($6||0,$5,$4,$3,$2-1,$y);
213 sub get_headref
($$) {
218 if (open(C
,"$git_dir/refs/heads/$name")) {
222 or die "Cannot get head id for $name ($sha): $!\n";
229 or mkdir($git_tree,0777)
230 or die "Could not create $git_tree: $!";
233 my $orig_branch = "";
234 my $forward_master = 0;
237 my $git_dir = $ENV{"GIT_DIR"} || ".git";
238 $git_dir = getwd
()."/".$git_dir unless $git_dir =~ m
#^/#;
239 $ENV{"GIT_DIR"} = $git_dir;
241 $orig_git_index = $ENV{GIT_INDEX_FILE
} if exists $ENV{GIT_INDEX_FILE
};
242 my ($git_ih, $git_index) = tempfile
('gitXXXXXX', SUFFIX
=> '.idx',
243 DIR
=> File
::Spec
->tmpdir());
245 $ENV{GIT_INDEX_FILE
} = $git_index;
249 my $current_rev = $opt_s || 1;
250 unless(-d
$git_dir) {
251 system("git-init-db");
252 die "Cannot init the GIT db at $git_tree: $?\n" if $?
;
253 system("git-read-tree");
254 die "Cannot init an empty tree: $?\n" if $?
;
256 $last_branch = $opt_o;
259 -f
"$git_dir/refs/heads/$opt_o"
260 or die "Branch '$opt_o' does not exist.\n".
261 "Either use the correct '-o branch' option,\n".
262 "or import to a new repository.\n";
264 -f
"$git_dir/svn2git"
265 or die "'$git_dir/svn2git' does not exist.\n".
266 "You need that file for incremental imports.\n";
267 open(F
, "git-symbolic-ref HEAD |") or
268 die "Cannot run git-symbolic-ref: $!\n";
269 chomp ($last_branch = <F
>);
270 $last_branch = basename
($last_branch);
272 unless($last_branch) {
273 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
274 $last_branch = "master";
276 $orig_branch = $last_branch;
277 $last_rev = get_headref
($orig_branch, $git_dir);
278 if (-f
"$git_dir/SVN2GIT_HEAD") {
281 Make sure your working directory corresponds to HEAD and remove SVN2GIT_HEAD.
284 git-read-tree -m -u SVN2GIT_HEAD HEAD
287 system('cp', "$git_dir/HEAD", "$git_dir/SVN2GIT_HEAD");
290 $opt_o ne 'master' && -f
"$git_dir/refs/heads/master" &&
291 system('cmp', '-s', "$git_dir/refs/heads/master",
292 "$git_dir/refs/heads/$opt_o") == 0;
295 system('git-read-tree', $last_rev);
296 die "read-tree failed: $?\n" if $?
;
298 # Get the last import timestamps
299 open my $B,"<", "$git_dir/svn2git";
302 my($num,$branch,$ref) = split;
303 $branches{$branch}{$num} = $ref;
304 $branches{$branch}{"LAST"} = $ref;
305 $current_rev = $num+1 if $current_rev <= $num;
310 or die "Could not create git subdir ($git_dir).\n";
312 my $default_authors = "$git_dir/svn-authors";
315 copy
($opt_A,$default_authors) or die "Copy failed: $!";
317 read_users
($default_authors) if -f
$default_authors;
320 open BRANCHES
,">>", "$git_dir/svn2git";
323 my ($branch, $path, $revision) = @_;
324 my $pool=SVN
::Pool
->new;
325 my $kind = $svn->{'svn'}->check_path(revert_split_path
($branch,$path),$revision,$pool);
330 sub revert_split_path
($$) {
331 my($branch,$path) = @_;
334 $path = "" if $path eq "/"; # this should not happen, but ...
336 $svnpath = "$trunk_name/$path";
337 } elsif($branch =~ m
#^/#) {
338 $svnpath = "$tag_name$branch/$path";
340 $svnpath = "$branch_name/$branch/$path";
348 my($rev,$branch,$path) = @_;
350 my $svnpath = revert_split_path
($branch,$path);
357 # /svn/!svn/bc/2/django/trunk/django-docs/build.py
358 my $url=$svn_url->clone();
359 $url->path($url->path."/!svn/bc/$rev/$svn_dir$svnpath");
360 print "... $path...\n" if $opt_v;
361 $req = HTTP
::Request
->new(GET
=> $url);
362 $res = $lwp_ua->request($req);
363 if ($res->is_success) {
365 ($fh, $name) = tempfile
('gitsvn.XXXXXX',
366 DIR
=> File
::Spec
->tmpdir(), UNLINK
=> 1);
367 print $fh $res->content;
368 close($fh) or die "Could not write $name: $!\n";
370 return undef if $res->code == 301; # directory?
371 die $res->status_line." at $url\n";
373 $mode = '0644'; # can't obtain mode via direct http request?
375 ($name,$mode) = $svn->file("$svnpath",$rev);
376 return undef unless defined $name;
379 my $pid = open(my $F, '-|');
380 die $! unless defined $pid;
382 exec("git-hash-object", "-w", $name)
383 or die "Cannot create object: $!\n";
389 return [$mode, $sha, $path];
392 sub get_ignore
($$$$$) {
393 my($new,$old,$rev,$branch,$path) = @_;
395 return unless $opt_I;
396 my $svnpath = revert_split_path
($branch,$path);
397 my $name = $svn->ignore("$svnpath",$rev);
401 $path = File
::Spec
->catfile($path,$opt_I);
404 my $pid = open(my $F, '-|');
405 die $! unless defined $pid;
407 exec("git-hash-object", "-w", $name)
408 or die "Cannot create object: $!\n";
414 push(@
$new,['0644',$sha,$path]);
424 if($path =~ s
#^/\Q$tag_name\E/([^/]+)/?##) {
426 } elsif($path =~ s
#^/\Q$trunk_name\E/?##) {
428 } elsif($path =~ s
#^/\Q$branch_name\E/([^/]+)/?##) {
436 print STDERR
"$rev: Unrecognized path: $path\n" unless (defined $no_error{$path});
439 $path = "/" if $path eq "";
440 return ($branch,$path);
445 my ($srcbranch,$uptorev) = @_;
447 my $bbranches = $branches{$srcbranch};
448 my @revs = reverse sort { ($a eq 'LAST' ?
0 : $a) <=> ($b eq 'LAST' ?
0 : $b) } keys %$bbranches;
450 foreach my $arev(@revs) {
451 next if ($arev eq 'LAST');
452 if ($arev <= $uptorev) {
460 sub copy_path
($$$$$$$$) {
461 # Somebody copied a whole subdirectory.
462 # We need to find the index entries from the old version which the
463 # SVN log entry points to, and add them to the new place.
465 my($newrev,$newbranch,$path,$oldpath,$rev,$node_kind,$new,$parents) = @_;
467 my($srcbranch,$srcpath) = split_path
($rev,$oldpath);
468 unless(defined $srcbranch) {
469 print "Path not found when copying from $oldpath @ $rev\n";
472 my $therev = branch_rev
($srcbranch, $rev);
473 my $gitrev = $branches{$srcbranch}{$therev};
475 print STDERR
"$newrev:$newbranch: could not find $oldpath \@ $rev\n";
478 if ($srcbranch ne $newbranch) {
479 push(@
$parents, $branches{$srcbranch}{'LAST'});
481 print "$newrev:$newbranch:$path: copying from $srcbranch:$srcpath @ $rev\n" if $opt_v;
482 if ($node_kind eq $SVN::Node
::dir
) {
483 $srcpath =~ s
#/*$#/#;
486 my $pid = open my $f,'-|';
487 die $! unless defined $pid;
489 exec("git-ls-tree","-r","-z",$gitrev,$srcpath)
495 my($m,$p) = split(/\t/,$_,2);
496 my($mode,$type,$sha1) = split(/ /,$m);
497 next if $type ne "blob";
498 if ($node_kind eq $SVN::Node
::dir
) {
499 $p = $path . substr($p,length($srcpath)-1);
503 push(@
$new,[$mode,$sha1,$p]);
506 print STDERR
"$newrev:$newbranch: could not list files in $oldpath \@ $rev\n";
510 my($branch, $changed_paths, $revision, $author, $date, $message) = @_;
511 my($author_name,$author_email,$dest);
512 my(@old,@new,@parents);
514 if (not defined $author) {
515 $author_name = $author_email = "unknown";
516 } elsif (defined $users_file) {
517 die "User $author is not listed in $users_file\n"
518 unless exists $users{$author};
519 ($author_name,$author_email) = @
{$users{$author}};
520 } elsif ($author =~ /^(.*?)\s+<(.*)>$/) {
521 ($author_name, $author_email) = ($1, $2);
523 $author =~ s/^<(.*)>$/$1/;
524 $author_name = $author_email = $author;
526 $date = pdate
($date);
530 if($branch eq "/") { # trunk
532 } elsif($branch =~ m
#^/(.+)#) { # tag
535 } else { # "normal" branch
541 my $prev = $changed_paths->{"/"};
542 if($prev and $prev->[0] eq "A") {
543 delete $changed_paths->{"/"};
544 my $oldpath = $prev->[1];
546 if(defined $oldpath) {
548 ($parent,$p) = split_path
($revision,$oldpath);
552 $parent =~ s
#^/##; # if it's a tag
560 if($revision > $opt_s and defined $parent) {
561 open(H
,"git-rev-parse --verify $parent |");
564 print STDERR
"$revision: cannot find commit '$parent'!\n";
568 if(length($rev) != 40) {
569 print STDERR
"$revision: cannot find commit '$parent'!\n";
572 $rev = $branches{($parent eq $opt_o) ?
"/" : $parent}{"LAST"};
573 if($revision != $opt_s and not $rev) {
574 print STDERR
"$revision: do not know ancestor for '$parent'!\n";
581 # if($prev and $prev->[0] eq "A") {
583 # unless(open(H,"> $git_dir/refs/heads/$branch")) {
584 # print STDERR "$revision: Could not create branch $branch: $!\n";
589 # or die "Could not write branch $branch: $!";
591 # or die "Could not write branch $branch: $!";
594 if(not defined $rev) {
596 } elsif ($rev ne $last_rev) {
597 print "Switching from $last_rev to $rev ($branch)\n" if $opt_v;
598 system("git-read-tree", $rev);
599 die "read-tree failed for $rev: $?\n" if $?
;
603 push (@parents, $rev) if defined $rev;
606 if($tag and not %$changed_paths) {
609 my @paths = sort keys %$changed_paths;
610 foreach my $path(@paths) {
611 my $action = $changed_paths->{$path};
613 if ($action->[0] eq "R") {
614 # refer to a file/tree in an earlier commit
615 push(@old,$path); # remove any old stuff
617 if(($action->[0] eq "A") || ($action->[0] eq "R")) {
618 my $node_kind = node_kind
($branch,$path,$revision);
620 copy_path
($revision,$branch,$path,$action->[1],$action->[2],$node_kind,\
@new,\
@parents);
621 } elsif ($node_kind eq $SVN::Node
::file
) {
622 my $f = get_file
($revision,$branch,$path);
626 my $opath = $action->[3];
627 print STDERR
"$revision: $branch: could not fetch '$opath'\n";
629 } elsif ($node_kind eq $SVN::Node
::dir
) {
630 get_ignore
(\
@new, \
@old, $revision,
633 } elsif ($action->[0] eq "D") {
635 } elsif ($action->[0] eq "M") {
636 my $node_kind = node_kind
($branch,$path,$revision);
637 if ($node_kind eq $SVN::Node
::file
) {
638 my $f = get_file
($revision,$branch,$path);
640 } elsif ($node_kind eq $SVN::Node
::dir
) {
641 get_ignore
(\
@new, \
@old, $revision,
645 die "$revision: unknown action '".$action->[0]."' for $path\n";
652 @o1 = splice(@old,0,50);
657 my $pid = open my $F, "-|";
658 die "$!" unless defined $pid;
660 exec("git-ls-files", "-z", @o1) or die $!;
673 @o2 = splice(@o1,0,50);
678 system("git-update-index","--force-remove","--",@o2);
679 die "Cannot remove files: $?\n" if $?
;
685 @n2 = splice(@new,0,10);
690 system("git-update-index","--add",
691 (map { ('--cacheinfo', @
$_) } @n2));
692 die "Cannot add files: $?\n" if $?
;
695 my $pid = open(C
,"-|");
696 die "Cannot fork: $!" unless defined $pid;
698 exec("git-write-tree");
699 die "Cannot exec git-write-tree: $!\n";
701 chomp(my $tree = <C
>);
703 or die "Cannot get tree id ($tree): $!\n";
705 or die "Error running git-write-tree: $?\n";
706 print "Tree ID $tree\n" if $opt_v;
708 my $pr = IO
::Pipe
->new() or die "Cannot open pipe: $!\n";
709 my $pw = IO
::Pipe
->new() or die "Cannot open pipe: $!\n";
711 die "Fork: $!\n" unless defined $pid;
715 open(OUT
,">&STDOUT");
716 dup2
($pw->fileno(),0);
717 dup2
($pr->fileno(),1);
723 # loose detection of merges
724 # based on the commit msg
725 foreach my $rx (@mergerx) {
726 if ($message =~ $rx) {
728 if ($mparent eq 'HEAD') { $mparent = $opt_o };
729 if ( -e
"$git_dir/refs/heads/$mparent") {
730 $mparent = get_headref
($mparent, $git_dir);
731 push (@parents, $mparent);
732 print OUT
"Merge parent branch: $mparent\n" if $opt_v;
736 my %seen_parents = ();
737 my @unique_parents = grep { ! $seen_parents{$_} ++ } @parents;
738 foreach my $bparent (@unique_parents) {
739 push @par, '-p', $bparent;
740 print OUT
"Merge parent branch: $bparent\n" if $opt_v;
744 "GIT_AUTHOR_NAME=$author_name",
745 "GIT_AUTHOR_EMAIL=$author_email",
746 "GIT_AUTHOR_DATE=".strftime
("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
747 "GIT_COMMITTER_NAME=$author_name",
748 "GIT_COMMITTER_EMAIL=$author_email",
749 "GIT_COMMITTER_DATE=".strftime
("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
750 "git-commit-tree", $tree,@par);
751 die "Cannot exec git-commit-tree: $!\n";
756 $message =~ s/[\s\n]+\z//;
757 $message = "r$revision: $message" if $opt_r;
759 print $pw "$message\n"
760 or die "Error writing to git-commit-tree: $!\n";
763 print "Committed change $revision:$branch ".strftime
("%Y-%m-%d %H:%M:%S",gmtime($date)).")\n" if $opt_v;
766 or die "Cannot get commit id ($cid): $!\n";
767 print "Commit ID $cid\n" if $opt_v;
771 die "Error running git-commit-tree: $?\n" if $?
;
774 if (not defined $cid) {
775 $cid = $branches{"/"}{"LAST"};
778 if(not defined $dest) {
779 print "... no known parent\n" if $opt_v;
781 print "Writing to refs/heads/$dest\n" if $opt_v;
782 open(C
,">$git_dir/refs/heads/$dest") and
783 print C
("$cid\n") and
785 or die "Cannot write branch $dest for update: $!\n";
789 my($in, $out) = ('','');
790 $last_rev = "-" if %$changed_paths;
791 # the tag was 'complex', i.e. did not refer to a "real" revision
793 $dest =~ tr/_/\./ if $opt_u;
796 my $pid = open2
($in, $out, 'git-mktag');
797 print $out ("object $cid\n".
800 "tagger $author_name <$author_email>\n") and
802 or die "Cannot create tag object $dest: $!\n";
807 if ( !close($in) or waitpid($pid, 0) != $pid or
808 $?
!= 0 or $tagobj !~ /^[0123456789abcdef]{40}$/ ) {
809 die "Cannot create tag object $dest: $!\n";
812 open(C
,">$git_dir/refs/tags/$dest") and
813 print C
("$tagobj\n") and
815 or die "Cannot create tag $branch: $!\n";
817 print "Created tag '$dest' on '$branch'\n" if $opt_v;
819 $branches{$branch}{"LAST"} = $cid;
820 $branches{$branch}{$revision} = $cid;
822 print BRANCHES
"$revision $branch $cid\n";
823 print "DONE: $revision $dest $cid\n" if $opt_v;
827 # Recursive use of the SVN connection does not work
830 my ($changed_paths, $revision, $author, $date, $message, $pool) = @_;
832 while(my($path,$action) = each %$changed_paths) {
833 $p{$path} = [ $action->action,$action->copyfrom_path, $action->copyfrom_rev, $path ];
835 $changed_paths = \
%p;
842 while(my($path,$action) = each %$changed_paths) {
843 ($branch,$path) = split_path
($revision,$path);
844 next if not defined $branch;
845 $done{$branch}{$path} = $action;
847 while(($branch,$changed_paths) = each %done) {
848 commit
($branch, $changed_paths, $revision, $author, $date, $message);
852 $opt_l = $svn->{'maxrev'} if not defined $opt_l or $opt_l > $svn->{'maxrev'};
854 if ($opt_l < $current_rev) {
855 print "Up to date: no new revisions to fetch!\n" if $opt_v;
856 unlink("$git_dir/SVN2GIT_HEAD");
860 print "Fetching from $current_rev to $opt_l ...\n" if $opt_v;
862 my $pool=SVN
::Pool
->new;
863 $svn->{'svn'}->get_log("/",$current_rev,$opt_l,0,1,1,\
&commit_all
,$pool);
869 if (defined $orig_git_index) {
870 $ENV{GIT_INDEX_FILE
} = $orig_git_index;
872 delete $ENV{GIT_INDEX_FILE
};
875 # Now switch back to the branch we were in before all of this happened
877 print "DONE\n" if $opt_v and (not defined $opt_l or $opt_l > 0);
878 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
881 system('git-read-tree', '-m', '-u', 'SVN2GIT_HEAD', 'HEAD');
882 die "read-tree failed: $?\n" if $?
;
885 $orig_branch = "master";
886 print "DONE; creating $orig_branch branch\n" if $opt_v and (not defined $opt_l or $opt_l > 0);
887 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
888 unless -f
"$git_dir/refs/heads/master";
889 system('git-update-ref', 'HEAD', "$orig_branch");
891 system('git checkout');
892 die "checkout failed: $?\n" if $?
;
895 unlink("$git_dir/SVN2GIT_HEAD");