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