]>
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 | |
9760adcc EW |
7 | $sha1 $sha1_short $_revision |
8 | $_q $_authors %users/; | |
3397f9df | 9 | $AUTHOR = 'Eric Wong <normalperson@yhbt.net>'; |
60d02ccc | 10 | $VERSION = '@@GIT_VERSION@@'; |
13ccd6d4 | 11 | |
706587fc | 12 | $ENV{GIT_DIR} ||= '.git'; |
9fa00b65 | 13 | $Git::SVN::default_repo_id = 'svn'; |
8b8fc068 | 14 | $Git::SVN::default_ref_id = $ENV{GIT_SVN_ID} || 'git-svn'; |
13ccd6d4 | 15 | |
f8c9d1d2 | 16 | $Git::SVN::Log::TZ = $ENV{TZ}; |
3397f9df | 17 | $ENV{TZ} = 'UTC'; |
a00439ac | 18 | $| = 1; # unbuffer STDOUT |
3397f9df | 19 | |
6fda05ae | 20 | sub fatal (@) { print STDERR @_; exit 1 } |
b9c85187 EW |
21 | require SVN::Core; # use()-ing this causes segfaults for me... *shrug* |
22 | require SVN::Ra; | |
23 | require SVN::Delta; | |
24 | if ($SVN::Core::VERSION lt '1.1.0') { | |
25 | fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)\n"; | |
26 | } | |
d81bf827 | 27 | push @Git::SVN::Ra::ISA, 'SVN::Ra'; |
b9c85187 EW |
28 | push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor'; |
29 | push @SVN::Git::Fetcher::ISA, 'SVN::Delta::Editor'; | |
3397f9df EW |
30 | use Carp qw/croak/; |
31 | use IO::File qw//; | |
32 | use File::Basename qw/dirname basename/; | |
33 | use File::Path qw/mkpath/; | |
79bb8d88 | 34 | use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev pass_through/; |
968bdf1f | 35 | use IPC::Open3; |
336f1714 | 36 | use Git; |
a5e0cedc | 37 | |
336f1714 EW |
38 | BEGIN { |
39 | my $s; | |
40 | foreach (qw/command command_oneline command_noisy command_output_pipe | |
41 | command_input_pipe command_close_pipe/) { | |
42 | $s .= "*SVN::Git::Editor::$_ = *SVN::Git::Fetcher::$_ = ". | |
706587fc | 43 | "*Git::SVN::Migration::$_ = ". |
f8c9d1d2 | 44 | "*Git::SVN::Log::$_ = *Git::SVN::$_ = *$_ = *Git::$_; "; |
336f1714 EW |
45 | } |
46 | eval $s; | |
47 | } | |
48 | ||
b9c85187 | 49 | my ($SVN); |
83e9940a | 50 | |
42d32870 | 51 | my $_optimize_commits = 1 unless $ENV{GIT_SVN_NO_OPTIMIZE_COMMITS}; |
f8c9d1d2 EW |
52 | $sha1 = qr/[a-f\d]{40}/; |
53 | $sha1_short = qr/[a-f\d]{4,40}/; | |
44320b9e | 54 | my ($_stdin, $_help, $_edit, |
9760adcc | 55 | $_message, $_file, |
d05d72e0 | 56 | $_template, $_shared, |
9760adcc | 57 | $_version, |
f8c9d1d2 EW |
58 | $_merge, $_strategy, $_dry_run, |
59 | $_prefix); | |
3397f9df | 60 | |
706587fc EW |
61 | my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username, |
62 | 'config-dir=s' => \$Git::SVN::Ra::config_dir, | |
63 | 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache ); | |
9760adcc | 64 | my %fc_opts = ( 'follow-parent|follow' => \$Git::SVN::_follow_parent, |
dc5869c0 | 65 | 'authors-file|A=s' => \$_authors, |
ecc712dd | 66 | 'repack:i' => \$Git::SVN::_repack, |
9760adcc | 67 | 'no-metadata' => \$Git::SVN::_no_metadata, |
80f50749 | 68 | 'quiet|q' => \$_q, |
ecc712dd EW |
69 | 'repack-flags|repack-args|repack-opts=s' => |
70 | \$Git::SVN::_repack_flags, | |
706587fc | 71 | %remote_opts ); |
36f5b1f0 | 72 | |
9d55b41a EW |
73 | my ($_trunk, $_tags, $_branches); |
74 | my %multi_opts = ( 'trunk|T=s' => \$_trunk, | |
75 | 'tags|t=s' => \$_tags, | |
76 | 'branches|b=s' => \$_branches ); | |
77 | my %init_opts = ( 'template=s' => \$_template, 'shared' => \$_shared ); | |
27e9fb8d | 78 | my %cmt_opts = ( 'edit|e' => \$_edit, |
24e22aa8 EW |
79 | 'rmdir' => \$SVN::Git::Editor::_rmdir, |
80 | 'find-copies-harder' => \$SVN::Git::Editor::_find_copies_harder, | |
81 | 'l=i' => \$SVN::Git::Editor::_rename_limit, | |
82 | 'copy-similarity|C=i'=> \$SVN::Git::Editor::_cp_similarity | |
27e9fb8d | 83 | ); |
9d55b41a | 84 | |
3397f9df | 85 | my %cmd = ( |
2a3240be | 86 | fetch => [ \&cmd_fetch, "Download new revisions from SVN", |
eeb0abe0 | 87 | { 'revision|r=s' => \$_revision, %fc_opts } ], |
d2866f9e | 88 | init => [ \&cmd_init, "Initialize a repo for tracking" . |
f8ab6b73 | 89 | " (requires URL argument)", |
9d55b41a | 90 | \%init_opts ], |
d7ad3bed EW |
91 | dcommit => [ \&cmd_dcommit, |
92 | 'Commit several diffs to merge with upstream', | |
3289e86e EW |
93 | { 'merge|m|M' => \$_merge, |
94 | 'strategy|s=s' => \$_strategy, | |
95 | 'dry-run|n' => \$_dry_run, | |
4b155223 | 96 | %cmt_opts, %fc_opts } ], |
1ce255dc EW |
97 | 'set-tree' => [ \&cmd_set_tree, |
98 | "Set an SVN repository to a git tree-ish", | |
99 | { 'stdin|' => \$_stdin, %cmt_opts, %fc_opts, } ], | |
5969cbe1 | 100 | 'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings", |
a5e0cedc | 101 | { 'revision|r=i' => \$_revision } ], |
8164b652 | 102 | 'multi-init' => [ \&cmd_multi_init, |
9d55b41a | 103 | 'Initialize multiple trees (like git-svnimport)', |
706587fc | 104 | { %multi_opts, %init_opts, %remote_opts, |
1ca7558d | 105 | 'revision|r=i' => \$_revision, |
ae410987 | 106 | 'prefix=s' => \$_prefix, |
1ca7558d | 107 | } ], |
1c8443b0 | 108 | 'multi-fetch' => [ \&cmd_multi_fetch, |
9d55b41a EW |
109 | 'Fetch multiple trees (like git-svnimport)', |
110 | \%fc_opts ], | |
706587fc EW |
111 | 'migrate' => [ sub { }, |
112 | # no-op, we automatically run this anyways, | |
706587fc EW |
113 | 'Migrate configuration/metadata/layout from |
114 | previous versions of git-svn', | |
115 | \%remote_opts ], | |
f8c9d1d2 EW |
116 | 'log' => [ \&Git::SVN::Log::cmd_show_log, 'Show commit logs', |
117 | { 'limit=i' => \$Git::SVN::Log::limit, | |
79bb8d88 | 118 | 'revision|r=s' => \$_revision, |
f8c9d1d2 EW |
119 | 'verbose|v' => \$Git::SVN::Log::verbose, |
120 | 'incremental' => \$Git::SVN::Log::incremental, | |
121 | 'oneline' => \$Git::SVN::Log::oneline, | |
122 | 'show-commit' => \$Git::SVN::Log::show_commit, | |
123 | 'non-recursive' => \$Git::SVN::Log::non_recursive, | |
79bb8d88 | 124 | 'authors-file|A=s' => \$_authors, |
f8c9d1d2 EW |
125 | 'color' => \$Git::SVN::Log::color, |
126 | 'pager=s' => \$Git::SVN::Log::pager, | |
79bb8d88 | 127 | } ], |
44320b9e EW |
128 | 'commit-diff' => [ \&cmd_commit_diff, |
129 | 'Commit a diff between two trees', | |
27e9fb8d EW |
130 | { 'message|m=s' => \$_message, |
131 | 'file|F=s' => \$_file, | |
45bf473a | 132 | 'revision|r=s' => \$_revision, |
27e9fb8d | 133 | %cmt_opts } ], |
3397f9df | 134 | ); |
9d55b41a | 135 | |
3397f9df EW |
136 | my $cmd; |
137 | for (my $i = 0; $i < @ARGV; $i++) { | |
138 | if (defined $cmd{$ARGV[$i]}) { | |
139 | $cmd = $ARGV[$i]; | |
140 | splice @ARGV, $i, 1; | |
141 | last; | |
142 | } | |
143 | }; | |
144 | ||
448c81b4 | 145 | my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd); |
a9612be2 | 146 | |
b8c92cad | 147 | read_repo_config(\%opts); |
9760adcc EW |
148 | my $rv = GetOptions(%opts, 'help|H|h' => \$_help, 'version|V' => \$_version, |
149 | 'minimize-connections' => \$Git::SVN::Migration::_minimize, | |
150 | 'id|i=s' => \$Git::SVN::default_ref_id, | |
151 | 'svn-remote|remote|R=s' => \$Git::SVN::default_repo_id); | |
79bb8d88 | 152 | exit 1 if (!$rv && $cmd ne 'log'); |
6f0783cf | 153 | |
3397f9df | 154 | usage(0) if $_help; |
551ce28f | 155 | version() if $_version; |
eeb0abe0 EW |
156 | usage(1) unless defined $cmd; |
157 | load_authors() if $_authors; | |
f0ecca10 | 158 | unless ($cmd =~ /^(?:init|multi-init|commit-diff)$/) { |
706587fc EW |
159 | Git::SVN::Migration::migration_check(); |
160 | } | |
ecc712dd | 161 | Git::SVN::init_vars(); |
b805b44a EW |
162 | eval { |
163 | Git::SVN::verify_remotes_sanity(); | |
164 | $cmd{$cmd}->[0]->(@ARGV); | |
165 | }; | |
166 | fatal $@ if $@; | |
3397f9df EW |
167 | exit 0; |
168 | ||
169 | ####################### primary functions ###################### | |
170 | sub usage { | |
171 | my $exit = shift || 0; | |
172 | my $fd = $exit ? \*STDERR : \*STDOUT; | |
173 | print $fd <<""; | |
174 | git-svn - bidirectional operations between a single Subversion tree and git | |
175 | Usage: $0 <command> [options] [arguments]\n | |
448c81b4 EW |
176 | |
177 | print $fd "Available commands:\n" unless $cmd; | |
3397f9df EW |
178 | |
179 | foreach (sort keys %cmd) { | |
448c81b4 | 180 | next if $cmd && $cmd ne $_; |
b203b769 | 181 | print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n"; |
448c81b4 EW |
182 | foreach (keys %{$cmd{$_}->[2]}) { |
183 | # prints out arguments as they should be passed: | |
b8c92cad | 184 | my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : ''; |
b203b769 | 185 | print $fd ' ' x 21, join(', ', map { length $_ > 1 ? |
448c81b4 EW |
186 | "--$_" : "-$_" } |
187 | split /\|/,$_)," $x\n"; | |
188 | } | |
3397f9df EW |
189 | } |
190 | print $fd <<""; | |
448c81b4 EW |
191 | \nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an |
192 | arbitrary identifier if you're tracking multiple SVN branches/repositories in | |
193 | one git repository and want to keep them separate. See git-svn(1) for more | |
194 | information. | |
3397f9df EW |
195 | |
196 | exit $exit; | |
197 | } | |
198 | ||
551ce28f | 199 | sub version { |
7d60ab2c | 200 | print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n"; |
551ce28f EW |
201 | exit 0; |
202 | } | |
203 | ||
8164b652 EW |
204 | sub do_git_init_db { |
205 | unless (-d $ENV{GIT_DIR}) { | |
206 | my @init_db = ('init'); | |
207 | push @init_db, "--template=$_template" if defined $_template; | |
208 | push @init_db, "--shared" if defined $_shared; | |
209 | command_noisy(@init_db); | |
210 | } | |
211 | } | |
212 | ||
d2866f9e | 213 | sub cmd_init { |
03e0ea87 | 214 | my $url = shift or die "SVN repository location required " . |
81c5a0e6 | 215 | "as a command-line argument\n"; |
03e0ea87 EW |
216 | if (my $repo_path = shift) { |
217 | unless (-d $repo_path) { | |
218 | mkpath([$repo_path]); | |
219 | } | |
d2866f9e EW |
220 | chdir $repo_path or croak $!; |
221 | $ENV{GIT_DIR} = $repo_path . "/.git"; | |
03e0ea87 | 222 | } |
8164b652 | 223 | do_git_init_db(); |
03e0ea87 | 224 | |
706587fc | 225 | Git::SVN->init($url); |
3397f9df EW |
226 | } |
227 | ||
2a3240be | 228 | sub cmd_fetch { |
07a1c950 EW |
229 | if (@_) { |
230 | die "Additional fetch arguments are no longer supported.\n", | |
231 | "Use --follow-parent if you have moved/copied directories | |
232 | instead.\n"; | |
233 | } | |
1c8443b0 | 234 | my $gs = Git::SVN->new; |
3ebe8df7 | 235 | $gs->fetch(parse_revision_argument()); |
1c8443b0 EW |
236 | if ($gs->{last_commit} && !verify_ref('refs/heads/master^0')) { |
237 | command_noisy(qw(update-ref refs/heads/master), | |
238 | $gs->{last_commit}); | |
239 | } | |
2a3240be EW |
240 | } |
241 | ||
1ce255dc | 242 | sub cmd_set_tree { |
3397f9df EW |
243 | my (@commits) = @_; |
244 | if ($_stdin || !@commits) { | |
245 | print "Reading from stdin...\n"; | |
246 | @commits = (); | |
247 | while (<STDIN>) { | |
1ca72aef | 248 | if (/\b($sha1_short)\b/o) { |
3397f9df EW |
249 | unshift @commits, $1; |
250 | } | |
251 | } | |
252 | } | |
253 | my @revs; | |
8de010ad | 254 | foreach my $c (@commits) { |
aef4e921 | 255 | my @tmp = command('rev-parse',$c); |
8de010ad EW |
256 | if (scalar @tmp == 1) { |
257 | push @revs, $tmp[0]; | |
258 | } elsif (scalar @tmp > 1) { | |
aef4e921 | 259 | push @revs, reverse(command('rev-list',@tmp)); |
8de010ad | 260 | } else { |
1ce255dc | 261 | fatal "Failed to rev-parse $c\n"; |
8de010ad | 262 | } |
3397f9df | 263 | } |
1ce255dc EW |
264 | my $gs = Git::SVN->new; |
265 | my ($r_last, $cmt_last) = $gs->last_rev_commit; | |
266 | $gs->fetch; | |
97f6987a | 267 | if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) { |
1ce255dc EW |
268 | fatal "There are new revisions that were fetched ", |
269 | "and need to be merged (or acknowledged) ", | |
270 | "before committing.\nlast rev: $r_last\n", | |
271 | " current: $gs->{last_rev}\n"; | |
a5e0cedc | 272 | } |
1ce255dc EW |
273 | $gs->set_tree($_) foreach @revs; |
274 | print "Done committing ",scalar @revs," revisions to SVN\n"; | |
a5e0cedc | 275 | } |
8f22562c | 276 | |
d7ad3bed EW |
277 | sub cmd_dcommit { |
278 | my $head = shift; | |
279 | my $gs = Git::SVN->new; | |
280 | $head ||= 'HEAD'; | |
281 | my @refs = command(qw/rev-list --no-merges/, $gs->refname."..$head"); | |
45bf473a | 282 | my $last_rev; |
b22d4497 | 283 | foreach my $d (reverse @refs) { |
aef4e921 | 284 | if (!verify_ref("$d~1")) { |
d7ad3bed EW |
285 | fatal "Commit $d\n", |
286 | "has no parent commit, and therefore ", | |
287 | "nothing to diff against.\n", | |
288 | "You should be working from a repository ", | |
289 | "originally created by git-svn\n"; | |
48d044b5 | 290 | } |
45bf473a EW |
291 | unless (defined $last_rev) { |
292 | (undef, $last_rev, undef) = cmt_metadata("$d~1"); | |
293 | unless (defined $last_rev) { | |
d7ad3bed EW |
294 | fatal "Unable to extract revision information ", |
295 | "from commit $d~1\n"; | |
45bf473a EW |
296 | } |
297 | } | |
b22d4497 EW |
298 | if ($_dry_run) { |
299 | print "diff-tree $d~1 $d\n"; | |
300 | } else { | |
d7ad3bed | 301 | my %ed_opts = ( r => $last_rev, |
61395354 EW |
302 | log => get_commit_entry($d)->{log}, |
303 | ra => $gs->ra, | |
304 | tree_a => "$d~1", | |
305 | tree_b => $d, | |
306 | editor_cb => sub { | |
307 | print "Committed r$_[0]\n"; | |
308 | $last_rev = $_[0]; }, | |
d3a840dc | 309 | svn_path => $gs->{path} ); |
61395354 | 310 | if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) { |
d7ad3bed EW |
311 | print "No changes\n$d~1 == $d\n"; |
312 | } | |
b22d4497 EW |
313 | } |
314 | } | |
315 | return if $_dry_run; | |
d7ad3bed EW |
316 | $gs->fetch; |
317 | # we always want to rebase against the current HEAD, not any | |
318 | # head that was passed to us | |
319 | my @diff = command('diff-tree', 'HEAD', $gs->refname, '--'); | |
b22d4497 EW |
320 | my @finish; |
321 | if (@diff) { | |
322 | @finish = qw/rebase/; | |
323 | push @finish, qw/--merge/ if $_merge; | |
324 | push @finish, "--strategy=$_strategy" if $_strategy; | |
d7ad3bed EW |
325 | print STDERR "W: HEAD and ", $gs->refname, " differ, ", |
326 | "using @finish:\n", "@diff"; | |
b22d4497 | 327 | } else { |
d7ad3bed EW |
328 | print "No changes between current HEAD and ", |
329 | $gs->refname, "\nResetting to the latest ", | |
330 | $gs->refname, "\n"; | |
4769489a | 331 | @finish = qw/reset --mixed/; |
b22d4497 | 332 | } |
d7ad3bed | 333 | command_noisy(@finish, $gs->refname); |
b22d4497 EW |
334 | } |
335 | ||
5969cbe1 EW |
336 | sub cmd_show_ignore { |
337 | my $gs = Git::SVN->new; | |
338 | my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum); | |
339 | $gs->traverse_ignore(\*STDOUT, '', $r); | |
a5e0cedc EW |
340 | } |
341 | ||
8164b652 | 342 | sub cmd_multi_init { |
9d55b41a | 343 | my $url = shift; |
98327e58 EW |
344 | unless (defined $_trunk || defined $_branches || defined $_tags) { |
345 | usage(1); | |
9d55b41a | 346 | } |
8164b652 EW |
347 | do_git_init_db(); |
348 | $_prefix = '' unless defined $_prefix; | |
706587fc | 349 | $url =~ s#/+$## if defined $url; |
98327e58 | 350 | if (defined $_trunk) { |
706587fc EW |
351 | my $trunk_ref = $_prefix . 'trunk'; |
352 | # try both old-style and new-style lookups: | |
353 | my $gs_trunk = eval { Git::SVN->new($trunk_ref) }; | |
8164b652 | 354 | unless ($gs_trunk) { |
706587fc EW |
355 | my ($trunk_url, $trunk_path) = |
356 | complete_svn_url($url, $_trunk); | |
357 | $gs_trunk = Git::SVN->init($trunk_url, $trunk_path, | |
358 | undef, $trunk_ref); | |
98327e58 | 359 | } |
c35b96e7 | 360 | } |
706587fc | 361 | return unless defined $_branches || defined $_tags; |
e7db67e6 EW |
362 | my $ra = $url ? Git::SVN::Ra->new($url) : undef; |
363 | complete_url_ls_init($ra, $_branches, '--branches/-b', $_prefix); | |
364 | complete_url_ls_init($ra, $_tags, '--tags/-t', $_prefix . 'tags/'); | |
9d55b41a EW |
365 | } |
366 | ||
1c8443b0 | 367 | sub cmd_multi_fetch { |
0af9c9f9 EW |
368 | my $remotes = Git::SVN::read_all_remotes(); |
369 | foreach my $repo_id (sort keys %$remotes) { | |
370 | my $url = $remotes->{$repo_id}->{url} or next; | |
371 | my $fetch = $remotes->{$repo_id}->{fetch} or next; | |
372 | Git::SVN::fetch_all($repo_id, $url, $fetch); | |
9d55b41a | 373 | } |
9d55b41a EW |
374 | } |
375 | ||
44320b9e EW |
376 | # this command is special because it requires no metadata |
377 | sub cmd_commit_diff { | |
378 | my ($ta, $tb, $url) = @_; | |
379 | my $usage = "Usage: $0 commit-diff -r<revision> ". | |
380 | "<tree-ish> <tree-ish> [<URL>]\n"; | |
381 | fatal($usage) if (!defined $ta || !defined $tb); | |
d3a840dc | 382 | my $svn_path; |
44320b9e EW |
383 | if (!defined $url) { |
384 | my $gs = eval { Git::SVN->new }; | |
385 | if (!$gs) { | |
386 | fatal("Needed URL or usable git-svn --id in ", | |
387 | "the command-line\n", $usage); | |
388 | } | |
389 | $url = $gs->{url}; | |
d3a840dc | 390 | $svn_path = $gs->{path}; |
44320b9e EW |
391 | } |
392 | unless (defined $_revision) { | |
393 | fatal("-r|--revision is a required argument\n", $usage); | |
394 | } | |
395 | if (defined $_message && defined $_file) { | |
396 | fatal("Both --message/-m and --file/-F specified ", | |
397 | "for the commit message.\n", | |
398 | "I have no idea what you mean\n"); | |
399 | } | |
400 | if (defined $_file) { | |
401 | $_message = file_to_s($_file); | |
402 | } else { | |
403 | $_message ||= get_commit_entry($tb)->{log}; | |
404 | } | |
405 | my $ra ||= Git::SVN::Ra->new($url); | |
d3a840dc | 406 | $svn_path ||= $ra->{svn_path}; |
44320b9e EW |
407 | my $r = $_revision; |
408 | if ($r eq 'HEAD') { | |
409 | $r = $ra->get_latest_revnum; | |
410 | } elsif ($r !~ /^\d+$/) { | |
411 | die "revision argument: $r not understood by git-svn\n"; | |
412 | } | |
61395354 EW |
413 | my %ed_opts = ( r => $r, |
414 | log => $_message, | |
415 | ra => $ra, | |
416 | tree_a => $ta, | |
417 | tree_b => $tb, | |
418 | editor_cb => sub { print "Committed r$_[0]\n" }, | |
419 | svn_path => $svn_path ); | |
420 | if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) { | |
44320b9e EW |
421 | print "No changes\n$ta == $tb\n"; |
422 | } | |
44320b9e EW |
423 | } |
424 | ||
3397f9df EW |
425 | ########################### utility functions ######################### |
426 | ||
3ebe8df7 EW |
427 | sub parse_revision_argument { |
428 | if (!defined $_revision || $_revision eq 'BASE:HEAD') { | |
429 | return (undef, undef); | |
430 | } | |
431 | return ($1, $2) if ($_revision =~ /^(\d+):(\d+)$/); | |
432 | return ($_revision, $_revision) if ($_revision =~ /^\d+$/); | |
433 | return (undef, $1) if ($_revision =~ /^BASE:(\d+)$/); | |
434 | return ($1, undef) if ($_revision =~ /^(\d+):HEAD$/); | |
435 | die "revision argument: $_revision not understood by git-svn\n", | |
436 | "Try using the command-line svn client instead\n"; | |
437 | } | |
438 | ||
98327e58 EW |
439 | sub complete_svn_url { |
440 | my ($url, $path) = @_; | |
441 | $path =~ s#/+$##; | |
98327e58 | 442 | if ($path !~ m#^[a-z\+]+://#) { |
98327e58 EW |
443 | if (!defined $url || $url !~ m#^[a-z\+]+://#) { |
444 | fatal("E: '$path' is not a complete URL ", | |
445 | "and a separate URL is not specified\n"); | |
446 | } | |
706587fc | 447 | return ($url, $path); |
98327e58 | 448 | } |
706587fc | 449 | return ($path, ''); |
98327e58 EW |
450 | } |
451 | ||
9d55b41a | 452 | sub complete_url_ls_init { |
706587fc EW |
453 | my ($ra, $repo_path, $switch, $pfx) = @_; |
454 | unless ($repo_path) { | |
9d55b41a EW |
455 | print STDERR "W: $switch not specified\n"; |
456 | return; | |
457 | } | |
706587fc EW |
458 | $repo_path =~ s#/+$##; |
459 | if ($repo_path =~ m#^[a-z\+]+://#) { | |
460 | $ra = Git::SVN::Ra->new($repo_path); | |
461 | $repo_path = ''; | |
e7db67e6 | 462 | } else { |
706587fc | 463 | $repo_path =~ s#^/+##; |
e7db67e6 | 464 | unless ($ra) { |
706587fc | 465 | fatal("E: '$repo_path' is not a complete URL ", |
e7db67e6 | 466 | "and a separate URL is not specified\n"); |
8164b652 | 467 | } |
e7db67e6 EW |
468 | } |
469 | my $r = defined $_revision ? $_revision : $ra->get_latest_revnum; | |
706587fc EW |
470 | my ($dirent, undef, undef) = $ra->get_dir($repo_path, $r); |
471 | my $url = $ra->{url}; | |
88cf4107 EW |
472 | my $remote_id; |
473 | my $remote_path; | |
e7db67e6 EW |
474 | foreach my $d (sort keys %$dirent) { |
475 | next if ($dirent->{$d}->kind != $SVN::Node::dir); | |
706587fc EW |
476 | my $path = "$repo_path/$d"; |
477 | my $ref = "$pfx$d"; | |
478 | my $gs = eval { Git::SVN->new($ref) }; | |
e7db67e6 | 479 | # don't try to init already existing refs |
8164b652 | 480 | unless ($gs) { |
706587fc | 481 | print "init $url/$path => $ref\n"; |
88cf4107 | 482 | $gs = Git::SVN->init($url, $path, undef, $ref); |
9d55b41a | 483 | } |
88cf4107 EW |
484 | $remote_id ||= $gs->{repo_id} if $gs; |
485 | } | |
486 | if (defined $remote_id) { | |
487 | $remote_path = "$ra->{svn_path}/$repo_path/*"; | |
488 | $remote_path =~ s#/+#/#g; | |
489 | $remote_path =~ s#^/##g; | |
490 | my ($n) = ($switch =~ /^--(\w+)/); | |
491 | command_noisy('config', "svn-remote.$remote_id.$n", | |
492 | $remote_path); | |
9d55b41a | 493 | } |
9d55b41a EW |
494 | } |
495 | ||
aef4e921 EW |
496 | sub verify_ref { |
497 | my ($ref) = @_; | |
2c5c1d53 EW |
498 | eval { command_oneline([ 'rev-parse', '--verify', $ref ], |
499 | { STDERR => 0 }); }; | |
aef4e921 EW |
500 | } |
501 | ||
a5e0cedc | 502 | sub get_tree_from_treeish { |
cf52b8f0 | 503 | my ($treeish) = @_; |
44320b9e | 504 | # $treeish can be a symbolic ref, too: |
aef4e921 | 505 | my $type = command_oneline(qw/cat-file -t/, $treeish); |
cf52b8f0 EW |
506 | my $expected; |
507 | while ($type eq 'tag') { | |
aef4e921 | 508 | ($treeish, $type) = command(qw/cat-file tag/, $treeish); |
cf52b8f0 EW |
509 | } |
510 | if ($type eq 'commit') { | |
aef4e921 EW |
511 | $expected = (grep /^tree /, command(qw/cat-file commit/, |
512 | $treeish))[0]; | |
44320b9e | 513 | ($expected) = ($expected =~ /^tree ($sha1)$/o); |
cf52b8f0 EW |
514 | die "Unable to get tree from $treeish\n" unless $expected; |
515 | } elsif ($type eq 'tree') { | |
516 | $expected = $treeish; | |
517 | } else { | |
518 | die "$treeish is a $type, expected tree, tag or commit\n"; | |
519 | } | |
a5e0cedc EW |
520 | return $expected; |
521 | } | |
522 | ||
44320b9e EW |
523 | sub get_commit_entry { |
524 | my ($treeish) = shift; | |
525 | my %log_entry = ( log => '', tree => get_tree_from_treeish($treeish) ); | |
526 | my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG"; | |
527 | my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG"; | |
528 | open my $log_fh, '>', $commit_editmsg or croak $!; | |
3397f9df | 529 | |
44320b9e | 530 | my $type = command_oneline(qw/cat-file -t/, $treeish); |
4ad4515d | 531 | if ($type eq 'commit' || $type eq 'tag') { |
aef4e921 | 532 | my ($msg_fh, $ctx) = command_output_pipe('cat-file', |
44320b9e | 533 | $type, $treeish); |
3397f9df EW |
534 | my $in_msg = 0; |
535 | while (<$msg_fh>) { | |
536 | if (!$in_msg) { | |
537 | $in_msg = 1 if (/^\s*$/); | |
df746c5a | 538 | } elsif (/^git-svn-id: /) { |
44320b9e EW |
539 | # skip this for now, we regenerate the |
540 | # correct one on re-fetch anyways | |
541 | # TODO: set *:merge properties or like... | |
3397f9df | 542 | } else { |
44320b9e | 543 | print $log_fh $_ or croak $!; |
3397f9df EW |
544 | } |
545 | } | |
aef4e921 | 546 | command_close_pipe($msg_fh, $ctx); |
3397f9df | 547 | } |
44320b9e | 548 | close $log_fh or croak $!; |
3397f9df EW |
549 | |
550 | if ($_edit || ($type eq 'tree')) { | |
551 | my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi'; | |
44320b9e EW |
552 | # TODO: strip out spaces, comments, like git-commit.sh |
553 | system($editor, $commit_editmsg); | |
3397f9df | 554 | } |
44320b9e EW |
555 | rename $commit_editmsg, $commit_msg or croak $!; |
556 | open $log_fh, '<', $commit_msg or croak $!; | |
557 | { local $/; chomp($log_entry{log} = <$log_fh>); } | |
558 | close $log_fh or croak $!; | |
559 | unlink $commit_msg; | |
560 | \%log_entry; | |
a5e0cedc EW |
561 | } |
562 | ||
3397f9df EW |
563 | sub s_to_file { |
564 | my ($str, $file, $mode) = @_; | |
565 | open my $fd,'>',$file or croak $!; | |
566 | print $fd $str,"\n" or croak $!; | |
567 | close $fd or croak $!; | |
568 | chmod ($mode &~ umask, $file) if (defined $mode); | |
569 | } | |
570 | ||
571 | sub file_to_s { | |
572 | my $file = shift; | |
573 | open my $fd,'<',$file or croak "$!: file: $file\n"; | |
574 | local $/; | |
575 | my $ret = <$fd>; | |
576 | close $fd or croak $!; | |
577 | $ret =~ s/\s*$//s; | |
578 | return $ret; | |
579 | } | |
580 | ||
eeb0abe0 EW |
581 | # '<svn username> = real-name <email address>' mapping based on git-svnimport: |
582 | sub load_authors { | |
583 | open my $authors, '<', $_authors or die "Can't open $_authors $!\n"; | |
f8c9d1d2 | 584 | my $log = $cmd eq 'log'; |
eeb0abe0 EW |
585 | while (<$authors>) { |
586 | chomp; | |
8815788e | 587 | next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/; |
eeb0abe0 | 588 | my ($user, $name, $email) = ($1, $2, $3); |
f8c9d1d2 EW |
589 | if ($log) { |
590 | $Git::SVN::Log::rusers{"$name <$email>"} = $user; | |
591 | } else { | |
592 | $users{$user} = [$name, $email]; | |
593 | } | |
79bb8d88 EW |
594 | } |
595 | close $authors or croak $!; | |
596 | } | |
597 | ||
e0d10e1c | 598 | # convert GetOpt::Long specs for use by git-config |
b8c92cad | 599 | sub read_repo_config { |
706587fc | 600 | return unless -d $ENV{GIT_DIR}; |
b8c92cad EW |
601 | my $opts = shift; |
602 | foreach my $o (keys %$opts) { | |
603 | my $v = $opts->{$o}; | |
604 | my ($key) = ($o =~ /^([a-z\-]+)/); | |
605 | $key =~ s/-//g; | |
e0d10e1c | 606 | my $arg = 'git-config'; |
b8c92cad EW |
607 | $arg .= ' --int' if ($o =~ /[:=]i$/); |
608 | $arg .= ' --bool' if ($o !~ /[:=][sfi]$/); | |
609 | if (ref $v eq 'ARRAY') { | |
610 | chomp(my @tmp = `$arg --get-all svn.$key`); | |
611 | @$v = @tmp if @tmp; | |
612 | } else { | |
613 | chomp(my $tmp = `$arg --get svn.$key`); | |
7774284a | 614 | if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) { |
b8c92cad EW |
615 | $$v = $tmp; |
616 | } | |
617 | } | |
618 | } | |
619 | } | |
620 | ||
79bb8d88 | 621 | sub extract_metadata { |
c1927a85 | 622 | my $id = shift or return (undef, undef, undef); |
79bb8d88 EW |
623 | my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+) |
624 | \s([a-f\d\-]+)$/x); | |
e70dc780 | 625 | if (!defined $rev || !$uuid || !$url) { |
79bb8d88 | 626 | # some of the original repositories I made had |
82e5a82f | 627 | # identifiers like this: |
79bb8d88 EW |
628 | ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/); |
629 | } | |
630 | return ($url, $rev, $uuid); | |
631 | } | |
632 | ||
c1927a85 EW |
633 | sub cmt_metadata { |
634 | return extract_metadata((grep(/^git-svn-id: /, | |
aef4e921 | 635 | command(qw/cat-file commit/, shift)))[-1]); |
c1927a85 EW |
636 | } |
637 | ||
9b981fc6 EW |
638 | package Git::SVN; |
639 | use strict; | |
640 | use warnings; | |
ecc712dd EW |
641 | use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent |
642 | $_repack $_repack_flags/; | |
9b981fc6 EW |
643 | use Carp qw/croak/; |
644 | use File::Path qw/mkpath/; | |
373274f9 | 645 | use File::Copy qw/copy/; |
9b981fc6 EW |
646 | use IPC::Open3; |
647 | ||
ecc712dd | 648 | my $_repack_nr; |
9b981fc6 EW |
649 | # properties that we do not log: |
650 | my %SKIP_PROP; | |
651 | BEGIN { | |
652 | %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url | |
653 | svn:special svn:executable | |
654 | svn:entry:committed-rev | |
655 | svn:entry:last-author | |
656 | svn:entry:uuid | |
657 | svn:entry:committed-date/; | |
658 | } | |
659 | ||
373274f9 EW |
660 | my %LOCKFILES; |
661 | END { unlink keys %LOCKFILES if %LOCKFILES } | |
662 | ||
0af9c9f9 EW |
663 | sub fetch_all { |
664 | my ($repo_id, $url, $fetch) = @_; | |
665 | my @gs; | |
666 | my $ra = Git::SVN::Ra->new($url); | |
667 | my $head = $ra->get_latest_revnum; | |
668 | my $base = $head; | |
0af9c9f9 EW |
669 | foreach my $p (sort keys %$fetch) { |
670 | my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p); | |
9c93fee5 | 671 | my $lr = $gs->rev_db_max; |
0af9c9f9 EW |
672 | if (defined $lr) { |
673 | $base = $lr if ($lr < $base); | |
0af9c9f9 EW |
674 | } |
675 | push @gs, $gs; | |
676 | } | |
0af9c9f9 EW |
677 | return if (++$base > $head); |
678 | $ra->gs_fetch_loop_common($base, $head, @gs); | |
679 | } | |
680 | ||
47e39c55 EW |
681 | sub read_all_remotes { |
682 | my $r = {}; | |
8b8fc068 | 683 | foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) { |
47e39c55 EW |
684 | if (m!^(.+)\.fetch=\s*(.*)\s*:\s*refs/remotes/(.+)\s*$!) { |
685 | $r->{$1}->{fetch}->{$2} = $3; | |
686 | } elsif (m!^(.+)\.url=\s*(.*)\s*$!) { | |
687 | $r->{$1}->{url} = $2; | |
688 | } | |
689 | } | |
690 | $r; | |
691 | } | |
692 | ||
ecc712dd EW |
693 | sub init_vars { |
694 | if (defined $_repack) { | |
695 | $_repack = 1000 if ($_repack <= 0); | |
696 | $_repack_nr = $_repack; | |
697 | $_repack_flags ||= '-d'; | |
698 | } | |
699 | } | |
700 | ||
b805b44a | 701 | sub verify_remotes_sanity { |
536c4b09 | 702 | return unless -d $ENV{GIT_DIR}; |
b805b44a EW |
703 | my %seen; |
704 | foreach (command(qw/config -l/)) { | |
705 | if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) { | |
706 | if ($seen{$1}) { | |
707 | die "Remote ref refs/remote/$1 is tracked by", | |
708 | "\n \"$_\"\nand\n \"$seen{$1}\"\n", | |
709 | "Please resolve this ambiguity in ", | |
710 | "your git configuration file before ", | |
711 | "continuing\n"; | |
712 | } | |
713 | $seen{$1} = $_; | |
714 | } | |
715 | } | |
716 | } | |
717 | ||
47e39c55 | 718 | # we allow more chars than remotes2config.sh... |
706587fc EW |
719 | sub sanitize_remote_name { |
720 | my ($name) = @_; | |
47e39c55 | 721 | $name =~ tr{A-Za-z0-9:,/+-}{.}c; |
706587fc EW |
722 | $name; |
723 | } | |
724 | ||
e6434f87 EW |
725 | sub find_existing_remote { |
726 | my ($url, $remotes) = @_; | |
727 | my $existing; | |
728 | foreach my $repo_id (keys %$remotes) { | |
729 | my $u = $remotes->{$repo_id}->{url} or next; | |
730 | next if $u ne $url; | |
731 | $existing = $repo_id; | |
732 | last; | |
733 | } | |
734 | $existing; | |
735 | } | |
b805b44a | 736 | |
e6434f87 | 737 | sub init_remote_config { |
d8115c51 | 738 | my ($self, $url, $no_write) = @_; |
e6434f87 EW |
739 | $url =~ s!/+$!!; # strip trailing slash |
740 | my $r = read_all_remotes(); | |
741 | my $existing = find_existing_remote($url, $r); | |
742 | if ($existing) { | |
743 | print STDERR "Using existing ", | |
744 | "[svn-remote \"$existing\"]\n"; | |
745 | $self->{repo_id} = $existing; | |
746 | } else { | |
747 | my $min_url = Git::SVN::Ra->new($url)->minimize_url; | |
748 | $existing = find_existing_remote($min_url, $r); | |
749 | if ($existing) { | |
750 | print STDERR "Using existing ", | |
751 | "[svn-remote \"$existing\"]\n"; | |
752 | $self->{repo_id} = $existing; | |
753 | } | |
754 | if ($min_url ne $url) { | |
755 | print STDERR "Using higher level of URL: ", | |
756 | "$url => $min_url\n"; | |
757 | my $old_path = $self->{path}; | |
758 | $self->{path} = $url; | |
759 | $self->{path} =~ s!^\Q$min_url\E/*!!; | |
760 | if (length $old_path) { | |
761 | $self->{path} .= "/$old_path"; | |
762 | } | |
763 | $url = $min_url; | |
764 | } | |
765 | } | |
766 | my $orig_url; | |
767 | if (!$existing) { | |
b805b44a | 768 | # verify that we aren't overwriting anything: |
e6434f87 | 769 | $orig_url = eval { |
706587fc | 770 | command_oneline('config', '--get', |
e6434f87 | 771 | "svn-remote.$self->{repo_id}.url") |
706587fc | 772 | }; |
b805b44a | 773 | if ($orig_url && ($orig_url ne $url)) { |
e6434f87 | 774 | die "svn-remote.$self->{repo_id}.url already set: ", |
b805b44a EW |
775 | "$orig_url\nwanted to set to: $url\n"; |
776 | } | |
9b981fc6 | 777 | } |
e6434f87 EW |
778 | my ($xrepo_id, $xpath) = find_ref($self->refname); |
779 | if (defined $xpath) { | |
780 | die "svn-remote.$xrepo_id.fetch already set to track ", | |
781 | "$xpath:refs/remotes/", $self->refname, "\n"; | |
782 | } | |
d8115c51 EW |
783 | unless ($no_write) { |
784 | command_noisy('config', | |
785 | "svn-remote.$self->{repo_id}.url", $url); | |
786 | command_noisy('config', '--add', | |
787 | "svn-remote.$self->{repo_id}.fetch", | |
788 | "$self->{path}:".$self->refname); | |
789 | } | |
9b981fc6 | 790 | $self->{url} = $url; |
e6434f87 EW |
791 | } |
792 | ||
793 | sub init { | |
d8115c51 | 794 | my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_; |
e6434f87 EW |
795 | my $self = _new($class, $repo_id, $ref_id, $path); |
796 | if (defined $url) { | |
d8115c51 | 797 | $self->init_remote_config($url, $no_write); |
e6434f87 | 798 | } |
9b981fc6 EW |
799 | $self; |
800 | } | |
801 | ||
706587fc EW |
802 | sub find_ref { |
803 | my ($ref_id) = @_; | |
804 | foreach (command(qw/config -l/)) { | |
805 | next unless m!^svn-remote\.(.+)\.fetch= | |
806 | \s*(.*)\s*:\s*refs/remotes/(.+)\s*$!x; | |
807 | my ($repo_id, $path, $ref) = ($1, $2, $3); | |
808 | if ($ref eq $ref_id) { | |
809 | $path = '' if ($path =~ m#^\./?#); | |
810 | return ($repo_id, $path); | |
811 | } | |
812 | } | |
813 | (undef, undef, undef); | |
814 | } | |
815 | ||
9b981fc6 | 816 | sub new { |
706587fc EW |
817 | my ($class, $ref_id, $repo_id, $path) = @_; |
818 | if (defined $ref_id && !defined $repo_id && !defined $path) { | |
819 | ($repo_id, $path) = find_ref($ref_id); | |
820 | if (!defined $repo_id) { | |
821 | die "Could not find a \"svn-remote.*.fetch\" key ", | |
822 | "in the repository configuration matching: ", | |
823 | "refs/remotes/$ref_id\n"; | |
824 | } | |
825 | } | |
826 | my $self = _new($class, $repo_id, $ref_id, $path); | |
8b8fc068 EW |
827 | if (!defined $self->{path} || !length $self->{path}) { |
828 | my $fetch = command_oneline('config', '--get', | |
829 | "svn-remote.$repo_id.fetch", | |
830 | ":refs/remotes/$ref_id\$") or | |
831 | die "Failed to read \"svn-remote.$repo_id.fetch\" ", | |
832 | "\":refs/remotes/$ref_id\$\" in config\n"; | |
833 | ($self->{path}, undef) = split(/\s*:\s*/, $fetch); | |
834 | } | |
706587fc EW |
835 | $self->{url} = command_oneline('config', '--get', |
836 | "svn-remote.$repo_id.url") or | |
837 | die "Failed to read \"svn-remote.$repo_id.url\" in config\n"; | |
f0ecca10 EW |
838 | if (-z $self->{db_path} && ::verify_ref($self->refname.'^0')) { |
839 | $self->rebuild; | |
840 | } | |
9b981fc6 EW |
841 | $self; |
842 | } | |
843 | ||
706587fc | 844 | sub refname { "refs/remotes/$_[0]->{ref_id}" } |
9b981fc6 EW |
845 | |
846 | sub ra { | |
847 | my ($self) = shift; | |
5d3b7cd5 | 848 | Git::SVN::Ra->new($self->{url}); |
9b981fc6 EW |
849 | } |
850 | ||
15710b6f EW |
851 | sub rel_path { |
852 | my ($self) = @_; | |
853 | my $repos_root = $self->ra->{repos_root}; | |
854 | return $self->{path} if ($self->{url} eq $repos_root); | |
855 | my $url = $self->{url} . | |
856 | (length $self->{path} ? "/$self->{path}" : $self->{path}); | |
857 | $url =~ s!^\Q$repos_root\E/*!!g; | |
858 | $url; | |
859 | } | |
860 | ||
9b981fc6 EW |
861 | sub traverse_ignore { |
862 | my ($self, $fh, $path, $r) = @_; | |
863 | $path =~ s#^/+##g; | |
5d3b7cd5 EW |
864 | my $ra = $self->ra; |
865 | my ($dirent, undef, $props) = $ra->get_dir($path, $r); | |
9b981fc6 | 866 | my $p = $path; |
5d3b7cd5 | 867 | $p =~ s#^\Q$ra->{svn_path}\E/##; |
9b981fc6 EW |
868 | print $fh length $p ? "\n# $p\n" : "\n# /\n"; |
869 | if (my $s = $props->{'svn:ignore'}) { | |
870 | $s =~ s/[\r\n]+/\n/g; | |
871 | chomp $s; | |
872 | if (length $p == 0) { | |
873 | $s =~ s#\n#\n/$p#g; | |
874 | print $fh "/$s\n"; | |
875 | } else { | |
876 | $s =~ s#\n#\n/$p/#g; | |
877 | print $fh "/$p/$s\n"; | |
878 | } | |
879 | } | |
880 | foreach (sort keys %$dirent) { | |
881 | next if $dirent->{$_}->kind != $SVN::Node::dir; | |
882 | $self->traverse_ignore($fh, "$path/$_", $r); | |
883 | } | |
884 | } | |
885 | ||
3ebe8df7 EW |
886 | sub last_rev { ($_[0]->last_rev_commit)[0] } |
887 | sub last_commit { ($_[0]->last_rev_commit)[1] } | |
888 | ||
9b981fc6 EW |
889 | # returns the newest SVN revision number and newest commit SHA1 |
890 | sub last_rev_commit { | |
891 | my ($self) = @_; | |
892 | if (defined $self->{last_rev} && defined $self->{last_commit}) { | |
893 | return ($self->{last_rev}, $self->{last_commit}); | |
894 | } | |
d2866f9e | 895 | my $c = ::verify_ref($self->refname.'^0'); |
706587fc | 896 | if ($c) { |
d2866f9e | 897 | my $rev = (::cmt_metadata($c))[1]; |
9b981fc6 EW |
898 | if (defined $rev) { |
899 | ($self->{last_rev}, $self->{last_commit}) = ($rev, $c); | |
900 | return ($rev, $c); | |
901 | } | |
902 | } | |
903 | my $offset = -41; # from tail | |
904 | my $rl; | |
905 | open my $fh, '<', $self->{db_path} or | |
906 | croak "$self->{db_path} not readable: $!\n"; | |
ce4b4af7 EW |
907 | sysseek($fh, $offset, 2); # don't care for errors |
908 | sysread($fh, $rl, 41) == 41 or return (undef, undef); | |
9b981fc6 | 909 | chomp $rl; |
ce4b4af7 | 910 | while (('0' x40) eq $rl && sysseek($fh, 0, 1) != 0) { |
9b981fc6 | 911 | $offset -= 41; |
ce4b4af7 EW |
912 | sysseek($fh, $offset, 2); # don't care for errors |
913 | sysread($fh, $rl, 41) == 41 or return (undef, undef); | |
9b981fc6 EW |
914 | chomp $rl; |
915 | } | |
9c93fee5 EW |
916 | if ($c) { |
917 | die "$self->{db_path} and ", $self->refname, | |
918 | " inconsistent!:\n$c != $rl\n"; | |
919 | } | |
ce4b4af7 | 920 | my $rev = sysseek($fh, 0, 1) or croak $!; |
9b981fc6 EW |
921 | $rev = ($rev - 41) / 41; |
922 | close $fh or croak $!; | |
923 | ($self->{last_rev}, $self->{last_commit}) = ($rev, $c); | |
924 | return ($rev, $c); | |
925 | } | |
926 | ||
3ebe8df7 EW |
927 | sub get_fetch_range { |
928 | my ($self, $min, $max) = @_; | |
929 | $max ||= $self->ra->get_latest_revnum; | |
9c93fee5 | 930 | $min ||= $self->rev_db_max; |
3ebe8df7 | 931 | (++$min, $max); |
9b981fc6 EW |
932 | } |
933 | ||
934 | sub tmp_index_do { | |
935 | my ($self, $sub) = @_; | |
936 | my $old_index = $ENV{GIT_INDEX_FILE}; | |
937 | $ENV{GIT_INDEX_FILE} = $self->{index}; | |
938 | my @ret = &$sub; | |
939 | if ($old_index) { | |
940 | $ENV{GIT_INDEX_FILE} = $old_index; | |
941 | } else { | |
942 | delete $ENV{GIT_INDEX_FILE}; | |
943 | } | |
944 | wantarray ? @ret : $ret[0]; | |
945 | } | |
946 | ||
947 | sub assert_index_clean { | |
948 | my ($self, $treeish) = @_; | |
949 | ||
950 | $self->tmp_index_do(sub { | |
951 | command_noisy('read-tree', $treeish) unless -e $self->{index}; | |
952 | my $x = command_oneline('write-tree'); | |
953 | my ($y) = (command(qw/cat-file commit/, $treeish) =~ | |
954 | /^tree ($::sha1)/mo); | |
955 | if ($y ne $x) { | |
956 | unlink $self->{index} or croak $!; | |
957 | command_noisy('read-tree', $treeish); | |
958 | } | |
959 | $x = command_oneline('write-tree'); | |
960 | if ($y ne $x) { | |
961 | ::fatal "trees ($treeish) $y != $x\n", | |
962 | "Something is seriously wrong...\n"; | |
963 | } | |
964 | }); | |
965 | } | |
966 | ||
967 | sub get_commit_parents { | |
0af9c9f9 | 968 | my ($self, $log_entry) = @_; |
9b981fc6 | 969 | my (%seen, @ret, @tmp); |
0af9c9f9 EW |
970 | # legacy support for 'set-tree'; this is only used by set_tree_cb: |
971 | if (my $ip = $self->{inject_parents}) { | |
972 | if (my $commit = delete $ip->{$log_entry->{revision}}) { | |
973 | push @tmp, $commit; | |
9b981fc6 EW |
974 | } |
975 | } | |
d2866f9e | 976 | if (my $cur = ::verify_ref($self->refname.'^0')) { |
9b981fc6 EW |
977 | push @tmp, $cur; |
978 | } | |
44320b9e | 979 | push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp); |
9b981fc6 EW |
980 | while (my $p = shift @tmp) { |
981 | next if $seen{$p}; | |
982 | $seen{$p} = 1; | |
983 | push @ret, $p; | |
984 | # MAXPARENT is defined to 16 in commit-tree.c: | |
985 | last if @ret >= 16; | |
986 | } | |
987 | if (@tmp) { | |
44320b9e | 988 | die "r$log_entry->{revision}: No room for parents:\n\t", |
9b981fc6 EW |
989 | join("\n\t", @tmp), "\n"; |
990 | } | |
991 | @ret; | |
992 | } | |
993 | ||
706587fc | 994 | sub full_url { |
9b981fc6 | 995 | my ($self) = @_; |
5d3b7cd5 | 996 | $self->{url} . (length $self->{path} ? '/' . $self->{path} : ''); |
9b981fc6 EW |
997 | } |
998 | ||
999 | sub do_git_commit { | |
0af9c9f9 | 1000 | my ($self, $log_entry) = @_; |
8a603774 EW |
1001 | my $lr = $self->last_rev; |
1002 | if (defined $lr && $lr >= $log_entry->{revision}) { | |
1003 | die "Last fetched revision of ", $self->refname, | |
1004 | " was r$lr, but we are about to fetch: ", | |
1005 | "r$log_entry->{revision}!\n"; | |
1006 | } | |
44320b9e EW |
1007 | if (my $c = $self->rev_db_get($log_entry->{revision})) { |
1008 | croak "$log_entry->{revision} = $c already exists! ", | |
9b981fc6 EW |
1009 | "Why are we refetching it?\n"; |
1010 | } | |
1c8443b0 EW |
1011 | my $author = $log_entry->{author}; |
1012 | my ($name, $email) = (defined $::users{$author} ? @{$::users{$author}} | |
1013 | : ($author, "$author\@".$self->ra->uuid)); | |
9b981fc6 EW |
1014 | $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $name; |
1015 | $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} = $email; | |
44320b9e | 1016 | $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date}; |
9b981fc6 | 1017 | |
44320b9e | 1018 | my $tree = $log_entry->{tree}; |
9b981fc6 EW |
1019 | if (!defined $tree) { |
1020 | $tree = $self->tmp_index_do(sub { | |
1021 | command_oneline('write-tree') }); | |
1022 | } | |
1023 | die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o; | |
1024 | ||
1025 | my @exec = ('git-commit-tree', $tree); | |
0af9c9f9 | 1026 | foreach ($self->get_commit_parents($log_entry)) { |
9b981fc6 EW |
1027 | push @exec, '-p', $_; |
1028 | } | |
1029 | defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec)) | |
1030 | or croak $!; | |
44320b9e | 1031 | print $msg_fh $log_entry->{log} or croak $!; |
9760adcc EW |
1032 | unless ($_no_metadata) { |
1033 | print $msg_fh "\ngit-svn-id: ", $self->full_url, '@', | |
1034 | $log_entry->{revision}, ' ', | |
1035 | $self->ra->uuid, "\n" or croak $!; | |
1036 | } | |
9b981fc6 EW |
1037 | $msg_fh->flush == 0 or croak $!; |
1038 | close $msg_fh or croak $!; | |
1039 | chomp(my $commit = do { local $/; <$out_fh> }); | |
1040 | close $out_fh or croak $!; | |
1041 | waitpid $pid, 0; | |
1042 | croak $? if $?; | |
1043 | if ($commit !~ /^$::sha1$/o) { | |
1044 | die "Failed to commit, invalid sha1: $commit\n"; | |
1045 | } | |
1046 | ||
373274f9 | 1047 | $self->rev_db_set($log_entry->{revision}, $commit, 1); |
9b981fc6 | 1048 | |
44320b9e | 1049 | $self->{last_rev} = $log_entry->{revision}; |
9b981fc6 | 1050 | $self->{last_commit} = $commit; |
8a603774 | 1051 | print "r$log_entry->{revision} = $commit ($self->{ref_id})\n"; |
ecc712dd EW |
1052 | if (defined $_repack && (--$_repack_nr == 0)) { |
1053 | $_repack_nr = $_repack; | |
1054 | # repack doesn't use any arguments with spaces in them, does it? | |
1055 | print "Running git repack $_repack_flags ...\n"; | |
1056 | command_noisy('repack', split(/\s+/, $_repack_flags)); | |
1057 | print "Done repacking\n"; | |
1058 | } | |
9b981fc6 EW |
1059 | return $commit; |
1060 | } | |
1061 | ||
15710b6f EW |
1062 | sub find_parent_branch { |
1063 | my ($self, $paths, $rev) = @_; | |
9760adcc | 1064 | return undef unless $_follow_parent; |
e5a0b240 | 1065 | unless (defined $paths) { |
c7eba716 EW |
1066 | my $err_handler = $SVN::Error::handler; |
1067 | $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs; | |
d4eff2bd EW |
1068 | $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1, sub { |
1069 | $paths = | |
1070 | Git::SVN::Ra::dup_changed_paths($_[0]) }); | |
c7eba716 | 1071 | $SVN::Error::handler = $err_handler; |
e5a0b240 EW |
1072 | } |
1073 | return undef unless defined $paths; | |
15710b6f EW |
1074 | |
1075 | # look for a parent from another branch: | |
7f578c55 EW |
1076 | my @b_path_components = split m#/#, $self->rel_path; |
1077 | my @a_path_components; | |
1078 | my $i; | |
1079 | while (@b_path_components) { | |
1080 | $i = $paths->{'/'.join('/', @b_path_components)}; | |
1081 | last if $i; | |
1082 | unshift(@a_path_components, pop(@b_path_components)); | |
1083 | } | |
1084 | goto not_found unless defined $i; | |
3ebe8df7 | 1085 | my $branch_from = $i->{copyfrom_path} or goto not_found; |
7f578c55 EW |
1086 | if (@a_path_components) { |
1087 | print STDERR "branch_from: $branch_from => "; | |
1088 | $branch_from .= '/'.join('/', @a_path_components); | |
1089 | print STDERR $branch_from, "\n"; | |
1090 | } | |
3ebe8df7 | 1091 | my $r = $i->{copyfrom_rev}; |
15710b6f EW |
1092 | my $repos_root = $self->ra->{repos_root}; |
1093 | my $url = $self->ra->{url}; | |
1094 | my $new_url = $repos_root . $branch_from; | |
1095 | print STDERR "Found possible branch point: ", | |
1096 | "$new_url => ", $self->full_url, ", $r\n"; | |
1097 | $branch_from =~ s#^/##; | |
1098 | my $remotes = read_all_remotes(); | |
1099 | my $gs; | |
1100 | foreach my $repo_id (keys %$remotes) { | |
1101 | my $u = $remotes->{$repo_id}->{url} or next; | |
1102 | next if $url ne $u; | |
1103 | my $fetch = $remotes->{$repo_id}->{fetch}; | |
1104 | foreach my $f (keys %$fetch) { | |
1105 | next if $f ne $branch_from; | |
1106 | $gs = Git::SVN->new($fetch->{$f}, $repo_id, $f); | |
1107 | last; | |
1108 | } | |
1109 | last if $gs; | |
1110 | } | |
1111 | unless ($gs) { | |
ce2a0f2f EW |
1112 | my $ref_id = $self->{ref_id}; |
1113 | $ref_id =~ s/\@\d+$//; | |
1114 | $ref_id .= "\@$r"; | |
15710b6f EW |
1115 | # just grow a tail if we're not unique enough :x |
1116 | $ref_id .= '-' while find_ref($ref_id); | |
ce2a0f2f | 1117 | print STDERR "Initializing parent: $ref_id\n"; |
d8115c51 | 1118 | $gs = Git::SVN->init($new_url, '', $ref_id, $ref_id, 1); |
15710b6f EW |
1119 | } |
1120 | my ($r0, $parent) = $gs->find_rev_before($r, 1); | |
9760adcc | 1121 | if ($_follow_parent && (!defined $r0 || !defined $parent)) { |
3ebe8df7 | 1122 | $gs->fetch(0, $r); |
15710b6f EW |
1123 | ($r0, $parent) = $gs->last_rev_commit; |
1124 | } | |
ef70de96 | 1125 | if (defined $r0 && defined $parent) { |
15710b6f | 1126 | print STDERR "Found branch parent: ($self->{ref_id}) $parent\n"; |
a2003abc | 1127 | $self->assert_index_clean($parent); |
15710b6f EW |
1128 | my $ed; |
1129 | if ($self->ra->can_do_switch) { | |
8b8fc068 | 1130 | print STDERR "Following parent with do_switch\n"; |
15710b6f | 1131 | # do_switch works with svn/trunk >= r22312, but that |
2b27f6c8 | 1132 | # is not included with SVN 1.4.3 (the latest version |
15710b6f EW |
1133 | # at the moment), so we can't rely on it |
1134 | $self->{last_commit} = $parent; | |
1135 | $ed = SVN::Git::Fetcher->new($self); | |
8a603774 | 1136 | $gs->ra->gs_do_switch($r0, $rev, $gs, |
15710b6f EW |
1137 | $self->full_url, $ed) |
1138 | or die "SVN connection failed somewhere...\n"; | |
1139 | } else { | |
8b8fc068 | 1140 | print STDERR "Following parent with do_update\n"; |
15710b6f | 1141 | $ed = SVN::Git::Fetcher->new($self); |
8a603774 | 1142 | $self->ra->gs_do_update($rev, $rev, $self, $ed) |
15710b6f EW |
1143 | or die "SVN connection failed somewhere...\n"; |
1144 | } | |
f7c3fc4a | 1145 | print STDERR "Successfully followed parent\n"; |
15710b6f EW |
1146 | return $self->make_log_entry($rev, [$parent], $ed); |
1147 | } | |
9bf04637 | 1148 | not_found: |
7f578c55 | 1149 | print STDERR "Branch parent for path: '/", |
3ebe8df7 EW |
1150 | $self->rel_path, "' @ r$rev not found:\n"; |
1151 | return undef unless $paths; | |
1152 | print STDERR "Changed paths:\n"; | |
1153 | foreach my $x (sort keys %$paths) { | |
1154 | my $p = $paths->{$x}; | |
1155 | print STDERR "\t$p->{action}\t$x"; | |
1156 | if ($p->{copyfrom_path}) { | |
1157 | print STDERR "(from $p->{copyfrom_path}: ", | |
1158 | "$p->{copyfrom_rev})"; | |
1159 | } | |
1160 | print STDERR "\n"; | |
1161 | } | |
1162 | print STDERR '-'x72, "\n"; | |
15710b6f EW |
1163 | return undef; |
1164 | } | |
1165 | ||
9b981fc6 | 1166 | sub do_fetch { |
706587fc | 1167 | my ($self, $paths, $rev) = @_; |
15710b6f | 1168 | my $ed; |
9b981fc6 EW |
1169 | my ($last_rev, @parents); |
1170 | if ($self->{last_commit}) { | |
15710b6f | 1171 | $ed = SVN::Git::Fetcher->new($self); |
9b981fc6 EW |
1172 | $last_rev = $self->{last_rev}; |
1173 | $ed->{c} = $self->{last_commit}; | |
1174 | @parents = ($self->{last_commit}); | |
1175 | } else { | |
1176 | $last_rev = $rev; | |
15710b6f EW |
1177 | if (my $log_entry = $self->find_parent_branch($paths, $rev)) { |
1178 | return $log_entry; | |
1179 | } | |
1180 | $ed = SVN::Git::Fetcher->new($self); | |
9b981fc6 | 1181 | } |
8a603774 | 1182 | unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) { |
9b981fc6 EW |
1183 | die "SVN connection failed somewhere...\n"; |
1184 | } | |
1185 | $self->make_log_entry($rev, \@parents, $ed); | |
1186 | } | |
1187 | ||
97f6987a EW |
1188 | sub get_untracked { |
1189 | my ($self, $ed) = @_; | |
1190 | my @out; | |
1191 | my $h = $ed->{empty}; | |
9b981fc6 EW |
1192 | foreach (sort keys %$h) { |
1193 | my $act = $h->{$_} ? '+empty_dir' : '-empty_dir'; | |
97f6987a | 1194 | push @out, " $act: " . uri_encode($_); |
9b981fc6 EW |
1195 | warn "W: $act: $_\n"; |
1196 | } | |
1197 | foreach my $t (qw/dir_prop file_prop/) { | |
97f6987a | 1198 | $h = $ed->{$t} or next; |
9b981fc6 EW |
1199 | foreach my $path (sort keys %$h) { |
1200 | my $ppath = $path eq '' ? '.' : $path; | |
1201 | foreach my $prop (sort keys %{$h->{$path}}) { | |
1ce255dc | 1202 | next if $SKIP_PROP{$prop}; |
9b981fc6 | 1203 | my $v = $h->{$path}->{$prop}; |
97f6987a EW |
1204 | my $t_ppath_prop = "$t: " . |
1205 | uri_encode($ppath) . ' ' . | |
1206 | uri_encode($prop); | |
9b981fc6 | 1207 | if (defined $v) { |
97f6987a EW |
1208 | push @out, " +$t_ppath_prop " . |
1209 | uri_encode($v); | |
9b981fc6 | 1210 | } else { |
97f6987a | 1211 | push @out, " -$t_ppath_prop"; |
9b981fc6 EW |
1212 | } |
1213 | } | |
1214 | } | |
1215 | } | |
1216 | foreach my $t (qw/absent_file absent_directory/) { | |
97f6987a | 1217 | $h = $ed->{$t} or next; |
9b981fc6 EW |
1218 | foreach my $parent (sort keys %$h) { |
1219 | foreach my $path (sort @{$h->{$parent}}) { | |
97f6987a EW |
1220 | push @out, " $t: " . |
1221 | uri_encode("$parent/$path"); | |
9b981fc6 EW |
1222 | warn "W: $t: $parent/$path ", |
1223 | "Insufficient permissions?\n"; | |
1224 | } | |
1225 | } | |
1226 | } | |
97f6987a | 1227 | \@out; |
9b981fc6 EW |
1228 | } |
1229 | ||
1c8443b0 EW |
1230 | sub parse_svn_date { |
1231 | my $date = shift || return '+0000 1970-01-01 00:00:00'; | |
1232 | my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T | |
1233 | (\d\d)\:(\d\d)\:(\d\d).\d+Z$/x) or | |
1234 | croak "Unable to parse date: $date\n"; | |
1235 | "+0000 $Y-$m-$d $H:$M:$S"; | |
1236 | } | |
1237 | ||
1238 | sub check_author { | |
1239 | my ($author) = @_; | |
1240 | if (!defined $author || length $author == 0) { | |
1241 | $author = '(no author)'; | |
1242 | } | |
1243 | if (defined $::_authors && ! defined $::users{$author}) { | |
1244 | die "Author: $author not defined in $::_authors file\n"; | |
1245 | } | |
1246 | $author; | |
1247 | } | |
1248 | ||
9b981fc6 | 1249 | sub make_log_entry { |
97f6987a EW |
1250 | my ($self, $rev, $parents, $ed) = @_; |
1251 | my $untracked = $self->get_untracked($ed); | |
1252 | ||
9b981fc6 | 1253 | open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!; |
97f6987a EW |
1254 | print $un "r$rev\n" or croak $!; |
1255 | print $un $_, "\n" foreach @$untracked; | |
1256 | my %log_entry = ( parents => $parents || [], revision => $rev, | |
1257 | log => ''); | |
1258 | my $rp = $self->ra->rev_proplist($rev); | |
9b981fc6 EW |
1259 | foreach (sort keys %$rp) { |
1260 | my $v = $rp->{$_}; | |
1261 | if (/^svn:(author|date|log)$/) { | |
1262 | $log_entry{$1} = $v; | |
1263 | } else { | |
1264 | print $un " rev_prop: ", uri_encode($_), ' ', | |
1265 | uri_encode($v), "\n"; | |
1266 | } | |
1267 | } | |
1268 | close $un or croak $!; | |
97f6987a | 1269 | |
9b981fc6 EW |
1270 | $log_entry{date} = parse_svn_date($log_entry{date}); |
1271 | $log_entry{author} = check_author($log_entry{author}); | |
1272 | $log_entry{log} .= "\n"; | |
1273 | \%log_entry; | |
1274 | } | |
1275 | ||
1276 | sub fetch { | |
3ebe8df7 | 1277 | my ($self, $min_rev, $max_rev, @parents) = @_; |
9b981fc6 | 1278 | my ($last_rev, $last_commit) = $self->last_rev_commit; |
3ebe8df7 | 1279 | my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev); |
9b981fc6 | 1280 | return if ($base > $head); |
0af9c9f9 | 1281 | $self->ra->gs_fetch_loop_common($base, $head, $self); |
9b981fc6 EW |
1282 | } |
1283 | ||
1284 | sub set_tree_cb { | |
1285 | my ($self, $log_entry, $tree, $rev, $date, $author) = @_; | |
1286 | # TODO: enable and test optimized commits: | |
1287 | if (0 && $rev == ($self->{last_rev} + 1)) { | |
1288 | $log_entry->{revision} = $rev; | |
1289 | $log_entry->{author} = $author; | |
1290 | $self->do_git_commit($log_entry, "$rev=$tree"); | |
1291 | } else { | |
0af9c9f9 EW |
1292 | $self->{inject_parents} = { $rev => $tree }; |
1293 | $self->fetch(undef, undef); | |
9b981fc6 EW |
1294 | } |
1295 | } | |
1296 | ||
1297 | sub set_tree { | |
1298 | my ($self, $tree) = (shift, shift); | |
1ce255dc | 1299 | my $log_entry = ::get_commit_entry($tree); |
9b981fc6 EW |
1300 | unless ($self->{last_rev}) { |
1301 | fatal("Must have an existing revision to commit\n"); | |
1302 | } | |
61395354 EW |
1303 | my %ed_opts = ( r => $self->{last_rev}, |
1304 | log => $log_entry->{log}, | |
1305 | ra => $self->ra, | |
1306 | tree_a => $self->{last_commit}, | |
1307 | tree_b => $tree, | |
1308 | editor_cb => sub { | |
1309 | $self->set_tree_cb($log_entry, $tree, @_) }, | |
1310 | svn_path => $self->{path} ); | |
1311 | if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) { | |
9b981fc6 EW |
1312 | print "No changes\nr$self->{last_rev} = $tree\n"; |
1313 | } | |
9b981fc6 EW |
1314 | } |
1315 | ||
f0ecca10 EW |
1316 | sub rebuild { |
1317 | my ($self) = @_; | |
1318 | print "Rebuilding $self->{db_path} ...\n"; | |
1319 | my ($rev_list, $ctx) = command_output_pipe("rev-list", $self->refname); | |
1320 | my $latest; | |
1321 | my $full_url = $self->full_url; | |
1322 | my $svn_uuid; | |
1323 | while (<$rev_list>) { | |
1324 | chomp; | |
1325 | my $c = $_; | |
1326 | die "Non-SHA1: $c\n" unless $c =~ /^$::sha1$/o; | |
1327 | my ($url, $rev, $uuid) = ::cmt_metadata($c); | |
1328 | ||
1329 | # ignore merges (from set-tree) | |
1330 | next if (!defined $rev || !$uuid); | |
1331 | ||
1332 | # if we merged or otherwise started elsewhere, this is | |
1333 | # how we break out of it | |
1334 | if ((defined $svn_uuid && ($uuid ne $svn_uuid)) || | |
1335 | ($full_url && $url && ($url ne $full_url))) { | |
1336 | next; | |
1337 | } | |
1338 | $latest ||= $rev; | |
1339 | $svn_uuid ||= $uuid; | |
1340 | ||
1341 | $self->rev_db_set($rev, $c); | |
1342 | print "r$rev = $c\n"; | |
1343 | } | |
1344 | command_close_pipe($rev_list, $ctx); | |
1345 | print "Done rebuilding $self->{db_path}\n"; | |
1346 | } | |
1347 | ||
9b981fc6 EW |
1348 | # rev_db: |
1349 | # Tie::File seems to be prone to offset errors if revisions get sparse, | |
1350 | # it's not that fast, either. Tie::File is also not in Perl 5.6. So | |
1351 | # one of my favorite modules is out :< Next up would be one of the DBM | |
1352 | # modules, but I'm not sure which is most portable... So I'll just | |
1353 | # go with something that's plain-text, but still capable of | |
1354 | # being randomly accessed. So here's my ultra-simple fixed-width | |
1355 | # database. All records are 40 characters + "\n", so it's easy to seek | |
1356 | # to a revision: (41 * rev) is the byte offset. | |
1357 | # A record of 40 0s denotes an empty revision. | |
1358 | # And yes, it's still pretty fast (faster than Tie::File). | |
373274f9 | 1359 | # These files are disposable unless --no-metadata is set |
9b981fc6 EW |
1360 | |
1361 | sub rev_db_set { | |
373274f9 | 1362 | my ($self, $rev, $commit, $update_ref) = @_; |
9b981fc6 | 1363 | length $commit == 40 or croak "arg3 must be a full SHA1 hexsum\n"; |
373274f9 EW |
1364 | my ($db, $db_lock) = ($self->{db_path}, "$self->{db_path}.lock"); |
1365 | my $sig; | |
1366 | if ($update_ref) { | |
1367 | $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} = | |
1368 | $SIG{USR1} = $SIG{USR2} = sub { $sig = $_[0] }; | |
1369 | } | |
1370 | $LOCKFILES{$db_lock} = 1; | |
1371 | if ($_no_metadata) { | |
1372 | copy($db, $db_lock) or die "rev_db_set(@_): ", | |
1373 | "Failed to copy: ", | |
1374 | "$db => $db_lock ($!)\n"; | |
1375 | } else { | |
1376 | rename $db, $db_lock or die "rev_db_set(@_): ", | |
1377 | "Failed to rename: ", | |
1378 | "$db => $db_lock ($!)\n"; | |
1379 | } | |
1380 | open my $fh, '+<', $db_lock or croak $!; | |
9b981fc6 EW |
1381 | my $offset = $rev * 41; |
1382 | # assume that append is the common case: | |
1383 | seek $fh, 0, 2 or croak $!; | |
1384 | my $pos = tell $fh; | |
1385 | if ($pos < $offset) { | |
47a0b75e EW |
1386 | for (1 .. (($offset - $pos) / 41)) { |
1387 | print $fh (('0' x 40),"\n") or croak $!; | |
1388 | } | |
9b981fc6 EW |
1389 | } |
1390 | seek $fh, $offset, 0 or croak $!; | |
1391 | print $fh $commit,"\n" or croak $!; | |
1392 | close $fh or croak $!; | |
373274f9 EW |
1393 | if ($update_ref) { |
1394 | command_noisy('update-ref', '-m', "r$rev", | |
1395 | $self->refname, $commit); | |
1396 | } | |
1397 | rename $db_lock, $db or die "rev_db_set(@_): ", "Failed to rename: ", | |
1398 | "$db_lock => $db ($!)\n"; | |
1399 | delete $LOCKFILES{$db_lock}; | |
1400 | if ($update_ref) { | |
1401 | $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} = | |
1402 | $SIG{USR1} = $SIG{USR2} = 'DEFAULT'; | |
1403 | kill $sig, $$ if defined $sig; | |
1404 | } | |
9b981fc6 EW |
1405 | } |
1406 | ||
9c93fee5 EW |
1407 | sub rev_db_max { |
1408 | my ($self) = @_; | |
1409 | my @stat = stat $self->{db_path} or | |
1410 | die "Couldn't stat $self->{db_path}: $!\n"; | |
1411 | ($stat[7] % 41) == 0 or | |
1412 | die "$self->{db_path} inconsistent size:$stat[7]\n"; | |
1413 | my $max = $stat[7] / 41; | |
1414 | (($max > 0) ? $max - 1 : 0); | |
1415 | } | |
1416 | ||
9b981fc6 EW |
1417 | sub rev_db_get { |
1418 | my ($self, $rev) = @_; | |
1419 | my $ret; | |
1420 | my $offset = $rev * 41; | |
1421 | open my $fh, '<', $self->{db_path} or croak $!; | |
ce4b4af7 EW |
1422 | if (sysseek($fh, $offset, 0) == $offset) { |
1423 | my $read = sysread($fh, $ret, 40); | |
1424 | $ret = undef if ($read != 40 || $ret eq ('0'x40)); | |
9b981fc6 EW |
1425 | } |
1426 | close $fh or croak $!; | |
1427 | $ret; | |
1428 | } | |
1429 | ||
15710b6f EW |
1430 | sub find_rev_before { |
1431 | my ($self, $rev, $eq_ok) = @_; | |
1432 | --$rev unless $eq_ok; | |
1433 | while ($rev > 0) { | |
1434 | if (my $c = $self->rev_db_get($rev)) { | |
1435 | return ($rev, $c); | |
1436 | } | |
1437 | --$rev; | |
1438 | } | |
1439 | return (undef, undef); | |
1440 | } | |
1441 | ||
9b981fc6 | 1442 | sub _new { |
706587fc EW |
1443 | my ($class, $repo_id, $ref_id, $path) = @_; |
1444 | unless (defined $repo_id && length $repo_id) { | |
1445 | $repo_id = $Git::SVN::default_repo_id; | |
1446 | } | |
1447 | unless (defined $ref_id && length $ref_id) { | |
8b8fc068 | 1448 | $_[2] = $ref_id = $Git::SVN::default_ref_id; |
706587fc EW |
1449 | } |
1450 | $_[1] = $repo_id = sanitize_remote_name($repo_id); | |
1451 | my $dir = "$ENV{GIT_DIR}/svn/$ref_id"; | |
1452 | $_[3] = $path = '' unless (defined $path); | |
8b8fc068 EW |
1453 | mkpath([$dir]); |
1454 | unless (-f "$dir/.rev_db") { | |
1455 | open my $fh, '>>', "$dir/.rev_db" or croak $!; | |
1456 | close $fh or croak $!; | |
1457 | } | |
706587fc EW |
1458 | bless { ref_id => $ref_id, dir => $dir, index => "$dir/index", |
1459 | path => $path, | |
1460 | db_path => "$dir/.rev_db", repo_id => $repo_id }, $class; | |
9b981fc6 EW |
1461 | } |
1462 | ||
1c8443b0 EW |
1463 | sub uri_encode { |
1464 | my ($f) = @_; | |
1465 | $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg; | |
1466 | $f | |
1467 | } | |
9b981fc6 | 1468 | |
d976acfd EW |
1469 | package Git::SVN::Prompt; |
1470 | use strict; | |
1471 | use warnings; | |
1472 | require SVN::Core; | |
1473 | use vars qw/$_no_auth_cache $_username/; | |
1474 | ||
1475 | sub simple { | |
30d055aa EW |
1476 | my ($cred, $realm, $default_username, $may_save, $pool) = @_; |
1477 | $may_save = undef if $_no_auth_cache; | |
1478 | $default_username = $_username if defined $_username; | |
1479 | if (defined $default_username && length $default_username) { | |
1480 | if (defined $realm && length $realm) { | |
6f729591 EW |
1481 | print STDERR "Authentication realm: $realm\n"; |
1482 | STDERR->flush; | |
30d055aa EW |
1483 | } |
1484 | $cred->username($default_username); | |
1485 | } else { | |
d976acfd | 1486 | username($cred, $realm, $may_save, $pool); |
30d055aa EW |
1487 | } |
1488 | $cred->password(_read_password("Password for '" . | |
1489 | $cred->username . "': ", $realm)); | |
1490 | $cred->may_save($may_save); | |
1491 | $SVN::_Core::SVN_NO_ERROR; | |
1492 | } | |
1493 | ||
d976acfd | 1494 | sub ssl_server_trust { |
30d055aa EW |
1495 | my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_; |
1496 | $may_save = undef if $_no_auth_cache; | |
6f729591 | 1497 | print STDERR "Error validating server certificate for '$realm':\n"; |
30d055aa | 1498 | if ($failures & $SVN::Auth::SSL::UNKNOWNCA) { |
6f729591 | 1499 | print STDERR " - The certificate is not issued by a trusted ", |
30d055aa EW |
1500 | "authority. Use the\n", |
1501 | " fingerprint to validate the certificate manually!\n"; | |
1502 | } | |
1503 | if ($failures & $SVN::Auth::SSL::CNMISMATCH) { | |
6f729591 | 1504 | print STDERR " - The certificate hostname does not match.\n"; |
30d055aa EW |
1505 | } |
1506 | if ($failures & $SVN::Auth::SSL::NOTYETVALID) { | |
6f729591 | 1507 | print STDERR " - The certificate is not yet valid.\n"; |
30d055aa EW |
1508 | } |
1509 | if ($failures & $SVN::Auth::SSL::EXPIRED) { | |
6f729591 | 1510 | print STDERR " - The certificate has expired.\n"; |
30d055aa EW |
1511 | } |
1512 | if ($failures & $SVN::Auth::SSL::OTHER) { | |
6f729591 | 1513 | print STDERR " - The certificate has an unknown error.\n"; |
30d055aa | 1514 | } |
6f729591 EW |
1515 | printf STDERR |
1516 | "Certificate information:\n". | |
30d055aa EW |
1517 | " - Hostname: %s\n". |
1518 | " - Valid: from %s until %s\n". | |
1519 | " - Issuer: %s\n". | |
1520 | " - Fingerprint: %s\n", | |
1521 | map $cert_info->$_, qw(hostname valid_from valid_until | |
6f729591 | 1522 | issuer_dname fingerprint); |
30d055aa EW |
1523 | my $choice; |
1524 | prompt: | |
6f729591 | 1525 | print STDERR $may_save ? |
30d055aa EW |
1526 | "(R)eject, accept (t)emporarily or accept (p)ermanently? " : |
1527 | "(R)eject or accept (t)emporarily? "; | |
6f729591 | 1528 | STDERR->flush; |
30d055aa EW |
1529 | $choice = lc(substr(<STDIN> || 'R', 0, 1)); |
1530 | if ($choice =~ /^t$/i) { | |
1531 | $cred->may_save(undef); | |
1532 | } elsif ($choice =~ /^r$/i) { | |
1533 | return -1; | |
1534 | } elsif ($may_save && $choice =~ /^p$/i) { | |
1535 | $cred->may_save($may_save); | |
1536 | } else { | |
1537 | goto prompt; | |
1538 | } | |
1539 | $cred->accepted_failures($failures); | |
1540 | $SVN::_Core::SVN_NO_ERROR; | |
1541 | } | |
1542 | ||
d976acfd | 1543 | sub ssl_client_cert { |
30d055aa EW |
1544 | my ($cred, $realm, $may_save, $pool) = @_; |
1545 | $may_save = undef if $_no_auth_cache; | |
6f729591 EW |
1546 | print STDERR "Client certificate filename: "; |
1547 | STDERR->flush; | |
30d055aa EW |
1548 | chomp(my $filename = <STDIN>); |
1549 | $cred->cert_file($filename); | |
1550 | $cred->may_save($may_save); | |
1551 | $SVN::_Core::SVN_NO_ERROR; | |
1552 | } | |
1553 | ||
d976acfd | 1554 | sub ssl_client_cert_pw { |
30d055aa EW |
1555 | my ($cred, $realm, $may_save, $pool) = @_; |
1556 | $may_save = undef if $_no_auth_cache; | |
1557 | $cred->password(_read_password("Password: ", $realm)); | |
1558 | $cred->may_save($may_save); | |
1559 | $SVN::_Core::SVN_NO_ERROR; | |
1560 | } | |
1561 | ||
d976acfd | 1562 | sub username { |
30d055aa EW |
1563 | my ($cred, $realm, $may_save, $pool) = @_; |
1564 | $may_save = undef if $_no_auth_cache; | |
1565 | if (defined $realm && length $realm) { | |
6f729591 | 1566 | print STDERR "Authentication realm: $realm\n"; |
30d055aa EW |
1567 | } |
1568 | my $username; | |
1569 | if (defined $_username) { | |
1570 | $username = $_username; | |
1571 | } else { | |
6f729591 EW |
1572 | print STDERR "Username: "; |
1573 | STDERR->flush; | |
30d055aa EW |
1574 | chomp($username = <STDIN>); |
1575 | } | |
1576 | $cred->username($username); | |
1577 | $cred->may_save($may_save); | |
1578 | $SVN::_Core::SVN_NO_ERROR; | |
1579 | } | |
1580 | ||
1581 | sub _read_password { | |
1582 | my ($prompt, $realm) = @_; | |
6f729591 EW |
1583 | print STDERR $prompt; |
1584 | STDERR->flush; | |
30d055aa EW |
1585 | require Term::ReadKey; |
1586 | Term::ReadKey::ReadMode('noecho'); | |
1587 | my $password = ''; | |
1588 | while (defined(my $key = Term::ReadKey::ReadKey(0))) { | |
1589 | last if $key =~ /[\012\015]/; # \n\r | |
1590 | $password .= $key; | |
1591 | } | |
1592 | Term::ReadKey::ReadMode('restore'); | |
6f729591 EW |
1593 | print STDERR "\n"; |
1594 | STDERR->flush; | |
30d055aa EW |
1595 | $password; |
1596 | } | |
1597 | ||
d976acfd EW |
1598 | package main; |
1599 | ||
b9c85187 EW |
1600 | { |
1601 | my $kill_stupid_warnings = $SVN::Node::none.$SVN::Node::file. | |
1602 | $SVN::Node::dir.$SVN::Node::unknown. | |
1603 | $SVN::Node::none.$SVN::Node::file. | |
1604 | $SVN::Node::dir.$SVN::Node::unknown. | |
1605 | $SVN::Auth::SSL::CNMISMATCH. | |
1606 | $SVN::Auth::SSL::NOTYETVALID. | |
1607 | $SVN::Auth::SSL::EXPIRED. | |
1608 | $SVN::Auth::SSL::UNKNOWNCA. | |
1609 | $SVN::Auth::SSL::OTHER; | |
1610 | } | |
1611 | ||
27a1a801 EW |
1612 | package SVN::Git::Fetcher; |
1613 | use vars qw/@ISA/; | |
1614 | use strict; | |
1615 | use warnings; | |
1616 | use Carp qw/croak/; | |
1617 | use IO::File qw//; | |
90c1b15d | 1618 | use Digest::MD5; |
27a1a801 EW |
1619 | |
1620 | # file baton members: path, mode_a, mode_b, pool, fh, blob, base | |
1621 | sub new { | |
1622 | my ($class, $git_svn) = @_; | |
1623 | my $self = SVN::Delta::Editor->new; | |
1624 | bless $self, $class; | |
1c8443b0 | 1625 | $self->{c} = $git_svn->{last_commit} if exists $git_svn->{last_commit}; |
d2a9a87b EW |
1626 | $self->{empty} = {}; |
1627 | $self->{dir_prop} = {}; | |
1628 | $self->{file_prop} = {}; | |
1629 | $self->{absent_dir} = {}; | |
1630 | $self->{absent_file} = {}; | |
ef3cfaad | 1631 | $self->{gii} = $git_svn->tmp_index_do(sub { Git::IndexInfo->new }); |
27a1a801 EW |
1632 | $self; |
1633 | } | |
1634 | ||
8b8fc068 EW |
1635 | sub set_path_strip { |
1636 | my ($self, $path) = @_; | |
1637 | $self->{path_strip} = qr/^\Q$path\E\/?/; | |
1638 | } | |
1639 | ||
d2a9a87b EW |
1640 | sub open_root { |
1641 | { path => '' }; | |
1642 | } | |
1643 | ||
1644 | sub open_directory { | |
1645 | my ($self, $path, $pb, $rev) = @_; | |
1646 | { path => $path }; | |
1647 | } | |
1648 | ||
706587fc EW |
1649 | sub git_path { |
1650 | my ($self, $path) = @_; | |
2b27f6c8 EW |
1651 | if ($self->{path_strip}) { |
1652 | $path =~ s!$self->{path_strip}!! or | |
1653 | die "Failed to strip path '$path' ($self->{path_strip})\n"; | |
1654 | } | |
706587fc EW |
1655 | $path; |
1656 | } | |
1657 | ||
27a1a801 EW |
1658 | sub delete_entry { |
1659 | my ($self, $path, $rev, $pb) = @_; | |
4a87db0e | 1660 | |
706587fc | 1661 | my $gpath = $self->git_path($path); |
8a603774 EW |
1662 | return undef if ($gpath eq ''); |
1663 | ||
4a87db0e | 1664 | # remove entire directories. |
706587fc | 1665 | if (command('ls-tree', $self->{c}, '--', $gpath) =~ /^040000 tree/) { |
4a87db0e EW |
1666 | my ($ls, $ctx) = command_output_pipe(qw/ls-tree |
1667 | -r --name-only -z/, | |
706587fc | 1668 | $self->{c}, '--', $gpath); |
4a87db0e EW |
1669 | local $/ = "\0"; |
1670 | while (<$ls>) { | |
ef3cfaad EW |
1671 | chomp; |
1672 | $self->{gii}->remove($_); | |
4a87db0e EW |
1673 | print "\tD\t$_\n" unless $self->{q}; |
1674 | } | |
706587fc | 1675 | print "\tD\t$gpath/\n" unless $self->{q}; |
4a87db0e EW |
1676 | command_close_pipe($ls, $ctx); |
1677 | $self->{empty}->{$path} = 0 | |
1678 | } else { | |
ef3cfaad | 1679 | $self->{gii}->remove($gpath); |
706587fc | 1680 | print "\tD\t$gpath\n" unless $self->{q}; |
4a87db0e | 1681 | } |
27a1a801 EW |
1682 | undef; |
1683 | } | |
1684 | ||
1685 | sub open_file { | |
1686 | my ($self, $path, $pb, $rev) = @_; | |
706587fc EW |
1687 | my $gpath = $self->git_path($path); |
1688 | my ($mode, $blob) = (command('ls-tree', $self->{c}, '--', $gpath) | |
27a1a801 | 1689 | =~ /^(\d{6}) blob ([a-f\d]{40})\t/); |
006ede5e EW |
1690 | unless (defined $mode && defined $blob) { |
1691 | die "$path was not found in commit $self->{c} (r$rev)\n"; | |
1692 | } | |
27a1a801 | 1693 | { path => $path, mode_a => $mode, mode_b => $mode, blob => $blob, |
0864e3ba | 1694 | pool => SVN::Pool->new, action => 'M' }; |
27a1a801 EW |
1695 | } |
1696 | ||
1697 | sub add_file { | |
1698 | my ($self, $path, $pb, $cp_path, $cp_rev) = @_; | |
d2a9a87b EW |
1699 | my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#); |
1700 | delete $self->{empty}->{$dir}; | |
27a1a801 | 1701 | { path => $path, mode_a => 100644, mode_b => 100644, |
0864e3ba | 1702 | pool => SVN::Pool->new, action => 'A' }; |
27a1a801 EW |
1703 | } |
1704 | ||
d2a9a87b EW |
1705 | sub add_directory { |
1706 | my ($self, $path, $cp_path, $cp_rev) = @_; | |
1707 | my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#); | |
1708 | delete $self->{empty}->{$dir}; | |
1709 | $self->{empty}->{$path} = 1; | |
1710 | { path => $path }; | |
1711 | } | |
1712 | ||
1713 | sub change_dir_prop { | |
1714 | my ($self, $db, $prop, $value) = @_; | |
1715 | $self->{dir_prop}->{$db->{path}} ||= {}; | |
1716 | $self->{dir_prop}->{$db->{path}}->{$prop} = $value; | |
1717 | undef; | |
1718 | } | |
1719 | ||
1720 | sub absent_directory { | |
1721 | my ($self, $path, $pb) = @_; | |
1722 | $self->{absent_dir}->{$pb->{path}} ||= []; | |
1723 | push @{$self->{absent_dir}->{$pb->{path}}}, $path; | |
1724 | undef; | |
1725 | } | |
1726 | ||
1727 | sub absent_file { | |
1728 | my ($self, $path, $pb) = @_; | |
1729 | $self->{absent_file}->{$pb->{path}} ||= []; | |
1730 | push @{$self->{absent_file}->{$pb->{path}}}, $path; | |
1731 | undef; | |
1732 | } | |
1733 | ||
27a1a801 EW |
1734 | sub change_file_prop { |
1735 | my ($self, $fb, $prop, $value) = @_; | |
1736 | if ($prop eq 'svn:executable') { | |
1737 | if ($fb->{mode_b} != 120000) { | |
1738 | $fb->{mode_b} = defined $value ? 100755 : 100644; | |
1739 | } | |
1740 | } elsif ($prop eq 'svn:special') { | |
1741 | $fb->{mode_b} = defined $value ? 120000 : 100644; | |
d2a9a87b EW |
1742 | } else { |
1743 | $self->{file_prop}->{$fb->{path}} ||= {}; | |
1744 | $self->{file_prop}->{$fb->{path}}->{$prop} = $value; | |
27a1a801 EW |
1745 | } |
1746 | undef; | |
1747 | } | |
1748 | ||
1749 | sub apply_textdelta { | |
1750 | my ($self, $fb, $exp) = @_; | |
1751 | my $fh = IO::File->new_tmpfile; | |
1752 | $fh->autoflush(1); | |
1753 | # $fh gets auto-closed() by SVN::TxDelta::apply(), | |
1754 | # (but $base does not,) so dup() it for reading in close_file | |
1755 | open my $dup, '<&', $fh or croak $!; | |
1756 | my $base = IO::File->new_tmpfile; | |
1757 | $base->autoflush(1); | |
1758 | if ($fb->{blob}) { | |
1759 | defined (my $pid = fork) or croak $!; | |
1760 | if (!$pid) { | |
1761 | open STDOUT, '>&', $base or croak $!; | |
1762 | print STDOUT 'link ' if ($fb->{mode_a} == 120000); | |
1763 | exec qw/git-cat-file blob/, $fb->{blob} or croak $!; | |
1764 | } | |
1765 | waitpid $pid, 0; | |
1766 | croak $? if $?; | |
1767 | ||
1768 | if (defined $exp) { | |
1769 | seek $base, 0, 0 or croak $!; | |
1770 | my $md5 = Digest::MD5->new; | |
1771 | $md5->addfile($base); | |
1772 | my $got = $md5->hexdigest; | |
1773 | die "Checksum mismatch: $fb->{path} $fb->{blob}\n", | |
1774 | "expected: $exp\n", | |
1775 | " got: $got\n" if ($got ne $exp); | |
1776 | } | |
1777 | } | |
1778 | seek $base, 0, 0 or croak $!; | |
1779 | $fb->{fh} = $dup; | |
1780 | $fb->{base} = $base; | |
1781 | [ SVN::TxDelta::apply($base, $fh, undef, $fb->{path}, $fb->{pool}) ]; | |
1782 | } | |
1783 | ||
1784 | sub close_file { | |
1785 | my ($self, $fb, $exp) = @_; | |
1786 | my $hash; | |
706587fc | 1787 | my $path = $self->git_path($fb->{path}); |
27a1a801 EW |
1788 | if (my $fh = $fb->{fh}) { |
1789 | seek($fh, 0, 0) or croak $!; | |
1790 | my $md5 = Digest::MD5->new; | |
1791 | $md5->addfile($fh); | |
1792 | my $got = $md5->hexdigest; | |
1793 | die "Checksum mismatch: $path\n", | |
1794 | "expected: $exp\n got: $got\n" if ($got ne $exp); | |
1795 | seek($fh, 0, 0) or croak $!; | |
1796 | if ($fb->{mode_b} == 120000) { | |
1797 | read($fh, my $buf, 5) == 5 or croak $!; | |
1798 | $buf eq 'link ' or die "$path has mode 120000", | |
1799 | "but is not a link\n"; | |
1800 | } | |
1801 | defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n"; | |
1802 | if (!$pid) { | |
1803 | open STDIN, '<&', $fh or croak $!; | |
1804 | exec qw/git-hash-object -w --stdin/ or croak $!; | |
1805 | } | |
1806 | chomp($hash = do { local $/; <$out> }); | |
1807 | close $out or croak $!; | |
1808 | close $fh or croak $!; | |
1809 | $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n"; | |
1810 | close $fb->{base} or croak $!; | |
1811 | } else { | |
1812 | $hash = $fb->{blob} or die "no blob information\n"; | |
1813 | } | |
1814 | $fb->{pool}->clear; | |
ef3cfaad | 1815 | $self->{gii}->update($fb->{mode_b}, $hash, $path) or croak $!; |
0864e3ba | 1816 | print "\t$fb->{action}\t$path\n" if $fb->{action} && ! $self->{q}; |
27a1a801 EW |
1817 | undef; |
1818 | } | |
1819 | ||
1820 | sub abort_edit { | |
1821 | my $self = shift; | |
ef3cfaad EW |
1822 | $self->{nr} = $self->{gii}->{nr}; |
1823 | delete $self->{gii}; | |
27a1a801 EW |
1824 | $self->SUPER::abort_edit(@_); |
1825 | } | |
1826 | ||
1827 | sub close_edit { | |
1828 | my $self = shift; | |
dad73c0b | 1829 | $self->{git_commit_ok} = 1; |
ef3cfaad EW |
1830 | $self->{nr} = $self->{gii}->{nr}; |
1831 | delete $self->{gii}; | |
27a1a801 EW |
1832 | $self->SUPER::close_edit(@_); |
1833 | } | |
1a82e793 | 1834 | |
a5e0cedc | 1835 | package SVN::Git::Editor; |
24e22aa8 | 1836 | use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/; |
a5e0cedc EW |
1837 | use strict; |
1838 | use warnings; | |
1839 | use Carp qw/croak/; | |
1840 | use IO::File; | |
90c1b15d | 1841 | use Digest::MD5; |
a5e0cedc EW |
1842 | |
1843 | sub new { | |
61395354 EW |
1844 | my ($class, $opts) = @_; |
1845 | foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) { | |
1846 | die "$_ required!\n" unless (defined $opts->{$_}); | |
1847 | } | |
1848 | ||
1849 | my $pool = SVN::Pool->new; | |
1850 | my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b}); | |
1851 | my $types = check_diff_paths($opts->{ra}, $opts->{svn_path}, | |
1852 | $opts->{r}, $mods); | |
1853 | ||
1854 | # $opts->{ra} functions should not be used after this: | |
1855 | my @ce = $opts->{ra}->get_commit_editor($opts->{log}, | |
1856 | $opts->{editor_cb}, $pool); | |
1857 | my $self = SVN::Delta::Editor->new(@ce, $pool); | |
a5e0cedc | 1858 | bless $self, $class; |
61395354 EW |
1859 | foreach (qw/svn_path r tree_a tree_b/) { |
1860 | $self->{$_} = $opts->{$_}; | |
a5e0cedc | 1861 | } |
61395354 EW |
1862 | $self->{url} = $opts->{ra}->{url}; |
1863 | $self->{mods} = $mods; | |
1864 | $self->{types} = $types; | |
1865 | $self->{pool} = $pool; | |
a5e0cedc EW |
1866 | $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) }; |
1867 | $self->{rm} = { }; | |
d3a840dc EW |
1868 | $self->{path_prefix} = length $self->{svn_path} ? |
1869 | "$self->{svn_path}/" : ''; | |
a5e0cedc EW |
1870 | return $self; |
1871 | } | |
1872 | ||
61395354 EW |
1873 | sub generate_diff { |
1874 | my ($tree_a, $tree_b) = @_; | |
1875 | my @diff_tree = qw(diff-tree -z -r); | |
24e22aa8 EW |
1876 | if ($_cp_similarity) { |
1877 | push @diff_tree, "-C$_cp_similarity"; | |
61395354 EW |
1878 | } else { |
1879 | push @diff_tree, '-C'; | |
1880 | } | |
24e22aa8 EW |
1881 | push @diff_tree, '--find-copies-harder' if $_find_copies_harder; |
1882 | push @diff_tree, "-l$_rename_limit" if defined $_rename_limit; | |
61395354 EW |
1883 | push @diff_tree, $tree_a, $tree_b; |
1884 | my ($diff_fh, $ctx) = command_output_pipe(@diff_tree); | |
1885 | local $/ = "\0"; | |
1886 | my $state = 'meta'; | |
1887 | my @mods; | |
1888 | while (<$diff_fh>) { | |
1889 | chomp $_; # this gets rid of the trailing "\0" | |
1890 | if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s | |
1891 | $::sha1\s($::sha1)\s | |
1892 | ([MTCRAD])\d*$/xo) { | |
1893 | push @mods, { mode_a => $1, mode_b => $2, | |
1894 | sha1_b => $3, chg => $4 }; | |
1895 | if ($4 =~ /^(?:C|R)$/) { | |
1896 | $state = 'file_a'; | |
1897 | } else { | |
1898 | $state = 'file_b'; | |
1899 | } | |
1900 | } elsif ($state eq 'file_a') { | |
1901 | my $x = $mods[$#mods] or croak "Empty array\n"; | |
1902 | if ($x->{chg} !~ /^(?:C|R)$/) { | |
1903 | croak "Error parsing $_, $x->{chg}\n"; | |
1904 | } | |
1905 | $x->{file_a} = $_; | |
1906 | $state = 'file_b'; | |
1907 | } elsif ($state eq 'file_b') { | |
1908 | my $x = $mods[$#mods] or croak "Empty array\n"; | |
1909 | if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) { | |
1910 | croak "Error parsing $_, $x->{chg}\n"; | |
1911 | } | |
1912 | if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) { | |
1913 | croak "Error parsing $_, $x->{chg}\n"; | |
1914 | } | |
1915 | $x->{file_b} = $_; | |
1916 | $state = 'meta'; | |
1917 | } else { | |
1918 | croak "Error parsing $_\n"; | |
1919 | } | |
1920 | } | |
1921 | command_close_pipe($diff_fh, $ctx); | |
1922 | \@mods; | |
1923 | } | |
1924 | ||
1925 | sub check_diff_paths { | |
1926 | my ($ra, $pfx, $rev, $mods) = @_; | |
1927 | my %types; | |
1928 | $pfx .= '/' if length $pfx; | |
1929 | ||
1930 | sub type_diff_paths { | |
1931 | my ($ra, $types, $path, $rev) = @_; | |
1932 | my @p = split m#/+#, $path; | |
1933 | my $c = shift @p; | |
1934 | unless (defined $types->{$c}) { | |
1935 | $types->{$c} = $ra->check_path($c, $rev); | |
1936 | } | |
1937 | while (@p) { | |
1938 | $c .= '/' . shift @p; | |
1939 | next if defined $types->{$c}; | |
1940 | $types->{$c} = $ra->check_path($c, $rev); | |
1941 | } | |
1942 | } | |
1943 | ||
1944 | foreach my $m (@$mods) { | |
1945 | foreach my $f (qw/file_a file_b/) { | |
1946 | next unless defined $m->{$f}; | |
1947 | my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#); | |
1948 | if (length $pfx.$dir && ! defined $types{$dir}) { | |
1949 | type_diff_paths($ra, \%types, $pfx.$dir, $rev); | |
1950 | } | |
1951 | } | |
1952 | } | |
1953 | \%types; | |
1954 | } | |
1955 | ||
a5e0cedc EW |
1956 | sub split_path { |
1957 | return ($_[0] =~ m#^(.*?)/?([^/]+)$#); | |
1958 | } | |
1959 | ||
1960 | sub repo_path { | |
d3a840dc EW |
1961 | my ($self, $path) = @_; |
1962 | $self->{path_prefix}.(defined $path ? $path : ''); | |
a5e0cedc EW |
1963 | } |
1964 | ||
1965 | sub url_path { | |
1966 | my ($self, $path) = @_; | |
6e8548cc | 1967 | $self->{url} . '/' . $self->repo_path($path); |
a5e0cedc EW |
1968 | } |
1969 | ||
1970 | sub rmdirs { | |
61395354 | 1971 | my ($self) = @_; |
a5e0cedc EW |
1972 | my $rm = $self->{rm}; |
1973 | delete $rm->{''}; # we never delete the url we're tracking | |
1974 | return unless %$rm; | |
1975 | ||
1976 | foreach (keys %$rm) { | |
1977 | my @d = split m#/#, $_; | |
1978 | my $c = shift @d; | |
1979 | $rm->{$c} = 1; | |
1980 | while (@d) { | |
1981 | $c .= '/' . shift @d; | |
1982 | $rm->{$c} = 1; | |
1983 | } | |
1984 | } | |
1985 | delete $rm->{$self->{svn_path}}; | |
1986 | delete $rm->{''}; # we never delete the url we're tracking | |
1987 | return unless %$rm; | |
1988 | ||
61395354 EW |
1989 | my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/, |
1990 | $self->{tree_b}); | |
a5e0cedc EW |
1991 | local $/ = "\0"; |
1992 | while (<$fh>) { | |
1993 | chomp; | |
747fa12c | 1994 | my @dn = split m#/#, $_; |
c07eee1f EW |
1995 | while (pop @dn) { |
1996 | delete $rm->{join '/', @dn}; | |
1997 | } | |
1998 | unless (%$rm) { | |
22600a25 | 1999 | close $fh; |
c07eee1f EW |
2000 | return; |
2001 | } | |
a5e0cedc | 2002 | } |
aef4e921 | 2003 | command_close_pipe($fh, $ctx); |
c07eee1f | 2004 | |
a5e0cedc EW |
2005 | my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat}); |
2006 | foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) { | |
2007 | $self->close_directory($bat->{$d}, $p); | |
2008 | my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#); | |
44320b9e | 2009 | print "\tD+\t$d/\n" unless $::_q; |
a5e0cedc EW |
2010 | $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p); |
2011 | delete $bat->{$d}; | |
2012 | } | |
2013 | } | |
2014 | ||
2015 | sub open_or_add_dir { | |
2016 | my ($self, $full_path, $baton) = @_; | |
6e8548cc EW |
2017 | my $t = $self->{types}->{$full_path}; |
2018 | if (!defined $t) { | |
2019 | die "$full_path not known in r$self->{r} or we have a bug!\n"; | |
2020 | } | |
a5e0cedc EW |
2021 | if ($t == $SVN::Node::none) { |
2022 | return $self->add_directory($full_path, $baton, | |
2023 | undef, -1, $self->{pool}); | |
2024 | } elsif ($t == $SVN::Node::dir) { | |
2025 | return $self->open_directory($full_path, $baton, | |
2026 | $self->{r}, $self->{pool}); | |
2027 | } | |
2028 | print STDERR "$full_path already exists in repository at ", | |
2029 | "r$self->{r} and it is not a directory (", | |
2030 | ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n"; | |
2031 | exit 1; | |
2032 | } | |
2033 | ||
2034 | sub ensure_path { | |
2035 | my ($self, $path) = @_; | |
2036 | my $bat = $self->{bat}; | |
6e8548cc EW |
2037 | my $repo_path = $self->repo_path($path); |
2038 | return $bat->{''} unless (length $repo_path); | |
2039 | my @p = split m#/+#, $repo_path; | |
a5e0cedc EW |
2040 | my $c = shift @p; |
2041 | $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''}); | |
2042 | while (@p) { | |
2043 | my $c0 = $c; | |
2044 | $c .= '/' . shift @p; | |
2045 | $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0}); | |
2046 | } | |
2047 | return $bat->{$c}; | |
2048 | } | |
2049 | ||
2050 | sub A { | |
44320b9e | 2051 | my ($self, $m) = @_; |
a5e0cedc EW |
2052 | my ($dir, $file) = split_path($m->{file_b}); |
2053 | my $pbat = $self->ensure_path($dir); | |
2054 | my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat, | |
2055 | undef, -1); | |
44320b9e | 2056 | print "\tA\t$m->{file_b}\n" unless $::_q; |
a5e0cedc EW |
2057 | $self->chg_file($fbat, $m); |
2058 | $self->close_file($fbat,undef,$self->{pool}); | |
2059 | } | |
2060 | ||
2061 | sub C { | |
44320b9e | 2062 | my ($self, $m) = @_; |
a5e0cedc EW |
2063 | my ($dir, $file) = split_path($m->{file_b}); |
2064 | my $pbat = $self->ensure_path($dir); | |
2065 | my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat, | |
2066 | $self->url_path($m->{file_a}), $self->{r}); | |
44320b9e | 2067 | print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q; |
a5e0cedc EW |
2068 | $self->chg_file($fbat, $m); |
2069 | $self->close_file($fbat,undef,$self->{pool}); | |
2070 | } | |
2071 | ||
2072 | sub delete_entry { | |
2073 | my ($self, $path, $pbat) = @_; | |
2074 | my $rpath = $self->repo_path($path); | |
2075 | my ($dir, $file) = split_path($rpath); | |
2076 | $self->{rm}->{$dir} = 1; | |
2077 | $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool}); | |
2078 | } | |
2079 | ||
2080 | sub R { | |
44320b9e | 2081 | my ($self, $m) = @_; |
a5e0cedc EW |
2082 | my ($dir, $file) = split_path($m->{file_b}); |
2083 | my $pbat = $self->ensure_path($dir); | |
2084 | my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat, | |
2085 | $self->url_path($m->{file_a}), $self->{r}); | |
44320b9e | 2086 | print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q; |
a5e0cedc EW |
2087 | $self->chg_file($fbat, $m); |
2088 | $self->close_file($fbat,undef,$self->{pool}); | |
2089 | ||
2090 | ($dir, $file) = split_path($m->{file_a}); | |
2091 | $pbat = $self->ensure_path($dir); | |
2092 | $self->delete_entry($m->{file_a}, $pbat); | |
2093 | } | |
2094 | ||
2095 | sub M { | |
44320b9e | 2096 | my ($self, $m) = @_; |
a5e0cedc EW |
2097 | my ($dir, $file) = split_path($m->{file_b}); |
2098 | my $pbat = $self->ensure_path($dir); | |
2099 | my $fbat = $self->open_file($self->repo_path($m->{file_b}), | |
2100 | $pbat,$self->{r},$self->{pool}); | |
44320b9e | 2101 | print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q; |
a5e0cedc EW |
2102 | $self->chg_file($fbat, $m); |
2103 | $self->close_file($fbat,undef,$self->{pool}); | |
2104 | } | |
2105 | ||
2106 | sub T { shift->M(@_) } | |
2107 | ||
2108 | sub change_file_prop { | |
2109 | my ($self, $fbat, $pname, $pval) = @_; | |
2110 | $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool}); | |
2111 | } | |
2112 | ||
2113 | sub chg_file { | |
2114 | my ($self, $fbat, $m) = @_; | |
2115 | if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) { | |
2116 | $self->change_file_prop($fbat,'svn:executable','*'); | |
2117 | } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) { | |
2118 | $self->change_file_prop($fbat,'svn:executable',undef); | |
2119 | } | |
2120 | my $fh = IO::File->new_tmpfile or croak $!; | |
2121 | if ($m->{mode_b} =~ /^120/) { | |
2122 | print $fh 'link ' or croak $!; | |
2123 | $self->change_file_prop($fbat,'svn:special','*'); | |
2124 | } elsif ($m->{mode_a} =~ /^120/ && $m->{mode_b} !~ /^120/) { | |
2125 | $self->change_file_prop($fbat,'svn:special',undef); | |
2126 | } | |
2127 | defined(my $pid = fork) or croak $!; | |
2128 | if (!$pid) { | |
2129 | open STDOUT, '>&', $fh or croak $!; | |
2130 | exec qw/git-cat-file blob/, $m->{sha1_b} or croak $!; | |
2131 | } | |
2132 | waitpid $pid, 0; | |
2133 | croak $? if $?; | |
2134 | $fh->flush == 0 or croak $!; | |
2135 | seek $fh, 0, 0 or croak $!; | |
2136 | ||
2137 | my $md5 = Digest::MD5->new; | |
2138 | $md5->addfile($fh) or croak $!; | |
2139 | seek $fh, 0, 0 or croak $!; | |
2140 | ||
2141 | my $exp = $md5->hexdigest; | |
f7197dff EW |
2142 | my $pool = SVN::Pool->new; |
2143 | my $atd = $self->apply_textdelta($fbat, undef, $pool); | |
2144 | my $got = SVN::TxDelta::send_stream($fh, @$atd, $pool); | |
a5e0cedc | 2145 | die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp); |
f7197dff | 2146 | $pool->clear; |
a5e0cedc EW |
2147 | |
2148 | close $fh or croak $!; | |
2149 | } | |
2150 | ||
2151 | sub D { | |
44320b9e | 2152 | my ($self, $m) = @_; |
a5e0cedc EW |
2153 | my ($dir, $file) = split_path($m->{file_b}); |
2154 | my $pbat = $self->ensure_path($dir); | |
44320b9e | 2155 | print "\tD\t$m->{file_b}\n" unless $::_q; |
a5e0cedc EW |
2156 | $self->delete_entry($m->{file_b}, $pbat); |
2157 | } | |
2158 | ||
2159 | sub close_edit { | |
2160 | my ($self) = @_; | |
2161 | my ($p,$bat) = ($self->{pool}, $self->{bat}); | |
2162 | foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) { | |
2163 | $self->close_directory($bat->{$_}, $p); | |
2164 | } | |
2165 | $self->SUPER::close_edit($p); | |
2166 | $p->clear; | |
2167 | } | |
2168 | ||
2169 | sub abort_edit { | |
2170 | my ($self) = @_; | |
2171 | $self->SUPER::abort_edit($self->{pool}); | |
61395354 EW |
2172 | } |
2173 | ||
2174 | sub DESTROY { | |
2175 | my $self = shift; | |
2176 | $self->SUPER::DESTROY(@_); | |
a5e0cedc EW |
2177 | $self->{pool}->clear; |
2178 | } | |
2179 | ||
44320b9e EW |
2180 | # this drives the editor |
2181 | sub apply_diff { | |
61395354 EW |
2182 | my ($self) = @_; |
2183 | my $mods = $self->{mods}; | |
44320b9e | 2184 | my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 ); |
6e8548cc | 2185 | foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) { |
44320b9e EW |
2186 | my $f = $m->{chg}; |
2187 | if (defined $o{$f}) { | |
2188 | $self->$f($m); | |
2189 | } else { | |
2190 | fatal("Invalid change type: $f\n"); | |
2191 | } | |
2192 | } | |
24e22aa8 | 2193 | $self->rmdirs if $_rmdir; |
6e8548cc | 2194 | if (@$mods == 0) { |
44320b9e EW |
2195 | $self->abort_edit; |
2196 | } else { | |
2197 | $self->close_edit; | |
2198 | } | |
6e8548cc | 2199 | return scalar @$mods; |
44320b9e EW |
2200 | } |
2201 | ||
d81bf827 EW |
2202 | package Git::SVN::Ra; |
2203 | use vars qw/@ISA $config_dir/; | |
2204 | use strict; | |
2205 | use warnings; | |
2206 | my ($can_do_switch); | |
5d3b7cd5 | 2207 | my $RA; |
d81bf827 EW |
2208 | |
2209 | BEGIN { | |
2210 | # enforce temporary pool usage for some simple functions | |
2211 | my $e; | |
2212 | foreach (qw/get_latest_revnum rev_proplist get_file | |
2213 | check_path get_dir get_uuid get_repos_root/) { | |
2214 | $e .= "sub $_ { | |
2215 | my \$self = shift; | |
2216 | my \$pool = SVN::Pool->new; | |
2217 | my \@ret = \$self->SUPER::$_(\@_,\$pool); | |
2218 | \$pool->clear; | |
2219 | wantarray ? \@ret : \$ret[0]; }\n"; | |
2220 | } | |
2221 | eval $e; | |
2222 | } | |
2223 | ||
2224 | sub new { | |
2225 | my ($class, $url) = @_; | |
f6f09876 | 2226 | $url =~ s!/+$!!; |
5d3b7cd5 | 2227 | return $RA if ($RA && $RA->{url} eq $url); |
f6f09876 | 2228 | |
d81bf827 EW |
2229 | SVN::_Core::svn_config_ensure($config_dir, undef); |
2230 | my ($baton, $callbacks) = SVN::Core::auth_open_helper([ | |
2231 | SVN::Client::get_simple_provider(), | |
2232 | SVN::Client::get_ssl_server_trust_file_provider(), | |
2233 | SVN::Client::get_simple_prompt_provider( | |
2234 | \&Git::SVN::Prompt::simple, 2), | |
2235 | SVN::Client::get_ssl_client_cert_prompt_provider( | |
2236 | \&Git::SVN::Prompt::ssl_client_cert, 2), | |
2237 | SVN::Client::get_ssl_client_cert_pw_prompt_provider( | |
2238 | \&Git::SVN::Prompt::ssl_client_cert_pw, 2), | |
2239 | SVN::Client::get_username_provider(), | |
2240 | SVN::Client::get_ssl_server_trust_prompt_provider( | |
2241 | \&Git::SVN::Prompt::ssl_server_trust), | |
2242 | SVN::Client::get_username_prompt_provider( | |
2243 | \&Git::SVN::Prompt::username, 2), | |
2244 | ]); | |
2245 | my $config = SVN::Core::config_get_config($config_dir); | |
2246 | my $self = SVN::Ra->new(url => $url, auth => $baton, | |
2247 | config => $config, | |
2248 | pool => SVN::Pool->new, | |
2249 | auth_provider_callbacks => $callbacks); | |
2250 | $self->{svn_path} = $url; | |
2251 | $self->{repos_root} = $self->get_repos_root; | |
2252 | $self->{svn_path} =~ s#^\Q$self->{repos_root}\E/*##; | |
5d3b7cd5 | 2253 | $RA = bless $self, $class; |
d81bf827 EW |
2254 | } |
2255 | ||
2256 | sub DESTROY { | |
5d3b7cd5 | 2257 | # do not call the real DESTROY since we store ourselves in $RA |
d81bf827 EW |
2258 | } |
2259 | ||
d81bf827 EW |
2260 | sub get_log { |
2261 | my ($self, @args) = @_; | |
2262 | my $pool = SVN::Pool->new; | |
d81bf827 EW |
2263 | splice(@args, 3, 1) if ($SVN::Core::VERSION le '1.2.0'); |
2264 | my $ret = $self->SUPER::get_log(@args, $pool); | |
2265 | $pool->clear; | |
2266 | $ret; | |
2267 | } | |
2268 | ||
2269 | sub get_commit_editor { | |
44320b9e | 2270 | my ($self, $log, $cb, $pool) = @_; |
d81bf827 | 2271 | my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : (); |
44320b9e | 2272 | $self->SUPER::get_commit_editor($log, $cb, @lock, $pool); |
d81bf827 EW |
2273 | } |
2274 | ||
2275 | sub uuid { | |
2276 | my ($self) = @_; | |
2277 | $self->{uuid} ||= $self->get_uuid; | |
2278 | } | |
2279 | ||
2280 | sub gs_do_update { | |
8a603774 EW |
2281 | my ($self, $rev_a, $rev_b, $gs, $editor) = @_; |
2282 | my $new = ($rev_a == $rev_b); | |
2283 | my $path = $gs->{path}; | |
2284 | ||
2285 | my $ta = $self->check_path($path, $rev_a); | |
2286 | my $tb = $new ? $ta : $self->check_path($path, $rev_b); | |
2287 | return 1 if ($tb != $SVN::Node::dir && $ta != $SVN::Node::dir); | |
2288 | if ($ta == $SVN::Node::none) { | |
2289 | $rev_a = $rev_b; | |
2290 | $new = 1; | |
2291 | } | |
2292 | ||
d81bf827 | 2293 | my $pool = SVN::Pool->new; |
8b8fc068 | 2294 | $editor->set_path_strip($path); |
2b27f6c8 EW |
2295 | my (@pc) = split m#/#, $path; |
2296 | my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''), | |
8a603774 | 2297 | 1, $editor, $pool); |
d81bf827 | 2298 | my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : (); |
2b27f6c8 EW |
2299 | |
2300 | # Since we can't rely on svn_ra_reparent being available, we'll | |
2301 | # just have to do some magic with set_path to make it so | |
2302 | # we only want a partial path. | |
2303 | my $sp = ''; | |
2304 | my $final = join('/', @pc); | |
2305 | while (@pc) { | |
2306 | $reporter->set_path($sp, $rev_b, 0, @lock, $pool); | |
2307 | $sp .= '/' if length $sp; | |
2308 | $sp .= shift @pc; | |
2309 | } | |
2310 | die "BUG: '$sp' != '$final'\n" if ($sp ne $final); | |
2311 | ||
2b27f6c8 EW |
2312 | $reporter->set_path($sp, $rev_a, $new, @lock, $pool); |
2313 | ||
d81bf827 EW |
2314 | $reporter->finish_report($pool); |
2315 | $pool->clear; | |
2316 | $editor->{git_commit_ok}; | |
2317 | } | |
2318 | ||
2b27f6c8 EW |
2319 | # this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and |
2320 | # svn_ra_reparent didn't work before 1.4) | |
d81bf827 | 2321 | sub gs_do_switch { |
8a603774 EW |
2322 | my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_; |
2323 | my $path = $gs->{path}; | |
d81bf827 | 2324 | my $pool = SVN::Pool->new; |
2b27f6c8 EW |
2325 | |
2326 | my $full_url = $self->{url}; | |
2327 | my $old_url = $full_url; | |
2328 | $full_url .= "/$path" if length $path; | |
5d3b7cd5 EW |
2329 | my ($ra, $reparented); |
2330 | if ($old_url ne $full_url) { | |
2331 | if ($old_url !~ m#^svn(\+ssh)?://#) { | |
2332 | SVN::_Ra::svn_ra_reparent($self->{session}, $full_url, | |
2333 | $pool); | |
2334 | $self->{url} = $full_url; | |
2335 | $reparented = 1; | |
2336 | } else { | |
2337 | $ra = Git::SVN::Ra->new($full_url); | |
2338 | } | |
2339 | } | |
2340 | $ra ||= $self; | |
8a603774 | 2341 | my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool); |
d81bf827 | 2342 | my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : (); |
8b8fc068 | 2343 | $reporter->set_path('', $rev_a, 0, @lock, $pool); |
d81bf827 | 2344 | $reporter->finish_report($pool); |
2b27f6c8 | 2345 | |
5d3b7cd5 EW |
2346 | if ($reparented) { |
2347 | SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool); | |
2348 | $self->{url} = $old_url; | |
2349 | } | |
2b27f6c8 | 2350 | |
d81bf827 EW |
2351 | $pool->clear; |
2352 | $editor->{git_commit_ok}; | |
2353 | } | |
2354 | ||
0af9c9f9 EW |
2355 | sub gs_fetch_loop_common { |
2356 | my ($self, $base, $head, @gs) = @_; | |
2357 | my $inc = 1000; | |
2358 | my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc); | |
0af9c9f9 EW |
2359 | foreach my $gs (@gs) { |
2360 | if (my $last_commit = $gs->last_commit) { | |
2361 | $gs->assert_index_clean($last_commit); | |
2362 | } | |
0af9c9f9 EW |
2363 | } |
2364 | while (1) { | |
d4eff2bd | 2365 | my %revs; |
f7c3fc4a EW |
2366 | my $err; |
2367 | my $err_handler = $SVN::Error::handler; | |
2368 | $SVN::Error::handler = sub { | |
2369 | ($err) = @_; | |
2370 | skip_unknown_revs($err); | |
2371 | }; | |
d4eff2bd | 2372 | foreach my $gs (@gs) { |
502c1bf6 EW |
2373 | my $min_r = $min; |
2374 | my $rdb_max = $gs->rev_db_max; | |
2375 | next if $rdb_max >= $max; | |
2376 | $min_r = $rdb_max + 1 if ($rdb_max > $min_r); | |
2377 | $self->get_log([$gs->{path}], $min_r, $max, | |
2378 | 0, 1, 1, sub | |
d4eff2bd EW |
2379 | { my ($paths, $rev) = @_; |
2380 | push @{$revs{$rev}}, | |
2381 | [ $gs, | |
2382 | dup_changed_paths($paths) ] }); | |
2383 | ||
2384 | next unless ($err && $max >= $head); | |
2385 | ||
2386 | print STDERR "Path '$gs->{path}' ", | |
2387 | "was probably deleted:\n", | |
2388 | $err->expanded_message, | |
2389 | "\nWill attempt to follow ", | |
2390 | "revisions r$min .. r$max ", | |
2391 | "committed before the deletion\n"; | |
2392 | my $hi = $max; | |
2393 | while (--$hi >= $min) { | |
2394 | my $ok; | |
2395 | $self->get_log([$gs->{path}], $min, $hi, | |
2396 | 0, 1, 1, sub { | |
2397 | my ($paths, $rev) = @_; | |
2398 | $ok = $rev; | |
2399 | push @{$revs{$rev}}, [ $gs, | |
2400 | dup_changed_paths($_[0])]}); | |
2401 | if ($ok) { | |
2402 | print STDERR "r$min .. r$ok OK\n"; | |
2403 | last; | |
0af9c9f9 | 2404 | } |
d4eff2bd EW |
2405 | } |
2406 | } | |
2407 | $SVN::Error::handler = $err_handler; | |
2408 | foreach my $r (sort {$a <=> $b} keys %revs) { | |
2409 | foreach (@{$revs{$r}}) { | |
2410 | my ($gs, $paths) = @$_; | |
8a603774 EW |
2411 | my $lr = $gs->last_rev; |
2412 | next if defined $lr && $lr >= $r; | |
0af9c9f9 EW |
2413 | next if defined $gs->rev_db_get($r); |
2414 | if (my $log_entry = $gs->do_fetch($paths, $r)) { | |
2415 | $gs->do_git_commit($log_entry); | |
2416 | } | |
2417 | } | |
2418 | } | |
9c93fee5 EW |
2419 | # pre-fill the .rev_db since it'll eventually get filled in |
2420 | # with '0' x40 if something new gets committed | |
2421 | foreach my $gs (@gs) { | |
2422 | next if defined $gs->rev_db_get($max); | |
2423 | $gs->rev_db_set($max, 0 x40); | |
2424 | } | |
0af9c9f9 EW |
2425 | last if $max >= $head; |
2426 | $min = $max + 1; | |
2427 | $max += $inc; | |
2428 | $max = $head if ($max > $head); | |
2429 | } | |
0af9c9f9 EW |
2430 | } |
2431 | ||
e6434f87 EW |
2432 | sub minimize_url { |
2433 | my ($self) = @_; | |
2434 | return $self->{url} if ($self->{url} eq $self->{repos_root}); | |
2435 | my $url = $self->{repos_root}; | |
2436 | my @components = split(m!/!, $self->{svn_path}); | |
2437 | my $c = ''; | |
2438 | do { | |
2439 | $url .= "/$c" if length $c; | |
2440 | eval { (ref $self)->new($url)->get_latest_revnum }; | |
2441 | } while ($@ && ($c = shift @components)); | |
2442 | $url; | |
2443 | } | |
2444 | ||
d81bf827 EW |
2445 | sub can_do_switch { |
2446 | my $self = shift; | |
2447 | unless (defined $can_do_switch) { | |
2448 | my $pool = SVN::Pool->new; | |
2449 | my $rep = eval { | |
2450 | $self->do_switch(1, '', 0, $self->{url}, | |
2451 | SVN::Delta::Editor->new, $pool); | |
2452 | }; | |
2453 | if ($@) { | |
2454 | $can_do_switch = 0; | |
2455 | } else { | |
2456 | $rep->abort_report($pool); | |
2457 | $can_do_switch = 1; | |
2458 | } | |
2459 | $pool->clear; | |
2460 | } | |
2461 | $can_do_switch; | |
2462 | } | |
2463 | ||
0af9c9f9 EW |
2464 | sub skip_unknown_revs { |
2465 | my ($err) = @_; | |
2466 | my $errno = $err->apr_err(); | |
2467 | # Maybe the branch we're tracking didn't | |
2468 | # exist when the repo started, so it's | |
2469 | # not an error if it doesn't, just continue | |
2470 | # | |
2471 | # Wonderfully consistent library, eh? | |
2472 | # 160013 - svn:// and file:// | |
2473 | # 175002 - http(s):// | |
2474 | # 175007 - http(s):// (this repo required authorization, too...) | |
2475 | # More codes may be discovered later... | |
2476 | if ($errno == 175007 || $errno == 175002 || $errno == 160013) { | |
2477 | return; | |
2478 | } | |
2479 | die "Error from SVN, ($errno): ", $err->expanded_message,"\n"; | |
2480 | } | |
2481 | ||
2482 | # svn_log_changed_path_t objects passed to get_log are likely to be | |
2483 | # overwritten even if only the refs are copied to an external variable, | |
2484 | # so we should dup the structures in their entirety. Using an externally | |
2485 | # passed pool (instead of our temporary and quickly cleared pool in | |
2486 | # Git::SVN::Ra) does not help matters at all... | |
2487 | sub dup_changed_paths { | |
2488 | my ($paths) = @_; | |
2489 | return undef unless $paths; | |
2490 | my %ret; | |
2491 | foreach my $p (keys %$paths) { | |
2492 | my $i = $paths->{$p}; | |
2493 | my %s = map { $_ => $i->$_ } | |
2494 | qw/copyfrom_path copyfrom_rev action/; | |
2495 | $ret{$p} = \%s; | |
2496 | } | |
2497 | \%ret; | |
2498 | } | |
2499 | ||
f8c9d1d2 EW |
2500 | package Git::SVN::Log; |
2501 | use strict; | |
2502 | use warnings; | |
2503 | use POSIX qw/strftime/; | |
2504 | use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline | |
2505 | %rusers $show_commit $incremental/; | |
2506 | my $l_fmt; | |
2507 | ||
2508 | sub cmt_showable { | |
2509 | my ($c) = @_; | |
2510 | return 1 if defined $c->{r}; | |
2511 | if ($c->{l} && $c->{l}->[-1] eq "...\n" && | |
2512 | $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) { | |
44320b9e EW |
2513 | my @log = command(qw/cat-file commit/, $c->{c}); |
2514 | shift @log while ($log[0] ne "\n"); | |
2515 | shift @log; | |
2516 | @{$c->{l}} = grep !/^git-svn-id: /, @log; | |
f8c9d1d2 EW |
2517 | |
2518 | (undef, $c->{r}, undef) = ::extract_metadata( | |
44320b9e | 2519 | (grep(/^git-svn-id: /, @log))[-1]); |
f8c9d1d2 EW |
2520 | } |
2521 | return defined $c->{r}; | |
2522 | } | |
2523 | ||
2524 | sub log_use_color { | |
2525 | return 1 if $color; | |
2526 | my ($dc, $dcvar); | |
2527 | $dcvar = 'color.diff'; | |
2528 | $dc = `git-config --get $dcvar`; | |
2529 | if ($dc eq '') { | |
2530 | # nothing at all; fallback to "diff.color" | |
2531 | $dcvar = 'diff.color'; | |
2532 | $dc = `git-config --get $dcvar`; | |
2533 | } | |
2534 | chomp($dc); | |
2535 | if ($dc eq 'auto') { | |
2536 | my $pc; | |
2537 | $pc = `git-config --get color.pager`; | |
2538 | if ($pc eq '') { | |
2539 | # does not have it -- fallback to pager.color | |
2540 | $pc = `git-config --bool --get pager.color`; | |
2541 | } | |
2542 | else { | |
2543 | $pc = `git-config --bool --get color.pager`; | |
2544 | if ($?) { | |
2545 | $pc = 'false'; | |
2546 | } | |
2547 | } | |
2548 | chomp($pc); | |
2549 | if (-t *STDOUT || (defined $pager && $pc eq 'true')) { | |
2550 | return ($ENV{TERM} && $ENV{TERM} ne 'dumb'); | |
2551 | } | |
2552 | return 0; | |
2553 | } | |
2554 | return 0 if $dc eq 'never'; | |
2555 | return 1 if $dc eq 'always'; | |
2556 | chomp($dc = `git-config --bool --get $dcvar`); | |
2557 | return ($dc eq 'true'); | |
2558 | } | |
2559 | ||
2560 | sub git_svn_log_cmd { | |
2561 | my ($r_min, $r_max) = @_; | |
2562 | my $gs = Git::SVN->_new; | |
2563 | my @cmd = (qw/log --abbrev-commit --pretty=raw --default/, | |
2564 | $gs->refname); | |
2565 | push @cmd, '-r' unless $non_recursive; | |
2566 | push @cmd, qw/--raw --name-status/ if $verbose; | |
2567 | push @cmd, '--color' if log_use_color(); | |
2568 | return @cmd unless defined $r_max; | |
2569 | if ($r_max == $r_min) { | |
2570 | push @cmd, '--max-count=1'; | |
2571 | if (my $c = $gs->rev_db_get($r_max)) { | |
2572 | push @cmd, $c; | |
2573 | } | |
2574 | } else { | |
2575 | my ($c_min, $c_max); | |
2576 | $c_max = $gs->rev_db_get($r_max); | |
2577 | $c_min = $gs->rev_db_get($r_min); | |
2578 | if (defined $c_min && defined $c_max) { | |
2579 | if ($r_max > $r_max) { | |
2580 | push @cmd, "$c_min..$c_max"; | |
2581 | } else { | |
2582 | push @cmd, "$c_max..$c_min"; | |
2583 | } | |
2584 | } elsif ($r_max > $r_min) { | |
2585 | push @cmd, $c_max; | |
2586 | } else { | |
2587 | push @cmd, $c_min; | |
2588 | } | |
2589 | } | |
2590 | return @cmd; | |
2591 | } | |
2592 | ||
2593 | # adapted from pager.c | |
2594 | sub config_pager { | |
2595 | $pager ||= $ENV{GIT_PAGER} || $ENV{PAGER}; | |
2596 | if (!defined $pager) { | |
2597 | $pager = 'less'; | |
2598 | } elsif (length $pager == 0 || $pager eq 'cat') { | |
2599 | $pager = undef; | |
2600 | } | |
2601 | } | |
2602 | ||
2603 | sub run_pager { | |
2604 | return unless -t *STDOUT; | |
2605 | pipe my $rfd, my $wfd or return; | |
2606 | defined(my $pid = fork) or ::fatal "Can't fork: $!\n"; | |
2607 | if (!$pid) { | |
2608 | open STDOUT, '>&', $wfd or | |
2609 | ::fatal "Can't redirect to stdout: $!\n"; | |
2610 | return; | |
2611 | } | |
2612 | open STDIN, '<&', $rfd or ::fatal "Can't redirect stdin: $!\n"; | |
2613 | $ENV{LESS} ||= 'FRSX'; | |
2614 | exec $pager or ::fatal "Can't run pager: $! ($pager)\n"; | |
2615 | } | |
2616 | ||
21819a37 EW |
2617 | sub tz_to_s_offset { |
2618 | my ($tz) = @_; | |
2619 | $tz =~ s/(\d\d)$//; | |
2620 | return ($1 * 60) + ($tz * 3600); | |
2621 | } | |
2622 | ||
f8c9d1d2 EW |
2623 | sub get_author_info { |
2624 | my ($dest, $author, $t, $tz) = @_; | |
2625 | $author =~ s/(?:^\s*|\s*$)//g; | |
2626 | $dest->{a_raw} = $author; | |
2627 | my $au; | |
1c8443b0 | 2628 | if ($::_authors) { |
f8c9d1d2 EW |
2629 | $au = $rusers{$author} || undef; |
2630 | } | |
2631 | if (!$au) { | |
2632 | ($au) = ($author =~ /<([^>]+)\@[^>]+>$/); | |
2633 | } | |
2634 | $dest->{t} = $t; | |
2635 | $dest->{tz} = $tz; | |
2636 | $dest->{a} = $au; | |
2637 | # Date::Parse isn't in the standard Perl distro :( | |
2638 | if ($tz =~ s/^\+//) { | |
21819a37 | 2639 | $t += tz_to_s_offset($tz); |
f8c9d1d2 | 2640 | } elsif ($tz =~ s/^\-//) { |
21819a37 | 2641 | $t -= tz_to_s_offset($tz); |
f8c9d1d2 EW |
2642 | } |
2643 | $dest->{t_utc} = $t; | |
2644 | } | |
2645 | ||
2646 | sub process_commit { | |
2647 | my ($c, $r_min, $r_max, $defer) = @_; | |
2648 | if (defined $r_min && defined $r_max) { | |
2649 | if ($r_min == $c->{r} && $r_min == $r_max) { | |
2650 | show_commit($c); | |
2651 | return 0; | |
2652 | } | |
2653 | return 1 if $r_min == $r_max; | |
2654 | if ($r_min < $r_max) { | |
2655 | # we need to reverse the print order | |
2656 | return 0 if (defined $limit && --$limit < 0); | |
2657 | push @$defer, $c; | |
2658 | return 1; | |
2659 | } | |
2660 | if ($r_min != $r_max) { | |
2661 | return 1 if ($r_min < $c->{r}); | |
2662 | return 1 if ($r_max > $c->{r}); | |
2663 | } | |
2664 | } | |
2665 | return 0 if (defined $limit && --$limit < 0); | |
2666 | show_commit($c); | |
2667 | return 1; | |
2668 | } | |
2669 | ||
2670 | sub show_commit { | |
2671 | my $c = shift; | |
2672 | if ($oneline) { | |
2673 | my $x = "\n"; | |
2674 | if (my $l = $c->{l}) { | |
2675 | while ($l->[0] =~ /^\s*$/) { shift @$l } | |
2676 | $x = $l->[0]; | |
2677 | } | |
2678 | $l_fmt ||= 'A' . length($c->{r}); | |
2679 | print 'r',pack($l_fmt, $c->{r}),' | '; | |
2680 | print "$c->{c} | " if $show_commit; | |
2681 | print $x; | |
2682 | } else { | |
2683 | show_commit_normal($c); | |
2684 | } | |
2685 | } | |
2686 | ||
2687 | sub show_commit_changed_paths { | |
2688 | my ($c) = @_; | |
2689 | return unless $c->{changed}; | |
2690 | print "Changed paths:\n", @{$c->{changed}}; | |
2691 | } | |
2692 | ||
2693 | sub show_commit_normal { | |
2694 | my ($c) = @_; | |
2695 | print '-' x72, "\nr$c->{r} | "; | |
2696 | print "$c->{c} | " if $show_commit; | |
2697 | print "$c->{a} | ", strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)", | |
2698 | localtime($c->{t_utc})), ' | '; | |
2699 | my $nr_line = 0; | |
2700 | ||
2701 | if (my $l = $c->{l}) { | |
2702 | while ($l->[$#$l] eq "\n" && $#$l > 0 | |
2703 | && $l->[($#$l - 1)] eq "\n") { | |
2704 | pop @$l; | |
2705 | } | |
2706 | $nr_line = scalar @$l; | |
2707 | if (!$nr_line) { | |
2708 | print "1 line\n\n\n"; | |
2709 | } else { | |
2710 | if ($nr_line == 1) { | |
2711 | $nr_line = '1 line'; | |
2712 | } else { | |
2713 | $nr_line .= ' lines'; | |
2714 | } | |
2715 | print $nr_line, "\n"; | |
2716 | show_commit_changed_paths($c); | |
2717 | print "\n"; | |
2718 | print $_ foreach @$l; | |
2719 | } | |
2720 | } else { | |
2721 | print "1 line\n"; | |
2722 | show_commit_changed_paths($c); | |
2723 | print "\n"; | |
2724 | ||
2725 | } | |
2726 | foreach my $x (qw/raw diff/) { | |
2727 | if ($c->{$x}) { | |
2728 | print "\n"; | |
2729 | print $_ foreach @{$c->{$x}} | |
2730 | } | |
2731 | } | |
2732 | } | |
2733 | ||
2734 | sub cmd_show_log { | |
2735 | my (@args) = @_; | |
2736 | my ($r_min, $r_max); | |
2737 | my $r_last = -1; # prevent dupes | |
2738 | if (defined $TZ) { | |
2739 | $ENV{TZ} = $TZ; | |
2740 | } else { | |
2741 | delete $ENV{TZ}; | |
2742 | } | |
2743 | if (defined $::_revision) { | |
2744 | if ($::_revision =~ /^(\d+):(\d+)$/) { | |
2745 | ($r_min, $r_max) = ($1, $2); | |
2746 | } elsif ($::_revision =~ /^\d+$/) { | |
2747 | $r_min = $r_max = $::_revision; | |
2748 | } else { | |
2749 | ::fatal "-r$::_revision is not supported, use ", | |
2750 | "standard \'git log\' arguments instead\n"; | |
2751 | } | |
2752 | } | |
2753 | ||
2754 | config_pager(); | |
2755 | @args = (git_svn_log_cmd($r_min, $r_max), @args); | |
2756 | my $log = command_output_pipe(@args); | |
2757 | run_pager(); | |
2758 | my (@k, $c, $d); | |
2759 | my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/; | |
2760 | while (<$log>) { | |
2761 | if (/^${esc_color}commit ($::sha1_short)/o) { | |
2762 | my $cmt = $1; | |
2763 | if ($c && cmt_showable($c) && $c->{r} != $r_last) { | |
2764 | $r_last = $c->{r}; | |
2765 | process_commit($c, $r_min, $r_max, \@k) or | |
2766 | goto out; | |
2767 | } | |
2768 | $d = undef; | |
2769 | $c = { c => $cmt }; | |
2770 | } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) { | |
2771 | get_author_info($c, $1, $2, $3); | |
2772 | } elsif (/^${esc_color}(?:tree|parent|committer) /o) { | |
2773 | # ignore | |
2774 | } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) { | |
2775 | push @{$c->{raw}}, $_; | |
2776 | } elsif (/^${esc_color}[ACRMDT]\t/) { | |
2777 | # we could add $SVN->{svn_path} here, but that requires | |
2778 | # remote access at the moment (repo_path_split)... | |
2779 | s#^(${esc_color})([ACRMDT])\t#$1 $2 #o; | |
2780 | push @{$c->{changed}}, $_; | |
2781 | } elsif (/^${esc_color}diff /o) { | |
2782 | $d = 1; | |
2783 | push @{$c->{diff}}, $_; | |
2784 | } elsif ($d) { | |
2785 | push @{$c->{diff}}, $_; | |
2786 | } elsif (/^${esc_color} (git-svn-id:.+)$/o) { | |
2787 | ($c->{url}, $c->{r}, undef) = ::extract_metadata($1); | |
2788 | } elsif (s/^${esc_color} //o) { | |
2789 | push @{$c->{l}}, $_; | |
2790 | } | |
2791 | } | |
2792 | if ($c && defined $c->{r} && $c->{r} != $r_last) { | |
2793 | $r_last = $c->{r}; | |
2794 | process_commit($c, $r_min, $r_max, \@k); | |
2795 | } | |
2796 | if (@k) { | |
2797 | my $swap = $r_max; | |
2798 | $r_max = $r_min; | |
2799 | $r_min = $swap; | |
2800 | process_commit($_, $r_min, $r_max) foreach reverse @k; | |
2801 | } | |
2802 | out: | |
c843c464 | 2803 | close $log; |
f8c9d1d2 EW |
2804 | print '-' x72,"\n" unless $incremental || $oneline; |
2805 | } | |
2806 | ||
706587fc EW |
2807 | package Git::SVN::Migration; |
2808 | # these version numbers do NOT correspond to actual version numbers | |
2809 | # of git nor git-svn. They are just relative. | |
2810 | # | |
2811 | # v0 layout: .git/$id/info/url, refs/heads/$id-HEAD | |
2812 | # | |
2813 | # v1 layout: .git/$id/info/url, refs/remotes/$id | |
2814 | # | |
2815 | # v2 layout: .git/svn/$id/info/url, refs/remotes/$id | |
2816 | # | |
2817 | # v3 layout: .git/svn/$id, refs/remotes/$id | |
2818 | # - info/url may remain for backwards compatibility | |
2819 | # - this is what we migrate up to this layout automatically, | |
2820 | # - this will be used by git svn init on single branches | |
2821 | # | |
2822 | # v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id | |
2823 | # - this is only created for newly multi-init-ed | |
2824 | # repositories. Similar in spirit to the | |
2825 | # --use-separate-remotes option in git-clone (now default) | |
2826 | # - we do not automatically migrate to this (following | |
2827 | # the example set by core git) | |
2828 | use strict; | |
2829 | use warnings; | |
2830 | use Carp qw/croak/; | |
2831 | use File::Path qw/mkpath/; | |
47e39c55 EW |
2832 | use File::Basename qw/dirname basename/; |
2833 | use vars qw/$_minimize/; | |
706587fc EW |
2834 | |
2835 | sub migrate_from_v0 { | |
2836 | my $git_dir = $ENV{GIT_DIR}; | |
2837 | return undef unless -d $git_dir; | |
2838 | my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/); | |
2839 | my $migrated = 0; | |
2840 | while (<$fh>) { | |
2841 | chomp; | |
2842 | my ($id, $orig_ref) = ($_, $_); | |
2843 | next unless $id =~ s#^refs/heads/(.+)-HEAD$#$1#; | |
2844 | next unless -f "$git_dir/$id/info/url"; | |
2845 | my $new_ref = "refs/remotes/$id"; | |
2846 | if (::verify_ref("$new_ref^0")) { | |
2847 | print STDERR "W: $orig_ref is probably an old ", | |
2848 | "branch used by an ancient version of ", | |
2849 | "git-svn.\n", | |
2850 | "However, $new_ref also exists.\n", | |
2851 | "We will not be able ", | |
2852 | "to use this branch until this ", | |
2853 | "ambiguity is resolved.\n"; | |
2854 | next; | |
2855 | } | |
2856 | print STDERR "Migrating from v0 layout...\n" if !$migrated; | |
2857 | print STDERR "Renaming ref: $orig_ref => $new_ref\n"; | |
2858 | command_noisy('update-ref', $new_ref, $orig_ref); | |
2859 | command_noisy('update-ref', '-d', $orig_ref, $orig_ref); | |
2860 | $migrated++; | |
2861 | } | |
2862 | command_close_pipe($fh, $ctx); | |
2863 | print STDERR "Done migrating from v0 layout...\n" if $migrated; | |
2864 | $migrated; | |
2865 | } | |
2866 | ||
2867 | sub migrate_from_v1 { | |
2868 | my $git_dir = $ENV{GIT_DIR}; | |
2869 | my $migrated = 0; | |
2870 | return $migrated unless -d $git_dir; | |
2871 | my $svn_dir = "$git_dir/svn"; | |
2872 | ||
2873 | # just in case somebody used 'svn' as their $id at some point... | |
2874 | return $migrated if -d $svn_dir && ! -f "$svn_dir/info/url"; | |
2875 | ||
2876 | print STDERR "Migrating from a git-svn v1 layout...\n"; | |
2877 | mkpath([$svn_dir]); | |
2878 | print STDERR "Data from a previous version of git-svn exists, but\n\t", | |
2879 | "$svn_dir\n\t(required for this version ", | |
2880 | "($::VERSION) of git-svn) does not. exist\n"; | |
2881 | my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/); | |
2882 | while (<$fh>) { | |
2883 | my $x = $_; | |
2884 | next unless $x =~ s#^refs/remotes/##; | |
2885 | chomp $x; | |
2886 | next unless -f "$git_dir/$x/info/url"; | |
2887 | my $u = eval { ::file_to_s("$git_dir/$x/info/url") }; | |
2888 | next unless $u; | |
2889 | my $dn = dirname("$git_dir/svn/$x"); | |
2890 | mkpath([$dn]) unless -d $dn; | |
2891 | if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID: | |
2892 | mkpath(["$git_dir/svn/svn"]); | |
2893 | print STDERR " - $git_dir/$x/info => ", | |
2894 | "$git_dir/svn/$x/info\n"; | |
2895 | rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or | |
2896 | croak "$!: $x"; | |
2897 | # don't worry too much about these, they probably | |
2898 | # don't exist with repos this old (save for index, | |
2899 | # and we can easily regenerate that) | |
2900 | foreach my $f (qw/unhandled.log index .rev_db/) { | |
2901 | rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f"; | |
2902 | } | |
2903 | } else { | |
2904 | print STDERR " - $git_dir/$x => $git_dir/svn/$x\n"; | |
2905 | rename "$git_dir/$x", "$git_dir/svn/$x" or | |
2906 | croak "$!: $x"; | |
2907 | } | |
2908 | $migrated++; | |
2909 | } | |
2910 | command_close_pipe($fh, $ctx); | |
2911 | print STDERR "Done migrating from a git-svn v1 layout\n"; | |
2912 | $migrated; | |
2913 | } | |
2914 | ||
2915 | sub read_old_urls { | |
2916 | my ($l_map, $pfx, $path) = @_; | |
2917 | my @dir; | |
2918 | foreach (<$path/*>) { | |
2919 | if (-r "$_/info/url") { | |
2920 | $pfx .= '/' if $pfx && $pfx !~ m!/$!; | |
2921 | my $ref_id = $pfx . basename $_; | |
2922 | my $url = ::file_to_s("$_/info/url"); | |
2923 | $l_map->{$ref_id} = $url; | |
2924 | } elsif (-d $_) { | |
2925 | push @dir, $_; | |
2926 | } | |
2927 | } | |
2928 | foreach (@dir) { | |
2929 | my $x = $_; | |
2930 | $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o; | |
2931 | read_old_urls($l_map, $x, $_); | |
2932 | } | |
2933 | } | |
2934 | ||
2935 | sub migrate_from_v2 { | |
2936 | my @cfg = command(qw/config -l/); | |
2937 | return if grep /^svn-remote\..+\.url=/, @cfg; | |
2938 | my %l_map; | |
2939 | read_old_urls(\%l_map, '', "$ENV{GIT_DIR}/svn"); | |
2940 | my $migrated = 0; | |
2941 | ||
2942 | foreach my $ref_id (sort keys %l_map) { | |
471bc000 EW |
2943 | eval { Git::SVN->init($l_map{$ref_id}, '', undef, $ref_id) }; |
2944 | if ($@) { | |
2945 | Git::SVN->init($l_map{$ref_id}, '', $ref_id, $ref_id); | |
2946 | } | |
706587fc EW |
2947 | $migrated++; |
2948 | } | |
2949 | $migrated; | |
2950 | } | |
2951 | ||
47e39c55 EW |
2952 | sub minimize_connections { |
2953 | my $r = Git::SVN::read_all_remotes(); | |
2954 | my $new_urls = {}; | |
2955 | my $root_repos = {}; | |
2956 | foreach my $repo_id (keys %$r) { | |
2957 | my $url = $r->{$repo_id}->{url} or next; | |
2958 | my $fetch = $r->{$repo_id}->{fetch} or next; | |
2959 | my $ra = Git::SVN::Ra->new($url); | |
2960 | ||
2961 | # skip existing cases where we already connect to the root | |
2962 | if (($ra->{url} eq $ra->{repos_root}) || | |
2963 | (Git::SVN::sanitize_remote_name($ra->{repos_root}) eq | |
2964 | $repo_id)) { | |
2965 | $root_repos->{$ra->{url}} = $repo_id; | |
2966 | next; | |
2967 | } | |
2968 | ||
2969 | my $root_ra = Git::SVN::Ra->new($ra->{repos_root}); | |
2970 | my $root_path = $ra->{url}; | |
2971 | $root_path =~ s#^\Q$ra->{repos_root}\E/*##; | |
2972 | foreach my $path (keys %$fetch) { | |
2973 | my $ref_id = $fetch->{$path}; | |
2974 | my $gs = Git::SVN->new($ref_id, $repo_id, $path); | |
2975 | ||
2976 | # make sure we can read when connecting to | |
2977 | # a higher level of a repository | |
2978 | my ($last_rev, undef) = $gs->last_rev_commit; | |
2979 | if (!defined $last_rev) { | |
2980 | $last_rev = eval { | |
2981 | $root_ra->get_latest_revnum; | |
2982 | }; | |
2983 | next if $@; | |
2984 | } | |
2985 | my $new = $root_path; | |
2986 | $new .= length $path ? "/$path" : ''; | |
2987 | eval { | |
2988 | $root_ra->get_log([$new], $last_rev, $last_rev, | |
2989 | 0, 0, 1, sub { }); | |
2990 | }; | |
2991 | next if $@; | |
2992 | $new_urls->{$ra->{repos_root}}->{$new} = | |
2993 | { ref_id => $ref_id, | |
2994 | old_repo_id => $repo_id, | |
2995 | old_path => $path }; | |
2996 | } | |
2997 | } | |
2998 | ||
2999 | my @emptied; | |
3000 | foreach my $url (keys %$new_urls) { | |
3001 | # see if we can re-use an existing [svn-remote "repo_id"] | |
3002 | # instead of creating a(n ugly) new section: | |
3003 | my $repo_id = $root_repos->{$url} || | |
3004 | Git::SVN::sanitize_remote_name($url); | |
3005 | ||
3006 | my $fetch = $new_urls->{$url}; | |
3007 | foreach my $path (keys %$fetch) { | |
3008 | my $x = $fetch->{$path}; | |
3009 | Git::SVN->init($url, $path, $repo_id, $x->{ref_id}); | |
3010 | my $pfx = "svn-remote.$x->{old_repo_id}"; | |
3011 | ||
3012 | my $old_fetch = quotemeta("$x->{old_path}:". | |
3013 | "refs/remotes/$x->{ref_id}"); | |
8b8fc068 | 3014 | command_noisy(qw/config --unset/, |
47e39c55 EW |
3015 | "$pfx.fetch", '^'. $old_fetch . '$'); |
3016 | delete $r->{$x->{old_repo_id}}-> | |
3017 | {fetch}->{$x->{old_path}}; | |
3018 | if (!keys %{$r->{$x->{old_repo_id}}->{fetch}}) { | |
8b8fc068 | 3019 | command_noisy(qw/config --unset/, |
47e39c55 EW |
3020 | "$pfx.url"); |
3021 | push @emptied, $x->{old_repo_id} | |
3022 | } | |
3023 | } | |
3024 | } | |
3025 | if (@emptied) { | |
3026 | my $file = $ENV{GIT_CONFIG} || $ENV{GIT_CONFIG_LOCAL} || | |
3027 | "$ENV{GIT_DIR}/config"; | |
3028 | print STDERR <<EOF; | |
3029 | The following [svn-remote] sections in your config file ($file) are empty | |
3030 | and can be safely removed: | |
3031 | EOF | |
3032 | print STDERR "[svn-remote \"$_\"]\n" foreach @emptied; | |
3033 | } | |
3034 | } | |
3035 | ||
706587fc EW |
3036 | sub migration_check { |
3037 | migrate_from_v0(); | |
3038 | migrate_from_v1(); | |
3039 | migrate_from_v2(); | |
47e39c55 | 3040 | minimize_connections() if $_minimize; |
706587fc EW |
3041 | } |
3042 | ||
ef3cfaad EW |
3043 | package Git::IndexInfo; |
3044 | use strict; | |
3045 | use warnings; | |
3046 | use Git qw/command_input_pipe command_close_pipe/; | |
3047 | ||
3048 | sub new { | |
3049 | my ($class) = @_; | |
3050 | my ($gui, $ctx) = command_input_pipe(qw/update-index -z --index-info/); | |
3051 | bless { gui => $gui, ctx => $ctx, nr => 0}, $class; | |
3052 | } | |
3053 | ||
3054 | sub remove { | |
3055 | my ($self, $path) = @_; | |
3056 | if (print { $self->{gui} } '0 ', 0 x 40, "\t", $path, "\0") { | |
3057 | return ++$self->{nr}; | |
3058 | } | |
3059 | undef; | |
3060 | } | |
3061 | ||
3062 | sub update { | |
3063 | my ($self, $mode, $hash, $path) = @_; | |
3064 | if (print { $self->{gui} } $mode, ' ', $hash, "\t", $path, "\0") { | |
3065 | return ++$self->{nr}; | |
3066 | } | |
3067 | undef; | |
3068 | } | |
3069 | ||
3070 | sub DESTROY { | |
3071 | my ($self) = @_; | |
3072 | command_close_pipe($self->{gui}, $self->{ctx}); | |
3073 | } | |
3074 | ||
3397f9df EW |
3075 | __END__ |
3076 | ||
3077 | Data structures: | |
3078 | ||
44320b9e | 3079 | $log_entry hashref as returned by libsvn_log_entry() |
3397f9df | 3080 | { |
44320b9e | 3081 | log => 'whitespace-formatted log entry |
3397f9df EW |
3082 | ', # trailing newline is preserved |
3083 | revision => '8', # integer | |
3084 | date => '2004-02-24T17:01:44.108345Z', # commit date | |
3085 | author => 'committer name' | |
3086 | }; | |
3087 | ||
6e8548cc EW |
3088 | |
3089 | # this is generated by generate_diff(); | |
3397f9df EW |
3090 | @mods = array of diff-index line hashes, each element represents one line |
3091 | of diff-index output | |
3092 | ||
3093 | diff-index line ($m hash) | |
3094 | { | |
3095 | mode_a => first column of diff-index output, no leading ':', | |
3096 | mode_b => second column of diff-index output, | |
3097 | sha1_b => sha1sum of the final blob, | |
ac8e0b91 | 3098 | chg => change type [MCRADT], |
3397f9df EW |
3099 | file_a => original file name of a file (iff chg is 'C' or 'R') |
3100 | file_b => new/current file name of a file (any chg) | |
3101 | } | |
3102 | ; | |
a5e0cedc | 3103 | |
a00439ac EW |
3104 | # retval of read_url_paths{,_all}(); |
3105 | $l_map = { | |
3106 | # repository root url | |
3107 | 'https://svn.musicpd.org' => { | |
3108 | # repository path # GIT_SVN_ID | |
3109 | 'mpd/trunk' => 'trunk', | |
3110 | 'mpd/tags/0.11.5' => 'tags/0.11.5', | |
3111 | }, | |
3112 | } | |
3113 | ||
a5e0cedc EW |
3114 | Notes: |
3115 | I don't trust the each() function on unless I created %hash myself | |
3116 | because the internal iterator may not have started at base. |