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