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