1 /* Produce a unidiff output from a diff_result. */
3 * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
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.
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.
24 #include <arraylist.h>
25 #include <diff_main.h>
26 #include <diff_output.h>
28 #include "diff_internal.h"
29 #include "diff_debug.h"
32 chunk_context_empty(const struct diff_chunk_context *cc)
34 return diff_range_empty(&cc->chunk);
38 diff_chunk_get_left_start(const struct diff_chunk *c,
39 const struct diff_result *r, int context_lines)
41 int left_start = diff_atom_root_idx(&r->left, c->left_start);
42 return MAX(0, left_start - context_lines);
46 diff_chunk_get_left_end(const struct diff_chunk *c,
47 const struct diff_result *r, int context_lines)
49 int left_start = diff_chunk_get_left_start(c, r, 0);
50 return MIN(r->left.atoms.len,
51 left_start + c->left_count + context_lines);
55 diff_chunk_get_right_start(const struct diff_chunk *c,
56 const struct diff_result *r, int context_lines)
58 int right_start = diff_atom_root_idx(&r->right, c->right_start);
59 return MAX(0, right_start - context_lines);
63 diff_chunk_get_right_end(const struct diff_chunk *c,
64 const struct diff_result *r, int context_lines)
66 int right_start = diff_chunk_get_right_start(c, r, 0);
67 return MIN(r->right.atoms.len,
68 right_start + c->right_count + context_lines);
72 diff_chunk_context_get(struct diff_chunk_context *cc, const struct diff_result *r,
73 int chunk_idx, int context_lines)
75 const struct diff_chunk *c = &r->chunks.head[chunk_idx];
76 int left_start = diff_chunk_get_left_start(c, r, context_lines);
77 int left_end = diff_chunk_get_left_end(c, r, context_lines);
78 int right_start = diff_chunk_get_right_start(c, r, context_lines);
79 int right_end = diff_chunk_get_right_end(c, r, context_lines);
81 *cc = (struct diff_chunk_context){
98 chunk_contexts_touch(const struct diff_chunk_context *cc,
99 const struct diff_chunk_context *other)
101 return diff_ranges_touch(&cc->chunk, &other->chunk)
102 || diff_ranges_touch(&cc->left, &other->left)
103 || diff_ranges_touch(&cc->right, &other->right);
107 chunk_contexts_merge(struct diff_chunk_context *cc,
108 const struct diff_chunk_context *other)
110 diff_ranges_merge(&cc->chunk, &other->chunk);
111 diff_ranges_merge(&cc->left, &other->left);
112 diff_ranges_merge(&cc->right, &other->right);
115 struct diff_output_unidiff_state {
119 struct diff_output_unidiff_state *
120 diff_output_unidiff_state_alloc(void)
122 struct diff_output_unidiff_state *state;
124 state = calloc(1, sizeof(struct diff_output_unidiff_state));
126 diff_output_unidiff_state_reset(state);
131 diff_output_unidiff_state_reset(struct diff_output_unidiff_state *state)
133 state->header_printed = false;
137 diff_output_unidiff_state_free(struct diff_output_unidiff_state *state)
143 output_unidiff_chunk(struct diff_output_info *outinfo, FILE *dest,
144 struct diff_output_unidiff_state *state,
145 const struct diff_input_info *info,
146 const struct diff_result *result,
147 const struct diff_chunk_context *cc)
150 off_t outoff = 0, *offp;
152 if (diff_range_empty(&cc->left) && diff_range_empty(&cc->right))
155 if (outinfo && outinfo->line_offsets.len > 0) {
156 unsigned int idx = outinfo->line_offsets.len - 1;
157 outoff = outinfo->line_offsets.head[idx];
160 if (!(state->header_printed)) {
161 rc = fprintf(dest, "--- %s\n", info->left_path ? : "a");
165 ARRAYLIST_ADD(offp, outinfo->line_offsets);
172 rc = fprintf(dest, "+++ %s\n", info->right_path ? : "b");
176 ARRAYLIST_ADD(offp, outinfo->line_offsets);
183 state->header_printed = true;
186 rc = fprintf(dest, "@@ -%d,%d +%d,%d @@\n",
187 cc->left.start + 1, cc->left.end - cc->left.start,
188 cc->right.start + 1, cc->right.end - cc->right.start);
192 ARRAYLIST_ADD(offp, outinfo->line_offsets);
200 /* Got the absolute line numbers where to start printing, and the index
201 * of the interesting (non-context) chunk.
202 * To print context lines above the interesting chunk, nipping on the
203 * previous chunk index may be necessary.
204 * It is guaranteed to be only context lines where left == right, so it
205 * suffices to look on the left. */
206 const struct diff_chunk *first_chunk;
207 int chunk_start_line;
208 first_chunk = &result->chunks.head[cc->chunk.start];
209 chunk_start_line = diff_atom_root_idx(&result->left,
210 first_chunk->left_start);
211 if (cc->left.start < chunk_start_line) {
212 rc = diff_output_lines(outinfo, dest, " ",
213 &result->left.atoms.head[cc->left.start],
214 chunk_start_line - cc->left.start);
219 /* Now write out all the joined chunks and contexts between them */
221 for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
222 const struct diff_chunk *c = &result->chunks.head[c_idx];
224 if (c->left_count && c->right_count)
225 rc = diff_output_lines(outinfo, dest,
226 c->solved ? " " : "?",
227 c->left_start, c->left_count);
228 else if (c->left_count && !c->right_count)
229 rc = diff_output_lines(outinfo, dest,
230 c->solved ? "-" : "?",
231 c->left_start, c->left_count);
232 else if (c->right_count && !c->left_count)
233 rc = diff_output_lines(outinfo, dest,
234 c->solved ? "+" : "?",
235 c->right_start, c->right_count);
240 /* Trailing context? */
241 const struct diff_chunk *last_chunk;
243 last_chunk = &result->chunks.head[cc->chunk.end - 1];
244 chunk_end_line = diff_atom_root_idx(&result->left,
245 last_chunk->left_start
246 + last_chunk->left_count);
247 if (cc->left.end > chunk_end_line) {
248 rc = diff_output_lines(outinfo, dest, " ",
249 &result->left.atoms.head[chunk_end_line],
250 cc->left.end - chunk_end_line);
259 diff_output_unidiff_chunk(struct diff_output_info **output_info, FILE *dest,
260 struct diff_output_unidiff_state *state,
261 const struct diff_input_info *info,
262 const struct diff_result *result,
263 const struct diff_chunk_context *cc)
265 struct diff_output_info *outinfo = NULL;
268 *output_info = diff_output_info_alloc();
269 if (*output_info == NULL)
271 outinfo = *output_info;
274 return output_unidiff_chunk(outinfo, dest, state, info,
279 diff_output_unidiff(struct diff_output_info **output_info,
280 FILE *dest, const struct diff_input_info *info,
281 const struct diff_result *result,
282 unsigned int context_lines)
284 struct diff_output_unidiff_state *state;
285 struct diff_chunk_context cc = {};
286 struct diff_output_info *outinfo = NULL;
291 if (result->rc != DIFF_RC_OK)
295 *output_info = diff_output_info_alloc();
296 if (*output_info == NULL)
298 outinfo = *output_info;
301 state = diff_output_unidiff_state_alloc();
304 diff_output_info_free(*output_info);
311 for (i = 0; i < result->chunks.len; i++) {
312 struct diff_chunk *c = &result->chunks.head[i];
313 enum diff_chunk_type t = diff_chunk_type(c);
314 struct diff_chunk_context next;
316 if (t != CHUNK_MINUS && t != CHUNK_PLUS)
319 if (chunk_context_empty(&cc)) {
320 /* These are the first lines being printed.
321 * Note down the start point, any number of subsequent
322 * chunks may be joined up to this unidiff chunk by
323 * context lines or by being directly adjacent. */
324 diff_chunk_context_get(&cc, result, i, context_lines);
325 debug("new chunk to be printed:"
326 " chunk %d-%d left %d-%d right %d-%d\n",
327 cc.chunk.start, cc.chunk.end,
328 cc.left.start, cc.left.end,
329 cc.right.start, cc.right.end);
333 /* There already is a previous chunk noted down for being
334 * printed. Does it join up with this one? */
335 diff_chunk_context_get(&next, result, i, context_lines);
336 debug("new chunk to be printed:"
337 " chunk %d-%d left %d-%d right %d-%d\n",
338 next.chunk.start, next.chunk.end,
339 next.left.start, next.left.end,
340 next.right.start, next.right.end);
342 if (chunk_contexts_touch(&cc, &next)) {
343 /* This next context touches or overlaps the previous
345 chunk_contexts_merge(&cc, &next);
346 debug("new chunk to be printed touches previous chunk,"
347 " now: left %d-%d right %d-%d\n",
348 cc.left.start, cc.left.end,
349 cc.right.start, cc.right.end);
353 /* No touching, so the previous context is complete with a gap
354 * between it and this next one. Print the previous one and
355 * start fresh here. */
356 debug("new chunk to be printed does not touch previous chunk;"
357 " print left %d-%d right %d-%d\n",
358 cc.left.start, cc.left.end, cc.right.start, cc.right.end);
359 output_unidiff_chunk(outinfo, dest, state, info, result, &cc);
361 debug("new unprinted chunk is left %d-%d right %d-%d\n",
362 cc.left.start, cc.left.end, cc.right.start, cc.right.end);
365 if (!chunk_context_empty(&cc))
366 output_unidiff_chunk(outinfo, dest, state, info, result, &cc);
367 diff_output_unidiff_state_free(state);