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