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);
101 use Fcntl
qw(SEEK_SET);
104 my($what,$repo) = @_;
105 $what=ref($what) if ref($what);
108 $self->{'buffer'} = "";
112 $self->{'fullrep'} = $repo;
120 my $repo = $self->{'fullrep'};
121 my $auth = SVN
::Core
::auth_open
([SVN
::Client
::get_simple_provider
,
122 SVN
::Client
::get_ssl_server_trust_file_provider
,
123 SVN
::Client
::get_username_provider
]);
124 my $s = SVN
::Ra
->new(url
=> $repo, auth
=> $auth);
125 die "SVN connection to $repo: $!\n" unless defined $s;
127 $self->{'repo'} = $repo;
128 $self->{'maxrev'} = $s->get_latest_revnum();
132 my($self,$path,$rev) = @_;
134 my ($fh, $name) = tempfile
('gitsvn.XXXXXX',
135 DIR
=> File
::Spec
->tmpdir(), UNLINK
=> 1);
137 print "... $rev $path ...\n" if $opt_v;
138 my (undef, $properties);
139 my $pool = SVN
::Pool
->new();
140 eval { (undef, $properties)
141 = $self->{'svn'}->get_file($path,$rev,$fh,$pool); };
144 return undef if $@
=~ /Attempted to get checksum/;
148 if (exists $properties->{'svn:executable'}) {
150 } elsif (exists $properties->{'svn:special'}) {
151 my ($special_content, $filesize);
152 $filesize = tell $fh;
153 seek $fh, 0, SEEK_SET
;
154 read $fh, $special_content, $filesize;
155 if ($special_content =~ s/^link //) {
157 seek $fh, 0, SEEK_SET
;
159 print $fh $special_content;
161 die "unexpected svn:special file encountered";
168 return ($name, $mode);
172 my($self,$path,$rev) = @_;
174 print "... $rev $path ...\n" if $opt_v;
175 my (undef,undef,$properties)
176 = $self->{'svn'}->get_dir($path,$rev,undef);
177 if (exists $properties->{'svn:ignore'}) {
178 my ($fh, $name) = tempfile
('gitsvn.XXXXXX',
179 DIR
=> File
::Spec
->tmpdir(),
181 print $fh $properties->{'svn:ignore'};
193 $svn .= "/$svn_dir" if defined $svn_dir;
194 my $svn2 = SVNconn
->new($svn);
195 $svn = SVNconn
->new($svn);
198 if($opt_d or $opt_D) {
199 $svn_url = URI
->new($svn_url)->canonical;
201 $svn_dir =~ s
#/*$#/#;
205 if ($svn_url->scheme eq "http") {
207 $lwp_ua = LWP
::UserAgent
->new(keep_alive
=> 1, requests_redirectable
=> []);
209 print STDERR
"Warning: not HTTP; turning off direct file access\n";
216 $d =~ m
#(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)#
217 or die "Unparseable date: $d\n";
218 my $y=$1; $y-=1900 if $y>1900;
219 return timegm
($6||0,$5,$4,$3,$2-1,$y);
229 sub get_headref
($$) {
234 if (open(C
,"$git_dir/refs/heads/$name")) {
238 or die "Cannot get head id for $name ($sha): $!\n";
245 or mkdir($git_tree,0777)
246 or die "Could not create $git_tree: $!";
249 my $orig_branch = "";
250 my $forward_master = 0;
253 my $git_dir = $ENV{"GIT_DIR"} || ".git";
254 $git_dir = getwd
()."/".$git_dir unless $git_dir =~ m
#^/#;
255 $ENV{"GIT_DIR"} = $git_dir;
257 $orig_git_index = $ENV{GIT_INDEX_FILE
} if exists $ENV{GIT_INDEX_FILE
};
258 my ($git_ih, $git_index) = tempfile
('gitXXXXXX', SUFFIX
=> '.idx',
259 DIR
=> File
::Spec
->tmpdir());
261 $ENV{GIT_INDEX_FILE
} = $git_index;
265 my $current_rev = $opt_s || 1;
266 unless(-d
$git_dir) {
267 system("git-init-db");
268 die "Cannot init the GIT db at $git_tree: $?\n" if $?
;
269 system("git-read-tree");
270 die "Cannot init an empty tree: $?\n" if $?
;
272 $last_branch = $opt_o;
275 -f
"$git_dir/refs/heads/$opt_o"
276 or die "Branch '$opt_o' does not exist.\n".
277 "Either use the correct '-o branch' option,\n".
278 "or import to a new repository.\n";
280 -f
"$git_dir/svn2git"
281 or die "'$git_dir/svn2git' does not exist.\n".
282 "You need that file for incremental imports.\n";
283 open(F
, "git-symbolic-ref HEAD |") or
284 die "Cannot run git-symbolic-ref: $!\n";
285 chomp ($last_branch = <F
>);
286 $last_branch = basename
($last_branch);
288 unless($last_branch) {
289 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
290 $last_branch = "master";
292 $orig_branch = $last_branch;
293 $last_rev = get_headref
($orig_branch, $git_dir);
294 if (-f
"$git_dir/SVN2GIT_HEAD") {
297 Make sure your working directory corresponds to HEAD and remove SVN2GIT_HEAD.
300 git-read-tree -m -u SVN2GIT_HEAD HEAD
303 system('cp', "$git_dir/HEAD", "$git_dir/SVN2GIT_HEAD");
306 $opt_o ne 'master' && -f
"$git_dir/refs/heads/master" &&
307 system('cmp', '-s', "$git_dir/refs/heads/master",
308 "$git_dir/refs/heads/$opt_o") == 0;
311 system('git-read-tree', $last_rev);
312 die "read-tree failed: $?\n" if $?
;
314 # Get the last import timestamps
315 open my $B,"<", "$git_dir/svn2git";
318 my($num,$branch,$ref) = split;
319 $branches{$branch}{$num} = $ref;
320 $branches{$branch}{"LAST"} = $ref;
321 $current_rev = $num+1 if $current_rev <= $num;
326 or die "Could not create git subdir ($git_dir).\n";
328 my $default_authors = "$git_dir/svn-authors";
331 copy
($opt_A,$default_authors) or die "Copy failed: $!";
333 read_users
($default_authors) if -f
$default_authors;
336 open BRANCHES
,">>", "$git_dir/svn2git";
339 my ($branch, $path, $revision) = @_;
340 my $pool=SVN
::Pool
->new;
341 my $kind = $svn->{'svn'}->check_path(revert_split_path
($branch,$path),$revision,$pool);
346 sub revert_split_path
($$) {
347 my($branch,$path) = @_;
350 $path = "" if $path eq "/"; # this should not happen, but ...
352 $svnpath = "$trunk_name/$path";
353 } elsif($branch =~ m
#^/#) {
354 $svnpath = "$tag_name$branch/$path";
356 $svnpath = "$branch_name/$branch/$path";
364 my($rev,$branch,$path) = @_;
366 my $svnpath = revert_split_path
($branch,$path);
373 # /svn/!svn/bc/2/django/trunk/django-docs/build.py
374 my $url=$svn_url->clone();
375 $url->path($url->path."/!svn/bc/$rev/$svn_dir$svnpath");
376 print "... $path...\n" if $opt_v;
377 $req = HTTP
::Request
->new(GET
=> $url);
378 $res = $lwp_ua->request($req);
379 if ($res->is_success) {
381 ($fh, $name) = tempfile
('gitsvn.XXXXXX',
382 DIR
=> File
::Spec
->tmpdir(), UNLINK
=> 1);
383 print $fh $res->content;
384 close($fh) or die "Could not write $name: $!\n";
386 return undef if $res->code == 301; # directory?
387 die $res->status_line." at $url\n";
389 $mode = '0644'; # can't obtain mode via direct http request?
391 ($name,$mode) = $svn->file("$svnpath",$rev);
392 return undef unless defined $name;
395 my $pid = open(my $F, '-|');
396 die $! unless defined $pid;
398 exec("git-hash-object", "-w", $name)
399 or die "Cannot create object: $!\n";
405 return [$mode, $sha, $path];
408 sub get_ignore
($$$$$) {
409 my($new,$old,$rev,$branch,$path) = @_;
411 return unless $opt_I;
412 my $svnpath = revert_split_path
($branch,$path);
413 my $name = $svn->ignore("$svnpath",$rev);
417 $path = File
::Spec
->catfile($path,$opt_I);
420 my $pid = open(my $F, '-|');
421 die $! unless defined $pid;
423 exec("git-hash-object", "-w", $name)
424 or die "Cannot create object: $!\n";
430 push(@
$new,['0644',$sha,$path]);
440 if($path =~ s
#^/\Q$tag_name\E/([^/]+)/?##) {
442 } elsif($path =~ s
#^/\Q$trunk_name\E/?##) {
444 } elsif($path =~ s
#^/\Q$branch_name\E/([^/]+)/?##) {
452 print STDERR
"$rev: Unrecognized path: $path\n" unless (defined $no_error{$path});
455 $path = "/" if $path eq "";
456 return ($branch,$path);
461 my ($srcbranch,$uptorev) = @_;
463 my $bbranches = $branches{$srcbranch};
464 my @revs = reverse sort { ($a eq 'LAST' ?
0 : $a) <=> ($b eq 'LAST' ?
0 : $b) } keys %$bbranches;
466 foreach my $arev(@revs) {
467 next if ($arev eq 'LAST');
468 if ($arev <= $uptorev) {
476 sub copy_path
($$$$$$$$) {
477 # Somebody copied a whole subdirectory.
478 # We need to find the index entries from the old version which the
479 # SVN log entry points to, and add them to the new place.
481 my($newrev,$newbranch,$path,$oldpath,$rev,$node_kind,$new,$parents) = @_;
483 my($srcbranch,$srcpath) = split_path
($rev,$oldpath);
484 unless(defined $srcbranch) {
485 print "Path not found when copying from $oldpath @ $rev\n";
488 my $therev = branch_rev
($srcbranch, $rev);
489 my $gitrev = $branches{$srcbranch}{$therev};
491 print STDERR
"$newrev:$newbranch: could not find $oldpath \@ $rev\n";
494 if ($srcbranch ne $newbranch) {
495 push(@
$parents, $branches{$srcbranch}{'LAST'});
497 print "$newrev:$newbranch:$path: copying from $srcbranch:$srcpath @ $rev\n" if $opt_v;
498 if ($node_kind eq $SVN::Node
::dir
) {
499 $srcpath =~ s
#/*$#/#;
502 my $pid = open my $f,'-|';
503 die $! unless defined $pid;
505 exec("git-ls-tree","-r","-z",$gitrev,$srcpath)
511 my($m,$p) = split(/\t/,$_,2);
512 my($mode,$type,$sha1) = split(/ /,$m);
513 next if $type ne "blob";
514 if ($node_kind eq $SVN::Node
::dir
) {
515 $p = $path . substr($p,length($srcpath)-1);
519 push(@
$new,[$mode,$sha1,$p]);
522 print STDERR
"$newrev:$newbranch: could not list files in $oldpath \@ $rev\n";
526 my($branch, $changed_paths, $revision, $author, $date, $message) = @_;
527 my($author_name,$author_email,$dest);
528 my(@old,@new,@parents);
530 if (not defined $author) {
531 $author_name = $author_email = "unknown";
532 } elsif (defined $users_file) {
533 die "User $author is not listed in $users_file\n"
534 unless exists $users{$author};
535 ($author_name,$author_email) = @
{$users{$author}};
536 } elsif ($author =~ /^(.*?)\s+<(.*)>$/) {
537 ($author_name, $author_email) = ($1, $2);
539 $author =~ s/^<(.*)>$/$1/;
540 $author_name = $author_email = $author;
542 $date = pdate
($date);
546 if($branch eq "/") { # trunk
548 } elsif($branch =~ m
#^/(.+)#) { # tag
551 } else { # "normal" branch
557 my $prev = $changed_paths->{"/"};
558 if($prev and $prev->[0] eq "A") {
559 delete $changed_paths->{"/"};
560 my $oldpath = $prev->[1];
562 if(defined $oldpath) {
564 ($parent,$p) = split_path
($revision,$oldpath);
568 $parent =~ s
#^/##; # if it's a tag
576 if($revision > $opt_s and defined $parent) {
577 open(H
,"git-rev-parse --verify $parent |");
580 print STDERR
"$revision: cannot find commit '$parent'!\n";
584 if(length($rev) != 40) {
585 print STDERR
"$revision: cannot find commit '$parent'!\n";
588 $rev = $branches{($parent eq $opt_o) ?
"/" : $parent}{"LAST"};
589 if($revision != $opt_s and not $rev) {
590 print STDERR
"$revision: do not know ancestor for '$parent'!\n";
597 # if($prev and $prev->[0] eq "A") {
599 # unless(open(H,"> $git_dir/refs/heads/$branch")) {
600 # print STDERR "$revision: Could not create branch $branch: $!\n";
605 # or die "Could not write branch $branch: $!";
607 # or die "Could not write branch $branch: $!";
610 if(not defined $rev) {
612 } elsif ($rev ne $last_rev) {
613 print "Switching from $last_rev to $rev ($branch)\n" if $opt_v;
614 system("git-read-tree", $rev);
615 die "read-tree failed for $rev: $?\n" if $?
;
619 push (@parents, $rev) if defined $rev;
622 if($tag and not %$changed_paths) {
625 my @paths = sort keys %$changed_paths;
626 foreach my $path(@paths) {
627 my $action = $changed_paths->{$path};
629 if ($action->[0] eq "R") {
630 # refer to a file/tree in an earlier commit
631 push(@old,$path); # remove any old stuff
633 if(($action->[0] eq "A") || ($action->[0] eq "R")) {
634 my $node_kind = node_kind
($branch,$path,$revision);
635 if ($node_kind eq $SVN::Node
::file
) {
636 my $f = get_file
($revision,$branch,$path);
640 my $opath = $action->[3];
641 print STDERR
"$revision: $branch: could not fetch '$opath'\n";
643 } elsif ($node_kind eq $SVN::Node
::dir
) {
645 copy_path
($revision, $branch,
647 $action->[2], $node_kind,
650 get_ignore
(\
@new, \
@old, $revision,
654 } elsif ($action->[0] eq "D") {
656 } elsif ($action->[0] eq "M") {
657 my $node_kind = node_kind
($branch,$path,$revision);
658 if ($node_kind eq $SVN::Node
::file
) {
659 my $f = get_file
($revision,$branch,$path);
661 } elsif ($node_kind eq $SVN::Node
::dir
) {
662 get_ignore
(\
@new, \
@old, $revision,
666 die "$revision: unknown action '".$action->[0]."' for $path\n";
673 @o1 = splice(@old,0,50);
678 my $pid = open my $F, "-|";
679 die "$!" unless defined $pid;
681 exec("git-ls-files", "-z", @o1) or die $!;
694 @o2 = splice(@o1,0,50);
699 system("git-update-index","--force-remove","--",@o2);
700 die "Cannot remove files: $?\n" if $?
;
706 @n2 = splice(@new,0,10);
711 system("git-update-index","--add",
712 (map { ('--cacheinfo', @
$_) } @n2));
713 die "Cannot add files: $?\n" if $?
;
716 my $pid = open(C
,"-|");
717 die "Cannot fork: $!" unless defined $pid;
719 exec("git-write-tree");
720 die "Cannot exec git-write-tree: $!\n";
722 chomp(my $tree = <C
>);
724 or die "Cannot get tree id ($tree): $!\n";
726 or die "Error running git-write-tree: $?\n";
727 print "Tree ID $tree\n" if $opt_v;
729 my $pr = IO
::Pipe
->new() or die "Cannot open pipe: $!\n";
730 my $pw = IO
::Pipe
->new() or die "Cannot open pipe: $!\n";
732 die "Fork: $!\n" unless defined $pid;
736 open(OUT
,">&STDOUT");
737 dup2
($pw->fileno(),0);
738 dup2
($pr->fileno(),1);
744 # loose detection of merges
745 # based on the commit msg
746 foreach my $rx (@mergerx) {
747 if ($message =~ $rx) {
749 if ($mparent eq 'HEAD') { $mparent = $opt_o };
750 if ( -e
"$git_dir/refs/heads/$mparent") {
751 $mparent = get_headref
($mparent, $git_dir);
752 push (@parents, $mparent);
753 print OUT
"Merge parent branch: $mparent\n" if $opt_v;
757 my %seen_parents = ();
758 my @unique_parents = grep { ! $seen_parents{$_} ++ } @parents;
759 foreach my $bparent (@unique_parents) {
760 push @par, '-p', $bparent;
761 print OUT
"Merge parent branch: $bparent\n" if $opt_v;
765 "GIT_AUTHOR_NAME=$author_name",
766 "GIT_AUTHOR_EMAIL=$author_email",
767 "GIT_AUTHOR_DATE=".strftime
("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
768 "GIT_COMMITTER_NAME=$author_name",
769 "GIT_COMMITTER_EMAIL=$author_email",
770 "GIT_COMMITTER_DATE=".strftime
("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
771 "git-commit-tree", $tree,@par);
772 die "Cannot exec git-commit-tree: $!\n";
777 $message =~ s/[\s\n]+\z//;
778 $message = "r$revision: $message" if $opt_r;
780 print $pw "$message\n"
781 or die "Error writing to git-commit-tree: $!\n";
784 print "Committed change $revision:$branch ".strftime
("%Y-%m-%d %H:%M:%S",gmtime($date)).")\n" if $opt_v;
787 or die "Cannot get commit id ($cid): $!\n";
788 print "Commit ID $cid\n" if $opt_v;
792 die "Error running git-commit-tree: $?\n" if $?
;
795 if (not defined $cid) {
796 $cid = $branches{"/"}{"LAST"};
799 if(not defined $dest) {
800 print "... no known parent\n" if $opt_v;
802 print "Writing to refs/heads/$dest\n" if $opt_v;
803 open(C
,">$git_dir/refs/heads/$dest") and
804 print C
("$cid\n") and
806 or die "Cannot write branch $dest for update: $!\n";
810 my($in, $out) = ('','');
811 $last_rev = "-" if %$changed_paths;
812 # the tag was 'complex', i.e. did not refer to a "real" revision
814 $dest =~ tr/_/\./ if $opt_u;
817 my $pid = open2
($in, $out, 'git-mktag');
818 print $out ("object $cid\n".
821 "tagger $author_name <$author_email>\n") and
823 or die "Cannot create tag object $dest: $!\n";
828 if ( !close($in) or waitpid($pid, 0) != $pid or
829 $?
!= 0 or $tagobj !~ /^[0123456789abcdef]{40}$/ ) {
830 die "Cannot create tag object $dest: $!\n";
833 open(C
,">$git_dir/refs/tags/$dest") and
834 print C
("$tagobj\n") and
836 or die "Cannot create tag $branch: $!\n";
838 print "Created tag '$dest' on '$branch'\n" if $opt_v;
840 $branches{$branch}{"LAST"} = $cid;
841 $branches{$branch}{$revision} = $cid;
843 print BRANCHES
"$revision $branch $cid\n";
844 print "DONE: $revision $dest $cid\n" if $opt_v;
848 # Recursive use of the SVN connection does not work
851 my ($changed_paths, $revision, $author, $date, $message, $pool) = @_;
853 while(my($path,$action) = each %$changed_paths) {
854 $p{$path} = [ $action->action,$action->copyfrom_path, $action->copyfrom_rev, $path ];
856 $changed_paths = \
%p;
863 while(my($path,$action) = each %$changed_paths) {
864 ($branch,$path) = split_path
($revision,$path);
865 next if not defined $branch;
866 $done{$branch}{$path} = $action;
868 while(($branch,$changed_paths) = each %done) {
869 commit
($branch, $changed_paths, $revision, $author, $date, $message);
873 $opt_l = $svn->{'maxrev'} if not defined $opt_l or $opt_l > $svn->{'maxrev'};
875 if ($opt_l < $current_rev) {
876 print "Up to date: no new revisions to fetch!\n" if $opt_v;
877 unlink("$git_dir/SVN2GIT_HEAD");
881 print "Fetching from $current_rev to $opt_l ...\n" if $opt_v;
883 my $pool=SVN
::Pool
->new;
884 $svn->{'svn'}->get_log("/",$current_rev,$opt_l,0,1,1,\
&commit_all
,$pool);
890 if (defined $orig_git_index) {
891 $ENV{GIT_INDEX_FILE
} = $orig_git_index;
893 delete $ENV{GIT_INDEX_FILE
};
896 # Now switch back to the branch we were in before all of this happened
898 print "DONE\n" if $opt_v and (not defined $opt_l or $opt_l > 0);
899 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
902 system('git-read-tree', '-m', '-u', 'SVN2GIT_HEAD', 'HEAD');
903 die "read-tree failed: $?\n" if $?
;
906 $orig_branch = "master";
907 print "DONE; creating $orig_branch branch\n" if $opt_v and (not defined $opt_l or $opt_l > 0);
908 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
909 unless -f
"$git_dir/refs/heads/master";
910 system('git-update-ref', 'HEAD', "$orig_branch");
912 system('git checkout');
913 die "checkout failed: $?\n" if $?
;
916 unlink("$git_dir/SVN2GIT_HEAD");