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