Blame


1 fe621944 2020-11-10 stsp /* Produce a unidiff output from a diff_result. */
2 fe621944 2020-11-10 stsp /*
3 fe621944 2020-11-10 stsp * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
4 fe621944 2020-11-10 stsp *
5 fe621944 2020-11-10 stsp * Permission to use, copy, modify, and distribute this software for any
6 fe621944 2020-11-10 stsp * purpose with or without fee is hereby granted, provided that the above
7 fe621944 2020-11-10 stsp * copyright notice and this permission notice appear in all copies.
8 fe621944 2020-11-10 stsp *
9 fe621944 2020-11-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 fe621944 2020-11-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 fe621944 2020-11-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 fe621944 2020-11-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 fe621944 2020-11-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 fe621944 2020-11-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 fe621944 2020-11-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 fe621944 2020-11-10 stsp */
17 fe621944 2020-11-10 stsp
18 fe621944 2020-11-10 stsp #include <errno.h>
19 fe621944 2020-11-10 stsp #include <stdbool.h>
20 f3c44083 2020-11-14 naddy #include <stdint.h>
21 fe621944 2020-11-10 stsp #include <stdio.h>
22 fe621944 2020-11-10 stsp #include <stdlib.h>
23 f3b2b552 2020-12-10 stsp #include <string.h>
24 fe621944 2020-11-10 stsp #include <assert.h>
25 fe621944 2020-11-10 stsp
26 fe621944 2020-11-10 stsp #include <arraylist.h>
27 fe621944 2020-11-10 stsp #include <diff_main.h>
28 fe621944 2020-11-10 stsp #include <diff_output.h>
29 fe621944 2020-11-10 stsp
30 fe621944 2020-11-10 stsp #include "diff_internal.h"
31 fe621944 2020-11-10 stsp #include "diff_debug.h"
32 fe621944 2020-11-10 stsp
33 fe621944 2020-11-10 stsp bool
34 fe621944 2020-11-10 stsp diff_chunk_context_empty(const struct diff_chunk_context *cc)
35 fe621944 2020-11-10 stsp {
36 fe621944 2020-11-10 stsp return diff_range_empty(&cc->chunk);
37 fe621944 2020-11-10 stsp }
38 fe621944 2020-11-10 stsp
39 fe621944 2020-11-10 stsp int
40 fe621944 2020-11-10 stsp diff_chunk_get_left_start(const struct diff_chunk *c,
41 fe621944 2020-11-10 stsp const struct diff_result *r, int context_lines)
42 fe621944 2020-11-10 stsp {
43 fe621944 2020-11-10 stsp int left_start = diff_atom_root_idx(r->left, c->left_start);
44 fe621944 2020-11-10 stsp return MAX(0, left_start - context_lines);
45 fe621944 2020-11-10 stsp }
46 fe621944 2020-11-10 stsp
47 fe621944 2020-11-10 stsp int
48 fe621944 2020-11-10 stsp diff_chunk_get_left_end(const struct diff_chunk *c,
49 fe621944 2020-11-10 stsp const struct diff_result *r, int context_lines)
50 fe621944 2020-11-10 stsp {
51 fe621944 2020-11-10 stsp int left_start = diff_chunk_get_left_start(c, r, 0);
52 fe621944 2020-11-10 stsp return MIN(r->left->atoms.len,
53 fe621944 2020-11-10 stsp left_start + c->left_count + context_lines);
54 fe621944 2020-11-10 stsp }
55 fe621944 2020-11-10 stsp
56 fe621944 2020-11-10 stsp int
57 fe621944 2020-11-10 stsp diff_chunk_get_right_start(const struct diff_chunk *c,
58 fe621944 2020-11-10 stsp const struct diff_result *r, int context_lines)
59 fe621944 2020-11-10 stsp {
60 fe621944 2020-11-10 stsp int right_start = diff_atom_root_idx(r->right, c->right_start);
61 fe621944 2020-11-10 stsp return MAX(0, right_start - context_lines);
62 fe621944 2020-11-10 stsp }
63 fe621944 2020-11-10 stsp
64 fe621944 2020-11-10 stsp int
65 fe621944 2020-11-10 stsp diff_chunk_get_right_end(const struct diff_chunk *c,
66 fe621944 2020-11-10 stsp const struct diff_result *r, int context_lines)
67 fe621944 2020-11-10 stsp {
68 fe621944 2020-11-10 stsp int right_start = diff_chunk_get_right_start(c, r, 0);
69 fe621944 2020-11-10 stsp return MIN(r->right->atoms.len,
70 fe621944 2020-11-10 stsp right_start + c->right_count + context_lines);
71 fe621944 2020-11-10 stsp }
72 fe621944 2020-11-10 stsp
73 fe621944 2020-11-10 stsp struct diff_chunk *
74 fe621944 2020-11-10 stsp diff_chunk_get(const struct diff_result *r, int chunk_idx)
75 fe621944 2020-11-10 stsp {
76 fe621944 2020-11-10 stsp return &r->chunks.head[chunk_idx];
77 fe621944 2020-11-10 stsp }
78 fe621944 2020-11-10 stsp
79 fe621944 2020-11-10 stsp int
80 fe621944 2020-11-10 stsp diff_chunk_get_left_count(struct diff_chunk *c)
81 fe621944 2020-11-10 stsp {
82 fe621944 2020-11-10 stsp return c->left_count;
83 fe621944 2020-11-10 stsp }
84 fe621944 2020-11-10 stsp
85 fe621944 2020-11-10 stsp int
86 fe621944 2020-11-10 stsp diff_chunk_get_right_count(struct diff_chunk *c)
87 fe621944 2020-11-10 stsp {
88 fe621944 2020-11-10 stsp return c->right_count;
89 fe621944 2020-11-10 stsp }
90 fe621944 2020-11-10 stsp
91 fe621944 2020-11-10 stsp void
92 fe621944 2020-11-10 stsp diff_chunk_context_get(struct diff_chunk_context *cc, const struct diff_result *r,
93 fe621944 2020-11-10 stsp int chunk_idx, int context_lines)
94 fe621944 2020-11-10 stsp {
95 fe621944 2020-11-10 stsp const struct diff_chunk *c = &r->chunks.head[chunk_idx];
96 fe621944 2020-11-10 stsp int left_start = diff_chunk_get_left_start(c, r, context_lines);
97 fe621944 2020-11-10 stsp int left_end = diff_chunk_get_left_end(c, r, context_lines);
98 fe621944 2020-11-10 stsp int right_start = diff_chunk_get_right_start(c, r, context_lines);
99 fe621944 2020-11-10 stsp int right_end = diff_chunk_get_right_end(c, r, context_lines);
100 fe621944 2020-11-10 stsp
101 fe621944 2020-11-10 stsp *cc = (struct diff_chunk_context){
102 fe621944 2020-11-10 stsp .chunk = {
103 fe621944 2020-11-10 stsp .start = chunk_idx,
104 fe621944 2020-11-10 stsp .end = chunk_idx + 1,
105 fe621944 2020-11-10 stsp },
106 fe621944 2020-11-10 stsp .left = {
107 fe621944 2020-11-10 stsp .start = left_start,
108 fe621944 2020-11-10 stsp .end = left_end,
109 fe621944 2020-11-10 stsp },
110 fe621944 2020-11-10 stsp .right = {
111 fe621944 2020-11-10 stsp .start = right_start,
112 fe621944 2020-11-10 stsp .end = right_end,
113 fe621944 2020-11-10 stsp },
114 fe621944 2020-11-10 stsp };
115 fe621944 2020-11-10 stsp }
116 fe621944 2020-11-10 stsp
117 fe621944 2020-11-10 stsp bool
118 fe621944 2020-11-10 stsp diff_chunk_contexts_touch(const struct diff_chunk_context *cc,
119 fe621944 2020-11-10 stsp const struct diff_chunk_context *other)
120 fe621944 2020-11-10 stsp {
121 fe621944 2020-11-10 stsp return diff_ranges_touch(&cc->chunk, &other->chunk)
122 fe621944 2020-11-10 stsp || diff_ranges_touch(&cc->left, &other->left)
123 fe621944 2020-11-10 stsp || diff_ranges_touch(&cc->right, &other->right);
124 fe621944 2020-11-10 stsp }
125 fe621944 2020-11-10 stsp
126 fe621944 2020-11-10 stsp void
127 fe621944 2020-11-10 stsp diff_chunk_contexts_merge(struct diff_chunk_context *cc,
128 fe621944 2020-11-10 stsp const struct diff_chunk_context *other)
129 fe621944 2020-11-10 stsp {
130 fe621944 2020-11-10 stsp diff_ranges_merge(&cc->chunk, &other->chunk);
131 fe621944 2020-11-10 stsp diff_ranges_merge(&cc->left, &other->left);
132 fe621944 2020-11-10 stsp diff_ranges_merge(&cc->right, &other->right);
133 fe621944 2020-11-10 stsp }
134 fe621944 2020-11-10 stsp
135 fe621944 2020-11-10 stsp void
136 fe621944 2020-11-10 stsp diff_chunk_context_load_change(struct diff_chunk_context *cc,
137 fe621944 2020-11-10 stsp int *nchunks_used,
138 fe621944 2020-11-10 stsp struct diff_result *result,
139 fe621944 2020-11-10 stsp int start_chunk_idx,
140 fe621944 2020-11-10 stsp int context_lines)
141 fe621944 2020-11-10 stsp {
142 fe621944 2020-11-10 stsp int i;
143 fe621944 2020-11-10 stsp int seen_minus = 0, seen_plus = 0;
144 fe621944 2020-11-10 stsp
145 fe621944 2020-11-10 stsp if (nchunks_used)
146 fe621944 2020-11-10 stsp *nchunks_used = 0;
147 fe621944 2020-11-10 stsp
148 fe621944 2020-11-10 stsp for (i = start_chunk_idx; i < result->chunks.len; i++) {
149 fe621944 2020-11-10 stsp struct diff_chunk *chunk = &result->chunks.head[i];
150 fe621944 2020-11-10 stsp enum diff_chunk_type t = diff_chunk_type(chunk);
151 fe621944 2020-11-10 stsp struct diff_chunk_context next;
152 fe621944 2020-11-10 stsp
153 fe621944 2020-11-10 stsp if (t != CHUNK_MINUS && t != CHUNK_PLUS) {
154 fe621944 2020-11-10 stsp if (nchunks_used)
155 fe621944 2020-11-10 stsp (*nchunks_used)++;
156 fe621944 2020-11-10 stsp if (seen_minus || seen_plus)
157 fe621944 2020-11-10 stsp break;
158 fe621944 2020-11-10 stsp else
159 fe621944 2020-11-10 stsp continue;
160 fe621944 2020-11-10 stsp } else if (t == CHUNK_MINUS)
161 fe621944 2020-11-10 stsp seen_minus = 1;
162 fe621944 2020-11-10 stsp else if (t == CHUNK_PLUS)
163 fe621944 2020-11-10 stsp seen_plus = 1;
164 fe621944 2020-11-10 stsp
165 fe621944 2020-11-10 stsp if (diff_chunk_context_empty(cc)) {
166 fe621944 2020-11-10 stsp /* Note down the start point, any number of subsequent
167 fe621944 2020-11-10 stsp * chunks may be joined up to this chunk by being
168 fe621944 2020-11-10 stsp * directly adjacent. */
169 fe621944 2020-11-10 stsp diff_chunk_context_get(cc, result, i, context_lines);
170 fe621944 2020-11-10 stsp if (nchunks_used)
171 fe621944 2020-11-10 stsp (*nchunks_used)++;
172 fe621944 2020-11-10 stsp continue;
173 fe621944 2020-11-10 stsp }
174 fe621944 2020-11-10 stsp
175 fe621944 2020-11-10 stsp /* There already is a previous chunk noted down for being
176 fe621944 2020-11-10 stsp * printed. Does it join up with this one? */
177 fe621944 2020-11-10 stsp diff_chunk_context_get(&next, result, i, context_lines);
178 fe621944 2020-11-10 stsp
179 fe621944 2020-11-10 stsp if (diff_chunk_contexts_touch(cc, &next)) {
180 fe621944 2020-11-10 stsp /* This next context touches or overlaps the previous
181 fe621944 2020-11-10 stsp * one, join. */
182 fe621944 2020-11-10 stsp diff_chunk_contexts_merge(cc, &next);
183 fe621944 2020-11-10 stsp if (nchunks_used)
184 fe621944 2020-11-10 stsp (*nchunks_used)++;
185 fe621944 2020-11-10 stsp continue;
186 fe621944 2020-11-10 stsp } else
187 fe621944 2020-11-10 stsp break;
188 fe621944 2020-11-10 stsp }
189 fe621944 2020-11-10 stsp }
190 fe621944 2020-11-10 stsp
191 fe621944 2020-11-10 stsp struct diff_output_unidiff_state {
192 fe621944 2020-11-10 stsp bool header_printed;
193 f3b2b552 2020-12-10 stsp char prototype[DIFF_FUNCTION_CONTEXT_SIZE];
194 f3b2b552 2020-12-10 stsp int last_prototype_idx;
195 fe621944 2020-11-10 stsp };
196 fe621944 2020-11-10 stsp
197 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *
198 fe621944 2020-11-10 stsp diff_output_unidiff_state_alloc(void)
199 fe621944 2020-11-10 stsp {
200 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *state;
201 fe621944 2020-11-10 stsp
202 fe621944 2020-11-10 stsp state = calloc(1, sizeof(struct diff_output_unidiff_state));
203 fe621944 2020-11-10 stsp if (state != NULL)
204 fe621944 2020-11-10 stsp diff_output_unidiff_state_reset(state);
205 fe621944 2020-11-10 stsp return state;
206 fe621944 2020-11-10 stsp }
207 fe621944 2020-11-10 stsp
208 fe621944 2020-11-10 stsp void
209 fe621944 2020-11-10 stsp diff_output_unidiff_state_reset(struct diff_output_unidiff_state *state)
210 fe621944 2020-11-10 stsp {
211 fe621944 2020-11-10 stsp state->header_printed = false;
212 f3b2b552 2020-12-10 stsp memset(state->prototype, 0, sizeof(state->prototype));
213 f3b2b552 2020-12-10 stsp state->last_prototype_idx = 0;
214 fe621944 2020-11-10 stsp }
215 fe621944 2020-11-10 stsp
216 fe621944 2020-11-10 stsp void
217 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(struct diff_output_unidiff_state *state)
218 fe621944 2020-11-10 stsp {
219 fe621944 2020-11-10 stsp free(state);
220 fe621944 2020-11-10 stsp }
221 fe621944 2020-11-10 stsp
222 fe621944 2020-11-10 stsp static int
223 fe621944 2020-11-10 stsp output_unidiff_chunk(struct diff_output_info *outinfo, FILE *dest,
224 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *state,
225 fe621944 2020-11-10 stsp const struct diff_input_info *info,
226 fe621944 2020-11-10 stsp const struct diff_result *result,
227 fe621944 2020-11-10 stsp bool print_header, bool show_function_prototypes,
228 579042a9 2022-09-23 stsp const struct diff_chunk_context *cc, unsigned int ncontext)
229 fe621944 2020-11-10 stsp {
230 fe621944 2020-11-10 stsp int rc, left_start, left_len, right_start, right_len;
231 fe621944 2020-11-10 stsp off_t outoff = 0, *offp;
232 b77ebd68 2022-08-04 mark uint8_t *typep;
233 fe621944 2020-11-10 stsp
234 fe621944 2020-11-10 stsp if (diff_range_empty(&cc->left) && diff_range_empty(&cc->right))
235 fe621944 2020-11-10 stsp return DIFF_RC_OK;
236 fe621944 2020-11-10 stsp
237 fe621944 2020-11-10 stsp if (outinfo && outinfo->line_offsets.len > 0) {
238 fe621944 2020-11-10 stsp unsigned int idx = outinfo->line_offsets.len - 1;
239 fe621944 2020-11-10 stsp outoff = outinfo->line_offsets.head[idx];
240 fe621944 2020-11-10 stsp }
241 fe621944 2020-11-10 stsp
242 fe621944 2020-11-10 stsp if (print_header && !(state->header_printed)) {
243 c4cd9c5b 2020-11-21 stsp rc = fprintf(dest, "--- %s\n",
244 c4cd9c5b 2020-11-21 stsp diff_output_get_label_left(info));
245 fe621944 2020-11-10 stsp if (rc < 0)
246 fe621944 2020-11-10 stsp return errno;
247 fe621944 2020-11-10 stsp if (outinfo) {
248 fe621944 2020-11-10 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
249 fe621944 2020-11-10 stsp if (offp == NULL)
250 fe621944 2020-11-10 stsp return ENOMEM;
251 fe621944 2020-11-10 stsp outoff += rc;
252 fe621944 2020-11-10 stsp *offp = outoff;
253 b77ebd68 2022-08-04 mark ARRAYLIST_ADD(typep, outinfo->line_types);
254 b77ebd68 2022-08-04 mark if (typep == NULL)
255 b77ebd68 2022-08-04 mark return ENOMEM;
256 b77ebd68 2022-08-04 mark *typep = DIFF_LINE_MINUS;
257 fe621944 2020-11-10 stsp }
258 c4cd9c5b 2020-11-21 stsp rc = fprintf(dest, "+++ %s\n",
259 c4cd9c5b 2020-11-21 stsp diff_output_get_label_right(info));
260 fe621944 2020-11-10 stsp if (rc < 0)
261 fe621944 2020-11-10 stsp return errno;
262 fe621944 2020-11-10 stsp if (outinfo) {
263 fe621944 2020-11-10 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
264 fe621944 2020-11-10 stsp if (offp == NULL)
265 fe621944 2020-11-10 stsp return ENOMEM;
266 fe621944 2020-11-10 stsp outoff += rc;
267 fe621944 2020-11-10 stsp *offp = outoff;
268 b77ebd68 2022-08-04 mark ARRAYLIST_ADD(typep, outinfo->line_types);
269 b77ebd68 2022-08-04 mark if (typep == NULL)
270 b77ebd68 2022-08-04 mark return ENOMEM;
271 b77ebd68 2022-08-04 mark *typep = DIFF_LINE_PLUS;
272 fe621944 2020-11-10 stsp }
273 fe621944 2020-11-10 stsp state->header_printed = true;
274 fe621944 2020-11-10 stsp }
275 fe621944 2020-11-10 stsp
276 fe621944 2020-11-10 stsp left_len = cc->left.end - cc->left.start;
277 fe621944 2020-11-10 stsp if (result->left->atoms.len == 0)
278 fe621944 2020-11-10 stsp left_start = 0;
279 fe621944 2020-11-10 stsp else if (left_len == 0 && cc->left.start > 0)
280 fe621944 2020-11-10 stsp left_start = cc->left.start;
281 fe621944 2020-11-10 stsp else
282 fe621944 2020-11-10 stsp left_start = cc->left.start + 1;
283 fe621944 2020-11-10 stsp
284 fe621944 2020-11-10 stsp right_len = cc->right.end - cc->right.start;
285 fe621944 2020-11-10 stsp if (result->right->atoms.len == 0)
286 fe621944 2020-11-10 stsp right_start = 0;
287 fe621944 2020-11-10 stsp else if (right_len == 0 && cc->right.start > 0)
288 fe621944 2020-11-10 stsp right_start = cc->right.start;
289 fe621944 2020-11-10 stsp else
290 fe621944 2020-11-10 stsp right_start = cc->right.start + 1;
291 fe621944 2020-11-10 stsp
292 fe621944 2020-11-10 stsp if (show_function_prototypes) {
293 f3b2b552 2020-12-10 stsp rc = diff_output_match_function_prototype(state->prototype,
294 f3b2b552 2020-12-10 stsp sizeof(state->prototype), &state->last_prototype_idx,
295 579042a9 2022-09-23 stsp result, cc, ncontext);
296 fe621944 2020-11-10 stsp if (rc)
297 fe621944 2020-11-10 stsp return rc;
298 fe621944 2020-11-10 stsp }
299 fe621944 2020-11-10 stsp
300 fe621944 2020-11-10 stsp if (left_len == 1 && right_len == 1) {
301 fe621944 2020-11-10 stsp rc = fprintf(dest, "@@ -%d +%d @@%s%s\n",
302 fe621944 2020-11-10 stsp left_start, right_start,
303 f3b2b552 2020-12-10 stsp state->prototype[0] ? " " : "",
304 f3b2b552 2020-12-10 stsp state->prototype[0] ? state->prototype : "");
305 fe621944 2020-11-10 stsp } else if (left_len == 1 && right_len != 1) {
306 fe621944 2020-11-10 stsp rc = fprintf(dest, "@@ -%d +%d,%d @@%s%s\n",
307 fe621944 2020-11-10 stsp left_start, right_start, right_len,
308 f3b2b552 2020-12-10 stsp state->prototype[0] ? " " : "",
309 f3b2b552 2020-12-10 stsp state->prototype[0] ? state->prototype : "");
310 fe621944 2020-11-10 stsp } else if (left_len != 1 && right_len == 1) {
311 fe621944 2020-11-10 stsp rc = fprintf(dest, "@@ -%d,%d +%d @@%s%s\n",
312 fe621944 2020-11-10 stsp left_start, left_len, right_start,
313 f3b2b552 2020-12-10 stsp state->prototype[0] ? " " : "",
314 f3b2b552 2020-12-10 stsp state->prototype[0] ? state->prototype : "");
315 fe621944 2020-11-10 stsp } else {
316 fe621944 2020-11-10 stsp rc = fprintf(dest, "@@ -%d,%d +%d,%d @@%s%s\n",
317 fe621944 2020-11-10 stsp left_start, left_len, right_start, right_len,
318 f3b2b552 2020-12-10 stsp state->prototype[0] ? " " : "",
319 f3b2b552 2020-12-10 stsp state->prototype[0] ? state->prototype : "");
320 fe621944 2020-11-10 stsp }
321 fe621944 2020-11-10 stsp if (rc < 0)
322 fe621944 2020-11-10 stsp return errno;
323 fe621944 2020-11-10 stsp if (outinfo) {
324 fe621944 2020-11-10 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
325 fe621944 2020-11-10 stsp if (offp == NULL)
326 fe621944 2020-11-10 stsp return ENOMEM;
327 fe621944 2020-11-10 stsp outoff += rc;
328 fe621944 2020-11-10 stsp *offp = outoff;
329 b77ebd68 2022-08-04 mark ARRAYLIST_ADD(typep, outinfo->line_types);
330 b77ebd68 2022-08-04 mark if (typep == NULL)
331 b77ebd68 2022-08-04 mark return ENOMEM;
332 b77ebd68 2022-08-04 mark *typep = DIFF_LINE_HUNK;
333 fe621944 2020-11-10 stsp }
334 fe621944 2020-11-10 stsp
335 fe621944 2020-11-10 stsp /* Got the absolute line numbers where to start printing, and the index
336 fe621944 2020-11-10 stsp * of the interesting (non-context) chunk.
337 fe621944 2020-11-10 stsp * To print context lines above the interesting chunk, nipping on the
338 fe621944 2020-11-10 stsp * previous chunk index may be necessary.
339 fe621944 2020-11-10 stsp * It is guaranteed to be only context lines where left == right, so it
340 fe621944 2020-11-10 stsp * suffices to look on the left. */
341 fe621944 2020-11-10 stsp const struct diff_chunk *first_chunk;
342 fe621944 2020-11-10 stsp int chunk_start_line;
343 fe621944 2020-11-10 stsp first_chunk = &result->chunks.head[cc->chunk.start];
344 fe621944 2020-11-10 stsp chunk_start_line = diff_atom_root_idx(result->left,
345 fe621944 2020-11-10 stsp first_chunk->left_start);
346 fe621944 2020-11-10 stsp if (cc->left.start < chunk_start_line) {
347 fe621944 2020-11-10 stsp rc = diff_output_lines(outinfo, dest, " ",
348 fe621944 2020-11-10 stsp &result->left->atoms.head[cc->left.start],
349 fe621944 2020-11-10 stsp chunk_start_line - cc->left.start);
350 fe621944 2020-11-10 stsp if (rc)
351 fe621944 2020-11-10 stsp return rc;
352 fe621944 2020-11-10 stsp }
353 fe621944 2020-11-10 stsp
354 fe621944 2020-11-10 stsp /* Now write out all the joined chunks and contexts between them */
355 fe621944 2020-11-10 stsp int c_idx;
356 fe621944 2020-11-10 stsp for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
357 fe621944 2020-11-10 stsp const struct diff_chunk *c = &result->chunks.head[c_idx];
358 fe621944 2020-11-10 stsp
359 fe621944 2020-11-10 stsp if (c->left_count && c->right_count)
360 fe621944 2020-11-10 stsp rc = diff_output_lines(outinfo, dest,
361 fe621944 2020-11-10 stsp c->solved ? " " : "?",
362 fe621944 2020-11-10 stsp c->left_start, c->left_count);
363 fe621944 2020-11-10 stsp else if (c->left_count && !c->right_count)
364 fe621944 2020-11-10 stsp rc = diff_output_lines(outinfo, dest,
365 fe621944 2020-11-10 stsp c->solved ? "-" : "?",
366 fe621944 2020-11-10 stsp c->left_start, c->left_count);
367 fe621944 2020-11-10 stsp else if (c->right_count && !c->left_count)
368 fe621944 2020-11-10 stsp rc = diff_output_lines(outinfo, dest,
369 fe621944 2020-11-10 stsp c->solved ? "+" : "?",
370 fe621944 2020-11-10 stsp c->right_start, c->right_count);
371 fe621944 2020-11-10 stsp if (rc)
372 fe621944 2020-11-10 stsp return rc;
373 fe621944 2020-11-10 stsp
374 fe621944 2020-11-10 stsp if (cc->chunk.end == result->chunks.len) {
375 fe621944 2020-11-10 stsp rc = diff_output_trailing_newline_msg(outinfo, dest, c);
376 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK)
377 fe621944 2020-11-10 stsp return rc;
378 fe621944 2020-11-10 stsp }
379 fe621944 2020-11-10 stsp }
380 fe621944 2020-11-10 stsp
381 fe621944 2020-11-10 stsp /* Trailing context? */
382 fe621944 2020-11-10 stsp const struct diff_chunk *last_chunk;
383 fe621944 2020-11-10 stsp int chunk_end_line;
384 fe621944 2020-11-10 stsp last_chunk = &result->chunks.head[cc->chunk.end - 1];
385 fe621944 2020-11-10 stsp chunk_end_line = diff_atom_root_idx(result->left,
386 fe621944 2020-11-10 stsp last_chunk->left_start
387 fe621944 2020-11-10 stsp + last_chunk->left_count);
388 fe621944 2020-11-10 stsp if (cc->left.end > chunk_end_line) {
389 fe621944 2020-11-10 stsp rc = diff_output_lines(outinfo, dest, " ",
390 fe621944 2020-11-10 stsp &result->left->atoms.head[chunk_end_line],
391 fe621944 2020-11-10 stsp cc->left.end - chunk_end_line);
392 fe621944 2020-11-10 stsp if (rc)
393 fe621944 2020-11-10 stsp return rc;
394 b16ee069 2022-10-11 stsp
395 b16ee069 2022-10-11 stsp rc = diff_output_trailing_newline_msg(outinfo, dest,
396 b16ee069 2022-10-11 stsp &result->chunks.head[result->chunks.len - 1]);
397 b16ee069 2022-10-11 stsp if (rc != DIFF_RC_OK)
398 b16ee069 2022-10-11 stsp return rc;
399 fe621944 2020-11-10 stsp }
400 fe621944 2020-11-10 stsp
401 fe621944 2020-11-10 stsp return DIFF_RC_OK;
402 fe621944 2020-11-10 stsp }
403 fe621944 2020-11-10 stsp
404 fe621944 2020-11-10 stsp int
405 fe621944 2020-11-10 stsp diff_output_unidiff_chunk(struct diff_output_info **output_info, FILE *dest,
406 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *state,
407 fe621944 2020-11-10 stsp const struct diff_input_info *info,
408 fe621944 2020-11-10 stsp const struct diff_result *result,
409 fe621944 2020-11-10 stsp const struct diff_chunk_context *cc)
410 fe621944 2020-11-10 stsp {
411 fe621944 2020-11-10 stsp struct diff_output_info *outinfo = NULL;
412 fe621944 2020-11-10 stsp int flags = (result->left->root->diff_flags |
413 fe621944 2020-11-10 stsp result->right->root->diff_flags);
414 fe621944 2020-11-10 stsp bool show_function_prototypes = (flags & DIFF_FLAG_SHOW_PROTOTYPES);
415 fe621944 2020-11-10 stsp
416 fe621944 2020-11-10 stsp if (output_info) {
417 fe621944 2020-11-10 stsp *output_info = diff_output_info_alloc();
418 fe621944 2020-11-10 stsp if (*output_info == NULL)
419 fe621944 2020-11-10 stsp return ENOMEM;
420 fe621944 2020-11-10 stsp outinfo = *output_info;
421 fe621944 2020-11-10 stsp }
422 fe621944 2020-11-10 stsp
423 fe621944 2020-11-10 stsp return output_unidiff_chunk(outinfo, dest, state, info,
424 579042a9 2022-09-23 stsp result, false, show_function_prototypes, cc, 0);
425 fe621944 2020-11-10 stsp }
426 fe621944 2020-11-10 stsp
427 fe621944 2020-11-10 stsp int
428 fe621944 2020-11-10 stsp diff_output_unidiff(struct diff_output_info **output_info,
429 fe621944 2020-11-10 stsp FILE *dest, const struct diff_input_info *info,
430 fe621944 2020-11-10 stsp const struct diff_result *result,
431 fe621944 2020-11-10 stsp unsigned int context_lines)
432 fe621944 2020-11-10 stsp {
433 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *state;
434 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
435 fe621944 2020-11-10 stsp struct diff_output_info *outinfo = NULL;
436 b67f3bcb 2020-11-21 stsp int atomizer_flags = (result->left->atomizer_flags|
437 b67f3bcb 2020-11-21 stsp result->right->atomizer_flags);
438 fe621944 2020-11-10 stsp int flags = (result->left->root->diff_flags |
439 fe621944 2020-11-10 stsp result->right->root->diff_flags);
440 fe621944 2020-11-10 stsp bool show_function_prototypes = (flags & DIFF_FLAG_SHOW_PROTOTYPES);
441 b67f3bcb 2020-11-21 stsp bool force_text = (flags & DIFF_FLAG_FORCE_TEXT_DATA);
442 b67f3bcb 2020-11-21 stsp bool have_binary = (atomizer_flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
443 2471f5c4 2022-01-06 stsp off_t outoff = 0, *offp;
444 b77ebd68 2022-08-04 mark uint8_t *typep;
445 2471f5c4 2022-01-06 stsp int rc, i;
446 fe621944 2020-11-10 stsp
447 fe621944 2020-11-10 stsp if (!result)
448 fe621944 2020-11-10 stsp return EINVAL;
449 fe621944 2020-11-10 stsp if (result->rc != DIFF_RC_OK)
450 fe621944 2020-11-10 stsp return result->rc;
451 fe621944 2020-11-10 stsp
452 fe621944 2020-11-10 stsp if (output_info) {
453 fe621944 2020-11-10 stsp *output_info = diff_output_info_alloc();
454 fe621944 2020-11-10 stsp if (*output_info == NULL)
455 fe621944 2020-11-10 stsp return ENOMEM;
456 fe621944 2020-11-10 stsp outinfo = *output_info;
457 fe621944 2020-11-10 stsp }
458 b67f3bcb 2020-11-21 stsp
459 b67f3bcb 2020-11-21 stsp if (have_binary && !force_text) {
460 b67f3bcb 2020-11-21 stsp for (i = 0; i < result->chunks.len; i++) {
461 b67f3bcb 2020-11-21 stsp struct diff_chunk *c = &result->chunks.head[i];
462 b67f3bcb 2020-11-21 stsp enum diff_chunk_type t = diff_chunk_type(c);
463 fe621944 2020-11-10 stsp
464 b67f3bcb 2020-11-21 stsp if (t != CHUNK_MINUS && t != CHUNK_PLUS)
465 b67f3bcb 2020-11-21 stsp continue;
466 b67f3bcb 2020-11-21 stsp
467 2471f5c4 2022-01-06 stsp if (outinfo && outinfo->line_offsets.len > 0) {
468 2471f5c4 2022-01-06 stsp unsigned int idx =
469 2471f5c4 2022-01-06 stsp outinfo->line_offsets.len - 1;
470 2471f5c4 2022-01-06 stsp outoff = outinfo->line_offsets.head[idx];
471 2471f5c4 2022-01-06 stsp }
472 2471f5c4 2022-01-06 stsp
473 2471f5c4 2022-01-06 stsp rc = fprintf(dest, "Binary files %s and %s differ\n",
474 c4cd9c5b 2020-11-21 stsp diff_output_get_label_left(info),
475 c4cd9c5b 2020-11-21 stsp diff_output_get_label_right(info));
476 2471f5c4 2022-01-06 stsp if (outinfo) {
477 2471f5c4 2022-01-06 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
478 2471f5c4 2022-01-06 stsp if (offp == NULL)
479 2471f5c4 2022-01-06 stsp return ENOMEM;
480 2471f5c4 2022-01-06 stsp outoff += rc;
481 2471f5c4 2022-01-06 stsp *offp = outoff;
482 b77ebd68 2022-08-04 mark ARRAYLIST_ADD(typep, outinfo->line_types);
483 b77ebd68 2022-08-04 mark if (typep == NULL)
484 b77ebd68 2022-08-04 mark return ENOMEM;
485 b77ebd68 2022-08-04 mark *typep = DIFF_LINE_NONE;
486 2471f5c4 2022-01-06 stsp }
487 b67f3bcb 2020-11-21 stsp break;
488 b67f3bcb 2020-11-21 stsp }
489 b67f3bcb 2020-11-21 stsp
490 b67f3bcb 2020-11-21 stsp return DIFF_RC_OK;
491 b67f3bcb 2020-11-21 stsp }
492 b67f3bcb 2020-11-21 stsp
493 fe621944 2020-11-10 stsp state = diff_output_unidiff_state_alloc();
494 fe621944 2020-11-10 stsp if (state == NULL) {
495 fe621944 2020-11-10 stsp if (output_info) {
496 fe621944 2020-11-10 stsp diff_output_info_free(*output_info);
497 fe621944 2020-11-10 stsp *output_info = NULL;
498 fe621944 2020-11-10 stsp }
499 fe621944 2020-11-10 stsp return ENOMEM;
500 fe621944 2020-11-10 stsp }
501 fe621944 2020-11-10 stsp
502 fe621944 2020-11-10 stsp #if DEBUG
503 fe621944 2020-11-10 stsp unsigned int check_left_pos, check_right_pos;
504 fe621944 2020-11-10 stsp check_left_pos = 0;
505 fe621944 2020-11-10 stsp check_right_pos = 0;
506 fe621944 2020-11-10 stsp for (i = 0; i < result->chunks.len; i++) {
507 fe621944 2020-11-10 stsp struct diff_chunk *c = &result->chunks.head[i];
508 fe621944 2020-11-10 stsp enum diff_chunk_type t = diff_chunk_type(c);
509 fe621944 2020-11-10 stsp
510 fe621944 2020-11-10 stsp debug("[%d] %s lines L%d R%d @L %d @R %d\n",
511 fe621944 2020-11-10 stsp i, (t == CHUNK_MINUS ? "minus" :
512 fe621944 2020-11-10 stsp (t == CHUNK_PLUS ? "plus" :
513 fe621944 2020-11-10 stsp (t == CHUNK_SAME ? "same" : "?"))),
514 fe621944 2020-11-10 stsp c->left_count,
515 fe621944 2020-11-10 stsp c->right_count,
516 fe621944 2020-11-10 stsp c->left_start ? diff_atom_root_idx(result->left, c->left_start) : -1,
517 fe621944 2020-11-10 stsp c->right_start ? diff_atom_root_idx(result->right, c->right_start) : -1);
518 fe621944 2020-11-10 stsp assert(check_left_pos == diff_atom_root_idx(result->left, c->left_start));
519 fe621944 2020-11-10 stsp assert(check_right_pos == diff_atom_root_idx(result->right, c->right_start));
520 fe621944 2020-11-10 stsp check_left_pos += c->left_count;
521 fe621944 2020-11-10 stsp check_right_pos += c->right_count;
522 fe621944 2020-11-10 stsp
523 fe621944 2020-11-10 stsp }
524 fe621944 2020-11-10 stsp assert(check_left_pos == result->left->atoms.len);
525 fe621944 2020-11-10 stsp assert(check_right_pos == result->right->atoms.len);
526 fe621944 2020-11-10 stsp #endif
527 fe621944 2020-11-10 stsp
528 fe621944 2020-11-10 stsp for (i = 0; i < result->chunks.len; i++) {
529 fe621944 2020-11-10 stsp struct diff_chunk *c = &result->chunks.head[i];
530 fe621944 2020-11-10 stsp enum diff_chunk_type t = diff_chunk_type(c);
531 fe621944 2020-11-10 stsp struct diff_chunk_context next;
532 fe621944 2020-11-10 stsp
533 fe621944 2020-11-10 stsp if (t != CHUNK_MINUS && t != CHUNK_PLUS)
534 fe621944 2020-11-10 stsp continue;
535 fe621944 2020-11-10 stsp
536 fe621944 2020-11-10 stsp if (diff_chunk_context_empty(&cc)) {
537 fe621944 2020-11-10 stsp /* These are the first lines being printed.
538 fe621944 2020-11-10 stsp * Note down the start point, any number of subsequent
539 fe621944 2020-11-10 stsp * chunks may be joined up to this unidiff chunk by
540 fe621944 2020-11-10 stsp * context lines or by being directly adjacent. */
541 fe621944 2020-11-10 stsp diff_chunk_context_get(&cc, result, i, context_lines);
542 fe621944 2020-11-10 stsp debug("new chunk to be printed:"
543 fe621944 2020-11-10 stsp " chunk %d-%d left %d-%d right %d-%d\n",
544 fe621944 2020-11-10 stsp cc.chunk.start, cc.chunk.end,
545 fe621944 2020-11-10 stsp cc.left.start, cc.left.end,
546 fe621944 2020-11-10 stsp cc.right.start, cc.right.end);
547 fe621944 2020-11-10 stsp continue;
548 fe621944 2020-11-10 stsp }
549 fe621944 2020-11-10 stsp
550 fe621944 2020-11-10 stsp /* There already is a previous chunk noted down for being
551 fe621944 2020-11-10 stsp * printed. Does it join up with this one? */
552 fe621944 2020-11-10 stsp diff_chunk_context_get(&next, result, i, context_lines);
553 fe621944 2020-11-10 stsp debug("new chunk to be printed:"
554 fe621944 2020-11-10 stsp " chunk %d-%d left %d-%d right %d-%d\n",
555 fe621944 2020-11-10 stsp next.chunk.start, next.chunk.end,
556 fe621944 2020-11-10 stsp next.left.start, next.left.end,
557 fe621944 2020-11-10 stsp next.right.start, next.right.end);
558 fe621944 2020-11-10 stsp
559 fe621944 2020-11-10 stsp if (diff_chunk_contexts_touch(&cc, &next)) {
560 fe621944 2020-11-10 stsp /* This next context touches or overlaps the previous
561 fe621944 2020-11-10 stsp * one, join. */
562 fe621944 2020-11-10 stsp diff_chunk_contexts_merge(&cc, &next);
563 fe621944 2020-11-10 stsp debug("new chunk to be printed touches previous chunk,"
564 fe621944 2020-11-10 stsp " now: left %d-%d right %d-%d\n",
565 fe621944 2020-11-10 stsp cc.left.start, cc.left.end,
566 fe621944 2020-11-10 stsp cc.right.start, cc.right.end);
567 fe621944 2020-11-10 stsp continue;
568 fe621944 2020-11-10 stsp }
569 fe621944 2020-11-10 stsp
570 fe621944 2020-11-10 stsp /* No touching, so the previous context is complete with a gap
571 fe621944 2020-11-10 stsp * between it and this next one. Print the previous one and
572 fe621944 2020-11-10 stsp * start fresh here. */
573 fe621944 2020-11-10 stsp debug("new chunk to be printed does not touch previous chunk;"
574 fe621944 2020-11-10 stsp " print left %d-%d right %d-%d\n",
575 fe621944 2020-11-10 stsp cc.left.start, cc.left.end, cc.right.start, cc.right.end);
576 fe621944 2020-11-10 stsp output_unidiff_chunk(outinfo, dest, state, info, result,
577 579042a9 2022-09-23 stsp true, show_function_prototypes, &cc, context_lines);
578 fe621944 2020-11-10 stsp cc = next;
579 fe621944 2020-11-10 stsp debug("new unprinted chunk is left %d-%d right %d-%d\n",
580 fe621944 2020-11-10 stsp cc.left.start, cc.left.end, cc.right.start, cc.right.end);
581 fe621944 2020-11-10 stsp }
582 fe621944 2020-11-10 stsp
583 fe621944 2020-11-10 stsp if (!diff_chunk_context_empty(&cc))
584 fe621944 2020-11-10 stsp output_unidiff_chunk(outinfo, dest, state, info, result,
585 579042a9 2022-09-23 stsp true, show_function_prototypes, &cc, context_lines);
586 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(state);
587 fe621944 2020-11-10 stsp return DIFF_RC_OK;
588 fe621944 2020-11-10 stsp }