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