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