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