]> git.ipfire.org Git - thirdparty/git.git/blame - contrib/examples/git-svnimport.perl
refs: docstring typo
[thirdparty/git.git] / contrib / examples / git-svnimport.perl
CommitLineData
3328aced 1#!/usr/bin/perl
eaf718f3
MU
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#
4a91b796 8# Checking out the files is done by a single long-running SVN connection.
eaf718f3
MU
9#
10# The head revision is on branch "origin" by default.
11# You can change that with the '-o' option.
12
eaf718f3
MU
13use strict;
14use warnings;
15use Getopt::Std;
d3cac2c9 16use File::Copy;
eaf718f3
MU
17use File::Spec;
18use File::Temp qw(tempfile);
19use File::Path qw(mkpath);
20use File::Basename qw(basename dirname);
21use Time::Local;
22use IO::Pipe;
23use POSIX qw(strftime dup2);
24use IPC::Open2;
25use SVN::Core;
26use SVN::Ra;
27
f3ad0625 28die "Need SVN:Core 1.2.1 or better" if $SVN::Core::VERSION lt "1.2.1";
37dcf6de 29
eaf718f3
MU
30$SIG{'PIPE'}="IGNORE";
31$ENV{'TZ'}="UTC";
32
c55f3fff 33our($opt_h,$opt_o,$opt_v,$opt_u,$opt_C,$opt_i,$opt_m,$opt_M,$opt_t,$opt_T,
40006ea0
SK
34 $opt_b,$opt_r,$opt_I,$opt_A,$opt_s,$opt_l,$opt_d,$opt_D,$opt_S,$opt_F,
35 $opt_P,$opt_R);
eaf718f3
MU
36
37sub usage() {
38 print STDERR <<END;
beb5ab18 39usage: ${\basename $0} # fetch/update GIT from SVN
40006ea0 40 [-o branch-for-HEAD] [-h] [-v] [-l max_rev] [-R repack_each_revs]
eaf718f3 41 [-C GIT_repository] [-t tagname] [-T trunkname] [-b branchname]
c55f3fff 42 [-d|-D] [-i] [-u] [-r] [-I ignorefilename] [-s start_chg]
ec1e4689 43 [-m] [-M regex] [-A author_file] [-S] [-F] [-P project_name] [SVN_URL]
eaf718f3
MU
44END
45 exit(1);
46}
47
40006ea0 48getopts("A:b:C:dDFhiI:l:mM:o:rs:t:T:SP:R:uv") or usage();
eaf718f3
MU
49usage if $opt_h;
50
51my $tag_name = $opt_t || "tags";
0476786e 52my $trunk_name = defined $opt_T ? $opt_T : "trunk";
eaf718f3 53my $branch_name = $opt_b || "branches";
ec1e4689
SK
54my $project_name = $opt_P || "";
55$project_name = "/" . $project_name if ($project_name);
40006ea0 56my $repack_after = $opt_R || 1000;
5d17d765 57my $root_pool = SVN::Pool->new_default;
eaf718f3 58
25f6f325 59@ARGV == 1 or @ARGV == 2 or usage();
eaf718f3
MU
60
61$opt_o ||= "origin";
2fa92046 62$opt_s ||= 1;
eaf718f3
MU
63my $git_tree = $opt_C;
64$git_tree ||= ".";
65
4a91b796 66my $svn_url = $ARGV[0];
25f6f325 67my $svn_dir = $ARGV[1];
eaf718f3
MU
68
69our @mergerx = ();
70if ($opt_m) {
65160b8b
FF
71 my $branch_esc = quotemeta ($branch_name);
72 my $trunk_esc = quotemeta ($trunk_name);
73 @mergerx =
74 (
75 qr!\b(?:merg(?:ed?|ing))\b.*?\b((?:(?<=$branch_esc/)[\w\.\-]+)|(?:$trunk_esc))\b!i,
76 qr!\b(?:from|of)\W+((?:(?<=$branch_esc/)[\w\.\-]+)|(?:$trunk_esc))\b!i,
77 qr!\b(?:from|of)\W+(?:the )?([\w\.\-]+)[-\s]branch\b!i
78 );
eaf718f3
MU
79}
80if ($opt_M) {
65160b8b 81 unshift (@mergerx, qr/$opt_M/);
eaf718f3
MU
82}
83
d3cac2c9
KW
84# Absolutize filename now, since we will have chdir'ed by the time we
85# get around to opening it.
86$opt_A = File::Spec->rel2abs($opt_A) if $opt_A;
87
36610b24 88our %users = ();
d3cac2c9
KW
89our $users_file = undef;
90sub read_users($) {
91 $users_file = File::Spec->rel2abs(@_);
92 die "Cannot open $users_file\n" unless -f $users_file;
93 open(my $authors,$users_file);
36610b24
KW
94 while(<$authors>) {
95 chomp;
80804d0a 96 next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
36610b24
KW
97 (my $user,my $name,my $email) = ($1,$2,$3);
98 $users{$user} = [$name,$email];
99 }
100 close($authors);
101}
102
eaf718f3
MU
103select(STDERR); $|=1; select(STDOUT);
104
105
106package SVNconn;
107# Basic SVN connection.
108# We're only interested in connecting and downloading, so ...
109
110use File::Spec;
111use File::Temp qw(tempfile);
112use POSIX qw(strftime dup2);
08ddd4f7 113use Fcntl qw(SEEK_SET);
eaf718f3
MU
114
115sub new {
116 my($what,$repo) = @_;
117 $what=ref($what) if ref($what);
118
119 my $self = {};
120 $self->{'buffer'} = "";
121 bless($self,$what);
122
123 $repo =~ s#/+$##;
124 $self->{'fullrep'} = $repo;
125 $self->conn();
126
eaf718f3
MU
127 return $self;
128}
129
130sub conn {
131 my $self = shift;
132 my $repo = $self->{'fullrep'};
2961e0ee
EW
133 my $auth = SVN::Core::auth_open ([SVN::Client::get_simple_provider,
134 SVN::Client::get_ssl_server_trust_file_provider,
135 SVN::Client::get_username_provider]);
5d17d765 136 my $s = SVN::Ra->new(url => $repo, auth => $auth, pool => $root_pool);
eaf718f3
MU
137 die "SVN connection to $repo: $!\n" unless defined $s;
138 $self->{'svn'} = $s;
139 $self->{'repo'} = $repo;
140 $self->{'maxrev'} = $s->get_latest_revnum();
141}
142
143sub file {
144 my($self,$path,$rev) = @_;
eaf718f3 145
29504118 146 my ($fh, $name) = tempfile('gitsvn.XXXXXX',
eaf718f3
MU
147 DIR => File::Spec->tmpdir(), UNLINK => 1);
148
2b5e63d1 149 print "... $rev $path ...\n" if $opt_v;
4802426d 150 my (undef, $properties);
09c3a408 151 $path =~ s#^/*##;
5d17d765 152 my $subpool = SVN::Pool::new_default_sub;
4802426d 153 eval { (undef, $properties)
5d17d765 154 = $self->{'svn'}->get_file($path,$rev,$fh); };
25f6f325
MU
155 if($@) {
156 return undef if $@ =~ /Attempted to get checksum/;
157 die $@;
158 }
4802426d
KW
159 my $mode;
160 if (exists $properties->{'svn:executable'}) {
08ddd4f7
HVR
161 $mode = '100755';
162 } elsif (exists $properties->{'svn:special'}) {
163 my ($special_content, $filesize);
164 $filesize = tell $fh;
165 seek $fh, 0, SEEK_SET;
166 read $fh, $special_content, $filesize;
167 if ($special_content =~ s/^link //) {
168 $mode = '120000';
169 seek $fh, 0, SEEK_SET;
170 truncate $fh, 0;
171 print $fh $special_content;
172 } else {
173 die "unexpected svn:special file encountered";
174 }
4802426d 175 } else {
08ddd4f7 176 $mode = '100644';
4802426d 177 }
eaf718f3
MU
178 close ($fh);
179
4802426d 180 return ($name, $mode);
eaf718f3
MU
181}
182
c55f3fff
KW
183sub ignore {
184 my($self,$path,$rev) = @_;
185
186 print "... $rev $path ...\n" if $opt_v;
09c3a408 187 $path =~ s#^/*##;
5d17d765 188 my $subpool = SVN::Pool::new_default_sub;
c55f3fff
KW
189 my (undef,undef,$properties)
190 = $self->{'svn'}->get_dir($path,$rev,undef);
191 if (exists $properties->{'svn:ignore'}) {
192 my ($fh, $name) = tempfile('gitsvn.XXXXXX',
193 DIR => File::Spec->tmpdir(),
194 UNLINK => 1);
195 print $fh $properties->{'svn:ignore'};
196 close($fh);
197 return $name;
198 } else {
199 return undef;
200 }
201}
202
83936a29
SK
203sub dir_list {
204 my($self,$path,$rev) = @_;
09c3a408 205 $path =~ s#^/*##;
5d17d765 206 my $subpool = SVN::Pool::new_default_sub;
83936a29
SK
207 my ($dirents,undef,$properties)
208 = $self->{'svn'}->get_dir($path,$rev,undef);
209 return $dirents;
210}
211
eaf718f3 212package main;
25f6f325 213use URI;
eaf718f3 214
03490804 215our $svn = $svn_url;
25f6f325 216$svn .= "/$svn_dir" if defined $svn_dir;
03490804 217my $svn2 = SVNconn->new($svn);
25f6f325 218$svn = SVNconn->new($svn);
eaf718f3 219
25f6f325
MU
220my $lwp_ua;
221if($opt_d or $opt_D) {
222 $svn_url = URI->new($svn_url)->canonical;
223 if($opt_D) {
224 $svn_dir =~ s#/*$#/#;
225 } else {
226 $svn_dir = "";
227 }
228 if ($svn_url->scheme eq "http") {
229 use LWP::UserAgent;
230 $lwp_ua = LWP::UserAgent->new(keep_alive => 1, requests_redirectable => []);
231 } else {
232 print STDERR "Warning: not HTTP; turning off direct file access\n";
233 $opt_d=0;
234 }
235}
eaf718f3
MU
236
237sub pdate($) {
238 my($d) = @_;
239 $d =~ m#(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)#
240 or die "Unparseable date: $d\n";
a40e06ee 241 my $y=$1; $y+=1900 if $y<1000;
eaf718f3
MU
242 return timegm($6||0,$5,$4,$3,$2-1,$y);
243}
244
eaf718f3
MU
245sub getwd() {
246 my $pwd = `pwd`;
247 chomp $pwd;
248 return $pwd;
249}
250
251
252sub get_headref($$) {
253 my $name = shift;
29504118 254 my $git_dir = shift;
eaf718f3 255 my $sha;
29504118 256
eaf718f3
MU
257 if (open(C,"$git_dir/refs/heads/$name")) {
258 chomp($sha = <C>);
259 close(C);
260 length($sha) == 40
261 or die "Cannot get head id for $name ($sha): $!\n";
262 }
263 return $sha;
264}
265
266
267-d $git_tree
268 or mkdir($git_tree,0777)
269 or die "Could not create $git_tree: $!";
270chdir($git_tree);
271
272my $orig_branch = "";
273my $forward_master = 0;
274my %branches;
275
276my $git_dir = $ENV{"GIT_DIR"} || ".git";
277$git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#;
278$ENV{"GIT_DIR"} = $git_dir;
279my $orig_git_index;
280$orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE};
281my ($git_ih, $git_index) = tempfile('gitXXXXXX', SUFFIX => '.idx',
282 DIR => File::Spec->tmpdir());
283close ($git_ih);
284$ENV{GIT_INDEX_FILE} = $git_index;
285my $maxnum = 0;
286my $last_rev = "";
287my $last_branch;
03490804 288my $current_rev = $opt_s || 1;
eaf718f3 289unless(-d $git_dir) {
3ca93642 290 system("git init");
eaf718f3 291 die "Cannot init the GIT db at $git_tree: $?\n" if $?;
1bb28d87 292 system("git read-tree --empty");
eaf718f3
MU
293 die "Cannot init an empty tree: $?\n" if $?;
294
295 $last_branch = $opt_o;
296 $orig_branch = "";
297} else {
298 -f "$git_dir/refs/heads/$opt_o"
299 or die "Branch '$opt_o' does not exist.\n".
300 "Either use the correct '-o branch' option,\n".
301 "or import to a new repository.\n";
302
303 -f "$git_dir/svn2git"
304 or die "'$git_dir/svn2git' does not exist.\n".
305 "You need that file for incremental imports.\n";
3ca93642 306 open(F, "git symbolic-ref HEAD |") or
8366a10a
PR
307 die "Cannot run git-symbolic-ref: $!\n";
308 chomp ($last_branch = <F>);
309 $last_branch = basename($last_branch);
310 close(F);
eaf718f3
MU
311 unless($last_branch) {
312 warn "Cannot read the last branch name: $! -- assuming 'master'\n";
313 $last_branch = "master";
314 }
315 $orig_branch = $last_branch;
316 $last_rev = get_headref($orig_branch, $git_dir);
317 if (-f "$git_dir/SVN2GIT_HEAD") {
318 die <<EOM;
319SVN2GIT_HEAD exists.
320Make sure your working directory corresponds to HEAD and remove SVN2GIT_HEAD.
321You may need to run
322
323 git-read-tree -m -u SVN2GIT_HEAD HEAD
324EOM
325 }
326 system('cp', "$git_dir/HEAD", "$git_dir/SVN2GIT_HEAD");
327
328 $forward_master =
329 $opt_o ne 'master' && -f "$git_dir/refs/heads/master" &&
29504118 330 system('cmp', '-s', "$git_dir/refs/heads/master",
eaf718f3
MU
331 "$git_dir/refs/heads/$opt_o") == 0;
332
333 # populate index
3ca93642 334 system('git', 'read-tree', $last_rev);
eaf718f3
MU
335 die "read-tree failed: $?\n" if $?;
336
337 # Get the last import timestamps
338 open my $B,"<", "$git_dir/svn2git";
339 while(<$B>) {
340 chomp;
341 my($num,$branch,$ref) = split;
342 $branches{$branch}{$num} = $ref;
343 $branches{$branch}{"LAST"} = $ref;
03490804 344 $current_rev = $num+1 if $current_rev <= $num;
eaf718f3
MU
345 }
346 close($B);
347}
348-d $git_dir
349 or die "Could not create git subdir ($git_dir).\n";
350
d3cac2c9
KW
351my $default_authors = "$git_dir/svn-authors";
352if ($opt_A) {
353 read_users($opt_A);
354 copy($opt_A,$default_authors) or die "Copy failed: $!";
355} else {
356 read_users($default_authors) if -f $default_authors;
357}
358
eaf718f3
MU
359open BRANCHES,">>", "$git_dir/svn2git";
360
83936a29
SK
361sub node_kind($$) {
362 my ($svnpath, $revision) = @_;
09c3a408 363 $svnpath =~ s#^/*##;
5d17d765
SS
364 my $subpool = SVN::Pool::new_default_sub;
365 my $kind = $svn->{'svn'}->check_path($svnpath,$revision);
cbce5d89
YAS
366 return $kind;
367}
368
cbce5d89 369sub get_file($$$) {
83936a29 370 my($svnpath,$rev,$path) = @_;
cbce5d89 371
eaf718f3 372 # now get it
4802426d 373 my ($name,$mode);
25f6f325
MU
374 if($opt_d) {
375 my($req,$res);
376
377 # /svn/!svn/bc/2/django/trunk/django-docs/build.py
378 my $url=$svn_url->clone();
379 $url->path($url->path."/!svn/bc/$rev/$svn_dir$svnpath");
0090a618 380 print "... $path...\n" if $opt_v;
25f6f325
MU
381 $req = HTTP::Request->new(GET => $url);
382 $res = $lwp_ua->request($req);
383 if ($res->is_success) {
384 my $fh;
29504118
JH
385 ($fh, $name) = tempfile('gitsvn.XXXXXX',
386 DIR => File::Spec->tmpdir(), UNLINK => 1);
25f6f325
MU
387 print $fh $res->content;
388 close($fh) or die "Could not write $name: $!\n";
389 } else {
390 return undef if $res->code == 301; # directory?
391 die $res->status_line." at $url\n";
392 }
4802426d 393 $mode = '0644'; # can't obtain mode via direct http request?
25f6f325 394 } else {
4802426d 395 ($name,$mode) = $svn->file("$svnpath",$rev);
25f6f325
MU
396 return undef unless defined $name;
397 }
eaf718f3 398
7ae0dc01
JH
399 my $pid = open(my $F, '-|');
400 die $! unless defined $pid;
401 if (!$pid) {
3ca93642 402 exec("git", "hash-object", "-w", $name)
eaf718f3 403 or die "Cannot create object: $!\n";
7ae0dc01 404 }
eaf718f3
MU
405 my $sha = <$F>;
406 chomp $sha;
407 close $F;
22dcbb75 408 unlink $name;
eaf718f3
MU
409 return [$mode, $sha, $path];
410}
411
c55f3fff 412sub get_ignore($$$$$) {
83936a29 413 my($new,$old,$rev,$path,$svnpath) = @_;
c55f3fff
KW
414
415 return unless $opt_I;
c55f3fff
KW
416 my $name = $svn->ignore("$svnpath",$rev);
417 if ($path eq '/') {
418 $path = $opt_I;
419 } else {
420 $path = File::Spec->catfile($path,$opt_I);
421 }
422 if (defined $name) {
423 my $pid = open(my $F, '-|');
424 die $! unless defined $pid;
425 if (!$pid) {
3ca93642 426 exec("git", "hash-object", "-w", $name)
c55f3fff
KW
427 or die "Cannot create object: $!\n";
428 }
429 my $sha = <$F>;
430 chomp $sha;
431 close $F;
432 unlink $name;
433 push(@$new,['0644',$sha,$path]);
83936a29 434 } elsif (defined $old) {
c55f3fff
KW
435 push(@$old,$path);
436 }
437}
438
ec1e4689
SK
439sub project_path($$)
440{
441 my ($path, $project) = @_;
442
443 $path = "/".$path unless ($path =~ m#^\/#) ;
444 return $1 if ($path =~ m#^$project\/(.*)$#);
445
446 $path =~ s#\.#\\\.#g;
447 $path =~ s#\+#\\\+#g;
448 return "/" if ($project =~ m#^$path.*$#);
449
450 return undef;
451}
452
eaf718f3
MU
453sub split_path($$) {
454 my($rev,$path) = @_;
455 my $branch;
456
457 if($path =~ s#^/\Q$tag_name\E/([^/]+)/?##) {
458 $branch = "/$1";
459 } elsif($path =~ s#^/\Q$trunk_name\E/?##) {
460 $branch = "/";
461 } elsif($path =~ s#^/\Q$branch_name\E/([^/]+)/?##) {
462 $branch = $1;
463 } else {
c2c07a5c
YAS
464 my %no_error = (
465 "/" => 1,
466 "/$tag_name" => 1,
467 "/$branch_name" => 1
468 );
469 print STDERR "$rev: Unrecognized path: $path\n" unless (defined $no_error{$path});
eaf718f3
MU
470 return ()
471 }
ec1e4689
SK
472 if ($path eq "") {
473 $path = "/";
474 } elsif ($project_name) {
475 $path = project_path($path, $project_name);
476 }
eaf718f3
MU
477 return ($branch,$path);
478}
479
8168373f
YAS
480sub branch_rev($$) {
481
482 my ($srcbranch,$uptorev) = @_;
483
484 my $bbranches = $branches{$srcbranch};
485 my @revs = reverse sort { ($a eq 'LAST' ? 0 : $a) <=> ($b eq 'LAST' ? 0 : $b) } keys %$bbranches;
486 my $therev;
487 foreach my $arev(@revs) {
488 next if ($arev eq 'LAST');
489 if ($arev <= $uptorev) {
490 $therev = $arev;
491 last;
492 }
493 }
494 return $therev;
495}
496
83936a29
SK
497sub expand_svndir($$$);
498
499sub expand_svndir($$$)
500{
501 my ($svnpath, $rev, $path) = @_;
502 my @list;
503 get_ignore(\@list, undef, $rev, $path, $svnpath);
504 my $dirents = $svn->dir_list($svnpath, $rev);
505 foreach my $p(keys %$dirents) {
506 my $kind = node_kind($svnpath.'/'.$p, $rev);
507 if ($kind eq $SVN::Node::file) {
508 my $f = get_file($svnpath.'/'.$p, $rev, $path.'/'.$p);
509 push(@list, $f) if $f;
510 } elsif ($kind eq $SVN::Node::dir) {
511 push(@list,
512 expand_svndir($svnpath.'/'.$p, $rev, $path.'/'.$p));
513 }
514 }
515 return @list;
516}
517
109fc2b9 518sub copy_path($$$$$$$$) {
0090a618
MU
519 # Somebody copied a whole subdirectory.
520 # We need to find the index entries from the old version which the
521 # SVN log entry points to, and add them to the new place.
522
109fc2b9 523 my($newrev,$newbranch,$path,$oldpath,$rev,$node_kind,$new,$parents) = @_;
0090a618 524
8168373f 525 my($srcbranch,$srcpath) = split_path($rev,$oldpath);
83936a29
SK
526 unless(defined $srcbranch && defined $srcpath) {
527 print "Path not found when copying from $oldpath @ $rev.\n".
528 "Will try to copy from original SVN location...\n"
529 if $opt_v;
530 push (@$new, expand_svndir($oldpath, $rev, $path));
4b1ca25e
MU
531 return;
532 }
8168373f
YAS
533 my $therev = branch_rev($srcbranch, $rev);
534 my $gitrev = $branches{$srcbranch}{$therev};
0090a618
MU
535 unless($gitrev) {
536 print STDERR "$newrev:$newbranch: could not find $oldpath \@ $rev\n";
537 return;
538 }
109fc2b9
YAS
539 if ($srcbranch ne $newbranch) {
540 push(@$parents, $branches{$srcbranch}{'LAST'});
541 }
8168373f
YAS
542 print "$newrev:$newbranch:$path: copying from $srcbranch:$srcpath @ $rev\n" if $opt_v;
543 if ($node_kind eq $SVN::Node::dir) {
83936a29 544 $srcpath =~ s#/*$#/#;
8168373f 545 }
a6080a0a 546
7ae0dc01
JH
547 my $pid = open my $f,'-|';
548 die $! unless defined $pid;
549 if (!$pid) {
3ca93642 550 exec("git","ls-tree","-r","-z",$gitrev,$srcpath)
7ae0dc01
JH
551 or die $!;
552 }
0090a618
MU
553 local $/ = "\0";
554 while(<$f>) {
555 chomp;
556 my($m,$p) = split(/\t/,$_,2);
557 my($mode,$type,$sha1) = split(/ /,$m);
558 next if $type ne "blob";
8168373f
YAS
559 if ($node_kind eq $SVN::Node::dir) {
560 $p = $path . substr($p,length($srcpath)-1);
561 } else {
562 $p = $path;
563 }
a6080a0a 564 push(@$new,[$mode,$sha1,$p]);
0090a618 565 }
29504118 566 close($f) or
0090a618
MU
567 print STDERR "$newrev:$newbranch: could not list files in $oldpath \@ $rev\n";
568}
569
eaf718f3
MU
570sub commit {
571 my($branch, $changed_paths, $revision, $author, $date, $message) = @_;
ae35b304
SK
572 my($committer_name,$committer_email,$dest);
573 my($author_name,$author_email);
109fc2b9 574 my(@old,@new,@parents);
eaf718f3 575
35c636ec 576 if (not defined $author or $author eq "") {
ae35b304 577 $committer_name = $committer_email = "unknown";
d3cac2c9
KW
578 } elsif (defined $users_file) {
579 die "User $author is not listed in $users_file\n"
36610b24 580 unless exists $users{$author};
ae35b304 581 ($committer_name,$committer_email) = @{$users{$author}};
2b5e63d1 582 } elsif ($author =~ /^(.*?)\s+<(.*)>$/) {
ae35b304 583 ($committer_name, $committer_email) = ($1, $2);
eaf718f3
MU
584 } else {
585 $author =~ s/^<(.*)>$/$1/;
ae35b304
SK
586 $committer_name = $committer_email = $author;
587 }
588
7b40e7d1 589 if ($opt_F && $message =~ /From:\s+(.*?)\s+<(.*)>\s*\n/) {
ae35b304 590 ($author_name, $author_email) = ($1, $2);
7b40e7d1
AW
591 print "Author from From: $1 <$2>\n" if ($opt_v);;
592 } elsif ($opt_S && $message =~ /Signed-off-by:\s+(.*?)\s+<(.*)>\s*\n/) {
593 ($author_name, $author_email) = ($1, $2);
594 print "Author from Signed-off-by: $1 <$2>\n" if ($opt_v);;
ae35b304
SK
595 } else {
596 $author_name = $committer_name;
597 $author_email = $committer_email;
eaf718f3 598 }
ae35b304 599
eaf718f3
MU
600 $date = pdate($date);
601
602 my $tag;
603 my $parent;
604 if($branch eq "/") { # trunk
605 $parent = $opt_o;
606 } elsif($branch =~ m#^/(.+)#) { # tag
607 $tag = 1;
608 $parent = $1;
609 } else { # "normal" branch
610 # nothing to do
611 $parent = $branch;
612 }
613 $dest = $parent;
614
615 my $prev = $changed_paths->{"/"};
f0daa628 616 if($prev and $prev->[0] eq "A") {
eaf718f3 617 delete $changed_paths->{"/"};
f0daa628 618 my $oldpath = $prev->[1];
eaf718f3
MU
619 my $rev;
620 if(defined $oldpath) {
621 my $p;
622 ($parent,$p) = split_path($revision,$oldpath);
83936a29
SK
623 if(defined $parent) {
624 if($parent eq "/") {
625 $parent = $opt_o;
626 } else {
627 $parent =~ s#^/##; # if it's a tag
628 }
eaf718f3
MU
629 }
630 } else {
631 $parent = undef;
632 }
633 }
634
635 my $rev;
7ee74a99 636 if($revision > $opt_s and defined $parent) {
3ca93642 637 open(H,'-|',"git","rev-parse","--verify",$parent);
eaf718f3
MU
638 $rev = <H>;
639 close(H) or do {
640 print STDERR "$revision: cannot find commit '$parent'!\n";
641 return;
642 };
643 chop $rev;
644 if(length($rev) != 40) {
645 print STDERR "$revision: cannot find commit '$parent'!\n";
646 return;
647 }
648 $rev = $branches{($parent eq $opt_o) ? "/" : $parent}{"LAST"};
7ee74a99 649 if($revision != $opt_s and not $rev) {
eaf718f3
MU
650 print STDERR "$revision: do not know ancestor for '$parent'!\n";
651 return;
652 }
653 } else {
654 $rev = undef;
655 }
656
f0daa628 657# if($prev and $prev->[0] eq "A") {
eaf718f3
MU
658# if(not $tag) {
659# unless(open(H,"> $git_dir/refs/heads/$branch")) {
660# print STDERR "$revision: Could not create branch $branch: $!\n";
661# $state=11;
662# next;
663# }
664# print H "$rev\n"
665# or die "Could not write branch $branch: $!";
666# close(H)
667# or die "Could not write branch $branch: $!";
668# }
669# }
670 if(not defined $rev) {
671 unlink($git_index);
672 } elsif ($rev ne $last_rev) {
673 print "Switching from $last_rev to $rev ($branch)\n" if $opt_v;
3ca93642 674 system("git", "read-tree", $rev);
eaf718f3
MU
675 die "read-tree failed for $rev: $?\n" if $?;
676 $last_rev = $rev;
677 }
678
109fc2b9
YAS
679 push (@parents, $rev) if defined $rev;
680
bf267d99
MU
681 my $cid;
682 if($tag and not %$changed_paths) {
683 $cid = $rev;
684 } else {
0090a618
MU
685 my @paths = sort keys %$changed_paths;
686 foreach my $path(@paths) {
687 my $action = $changed_paths->{$path};
688
8168373f
YAS
689 if ($action->[0] eq "R") {
690 # refer to a file/tree in an earlier commit
691 push(@old,$path); # remove any old stuff
692 }
693 if(($action->[0] eq "A") || ($action->[0] eq "R")) {
83936a29 694 my $node_kind = node_kind($action->[3], $revision);
e67c6625 695 if ($node_kind eq $SVN::Node::file) {
83936a29
SK
696 my $f = get_file($action->[3],
697 $revision, $path);
8168373f
YAS
698 if ($f) {
699 push(@new,$f) if $f;
700 } else {
701 my $opath = $action->[3];
702 print STDERR "$revision: $branch: could not fetch '$opath'\n";
703 }
c55f3fff 704 } elsif ($node_kind eq $SVN::Node::dir) {
e67c6625
KW
705 if($action->[1]) {
706 copy_path($revision, $branch,
707 $path, $action->[1],
708 $action->[2], $node_kind,
709 \@new, \@parents);
710 } else {
711 get_ignore(\@new, \@old, $revision,
83936a29 712 $path, $action->[3]);
e67c6625 713 }
0090a618 714 }
bf267d99
MU
715 } elsif ($action->[0] eq "D") {
716 push(@old,$path);
717 } elsif ($action->[0] eq "M") {
83936a29 718 my $node_kind = node_kind($action->[3], $revision);
8168373f 719 if ($node_kind eq $SVN::Node::file) {
83936a29
SK
720 my $f = get_file($action->[3],
721 $revision, $path);
8168373f 722 push(@new,$f) if $f;
c55f3fff
KW
723 } elsif ($node_kind eq $SVN::Node::dir) {
724 get_ignore(\@new, \@old, $revision,
83936a29 725 $path, $action->[3]);
bf267d99
MU
726 }
727 } else {
728 die "$revision: unknown action '".$action->[0]."' for $path\n";
729 }
730 }
731
d9e2e127
SK
732 while(@old) {
733 my @o1;
734 if(@old > 55) {
735 @o1 = splice(@old,0,50);
736 } else {
737 @o1 = @old;
738 @old = ();
739 }
7ae0dc01
JH
740 my $pid = open my $F, "-|";
741 die "$!" unless defined $pid;
742 if (!$pid) {
3ca93642 743 exec("git", "ls-files", "-z", @o1) or die $!;
7ae0dc01 744 }
d9e2e127 745 @o1 = ();
0090a618 746 local $/ = "\0";
eaf718f3
MU
747 while(<$F>) {
748 chomp;
d9e2e127 749 push(@o1,$_);
bf267d99
MU
750 }
751 close($F);
752
d9e2e127 753 while(@o1) {
bf267d99 754 my @o2;
d9e2e127
SK
755 if(@o1 > 55) {
756 @o2 = splice(@o1,0,50);
bf267d99 757 } else {
d9e2e127
SK
758 @o2 = @o1;
759 @o1 = ();
bf267d99 760 }
3ca93642 761 system("git","update-index","--force-remove","--",@o2);
bf267d99 762 die "Cannot remove files: $?\n" if $?;
eaf718f3 763 }
eaf718f3 764 }
bf267d99
MU
765 while(@new) {
766 my @n2;
767 if(@new > 12) {
768 @n2 = splice(@new,0,10);
eaf718f3 769 } else {
bf267d99
MU
770 @n2 = @new;
771 @new = ();
eaf718f3 772 }
3ca93642 773 system("git","update-index","--add",
bf267d99
MU
774 (map { ('--cacheinfo', @$_) } @n2));
775 die "Cannot add files: $?\n" if $?;
eaf718f3 776 }
eaf718f3 777
bf267d99
MU
778 my $pid = open(C,"-|");
779 die "Cannot fork: $!" unless defined $pid;
780 unless($pid) {
3ca93642 781 exec("git","write-tree");
bf267d99 782 die "Cannot exec git-write-tree: $!\n";
eaf718f3 783 }
bf267d99
MU
784 chomp(my $tree = <C>);
785 length($tree) == 40
786 or die "Cannot get tree id ($tree): $!\n";
787 close(C)
788 or die "Error running git-write-tree: $?\n";
789 print "Tree ID $tree\n" if $opt_v;
790
791 my $pr = IO::Pipe->new() or die "Cannot open pipe: $!\n";
792 my $pw = IO::Pipe->new() or die "Cannot open pipe: $!\n";
793 $pid = fork();
794 die "Fork: $!\n" unless defined $pid;
795 unless($pid) {
796 $pr->writer();
797 $pw->reader();
798 open(OUT,">&STDOUT");
799 dup2($pw->fileno(),0);
800 dup2($pr->fileno(),1);
801 $pr->close();
802 $pw->close();
803
804 my @par = ();
bf267d99
MU
805
806 # loose detection of merges
807 # based on the commit msg
808 foreach my $rx (@mergerx) {
809 if ($message =~ $rx) {
810 my $mparent = $1;
811 if ($mparent eq 'HEAD') { $mparent = $opt_o };
812 if ( -e "$git_dir/refs/heads/$mparent") {
813 $mparent = get_headref($mparent, $git_dir);
109fc2b9 814 push (@parents, $mparent);
bf267d99
MU
815 print OUT "Merge parent branch: $mparent\n" if $opt_v;
816 }
29504118 817 }
bf267d99 818 }
109fc2b9
YAS
819 my %seen_parents = ();
820 my @unique_parents = grep { ! $seen_parents{$_} ++ } @parents;
821 foreach my $bparent (@unique_parents) {
822 push @par, '-p', $bparent;
823 print OUT "Merge parent branch: $bparent\n" if $opt_v;
824 }
eaf718f3 825
bf267d99
MU
826 exec("env",
827 "GIT_AUTHOR_NAME=$author_name",
828 "GIT_AUTHOR_EMAIL=$author_email",
829 "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
ae35b304
SK
830 "GIT_COMMITTER_NAME=$committer_name",
831 "GIT_COMMITTER_EMAIL=$committer_email",
bf267d99 832 "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
3ca93642 833 "git", "commit-tree", $tree,@par);
bf267d99
MU
834 die "Cannot exec git-commit-tree: $!\n";
835 }
836 $pw->writer();
837 $pr->reader();
eaf718f3 838
bf267d99 839 $message =~ s/[\s\n]+\z//;
0a48a344 840 $message = "r$revision: $message" if $opt_r;
eaf718f3 841
bf267d99
MU
842 print $pw "$message\n"
843 or die "Error writing to git-commit-tree: $!\n";
844 $pw->close();
eaf718f3 845
bf267d99
MU
846 print "Committed change $revision:$branch ".strftime("%Y-%m-%d %H:%M:%S",gmtime($date)).")\n" if $opt_v;
847 chomp($cid = <$pr>);
848 length($cid) == 40
849 or die "Cannot get commit id ($cid): $!\n";
850 print "Commit ID $cid\n" if $opt_v;
851 $pr->close();
eaf718f3 852
bf267d99
MU
853 waitpid($pid,0);
854 die "Error running git-commit-tree: $?\n" if $?;
855 }
eaf718f3 856
a16db4f4
YAS
857 if (not defined $cid) {
858 $cid = $branches{"/"}{"LAST"};
859 }
860
f02b3eba
MU
861 if(not defined $dest) {
862 print "... no known parent\n" if $opt_v;
863 } elsif(not $tag) {
eaf718f3 864 print "Writing to refs/heads/$dest\n" if $opt_v;
29504118 865 open(C,">$git_dir/refs/heads/$dest") and
eaf718f3
MU
866 print C ("$cid\n") and
867 close(C)
868 or die "Cannot write branch $dest for update: $!\n";
eaf718f3 869 }
eaf718f3 870
47ee8ed2 871 if ($tag) {
eaf718f3
MU
872 $last_rev = "-" if %$changed_paths;
873 # the tag was 'complex', i.e. did not refer to a "real" revision
29504118 874
f02b3eba 875 $dest =~ tr/_/\./ if $opt_u;
eaf718f3 876
3ca93642 877 system('git', 'tag', '-f', $dest, $cid) == 0
47ee8ed2 878 or die "Cannot create tag $dest: $!\n";
eaf718f3 879
f02b3eba 880 print "Created tag '$dest' on '$branch'\n" if $opt_v;
eaf718f3 881 }
e7e477df
MU
882 $branches{$branch}{"LAST"} = $cid;
883 $branches{$branch}{$revision} = $cid;
884 $last_rev = $cid;
885 print BRANCHES "$revision $branch $cid\n";
886 print "DONE: $revision $dest $cid\n" if $opt_v;
eaf718f3
MU
887}
888
03490804
MU
889sub commit_all {
890 # Recursive use of the SVN connection does not work
891 local $svn = $svn2;
892
5d17d765 893 my ($changed_paths, $revision, $author, $date, $message) = @_;
f0daa628
MU
894 my %p;
895 while(my($path,$action) = each %$changed_paths) {
0090a618 896 $p{$path} = [ $action->action,$action->copyfrom_path, $action->copyfrom_rev, $path ];
f0daa628
MU
897 }
898 $changed_paths = \%p;
f0daa628 899
eaf718f3
MU
900 my %done;
901 my @col;
902 my $pref;
903 my $branch;
904
905 while(my($path,$action) = each %$changed_paths) {
906 ($branch,$path) = split_path($revision,$path);
907 next if not defined $branch;
ec1e4689 908 next if not defined $path;
eaf718f3
MU
909 $done{$branch}{$path} = $action;
910 }
911 while(($branch,$changed_paths) = each %done) {
912 commit($branch, $changed_paths, $revision, $author, $date, $message);
913 }
914}
915
03490804 916$opt_l = $svn->{'maxrev'} if not defined $opt_l or $opt_l > $svn->{'maxrev'};
988eece4 917
a7cfb4a4 918if ($opt_l < $current_rev) {
988eece4
ML
919 print "Up to date: no new revisions to fetch!\n" if $opt_v;
920 unlink("$git_dir/SVN2GIT_HEAD");
921 exit;
922}
923
40006ea0 924print "Processing from $current_rev to $opt_l ...\n" if $opt_v;
03490804 925
40006ea0 926my $from_rev;
69216777 927my $to_rev = $current_rev - 1;
40006ea0 928
5d17d765 929my $subpool = SVN::Pool::new_default_sub;
40006ea0 930while ($to_rev < $opt_l) {
5d17d765 931 $subpool->clear;
69216777 932 $from_rev = $to_rev + 1;
40006ea0
SK
933 $to_rev = $from_rev + $repack_after;
934 $to_rev = $opt_l if $opt_l < $to_rev;
935 print "Fetching from $from_rev to $to_rev ...\n" if $opt_v;
e8a43a13 936 $svn->{'svn'}->get_log("",$from_rev,$to_rev,0,1,1,\&commit_all);
40006ea0
SK
937 my $pid = fork();
938 die "Fork: $!\n" unless defined $pid;
939 unless($pid) {
3ca93642 940 exec("git", "repack", "-d")
40006ea0
SK
941 or die "Cannot repack: $!\n";
942 }
943 waitpid($pid, 0);
944}
eaf718f3
MU
945
946
947unlink($git_index);
948
949if (defined $orig_git_index) {
950 $ENV{GIT_INDEX_FILE} = $orig_git_index;
951} else {
952 delete $ENV{GIT_INDEX_FILE};
953}
954
955# Now switch back to the branch we were in before all of this happened
956if($orig_branch) {
3ef378a6 957 print "DONE\n" if $opt_v and (not defined $opt_l or $opt_l > 0);
eaf718f3
MU
958 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
959 if $forward_master;
960 unless ($opt_i) {
3ca93642 961 system('git', 'read-tree', '-m', '-u', 'SVN2GIT_HEAD', 'HEAD');
eaf718f3
MU
962 die "read-tree failed: $?\n" if $?;
963 }
964} else {
965 $orig_branch = "master";
3ef378a6 966 print "DONE; creating $orig_branch branch\n" if $opt_v and (not defined $opt_l or $opt_l > 0);
eaf718f3
MU
967 system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master")
968 unless -f "$git_dir/refs/heads/master";
3ca93642 969 system('git', 'update-ref', 'HEAD', "$orig_branch");
eaf718f3
MU
970 unless ($opt_i) {
971 system('git checkout');
972 die "checkout failed: $?\n" if $?;
973 }
974}
975unlink("$git_dir/SVN2GIT_HEAD");
976close(BRANCHES);