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