Blob


1 /* Produce a unidiff output from a diff_result. */
2 /*
3 * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <diff/diff_output.h>
20 #include "debug.h"
22 enum chunk_type {
23 CHUNK_EMPTY,
24 CHUNK_PLUS,
25 CHUNK_MINUS,
26 CHUNK_SAME,
27 CHUNK_WEIRD,
28 };
30 static inline enum chunk_type
31 chunk_type(const struct diff_chunk *chunk)
32 {
33 if (!chunk->left_count && !chunk->right_count)
34 return CHUNK_EMPTY;
35 if (!chunk->solved)
36 return CHUNK_WEIRD;
37 if (!chunk->right_count)
38 return CHUNK_MINUS;
39 if (!chunk->left_count)
40 return CHUNK_PLUS;
41 if (chunk->left_count != chunk->right_count)
42 return CHUNK_WEIRD;
43 return CHUNK_SAME;
44 }
46 struct chunk_context {
47 struct range chunk;
48 struct range left, right;
49 };
51 static bool
52 chunk_context_empty(const struct chunk_context *cc)
53 {
54 return range_empty(&cc->chunk);
55 }
57 static void
58 chunk_context_get(struct chunk_context *cc, const struct diff_result *r, int chunk_idx,
59 int context_lines)
60 {
61 const struct diff_chunk *c = &r->chunks.head[chunk_idx];
62 int left_start = diff_atom_root_idx(&r->left, c->left_start);
63 int right_start = diff_atom_root_idx(&r->right, c->right_start);
65 *cc = (struct chunk_context){
66 .chunk = {
67 .start = chunk_idx,
68 .end = chunk_idx + 1,
69 },
70 .left = {
71 .start = MAX(0, left_start - context_lines),
72 .end = MIN(r->left.atoms.len, left_start + c->left_count + context_lines),
73 },
74 .right = {
75 .start = MAX(0, right_start - context_lines),
76 .end = MIN(r->right.atoms.len, right_start + c->right_count + context_lines),
77 },
78 };
79 }
81 static bool
82 chunk_contexts_touch(const struct chunk_context *cc, const struct chunk_context *other)
83 {
84 return ranges_touch(&cc->chunk, &other->chunk)
85 || ranges_touch(&cc->left, &other->left)
86 || ranges_touch(&cc->right, &other->right);
87 }
89 static void
90 chunk_contexts_merge(struct chunk_context *cc, const struct chunk_context *other)
91 {
92 ranges_merge(&cc->chunk, &other->chunk);
93 ranges_merge(&cc->left, &other->left);
94 ranges_merge(&cc->right, &other->right);
95 }
97 static void
98 diff_output_unidiff_chunk(FILE *dest, bool *header_printed, const struct diff_input_info *info,
99 const struct diff_result *result, const struct chunk_context *cc)
101 if (range_empty(&cc->left) && range_empty(&cc->right))
102 return;
104 if (!(*header_printed)) {
105 fprintf(dest, "--- %s\n+++ %s\n",
106 info->left_path ? : "a",
107 info->right_path ? : "b");
108 *header_printed = true;
111 fprintf(dest, "@@ -%d,%d +%d,%d @@\n",
112 cc->left.start + 1, cc->left.end - cc->left.start,
113 cc->right.start + 1, cc->right.end - cc->right.start);
115 /* Got the absolute line numbers where to start printing, and the index of the interesting (non-context) chunk.
116 * To print context lines above the interesting chunk, nipping on the previous chunk index may be necessary.
117 * It is guaranteed to be only context lines where left == right, so it suffices to look on the left. */
118 const struct diff_chunk *first_chunk = &result->chunks.head[cc->chunk.start];
119 int chunk_start_line = diff_atom_root_idx(&result->left, first_chunk->left_start);
120 if (cc->left.start < chunk_start_line)
121 diff_output_lines(dest, " ", &result->left.atoms.head[cc->left.start],
122 chunk_start_line - cc->left.start);
124 /* Now write out all the joined chunks and contexts between them */
125 int c_idx;
126 for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
127 const struct diff_chunk *c = &result->chunks.head[c_idx];
129 if (c->left_count && c->right_count)
130 diff_output_lines(dest, c->solved ? " " : "?", c->left_start, c->left_count);
131 else if (c->left_count && !c->right_count)
132 diff_output_lines(dest, c->solved ? "-" : "?", c->left_start, c->left_count);
133 else if (c->right_count && !c->left_count)
134 diff_output_lines(dest, c->solved ? "+" : "?", c->right_start, c->right_count);
137 /* Trailing context? */
138 const struct diff_chunk *last_chunk = &result->chunks.head[cc->chunk.end - 1];
139 int chunk_end_line = diff_atom_root_idx(&result->left, last_chunk->left_start + last_chunk->left_count);
140 if (cc->left.end > chunk_end_line)
141 diff_output_lines(dest, " ", &result->left.atoms.head[chunk_end_line],
142 cc->left.end - chunk_end_line);
145 enum diff_rc
146 diff_output_unidiff(FILE *dest, const struct diff_input_info *info,
147 const struct diff_result *result, unsigned int context_lines)
149 if (!result)
150 return DIFF_RC_EINVAL;
151 if (result->rc != DIFF_RC_OK)
152 return result->rc;
154 struct chunk_context cc = {};
155 bool header_printed = false;
157 int i;
158 for (i = 0; i < result->chunks.len; i++) {
159 struct diff_chunk *c = &result->chunks.head[i];
160 enum chunk_type t = chunk_type(c);
161 struct chunk_context next;
163 if (t != CHUNK_MINUS && t != CHUNK_PLUS)
164 continue;
166 if (chunk_context_empty(&cc)) {
167 /* These are the first lines being printed.
168 * Note down the start point, any number of subsequent chunks may be joined up to this
169 * unidiff chunk by context lines or by being directly adjacent. */
170 chunk_context_get(&cc, result, i, context_lines);
171 debug("new chunk to be printed: chunk %d-%d left %d-%d right %d-%d\n",
172 cc.chunk.start, cc.chunk.end,
173 cc.left.start, cc.left.end,
174 cc.right.start, cc.right.end);
175 continue;
178 /* There already is a previous chunk noted down for being printed.
179 * Does it join up with this one? */
180 chunk_context_get(&next, result, i, context_lines);
181 debug("new chunk to be printed: chunk %d-%d left %d-%d right %d-%d\n",
182 next.chunk.start, next.chunk.end,
183 next.left.start, next.left.end,
184 next.right.start, next.right.end);
186 if (chunk_contexts_touch(&cc, &next)) {
187 /* This next context touches or overlaps the previous one, join. */
188 chunk_contexts_merge(&cc, &next);
189 debug("new chunk to be printed touches previous chunk, now: left %d-%d right %d-%d\n",
190 cc.left.start, cc.left.end,
191 cc.right.start, cc.right.end);
192 continue;
195 /* No touching, so the previous context is complete with a gap between it and
196 * this next one. Print the previous one and start fresh here. */
197 debug("new chunk to be printed does not touch previous chunk; print left %d-%d right %d-%d\n",
198 cc.left.start, cc.left.end, cc.right.start, cc.right.end);
199 diff_output_unidiff_chunk(dest, &header_printed, info, result, &cc);
200 cc = next;
201 debug("new unprinted chunk is left %d-%d right %d-%d\n",
202 cc.left.start, cc.left.end, cc.right.start, cc.right.end);
205 if (!chunk_context_empty(&cc))
206 diff_output_unidiff_chunk(dest, &header_printed, info, result, &cc);
207 return DIFF_RC_OK;