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