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 static bool
32 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 static bool
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);
106 static void
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 {
116 bool header_printed;
117 };
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));
125 if (state != NULL)
126 diff_output_unidiff_state_reset(state);
127 return state;
130 void
131 diff_output_unidiff_state_reset(struct diff_output_unidiff_state *state)
133 state->header_printed = false;
136 void
137 diff_output_unidiff_state_free(struct diff_output_unidiff_state *state)
139 free(state);
142 static int
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)
149 int rc, left_start, left_len, right_start, right_len;
150 off_t outoff = 0, *offp;
152 if (diff_range_empty(&cc->left) && diff_range_empty(&cc->right))
153 return DIFF_RC_OK;
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");
162 if (rc < 0)
163 return errno;
164 if (outinfo) {
165 ARRAYLIST_ADD(offp, outinfo->line_offsets);
166 if (offp == NULL)
167 return ENOMEM;
168 outoff += rc;
169 *offp = outoff;
172 rc = fprintf(dest, "+++ %s\n", info->right_path ? : "b");
173 if (rc < 0)
174 return errno;
175 if (outinfo) {
176 ARRAYLIST_ADD(offp, outinfo->line_offsets);
177 if (offp == NULL)
178 return ENOMEM;
179 outoff += rc;
180 *offp = outoff;
183 state->header_printed = true;
186 left_len = cc->left.end - cc->left.start;
187 if (result->left.atoms.len == 0)
188 left_start = 0;
189 else if (left_len == 0 && cc->left.start > 0)
190 left_start = cc->left.start;
191 else
192 left_start = cc->left.start + 1;
194 right_len = cc->right.end - cc->right.start;
195 if (result->right.atoms.len == 0)
196 right_start = 0;
197 else if (right_len == 0 && cc->right.start > 0)
198 right_start = cc->right.start;
199 else
200 right_start = cc->right.start + 1;
202 if (left_len == 1 && right_len == 1) {
203 rc = fprintf(dest, "@@ -%d +%d @@\n",
204 left_start, right_start);
205 } else if (left_len == 1 && right_len != 1) {
206 rc = fprintf(dest, "@@ -%d +%d,%d @@\n",
207 left_start, right_start, right_len);
208 } else if (left_len != 1 && right_len == 1) {
209 rc = fprintf(dest, "@@ -%d,%d +%d @@\n",
210 left_start, left_len, right_start);
211 } else {
212 rc = fprintf(dest, "@@ -%d,%d +%d,%d @@\n",
213 left_start, left_len, right_start, right_len);
215 if (rc < 0)
216 return errno;
217 if (outinfo) {
218 ARRAYLIST_ADD(offp, outinfo->line_offsets);
219 if (offp == NULL)
220 return ENOMEM;
221 outoff += rc;
222 *offp = outoff;
226 /* Got the absolute line numbers where to start printing, and the index
227 * of the interesting (non-context) chunk.
228 * To print context lines above the interesting chunk, nipping on the
229 * previous chunk index may be necessary.
230 * It is guaranteed to be only context lines where left == right, so it
231 * suffices to look on the left. */
232 const struct diff_chunk *first_chunk;
233 int chunk_start_line;
234 first_chunk = &result->chunks.head[cc->chunk.start];
235 chunk_start_line = diff_atom_root_idx(&result->left,
236 first_chunk->left_start);
237 if (cc->left.start < chunk_start_line) {
238 rc = diff_output_lines(outinfo, dest, " ",
239 &result->left.atoms.head[cc->left.start],
240 chunk_start_line - cc->left.start);
241 if (rc)
242 return rc;
245 /* Now write out all the joined chunks and contexts between them */
246 int c_idx;
247 for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
248 const struct diff_chunk *c = &result->chunks.head[c_idx];
250 if (c->left_count && c->right_count)
251 rc = diff_output_lines(outinfo, dest,
252 c->solved ? " " : "?",
253 c->left_start, c->left_count);
254 else if (c->left_count && !c->right_count)
255 rc = diff_output_lines(outinfo, dest,
256 c->solved ? "-" : "?",
257 c->left_start, c->left_count);
258 else if (c->right_count && !c->left_count)
259 rc = diff_output_lines(outinfo, dest,
260 c->solved ? "+" : "?",
261 c->right_start, c->right_count);
262 if (rc)
263 return rc;
266 /* Trailing context? */
267 const struct diff_chunk *last_chunk;
268 int chunk_end_line;
269 last_chunk = &result->chunks.head[cc->chunk.end - 1];
270 chunk_end_line = diff_atom_root_idx(&result->left,
271 last_chunk->left_start
272 + last_chunk->left_count);
273 if (cc->left.end > chunk_end_line) {
274 rc = diff_output_lines(outinfo, dest, " ",
275 &result->left.atoms.head[chunk_end_line],
276 cc->left.end - chunk_end_line);
277 if (rc)
278 return rc;
281 return DIFF_RC_OK;
284 int
285 diff_output_unidiff_chunk(struct diff_output_info **output_info, FILE *dest,
286 struct diff_output_unidiff_state *state,
287 const struct diff_input_info *info,
288 const struct diff_result *result,
289 const struct diff_chunk_context *cc)
291 struct diff_output_info *outinfo = NULL;
293 if (output_info) {
294 *output_info = diff_output_info_alloc();
295 if (*output_info == NULL)
296 return ENOMEM;
297 outinfo = *output_info;
300 return output_unidiff_chunk(outinfo, dest, state, info,
301 result, cc);
304 int
305 diff_output_unidiff(struct diff_output_info **output_info,
306 FILE *dest, const struct diff_input_info *info,
307 const struct diff_result *result,
308 unsigned int context_lines)
310 struct diff_output_unidiff_state *state;
311 struct diff_chunk_context cc = {};
312 struct diff_output_info *outinfo = NULL;
313 int i;
315 if (!result)
316 return EINVAL;
317 if (result->rc != DIFF_RC_OK)
318 return result->rc;
320 if (output_info) {
321 *output_info = diff_output_info_alloc();
322 if (*output_info == NULL)
323 return ENOMEM;
324 outinfo = *output_info;
327 state = diff_output_unidiff_state_alloc();
328 if (state == NULL) {
329 if (output_info) {
330 diff_output_info_free(*output_info);
331 *output_info = NULL;
333 return ENOMEM;
336 #if DEBUG
337 for (i = 0; i < result->chunks.len; i++) {
338 struct diff_chunk *c = &result->chunks.head[i];
339 enum diff_chunk_type t = diff_chunk_type(c);
341 debug("[%d] %s lines L%d R%d @L %d @R %d\n",
342 i, (t == CHUNK_MINUS ? "minus" :
343 (t == CHUNK_PLUS ? "plus" :
344 (t == CHUNK_SAME ? "same" : "?"))),
345 c->left_count,
346 c->right_count,
347 c->left_start ? diff_atom_root_idx(&result->left, c->left_start) : -1,
348 c->right_start ? diff_atom_root_idx(&result->right, c->right_start) : -1);
350 #endif
352 for (i = 0; i < result->chunks.len; i++) {
353 struct diff_chunk *c = &result->chunks.head[i];
354 enum diff_chunk_type t = diff_chunk_type(c);
355 struct diff_chunk_context next;
357 if (t != CHUNK_MINUS && t != CHUNK_PLUS)
358 continue;
360 if (chunk_context_empty(&cc)) {
361 /* These are the first lines being printed.
362 * Note down the start point, any number of subsequent
363 * chunks may be joined up to this unidiff chunk by
364 * context lines or by being directly adjacent. */
365 diff_chunk_context_get(&cc, result, i, context_lines);
366 debug("new chunk to be printed:"
367 " chunk %d-%d left %d-%d right %d-%d\n",
368 cc.chunk.start, cc.chunk.end,
369 cc.left.start, cc.left.end,
370 cc.right.start, cc.right.end);
371 continue;
374 /* There already is a previous chunk noted down for being
375 * printed. Does it join up with this one? */
376 diff_chunk_context_get(&next, result, i, context_lines);
377 debug("new chunk to be printed:"
378 " chunk %d-%d left %d-%d right %d-%d\n",
379 next.chunk.start, next.chunk.end,
380 next.left.start, next.left.end,
381 next.right.start, next.right.end);
383 if (chunk_contexts_touch(&cc, &next)) {
384 /* This next context touches or overlaps the previous
385 * one, join. */
386 chunk_contexts_merge(&cc, &next);
387 debug("new chunk to be printed touches previous chunk,"
388 " now: left %d-%d right %d-%d\n",
389 cc.left.start, cc.left.end,
390 cc.right.start, cc.right.end);
391 continue;
394 /* No touching, so the previous context is complete with a gap
395 * between it and this next one. Print the previous one and
396 * start fresh here. */
397 debug("new chunk to be printed does not touch previous chunk;"
398 " print left %d-%d right %d-%d\n",
399 cc.left.start, cc.left.end, cc.right.start, cc.right.end);
400 output_unidiff_chunk(outinfo, dest, state, info, result, &cc);
401 cc = next;
402 debug("new unprinted chunk is left %d-%d right %d-%d\n",
403 cc.left.start, cc.left.end, cc.right.start, cc.right.end);
406 if (!chunk_context_empty(&cc))
407 output_unidiff_chunk(outinfo, dest, state, info, result, &cc);
408 diff_output_unidiff_state_free(state);
409 return DIFF_RC_OK;