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 <diff/arraylist.h>
25 #include <diff/diff_main.h>
26 #include <diff/diff_output.h>
28 #include "diff_debug.h"
29 #include "diff_internal.h"
32 chunk_context_empty(const struct diff_chunk_context *cc)
34 return diff_range_empty(&cc->chunk);
38 diff_chunk_context_get(struct diff_chunk_context *cc, const struct diff_result *r,
39 int chunk_idx, int context_lines)
41 const struct diff_chunk *c = &r->chunks.head[chunk_idx];
42 int left_start = diff_atom_root_idx(&r->left, c->left_start);
43 int left_end = MIN(r->left.atoms.len,
44 left_start + c->left_count + context_lines);
45 int right_start = diff_atom_root_idx(&r->right, c->right_start);
46 int right_end = MIN(r->right.atoms.len,
47 right_start + c->right_count + context_lines);
49 left_start = MAX(0, left_start - context_lines);
50 right_start = MAX(0, right_start - context_lines);
52 *cc = (struct diff_chunk_context){
69 chunk_contexts_touch(const struct diff_chunk_context *cc,
70 const struct diff_chunk_context *other)
72 return diff_ranges_touch(&cc->chunk, &other->chunk)
73 || diff_ranges_touch(&cc->left, &other->left)
74 || diff_ranges_touch(&cc->right, &other->right);
78 chunk_contexts_merge(struct diff_chunk_context *cc,
79 const struct diff_chunk_context *other)
81 diff_ranges_merge(&cc->chunk, &other->chunk);
82 diff_ranges_merge(&cc->left, &other->left);
83 diff_ranges_merge(&cc->right, &other->right);
86 struct diff_output_unidiff_state {
90 struct diff_output_unidiff_state *
91 diff_output_unidiff_state_alloc(void)
93 struct diff_output_unidiff_state *state;
95 state = calloc(1, sizeof(struct diff_output_unidiff_state));
97 diff_output_unidiff_state_reset(state);
102 diff_output_unidiff_state_reset(struct diff_output_unidiff_state *state)
104 state->header_printed = false;
108 diff_output_unidiff_state_free(struct diff_output_unidiff_state *state)
114 output_unidiff_chunk(struct diff_output_info *outinfo, FILE *dest,
115 struct diff_output_unidiff_state *state,
116 const struct diff_input_info *info,
117 const struct diff_result *result,
118 const struct diff_chunk_context *cc)
121 off_t outoff = 0, outlen = 0, *offp;
123 if (diff_range_empty(&cc->left) && diff_range_empty(&cc->right))
126 if (outinfo && outinfo->line_offsets.len > 0) {
127 unsigned int idx = outinfo->line_offsets.len - 1;
128 outoff = outinfo->line_offsets.head[idx];
131 if (!(state->header_printed)) {
132 rc = fprintf(dest, "--- %s\n", info->left_path ? : "a");
136 ARRAYLIST_ADD(offp, outinfo->line_offsets);
141 rc = fprintf(dest, "+++ %s\n", info->right_path ? : "b");
145 ARRAYLIST_ADD(offp, outinfo->line_offsets);
150 state->header_printed = true;
153 rc = fprintf(dest, "@@ -%d,%d +%d,%d @@\n",
154 cc->left.start + 1, cc->left.end - cc->left.start,
155 cc->right.start + 1, cc->right.end - cc->right.start);
159 ARRAYLIST_ADD(offp, outinfo->line_offsets);
165 /* Got the absolute line numbers where to start printing, and the index
166 * of the interesting (non-context) chunk.
167 * To print context lines above the interesting chunk, nipping on the
168 * previous chunk index may be necessary.
169 * It is guaranteed to be only context lines where left == right, so it
170 * suffices to look on the left. */
171 const struct diff_chunk *first_chunk;
172 int chunk_start_line;
173 first_chunk = &result->chunks.head[cc->chunk.start];
174 chunk_start_line = diff_atom_root_idx(&result->left,
175 first_chunk->left_start);
176 if (cc->left.start < chunk_start_line) {
177 rc = diff_output_lines(outinfo, dest, " ",
178 &result->left.atoms.head[cc->left.start],
179 chunk_start_line - cc->left.start);
184 /* Now write out all the joined chunks and contexts between them */
186 for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
187 const struct diff_chunk *c = &result->chunks.head[c_idx];
189 if (c->left_count && c->right_count)
190 rc = diff_output_lines(outinfo, dest,
191 c->solved ? " " : "?",
192 c->left_start, c->left_count);
193 else if (c->left_count && !c->right_count)
194 rc = diff_output_lines(outinfo, dest,
195 c->solved ? "-" : "?",
196 c->left_start, c->left_count);
197 else if (c->right_count && !c->left_count)
198 rc = diff_output_lines(outinfo, dest,
199 c->solved ? "+" : "?",
200 c->right_start, c->right_count);
205 /* Trailing context? */
206 const struct diff_chunk *last_chunk;
208 last_chunk = &result->chunks.head[cc->chunk.end - 1];
209 chunk_end_line = diff_atom_root_idx(&result->left,
210 last_chunk->left_start
211 + last_chunk->left_count);
212 if (cc->left.end > chunk_end_line) {
213 rc = diff_output_lines(outinfo, dest, " ",
214 &result->left.atoms.head[chunk_end_line],
215 cc->left.end - chunk_end_line);
224 diff_output_unidiff_chunk(struct diff_output_info **output_info, FILE *dest,
225 struct diff_output_unidiff_state *state,
226 const struct diff_input_info *info,
227 const struct diff_result *result,
228 const struct diff_chunk_context *cc)
230 struct diff_output_info *outinfo = NULL;
233 *output_info = diff_output_info_alloc();
234 if (*output_info == NULL)
236 outinfo = *output_info;
239 return output_unidiff_chunk(outinfo, dest, state, info,
244 diff_output_unidiff(struct diff_output_info **output_info,
245 FILE *dest, const struct diff_input_info *info,
246 const struct diff_result *result,
247 unsigned int context_lines)
249 struct diff_output_unidiff_state *state;
250 struct diff_chunk_context cc = {};
251 struct diff_output_info *outinfo = NULL;
256 if (result->rc != DIFF_RC_OK)
260 *output_info = diff_output_info_alloc();
261 if (*output_info == NULL)
263 outinfo = *output_info;
266 state = diff_output_unidiff_state_alloc();
269 diff_output_info_free(*output_info);
276 for (i = 0; i < result->chunks.len; i++) {
277 struct diff_chunk *c = &result->chunks.head[i];
278 enum diff_chunk_type t = diff_chunk_type(c);
279 struct diff_chunk_context next;
281 if (t != CHUNK_MINUS && t != CHUNK_PLUS)
284 if (chunk_context_empty(&cc)) {
285 /* These are the first lines being printed.
286 * Note down the start point, any number of subsequent
287 * chunks may be joined up to this unidiff chunk by
288 * context lines or by being directly adjacent. */
289 diff_chunk_context_get(&cc, result, i, context_lines);
290 debug("new chunk to be printed:"
291 " chunk %d-%d left %d-%d right %d-%d\n",
292 cc.chunk.start, cc.chunk.end,
293 cc.left.start, cc.left.end,
294 cc.right.start, cc.right.end);
298 /* There already is a previous chunk noted down for being
299 * printed. Does it join up with this one? */
300 diff_chunk_context_get(&next, result, i, context_lines);
301 debug("new chunk to be printed:"
302 " chunk %d-%d left %d-%d right %d-%d\n",
303 next.chunk.start, next.chunk.end,
304 next.left.start, next.left.end,
305 next.right.start, next.right.end);
307 if (chunk_contexts_touch(&cc, &next)) {
308 /* This next context touches or overlaps the previous
310 chunk_contexts_merge(&cc, &next);
311 debug("new chunk to be printed touches previous chunk,"
312 " now: left %d-%d right %d-%d\n",
313 cc.left.start, cc.left.end,
314 cc.right.start, cc.right.end);
318 /* No touching, so the previous context is complete with a gap
319 * between it and this next one. Print the previous one and
320 * start fresh here. */
321 debug("new chunk to be printed does not touch previous chunk;"
322 " print left %d-%d right %d-%d\n",
323 cc.left.start, cc.left.end, cc.right.start, cc.right.end);
324 output_unidiff_chunk(outinfo, dest, state, info, result, &cc);
326 debug("new unprinted chunk is left %d-%d right %d-%d\n",
327 cc.left.start, cc.left.end, cc.right.start, cc.right.end);
330 if (!chunk_context_empty(&cc))
331 output_unidiff_chunk(outinfo, dest, state, info, result, &cc);
332 diff_output_unidiff_state_free(state);