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