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 <inttypes.h>
20 e10a628a 2020-09-16 stsp #include <stdbool.h>
21 e10a628a 2020-09-16 stsp #include <stdio.h>
22 e10a628a 2020-09-16 stsp #include <stdlib.h>
24 1dfba055 2020-10-07 stsp #include <arraylist.h>
25 1dfba055 2020-10-07 stsp #include <diff_main.h>
26 1dfba055 2020-10-07 stsp #include <diff_output.h>
28 85ab4559 2020-09-22 stsp #include "diff_internal.h"
29 2a1b94d0 2020-09-26 stsp #include "diff_debug.h"
32 cbf93b70 2020-10-16 stsp diff_chunk_context_empty(const struct diff_chunk_context *cc)
34 d362ea2e 2020-07-25 stsp return diff_range_empty(&cc->chunk);
38 fe8af0d6 2020-10-06 stsp diff_chunk_get_left_start(const struct diff_chunk *c,
39 fe8af0d6 2020-10-06 stsp const struct diff_result *r, int context_lines)
41 fe8af0d6 2020-10-06 stsp int left_start = diff_atom_root_idx(&r->left, c->left_start);
42 fe8af0d6 2020-10-06 stsp return MAX(0, left_start - context_lines);
46 fe8af0d6 2020-10-06 stsp diff_chunk_get_left_end(const struct diff_chunk *c,
47 fe8af0d6 2020-10-06 stsp const struct diff_result *r, int context_lines)
49 fe8af0d6 2020-10-06 stsp int left_start = diff_chunk_get_left_start(c, r, 0);
50 fe8af0d6 2020-10-06 stsp return MIN(r->left.atoms.len,
51 fe8af0d6 2020-10-06 stsp left_start + c->left_count + context_lines);
55 fe8af0d6 2020-10-06 stsp diff_chunk_get_right_start(const struct diff_chunk *c,
56 fe8af0d6 2020-10-06 stsp const struct diff_result *r, int context_lines)
58 fe8af0d6 2020-10-06 stsp int right_start = diff_atom_root_idx(&r->right, c->right_start);
59 fe8af0d6 2020-10-06 stsp return MAX(0, right_start - context_lines);
63 fe8af0d6 2020-10-06 stsp diff_chunk_get_right_end(const struct diff_chunk *c,
64 fe8af0d6 2020-10-06 stsp const struct diff_result *r, int context_lines)
66 fe8af0d6 2020-10-06 stsp int right_start = diff_chunk_get_right_start(c, r, 0);
67 fe8af0d6 2020-10-06 stsp return MIN(r->right.atoms.len,
68 fe8af0d6 2020-10-06 stsp right_start + c->right_count + context_lines);
72 f374e913 2020-09-22 stsp diff_chunk_context_get(struct diff_chunk_context *cc, const struct diff_result *r,
73 0d27172a 2020-05-06 neels int chunk_idx, int context_lines)
75 3b0f3d61 2020-01-22 neels const struct diff_chunk *c = &r->chunks.head[chunk_idx];
76 fe8af0d6 2020-10-06 stsp int left_start = diff_chunk_get_left_start(c, r, context_lines);
77 fe8af0d6 2020-10-06 stsp int left_end = diff_chunk_get_left_end(c, r, context_lines);
78 fe8af0d6 2020-10-06 stsp int right_start = diff_chunk_get_right_start(c, r, context_lines);
79 fe8af0d6 2020-10-06 stsp int right_end = diff_chunk_get_right_end(c, r, context_lines);
81 f374e913 2020-09-22 stsp *cc = (struct diff_chunk_context){
83 3b0f3d61 2020-01-22 neels .start = chunk_idx,
84 3b0f3d61 2020-01-22 neels .end = chunk_idx + 1,
87 0d27172a 2020-05-06 neels .start = left_start,
88 0d27172a 2020-05-06 neels .end = left_end,
91 0d27172a 2020-05-06 neels .start = right_start,
92 0d27172a 2020-05-06 neels .end = right_end,
98 26595c7d 2020-10-15 stsp diff_chunk_contexts_touch(const struct diff_chunk_context *cc,
99 26595c7d 2020-10-15 stsp const struct diff_chunk_context *other)
101 d362ea2e 2020-07-25 stsp return diff_ranges_touch(&cc->chunk, &other->chunk)
102 d362ea2e 2020-07-25 stsp || diff_ranges_touch(&cc->left, &other->left)
103 d362ea2e 2020-07-25 stsp || diff_ranges_touch(&cc->right, &other->right);
107 26595c7d 2020-10-15 stsp diff_chunk_contexts_merge(struct diff_chunk_context *cc,
108 26595c7d 2020-10-15 stsp const struct diff_chunk_context *other)
110 d362ea2e 2020-07-25 stsp diff_ranges_merge(&cc->chunk, &other->chunk);
111 d362ea2e 2020-07-25 stsp diff_ranges_merge(&cc->left, &other->left);
112 d362ea2e 2020-07-25 stsp diff_ranges_merge(&cc->right, &other->right);
116 7187fe97 2020-10-17 stsp diff_chunk_context_load_change(struct diff_chunk_context *cc,
117 7187fe97 2020-10-17 stsp int *nchunks_used,
118 7187fe97 2020-10-17 stsp struct diff_result *result,
119 7187fe97 2020-10-17 stsp int start_chunk_idx,
120 7187fe97 2020-10-17 stsp int context_lines)
123 7187fe97 2020-10-17 stsp int seen_minus = 0, seen_plus = 0;
125 7187fe97 2020-10-17 stsp if (nchunks_used)
126 7187fe97 2020-10-17 stsp *nchunks_used = 0;
128 7187fe97 2020-10-17 stsp for (i = start_chunk_idx; i < result->chunks.len; i++) {
129 7187fe97 2020-10-17 stsp struct diff_chunk *chunk = &result->chunks.head[i];
130 7187fe97 2020-10-17 stsp enum diff_chunk_type t = diff_chunk_type(chunk);
131 7187fe97 2020-10-17 stsp struct diff_chunk_context next;
133 7187fe97 2020-10-17 stsp if (t != CHUNK_MINUS && t != CHUNK_PLUS) {
134 7187fe97 2020-10-17 stsp if (nchunks_used)
135 7187fe97 2020-10-17 stsp (*nchunks_used)++;
136 7187fe97 2020-10-17 stsp if (seen_minus && seen_plus)
140 7187fe97 2020-10-17 stsp } else if (t == CHUNK_MINUS)
141 7187fe97 2020-10-17 stsp seen_minus = 1;
142 7187fe97 2020-10-17 stsp else if (t == CHUNK_PLUS)
143 7187fe97 2020-10-17 stsp seen_plus = 1;
145 7187fe97 2020-10-17 stsp if (diff_chunk_context_empty(cc)) {
146 7187fe97 2020-10-17 stsp /* Note down the start point, any number of subsequent
147 7187fe97 2020-10-17 stsp * chunks may be joined up to this chunk by being
148 7187fe97 2020-10-17 stsp * directly adjacent. */
149 7187fe97 2020-10-17 stsp diff_chunk_context_get(cc, result, i, context_lines);
150 7187fe97 2020-10-17 stsp if (nchunks_used)
151 7187fe97 2020-10-17 stsp (*nchunks_used)++;
155 7187fe97 2020-10-17 stsp /* There already is a previous chunk noted down for being
156 7187fe97 2020-10-17 stsp * printed. Does it join up with this one? */
157 7187fe97 2020-10-17 stsp diff_chunk_context_get(&next, result, i, context_lines);
159 7187fe97 2020-10-17 stsp if (diff_chunk_contexts_touch(cc, &next)) {
160 7187fe97 2020-10-17 stsp /* This next context touches or overlaps the previous
161 7187fe97 2020-10-17 stsp * one, join. */
162 7187fe97 2020-10-17 stsp diff_chunk_contexts_merge(cc, &next);
163 7187fe97 2020-10-17 stsp if (nchunks_used)
164 7187fe97 2020-10-17 stsp (*nchunks_used)++;
171 11caa5cc 2020-09-22 stsp struct diff_output_unidiff_state {
172 11caa5cc 2020-09-22 stsp bool header_printed;
175 11caa5cc 2020-09-22 stsp struct diff_output_unidiff_state *
176 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_alloc(void)
178 11caa5cc 2020-09-22 stsp struct diff_output_unidiff_state *state;
180 11caa5cc 2020-09-22 stsp state = calloc(1, sizeof(struct diff_output_unidiff_state));
181 11caa5cc 2020-09-22 stsp if (state != NULL)
182 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_reset(state);
183 11caa5cc 2020-09-22 stsp return state;
187 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_reset(struct diff_output_unidiff_state *state)
189 11caa5cc 2020-09-22 stsp state->header_printed = false;
193 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_free(struct diff_output_unidiff_state *state)
195 11caa5cc 2020-09-22 stsp free(state);
199 2c20a3ed 2020-09-22 stsp output_unidiff_chunk(struct diff_output_info *outinfo, FILE *dest,
200 2c20a3ed 2020-09-22 stsp struct diff_output_unidiff_state *state,
201 2c20a3ed 2020-09-22 stsp const struct diff_input_info *info,
202 2c20a3ed 2020-09-22 stsp const struct diff_result *result,
203 66ea8e5a 2020-10-17 stsp bool print_header,
204 2c20a3ed 2020-09-22 stsp const struct diff_chunk_context *cc)
206 b6adedb6 2020-10-07 stsp int rc, left_start, left_len, right_start, right_len;
207 ab528e22 2020-09-22 stsp off_t outoff = 0, *offp;
209 d362ea2e 2020-07-25 stsp if (diff_range_empty(&cc->left) && diff_range_empty(&cc->right))
210 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
212 2c20a3ed 2020-09-22 stsp if (outinfo && outinfo->line_offsets.len > 0) {
213 2c20a3ed 2020-09-22 stsp unsigned int idx = outinfo->line_offsets.len - 1;
214 2c20a3ed 2020-09-22 stsp outoff = outinfo->line_offsets.head[idx];
217 66ea8e5a 2020-10-17 stsp if (print_header && !(state->header_printed)) {
218 2c20a3ed 2020-09-22 stsp rc = fprintf(dest, "--- %s\n", info->left_path ? : "a");
219 2c20a3ed 2020-09-22 stsp if (rc < 0)
220 2c20a3ed 2020-09-22 stsp return errno;
221 2c20a3ed 2020-09-22 stsp if (outinfo) {
222 2c20a3ed 2020-09-22 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
223 dabc1008 2020-09-22 stsp if (offp == NULL)
224 dabc1008 2020-09-22 stsp return ENOMEM;
225 2c20a3ed 2020-09-22 stsp outoff += rc;
226 2c20a3ed 2020-09-22 stsp *offp = outoff;
229 2c20a3ed 2020-09-22 stsp rc = fprintf(dest, "+++ %s\n", info->right_path ? : "b");
230 2c20a3ed 2020-09-22 stsp if (rc < 0)
231 2c20a3ed 2020-09-22 stsp return errno;
232 2c20a3ed 2020-09-22 stsp if (outinfo) {
233 2c20a3ed 2020-09-22 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
234 dabc1008 2020-09-22 stsp if (offp == NULL)
235 dabc1008 2020-09-22 stsp return ENOMEM;
236 2c20a3ed 2020-09-22 stsp outoff += rc;
237 2c20a3ed 2020-09-22 stsp *offp = outoff;
240 11caa5cc 2020-09-22 stsp state->header_printed = true;
243 b6adedb6 2020-10-07 stsp left_len = cc->left.end - cc->left.start;
244 11d9f2f7 2020-10-07 stsp if (result->left.atoms.len == 0)
245 11d9f2f7 2020-10-07 stsp left_start = 0;
246 11d9f2f7 2020-10-07 stsp else if (left_len == 0 && cc->left.start > 0)
247 b6adedb6 2020-10-07 stsp left_start = cc->left.start;
249 b6adedb6 2020-10-07 stsp left_start = cc->left.start + 1;
251 b6adedb6 2020-10-07 stsp right_len = cc->right.end - cc->right.start;
252 11d9f2f7 2020-10-07 stsp if (result->right.atoms.len == 0)
253 11d9f2f7 2020-10-07 stsp right_start = 0;
254 11d9f2f7 2020-10-07 stsp else if (right_len == 0 && cc->right.start > 0)
255 b6adedb6 2020-10-07 stsp right_start = cc->right.start;
257 b6adedb6 2020-10-07 stsp right_start = cc->right.start + 1;
259 d2dfa2ec 2020-10-07 stsp if (left_len == 1 && right_len == 1) {
260 d2dfa2ec 2020-10-07 stsp rc = fprintf(dest, "@@ -%d +%d @@\n",
261 d2dfa2ec 2020-10-07 stsp left_start, right_start);
262 d2dfa2ec 2020-10-07 stsp } else if (left_len == 1 && right_len != 1) {
263 b6adedb6 2020-10-07 stsp rc = fprintf(dest, "@@ -%d +%d,%d @@\n",
264 b6adedb6 2020-10-07 stsp left_start, right_start, right_len);
265 b6adedb6 2020-10-07 stsp } else if (left_len != 1 && right_len == 1) {
266 b6adedb6 2020-10-07 stsp rc = fprintf(dest, "@@ -%d,%d +%d @@\n",
267 b6adedb6 2020-10-07 stsp left_start, left_len, right_start);
269 b6adedb6 2020-10-07 stsp rc = fprintf(dest, "@@ -%d,%d +%d,%d @@\n",
270 b6adedb6 2020-10-07 stsp left_start, left_len, right_start, right_len);
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;
283 0d27172a 2020-05-06 neels /* Got the absolute line numbers where to start printing, and the index
284 0d27172a 2020-05-06 neels * of the interesting (non-context) chunk.
285 0d27172a 2020-05-06 neels * To print context lines above the interesting chunk, nipping on the
286 0d27172a 2020-05-06 neels * previous chunk index may be necessary.
287 0d27172a 2020-05-06 neels * It is guaranteed to be only context lines where left == right, so it
288 0d27172a 2020-05-06 neels * suffices to look on the left. */
289 0d27172a 2020-05-06 neels const struct diff_chunk *first_chunk;
290 0d27172a 2020-05-06 neels int chunk_start_line;
291 0d27172a 2020-05-06 neels first_chunk = &result->chunks.head[cc->chunk.start];
292 0d27172a 2020-05-06 neels chunk_start_line = diff_atom_root_idx(&result->left,
293 0d27172a 2020-05-06 neels first_chunk->left_start);
294 2c20a3ed 2020-09-22 stsp if (cc->left.start < chunk_start_line) {
295 2c20a3ed 2020-09-22 stsp rc = diff_output_lines(outinfo, dest, " ",
296 0d27172a 2020-05-06 neels &result->left.atoms.head[cc->left.start],
297 3b0f3d61 2020-01-22 neels chunk_start_line - cc->left.start);
302 3b0f3d61 2020-01-22 neels /* Now write out all the joined chunks and contexts between them */
303 3b0f3d61 2020-01-22 neels int c_idx;
304 3b0f3d61 2020-01-22 neels for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
305 3b0f3d61 2020-01-22 neels const struct diff_chunk *c = &result->chunks.head[c_idx];
307 3b0f3d61 2020-01-22 neels if (c->left_count && c->right_count)
308 2c20a3ed 2020-09-22 stsp rc = diff_output_lines(outinfo, dest,
309 0d27172a 2020-05-06 neels c->solved ? " " : "?",
310 0d27172a 2020-05-06 neels c->left_start, c->left_count);
311 3b0f3d61 2020-01-22 neels else if (c->left_count && !c->right_count)
312 2c20a3ed 2020-09-22 stsp rc = diff_output_lines(outinfo, dest,
313 0d27172a 2020-05-06 neels c->solved ? "-" : "?",
314 0d27172a 2020-05-06 neels c->left_start, c->left_count);
315 3b0f3d61 2020-01-22 neels else if (c->right_count && !c->left_count)
316 2c20a3ed 2020-09-22 stsp rc = diff_output_lines(outinfo, dest,
317 0d27172a 2020-05-06 neels c->solved ? "+" : "?",
318 0d27172a 2020-05-06 neels c->right_start, c->right_count);
322 7021523c 2020-10-16 stsp if (cc->chunk.end == result->chunks.len) {
323 7021523c 2020-10-16 stsp rc = diff_output_trailing_newline_msg(outinfo, dest, c);
324 7021523c 2020-10-16 stsp if (rc != DIFF_RC_OK)
329 3b0f3d61 2020-01-22 neels /* Trailing context? */
330 0d27172a 2020-05-06 neels const struct diff_chunk *last_chunk;
331 0d27172a 2020-05-06 neels int chunk_end_line;
332 0d27172a 2020-05-06 neels last_chunk = &result->chunks.head[cc->chunk.end - 1];
333 0d27172a 2020-05-06 neels chunk_end_line = diff_atom_root_idx(&result->left,
334 0d27172a 2020-05-06 neels last_chunk->left_start
335 0d27172a 2020-05-06 neels + last_chunk->left_count);
336 2c20a3ed 2020-09-22 stsp if (cc->left.end > chunk_end_line) {
337 2c20a3ed 2020-09-22 stsp rc = diff_output_lines(outinfo, dest, " ",
338 0d27172a 2020-05-06 neels &result->left.atoms.head[chunk_end_line],
339 3b0f3d61 2020-01-22 neels cc->left.end - chunk_end_line);
344 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
348 2c20a3ed 2020-09-22 stsp diff_output_unidiff_chunk(struct diff_output_info **output_info, FILE *dest,
349 2c20a3ed 2020-09-22 stsp struct diff_output_unidiff_state *state,
350 2c20a3ed 2020-09-22 stsp const struct diff_input_info *info,
351 2c20a3ed 2020-09-22 stsp const struct diff_result *result,
352 2c20a3ed 2020-09-22 stsp const struct diff_chunk_context *cc)
354 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
356 2c20a3ed 2020-09-22 stsp if (output_info) {
357 2c20a3ed 2020-09-22 stsp *output_info = diff_output_info_alloc();
358 2c20a3ed 2020-09-22 stsp if (*output_info == NULL)
359 2c20a3ed 2020-09-22 stsp return ENOMEM;
360 2c20a3ed 2020-09-22 stsp outinfo = *output_info;
363 2c20a3ed 2020-09-22 stsp return output_unidiff_chunk(outinfo, dest, state, info,
364 66ea8e5a 2020-10-17 stsp result, false, cc);
368 2c20a3ed 2020-09-22 stsp diff_output_unidiff(struct diff_output_info **output_info,
369 2c20a3ed 2020-09-22 stsp FILE *dest, const struct diff_input_info *info,
370 0d27172a 2020-05-06 neels const struct diff_result *result,
371 0d27172a 2020-05-06 neels unsigned int context_lines)
373 11caa5cc 2020-09-22 stsp struct diff_output_unidiff_state *state;
374 11caa5cc 2020-09-22 stsp struct diff_chunk_context cc = {};
375 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
378 3b0f3d61 2020-01-22 neels if (!result)
379 3e6cba3a 2020-08-13 stsp return EINVAL;
380 3b0f3d61 2020-01-22 neels if (result->rc != DIFF_RC_OK)
381 3b0f3d61 2020-01-22 neels return result->rc;
383 2c20a3ed 2020-09-22 stsp if (output_info) {
384 2c20a3ed 2020-09-22 stsp *output_info = diff_output_info_alloc();
385 2c20a3ed 2020-09-22 stsp if (*output_info == NULL)
386 2c20a3ed 2020-09-22 stsp return ENOMEM;
387 2c20a3ed 2020-09-22 stsp outinfo = *output_info;
390 11caa5cc 2020-09-22 stsp state = diff_output_unidiff_state_alloc();
391 2c20a3ed 2020-09-22 stsp if (state == NULL) {
392 2c20a3ed 2020-09-22 stsp if (output_info) {
393 2c20a3ed 2020-09-22 stsp diff_output_info_free(*output_info);
394 2c20a3ed 2020-09-22 stsp *output_info = NULL;
396 11caa5cc 2020-09-22 stsp return ENOMEM;
400 5ff75996 2020-10-11 neels for (i = 0; i < result->chunks.len; i++) {
401 5ff75996 2020-10-11 neels struct diff_chunk *c = &result->chunks.head[i];
402 5ff75996 2020-10-11 neels enum diff_chunk_type t = diff_chunk_type(c);
404 5ff75996 2020-10-11 neels debug("[%d] %s lines L%d R%d @L %d @R %d\n",
405 5ff75996 2020-10-11 neels i, (t == CHUNK_MINUS ? "minus" :
406 5ff75996 2020-10-11 neels (t == CHUNK_PLUS ? "plus" :
407 5ff75996 2020-10-11 neels (t == CHUNK_SAME ? "same" : "?"))),
408 5ff75996 2020-10-11 neels c->left_count,
409 5ff75996 2020-10-11 neels c->right_count,
410 5ff75996 2020-10-11 neels c->left_start ? diff_atom_root_idx(&result->left, c->left_start) : -1,
411 5ff75996 2020-10-11 neels c->right_start ? diff_atom_root_idx(&result->right, c->right_start) : -1);
415 3b0f3d61 2020-01-22 neels for (i = 0; i < result->chunks.len; i++) {
416 3b0f3d61 2020-01-22 neels struct diff_chunk *c = &result->chunks.head[i];
417 8546b045 2020-09-20 neels enum diff_chunk_type t = diff_chunk_type(c);
418 f374e913 2020-09-22 stsp struct diff_chunk_context next;
420 9cc49695 2020-05-05 neels if (t != CHUNK_MINUS && t != CHUNK_PLUS)
423 cbf93b70 2020-10-16 stsp if (diff_chunk_context_empty(&cc)) {
424 9cc49695 2020-05-05 neels /* These are the first lines being printed.
425 0d27172a 2020-05-06 neels * Note down the start point, any number of subsequent
426 0d27172a 2020-05-06 neels * chunks may be joined up to this unidiff chunk by
427 0d27172a 2020-05-06 neels * context lines or by being directly adjacent. */
428 f374e913 2020-09-22 stsp diff_chunk_context_get(&cc, result, i, context_lines);
429 0d27172a 2020-05-06 neels debug("new chunk to be printed:"
430 0d27172a 2020-05-06 neels " chunk %d-%d left %d-%d right %d-%d\n",
431 9cc49695 2020-05-05 neels cc.chunk.start, cc.chunk.end,
432 9cc49695 2020-05-05 neels cc.left.start, cc.left.end,
433 9cc49695 2020-05-05 neels cc.right.start, cc.right.end);
437 0d27172a 2020-05-06 neels /* There already is a previous chunk noted down for being
438 0d27172a 2020-05-06 neels * printed. Does it join up with this one? */
439 f374e913 2020-09-22 stsp diff_chunk_context_get(&next, result, i, context_lines);
440 0d27172a 2020-05-06 neels debug("new chunk to be printed:"
441 0d27172a 2020-05-06 neels " chunk %d-%d left %d-%d right %d-%d\n",
442 9cc49695 2020-05-05 neels next.chunk.start, next.chunk.end,
443 9cc49695 2020-05-05 neels next.left.start, next.left.end,
444 9cc49695 2020-05-05 neels next.right.start, next.right.end);
446 26595c7d 2020-10-15 stsp if (diff_chunk_contexts_touch(&cc, &next)) {
447 0d27172a 2020-05-06 neels /* This next context touches or overlaps the previous
448 0d27172a 2020-05-06 neels * one, join. */
449 26595c7d 2020-10-15 stsp diff_chunk_contexts_merge(&cc, &next);
450 0d27172a 2020-05-06 neels debug("new chunk to be printed touches previous chunk,"
451 0d27172a 2020-05-06 neels " now: left %d-%d right %d-%d\n",
452 9cc49695 2020-05-05 neels cc.left.start, cc.left.end,
453 9cc49695 2020-05-05 neels cc.right.start, cc.right.end);
457 0d27172a 2020-05-06 neels /* No touching, so the previous context is complete with a gap
458 0d27172a 2020-05-06 neels * between it and this next one. Print the previous one and
459 0d27172a 2020-05-06 neels * start fresh here. */
460 0d27172a 2020-05-06 neels debug("new chunk to be printed does not touch previous chunk;"
461 0d27172a 2020-05-06 neels " print left %d-%d right %d-%d\n",
462 9cc49695 2020-05-05 neels cc.left.start, cc.left.end, cc.right.start, cc.right.end);
463 66ea8e5a 2020-10-17 stsp output_unidiff_chunk(outinfo, dest, state, info, result,
464 66ea8e5a 2020-10-17 stsp true, &cc);
465 9cc49695 2020-05-05 neels cc = next;
466 9cc49695 2020-05-05 neels debug("new unprinted chunk is left %d-%d right %d-%d\n",
467 9cc49695 2020-05-05 neels cc.left.start, cc.left.end, cc.right.start, cc.right.end);
470 cbf93b70 2020-10-16 stsp if (!diff_chunk_context_empty(&cc))
471 66ea8e5a 2020-10-17 stsp output_unidiff_chunk(outinfo, dest, state, info, result,
472 66ea8e5a 2020-10-17 stsp true, &cc);
473 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_free(state);
474 3b0f3d61 2020-01-22 neels return DIFF_RC_OK;