2 * wiggle - apply rejected patches
4 * Copyright (C) 2005 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>
30 * Second attempt at merging....
32 * We want to create a mergelist which identifies 'orig' and 'after'
33 * sections (from a and c) and conflicts (which are ranges of a,b,c which
35 * It is also helpful to differentiate 'orig' sections that aren't
36 * matched in 'b' with orig sections that are.
37 * To help with highlighting, it will be useful to know where
38 * the conflicts match the csl lists.
40 * This can all be achieved with a list of (a,b,c,c1,c1) 5-tuples.
41 * If two consecutive differ in more than one of a,b,c, it is a
43 * If only 'a' differ, it is un-matched original.
44 * If only 'b' differ, it is matched, unchanged original
45 * If only 'c' differ, it is 1
48 static inline int min(int a, int b)
53 static int check_alreadyapplied(struct file af, struct file cf,
59 for (i = 0; i < m->al; i++) {
60 if (af.list[m->a+i].len != cf.list[m->c+i].len)
62 if (strncmp(af.list[m->a+i].start,
63 cf.list[m->c+i].start,
64 af.list[m->a+i].len) != 0)
68 printf("already applied %d,%d,%d - %d,%d,%d\n",
69 m->a, m->b, m->c, m->al, m->bl, m->cl);
70 printf(" %.10s - %.10s\n", af.list[m->a].start,
73 m->type = AlreadyApplied;
77 /* A 'cut-point' is a location in the merger where it is reasonable
78 * the change the mode of display - between displaying the merger
79 * and displaying the separate streams.
80 * A 'conflict' can only be displayed as separate stream so when
81 * one is found, we need to find a preceeding and trailing cut-point
82 * and enlarge the conflict to that range.
83 * A suitable location is one where all three streams are at a line-end.
85 static int is_cutpoint(struct merge m,
86 struct file af, struct file bf, struct file cf)
88 return ((m.a == 0 || ends_line(af.list[m.a-1])) &&
89 (m.b == 0 || ends_line(bf.list[m.b-1])) &&
90 (m.c == 0 || ends_line(cf.list[m.c-1])));
93 int isolate_conflicts(struct file af, struct file bf, struct file cf,
94 struct csl *csl1, struct csl *csl2, int words,
95 struct merge *m, int show_wiggles,
98 /* A Conflict indicates that something is definitely wrong
99 * and so we need to be a bit suspicious of nearby apparent matches.
100 * To display a conflict effectively we expands its effect to
101 * include any Extraneous, Unmatched, Changed or AlreadyApplied text.
102 * Also, unless 'words', we need to include any partial lines
103 * in the Unchanged text that forms the border of a conflict.
105 * A Changed text may also border a conflict, but it can
106 * only border one conflict (where as an Unchanged can border
107 * a preceeding and a following conflict).
108 * The 'new' section of a Changed text appears in the
109 * conflict as does any part of the original before
112 * A hunk header (Extraneous) is never considered part of a
113 * conflict. It thereby can serve as a separator between
116 * Extended conflicts are marked by setting ->in_conflict in
117 * the "struct merge". This is '1' for an Unchanged, Changed,
118 * or (Extraneous) hunk header which borders the conflict,
119 * '2' for a merger which is truly in conflict, and '3' for
120 * a merger which is causing a 'wiggle'.
121 * When in_conflict == 1, the 'lo' and 'hi' fields indicate
122 * how much of the 'a' file is included in the conflict, the rest
123 * being part of the clean result.
124 * Elements in af from m->a to m->a+m->lo are in the preceeding
125 * conflict, from m->a+m->lo to m->a+m->hi are clean, and
126 * m->a+m->hi to m->a+m->al are in the following conflict.
128 * We need to ensure there is adequate context for the conflict.
129 * So ensure there are at least 3 newlines in Extraneous or
130 * Unchanged on both sides of a Conflict - but don't go so far
131 * as including a hunk header.
132 * If there are 3, and they are all in 'Unchanged' sections, then
133 * that much context is not really needed - reduce it a bit.
135 * If a wiggle is adjacent to a conflict then:
136 * - if show_wiggles is set, we just merge them
137 * - if it is not set, we still want to count the wiggle.
140 int cnt = 0, wiggles = 0;
146 for (i = 0; m[i].type != End; i++)
147 m[i].in_conflict = 0;
149 for (i = 0; m[i].type != End; i++) {
150 /* The '3' here is a count of newlines. Once we find
151 * that many newlines of the particular type, we have escaped.
153 if (m[i].type == Changed)
155 if (m[i].type == Unmatched)
157 if (m[i].type == Extraneous && bf.list[m[i].b].start[0])
158 /* hunk headers don't imply wiggles, other
159 * extraneous text does.
163 if (m[i].type != Unchanged && changed && (unmatched || extraneous)) {
170 if ((m[i].type == Conflict) ||
171 (show_wiggles && in_wiggle)) {
172 /* We have a conflict or wiggle here.
173 * First search backwards for an Unchanged marking
174 * things as in_conflict. Then find the
175 * cut-point in the Unchanged. If there isn't one,
178 * Then search forward doing the same thing.
181 m[i].in_conflict = m[i].type == Conflict ? 2 : 3;
186 if (m[j].type == Extraneous &&
187 bf.list[m[j].b].start[0] == '\0')
188 /* hunk header - not conflict any more */
190 if (m[j].in_conflict > 1)
191 /* Merge the conflicts */
193 if (!m[j].in_conflict) {
194 m[j].in_conflict = 1;
197 /* Following must set m[j].hi, or set
200 if (m[j].type == Extraneous) {
201 for (k = m[j].bl; k > 0; k--)
202 if (ends_line(bf.list[m[j].b+k-1]))
206 if (m[j].type != Unchanged &&
207 m[j].type != Changed) {
208 if (m[j].type == Conflict)
209 m[j].in_conflict = 2;
211 m[j].in_conflict = m[i].in_conflict;
214 /* If we find enough newlines in this section,
215 * then we only really need 1, but would rather
216 * it wasn't the first one. 'firstk' allows us
217 * to track which newline we actually use
224 /* need to find the last line-break, which
225 * might be after the last newline, if there
226 * is one, or might be at the start
228 for (k = m[j].al; k > 0; k--)
229 if (ends_line(af.list[m[j].a+k-1])) {
230 if (firstk > m[j].al)
242 else if (is_cutpoint(m[j], af,bf,cf))
245 /* no start-of-line found... */
248 (m[j].type == Changed)) {
249 /* this can only work if start is
250 * also a line break */
251 if (is_cutpoint(m[j], af,bf,cf))
258 m[j].in_conflict = m[i].in_conflict;
261 /* now the forward search */
263 for (j = i+1; m[j].type != End; j++) {
264 if (m[j].type == Extraneous &&
265 bf.list[m[j].b].start[0] == '\0')
266 /* hunk header - not conflict any more */
268 if (m[j].type == Extraneous) {
269 for (k = 0; k < m[j].bl; k++)
270 if (ends_line(bf.list[m[j].b+k]))
273 if (m[j].type != Unchanged &&
274 m[j].type != Changed) {
275 if (m[j].type == Conflict)
276 m[j].in_conflict = 2;
278 m[j].in_conflict = m[i].in_conflict;
281 m[j].in_conflict = 1;
287 /* need to find a line-break, which might be at
288 * the very beginning, or might be after the
289 * first newline - if there is one
291 if (is_cutpoint(m[j], af,bf,cf))
294 /* If we find enough newlines in this section,
295 * then we only really need 1, but would rather
296 * it wasn't the first one. 'firstk' allows us
297 * to track which newline we actually use
300 for (k = 0 ; k < m[j].al ; k++)
301 if (ends_line(af.list[m[j].a+k])) {
312 /* Hit end of file, pretend we found 3 newlines. */
316 m[j+1].type == Unmatched) {
317 /* If this Unmatched exceeds 3 lines, just stop here */
320 for (p = 0; p < m[j+1].al ; p++)
321 if (ends_line(af.list[m[j+1].a+p])) {
332 /* no start-of-line found */
335 if (m[j].lo <= m[j].al+1 &&
336 (m[j].type == Changed)) {
337 /* this can only work if the end is a line break */
338 if (is_cutpoint(m[j+1], af,bf,cf))
343 if (m[j].lo < m[j].al+1)
345 m[j].in_conflict = m[i].in_conflict;
347 if (m[j-1].in_conflict == 1)
350 /* A hunk header bordered the conflict */
353 /* If any of the merges are Changed or Conflict,
354 * then this really is a Conflict or Wiggle.
355 * If not they are just Unchanged, Unmatched,
356 * Extraneous or AlreadyApplied, and so don't
358 * Note that the first/last merges (in_conflict==1)
359 * can be Changed and so much be check separately.
361 if (m[j].type == Changed)
363 for (j = i-1; j >= 0 && m[j].in_conflict > 1; j--)
364 if (m[j].type == Changed || m[j].type == Conflict)
366 if (j >= 0 && m[j].type == Changed)
368 /* False alarm, no real conflict/wiggle here as
369 * nothing changed. */
372 if (m[j].in_conflict == 1) {
375 m[j].in_conflict = 0;
379 m[j++].in_conflict = 0;
381 if (m[i].type == End)
384 for (k = 1; k < m[i].al; k++)
385 if (words || ends_line(af.list[m[i].a+k])) {
396 /* Now count the conflicts and wiggles */
397 for (i = 0; m[i].type != End; i++) {
398 int true_conflict = 0;
399 if (!m[i].in_conflict)
402 for (j = i; m[j].type != End && m[j].in_conflict; j++) {
403 if (m[j].in_conflict == 2)
406 m[j].in_conflict == 1) {
408 if (!m[j+1].in_conflict)
424 struct ci make_merger(struct file af, struct file bf, struct file cf,
425 struct csl *csl1, struct csl *csl2, int words,
426 int ignore_already, int show_wiggles)
428 /* find the wiggles and conflicts between csl1 and csl2
433 int header_checked = -1;
434 int header_found = 0;
436 rv.conflicts = rv.wiggles = rv.ignored = 0;
438 for (i = 0; csl1[i].len; i++)
441 for (i = 0; csl2[i].len; i++)
444 /* maybe a bit of slack at each end */
447 rv.merger = xmalloc(sizeof(struct merge)*l);
449 a = b = c = c1 = c2 = 0;
453 match1 = (a >= csl1[c1].a && b >= csl1[c1].b); /* c1 doesn't match */
454 match2 = (b >= csl2[c2].a && c >= csl2[c2].b);
456 if (header_checked != c2) {
457 /* Check if there is a hunk header in this range */
460 for (j = b; j < csl2[c2].a + csl2[c2].len; j++)
461 if (bf.list[j].start[0] == '\0') {
470 rv.merger[i].c1 = c1;
471 rv.merger[i].c2 = c2;
472 rv.merger[i].in_conflict = 0;
474 if (!match1 && match2) {
475 /* This is either Unmatched or Extraneous - probably both.
476 * If the match2 has a hunk-header Extraneous, it must
477 * align with an end-of-line in 'a', so adjust endpoint
479 int newa = csl1[c1].a;
480 if (header_found >= 0) {
482 !ends_line(af.list[newa-1]))
485 if (a == newa && b == csl1[c1].b)
488 /* some unmatched text */
489 rv.merger[i].type = Unmatched;
490 rv.merger[i].al = newa - a;
495 assert(b < csl1[c1].b);
496 /* some Extraneous text */
497 /* length is min of unmatched on left
498 * and matched on right.
499 * However a hunk-header must be an
500 * Extraneous section by itself, so if this
501 * start with one, the length is 1, and if
502 * there is one in the middle, only take the
503 * text up to there for now.
505 rv.merger[i].type = Extraneous;
509 csl2[c2].len - (b-csl2[c2].a));
510 if (header_found == b) {
513 } else if (header_found > b && header_found < newb) {
519 rv.merger[i].bl = newb - b;
521 } else if (match1 && !match2) {
523 * if 'c' is currently at a suitable cut-point, then
524 * we can look for a triple-cut-point for start.
525 * Also, if csl2[c2].b isn't in a conflict, and is
526 * a suitable cut-point, then we could make a
527 * triple-cut-point for end of a conflict.
530 rv.merger[i].type = Changed;
531 rv.merger[i].bl = min(csl1[c1].b+csl1[c1].len, csl2[c2].a) - b;
532 rv.merger[i].al = rv.merger[i].bl;
533 rv.merger[i].cl = csl2[c2].b - c;
534 } else if (match1 && match2) {
535 /* Some unchanged text
537 rv.merger[i].type = Unchanged;
539 min(csl1[c1].len - (b-csl1[c1].b),
540 csl2[c2].len - (b-csl2[c2].a));
541 rv.merger[i].al = rv.merger[i].cl =
544 /* must be a conflict.
545 * Move a and c to next match, and b to closest of the two
547 rv.merger[i].type = Conflict;
548 rv.merger[i].al = csl1[c1].a - a;
549 rv.merger[i].cl = csl2[c2].b - c;
550 rv.merger[i].bl = min(csl1[c1].b, csl2[c2].a) - b;
551 if (ignore_already &&
552 check_alreadyapplied(af, cf, &rv.merger[i]))
554 else if (rv.merger[i].bl == 0 &&
556 /* As the 'before' stream is empty, this
557 * could look like Unmatched in the
558 * original, and an insertion in the
559 * diff. Reporting it like that is
560 * probably more useful that as a full
562 * Leave the type for the insertion as
563 * Conflict (not Changed) as there is some
564 * real uncertainty here, but allow the
565 * original to become Unmatched.
569 rv.merger[i].oldtype = rv.merger[i].type;
570 a += rv.merger[i].al;
571 b += rv.merger[i].bl;
572 c += rv.merger[i].cl;
575 while (csl1[c1].a + csl1[c1].len <= a && csl1[c1].len)
577 assert(csl1[c1].b + csl1[c1].len >= b);
578 while (csl2[c2].b + csl2[c2].len <= c && csl2[c2].len)
580 assert(csl2[c2].a + csl2[c2].len >= b);
581 if (csl1[c1].len == 0 && csl2[c2].len == 0 &&
582 a == csl1[c1].a && b == csl1[c1].b &&
583 b == csl2[c2].a && c == csl2[c2].b)
586 rv.merger[i].type = End;
587 rv.merger[i].oldtype = End;
591 rv.merger[i].c1 = c1;
592 rv.merger[i].c2 = c2;
593 rv.merger[i].in_conflict = 0;
596 /* Now revert any AlreadyApplied that aren't bounded by
597 * Unchanged or Changed.
599 for (i = 0; rv.merger[i].type != End; i++) {
600 if (rv.merger[i].type != AlreadyApplied)
602 if (i > 0 && rv.merger[i-1].type != Unchanged &&
603 rv.merger[i-1].type != Changed)
604 rv.merger[i].type = Conflict;
605 if (rv.merger[i+1].type != Unchanged &&
606 rv.merger[i+1].type != Changed &&
607 rv.merger[i+1].type != End)
608 rv.merger[i].type = Conflict;
610 rv.conflicts = isolate_conflicts(af, bf, cf, csl1, csl2, words,
611 rv.merger, show_wiggles, &rv.wiggles);
615 static int printrange(FILE *out, struct file *f, int start, int len,
620 struct elmnt e = f->list[start];
622 if (e.start[e.plen-1] == '\n' &&
632 int print_merge(FILE *out, struct file *a, struct file *b, struct file *c,
633 int words, struct merge *merger,
634 struct merge *mpos, int streampos, int offsetpos)
639 int offset = INT_MAX;
641 for (m = merger; m->type != End ; m++) {
644 printf("[%s: %d-%d,%d-%d,%d-%d%s(%d,%d)]\n",
645 m->type==Unmatched ? "Unmatched" :
646 m->type==Unchanged ? "Unchanged" :
647 m->type==Extraneous ? "Extraneous" :
648 m->type==Changed ? "Changed" :
649 m->type==AlreadyApplied ? "AlreadyApplied" :
650 m->type==Conflict ? "Conflict":"unknown",
654 m->in_conflict ? " in_conflict" : "",
657 while (m->in_conflict) {
658 /* need to print from 'hi' to 'lo' of next
659 * Unchanged which is < it's hi
661 int found_conflict = 0;
663 if (m->in_conflict == 1)
669 if (m->in_conflict == 1 && m->type == Unchanged)
670 lineno += printrange(out, a, m->a+m->lo, m->hi - m->lo, offset - m->lo);
675 for (cm = m; cm->in_conflict; cm++) {
676 printf("{%s: %d-%d,%d-%d,%d-%d%s(%d,%d)}\n",
677 cm->type==Unmatched?"Unmatched":
678 cm->type==Unchanged?"Unchanged":
679 cm->type==Extraneous?"Extraneous":
680 cm->type==Changed?"Changed":
681 cm->type==AlreadyApplied?"AlreadyApplied":
682 cm->type==Conflict?"Conflict":"unknown",
683 cm->a, cm->a+cm->al-1,
684 cm->b, cm->b+cm->bl-1,
685 cm->c, cm->c+cm->cl-1,
686 cm->in_conflict ? " in_conflict" : "",
688 if (cm->in_conflict == 1 && cm != m)
692 if (m->in_conflict == 1 &&
693 m[1].in_conflict == 1) {
694 /* Nothing between two conflicts */
699 fputs(words ? "<<<---" : "<<<<<<< found\n", out);
702 for (cm = m; cm->in_conflict; cm++) {
703 if (cm == mpos && streampos == 0)
705 if (cm->type == Conflict)
707 if (cm->in_conflict == 1 && cm != m) {
708 lineno += printrange(out, a, cm->a, cm->lo, offset);
711 lineno += printrange(out, a, cm->a+st1, cm->al-st1, offset-st1);
713 if (cm == mpos && streampos == 0)
716 if (cm == mpos && streampos == 0)
718 fputs(words ? "|||" : "||||||| expected\n", out);
722 for (cm = m; cm->in_conflict; cm++) {
723 if (cm == mpos && streampos == 1)
725 if (cm->in_conflict == 1 && cm != m) {
726 lineno += printrange(out, a, cm->a, cm->lo, offset);
729 lineno += printrange(out, b, cm->b+st1, cm->bl-st1, offset-st1);
731 if (cm == mpos && streampos == 1)
734 if (cm == mpos && streampos == 1)
736 fputs(words ? "===" : "=======\n", out);
740 for (cm = m; cm->in_conflict; cm++) {
741 if (cm == mpos && streampos == 2)
743 if (cm->in_conflict == 1 && cm != m) {
744 if (cm->type == Unchanged)
745 lineno += printrange(out, a, cm->a, cm->lo, offset);
747 lineno += printrange(out, c, cm->c, cm->cl, offset);
750 if (cm->type == Changed)
751 st1 = 0; /* All of result of change must be printed */
752 lineno += printrange(out, c, cm->c+st1, cm->cl-st1, offset-st1);
754 if (cm == mpos && streampos == 2)
757 if (cm == mpos && streampos == 2)
759 if (!found_conflict) {
760 /* This section was wiggled in successfully,
761 * but full conflict display was requested.
762 * So now print out the wiggled result as well.
764 fputs(words ? "&&&" : "&&&&&&& resolution\n", out);
768 for (cm = m; cm->in_conflict; cm++) {
770 if (cm->in_conflict == 1 && cm != m)
776 lineno += printrange(out, a, cm->a+st1,
777 last ? cm->lo : cm->al-st1, offset-st1);
782 lineno += printrange(out, c, cm->c,
783 last ? cm->lo : cm->cl, offset);
794 fputs(words ? "--->>>" : ">>>>>>> replacement\n", out);
798 if (m->in_conflict == 1 && m[1].in_conflict == 0) {
799 /* End of a conflict, no conflict follows */
802 if (m->type == Unchanged)
803 lineno += printrange(out, a, m->a+m->lo, m->hi-m->lo, offset-m->lo);
810 /* there is always some non-conflict after a conflict,
811 * unless we hit the end
817 printf("<<%s: %d-%d,%d-%d,%d-%d%s(%d,%d)>>\n",
818 m->type==Unmatched?"Unmatched":
819 m->type==Unchanged?"Unchanged":
820 m->type==Extraneous?"Extraneous":
821 m->type==Changed?"Changed":
822 m->type==AlreadyApplied?"AlreadyApplied":
823 m->type==Conflict?"Conflict":"unknown",
827 m->in_conflict ? " in_conflict" : "",
837 lineno += printrange(out, a, m->a, m->al, offset);
842 lineno += printrange(out, c, m->c, m->cl, offset);
854 int save_merge(struct file a, struct file b, struct file c,
855 struct merge *merger, char *file, int backup)
857 char *replacename = xmalloc(strlen(file) + 20);
858 char *orignew = xmalloc(strlen(file) + 20);
863 strcpy(replacename, file);
864 strcat(replacename, "XXXXXX");
865 strcpy(orignew, file);
866 strcat(orignew, ".porig");
868 fd = mkstemp(replacename);
873 outfile = fdopen(fd, "w");
874 lineno = print_merge(outfile, &a, &b, &c, 0, merger,
877 if (backup && rename(file, orignew) != 0)
879 else if (rename(replacename, file) != 0)
885 return err < 0 ? err : lineno;
888 int save_tmp_merge(struct file a, struct file b, struct file c,
889 struct merge *merger, char **filep,
890 struct merge *mpos, int streampos, int offsetpos)
897 dir = getenv("TMPDIR");
901 asprintf(&fname, "%s/wiggle-tmp-XXXXXX", dir);
908 outfile = fdopen(fd, "w");
909 lineno = print_merge(outfile, &a, &b, &c, 0, merger,
910 mpos, streampos, offsetpos);