2 * wiggle - apply rejected patches
4 * Copyright (C) 2003 Neil Brown <neilb@cse.unsw.edu.au>
5 * Copyright (C) 2010 Neil Brown <neilb@suse.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Email: <neilb@suse.de>
27 * Wiggle is a tool for working with patches that don't quite apply properly.
28 * It provides functionality similar to 'diff' and 'merge' but can
29 * work at the level of individual words thus allowing the merging of
30 * two changes that affect the same line, but not the same parts of that line.
32 * Wiggle can also read patch and merge files. Unlike 'merge' it does not
33 * need to be given three separate files, but can be given a file and a patch
34 * and it will extract the pieces of the two other files that it needs from
37 * Wiggle performs one of three core function:
38 * --extract -x extract part of a patch or merge file
39 * --diff -d report differences between two files
40 * --merge -m merge the changes between two files into a third file
42 * This is also a --browse (-B) mode which provides interactive access
45 * To perform these, wiggle requires 1, 2, or 3 input streams respectively.
46 * I can get these from individual files, from a diff (unified or context) or
50 * If one file is given, it is a merge file (output of 'merge').
51 * If two files are given, the second is assumed to be a patch,
52 * the first is a normal file.
53 * If three files are given, they are taken to be normal files.
56 * If one file is given, it is a patch
57 * If two files are given, they are normal files.
60 * Only one file can be given. -p indicates it is a patch,
61 * otherwise it is a merge.
62 * One of the flags -1 -2 or -3 must also be given and they indicate which
63 * part of the patch or merge to extract.
65 * Difference calculation and merging is performed on lines (-l) or words (-w).
66 * Each 'word' is either 1/all alphnumeric (or '_'), 2/ all space or tab,
67 * or 3/ any other single character.
69 * In the case of -w, an initial diff is computed based on non-trivial words
70 * which includes alhpanumeric words and newlines.
72 * This diff is computed from the ends of the file and is used to find
73 * a suitable starting point and range. Then a more precise diff is
74 * computed over that restricted range
76 * Other options available are:
77 * --replace -r replace first file with result of merge.
78 * --help -h provide help
79 * --version -v version
81 * Defaults are --merge --words
97 void die(char *reason)
99 fprintf(stderr, "%s: fatal error: %s failure\n", Cmd, reason);
103 void check_dir(char *name, int fd)
106 if (fstat(fd, &st) != 0) {
107 fprintf(stderr, "%s: fatal: %s is strange\n", Cmd, name);
110 if (S_ISDIR(st.st_mode)) {
111 fprintf(stderr, "%s: %s is a directory\n", Cmd, name);
116 void *xmalloc(int size)
118 void *rv = malloc(size);
120 char *msg = "Failed to allocate memory - aborting\n";
121 write(2, msg, strlen(msg));
127 void printword(FILE *f, struct elmnt e)
130 fprintf(f, "%.*s", e.plen + e.prefix,
134 sscanf(e.start+1, "%d %d %d", &a, &b, &c);
135 fprintf(f, "*** %d,%d **** %d\n", b, c, a);
139 static void printsep(struct elmnt e1, struct elmnt e2)
141 int a, b, c, d, e, f;
142 sscanf(e1.start+1, "%d %d %d", &a, &b, &c);
143 sscanf(e2.start+1, "%d %d %d", &d, &e, &f);
144 printf("@@ -%d,%d +%d,%d @@\n", b, c, e, f);
147 static int extract(int argc, char *argv[], int ispatch, int which)
149 /* extract a branch of a diff or diff3 or merge output
152 struct stream f, flist[3];
156 "%s: no file given for --extract\n", Cmd);
161 "%s: only give one file for --extract\n", Cmd);
164 f = load_file(argv[0]);
165 if (f.body == NULL) {
167 "%s: cannot load file '%s' - %s\n", Cmd,
168 argv[0], strerror(errno));
172 if (split_patch(f, &flist[0], &flist[1]) == 0) {
174 "%s: No chunk found in patch: %s\n", Cmd,
179 if (!split_merge(f, &flist[0], &flist[1], &flist[2])) {
181 "%s: merge file %s looks bad.\n", Cmd,
186 if (flist[which-'1'].body == NULL) {
188 "%s: %s has no -%c component.\n", Cmd,
192 if (write(1, flist[which-'1'].body,
193 flist[which-'1'].len)
194 != flist[which-'1'].len)
200 static int do_diff_lines(struct file fl[2], struct csl *csl)
205 while (a < fl[0].elcnt || b < fl[1].elcnt) {
207 if (fl[0].list[a].start[0]) {
214 } else if (b < csl->b) {
215 if (fl[1].list[b].start[0]) {
223 if (fl[0].list[a].start[0] == '\0')
224 printsep(fl[0].list[a],
233 if (a >= csl->a+csl->len)
240 static int do_diff_words(struct file fl[2], struct csl *csl)
244 int sol = 1; /* start of line */
246 while (a < fl[0].elcnt || b < fl[1].elcnt) {
256 for (a1 = a; a1 < csl->a ; a1++)
257 if (ends_line(fl[0].list[a1])) {
263 for (; a < csl->a ; a++) {
264 printword(stdout, fl[0].list[a]);
265 if (ends_line(fl[0].list[a])) {
278 printword(stdout, fl[0].list[a]);
279 sol = ends_line(fl[0].list[a]);
281 } while (a < csl->a);
282 printf("%s-->>>", sol ? "|" : "");
285 } else if (b < csl->b) {
290 for (b1 = b; b1 < csl->b; b1++)
291 if (ends_line(fl[1].list[b1])) {
297 for (; b < csl->b ; b++) {
298 printword(stdout, fl[1].list[b]);
299 if (ends_line(fl[1].list[b])) {
312 printword(stdout, fl[1].list[b]);
313 sol = ends_line(fl[1].list[b]);
315 } while (b < csl->b);
316 printf("%s++>>>", sol ? "|" : "");
323 for (a1 = a; a1 < csl->a+csl->len; a1++)
324 if (ends_line(fl[0].list[a1]))
327 if (fl[0].list[a].start[0]) {
329 for (; a < csl->a+csl->len; a++, b++) {
330 printword(stdout, fl[0].list[a]);
331 if (ends_line(fl[0].list[a])) {
337 printsep(fl[0].list[a], fl[1].list[b]);
344 printword(stdout, fl[0].list[a]);
345 if (ends_line(fl[0].list[a]))
350 if (a >= csl->a+csl->len)
357 static int do_diff(int argc, char *argv[], int obj, int ispatch,
358 int which, int reverse)
360 /* create a diff (line or char) of two streams */
361 struct stream f, flist[3];
362 int chunks1 = 0, chunks2 = 0, chunks3 = 0;
369 fprintf(stderr, "%s: no file given for --diff\n", Cmd);
372 f = load_file(argv[0]);
373 if (f.body == NULL) {
375 "%s: cannot load file '%s' - %s\n", Cmd,
376 argv[0], strerror(errno));
380 split_patch(f, &flist[0], &flist[1]);
381 if (!flist[0].body || !flist[1].body) {
383 "%s: couldn't parse patch %s\n", Cmd,
389 flist[0] = load_file(argv[0]);
390 if (flist[0].body == NULL) {
392 "%s: cannot load file '%s' - %s\n", Cmd,
393 argv[0], strerror(errno));
397 f = load_file(argv[1]);
398 if (f.body == NULL) {
400 "%s: cannot load patch '%s' - %s\n", Cmd,
401 argv[1], strerror(errno));
406 split_patch(f, &flist[2],
410 split_patch(f, &flist[1],
414 flist[1] = load_file(argv[1]);
415 if (flist[1].body == NULL) {
417 "%s: cannot load file '%s' - %s\n", Cmd,
418 argv[1], strerror(errno));
424 "%s: too many files given for --diff\n", Cmd);
432 fl[0] = split_stream(flist[0], obj);
433 fl[1] = split_stream(flist[1], obj);
434 if (chunks2 && !chunks1)
435 csl = pdiff(fl[0], fl[1], chunks2);
437 csl = diff_patch(fl[0], fl[1]);
438 if ((obj & ByMask) == ByLine) {
440 printf("@@ -1,%d +1,%d @@\n",
441 fl[0].elcnt, fl[1].elcnt);
442 exit_status = do_diff_lines(fl, csl);
445 /* count lines in each file */
448 for (i = 0 ; i < fl[0].elcnt ; i++)
449 if (ends_line(fl[0].list[i]))
451 for (i = 0 ; i < fl[1].elcnt ; i++)
452 if (ends_line(fl[1].list[i]))
454 printf("@@ -1,%d +1,%d @@\n", l1, l2);
456 exit_status = do_diff_words(fl, csl);
461 static int do_merge(int argc, char *argv[], int obj, int blanks,
462 int reverse, int replace, int ignore, int show_wiggles,
465 /* merge three files, A B C, so changed between B and C get made to A
467 struct stream f, flist[3];
470 int chunks1 = 0, chunks2 = 0, chunks3 = 0;
471 char *replacename = NULL, *orignew = NULL;
472 struct csl *csl1, *csl2;
474 FILE *outfile = stdout;
478 fprintf(stderr, "%s: no files given for --merge\n", Cmd);
483 for (i = 0; i < argc; i++) {
484 flist[i] = load_file(argv[i]);
485 if (flist[i].body == NULL) {
486 fprintf(stderr, "%s: cannot load file '%s' - %s\n",
488 argv[i], strerror(errno));
494 fprintf(stderr, "%s: too many files given for --merge\n",
499 case 1: /* a merge file */
501 if (!split_merge(f, &flist[0], &flist[1], &flist[2])) {
502 fprintf(stderr, "%s: merge file %s looks bad.\n",
508 case 2: /* a file and a patch */
510 chunks2 = chunks3 = split_patch(f, &flist[1], &flist[2]);
512 case 3: /* three separate files */
521 for (i = 0; i < 3; i++) {
522 if (flist[i].body == NULL) {
523 fprintf(stderr, "%s: file %d missing\n", Cmd, i);
529 replacename = xmalloc(strlen(argv[0]) + 20);
530 orignew = xmalloc(strlen(argv[0]) + 20);
531 strcpy(replacename, argv[0]);
532 strcpy(orignew, argv[0]);
533 strcat(orignew, ".porig");
534 if (open(orignew, O_RDONLY) >= 0 ||
536 fprintf(stderr, "%s: %s already exists\n",
541 strcat(replacename, "XXXXXX");
542 fd = mkstemp(replacename);
545 "%s: could not create temporary file for %s\n",
550 outfile = fdopen(fd, "w");
557 fl[0] = split_stream(flist[0], blanks);
558 fl[1] = split_stream(flist[1], blanks);
559 fl[2] = split_stream(flist[2], blanks);
561 if (chunks2 && !chunks1)
562 csl1 = pdiff(fl[0], fl[1], chunks2);
564 csl1 = diff(fl[0], fl[1]);
565 csl2 = diff_patch(fl[1], fl[2]);
567 ci = make_merger(fl[0], fl[1], fl[2], csl1, csl2,
568 obj == 'w', ignore, show_wiggles);
569 print_merge(outfile, &fl[0], &fl[1], &fl[2],
570 obj == 'w', ci.merger);
571 if (!quiet && ci.conflicts)
573 "%d unresolved conflict%s found\n",
575 ci.conflicts == 1 ? "" : "s");
576 if (!quiet && ci.ignored)
578 "%d already-applied change%s ignored\n",
580 ci.ignored == 1 ? "" : "s");
584 if (rename(argv[0], orignew) == 0 &&
585 rename(replacename, argv[0]) == 0)
589 "%s: failed to move new file into place.\n",
594 return (ci.conflicts > 0);
597 static int multi_merge(int argc, char *argv[], int obj, int blanks,
598 int reverse, int ignore, int show_wiggles,
599 int replace, int strip,
611 "%s: -p in merge mode requires -r\n",
617 "%s: -p in merge mode requires exactly one file\n",
622 f = fopen(filename, "r");
624 fprintf(stderr, "%s: cannot open %s\n",
628 check_dir(filename, fileno(f));
629 pl = parse_patch(f, NULL, &num_patches);
631 if (set_prefix(pl, num_patches, strip) == 0) {
632 fprintf(stderr, "%s: aborting\n", Cmd);
635 for (i = 0; i < num_patches; i++) {
638 asprintf(&name, "_wiggle_:%d:%d:%s",
639 pl[i].start, pl[i].end, filename);
642 rv |= do_merge(2, av, obj, blanks, reverse, 1, ignore,
643 show_wiggles, quiet);
648 int main(int argc, char *argv[])
658 int verbose = 0, quiet = 0;
662 int show_wiggles = 0;
666 int ignore_blanks = 0;
668 trace = getenv("WIGGLE_TRACE");
672 while ((opt = getopt_long(argc, argv,
673 short_options, long_options,
674 &option_index)) != -1)
680 helpmsg = HelpExtract;
689 helpmsg = HelpBrowse;
692 fputs(helpmsg, stderr);
696 fputs(Version, stderr);
701 fputs(Usage, stderr);
713 "%s: mode is '%c' - cannot set to '%c'\n",
719 if (obj == 0 || obj == opt) {
724 "%s: cannot select both words and lines.\n", Cmd);
735 ignore_blanks = IgnoreBlanks;
749 if (which == 0 || which == opt) {
754 "%s: can only select one of -1, -2, -3\n", Cmd);
757 case 'p': /* 'patch' or 'strip' */
759 strip = atol(optarg);
778 vpatch(argc-optind, argv+optind, ispatch,
779 strip, reverse, replace, selftest,
781 /* should not return */
785 if (obj && mode == 'x') {
787 "%s: cannot specify --line or --word with --extract\n",
791 if (mode != 'm' && !obj)
793 if (replace && mode != 'm') {
795 "%s: --replace only allowed with --merge\n", Cmd);
798 if (mode == 'x' && !which) {
800 "%s: must specify -1, -2 or -3 with --extract\n", Cmd);
803 if (mode != 'x' && mode != 'd' && which) {
805 "%s: -1, -2 or -3 only allowed with --extract or --diff\n",
810 if (ispatch && which == '3') {
812 "%s: cannot extract -3 from a patch.\n", Cmd);
818 exit_status = extract(argc-optind, argv+optind, ispatch, which);
821 exit_status = do_diff(argc-optind, argv+optind,
822 (obj == 'l' ? ByLine : ByWord)
824 ispatch, which, reverse);
828 exit_status = multi_merge(argc-optind,
836 exit_status = do_merge(
837 argc-optind, argv+optind,
838 obj, ignore_blanks, reverse, replace,
839 ignore, show_wiggles, quiet);