From faa36dd65bfccde8f61debeaa9a691b5185f5777 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Fri, 22 Mar 2013 15:04:48 +1100 Subject: [PATCH] csl_join should merge adjacent matches. When diffing against a patch we treat each hunk separately and join the result. This joining doesn't merge adjacent matches so it doesn't really tell the full story of where the matches are and how big they are. This can confuse other code, particularly when examining hunk headers to create the merger. So add detection for adjacent matches. Signed-off-by: NeilBrown --- diff.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/diff.c b/diff.c index 5eba75a..1c3d656 100644 --- a/diff.c +++ b/diff.c @@ -580,6 +580,7 @@ struct csl *diff_partial(struct file a, struct file b, struct csl *csl_join(struct csl *c1, struct csl *c2) { int cnt1, cnt2; + int offset = 0; if (c1 == NULL) return c2; if (c2 == NULL) @@ -589,9 +590,17 @@ struct csl *csl_join(struct csl *c1, struct csl *c2) ; for (cnt2 = 0; c2[cnt2].len; cnt2++) ; + if (cnt1 && cnt2 && + c1[cnt1-1].a + c1[cnt1-1].len == c2[0].a && + c1[cnt1-1].b + c1[cnt1-1].len == c2[0].b) { + /* Merge these two */ + c1[cnt1-1].len += c2[0].len; + offset = 1; + cnt2--; + } c1 = realloc(c1, (cnt1+cnt2+1)*sizeof(*c1)); while (cnt2 >= 0) { - c1[cnt1+cnt2] = c2[cnt2]; + c1[cnt1+cnt2] = c2[cnt2 + offset]; cnt2--; } free(c2); -- 2.39.5