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%s", b, c, a, e.start+18);
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 @@%s", b, c, e, f, e1.start+18);
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, char *outfilename,
463 int ignore, int show_wiggles,
466 /* merge three files, A B C, so changed between B and C get made to A
468 struct stream f, flist[3];
471 int chunks1 = 0, chunks2 = 0, chunks3 = 0;
472 char *replacename = NULL, *orignew = NULL;
473 struct csl *csl1, *csl2;
475 FILE *outfile = stdout;
479 fprintf(stderr, "%s: no files given for --merge\n", Cmd);
484 for (i = 0; i < argc; i++) {
485 flist[i] = load_file(argv[i]);
486 if (flist[i].body == NULL) {
487 fprintf(stderr, "%s: cannot load file '%s' - %s\n",
489 argv[i], strerror(errno));
495 fprintf(stderr, "%s: too many files given for --merge\n",
500 case 1: /* a merge file */
502 if (!split_merge(f, &flist[0], &flist[1], &flist[2])) {
503 fprintf(stderr, "%s: merge file %s looks bad.\n",
509 case 2: /* a file and a patch */
511 chunks2 = chunks3 = split_patch(f, &flist[1], &flist[2]);
513 case 3: /* three separate files */
522 for (i = 0; i < 3; i++) {
523 if (flist[i].body == NULL) {
524 fprintf(stderr, "%s: file %d missing\n", Cmd, i);
529 outfile = fopen(outfilename, "w");
531 fprintf(stderr, "%s: could not create %s\n",
535 } else if (replace) {
537 replacename = xmalloc(strlen(argv[0]) + 20);
538 orignew = xmalloc(strlen(argv[0]) + 20);
539 strcpy(replacename, argv[0]);
540 strcpy(orignew, argv[0]);
541 strcat(orignew, ".porig");
542 if (open(orignew, O_RDONLY) >= 0 ||
544 fprintf(stderr, "%s: %s already exists\n",
549 strcat(replacename, "XXXXXX");
550 fd = mkstemp(replacename);
553 "%s: could not create temporary file for %s\n",
558 outfile = fdopen(fd, "w");
565 fl[0] = split_stream(flist[0], blanks);
566 fl[1] = split_stream(flist[1], blanks);
567 fl[2] = split_stream(flist[2], blanks);
569 if (chunks2 && !chunks1)
570 csl1 = pdiff(fl[0], fl[1], chunks2);
572 csl1 = diff(fl[0], fl[1]);
573 csl2 = diff_patch(fl[1], fl[2]);
575 ci = make_merger(fl[0], fl[1], fl[2], csl1, csl2,
576 obj == 'w', ignore, show_wiggles > 1);
577 print_merge(outfile, &fl[0], &fl[1], &fl[2],
578 obj == 'w', ci.merger, NULL, 0, 0);
579 if (!quiet && ci.conflicts)
581 "%d unresolved conflict%s found\n",
583 ci.conflicts == 1 ? "" : "s");
584 if (!quiet && ci.ignored)
586 "%d already-applied change%s ignored\n",
588 ci.ignored == 1 ? "" : "s");
594 if (rename(argv[0], orignew) == 0 &&
595 rename(replacename, argv[0]) == 0)
599 "%s: failed to move new file into place.\n",
605 return ci.conflicts + ci.wiggles > 0;
607 return ci.conflicts > 0;
610 static int multi_merge(int argc, char *argv[], int obj, int blanks,
611 int reverse, int ignore, int show_wiggles,
612 int replace, int strip,
624 "%s: -p in merge mode requires -r\n",
630 "%s: -p in merge mode requires exactly one file\n",
635 f = fopen(filename, "r");
637 fprintf(stderr, "%s: cannot open %s\n",
641 check_dir(filename, fileno(f));
642 pl = parse_patch(f, NULL, &num_patches);
644 if (set_prefix(pl, num_patches, strip) == 0) {
645 fprintf(stderr, "%s: aborting\n", Cmd);
648 for (i = 0; i < num_patches; i++) {
651 asprintf(&name, "_wiggle_:%d:%d:%s",
652 pl[i].start, pl[i].end, filename);
655 rv |= do_merge(2, av, obj, blanks, reverse, 1, NULL, ignore,
656 show_wiggles, quiet);
661 int main(int argc, char *argv[])
671 int verbose = 0, quiet = 0;
675 int show_wiggles = 0;
678 char *outfile = NULL;
680 int ignore_blanks = 0;
682 trace = getenv("WIGGLE_TRACE");
686 while ((opt = getopt_long(argc, argv,
687 short_options, long_options,
688 &option_index)) != -1)
694 helpmsg = HelpExtract;
703 helpmsg = HelpBrowse;
706 fputs(helpmsg, stderr);
710 fputs(Version, stderr);
715 fputs(Usage, stderr);
726 if (mode == 'B' && opt == 'd') {
727 /* Browse/diff mode */
732 "%s: mode is '%c' - cannot set to '%c'\n",
738 if (obj == 0 || obj == opt) {
743 "%s: cannot select both words and lines.\n", Cmd);
758 ignore_blanks = IgnoreBlanks;
775 if (which == 0 || which == opt) {
780 "%s: can only select one of -1, -2, -3\n", Cmd);
783 case 'p': /* 'patch' or 'strip' */
785 strip = atol(optarg);
804 vpatch(argc-optind, argv+optind, ispatch,
805 strip, reverse, replace, outfile, selftest,
807 /* should not return */
811 if (obj && mode == 'x') {
813 "%s: cannot specify --line or --word with --extract\n",
817 if (mode != 'm' && !obj)
819 if (ispatch && outfile) {
820 fprintf(stderr, "%s: --output incompatible with --patch\n",
824 if (replace && mode != 'm') {
826 "%s: --replace or --output only allowed with --merge\n", Cmd);
829 if (mode == 'x' && !which) {
831 "%s: must specify -1, -2 or -3 with --extract\n", Cmd);
834 if (mode != 'x' && mode != 'd' && which) {
836 "%s: -1, -2 or -3 only allowed with --extract or --diff\n",
841 if (ispatch && which == '3') {
843 "%s: cannot extract -3 from a patch.\n", Cmd);
849 exit_status = extract(argc-optind, argv+optind, ispatch, which);
852 exit_status = do_diff(argc-optind, argv+optind,
853 (obj == 'l' ? ByLine : ByWord)
855 ispatch, which, reverse);
859 exit_status = multi_merge(argc-optind,
867 exit_status = do_merge(
868 argc-optind, argv+optind,
869 obj, ignore_blanks, reverse, replace,
871 ignore, show_wiggles, quiet);