-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcj-git-patchtool-wiggle
More file actions
executable file
·258 lines (225 loc) · 7.23 KB
/
cj-git-patchtool-wiggle
File metadata and controls
executable file
·258 lines (225 loc) · 7.23 KB
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
#!/usr/bin/perl -w
# Fri Sep 3 14:50:55 EDT 2010
(my $email='chrjae%gmail,com')=~ tr/%,/@./;
use strict;
use Getopt::Long;
use Chj::Util::AskYN qw(maybe_askyn);
use Chj::xpipe;
use Chj::Unix::Exitcode;
use Chj::xperlfunc ':all';
use Chj::xopen 'xopen_read';
use Chj::xtmpfile;
use Chj::IO::Command;
sub hash_add ($$$) {
my ($h,$k,$v)=@_;
push @{$$h{$k}}, $v;
}
$0=~ /(.*?)([^\/]+)\z/s or die "?";
my ($mydir, $myname)=($1,$2);
sub usage {
print STDERR map{"$_\n"} @_ if @_;
print "$myname basedir patchfilename
-or-
$myname basedir/patchfilename
apply patchfile, then feed the rejects to wiggle,
if those worked out, save the new diff into the patchfile.
Careful:
- runs reset --hard
- modifies the patchfile (but that should be in Git)
(Christian Jaeger <$email>)
";
exit (@_ ? 1 : 0);
}
our $verbose=0;
#our $opt_dry;
GetOptions("verbose"=> \$verbose,
"help"=> sub{usage},
#"dry-run"=> \$opt_dry,
) or exit 1;
our $patchpath = do {
if (@ARGV==2) {
my($basedir,$patchfilename)=@ARGV;
"$basedir/$patchfilename"
} elsif (@ARGV==1) {
$ARGV[0]
} else {
usage
}
};
my $oldpatch= xopen_read $patchpath;
xxsystem "git","reset","--hard";
#hm grr how again pipelines sigh whatever
my $rej= do {
my ($r,$w)=xpipe;
if (xfork) {
$w->xclose;
my @rej;
while (<$r>) {
next if /^patching file /;
next if /^Hunk #\d+ FAILED/;
chomp;
if (/\d+ out of \d+ hunks? (?:FAILED|ignored).*saving rejects to file (.*)/) {
my $file=$1;
if ($file=~ s/^'//) {
$file=~ s/'$// or die "hmm [$file] how?";
}
push @rej, $file;
} elsif ($_ eq 'Reversed (or previously applied) patch detected! Assume -R? [n] ') {
print "NOTE: $_\n";
#since i'm not sure what to do then.
} elsif ($_ eq 'Apply anyway? [n] ') {
print "NOTE: $_\n";
#grr. why does it ask when I said -f ?
} elsif ($_ eq 'Skipping patch.') {
print "NOTE: $_\n";
#HMMMMM.
} elsif (/^Hunk #\d+ succeeded/) {
# print?
print "NOTE: $_\n";
} elsif (/^patch: .* malformed patch at/) {
# handedited patch, from xemacs, hum. anyway, hope it
# will be fixed by wiggle.
print "NOTE: $_\n";
#hm. but die anyway, it seems patch does not produce
#an actual rej file then!
die "cannot proceed";
} elsif (my ($lineno) = /can't find file to patch at input line (\d+)/) {
# somehow patch reports the line after the one with
# the path (in addition to being 0-based)
$lineno--;
my $f = xopen_read $patchpath;
my @lines = $f->xreadline;
@lines >= $lineno or die "bug";
my $linestr = $lines[$lineno-1];
$linestr=~ s{^\+\+\+ b/}{} or die "bug";
my $filepath = $linestr;
die "can't find file to patch at '$patchpath' line $lineno: $filepath";
} elsif ($_ eq '') {
#eh.whatever
} else {
die "unknown message from patch: '$_'";
}
}
$r->xclose;
#my $code= xwait;
xwait;
my $code= $?; #GAH
($code==0
or $code== (1<<8))
or do {
die "patch died of a serious error, code ".exitcode($code);
};
\@rej
} else {
$r->xclose;
$w->xdup2(1);
$w->xdup2(2);
my $p= Chj::IO::Command->new_receiver("patch",
"--ignore-whitespace",
"-p1");
$oldpatch->xsendfile_to ($p);
$oldpatch->xclose;
$p->xfinish;
exit;
}
};
print "Ok, patch possibly applied.\n";
my $nwiggles=0;
my %wiggled;
for my $rejfile (@$rej) {
print "Calling wiggle for '$rejfile'..\n";
# +-copy from /opt/chj/bin/wiggle¬
my $file= $rejfile;
$file=~ s/\.rej$//
or die "file '$file' does not have suffix .rej";
$wiggled{$file}++;
{
my $porig=$file.".porig";
if (-e $porig) {
unlink $porig;
}
}
my $stat= xlstat $file;
my $res= xsystem "wiggle", "--replace", $file, $rejfile;
# reconstruct the x flag that wiggle dropped.
xchmod $stat->mode, $file;
if ($res) {
if (!maybe_askyn "wiggle exited with error code $res; continue? ") {
die "stopped by user request.\n";
}
}
$nwiggles++;
}
print "Ok, $nwiggles wiggles.\n";
sub parse_patch ($) {
my ($in)= @_;
my @headlines;
my @diffparts;
# ({ oldfilename=> ..,
# newfilename=> ..,
# lines=> [ lines.. ] }.. )
my $touchedfilename2parts= {};
my $maybe_current_collector;
while (<$in>) {
if (my ($a,$b)= m{^diff --git a/(\S*) b/(\S*)}) {
# ^XX this is a bit unsafe, could be part of commit
# message! A proper check would be for "---" then
# emptyline then diff? or something. Also assumes no
# whitespace(/special chars) in paths.
$maybe_current_collector= [$_];
my $part= +{ oldfilename=> $a,
newfilename=> $b,
lines=> $maybe_current_collector};
push @diffparts, $part;
hash_add $touchedfilename2parts, $a, $part;
hash_add $touchedfilename2parts, $b, $part
unless $a eq $b;
} else {
if ($maybe_current_collector) {
push @$maybe_current_collector, $_;
} else {
push @headlines, $_;
}
}
}
+{ headlines=> \@headlines,
diffparts=> \@diffparts,
touchedfilename2parts=> $touchedfilename2parts,
}
}
{
# Update the patchfile:
# Copy the head from oldpatch, and an assortment of diff parts
# from `git diff` and the old patch.
$oldpatch->xrewind;
my $oldp= parse_patch($oldpatch);
$oldpatch->xclose;
# Get the `git diff`, also split into parts:
my $d= Chj::IO::Command->new_sender ("git","diff");
my $gitp= parse_patch($d);
$d->xxfinish;
# Recreate the patchfile:
my $new= xtmpfile $patchpath;
$new->xprint(@{ $oldp->{headlines} });
for my $diffpart (@{ $oldp->{diffparts} }) {
my $filename= $diffpart->{newfilename};
# Take diff part from gitp if present, otherwise from
# oldp. Note that we can't just look at
# $gitp->{touchedfilename2part}{$filename} to know whether wee
# need to choose gitp, since if it wasn't wiggled, the git
# diff will still have the file but as a deletion.
if (my $ps= $gitp->{touchedfilename2parts}{$filename}) {
for my $p (@$ps) {
$new->xprint(@{$p->{lines}});
}
} else {
$new->xprint(@{$diffpart->{lines}});
}
}
$new->xclose;
$new->xputback;
}
print "Ok.\n";
#use Chj::ruse;
#use Chj::Backtrace; use Chj::repl; repl;
#use Chj::Trapl;