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