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 <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"
31 static bool
32 chunk_context_empty(const struct diff_chunk_context *cc)
33 {
34 return diff_range_empty(&cc->chunk);
35 }
37 void
38 diff_chunk_context_get(struct diff_chunk_context *cc, const struct diff_result *r,
39 int chunk_idx, int context_lines)
40 {
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){
53 .chunk = {
54 .start = chunk_idx,
55 .end = chunk_idx + 1,
56 },
57 .left = {
58 .start = left_start,
59 .end = left_end,
60 },
61 .right = {
62 .start = right_start,
63 .end = right_end,
64 },
65 };
66 }
68 static bool
69 chunk_contexts_touch(const struct diff_chunk_context *cc,
70 const struct diff_chunk_context *other)
71 {
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);
75 }
77 static void
78 chunk_contexts_merge(struct diff_chunk_context *cc,
79 const struct diff_chunk_context *other)
80 {
81 diff_ranges_merge(&cc->chunk, &other->chunk);
82 diff_ranges_merge(&cc->left, &other->left);
83 diff_ranges_merge(&cc->right, &other->right);
84 }
86 struct diff_output_unidiff_state {
87 bool header_printed;
88 };
90 struct diff_output_unidiff_state *
91 diff_output_unidiff_state_alloc(void)
92 {
93 struct diff_output_unidiff_state *state;
95 state = calloc(1, sizeof(struct diff_output_unidiff_state));
96 if (state != NULL)
97 diff_output_unidiff_state_reset(state);
98 return state;
99 }
101 void
102 diff_output_unidiff_state_reset(struct diff_output_unidiff_state *state)
104 state->header_printed = false;
107 void
108 diff_output_unidiff_state_free(struct diff_output_unidiff_state *state)
110 free(state);
113 static int
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)
120 int rc;
121 off_t outoff = 0, outlen = 0, *offp;
123 if (diff_range_empty(&cc->left) && diff_range_empty(&cc->right))
124 return DIFF_RC_OK;
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");
133 if (rc < 0)
134 return errno;
135 if (outinfo) {
136 ARRAYLIST_ADD(offp, outinfo->line_offsets);
137 outoff += rc;
138 *offp = outoff;
141 rc = fprintf(dest, "+++ %s\n", info->right_path ? : "b");
142 if (rc < 0)
143 return errno;
144 if (outinfo) {
145 ARRAYLIST_ADD(offp, outinfo->line_offsets);
146 outoff += rc;
147 *offp = outoff;
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);
156 if (rc < 0)
157 return errno;
158 if (outinfo) {
159 ARRAYLIST_ADD(offp, outinfo->line_offsets);
160 outoff += rc;
161 *offp = outoff;
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);
180 if (rc)
181 return rc;
184 /* Now write out all the joined chunks and contexts between them */
185 int c_idx;
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);
201 if (rc)
202 return rc;
205 /* Trailing context? */
206 const struct diff_chunk *last_chunk;
207 int chunk_end_line;
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);
216 if (rc)
217 return rc;
220 return DIFF_RC_OK;
223 int
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;
232 if (output_info) {
233 *output_info = diff_output_info_alloc();
234 if (*output_info == NULL)
235 return ENOMEM;
236 outinfo = *output_info;
239 return output_unidiff_chunk(outinfo, dest, state, info,
240 result, cc);
243 int
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;
252 int i;
254 if (!result)
255 return EINVAL;
256 if (result->rc != DIFF_RC_OK)
257 return result->rc;
259 if (output_info) {
260 *output_info = diff_output_info_alloc();
261 if (*output_info == NULL)
262 return ENOMEM;
263 outinfo = *output_info;
266 state = diff_output_unidiff_state_alloc();
267 if (state == NULL) {
268 if (output_info) {
269 diff_output_info_free(*output_info);
270 *output_info = NULL;
272 return ENOMEM;
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)
282 continue;
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);
295 continue;
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
309 * one, join. */
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);
315 continue;
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);
325 cc = next;
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);
333 return DIFF_RC_OK;