t1500: test_rev_parse: facilitate future test enhancements
[git/raj.git] / perl / Git / SVN / Editor.pm
blob4c4199afecac2c5fc6d918cf0d3b75449504b808
1 package Git::SVN::Editor;
2 use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
3 use strict;
4 use warnings;
5 use SVN::Core;
6 use SVN::Delta;
7 use Carp qw/croak/;
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/;
11 BEGIN {
12 @ISA = qw(SVN::Delta::Editor);
15 sub new {
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},
24 $opts->{r}, $mods);
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);
30 bless $self, $class;
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}) };
39 $self->{rm} = { };
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');
45 return $self;
48 sub generate_diff {
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";
53 } else {
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);
60 local $/ = "\0";
61 my $state = 'meta';
62 my @mods;
63 while (<$diff_fh>) {
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
67 ([MTCRAD])\d*$/xo) {
68 push @mods, { mode_a => $1, mode_b => $2,
69 sha1_a => $3, sha1_b => $4,
70 chg => $5 };
71 if ($5 =~ /^(?:C|R)$/) {
72 $state = 'file_a';
73 } else {
74 $state = 'file_b';
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";
81 $x->{file_a} = $_;
82 $state = 'file_b';
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";
91 $x->{file_b} = $_;
92 $state = 'meta';
93 } else {
94 croak "Error parsing $_\n";
97 command_close_pipe($diff_fh, $ctx);
98 \@mods;
101 sub check_diff_paths {
102 my ($ra, $pfx, $rev, $mods) = @_;
103 my %types;
104 $pfx .= '/' if length $pfx;
106 sub type_diff_paths {
107 my ($ra, $types, $path, $rev) = @_;
108 my @p = split m#/+#, $path;
109 my $c = shift @p;
110 unless (defined $types->{$c}) {
111 $types->{$c} = $ra->check_path($c, $rev);
113 while (@p) {
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);
129 \%types;
132 sub split_path {
133 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
136 sub repo_path {
137 my ($self, $path) = @_;
138 if (my $enc = $self->{pathnameencoding}) {
139 require Encode;
140 Encode::from_to($path, $enc, 'UTF-8');
142 $self->{path_prefix}.(defined $path ? $path : '');
145 sub url_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;
155 sub rmdirs {
156 my ($self) = @_;
157 my $rm = $self->{rm};
158 delete $rm->{''}; # we never delete the url we're tracking
159 return unless %$rm;
161 foreach (keys %$rm) {
162 my @d = split m#/#, $_;
163 my $c = shift @d;
164 $rm->{$c} = 1;
165 while (@d) {
166 $c .= '/' . shift @d;
167 $rm->{$c} = 1;
170 delete $rm->{$self->{svn_path}};
171 delete $rm->{''}; # we never delete the url we're tracking
172 return unless %$rm;
174 my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/,
175 $self->{tree_b});
176 local $/ = "\0";
177 while (<$fh>) {
178 chomp;
179 my @dn = split m#/#, $_;
180 while (pop @dn) {
181 delete $rm->{join '/', @dn};
183 unless (%$rm) {
184 close $fh;
185 return;
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);
196 delete $bat->{$d};
200 sub open_or_add_dir {
201 my ($self, $full_path, $baton, $deletions) = @_;
202 my $t = $self->{types}->{$full_path};
203 if (!defined $t) {
204 die "$full_path not known in r$self->{r} or we have a bug!\n";
207 no warnings 'once';
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'
221 exit 1;
224 sub ensure_path {
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;
231 my $c = shift @p;
232 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''}, $deletions);
233 while (@p) {
234 my $c0 = $c;
235 $c .= '/' . shift @p;
236 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0}, $deletions);
238 return $bat->{$c};
241 # Subroutine to convert a globbing pattern to a regular expression.
242 # From perl cookbook.
243 sub glob2pat {
244 my $globstr = shift;
245 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
246 $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
247 return '^' . $globstr . '$';
250 sub check_autoprop {
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);
262 for ($n, $v) {
263 s/^\s+//; s/\s+$//;
265 $self->change_file_prop($fbat, $n, $v);
271 sub apply_autoprops {
272 my ($self, $file, $fbat) = @_;
273 my $conf_t = ${$self->{config}}{'config'};
274 no warnings 'once';
275 # Check [miscellany]/enable-auto-props in svn configuration.
276 if (SVN::_Core::svn_config_get_bool(
277 $conf_t,
278 $SVN::_Core::SVN_CONFIG_SECTION_MISCELLANY,
279 $SVN::_Core::SVN_CONFIG_OPTION_ENABLE_AUTO_PROPS,
280 0)) {
281 # Auto-props are enabled. Enumerate them to look for matches.
282 my $callback = sub {
283 $self->check_autoprop($_[0], $_[1], $file, $fbat);
285 SVN::_Core::svn_config_enumerate(
286 $conf_t,
287 $SVN::_Core::SVN_CONFIG_SECTION_AUTO_PROPS,
288 $callback);
292 sub check_attr {
293 my ($attr,$path) = @_;
294 my $val = command_oneline("check-attr", $attr, "--", $path);
295 if ($val) { $val =~ s/^[^:]*:\s*[^:]*:\s*(.*)\s*$/$1/; }
296 return $val;
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);
315 for ($n, $v) {
316 s/^\s+//; s/\s+$//;
318 my $existing = $existing_props->{$n};
319 if (!defined($existing) || $existing ne $v) {
320 $self->change_file_prop($fbat, $n, $v);
326 sub A {
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,
331 undef, -1);
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});
339 sub C {
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,
348 $upa, $self->{r});
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});
355 sub delete_entry {
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});
363 sub R {
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,
370 $upa, $self->{r});
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);
382 sub M {
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});
394 sub T {
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/)) {
403 $self->D({
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}
407 }, $deletions);
408 $self->A({
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}
412 }, $deletions);
413 return;
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 $!;
447 return ($fh, $exp);
450 sub chg_file {
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);
461 if (-s $fh_a) {
462 my $txstream = SVN::TxDelta::new ($fh_a, $fh_b, $pool);
463 my $res = SVN::TxDelta::send_txstream($txstream, @$atd, $pool);
464 if (defined $res) {
465 die "Unexpected result from send_txstream: $res\n",
466 "(SVN::Core::VERSION: $SVN::Core::VERSION)\n";
468 } else {
469 my $got = SVN::TxDelta::send_stream($fh_b, @$atd, $pool);
470 die "Checksum mismatch\nexpected: $exp_b\ngot: $got\n"
471 if ($got ne $exp_b);
473 Git::temp_release($fh_b, 1);
474 Git::temp_release($fh_a, 1);
475 $pool->clear;
478 sub D {
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);
486 sub close_edit {
487 my ($self) = @_;
488 my ($p,$bat) = ($self->{pool}, $self->{bat});
489 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
490 next if $_ eq '';
491 $self->close_directory($bat->{$_}, $p);
493 $self->close_directory($bat->{''}, $p);
494 $self->SUPER::close_edit($p);
495 $p->clear;
498 sub abort_edit {
499 my ($self) = @_;
500 $self->SUPER::abort_edit($self->{pool});
503 sub DESTROY {
504 my $self = shift;
505 $self->SUPER::DESTROY(@_);
506 $self->{pool}->clear;
509 # this drives the editor
510 sub apply_diff {
511 my ($self) = @_;
512 my $mods = $self->{mods};
513 my %o = ( D => 0, C => 1, R => 2, A => 3, M => 4, T => 5 );
514 my %deletions;
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) {
523 my $f = $m->{chg};
524 if (defined $o{$f}) {
525 $self->$f($m, \%deletions);
526 } else {
527 fatal("Invalid change type: $f");
531 if (defined($self->{mergeinfo})) {
532 $self->change_dir_prop($self->{bat}{''}, "svn:mergeinfo",
533 $self->{mergeinfo});
535 $self->rmdirs if $_rmdir;
536 if (@$mods == 0 && !defined($self->{mergeinfo})) {
537 $self->abort_edit;
538 } else {
539 $self->close_edit;
541 return scalar @$mods;
545 __END__
547 =head1 NAME
549 Git::SVN::Editor - commit driver for "git svn set-tree" and dcommit
551 =head1 SYNOPSIS
553 use Git::SVN::Editor;
554 use Git::SVN::Ra;
556 my $ra = Git::SVN::Ra->new($url);
557 my %opts = (
558 r => 19,
559 log => "log message",
560 ra => $ra,
561 config => SVN::Core::config_get_config($svn_config_dir),
562 tree_a => "$commit^",
563 tree_b => "$commit",
564 editor_cb => sub { print "Committed r$_[0]\n"; },
565 mergeinfo => "/branches/foo:1-10",
566 svn_path => "trunk"
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/) {
572 print "matched!\n";
575 =head1 DESCRIPTION
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
583 repository.
585 The interface will change as git-svn evolves.
587 =head1 DEPENDENCIES
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
594 B<git-svn> itself.
596 =head1 SEE ALSO
598 L<SVN::Delta>,
599 L<Git::SVN::Fetcher>.
601 =head1 INCOMPATIBILITIES
603 None reported.
605 =head1 BUGS
607 None.