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