]> git.ipfire.org Git - thirdparty/git.git/blame - git-svnimport.perl
svn import: skip initial revisions
[thirdparty/git.git] / git-svnimport.perl
CommitLineData
eaf718f3
MU
1#!/usr/bin/perl -w
2
3# This tool is copyright (c) 2005, Matthias Urlichs.
4# It is released under the Gnu Public License, version 2.
5#
6# The basic idea is to pull and analyze SVN changes.
7#
8# Checking out the files is done by a single long-running CVS connection
9# / server process.
10#
11# The head revision is on branch "origin" by default.
12# You can change that with the '-o' option.
13
14require v5.8.0; # for shell-safe open("-|",LIST)
15use strict;
16use warnings;
17use Getopt::Std;
18use File::Spec;
19use File::Temp qw(tempfile);
20use File::Path qw(mkpath);
21use File::Basename qw(basename dirname);
22use Time::Local;
23use IO::Pipe;
24use POSIX qw(strftime dup2);
25use IPC::Open2;
26use SVN::Core;
27use SVN::Ra;
28
37dcf6de
MU
29die "Need CVN:COre 1.2.1 or better" if $SVN::Core::VERSION lt "1.2.1";
30
eaf718f3
MU
31$SIG{'PIPE'}="IGNORE";
32$ENV{'TZ'}="UTC";
33
7ee74a99 34our($opt_h,$opt_o,$opt_v,$opt_u,$opt_C,$opt_i,$opt_m,$opt_M,$opt_t,$opt_T,$opt_b,$opt_s);
eaf718f3
MU
35
36sub usage() {
37 print STDERR <<END;
38Usage: ${\basename $0} # fetch/update GIT from CVS
39 [-o branch-for-HEAD] [-h] [-v]
40 [-C GIT_repository] [-t tagname] [-T trunkname] [-b branchname]
7ee74a99 41 [-i] [-u] [-s start_chg] [-m] [-M regex] [SVN_URL]
eaf718f3
MU
42END
43 exit(1);
44}
45
7ee74a99 46getopts("b:C:hivmM:o:s:t:T:u") or usage();
eaf718f3
MU
47usage if $opt_h;
48
49my $tag_name = $opt_t || "tags";
50my $trunk_name = $opt_T || "trunk";
51my $branch_name = $opt_b || "branches";
52
53@ARGV <= 1 or usage();
54
55$opt_o ||= "origin";
56my $git_tree = $opt_C;
57$git_tree ||= ".";
58
59my $cvs_tree;
60if ($#ARGV == 0) {
61 $cvs_tree = $ARGV[0];
62} elsif (-f 'CVS/Repository') {
63 open my $f, '<', 'CVS/Repository' or
64 die 'Failed to open CVS/Repository';
65 $cvs_tree = <$f>;
66 chomp $cvs_tree;
67 close $f;
68} else {
69 usage();
70}
71
72our @mergerx = ();
73if ($opt_m) {
74 @mergerx = ( qr/\W(?:from|of|merge|merging|merged) (\w+)/i );
75}
76if ($opt_M) {
77 push (@mergerx, qr/$opt_M/);
78}
79
80select(STDERR); $|=1; select(STDOUT);
81
82
83package SVNconn;
84# Basic SVN connection.
85# We're only interested in connecting and downloading, so ...
86
87use File::Spec;
88use File::Temp qw(tempfile);
89use POSIX qw(strftime dup2);
90
91sub new {
92 my($what,$repo) = @_;
93 $what=ref($what) if ref($what);
94
95 my $self = {};
96 $self->{'buffer'} = "";
97 bless($self,$what);
98
99 $repo =~ s#/+$##;
100 $self->{'fullrep'} = $repo;
101 $self->conn();
102
eaf718f3
MU
103 return $self;
104}
105
106sub conn {
107 my $self = shift;
108 my $repo = $self->{'fullrep'};
109 my $s = SVN::Ra->new($repo);
110
111 die "SVN connection to $repo: $!\n" unless defined $s;
112 $self->{'svn'} = $s;
113 $self->{'repo'} = $repo;
114 $self->{'maxrev'} = $s->get_latest_revnum();
115}
116
117sub file {
118 my($self,$path,$rev) = @_;
119 my $res;
120
121 my ($fh, $name) = tempfile('gitsvn.XXXXXX',
122 DIR => File::Spec->tmpdir(), UNLINK => 1);
123
2b5e63d1 124 print "... $rev $path ...\n" if $opt_v;
f02b3eba 125 eval { $self->{'svn'}->get_file($path,$rev,$fh); };
8cd4177d 126 if ($@ and $@ !~ /Attempted to get checksum/) {
eaf718f3
MU
127 # retry
128 $self->conn();
2b5e63d1 129 eval { $self->{'svn'}->get_file($path,$rev,$fh); };
eaf718f3 130 };
8cd4177d 131 return () if $@ and $@ !~ /Attempted to get checksum/;
2b5e63d1 132 die $@ if $@;
eaf718f3
MU
133 close ($fh);
134
135 return ($name, $res);
136}
137
138
139package main;
140
141my $svn = SVNconn->new($cvs_tree);
142
143
144sub pdate($) {
145 my($d) = @_;
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);
150}
151
eaf718f3
MU
152sub getwd() {
153 my $pwd = `pwd`;
154 chomp $pwd;
155 return $pwd;
156}
157
158
159sub get_headref($$) {
160 my $name = shift;
161 my $git_dir = shift;
162 my $sha;
163
164 if (open(C,"$git_dir/refs/heads/$name")) {
165 chomp($sha = <C>);
166 close(C);
167 length($sha) == 40
168 or die "Cannot get head id for $name ($sha): $!\n";
169 }
170 return $sha;
171}
172
173
174-d $git_tree
175 or mkdir($git_tree,0777)
176 or die "Could not create $git_tree: $!";
177chdir($git_tree);
178
179my $orig_branch = "";
180my $forward_master = 0;
181my %branches;
182
183my $git_dir = $ENV{"GIT_DIR"} || ".git";
184$git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#;
185$ENV{"GIT_DIR"} = $git_dir;
186my $orig_git_index;
187$orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE};
188my ($git_ih, $git_index) = tempfile('gitXXXXXX', SUFFIX => '.idx',
189 DIR => File::Spec->tmpdir());
190close ($git_ih);
191$ENV{GIT_INDEX_FILE} = $git_index;
192my $maxnum = 0;
193my $last_rev = "";
194my $last_branch;
7ee74a99 195my $current_rev = $opt_s ? ($opt_s-1) : 0;
eaf718f3
MU
196unless(-d $git_dir) {
197 system("git-init-db");
198 die "Cannot init the GIT db at $git_tree: $?\n" if $?;
199 system("git-read-tree");
200 die "Cannot init an empty tree: $?\n" if $?;
201
202 $last_branch = $opt_o;
203 $orig_branch = "";
204} else {
205 -f "$git_dir/refs/heads/$opt_o"
206 or die "Branch '$opt_o' does not exist.\n".
207 "Either use the correct '-o branch' option,\n".
208 "or import to a new repository.\n";
209
210 -f "$git_dir/svn2git"
211 or die "'$git_dir/svn2git' does not exist.\n".
212 "You need that file for incremental imports.\n";
213 $last_branch = basename(readlink("$git_dir/HEAD"));
214 unless($last_branch) {
215 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
216 $last_branch = "master";
217 }
218 $orig_branch = $last_branch;
219 $last_rev = get_headref($orig_branch, $git_dir);
220 if (-f "$git_dir/SVN2GIT_HEAD") {
221 die <<EOM;
222SVN2GIT_HEAD exists.
223Make sure your working directory corresponds to HEAD and remove SVN2GIT_HEAD.
224You may need to run
225
226 git-read-tree -m -u SVN2GIT_HEAD HEAD
227EOM
228 }
229 system('cp', "$git_dir/HEAD", "$git_dir/SVN2GIT_HEAD");
230
231 $forward_master =
232 $opt_o ne 'master' && -f "$git_dir/refs/heads/master" &&
233 system('cmp', '-s', "$git_dir/refs/heads/master",
234 "$git_dir/refs/heads/$opt_o") == 0;
235
236 # populate index
237 system('git-read-tree', $last_rev);
238 die "read-tree failed: $?\n" if $?;
239
240 # Get the last import timestamps
241 open my $B,"<", "$git_dir/svn2git";
242 while(<$B>) {
243 chomp;
244 my($num,$branch,$ref) = split;
245 $branches{$branch}{$num} = $ref;
246 $branches{$branch}{"LAST"} = $ref;
247 $current_rev = $num+1 if $current_rev < $num+1;
248 }
249 close($B);
250}
251-d $git_dir
252 or die "Could not create git subdir ($git_dir).\n";
253
254open BRANCHES,">>", "$git_dir/svn2git";
255
256
257## cvsps output:
258#---------------------
259#PatchSet 314
260#Date: 1999/09/18 13:03:59
261#Author: wkoch
262#Branch: STABLE-BRANCH-1-0
263#Ancestor branch: HEAD
264#Tag: (none)
265#Log:
266# See ChangeLog: Sat Sep 18 13:03:28 CEST 1999 Werner Koch
267#Members:
268# README:1.57->1.57.2.1
269# VERSION:1.96->1.96.2.1
270#
271#---------------------
272
273my $state = 0;
274
275sub get_file($$$) {
276 my($rev,$branch,$path) = @_;
277
278 # revert split_path(), below
279 my $svnpath;
280 $path = "" if $path eq "/"; # this should not happen, but ...
281 if($branch eq "/") {
282 $svnpath = "/$trunk_name/$path";
283 } elsif($branch =~ m#^/#) {
284 $svnpath = "/$tag_name$branch/$path";
285 } else {
286 $svnpath = "/$branch_name/$branch/$path";
287 }
288
289 # now get it
2b5e63d1
MU
290 my ($name, $res) = eval { $svn->file($svnpath,$rev); };
291 return () unless defined $name;
eaf718f3 292
6d281217 293 open my $F, '-|', "git-hash-object", "-w", $name
eaf718f3
MU
294 or die "Cannot create object: $!\n";
295 my $sha = <$F>;
296 chomp $sha;
297 close $F;
eaf718f3
MU
298 my $mode = "0644"; # SV does not seem to store any file modes
299 return [$mode, $sha, $path];
300}
301
302sub split_path($$) {
303 my($rev,$path) = @_;
304 my $branch;
305
306 if($path =~ s#^/\Q$tag_name\E/([^/]+)/?##) {
307 $branch = "/$1";
308 } elsif($path =~ s#^/\Q$trunk_name\E/?##) {
309 $branch = "/";
310 } elsif($path =~ s#^/\Q$branch_name\E/([^/]+)/?##) {
311 $branch = $1;
312 } else {
313 print STDERR "$rev: Unrecognized path: $path\n";
314 return ()
315 }
316 $path = "/" if $path eq "";
317 return ($branch,$path);
318}
319
320sub commit {
321 my($branch, $changed_paths, $revision, $author, $date, $message) = @_;
322 my($author_name,$author_email,$dest);
323 my(@old,@new);
324
2b5e63d1
MU
325 if (not defined $author) {
326 $author_name = $author_email = "unknown";
327 } elsif ($author =~ /^(.*?)\s+<(.*)>$/) {
eaf718f3
MU
328 ($author_name, $author_email) = ($1, $2);
329 } else {
330 $author =~ s/^<(.*)>$/$1/;
331 $author_name = $author_email = $author;
332 }
333 $date = pdate($date);
334
335 my $tag;
336 my $parent;
337 if($branch eq "/") { # trunk
338 $parent = $opt_o;
339 } elsif($branch =~ m#^/(.+)#) { # tag
340 $tag = 1;
341 $parent = $1;
342 } else { # "normal" branch
343 # nothing to do
344 $parent = $branch;
345 }
346 $dest = $parent;
347
348 my $prev = $changed_paths->{"/"};
f0daa628 349 if($prev and $prev->[0] eq "A") {
eaf718f3 350 delete $changed_paths->{"/"};
f0daa628 351 my $oldpath = $prev->[1];
eaf718f3
MU
352 my $rev;
353 if(defined $oldpath) {
354 my $p;
355 ($parent,$p) = split_path($revision,$oldpath);
356 if($parent eq "/") {
357 $parent = $opt_o;
358 } else {
359 $parent =~ s#^/##; # if it's a tag
360 }
361 } else {
362 $parent = undef;
363 }
364 }
365
366 my $rev;
7ee74a99 367 if($revision > $opt_s and defined $parent) {
eaf718f3
MU
368 open(H,"git-rev-parse --verify $parent |");
369 $rev = <H>;
370 close(H) or do {
371 print STDERR "$revision: cannot find commit '$parent'!\n";
372 return;
373 };
374 chop $rev;
375 if(length($rev) != 40) {
376 print STDERR "$revision: cannot find commit '$parent'!\n";
377 return;
378 }
379 $rev = $branches{($parent eq $opt_o) ? "/" : $parent}{"LAST"};
7ee74a99 380 if($revision != $opt_s and not $rev) {
eaf718f3
MU
381 print STDERR "$revision: do not know ancestor for '$parent'!\n";
382 return;
383 }
384 } else {
385 $rev = undef;
386 }
387
f0daa628 388# if($prev and $prev->[0] eq "A") {
eaf718f3
MU
389# if(not $tag) {
390# unless(open(H,"> $git_dir/refs/heads/$branch")) {
391# print STDERR "$revision: Could not create branch $branch: $!\n";
392# $state=11;
393# next;
394# }
395# print H "$rev\n"
396# or die "Could not write branch $branch: $!";
397# close(H)
398# or die "Could not write branch $branch: $!";
399# }
400# }
401 if(not defined $rev) {
402 unlink($git_index);
403 } elsif ($rev ne $last_rev) {
404 print "Switching from $last_rev to $rev ($branch)\n" if $opt_v;
405 system("git-read-tree", $rev);
406 die "read-tree failed for $rev: $?\n" if $?;
407 $last_rev = $rev;
408 }
409
bf267d99
MU
410 my $cid;
411 if($tag and not %$changed_paths) {
412 $cid = $rev;
413 } else {
414 while(my($path,$action) = each %$changed_paths) {
415 if ($action->[0] eq "A") {
416 my $f = get_file($revision,$branch,$path);
417 push(@new,$f) if $f;
418 } elsif ($action->[0] eq "D") {
419 push(@old,$path);
420 } elsif ($action->[0] eq "M") {
421 my $f = get_file($revision,$branch,$path);
422 push(@new,$f) if $f;
423 } elsif ($action->[0] eq "R") {
424 # refer to a file/tree in an earlier commit
425 push(@old,$path); # remove any old stuff
426
427 # ... and add any new stuff
428 my($b,$p) = split_path($revision,$action->[1]);
429 open my $F,"-|","git-ls-tree","-r","-z", $branches{$b}{$action->[2]}, $p;
430 local $/ = '\0';
431 while(<$F>) {
432 chomp;
433 my($m,$p) = split(/\t/,$_,2);
434 my($mode,$type,$sha1) = split(/ /,$m);
435 next if $type ne "blob";
436 push(@new,[$mode,$sha1,$p]);
437 }
438 } else {
439 die "$revision: unknown action '".$action->[0]."' for $path\n";
440 }
441 }
442
443 if(@old) {
444 open my $F, "-|", "git-ls-files", "-z", @old or die $!;
445 @old = ();
eaf718f3
MU
446 local $/ = '\0';
447 while(<$F>) {
448 chomp;
bf267d99
MU
449 push(@old,$_);
450 }
451 close($F);
452
453 while(@old) {
454 my @o2;
455 if(@old > 55) {
456 @o2 = splice(@old,0,50);
457 } else {
458 @o2 = @old;
459 @old = ();
460 }
461 system("git-update-index","--force-remove","--",@o2);
462 die "Cannot remove files: $?\n" if $?;
eaf718f3 463 }
eaf718f3 464 }
bf267d99
MU
465 while(@new) {
466 my @n2;
467 if(@new > 12) {
468 @n2 = splice(@new,0,10);
eaf718f3 469 } else {
bf267d99
MU
470 @n2 = @new;
471 @new = ();
eaf718f3 472 }
bf267d99
MU
473 system("git-update-index","--add",
474 (map { ('--cacheinfo', @$_) } @n2));
475 die "Cannot add files: $?\n" if $?;
eaf718f3 476 }
eaf718f3 477
bf267d99
MU
478 my $pid = open(C,"-|");
479 die "Cannot fork: $!" unless defined $pid;
480 unless($pid) {
481 exec("git-write-tree");
482 die "Cannot exec git-write-tree: $!\n";
eaf718f3 483 }
bf267d99
MU
484 chomp(my $tree = <C>);
485 length($tree) == 40
486 or die "Cannot get tree id ($tree): $!\n";
487 close(C)
488 or die "Error running git-write-tree: $?\n";
489 print "Tree ID $tree\n" if $opt_v;
490
491 my $pr = IO::Pipe->new() or die "Cannot open pipe: $!\n";
492 my $pw = IO::Pipe->new() or die "Cannot open pipe: $!\n";
493 $pid = fork();
494 die "Fork: $!\n" unless defined $pid;
495 unless($pid) {
496 $pr->writer();
497 $pw->reader();
498 open(OUT,">&STDOUT");
499 dup2($pw->fileno(),0);
500 dup2($pr->fileno(),1);
501 $pr->close();
502 $pw->close();
503
504 my @par = ();
505 @par = ("-p",$rev) if defined $rev;
506
507 # loose detection of merges
508 # based on the commit msg
509 foreach my $rx (@mergerx) {
510 if ($message =~ $rx) {
511 my $mparent = $1;
512 if ($mparent eq 'HEAD') { $mparent = $opt_o };
513 if ( -e "$git_dir/refs/heads/$mparent") {
514 $mparent = get_headref($mparent, $git_dir);
515 push @par, '-p', $mparent;
516 print OUT "Merge parent branch: $mparent\n" if $opt_v;
517 }
518 }
519 }
eaf718f3 520
bf267d99
MU
521 exec("env",
522 "GIT_AUTHOR_NAME=$author_name",
523 "GIT_AUTHOR_EMAIL=$author_email",
524 "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
525 "GIT_COMMITTER_NAME=$author_name",
526 "GIT_COMMITTER_EMAIL=$author_email",
527 "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
528 "git-commit-tree", $tree,@par);
529 die "Cannot exec git-commit-tree: $!\n";
530 }
531 $pw->writer();
532 $pr->reader();
eaf718f3 533
bf267d99 534 $message =~ s/[\s\n]+\z//;
eaf718f3 535
bf267d99
MU
536 print $pw "$message\n"
537 or die "Error writing to git-commit-tree: $!\n";
538 $pw->close();
eaf718f3 539
bf267d99
MU
540 print "Committed change $revision:$branch ".strftime("%Y-%m-%d %H:%M:%S",gmtime($date)).")\n" if $opt_v;
541 chomp($cid = <$pr>);
542 length($cid) == 40
543 or die "Cannot get commit id ($cid): $!\n";
544 print "Commit ID $cid\n" if $opt_v;
545 $pr->close();
eaf718f3 546
bf267d99
MU
547 waitpid($pid,0);
548 die "Error running git-commit-tree: $?\n" if $?;
549 }
eaf718f3 550
f02b3eba
MU
551 if(not defined $dest) {
552 print "... no known parent\n" if $opt_v;
553 } elsif(not $tag) {
eaf718f3
MU
554 print "Writing to refs/heads/$dest\n" if $opt_v;
555 open(C,">$git_dir/refs/heads/$dest") and
556 print C ("$cid\n") and
557 close(C)
558 or die "Cannot write branch $dest for update: $!\n";
eaf718f3
MU
559 }
560 $branches{$branch}{"LAST"} = $cid;
561 $branches{$branch}{$revision} = $cid;
562 $last_rev = $cid;
563 print BRANCHES "$revision $branch $cid\n";
564 print "DONE: $revision $dest $cid\n" if $opt_v;
565
566 if($tag) {
567 my($in, $out) = ('','');
568 $last_rev = "-" if %$changed_paths;
569 # the tag was 'complex', i.e. did not refer to a "real" revision
570
f02b3eba 571 $dest =~ tr/_/\./ if $opt_u;
eaf718f3
MU
572
573 my $pid = open2($in, $out, 'git-mktag');
574 print $out ("object $cid\n".
575 "type commit\n".
f02b3eba 576 "tag $dest\n".
eaf718f3
MU
577 "tagger $author_name <$author_email>\n") and
578 close($out)
f02b3eba 579 or die "Cannot create tag object $dest: $!\n";
eaf718f3
MU
580
581 my $tagobj = <$in>;
582 chomp $tagobj;
583
584 if ( !close($in) or waitpid($pid, 0) != $pid or
585 $? != 0 or $tagobj !~ /^[0123456789abcdef]{40}$/ ) {
f02b3eba 586 die "Cannot create tag object $dest: $!\n";
eaf718f3 587 }
eaf718f3 588
f02b3eba
MU
589 open(C,">$git_dir/refs/tags/$dest") and
590 print C ("$tagobj\n") and
eaf718f3 591 close(C)
f02b3eba 592 or die "Cannot create tag $branch: $!\n";
eaf718f3 593
f02b3eba 594 print "Created tag '$dest' on '$branch'\n" if $opt_v;
eaf718f3
MU
595 }
596}
597
598my ($changed_paths, $revision, $author, $date, $message, $pool) = @_;
599sub _commit_all {
600 ($changed_paths, $revision, $author, $date, $message, $pool) = @_;
f0daa628
MU
601 my %p;
602 while(my($path,$action) = each %$changed_paths) {
603 $p{$path} = [ $action->action,$action->copyfrom_path, $action->copyfrom_rev ];
604 }
605 $changed_paths = \%p;
eaf718f3 606}
f0daa628 607
eaf718f3
MU
608sub commit_all {
609 my %done;
610 my @col;
611 my $pref;
612 my $branch;
613
614 while(my($path,$action) = each %$changed_paths) {
615 ($branch,$path) = split_path($revision,$path);
616 next if not defined $branch;
617 $done{$branch}{$path} = $action;
618 }
619 while(($branch,$changed_paths) = each %done) {
620 commit($branch, $changed_paths, $revision, $author, $date, $message);
621 }
622}
623
624while(++$current_rev < $svn->{'maxrev'}) {
625 $svn->{'svn'}->get_log("/",$current_rev,$current_rev,$current_rev,1,1,\&_commit_all,"");
626 commit_all();
627}
628
629
630unlink($git_index);
631
632if (defined $orig_git_index) {
633 $ENV{GIT_INDEX_FILE} = $orig_git_index;
634} else {
635 delete $ENV{GIT_INDEX_FILE};
636}
637
638# Now switch back to the branch we were in before all of this happened
639if($orig_branch) {
640 print "DONE\n" if $opt_v;
641 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
642 if $forward_master;
643 unless ($opt_i) {
644 system('git-read-tree', '-m', '-u', 'SVN2GIT_HEAD', 'HEAD');
645 die "read-tree failed: $?\n" if $?;
646 }
647} else {
648 $orig_branch = "master";
649 print "DONE; creating $orig_branch branch\n" if $opt_v;
650 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
651 unless -f "$git_dir/refs/heads/master";
652 unlink("$git_dir/HEAD");
653 symlink("refs/heads/$orig_branch","$git_dir/HEAD");
654 unless ($opt_i) {
655 system('git checkout');
656 die "checkout failed: $?\n" if $?;
657 }
658}
659unlink("$git_dir/SVN2GIT_HEAD");
660close(BRANCHES);