2 * wiggle - apply rejected patches
4 * Copyright (C) 2003 Neil Brown <neilb@cse.unsw.edu.au>
5 * Copyright (C) 2010-2013 Neil Brown <neilb@suse.de>
6 * Copyright (C) 2014-2020 Neil Brown <neil@brown.name>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program.
23 * Email: <neil@brown.name>
27 * split patch or merge files.
33 /* skip 'cp' past the new '\n', or all the way to 'end' */
34 static void skip_eol(char **cp, char *end)
37 while (c < end && *c != '\n')
44 /* copy one line, or to end, from 'cp' into the stream, extending
47 static void copyline(struct stream *s, char **cp, char *end)
50 char *to = s->body+s->len;
52 while (from < end && *from != '\n')
60 int wiggle_split_patch(struct stream f, struct stream *f1, struct stream *f2)
66 int acnt = 0, bcnt = 0;
69 char before[100], after[100];
72 f1->body = f2->body = NULL;
74 r1.body = wiggle_xmalloc(f.len);
75 r2.body = wiggle_xmalloc(f.len);
84 * 1 first half of context
85 * 2 second half of context
91 if (sscanf(cp, "@@ -%99s +%99s @@%99[^\n]", before, after, func) >= 2) {
93 if (sscanf(before, "%d,%d", &a, &b) == 2)
95 else if (sscanf(before, "%d", &a) == 1)
100 if (sscanf(after, "%d,%d", &c, &d) == 2)
102 else if (sscanf(after, "%d", &c) == 1)
110 } else if (sscanf(cp, "*** %d,%d ****", &a, &b) == 2) {
113 } else if (sscanf(cp, "--- %d,%d ----", &c, &d) == 2) {
117 sscanf(cp, "***************%99[^\n]", func);
120 if (state == 1 || state == 3) {
123 /* Reserve enough space for 3 integers separated
124 * by a single space, and prefixed and terminated
125 * with a null character.
130 slen = sprintf(buf+1, "%5d %5d %5d", chunks, a, acnt)+1;
131 memcpy(r1.body+r1.len, buf, slen);
137 r1.body[r1.len++] = ' ';
138 strcpy(r1.body + r1.len, f);
141 r1.body[r1.len++] = '\n';
142 r1.body[r1.len++] = '\0';
144 if (state == 2 || state == 3) {
146 /* Reserve enough space for 3 integers separated
147 * by a single space, prefixed with a null character
148 * and terminated with a new line and null character.
152 slen = sprintf(buf+1, "%5d %5d %5d\n", chunks, c, bcnt)+2;
153 memcpy(r2.body+r2.len, buf, slen);
160 if ((*cp == ' ' || *cp == '!' || *cp == '-' || *cp == '+')
163 copyline(&r1, &cp, end);
168 fprintf(stderr, "%s: bad context patch at line %d\n",
174 if ((*cp == ' ' || *cp == '!' || *cp == '-' || *cp == '+')
177 copyline(&r2, &cp, end);
182 fprintf(stderr, "%s: bad context patch/2 at line %d\n",
192 copyline(&r1, &cp, end);
193 copyline(&r2, &cp2, end);
195 } else if (*cp == '-') {
197 copyline(&r1, &cp, end);
199 } else if (*cp == '+') {
201 copyline(&r2, &cp, end);
203 } else if (*cp == '\n') {
204 /* Empty line - treat like " \n" - a blank line in both */
206 copyline(&r1, &cp, end);
207 copyline(&r2, &cp2, end);
210 fprintf(stderr, "%s: bad unified patch at line %d\n",
214 if (acnt <= 0 && bcnt <= 0)
219 if (r1.len > f.len || r2.len > f.len)
227 * extract parts of a "diff3 -m" or "wiggle -m" output
229 int wiggle_split_merge(struct stream f, struct stream *f1, struct stream *f2,
234 struct stream r1, r2, r3;
238 r1.body = wiggle_xmalloc(f.len);
239 r2.body = wiggle_xmalloc(f.len);
240 r3.body = wiggle_xmalloc(f.len);
241 r1.len = r2.len = r3.len = 0;
248 * 1 in file 1 of conflict
249 * 2 in file 2 of conflict
250 * 3 in file 3 of conflict
251 * 4 in file 2 but expecting 1/3 next
258 strncmp(cp, "<<<<<<<", 7) == 0 &&
259 (cp[7] == ' ' || cp[7] == '\n')
264 /* diff3 will do something a bit strange in
265 * the 1st and 3rd sections are the same.
272 * Without a ||||||| at all.
273 * so to know if we are in '1' or '2', skip forward
279 (peek[7] == ' ' || peek[7] == '\n')) {
280 if (strncmp(peek, "|||||||", 7) == 0 ||
281 strncmp(peek, ">>>>>>>", 7) == 0)
283 else if (strncmp(peek, "=======", 7) == 0) {
288 skip_eol(&peek, end);
292 copyline(&r1, &cp2, end);
294 copyline(&r2, &cp2, end);
295 copyline(&r3, &cp, end);
300 strncmp(cp, "|||||||", 7) == 0 &&
301 (cp[7] == ' ' || cp[7] == '\n')
306 copyline(&r1, &cp, end);
310 strncmp(cp, "=======", 7) == 0 &&
311 (cp[7] == ' ' || cp[7] == '\n')
316 copyline(&r2, &cp, end);
320 strncmp(cp, ">>>>>>>", 7) == 0 &&
321 (cp[7] == ' ' || cp[7] == '\n')
326 copyline(&r3, &cp, end);
330 strncmp(cp, "=======", 7) == 0 &&
331 (cp[7] == ' ' || cp[7] == '\n')
336 copyline(&r2, &cp, end);
340 strncmp(cp, ">>>>>>>", 7) == 0 &&
341 (cp[7] == ' ' || cp[7] == '\n')
347 copyline(&r1, &t, end);
348 copyline(&r3, &cp, end);