1 3b0f3d61 2020-01-22 neels /* Produce a unidiff output from a diff_result. */
3 3b0f3d61 2020-01-22 neels * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
5 3b0f3d61 2020-01-22 neels * Permission to use, copy, modify, and distribute this software for any
6 3b0f3d61 2020-01-22 neels * purpose with or without fee is hereby granted, provided that the above
7 3b0f3d61 2020-01-22 neels * copyright notice and this permission notice appear in all copies.
9 3b0f3d61 2020-01-22 neels * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 3b0f3d61 2020-01-22 neels * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 3b0f3d61 2020-01-22 neels * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 3b0f3d61 2020-01-22 neels * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 3b0f3d61 2020-01-22 neels * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 3b0f3d61 2020-01-22 neels * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 3b0f3d61 2020-01-22 neels * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 e10a628a 2020-09-16 stsp #include <errno.h>
19 e10a628a 2020-09-16 stsp #include <stdbool.h>
20 fe6d58fb 2020-11-14 naddy #include <stdint.h>
21 e10a628a 2020-09-16 stsp #include <stdio.h>
22 e10a628a 2020-09-16 stsp #include <stdlib.h>
23 b3fd1fa2 2020-12-10 stsp #include <string.h>
24 4861c9da 2020-10-30 neels #include <assert.h>
26 1dfba055 2020-10-07 stsp #include <arraylist.h>
27 1dfba055 2020-10-07 stsp #include <diff_main.h>
28 1dfba055 2020-10-07 stsp #include <diff_output.h>
30 85ab4559 2020-09-22 stsp #include "diff_internal.h"
31 2a1b94d0 2020-09-26 stsp #include "diff_debug.h"
34 f26db7cd 2023-02-20 mark diff_chunk_get_left_start_pos(const struct diff_chunk *c)
36 f26db7cd 2023-02-20 mark return c->left_start->pos;
40 f26db7cd 2023-02-20 mark diff_chunk_get_right_start_pos(const struct diff_chunk *c)
42 f26db7cd 2023-02-20 mark return c->right_start->pos;
46 cbf93b70 2020-10-16 stsp diff_chunk_context_empty(const struct diff_chunk_context *cc)
48 d362ea2e 2020-07-25 stsp return diff_range_empty(&cc->chunk);
52 fe8af0d6 2020-10-06 stsp diff_chunk_get_left_start(const struct diff_chunk *c,
53 fe8af0d6 2020-10-06 stsp const struct diff_result *r, int context_lines)
55 c16dde50 2020-10-22 stsp int left_start = diff_atom_root_idx(r->left, c->left_start);
56 fe8af0d6 2020-10-06 stsp return MAX(0, left_start - context_lines);
60 fe8af0d6 2020-10-06 stsp diff_chunk_get_left_end(const struct diff_chunk *c,
61 fe8af0d6 2020-10-06 stsp const struct diff_result *r, int context_lines)
63 fe8af0d6 2020-10-06 stsp int left_start = diff_chunk_get_left_start(c, r, 0);
64 c16dde50 2020-10-22 stsp return MIN(r->left->atoms.len,
65 fe8af0d6 2020-10-06 stsp left_start + c->left_count + context_lines);
69 fe8af0d6 2020-10-06 stsp diff_chunk_get_right_start(const struct diff_chunk *c,
70 fe8af0d6 2020-10-06 stsp const struct diff_result *r, int context_lines)
72 c16dde50 2020-10-22 stsp int right_start = diff_atom_root_idx(r->right, c->right_start);
73 fe8af0d6 2020-10-06 stsp return MAX(0, right_start - context_lines);
77 fe8af0d6 2020-10-06 stsp diff_chunk_get_right_end(const struct diff_chunk *c,
78 fe8af0d6 2020-10-06 stsp const struct diff_result *r, int context_lines)
80 fe8af0d6 2020-10-06 stsp int right_start = diff_chunk_get_right_start(c, r, 0);
81 c16dde50 2020-10-22 stsp return MIN(r->right->atoms.len,
82 fe8af0d6 2020-10-06 stsp right_start + c->right_count + context_lines);
85 e8eedebc 2020-11-06 stsp struct diff_chunk *
86 e8eedebc 2020-11-06 stsp diff_chunk_get(const struct diff_result *r, int chunk_idx)
88 e8eedebc 2020-11-06 stsp return &r->chunks.head[chunk_idx];
92 e8eedebc 2020-11-06 stsp diff_chunk_get_left_count(struct diff_chunk *c)
94 e8eedebc 2020-11-06 stsp return c->left_count;
98 e8eedebc 2020-11-06 stsp diff_chunk_get_right_count(struct diff_chunk *c)
100 e8eedebc 2020-11-06 stsp return c->right_count;
104 f374e913 2020-09-22 stsp diff_chunk_context_get(struct diff_chunk_context *cc, const struct diff_result *r,
105 0d27172a 2020-05-06 neels int chunk_idx, int context_lines)
107 3b0f3d61 2020-01-22 neels const struct diff_chunk *c = &r->chunks.head[chunk_idx];
108 fe8af0d6 2020-10-06 stsp int left_start = diff_chunk_get_left_start(c, r, context_lines);
109 fe8af0d6 2020-10-06 stsp int left_end = diff_chunk_get_left_end(c, r, context_lines);
110 fe8af0d6 2020-10-06 stsp int right_start = diff_chunk_get_right_start(c, r, context_lines);
111 fe8af0d6 2020-10-06 stsp int right_end = diff_chunk_get_right_end(c, r, context_lines);
113 f374e913 2020-09-22 stsp *cc = (struct diff_chunk_context){
114 3b0f3d61 2020-01-22 neels .chunk = {
115 3b0f3d61 2020-01-22 neels .start = chunk_idx,
116 3b0f3d61 2020-01-22 neels .end = chunk_idx + 1,
119 0d27172a 2020-05-06 neels .start = left_start,
120 0d27172a 2020-05-06 neels .end = left_end,
122 3b0f3d61 2020-01-22 neels .right = {
123 0d27172a 2020-05-06 neels .start = right_start,
124 0d27172a 2020-05-06 neels .end = right_end,
130 26595c7d 2020-10-15 stsp diff_chunk_contexts_touch(const struct diff_chunk_context *cc,
131 26595c7d 2020-10-15 stsp const struct diff_chunk_context *other)
133 d362ea2e 2020-07-25 stsp return diff_ranges_touch(&cc->chunk, &other->chunk)
134 d362ea2e 2020-07-25 stsp || diff_ranges_touch(&cc->left, &other->left)
135 d362ea2e 2020-07-25 stsp || diff_ranges_touch(&cc->right, &other->right);
139 26595c7d 2020-10-15 stsp diff_chunk_contexts_merge(struct diff_chunk_context *cc,
140 26595c7d 2020-10-15 stsp const struct diff_chunk_context *other)
142 d362ea2e 2020-07-25 stsp diff_ranges_merge(&cc->chunk, &other->chunk);
143 d362ea2e 2020-07-25 stsp diff_ranges_merge(&cc->left, &other->left);
144 d362ea2e 2020-07-25 stsp diff_ranges_merge(&cc->right, &other->right);
148 7187fe97 2020-10-17 stsp diff_chunk_context_load_change(struct diff_chunk_context *cc,
149 7187fe97 2020-10-17 stsp int *nchunks_used,
150 7187fe97 2020-10-17 stsp struct diff_result *result,
151 7187fe97 2020-10-17 stsp int start_chunk_idx,
152 7187fe97 2020-10-17 stsp int context_lines)
155 7187fe97 2020-10-17 stsp int seen_minus = 0, seen_plus = 0;
157 7187fe97 2020-10-17 stsp if (nchunks_used)
158 7187fe97 2020-10-17 stsp *nchunks_used = 0;
160 7187fe97 2020-10-17 stsp for (i = start_chunk_idx; i < result->chunks.len; i++) {
161 7187fe97 2020-10-17 stsp struct diff_chunk *chunk = &result->chunks.head[i];
162 7187fe97 2020-10-17 stsp enum diff_chunk_type t = diff_chunk_type(chunk);
163 7187fe97 2020-10-17 stsp struct diff_chunk_context next;
165 7187fe97 2020-10-17 stsp if (t != CHUNK_MINUS && t != CHUNK_PLUS) {
166 7187fe97 2020-10-17 stsp if (nchunks_used)
167 7187fe97 2020-10-17 stsp (*nchunks_used)++;
168 f4867452 2020-10-17 stsp if (seen_minus || seen_plus)
172 7187fe97 2020-10-17 stsp } else if (t == CHUNK_MINUS)
173 7187fe97 2020-10-17 stsp seen_minus = 1;
174 7187fe97 2020-10-17 stsp else if (t == CHUNK_PLUS)
175 7187fe97 2020-10-17 stsp seen_plus = 1;
177 7187fe97 2020-10-17 stsp if (diff_chunk_context_empty(cc)) {
178 7187fe97 2020-10-17 stsp /* Note down the start point, any number of subsequent
179 7187fe97 2020-10-17 stsp * chunks may be joined up to this chunk by being
180 7187fe97 2020-10-17 stsp * directly adjacent. */
181 7187fe97 2020-10-17 stsp diff_chunk_context_get(cc, result, i, context_lines);
182 7187fe97 2020-10-17 stsp if (nchunks_used)
183 7187fe97 2020-10-17 stsp (*nchunks_used)++;
187 7187fe97 2020-10-17 stsp /* There already is a previous chunk noted down for being
188 7187fe97 2020-10-17 stsp * printed. Does it join up with this one? */
189 7187fe97 2020-10-17 stsp diff_chunk_context_get(&next, result, i, context_lines);
191 7187fe97 2020-10-17 stsp if (diff_chunk_contexts_touch(cc, &next)) {
192 7187fe97 2020-10-17 stsp /* This next context touches or overlaps the previous
193 7187fe97 2020-10-17 stsp * one, join. */
194 7187fe97 2020-10-17 stsp diff_chunk_contexts_merge(cc, &next);
195 7187fe97 2020-10-17 stsp if (nchunks_used)
196 7187fe97 2020-10-17 stsp (*nchunks_used)++;
203 11caa5cc 2020-09-22 stsp struct diff_output_unidiff_state {
204 11caa5cc 2020-09-22 stsp bool header_printed;
205 b3fd1fa2 2020-12-10 stsp char prototype[DIFF_FUNCTION_CONTEXT_SIZE];
206 b3fd1fa2 2020-12-10 stsp int last_prototype_idx;
209 11caa5cc 2020-09-22 stsp struct diff_output_unidiff_state *
210 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_alloc(void)
212 11caa5cc 2020-09-22 stsp struct diff_output_unidiff_state *state;
214 11caa5cc 2020-09-22 stsp state = calloc(1, sizeof(struct diff_output_unidiff_state));
215 11caa5cc 2020-09-22 stsp if (state != NULL)
216 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_reset(state);
217 11caa5cc 2020-09-22 stsp return state;
221 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_reset(struct diff_output_unidiff_state *state)
223 11caa5cc 2020-09-22 stsp state->header_printed = false;
224 b3fd1fa2 2020-12-10 stsp memset(state->prototype, 0, sizeof(state->prototype));
225 b3fd1fa2 2020-12-10 stsp state->last_prototype_idx = 0;
229 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_free(struct diff_output_unidiff_state *state)
231 11caa5cc 2020-09-22 stsp free(state);
235 2c20a3ed 2020-09-22 stsp output_unidiff_chunk(struct diff_output_info *outinfo, FILE *dest,
236 2c20a3ed 2020-09-22 stsp struct diff_output_unidiff_state *state,
237 2c20a3ed 2020-09-22 stsp const struct diff_input_info *info,
238 2c20a3ed 2020-09-22 stsp const struct diff_result *result,
239 13e2caa3 2020-10-17 stsp bool print_header, bool show_function_prototypes,
240 dc306c6b 2023-08-29 stsp const struct diff_chunk_context *cc)
242 b6adedb6 2020-10-07 stsp int rc, left_start, left_len, right_start, right_len;
243 ab528e22 2020-09-22 stsp off_t outoff = 0, *offp;
244 9343b925 2022-08-04 mark uint8_t *typep;
246 d362ea2e 2020-07-25 stsp if (diff_range_empty(&cc->left) && diff_range_empty(&cc->right))
247 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
249 2c20a3ed 2020-09-22 stsp if (outinfo && outinfo->line_offsets.len > 0) {
250 2c20a3ed 2020-09-22 stsp unsigned int idx = outinfo->line_offsets.len - 1;
251 2c20a3ed 2020-09-22 stsp outoff = outinfo->line_offsets.head[idx];
254 66ea8e5a 2020-10-17 stsp if (print_header && !(state->header_printed)) {
255 e4c510c1 2020-11-21 stsp rc = fprintf(dest, "--- %s\n",
256 e4c510c1 2020-11-21 stsp diff_output_get_label_left(info));
257 2c20a3ed 2020-09-22 stsp if (rc < 0)
258 2c20a3ed 2020-09-22 stsp return errno;
259 2c20a3ed 2020-09-22 stsp if (outinfo) {
260 2c20a3ed 2020-09-22 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
261 dabc1008 2020-09-22 stsp if (offp == NULL)
262 dabc1008 2020-09-22 stsp return ENOMEM;
263 2c20a3ed 2020-09-22 stsp outoff += rc;
264 2c20a3ed 2020-09-22 stsp *offp = outoff;
265 9343b925 2022-08-04 mark ARRAYLIST_ADD(typep, outinfo->line_types);
266 9343b925 2022-08-04 mark if (typep == NULL)
267 9343b925 2022-08-04 mark return ENOMEM;
268 9343b925 2022-08-04 mark *typep = DIFF_LINE_MINUS;
270 e4c510c1 2020-11-21 stsp rc = fprintf(dest, "+++ %s\n",
271 e4c510c1 2020-11-21 stsp diff_output_get_label_right(info));
272 2c20a3ed 2020-09-22 stsp if (rc < 0)
273 2c20a3ed 2020-09-22 stsp return errno;
274 2c20a3ed 2020-09-22 stsp if (outinfo) {
275 2c20a3ed 2020-09-22 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
276 dabc1008 2020-09-22 stsp if (offp == NULL)
277 dabc1008 2020-09-22 stsp return ENOMEM;
278 2c20a3ed 2020-09-22 stsp outoff += rc;
279 2c20a3ed 2020-09-22 stsp *offp = outoff;
280 9343b925 2022-08-04 mark ARRAYLIST_ADD(typep, outinfo->line_types);
281 9343b925 2022-08-04 mark if (typep == NULL)
282 9343b925 2022-08-04 mark return ENOMEM;
283 9343b925 2022-08-04 mark *typep = DIFF_LINE_PLUS;
285 11caa5cc 2020-09-22 stsp state->header_printed = true;
288 b6adedb6 2020-10-07 stsp left_len = cc->left.end - cc->left.start;
289 c16dde50 2020-10-22 stsp if (result->left->atoms.len == 0)
290 11d9f2f7 2020-10-07 stsp left_start = 0;
291 11d9f2f7 2020-10-07 stsp else if (left_len == 0 && cc->left.start > 0)
292 b6adedb6 2020-10-07 stsp left_start = cc->left.start;
294 b6adedb6 2020-10-07 stsp left_start = cc->left.start + 1;
296 b6adedb6 2020-10-07 stsp right_len = cc->right.end - cc->right.start;
297 c16dde50 2020-10-22 stsp if (result->right->atoms.len == 0)
298 11d9f2f7 2020-10-07 stsp right_start = 0;
299 11d9f2f7 2020-10-07 stsp else if (right_len == 0 && cc->right.start > 0)
300 b6adedb6 2020-10-07 stsp right_start = cc->right.start;
302 b6adedb6 2020-10-07 stsp right_start = cc->right.start + 1;
304 13e2caa3 2020-10-17 stsp if (show_function_prototypes) {
305 b3fd1fa2 2020-12-10 stsp rc = diff_output_match_function_prototype(state->prototype,
306 b3fd1fa2 2020-12-10 stsp sizeof(state->prototype), &state->last_prototype_idx,
307 dc306c6b 2023-08-29 stsp result, cc);
312 d2dfa2ec 2020-10-07 stsp if (left_len == 1 && right_len == 1) {
313 13e2caa3 2020-10-17 stsp rc = fprintf(dest, "@@ -%d +%d @@%s%s\n",
314 13e2caa3 2020-10-17 stsp left_start, right_start,
315 b3fd1fa2 2020-12-10 stsp state->prototype[0] ? " " : "",
316 b3fd1fa2 2020-12-10 stsp state->prototype[0] ? state->prototype : "");
317 d2dfa2ec 2020-10-07 stsp } else if (left_len == 1 && right_len != 1) {
318 13e2caa3 2020-10-17 stsp rc = fprintf(dest, "@@ -%d +%d,%d @@%s%s\n",
319 13e2caa3 2020-10-17 stsp left_start, right_start, right_len,
320 b3fd1fa2 2020-12-10 stsp state->prototype[0] ? " " : "",
321 b3fd1fa2 2020-12-10 stsp state->prototype[0] ? state->prototype : "");
322 b6adedb6 2020-10-07 stsp } else if (left_len != 1 && right_len == 1) {
323 13e2caa3 2020-10-17 stsp rc = fprintf(dest, "@@ -%d,%d +%d @@%s%s\n",
324 13e2caa3 2020-10-17 stsp left_start, left_len, right_start,
325 b3fd1fa2 2020-12-10 stsp state->prototype[0] ? " " : "",
326 b3fd1fa2 2020-12-10 stsp state->prototype[0] ? state->prototype : "");
328 13e2caa3 2020-10-17 stsp rc = fprintf(dest, "@@ -%d,%d +%d,%d @@%s%s\n",
329 13e2caa3 2020-10-17 stsp left_start, left_len, right_start, right_len,
330 b3fd1fa2 2020-12-10 stsp state->prototype[0] ? " " : "",
331 b3fd1fa2 2020-12-10 stsp state->prototype[0] ? state->prototype : "");
333 2c20a3ed 2020-09-22 stsp if (rc < 0)
334 2c20a3ed 2020-09-22 stsp return errno;
335 2c20a3ed 2020-09-22 stsp if (outinfo) {
336 2c20a3ed 2020-09-22 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
337 dabc1008 2020-09-22 stsp if (offp == NULL)
338 dabc1008 2020-09-22 stsp return ENOMEM;
339 2c20a3ed 2020-09-22 stsp outoff += rc;
340 2c20a3ed 2020-09-22 stsp *offp = outoff;
341 9343b925 2022-08-04 mark ARRAYLIST_ADD(typep, outinfo->line_types);
342 9343b925 2022-08-04 mark if (typep == NULL)
343 9343b925 2022-08-04 mark return ENOMEM;
344 9343b925 2022-08-04 mark *typep = DIFF_LINE_HUNK;
347 0d27172a 2020-05-06 neels /* Got the absolute line numbers where to start printing, and the index
348 0d27172a 2020-05-06 neels * of the interesting (non-context) chunk.
349 0d27172a 2020-05-06 neels * To print context lines above the interesting chunk, nipping on the
350 0d27172a 2020-05-06 neels * previous chunk index may be necessary.
351 0d27172a 2020-05-06 neels * It is guaranteed to be only context lines where left == right, so it
352 0d27172a 2020-05-06 neels * suffices to look on the left. */
353 0d27172a 2020-05-06 neels const struct diff_chunk *first_chunk;
354 0d27172a 2020-05-06 neels int chunk_start_line;
355 0d27172a 2020-05-06 neels first_chunk = &result->chunks.head[cc->chunk.start];
356 c16dde50 2020-10-22 stsp chunk_start_line = diff_atom_root_idx(result->left,
357 0d27172a 2020-05-06 neels first_chunk->left_start);
358 2c20a3ed 2020-09-22 stsp if (cc->left.start < chunk_start_line) {
359 2c20a3ed 2020-09-22 stsp rc = diff_output_lines(outinfo, dest, " ",
360 c16dde50 2020-10-22 stsp &result->left->atoms.head[cc->left.start],
361 3b0f3d61 2020-01-22 neels chunk_start_line - cc->left.start);
366 3b0f3d61 2020-01-22 neels /* Now write out all the joined chunks and contexts between them */
367 3b0f3d61 2020-01-22 neels int c_idx;
368 3b0f3d61 2020-01-22 neels for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
369 3b0f3d61 2020-01-22 neels const struct diff_chunk *c = &result->chunks.head[c_idx];
371 3b0f3d61 2020-01-22 neels if (c->left_count && c->right_count)
372 2c20a3ed 2020-09-22 stsp rc = diff_output_lines(outinfo, dest,
373 0d27172a 2020-05-06 neels c->solved ? " " : "?",
374 0d27172a 2020-05-06 neels c->left_start, c->left_count);
375 3b0f3d61 2020-01-22 neels else if (c->left_count && !c->right_count)
376 2c20a3ed 2020-09-22 stsp rc = diff_output_lines(outinfo, dest,
377 0d27172a 2020-05-06 neels c->solved ? "-" : "?",
378 0d27172a 2020-05-06 neels c->left_start, c->left_count);
379 3b0f3d61 2020-01-22 neels else if (c->right_count && !c->left_count)
380 2c20a3ed 2020-09-22 stsp rc = diff_output_lines(outinfo, dest,
381 0d27172a 2020-05-06 neels c->solved ? "+" : "?",
382 0d27172a 2020-05-06 neels c->right_start, c->right_count);
386 7021523c 2020-10-16 stsp if (cc->chunk.end == result->chunks.len) {
387 7021523c 2020-10-16 stsp rc = diff_output_trailing_newline_msg(outinfo, dest, c);
388 7021523c 2020-10-16 stsp if (rc != DIFF_RC_OK)
393 3b0f3d61 2020-01-22 neels /* Trailing context? */
394 0d27172a 2020-05-06 neels const struct diff_chunk *last_chunk;
395 0d27172a 2020-05-06 neels int chunk_end_line;
396 0d27172a 2020-05-06 neels last_chunk = &result->chunks.head[cc->chunk.end - 1];
397 c16dde50 2020-10-22 stsp chunk_end_line = diff_atom_root_idx(result->left,
398 0d27172a 2020-05-06 neels last_chunk->left_start
399 0d27172a 2020-05-06 neels + last_chunk->left_count);
400 2c20a3ed 2020-09-22 stsp if (cc->left.end > chunk_end_line) {
401 2c20a3ed 2020-09-22 stsp rc = diff_output_lines(outinfo, dest, " ",
402 c16dde50 2020-10-22 stsp &result->left->atoms.head[chunk_end_line],
403 3b0f3d61 2020-01-22 neels cc->left.end - chunk_end_line);
407 4125b1ef 2022-10-10 tj rc = diff_output_trailing_newline_msg(outinfo, dest,
408 4125b1ef 2022-10-10 tj &result->chunks.head[result->chunks.len - 1]);
409 4125b1ef 2022-10-10 tj if (rc != DIFF_RC_OK)
413 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
417 2c20a3ed 2020-09-22 stsp diff_output_unidiff_chunk(struct diff_output_info **output_info, FILE *dest,
418 2c20a3ed 2020-09-22 stsp struct diff_output_unidiff_state *state,
419 2c20a3ed 2020-09-22 stsp const struct diff_input_info *info,
420 2c20a3ed 2020-09-22 stsp const struct diff_result *result,
421 2c20a3ed 2020-09-22 stsp const struct diff_chunk_context *cc)
423 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
424 c16dde50 2020-10-22 stsp int flags = (result->left->root->diff_flags |
425 c16dde50 2020-10-22 stsp result->right->root->diff_flags);
426 13e2caa3 2020-10-17 stsp bool show_function_prototypes = (flags & DIFF_FLAG_SHOW_PROTOTYPES);
428 2c20a3ed 2020-09-22 stsp if (output_info) {
429 2c20a3ed 2020-09-22 stsp *output_info = diff_output_info_alloc();
430 2c20a3ed 2020-09-22 stsp if (*output_info == NULL)
431 2c20a3ed 2020-09-22 stsp return ENOMEM;
432 2c20a3ed 2020-09-22 stsp outinfo = *output_info;
435 2c20a3ed 2020-09-22 stsp return output_unidiff_chunk(outinfo, dest, state, info,
436 dc306c6b 2023-08-29 stsp result, false, show_function_prototypes, cc);
440 2c20a3ed 2020-09-22 stsp diff_output_unidiff(struct diff_output_info **output_info,
441 2c20a3ed 2020-09-22 stsp FILE *dest, const struct diff_input_info *info,
442 0d27172a 2020-05-06 neels const struct diff_result *result,
443 0d27172a 2020-05-06 neels unsigned int context_lines)
445 11caa5cc 2020-09-22 stsp struct diff_output_unidiff_state *state;
446 11caa5cc 2020-09-22 stsp struct diff_chunk_context cc = {};
447 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
448 e51ebd83 2020-11-21 stsp int atomizer_flags = (result->left->atomizer_flags|
449 e51ebd83 2020-11-21 stsp result->right->atomizer_flags);
450 c16dde50 2020-10-22 stsp int flags = (result->left->root->diff_flags |
451 c16dde50 2020-10-22 stsp result->right->root->diff_flags);
452 13e2caa3 2020-10-17 stsp bool show_function_prototypes = (flags & DIFF_FLAG_SHOW_PROTOTYPES);
453 e51ebd83 2020-11-21 stsp bool force_text = (flags & DIFF_FLAG_FORCE_TEXT_DATA);
454 e51ebd83 2020-11-21 stsp bool have_binary = (atomizer_flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
455 b72f51ff 2022-01-06 stsp off_t outoff = 0, *offp;
456 9343b925 2022-08-04 mark uint8_t *typep;
459 3b0f3d61 2020-01-22 neels if (!result)
460 3e6cba3a 2020-08-13 stsp return EINVAL;
461 3b0f3d61 2020-01-22 neels if (result->rc != DIFF_RC_OK)
462 3b0f3d61 2020-01-22 neels return result->rc;
464 2c20a3ed 2020-09-22 stsp if (output_info) {
465 2c20a3ed 2020-09-22 stsp *output_info = diff_output_info_alloc();
466 2c20a3ed 2020-09-22 stsp if (*output_info == NULL)
467 2c20a3ed 2020-09-22 stsp return ENOMEM;
468 2c20a3ed 2020-09-22 stsp outinfo = *output_info;
471 e51ebd83 2020-11-21 stsp if (have_binary && !force_text) {
472 e51ebd83 2020-11-21 stsp for (i = 0; i < result->chunks.len; i++) {
473 e51ebd83 2020-11-21 stsp struct diff_chunk *c = &result->chunks.head[i];
474 e51ebd83 2020-11-21 stsp enum diff_chunk_type t = diff_chunk_type(c);
476 e51ebd83 2020-11-21 stsp if (t != CHUNK_MINUS && t != CHUNK_PLUS)
479 b72f51ff 2022-01-06 stsp if (outinfo && outinfo->line_offsets.len > 0) {
480 b72f51ff 2022-01-06 stsp unsigned int idx =
481 b72f51ff 2022-01-06 stsp outinfo->line_offsets.len - 1;
482 b72f51ff 2022-01-06 stsp outoff = outinfo->line_offsets.head[idx];
485 b72f51ff 2022-01-06 stsp rc = fprintf(dest, "Binary files %s and %s differ\n",
486 e4c510c1 2020-11-21 stsp diff_output_get_label_left(info),
487 e4c510c1 2020-11-21 stsp diff_output_get_label_right(info));
488 b72f51ff 2022-01-06 stsp if (outinfo) {
489 b72f51ff 2022-01-06 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
490 b72f51ff 2022-01-06 stsp if (offp == NULL)
491 b72f51ff 2022-01-06 stsp return ENOMEM;
492 b72f51ff 2022-01-06 stsp outoff += rc;
493 b72f51ff 2022-01-06 stsp *offp = outoff;
494 9343b925 2022-08-04 mark ARRAYLIST_ADD(typep, outinfo->line_types);
495 9343b925 2022-08-04 mark if (typep == NULL)
496 9343b925 2022-08-04 mark return ENOMEM;
497 9343b925 2022-08-04 mark *typep = DIFF_LINE_NONE;
502 e51ebd83 2020-11-21 stsp return DIFF_RC_OK;
505 11caa5cc 2020-09-22 stsp state = diff_output_unidiff_state_alloc();
506 2c20a3ed 2020-09-22 stsp if (state == NULL) {
507 2c20a3ed 2020-09-22 stsp if (output_info) {
508 2c20a3ed 2020-09-22 stsp diff_output_info_free(*output_info);
509 2c20a3ed 2020-09-22 stsp *output_info = NULL;
511 11caa5cc 2020-09-22 stsp return ENOMEM;
515 e638575c 2020-11-06 neels unsigned int check_left_pos, check_right_pos;
516 4861c9da 2020-10-30 neels check_left_pos = 0;
517 4861c9da 2020-10-30 neels check_right_pos = 0;
518 5ff75996 2020-10-11 neels for (i = 0; i < result->chunks.len; i++) {
519 5ff75996 2020-10-11 neels struct diff_chunk *c = &result->chunks.head[i];
520 5ff75996 2020-10-11 neels enum diff_chunk_type t = diff_chunk_type(c);
522 5ff75996 2020-10-11 neels debug("[%d] %s lines L%d R%d @L %d @R %d\n",
523 5ff75996 2020-10-11 neels i, (t == CHUNK_MINUS ? "minus" :
524 5ff75996 2020-10-11 neels (t == CHUNK_PLUS ? "plus" :
525 5ff75996 2020-10-11 neels (t == CHUNK_SAME ? "same" : "?"))),
526 5ff75996 2020-10-11 neels c->left_count,
527 5ff75996 2020-10-11 neels c->right_count,
528 9403a358 2020-10-24 neels c->left_start ? diff_atom_root_idx(result->left, c->left_start) : -1,
529 9403a358 2020-10-24 neels c->right_start ? diff_atom_root_idx(result->right, c->right_start) : -1);
530 4861c9da 2020-10-30 neels assert(check_left_pos == diff_atom_root_idx(result->left, c->left_start));
531 4861c9da 2020-10-30 neels assert(check_right_pos == diff_atom_root_idx(result->right, c->right_start));
532 4861c9da 2020-10-30 neels check_left_pos += c->left_count;
533 4861c9da 2020-10-30 neels check_right_pos += c->right_count;
536 4861c9da 2020-10-30 neels assert(check_left_pos == result->left->atoms.len);
537 4861c9da 2020-10-30 neels assert(check_right_pos == result->right->atoms.len);
540 3b0f3d61 2020-01-22 neels for (i = 0; i < result->chunks.len; i++) {
541 3b0f3d61 2020-01-22 neels struct diff_chunk *c = &result->chunks.head[i];
542 8546b045 2020-09-20 neels enum diff_chunk_type t = diff_chunk_type(c);
543 f374e913 2020-09-22 stsp struct diff_chunk_context next;
545 9cc49695 2020-05-05 neels if (t != CHUNK_MINUS && t != CHUNK_PLUS)
548 cbf93b70 2020-10-16 stsp if (diff_chunk_context_empty(&cc)) {
549 9cc49695 2020-05-05 neels /* These are the first lines being printed.
550 0d27172a 2020-05-06 neels * Note down the start point, any number of subsequent
551 0d27172a 2020-05-06 neels * chunks may be joined up to this unidiff chunk by
552 0d27172a 2020-05-06 neels * context lines or by being directly adjacent. */
553 f374e913 2020-09-22 stsp diff_chunk_context_get(&cc, result, i, context_lines);
554 0d27172a 2020-05-06 neels debug("new chunk to be printed:"
555 0d27172a 2020-05-06 neels " chunk %d-%d left %d-%d right %d-%d\n",
556 9cc49695 2020-05-05 neels cc.chunk.start, cc.chunk.end,
557 9cc49695 2020-05-05 neels cc.left.start, cc.left.end,
558 9cc49695 2020-05-05 neels cc.right.start, cc.right.end);
562 0d27172a 2020-05-06 neels /* There already is a previous chunk noted down for being
563 0d27172a 2020-05-06 neels * printed. Does it join up with this one? */
564 f374e913 2020-09-22 stsp diff_chunk_context_get(&next, result, i, context_lines);
565 0d27172a 2020-05-06 neels debug("new chunk to be printed:"
566 0d27172a 2020-05-06 neels " chunk %d-%d left %d-%d right %d-%d\n",
567 9cc49695 2020-05-05 neels next.chunk.start, next.chunk.end,
568 9cc49695 2020-05-05 neels next.left.start, next.left.end,
569 9cc49695 2020-05-05 neels next.right.start, next.right.end);
571 26595c7d 2020-10-15 stsp if (diff_chunk_contexts_touch(&cc, &next)) {
572 0d27172a 2020-05-06 neels /* This next context touches or overlaps the previous
573 0d27172a 2020-05-06 neels * one, join. */
574 26595c7d 2020-10-15 stsp diff_chunk_contexts_merge(&cc, &next);
575 0d27172a 2020-05-06 neels debug("new chunk to be printed touches previous chunk,"
576 0d27172a 2020-05-06 neels " now: left %d-%d right %d-%d\n",
577 9cc49695 2020-05-05 neels cc.left.start, cc.left.end,
578 9cc49695 2020-05-05 neels cc.right.start, cc.right.end);
582 0d27172a 2020-05-06 neels /* No touching, so the previous context is complete with a gap
583 0d27172a 2020-05-06 neels * between it and this next one. Print the previous one and
584 0d27172a 2020-05-06 neels * start fresh here. */
585 0d27172a 2020-05-06 neels debug("new chunk to be printed does not touch previous chunk;"
586 0d27172a 2020-05-06 neels " print left %d-%d right %d-%d\n",
587 9cc49695 2020-05-05 neels cc.left.start, cc.left.end, cc.right.start, cc.right.end);
588 66ea8e5a 2020-10-17 stsp output_unidiff_chunk(outinfo, dest, state, info, result,
589 dc306c6b 2023-08-29 stsp true, show_function_prototypes, &cc);
590 9cc49695 2020-05-05 neels cc = next;
591 9cc49695 2020-05-05 neels debug("new unprinted chunk is left %d-%d right %d-%d\n",
592 9cc49695 2020-05-05 neels cc.left.start, cc.left.end, cc.right.start, cc.right.end);
595 cbf93b70 2020-10-16 stsp if (!diff_chunk_context_empty(&cc))
596 66ea8e5a 2020-10-17 stsp output_unidiff_chunk(outinfo, dest, state, info, result,
597 dc306c6b 2023-08-29 stsp true, show_function_prototypes, &cc);
598 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_free(state);
599 3b0f3d61 2020-01-22 neels return DIFF_RC_OK;