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