]>
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 | ||
f3ad0625 | 28 | die "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 | 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; | |
f3ad0625 | 37 | Usage: ${\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 |
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; |
eaf718f3 MU |
56 | my $git_tree = $opt_C; |
57 | $git_tree ||= "."; | |
58 | ||
4a91b796 | 59 | my $svn_url = $ARGV[0]; |
25f6f325 | 60 | my $svn_dir = $ARGV[1]; |
eaf718f3 MU |
61 | |
62 | our @mergerx = (); | |
63 | if ($opt_m) { | |
64 | @mergerx = ( qr/\W(?:from|of|merge|merging|merged) (\w+)/i ); | |
65 | } | |
66 | if ($opt_M) { | |
67 | push (@mergerx, qr/$opt_M/); | |
68 | } | |
69 | ||
70 | select(STDERR); $|=1; select(STDOUT); | |
71 | ||
72 | ||
73 | package SVNconn; | |
74 | # Basic SVN connection. | |
75 | # We're only interested in connecting and downloading, so ... | |
76 | ||
77 | use File::Spec; | |
78 | use File::Temp qw(tempfile); | |
79 | use POSIX qw(strftime dup2); | |
80 | ||
81 | sub 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 | ||
96 | sub 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 | ||
107 | sub 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 | 126 | package main; |
25f6f325 | 127 | use URI; |
eaf718f3 | 128 | |
25f6f325 MU |
129 | my $svn = $svn_url; |
130 | $svn .= "/$svn_dir" if defined $svn_dir; | |
131 | $svn = SVNconn->new($svn); | |
eaf718f3 | 132 | |
25f6f325 MU |
133 | my $lwp_ua; |
134 | if($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 | |
150 | sub 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 |
158 | sub getwd() { |
159 | my $pwd = `pwd`; | |
160 | chomp $pwd; | |
161 | return $pwd; | |
162 | } | |
163 | ||
164 | ||
165 | sub 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: $!"; | |
183 | chdir($git_tree); | |
184 | ||
185 | my $orig_branch = ""; | |
186 | my $forward_master = 0; | |
187 | my %branches; | |
188 | ||
189 | my $git_dir = $ENV{"GIT_DIR"} || ".git"; | |
190 | $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#; | |
191 | $ENV{"GIT_DIR"} = $git_dir; | |
192 | my $orig_git_index; | |
193 | $orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE}; | |
194 | my ($git_ih, $git_index) = tempfile('gitXXXXXX', SUFFIX => '.idx', | |
195 | DIR => File::Spec->tmpdir()); | |
196 | close ($git_ih); | |
197 | $ENV{GIT_INDEX_FILE} = $git_index; | |
198 | my $maxnum = 0; | |
199 | my $last_rev = ""; | |
200 | my $last_branch; | |
2fa92046 | 201 | my $current_rev = $opt_s-1; |
eaf718f3 MU |
202 | unless(-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; | |
228 | SVN2GIT_HEAD exists. | |
229 | Make sure your working directory corresponds to HEAD and remove SVN2GIT_HEAD. | |
230 | You may need to run | |
231 | ||
232 | git-read-tree -m -u SVN2GIT_HEAD HEAD | |
233 | EOM | |
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 | ||
260 | open BRANCHES,">>", "$git_dir/svn2git"; | |
261 | ||
cbce5d89 YAS |
262 | sub 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 | ||
270 | sub 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 | ||
4b1ca25e MU |
283 | $svnpath =~ s#/+$##; |
284 | return $svnpath; | |
cbce5d89 YAS |
285 | } |
286 | ||
287 | sub get_file($$$) { | |
288 | my($rev,$branch,$path) = @_; | |
289 | ||
290 | my $svnpath = revert_split_path($branch,$path); | |
291 | ||
eaf718f3 | 292 | # now get it |
25f6f325 MU |
293 | my $name; |
294 | if($opt_d) { | |
295 | my($req,$res); | |
296 | ||
297 | # /svn/!svn/bc/2/django/trunk/django-docs/build.py | |
298 | my $url=$svn_url->clone(); | |
299 | $url->path($url->path."/!svn/bc/$rev/$svn_dir$svnpath"); | |
0090a618 | 300 | print "... $path...\n" if $opt_v; |
25f6f325 MU |
301 | $req = HTTP::Request->new(GET => $url); |
302 | $res = $lwp_ua->request($req); | |
303 | if ($res->is_success) { | |
304 | my $fh; | |
29504118 JH |
305 | ($fh, $name) = tempfile('gitsvn.XXXXXX', |
306 | DIR => File::Spec->tmpdir(), UNLINK => 1); | |
25f6f325 MU |
307 | print $fh $res->content; |
308 | close($fh) or die "Could not write $name: $!\n"; | |
309 | } else { | |
310 | return undef if $res->code == 301; # directory? | |
311 | die $res->status_line." at $url\n"; | |
312 | } | |
313 | } else { | |
0090a618 | 314 | $name = $svn->file("/$svnpath",$rev); |
25f6f325 MU |
315 | return undef unless defined $name; |
316 | } | |
eaf718f3 | 317 | |
6d281217 | 318 | open my $F, '-|', "git-hash-object", "-w", $name |
eaf718f3 MU |
319 | or die "Cannot create object: $!\n"; |
320 | my $sha = <$F>; | |
321 | chomp $sha; | |
322 | close $F; | |
22dcbb75 | 323 | unlink $name; |
eaf718f3 MU |
324 | my $mode = "0644"; # SV does not seem to store any file modes |
325 | return [$mode, $sha, $path]; | |
326 | } | |
327 | ||
328 | sub split_path($$) { | |
329 | my($rev,$path) = @_; | |
330 | my $branch; | |
331 | ||
332 | if($path =~ s#^/\Q$tag_name\E/([^/]+)/?##) { | |
333 | $branch = "/$1"; | |
334 | } elsif($path =~ s#^/\Q$trunk_name\E/?##) { | |
335 | $branch = "/"; | |
336 | } elsif($path =~ s#^/\Q$branch_name\E/([^/]+)/?##) { | |
337 | $branch = $1; | |
338 | } else { | |
c2c07a5c YAS |
339 | my %no_error = ( |
340 | "/" => 1, | |
341 | "/$tag_name" => 1, | |
342 | "/$branch_name" => 1 | |
343 | ); | |
344 | print STDERR "$rev: Unrecognized path: $path\n" unless (defined $no_error{$path}); | |
eaf718f3 MU |
345 | return () |
346 | } | |
347 | $path = "/" if $path eq ""; | |
348 | return ($branch,$path); | |
349 | } | |
350 | ||
8168373f YAS |
351 | sub branch_rev($$) { |
352 | ||
353 | my ($srcbranch,$uptorev) = @_; | |
354 | ||
355 | my $bbranches = $branches{$srcbranch}; | |
356 | my @revs = reverse sort { ($a eq 'LAST' ? 0 : $a) <=> ($b eq 'LAST' ? 0 : $b) } keys %$bbranches; | |
357 | my $therev; | |
358 | foreach my $arev(@revs) { | |
359 | next if ($arev eq 'LAST'); | |
360 | if ($arev <= $uptorev) { | |
361 | $therev = $arev; | |
362 | last; | |
363 | } | |
364 | } | |
365 | return $therev; | |
366 | } | |
367 | ||
109fc2b9 | 368 | sub copy_path($$$$$$$$) { |
0090a618 MU |
369 | # Somebody copied a whole subdirectory. |
370 | # We need to find the index entries from the old version which the | |
371 | # SVN log entry points to, and add them to the new place. | |
372 | ||
109fc2b9 | 373 | my($newrev,$newbranch,$path,$oldpath,$rev,$node_kind,$new,$parents) = @_; |
0090a618 | 374 | |
8168373f | 375 | my($srcbranch,$srcpath) = split_path($rev,$oldpath); |
4b1ca25e MU |
376 | unless(defined $srcbranch) { |
377 | print "Path not found when copying from $oldpath @ $rev\n"; | |
378 | return; | |
379 | } | |
8168373f YAS |
380 | my $therev = branch_rev($srcbranch, $rev); |
381 | my $gitrev = $branches{$srcbranch}{$therev}; | |
0090a618 MU |
382 | unless($gitrev) { |
383 | print STDERR "$newrev:$newbranch: could not find $oldpath \@ $rev\n"; | |
384 | return; | |
385 | } | |
109fc2b9 YAS |
386 | if ($srcbranch ne $newbranch) { |
387 | push(@$parents, $branches{$srcbranch}{'LAST'}); | |
388 | } | |
8168373f YAS |
389 | print "$newrev:$newbranch:$path: copying from $srcbranch:$srcpath @ $rev\n" if $opt_v; |
390 | if ($node_kind eq $SVN::Node::dir) { | |
391 | $srcpath =~ s#/*$#/#; | |
392 | } | |
393 | ||
0090a618 MU |
394 | open my $f,"-|","git-ls-tree","-r","-z",$gitrev,$srcpath; |
395 | local $/ = "\0"; | |
396 | while(<$f>) { | |
397 | chomp; | |
398 | my($m,$p) = split(/\t/,$_,2); | |
399 | my($mode,$type,$sha1) = split(/ /,$m); | |
400 | next if $type ne "blob"; | |
8168373f YAS |
401 | if ($node_kind eq $SVN::Node::dir) { |
402 | $p = $path . substr($p,length($srcpath)-1); | |
403 | } else { | |
404 | $p = $path; | |
405 | } | |
406 | push(@$new,[$mode,$sha1,$p]); | |
0090a618 | 407 | } |
29504118 | 408 | close($f) or |
0090a618 MU |
409 | print STDERR "$newrev:$newbranch: could not list files in $oldpath \@ $rev\n"; |
410 | } | |
411 | ||
eaf718f3 MU |
412 | sub commit { |
413 | my($branch, $changed_paths, $revision, $author, $date, $message) = @_; | |
414 | my($author_name,$author_email,$dest); | |
109fc2b9 | 415 | my(@old,@new,@parents); |
eaf718f3 | 416 | |
2b5e63d1 MU |
417 | if (not defined $author) { |
418 | $author_name = $author_email = "unknown"; | |
419 | } elsif ($author =~ /^(.*?)\s+<(.*)>$/) { | |
eaf718f3 MU |
420 | ($author_name, $author_email) = ($1, $2); |
421 | } else { | |
422 | $author =~ s/^<(.*)>$/$1/; | |
423 | $author_name = $author_email = $author; | |
424 | } | |
425 | $date = pdate($date); | |
426 | ||
427 | my $tag; | |
428 | my $parent; | |
429 | if($branch eq "/") { # trunk | |
430 | $parent = $opt_o; | |
431 | } elsif($branch =~ m#^/(.+)#) { # tag | |
432 | $tag = 1; | |
433 | $parent = $1; | |
434 | } else { # "normal" branch | |
435 | # nothing to do | |
436 | $parent = $branch; | |
437 | } | |
438 | $dest = $parent; | |
439 | ||
440 | my $prev = $changed_paths->{"/"}; | |
f0daa628 | 441 | if($prev and $prev->[0] eq "A") { |
eaf718f3 | 442 | delete $changed_paths->{"/"}; |
f0daa628 | 443 | my $oldpath = $prev->[1]; |
eaf718f3 MU |
444 | my $rev; |
445 | if(defined $oldpath) { | |
446 | my $p; | |
447 | ($parent,$p) = split_path($revision,$oldpath); | |
448 | if($parent eq "/") { | |
449 | $parent = $opt_o; | |
450 | } else { | |
451 | $parent =~ s#^/##; # if it's a tag | |
452 | } | |
453 | } else { | |
454 | $parent = undef; | |
455 | } | |
456 | } | |
457 | ||
458 | my $rev; | |
7ee74a99 | 459 | if($revision > $opt_s and defined $parent) { |
eaf718f3 MU |
460 | open(H,"git-rev-parse --verify $parent |"); |
461 | $rev = <H>; | |
462 | close(H) or do { | |
463 | print STDERR "$revision: cannot find commit '$parent'!\n"; | |
464 | return; | |
465 | }; | |
466 | chop $rev; | |
467 | if(length($rev) != 40) { | |
468 | print STDERR "$revision: cannot find commit '$parent'!\n"; | |
469 | return; | |
470 | } | |
471 | $rev = $branches{($parent eq $opt_o) ? "/" : $parent}{"LAST"}; | |
7ee74a99 | 472 | if($revision != $opt_s and not $rev) { |
eaf718f3 MU |
473 | print STDERR "$revision: do not know ancestor for '$parent'!\n"; |
474 | return; | |
475 | } | |
476 | } else { | |
477 | $rev = undef; | |
478 | } | |
479 | ||
f0daa628 | 480 | # if($prev and $prev->[0] eq "A") { |
eaf718f3 MU |
481 | # if(not $tag) { |
482 | # unless(open(H,"> $git_dir/refs/heads/$branch")) { | |
483 | # print STDERR "$revision: Could not create branch $branch: $!\n"; | |
484 | # $state=11; | |
485 | # next; | |
486 | # } | |
487 | # print H "$rev\n" | |
488 | # or die "Could not write branch $branch: $!"; | |
489 | # close(H) | |
490 | # or die "Could not write branch $branch: $!"; | |
491 | # } | |
492 | # } | |
493 | if(not defined $rev) { | |
494 | unlink($git_index); | |
495 | } elsif ($rev ne $last_rev) { | |
496 | print "Switching from $last_rev to $rev ($branch)\n" if $opt_v; | |
497 | system("git-read-tree", $rev); | |
498 | die "read-tree failed for $rev: $?\n" if $?; | |
499 | $last_rev = $rev; | |
500 | } | |
501 | ||
109fc2b9 YAS |
502 | push (@parents, $rev) if defined $rev; |
503 | ||
bf267d99 MU |
504 | my $cid; |
505 | if($tag and not %$changed_paths) { | |
506 | $cid = $rev; | |
507 | } else { | |
0090a618 MU |
508 | my @paths = sort keys %$changed_paths; |
509 | foreach my $path(@paths) { | |
510 | my $action = $changed_paths->{$path}; | |
511 | ||
8168373f YAS |
512 | if ($action->[0] eq "R") { |
513 | # refer to a file/tree in an earlier commit | |
514 | push(@old,$path); # remove any old stuff | |
515 | } | |
516 | if(($action->[0] eq "A") || ($action->[0] eq "R")) { | |
517 | my $node_kind = node_kind($branch,$path,$revision); | |
518 | if($action->[1]) { | |
109fc2b9 | 519 | copy_path($revision,$branch,$path,$action->[1],$action->[2],$node_kind,\@new,\@parents); |
8168373f YAS |
520 | } elsif ($node_kind eq $SVN::Node::file) { |
521 | my $f = get_file($revision,$branch,$path); | |
522 | if ($f) { | |
523 | push(@new,$f) if $f; | |
524 | } else { | |
525 | my $opath = $action->[3]; | |
526 | print STDERR "$revision: $branch: could not fetch '$opath'\n"; | |
527 | } | |
0090a618 | 528 | } |
bf267d99 MU |
529 | } elsif ($action->[0] eq "D") { |
530 | push(@old,$path); | |
531 | } elsif ($action->[0] eq "M") { | |
8168373f YAS |
532 | my $node_kind = node_kind($branch,$path,$revision); |
533 | if ($node_kind eq $SVN::Node::file) { | |
534 | my $f = get_file($revision,$branch,$path); | |
535 | push(@new,$f) if $f; | |
bf267d99 MU |
536 | } |
537 | } else { | |
538 | die "$revision: unknown action '".$action->[0]."' for $path\n"; | |
539 | } | |
540 | } | |
541 | ||
542 | if(@old) { | |
543 | open my $F, "-|", "git-ls-files", "-z", @old or die $!; | |
544 | @old = (); | |
0090a618 | 545 | local $/ = "\0"; |
eaf718f3 MU |
546 | while(<$F>) { |
547 | chomp; | |
bf267d99 MU |
548 | push(@old,$_); |
549 | } | |
550 | close($F); | |
551 | ||
552 | while(@old) { | |
553 | my @o2; | |
554 | if(@old > 55) { | |
555 | @o2 = splice(@old,0,50); | |
556 | } else { | |
557 | @o2 = @old; | |
558 | @old = (); | |
559 | } | |
560 | system("git-update-index","--force-remove","--",@o2); | |
561 | die "Cannot remove files: $?\n" if $?; | |
eaf718f3 | 562 | } |
eaf718f3 | 563 | } |
bf267d99 MU |
564 | while(@new) { |
565 | my @n2; | |
566 | if(@new > 12) { | |
567 | @n2 = splice(@new,0,10); | |
eaf718f3 | 568 | } else { |
bf267d99 MU |
569 | @n2 = @new; |
570 | @new = (); | |
eaf718f3 | 571 | } |
bf267d99 MU |
572 | system("git-update-index","--add", |
573 | (map { ('--cacheinfo', @$_) } @n2)); | |
574 | die "Cannot add files: $?\n" if $?; | |
eaf718f3 | 575 | } |
eaf718f3 | 576 | |
bf267d99 MU |
577 | my $pid = open(C,"-|"); |
578 | die "Cannot fork: $!" unless defined $pid; | |
579 | unless($pid) { | |
580 | exec("git-write-tree"); | |
581 | die "Cannot exec git-write-tree: $!\n"; | |
eaf718f3 | 582 | } |
bf267d99 MU |
583 | chomp(my $tree = <C>); |
584 | length($tree) == 40 | |
585 | or die "Cannot get tree id ($tree): $!\n"; | |
586 | close(C) | |
587 | or die "Error running git-write-tree: $?\n"; | |
588 | print "Tree ID $tree\n" if $opt_v; | |
589 | ||
590 | my $pr = IO::Pipe->new() or die "Cannot open pipe: $!\n"; | |
591 | my $pw = IO::Pipe->new() or die "Cannot open pipe: $!\n"; | |
592 | $pid = fork(); | |
593 | die "Fork: $!\n" unless defined $pid; | |
594 | unless($pid) { | |
595 | $pr->writer(); | |
596 | $pw->reader(); | |
597 | open(OUT,">&STDOUT"); | |
598 | dup2($pw->fileno(),0); | |
599 | dup2($pr->fileno(),1); | |
600 | $pr->close(); | |
601 | $pw->close(); | |
602 | ||
603 | my @par = (); | |
bf267d99 MU |
604 | |
605 | # loose detection of merges | |
606 | # based on the commit msg | |
607 | foreach my $rx (@mergerx) { | |
608 | if ($message =~ $rx) { | |
609 | my $mparent = $1; | |
610 | if ($mparent eq 'HEAD') { $mparent = $opt_o }; | |
611 | if ( -e "$git_dir/refs/heads/$mparent") { | |
612 | $mparent = get_headref($mparent, $git_dir); | |
109fc2b9 | 613 | push (@parents, $mparent); |
bf267d99 MU |
614 | print OUT "Merge parent branch: $mparent\n" if $opt_v; |
615 | } | |
29504118 | 616 | } |
bf267d99 | 617 | } |
109fc2b9 YAS |
618 | my %seen_parents = (); |
619 | my @unique_parents = grep { ! $seen_parents{$_} ++ } @parents; | |
620 | foreach my $bparent (@unique_parents) { | |
621 | push @par, '-p', $bparent; | |
622 | print OUT "Merge parent branch: $bparent\n" if $opt_v; | |
623 | } | |
eaf718f3 | 624 | |
bf267d99 MU |
625 | exec("env", |
626 | "GIT_AUTHOR_NAME=$author_name", | |
627 | "GIT_AUTHOR_EMAIL=$author_email", | |
628 | "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)), | |
629 | "GIT_COMMITTER_NAME=$author_name", | |
630 | "GIT_COMMITTER_EMAIL=$author_email", | |
631 | "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)), | |
632 | "git-commit-tree", $tree,@par); | |
633 | die "Cannot exec git-commit-tree: $!\n"; | |
634 | } | |
635 | $pw->writer(); | |
636 | $pr->reader(); | |
eaf718f3 | 637 | |
bf267d99 | 638 | $message =~ s/[\s\n]+\z//; |
eaf718f3 | 639 | |
bf267d99 MU |
640 | print $pw "$message\n" |
641 | or die "Error writing to git-commit-tree: $!\n"; | |
642 | $pw->close(); | |
eaf718f3 | 643 | |
bf267d99 MU |
644 | print "Committed change $revision:$branch ".strftime("%Y-%m-%d %H:%M:%S",gmtime($date)).")\n" if $opt_v; |
645 | chomp($cid = <$pr>); | |
646 | length($cid) == 40 | |
647 | or die "Cannot get commit id ($cid): $!\n"; | |
648 | print "Commit ID $cid\n" if $opt_v; | |
649 | $pr->close(); | |
eaf718f3 | 650 | |
bf267d99 MU |
651 | waitpid($pid,0); |
652 | die "Error running git-commit-tree: $?\n" if $?; | |
653 | } | |
eaf718f3 | 654 | |
a16db4f4 YAS |
655 | if (not defined $cid) { |
656 | $cid = $branches{"/"}{"LAST"}; | |
657 | } | |
658 | ||
f02b3eba MU |
659 | if(not defined $dest) { |
660 | print "... no known parent\n" if $opt_v; | |
661 | } elsif(not $tag) { | |
eaf718f3 | 662 | print "Writing to refs/heads/$dest\n" if $opt_v; |
29504118 | 663 | open(C,">$git_dir/refs/heads/$dest") and |
eaf718f3 MU |
664 | print C ("$cid\n") and |
665 | close(C) | |
666 | or die "Cannot write branch $dest for update: $!\n"; | |
eaf718f3 | 667 | } |
eaf718f3 MU |
668 | |
669 | if($tag) { | |
670 | my($in, $out) = ('',''); | |
671 | $last_rev = "-" if %$changed_paths; | |
672 | # the tag was 'complex', i.e. did not refer to a "real" revision | |
29504118 | 673 | |
f02b3eba | 674 | $dest =~ tr/_/\./ if $opt_u; |
a16db4f4 | 675 | $branch = $dest; |
eaf718f3 MU |
676 | |
677 | my $pid = open2($in, $out, 'git-mktag'); | |
678 | print $out ("object $cid\n". | |
679 | "type commit\n". | |
f02b3eba | 680 | "tag $dest\n". |
eaf718f3 MU |
681 | "tagger $author_name <$author_email>\n") and |
682 | close($out) | |
f02b3eba | 683 | or die "Cannot create tag object $dest: $!\n"; |
eaf718f3 MU |
684 | |
685 | my $tagobj = <$in>; | |
686 | chomp $tagobj; | |
687 | ||
688 | if ( !close($in) or waitpid($pid, 0) != $pid or | |
689 | $? != 0 or $tagobj !~ /^[0123456789abcdef]{40}$/ ) { | |
f02b3eba | 690 | die "Cannot create tag object $dest: $!\n"; |
eaf718f3 | 691 | } |
eaf718f3 | 692 | |
f02b3eba MU |
693 | open(C,">$git_dir/refs/tags/$dest") and |
694 | print C ("$tagobj\n") and | |
eaf718f3 | 695 | close(C) |
f02b3eba | 696 | or die "Cannot create tag $branch: $!\n"; |
eaf718f3 | 697 | |
f02b3eba | 698 | print "Created tag '$dest' on '$branch'\n" if $opt_v; |
eaf718f3 | 699 | } |
e7e477df MU |
700 | $branches{$branch}{"LAST"} = $cid; |
701 | $branches{$branch}{$revision} = $cid; | |
702 | $last_rev = $cid; | |
703 | print BRANCHES "$revision $branch $cid\n"; | |
704 | print "DONE: $revision $dest $cid\n" if $opt_v; | |
eaf718f3 MU |
705 | } |
706 | ||
707 | my ($changed_paths, $revision, $author, $date, $message, $pool) = @_; | |
708 | sub _commit_all { | |
709 | ($changed_paths, $revision, $author, $date, $message, $pool) = @_; | |
f0daa628 MU |
710 | my %p; |
711 | while(my($path,$action) = each %$changed_paths) { | |
0090a618 | 712 | $p{$path} = [ $action->action,$action->copyfrom_path, $action->copyfrom_rev, $path ]; |
f0daa628 MU |
713 | } |
714 | $changed_paths = \%p; | |
eaf718f3 | 715 | } |
f0daa628 | 716 | |
eaf718f3 MU |
717 | sub commit_all { |
718 | my %done; | |
719 | my @col; | |
720 | my $pref; | |
721 | my $branch; | |
722 | ||
723 | while(my($path,$action) = each %$changed_paths) { | |
724 | ($branch,$path) = split_path($revision,$path); | |
725 | next if not defined $branch; | |
726 | $done{$branch}{$path} = $action; | |
727 | } | |
728 | while(($branch,$changed_paths) = each %done) { | |
729 | commit($branch, $changed_paths, $revision, $author, $date, $message); | |
730 | } | |
731 | } | |
732 | ||
8470b7f3 | 733 | while(++$current_rev <= $svn->{'maxrev'}) { |
01d4f0e7 YAS |
734 | if (defined $opt_l) { |
735 | $opt_l--; | |
736 | if ($opt_l < 0) { | |
737 | last; | |
738 | } | |
739 | } | |
fcfa32b9 YAS |
740 | my $pool=SVN::Pool->new; |
741 | $svn->{'svn'}->get_log("/",$current_rev,$current_rev,1,1,1,\&_commit_all,$pool); | |
742 | $pool->clear; | |
eaf718f3 MU |
743 | commit_all(); |
744 | } | |
745 | ||
746 | ||
747 | unlink($git_index); | |
748 | ||
749 | if (defined $orig_git_index) { | |
750 | $ENV{GIT_INDEX_FILE} = $orig_git_index; | |
751 | } else { | |
752 | delete $ENV{GIT_INDEX_FILE}; | |
753 | } | |
754 | ||
755 | # Now switch back to the branch we were in before all of this happened | |
756 | if($orig_branch) { | |
3ef378a6 | 757 | print "DONE\n" if $opt_v and (not defined $opt_l or $opt_l > 0); |
eaf718f3 MU |
758 | system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master") |
759 | if $forward_master; | |
760 | unless ($opt_i) { | |
761 | system('git-read-tree', '-m', '-u', 'SVN2GIT_HEAD', 'HEAD'); | |
762 | die "read-tree failed: $?\n" if $?; | |
763 | } | |
764 | } else { | |
765 | $orig_branch = "master"; | |
3ef378a6 | 766 | print "DONE; creating $orig_branch branch\n" if $opt_v and (not defined $opt_l or $opt_l > 0); |
eaf718f3 MU |
767 | system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master") |
768 | unless -f "$git_dir/refs/heads/master"; | |
769 | unlink("$git_dir/HEAD"); | |
770 | symlink("refs/heads/$orig_branch","$git_dir/HEAD"); | |
771 | unless ($opt_i) { | |
772 | system('git checkout'); | |
773 | die "checkout failed: $?\n" if $?; | |
774 | } | |
775 | } | |
776 | unlink("$git_dir/SVN2GIT_HEAD"); | |
777 | close(BRANCHES); |