1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
package SVK::Resolve;
use strict;
use SVK::I18N;
use SVK::Util qw(
slurp_fh tmpfile get_prompt edit_file
read_file can_run is_executable bsd_glob
);
use File::Copy ();
use constant Actions => {qw(
a accept e edit d diff
m merge s skip t theirs
y yours h help
)};
sub new {
my ($class, %args) = @_;
return bless(\%args, $class);
}
sub run {
my $self = shift;
%$self = (
action => $self->{action},
external => $self->{external},
@_
);
return $self->do_resolve;
}
sub init {
my $self = shift;
return if exists $self->{merged};
@{$self}{qw( yours theirs base )} = map $self->{fh}{$_}[1], qw( local new base );
$self->{merged} = tmpfile ('merged-', OPEN => 0, UNLINK => 0);
$self->{conflict} = "$self->{mfh}";
close $self->{mfh};
$self->{this_action} = $self->{action};
File::Copy::copy ($self->{conflict} => $self->{merged});
}
sub do_resolve {
my ($self) = @_;
return if !exists $self->{has_conflict};
$self->init;
my ($prompt, $default);
if ($self->{has_conflict}) {
$default = ($self->{external} ? 'm' : 'e');
$prompt = loc(
"Conflict found in %1:\ne)dit, d)iff, m)erge, s)kip, t)heirs, y)ours, h)elp? [%2] ",
$self->{path}, $default
);
}
else {
$default = 'a';
$prompt = loc(
"Merged %1:\na)ccept, e)dit, d)iff, m)erge, s)kip, t)heirs, y)ours, h)elp? [%2] ",
$self->{path}, $default
);
}
my $action = lc(delete($self->{this_action}) || get_prompt(
$prompt, qr/^[aedmstyh?]?/i
) || $default);
my ($cmd, @arg) = split(//, $action);
my $method = $self->Actions->{$cmd} || 'help';
# tail recursion, heh.
$self->$method(@arg ? @arg : '');
goto &{$self->can('do_resolve')};
}
sub edit {
my $self = shift;
edit_file ($self->{merged});
$self->{has_conflict} = index (read_file ($self->{merged}), $self->{marker}) >= 0;
return 0;
}
sub diff {
my ($self, $arg) = @_;
my ($lfn, $llabel, $rfn, $rlabel) = (
map { $self->{$_}, "$self->{path} (\U$_)" }
($arg eq 'y') ? qw( base yours ) :
($arg eq 't') ? qw( base theirs ) :
($arg eq 'm') ? qw( base merged ) :
qw( yours merged )
);
my $diff = SVN::Core::diff_file_diff( $lfn, $rfn );
no strict 'refs';
SVN::Core::diff_file_output_unified(
\*{select()}, $diff, $lfn, $rfn, $llabel, $rlabel
);
return 0;
}
sub merge {
my $self = shift;
my ($fh, $path) = @{$self}{qw( fh path )};
$self->{"label_$_"} = "$path (\U$_)" foreach qw( yours base theirs );
my ($resolver, $cmd, @args) = (is_executable(split(/ /, $self->{external}||'')) ? (
$self,
split(/ /, $self->{external}),
(map @{$self}{"label_$_" => $_}, qw( yours base theirs )),
$self->{merged},
) : $self->get_resolver);
if (!$resolver) {
print loc("Cannot launch an external merge tool for %1.\n", $path);
return;
}
# maybe some message here
print loc("Invoking merge tool '%1' for %2.\n", $resolver->name, $path);
if ($resolver->run_resolver($cmd, @args)) {
$self->{has_conflict} = (
index(read_file($self->{merged}), $self->{marker}) >= 0
);
}
else {
File::Copy::copy ($self->{conflict} => $self->{merged});
}
return 0;
}
sub accept {
my ($self, $arg) = @_;
return $self->yours if ($arg eq 'y');
return $self->theirs if ($arg eq 't');
delete $self->{has_conflict} if !$self->{has_conflict};
}
sub skip {
my $self = shift;
unlink delete $self->{merged};
delete $self->{has_conflict};
}
sub filter_conflict {
my ($self, $want) = @_;
open my $ifh, '<', $self->{conflict} or die $!;
open my $ofh, '>', $self->{merged} or die $!;
my $state = '';
my $newline = "[\r\n|\n\r?]";
my $in = qr/^[>=]{4} (YOUR|ORIGINAL|THEIR) VERSION $self->{path} $self->{marker}$newline$/;
my $out = qr/<<<< $self->{marker}$newline/;
while (<$ifh>) {
if ($_ =~ $in) {
$state = $1;
next;
}
elsif (s/$out//) {
$state = '';
next unless $want eq 'THEIR';
}
next if $state and $state ne $want;
print $ofh $_;
}
}
sub theirs {
my $self = shift;
$self->filter_conflict('THEIR');
delete $self->{has_conflict};
}
sub yours {
my $self = shift;
$self->filter_conflict('YOUR');
delete $self->{has_conflict};
}
sub filename {
my $self = shift;
$self->SVK::Command::filename;
}
sub help {
my $self = shift;
$self->SVK::Command::usage(1);
}
sub get_resolver {
my $self = shift;
my %name;
foreach my $file ( grep -e, map bsd_glob("$_/SVK/Resolve/*.pm"), @INC ) {
$file =~ /(\w+)\.pm$/i or next;
if (lc($1) eq lc($self->{external})) {
%name = ( $1 => 1 ); last;
}
$name{$1}++;
}
my @resolver;
foreach my $name ( sort keys %name ) {
eval { require "SVK/Resolve/$name.pm"; 1 } or next;
my $resolver = "SVK::Resolve::$name"->new(%$self);
my $pathname = $resolver->find_command($resolver->commands) or next;
push @resolver, [$resolver, $pathname];
}
if (@resolver > 1) {
my $range = join('|', 1..@resolver);
print loc("Multiple merge tools found, choose one:\n");
print loc(
"(to skip this question, set the %1 environment variable to one of them)\n",
'SVKMERGE',
);
my $answer = get_prompt(
join(
loc(', '),
(map { "$_)".$resolver[$_-1][0]->name } 1..@resolver),
loc('q)uit? ')
), qr/^(?:$range|[qQ])$/,
);
return if $answer =~ /[qQ]/;
@resolver = $resolver[$answer-1];
}
my ($resolver, $pathname) = @{$resolver[0]||[]} or return;
return ($resolver, $pathname, $resolver->arguments);
}
sub run_resolver {
my ($self, $cmd, @args) = @_;
my $rv = system {$cmd} ($cmd, @args);
return ($rv == 0 and -e $self->{merged});
}
sub commands {
my $self = shift;
if (ref($self) =~ /(\w*)$/) {
return lc($1);
}
}
sub paths { () }
sub name {
my $self = shift;
return $self->{external} if ref($self) eq __PACKAGE__;
return $1 if (ref($self) =~ /(\w*)$/);
}
sub find_command {
my $self = shift;
foreach my $cmd (@_) {
my $pathname = can_run($cmd, $self->paths) or next;
return $pathname;
}
return;
}
sub DESTROY {
my $self = shift;
ref($self) eq __PACKAGE__ or return;
unlink $_ for grep {defined and -f} (
$self->{merged},
);
}
1;
__DATA__
=head1 NAME
SVK::Resolve - Interactively resolve conflicts
=head1 DESCRIPTION
Accept:
a : Accept the merged/edited file.
y : Keep only changes to your file.
t : Keep only changes to their file.
Diff:
d : Diff your file against merged file.
dm : See merged changes.
dy : See your changes alone.
dt : See their changes alone.
Edit:
e : Edit merged file with an editor.
m : Run an external merge tool to edit merged file.
Misc:
s : Skip this file.
h : Print this help message.
Environment variables:
EDITOR : Editor to use for 'e'.
SVKMERGE : External merge tool to always use for 'm'.
SVKRESOLVE : The resolve action to take, instead of asking.
=cut
|