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