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