1 package Git::SVN::Editor;
2 use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
8 use Git qw/command command_oneline command_noisy command_output_pipe
9 command_input_pipe command_close_pipe
10 command_bidi_pipe command_close_bidi_pipe/;
12 @ISA = qw(SVN::Delta::Editor);
16 my ($class, $opts) = @_;
17 foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) {
18 die "$_ required!\n" unless (defined $opts->{$_});
21 my $pool = SVN::Pool->new;
22 my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b});
23 my $types = check_diff_paths($opts->{ra}, $opts->{svn_path},
26 # $opts->{ra} functions should not be used after this:
27 my @ce = $opts->{ra}->get_commit_editor($opts->{log},
28 $opts->{editor_cb}, $pool);
29 my $self = SVN::Delta::Editor->new(@ce, $pool);
31 foreach (qw/svn_path r tree_a tree_b/) {
32 $self->{$_} = $opts->{$_};
34 $self->{url} = $opts->{ra}->{url};
35 $self->{mods} = $mods;
36 $self->{types} = $types;
37 $self->{pool} = $pool;
38 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
40 $self->{path_prefix} = length $self->{svn_path} ?
41 "$self->{svn_path}/" : '';
42 $self->{config} = $opts->{config};
43 $self->{mergeinfo} = $opts->{mergeinfo};
44 $self->{pathnameencoding} = Git::config('svn.pathnameencoding');
49 my ($tree_a, $tree_b) = @_;
50 my @diff_tree = qw(diff-tree -z -r);
51 if ($_cp_similarity) {
52 push @diff_tree, "-C$_cp_similarity";
54 push @diff_tree, '-C';
56 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
57 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
58 push @diff_tree, $tree_a, $tree_b;
59 my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
64 chomp $_; # this gets rid of the trailing "\0"
65 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
66 ($::sha1)\s($::sha1)\s
68 push @mods, { mode_a => $1, mode_b => $2,
69 sha1_a => $3, sha1_b => $4,
71 if ($5 =~ /^(?:C|R)$/) {
76 } elsif ($state eq 'file_a') {
77 my $x = $mods[$#mods] or croak "Empty array\n";
78 if ($x->{chg} !~ /^(?:C|R)$/) {
79 croak "Error parsing $_, $x->{chg}\n";
83 } elsif ($state eq 'file_b') {
84 my $x = $mods[$#mods] or croak "Empty array\n";
85 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
86 croak "Error parsing $_, $x->{chg}\n";
88 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
89 croak "Error parsing $_, $x->{chg}\n";
94 croak "Error parsing $_\n";
97 command_close_pipe($diff_fh, $ctx);
101 sub check_diff_paths {
102 my ($ra, $pfx, $rev, $mods) = @_;
104 $pfx .= '/' if length $pfx;
106 sub type_diff_paths {
107 my ($ra, $types, $path, $rev) = @_;
108 my @p = split m#/+#, $path;
110 unless (defined $types->{$c}) {
111 $types->{$c} = $ra->check_path($c, $rev);
114 $c .= '/' . shift @p;
115 next if defined $types->{$c};
116 $types->{$c} = $ra->check_path($c, $rev);
120 foreach my $m (@$mods) {
121 foreach my $f (qw/file_a file_b/) {
122 next unless defined $m->{$f};
123 my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#);
124 if (length $pfx.$dir && ! defined $types{$dir}) {
125 type_diff_paths($ra, \%types, $pfx.$dir, $rev);
133 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
137 my ($self, $path) = @_;
138 if (my $enc = $self->{pathnameencoding}) {
140 Encode::from_to($path, $enc, 'UTF-8');
142 $self->{path_prefix}.(defined $path ? $path : '');
146 my ($self, $path) = @_;
147 $path = $self->repo_path($path);
148 if ($self->{url} =~ m#^https?://#) {
149 # characters are taken from subversion/libsvn_subr/path.c
150 $path =~ s#([^~a-zA-Z0-9_./!$&'()*+,-])#sprintf("%%%02X",ord($1))#eg;
152 $self->{url} . '/' . $path;
157 my $rm = $self->{rm};
158 delete $rm->{''}; # we never delete the url we're tracking
161 foreach (keys %$rm) {
162 my @d = split m#/#, $_;
166 $c .= '/' . shift @d;
170 delete $rm->{$self->{svn_path}};
171 delete $rm->{''}; # we never delete the url we're tracking
174 my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/,
179 my @dn = split m#/#, $_;
181 delete $rm->{join '/', @dn};
188 command_close_pipe($fh, $ctx);
190 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
191 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
192 $self->close_directory($bat->{$d}, $p);
193 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
194 print "\tD+\t$d/\n" unless $::_q;
195 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
200 sub open_or_add_dir {
201 my ($self, $full_path, $baton, $deletions) = @_;
202 my $t = $self->{types}->{$full_path};
204 die "$full_path not known in r$self->{r} or we have a bug!\n";
208 # SVN::Node::none and SVN::Node::file are used only once,
209 # so we're shutting up Perl's warnings about them.
210 if ($t == $SVN::Node::none || defined($deletions->{$full_path})) {
211 return $self->add_directory($full_path, $baton,
212 undef, -1, $self->{pool});
213 } elsif ($t == $SVN::Node::dir) {
214 return $self->open_directory($full_path, $baton,
215 $self->{r}, $self->{pool});
216 } # no warnings 'once'
217 print STDERR "$full_path already exists in repository at ",
218 "r$self->{r} and it is not a directory (",
219 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
220 } # no warnings 'once'
225 my ($self, $path, $deletions) = @_;
226 my $bat = $self->{bat};
227 my $repo_path = $self->repo_path($path);
228 return $bat->{''} unless (length $repo_path);
230 my @p = split m#/+#, $repo_path;
232 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''}, $deletions);
235 $c .= '/' . shift @p;
236 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0}, $deletions);
241 # Subroutine to convert a globbing pattern to a regular expression.
242 # From perl cookbook.
245 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
246 $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
247 return '^' . $globstr . '$';
251 my ($self, $pattern, $properties, $file, $fbat) = @_;
252 # Convert the globbing pattern to a regular expression.
253 my $regex = glob2pat($pattern);
254 # Check if the pattern matches the file name.
255 if($file =~ m/($regex)/) {
256 # Parse the list of properties to set.
257 my @props = split(/;/, $properties);
258 foreach my $prop (@props) {
259 # Parse 'name=value' syntax and set the property.
260 if ($prop =~ /([^=]+)=(.*)/) {
261 my ($n,$v) = ($1,$2);
265 $self->change_file_prop($fbat, $n, $v);
271 sub apply_autoprops {
272 my ($self, $file, $fbat) = @_;
273 my $conf_t = ${$self->{config}}{'config'};
275 # Check [miscellany]/enable-auto-props in svn configuration.
276 if (SVN::_Core::svn_config_get_bool(
278 $SVN::_Core::SVN_CONFIG_SECTION_MISCELLANY,
279 $SVN::_Core::SVN_CONFIG_OPTION_ENABLE_AUTO_PROPS,
281 # Auto-props are enabled. Enumerate them to look for matches.
283 $self->check_autoprop($_[0], $_[1], $file, $fbat);
285 SVN::_Core::svn_config_enumerate(
287 $SVN::_Core::SVN_CONFIG_SECTION_AUTO_PROPS,
293 my ($attr,$path) = @_;
294 my $val = command_oneline("check-attr", $attr, "--", $path);
295 if ($val) { $val =~ s/^[^:]*:\s*[^:]*:\s*(.*)\s*$/$1/; }
299 sub apply_manualprops {
300 my ($self, $file, $fbat) = @_;
301 my $pending_properties = check_attr( "svn-properties", $file );
302 if ($pending_properties eq "") { return; }
303 # Parse the list of properties to set.
304 my @props = split(/;/, $pending_properties);
305 # TODO: get existing properties to compare to
306 # - this fails for add so currently not done
307 # my $existing_props = ::get_svnprops($file);
308 my $existing_props = {};
309 # TODO: caching svn properties or storing them in .gitattributes
310 # would make that faster
311 foreach my $prop (@props) {
312 # Parse 'name=value' syntax and set the property.
313 if ($prop =~ /([^=]+)=(.*)/) {
314 my ($n,$v) = ($1,$2);
318 my $existing = $existing_props->{$n};
319 if (!defined($existing) || $existing ne $v) {
320 $self->change_file_prop($fbat, $n, $v);
327 my ($self, $m, $deletions) = @_;
328 my ($dir, $file) = split_path($m->{file_b});
329 my $pbat = $self->ensure_path($dir, $deletions);
330 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
332 print "\tA\t$m->{file_b}\n" unless $::_q;
333 $self->apply_autoprops($file, $fbat);
334 $self->apply_manualprops($m->{file_b}, $fbat);
335 $self->chg_file($fbat, $m);
336 $self->close_file($fbat,undef,$self->{pool});
340 my ($self, $m, $deletions) = @_;
341 my ($dir, $file) = split_path($m->{file_b});
342 my $pbat = $self->ensure_path($dir, $deletions);
343 # workaround for a bug in svn serf backend (v1.8.5 and below):
344 # store third argument to ->add_file() in a local variable, to make it
345 # have the same lifetime as $fbat
346 my $upa = $self->url_path($m->{file_a});
347 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
349 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
350 $self->apply_manualprops($m->{file_b}, $fbat);
351 $self->chg_file($fbat, $m);
352 $self->close_file($fbat,undef,$self->{pool});
356 my ($self, $path, $pbat) = @_;
357 my $rpath = $self->repo_path($path);
358 my ($dir, $file) = split_path($rpath);
359 $self->{rm}->{$dir} = 1;
360 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
364 my ($self, $m, $deletions) = @_;
365 my ($dir, $file) = split_path($m->{file_b});
366 my $pbat = $self->ensure_path($dir, $deletions);
367 # workaround for a bug in svn serf backend, see comment in C() above
368 my $upa = $self->url_path($m->{file_a});
369 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
371 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
372 $self->apply_autoprops($file, $fbat);
373 $self->apply_manualprops($m->{file_b}, $fbat);
374 $self->chg_file($fbat, $m);
375 $self->close_file($fbat,undef,$self->{pool});
377 ($dir, $file) = split_path($m->{file_a});
378 $pbat = $self->ensure_path($dir, $deletions);
379 $self->delete_entry($m->{file_a}, $pbat);
383 my ($self, $m, $deletions) = @_;
384 my ($dir, $file) = split_path($m->{file_b});
385 my $pbat = $self->ensure_path($dir, $deletions);
386 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
387 $pbat,$self->{r},$self->{pool});
388 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q;
389 $self->apply_manualprops($m->{file_b}, $fbat);
390 $self->chg_file($fbat, $m);
391 $self->close_file($fbat,undef,$self->{pool});
395 my ($self, $m, $deletions) = @_;
397 # Work around subversion issue 4091: toggling the "is a
398 # symlink" property requires removing and re-adding a
399 # file or else "svn up" on affected clients trips an
400 # assertion and aborts.
401 if (($m->{mode_b} =~ /^120/ && $m->{mode_a} !~ /^120/) ||
402 ($m->{mode_b} !~ /^120/ && $m->{mode_a} =~ /^120/)) {
404 mode_a => $m->{mode_a}, mode_b => '000000',
405 sha1_a => $m->{sha1_a}, sha1_b => '0' x 40,
406 chg => 'D', file_b => $m->{file_b}
409 mode_a => '000000', mode_b => $m->{mode_b},
410 sha1_a => '0' x 40, sha1_b => $m->{sha1_b},
411 chg => 'A', file_b => $m->{file_b}
416 $self->M($m, $deletions);
419 sub change_file_prop {
420 my ($self, $fbat, $pname, $pval) = @_;
421 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
424 sub change_dir_prop {
425 my ($self, $pbat, $pname, $pval) = @_;
426 $self->SUPER::change_dir_prop($pbat, $pname, $pval, $self->{pool});
429 sub _chg_file_get_blob ($$$$) {
430 my ($self, $fbat, $m, $which) = @_;
431 my $fh = $::_repository->temp_acquire("git_blob_$which");
432 if ($m->{"mode_$which"} =~ /^120/) {
433 print $fh 'link ' or croak $!;
434 $self->change_file_prop($fbat,'svn:special','*');
435 } elsif ($m->{mode_a} =~ /^120/ && $m->{"mode_$which"} !~ /^120/) {
436 $self->change_file_prop($fbat,'svn:special',undef);
438 my $blob = $m->{"sha1_$which"};
439 return ($fh,) if ($blob =~ /^0{40}$/);
440 my $size = $::_repository->cat_blob($blob, $fh);
441 croak "Failed to read object $blob" if ($size < 0);
442 $fh->flush == 0 or croak $!;
443 seek $fh, 0, 0 or croak $!;
445 my $exp = ::md5sum($fh);
446 seek $fh, 0, 0 or croak $!;
451 my ($self, $fbat, $m) = @_;
452 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
453 $self->change_file_prop($fbat,'svn:executable','*');
454 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
455 $self->change_file_prop($fbat,'svn:executable',undef);
457 my ($fh_a, $exp_a) = _chg_file_get_blob $self, $fbat, $m, 'a';
458 my ($fh_b, $exp_b) = _chg_file_get_blob $self, $fbat, $m, 'b';
459 my $pool = SVN::Pool->new;
460 my $atd = $self->apply_textdelta($fbat, $exp_a, $pool);
462 my $txstream = SVN::TxDelta::new ($fh_a, $fh_b, $pool);
463 my $res = SVN::TxDelta::send_txstream($txstream, @$atd, $pool);
465 die "Unexpected result from send_txstream: $res\n",
466 "(SVN::Core::VERSION: $SVN::Core::VERSION)\n";
469 my $got = SVN::TxDelta::send_stream($fh_b, @$atd, $pool);
470 die "Checksum mismatch\nexpected: $exp_b\ngot: $got\n"
473 Git::temp_release($fh_b, 1);
474 Git::temp_release($fh_a, 1);
479 my ($self, $m, $deletions) = @_;
480 my ($dir, $file) = split_path($m->{file_b});
481 my $pbat = $self->ensure_path($dir, $deletions);
482 print "\tD\t$m->{file_b}\n" unless $::_q;
483 $self->delete_entry($m->{file_b}, $pbat);
488 my ($p,$bat) = ($self->{pool}, $self->{bat});
489 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
491 $self->close_directory($bat->{$_}, $p);
493 $self->close_directory($bat->{''}, $p);
494 $self->SUPER::close_edit($p);
500 $self->SUPER::abort_edit($self->{pool});
505 $self->SUPER::DESTROY(@_);
506 $self->{pool}->clear;
509 # this drives the editor
512 my $mods = $self->{mods};
513 my %o = ( D => 0, C => 1, R => 2, A => 3, M => 4, T => 5 );
516 foreach my $m (@$mods) {
517 if ($m->{chg} eq "D") {
518 $deletions{$m->{file_b}} = 1;
522 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
524 if (defined $o{$f}) {
525 $self->$f($m, \%deletions);
527 fatal("Invalid change type: $f");
531 if (defined($self->{mergeinfo})) {
532 $self->change_dir_prop($self->{bat}{''}, "svn:mergeinfo",
535 $self->rmdirs if $_rmdir;
536 if (@$mods == 0 && !defined($self->{mergeinfo})) {
541 return scalar @$mods;
549 Git::SVN::Editor - commit driver for "git svn set-tree" and dcommit
553 use Git::SVN::Editor;
556 my $ra = Git::SVN::Ra->new($url);
559 log => "log message",
561 config => SVN::Core::config_get_config($svn_config_dir),
562 tree_a => "$commit^",
564 editor_cb => sub { print "Committed r$_[0]\n"; },
565 mergeinfo => "/branches/foo:1-10",
568 Git::SVN::Editor->new(\%opts)->apply_diff or print "No changes\n";
570 my $re = Git::SVN::Editor::glob2pat("trunk/*");
571 if ($branchname =~ /$re/) {
577 This module is an implementation detail of the "git svn" command.
578 Do not use it unless you are developing git-svn.
580 This module adapts the C<SVN::Delta::Editor> object returned by
581 C<SVN::Delta::get_commit_editor> and drives it to convey the
582 difference between two git tree objects to a remote Subversion
585 The interface will change as git-svn evolves.
589 Subversion perl bindings,
590 the core L<Carp> module,
591 and git's L<Git> helper module.
593 C<Git::SVN::Editor> has not been tested using callers other than
599 L<Git::SVN::Fetcher>.
601 =head1 INCOMPATIBILITIES