]>
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 | |
9760adcc EW |
7 | $sha1 $sha1_short $_revision |
8 | $_q $_authors %users/; | |
3397f9df | 9 | $AUTHOR = 'Eric Wong <normalperson@yhbt.net>'; |
60d02ccc | 10 | $VERSION = '@@GIT_VERSION@@'; |
13ccd6d4 | 11 | |
706587fc | 12 | $ENV{GIT_DIR} ||= '.git'; |
9fa00b65 | 13 | $Git::SVN::default_repo_id = 'svn'; |
8b8fc068 | 14 | $Git::SVN::default_ref_id = $ENV{GIT_SVN_ID} || 'git-svn'; |
6af1db44 | 15 | $Git::SVN::Ra::_log_window_size = 100; |
13ccd6d4 | 16 | |
f8c9d1d2 | 17 | $Git::SVN::Log::TZ = $ENV{TZ}; |
3397f9df | 18 | $ENV{TZ} = 'UTC'; |
a00439ac | 19 | $| = 1; # unbuffer STDOUT |
3397f9df | 20 | |
6fda05ae | 21 | sub fatal (@) { print STDERR @_; exit 1 } |
b9c85187 EW |
22 | require SVN::Core; # use()-ing this causes segfaults for me... *shrug* |
23 | require SVN::Ra; | |
24 | require SVN::Delta; | |
25 | if ($SVN::Core::VERSION lt '1.1.0') { | |
26 | fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)\n"; | |
27 | } | |
d81bf827 | 28 | push @Git::SVN::Ra::ISA, 'SVN::Ra'; |
b9c85187 EW |
29 | push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor'; |
30 | push @SVN::Git::Fetcher::ISA, 'SVN::Delta::Editor'; | |
3397f9df EW |
31 | use Carp qw/croak/; |
32 | use IO::File qw//; | |
33 | use File::Basename qw/dirname basename/; | |
34 | use File::Path qw/mkpath/; | |
79bb8d88 | 35 | use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev pass_through/; |
968bdf1f | 36 | use IPC::Open3; |
336f1714 | 37 | use Git; |
a5e0cedc | 38 | |
336f1714 EW |
39 | BEGIN { |
40 | my $s; | |
41 | foreach (qw/command command_oneline command_noisy command_output_pipe | |
42 | command_input_pipe command_close_pipe/) { | |
43 | $s .= "*SVN::Git::Editor::$_ = *SVN::Git::Fetcher::$_ = ". | |
706587fc | 44 | "*Git::SVN::Migration::$_ = ". |
f8c9d1d2 | 45 | "*Git::SVN::Log::$_ = *Git::SVN::$_ = *$_ = *Git::$_; "; |
336f1714 EW |
46 | } |
47 | eval $s; | |
48 | } | |
49 | ||
b9c85187 | 50 | my ($SVN); |
83e9940a | 51 | |
f8c9d1d2 EW |
52 | $sha1 = qr/[a-f\d]{40}/; |
53 | $sha1_short = qr/[a-f\d]{4,40}/; | |
44320b9e | 54 | my ($_stdin, $_help, $_edit, |
9760adcc | 55 | $_message, $_file, |
d05d72e0 | 56 | $_template, $_shared, |
e98671e5 | 57 | $_version, $_fetch_all, |
f8c9d1d2 | 58 | $_merge, $_strategy, $_dry_run, |
905f8b7d | 59 | $_prefix, $_no_checkout, $_verbose); |
0bed5eaa | 60 | $Git::SVN::_follow_parent = 1; |
706587fc EW |
61 | my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username, |
62 | 'config-dir=s' => \$Git::SVN::Ra::config_dir, | |
63 | 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache ); | |
0bed5eaa | 64 | my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent, |
dc5869c0 | 65 | 'authors-file|A=s' => \$_authors, |
ecc712dd | 66 | 'repack:i' => \$Git::SVN::_repack, |
97ae0911 EW |
67 | 'noMetadata' => \$Git::SVN::_no_metadata, |
68 | 'useSvmProps' => \$Git::SVN::_use_svm_props, | |
62e349d2 | 69 | 'useSvnsyncProps' => \$Git::SVN::_use_svnsync_props, |
6af1db44 | 70 | 'log-window-size=i' => \$Git::SVN::Ra::_log_window_size, |
1e889ef3 | 71 | 'no-checkout' => \$_no_checkout, |
80f50749 | 72 | 'quiet|q' => \$_q, |
ecc712dd EW |
73 | 'repack-flags|repack-args|repack-opts=s' => |
74 | \$Git::SVN::_repack_flags, | |
706587fc | 75 | %remote_opts ); |
36f5b1f0 | 76 | |
9d55b41a | 77 | my ($_trunk, $_tags, $_branches); |
dadc6d2a EW |
78 | my %init_opts = ( 'template=s' => \$_template, 'shared:s' => \$_shared, |
79 | 'trunk|T=s' => \$_trunk, 'tags|t=s' => \$_tags, | |
80 | 'branches|b=s' => \$_branches, 'prefix=s' => \$_prefix, | |
81 | %remote_opts ); | |
27e9fb8d | 82 | my %cmt_opts = ( 'edit|e' => \$_edit, |
24e22aa8 EW |
83 | 'rmdir' => \$SVN::Git::Editor::_rmdir, |
84 | 'find-copies-harder' => \$SVN::Git::Editor::_find_copies_harder, | |
85 | 'l=i' => \$SVN::Git::Editor::_rename_limit, | |
86 | 'copy-similarity|C=i'=> \$SVN::Git::Editor::_cp_similarity | |
27e9fb8d | 87 | ); |
9d55b41a | 88 | |
3397f9df | 89 | my %cmd = ( |
2a3240be | 90 | fetch => [ \&cmd_fetch, "Download new revisions from SVN", |
e98671e5 | 91 | { 'revision|r=s' => \$_revision, |
905f8b7d | 92 | 'fetch-all|all' => \$_fetch_all, |
e98671e5 | 93 | %fc_opts } ], |
0425ea90 EW |
94 | clone => [ \&cmd_clone, "Initialize and fetch revisions", |
95 | { 'revision|r=s' => \$_revision, | |
96 | %fc_opts, %init_opts } ], | |
d2866f9e | 97 | init => [ \&cmd_init, "Initialize a repo for tracking" . |
f8ab6b73 | 98 | " (requires URL argument)", |
9d55b41a | 99 | \%init_opts ], |
dadc6d2a EW |
100 | 'multi-init' => [ \&cmd_multi_init, |
101 | "Deprecated alias for ". | |
102 | "'$0 init -T<trunk> -b<branches> -t<tags>'", | |
103 | \%init_opts ], | |
d7ad3bed EW |
104 | dcommit => [ \&cmd_dcommit, |
105 | 'Commit several diffs to merge with upstream', | |
3289e86e EW |
106 | { 'merge|m|M' => \$_merge, |
107 | 'strategy|s=s' => \$_strategy, | |
905f8b7d | 108 | 'verbose|v' => \$_verbose, |
3289e86e | 109 | 'dry-run|n' => \$_dry_run, |
905f8b7d | 110 | 'fetch-all|all' => \$_fetch_all, |
4b155223 | 111 | %cmt_opts, %fc_opts } ], |
1ce255dc EW |
112 | 'set-tree' => [ \&cmd_set_tree, |
113 | "Set an SVN repository to a git tree-ish", | |
114 | { 'stdin|' => \$_stdin, %cmt_opts, %fc_opts, } ], | |
5969cbe1 | 115 | 'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings", |
a5e0cedc | 116 | { 'revision|r=i' => \$_revision } ], |
1c8443b0 | 117 | 'multi-fetch' => [ \&cmd_multi_fetch, |
e98671e5 EW |
118 | "Deprecated alias for $0 fetch --all", |
119 | { 'revision|r=s' => \$_revision, %fc_opts } ], | |
706587fc EW |
120 | 'migrate' => [ sub { }, |
121 | # no-op, we automatically run this anyways, | |
706587fc EW |
122 | 'Migrate configuration/metadata/layout from |
123 | previous versions of git-svn', | |
a836a0e1 EW |
124 | { 'minimize' => \$Git::SVN::Migration::_minimize, |
125 | %remote_opts } ], | |
f8c9d1d2 EW |
126 | 'log' => [ \&Git::SVN::Log::cmd_show_log, 'Show commit logs', |
127 | { 'limit=i' => \$Git::SVN::Log::limit, | |
79bb8d88 | 128 | 'revision|r=s' => \$_revision, |
f8c9d1d2 EW |
129 | 'verbose|v' => \$Git::SVN::Log::verbose, |
130 | 'incremental' => \$Git::SVN::Log::incremental, | |
131 | 'oneline' => \$Git::SVN::Log::oneline, | |
132 | 'show-commit' => \$Git::SVN::Log::show_commit, | |
133 | 'non-recursive' => \$Git::SVN::Log::non_recursive, | |
79bb8d88 | 134 | 'authors-file|A=s' => \$_authors, |
f8c9d1d2 EW |
135 | 'color' => \$Git::SVN::Log::color, |
136 | 'pager=s' => \$Git::SVN::Log::pager, | |
79bb8d88 | 137 | } ], |
905f8b7d EW |
138 | 'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory", |
139 | { 'merge|m|M' => \$_merge, | |
140 | 'verbose|v' => \$_verbose, | |
141 | 'strategy|s=s' => \$_strategy, | |
142 | 'fetch-all|all' => \$_fetch_all, | |
143 | %fc_opts } ], | |
44320b9e EW |
144 | 'commit-diff' => [ \&cmd_commit_diff, |
145 | 'Commit a diff between two trees', | |
27e9fb8d EW |
146 | { 'message|m=s' => \$_message, |
147 | 'file|F=s' => \$_file, | |
45bf473a | 148 | 'revision|r=s' => \$_revision, |
27e9fb8d | 149 | %cmt_opts } ], |
3397f9df | 150 | ); |
9d55b41a | 151 | |
3397f9df EW |
152 | my $cmd; |
153 | for (my $i = 0; $i < @ARGV; $i++) { | |
154 | if (defined $cmd{$ARGV[$i]}) { | |
155 | $cmd = $ARGV[$i]; | |
156 | splice @ARGV, $i, 1; | |
157 | last; | |
158 | } | |
159 | }; | |
160 | ||
448c81b4 | 161 | my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd); |
a9612be2 | 162 | |
b8c92cad | 163 | read_repo_config(\%opts); |
9760adcc EW |
164 | my $rv = GetOptions(%opts, 'help|H|h' => \$_help, 'version|V' => \$_version, |
165 | 'minimize-connections' => \$Git::SVN::Migration::_minimize, | |
166 | 'id|i=s' => \$Git::SVN::default_ref_id, | |
befc9adc EW |
167 | 'svn-remote|remote|R=s' => sub { |
168 | $Git::SVN::no_reuse_existing = 1; | |
169 | $Git::SVN::default_repo_id = $_[1] }); | |
79bb8d88 | 170 | exit 1 if (!$rv && $cmd ne 'log'); |
6f0783cf | 171 | |
3397f9df | 172 | usage(0) if $_help; |
551ce28f | 173 | version() if $_version; |
eeb0abe0 EW |
174 | usage(1) unless defined $cmd; |
175 | load_authors() if $_authors; | |
0425ea90 | 176 | unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) { |
706587fc EW |
177 | Git::SVN::Migration::migration_check(); |
178 | } | |
ecc712dd | 179 | Git::SVN::init_vars(); |
b805b44a EW |
180 | eval { |
181 | Git::SVN::verify_remotes_sanity(); | |
182 | $cmd{$cmd}->[0]->(@ARGV); | |
183 | }; | |
184 | fatal $@ if $@; | |
1e889ef3 | 185 | post_fetch_checkout(); |
3397f9df EW |
186 | exit 0; |
187 | ||
188 | ####################### primary functions ###################### | |
189 | sub usage { | |
190 | my $exit = shift || 0; | |
191 | my $fd = $exit ? \*STDERR : \*STDOUT; | |
192 | print $fd <<""; | |
193 | git-svn - bidirectional operations between a single Subversion tree and git | |
194 | Usage: $0 <command> [options] [arguments]\n | |
448c81b4 EW |
195 | |
196 | print $fd "Available commands:\n" unless $cmd; | |
3397f9df EW |
197 | |
198 | foreach (sort keys %cmd) { | |
448c81b4 | 199 | next if $cmd && $cmd ne $_; |
a836a0e1 | 200 | next if /^multi-/; # don't show deprecated commands |
b203b769 | 201 | print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n"; |
448c81b4 EW |
202 | foreach (keys %{$cmd{$_}->[2]}) { |
203 | # prints out arguments as they should be passed: | |
b8c92cad | 204 | my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : ''; |
b203b769 | 205 | print $fd ' ' x 21, join(', ', map { length $_ > 1 ? |
448c81b4 EW |
206 | "--$_" : "-$_" } |
207 | split /\|/,$_)," $x\n"; | |
208 | } | |
3397f9df EW |
209 | } |
210 | print $fd <<""; | |
448c81b4 EW |
211 | \nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an |
212 | arbitrary identifier if you're tracking multiple SVN branches/repositories in | |
213 | one git repository and want to keep them separate. See git-svn(1) for more | |
214 | information. | |
3397f9df EW |
215 | |
216 | exit $exit; | |
217 | } | |
218 | ||
551ce28f | 219 | sub version { |
7d60ab2c | 220 | print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n"; |
551ce28f EW |
221 | exit 0; |
222 | } | |
223 | ||
8164b652 EW |
224 | sub do_git_init_db { |
225 | unless (-d $ENV{GIT_DIR}) { | |
226 | my @init_db = ('init'); | |
227 | push @init_db, "--template=$_template" if defined $_template; | |
dadc6d2a EW |
228 | if (defined $_shared) { |
229 | if ($_shared =~ /[a-z]/) { | |
230 | push @init_db, "--shared=$_shared"; | |
231 | } else { | |
232 | push @init_db, "--shared"; | |
233 | } | |
234 | } | |
8164b652 EW |
235 | command_noisy(@init_db); |
236 | } | |
237 | } | |
238 | ||
dadc6d2a EW |
239 | sub init_subdir { |
240 | my $repo_path = shift or return; | |
241 | mkpath([$repo_path]) unless -d $repo_path; | |
242 | chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n"; | |
243 | $ENV{GIT_DIR} = $repo_path . "/.git"; | |
244 | } | |
245 | ||
0425ea90 EW |
246 | sub cmd_clone { |
247 | my ($url, $path) = @_; | |
248 | if (!defined $path && | |
249 | (defined $_trunk || defined $_branches || defined $_tags) && | |
250 | $url !~ m#^[a-z\+]+://#) { | |
251 | $path = $url; | |
252 | } | |
253 | warn "--path: $path\n" if defined $path; | |
254 | $path = basename($url) if !defined $path || !length $path; | |
255 | warn "++path: $path\n" if defined $path; | |
256 | mkpath([$path]); | |
257 | chdir $path or die "Couldn't chdir to $path\n"; | |
258 | cmd_init(@_); | |
259 | Git::SVN::fetch_all($Git::SVN::default_repo_id); | |
260 | } | |
261 | ||
d2866f9e | 262 | sub cmd_init { |
dadc6d2a EW |
263 | if (defined $_trunk || defined $_branches || defined $_tags) { |
264 | return cmd_multi_init(@_); | |
03e0ea87 | 265 | } |
dadc6d2a EW |
266 | my $url = shift or die "SVN repository location required ", |
267 | "as a command-line argument\n"; | |
268 | init_subdir(@_); | |
8164b652 | 269 | do_git_init_db(); |
03e0ea87 | 270 | |
706587fc | 271 | Git::SVN->init($url); |
3397f9df EW |
272 | } |
273 | ||
2a3240be | 274 | sub cmd_fetch { |
e98671e5 EW |
275 | if (grep /^\d+=./, @_) { |
276 | die "'<rev>=<commit>' fetch arguments are ", | |
277 | "no longer supported.\n"; | |
07a1c950 | 278 | } |
e98671e5 EW |
279 | my ($remote) = @_; |
280 | if (@_ > 1) { | |
905f8b7d | 281 | die "Usage: $0 fetch [--all] [svn-remote]\n"; |
e98671e5 EW |
282 | } |
283 | $remote ||= $Git::SVN::default_repo_id; | |
284 | if ($_fetch_all) { | |
285 | cmd_multi_fetch(); | |
286 | } else { | |
287 | Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes()); | |
1c8443b0 | 288 | } |
2a3240be EW |
289 | } |
290 | ||
1ce255dc | 291 | sub cmd_set_tree { |
3397f9df EW |
292 | my (@commits) = @_; |
293 | if ($_stdin || !@commits) { | |
294 | print "Reading from stdin...\n"; | |
295 | @commits = (); | |
296 | while (<STDIN>) { | |
1ca72aef | 297 | if (/\b($sha1_short)\b/o) { |
3397f9df EW |
298 | unshift @commits, $1; |
299 | } | |
300 | } | |
301 | } | |
302 | my @revs; | |
8de010ad | 303 | foreach my $c (@commits) { |
aef4e921 | 304 | my @tmp = command('rev-parse',$c); |
8de010ad EW |
305 | if (scalar @tmp == 1) { |
306 | push @revs, $tmp[0]; | |
307 | } elsif (scalar @tmp > 1) { | |
aef4e921 | 308 | push @revs, reverse(command('rev-list',@tmp)); |
8de010ad | 309 | } else { |
1ce255dc | 310 | fatal "Failed to rev-parse $c\n"; |
8de010ad | 311 | } |
3397f9df | 312 | } |
1ce255dc EW |
313 | my $gs = Git::SVN->new; |
314 | my ($r_last, $cmt_last) = $gs->last_rev_commit; | |
315 | $gs->fetch; | |
97f6987a | 316 | if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) { |
1ce255dc EW |
317 | fatal "There are new revisions that were fetched ", |
318 | "and need to be merged (or acknowledged) ", | |
319 | "before committing.\nlast rev: $r_last\n", | |
320 | " current: $gs->{last_rev}\n"; | |
a5e0cedc | 321 | } |
1ce255dc EW |
322 | $gs->set_tree($_) foreach @revs; |
323 | print "Done committing ",scalar @revs," revisions to SVN\n"; | |
a5e0cedc | 324 | } |
8f22562c | 325 | |
d7ad3bed EW |
326 | sub cmd_dcommit { |
327 | my $head = shift; | |
d7ad3bed | 328 | $head ||= 'HEAD'; |
a8ae2623 | 329 | my @refs; |
905f8b7d EW |
330 | my ($url, $rev, $uuid) = working_head_info($head, \@refs); |
331 | my $c = $refs[-1]; | |
a8ae2623 EW |
332 | unless (defined $url && defined $rev && defined $uuid) { |
333 | die "Unable to determine upstream SVN information from ", | |
905f8b7d | 334 | "$head history\n"; |
a8ae2623 | 335 | } |
60d9c97a | 336 | my $gs = Git::SVN->find_by_url($url); |
45bf473a | 337 | my $last_rev; |
a8ae2623 | 338 | foreach my $d (@refs) { |
aef4e921 | 339 | if (!verify_ref("$d~1")) { |
d7ad3bed EW |
340 | fatal "Commit $d\n", |
341 | "has no parent commit, and therefore ", | |
342 | "nothing to diff against.\n", | |
343 | "You should be working from a repository ", | |
344 | "originally created by git-svn\n"; | |
48d044b5 | 345 | } |
45bf473a EW |
346 | unless (defined $last_rev) { |
347 | (undef, $last_rev, undef) = cmt_metadata("$d~1"); | |
348 | unless (defined $last_rev) { | |
d7ad3bed EW |
349 | fatal "Unable to extract revision information ", |
350 | "from commit $d~1\n"; | |
45bf473a EW |
351 | } |
352 | } | |
b22d4497 EW |
353 | if ($_dry_run) { |
354 | print "diff-tree $d~1 $d\n"; | |
355 | } else { | |
d7ad3bed | 356 | my %ed_opts = ( r => $last_rev, |
61395354 | 357 | log => get_commit_entry($d)->{log}, |
a8ae2623 | 358 | ra => Git::SVN::Ra->new($url), |
61395354 EW |
359 | tree_a => "$d~1", |
360 | tree_b => $d, | |
361 | editor_cb => sub { | |
362 | print "Committed r$_[0]\n"; | |
363 | $last_rev = $_[0]; }, | |
a8ae2623 | 364 | svn_path => ''); |
61395354 | 365 | if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) { |
d7ad3bed EW |
366 | print "No changes\n$d~1 == $d\n"; |
367 | } | |
b22d4497 EW |
368 | } |
369 | } | |
370 | return if $_dry_run; | |
60d9c97a EW |
371 | unless ($gs) { |
372 | warn "Could not determine fetch information for $url\n", | |
373 | "Will not attempt to fetch and rebase commits.\n", | |
374 | "This probably means you have useSvmProps and should\n", | |
375 | "now resync your SVN::Mirror repository.\n"; | |
376 | return; | |
377 | } | |
905f8b7d | 378 | $_fetch_all ? $gs->fetch_all : $gs->fetch; |
d7ad3bed EW |
379 | # we always want to rebase against the current HEAD, not any |
380 | # head that was passed to us | |
381 | my @diff = command('diff-tree', 'HEAD', $gs->refname, '--'); | |
b22d4497 EW |
382 | my @finish; |
383 | if (@diff) { | |
905f8b7d | 384 | @finish = rebase_cmd(); |
d7ad3bed EW |
385 | print STDERR "W: HEAD and ", $gs->refname, " differ, ", |
386 | "using @finish:\n", "@diff"; | |
b22d4497 | 387 | } else { |
d7ad3bed EW |
388 | print "No changes between current HEAD and ", |
389 | $gs->refname, "\nResetting to the latest ", | |
390 | $gs->refname, "\n"; | |
4769489a | 391 | @finish = qw/reset --mixed/; |
b22d4497 | 392 | } |
d7ad3bed | 393 | command_noisy(@finish, $gs->refname); |
b22d4497 EW |
394 | } |
395 | ||
905f8b7d EW |
396 | sub cmd_rebase { |
397 | command_noisy(qw/update-index --refresh/); | |
398 | my $url = (working_head_info('HEAD'))[0]; | |
399 | if (!defined $url) { | |
400 | die "Unable to determine upstream SVN information from ", | |
401 | "working tree history\n"; | |
402 | } | |
403 | ||
404 | my $gs = Git::SVN->find_by_url($url); | |
405 | if (command(qw/diff-index HEAD --/)) { | |
406 | print STDERR "Cannot rebase with uncommited changes:\n"; | |
407 | command_noisy('status'); | |
408 | exit 1; | |
409 | } | |
410 | $_fetch_all ? $gs->fetch_all : $gs->fetch; | |
411 | command_noisy(rebase_cmd(), $gs->refname); | |
412 | } | |
413 | ||
5969cbe1 EW |
414 | sub cmd_show_ignore { |
415 | my $gs = Git::SVN->new; | |
416 | my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum); | |
417 | $gs->traverse_ignore(\*STDOUT, '', $r); | |
a5e0cedc EW |
418 | } |
419 | ||
8164b652 | 420 | sub cmd_multi_init { |
9d55b41a | 421 | my $url = shift; |
98327e58 EW |
422 | unless (defined $_trunk || defined $_branches || defined $_tags) { |
423 | usage(1); | |
9d55b41a | 424 | } |
8164b652 EW |
425 | do_git_init_db(); |
426 | $_prefix = '' unless defined $_prefix; | |
dadc6d2a EW |
427 | if (defined $url) { |
428 | $url =~ s#/+$##; | |
429 | init_subdir(@_); | |
430 | } | |
98327e58 | 431 | if (defined $_trunk) { |
706587fc EW |
432 | my $trunk_ref = $_prefix . 'trunk'; |
433 | # try both old-style and new-style lookups: | |
434 | my $gs_trunk = eval { Git::SVN->new($trunk_ref) }; | |
8164b652 | 435 | unless ($gs_trunk) { |
706587fc EW |
436 | my ($trunk_url, $trunk_path) = |
437 | complete_svn_url($url, $_trunk); | |
438 | $gs_trunk = Git::SVN->init($trunk_url, $trunk_path, | |
439 | undef, $trunk_ref); | |
98327e58 | 440 | } |
c35b96e7 | 441 | } |
706587fc | 442 | return unless defined $_branches || defined $_tags; |
e7db67e6 EW |
443 | my $ra = $url ? Git::SVN::Ra->new($url) : undef; |
444 | complete_url_ls_init($ra, $_branches, '--branches/-b', $_prefix); | |
445 | complete_url_ls_init($ra, $_tags, '--tags/-t', $_prefix . 'tags/'); | |
9d55b41a EW |
446 | } |
447 | ||
1c8443b0 | 448 | sub cmd_multi_fetch { |
0af9c9f9 EW |
449 | my $remotes = Git::SVN::read_all_remotes(); |
450 | foreach my $repo_id (sort keys %$remotes) { | |
db03cd24 | 451 | if ($remotes->{$repo_id}->{url}) { |
4bb9ed04 EW |
452 | Git::SVN::fetch_all($repo_id, $remotes); |
453 | } | |
9d55b41a | 454 | } |
9d55b41a EW |
455 | } |
456 | ||
44320b9e EW |
457 | # this command is special because it requires no metadata |
458 | sub cmd_commit_diff { | |
459 | my ($ta, $tb, $url) = @_; | |
460 | my $usage = "Usage: $0 commit-diff -r<revision> ". | |
461 | "<tree-ish> <tree-ish> [<URL>]\n"; | |
462 | fatal($usage) if (!defined $ta || !defined $tb); | |
d3a840dc | 463 | my $svn_path; |
44320b9e EW |
464 | if (!defined $url) { |
465 | my $gs = eval { Git::SVN->new }; | |
466 | if (!$gs) { | |
467 | fatal("Needed URL or usable git-svn --id in ", | |
468 | "the command-line\n", $usage); | |
469 | } | |
470 | $url = $gs->{url}; | |
d3a840dc | 471 | $svn_path = $gs->{path}; |
44320b9e EW |
472 | } |
473 | unless (defined $_revision) { | |
474 | fatal("-r|--revision is a required argument\n", $usage); | |
475 | } | |
476 | if (defined $_message && defined $_file) { | |
477 | fatal("Both --message/-m and --file/-F specified ", | |
478 | "for the commit message.\n", | |
479 | "I have no idea what you mean\n"); | |
480 | } | |
481 | if (defined $_file) { | |
482 | $_message = file_to_s($_file); | |
483 | } else { | |
484 | $_message ||= get_commit_entry($tb)->{log}; | |
485 | } | |
486 | my $ra ||= Git::SVN::Ra->new($url); | |
d3a840dc | 487 | $svn_path ||= $ra->{svn_path}; |
44320b9e EW |
488 | my $r = $_revision; |
489 | if ($r eq 'HEAD') { | |
490 | $r = $ra->get_latest_revnum; | |
491 | } elsif ($r !~ /^\d+$/) { | |
492 | die "revision argument: $r not understood by git-svn\n"; | |
493 | } | |
61395354 EW |
494 | my %ed_opts = ( r => $r, |
495 | log => $_message, | |
496 | ra => $ra, | |
497 | tree_a => $ta, | |
498 | tree_b => $tb, | |
499 | editor_cb => sub { print "Committed r$_[0]\n" }, | |
500 | svn_path => $svn_path ); | |
501 | if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) { | |
44320b9e EW |
502 | print "No changes\n$ta == $tb\n"; |
503 | } | |
44320b9e EW |
504 | } |
505 | ||
3397f9df EW |
506 | ########################### utility functions ######################### |
507 | ||
905f8b7d EW |
508 | sub rebase_cmd { |
509 | my @cmd = qw/rebase/; | |
510 | push @cmd, '-v' if $_verbose; | |
511 | push @cmd, qw/--merge/ if $_merge; | |
512 | push @cmd, "--strategy=$_strategy" if $_strategy; | |
513 | @cmd; | |
514 | } | |
515 | ||
1e889ef3 EW |
516 | sub post_fetch_checkout { |
517 | return if $_no_checkout; | |
518 | my $gs = $Git::SVN::_head or return; | |
519 | return if verify_ref('refs/heads/master^0'); | |
520 | ||
521 | my $valid_head = verify_ref('HEAD^0'); | |
522 | command_noisy(qw(update-ref refs/heads/master), $gs->refname); | |
523 | return if ($valid_head || !verify_ref('HEAD^0')); | |
524 | ||
525 | return if $ENV{GIT_DIR} !~ m#^(?:.*/)?\.git$#; | |
526 | my $index = $ENV{GIT_INDEX_FILE} || "$ENV{GIT_DIR}/index"; | |
527 | return if -f $index; | |
528 | ||
529 | chomp(my $bare = `git config --bool --get core.bare`); | |
530 | return if $bare eq 'true'; | |
531 | return if command_oneline(qw/rev-parse --is-inside-git-dir/) eq 'true'; | |
532 | command_noisy(qw/read-tree -m -u -v HEAD HEAD/); | |
533 | print STDERR "Checked out HEAD:\n ", | |
534 | $gs->full_url, " r", $gs->last_rev, "\n"; | |
535 | } | |
536 | ||
98327e58 EW |
537 | sub complete_svn_url { |
538 | my ($url, $path) = @_; | |
539 | $path =~ s#/+$##; | |
98327e58 | 540 | if ($path !~ m#^[a-z\+]+://#) { |
98327e58 EW |
541 | if (!defined $url || $url !~ m#^[a-z\+]+://#) { |
542 | fatal("E: '$path' is not a complete URL ", | |
543 | "and a separate URL is not specified\n"); | |
544 | } | |
706587fc | 545 | return ($url, $path); |
98327e58 | 546 | } |
706587fc | 547 | return ($path, ''); |
98327e58 EW |
548 | } |
549 | ||
9d55b41a | 550 | sub complete_url_ls_init { |
706587fc EW |
551 | my ($ra, $repo_path, $switch, $pfx) = @_; |
552 | unless ($repo_path) { | |
9d55b41a EW |
553 | print STDERR "W: $switch not specified\n"; |
554 | return; | |
555 | } | |
706587fc EW |
556 | $repo_path =~ s#/+$##; |
557 | if ($repo_path =~ m#^[a-z\+]+://#) { | |
558 | $ra = Git::SVN::Ra->new($repo_path); | |
559 | $repo_path = ''; | |
e7db67e6 | 560 | } else { |
706587fc | 561 | $repo_path =~ s#^/+##; |
e7db67e6 | 562 | unless ($ra) { |
706587fc | 563 | fatal("E: '$repo_path' is not a complete URL ", |
e7db67e6 | 564 | "and a separate URL is not specified\n"); |
8164b652 | 565 | } |
e7db67e6 | 566 | } |
706587fc | 567 | my $url = $ra->{url}; |
b4d57e5e EW |
568 | my $gs = Git::SVN->init($url, undef, undef, undef, 1); |
569 | my $k = "svn-remote.$gs->{repo_id}.url"; | |
570 | my $orig_url = eval { command_oneline(qw/config --get/, $k) }; | |
571 | if ($orig_url && ($orig_url ne $gs->{url})) { | |
572 | die "$k already set: $orig_url\n", | |
573 | "wanted to set to: $gs->{url}\n"; | |
88cf4107 | 574 | } |
b4d57e5e EW |
575 | command_oneline('config', $k, $gs->{url}) unless $orig_url; |
576 | my $remote_path = "$ra->{svn_path}/$repo_path/*"; | |
577 | $remote_path =~ s#/+#/#g; | |
578 | $remote_path =~ s#^/##g; | |
579 | my ($n) = ($switch =~ /^--(\w+)/); | |
580 | if (length $pfx && $pfx !~ m#/$#) { | |
581 | die "--prefix='$pfx' must have a trailing slash '/'\n"; | |
9d55b41a | 582 | } |
b4d57e5e EW |
583 | command_noisy('config', "svn-remote.$gs->{repo_id}.$n", |
584 | "$remote_path:refs/remotes/$pfx*"); | |
9d55b41a EW |
585 | } |
586 | ||
aef4e921 EW |
587 | sub verify_ref { |
588 | my ($ref) = @_; | |
2c5c1d53 EW |
589 | eval { command_oneline([ 'rev-parse', '--verify', $ref ], |
590 | { STDERR => 0 }); }; | |
aef4e921 EW |
591 | } |
592 | ||
a5e0cedc | 593 | sub get_tree_from_treeish { |
cf52b8f0 | 594 | my ($treeish) = @_; |
44320b9e | 595 | # $treeish can be a symbolic ref, too: |
aef4e921 | 596 | my $type = command_oneline(qw/cat-file -t/, $treeish); |
cf52b8f0 EW |
597 | my $expected; |
598 | while ($type eq 'tag') { | |
aef4e921 | 599 | ($treeish, $type) = command(qw/cat-file tag/, $treeish); |
cf52b8f0 EW |
600 | } |
601 | if ($type eq 'commit') { | |
aef4e921 EW |
602 | $expected = (grep /^tree /, command(qw/cat-file commit/, |
603 | $treeish))[0]; | |
44320b9e | 604 | ($expected) = ($expected =~ /^tree ($sha1)$/o); |
cf52b8f0 EW |
605 | die "Unable to get tree from $treeish\n" unless $expected; |
606 | } elsif ($type eq 'tree') { | |
607 | $expected = $treeish; | |
608 | } else { | |
609 | die "$treeish is a $type, expected tree, tag or commit\n"; | |
610 | } | |
a5e0cedc EW |
611 | return $expected; |
612 | } | |
613 | ||
44320b9e EW |
614 | sub get_commit_entry { |
615 | my ($treeish) = shift; | |
616 | my %log_entry = ( log => '', tree => get_tree_from_treeish($treeish) ); | |
617 | my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG"; | |
618 | my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG"; | |
619 | open my $log_fh, '>', $commit_editmsg or croak $!; | |
3397f9df | 620 | |
44320b9e | 621 | my $type = command_oneline(qw/cat-file -t/, $treeish); |
4ad4515d | 622 | if ($type eq 'commit' || $type eq 'tag') { |
aef4e921 | 623 | my ($msg_fh, $ctx) = command_output_pipe('cat-file', |
44320b9e | 624 | $type, $treeish); |
3397f9df EW |
625 | my $in_msg = 0; |
626 | while (<$msg_fh>) { | |
627 | if (!$in_msg) { | |
628 | $in_msg = 1 if (/^\s*$/); | |
df746c5a | 629 | } elsif (/^git-svn-id: /) { |
44320b9e EW |
630 | # skip this for now, we regenerate the |
631 | # correct one on re-fetch anyways | |
632 | # TODO: set *:merge properties or like... | |
3397f9df | 633 | } else { |
44320b9e | 634 | print $log_fh $_ or croak $!; |
3397f9df EW |
635 | } |
636 | } | |
aef4e921 | 637 | command_close_pipe($msg_fh, $ctx); |
3397f9df | 638 | } |
44320b9e | 639 | close $log_fh or croak $!; |
3397f9df EW |
640 | |
641 | if ($_edit || ($type eq 'tree')) { | |
642 | my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi'; | |
44320b9e EW |
643 | # TODO: strip out spaces, comments, like git-commit.sh |
644 | system($editor, $commit_editmsg); | |
3397f9df | 645 | } |
44320b9e EW |
646 | rename $commit_editmsg, $commit_msg or croak $!; |
647 | open $log_fh, '<', $commit_msg or croak $!; | |
648 | { local $/; chomp($log_entry{log} = <$log_fh>); } | |
649 | close $log_fh or croak $!; | |
650 | unlink $commit_msg; | |
651 | \%log_entry; | |
a5e0cedc EW |
652 | } |
653 | ||
3397f9df EW |
654 | sub s_to_file { |
655 | my ($str, $file, $mode) = @_; | |
656 | open my $fd,'>',$file or croak $!; | |
657 | print $fd $str,"\n" or croak $!; | |
658 | close $fd or croak $!; | |
659 | chmod ($mode &~ umask, $file) if (defined $mode); | |
660 | } | |
661 | ||
662 | sub file_to_s { | |
663 | my $file = shift; | |
664 | open my $fd,'<',$file or croak "$!: file: $file\n"; | |
665 | local $/; | |
666 | my $ret = <$fd>; | |
667 | close $fd or croak $!; | |
668 | $ret =~ s/\s*$//s; | |
669 | return $ret; | |
670 | } | |
671 | ||
eeb0abe0 EW |
672 | # '<svn username> = real-name <email address>' mapping based on git-svnimport: |
673 | sub load_authors { | |
674 | open my $authors, '<', $_authors or die "Can't open $_authors $!\n"; | |
f8c9d1d2 | 675 | my $log = $cmd eq 'log'; |
eeb0abe0 EW |
676 | while (<$authors>) { |
677 | chomp; | |
8815788e | 678 | next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/; |
eeb0abe0 | 679 | my ($user, $name, $email) = ($1, $2, $3); |
f8c9d1d2 EW |
680 | if ($log) { |
681 | $Git::SVN::Log::rusers{"$name <$email>"} = $user; | |
682 | } else { | |
683 | $users{$user} = [$name, $email]; | |
684 | } | |
79bb8d88 EW |
685 | } |
686 | close $authors or croak $!; | |
687 | } | |
688 | ||
e0d10e1c | 689 | # convert GetOpt::Long specs for use by git-config |
b8c92cad | 690 | sub read_repo_config { |
706587fc | 691 | return unless -d $ENV{GIT_DIR}; |
b8c92cad | 692 | my $opts = shift; |
97ae0911 | 693 | my @config_only; |
b8c92cad | 694 | foreach my $o (keys %$opts) { |
97ae0911 EW |
695 | # if we have mixedCase and a long option-only, then |
696 | # it's a config-only variable that we don't need for | |
697 | # the command-line. | |
698 | push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i); | |
b8c92cad | 699 | my $v = $opts->{$o}; |
97ae0911 | 700 | my ($key) = ($o =~ /^([a-zA-Z\-]+)/); |
b8c92cad | 701 | $key =~ s/-//g; |
e0d10e1c | 702 | my $arg = 'git-config'; |
b8c92cad EW |
703 | $arg .= ' --int' if ($o =~ /[:=]i$/); |
704 | $arg .= ' --bool' if ($o !~ /[:=][sfi]$/); | |
705 | if (ref $v eq 'ARRAY') { | |
706 | chomp(my @tmp = `$arg --get-all svn.$key`); | |
707 | @$v = @tmp if @tmp; | |
708 | } else { | |
709 | chomp(my $tmp = `$arg --get svn.$key`); | |
7774284a | 710 | if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) { |
b8c92cad EW |
711 | $$v = $tmp; |
712 | } | |
713 | } | |
714 | } | |
97ae0911 | 715 | delete @$opts{@config_only} if @config_only; |
b8c92cad EW |
716 | } |
717 | ||
79bb8d88 | 718 | sub extract_metadata { |
c1927a85 | 719 | my $id = shift or return (undef, undef, undef); |
79bb8d88 EW |
720 | my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+) |
721 | \s([a-f\d\-]+)$/x); | |
e70dc780 | 722 | if (!defined $rev || !$uuid || !$url) { |
79bb8d88 | 723 | # some of the original repositories I made had |
82e5a82f | 724 | # identifiers like this: |
79bb8d88 EW |
725 | ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/); |
726 | } | |
727 | return ($url, $rev, $uuid); | |
728 | } | |
729 | ||
c1927a85 EW |
730 | sub cmt_metadata { |
731 | return extract_metadata((grep(/^git-svn-id: /, | |
aef4e921 | 732 | command(qw/cat-file commit/, shift)))[-1]); |
c1927a85 EW |
733 | } |
734 | ||
905f8b7d EW |
735 | sub working_head_info { |
736 | my ($head, $refs) = @_; | |
737 | my ($url, $rev, $uuid); | |
738 | my ($fh, $ctx) = command_output_pipe('rev-list', $head); | |
739 | while (<$fh>) { | |
740 | chomp; | |
741 | ($url, $rev, $uuid) = cmt_metadata($_); | |
742 | last if (defined $url && defined $rev && defined $uuid); | |
743 | unshift @$refs, $_ if $refs; | |
744 | } | |
745 | close $fh; # break the pipe | |
746 | ($url, $rev, $uuid); | |
747 | } | |
748 | ||
9b981fc6 EW |
749 | package Git::SVN; |
750 | use strict; | |
751 | use warnings; | |
ecc712dd | 752 | use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent |
62e349d2 | 753 | $_repack $_repack_flags $_use_svm_props $_head |
befc9adc | 754 | $_use_svnsync_props $no_reuse_existing/; |
9b981fc6 EW |
755 | use Carp qw/croak/; |
756 | use File::Path qw/mkpath/; | |
373274f9 | 757 | use File::Copy qw/copy/; |
9b981fc6 EW |
758 | use IPC::Open3; |
759 | ||
ecc712dd | 760 | my $_repack_nr; |
9b981fc6 EW |
761 | # properties that we do not log: |
762 | my %SKIP_PROP; | |
763 | BEGIN { | |
764 | %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url | |
765 | svn:special svn:executable | |
766 | svn:entry:committed-rev | |
767 | svn:entry:last-author | |
768 | svn:entry:uuid | |
769 | svn:entry:committed-date/; | |
91b03282 EW |
770 | |
771 | # some options are read globally, but can be overridden locally | |
772 | # per [svn-remote "..."] section. Command-line options will *NOT* | |
773 | # override options set in an [svn-remote "..."] section | |
774 | my $e; | |
62e349d2 EW |
775 | foreach (qw/follow_parent no_metadata use_svm_props |
776 | use_svnsync_props/) { | |
91b03282 EW |
777 | my $key = $_; |
778 | $key =~ tr/_//d; | |
779 | $e .= "sub $_ { | |
780 | my (\$self) = \@_; | |
781 | return \$self->{-$_} if exists \$self->{-$_}; | |
782 | my \$k = \"svn-remote.\$self->{repo_id}\.$key\"; | |
783 | eval { command_oneline(qw/config --get/, \$k) }; | |
784 | if (\$@) { | |
785 | \$self->{-$_} = \$Git::SVN::_$_; | |
786 | } else { | |
787 | my \$v = command_oneline(qw/config --bool/,\$k); | |
788 | \$self->{-$_} = \$v eq 'false' ? 0 : 1; | |
789 | } | |
790 | return \$self->{-$_} }\n"; | |
791 | } | |
792 | $e .= "1;\n"; | |
793 | eval $e or die $@; | |
9b981fc6 EW |
794 | } |
795 | ||
373274f9 EW |
796 | my %LOCKFILES; |
797 | END { unlink keys %LOCKFILES if %LOCKFILES } | |
798 | ||
4bb9ed04 EW |
799 | sub resolve_local_globs { |
800 | my ($url, $fetch, $glob_spec) = @_; | |
801 | return unless defined $glob_spec; | |
802 | my $ref = $glob_spec->{ref}; | |
803 | my $path = $glob_spec->{path}; | |
804 | foreach (command(qw#for-each-ref --format=%(refname) refs/remotes#)) { | |
805 | next unless m#^refs/remotes/$ref->{regex}$#; | |
806 | my $p = $1; | |
807 | my $pathname = $path->full_path($p); | |
808 | my $refname = $ref->full_path($p); | |
809 | if (my $existing = $fetch->{$pathname}) { | |
810 | if ($existing ne $refname) { | |
811 | die "Refspec conflict:\n", | |
812 | "existing: refs/remotes/$existing\n", | |
813 | " globbed: refs/remotes/$refname\n"; | |
814 | } | |
815 | my $u = (::cmt_metadata("refs/remotes/$refname"))[0]; | |
4e9f6cc7 | 816 | $u =~ s!^\Q$url\E(/|$)!! or die |
4bb9ed04 EW |
817 | "refs/remotes/$refname: '$url' not found in '$u'\n"; |
818 | if ($pathname ne $u) { | |
819 | warn "W: Refspec glob conflict ", | |
820 | "(ref: refs/remotes/$refname):\n", | |
821 | "expected path: $pathname\n", | |
822 | " real path: $u\n", | |
823 | "Continuing ahead with $u\n"; | |
824 | next; | |
825 | } | |
826 | } else { | |
4bb9ed04 EW |
827 | $fetch->{$pathname} = $refname; |
828 | } | |
829 | } | |
830 | } | |
831 | ||
e98671e5 EW |
832 | sub parse_revision_argument { |
833 | my ($base, $head) = @_; | |
834 | if (!defined $::_revision || $::_revision eq 'BASE:HEAD') { | |
835 | return ($base, $head); | |
836 | } | |
837 | return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/); | |
838 | return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/); | |
839 | return ($head, $head) if ($::_revision eq 'HEAD'); | |
840 | return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/); | |
841 | return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/); | |
842 | die "revision argument: $::_revision not understood by git-svn\n"; | |
843 | } | |
844 | ||
0af9c9f9 | 845 | sub fetch_all { |
4bb9ed04 | 846 | my ($repo_id, $remotes) = @_; |
905f8b7d EW |
847 | if (ref $repo_id) { |
848 | my $gs = $repo_id; | |
849 | $repo_id = undef; | |
850 | $repo_id = $gs->{repo_id}; | |
851 | } | |
852 | $remotes ||= read_all_remotes(); | |
7447b4bc EW |
853 | my $remote = $remotes->{$repo_id} or |
854 | die "[svn-remote \"$repo_id\"] unknown\n"; | |
e518192f | 855 | my $fetch = $remote->{fetch}; |
7447b4bc | 856 | my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n"; |
e518192f | 857 | my (@gs, @globs); |
0af9c9f9 | 858 | my $ra = Git::SVN::Ra->new($url); |
26a62d57 | 859 | my $uuid = $ra->get_uuid; |
0af9c9f9 | 860 | my $head = $ra->get_latest_revnum; |
28710f74 | 861 | my $base = defined $fetch ? $head : 0; |
e518192f EW |
862 | |
863 | # read the max revs for wildcard expansion (branches/*, tags/*) | |
864 | foreach my $t (qw/branches tags/) { | |
865 | defined $remote->{$t} or next; | |
866 | push @globs, $remote->{$t}; | |
93f2689c EW |
867 | my $max_rev = eval { tmp_config(qw/--int --get/, |
868 | "svn-remote.$repo_id.${t}-maxRev") }; | |
869 | if (defined $max_rev && ($max_rev < $base)) { | |
870 | $base = $max_rev; | |
d6d3346b EW |
871 | } elsif (!defined $max_rev) { |
872 | $base = 0; | |
e518192f EW |
873 | } |
874 | } | |
875 | ||
db03cd24 EW |
876 | if ($fetch) { |
877 | foreach my $p (sort keys %$fetch) { | |
878 | my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p); | |
879 | my $lr = $gs->rev_db_max; | |
880 | if (defined $lr) { | |
881 | $base = $lr if ($lr < $base); | |
882 | } | |
883 | push @gs, $gs; | |
0af9c9f9 | 884 | } |
0af9c9f9 | 885 | } |
e98671e5 EW |
886 | |
887 | ($base, $head) = parse_revision_argument($base, $head); | |
e518192f | 888 | $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs); |
0af9c9f9 EW |
889 | } |
890 | ||
47e39c55 EW |
891 | sub read_all_remotes { |
892 | my $r = {}; | |
8b8fc068 | 893 | foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) { |
47e39c55 EW |
894 | if (m!^(.+)\.fetch=\s*(.*)\s*:\s*refs/remotes/(.+)\s*$!) { |
895 | $r->{$1}->{fetch}->{$2} = $3; | |
896 | } elsif (m!^(.+)\.url=\s*(.*)\s*$!) { | |
897 | $r->{$1}->{url} = $2; | |
4bb9ed04 EW |
898 | } elsif (m!^(.+)\.(branches|tags)= |
899 | (.*):refs/remotes/(.+)\s*$/!x) { | |
900 | my ($p, $g) = ($3, $4); | |
901 | my $rs = $r->{$1}->{$2} = { | |
e518192f | 902 | t => $2, |
93f2689c | 903 | remote => $1, |
4bb9ed04 EW |
904 | path => Git::SVN::GlobSpec->new($p), |
905 | ref => Git::SVN::GlobSpec->new($g) }; | |
906 | if (length($rs->{ref}->{right}) != 0) { | |
907 | die "The '*' glob character must be the last ", | |
908 | "character of '$g'\n"; | |
909 | } | |
47e39c55 EW |
910 | } |
911 | } | |
912 | $r; | |
913 | } | |
914 | ||
ecc712dd EW |
915 | sub init_vars { |
916 | if (defined $_repack) { | |
917 | $_repack = 1000 if ($_repack <= 0); | |
918 | $_repack_nr = $_repack; | |
919 | $_repack_flags ||= '-d'; | |
920 | } | |
921 | } | |
922 | ||
b805b44a | 923 | sub verify_remotes_sanity { |
536c4b09 | 924 | return unless -d $ENV{GIT_DIR}; |
b805b44a EW |
925 | my %seen; |
926 | foreach (command(qw/config -l/)) { | |
927 | if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) { | |
928 | if ($seen{$1}) { | |
929 | die "Remote ref refs/remote/$1 is tracked by", | |
930 | "\n \"$_\"\nand\n \"$seen{$1}\"\n", | |
931 | "Please resolve this ambiguity in ", | |
932 | "your git configuration file before ", | |
933 | "continuing\n"; | |
934 | } | |
935 | $seen{$1} = $_; | |
936 | } | |
937 | } | |
938 | } | |
939 | ||
47e39c55 | 940 | # we allow more chars than remotes2config.sh... |
706587fc EW |
941 | sub sanitize_remote_name { |
942 | my ($name) = @_; | |
47e39c55 | 943 | $name =~ tr{A-Za-z0-9:,/+-}{.}c; |
706587fc EW |
944 | $name; |
945 | } | |
946 | ||
e6434f87 EW |
947 | sub find_existing_remote { |
948 | my ($url, $remotes) = @_; | |
befc9adc | 949 | return undef if $no_reuse_existing; |
e6434f87 EW |
950 | my $existing; |
951 | foreach my $repo_id (keys %$remotes) { | |
952 | my $u = $remotes->{$repo_id}->{url} or next; | |
953 | next if $u ne $url; | |
954 | $existing = $repo_id; | |
955 | last; | |
956 | } | |
957 | $existing; | |
958 | } | |
b805b44a | 959 | |
e6434f87 | 960 | sub init_remote_config { |
d8115c51 | 961 | my ($self, $url, $no_write) = @_; |
e6434f87 EW |
962 | $url =~ s!/+$!!; # strip trailing slash |
963 | my $r = read_all_remotes(); | |
964 | my $existing = find_existing_remote($url, $r); | |
965 | if ($existing) { | |
e518192f EW |
966 | unless ($no_write) { |
967 | print STDERR "Using existing ", | |
968 | "[svn-remote \"$existing\"]\n"; | |
969 | } | |
e6434f87 EW |
970 | $self->{repo_id} = $existing; |
971 | } else { | |
972 | my $min_url = Git::SVN::Ra->new($url)->minimize_url; | |
973 | $existing = find_existing_remote($min_url, $r); | |
974 | if ($existing) { | |
e518192f EW |
975 | unless ($no_write) { |
976 | print STDERR "Using existing ", | |
977 | "[svn-remote \"$existing\"]\n"; | |
978 | } | |
e6434f87 EW |
979 | $self->{repo_id} = $existing; |
980 | } | |
981 | if ($min_url ne $url) { | |
e518192f EW |
982 | unless ($no_write) { |
983 | print STDERR "Using higher level of URL: ", | |
984 | "$url => $min_url\n"; | |
985 | } | |
e6434f87 EW |
986 | my $old_path = $self->{path}; |
987 | $self->{path} = $url; | |
4e9f6cc7 | 988 | $self->{path} =~ s!^\Q$min_url\E(/|$)!!; |
e6434f87 EW |
989 | if (length $old_path) { |
990 | $self->{path} .= "/$old_path"; | |
991 | } | |
992 | $url = $min_url; | |
993 | } | |
994 | } | |
995 | my $orig_url; | |
996 | if (!$existing) { | |
b805b44a | 997 | # verify that we aren't overwriting anything: |
e6434f87 | 998 | $orig_url = eval { |
706587fc | 999 | command_oneline('config', '--get', |
e6434f87 | 1000 | "svn-remote.$self->{repo_id}.url") |
706587fc | 1001 | }; |
b805b44a | 1002 | if ($orig_url && ($orig_url ne $url)) { |
e6434f87 | 1003 | die "svn-remote.$self->{repo_id}.url already set: ", |
b805b44a EW |
1004 | "$orig_url\nwanted to set to: $url\n"; |
1005 | } | |
9b981fc6 | 1006 | } |
e6434f87 EW |
1007 | my ($xrepo_id, $xpath) = find_ref($self->refname); |
1008 | if (defined $xpath) { | |
1009 | die "svn-remote.$xrepo_id.fetch already set to track ", | |
1010 | "$xpath:refs/remotes/", $self->refname, "\n"; | |
1011 | } | |
d8115c51 EW |
1012 | unless ($no_write) { |
1013 | command_noisy('config', | |
1014 | "svn-remote.$self->{repo_id}.url", $url); | |
1015 | command_noisy('config', '--add', | |
1016 | "svn-remote.$self->{repo_id}.fetch", | |
1017 | "$self->{path}:".$self->refname); | |
1018 | } | |
9b981fc6 | 1019 | $self->{url} = $url; |
e6434f87 EW |
1020 | } |
1021 | ||
a8ae2623 EW |
1022 | sub find_by_url { # repos_root and, path are optional |
1023 | my ($class, $full_url, $repos_root, $path) = @_; | |
1024 | my $remotes = read_all_remotes(); | |
1025 | if (defined $full_url && defined $repos_root && !defined $path) { | |
1026 | $path = $full_url; | |
1027 | $path =~ s#^\Q$repos_root\E(?:/|$)##; | |
1028 | } | |
1029 | foreach my $repo_id (keys %$remotes) { | |
1030 | my $u = $remotes->{$repo_id}->{url} or next; | |
1031 | next if defined $repos_root && $repos_root ne $u; | |
1032 | ||
1033 | my $fetch = $remotes->{$repo_id}->{fetch} || {}; | |
1034 | foreach (qw/branches tags/) { | |
1035 | resolve_local_globs($u, $fetch, | |
1036 | $remotes->{$repo_id}->{$_}); | |
1037 | } | |
1038 | my $p = $path; | |
1039 | unless (defined $p) { | |
1040 | $p = $full_url; | |
1041 | $p =~ s#^\Q$u\E(?:/|$)## or next; | |
1042 | } | |
1043 | foreach my $f (keys %$fetch) { | |
1044 | next if $f ne $p; | |
1045 | return Git::SVN->new($fetch->{$f}, $repo_id, $f); | |
1046 | } | |
1047 | } | |
1048 | undef; | |
1049 | } | |
1050 | ||
e6434f87 | 1051 | sub init { |
d8115c51 | 1052 | my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_; |
e6434f87 EW |
1053 | my $self = _new($class, $repo_id, $ref_id, $path); |
1054 | if (defined $url) { | |
d8115c51 | 1055 | $self->init_remote_config($url, $no_write); |
e6434f87 | 1056 | } |
9b981fc6 EW |
1057 | $self; |
1058 | } | |
1059 | ||
706587fc EW |
1060 | sub find_ref { |
1061 | my ($ref_id) = @_; | |
1062 | foreach (command(qw/config -l/)) { | |
1063 | next unless m!^svn-remote\.(.+)\.fetch= | |
1064 | \s*(.*)\s*:\s*refs/remotes/(.+)\s*$!x; | |
1065 | my ($repo_id, $path, $ref) = ($1, $2, $3); | |
1066 | if ($ref eq $ref_id) { | |
1067 | $path = '' if ($path =~ m#^\./?#); | |
1068 | return ($repo_id, $path); | |
1069 | } | |
1070 | } | |
1071 | (undef, undef, undef); | |
1072 | } | |
1073 | ||
9b981fc6 | 1074 | sub new { |
706587fc EW |
1075 | my ($class, $ref_id, $repo_id, $path) = @_; |
1076 | if (defined $ref_id && !defined $repo_id && !defined $path) { | |
1077 | ($repo_id, $path) = find_ref($ref_id); | |
1078 | if (!defined $repo_id) { | |
1079 | die "Could not find a \"svn-remote.*.fetch\" key ", | |
1080 | "in the repository configuration matching: ", | |
1081 | "refs/remotes/$ref_id\n"; | |
1082 | } | |
1083 | } | |
1084 | my $self = _new($class, $repo_id, $ref_id, $path); | |
8b8fc068 EW |
1085 | if (!defined $self->{path} || !length $self->{path}) { |
1086 | my $fetch = command_oneline('config', '--get', | |
1087 | "svn-remote.$repo_id.fetch", | |
1088 | ":refs/remotes/$ref_id\$") or | |
1089 | die "Failed to read \"svn-remote.$repo_id.fetch\" ", | |
1090 | "\":refs/remotes/$ref_id\$\" in config\n"; | |
1091 | ($self->{path}, undef) = split(/\s*:\s*/, $fetch); | |
1092 | } | |
706587fc EW |
1093 | $self->{url} = command_oneline('config', '--get', |
1094 | "svn-remote.$repo_id.url") or | |
1095 | die "Failed to read \"svn-remote.$repo_id.url\" in config\n"; | |
d6d3346b | 1096 | $self->rebuild; |
9b981fc6 EW |
1097 | $self; |
1098 | } | |
1099 | ||
706587fc | 1100 | sub refname { "refs/remotes/$_[0]->{ref_id}" } |
9b981fc6 | 1101 | |
26a62d57 EW |
1102 | sub svm_uuid { |
1103 | my ($self) = @_; | |
1104 | return $self->{svm}->{uuid} if $self->svm; | |
1105 | $self->ra; | |
1106 | unless ($self->{svm}) { | |
1107 | die "SVM UUID not cached, and reading remotely failed\n"; | |
1108 | } | |
1109 | $self->{svm}->{uuid}; | |
1110 | } | |
8a49ee97 | 1111 | |
26a62d57 EW |
1112 | sub svm { |
1113 | my ($self) = @_; | |
1114 | return $self->{svm} if $self->{svm}; | |
1115 | my $svm; | |
8a49ee97 EW |
1116 | # see if we have it in our config, first: |
1117 | eval { | |
26a62d57 EW |
1118 | my $section = "svn-remote.$self->{repo_id}"; |
1119 | $svm = { | |
93f2689c EW |
1120 | source => tmp_config('--get', "$section.svm-source"), |
1121 | uuid => tmp_config('--get', "$section.svm-uuid"), | |
befc9adc | 1122 | replace => tmp_config('--get', "$section.svm-replace"), |
8a49ee97 EW |
1123 | } |
1124 | }; | |
befc9adc EW |
1125 | if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) { |
1126 | $self->{svm} = $svm; | |
1127 | } | |
26a62d57 EW |
1128 | $self->{svm}; |
1129 | } | |
1130 | ||
1131 | sub _set_svm_vars { | |
1132 | my ($self, $ra) = @_; | |
db03cd24 EW |
1133 | return $ra if $self->svm; |
1134 | ||
1135 | my @err = ( "useSvmProps set, but failed to read SVM properties\n", | |
befc9adc | 1136 | "(svm:source, svm:uuid) ", |
db03cd24 EW |
1137 | "from the following URLs:\n" ); |
1138 | sub read_svm_props { | |
befc9adc EW |
1139 | my ($self, $ra, $path, $r) = @_; |
1140 | my $props = ($ra->get_dir($path, $r))[2]; | |
db03cd24 | 1141 | my $src = $props->{'svm:source'}; |
db03cd24 | 1142 | my $uuid = $props->{'svm:uuid'}; |
befc9adc | 1143 | return undef if (!$src || !$uuid); |
26a62d57 | 1144 | |
befc9adc | 1145 | chomp($src, $uuid); |
26a62d57 | 1146 | |
db03cd24 EW |
1147 | $uuid =~ m{^[0-9a-f\-]{30,}$} |
1148 | or die "doesn't look right - svm:uuid is '$uuid'\n"; | |
befc9adc EW |
1149 | |
1150 | # the '!' is used to mark the repos_root!/relative/path | |
1151 | $src =~ s{/?!/?}{/}; | |
db03cd24 | 1152 | $src =~ s{/+$}{}; # no trailing slashes please |
befc9adc | 1153 | # username is of no interest |
8a49ee97 | 1154 | $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1}; |
8a49ee97 | 1155 | |
befc9adc EW |
1156 | my $replace = $ra->{url}; |
1157 | $replace .= "/$path" if length $path; | |
1158 | ||
db03cd24 | 1159 | my $section = "svn-remote.$self->{repo_id}"; |
befc9adc EW |
1160 | tmp_config("$section.svm-source", $src); |
1161 | tmp_config("$section.svm-replace", $replace); | |
1162 | tmp_config("$section.svm-uuid", $uuid); | |
1163 | $self->{svm} = { | |
1164 | source => $src, | |
1165 | uuid => $uuid, | |
1166 | replace => $replace | |
1167 | }; | |
db03cd24 EW |
1168 | } |
1169 | ||
1170 | my $r = $ra->get_latest_revnum; | |
1171 | my $path = $self->{path}; | |
befc9adc | 1172 | my %tried; |
db03cd24 | 1173 | while (length $path) { |
befc9adc EW |
1174 | unless ($tried{"$self->{url}/$path"}) { |
1175 | return $ra if $self->read_svm_props($ra, $path, $r); | |
1176 | $tried{"$self->{url}/$path"} = 1; | |
db03cd24 | 1177 | } |
befc9adc | 1178 | $path =~ s#/?[^/]+$##; |
8a49ee97 | 1179 | } |
befc9adc EW |
1180 | die "Path: '$path' should be ''\n" if $path ne ''; |
1181 | return $ra if $self->read_svm_props($ra, $path, $r); | |
1182 | $tried{"$self->{url}/$path"} = 1; | |
db03cd24 EW |
1183 | |
1184 | if ($ra->{repos_root} eq $self->{url}) { | |
befc9adc | 1185 | die @err, (map { " $_\n" } keys %tried), "\n"; |
db03cd24 EW |
1186 | } |
1187 | ||
1188 | # nope, make sure we're connected to the repository root: | |
1189 | my $ok; | |
1190 | my @tried_b; | |
1191 | $path = $ra->{svn_path}; | |
db03cd24 EW |
1192 | $ra = Git::SVN::Ra->new($ra->{repos_root}); |
1193 | while (length $path) { | |
befc9adc EW |
1194 | unless ($tried{"$ra->{url}/$path"}) { |
1195 | $ok = $self->read_svm_props($ra, $path, $r); | |
1196 | last if $ok; | |
1197 | $tried{"$ra->{url}/$path"} = 1; | |
1198 | } | |
1199 | $path =~ s#/?[^/]+$##; | |
db03cd24 | 1200 | } |
befc9adc EW |
1201 | die "Path: '$path' should be ''\n" if $path ne ''; |
1202 | $ok ||= $self->read_svm_props($ra, $path, $r); | |
1203 | $tried{"$ra->{url}/$path"} = 1; | |
db03cd24 | 1204 | if (!$ok) { |
befc9adc | 1205 | die @err, (map { " $_\n" } keys %tried), "\n"; |
db03cd24 EW |
1206 | } |
1207 | Git::SVN::Ra->new($self->{url}); | |
8a49ee97 EW |
1208 | } |
1209 | ||
62e349d2 EW |
1210 | sub svnsync { |
1211 | my ($self) = @_; | |
1212 | return $self->{svnsync} if $self->{svnsync}; | |
1213 | ||
1214 | if ($self->no_metadata) { | |
1215 | die "Can't have both 'noMetadata' and ", | |
1216 | "'useSvnsyncProps' options set!\n"; | |
1217 | } | |
1218 | if ($self->rewrite_root) { | |
1219 | die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ", | |
1220 | "options set!\n"; | |
1221 | } | |
1222 | ||
1223 | my $svnsync; | |
1224 | # see if we have it in our config, first: | |
1225 | eval { | |
1226 | my $section = "svn-remote.$self->{repo_id}"; | |
1227 | $svnsync = { | |
1228 | url => tmp_config('--get', "$section.svnsync-url"), | |
1229 | uuid => tmp_config('--get', "$section.svnsync-uuid"), | |
1230 | } | |
1231 | }; | |
1232 | if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) { | |
1233 | return $self->{svnsync} = $svnsync; | |
1234 | } | |
1235 | ||
1236 | my $err = "useSvnsyncProps set, but failed to read " . | |
1237 | "svnsync property: svn:sync-from-"; | |
1238 | my $rp = $self->ra->rev_proplist(0); | |
1239 | ||
1240 | my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n"; | |
1241 | $url =~ m{^[a-z\+]+://} or | |
1242 | die "doesn't look right - svn:sync-from-url is '$url'\n"; | |
1243 | ||
1244 | my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n"; | |
1245 | $uuid =~ m{^[0-9a-f\-]{30,}$} or | |
1246 | die "doesn't look right - svn:sync-from-uuid is '$uuid'\n"; | |
1247 | ||
1248 | my $section = "svn-remote.$self->{repo_id}"; | |
1249 | tmp_config('--add', "$section.svnsync-uuid", $uuid); | |
1250 | tmp_config('--add', "$section.svnsync-url", $url); | |
1251 | return $self->{svnsync} = { url => $url, uuid => $uuid }; | |
1252 | } | |
1253 | ||
26a62d57 EW |
1254 | # this allows us to memoize our SVN::Ra UUID locally and avoid a |
1255 | # remote lookup (useful for 'git svn log'). | |
1256 | sub ra_uuid { | |
1257 | my ($self) = @_; | |
1258 | unless ($self->{ra_uuid}) { | |
1259 | my $key = "svn-remote.$self->{repo_id}.uuid"; | |
1260 | my $uuid = eval { tmp_config('--get', $key) }; | |
1261 | if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/) { | |
1262 | $self->{ra_uuid} = $uuid; | |
1263 | } else { | |
1264 | die "ra_uuid called without URL\n" unless $self->{url}; | |
1265 | $self->{ra_uuid} = $self->ra->get_uuid; | |
1266 | tmp_config('--add', $key, $self->{ra_uuid}); | |
1267 | } | |
1268 | } | |
1269 | $self->{ra_uuid}; | |
1270 | } | |
1271 | ||
9b981fc6 EW |
1272 | sub ra { |
1273 | my ($self) = shift; | |
8a49ee97 | 1274 | my $ra = Git::SVN::Ra->new($self->{url}); |
91b03282 EW |
1275 | if ($self->use_svm_props && !$self->{svm}) { |
1276 | if ($self->no_metadata) { | |
97ae0911 EW |
1277 | die "Can't have both 'noMetadata' and ", |
1278 | "'useSvmProps' options set!\n"; | |
62e349d2 EW |
1279 | } elsif ($self->use_svnsync_props) { |
1280 | die "Can't have both 'useSvnsyncProps' and ", | |
1281 | "'useSvmProps' options set!\n"; | |
91b03282 | 1282 | } |
26a62d57 | 1283 | $ra = $self->_set_svm_vars($ra); |
8a49ee97 EW |
1284 | $self->{-want_revprops} = 1; |
1285 | } | |
1286 | $ra; | |
9b981fc6 EW |
1287 | } |
1288 | ||
15710b6f EW |
1289 | sub rel_path { |
1290 | my ($self) = @_; | |
1291 | my $repos_root = $self->ra->{repos_root}; | |
1292 | return $self->{path} if ($self->{url} eq $repos_root); | |
4e9f6cc7 EW |
1293 | die "BUG: rel_path failed! repos_root: $repos_root, Ra URL: ", |
1294 | $self->ra->{url}, " path: $self->{path}, URL: $self->{url}\n"; | |
15710b6f EW |
1295 | } |
1296 | ||
9b981fc6 EW |
1297 | sub traverse_ignore { |
1298 | my ($self, $fh, $path, $r) = @_; | |
1299 | $path =~ s#^/+##g; | |
5d3b7cd5 EW |
1300 | my $ra = $self->ra; |
1301 | my ($dirent, undef, $props) = $ra->get_dir($path, $r); | |
9b981fc6 | 1302 | my $p = $path; |
5d3b7cd5 | 1303 | $p =~ s#^\Q$ra->{svn_path}\E/##; |
9b981fc6 EW |
1304 | print $fh length $p ? "\n# $p\n" : "\n# /\n"; |
1305 | if (my $s = $props->{'svn:ignore'}) { | |
1306 | $s =~ s/[\r\n]+/\n/g; | |
1307 | chomp $s; | |
1308 | if (length $p == 0) { | |
1309 | $s =~ s#\n#\n/$p#g; | |
1310 | print $fh "/$s\n"; | |
1311 | } else { | |
1312 | $s =~ s#\n#\n/$p/#g; | |
1313 | print $fh "/$p/$s\n"; | |
1314 | } | |
1315 | } | |
1316 | foreach (sort keys %$dirent) { | |
1317 | next if $dirent->{$_}->kind != $SVN::Node::dir; | |
1318 | $self->traverse_ignore($fh, "$path/$_", $r); | |
1319 | } | |
1320 | } | |
1321 | ||
3ebe8df7 EW |
1322 | sub last_rev { ($_[0]->last_rev_commit)[0] } |
1323 | sub last_commit { ($_[0]->last_rev_commit)[1] } | |
1324 | ||
9b981fc6 EW |
1325 | # returns the newest SVN revision number and newest commit SHA1 |
1326 | sub last_rev_commit { | |
1327 | my ($self) = @_; | |
1328 | if (defined $self->{last_rev} && defined $self->{last_commit}) { | |
1329 | return ($self->{last_rev}, $self->{last_commit}); | |
1330 | } | |
d2866f9e | 1331 | my $c = ::verify_ref($self->refname.'^0'); |
91b03282 | 1332 | if ($c && !$self->use_svm_props && !$self->no_metadata) { |
d2866f9e | 1333 | my $rev = (::cmt_metadata($c))[1]; |
9b981fc6 EW |
1334 | if (defined $rev) { |
1335 | ($self->{last_rev}, $self->{last_commit}) = ($rev, $c); | |
1336 | return ($rev, $c); | |
1337 | } | |
1338 | } | |
26a62d57 EW |
1339 | my $db_path = $self->db_path; |
1340 | unless (-e $db_path) { | |
1341 | ($self->{last_rev}, $self->{last_commit}) = (undef, undef); | |
1342 | return (undef, undef); | |
1343 | } | |
9b981fc6 EW |
1344 | my $offset = -41; # from tail |
1345 | my $rl; | |
26a62d57 | 1346 | open my $fh, '<', $db_path or croak "$db_path not readable: $!\n"; |
ce4b4af7 EW |
1347 | sysseek($fh, $offset, 2); # don't care for errors |
1348 | sysread($fh, $rl, 41) == 41 or return (undef, undef); | |
9b981fc6 | 1349 | chomp $rl; |
ce4b4af7 | 1350 | while (('0' x40) eq $rl && sysseek($fh, 0, 1) != 0) { |
9b981fc6 | 1351 | $offset -= 41; |
ce4b4af7 EW |
1352 | sysseek($fh, $offset, 2); # don't care for errors |
1353 | sysread($fh, $rl, 41) == 41 or return (undef, undef); | |
9b981fc6 EW |
1354 | chomp $rl; |
1355 | } | |
91b03282 | 1356 | if ($c && $c ne $rl) { |
26a62d57 | 1357 | die "$db_path and ", $self->refname, |
9c93fee5 EW |
1358 | " inconsistent!:\n$c != $rl\n"; |
1359 | } | |
ce4b4af7 | 1360 | my $rev = sysseek($fh, 0, 1) or croak $!; |
9b981fc6 EW |
1361 | $rev = ($rev - 41) / 41; |
1362 | close $fh or croak $!; | |
1363 | ($self->{last_rev}, $self->{last_commit}) = ($rev, $c); | |
1364 | return ($rev, $c); | |
1365 | } | |
1366 | ||
3ebe8df7 EW |
1367 | sub get_fetch_range { |
1368 | my ($self, $min, $max) = @_; | |
1369 | $max ||= $self->ra->get_latest_revnum; | |
9c93fee5 | 1370 | $min ||= $self->rev_db_max; |
3ebe8df7 | 1371 | (++$min, $max); |
9b981fc6 EW |
1372 | } |
1373 | ||
8a49ee97 | 1374 | sub tmp_config { |
93f2689c | 1375 | my (@args) = @_; |
b7e5348c EW |
1376 | my $old_def_config = "$ENV{GIT_DIR}/svn/config"; |
1377 | my $config = "$ENV{GIT_DIR}/svn/.metadata"; | |
1378 | if (-e $old_def_config && ! -e $config) { | |
1379 | rename $old_def_config, $config or | |
1380 | die "Failed rename $old_def_config => $config: $!\n"; | |
1381 | } | |
8a49ee97 | 1382 | my $old_config = $ENV{GIT_CONFIG}; |
93f2689c | 1383 | $ENV{GIT_CONFIG} = $config; |
8a49ee97 | 1384 | $@ = undef; |
b4d57e5e EW |
1385 | my @ret = eval { |
1386 | unless (-f $config) { | |
1387 | mkfile($config); | |
1388 | open my $fh, '>', $config or | |
1389 | die "Can't open $config: $!\n"; | |
1390 | print $fh "; This file is used internally by ", | |
1391 | "git-svn\n" or die | |
1392 | "Couldn't write to $config: $!\n"; | |
1393 | print $fh "; You should not have to edit it\n" or | |
1394 | die "Couldn't write to $config: $!\n"; | |
1395 | close $fh or die "Couldn't close $config: $!\n"; | |
1396 | } | |
1397 | command('config', @args); | |
1398 | }; | |
8a49ee97 EW |
1399 | my $err = $@; |
1400 | if (defined $old_config) { | |
1401 | $ENV{GIT_CONFIG} = $old_config; | |
1402 | } else { | |
1403 | delete $ENV{GIT_CONFIG}; | |
1404 | } | |
1405 | die $err if $err; | |
1406 | wantarray ? @ret : $ret[0]; | |
1407 | } | |
1408 | ||
9b981fc6 EW |
1409 | sub tmp_index_do { |
1410 | my ($self, $sub) = @_; | |
1411 | my $old_index = $ENV{GIT_INDEX_FILE}; | |
1412 | $ENV{GIT_INDEX_FILE} = $self->{index}; | |
8a49ee97 | 1413 | $@ = undef; |
b4d57e5e EW |
1414 | my @ret = eval { |
1415 | my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#); | |
1416 | mkpath([$dir]) unless -d $dir; | |
1417 | &$sub; | |
1418 | }; | |
8a49ee97 EW |
1419 | my $err = $@; |
1420 | if (defined $old_index) { | |
9b981fc6 EW |
1421 | $ENV{GIT_INDEX_FILE} = $old_index; |
1422 | } else { | |
1423 | delete $ENV{GIT_INDEX_FILE}; | |
1424 | } | |
8a49ee97 | 1425 | die $err if $err; |
9b981fc6 EW |
1426 | wantarray ? @ret : $ret[0]; |
1427 | } | |
1428 | ||
1429 | sub assert_index_clean { | |
1430 | my ($self, $treeish) = @_; | |
1431 | ||
1432 | $self->tmp_index_do(sub { | |
1433 | command_noisy('read-tree', $treeish) unless -e $self->{index}; | |
1434 | my $x = command_oneline('write-tree'); | |
1435 | my ($y) = (command(qw/cat-file commit/, $treeish) =~ | |
1436 | /^tree ($::sha1)/mo); | |
e8d120bd EW |
1437 | return if $y eq $x; |
1438 | ||
1439 | warn "Index mismatch: $y != $x\nrereading $treeish\n"; | |
1440 | unlink $self->{index} or die "unlink $self->{index}: $!\n"; | |
1441 | command_noisy('read-tree', $treeish); | |
9b981fc6 EW |
1442 | $x = command_oneline('write-tree'); |
1443 | if ($y ne $x) { | |
1444 | ::fatal "trees ($treeish) $y != $x\n", | |
1445 | "Something is seriously wrong...\n"; | |
1446 | } | |
1447 | }); | |
1448 | } | |
1449 | ||
1450 | sub get_commit_parents { | |
0af9c9f9 | 1451 | my ($self, $log_entry) = @_; |
9b981fc6 | 1452 | my (%seen, @ret, @tmp); |
0af9c9f9 EW |
1453 | # legacy support for 'set-tree'; this is only used by set_tree_cb: |
1454 | if (my $ip = $self->{inject_parents}) { | |
1455 | if (my $commit = delete $ip->{$log_entry->{revision}}) { | |
1456 | push @tmp, $commit; | |
9b981fc6 EW |
1457 | } |
1458 | } | |
d2866f9e | 1459 | if (my $cur = ::verify_ref($self->refname.'^0')) { |
9b981fc6 EW |
1460 | push @tmp, $cur; |
1461 | } | |
44320b9e | 1462 | push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp); |
9b981fc6 EW |
1463 | while (my $p = shift @tmp) { |
1464 | next if $seen{$p}; | |
1465 | $seen{$p} = 1; | |
1466 | push @ret, $p; | |
1467 | # MAXPARENT is defined to 16 in commit-tree.c: | |
1468 | last if @ret >= 16; | |
1469 | } | |
1470 | if (@tmp) { | |
44320b9e | 1471 | die "r$log_entry->{revision}: No room for parents:\n\t", |
9b981fc6 EW |
1472 | join("\n\t", @tmp), "\n"; |
1473 | } | |
1474 | @ret; | |
1475 | } | |
1476 | ||
aea736cc EW |
1477 | sub rewrite_root { |
1478 | my ($self) = @_; | |
1479 | return $self->{-rewrite_root} if exists $self->{-rewrite_root}; | |
1480 | my $k = "svn-remote.$self->{repo_id}.rewriteRoot"; | |
1481 | my $rwr = eval { command_oneline(qw/config --get/, $k) }; | |
1482 | if ($rwr) { | |
1483 | $rwr =~ s#/+$##; | |
1484 | if ($rwr !~ m#^[a-z\+]+://#) { | |
1485 | die "$rwr is not a valid URL (key: $k)\n"; | |
1486 | } | |
1487 | } | |
1488 | $self->{-rewrite_root} = $rwr; | |
1489 | } | |
1490 | ||
1491 | sub metadata_url { | |
1492 | my ($self) = @_; | |
1493 | ($self->rewrite_root || $self->{url}) . | |
1494 | (length $self->{path} ? '/' . $self->{path} : ''); | |
1495 | } | |
1496 | ||
706587fc | 1497 | sub full_url { |
9b981fc6 | 1498 | my ($self) = @_; |
5d3b7cd5 | 1499 | $self->{url} . (length $self->{path} ? '/' . $self->{path} : ''); |
9b981fc6 EW |
1500 | } |
1501 | ||
1502 | sub do_git_commit { | |
0af9c9f9 | 1503 | my ($self, $log_entry) = @_; |
8a603774 EW |
1504 | my $lr = $self->last_rev; |
1505 | if (defined $lr && $lr >= $log_entry->{revision}) { | |
1506 | die "Last fetched revision of ", $self->refname, | |
1507 | " was r$lr, but we are about to fetch: ", | |
1508 | "r$log_entry->{revision}!\n"; | |
1509 | } | |
44320b9e EW |
1510 | if (my $c = $self->rev_db_get($log_entry->{revision})) { |
1511 | croak "$log_entry->{revision} = $c already exists! ", | |
9b981fc6 EW |
1512 | "Why are we refetching it?\n"; |
1513 | } | |
db03cd24 EW |
1514 | $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $log_entry->{name}; |
1515 | $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} = | |
1516 | $log_entry->{email}; | |
44320b9e | 1517 | $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date}; |
9b981fc6 | 1518 | |
44320b9e | 1519 | my $tree = $log_entry->{tree}; |
9b981fc6 EW |
1520 | if (!defined $tree) { |
1521 | $tree = $self->tmp_index_do(sub { | |
1522 | command_oneline('write-tree') }); | |
1523 | } | |
1524 | die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o; | |
1525 | ||
1526 | my @exec = ('git-commit-tree', $tree); | |
0af9c9f9 | 1527 | foreach ($self->get_commit_parents($log_entry)) { |
9b981fc6 EW |
1528 | push @exec, '-p', $_; |
1529 | } | |
1530 | defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec)) | |
1531 | or croak $!; | |
44320b9e | 1532 | print $msg_fh $log_entry->{log} or croak $!; |
91b03282 | 1533 | unless ($self->no_metadata) { |
8a49ee97 EW |
1534 | print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n" |
1535 | or croak $!; | |
9760adcc | 1536 | } |
9b981fc6 EW |
1537 | $msg_fh->flush == 0 or croak $!; |
1538 | close $msg_fh or croak $!; | |
1539 | chomp(my $commit = do { local $/; <$out_fh> }); | |
1540 | close $out_fh or croak $!; | |
1541 | waitpid $pid, 0; | |
1542 | croak $? if $?; | |
1543 | if ($commit !~ /^$::sha1$/o) { | |
1544 | die "Failed to commit, invalid sha1: $commit\n"; | |
1545 | } | |
1546 | ||
373274f9 | 1547 | $self->rev_db_set($log_entry->{revision}, $commit, 1); |
9b981fc6 | 1548 | |
44320b9e | 1549 | $self->{last_rev} = $log_entry->{revision}; |
9b981fc6 | 1550 | $self->{last_commit} = $commit; |
8a49ee97 EW |
1551 | print "r$log_entry->{revision}"; |
1552 | if (defined $log_entry->{svm_revision}) { | |
1553 | print " (\@$log_entry->{svm_revision})"; | |
26a62d57 EW |
1554 | $self->rev_db_set($log_entry->{svm_revision}, $commit, |
1555 | 0, $self->svm_uuid); | |
8a49ee97 EW |
1556 | } |
1557 | print " = $commit ($self->{ref_id})\n"; | |
ecc712dd EW |
1558 | if (defined $_repack && (--$_repack_nr == 0)) { |
1559 | $_repack_nr = $_repack; | |
1560 | # repack doesn't use any arguments with spaces in them, does it? | |
1561 | print "Running git repack $_repack_flags ...\n"; | |
1562 | command_noisy('repack', split(/\s+/, $_repack_flags)); | |
1563 | print "Done repacking\n"; | |
1564 | } | |
9b981fc6 EW |
1565 | return $commit; |
1566 | } | |
1567 | ||
fbcc1737 EW |
1568 | sub match_paths { |
1569 | my ($self, $paths, $r) = @_; | |
4e9f6cc7 | 1570 | return 1 if $self->{path} eq ''; |
d542aedb EW |
1571 | if (my $path = $paths->{"/$self->{path}"}) { |
1572 | return ($path->{action} eq 'D') ? 0 : 1; | |
1573 | } | |
4e9f6cc7 | 1574 | $self->{path_regex} ||= qr/^\/\Q$self->{path}\E\//; |
fbcc1737 EW |
1575 | if (grep /$self->{path_regex}/, keys %$paths) { |
1576 | return 1; | |
1577 | } | |
1578 | my $c = ''; | |
1579 | foreach (split m#/#, $self->{path}) { | |
1580 | $c .= "/$_"; | |
74a81227 EW |
1581 | next unless ($paths->{$c} && |
1582 | ($paths->{$c}->{action} =~ /^[AR]$/)); | |
e518192f EW |
1583 | if ($self->ra->check_path($self->{path}, $r) == |
1584 | $SVN::Node::dir) { | |
fbcc1737 EW |
1585 | return 1; |
1586 | } | |
1587 | } | |
1588 | return 0; | |
1589 | } | |
1590 | ||
15710b6f EW |
1591 | sub find_parent_branch { |
1592 | my ($self, $paths, $rev) = @_; | |
91b03282 | 1593 | return undef unless $self->follow_parent; |
e5a0b240 | 1594 | unless (defined $paths) { |
c7eba716 EW |
1595 | my $err_handler = $SVN::Error::handler; |
1596 | $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs; | |
d4eff2bd EW |
1597 | $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1, sub { |
1598 | $paths = | |
1599 | Git::SVN::Ra::dup_changed_paths($_[0]) }); | |
c7eba716 | 1600 | $SVN::Error::handler = $err_handler; |
e5a0b240 EW |
1601 | } |
1602 | return undef unless defined $paths; | |
15710b6f EW |
1603 | |
1604 | # look for a parent from another branch: | |
7f578c55 EW |
1605 | my @b_path_components = split m#/#, $self->rel_path; |
1606 | my @a_path_components; | |
1607 | my $i; | |
1608 | while (@b_path_components) { | |
1609 | $i = $paths->{'/'.join('/', @b_path_components)}; | |
74a81227 | 1610 | last if $i && defined $i->{copyfrom_path}; |
7f578c55 EW |
1611 | unshift(@a_path_components, pop(@b_path_components)); |
1612 | } | |
74a81227 EW |
1613 | return undef unless defined $i && defined $i->{copyfrom_path}; |
1614 | my $branch_from = $i->{copyfrom_path}; | |
7f578c55 EW |
1615 | if (@a_path_components) { |
1616 | print STDERR "branch_from: $branch_from => "; | |
1617 | $branch_from .= '/'.join('/', @a_path_components); | |
1618 | print STDERR $branch_from, "\n"; | |
1619 | } | |
3ebe8df7 | 1620 | my $r = $i->{copyfrom_rev}; |
15710b6f EW |
1621 | my $repos_root = $self->ra->{repos_root}; |
1622 | my $url = $self->ra->{url}; | |
1623 | my $new_url = $repos_root . $branch_from; | |
1624 | print STDERR "Found possible branch point: ", | |
1625 | "$new_url => ", $self->full_url, ", $r\n"; | |
1626 | $branch_from =~ s#^/##; | |
a8ae2623 | 1627 | my $gs = Git::SVN->find_by_url($new_url, $repos_root, $branch_from); |
15710b6f | 1628 | unless ($gs) { |
ce2a0f2f EW |
1629 | my $ref_id = $self->{ref_id}; |
1630 | $ref_id =~ s/\@\d+$//; | |
1631 | $ref_id .= "\@$r"; | |
15710b6f EW |
1632 | # just grow a tail if we're not unique enough :x |
1633 | $ref_id .= '-' while find_ref($ref_id); | |
ce2a0f2f | 1634 | print STDERR "Initializing parent: $ref_id\n"; |
d8115c51 | 1635 | $gs = Git::SVN->init($new_url, '', $ref_id, $ref_id, 1); |
15710b6f EW |
1636 | } |
1637 | my ($r0, $parent) = $gs->find_rev_before($r, 1); | |
91b03282 | 1638 | if (!defined $r0 || !defined $parent) { |
3ebe8df7 | 1639 | $gs->fetch(0, $r); |
15710b6f EW |
1640 | ($r0, $parent) = $gs->last_rev_commit; |
1641 | } | |
ef70de96 | 1642 | if (defined $r0 && defined $parent) { |
15710b6f | 1643 | print STDERR "Found branch parent: ($self->{ref_id}) $parent\n"; |
a2003abc | 1644 | $self->assert_index_clean($parent); |
15710b6f EW |
1645 | my $ed; |
1646 | if ($self->ra->can_do_switch) { | |
8b8fc068 | 1647 | print STDERR "Following parent with do_switch\n"; |
15710b6f | 1648 | # do_switch works with svn/trunk >= r22312, but that |
2b27f6c8 | 1649 | # is not included with SVN 1.4.3 (the latest version |
15710b6f EW |
1650 | # at the moment), so we can't rely on it |
1651 | $self->{last_commit} = $parent; | |
1652 | $ed = SVN::Git::Fetcher->new($self); | |
8a603774 | 1653 | $gs->ra->gs_do_switch($r0, $rev, $gs, |
15710b6f EW |
1654 | $self->full_url, $ed) |
1655 | or die "SVN connection failed somewhere...\n"; | |
1656 | } else { | |
8b8fc068 | 1657 | print STDERR "Following parent with do_update\n"; |
15710b6f | 1658 | $ed = SVN::Git::Fetcher->new($self); |
8a603774 | 1659 | $self->ra->gs_do_update($rev, $rev, $self, $ed) |
15710b6f EW |
1660 | or die "SVN connection failed somewhere...\n"; |
1661 | } | |
f7c3fc4a | 1662 | print STDERR "Successfully followed parent\n"; |
15710b6f EW |
1663 | return $self->make_log_entry($rev, [$parent], $ed); |
1664 | } | |
15710b6f EW |
1665 | return undef; |
1666 | } | |
1667 | ||
9b981fc6 | 1668 | sub do_fetch { |
706587fc | 1669 | my ($self, $paths, $rev) = @_; |
15710b6f | 1670 | my $ed; |
9b981fc6 | 1671 | my ($last_rev, @parents); |
b9dffd8c EW |
1672 | if (my $lc = $self->last_commit) { |
1673 | # we can have a branch that was deleted, then re-added | |
1674 | # under the same name but copied from another path, in | |
1675 | # which case we'll have multiple parents (we don't | |
1676 | # want to break the original ref, nor lose copypath info): | |
1677 | if (my $log_entry = $self->find_parent_branch($paths, $rev)) { | |
1678 | push @{$log_entry->{parents}}, $lc; | |
1679 | return $log_entry; | |
1680 | } | |
15710b6f | 1681 | $ed = SVN::Git::Fetcher->new($self); |
9b981fc6 | 1682 | $last_rev = $self->{last_rev}; |
b9dffd8c EW |
1683 | $ed->{c} = $lc; |
1684 | @parents = ($lc); | |
9b981fc6 EW |
1685 | } else { |
1686 | $last_rev = $rev; | |
15710b6f EW |
1687 | if (my $log_entry = $self->find_parent_branch($paths, $rev)) { |
1688 | return $log_entry; | |
1689 | } | |
1690 | $ed = SVN::Git::Fetcher->new($self); | |
9b981fc6 | 1691 | } |
8a603774 | 1692 | unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) { |
9b981fc6 EW |
1693 | die "SVN connection failed somewhere...\n"; |
1694 | } | |
1695 | $self->make_log_entry($rev, \@parents, $ed); | |
1696 | } | |
1697 | ||
97f6987a EW |
1698 | sub get_untracked { |
1699 | my ($self, $ed) = @_; | |
1700 | my @out; | |
1701 | my $h = $ed->{empty}; | |
9b981fc6 EW |
1702 | foreach (sort keys %$h) { |
1703 | my $act = $h->{$_} ? '+empty_dir' : '-empty_dir'; | |
97f6987a | 1704 | push @out, " $act: " . uri_encode($_); |
9b981fc6 EW |
1705 | warn "W: $act: $_\n"; |
1706 | } | |
1707 | foreach my $t (qw/dir_prop file_prop/) { | |
97f6987a | 1708 | $h = $ed->{$t} or next; |
9b981fc6 EW |
1709 | foreach my $path (sort keys %$h) { |
1710 | my $ppath = $path eq '' ? '.' : $path; | |
1711 | foreach my $prop (sort keys %{$h->{$path}}) { | |
1ce255dc | 1712 | next if $SKIP_PROP{$prop}; |
9b981fc6 | 1713 | my $v = $h->{$path}->{$prop}; |
97f6987a EW |
1714 | my $t_ppath_prop = "$t: " . |
1715 | uri_encode($ppath) . ' ' . | |
1716 | uri_encode($prop); | |
9b981fc6 | 1717 | if (defined $v) { |
97f6987a EW |
1718 | push @out, " +$t_ppath_prop " . |
1719 | uri_encode($v); | |
9b981fc6 | 1720 | } else { |
97f6987a | 1721 | push @out, " -$t_ppath_prop"; |
9b981fc6 EW |
1722 | } |
1723 | } | |
1724 | } | |
1725 | } | |
1726 | foreach my $t (qw/absent_file absent_directory/) { | |
97f6987a | 1727 | $h = $ed->{$t} or next; |
9b981fc6 EW |
1728 | foreach my $parent (sort keys %$h) { |
1729 | foreach my $path (sort @{$h->{$parent}}) { | |
97f6987a EW |
1730 | push @out, " $t: " . |
1731 | uri_encode("$parent/$path"); | |
9b981fc6 EW |
1732 | warn "W: $t: $parent/$path ", |
1733 | "Insufficient permissions?\n"; | |
1734 | } | |
1735 | } | |
1736 | } | |
97f6987a | 1737 | \@out; |
9b981fc6 EW |
1738 | } |
1739 | ||
1c8443b0 EW |
1740 | sub parse_svn_date { |
1741 | my $date = shift || return '+0000 1970-01-01 00:00:00'; | |
1742 | my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T | |
1743 | (\d\d)\:(\d\d)\:(\d\d).\d+Z$/x) or | |
1744 | croak "Unable to parse date: $date\n"; | |
1745 | "+0000 $Y-$m-$d $H:$M:$S"; | |
1746 | } | |
1747 | ||
1748 | sub check_author { | |
1749 | my ($author) = @_; | |
1750 | if (!defined $author || length $author == 0) { | |
1751 | $author = '(no author)'; | |
1752 | } | |
1753 | if (defined $::_authors && ! defined $::users{$author}) { | |
1754 | die "Author: $author not defined in $::_authors file\n"; | |
1755 | } | |
1756 | $author; | |
1757 | } | |
1758 | ||
9b981fc6 | 1759 | sub make_log_entry { |
97f6987a EW |
1760 | my ($self, $rev, $parents, $ed) = @_; |
1761 | my $untracked = $self->get_untracked($ed); | |
1762 | ||
9b981fc6 | 1763 | open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!; |
97f6987a EW |
1764 | print $un "r$rev\n" or croak $!; |
1765 | print $un $_, "\n" foreach @$untracked; | |
1766 | my %log_entry = ( parents => $parents || [], revision => $rev, | |
1767 | log => ''); | |
fbcc1737 | 1768 | |
8a49ee97 | 1769 | my $headrev; |
fbcc1737 | 1770 | my $logged = delete $self->{logged_rev_props}; |
8a49ee97 | 1771 | if (!$logged || $self->{-want_revprops}) { |
fbcc1737 EW |
1772 | my $rp = $self->ra->rev_proplist($rev); |
1773 | foreach (sort keys %$rp) { | |
1774 | my $v = $rp->{$_}; | |
1775 | if (/^svn:(author|date|log)$/) { | |
1776 | $log_entry{$1} = $v; | |
8a49ee97 EW |
1777 | } elsif ($_ eq 'svm:headrev') { |
1778 | $headrev = $v; | |
fbcc1737 EW |
1779 | } else { |
1780 | print $un " rev_prop: ", uri_encode($_), ' ', | |
1781 | uri_encode($v), "\n"; | |
1782 | } | |
9b981fc6 | 1783 | } |
fbcc1737 EW |
1784 | } else { |
1785 | map { $log_entry{$_} = $logged->{$_} } keys %$logged; | |
9b981fc6 EW |
1786 | } |
1787 | close $un or croak $!; | |
97f6987a | 1788 | |
9b981fc6 | 1789 | $log_entry{date} = parse_svn_date($log_entry{date}); |
9b981fc6 | 1790 | $log_entry{log} .= "\n"; |
db03cd24 EW |
1791 | my $author = $log_entry{author} = check_author($log_entry{author}); |
1792 | my ($name, $email) = defined $::users{$author} ? @{$::users{$author}} | |
1793 | : ($author, undef); | |
91b03282 | 1794 | if (defined $headrev && $self->use_svm_props) { |
aea736cc EW |
1795 | if ($self->rewrite_root) { |
1796 | die "Can't have both 'useSvmProps' and 'rewriteRoot' ", | |
1797 | "options set!\n"; | |
1798 | } | |
8a49ee97 | 1799 | my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d+)$}; |
befc9adc EW |
1800 | # we don't want "SVM: initializing mirror for junk" ... |
1801 | return undef if $r == 0; | |
1802 | my $svm = $self->svm; | |
1803 | if ($uuid ne $svm->{uuid}) { | |
8a49ee97 | 1804 | die "UUID mismatch on SVM path:\n", |
befc9adc | 1805 | "expected: $svm->{uuid}\n", |
8a49ee97 EW |
1806 | " got: $uuid\n"; |
1807 | } | |
befc9adc EW |
1808 | my $full_url = $self->full_url; |
1809 | $full_url =~ s#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or | |
1810 | die "Failed to replace '$svm->{replace}' with ", | |
1811 | "'$svm->{source}' in $full_url\n"; | |
8a49ee97 EW |
1812 | $log_entry{metadata} = "$full_url\@$r $uuid"; |
1813 | $log_entry{svm_revision} = $r; | |
db03cd24 | 1814 | $email ||= "$author\@$uuid" |
62e349d2 EW |
1815 | } elsif ($self->use_svnsync_props) { |
1816 | my $full_url = $self->svnsync->{url}; | |
1817 | $full_url .= "/$self->{path}" if length $self->{path}; | |
1818 | my $uuid = $self->svnsync->{uuid}; | |
1819 | $log_entry{metadata} = "$full_url\@$rev $uuid"; | |
1820 | $email ||= "$author\@$uuid" | |
8a49ee97 | 1821 | } else { |
aea736cc | 1822 | $log_entry{metadata} = $self->metadata_url. "\@$rev " . |
26a62d57 | 1823 | $self->ra->get_uuid; |
db03cd24 | 1824 | $email ||= "$author\@" . $self->ra->get_uuid; |
8a49ee97 | 1825 | } |
db03cd24 EW |
1826 | $log_entry{name} = $name; |
1827 | $log_entry{email} = $email; | |
9b981fc6 EW |
1828 | \%log_entry; |
1829 | } | |
1830 | ||
1831 | sub fetch { | |
3ebe8df7 | 1832 | my ($self, $min_rev, $max_rev, @parents) = @_; |
9b981fc6 | 1833 | my ($last_rev, $last_commit) = $self->last_rev_commit; |
3ebe8df7 | 1834 | my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev); |
e518192f | 1835 | $self->ra->gs_fetch_loop_common($base, $head, [$self]); |
9b981fc6 EW |
1836 | } |
1837 | ||
1838 | sub set_tree_cb { | |
1839 | my ($self, $log_entry, $tree, $rev, $date, $author) = @_; | |
490f49ea EW |
1840 | $self->{inject_parents} = { $rev => $tree }; |
1841 | $self->fetch(undef, undef); | |
9b981fc6 EW |
1842 | } |
1843 | ||
1844 | sub set_tree { | |
1845 | my ($self, $tree) = (shift, shift); | |
1ce255dc | 1846 | my $log_entry = ::get_commit_entry($tree); |
9b981fc6 EW |
1847 | unless ($self->{last_rev}) { |
1848 | fatal("Must have an existing revision to commit\n"); | |
1849 | } | |
61395354 EW |
1850 | my %ed_opts = ( r => $self->{last_rev}, |
1851 | log => $log_entry->{log}, | |
1852 | ra => $self->ra, | |
1853 | tree_a => $self->{last_commit}, | |
1854 | tree_b => $tree, | |
1855 | editor_cb => sub { | |
1856 | $self->set_tree_cb($log_entry, $tree, @_) }, | |
1857 | svn_path => $self->{path} ); | |
1858 | if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) { | |
9b981fc6 EW |
1859 | print "No changes\nr$self->{last_rev} = $tree\n"; |
1860 | } | |
9b981fc6 EW |
1861 | } |
1862 | ||
f0ecca10 EW |
1863 | sub rebuild { |
1864 | my ($self) = @_; | |
26a62d57 | 1865 | my $db_path = $self->db_path; |
d6d3346b EW |
1866 | return if (-e $db_path && ! -z $db_path); |
1867 | return unless ::verify_ref($self->refname.'^0'); | |
26a62d57 EW |
1868 | if (-f $self->{db_root}) { |
1869 | rename $self->{db_root}, $db_path or die | |
1870 | "rename $self->{db_root} => $db_path failed: $!\n"; | |
1871 | my ($dir, $base) = ($db_path =~ m#^(.*?)/?([^/]+)$#); | |
1872 | symlink $base, $self->{db_root} or die | |
1873 | "symlink $base => $self->{db_root} failed: $!\n"; | |
1874 | return; | |
1875 | } | |
1876 | print "Rebuilding $db_path ...\n"; | |
f0ecca10 EW |
1877 | my ($rev_list, $ctx) = command_output_pipe("rev-list", $self->refname); |
1878 | my $latest; | |
1879 | my $full_url = $self->full_url; | |
1880 | my $svn_uuid; | |
1881 | while (<$rev_list>) { | |
1882 | chomp; | |
1883 | my $c = $_; | |
1884 | die "Non-SHA1: $c\n" unless $c =~ /^$::sha1$/o; | |
1885 | my ($url, $rev, $uuid) = ::cmt_metadata($c); | |
1886 | ||
1887 | # ignore merges (from set-tree) | |
1888 | next if (!defined $rev || !$uuid); | |
1889 | ||
1890 | # if we merged or otherwise started elsewhere, this is | |
1891 | # how we break out of it | |
1892 | if ((defined $svn_uuid && ($uuid ne $svn_uuid)) || | |
1893 | ($full_url && $url && ($url ne $full_url))) { | |
1894 | next; | |
1895 | } | |
1896 | $latest ||= $rev; | |
1897 | $svn_uuid ||= $uuid; | |
1898 | ||
1899 | $self->rev_db_set($rev, $c); | |
1900 | print "r$rev = $c\n"; | |
1901 | } | |
1902 | command_close_pipe($rev_list, $ctx); | |
26a62d57 | 1903 | print "Done rebuilding $db_path\n"; |
f0ecca10 EW |
1904 | } |
1905 | ||
9b981fc6 EW |
1906 | # rev_db: |
1907 | # Tie::File seems to be prone to offset errors if revisions get sparse, | |
1908 | # it's not that fast, either. Tie::File is also not in Perl 5.6. So | |
1909 | # one of my favorite modules is out :< Next up would be one of the DBM | |
1910 | # modules, but I'm not sure which is most portable... So I'll just | |
1911 | # go with something that's plain-text, but still capable of | |
1912 | # being randomly accessed. So here's my ultra-simple fixed-width | |
1913 | # database. All records are 40 characters + "\n", so it's easy to seek | |
1914 | # to a revision: (41 * rev) is the byte offset. | |
1915 | # A record of 40 0s denotes an empty revision. | |
1916 | # And yes, it's still pretty fast (faster than Tie::File). | |
97ae0911 | 1917 | # These files are disposable unless noMetadata or useSvmProps is set |
9b981fc6 | 1918 | |
26a62d57 EW |
1919 | sub _rev_db_set { |
1920 | my ($fh, $rev, $commit) = @_; | |
1921 | my $offset = $rev * 41; | |
1922 | # assume that append is the common case: | |
1923 | seek $fh, 0, 2 or croak $!; | |
1924 | my $pos = tell $fh; | |
1925 | if ($pos < $offset) { | |
1926 | for (1 .. (($offset - $pos) / 41)) { | |
1927 | print $fh (('0' x 40),"\n") or croak $!; | |
1928 | } | |
1929 | } | |
1930 | seek $fh, $offset, 0 or croak $!; | |
1931 | print $fh $commit,"\n" or croak $!; | |
1932 | } | |
1933 | ||
1934 | sub mkfile { | |
1935 | my ($path) = @_; | |
1936 | unless (-e $path) { | |
1937 | my ($dir, $base) = ($path =~ m#^(.*?)/?([^/]+)$#); | |
1938 | mkpath([$dir]) unless -d $dir; | |
1939 | open my $fh, '>>', $path or die "Couldn't create $path: $!\n"; | |
1940 | close $fh or die "Couldn't close (create) $path: $!\n"; | |
1941 | } | |
1942 | } | |
1943 | ||
9b981fc6 | 1944 | sub rev_db_set { |
26a62d57 EW |
1945 | my ($self, $rev, $commit, $update_ref, $uuid) = @_; |
1946 | length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n"; | |
1947 | my $db = $self->db_path($uuid); | |
1948 | my $db_lock = "$db.lock"; | |
373274f9 EW |
1949 | my $sig; |
1950 | if ($update_ref) { | |
1951 | $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} = | |
1952 | $SIG{USR1} = $SIG{USR2} = sub { $sig = $_[0] }; | |
1953 | } | |
26a62d57 EW |
1954 | mkfile($db); |
1955 | ||
373274f9 | 1956 | $LOCKFILES{$db_lock} = 1; |
97ae0911 | 1957 | my $sync; |
97ae0911 EW |
1958 | # both of these options make our .rev_db file very, very important |
1959 | # and we can't afford to lose it because rebuild() won't work | |
1960 | if ($self->use_svm_props || $self->no_metadata) { | |
1961 | $sync = 1; | |
373274f9 | 1962 | copy($db, $db_lock) or die "rev_db_set(@_): ", |
26a62d57 | 1963 | "Failed to copy: ", |
373274f9 EW |
1964 | "$db => $db_lock ($!)\n"; |
1965 | } else { | |
1966 | rename $db, $db_lock or die "rev_db_set(@_): ", | |
26a62d57 | 1967 | "Failed to rename: ", |
373274f9 EW |
1968 | "$db => $db_lock ($!)\n"; |
1969 | } | |
26a62d57 EW |
1970 | open my $fh, '+<', $db_lock or die "Couldn't open $db_lock: $!\n"; |
1971 | _rev_db_set($fh, $rev, $commit); | |
97ae0911 EW |
1972 | if ($sync) { |
1973 | $fh->flush or die "Couldn't flush $db_lock: $!\n"; | |
1974 | $fh->sync or die "Couldn't sync $db_lock: $!\n"; | |
1975 | } | |
9b981fc6 | 1976 | close $fh or croak $!; |
373274f9 | 1977 | if ($update_ref) { |
1e889ef3 | 1978 | $_head = $self; |
373274f9 EW |
1979 | command_noisy('update-ref', '-m', "r$rev", |
1980 | $self->refname, $commit); | |
1981 | } | |
1982 | rename $db_lock, $db or die "rev_db_set(@_): ", "Failed to rename: ", | |
1983 | "$db_lock => $db ($!)\n"; | |
1984 | delete $LOCKFILES{$db_lock}; | |
1985 | if ($update_ref) { | |
1986 | $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} = | |
1987 | $SIG{USR1} = $SIG{USR2} = 'DEFAULT'; | |
1988 | kill $sig, $$ if defined $sig; | |
1989 | } | |
9b981fc6 EW |
1990 | } |
1991 | ||
9c93fee5 EW |
1992 | sub rev_db_max { |
1993 | my ($self) = @_; | |
d6d3346b | 1994 | $self->rebuild; |
26a62d57 EW |
1995 | my $db_path = $self->db_path; |
1996 | my @stat = stat $db_path or return 0; | |
1997 | ($stat[7] % 41) == 0 or die "$db_path inconsistent size: $stat[7]\n"; | |
9c93fee5 EW |
1998 | my $max = $stat[7] / 41; |
1999 | (($max > 0) ? $max - 1 : 0); | |
2000 | } | |
2001 | ||
9b981fc6 | 2002 | sub rev_db_get { |
26a62d57 | 2003 | my ($self, $rev, $uuid) = @_; |
9b981fc6 EW |
2004 | my $ret; |
2005 | my $offset = $rev * 41; | |
26a62d57 EW |
2006 | my $db_path = $self->db_path($uuid); |
2007 | return undef unless -e $db_path; | |
2008 | open my $fh, '<', $db_path or croak $!; | |
ce4b4af7 EW |
2009 | if (sysseek($fh, $offset, 0) == $offset) { |
2010 | my $read = sysread($fh, $ret, 40); | |
2011 | $ret = undef if ($read != 40 || $ret eq ('0'x40)); | |
9b981fc6 EW |
2012 | } |
2013 | close $fh or croak $!; | |
2014 | $ret; | |
2015 | } | |
2016 | ||
15710b6f EW |
2017 | sub find_rev_before { |
2018 | my ($self, $rev, $eq_ok) = @_; | |
2019 | --$rev unless $eq_ok; | |
2020 | while ($rev > 0) { | |
2021 | if (my $c = $self->rev_db_get($rev)) { | |
2022 | return ($rev, $c); | |
2023 | } | |
2024 | --$rev; | |
2025 | } | |
2026 | return (undef, undef); | |
2027 | } | |
2028 | ||
9b981fc6 | 2029 | sub _new { |
706587fc EW |
2030 | my ($class, $repo_id, $ref_id, $path) = @_; |
2031 | unless (defined $repo_id && length $repo_id) { | |
2032 | $repo_id = $Git::SVN::default_repo_id; | |
2033 | } | |
2034 | unless (defined $ref_id && length $ref_id) { | |
8b8fc068 | 2035 | $_[2] = $ref_id = $Git::SVN::default_ref_id; |
706587fc EW |
2036 | } |
2037 | $_[1] = $repo_id = sanitize_remote_name($repo_id); | |
2038 | my $dir = "$ENV{GIT_DIR}/svn/$ref_id"; | |
2039 | $_[3] = $path = '' unless (defined $path); | |
b4d57e5e | 2040 | mkpath(["$ENV{GIT_DIR}/svn"]); |
26a62d57 EW |
2041 | bless { |
2042 | ref_id => $ref_id, dir => $dir, index => "$dir/index", | |
8a49ee97 | 2043 | path => $path, config => "$ENV{GIT_DIR}/svn/config", |
26a62d57 EW |
2044 | db_root => "$dir/.rev_db", repo_id => $repo_id }, $class; |
2045 | } | |
2046 | ||
2047 | sub db_path { | |
2048 | my ($self, $uuid) = @_; | |
2049 | $uuid ||= $self->ra_uuid; | |
2050 | "$self->{db_root}.$uuid"; | |
9b981fc6 EW |
2051 | } |
2052 | ||
1c8443b0 EW |
2053 | sub uri_encode { |
2054 | my ($f) = @_; | |
2055 | $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg; | |
2056 | $f | |
2057 | } | |
9b981fc6 | 2058 | |
d976acfd EW |
2059 | package Git::SVN::Prompt; |
2060 | use strict; | |
2061 | use warnings; | |
2062 | require SVN::Core; | |
2063 | use vars qw/$_no_auth_cache $_username/; | |
2064 | ||
2065 | sub simple { | |
30d055aa EW |
2066 | my ($cred, $realm, $default_username, $may_save, $pool) = @_; |
2067 | $may_save = undef if $_no_auth_cache; | |
2068 | $default_username = $_username if defined $_username; | |
2069 | if (defined $default_username && length $default_username) { | |
2070 | if (defined $realm && length $realm) { | |
6f729591 EW |
2071 | print STDERR "Authentication realm: $realm\n"; |
2072 | STDERR->flush; | |
30d055aa EW |
2073 | } |
2074 | $cred->username($default_username); | |
2075 | } else { | |
d976acfd | 2076 | username($cred, $realm, $may_save, $pool); |
30d055aa EW |
2077 | } |
2078 | $cred->password(_read_password("Password for '" . | |
2079 | $cred->username . "': ", $realm)); | |
2080 | $cred->may_save($may_save); | |
2081 | $SVN::_Core::SVN_NO_ERROR; | |
2082 | } | |
2083 | ||
d976acfd | 2084 | sub ssl_server_trust { |
30d055aa EW |
2085 | my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_; |
2086 | $may_save = undef if $_no_auth_cache; | |
6f729591 | 2087 | print STDERR "Error validating server certificate for '$realm':\n"; |
30d055aa | 2088 | if ($failures & $SVN::Auth::SSL::UNKNOWNCA) { |
6f729591 | 2089 | print STDERR " - The certificate is not issued by a trusted ", |
30d055aa EW |
2090 | "authority. Use the\n", |
2091 | " fingerprint to validate the certificate manually!\n"; | |
2092 | } | |
2093 | if ($failures & $SVN::Auth::SSL::CNMISMATCH) { | |
6f729591 | 2094 | print STDERR " - The certificate hostname does not match.\n"; |
30d055aa EW |
2095 | } |
2096 | if ($failures & $SVN::Auth::SSL::NOTYETVALID) { | |
6f729591 | 2097 | print STDERR " - The certificate is not yet valid.\n"; |
30d055aa EW |
2098 | } |
2099 | if ($failures & $SVN::Auth::SSL::EXPIRED) { | |
6f729591 | 2100 | print STDERR " - The certificate has expired.\n"; |
30d055aa EW |
2101 | } |
2102 | if ($failures & $SVN::Auth::SSL::OTHER) { | |
6f729591 | 2103 | print STDERR " - The certificate has an unknown error.\n"; |
30d055aa | 2104 | } |
6f729591 EW |
2105 | printf STDERR |
2106 | "Certificate information:\n". | |
30d055aa EW |
2107 | " - Hostname: %s\n". |
2108 | " - Valid: from %s until %s\n". | |
2109 | " - Issuer: %s\n". | |
2110 | " - Fingerprint: %s\n", | |
2111 | map $cert_info->$_, qw(hostname valid_from valid_until | |
6f729591 | 2112 | issuer_dname fingerprint); |
30d055aa EW |
2113 | my $choice; |
2114 | prompt: | |
6f729591 | 2115 | print STDERR $may_save ? |
30d055aa EW |
2116 | "(R)eject, accept (t)emporarily or accept (p)ermanently? " : |
2117 | "(R)eject or accept (t)emporarily? "; | |
6f729591 | 2118 | STDERR->flush; |
30d055aa EW |
2119 | $choice = lc(substr(<STDIN> || 'R', 0, 1)); |
2120 | if ($choice =~ /^t$/i) { | |
2121 | $cred->may_save(undef); | |
2122 | } elsif ($choice =~ /^r$/i) { | |
2123 | return -1; | |
2124 | } elsif ($may_save && $choice =~ /^p$/i) { | |
2125 | $cred->may_save($may_save); | |
2126 | } else { | |
2127 | goto prompt; | |
2128 | } | |
2129 | $cred->accepted_failures($failures); | |
2130 | $SVN::_Core::SVN_NO_ERROR; | |
2131 | } | |
2132 | ||
d976acfd | 2133 | sub ssl_client_cert { |
30d055aa EW |
2134 | my ($cred, $realm, $may_save, $pool) = @_; |
2135 | $may_save = undef if $_no_auth_cache; | |
6f729591 EW |
2136 | print STDERR "Client certificate filename: "; |
2137 | STDERR->flush; | |
30d055aa EW |
2138 | chomp(my $filename = <STDIN>); |
2139 | $cred->cert_file($filename); | |
2140 | $cred->may_save($may_save); | |
2141 | $SVN::_Core::SVN_NO_ERROR; | |
2142 | } | |
2143 | ||
d976acfd | 2144 | sub ssl_client_cert_pw { |
30d055aa EW |
2145 | my ($cred, $realm, $may_save, $pool) = @_; |
2146 | $may_save = undef if $_no_auth_cache; | |
2147 | $cred->password(_read_password("Password: ", $realm)); | |
2148 | $cred->may_save($may_save); | |
2149 | $SVN::_Core::SVN_NO_ERROR; | |
2150 | } | |
2151 | ||
d976acfd | 2152 | sub username { |
30d055aa EW |
2153 | my ($cred, $realm, $may_save, $pool) = @_; |
2154 | $may_save = undef if $_no_auth_cache; | |
2155 | if (defined $realm && length $realm) { | |
6f729591 | 2156 | print STDERR "Authentication realm: $realm\n"; |
30d055aa EW |
2157 | } |
2158 | my $username; | |
2159 | if (defined $_username) { | |
2160 | $username = $_username; | |
2161 | } else { | |
6f729591 EW |
2162 | print STDERR "Username: "; |
2163 | STDERR->flush; | |
30d055aa EW |
2164 | chomp($username = <STDIN>); |
2165 | } | |
2166 | $cred->username($username); | |
2167 | $cred->may_save($may_save); | |
2168 | $SVN::_Core::SVN_NO_ERROR; | |
2169 | } | |
2170 | ||
2171 | sub _read_password { | |
2172 | my ($prompt, $realm) = @_; | |
6f729591 EW |
2173 | print STDERR $prompt; |
2174 | STDERR->flush; | |
30d055aa EW |
2175 | require Term::ReadKey; |
2176 | Term::ReadKey::ReadMode('noecho'); | |
2177 | my $password = ''; | |
2178 | while (defined(my $key = Term::ReadKey::ReadKey(0))) { | |
2179 | last if $key =~ /[\012\015]/; # \n\r | |
2180 | $password .= $key; | |
2181 | } | |
2182 | Term::ReadKey::ReadMode('restore'); | |
6f729591 EW |
2183 | print STDERR "\n"; |
2184 | STDERR->flush; | |
30d055aa EW |
2185 | $password; |
2186 | } | |
2187 | ||
d976acfd EW |
2188 | package main; |
2189 | ||
b9c85187 EW |
2190 | { |
2191 | my $kill_stupid_warnings = $SVN::Node::none.$SVN::Node::file. | |
2192 | $SVN::Node::dir.$SVN::Node::unknown. | |
2193 | $SVN::Node::none.$SVN::Node::file. | |
2194 | $SVN::Node::dir.$SVN::Node::unknown. | |
2195 | $SVN::Auth::SSL::CNMISMATCH. | |
2196 | $SVN::Auth::SSL::NOTYETVALID. | |
2197 | $SVN::Auth::SSL::EXPIRED. | |
2198 | $SVN::Auth::SSL::UNKNOWNCA. | |
2199 | $SVN::Auth::SSL::OTHER; | |
2200 | } | |
2201 | ||
27a1a801 EW |
2202 | package SVN::Git::Fetcher; |
2203 | use vars qw/@ISA/; | |
2204 | use strict; | |
2205 | use warnings; | |
2206 | use Carp qw/croak/; | |
2207 | use IO::File qw//; | |
90c1b15d | 2208 | use Digest::MD5; |
27a1a801 EW |
2209 | |
2210 | # file baton members: path, mode_a, mode_b, pool, fh, blob, base | |
2211 | sub new { | |
2212 | my ($class, $git_svn) = @_; | |
2213 | my $self = SVN::Delta::Editor->new; | |
2214 | bless $self, $class; | |
1c8443b0 | 2215 | $self->{c} = $git_svn->{last_commit} if exists $git_svn->{last_commit}; |
d2a9a87b EW |
2216 | $self->{empty} = {}; |
2217 | $self->{dir_prop} = {}; | |
2218 | $self->{file_prop} = {}; | |
2219 | $self->{absent_dir} = {}; | |
2220 | $self->{absent_file} = {}; | |
ef3cfaad | 2221 | $self->{gii} = $git_svn->tmp_index_do(sub { Git::IndexInfo->new }); |
27a1a801 EW |
2222 | $self; |
2223 | } | |
2224 | ||
8b8fc068 EW |
2225 | sub set_path_strip { |
2226 | my ($self, $path) = @_; | |
4e9f6cc7 | 2227 | $self->{path_strip} = qr/^\Q$path\E(\/|$)/ if length $path; |
8b8fc068 EW |
2228 | } |
2229 | ||
d2a9a87b EW |
2230 | sub open_root { |
2231 | { path => '' }; | |
2232 | } | |
2233 | ||
2234 | sub open_directory { | |
2235 | my ($self, $path, $pb, $rev) = @_; | |
2236 | { path => $path }; | |
2237 | } | |
2238 | ||
706587fc EW |
2239 | sub git_path { |
2240 | my ($self, $path) = @_; | |
2b27f6c8 EW |
2241 | if ($self->{path_strip}) { |
2242 | $path =~ s!$self->{path_strip}!! or | |
2243 | die "Failed to strip path '$path' ($self->{path_strip})\n"; | |
2244 | } | |
706587fc EW |
2245 | $path; |
2246 | } | |
2247 | ||
27a1a801 EW |
2248 | sub delete_entry { |
2249 | my ($self, $path, $rev, $pb) = @_; | |
4a87db0e | 2250 | |
706587fc | 2251 | my $gpath = $self->git_path($path); |
8a603774 EW |
2252 | return undef if ($gpath eq ''); |
2253 | ||
4a87db0e | 2254 | # remove entire directories. |
706587fc | 2255 | if (command('ls-tree', $self->{c}, '--', $gpath) =~ /^040000 tree/) { |
4a87db0e EW |
2256 | my ($ls, $ctx) = command_output_pipe(qw/ls-tree |
2257 | -r --name-only -z/, | |
706587fc | 2258 | $self->{c}, '--', $gpath); |
4a87db0e EW |
2259 | local $/ = "\0"; |
2260 | while (<$ls>) { | |
ef3cfaad EW |
2261 | chomp; |
2262 | $self->{gii}->remove($_); | |
9e3cdbd4 | 2263 | print "\tD\t$_\n" unless $::_q; |
4a87db0e | 2264 | } |
9e3cdbd4 | 2265 | print "\tD\t$gpath/\n" unless $::_q; |
4a87db0e EW |
2266 | command_close_pipe($ls, $ctx); |
2267 | $self->{empty}->{$path} = 0 | |
2268 | } else { | |
ef3cfaad | 2269 | $self->{gii}->remove($gpath); |
9e3cdbd4 | 2270 | print "\tD\t$gpath\n" unless $::_q; |
4a87db0e | 2271 | } |
27a1a801 EW |
2272 | undef; |
2273 | } | |
2274 | ||
2275 | sub open_file { | |
2276 | my ($self, $path, $pb, $rev) = @_; | |
706587fc EW |
2277 | my $gpath = $self->git_path($path); |
2278 | my ($mode, $blob) = (command('ls-tree', $self->{c}, '--', $gpath) | |
27a1a801 | 2279 | =~ /^(\d{6}) blob ([a-f\d]{40})\t/); |
006ede5e EW |
2280 | unless (defined $mode && defined $blob) { |
2281 | die "$path was not found in commit $self->{c} (r$rev)\n"; | |
2282 | } | |
27a1a801 | 2283 | { path => $path, mode_a => $mode, mode_b => $mode, blob => $blob, |
0864e3ba | 2284 | pool => SVN::Pool->new, action => 'M' }; |
27a1a801 EW |
2285 | } |
2286 | ||
2287 | sub add_file { | |
2288 | my ($self, $path, $pb, $cp_path, $cp_rev) = @_; | |
d2a9a87b EW |
2289 | my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#); |
2290 | delete $self->{empty}->{$dir}; | |
27a1a801 | 2291 | { path => $path, mode_a => 100644, mode_b => 100644, |
0864e3ba | 2292 | pool => SVN::Pool->new, action => 'A' }; |
27a1a801 EW |
2293 | } |
2294 | ||
d2a9a87b EW |
2295 | sub add_directory { |
2296 | my ($self, $path, $cp_path, $cp_rev) = @_; | |
2297 | my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#); | |
2298 | delete $self->{empty}->{$dir}; | |
2299 | $self->{empty}->{$path} = 1; | |
2300 | { path => $path }; | |
2301 | } | |
2302 | ||
2303 | sub change_dir_prop { | |
2304 | my ($self, $db, $prop, $value) = @_; | |
2305 | $self->{dir_prop}->{$db->{path}} ||= {}; | |
2306 | $self->{dir_prop}->{$db->{path}}->{$prop} = $value; | |
2307 | undef; | |
2308 | } | |
2309 | ||
2310 | sub absent_directory { | |
2311 | my ($self, $path, $pb) = @_; | |
2312 | $self->{absent_dir}->{$pb->{path}} ||= []; | |
2313 | push @{$self->{absent_dir}->{$pb->{path}}}, $path; | |
2314 | undef; | |
2315 | } | |
2316 | ||
2317 | sub absent_file { | |
2318 | my ($self, $path, $pb) = @_; | |
2319 | $self->{absent_file}->{$pb->{path}} ||= []; | |
2320 | push @{$self->{absent_file}->{$pb->{path}}}, $path; | |
2321 | undef; | |
2322 | } | |
2323 | ||
27a1a801 EW |
2324 | sub change_file_prop { |
2325 | my ($self, $fb, $prop, $value) = @_; | |
2326 | if ($prop eq 'svn:executable') { | |
2327 | if ($fb->{mode_b} != 120000) { | |
2328 | $fb->{mode_b} = defined $value ? 100755 : 100644; | |
2329 | } | |
2330 | } elsif ($prop eq 'svn:special') { | |
2331 | $fb->{mode_b} = defined $value ? 120000 : 100644; | |
d2a9a87b EW |
2332 | } else { |
2333 | $self->{file_prop}->{$fb->{path}} ||= {}; | |
2334 | $self->{file_prop}->{$fb->{path}}->{$prop} = $value; | |
27a1a801 EW |
2335 | } |
2336 | undef; | |
2337 | } | |
2338 | ||
2339 | sub apply_textdelta { | |
2340 | my ($self, $fb, $exp) = @_; | |
2341 | my $fh = IO::File->new_tmpfile; | |
2342 | $fh->autoflush(1); | |
2343 | # $fh gets auto-closed() by SVN::TxDelta::apply(), | |
2344 | # (but $base does not,) so dup() it for reading in close_file | |
2345 | open my $dup, '<&', $fh or croak $!; | |
2346 | my $base = IO::File->new_tmpfile; | |
2347 | $base->autoflush(1); | |
2348 | if ($fb->{blob}) { | |
2349 | defined (my $pid = fork) or croak $!; | |
2350 | if (!$pid) { | |
2351 | open STDOUT, '>&', $base or croak $!; | |
2352 | print STDOUT 'link ' if ($fb->{mode_a} == 120000); | |
2353 | exec qw/git-cat-file blob/, $fb->{blob} or croak $!; | |
2354 | } | |
2355 | waitpid $pid, 0; | |
2356 | croak $? if $?; | |
2357 | ||
2358 | if (defined $exp) { | |
2359 | seek $base, 0, 0 or croak $!; | |
2360 | my $md5 = Digest::MD5->new; | |
2361 | $md5->addfile($base); | |
2362 | my $got = $md5->hexdigest; | |
2363 | die "Checksum mismatch: $fb->{path} $fb->{blob}\n", | |
2364 | "expected: $exp\n", | |
2365 | " got: $got\n" if ($got ne $exp); | |
2366 | } | |
2367 | } | |
2368 | seek $base, 0, 0 or croak $!; | |
2369 | $fb->{fh} = $dup; | |
2370 | $fb->{base} = $base; | |
2371 | [ SVN::TxDelta::apply($base, $fh, undef, $fb->{path}, $fb->{pool}) ]; | |
2372 | } | |
2373 | ||
2374 | sub close_file { | |
2375 | my ($self, $fb, $exp) = @_; | |
2376 | my $hash; | |
706587fc | 2377 | my $path = $self->git_path($fb->{path}); |
27a1a801 EW |
2378 | if (my $fh = $fb->{fh}) { |
2379 | seek($fh, 0, 0) or croak $!; | |
2380 | my $md5 = Digest::MD5->new; | |
2381 | $md5->addfile($fh); | |
2382 | my $got = $md5->hexdigest; | |
2383 | die "Checksum mismatch: $path\n", | |
2384 | "expected: $exp\n got: $got\n" if ($got ne $exp); | |
2385 | seek($fh, 0, 0) or croak $!; | |
2386 | if ($fb->{mode_b} == 120000) { | |
2387 | read($fh, my $buf, 5) == 5 or croak $!; | |
2388 | $buf eq 'link ' or die "$path has mode 120000", | |
2389 | "but is not a link\n"; | |
2390 | } | |
2391 | defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n"; | |
2392 | if (!$pid) { | |
2393 | open STDIN, '<&', $fh or croak $!; | |
2394 | exec qw/git-hash-object -w --stdin/ or croak $!; | |
2395 | } | |
2396 | chomp($hash = do { local $/; <$out> }); | |
2397 | close $out or croak $!; | |
2398 | close $fh or croak $!; | |
2399 | $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n"; | |
2400 | close $fb->{base} or croak $!; | |
2401 | } else { | |
2402 | $hash = $fb->{blob} or die "no blob information\n"; | |
2403 | } | |
2404 | $fb->{pool}->clear; | |
ef3cfaad | 2405 | $self->{gii}->update($fb->{mode_b}, $hash, $path) or croak $!; |
9e3cdbd4 | 2406 | print "\t$fb->{action}\t$path\n" if $fb->{action} && ! $::_q; |
27a1a801 EW |
2407 | undef; |
2408 | } | |
2409 | ||
2410 | sub abort_edit { | |
2411 | my $self = shift; | |
ef3cfaad EW |
2412 | $self->{nr} = $self->{gii}->{nr}; |
2413 | delete $self->{gii}; | |
27a1a801 EW |
2414 | $self->SUPER::abort_edit(@_); |
2415 | } | |
2416 | ||
2417 | sub close_edit { | |
2418 | my $self = shift; | |
dad73c0b | 2419 | $self->{git_commit_ok} = 1; |
ef3cfaad EW |
2420 | $self->{nr} = $self->{gii}->{nr}; |
2421 | delete $self->{gii}; | |
27a1a801 EW |
2422 | $self->SUPER::close_edit(@_); |
2423 | } | |
1a82e793 | 2424 | |
a5e0cedc | 2425 | package SVN::Git::Editor; |
24e22aa8 | 2426 | use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/; |
a5e0cedc EW |
2427 | use strict; |
2428 | use warnings; | |
2429 | use Carp qw/croak/; | |
2430 | use IO::File; | |
90c1b15d | 2431 | use Digest::MD5; |
a5e0cedc EW |
2432 | |
2433 | sub new { | |
61395354 EW |
2434 | my ($class, $opts) = @_; |
2435 | foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) { | |
2436 | die "$_ required!\n" unless (defined $opts->{$_}); | |
2437 | } | |
2438 | ||
2439 | my $pool = SVN::Pool->new; | |
2440 | my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b}); | |
2441 | my $types = check_diff_paths($opts->{ra}, $opts->{svn_path}, | |
2442 | $opts->{r}, $mods); | |
2443 | ||
2444 | # $opts->{ra} functions should not be used after this: | |
2445 | my @ce = $opts->{ra}->get_commit_editor($opts->{log}, | |
2446 | $opts->{editor_cb}, $pool); | |
2447 | my $self = SVN::Delta::Editor->new(@ce, $pool); | |
a5e0cedc | 2448 | bless $self, $class; |
61395354 EW |
2449 | foreach (qw/svn_path r tree_a tree_b/) { |
2450 | $self->{$_} = $opts->{$_}; | |
a5e0cedc | 2451 | } |
61395354 EW |
2452 | $self->{url} = $opts->{ra}->{url}; |
2453 | $self->{mods} = $mods; | |
2454 | $self->{types} = $types; | |
2455 | $self->{pool} = $pool; | |
a5e0cedc EW |
2456 | $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) }; |
2457 | $self->{rm} = { }; | |
d3a840dc EW |
2458 | $self->{path_prefix} = length $self->{svn_path} ? |
2459 | "$self->{svn_path}/" : ''; | |
a5e0cedc EW |
2460 | return $self; |
2461 | } | |
2462 | ||
61395354 EW |
2463 | sub generate_diff { |
2464 | my ($tree_a, $tree_b) = @_; | |
2465 | my @diff_tree = qw(diff-tree -z -r); | |
24e22aa8 EW |
2466 | if ($_cp_similarity) { |
2467 | push @diff_tree, "-C$_cp_similarity"; | |
61395354 EW |
2468 | } else { |
2469 | push @diff_tree, '-C'; | |
2470 | } | |
24e22aa8 EW |
2471 | push @diff_tree, '--find-copies-harder' if $_find_copies_harder; |
2472 | push @diff_tree, "-l$_rename_limit" if defined $_rename_limit; | |
61395354 EW |
2473 | push @diff_tree, $tree_a, $tree_b; |
2474 | my ($diff_fh, $ctx) = command_output_pipe(@diff_tree); | |
2475 | local $/ = "\0"; | |
2476 | my $state = 'meta'; | |
2477 | my @mods; | |
2478 | while (<$diff_fh>) { | |
2479 | chomp $_; # this gets rid of the trailing "\0" | |
2480 | if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s | |
2481 | $::sha1\s($::sha1)\s | |
2482 | ([MTCRAD])\d*$/xo) { | |
2483 | push @mods, { mode_a => $1, mode_b => $2, | |
2484 | sha1_b => $3, chg => $4 }; | |
2485 | if ($4 =~ /^(?:C|R)$/) { | |
2486 | $state = 'file_a'; | |
2487 | } else { | |
2488 | $state = 'file_b'; | |
2489 | } | |
2490 | } elsif ($state eq 'file_a') { | |
2491 | my $x = $mods[$#mods] or croak "Empty array\n"; | |
2492 | if ($x->{chg} !~ /^(?:C|R)$/) { | |
2493 | croak "Error parsing $_, $x->{chg}\n"; | |
2494 | } | |
2495 | $x->{file_a} = $_; | |
2496 | $state = 'file_b'; | |
2497 | } elsif ($state eq 'file_b') { | |
2498 | my $x = $mods[$#mods] or croak "Empty array\n"; | |
2499 | if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) { | |
2500 | croak "Error parsing $_, $x->{chg}\n"; | |
2501 | } | |
2502 | if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) { | |
2503 | croak "Error parsing $_, $x->{chg}\n"; | |
2504 | } | |
2505 | $x->{file_b} = $_; | |
2506 | $state = 'meta'; | |
2507 | } else { | |
2508 | croak "Error parsing $_\n"; | |
2509 | } | |
2510 | } | |
2511 | command_close_pipe($diff_fh, $ctx); | |
2512 | \@mods; | |
2513 | } | |
2514 | ||
2515 | sub check_diff_paths { | |
2516 | my ($ra, $pfx, $rev, $mods) = @_; | |
2517 | my %types; | |
2518 | $pfx .= '/' if length $pfx; | |
2519 | ||
2520 | sub type_diff_paths { | |
2521 | my ($ra, $types, $path, $rev) = @_; | |
2522 | my @p = split m#/+#, $path; | |
2523 | my $c = shift @p; | |
2524 | unless (defined $types->{$c}) { | |
2525 | $types->{$c} = $ra->check_path($c, $rev); | |
2526 | } | |
2527 | while (@p) { | |
2528 | $c .= '/' . shift @p; | |
2529 | next if defined $types->{$c}; | |
2530 | $types->{$c} = $ra->check_path($c, $rev); | |
2531 | } | |
2532 | } | |
2533 | ||
2534 | foreach my $m (@$mods) { | |
2535 | foreach my $f (qw/file_a file_b/) { | |
2536 | next unless defined $m->{$f}; | |
2537 | my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#); | |
2538 | if (length $pfx.$dir && ! defined $types{$dir}) { | |
2539 | type_diff_paths($ra, \%types, $pfx.$dir, $rev); | |
2540 | } | |
2541 | } | |
2542 | } | |
2543 | \%types; | |
2544 | } | |
2545 | ||
a5e0cedc EW |
2546 | sub split_path { |
2547 | return ($_[0] =~ m#^(.*?)/?([^/]+)$#); | |
2548 | } | |
2549 | ||
2550 | sub repo_path { | |
d3a840dc EW |
2551 | my ($self, $path) = @_; |
2552 | $self->{path_prefix}.(defined $path ? $path : ''); | |
a5e0cedc EW |
2553 | } |
2554 | ||
2555 | sub url_path { | |
2556 | my ($self, $path) = @_; | |
6e8548cc | 2557 | $self->{url} . '/' . $self->repo_path($path); |
a5e0cedc EW |
2558 | } |
2559 | ||
2560 | sub rmdirs { | |
61395354 | 2561 | my ($self) = @_; |
a5e0cedc EW |
2562 | my $rm = $self->{rm}; |
2563 | delete $rm->{''}; # we never delete the url we're tracking | |
2564 | return unless %$rm; | |
2565 | ||
2566 | foreach (keys %$rm) { | |
2567 | my @d = split m#/#, $_; | |
2568 | my $c = shift @d; | |
2569 | $rm->{$c} = 1; | |
2570 | while (@d) { | |
2571 | $c .= '/' . shift @d; | |
2572 | $rm->{$c} = 1; | |
2573 | } | |
2574 | } | |
2575 | delete $rm->{$self->{svn_path}}; | |
2576 | delete $rm->{''}; # we never delete the url we're tracking | |
2577 | return unless %$rm; | |
2578 | ||
61395354 EW |
2579 | my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/, |
2580 | $self->{tree_b}); | |
a5e0cedc EW |
2581 | local $/ = "\0"; |
2582 | while (<$fh>) { | |
2583 | chomp; | |
747fa12c | 2584 | my @dn = split m#/#, $_; |
c07eee1f EW |
2585 | while (pop @dn) { |
2586 | delete $rm->{join '/', @dn}; | |
2587 | } | |
2588 | unless (%$rm) { | |
22600a25 | 2589 | close $fh; |
c07eee1f EW |
2590 | return; |
2591 | } | |
a5e0cedc | 2592 | } |
aef4e921 | 2593 | command_close_pipe($fh, $ctx); |
c07eee1f | 2594 | |
a5e0cedc EW |
2595 | my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat}); |
2596 | foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) { | |
2597 | $self->close_directory($bat->{$d}, $p); | |
2598 | my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#); | |
44320b9e | 2599 | print "\tD+\t$d/\n" unless $::_q; |
a5e0cedc EW |
2600 | $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p); |
2601 | delete $bat->{$d}; | |
2602 | } | |
2603 | } | |
2604 | ||
2605 | sub open_or_add_dir { | |
2606 | my ($self, $full_path, $baton) = @_; | |
6e8548cc EW |
2607 | my $t = $self->{types}->{$full_path}; |
2608 | if (!defined $t) { | |
2609 | die "$full_path not known in r$self->{r} or we have a bug!\n"; | |
2610 | } | |
a5e0cedc EW |
2611 | if ($t == $SVN::Node::none) { |
2612 | return $self->add_directory($full_path, $baton, | |
2613 | undef, -1, $self->{pool}); | |
2614 | } elsif ($t == $SVN::Node::dir) { | |
2615 | return $self->open_directory($full_path, $baton, | |
2616 | $self->{r}, $self->{pool}); | |
2617 | } | |
2618 | print STDERR "$full_path already exists in repository at ", | |
2619 | "r$self->{r} and it is not a directory (", | |
2620 | ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n"; | |
2621 | exit 1; | |
2622 | } | |
2623 | ||
2624 | sub ensure_path { | |
2625 | my ($self, $path) = @_; | |
2626 | my $bat = $self->{bat}; | |
6e8548cc EW |
2627 | my $repo_path = $self->repo_path($path); |
2628 | return $bat->{''} unless (length $repo_path); | |
2629 | my @p = split m#/+#, $repo_path; | |
a5e0cedc EW |
2630 | my $c = shift @p; |
2631 | $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''}); | |
2632 | while (@p) { | |
2633 | my $c0 = $c; | |
2634 | $c .= '/' . shift @p; | |
2635 | $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0}); | |
2636 | } | |
2637 | return $bat->{$c}; | |
2638 | } | |
2639 | ||
2640 | sub A { | |
44320b9e | 2641 | my ($self, $m) = @_; |
a5e0cedc EW |
2642 | my ($dir, $file) = split_path($m->{file_b}); |
2643 | my $pbat = $self->ensure_path($dir); | |
2644 | my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat, | |
2645 | undef, -1); | |
44320b9e | 2646 | print "\tA\t$m->{file_b}\n" unless $::_q; |
a5e0cedc EW |
2647 | $self->chg_file($fbat, $m); |
2648 | $self->close_file($fbat,undef,$self->{pool}); | |
2649 | } | |
2650 | ||
2651 | sub C { | |
44320b9e | 2652 | my ($self, $m) = @_; |
a5e0cedc EW |
2653 | my ($dir, $file) = split_path($m->{file_b}); |
2654 | my $pbat = $self->ensure_path($dir); | |
2655 | my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat, | |
2656 | $self->url_path($m->{file_a}), $self->{r}); | |
44320b9e | 2657 | print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q; |
a5e0cedc EW |
2658 | $self->chg_file($fbat, $m); |
2659 | $self->close_file($fbat,undef,$self->{pool}); | |
2660 | } | |
2661 | ||
2662 | sub delete_entry { | |
2663 | my ($self, $path, $pbat) = @_; | |
2664 | my $rpath = $self->repo_path($path); | |
2665 | my ($dir, $file) = split_path($rpath); | |
2666 | $self->{rm}->{$dir} = 1; | |
2667 | $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool}); | |
2668 | } | |
2669 | ||
2670 | sub R { | |
44320b9e | 2671 | my ($self, $m) = @_; |
a5e0cedc EW |
2672 | my ($dir, $file) = split_path($m->{file_b}); |
2673 | my $pbat = $self->ensure_path($dir); | |
2674 | my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat, | |
2675 | $self->url_path($m->{file_a}), $self->{r}); | |
44320b9e | 2676 | print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q; |
a5e0cedc EW |
2677 | $self->chg_file($fbat, $m); |
2678 | $self->close_file($fbat,undef,$self->{pool}); | |
2679 | ||
2680 | ($dir, $file) = split_path($m->{file_a}); | |
2681 | $pbat = $self->ensure_path($dir); | |
2682 | $self->delete_entry($m->{file_a}, $pbat); | |
2683 | } | |
2684 | ||
2685 | sub M { | |
44320b9e | 2686 | my ($self, $m) = @_; |
a5e0cedc EW |
2687 | my ($dir, $file) = split_path($m->{file_b}); |
2688 | my $pbat = $self->ensure_path($dir); | |
2689 | my $fbat = $self->open_file($self->repo_path($m->{file_b}), | |
2690 | $pbat,$self->{r},$self->{pool}); | |
44320b9e | 2691 | print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q; |
a5e0cedc EW |
2692 | $self->chg_file($fbat, $m); |
2693 | $self->close_file($fbat,undef,$self->{pool}); | |
2694 | } | |
2695 | ||
2696 | sub T { shift->M(@_) } | |
2697 | ||
2698 | sub change_file_prop { | |
2699 | my ($self, $fbat, $pname, $pval) = @_; | |
2700 | $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool}); | |
2701 | } | |
2702 | ||
2703 | sub chg_file { | |
2704 | my ($self, $fbat, $m) = @_; | |
2705 | if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) { | |
2706 | $self->change_file_prop($fbat,'svn:executable','*'); | |
2707 | } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) { | |
2708 | $self->change_file_prop($fbat,'svn:executable',undef); | |
2709 | } | |
2710 | my $fh = IO::File->new_tmpfile or croak $!; | |
2711 | if ($m->{mode_b} =~ /^120/) { | |
2712 | print $fh 'link ' or croak $!; | |
2713 | $self->change_file_prop($fbat,'svn:special','*'); | |
2714 | } elsif ($m->{mode_a} =~ /^120/ && $m->{mode_b} !~ /^120/) { | |
2715 | $self->change_file_prop($fbat,'svn:special',undef); | |
2716 | } | |
2717 | defined(my $pid = fork) or croak $!; | |
2718 | if (!$pid) { | |
2719 | open STDOUT, '>&', $fh or croak $!; | |
2720 | exec qw/git-cat-file blob/, $m->{sha1_b} or croak $!; | |
2721 | } | |
2722 | waitpid $pid, 0; | |
2723 | croak $? if $?; | |
2724 | $fh->flush == 0 or croak $!; | |
2725 | seek $fh, 0, 0 or croak $!; | |
2726 | ||
2727 | my $md5 = Digest::MD5->new; | |
2728 | $md5->addfile($fh) or croak $!; | |
2729 | seek $fh, 0, 0 or croak $!; | |
2730 | ||
2731 | my $exp = $md5->hexdigest; | |
f7197dff EW |
2732 | my $pool = SVN::Pool->new; |
2733 | my $atd = $self->apply_textdelta($fbat, undef, $pool); | |
2734 | my $got = SVN::TxDelta::send_stream($fh, @$atd, $pool); | |
a5e0cedc | 2735 | die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp); |
f7197dff | 2736 | $pool->clear; |
a5e0cedc EW |
2737 | |
2738 | close $fh or croak $!; | |
2739 | } | |
2740 | ||
2741 | sub D { | |
44320b9e | 2742 | my ($self, $m) = @_; |
a5e0cedc EW |
2743 | my ($dir, $file) = split_path($m->{file_b}); |
2744 | my $pbat = $self->ensure_path($dir); | |
44320b9e | 2745 | print "\tD\t$m->{file_b}\n" unless $::_q; |
a5e0cedc EW |
2746 | $self->delete_entry($m->{file_b}, $pbat); |
2747 | } | |
2748 | ||
2749 | sub close_edit { | |
2750 | my ($self) = @_; | |
2751 | my ($p,$bat) = ($self->{pool}, $self->{bat}); | |
2752 | foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) { | |
2753 | $self->close_directory($bat->{$_}, $p); | |
2754 | } | |
2755 | $self->SUPER::close_edit($p); | |
2756 | $p->clear; | |
2757 | } | |
2758 | ||
2759 | sub abort_edit { | |
2760 | my ($self) = @_; | |
2761 | $self->SUPER::abort_edit($self->{pool}); | |
61395354 EW |
2762 | } |
2763 | ||
2764 | sub DESTROY { | |
2765 | my $self = shift; | |
2766 | $self->SUPER::DESTROY(@_); | |
a5e0cedc EW |
2767 | $self->{pool}->clear; |
2768 | } | |
2769 | ||
44320b9e EW |
2770 | # this drives the editor |
2771 | sub apply_diff { | |
61395354 EW |
2772 | my ($self) = @_; |
2773 | my $mods = $self->{mods}; | |
44320b9e | 2774 | my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 ); |
6e8548cc | 2775 | foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) { |
44320b9e EW |
2776 | my $f = $m->{chg}; |
2777 | if (defined $o{$f}) { | |
2778 | $self->$f($m); | |
2779 | } else { | |
2780 | fatal("Invalid change type: $f\n"); | |
2781 | } | |
2782 | } | |
24e22aa8 | 2783 | $self->rmdirs if $_rmdir; |
6e8548cc | 2784 | if (@$mods == 0) { |
44320b9e EW |
2785 | $self->abort_edit; |
2786 | } else { | |
2787 | $self->close_edit; | |
2788 | } | |
6e8548cc | 2789 | return scalar @$mods; |
44320b9e EW |
2790 | } |
2791 | ||
d81bf827 | 2792 | package Git::SVN::Ra; |
6af1db44 | 2793 | use vars qw/@ISA $config_dir $_log_window_size/; |
d81bf827 EW |
2794 | use strict; |
2795 | use warnings; | |
2796 | my ($can_do_switch); | |
5d3b7cd5 | 2797 | my $RA; |
d81bf827 EW |
2798 | |
2799 | BEGIN { | |
2800 | # enforce temporary pool usage for some simple functions | |
2801 | my $e; | |
74a81227 | 2802 | foreach (qw/get_latest_revnum get_uuid get_repos_root/) { |
d81bf827 EW |
2803 | $e .= "sub $_ { |
2804 | my \$self = shift; | |
2805 | my \$pool = SVN::Pool->new; | |
2806 | my \@ret = \$self->SUPER::$_(\@_,\$pool); | |
2807 | \$pool->clear; | |
2808 | wantarray ? \@ret : \$ret[0]; }\n"; | |
2809 | } | |
74a81227 EW |
2810 | |
2811 | # get_dir needs $pool held in cache for dirents to work, | |
2812 | # check_path is cacheable and rev_proplist is close enough | |
2813 | # for our purposes. | |
2814 | foreach (qw/check_path get_dir rev_proplist/) { | |
2815 | $e .= "my \%${_}_cache; my \$${_}_rev = 0; sub $_ { | |
2816 | my \$self = shift; | |
2817 | my \$r = pop; | |
2818 | my \$k = join(\"\\0\", \@_); | |
2819 | if (my \$x = \$${_}_cache{\$r}->{\$k}) { | |
2820 | return wantarray ? \@\$x : \$x->[0]; | |
2821 | } | |
2822 | my \$pool = SVN::Pool->new; | |
2823 | my \@ret = \$self->SUPER::$_(\@_, \$r, \$pool); | |
2824 | if (\$r != \$${_}_rev) { | |
2825 | \%${_}_cache = ( pool => [] ); | |
2826 | \$${_}_rev = \$r; | |
2827 | } | |
2828 | \$${_}_cache{\$r}->{\$k} = \\\@ret; | |
2829 | push \@{\$${_}_cache{pool}}, \$pool; | |
2830 | wantarray ? \@ret : \$ret[0]; }\n"; | |
2831 | } | |
2832 | $e .= "\n1;"; | |
2833 | eval $e or die $@; | |
d81bf827 EW |
2834 | } |
2835 | ||
2836 | sub new { | |
2837 | my ($class, $url) = @_; | |
f6f09876 | 2838 | $url =~ s!/+$!!; |
5d3b7cd5 | 2839 | return $RA if ($RA && $RA->{url} eq $url); |
f6f09876 | 2840 | |
d81bf827 EW |
2841 | SVN::_Core::svn_config_ensure($config_dir, undef); |
2842 | my ($baton, $callbacks) = SVN::Core::auth_open_helper([ | |
2843 | SVN::Client::get_simple_provider(), | |
2844 | SVN::Client::get_ssl_server_trust_file_provider(), | |
2845 | SVN::Client::get_simple_prompt_provider( | |
2846 | \&Git::SVN::Prompt::simple, 2), | |
2847 | SVN::Client::get_ssl_client_cert_prompt_provider( | |
2848 | \&Git::SVN::Prompt::ssl_client_cert, 2), | |
2849 | SVN::Client::get_ssl_client_cert_pw_prompt_provider( | |
2850 | \&Git::SVN::Prompt::ssl_client_cert_pw, 2), | |
2851 | SVN::Client::get_username_provider(), | |
2852 | SVN::Client::get_ssl_server_trust_prompt_provider( | |
2853 | \&Git::SVN::Prompt::ssl_server_trust), | |
2854 | SVN::Client::get_username_prompt_provider( | |
2855 | \&Git::SVN::Prompt::username, 2), | |
2856 | ]); | |
2857 | my $config = SVN::Core::config_get_config($config_dir); | |
2858 | my $self = SVN::Ra->new(url => $url, auth => $baton, | |
2859 | config => $config, | |
2860 | pool => SVN::Pool->new, | |
2861 | auth_provider_callbacks => $callbacks); | |
2862 | $self->{svn_path} = $url; | |
2863 | $self->{repos_root} = $self->get_repos_root; | |
4e9f6cc7 | 2864 | $self->{svn_path} =~ s#^\Q$self->{repos_root}\E(/|$)##; |
5d3b7cd5 | 2865 | $RA = bless $self, $class; |
d81bf827 EW |
2866 | } |
2867 | ||
2868 | sub DESTROY { | |
5d3b7cd5 | 2869 | # do not call the real DESTROY since we store ourselves in $RA |
d81bf827 EW |
2870 | } |
2871 | ||
d81bf827 EW |
2872 | sub get_log { |
2873 | my ($self, @args) = @_; | |
2874 | my $pool = SVN::Pool->new; | |
d81bf827 EW |
2875 | splice(@args, 3, 1) if ($SVN::Core::VERSION le '1.2.0'); |
2876 | my $ret = $self->SUPER::get_log(@args, $pool); | |
2877 | $pool->clear; | |
2878 | $ret; | |
2879 | } | |
2880 | ||
2881 | sub get_commit_editor { | |
44320b9e | 2882 | my ($self, $log, $cb, $pool) = @_; |
d81bf827 | 2883 | my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : (); |
44320b9e | 2884 | $self->SUPER::get_commit_editor($log, $cb, @lock, $pool); |
d81bf827 EW |
2885 | } |
2886 | ||
d81bf827 | 2887 | sub gs_do_update { |
8a603774 EW |
2888 | my ($self, $rev_a, $rev_b, $gs, $editor) = @_; |
2889 | my $new = ($rev_a == $rev_b); | |
2890 | my $path = $gs->{path}; | |
2891 | ||
d81bf827 | 2892 | my $pool = SVN::Pool->new; |
8b8fc068 | 2893 | $editor->set_path_strip($path); |
2b27f6c8 EW |
2894 | my (@pc) = split m#/#, $path; |
2895 | my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''), | |
8a603774 | 2896 | 1, $editor, $pool); |
d81bf827 | 2897 | my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : (); |
2b27f6c8 EW |
2898 | |
2899 | # Since we can't rely on svn_ra_reparent being available, we'll | |
2900 | # just have to do some magic with set_path to make it so | |
2901 | # we only want a partial path. | |
2902 | my $sp = ''; | |
2903 | my $final = join('/', @pc); | |
2904 | while (@pc) { | |
2905 | $reporter->set_path($sp, $rev_b, 0, @lock, $pool); | |
2906 | $sp .= '/' if length $sp; | |
2907 | $sp .= shift @pc; | |
2908 | } | |
2909 | die "BUG: '$sp' != '$final'\n" if ($sp ne $final); | |
2910 | ||
2b27f6c8 EW |
2911 | $reporter->set_path($sp, $rev_a, $new, @lock, $pool); |
2912 | ||
d81bf827 EW |
2913 | $reporter->finish_report($pool); |
2914 | $pool->clear; | |
2915 | $editor->{git_commit_ok}; | |
2916 | } | |
2917 | ||
2b27f6c8 EW |
2918 | # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and |
2919 | # svn_ra_reparent didn't work before 1.4) | |
d81bf827 | 2920 | sub gs_do_switch { |
8a603774 EW |
2921 | my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_; |
2922 | my $path = $gs->{path}; | |
d81bf827 | 2923 | my $pool = SVN::Pool->new; |
2b27f6c8 EW |
2924 | |
2925 | my $full_url = $self->{url}; | |
2926 | my $old_url = $full_url; | |
2927 | $full_url .= "/$path" if length $path; | |
5d3b7cd5 EW |
2928 | my ($ra, $reparented); |
2929 | if ($old_url ne $full_url) { | |
2930 | if ($old_url !~ m#^svn(\+ssh)?://#) { | |
2931 | SVN::_Ra::svn_ra_reparent($self->{session}, $full_url, | |
2932 | $pool); | |
2933 | $self->{url} = $full_url; | |
2934 | $reparented = 1; | |
2935 | } else { | |
2936 | $ra = Git::SVN::Ra->new($full_url); | |
2937 | } | |
2938 | } | |
2939 | $ra ||= $self; | |
8a603774 | 2940 | my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool); |
d81bf827 | 2941 | my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : (); |
8b8fc068 | 2942 | $reporter->set_path('', $rev_a, 0, @lock, $pool); |
d81bf827 | 2943 | $reporter->finish_report($pool); |
2b27f6c8 | 2944 | |
5d3b7cd5 EW |
2945 | if ($reparented) { |
2946 | SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool); | |
2947 | $self->{url} = $old_url; | |
2948 | } | |
2b27f6c8 | 2949 | |
d81bf827 EW |
2950 | $pool->clear; |
2951 | $editor->{git_commit_ok}; | |
2952 | } | |
2953 | ||
0af9c9f9 | 2954 | sub gs_fetch_loop_common { |
e518192f EW |
2955 | my ($self, $base, $head, $gsv, $globs) = @_; |
2956 | return if ($base > $head); | |
6af1db44 | 2957 | my $inc = $_log_window_size; |
0af9c9f9 | 2958 | my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc); |
d2ae1434 | 2959 | my %common; |
e518192f EW |
2960 | my $common_max = scalar @$gsv; |
2961 | ||
2962 | foreach my $gs (@$gsv) { | |
d2ae1434 EW |
2963 | my @tmp = split m#/#, $gs->{path}; |
2964 | my $p = ''; | |
2965 | foreach (@tmp) { | |
2966 | $p .= length($p) ? "/$_" : $_; | |
2967 | $common{$p} ||= 0; | |
2968 | $common{$p}++; | |
2969 | } | |
2970 | } | |
e518192f EW |
2971 | $globs ||= []; |
2972 | $common_max += scalar @$globs; | |
2973 | foreach my $glob (@$globs) { | |
2974 | my @tmp = split m#/#, $glob->{path}->{left}; | |
2975 | my $p = ''; | |
2976 | foreach (@tmp) { | |
2977 | $p .= length($p) ? "/$_" : $_; | |
2978 | $common{$p} ||= 0; | |
2979 | $common{$p}++; | |
2980 | } | |
2981 | } | |
2982 | ||
d2ae1434 EW |
2983 | my $longest_path = ''; |
2984 | foreach (sort {length $b <=> length $a} keys %common) { | |
e518192f | 2985 | if ($common{$_} == $common_max) { |
d2ae1434 EW |
2986 | $longest_path = $_; |
2987 | last; | |
2988 | } | |
0af9c9f9 EW |
2989 | } |
2990 | while (1) { | |
d4eff2bd | 2991 | my %revs; |
d2ae1434 | 2992 | my $err; |
f7c3fc4a | 2993 | my $err_handler = $SVN::Error::handler; |
d2ae1434 EW |
2994 | $SVN::Error::handler = sub { |
2995 | ($err) = @_; | |
2996 | skip_unknown_revs($err); | |
2997 | }; | |
2998 | sub _cb { | |
2999 | my ($paths, $r, $author, $date, $log) = @_; | |
3000 | [ dup_changed_paths($paths), | |
3001 | { author => $author, date => $date, log => $log } ]; | |
3002 | } | |
3003 | $self->get_log([$longest_path], $min, $max, 0, 1, 1, | |
3004 | sub { $revs{$_[1]} = _cb(@_) }); | |
3005 | if ($err && $max >= $head) { | |
3006 | print STDERR "Path '$longest_path' ", | |
3007 | "was probably deleted:\n", | |
3008 | $err->expanded_message, | |
3009 | "\nWill attempt to follow ", | |
3010 | "revisions r$min .. r$max ", | |
3011 | "committed before the deletion\n"; | |
3012 | my $hi = $max; | |
3013 | while (--$hi >= $min) { | |
3014 | my $ok; | |
3015 | $self->get_log([$longest_path], $min, $hi, | |
3016 | 0, 1, 1, sub { | |
3017 | $ok ||= $_[1]; | |
3018 | $revs{$_[1]} = _cb(@_) }); | |
3019 | if ($ok) { | |
3020 | print STDERR "r$min .. r$ok OK\n"; | |
3021 | last; | |
3022 | } | |
3023 | } | |
3024 | } | |
d4eff2bd | 3025 | $SVN::Error::handler = $err_handler; |
fbcc1737 | 3026 | |
e518192f | 3027 | my %exists = map { $_->{path} => $_ } @$gsv; |
d4eff2bd | 3028 | foreach my $r (sort {$a <=> $b} keys %revs) { |
fbcc1737 | 3029 | my ($paths, $logged) = @{$revs{$r}}; |
e518192f EW |
3030 | |
3031 | foreach my $gs ($self->match_globs(\%exists, $paths, | |
3032 | $globs, $r)) { | |
fbcc1737 EW |
3033 | if ($gs->rev_db_max >= $r) { |
3034 | next; | |
3035 | } | |
3036 | next unless $gs->match_paths($paths, $r); | |
3037 | $gs->{logged_rev_props} = $logged; | |
e8d120bd EW |
3038 | if (my $last_commit = $gs->last_commit) { |
3039 | $gs->assert_index_clean($last_commit); | |
3040 | } | |
fbcc1737 EW |
3041 | my $log_entry = $gs->do_fetch($paths, $r); |
3042 | if ($log_entry) { | |
0af9c9f9 EW |
3043 | $gs->do_git_commit($log_entry); |
3044 | } | |
3045 | } | |
e518192f | 3046 | foreach my $g (@$globs) { |
93f2689c EW |
3047 | my $k = "svn-remote.$g->{remote}." . |
3048 | "$g->{t}-maxRev"; | |
3049 | Git::SVN::tmp_config($k, $r); | |
e518192f | 3050 | } |
0af9c9f9 | 3051 | } |
9c93fee5 EW |
3052 | # pre-fill the .rev_db since it'll eventually get filled in |
3053 | # with '0' x40 if something new gets committed | |
e518192f | 3054 | foreach my $gs (@$gsv) { |
9c93fee5 EW |
3055 | next if defined $gs->rev_db_get($max); |
3056 | $gs->rev_db_set($max, 0 x40); | |
3057 | } | |
c3560e53 EW |
3058 | foreach my $g (@$globs) { |
3059 | my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev"; | |
3060 | Git::SVN::tmp_config($k, $max); | |
3061 | } | |
0af9c9f9 EW |
3062 | last if $max >= $head; |
3063 | $min = $max + 1; | |
3064 | $max += $inc; | |
3065 | $max = $head if ($max > $head); | |
3066 | } | |
0af9c9f9 EW |
3067 | } |
3068 | ||
e518192f EW |
3069 | sub match_globs { |
3070 | my ($self, $exists, $paths, $globs, $r) = @_; | |
74a81227 EW |
3071 | |
3072 | sub get_dir_check { | |
3073 | my ($self, $exists, $g, $r) = @_; | |
3074 | my @x = eval { $self->get_dir($g->{path}->{left}, $r) }; | |
3075 | return unless scalar @x == 3; | |
3076 | my $dirents = $x[0]; | |
3077 | foreach my $de (keys %$dirents) { | |
3078 | next if $dirents->{$de}->kind != $SVN::Node::dir; | |
3079 | my $p = $g->{path}->full_path($de); | |
3080 | next if $exists->{$p}; | |
3081 | next if (length $g->{path}->{right} && | |
3082 | ($self->check_path($p, $r) != | |
3083 | $SVN::Node::dir)); | |
3084 | $exists->{$p} = Git::SVN->init($self->{url}, $p, undef, | |
3085 | $g->{ref}->full_path($de), 1); | |
3086 | } | |
3087 | } | |
e518192f | 3088 | foreach my $g (@$globs) { |
74a81227 EW |
3089 | if (my $path = $paths->{"/$g->{path}->{left}"}) { |
3090 | if ($path->{action} =~ /^[AR]$/) { | |
3091 | get_dir_check($self, $exists, $g, $r); | |
3092 | } | |
3093 | } | |
e518192f | 3094 | foreach (keys %$paths) { |
28710f74 EW |
3095 | if (/$g->{path}->{left_regex}/ && |
3096 | !/$g->{path}->{regex}/) { | |
74a81227 EW |
3097 | next if $paths->{$_}->{action} !~ /^[AR]$/; |
3098 | get_dir_check($self, $exists, $g, $r); | |
3099 | } | |
e518192f EW |
3100 | next unless /$g->{path}->{regex}/; |
3101 | my $p = $1; | |
3102 | my $pathname = $g->{path}->full_path($p); | |
3103 | next if $exists->{$pathname}; | |
3104 | $exists->{$pathname} = Git::SVN->init( | |
3105 | $self->{url}, $pathname, undef, | |
3106 | $g->{ref}->full_path($p), 1); | |
3107 | } | |
3108 | my $c = ''; | |
3109 | foreach (split m#/#, $g->{path}->{left}) { | |
3110 | $c .= "/$_"; | |
3111 | next unless ($paths->{$c} && | |
74a81227 EW |
3112 | ($paths->{$c}->{action} =~ /^[AR]$/)); |
3113 | get_dir_check($self, $exists, $g, $r); | |
e518192f EW |
3114 | } |
3115 | } | |
3116 | values %$exists; | |
3117 | } | |
3118 | ||
e6434f87 EW |
3119 | sub minimize_url { |
3120 | my ($self) = @_; | |
3121 | return $self->{url} if ($self->{url} eq $self->{repos_root}); | |
3122 | my $url = $self->{repos_root}; | |
3123 | my @components = split(m!/!, $self->{svn_path}); | |
3124 | my $c = ''; | |
3125 | do { | |
3126 | $url .= "/$c" if length $c; | |
3127 | eval { (ref $self)->new($url)->get_latest_revnum }; | |
3128 | } while ($@ && ($c = shift @components)); | |
3129 | $url; | |
3130 | } | |
3131 | ||
d81bf827 EW |
3132 | sub can_do_switch { |
3133 | my $self = shift; | |
3134 | unless (defined $can_do_switch) { | |
3135 | my $pool = SVN::Pool->new; | |
3136 | my $rep = eval { | |
3137 | $self->do_switch(1, '', 0, $self->{url}, | |
3138 | SVN::Delta::Editor->new, $pool); | |
3139 | }; | |
3140 | if ($@) { | |
3141 | $can_do_switch = 0; | |
3142 | } else { | |
3143 | $rep->abort_report($pool); | |
3144 | $can_do_switch = 1; | |
3145 | } | |
3146 | $pool->clear; | |
3147 | } | |
3148 | $can_do_switch; | |
3149 | } | |
3150 | ||
0af9c9f9 EW |
3151 | sub skip_unknown_revs { |
3152 | my ($err) = @_; | |
3153 | my $errno = $err->apr_err(); | |
3154 | # Maybe the branch we're tracking didn't | |
3155 | # exist when the repo started, so it's | |
3156 | # not an error if it doesn't, just continue | |
3157 | # | |
3158 | # Wonderfully consistent library, eh? | |
3159 | # 160013 - svn:// and file:// | |
3160 | # 175002 - http(s):// | |
3161 | # 175007 - http(s):// (this repo required authorization, too...) | |
3162 | # More codes may be discovered later... | |
3163 | if ($errno == 175007 || $errno == 175002 || $errno == 160013) { | |
6af1db44 EW |
3164 | warn "W: Ignoring error from SVN, path probably ", |
3165 | "does not exist: ($errno): ", | |
3166 | $err->expanded_message,"\n"; | |
0af9c9f9 EW |
3167 | return; |
3168 | } | |
3169 | die "Error from SVN, ($errno): ", $err->expanded_message,"\n"; | |
3170 | } | |
3171 | ||
3172 | # svn_log_changed_path_t objects passed to get_log are likely to be | |
3173 | # overwritten even if only the refs are copied to an external variable, | |
3174 | # so we should dup the structures in their entirety. Using an externally | |
3175 | # passed pool (instead of our temporary and quickly cleared pool in | |
3176 | # Git::SVN::Ra) does not help matters at all... | |
3177 | sub dup_changed_paths { | |
3178 | my ($paths) = @_; | |
3179 | return undef unless $paths; | |
3180 | my %ret; | |
3181 | foreach my $p (keys %$paths) { | |
3182 | my $i = $paths->{$p}; | |
3183 | my %s = map { $_ => $i->$_ } | |
3184 | qw/copyfrom_path copyfrom_rev action/; | |
3185 | $ret{$p} = \%s; | |
3186 | } | |
3187 | \%ret; | |
3188 | } | |
3189 | ||
f8c9d1d2 EW |
3190 | package Git::SVN::Log; |
3191 | use strict; | |
3192 | use warnings; | |
3193 | use POSIX qw/strftime/; | |
3194 | use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline | |
3195 | %rusers $show_commit $incremental/; | |
3196 | my $l_fmt; | |
3197 | ||
3198 | sub cmt_showable { | |
3199 | my ($c) = @_; | |
3200 | return 1 if defined $c->{r}; | |
3201 | if ($c->{l} && $c->{l}->[-1] eq "...\n" && | |
3202 | $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) { | |
44320b9e EW |
3203 | my @log = command(qw/cat-file commit/, $c->{c}); |
3204 | shift @log while ($log[0] ne "\n"); | |
3205 | shift @log; | |
3206 | @{$c->{l}} = grep !/^git-svn-id: /, @log; | |
f8c9d1d2 EW |
3207 | |
3208 | (undef, $c->{r}, undef) = ::extract_metadata( | |
44320b9e | 3209 | (grep(/^git-svn-id: /, @log))[-1]); |
f8c9d1d2 EW |
3210 | } |
3211 | return defined $c->{r}; | |
3212 | } | |
3213 | ||
3214 | sub log_use_color { | |
3215 | return 1 if $color; | |
3216 | my ($dc, $dcvar); | |
3217 | $dcvar = 'color.diff'; | |
3218 | $dc = `git-config --get $dcvar`; | |
3219 | if ($dc eq '') { | |
3220 | # nothing at all; fallback to "diff.color" | |
3221 | $dcvar = 'diff.color'; | |
3222 | $dc = `git-config --get $dcvar`; | |
3223 | } | |
3224 | chomp($dc); | |
3225 | if ($dc eq 'auto') { | |
3226 | my $pc; | |
3227 | $pc = `git-config --get color.pager`; | |
3228 | if ($pc eq '') { | |
3229 | # does not have it -- fallback to pager.color | |
3230 | $pc = `git-config --bool --get pager.color`; | |
3231 | } | |
3232 | else { | |
3233 | $pc = `git-config --bool --get color.pager`; | |
3234 | if ($?) { | |
3235 | $pc = 'false'; | |
3236 | } | |
3237 | } | |
3238 | chomp($pc); | |
3239 | if (-t *STDOUT || (defined $pager && $pc eq 'true')) { | |
3240 | return ($ENV{TERM} && $ENV{TERM} ne 'dumb'); | |
3241 | } | |
3242 | return 0; | |
3243 | } | |
3244 | return 0 if $dc eq 'never'; | |
3245 | return 1 if $dc eq 'always'; | |
3246 | chomp($dc = `git-config --bool --get $dcvar`); | |
3247 | return ($dc eq 'true'); | |
3248 | } | |
3249 | ||
3250 | sub git_svn_log_cmd { | |
3bc718ba EW |
3251 | my ($r_min, $r_max, @args) = @_; |
3252 | my $head = 'HEAD'; | |
3253 | foreach my $x (@args) { | |
3254 | last if $x eq '--'; | |
3255 | next unless ::verify_ref("$x^0"); | |
3256 | $head = $x; | |
3257 | last; | |
3258 | } | |
3259 | ||
905f8b7d | 3260 | my $url = (::working_head_info($head))[0]; |
3bc718ba | 3261 | my $gs = Git::SVN->find_by_url($url) || Git::SVN->_new; |
f8c9d1d2 EW |
3262 | my @cmd = (qw/log --abbrev-commit --pretty=raw --default/, |
3263 | $gs->refname); | |
3264 | push @cmd, '-r' unless $non_recursive; | |
3265 | push @cmd, qw/--raw --name-status/ if $verbose; | |
3266 | push @cmd, '--color' if log_use_color(); | |
3267 | return @cmd unless defined $r_max; | |
3268 | if ($r_max == $r_min) { | |
3269 | push @cmd, '--max-count=1'; | |
3270 | if (my $c = $gs->rev_db_get($r_max)) { | |
3271 | push @cmd, $c; | |
3272 | } | |
3273 | } else { | |
3274 | my ($c_min, $c_max); | |
3275 | $c_max = $gs->rev_db_get($r_max); | |
3276 | $c_min = $gs->rev_db_get($r_min); | |
3277 | if (defined $c_min && defined $c_max) { | |
3278 | if ($r_max > $r_max) { | |
3279 | push @cmd, "$c_min..$c_max"; | |
3280 | } else { | |
3281 | push @cmd, "$c_max..$c_min"; | |
3282 | } | |
3283 | } elsif ($r_max > $r_min) { | |
3284 | push @cmd, $c_max; | |
3285 | } else { | |
3286 | push @cmd, $c_min; | |
3287 | } | |
3288 | } | |
3289 | return @cmd; | |
3290 | } | |
3291 | ||
3292 | # adapted from pager.c | |
3293 | sub config_pager { | |
3294 | $pager ||= $ENV{GIT_PAGER} || $ENV{PAGER}; | |
3295 | if (!defined $pager) { | |
3296 | $pager = 'less'; | |
3297 | } elsif (length $pager == 0 || $pager eq 'cat') { | |
3298 | $pager = undef; | |
3299 | } | |
3300 | } | |
3301 | ||
3302 | sub run_pager { | |
3303 | return unless -t *STDOUT; | |
3304 | pipe my $rfd, my $wfd or return; | |
3305 | defined(my $pid = fork) or ::fatal "Can't fork: $!\n"; | |
3306 | if (!$pid) { | |
3307 | open STDOUT, '>&', $wfd or | |
3308 | ::fatal "Can't redirect to stdout: $!\n"; | |
3309 | return; | |
3310 | } | |
3311 | open STDIN, '<&', $rfd or ::fatal "Can't redirect stdin: $!\n"; | |
3312 | $ENV{LESS} ||= 'FRSX'; | |
3313 | exec $pager or ::fatal "Can't run pager: $! ($pager)\n"; | |
3314 | } | |
3315 | ||
21819a37 EW |
3316 | sub tz_to_s_offset { |
3317 | my ($tz) = @_; | |
3318 | $tz =~ s/(\d\d)$//; | |
3319 | return ($1 * 60) + ($tz * 3600); | |
3320 | } | |
3321 | ||
f8c9d1d2 EW |
3322 | sub get_author_info { |
3323 | my ($dest, $author, $t, $tz) = @_; | |
3324 | $author =~ s/(?:^\s*|\s*$)//g; | |
3325 | $dest->{a_raw} = $author; | |
3326 | my $au; | |
1c8443b0 | 3327 | if ($::_authors) { |
f8c9d1d2 EW |
3328 | $au = $rusers{$author} || undef; |
3329 | } | |
3330 | if (!$au) { | |
3331 | ($au) = ($author =~ /<([^>]+)\@[^>]+>$/); | |
3332 | } | |
3333 | $dest->{t} = $t; | |
3334 | $dest->{tz} = $tz; | |
3335 | $dest->{a} = $au; | |
3336 | # Date::Parse isn't in the standard Perl distro :( | |
3337 | if ($tz =~ s/^\+//) { | |
21819a37 | 3338 | $t += tz_to_s_offset($tz); |
f8c9d1d2 | 3339 | } elsif ($tz =~ s/^\-//) { |
21819a37 | 3340 | $t -= tz_to_s_offset($tz); |
f8c9d1d2 EW |
3341 | } |
3342 | $dest->{t_utc} = $t; | |
3343 | } | |
3344 | ||
3345 | sub process_commit { | |
3346 | my ($c, $r_min, $r_max, $defer) = @_; | |
3347 | if (defined $r_min && defined $r_max) { | |
3348 | if ($r_min == $c->{r} && $r_min == $r_max) { | |
3349 | show_commit($c); | |
3350 | return 0; | |
3351 | } | |
3352 | return 1 if $r_min == $r_max; | |
3353 | if ($r_min < $r_max) { | |
3354 | # we need to reverse the print order | |
3355 | return 0 if (defined $limit && --$limit < 0); | |
3356 | push @$defer, $c; | |
3357 | return 1; | |
3358 | } | |
3359 | if ($r_min != $r_max) { | |
3360 | return 1 if ($r_min < $c->{r}); | |
3361 | return 1 if ($r_max > $c->{r}); | |
3362 | } | |
3363 | } | |
3364 | return 0 if (defined $limit && --$limit < 0); | |
3365 | show_commit($c); | |
3366 | return 1; | |
3367 | } | |
3368 | ||
3369 | sub show_commit { | |
3370 | my $c = shift; | |
3371 | if ($oneline) { | |
3372 | my $x = "\n"; | |
3373 | if (my $l = $c->{l}) { | |
3374 | while ($l->[0] =~ /^\s*$/) { shift @$l } | |
3375 | $x = $l->[0]; | |
3376 | } | |
3377 | $l_fmt ||= 'A' . length($c->{r}); | |
3378 | print 'r',pack($l_fmt, $c->{r}),' | '; | |
3379 | print "$c->{c} | " if $show_commit; | |
3380 | print $x; | |
3381 | } else { | |
3382 | show_commit_normal($c); | |
3383 | } | |
3384 | } | |
3385 | ||
3386 | sub show_commit_changed_paths { | |
3387 | my ($c) = @_; | |
3388 | return unless $c->{changed}; | |
3389 | print "Changed paths:\n", @{$c->{changed}}; | |
3390 | } | |
3391 | ||
3392 | sub show_commit_normal { | |
3393 | my ($c) = @_; | |
3394 | print '-' x72, "\nr$c->{r} | "; | |
3395 | print "$c->{c} | " if $show_commit; | |
3396 | print "$c->{a} | ", strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)", | |
3397 | localtime($c->{t_utc})), ' | '; | |
3398 | my $nr_line = 0; | |
3399 | ||
3400 | if (my $l = $c->{l}) { | |
3401 | while ($l->[$#$l] eq "\n" && $#$l > 0 | |
3402 | && $l->[($#$l - 1)] eq "\n") { | |
3403 | pop @$l; | |
3404 | } | |
3405 | $nr_line = scalar @$l; | |
3406 | if (!$nr_line) { | |
3407 | print "1 line\n\n\n"; | |
3408 | } else { | |
3409 | if ($nr_line == 1) { | |
3410 | $nr_line = '1 line'; | |
3411 | } else { | |
3412 | $nr_line .= ' lines'; | |
3413 | } | |
3414 | print $nr_line, "\n"; | |
3415 | show_commit_changed_paths($c); | |
3416 | print "\n"; | |
3417 | print $_ foreach @$l; | |
3418 | } | |
3419 | } else { | |
3420 | print "1 line\n"; | |
3421 | show_commit_changed_paths($c); | |
3422 | print "\n"; | |
3423 | ||
3424 | } | |
488a63ec | 3425 | foreach my $x (qw/raw stat diff/) { |
f8c9d1d2 EW |
3426 | if ($c->{$x}) { |
3427 | print "\n"; | |
3428 | print $_ foreach @{$c->{$x}} | |
3429 | } | |
3430 | } | |
3431 | } | |
3432 | ||
3433 | sub cmd_show_log { | |
3434 | my (@args) = @_; | |
3435 | my ($r_min, $r_max); | |
3436 | my $r_last = -1; # prevent dupes | |
3437 | if (defined $TZ) { | |
3438 | $ENV{TZ} = $TZ; | |
3439 | } else { | |
3440 | delete $ENV{TZ}; | |
3441 | } | |
3442 | if (defined $::_revision) { | |
3443 | if ($::_revision =~ /^(\d+):(\d+)$/) { | |
3444 | ($r_min, $r_max) = ($1, $2); | |
3445 | } elsif ($::_revision =~ /^\d+$/) { | |
3446 | $r_min = $r_max = $::_revision; | |
3447 | } else { | |
3448 | ::fatal "-r$::_revision is not supported, use ", | |
3449 | "standard \'git log\' arguments instead\n"; | |
3450 | } | |
3451 | } | |
3452 | ||
3453 | config_pager(); | |
3bc718ba | 3454 | @args = (git_svn_log_cmd($r_min, $r_max, @args), @args); |
f8c9d1d2 EW |
3455 | my $log = command_output_pipe(@args); |
3456 | run_pager(); | |
488a63ec | 3457 | my (@k, $c, $d, $stat); |
f8c9d1d2 EW |
3458 | my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/; |
3459 | while (<$log>) { | |
3460 | if (/^${esc_color}commit ($::sha1_short)/o) { | |
3461 | my $cmt = $1; | |
3462 | if ($c && cmt_showable($c) && $c->{r} != $r_last) { | |
3463 | $r_last = $c->{r}; | |
3464 | process_commit($c, $r_min, $r_max, \@k) or | |
3465 | goto out; | |
3466 | } | |
3467 | $d = undef; | |
3468 | $c = { c => $cmt }; | |
3469 | } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) { | |
3470 | get_author_info($c, $1, $2, $3); | |
3471 | } elsif (/^${esc_color}(?:tree|parent|committer) /o) { | |
3472 | # ignore | |
3473 | } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) { | |
3474 | push @{$c->{raw}}, $_; | |
3475 | } elsif (/^${esc_color}[ACRMDT]\t/) { | |
3476 | # we could add $SVN->{svn_path} here, but that requires | |
3477 | # remote access at the moment (repo_path_split)... | |
3478 | s#^(${esc_color})([ACRMDT])\t#$1 $2 #o; | |
3479 | push @{$c->{changed}}, $_; | |
3480 | } elsif (/^${esc_color}diff /o) { | |
3481 | $d = 1; | |
3482 | push @{$c->{diff}}, $_; | |
3483 | } elsif ($d) { | |
3484 | push @{$c->{diff}}, $_; | |
488a63ec EW |
3485 | } elsif (/^\ .+\ \|\s*\d+\ $esc_color[\+\-]* |
3486 | $esc_color*[\+\-]*$esc_color$/x) { | |
3487 | $stat = 1; | |
3488 | push @{$c->{stat}}, $_; | |
3489 | } elsif ($stat && /^ \d+ files changed, \d+ insertions/) { | |
3490 | push @{$c->{stat}}, $_; | |
3491 | $stat = undef; | |
f8c9d1d2 EW |
3492 | } elsif (/^${esc_color} (git-svn-id:.+)$/o) { |
3493 | ($c->{url}, $c->{r}, undef) = ::extract_metadata($1); | |
3494 | } elsif (s/^${esc_color} //o) { | |
3495 | push @{$c->{l}}, $_; | |
3496 | } | |
3497 | } | |
3498 | if ($c && defined $c->{r} && $c->{r} != $r_last) { | |
3499 | $r_last = $c->{r}; | |
3500 | process_commit($c, $r_min, $r_max, \@k); | |
3501 | } | |
3502 | if (@k) { | |
3503 | my $swap = $r_max; | |
3504 | $r_max = $r_min; | |
3505 | $r_min = $swap; | |
3506 | process_commit($_, $r_min, $r_max) foreach reverse @k; | |
3507 | } | |
3508 | out: | |
c843c464 | 3509 | close $log; |
f8c9d1d2 EW |
3510 | print '-' x72,"\n" unless $incremental || $oneline; |
3511 | } | |
3512 | ||
706587fc EW |
3513 | package Git::SVN::Migration; |
3514 | # these version numbers do NOT correspond to actual version numbers | |
3515 | # of git nor git-svn. They are just relative. | |
3516 | # | |
3517 | # v0 layout: .git/$id/info/url, refs/heads/$id-HEAD | |
3518 | # | |
3519 | # v1 layout: .git/$id/info/url, refs/remotes/$id | |
3520 | # | |
3521 | # v2 layout: .git/svn/$id/info/url, refs/remotes/$id | |
3522 | # | |
3523 | # v3 layout: .git/svn/$id, refs/remotes/$id | |
3524 | # - info/url may remain for backwards compatibility | |
3525 | # - this is what we migrate up to this layout automatically, | |
3526 | # - this will be used by git svn init on single branches | |
26a62d57 EW |
3527 | # v3.1 layout (auto migrated): |
3528 | # - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink | |
3529 | # for backwards compatibility | |
706587fc EW |
3530 | # |
3531 | # v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id | |
3532 | # - this is only created for newly multi-init-ed | |
3533 | # repositories. Similar in spirit to the | |
3534 | # --use-separate-remotes option in git-clone (now default) | |
3535 | # - we do not automatically migrate to this (following | |
3536 | # the example set by core git) | |
3537 | use strict; | |
3538 | use warnings; | |
3539 | use Carp qw/croak/; | |
3540 | use File::Path qw/mkpath/; | |
47e39c55 EW |
3541 | use File::Basename qw/dirname basename/; |
3542 | use vars qw/$_minimize/; | |
706587fc EW |
3543 | |
3544 | sub migrate_from_v0 { | |
3545 | my $git_dir = $ENV{GIT_DIR}; | |
3546 | return undef unless -d $git_dir; | |
3547 | my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/); | |
3548 | my $migrated = 0; | |
3549 | while (<$fh>) { | |
3550 | chomp; | |
3551 | my ($id, $orig_ref) = ($_, $_); | |
3552 | next unless $id =~ s#^refs/heads/(.+)-HEAD$#$1#; | |
3553 | next unless -f "$git_dir/$id/info/url"; | |
3554 | my $new_ref = "refs/remotes/$id"; | |
3555 | if (::verify_ref("$new_ref^0")) { | |
3556 | print STDERR "W: $orig_ref is probably an old ", | |
3557 | "branch used by an ancient version of ", | |
3558 | "git-svn.\n", | |
3559 | "However, $new_ref also exists.\n", | |
3560 | "We will not be able ", | |
3561 | "to use this branch until this ", | |
3562 | "ambiguity is resolved.\n"; | |
3563 | next; | |
3564 | } | |
3565 | print STDERR "Migrating from v0 layout...\n" if !$migrated; | |
3566 | print STDERR "Renaming ref: $orig_ref => $new_ref\n"; | |
3567 | command_noisy('update-ref', $new_ref, $orig_ref); | |
3568 | command_noisy('update-ref', '-d', $orig_ref, $orig_ref); | |
3569 | $migrated++; | |
3570 | } | |
3571 | command_close_pipe($fh, $ctx); | |
3572 | print STDERR "Done migrating from v0 layout...\n" if $migrated; | |
3573 | $migrated; | |
3574 | } | |
3575 | ||
3576 | sub migrate_from_v1 { | |
3577 | my $git_dir = $ENV{GIT_DIR}; | |
3578 | my $migrated = 0; | |
3579 | return $migrated unless -d $git_dir; | |
3580 | my $svn_dir = "$git_dir/svn"; | |
3581 | ||
3582 | # just in case somebody used 'svn' as their $id at some point... | |
3583 | return $migrated if -d $svn_dir && ! -f "$svn_dir/info/url"; | |
3584 | ||
3585 | print STDERR "Migrating from a git-svn v1 layout...\n"; | |
3586 | mkpath([$svn_dir]); | |
3587 | print STDERR "Data from a previous version of git-svn exists, but\n\t", | |
3588 | "$svn_dir\n\t(required for this version ", | |
3589 | "($::VERSION) of git-svn) does not. exist\n"; | |
3590 | my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/); | |
3591 | while (<$fh>) { | |
3592 | my $x = $_; | |
3593 | next unless $x =~ s#^refs/remotes/##; | |
3594 | chomp $x; | |
3595 | next unless -f "$git_dir/$x/info/url"; | |
3596 | my $u = eval { ::file_to_s("$git_dir/$x/info/url") }; | |
3597 | next unless $u; | |
3598 | my $dn = dirname("$git_dir/svn/$x"); | |
3599 | mkpath([$dn]) unless -d $dn; | |
3600 | if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID: | |
3601 | mkpath(["$git_dir/svn/svn"]); | |
3602 | print STDERR " - $git_dir/$x/info => ", | |
3603 | "$git_dir/svn/$x/info\n"; | |
3604 | rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or | |
3605 | croak "$!: $x"; | |
3606 | # don't worry too much about these, they probably | |
3607 | # don't exist with repos this old (save for index, | |
3608 | # and we can easily regenerate that) | |
3609 | foreach my $f (qw/unhandled.log index .rev_db/) { | |
3610 | rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f"; | |
3611 | } | |
3612 | } else { | |
3613 | print STDERR " - $git_dir/$x => $git_dir/svn/$x\n"; | |
3614 | rename "$git_dir/$x", "$git_dir/svn/$x" or | |
3615 | croak "$!: $x"; | |
3616 | } | |
3617 | $migrated++; | |
3618 | } | |
3619 | command_close_pipe($fh, $ctx); | |
3620 | print STDERR "Done migrating from a git-svn v1 layout\n"; | |
3621 | $migrated; | |
3622 | } | |
3623 | ||
3624 | sub read_old_urls { | |
3625 | my ($l_map, $pfx, $path) = @_; | |
3626 | my @dir; | |
3627 | foreach (<$path/*>) { | |
3628 | if (-r "$_/info/url") { | |
3629 | $pfx .= '/' if $pfx && $pfx !~ m!/$!; | |
3630 | my $ref_id = $pfx . basename $_; | |
3631 | my $url = ::file_to_s("$_/info/url"); | |
3632 | $l_map->{$ref_id} = $url; | |
3633 | } elsif (-d $_) { | |
3634 | push @dir, $_; | |
3635 | } | |
3636 | } | |
3637 | foreach (@dir) { | |
3638 | my $x = $_; | |
3639 | $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o; | |
3640 | read_old_urls($l_map, $x, $_); | |
3641 | } | |
3642 | } | |
3643 | ||
3644 | sub migrate_from_v2 { | |
3645 | my @cfg = command(qw/config -l/); | |
3646 | return if grep /^svn-remote\..+\.url=/, @cfg; | |
3647 | my %l_map; | |
3648 | read_old_urls(\%l_map, '', "$ENV{GIT_DIR}/svn"); | |
3649 | my $migrated = 0; | |
3650 | ||
3651 | foreach my $ref_id (sort keys %l_map) { | |
471bc000 EW |
3652 | eval { Git::SVN->init($l_map{$ref_id}, '', undef, $ref_id) }; |
3653 | if ($@) { | |
3654 | Git::SVN->init($l_map{$ref_id}, '', $ref_id, $ref_id); | |
3655 | } | |
706587fc EW |
3656 | $migrated++; |
3657 | } | |
3658 | $migrated; | |
3659 | } | |
3660 | ||
47e39c55 EW |
3661 | sub minimize_connections { |
3662 | my $r = Git::SVN::read_all_remotes(); | |
3663 | my $new_urls = {}; | |
3664 | my $root_repos = {}; | |
3665 | foreach my $repo_id (keys %$r) { | |
3666 | my $url = $r->{$repo_id}->{url} or next; | |
3667 | my $fetch = $r->{$repo_id}->{fetch} or next; | |
3668 | my $ra = Git::SVN::Ra->new($url); | |
3669 | ||
3670 | # skip existing cases where we already connect to the root | |
3671 | if (($ra->{url} eq $ra->{repos_root}) || | |
3672 | (Git::SVN::sanitize_remote_name($ra->{repos_root}) eq | |
3673 | $repo_id)) { | |
3674 | $root_repos->{$ra->{url}} = $repo_id; | |
3675 | next; | |
3676 | } | |
3677 | ||
3678 | my $root_ra = Git::SVN::Ra->new($ra->{repos_root}); | |
3679 | my $root_path = $ra->{url}; | |
4e9f6cc7 | 3680 | $root_path =~ s#^\Q$ra->{repos_root}\E(/|$)##; |
47e39c55 EW |
3681 | foreach my $path (keys %$fetch) { |
3682 | my $ref_id = $fetch->{$path}; | |
3683 | my $gs = Git::SVN->new($ref_id, $repo_id, $path); | |
3684 | ||
3685 | # make sure we can read when connecting to | |
3686 | # a higher level of a repository | |
3687 | my ($last_rev, undef) = $gs->last_rev_commit; | |
3688 | if (!defined $last_rev) { | |
3689 | $last_rev = eval { | |
3690 | $root_ra->get_latest_revnum; | |
3691 | }; | |
3692 | next if $@; | |
3693 | } | |
3694 | my $new = $root_path; | |
3695 | $new .= length $path ? "/$path" : ''; | |
3696 | eval { | |
3697 | $root_ra->get_log([$new], $last_rev, $last_rev, | |
3698 | 0, 0, 1, sub { }); | |
3699 | }; | |
3700 | next if $@; | |
3701 | $new_urls->{$ra->{repos_root}}->{$new} = | |
3702 | { ref_id => $ref_id, | |
3703 | old_repo_id => $repo_id, | |
3704 | old_path => $path }; | |
3705 | } | |
3706 | } | |
3707 | ||
3708 | my @emptied; | |
3709 | foreach my $url (keys %$new_urls) { | |
3710 | # see if we can re-use an existing [svn-remote "repo_id"] | |
3711 | # instead of creating a(n ugly) new section: | |
3712 | my $repo_id = $root_repos->{$url} || | |
3713 | Git::SVN::sanitize_remote_name($url); | |
3714 | ||
3715 | my $fetch = $new_urls->{$url}; | |
3716 | foreach my $path (keys %$fetch) { | |
3717 | my $x = $fetch->{$path}; | |
3718 | Git::SVN->init($url, $path, $repo_id, $x->{ref_id}); | |
3719 | my $pfx = "svn-remote.$x->{old_repo_id}"; | |
3720 | ||
3721 | my $old_fetch = quotemeta("$x->{old_path}:". | |
3722 | "refs/remotes/$x->{ref_id}"); | |
8b8fc068 | 3723 | command_noisy(qw/config --unset/, |
47e39c55 EW |
3724 | "$pfx.fetch", '^'. $old_fetch . '$'); |
3725 | delete $r->{$x->{old_repo_id}}-> | |
3726 | {fetch}->{$x->{old_path}}; | |
3727 | if (!keys %{$r->{$x->{old_repo_id}}->{fetch}}) { | |
8b8fc068 | 3728 | command_noisy(qw/config --unset/, |
47e39c55 EW |
3729 | "$pfx.url"); |
3730 | push @emptied, $x->{old_repo_id} | |
3731 | } | |
3732 | } | |
3733 | } | |
3734 | if (@emptied) { | |
3735 | my $file = $ENV{GIT_CONFIG} || $ENV{GIT_CONFIG_LOCAL} || | |
3736 | "$ENV{GIT_DIR}/config"; | |
3737 | print STDERR <<EOF; | |
3738 | The following [svn-remote] sections in your config file ($file) are empty | |
3739 | and can be safely removed: | |
3740 | EOF | |
3741 | print STDERR "[svn-remote \"$_\"]\n" foreach @emptied; | |
3742 | } | |
3743 | } | |
3744 | ||
706587fc EW |
3745 | sub migration_check { |
3746 | migrate_from_v0(); | |
3747 | migrate_from_v1(); | |
3748 | migrate_from_v2(); | |
47e39c55 | 3749 | minimize_connections() if $_minimize; |
706587fc EW |
3750 | } |
3751 | ||
ef3cfaad EW |
3752 | package Git::IndexInfo; |
3753 | use strict; | |
3754 | use warnings; | |
3755 | use Git qw/command_input_pipe command_close_pipe/; | |
3756 | ||
3757 | sub new { | |
3758 | my ($class) = @_; | |
3759 | my ($gui, $ctx) = command_input_pipe(qw/update-index -z --index-info/); | |
3760 | bless { gui => $gui, ctx => $ctx, nr => 0}, $class; | |
3761 | } | |
3762 | ||
3763 | sub remove { | |
3764 | my ($self, $path) = @_; | |
3765 | if (print { $self->{gui} } '0 ', 0 x 40, "\t", $path, "\0") { | |
3766 | return ++$self->{nr}; | |
3767 | } | |
3768 | undef; | |
3769 | } | |
3770 | ||
3771 | sub update { | |
3772 | my ($self, $mode, $hash, $path) = @_; | |
3773 | if (print { $self->{gui} } $mode, ' ', $hash, "\t", $path, "\0") { | |
3774 | return ++$self->{nr}; | |
3775 | } | |
3776 | undef; | |
3777 | } | |
3778 | ||
3779 | sub DESTROY { | |
3780 | my ($self) = @_; | |
3781 | command_close_pipe($self->{gui}, $self->{ctx}); | |
3782 | } | |
3783 | ||
4bb9ed04 EW |
3784 | package Git::SVN::GlobSpec; |
3785 | use strict; | |
3786 | use warnings; | |
3787 | ||
3788 | sub new { | |
3789 | my ($class, $glob) = @_; | |
4bb9ed04 EW |
3790 | my $re = $glob; |
3791 | $re =~ s!/+$!!g; # no need for trailing slashes | |
4e9f6cc7 | 3792 | my $nr = ($re =~ s!^(.*)\*(.*)$!\(\[^/\]+\)!g); |
4bb9ed04 EW |
3793 | my ($left, $right) = ($1, $2); |
3794 | if ($nr > 1) { | |
e518192f EW |
3795 | die "Only one '*' wildcard expansion ", |
3796 | "is supported (got $nr): '$glob'\n"; | |
4bb9ed04 | 3797 | } elsif ($nr == 0) { |
e518192f | 3798 | die "One '*' is needed for glob: '$glob'\n"; |
4bb9ed04 EW |
3799 | } |
3800 | $re = quotemeta($left) . $re . quotemeta($right); | |
4e9f6cc7 EW |
3801 | if (length $left && !($left =~ s!/+$!!g)) { |
3802 | die "Missing trailing '/' on left side of: '$glob' ($left)\n"; | |
3803 | } | |
3804 | if (length $right && !($right =~ s!^/+!!g)) { | |
3805 | die "Missing leading '/' on right side of: '$glob' ($right)\n"; | |
3806 | } | |
74a81227 EW |
3807 | my $left_re = qr/^\/\Q$left\E(\/|$)/; |
3808 | bless { left => $left, right => $right, left_regex => $left_re, | |
4bb9ed04 EW |
3809 | regex => qr/$re/, glob => $glob }, $class; |
3810 | } | |
3811 | ||
3812 | sub full_path { | |
3813 | my ($self, $path) = @_; | |
3814 | return (length $self->{left} ? "$self->{left}/" : '') . | |
3815 | $path . (length $self->{right} ? "/$self->{right}" : ''); | |
3816 | } | |
3817 | ||
3397f9df EW |
3818 | __END__ |
3819 | ||
3820 | Data structures: | |
3821 | ||
4bb9ed04 EW |
3822 | |
3823 | $remotes = { # returned by read_all_remotes() | |
3824 | 'svn' => { | |
3825 | # svn-remote.svn.url=https://svn.musicpd.org | |
3826 | url => 'https://svn.musicpd.org', | |
3827 | # svn-remote.svn.fetch=mpd/trunk:trunk | |
3828 | fetch => { | |
3829 | 'mpd/trunk' => 'trunk', | |
3830 | }, | |
3831 | # svn-remote.svn.tags=mpd/tags/*:tags/* | |
3832 | tags => { | |
3833 | path => { | |
3834 | left => 'mpd/tags', | |
3835 | right => '', | |
3836 | regex => qr!mpd/tags/([^/]+)$!, | |
3837 | glob => 'tags/*', | |
3838 | }, | |
3839 | ref => { | |
3840 | left => 'tags', | |
3841 | right => '', | |
3842 | regex => qr!tags/([^/]+)$!, | |
3843 | glob => 'tags/*', | |
3844 | }, | |
3845 | } | |
3846 | } | |
3847 | }; | |
3848 | ||
44320b9e | 3849 | $log_entry hashref as returned by libsvn_log_entry() |
3397f9df | 3850 | { |
44320b9e | 3851 | log => 'whitespace-formatted log entry |
3397f9df EW |
3852 | ', # trailing newline is preserved |
3853 | revision => '8', # integer | |
3854 | date => '2004-02-24T17:01:44.108345Z', # commit date | |
3855 | author => 'committer name' | |
3856 | }; | |
3857 | ||
6e8548cc EW |
3858 | |
3859 | # this is generated by generate_diff(); | |
3397f9df EW |
3860 | @mods = array of diff-index line hashes, each element represents one line |
3861 | of diff-index output | |
3862 | ||
3863 | diff-index line ($m hash) | |
3864 | { | |
3865 | mode_a => first column of diff-index output, no leading ':', | |
3866 | mode_b => second column of diff-index output, | |
3867 | sha1_b => sha1sum of the final blob, | |
ac8e0b91 | 3868 | chg => change type [MCRADT], |
3397f9df EW |
3869 | file_a => original file name of a file (iff chg is 'C' or 'R') |
3870 | file_b => new/current file name of a file (any chg) | |
3871 | } | |
3872 | ; | |
a5e0cedc | 3873 | |
a00439ac EW |
3874 | # retval of read_url_paths{,_all}(); |
3875 | $l_map = { | |
3876 | # repository root url | |
3877 | 'https://svn.musicpd.org' => { | |
3878 | # repository path # GIT_SVN_ID | |
3879 | 'mpd/trunk' => 'trunk', | |
3880 | 'mpd/tags/0.11.5' => 'tags/0.11.5', | |
3881 | }, | |
3882 | } | |
3883 | ||
a5e0cedc EW |
3884 | Notes: |
3885 | I don't trust the each() function on unless I created %hash myself | |
3886 | because the internal iterator may not have started at base. |