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