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