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