]>
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 | |
d48b2841 | 4 | use 5.008; |
3397f9df EW |
5 | use warnings; |
6 | use strict; | |
7 | use vars qw/ $AUTHOR $VERSION | |
ffe256f9 | 8 | $sha1 $sha1_short $_revision $_repository |
36db1edd | 9 | $_q $_authors $_authors_prog %users/; |
3397f9df | 10 | $AUTHOR = 'Eric Wong <normalperson@yhbt.net>'; |
60d02ccc | 11 | $VERSION = '@@GIT_VERSION@@'; |
13ccd6d4 | 12 | |
15153451 BS |
13 | # From which subdir have we been invoked? |
14 | my $cmd_dir_prefix = eval { | |
15 | command_oneline([qw/rev-parse --show-prefix/], STDERR => 0) | |
16 | } || ''; | |
17 | ||
5253dc33 | 18 | my $git_dir_user_set = 1 if defined $ENV{GIT_DIR}; |
706587fc | 19 | $ENV{GIT_DIR} ||= '.git'; |
9fa00b65 | 20 | $Git::SVN::default_repo_id = 'svn'; |
8b8fc068 | 21 | $Git::SVN::default_ref_id = $ENV{GIT_SVN_ID} || 'git-svn'; |
6af1db44 | 22 | $Git::SVN::Ra::_log_window_size = 100; |
6b48829d | 23 | $Git::SVN::_minimize_url = 'unset'; |
13ccd6d4 | 24 | |
184892fb SS |
25 | if (! exists $ENV{SVN_SSH} && exists $ENV{GIT_SSH}) { |
26 | $ENV{SVN_SSH} = $ENV{GIT_SSH}; | |
27 | } | |
28 | ||
29 | if (exists $ENV{SVN_SSH} && $^O eq 'msys') { | |
30 | $ENV{SVN_SSH} =~ s/\\/\\\\/g; | |
31 | $ENV{SVN_SSH} =~ s/(.*)/"$1"/; | |
f3a87d92 K |
32 | } |
33 | ||
f8c9d1d2 | 34 | $Git::SVN::Log::TZ = $ENV{TZ}; |
3397f9df | 35 | $ENV{TZ} = 'UTC'; |
a00439ac | 36 | $| = 1; # unbuffer STDOUT |
3397f9df | 37 | |
207f1a75 | 38 | sub fatal (@) { print STDERR "@_\n"; exit 1 } |
d32fad2b | 39 | sub _req_svn { |
40 | require SVN::Core; # use()-ing this causes segfaults for me... *shrug* | |
41 | require SVN::Ra; | |
42 | require SVN::Delta; | |
43 | if ($SVN::Core::VERSION lt '1.1.0') { | |
44 | fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)"; | |
45 | } | |
b9c85187 | 46 | } |
2da9ee08 | 47 | my $can_compress = eval { require Compress::Zlib; 1}; |
d81bf827 | 48 | push @Git::SVN::Ra::ISA, 'SVN::Ra'; |
b9c85187 EW |
49 | push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor'; |
50 | push @SVN::Git::Fetcher::ISA, 'SVN::Delta::Editor'; | |
3397f9df | 51 | use Carp qw/croak/; |
8d7c4fad | 52 | use Digest::MD5; |
3397f9df EW |
53 | use IO::File qw//; |
54 | use File::Basename qw/dirname basename/; | |
55 | use File::Path qw/mkpath/; | |
36db1edd | 56 | use File::Spec; |
2da9ee08 | 57 | use File::Find; |
512b620b | 58 | use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/; |
968bdf1f | 59 | use IPC::Open3; |
336f1714 | 60 | use Git; |
f5549afd | 61 | use Memoize; # core since 5.8.0, Jul 2002 |
a5e0cedc | 62 | |
336f1714 | 63 | BEGIN { |
c5f71ad0 SV |
64 | # import functions from Git into our packages, en masse |
65 | no strict 'refs'; | |
336f1714 | 66 | foreach (qw/command command_oneline command_noisy command_output_pipe |
6ea42032 BB |
67 | command_input_pipe command_close_pipe |
68 | command_bidi_pipe command_close_bidi_pipe/) { | |
c5f71ad0 | 69 | for my $package ( qw(SVN::Git::Editor SVN::Git::Fetcher |
8d7c4fad | 70 | Git::SVN::Migration Git::SVN::Log Git::SVN), |
c5f71ad0 SV |
71 | __PACKAGE__) { |
72 | *{"${package}::$_"} = \&{"Git::$_"}; | |
73 | } | |
336f1714 | 74 | } |
f5549afd JK |
75 | Memoize::memoize 'Git::config'; |
76 | Memoize::memoize 'Git::config_bool'; | |
336f1714 EW |
77 | } |
78 | ||
b9c85187 | 79 | my ($SVN); |
83e9940a | 80 | |
f8c9d1d2 EW |
81 | $sha1 = qr/[a-f\d]{40}/; |
82 | $sha1_short = qr/[a-f\d]{4,40}/; | |
44320b9e | 83 | my ($_stdin, $_help, $_edit, |
62244069 | 84 | $_message, $_file, $_branch_dest, |
d05d72e0 | 85 | $_template, $_shared, |
c2abd83f | 86 | $_version, $_fetch_all, $_no_rebase, $_fetch_parent, |
dee41f3e | 87 | $_merge, $_strategy, $_dry_run, $_local, |
4be40381 | 88 | $_prefix, $_no_checkout, $_url, $_verbose, |
afd7f1eb | 89 | $_git_format, $_commit_url, $_tag, $_merge_info, $_interactive); |
0bed5eaa | 90 | $Git::SVN::_follow_parent = 1; |
40a1530c | 91 | $SVN::Git::Fetcher::_placeholder_filename = ".gitignore"; |
49750f30 | 92 | $_q ||= 0; |
706587fc EW |
93 | my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username, |
94 | 'config-dir=s' => \$Git::SVN::Ra::config_dir, | |
edc662f9 | 95 | 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache, |
cdb51a13 MO |
96 | 'ignore-paths=s' => \$SVN::Git::Fetcher::_ignore_regex, |
97 | 'ignore-refs=s' => \$Git::SVN::Ra::_ignore_refs_regex ); | |
0bed5eaa | 98 | my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent, |
dc5869c0 | 99 | 'authors-file|A=s' => \$_authors, |
36db1edd | 100 | 'authors-prog=s' => \$_authors_prog, |
ecc712dd | 101 | 'repack:i' => \$Git::SVN::_repack, |
97ae0911 EW |
102 | 'noMetadata' => \$Git::SVN::_no_metadata, |
103 | 'useSvmProps' => \$Git::SVN::_use_svm_props, | |
62e349d2 | 104 | 'useSvnsyncProps' => \$Git::SVN::_use_svnsync_props, |
6af1db44 | 105 | 'log-window-size=i' => \$Git::SVN::Ra::_log_window_size, |
1e889ef3 | 106 | 'no-checkout' => \$_no_checkout, |
49750f30 | 107 | 'quiet|q+' => \$_q, |
ecc712dd EW |
108 | 'repack-flags|repack-args|repack-opts=s' => |
109 | \$Git::SVN::_repack_flags, | |
70ae04e4 | 110 | 'use-log-author' => \$Git::SVN::_use_log_author, |
6aa9ba14 | 111 | 'add-author-from' => \$Git::SVN::_add_author_from, |
e82f0d73 | 112 | 'localtime' => \$Git::SVN::_localtime, |
706587fc | 113 | %remote_opts ); |
36f5b1f0 | 114 | |
62244069 | 115 | my ($_trunk, @_tags, @_branches, $_stdlayout); |
0dfaf0a4 | 116 | my %icv; |
dadc6d2a | 117 | my %init_opts = ( 'template=s' => \$_template, 'shared:s' => \$_shared, |
62244069 MB |
118 | 'trunk|T=s' => \$_trunk, 'tags|t=s@' => \@_tags, |
119 | 'branches|b=s@' => \@_branches, 'prefix=s' => \$_prefix, | |
8f728fb9 | 120 | 'stdlayout|s' => \$_stdlayout, |
6b48829d | 121 | 'minimize-url|m!' => \$Git::SVN::_minimize_url, |
0dfaf0a4 EW |
122 | 'no-metadata' => sub { $icv{noMetadata} = 1 }, |
123 | 'use-svm-props' => sub { $icv{useSvmProps} = 1 }, | |
124 | 'use-svnsync-props' => sub { $icv{useSvnsyncProps} = 1 }, | |
125 | 'rewrite-root=s' => sub { $icv{rewriteRoot} = $_[1] }, | |
3e18ce1a | 126 | 'rewrite-uuid=s' => sub { $icv{rewriteUUID} = $_[1] }, |
dadc6d2a | 127 | %remote_opts ); |
27e9fb8d | 128 | my %cmt_opts = ( 'edit|e' => \$_edit, |
24e22aa8 EW |
129 | 'rmdir' => \$SVN::Git::Editor::_rmdir, |
130 | 'find-copies-harder' => \$SVN::Git::Editor::_find_copies_harder, | |
131 | 'l=i' => \$SVN::Git::Editor::_rename_limit, | |
132 | 'copy-similarity|C=i'=> \$SVN::Git::Editor::_cp_similarity | |
27e9fb8d | 133 | ); |
9d55b41a | 134 | |
3397f9df | 135 | my %cmd = ( |
2a3240be | 136 | fetch => [ \&cmd_fetch, "Download new revisions from SVN", |
e98671e5 | 137 | { 'revision|r=s' => \$_revision, |
905f8b7d | 138 | 'fetch-all|all' => \$_fetch_all, |
c2abd83f | 139 | 'parent|p' => \$_fetch_parent, |
e98671e5 | 140 | %fc_opts } ], |
0425ea90 EW |
141 | clone => [ \&cmd_clone, "Initialize and fetch revisions", |
142 | { 'revision|r=s' => \$_revision, | |
40a1530c RC |
143 | 'preserve-empty-dirs' => |
144 | \$SVN::Git::Fetcher::_preserve_empty_dirs, | |
145 | 'placeholder-filename=s' => | |
146 | \$SVN::Git::Fetcher::_placeholder_filename, | |
0425ea90 | 147 | %fc_opts, %init_opts } ], |
d2866f9e | 148 | init => [ \&cmd_init, "Initialize a repo for tracking" . |
f8ab6b73 | 149 | " (requires URL argument)", |
9d55b41a | 150 | \%init_opts ], |
dadc6d2a EW |
151 | 'multi-init' => [ \&cmd_multi_init, |
152 | "Deprecated alias for ". | |
153 | "'$0 init -T<trunk> -b<branches> -t<tags>'", | |
154 | \%init_opts ], | |
d7ad3bed EW |
155 | dcommit => [ \&cmd_dcommit, |
156 | 'Commit several diffs to merge with upstream', | |
3289e86e EW |
157 | { 'merge|m|M' => \$_merge, |
158 | 'strategy|s=s' => \$_strategy, | |
905f8b7d | 159 | 'verbose|v' => \$_verbose, |
3289e86e | 160 | 'dry-run|n' => \$_dry_run, |
905f8b7d | 161 | 'fetch-all|all' => \$_fetch_all, |
ba24e745 EW |
162 | 'commit-url=s' => \$_commit_url, |
163 | 'revision|r=i' => \$_revision, | |
171af110 | 164 | 'no-rebase' => \$_no_rebase, |
6abd9332 | 165 | 'mergeinfo=s' => \$_merge_info, |
afd7f1eb | 166 | 'interactive|i' => \$_interactive, |
4b155223 | 167 | %cmt_opts, %fc_opts } ], |
5de70efb FR |
168 | branch => [ \&cmd_branch, |
169 | 'Create a branch in the SVN repository', | |
170 | { 'message|m=s' => \$_message, | |
62244069 | 171 | 'destination|d=s' => \$_branch_dest, |
5de70efb | 172 | 'dry-run|n' => \$_dry_run, |
6594f0b7 IM |
173 | 'tag|t' => \$_tag, |
174 | 'username=s' => \$Git::SVN::Prompt::_username, | |
175 | 'commit-url=s' => \$_commit_url } ], | |
5de70efb FR |
176 | tag => [ sub { $_tag = 1; cmd_branch(@_) }, |
177 | 'Create a tag in the SVN repository', | |
178 | { 'message|m=s' => \$_message, | |
62244069 | 179 | 'destination|d=s' => \$_branch_dest, |
6594f0b7 IM |
180 | 'dry-run|n' => \$_dry_run, |
181 | 'username=s' => \$Git::SVN::Prompt::_username, | |
182 | 'commit-url=s' => \$_commit_url } ], | |
1ce255dc EW |
183 | 'set-tree' => [ \&cmd_set_tree, |
184 | "Set an SVN repository to a git tree-ish", | |
e84dc6df | 185 | { 'stdin' => \$_stdin, %cmt_opts, %fc_opts, } ], |
d05ddec5 BS |
186 | 'create-ignore' => [ \&cmd_create_ignore, |
187 | 'Create a .gitignore per svn:ignore', | |
188 | { 'revision|r=i' => \$_revision | |
189 | } ], | |
6111b934 EW |
190 | 'mkdirs' => [ \&cmd_mkdirs , |
191 | "recreate empty directories after a checkout", | |
192 | { 'revision|r=i' => \$_revision } ], | |
15153451 BS |
193 | 'propget' => [ \&cmd_propget, |
194 | 'Print the value of a property on a file or directory', | |
195 | { 'revision|r=i' => \$_revision } ], | |
51e057cf BS |
196 | 'proplist' => [ \&cmd_proplist, |
197 | 'List all properties of a file or directory', | |
198 | { 'revision|r=i' => \$_revision } ], | |
5969cbe1 | 199 | 'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings", |
4dbfe2e9 | 200 | { 'revision|r=i' => \$_revision |
05b4df31 | 201 | } ], |
2d879792 VK |
202 | 'show-externals' => [ \&cmd_show_externals, "Show svn:externals listings", |
203 | { 'revision|r=i' => \$_revision | |
204 | } ], | |
1c8443b0 | 205 | 'multi-fetch' => [ \&cmd_multi_fetch, |
e98671e5 EW |
206 | "Deprecated alias for $0 fetch --all", |
207 | { 'revision|r=s' => \$_revision, %fc_opts } ], | |
706587fc EW |
208 | 'migrate' => [ sub { }, |
209 | # no-op, we automatically run this anyways, | |
706587fc EW |
210 | 'Migrate configuration/metadata/layout from |
211 | previous versions of git-svn', | |
a836a0e1 EW |
212 | { 'minimize' => \$Git::SVN::Migration::_minimize, |
213 | %remote_opts } ], | |
f8c9d1d2 EW |
214 | 'log' => [ \&Git::SVN::Log::cmd_show_log, 'Show commit logs', |
215 | { 'limit=i' => \$Git::SVN::Log::limit, | |
79bb8d88 | 216 | 'revision|r=s' => \$_revision, |
f8c9d1d2 EW |
217 | 'verbose|v' => \$Git::SVN::Log::verbose, |
218 | 'incremental' => \$Git::SVN::Log::incremental, | |
219 | 'oneline' => \$Git::SVN::Log::oneline, | |
220 | 'show-commit' => \$Git::SVN::Log::show_commit, | |
221 | 'non-recursive' => \$Git::SVN::Log::non_recursive, | |
79bb8d88 | 222 | 'authors-file|A=s' => \$_authors, |
f8c9d1d2 | 223 | 'color' => \$Git::SVN::Log::color, |
4dbfe2e9 | 224 | 'pager=s' => \$Git::SVN::Log::pager |
79bb8d88 | 225 | } ], |
222566e4 EW |
226 | 'find-rev' => [ \&cmd_find_rev, |
227 | "Translate between SVN revision numbers and tree-ish", | |
4dbfe2e9 | 228 | {} ], |
905f8b7d EW |
229 | 'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory", |
230 | { 'merge|m|M' => \$_merge, | |
231 | 'verbose|v' => \$_verbose, | |
232 | 'strategy|s=s' => \$_strategy, | |
dee41f3e | 233 | 'local|l' => \$_local, |
905f8b7d | 234 | 'fetch-all|all' => \$_fetch_all, |
7d45e146 | 235 | 'dry-run|n' => \$_dry_run, |
905f8b7d | 236 | %fc_opts } ], |
44320b9e EW |
237 | 'commit-diff' => [ \&cmd_commit_diff, |
238 | 'Commit a diff between two trees', | |
27e9fb8d EW |
239 | { 'message|m=s' => \$_message, |
240 | 'file|F=s' => \$_file, | |
45bf473a | 241 | 'revision|r=s' => \$_revision, |
27e9fb8d | 242 | %cmt_opts } ], |
e6fefa92 DK |
243 | 'info' => [ \&cmd_info, |
244 | "Show info about the latest SVN revision | |
245 | on the current branch", | |
8b014d71 | 246 | { 'url' => \$_url, } ], |
6fb5375e TS |
247 | 'blame' => [ \&Git::SVN::Log::cmd_blame, |
248 | "Show what revision and author last modified each line of a file", | |
4be40381 | 249 | { 'git-format' => \$_git_format } ], |
195643f2 BJ |
250 | 'reset' => [ \&cmd_reset, |
251 | "Undo fetches back to the specified SVN revision", | |
252 | { 'revision|r=s' => \$_revision, | |
253 | 'parent|p' => \$_fetch_parent } ], | |
2da9ee08 RZ |
254 | 'gc' => [ \&cmd_gc, |
255 | "Compress unhandled.log files in .git/svn and remove " . | |
256 | "index files in .git/svn", | |
257 | {} ], | |
3397f9df | 258 | ); |
9d55b41a | 259 | |
afd7f1eb FH |
260 | use Term::ReadLine; |
261 | package FakeTerm; | |
262 | sub new { | |
263 | my ($class, $reason) = @_; | |
264 | return bless \$reason, shift; | |
265 | } | |
266 | sub readline { | |
267 | my $self = shift; | |
268 | die "Cannot use readline on FakeTerm: $$self"; | |
269 | } | |
270 | package main; | |
271 | ||
272 | my $term = eval { | |
273 | $ENV{"GIT_SVN_NOTTY"} | |
274 | ? new Term::ReadLine 'git-svn', \*STDIN, \*STDOUT | |
275 | : new Term::ReadLine 'git-svn'; | |
276 | }; | |
277 | if ($@) { | |
278 | $term = new FakeTerm "$@: going non-interactive"; | |
279 | } | |
280 | ||
3397f9df EW |
281 | my $cmd; |
282 | for (my $i = 0; $i < @ARGV; $i++) { | |
283 | if (defined $cmd{$ARGV[$i]}) { | |
284 | $cmd = $ARGV[$i]; | |
285 | splice @ARGV, $i, 1; | |
286 | last; | |
9a8c92ac BJ |
287 | } elsif ($ARGV[$i] eq 'help') { |
288 | $cmd = $ARGV[$i+1]; | |
289 | usage(0); | |
3397f9df EW |
290 | } |
291 | }; | |
292 | ||
540424b2 EW |
293 | # make sure we're always running at the top-level working directory |
294 | unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) { | |
5253dc33 EW |
295 | unless (-d $ENV{GIT_DIR}) { |
296 | if ($git_dir_user_set) { | |
297 | die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ", | |
298 | "but it is not a directory\n"; | |
299 | } | |
300 | my $git_dir = delete $ENV{GIT_DIR}; | |
fe4003f6 DM |
301 | my $cdup = undef; |
302 | git_cmd_try { | |
303 | $cdup = command_oneline(qw/rev-parse --show-cdup/); | |
304 | $git_dir = '.' unless ($cdup); | |
305 | chomp $cdup if ($cdup); | |
306 | $cdup = "." unless ($cdup && length $cdup); | |
307 | } "Already at toplevel, but $git_dir not found\n"; | |
5253dc33 EW |
308 | chdir $cdup or die "Unable to chdir up to '$cdup'\n"; |
309 | unless (-d $git_dir) { | |
310 | die "$git_dir still not found after going to ", | |
311 | "'$cdup'\n"; | |
312 | } | |
313 | $ENV{GIT_DIR} = $git_dir; | |
314 | } | |
ffe256f9 | 315 | $_repository = Git->repository(Repository => $ENV{GIT_DIR}); |
5253dc33 | 316 | } |
f4dd334b GH |
317 | |
318 | my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd); | |
319 | ||
1a30582b | 320 | read_git_config(\%opts); |
222566e4 EW |
321 | if ($cmd && ($cmd eq 'log' || $cmd eq 'blame')) { |
322 | Getopt::Long::Configure('pass_through'); | |
323 | } | |
87182b17 | 324 | my $rv = GetOptions(%opts, 'h|H' => \$_help, 'version|V' => \$_version, |
f4dd334b GH |
325 | 'minimize-connections' => \$Git::SVN::Migration::_minimize, |
326 | 'id|i=s' => \$Git::SVN::default_ref_id, | |
327 | 'svn-remote|remote|R=s' => sub { | |
328 | $Git::SVN::no_reuse_existing = 1; | |
329 | $Git::SVN::default_repo_id = $_[1] }); | |
330 | exit 1 if (!$rv && $cmd && $cmd ne 'log'); | |
331 | ||
332 | usage(0) if $_help; | |
333 | version() if $_version; | |
334 | usage(1) unless defined $cmd; | |
335 | load_authors() if $_authors; | |
36db1edd ML |
336 | if (defined $_authors_prog) { |
337 | $_authors_prog = "'" . File::Spec->rel2abs($_authors_prog) . "'"; | |
338 | } | |
f4dd334b | 339 | |
0425ea90 | 340 | unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) { |
706587fc EW |
341 | Git::SVN::Migration::migration_check(); |
342 | } | |
ecc712dd | 343 | Git::SVN::init_vars(); |
b805b44a EW |
344 | eval { |
345 | Git::SVN::verify_remotes_sanity(); | |
346 | $cmd{$cmd}->[0]->(@ARGV); | |
347 | }; | |
348 | fatal $@ if $@; | |
1e889ef3 | 349 | post_fetch_checkout(); |
3397f9df EW |
350 | exit 0; |
351 | ||
352 | ####################### primary functions ###################### | |
353 | sub usage { | |
354 | my $exit = shift || 0; | |
355 | my $fd = $exit ? \*STDERR : \*STDOUT; | |
356 | print $fd <<""; | |
357 | git-svn - bidirectional operations between a single Subversion tree and git | |
1b1dd23f | 358 | Usage: git svn <command> [options] [arguments]\n |
448c81b4 EW |
359 | |
360 | print $fd "Available commands:\n" unless $cmd; | |
3397f9df EW |
361 | |
362 | foreach (sort keys %cmd) { | |
448c81b4 | 363 | next if $cmd && $cmd ne $_; |
a836a0e1 | 364 | next if /^multi-/; # don't show deprecated commands |
b203b769 | 365 | print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n"; |
aa807bc2 | 366 | foreach (sort keys %{$cmd{$_}->[2]}) { |
512b620b EW |
367 | # mixed-case options are for .git/config only |
368 | next if /[A-Z]/ && /^[a-z]+$/i; | |
448c81b4 | 369 | # prints out arguments as they should be passed: |
b8c92cad | 370 | my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : ''; |
b203b769 | 371 | print $fd ' ' x 21, join(', ', map { length $_ > 1 ? |
448c81b4 EW |
372 | "--$_" : "-$_" } |
373 | split /\|/,$_)," $x\n"; | |
374 | } | |
3397f9df EW |
375 | } |
376 | print $fd <<""; | |
448c81b4 EW |
377 | \nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an |
378 | arbitrary identifier if you're tracking multiple SVN branches/repositories in | |
379 | one git repository and want to keep them separate. See git-svn(1) for more | |
380 | information. | |
3397f9df EW |
381 | |
382 | exit $exit; | |
383 | } | |
384 | ||
551ce28f | 385 | sub version { |
b0779246 | 386 | ::_req_svn(); |
7d60ab2c | 387 | print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n"; |
551ce28f EW |
388 | exit 0; |
389 | } | |
390 | ||
afd7f1eb FH |
391 | sub ask { |
392 | my ($prompt, %arg) = @_; | |
393 | my $valid_re = $arg{valid_re}; | |
394 | my $default = $arg{default}; | |
395 | my $resp; | |
396 | my $i = 0; | |
397 | ||
398 | if ( !( defined($term->IN) | |
399 | && defined( fileno($term->IN) ) | |
400 | && defined( $term->OUT ) | |
401 | && defined( fileno($term->OUT) ) ) ){ | |
402 | return defined($default) ? $default : undef; | |
403 | } | |
404 | ||
405 | while ($i++ < 10) { | |
406 | $resp = $term->readline($prompt); | |
407 | if (!defined $resp) { # EOF | |
408 | print "\n"; | |
409 | return defined $default ? $default : undef; | |
410 | } | |
411 | if ($resp eq '' and defined $default) { | |
412 | return $default; | |
413 | } | |
414 | if (!defined $valid_re or $resp =~ /$valid_re/) { | |
415 | return $resp; | |
416 | } | |
417 | } | |
418 | return undef; | |
419 | } | |
420 | ||
8164b652 EW |
421 | sub do_git_init_db { |
422 | unless (-d $ENV{GIT_DIR}) { | |
423 | my @init_db = ('init'); | |
424 | push @init_db, "--template=$_template" if defined $_template; | |
dadc6d2a EW |
425 | if (defined $_shared) { |
426 | if ($_shared =~ /[a-z]/) { | |
427 | push @init_db, "--shared=$_shared"; | |
428 | } else { | |
429 | push @init_db, "--shared"; | |
430 | } | |
431 | } | |
8164b652 | 432 | command_noisy(@init_db); |
ffe256f9 | 433 | $_repository = Git->repository(Repository => ".git"); |
8164b652 | 434 | } |
0dfaf0a4 EW |
435 | my $set; |
436 | my $pfx = "svn-remote.$Git::SVN::default_repo_id"; | |
437 | foreach my $i (keys %icv) { | |
438 | die "'$set' and '$i' cannot both be set\n" if $set; | |
439 | next unless defined $icv{$i}; | |
440 | command_noisy('config', "$pfx.$i", $icv{$i}); | |
441 | $set = $i; | |
442 | } | |
cdb51a13 MO |
443 | my $ignore_paths_regex = \$SVN::Git::Fetcher::_ignore_regex; |
444 | command_noisy('config', "$pfx.ignore-paths", $$ignore_paths_regex) | |
445 | if defined $$ignore_paths_regex; | |
446 | my $ignore_refs_regex = \$Git::SVN::Ra::_ignore_refs_regex; | |
447 | command_noisy('config', "$pfx.ignore-refs", $$ignore_refs_regex) | |
448 | if defined $$ignore_refs_regex; | |
40a1530c RC |
449 | |
450 | if (defined $SVN::Git::Fetcher::_preserve_empty_dirs) { | |
451 | my $fname = \$SVN::Git::Fetcher::_placeholder_filename; | |
452 | command_noisy('config', "$pfx.preserve-empty-dirs", 'true'); | |
453 | command_noisy('config', "$pfx.placeholder-filename", $$fname); | |
454 | } | |
8164b652 EW |
455 | } |
456 | ||
dadc6d2a EW |
457 | sub init_subdir { |
458 | my $repo_path = shift or return; | |
459 | mkpath([$repo_path]) unless -d $repo_path; | |
460 | chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n"; | |
f30603fc | 461 | $ENV{GIT_DIR} = '.git'; |
ffe256f9 | 462 | $_repository = Git->repository(Repository => $ENV{GIT_DIR}); |
dadc6d2a EW |
463 | } |
464 | ||
0425ea90 EW |
465 | sub cmd_clone { |
466 | my ($url, $path) = @_; | |
467 | if (!defined $path && | |
62244069 | 468 | (defined $_trunk || @_branches || @_tags || |
8f728fb9 | 469 | defined $_stdlayout) && |
0425ea90 EW |
470 | $url !~ m#^[a-z\+]+://#) { |
471 | $path = $url; | |
472 | } | |
0425ea90 | 473 | $path = basename($url) if !defined $path || !length $path; |
2bc35dcb | 474 | my $authors_absolute = $_authors ? File::Spec->rel2abs($_authors) : ""; |
f30603fc | 475 | cmd_init($url, $path); |
2bc35dcb AV |
476 | command_oneline('config', 'svn.authorsfile', $authors_absolute) |
477 | if $_authors; | |
f5841509 | 478 | Git::SVN::fetch_all($Git::SVN::default_repo_id); |
0425ea90 EW |
479 | } |
480 | ||
d2866f9e | 481 | sub cmd_init { |
8f728fb9 | 482 | if (defined $_stdlayout) { |
483 | $_trunk = 'trunk' if (!defined $_trunk); | |
62244069 MB |
484 | @_tags = 'tags' if (! @_tags); |
485 | @_branches = 'branches' if (! @_branches); | |
8f728fb9 | 486 | } |
62244069 | 487 | if (defined $_trunk || @_branches || @_tags) { |
dadc6d2a | 488 | return cmd_multi_init(@_); |
03e0ea87 | 489 | } |
dadc6d2a EW |
490 | my $url = shift or die "SVN repository location required ", |
491 | "as a command-line argument\n"; | |
50ff2366 | 492 | $url = canonicalize_url($url); |
dadc6d2a | 493 | init_subdir(@_); |
8164b652 | 494 | do_git_init_db(); |
03e0ea87 | 495 | |
6b48829d EW |
496 | if ($Git::SVN::_minimize_url eq 'unset') { |
497 | $Git::SVN::_minimize_url = 0; | |
498 | } | |
499 | ||
706587fc | 500 | Git::SVN->init($url); |
3397f9df EW |
501 | } |
502 | ||
2a3240be | 503 | sub cmd_fetch { |
e98671e5 EW |
504 | if (grep /^\d+=./, @_) { |
505 | die "'<rev>=<commit>' fetch arguments are ", | |
506 | "no longer supported.\n"; | |
07a1c950 | 507 | } |
e98671e5 EW |
508 | my ($remote) = @_; |
509 | if (@_ > 1) { | |
c2abd83f | 510 | die "Usage: $0 fetch [--all] [--parent] [svn-remote]\n"; |
e98671e5 | 511 | } |
4d0157d6 | 512 | $Git::SVN::no_reuse_existing = undef; |
c2abd83f JM |
513 | if ($_fetch_parent) { |
514 | my ($url, $rev, $uuid, $gs) = working_head_info('HEAD'); | |
515 | unless ($gs) { | |
516 | die "Unable to determine upstream SVN information from ", | |
517 | "working tree history\n"; | |
518 | } | |
519 | # just fetch, don't checkout. | |
520 | $_no_checkout = 'true'; | |
521 | $_fetch_all ? $gs->fetch_all : $gs->fetch; | |
522 | } elsif ($_fetch_all) { | |
e98671e5 EW |
523 | cmd_multi_fetch(); |
524 | } else { | |
c2abd83f | 525 | $remote ||= $Git::SVN::default_repo_id; |
e98671e5 | 526 | Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes()); |
1c8443b0 | 527 | } |
2a3240be EW |
528 | } |
529 | ||
1ce255dc | 530 | sub cmd_set_tree { |
3397f9df EW |
531 | my (@commits) = @_; |
532 | if ($_stdin || !@commits) { | |
533 | print "Reading from stdin...\n"; | |
534 | @commits = (); | |
535 | while (<STDIN>) { | |
1ca72aef | 536 | if (/\b($sha1_short)\b/o) { |
3397f9df EW |
537 | unshift @commits, $1; |
538 | } | |
539 | } | |
540 | } | |
541 | my @revs; | |
8de010ad | 542 | foreach my $c (@commits) { |
aef4e921 | 543 | my @tmp = command('rev-parse',$c); |
8de010ad EW |
544 | if (scalar @tmp == 1) { |
545 | push @revs, $tmp[0]; | |
546 | } elsif (scalar @tmp > 1) { | |
aef4e921 | 547 | push @revs, reverse(command('rev-list',@tmp)); |
8de010ad | 548 | } else { |
207f1a75 | 549 | fatal "Failed to rev-parse $c"; |
8de010ad | 550 | } |
3397f9df | 551 | } |
1ce255dc EW |
552 | my $gs = Git::SVN->new; |
553 | my ($r_last, $cmt_last) = $gs->last_rev_commit; | |
554 | $gs->fetch; | |
97f6987a | 555 | if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) { |
1ce255dc EW |
556 | fatal "There are new revisions that were fetched ", |
557 | "and need to be merged (or acknowledged) ", | |
558 | "before committing.\nlast rev: $r_last\n", | |
207f1a75 | 559 | " current: $gs->{last_rev}"; |
a5e0cedc | 560 | } |
1ce255dc EW |
561 | $gs->set_tree($_) foreach @revs; |
562 | print "Done committing ",scalar @revs," revisions to SVN\n"; | |
3157dd9e | 563 | unlink $gs->{index}; |
a5e0cedc | 564 | } |
8f22562c | 565 | |
1e5814f3 BJ |
566 | sub split_merge_info_range { |
567 | my ($range) = @_; | |
568 | if ($range =~ /(\d+)-(\d+)/) { | |
569 | return (int($1), int($2)); | |
570 | } else { | |
571 | return (int($range), int($range)); | |
572 | } | |
573 | } | |
574 | ||
575 | sub combine_ranges { | |
576 | my ($in) = @_; | |
577 | ||
578 | my @fnums = (); | |
579 | my @arr = split(/,/, $in); | |
580 | for my $element (@arr) { | |
581 | my ($start, $end) = split_merge_info_range($element); | |
582 | push @fnums, $start; | |
583 | } | |
584 | ||
585 | my @sorted = @arr [ sort { | |
586 | $fnums[$a] <=> $fnums[$b] | |
587 | } 0..$#arr ]; | |
588 | ||
589 | my @return = (); | |
590 | my $last = -1; | |
591 | my $first = -1; | |
592 | for my $element (@sorted) { | |
593 | my ($start, $end) = split_merge_info_range($element); | |
594 | ||
595 | if ($last == -1) { | |
596 | $first = $start; | |
597 | $last = $end; | |
598 | next; | |
599 | } | |
600 | if ($start <= $last+1) { | |
601 | if ($end > $last) { | |
602 | $last = $end; | |
603 | } | |
604 | next; | |
605 | } | |
606 | if ($first == $last) { | |
607 | push @return, "$first"; | |
608 | } else { | |
609 | push @return, "$first-$last"; | |
610 | } | |
611 | $first = $start; | |
612 | $last = $end; | |
613 | } | |
614 | ||
615 | if ($first != -1) { | |
616 | if ($first == $last) { | |
617 | push @return, "$first"; | |
618 | } else { | |
619 | push @return, "$first-$last"; | |
620 | } | |
621 | } | |
622 | ||
623 | return join(',', @return); | |
624 | } | |
625 | ||
626 | sub merge_revs_into_hash { | |
627 | my ($hash, $minfo) = @_; | |
628 | my @lines = split(' ', $minfo); | |
629 | ||
630 | for my $line (@lines) { | |
631 | my ($branchpath, $revs) = split(/:/, $line); | |
632 | ||
633 | if (exists($hash->{$branchpath})) { | |
634 | # Merge the two revision sets | |
635 | my $combined = "$hash->{$branchpath},$revs"; | |
636 | $hash->{$branchpath} = combine_ranges($combined); | |
637 | } else { | |
638 | # Just do range combining for consolidation | |
639 | $hash->{$branchpath} = combine_ranges($revs); | |
640 | } | |
641 | } | |
642 | } | |
643 | ||
644 | sub merge_merge_info { | |
645 | my ($mergeinfo_one, $mergeinfo_two) = @_; | |
646 | my %result_hash = (); | |
647 | ||
648 | merge_revs_into_hash(\%result_hash, $mergeinfo_one); | |
649 | merge_revs_into_hash(\%result_hash, $mergeinfo_two); | |
650 | ||
651 | my $result = ''; | |
652 | # Sort below is for consistency's sake | |
653 | for my $branchname (sort keys(%result_hash)) { | |
654 | my $revlist = $result_hash{$branchname}; | |
655 | $result .= "$branchname:$revlist\n" | |
656 | } | |
657 | return $result; | |
658 | } | |
659 | ||
660 | sub populate_merge_info { | |
661 | my ($d, $gs, $uuid, $linear_refs, $rewritten_parent) = @_; | |
662 | ||
663 | my %parentshash; | |
664 | read_commit_parents(\%parentshash, $d); | |
665 | my @parents = @{$parentshash{$d}}; | |
666 | if ($#parents > 0) { | |
667 | # Merge commit | |
668 | my $all_parents_ok = 1; | |
669 | my $aggregate_mergeinfo = ''; | |
670 | my $rooturl = $gs->repos_root; | |
671 | ||
672 | if (defined($rewritten_parent)) { | |
673 | # Replace first parent with newly-rewritten version | |
674 | shift @parents; | |
675 | unshift @parents, $rewritten_parent; | |
676 | } | |
677 | ||
678 | foreach my $parent (@parents) { | |
679 | my ($branchurl, $svnrev, $paruuid) = | |
680 | cmt_metadata($parent); | |
681 | ||
682 | unless (defined($svnrev)) { | |
683 | # Should have been caught be preflight check | |
684 | fatal "merge commit $d has ancestor $parent, but that change " | |
685 | ."does not have git-svn metadata!"; | |
686 | } | |
0e7e30f5 | 687 | unless ($branchurl =~ /^\Q$rooturl\E(.*)/) { |
1e5814f3 BJ |
688 | fatal "commit $parent git-svn metadata changed mid-run!"; |
689 | } | |
690 | my $branchpath = $1; | |
691 | ||
692 | my $ra = Git::SVN::Ra->new($branchurl); | |
693 | my (undef, undef, $props) = | |
694 | $ra->get_dir(canonicalize_path("."), $svnrev); | |
695 | my $par_mergeinfo = $props->{'svn:mergeinfo'}; | |
696 | unless (defined $par_mergeinfo) { | |
697 | $par_mergeinfo = ''; | |
698 | } | |
699 | # Merge previous mergeinfo values | |
700 | $aggregate_mergeinfo = | |
701 | merge_merge_info($aggregate_mergeinfo, | |
702 | $par_mergeinfo, 0); | |
703 | ||
704 | next if $parent eq $parents[0]; # Skip first parent | |
705 | # Add new changes being placed in tree by merge | |
706 | my @cmd = (qw/rev-list --reverse/, | |
707 | $parent, qw/--not/); | |
708 | foreach my $par (@parents) { | |
709 | unless ($par eq $parent) { | |
710 | push @cmd, $par; | |
711 | } | |
712 | } | |
713 | my @revsin = (); | |
714 | my ($revlist, $ctx) = command_output_pipe(@cmd); | |
715 | while (<$revlist>) { | |
716 | my $irev = $_; | |
717 | chomp $irev; | |
718 | my (undef, $csvnrev, undef) = | |
719 | cmt_metadata($irev); | |
720 | unless (defined $csvnrev) { | |
721 | # A child is missing SVN annotations... | |
722 | # this might be OK, or might not be. | |
723 | warn "W:child $irev is merged into revision " | |
724 | ."$d but does not have git-svn metadata. " | |
725 | ."This means git-svn cannot determine the " | |
726 | ."svn revision numbers to place into the " | |
727 | ."svn:mergeinfo property. You must ensure " | |
728 | ."a branch is entirely committed to " | |
729 | ."SVN before merging it in order for " | |
730 | ."svn:mergeinfo population to function " | |
731 | ."properly"; | |
732 | } | |
733 | push @revsin, $csvnrev; | |
734 | } | |
735 | command_close_pipe($revlist, $ctx); | |
736 | ||
737 | last unless $all_parents_ok; | |
738 | ||
739 | # We now have a list of all SVN revnos which are | |
740 | # merged by this particular parent. Integrate them. | |
741 | next if $#revsin == -1; | |
742 | my $newmergeinfo = "$branchpath:" . join(',', @revsin); | |
743 | $aggregate_mergeinfo = | |
744 | merge_merge_info($aggregate_mergeinfo, | |
745 | $newmergeinfo, 1); | |
746 | } | |
747 | if ($all_parents_ok and $aggregate_mergeinfo) { | |
748 | return $aggregate_mergeinfo; | |
749 | } | |
750 | } | |
751 | ||
752 | return undef; | |
753 | } | |
754 | ||
d7ad3bed EW |
755 | sub cmd_dcommit { |
756 | my $head = shift; | |
181264ad | 757 | command_noisy(qw/update-index --refresh/); |
c8cfa3e4 | 758 | git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) } |
826a9339 | 759 | 'Cannot dcommit with a dirty index. Commit your changes first, ' |
c8cfa3e4 | 760 | . "or stash them with `git stash'.\n"; |
d7ad3bed | 761 | $head ||= 'HEAD'; |
5eec27e3 TR |
762 | |
763 | my $old_head; | |
764 | if ($head ne 'HEAD') { | |
765 | $old_head = eval { | |
766 | command_oneline([qw/symbolic-ref -q HEAD/]) | |
767 | }; | |
768 | if ($old_head) { | |
769 | $old_head =~ s{^refs/heads/}{}; | |
770 | } else { | |
771 | $old_head = eval { command_oneline(qw/rev-parse HEAD/) }; | |
772 | } | |
773 | command(['checkout', $head], STDERR => 0); | |
774 | } | |
775 | ||
a8ae2623 | 776 | my @refs; |
5eec27e3 | 777 | my ($url, $rev, $uuid, $gs) = working_head_info('HEAD', \@refs); |
2cb61105 TR |
778 | unless ($gs) { |
779 | die "Unable to determine upstream SVN information from ", | |
780 | "$head history.\nPerhaps the repository is empty."; | |
781 | } | |
0df84059 PO |
782 | |
783 | if (defined $_commit_url) { | |
784 | $url = $_commit_url; | |
785 | } else { | |
786 | $url = eval { command_oneline('config', '--get', | |
787 | "svn-remote.$gs->{repo_id}.commiturl") }; | |
788 | if (!$url) { | |
12a296bc | 789 | $url = $gs->full_pushurl |
0df84059 PO |
790 | } |
791 | } | |
792 | ||
ba24e745 | 793 | my $last_rev = $_revision if defined $_revision; |
59b0c24d MM |
794 | if ($url) { |
795 | print "Committing to $url ...\n"; | |
796 | } | |
733a65aa | 797 | my ($linear_refs, $parents) = linearize_history($gs, \@refs); |
751eb395 EW |
798 | if ($_no_rebase && scalar(@$linear_refs) > 1) { |
799 | warn "Attempting to commit more than one change while ", | |
800 | "--no-rebase is enabled.\n", | |
801 | "If these changes depend on each other, re-running ", | |
7dfa16b9 | 802 | "without --no-rebase may be required." |
751eb395 | 803 | } |
afd7f1eb FH |
804 | |
805 | if (defined $_interactive){ | |
806 | my $ask_default = "y"; | |
807 | foreach my $d (@$linear_refs){ | |
808 | my ($fh, $ctx) = command_output_pipe(qw(show --summary), "$d"); | |
809 | while (<$fh>){ | |
810 | print $_; | |
811 | } | |
812 | command_close_pipe($fh, $ctx); | |
813 | $_ = ask("Commit this patch to SVN? ([y]es (default)|[n]o|[q]uit|[a]ll): ", | |
814 | valid_re => qr/^(?:yes|y|no|n|quit|q|all|a)/i, | |
815 | default => $ask_default); | |
816 | die "Commit this patch reply required" unless defined $_; | |
817 | if (/^[nq]/i) { | |
818 | exit(0); | |
819 | } elsif (/^a/i) { | |
820 | last; | |
821 | } | |
822 | } | |
823 | } | |
824 | ||
711521e2 | 825 | my $expect_url = $url; |
1e5814f3 BJ |
826 | |
827 | my $push_merge_info = eval { | |
828 | command_oneline(qw/config --get svn.pushmergeinfo/) | |
829 | }; | |
830 | if (not defined($push_merge_info) | |
831 | or $push_merge_info eq "false" | |
832 | or $push_merge_info eq "no" | |
833 | or $push_merge_info eq "never") { | |
834 | $push_merge_info = 0; | |
835 | } | |
836 | ||
837 | unless (defined($_merge_info) || ! $push_merge_info) { | |
838 | # Preflight check of changes to ensure no issues with mergeinfo | |
839 | # This includes check for uncommitted-to-SVN parents | |
840 | # (other than the first parent, which we will handle), | |
841 | # information from different SVN repos, and paths | |
842 | # which are not underneath this repository root. | |
843 | my $rooturl = $gs->repos_root; | |
844 | foreach my $d (@$linear_refs) { | |
845 | my %parentshash; | |
846 | read_commit_parents(\%parentshash, $d); | |
847 | my @realparents = @{$parentshash{$d}}; | |
848 | if ($#realparents > 0) { | |
849 | # Merge commit | |
850 | shift @realparents; # Remove/ignore first parent | |
851 | foreach my $parent (@realparents) { | |
852 | my ($branchurl, $svnrev, $paruuid) = cmt_metadata($parent); | |
853 | unless (defined $paruuid) { | |
854 | # A parent is missing SVN annotations... | |
855 | # abort the whole operation. | |
856 | fatal "$parent is merged into revision $d, " | |
857 | ."but does not have git-svn metadata. " | |
858 | ."Either dcommit the branch or use a " | |
859 | ."local cherry-pick, FF merge, or rebase " | |
860 | ."instead of an explicit merge commit."; | |
861 | } | |
862 | ||
863 | unless ($paruuid eq $uuid) { | |
864 | # Parent has SVN metadata from different repository | |
865 | fatal "merge parent $parent for change $d has " | |
866 | ."git-svn uuid $paruuid, while current change " | |
867 | ."has uuid $uuid!"; | |
868 | } | |
869 | ||
0e7e30f5 | 870 | unless ($branchurl =~ /^\Q$rooturl\E(.*)/) { |
1e5814f3 BJ |
871 | # This branch is very strange indeed. |
872 | fatal "merge parent $parent for $d is on branch " | |
873 | ."$branchurl, which is not under the " | |
874 | ."git-svn root $rooturl!"; | |
875 | } | |
876 | } | |
877 | } | |
878 | } | |
879 | } | |
880 | ||
881 | my $rewritten_parent; | |
711521e2 | 882 | Git::SVN::remove_username($expect_url); |
98c4ab32 BJ |
883 | if (defined($_merge_info)) { |
884 | $_merge_info =~ tr{ }{\n}; | |
885 | } | |
c74d9acf EW |
886 | while (1) { |
887 | my $d = shift @$linear_refs or last; | |
45bf473a EW |
888 | unless (defined $last_rev) { |
889 | (undef, $last_rev, undef) = cmt_metadata("$d~1"); | |
890 | unless (defined $last_rev) { | |
d7ad3bed | 891 | fatal "Unable to extract revision information ", |
207f1a75 | 892 | "from commit $d~1"; |
45bf473a EW |
893 | } |
894 | } | |
b22d4497 EW |
895 | if ($_dry_run) { |
896 | print "diff-tree $d~1 $d\n"; | |
897 | } else { | |
751eb395 | 898 | my $cmt_rev; |
1e5814f3 BJ |
899 | |
900 | unless (defined($_merge_info) || ! $push_merge_info) { | |
901 | $_merge_info = populate_merge_info($d, $gs, | |
902 | $uuid, | |
903 | $linear_refs, | |
904 | $rewritten_parent); | |
905 | } | |
906 | ||
d7ad3bed | 907 | my %ed_opts = ( r => $last_rev, |
61395354 | 908 | log => get_commit_entry($d)->{log}, |
ba24e745 | 909 | ra => Git::SVN::Ra->new($url), |
3caf320b KA |
910 | config => SVN::Core::config_get_config( |
911 | $Git::SVN::Ra::config_dir | |
912 | ), | |
61395354 EW |
913 | tree_a => "$d~1", |
914 | tree_b => $d, | |
915 | editor_cb => sub { | |
916 | print "Committed r$_[0]\n"; | |
751eb395 EW |
917 | $cmt_rev = $_[0]; |
918 | }, | |
6abd9332 | 919 | mergeinfo => $_merge_info, |
a8ae2623 | 920 | svn_path => ''); |
61395354 | 921 | if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) { |
d7ad3bed | 922 | print "No changes\n$d~1 == $d\n"; |
733a65aa | 923 | } elsif ($parents->{$d} && @{$parents->{$d}}) { |
751eb395 | 924 | $gs->{inject_parents_dcommit}->{$cmt_rev} = |
733a65aa | 925 | $parents->{$d}; |
d7ad3bed | 926 | } |
751eb395 | 927 | $_fetch_all ? $gs->fetch_all : $gs->fetch; |
7dfa16b9 | 928 | $last_rev = $cmt_rev; |
751eb395 EW |
929 | next if $_no_rebase; |
930 | ||
931 | # we always want to rebase against the current HEAD, | |
932 | # not any head that was passed to us | |
c74d9acf | 933 | my @diff = command('diff-tree', $d, |
751eb395 EW |
934 | $gs->refname, '--'); |
935 | my @finish; | |
936 | if (@diff) { | |
937 | @finish = rebase_cmd(); | |
c74d9acf | 938 | print STDERR "W: $d and ", $gs->refname, |
751eb395 | 939 | " differ, using @finish:\n", |
c74d9acf | 940 | join("\n", @diff), "\n"; |
751eb395 EW |
941 | } else { |
942 | print "No changes between current HEAD and ", | |
943 | $gs->refname, | |
944 | "\nResetting to the latest ", | |
945 | $gs->refname, "\n"; | |
946 | @finish = qw/reset --mixed/; | |
947 | } | |
948 | command_noisy(@finish, $gs->refname); | |
1e5814f3 BJ |
949 | |
950 | $rewritten_parent = command_oneline(qw/rev-parse HEAD/); | |
951 | ||
c74d9acf EW |
952 | if (@diff) { |
953 | @refs = (); | |
954 | my ($url_, $rev_, $uuid_, $gs_) = | |
5eec27e3 | 955 | working_head_info('HEAD', \@refs); |
c74d9acf EW |
956 | my ($linear_refs_, $parents_) = |
957 | linearize_history($gs_, \@refs); | |
958 | if (scalar(@$linear_refs) != | |
959 | scalar(@$linear_refs_)) { | |
960 | fatal "# of revisions changed ", | |
961 | "\nbefore:\n", | |
962 | join("\n", @$linear_refs), | |
963 | "\n\nafter:\n", | |
964 | join("\n", @$linear_refs_), "\n", | |
965 | 'If you are attempting to commit ', | |
966 | "merges, try running:\n\t", | |
967 | 'git rebase --interactive', | |
968 | '--preserve-merges ', | |
969 | $gs->refname, | |
970 | "\nBefore dcommitting"; | |
971 | } | |
711521e2 | 972 | if ($url_ ne $expect_url) { |
c03c1f79 AG |
973 | if ($url_ eq $gs->metadata_url) { |
974 | ||
975 | "Accepting rewritten URL:", | |
976 | " $url_\n"; | |
977 | } else { | |
978 | fatal | |
979 | "URL mismatch after rebase:", | |
980 | " $url_ != $expect_url"; | |
981 | } | |
c74d9acf EW |
982 | } |
983 | if ($uuid_ ne $uuid) { | |
984 | fatal "uuid mismatch after rebase: ", | |
985 | "$uuid_ != $uuid"; | |
986 | } | |
987 | # remap parents | |
988 | my (%p, @l, $i); | |
989 | for ($i = 0; $i < scalar @$linear_refs; $i++) { | |
990 | my $new = $linear_refs_->[$i] or next; | |
991 | $p{$new} = | |
992 | $parents->{$linear_refs->[$i]}; | |
993 | push @l, $new; | |
994 | } | |
995 | $parents = \%p; | |
996 | $linear_refs = \@l; | |
997 | } | |
b22d4497 EW |
998 | } |
999 | } | |
5eec27e3 TR |
1000 | |
1001 | if ($old_head) { | |
1002 | my $new_head = command_oneline(qw/rev-parse HEAD/); | |
1003 | my $new_is_symbolic = eval { | |
1004 | command_oneline(qw/symbolic-ref -q HEAD/); | |
1005 | }; | |
1006 | if ($new_is_symbolic) { | |
1007 | print "dcommitted the branch ", $head, "\n"; | |
1008 | } else { | |
1009 | print "dcommitted on a detached HEAD because you gave ", | |
1010 | "a revision argument.\n", | |
1011 | "The rewritten commit is: ", $new_head, "\n"; | |
1012 | } | |
1013 | command(['checkout', $old_head], STDERR => 0); | |
1014 | } | |
1015 | ||
3157dd9e | 1016 | unlink $gs->{index}; |
b22d4497 EW |
1017 | } |
1018 | ||
5de70efb FR |
1019 | sub cmd_branch { |
1020 | my ($branch_name, $head) = @_; | |
1021 | ||
1022 | unless (defined $branch_name && length $branch_name) { | |
1023 | die(($_tag ? "tag" : "branch") . " name required\n"); | |
1024 | } | |
1025 | $head ||= 'HEAD'; | |
1026 | ||
150d38c4 | 1027 | my (undef, $rev, undef, $gs) = working_head_info($head); |
12a296bc | 1028 | my $src = $gs->full_pushurl; |
5de70efb | 1029 | |
a0fbc87c | 1030 | my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}}; |
62244069 MB |
1031 | my $allglobs = $remote->{ $_tag ? 'tags' : 'branches' }; |
1032 | my $glob; | |
1033 | if ($#{$allglobs} == 0) { | |
1034 | $glob = $allglobs->[0]; | |
1035 | } else { | |
1036 | unless(defined $_branch_dest) { | |
1037 | die "Multiple ", | |
1038 | $_tag ? "tag" : "branch", | |
1039 | " paths defined for Subversion repository.\n", | |
1040 | "You must specify where you want to create the ", | |
1041 | $_tag ? "tag" : "branch", | |
1042 | " with the --destination argument.\n"; | |
1043 | } | |
1044 | foreach my $g (@{$allglobs}) { | |
f7050599 EW |
1045 | # SVN::Git::Editor could probably be moved to Git.pm.. |
1046 | my $re = SVN::Git::Editor::glob2pat($g->{path}->{left}); | |
1047 | if ($_branch_dest =~ /$re/) { | |
62244069 MB |
1048 | $glob = $g; |
1049 | last; | |
1050 | } | |
1051 | } | |
1052 | unless (defined $glob) { | |
eaa14ff8 EW |
1053 | my $dest_re = qr/\b\Q$_branch_dest\E\b/; |
1054 | foreach my $g (@{$allglobs}) { | |
1055 | $g->{path}->{left} =~ /$dest_re/ or next; | |
1056 | if (defined $glob) { | |
1057 | die "Ambiguous destination: ", | |
1058 | $_branch_dest, "\nmatches both '", | |
1059 | $glob->{path}->{left}, "' and '", | |
1060 | $g->{path}->{left}, "'\n"; | |
1061 | } | |
1062 | $glob = $g; | |
1063 | } | |
1064 | unless (defined $glob) { | |
1065 | die "Unknown ", | |
1066 | $_tag ? "tag" : "branch", | |
1067 | " destination $_branch_dest\n"; | |
1068 | } | |
62244069 MB |
1069 | } |
1070 | } | |
5de70efb | 1071 | my ($lft, $rgt) = @{ $glob->{path} }{qw/left right/}; |
99bacd6c IM |
1072 | my $url; |
1073 | if (defined $_commit_url) { | |
1074 | $url = $_commit_url; | |
1075 | } else { | |
1076 | $url = eval { command_oneline('config', '--get', | |
1077 | "svn-remote.$gs->{repo_id}.commiturl") }; | |
1078 | if (!$url) { | |
12a296bc | 1079 | $url = $remote->{pushurl} || $remote->{url}; |
99bacd6c IM |
1080 | } |
1081 | } | |
1082 | my $dst = join '/', $url, $lft, $branch_name, ($rgt || ()); | |
5de70efb | 1083 | |
a83b91e7 IM |
1084 | if ($dst =~ /^https:/ && $src =~ /^http:/) { |
1085 | $src=~s/^http:/https:/; | |
1086 | } | |
1087 | ||
d32fad2b | 1088 | ::_req_svn(); |
1089 | ||
5de70efb FR |
1090 | my $ctx = SVN::Client->new( |
1091 | auth => Git::SVN::Ra::_auth_providers(), | |
1092 | log_msg => sub { | |
1093 | ${ $_[0] } = defined $_message | |
1094 | ? $_message | |
1095 | : 'Create ' . ($_tag ? 'tag ' : 'branch ' ) | |
1096 | . $branch_name; | |
1097 | }, | |
1098 | ); | |
1099 | ||
1100 | eval { | |
1101 | $ctx->ls($dst, 'HEAD', 0); | |
1102 | } and die "branch ${branch_name} already exists\n"; | |
1103 | ||
1104 | print "Copying ${src} at r${rev} to ${dst}...\n"; | |
1105 | $ctx->copy($src, $rev, $dst) | |
1106 | unless $_dry_run; | |
1107 | ||
1108 | $gs->fetch_all; | |
1109 | } | |
1110 | ||
26e60160 | 1111 | sub cmd_find_rev { |
ea14e6c5 MAL |
1112 | my $revision_or_hash = shift or die "SVN or git revision required ", |
1113 | "as a command-line argument\n"; | |
26e60160 AR |
1114 | my $result; |
1115 | if ($revision_or_hash =~ /^r\d+$/) { | |
b3cb7e45 AR |
1116 | my $head = shift; |
1117 | $head ||= 'HEAD'; | |
1118 | my @refs; | |
63c56022 | 1119 | my (undef, undef, $uuid, $gs) = working_head_info($head, \@refs); |
b3cb7e45 AR |
1120 | unless ($gs) { |
1121 | die "Unable to determine upstream SVN information from ", | |
1122 | "$head history\n"; | |
26e60160 | 1123 | } |
b3cb7e45 | 1124 | my $desired_revision = substr($revision_or_hash, 1); |
63c56022 | 1125 | $result = $gs->rev_map_get($desired_revision, $uuid); |
26e60160 AR |
1126 | } else { |
1127 | my (undef, $rev, undef) = cmt_metadata($revision_or_hash); | |
1128 | $result = $rev; | |
1129 | } | |
1130 | print "$result\n" if $result; | |
1131 | } | |
1132 | ||
55f9d7a7 MH |
1133 | sub auto_create_empty_directories { |
1134 | my ($gs) = @_; | |
1135 | my $var = eval { command_oneline('config', '--get', '--bool', | |
1136 | "svn-remote.$gs->{repo_id}.automkdirs") }; | |
1137 | # By default, create empty directories by consulting the unhandled log, | |
1138 | # but allow setting it to 'false' to skip it. | |
1139 | return !($var && $var eq 'false'); | |
1140 | } | |
1141 | ||
905f8b7d EW |
1142 | sub cmd_rebase { |
1143 | command_noisy(qw/update-index --refresh/); | |
13c823fb EW |
1144 | my ($url, $rev, $uuid, $gs) = working_head_info('HEAD'); |
1145 | unless ($gs) { | |
905f8b7d EW |
1146 | die "Unable to determine upstream SVN information from ", |
1147 | "working tree history\n"; | |
1148 | } | |
7d45e146 SF |
1149 | if ($_dry_run) { |
1150 | print "Remote Branch: " . $gs->refname . "\n"; | |
1151 | print "SVN URL: " . $url . "\n"; | |
1152 | return; | |
1153 | } | |
905f8b7d EW |
1154 | if (command(qw/diff-index HEAD --/)) { |
1155 | print STDERR "Cannot rebase with uncommited changes:\n"; | |
1156 | command_noisy('status'); | |
1157 | exit 1; | |
1158 | } | |
dee41f3e | 1159 | unless ($_local) { |
cec0d5a3 SG |
1160 | # rebase will checkout for us, so no need to do it explicitly |
1161 | $_no_checkout = 'true'; | |
dee41f3e EW |
1162 | $_fetch_all ? $gs->fetch_all : $gs->fetch; |
1163 | } | |
905f8b7d | 1164 | command_noisy(rebase_cmd(), $gs->refname); |
55f9d7a7 MH |
1165 | if (auto_create_empty_directories($gs)) { |
1166 | $gs->mkemptydirs; | |
1167 | } | |
905f8b7d EW |
1168 | } |
1169 | ||
5969cbe1 | 1170 | sub cmd_show_ignore { |
13c823fb EW |
1171 | my ($url, $rev, $uuid, $gs) = working_head_info('HEAD'); |
1172 | $gs ||= Git::SVN->new; | |
5969cbe1 | 1173 | my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum); |
01bdab84 BS |
1174 | $gs->prop_walk($gs->{path}, $r, sub { |
1175 | my ($gs, $path, $props) = @_; | |
1176 | print STDOUT "\n# $path\n"; | |
1177 | my $s = $props->{'svn:ignore'} or return; | |
1178 | $s =~ s/[\r\n]+/\n/g; | |
a7d72544 | 1179 | $s =~ s/^\n+//; |
01bdab84 BS |
1180 | chomp $s; |
1181 | $s =~ s#^#$path#gm; | |
1182 | print STDOUT "$s\n"; | |
1183 | }); | |
a5e0cedc EW |
1184 | } |
1185 | ||
2d879792 VK |
1186 | sub cmd_show_externals { |
1187 | my ($url, $rev, $uuid, $gs) = working_head_info('HEAD'); | |
1188 | $gs ||= Git::SVN->new; | |
1189 | my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum); | |
1190 | $gs->prop_walk($gs->{path}, $r, sub { | |
1191 | my ($gs, $path, $props) = @_; | |
1192 | print STDOUT "\n# $path\n"; | |
1193 | my $s = $props->{'svn:externals'} or return; | |
1194 | $s =~ s/[\r\n]+/\n/g; | |
1195 | chomp $s; | |
1196 | $s =~ s#^#$path#gm; | |
1197 | print STDOUT "$s\n"; | |
1198 | }); | |
1199 | } | |
1200 | ||
d05ddec5 BS |
1201 | sub cmd_create_ignore { |
1202 | my ($url, $rev, $uuid, $gs) = working_head_info('HEAD'); | |
1203 | $gs ||= Git::SVN->new; | |
1204 | my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum); | |
1205 | $gs->prop_walk($gs->{path}, $r, sub { | |
1206 | my ($gs, $path, $props) = @_; | |
1207 | # $path is of the form /path/to/dir/ | |
7d9fd459 BG |
1208 | $path = '.' . $path; |
1209 | # SVN can have attributes on empty directories, | |
1210 | # which git won't track | |
1211 | mkpath([$path]) unless -d $path; | |
1212 | my $ignore = $path . '.gitignore'; | |
d05ddec5 BS |
1213 | my $s = $props->{'svn:ignore'} or return; |
1214 | open(GITIGNORE, '>', $ignore) | |
207f1a75 | 1215 | or fatal("Failed to open `$ignore' for writing: $!"); |
d05ddec5 | 1216 | $s =~ s/[\r\n]+/\n/g; |
a7d72544 | 1217 | $s =~ s/^\n+//; |
d05ddec5 BS |
1218 | chomp $s; |
1219 | # Prefix all patterns so that the ignore doesn't apply | |
1220 | # to sub-directories. | |
1221 | $s =~ s#^#/#gm; | |
1222 | print GITIGNORE "$s\n"; | |
1223 | close(GITIGNORE) | |
207f1a75 | 1224 | or fatal("Failed to close `$ignore': $!"); |
c4c66b26 | 1225 | command_noisy('add', '-f', $ignore); |
d05ddec5 BS |
1226 | }); |
1227 | } | |
1228 | ||
6111b934 EW |
1229 | sub cmd_mkdirs { |
1230 | my ($url, $rev, $uuid, $gs) = working_head_info('HEAD'); | |
1231 | $gs ||= Git::SVN->new; | |
1232 | $gs->mkemptydirs($_revision); | |
1233 | } | |
1234 | ||
b2b3ada7 DK |
1235 | sub canonicalize_path { |
1236 | my ($path) = @_; | |
e6fefa92 DK |
1237 | my $dot_slash_added = 0; |
1238 | if (substr($path, 0, 1) ne "/") { | |
1239 | $path = "./" . $path; | |
1240 | $dot_slash_added = 1; | |
1241 | } | |
b2b3ada7 DK |
1242 | # File::Spec->canonpath doesn't collapse x/../y into y (for a |
1243 | # good reason), so let's do this manually. | |
1244 | $path =~ s#/+#/#g; | |
1245 | $path =~ s#/\.(?:/|$)#/#g; | |
1246 | $path =~ s#/[^/]+/\.\.##g; | |
1247 | $path =~ s#/$##g; | |
e6fefa92 | 1248 | $path =~ s#^\./## if $dot_slash_added; |
2fe403e7 GP |
1249 | $path =~ s#^/##; |
1250 | $path =~ s#^\.$##; | |
b2b3ada7 DK |
1251 | return $path; |
1252 | } | |
1253 | ||
50ff2366 UD |
1254 | sub canonicalize_url { |
1255 | my ($url) = @_; | |
1256 | $url =~ s#^([^:]+://[^/]*/)(.*)$#$1 . canonicalize_path($2)#e; | |
1257 | return $url; | |
1258 | } | |
1259 | ||
15153451 BS |
1260 | # get_svnprops(PATH) |
1261 | # ------------------ | |
51e057cf | 1262 | # Helper for cmd_propget and cmd_proplist below. |
15153451 BS |
1263 | sub get_svnprops { |
1264 | my $path = shift; | |
1265 | my ($url, $rev, $uuid, $gs) = working_head_info('HEAD'); | |
1266 | $gs ||= Git::SVN->new; | |
1267 | ||
1268 | # prefix THE PATH by the sub-directory from which the user | |
1269 | # invoked us. | |
1270 | $path = $cmd_dir_prefix . $path; | |
207f1a75 | 1271 | fatal("No such file or directory: $path") unless -e $path; |
15153451 BS |
1272 | my $is_dir = -d $path ? 1 : 0; |
1273 | $path = $gs->{path} . '/' . $path; | |
1274 | ||
1275 | # canonicalize the path (otherwise libsvn will abort or fail to | |
1276 | # find the file) | |
b2b3ada7 | 1277 | $path = canonicalize_path($path); |
15153451 BS |
1278 | |
1279 | my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum); | |
1280 | my $props; | |
1281 | if ($is_dir) { | |
1282 | (undef, undef, $props) = $gs->ra->get_dir($path, $r); | |
1283 | } | |
1284 | else { | |
1285 | (undef, $props) = $gs->ra->get_file($path, $r, undef); | |
1286 | } | |
1287 | return $props; | |
1288 | } | |
1289 | ||
1290 | # cmd_propget (PROP, PATH) | |
1291 | # ------------------------ | |
1292 | # Print the SVN property PROP for PATH. | |
1293 | sub cmd_propget { | |
1294 | my ($prop, $path) = @_; | |
1295 | $path = '.' if not defined $path; | |
1296 | usage(1) if not defined $prop; | |
1297 | my $props = get_svnprops($path); | |
1298 | if (not defined $props->{$prop}) { | |
207f1a75 | 1299 | fatal("`$path' does not have a `$prop' SVN property."); |
15153451 BS |
1300 | } |
1301 | print $props->{$prop} . "\n"; | |
1302 | } | |
1303 | ||
51e057cf BS |
1304 | # cmd_proplist (PATH) |
1305 | # ------------------- | |
1306 | # Print the list of SVN properties for PATH. | |
1307 | sub cmd_proplist { | |
1308 | my $path = shift; | |
1309 | $path = '.' if not defined $path; | |
1310 | my $props = get_svnprops($path); | |
1311 | print "Properties on '$path':\n"; | |
1312 | foreach (sort keys %{$props}) { | |
1313 | print " $_\n"; | |
1314 | } | |
1315 | } | |
1316 | ||
8164b652 | 1317 | sub cmd_multi_init { |
9d55b41a | 1318 | my $url = shift; |
62244069 | 1319 | unless (defined $_trunk || @_branches || @_tags) { |
98327e58 | 1320 | usage(1); |
9d55b41a | 1321 | } |
dc431666 | 1322 | |
8164b652 | 1323 | $_prefix = '' unless defined $_prefix; |
dadc6d2a | 1324 | if (defined $url) { |
50ff2366 | 1325 | $url = canonicalize_url($url); |
dadc6d2a EW |
1326 | init_subdir(@_); |
1327 | } | |
f30603fc | 1328 | do_git_init_db(); |
98327e58 | 1329 | if (defined $_trunk) { |
b4b33600 | 1330 | $_trunk =~ s#^/+##; |
6f5748e1 | 1331 | my $trunk_ref = 'refs/remotes/' . $_prefix . 'trunk'; |
706587fc EW |
1332 | # try both old-style and new-style lookups: |
1333 | my $gs_trunk = eval { Git::SVN->new($trunk_ref) }; | |
8164b652 | 1334 | unless ($gs_trunk) { |
706587fc EW |
1335 | my ($trunk_url, $trunk_path) = |
1336 | complete_svn_url($url, $_trunk); | |
1337 | $gs_trunk = Git::SVN->init($trunk_url, $trunk_path, | |
1338 | undef, $trunk_ref); | |
98327e58 | 1339 | } |
c35b96e7 | 1340 | } |
62244069 | 1341 | return unless @_branches || @_tags; |
e7db67e6 | 1342 | my $ra = $url ? Git::SVN::Ra->new($url) : undef; |
62244069 MB |
1343 | foreach my $path (@_branches) { |
1344 | complete_url_ls_init($ra, $path, '--branches/-b', $_prefix); | |
1345 | } | |
1346 | foreach my $path (@_tags) { | |
1347 | complete_url_ls_init($ra, $path, '--tags/-t', $_prefix.'tags/'); | |
1348 | } | |
9d55b41a EW |
1349 | } |
1350 | ||
1c8443b0 | 1351 | sub cmd_multi_fetch { |
4d0157d6 | 1352 | $Git::SVN::no_reuse_existing = undef; |
0af9c9f9 EW |
1353 | my $remotes = Git::SVN::read_all_remotes(); |
1354 | foreach my $repo_id (sort keys %$remotes) { | |
db03cd24 | 1355 | if ($remotes->{$repo_id}->{url}) { |
4bb9ed04 EW |
1356 | Git::SVN::fetch_all($repo_id, $remotes); |
1357 | } | |
9d55b41a | 1358 | } |
9d55b41a EW |
1359 | } |
1360 | ||
44320b9e EW |
1361 | # this command is special because it requires no metadata |
1362 | sub cmd_commit_diff { | |
1363 | my ($ta, $tb, $url) = @_; | |
1364 | my $usage = "Usage: $0 commit-diff -r<revision> ". | |
207f1a75 | 1365 | "<tree-ish> <tree-ish> [<URL>]"; |
44320b9e | 1366 | fatal($usage) if (!defined $ta || !defined $tb); |
d72ab8c8 | 1367 | my $svn_path = ''; |
44320b9e EW |
1368 | if (!defined $url) { |
1369 | my $gs = eval { Git::SVN->new }; | |
1370 | if (!$gs) { | |
1371 | fatal("Needed URL or usable git-svn --id in ", | |
1372 | "the command-line\n", $usage); | |
1373 | } | |
1374 | $url = $gs->{url}; | |
d3a840dc | 1375 | $svn_path = $gs->{path}; |
44320b9e EW |
1376 | } |
1377 | unless (defined $_revision) { | |
1378 | fatal("-r|--revision is a required argument\n", $usage); | |
1379 | } | |
1380 | if (defined $_message && defined $_file) { | |
1381 | fatal("Both --message/-m and --file/-F specified ", | |
1382 | "for the commit message.\n", | |
207f1a75 | 1383 | "I have no idea what you mean"); |
44320b9e EW |
1384 | } |
1385 | if (defined $_file) { | |
1386 | $_message = file_to_s($_file); | |
1387 | } else { | |
1388 | $_message ||= get_commit_entry($tb)->{log}; | |
1389 | } | |
1390 | my $ra ||= Git::SVN::Ra->new($url); | |
1391 | my $r = $_revision; | |
1392 | if ($r eq 'HEAD') { | |
1393 | $r = $ra->get_latest_revnum; | |
1394 | } elsif ($r !~ /^\d+$/) { | |
1395 | die "revision argument: $r not understood by git-svn\n"; | |
1396 | } | |
61395354 EW |
1397 | my %ed_opts = ( r => $r, |
1398 | log => $_message, | |
1399 | ra => $ra, | |
1400 | tree_a => $ta, | |
1401 | tree_b => $tb, | |
1402 | editor_cb => sub { print "Committed r$_[0]\n" }, | |
1403 | svn_path => $svn_path ); | |
1404 | if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) { | |
44320b9e EW |
1405 | print "No changes\n$ta == $tb\n"; |
1406 | } | |
44320b9e EW |
1407 | } |
1408 | ||
05427b91 TR |
1409 | sub escape_uri_only { |
1410 | my ($uri) = @_; | |
1411 | my @tmp; | |
1412 | foreach (split m{/}, $uri) { | |
6a004d3f | 1413 | s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg; |
05427b91 TR |
1414 | push @tmp, $_; |
1415 | } | |
1416 | join('/', @tmp); | |
1417 | } | |
1418 | ||
1419 | sub escape_url { | |
1420 | my ($url) = @_; | |
1421 | if ($url =~ m#^([^:]+)://([^/]*)(.*)$#) { | |
1422 | my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3)); | |
1423 | $url = "$scheme://$domain$uri"; | |
1424 | } | |
1425 | $url; | |
1426 | } | |
1427 | ||
e6fefa92 | 1428 | sub cmd_info { |
bd2d4f96 | 1429 | my $path = canonicalize_path(defined($_[0]) ? $_[0] : "."); |
edde9112 | 1430 | my $fullpath = canonicalize_path($cmd_dir_prefix . $path); |
bd2d4f96 | 1431 | if (exists $_[1]) { |
e6fefa92 DK |
1432 | die "Too many arguments specified\n"; |
1433 | } | |
1434 | ||
1435 | my ($file_type, $diff_status) = find_file_type_and_diff_status($path); | |
1436 | ||
1437 | if (!$file_type && !$diff_status) { | |
2cf3e3ac TR |
1438 | print STDERR "svn: '$path' is not under version control\n"; |
1439 | exit 1; | |
e6fefa92 DK |
1440 | } |
1441 | ||
1442 | my ($url, $rev, $uuid, $gs) = working_head_info('HEAD'); | |
1443 | unless ($gs) { | |
1444 | die "Unable to determine upstream SVN information from ", | |
1445 | "working tree history\n"; | |
1446 | } | |
bd2d4f96 EW |
1447 | |
1448 | # canonicalize_path() will return "" to make libsvn 1.5.x happy, | |
1449 | $path = "." if $path eq ""; | |
1450 | ||
edde9112 | 1451 | my $full_url = $url . ($fullpath eq "" ? "" : "/$fullpath"); |
e6fefa92 | 1452 | |
8b014d71 | 1453 | if ($_url) { |
05427b91 | 1454 | print escape_url($full_url), "\n"; |
8b014d71 DK |
1455 | return; |
1456 | } | |
1457 | ||
e6fefa92 DK |
1458 | my $result = "Path: $path\n"; |
1459 | $result .= "Name: " . basename($path) . "\n" if $file_type ne "dir"; | |
05427b91 | 1460 | $result .= "URL: " . escape_url($full_url) . "\n"; |
e6fefa92 | 1461 | |
a5460eb7 EW |
1462 | eval { |
1463 | my $repos_root = $gs->repos_root; | |
1464 | Git::SVN::remove_username($repos_root); | |
05427b91 | 1465 | $result .= "Repository Root: " . escape_url($repos_root) . "\n"; |
a5460eb7 EW |
1466 | }; |
1467 | if ($@) { | |
1468 | $result .= "Repository Root: (offline)\n"; | |
1469 | } | |
b91a8a3e | 1470 | ::_req_svn(); |
22ba47f5 MK |
1471 | $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A" && |
1472 | ($SVN::Core::VERSION le '1.5.4' || $file_type ne "dir"); | |
e6fefa92 DK |
1473 | $result .= "Revision: " . ($diff_status eq "A" ? 0 : $rev) . "\n"; |
1474 | ||
1475 | $result .= "Node Kind: " . | |
1476 | ($file_type eq "dir" ? "directory" : "file") . "\n"; | |
1477 | ||
1478 | my $schedule = $diff_status eq "A" | |
1479 | ? "add" | |
1480 | : ($diff_status eq "D" ? "delete" : "normal"); | |
1481 | $result .= "Schedule: $schedule\n"; | |
1482 | ||
1483 | if ($diff_status eq "A") { | |
1484 | print $result, "\n"; | |
1485 | return; | |
1486 | } | |
1487 | ||
1488 | my ($lc_author, $lc_rev, $lc_date_utc); | |
edde9112 | 1489 | my @args = Git::SVN::Log::git_svn_log_cmd($rev, $rev, "--", $fullpath); |
e6fefa92 DK |
1490 | my $log = command_output_pipe(@args); |
1491 | my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/; | |
1492 | while (<$log>) { | |
1493 | if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) { | |
1494 | $lc_author = $1; | |
1495 | $lc_date_utc = Git::SVN::Log::parse_git_date($2, $3); | |
1496 | } elsif (/^${esc_color} (git-svn-id:.+)$/o) { | |
1497 | (undef, $lc_rev, undef) = ::extract_metadata($1); | |
1498 | } | |
1499 | } | |
1500 | close $log; | |
1501 | ||
1502 | Git::SVN::Log::set_local_timezone(); | |
1503 | ||
1504 | $result .= "Last Changed Author: $lc_author\n"; | |
1505 | $result .= "Last Changed Rev: $lc_rev\n"; | |
1506 | $result .= "Last Changed Date: " . | |
1507 | Git::SVN::Log::format_svn_date($lc_date_utc) . "\n"; | |
1508 | ||
1509 | if ($file_type ne "dir") { | |
1510 | my $text_last_updated_date = | |
1511 | ($diff_status eq "D" ? $lc_date_utc : (stat $path)[9]); | |
1512 | $result .= | |
1513 | "Text Last Updated: " . | |
1514 | Git::SVN::Log::format_svn_date($text_last_updated_date) . | |
1515 | "\n"; | |
1516 | my $checksum; | |
1517 | if ($diff_status eq "D") { | |
1518 | my ($fh, $ctx) = | |
1519 | command_output_pipe(qw(cat-file blob), "HEAD:$path"); | |
1520 | if ($file_type eq "link") { | |
1521 | my $file_name = <$fh>; | |
8d7c4fad | 1522 | $checksum = md5sum("link $file_name"); |
e6fefa92 | 1523 | } else { |
8d7c4fad | 1524 | $checksum = md5sum($fh); |
e6fefa92 DK |
1525 | } |
1526 | command_close_pipe($fh, $ctx); | |
1527 | } elsif ($file_type eq "link") { | |
1528 | my $file_name = | |
1529 | command(qw(cat-file blob), "HEAD:$path"); | |
1530 | $checksum = | |
8d7c4fad | 1531 | md5sum("link " . $file_name); |
e6fefa92 DK |
1532 | } else { |
1533 | open FILE, "<", $path or die $!; | |
8d7c4fad | 1534 | $checksum = md5sum(\*FILE); |
e6fefa92 DK |
1535 | close FILE or die $!; |
1536 | } | |
1537 | $result .= "Checksum: " . $checksum . "\n"; | |
1538 | } | |
1539 | ||
1540 | print $result, "\n"; | |
1541 | } | |
1542 | ||
195643f2 BJ |
1543 | sub cmd_reset { |
1544 | my $target = shift || $_revision or die "SVN revision required\n"; | |
1545 | $target = $1 if $target =~ /^r(\d+)$/; | |
1546 | $target =~ /^\d+$/ or die "Numeric SVN revision expected\n"; | |
1547 | my ($url, $rev, $uuid, $gs) = working_head_info('HEAD'); | |
1548 | unless ($gs) { | |
1549 | die "Unable to determine upstream SVN information from ". | |
1550 | "history\n"; | |
1551 | } | |
1552 | my ($r, $c) = $gs->find_rev_before($target, not $_fetch_parent); | |
70ee0b77 | 1553 | die "Cannot find SVN revision $target\n" unless defined($c); |
195643f2 BJ |
1554 | $gs->rev_map_set($r, $c, 'reset', $uuid); |
1555 | print "r$r = $c ($gs->{ref_id})\n"; | |
1556 | } | |
1557 | ||
2da9ee08 RZ |
1558 | sub cmd_gc { |
1559 | if (!$can_compress) { | |
1560 | warn "Compress::Zlib could not be found; unhandled.log " . | |
1561 | "files will not be compressed.\n"; | |
1562 | } | |
1563 | find({ wanted => \&gc_directory, no_chdir => 1}, "$ENV{GIT_DIR}/svn"); | |
1564 | } | |
1565 | ||
3397f9df EW |
1566 | ########################### utility functions ######################### |
1567 | ||
905f8b7d EW |
1568 | sub rebase_cmd { |
1569 | my @cmd = qw/rebase/; | |
1570 | push @cmd, '-v' if $_verbose; | |
1571 | push @cmd, qw/--merge/ if $_merge; | |
1572 | push @cmd, "--strategy=$_strategy" if $_strategy; | |
1573 | @cmd; | |
1574 | } | |
1575 | ||
1e889ef3 EW |
1576 | sub post_fetch_checkout { |
1577 | return if $_no_checkout; | |
1578 | my $gs = $Git::SVN::_head or return; | |
1579 | return if verify_ref('refs/heads/master^0'); | |
1580 | ||
b186a261 EW |
1581 | # look for "trunk" ref if it exists |
1582 | my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}}; | |
1583 | my $fetch = $remote->{fetch}; | |
1584 | if ($fetch) { | |
1585 | foreach my $p (keys %$fetch) { | |
1586 | basename($fetch->{$p}) eq 'trunk' or next; | |
1587 | $gs = Git::SVN->new($fetch->{$p}, $gs->{repo_id}, $p); | |
1588 | last; | |
1589 | } | |
1590 | } | |
1591 | ||
1e889ef3 EW |
1592 | my $valid_head = verify_ref('HEAD^0'); |
1593 | command_noisy(qw(update-ref refs/heads/master), $gs->refname); | |
1594 | return if ($valid_head || !verify_ref('HEAD^0')); | |
1595 | ||
1596 | return if $ENV{GIT_DIR} !~ m#^(?:.*/)?\.git$#; | |
1597 | my $index = $ENV{GIT_INDEX_FILE} || "$ENV{GIT_DIR}/index"; | |
1598 | return if -f $index; | |
1599 | ||
7ae3df8c | 1600 | return if command_oneline(qw/rev-parse --is-inside-work-tree/) eq 'false'; |
1e889ef3 EW |
1601 | return if command_oneline(qw/rev-parse --is-inside-git-dir/) eq 'true'; |
1602 | command_noisy(qw/read-tree -m -u -v HEAD HEAD/); | |
1603 | print STDERR "Checked out HEAD:\n ", | |
1604 | $gs->full_url, " r", $gs->last_rev, "\n"; | |
55f9d7a7 MH |
1605 | if (auto_create_empty_directories($gs)) { |
1606 | $gs->mkemptydirs($gs->last_rev); | |
1607 | } | |
1e889ef3 EW |
1608 | } |
1609 | ||
98327e58 EW |
1610 | sub complete_svn_url { |
1611 | my ($url, $path) = @_; | |
1612 | $path =~ s#/+$##; | |
98327e58 | 1613 | if ($path !~ m#^[a-z\+]+://#) { |
98327e58 EW |
1614 | if (!defined $url || $url !~ m#^[a-z\+]+://#) { |
1615 | fatal("E: '$path' is not a complete URL ", | |
207f1a75 | 1616 | "and a separate URL is not specified"); |
98327e58 | 1617 | } |
706587fc | 1618 | return ($url, $path); |
98327e58 | 1619 | } |
706587fc | 1620 | return ($path, ''); |
98327e58 EW |
1621 | } |
1622 | ||
9d55b41a | 1623 | sub complete_url_ls_init { |
706587fc EW |
1624 | my ($ra, $repo_path, $switch, $pfx) = @_; |
1625 | unless ($repo_path) { | |
9d55b41a EW |
1626 | print STDERR "W: $switch not specified\n"; |
1627 | return; | |
1628 | } | |
706587fc EW |
1629 | $repo_path =~ s#/+$##; |
1630 | if ($repo_path =~ m#^[a-z\+]+://#) { | |
1631 | $ra = Git::SVN::Ra->new($repo_path); | |
1632 | $repo_path = ''; | |
e7db67e6 | 1633 | } else { |
706587fc | 1634 | $repo_path =~ s#^/+##; |
e7db67e6 | 1635 | unless ($ra) { |
706587fc | 1636 | fatal("E: '$repo_path' is not a complete URL ", |
207f1a75 | 1637 | "and a separate URL is not specified"); |
8164b652 | 1638 | } |
e7db67e6 | 1639 | } |
706587fc | 1640 | my $url = $ra->{url}; |
b4d57e5e EW |
1641 | my $gs = Git::SVN->init($url, undef, undef, undef, 1); |
1642 | my $k = "svn-remote.$gs->{repo_id}.url"; | |
1643 | my $orig_url = eval { command_oneline(qw/config --get/, $k) }; | |
1644 | if ($orig_url && ($orig_url ne $gs->{url})) { | |
1645 | die "$k already set: $orig_url\n", | |
1646 | "wanted to set to: $gs->{url}\n"; | |
88cf4107 | 1647 | } |
b4d57e5e | 1648 | command_oneline('config', $k, $gs->{url}) unless $orig_url; |
0b2af457 | 1649 | my $remote_path = "$gs->{path}/$repo_path"; |
5268f9ed | 1650 | $remote_path =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg; |
b4d57e5e EW |
1651 | $remote_path =~ s#/+#/#g; |
1652 | $remote_path =~ s#^/##g; | |
ed0b9d43 | 1653 | $remote_path .= "/*" if $remote_path !~ /\*/; |
b4d57e5e EW |
1654 | my ($n) = ($switch =~ /^--(\w+)/); |
1655 | if (length $pfx && $pfx !~ m#/$#) { | |
1656 | die "--prefix='$pfx' must have a trailing slash '/'\n"; | |
9d55b41a | 1657 | } |
570d35c2 | 1658 | command_noisy('config', |
62244069 | 1659 | '--add', |
570d35c2 MG |
1660 | "svn-remote.$gs->{repo_id}.$n", |
1661 | "$remote_path:refs/remotes/$pfx*" . | |
1662 | ('/*' x (($remote_path =~ tr/*/*/) - 1)) ); | |
9d55b41a EW |
1663 | } |
1664 | ||
aef4e921 EW |
1665 | sub verify_ref { |
1666 | my ($ref) = @_; | |
2c5c1d53 EW |
1667 | eval { command_oneline([ 'rev-parse', '--verify', $ref ], |
1668 | { STDERR => 0 }); }; | |
aef4e921 EW |
1669 | } |
1670 | ||
a5e0cedc | 1671 | sub get_tree_from_treeish { |
cf52b8f0 | 1672 | my ($treeish) = @_; |
44320b9e | 1673 | # $treeish can be a symbolic ref, too: |
aef4e921 | 1674 | my $type = command_oneline(qw/cat-file -t/, $treeish); |
cf52b8f0 EW |
1675 | my $expected; |
1676 | while ($type eq 'tag') { | |
aef4e921 | 1677 | ($treeish, $type) = command(qw/cat-file tag/, $treeish); |
cf52b8f0 EW |
1678 | } |
1679 | if ($type eq 'commit') { | |
aef4e921 EW |
1680 | $expected = (grep /^tree /, command(qw/cat-file commit/, |
1681 | $treeish))[0]; | |
44320b9e | 1682 | ($expected) = ($expected =~ /^tree ($sha1)$/o); |
cf52b8f0 EW |
1683 | die "Unable to get tree from $treeish\n" unless $expected; |
1684 | } elsif ($type eq 'tree') { | |
1685 | $expected = $treeish; | |
1686 | } else { | |
1687 | die "$treeish is a $type, expected tree, tag or commit\n"; | |
1688 | } | |
a5e0cedc EW |
1689 | return $expected; |
1690 | } | |
1691 | ||
44320b9e EW |
1692 | sub get_commit_entry { |
1693 | my ($treeish) = shift; | |
1694 | my %log_entry = ( log => '', tree => get_tree_from_treeish($treeish) ); | |
1695 | my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG"; | |
1696 | my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG"; | |
1697 | open my $log_fh, '>', $commit_editmsg or croak $!; | |
3397f9df | 1698 | |
44320b9e | 1699 | my $type = command_oneline(qw/cat-file -t/, $treeish); |
4ad4515d | 1700 | if ($type eq 'commit' || $type eq 'tag') { |
aef4e921 | 1701 | my ($msg_fh, $ctx) = command_output_pipe('cat-file', |
44320b9e | 1702 | $type, $treeish); |
3397f9df | 1703 | my $in_msg = 0; |
6aa9ba14 AP |
1704 | my $author; |
1705 | my $saw_from = 0; | |
328eb9b3 | 1706 | my $msgbuf = ""; |
3397f9df EW |
1707 | while (<$msg_fh>) { |
1708 | if (!$in_msg) { | |
1709 | $in_msg = 1 if (/^\s*$/); | |
6aa9ba14 | 1710 | $author = $1 if (/^author (.*>)/); |
df746c5a | 1711 | } elsif (/^git-svn-id: /) { |
44320b9e EW |
1712 | # skip this for now, we regenerate the |
1713 | # correct one on re-fetch anyways | |
1714 | # TODO: set *:merge properties or like... | |
3397f9df | 1715 | } else { |
6aa9ba14 AP |
1716 | if (/^From:/ || /^Signed-off-by:/) { |
1717 | $saw_from = 1; | |
1718 | } | |
328eb9b3 | 1719 | $msgbuf .= $_; |
3397f9df EW |
1720 | } |
1721 | } | |
328eb9b3 | 1722 | $msgbuf =~ s/\s+$//s; |
6aa9ba14 AP |
1723 | if ($Git::SVN::_add_author_from && defined($author) |
1724 | && !$saw_from) { | |
328eb9b3 | 1725 | $msgbuf .= "\n\nFrom: $author"; |
6aa9ba14 | 1726 | } |
328eb9b3 | 1727 | print $log_fh $msgbuf or croak $!; |
aef4e921 | 1728 | command_close_pipe($msg_fh, $ctx); |
3397f9df | 1729 | } |
44320b9e | 1730 | close $log_fh or croak $!; |
3397f9df EW |
1731 | |
1732 | if ($_edit || ($type eq 'tree')) { | |
b4479f07 JN |
1733 | chomp(my $editor = command_oneline(qw(var GIT_EDITOR))); |
1734 | system('sh', '-c', $editor.' "$@"', $editor, $commit_editmsg); | |
3397f9df | 1735 | } |
44320b9e | 1736 | rename $commit_editmsg, $commit_msg or croak $!; |
16fc08e2 | 1737 | { |
b510df8a | 1738 | require Encode; |
16fc08e2 EW |
1739 | # SVN requires messages to be UTF-8 when entering the repo |
1740 | local $/; | |
1741 | open $log_fh, '<', $commit_msg or croak $!; | |
1742 | binmode $log_fh; | |
1743 | chomp($log_entry{log} = <$log_fh>); | |
1744 | ||
b510df8a EW |
1745 | my $enc = Git::config('i18n.commitencoding') || 'UTF-8'; |
1746 | my $msg = $log_entry{log}; | |
1747 | ||
1748 | eval { $msg = Encode::decode($enc, $msg, 1) }; | |
1749 | if ($@) { | |
1750 | die "Could not decode as $enc:\n", $msg, | |
1751 | "\nPerhaps you need to set i18n.commitencoding\n"; | |
16fc08e2 | 1752 | } |
b510df8a EW |
1753 | |
1754 | eval { $msg = Encode::encode('UTF-8', $msg, 1) }; | |
1755 | die "Could not encode as UTF-8:\n$msg\n" if $@; | |
1756 | ||
1757 | $log_entry{log} = $msg; | |
1758 | ||
16fc08e2 EW |
1759 | close $log_fh or croak $!; |
1760 | } | |
44320b9e EW |
1761 | unlink $commit_msg; |
1762 | \%log_entry; | |
a5e0cedc EW |
1763 | } |
1764 | ||
3397f9df EW |
1765 | sub s_to_file { |
1766 | my ($str, $file, $mode) = @_; | |
1767 | open my $fd,'>',$file or croak $!; | |
1768 | print $fd $str,"\n" or croak $!; | |
1769 | close $fd or croak $!; | |
1770 | chmod ($mode &~ umask, $file) if (defined $mode); | |
1771 | } | |
1772 | ||
1773 | sub file_to_s { | |
1774 | my $file = shift; | |
1775 | open my $fd,'<',$file or croak "$!: file: $file\n"; | |
1776 | local $/; | |
1777 | my $ret = <$fd>; | |
1778 | close $fd or croak $!; | |
1779 | $ret =~ s/\s*$//s; | |
1780 | return $ret; | |
1781 | } | |
1782 | ||
eeb0abe0 EW |
1783 | # '<svn username> = real-name <email address>' mapping based on git-svnimport: |
1784 | sub load_authors { | |
1785 | open my $authors, '<', $_authors or die "Can't open $_authors $!\n"; | |
f8c9d1d2 | 1786 | my $log = $cmd eq 'log'; |
eeb0abe0 EW |
1787 | while (<$authors>) { |
1788 | chomp; | |
575d025c | 1789 | next unless /^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/; |
eeb0abe0 | 1790 | my ($user, $name, $email) = ($1, $2, $3); |
f8c9d1d2 EW |
1791 | if ($log) { |
1792 | $Git::SVN::Log::rusers{"$name <$email>"} = $user; | |
1793 | } else { | |
1794 | $users{$user} = [$name, $email]; | |
1795 | } | |
79bb8d88 EW |
1796 | } |
1797 | close $authors or croak $!; | |
1798 | } | |
1799 | ||
e0d10e1c | 1800 | # convert GetOpt::Long specs for use by git-config |
1a30582b | 1801 | sub read_git_config { |
b8c92cad | 1802 | my $opts = shift; |
97ae0911 | 1803 | my @config_only; |
b8c92cad | 1804 | foreach my $o (keys %$opts) { |
97ae0911 EW |
1805 | # if we have mixedCase and a long option-only, then |
1806 | # it's a config-only variable that we don't need for | |
1807 | # the command-line. | |
1808 | push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i); | |
b8c92cad | 1809 | my $v = $opts->{$o}; |
97ae0911 | 1810 | my ($key) = ($o =~ /^([a-zA-Z\-]+)/); |
b8c92cad | 1811 | $key =~ s/-//g; |
225f1d0c | 1812 | my $arg = 'git config'; |
b8c92cad EW |
1813 | $arg .= ' --int' if ($o =~ /[:=]i$/); |
1814 | $arg .= ' --bool' if ($o !~ /[:=][sfi]$/); | |
1815 | if (ref $v eq 'ARRAY') { | |
1816 | chomp(my @tmp = `$arg --get-all svn.$key`); | |
1817 | @$v = @tmp if @tmp; | |
1818 | } else { | |
1819 | chomp(my $tmp = `$arg --get svn.$key`); | |
7774284a | 1820 | if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) { |
b8c92cad EW |
1821 | $$v = $tmp; |
1822 | } | |
1823 | } | |
1824 | } | |
97ae0911 | 1825 | delete @$opts{@config_only} if @config_only; |
b8c92cad EW |
1826 | } |
1827 | ||
79bb8d88 | 1828 | sub extract_metadata { |
c1927a85 | 1829 | my $id = shift or return (undef, undef, undef); |
3dfab993 | 1830 | my ($url, $rev, $uuid) = ($id =~ /^\s*git-svn-id:\s+(.*)\@(\d+) |
b3e95936 | 1831 | \s([a-f\d\-]+)$/ix); |
e70dc780 | 1832 | if (!defined $rev || !$uuid || !$url) { |
79bb8d88 | 1833 | # some of the original repositories I made had |
82e5a82f | 1834 | # identifiers like this: |
b3e95936 | 1835 | ($rev, $uuid) = ($id =~/^\s*git-svn-id:\s(\d+)\@([a-f\d\-]+)/i); |
79bb8d88 EW |
1836 | } |
1837 | return ($url, $rev, $uuid); | |
1838 | } | |
1839 | ||
c1927a85 EW |
1840 | sub cmt_metadata { |
1841 | return extract_metadata((grep(/^git-svn-id: /, | |
aef4e921 | 1842 | command(qw/cat-file commit/, shift)))[-1]); |
c1927a85 EW |
1843 | } |
1844 | ||
6ea42032 BB |
1845 | sub cmt_sha2rev_batch { |
1846 | my %s2r; | |
1847 | my ($pid, $in, $out, $ctx) = command_bidi_pipe(qw/cat-file --batch/); | |
1848 | my $list = shift; | |
1849 | ||
1850 | foreach my $sha (@{$list}) { | |
1851 | my $first = 1; | |
1852 | my $size = 0; | |
1853 | print $out $sha, "\n"; | |
1854 | ||
1855 | while (my $line = <$in>) { | |
1856 | if ($first && $line =~ /^[[:xdigit:]]{40}\smissing$/) { | |
1857 | last; | |
1858 | } elsif ($first && | |
1859 | $line =~ /^[[:xdigit:]]{40}\scommit\s(\d+)$/) { | |
1860 | $first = 0; | |
1861 | $size = $1; | |
1862 | next; | |
1863 | } elsif ($line =~ /^(git-svn-id: )/) { | |
1864 | my (undef, $rev, undef) = | |
1865 | extract_metadata($line); | |
1866 | $s2r{$sha} = $rev; | |
1867 | } | |
1868 | ||
1869 | $size -= length($line); | |
1870 | last if ($size == 0); | |
1871 | } | |
1872 | } | |
1873 | ||
1874 | command_close_bidi_pipe($pid, $in, $out, $ctx); | |
1875 | ||
1876 | return \%s2r; | |
1877 | } | |
1878 | ||
905f8b7d EW |
1879 | sub working_head_info { |
1880 | my ($head, $refs) = @_; | |
83cf21f9 | 1881 | my @args = qw/rev-list --first-parent --pretty=medium/; |
05b4df31 | 1882 | my ($fh, $ctx) = command_output_pipe(@args, $head); |
3dfab993 | 1883 | my $hash; |
40cb8f8f | 1884 | my %max; |
3dfab993 SV |
1885 | while (<$fh>) { |
1886 | if ( m{^commit ($::sha1)$} ) { | |
1887 | unshift @$refs, $hash if $hash and $refs; | |
1888 | $hash = $1; | |
1889 | next; | |
1890 | } | |
1891 | next unless s{^\s*(git-svn-id:)}{$1}; | |
1892 | my ($url, $rev, $uuid) = extract_metadata($_); | |
13c823fb | 1893 | if (defined $url && defined $rev) { |
40cb8f8f | 1894 | next if $max{$url} and $max{$url} < $rev; |
13c823fb | 1895 | if (my $gs = Git::SVN->find_by_url($url)) { |
63c56022 | 1896 | my $c = $gs->rev_map_get($rev, $uuid); |
b03c7a63 | 1897 | if ($c && $c eq $hash) { |
13c823fb EW |
1898 | close $fh; # break the pipe |
1899 | return ($url, $rev, $uuid, $gs); | |
40cb8f8f | 1900 | } else { |
060610c5 | 1901 | $max{$url} ||= $gs->rev_map_max; |
13c823fb EW |
1902 | } |
1903 | } | |
1904 | } | |
905f8b7d | 1905 | } |
13c823fb EW |
1906 | command_close_pipe($fh, $ctx); |
1907 | (undef, undef, undef, undef); | |
905f8b7d EW |
1908 | } |
1909 | ||
733a65aa EW |
1910 | sub read_commit_parents { |
1911 | my ($parents, $c) = @_; | |
7b02b85a EW |
1912 | chomp(my $p = command_oneline(qw/rev-list --parents -1/, $c)); |
1913 | $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n"; | |
1914 | @{$parents->{$c}} = split(/ /, $p); | |
733a65aa EW |
1915 | } |
1916 | ||
1917 | sub linearize_history { | |
1918 | my ($gs, $refs) = @_; | |
1919 | my %parents; | |
1920 | foreach my $c (@$refs) { | |
1921 | read_commit_parents(\%parents, $c); | |
1922 | } | |
1923 | ||
1924 | my @linear_refs; | |
1925 | my %skip = (); | |
1926 | my $last_svn_commit = $gs->last_commit; | |
1927 | foreach my $c (reverse @$refs) { | |
1928 | next if $c eq $last_svn_commit; | |
1929 | last if $skip{$c}; | |
1930 | ||
1931 | unshift @linear_refs, $c; | |
1932 | $skip{$c} = 1; | |
1933 | ||
1934 | # we only want the first parent to diff against for linear | |
1935 | # history, we save the rest to inject when we finalize the | |
1936 | # svn commit | |
1937 | my $fp_a = verify_ref("$c~1"); | |
1938 | my $fp_b = shift @{$parents{$c}} if $parents{$c}; | |
1939 | if (!$fp_a || !$fp_b) { | |
1940 | die "Commit $c\n", | |
1941 | "has no parent commit, and therefore ", | |
1942 | "nothing to diff against.\n", | |
1943 | "You should be working from a repository ", | |
1944 | "originally created by git-svn\n"; | |
1945 | } | |
1946 | if ($fp_a ne $fp_b) { | |
1947 | die "$c~1 = $fp_a, however parsing commit $c ", | |
1948 | "revealed that:\n$c~1 = $fp_b\nBUG!\n"; | |
1949 | } | |
1950 | ||
1951 | foreach my $p (@{$parents{$c}}) { | |
1952 | $skip{$p} = 1; | |
1953 | } | |
1954 | } | |
1955 | (\@linear_refs, \%parents); | |
1956 | } | |
1957 | ||
e6fefa92 DK |
1958 | sub find_file_type_and_diff_status { |
1959 | my ($path) = @_; | |
107cee50 | 1960 | return ('dir', '') if $path eq ''; |
e6fefa92 DK |
1961 | |
1962 | my $diff_output = | |
1963 | command_oneline(qw(diff --cached --name-status --), $path) || ""; | |
1964 | my $diff_status = (split(' ', $diff_output))[0] || ""; | |
1965 | ||
1966 | my $ls_tree = command_oneline(qw(ls-tree HEAD), $path) || ""; | |
1967 | ||
1968 | return (undef, undef) if !$diff_status && !$ls_tree; | |
1969 | ||
1970 | if ($diff_status eq "A") { | |
1971 | return ("link", $diff_status) if -l $path; | |
1972 | return ("dir", $diff_status) if -d $path; | |
1973 | return ("file", $diff_status); | |
1974 | } | |
1975 | ||
1976 | my $mode = (split(' ', $ls_tree))[0] || ""; | |
1977 | ||
1978 | return ("link", $diff_status) if $mode eq "120000"; | |
1979 | return ("dir", $diff_status) if $mode eq "040000"; | |
1980 | return ("file", $diff_status); | |
1981 | } | |
1982 | ||
b2b3ada7 DK |
1983 | sub md5sum { |
1984 | my $arg = shift; | |
1985 | my $ref = ref $arg; | |
1986 | my $md5 = Digest::MD5->new(); | |
0b19138b | 1987 | if ($ref eq 'GLOB' || $ref eq 'IO::File' || $ref eq 'File::Temp') { |
b2b3ada7 DK |
1988 | $md5->addfile($arg) or croak $!; |
1989 | } elsif ($ref eq 'SCALAR') { | |
1990 | $md5->add($$arg) or croak $!; | |
1991 | } elsif (!$ref) { | |
1992 | $md5->add($arg) or croak $!; | |
1993 | } else { | |
1994 | ::fatal "Can't provide MD5 hash for unknown ref type: '", $ref, "'"; | |
1995 | } | |
1996 | return $md5->hexdigest(); | |
1997 | } | |
1998 | ||
2da9ee08 RZ |
1999 | sub gc_directory { |
2000 | if ($can_compress && -f $_ && basename($_) eq "unhandled.log") { | |
2001 | my $out_filename = $_ . ".gz"; | |
2002 | open my $in_fh, "<", $_ or die "Unable to open $_: $!\n"; | |
2003 | binmode $in_fh; | |
2004 | my $gz = Compress::Zlib::gzopen($out_filename, "ab") or | |
2005 | die "Unable to open $out_filename: $!\n"; | |
2006 | ||
2007 | my $res; | |
2008 | while ($res = sysread($in_fh, my $str, 1024)) { | |
2009 | $gz->gzwrite($str) or | |
2010 | die "Unable to write: ".$gz->gzerror()."!\n"; | |
2011 | } | |
2012 | unlink $_ or die "unlink $File::Find::name: $!\n"; | |
2013 | } elsif (-f $_ && basename($_) eq "index") { | |
2014 | unlink $_ or die "unlink $_: $!\n"; | |
2015 | } | |
2016 | } | |
2017 | ||
9b981fc6 EW |
2018 | package Git::SVN; |
2019 | use strict; | |
2020 | use warnings; | |
060610c5 EW |
2021 | use Fcntl qw/:DEFAULT :seek/; |
2022 | use constant rev_map_fmt => 'NH40'; | |
ecc712dd | 2023 | use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent |
62e349d2 | 2024 | $_repack $_repack_flags $_use_svm_props $_head |
70ae04e4 | 2025 | $_use_svnsync_props $no_reuse_existing $_minimize_url |
e82f0d73 | 2026 | $_use_log_author $_add_author_from $_localtime/; |
9b981fc6 EW |
2027 | use Carp qw/croak/; |
2028 | use File::Path qw/mkpath/; | |
373274f9 | 2029 | use File::Copy qw/copy/; |
9b981fc6 | 2030 | use IPC::Open3; |
6aa17fc6 | 2031 | use Time::Local; |
7d944c33 | 2032 | use Memoize; # core since 5.8.0, Jul 2002 |
8bff7c53 | 2033 | use Memoize::Storable; |
9b981fc6 | 2034 | |
94bc914c KW |
2035 | my ($_gc_nr, $_gc_period); |
2036 | ||
9b981fc6 EW |
2037 | # properties that we do not log: |
2038 | my %SKIP_PROP; | |
2039 | BEGIN { | |
2040 | %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url | |
2041 | svn:special svn:executable | |
2042 | svn:entry:committed-rev | |
2043 | svn:entry:last-author | |
2044 | svn:entry:uuid | |
2045 | svn:entry:committed-date/; | |
91b03282 EW |
2046 | |
2047 | # some options are read globally, but can be overridden locally | |
2048 | # per [svn-remote "..."] section. Command-line options will *NOT* | |
2049 | # override options set in an [svn-remote "..."] section | |
c5f71ad0 SV |
2050 | no strict 'refs'; |
2051 | for my $option (qw/follow_parent no_metadata use_svm_props | |
2052 | use_svnsync_props/) { | |
2053 | my $key = $option; | |
91b03282 | 2054 | $key =~ tr/_//d; |
c5f71ad0 SV |
2055 | my $prop = "-$option"; |
2056 | *$option = sub { | |
2057 | my ($self) = @_; | |
2058 | return $self->{$prop} if exists $self->{$prop}; | |
2059 | my $k = "svn-remote.$self->{repo_id}.$key"; | |
2060 | eval { command_oneline(qw/config --get/, $k) }; | |
2061 | if ($@) { | |
2062 | $self->{$prop} = ${"Git::SVN::_$option"}; | |
91b03282 | 2063 | } else { |
c5f71ad0 SV |
2064 | my $v = command_oneline(qw/config --bool/,$k); |
2065 | $self->{$prop} = $v eq 'false' ? 0 : 1; | |
91b03282 | 2066 | } |
c5f71ad0 SV |
2067 | return $self->{$prop}; |
2068 | } | |
91b03282 | 2069 | } |
9b981fc6 EW |
2070 | } |
2071 | ||
0b19138b | 2072 | |
321b1842 EW |
2073 | my (%LOCKFILES, %INDEX_FILES); |
2074 | END { | |
2075 | unlink keys %LOCKFILES if %LOCKFILES; | |
2076 | unlink keys %INDEX_FILES if %INDEX_FILES; | |
2077 | } | |
373274f9 | 2078 | |
4bb9ed04 EW |
2079 | sub resolve_local_globs { |
2080 | my ($url, $fetch, $glob_spec) = @_; | |
2081 | return unless defined $glob_spec; | |
2082 | my $ref = $glob_spec->{ref}; | |
2083 | my $path = $glob_spec->{path}; | |
6f5748e1 AB |
2084 | foreach (command(qw#for-each-ref --format=%(refname) refs/#)) { |
2085 | next unless m#^$ref->{regex}$#; | |
4bb9ed04 | 2086 | my $p = $1; |
bf655fd7 RE |
2087 | my $pathname = desanitize_refname($path->full_path($p)); |
2088 | my $refname = desanitize_refname($ref->full_path($p)); | |
4bb9ed04 EW |
2089 | if (my $existing = $fetch->{$pathname}) { |
2090 | if ($existing ne $refname) { | |
2091 | die "Refspec conflict:\n", | |
6f5748e1 AB |
2092 | "existing: $existing\n", |
2093 | " globbed: $refname\n"; | |
4bb9ed04 | 2094 | } |
6f5748e1 | 2095 | my $u = (::cmt_metadata("$refname"))[0]; |
4e9f6cc7 | 2096 | $u =~ s!^\Q$url\E(/|$)!! or die |
6f5748e1 | 2097 | "$refname: '$url' not found in '$u'\n"; |
4bb9ed04 EW |
2098 | if ($pathname ne $u) { |
2099 | warn "W: Refspec glob conflict ", | |
6f5748e1 | 2100 | "(ref: $refname):\n", |
4bb9ed04 EW |
2101 | "expected path: $pathname\n", |
2102 | " real path: $u\n", | |
2103 | "Continuing ahead with $u\n"; | |
2104 | next; | |
2105 | } | |
2106 | } else { | |
4bb9ed04 EW |
2107 | $fetch->{$pathname} = $refname; |
2108 | } | |
2109 | } | |
2110 | } | |
2111 | ||
e98671e5 EW |
2112 | sub parse_revision_argument { |
2113 | my ($base, $head) = @_; | |
2114 | if (!defined $::_revision || $::_revision eq 'BASE:HEAD') { | |
2115 | return ($base, $head); | |
2116 | } | |
2117 | return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/); | |
2118 | return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/); | |
2119 | return ($head, $head) if ($::_revision eq 'HEAD'); | |
2120 | return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/); | |
2121 | return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/); | |
2122 | die "revision argument: $::_revision not understood by git-svn\n"; | |
2123 | } | |
2124 | ||
0af9c9f9 | 2125 | sub fetch_all { |
4bb9ed04 | 2126 | my ($repo_id, $remotes) = @_; |
905f8b7d EW |
2127 | if (ref $repo_id) { |
2128 | my $gs = $repo_id; | |
2129 | $repo_id = undef; | |
2130 | $repo_id = $gs->{repo_id}; | |
2131 | } | |
2132 | $remotes ||= read_all_remotes(); | |
7447b4bc EW |
2133 | my $remote = $remotes->{$repo_id} or |
2134 | die "[svn-remote \"$repo_id\"] unknown\n"; | |
e518192f | 2135 | my $fetch = $remote->{fetch}; |
7447b4bc | 2136 | my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n"; |
e518192f | 2137 | my (@gs, @globs); |
0af9c9f9 | 2138 | my $ra = Git::SVN::Ra->new($url); |
26a62d57 | 2139 | my $uuid = $ra->get_uuid; |
0af9c9f9 | 2140 | my $head = $ra->get_latest_revnum; |
577e9fca EW |
2141 | |
2142 | # ignore errors, $head revision may not even exist anymore | |
2143 | eval { $ra->get_log("", $head, 0, 1, 0, 1, sub { $head = $_[1] }) }; | |
2144 | warn "W: $@\n" if $@; | |
2145 | ||
28710f74 | 2146 | my $base = defined $fetch ? $head : 0; |
e518192f EW |
2147 | |
2148 | # read the max revs for wildcard expansion (branches/*, tags/*) | |
2149 | foreach my $t (qw/branches tags/) { | |
2150 | defined $remote->{$t} or next; | |
62244069 MB |
2151 | push @globs, @{$remote->{$t}}; |
2152 | ||
93f2689c EW |
2153 | my $max_rev = eval { tmp_config(qw/--int --get/, |
2154 | "svn-remote.$repo_id.${t}-maxRev") }; | |
2155 | if (defined $max_rev && ($max_rev < $base)) { | |
2156 | $base = $max_rev; | |
d6d3346b EW |
2157 | } elsif (!defined $max_rev) { |
2158 | $base = 0; | |
e518192f EW |
2159 | } |
2160 | } | |
2161 | ||
db03cd24 EW |
2162 | if ($fetch) { |
2163 | foreach my $p (sort keys %$fetch) { | |
2164 | my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p); | |
060610c5 | 2165 | my $lr = $gs->rev_map_max; |
db03cd24 EW |
2166 | if (defined $lr) { |
2167 | $base = $lr if ($lr < $base); | |
2168 | } | |
2169 | push @gs, $gs; | |
0af9c9f9 | 2170 | } |
0af9c9f9 | 2171 | } |
e98671e5 EW |
2172 | |
2173 | ($base, $head) = parse_revision_argument($base, $head); | |
e518192f | 2174 | $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs); |
0af9c9f9 EW |
2175 | } |
2176 | ||
47e39c55 EW |
2177 | sub read_all_remotes { |
2178 | my $r = {}; | |
63c56022 JA |
2179 | my $use_svm_props = eval { command_oneline(qw/config --bool |
2180 | svn.useSvmProps/) }; | |
2181 | $use_svm_props = $use_svm_props eq 'true' if $use_svm_props; | |
ffd5c8e4 | 2182 | my $svn_refspec = qr{\s*(.*?)\s*:\s*(.+?)\s*}; |
8b8fc068 | 2183 | foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) { |
6f5748e1 AB |
2184 | if (m!^(.+)\.fetch=$svn_refspec$!) { |
2185 | my ($remote, $local_ref, $remote_ref) = ($1, $2, $3); | |
2186 | die("svn-remote.$remote: remote ref '$remote_ref' " | |
2187 | . "must start with 'refs/'\n") | |
2188 | unless $remote_ref =~ m{^refs/}; | |
46cb16fb | 2189 | $local_ref = uri_decode($local_ref); |
46cf98ba | 2190 | $r->{$remote}->{fetch}->{$local_ref} = $remote_ref; |
63c56022 JA |
2191 | $r->{$remote}->{svm} = {} if $use_svm_props; |
2192 | } elsif (m!^(.+)\.usesvmprops=\s*(.*)\s*$!) { | |
2193 | $r->{$1}->{svm} = {}; | |
47e39c55 EW |
2194 | } elsif (m!^(.+)\.url=\s*(.*)\s*$!) { |
2195 | $r->{$1}->{url} = $2; | |
12a296bc AS |
2196 | } elsif (m!^(.+)\.pushurl=\s*(.*)\s*$!) { |
2197 | $r->{$1}->{pushurl} = $2; | |
cdb51a13 MO |
2198 | } elsif (m!^(.+)\.ignore-refs=\s*(.*)\s*$!) { |
2199 | $r->{$1}->{ignore_refs_regex} = $2; | |
6f5748e1 AB |
2200 | } elsif (m!^(.+)\.(branches|tags)=$svn_refspec$!) { |
2201 | my ($remote, $t, $local_ref, $remote_ref) = | |
2202 | ($1, $2, $3, $4); | |
2203 | die("svn-remote.$remote: remote ref '$remote_ref' ($t) " | |
2204 | . "must start with 'refs/'\n") | |
2205 | unless $remote_ref =~ m{^refs/}; | |
46cb16fb | 2206 | $local_ref = uri_decode($local_ref); |
62244069 | 2207 | my $rs = { |
6f5748e1 AB |
2208 | t => $t, |
2209 | remote => $remote, | |
07576208 JS |
2210 | path => Git::SVN::GlobSpec->new($local_ref, 1), |
2211 | ref => Git::SVN::GlobSpec->new($remote_ref, 0) }; | |
4bb9ed04 EW |
2212 | if (length($rs->{ref}->{right}) != 0) { |
2213 | die "The '*' glob character must be the last ", | |
6f5748e1 | 2214 | "character of '$remote_ref'\n"; |
4bb9ed04 | 2215 | } |
6f5748e1 | 2216 | push @{ $r->{$remote}->{$t} }, $rs; |
47e39c55 EW |
2217 | } |
2218 | } | |
63c56022 JA |
2219 | |
2220 | map { | |
2221 | if (defined $r->{$_}->{svm}) { | |
2222 | my $svm; | |
2223 | eval { | |
2224 | my $section = "svn-remote.$_"; | |
2225 | $svm = { | |
2226 | source => tmp_config('--get', | |
2227 | "$section.svm-source"), | |
2228 | replace => tmp_config('--get', | |
2229 | "$section.svm-replace"), | |
2230 | } | |
2231 | }; | |
2232 | $r->{$_}->{svm} = $svm; | |
2233 | } | |
2234 | } keys %$r; | |
2235 | ||
cdb51a13 MO |
2236 | foreach my $remote (keys %$r) { |
2237 | foreach ( grep { defined $_ } | |
2238 | map { $r->{$remote}->{$_} } qw(branches tags) ) { | |
2239 | foreach my $rs ( @$_ ) { | |
2240 | $rs->{ignore_refs_regex} = | |
2241 | $r->{$remote}->{ignore_refs_regex}; | |
2242 | } | |
2243 | } | |
2244 | } | |
2245 | ||
47e39c55 EW |
2246 | $r; |
2247 | } | |
2248 | ||
ecc712dd | 2249 | sub init_vars { |
94bc914c | 2250 | $_gc_nr = $_gc_period = 1000; |
af788a6e KW |
2251 | if (defined $_repack || defined $_repack_flags) { |
2252 | warn "Repack options are obsolete; they have no effect.\n"; | |
2253 | } | |
ecc712dd EW |
2254 | } |
2255 | ||
b805b44a | 2256 | sub verify_remotes_sanity { |
536c4b09 | 2257 | return unless -d $ENV{GIT_DIR}; |
b805b44a EW |
2258 | my %seen; |
2259 | foreach (command(qw/config -l/)) { | |
2260 | if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) { | |
2261 | if ($seen{$1}) { | |
2262 | die "Remote ref refs/remote/$1 is tracked by", | |
2263 | "\n \"$_\"\nand\n \"$seen{$1}\"\n", | |
2264 | "Please resolve this ambiguity in ", | |
2265 | "your git configuration file before ", | |
2266 | "continuing\n"; | |
2267 | } | |
2268 | $seen{$1} = $_; | |
2269 | } | |
2270 | } | |
2271 | } | |
2272 | ||
e6434f87 EW |
2273 | sub find_existing_remote { |
2274 | my ($url, $remotes) = @_; | |
befc9adc | 2275 | return undef if $no_reuse_existing; |
e6434f87 EW |
2276 | my $existing; |
2277 | foreach my $repo_id (keys %$remotes) { | |
2278 | my $u = $remotes->{$repo_id}->{url} or next; | |
2279 | next if $u ne $url; | |
2280 | $existing = $repo_id; | |
2281 | last; | |
2282 | } | |
2283 | $existing; | |
2284 | } | |
b805b44a | 2285 | |
e6434f87 | 2286 | sub init_remote_config { |
d8115c51 | 2287 | my ($self, $url, $no_write) = @_; |
e6434f87 EW |
2288 | $url =~ s!/+$!!; # strip trailing slash |
2289 | my $r = read_all_remotes(); | |
2290 | my $existing = find_existing_remote($url, $r); | |
2291 | if ($existing) { | |
e518192f EW |
2292 | unless ($no_write) { |
2293 | print STDERR "Using existing ", | |
2294 | "[svn-remote \"$existing\"]\n"; | |
2295 | } | |
e6434f87 | 2296 | $self->{repo_id} = $existing; |
4a1bb4c3 | 2297 | } elsif ($_minimize_url) { |
e6434f87 EW |
2298 | my $min_url = Git::SVN::Ra->new($url)->minimize_url; |
2299 | $existing = find_existing_remote($min_url, $r); | |
2300 | if ($existing) { | |
e518192f EW |
2301 | unless ($no_write) { |
2302 | print STDERR "Using existing ", | |
2303 | "[svn-remote \"$existing\"]\n"; | |
2304 | } | |
e6434f87 EW |
2305 | $self->{repo_id} = $existing; |
2306 | } | |
2307 | if ($min_url ne $url) { | |
e518192f EW |
2308 | unless ($no_write) { |
2309 | print STDERR "Using higher level of URL: ", | |
2310 | "$url => $min_url\n"; | |
2311 | } | |
e6434f87 EW |
2312 | my $old_path = $self->{path}; |
2313 | $self->{path} = $url; | |
4e9f6cc7 | 2314 | $self->{path} =~ s!^\Q$min_url\E(/|$)!!; |
e6434f87 EW |
2315 | if (length $old_path) { |
2316 | $self->{path} .= "/$old_path"; | |
2317 | } | |
2318 | $url = $min_url; | |
2319 | } | |
2320 | } | |
2321 | my $orig_url; | |
2322 | if (!$existing) { | |
b805b44a | 2323 | # verify that we aren't overwriting anything: |
e6434f87 | 2324 | $orig_url = eval { |
706587fc | 2325 | command_oneline('config', '--get', |
e6434f87 | 2326 | "svn-remote.$self->{repo_id}.url") |
706587fc | 2327 | }; |
b805b44a | 2328 | if ($orig_url && ($orig_url ne $url)) { |
e6434f87 | 2329 | die "svn-remote.$self->{repo_id}.url already set: ", |
b805b44a EW |
2330 | "$orig_url\nwanted to set to: $url\n"; |
2331 | } | |
9b981fc6 | 2332 | } |
e6434f87 | 2333 | my ($xrepo_id, $xpath) = find_ref($self->refname); |
6f5748e1 | 2334 | if (!$no_write && defined $xpath) { |
e6434f87 | 2335 | die "svn-remote.$xrepo_id.fetch already set to track ", |
6f5748e1 | 2336 | "$xpath:", $self->refname, "\n"; |
e6434f87 | 2337 | } |
d8115c51 EW |
2338 | unless ($no_write) { |
2339 | command_noisy('config', | |
2340 | "svn-remote.$self->{repo_id}.url", $url); | |
46cf98ba | 2341 | $self->{path} =~ s{^/}{}; |
5268f9ed | 2342 | $self->{path} =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg; |
d8115c51 EW |
2343 | command_noisy('config', '--add', |
2344 | "svn-remote.$self->{repo_id}.fetch", | |
2345 | "$self->{path}:".$self->refname); | |
2346 | } | |
9b981fc6 | 2347 | $self->{url} = $url; |
e6434f87 EW |
2348 | } |
2349 | ||
a8ae2623 EW |
2350 | sub find_by_url { # repos_root and, path are optional |
2351 | my ($class, $full_url, $repos_root, $path) = @_; | |
56973d20 | 2352 | |
1a97a506 | 2353 | return undef unless defined $full_url; |
56973d20 AR |
2354 | remove_username($full_url); |
2355 | remove_username($repos_root) if defined $repos_root; | |
a8ae2623 EW |
2356 | my $remotes = read_all_remotes(); |
2357 | if (defined $full_url && defined $repos_root && !defined $path) { | |
2358 | $path = $full_url; | |
2359 | $path =~ s#^\Q$repos_root\E(?:/|$)##; | |
2360 | } | |
2361 | foreach my $repo_id (keys %$remotes) { | |
2362 | my $u = $remotes->{$repo_id}->{url} or next; | |
56973d20 | 2363 | remove_username($u); |
a8ae2623 EW |
2364 | next if defined $repos_root && $repos_root ne $u; |
2365 | ||
2366 | my $fetch = $remotes->{$repo_id}->{fetch} || {}; | |
62244069 MB |
2367 | foreach my $t (qw/branches tags/) { |
2368 | foreach my $globspec (@{$remotes->{$repo_id}->{$t}}) { | |
2369 | resolve_local_globs($u, $fetch, $globspec); | |
2370 | } | |
a8ae2623 EW |
2371 | } |
2372 | my $p = $path; | |
0bb91d9a | 2373 | my $rwr = rewrite_root({repo_id => $repo_id}); |
63c56022 JA |
2374 | my $svm = $remotes->{$repo_id}->{svm} |
2375 | if defined $remotes->{$repo_id}->{svm}; | |
a8ae2623 EW |
2376 | unless (defined $p) { |
2377 | $p = $full_url; | |
0bb91d9a | 2378 | my $z = $u; |
63c56022 | 2379 | my $prefix = ''; |
0bb91d9a JG |
2380 | if ($rwr) { |
2381 | $z = $rwr; | |
1b7e543a | 2382 | remove_username($z); |
63c56022 JA |
2383 | } elsif (defined $svm) { |
2384 | $z = $svm->{source}; | |
2385 | $prefix = $svm->{replace}; | |
2386 | $prefix =~ s#^\Q$u\E(?:/|$)##; | |
2387 | $prefix =~ s#/$##; | |
0bb91d9a | 2388 | } |
63c56022 | 2389 | $p =~ s#^\Q$z\E(?:/|$)#$prefix# or next; |
a8ae2623 EW |
2390 | } |
2391 | foreach my $f (keys %$fetch) { | |
2392 | next if $f ne $p; | |
2393 | return Git::SVN->new($fetch->{$f}, $repo_id, $f); | |
2394 | } | |
2395 | } | |
2396 | undef; | |
2397 | } | |
2398 | ||
e6434f87 | 2399 | sub init { |
d8115c51 | 2400 | my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_; |
e6434f87 EW |
2401 | my $self = _new($class, $repo_id, $ref_id, $path); |
2402 | if (defined $url) { | |
d8115c51 | 2403 | $self->init_remote_config($url, $no_write); |
e6434f87 | 2404 | } |
9b981fc6 EW |
2405 | $self; |
2406 | } | |
2407 | ||
706587fc EW |
2408 | sub find_ref { |
2409 | my ($ref_id) = @_; | |
2410 | foreach (command(qw/config -l/)) { | |
2411 | next unless m!^svn-remote\.(.+)\.fetch= | |
ffd5c8e4 | 2412 | \s*(.*?)\s*:\s*(.+?)\s*$!x; |
706587fc EW |
2413 | my ($repo_id, $path, $ref) = ($1, $2, $3); |
2414 | if ($ref eq $ref_id) { | |
2415 | $path = '' if ($path =~ m#^\./?#); | |
2416 | return ($repo_id, $path); | |
2417 | } | |
2418 | } | |
2419 | (undef, undef, undef); | |
2420 | } | |
2421 | ||
9b981fc6 | 2422 | sub new { |
706587fc EW |
2423 | my ($class, $ref_id, $repo_id, $path) = @_; |
2424 | if (defined $ref_id && !defined $repo_id && !defined $path) { | |
2425 | ($repo_id, $path) = find_ref($ref_id); | |
2426 | if (!defined $repo_id) { | |
2427 | die "Could not find a \"svn-remote.*.fetch\" key ", | |
2428 | "in the repository configuration matching: ", | |
6f5748e1 | 2429 | "$ref_id\n"; |
706587fc EW |
2430 | } |
2431 | } | |
2432 | my $self = _new($class, $repo_id, $ref_id, $path); | |
8b8fc068 EW |
2433 | if (!defined $self->{path} || !length $self->{path}) { |
2434 | my $fetch = command_oneline('config', '--get', | |
2435 | "svn-remote.$repo_id.fetch", | |
6f5748e1 | 2436 | ":$ref_id\$") or |
8b8fc068 | 2437 | die "Failed to read \"svn-remote.$repo_id.fetch\" ", |
6f5748e1 | 2438 | "\":$ref_id\$\" in config\n"; |
8b8fc068 EW |
2439 | ($self->{path}, undef) = split(/\s*:\s*/, $fetch); |
2440 | } | |
b1a954a3 EW |
2441 | $self->{path} =~ s{/+}{/}g; |
2442 | $self->{path} =~ s{\A/}{}; | |
2443 | $self->{path} =~ s{/\z}{}; | |
706587fc EW |
2444 | $self->{url} = command_oneline('config', '--get', |
2445 | "svn-remote.$repo_id.url") or | |
2446 | die "Failed to read \"svn-remote.$repo_id.url\" in config\n"; | |
12a296bc AS |
2447 | $self->{pushurl} = eval { command_oneline('config', '--get', |
2448 | "svn-remote.$repo_id.pushurl") }; | |
d6d3346b | 2449 | $self->rebuild; |
9b981fc6 EW |
2450 | $self; |
2451 | } | |
2452 | ||
bf655fd7 | 2453 | sub refname { |
6f5748e1 | 2454 | my ($refname) = $_[0]->{ref_id} ; |
bf655fd7 RE |
2455 | |
2456 | # It cannot end with a slash /, we'll throw up on this because | |
2457 | # SVN can't have directories with a slash in their name, either: | |
2458 | if ($refname =~ m{/$}) { | |
2459 | die "ref: '$refname' ends with a trailing slash, this is ", | |
2460 | "not permitted by git nor Subversion\n"; | |
2461 | } | |
2462 | ||
2463 | # It cannot have ASCII control character space, tilde ~, caret ^, | |
2464 | # colon :, question-mark ?, asterisk *, space, or open bracket [ | |
2465 | # anywhere. | |
2466 | # | |
2467 | # Additionally, % must be escaped because it is used for escaping | |
2468 | # and we want our escaped refname to be reversible | |
2469 | $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg; | |
2470 | ||
2471 | # no slash-separated component can begin with a dot . | |
2472 | # /.* becomes /%2E* | |
2473 | $refname =~ s{/\.}{/%2E}g; | |
2474 | ||
2475 | # It cannot have two consecutive dots .. anywhere | |
2476 | # .. becomes %2E%2E | |
2477 | $refname =~ s{\.\.}{%2E%2E}g; | |
2478 | ||
73d41955 TS |
2479 | # trailing dots and .lock are not allowed |
2480 | # .$ becomes %2E and .lock becomes %2Elock | |
2481 | $refname =~ s{\.(?=$|lock$)}{%2E}; | |
2482 | ||
2483 | # the sequence @{ is used to access the reflog | |
2484 | # @{ becomes %40{ | |
2485 | $refname =~ s{\@\{}{%40\{}g; | |
2486 | ||
bf655fd7 RE |
2487 | return $refname; |
2488 | } | |
2489 | ||
2490 | sub desanitize_refname { | |
2491 | my ($refname) = @_; | |
2492 | $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg; | |
2493 | return $refname; | |
2494 | } | |
9b981fc6 | 2495 | |
26a62d57 EW |
2496 | sub svm_uuid { |
2497 | my ($self) = @_; | |
2498 | return $self->{svm}->{uuid} if $self->svm; | |
2499 | $self->ra; | |
2500 | unless ($self->{svm}) { | |
2501 | die "SVM UUID not cached, and reading remotely failed\n"; | |
2502 | } | |
2503 | $self->{svm}->{uuid}; | |
2504 | } | |
8a49ee97 | 2505 | |
26a62d57 EW |
2506 | sub svm { |
2507 | my ($self) = @_; | |
2508 | return $self->{svm} if $self->{svm}; | |
2509 | my $svm; | |
8a49ee97 EW |
2510 | # see if we have it in our config, first: |
2511 | eval { | |
26a62d57 EW |
2512 | my $section = "svn-remote.$self->{repo_id}"; |
2513 | $svm = { | |
93f2689c EW |
2514 | source => tmp_config('--get', "$section.svm-source"), |
2515 | uuid => tmp_config('--get', "$section.svm-uuid"), | |
befc9adc | 2516 | replace => tmp_config('--get', "$section.svm-replace"), |
8a49ee97 EW |
2517 | } |
2518 | }; | |
befc9adc EW |
2519 | if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) { |
2520 | $self->{svm} = $svm; | |
2521 | } | |
26a62d57 EW |
2522 | $self->{svm}; |
2523 | } | |
2524 | ||
2525 | sub _set_svm_vars { | |
2526 | my ($self, $ra) = @_; | |
db03cd24 EW |
2527 | return $ra if $self->svm; |
2528 | ||
2529 | my @err = ( "useSvmProps set, but failed to read SVM properties\n", | |
befc9adc | 2530 | "(svm:source, svm:uuid) ", |
db03cd24 EW |
2531 | "from the following URLs:\n" ); |
2532 | sub read_svm_props { | |
befc9adc EW |
2533 | my ($self, $ra, $path, $r) = @_; |
2534 | my $props = ($ra->get_dir($path, $r))[2]; | |
db03cd24 | 2535 | my $src = $props->{'svm:source'}; |
db03cd24 | 2536 | my $uuid = $props->{'svm:uuid'}; |
befc9adc | 2537 | return undef if (!$src || !$uuid); |
26a62d57 | 2538 | |
befc9adc | 2539 | chomp($src, $uuid); |
26a62d57 | 2540 | |
b3e95936 | 2541 | $uuid =~ m{^[0-9a-f\-]{30,}$}i |
db03cd24 | 2542 | or die "doesn't look right - svm:uuid is '$uuid'\n"; |
befc9adc EW |
2543 | |
2544 | # the '!' is used to mark the repos_root!/relative/path | |
2545 | $src =~ s{/?!/?}{/}; | |
db03cd24 | 2546 | $src =~ s{/+$}{}; # no trailing slashes please |
befc9adc | 2547 | # username is of no interest |
8a49ee97 | 2548 | $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1}; |
8a49ee97 | 2549 | |
befc9adc EW |
2550 | my $replace = $ra->{url}; |
2551 | $replace .= "/$path" if length $path; | |
2552 | ||
db03cd24 | 2553 | my $section = "svn-remote.$self->{repo_id}"; |
befc9adc EW |
2554 | tmp_config("$section.svm-source", $src); |
2555 | tmp_config("$section.svm-replace", $replace); | |
2556 | tmp_config("$section.svm-uuid", $uuid); | |
2557 | $self->{svm} = { | |
2558 | source => $src, | |
2559 | uuid => $uuid, | |
2560 | replace => $replace | |
2561 | }; | |
db03cd24 EW |
2562 | } |
2563 | ||
2564 | my $r = $ra->get_latest_revnum; | |
2565 | my $path = $self->{path}; | |
befc9adc | 2566 | my %tried; |
db03cd24 | 2567 | while (length $path) { |
befc9adc EW |
2568 | unless ($tried{"$self->{url}/$path"}) { |
2569 | return $ra if $self->read_svm_props($ra, $path, $r); | |
2570 | $tried{"$self->{url}/$path"} = 1; | |
db03cd24 | 2571 | } |
befc9adc | 2572 | $path =~ s#/?[^/]+$##; |
8a49ee97 | 2573 | } |
befc9adc EW |
2574 | die "Path: '$path' should be ''\n" if $path ne ''; |
2575 | return $ra if $self->read_svm_props($ra, $path, $r); | |
2576 | $tried{"$self->{url}/$path"} = 1; | |
db03cd24 EW |
2577 | |
2578 | if ($ra->{repos_root} eq $self->{url}) { | |
befc9adc | 2579 | die @err, (map { " $_\n" } keys %tried), "\n"; |
db03cd24 EW |
2580 | } |
2581 | ||
2582 | # nope, make sure we're connected to the repository root: | |
2583 | my $ok; | |
2584 | my @tried_b; | |
2585 | $path = $ra->{svn_path}; | |
db03cd24 EW |
2586 | $ra = Git::SVN::Ra->new($ra->{repos_root}); |
2587 | while (length $path) { | |
befc9adc EW |
2588 | unless ($tried{"$ra->{url}/$path"}) { |
2589 | $ok = $self->read_svm_props($ra, $path, $r); | |
2590 | last if $ok; | |
2591 | $tried{"$ra->{url}/$path"} = 1; | |
2592 | } | |
2593 | $path =~ s#/?[^/]+$##; | |
db03cd24 | 2594 | } |
befc9adc EW |
2595 | die "Path: '$path' should be ''\n" if $path ne ''; |
2596 | $ok ||= $self->read_svm_props($ra, $path, $r); | |
2597 | $tried{"$ra->{url}/$path"} = 1; | |
db03cd24 | 2598 | if (!$ok) { |
befc9adc | 2599 | die @err, (map { " $_\n" } keys %tried), "\n"; |
db03cd24 EW |
2600 | } |
2601 | Git::SVN::Ra->new($self->{url}); | |
8a49ee97 EW |
2602 | } |
2603 | ||
62e349d2 EW |
2604 | sub svnsync { |
2605 | my ($self) = @_; | |
2606 | return $self->{svnsync} if $self->{svnsync}; | |
2607 | ||
2608 | if ($self->no_metadata) { | |
2609 | die "Can't have both 'noMetadata' and ", | |
2610 | "'useSvnsyncProps' options set!\n"; | |
2611 | } | |
2612 | if ($self->rewrite_root) { | |
2613 | die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ", | |
2614 | "options set!\n"; | |
2615 | } | |
3e18ce1a JS |
2616 | if ($self->rewrite_uuid) { |
2617 | die "Can't have both 'useSvnsyncProps' and 'rewriteUUID' ", | |
2618 | "options set!\n"; | |
2619 | } | |
62e349d2 EW |
2620 | |
2621 | my $svnsync; | |
2622 | # see if we have it in our config, first: | |
2623 | eval { | |
2624 | my $section = "svn-remote.$self->{repo_id}"; | |
98fa5b68 EW |
2625 | |
2626 | my $url = tmp_config('--get', "$section.svnsync-url"); | |
2627 | ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or | |
2628 | die "doesn't look right - svn:sync-from-url is '$url'\n"; | |
2629 | ||
2630 | my $uuid = tmp_config('--get', "$section.svnsync-uuid"); | |
b3e95936 | 2631 | ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or |
98fa5b68 EW |
2632 | die "doesn't look right - svn:sync-from-uuid is '$uuid'\n"; |
2633 | ||
2634 | $svnsync = { url => $url, uuid => $uuid } | |
62e349d2 EW |
2635 | }; |
2636 | if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) { | |
2637 | return $self->{svnsync} = $svnsync; | |
2638 | } | |
2639 | ||
2640 | my $err = "useSvnsyncProps set, but failed to read " . | |
2641 | "svnsync property: svn:sync-from-"; | |
2642 | my $rp = $self->ra->rev_proplist(0); | |
2643 | ||
2644 | my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n"; | |
98fa5b68 | 2645 | ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or |
62e349d2 EW |
2646 | die "doesn't look right - svn:sync-from-url is '$url'\n"; |
2647 | ||
2648 | my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n"; | |
b3e95936 | 2649 | ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or |
62e349d2 EW |
2650 | die "doesn't look right - svn:sync-from-uuid is '$uuid'\n"; |
2651 | ||
2652 | my $section = "svn-remote.$self->{repo_id}"; | |
2653 | tmp_config('--add', "$section.svnsync-uuid", $uuid); | |
2654 | tmp_config('--add', "$section.svnsync-url", $url); | |
2655 | return $self->{svnsync} = { url => $url, uuid => $uuid }; | |
2656 | } | |
2657 | ||
26a62d57 EW |
2658 | # this allows us to memoize our SVN::Ra UUID locally and avoid a |
2659 | # remote lookup (useful for 'git svn log'). | |
2660 | sub ra_uuid { | |
2661 | my ($self) = @_; | |
2662 | unless ($self->{ra_uuid}) { | |
2663 | my $key = "svn-remote.$self->{repo_id}.uuid"; | |
2664 | my $uuid = eval { tmp_config('--get', $key) }; | |
b3e95936 | 2665 | if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/i) { |
26a62d57 EW |
2666 | $self->{ra_uuid} = $uuid; |
2667 | } else { | |
2668 | die "ra_uuid called without URL\n" unless $self->{url}; | |
2669 | $self->{ra_uuid} = $self->ra->get_uuid; | |
2670 | tmp_config('--add', $key, $self->{ra_uuid}); | |
2671 | } | |
2672 | } | |
2673 | $self->{ra_uuid}; | |
2674 | } | |
2675 | ||
a5460eb7 EW |
2676 | sub _set_repos_root { |
2677 | my ($self, $repos_root) = @_; | |
2678 | my $k = "svn-remote.$self->{repo_id}.reposRoot"; | |
2679 | $repos_root ||= $self->ra->{repos_root}; | |
2680 | tmp_config($k, $repos_root); | |
2681 | $repos_root; | |
2682 | } | |
2683 | ||
2684 | sub repos_root { | |
2685 | my ($self) = @_; | |
2686 | my $k = "svn-remote.$self->{repo_id}.reposRoot"; | |
2687 | eval { tmp_config('--get', $k) } || $self->_set_repos_root; | |
2688 | } | |
2689 | ||
9b981fc6 EW |
2690 | sub ra { |
2691 | my ($self) = shift; | |
8a49ee97 | 2692 | my $ra = Git::SVN::Ra->new($self->{url}); |
a5460eb7 | 2693 | $self->_set_repos_root($ra->{repos_root}); |
91b03282 EW |
2694 | if ($self->use_svm_props && !$self->{svm}) { |
2695 | if ($self->no_metadata) { | |
97ae0911 EW |
2696 | die "Can't have both 'noMetadata' and ", |
2697 | "'useSvmProps' options set!\n"; | |
62e349d2 EW |
2698 | } elsif ($self->use_svnsync_props) { |
2699 | die "Can't have both 'useSvnsyncProps' and ", | |
2700 | "'useSvmProps' options set!\n"; | |
91b03282 | 2701 | } |
26a62d57 | 2702 | $ra = $self->_set_svm_vars($ra); |
8a49ee97 EW |
2703 | $self->{-want_revprops} = 1; |
2704 | } | |
2705 | $ra; | |
9b981fc6 EW |
2706 | } |
2707 | ||
01bdab84 BS |
2708 | # prop_walk(PATH, REV, SUB) |
2709 | # ------------------------- | |
2710 | # Recursively traverse PATH at revision REV and invoke SUB for each | |
2711 | # directory that contains a SVN property. SUB will be invoked as | |
2712 | # follows: &SUB(gs, path, props); where `gs' is this instance of | |
2713 | # Git::SVN, `path' the path to the directory where the properties | |
2714 | # `props' were found. The `path' will be relative to point of checkout, | |
2715 | # that is, if url://repo/trunk is the current Git branch, and that | |
2716 | # directory contains a sub-directory `d', SUB will be invoked with `/d/' | |
2717 | # as `path' (note the trailing `/'). | |
2718 | sub prop_walk { | |
2719 | my ($self, $path, $rev, $sub) = @_; | |
2720 | ||
35cda061 | 2721 | $path =~ s#^/##; |
01bdab84 BS |
2722 | my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev); |
2723 | $path =~ s#^/*#/#g; | |
9b981fc6 | 2724 | my $p = $path; |
01bdab84 BS |
2725 | # Strip the irrelevant part of the path. |
2726 | $p =~ s#^/+\Q$self->{path}\E(/|$)#/#; | |
2727 | # Ensure the path is terminated by a `/'. | |
2728 | $p =~ s#/*$#/#; | |
2729 | ||
2730 | # The properties contain all the internal SVN stuff nobody | |
2731 | # (usually) cares about. | |
2732 | my $interesting_props = 0; | |
2733 | foreach (keys %{$props}) { | |
2734 | # If it doesn't start with `svn:', it must be a | |
2735 | # user-defined property. | |
2736 | ++$interesting_props and next if $_ !~ /^svn:/; | |
2737 | # FIXME: Fragile, if SVN adds new public properties, | |
2738 | # this needs to be updated. | |
2739 | ++$interesting_props if /^svn:(?:ignore|keywords|executable | |
2740 | |eol-style|mime-type | |
2741 | |externals|needs-lock)$/x; | |
2742 | } | |
2743 | &$sub($self, $p, $props) if $interesting_props; | |
2744 | ||
9b981fc6 | 2745 | foreach (sort keys %$dirent) { |
0dc03d6a | 2746 | next if $dirent->{$_}->{kind} != $SVN::Node::dir; |
b7166cce | 2747 | $self->prop_walk($self->{path} . $p . $_, $rev, $sub); |
9b981fc6 EW |
2748 | } |
2749 | } | |
2750 | ||
3ebe8df7 EW |
2751 | sub last_rev { ($_[0]->last_rev_commit)[0] } |
2752 | sub last_commit { ($_[0]->last_rev_commit)[1] } | |
2753 | ||
9b981fc6 EW |
2754 | # returns the newest SVN revision number and newest commit SHA1 |
2755 | sub last_rev_commit { | |
2756 | my ($self) = @_; | |
2757 | if (defined $self->{last_rev} && defined $self->{last_commit}) { | |
2758 | return ($self->{last_rev}, $self->{last_commit}); | |
2759 | } | |
d2866f9e | 2760 | my $c = ::verify_ref($self->refname.'^0'); |
91b03282 | 2761 | if ($c && !$self->use_svm_props && !$self->no_metadata) { |
d2866f9e | 2762 | my $rev = (::cmt_metadata($c))[1]; |
9b981fc6 EW |
2763 | if (defined $rev) { |
2764 | ($self->{last_rev}, $self->{last_commit}) = ($rev, $c); | |
2765 | return ($rev, $c); | |
2766 | } | |
2767 | } | |
060610c5 EW |
2768 | my $map_path = $self->map_path; |
2769 | unless (-e $map_path) { | |
26a62d57 EW |
2770 | ($self->{last_rev}, $self->{last_commit}) = (undef, undef); |
2771 | return (undef, undef); | |
2772 | } | |
66ab84b9 | 2773 | my ($rev, $commit) = $self->rev_map_max(1); |
060610c5 EW |
2774 | ($self->{last_rev}, $self->{last_commit}) = ($rev, $commit); |
2775 | return ($rev, $commit); | |
9b981fc6 EW |
2776 | } |
2777 | ||
3ebe8df7 EW |
2778 | sub get_fetch_range { |
2779 | my ($self, $min, $max) = @_; | |
2780 | $max ||= $self->ra->get_latest_revnum; | |
060610c5 | 2781 | $min ||= $self->rev_map_max; |
3ebe8df7 | 2782 | (++$min, $max); |
9b981fc6 EW |
2783 | } |
2784 | ||
8a49ee97 | 2785 | sub tmp_config { |
93f2689c | 2786 | my (@args) = @_; |
b7e5348c EW |
2787 | my $old_def_config = "$ENV{GIT_DIR}/svn/config"; |
2788 | my $config = "$ENV{GIT_DIR}/svn/.metadata"; | |
38570a47 | 2789 | if (! -f $config && -f $old_def_config) { |
b7e5348c EW |
2790 | rename $old_def_config, $config or |
2791 | die "Failed rename $old_def_config => $config: $!\n"; | |
2792 | } | |
8a49ee97 | 2793 | my $old_config = $ENV{GIT_CONFIG}; |
93f2689c | 2794 | $ENV{GIT_CONFIG} = $config; |
8a49ee97 | 2795 | $@ = undef; |
b4d57e5e EW |
2796 | my @ret = eval { |
2797 | unless (-f $config) { | |
2798 | mkfile($config); | |
2799 | open my $fh, '>', $config or | |
2800 | die "Can't open $config: $!\n"; | |
2801 | print $fh "; This file is used internally by ", | |
2802 | "git-svn\n" or die | |
2803 | "Couldn't write to $config: $!\n"; | |
2804 | print $fh "; You should not have to edit it\n" or | |
2805 | die "Couldn't write to $config: $!\n"; | |
2806 | close $fh or die "Couldn't close $config: $!\n"; | |
2807 | } | |
2808 | command('config', @args); | |
2809 | }; | |
8a49ee97 EW |
2810 | my $err = $@; |
2811 | if (defined $old_config) { | |
2812 | $ENV{GIT_CONFIG} = $old_config; | |
2813 | } else { | |
2814 | delete $ENV{GIT_CONFIG}; | |
2815 | } | |
2816 | die $err if $err; | |
2817 | wantarray ? @ret : $ret[0]; | |
2818 | } | |
2819 | ||
9b981fc6 EW |
2820 | sub tmp_index_do { |
2821 | my ($self, $sub) = @_; | |
2822 | my $old_index = $ENV{GIT_INDEX_FILE}; | |
2823 | $ENV{GIT_INDEX_FILE} = $self->{index}; | |
8a49ee97 | 2824 | $@ = undef; |
b4d57e5e EW |
2825 | my @ret = eval { |
2826 | my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#); | |
2827 | mkpath([$dir]) unless -d $dir; | |
2828 | &$sub; | |
2829 | }; | |
8a49ee97 EW |
2830 | my $err = $@; |
2831 | if (defined $old_index) { | |
9b981fc6 EW |
2832 | $ENV{GIT_INDEX_FILE} = $old_index; |
2833 | } else { | |
2834 | delete $ENV{GIT_INDEX_FILE}; | |
2835 | } | |
8a49ee97 | 2836 | die $err if $err; |
9b981fc6 EW |
2837 | wantarray ? @ret : $ret[0]; |
2838 | } | |
2839 | ||
2840 | sub assert_index_clean { | |
2841 | my ($self, $treeish) = @_; | |
2842 | ||
2843 | $self->tmp_index_do(sub { | |
2844 | command_noisy('read-tree', $treeish) unless -e $self->{index}; | |
2845 | my $x = command_oneline('write-tree'); | |
2846 | my ($y) = (command(qw/cat-file commit/, $treeish) =~ | |
2847 | /^tree ($::sha1)/mo); | |
e8d120bd EW |
2848 | return if $y eq $x; |
2849 | ||
2850 | warn "Index mismatch: $y != $x\nrereading $treeish\n"; | |
2851 | unlink $self->{index} or die "unlink $self->{index}: $!\n"; | |
2852 | command_noisy('read-tree', $treeish); | |
9b981fc6 EW |
2853 | $x = command_oneline('write-tree'); |
2854 | if ($y ne $x) { | |
2855 | ::fatal "trees ($treeish) $y != $x\n", | |
207f1a75 | 2856 | "Something is seriously wrong..."; |
9b981fc6 EW |
2857 | } |
2858 | }); | |
2859 | } | |
2860 | ||
2861 | sub get_commit_parents { | |
0af9c9f9 | 2862 | my ($self, $log_entry) = @_; |
9b981fc6 | 2863 | my (%seen, @ret, @tmp); |
0af9c9f9 EW |
2864 | # legacy support for 'set-tree'; this is only used by set_tree_cb: |
2865 | if (my $ip = $self->{inject_parents}) { | |
2866 | if (my $commit = delete $ip->{$log_entry->{revision}}) { | |
2867 | push @tmp, $commit; | |
9b981fc6 EW |
2868 | } |
2869 | } | |
d2866f9e | 2870 | if (my $cur = ::verify_ref($self->refname.'^0')) { |
9b981fc6 EW |
2871 | push @tmp, $cur; |
2872 | } | |
733a65aa EW |
2873 | if (my $ipd = $self->{inject_parents_dcommit}) { |
2874 | if (my $commit = delete $ipd->{$log_entry->{revision}}) { | |
2875 | push @tmp, @$commit; | |
2876 | } | |
2877 | } | |
44320b9e | 2878 | push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp); |
9b981fc6 EW |
2879 | while (my $p = shift @tmp) { |
2880 | next if $seen{$p}; | |
2881 | $seen{$p} = 1; | |
2882 | push @ret, $p; | |
9b981fc6 EW |
2883 | } |
2884 | @ret; | |
2885 | } | |
2886 | ||
aea736cc EW |
2887 | sub rewrite_root { |
2888 | my ($self) = @_; | |
2889 | return $self->{-rewrite_root} if exists $self->{-rewrite_root}; | |
2890 | my $k = "svn-remote.$self->{repo_id}.rewriteRoot"; | |
2891 | my $rwr = eval { command_oneline(qw/config --get/, $k) }; | |
2892 | if ($rwr) { | |
2893 | $rwr =~ s#/+$##; | |
2894 | if ($rwr !~ m#^[a-z\+]+://#) { | |
2895 | die "$rwr is not a valid URL (key: $k)\n"; | |
2896 | } | |
2897 | } | |
2898 | $self->{-rewrite_root} = $rwr; | |
2899 | } | |
2900 | ||
3e18ce1a JS |
2901 | sub rewrite_uuid { |
2902 | my ($self) = @_; | |
2903 | return $self->{-rewrite_uuid} if exists $self->{-rewrite_uuid}; | |
2904 | my $k = "svn-remote.$self->{repo_id}.rewriteUUID"; | |
2905 | my $rwid = eval { command_oneline(qw/config --get/, $k) }; | |
2906 | if ($rwid) { | |
2907 | $rwid =~ s#/+$##; | |
2908 | if ($rwid !~ m#^[a-f0-9]{8}-(?:[a-f0-9]{4}-){3}[a-f0-9]{12}$#) { | |
2909 | die "$rwid is not a valid UUID (key: $k)\n"; | |
2910 | } | |
2911 | } | |
2912 | $self->{-rewrite_uuid} = $rwid; | |
2913 | } | |
2914 | ||
aea736cc EW |
2915 | sub metadata_url { |
2916 | my ($self) = @_; | |
2917 | ($self->rewrite_root || $self->{url}) . | |
2918 | (length $self->{path} ? '/' . $self->{path} : ''); | |
2919 | } | |
2920 | ||
706587fc | 2921 | sub full_url { |
9b981fc6 | 2922 | my ($self) = @_; |
5d3b7cd5 | 2923 | $self->{url} . (length $self->{path} ? '/' . $self->{path} : ''); |
9b981fc6 EW |
2924 | } |
2925 | ||
12a296bc AS |
2926 | sub full_pushurl { |
2927 | my ($self) = @_; | |
2928 | if ($self->{pushurl}) { | |
2929 | return $self->{pushurl} . (length $self->{path} ? '/' . | |
2930 | $self->{path} : ''); | |
2931 | } else { | |
2932 | return $self->full_url; | |
2933 | } | |
2934 | } | |
ad94802a EW |
2935 | |
2936 | sub set_commit_header_env { | |
2937 | my ($log_entry) = @_; | |
2938 | my %env; | |
2939 | foreach my $ned (qw/NAME EMAIL DATE/) { | |
2940 | foreach my $ac (qw/AUTHOR COMMITTER/) { | |
2941 | $env{"GIT_${ac}_${ned}"} = $ENV{"GIT_${ac}_${ned}"}; | |
2942 | } | |
9b981fc6 | 2943 | } |
ad94802a | 2944 | |
70ae04e4 AW |
2945 | $ENV{GIT_AUTHOR_NAME} = $log_entry->{name}; |
2946 | $ENV{GIT_AUTHOR_EMAIL} = $log_entry->{email}; | |
44320b9e | 2947 | $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date}; |
9b981fc6 | 2948 | |
70ae04e4 AW |
2949 | $ENV{GIT_COMMITTER_NAME} = (defined $log_entry->{commit_name}) |
2950 | ? $log_entry->{commit_name} | |
2951 | : $log_entry->{name}; | |
2952 | $ENV{GIT_COMMITTER_EMAIL} = (defined $log_entry->{commit_email}) | |
2953 | ? $log_entry->{commit_email} | |
2954 | : $log_entry->{email}; | |
ad94802a EW |
2955 | \%env; |
2956 | } | |
70ae04e4 | 2957 | |
ad94802a EW |
2958 | sub restore_commit_header_env { |
2959 | my ($env) = @_; | |
2960 | foreach my $ned (qw/NAME EMAIL DATE/) { | |
2961 | foreach my $ac (qw/AUTHOR COMMITTER/) { | |
2962 | my $k = "GIT_${ac}_${ned}"; | |
2963 | if (defined $env->{$k}) { | |
2964 | $ENV{$k} = $env->{$k}; | |
2965 | } else { | |
2966 | delete $ENV{$k}; | |
2967 | } | |
2968 | } | |
2969 | } | |
2970 | } | |
2971 | ||
94bc914c KW |
2972 | sub gc { |
2973 | command_noisy('gc', '--auto'); | |
2974 | }; | |
2975 | ||
ad94802a EW |
2976 | sub do_git_commit { |
2977 | my ($self, $log_entry) = @_; | |
2978 | my $lr = $self->last_rev; | |
2979 | if (defined $lr && $lr >= $log_entry->{revision}) { | |
2980 | die "Last fetched revision of ", $self->refname, | |
2981 | " was r$lr, but we are about to fetch: ", | |
2982 | "r$log_entry->{revision}!\n"; | |
2983 | } | |
2984 | if (my $c = $self->rev_map_get($log_entry->{revision})) { | |
2985 | croak "$log_entry->{revision} = $c already exists! ", | |
2986 | "Why are we refetching it?\n"; | |
2987 | } | |
2988 | my $old_env = set_commit_header_env($log_entry); | |
44320b9e | 2989 | my $tree = $log_entry->{tree}; |
9b981fc6 EW |
2990 | if (!defined $tree) { |
2991 | $tree = $self->tmp_index_do(sub { | |
2992 | command_oneline('write-tree') }); | |
2993 | } | |
2994 | die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o; | |
2995 | ||
e855bfc0 | 2996 | my @exec = ('git', 'commit-tree', $tree); |
0af9c9f9 | 2997 | foreach ($self->get_commit_parents($log_entry)) { |
9b981fc6 EW |
2998 | push @exec, '-p', $_; |
2999 | } | |
3000 | defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec)) | |
3001 | or croak $!; | |
16fc08e2 EW |
3002 | binmode $msg_fh; |
3003 | ||
3004 | # we always get UTF-8 from SVN, but we may want our commits in | |
3005 | # a different encoding. | |
3006 | if (my $enc = Git::config('i18n.commitencoding')) { | |
3007 | require Encode; | |
3008 | Encode::from_to($log_entry->{log}, 'UTF-8', $enc); | |
3009 | } | |
44320b9e | 3010 | print $msg_fh $log_entry->{log} or croak $!; |
ad94802a | 3011 | restore_commit_header_env($old_env); |
91b03282 | 3012 | unless ($self->no_metadata) { |
8a49ee97 EW |
3013 | print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n" |
3014 | or croak $!; | |
9760adcc | 3015 | } |
9b981fc6 EW |
3016 | $msg_fh->flush == 0 or croak $!; |
3017 | close $msg_fh or croak $!; | |
3018 | chomp(my $commit = do { local $/; <$out_fh> }); | |
3019 | close $out_fh or croak $!; | |
3020 | waitpid $pid, 0; | |
3021 | croak $? if $?; | |
3022 | if ($commit !~ /^$::sha1$/o) { | |
3023 | die "Failed to commit, invalid sha1: $commit\n"; | |
3024 | } | |
3025 | ||
060610c5 | 3026 | $self->rev_map_set($log_entry->{revision}, $commit, 1); |
9b981fc6 | 3027 | |
44320b9e | 3028 | $self->{last_rev} = $log_entry->{revision}; |
9b981fc6 | 3029 | $self->{last_commit} = $commit; |
49750f30 | 3030 | print "r$log_entry->{revision}" unless $::_q > 1; |
8a49ee97 | 3031 | if (defined $log_entry->{svm_revision}) { |
49750f30 | 3032 | print " (\@$log_entry->{svm_revision})" unless $::_q > 1; |
060610c5 | 3033 | $self->rev_map_set($log_entry->{svm_revision}, $commit, |
26a62d57 | 3034 | 0, $self->svm_uuid); |
8a49ee97 | 3035 | } |
49750f30 | 3036 | print " = $commit ($self->{ref_id})\n" unless $::_q > 1; |
94bc914c KW |
3037 | if (--$_gc_nr == 0) { |
3038 | $_gc_nr = $_gc_period; | |
3039 | gc(); | |
3040 | } | |
9b981fc6 EW |
3041 | return $commit; |
3042 | } | |
3043 | ||
fbcc1737 EW |
3044 | sub match_paths { |
3045 | my ($self, $paths, $r) = @_; | |
4e9f6cc7 | 3046 | return 1 if $self->{path} eq ''; |
d542aedb EW |
3047 | if (my $path = $paths->{"/$self->{path}"}) { |
3048 | return ($path->{action} eq 'D') ? 0 : 1; | |
3049 | } | |
0b2af457 | 3050 | $self->{path_regex} ||= qr/^\/\Q$self->{path}\E\//; |
fbcc1737 EW |
3051 | if (grep /$self->{path_regex}/, keys %$paths) { |
3052 | return 1; | |
3053 | } | |
3054 | my $c = ''; | |
3055 | foreach (split m#/#, $self->{path}) { | |
3056 | $c .= "/$_"; | |
74a81227 EW |
3057 | next unless ($paths->{$c} && |
3058 | ($paths->{$c}->{action} =~ /^[AR]$/)); | |
e518192f EW |
3059 | if ($self->ra->check_path($self->{path}, $r) == |
3060 | $SVN::Node::dir) { | |
fbcc1737 EW |
3061 | return 1; |
3062 | } | |
3063 | } | |
3064 | return 0; | |
3065 | } | |
3066 | ||
15710b6f EW |
3067 | sub find_parent_branch { |
3068 | my ($self, $paths, $rev) = @_; | |
91b03282 | 3069 | return undef unless $self->follow_parent; |
e5a0b240 | 3070 | unless (defined $paths) { |
c7eba716 EW |
3071 | my $err_handler = $SVN::Error::handler; |
3072 | $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs; | |
3c49a035 MN |
3073 | $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1, |
3074 | sub { $paths = $_[0] }); | |
c7eba716 | 3075 | $SVN::Error::handler = $err_handler; |
e5a0b240 EW |
3076 | } |
3077 | return undef unless defined $paths; | |
15710b6f EW |
3078 | |
3079 | # look for a parent from another branch: | |
0b2af457 | 3080 | my @b_path_components = split m#/#, $self->{path}; |
7f578c55 EW |
3081 | my @a_path_components; |
3082 | my $i; | |
3083 | while (@b_path_components) { | |
3084 | $i = $paths->{'/'.join('/', @b_path_components)}; | |
74a81227 | 3085 | last if $i && defined $i->{copyfrom_path}; |
7f578c55 EW |
3086 | unshift(@a_path_components, pop(@b_path_components)); |
3087 | } | |
74a81227 EW |
3088 | return undef unless defined $i && defined $i->{copyfrom_path}; |
3089 | my $branch_from = $i->{copyfrom_path}; | |
7f578c55 EW |
3090 | if (@a_path_components) { |
3091 | print STDERR "branch_from: $branch_from => "; | |
3092 | $branch_from .= '/'.join('/', @a_path_components); | |
3093 | print STDERR $branch_from, "\n"; | |
3094 | } | |
3ebe8df7 | 3095 | my $r = $i->{copyfrom_rev}; |
15710b6f EW |
3096 | my $repos_root = $self->ra->{repos_root}; |
3097 | my $url = $self->ra->{url}; | |
0b2af457 | 3098 | my $new_url = $url . $branch_from; |
15710b6f | 3099 | print STDERR "Found possible branch point: ", |
85886162 SA |
3100 | "$new_url => ", $self->full_url, ", $r\n" |
3101 | unless $::_q > 1; | |
15710b6f | 3102 | $branch_from =~ s#^/##; |
0b2af457 | 3103 | my $gs = $self->other_gs($new_url, $url, |
8e3f9b17 | 3104 | $branch_from, $r, $self->{ref_id}); |
15710b6f | 3105 | my ($r0, $parent) = $gs->find_rev_before($r, 1); |
553589f7 DM |
3106 | { |
3107 | my ($base, $head); | |
3108 | if (!defined $r0 || !defined $parent) { | |
3109 | ($base, $head) = parse_revision_argument(0, $r); | |
3110 | } else { | |
3111 | if ($r0 < $r) { | |
3112 | $gs->ra->get_log([$gs->{path}], $r0 + 1, $r, 1, | |
3113 | 0, 1, sub { $base = $_[1] - 1 }); | |
3114 | } | |
3115 | } | |
3116 | if (defined $base && $base <= $r) { | |
d627de6b EW |
3117 | $gs->fetch($base, $r); |
3118 | } | |
553589f7 | 3119 | ($r0, $parent) = $gs->find_rev_before($r, 1); |
15710b6f | 3120 | } |
ef70de96 | 3121 | if (defined $r0 && defined $parent) { |
85886162 SA |
3122 | print STDERR "Found branch parent: ($self->{ref_id}) $parent\n" |
3123 | unless $::_q > 1; | |
15710b6f EW |
3124 | my $ed; |
3125 | if ($self->ra->can_do_switch) { | |
2e5e2480 | 3126 | $self->assert_index_clean($parent); |
85886162 SA |
3127 | print STDERR "Following parent with do_switch\n" |
3128 | unless $::_q > 1; | |
15710b6f | 3129 | # do_switch works with svn/trunk >= r22312, but that |
2b27f6c8 | 3130 | # is not included with SVN 1.4.3 (the latest version |
15710b6f | 3131 | # at the moment), so we can't rely on it |
83c2fcff | 3132 | $self->{last_rev} = $r0; |
15710b6f | 3133 | $self->{last_commit} = $parent; |
8841b37f | 3134 | $ed = SVN::Git::Fetcher->new($self, $gs->{path}); |
8a603774 | 3135 | $gs->ra->gs_do_switch($r0, $rev, $gs, |
15710b6f EW |
3136 | $self->full_url, $ed) |
3137 | or die "SVN connection failed somewhere...\n"; | |
9ff74e95 SW |
3138 | } elsif ($self->ra->trees_match($new_url, $r0, |
3139 | $self->full_url, $rev)) { | |
3140 | print STDERR "Trees match:\n", | |
3141 | " $new_url\@$r0\n", | |
3142 | " ${\$self->full_url}\@$rev\n", | |
85886162 SA |
3143 | "Following parent with no changes\n" |
3144 | unless $::_q > 1; | |
9ff74e95 SW |
3145 | $self->tmp_index_do(sub { |
3146 | command_noisy('read-tree', $parent); | |
3147 | }); | |
3148 | $self->{last_commit} = $parent; | |
15710b6f | 3149 | } else { |
85886162 SA |
3150 | print STDERR "Following parent with do_update\n" |
3151 | unless $::_q > 1; | |
15710b6f | 3152 | $ed = SVN::Git::Fetcher->new($self); |
8a603774 | 3153 | $self->ra->gs_do_update($rev, $rev, $self, $ed) |
15710b6f EW |
3154 | or die "SVN connection failed somewhere...\n"; |
3155 | } | |
85886162 | 3156 | print STDERR "Successfully followed parent\n" unless $::_q > 1; |
15710b6f EW |
3157 | return $self->make_log_entry($rev, [$parent], $ed); |
3158 | } | |
15710b6f EW |
3159 | return undef; |
3160 | } | |
3161 | ||
9b981fc6 | 3162 | sub do_fetch { |
706587fc | 3163 | my ($self, $paths, $rev) = @_; |
15710b6f | 3164 | my $ed; |
9b981fc6 | 3165 | my ($last_rev, @parents); |
b9dffd8c EW |
3166 | if (my $lc = $self->last_commit) { |
3167 | # we can have a branch that was deleted, then re-added | |
3168 | # under the same name but copied from another path, in | |
3169 | # which case we'll have multiple parents (we don't | |
3170 | # want to break the original ref, nor lose copypath info): | |
3171 | if (my $log_entry = $self->find_parent_branch($paths, $rev)) { | |
3172 | push @{$log_entry->{parents}}, $lc; | |
3173 | return $log_entry; | |
3174 | } | |
15710b6f | 3175 | $ed = SVN::Git::Fetcher->new($self); |
9b981fc6 | 3176 | $last_rev = $self->{last_rev}; |
b9dffd8c EW |
3177 | $ed->{c} = $lc; |
3178 | @parents = ($lc); | |
9b981fc6 EW |
3179 | } else { |
3180 | $last_rev = $rev; | |
15710b6f EW |
3181 | if (my $log_entry = $self->find_parent_branch($paths, $rev)) { |
3182 | return $log_entry; | |
3183 | } | |
3184 | $ed = SVN::Git::Fetcher->new($self); | |
9b981fc6 | 3185 | } |
8a603774 | 3186 | unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) { |
9b981fc6 EW |
3187 | die "SVN connection failed somewhere...\n"; |
3188 | } | |
3189 | $self->make_log_entry($rev, \@parents, $ed); | |
3190 | } | |
3191 | ||
6111b934 EW |
3192 | sub mkemptydirs { |
3193 | my ($self, $r) = @_; | |
a5b80d92 EW |
3194 | |
3195 | sub scan { | |
3196 | my ($r, $empty_dirs, $line) = @_; | |
3197 | if (defined $r && $line =~ /^r(\d+)$/) { | |
3198 | return 0 if $1 > $r; | |
3199 | } elsif ($line =~ /^ \+empty_dir: (.+)$/) { | |
3200 | $empty_dirs->{$1} = 1; | |
3201 | } elsif ($line =~ /^ \-empty_dir: (.+)$/) { | |
3202 | my @d = grep {m[^\Q$1\E(/|$)]} (keys %$empty_dirs); | |
3203 | delete @$empty_dirs{@d}; | |
3204 | } | |
3205 | 1; # continue | |
3206 | }; | |
3207 | ||
6111b934 | 3208 | my %empty_dirs = (); |
a5b80d92 EW |
3209 | my $gz_file = "$self->{dir}/unhandled.log.gz"; |
3210 | if (-f $gz_file) { | |
3211 | if (!$can_compress) { | |
3212 | warn "Compress::Zlib could not be found; ", | |
3213 | "empty directories in $gz_file will not be read\n"; | |
3214 | } else { | |
3215 | my $gz = Compress::Zlib::gzopen($gz_file, "rb") or | |
3216 | die "Unable to open $gz_file: $!\n"; | |
3217 | my $line; | |
3218 | while ($gz->gzreadline($line) > 0) { | |
3219 | scan($r, \%empty_dirs, $line) or last; | |
3220 | } | |
3221 | $gz->gzclose; | |
3222 | } | |
3223 | } | |
6111b934 | 3224 | |
a5b80d92 EW |
3225 | if (open my $fh, '<', "$self->{dir}/unhandled.log") { |
3226 | binmode $fh or croak "binmode: $!"; | |
3227 | while (<$fh>) { | |
3228 | scan($r, \%empty_dirs, $_) or last; | |
6111b934 | 3229 | } |
a5b80d92 | 3230 | close $fh; |
6111b934 | 3231 | } |
9be30eed EW |
3232 | |
3233 | my $strip = qr/\A\Q$self->{path}\E(?:\/|$)/; | |
6111b934 EW |
3234 | foreach my $d (sort keys %empty_dirs) { |
3235 | $d = uri_decode($d); | |
9be30eed | 3236 | $d =~ s/$strip//; |
7c42e390 | 3237 | next unless length($d); |
6111b934 | 3238 | next if -d $d; |
7c42e390 | 3239 | if (-e $d) { |
6111b934 EW |
3240 | warn "$d exists but is not a directory\n"; |
3241 | } else { | |
3242 | print "creating empty directory: $d\n"; | |
3243 | mkpath([$d]); | |
3244 | } | |
3245 | } | |
3246 | } | |
3247 | ||
97f6987a EW |
3248 | sub get_untracked { |
3249 | my ($self, $ed) = @_; | |
3250 | my @out; | |
3251 | my $h = $ed->{empty}; | |
9b981fc6 EW |
3252 | foreach (sort keys %$h) { |
3253 | my $act = $h->{$_} ? '+empty_dir' : '-empty_dir'; | |
97f6987a | 3254 | push @out, " $act: " . uri_encode($_); |
9b981fc6 EW |
3255 | warn "W: $act: $_\n"; |
3256 | } | |
3257 | foreach my $t (qw/dir_prop file_prop/) { | |
97f6987a | 3258 | $h = $ed->{$t} or next; |
9b981fc6 EW |
3259 | foreach my $path (sort keys %$h) { |
3260 | my $ppath = $path eq '' ? '.' : $path; | |
3261 | foreach my $prop (sort keys %{$h->{$path}}) { | |
1ce255dc | 3262 | next if $SKIP_PROP{$prop}; |
9b981fc6 | 3263 | my $v = $h->{$path}->{$prop}; |
97f6987a EW |
3264 | my $t_ppath_prop = "$t: " . |
3265 | uri_encode($ppath) . ' ' . | |
3266 | uri_encode($prop); | |
9b981fc6 | 3267 | if (defined $v) { |
97f6987a EW |
3268 | push @out, " +$t_ppath_prop " . |
3269 | uri_encode($v); | |
9b981fc6 | 3270 | } else { |
97f6987a | 3271 | push @out, " -$t_ppath_prop"; |
9b981fc6 EW |
3272 | } |
3273 | } | |
3274 | } | |
3275 | } | |
3276 | foreach my $t (qw/absent_file absent_directory/) { | |
97f6987a | 3277 | $h = $ed->{$t} or next; |
9b981fc6 EW |
3278 | foreach my $parent (sort keys %$h) { |
3279 | foreach my $path (sort @{$h->{$parent}}) { | |
97f6987a EW |
3280 | push @out, " $t: " . |
3281 | uri_encode("$parent/$path"); | |
9b981fc6 EW |
3282 | warn "W: $t: $parent/$path ", |
3283 | "Insufficient permissions?\n"; | |
3284 | } | |
3285 | } | |
3286 | } | |
97f6987a | 3287 | \@out; |
9b981fc6 EW |
3288 | } |
3289 | ||
6aa17fc6 WYC |
3290 | sub get_tz { |
3291 | # some systmes don't handle or mishandle %z, so be creative. | |
3292 | my $t = shift || time; | |
3293 | my $gm = timelocal(gmtime($t)); | |
3294 | my $sign = qw( + + - )[ $t <=> $gm ]; | |
3295 | return sprintf("%s%02d%02d", $sign, (gmtime(abs($t - $gm)))[2,1]); | |
3296 | } | |
3297 | ||
e82f0d73 PH |
3298 | # parse_svn_date(DATE) |
3299 | # -------------------- | |
3300 | # Given a date (in UTC) from Subversion, return a string in the format | |
3301 | # "<TZ Offset> <local date/time>" that Git will use. | |
3302 | # | |
3303 | # By default the parsed date will be in UTC; if $Git::SVN::_localtime | |
3304 | # is true we'll convert it to the local timezone instead. | |
1c8443b0 EW |
3305 | sub parse_svn_date { |
3306 | my $date = shift || return '+0000 1970-01-01 00:00:00'; | |
3307 | my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T | |
b94ead75 | 3308 | (\d\d)\:(\d\d)\:(\d\d)\.\d*Z$/x) or |
1c8443b0 | 3309 | croak "Unable to parse date: $date\n"; |
e82f0d73 PH |
3310 | my $parsed_date; # Set next. |
3311 | ||
3312 | if ($Git::SVN::_localtime) { | |
3313 | # Translate the Subversion datetime to an epoch time. | |
3314 | # Begin by switching ourselves to $date's timezone, UTC. | |
3315 | my $old_env_TZ = $ENV{TZ}; | |
3316 | $ENV{TZ} = 'UTC'; | |
3317 | ||
3318 | my $epoch_in_UTC = | |
3319 | POSIX::strftime('%s', $S, $M, $H, $d, $m - 1, $Y - 1900); | |
3320 | ||
3321 | # Determine our local timezone (including DST) at the | |
3322 | # time of $epoch_in_UTC. $Git::SVN::Log::TZ stored the | |
3323 | # value of TZ, if any, at the time we were run. | |
3324 | if (defined $Git::SVN::Log::TZ) { | |
3325 | $ENV{TZ} = $Git::SVN::Log::TZ; | |
3326 | } else { | |
3327 | delete $ENV{TZ}; | |
3328 | } | |
3329 | ||
6aa17fc6 | 3330 | my $our_TZ = get_tz(); |
e82f0d73 PH |
3331 | |
3332 | # This converts $epoch_in_UTC into our local timezone. | |
3333 | my ($sec, $min, $hour, $mday, $mon, $year, | |
3334 | $wday, $yday, $isdst) = localtime($epoch_in_UTC); | |
3335 | ||
3336 | $parsed_date = sprintf('%s %04d-%02d-%02d %02d:%02d:%02d', | |
3337 | $our_TZ, $year + 1900, $mon + 1, | |
3338 | $mday, $hour, $min, $sec); | |
3339 | ||
3340 | # Reset us to the timezone in effect when we entered | |
3341 | # this routine. | |
3342 | if (defined $old_env_TZ) { | |
3343 | $ENV{TZ} = $old_env_TZ; | |
3344 | } else { | |
3345 | delete $ENV{TZ}; | |
3346 | } | |
3347 | } else { | |
3348 | $parsed_date = "+0000 $Y-$m-$d $H:$M:$S"; | |
3349 | } | |
3350 | ||
3351 | return $parsed_date; | |
1c8443b0 EW |
3352 | } |
3353 | ||
8e3f9b17 | 3354 | sub other_gs { |
0b2af457 | 3355 | my ($self, $new_url, $url, |
8e3f9b17 | 3356 | $branch_from, $r, $old_ref_id) = @_; |
0b2af457 | 3357 | my $gs = Git::SVN->find_by_url($new_url, $url, $branch_from); |
8e3f9b17 SV |
3358 | unless ($gs) { |
3359 | my $ref_id = $old_ref_id; | |
54fb7f9b | 3360 | $ref_id =~ s/\@\d+-*$//; |
8e3f9b17 SV |
3361 | $ref_id .= "\@$r"; |
3362 | # just grow a tail if we're not unique enough :x | |
3363 | $ref_id .= '-' while find_ref($ref_id); | |
8e3f9b17 SV |
3364 | my ($u, $p, $repo_id) = ($new_url, '', $ref_id); |
3365 | if ($u =~ s#^\Q$url\E(/|$)##) { | |
3366 | $p = $u; | |
3367 | $u = $url; | |
3368 | $repo_id = $self->{repo_id}; | |
3369 | } | |
3235b705 DK |
3370 | while (1) { |
3371 | # It is possible to tag two different subdirectories at | |
3372 | # the same revision. If the url for an existing ref | |
3373 | # does not match, we must either find a ref with a | |
3374 | # matching url or create a new ref by growing a tail. | |
3375 | $gs = Git::SVN->init($u, $p, $repo_id, $ref_id, 1); | |
3376 | my (undef, $max_commit) = $gs->rev_map_max(1); | |
3377 | last if (!$max_commit); | |
3378 | my ($url) = ::cmt_metadata($max_commit); | |
85f022e9 | 3379 | last if ($url eq $gs->metadata_url); |
3235b705 DK |
3380 | $ref_id .= '-'; |
3381 | } | |
3382 | print STDERR "Initializing parent: $ref_id\n" unless $::_q > 1; | |
8e3f9b17 SV |
3383 | } |
3384 | $gs | |
3385 | } | |
3386 | ||
36db1edd ML |
3387 | sub call_authors_prog { |
3388 | my ($orig_author) = @_; | |
d3d7d47e | 3389 | $orig_author = command_oneline('rev-parse', '--sq-quote', $orig_author); |
36db1edd ML |
3390 | my $author = `$::_authors_prog $orig_author`; |
3391 | if ($? != 0) { | |
3392 | die "$::_authors_prog failed with exit code $?\n" | |
3393 | } | |
3394 | if ($author =~ /^\s*(.+?)\s*<(.*)>\s*$/) { | |
3395 | my ($name, $email) = ($1, $2); | |
3396 | $email = undef if length $2 == 0; | |
3397 | return [$name, $email]; | |
3398 | } else { | |
3399 | die "Author: $orig_author: $::_authors_prog returned " | |
3400 | . "invalid author format: $author\n"; | |
3401 | } | |
3402 | } | |
3403 | ||
1c8443b0 EW |
3404 | sub check_author { |
3405 | my ($author) = @_; | |
3406 | if (!defined $author || length $author == 0) { | |
3407 | $author = '(no author)'; | |
36db1edd ML |
3408 | } |
3409 | if (!defined $::users{$author}) { | |
3410 | if (defined $::_authors_prog) { | |
3411 | $::users{$author} = call_authors_prog($author); | |
3412 | } elsif (defined $::_authors) { | |
3413 | die "Author: $author not defined in $::_authors file\n"; | |
3414 | } | |
1c8443b0 EW |
3415 | } |
3416 | $author; | |
3417 | } | |
3418 | ||
f1264bd6 SV |
3419 | sub find_extra_svk_parents { |
3420 | my ($self, $ed, $tickets, $parents) = @_; | |
3421 | # aha! svk:merge property changed... | |
3422 | my @tickets = split "\n", $tickets; | |
3423 | my @known_parents; | |
3424 | for my $ticket ( @tickets ) { | |
3425 | my ($uuid, $path, $rev) = split /:/, $ticket; | |
3426 | if ( $uuid eq $self->ra_uuid ) { | |
bf60fff8 | 3427 | my $url = $self->{url}; |
f1264bd6 SV |
3428 | my $repos_root = $url; |
3429 | my $branch_from = $path; | |
3430 | $branch_from =~ s{^/}{}; | |
3431 | my $gs = $self->other_gs($repos_root."/".$branch_from, | |
3432 | $url, | |
3433 | $branch_from, | |
3434 | $rev, | |
3435 | $self->{ref_id}); | |
3436 | if ( my $commit = $gs->rev_map_get($rev, $uuid) ) { | |
3437 | # wahey! we found it, but it might be | |
3438 | # an old one (!) | |
e9e4c8b7 | 3439 | push @known_parents, [ $rev, $commit ]; |
f1264bd6 SV |
3440 | } |
3441 | } | |
3442 | } | |
e9e4c8b7 AV |
3443 | # Ordering matters; highest-numbered commit merge tickets |
3444 | # first, as they may account for later merge ticket additions | |
3445 | # or changes. | |
3446 | @known_parents = map {$_->[1]} sort {$b->[0] <=> $a->[0]} @known_parents; | |
f1264bd6 SV |
3447 | for my $parent ( @known_parents ) { |
3448 | my @cmd = ('rev-list', $parent, map { "^$_" } @$parents ); | |
3449 | my ($msg_fh, $ctx) = command_output_pipe(@cmd); | |
3450 | my $new; | |
3451 | while ( <$msg_fh> ) { | |
3452 | $new=1;last; | |
3453 | } | |
3454 | command_close_pipe($msg_fh, $ctx); | |
3455 | if ( $new ) { | |
3456 | print STDERR | |
3457 | "Found merge parent (svk:merge ticket): $parent\n"; | |
3458 | push @$parents, $parent; | |
3459 | } | |
3460 | } | |
3461 | } | |
3462 | ||
7d944c33 SV |
3463 | sub lookup_svn_merge { |
3464 | my $uuid = shift; | |
3465 | my $url = shift; | |
3466 | my $merge = shift; | |
3467 | ||
3468 | my ($source, $revs) = split ":", $merge; | |
3469 | my $path = $source; | |
3470 | $path =~ s{^/}{}; | |
3471 | my $gs = Git::SVN->find_by_url($url.$source, $url, $path); | |
3472 | if ( !$gs ) { | |
3473 | warn "Couldn't find revmap for $url$source\n"; | |
3474 | return; | |
3475 | } | |
3476 | my @ranges = split ",", $revs; | |
3477 | my ($tip, $tip_commit); | |
3478 | my @merged_commit_ranges; | |
3479 | # find the tip | |
3480 | for my $range ( @ranges ) { | |
3481 | my ($bottom, $top) = split "-", $range; | |
3482 | $top ||= $bottom; | |
33973a5b SV |
3483 | my $bottom_commit = $gs->find_rev_after( $bottom, 1, $top ); |
3484 | my $top_commit = $gs->find_rev_before( $top, 1, $bottom ); | |
7d944c33 SV |
3485 | |
3486 | unless ($top_commit and $bottom_commit) { | |
3487 | warn "W:unknown path/rev in svn:mergeinfo " | |
3488 | ."dirprop: $source:$range\n"; | |
3489 | next; | |
3490 | } | |
3491 | ||
124b70a2 MH |
3492 | if (scalar(command('rev-parse', "$bottom_commit^@"))) { |
3493 | push @merged_commit_ranges, | |
3494 | "$bottom_commit^..$top_commit"; | |
3495 | } else { | |
3496 | push @merged_commit_ranges, "$top_commit"; | |
3497 | } | |
7d944c33 SV |
3498 | |
3499 | if ( !defined $tip or $top > $tip ) { | |
3500 | $tip = $top; | |
3501 | $tip_commit = $top_commit; | |
3502 | } | |
3503 | } | |
3504 | return ($tip_commit, @merged_commit_ranges); | |
3505 | } | |
7a955a53 SV |
3506 | |
3507 | sub _rev_list { | |
3508 | my ($msg_fh, $ctx) = command_output_pipe( | |
3509 | "rev-list", @_, | |
3510 | ); | |
3511 | my @rv; | |
3512 | while ( <$msg_fh> ) { | |
3513 | chomp; | |
3514 | push @rv, $_; | |
3515 | } | |
3516 | command_close_pipe($msg_fh, $ctx); | |
3517 | @rv; | |
3518 | } | |
3519 | ||
3520 | sub check_cherry_pick { | |
3521 | my $base = shift; | |
3522 | my $tip = shift; | |
a3c75056 | 3523 | my $parents = shift; |
7a955a53 SV |
3524 | my @ranges = @_; |
3525 | my %commits = map { $_ => 1 } | |
eabd73a3 | 3526 | _rev_list("--no-merges", $tip, "--not", $base, @$parents, "--"); |
7a955a53 | 3527 | for my $range ( @ranges ) { |
eabd73a3 | 3528 | delete @commits{_rev_list($range, "--")}; |
7a955a53 | 3529 | } |
1cef6500 AM |
3530 | for my $commit (keys %commits) { |
3531 | if (has_no_changes($commit)) { | |
3532 | delete $commits{$commit}; | |
3533 | } | |
3534 | } | |
7a955a53 SV |
3535 | return (keys %commits); |
3536 | } | |
3537 | ||
1cef6500 AM |
3538 | sub has_no_changes { |
3539 | my $commit = shift; | |
3540 | ||
3541 | my @revs = split / /, command_oneline( | |
3542 | qw(rev-list --parents -1 -m), $commit); | |
3543 | ||
3544 | # Commits with no parents, e.g. the start of a partial branch, | |
3545 | # have changes by definition. | |
3546 | return 1 if (@revs < 2); | |
3547 | ||
3548 | # Commits with multiple parents, e.g a merge, have no changes | |
3549 | # by definition. | |
3550 | return 0 if (@revs > 2); | |
3551 | ||
3552 | return (command_oneline("rev-parse", "$commit^{tree}") eq | |
3553 | command_oneline("rev-parse", "$commit~1^{tree}")); | |
3554 | } | |
3555 | ||
8bff7c53 AM |
3556 | # The GIT_DIR environment variable is not always set until after the command |
3557 | # line arguments are processed, so we can't memoize in a BEGIN block. | |
3558 | { | |
3559 | my $memoized = 0; | |
3560 | ||
3561 | sub memoize_svn_mergeinfo_functions { | |
3562 | return if $memoized; | |
3563 | $memoized = 1; | |
3564 | ||
3565 | my $cache_path = "$ENV{GIT_DIR}/svn/.caches/"; | |
3566 | mkpath([$cache_path]) unless -d $cache_path; | |
3567 | ||
3568 | tie my %lookup_svn_merge_cache => 'Memoize::Storable', | |
3569 | "$cache_path/lookup_svn_merge.db", 'nstore'; | |
3570 | memoize 'lookup_svn_merge', | |
3571 | SCALAR_CACHE => 'FAULT', | |
3572 | LIST_CACHE => ['HASH' => \%lookup_svn_merge_cache], | |
3573 | ; | |
3574 | ||
3575 | tie my %check_cherry_pick_cache => 'Memoize::Storable', | |
3576 | "$cache_path/check_cherry_pick.db", 'nstore'; | |
3577 | memoize 'check_cherry_pick', | |
3578 | SCALAR_CACHE => 'FAULT', | |
3579 | LIST_CACHE => ['HASH' => \%check_cherry_pick_cache], | |
3580 | ; | |
3581 | ||
3582 | tie my %has_no_changes_cache => 'Memoize::Storable', | |
3583 | "$cache_path/has_no_changes.db", 'nstore'; | |
3584 | memoize 'has_no_changes', | |
3585 | SCALAR_CACHE => ['HASH' => \%has_no_changes_cache], | |
3586 | LIST_CACHE => 'FAULT', | |
3587 | ; | |
3588 | } | |
8ac3a667 SV |
3589 | |
3590 | sub unmemoize_svn_mergeinfo_functions { | |
3591 | return if not $memoized; | |
3592 | $memoized = 0; | |
3593 | ||
3594 | Memoize::unmemoize 'lookup_svn_merge'; | |
3595 | Memoize::unmemoize 'check_cherry_pick'; | |
3596 | Memoize::unmemoize 'has_no_changes'; | |
3597 | } | |
f5549afd JK |
3598 | |
3599 | Memoize::memoize 'Git::SVN::repos_root'; | |
8ac3a667 SV |
3600 | } |
3601 | ||
3602 | END { | |
3603 | # Force cache writeout explicitly instead of waiting for | |
3604 | # global destruction to avoid segfault in Storable: | |
3605 | # http://rt.cpan.org/Public/Bug/Display.html?id=36087 | |
3606 | unmemoize_svn_mergeinfo_functions(); | |
7d944c33 SV |
3607 | } |
3608 | ||
ea020cbd SV |
3609 | sub parents_exclude { |
3610 | my $parents = shift; | |
3611 | my @commits = @_; | |
3612 | return unless @commits; | |
3613 | ||
3614 | my @excluded; | |
3615 | my $excluded; | |
3616 | do { | |
3617 | my @cmd = ('rev-list', "-1", @commits, "--not", @$parents ); | |
3618 | $excluded = command_oneline(@cmd); | |
3619 | if ( $excluded ) { | |
3620 | my @new; | |
3621 | my $found; | |
3622 | for my $commit ( @commits ) { | |
3623 | if ( $commit eq $excluded ) { | |
3624 | push @excluded, $commit; | |
3625 | $found++; | |
3626 | last; | |
3627 | } | |
3628 | else { | |
3629 | push @new, $commit; | |
3630 | } | |
3631 | } | |
3632 | die "saw commit '$excluded' in rev-list output, " | |
3633 | ."but we didn't ask for that commit (wanted: @commits --not @$parents)" | |
3634 | unless $found; | |
3635 | @commits = @new; | |
3636 | } | |
3637 | } | |
3638 | while ($excluded and @commits); | |
3639 | ||
3640 | return @excluded; | |
3641 | } | |
3642 | ||
3643 | ||
dff589ef SV |
3644 | # note: this function should only be called if the various dirprops |
3645 | # have actually changed | |
3646 | sub find_extra_svn_parents { | |
3647 | my ($self, $ed, $mergeinfo, $parents) = @_; | |
3648 | # aha! svk:merge property changed... | |
3649 | ||
8bff7c53 AM |
3650 | memoize_svn_mergeinfo_functions(); |
3651 | ||
dff589ef SV |
3652 | # We first search for merged tips which are not in our |
3653 | # history. Then, we figure out which git revisions are in | |
3654 | # that tip, but not this revision. If all of those revisions | |
3655 | # are now marked as merge, we can add the tip as a parent. | |
3656 | my @merges = split "\n", $mergeinfo; | |
3657 | my @merge_tips; | |
bf60fff8 | 3658 | my $url = $self->{url}; |
7d944c33 | 3659 | my $uuid = $self->ra_uuid; |
ea020cbd | 3660 | my %ranges; |
dff589ef | 3661 | for my $merge ( @merges ) { |
7d944c33 SV |
3662 | my ($tip_commit, @ranges) = |
3663 | lookup_svn_merge( $uuid, $url, $merge ); | |
dff589ef SV |
3664 | unless (!$tip_commit or |
3665 | grep { $_ eq $tip_commit } @$parents ) { | |
3666 | push @merge_tips, $tip_commit; | |
ea020cbd | 3667 | $ranges{$tip_commit} = \@ranges; |
dff589ef SV |
3668 | } else { |
3669 | push @merge_tips, undef; | |
3670 | } | |
3671 | } | |
ea020cbd SV |
3672 | |
3673 | my %excluded = map { $_ => 1 } | |
3674 | parents_exclude($parents, grep { defined } @merge_tips); | |
3675 | ||
3676 | # check merge tips for new parents | |
3677 | my @new_parents; | |
dff589ef SV |
3678 | for my $merge_tip ( @merge_tips ) { |
3679 | my $spec = shift @merges; | |
ea020cbd SV |
3680 | next unless $merge_tip and $excluded{$merge_tip}; |
3681 | ||
3682 | my $ranges = $ranges{$merge_tip}; | |
3683 | ||
7a955a53 | 3684 | # check out 'new' tips |
41c01693 AM |
3685 | my $merge_base; |
3686 | eval { | |
3687 | $merge_base = command_oneline( | |
3688 | "merge-base", | |
3689 | @$parents, $merge_tip, | |
3690 | ); | |
3691 | }; | |
3692 | if ($@) { | |
3693 | die "An error occurred during merge-base" | |
3694 | unless $@->isa("Git::Error::Command"); | |
3695 | ||
3696 | warn "W: Cannot find common ancestor between ". | |
3697 | "@$parents and $merge_tip. Ignoring merge info.\n"; | |
3698 | next; | |
3699 | } | |
7a955a53 SV |
3700 | |
3701 | # double check that there are no missing non-merge commits | |
3702 | my (@incomplete) = check_cherry_pick( | |
3703 | $merge_base, $merge_tip, | |
a3c75056 | 3704 | $parents, |
7a955a53 SV |
3705 | @$ranges, |
3706 | ); | |
3707 | ||
3708 | if ( @incomplete ) { | |
3709 | warn "W:svn cherry-pick ignored ($spec) - missing " | |
3710 | .@incomplete." commit(s) (eg $incomplete[0])\n"; | |
3711 | } else { | |
3712 | warn | |
3713 | "Found merge parent (svn:mergeinfo prop): ", | |
3714 | $merge_tip, "\n"; | |
3715 | push @new_parents, $merge_tip; | |
3716 | } | |
3717 | } | |
3718 | ||
3719 | # cater for merges which merge commits from multiple branches | |
3720 | if ( @new_parents > 1 ) { | |
3721 | for ( my $i = 0; $i <= $#new_parents; $i++ ) { | |
3722 | for ( my $j = 0; $j <= $#new_parents; $j++ ) { | |
3723 | next if $i == $j; | |
3724 | next unless $new_parents[$i]; | |
3725 | next unless $new_parents[$j]; | |
3726 | my $revs = command_oneline( | |
0fe19753 EW |
3727 | "rev-list", "-1", |
3728 | "$new_parents[$i]..$new_parents[$j]", | |
7a955a53 SV |
3729 | ); |
3730 | if ( !$revs ) { | |
6a2009e7 | 3731 | undef($new_parents[$j]); |
7a955a53 | 3732 | } |
dff589ef SV |
3733 | } |
3734 | } | |
3735 | } | |
7a955a53 | 3736 | push @$parents, grep { defined } @new_parents; |
dff589ef SV |
3737 | } |
3738 | ||
9b981fc6 | 3739 | sub make_log_entry { |
97f6987a EW |
3740 | my ($self, $rev, $parents, $ed) = @_; |
3741 | my $untracked = $self->get_untracked($ed); | |
3742 | ||
f1264bd6 SV |
3743 | my @parents = @$parents; |
3744 | my $ps = $ed->{path_strip} || ""; | |
3745 | for my $path ( grep { m/$ps/ } %{$ed->{dir_prop}} ) { | |
3746 | my $props = $ed->{dir_prop}{$path}; | |
3747 | if ( $props->{"svk:merge"} ) { | |
3748 | $self->find_extra_svk_parents | |
3749 | ($ed, $props->{"svk:merge"}, \@parents); | |
3750 | } | |
dff589ef SV |
3751 | if ( $props->{"svn:mergeinfo"} ) { |
3752 | $self->find_extra_svn_parents | |
3753 | ($ed, | |
3754 | $props->{"svn:mergeinfo"}, | |
3755 | \@parents); | |
3756 | } | |
f1264bd6 SV |
3757 | } |
3758 | ||
9b981fc6 | 3759 | open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!; |
97f6987a EW |
3760 | print $un "r$rev\n" or croak $!; |
3761 | print $un $_, "\n" foreach @$untracked; | |
f1264bd6 | 3762 | my %log_entry = ( parents => \@parents, revision => $rev, |
97f6987a | 3763 | log => ''); |
fbcc1737 | 3764 | |
8a49ee97 | 3765 | my $headrev; |
fbcc1737 | 3766 | my $logged = delete $self->{logged_rev_props}; |
8a49ee97 | 3767 | if (!$logged || $self->{-want_revprops}) { |
fbcc1737 EW |
3768 | my $rp = $self->ra->rev_proplist($rev); |
3769 | foreach (sort keys %$rp) { | |
3770 | my $v = $rp->{$_}; | |
3771 | if (/^svn:(author|date|log)$/) { | |
3772 | $log_entry{$1} = $v; | |
8a49ee97 EW |
3773 | } elsif ($_ eq 'svm:headrev') { |
3774 | $headrev = $v; | |
fbcc1737 EW |
3775 | } else { |
3776 | print $un " rev_prop: ", uri_encode($_), ' ', | |
3777 | uri_encode($v), "\n"; | |
3778 | } | |
9b981fc6 | 3779 | } |
fbcc1737 EW |
3780 | } else { |
3781 | map { $log_entry{$_} = $logged->{$_} } keys %$logged; | |
9b981fc6 EW |
3782 | } |
3783 | close $un or croak $!; | |
97f6987a | 3784 | |
9b981fc6 | 3785 | $log_entry{date} = parse_svn_date($log_entry{date}); |
9b981fc6 | 3786 | $log_entry{log} .= "\n"; |
db03cd24 EW |
3787 | my $author = $log_entry{author} = check_author($log_entry{author}); |
3788 | my ($name, $email) = defined $::users{$author} ? @{$::users{$author}} | |
70ae04e4 AW |
3789 | : ($author, undef); |
3790 | ||
3791 | my ($commit_name, $commit_email) = ($name, $email); | |
3792 | if ($_use_log_author) { | |
5ff6aae8 AW |
3793 | my $name_field; |
3794 | if ($log_entry{log} =~ /From:\s+(.*\S)\s*\n/i) { | |
3795 | $name_field = $1; | |
3796 | } elsif ($log_entry{log} =~ /Signed-off-by:\s+(.*\S)\s*\n/i) { | |
3797 | $name_field = $1; | |
3798 | } | |
3799 | if (!defined $name_field) { | |
abfa533d SB |
3800 | if (!defined $email) { |
3801 | $email = $name; | |
3802 | } | |
5ff6aae8 | 3803 | } elsif ($name_field =~ /(.*?)\s+<(.*)>/) { |
70ae04e4 | 3804 | ($name, $email) = ($1, $2); |
5ff6aae8 AW |
3805 | } elsif ($name_field =~ /(.*)@/) { |
3806 | ($name, $email) = ($1, $name_field); | |
3807 | } else { | |
abfa533d | 3808 | ($name, $email) = ($name_field, $name_field); |
70ae04e4 AW |
3809 | } |
3810 | } | |
91b03282 | 3811 | if (defined $headrev && $self->use_svm_props) { |
aea736cc EW |
3812 | if ($self->rewrite_root) { |
3813 | die "Can't have both 'useSvmProps' and 'rewriteRoot' ", | |
3814 | "options set!\n"; | |
3815 | } | |
3e18ce1a JS |
3816 | if ($self->rewrite_uuid) { |
3817 | die "Can't have both 'useSvmProps' and 'rewriteUUID' ", | |
3818 | "options set!\n"; | |
3819 | } | |
b3e95936 | 3820 | my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d+)$}i; |
befc9adc EW |
3821 | # we don't want "SVM: initializing mirror for junk" ... |
3822 | return undef if $r == 0; | |
3823 | my $svm = $self->svm; | |
3824 | if ($uuid ne $svm->{uuid}) { | |
8a49ee97 | 3825 | die "UUID mismatch on SVM path:\n", |
befc9adc | 3826 | "expected: $svm->{uuid}\n", |
8a49ee97 EW |
3827 | " got: $uuid\n"; |
3828 | } | |
befc9adc EW |
3829 | my $full_url = $self->full_url; |
3830 | $full_url =~ s#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or | |
3831 | die "Failed to replace '$svm->{replace}' with ", | |
3832 | "'$svm->{source}' in $full_url\n"; | |
18ea92bd SV |
3833 | # throw away username for storing in records |
3834 | remove_username($full_url); | |
8a49ee97 EW |
3835 | $log_entry{metadata} = "$full_url\@$r $uuid"; |
3836 | $log_entry{svm_revision} = $r; | |
70ae04e4 AW |
3837 | $email ||= "$author\@$uuid"; |
3838 | $commit_email ||= "$author\@$uuid"; | |
62e349d2 EW |
3839 | } elsif ($self->use_svnsync_props) { |
3840 | my $full_url = $self->svnsync->{url}; | |
3841 | $full_url .= "/$self->{path}" if length $self->{path}; | |
ce118739 | 3842 | remove_username($full_url); |
62e349d2 EW |
3843 | my $uuid = $self->svnsync->{uuid}; |
3844 | $log_entry{metadata} = "$full_url\@$rev $uuid"; | |
70ae04e4 AW |
3845 | $email ||= "$author\@$uuid"; |
3846 | $commit_email ||= "$author\@$uuid"; | |
8a49ee97 | 3847 | } else { |
ce118739 AR |
3848 | my $url = $self->metadata_url; |
3849 | remove_username($url); | |
3e18ce1a JS |
3850 | my $uuid = $self->rewrite_uuid || $self->ra->get_uuid; |
3851 | $log_entry{metadata} = "$url\@$rev " . $uuid; | |
3852 | $email ||= "$author\@" . $uuid; | |
3853 | $commit_email ||= "$author\@" . $uuid; | |
8a49ee97 | 3854 | } |
db03cd24 EW |
3855 | $log_entry{name} = $name; |
3856 | $log_entry{email} = $email; | |
70ae04e4 AW |
3857 | $log_entry{commit_name} = $commit_name; |
3858 | $log_entry{commit_email} = $commit_email; | |
9b981fc6 EW |
3859 | \%log_entry; |
3860 | } | |
3861 | ||
3862 | sub fetch { | |
3ebe8df7 | 3863 | my ($self, $min_rev, $max_rev, @parents) = @_; |
9b981fc6 | 3864 | my ($last_rev, $last_commit) = $self->last_rev_commit; |
3ebe8df7 | 3865 | my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev); |
e518192f | 3866 | $self->ra->gs_fetch_loop_common($base, $head, [$self]); |
9b981fc6 EW |
3867 | } |
3868 | ||
3869 | sub set_tree_cb { | |
3870 | my ($self, $log_entry, $tree, $rev, $date, $author) = @_; | |
490f49ea EW |
3871 | $self->{inject_parents} = { $rev => $tree }; |
3872 | $self->fetch(undef, undef); | |
9b981fc6 EW |
3873 | } |
3874 | ||
3875 | sub set_tree { | |
3876 | my ($self, $tree) = (shift, shift); | |
1ce255dc | 3877 | my $log_entry = ::get_commit_entry($tree); |
9b981fc6 | 3878 | unless ($self->{last_rev}) { |
0a1a1c86 | 3879 | ::fatal("Must have an existing revision to commit"); |
9b981fc6 | 3880 | } |
61395354 EW |
3881 | my %ed_opts = ( r => $self->{last_rev}, |
3882 | log => $log_entry->{log}, | |
3883 | ra => $self->ra, | |
3884 | tree_a => $self->{last_commit}, | |
3885 | tree_b => $tree, | |
3886 | editor_cb => sub { | |
3887 | $self->set_tree_cb($log_entry, $tree, @_) }, | |
3888 | svn_path => $self->{path} ); | |
3889 | if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) { | |
9b981fc6 EW |
3890 | print "No changes\nr$self->{last_rev} = $tree\n"; |
3891 | } | |
9b981fc6 EW |
3892 | } |
3893 | ||
060610c5 EW |
3894 | sub rebuild_from_rev_db { |
3895 | my ($self, $path) = @_; | |
3896 | my $r = -1; | |
3897 | open my $fh, '<', $path or croak "open: $!"; | |
4f7ec797 | 3898 | binmode $fh or croak "binmode: $!"; |
060610c5 EW |
3899 | while (<$fh>) { |
3900 | length($_) == 41 or croak "inconsistent size in ($_) != 41"; | |
3901 | chomp($_); | |
3902 | ++$r; | |
3903 | next if $_ eq ('0' x 40); | |
3904 | $self->rev_map_set($r, $_); | |
3905 | print "r$r = $_\n"; | |
3906 | } | |
3907 | close $fh or croak "close: $!"; | |
3908 | unlink $path or croak "unlink: $!"; | |
3909 | } | |
3910 | ||
f0ecca10 EW |
3911 | sub rebuild { |
3912 | my ($self) = @_; | |
060610c5 | 3913 | my $map_path = $self->map_path; |
2beec897 | 3914 | my $partial = (-e $map_path && ! -z $map_path); |
d6d3346b | 3915 | return unless ::verify_ref($self->refname.'^0'); |
2beec897 | 3916 | if (!$partial && ($self->use_svm_props || $self->no_metadata)) { |
060610c5 EW |
3917 | my $rev_db = $self->rev_db_path; |
3918 | $self->rebuild_from_rev_db($rev_db); | |
3919 | if ($self->use_svm_props) { | |
3920 | my $svm_rev_db = $self->rev_db_path($self->svm_uuid); | |
3921 | $self->rebuild_from_rev_db($svm_rev_db); | |
3922 | } | |
3923 | $self->unlink_rev_db_symlink; | |
26a62d57 EW |
3924 | return; |
3925 | } | |
2beec897 DM |
3926 | print "Rebuilding $map_path ...\n" if (!$partial); |
3927 | my ($base_rev, $head) = ($partial ? $self->rev_map_max_norebuild(1) : | |
3928 | (undef, undef)); | |
060610c5 | 3929 | my ($log, $ctx) = |
b380e3a7 | 3930 | command_output_pipe(qw/rev-list --pretty=raw --reverse/, |
2beec897 DM |
3931 | ($head ? "$head.." : "") . $self->refname, |
3932 | '--'); | |
74b1e123 JK |
3933 | my $metadata_url = $self->metadata_url; |
3934 | remove_username($metadata_url); | |
3e18ce1a | 3935 | my $svn_uuid = $self->rewrite_uuid || $self->ra_uuid; |
3dfab993 SV |
3936 | my $c; |
3937 | while (<$log>) { | |
3938 | if ( m{^commit ($::sha1)$} ) { | |
3939 | $c = $1; | |
3940 | next; | |
3941 | } | |
3942 | next unless s{^\s*(git-svn-id:)}{$1}; | |
3943 | my ($url, $rev, $uuid) = ::extract_metadata($_); | |
18ea92bd | 3944 | remove_username($url); |
f0ecca10 EW |
3945 | |
3946 | # ignore merges (from set-tree) | |
3947 | next if (!defined $rev || !$uuid); | |
3948 | ||
3949 | # if we merged or otherwise started elsewhere, this is | |
3950 | # how we break out of it | |
060610c5 | 3951 | if (($uuid ne $svn_uuid) || |
74b1e123 | 3952 | ($metadata_url && $url && ($url ne $metadata_url))) { |
f0ecca10 EW |
3953 | next; |
3954 | } | |
2beec897 DM |
3955 | if ($partial && $head) { |
3956 | print "Partial-rebuilding $map_path ...\n"; | |
3957 | print "Currently at $base_rev = $head\n"; | |
3958 | $head = undef; | |
3959 | } | |
f0ecca10 | 3960 | |
060610c5 | 3961 | $self->rev_map_set($rev, $c); |
f0ecca10 EW |
3962 | print "r$rev = $c\n"; |
3963 | } | |
3dfab993 | 3964 | command_close_pipe($log, $ctx); |
2beec897 | 3965 | print "Done rebuilding $map_path\n" if (!$partial || !$head); |
060610c5 EW |
3966 | my $rev_db_path = $self->rev_db_path; |
3967 | if (-f $self->rev_db_path) { | |
3968 | unlink $self->rev_db_path or croak "unlink: $!"; | |
3969 | } | |
3970 | $self->unlink_rev_db_symlink; | |
f0ecca10 EW |
3971 | } |
3972 | ||
060610c5 | 3973 | # rev_map: |
9b981fc6 EW |
3974 | # Tie::File seems to be prone to offset errors if revisions get sparse, |
3975 | # it's not that fast, either. Tie::File is also not in Perl 5.6. So | |
3976 | # one of my favorite modules is out :< Next up would be one of the DBM | |
060610c5 EW |
3977 | # modules, but I'm not sure which is most portable... |
3978 | # | |
3979 | # This is the replacement for the rev_db format, which was too big | |
3980 | # and inefficient for large repositories with a lot of sparse history | |
3981 | # (mainly tags) | |
3982 | # | |
3983 | # The format is this: | |
3984 | # - 24 bytes for every record, | |
3985 | # * 4 bytes for the integer representing an SVN revision number | |
3986 | # * 20 bytes representing the sha1 of a git commit | |
3987 | # - No empty padding records like the old format | |
66ab84b9 | 3988 | # (except the last record, which can be overwritten) |
060610c5 EW |
3989 | # - new records are written append-only since SVN revision numbers |
3990 | # increase monotonically | |
3991 | # - lookups on SVN revision number are done via a binary search | |
66ab84b9 EW |
3992 | # - Piping the file to xxd -c24 is a good way of dumping it for |
3993 | # viewing or editing (piped back through xxd -r), should the need | |
3994 | # ever arise. | |
3995 | # - The last record can be padding revision with an all-zero sha1 | |
3996 | # This is used to optimize fetch performance when using multiple | |
3997 | # "fetch" directives in .git/config | |
060610c5 | 3998 | # |
97ae0911 | 3999 | # These files are disposable unless noMetadata or useSvmProps is set |
9b981fc6 | 4000 | |
060610c5 | 4001 | sub _rev_map_set { |
26a62d57 | 4002 | my ($fh, $rev, $commit) = @_; |
060610c5 | 4003 | |
4f7ec797 | 4004 | binmode $fh or croak "binmode: $!"; |
060610c5 EW |
4005 | my $size = (stat($fh))[7]; |
4006 | ($size % 24) == 0 or croak "inconsistent size: $size"; | |
4007 | ||
66ab84b9 | 4008 | my $wr_offset = 0; |
060610c5 EW |
4009 | if ($size > 0) { |
4010 | sysseek($fh, -24, SEEK_END) or croak "seek: $!"; | |
4011 | my $read = sysread($fh, my $buf, 24) or croak "read: $!"; | |
4012 | $read == 24 or croak "read only $read bytes (!= 24)"; | |
4013 | my ($last_rev, $last_commit) = unpack(rev_map_fmt, $buf); | |
66ab84b9 EW |
4014 | if ($last_commit eq ('0' x40)) { |
4015 | if ($size >= 48) { | |
4016 | sysseek($fh, -48, SEEK_END) or croak "seek: $!"; | |
4017 | $read = sysread($fh, $buf, 24) or | |
4018 | croak "read: $!"; | |
4019 | $read == 24 or | |
4020 | croak "read only $read bytes (!= 24)"; | |
4021 | ($last_rev, $last_commit) = | |
4022 | unpack(rev_map_fmt, $buf); | |
4023 | if ($last_commit eq ('0' x40)) { | |
4024 | croak "inconsistent .rev_map\n"; | |
4025 | } | |
4026 | } | |
4027 | if ($last_rev >= $rev) { | |
4028 | croak "last_rev is higher!: $last_rev >= $rev"; | |
4029 | } | |
4030 | $wr_offset = -24; | |
26a62d57 EW |
4031 | } |
4032 | } | |
66ab84b9 | 4033 | sysseek($fh, $wr_offset, SEEK_END) or croak "seek: $!"; |
060610c5 EW |
4034 | syswrite($fh, pack(rev_map_fmt, $rev, $commit), 24) == 24 or |
4035 | croak "write: $!"; | |
26a62d57 EW |
4036 | } |
4037 | ||
195643f2 BJ |
4038 | sub _rev_map_reset { |
4039 | my ($fh, $rev, $commit) = @_; | |
4040 | my $c = _rev_map_get($fh, $rev); | |
4041 | $c eq $commit or die "_rev_map_reset(@_) commit $c does not match!\n"; | |
4042 | my $offset = sysseek($fh, 0, SEEK_CUR) or croak "seek: $!"; | |
4043 | truncate $fh, $offset or croak "truncate: $!"; | |
4044 | } | |
4045 | ||
26a62d57 EW |
4046 | sub mkfile { |
4047 | my ($path) = @_; | |
4048 | unless (-e $path) { | |
4049 | my ($dir, $base) = ($path =~ m#^(.*?)/?([^/]+)$#); | |
4050 | mkpath([$dir]) unless -d $dir; | |
4051 | open my $fh, '>>', $path or die "Couldn't create $path: $!\n"; | |
4052 | close $fh or die "Couldn't close (create) $path: $!\n"; | |
4053 | } | |
4054 | } | |
4055 | ||
060610c5 | 4056 | sub rev_map_set { |
26a62d57 | 4057 | my ($self, $rev, $commit, $update_ref, $uuid) = @_; |
70ee0b77 | 4058 | defined $commit or die "missing arg3\n"; |
26a62d57 | 4059 | length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n"; |
060610c5 | 4060 | my $db = $self->map_path($uuid); |
26a62d57 | 4061 | my $db_lock = "$db.lock"; |
373274f9 | 4062 | my $sig; |
195643f2 | 4063 | $update_ref ||= 0; |
373274f9 EW |
4064 | if ($update_ref) { |
4065 | $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} = | |
4066 | $SIG{USR1} = $SIG{USR2} = sub { $sig = $_[0] }; | |
4067 | } | |
26a62d57 EW |
4068 | mkfile($db); |
4069 | ||
373274f9 | 4070 | $LOCKFILES{$db_lock} = 1; |
97ae0911 | 4071 | my $sync; |
97ae0911 EW |
4072 | # both of these options make our .rev_db file very, very important |
4073 | # and we can't afford to lose it because rebuild() won't work | |
4074 | if ($self->use_svm_props || $self->no_metadata) { | |
4075 | $sync = 1; | |
060610c5 | 4076 | copy($db, $db_lock) or die "rev_map_set(@_): ", |
26a62d57 | 4077 | "Failed to copy: ", |
373274f9 EW |
4078 | "$db => $db_lock ($!)\n"; |
4079 | } else { | |
060610c5 | 4080 | rename $db, $db_lock or die "rev_map_set(@_): ", |
26a62d57 | 4081 | "Failed to rename: ", |
373274f9 EW |
4082 | "$db => $db_lock ($!)\n"; |
4083 | } | |
060610c5 | 4084 | |
66ab84b9 | 4085 | sysopen(my $fh, $db_lock, O_RDWR | O_CREAT) |
060610c5 | 4086 | or croak "Couldn't open $db_lock: $!\n"; |
195643f2 BJ |
4087 | $update_ref eq 'reset' ? _rev_map_reset($fh, $rev, $commit) : |
4088 | _rev_map_set($fh, $rev, $commit); | |
97ae0911 EW |
4089 | if ($sync) { |
4090 | $fh->flush or die "Couldn't flush $db_lock: $!\n"; | |
4091 | $fh->sync or die "Couldn't sync $db_lock: $!\n"; | |
4092 | } | |
9b981fc6 | 4093 | close $fh or croak $!; |
373274f9 | 4094 | if ($update_ref) { |
1e889ef3 | 4095 | $_head = $self; |
195643f2 BJ |
4096 | my $note = ""; |
4097 | $note = " ($update_ref)" if ($update_ref !~ /^\d*$/); | |
4098 | command_noisy('update-ref', '-m', "r$rev$note", | |
373274f9 EW |
4099 | $self->refname, $commit); |
4100 | } | |
060610c5 | 4101 | rename $db_lock, $db or die "rev_map_set(@_): ", "Failed to rename: ", |
373274f9 EW |
4102 | "$db_lock => $db ($!)\n"; |
4103 | delete $LOCKFILES{$db_lock}; | |
4104 | if ($update_ref) { | |
4105 | $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} = | |
4106 | $SIG{USR1} = $SIG{USR2} = 'DEFAULT'; | |
4107 | kill $sig, $$ if defined $sig; | |
4108 | } | |
9b981fc6 EW |
4109 | } |
4110 | ||
66ab84b9 EW |
4111 | # If want_commit, this will return an array of (rev, commit) where |
4112 | # commit _must_ be a valid commit in the archive. | |
4113 | # Otherwise, it'll return the max revision (whether or not the | |
4114 | # commit is valid or just a 0x40 placeholder). | |
060610c5 | 4115 | sub rev_map_max { |
66ab84b9 | 4116 | my ($self, $want_commit) = @_; |
d6d3346b | 4117 | $self->rebuild; |
2beec897 DM |
4118 | my ($r, $c) = $self->rev_map_max_norebuild($want_commit); |
4119 | $want_commit ? ($r, $c) : $r; | |
4120 | } | |
4121 | ||
4122 | sub rev_map_max_norebuild { | |
4123 | my ($self, $want_commit) = @_; | |
060610c5 | 4124 | my $map_path = $self->map_path; |
66ab84b9 | 4125 | stat $map_path or return $want_commit ? (0, undef) : 0; |
060610c5 | 4126 | sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!"; |
4f7ec797 | 4127 | binmode $fh or croak "binmode: $!"; |
060610c5 EW |
4128 | my $size = (stat($fh))[7]; |
4129 | ($size % 24) == 0 or croak "inconsistent size: $size"; | |
4130 | ||
4131 | if ($size == 0) { | |
4132 | close $fh or croak "close: $!"; | |
66ab84b9 | 4133 | return $want_commit ? (0, undef) : 0; |
060610c5 EW |
4134 | } |
4135 | ||
66ab84b9 | 4136 | sysseek($fh, -24, SEEK_END) or croak "seek: $!"; |
060610c5 | 4137 | sysread($fh, my $buf, 24) == 24 or croak "read: $!"; |
060610c5 | 4138 | my ($r, $c) = unpack(rev_map_fmt, $buf); |
66ab84b9 EW |
4139 | if ($want_commit && $c eq ('0' x40)) { |
4140 | if ($size < 48) { | |
4141 | return $want_commit ? (0, undef) : 0; | |
4142 | } | |
4143 | sysseek($fh, -48, SEEK_END) or croak "seek: $!"; | |
4144 | sysread($fh, $buf, 24) == 24 or croak "read: $!"; | |
4145 | ($r, $c) = unpack(rev_map_fmt, $buf); | |
4146 | if ($c eq ('0'x40)) { | |
4147 | croak "Penultimate record is all-zeroes in $map_path"; | |
4148 | } | |
4149 | } | |
4150 | close $fh or croak "close: $!"; | |
4151 | $want_commit ? ($r, $c) : $r; | |
9c93fee5 EW |
4152 | } |
4153 | ||
060610c5 | 4154 | sub rev_map_get { |
26a62d57 | 4155 | my ($self, $rev, $uuid) = @_; |
060610c5 EW |
4156 | my $map_path = $self->map_path($uuid); |
4157 | return undef unless -e $map_path; | |
4158 | ||
4159 | sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!"; | |
195643f2 BJ |
4160 | my $c = _rev_map_get($fh, $rev); |
4161 | close($fh) or croak "close: $!"; | |
4162 | $c | |
4163 | } | |
4164 | ||
4165 | sub _rev_map_get { | |
4166 | my ($fh, $rev) = @_; | |
4167 | ||
4f7ec797 | 4168 | binmode $fh or croak "binmode: $!"; |
060610c5 EW |
4169 | my $size = (stat($fh))[7]; |
4170 | ($size % 24) == 0 or croak "inconsistent size: $size"; | |
4171 | ||
4172 | if ($size == 0) { | |
060610c5 EW |
4173 | return undef; |
4174 | } | |
4175 | ||
4176 | my ($l, $u) = (0, $size - 24); | |
4177 | my ($r, $c, $buf); | |
4178 | ||
4179 | while ($l <= $u) { | |
4180 | my $i = int(($l/24 + $u/24) / 2) * 24; | |
4181 | sysseek($fh, $i, SEEK_SET) or croak "seek: $!"; | |
4182 | sysread($fh, my $buf, 24) == 24 or croak "read: $!"; | |
c83f4e68 | 4183 | my ($r, $c) = unpack(rev_map_fmt, $buf); |
060610c5 EW |
4184 | |
4185 | if ($r < $rev) { | |
4186 | $l = $i + 24; | |
4187 | } elsif ($r > $rev) { | |
4188 | $u = $i - 24; | |
4189 | } else { # $r == $rev | |
66ab84b9 | 4190 | return $c eq ('0' x 40) ? undef : $c; |
060610c5 | 4191 | } |
9b981fc6 | 4192 | } |
060610c5 | 4193 | undef; |
9b981fc6 EW |
4194 | } |
4195 | ||
111947ef DK |
4196 | # Finds the first svn revision that exists on (if $eq_ok is true) or |
4197 | # before $rev for the current branch. It will not search any lower | |
4198 | # than $min_rev. Returns the git commit hash and svn revision number | |
4199 | # if found, else (undef, undef). | |
15710b6f | 4200 | sub find_rev_before { |
111947ef | 4201 | my ($self, $rev, $eq_ok, $min_rev) = @_; |
15710b6f | 4202 | --$rev unless $eq_ok; |
111947ef | 4203 | $min_rev ||= 1; |
ca5e880e BJ |
4204 | my $max_rev = $self->rev_map_max; |
4205 | $rev = $max_rev if ($rev > $max_rev); | |
111947ef | 4206 | while ($rev >= $min_rev) { |
060610c5 | 4207 | if (my $c = $self->rev_map_get($rev)) { |
15710b6f EW |
4208 | return ($rev, $c); |
4209 | } | |
4210 | --$rev; | |
4211 | } | |
4212 | return (undef, undef); | |
4213 | } | |
4214 | ||
111947ef DK |
4215 | # Finds the first svn revision that exists on (if $eq_ok is true) or |
4216 | # after $rev for the current branch. It will not search any higher | |
4217 | # than $max_rev. Returns the git commit hash and svn revision number | |
4218 | # if found, else (undef, undef). | |
4219 | sub find_rev_after { | |
4220 | my ($self, $rev, $eq_ok, $max_rev) = @_; | |
4221 | ++$rev unless $eq_ok; | |
060610c5 | 4222 | $max_rev ||= $self->rev_map_max; |
111947ef | 4223 | while ($rev <= $max_rev) { |
060610c5 | 4224 | if (my $c = $self->rev_map_get($rev)) { |
111947ef DK |
4225 | return ($rev, $c); |
4226 | } | |
4227 | ++$rev; | |
4228 | } | |
4229 | return (undef, undef); | |
4230 | } | |
4231 | ||
9b981fc6 | 4232 | sub _new { |
706587fc EW |
4233 | my ($class, $repo_id, $ref_id, $path) = @_; |
4234 | unless (defined $repo_id && length $repo_id) { | |
4235 | $repo_id = $Git::SVN::default_repo_id; | |
4236 | } | |
4237 | unless (defined $ref_id && length $ref_id) { | |
63de84ad | 4238 | $_prefix = '' unless defined($_prefix); |
6f5748e1 AB |
4239 | $_[2] = $ref_id = |
4240 | "refs/remotes/$_prefix$Git::SVN::default_ref_id"; | |
706587fc | 4241 | } |
7829f20f | 4242 | $_[1] = $repo_id; |
706587fc | 4243 | my $dir = "$ENV{GIT_DIR}/svn/$ref_id"; |
6f5748e1 AB |
4244 | |
4245 | # Older repos imported by us used $GIT_DIR/svn/foo instead of | |
4246 | # $GIT_DIR/svn/refs/remotes/foo when tracking refs/remotes/foo | |
4247 | if ($ref_id =~ m{^refs/remotes/(.*)}) { | |
4248 | my $old_dir = "$ENV{GIT_DIR}/svn/$1"; | |
4249 | if (-d $old_dir && ! -d $dir) { | |
4250 | $dir = $old_dir; | |
4251 | } | |
4252 | } | |
4253 | ||
706587fc | 4254 | $_[3] = $path = '' unless (defined $path); |
6f5748e1 | 4255 | mkpath([$dir]); |
26a62d57 EW |
4256 | bless { |
4257 | ref_id => $ref_id, dir => $dir, index => "$dir/index", | |
8a49ee97 | 4258 | path => $path, config => "$ENV{GIT_DIR}/svn/config", |
060610c5 | 4259 | map_root => "$dir/.rev_map", repo_id => $repo_id }, $class; |
26a62d57 EW |
4260 | } |
4261 | ||
060610c5 EW |
4262 | # for read-only access of old .rev_db formats |
4263 | sub unlink_rev_db_symlink { | |
4264 | my ($self) = @_; | |
4265 | my $link = $self->rev_db_path; | |
4266 | $link =~ s/\.[\w-]+$// or croak "missing UUID at the end of $link"; | |
4267 | if (-l $link) { | |
4268 | unlink $link or croak "unlink: $link failed!"; | |
4269 | } | |
4270 | } | |
4271 | ||
4272 | sub rev_db_path { | |
4273 | my ($self, $uuid) = @_; | |
4274 | my $db_path = $self->map_path($uuid); | |
4275 | $db_path =~ s{/\.rev_map\.}{/\.rev_db\.} | |
4276 | or croak "map_path: $db_path does not contain '/.rev_map.' !"; | |
4277 | $db_path; | |
4278 | } | |
4279 | ||
4280 | # the new replacement for .rev_db | |
4281 | sub map_path { | |
26a62d57 EW |
4282 | my ($self, $uuid) = @_; |
4283 | $uuid ||= $self->ra_uuid; | |
060610c5 | 4284 | "$self->{map_root}.$uuid"; |
9b981fc6 EW |
4285 | } |
4286 | ||
1c8443b0 EW |
4287 | sub uri_encode { |
4288 | my ($f) = @_; | |
4289 | $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg; | |
4290 | $f | |
4291 | } | |
9b981fc6 | 4292 | |
6111b934 EW |
4293 | sub uri_decode { |
4294 | my ($f) = @_; | |
4295 | $f =~ s#%([0-9a-fA-F]{2})#chr(hex($1))#eg; | |
4296 | $f | |
4297 | } | |
4298 | ||
18ea92bd SV |
4299 | sub remove_username { |
4300 | $_[0] =~ s{^([^:]*://)[^@]+@}{$1}; | |
4301 | } | |
4302 | ||
d976acfd EW |
4303 | package Git::SVN::Prompt; |
4304 | use strict; | |
4305 | use warnings; | |
4306 | require SVN::Core; | |
4307 | use vars qw/$_no_auth_cache $_username/; | |
4308 | ||
4309 | sub simple { | |
30d055aa EW |
4310 | my ($cred, $realm, $default_username, $may_save, $pool) = @_; |
4311 | $may_save = undef if $_no_auth_cache; | |
4312 | $default_username = $_username if defined $_username; | |
4313 | if (defined $default_username && length $default_username) { | |
4314 | if (defined $realm && length $realm) { | |
6f729591 EW |
4315 | print STDERR "Authentication realm: $realm\n"; |
4316 | STDERR->flush; | |
30d055aa EW |
4317 | } |
4318 | $cred->username($default_username); | |
4319 | } else { | |
d976acfd | 4320 | username($cred, $realm, $may_save, $pool); |
30d055aa EW |
4321 | } |
4322 | $cred->password(_read_password("Password for '" . | |
4323 | $cred->username . "': ", $realm)); | |
4324 | $cred->may_save($may_save); | |
4325 | $SVN::_Core::SVN_NO_ERROR; | |
4326 | } | |
4327 | ||
d976acfd | 4328 | sub ssl_server_trust { |
30d055aa EW |
4329 | my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_; |
4330 | $may_save = undef if $_no_auth_cache; | |
6f729591 | 4331 | print STDERR "Error validating server certificate for '$realm':\n"; |
fd499bcc ER |
4332 | { |
4333 | no warnings 'once'; | |
4334 | # All variables SVN::Auth::SSL::* are used only once, | |
4335 | # so we're shutting up Perl warnings about this. | |
4336 | if ($failures & $SVN::Auth::SSL::UNKNOWNCA) { | |
4337 | print STDERR " - The certificate is not issued ", | |
4338 | "by a trusted authority. Use the\n", | |
4339 | " fingerprint to validate ", | |
4340 | "the certificate manually!\n"; | |
4341 | } | |
4342 | if ($failures & $SVN::Auth::SSL::CNMISMATCH) { | |
4343 | print STDERR " - The certificate hostname ", | |
4344 | "does not match.\n"; | |
4345 | } | |
4346 | if ($failures & $SVN::Auth::SSL::NOTYETVALID) { | |
4347 | print STDERR " - The certificate is not yet valid.\n"; | |
4348 | } | |
4349 | if ($failures & $SVN::Auth::SSL::EXPIRED) { | |
4350 | print STDERR " - The certificate has expired.\n"; | |
4351 | } | |
4352 | if ($failures & $SVN::Auth::SSL::OTHER) { | |
4353 | print STDERR " - The certificate has ", | |
4354 | "an unknown error.\n"; | |
4355 | } | |
4356 | } # no warnings 'once' | |
6f729591 EW |
4357 | printf STDERR |
4358 | "Certificate information:\n". | |
30d055aa EW |
4359 | " - Hostname: %s\n". |
4360 | " - Valid: from %s until %s\n". | |
4361 | " - Issuer: %s\n". | |
4362 | " - Fingerprint: %s\n", | |
4363 | map $cert_info->$_, qw(hostname valid_from valid_until | |
6f729591 | 4364 | issuer_dname fingerprint); |
30d055aa EW |
4365 | my $choice; |
4366 | prompt: | |
6f729591 | 4367 | print STDERR $may_save ? |
30d055aa EW |
4368 | "(R)eject, accept (t)emporarily or accept (p)ermanently? " : |
4369 | "(R)eject or accept (t)emporarily? "; | |
6f729591 | 4370 | STDERR->flush; |
30d055aa EW |
4371 | $choice = lc(substr(<STDIN> || 'R', 0, 1)); |
4372 | if ($choice =~ /^t$/i) { | |
4373 | $cred->may_save(undef); | |
4374 | } elsif ($choice =~ /^r$/i) { | |
4375 | return -1; | |
4376 | } elsif ($may_save && $choice =~ /^p$/i) { | |
4377 | $cred->may_save($may_save); | |
4378 | } else { | |
4379 | goto prompt; | |
4380 | } | |
4381 | $cred->accepted_failures($failures); | |
4382 | $SVN::_Core::SVN_NO_ERROR; | |
4383 | } | |
4384 | ||
d976acfd | 4385 | sub ssl_client_cert { |
30d055aa EW |
4386 | my ($cred, $realm, $may_save, $pool) = @_; |
4387 | $may_save = undef if $_no_auth_cache; | |
6f729591 EW |
4388 | print STDERR "Client certificate filename: "; |
4389 | STDERR->flush; | |
30d055aa EW |
4390 | chomp(my $filename = <STDIN>); |
4391 | $cred->cert_file($filename); | |
4392 | $cred->may_save($may_save); | |
4393 | $SVN::_Core::SVN_NO_ERROR; | |
4394 | } | |
4395 | ||
d976acfd | 4396 | sub ssl_client_cert_pw { |
30d055aa EW |
4397 | my ($cred, $realm, $may_save, $pool) = @_; |
4398 | $may_save = undef if $_no_auth_cache; | |
4399 | $cred->password(_read_password("Password: ", $realm)); | |
4400 | $cred->may_save($may_save); | |
4401 | $SVN::_Core::SVN_NO_ERROR; | |
4402 | } | |
4403 | ||
d976acfd | 4404 | sub username { |
30d055aa EW |
4405 | my ($cred, $realm, $may_save, $pool) = @_; |
4406 | $may_save = undef if $_no_auth_cache; | |
4407 | if (defined $realm && length $realm) { | |
6f729591 | 4408 | print STDERR "Authentication realm: $realm\n"; |
30d055aa EW |
4409 | } |
4410 | my $username; | |
4411 | if (defined $_username) { | |
4412 | $username = $_username; | |
4413 | } else { | |
6f729591 EW |
4414 | print STDERR "Username: "; |
4415 | STDERR->flush; | |
30d055aa EW |
4416 | chomp($username = <STDIN>); |
4417 | } | |
4418 | $cred->username($username); | |
4419 | $cred->may_save($may_save); | |
4420 | $SVN::_Core::SVN_NO_ERROR; | |
4421 | } | |
4422 | ||
4423 | sub _read_password { | |
4424 | my ($prompt, $realm) = @_; | |
30d055aa | 4425 | my $password = ''; |
56a853b6 FL |
4426 | if (exists $ENV{GIT_ASKPASS}) { |
4427 | open(PH, "-|", $ENV{GIT_ASKPASS}, $prompt); | |
4428 | $password = <PH>; | |
4429 | $password =~ s/[\012\015]//; # \n\r | |
4430 | close(PH); | |
4431 | } else { | |
4432 | print STDERR $prompt; | |
4433 | STDERR->flush; | |
4434 | require Term::ReadKey; | |
4435 | Term::ReadKey::ReadMode('noecho'); | |
4436 | while (defined(my $key = Term::ReadKey::ReadKey(0))) { | |
4437 | last if $key =~ /[\012\015]/; # \n\r | |
4438 | $password .= $key; | |
4439 | } | |
4440 | Term::ReadKey::ReadMode('restore'); | |
4441 | print STDERR "\n"; | |
4442 | STDERR->flush; | |
30d055aa | 4443 | } |
30d055aa EW |
4444 | $password; |
4445 | } | |
4446 | ||
27a1a801 | 4447 | package SVN::Git::Fetcher; |
40a1530c RC |
4448 | use vars qw/@ISA $_ignore_regex $_preserve_empty_dirs $_placeholder_filename |
4449 | @deleted_gpath %added_placeholder $repo_id/; | |
27a1a801 EW |
4450 | use strict; |
4451 | use warnings; | |
4452 | use Carp qw/croak/; | |
40a1530c | 4453 | use File::Basename qw/dirname/; |
27a1a801 EW |
4454 | use IO::File qw//; |
4455 | ||
4456 | # file baton members: path, mode_a, mode_b, pool, fh, blob, base | |
4457 | sub new { | |
8841b37f | 4458 | my ($class, $git_svn, $switch_path) = @_; |
27a1a801 EW |
4459 | my $self = SVN::Delta::Editor->new; |
4460 | bless $self, $class; | |
dbc6c74d EW |
4461 | if (exists $git_svn->{last_commit}) { |
4462 | $self->{c} = $git_svn->{last_commit}; | |
8841b37f EW |
4463 | $self->{empty_symlinks} = |
4464 | _mark_empty_symlinks($git_svn, $switch_path); | |
dbc6c74d | 4465 | } |
40a1530c RC |
4466 | |
4467 | # some options are read globally, but can be overridden locally | |
4468 | # per [svn-remote "..."] section. Command-line options will *NOT* | |
4469 | # override options set in an [svn-remote "..."] section | |
4470 | $repo_id = $git_svn->{repo_id}; | |
4471 | my $k = "svn-remote.$repo_id.ignore-paths"; | |
4472 | my $v = eval { command_oneline('config', '--get', $k) }; | |
4473 | $self->{ignore_regex} = $v; | |
4474 | ||
4475 | $k = "svn-remote.$repo_id.preserve-empty-dirs"; | |
4476 | $v = eval { command_oneline('config', '--get', '--bool', $k) }; | |
4477 | if ($v && $v eq 'true') { | |
4478 | $_preserve_empty_dirs = 1; | |
4479 | $k = "svn-remote.$repo_id.placeholder-filename"; | |
4480 | $v = eval { command_oneline('config', '--get', $k) }; | |
4481 | $_placeholder_filename = $v; | |
4482 | } | |
4483 | ||
4484 | # Load the list of placeholder files added during previous invocations. | |
4485 | $k = "svn-remote.$repo_id.added-placeholder"; | |
4486 | $v = eval { command_oneline('config', '--get-all', $k) }; | |
4487 | if ($_preserve_empty_dirs && $v) { | |
4488 | # command() prints errors to stderr, so we only call it if | |
4489 | # command_oneline() succeeded. | |
4490 | my @v = command('config', '--get-all', $k); | |
4491 | $added_placeholder{ dirname($_) } = $_ foreach @v; | |
4492 | } | |
4493 | ||
d2a9a87b EW |
4494 | $self->{empty} = {}; |
4495 | $self->{dir_prop} = {}; | |
4496 | $self->{file_prop} = {}; | |
4497 | $self->{absent_dir} = {}; | |
4498 | $self->{absent_file} = {}; | |
ef3cfaad | 4499 | $self->{gii} = $git_svn->tmp_index_do(sub { Git::IndexInfo->new }); |
3713e222 | 4500 | $self->{pathnameencoding} = Git::config('svn.pathnameencoding'); |
27a1a801 EW |
4501 | $self; |
4502 | } | |
4503 | ||
dbc6c74d EW |
4504 | # this uses the Ra object, so it must be called before do_{switch,update}, |
4505 | # not inside them (when the Git::SVN::Fetcher object is passed) to | |
4506 | # do_{switch,update} | |
4507 | sub _mark_empty_symlinks { | |
8841b37f | 4508 | my ($git_svn, $switch_path) = @_; |
4c58a711 | 4509 | my $bool = Git::config_bool('svn.brokenSymlinkWorkaround'); |
48679e5c | 4510 | return {} if (!defined($bool)) || (defined($bool) && ! $bool); |
4c58a711 | 4511 | |
dbc6c74d EW |
4512 | my %ret; |
4513 | my ($rev, $cmt) = $git_svn->last_rev_commit; | |
4514 | return {} unless ($rev && $cmt); | |
4515 | ||
4c58a711 EW |
4516 | # allow the warning to be printed for each revision we fetch to |
4517 | # ensure the user sees it. The user can also disable the workaround | |
4518 | # on the repository even while git svn is running and the next | |
4519 | # revision fetched will skip this expensive function. | |
4520 | my $printed_warning; | |
dbc6c74d EW |
4521 | chomp(my $empty_blob = `git hash-object -t blob --stdin < /dev/null`); |
4522 | my ($ls, $ctx) = command_output_pipe(qw/ls-tree -r -z/, $cmt); | |
4523 | local $/ = "\0"; | |
8841b37f | 4524 | my $pfx = defined($switch_path) ? $switch_path : $git_svn->{path}; |
dbc6c74d EW |
4525 | $pfx .= '/' if length($pfx); |
4526 | while (<$ls>) { | |
4527 | chomp; | |
4528 | s/\A100644 blob $empty_blob\t//o or next; | |
4c58a711 EW |
4529 | unless ($printed_warning) { |
4530 | print STDERR "Scanning for empty symlinks, ", | |
4531 | "this may take a while if you have ", | |
4532 | "many empty files\n", | |
4533 | "You may disable this with `", | |
4534 | "git config svn.brokenSymlinkWorkaround ", | |
4535 | "false'.\n", | |
4536 | "This may be done in a different ", | |
4537 | "terminal without restarting ", | |
4538 | "git svn\n"; | |
4539 | $printed_warning = 1; | |
4540 | } | |
dbc6c74d EW |
4541 | my $path = $_; |
4542 | my (undef, $props) = | |
4543 | $git_svn->ra->get_file($pfx.$path, $rev, undef); | |
4544 | if ($props->{'svn:special'}) { | |
4545 | $ret{$path} = 1; | |
4546 | } | |
4547 | } | |
4548 | command_close_pipe($ls, $ctx); | |
4549 | \%ret; | |
4550 | } | |
4551 | ||
b03a71a6 EW |
4552 | # returns true if a given path is inside a ".git" directory |
4553 | sub in_dot_git { | |
4554 | $_[0] =~ m{(?:^|/)\.git(?:/|$)}; | |
4555 | } | |
4556 | ||
edc662f9 VS |
4557 | # return value: 0 -- don't ignore, 1 -- ignore |
4558 | sub is_path_ignored { | |
0d8bee71 | 4559 | my ($self, $path) = @_; |
edc662f9 | 4560 | return 1 if in_dot_git($path); |
0d8bee71 BJ |
4561 | return 1 if defined($self->{ignore_regex}) && |
4562 | $path =~ m!$self->{ignore_regex}!; | |
edc662f9 VS |
4563 | return 0 unless defined($_ignore_regex); |
4564 | return 1 if $path =~ m!$_ignore_regex!o; | |
4565 | return 0; | |
4566 | } | |
4567 | ||
8b8fc068 EW |
4568 | sub set_path_strip { |
4569 | my ($self, $path) = @_; | |
4e9f6cc7 | 4570 | $self->{path_strip} = qr/^\Q$path\E(\/|$)/ if length $path; |
8b8fc068 EW |
4571 | } |
4572 | ||
d2a9a87b EW |
4573 | sub open_root { |
4574 | { path => '' }; | |
4575 | } | |
4576 | ||
4577 | sub open_directory { | |
4578 | my ($self, $path, $pb, $rev) = @_; | |
4579 | { path => $path }; | |
4580 | } | |
4581 | ||
706587fc EW |
4582 | sub git_path { |
4583 | my ($self, $path) = @_; | |
3713e222 DS |
4584 | if (my $enc = $self->{pathnameencoding}) { |
4585 | require Encode; | |
4586 | Encode::from_to($path, 'UTF-8', $enc); | |
4587 | } | |
2b27f6c8 EW |
4588 | if ($self->{path_strip}) { |
4589 | $path =~ s!$self->{path_strip}!! or | |
4590 | die "Failed to strip path '$path' ($self->{path_strip})\n"; | |
4591 | } | |
706587fc EW |
4592 | $path; |
4593 | } | |
4594 | ||
27a1a801 EW |
4595 | sub delete_entry { |
4596 | my ($self, $path, $rev, $pb) = @_; | |
0d8bee71 | 4597 | return undef if $self->is_path_ignored($path); |
4a87db0e | 4598 | |
706587fc | 4599 | my $gpath = $self->git_path($path); |
8a603774 EW |
4600 | return undef if ($gpath eq ''); |
4601 | ||
4a87db0e | 4602 | # remove entire directories. |
4f821012 EW |
4603 | my ($tree) = (command('ls-tree', '-z', $self->{c}, "./$gpath") |
4604 | =~ /\A040000 tree ([a-f\d]{40})\t\Q$gpath\E\0/); | |
4605 | if ($tree) { | |
4a87db0e EW |
4606 | my ($ls, $ctx) = command_output_pipe(qw/ls-tree |
4607 | -r --name-only -z/, | |
4f821012 | 4608 | $tree); |
4a87db0e EW |
4609 | local $/ = "\0"; |
4610 | while (<$ls>) { | |
ef3cfaad | 4611 | chomp; |
4f821012 EW |
4612 | my $rmpath = "$gpath/$_"; |
4613 | $self->{gii}->remove($rmpath); | |
4614 | print "\tD\t$rmpath\n" unless $::_q; | |
4a87db0e | 4615 | } |
9e3cdbd4 | 4616 | print "\tD\t$gpath/\n" unless $::_q; |
4a87db0e | 4617 | command_close_pipe($ls, $ctx); |
4a87db0e | 4618 | } else { |
ef3cfaad | 4619 | $self->{gii}->remove($gpath); |
9e3cdbd4 | 4620 | print "\tD\t$gpath\n" unless $::_q; |
4a87db0e | 4621 | } |
40a1530c RC |
4622 | # Don't add to @deleted_gpath if we're deleting a placeholder file. |
4623 | push @deleted_gpath, $gpath unless $added_placeholder{dirname($path)}; | |
f9ad77a7 | 4624 | $self->{empty}->{$path} = 0; |
27a1a801 EW |
4625 | undef; |
4626 | } | |
4627 | ||
4628 | sub open_file { | |
4629 | my ($self, $path, $pb, $rev) = @_; | |
b03a71a6 EW |
4630 | my ($mode, $blob); |
4631 | ||
0d8bee71 | 4632 | goto out if $self->is_path_ignored($path); |
b03a71a6 | 4633 | |
706587fc | 4634 | my $gpath = $self->git_path($path); |
4f821012 EW |
4635 | ($mode, $blob) = (command('ls-tree', '-z', $self->{c}, "./$gpath") |
4636 | =~ /\A(\d{6}) blob ([a-f\d]{40})\t\Q$gpath\E\0/); | |
006ede5e EW |
4637 | unless (defined $mode && defined $blob) { |
4638 | die "$path was not found in commit $self->{c} (r$rev)\n"; | |
4639 | } | |
dbc6c74d EW |
4640 | if ($mode eq '100644' && $self->{empty_symlinks}->{$path}) { |
4641 | $mode = '120000'; | |
4642 | } | |
b03a71a6 | 4643 | out: |
27a1a801 | 4644 | { path => $path, mode_a => $mode, mode_b => $mode, blob => $blob, |
0864e3ba | 4645 | pool => SVN::Pool->new, action => 'M' }; |
27a1a801 EW |
4646 | } |
4647 | ||
4648 | sub add_file { | |
4649 | my ($self, $path, $pb, $cp_path, $cp_rev) = @_; | |
b03a71a6 EW |
4650 | my $mode; |
4651 | ||
0d8bee71 | 4652 | if (!$self->is_path_ignored($path)) { |
b03a71a6 EW |
4653 | my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#); |
4654 | delete $self->{empty}->{$dir}; | |
4655 | $mode = '100644'; | |
40a1530c RC |
4656 | |
4657 | if ($added_placeholder{$dir}) { | |
4658 | # Remove our placeholder file, if we created one. | |
4659 | delete_entry($self, $added_placeholder{$dir}) | |
4660 | unless $path eq $added_placeholder{$dir}; | |
4661 | delete $added_placeholder{$dir} | |
4662 | } | |
b03a71a6 | 4663 | } |
40a1530c | 4664 | |
b03a71a6 | 4665 | { path => $path, mode_a => $mode, mode_b => $mode, |
0864e3ba | 4666 | pool => SVN::Pool->new, action => 'A' }; |
27a1a801 EW |
4667 | } |
4668 | ||
d2a9a87b EW |
4669 | sub add_directory { |
4670 | my ($self, $path, $cp_path, $cp_rev) = @_; | |
0d8bee71 | 4671 | goto out if $self->is_path_ignored($path); |
12a6d752 EW |
4672 | my $gpath = $self->git_path($path); |
4673 | if ($gpath eq '') { | |
4674 | my ($ls, $ctx) = command_output_pipe(qw/ls-tree | |
4675 | -r --name-only -z/, | |
4676 | $self->{c}); | |
4677 | local $/ = "\0"; | |
4678 | while (<$ls>) { | |
4679 | chomp; | |
4680 | $self->{gii}->remove($_); | |
4681 | print "\tD\t$_\n" unless $::_q; | |
40a1530c | 4682 | push @deleted_gpath, $gpath; |
12a6d752 EW |
4683 | } |
4684 | command_close_pipe($ls, $ctx); | |
4685 | $self->{empty}->{$path} = 0; | |
4686 | } | |
d2a9a87b EW |
4687 | my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#); |
4688 | delete $self->{empty}->{$dir}; | |
4689 | $self->{empty}->{$path} = 1; | |
40a1530c RC |
4690 | |
4691 | if ($added_placeholder{$dir}) { | |
4692 | # Remove our placeholder file, if we created one. | |
4693 | delete_entry($self, $added_placeholder{$dir}); | |
4694 | delete $added_placeholder{$dir} | |
4695 | } | |
4696 | ||
b03a71a6 | 4697 | out: |
d2a9a87b EW |
4698 | { path => $path }; |
4699 | } | |
4700 | ||
4701 | sub change_dir_prop { | |
4702 | my ($self, $db, $prop, $value) = @_; | |
0d8bee71 | 4703 | return undef if $self->is_path_ignored($db->{path}); |
d2a9a87b EW |
4704 | $self->{dir_prop}->{$db->{path}} ||= {}; |
4705 | $self->{dir_prop}->{$db->{path}}->{$prop} = $value; | |
4706 | undef; | |
4707 | } | |
4708 | ||
4709 | sub absent_directory { | |
4710 | my ($self, $path, $pb) = @_; | |
0d8bee71 | 4711 | return undef if $self->is_path_ignored($path); |
d2a9a87b EW |
4712 | $self->{absent_dir}->{$pb->{path}} ||= []; |
4713 | push @{$self->{absent_dir}->{$pb->{path}}}, $path; | |
4714 | undef; | |
4715 | } | |
4716 | ||
4717 | sub absent_file { | |
4718 | my ($self, $path, $pb) = @_; | |
0d8bee71 | 4719 | return undef if $self->is_path_ignored($path); |
d2a9a87b EW |
4720 | $self->{absent_file}->{$pb->{path}} ||= []; |
4721 | push @{$self->{absent_file}->{$pb->{path}}}, $path; | |
4722 | undef; | |
4723 | } | |
4724 | ||
27a1a801 EW |
4725 | sub change_file_prop { |
4726 | my ($self, $fb, $prop, $value) = @_; | |
0d8bee71 | 4727 | return undef if $self->is_path_ignored($fb->{path}); |
27a1a801 EW |
4728 | if ($prop eq 'svn:executable') { |
4729 | if ($fb->{mode_b} != 120000) { | |
4730 | $fb->{mode_b} = defined $value ? 100755 : 100644; | |
4731 | } | |
4732 | } elsif ($prop eq 'svn:special') { | |
4733 | $fb->{mode_b} = defined $value ? 120000 : 100644; | |
d2a9a87b EW |
4734 | } else { |
4735 | $self->{file_prop}->{$fb->{path}} ||= {}; | |
4736 | $self->{file_prop}->{$fb->{path}}->{$prop} = $value; | |
27a1a801 EW |
4737 | } |
4738 | undef; | |
4739 | } | |
4740 | ||
4741 | sub apply_textdelta { | |
4742 | my ($self, $fb, $exp) = @_; | |
0d8bee71 | 4743 | return undef if $self->is_path_ignored($fb->{path}); |
1b3069a7 | 4744 | my $fh = $::_repository->temp_acquire('svn_delta'); |
27a1a801 EW |
4745 | # $fh gets auto-closed() by SVN::TxDelta::apply(), |
4746 | # (but $base does not,) so dup() it for reading in close_file | |
4747 | open my $dup, '<&', $fh or croak $!; | |
1b3069a7 | 4748 | my $base = $::_repository->temp_acquire('git_blob'); |
b03a71a6 | 4749 | |
27a1a801 | 4750 | if ($fb->{blob}) { |
baf5fa8a EW |
4751 | my ($base_is_link, $size); |
4752 | ||
dbc6c74d EW |
4753 | if ($fb->{mode_a} eq '120000' && |
4754 | ! $self->{empty_symlinks}->{$fb->{path}}) { | |
4755 | print $base 'link ' or die "print $!\n"; | |
baf5fa8a | 4756 | $base_is_link = 1; |
dbc6c74d | 4757 | } |
baf5fa8a EW |
4758 | retry: |
4759 | $size = $::_repository->cat_blob($fb->{blob}, $base); | |
d683a0e0 | 4760 | die "Failed to read object $fb->{blob}" if ($size < 0); |
27a1a801 EW |
4761 | |
4762 | if (defined $exp) { | |
4763 | seek $base, 0, 0 or croak $!; | |
8d7c4fad | 4764 | my $got = ::md5sum($base); |
baf5fa8a EW |
4765 | if ($got ne $exp) { |
4766 | my $err = "Checksum mismatch: ". | |
4767 | "$fb->{path} $fb->{blob}\n" . | |
4768 | "expected: $exp\n" . | |
4769 | " got: $got\n"; | |
4770 | if ($base_is_link) { | |
4771 | warn $err, | |
4772 | "Retrying... (possibly ", | |
4773 | "a bad symlink from SVN)\n"; | |
4774 | $::_repository->temp_reset($base); | |
4775 | $base_is_link = 0; | |
4776 | goto retry; | |
4777 | } | |
4778 | die $err; | |
4779 | } | |
27a1a801 EW |
4780 | } |
4781 | } | |
4782 | seek $base, 0, 0 or croak $!; | |
0b19138b | 4783 | $fb->{fh} = $fh; |
27a1a801 | 4784 | $fb->{base} = $base; |
0b19138b | 4785 | [ SVN::TxDelta::apply($base, $dup, undef, $fb->{path}, $fb->{pool}) ]; |
27a1a801 EW |
4786 | } |
4787 | ||
4788 | sub close_file { | |
4789 | my ($self, $fb, $exp) = @_; | |
0d8bee71 | 4790 | return undef if $self->is_path_ignored($fb->{path}); |
b03a71a6 | 4791 | |
27a1a801 | 4792 | my $hash; |
706587fc | 4793 | my $path = $self->git_path($fb->{path}); |
27a1a801 | 4794 | if (my $fh = $fb->{fh}) { |
7faf0686 EW |
4795 | if (defined $exp) { |
4796 | seek($fh, 0, 0) or croak $!; | |
8d7c4fad | 4797 | my $got = ::md5sum($fh); |
7faf0686 EW |
4798 | if ($got ne $exp) { |
4799 | die "Checksum mismatch: $path\n", | |
4800 | "expected: $exp\n got: $got\n"; | |
4801 | } | |
4802 | } | |
27a1a801 | 4803 | if ($fb->{mode_b} == 120000) { |
510b0945 | 4804 | sysseek($fh, 0, 0) or croak $!; |
dbc6c74d | 4805 | my $rd = sysread($fh, my $buf, 5); |
ffe256f9 | 4806 | |
dbc6c74d EW |
4807 | if (!defined $rd) { |
4808 | croak "sysread: $!\n"; | |
4809 | } elsif ($rd == 0) { | |
4810 | warn "$path has mode 120000", | |
4811 | " but it points to nothing\n", | |
4812 | "converting to an empty file with mode", | |
4813 | " 100644\n"; | |
4814 | $fb->{mode_b} = '100644'; | |
4815 | } elsif ($buf ne 'link ') { | |
510b0945 | 4816 | warn "$path has mode 120000", |
dbc6c74d | 4817 | " but is not a link\n"; |
510b0945 | 4818 | } else { |
1b3069a7 MS |
4819 | my $tmp_fh = $::_repository->temp_acquire( |
4820 | 'svn_hash'); | |
510b0945 MG |
4821 | my $res; |
4822 | while ($res = sysread($fh, my $str, 1024)) { | |
4823 | my $out = syswrite($tmp_fh, $str, $res); | |
4824 | defined($out) && $out == $res | |
4825 | or croak("write ", | |
836ff95d | 4826 | Git::temp_path($tmp_fh), |
510b0945 MG |
4827 | ": $!\n"); |
4828 | } | |
4829 | defined $res or croak $!; | |
ffe256f9 | 4830 | |
510b0945 MG |
4831 | ($fh, $tmp_fh) = ($tmp_fh, $fh); |
4832 | Git::temp_release($tmp_fh, 1); | |
4833 | } | |
4834 | } | |
0b19138b MG |
4835 | |
4836 | $hash = $::_repository->hash_and_insert_object( | |
836ff95d | 4837 | Git::temp_path($fh)); |
27a1a801 | 4838 | $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n"; |
0b19138b MG |
4839 | |
4840 | Git::temp_release($fb->{base}, 1); | |
510b0945 | 4841 | Git::temp_release($fh, 1); |
27a1a801 EW |
4842 | } else { |
4843 | $hash = $fb->{blob} or die "no blob information\n"; | |
4844 | } | |
4845 | $fb->{pool}->clear; | |
ef3cfaad | 4846 | $self->{gii}->update($fb->{mode_b}, $hash, $path) or croak $!; |
9e3cdbd4 | 4847 | print "\t$fb->{action}\t$path\n" if $fb->{action} && ! $::_q; |
27a1a801 EW |
4848 | undef; |
4849 | } | |
4850 | ||
4851 | sub abort_edit { | |
4852 | my $self = shift; | |
ef3cfaad EW |
4853 | $self->{nr} = $self->{gii}->{nr}; |
4854 | delete $self->{gii}; | |
27a1a801 EW |
4855 | $self->SUPER::abort_edit(@_); |
4856 | } | |
4857 | ||
4858 | sub close_edit { | |
4859 | my $self = shift; | |
40a1530c RC |
4860 | |
4861 | if ($_preserve_empty_dirs) { | |
4862 | my @empty_dirs; | |
4863 | ||
4864 | # Any entry flagged as empty that also has an associated | |
4865 | # dir_prop represents a newly created empty directory. | |
4866 | foreach my $i (keys %{$self->{empty}}) { | |
4867 | push @empty_dirs, $i if exists $self->{dir_prop}->{$i}; | |
4868 | } | |
4869 | ||
4870 | # Search for directories that have become empty due subsequent | |
4871 | # file deletes. | |
4872 | push @empty_dirs, $self->find_empty_directories(); | |
4873 | ||
4874 | # Finally, add a placeholder file to each empty directory. | |
4875 | $self->add_placeholder_file($_) foreach (@empty_dirs); | |
4876 | ||
4877 | $self->stash_placeholder_list(); | |
4878 | } | |
4879 | ||
dad73c0b | 4880 | $self->{git_commit_ok} = 1; |
ef3cfaad EW |
4881 | $self->{nr} = $self->{gii}->{nr}; |
4882 | delete $self->{gii}; | |
27a1a801 EW |
4883 | $self->SUPER::close_edit(@_); |
4884 | } | |
1a82e793 | 4885 | |
40a1530c RC |
4886 | sub find_empty_directories { |
4887 | my ($self) = @_; | |
4888 | my @empty_dirs; | |
4889 | my %dirs = map { dirname($_) => 1 } @deleted_gpath; | |
4890 | ||
4891 | foreach my $dir (sort keys %dirs) { | |
4892 | next if $dir eq "."; | |
4893 | ||
4894 | # If there have been any additions to this directory, there is | |
4895 | # no reason to check if it is empty. | |
4896 | my $skip_added = 0; | |
4897 | foreach my $t (qw/dir_prop file_prop/) { | |
4898 | foreach my $path (keys %{ $self->{$t} }) { | |
4899 | if (exists $self->{$t}->{dirname($path)}) { | |
4900 | $skip_added = 1; | |
4901 | last; | |
4902 | } | |
4903 | } | |
4904 | last if $skip_added; | |
4905 | } | |
4906 | next if $skip_added; | |
4907 | ||
4908 | # Use `git ls-tree` to get the filenames of this directory | |
4909 | # that existed prior to this particular commit. | |
4910 | my $ls = command('ls-tree', '-z', '--name-only', | |
4911 | $self->{c}, "$dir/"); | |
4912 | my %files = map { $_ => 1 } split(/\0/, $ls); | |
4913 | ||
4914 | # Remove the filenames that were deleted during this commit. | |
4915 | delete $files{$_} foreach (@deleted_gpath); | |
4916 | ||
4917 | # Report the directory if there are no filenames left. | |
4918 | push @empty_dirs, $dir unless (scalar %files); | |
4919 | } | |
4920 | @empty_dirs; | |
4921 | } | |
4922 | ||
4923 | sub add_placeholder_file { | |
4924 | my ($self, $dir) = @_; | |
4925 | my $path = "$dir/$_placeholder_filename"; | |
4926 | my $gpath = $self->git_path($path); | |
4927 | ||
4928 | my $fh = $::_repository->temp_acquire($gpath); | |
4929 | my $hash = $::_repository->hash_and_insert_object(Git::temp_path($fh)); | |
4930 | Git::temp_release($fh, 1); | |
4931 | $self->{gii}->update('100644', $hash, $gpath) or croak $!; | |
4932 | ||
4933 | # The directory should no longer be considered empty. | |
4934 | delete $self->{empty}->{$dir} if exists $self->{empty}->{$dir}; | |
4935 | ||
4936 | # Keep track of any placeholder files we create. | |
4937 | $added_placeholder{$dir} = $path; | |
4938 | } | |
4939 | ||
4940 | sub stash_placeholder_list { | |
4941 | my ($self) = @_; | |
4942 | my $k = "svn-remote.$repo_id.added-placeholder"; | |
4943 | my $v = eval { command_oneline('config', '--get-all', $k) }; | |
4944 | command_noisy('config', '--unset-all', $k) if $v; | |
4945 | foreach (values %added_placeholder) { | |
4946 | command_noisy('config', '--add', $k, $_); | |
4947 | } | |
4948 | } | |
4949 | ||
a5e0cedc | 4950 | package SVN::Git::Editor; |
24e22aa8 | 4951 | use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/; |
a5e0cedc EW |
4952 | use strict; |
4953 | use warnings; | |
4954 | use Carp qw/croak/; | |
4955 | use IO::File; | |
4956 | ||
4957 | sub new { | |
61395354 EW |
4958 | my ($class, $opts) = @_; |
4959 | foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) { | |
4960 | die "$_ required!\n" unless (defined $opts->{$_}); | |
4961 | } | |
4962 | ||
4963 | my $pool = SVN::Pool->new; | |
4964 | my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b}); | |
4965 | my $types = check_diff_paths($opts->{ra}, $opts->{svn_path}, | |
4966 | $opts->{r}, $mods); | |
4967 | ||
4968 | # $opts->{ra} functions should not be used after this: | |
4969 | my @ce = $opts->{ra}->get_commit_editor($opts->{log}, | |
4970 | $opts->{editor_cb}, $pool); | |
4971 | my $self = SVN::Delta::Editor->new(@ce, $pool); | |
a5e0cedc | 4972 | bless $self, $class; |
61395354 EW |
4973 | foreach (qw/svn_path r tree_a tree_b/) { |
4974 | $self->{$_} = $opts->{$_}; | |
a5e0cedc | 4975 | } |
61395354 EW |
4976 | $self->{url} = $opts->{ra}->{url}; |
4977 | $self->{mods} = $mods; | |
4978 | $self->{types} = $types; | |
4979 | $self->{pool} = $pool; | |
a5e0cedc EW |
4980 | $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) }; |
4981 | $self->{rm} = { }; | |
d3a840dc EW |
4982 | $self->{path_prefix} = length $self->{svn_path} ? |
4983 | "$self->{svn_path}/" : ''; | |
128de657 | 4984 | $self->{config} = $opts->{config}; |
6abd9332 | 4985 | $self->{mergeinfo} = $opts->{mergeinfo}; |
a5e0cedc EW |
4986 | return $self; |
4987 | } | |
4988 | ||
61395354 EW |
4989 | sub generate_diff { |
4990 | my ($tree_a, $tree_b) = @_; | |
4991 | my @diff_tree = qw(diff-tree -z -r); | |
24e22aa8 EW |
4992 | if ($_cp_similarity) { |
4993 | push @diff_tree, "-C$_cp_similarity"; | |
61395354 EW |
4994 | } else { |
4995 | push @diff_tree, '-C'; | |
4996 | } | |
24e22aa8 EW |
4997 | push @diff_tree, '--find-copies-harder' if $_find_copies_harder; |
4998 | push @diff_tree, "-l$_rename_limit" if defined $_rename_limit; | |
61395354 EW |
4999 | push @diff_tree, $tree_a, $tree_b; |
5000 | my ($diff_fh, $ctx) = command_output_pipe(@diff_tree); | |
5001 | local $/ = "\0"; | |
5002 | my $state = 'meta'; | |
5003 | my @mods; | |
5004 | while (<$diff_fh>) { | |
5005 | chomp $_; # this gets rid of the trailing "\0" | |
5006 | if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s | |
2d0c8acc | 5007 | ($::sha1)\s($::sha1)\s |
61395354 EW |
5008 | ([MTCRAD])\d*$/xo) { |
5009 | push @mods, { mode_a => $1, mode_b => $2, | |
2d0c8acc FW |
5010 | sha1_a => $3, sha1_b => $4, |
5011 | chg => $5 }; | |
5012 | if ($5 =~ /^(?:C|R)$/) { | |
61395354 EW |
5013 | $state = 'file_a'; |
5014 | } else { | |
5015 | $state = 'file_b'; | |
5016 | } | |
5017 | } elsif ($state eq 'file_a') { | |
5018 | my $x = $mods[$#mods] or croak "Empty array\n"; | |
5019 | if ($x->{chg} !~ /^(?:C|R)$/) { | |
5020 | croak "Error parsing $_, $x->{chg}\n"; | |
5021 | } | |
5022 | $x->{file_a} = $_; | |
5023 | $state = 'file_b'; | |
5024 | } elsif ($state eq 'file_b') { | |
5025 | my $x = $mods[$#mods] or croak "Empty array\n"; | |
5026 | if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) { | |
5027 | croak "Error parsing $_, $x->{chg}\n"; | |
5028 | } | |
5029 | if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) { | |
5030 | croak "Error parsing $_, $x->{chg}\n"; | |
5031 | } | |
5032 | $x->{file_b} = $_; | |
5033 | $state = 'meta'; | |
5034 | } else { | |
5035 | croak "Error parsing $_\n"; | |
5036 | } | |
5037 | } | |
5038 | command_close_pipe($diff_fh, $ctx); | |
5039 | \@mods; | |
5040 | } | |
5041 | ||
5042 | sub check_diff_paths { | |
5043 | my ($ra, $pfx, $rev, $mods) = @_; | |
5044 | my %types; | |
5045 | $pfx .= '/' if length $pfx; | |
5046 | ||
5047 | sub type_diff_paths { | |
5048 | my ($ra, $types, $path, $rev) = @_; | |
5049 | my @p = split m#/+#, $path; | |
5050 | my $c = shift @p; | |
5051 | unless (defined $types->{$c}) { | |
5052 | $types->{$c} = $ra->check_path($c, $rev); | |
5053 | } | |
5054 | while (@p) { | |
5055 | $c .= '/' . shift @p; | |
5056 | next if defined $types->{$c}; | |
5057 | $types->{$c} = $ra->check_path($c, $rev); | |
5058 | } | |
5059 | } | |
5060 | ||
5061 | foreach my $m (@$mods) { | |
5062 | foreach my $f (qw/file_a file_b/) { | |
5063 | next unless defined $m->{$f}; | |
5064 | my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#); | |
5065 | if (length $pfx.$dir && ! defined $types{$dir}) { | |
5066 | type_diff_paths($ra, \%types, $pfx.$dir, $rev); | |
5067 | } | |
5068 | } | |
5069 | } | |
5070 | \%types; | |
5071 | } | |
5072 | ||
a5e0cedc EW |
5073 | sub split_path { |
5074 | return ($_[0] =~ m#^(.*?)/?([^/]+)$#); | |
5075 | } | |
5076 | ||
5077 | sub repo_path { | |
d3a840dc | 5078 | my ($self, $path) = @_; |
3713e222 DS |
5079 | if (my $enc = $self->{pathnameencoding}) { |
5080 | require Encode; | |
5081 | Encode::from_to($path, $enc, 'UTF-8'); | |
5082 | } | |
d3a840dc | 5083 | $self->{path_prefix}.(defined $path ? $path : ''); |
a5e0cedc EW |
5084 | } |
5085 | ||
5086 | sub url_path { | |
5087 | my ($self, $path) = @_; | |
29633bb9 | 5088 | if ($self->{url} =~ m#^https?://#) { |
884cce5b | 5089 | $path =~ s!([^~a-zA-Z0-9_./-])!uc sprintf("%%%02x",ord($1))!eg; |
29633bb9 | 5090 | } |
6e8548cc | 5091 | $self->{url} . '/' . $self->repo_path($path); |
a5e0cedc EW |
5092 | } |
5093 | ||
5094 | sub rmdirs { | |
61395354 | 5095 | my ($self) = @_; |
a5e0cedc EW |
5096 | my $rm = $self->{rm}; |
5097 | delete $rm->{''}; # we never delete the url we're tracking | |
5098 | return unless %$rm; | |
5099 | ||
5100 | foreach (keys %$rm) { | |
5101 | my @d = split m#/#, $_; | |
5102 | my $c = shift @d; | |
5103 | $rm->{$c} = 1; | |
5104 | while (@d) { | |
5105 | $c .= '/' . shift @d; | |
5106 | $rm->{$c} = 1; | |
5107 | } | |
5108 | } | |
5109 | delete $rm->{$self->{svn_path}}; | |
5110 | delete $rm->{''}; # we never delete the url we're tracking | |
5111 | return unless %$rm; | |
5112 | ||
61395354 EW |
5113 | my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/, |
5114 | $self->{tree_b}); | |
a5e0cedc EW |
5115 | local $/ = "\0"; |
5116 | while (<$fh>) { | |
5117 | chomp; | |
747fa12c | 5118 | my @dn = split m#/#, $_; |
c07eee1f EW |
5119 | while (pop @dn) { |
5120 | delete $rm->{join '/', @dn}; | |
5121 | } | |
5122 | unless (%$rm) { | |
22600a25 | 5123 | close $fh; |
c07eee1f EW |
5124 | return; |
5125 | } | |
a5e0cedc | 5126 | } |
aef4e921 | 5127 | command_close_pipe($fh, $ctx); |
c07eee1f | 5128 | |
a5e0cedc EW |
5129 | my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat}); |
5130 | foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) { | |
5131 | $self->close_directory($bat->{$d}, $p); | |
5132 | my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#); | |
44320b9e | 5133 | print "\tD+\t$d/\n" unless $::_q; |
a5e0cedc EW |
5134 | $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p); |
5135 | delete $bat->{$d}; | |
5136 | } | |
5137 | } | |
5138 | ||
5139 | sub open_or_add_dir { | |
379862ec | 5140 | my ($self, $full_path, $baton, $deletions) = @_; |
6e8548cc EW |
5141 | my $t = $self->{types}->{$full_path}; |
5142 | if (!defined $t) { | |
5143 | die "$full_path not known in r$self->{r} or we have a bug!\n"; | |
5144 | } | |
fd499bcc ER |
5145 | { |
5146 | no warnings 'once'; | |
5147 | # SVN::Node::none and SVN::Node::file are used only once, | |
5148 | # so we're shutting up Perl's warnings about them. | |
379862ec | 5149 | if ($t == $SVN::Node::none || defined($deletions->{$full_path})) { |
fd499bcc ER |
5150 | return $self->add_directory($full_path, $baton, |
5151 | undef, -1, $self->{pool}); | |
5152 | } elsif ($t == $SVN::Node::dir) { | |
5153 | return $self->open_directory($full_path, $baton, | |
5154 | $self->{r}, $self->{pool}); | |
5155 | } # no warnings 'once' | |
5156 | print STDERR "$full_path already exists in repository at ", | |
5157 | "r$self->{r} and it is not a directory (", | |
5158 | ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n"; | |
5159 | } # no warnings 'once' | |
a5e0cedc EW |
5160 | exit 1; |
5161 | } | |
5162 | ||
5163 | sub ensure_path { | |
379862ec | 5164 | my ($self, $path, $deletions) = @_; |
a5e0cedc | 5165 | my $bat = $self->{bat}; |
6e8548cc EW |
5166 | my $repo_path = $self->repo_path($path); |
5167 | return $bat->{''} unless (length $repo_path); | |
379862ec | 5168 | |
6e8548cc | 5169 | my @p = split m#/+#, $repo_path; |
a5e0cedc | 5170 | my $c = shift @p; |
379862ec | 5171 | $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''}, $deletions); |
a5e0cedc EW |
5172 | while (@p) { |
5173 | my $c0 = $c; | |
5174 | $c .= '/' . shift @p; | |
379862ec | 5175 | $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0}, $deletions); |
a5e0cedc EW |
5176 | } |
5177 | return $bat->{$c}; | |
5178 | } | |
5179 | ||
128de657 BK |
5180 | # Subroutine to convert a globbing pattern to a regular expression. |
5181 | # From perl cookbook. | |
5182 | sub glob2pat { | |
5183 | my $globstr = shift; | |
5184 | my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']'); | |
5185 | $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge; | |
5186 | return '^' . $globstr . '$'; | |
5187 | } | |
5188 | ||
5189 | sub check_autoprop { | |
5190 | my ($self, $pattern, $properties, $file, $fbat) = @_; | |
5191 | # Convert the globbing pattern to a regular expression. | |
5192 | my $regex = glob2pat($pattern); | |
5193 | # Check if the pattern matches the file name. | |
5194 | if($file =~ m/($regex)/) { | |
5195 | # Parse the list of properties to set. | |
5196 | my @props = split(/;/, $properties); | |
5197 | foreach my $prop (@props) { | |
5198 | # Parse 'name=value' syntax and set the property. | |
5199 | if ($prop =~ /([^=]+)=(.*)/) { | |
5200 | my ($n,$v) = ($1,$2); | |
5201 | for ($n, $v) { | |
5202 | s/^\s+//; s/\s+$//; | |
5203 | } | |
5204 | $self->change_file_prop($fbat, $n, $v); | |
5205 | } | |
5206 | } | |
5207 | } | |
5208 | } | |
5209 | ||
5210 | sub apply_autoprops { | |
5211 | my ($self, $file, $fbat) = @_; | |
5212 | my $conf_t = ${$self->{config}}{'config'}; | |
5213 | no warnings 'once'; | |
5214 | # Check [miscellany]/enable-auto-props in svn configuration. | |
5215 | if (SVN::_Core::svn_config_get_bool( | |
5216 | $conf_t, | |
5217 | $SVN::_Core::SVN_CONFIG_SECTION_MISCELLANY, | |
5218 | $SVN::_Core::SVN_CONFIG_OPTION_ENABLE_AUTO_PROPS, | |
5219 | 0)) { | |
5220 | # Auto-props are enabled. Enumerate them to look for matches. | |
5221 | my $callback = sub { | |
5222 | $self->check_autoprop($_[0], $_[1], $file, $fbat); | |
5223 | }; | |
5224 | SVN::_Core::svn_config_enumerate( | |
5225 | $conf_t, | |
5226 | $SVN::_Core::SVN_CONFIG_SECTION_AUTO_PROPS, | |
5227 | $callback); | |
5228 | } | |
5229 | } | |
5230 | ||
a5e0cedc | 5231 | sub A { |
379862ec | 5232 | my ($self, $m, $deletions) = @_; |
a5e0cedc | 5233 | my ($dir, $file) = split_path($m->{file_b}); |
379862ec | 5234 | my $pbat = $self->ensure_path($dir, $deletions); |
a5e0cedc EW |
5235 | my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat, |
5236 | undef, -1); | |
44320b9e | 5237 | print "\tA\t$m->{file_b}\n" unless $::_q; |
128de657 | 5238 | $self->apply_autoprops($file, $fbat); |
a5e0cedc EW |
5239 | $self->chg_file($fbat, $m); |
5240 | $self->close_file($fbat,undef,$self->{pool}); | |
5241 | } | |
5242 | ||
5243 | sub C { | |
379862ec | 5244 | my ($self, $m, $deletions) = @_; |
a5e0cedc | 5245 | my ($dir, $file) = split_path($m->{file_b}); |
379862ec | 5246 | my $pbat = $self->ensure_path($dir, $deletions); |
a5e0cedc EW |
5247 | my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat, |
5248 | $self->url_path($m->{file_a}), $self->{r}); | |
44320b9e | 5249 | print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q; |
a5e0cedc EW |
5250 | $self->chg_file($fbat, $m); |
5251 | $self->close_file($fbat,undef,$self->{pool}); | |
5252 | } | |
5253 | ||
5254 | sub delete_entry { | |
5255 | my ($self, $path, $pbat) = @_; | |
5256 | my $rpath = $self->repo_path($path); | |
5257 | my ($dir, $file) = split_path($rpath); | |
5258 | $self->{rm}->{$dir} = 1; | |
5259 | $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool}); | |
5260 | } | |
5261 | ||
5262 | sub R { | |
379862ec | 5263 | my ($self, $m, $deletions) = @_; |
a5e0cedc | 5264 | my ($dir, $file) = split_path($m->{file_b}); |
379862ec | 5265 | my $pbat = $self->ensure_path($dir, $deletions); |
a5e0cedc EW |
5266 | my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat, |
5267 | $self->url_path($m->{file_a}), $self->{r}); | |
44320b9e | 5268 | print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q; |
7c4d0219 | 5269 | $self->apply_autoprops($file, $fbat); |
a5e0cedc EW |
5270 | $self->chg_file($fbat, $m); |
5271 | $self->close_file($fbat,undef,$self->{pool}); | |
5272 | ||
5273 | ($dir, $file) = split_path($m->{file_a}); | |
379862ec | 5274 | $pbat = $self->ensure_path($dir, $deletions); |
a5e0cedc EW |
5275 | $self->delete_entry($m->{file_a}, $pbat); |
5276 | } | |
5277 | ||
5278 | sub M { | |
379862ec | 5279 | my ($self, $m, $deletions) = @_; |
a5e0cedc | 5280 | my ($dir, $file) = split_path($m->{file_b}); |
379862ec | 5281 | my $pbat = $self->ensure_path($dir, $deletions); |
a5e0cedc EW |
5282 | my $fbat = $self->open_file($self->repo_path($m->{file_b}), |
5283 | $pbat,$self->{r},$self->{pool}); | |
44320b9e | 5284 | print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q; |
a5e0cedc EW |
5285 | $self->chg_file($fbat, $m); |
5286 | $self->close_file($fbat,undef,$self->{pool}); | |
5287 | } | |
5288 | ||
5289 | sub T { shift->M(@_) } | |
5290 | ||
5291 | sub change_file_prop { | |
5292 | my ($self, $fbat, $pname, $pval) = @_; | |
5293 | $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool}); | |
5294 | } | |
5295 | ||
6abd9332 SW |
5296 | sub change_dir_prop { |
5297 | my ($self, $pbat, $pname, $pval) = @_; | |
5298 | $self->SUPER::change_dir_prop($pbat, $pname, $pval, $self->{pool}); | |
5299 | } | |
5300 | ||
214a34d2 FW |
5301 | sub _chg_file_get_blob ($$$$) { |
5302 | my ($self, $fbat, $m, $which) = @_; | |
1b3069a7 | 5303 | my $fh = $::_repository->temp_acquire("git_blob_$which"); |
214a34d2 | 5304 | if ($m->{"mode_$which"} =~ /^120/) { |
a5e0cedc EW |
5305 | print $fh 'link ' or croak $!; |
5306 | $self->change_file_prop($fbat,'svn:special','*'); | |
214a34d2 | 5307 | } elsif ($m->{mode_a} =~ /^120/ && $m->{"mode_$which"} !~ /^120/) { |
a5e0cedc EW |
5308 | $self->change_file_prop($fbat,'svn:special',undef); |
5309 | } | |
214a34d2 FW |
5310 | my $blob = $m->{"sha1_$which"}; |
5311 | return ($fh,) if ($blob =~ /^0{40}$/); | |
5312 | my $size = $::_repository->cat_blob($blob, $fh); | |
5313 | croak "Failed to read object $blob" if ($size < 0); | |
a5e0cedc EW |
5314 | $fh->flush == 0 or croak $!; |
5315 | seek $fh, 0, 0 or croak $!; | |
5316 | ||
8d7c4fad | 5317 | my $exp = ::md5sum($fh); |
a5e0cedc | 5318 | seek $fh, 0, 0 or croak $!; |
214a34d2 FW |
5319 | return ($fh, $exp); |
5320 | } | |
a5e0cedc | 5321 | |
214a34d2 FW |
5322 | sub chg_file { |
5323 | my ($self, $fbat, $m) = @_; | |
5324 | if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) { | |
5325 | $self->change_file_prop($fbat,'svn:executable','*'); | |
5326 | } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) { | |
5327 | $self->change_file_prop($fbat,'svn:executable',undef); | |
5328 | } | |
8598db93 FW |
5329 | my ($fh_a, $exp_a) = _chg_file_get_blob $self, $fbat, $m, 'a'; |
5330 | my ($fh_b, $exp_b) = _chg_file_get_blob $self, $fbat, $m, 'b'; | |
f7197dff | 5331 | my $pool = SVN::Pool->new; |
8598db93 FW |
5332 | my $atd = $self->apply_textdelta($fbat, $exp_a, $pool); |
5333 | if (-s $fh_a) { | |
5334 | my $txstream = SVN::TxDelta::new ($fh_a, $fh_b, $pool); | |
991255c6 EW |
5335 | my $res = SVN::TxDelta::send_txstream($txstream, @$atd, $pool); |
5336 | if (defined $res) { | |
5337 | die "Unexpected result from send_txstream: $res\n", | |
5338 | "(SVN::Core::VERSION: $SVN::Core::VERSION)\n"; | |
5339 | } | |
8598db93 FW |
5340 | } else { |
5341 | my $got = SVN::TxDelta::send_stream($fh_b, @$atd, $pool); | |
5342 | die "Checksum mismatch\nexpected: $exp_b\ngot: $got\n" | |
5343 | if ($got ne $exp_b); | |
5344 | } | |
5345 | Git::temp_release($fh_b, 1); | |
5346 | Git::temp_release($fh_a, 1); | |
f7197dff | 5347 | $pool->clear; |
a5e0cedc EW |
5348 | } |
5349 | ||
5350 | sub D { | |
379862ec | 5351 | my ($self, $m, $deletions) = @_; |
a5e0cedc | 5352 | my ($dir, $file) = split_path($m->{file_b}); |
379862ec | 5353 | my $pbat = $self->ensure_path($dir, $deletions); |
44320b9e | 5354 | print "\tD\t$m->{file_b}\n" unless $::_q; |
a5e0cedc EW |
5355 | $self->delete_entry($m->{file_b}, $pbat); |
5356 | } | |
5357 | ||
5358 | sub close_edit { | |
5359 | my ($self) = @_; | |
5360 | my ($p,$bat) = ($self->{pool}, $self->{bat}); | |
5361 | foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) { | |
6442754d | 5362 | next if $_ eq ''; |
a5e0cedc EW |
5363 | $self->close_directory($bat->{$_}, $p); |
5364 | } | |
6442754d | 5365 | $self->close_directory($bat->{''}, $p); |
a5e0cedc EW |
5366 | $self->SUPER::close_edit($p); |
5367 | $p->clear; | |
5368 | } | |
5369 | ||
5370 | sub abort_edit { | |
5371 | my ($self) = @_; | |
5372 | $self->SUPER::abort_edit($self->{pool}); | |
61395354 EW |
5373 | } |
5374 | ||
5375 | sub DESTROY { | |
5376 | my $self = shift; | |
5377 | $self->SUPER::DESTROY(@_); | |
a5e0cedc EW |
5378 | $self->{pool}->clear; |
5379 | } | |
5380 | ||
44320b9e EW |
5381 | # this drives the editor |
5382 | sub apply_diff { | |
61395354 EW |
5383 | my ($self) = @_; |
5384 | my $mods = $self->{mods}; | |
5ec514bd | 5385 | my %o = ( D => 0, C => 1, R => 2, A => 3, M => 4, T => 5 ); |
379862ec SW |
5386 | my %deletions; |
5387 | ||
5388 | foreach my $m (@$mods) { | |
5389 | if ($m->{chg} eq "D") { | |
5390 | $deletions{$m->{file_b}} = 1; | |
5391 | } | |
5392 | } | |
5393 | ||
6e8548cc | 5394 | foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) { |
44320b9e EW |
5395 | my $f = $m->{chg}; |
5396 | if (defined $o{$f}) { | |
379862ec | 5397 | $self->$f($m, \%deletions); |
44320b9e | 5398 | } else { |
207f1a75 | 5399 | fatal("Invalid change type: $f"); |
44320b9e EW |
5400 | } |
5401 | } | |
6abd9332 SW |
5402 | |
5403 | if (defined($self->{mergeinfo})) { | |
5404 | $self->change_dir_prop($self->{bat}{''}, "svn:mergeinfo", | |
5405 | $self->{mergeinfo}); | |
5406 | } | |
24e22aa8 | 5407 | $self->rmdirs if $_rmdir; |
93ccbba6 | 5408 | if (@$mods == 0 && !defined($self->{mergeinfo})) { |
44320b9e EW |
5409 | $self->abort_edit; |
5410 | } else { | |
5411 | $self->close_edit; | |
5412 | } | |
6e8548cc | 5413 | return scalar @$mods; |
44320b9e EW |
5414 | } |
5415 | ||
d81bf827 | 5416 | package Git::SVN::Ra; |
cdb51a13 | 5417 | use vars qw/@ISA $config_dir $_ignore_refs_regex $_log_window_size/; |
d81bf827 EW |
5418 | use strict; |
5419 | use warnings; | |
a51cdb0c | 5420 | my ($ra_invalid, $can_do_switch, %ignored_err, $RA); |
d81bf827 EW |
5421 | |
5422 | BEGIN { | |
5423 | # enforce temporary pool usage for some simple functions | |
c5f71ad0 | 5424 | no strict 'refs'; |
bf8a40b8 EW |
5425 | for my $f (qw/rev_proplist get_latest_revnum get_uuid get_repos_root |
5426 | get_file/) { | |
c5f71ad0 SV |
5427 | my $SUPER = "SUPER::$f"; |
5428 | *$f = sub { | |
5429 | my $self = shift; | |
5430 | my $pool = SVN::Pool->new; | |
5431 | my @ret = $self->$SUPER(@_,$pool); | |
5432 | $pool->clear; | |
5433 | wantarray ? @ret : $ret[0]; | |
5434 | }; | |
d81bf827 | 5435 | } |
d81bf827 EW |
5436 | } |
5437 | ||
9ff74e95 SW |
5438 | sub _auth_providers () { |
5439 | [ | |
5440 | SVN::Client::get_simple_provider(), | |
5441 | SVN::Client::get_ssl_server_trust_file_provider(), | |
5442 | SVN::Client::get_simple_prompt_provider( | |
5443 | \&Git::SVN::Prompt::simple, 2), | |
5444 | SVN::Client::get_ssl_client_cert_file_provider(), | |
5445 | SVN::Client::get_ssl_client_cert_prompt_provider( | |
5446 | \&Git::SVN::Prompt::ssl_client_cert, 2), | |
77266e96 | 5447 | SVN::Client::get_ssl_client_cert_pw_file_provider(), |
9ff74e95 SW |
5448 | SVN::Client::get_ssl_client_cert_pw_prompt_provider( |
5449 | \&Git::SVN::Prompt::ssl_client_cert_pw, 2), | |
5450 | SVN::Client::get_username_provider(), | |
5451 | SVN::Client::get_ssl_server_trust_prompt_provider( | |
5452 | \&Git::SVN::Prompt::ssl_server_trust), | |
5453 | SVN::Client::get_username_prompt_provider( | |
5454 | \&Git::SVN::Prompt::username, 2) | |
5455 | ] | |
5456 | } | |
5457 | ||
cfbe7ab3 EW |
5458 | sub escape_uri_only { |
5459 | my ($uri) = @_; | |
5460 | my @tmp; | |
5461 | foreach (split m{/}, $uri) { | |
6a004d3f | 5462 | s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg; |
cfbe7ab3 EW |
5463 | push @tmp, $_; |
5464 | } | |
5465 | join('/', @tmp); | |
5466 | } | |
5467 | ||
5468 | sub escape_url { | |
5469 | my ($url) = @_; | |
5470 | if ($url =~ m#^(https?)://([^/]+)(.*)$#) { | |
5471 | my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3)); | |
5472 | $url = "$scheme://$domain$uri"; | |
5473 | } | |
5474 | $url; | |
5475 | } | |
5476 | ||
d81bf827 EW |
5477 | sub new { |
5478 | my ($class, $url) = @_; | |
f6f09876 | 5479 | $url =~ s!/+$!!; |
5d3b7cd5 | 5480 | return $RA if ($RA && $RA->{url} eq $url); |
f6f09876 | 5481 | |
d32fad2b | 5482 | ::_req_svn(); |
5483 | ||
d81bf827 | 5484 | SVN::_Core::svn_config_ensure($config_dir, undef); |
9ff74e95 | 5485 | my ($baton, $callbacks) = SVN::Core::auth_open_helper(_auth_providers); |
d81bf827 | 5486 | my $config = SVN::Core::config_get_config($config_dir); |
7730fbe6 | 5487 | $RA = undef; |
602015e0 ER |
5488 | my $dont_store_passwords = 1; |
5489 | my $conf_t = ${$config}{'config'}; | |
5490 | { | |
fd499bcc | 5491 | no warnings 'once'; |
602015e0 ER |
5492 | # The usage of $SVN::_Core::SVN_CONFIG_* variables |
5493 | # produces warnings that variables are used only once. | |
5494 | # I had not found the better way to shut them up, so | |
fd499bcc | 5495 | # the warnings of type 'once' are disabled in this block. |
602015e0 ER |
5496 | if (SVN::_Core::svn_config_get_bool($conf_t, |
5497 | $SVN::_Core::SVN_CONFIG_SECTION_AUTH, | |
5498 | $SVN::_Core::SVN_CONFIG_OPTION_STORE_PASSWORDS, | |
5499 | 1) == 0) { | |
5500 | SVN::_Core::svn_auth_set_parameter($baton, | |
5501 | $SVN::_Core::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS, | |
5502 | bless (\$dont_store_passwords, "_p_void")); | |
5503 | } | |
5504 | if (SVN::_Core::svn_config_get_bool($conf_t, | |
5505 | $SVN::_Core::SVN_CONFIG_SECTION_AUTH, | |
5506 | $SVN::_Core::SVN_CONFIG_OPTION_STORE_AUTH_CREDS, | |
5507 | 1) == 0) { | |
5508 | $Git::SVN::Prompt::_no_auth_cache = 1; | |
5509 | } | |
fd499bcc | 5510 | } # no warnings 'once' |
cfbe7ab3 | 5511 | my $self = SVN::Ra->new(url => escape_url($url), auth => $baton, |
d81bf827 EW |
5512 | config => $config, |
5513 | pool => SVN::Pool->new, | |
5514 | auth_provider_callbacks => $callbacks); | |
cfbe7ab3 | 5515 | $self->{url} = $url; |
d81bf827 EW |
5516 | $self->{svn_path} = $url; |
5517 | $self->{repos_root} = $self->get_repos_root; | |
4e9f6cc7 | 5518 | $self->{svn_path} =~ s#^\Q$self->{repos_root}\E(/|$)##; |
0dc03d6a EW |
5519 | $self->{cache} = { check_path => { r => 0, data => {} }, |
5520 | get_dir => { r => 0, data => {} } }; | |
5d3b7cd5 | 5521 | $RA = bless $self, $class; |
d81bf827 EW |
5522 | } |
5523 | ||
0dc03d6a EW |
5524 | sub check_path { |
5525 | my ($self, $path, $r) = @_; | |
5526 | my $cache = $self->{cache}->{check_path}; | |
5527 | if ($r == $cache->{r} && exists $cache->{data}->{$path}) { | |
5528 | return $cache->{data}->{$path}; | |
5529 | } | |
5530 | my $pool = SVN::Pool->new; | |
5531 | my $t = $self->SUPER::check_path($path, $r, $pool); | |
5532 | $pool->clear; | |
5533 | if ($r != $cache->{r}) { | |
5534 | %{$cache->{data}} = (); | |
5535 | $cache->{r} = $r; | |
5536 | } | |
5537 | $cache->{data}->{$path} = $t; | |
5538 | } | |
5539 | ||
5540 | sub get_dir { | |
5541 | my ($self, $dir, $r) = @_; | |
5542 | my $cache = $self->{cache}->{get_dir}; | |
5543 | if ($r == $cache->{r}) { | |
5544 | if (my $x = $cache->{data}->{$dir}) { | |
5545 | return wantarray ? @$x : $x->[0]; | |
5546 | } | |
5547 | } | |
5548 | my $pool = SVN::Pool->new; | |
5549 | my ($d, undef, $props) = $self->SUPER::get_dir($dir, $r, $pool); | |
5550 | my %dirents = map { $_ => { kind => $d->{$_}->kind } } keys %$d; | |
5551 | $pool->clear; | |
5552 | if ($r != $cache->{r}) { | |
5553 | %{$cache->{data}} = (); | |
5554 | $cache->{r} = $r; | |
5555 | } | |
5556 | $cache->{data}->{$dir} = [ \%dirents, $r, $props ]; | |
5557 | wantarray ? (\%dirents, $r, $props) : \%dirents; | |
5558 | } | |
5559 | ||
d81bf827 | 5560 | sub DESTROY { |
5d3b7cd5 | 5561 | # do not call the real DESTROY since we store ourselves in $RA |
d81bf827 EW |
5562 | } |
5563 | ||
1ef626b4 EW |
5564 | # get_log(paths, start, end, limit, |
5565 | # discover_changed_paths, strict_node_history, receiver) | |
d81bf827 EW |
5566 | sub get_log { |
5567 | my ($self, @args) = @_; | |
5568 | my $pool = SVN::Pool->new; | |
1ef626b4 | 5569 | |
3c49a035 MN |
5570 | # svn_log_changed_path_t objects passed to get_log are likely to be |
5571 | # overwritten even if only the refs are copied to an external variable, | |
5572 | # so we should dup the structures in their entirety. Using an | |
5573 | # externally passed pool (instead of our temporary and quickly cleared | |
5574 | # pool in Git::SVN::Ra) does not help matters at all... | |
5575 | my $receiver = pop @args; | |
0b2af457 MN |
5576 | my $prefix = "/".$self->{svn_path}; |
5577 | $prefix =~ s#/+($)##; | |
5578 | my $prefix_regex = qr#^\Q$prefix\E#; | |
3c49a035 MN |
5579 | push(@args, sub { |
5580 | my ($paths) = $_[0]; | |
5581 | return &$receiver(@_) unless $paths; | |
5582 | $_[0] = (); | |
5583 | foreach my $p (keys %$paths) { | |
5584 | my $i = $paths->{$p}; | |
0b2af457 MN |
5585 | # Make path relative to our url, not repos_root |
5586 | $p =~ s/$prefix_regex//; | |
5587 | my %s = map { $_ => $i->$_; } | |
5588 | qw/copyfrom_path copyfrom_rev action/; | |
5589 | if ($s{'copyfrom_path'}) { | |
5590 | $s{'copyfrom_path'} =~ s/$prefix_regex//; | |
5591 | } | |
3c49a035 MN |
5592 | $_[0]{$p} = \%s; |
5593 | } | |
5594 | &$receiver(@_); | |
5595 | }); | |
5596 | ||
5597 | ||
1ef626b4 EW |
5598 | # the limit parameter was not supported in SVN 1.1.x, so we |
5599 | # drop it. Therefore, the receiver callback passed to it | |
5600 | # is made aware of this limitation by being wrapped if | |
5601 | # the limit passed to is being wrapped. | |
5602 | if ($SVN::Core::VERSION le '1.2.0') { | |
5603 | my $limit = splice(@args, 3, 1); | |
5604 | if ($limit > 0) { | |
5605 | my $receiver = pop @args; | |
5606 | push(@args, sub { &$receiver(@_) if (--$limit >= 0) }); | |
5607 | } | |
5608 | } | |
d81bf827 EW |
5609 | my $ret = $self->SUPER::get_log(@args, $pool); |
5610 | $pool->clear; | |
5611 | $ret; | |
5612 | } | |
5613 | ||
9ff74e95 SW |
5614 | sub trees_match { |
5615 | my ($self, $url1, $rev1, $url2, $rev2) = @_; | |
5616 | my $ctx = SVN::Client->new(auth => _auth_providers); | |
5617 | my $out = IO::File->new_tmpfile; | |
5618 | ||
5619 | # older SVN (1.1.x) doesn't take $pool as the last parameter for | |
5620 | # $ctx->diff(), so we'll create a default one | |
5621 | my $pool = SVN::Pool->new_default_sub; | |
5622 | ||
5623 | $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1 | |
5624 | $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out); | |
5625 | $out->flush; | |
5626 | my $ret = (($out->stat)[7] == 0); | |
5627 | close $out or croak $!; | |
5628 | ||
5629 | $ret; | |
5630 | } | |
5631 | ||
d81bf827 | 5632 | sub get_commit_editor { |
44320b9e | 5633 | my ($self, $log, $cb, $pool) = @_; |
d81bf827 | 5634 | my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : (); |
44320b9e | 5635 | $self->SUPER::get_commit_editor($log, $cb, @lock, $pool); |
d81bf827 EW |
5636 | } |
5637 | ||
d81bf827 | 5638 | sub gs_do_update { |
8a603774 EW |
5639 | my ($self, $rev_a, $rev_b, $gs, $editor) = @_; |
5640 | my $new = ($rev_a == $rev_b); | |
5641 | my $path = $gs->{path}; | |
5642 | ||
2e5e2480 EW |
5643 | if ($new && -e $gs->{index}) { |
5644 | unlink $gs->{index} or die | |
5645 | "Couldn't unlink index: $gs->{index}: $!\n"; | |
5646 | } | |
d81bf827 | 5647 | my $pool = SVN::Pool->new; |
8b8fc068 | 5648 | $editor->set_path_strip($path); |
2b27f6c8 EW |
5649 | my (@pc) = split m#/#, $path; |
5650 | my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''), | |
8a603774 | 5651 | 1, $editor, $pool); |
d81bf827 | 5652 | my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : (); |
2b27f6c8 EW |
5653 | |
5654 | # Since we can't rely on svn_ra_reparent being available, we'll | |
5655 | # just have to do some magic with set_path to make it so | |
5656 | # we only want a partial path. | |
5657 | my $sp = ''; | |
5658 | my $final = join('/', @pc); | |
5659 | while (@pc) { | |
5660 | $reporter->set_path($sp, $rev_b, 0, @lock, $pool); | |
5661 | $sp .= '/' if length $sp; | |
5662 | $sp .= shift @pc; | |
5663 | } | |
5664 | die "BUG: '$sp' != '$final'\n" if ($sp ne $final); | |
5665 | ||
2b27f6c8 EW |
5666 | $reporter->set_path($sp, $rev_a, $new, @lock, $pool); |
5667 | ||
d81bf827 EW |
5668 | $reporter->finish_report($pool); |
5669 | $pool->clear; | |
5670 | $editor->{git_commit_ok}; | |
5671 | } | |
5672 | ||
2b27f6c8 EW |
5673 | # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and |
5674 | # svn_ra_reparent didn't work before 1.4) | |
d81bf827 | 5675 | sub gs_do_switch { |
8a603774 EW |
5676 | my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_; |
5677 | my $path = $gs->{path}; | |
d81bf827 | 5678 | my $pool = SVN::Pool->new; |
2b27f6c8 EW |
5679 | |
5680 | my $full_url = $self->{url}; | |
5681 | my $old_url = $full_url; | |
2a679c7a | 5682 | $full_url .= '/' . $path if length $path; |
5d3b7cd5 | 5683 | my ($ra, $reparented); |
ad0a82ba | 5684 | |
2a679c7a EW |
5685 | if ($old_url =~ m#^svn(\+ssh)?://# || |
5686 | ($full_url =~ m#^https?://# && | |
5687 | escape_url($full_url) ne $full_url)) { | |
ad0a82ba AB |
5688 | $_[0] = undef; |
5689 | $self = undef; | |
5690 | $RA = undef; | |
5691 | $ra = Git::SVN::Ra->new($full_url); | |
5692 | $ra_invalid = 1; | |
5693 | } elsif ($old_url ne $full_url) { | |
5694 | SVN::_Ra::svn_ra_reparent($self->{session}, $full_url, $pool); | |
5695 | $self->{url} = $full_url; | |
5696 | $reparented = 1; | |
5d3b7cd5 | 5697 | } |
ad0a82ba | 5698 | |
5d3b7cd5 | 5699 | $ra ||= $self; |
f4392df4 | 5700 | $url_b = escape_url($url_b); |
8a603774 | 5701 | my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool); |
d81bf827 | 5702 | my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : (); |
8b8fc068 | 5703 | $reporter->set_path('', $rev_a, 0, @lock, $pool); |
d81bf827 | 5704 | $reporter->finish_report($pool); |
2b27f6c8 | 5705 | |
5d3b7cd5 EW |
5706 | if ($reparented) { |
5707 | SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool); | |
5708 | $self->{url} = $old_url; | |
5709 | } | |
2b27f6c8 | 5710 | |
d81bf827 EW |
5711 | $pool->clear; |
5712 | $editor->{git_commit_ok}; | |
5713 | } | |
5714 | ||
b54a901e EW |
5715 | sub longest_common_path { |
5716 | my ($gsv, $globs) = @_; | |
d2ae1434 | 5717 | my %common; |
e518192f EW |
5718 | my $common_max = scalar @$gsv; |
5719 | ||
5720 | foreach my $gs (@$gsv) { | |
d2ae1434 EW |
5721 | my @tmp = split m#/#, $gs->{path}; |
5722 | my $p = ''; | |
5723 | foreach (@tmp) { | |
5724 | $p .= length($p) ? "/$_" : $_; | |
5725 | $common{$p} ||= 0; | |
5726 | $common{$p}++; | |
5727 | } | |
5728 | } | |
e518192f EW |
5729 | $globs ||= []; |
5730 | $common_max += scalar @$globs; | |
5731 | foreach my $glob (@$globs) { | |
5732 | my @tmp = split m#/#, $glob->{path}->{left}; | |
5733 | my $p = ''; | |
5734 | foreach (@tmp) { | |
5735 | $p .= length($p) ? "/$_" : $_; | |
5736 | $common{$p} ||= 0; | |
5737 | $common{$p}++; | |
5738 | } | |
5739 | } | |
5740 | ||
d2ae1434 EW |
5741 | my $longest_path = ''; |
5742 | foreach (sort {length $b <=> length $a} keys %common) { | |
e518192f | 5743 | if ($common{$_} == $common_max) { |
d2ae1434 EW |
5744 | $longest_path = $_; |
5745 | last; | |
5746 | } | |
0af9c9f9 | 5747 | } |
b54a901e EW |
5748 | $longest_path; |
5749 | } | |
5750 | ||
5751 | sub gs_fetch_loop_common { | |
5752 | my ($self, $base, $head, $gsv, $globs) = @_; | |
5753 | return if ($base > $head); | |
5754 | my $inc = $_log_window_size; | |
5755 | my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc); | |
5756 | my $longest_path = longest_common_path($gsv, $globs); | |
a51cdb0c | 5757 | my $ra_url = $self->{url}; |
c69700fe | 5758 | my $find_trailing_edge; |
0af9c9f9 | 5759 | while (1) { |
d4eff2bd | 5760 | my %revs; |
d2ae1434 | 5761 | my $err; |
f7c3fc4a | 5762 | my $err_handler = $SVN::Error::handler; |
d2ae1434 EW |
5763 | $SVN::Error::handler = sub { |
5764 | ($err) = @_; | |
5765 | skip_unknown_revs($err); | |
5766 | }; | |
5767 | sub _cb { | |
5768 | my ($paths, $r, $author, $date, $log) = @_; | |
3c49a035 | 5769 | [ $paths, |
d2ae1434 EW |
5770 | { author => $author, date => $date, log => $log } ]; |
5771 | } | |
5772 | $self->get_log([$longest_path], $min, $max, 0, 1, 1, | |
5773 | sub { $revs{$_[1]} = _cb(@_) }); | |
99366565 DM |
5774 | if ($err) { |
5775 | print "Checked through r$max\r"; | |
c69700fe AV |
5776 | } else { |
5777 | $find_trailing_edge = 1; | |
99366565 | 5778 | } |
c69700fe | 5779 | if ($err and $find_trailing_edge) { |
d2ae1434 EW |
5780 | print STDERR "Path '$longest_path' ", |
5781 | "was probably deleted:\n", | |
5782 | $err->expanded_message, | |
5783 | "\nWill attempt to follow ", | |
5784 | "revisions r$min .. r$max ", | |
5785 | "committed before the deletion\n"; | |
5786 | my $hi = $max; | |
5787 | while (--$hi >= $min) { | |
5788 | my $ok; | |
5789 | $self->get_log([$longest_path], $min, $hi, | |
5790 | 0, 1, 1, sub { | |
b6c61778 | 5791 | $ok = $_[1]; |
d2ae1434 EW |
5792 | $revs{$_[1]} = _cb(@_) }); |
5793 | if ($ok) { | |
5794 | print STDERR "r$min .. r$ok OK\n"; | |
5795 | last; | |
5796 | } | |
5797 | } | |
c69700fe | 5798 | $find_trailing_edge = 0; |
d2ae1434 | 5799 | } |
d4eff2bd | 5800 | $SVN::Error::handler = $err_handler; |
fbcc1737 | 5801 | |
e518192f | 5802 | my %exists = map { $_->{path} => $_ } @$gsv; |
d4eff2bd | 5803 | foreach my $r (sort {$a <=> $b} keys %revs) { |
fbcc1737 | 5804 | my ($paths, $logged) = @{$revs{$r}}; |
e518192f EW |
5805 | |
5806 | foreach my $gs ($self->match_globs(\%exists, $paths, | |
5807 | $globs, $r)) { | |
060610c5 | 5808 | if ($gs->rev_map_max >= $r) { |
fbcc1737 EW |
5809 | next; |
5810 | } | |
5811 | next unless $gs->match_paths($paths, $r); | |
5812 | $gs->{logged_rev_props} = $logged; | |
e8d120bd EW |
5813 | if (my $last_commit = $gs->last_commit) { |
5814 | $gs->assert_index_clean($last_commit); | |
5815 | } | |
fbcc1737 EW |
5816 | my $log_entry = $gs->do_fetch($paths, $r); |
5817 | if ($log_entry) { | |
0af9c9f9 EW |
5818 | $gs->do_git_commit($log_entry); |
5819 | } | |
321b1842 | 5820 | $INDEX_FILES{$gs->{index}} = 1; |
0af9c9f9 | 5821 | } |
e518192f | 5822 | foreach my $g (@$globs) { |
93f2689c EW |
5823 | my $k = "svn-remote.$g->{remote}." . |
5824 | "$g->{t}-maxRev"; | |
5825 | Git::SVN::tmp_config($k, $r); | |
e518192f | 5826 | } |
a51cdb0c EW |
5827 | if ($ra_invalid) { |
5828 | $_[0] = undef; | |
5829 | $self = undef; | |
5830 | $RA = undef; | |
5831 | $self = Git::SVN::Ra->new($ra_url); | |
5832 | $ra_invalid = undef; | |
5833 | } | |
0af9c9f9 | 5834 | } |
9c93fee5 EW |
5835 | # pre-fill the .rev_db since it'll eventually get filled in |
5836 | # with '0' x40 if something new gets committed | |
e518192f | 5837 | foreach my $gs (@$gsv) { |
66ab84b9 EW |
5838 | next if $gs->rev_map_max >= $max; |
5839 | next if defined $gs->rev_map_get($max); | |
5840 | $gs->rev_map_set($max, 0 x40); | |
9c93fee5 | 5841 | } |
c3560e53 EW |
5842 | foreach my $g (@$globs) { |
5843 | my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev"; | |
5844 | Git::SVN::tmp_config($k, $max); | |
5845 | } | |
0af9c9f9 EW |
5846 | last if $max >= $head; |
5847 | $min = $max + 1; | |
5848 | $max += $inc; | |
5849 | $max = $head if ($max > $head); | |
5850 | } | |
94bc914c | 5851 | Git::SVN::gc(); |
0af9c9f9 EW |
5852 | } |
5853 | ||
570d35c2 MG |
5854 | sub get_dir_globbed { |
5855 | my ($self, $left, $depth, $r) = @_; | |
5856 | ||
5857 | my @x = eval { $self->get_dir($left, $r) }; | |
5858 | return unless scalar @x == 3; | |
5859 | my $dirents = $x[0]; | |
5860 | my @finalents; | |
5861 | foreach my $de (keys %$dirents) { | |
5862 | next if $dirents->{$de}->{kind} != $SVN::Node::dir; | |
5863 | if ($depth > 1) { | |
5864 | my @args = ("$left/$de", $depth - 1, $r); | |
5865 | foreach my $dir ($self->get_dir_globbed(@args)) { | |
5866 | push @finalents, "$de/$dir"; | |
5867 | } | |
5868 | } else { | |
5869 | push @finalents, $de; | |
5870 | } | |
5871 | } | |
5872 | @finalents; | |
5873 | } | |
5874 | ||
cdb51a13 MO |
5875 | # return value: 0 -- don't ignore, 1 -- ignore |
5876 | sub is_ref_ignored { | |
5877 | my ($g, $p) = @_; | |
5878 | my $refname = $g->{ref}->full_path($p); | |
5879 | return 1 if defined($g->{ignore_refs_regex}) && | |
5880 | $refname =~ m!$g->{ignore_refs_regex}!; | |
5881 | return 0 unless defined($_ignore_refs_regex); | |
5882 | return 1 if $refname =~ m!$_ignore_refs_regex!o; | |
5883 | return 0; | |
5884 | } | |
5885 | ||
e518192f EW |
5886 | sub match_globs { |
5887 | my ($self, $exists, $paths, $globs, $r) = @_; | |
74a81227 EW |
5888 | |
5889 | sub get_dir_check { | |
5890 | my ($self, $exists, $g, $r) = @_; | |
570d35c2 MG |
5891 | |
5892 | my @dirs = $self->get_dir_globbed($g->{path}->{left}, | |
5893 | $g->{path}->{depth}, | |
5894 | $r); | |
5895 | ||
5896 | foreach my $de (@dirs) { | |
74a81227 EW |
5897 | my $p = $g->{path}->full_path($de); |
5898 | next if $exists->{$p}; | |
5899 | next if (length $g->{path}->{right} && | |
5900 | ($self->check_path($p, $r) != | |
5901 | $SVN::Node::dir)); | |
07576208 | 5902 | next unless $p =~ /$g->{path}->{regex}/; |
74a81227 EW |
5903 | $exists->{$p} = Git::SVN->init($self->{url}, $p, undef, |
5904 | $g->{ref}->full_path($de), 1); | |
5905 | } | |
5906 | } | |
e518192f | 5907 | foreach my $g (@$globs) { |
74a81227 EW |
5908 | if (my $path = $paths->{"/$g->{path}->{left}"}) { |
5909 | if ($path->{action} =~ /^[AR]$/) { | |
5910 | get_dir_check($self, $exists, $g, $r); | |
5911 | } | |
5912 | } | |
e518192f | 5913 | foreach (keys %$paths) { |
28710f74 EW |
5914 | if (/$g->{path}->{left_regex}/ && |
5915 | !/$g->{path}->{regex}/) { | |
74a81227 EW |
5916 | next if $paths->{$_}->{action} !~ /^[AR]$/; |
5917 | get_dir_check($self, $exists, $g, $r); | |
5918 | } | |
e518192f EW |
5919 | next unless /$g->{path}->{regex}/; |
5920 | my $p = $1; | |
5921 | my $pathname = $g->{path}->full_path($p); | |
cdb51a13 | 5922 | next if is_ref_ignored($g, $p); |
e518192f | 5923 | next if $exists->{$pathname}; |
0c1ec5a1 EW |
5924 | next if ($self->check_path($pathname, $r) != |
5925 | $SVN::Node::dir); | |
e518192f EW |
5926 | $exists->{$pathname} = Git::SVN->init( |
5927 | $self->{url}, $pathname, undef, | |
5928 | $g->{ref}->full_path($p), 1); | |
5929 | } | |
5930 | my $c = ''; | |
5931 | foreach (split m#/#, $g->{path}->{left}) { | |
5932 | $c .= "/$_"; | |
5933 | next unless ($paths->{$c} && | |
74a81227 EW |
5934 | ($paths->{$c}->{action} =~ /^[AR]$/)); |
5935 | get_dir_check($self, $exists, $g, $r); | |
e518192f EW |
5936 | } |
5937 | } | |
5938 | values %$exists; | |
5939 | } | |
5940 | ||
e6434f87 EW |
5941 | sub minimize_url { |
5942 | my ($self) = @_; | |
5943 | return $self->{url} if ($self->{url} eq $self->{repos_root}); | |
5944 | my $url = $self->{repos_root}; | |
5945 | my @components = split(m!/!, $self->{svn_path}); | |
5946 | my $c = ''; | |
5947 | do { | |
5948 | $url .= "/$c" if length $c; | |
5f8b2cba EW |
5949 | eval { |
5950 | my $ra = (ref $self)->new($url); | |
5951 | my $latest = $ra->get_latest_revnum; | |
5952 | $ra->get_log("", $latest, 0, 1, 0, 1, sub {}); | |
5953 | }; | |
e6434f87 EW |
5954 | } while ($@ && ($c = shift @components)); |
5955 | $url; | |
5956 | } | |
5957 | ||
d81bf827 EW |
5958 | sub can_do_switch { |
5959 | my $self = shift; | |
5960 | unless (defined $can_do_switch) { | |
5961 | my $pool = SVN::Pool->new; | |
5962 | my $rep = eval { | |
5963 | $self->do_switch(1, '', 0, $self->{url}, | |
5964 | SVN::Delta::Editor->new, $pool); | |
5965 | }; | |
5966 | if ($@) { | |
5967 | $can_do_switch = 0; | |
5968 | } else { | |
5969 | $rep->abort_report($pool); | |
5970 | $can_do_switch = 1; | |
5971 | } | |
5972 | $pool->clear; | |
5973 | } | |
5974 | $can_do_switch; | |
5975 | } | |
5976 | ||
0af9c9f9 EW |
5977 | sub skip_unknown_revs { |
5978 | my ($err) = @_; | |
5979 | my $errno = $err->apr_err(); | |
5980 | # Maybe the branch we're tracking didn't | |
5981 | # exist when the repo started, so it's | |
5982 | # not an error if it doesn't, just continue | |
5983 | # | |
5984 | # Wonderfully consistent library, eh? | |
5985 | # 160013 - svn:// and file:// | |
5986 | # 175002 - http(s):// | |
5987 | # 175007 - http(s):// (this repo required authorization, too...) | |
5988 | # More codes may be discovered later... | |
5989 | if ($errno == 175007 || $errno == 175002 || $errno == 160013) { | |
a6a15a99 EW |
5990 | my $err_key = $err->expanded_message; |
5991 | # revision numbers change every time, filter them out | |
5992 | $err_key =~ s/\d+/\0/g; | |
5993 | $err_key = "$errno\0$err_key"; | |
5994 | unless ($ignored_err{$err_key}) { | |
5995 | warn "W: Ignoring error from SVN, path probably ", | |
5996 | "does not exist: ($errno): ", | |
5997 | $err->expanded_message,"\n"; | |
eee8a174 EW |
5998 | warn "W: Do not be alarmed at the above message ", |
5999 | "git-svn is just searching aggressively for ", | |
6000 | "old history.\n", | |
6001 | "This may take a while on large repositories\n"; | |
a6a15a99 EW |
6002 | $ignored_err{$err_key} = 1; |
6003 | } | |
0af9c9f9 EW |
6004 | return; |
6005 | } | |
6006 | die "Error from SVN, ($errno): ", $err->expanded_message,"\n"; | |
6007 | } | |
6008 | ||
f8c9d1d2 EW |
6009 | package Git::SVN::Log; |
6010 | use strict; | |
6011 | use warnings; | |
6012 | use POSIX qw/strftime/; | |
111947ef | 6013 | use constant commit_log_separator => ('-' x 72) . "\n"; |
f8c9d1d2 EW |
6014 | use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline |
6015 | %rusers $show_commit $incremental/; | |
6016 | my $l_fmt; | |
6017 | ||
6018 | sub cmt_showable { | |
6019 | my ($c) = @_; | |
6020 | return 1 if defined $c->{r}; | |
c16d0871 EW |
6021 | |
6022 | # big commit message got truncated by the 16k pretty buffer in rev-list | |
f8c9d1d2 EW |
6023 | if ($c->{l} && $c->{l}->[-1] eq "...\n" && |
6024 | $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) { | |
c16d0871 | 6025 | @{$c->{l}} = (); |
44320b9e | 6026 | my @log = command(qw/cat-file commit/, $c->{c}); |
c16d0871 EW |
6027 | |
6028 | # shift off the headers | |
6029 | shift @log while ($log[0] ne ''); | |
44320b9e | 6030 | shift @log; |
c16d0871 EW |
6031 | |
6032 | # TODO: make $c->{l} not have a trailing newline in the future | |
6033 | @{$c->{l}} = map { "$_\n" } grep !/^git-svn-id: /, @log; | |
f8c9d1d2 EW |
6034 | |
6035 | (undef, $c->{r}, undef) = ::extract_metadata( | |
44320b9e | 6036 | (grep(/^git-svn-id: /, @log))[-1]); |
f8c9d1d2 EW |
6037 | } |
6038 | return defined $c->{r}; | |
6039 | } | |
6040 | ||
6041 | sub log_use_color { | |
cd459e3f | 6042 | return $color || Git->repository->get_colorbool('color.diff'); |
f8c9d1d2 EW |
6043 | } |
6044 | ||
6045 | sub git_svn_log_cmd { | |
3bc718ba EW |
6046 | my ($r_min, $r_max, @args) = @_; |
6047 | my $head = 'HEAD'; | |
6ed77266 | 6048 | my (@files, @log_opts); |
3bc718ba | 6049 | foreach my $x (@args) { |
6ed77266 EW |
6050 | if ($x eq '--' || @files) { |
6051 | push @files, $x; | |
6052 | } else { | |
6053 | if (::verify_ref("$x^0")) { | |
6054 | $head = $x; | |
6055 | } else { | |
6056 | push @log_opts, $x; | |
6057 | } | |
6058 | } | |
3bc718ba EW |
6059 | } |
6060 | ||
13c823fb EW |
6061 | my ($url, $rev, $uuid, $gs) = ::working_head_info($head); |
6062 | $gs ||= Git::SVN->_new; | |
f8c9d1d2 EW |
6063 | my @cmd = (qw/log --abbrev-commit --pretty=raw --default/, |
6064 | $gs->refname); | |
6065 | push @cmd, '-r' unless $non_recursive; | |
6066 | push @cmd, qw/--raw --name-status/ if $verbose; | |
6067 | push @cmd, '--color' if log_use_color(); | |
6ed77266 EW |
6068 | push @cmd, @log_opts; |
6069 | if (defined $r_max && $r_max == $r_min) { | |
f8c9d1d2 | 6070 | push @cmd, '--max-count=1'; |
060610c5 | 6071 | if (my $c = $gs->rev_map_get($r_max)) { |
f8c9d1d2 EW |
6072 | push @cmd, $c; |
6073 | } | |
6ed77266 | 6074 | } elsif (defined $r_max) { |
111947ef DK |
6075 | if ($r_max < $r_min) { |
6076 | ($r_min, $r_max) = ($r_max, $r_min); | |
6077 | } | |
6078 | my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min); | |
6079 | my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max); | |
6080 | # If there are no commits in the range, both $c_max and $c_min | |
6081 | # will be undefined. If there is at least 1 commit in the | |
6082 | # range, both will be defined. | |
6083 | return () if !defined $c_min || !defined $c_max; | |
6084 | if ($c_min eq $c_max) { | |
6085 | push @cmd, '--max-count=1', $c_min; | |
f8c9d1d2 | 6086 | } else { |
111947ef | 6087 | push @cmd, '--boundary', "$c_min..$c_max"; |
f8c9d1d2 EW |
6088 | } |
6089 | } | |
6ed77266 | 6090 | return (@cmd, @files); |
f8c9d1d2 EW |
6091 | } |
6092 | ||
6093 | # adapted from pager.c | |
6094 | sub config_pager { | |
190c1cda JN |
6095 | if (! -t *STDOUT) { |
6096 | $ENV{GIT_PAGER_IN_USE} = 'false'; | |
6097 | $pager = undef; | |
6098 | return; | |
6099 | } | |
6100 | chomp($pager = command_oneline(qw(var GIT_PAGER))); | |
dec543e6 | 6101 | if ($pager eq 'cat') { |
f8c9d1d2 EW |
6102 | $pager = undef; |
6103 | } | |
cd459e3f | 6104 | $ENV{GIT_PAGER_IN_USE} = defined($pager); |
f8c9d1d2 EW |
6105 | } |
6106 | ||
6107 | sub run_pager { | |
190c1cda | 6108 | return unless defined $pager; |
971e6283 | 6109 | pipe my ($rfd, $wfd) or return; |
207f1a75 | 6110 | defined(my $pid = fork) or ::fatal "Can't fork: $!"; |
f8c9d1d2 EW |
6111 | if (!$pid) { |
6112 | open STDOUT, '>&', $wfd or | |
207f1a75 | 6113 | ::fatal "Can't redirect to stdout: $!"; |
f8c9d1d2 EW |
6114 | return; |
6115 | } | |
207f1a75 | 6116 | open STDIN, '<&', $rfd or ::fatal "Can't redirect stdin: $!"; |
f8c9d1d2 | 6117 | $ENV{LESS} ||= 'FRSX'; |
207f1a75 | 6118 | exec $pager or ::fatal "Can't run pager: $! ($pager)"; |
f8c9d1d2 EW |
6119 | } |
6120 | ||
b2b3ada7 | 6121 | sub format_svn_date { |
736e619a | 6122 | my $t = shift || time; |
6aa17fc6 | 6123 | my $gmoff = Git::SVN::get_tz($t); |
e8717841 | 6124 | return strftime("%Y-%m-%d %H:%M:%S $gmoff (%a, %d %b %Y)", localtime($t)); |
b2b3ada7 DK |
6125 | } |
6126 | ||
6127 | sub parse_git_date { | |
6128 | my ($t, $tz) = @_; | |
6129 | # Date::Parse isn't in the standard Perl distro :( | |
6130 | if ($tz =~ s/^\+//) { | |
6131 | $t += tz_to_s_offset($tz); | |
6132 | } elsif ($tz =~ s/^\-//) { | |
6133 | $t -= tz_to_s_offset($tz); | |
6134 | } | |
6135 | return $t; | |
6136 | } | |
6137 | ||
6138 | sub set_local_timezone { | |
6139 | if (defined $TZ) { | |
6140 | $ENV{TZ} = $TZ; | |
6141 | } else { | |
6142 | delete $ENV{TZ}; | |
6143 | } | |
6144 | } | |
6145 | ||
21819a37 EW |
6146 | sub tz_to_s_offset { |
6147 | my ($tz) = @_; | |
6148 | $tz =~ s/(\d\d)$//; | |
6149 | return ($1 * 60) + ($tz * 3600); | |
6150 | } | |
6151 | ||
f8c9d1d2 EW |
6152 | sub get_author_info { |
6153 | my ($dest, $author, $t, $tz) = @_; | |
6154 | $author =~ s/(?:^\s*|\s*$)//g; | |
6155 | $dest->{a_raw} = $author; | |
6156 | my $au; | |
1c8443b0 | 6157 | if ($::_authors) { |
f8c9d1d2 EW |
6158 | $au = $rusers{$author} || undef; |
6159 | } | |
6160 | if (!$au) { | |
6161 | ($au) = ($author =~ /<([^>]+)\@[^>]+>$/); | |
6162 | } | |
6163 | $dest->{t} = $t; | |
6164 | $dest->{tz} = $tz; | |
6165 | $dest->{a} = $au; | |
b2b3ada7 | 6166 | $dest->{t_utc} = parse_git_date($t, $tz); |
f8c9d1d2 EW |
6167 | } |
6168 | ||
6169 | sub process_commit { | |
6170 | my ($c, $r_min, $r_max, $defer) = @_; | |
6171 | if (defined $r_min && defined $r_max) { | |
6172 | if ($r_min == $c->{r} && $r_min == $r_max) { | |
6173 | show_commit($c); | |
6174 | return 0; | |
6175 | } | |
6176 | return 1 if $r_min == $r_max; | |
6177 | if ($r_min < $r_max) { | |
6178 | # we need to reverse the print order | |
6179 | return 0 if (defined $limit && --$limit < 0); | |
6180 | push @$defer, $c; | |
6181 | return 1; | |
6182 | } | |
6183 | if ($r_min != $r_max) { | |
6184 | return 1 if ($r_min < $c->{r}); | |
6185 | return 1 if ($r_max > $c->{r}); | |
6186 | } | |
6187 | } | |
6188 | return 0 if (defined $limit && --$limit < 0); | |
6189 | show_commit($c); | |
6190 | return 1; | |
6191 | } | |
6192 | ||
6193 | sub show_commit { | |
6194 | my $c = shift; | |
6195 | if ($oneline) { | |
6196 | my $x = "\n"; | |
6197 | if (my $l = $c->{l}) { | |
6198 | while ($l->[0] =~ /^\s*$/) { shift @$l } | |
6199 | $x = $l->[0]; | |
6200 | } | |
6201 | $l_fmt ||= 'A' . length($c->{r}); | |
6202 | print 'r',pack($l_fmt, $c->{r}),' | '; | |
6203 | print "$c->{c} | " if $show_commit; | |
6204 | print $x; | |
6205 | } else { | |
6206 | show_commit_normal($c); | |
6207 | } | |
6208 | } | |
6209 | ||
6210 | sub show_commit_changed_paths { | |
6211 | my ($c) = @_; | |
6212 | return unless $c->{changed}; | |
6213 | print "Changed paths:\n", @{$c->{changed}}; | |
6214 | } | |
6215 | ||
6216 | sub show_commit_normal { | |
6217 | my ($c) = @_; | |
111947ef | 6218 | print commit_log_separator, "r$c->{r} | "; |
f8c9d1d2 | 6219 | print "$c->{c} | " if $show_commit; |
b2b3ada7 | 6220 | print "$c->{a} | ", format_svn_date($c->{t_utc}), ' | '; |
f8c9d1d2 EW |
6221 | my $nr_line = 0; |
6222 | ||
6223 | if (my $l = $c->{l}) { | |
6224 | while ($l->[$#$l] eq "\n" && $#$l > 0 | |
6225 | && $l->[($#$l - 1)] eq "\n") { | |
6226 | pop @$l; | |
6227 | } | |
6228 | $nr_line = scalar @$l; | |
6229 | if (!$nr_line) { | |
6230 | print "1 line\n\n\n"; | |
6231 | } else { | |
6232 | if ($nr_line == 1) { | |
6233 | $nr_line = '1 line'; | |
6234 | } else { | |
6235 | $nr_line .= ' lines'; | |
6236 | } | |
6237 | print $nr_line, "\n"; | |
6238 | show_commit_changed_paths($c); | |
6239 | print "\n"; | |
6240 | print $_ foreach @$l; | |
6241 | } | |
6242 | } else { | |
6243 | print "1 line\n"; | |
6244 | show_commit_changed_paths($c); | |
6245 | print "\n"; | |
6246 | ||
6247 | } | |
488a63ec | 6248 | foreach my $x (qw/raw stat diff/) { |
f8c9d1d2 EW |
6249 | if ($c->{$x}) { |
6250 | print "\n"; | |
6251 | print $_ foreach @{$c->{$x}} | |
6252 | } | |
6253 | } | |
6254 | } | |
6255 | ||
6256 | sub cmd_show_log { | |
6257 | my (@args) = @_; | |
6258 | my ($r_min, $r_max); | |
6259 | my $r_last = -1; # prevent dupes | |
b2b3ada7 | 6260 | set_local_timezone(); |
f8c9d1d2 EW |
6261 | if (defined $::_revision) { |
6262 | if ($::_revision =~ /^(\d+):(\d+)$/) { | |
6263 | ($r_min, $r_max) = ($1, $2); | |
6264 | } elsif ($::_revision =~ /^\d+$/) { | |
6265 | $r_min = $r_max = $::_revision; | |
6266 | } else { | |
6267 | ::fatal "-r$::_revision is not supported, use ", | |
207f1a75 | 6268 | "standard 'git log' arguments instead"; |
f8c9d1d2 EW |
6269 | } |
6270 | } | |
6271 | ||
6272 | config_pager(); | |
6ed77266 | 6273 | @args = git_svn_log_cmd($r_min, $r_max, @args); |
111947ef DK |
6274 | if (!@args) { |
6275 | print commit_log_separator unless $incremental || $oneline; | |
6276 | return; | |
6277 | } | |
f8c9d1d2 EW |
6278 | my $log = command_output_pipe(@args); |
6279 | run_pager(); | |
488a63ec | 6280 | my (@k, $c, $d, $stat); |
f8c9d1d2 EW |
6281 | my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/; |
6282 | while (<$log>) { | |
9963e025 | 6283 | if (/^${esc_color}commit (?:- )?($::sha1_short)/o) { |
f8c9d1d2 EW |
6284 | my $cmt = $1; |
6285 | if ($c && cmt_showable($c) && $c->{r} != $r_last) { | |
6286 | $r_last = $c->{r}; | |
6287 | process_commit($c, $r_min, $r_max, \@k) or | |
6288 | goto out; | |
6289 | } | |
6290 | $d = undef; | |
6291 | $c = { c => $cmt }; | |
6292 | } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) { | |
6293 | get_author_info($c, $1, $2, $3); | |
6294 | } elsif (/^${esc_color}(?:tree|parent|committer) /o) { | |
6295 | # ignore | |
6296 | } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) { | |
6297 | push @{$c->{raw}}, $_; | |
6298 | } elsif (/^${esc_color}[ACRMDT]\t/) { | |
6299 | # we could add $SVN->{svn_path} here, but that requires | |
6300 | # remote access at the moment (repo_path_split)... | |
6301 | s#^(${esc_color})([ACRMDT])\t#$1 $2 #o; | |
6302 | push @{$c->{changed}}, $_; | |
6303 | } elsif (/^${esc_color}diff /o) { | |
6304 | $d = 1; | |
6305 | push @{$c->{diff}}, $_; | |
6306 | } elsif ($d) { | |
6307 | push @{$c->{diff}}, $_; | |
488a63ec EW |
6308 | } elsif (/^\ .+\ \|\s*\d+\ $esc_color[\+\-]* |
6309 | $esc_color*[\+\-]*$esc_color$/x) { | |
6310 | $stat = 1; | |
6311 | push @{$c->{stat}}, $_; | |
6312 | } elsif ($stat && /^ \d+ files changed, \d+ insertions/) { | |
6313 | push @{$c->{stat}}, $_; | |
6314 | $stat = undef; | |
f8c9d1d2 EW |
6315 | } elsif (/^${esc_color} (git-svn-id:.+)$/o) { |
6316 | ($c->{url}, $c->{r}, undef) = ::extract_metadata($1); | |
6317 | } elsif (s/^${esc_color} //o) { | |
6318 | push @{$c->{l}}, $_; | |
6319 | } | |
6320 | } | |
6321 | if ($c && defined $c->{r} && $c->{r} != $r_last) { | |
6322 | $r_last = $c->{r}; | |
6323 | process_commit($c, $r_min, $r_max, \@k); | |
6324 | } | |
6325 | if (@k) { | |
111947ef | 6326 | ($r_min, $r_max) = ($r_max, $r_min); |
f8c9d1d2 EW |
6327 | process_commit($_, $r_min, $r_max) foreach reverse @k; |
6328 | } | |
6329 | out: | |
c843c464 | 6330 | close $log; |
111947ef | 6331 | print commit_log_separator unless $incremental || $oneline; |
f8c9d1d2 EW |
6332 | } |
6333 | ||
6fb5375e | 6334 | sub cmd_blame { |
4be40381 | 6335 | my $path = pop; |
6fb5375e TS |
6336 | |
6337 | config_pager(); | |
6338 | run_pager(); | |
6339 | ||
4be40381 SG |
6340 | my ($fh, $ctx, $rev); |
6341 | ||
6342 | if ($_git_format) { | |
6343 | ($fh, $ctx) = command_output_pipe('blame', @_, $path); | |
6344 | while (my $line = <$fh>) { | |
6345 | if ($line =~ /^\^?([[:xdigit:]]+)\s/) { | |
6346 | # Uncommitted edits show up as a rev ID of | |
6347 | # all zeros, which we can't look up with | |
6348 | # cmt_metadata | |
6349 | if ($1 !~ /^0+$/) { | |
6350 | (undef, $rev, undef) = | |
6351 | ::cmt_metadata($1); | |
6352 | $rev = '0' if (!$rev); | |
6353 | } else { | |
6354 | $rev = '0'; | |
6355 | } | |
6356 | $rev = sprintf('%-10s', $rev); | |
6357 | $line =~ s/^\^?[[:xdigit:]]+(\s)/$rev$1/; | |
6358 | } | |
6359 | print $line; | |
6360 | } | |
6361 | } else { | |
6362 | ($fh, $ctx) = command_output_pipe('blame', '-p', @_, 'HEAD', | |
6363 | '--', $path); | |
6364 | my ($sha1); | |
6365 | my %authors; | |
6ea42032 BB |
6366 | my @buffer; |
6367 | my %dsha; #distinct sha keys | |
6368 | ||
4be40381 | 6369 | while (my $line = <$fh>) { |
6ea42032 BB |
6370 | push @buffer, $line; |
6371 | if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) { | |
6372 | $dsha{$1} = 1; | |
6373 | } | |
6374 | } | |
6375 | ||
6376 | my $s2r = ::cmt_sha2rev_batch([keys %dsha]); | |
6377 | ||
6378 | foreach my $line (@buffer) { | |
4be40381 | 6379 | if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) { |
6ea42032 BB |
6380 | $rev = $s2r->{$1}; |
6381 | $rev = '0' if (!$rev) | |
4be40381 SG |
6382 | } |
6383 | elsif ($line =~ /^author (.*)/) { | |
6384 | $authors{$rev} = $1; | |
6385 | $authors{$rev} =~ s/\s/_/g; | |
6386 | } | |
6387 | elsif ($line =~ /^\t(.*)$/) { | |
6388 | printf("%6s %10s %s\n", $rev, $authors{$rev}, $1); | |
6389 | } | |
6fb5375e | 6390 | } |
6fb5375e TS |
6391 | } |
6392 | command_close_pipe($fh, $ctx); | |
6393 | } | |
6394 | ||
706587fc EW |
6395 | package Git::SVN::Migration; |
6396 | # these version numbers do NOT correspond to actual version numbers | |
6397 | # of git nor git-svn. They are just relative. | |
6398 | # | |
6399 | # v0 layout: .git/$id/info/url, refs/heads/$id-HEAD | |
6400 | # | |
6401 | # v1 layout: .git/$id/info/url, refs/remotes/$id | |
6402 | # | |
6403 | # v2 layout: .git/svn/$id/info/url, refs/remotes/$id | |
6404 | # | |
6405 | # v3 layout: .git/svn/$id, refs/remotes/$id | |
6406 | # - info/url may remain for backwards compatibility | |
6407 | # - this is what we migrate up to this layout automatically, | |
6408 | # - this will be used by git svn init on single branches | |
26a62d57 EW |
6409 | # v3.1 layout (auto migrated): |
6410 | # - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink | |
6411 | # for backwards compatibility | |
706587fc EW |
6412 | # |
6413 | # v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id | |
6414 | # - this is only created for newly multi-init-ed | |
6415 | # repositories. Similar in spirit to the | |
6416 | # --use-separate-remotes option in git-clone (now default) | |
6417 | # - we do not automatically migrate to this (following | |
6418 | # the example set by core git) | |
060610c5 EW |
6419 | # |
6420 | # v5 layout: .rev_db.$UUID => .rev_map.$UUID | |
6421 | # - newer, more-efficient format that uses 24-bytes per record | |
6422 | # with no filler space. | |
6423 | # - use xxd -c24 < .rev_map.$UUID to view and debug | |
6424 | # - This is a one-way migration, repositories updated to the | |
6425 | # new format will not be able to use old git-svn without | |
6426 | # rebuilding the .rev_db. Rebuilding the rev_db is not | |
6427 | # possible if noMetadata or useSvmProps are set; but should | |
6428 | # be no problem for users that use the (sensible) defaults. | |
706587fc EW |
6429 | use strict; |
6430 | use warnings; | |
6431 | use Carp qw/croak/; | |
6432 | use File::Path qw/mkpath/; | |
47e39c55 EW |
6433 | use File::Basename qw/dirname basename/; |
6434 | use vars qw/$_minimize/; | |
706587fc EW |
6435 | |
6436 | sub migrate_from_v0 { | |
6437 | my $git_dir = $ENV{GIT_DIR}; | |
6438 | return undef unless -d $git_dir; | |
6439 | my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/); | |
6440 | my $migrated = 0; | |
6441 | while (<$fh>) { | |
6442 | chomp; | |
6443 | my ($id, $orig_ref) = ($_, $_); | |
6444 | next unless $id =~ s#^refs/heads/(.+)-HEAD$#$1#; | |
6445 | next unless -f "$git_dir/$id/info/url"; | |
6446 | my $new_ref = "refs/remotes/$id"; | |
6447 | if (::verify_ref("$new_ref^0")) { | |
6448 | print STDERR "W: $orig_ref is probably an old ", | |
6449 | "branch used by an ancient version of ", | |
6450 | "git-svn.\n", | |
6451 | "However, $new_ref also exists.\n", | |
6452 | "We will not be able ", | |
6453 | "to use this branch until this ", | |
6454 | "ambiguity is resolved.\n"; | |
6455 | next; | |
6456 | } | |
6457 | print STDERR "Migrating from v0 layout...\n" if !$migrated; | |
6458 | print STDERR "Renaming ref: $orig_ref => $new_ref\n"; | |
6459 | command_noisy('update-ref', $new_ref, $orig_ref); | |
6460 | command_noisy('update-ref', '-d', $orig_ref, $orig_ref); | |
6461 | $migrated++; | |
6462 | } | |
6463 | command_close_pipe($fh, $ctx); | |
6464 | print STDERR "Done migrating from v0 layout...\n" if $migrated; | |
6465 | $migrated; | |
6466 | } | |
6467 | ||
6468 | sub migrate_from_v1 { | |
6469 | my $git_dir = $ENV{GIT_DIR}; | |
6470 | my $migrated = 0; | |
6471 | return $migrated unless -d $git_dir; | |
6472 | my $svn_dir = "$git_dir/svn"; | |
6473 | ||
6474 | # just in case somebody used 'svn' as their $id at some point... | |
6475 | return $migrated if -d $svn_dir && ! -f "$svn_dir/info/url"; | |
6476 | ||
6477 | print STDERR "Migrating from a git-svn v1 layout...\n"; | |
6478 | mkpath([$svn_dir]); | |
6479 | print STDERR "Data from a previous version of git-svn exists, but\n\t", | |
6480 | "$svn_dir\n\t(required for this version ", | |
8f510bef | 6481 | "($::VERSION) of git-svn) does not exist.\n"; |
706587fc EW |
6482 | my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/); |
6483 | while (<$fh>) { | |
6484 | my $x = $_; | |
6485 | next unless $x =~ s#^refs/remotes/##; | |
6486 | chomp $x; | |
6487 | next unless -f "$git_dir/$x/info/url"; | |
6488 | my $u = eval { ::file_to_s("$git_dir/$x/info/url") }; | |
6489 | next unless $u; | |
6490 | my $dn = dirname("$git_dir/svn/$x"); | |
6491 | mkpath([$dn]) unless -d $dn; | |
6492 | if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID: | |
6493 | mkpath(["$git_dir/svn/svn"]); | |
6494 | print STDERR " - $git_dir/$x/info => ", | |
6495 | "$git_dir/svn/$x/info\n"; | |
6496 | rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or | |
6497 | croak "$!: $x"; | |
6498 | # don't worry too much about these, they probably | |
6499 | # don't exist with repos this old (save for index, | |
6500 | # and we can easily regenerate that) | |
6501 | foreach my $f (qw/unhandled.log index .rev_db/) { | |
6502 | rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f"; | |
6503 | } | |
6504 | } else { | |
6505 | print STDERR " - $git_dir/$x => $git_dir/svn/$x\n"; | |
6506 | rename "$git_dir/$x", "$git_dir/svn/$x" or | |
6507 | croak "$!: $x"; | |
6508 | } | |
6509 | $migrated++; | |
6510 | } | |
6511 | command_close_pipe($fh, $ctx); | |
6512 | print STDERR "Done migrating from a git-svn v1 layout\n"; | |
6513 | $migrated; | |
6514 | } | |
6515 | ||
6516 | sub read_old_urls { | |
6517 | my ($l_map, $pfx, $path) = @_; | |
6518 | my @dir; | |
6519 | foreach (<$path/*>) { | |
6520 | if (-r "$_/info/url") { | |
6521 | $pfx .= '/' if $pfx && $pfx !~ m!/$!; | |
6522 | my $ref_id = $pfx . basename $_; | |
6523 | my $url = ::file_to_s("$_/info/url"); | |
6524 | $l_map->{$ref_id} = $url; | |
6525 | } elsif (-d $_) { | |
6526 | push @dir, $_; | |
6527 | } | |
6528 | } | |
6529 | foreach (@dir) { | |
6530 | my $x = $_; | |
6531 | $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o; | |
6532 | read_old_urls($l_map, $x, $_); | |
6533 | } | |
6534 | } | |
6535 | ||
6536 | sub migrate_from_v2 { | |
6537 | my @cfg = command(qw/config -l/); | |
6538 | return if grep /^svn-remote\..+\.url=/, @cfg; | |
6539 | my %l_map; | |
6540 | read_old_urls(\%l_map, '', "$ENV{GIT_DIR}/svn"); | |
6541 | my $migrated = 0; | |
6542 | ||
6543 | foreach my $ref_id (sort keys %l_map) { | |
471bc000 EW |
6544 | eval { Git::SVN->init($l_map{$ref_id}, '', undef, $ref_id) }; |
6545 | if ($@) { | |
6546 | Git::SVN->init($l_map{$ref_id}, '', $ref_id, $ref_id); | |
6547 | } | |
706587fc EW |
6548 | $migrated++; |
6549 | } | |
6550 | $migrated; | |
6551 | } | |
6552 | ||
47e39c55 EW |
6553 | sub minimize_connections { |
6554 | my $r = Git::SVN::read_all_remotes(); | |
6555 | my $new_urls = {}; | |
6556 | my $root_repos = {}; | |
6557 | foreach my $repo_id (keys %$r) { | |
6558 | my $url = $r->{$repo_id}->{url} or next; | |
6559 | my $fetch = $r->{$repo_id}->{fetch} or next; | |
6560 | my $ra = Git::SVN::Ra->new($url); | |
6561 | ||
6562 | # skip existing cases where we already connect to the root | |
6563 | if (($ra->{url} eq $ra->{repos_root}) || | |
7829f20f | 6564 | ($ra->{repos_root} eq $repo_id)) { |
47e39c55 EW |
6565 | $root_repos->{$ra->{url}} = $repo_id; |
6566 | next; | |
6567 | } | |
6568 | ||
6569 | my $root_ra = Git::SVN::Ra->new($ra->{repos_root}); | |
6570 | my $root_path = $ra->{url}; | |
4e9f6cc7 | 6571 | $root_path =~ s#^\Q$ra->{repos_root}\E(/|$)##; |
47e39c55 EW |
6572 | foreach my $path (keys %$fetch) { |
6573 | my $ref_id = $fetch->{$path}; | |
6574 | my $gs = Git::SVN->new($ref_id, $repo_id, $path); | |
6575 | ||
6576 | # make sure we can read when connecting to | |
6577 | # a higher level of a repository | |
6578 | my ($last_rev, undef) = $gs->last_rev_commit; | |
6579 | if (!defined $last_rev) { | |
6580 | $last_rev = eval { | |
6581 | $root_ra->get_latest_revnum; | |
6582 | }; | |
6583 | next if $@; | |
6584 | } | |
6585 | my $new = $root_path; | |
6586 | $new .= length $path ? "/$path" : ''; | |
6587 | eval { | |
6588 | $root_ra->get_log([$new], $last_rev, $last_rev, | |
6589 | 0, 0, 1, sub { }); | |
6590 | }; | |
6591 | next if $@; | |
6592 | $new_urls->{$ra->{repos_root}}->{$new} = | |
6593 | { ref_id => $ref_id, | |
6594 | old_repo_id => $repo_id, | |
6595 | old_path => $path }; | |
6596 | } | |
6597 | } | |
6598 | ||
6599 | my @emptied; | |
6600 | foreach my $url (keys %$new_urls) { | |
6601 | # see if we can re-use an existing [svn-remote "repo_id"] | |
6602 | # instead of creating a(n ugly) new section: | |
7829f20f | 6603 | my $repo_id = $root_repos->{$url} || $url; |
47e39c55 EW |
6604 | |
6605 | my $fetch = $new_urls->{$url}; | |
6606 | foreach my $path (keys %$fetch) { | |
6607 | my $x = $fetch->{$path}; | |
6608 | Git::SVN->init($url, $path, $repo_id, $x->{ref_id}); | |
6609 | my $pfx = "svn-remote.$x->{old_repo_id}"; | |
6610 | ||
6611 | my $old_fetch = quotemeta("$x->{old_path}:". | |
6f5748e1 | 6612 | "$x->{ref_id}"); |
8b8fc068 | 6613 | command_noisy(qw/config --unset/, |
47e39c55 EW |
6614 | "$pfx.fetch", '^'. $old_fetch . '$'); |
6615 | delete $r->{$x->{old_repo_id}}-> | |
6616 | {fetch}->{$x->{old_path}}; | |
6617 | if (!keys %{$r->{$x->{old_repo_id}}->{fetch}}) { | |
8b8fc068 | 6618 | command_noisy(qw/config --unset/, |
47e39c55 EW |
6619 | "$pfx.url"); |
6620 | push @emptied, $x->{old_repo_id} | |
6621 | } | |
6622 | } | |
6623 | } | |
6624 | if (@emptied) { | |
8befc50c | 6625 | my $file = $ENV{GIT_CONFIG} || "$ENV{GIT_DIR}/config"; |
47e39c55 EW |
6626 | print STDERR <<EOF; |
6627 | The following [svn-remote] sections in your config file ($file) are empty | |
6628 | and can be safely removed: | |
6629 | EOF | |
6630 | print STDERR "[svn-remote \"$_\"]\n" foreach @emptied; | |
6631 | } | |
6632 | } | |
6633 | ||
706587fc EW |
6634 | sub migration_check { |
6635 | migrate_from_v0(); | |
6636 | migrate_from_v1(); | |
6637 | migrate_from_v2(); | |
47e39c55 | 6638 | minimize_connections() if $_minimize; |
706587fc EW |
6639 | } |
6640 | ||
ef3cfaad EW |
6641 | package Git::IndexInfo; |
6642 | use strict; | |
6643 | use warnings; | |
6644 | use Git qw/command_input_pipe command_close_pipe/; | |
6645 | ||
6646 | sub new { | |
6647 | my ($class) = @_; | |
6648 | my ($gui, $ctx) = command_input_pipe(qw/update-index -z --index-info/); | |
6649 | bless { gui => $gui, ctx => $ctx, nr => 0}, $class; | |
6650 | } | |
6651 | ||
6652 | sub remove { | |
6653 | my ($self, $path) = @_; | |
6654 | if (print { $self->{gui} } '0 ', 0 x 40, "\t", $path, "\0") { | |
6655 | return ++$self->{nr}; | |
6656 | } | |
6657 | undef; | |
6658 | } | |
6659 | ||
6660 | sub update { | |
6661 | my ($self, $mode, $hash, $path) = @_; | |
6662 | if (print { $self->{gui} } $mode, ' ', $hash, "\t", $path, "\0") { | |
6663 | return ++$self->{nr}; | |
6664 | } | |
6665 | undef; | |
6666 | } | |
6667 | ||
6668 | sub DESTROY { | |
6669 | my ($self) = @_; | |
6670 | command_close_pipe($self->{gui}, $self->{ctx}); | |
6671 | } | |
6672 | ||
4bb9ed04 EW |
6673 | package Git::SVN::GlobSpec; |
6674 | use strict; | |
6675 | use warnings; | |
6676 | ||
6677 | sub new { | |
07576208 | 6678 | my ($class, $glob, $pattern_ok) = @_; |
4bb9ed04 EW |
6679 | my $re = $glob; |
6680 | $re =~ s!/+$!!g; # no need for trailing slashes | |
07576208 JS |
6681 | my (@left, @right, @patterns); |
6682 | my $state = "left"; | |
6683 | my $die_msg = "Only one set of wildcard directories " . | |
6684 | "(e.g. '*' or '*/*/*') is supported: '$glob'\n"; | |
6685 | for my $part (split(m|/|, $glob)) { | |
6686 | if ($part =~ /\*/ && $part ne "*") { | |
6687 | die "Invalid pattern in '$glob': $part\n"; | |
6688 | } elsif ($pattern_ok && $part =~ /[{}]/ && | |
6689 | $part !~ /^\{[^{}]+\}/) { | |
6690 | die "Invalid pattern in '$glob': $part\n"; | |
6691 | } | |
6692 | if ($part eq "*") { | |
6693 | die $die_msg if $state eq "right"; | |
6694 | $state = "pattern"; | |
6695 | push(@patterns, "[^/]*"); | |
6696 | } elsif ($pattern_ok && $part =~ /^\{(.*)\}$/) { | |
6697 | die $die_msg if $state eq "right"; | |
6698 | $state = "pattern"; | |
6699 | my $p = quotemeta($1); | |
6700 | $p =~ s/\\,/|/g; | |
6701 | push(@patterns, "(?:$p)"); | |
6702 | } else { | |
6703 | if ($state eq "left") { | |
6704 | push(@left, $part); | |
6705 | } else { | |
6706 | push(@right, $part); | |
6707 | $state = "right"; | |
6708 | } | |
6709 | } | |
570d35c2 | 6710 | } |
07576208 | 6711 | my $depth = @patterns; |
570d35c2 | 6712 | if ($depth == 0) { |
07576208 | 6713 | die "One '*' is needed in glob: '$glob'\n"; |
4e9f6cc7 | 6714 | } |
07576208 JS |
6715 | my $left = join('/', @left); |
6716 | my $right = join('/', @right); | |
6717 | $re = join('/', @patterns); | |
6718 | $re = join('\/', | |
6719 | grep(length, quotemeta($left), "($re)", quotemeta($right))); | |
74a81227 EW |
6720 | my $left_re = qr/^\/\Q$left\E(\/|$)/; |
6721 | bless { left => $left, right => $right, left_regex => $left_re, | |
570d35c2 | 6722 | regex => qr/$re/, glob => $glob, depth => $depth }, $class; |
4bb9ed04 EW |
6723 | } |
6724 | ||
6725 | sub full_path { | |
6726 | my ($self, $path) = @_; | |
6727 | return (length $self->{left} ? "$self->{left}/" : '') . | |
6728 | $path . (length $self->{right} ? "/$self->{right}" : ''); | |
6729 | } | |
6730 | ||
3397f9df EW |
6731 | __END__ |
6732 | ||
6733 | Data structures: | |
6734 | ||
4bb9ed04 EW |
6735 | |
6736 | $remotes = { # returned by read_all_remotes() | |
6737 | 'svn' => { | |
6738 | # svn-remote.svn.url=https://svn.musicpd.org | |
6739 | url => 'https://svn.musicpd.org', | |
6740 | # svn-remote.svn.fetch=mpd/trunk:trunk | |
6741 | fetch => { | |
6742 | 'mpd/trunk' => 'trunk', | |
6743 | }, | |
6744 | # svn-remote.svn.tags=mpd/tags/*:tags/* | |
6745 | tags => { | |
6746 | path => { | |
6747 | left => 'mpd/tags', | |
6748 | right => '', | |
6749 | regex => qr!mpd/tags/([^/]+)$!, | |
6750 | glob => 'tags/*', | |
6751 | }, | |
6752 | ref => { | |
6753 | left => 'tags', | |
6754 | right => '', | |
6755 | regex => qr!tags/([^/]+)$!, | |
6756 | glob => 'tags/*', | |
6757 | }, | |
6758 | } | |
6759 | } | |
6760 | }; | |
6761 | ||
44320b9e | 6762 | $log_entry hashref as returned by libsvn_log_entry() |
3397f9df | 6763 | { |
44320b9e | 6764 | log => 'whitespace-formatted log entry |
3397f9df EW |
6765 | ', # trailing newline is preserved |
6766 | revision => '8', # integer | |
6767 | date => '2004-02-24T17:01:44.108345Z', # commit date | |
6768 | author => 'committer name' | |
6769 | }; | |
6770 | ||
6e8548cc EW |
6771 | |
6772 | # this is generated by generate_diff(); | |
3397f9df EW |
6773 | @mods = array of diff-index line hashes, each element represents one line |
6774 | of diff-index output | |
6775 | ||
6776 | diff-index line ($m hash) | |
6777 | { | |
6778 | mode_a => first column of diff-index output, no leading ':', | |
6779 | mode_b => second column of diff-index output, | |
6780 | sha1_b => sha1sum of the final blob, | |
ac8e0b91 | 6781 | chg => change type [MCRADT], |
3397f9df EW |
6782 | file_a => original file name of a file (iff chg is 'C' or 'R') |
6783 | file_b => new/current file name of a file (any chg) | |
6784 | } | |
6785 | ; | |
a5e0cedc | 6786 | |
a00439ac EW |
6787 | # retval of read_url_paths{,_all}(); |
6788 | $l_map = { | |
6789 | # repository root url | |
6790 | 'https://svn.musicpd.org' => { | |
6791 | # repository path # GIT_SVN_ID | |
6792 | 'mpd/trunk' => 'trunk', | |
6793 | 'mpd/tags/0.11.5' => 'tags/0.11.5', | |
6794 | }, | |
6795 | } | |
6796 | ||
a5e0cedc EW |
6797 | Notes: |
6798 | I don't trust the each() function on unless I created %hash myself | |
6799 | because the internal iterator may not have started at base. |