]>
Commit | Line | Data |
---|---|---|
3397f9df | 1 | #!/usr/bin/env perl |
551ce28f EW |
2 | # Copyright (C) 2006, Eric Wong <normalperson@yhbt.net> |
3 | # License: GPL v2 or later | |
3397f9df EW |
4 | use warnings; |
5 | use strict; | |
6 | use vars qw/ $AUTHOR $VERSION | |
1ca72aef | 7 | $SVN_URL $SVN_INFO $SVN_WC $SVN_UUID |
3397f9df | 8 | $GIT_SVN_INDEX $GIT_SVN |
42d32870 | 9 | $GIT_DIR $GIT_SVN_DIR $REVDB/; |
3397f9df | 10 | $AUTHOR = 'Eric Wong <normalperson@yhbt.net>'; |
42d32870 | 11 | $VERSION = '1.1.1-broken'; |
13ccd6d4 EW |
12 | |
13 | use Cwd qw/abs_path/; | |
14 | $GIT_DIR = abs_path($ENV{GIT_DIR} || '.git'); | |
15 | $ENV{GIT_DIR} = $GIT_DIR; | |
16 | ||
ce475dfc | 17 | my $LC_ALL = $ENV{LC_ALL}; |
79bb8d88 | 18 | my $TZ = $ENV{TZ}; |
3397f9df EW |
19 | # make sure the svn binary gives consistent output between locales and TZs: |
20 | $ENV{TZ} = 'UTC'; | |
21 | $ENV{LC_ALL} = 'C'; | |
a00439ac | 22 | $| = 1; # unbuffer STDOUT |
3397f9df EW |
23 | |
24 | # If SVN:: library support is added, please make the dependencies | |
25 | # optional and preserve the capability to use the command-line client. | |
ce6f3519 | 26 | # use eval { require SVN::... } to make it lazy load |
eeb0abe0 | 27 | # We don't use any modules not in the standard Perl distribution: |
3397f9df EW |
28 | use Carp qw/croak/; |
29 | use IO::File qw//; | |
30 | use File::Basename qw/dirname basename/; | |
31 | use File::Path qw/mkpath/; | |
79bb8d88 | 32 | use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev pass_through/; |
3397f9df | 33 | use File::Spec qw//; |
e17512f3 | 34 | use POSIX qw/strftime/; |
968bdf1f | 35 | use IPC::Open3; |
42d32870 EW |
36 | use Memoize; |
37 | memoize('revisions_eq'); | |
c1927a85 EW |
38 | memoize('cmt_metadata'); |
39 | memoize('get_commit_time'); | |
a5e0cedc EW |
40 | |
41 | my ($SVN_PATH, $SVN, $SVN_LOG, $_use_lib); | |
42 | $_use_lib = 1 unless $ENV{GIT_SVN_NO_LIB}; | |
43 | libsvn_load(); | |
42d32870 | 44 | my $_optimize_commits = 1 unless $ENV{GIT_SVN_NO_OPTIMIZE_COMMITS}; |
3397f9df | 45 | my $sha1 = qr/[a-f\d]{40}/; |
ac8e0b91 | 46 | my $sha1_short = qr/[a-f\d]{4,40}/; |
72942938 | 47 | my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit, |
1a82e793 | 48 | $_find_copies_harder, $_l, $_cp_similarity, $_cp_remote, |
80f50749 | 49 | $_repack, $_repack_nr, $_repack_flags, $_q, |
a00439ac | 50 | $_message, $_file, $_follow_parent, $_no_metadata, |
9d55b41a | 51 | $_template, $_shared, $_no_default_regex, $_no_graft_copy, |
79bb8d88 | 52 | $_limit, $_verbose, $_incremental, $_oneline, $_l_fmt, $_show_commit, |
9d55b41a | 53 | $_version, $_upgrade, $_authors, $_branch_all_refs, @_opt_m); |
42d32870 | 54 | my (@_branch_from, %tree_map, %users, %rusers, %equiv); |
8a97e368 | 55 | my ($_svn_co_url_revs, $_svn_pg_peg_revs); |
883d0a78 | 56 | my @repo_path_split_cache; |
3397f9df | 57 | |
eeb0abe0 | 58 | my %fc_opts = ( 'no-ignore-externals' => \$_no_ignore_ext, |
69f0d91e | 59 | 'branch|b=s' => \@_branch_from, |
a00439ac | 60 | 'follow-parent|follow' => \$_follow_parent, |
bf78b1d8 | 61 | 'branch-all-refs|B' => \$_branch_all_refs, |
dc5869c0 EW |
62 | 'authors-file|A=s' => \$_authors, |
63 | 'repack:i' => \$_repack, | |
a00439ac | 64 | 'no-metadata' => \$_no_metadata, |
80f50749 | 65 | 'quiet|q' => \$_q, |
dc5869c0 | 66 | 'repack-flags|repack-args|repack-opts=s' => \$_repack_flags); |
36f5b1f0 | 67 | |
9d55b41a EW |
68 | my ($_trunk, $_tags, $_branches); |
69 | my %multi_opts = ( 'trunk|T=s' => \$_trunk, | |
70 | 'tags|t=s' => \$_tags, | |
71 | 'branches|b=s' => \$_branches ); | |
72 | my %init_opts = ( 'template=s' => \$_template, 'shared' => \$_shared ); | |
27e9fb8d EW |
73 | my %cmt_opts = ( 'edit|e' => \$_edit, |
74 | 'rmdir' => \$_rmdir, | |
75 | 'find-copies-harder' => \$_find_copies_harder, | |
76 | 'l=i' => \$_l, | |
77 | 'copy-similarity|C=i'=> \$_cp_similarity | |
78 | ); | |
9d55b41a | 79 | |
36f5b1f0 EW |
80 | # yes, 'native' sets "\n". Patches to fix this for non-*nix systems welcome: |
81 | my %EOL = ( CR => "\015", LF => "\012", CRLF => "\015\012", native => "\012" ); | |
82 | ||
3397f9df | 83 | my %cmd = ( |
eeb0abe0 EW |
84 | fetch => [ \&fetch, "Download new revisions from SVN", |
85 | { 'revision|r=s' => \$_revision, %fc_opts } ], | |
81c5a0e6 | 86 | init => [ \&init, "Initialize a repo for tracking" . |
f8ab6b73 | 87 | " (requires URL argument)", |
9d55b41a | 88 | \%init_opts ], |
eeb0abe0 | 89 | commit => [ \&commit, "Commit git revisions to SVN", |
27e9fb8d | 90 | { 'stdin|' => \$_stdin, %cmt_opts, %fc_opts, } ], |
a5e0cedc EW |
91 | 'show-ignore' => [ \&show_ignore, "Show svn:ignore listings", |
92 | { 'revision|r=i' => \$_revision } ], | |
eeb0abe0 EW |
93 | rebuild => [ \&rebuild, "Rebuild git-svn metadata (after git clone)", |
94 | { 'no-ignore-externals' => \$_no_ignore_ext, | |
1a82e793 | 95 | 'copy-remote|remote=s' => \$_cp_remote, |
eeb0abe0 | 96 | 'upgrade' => \$_upgrade } ], |
9d55b41a EW |
97 | 'graft-branches' => [ \&graft_branches, |
98 | 'Detect merges/branches from already imported history', | |
99 | { 'merge-rx|m' => \@_opt_m, | |
c1927a85 EW |
100 | 'branch|b=s' => \@_branch_from, |
101 | 'branch-all-refs|B' => \$_branch_all_refs, | |
9d55b41a EW |
102 | 'no-default-regex' => \$_no_default_regex, |
103 | 'no-graft-copy' => \$_no_graft_copy } ], | |
104 | 'multi-init' => [ \&multi_init, | |
105 | 'Initialize multiple trees (like git-svnimport)', | |
106 | { %multi_opts, %fc_opts } ], | |
107 | 'multi-fetch' => [ \&multi_fetch, | |
108 | 'Fetch multiple trees (like git-svnimport)', | |
109 | \%fc_opts ], | |
79bb8d88 EW |
110 | 'log' => [ \&show_log, 'Show commit logs', |
111 | { 'limit=i' => \$_limit, | |
112 | 'revision|r=s' => \$_revision, | |
113 | 'verbose|v' => \$_verbose, | |
114 | 'incremental' => \$_incremental, | |
115 | 'oneline' => \$_oneline, | |
116 | 'show-commit' => \$_show_commit, | |
117 | 'authors-file|A=s' => \$_authors, | |
118 | } ], | |
27e9fb8d EW |
119 | 'commit-diff' => [ \&commit_diff, 'Commit a diff between two trees', |
120 | { 'message|m=s' => \$_message, | |
121 | 'file|F=s' => \$_file, | |
122 | %cmt_opts } ], | |
3397f9df | 123 | ); |
9d55b41a | 124 | |
3397f9df EW |
125 | my $cmd; |
126 | for (my $i = 0; $i < @ARGV; $i++) { | |
127 | if (defined $cmd{$ARGV[$i]}) { | |
128 | $cmd = $ARGV[$i]; | |
129 | splice @ARGV, $i, 1; | |
130 | last; | |
131 | } | |
132 | }; | |
133 | ||
448c81b4 | 134 | my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd); |
a9612be2 | 135 | |
b8c92cad | 136 | read_repo_config(\%opts); |
79bb8d88 EW |
137 | my $rv = GetOptions(%opts, 'help|H|h' => \$_help, |
138 | 'version|V' => \$_version, | |
139 | 'id|i=s' => \$GIT_SVN); | |
140 | exit 1 if (!$rv && $cmd ne 'log'); | |
6f0783cf | 141 | |
dc5869c0 | 142 | set_default_vals(); |
3397f9df | 143 | usage(0) if $_help; |
551ce28f | 144 | version() if $_version; |
eeb0abe0 | 145 | usage(1) unless defined $cmd; |
b8c92cad | 146 | init_vars(); |
eeb0abe0 | 147 | load_authors() if $_authors; |
bf78b1d8 | 148 | load_all_refs() if $_branch_all_refs; |
dc62e25c | 149 | svn_compat_check() unless $_use_lib; |
1a82e793 | 150 | migration_check() unless $cmd =~ /^(?:init|rebuild|multi-init)$/; |
3397f9df EW |
151 | $cmd{$cmd}->[0]->(@ARGV); |
152 | exit 0; | |
153 | ||
154 | ####################### primary functions ###################### | |
155 | sub usage { | |
156 | my $exit = shift || 0; | |
157 | my $fd = $exit ? \*STDERR : \*STDOUT; | |
158 | print $fd <<""; | |
159 | git-svn - bidirectional operations between a single Subversion tree and git | |
160 | Usage: $0 <command> [options] [arguments]\n | |
448c81b4 EW |
161 | |
162 | print $fd "Available commands:\n" unless $cmd; | |
3397f9df EW |
163 | |
164 | foreach (sort keys %cmd) { | |
448c81b4 | 165 | next if $cmd && $cmd ne $_; |
ac8e0b91 | 166 | print $fd ' ',pack('A13',$_),$cmd{$_}->[1],"\n"; |
448c81b4 EW |
167 | foreach (keys %{$cmd{$_}->[2]}) { |
168 | # prints out arguments as they should be passed: | |
b8c92cad | 169 | my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : ''; |
448c81b4 EW |
170 | print $fd ' ' x 17, join(', ', map { length $_ > 1 ? |
171 | "--$_" : "-$_" } | |
172 | split /\|/,$_)," $x\n"; | |
173 | } | |
3397f9df EW |
174 | } |
175 | print $fd <<""; | |
448c81b4 EW |
176 | \nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an |
177 | arbitrary identifier if you're tracking multiple SVN branches/repositories in | |
178 | one git repository and want to keep them separate. See git-svn(1) for more | |
179 | information. | |
3397f9df EW |
180 | |
181 | exit $exit; | |
182 | } | |
183 | ||
551ce28f EW |
184 | sub version { |
185 | print "git-svn version $VERSION\n"; | |
186 | exit 0; | |
187 | } | |
188 | ||
3397f9df | 189 | sub rebuild { |
1a82e793 EW |
190 | if (quiet_run(qw/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0")) { |
191 | copy_remote_ref(); | |
192 | } | |
3397f9df | 193 | $SVN_URL = shift or undef; |
3397f9df | 194 | my $newest_rev = 0; |
2beb3cdd EW |
195 | if ($_upgrade) { |
196 | sys('git-update-ref',"refs/remotes/$GIT_SVN","$GIT_SVN-HEAD"); | |
197 | } else { | |
198 | check_upgrade_needed(); | |
199 | } | |
3397f9df EW |
200 | |
201 | my $pid = open(my $rev_list,'-|'); | |
202 | defined $pid or croak $!; | |
203 | if ($pid == 0) { | |
2beb3cdd | 204 | exec("git-rev-list","refs/remotes/$GIT_SVN") or croak $!; |
3397f9df | 205 | } |
2beb3cdd | 206 | my $latest; |
3397f9df EW |
207 | while (<$rev_list>) { |
208 | chomp; | |
209 | my $c = $_; | |
210 | croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o; | |
211 | my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`); | |
212 | next if (!@commit); # skip merges | |
79bb8d88 EW |
213 | my ($url, $rev, $uuid) = extract_metadata($commit[$#commit]); |
214 | if (!$rev || !$uuid) { | |
215 | croak "Unable to extract revision or UUID from ", | |
216 | "$c, $commit[$#commit]\n"; | |
3397f9df | 217 | } |
2beb3cdd EW |
218 | |
219 | # if we merged or otherwise started elsewhere, this is | |
220 | # how we break out of it | |
1ca72aef | 221 | next if (defined $SVN_UUID && ($uuid ne $SVN_UUID)); |
779b1446 | 222 | next if (defined $SVN_URL && defined $url && ($url ne $SVN_URL)); |
2beb3cdd | 223 | |
2beb3cdd | 224 | unless (defined $latest) { |
3397f9df EW |
225 | if (!$SVN_URL && !$url) { |
226 | croak "SVN repository location required: $url\n"; | |
227 | } | |
228 | $SVN_URL ||= $url; | |
1d52aba8 EW |
229 | $SVN_UUID ||= $uuid; |
230 | setup_git_svn(); | |
2beb3cdd | 231 | $latest = $rev; |
3397f9df | 232 | } |
42d32870 EW |
233 | revdb_set($REVDB, $rev, $c); |
234 | print "r$rev = $c\n"; | |
3397f9df EW |
235 | $newest_rev = $rev if ($rev > $newest_rev); |
236 | } | |
237 | close $rev_list or croak $?; | |
a5e0cedc EW |
238 | |
239 | goto out if $_use_lib; | |
3397f9df | 240 | if (!chdir $SVN_WC) { |
1d52aba8 | 241 | svn_cmd_checkout($SVN_URL, $latest, $SVN_WC); |
3397f9df EW |
242 | chdir $SVN_WC or croak $!; |
243 | } | |
244 | ||
245 | $pid = fork; | |
246 | defined $pid or croak $!; | |
247 | if ($pid == 0) { | |
248 | my @svn_up = qw(svn up); | |
249 | push @svn_up, '--ignore-externals' unless $_no_ignore_ext; | |
250 | sys(@svn_up,"-r$newest_rev"); | |
251 | $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX; | |
36f5b1f0 | 252 | index_changes(); |
b8c92cad | 253 | exec('git-write-tree') or croak $!; |
3397f9df EW |
254 | } |
255 | waitpid $pid, 0; | |
b8c92cad | 256 | croak $? if $?; |
a5e0cedc | 257 | out: |
2beb3cdd EW |
258 | if ($_upgrade) { |
259 | print STDERR <<""; | |
260 | Keeping deprecated refs/head/$GIT_SVN-HEAD for now. Please remove it | |
261 | when you have upgraded your tools and habits to use refs/remotes/$GIT_SVN | |
262 | ||
263 | } | |
3397f9df EW |
264 | } |
265 | ||
266 | sub init { | |
81c5a0e6 EW |
267 | $SVN_URL = shift or die "SVN repository location required " . |
268 | "as a command-line argument\n"; | |
9d55b41a | 269 | $SVN_URL =~ s!/+$!!; # strip trailing slash |
3397f9df | 270 | unless (-d $GIT_DIR) { |
f8ab6b73 EW |
271 | my @init_db = ('git-init-db'); |
272 | push @init_db, "--template=$_template" if defined $_template; | |
273 | push @init_db, "--shared" if defined $_shared; | |
274 | sys(@init_db); | |
3397f9df EW |
275 | } |
276 | setup_git_svn(); | |
277 | } | |
278 | ||
279 | sub fetch { | |
2beb3cdd | 280 | check_upgrade_needed(); |
883d0a78 | 281 | $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url"); |
a5e0cedc EW |
282 | my $ret = $_use_lib ? fetch_lib(@_) : fetch_cmd(@_); |
283 | if ($ret->{commit} && quiet_run(qw(git-rev-parse --verify | |
284 | refs/heads/master^0))) { | |
285 | sys(qw(git-update-ref refs/heads/master),$ret->{commit}); | |
286 | } | |
287 | return $ret; | |
288 | } | |
289 | ||
290 | sub fetch_cmd { | |
291 | my (@parents) = @_; | |
3397f9df | 292 | my @log_args = -d $SVN_WC ? ($SVN_WC) : ($SVN_URL); |
defc6492 EW |
293 | unless ($_revision) { |
294 | $_revision = -d $SVN_WC ? 'BASE:HEAD' : '0:HEAD'; | |
3397f9df | 295 | } |
defc6492 | 296 | push @log_args, "-r$_revision"; |
3397f9df EW |
297 | push @log_args, '--stop-on-copy' unless $_no_stop_copy; |
298 | ||
ce6f3519 | 299 | my $svn_log = svn_log_raw(@log_args); |
3397f9df | 300 | |
03823184 | 301 | my $base = next_log_entry($svn_log) or croak "No base revision!\n"; |
42d32870 EW |
302 | # don't need last_revision from grab_base_rev() because |
303 | # user could've specified a different revision to skip (they | |
304 | # didn't want to import certain revisions into git for whatever | |
305 | # reason, so trust $base->{revision} instead. | |
306 | my (undef, $last_commit) = svn_grab_base_rev(); | |
3397f9df | 307 | unless (-d $SVN_WC) { |
1d52aba8 | 308 | svn_cmd_checkout($SVN_URL,$base->{revision},$SVN_WC); |
3397f9df | 309 | chdir $SVN_WC or croak $!; |
1d52aba8 | 310 | read_uuid(); |
3397f9df | 311 | $last_commit = git_commit($base, @parents); |
36f5b1f0 | 312 | assert_tree($last_commit); |
3397f9df EW |
313 | } else { |
314 | chdir $SVN_WC or croak $!; | |
1d52aba8 | 315 | read_uuid(); |
6dfbe516 EW |
316 | # looks like a user manually cp'd and svn switch'ed |
317 | unless ($last_commit) { | |
318 | sys(qw/svn revert -R ./); | |
319 | assert_svn_wc_clean($base->{revision}); | |
320 | $last_commit = git_commit($base, @parents); | |
321 | assert_tree($last_commit); | |
322 | } | |
3397f9df EW |
323 | } |
324 | my @svn_up = qw(svn up); | |
325 | push @svn_up, '--ignore-externals' unless $_no_ignore_ext; | |
03823184 EW |
326 | my $last = $base; |
327 | while (my $log_msg = next_log_entry($svn_log)) { | |
03823184 EW |
328 | if ($last->{revision} >= $log_msg->{revision}) { |
329 | croak "Out of order: last >= current: ", | |
330 | "$last->{revision} >= $log_msg->{revision}\n"; | |
331 | } | |
36f5b1f0 EW |
332 | # Revert is needed for cases like: |
333 | # https://svn.musicpd.org/Jamming/trunk (r166:167), but | |
334 | # I can't seem to reproduce something like that on a test... | |
335 | sys(qw/svn revert -R ./); | |
336 | assert_svn_wc_clean($last->{revision}); | |
03823184 | 337 | sys(@svn_up,"-r$log_msg->{revision}"); |
3397f9df | 338 | $last_commit = git_commit($log_msg, $last_commit, @parents); |
03823184 | 339 | $last = $log_msg; |
3397f9df | 340 | } |
b8c92cad | 341 | close $svn_log->{fh}; |
a5e0cedc | 342 | $last->{commit} = $last_commit; |
03823184 | 343 | return $last; |
3397f9df EW |
344 | } |
345 | ||
a5e0cedc EW |
346 | sub fetch_lib { |
347 | my (@parents) = @_; | |
348 | $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url"); | |
349 | my $repo; | |
350 | ($repo, $SVN_PATH) = repo_path_split($SVN_URL); | |
351 | $SVN_LOG ||= libsvn_connect($repo); | |
352 | $SVN ||= libsvn_connect($repo); | |
353 | my ($last_rev, $last_commit) = svn_grab_base_rev(); | |
354 | my ($base, $head) = libsvn_parse_revision($last_rev); | |
355 | if ($base > $head) { | |
356 | return { revision => $last_rev, commit => $last_commit } | |
357 | } | |
358 | my $index = set_index($GIT_SVN_INDEX); | |
359 | ||
360 | # limit ourselves and also fork() since get_log won't release memory | |
361 | # after processing a revision and SVN stuff seems to leak | |
362 | my $inc = 1000; | |
363 | my ($min, $max) = ($base, $head < $base+$inc ? $head : $base+$inc); | |
364 | read_uuid(); | |
365 | if (defined $last_commit) { | |
366 | unless (-e $GIT_SVN_INDEX) { | |
367 | sys(qw/git-read-tree/, $last_commit); | |
368 | } | |
369 | chomp (my $x = `git-write-tree`); | |
370 | my ($y) = (`git-cat-file commit $last_commit` | |
371 | =~ /^tree ($sha1)/m); | |
372 | if ($y ne $x) { | |
373 | unlink $GIT_SVN_INDEX or croak $!; | |
374 | sys(qw/git-read-tree/, $last_commit); | |
375 | } | |
376 | chomp ($x = `git-write-tree`); | |
377 | if ($y ne $x) { | |
378 | print STDERR "trees ($last_commit) $y != $x\n", | |
379 | "Something is seriously wrong...\n"; | |
380 | } | |
381 | } | |
382 | while (1) { | |
383 | # fork, because using SVN::Pool with get_log() still doesn't | |
384 | # seem to help enough to keep memory usage down. | |
385 | defined(my $pid = fork) or croak $!; | |
386 | if (!$pid) { | |
387 | $SVN::Error::handler = \&libsvn_skip_unknown_revs; | |
a5e0cedc EW |
388 | |
389 | # Yes I'm perfectly aware that the fourth argument | |
390 | # below is the limit revisions number. Unfortunately | |
391 | # performance sucks with it enabled, so it's much | |
392 | # faster to fetch revision ranges instead of relying | |
393 | # on the limiter. | |
dc62e25c EW |
394 | libsvn_get_log($SVN_LOG, '/'.$SVN_PATH, |
395 | $min, $max, 0, 1, 1, | |
a5e0cedc EW |
396 | sub { |
397 | my $log_msg; | |
398 | if ($last_commit) { | |
399 | $log_msg = libsvn_fetch( | |
400 | $last_commit, @_); | |
401 | $last_commit = git_commit( | |
402 | $log_msg, | |
403 | $last_commit, | |
404 | @parents); | |
405 | } else { | |
406 | $log_msg = libsvn_new_tree(@_); | |
407 | $last_commit = git_commit( | |
408 | $log_msg, @parents); | |
409 | } | |
410 | }); | |
a5e0cedc EW |
411 | exit 0; |
412 | } | |
413 | waitpid $pid, 0; | |
414 | croak $? if $?; | |
415 | ($last_rev, $last_commit) = svn_grab_base_rev(); | |
416 | last if ($max >= $head); | |
417 | $min = $max + 1; | |
418 | $max += $inc; | |
419 | $max = $head if ($max > $head); | |
420 | } | |
421 | restore_index($index); | |
422 | return { revision => $last_rev, commit => $last_commit }; | |
423 | } | |
424 | ||
3397f9df EW |
425 | sub commit { |
426 | my (@commits) = @_; | |
2beb3cdd | 427 | check_upgrade_needed(); |
3397f9df EW |
428 | if ($_stdin || !@commits) { |
429 | print "Reading from stdin...\n"; | |
430 | @commits = (); | |
431 | while (<STDIN>) { | |
1ca72aef | 432 | if (/\b($sha1_short)\b/o) { |
3397f9df EW |
433 | unshift @commits, $1; |
434 | } | |
435 | } | |
436 | } | |
437 | my @revs; | |
8de010ad EW |
438 | foreach my $c (@commits) { |
439 | chomp(my @tmp = safe_qx('git-rev-parse',$c)); | |
440 | if (scalar @tmp == 1) { | |
441 | push @revs, $tmp[0]; | |
442 | } elsif (scalar @tmp > 1) { | |
443 | push @revs, reverse (safe_qx('git-rev-list',@tmp)); | |
444 | } else { | |
445 | die "Failed to rev-parse $c\n"; | |
446 | } | |
3397f9df EW |
447 | } |
448 | chomp @revs; | |
a5e0cedc EW |
449 | $_use_lib ? commit_lib(@revs) : commit_cmd(@revs); |
450 | print "Done committing ",scalar @revs," revisions to SVN\n"; | |
451 | } | |
452 | ||
453 | sub commit_cmd { | |
454 | my (@revs) = @_; | |
3397f9df | 455 | |
b63af9b3 | 456 | chdir $SVN_WC or croak "Unable to chdir $SVN_WC: $!\n"; |
1d52aba8 | 457 | my $info = svn_info('.'); |
b63af9b3 EW |
458 | my $fetched = fetch(); |
459 | if ($info->{Revision} != $fetched->{revision}) { | |
460 | print STDERR "There are new revisions that were fetched ", | |
461 | "and need to be merged (or acknowledged) ", | |
462 | "before committing.\n"; | |
463 | exit 1; | |
464 | } | |
465 | $info = svn_info('.'); | |
1d52aba8 | 466 | read_uuid($info); |
42d32870 | 467 | my $last = $fetched; |
3397f9df | 468 | foreach my $c (@revs) { |
42d32870 | 469 | my $mods = svn_checkout_tree($last, $c); |
cf52b8f0 EW |
470 | if (scalar @$mods == 0) { |
471 | print "Skipping, no changes detected\n"; | |
472 | next; | |
473 | } | |
42d32870 | 474 | $last = svn_commit_tree($last, $c); |
3397f9df | 475 | } |
3397f9df EW |
476 | } |
477 | ||
a5e0cedc EW |
478 | sub commit_lib { |
479 | my (@revs) = @_; | |
480 | my ($r_last, $cmt_last) = svn_grab_base_rev(); | |
481 | defined $r_last or die "Must have an existing revision to commit\n"; | |
cf7424b0 | 482 | my $fetched = fetch(); |
a5e0cedc EW |
483 | if ($r_last != $fetched->{revision}) { |
484 | print STDERR "There are new revisions that were fetched ", | |
485 | "and need to be merged (or acknowledged) ", | |
486 | "before committing.\n", | |
487 | "last rev: $r_last\n", | |
488 | " current: $fetched->{revision}\n"; | |
489 | exit 1; | |
490 | } | |
491 | read_uuid(); | |
492 | my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : (); | |
493 | my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$"; | |
494 | ||
27e9fb8d | 495 | set_svn_commit_env(); |
a5e0cedc | 496 | foreach my $c (@revs) { |
ec9d00d0 EW |
497 | my $log_msg = get_commit_message($c, $commit_msg); |
498 | ||
a5e0cedc EW |
499 | # fork for each commit because there's a memory leak I |
500 | # can't track down... (it's probably in the SVN code) | |
501 | defined(my $pid = open my $fh, '-|') or croak $!; | |
502 | if (!$pid) { | |
a5e0cedc EW |
503 | my $ed = SVN::Git::Editor->new( |
504 | { r => $r_last, | |
505 | ra => $SVN, | |
506 | c => $c, | |
507 | svn_path => $SVN_PATH | |
508 | }, | |
509 | $SVN->get_commit_editor( | |
510 | $log_msg->{msg}, | |
511 | sub { | |
512 | libsvn_commit_cb( | |
513 | @_, $c, | |
514 | $log_msg->{msg}, | |
515 | $r_last, | |
516 | $cmt_last) | |
517 | }, | |
518 | @lock) | |
519 | ); | |
42d32870 | 520 | my $mods = libsvn_checkout_tree($cmt_last, $c, $ed); |
a5e0cedc EW |
521 | if (@$mods == 0) { |
522 | print "No changes\nr$r_last = $cmt_last\n"; | |
523 | $ed->abort_edit; | |
524 | } else { | |
525 | $ed->close_edit; | |
526 | } | |
527 | exit 0; | |
528 | } | |
529 | my ($r_new, $cmt_new, $no); | |
530 | while (<$fh>) { | |
531 | print $_; | |
532 | chomp; | |
533 | if (/^r(\d+) = ($sha1)$/o) { | |
534 | ($r_new, $cmt_new) = ($1, $2); | |
535 | } elsif ($_ eq 'No changes') { | |
536 | $no = 1; | |
537 | } | |
538 | } | |
cf7424b0 | 539 | close $fh or croak $?; |
a5e0cedc EW |
540 | if (! defined $r_new && ! defined $cmt_new) { |
541 | unless ($no) { | |
542 | die "Failed to parse revision information\n"; | |
543 | } | |
544 | } else { | |
545 | ($r_last, $cmt_last) = ($r_new, $cmt_new); | |
546 | } | |
547 | } | |
ec9d00d0 | 548 | $ENV{LC_ALL} = 'C'; |
a5e0cedc EW |
549 | unlink $commit_msg; |
550 | } | |
8f22562c | 551 | |
a5e0cedc | 552 | sub show_ignore { |
883d0a78 | 553 | $SVN_URL ||= file_to_s("$GIT_SVN_DIR/info/url"); |
a5e0cedc EW |
554 | $_use_lib ? show_ignore_lib() : show_ignore_cmd(); |
555 | } | |
556 | ||
557 | sub show_ignore_cmd { | |
558 | require File::Find or die $!; | |
559 | if (defined $_revision) { | |
560 | die "-r/--revision option doesn't work unless the Perl SVN ", | |
561 | "libraries are used\n"; | |
562 | } | |
8f22562c EW |
563 | chdir $SVN_WC or croak $!; |
564 | my %ign; | |
565 | File::Find::find({wanted=>sub{if(lstat $_ && -d _ && -d "$_/.svn"){ | |
566 | s#^\./##; | |
8a97e368 | 567 | @{$ign{$_}} = svn_propget_base('svn:ignore', $_); |
8f22562c EW |
568 | }}, no_chdir=>1},'.'); |
569 | ||
570 | print "\n# /\n"; | |
571 | foreach (@{$ign{'.'}}) { print '/',$_ if /\S/ } | |
572 | delete $ign{'.'}; | |
573 | foreach my $i (sort keys %ign) { | |
574 | print "\n# ",$i,"\n"; | |
575 | foreach (@{$ign{$i}}) { print '/',$i,'/',$_ if /\S/ } | |
576 | } | |
577 | } | |
578 | ||
a5e0cedc EW |
579 | sub show_ignore_lib { |
580 | my $repo; | |
581 | ($repo, $SVN_PATH) = repo_path_split($SVN_URL); | |
582 | $SVN ||= libsvn_connect($repo); | |
583 | my $r = defined $_revision ? $_revision : $SVN->get_latest_revnum; | |
584 | libsvn_traverse_ignore(\*STDOUT, $SVN_PATH, $r); | |
585 | } | |
586 | ||
9d55b41a EW |
587 | sub graft_branches { |
588 | my $gr_file = "$GIT_DIR/info/grafts"; | |
589 | my ($grafts, $comments) = read_grafts($gr_file); | |
590 | my $gr_sha1; | |
591 | ||
592 | if (%$grafts) { | |
593 | # temporarily disable our grafts file to make this idempotent | |
594 | chomp($gr_sha1 = safe_qx(qw/git-hash-object -w/,$gr_file)); | |
595 | rename $gr_file, "$gr_file~$gr_sha1" or croak $!; | |
596 | } | |
597 | ||
598 | my $l_map = read_url_paths(); | |
599 | my @re = map { qr/$_/is } @_opt_m if @_opt_m; | |
600 | unless ($_no_default_regex) { | |
c1927a85 EW |
601 | push @re, (qr/\b(?:merge|merging|merged)\s+with\s+([\w\.\-]+)/i, |
602 | qr/\b(?:merge|merging|merged)\s+([\w\.\-]+)/i, | |
603 | qr/\b(?:from|of)\s+([\w\.\-]+)/i ); | |
9d55b41a EW |
604 | } |
605 | foreach my $u (keys %$l_map) { | |
606 | if (@re) { | |
607 | foreach my $p (keys %{$l_map->{$u}}) { | |
c1927a85 | 608 | graft_merge_msg($grafts,$l_map,$u,$p,@re); |
9d55b41a EW |
609 | } |
610 | } | |
a5e0cedc EW |
611 | unless ($_no_graft_copy) { |
612 | if ($_use_lib) { | |
613 | graft_file_copy_lib($grafts,$l_map,$u); | |
614 | } else { | |
615 | graft_file_copy_cmd($grafts,$l_map,$u); | |
616 | } | |
617 | } | |
9d55b41a | 618 | } |
c1927a85 | 619 | graft_tree_joins($grafts); |
9d55b41a EW |
620 | |
621 | write_grafts($grafts, $comments, $gr_file); | |
622 | unlink "$gr_file~$gr_sha1" if $gr_sha1; | |
623 | } | |
624 | ||
625 | sub multi_init { | |
626 | my $url = shift; | |
627 | $_trunk ||= 'trunk'; | |
628 | $_trunk =~ s#/+$##; | |
629 | $url =~ s#/+$## if $url; | |
630 | if ($_trunk !~ m#^[a-z\+]+://#) { | |
631 | $_trunk = '/' . $_trunk if ($_trunk !~ m#^/#); | |
632 | unless ($url) { | |
633 | print STDERR "E: '$_trunk' is not a complete URL ", | |
634 | "and a separate URL is not specified\n"; | |
635 | exit 1; | |
636 | } | |
637 | $_trunk = $url . $_trunk; | |
638 | } | |
639 | if ($GIT_SVN eq 'git-svn') { | |
640 | print "GIT_SVN_ID set to 'trunk' for $_trunk\n"; | |
641 | $GIT_SVN = $ENV{GIT_SVN_ID} = 'trunk'; | |
642 | } | |
643 | init_vars(); | |
644 | init($_trunk); | |
645 | complete_url_ls_init($url, $_branches, '--branches/-b', ''); | |
646 | complete_url_ls_init($url, $_tags, '--tags/-t', 'tags/'); | |
647 | } | |
648 | ||
649 | sub multi_fetch { | |
650 | # try to do trunk first, since branches/tags | |
651 | # may be descended from it. | |
cf7424b0 EW |
652 | if (-e "$GIT_DIR/svn/trunk/info/url") { |
653 | fetch_child_id('trunk', @_); | |
9d55b41a EW |
654 | } |
655 | rec_fetch('', "$GIT_DIR/svn", @_); | |
656 | } | |
657 | ||
79bb8d88 EW |
658 | sub show_log { |
659 | my (@args) = @_; | |
660 | my ($r_min, $r_max); | |
661 | my $r_last = -1; # prevent dupes | |
662 | rload_authors() if $_authors; | |
663 | if (defined $TZ) { | |
664 | $ENV{TZ} = $TZ; | |
665 | } else { | |
666 | delete $ENV{TZ}; | |
667 | } | |
668 | if (defined $_revision) { | |
669 | if ($_revision =~ /^(\d+):(\d+)$/) { | |
670 | ($r_min, $r_max) = ($1, $2); | |
671 | } elsif ($_revision =~ /^\d+$/) { | |
672 | $r_min = $r_max = $_revision; | |
673 | } else { | |
674 | print STDERR "-r$_revision is not supported, use ", | |
675 | "standard \'git log\' arguments instead\n"; | |
676 | exit 1; | |
677 | } | |
678 | } | |
679 | ||
680 | my $pid = open(my $log,'-|'); | |
681 | defined $pid or croak $!; | |
682 | if (!$pid) { | |
c0d48222 | 683 | exec(git_svn_log_cmd($r_min,$r_max), @args) or croak $!; |
79bb8d88 EW |
684 | } |
685 | setup_pager(); | |
686 | my (@k, $c, $d); | |
c0d48222 | 687 | |
79bb8d88 EW |
688 | while (<$log>) { |
689 | if (/^commit ($sha1_short)/o) { | |
690 | my $cmt = $1; | |
c0d48222 | 691 | if ($c && cmt_showable($c) && $c->{r} != $r_last) { |
79bb8d88 EW |
692 | $r_last = $c->{r}; |
693 | process_commit($c, $r_min, $r_max, \@k) or | |
694 | goto out; | |
695 | } | |
696 | $d = undef; | |
697 | $c = { c => $cmt }; | |
698 | } elsif (/^author (.+) (\d+) ([\-\+]?\d+)$/) { | |
699 | get_author_info($c, $1, $2, $3); | |
700 | } elsif (/^(?:tree|parent|committer) /) { | |
701 | # ignore | |
702 | } elsif (/^:\d{6} \d{6} $sha1_short/o) { | |
703 | push @{$c->{raw}}, $_; | |
704 | } elsif (/^diff /) { | |
705 | $d = 1; | |
706 | push @{$c->{diff}}, $_; | |
707 | } elsif ($d) { | |
708 | push @{$c->{diff}}, $_; | |
709 | } elsif (/^ (git-svn-id:.+)$/) { | |
c0d48222 | 710 | (undef, $c->{r}, undef) = extract_metadata($1); |
79bb8d88 EW |
711 | } elsif (s/^ //) { |
712 | push @{$c->{l}}, $_; | |
713 | } | |
714 | } | |
715 | if ($c && defined $c->{r} && $c->{r} != $r_last) { | |
716 | $r_last = $c->{r}; | |
717 | process_commit($c, $r_min, $r_max, \@k); | |
718 | } | |
719 | if (@k) { | |
720 | my $swap = $r_max; | |
721 | $r_max = $r_min; | |
722 | $r_min = $swap; | |
723 | process_commit($_, $r_min, $r_max) foreach reverse @k; | |
724 | } | |
725 | out: | |
726 | close $log; | |
727 | print '-' x72,"\n" unless $_incremental || $_oneline; | |
728 | } | |
729 | ||
27e9fb8d EW |
730 | sub commit_diff_usage { |
731 | print STDERR "Usage: $0 commit-diff <tree-ish> <tree-ish> [<URL>]\n"; | |
732 | exit 1 | |
733 | } | |
734 | ||
735 | sub commit_diff { | |
736 | if (!$_use_lib) { | |
737 | print STDERR "commit-diff must be used with SVN libraries\n"; | |
738 | exit 1; | |
739 | } | |
740 | my $ta = shift or commit_diff_usage(); | |
741 | my $tb = shift or commit_diff_usage(); | |
742 | if (!eval { $SVN_URL = shift || file_to_s("$GIT_SVN_DIR/info/url") }) { | |
743 | print STDERR "Needed URL or usable git-svn id command-line\n"; | |
744 | commit_diff_usage(); | |
745 | } | |
746 | if (defined $_message && defined $_file) { | |
747 | print STDERR "Both --message/-m and --file/-F specified ", | |
748 | "for the commit message.\n", | |
749 | "I have no idea what you mean\n"; | |
750 | exit 1; | |
751 | } | |
752 | if (defined $_file) { | |
753 | $_message = file_to_s($_message); | |
754 | } else { | |
755 | $_message ||= get_commit_message($tb, | |
756 | "$GIT_DIR/.svn-commit.tmp.$$")->{msg}; | |
757 | } | |
758 | my $repo; | |
759 | ($repo, $SVN_PATH) = repo_path_split($SVN_URL); | |
760 | $SVN_LOG ||= libsvn_connect($repo); | |
761 | $SVN ||= libsvn_connect($repo); | |
762 | my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : (); | |
763 | my $ed = SVN::Git::Editor->new({ r => $SVN->get_latest_revnum, | |
764 | ra => $SVN, c => $tb, | |
765 | svn_path => $SVN_PATH | |
766 | }, | |
767 | $SVN->get_commit_editor($_message, | |
768 | sub {print "Committed $_[0]\n"},@lock) | |
769 | ); | |
770 | my $mods = libsvn_checkout_tree($ta, $tb, $ed); | |
771 | if (@$mods == 0) { | |
772 | print "No changes\n$ta == $tb\n"; | |
773 | $ed->abort_edit; | |
774 | } else { | |
775 | $ed->close_edit; | |
776 | } | |
777 | } | |
778 | ||
3397f9df EW |
779 | ########################### utility functions ######################### |
780 | ||
c0d48222 EW |
781 | sub cmt_showable { |
782 | my ($c) = @_; | |
783 | return 1 if defined $c->{r}; | |
784 | if ($c->{l} && $c->{l}->[-1] eq "...\n" && | |
785 | $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) { | |
786 | my @msg = safe_qx(qw/git-cat-file commit/, $c->{c}); | |
787 | shift @msg while ($msg[0] ne "\n"); | |
788 | shift @msg; | |
789 | @{$c->{l}} = grep !/^git-svn-id: /, @msg; | |
790 | ||
791 | (undef, $c->{r}, undef) = extract_metadata( | |
792 | (grep(/^git-svn-id: /, @msg))[-1]); | |
793 | } | |
794 | return defined $c->{r}; | |
795 | } | |
796 | ||
797 | sub git_svn_log_cmd { | |
798 | my ($r_min, $r_max) = @_; | |
799 | my @cmd = (qw/git-log --abbrev-commit --pretty=raw | |
800 | --default/, "refs/remotes/$GIT_SVN"); | |
801 | push @cmd, '--summary' if $_verbose; | |
802 | return @cmd unless defined $r_max; | |
803 | if ($r_max == $r_min) { | |
804 | push @cmd, '--max-count=1'; | |
805 | if (my $c = revdb_get($REVDB, $r_max)) { | |
806 | push @cmd, $c; | |
807 | } | |
808 | } else { | |
809 | my ($c_min, $c_max); | |
810 | $c_max = revdb_get($REVDB, $r_max); | |
811 | $c_min = revdb_get($REVDB, $r_min); | |
812 | if ($c_min && $c_max) { | |
813 | if ($r_max > $r_max) { | |
814 | push @cmd, "$c_min..$c_max"; | |
815 | } else { | |
816 | push @cmd, "$c_max..$c_min"; | |
817 | } | |
818 | } elsif ($r_max > $r_min) { | |
819 | push @cmd, $c_max; | |
820 | } else { | |
821 | push @cmd, $c_min; | |
822 | } | |
823 | } | |
824 | return @cmd; | |
825 | } | |
826 | ||
cf7424b0 EW |
827 | sub fetch_child_id { |
828 | my $id = shift; | |
829 | print "Fetching $id\n"; | |
830 | my $ref = "$GIT_DIR/refs/remotes/$id"; | |
a00439ac | 831 | defined(my $pid = open my $fh, '-|') or croak $!; |
cf7424b0 | 832 | if (!$pid) { |
a00439ac | 833 | $_repack = undef; |
cf7424b0 EW |
834 | $GIT_SVN = $ENV{GIT_SVN_ID} = $id; |
835 | init_vars(); | |
836 | fetch(@_); | |
837 | exit 0; | |
838 | } | |
cf7424b0 | 839 | while (<$fh>) { |
a00439ac EW |
840 | print $_; |
841 | check_repack() if (/^r\d+ = $sha1/); | |
cf7424b0 | 842 | } |
a00439ac | 843 | close $fh or croak $?; |
cf7424b0 EW |
844 | } |
845 | ||
9d55b41a EW |
846 | sub rec_fetch { |
847 | my ($pfx, $p, @args) = @_; | |
848 | my @dir; | |
849 | foreach (sort <$p/*>) { | |
850 | if (-r "$_/info/url") { | |
851 | $pfx .= '/' if $pfx && $pfx !~ m!/$!; | |
852 | my $id = $pfx . basename $_; | |
853 | next if $id eq 'trunk'; | |
cf7424b0 | 854 | fetch_child_id($id, @args); |
9d55b41a EW |
855 | } elsif (-d $_) { |
856 | push @dir, $_; | |
857 | } | |
858 | } | |
859 | foreach (@dir) { | |
860 | my $x = $_; | |
861 | $x =~ s!^\Q$GIT_DIR\E/svn/!!; | |
862 | rec_fetch($x, $_); | |
863 | } | |
864 | } | |
865 | ||
866 | sub complete_url_ls_init { | |
867 | my ($url, $var, $switch, $pfx) = @_; | |
868 | unless ($var) { | |
869 | print STDERR "W: $switch not specified\n"; | |
870 | return; | |
871 | } | |
872 | $var =~ s#/+$##; | |
873 | if ($var !~ m#^[a-z\+]+://#) { | |
874 | $var = '/' . $var if ($var !~ m#^/#); | |
875 | unless ($url) { | |
876 | print STDERR "E: '$var' is not a complete URL ", | |
877 | "and a separate URL is not specified\n"; | |
878 | exit 1; | |
879 | } | |
880 | $var = $url . $var; | |
881 | } | |
a5e0cedc EW |
882 | chomp(my @ls = $_use_lib ? libsvn_ls_fullurl($var) |
883 | : safe_qx(qw/svn ls --non-interactive/, $var)); | |
9d55b41a EW |
884 | my $old = $GIT_SVN; |
885 | defined(my $pid = fork) or croak $!; | |
886 | if (!$pid) { | |
887 | foreach my $u (map { "$var/$_" } (grep m!/$!, @ls)) { | |
888 | $u =~ s#/+$##; | |
889 | if ($u !~ m!\Q$var\E/(.+)$!) { | |
890 | print STDERR "W: Unrecognized URL: $u\n"; | |
891 | die "This should never happen\n"; | |
892 | } | |
893 | my $id = $pfx.$1; | |
894 | print "init $u => $id\n"; | |
895 | $GIT_SVN = $ENV{GIT_SVN_ID} = $id; | |
896 | init_vars(); | |
897 | init($u); | |
898 | } | |
899 | exit 0; | |
900 | } | |
901 | waitpid $pid, 0; | |
902 | croak $? if $?; | |
903 | } | |
904 | ||
905 | sub common_prefix { | |
906 | my $paths = shift; | |
907 | my %common; | |
908 | foreach (@$paths) { | |
909 | my @tmp = split m#/#, $_; | |
910 | my $p = ''; | |
911 | while (my $x = shift @tmp) { | |
912 | $p .= "/$x"; | |
913 | $common{$p} ||= 0; | |
914 | $common{$p}++; | |
915 | } | |
916 | } | |
917 | foreach (sort {length $b <=> length $a} keys %common) { | |
918 | if ($common{$_} == @$paths) { | |
919 | return $_; | |
920 | } | |
921 | } | |
922 | return ''; | |
923 | } | |
924 | ||
c1927a85 EW |
925 | # grafts set here are 'stronger' in that they're based on actual tree |
926 | # matches, and won't be deleted from merge-base checking in write_grafts() | |
927 | sub graft_tree_joins { | |
928 | my $grafts = shift; | |
929 | map_tree_joins() if (@_branch_from && !%tree_map); | |
930 | return unless %tree_map; | |
931 | ||
932 | git_svn_each(sub { | |
933 | my $i = shift; | |
934 | defined(my $pid = open my $fh, '-|') or croak $!; | |
935 | if (!$pid) { | |
936 | exec qw/git-rev-list --pretty=raw/, | |
937 | "refs/remotes/$i" or croak $!; | |
938 | } | |
939 | while (<$fh>) { | |
940 | next unless /^commit ($sha1)$/o; | |
941 | my $c = $1; | |
942 | my ($t) = (<$fh> =~ /^tree ($sha1)$/o); | |
943 | next unless $tree_map{$t}; | |
944 | ||
945 | my $l; | |
946 | do { | |
947 | $l = readline $fh; | |
948 | } until ($l =~ /^committer (?:.+) (\d+) ([\-\+]?\d+)$/); | |
949 | ||
950 | my ($s, $tz) = ($1, $2); | |
951 | if ($tz =~ s/^\+//) { | |
952 | $s += tz_to_s_offset($tz); | |
953 | } elsif ($tz =~ s/^\-//) { | |
954 | $s -= tz_to_s_offset($tz); | |
955 | } | |
956 | ||
957 | my ($url_a, $r_a, $uuid_a) = cmt_metadata($c); | |
958 | ||
959 | foreach my $p (@{$tree_map{$t}}) { | |
960 | next if $p eq $c; | |
961 | my $mb = eval { | |
962 | safe_qx('git-merge-base', $c, $p) | |
963 | }; | |
964 | next unless ($@ || $?); | |
965 | if (defined $r_a) { | |
966 | # see if SVN says it's a relative | |
967 | my ($url_b, $r_b, $uuid_b) = | |
968 | cmt_metadata($p); | |
969 | next if (defined $url_b && | |
970 | defined $url_a && | |
971 | ($url_a eq $url_b) && | |
972 | ($uuid_a eq $uuid_b)); | |
973 | if ($uuid_a eq $uuid_b) { | |
974 | if ($r_b < $r_a) { | |
975 | $grafts->{$c}->{$p} = 2; | |
976 | next; | |
977 | } elsif ($r_b > $r_a) { | |
978 | $grafts->{$p}->{$c} = 2; | |
979 | next; | |
980 | } | |
981 | } | |
982 | } | |
983 | my $ct = get_commit_time($p); | |
984 | if ($ct < $s) { | |
985 | $grafts->{$c}->{$p} = 2; | |
986 | } elsif ($ct > $s) { | |
987 | $grafts->{$p}->{$c} = 2; | |
988 | } | |
989 | # what should we do when $ct == $s ? | |
990 | } | |
991 | } | |
992 | close $fh or croak $?; | |
993 | }); | |
994 | } | |
995 | ||
9d55b41a | 996 | # this isn't funky-filename safe, but good enough for now... |
a5e0cedc | 997 | sub graft_file_copy_cmd { |
9d55b41a EW |
998 | my ($grafts, $l_map, $u) = @_; |
999 | my $paths = $l_map->{$u}; | |
1000 | my $pfx = common_prefix([keys %$paths]); | |
42d32870 | 1001 | $SVN_URL ||= $u.$pfx; |
9d55b41a EW |
1002 | my $pid = open my $fh, '-|'; |
1003 | defined $pid or croak $!; | |
1004 | unless ($pid) { | |
a5e0cedc EW |
1005 | my @exec = qw/svn log -v/; |
1006 | push @exec, "-r$_revision" if defined $_revision; | |
1007 | exec @exec, $u.$pfx or croak $!; | |
9d55b41a EW |
1008 | } |
1009 | my ($r, $mp) = (undef, undef); | |
1010 | while (<$fh>) { | |
1011 | chomp; | |
1012 | if (/^\-{72}$/) { | |
1013 | $mp = $r = undef; | |
1014 | } elsif (/^r(\d+) \| /) { | |
1015 | $r = $1 unless defined $r; | |
1016 | } elsif (/^Changed paths:/) { | |
1017 | $mp = 1; | |
1018 | } elsif ($mp && m#^ [AR] /(\S.*?) \(from /(\S+?):(\d+)\)$#) { | |
9d55b41a | 1019 | my ($p1, $p0, $r0) = ($1, $2, $3); |
a5e0cedc | 1020 | my $c = find_graft_path_commit($paths, $p1, $r); |
9d55b41a | 1021 | next unless $c; |
a5e0cedc | 1022 | find_graft_path_parents($grafts, $paths, $c, $p0, $r0); |
9d55b41a EW |
1023 | } |
1024 | } | |
1025 | } | |
1026 | ||
a5e0cedc EW |
1027 | sub graft_file_copy_lib { |
1028 | my ($grafts, $l_map, $u) = @_; | |
1029 | my $tree_paths = $l_map->{$u}; | |
1030 | my $pfx = common_prefix([keys %$tree_paths]); | |
1031 | my ($repo, $path) = repo_path_split($u.$pfx); | |
1032 | $SVN_LOG ||= libsvn_connect($repo); | |
1033 | $SVN ||= libsvn_connect($repo); | |
1034 | ||
1035 | my ($base, $head) = libsvn_parse_revision(); | |
1036 | my $inc = 1000; | |
1037 | my ($min, $max) = ($base, $head < $base+$inc ? $head : $base+$inc); | |
42d32870 EW |
1038 | my $eh = $SVN::Error::handler; |
1039 | $SVN::Error::handler = \&libsvn_skip_unknown_revs; | |
a5e0cedc EW |
1040 | while (1) { |
1041 | my $pool = SVN::Pool->new; | |
dc62e25c | 1042 | libsvn_get_log($SVN_LOG, "/$path", $min, $max, 0, 1, 1, |
a5e0cedc EW |
1043 | sub { |
1044 | libsvn_graft_file_copies($grafts, $tree_paths, | |
1045 | $path, @_); | |
1046 | }, $pool); | |
1047 | $pool->clear; | |
1048 | last if ($max >= $head); | |
1049 | $min = $max + 1; | |
1050 | $max += $inc; | |
1051 | $max = $head if ($max > $head); | |
1052 | } | |
42d32870 | 1053 | $SVN::Error::handler = $eh; |
a5e0cedc EW |
1054 | } |
1055 | ||
9d55b41a EW |
1056 | sub process_merge_msg_matches { |
1057 | my ($grafts, $l_map, $u, $p, $c, @matches) = @_; | |
1058 | my (@strong, @weak); | |
1059 | foreach (@matches) { | |
1060 | # merging with ourselves is not interesting | |
1061 | next if $_ eq $p; | |
1062 | if ($l_map->{$u}->{$_}) { | |
1063 | push @strong, $_; | |
1064 | } else { | |
1065 | push @weak, $_; | |
1066 | } | |
1067 | } | |
1068 | foreach my $w (@weak) { | |
1069 | last if @strong; | |
1070 | # no exact match, use branch name as regexp. | |
1071 | my $re = qr/\Q$w\E/i; | |
1072 | foreach (keys %{$l_map->{$u}}) { | |
1073 | if (/$re/) { | |
c1927a85 | 1074 | push @strong, $l_map->{$u}->{$_}; |
9d55b41a EW |
1075 | last; |
1076 | } | |
1077 | } | |
1078 | last if @strong; | |
1079 | $w = basename($w); | |
1080 | $re = qr/\Q$w\E/i; | |
1081 | foreach (keys %{$l_map->{$u}}) { | |
1082 | if (/$re/) { | |
c1927a85 | 1083 | push @strong, $l_map->{$u}->{$_}; |
9d55b41a EW |
1084 | last; |
1085 | } | |
1086 | } | |
1087 | } | |
1088 | my ($rev) = ($c->{m} =~ /^git-svn-id:\s(?:\S+?)\@(\d+) | |
1089 | \s(?:[a-f\d\-]+)$/xsm); | |
1090 | unless (defined $rev) { | |
1091 | ($rev) = ($c->{m} =~/^git-svn-id:\s(\d+) | |
1092 | \@(?:[a-f\d\-]+)/xsm); | |
1093 | return unless defined $rev; | |
1094 | } | |
1095 | foreach my $m (@strong) { | |
c1927a85 | 1096 | my ($r0, $s0) = find_rev_before($rev, $m, 1); |
9d55b41a EW |
1097 | $grafts->{$c->{c}}->{$s0} = 1 if defined $s0; |
1098 | } | |
1099 | } | |
1100 | ||
1101 | sub graft_merge_msg { | |
1102 | my ($grafts, $l_map, $u, $p, @re) = @_; | |
1103 | ||
1104 | my $x = $l_map->{$u}->{$p}; | |
1105 | my $rl = rev_list_raw($x); | |
1106 | while (my $c = next_rev_list_entry($rl)) { | |
1107 | foreach my $re (@re) { | |
1108 | my (@br) = ($c->{m} =~ /$re/g); | |
1109 | next unless @br; | |
1110 | process_merge_msg_matches($grafts,$l_map,$u,$p,$c,@br); | |
1111 | } | |
1112 | } | |
1113 | } | |
1114 | ||
1d52aba8 EW |
1115 | sub read_uuid { |
1116 | return if $SVN_UUID; | |
a5e0cedc EW |
1117 | if ($_use_lib) { |
1118 | my $pool = SVN::Pool->new; | |
1119 | $SVN_UUID = $SVN->get_uuid($pool); | |
1120 | $pool->clear; | |
1121 | } else { | |
1122 | my $info = shift || svn_info('.'); | |
1123 | $SVN_UUID = $info->{'Repository UUID'} or | |
1d52aba8 | 1124 | croak "Repository UUID unreadable\n"; |
a5e0cedc | 1125 | } |
883d0a78 EW |
1126 | } |
1127 | ||
1128 | sub quiet_run { | |
1129 | my $pid = fork; | |
1130 | defined $pid or croak $!; | |
1131 | if (!$pid) { | |
1132 | open my $null, '>', '/dev/null' or croak $!; | |
1133 | open STDERR, '>&', $null or croak $!; | |
1134 | open STDOUT, '>&', $null or croak $!; | |
1135 | exec @_ or croak $!; | |
1136 | } | |
1137 | waitpid $pid, 0; | |
1138 | return $?; | |
1139 | } | |
1140 | ||
1141 | sub repo_path_split { | |
1142 | my $full_url = shift; | |
1143 | $full_url =~ s#/+$##; | |
1144 | ||
1145 | foreach (@repo_path_split_cache) { | |
1146 | if ($full_url =~ s#$_##) { | |
1147 | my $u = $1; | |
1148 | $full_url =~ s#^/+##; | |
1149 | return ($u, $full_url); | |
1150 | } | |
1151 | } | |
1152 | ||
1153 | my ($url, $path) = ($full_url =~ m!^([a-z\+]+://[^/]*)(.*)$!i); | |
1154 | $path =~ s#^/+##; | |
1155 | my @paths = split(m#/+#, $path); | |
1156 | ||
a5e0cedc EW |
1157 | if ($_use_lib) { |
1158 | while (1) { | |
1159 | $SVN = libsvn_connect($url); | |
1160 | last if (defined $SVN && | |
1161 | defined eval { $SVN->get_latest_revnum }); | |
1162 | my $n = shift @paths || last; | |
1163 | $url .= "/$n"; | |
1164 | } | |
1165 | } else { | |
1166 | while (quiet_run(qw/svn ls --non-interactive/, $url)) { | |
1167 | my $n = shift @paths || last; | |
1168 | $url .= "/$n"; | |
1169 | } | |
883d0a78 EW |
1170 | } |
1171 | push @repo_path_split_cache, qr/^(\Q$url\E)/; | |
9d55b41a | 1172 | $path = join('/',@paths); |
883d0a78 | 1173 | return ($url, $path); |
1d52aba8 EW |
1174 | } |
1175 | ||
3397f9df EW |
1176 | sub setup_git_svn { |
1177 | defined $SVN_URL or croak "SVN repository location required\n"; | |
1178 | unless (-d $GIT_DIR) { | |
1179 | croak "GIT_DIR=$GIT_DIR does not exist!\n"; | |
1180 | } | |
883d0a78 EW |
1181 | mkpath([$GIT_SVN_DIR]); |
1182 | mkpath(["$GIT_SVN_DIR/info"]); | |
42d32870 EW |
1183 | open my $fh, '>>',$REVDB or croak $!; |
1184 | close $fh; | |
883d0a78 | 1185 | s_to_file($SVN_URL,"$GIT_SVN_DIR/info/url"); |
3397f9df | 1186 | |
3397f9df EW |
1187 | } |
1188 | ||
1189 | sub assert_svn_wc_clean { | |
a5e0cedc | 1190 | return if $_use_lib; |
36f5b1f0 | 1191 | my ($svn_rev) = @_; |
3397f9df | 1192 | croak "$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/); |
ce4c8b24 EW |
1193 | my $lcr = svn_info('.')->{'Last Changed Rev'}; |
1194 | if ($svn_rev != $lcr) { | |
1195 | print STDERR "Checking for copy-tree ... "; | |
ce4c8b24 EW |
1196 | my @diff = grep(/^Index: /,(safe_qx(qw(svn diff), |
1197 | "-r$lcr:$svn_rev"))); | |
1198 | if (@diff) { | |
1199 | croak "Nope! Expected r$svn_rev, got r$lcr\n"; | |
1200 | } else { | |
1201 | print STDERR "OK!\n"; | |
1202 | } | |
3397f9df EW |
1203 | } |
1204 | my @status = grep(!/^Performing status on external/,(`svn status`)); | |
1205 | @status = grep(!/^\s*$/,@status); | |
1206 | if (scalar @status) { | |
1207 | print STDERR "Tree ($SVN_WC) is not clean:\n"; | |
1208 | print STDERR $_ foreach @status; | |
1209 | croak; | |
1210 | } | |
cf52b8f0 EW |
1211 | } |
1212 | ||
a5e0cedc | 1213 | sub get_tree_from_treeish { |
cf52b8f0 EW |
1214 | my ($treeish) = @_; |
1215 | croak "Not a sha1: $treeish\n" unless $treeish =~ /^$sha1$/o; | |
1216 | chomp(my $type = `git-cat-file -t $treeish`); | |
1217 | my $expected; | |
1218 | while ($type eq 'tag') { | |
1219 | chomp(($treeish, $type) = `git-cat-file tag $treeish`); | |
1220 | } | |
1221 | if ($type eq 'commit') { | |
1222 | $expected = (grep /^tree /,`git-cat-file commit $treeish`)[0]; | |
1223 | ($expected) = ($expected =~ /^tree ($sha1)$/); | |
1224 | die "Unable to get tree from $treeish\n" unless $expected; | |
1225 | } elsif ($type eq 'tree') { | |
1226 | $expected = $treeish; | |
1227 | } else { | |
1228 | die "$treeish is a $type, expected tree, tag or commit\n"; | |
1229 | } | |
a5e0cedc EW |
1230 | return $expected; |
1231 | } | |
1232 | ||
1233 | sub assert_tree { | |
1234 | return if $_use_lib; | |
1235 | my ($treeish) = @_; | |
1236 | my $expected = get_tree_from_treeish($treeish); | |
cf52b8f0 | 1237 | |
cf52b8f0 EW |
1238 | my $tmpindex = $GIT_SVN_INDEX.'.assert-tmp'; |
1239 | if (-e $tmpindex) { | |
1240 | unlink $tmpindex or croak $!; | |
1241 | } | |
a5e0cedc | 1242 | my $old_index = set_index($tmpindex); |
36f5b1f0 | 1243 | index_changes(1); |
cf52b8f0 | 1244 | chomp(my $tree = `git-write-tree`); |
a5e0cedc | 1245 | restore_index($old_index); |
cf52b8f0 EW |
1246 | if ($tree ne $expected) { |
1247 | croak "Tree mismatch, Got: $tree, Expected: $expected\n"; | |
3397f9df | 1248 | } |
36f5b1f0 | 1249 | unlink $tmpindex; |
3397f9df EW |
1250 | } |
1251 | ||
1252 | sub parse_diff_tree { | |
1253 | my $diff_fh = shift; | |
1254 | local $/ = "\0"; | |
1255 | my $state = 'meta'; | |
1256 | my @mods; | |
1257 | while (<$diff_fh>) { | |
1258 | chomp $_; # this gets rid of the trailing "\0" | |
3397f9df EW |
1259 | if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s |
1260 | $sha1\s($sha1)\s([MTCRAD])\d*$/xo) { | |
1261 | push @mods, { mode_a => $1, mode_b => $2, | |
1262 | sha1_b => $3, chg => $4 }; | |
1263 | if ($4 =~ /^(?:C|R)$/) { | |
1264 | $state = 'file_a'; | |
1265 | } else { | |
1266 | $state = 'file_b'; | |
1267 | } | |
1268 | } elsif ($state eq 'file_a') { | |
cf52b8f0 | 1269 | my $x = $mods[$#mods] or croak "Empty array\n"; |
3397f9df | 1270 | if ($x->{chg} !~ /^(?:C|R)$/) { |
cf52b8f0 | 1271 | croak "Error parsing $_, $x->{chg}\n"; |
3397f9df EW |
1272 | } |
1273 | $x->{file_a} = $_; | |
1274 | $state = 'file_b'; | |
1275 | } elsif ($state eq 'file_b') { | |
cf52b8f0 | 1276 | my $x = $mods[$#mods] or croak "Empty array\n"; |
3397f9df | 1277 | if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) { |
cf52b8f0 | 1278 | croak "Error parsing $_, $x->{chg}\n"; |
3397f9df EW |
1279 | } |
1280 | if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) { | |
cf52b8f0 | 1281 | croak "Error parsing $_, $x->{chg}\n"; |
3397f9df EW |
1282 | } |
1283 | $x->{file_b} = $_; | |
1284 | $state = 'meta'; | |
1285 | } else { | |
cf52b8f0 | 1286 | croak "Error parsing $_\n"; |
3397f9df EW |
1287 | } |
1288 | } | |
cf7424b0 | 1289 | close $diff_fh or croak $?; |
cf52b8f0 | 1290 | |
3397f9df EW |
1291 | return \@mods; |
1292 | } | |
1293 | ||
1294 | sub svn_check_prop_executable { | |
1295 | my $m = shift; | |
cf52b8f0 EW |
1296 | return if -l $m->{file_b}; |
1297 | if ($m->{mode_b} =~ /755$/) { | |
1298 | chmod((0755 &~ umask),$m->{file_b}) or croak $!; | |
1299 | if ($m->{mode_a} !~ /755$/) { | |
1300 | sys(qw(svn propset svn:executable 1), $m->{file_b}); | |
1301 | } | |
1302 | -x $m->{file_b} or croak "$m->{file_b} is not executable!\n"; | |
3397f9df EW |
1303 | } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) { |
1304 | sys(qw(svn propdel svn:executable), $m->{file_b}); | |
cf52b8f0 EW |
1305 | chmod((0644 &~ umask),$m->{file_b}) or croak $!; |
1306 | -x $m->{file_b} and croak "$m->{file_b} is executable!\n"; | |
3397f9df EW |
1307 | } |
1308 | } | |
1309 | ||
1310 | sub svn_ensure_parent_path { | |
1311 | my $dir_b = dirname(shift); | |
1312 | svn_ensure_parent_path($dir_b) if ($dir_b ne File::Spec->curdir); | |
1313 | mkpath([$dir_b]) unless (-d $dir_b); | |
1314 | sys(qw(svn add -N), $dir_b) unless (-d "$dir_b/.svn"); | |
1315 | } | |
1316 | ||
cf52b8f0 EW |
1317 | sub precommit_check { |
1318 | my $mods = shift; | |
1319 | my (%rm_file, %rmdir_check, %added_check); | |
1320 | ||
1321 | my %o = ( D => 0, R => 1, C => 2, A => 3, M => 3, T => 3 ); | |
1322 | foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) { | |
1323 | if ($m->{chg} eq 'R') { | |
1324 | if (-d $m->{file_b}) { | |
1325 | err_dir_to_file("$m->{file_a} => $m->{file_b}"); | |
1326 | } | |
1327 | # dir/$file => dir/file/$file | |
1328 | my $dirname = dirname($m->{file_b}); | |
1329 | while ($dirname ne File::Spec->curdir) { | |
1330 | if ($dirname ne $m->{file_a}) { | |
1331 | $dirname = dirname($dirname); | |
1332 | next; | |
1333 | } | |
1334 | err_file_to_dir("$m->{file_a} => $m->{file_b}"); | |
1335 | } | |
1336 | # baz/zzz => baz (baz is a file) | |
1337 | $dirname = dirname($m->{file_a}); | |
1338 | while ($dirname ne File::Spec->curdir) { | |
1339 | if ($dirname ne $m->{file_b}) { | |
1340 | $dirname = dirname($dirname); | |
1341 | next; | |
1342 | } | |
1343 | err_dir_to_file("$m->{file_a} => $m->{file_b}"); | |
1344 | } | |
1345 | } | |
1346 | if ($m->{chg} =~ /^(D|R)$/) { | |
1347 | my $t = $1 eq 'D' ? 'file_b' : 'file_a'; | |
1348 | $rm_file{ $m->{$t} } = 1; | |
1349 | my $dirname = dirname( $m->{$t} ); | |
1350 | my $basename = basename( $m->{$t} ); | |
1351 | $rmdir_check{$dirname}->{$basename} = 1; | |
1352 | } elsif ($m->{chg} =~ /^(?:A|C)$/) { | |
1353 | if (-d $m->{file_b}) { | |
1354 | err_dir_to_file($m->{file_b}); | |
1355 | } | |
1356 | my $dirname = dirname( $m->{file_b} ); | |
1357 | my $basename = basename( $m->{file_b} ); | |
1358 | $added_check{$dirname}->{$basename} = 1; | |
1359 | while ($dirname ne File::Spec->curdir) { | |
1360 | if ($rm_file{$dirname}) { | |
1361 | err_file_to_dir($m->{file_b}); | |
1362 | } | |
1363 | $dirname = dirname $dirname; | |
1364 | } | |
1365 | } | |
1366 | } | |
1367 | return (\%rmdir_check, \%added_check); | |
1368 | ||
1369 | sub err_dir_to_file { | |
1370 | my $file = shift; | |
1371 | print STDERR "Node change from directory to file ", | |
1372 | "is not supported by Subversion: ",$file,"\n"; | |
1373 | exit 1; | |
1374 | } | |
1375 | sub err_file_to_dir { | |
1376 | my $file = shift; | |
1377 | print STDERR "Node change from file to directory ", | |
1378 | "is not supported by Subversion: ",$file,"\n"; | |
1379 | exit 1; | |
1380 | } | |
1381 | } | |
1382 | ||
a5e0cedc EW |
1383 | |
1384 | sub get_diff { | |
42d32870 | 1385 | my ($from, $treeish) = @_; |
36f5b1f0 | 1386 | assert_tree($from); |
ac8e0b91 | 1387 | print "diff-tree $from $treeish\n"; |
3397f9df EW |
1388 | my $pid = open my $diff_fh, '-|'; |
1389 | defined $pid or croak $!; | |
1390 | if ($pid == 0) { | |
162f4129 EW |
1391 | my @diff_tree = qw(git-diff-tree -z -r); |
1392 | if ($_cp_similarity) { | |
1393 | push @diff_tree, "-C$_cp_similarity"; | |
1394 | } else { | |
1395 | push @diff_tree, '-C'; | |
1396 | } | |
72942938 EW |
1397 | push @diff_tree, '--find-copies-harder' if $_find_copies_harder; |
1398 | push @diff_tree, "-l$_l" if defined $_l; | |
cf52b8f0 | 1399 | exec(@diff_tree, $from, $treeish) or croak $!; |
3397f9df | 1400 | } |
a5e0cedc EW |
1401 | return parse_diff_tree($diff_fh); |
1402 | } | |
1403 | ||
1404 | sub svn_checkout_tree { | |
42d32870 EW |
1405 | my ($from, $treeish) = @_; |
1406 | my $mods = get_diff($from->{commit}, $treeish); | |
a5e0cedc | 1407 | return $mods unless (scalar @$mods); |
cf52b8f0 EW |
1408 | my ($rm, $add) = precommit_check($mods); |
1409 | ||
1410 | my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 ); | |
1411 | foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) { | |
3397f9df EW |
1412 | if ($m->{chg} eq 'C') { |
1413 | svn_ensure_parent_path( $m->{file_b} ); | |
1414 | sys(qw(svn cp), $m->{file_a}, $m->{file_b}); | |
cf52b8f0 | 1415 | apply_mod_line_blob($m); |
3397f9df EW |
1416 | svn_check_prop_executable($m); |
1417 | } elsif ($m->{chg} eq 'D') { | |
3397f9df EW |
1418 | sys(qw(svn rm --force), $m->{file_b}); |
1419 | } elsif ($m->{chg} eq 'R') { | |
1420 | svn_ensure_parent_path( $m->{file_b} ); | |
1421 | sys(qw(svn mv --force), $m->{file_a}, $m->{file_b}); | |
cf52b8f0 | 1422 | apply_mod_line_blob($m); |
3397f9df | 1423 | svn_check_prop_executable($m); |
3397f9df | 1424 | } elsif ($m->{chg} eq 'M') { |
cf52b8f0 | 1425 | apply_mod_line_blob($m); |
3397f9df EW |
1426 | svn_check_prop_executable($m); |
1427 | } elsif ($m->{chg} eq 'T') { | |
1428 | sys(qw(svn rm --force),$m->{file_b}); | |
cf52b8f0 | 1429 | apply_mod_line_blob($m); |
0e8a002c | 1430 | sys(qw(svn add), $m->{file_b}); |
cf52b8f0 | 1431 | svn_check_prop_executable($m); |
3397f9df EW |
1432 | } elsif ($m->{chg} eq 'A') { |
1433 | svn_ensure_parent_path( $m->{file_b} ); | |
cf52b8f0 | 1434 | apply_mod_line_blob($m); |
0e8a002c | 1435 | sys(qw(svn add), $m->{file_b}); |
cf52b8f0 | 1436 | svn_check_prop_executable($m); |
3397f9df EW |
1437 | } else { |
1438 | croak "Invalid chg: $m->{chg}\n"; | |
1439 | } | |
1440 | } | |
cf52b8f0 EW |
1441 | |
1442 | assert_tree($treeish); | |
1443 | if ($_rmdir) { # remove empty directories | |
1444 | handle_rmdir($rm, $add); | |
1445 | } | |
1446 | assert_tree($treeish); | |
1447 | return $mods; | |
1448 | } | |
1449 | ||
a5e0cedc | 1450 | sub libsvn_checkout_tree { |
42d32870 EW |
1451 | my ($from, $treeish, $ed) = @_; |
1452 | my $mods = get_diff($from, $treeish); | |
a5e0cedc EW |
1453 | return $mods unless (scalar @$mods); |
1454 | my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 ); | |
1455 | foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) { | |
1456 | my $f = $m->{chg}; | |
1457 | if (defined $o{$f}) { | |
80f50749 | 1458 | $ed->$f($m, $_q); |
a5e0cedc EW |
1459 | } else { |
1460 | croak "Invalid change type: $f\n"; | |
1461 | } | |
1462 | } | |
80f50749 | 1463 | $ed->rmdirs($_q) if $_rmdir; |
a5e0cedc EW |
1464 | return $mods; |
1465 | } | |
1466 | ||
cf52b8f0 EW |
1467 | # svn ls doesn't work with respect to the current working tree, but what's |
1468 | # in the repository. There's not even an option for it... *sigh* | |
1469 | # (added files don't show up and removed files remain in the ls listing) | |
1470 | sub svn_ls_current { | |
1471 | my ($dir, $rm, $add) = @_; | |
1472 | chomp(my @ls = safe_qx('svn','ls',$dir)); | |
1473 | my @ret = (); | |
1474 | foreach (@ls) { | |
1475 | s#/$##; # trailing slashes are evil | |
1476 | push @ret, $_ unless $rm->{$dir}->{$_}; | |
1477 | } | |
1478 | if (exists $add->{$dir}) { | |
1479 | push @ret, keys %{$add->{$dir}}; | |
1480 | } | |
1481 | return \@ret; | |
1482 | } | |
1483 | ||
1484 | sub handle_rmdir { | |
1485 | my ($rm, $add) = @_; | |
1486 | ||
1487 | foreach my $dir (sort {length $b <=> length $a} keys %$rm) { | |
1488 | my $ls = svn_ls_current($dir, $rm, $add); | |
1489 | next if (scalar @$ls); | |
1490 | sys(qw(svn rm --force),$dir); | |
1491 | ||
1492 | my $dn = dirname $dir; | |
1493 | $rm->{ $dn }->{ basename $dir } = 1; | |
1494 | $ls = svn_ls_current($dn, $rm, $add); | |
1495 | while (scalar @$ls == 0 && $dn ne File::Spec->curdir) { | |
1496 | sys(qw(svn rm --force),$dn); | |
1497 | $dir = basename $dn; | |
1498 | $dn = dirname $dn; | |
1499 | $rm->{ $dn }->{ $dir } = 1; | |
1500 | $ls = svn_ls_current($dn, $rm, $add); | |
3397f9df EW |
1501 | } |
1502 | } | |
1503 | } | |
1504 | ||
a5e0cedc EW |
1505 | sub get_commit_message { |
1506 | my ($commit, $commit_msg) = (@_); | |
e17512f3 | 1507 | my %log_msg = ( msg => '' ); |
ac8e0b91 | 1508 | open my $msg, '>', $commit_msg or croak $!; |
3397f9df EW |
1509 | |
1510 | chomp(my $type = `git-cat-file -t $commit`); | |
1511 | if ($type eq 'commit') { | |
1512 | my $pid = open my $msg_fh, '-|'; | |
1513 | defined $pid or croak $!; | |
1514 | ||
1515 | if ($pid == 0) { | |
1516 | exec(qw(git-cat-file commit), $commit) or croak $!; | |
1517 | } | |
1518 | my $in_msg = 0; | |
1519 | while (<$msg_fh>) { | |
1520 | if (!$in_msg) { | |
1521 | $in_msg = 1 if (/^\s*$/); | |
df746c5a EW |
1522 | } elsif (/^git-svn-id: /) { |
1523 | # skip this, we regenerate the correct one | |
1524 | # on re-fetch anyways | |
3397f9df EW |
1525 | } else { |
1526 | print $msg $_ or croak $!; | |
1527 | } | |
1528 | } | |
cf7424b0 | 1529 | close $msg_fh or croak $?; |
3397f9df EW |
1530 | } |
1531 | close $msg or croak $!; | |
1532 | ||
1533 | if ($_edit || ($type eq 'tree')) { | |
1534 | my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi'; | |
1535 | system($editor, $commit_msg); | |
1536 | } | |
ac8e0b91 EW |
1537 | |
1538 | # file_to_s removes all trailing newlines, so just use chomp() here: | |
1539 | open $msg, '<', $commit_msg or croak $!; | |
1540 | { local $/; chomp($log_msg{msg} = <$msg>); } | |
1541 | close $msg or croak $!; | |
1542 | ||
a5e0cedc EW |
1543 | return \%log_msg; |
1544 | } | |
1545 | ||
27e9fb8d EW |
1546 | sub set_svn_commit_env { |
1547 | if (defined $LC_ALL) { | |
1548 | $ENV{LC_ALL} = $LC_ALL; | |
1549 | } else { | |
1550 | delete $ENV{LC_ALL}; | |
1551 | } | |
1552 | } | |
1553 | ||
a5e0cedc | 1554 | sub svn_commit_tree { |
42d32870 | 1555 | my ($last, $commit) = @_; |
a5e0cedc EW |
1556 | my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$"; |
1557 | my $log_msg = get_commit_message($commit, $commit_msg); | |
1558 | my ($oneline) = ($log_msg->{msg} =~ /([^\n\r]+)/); | |
ac8e0b91 EW |
1559 | print "Committing $commit: $oneline\n"; |
1560 | ||
27e9fb8d | 1561 | set_svn_commit_env(); |
3397f9df | 1562 | my @ci_output = safe_qx(qw(svn commit -F),$commit_msg); |
ce475dfc | 1563 | $ENV{LC_ALL} = 'C'; |
3397f9df | 1564 | unlink $commit_msg; |
ce475dfc EW |
1565 | my ($committed) = ($ci_output[$#ci_output] =~ /(\d+)/); |
1566 | if (!defined $committed) { | |
1567 | my $out = join("\n",@ci_output); | |
1568 | print STDERR "W: Trouble parsing \`svn commit' output:\n\n", | |
1569 | $out, "\n\nAssuming English locale..."; | |
1570 | ($committed) = ($out =~ /^Committed revision \d+\./sm); | |
1571 | defined $committed or die " FAILED!\n", | |
3397f9df | 1572 | "Commit output failed to parse committed revision!\n", |
ce475dfc EW |
1573 | print STDERR " OK\n"; |
1574 | } | |
3397f9df | 1575 | |
e17512f3 | 1576 | my @svn_up = qw(svn up); |
3397f9df | 1577 | push @svn_up, '--ignore-externals' unless $_no_ignore_ext; |
42d32870 | 1578 | if ($_optimize_commits && ($committed == ($last->{revision} + 1))) { |
ce475dfc | 1579 | push @svn_up, "-r$committed"; |
e17512f3 EW |
1580 | sys(@svn_up); |
1581 | my $info = svn_info('.'); | |
1582 | my $date = $info->{'Last Changed Date'} or die "Missing date\n"; | |
ce475dfc EW |
1583 | if ($info->{'Last Changed Rev'} != $committed) { |
1584 | croak "$info->{'Last Changed Rev'} != $committed\n" | |
e17512f3 EW |
1585 | } |
1586 | my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~ | |
1587 | /(\d{4})\-(\d\d)\-(\d\d)\s | |
1588 | (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x) | |
1589 | or croak "Failed to parse date: $date\n"; | |
a5e0cedc EW |
1590 | $log_msg->{date} = "$tz $Y-$m-$d $H:$M:$S"; |
1591 | $log_msg->{author} = $info->{'Last Changed Author'}; | |
1592 | $log_msg->{revision} = $committed; | |
1593 | $log_msg->{msg} .= "\n"; | |
42d32870 EW |
1594 | $log_msg->{parents} = [ $last->{commit} ]; |
1595 | $log_msg->{commit} = git_commit($log_msg, $commit); | |
1596 | return $log_msg; | |
e17512f3 EW |
1597 | } |
1598 | # resync immediately | |
42d32870 | 1599 | push @svn_up, "-r$last->{revision}"; |
3397f9df | 1600 | sys(@svn_up); |
42d32870 | 1601 | return fetch("$committed=$commit"); |
3397f9df EW |
1602 | } |
1603 | ||
9d55b41a EW |
1604 | sub rev_list_raw { |
1605 | my (@args) = @_; | |
1606 | my $pid = open my $fh, '-|'; | |
1607 | defined $pid or croak $!; | |
1608 | if (!$pid) { | |
1609 | exec(qw/git-rev-list --pretty=raw/, @args) or croak $!; | |
1610 | } | |
1611 | return { fh => $fh, t => { } }; | |
1612 | } | |
1613 | ||
1614 | sub next_rev_list_entry { | |
1615 | my $rl = shift; | |
1616 | my $fh = $rl->{fh}; | |
1617 | my $x = $rl->{t}; | |
1618 | while (<$fh>) { | |
1619 | if (/^commit ($sha1)$/o) { | |
1620 | if ($x->{c}) { | |
1621 | $rl->{t} = { c => $1 }; | |
1622 | return $x; | |
1623 | } else { | |
1624 | $x->{c} = $1; | |
1625 | } | |
1626 | } elsif (/^parent ($sha1)$/o) { | |
1627 | $x->{p}->{$1} = 1; | |
1628 | } elsif (s/^ //) { | |
1629 | $x->{m} ||= ''; | |
1630 | $x->{m} .= $_; | |
1631 | } | |
1632 | } | |
1633 | return ($x != $rl->{t}) ? $x : undef; | |
1634 | } | |
1635 | ||
03823184 EW |
1636 | # read the entire log into a temporary file (which is removed ASAP) |
1637 | # and store the file handle + parser state | |
3397f9df EW |
1638 | sub svn_log_raw { |
1639 | my (@log_args) = @_; | |
03823184 EW |
1640 | my $log_fh = IO::File->new_tmpfile or croak $!; |
1641 | my $pid = fork; | |
3397f9df | 1642 | defined $pid or croak $!; |
03823184 EW |
1643 | if (!$pid) { |
1644 | open STDOUT, '>&', $log_fh or croak $!; | |
3397f9df EW |
1645 | exec (qw(svn log), @log_args) or croak $! |
1646 | } | |
03823184 | 1647 | waitpid $pid, 0; |
b8c92cad | 1648 | croak $? if $?; |
03823184 EW |
1649 | seek $log_fh, 0, 0 or croak $!; |
1650 | return { state => 'sep', fh => $log_fh }; | |
1651 | } | |
1652 | ||
1653 | sub next_log_entry { | |
1654 | my $log = shift; # retval of svn_log_raw() | |
1655 | my $ret = undef; | |
1656 | my $fh = $log->{fh}; | |
3397f9df | 1657 | |
03823184 | 1658 | while (<$fh>) { |
3397f9df EW |
1659 | chomp; |
1660 | if (/^\-{72}$/) { | |
03823184 EW |
1661 | if ($log->{state} eq 'msg') { |
1662 | if ($ret->{lines}) { | |
1663 | $ret->{msg} .= $_."\n"; | |
1664 | unless(--$ret->{lines}) { | |
1665 | $log->{state} = 'sep'; | |
ce6f3519 EW |
1666 | } |
1667 | } else { | |
1668 | croak "Log parse error at: $_\n", | |
03823184 | 1669 | $ret->{revision}, |
ce6f3519 EW |
1670 | "\n"; |
1671 | } | |
1672 | next; | |
1673 | } | |
03823184 | 1674 | if ($log->{state} ne 'sep') { |
ce6f3519 | 1675 | croak "Log parse error at: $_\n", |
03823184 EW |
1676 | "state: $log->{state}\n", |
1677 | $ret->{revision}, | |
ce6f3519 EW |
1678 | "\n"; |
1679 | } | |
03823184 | 1680 | $log->{state} = 'rev'; |
3397f9df EW |
1681 | |
1682 | # if we have an empty log message, put something there: | |
03823184 EW |
1683 | if ($ret) { |
1684 | $ret->{msg} ||= "\n"; | |
1685 | delete $ret->{lines}; | |
1686 | return $ret; | |
3397f9df EW |
1687 | } |
1688 | next; | |
1689 | } | |
03823184 | 1690 | if ($log->{state} eq 'rev' && s/^r(\d+)\s*\|\s*//) { |
3397f9df | 1691 | my $rev = $1; |
ce6f3519 EW |
1692 | my ($author, $date, $lines) = split(/\s*\|\s*/, $_, 3); |
1693 | ($lines) = ($lines =~ /(\d+)/); | |
3397f9df EW |
1694 | my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~ |
1695 | /(\d{4})\-(\d\d)\-(\d\d)\s | |
1696 | (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x) | |
1697 | or croak "Failed to parse date: $date\n"; | |
03823184 | 1698 | $ret = { revision => $rev, |
3397f9df EW |
1699 | date => "$tz $Y-$m-$d $H:$M:$S", |
1700 | author => $author, | |
ce6f3519 | 1701 | lines => $lines, |
03823184 | 1702 | msg => '' }; |
a9612be2 EW |
1703 | if (defined $_authors && ! defined $users{$author}) { |
1704 | die "Author: $author not defined in ", | |
1705 | "$_authors file\n"; | |
1706 | } | |
03823184 | 1707 | $log->{state} = 'msg_start'; |
3397f9df EW |
1708 | next; |
1709 | } | |
1710 | # skip the first blank line of the message: | |
03823184 EW |
1711 | if ($log->{state} eq 'msg_start' && /^$/) { |
1712 | $log->{state} = 'msg'; | |
1713 | } elsif ($log->{state} eq 'msg') { | |
1714 | if ($ret->{lines}) { | |
1715 | $ret->{msg} .= $_."\n"; | |
1716 | unless (--$ret->{lines}) { | |
1717 | $log->{state} = 'sep'; | |
ce6f3519 EW |
1718 | } |
1719 | } else { | |
1720 | croak "Log parse error at: $_\n", | |
03823184 | 1721 | $ret->{revision},"\n"; |
ce6f3519 | 1722 | } |
3397f9df EW |
1723 | } |
1724 | } | |
03823184 | 1725 | return $ret; |
3397f9df EW |
1726 | } |
1727 | ||
1728 | sub svn_info { | |
1729 | my $url = shift || $SVN_URL; | |
1730 | ||
1731 | my $pid = open my $info_fh, '-|'; | |
1732 | defined $pid or croak $!; | |
1733 | ||
1734 | if ($pid == 0) { | |
1735 | exec(qw(svn info),$url) or croak $!; | |
1736 | } | |
1737 | ||
1738 | my $ret = {}; | |
1739 | # only single-lines seem to exist in svn info output | |
1740 | while (<$info_fh>) { | |
1741 | chomp $_; | |
e17512f3 | 1742 | if (m#^([^:]+)\s*:\s*(\S.*)$#) { |
3397f9df EW |
1743 | $ret->{$1} = $2; |
1744 | push @{$ret->{-order}}, $1; | |
1745 | } | |
1746 | } | |
cf7424b0 | 1747 | close $info_fh or croak $?; |
3397f9df EW |
1748 | return $ret; |
1749 | } | |
1750 | ||
1751 | sub sys { system(@_) == 0 or croak $? } | |
1752 | ||
36f5b1f0 EW |
1753 | sub eol_cp { |
1754 | my ($from, $to) = @_; | |
8a97e368 | 1755 | my $es = svn_propget_base('svn:eol-style', $to); |
36f5b1f0 EW |
1756 | open my $rfd, '<', $from or croak $!; |
1757 | binmode $rfd or croak $!; | |
1758 | open my $wfd, '>', $to or croak $!; | |
1759 | binmode $wfd or croak $!; | |
a5e0cedc EW |
1760 | eol_cp_fd($rfd, $wfd, $es); |
1761 | close $rfd or croak $!; | |
1762 | close $wfd or croak $!; | |
1763 | } | |
36f5b1f0 | 1764 | |
a5e0cedc EW |
1765 | sub eol_cp_fd { |
1766 | my ($rfd, $wfd, $es) = @_; | |
1767 | my $eol = defined $es ? $EOL{$es} : undef; | |
36f5b1f0 | 1768 | my $buf; |
4a393f2b | 1769 | use bytes; |
36f5b1f0 EW |
1770 | while (1) { |
1771 | my ($r, $w, $t); | |
1772 | defined($r = sysread($rfd, $buf, 4096)) or croak $!; | |
1773 | return unless $r; | |
4a393f2b EW |
1774 | if ($eol) { |
1775 | if ($buf =~ /\015$/) { | |
1776 | my $c; | |
1777 | defined($r = sysread($rfd,$c,1)) or croak $!; | |
1778 | $buf .= $c if $r > 0; | |
1779 | } | |
1780 | $buf =~ s/(?:\015\012|\015|\012)/$eol/gs; | |
1781 | $r = length($buf); | |
1782 | } | |
36f5b1f0 EW |
1783 | for ($w = 0; $w < $r; $w += $t) { |
1784 | $t = syswrite($wfd, $buf, $r - $w, $w) or croak $!; | |
1785 | } | |
1786 | } | |
4a393f2b | 1787 | no bytes; |
36f5b1f0 EW |
1788 | } |
1789 | ||
1790 | sub do_update_index { | |
1791 | my ($z_cmd, $cmd, $no_text_base) = @_; | |
1792 | ||
1793 | my $z = open my $p, '-|'; | |
1794 | defined $z or croak $!; | |
1795 | unless ($z) { exec @$z_cmd or croak $! } | |
1796 | ||
1797 | my $pid = open my $ui, '|-'; | |
1798 | defined $pid or croak $!; | |
1799 | unless ($pid) { | |
1800 | exec('git-update-index',"--$cmd",'-z','--stdin') or croak $!; | |
1801 | } | |
1802 | local $/ = "\0"; | |
1803 | while (my $x = <$p>) { | |
1804 | chomp $x; | |
1805 | if (!$no_text_base && lstat $x && ! -l _ && | |
8a97e368 | 1806 | svn_propget_base('svn:keywords', $x)) { |
36f5b1f0 EW |
1807 | my $mode = -x _ ? 0755 : 0644; |
1808 | my ($v,$d,$f) = File::Spec->splitpath($x); | |
1809 | my $tb = File::Spec->catfile($d, '.svn', 'tmp', | |
1810 | 'text-base',"$f.svn-base"); | |
1811 | $tb =~ s#^/##; | |
1812 | unless (-f $tb) { | |
1813 | $tb = File::Spec->catfile($d, '.svn', | |
1814 | 'text-base',"$f.svn-base"); | |
1815 | $tb =~ s#^/##; | |
1816 | } | |
1817 | unlink $x or croak $!; | |
1818 | eol_cp($tb, $x); | |
1819 | chmod(($mode &~ umask), $x) or croak $!; | |
1820 | } | |
1821 | print $ui $x,"\0"; | |
1822 | } | |
cf7424b0 | 1823 | close $ui or croak $?; |
36f5b1f0 EW |
1824 | } |
1825 | ||
1826 | sub index_changes { | |
a5e0cedc | 1827 | return if $_use_lib; |
6c5cda89 EW |
1828 | |
1829 | if (!-f "$GIT_SVN_DIR/info/exclude") { | |
1830 | open my $fd, '>>', "$GIT_SVN_DIR/info/exclude" or croak $!; | |
1831 | print $fd '.svn',"\n"; | |
1832 | close $fd or croak $!; | |
1833 | } | |
36f5b1f0 EW |
1834 | my $no_text_base = shift; |
1835 | do_update_index([qw/git-diff-files --name-only -z/], | |
1836 | 'remove', | |
1837 | $no_text_base); | |
1838 | do_update_index([qw/git-ls-files -z --others/, | |
883d0a78 | 1839 | "--exclude-from=$GIT_SVN_DIR/info/exclude"], |
36f5b1f0 EW |
1840 | 'add', |
1841 | $no_text_base); | |
3397f9df EW |
1842 | } |
1843 | ||
1844 | sub s_to_file { | |
1845 | my ($str, $file, $mode) = @_; | |
1846 | open my $fd,'>',$file or croak $!; | |
1847 | print $fd $str,"\n" or croak $!; | |
1848 | close $fd or croak $!; | |
1849 | chmod ($mode &~ umask, $file) if (defined $mode); | |
1850 | } | |
1851 | ||
1852 | sub file_to_s { | |
1853 | my $file = shift; | |
1854 | open my $fd,'<',$file or croak "$!: file: $file\n"; | |
1855 | local $/; | |
1856 | my $ret = <$fd>; | |
1857 | close $fd or croak $!; | |
1858 | $ret =~ s/\s*$//s; | |
1859 | return $ret; | |
1860 | } | |
1861 | ||
1862 | sub assert_revision_unknown { | |
42d32870 EW |
1863 | my $r = shift; |
1864 | if (my $c = revdb_get($REVDB, $r)) { | |
1865 | croak "$r = $c already exists! Why are we refetching it?"; | |
3397f9df EW |
1866 | } |
1867 | } | |
1868 | ||
ac749050 EW |
1869 | sub trees_eq { |
1870 | my ($x, $y) = @_; | |
1871 | my @x = safe_qx('git-cat-file','commit',$x); | |
1872 | my @y = safe_qx('git-cat-file','commit',$y); | |
1873 | if (($y[0] ne $x[0]) || $x[0] !~ /^tree $sha1\n$/ | |
1874 | || $y[0] !~ /^tree $sha1\n$/) { | |
1875 | print STDERR "Trees not equal: $y[0] != $x[0]\n"; | |
1876 | return 0 | |
1877 | } | |
1878 | return 1; | |
1879 | } | |
1880 | ||
3397f9df EW |
1881 | sub git_commit { |
1882 | my ($log_msg, @parents) = @_; | |
1883 | assert_revision_unknown($log_msg->{revision}); | |
69f0d91e EW |
1884 | map_tree_joins() if (@_branch_from && !%tree_map); |
1885 | ||
a5e0cedc EW |
1886 | my (@tmp_parents, @exec_parents, %seen_parent); |
1887 | if (my $lparents = $log_msg->{parents}) { | |
1888 | @tmp_parents = @$lparents | |
1889 | } | |
3397f9df EW |
1890 | # commit parents can be conditionally bound to a particular |
1891 | # svn revision via: "svn_revno=commit_sha1", filter them out here: | |
3397f9df EW |
1892 | foreach my $p (@parents) { |
1893 | next unless defined $p; | |
1894 | if ($p =~ /^(\d+)=($sha1_short)$/o) { | |
1895 | if ($1 == $log_msg->{revision}) { | |
a5e0cedc | 1896 | push @tmp_parents, $2; |
3397f9df EW |
1897 | } |
1898 | } else { | |
a5e0cedc | 1899 | push @tmp_parents, $p if $p =~ /$sha1_short/o; |
3397f9df EW |
1900 | } |
1901 | } | |
a5e0cedc EW |
1902 | my $tree = $log_msg->{tree}; |
1903 | if (!defined $tree) { | |
1904 | my $index = set_index($GIT_SVN_INDEX); | |
36f5b1f0 | 1905 | index_changes(); |
a5e0cedc | 1906 | chomp($tree = `git-write-tree`); |
b8c92cad | 1907 | croak $? if $?; |
a5e0cedc EW |
1908 | restore_index($index); |
1909 | } | |
a00439ac EW |
1910 | |
1911 | # just in case we clobber the existing ref, we still want that ref | |
1912 | # as our parent: | |
1913 | if (my $cur = eval { file_to_s("$GIT_DIR/refs/remotes/$GIT_SVN") }) { | |
1914 | push @tmp_parents, $cur; | |
1915 | } | |
1916 | ||
a5e0cedc | 1917 | if (exists $tree_map{$tree}) { |
c1927a85 EW |
1918 | foreach my $p (@{$tree_map{$tree}}) { |
1919 | my $skip; | |
1920 | foreach (@tmp_parents) { | |
1921 | # see if a common parent is found | |
1922 | my $mb = eval { | |
1923 | safe_qx('git-merge-base', $_, $p) | |
1924 | }; | |
1925 | next if ($@ || $?); | |
1926 | $skip = 1; | |
1927 | last; | |
1928 | } | |
1929 | next if $skip; | |
1930 | my ($url_p, $r_p, $uuid_p) = cmt_metadata($p); | |
1931 | next if (($SVN_UUID eq $uuid_p) && | |
1932 | ($log_msg->{revision} > $r_p)); | |
1933 | next if (defined $url_p && defined $SVN_URL && | |
1934 | ($SVN_UUID eq $uuid_p) && | |
1935 | ($url_p eq $SVN_URL)); | |
1936 | push @tmp_parents, $p; | |
1937 | } | |
a5e0cedc EW |
1938 | } |
1939 | foreach (@tmp_parents) { | |
1940 | next if $seen_parent{$_}; | |
1941 | $seen_parent{$_} = 1; | |
1942 | push @exec_parents, $_; | |
1943 | # MAXPARENT is defined to 16 in commit-tree.c: | |
1944 | last if @exec_parents > 16; | |
1945 | } | |
1946 | ||
a00439ac EW |
1947 | set_commit_env($log_msg); |
1948 | my @exec = ('git-commit-tree', $tree); | |
1949 | push @exec, '-p', $_ foreach @exec_parents; | |
1950 | defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec)) | |
1951 | or croak $!; | |
1952 | print $msg_fh $log_msg->{msg} or croak $!; | |
1953 | unless ($_no_metadata) { | |
1954 | print $msg_fh "\ngit-svn-id: $SVN_URL\@$log_msg->{revision}", | |
1ca72aef | 1955 | " $SVN_UUID\n" or croak $!; |
3397f9df | 1956 | } |
a00439ac EW |
1957 | $msg_fh->flush == 0 or croak $!; |
1958 | close $msg_fh or croak $!; | |
3397f9df | 1959 | chomp(my $commit = do { local $/; <$out_fh> }); |
a00439ac EW |
1960 | close $out_fh or croak $!; |
1961 | waitpid $pid, 0; | |
1962 | croak $? if $?; | |
3397f9df | 1963 | if ($commit !~ /^$sha1$/o) { |
a00439ac | 1964 | die "Failed to commit, invalid sha1: $commit\n"; |
3397f9df | 1965 | } |
a00439ac | 1966 | sys('git-update-ref',"refs/remotes/$GIT_SVN",$commit); |
42d32870 EW |
1967 | revdb_set($REVDB, $log_msg->{revision}, $commit); |
1968 | ||
a5e0cedc | 1969 | # this output is read via pipe, do not change: |
3397f9df | 1970 | print "r$log_msg->{revision} = $commit\n"; |
cf7424b0 EW |
1971 | check_repack(); |
1972 | return $commit; | |
1973 | } | |
1974 | ||
1975 | sub check_repack { | |
dc5869c0 EW |
1976 | if ($_repack && (--$_repack_nr == 0)) { |
1977 | $_repack_nr = $_repack; | |
1978 | sys("git repack $_repack_flags"); | |
1979 | } | |
3397f9df EW |
1980 | } |
1981 | ||
a9612be2 | 1982 | sub set_commit_env { |
1ca72aef | 1983 | my ($log_msg) = @_; |
a9612be2 | 1984 | my $author = $log_msg->{author}; |
a5e0cedc EW |
1985 | if (!defined $author || length $author == 0) { |
1986 | $author = '(no author)'; | |
1987 | } | |
a9612be2 | 1988 | my ($name,$email) = defined $users{$author} ? @{$users{$author}} |
1ca72aef | 1989 | : ($author,"$author\@$SVN_UUID"); |
a9612be2 EW |
1990 | $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $name; |
1991 | $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} = $email; | |
1992 | $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_msg->{date}; | |
1993 | } | |
1994 | ||
cf52b8f0 EW |
1995 | sub apply_mod_line_blob { |
1996 | my $m = shift; | |
1997 | if ($m->{mode_b} =~ /^120/) { | |
1998 | blob_to_symlink($m->{sha1_b}, $m->{file_b}); | |
1999 | } else { | |
2000 | blob_to_file($m->{sha1_b}, $m->{file_b}); | |
2001 | } | |
2002 | } | |
2003 | ||
3397f9df EW |
2004 | sub blob_to_symlink { |
2005 | my ($blob, $link) = @_; | |
2006 | defined $link or croak "\$link not defined!\n"; | |
2007 | croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o; | |
cf52b8f0 EW |
2008 | if (-l $link || -f _) { |
2009 | unlink $link or croak $!; | |
2010 | } | |
2011 | ||
3397f9df EW |
2012 | my $dest = `git-cat-file blob $blob`; # no newline, so no chomp |
2013 | symlink $dest, $link or croak $!; | |
2014 | } | |
2015 | ||
2016 | sub blob_to_file { | |
2017 | my ($blob, $file) = @_; | |
2018 | defined $file or croak "\$file not defined!\n"; | |
2019 | croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o; | |
cf52b8f0 EW |
2020 | if (-l $file || -f _) { |
2021 | unlink $file or croak $!; | |
2022 | } | |
2023 | ||
3397f9df EW |
2024 | open my $blob_fh, '>', $file or croak "$!: $file\n"; |
2025 | my $pid = fork; | |
2026 | defined $pid or croak $!; | |
2027 | ||
2028 | if ($pid == 0) { | |
2029 | open STDOUT, '>&', $blob_fh or croak $!; | |
b8c92cad | 2030 | exec('git-cat-file','blob',$blob) or croak $!; |
3397f9df EW |
2031 | } |
2032 | waitpid $pid, 0; | |
2033 | croak $? if $?; | |
2034 | ||
2035 | close $blob_fh or croak $!; | |
2036 | } | |
2037 | ||
2038 | sub safe_qx { | |
2039 | my $pid = open my $child, '-|'; | |
2040 | defined $pid or croak $!; | |
2041 | if ($pid == 0) { | |
b8c92cad | 2042 | exec(@_) or croak $!; |
3397f9df EW |
2043 | } |
2044 | my @ret = (<$child>); | |
2045 | close $child or croak $?; | |
2046 | die $? if $?; # just in case close didn't error out | |
2047 | return wantarray ? @ret : join('',@ret); | |
2048 | } | |
2049 | ||
1d52aba8 | 2050 | sub svn_compat_check { |
a00439ac EW |
2051 | if ($_follow_parent) { |
2052 | print STDERR 'E: --follow-parent functionality is only ', | |
2053 | "available when SVN libraries are used\n"; | |
2054 | exit 1; | |
2055 | } | |
1d52aba8 EW |
2056 | my @co_help = safe_qx(qw(svn co -h)); |
2057 | unless (grep /ignore-externals/,@co_help) { | |
3397f9df EW |
2058 | print STDERR "W: Installed svn version does not support ", |
2059 | "--ignore-externals\n"; | |
2060 | $_no_ignore_ext = 1; | |
2061 | } | |
1d52aba8 EW |
2062 | if (grep /usage: checkout URL\[\@REV\]/,@co_help) { |
2063 | $_svn_co_url_revs = 1; | |
2064 | } | |
8a97e368 EW |
2065 | if (grep /\[TARGET\[\@REV\]\.\.\.\]/, `svn propget -h`) { |
2066 | $_svn_pg_peg_revs = 1; | |
2067 | } | |
7317ed90 EW |
2068 | |
2069 | # I really, really hope nobody hits this... | |
2070 | unless (grep /stop-on-copy/, (safe_qx(qw(svn log -h)))) { | |
2071 | print STDERR <<''; | |
2072 | W: The installed svn version does not support the --stop-on-copy flag in | |
2073 | the log command. | |
2074 | Lets hope the directory you're tracking is not a branch or tag | |
2075 | and was never moved within the repository... | |
2076 | ||
2077 | $_no_stop_copy = 1; | |
2078 | } | |
1d52aba8 EW |
2079 | } |
2080 | ||
2081 | # *sigh*, new versions of svn won't honor -r<rev> without URL@<rev>, | |
2082 | # (and they won't honor URL@<rev> without -r<rev>, too!) | |
2083 | sub svn_cmd_checkout { | |
2084 | my ($url, $rev, $dir) = @_; | |
2085 | my @cmd = ('svn','co', "-r$rev"); | |
2086 | push @cmd, '--ignore-externals' unless $_no_ignore_ext; | |
2087 | $url .= "\@$rev" if $_svn_co_url_revs; | |
2088 | sys(@cmd, $url, $dir); | |
3397f9df | 2089 | } |
2beb3cdd EW |
2090 | |
2091 | sub check_upgrade_needed { | |
cf7424b0 | 2092 | if (!-r $REVDB) { |
1a82e793 | 2093 | -d $GIT_SVN_DIR or mkpath([$GIT_SVN_DIR]); |
cf7424b0 EW |
2094 | open my $fh, '>>',$REVDB or croak $!; |
2095 | close $fh; | |
2096 | } | |
2beb3cdd EW |
2097 | my $old = eval { |
2098 | my $pid = open my $child, '-|'; | |
2099 | defined $pid or croak $!; | |
2100 | if ($pid == 0) { | |
2101 | close STDERR; | |
b8c92cad | 2102 | exec('git-rev-parse',"$GIT_SVN-HEAD") or croak $!; |
2beb3cdd EW |
2103 | } |
2104 | my @ret = (<$child>); | |
2105 | close $child or croak $?; | |
2106 | die $? if $?; # just in case close didn't error out | |
2107 | return wantarray ? @ret : join('',@ret); | |
2108 | }; | |
2109 | return unless $old; | |
2110 | my $head = eval { safe_qx('git-rev-parse',"refs/remotes/$GIT_SVN") }; | |
2111 | if ($@ || !$head) { | |
2112 | print STDERR "Please run: $0 rebuild --upgrade\n"; | |
2113 | exit 1; | |
2114 | } | |
2115 | } | |
2116 | ||
69f0d91e EW |
2117 | # fills %tree_map with a reverse mapping of trees to commits. Useful |
2118 | # for finding parents to commit on. | |
2119 | sub map_tree_joins { | |
098749d9 | 2120 | my %seen; |
69f0d91e EW |
2121 | foreach my $br (@_branch_from) { |
2122 | my $pid = open my $pipe, '-|'; | |
2123 | defined $pid or croak $!; | |
2124 | if ($pid == 0) { | |
098749d9 | 2125 | exec(qw(git-rev-list --topo-order --pretty=raw), $br) |
b8c92cad | 2126 | or croak $!; |
69f0d91e EW |
2127 | } |
2128 | while (<$pipe>) { | |
2129 | if (/^commit ($sha1)$/o) { | |
2130 | my $commit = $1; | |
098749d9 EW |
2131 | |
2132 | # if we've seen a commit, | |
2133 | # we've seen its parents | |
2134 | last if $seen{$commit}; | |
69f0d91e EW |
2135 | my ($tree) = (<$pipe> =~ /^tree ($sha1)$/o); |
2136 | unless (defined $tree) { | |
2137 | die "Failed to parse commit $commit\n"; | |
2138 | } | |
2139 | push @{$tree_map{$tree}}, $commit; | |
098749d9 | 2140 | $seen{$commit} = 1; |
69f0d91e EW |
2141 | } |
2142 | } | |
098749d9 | 2143 | close $pipe; # we could be breaking the pipe early |
69f0d91e EW |
2144 | } |
2145 | } | |
2146 | ||
bf78b1d8 EW |
2147 | sub load_all_refs { |
2148 | if (@_branch_from) { | |
2149 | print STDERR '--branch|-b parameters are ignored when ', | |
2150 | "--branch-all-refs|-B is passed\n"; | |
2151 | } | |
2152 | ||
2153 | # don't worry about rev-list on non-commit objects/tags, | |
2154 | # it shouldn't blow up if a ref is a blob or tree... | |
2155 | chomp(@_branch_from = `git-rev-parse --symbolic --all`); | |
2156 | } | |
2157 | ||
eeb0abe0 EW |
2158 | # '<svn username> = real-name <email address>' mapping based on git-svnimport: |
2159 | sub load_authors { | |
2160 | open my $authors, '<', $_authors or die "Can't open $_authors $!\n"; | |
2161 | while (<$authors>) { | |
2162 | chomp; | |
2163 | next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/; | |
2164 | my ($user, $name, $email) = ($1, $2, $3); | |
2165 | $users{$user} = [$name, $email]; | |
2166 | } | |
2167 | close $authors or croak $!; | |
2168 | } | |
2169 | ||
79bb8d88 EW |
2170 | sub rload_authors { |
2171 | open my $authors, '<', $_authors or die "Can't open $_authors $!\n"; | |
2172 | while (<$authors>) { | |
2173 | chomp; | |
2174 | next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/; | |
2175 | my ($user, $name, $email) = ($1, $2, $3); | |
2176 | $rusers{"$name <$email>"} = $user; | |
2177 | } | |
2178 | close $authors or croak $!; | |
2179 | } | |
2180 | ||
8a97e368 EW |
2181 | sub svn_propget_base { |
2182 | my ($p, $f) = @_; | |
2183 | $f .= '@BASE' if $_svn_pg_peg_revs; | |
2184 | return safe_qx(qw/svn propget/, $p, $f); | |
2185 | } | |
2186 | ||
9d55b41a EW |
2187 | sub git_svn_each { |
2188 | my $sub = shift; | |
2189 | foreach (`git-rev-parse --symbolic --all`) { | |
2190 | next unless s#^refs/remotes/##; | |
2191 | chomp $_; | |
2192 | next unless -f "$GIT_DIR/svn/$_/info/url"; | |
2193 | &$sub($_); | |
2194 | } | |
2195 | } | |
2196 | ||
42d32870 EW |
2197 | sub migrate_revdb { |
2198 | git_svn_each(sub { | |
2199 | my $id = shift; | |
2200 | defined(my $pid = fork) or croak $!; | |
2201 | if (!$pid) { | |
2202 | $GIT_SVN = $ENV{GIT_SVN_ID} = $id; | |
2203 | init_vars(); | |
2204 | exit 0 if -r $REVDB; | |
2205 | print "Upgrading svn => git mapping...\n"; | |
1a82e793 | 2206 | -d $GIT_SVN_DIR or mkpath([$GIT_SVN_DIR]); |
42d32870 EW |
2207 | open my $fh, '>>',$REVDB or croak $!; |
2208 | close $fh; | |
2209 | rebuild(); | |
2210 | print "Done upgrading. You may now delete the ", | |
2211 | "deprecated $GIT_SVN_DIR/revs directory\n"; | |
2212 | exit 0; | |
2213 | } | |
2214 | waitpid $pid, 0; | |
2215 | croak $? if $?; | |
2216 | }); | |
2217 | } | |
2218 | ||
883d0a78 | 2219 | sub migration_check { |
42d32870 | 2220 | migrate_revdb() unless (-e $REVDB); |
883d0a78 EW |
2221 | return if (-d "$GIT_DIR/svn" || !-d $GIT_DIR); |
2222 | print "Upgrading repository...\n"; | |
2223 | unless (-d "$GIT_DIR/svn") { | |
2224 | mkdir "$GIT_DIR/svn" or croak $!; | |
2225 | } | |
2226 | print "Data from a previous version of git-svn exists, but\n\t", | |
2227 | "$GIT_SVN_DIR\n\t(required for this version ", | |
2228 | "($VERSION) of git-svn) does not.\n"; | |
2229 | ||
2230 | foreach my $x (`git-rev-parse --symbolic --all`) { | |
2231 | next unless $x =~ s#^refs/remotes/##; | |
2232 | chomp $x; | |
2233 | next unless -f "$GIT_DIR/$x/info/url"; | |
2234 | my $u = eval { file_to_s("$GIT_DIR/$x/info/url") }; | |
2235 | next unless $u; | |
2236 | my $dn = dirname("$GIT_DIR/svn/$x"); | |
2237 | mkpath([$dn]) unless -d $dn; | |
2238 | rename "$GIT_DIR/$x", "$GIT_DIR/svn/$x" or croak "$!: $x"; | |
883d0a78 | 2239 | } |
42d32870 | 2240 | migrate_revdb() if (-d $GIT_SVN_DIR && !-w $REVDB); |
883d0a78 EW |
2241 | print "Done upgrading.\n"; |
2242 | } | |
2243 | ||
9d55b41a | 2244 | sub find_rev_before { |
42d32870 EW |
2245 | my ($r, $id, $eq_ok) = @_; |
2246 | my $f = "$GIT_DIR/svn/$id/.rev_db"; | |
cf7424b0 EW |
2247 | return (undef,undef) unless -r $f; |
2248 | --$r unless $eq_ok; | |
42d32870 EW |
2249 | while ($r > 0) { |
2250 | if (my $c = revdb_get($f, $r)) { | |
2251 | return ($r, $c); | |
2252 | } | |
2253 | --$r; | |
9d55b41a EW |
2254 | } |
2255 | return (undef, undef); | |
2256 | } | |
2257 | ||
b8c92cad EW |
2258 | sub init_vars { |
2259 | $GIT_SVN ||= $ENV{GIT_SVN_ID} || 'git-svn'; | |
2260 | $GIT_SVN_DIR = "$GIT_DIR/svn/$GIT_SVN"; | |
42d32870 | 2261 | $REVDB = "$GIT_SVN_DIR/.rev_db"; |
b8c92cad EW |
2262 | $GIT_SVN_INDEX = "$GIT_SVN_DIR/index"; |
2263 | $SVN_URL = undef; | |
b8c92cad | 2264 | $SVN_WC = "$GIT_SVN_DIR/tree"; |
c1927a85 | 2265 | %tree_map = (); |
b8c92cad EW |
2266 | } |
2267 | ||
2268 | # convert GetOpt::Long specs for use by git-repo-config | |
2269 | sub read_repo_config { | |
2270 | return unless -d $GIT_DIR; | |
2271 | my $opts = shift; | |
2272 | foreach my $o (keys %$opts) { | |
2273 | my $v = $opts->{$o}; | |
2274 | my ($key) = ($o =~ /^([a-z\-]+)/); | |
2275 | $key =~ s/-//g; | |
2276 | my $arg = 'git-repo-config'; | |
2277 | $arg .= ' --int' if ($o =~ /[:=]i$/); | |
2278 | $arg .= ' --bool' if ($o !~ /[:=][sfi]$/); | |
2279 | if (ref $v eq 'ARRAY') { | |
2280 | chomp(my @tmp = `$arg --get-all svn.$key`); | |
2281 | @$v = @tmp if @tmp; | |
2282 | } else { | |
2283 | chomp(my $tmp = `$arg --get svn.$key`); | |
2284 | if ($tmp && !($arg =~ / --bool / && $tmp eq 'false')) { | |
2285 | $$v = $tmp; | |
2286 | } | |
2287 | } | |
2288 | } | |
2289 | } | |
2290 | ||
dc5869c0 EW |
2291 | sub set_default_vals { |
2292 | if (defined $_repack) { | |
2293 | $_repack = 1000 if ($_repack <= 0); | |
2294 | $_repack_nr = $_repack; | |
cf7424b0 | 2295 | $_repack_flags ||= '-d'; |
dc5869c0 EW |
2296 | } |
2297 | } | |
2298 | ||
9d55b41a EW |
2299 | sub read_grafts { |
2300 | my $gr_file = shift; | |
2301 | my ($grafts, $comments) = ({}, {}); | |
2302 | if (open my $fh, '<', $gr_file) { | |
2303 | my @tmp; | |
2304 | while (<$fh>) { | |
2305 | if (/^($sha1)\s+/) { | |
2306 | my $c = $1; | |
2307 | if (@tmp) { | |
2308 | @{$comments->{$c}} = @tmp; | |
2309 | @tmp = (); | |
2310 | } | |
2311 | foreach my $p (split /\s+/, $_) { | |
2312 | $grafts->{$c}->{$p} = 1; | |
2313 | } | |
2314 | } else { | |
2315 | push @tmp, $_; | |
2316 | } | |
2317 | } | |
2318 | close $fh or croak $!; | |
2319 | @{$comments->{'END'}} = @tmp if @tmp; | |
2320 | } | |
2321 | return ($grafts, $comments); | |
2322 | } | |
2323 | ||
2324 | sub write_grafts { | |
2325 | my ($grafts, $comments, $gr_file) = @_; | |
2326 | ||
2327 | open my $fh, '>', $gr_file or croak $!; | |
2328 | foreach my $c (sort keys %$grafts) { | |
2329 | if ($comments->{$c}) { | |
2330 | print $fh $_ foreach @{$comments->{$c}}; | |
2331 | } | |
2332 | my $p = $grafts->{$c}; | |
c1927a85 | 2333 | my %x; # real parents |
9d55b41a EW |
2334 | delete $p->{$c}; # commits are not self-reproducing... |
2335 | my $pid = open my $ch, '-|'; | |
2336 | defined $pid or croak $!; | |
2337 | if (!$pid) { | |
2338 | exec(qw/git-cat-file commit/, $c) or croak $!; | |
2339 | } | |
2340 | while (<$ch>) { | |
c1927a85 EW |
2341 | if (/^parent ($sha1)/) { |
2342 | $x{$1} = $p->{$1} = 1; | |
9d55b41a | 2343 | } else { |
c1927a85 | 2344 | last unless /^\S/; |
9d55b41a EW |
2345 | } |
2346 | } | |
2347 | close $ch; # breaking the pipe | |
c1927a85 EW |
2348 | |
2349 | # if real parents are the only ones in the grafts, drop it | |
2350 | next if join(' ',sort keys %$p) eq join(' ',sort keys %x); | |
2351 | ||
2352 | my (@ip, @jp, $mb); | |
2353 | my %del = %x; | |
2354 | @ip = @jp = keys %$p; | |
2355 | foreach my $i (@ip) { | |
2356 | next if $del{$i} || $p->{$i} == 2; | |
2357 | foreach my $j (@jp) { | |
2358 | next if $i eq $j || $del{$j} || $p->{$j} == 2; | |
2359 | $mb = eval { safe_qx('git-merge-base',$i,$j) }; | |
2360 | next unless $mb; | |
2361 | chomp $mb; | |
2362 | next if $x{$mb}; | |
2363 | if ($mb eq $j) { | |
2364 | delete $p->{$i}; | |
2365 | $del{$i} = 1; | |
2366 | } elsif ($mb eq $i) { | |
2367 | delete $p->{$j}; | |
2368 | $del{$j} = 1; | |
2369 | } | |
2370 | } | |
2371 | } | |
2372 | ||
2373 | # if real parents are the only ones in the grafts, drop it | |
2374 | next if join(' ',sort keys %$p) eq join(' ',sort keys %x); | |
2375 | ||
9d55b41a EW |
2376 | print $fh $c, ' ', join(' ', sort keys %$p),"\n"; |
2377 | } | |
2378 | if ($comments->{'END'}) { | |
2379 | print $fh $_ foreach @{$comments->{'END'}}; | |
2380 | } | |
2381 | close $fh or croak $!; | |
2382 | } | |
2383 | ||
a00439ac EW |
2384 | sub read_url_paths_all { |
2385 | my ($l_map, $pfx, $p) = @_; | |
2386 | my @dir; | |
2387 | foreach (<$p/*>) { | |
2388 | if (-r "$_/info/url") { | |
2389 | $pfx .= '/' if $pfx && $pfx !~ m!/$!; | |
2390 | my $id = $pfx . basename $_; | |
2391 | my $url = file_to_s("$_/info/url"); | |
2392 | my ($u, $p) = repo_path_split($url); | |
2393 | $l_map->{$u}->{$p} = $id; | |
2394 | } elsif (-d $_) { | |
2395 | push @dir, $_; | |
2396 | } | |
2397 | } | |
2398 | foreach (@dir) { | |
2399 | my $x = $_; | |
2400 | $x =~ s!^\Q$GIT_DIR\E/svn/!!o; | |
2401 | read_url_paths_all($l_map, $x, $_); | |
2402 | } | |
2403 | } | |
2404 | ||
2405 | # this one only gets ids that have been imported, not new ones | |
9d55b41a EW |
2406 | sub read_url_paths { |
2407 | my $l_map = {}; | |
2408 | git_svn_each(sub { my $x = shift; | |
6c5cda89 EW |
2409 | my $url = file_to_s("$GIT_DIR/svn/$x/info/url"); |
2410 | my ($u, $p) = repo_path_split($url); | |
9d55b41a EW |
2411 | $l_map->{$u}->{$p} = $x; |
2412 | }); | |
2413 | return $l_map; | |
2414 | } | |
2415 | ||
79bb8d88 | 2416 | sub extract_metadata { |
c1927a85 | 2417 | my $id = shift or return (undef, undef, undef); |
79bb8d88 EW |
2418 | my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+) |
2419 | \s([a-f\d\-]+)$/x); | |
2420 | if (!$rev || !$uuid || !$url) { | |
2421 | # some of the original repositories I made had | |
2422 | # indentifiers like this: | |
2423 | ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/); | |
2424 | } | |
2425 | return ($url, $rev, $uuid); | |
2426 | } | |
2427 | ||
c1927a85 EW |
2428 | sub cmt_metadata { |
2429 | return extract_metadata((grep(/^git-svn-id: /, | |
2430 | safe_qx(qw/git-cat-file commit/, shift)))[-1]); | |
2431 | } | |
2432 | ||
2433 | sub get_commit_time { | |
2434 | my $cmt = shift; | |
2435 | defined(my $pid = open my $fh, '-|') or croak $!; | |
2436 | if (!$pid) { | |
2437 | exec qw/git-rev-list --pretty=raw -n1/, $cmt or croak $!; | |
2438 | } | |
2439 | while (<$fh>) { | |
2440 | /^committer\s(?:.+) (\d+) ([\-\+]?\d+)$/ or next; | |
2441 | my ($s, $tz) = ($1, $2); | |
2442 | if ($tz =~ s/^\+//) { | |
2443 | $s += tz_to_s_offset($tz); | |
2444 | } elsif ($tz =~ s/^\-//) { | |
2445 | $s -= tz_to_s_offset($tz); | |
2446 | } | |
2447 | close $fh; | |
2448 | return $s; | |
2449 | } | |
2450 | die "Can't get commit time for commit: $cmt\n"; | |
2451 | } | |
2452 | ||
79bb8d88 EW |
2453 | sub tz_to_s_offset { |
2454 | my ($tz) = @_; | |
2455 | $tz =~ s/(\d\d)$//; | |
2456 | return ($1 * 60) + ($tz * 3600); | |
2457 | } | |
2458 | ||
2459 | sub setup_pager { # translated to Perl from pager.c | |
2460 | return unless (-t *STDOUT); | |
2461 | my $pager = $ENV{PAGER}; | |
2462 | if (!defined $pager) { | |
2463 | $pager = 'less'; | |
2464 | } elsif (length $pager == 0 || $pager eq 'cat') { | |
2465 | return; | |
2466 | } | |
2467 | pipe my $rfd, my $wfd or return; | |
2468 | defined(my $pid = fork) or croak $!; | |
2469 | if (!$pid) { | |
2470 | open STDOUT, '>&', $wfd or croak $!; | |
2471 | return; | |
2472 | } | |
2473 | open STDIN, '<&', $rfd or croak $!; | |
2474 | $ENV{LESS} ||= '-S'; | |
2475 | exec $pager or croak "Can't run pager: $!\n";; | |
2476 | } | |
2477 | ||
2478 | sub get_author_info { | |
2479 | my ($dest, $author, $t, $tz) = @_; | |
2480 | $author =~ s/(?:^\s*|\s*$)//g; | |
c0d48222 | 2481 | $dest->{a_raw} = $author; |
79bb8d88 EW |
2482 | my $_a; |
2483 | if ($_authors) { | |
2484 | $_a = $rusers{$author} || undef; | |
2485 | } | |
2486 | if (!$_a) { | |
2487 | ($_a) = ($author =~ /<([^>]+)\@[^>]+>$/); | |
2488 | } | |
2489 | $dest->{t} = $t; | |
2490 | $dest->{tz} = $tz; | |
2491 | $dest->{a} = $_a; | |
2492 | # Date::Parse isn't in the standard Perl distro :( | |
2493 | if ($tz =~ s/^\+//) { | |
2494 | $t += tz_to_s_offset($tz); | |
2495 | } elsif ($tz =~ s/^\-//) { | |
2496 | $t -= tz_to_s_offset($tz); | |
2497 | } | |
2498 | $dest->{t_utc} = $t; | |
2499 | } | |
2500 | ||
2501 | sub process_commit { | |
2502 | my ($c, $r_min, $r_max, $defer) = @_; | |
2503 | if (defined $r_min && defined $r_max) { | |
2504 | if ($r_min == $c->{r} && $r_min == $r_max) { | |
2505 | show_commit($c); | |
2506 | return 0; | |
2507 | } | |
2508 | return 1 if $r_min == $r_max; | |
2509 | if ($r_min < $r_max) { | |
2510 | # we need to reverse the print order | |
2511 | return 0 if (defined $_limit && --$_limit < 0); | |
2512 | push @$defer, $c; | |
2513 | return 1; | |
2514 | } | |
2515 | if ($r_min != $r_max) { | |
2516 | return 1 if ($r_min < $c->{r}); | |
2517 | return 1 if ($r_max > $c->{r}); | |
2518 | } | |
2519 | } | |
2520 | return 0 if (defined $_limit && --$_limit < 0); | |
2521 | show_commit($c); | |
2522 | return 1; | |
2523 | } | |
2524 | ||
2525 | sub show_commit { | |
2526 | my $c = shift; | |
2527 | if ($_oneline) { | |
2528 | my $x = "\n"; | |
2529 | if (my $l = $c->{l}) { | |
2530 | while ($l->[0] =~ /^\s*$/) { shift @$l } | |
2531 | $x = $l->[0]; | |
2532 | } | |
2533 | $_l_fmt ||= 'A' . length($c->{r}); | |
2534 | print 'r',pack($_l_fmt, $c->{r}),' | '; | |
2535 | print "$c->{c} | " if $_show_commit; | |
2536 | print $x; | |
2537 | } else { | |
2538 | show_commit_normal($c); | |
2539 | } | |
2540 | } | |
2541 | ||
2542 | sub show_commit_normal { | |
2543 | my ($c) = @_; | |
2544 | print '-' x72, "\nr$c->{r} | "; | |
2545 | print "$c->{c} | " if $_show_commit; | |
2546 | print "$c->{a} | ", strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)", | |
2547 | localtime($c->{t_utc})), ' | '; | |
2548 | my $nr_line = 0; | |
2549 | ||
2550 | if (my $l = $c->{l}) { | |
2551 | while ($l->[$#$l] eq "\n" && $l->[($#$l - 1)] eq "\n") { | |
2552 | pop @$l; | |
2553 | } | |
2554 | $nr_line = scalar @$l; | |
2555 | if (!$nr_line) { | |
2556 | print "1 line\n\n\n"; | |
2557 | } else { | |
2558 | if ($nr_line == 1) { | |
2559 | $nr_line = '1 line'; | |
2560 | } else { | |
2561 | $nr_line .= ' lines'; | |
2562 | } | |
2563 | print $nr_line, "\n\n"; | |
2564 | print $_ foreach @$l; | |
2565 | } | |
2566 | } else { | |
2567 | print "1 line\n\n"; | |
2568 | ||
2569 | } | |
2570 | foreach my $x (qw/raw diff/) { | |
2571 | if ($c->{$x}) { | |
2572 | print "\n"; | |
2573 | print $_ foreach @{$c->{$x}} | |
2574 | } | |
2575 | } | |
2576 | } | |
2577 | ||
a5e0cedc EW |
2578 | sub libsvn_load { |
2579 | return unless $_use_lib; | |
2580 | $_use_lib = eval { | |
2581 | require SVN::Core; | |
dc62e25c EW |
2582 | if ($SVN::Core::VERSION lt '1.1.0') { |
2583 | die "Need SVN::Core 1.1.0 or better ", | |
a5e0cedc EW |
2584 | "(got $SVN::Core::VERSION) ", |
2585 | "Falling back to command-line svn\n"; | |
2586 | } | |
2587 | require SVN::Ra; | |
2588 | require SVN::Delta; | |
2589 | push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor'; | |
2590 | my $kill_stupid_warnings = $SVN::Node::none.$SVN::Node::file. | |
2591 | $SVN::Node::dir.$SVN::Node::unknown. | |
2592 | $SVN::Node::none.$SVN::Node::file. | |
2593 | $SVN::Node::dir.$SVN::Node::unknown; | |
2594 | 1; | |
2595 | }; | |
2596 | } | |
2597 | ||
2598 | sub libsvn_connect { | |
2599 | my ($url) = @_; | |
2600 | my $auth = SVN::Core::auth_open([SVN::Client::get_simple_provider(), | |
2601 | SVN::Client::get_ssl_server_trust_file_provider(), | |
2602 | SVN::Client::get_username_provider()]); | |
2603 | my $s = eval { SVN::Ra->new(url => $url, auth => $auth) }; | |
2604 | return $s; | |
2605 | } | |
2606 | ||
2607 | sub libsvn_get_file { | |
2608 | my ($gui, $f, $rev) = @_; | |
2609 | my $p = $f; | |
2610 | return unless ($p =~ s#^\Q$SVN_PATH\E/?##); | |
2611 | ||
968bdf1f | 2612 | my ($hash, $pid, $in, $out); |
a5e0cedc | 2613 | my $pool = SVN::Pool->new; |
968bdf1f EW |
2614 | defined($pid = open3($in, $out, '>&STDERR', |
2615 | qw/git-hash-object -w --stdin/)) or croak $!; | |
dc62e25c EW |
2616 | # redirect STDOUT for SVN 1.1.x compatibility |
2617 | open my $stdout, '>&', \*STDOUT or croak $!; | |
2618 | open STDOUT, '>&', $in or croak $!; | |
dc62e25c | 2619 | my ($r, $props) = $SVN->get_file($f, $rev, \*STDOUT, $pool); |
968bdf1f | 2620 | $in->flush == 0 or croak $!; |
dc62e25c | 2621 | open STDOUT, '>&', $stdout or croak $!; |
968bdf1f | 2622 | close $in or croak $!; |
dc62e25c | 2623 | close $stdout or croak $!; |
a5e0cedc | 2624 | $pool->clear; |
968bdf1f EW |
2625 | chomp($hash = do { local $/; <$out> }); |
2626 | close $out or croak $!; | |
2627 | waitpid $pid, 0; | |
2628 | $hash =~ /^$sha1$/o or die "not a sha1: $hash\n"; | |
2629 | ||
2630 | my $mode = exists $props->{'svn:executable'} ? '100755' : '100644'; | |
a5e0cedc EW |
2631 | if (exists $props->{'svn:special'}) { |
2632 | $mode = '120000'; | |
968bdf1f | 2633 | my $link = `git-cat-file blob $hash`; |
a5e0cedc EW |
2634 | $link =~ s/^link // or die "svn:special file with contents: <", |
2635 | $link, "> is not understood\n"; | |
968bdf1f EW |
2636 | defined($pid = open3($in, $out, '>&STDERR', |
2637 | qw/git-hash-object -w --stdin/)) or croak $!; | |
2638 | print $in $link; | |
2639 | $in->flush == 0 or croak $!; | |
2640 | close $in or croak $!; | |
2641 | chomp($hash = do { local $/; <$out> }); | |
2642 | close $out or croak $!; | |
2643 | waitpid $pid, 0; | |
2644 | $hash =~ /^$sha1$/o or die "not a sha1: $hash\n"; | |
a5e0cedc | 2645 | } |
a5e0cedc | 2646 | print $gui $mode,' ',$hash,"\t",$p,"\0" or croak $!; |
a5e0cedc EW |
2647 | } |
2648 | ||
2649 | sub libsvn_log_entry { | |
2650 | my ($rev, $author, $date, $msg, $parents) = @_; | |
2651 | my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T | |
2652 | (\d\d)\:(\d\d)\:(\d\d).\d+Z$/x) | |
2653 | or die "Unable to parse date: $date\n"; | |
2654 | if (defined $_authors && ! defined $users{$author}) { | |
2655 | die "Author: $author not defined in $_authors file\n"; | |
2656 | } | |
2657 | return { revision => $rev, date => "+0000 $Y-$m-$d $H:$M:$S", | |
2658 | author => $author, msg => $msg."\n", parents => $parents || [] } | |
2659 | } | |
2660 | ||
2661 | sub process_rm { | |
2662 | my ($gui, $last_commit, $f) = @_; | |
2663 | $f =~ s#^\Q$SVN_PATH\E/?## or return; | |
2664 | # remove entire directories. | |
2665 | if (safe_qx('git-ls-tree',$last_commit,'--',$f) =~ /^040000 tree/) { | |
2666 | defined(my $pid = open my $ls, '-|') or croak $!; | |
2667 | if (!$pid) { | |
2668 | exec(qw/git-ls-tree -r --name-only -z/, | |
2669 | $last_commit,'--',$f) or croak $!; | |
2670 | } | |
2671 | local $/ = "\0"; | |
2672 | while (<$ls>) { | |
2673 | print $gui '0 ',0 x 40,"\t",$_ or croak $!; | |
2674 | } | |
cf7424b0 | 2675 | close $ls or croak $?; |
a5e0cedc EW |
2676 | } else { |
2677 | print $gui '0 ',0 x 40,"\t",$f,"\0" or croak $!; | |
2678 | } | |
2679 | } | |
2680 | ||
2681 | sub libsvn_fetch { | |
2682 | my ($last_commit, $paths, $rev, $author, $date, $msg) = @_; | |
2683 | open my $gui, '| git-update-index -z --index-info' or croak $!; | |
2684 | my @amr; | |
2685 | foreach my $f (keys %$paths) { | |
2686 | my $m = $paths->{$f}->action(); | |
2687 | $f =~ s#^/+##; | |
2688 | if ($m =~ /^[DR]$/) { | |
80f50749 | 2689 | print "\t$m\t$f\n" unless $_q; |
a5e0cedc EW |
2690 | process_rm($gui, $last_commit, $f); |
2691 | next if $m eq 'D'; | |
2692 | # 'R' can be file replacements, too, right? | |
2693 | } | |
2694 | my $pool = SVN::Pool->new; | |
2695 | my $t = $SVN->check_path($f, $rev, $pool); | |
2696 | if ($t == $SVN::Node::file) { | |
2697 | if ($m =~ /^[AMR]$/) { | |
80f50749 | 2698 | push @amr, [ $m, $f ]; |
a5e0cedc EW |
2699 | } else { |
2700 | die "Unrecognized action: $m, ($f r$rev)\n"; | |
2701 | } | |
2702 | } | |
2703 | $pool->clear; | |
2704 | } | |
80f50749 EW |
2705 | foreach (@amr) { |
2706 | print "\t$_->[0]\t$_->[1]\n" unless $_q; | |
2707 | libsvn_get_file($gui, $_->[1], $rev) | |
2708 | } | |
cf7424b0 | 2709 | close $gui or croak $?; |
a5e0cedc EW |
2710 | return libsvn_log_entry($rev, $author, $date, $msg, [$last_commit]); |
2711 | } | |
2712 | ||
2713 | sub svn_grab_base_rev { | |
2714 | defined(my $pid = open my $fh, '-|') or croak $!; | |
2715 | if (!$pid) { | |
2716 | open my $null, '>', '/dev/null' or croak $!; | |
2717 | open STDERR, '>&', $null or croak $!; | |
2718 | exec qw/git-rev-parse --verify/,"refs/remotes/$GIT_SVN^0" | |
2719 | or croak $!; | |
2720 | } | |
2721 | chomp(my $c = do { local $/; <$fh> }); | |
2722 | close $fh; | |
2723 | if (defined $c && length $c) { | |
c1927a85 | 2724 | my ($url, $rev, $uuid) = cmt_metadata($c); |
a00439ac EW |
2725 | return ($rev, $c) if defined $rev; |
2726 | } | |
2727 | if ($_no_metadata) { | |
2728 | my $offset = -41; # from tail | |
2729 | my $rl; | |
2730 | open my $fh, '<', $REVDB or | |
2731 | die "--no-metadata specified and $REVDB not readable\n"; | |
2732 | seek $fh, $offset, 2; | |
2733 | $rl = readline $fh; | |
2734 | defined $rl or return (undef, undef); | |
2735 | chomp $rl; | |
2736 | while ($c ne $rl && tell $fh != 0) { | |
2737 | $offset -= 41; | |
2738 | seek $fh, $offset, 2; | |
2739 | $rl = readline $fh; | |
2740 | defined $rl or return (undef, undef); | |
2741 | chomp $rl; | |
2742 | } | |
2743 | my $rev = tell $fh; | |
2744 | croak $! if ($rev < -1); | |
2745 | $rev = ($rev - 41) / 41; | |
2746 | close $fh or croak $!; | |
a5e0cedc EW |
2747 | return ($rev, $c); |
2748 | } | |
2749 | return (undef, undef); | |
2750 | } | |
2751 | ||
2752 | sub libsvn_parse_revision { | |
2753 | my $base = shift; | |
2754 | my $head = $SVN->get_latest_revnum(); | |
2755 | if (!defined $_revision || $_revision eq 'BASE:HEAD') { | |
2756 | return ($base + 1, $head) if (defined $base); | |
2757 | return (0, $head); | |
2758 | } | |
2759 | return ($1, $2) if ($_revision =~ /^(\d+):(\d+)$/); | |
2760 | return ($_revision, $_revision) if ($_revision =~ /^\d+$/); | |
2761 | if ($_revision =~ /^BASE:(\d+)$/) { | |
2762 | return ($base + 1, $1) if (defined $base); | |
2763 | return (0, $head); | |
2764 | } | |
2765 | return ($1, $head) if ($_revision =~ /^(\d+):HEAD$/); | |
2766 | die "revision argument: $_revision not understood by git-svn\n", | |
2767 | "Try using the command-line svn client instead\n"; | |
2768 | } | |
2769 | ||
2770 | sub libsvn_traverse { | |
2771 | my ($gui, $pfx, $path, $rev) = @_; | |
2772 | my $cwd = "$pfx/$path"; | |
2773 | my $pool = SVN::Pool->new; | |
2774 | $cwd =~ s#^/+##g; | |
2775 | my ($dirent, $r, $props) = $SVN->get_dir($cwd, $rev, $pool); | |
2776 | foreach my $d (keys %$dirent) { | |
2777 | my $t = $dirent->{$d}->kind; | |
2778 | if ($t == $SVN::Node::dir) { | |
2779 | libsvn_traverse($gui, $cwd, $d, $rev); | |
2780 | } elsif ($t == $SVN::Node::file) { | |
80f50749 | 2781 | print "\tA\t$cwd/$d\n" unless $_q; |
a5e0cedc EW |
2782 | libsvn_get_file($gui, "$cwd/$d", $rev); |
2783 | } | |
2784 | } | |
2785 | $pool->clear; | |
2786 | } | |
2787 | ||
2788 | sub libsvn_traverse_ignore { | |
2789 | my ($fh, $path, $r) = @_; | |
2790 | $path =~ s#^/+##g; | |
2791 | my $pool = SVN::Pool->new; | |
2792 | my ($dirent, undef, $props) = $SVN->get_dir($path, $r, $pool); | |
2793 | my $p = $path; | |
2794 | $p =~ s#^\Q$SVN_PATH\E/?##; | |
2795 | print $fh length $p ? "\n# $p\n" : "\n# /\n"; | |
2796 | if (my $s = $props->{'svn:ignore'}) { | |
2797 | $s =~ s/[\r\n]+/\n/g; | |
2798 | chomp $s; | |
2799 | if (length $p == 0) { | |
2800 | $s =~ s#\n#\n/$p#g; | |
2801 | print $fh "/$s\n"; | |
2802 | } else { | |
2803 | $s =~ s#\n#\n/$p/#g; | |
2804 | print $fh "/$p/$s\n"; | |
2805 | } | |
2806 | } | |
2807 | foreach (sort keys %$dirent) { | |
2808 | next if $dirent->{$_}->kind != $SVN::Node::dir; | |
2809 | libsvn_traverse_ignore($fh, "$path/$_", $r); | |
2810 | } | |
2811 | $pool->clear; | |
2812 | } | |
2813 | ||
42d32870 EW |
2814 | sub revisions_eq { |
2815 | my ($path, $r0, $r1) = @_; | |
2816 | return 1 if $r0 == $r1; | |
2817 | my $nr = 0; | |
2818 | if ($_use_lib) { | |
2819 | # should be OK to use Pool here (r1 - r0) should be small | |
2820 | my $pool = SVN::Pool->new; | |
dc62e25c EW |
2821 | libsvn_get_log($SVN, "/$path", $r0, $r1, |
2822 | 0, 1, 1, sub {$nr++}, $pool); | |
42d32870 EW |
2823 | $pool->clear; |
2824 | } else { | |
2825 | my ($url, undef) = repo_path_split($SVN_URL); | |
2826 | my $svn_log = svn_log_raw("$url/$path","-r$r0:$r1"); | |
2827 | while (next_log_entry($svn_log)) { $nr++ } | |
2828 | close $svn_log->{fh}; | |
2829 | } | |
2830 | return 0 if ($nr > 1); | |
2831 | return 1; | |
2832 | } | |
2833 | ||
2834 | sub libsvn_find_parent_branch { | |
a5e0cedc EW |
2835 | my ($paths, $rev, $author, $date, $msg) = @_; |
2836 | my $svn_path = '/'.$SVN_PATH; | |
2837 | ||
2838 | # look for a parent from another branch: | |
cf7424b0 EW |
2839 | my $i = $paths->{$svn_path} or return; |
2840 | my $branch_from = $i->copyfrom_path or return; | |
2841 | my $r = $i->copyfrom_rev; | |
2842 | print STDERR "Found possible branch point: ", | |
2843 | "$branch_from => $svn_path, $r\n"; | |
2844 | $branch_from =~ s#^/##; | |
a00439ac EW |
2845 | my $l_map = {}; |
2846 | read_url_paths_all($l_map, '', "$GIT_DIR/svn"); | |
cf7424b0 EW |
2847 | my $url = $SVN->{url}; |
2848 | defined $l_map->{$url} or return; | |
a00439ac EW |
2849 | my $id = $l_map->{$url}->{$branch_from}; |
2850 | if (!defined $id && $_follow_parent) { | |
2851 | print STDERR "Following parent: $branch_from\@$r\n"; | |
2852 | # auto create a new branch and follow it | |
2853 | $id = basename($branch_from); | |
2854 | $id .= '@'.$r if -r "$GIT_DIR/svn/$id"; | |
2855 | while (-r "$GIT_DIR/svn/$id") { | |
2856 | # just grow a tail if we're not unique enough :x | |
2857 | $id .= '-'; | |
2858 | } | |
2859 | } | |
2860 | return unless defined $id; | |
2861 | ||
cf7424b0 | 2862 | my ($r0, $parent) = find_rev_before($r,$id,1); |
a00439ac EW |
2863 | if ($_follow_parent && (!defined $r0 || !defined $parent)) { |
2864 | defined(my $pid = fork) or croak $!; | |
2865 | if (!$pid) { | |
2866 | $GIT_SVN = $ENV{GIT_SVN_ID} = $id; | |
2867 | init_vars(); | |
2868 | $SVN_URL = "$url/$branch_from"; | |
2869 | $SVN_LOG = $SVN = undef; | |
2870 | setup_git_svn(); | |
2871 | # we can't assume SVN_URL exists at r+1: | |
2872 | $_revision = "0:$r"; | |
2873 | fetch_lib(); | |
2874 | exit 0; | |
2875 | } | |
2876 | waitpid $pid, 0; | |
2877 | croak $? if $?; | |
2878 | ($r0, $parent) = find_rev_before($r,$id,1); | |
2879 | } | |
cf7424b0 EW |
2880 | return unless (defined $r0 && defined $parent); |
2881 | if (revisions_eq($branch_from, $r0, $r)) { | |
2882 | unlink $GIT_SVN_INDEX; | |
a00439ac | 2883 | print STDERR "Found branch parent: ($GIT_SVN) $parent\n"; |
cf7424b0 EW |
2884 | sys(qw/git-read-tree/, $parent); |
2885 | return libsvn_fetch($parent, $paths, $rev, | |
2886 | $author, $date, $msg); | |
2887 | } | |
2888 | print STDERR "Nope, branch point not imported or unknown\n"; | |
42d32870 EW |
2889 | return undef; |
2890 | } | |
2891 | ||
dc62e25c EW |
2892 | sub libsvn_get_log { |
2893 | my ($ra, @args) = @_; | |
2894 | if ($SVN::Core::VERSION le '1.2.0') { | |
2895 | splice(@args, 3, 1); | |
2896 | } | |
2897 | $ra->get_log(@args); | |
2898 | } | |
2899 | ||
42d32870 EW |
2900 | sub libsvn_new_tree { |
2901 | if (my $log_entry = libsvn_find_parent_branch(@_)) { | |
2902 | return $log_entry; | |
2903 | } | |
2904 | my ($paths, $rev, $author, $date, $msg) = @_; | |
a5e0cedc EW |
2905 | open my $gui, '| git-update-index -z --index-info' or croak $!; |
2906 | my $pool = SVN::Pool->new; | |
2907 | libsvn_traverse($gui, '', $SVN_PATH, $rev, $pool); | |
2908 | $pool->clear; | |
cf7424b0 | 2909 | close $gui or croak $?; |
a5e0cedc EW |
2910 | return libsvn_log_entry($rev, $author, $date, $msg); |
2911 | } | |
2912 | ||
2913 | sub find_graft_path_commit { | |
2914 | my ($tree_paths, $p1, $r1) = @_; | |
2915 | foreach my $x (keys %$tree_paths) { | |
2916 | next unless ($p1 =~ /^\Q$x\E/); | |
2917 | my $i = $tree_paths->{$x}; | |
42d32870 EW |
2918 | my ($r0, $parent) = find_rev_before($r1,$i,1); |
2919 | return $parent if (defined $r0 && $r0 == $r1); | |
a5e0cedc EW |
2920 | print STDERR "r$r1 of $i not imported\n"; |
2921 | next; | |
2922 | } | |
2923 | return undef; | |
2924 | } | |
2925 | ||
2926 | sub find_graft_path_parents { | |
2927 | my ($grafts, $tree_paths, $c, $p0, $r0) = @_; | |
2928 | foreach my $x (keys %$tree_paths) { | |
2929 | next unless ($p0 =~ /^\Q$x\E/); | |
2930 | my $i = $tree_paths->{$x}; | |
42d32870 EW |
2931 | my ($r, $parent) = find_rev_before($r0, $i, 1); |
2932 | if (defined $r && defined $parent && revisions_eq($x,$r,$r0)) { | |
c1927a85 EW |
2933 | my ($url_b, undef, $uuid_b) = cmt_metadata($c); |
2934 | my ($url_a, undef, $uuid_a) = cmt_metadata($parent); | |
2935 | next if ($url_a && $url_b && $url_a eq $url_b && | |
2936 | $uuid_b eq $uuid_a); | |
42d32870 | 2937 | $grafts->{$c}->{$parent} = 1; |
a5e0cedc | 2938 | } |
a5e0cedc EW |
2939 | } |
2940 | } | |
2941 | ||
2942 | sub libsvn_graft_file_copies { | |
2943 | my ($grafts, $tree_paths, $path, $paths, $rev) = @_; | |
2944 | foreach (keys %$paths) { | |
2945 | my $i = $paths->{$_}; | |
2946 | my ($m, $p0, $r0) = ($i->action, $i->copyfrom_path, | |
2947 | $i->copyfrom_rev); | |
2948 | next unless (defined $p0 && defined $r0); | |
2949 | ||
2950 | my $p1 = $_; | |
2951 | $p1 =~ s#^/##; | |
2952 | $p0 =~ s#^/##; | |
2953 | my $c = find_graft_path_commit($tree_paths, $p1, $rev); | |
2954 | next unless $c; | |
2955 | find_graft_path_parents($grafts, $tree_paths, $c, $p0, $r0); | |
2956 | } | |
2957 | } | |
2958 | ||
2959 | sub set_index { | |
2960 | my $old = $ENV{GIT_INDEX_FILE}; | |
2961 | $ENV{GIT_INDEX_FILE} = shift; | |
2962 | return $old; | |
2963 | } | |
2964 | ||
2965 | sub restore_index { | |
2966 | my ($old) = @_; | |
2967 | if (defined $old) { | |
2968 | $ENV{GIT_INDEX_FILE} = $old; | |
2969 | } else { | |
2970 | delete $ENV{GIT_INDEX_FILE}; | |
2971 | } | |
2972 | } | |
2973 | ||
2974 | sub libsvn_commit_cb { | |
2975 | my ($rev, $date, $committer, $c, $msg, $r_last, $cmt_last) = @_; | |
42d32870 | 2976 | if ($_optimize_commits && $rev == ($r_last + 1)) { |
a5e0cedc EW |
2977 | my $log = libsvn_log_entry($rev,$committer,$date,$msg); |
2978 | $log->{tree} = get_tree_from_treeish($c); | |
2979 | my $cmt = git_commit($log, $cmt_last, $c); | |
2980 | my @diff = safe_qx('git-diff-tree', $cmt, $c); | |
2981 | if (@diff) { | |
2982 | print STDERR "Trees differ: $cmt $c\n", | |
2983 | join('',@diff),"\n"; | |
2984 | exit 1; | |
2985 | } | |
2986 | } else { | |
cf7424b0 | 2987 | fetch("$rev=$c"); |
a5e0cedc EW |
2988 | } |
2989 | } | |
2990 | ||
2991 | sub libsvn_ls_fullurl { | |
2992 | my $fullurl = shift; | |
2993 | my ($repo, $path) = repo_path_split($fullurl); | |
2994 | $SVN ||= libsvn_connect($repo); | |
2995 | my @ret; | |
2996 | my $pool = SVN::Pool->new; | |
2997 | my ($dirent, undef, undef) = $SVN->get_dir($path, | |
2998 | $SVN->get_latest_revnum, $pool); | |
2999 | foreach my $d (keys %$dirent) { | |
3000 | if ($dirent->{$d}->kind == $SVN::Node::dir) { | |
3001 | push @ret, "$d/"; # add '/' for compat with cli svn | |
3002 | } | |
3003 | } | |
3004 | $pool->clear; | |
3005 | return @ret; | |
3006 | } | |
3007 | ||
3008 | ||
3009 | sub libsvn_skip_unknown_revs { | |
3010 | my $err = shift; | |
3011 | my $errno = $err->apr_err(); | |
3012 | # Maybe the branch we're tracking didn't | |
3013 | # exist when the repo started, so it's | |
3014 | # not an error if it doesn't, just continue | |
3015 | # | |
3016 | # Wonderfully consistent library, eh? | |
3017 | # 160013 - svn:// and file:// | |
3018 | # 175002 - http(s):// | |
3019 | # More codes may be discovered later... | |
3020 | if ($errno == 175002 || $errno == 160013) { | |
a5e0cedc EW |
3021 | return; |
3022 | } | |
3023 | croak "Error from SVN, ($errno): ", $err->expanded_message,"\n"; | |
3024 | }; | |
3025 | ||
42d32870 EW |
3026 | # Tie::File seems to be prone to offset errors if revisions get sparse, |
3027 | # it's not that fast, either. Tie::File is also not in Perl 5.6. So | |
3028 | # one of my favorite modules is out :< Next up would be one of the DBM | |
3029 | # modules, but I'm not sure which is most portable... So I'll just | |
3030 | # go with something that's plain-text, but still capable of | |
3031 | # being randomly accessed. So here's my ultra-simple fixed-width | |
3032 | # database. All records are 40 characters + "\n", so it's easy to seek | |
3033 | # to a revision: (41 * rev) is the byte offset. | |
3034 | # A record of 40 0s denotes an empty revision. | |
3035 | # And yes, it's still pretty fast (faster than Tie::File). | |
3036 | sub revdb_set { | |
3037 | my ($file, $rev, $commit) = @_; | |
3038 | length $commit == 40 or croak "arg3 must be a full SHA1 hexsum\n"; | |
3039 | open my $fh, '+<', $file or croak $!; | |
3040 | my $offset = $rev * 41; | |
3041 | # assume that append is the common case: | |
3042 | seek $fh, 0, 2 or croak $!; | |
3043 | my $pos = tell $fh; | |
3044 | if ($pos < $offset) { | |
3045 | print $fh (('0' x 40),"\n") x (($offset - $pos) / 41); | |
3046 | } | |
3047 | seek $fh, $offset, 0 or croak $!; | |
3048 | print $fh $commit,"\n"; | |
3049 | close $fh or croak $!; | |
3050 | } | |
3051 | ||
3052 | sub revdb_get { | |
3053 | my ($file, $rev) = @_; | |
3054 | my $ret; | |
3055 | my $offset = $rev * 41; | |
3056 | open my $fh, '<', $file or croak $!; | |
3057 | seek $fh, $offset, 0; | |
3058 | if (tell $fh == $offset) { | |
3059 | $ret = readline $fh; | |
3060 | if (defined $ret) { | |
3061 | chomp $ret; | |
3062 | $ret = undef if ($ret =~ /^0{40}$/); | |
3063 | } | |
3064 | } | |
3065 | close $fh or croak $!; | |
3066 | return $ret; | |
3067 | } | |
3068 | ||
1a82e793 EW |
3069 | sub copy_remote_ref { |
3070 | my $origin = $_cp_remote ? $_cp_remote : 'origin'; | |
3071 | my $ref = "refs/remotes/$GIT_SVN"; | |
3072 | if (safe_qx('git-ls-remote', $origin, $ref)) { | |
3073 | sys(qw/git fetch/, $origin, "$ref:$ref"); | |
3074 | } else { | |
3075 | die "Unable to find remote reference: ", | |
3076 | "refs/remotes/$GIT_SVN on $origin\n"; | |
3077 | } | |
3078 | } | |
3079 | ||
a5e0cedc EW |
3080 | package SVN::Git::Editor; |
3081 | use vars qw/@ISA/; | |
3082 | use strict; | |
3083 | use warnings; | |
3084 | use Carp qw/croak/; | |
3085 | use IO::File; | |
3086 | ||
3087 | sub new { | |
3088 | my $class = shift; | |
3089 | my $git_svn = shift; | |
3090 | my $self = SVN::Delta::Editor->new(@_); | |
3091 | bless $self, $class; | |
3092 | foreach (qw/svn_path c r ra /) { | |
3093 | die "$_ required!\n" unless (defined $git_svn->{$_}); | |
3094 | $self->{$_} = $git_svn->{$_}; | |
3095 | } | |
3096 | $self->{pool} = SVN::Pool->new; | |
3097 | $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) }; | |
3098 | $self->{rm} = { }; | |
3099 | require Digest::MD5; | |
3100 | return $self; | |
3101 | } | |
3102 | ||
3103 | sub split_path { | |
3104 | return ($_[0] =~ m#^(.*?)/?([^/]+)$#); | |
3105 | } | |
3106 | ||
3107 | sub repo_path { | |
3108 | (defined $_[1] && length $_[1]) ? "$_[0]->{svn_path}/$_[1]" | |
3109 | : $_[0]->{svn_path} | |
3110 | } | |
3111 | ||
3112 | sub url_path { | |
3113 | my ($self, $path) = @_; | |
3114 | $self->{ra}->{url} . '/' . $self->repo_path($path); | |
3115 | } | |
3116 | ||
3117 | sub rmdirs { | |
80f50749 | 3118 | my ($self, $q) = @_; |
a5e0cedc EW |
3119 | my $rm = $self->{rm}; |
3120 | delete $rm->{''}; # we never delete the url we're tracking | |
3121 | return unless %$rm; | |
3122 | ||
3123 | foreach (keys %$rm) { | |
3124 | my @d = split m#/#, $_; | |
3125 | my $c = shift @d; | |
3126 | $rm->{$c} = 1; | |
3127 | while (@d) { | |
3128 | $c .= '/' . shift @d; | |
3129 | $rm->{$c} = 1; | |
3130 | } | |
3131 | } | |
3132 | delete $rm->{$self->{svn_path}}; | |
3133 | delete $rm->{''}; # we never delete the url we're tracking | |
3134 | return unless %$rm; | |
3135 | ||
3136 | defined(my $pid = open my $fh,'-|') or croak $!; | |
3137 | if (!$pid) { | |
3138 | exec qw/git-ls-tree --name-only -r -z/, $self->{c} or croak $!; | |
3139 | } | |
3140 | local $/ = "\0"; | |
c07eee1f | 3141 | my @svn_path = split m#/#, $self->{svn_path}; |
a5e0cedc EW |
3142 | while (<$fh>) { |
3143 | chomp; | |
c07eee1f EW |
3144 | my @dn = (@svn_path, (split m#/#, $_)); |
3145 | while (pop @dn) { | |
3146 | delete $rm->{join '/', @dn}; | |
3147 | } | |
3148 | unless (%$rm) { | |
3149 | close $fh; | |
3150 | return; | |
3151 | } | |
a5e0cedc | 3152 | } |
c07eee1f EW |
3153 | close $fh; |
3154 | ||
a5e0cedc EW |
3155 | my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat}); |
3156 | foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) { | |
3157 | $self->close_directory($bat->{$d}, $p); | |
3158 | my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#); | |
80f50749 | 3159 | print "\tD+\t/$d/\n" unless $q; |
a5e0cedc EW |
3160 | $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p); |
3161 | delete $bat->{$d}; | |
3162 | } | |
3163 | } | |
3164 | ||
3165 | sub open_or_add_dir { | |
3166 | my ($self, $full_path, $baton) = @_; | |
3167 | my $p = SVN::Pool->new; | |
3168 | my $t = $self->{ra}->check_path($full_path, $self->{r}, $p); | |
3169 | $p->clear; | |
3170 | if ($t == $SVN::Node::none) { | |
3171 | return $self->add_directory($full_path, $baton, | |
3172 | undef, -1, $self->{pool}); | |
3173 | } elsif ($t == $SVN::Node::dir) { | |
3174 | return $self->open_directory($full_path, $baton, | |
3175 | $self->{r}, $self->{pool}); | |
3176 | } | |
3177 | print STDERR "$full_path already exists in repository at ", | |
3178 | "r$self->{r} and it is not a directory (", | |
3179 | ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n"; | |
3180 | exit 1; | |
3181 | } | |
3182 | ||
3183 | sub ensure_path { | |
3184 | my ($self, $path) = @_; | |
3185 | my $bat = $self->{bat}; | |
3186 | $path = $self->repo_path($path); | |
3187 | return $bat->{''} unless (length $path); | |
3188 | my @p = split m#/+#, $path; | |
3189 | my $c = shift @p; | |
3190 | $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''}); | |
3191 | while (@p) { | |
3192 | my $c0 = $c; | |
3193 | $c .= '/' . shift @p; | |
3194 | $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0}); | |
3195 | } | |
3196 | return $bat->{$c}; | |
3197 | } | |
3198 | ||
3199 | sub A { | |
80f50749 | 3200 | my ($self, $m, $q) = @_; |
a5e0cedc EW |
3201 | my ($dir, $file) = split_path($m->{file_b}); |
3202 | my $pbat = $self->ensure_path($dir); | |
3203 | my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat, | |
3204 | undef, -1); | |
80f50749 | 3205 | print "\tA\t$m->{file_b}\n" unless $q; |
a5e0cedc EW |
3206 | $self->chg_file($fbat, $m); |
3207 | $self->close_file($fbat,undef,$self->{pool}); | |
3208 | } | |
3209 | ||
3210 | sub C { | |
80f50749 | 3211 | my ($self, $m, $q) = @_; |
a5e0cedc EW |
3212 | my ($dir, $file) = split_path($m->{file_b}); |
3213 | my $pbat = $self->ensure_path($dir); | |
3214 | my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat, | |
3215 | $self->url_path($m->{file_a}), $self->{r}); | |
80f50749 | 3216 | print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $q; |
a5e0cedc EW |
3217 | $self->chg_file($fbat, $m); |
3218 | $self->close_file($fbat,undef,$self->{pool}); | |
3219 | } | |
3220 | ||
3221 | sub delete_entry { | |
3222 | my ($self, $path, $pbat) = @_; | |
3223 | my $rpath = $self->repo_path($path); | |
3224 | my ($dir, $file) = split_path($rpath); | |
3225 | $self->{rm}->{$dir} = 1; | |
3226 | $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool}); | |
3227 | } | |
3228 | ||
3229 | sub R { | |
80f50749 | 3230 | my ($self, $m, $q) = @_; |
a5e0cedc EW |
3231 | my ($dir, $file) = split_path($m->{file_b}); |
3232 | my $pbat = $self->ensure_path($dir); | |
3233 | my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat, | |
3234 | $self->url_path($m->{file_a}), $self->{r}); | |
80f50749 | 3235 | print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $q; |
a5e0cedc EW |
3236 | $self->chg_file($fbat, $m); |
3237 | $self->close_file($fbat,undef,$self->{pool}); | |
3238 | ||
3239 | ($dir, $file) = split_path($m->{file_a}); | |
3240 | $pbat = $self->ensure_path($dir); | |
3241 | $self->delete_entry($m->{file_a}, $pbat); | |
3242 | } | |
3243 | ||
3244 | sub M { | |
80f50749 | 3245 | my ($self, $m, $q) = @_; |
a5e0cedc EW |
3246 | my ($dir, $file) = split_path($m->{file_b}); |
3247 | my $pbat = $self->ensure_path($dir); | |
3248 | my $fbat = $self->open_file($self->repo_path($m->{file_b}), | |
3249 | $pbat,$self->{r},$self->{pool}); | |
80f50749 | 3250 | print "\t$m->{chg}\t$m->{file_b}\n" unless $q; |
a5e0cedc EW |
3251 | $self->chg_file($fbat, $m); |
3252 | $self->close_file($fbat,undef,$self->{pool}); | |
3253 | } | |
3254 | ||
3255 | sub T { shift->M(@_) } | |
3256 | ||
3257 | sub change_file_prop { | |
3258 | my ($self, $fbat, $pname, $pval) = @_; | |
3259 | $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool}); | |
3260 | } | |
3261 | ||
3262 | sub chg_file { | |
3263 | my ($self, $fbat, $m) = @_; | |
3264 | if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) { | |
3265 | $self->change_file_prop($fbat,'svn:executable','*'); | |
3266 | } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) { | |
3267 | $self->change_file_prop($fbat,'svn:executable',undef); | |
3268 | } | |
3269 | my $fh = IO::File->new_tmpfile or croak $!; | |
3270 | if ($m->{mode_b} =~ /^120/) { | |
3271 | print $fh 'link ' or croak $!; | |
3272 | $self->change_file_prop($fbat,'svn:special','*'); | |
3273 | } elsif ($m->{mode_a} =~ /^120/ && $m->{mode_b} !~ /^120/) { | |
3274 | $self->change_file_prop($fbat,'svn:special',undef); | |
3275 | } | |
3276 | defined(my $pid = fork) or croak $!; | |
3277 | if (!$pid) { | |
3278 | open STDOUT, '>&', $fh or croak $!; | |
3279 | exec qw/git-cat-file blob/, $m->{sha1_b} or croak $!; | |
3280 | } | |
3281 | waitpid $pid, 0; | |
3282 | croak $? if $?; | |
3283 | $fh->flush == 0 or croak $!; | |
3284 | seek $fh, 0, 0 or croak $!; | |
3285 | ||
3286 | my $md5 = Digest::MD5->new; | |
3287 | $md5->addfile($fh) or croak $!; | |
3288 | seek $fh, 0, 0 or croak $!; | |
3289 | ||
3290 | my $exp = $md5->hexdigest; | |
3291 | my $atd = $self->apply_textdelta($fbat, undef, $self->{pool}); | |
3292 | my $got = SVN::TxDelta::send_stream($fh, @$atd, $self->{pool}); | |
3293 | die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp); | |
3294 | ||
3295 | close $fh or croak $!; | |
3296 | } | |
3297 | ||
3298 | sub D { | |
80f50749 | 3299 | my ($self, $m, $q) = @_; |
a5e0cedc EW |
3300 | my ($dir, $file) = split_path($m->{file_b}); |
3301 | my $pbat = $self->ensure_path($dir); | |
80f50749 | 3302 | print "\tD\t$m->{file_b}\n" unless $q; |
a5e0cedc EW |
3303 | $self->delete_entry($m->{file_b}, $pbat); |
3304 | } | |
3305 | ||
3306 | sub close_edit { | |
3307 | my ($self) = @_; | |
3308 | my ($p,$bat) = ($self->{pool}, $self->{bat}); | |
3309 | foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) { | |
3310 | $self->close_directory($bat->{$_}, $p); | |
3311 | } | |
3312 | $self->SUPER::close_edit($p); | |
3313 | $p->clear; | |
3314 | } | |
3315 | ||
3316 | sub abort_edit { | |
3317 | my ($self) = @_; | |
3318 | $self->SUPER::abort_edit($self->{pool}); | |
3319 | $self->{pool}->clear; | |
3320 | } | |
3321 | ||
3397f9df EW |
3322 | __END__ |
3323 | ||
3324 | Data structures: | |
3325 | ||
03823184 EW |
3326 | $svn_log hashref (as returned by svn_log_raw) |
3327 | { | |
3328 | fh => file handle of the log file, | |
3329 | state => state of the log file parser (sep/msg/rev/msg_start...) | |
3330 | } | |
3397f9df | 3331 | |
03823184 | 3332 | $log_msg hashref as returned by next_log_entry($svn_log) |
3397f9df EW |
3333 | { |
3334 | msg => 'whitespace-formatted log entry | |
3335 | ', # trailing newline is preserved | |
3336 | revision => '8', # integer | |
3337 | date => '2004-02-24T17:01:44.108345Z', # commit date | |
3338 | author => 'committer name' | |
3339 | }; | |
3340 | ||
3341 | ||
3342 | @mods = array of diff-index line hashes, each element represents one line | |
3343 | of diff-index output | |
3344 | ||
3345 | diff-index line ($m hash) | |
3346 | { | |
3347 | mode_a => first column of diff-index output, no leading ':', | |
3348 | mode_b => second column of diff-index output, | |
3349 | sha1_b => sha1sum of the final blob, | |
ac8e0b91 | 3350 | chg => change type [MCRADT], |
3397f9df EW |
3351 | file_a => original file name of a file (iff chg is 'C' or 'R') |
3352 | file_b => new/current file name of a file (any chg) | |
3353 | } | |
3354 | ; | |
a5e0cedc | 3355 | |
a00439ac EW |
3356 | # retval of read_url_paths{,_all}(); |
3357 | $l_map = { | |
3358 | # repository root url | |
3359 | 'https://svn.musicpd.org' => { | |
3360 | # repository path # GIT_SVN_ID | |
3361 | 'mpd/trunk' => 'trunk', | |
3362 | 'mpd/tags/0.11.5' => 'tags/0.11.5', | |
3363 | }, | |
3364 | } | |
3365 | ||
a5e0cedc EW |
3366 | Notes: |
3367 | I don't trust the each() function on unless I created %hash myself | |
3368 | because the internal iterator may not have started at base. |