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