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