1 package Git::SVN::Editor;
2 use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
9 use Git qw/command command_oneline command_noisy command_output_pipe
10 command_input_pipe command_close_pipe
11 command_bidi_pipe command_close_bidi_pipe/;
13 @ISA = qw(SVN::Delta::Editor);
17 my ($class, $opts) = @_;
18 foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) {
19 die "$_ required!\n" unless (defined $opts->{$_});
22 my $pool = SVN::Pool->new;
23 my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b});
24 my $types = check_diff_paths($opts->{ra}, $opts->{svn_path},
27 # $opts->{ra} functions should not be used after this:
28 my @ce = $opts->{ra}->get_commit_editor($opts->{log},
29 $opts->{editor_cb}, $pool);
30 my $self = SVN::Delta::Editor->new(@ce, $pool);
32 foreach (qw/svn_path r tree_a tree_b/) {
33 $self->{$_} = $opts->{$_};
35 $self->{url} = $opts->{ra}->{url};
36 $self->{mods} = $mods;
37 $self->{types} = $types;
38 $self->{pool} = $pool;
39 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
41 $self->{path_prefix} = length $self->{svn_path} ?
42 "$self->{svn_path}/" : '';
43 $self->{config} = $opts->{config};
44 $self->{mergeinfo} = $opts->{mergeinfo};
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 if ($self->{url} =~ m#^https?://#) {
148 # characters are taken from subversion/libsvn_subr/path.c
149 $path =~ s#([^~a-zA-Z0-9_./!$&'()*+,-])#sprintf("%%%02X",ord($1))#eg;
151 $self->{url} . '/' . $self->repo_path($path);
156 my $rm = $self->{rm};
157 delete $rm->{''}; # we never delete the url we're tracking
160 foreach (keys %$rm) {
161 my @d = split m#/#, $_;
165 $c .= '/' . shift @d;
169 delete $rm->{$self->{svn_path}};
170 delete $rm->{''}; # we never delete the url we're tracking
173 my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/,
178 my @dn = split m#/#, $_;
180 delete $rm->{join '/', @dn};
187 command_close_pipe($fh, $ctx);
189 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
190 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
191 $self->close_directory($bat->{$d}, $p);
192 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
193 print "\tD+\t$d/\n" unless $::_q;
194 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
199 sub open_or_add_dir {
200 my ($self, $full_path, $baton, $deletions) = @_;
201 my $t = $self->{types}->{$full_path};
203 die "$full_path not known in r$self->{r} or we have a bug!\n";
207 # SVN::Node::none and SVN::Node::file are used only once,
208 # so we're shutting up Perl's warnings about them.
209 if ($t == $SVN::Node::none || defined($deletions->{$full_path})) {
210 return $self->add_directory($full_path, $baton,
211 undef, -1, $self->{pool});
212 } elsif ($t == $SVN::Node::dir) {
213 return $self->open_directory($full_path, $baton,
214 $self->{r}, $self->{pool});
215 } # no warnings 'once'
216 print STDERR "$full_path already exists in repository at ",
217 "r$self->{r} and it is not a directory (",
218 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
219 } # no warnings 'once'
224 my ($self, $path, $deletions) = @_;
225 my $bat = $self->{bat};
226 my $repo_path = $self->repo_path($path);
227 return $bat->{''} unless (length $repo_path);
229 my @p = split m#/+#, $repo_path;
231 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''}, $deletions);
234 $c .= '/' . shift @p;
235 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0}, $deletions);
240 # Subroutine to convert a globbing pattern to a regular expression.
241 # From perl cookbook.
244 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
245 $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
246 return '^' . $globstr . '$';
250 my ($self, $pattern, $properties, $file, $fbat) = @_;
251 # Convert the globbing pattern to a regular expression.
252 my $regex = glob2pat($pattern);
253 # Check if the pattern matches the file name.
254 if($file =~ m/($regex)/) {
255 # Parse the list of properties to set.
256 my @props = split(/;/, $properties);
257 foreach my $prop (@props) {
258 # Parse 'name=value' syntax and set the property.
259 if ($prop =~ /([^=]+)=(.*)/) {
260 my ($n,$v) = ($1,$2);
264 $self->change_file_prop($fbat, $n, $v);
270 sub apply_autoprops {
271 my ($self, $file, $fbat) = @_;
272 my $conf_t = ${$self->{config}}{'config'};
274 # Check [miscellany]/enable-auto-props in svn configuration.
275 if (SVN::_Core::svn_config_get_bool(
277 $SVN::_Core::SVN_CONFIG_SECTION_MISCELLANY,
278 $SVN::_Core::SVN_CONFIG_OPTION_ENABLE_AUTO_PROPS,
280 # Auto-props are enabled. Enumerate them to look for matches.
282 $self->check_autoprop($_[0], $_[1], $file, $fbat);
284 SVN::_Core::svn_config_enumerate(
286 $SVN::_Core::SVN_CONFIG_SECTION_AUTO_PROPS,
292 my ($attr,$path) = @_;
293 my $val = command_oneline("check-attr", $attr, "--", $path);
294 if ($val) { $val =~ s/^[^:]*:\s*[^:]*:\s*(.*)\s*$/$1/; }
298 sub apply_manualprops {
299 my ($self, $file, $fbat) = @_;
300 my $pending_properties = check_attr( "svn-properties", $file );
301 if ($pending_properties eq "") { return; }
302 # Parse the list of properties to set.
303 my @props = split(/;/, $pending_properties);
304 # TODO: get existing properties to compare to
305 # - this fails for add so currently not done
306 # my $existing_props = ::get_svnprops($file);
307 my $existing_props = {};
308 # TODO: caching svn properties or storing them in .gitattributes
309 # would make that faster
310 foreach my $prop (@props) {
311 # Parse 'name=value' syntax and set the property.
312 if ($prop =~ /([^=]+)=(.*)/) {
313 my ($n,$v) = ($1,$2);
317 my $existing = $existing_props->{$n};
318 if (!defined($existing) || $existing ne $v) {
319 $self->change_file_prop($fbat, $n, $v);
326 my ($self, $m, $deletions) = @_;
327 my ($dir, $file) = split_path($m->{file_b});
328 my $pbat = $self->ensure_path($dir, $deletions);
329 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
331 print "\tA\t$m->{file_b}\n" unless $::_q;
332 $self->apply_autoprops($file, $fbat);
333 $self->apply_manualprops($m->{file_b}, $fbat);
334 $self->chg_file($fbat, $m);
335 $self->close_file($fbat,undef,$self->{pool});
339 my ($self, $m, $deletions) = @_;
340 my ($dir, $file) = split_path($m->{file_b});
341 my $pbat = $self->ensure_path($dir, $deletions);
342 # workaround for a bug in svn serf backend (v1.8.5 and below):
343 # store third argument to ->add_file() in a local variable, to make it
344 # have the same lifetime as $fbat
345 my $upa = $self->url_path($m->{file_a});
346 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
348 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
349 $self->apply_manualprops($m->{file_b}, $fbat);
350 $self->chg_file($fbat, $m);
351 $self->close_file($fbat,undef,$self->{pool});
355 my ($self, $path, $pbat) = @_;
356 my $rpath = $self->repo_path($path);
357 my ($dir, $file) = split_path($rpath);
358 $self->{rm}->{$dir} = 1;
359 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
363 my ($self, $m, $deletions) = @_;
364 my ($dir, $file) = split_path($m->{file_b});
365 my $pbat = $self->ensure_path($dir, $deletions);
366 # workaround for a bug in svn serf backend, see comment in C() above
367 my $upa = $self->url_path($m->{file_a});
368 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
370 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
371 $self->apply_autoprops($file, $fbat);
372 $self->apply_manualprops($m->{file_b}, $fbat);
373 $self->chg_file($fbat, $m);
374 $self->close_file($fbat,undef,$self->{pool});
376 ($dir, $file) = split_path($m->{file_a});
377 $pbat = $self->ensure_path($dir, $deletions);
378 $self->delete_entry($m->{file_a}, $pbat);
382 my ($self, $m, $deletions) = @_;
383 my ($dir, $file) = split_path($m->{file_b});
384 my $pbat = $self->ensure_path($dir, $deletions);
385 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
386 $pbat,$self->{r},$self->{pool});
387 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q;
388 $self->apply_manualprops($m->{file_b}, $fbat);
389 $self->chg_file($fbat, $m);
390 $self->close_file($fbat,undef,$self->{pool});
394 my ($self, $m, $deletions) = @_;
396 # Work around subversion issue 4091: toggling the "is a
397 # symlink" property requires removing and re-adding a
398 # file or else "svn up" on affected clients trips an
399 # assertion and aborts.
400 if (($m->{mode_b} =~ /^120/ && $m->{mode_a} !~ /^120/) ||
401 ($m->{mode_b} !~ /^120/ && $m->{mode_a} =~ /^120/)) {
403 mode_a => $m->{mode_a}, mode_b => '000000',
404 sha1_a => $m->{sha1_a}, sha1_b => '0' x 40,
405 chg => 'D', file_b => $m->{file_b}
408 mode_a => '000000', mode_b => $m->{mode_b},
409 sha1_a => '0' x 40, sha1_b => $m->{sha1_b},
410 chg => 'A', file_b => $m->{file_b}
415 $self->M($m, $deletions);
418 sub change_file_prop {
419 my ($self, $fbat, $pname, $pval) = @_;
420 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
423 sub change_dir_prop {
424 my ($self, $pbat, $pname, $pval) = @_;
425 $self->SUPER::change_dir_prop($pbat, $pname, $pval, $self->{pool});
428 sub _chg_file_get_blob ($$$$) {
429 my ($self, $fbat, $m, $which) = @_;
430 my $fh = $::_repository->temp_acquire("git_blob_$which");
431 if ($m->{"mode_$which"} =~ /^120/) {
432 print $fh 'link ' or croak $!;
433 $self->change_file_prop($fbat,'svn:special','*');
434 } elsif ($m->{mode_a} =~ /^120/ && $m->{"mode_$which"} !~ /^120/) {
435 $self->change_file_prop($fbat,'svn:special',undef);
437 my $blob = $m->{"sha1_$which"};
438 return ($fh,) if ($blob =~ /^0{40}$/);
439 my $size = $::_repository->cat_blob($blob, $fh);
440 croak "Failed to read object $blob" if ($size < 0);
441 $fh->flush == 0 or croak $!;
442 seek $fh, 0, 0 or croak $!;
444 my $exp = ::md5sum($fh);
445 seek $fh, 0, 0 or croak $!;
450 my ($self, $fbat, $m) = @_;
451 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
452 $self->change_file_prop($fbat,'svn:executable','*');
453 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
454 $self->change_file_prop($fbat,'svn:executable',undef);
456 my ($fh_a, $exp_a) = _chg_file_get_blob $self, $fbat, $m, 'a';
457 my ($fh_b, $exp_b) = _chg_file_get_blob $self, $fbat, $m, 'b';
458 my $pool = SVN::Pool->new;
459 my $atd = $self->apply_textdelta($fbat, $exp_a, $pool);
461 my $txstream = SVN::TxDelta::new ($fh_a, $fh_b, $pool);
462 my $res = SVN::TxDelta::send_txstream($txstream, @$atd, $pool);
464 die "Unexpected result from send_txstream: $res\n",
465 "(SVN::Core::VERSION: $SVN::Core::VERSION)\n";
468 my $got = SVN::TxDelta::send_stream($fh_b, @$atd, $pool);
469 die "Checksum mismatch\nexpected: $exp_b\ngot: $got\n"
472 Git::temp_release($fh_b, 1);
473 Git::temp_release($fh_a, 1);
478 my ($self, $m, $deletions) = @_;
479 my ($dir, $file) = split_path($m->{file_b});
480 my $pbat = $self->ensure_path($dir, $deletions);
481 print "\tD\t$m->{file_b}\n" unless $::_q;
482 $self->delete_entry($m->{file_b}, $pbat);
487 my ($p,$bat) = ($self->{pool}, $self->{bat});
488 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
490 $self->close_directory($bat->{$_}, $p);
492 $self->close_directory($bat->{''}, $p);
493 $self->SUPER::close_edit($p);
499 $self->SUPER::abort_edit($self->{pool});
504 $self->SUPER::DESTROY(@_);
505 $self->{pool}->clear;
508 # this drives the editor
511 my $mods = $self->{mods};
512 my %o = ( D => 0, C => 1, R => 2, A => 3, M => 4, T => 5 );
515 foreach my $m (@$mods) {
516 if ($m->{chg} eq "D") {
517 $deletions{$m->{file_b}} = 1;
521 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
523 if (defined $o{$f}) {
524 $self->$f($m, \%deletions);
526 fatal("Invalid change type: $f");
530 if (defined($self->{mergeinfo})) {
531 $self->change_dir_prop($self->{bat}{''}, "svn:mergeinfo",
534 $self->rmdirs if $_rmdir;
535 if (@$mods == 0 && !defined($self->{mergeinfo})) {
540 return scalar @$mods;
548 Git::SVN::Editor - commit driver for "git svn set-tree" and dcommit
552 use Git::SVN::Editor;
555 my $ra = Git::SVN::Ra->new($url);
558 log => "log message",
560 config => SVN::Core::config_get_config($svn_config_dir),
561 tree_a => "$commit^",
563 editor_cb => sub { print "Committed r$_[0]\n"; },
564 mergeinfo => "/branches/foo:1-10",
567 Git::SVN::Editor->new(\%opts)->apply_diff or print "No changes\n";
569 my $re = Git::SVN::Editor::glob2pat("trunk/*");
570 if ($branchname =~ /$re/) {
576 This module is an implementation detail of the "git svn" command.
577 Do not use it unless you are developing git-svn.
579 This module adapts the C<SVN::Delta::Editor> object returned by
580 C<SVN::Delta::get_commit_editor> and drives it to convey the
581 difference between two git tree objects to a remote Subversion
584 The interface will change as git-svn evolves.
588 Subversion perl bindings,
589 the core L<Carp> and L<IO::File> modules,
590 and git's L<Git> helper module.
592 C<Git::SVN::Editor> has not been tested using callers other than
598 L<Git::SVN::Fetcher>.
600 =head1 INCOMPATIBILITIES