Commit | Line | Data |
---|---|---|
3397f9df | 1 | #!/usr/bin/env perl |
551ce28f EW |
2 | # Copyright (C) 2006, Eric Wong <normalperson@yhbt.net> |
3 | # License: GPL v2 or later | |
d48b2841 | 4 | use 5.008; |
3397f9df EW |
5 | use warnings; |
6 | use strict; | |
7 | use vars qw/ $AUTHOR $VERSION | |
ffe256f9 | 8 | $sha1 $sha1_short $_revision $_repository |
36db1edd | 9 | $_q $_authors $_authors_prog %users/; |
3397f9df | 10 | $AUTHOR = 'Eric Wong <normalperson@yhbt.net>'; |
60d02ccc | 11 | $VERSION = '@@GIT_VERSION@@'; |
13ccd6d4 | 12 | |
15153451 BS |
13 | # From which subdir have we been invoked? |
14 | my $cmd_dir_prefix = eval { | |
15 | command_oneline([qw/rev-parse --show-prefix/], STDERR => 0) | |
16 | } || ''; | |
17 | ||
5253dc33 | 18 | my $git_dir_user_set = 1 if defined $ENV{GIT_DIR}; |
706587fc | 19 | $ENV{GIT_DIR} ||= '.git'; |
9fa00b65 | 20 | $Git::SVN::default_repo_id = 'svn'; |
8b8fc068 | 21 | $Git::SVN::default_ref_id = $ENV{GIT_SVN_ID} || 'git-svn'; |
6af1db44 | 22 | $Git::SVN::Ra::_log_window_size = 100; |
6b48829d | 23 | $Git::SVN::_minimize_url = 'unset'; |
13ccd6d4 | 24 | |
f3a87d92 K |
25 | if (! exists $ENV{SVN_SSH}) { |
26 | if (exists $ENV{GIT_SSH}) { | |
27 | $ENV{SVN_SSH} = $ENV{GIT_SSH}; | |
28 | if ($^O eq 'msys') { | |
29 | $ENV{SVN_SSH} =~ s/\\/\\\\/g; | |
a004fb92 | 30 | $ENV{SVN_SSH} =~ s/(.*)/"$1"/; |
f3a87d92 K |
31 | } |
32 | } | |
33 | } | |
34 | ||
f8c9d1d2 | 35 | $Git::SVN::Log::TZ = $ENV{TZ}; |
3397f9df | 36 | $ENV{TZ} = 'UTC'; |
a00439ac | 37 | $| = 1; # unbuffer STDOUT |
3397f9df | 38 | |
207f1a75 | 39 | sub fatal (@) { print STDERR "@_\n"; exit 1 } |
d32fad2b | 40 | sub _req_svn { |
41 | require SVN::Core; # use()-ing this causes segfaults for me... *shrug* | |
42 | require SVN::Ra; | |
43 | require SVN::Delta; | |
44 | if ($SVN::Core::VERSION lt '1.1.0') { | |
45 | fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)"; | |
46 | } | |
b9c85187 | 47 | } |
2da9ee08 | 48 | my $can_compress = eval { require Compress::Zlib; 1}; |
d81bf827 | 49 | push @Git::SVN::Ra::ISA, 'SVN::Ra'; |
b9c85187 EW |
50 | push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor'; |
51 | push @SVN::Git::Fetcher::ISA, 'SVN::Delta::Editor'; | |
3397f9df | 52 | use Carp qw/croak/; |
8d7c4fad | 53 | use Digest::MD5; |
3397f9df EW |
54 | use IO::File qw//; |
55 | use File::Basename qw/dirname basename/; | |
56 | use File::Path qw/mkpath/; | |
36db1edd | 57 | use File::Spec; |
2da9ee08 | 58 | use File::Find; |
512b620b | 59 | use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/; |
968bdf1f | 60 | use IPC::Open3; |
336f1714 | 61 | use Git; |
f5549afd | 62 | use Memoize; # core since 5.8.0, Jul 2002 |
a5e0cedc | 63 | |
336f1714 | 64 | BEGIN { |
c5f71ad0 SV |
65 | # import functions from Git into our packages, en masse |
66 | no strict 'refs'; | |
336f1714 | 67 | foreach (qw/command command_oneline command_noisy command_output_pipe |
6ea42032 BB |
68 | command_input_pipe command_close_pipe |
69 | command_bidi_pipe command_close_bidi_pipe/) { | |
c5f71ad0 | 70 | for my $package ( qw(SVN::Git::Editor SVN::Git::Fetcher |
8d7c4fad | 71 | Git::SVN::Migration Git::SVN::Log Git::SVN), |
c5f71ad0 SV |
72 | __PACKAGE__) { |
73 | *{"${package}::$_"} = \&{"Git::$_"}; | |
74 | } | |
336f1714 | 75 | } |
f5549afd JK |
76 | Memoize::memoize 'Git::config'; |
77 | Memoize::memoize 'Git::config_bool'; | |
336f1714 EW |
78 | } |
79 | ||
b9c85187 | 80 | my ($SVN); |
83e9940a | 81 | |
f8c9d1d2 EW |
82 | $sha1 = qr/[a-f\d]{40}/; |
83 | $sha1_short = qr/[a-f\d]{4,40}/; | |
44320b9e | 84 | my ($_stdin, $_help, $_edit, |
62244069 | 85 | $_message, $_file, $_branch_dest, |
d05d72e0 | 86 | $_template, $_shared, |
c2abd83f | 87 | $_version, $_fetch_all, $_no_rebase, $_fetch_parent, |
dee41f3e | 88 | $_merge, $_strategy, $_dry_run, $_local, |
4be40381 | 89 | $_prefix, $_no_checkout, $_url, $_verbose, |
6abd9332 | 90 | $_git_format, $_commit_url, $_tag, $_merge_info); |
0bed5eaa | 91 | $Git::SVN::_follow_parent = 1; |
40a1530c | 92 | $SVN::Git::Fetcher::_placeholder_filename = ".gitignore"; |
49750f30 | 93 | $_q ||= 0; |
706587fc EW |
94 | my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username, |
95 | 'config-dir=s' => \$Git::SVN::Ra::config_dir, | |
edc662f9 VS |
96 | 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache, |
97 | 'ignore-paths=s' => \$SVN::Git::Fetcher::_ignore_regex ); | |
0bed5eaa | 98 | my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent, |
dc5869c0 | 99 | 'authors-file|A=s' => \$_authors, |
36db1edd | 100 | 'authors-prog=s' => \$_authors_prog, |
ecc712dd | 101 | 'repack:i' => \$Git::SVN::_repack, |
97ae0911 EW |
102 | 'noMetadata' => \$Git::SVN::_no_metadata, |
103 | 'useSvmProps' => \$Git::SVN::_use_svm_props, | |
62e349d2 | 104 | 'useSvnsyncProps' => \$Git::SVN::_use_svnsync_props, |
6af1db44 | 105 | 'log-window-size=i' => \$Git::SVN::Ra::_log_window_size, |
1e889ef3 | 106 | 'no-checkout' => \$_no_checkout, |
49750f30 | 107 | 'quiet|q+' => \$_q, |
ecc712dd EW |
108 | 'repack-flags|repack-args|repack-opts=s' => |
109 | \$Git::SVN::_repack_flags, | |
70ae04e4 | 110 | 'use-log-author' => \$Git::SVN::_use_log_author, |
6aa9ba14 | 111 | 'add-author-from' => \$Git::SVN::_add_author_from, |
e82f0d73 | 112 | 'localtime' => \$Git::SVN::_localtime, |
706587fc | 113 | %remote_opts ); |
36f5b1f0 | 114 | |
62244069 | 115 | my ($_trunk, @_tags, @_branches, $_stdlayout); |
0dfaf0a4 | 116 | my %icv; |
dadc6d2a | 117 | my %init_opts = ( 'template=s' => \$_template, 'shared:s' => \$_shared, |
62244069 MB |
118 | 'trunk|T=s' => \$_trunk, 'tags|t=s@' => \@_tags, |
119 | 'branches|b=s@' => \@_branches, 'prefix=s' => \$_prefix, | |
8f728fb9 | 120 | 'stdlayout|s' => \$_stdlayout, |
6b48829d | 121 | 'minimize-url|m!' => \$Git::SVN::_minimize_url, |
0dfaf0a4 EW |
122 | 'no-metadata' => sub { $icv{noMetadata} = 1 }, |
123 | 'use-svm-props' => sub { $icv{useSvmProps} = 1 }, | |
124 | 'use-svnsync-props' => sub { $icv{useSvnsyncProps} = 1 }, | |
125 | 'rewrite-root=s' => sub { $icv{rewriteRoot} = $_[1] }, | |
3e18ce1a | 126 | 'rewrite-uuid=s' => sub { $icv{rewriteUUID} = $_[1] }, |
dadc6d2a | 127 | %remote_opts ); |
27e9fb8d | 128 | my %cmt_opts = ( 'edit|e' => \$_edit, |
24e22aa8 EW |
129 | 'rmdir' => \$SVN::Git::Editor::_rmdir, |
130 | 'find-copies-harder' => \$SVN::Git::Editor::_find_copies_harder, | |
131 | 'l=i' => \$SVN::Git::Editor::_rename_limit, | |
132 | 'copy-similarity|C=i'=> \$SVN::Git::Editor::_cp_similarity | |
27e9fb8d | 133 | ); |
9d55b41a | 134 | |
3397f9df | 135 | my %cmd = ( |
2a3240be | 136 | fetch => [ \&cmd_fetch, "Download new revisions from SVN", |
e98671e5 | 137 | { 'revision|r=s' => \$_revision, |
905f8b7d | 138 | 'fetch-all|all' => \$_fetch_all, |
c2abd83f | 139 | 'parent|p' => \$_fetch_parent, |
e98671e5 | 140 | %fc_opts } ], |
0425ea90 EW |
141 | clone => [ \&cmd_clone, "Initialize and fetch revisions", |
142 | { 'revision|r=s' => \$_revision, | |
40a1530c RC |
143 | 'preserve-empty-dirs' => |
144 | \$SVN::Git::Fetcher::_preserve_empty_dirs, | |
145 | 'placeholder-filename=s' => | |
146 | \$SVN::Git::Fetcher::_placeholder_filename, | |
0425ea90 | 147 | %fc_opts, %init_opts } ], |
d2866f9e | 148 | init => [ \&cmd_init, "Initialize a repo for tracking" . |
f8ab6b73 | 149 | " (requires URL argument)", |
9d55b41a | 150 | \%init_opts ], |
dadc6d2a EW |
151 | 'multi-init' => [ \&cmd_multi_init, |
152 | "Deprecated alias for ". | |
153 | "'$0 init -T<trunk> -b<branches> -t<tags>'", | |
154 | \%init_opts ], | |
d7ad3bed EW |
155 | dcommit => [ \&cmd_dcommit, |
156 | 'Commit several diffs to merge with upstream', | |
3289e86e EW |
157 | { 'merge|m|M' => \$_merge, |
158 | 'strategy|s=s' => \$_strategy, | |
905f8b7d | 159 | 'verbose|v' => \$_verbose, |
3289e86e | 160 | 'dry-run|n' => \$_dry_run, |
905f8b7d | 161 | 'fetch-all|all' => \$_fetch_all, |
ba24e745 EW |
162 | 'commit-url=s' => \$_commit_url, |
163 | 'revision|r=i' => \$_revision, | |
171af110 | 164 | 'no-rebase' => \$_no_rebase, |
6abd9332 | 165 | 'mergeinfo=s' => \$_merge_info, |
4b155223 | 166 | %cmt_opts, %fc_opts } ], |
5de70efb FR |
167 | branch => [ \&cmd_branch, |
168 | 'Create a branch in the SVN repository', | |
169 | { 'message|m=s' => \$_message, | |
62244069 | 170 | 'destination|d=s' => \$_branch_dest, |
5de70efb | 171 | 'dry-run|n' => \$_dry_run, |
6594f0b7 IM |
172 | 'tag|t' => \$_tag, |
173 | 'username=s' => \$Git::SVN::Prompt::_username, | |
174 | 'commit-url=s' => \$_commit_url } ], | |
5de70efb FR |
175 | tag => [ sub { $_tag = 1; cmd_branch(@_) }, |
176 | 'Create a tag in the SVN repository', | |
177 | { 'message|m=s' => \$_message, | |
62244069 | 178 | 'destination|d=s' => \$_branch_dest, |
6594f0b7 IM |
179 | 'dry-run|n' => \$_dry_run, |
180 | 'username=s' => \$Git::SVN::Prompt::_username, | |
181 | 'commit-url=s' => \$_commit_url } ], | |
1ce255dc EW |
182 | 'set-tree' => [ \&cmd_set_tree, |
183 | "Set an SVN repository to a git tree-ish", | |
e84dc6df | 184 | { 'stdin' => \$_stdin, %cmt_opts, %fc_opts, } ], |
d05ddec5 BS |
185 | 'create-ignore' => [ \&cmd_create_ignore, |
186 | 'Create a .gitignore per svn:ignore', | |
187 | { 'revision|r=i' => \$_revision | |
188 | } ], | |
6111b934 EW |
189 | 'mkdirs' => [ \&cmd_mkdirs , |
190 | "recreate empty directories after a checkout", | |
191 | { 'revision|r=i' => \$_revision } ], | |
15153451 BS |
192 | 'propget' => [ \&cmd_propget, |
193 | 'Print the value of a property on a file or directory', | |
194 | { 'revision|r=i' => \$_revision } ], | |
51e057cf BS |
195 | 'proplist' => [ \&cmd_proplist, |
196 | 'List all properties of a file or directory', | |
197 | { 'revision|r=i' => \$_revision } ], | |
5969cbe1 | 198 | 'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings", |
4dbfe2e9 | 199 | { 'revision|r=i' => \$_revision |
05b4df31 | 200 | } ], |
2d879792 VK |
201 | 'show-externals' => [ \&cmd_show_externals, "Show svn:externals listings", |
202 | { 'revision|r=i' => \$_revision | |
203 | } ], | |
1c8443b0 | 204 | 'multi-fetch' => [ \&cmd_multi_fetch, |
e98671e5 EW |
205 | "Deprecated alias for $0 fetch --all", |
206 | { 'revision|r=s' => \$_revision, %fc_opts } ], | |
706587fc EW |
207 | 'migrate' => [ sub { }, |
208 | # no-op, we automatically run this anyways, | |
706587fc EW |
209 | 'Migrate configuration/metadata/layout from |
210 | previous versions of git-svn', | |
a836a0e1 EW |
211 | { 'minimize' => \$Git::SVN::Migration::_minimize, |
212 | %remote_opts } ], | |
f8c9d1d2 EW |
213 | 'log' => [ \&Git::SVN::Log::cmd_show_log, 'Show commit logs', |
214 | { 'limit=i' => \$Git::SVN::Log::limit, | |
79bb8d88 | 215 | 'revision|r=s' => \$_revision, |
f8c9d1d2 EW |
216 | 'verbose|v' => \$Git::SVN::Log::verbose, |
217 | 'incremental' => \$Git::SVN::Log::incremental, | |
218 | 'oneline' => \$Git::SVN::Log::oneline, | |
219 | 'show-commit' => \$Git::SVN::Log::show_commit, | |
220 | 'non-recursive' => \$Git::SVN::Log::non_recursive, | |
79bb8d88 | 221 | 'authors-file|A=s' => \$_authors, |
f8c9d1d2 | 222 | 'color' => \$Git::SVN::Log::color, |
4dbfe2e9 | 223 | 'pager=s' => \$Git::SVN::Log::pager |
79bb8d88 | 224 | } ], |
222566e4 EW |
225 | 'find-rev' => [ \&cmd_find_rev, |
226 | "Translate between SVN revision numbers and tree-ish", | |
4dbfe2e9 | 227 | {} ], |
905f8b7d EW |
228 | 'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory", |
229 | { 'merge|m|M' => \$_merge, | |
230 | 'verbose|v' => \$_verbose, | |
231 | 'strategy|s=s' => \$_strategy, | |
dee41f3e | 232 | 'local|l' => \$_local, |
905f8b7d | 233 | 'fetch-all|all' => \$_fetch_all, |
7d45e146 | 234 | 'dry-run|n' => \$_dry_run, |
905f8b7d | 235 | %fc_opts } ], |
44320b9e EW |
236 | 'commit-diff' => [ \&cmd_commit_diff, |
237 | 'Commit a diff between two trees', | |
27e9fb8d EW |
238 | { 'message|m=s' => \$_message, |
239 | 'file|F=s' => \$_file, | |
45bf473a | 240 | 'revision|r=s' => \$_revision, |
27e9fb8d | 241 | %cmt_opts } ], |
e6fefa92 DK |
242 | 'info' => [ \&cmd_info, |
243 | "Show info about the latest SVN revision | |
244 | on the current branch", | |
8b014d71 | 245 | { 'url' => \$_url, } ], |
6fb5375e TS |
246 | 'blame' => [ \&Git::SVN::Log::cmd_blame, |
247 | "Show what revision and author last modified each line of a file", | |
4be40381 | 248 | { 'git-format' => \$_git_format } ], |
195643f2 BJ |
249 | 'reset' => [ \&cmd_reset, |
250 | "Undo fetches back to the specified SVN revision", | |
251 | { 'revision|r=s' => \$_revision, | |
252 | 'parent|p' => \$_fetch_parent } ], | |
2da9ee08 RZ |
253 | 'gc' => [ \&cmd_gc, |
254 | "Compress unhandled.log files in .git/svn and remove " . | |
255 | "index files in .git/svn", | |
256 | {} ], | |
3397f9df | 257 | ); |
9d55b41a | 258 | |
3397f9df EW |
259 | my $cmd; |
260 | for (my $i = 0; $i < @ARGV; $i++) { | |
261 | if (defined $cmd{$ARGV[$i]}) { | |
262 | $cmd = $ARGV[$i]; | |
263 | splice @ARGV, $i, 1; | |
264 | last; | |
9a8c92ac BJ |
265 | } elsif ($ARGV[$i] eq 'help') { |
266 | $cmd = $ARGV[$i+1]; | |
267 | usage(0); | |
3397f9df EW |
268 | } |
269 | }; | |
270 | ||
540424b2 EW |
271 | # make sure we're always running at the top-level working directory |
272 | unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) { | |
5253dc33 EW |
273 | unless (-d $ENV{GIT_DIR}) { |
274 | if ($git_dir_user_set) { | |
275 | die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ", | |
276 | "but it is not a directory\n"; | |
277 | } | |
278 | my $git_dir = delete $ENV{GIT_DIR}; | |
fe4003f6 DM |
279 | my $cdup = undef; |
280 | git_cmd_try { | |
281 | $cdup = command_oneline(qw/rev-parse --show-cdup/); | |
282 | $git_dir = '.' unless ($cdup); | |
283 | chomp $cdup if ($cdup); | |
284 | $cdup = "." unless ($cdup && length $cdup); | |
285 | } "Already at toplevel, but $git_dir not found\n"; | |
5253dc33 EW |
286 | chdir $cdup or die "Unable to chdir up to '$cdup'\n"; |
287 | unless (-d $git_dir) { | |
288 | die "$git_dir still not found after going to ", | |
289 | "'$cdup'\n"; | |
290 | } | |
291 | $ENV{GIT_DIR} = $git_dir; | |
292 | } | |
ffe256f9 | 293 | $_repository = Git->repository(Repository => $ENV{GIT_DIR}); |
5253dc33 | 294 | } |
f4dd334b GH |
295 | |
296 | my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd); | |
297 | ||
1a30582b | 298 | read_git_config(\%opts); |
222566e4 EW |
299 | if ($cmd && ($cmd eq 'log' || $cmd eq 'blame')) { |
300 | Getopt::Long::Configure('pass_through'); | |
301 | } | |
87182b17 | 302 | my $rv = GetOptions(%opts, 'h|H' => \$_help, 'version|V' => \$_version, |
f4dd334b GH |
303 | 'minimize-connections' => \$Git::SVN::Migration::_minimize, |
304 | 'id|i=s' => \$Git::SVN::default_ref_id, | |
305 | 'svn-remote|remote|R=s' => sub { | |
306 | $Git::SVN::no_reuse_existing = 1; | |
307 | $Git::SVN::default_repo_id = $_[1] }); | |
308 | exit 1 if (!$rv && $cmd && $cmd ne 'log'); | |
309 | ||
310 | usage(0) if $_help; | |
311 | version() if $_version; | |
312 | usage(1) unless defined $cmd; | |
313 | load_authors() if $_authors; | |
36db1edd ML |
314 | if (defined $_authors_prog) { |
315 | $_authors_prog = "'" . File::Spec->rel2abs($_authors_prog) . "'"; | |
316 | } | |
f4dd334b | 317 | |
0425ea90 | 318 | unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) { |
706587fc EW |
319 | Git::SVN::Migration::migration_check(); |
320 | } | |
ecc712dd | 321 | Git::SVN::init_vars(); |
b805b44a EW |
322 | eval { |
323 | Git::SVN::verify_remotes_sanity(); | |
324 | $cmd{$cmd}->[0]->(@ARGV); | |
325 | }; | |
326 | fatal $@ if $@; | |
1e889ef3 | 327 | post_fetch_checkout(); |
3397f9df EW |
328 | exit 0; |
329 | ||
330 | ####################### primary functions ###################### | |
331 | sub usage { | |
332 | my $exit = shift || 0; | |
333 | my $fd = $exit ? \*STDERR : \*STDOUT; | |
334 | print $fd <<""; | |
335 | git-svn - bidirectional operations between a single Subversion tree and git | |
1b1dd23f | 336 | Usage: git svn <command> [options] [arguments]\n |
448c81b4 EW |
337 | |
338 | print $fd "Available commands:\n" unless $cmd; | |
3397f9df EW |
339 | |
340 | foreach (sort keys %cmd) { | |
448c81b4 | 341 | next if $cmd && $cmd ne $_; |
a836a0e1 | 342 | next if /^multi-/; # don't show deprecated commands |
b203b769 | 343 | print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n"; |
aa807bc2 | 344 | foreach (sort keys %{$cmd{$_}->[2]}) { |
512b620b EW |
345 | # mixed-case options are for .git/config only |
346 | next if /[A-Z]/ && /^[a-z]+$/i; | |
448c81b4 | 347 | # prints out arguments as they should be passed: |
b8c92cad | 348 | my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : ''; |
b203b769 | 349 | print $fd ' ' x 21, join(', ', map { length $_ > 1 ? |
448c81b4 EW |
350 | "--$_" : "-$_" } |
351 | split /\|/,$_)," $x\n"; | |
352 | } | |
3397f9df EW |
353 | } |
354 | print $fd <<""; | |
448c81b4 EW |
355 | \nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an |
356 | arbitrary identifier if you're tracking multiple SVN branches/repositories in | |
357 | one git repository and want to keep them separate. See git-svn(1) for more | |
358 | information. | |
3397f9df EW |
359 | |
360 | exit $exit; | |
361 | } | |
362 | ||
551ce28f | 363 | sub version { |
b0779246 | 364 | ::_req_svn(); |
7d60ab2c | 365 | print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n"; |
551ce28f EW |
366 | exit 0; |
367 | } | |
368 | ||
8164b652 EW |
369 | sub do_git_init_db { |
370 | unless (-d $ENV{GIT_DIR}) { | |
371 | my @init_db = ('init'); | |
372 | push @init_db, "--template=$_template" if defined $_template; | |
dadc6d2a EW |
373 | if (defined $_shared) { |
374 | if ($_shared =~ /[a-z]/) { | |
375 | push @init_db, "--shared=$_shared"; | |
376 | } else { | |
377 | push @init_db, "--shared"; | |
378 | } | |
379 | } | |
8164b652 | 380 | command_noisy(@init_db); |
ffe256f9 | 381 | $_repository = Git->repository(Repository => ".git"); |
8164b652 | 382 | } |
0dfaf0a4 EW |
383 | my $set; |
384 | my $pfx = "svn-remote.$Git::SVN::default_repo_id"; | |
385 | foreach my $i (keys %icv) { | |
386 | die "'$set' and '$i' cannot both be set\n" if $set; | |
387 | next unless defined $icv{$i}; | |
388 | command_noisy('config', "$pfx.$i", $icv{$i}); | |
389 | $set = $i; | |
390 | } | |
88ec2054 BJ |
391 | my $ignore_regex = \$SVN::Git::Fetcher::_ignore_regex; |
392 | command_noisy('config', "$pfx.ignore-paths", $$ignore_regex) | |
393 | if defined $$ignore_regex; | |
40a1530c RC |
394 | |
395 | if (defined $SVN::Git::Fetcher::_preserve_empty_dirs) { | |
396 | my $fname = \$SVN::Git::Fetcher::_placeholder_filename; | |
397 | command_noisy('config', "$pfx.preserve-empty-dirs", 'true'); | |
398 | command_noisy('config', "$pfx.placeholder-filename", $$fname); | |
399 | } | |
8164b652 EW |
400 | } |
401 | ||
dadc6d2a EW |
402 | sub init_subdir { |
403 | my $repo_path = shift or return; | |
404 | mkpath([$repo_path]) unless -d $repo_path; | |
405 | chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n"; | |
f30603fc | 406 | $ENV{GIT_DIR} = '.git'; |
ffe256f9 | 407 | $_repository = Git->repository(Repository => $ENV{GIT_DIR}); |
dadc6d2a EW |
408 | } |
409 | ||
0425ea90 EW |
410 | sub cmd_clone { |
411 | my ($url, $path) = @_; | |
412 | if (!defined $path && | |
62244069 | 413 | (defined $_trunk || @_branches || @_tags || |
8f728fb9 | 414 | defined $_stdlayout) && |
0425ea90 EW |
415 | $url !~ m#^[a-z\+]+://#) { |
416 | $path = $url; | |
417 | } | |
0425ea90 | 418 | $path = basename($url) if !defined $path || !length $path; |
2bc35dcb | 419 | my $authors_absolute = $_authors ? File::Spec->rel2abs($_authors) : ""; |
f30603fc | 420 | cmd_init($url, $path); |
2bc35dcb AV |
421 | command_oneline('config', 'svn.authorsfile', $authors_absolute) |
422 | if $_authors; | |
f5841509 | 423 | Git::SVN::fetch_all($Git::SVN::default_repo_id); |
0425ea90 EW |
424 | } |
425 | ||
d2866f9e | 426 | sub cmd_init { |
8f728fb9 | 427 | if (defined $_stdlayout) { |
428 | $_trunk = 'trunk' if (!defined $_trunk); | |
62244069 MB |
429 | @_tags = 'tags' if (! @_tags); |
430 | @_branches = 'branches' if (! @_branches); | |
8f728fb9 | 431 | } |
62244069 | 432 | if (defined $_trunk || @_branches || @_tags) { |
dadc6d2a | 433 | return cmd_multi_init(@_); |
03e0ea87 | 434 | } |
dadc6d2a EW |
435 | my $url = shift or die "SVN repository location required ", |
436 | "as a command-line argument\n"; | |
50ff2366 | 437 | $url = canonicalize_url($url); |
dadc6d2a | 438 | init_subdir(@_); |
8164b652 | 439 | do_git_init_db(); |
03e0ea87 | 440 | |
6b48829d EW |
441 | if ($Git::SVN::_minimize_url eq 'unset') { |
442 | $Git::SVN::_minimize_url = 0; | |
443 | } | |
444 | ||
706587fc | 445 | Git::SVN->init($url); |
3397f9df EW |
446 | } |
447 | ||
2a3240be | 448 | sub cmd_fetch { |
e98671e5 EW |
449 | if (grep /^\d+=./, @_) { |
450 | die "'<rev>=<commit>' fetch arguments are ", | |
451 | "no longer supported.\n"; | |
07a1c950 | 452 | } |
e98671e5 EW |
453 | my ($remote) = @_; |
454 | if (@_ > 1) { | |
c2abd83f | 455 | die "Usage: $0 fetch [--all] [--parent] [svn-remote]\n"; |
e98671e5 | 456 | } |
4d0157d6 | 457 | $Git::SVN::no_reuse_existing = undef; |
c2abd83f JM |
458 | if ($_fetch_parent) { |
459 | my ($url, $rev, $uuid, $gs) = working_head_info('HEAD'); | |
460 | unless ($gs) { | |
461 | die "Unable to determine upstream SVN information from ", | |
462 | "working tree history\n"; | |
463 | } | |
464 | # just fetch, don't checkout. | |
465 | $_no_checkout = 'true'; | |
466 | $_fetch_all ? $gs->fetch_all : $gs->fetch; | |
467 | } elsif ($_fetch_all) { | |
e98671e5 EW |
468 | cmd_multi_fetch(); |
469 | } else { | |
c2abd83f | 470 | $remote ||= $Git::SVN::default_repo_id; |
e98671e5 | 471 | Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes()); |
1c8443b0 | 472 | } |
2a3240be EW |
473 | } |
474 | ||
1ce255dc | 475 | sub cmd_set_tree { |
3397f9df EW |
476 | my (@commits) = @_; |
477 | if ($_stdin || !@commits) { | |
478 | print "Reading from stdin...\n"; | |
479 | @commits = (); | |
480 | while (<STDIN>) { | |
1ca72aef | 481 | if (/\b($sha1_short)\b/o) { |
3397f9df EW |
482 | unshift @commits, $1; |
483 | } | |
484 | } | |
485 | } | |
486 | my @revs; | |
8de010ad | 487 | foreach my $c (@commits) { |
aef4e921 | 488 | my @tmp = command('rev-parse',$c); |
8de010ad EW |
489 | if (scalar @tmp == 1) { |
490 | push @revs, $tmp[0]; | |
491 | } elsif (scalar @tmp > 1) { | |
aef4e921 | 492 | push @revs, reverse(command('rev-list',@tmp)); |
8de010ad | 493 | } else { |
207f1a75 | 494 | fatal "Failed to rev-parse $c"; |
8de010ad | 495 | } |
3397f9df | 496 | } |
1ce255dc EW |
497 | my $gs = Git::SVN->new; |
498 | my ($r_last, $cmt_last) = $gs->last_rev_commit; | |
499 | $gs->fetch; | |
97f6987a | 500 | if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) { |
1ce255dc EW |
501 | fatal "There are new revisions that were fetched ", |
502 | "and need to be merged (or acknowledged) ", | |
503 | "before committing.\nlast rev: $r_last\n", | |
207f1a75 | 504 | " current: $gs->{last_rev}"; |
a5e0cedc | 505 | } |
1ce255dc EW |
506 | $gs->set_tree($_) foreach @revs; |
507 | print "Done committing ",scalar @revs," revisions to SVN\n"; | |
3157dd9e | 508 | unlink $gs->{index}; |
a5e0cedc | 509 | } |
8f22562c | 510 | |
1e5814f3 BJ |
511 | sub split_merge_info_range { |
512 | my ($range) = @_; | |
513 | if ($range =~ /(\d+)-(\d+)/) { | |
514 | return (int($1), int($2)); | |
515 | } else { | |
516 | return (int($range), int($range)); | |
517 | } | |
518 | } | |
519 | ||
520 | sub combine_ranges { | |
521 | my ($in) = @_; | |
522 | ||
523 | my @fnums = (); | |
524 | my @arr = split(/,/, $in); | |
525 | for my $element (@arr) { | |
526 | my ($start, $end) = split_merge_info_range($element); | |
527 | push @fnums, $start; | |
528 | } | |
529 | ||
530 | my @sorted = @arr [ sort { | |
531 | $fnums[$a] <=> $fnums[$b] | |
532 | } 0..$#arr ]; | |
533 | ||
534 | my @return = (); | |
535 | my $last = -1; | |
536 | my $first = -1; | |
537 | for my $element (@sorted) { | |
538 | my ($start, $end) = split_merge_info_range($element); | |
539 | ||
540 | if ($last == -1) { | |
541 | $first = $start; | |
542 | $last = $end; | |
543 | next; | |
544 | } | |
545 | if ($start <= $last+1) { | |
546 | if ($end > $last) { | |
547 | $last = $end; | |
548 | } | |
549 | next; | |
550 | } | |
551 | if ($first == $last) { | |
552 | push @return, "$first"; | |
553 | } else { | |
554 | push @return, "$first-$last"; | |
555 | } | |
556 | $first = $start; | |
557 | $last = $end; | |
558 | } | |
559 | ||
560 | if ($first != -1) { | |
561 | if ($first == $last) { | |
562 | push @return, "$first"; | |
563 | } else { | |
564 | push @return, "$first-$last"; | |
565 | } | |
566 | } | |
567 | ||
568 | return join(',', @return); | |
569 | } | |
570 | ||
571 | sub merge_revs_into_hash { | |
572 | my ($hash, $minfo) = @_; | |
573 | my @lines = split(' ', $minfo); | |
574 | ||
575 | for my $line (@lines) { | |
576 | my ($branchpath, $revs) = split(/:/, $line); | |
577 | ||
578 | if (exists($hash->{$branchpath})) { | |
579 | # Merge the two revision sets | |
580 | my $combined = "$hash->{$branchpath},$revs"; | |
581 | $hash->{$branchpath} = combine_ranges($combined); | |
582 | } else { | |
583 | # Just do range combining for consolidation | |
584 | $hash->{$branchpath} = combine_ranges($revs); | |
585 | } | |
586 | } | |
587 | } | |
588 | ||
589 | sub merge_merge_info { | |
590 | my ($mergeinfo_one, $mergeinfo_two) = @_; | |
591 | my %result_hash = (); | |
592 | ||
593 | merge_revs_into_hash(\%result_hash, $mergeinfo_one); | |
594 | merge_revs_into_hash(\%result_hash, $mergeinfo_two); | |
595 | ||
596 | my $result = ''; | |
597 | # Sort below is for consistency's sake | |
598 | for my $branchname (sort keys(%result_hash)) { | |
599 | my $revlist = $result_hash{$branchname}; | |
600 | $result .= "$branchname:$revlist\n" | |
601 | } | |
602 | return $result; | |
603 | } | |
604 | ||
605 | sub populate_merge_info { | |
606 | my ($d, $gs, $uuid, $linear_refs, $rewritten_parent) = @_; | |
607 | ||
608 | my %parentshash; | |
609 | read_commit_parents(\%parentshash, $d); | |
610 | my @parents = @{$parentshash{$d}}; | |
611 | if ($#parents > 0) { | |
612 | # Merge commit | |
613 | my $all_parents_ok = 1; | |
614 | my $aggregate_mergeinfo = ''; | |
615 | my $rooturl = $gs->repos_root; | |
616 | ||
617 | if (defined($rewritten_parent)) { | |
618 | # Replace first parent with newly-rewritten version | |
619 | shift @parents; | |
620 | unshift @parents, $rewritten_parent; | |
621 | } | |
622 | ||
623 | foreach my $parent (@parents) { | |
624 | my ($branchurl, $svnrev, $paruuid) = | |
625 | cmt_metadata($parent); | |
626 | ||
627 | unless (defined($svnrev)) { | |
628 | # Should have been caught be preflight check | |
629 | fatal "merge commit $d has ancestor $parent, but that change " | |
630 | ."does not have git-svn metadata!"; | |
631 | } | |
632 | unless ($branchurl =~ /^$rooturl(.*)/) { | |
633 | fatal "commit $parent git-svn metadata changed mid-run!"; | |
634 | } | |
635 | my $branchpath = $1; | |
636 | ||
637 | my $ra = Git::SVN::Ra->new($branchurl); | |
638 | my (undef, undef, $props) = | |
639 | $ra->get_dir(canonicalize_path("."), $svnrev); | |
640 | my $par_mergeinfo = $props->{'svn:mergeinfo'}; | |
641 | unless (defined $par_mergeinfo) { | |
642 | $par_mergeinfo = ''; | |
643 | } | |
644 | # Merge previous mergeinfo values | |
645 | $aggregate_mergeinfo = | |
646 | merge_merge_info($aggregate_mergeinfo, | |
647 | $par_mergeinfo, 0); | |
648 | ||
649 | next if $parent eq $parents[0]; # Skip first parent | |
650 | # Add new changes being placed in tree by merge | |
651 | my @cmd = (qw/rev-list --reverse/, | |
652 | $parent, qw/--not/); | |
653 | foreach my $par (@parents) { | |
654 | unless ($par eq $parent) { | |
655 | push @cmd, $par; | |
656 | } | |
657 | } | |
658 | my @revsin = (); | |
659 | my ($revlist, $ctx) = command_output_pipe(@cmd); | |
660 | while (<$revlist>) { | |
661 | my $irev = $_; | |
662 | chomp $irev; | |
663 | my (undef, $csvnrev, undef) = | |
664 | cmt_metadata($irev); | |
665 | unless (defined $csvnrev) { | |
666 | # A child is missing SVN annotations... | |
667 | # this might be OK, or might not be. | |
668 | warn "W:child $irev is merged into revision " | |
669 | ."$d but does not have git-svn metadata. " | |
670 | ."This means git-svn cannot determine the " | |
671 | ."svn revision numbers to place into the " | |
672 | ."svn:mergeinfo property. You must ensure " | |
673 | ."a branch is entirely committed to " | |
674 | ."SVN before merging it in order for " | |
675 | ."svn:mergeinfo population to function " | |
676 | ."properly"; | |
677 | } | |
678 | push @revsin, $csvnrev; | |
679 | } | |
680 | command_close_pipe($revlist, $ctx); | |
681 | ||
682 | last unless $all_parents_ok; | |
683 | ||
684 | # We now have a list of all SVN revnos which are | |
685 | # merged by this particular parent. Integrate them. | |
686 | next if $#revsin == -1; | |
687 | my $newmergeinfo = "$branchpath:" . join(',', @revsin); | |
688 | $aggregate_mergeinfo = | |
689 | merge_merge_info($aggregate_mergeinfo, | |
690 | $newmergeinfo, 1); | |
691 | } | |
692 | if ($all_parents_ok and $aggregate_mergeinfo) { | |
693 | return $aggregate_mergeinfo; | |
694 | } | |
695 | } | |
696 | ||
697 | return undef; | |
698 | } | |
699 | ||
d7ad3bed EW |
700 | sub cmd_dcommit { |
701 | my $head = shift; | |
181264ad | 702 | command_noisy(qw/update-index --refresh/); |
c8cfa3e4 | 703 | git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) } |
826a9339 | 704 | 'Cannot dcommit with a dirty index. Commit your changes first, ' |
c8cfa3e4 | 705 | . "or stash them with `git stash'.\n"; |
d7ad3bed | 706 | $head ||= 'HEAD'; |
5eec27e3 TR |
707 | |
708 | my $old_head; | |
709 | if ($head ne 'HEAD') { | |
710 | $old_head = eval { | |
711 | command_oneline([qw/symbolic-ref -q HEAD/]) | |
712 | }; | |
713 | if ($old_head) { | |
714 | $old_head =~ s{^refs/heads/}{}; | |
715 | } else { | |
716 | $old_head = eval { command_oneline(qw/rev-parse HEAD/) }; | |
717 | } | |
718 | command(['checkout', $head], STDERR => 0); | |
719 | } | |
720 | ||
a8ae2623 | 721 | my @refs; |
5eec27e3 | 722 | my ($url, $rev, $uuid, $gs) = working_head_info('HEAD', \@refs); |
2cb61105 TR |
723 | unless ($gs) { |
724 | die "Unable to determine upstream SVN information from ", | |
725 | "$head history.\nPerhaps the repository is empty."; | |
726 | } | |
0df84059 PO |
727 | |
728 | if (defined $_commit_url) { | |
729 | $url = $_commit_url; | |
730 | } else { | |
731 | $url = eval { command_oneline('config', '--get', | |
732 | "svn-remote.$gs->{repo_id}.commiturl") }; | |
733 | if (!$url) { | |
12a296bc | 734 | $url = $gs->full_pushurl |
0df84059 PO |
735 | } |
736 | } | |
737 | ||
ba24e745 | 738 | my $last_rev = $_revision if defined $_revision; |
59b0c24d MM |
739 | if ($url) { |
740 | print "Committing to $url ...\n"; | |
741 | } | |
733a65aa | 742 | my ($linear_refs, $parents) = linearize_history($gs, \@refs); |
751eb395 EW |
743 | if ($_no_rebase && scalar(@$linear_refs) > 1) { |
744 | warn "Attempting to commit more than one change while ", | |
745 | "--no-rebase is enabled.\n", | |
746 | "If these changes depend on each other, re-running ", | |
7dfa16b9 | 747 | "without --no-rebase may be required." |
751eb395 | 748 | } |
711521e2 | 749 | my $expect_url = $url; |
1e5814f3 BJ |
750 | |
751 | my $push_merge_info = eval { | |
752 | command_oneline(qw/config --get svn.pushmergeinfo/) | |
753 | }; | |
754 | if (not defined($push_merge_info) | |
755 | or $push_merge_info eq "false" | |
756 | or $push_merge_info eq "no" | |
757 | or $push_merge_info eq "never") { | |
758 | $push_merge_info = 0; | |
759 | } | |
760 | ||
761 | unless (defined($_merge_info) || ! $push_merge_info) { | |
762 | # Preflight check of changes to ensure no issues with mergeinfo | |
763 | # This includes check for uncommitted-to-SVN parents | |
764 | # (other than the first parent, which we will handle), | |
765 | # information from different SVN repos, and paths | |
766 | # which are not underneath this repository root. | |
767 | my $rooturl = $gs->repos_root; | |
768 | foreach my $d (@$linear_refs) { | |
769 | my %parentshash; | |
770 | read_commit_parents(\%parentshash, $d); | |
771 | my @realparents = @{$parentshash{$d}}; | |
772 | if ($#realparents > 0) { | |
773 | # Merge commit | |
774 | shift @realparents; # Remove/ignore first parent | |
775 | foreach my $parent (@realparents) { | |
776 | my ($branchurl, $svnrev, $paruuid) = cmt_metadata($parent); | |
777 | unless (defined $paruuid) { | |
778 | # A parent is missing SVN annotations... | |
779 | # abort the whole operation. | |
780 | fatal "$parent is merged into revision $d, " | |
781 | ."but does not have git-svn metadata. " | |
782 | ."Either dcommit the branch or use a " | |
783 | ."local cherry-pick, FF merge, or rebase " | |
784 | ."instead of an explicit merge commit."; | |
785 | } | |
786 | ||
787 | unless ($paruuid eq $uuid) { | |
788 | # Parent has SVN metadata from different repository | |
789 | fatal "merge parent $parent for change $d has " | |
790 | ."git-svn uuid $paruuid, while current change " | |
791 | ."has uuid $uuid!"; | |
792 | } | |
793 | ||
794 | unless ($branchurl =~ /^$rooturl(.*)/) { | |
795 | # This branch is very strange indeed. | |
796 | fatal "merge parent $parent for $d is on branch " | |
797 | ."$branchurl, which is not under the " | |
798 | ."git-svn root $rooturl!"; | |
799 | } | |
800 | } | |
801 | } | |
802 | } | |
803 | } | |
804 | ||
805 | my $rewritten_parent; | |
711521e2 | 806 | Git::SVN::remove_username($expect_url); |
98c4ab32 BJ |
807 | if (defined($_merge_info)) { |
808 | $_merge_info =~ tr{ }{\n}; | |
809 | } | |
c74d9acf EW |
810 | while (1) { |
811 | my $d = shift @$linear_refs or last; | |
45bf473a EW |
812 | unless (defined $last_rev) { |
813 | (undef, $last_rev, undef) = cmt_metadata("$d~1"); | |
814 | unless (defined $last_rev) { | |
d7ad3bed | 815 | fatal "Unable to extract revision information ", |
207f1a75 | 816 | "from commit $d~1"; |
45bf473a EW |
817 | } |
818 | } | |
b22d4497 EW |
819 | if ($_dry_run) { |
820 | print "diff-tree $d~1 $d\n"; | |
821 | } else { | |
751eb395 | 822 | my $cmt_rev; |
1e5814f3 BJ |
823 | |
824 | unless (defined($_merge_info) || ! $push_merge_info) { | |
825 | $_merge_info = populate_merge_info($d, $gs, | |
826 | $uuid, | |
827 | $linear_refs, | |
828 | $rewritten_parent); | |
829 | } | |
830 | ||
d7ad3bed | 831 | my %ed_opts = ( r => $last_rev, |
61395354 | 832 | log => get_commit_entry($d)->{log}, |
ba24e745 | 833 | ra => Git::SVN::Ra->new($url), |
3caf320b KA |
834 | config => SVN::Core::config_get_config( |
835 | $Git::SVN::Ra::config_dir | |
836 | ), | |
61395354 EW |
837 | tree_a => "$d~1", |
838 | tree_b => $d, | |
839 | editor_cb => sub { | |
840 | print "Committed r$_[0]\n"; | |
751eb395 EW |
841 | $cmt_rev = $_[0]; |
842 | }, | |
6abd9332 | 843 | mergeinfo => $_merge_info, |
a8ae2623 | 844 | svn_path => ''); |
61395354 | 845 | if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) { |
d7ad3bed | 846 | print "No changes\n$d~1 == $d\n"; |
733a65aa | 847 | } elsif ($parents->{$d} && @{$parents->{$d}}) { |
751eb395 | 848 | $gs->{inject_parents_dcommit}->{$cmt_rev} = |
733a65aa | 849 | $parents->{$d}; |
d7ad3bed | 850 | } |
751eb395 | 851 | $_fetch_all ? $gs->fetch_all : $gs->fetch; |
7dfa16b9 | 852 | $last_rev = $cmt_rev; |
751eb395 EW |
853 | next if $_no_rebase; |
854 | ||
855 | # we always want to rebase against the current HEAD, | |
856 | # not any head that was passed to us | |
c74d9acf | 857 | my @diff = command('diff-tree', $d, |
751eb395 EW |
858 | $gs->refname, '--'); |
859 | my @finish; | |
860 | if (@diff) { | |
861 | @finish = rebase_cmd(); | |
c74d9acf | 862 | print STDERR "W: $d and ", $gs->refname, |
751eb395 | 863 | " differ, using @finish:\n", |
c74d9acf | 864 | join("\n", @diff), "\n"; |
751eb395 EW |
865 | } else { |
866 | print "No changes between current HEAD and ", | |
867 | $gs->refname, | |
868 | "\nResetting to the latest ", | |
869 | $gs->refname, "\n"; | |
870 | @finish = qw/reset --mixed/; | |
871 | } | |
872 | command_noisy(@finish, $gs->refname); | |
1e5814f3 BJ |
873 | |
874 | $rewritten_parent = command_oneline(qw/rev-parse HEAD/); | |
875 | ||
c74d9acf EW |
876 | if (@diff) { |
877 | @refs = (); | |
878 | my ($url_, $rev_, $uuid_, $gs_) = | |
5eec27e3 | 879 | working_head_info('HEAD', \@refs); |
c74d9acf EW |
880 | my ($linear_refs_, $parents_) = |
881 | linearize_history($gs_, \@refs); | |
882 | if (scalar(@$linear_refs) != | |
883 | scalar(@$linear_refs_)) { | |
884 | fatal "# of revisions changed ", | |
885 | "\nbefore:\n", | |
886 | join("\n", @$linear_refs), | |
887 | "\n\nafter:\n", | |
888 | join("\n", @$linear_refs_), "\n", | |
889 | 'If you are attempting to commit ', | |
890 | "merges, try running:\n\t", | |
891 | 'git rebase --interactive', | |
892 | '--preserve-merges ', | |
893 | $gs->refname, | |
894 | "\nBefore dcommitting"; | |
895 | } | |
711521e2 | 896 | if ($url_ ne $expect_url) { |
c03c1f79 AG |
897 | if ($url_ eq $gs->metadata_url) { |
898 | ||
899 | "Accepting rewritten URL:", | |
900 | " $url_\n"; | |
901 | } else { | |
902 | fatal | |
903 | "URL mismatch after rebase:", | |
904 | " $url_ != $expect_url"; | |
905 | } | |
c74d9acf EW |
906 | } |
907 | if ($uuid_ ne $uuid) { | |
908 | fatal "uuid mismatch after rebase: ", | |
909 | "$uuid_ != $uuid"; | |
910 | } | |
911 | # remap parents | |
912 | my (%p, @l, $i); | |
913 | for ($i = 0; $i < scalar @$linear_refs; $i++) { | |
914 | my $new = $linear_refs_->[$i] or next; | |
915 | $p{$new} = | |
916 | $parents->{$linear_refs->[$i]}; | |
917 | push @l, $new; | |
918 | } | |
919 | $parents = \%p; | |
920 | $linear_refs = \@l; | |
921 | } | |
b22d4497 EW |
922 | } |
923 | } | |
5eec27e3 TR |
924 | |
925 | if ($old_head) { | |
926 | my $new_head = command_oneline(qw/rev-parse HEAD/); | |
927 | my $new_is_symbolic = eval { | |
928 | command_oneline(qw/symbolic-ref -q HEAD/); | |
929 | }; | |
930 | if ($new_is_symbolic) { | |
931 | print "dcommitted the branch ", $head, "\n"; | |
932 | } else { | |
933 | print "dcommitted on a detached HEAD because you gave ", | |
934 | "a revision argument.\n", | |
935 | "The rewritten commit is: ", $new_head, "\n"; | |
936 | } | |
937 | command(['checkout', $old_head], STDERR => 0); | |
938 | } | |
939 | ||
3157dd9e | 940 | unlink $gs->{index}; |
b22d4497 EW |
941 | } |
942 | ||
5de70efb FR |
943 | sub cmd_branch { |
944 | my ($branch_name, $head) = @_; | |
945 | ||
946 | unless (defined $branch_name && length $branch_name) { | |
947 | die(($_tag ? "tag" : "branch") . " name required\n"); | |
948 | } | |
949 | $head ||= 'HEAD'; | |
950 | ||
150d38c4 | 951 | my (undef, $rev, undef, $gs) = working_head_info($head); |
12a296bc | 952 | my $src = $gs->full_pushurl; |
5de70efb | 953 | |
a0fbc87c | 954 | my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}}; |
62244069 MB |
955 | my $allglobs = $remote->{ $_tag ? 'tags' : 'branches' }; |
956 | my $glob; | |
957 | if ($#{$allglobs} == 0) { | |
958 | $glob = $allglobs->[0]; | |
959 | } else { | |
960 | unless(defined $_branch_dest) { | |
961 | die "Multiple ", | |
962 | $_tag ? "tag" : "branch", | |
963 | " paths defined for Subversion repository.\n", | |
964 | "You must specify where you want to create the ", | |
965 | $_tag ? "tag" : "branch", | |
966 | " with the --destination argument.\n"; | |
967 | } | |
968 | foreach my $g (@{$allglobs}) { | |
f7050599 EW |
969 | # SVN::Git::Editor could probably be moved to Git.pm.. |
970 | my $re = SVN::Git::Editor::glob2pat($g->{path}->{left}); | |
971 | if ($_branch_dest =~ /$re/) { | |
62244069 MB |
972 | $glob = $g; |
973 | last; | |
974 | } | |
975 | } | |
976 | unless (defined $glob) { | |
eaa14ff8 EW |
977 | my $dest_re = qr/\b\Q$_branch_dest\E\b/; |
978 | foreach my $g (@{$allglobs}) { | |
979 | $g->{path}->{left} =~ /$dest_re/ or next; | |
980 | if (defined $glob) { | |
981 | die "Ambiguous destination: ", | |
982 | $_branch_dest, "\nmatches both '", | |
983 | $glob->{path}->{left}, "' and '", | |
984 | $g->{path}->{left}, "'\n"; | |
985 | } | |
986 | $glob = $g; | |
987 | } | |
988 | unless (defined $glob) { | |
989 | die "Unknown ", | |
990 | $_tag ? "tag" : "branch", | |
991 | " destination $_branch_dest\n"; | |
992 | } | |
62244069 MB |
993 | } |
994 | } | |
5de70efb | 995 | my ($lft, $rgt) = @{ $glob->{path} }{qw/left right/}; |
99bacd6c IM |
996 | my $url; |
997 | if (defined $_commit_url) { | |
998 | $url = $_commit_url; | |
999 | } else { | |
1000 | $url = eval { command_oneline('config', '--get', | |
1001 | "svn-remote.$gs->{repo_id}.commiturl") }; | |
1002 | if (!$url) { | |
12a296bc | 1003 | $url = $remote->{pushurl} || $remote->{url}; |
99bacd6c IM |
1004 | } |
1005 | } | |
1006 | my $dst = join '/', $url, $lft, $branch_name, ($rgt || ()); | |
5de70efb | 1007 | |
a83b91e7 IM |
1008 | if ($dst =~ /^https:/ && $src =~ /^http:/) { |
1009 | $src=~s/^http:/https:/; | |
1010 | } | |
1011 | ||
d32fad2b | 1012 | ::_req_svn(); |
1013 | ||
5de70efb FR |
1014 | my $ctx = SVN::Client->new( |
1015 | auth => Git::SVN::Ra::_auth_providers(), | |
1016 | log_msg => sub { | |
1017 | ${ $_[0] } = defined $_message | |
1018 | ? $_message | |
1019 | : 'Create ' . ($_tag ? 'tag ' : 'branch ' ) | |
1020 | . $branch_name; | |
1021 | }, | |
1022 | ); | |
1023 | ||
1024 | eval { | |
1025 | $ctx->ls($dst, 'HEAD', 0); | |
1026 | } and die "branch ${branch_name} already exists\n"; | |
1027 | ||
1028 | print "Copying ${src} at r${rev} to ${dst}...\n"; | |
1029 | $ctx->copy($src, $rev, $dst) | |
1030 | unless $_dry_run; | |
1031 | ||
1032 | $gs->fetch_all; | |
1033 | } | |
1034 | ||
26e60160 | 1035 | sub cmd_find_rev { |
ea14e6c5 MAL |
1036 | my $revision_or_hash = shift or die "SVN or git revision required ", |
1037 | "as a command-line argument\n"; | |
26e60160 AR |
1038 | my $result; |
1039 | if ($revision_or_hash =~ /^r\d+$/) { | |
b3cb7e45 AR |
1040 | my $head = shift; |
1041 | $head ||= 'HEAD'; | |
1042 | my @refs; | |
63c56022 | 1043 | my (undef, undef, $uuid, $gs) = working_head_info($head, \@refs); |
b3cb7e45 AR |
1044 | unless ($gs) { |
1045 | die "Unable to determine upstream SVN information from ", | |
1046 | "$head history\n"; | |
26e60160 | 1047 | } |
b3cb7e45 | 1048 | my $desired_revision = substr($revision_or_hash, 1); |
63c56022 | 1049 | $result = $gs->rev_map_get($desired_revision, $uuid); |
26e60160 AR |
1050 | } else { |
1051 | my (undef, $rev, undef) = cmt_metadata($revision_or_hash); | |
1052 | $result = $rev; | |
1053 | } | |
1054 | print "$result\n" if $result; | |
1055 | } | |
1056 | ||
55f9d7a7 MH |
1057 | sub auto_create_empty_directories { |
1058 | my ($gs) = @_; | |
1059 | my $var = eval { command_oneline('config', '--get', '--bool', | |
1060 | "svn-remote.$gs->{repo_id}.automkdirs") }; | |
1061 | # By default, create empty directories by consulting the unhandled log, | |
1062 | # but allow setting it to 'false' to skip it. | |
1063 | return !($var && $var eq 'false'); | |
1064 | } | |
1065 | ||
905f8b7d EW |
1066 | sub cmd_rebase { |
1067 | command_noisy(qw/update-index --refresh/); | |
13c823fb EW |
1068 | my ($url, $rev, $uuid, $gs) = working_head_info('HEAD'); |
1069 | unless ($gs) { | |
905f8b7d EW |
1070 | die "Unable to determine upstream SVN information from ", |
1071 | "working tree history\n"; | |
1072 | } | |
7d45e146 SF |
1073 | if ($_dry_run) { |
1074 | print "Remote Branch: " . $gs->refname . "\n"; | |
1075 | print "SVN URL: " . $url . "\n"; | |
1076 | return; | |
1077 | } | |
905f8b7d EW |
1078 | if (command(qw/diff-index HEAD --/)) { |
1079 | print STDERR "Cannot rebase with uncommited changes:\n"; | |
1080 | command_noisy('status'); | |
1081 | exit 1; | |
1082 | } | |
dee41f3e | 1083 | unless ($_local) { |
cec0d5a3 SG |
1084 | # rebase will checkout for us, so no need to do it explicitly |
1085 | $_no_checkout = 'true'; | |
dee41f3e EW |
1086 | $_fetch_all ? $gs->fetch_all : $gs->fetch; |
1087 | } | |
905f8b7d | 1088 | command_noisy(rebase_cmd(), $gs->refname); |
55f9d7a7 MH |
1089 | if (auto_create_empty_directories($gs)) { |
1090 | $gs->mkemptydirs; | |
1091 | } | |
905f8b7d EW |
1092 | } |
1093 | ||
5969cbe1 | 1094 | sub cmd_show_ignore { |
13c823fb EW |
1095 | my ($url, $rev, $uuid, $gs) = working_head_info('HEAD'); |
1096 | $gs ||= Git::SVN->new; | |
5969cbe1 | 1097 | my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum); |
01bdab84 BS |
1098 | $gs->prop_walk($gs->{path}, $r, sub { |
1099 | my ($gs, $path, $props) = @_; | |
1100 | print STDOUT "\n# $path\n"; | |
1101 | my $s = $props->{'svn:ignore'} or return; | |
1102 | $s =~ s/[\r\n]+/\n/g; | |
a7d72544 | 1103 | $s =~ s/^\n+//; |
01bdab84 BS |
1104 | chomp $s; |
1105 | $s =~ s#^#$path#gm; | |
1106 | print STDOUT "$s\n"; | |
1107 | }); | |
a5e0cedc EW |
1108 | } |
1109 | ||
2d879792 VK |
1110 | sub cmd_show_externals { |
1111 | my ($url, $rev, $uuid, $gs) = working_head_info('HEAD'); | |
1112 | $gs ||= Git::SVN->new; | |
1113 | my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum); | |
1114 | $gs->prop_walk($gs->{path}, $r, sub { | |
1115 | my ($gs, $path, $props) = @_; | |
1116 | print STDOUT "\n# $path\n"; | |
1117 | my $s = $props->{'svn:externals'} or return; | |
1118 | $s =~ s/[\r\n]+/\n/g; | |
1119 | chomp $s; | |
1120 | $s =~ s#^#$path#gm; | |
1121 | print STDOUT "$s\n"; | |
1122 | }); | |
1123 | } | |
1124 | ||
d05ddec5 BS |
1125 | sub cmd_create_ignore { |
1126 | my ($url, $rev, $uuid, $gs) = working_head_info('HEAD'); | |
1127 | $gs ||= Git::SVN->new; | |
1128 | my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum); | |
1129 | $gs->prop_walk($gs->{path}, $r, sub { | |
1130 | my ($gs, $path, $props) = @_; | |
1131 | # $path is of the form /path/to/dir/ | |
7d9fd459 BG |
1132 | $path = '.' . $path; |
1133 | # SVN can have attributes on empty directories, | |
1134 | # which git won't track | |
1135 | mkpath([$path]) unless -d $path; | |
1136 | my $ignore = $path . '.gitignore'; | |
d05ddec5 BS |
1137 | my $s = $props->{'svn:ignore'} or return; |
1138 | open(GITIGNORE, '>', $ignore) | |
207f1a75 | 1139 | or fatal("Failed to open `$ignore' for writing: $!"); |
d05ddec5 | 1140 | $s =~ s/[\r\n]+/\n/g; |
a7d72544 | 1141 | $s =~ s/^\n+//; |
d05ddec5 BS |
1142 | chomp $s; |
1143 | # Prefix all patterns so that the ignore doesn't apply | |
1144 | # to sub-directories. | |
1145 | $s =~ s#^#/#gm; | |
1146 | print GITIGNORE "$s\n"; | |
1147 | close(GITIGNORE) | |
207f1a75 | 1148 | or fatal("Failed to close `$ignore': $!"); |
c4c66b26 | 1149 | command_noisy('add', '-f', $ignore); |
d05ddec5 BS |
1150 | }); |
1151 | } | |
1152 | ||
6111b934 EW |
1153 | sub cmd_mkdirs { |
1154 | my ($url, $rev, $uuid, $gs) = working_head_info('HEAD'); | |
1155 | $gs ||= Git::SVN->new; | |
1156 | $gs->mkemptydirs($_revision); | |
1157 | } | |
1158 | ||
b2b3ada7 DK |
1159 | sub canonicalize_path { |
1160 | my ($path) = @_; | |
e6fefa92 DK |
1161 | my $dot_slash_added = 0; |
1162 | if (substr($path, 0, 1) ne "/") { | |
1163 | $path = "./" . $path; | |
1164 | $dot_slash_added = 1; | |
1165 | } | |
b2b3ada7 DK |
1166 | # File::Spec->canonpath doesn't collapse x/../y into y (for a |
1167 | # good reason), so let's do this manually. | |
1168 | $path =~ s#/+#/#g; | |
1169 | $path =~ s#/\.(?:/|$)#/#g; | |
1170 | $path =~ s#/[^/]+/\.\.##g; | |
1171 | $path =~ s#/$##g; | |
e6fefa92 | 1172 | $path =~ s#^\./## if $dot_slash_added; |
2fe403e7 GP |
1173 | $path =~ s#^/##; |
1174 | $path =~ s#^\.$##; | |
b2b3ada7 DK |
1175 | return $path; |
1176 | } | |
1177 | ||
50ff2366 UD |
1178 | sub canonicalize_url { |
1179 | my ($url) = @_; | |
1180 | $url =~ s#^([^:]+://[^/]*/)(.*)$#$1 . canonicalize_path($2)#e; | |
1181 | return $url; | |
1182 | } | |
1183 | ||
15153451 BS |
1184 | # get_svnprops(PATH) |
1185 | # ------------------ | |
51e057cf | 1186 | # Helper for cmd_propget and cmd_proplist below. |
15153451 BS |
1187 | sub get_svnprops { |
1188 | my $path = shift; | |
1189 | my ($url, $rev, $uuid, $gs) = working_head_info('HEAD'); | |
1190 | $gs ||= Git::SVN->new; | |
1191 | ||
1192 | # prefix THE PATH by the sub-directory from which the user | |
1193 | # invoked us. | |
1194 | $path = $cmd_dir_prefix . $path; | |
207f1a75 | 1195 | fatal("No such file or directory: $path") unless -e $path; |
15153451 BS |
1196 | my $is_dir = -d $path ? 1 : 0; |
1197 | $path = $gs->{path} . '/' . $path; | |
1198 | ||
1199 | # canonicalize the path (otherwise libsvn will abort or fail to | |
1200 | # find the file) | |
b2b3ada7 | 1201 | $path = canonicalize_path($path); |
15153451 BS |
1202 | |
1203 | my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum); | |
1204 | my $props; | |
1205 | if ($is_dir) { | |
1206 | (undef, undef, $props) = $gs->ra->get_dir($path, $r); | |
1207 | } | |
1208 | else { | |
1209 | (undef, $props) = $gs->ra->get_file($path, $r, undef); | |
1210 | } | |
1211 | return $props; | |
1212 | } | |
1213 | ||
1214 | # cmd_propget (PROP, PATH) | |
1215 | # ------------------------ | |
1216 | # Print the SVN property PROP for PATH. | |
1217 | sub cmd_propget { | |
1218 | my ($prop, $path) = @_; | |
1219 | $path = '.' if not defined $path; | |
1220 | usage(1) if not defined $prop; | |
1221 | my $props = get_svnprops($path); | |
1222 | if (not defined $props->{$prop}) { | |
207f1a75 | 1223 | fatal("`$path' does not have a `$prop' SVN property."); |
15153451 BS |
1224 | } |
1225 | print $props->{$prop} . "\n"; | |
1226 | } | |
1227 | ||
51e057cf BS |
1228 | # cmd_proplist (PATH) |
1229 | # ------------------- | |
1230 | # Print the list of SVN properties for PATH. | |
1231 | sub cmd_proplist { | |
1232 | my $path = shift; | |
1233 | $path = '.' if not defined $path; | |
1234 | my $props = get_svnprops($path); | |
1235 | print "Properties on '$path':\n"; | |
1236 | foreach (sort keys %{$props}) { | |
1237 | print " $_\n"; | |
1238 | } | |
1239 | } | |
1240 | ||
8164b652 | 1241 | sub cmd_multi_init { |
9d55b41a | 1242 | my $url = shift; |
62244069 | 1243 | unless (defined $_trunk || @_branches || @_tags) { |
98327e58 | 1244 | usage(1); |
9d55b41a | 1245 | } |
dc431666 | 1246 | |
8164b652 | 1247 | $_prefix = '' unless defined $_prefix; |
dadc6d2a | 1248 | if (defined $url) { |
50ff2366 | 1249 | $url = canonicalize_url($url); |
dadc6d2a EW |
1250 | init_subdir(@_); |
1251 | } | |
f30603fc | 1252 | do_git_init_db(); |
98327e58 | 1253 | if (defined $_trunk) { |
b4b33600 | 1254 | $_trunk =~ s#^/+##; |
6f5748e1 | 1255 | my $trunk_ref = 'refs/remotes/' . $_prefix . 'trunk'; |
706587fc EW |
1256 | # try both old-style and new-style lookups: |
1257 | my $gs_trunk = eval { Git::SVN->new($trunk_ref) }; | |
8164b652 | 1258 | unless ($gs_trunk) { |
706587fc EW |
1259 | my ($trunk_url, $trunk_path) = |
1260 | complete_svn_url($url, $_trunk); | |
1261 | $gs_trunk = Git::SVN->init($trunk_url, $trunk_path, | |
1262 | undef, $trunk_ref); | |
98327e58 | 1263 | } |
c35b96e7 | 1264 | } |
62244069 | 1265 | return unless @_branches || @_tags; |
e7db67e6 | 1266 | my $ra = $url ? Git::SVN::Ra->new($url) : undef; |
62244069 MB |
1267 | foreach my $path (@_branches) { |
1268 | complete_url_ls_init($ra, $path, '--branches/-b', $_prefix); | |
1269 | } | |
1270 | foreach my $path (@_tags) { | |
1271 | complete_url_ls_init($ra, $path, '--tags/-t', $_prefix.'tags/'); | |
1272 | } | |
9d55b41a EW |
1273 | } |
1274 | ||
1c8443b0 | 1275 | sub cmd_multi_fetch { |
4d0157d6 | 1276 | $Git::SVN::no_reuse_existing = undef; |
0af9c9f9 EW |
1277 | my $remotes = Git::SVN::read_all_remotes(); |
1278 | foreach my $repo_id (sort keys %$remotes) { | |
db03cd24 | 1279 | if ($remotes->{$repo_id}->{url}) { |
4bb9ed04 EW |
1280 | Git::SVN::fetch_all($repo_id, $remotes); |
1281 | } | |
9d55b41a | 1282 | } |
9d55b41a EW |
1283 | } |
1284 | ||
44320b9e EW |
1285 | # this command is special because it requires no metadata |
1286 | sub cmd_commit_diff { | |
1287 | my ($ta, $tb, $url) = @_; | |
1288 | my $usage = "Usage: $0 commit-diff -r<revision> ". | |
207f1a75 | 1289 | "<tree-ish> <tree-ish> [<URL>]"; |
44320b9e | 1290 | fatal($usage) if (!defined $ta || !defined $tb); |
d72ab8c8 | 1291 | my $svn_path = ''; |
44320b9e EW |
1292 | if (!defined $url) { |
1293 | my $gs = eval { Git::SVN->new }; | |
1294 | if (!$gs) { | |
1295 | fatal("Needed URL or usable git-svn --id in ", | |
1296 | "the command-line\n", $usage); | |
1297 | } | |
1298 | $url = $gs->{url}; | |
d3a840dc | 1299 | $svn_path = $gs->{path}; |
44320b9e EW |
1300 | } |
1301 | unless (defined $_revision) { | |
1302 | fatal("-r|--revision is a required argument\n", $usage); | |
1303 | } | |
1304 | if (defined $_message && defined $_file) { | |
1305 | fatal("Both --message/-m and --file/-F specified ", | |
1306 | "for the commit message.\n", | |
207f1a75 | 1307 | "I have no idea what you mean"); |
44320b9e EW |
1308 | } |
1309 | if (defined $_file) { | |
1310 | $_message = file_to_s($_file); | |
1311 | } else { | |
1312 | $_message ||= get_commit_entry($tb)->{log}; | |
1313 | } | |
1314 | my $ra ||= Git::SVN::Ra->new($url); | |
1315 | my $r = $_revision; | |
1316 | if ($r eq 'HEAD') { | |
1317 | $r = $ra->get_latest_revnum; | |
1318 | } elsif ($r !~ /^\d+$/) { | |
1319 | die "revision argument: $r not understood by git-svn\n"; | |
1320 | } | |
61395354 EW |
1321 | my %ed_opts = ( r => $r, |
1322 | log => $_message, | |
1323 | ra => $ra, | |
1324 | tree_a => $ta, | |
1325 | tree_b => $tb, | |
1326 | editor_cb => sub { print "Committed r$_[0]\n" }, | |
1327 | svn_path => $svn_path ); | |
1328 | if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) { | |
44320b9e EW |
1329 | print "No changes\n$ta == $tb\n"; |
1330 | } | |
44320b9e EW |
1331 | } |
1332 | ||
05427b91 TR |
1333 | sub escape_uri_only { |
1334 | my ($uri) = @_; | |
1335 | my @tmp; | |
1336 | foreach (split m{/}, $uri) { | |
6a004d3f | 1337 | s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg; |
05427b91 TR |
1338 | push @tmp, $_; |
1339 | } | |
1340 | join('/', @tmp); | |
1341 | } | |
1342 | ||
1343 | sub escape_url { | |
1344 | my ($url) = @_; | |
1345 | if ($url =~ m#^([^:]+)://([^/]*)(.*)$#) { | |
1346 | my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3)); | |
1347 | $url = "$scheme://$domain$uri"; | |
1348 | } | |
1349 | $url; | |
1350 | } | |
1351 | ||
e6fefa92 | 1352 | sub cmd_info { |
bd2d4f96 | 1353 | my $path = canonicalize_path(defined($_[0]) ? $_[0] : "."); |
edde9112 | 1354 | my $fullpath = canonicalize_path($cmd_dir_prefix . $path); |
bd2d4f96 | 1355 | if (exists $_[1]) { |
e6fefa92 DK |
1356 | die "Too many arguments specified\n"; |
1357 | } | |
1358 | ||
1359 | my ($file_type, $diff_status) = find_file_type_and_diff_status($path); | |
1360 | ||
1361 | if (!$file_type && !$diff_status) { | |
2cf3e3ac TR |
1362 | print STDERR "svn: '$path' is not under version control\n"; |
1363 | exit 1; | |
e6fefa92 DK |
1364 | } |
1365 | ||
1366 | my ($url, $rev, $uuid, $gs) = working_head_info('HEAD'); | |
1367 | unless ($gs) { | |
1368 | die "Unable to determine upstream SVN information from ", | |
1369 | "working tree history\n"; | |
1370 | } | |
bd2d4f96 EW |
1371 | |
1372 | # canonicalize_path() will return "" to make libsvn 1.5.x happy, | |
1373 | $path = "." if $path eq ""; | |
1374 | ||
edde9112 | 1375 | my $full_url = $url . ($fullpath eq "" ? "" : "/$fullpath"); |
e6fefa92 | 1376 | |
8b014d71 | 1377 | if ($_url) { |
05427b91 | 1378 | print escape_url($full_url), "\n"; |
8b014d71 DK |
1379 | return; |
1380 | } | |
1381 | ||
e6fefa92 DK |
1382 | my $result = "Path: $path\n"; |
1383 | $result .= "Name: " . basename($path) . "\n" if $file_type ne "dir"; | |
05427b91 | 1384 | $result .= "URL: " . escape_url($full_url) . "\n"; |
e6fefa92 | 1385 | |
a5460eb7 EW |
1386 | eval { |
1387 | my $repos_root = $gs->repos_root; | |
1388 | Git::SVN::remove_username($repos_root); | |
05427b91 | 1389 | $result .= "Repository Root: " . escape_url($repos_root) . "\n"; |
a5460eb7 EW |
1390 | }; |
1391 | if ($@) { | |
1392 | $result .= "Repository Root: (offline)\n"; | |
1393 | } | |
b91a8a3e | 1394 | ::_req_svn(); |
22ba47f5 MK |
1395 | $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A" && |
1396 | ($SVN::Core::VERSION le '1.5.4' || $file_type ne "dir"); | |
e6fefa92 DK |
1397 | $result .= "Revision: " . ($diff_status eq "A" ? 0 : $rev) . "\n"; |
1398 | ||
1399 | $result .= "Node Kind: " . | |
1400 | ($file_type eq "dir" ? "directory" : "file") . "\n"; | |
1401 | ||
1402 | my $schedule = $diff_status eq "A" | |
1403 | ? "add" | |
1404 | : ($diff_status eq "D" ? "delete" : "normal"); | |
1405 | $result .= "Schedule: $schedule\n"; | |
1406 | ||
1407 | if ($diff_status eq "A") { | |
1408 | print $result, "\n"; | |
1409 | return; | |
1410 | } | |
1411 | ||
1412 | my ($lc_author, $lc_rev, $lc_date_utc); | |
edde9112 | 1413 | my @args = Git::SVN::Log::git_svn_log_cmd($rev, $rev, "--", $fullpath); |
e6fefa92 DK |
1414 | my $log = command_output_pipe(@args); |
1415 | my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/; | |
1416 | while (<$log>) { | |
1417 | if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) { | |
1418 | $lc_author = $1; | |
1419 | $lc_date_utc = Git::SVN::Log::parse_git_date($2, $3); | |
1420 | } elsif (/^${esc_color} (git-svn-id:.+)$/o) { | |
1421 | (undef, $lc_rev, undef) = ::extract_metadata($1); | |
1422 | } | |
1423 | } | |
1424 | close $log; | |
1425 | ||
1426 | Git::SVN::Log::set_local_timezone(); | |
1427 | ||
1428 | $result .= "Last Changed Author: $lc_author\n"; | |
1429 | $result .= "Last Changed Rev: $lc_rev\n"; | |
1430 | $result .= "Last Changed Date: " . | |
1431 | Git::SVN::Log::format_svn_date($lc_date_utc) . "\n"; | |
1432 | ||
1433 | if ($file_type ne "dir") { | |
1434 | my $text_last_updated_date = | |
1435 | ($diff_status eq "D" ? $lc_date_utc : (stat $path)[9]); | |
1436 | $result .= | |
1437 | "Text Last Updated: " . | |
1438 | Git::SVN::Log::format_svn_date($text_last_updated_date) . | |
1439 | "\n"; | |
1440 | my $checksum; | |
1441 | if ($diff_status eq "D") { | |
1442 | my ($fh, $ctx) = | |
1443 | command_output_pipe(qw(cat-file blob), "HEAD:$path"); | |
1444 | if ($file_type eq "link") { | |
1445 | my $file_name = <$fh>; | |
8d7c4fad | 1446 | $checksum = md5sum("link $file_name"); |
e6fefa92 | 1447 | } else { |
8d7c4fad | 1448 | $checksum = md5sum($fh); |
e6fefa92 DK |
1449 | } |
1450 | command_close_pipe($fh, $ctx); | |
1451 | } elsif ($file_type eq "link") { | |
1452 | my $file_name = | |
1453 | command(qw(cat-file blob), "HEAD:$path"); | |
1454 | $checksum = | |
8d7c4fad | 1455 | md5sum("link " . $file_name); |
e6fefa92 DK |
1456 | } else { |
1457 | open FILE, "<", $path or die $!; | |
8d7c4fad | 1458 | $checksum = md5sum(\*FILE); |
e6fefa92 DK |
1459 | close FILE or die $!; |
1460 | } | |
1461 | $result .= "Checksum: " . $checksum . "\n"; | |
1462 | } | |
1463 | ||
1464 | print $result, "\n"; | |
1465 | } | |
1466 | ||
195643f2 BJ |
1467 | sub cmd_reset { |
1468 | my $target = shift || $_revision or die "SVN revision required\n"; | |
1469 | $target = $1 if $target =~ /^r(\d+)$/; | |
1470 | $target =~ /^\d+$/ or die "Numeric SVN revision expected\n"; | |
1471 | my ($url, $rev, $uuid, $gs) = working_head_info('HEAD'); | |
1472 | unless ($gs) { | |
1473 | die "Unable to determine upstream SVN information from ". | |
1474 | "history\n"; | |
1475 | } | |
1476 | my ($r, $c) = $gs->find_rev_before($target, not $_fetch_parent); | |
70ee0b77 | 1477 | die "Cannot find SVN revision $target\n" unless defined($c); |
195643f2 BJ |
1478 | $gs->rev_map_set($r, $c, 'reset', $uuid); |
1479 | print "r$r = $c ($gs->{ref_id})\n"; | |
1480 | } | |
1481 | ||
2da9ee08 RZ |
1482 | sub cmd_gc { |
1483 | if (!$can_compress) { | |
1484 | warn "Compress::Zlib could not be found; unhandled.log " . | |
1485 | "files will not be compressed.\n"; | |
1486 | } | |
1487 | find({ wanted => \&gc_directory, no_chdir => 1}, "$ENV{GIT_DIR}/svn"); | |
1488 | } | |
1489 | ||
3397f9df EW |
1490 | ########################### utility functions ######################### |
1491 | ||
905f8b7d EW |
1492 | sub rebase_cmd { |
1493 | my @cmd = qw/rebase/; | |
1494 | push @cmd, '-v' if $_verbose; | |
1495 | push @cmd, qw/--merge/ if $_merge; | |
1496 | push @cmd, "--strategy=$_strategy" if $_strategy; | |
1497 | @cmd; | |
1498 | } | |
1499 | ||
1e889ef3 EW |
1500 | sub post_fetch_checkout { |
1501 | return if $_no_checkout; | |
1502 | my $gs = $Git::SVN::_head or return; | |
1503 | return if verify_ref('refs/heads/master^0'); | |
1504 | ||
b186a261 EW |
1505 | # look for "trunk" ref if it exists |
1506 | my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}}; | |
1507 | my $fetch = $remote->{fetch}; | |
1508 | if ($fetch) { | |
1509 | foreach my $p (keys %$fetch) { | |
1510 | basename($fetch->{$p}) eq 'trunk' or next; | |
1511 | $gs = Git::SVN->new($fetch->{$p}, $gs->{repo_id}, $p); | |
1512 | last; | |
1513 | } | |
1514 | } | |
1515 | ||
1e889ef3 EW |
1516 | my $valid_head = verify_ref('HEAD^0'); |
1517 | command_noisy(qw(update-ref refs/heads/master), $gs->refname); | |
1518 | return if ($valid_head || !verify_ref('HEAD^0')); | |
1519 | ||
1520 | return if $ENV{GIT_DIR} !~ m#^(?:.*/)?\.git$#; | |
1521 | my $index = $ENV{GIT_INDEX_FILE} || "$ENV{GIT_DIR}/index"; | |
1522 | return if -f $index; | |
1523 | ||
7ae3df8c | 1524 | return if command_oneline(qw/rev-parse --is-inside-work-tree/) eq 'false'; |
1e889ef3 EW |
1525 | return if command_oneline(qw/rev-parse --is-inside-git-dir/) eq 'true'; |
1526 | command_noisy(qw/read-tree -m -u -v HEAD HEAD/); | |
1527 | print STDERR "Checked out HEAD:\n ", | |
1528 | $gs->full_url, " r", $gs->last_rev, "\n"; | |
55f9d7a7 MH |
1529 | if (auto_create_empty_directories($gs)) { |
1530 | $gs->mkemptydirs($gs->last_rev); | |
1531 | } | |
1e889ef3 EW |
1532 | } |
1533 | ||
98327e58 EW |
1534 | sub complete_svn_url { |
1535 | my ($url, $path) = @_; | |
1536 | $path =~ s#/+$##; | |
98327e58 | 1537 | if ($path !~ m#^[a-z\+]+://#) { |
98327e58 EW |
1538 | if (!defined $url || $url !~ m#^[a-z\+]+://#) { |
1539 | fatal("E: '$path' is not a complete URL ", | |
207f1a75 | 1540 | "and a separate URL is not specified"); |
98327e58 | 1541 | } |
706587fc | 1542 | return ($url, $path); |
98327e58 | 1543 | } |
706587fc | 1544 | return ($path, ''); |
98327e58 EW |
1545 | } |
1546 | ||
9d55b41a | 1547 | sub complete_url_ls_init { |
706587fc EW |
1548 | my ($ra, $repo_path, $switch, $pfx) = @_; |
1549 | unless ($repo_path) { | |
9d55b41a EW |
1550 | print STDERR "W: $switch not specified\n"; |
1551 | return; | |
1552 | } | |
706587fc EW |
1553 | $repo_path =~ s#/+$##; |
1554 | if ($repo_path =~ m#^[a-z\+]+://#) { | |
1555 | $ra = Git::SVN::Ra->new($repo_path); | |
1556 | $repo_path = ''; | |
e7db67e6 | 1557 | } else { |
706587fc | 1558 | $repo_path =~ s#^/+##; |
e7db67e6 | 1559 | unless ($ra) { |
706587fc | 1560 | fatal("E: '$repo_path' is not a complete URL ", |
207f1a75 | 1561 | "and a separate URL is not specified"); |
8164b652 | 1562 | } |
e7db67e6 | 1563 | } |
706587fc | 1564 | my $url = $ra->{url}; |
b4d57e5e EW |
1565 | my $gs = Git::SVN->init($url, undef, undef, undef, 1); |
1566 | my $k = "svn-remote.$gs->{repo_id}.url"; | |
1567 | my $orig_url = eval { command_oneline(qw/config --get/, $k) }; | |
1568 | if ($orig_url && ($orig_url ne $gs->{url})) { | |
1569 | die "$k already set: $orig_url\n", | |
1570 | "wanted to set to: $gs->{url}\n"; | |
88cf4107 | 1571 | } |
b4d57e5e | 1572 | command_oneline('config', $k, $gs->{url}) unless $orig_url; |
0b2af457 | 1573 | my $remote_path = "$gs->{path}/$repo_path"; |
5268f9ed | 1574 | $remote_path =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg; |
b4d57e5e EW |
1575 | $remote_path =~ s#/+#/#g; |
1576 | $remote_path =~ s#^/##g; | |
ed0b9d43 | 1577 | $remote_path .= "/*" if $remote_path !~ /\*/; |
b4d57e5e EW |
1578 | my ($n) = ($switch =~ /^--(\w+)/); |
1579 | if (length $pfx && $pfx !~ m#/$#) { | |
1580 | die "--prefix='$pfx' must have a trailing slash '/'\n"; | |
9d55b41a | 1581 | } |
570d35c2 | 1582 | command_noisy('config', |
62244069 | 1583 | '--add', |
570d35c2 MG |
1584 | "svn-remote.$gs->{repo_id}.$n", |
1585 | "$remote_path:refs/remotes/$pfx*" . | |
1586 | ('/*' x (($remote_path =~ tr/*/*/) - 1)) ); | |
9d55b41a EW |
1587 | } |
1588 | ||
aef4e921 EW |
1589 | sub verify_ref { |
1590 | my ($ref) = @_; | |
2c5c1d53 EW |
1591 | eval { command_oneline([ 'rev-parse', '--verify', $ref ], |
1592 | { STDERR => 0 }); }; | |
aef4e921 EW |
1593 | } |
1594 | ||
a5e0cedc | 1595 | sub get_tree_from_treeish { |
cf52b8f0 | 1596 | my ($treeish) = @_; |
44320b9e | 1597 | # $treeish can be a symbolic ref, too: |
aef4e921 | 1598 | my $type = command_oneline(qw/cat-file -t/, $treeish); |
cf52b8f0 EW |
1599 | my $expected; |
1600 | while ($type eq 'tag') { | |
aef4e921 | 1601 | ($treeish, $type) = command(qw/cat-file tag/, $treeish); |
cf52b8f0 EW |
1602 | } |
1603 | if ($type eq 'commit') { | |
aef4e921 EW |
1604 | $expected = (grep /^tree /, command(qw/cat-file commit/, |
1605 | $treeish))[0]; | |
44320b9e | 1606 | ($expected) = ($expected =~ /^tree ($sha1)$/o); |
cf52b8f0 EW |
1607 | die "Unable to get tree from $treeish\n" unless $expected; |
1608 | } elsif ($type eq 'tree') { | |
1609 | $expected = $treeish; | |
1610 | } else { | |
1611 | die "$treeish is a $type, expected tree, tag or commit\n"; | |
1612 | } | |
a5e0cedc EW |
1613 | return $expected; |
1614 | } | |
1615 | ||
44320b9e EW |
1616 | sub get_commit_entry { |
1617 | my ($treeish) = shift; | |
1618 | my %log_entry = ( log => '', tree => get_tree_from_treeish($treeish) ); | |
1619 | my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG"; | |
1620 | my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG"; | |
1621 | open my $log_fh, '>', $commit_editmsg or croak $!; | |
3397f9df | 1622 | |
44320b9e | 1623 | my $type = command_oneline(qw/cat-file -t/, $treeish); |
4ad4515d | 1624 | if ($type eq 'commit' || $type eq 'tag') { |
aef4e921 | 1625 | my ($msg_fh, $ctx) = command_output_pipe('cat-file', |
44320b9e | 1626 | $type, $treeish); |
3397f9df | 1627 | my $in_msg = 0; |
6aa9ba14 AP |
1628 | my $author; |
1629 | my $saw_from = 0; | |
328eb9b3 | 1630 | my $msgbuf = ""; |
3397f9df EW |
1631 | while (<$msg_fh>) { |
1632 | if (!$in_msg) { | |
1633 | $in_msg = 1 if (/^\s*$/); | |
6aa9ba14 | 1634 | $author = $1 if (/^author (.*>)/); |
df746c5a | 1635 | } elsif (/^git-svn-id: /) { |
44320b9e EW |
1636 | # skip this for now, we regenerate the |
1637 | # correct one on re-fetch anyways | |
1638 | # TODO: set *:merge properties or like... | |
3397f9df | 1639 | } else { |
6aa9ba14 AP |
1640 | if (/^From:/ || /^Signed-off-by:/) { |
1641 | $saw_from = 1; | |
1642 | } | |
328eb9b3 | 1643 | $msgbuf .= $_; |
3397f9df EW |
1644 | } |
1645 | } | |
328eb9b3 | 1646 | $msgbuf =~ s/\s+$//s; |
6aa9ba14 AP |
1647 | if ($Git::SVN::_add_author_from && defined($author) |
1648 | && !$saw_from) { | |
328eb9b3 | 1649 | $msgbuf .= "\n\nFrom: $author"; |
6aa9ba14 | 1650 | } |
328eb9b3 | 1651 | print $log_fh $msgbuf or croak $!; |
aef4e921 | 1652 | command_close_pipe($msg_fh, $ctx); |
3397f9df | 1653 | } |
44320b9e | 1654 | close $log_fh or croak $!; |
3397f9df EW |
1655 | |
1656 | if ($_edit || ($type eq 'tree')) { | |
b4479f07 JN |
1657 | chomp(my $editor = command_oneline(qw(var GIT_EDITOR))); |
1658 | system('sh', '-c', $editor.' "$@"', $editor, $commit_editmsg); | |
3397f9df | 1659 | } |
44320b9e | 1660 | rename $commit_editmsg, $commit_msg or croak $!; |
16fc08e2 | 1661 | { |
b510df8a | 1662 | require Encode; |
16fc08e2 EW |
1663 | # SVN requires messages to be UTF-8 when entering the repo |
1664 | local $/; | |
1665 | open $log_fh, '<', $commit_msg or croak $!; | |
1666 | binmode $log_fh; | |
1667 | chomp($log_entry{log} = <$log_fh>); | |
1668 | ||
b510df8a EW |
1669 | my $enc = Git::config('i18n.commitencoding') || 'UTF-8'; |
1670 | my $msg = $log_entry{log}; | |
1671 | ||
1672 | eval { $msg = Encode::decode($enc, $msg, 1) }; | |
1673 | if ($@) { | |
1674 | die "Could not decode as $enc:\n", $msg, | |
1675 | "\nPerhaps you need to set i18n.commitencoding\n"; | |
16fc08e2 | 1676 | } |
b510df8a EW |
1677 | |
1678 | eval { $msg = Encode::encode('UTF-8', $msg, 1) }; | |
1679 | die "Could not encode as UTF-8:\n$msg\n" if $@; | |
1680 | ||
1681 | $log_entry{log} = $msg; | |
1682 | ||
16fc08e2 EW |
1683 | close $log_fh or croak $!; |
1684 | } | |
44320b9e EW |
1685 | unlink $commit_msg; |
1686 | \%log_entry; | |
a5e0cedc EW |
1687 | } |
1688 | ||
3397f9df EW |
1689 | sub s_to_file { |
1690 | my ($str, $file, $mode) = @_; | |
1691 | open my $fd,'>',$file or croak $!; | |
1692 | print $fd $str,"\n" or croak $!; | |
1693 | close $fd or croak $!; | |
1694 | chmod ($mode &~ umask, $file) if (defined $mode); | |
1695 | } | |
1696 | ||
1697 | sub file_to_s { | |
1698 | my $file = shift; | |
1699 | open my $fd,'<',$file or croak "$!: file: $file\n"; | |
1700 | local $/; | |
1701 | my $ret = <$fd>; | |
1702 | close $fd or croak $!; | |
1703 | $ret =~ s/\s*$//s; | |
1704 | return $ret; | |
1705 | } | |
1706 | ||
eeb0abe0 EW |
1707 | # '<svn username> = real-name <email address>' mapping based on git-svnimport: |
1708 | sub load_authors { | |
1709 | open my $authors, '<', $_authors or die "Can't open $_authors $!\n"; | |
f8c9d1d2 | 1710 | my $log = $cmd eq 'log'; |
eeb0abe0 EW |
1711 | while (<$authors>) { |
1712 | chomp; | |
575d025c | 1713 | next unless /^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/; |
eeb0abe0 | 1714 | my ($user, $name, $email) = ($1, $2, $3); |
f8c9d1d2 EW |
1715 | if ($log) { |
1716 | $Git::SVN::Log::rusers{"$name <$email>"} = $user; | |
1717 | } else { | |
1718 | $users{$user} = [$name, $email]; | |
1719 | } | |
79bb8d88 EW |
1720 | } |
1721 | close $authors or croak $!; | |
1722 | } | |
1723 | ||
e0d10e1c | 1724 | # convert GetOpt::Long specs for use by git-config |
1a30582b | 1725 | sub read_git_config { |
b8c92cad | 1726 | my $opts = shift; |
97ae0911 | 1727 | my @config_only; |
b8c92cad | 1728 | foreach my $o (keys %$opts) { |
97ae0911 EW |
1729 | # if we have mixedCase and a long option-only, then |
1730 | # it's a config-only variable that we don't need for | |
1731 | # the command-line. | |
1732 | push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i); | |
b8c92cad | 1733 | my $v = $opts->{$o}; |
97ae0911 | 1734 | my ($key) = ($o =~ /^([a-zA-Z\-]+)/); |
b8c92cad | 1735 | $key =~ s/-//g; |
225f1d0c | 1736 | my $arg = 'git config'; |
b8c92cad EW |
1737 | $arg .= ' --int' if ($o =~ /[:=]i$/); |
1738 | $arg .= ' --bool' if ($o !~ /[:=][sfi]$/); | |
1739 | if (ref $v eq 'ARRAY') { | |
1740 | chomp(my @tmp = `$arg --get-all svn.$key`); | |
1741 | @$v = @tmp if @tmp; | |
1742 | } else { | |
1743 | chomp(my $tmp = `$arg --get svn.$key`); | |
7774284a | 1744 | if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) { |
b8c92cad EW |
1745 | $$v = $tmp; |
1746 | } | |
1747 | } | |
1748 | } | |
97ae0911 | 1749 | delete @$opts{@config_only} if @config_only; |
b8c92cad EW |
1750 | } |
1751 | ||
79bb8d88 | 1752 | sub extract_metadata { |
c1927a85 | 1753 | my $id = shift or return (undef, undef, undef); |
3dfab993 | 1754 | my ($url, $rev, $uuid) = ($id =~ /^\s*git-svn-id:\s+(.*)\@(\d+) |
b3e95936 | 1755 | \s([a-f\d\-]+)$/ix); |
e70dc780 | 1756 | if (!defined $rev || !$uuid || !$url) { |
79bb8d88 | 1757 | # some of the original repositories I made had |
82e5a82f | 1758 | # identifiers like this: |
b3e95936 | 1759 | ($rev, $uuid) = ($id =~/^\s*git-svn-id:\s(\d+)\@([a-f\d\-]+)/i); |
79bb8d88 EW |
1760 | } |
1761 | return ($url, $rev, $uuid); | |
1762 | } | |
1763 | ||
c1927a85 EW |
1764 | sub cmt_metadata { |
1765 | return extract_metadata((grep(/^git-svn-id: /, | |
aef4e921 | 1766 | command(qw/cat-file commit/, shift)))[-1]); |
c1927a85 EW |
1767 | } |
1768 | ||
6ea42032 BB |
1769 | sub cmt_sha2rev_batch { |
1770 | my %s2r; | |
1771 | my ($pid, $in, $out, $ctx) = command_bidi_pipe(qw/cat-file --batch/); | |
1772 | my $list = shift; | |
1773 | ||
1774 | foreach my $sha (@{$list}) { | |
1775 | my $first = 1; | |
1776 | my $size = 0; | |
1777 | print $out $sha, "\n"; | |
1778 | ||
1779 | while (my $line = <$in>) { | |
1780 | if ($first && $line =~ /^[[:xdigit:]]{40}\smissing$/) { | |
1781 | last; | |
1782 | } elsif ($first && | |
1783 | $line =~ /^[[:xdigit:]]{40}\scommit\s(\d+)$/) { | |
1784 | $first = 0; | |
1785 | $size = $1; | |
1786 | next; | |
1787 | } elsif ($line =~ /^(git-svn-id: )/) { | |
1788 | my (undef, $rev, undef) = | |
1789 | extract_metadata($line); | |
1790 | $s2r{$sha} = $rev; | |
1791 | } | |
1792 | ||
1793 | $size -= length($line); | |
1794 | last if ($size == 0); | |
1795 | } | |
1796 | } | |
1797 | ||
1798 | command_close_bidi_pipe($pid, $in, $out, $ctx); | |
1799 | ||
1800 | return \%s2r; | |
1801 | } | |
1802 | ||
905f8b7d EW |
1803 | sub working_head_info { |
1804 | my ($head, $refs) = @_; | |
8565a568 ML |
1805 | my @args = qw/log --no-color --no-decorate --first-parent |
1806 | --pretty=medium/; | |
05b4df31 | 1807 | my ($fh, $ctx) = command_output_pipe(@args, $head); |
3dfab993 | 1808 | my $hash; |
40cb8f8f | 1809 | my %max; |
3dfab993 SV |
1810 | while (<$fh>) { |
1811 | if ( m{^commit ($::sha1)$} ) { | |
1812 | unshift @$refs, $hash if $hash and $refs; | |
1813 | $hash = $1; | |
1814 | next; | |
1815 | } | |
1816 | next unless s{^\s*(git-svn-id:)}{$1}; | |
1817 | my ($url, $rev, $uuid) = extract_metadata($_); | |
13c823fb | 1818 | if (defined $url && defined $rev) { |
40cb8f8f | 1819 | next if $max{$url} and $max{$url} < $rev; |
13c823fb | 1820 | if (my $gs = Git::SVN->find_by_url($url)) { |
63c56022 | 1821 | my $c = $gs->rev_map_get($rev, $uuid); |
b03c7a63 | 1822 | if ($c && $c eq $hash) { |
13c823fb EW |
1823 | close $fh; # break the pipe |
1824 | return ($url, $rev, $uuid, $gs); | |
40cb8f8f | 1825 | } else { |
060610c5 | 1826 | $max{$url} ||= $gs->rev_map_max; |
13c823fb EW |
1827 | } |
1828 | } | |
1829 | } | |
905f8b7d | 1830 | } |
13c823fb EW |
1831 | command_close_pipe($fh, $ctx); |
1832 | (undef, undef, undef, undef); | |
905f8b7d EW |
1833 | } |
1834 | ||
733a65aa EW |
1835 | sub read_commit_parents { |
1836 | my ($parents, $c) = @_; | |
7b02b85a EW |
1837 | chomp(my $p = command_oneline(qw/rev-list --parents -1/, $c)); |
1838 | $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n"; | |
1839 | @{$parents->{$c}} = split(/ /, $p); | |
733a65aa EW |
1840 | } |
1841 | ||
1842 | sub linearize_history { | |
1843 | my ($gs, $refs) = @_; | |
1844 | my %parents; | |
1845 | foreach my $c (@$refs) { | |
1846 | read_commit_parents(\%parents, $c); | |
1847 | } | |
1848 | ||
1849 | my @linear_refs; | |
1850 | my %skip = (); | |
1851 | my $last_svn_commit = $gs->last_commit; | |
1852 | foreach my $c (reverse @$refs) { | |
1853 | next if $c eq $last_svn_commit; | |
1854 | last if $skip{$c}; | |
1855 | ||
1856 | unshift @linear_refs, $c; | |
1857 | $skip{$c} = 1; | |
1858 | ||
1859 | # we only want the first parent to diff against for linear | |
1860 | # history, we save the rest to inject when we finalize the | |
1861 | # svn commit | |
1862 | my $fp_a = verify_ref("$c~1"); | |
1863 | my $fp_b = shift @{$parents{$c}} if $parents{$c}; | |
1864 | if (!$fp_a || !$fp_b) { | |
1865 | die "Commit $c\n", | |
1866 | "has no parent commit, and therefore ", | |
1867 | "nothing to diff against.\n", | |
1868 | "You should be working from a repository ", | |
1869 | "originally created by git-svn\n"; | |
1870 | } | |
1871 | if ($fp_a ne $fp_b) { | |
1872 | die "$c~1 = $fp_a, however parsing commit $c ", | |
1873 | "revealed that:\n$c~1 = $fp_b\nBUG!\n"; | |
1874 | } | |
1875 | ||
1876 | foreach my $p (@{$parents{$c}}) { | |
1877 | $skip{$p} = 1; | |
1878 | } | |
1879 | } | |
1880 | (\@linear_refs, \%parents); | |
1881 | } | |
1882 | ||
e6fefa92 DK |
1883 | sub find_file_type_and_diff_status { |
1884 | my ($path) = @_; | |
107cee50 | 1885 | return ('dir', '') if $path eq ''; |
e6fefa92 DK |
1886 | |
1887 | my $diff_output = | |
1888 | command_oneline(qw(diff --cached --name-status --), $path) || ""; | |
1889 | my $diff_status = (split(' ', $diff_output))[0] || ""; | |
1890 | ||
1891 | my $ls_tree = command_oneline(qw(ls-tree HEAD), $path) || ""; | |
1892 | ||
1893 | return (undef, undef) if !$diff_status && !$ls_tree; | |
1894 | ||
1895 | if ($diff_status eq "A") { | |
1896 | return ("link", $diff_status) if -l $path; | |
1897 | return ("dir", $diff_status) if -d $path; | |
1898 | return ("file", $diff_status); | |
1899 | } | |
1900 | ||
1901 | my $mode = (split(' ', $ls_tree))[0] || ""; | |
1902 | ||
1903 | return ("link", $diff_status) if $mode eq "120000"; | |
1904 | return ("dir", $diff_status) if $mode eq "040000"; | |
1905 | return ("file", $diff_status); | |
1906 | } | |
1907 | ||
b2b3ada7 DK |
1908 | sub md5sum { |
1909 | my $arg = shift; | |
1910 | my $ref = ref $arg; | |
1911 | my $md5 = Digest::MD5->new(); | |
0b19138b | 1912 | if ($ref eq 'GLOB' || $ref eq 'IO::File' || $ref eq 'File::Temp') { |
b2b3ada7 DK |
1913 | $md5->addfile($arg) or croak $!; |
1914 | } elsif ($ref eq 'SCALAR') { | |
1915 | $md5->add($$arg) or croak $!; | |
1916 | } elsif (!$ref) { | |
1917 | $md5->add($arg) or croak $!; | |
1918 | } else { | |
1919 | ::fatal "Can't provide MD5 hash for unknown ref type: '", $ref, "'"; | |
1920 | } | |
1921 | return $md5->hexdigest(); | |
1922 | } | |
1923 | ||
2da9ee08 RZ |
1924 | sub gc_directory { |
1925 | if ($can_compress && -f $_ && basename($_) eq "unhandled.log") { | |
1926 | my $out_filename = $_ . ".gz"; | |
1927 | open my $in_fh, "<", $_ or die "Unable to open $_: $!\n"; | |
1928 | binmode $in_fh; | |
1929 | my $gz = Compress::Zlib::gzopen($out_filename, "ab") or | |
1930 | die "Unable to open $out_filename: $!\n"; | |
1931 | ||
1932 | my $res; | |
1933 | while ($res = sysread($in_fh, my $str, 1024)) { | |
1934 | $gz->gzwrite($str) or | |
1935 | die "Unable to write: ".$gz->gzerror()."!\n"; | |
1936 | } | |
1937 | unlink $_ or die "unlink $File::Find::name: $!\n"; | |
1938 | } elsif (-f $_ && basename($_) eq "index") { | |
1939 | unlink $_ or die "unlink $_: $!\n"; | |
1940 | } | |
1941 | } | |
1942 | ||
9b981fc6 EW |
1943 | package Git::SVN; |
1944 | use strict; | |
1945 | use warnings; | |
060610c5 EW |
1946 | use Fcntl qw/:DEFAULT :seek/; |
1947 | use constant rev_map_fmt => 'NH40'; | |
ecc712dd | 1948 | use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent |
62e349d2 | 1949 | $_repack $_repack_flags $_use_svm_props $_head |
70ae04e4 | 1950 | $_use_svnsync_props $no_reuse_existing $_minimize_url |
e82f0d73 | 1951 | $_use_log_author $_add_author_from $_localtime/; |
9b981fc6 EW |
1952 | use Carp qw/croak/; |
1953 | use File::Path qw/mkpath/; | |
373274f9 | 1954 | use File::Copy qw/copy/; |
9b981fc6 | 1955 | use IPC::Open3; |
7d944c33 | 1956 | use Memoize; # core since 5.8.0, Jul 2002 |
8bff7c53 | 1957 | use Memoize::Storable; |
9b981fc6 | 1958 | |
94bc914c KW |
1959 | my ($_gc_nr, $_gc_period); |
1960 | ||
9b981fc6 EW |
1961 | # properties that we do not log: |
1962 | my %SKIP_PROP; | |
1963 | BEGIN { | |
1964 | %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url | |
1965 | svn:special svn:executable | |
1966 | svn:entry:committed-rev | |
1967 | svn:entry:last-author | |
1968 | svn:entry:uuid | |
1969 | svn:entry:committed-date/; | |
91b03282 EW |
1970 | |
1971 | # some options are read globally, but can be overridden locally | |
1972 | # per [svn-remote "..."] section. Command-line options will *NOT* | |
1973 | # override options set in an [svn-remote "..."] section | |
c5f71ad0 SV |
1974 | no strict 'refs'; |
1975 | for my $option (qw/follow_parent no_metadata use_svm_props | |
1976 | use_svnsync_props/) { | |
1977 | my $key = $option; | |
91b03282 | 1978 | $key =~ tr/_//d; |
c5f71ad0 SV |
1979 | my $prop = "-$option"; |
1980 | *$option = sub { | |
1981 | my ($self) = @_; | |
1982 | return $self->{$prop} if exists $self->{$prop}; | |
1983 | my $k = "svn-remote.$self->{repo_id}.$key"; | |
1984 | eval { command_oneline(qw/config --get/, $k) }; | |
1985 | if ($@) { | |
1986 | $self->{$prop} = ${"Git::SVN::_$option"}; | |
91b03282 | 1987 | } else { |
c5f71ad0 SV |
1988 | my $v = command_oneline(qw/config --bool/,$k); |
1989 | $self->{$prop} = $v eq 'false' ? 0 : 1; | |
91b03282 | 1990 | } |
c5f71ad0 SV |
1991 | return $self->{$prop}; |
1992 | } | |
91b03282 | 1993 | } |
9b981fc6 EW |
1994 | } |
1995 | ||
0b19138b | 1996 | |
321b1842 EW |
1997 | my (%LOCKFILES, %INDEX_FILES); |
1998 | END { | |
1999 | unlink keys %LOCKFILES if %LOCKFILES; | |
2000 | unlink keys %INDEX_FILES if %INDEX_FILES; | |
2001 | } | |
373274f9 | 2002 | |
4bb9ed04 EW |
2003 | sub resolve_local_globs { |
2004 | my ($url, $fetch, $glob_spec) = @_; | |
2005 | return unless defined $glob_spec; | |
2006 | my $ref = $glob_spec->{ref}; | |
2007 | my $path = $glob_spec->{path}; | |
6f5748e1 AB |
2008 | foreach (command(qw#for-each-ref --format=%(refname) refs/#)) { |
2009 | next unless m#^$ref->{regex}$#; | |
4bb9ed04 | 2010 | my $p = $1; |
bf655fd7 RE |
2011 | my $pathname = desanitize_refname($path->full_path($p)); |
2012 | my $refname = desanitize_refname($ref->full_path($p)); | |
4bb9ed04 EW |
2013 | if (my $existing = $fetch->{$pathname}) { |
2014 | if ($existing ne $refname) { | |
2015 | die "Refspec conflict:\n", | |
6f5748e1 AB |
2016 | "existing: $existing\n", |
2017 | " globbed: $refname\n"; | |
4bb9ed04 | 2018 | } |
6f5748e1 | 2019 | my $u = (::cmt_metadata("$refname"))[0]; |
4e9f6cc7 | 2020 | $u =~ s!^\Q$url\E(/|$)!! or die |
6f5748e1 | 2021 | "$refname: '$url' not found in '$u'\n"; |
4bb9ed04 EW |
2022 | if ($pathname ne $u) { |
2023 | warn "W: Refspec glob conflict ", | |
6f5748e1 | 2024 | "(ref: $refname):\n", |
4bb9ed04 EW |
2025 | "expected path: $pathname\n", |
2026 | " real path: $u\n", | |
2027 | "Continuing ahead with $u\n"; | |
2028 | next; | |
2029 | } | |
2030 | } else { | |
4bb9ed04 EW |
2031 | $fetch->{$pathname} = $refname; |
2032 | } | |
2033 | } | |
2034 | } | |
2035 | ||
e98671e5 EW |
2036 | sub parse_revision_argument { |
2037 | my ($base, $head) = @_; | |
2038 | if (!defined $::_revision || $::_revision eq 'BASE:HEAD') { | |
2039 | return ($base, $head); | |
2040 | } | |
2041 | return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/); | |
2042 | return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/); | |
2043 | return ($head, $head) if ($::_revision eq 'HEAD'); | |
2044 | return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/); | |
2045 | return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/); | |
2046 | die "revision argument: $::_revision not understood by git-svn\n"; | |
2047 | } | |
2048 | ||
0af9c9f9 | 2049 | sub fetch_all { |
4bb9ed04 | 2050 | my ($repo_id, $remotes) = @_; |
905f8b7d EW |
2051 | if (ref $repo_id) { |
2052 | my $gs = $repo_id; | |
2053 | $repo_id = undef; | |
2054 | $repo_id = $gs->{repo_id}; | |
2055 | } | |
2056 | $remotes ||= read_all_remotes(); | |
7447b4bc EW |
2057 | my $remote = $remotes->{$repo_id} or |
2058 | die "[svn-remote \"$repo_id\"] unknown\n"; | |
e518192f | 2059 | my $fetch = $remote->{fetch}; |
7447b4bc | 2060 | my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n"; |
e518192f | 2061 | my (@gs, @globs); |
0af9c9f9 | 2062 | my $ra = Git::SVN::Ra->new($url); |
26a62d57 | 2063 | my $uuid = $ra->get_uuid; |
0af9c9f9 | 2064 | my $head = $ra->get_latest_revnum; |
577e9fca EW |
2065 | |
2066 | # ignore errors, $head revision may not even exist anymore | |
2067 | eval { $ra->get_log("", $head, 0, 1, 0, 1, sub { $head = $_[1] }) }; | |
2068 | warn "W: $@\n" if $@; | |
2069 | ||
28710f74 | 2070 | my $base = defined $fetch ? $head : 0; |
e518192f EW |
2071 | |
2072 | # read the max revs for wildcard expansion (branches/*, tags/*) | |
2073 | foreach my $t (qw/branches tags/) { | |
2074 | defined $remote->{$t} or next; | |
62244069 MB |
2075 | push @globs, @{$remote->{$t}}; |
2076 | ||
93f2689c EW |
2077 | my $max_rev = eval { tmp_config(qw/--int --get/, |
2078 | "svn-remote.$repo_id.${t}-maxRev") }; | |
2079 | if (defined $max_rev && ($max_rev < $base)) { | |
2080 | $base = $max_rev; | |
d6d3346b EW |
2081 | } elsif (!defined $max_rev) { |
2082 | $base = 0; | |
e518192f EW |
2083 | } |
2084 | } | |
2085 | ||
db03cd24 EW |
2086 | if ($fetch) { |
2087 | foreach my $p (sort keys %$fetch) { | |
2088 | my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p); | |
060610c5 | 2089 | my $lr = $gs->rev_map_max; |
db03cd24 EW |
2090 | if (defined $lr) { |
2091 | $base = $lr if ($lr < $base); | |
2092 | } | |
2093 | push @gs, $gs; | |
0af9c9f9 | 2094 | } |
0af9c9f9 | 2095 | } |
e98671e5 EW |
2096 | |
2097 | ($base, $head) = parse_revision_argument($base, $head); | |
e518192f | 2098 | $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs); |
0af9c9f9 EW |
2099 | } |
2100 | ||
47e39c55 EW |
2101 | sub read_all_remotes { |
2102 | my $r = {}; | |
63c56022 JA |
2103 | my $use_svm_props = eval { command_oneline(qw/config --bool |
2104 | svn.useSvmProps/) }; | |
2105 | $use_svm_props = $use_svm_props eq 'true' if $use_svm_props; | |
ffd5c8e4 | 2106 | my $svn_refspec = qr{\s*(.*?)\s*:\s*(.+?)\s*}; |
8b8fc068 | 2107 | foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) { |
6f5748e1 AB |
2108 | if (m!^(.+)\.fetch=$svn_refspec$!) { |
2109 | my ($remote, $local_ref, $remote_ref) = ($1, $2, $3); | |
2110 | die("svn-remote.$remote: remote ref '$remote_ref' " | |
2111 | . "must start with 'refs/'\n") | |
2112 | unless $remote_ref =~ m{^refs/}; | |
46cb16fb | 2113 | $local_ref = uri_decode($local_ref); |
46cf98ba | 2114 | $r->{$remote}->{fetch}->{$local_ref} = $remote_ref; |
63c56022 JA |
2115 | $r->{$remote}->{svm} = {} if $use_svm_props; |
2116 | } elsif (m!^(.+)\.usesvmprops=\s*(.*)\s*$!) { | |
2117 | $r->{$1}->{svm} = {}; | |
47e39c55 EW |
2118 | } elsif (m!^(.+)\.url=\s*(.*)\s*$!) { |
2119 | $r->{$1}->{url} = $2; | |
12a296bc AS |
2120 | } elsif (m!^(.+)\.pushurl=\s*(.*)\s*$!) { |
2121 | $r->{$1}->{pushurl} = $2; | |
6f5748e1 AB |
2122 | } elsif (m!^(.+)\.(branches|tags)=$svn_refspec$!) { |
2123 | my ($remote, $t, $local_ref, $remote_ref) = | |
2124 | ($1, $2, $3, $4); | |
2125 | die("svn-remote.$remote: remote ref '$remote_ref' ($t) " | |
2126 | . "must start with 'refs/'\n") | |
2127 | unless $remote_ref =~ m{^refs/}; | |
46cb16fb | 2128 | $local_ref = uri_decode($local_ref); |
62244069 | 2129 | my $rs = { |
6f5748e1 AB |
2130 | t => $t, |
2131 | remote => $remote, | |
07576208 JS |
2132 | path => Git::SVN::GlobSpec->new($local_ref, 1), |
2133 | ref => Git::SVN::GlobSpec->new($remote_ref, 0) }; | |
4bb9ed04 EW |
2134 | if (length($rs->{ref}->{right}) != 0) { |
2135 | die "The '*' glob character must be the last ", | |
6f5748e1 | 2136 | "character of '$remote_ref'\n"; |
4bb9ed04 | 2137 | } |
6f5748e1 | 2138 | push @{ $r->{$remote}->{$t} }, $rs; |
47e39c55 EW |
2139 | } |
2140 | } | |
63c56022 JA |
2141 | |
2142 | map { | |
2143 | if (defined $r->{$_}->{svm}) { | |
2144 | my $svm; | |
2145 | eval { | |
2146 | my $section = "svn-remote.$_"; | |
2147 | $svm = { | |
2148 | source => tmp_config('--get', | |
2149 | "$section.svm-source"), | |
2150 | replace => tmp_config('--get', | |
2151 | "$section.svm-replace"), | |
2152 | } | |
2153 | }; | |
2154 | $r->{$_}->{svm} = $svm; | |
2155 | } | |
2156 | } keys %$r; | |
2157 | ||
47e39c55 EW |
2158 | $r; |
2159 | } | |
2160 | ||
ecc712dd | 2161 | sub init_vars { |
94bc914c | 2162 | $_gc_nr = $_gc_period = 1000; |
af788a6e KW |
2163 | if (defined $_repack || defined $_repack_flags) { |
2164 | warn "Repack options are obsolete; they have no effect.\n"; | |
2165 | } | |
ecc712dd EW |
2166 | } |
2167 | ||
b805b44a | 2168 | sub verify_remotes_sanity { |
536c4b09 | 2169 | return unless -d $ENV{GIT_DIR}; |
b805b44a EW |
2170 | my %seen; |
2171 | foreach (command(qw/config -l/)) { | |
2172 | if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) { | |
2173 | if ($seen{$1}) { | |
2174 | die "Remote ref refs/remote/$1 is tracked by", | |
2175 | "\n \"$_\"\nand\n \"$seen{$1}\"\n", | |
2176 | "Please resolve this ambiguity in ", | |
2177 | "your git configuration file before ", | |
2178 | "continuing\n"; | |
2179 | } | |
2180 | $seen{$1} = $_; | |
2181 | } | |
2182 | } | |
2183 | } | |
2184 | ||
e6434f87 EW |
2185 | sub find_existing_remote { |
2186 | my ($url, $remotes) = @_; | |
befc9adc | 2187 | return undef if $no_reuse_existing; |
e6434f87 EW |
2188 | my $existing; |
2189 | foreach my $repo_id (keys %$remotes) { | |
2190 | my $u = $remotes->{$repo_id}->{url} or next; | |
2191 | next if $u ne $url; | |
2192 | $existing = $repo_id; | |
2193 | last; | |
2194 | } | |
2195 | $existing; | |
2196 | } | |
b805b44a | 2197 | |
e6434f87 | 2198 | sub init_remote_config { |
d8115c51 | 2199 | my ($self, $url, $no_write) = @_; |
e6434f87 EW |
2200 | $url =~ s!/+$!!; # strip trailing slash |
2201 | my $r = read_all_remotes(); | |
2202 | my $existing = find_existing_remote($url, $r); | |
2203 | if ($existing) { | |
e518192f EW |
2204 | unless ($no_write) { |
2205 | print STDERR "Using existing ", | |
2206 | "[svn-remote \"$existing\"]\n"; | |
2207 | } | |
e6434f87 | 2208 | $self->{repo_id} = $existing; |
4a1bb4c3 | 2209 | } elsif ($_minimize_url) { |
e6434f87 EW |
2210 | my $min_url = Git::SVN::Ra->new($url)->minimize_url; |
2211 | $existing = find_existing_remote($min_url, $r); | |
2212 | if ($existing) { | |
e518192f EW |
2213 | unless ($no_write) { |
2214 | print STDERR "Using existing ", | |
2215 | "[svn-remote \"$existing\"]\n"; | |
2216 | } | |
e6434f87 EW |
2217 | $self->{repo_id} = $existing; |
2218 | } | |
2219 | if ($min_url ne $url) { | |
e518192f EW |
2220 | unless ($no_write) { |
2221 | print STDERR "Using higher level of URL: ", | |
2222 | "$url => $min_url\n"; | |
2223 | } | |
e6434f87 EW |
2224 | my $old_path = $self->{path}; |
2225 | $self->{path} = $url; | |
4e9f6cc7 | 2226 | $self->{path} =~ s!^\Q$min_url\E(/|$)!!; |
e6434f87 EW |
2227 | if (length $old_path) { |
2228 | $self->{path} .= "/$old_path"; | |
2229 | } | |
2230 | $url = $min_url; | |
2231 | } | |
2232 | } | |
2233 | my $orig_url; | |
2234 | if (!$existing) { | |
b805b44a | 2235 | # verify that we aren't overwriting anything: |
e6434f87 | 2236 | $orig_url = eval { |
706587fc | 2237 | command_oneline('config', '--get', |
e6434f87 | 2238 | "svn-remote.$self->{repo_id}.url") |
706587fc | 2239 | }; |
b805b44a | 2240 | if ($orig_url && ($orig_url ne $url)) { |
e6434f87 | 2241 | die "svn-remote.$self->{repo_id}.url already set: ", |
b805b44a EW |
2242 | "$orig_url\nwanted to set to: $url\n"; |
2243 | } | |
9b981fc6 | 2244 | } |
e6434f87 | 2245 | my ($xrepo_id, $xpath) = find_ref($self->refname); |
6f5748e1 | 2246 | if (!$no_write && defined $xpath) { |
e6434f87 | 2247 | die "svn-remote.$xrepo_id.fetch already set to track ", |
6f5748e1 | 2248 | "$xpath:", $self->refname, "\n"; |
e6434f87 | 2249 | } |
d8115c51 EW |
2250 | unless ($no_write) { |
2251 | command_noisy('config', | |
2252 | "svn-remote.$self->{repo_id}.url", $url); | |
46cf98ba | 2253 | $self->{path} =~ s{^/}{}; |
5268f9ed | 2254 | $self->{path} =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg; |
d8115c51 EW |
2255 | command_noisy('config', '--add', |
2256 | "svn-remote.$self->{repo_id}.fetch", | |
2257 | "$self->{path}:".$self->refname); | |
2258 | } | |
9b981fc6 | 2259 | $self->{url} = $url; |
e6434f87 EW |
2260 | } |
2261 | ||
a8ae2623 EW |
2262 | sub find_by_url { # repos_root and, path are optional |
2263 | my ($class, $full_url, $repos_root, $path) = @_; | |
56973d20 | 2264 | |
1a97a506 | 2265 | return undef unless defined $full_url; |
56973d20 AR |
2266 | remove_username($full_url); |
2267 | remove_username($repos_root) if defined $repos_root; | |
a8ae2623 EW |
2268 | my $remotes = read_all_remotes(); |
2269 | if (defined $full_url && defined $repos_root && !defined $path) { | |
2270 | $path = $full_url; | |
2271 | $path =~ s#^\Q$repos_root\E(?:/|$)##; | |
2272 | } | |
2273 | foreach my $repo_id (keys %$remotes) { | |
2274 | my $u = $remotes->{$repo_id}->{url} or next; | |
56973d20 | 2275 | remove_username($u); |
a8ae2623 EW |
2276 | next if defined $repos_root && $repos_root ne $u; |
2277 | ||
2278 | my $fetch = $remotes->{$repo_id}->{fetch} || {}; | |
62244069 MB |
2279 | foreach my $t (qw/branches tags/) { |
2280 | foreach my $globspec (@{$remotes->{$repo_id}->{$t}}) { | |
2281 | resolve_local_globs($u, $fetch, $globspec); | |
2282 | } | |
a8ae2623 EW |
2283 | } |
2284 | my $p = $path; | |
0bb91d9a | 2285 | my $rwr = rewrite_root({repo_id => $repo_id}); |
63c56022 JA |
2286 | my $svm = $remotes->{$repo_id}->{svm} |
2287 | if defined $remotes->{$repo_id}->{svm}; | |
a8ae2623 EW |
2288 | unless (defined $p) { |
2289 | $p = $full_url; | |
0bb91d9a | 2290 | my $z = $u; |
63c56022 | 2291 | my $prefix = ''; |
0bb91d9a JG |
2292 | if ($rwr) { |
2293 | $z = $rwr; | |
1b7e543a | 2294 | remove_username($z); |
63c56022 JA |
2295 | } elsif (defined $svm) { |
2296 | $z = $svm->{source}; | |
2297 | $prefix = $svm->{replace}; | |
2298 | $prefix =~ s#^\Q$u\E(?:/|$)##; | |
2299 | $prefix =~ s#/$##; | |
0bb91d9a | 2300 | } |
63c56022 | 2301 | $p =~ s#^\Q$z\E(?:/|$)#$prefix# or next; |
a8ae2623 EW |
2302 | } |
2303 | foreach my $f (keys %$fetch) { | |
2304 | next if $f ne $p; | |
2305 | return Git::SVN->new($fetch->{$f}, $repo_id, $f); | |
2306 | } | |
2307 | } | |
2308 | undef; | |
2309 | } | |
2310 | ||
e6434f87 | 2311 | sub init { |
d8115c51 | 2312 | my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_; |
e6434f87 EW |
2313 | my $self = _new($class, $repo_id, $ref_id, $path); |
2314 | if (defined $url) { | |
d8115c51 | 2315 | $self->init_remote_config($url, $no_write); |
e6434f87 | 2316 | } |
9b981fc6 EW |
2317 | $self; |
2318 | } | |
2319 | ||
706587fc EW |
2320 | sub find_ref { |
2321 | my ($ref_id) = @_; | |
2322 | foreach (command(qw/config -l/)) { | |
2323 | next unless m!^svn-remote\.(.+)\.fetch= | |
ffd5c8e4 | 2324 | \s*(.*?)\s*:\s*(.+?)\s*$!x; |
706587fc EW |
2325 | my ($repo_id, $path, $ref) = ($1, $2, $3); |
2326 | if ($ref eq $ref_id) { | |
2327 | $path = '' if ($path =~ m#^\./?#); | |
2328 | return ($repo_id, $path); | |
2329 | } | |
2330 | } | |
2331 | (undef, undef, undef); | |
2332 | } | |
2333 | ||
9b981fc6 | 2334 | sub new { |
706587fc EW |
2335 | my ($class, $ref_id, $repo_id, $path) = @_; |
2336 | if (defined $ref_id && !defined $repo_id && !defined $path) { | |
2337 | ($repo_id, $path) = find_ref($ref_id); | |
2338 | if (!defined $repo_id) { | |
2339 | die "Could not find a \"svn-remote.*.fetch\" key ", | |
2340 | "in the repository configuration matching: ", | |
6f5748e1 | 2341 | "$ref_id\n"; |
706587fc EW |
2342 | } |
2343 | } | |
2344 | my $self = _new($class, $repo_id, $ref_id, $path); | |
8b8fc068 EW |
2345 | if (!defined $self->{path} || !length $self->{path}) { |
2346 | my $fetch = command_oneline('config', '--get', | |
2347 | "svn-remote.$repo_id.fetch", | |
6f5748e1 | 2348 | ":$ref_id\$") or |
8b8fc068 | 2349 | die "Failed to read \"svn-remote.$repo_id.fetch\" ", |
6f5748e1 | 2350 | "\":$ref_id\$\" in config\n"; |
8b8fc068 EW |
2351 | ($self->{path}, undef) = split(/\s*:\s*/, $fetch); |
2352 | } | |
b1a954a3 EW |
2353 | $self->{path} =~ s{/+}{/}g; |
2354 | $self->{path} =~ s{\A/}{}; | |
2355 | $self->{path} =~ s{/\z}{}; | |
706587fc EW |
2356 | $self->{url} = command_oneline('config', '--get', |
2357 | "svn-remote.$repo_id.url") or | |
2358 | die "Failed to read \"svn-remote.$repo_id.url\" in config\n"; | |
12a296bc AS |
2359 | $self->{pushurl} = eval { command_oneline('config', '--get', |
2360 | "svn-remote.$repo_id.pushurl") }; | |
d6d3346b | 2361 | $self->rebuild; |
9b981fc6 EW |
2362 | $self; |
2363 | } | |
2364 | ||
bf655fd7 | 2365 | sub refname { |
6f5748e1 | 2366 | my ($refname) = $_[0]->{ref_id} ; |
bf655fd7 RE |
2367 | |
2368 | # It cannot end with a slash /, we'll throw up on this because | |
2369 | # SVN can't have directories with a slash in their name, either: | |
2370 | if ($refname =~ m{/$}) { | |
2371 | die "ref: '$refname' ends with a trailing slash, this is ", | |
2372 | "not permitted by git nor Subversion\n"; | |
2373 | } | |
2374 | ||
2375 | # It cannot have ASCII control character space, tilde ~, caret ^, | |
2376 | # colon :, question-mark ?, asterisk *, space, or open bracket [ | |
2377 | # anywhere. | |
2378 | # | |
2379 | # Additionally, % must be escaped because it is used for escaping | |
2380 | # and we want our escaped refname to be reversible | |
2381 | $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg; | |
2382 | ||
2383 | # no slash-separated component can begin with a dot . | |
2384 | # /.* becomes /%2E* | |
2385 | $refname =~ s{/\.}{/%2E}g; | |
2386 | ||
2387 | # It cannot have two consecutive dots .. anywhere | |
2388 | # .. becomes %2E%2E | |
2389 | $refname =~ s{\.\.}{%2E%2E}g; | |
2390 | ||
73d41955 TS |
2391 | # trailing dots and .lock are not allowed |
2392 | # .$ becomes %2E and .lock becomes %2Elock | |
2393 | $refname =~ s{\.(?=$|lock$)}{%2E}; | |
2394 | ||
2395 | # the sequence @{ is used to access the reflog | |
2396 | # @{ becomes %40{ | |
2397 | $refname =~ s{\@\{}{%40\{}g; | |
2398 | ||
bf655fd7 RE |
2399 | return $refname; |
2400 | } | |
2401 | ||
2402 | sub desanitize_refname { | |
2403 | my ($refname) = @_; | |
2404 | $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg; | |
2405 | return $refname; | |
2406 | } | |
9b981fc6 | 2407 | |
26a62d57 EW |
2408 | sub svm_uuid { |
2409 | my ($self) = @_; | |
2410 | return $self->{svm}->{uuid} if $self->svm; | |
2411 | $self->ra; | |
2412 | unless ($self->{svm}) { | |
2413 | die "SVM UUID not cached, and reading remotely failed\n"; | |
2414 | } | |
2415 | $self->{svm}->{uuid}; | |
2416 | } | |
8a49ee97 | 2417 | |
26a62d57 EW |
2418 | sub svm { |
2419 | my ($self) = @_; | |
2420 | return $self->{svm} if $self->{svm}; | |
2421 | my $svm; | |
8a49ee97 EW |
2422 | # see if we have it in our config, first: |
2423 | eval { | |
26a62d57 EW |
2424 | my $section = "svn-remote.$self->{repo_id}"; |
2425 | $svm = { | |
93f2689c EW |
2426 | source => tmp_config('--get', "$section.svm-source"), |
2427 | uuid => tmp_config('--get', "$section.svm-uuid"), | |
befc9adc | 2428 | replace => tmp_config('--get', "$section.svm-replace"), |
8a49ee97 EW |
2429 | } |
2430 | }; | |
befc9adc EW |
2431 | if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) { |
2432 | $self->{svm} = $svm; | |
2433 | } | |
26a62d57 EW |
2434 | $self->{svm}; |
2435 | } | |
2436 | ||
2437 | sub _set_svm_vars { | |
2438 | my ($self, $ra) = @_; | |
db03cd24 EW |
2439 | return $ra if $self->svm; |
2440 | ||
2441 | my @err = ( "useSvmProps set, but failed to read SVM properties\n", | |
befc9adc | 2442 | "(svm:source, svm:uuid) ", |
db03cd24 EW |
2443 | "from the following URLs:\n" ); |
2444 | sub read_svm_props { | |
befc9adc EW |
2445 | my ($self, $ra, $path, $r) = @_; |
2446 | my $props = ($ra->get_dir($path, $r))[2]; | |
db03cd24 | 2447 | my $src = $props->{'svm:source'}; |
db03cd24 | 2448 | my $uuid = $props->{'svm:uuid'}; |
befc9adc | 2449 | return undef if (!$src || !$uuid); |
26a62d57 | 2450 | |
befc9adc | 2451 | chomp($src, $uuid); |
26a62d57 | 2452 | |
b3e95936 | 2453 | $uuid =~ m{^[0-9a-f\-]{30,}$}i |
db03cd24 | 2454 | or die "doesn't look right - svm:uuid is '$uuid'\n"; |
befc9adc EW |
2455 | |
2456 | # the '!' is used to mark the repos_root!/relative/path | |
2457 | $src =~ s{/?!/?}{/}; | |
db03cd24 | 2458 | $src =~ s{/+$}{}; # no trailing slashes please |
befc9adc | 2459 | # username is of no interest |
8a49ee97 | 2460 | $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1}; |
8a49ee97 | 2461 | |
befc9adc EW |
2462 | my $replace = $ra->{url}; |
2463 | $replace .= "/$path" if length $path; | |
2464 | ||
db03cd24 | 2465 | my $section = "svn-remote.$self->{repo_id}"; |
befc9adc EW |
2466 | tmp_config("$section.svm-source", $src); |
2467 | tmp_config("$section.svm-replace", $replace); | |
2468 | tmp_config("$section.svm-uuid", $uuid); | |
2469 | $self->{svm} = { | |
2470 | source => $src, | |
2471 | uuid => $uuid, | |
2472 | replace => $replace | |
2473 | }; | |
db03cd24 EW |
2474 | } |
2475 | ||
2476 | my $r = $ra->get_latest_revnum; | |
2477 | my $path = $self->{path}; | |
befc9adc | 2478 | my %tried; |
db03cd24 | 2479 | while (length $path) { |
befc9adc EW |
2480 | unless ($tried{"$self->{url}/$path"}) { |
2481 | return $ra if $self->read_svm_props($ra, $path, $r); | |
2482 | $tried{"$self->{url}/$path"} = 1; | |
db03cd24 | 2483 | } |
befc9adc | 2484 | $path =~ s#/?[^/]+$##; |
8a49ee97 | 2485 | } |
befc9adc EW |
2486 | die "Path: '$path' should be ''\n" if $path ne ''; |
2487 | return $ra if $self->read_svm_props($ra, $path, $r); | |
2488 | $tried{"$self->{url}/$path"} = 1; | |
db03cd24 EW |
2489 | |
2490 | if ($ra->{repos_root} eq $self->{url}) { | |
befc9adc | 2491 | die @err, (map { " $_\n" } keys %tried), "\n"; |
db03cd24 EW |
2492 | } |
2493 | ||
2494 | # nope, make sure we're connected to the repository root: | |
2495 | my $ok; | |
2496 | my @tried_b; | |
2497 | $path = $ra->{svn_path}; | |
db03cd24 EW |
2498 | $ra = Git::SVN::Ra->new($ra->{repos_root}); |
2499 | while (length $path) { | |
befc9adc EW |
2500 | unless ($tried{"$ra->{url}/$path"}) { |
2501 | $ok = $self->read_svm_props($ra, $path, $r); | |
2502 | last if $ok; | |
2503 | $tried{"$ra->{url}/$path"} = 1; | |
2504 | } | |
2505 | $path =~ s#/?[^/]+$##; | |
db03cd24 | 2506 | } |
befc9adc EW |
2507 | die "Path: '$path' should be ''\n" if $path ne ''; |
2508 | $ok ||= $self->read_svm_props($ra, $path, $r); | |
2509 | $tried{"$ra->{url}/$path"} = 1; | |
db03cd24 | 2510 | if (!$ok) { |
befc9adc | 2511 | die @err, (map { " $_\n" } keys %tried), "\n"; |
db03cd24 EW |
2512 | } |
2513 | Git::SVN::Ra->new($self->{url}); | |
8a49ee97 EW |
2514 | } |
2515 | ||
62e349d2 EW |
2516 | sub svnsync { |
2517 | my ($self) = @_; | |
2518 | return $self->{svnsync} if $self->{svnsync}; | |
2519 | ||
2520 | if ($self->no_metadata) { | |
2521 | die "Can't have both 'noMetadata' and ", | |
2522 | "'useSvnsyncProps' options set!\n"; | |
2523 | } | |
2524 | if ($self->rewrite_root) { | |
2525 | die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ", | |
2526 | "options set!\n"; | |
2527 | } | |
3e18ce1a JS |
2528 | if ($self->rewrite_uuid) { |
2529 | die "Can't have both 'useSvnsyncProps' and 'rewriteUUID' ", | |
2530 | "options set!\n"; | |
2531 | } | |
62e349d2 EW |
2532 | |
2533 | my $svnsync; | |
2534 | # see if we have it in our config, first: | |
2535 | eval { | |
2536 | my $section = "svn-remote.$self->{repo_id}"; | |
98fa5b68 EW |
2537 | |
2538 | my $url = tmp_config('--get', "$section.svnsync-url"); | |
2539 | ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or | |
2540 | die "doesn't look right - svn:sync-from-url is '$url'\n"; | |
2541 | ||
2542 | my $uuid = tmp_config('--get', "$section.svnsync-uuid"); | |
b3e95936 | 2543 | ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or |
98fa5b68 EW |
2544 | die "doesn't look right - svn:sync-from-uuid is '$uuid'\n"; |
2545 | ||
2546 | $svnsync = { url => $url, uuid => $uuid } | |
62e349d2 EW |
2547 | }; |
2548 | if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) { | |
2549 | return $self->{svnsync} = $svnsync; | |
2550 | } | |
2551 | ||
2552 | my $err = "useSvnsyncProps set, but failed to read " . | |
2553 | "svnsync property: svn:sync-from-"; | |
2554 | my $rp = $self->ra->rev_proplist(0); | |
2555 | ||
2556 | my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n"; | |
98fa5b68 | 2557 | ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or |
62e349d2 EW |
2558 | die "doesn't look right - svn:sync-from-url is '$url'\n"; |
2559 | ||
2560 | my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n"; | |
b3e95936 | 2561 | ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or |
62e349d2 EW |
2562 | die "doesn't look right - svn:sync-from-uuid is '$uuid'\n"; |
2563 | ||
2564 | my $section = "svn-remote.$self->{repo_id}"; | |
2565 | tmp_config('--add', "$section.svnsync-uuid", $uuid); | |
2566 | tmp_config('--add', "$section.svnsync-url", $url); | |
2567 | return $self->{svnsync} = { url => $url, uuid => $uuid }; | |
2568 | } | |
2569 | ||
26a62d57 EW |
2570 | # this allows us to memoize our SVN::Ra UUID locally and avoid a |
2571 | # remote lookup (useful for 'git svn log'). | |
2572 | sub ra_uuid { | |
2573 | my ($self) = @_; | |
2574 | unless ($self->{ra_uuid}) { | |
2575 | my $key = "svn-remote.$self->{repo_id}.uuid"; | |
2576 | my $uuid = eval { tmp_config('--get', $key) }; | |
b3e95936 | 2577 | if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/i) { |
26a62d57 EW |
2578 | $self->{ra_uuid} = $uuid; |
2579 | } else { | |
2580 | die "ra_uuid called without URL\n" unless $self->{url}; | |
2581 | $self->{ra_uuid} = $self->ra->get_uuid; | |
2582 | tmp_config('--add', $key, $self->{ra_uuid}); | |
2583 | } | |
2584 | } | |
2585 | $self->{ra_uuid}; | |
2586 | } | |
2587 | ||
a5460eb7 EW |
2588 | sub _set_repos_root { |
2589 | my ($self, $repos_root) = @_; | |
2590 | my $k = "svn-remote.$self->{repo_id}.reposRoot"; | |
2591 | $repos_root ||= $self->ra->{repos_root}; | |
2592 | tmp_config($k, $repos_root); | |
2593 | $repos_root; | |
2594 | } | |
2595 | ||
2596 | sub repos_root { | |
2597 | my ($self) = @_; | |
2598 | my $k = "svn-remote.$self->{repo_id}.reposRoot"; | |
2599 | eval { tmp_config('--get', $k) } || $self->_set_repos_root; | |
2600 | } | |
2601 | ||
9b981fc6 EW |
2602 | sub ra { |
2603 | my ($self) = shift; | |
8a49ee97 | 2604 | my $ra = Git::SVN::Ra->new($self->{url}); |
a5460eb7 | 2605 | $self->_set_repos_root($ra->{repos_root}); |
91b03282 EW |
2606 | if ($self->use_svm_props && !$self->{svm}) { |
2607 | if ($self->no_metadata) { | |
97ae0911 EW |
2608 | die "Can't have both 'noMetadata' and ", |
2609 | "'useSvmProps' options set!\n"; | |
62e349d2 EW |
2610 | } elsif ($self->use_svnsync_props) { |
2611 | die "Can't have both 'useSvnsyncProps' and ", | |
2612 | "'useSvmProps' options set!\n"; | |
91b03282 | 2613 | } |
26a62d57 | 2614 | $ra = $self->_set_svm_vars($ra); |
8a49ee97 EW |
2615 | $self->{-want_revprops} = 1; |
2616 | } | |
2617 | $ra; | |
9b981fc6 EW |
2618 | } |
2619 | ||
01bdab84 BS |
2620 | # prop_walk(PATH, REV, SUB) |
2621 | # ------------------------- | |
2622 | # Recursively traverse PATH at revision REV and invoke SUB for each | |
2623 | # directory that contains a SVN property. SUB will be invoked as | |
2624 | # follows: &SUB(gs, path, props); where `gs' is this instance of | |
2625 | # Git::SVN, `path' the path to the directory where the properties | |
2626 | # `props' were found. The `path' will be relative to point of checkout, | |
2627 | # that is, if url://repo/trunk is the current Git branch, and that | |
2628 | # directory contains a sub-directory `d', SUB will be invoked with `/d/' | |
2629 | # as `path' (note the trailing `/'). | |
2630 | sub prop_walk { | |
2631 | my ($self, $path, $rev, $sub) = @_; | |
2632 | ||
35cda061 | 2633 | $path =~ s#^/##; |
01bdab84 BS |
2634 | my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev); |
2635 | $path =~ s#^/*#/#g; | |
9b981fc6 | 2636 | my $p = $path; |
01bdab84 BS |
2637 | # Strip the irrelevant part of the path. |
2638 | $p =~ s#^/+\Q$self->{path}\E(/|$)#/#; | |
2639 | # Ensure the path is terminated by a `/'. | |
2640 | $p =~ s#/*$#/#; | |
2641 | ||
2642 | # The properties contain all the internal SVN stuff nobody | |
2643 | # (usually) cares about. | |
2644 | my $interesting_props = 0; | |
2645 | foreach (keys %{$props}) { | |
2646 | # If it doesn't start with `svn:', it must be a | |
2647 | # user-defined property. | |
2648 | ++$interesting_props and next if $_ !~ /^svn:/; | |
2649 | # FIXME: Fragile, if SVN adds new public properties, | |
2650 | # this needs to be updated. | |
2651 | ++$interesting_props if /^svn:(?:ignore|keywords|executable | |
2652 | |eol-style|mime-type | |
2653 | |externals|needs-lock)$/x; | |
2654 | } | |
2655 | &$sub($self, $p, $props) if $interesting_props; | |
2656 | ||
9b981fc6 | 2657 | foreach (sort keys %$dirent) { |
0dc03d6a | 2658 | next if $dirent->{$_}->{kind} != $SVN::Node::dir; |
b7166cce | 2659 | $self->prop_walk($self->{path} . $p . $_, $rev, $sub); |
9b981fc6 EW |
2660 | } |
2661 | } | |
2662 | ||
3ebe8df7 EW |
2663 | sub last_rev { ($_[0]->last_rev_commit)[0] } |
2664 | sub last_commit { ($_[0]->last_rev_commit)[1] } | |
2665 | ||
9b981fc6 EW |
2666 | # returns the newest SVN revision number and newest commit SHA1 |
2667 | sub last_rev_commit { | |
2668 | my ($self) = @_; | |
2669 | if (defined $self->{last_rev} && defined $self->{last_commit}) { | |
2670 | return ($self->{last_rev}, $self->{last_commit}); | |
2671 | } | |
d2866f9e | 2672 | my $c = ::verify_ref($self->refname.'^0'); |
91b03282 | 2673 | if ($c && !$self->use_svm_props && !$self->no_metadata) { |
d2866f9e | 2674 | my $rev = (::cmt_metadata($c))[1]; |
9b981fc6 EW |
2675 | if (defined $rev) { |
2676 | ($self->{last_rev}, $self->{last_commit}) = ($rev, $c); | |
2677 | return ($rev, $c); | |
2678 | } | |
2679 | } | |
060610c5 EW |
2680 | my $map_path = $self->map_path; |
2681 | unless (-e $map_path) { | |
26a62d57 EW |
2682 | ($self->{last_rev}, $self->{last_commit}) = (undef, undef); |
2683 | return (undef, undef); | |
2684 | } | |
66ab84b9 | 2685 | my ($rev, $commit) = $self->rev_map_max(1); |
060610c5 EW |
2686 | ($self->{last_rev}, $self->{last_commit}) = ($rev, $commit); |
2687 | return ($rev, $commit); | |
9b981fc6 EW |
2688 | } |
2689 | ||
3ebe8df7 EW |
2690 | sub get_fetch_range { |
2691 | my ($self, $min, $max) = @_; | |
2692 | $max ||= $self->ra->get_latest_revnum; | |
060610c5 | 2693 | $min ||= $self->rev_map_max; |
3ebe8df7 | 2694 | (++$min, $max); |
9b981fc6 EW |
2695 | } |
2696 | ||
8a49ee97 | 2697 | sub tmp_config { |
93f2689c | 2698 | my (@args) = @_; |
b7e5348c EW |
2699 | my $old_def_config = "$ENV{GIT_DIR}/svn/config"; |
2700 | my $config = "$ENV{GIT_DIR}/svn/.metadata"; | |
38570a47 | 2701 | if (! -f $config && -f $old_def_config) { |
b7e5348c EW |
2702 | rename $old_def_config, $config or |
2703 | die "Failed rename $old_def_config => $config: $!\n"; | |
2704 | } | |
8a49ee97 | 2705 | my $old_config = $ENV{GIT_CONFIG}; |
93f2689c | 2706 | $ENV{GIT_CONFIG} = $config; |
8a49ee97 | 2707 | $@ = undef; |
b4d57e5e EW |
2708 | my @ret = eval { |
2709 | unless (-f $config) { | |
2710 | mkfile($config); | |
2711 | open my $fh, '>', $config or | |
2712 | die "Can't open $config: $!\n"; | |
2713 | print $fh "; This file is used internally by ", | |
2714 | "git-svn\n" or die | |
2715 | "Couldn't write to $config: $!\n"; | |
2716 | print $fh "; You should not have to edit it\n" or | |
2717 | die "Couldn't write to $config: $!\n"; | |
2718 | close $fh or die "Couldn't close $config: $!\n"; | |
2719 | } | |
2720 | command('config', @args); | |
2721 | }; | |
8a49ee97 EW |
2722 | my $err = $@; |
2723 | if (defined $old_config) { | |
2724 | $ENV{GIT_CONFIG} = $old_config; | |
2725 | } else { | |
2726 | delete $ENV{GIT_CONFIG}; | |
2727 | } | |
2728 | die $err if $err; | |
2729 | wantarray ? @ret : $ret[0]; | |
2730 | } | |
2731 | ||
9b981fc6 EW |
2732 | sub tmp_index_do { |
2733 | my ($self, $sub) = @_; | |
2734 | my $old_index = $ENV{GIT_INDEX_FILE}; | |
2735 | $ENV{GIT_INDEX_FILE} = $self->{index}; | |
8a49ee97 | 2736 | $@ = undef; |
b4d57e5e EW |
2737 | my @ret = eval { |
2738 | my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#); | |
2739 | mkpath([$dir]) unless -d $dir; | |
2740 | &$sub; | |
2741 | }; | |
8a49ee97 EW |
2742 | my $err = $@; |
2743 | if (defined $old_index) { | |
9b981fc6 EW |
2744 | $ENV{GIT_INDEX_FILE} = $old_index; |
2745 | } else { | |
2746 | delete $ENV{GIT_INDEX_FILE}; | |
2747 | } | |
8a49ee97 | 2748 | die $err if $err; |
9b981fc6 EW |
2749 | wantarray ? @ret : $ret[0]; |
2750 | } | |
2751 | ||
2752 | sub assert_index_clean { | |
2753 | my ($self, $treeish) = @_; | |
2754 | ||
2755 | $self->tmp_index_do(sub { | |
2756 | command_noisy('read-tree', $treeish) unless -e $self->{index}; | |
2757 | my $x = command_oneline('write-tree'); | |
2758 | my ($y) = (command(qw/cat-file commit/, $treeish) =~ | |
2759 | /^tree ($::sha1)/mo); | |
e8d120bd EW |
2760 | return if $y eq $x; |
2761 | ||
2762 | warn "Index mismatch: $y != $x\nrereading $treeish\n"; | |
2763 | unlink $self->{index} or die "unlink $self->{index}: $!\n"; | |
2764 | command_noisy('read-tree', $treeish); | |
9b981fc6 EW |
2765 | $x = command_oneline('write-tree'); |
2766 | if ($y ne $x) { | |
2767 | ::fatal "trees ($treeish) $y != $x\n", | |
207f1a75 | 2768 | "Something is seriously wrong..."; |
9b981fc6 EW |
2769 | } |
2770 | }); | |
2771 | } | |
2772 | ||
2773 | sub get_commit_parents { | |
0af9c9f9 | 2774 | my ($self, $log_entry) = @_; |
9b981fc6 | 2775 | my (%seen, @ret, @tmp); |
0af9c9f9 EW |
2776 | # legacy support for 'set-tree'; this is only used by set_tree_cb: |
2777 | if (my $ip = $self->{inject_parents}) { | |
2778 | if (my $commit = delete $ip->{$log_entry->{revision}}) { | |
2779 | push @tmp, $commit; | |
9b981fc6 EW |
2780 | } |
2781 | } | |
d2866f9e | 2782 | if (my $cur = ::verify_ref($self->refname.'^0')) { |
9b981fc6 EW |
2783 | push @tmp, $cur; |
2784 | } | |
733a65aa EW |
2785 | if (my $ipd = $self->{inject_parents_dcommit}) { |
2786 | if (my $commit = delete $ipd->{$log_entry->{revision}}) { | |
2787 | push @tmp, @$commit; | |
2788 | } | |
2789 | } | |
44320b9e | 2790 | push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp); |
9b981fc6 EW |
2791 | while (my $p = shift @tmp) { |
2792 | next if $seen{$p}; | |
2793 | $seen{$p} = 1; | |
2794 | push @ret, $p; | |
9b981fc6 EW |
2795 | } |
2796 | @ret; | |
2797 | } | |
2798 | ||
aea736cc EW |
2799 | sub rewrite_root { |
2800 | my ($self) = @_; | |
2801 | return $self->{-rewrite_root} if exists $self->{-rewrite_root}; | |
2802 | my $k = "svn-remote.$self->{repo_id}.rewriteRoot"; | |
2803 | my $rwr = eval { command_oneline(qw/config --get/, $k) }; | |
2804 | if ($rwr) { | |
2805 | $rwr =~ s#/+$##; | |
2806 | if ($rwr !~ m#^[a-z\+]+://#) { | |
2807 | die "$rwr is not a valid URL (key: $k)\n"; | |
2808 | } | |
2809 | } | |
2810 | $self->{-rewrite_root} = $rwr; | |
2811 | } | |
2812 | ||
3e18ce1a JS |
2813 | sub rewrite_uuid { |
2814 | my ($self) = @_; | |
2815 | return $self->{-rewrite_uuid} if exists $self->{-rewrite_uuid}; | |
2816 | my $k = "svn-remote.$self->{repo_id}.rewriteUUID"; | |
2817 | my $rwid = eval { command_oneline(qw/config --get/, $k) }; | |
2818 | if ($rwid) { | |
2819 | $rwid =~ s#/+$##; | |
2820 | if ($rwid !~ m#^[a-f0-9]{8}-(?:[a-f0-9]{4}-){3}[a-f0-9]{12}$#) { | |
2821 | die "$rwid is not a valid UUID (key: $k)\n"; | |
2822 | } | |
2823 | } | |
2824 | $self->{-rewrite_uuid} = $rwid; | |
2825 | } | |
2826 | ||
aea736cc EW |
2827 | sub metadata_url { |
2828 | my ($self) = @_; | |
2829 | ($self->rewrite_root || $self->{url}) . | |
2830 | (length $self->{path} ? '/' . $self->{path} : ''); | |
2831 | } | |
2832 | ||
706587fc | 2833 | sub full_url { |
9b981fc6 | 2834 | my ($self) = @_; |
5d3b7cd5 | 2835 | $self->{url} . (length $self->{path} ? '/' . $self->{path} : ''); |
9b981fc6 EW |
2836 | } |
2837 | ||
12a296bc AS |
2838 | sub full_pushurl { |
2839 | my ($self) = @_; | |
2840 | if ($self->{pushurl}) { | |
2841 | return $self->{pushurl} . (length $self->{path} ? '/' . | |
2842 | $self->{path} : ''); | |
2843 | } else { | |
2844 | return $self->full_url; | |
2845 | } | |
2846 | } | |
ad94802a EW |
2847 | |
2848 | sub set_commit_header_env { | |
2849 | my ($log_entry) = @_; | |
2850 | my %env; | |
2851 | foreach my $ned (qw/NAME EMAIL DATE/) { | |
2852 | foreach my $ac (qw/AUTHOR COMMITTER/) { | |
2853 | $env{"GIT_${ac}_${ned}"} = $ENV{"GIT_${ac}_${ned}"}; | |
2854 | } | |
9b981fc6 | 2855 | } |
ad94802a | 2856 | |
70ae04e4 AW |
2857 | $ENV{GIT_AUTHOR_NAME} = $log_entry->{name}; |
2858 | $ENV{GIT_AUTHOR_EMAIL} = $log_entry->{email}; | |
44320b9e | 2859 | $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date}; |
9b981fc6 | 2860 | |
70ae04e4 AW |
2861 | $ENV{GIT_COMMITTER_NAME} = (defined $log_entry->{commit_name}) |
2862 | ? $log_entry->{commit_name} | |
2863 | : $log_entry->{name}; | |
2864 | $ENV{GIT_COMMITTER_EMAIL} = (defined $log_entry->{commit_email}) | |
2865 | ? $log_entry->{commit_email} | |
2866 | : $log_entry->{email}; | |
ad94802a EW |
2867 | \%env; |
2868 | } | |
70ae04e4 | 2869 | |
ad94802a EW |
2870 | sub restore_commit_header_env { |
2871 | my ($env) = @_; | |
2872 | foreach my $ned (qw/NAME EMAIL DATE/) { | |
2873 | foreach my $ac (qw/AUTHOR COMMITTER/) { | |
2874 | my $k = "GIT_${ac}_${ned}"; | |
2875 | if (defined $env->{$k}) { | |
2876 | $ENV{$k} = $env->{$k}; | |
2877 | } else { | |
2878 | delete $ENV{$k}; | |
2879 | } | |
2880 | } | |
2881 | } | |
2882 | } | |
2883 | ||
94bc914c KW |
2884 | sub gc { |
2885 | command_noisy('gc', '--auto'); | |
2886 | }; | |
2887 | ||
ad94802a EW |
2888 | sub do_git_commit { |
2889 | my ($self, $log_entry) = @_; | |
2890 | my $lr = $self->last_rev; | |
2891 | if (defined $lr && $lr >= $log_entry->{revision}) { | |
2892 | die "Last fetched revision of ", $self->refname, | |
2893 | " was r$lr, but we are about to fetch: ", | |
2894 | "r$log_entry->{revision}!\n"; | |
2895 | } | |
2896 | if (my $c = $self->rev_map_get($log_entry->{revision})) { | |
2897 | croak "$log_entry->{revision} = $c already exists! ", | |
2898 | "Why are we refetching it?\n"; | |
2899 | } | |
2900 | my $old_env = set_commit_header_env($log_entry); | |
44320b9e | 2901 | my $tree = $log_entry->{tree}; |
9b981fc6 EW |
2902 | if (!defined $tree) { |
2903 | $tree = $self->tmp_index_do(sub { | |
2904 | command_oneline('write-tree') }); | |
2905 | } | |
2906 | die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o; | |
2907 | ||
e855bfc0 | 2908 | my @exec = ('git', 'commit-tree', $tree); |
0af9c9f9 | 2909 | foreach ($self->get_commit_parents($log_entry)) { |
9b981fc6 EW |
2910 | push @exec, '-p', $_; |
2911 | } | |
2912 | defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec)) | |
2913 | or croak $!; | |
16fc08e2 EW |
2914 | binmode $msg_fh; |
2915 | ||
2916 | # we always get UTF-8 from SVN, but we may want our commits in | |
2917 | # a different encoding. | |
2918 | if (my $enc = Git::config('i18n.commitencoding')) { | |
2919 | require Encode; | |
2920 | Encode::from_to($log_entry->{log}, 'UTF-8', $enc); | |
2921 | } | |
44320b9e | 2922 | print $msg_fh $log_entry->{log} or croak $!; |
ad94802a | 2923 | restore_commit_header_env($old_env); |
91b03282 | 2924 | unless ($self->no_metadata) { |
8a49ee97 EW |
2925 | print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n" |
2926 | or croak $!; | |
9760adcc | 2927 | } |
9b981fc6 EW |
2928 | $msg_fh->flush == 0 or croak $!; |
2929 | close $msg_fh or croak $!; | |
2930 | chomp(my $commit = do { local $/; <$out_fh> }); | |
2931 | close $out_fh or croak $!; | |
2932 | waitpid $pid, 0; | |
2933 | croak $? if $?; | |
2934 | if ($commit !~ /^$::sha1$/o) { | |
2935 | die "Failed to commit, invalid sha1: $commit\n"; | |
2936 | } | |
2937 | ||
060610c5 | 2938 | $self->rev_map_set($log_entry->{revision}, $commit, 1); |
9b981fc6 | 2939 | |
44320b9e | 2940 | $self->{last_rev} = $log_entry->{revision}; |
9b981fc6 | 2941 | $self->{last_commit} = $commit; |
49750f30 | 2942 | print "r$log_entry->{revision}" unless $::_q > 1; |
8a49ee97 | 2943 | if (defined $log_entry->{svm_revision}) { |
49750f30 | 2944 | print " (\@$log_entry->{svm_revision})" unless $::_q > 1; |
060610c5 | 2945 | $self->rev_map_set($log_entry->{svm_revision}, $commit, |
26a62d57 | 2946 | 0, $self->svm_uuid); |
8a49ee97 | 2947 | } |
49750f30 | 2948 | print " = $commit ($self->{ref_id})\n" unless $::_q > 1; |
94bc914c KW |
2949 | if (--$_gc_nr == 0) { |
2950 | $_gc_nr = $_gc_period; | |
2951 | gc(); | |
2952 | } | |
9b981fc6 EW |
2953 | return $commit; |
2954 | } | |
2955 | ||
fbcc1737 EW |
2956 | sub match_paths { |
2957 | my ($self, $paths, $r) = @_; | |
4e9f6cc7 | 2958 | return 1 if $self->{path} eq ''; |
d542aedb EW |
2959 | if (my $path = $paths->{"/$self->{path}"}) { |
2960 | return ($path->{action} eq 'D') ? 0 : 1; | |
2961 | } | |
0b2af457 | 2962 | $self->{path_regex} ||= qr/^\/\Q$self->{path}\E\//; |
fbcc1737 EW |
2963 | if (grep /$self->{path_regex}/, keys %$paths) { |
2964 | return 1; | |
2965 | } | |
2966 | my $c = ''; | |
2967 | foreach (split m#/#, $self->{path}) { | |
2968 | $c .= "/$_"; | |
74a81227 EW |
2969 | next unless ($paths->{$c} && |
2970 | ($paths->{$c}->{action} =~ /^[AR]$/)); | |
e518192f EW |
2971 | if ($self->ra->check_path($self->{path}, $r) == |
2972 | $SVN::Node::dir) { | |
fbcc1737 EW |
2973 | return 1; |
2974 | } | |
2975 | } | |
2976 | return 0; | |
2977 | } | |
2978 | ||
15710b6f EW |
2979 | sub find_parent_branch { |
2980 | my ($self, $paths, $rev) = @_; | |
91b03282 | 2981 | return undef unless $self->follow_parent; |
e5a0b240 | 2982 | unless (defined $paths) { |
c7eba716 EW |
2983 | my $err_handler = $SVN::Error::handler; |
2984 | $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs; | |
3c49a035 MN |
2985 | $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1, |
2986 | sub { $paths = $_[0] }); | |
c7eba716 | 2987 | $SVN::Error::handler = $err_handler; |
e5a0b240 EW |
2988 | } |
2989 | return undef unless defined $paths; | |
15710b6f EW |
2990 | |
2991 | # look for a parent from another branch: | |
0b2af457 | 2992 | my @b_path_components = split m#/#, $self->{path}; |
7f578c55 EW |
2993 | my @a_path_components; |
2994 | my $i; | |
2995 | while (@b_path_components) { | |
2996 | $i = $paths->{'/'.join('/', @b_path_components)}; | |
74a81227 | 2997 | last if $i && defined $i->{copyfrom_path}; |
7f578c55 EW |
2998 | unshift(@a_path_components, pop(@b_path_components)); |
2999 | } | |
74a81227 EW |
3000 | return undef unless defined $i && defined $i->{copyfrom_path}; |
3001 | my $branch_from = $i->{copyfrom_path}; | |
7f578c55 EW |
3002 | if (@a_path_components) { |
3003 | print STDERR "branch_from: $branch_from => "; | |
3004 | $branch_from .= '/'.join('/', @a_path_components); | |
3005 | print STDERR $branch_from, "\n"; | |
3006 | } | |
3ebe8df7 | 3007 | my $r = $i->{copyfrom_rev}; |
15710b6f EW |
3008 | my $repos_root = $self->ra->{repos_root}; |
3009 | my $url = $self->ra->{url}; | |
0b2af457 | 3010 | my $new_url = $url . $branch_from; |
15710b6f | 3011 | print STDERR "Found possible branch point: ", |
85886162 SA |
3012 | "$new_url => ", $self->full_url, ", $r\n" |
3013 | unless $::_q > 1; | |
15710b6f | 3014 | $branch_from =~ s#^/##; |
0b2af457 | 3015 | my $gs = $self->other_gs($new_url, $url, |
8e3f9b17 | 3016 | $branch_from, $r, $self->{ref_id}); |
15710b6f | 3017 | my ($r0, $parent) = $gs->find_rev_before($r, 1); |
553589f7 DM |
3018 | { |
3019 | my ($base, $head); | |
3020 | if (!defined $r0 || !defined $parent) { | |
3021 | ($base, $head) = parse_revision_argument(0, $r); | |
3022 | } else { | |
3023 | if ($r0 < $r) { | |
3024 | $gs->ra->get_log([$gs->{path}], $r0 + 1, $r, 1, | |
3025 | 0, 1, sub { $base = $_[1] - 1 }); | |
3026 | } | |
3027 | } | |
3028 | if (defined $base && $base <= $r) { | |
d627de6b EW |
3029 | $gs->fetch($base, $r); |
3030 | } | |
553589f7 | 3031 | ($r0, $parent) = $gs->find_rev_before($r, 1); |
15710b6f | 3032 | } |
ef70de96 | 3033 | if (defined $r0 && defined $parent) { |
85886162 SA |
3034 | print STDERR "Found branch parent: ($self->{ref_id}) $parent\n" |
3035 | unless $::_q > 1; | |
15710b6f EW |
3036 | my $ed; |
3037 | if ($self->ra->can_do_switch) { | |
2e5e2480 | 3038 | $self->assert_index_clean($parent); |
85886162 SA |
3039 | print STDERR "Following parent with do_switch\n" |
3040 | unless $::_q > 1; | |
15710b6f | 3041 | # do_switch works with svn/trunk >= r22312, but that |
2b27f6c8 | 3042 | # is not included with SVN 1.4.3 (the latest version |
15710b6f | 3043 | # at the moment), so we can't rely on it |
83c2fcff | 3044 | $self->{last_rev} = $r0; |
15710b6f | 3045 | $self->{last_commit} = $parent; |
8841b37f | 3046 | $ed = SVN::Git::Fetcher->new($self, $gs->{path}); |
8a603774 | 3047 | $gs->ra->gs_do_switch($r0, $rev, $gs, |
15710b6f EW |
3048 | $self->full_url, $ed) |
3049 | or die "SVN connection failed somewhere...\n"; | |
9ff74e95 SW |
3050 | } elsif ($self->ra->trees_match($new_url, $r0, |
3051 | $self->full_url, $rev)) { | |
3052 | print STDERR "Trees match:\n", | |
3053 | " $new_url\@$r0\n", | |
3054 | " ${\$self->full_url}\@$rev\n", | |
85886162 SA |
3055 | "Following parent with no changes\n" |
3056 | unless $::_q > 1; | |
9ff74e95 SW |
3057 | $self->tmp_index_do(sub { |
3058 | command_noisy('read-tree', $parent); | |
3059 | }); | |
3060 | $self->{last_commit} = $parent; | |
15710b6f | 3061 | } else { |
85886162 SA |
3062 | print STDERR "Following parent with do_update\n" |
3063 | unless $::_q > 1; | |
15710b6f | 3064 | $ed = SVN::Git::Fetcher->new($self); |
8a603774 | 3065 | $self->ra->gs_do_update($rev, $rev, $self, $ed) |
15710b6f EW |
3066 | or die "SVN connection failed somewhere...\n"; |
3067 | } | |
85886162 | 3068 | print STDERR "Successfully followed parent\n" unless $::_q > 1; |
15710b6f EW |
3069 | return $self->make_log_entry($rev, [$parent], $ed); |
3070 | } | |
15710b6f EW |
3071 | return undef; |
3072 | } | |
3073 | ||
9b981fc6 | 3074 | sub do_fetch { |
706587fc | 3075 | my ($self, $paths, $rev) = @_; |
15710b6f | 3076 | my $ed; |
9b981fc6 | 3077 | my ($last_rev, @parents); |
b9dffd8c EW |
3078 | if (my $lc = $self->last_commit) { |
3079 | # we can have a branch that was deleted, then re-added | |
3080 | # under the same name but copied from another path, in | |
3081 | # which case we'll have multiple parents (we don't | |
3082 | # want to break the original ref, nor lose copypath info): | |
3083 | if (my $log_entry = $self->find_parent_branch($paths, $rev)) { | |
3084 | push @{$log_entry->{parents}}, $lc; | |
3085 | return $log_entry; | |
3086 | } | |
15710b6f | 3087 | $ed = SVN::Git::Fetcher->new($self); |
9b981fc6 | 3088 | $last_rev = $self->{last_rev}; |
b9dffd8c EW |
3089 | $ed->{c} = $lc; |
3090 | @parents = ($lc); | |
9b981fc6 EW |
3091 | } else { |
3092 | $last_rev = $rev; | |
15710b6f EW |
3093 | if (my $log_entry = $self->find_parent_branch($paths, $rev)) { |
3094 | return $log_entry; | |
3095 | } | |
3096 | $ed = SVN::Git::Fetcher->new($self); | |
9b981fc6 | 3097 | } |
8a603774 | 3098 | unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) { |
9b981fc6 EW |
3099 | die "SVN connection failed somewhere...\n"; |
3100 | } | |
3101 | $self->make_log_entry($rev, \@parents, $ed); | |
3102 | } | |
3103 | ||
6111b934 EW |
3104 | sub mkemptydirs { |
3105 | my ($self, $r) = @_; | |
a5b80d92 EW |
3106 | |
3107 | sub scan { | |
3108 | my ($r, $empty_dirs, $line) = @_; | |
3109 | if (defined $r && $line =~ /^r(\d+)$/) { | |
3110 | return 0 if $1 > $r; | |
3111 | } elsif ($line =~ /^ \+empty_dir: (.+)$/) { | |
3112 | $empty_dirs->{$1} = 1; | |
3113 | } elsif ($line =~ /^ \-empty_dir: (.+)$/) { | |
3114 | my @d = grep {m[^\Q$1\E(/|$)]} (keys %$empty_dirs); | |
3115 | delete @$empty_dirs{@d}; | |
3116 | } | |
3117 | 1; # continue | |
3118 | }; | |
3119 | ||
6111b934 | 3120 | my %empty_dirs = (); |
a5b80d92 EW |
3121 | my $gz_file = "$self->{dir}/unhandled.log.gz"; |
3122 | if (-f $gz_file) { | |
3123 | if (!$can_compress) { | |
3124 | warn "Compress::Zlib could not be found; ", | |
3125 | "empty directories in $gz_file will not be read\n"; | |
3126 | } else { | |
3127 | my $gz = Compress::Zlib::gzopen($gz_file, "rb") or | |
3128 | die "Unable to open $gz_file: $!\n"; | |
3129 | my $line; | |
3130 | while ($gz->gzreadline($line) > 0) { | |
3131 | scan($r, \%empty_dirs, $line) or last; | |
3132 | } | |
3133 | $gz->gzclose; | |
3134 | } | |
3135 | } | |
6111b934 | 3136 | |
a5b80d92 EW |
3137 | if (open my $fh, '<', "$self->{dir}/unhandled.log") { |
3138 | binmode $fh or croak "binmode: $!"; | |
3139 | while (<$fh>) { | |
3140 | scan($r, \%empty_dirs, $_) or last; | |
6111b934 | 3141 | } |
a5b80d92 | 3142 | close $fh; |
6111b934 | 3143 | } |
9be30eed EW |
3144 | |
3145 | my $strip = qr/\A\Q$self->{path}\E(?:\/|$)/; | |
6111b934 EW |
3146 | foreach my $d (sort keys %empty_dirs) { |
3147 | $d = uri_decode($d); | |
9be30eed | 3148 | $d =~ s/$strip//; |
7c42e390 | 3149 | next unless length($d); |
6111b934 | 3150 | next if -d $d; |
7c42e390 | 3151 | if (-e $d) { |
6111b934 EW |
3152 | warn "$d exists but is not a directory\n"; |
3153 | } else { | |
3154 | print "creating empty directory: $d\n"; | |
3155 | mkpath([$d]); | |
3156 | } | |
3157 | } | |
3158 | } | |
3159 | ||
97f6987a EW |
3160 | sub get_untracked { |
3161 | my ($self, $ed) = @_; | |
3162 | my @out; | |
3163 | my $h = $ed->{empty}; | |
9b981fc6 EW |
3164 | foreach (sort keys %$h) { |
3165 | my $act = $h->{$_} ? '+empty_dir' : '-empty_dir'; | |
97f6987a | 3166 | push @out, " $act: " . uri_encode($_); |
9b981fc6 EW |
3167 | warn "W: $act: $_\n"; |
3168 | } | |
3169 | foreach my $t (qw/dir_prop file_prop/) { | |
97f6987a | 3170 | $h = $ed->{$t} or next; |
9b981fc6 EW |
3171 | foreach my $path (sort keys %$h) { |
3172 | my $ppath = $path eq '' ? '.' : $path; | |
3173 | foreach my $prop (sort keys %{$h->{$path}}) { | |
1ce255dc | 3174 | next if $SKIP_PROP{$prop}; |
9b981fc6 | 3175 | my $v = $h->{$path}->{$prop}; |
97f6987a EW |
3176 | my $t_ppath_prop = "$t: " . |
3177 | uri_encode($ppath) . ' ' . | |
3178 | uri_encode($prop); | |
9b981fc6 | 3179 | if (defined $v) { |
97f6987a EW |
3180 | push @out, " +$t_ppath_prop " . |
3181 | uri_encode($v); | |
9b981fc6 | 3182 | } else { |
97f6987a | 3183 | push @out, " -$t_ppath_prop"; |
9b981fc6 EW |
3184 | } |
3185 | } | |
3186 | } | |
3187 | } | |
3188 | foreach my $t (qw/absent_file absent_directory/) { | |
97f6987a | 3189 | $h = $ed->{$t} or next; |
9b981fc6 EW |
3190 | foreach my $parent (sort keys %$h) { |
3191 | foreach my $path (sort @{$h->{$parent}}) { | |
97f6987a EW |
3192 | push @out, " $t: " . |
3193 | uri_encode("$parent/$path"); | |
9b981fc6 EW |
3194 | warn "W: $t: $parent/$path ", |
3195 | "Insufficient permissions?\n"; | |
3196 | } | |
3197 | } | |
3198 | } | |
97f6987a | 3199 | \@out; |
9b981fc6 EW |
3200 | } |
3201 | ||
e82f0d73 PH |
3202 | # parse_svn_date(DATE) |
3203 | # -------------------- | |
3204 | # Given a date (in UTC) from Subversion, return a string in the format | |
3205 | # "<TZ Offset> <local date/time>" that Git will use. | |
3206 | # | |
3207 | # By default the parsed date will be in UTC; if $Git::SVN::_localtime | |
3208 | # is true we'll convert it to the local timezone instead. | |
1c8443b0 EW |
3209 | sub parse_svn_date { |
3210 | my $date = shift || return '+0000 1970-01-01 00:00:00'; | |
3211 | my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T | |
b94ead75 | 3212 | (\d\d)\:(\d\d)\:(\d\d)\.\d*Z$/x) or |
1c8443b0 | 3213 | croak "Unable to parse date: $date\n"; |
e82f0d73 PH |
3214 | my $parsed_date; # Set next. |
3215 | ||
3216 | if ($Git::SVN::_localtime) { | |
3217 | # Translate the Subversion datetime to an epoch time. | |
3218 | # Begin by switching ourselves to $date's timezone, UTC. | |
3219 | my $old_env_TZ = $ENV{TZ}; | |
3220 | $ENV{TZ} = 'UTC'; | |
3221 | ||
3222 | my $epoch_in_UTC = | |
3223 | POSIX::strftime('%s', $S, $M, $H, $d, $m - 1, $Y - 1900); | |
3224 | ||
3225 | # Determine our local timezone (including DST) at the | |
3226 | # time of $epoch_in_UTC. $Git::SVN::Log::TZ stored the | |
3227 | # value of TZ, if any, at the time we were run. | |
3228 | if (defined $Git::SVN::Log::TZ) { | |
3229 | $ENV{TZ} = $Git::SVN::Log::TZ; | |
3230 | } else { | |
3231 | delete $ENV{TZ}; | |
3232 | } | |
3233 | ||
3234 | my $our_TZ = | |
3235 | POSIX::strftime('%Z', $S, $M, $H, $d, $m - 1, $Y - 1900); | |
3236 | ||
3237 | # This converts $epoch_in_UTC into our local timezone. | |
3238 | my ($sec, $min, $hour, $mday, $mon, $year, | |
3239 | $wday, $yday, $isdst) = localtime($epoch_in_UTC); | |
3240 | ||
3241 | $parsed_date = sprintf('%s %04d-%02d-%02d %02d:%02d:%02d', | |
3242 | $our_TZ, $year + 1900, $mon + 1, | |
3243 | $mday, $hour, $min, $sec); | |
3244 | ||
3245 | # Reset us to the timezone in effect when we entered | |
3246 | # this routine. | |
3247 | if (defined $old_env_TZ) { | |
3248 | $ENV{TZ} = $old_env_TZ; | |
3249 | } else { | |
3250 | delete $ENV{TZ}; | |
3251 | } | |
3252 | } else { | |
3253 | $parsed_date = "+0000 $Y-$m-$d $H:$M:$S"; | |
3254 | } | |
3255 | ||
3256 | return $parsed_date; | |
1c8443b0 EW |
3257 | } |
3258 | ||
8e3f9b17 | 3259 | sub other_gs { |
0b2af457 | 3260 | my ($self, $new_url, $url, |
8e3f9b17 | 3261 | $branch_from, $r, $old_ref_id) = @_; |
0b2af457 | 3262 | my $gs = Git::SVN->find_by_url($new_url, $url, $branch_from); |
8e3f9b17 SV |
3263 | unless ($gs) { |
3264 | my $ref_id = $old_ref_id; | |
54fb7f9b | 3265 | $ref_id =~ s/\@\d+-*$//; |
8e3f9b17 SV |
3266 | $ref_id .= "\@$r"; |
3267 | # just grow a tail if we're not unique enough :x | |
3268 | $ref_id .= '-' while find_ref($ref_id); | |
8e3f9b17 SV |
3269 | my ($u, $p, $repo_id) = ($new_url, '', $ref_id); |
3270 | if ($u =~ s#^\Q$url\E(/|$)##) { | |
3271 | $p = $u; | |
3272 | $u = $url; | |
3273 | $repo_id = $self->{repo_id}; | |
3274 | } | |
3235b705 DK |
3275 | while (1) { |
3276 | # It is possible to tag two different subdirectories at | |
3277 | # the same revision. If the url for an existing ref | |
3278 | # does not match, we must either find a ref with a | |
3279 | # matching url or create a new ref by growing a tail. | |
3280 | $gs = Git::SVN->init($u, $p, $repo_id, $ref_id, 1); | |
3281 | my (undef, $max_commit) = $gs->rev_map_max(1); | |
3282 | last if (!$max_commit); | |
3283 | my ($url) = ::cmt_metadata($max_commit); | |
85f022e9 | 3284 | last if ($url eq $gs->metadata_url); |
3235b705 DK |
3285 | $ref_id .= '-'; |
3286 | } | |
3287 | print STDERR "Initializing parent: $ref_id\n" unless $::_q > 1; | |
8e3f9b17 SV |
3288 | } |
3289 | $gs | |
3290 | } | |
3291 | ||
36db1edd ML |
3292 | sub call_authors_prog { |
3293 | my ($orig_author) = @_; | |
d3d7d47e | 3294 | $orig_author = command_oneline('rev-parse', '--sq-quote', $orig_author); |
36db1edd ML |
3295 | my $author = `$::_authors_prog $orig_author`; |
3296 | if ($? != 0) { | |
3297 | die "$::_authors_prog failed with exit code $?\n" | |
3298 | } | |
3299 | if ($author =~ /^\s*(.+?)\s*<(.*)>\s*$/) { | |
3300 | my ($name, $email) = ($1, $2); | |
3301 | $email = undef if length $2 == 0; | |
3302 | return [$name, $email]; | |
3303 | } else { | |
3304 | die "Author: $orig_author: $::_authors_prog returned " | |
3305 | . "invalid author format: $author\n"; | |
3306 | } | |
3307 | } | |
3308 | ||
1c8443b0 EW |
3309 | sub check_author { |
3310 | my ($author) = @_; | |
3311 | if (!defined $author || length $author == 0) { | |
3312 | $author = '(no author)'; | |
36db1edd ML |
3313 | } |
3314 | if (!defined $::users{$author}) { | |
3315 | if (defined $::_authors_prog) { | |
3316 | $::users{$author} = call_authors_prog($author); | |
3317 | } elsif (defined $::_authors) { | |
3318 | die "Author: $author not defined in $::_authors file\n"; | |
3319 | } | |
1c8443b0 EW |
3320 | } |
3321 | $author; | |
3322 | } | |
3323 | ||
f1264bd6 SV |
3324 | sub find_extra_svk_parents { |
3325 | my ($self, $ed, $tickets, $parents) = @_; | |
3326 | # aha! svk:merge property changed... | |
3327 | my @tickets = split "\n", $tickets; | |
3328 | my @known_parents; | |
3329 | for my $ticket ( @tickets ) { | |
3330 | my ($uuid, $path, $rev) = split /:/, $ticket; | |
3331 | if ( $uuid eq $self->ra_uuid ) { | |
bf60fff8 | 3332 | my $url = $self->{url}; |
f1264bd6 SV |
3333 | my $repos_root = $url; |
3334 | my $branch_from = $path; | |
3335 | $branch_from =~ s{^/}{}; | |
3336 | my $gs = $self->other_gs($repos_root."/".$branch_from, | |
3337 | $url, | |
3338 | $branch_from, | |
3339 | $rev, | |
3340 | $self->{ref_id}); | |
3341 | if ( my $commit = $gs->rev_map_get($rev, $uuid) ) { | |
3342 | # wahey! we found it, but it might be | |
3343 | # an old one (!) | |
e9e4c8b7 | 3344 | push @known_parents, [ $rev, $commit ]; |
f1264bd6 SV |
3345 | } |
3346 | } | |
3347 | } | |
e9e4c8b7 AV |
3348 | # Ordering matters; highest-numbered commit merge tickets |
3349 | # first, as they may account for later merge ticket additions | |
3350 | # or changes. | |
3351 | @known_parents = map {$_->[1]} sort {$b->[0] <=> $a->[0]} @known_parents; | |
f1264bd6 SV |
3352 | for my $parent ( @known_parents ) { |
3353 | my @cmd = ('rev-list', $parent, map { "^$_" } @$parents ); | |
3354 | my ($msg_fh, $ctx) = command_output_pipe(@cmd); | |
3355 | my $new; | |
3356 | while ( <$msg_fh> ) { | |
3357 | $new=1;last; | |
3358 | } | |
3359 | command_close_pipe($msg_fh, $ctx); | |
3360 | if ( $new ) { | |
3361 | print STDERR | |
3362 | "Found merge parent (svk:merge ticket): $parent\n"; | |
3363 | push @$parents, $parent; | |
3364 | } | |
3365 | } | |
3366 | } | |
3367 | ||
7d944c33 SV |
3368 | sub lookup_svn_merge { |
3369 | my $uuid = shift; | |
3370 | my $url = shift; | |
3371 | my $merge = shift; | |
3372 | ||
3373 | my ($source, $revs) = split ":", $merge; | |
3374 | my $path = $source; | |
3375 | $path =~ s{^/}{}; | |
3376 | my $gs = Git::SVN->find_by_url($url.$source, $url, $path); | |
3377 | if ( !$gs ) { | |
3378 | warn "Couldn't find revmap for $url$source\n"; | |
3379 | return; | |
3380 | } | |
3381 | my @ranges = split ",", $revs; | |
3382 | my ($tip, $tip_commit); | |
3383 | my @merged_commit_ranges; | |
3384 | # find the tip | |
3385 | for my $range ( @ranges ) { | |
3386 | my ($bottom, $top) = split "-", $range; | |
3387 | $top ||= $bottom; | |
33973a5b SV |
3388 | my $bottom_commit = $gs->find_rev_after( $bottom, 1, $top ); |
3389 | my $top_commit = $gs->find_rev_before( $top, 1, $bottom ); | |
7d944c33 SV |
3390 | |
3391 | unless ($top_commit and $bottom_commit) { | |
3392 | warn "W:unknown path/rev in svn:mergeinfo " | |
3393 | ."dirprop: $source:$range\n"; | |
3394 | next; | |
3395 | } | |
3396 | ||
124b70a2 MH |
3397 | if (scalar(command('rev-parse', "$bottom_commit^@"))) { |
3398 | push @merged_commit_ranges, | |
3399 | "$bottom_commit^..$top_commit"; | |
3400 | } else { | |
3401 | push @merged_commit_ranges, "$top_commit"; | |
3402 | } | |
7d944c33 SV |
3403 | |
3404 | if ( !defined $tip or $top > $tip ) { | |
3405 | $tip = $top; | |
3406 | $tip_commit = $top_commit; | |
3407 | } | |
3408 | } | |
3409 | return ($tip_commit, @merged_commit_ranges); | |
3410 | } | |
7a955a53 SV |
3411 | |
3412 | sub _rev_list { | |
3413 | my ($msg_fh, $ctx) = command_output_pipe( | |
3414 | "rev-list", @_, | |
3415 | ); | |
3416 | my @rv; | |
3417 | while ( <$msg_fh> ) { | |
3418 | chomp; | |
3419 | push @rv, $_; | |
3420 | } | |
3421 | command_close_pipe($msg_fh, $ctx); | |
3422 | @rv; | |
3423 | } | |
3424 | ||
3425 | sub check_cherry_pick { | |
3426 | my $base = shift; | |
3427 | my $tip = shift; | |
a3c75056 | 3428 | my $parents = shift; |
7a955a53 SV |
3429 | my @ranges = @_; |
3430 | my %commits = map { $_ => 1 } | |
eabd73a3 | 3431 | _rev_list("--no-merges", $tip, "--not", $base, @$parents, "--"); |
7a955a53 | 3432 | for my $range ( @ranges ) { |
eabd73a3 | 3433 | delete @commits{_rev_list($range, "--")}; |
7a955a53 | 3434 | } |
1cef6500 AM |
3435 | for my $commit (keys %commits) { |
3436 | if (has_no_changes($commit)) { | |
3437 | delete $commits{$commit}; | |
3438 | } | |
3439 | } | |
7a955a53 SV |
3440 | return (keys %commits); |
3441 | } | |
3442 | ||
1cef6500 AM |
3443 | sub has_no_changes { |
3444 | my $commit = shift; | |
3445 | ||
3446 | my @revs = split / /, command_oneline( | |
3447 | qw(rev-list --parents -1 -m), $commit); | |
3448 | ||
3449 | # Commits with no parents, e.g. the start of a partial branch, | |
3450 | # have changes by definition. | |
3451 | return 1 if (@revs < 2); | |
3452 | ||
3453 | # Commits with multiple parents, e.g a merge, have no changes | |
3454 | # by definition. | |
3455 | return 0 if (@revs > 2); | |
3456 | ||
3457 | return (command_oneline("rev-parse", "$commit^{tree}") eq | |
3458 | command_oneline("rev-parse", "$commit~1^{tree}")); | |
3459 | } | |
3460 | ||
8bff7c53 AM |
3461 | # The GIT_DIR environment variable is not always set until after the command |
3462 | # line arguments are processed, so we can't memoize in a BEGIN block. | |
3463 | { | |
3464 | my $memoized = 0; | |
3465 | ||
3466 | sub memoize_svn_mergeinfo_functions { | |
3467 | return if $memoized; | |
3468 | $memoized = 1; | |
3469 | ||
3470 | my $cache_path = "$ENV{GIT_DIR}/svn/.caches/"; | |
3471 | mkpath([$cache_path]) unless -d $cache_path; | |
3472 | ||
3473 | tie my %lookup_svn_merge_cache => 'Memoize::Storable', | |
3474 | "$cache_path/lookup_svn_merge.db", 'nstore'; | |
3475 | memoize 'lookup_svn_merge', | |
3476 | SCALAR_CACHE => 'FAULT', | |
3477 | LIST_CACHE => ['HASH' => \%lookup_svn_merge_cache], | |
3478 | ; | |
3479 | ||
3480 | tie my %check_cherry_pick_cache => 'Memoize::Storable', | |
3481 | "$cache_path/check_cherry_pick.db", 'nstore'; | |
3482 | memoize 'check_cherry_pick', | |
3483 | SCALAR_CACHE => 'FAULT', | |
3484 | LIST_CACHE => ['HASH' => \%check_cherry_pick_cache], | |
3485 | ; | |
3486 | ||
3487 | tie my %has_no_changes_cache => 'Memoize::Storable', | |
3488 | "$cache_path/has_no_changes.db", 'nstore'; | |
3489 | memoize 'has_no_changes', | |
3490 | SCALAR_CACHE => ['HASH' => \%has_no_changes_cache], | |
3491 | LIST_CACHE => 'FAULT', | |
3492 | ; | |
3493 | } | |
8ac3a667 SV |
3494 | |
3495 | sub unmemoize_svn_mergeinfo_functions { | |
3496 | return if not $memoized; | |
3497 | $memoized = 0; | |
3498 | ||
3499 | Memoize::unmemoize 'lookup_svn_merge'; | |
3500 | Memoize::unmemoize 'check_cherry_pick'; | |
3501 | Memoize::unmemoize 'has_no_changes'; | |
3502 | } | |
f5549afd JK |
3503 | |
3504 | Memoize::memoize 'Git::SVN::repos_root'; | |
8ac3a667 SV |
3505 | } |
3506 | ||
3507 | END { | |
3508 | # Force cache writeout explicitly instead of waiting for | |
3509 | # global destruction to avoid segfault in Storable: | |
3510 | # http://rt.cpan.org/Public/Bug/Display.html?id=36087 | |
3511 | unmemoize_svn_mergeinfo_functions(); | |
7d944c33 SV |
3512 | } |
3513 | ||
ea020cbd SV |
3514 | sub parents_exclude { |
3515 | my $parents = shift; | |
3516 | my @commits = @_; | |
3517 | return unless @commits; | |
3518 | ||
3519 | my @excluded; | |
3520 | my $excluded; | |
3521 | do { | |
3522 | my @cmd = ('rev-list', "-1", @commits, "--not", @$parents ); | |
3523 | $excluded = command_oneline(@cmd); | |
3524 | if ( $excluded ) { | |
3525 | my @new; | |
3526 | my $found; | |
3527 | for my $commit ( @commits ) { | |
3528 | if ( $commit eq $excluded ) { | |
3529 | push @excluded, $commit; | |
3530 | $found++; | |
3531 | last; | |
3532 | } | |
3533 | else { | |
3534 | push @new, $commit; | |
3535 | } | |
3536 | } | |
3537 | die "saw commit '$excluded' in rev-list output, " | |
3538 | ."but we didn't ask for that commit (wanted: @commits --not @$parents)" | |
3539 | unless $found; | |
3540 | @commits = @new; | |
3541 | } | |
3542 | } | |
3543 | while ($excluded and @commits); | |
3544 | ||
3545 | return @excluded; | |
3546 | } | |
3547 | ||
3548 | ||
dff589ef SV |
3549 | # note: this function should only be called if the various dirprops |
3550 | # have actually changed | |
3551 | sub find_extra_svn_parents { | |
3552 | my ($self, $ed, $mergeinfo, $parents) = @_; | |
3553 | # aha! svk:merge property changed... | |
3554 | ||
8bff7c53 AM |
3555 | memoize_svn_mergeinfo_functions(); |
3556 | ||
dff589ef SV |
3557 | # We first search for merged tips which are not in our |
3558 | # history. Then, we figure out which git revisions are in | |
3559 | # that tip, but not this revision. If all of those revisions | |
3560 | # are now marked as merge, we can add the tip as a parent. | |
3561 | my @merges = split "\n", $mergeinfo; | |
3562 | my @merge_tips; | |
bf60fff8 | 3563 | my $url = $self->{url}; |
7d944c33 | 3564 | my $uuid = $self->ra_uuid; |
ea020cbd | 3565 | my %ranges; |
dff589ef | 3566 | for my $merge ( @merges ) { |
7d944c33 SV |
3567 | my ($tip_commit, @ranges) = |
3568 | lookup_svn_merge( $uuid, $url, $merge ); | |
dff589ef SV |
3569 | unless (!$tip_commit or |
3570 | grep { $_ eq $tip_commit } @$parents ) { | |
3571 | push @merge_tips, $tip_commit; | |
ea020cbd | 3572 | $ranges{$tip_commit} = \@ranges; |