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 <errno.h>
19 #include <inttypes.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
24 #include <arraylist.h>
25 #include <diff_main.h>
26 #include <diff_output.h>
28 #include "diff_internal.h"
29 #include "diff_debug.h"
31 bool
32 diff_chunk_context_empty(const struct diff_chunk_context *cc)
33 {
34 return diff_range_empty(&cc->chunk);
35 }
37 int
38 diff_chunk_get_left_start(const struct diff_chunk *c,
39 const struct diff_result *r, int context_lines)
40 {
41 int left_start = diff_atom_root_idx(&r->left, c->left_start);
42 return MAX(0, left_start - context_lines);
43 }
45 int
46 diff_chunk_get_left_end(const struct diff_chunk *c,
47 const struct diff_result *r, int context_lines)
48 {
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);
52 }
54 int
55 diff_chunk_get_right_start(const struct diff_chunk *c,
56 const struct diff_result *r, int context_lines)
57 {
58 int right_start = diff_atom_root_idx(&r->right, c->right_start);
59 return MAX(0, right_start - context_lines);
60 }
62 int
63 diff_chunk_get_right_end(const struct diff_chunk *c,
64 const struct diff_result *r, int context_lines)
65 {
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);
69 }
71 void
72 diff_chunk_context_get(struct diff_chunk_context *cc, const struct diff_result *r,
73 int chunk_idx, int context_lines)
74 {
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){
82 .chunk = {
83 .start = chunk_idx,
84 .end = chunk_idx + 1,
85 },
86 .left = {
87 .start = left_start,
88 .end = left_end,
89 },
90 .right = {
91 .start = right_start,
92 .end = right_end,
93 },
94 };
95 }
97 bool
98 diff_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);
106 void
107 diff_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 void
116 diff_chunk_context_load_change(struct diff_chunk_context *cc,
117 int *nchunks_used,
118 struct diff_result *result,
119 int start_chunk_idx,
120 int context_lines)
122 int i;
123 int seen_minus = 0, seen_plus = 0;
125 if (nchunks_used)
126 *nchunks_used = 0;
128 for (i = start_chunk_idx; i < result->chunks.len; i++) {
129 struct diff_chunk *chunk = &result->chunks.head[i];
130 enum diff_chunk_type t = diff_chunk_type(chunk);
131 struct diff_chunk_context next;
133 if (t != CHUNK_MINUS && t != CHUNK_PLUS) {
134 if (nchunks_used)
135 (*nchunks_used)++;
136 if (seen_minus && seen_plus)
137 break;
138 else
139 continue;
140 } else if (t == CHUNK_MINUS)
141 seen_minus = 1;
142 else if (t == CHUNK_PLUS)
143 seen_plus = 1;
145 if (diff_chunk_context_empty(cc)) {
146 /* Note down the start point, any number of subsequent
147 * chunks may be joined up to this chunk by being
148 * directly adjacent. */
149 diff_chunk_context_get(cc, result, i, context_lines);
150 if (nchunks_used)
151 (*nchunks_used)++;
152 continue;
155 /* There already is a previous chunk noted down for being
156 * printed. Does it join up with this one? */
157 diff_chunk_context_get(&next, result, i, context_lines);
159 if (diff_chunk_contexts_touch(cc, &next)) {
160 /* This next context touches or overlaps the previous
161 * one, join. */
162 diff_chunk_contexts_merge(cc, &next);
163 if (nchunks_used)
164 (*nchunks_used)++;
165 continue;
166 } else
167 break;
171 struct diff_output_unidiff_state {
172 bool header_printed;
173 };
175 struct diff_output_unidiff_state *
176 diff_output_unidiff_state_alloc(void)
178 struct diff_output_unidiff_state *state;
180 state = calloc(1, sizeof(struct diff_output_unidiff_state));
181 if (state != NULL)
182 diff_output_unidiff_state_reset(state);
183 return state;
186 void
187 diff_output_unidiff_state_reset(struct diff_output_unidiff_state *state)
189 state->header_printed = false;
192 void
193 diff_output_unidiff_state_free(struct diff_output_unidiff_state *state)
195 free(state);
198 static int
199 output_unidiff_chunk(struct diff_output_info *outinfo, FILE *dest,
200 struct diff_output_unidiff_state *state,
201 const struct diff_input_info *info,
202 const struct diff_result *result,
203 bool print_header,
204 const struct diff_chunk_context *cc)
206 int rc, left_start, left_len, right_start, right_len;
207 off_t outoff = 0, *offp;
209 if (diff_range_empty(&cc->left) && diff_range_empty(&cc->right))
210 return DIFF_RC_OK;
212 if (outinfo && outinfo->line_offsets.len > 0) {
213 unsigned int idx = outinfo->line_offsets.len - 1;
214 outoff = outinfo->line_offsets.head[idx];
217 if (print_header && !(state->header_printed)) {
218 rc = fprintf(dest, "--- %s\n", info->left_path ? : "a");
219 if (rc < 0)
220 return errno;
221 if (outinfo) {
222 ARRAYLIST_ADD(offp, outinfo->line_offsets);
223 if (offp == NULL)
224 return ENOMEM;
225 outoff += rc;
226 *offp = outoff;
229 rc = fprintf(dest, "+++ %s\n", info->right_path ? : "b");
230 if (rc < 0)
231 return errno;
232 if (outinfo) {
233 ARRAYLIST_ADD(offp, outinfo->line_offsets);
234 if (offp == NULL)
235 return ENOMEM;
236 outoff += rc;
237 *offp = outoff;
240 state->header_printed = true;
243 left_len = cc->left.end - cc->left.start;
244 if (result->left.atoms.len == 0)
245 left_start = 0;
246 else if (left_len == 0 && cc->left.start > 0)
247 left_start = cc->left.start;
248 else
249 left_start = cc->left.start + 1;
251 right_len = cc->right.end - cc->right.start;
252 if (result->right.atoms.len == 0)
253 right_start = 0;
254 else if (right_len == 0 && cc->right.start > 0)
255 right_start = cc->right.start;
256 else
257 right_start = cc->right.start + 1;
259 if (left_len == 1 && right_len == 1) {
260 rc = fprintf(dest, "@@ -%d +%d @@\n",
261 left_start, right_start);
262 } else if (left_len == 1 && right_len != 1) {
263 rc = fprintf(dest, "@@ -%d +%d,%d @@\n",
264 left_start, right_start, right_len);
265 } else if (left_len != 1 && right_len == 1) {
266 rc = fprintf(dest, "@@ -%d,%d +%d @@\n",
267 left_start, left_len, right_start);
268 } else {
269 rc = fprintf(dest, "@@ -%d,%d +%d,%d @@\n",
270 left_start, left_len, right_start, right_len);
272 if (rc < 0)
273 return errno;
274 if (outinfo) {
275 ARRAYLIST_ADD(offp, outinfo->line_offsets);
276 if (offp == NULL)
277 return ENOMEM;
278 outoff += rc;
279 *offp = outoff;
283 /* Got the absolute line numbers where to start printing, and the index
284 * of the interesting (non-context) chunk.
285 * To print context lines above the interesting chunk, nipping on the
286 * previous chunk index may be necessary.
287 * It is guaranteed to be only context lines where left == right, so it
288 * suffices to look on the left. */
289 const struct diff_chunk *first_chunk;
290 int chunk_start_line;
291 first_chunk = &result->chunks.head[cc->chunk.start];
292 chunk_start_line = diff_atom_root_idx(&result->left,
293 first_chunk->left_start);
294 if (cc->left.start < chunk_start_line) {
295 rc = diff_output_lines(outinfo, dest, " ",
296 &result->left.atoms.head[cc->left.start],
297 chunk_start_line - cc->left.start);
298 if (rc)
299 return rc;
302 /* Now write out all the joined chunks and contexts between them */
303 int c_idx;
304 for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
305 const struct diff_chunk *c = &result->chunks.head[c_idx];
307 if (c->left_count && c->right_count)
308 rc = diff_output_lines(outinfo, dest,
309 c->solved ? " " : "?",
310 c->left_start, c->left_count);
311 else if (c->left_count && !c->right_count)
312 rc = diff_output_lines(outinfo, dest,
313 c->solved ? "-" : "?",
314 c->left_start, c->left_count);
315 else if (c->right_count && !c->left_count)
316 rc = diff_output_lines(outinfo, dest,
317 c->solved ? "+" : "?",
318 c->right_start, c->right_count);
319 if (rc)
320 return rc;
322 if (cc->chunk.end == result->chunks.len) {
323 rc = diff_output_trailing_newline_msg(outinfo, dest, c);
324 if (rc != DIFF_RC_OK)
325 return rc;
329 /* Trailing context? */
330 const struct diff_chunk *last_chunk;
331 int chunk_end_line;
332 last_chunk = &result->chunks.head[cc->chunk.end - 1];
333 chunk_end_line = diff_atom_root_idx(&result->left,
334 last_chunk->left_start
335 + last_chunk->left_count);
336 if (cc->left.end > chunk_end_line) {
337 rc = diff_output_lines(outinfo, dest, " ",
338 &result->left.atoms.head[chunk_end_line],
339 cc->left.end - chunk_end_line);
340 if (rc)
341 return rc;
344 return DIFF_RC_OK;
347 int
348 diff_output_unidiff_chunk(struct diff_output_info **output_info, FILE *dest,
349 struct diff_output_unidiff_state *state,
350 const struct diff_input_info *info,
351 const struct diff_result *result,
352 const struct diff_chunk_context *cc)
354 struct diff_output_info *outinfo = NULL;
356 if (output_info) {
357 *output_info = diff_output_info_alloc();
358 if (*output_info == NULL)
359 return ENOMEM;
360 outinfo = *output_info;
363 return output_unidiff_chunk(outinfo, dest, state, info,
364 result, false, cc);
367 int
368 diff_output_unidiff(struct diff_output_info **output_info,
369 FILE *dest, const struct diff_input_info *info,
370 const struct diff_result *result,
371 unsigned int context_lines)
373 struct diff_output_unidiff_state *state;
374 struct diff_chunk_context cc = {};
375 struct diff_output_info *outinfo = NULL;
376 int i;
378 if (!result)
379 return EINVAL;
380 if (result->rc != DIFF_RC_OK)
381 return result->rc;
383 if (output_info) {
384 *output_info = diff_output_info_alloc();
385 if (*output_info == NULL)
386 return ENOMEM;
387 outinfo = *output_info;
390 state = diff_output_unidiff_state_alloc();
391 if (state == NULL) {
392 if (output_info) {
393 diff_output_info_free(*output_info);
394 *output_info = NULL;
396 return ENOMEM;
399 #if DEBUG
400 for (i = 0; i < result->chunks.len; i++) {
401 struct diff_chunk *c = &result->chunks.head[i];
402 enum diff_chunk_type t = diff_chunk_type(c);
404 debug("[%d] %s lines L%d R%d @L %d @R %d\n",
405 i, (t == CHUNK_MINUS ? "minus" :
406 (t == CHUNK_PLUS ? "plus" :
407 (t == CHUNK_SAME ? "same" : "?"))),
408 c->left_count,
409 c->right_count,
410 c->left_start ? diff_atom_root_idx(&result->left, c->left_start) : -1,
411 c->right_start ? diff_atom_root_idx(&result->right, c->right_start) : -1);
413 #endif
415 for (i = 0; i < result->chunks.len; i++) {
416 struct diff_chunk *c = &result->chunks.head[i];
417 enum diff_chunk_type t = diff_chunk_type(c);
418 struct diff_chunk_context next;
420 if (t != CHUNK_MINUS && t != CHUNK_PLUS)
421 continue;
423 if (diff_chunk_context_empty(&cc)) {
424 /* These are the first lines being printed.
425 * Note down the start point, any number of subsequent
426 * chunks may be joined up to this unidiff chunk by
427 * context lines or by being directly adjacent. */
428 diff_chunk_context_get(&cc, result, i, context_lines);
429 debug("new chunk to be printed:"
430 " chunk %d-%d left %d-%d right %d-%d\n",
431 cc.chunk.start, cc.chunk.end,
432 cc.left.start, cc.left.end,
433 cc.right.start, cc.right.end);
434 continue;
437 /* There already is a previous chunk noted down for being
438 * printed. Does it join up with this one? */
439 diff_chunk_context_get(&next, result, i, context_lines);
440 debug("new chunk to be printed:"
441 " chunk %d-%d left %d-%d right %d-%d\n",
442 next.chunk.start, next.chunk.end,
443 next.left.start, next.left.end,
444 next.right.start, next.right.end);
446 if (diff_chunk_contexts_touch(&cc, &next)) {
447 /* This next context touches or overlaps the previous
448 * one, join. */
449 diff_chunk_contexts_merge(&cc, &next);
450 debug("new chunk to be printed touches previous chunk,"
451 " now: left %d-%d right %d-%d\n",
452 cc.left.start, cc.left.end,
453 cc.right.start, cc.right.end);
454 continue;
457 /* No touching, so the previous context is complete with a gap
458 * between it and this next one. Print the previous one and
459 * start fresh here. */
460 debug("new chunk to be printed does not touch previous chunk;"
461 " print left %d-%d right %d-%d\n",
462 cc.left.start, cc.left.end, cc.right.start, cc.right.end);
463 output_unidiff_chunk(outinfo, dest, state, info, result,
464 true, &cc);
465 cc = next;
466 debug("new unprinted chunk is left %d-%d right %d-%d\n",
467 cc.left.start, cc.left.end, cc.right.start, cc.right.end);
470 if (!diff_chunk_context_empty(&cc))
471 output_unidiff_chunk(outinfo, dest, state, info, result,
472 true, &cc);
473 diff_output_unidiff_state_free(state);
474 return DIFF_RC_OK;