Blame


1 3b0f3d61 2020-01-22 neels /* Produce a unidiff output from a diff_result. */
2 3b0f3d61 2020-01-22 neels /*
3 3b0f3d61 2020-01-22 neels * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
4 3b0f3d61 2020-01-22 neels *
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.
8 3b0f3d61 2020-01-22 neels *
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.
16 3b0f3d61 2020-01-22 neels */
17 3b0f3d61 2020-01-22 neels
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>
25 e10a628a 2020-09-16 stsp
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>
29 3b0f3d61 2020-01-22 neels
30 85ab4559 2020-09-22 stsp #include "diff_internal.h"
31 2a1b94d0 2020-09-26 stsp #include "diff_debug.h"
32 f26db7cd 2023-02-20 mark
33 f26db7cd 2023-02-20 mark off_t
34 f26db7cd 2023-02-20 mark diff_chunk_get_left_start_pos(const struct diff_chunk *c)
35 f26db7cd 2023-02-20 mark {
36 f26db7cd 2023-02-20 mark return c->left_start->pos;
37 f26db7cd 2023-02-20 mark }
38 f26db7cd 2023-02-20 mark
39 f26db7cd 2023-02-20 mark off_t
40 f26db7cd 2023-02-20 mark diff_chunk_get_right_start_pos(const struct diff_chunk *c)
41 f26db7cd 2023-02-20 mark {
42 f26db7cd 2023-02-20 mark return c->right_start->pos;
43 f26db7cd 2023-02-20 mark }
44 3b0f3d61 2020-01-22 neels
45 2f26640c 2020-10-17 stsp bool
46 cbf93b70 2020-10-16 stsp diff_chunk_context_empty(const struct diff_chunk_context *cc)
47 3b0f3d61 2020-01-22 neels {
48 d362ea2e 2020-07-25 stsp return diff_range_empty(&cc->chunk);
49 3b0f3d61 2020-01-22 neels }
50 3b0f3d61 2020-01-22 neels
51 fe8af0d6 2020-10-06 stsp int
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)
54 fe8af0d6 2020-10-06 stsp {
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);
57 fe8af0d6 2020-10-06 stsp }
58 fe8af0d6 2020-10-06 stsp
59 fe8af0d6 2020-10-06 stsp int
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)
62 fe8af0d6 2020-10-06 stsp {
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);
66 fe8af0d6 2020-10-06 stsp }
67 fe8af0d6 2020-10-06 stsp
68 fe8af0d6 2020-10-06 stsp int
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)
71 fe8af0d6 2020-10-06 stsp {
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);
74 fe8af0d6 2020-10-06 stsp }
75 fe8af0d6 2020-10-06 stsp
76 fe8af0d6 2020-10-06 stsp int
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)
79 fe8af0d6 2020-10-06 stsp {
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);
83 e8eedebc 2020-11-06 stsp }
84 e8eedebc 2020-11-06 stsp
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)
87 e8eedebc 2020-11-06 stsp {
88 e8eedebc 2020-11-06 stsp return &r->chunks.head[chunk_idx];
89 e8eedebc 2020-11-06 stsp }
90 e8eedebc 2020-11-06 stsp
91 e8eedebc 2020-11-06 stsp int
92 e8eedebc 2020-11-06 stsp diff_chunk_get_left_count(struct diff_chunk *c)
93 e8eedebc 2020-11-06 stsp {
94 e8eedebc 2020-11-06 stsp return c->left_count;
95 e8eedebc 2020-11-06 stsp }
96 e8eedebc 2020-11-06 stsp
97 e8eedebc 2020-11-06 stsp int
98 e8eedebc 2020-11-06 stsp diff_chunk_get_right_count(struct diff_chunk *c)
99 e8eedebc 2020-11-06 stsp {
100 e8eedebc 2020-11-06 stsp return c->right_count;
101 fe8af0d6 2020-10-06 stsp }
102 fe8af0d6 2020-10-06 stsp
103 f374e913 2020-09-22 stsp void
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)
106 3b0f3d61 2020-01-22 neels {
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);
112 0d27172a 2020-05-06 neels
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,
117 3b0f3d61 2020-01-22 neels },
118 3b0f3d61 2020-01-22 neels .left = {
119 0d27172a 2020-05-06 neels .start = left_start,
120 0d27172a 2020-05-06 neels .end = left_end,
121 3b0f3d61 2020-01-22 neels },
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,
125 3b0f3d61 2020-01-22 neels },
126 3b0f3d61 2020-01-22 neels };
127 3b0f3d61 2020-01-22 neels }
128 3b0f3d61 2020-01-22 neels
129 2f26640c 2020-10-17 stsp bool
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)
132 3b0f3d61 2020-01-22 neels {
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);
136 3b0f3d61 2020-01-22 neels }
137 3b0f3d61 2020-01-22 neels
138 26595c7d 2020-10-15 stsp void
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)
141 3b0f3d61 2020-01-22 neels {
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);
145 7187fe97 2020-10-17 stsp }
146 7187fe97 2020-10-17 stsp
147 7187fe97 2020-10-17 stsp void
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)
153 7187fe97 2020-10-17 stsp {
154 7187fe97 2020-10-17 stsp int i;
155 7187fe97 2020-10-17 stsp int seen_minus = 0, seen_plus = 0;
156 7187fe97 2020-10-17 stsp
157 7187fe97 2020-10-17 stsp if (nchunks_used)
158 7187fe97 2020-10-17 stsp *nchunks_used = 0;
159 7187fe97 2020-10-17 stsp
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;
164 7187fe97 2020-10-17 stsp
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)
169 7187fe97 2020-10-17 stsp break;
170 7187fe97 2020-10-17 stsp else
171 7187fe97 2020-10-17 stsp continue;
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;
176 7187fe97 2020-10-17 stsp
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)++;
184 7187fe97 2020-10-17 stsp continue;
185 7187fe97 2020-10-17 stsp }
186 7187fe97 2020-10-17 stsp
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);
190 7187fe97 2020-10-17 stsp
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)++;
197 7187fe97 2020-10-17 stsp continue;
198 7187fe97 2020-10-17 stsp } else
199 7187fe97 2020-10-17 stsp break;
200 7187fe97 2020-10-17 stsp }
201 3b0f3d61 2020-01-22 neels }
202 3b0f3d61 2020-01-22 neels
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;
207 11caa5cc 2020-09-22 stsp };
208 11caa5cc 2020-09-22 stsp
209 11caa5cc 2020-09-22 stsp struct diff_output_unidiff_state *
210 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_alloc(void)
211 11caa5cc 2020-09-22 stsp {
212 11caa5cc 2020-09-22 stsp struct diff_output_unidiff_state *state;
213 11caa5cc 2020-09-22 stsp
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;
218 11caa5cc 2020-09-22 stsp }
219 11caa5cc 2020-09-22 stsp
220 f374e913 2020-09-22 stsp void
221 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_reset(struct diff_output_unidiff_state *state)
222 11caa5cc 2020-09-22 stsp {
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;
226 11caa5cc 2020-09-22 stsp }
227 11caa5cc 2020-09-22 stsp
228 11caa5cc 2020-09-22 stsp void
229 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_free(struct diff_output_unidiff_state *state)
230 11caa5cc 2020-09-22 stsp {
231 11caa5cc 2020-09-22 stsp free(state);
232 11caa5cc 2020-09-22 stsp }
233 11caa5cc 2020-09-22 stsp
234 2c20a3ed 2020-09-22 stsp static int
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)
241 3b0f3d61 2020-01-22 neels {
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;
245 2c20a3ed 2020-09-22 stsp
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;
248 3b0f3d61 2020-01-22 neels
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];
252 2c20a3ed 2020-09-22 stsp }
253 2c20a3ed 2020-09-22 stsp
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;
269 2c20a3ed 2020-09-22 stsp }
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;
284 2c20a3ed 2020-09-22 stsp }
285 11caa5cc 2020-09-22 stsp state->header_printed = true;
286 3b0f3d61 2020-01-22 neels }
287 3b0f3d61 2020-01-22 neels
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;
293 b6adedb6 2020-10-07 stsp else
294 b6adedb6 2020-10-07 stsp left_start = cc->left.start + 1;
295 b6adedb6 2020-10-07 stsp
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;
301 b6adedb6 2020-10-07 stsp else
302 b6adedb6 2020-10-07 stsp right_start = cc->right.start + 1;
303 13e2caa3 2020-10-17 stsp
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);
308 13e2caa3 2020-10-17 stsp if (rc)
309 13e2caa3 2020-10-17 stsp return rc;
310 13e2caa3 2020-10-17 stsp }
311 b6adedb6 2020-10-07 stsp
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 : "");
327 b6adedb6 2020-10-07 stsp } else {
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 : "");
332 b6adedb6 2020-10-07 stsp }
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;
345 2c20a3ed 2020-09-22 stsp }
346 2c20a3ed 2020-09-22 stsp
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);
362 2c20a3ed 2020-09-22 stsp if (rc)
363 2c20a3ed 2020-09-22 stsp return rc;
364 2c20a3ed 2020-09-22 stsp }
365 3b0f3d61 2020-01-22 neels
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];
370 3b0f3d61 2020-01-22 neels
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);
383 2c20a3ed 2020-09-22 stsp if (rc)
384 2c20a3ed 2020-09-22 stsp return rc;
385 7021523c 2020-10-16 stsp
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)
389 7021523c 2020-10-16 stsp return rc;
390 7021523c 2020-10-16 stsp }
391 3b0f3d61 2020-01-22 neels }
392 3b0f3d61 2020-01-22 neels
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);
404 2c20a3ed 2020-09-22 stsp if (rc)
405 2c20a3ed 2020-09-22 stsp return rc;
406 4125b1ef 2022-10-10 tj
407 b5a9c15f 2023-09-15 stsp if (cc->left.end == result->left->atoms.len) {
408 b5a9c15f 2023-09-15 stsp rc = diff_output_trailing_newline_msg(outinfo, dest,
409 b5a9c15f 2023-09-15 stsp &result->chunks.head[result->chunks.len - 1]);
410 b5a9c15f 2023-09-15 stsp if (rc != DIFF_RC_OK)
411 b5a9c15f 2023-09-15 stsp return rc;
412 b5a9c15f 2023-09-15 stsp }
413 2c20a3ed 2020-09-22 stsp }
414 2c20a3ed 2020-09-22 stsp
415 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
416 3b0f3d61 2020-01-22 neels }
417 3b0f3d61 2020-01-22 neels
418 3e6cba3a 2020-08-13 stsp int
419 2c20a3ed 2020-09-22 stsp diff_output_unidiff_chunk(struct diff_output_info **output_info, FILE *dest,
420 2c20a3ed 2020-09-22 stsp struct diff_output_unidiff_state *state,
421 2c20a3ed 2020-09-22 stsp const struct diff_input_info *info,
422 2c20a3ed 2020-09-22 stsp const struct diff_result *result,
423 2c20a3ed 2020-09-22 stsp const struct diff_chunk_context *cc)
424 2c20a3ed 2020-09-22 stsp {
425 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
426 c16dde50 2020-10-22 stsp int flags = (result->left->root->diff_flags |
427 c16dde50 2020-10-22 stsp result->right->root->diff_flags);
428 13e2caa3 2020-10-17 stsp bool show_function_prototypes = (flags & DIFF_FLAG_SHOW_PROTOTYPES);
429 2c20a3ed 2020-09-22 stsp
430 2c20a3ed 2020-09-22 stsp if (output_info) {
431 2c20a3ed 2020-09-22 stsp *output_info = diff_output_info_alloc();
432 2c20a3ed 2020-09-22 stsp if (*output_info == NULL)
433 2c20a3ed 2020-09-22 stsp return ENOMEM;
434 2c20a3ed 2020-09-22 stsp outinfo = *output_info;
435 2c20a3ed 2020-09-22 stsp }
436 2c20a3ed 2020-09-22 stsp
437 2c20a3ed 2020-09-22 stsp return output_unidiff_chunk(outinfo, dest, state, info,
438 dc306c6b 2023-08-29 stsp result, false, show_function_prototypes, cc);
439 2c20a3ed 2020-09-22 stsp }
440 2c20a3ed 2020-09-22 stsp
441 2c20a3ed 2020-09-22 stsp int
442 2c20a3ed 2020-09-22 stsp diff_output_unidiff(struct diff_output_info **output_info,
443 2c20a3ed 2020-09-22 stsp FILE *dest, const struct diff_input_info *info,
444 0d27172a 2020-05-06 neels const struct diff_result *result,
445 0d27172a 2020-05-06 neels unsigned int context_lines)
446 3b0f3d61 2020-01-22 neels {
447 11caa5cc 2020-09-22 stsp struct diff_output_unidiff_state *state;
448 11caa5cc 2020-09-22 stsp struct diff_chunk_context cc = {};
449 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
450 e51ebd83 2020-11-21 stsp int atomizer_flags = (result->left->atomizer_flags|
451 e51ebd83 2020-11-21 stsp result->right->atomizer_flags);
452 c16dde50 2020-10-22 stsp int flags = (result->left->root->diff_flags |
453 c16dde50 2020-10-22 stsp result->right->root->diff_flags);
454 13e2caa3 2020-10-17 stsp bool show_function_prototypes = (flags & DIFF_FLAG_SHOW_PROTOTYPES);
455 e51ebd83 2020-11-21 stsp bool force_text = (flags & DIFF_FLAG_FORCE_TEXT_DATA);
456 e51ebd83 2020-11-21 stsp bool have_binary = (atomizer_flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
457 b72f51ff 2022-01-06 stsp off_t outoff = 0, *offp;
458 9343b925 2022-08-04 mark uint8_t *typep;
459 b72f51ff 2022-01-06 stsp int rc, i;
460 11caa5cc 2020-09-22 stsp
461 3b0f3d61 2020-01-22 neels if (!result)
462 3e6cba3a 2020-08-13 stsp return EINVAL;
463 3b0f3d61 2020-01-22 neels if (result->rc != DIFF_RC_OK)
464 3b0f3d61 2020-01-22 neels return result->rc;
465 3b0f3d61 2020-01-22 neels
466 2c20a3ed 2020-09-22 stsp if (output_info) {
467 2c20a3ed 2020-09-22 stsp *output_info = diff_output_info_alloc();
468 2c20a3ed 2020-09-22 stsp if (*output_info == NULL)
469 2c20a3ed 2020-09-22 stsp return ENOMEM;
470 2c20a3ed 2020-09-22 stsp outinfo = *output_info;
471 2c20a3ed 2020-09-22 stsp }
472 e51ebd83 2020-11-21 stsp
473 e51ebd83 2020-11-21 stsp if (have_binary && !force_text) {
474 e51ebd83 2020-11-21 stsp for (i = 0; i < result->chunks.len; i++) {
475 e51ebd83 2020-11-21 stsp struct diff_chunk *c = &result->chunks.head[i];
476 e51ebd83 2020-11-21 stsp enum diff_chunk_type t = diff_chunk_type(c);
477 2c20a3ed 2020-09-22 stsp
478 e51ebd83 2020-11-21 stsp if (t != CHUNK_MINUS && t != CHUNK_PLUS)
479 e51ebd83 2020-11-21 stsp continue;
480 e51ebd83 2020-11-21 stsp
481 b72f51ff 2022-01-06 stsp if (outinfo && outinfo->line_offsets.len > 0) {
482 b72f51ff 2022-01-06 stsp unsigned int idx =
483 b72f51ff 2022-01-06 stsp outinfo->line_offsets.len - 1;
484 b72f51ff 2022-01-06 stsp outoff = outinfo->line_offsets.head[idx];
485 b72f51ff 2022-01-06 stsp }
486 b72f51ff 2022-01-06 stsp
487 b72f51ff 2022-01-06 stsp rc = fprintf(dest, "Binary files %s and %s differ\n",
488 e4c510c1 2020-11-21 stsp diff_output_get_label_left(info),
489 e4c510c1 2020-11-21 stsp diff_output_get_label_right(info));
490 b72f51ff 2022-01-06 stsp if (outinfo) {
491 b72f51ff 2022-01-06 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
492 b72f51ff 2022-01-06 stsp if (offp == NULL)
493 b72f51ff 2022-01-06 stsp return ENOMEM;
494 b72f51ff 2022-01-06 stsp outoff += rc;
495 b72f51ff 2022-01-06 stsp *offp = outoff;
496 9343b925 2022-08-04 mark ARRAYLIST_ADD(typep, outinfo->line_types);
497 9343b925 2022-08-04 mark if (typep == NULL)
498 9343b925 2022-08-04 mark return ENOMEM;
499 9343b925 2022-08-04 mark *typep = DIFF_LINE_NONE;
500 b72f51ff 2022-01-06 stsp }
501 e51ebd83 2020-11-21 stsp break;
502 e51ebd83 2020-11-21 stsp }
503 e51ebd83 2020-11-21 stsp
504 e51ebd83 2020-11-21 stsp return DIFF_RC_OK;
505 e51ebd83 2020-11-21 stsp }
506 e51ebd83 2020-11-21 stsp
507 11caa5cc 2020-09-22 stsp state = diff_output_unidiff_state_alloc();
508 2c20a3ed 2020-09-22 stsp if (state == NULL) {
509 2c20a3ed 2020-09-22 stsp if (output_info) {
510 2c20a3ed 2020-09-22 stsp diff_output_info_free(*output_info);
511 2c20a3ed 2020-09-22 stsp *output_info = NULL;
512 2c20a3ed 2020-09-22 stsp }
513 11caa5cc 2020-09-22 stsp return ENOMEM;
514 2c20a3ed 2020-09-22 stsp }
515 3b0f3d61 2020-01-22 neels
516 5ff75996 2020-10-11 neels #if DEBUG
517 e638575c 2020-11-06 neels unsigned int check_left_pos, check_right_pos;
518 4861c9da 2020-10-30 neels check_left_pos = 0;
519 4861c9da 2020-10-30 neels check_right_pos = 0;
520 5ff75996 2020-10-11 neels for (i = 0; i < result->chunks.len; i++) {
521 5ff75996 2020-10-11 neels struct diff_chunk *c = &result->chunks.head[i];
522 5ff75996 2020-10-11 neels enum diff_chunk_type t = diff_chunk_type(c);
523 2c20a3ed 2020-09-22 stsp
524 5ff75996 2020-10-11 neels debug("[%d] %s lines L%d R%d @L %d @R %d\n",
525 5ff75996 2020-10-11 neels i, (t == CHUNK_MINUS ? "minus" :
526 5ff75996 2020-10-11 neels (t == CHUNK_PLUS ? "plus" :
527 5ff75996 2020-10-11 neels (t == CHUNK_SAME ? "same" : "?"))),
528 5ff75996 2020-10-11 neels c->left_count,
529 5ff75996 2020-10-11 neels c->right_count,
530 9403a358 2020-10-24 neels c->left_start ? diff_atom_root_idx(result->left, c->left_start) : -1,
531 9403a358 2020-10-24 neels c->right_start ? diff_atom_root_idx(result->right, c->right_start) : -1);
532 4861c9da 2020-10-30 neels assert(check_left_pos == diff_atom_root_idx(result->left, c->left_start));
533 4861c9da 2020-10-30 neels assert(check_right_pos == diff_atom_root_idx(result->right, c->right_start));
534 4861c9da 2020-10-30 neels check_left_pos += c->left_count;
535 4861c9da 2020-10-30 neels check_right_pos += c->right_count;
536 4861c9da 2020-10-30 neels
537 5ff75996 2020-10-11 neels }
538 4861c9da 2020-10-30 neels assert(check_left_pos == result->left->atoms.len);
539 4861c9da 2020-10-30 neels assert(check_right_pos == result->right->atoms.len);
540 5ff75996 2020-10-11 neels #endif
541 5ff75996 2020-10-11 neels
542 3b0f3d61 2020-01-22 neels for (i = 0; i < result->chunks.len; i++) {
543 3b0f3d61 2020-01-22 neels struct diff_chunk *c = &result->chunks.head[i];
544 8546b045 2020-09-20 neels enum diff_chunk_type t = diff_chunk_type(c);
545 f374e913 2020-09-22 stsp struct diff_chunk_context next;
546 3b0f3d61 2020-01-22 neels
547 9cc49695 2020-05-05 neels if (t != CHUNK_MINUS && t != CHUNK_PLUS)
548 9cc49695 2020-05-05 neels continue;
549 9cc49695 2020-05-05 neels
550 cbf93b70 2020-10-16 stsp if (diff_chunk_context_empty(&cc)) {
551 9cc49695 2020-05-05 neels /* These are the first lines being printed.
552 0d27172a 2020-05-06 neels * Note down the start point, any number of subsequent
553 0d27172a 2020-05-06 neels * chunks may be joined up to this unidiff chunk by
554 0d27172a 2020-05-06 neels * context lines or by being directly adjacent. */
555 f374e913 2020-09-22 stsp diff_chunk_context_get(&cc, result, i, context_lines);
556 0d27172a 2020-05-06 neels debug("new chunk to be printed:"
557 0d27172a 2020-05-06 neels " chunk %d-%d left %d-%d right %d-%d\n",
558 9cc49695 2020-05-05 neels cc.chunk.start, cc.chunk.end,
559 9cc49695 2020-05-05 neels cc.left.start, cc.left.end,
560 9cc49695 2020-05-05 neels cc.right.start, cc.right.end);
561 9cc49695 2020-05-05 neels continue;
562 3b0f3d61 2020-01-22 neels }
563 3b0f3d61 2020-01-22 neels
564 0d27172a 2020-05-06 neels /* There already is a previous chunk noted down for being
565 0d27172a 2020-05-06 neels * printed. Does it join up with this one? */
566 f374e913 2020-09-22 stsp diff_chunk_context_get(&next, result, i, context_lines);
567 0d27172a 2020-05-06 neels debug("new chunk to be printed:"
568 0d27172a 2020-05-06 neels " chunk %d-%d left %d-%d right %d-%d\n",
569 9cc49695 2020-05-05 neels next.chunk.start, next.chunk.end,
570 9cc49695 2020-05-05 neels next.left.start, next.left.end,
571 9cc49695 2020-05-05 neels next.right.start, next.right.end);
572 9cc49695 2020-05-05 neels
573 26595c7d 2020-10-15 stsp if (diff_chunk_contexts_touch(&cc, &next)) {
574 0d27172a 2020-05-06 neels /* This next context touches or overlaps the previous
575 0d27172a 2020-05-06 neels * one, join. */
576 26595c7d 2020-10-15 stsp diff_chunk_contexts_merge(&cc, &next);
577 0d27172a 2020-05-06 neels debug("new chunk to be printed touches previous chunk,"
578 0d27172a 2020-05-06 neels " now: left %d-%d right %d-%d\n",
579 9cc49695 2020-05-05 neels cc.left.start, cc.left.end,
580 9cc49695 2020-05-05 neels cc.right.start, cc.right.end);
581 9cc49695 2020-05-05 neels continue;
582 9cc49695 2020-05-05 neels }
583 9cc49695 2020-05-05 neels
584 0d27172a 2020-05-06 neels /* No touching, so the previous context is complete with a gap
585 0d27172a 2020-05-06 neels * between it and this next one. Print the previous one and
586 0d27172a 2020-05-06 neels * start fresh here. */
587 0d27172a 2020-05-06 neels debug("new chunk to be printed does not touch previous chunk;"
588 0d27172a 2020-05-06 neels " print left %d-%d right %d-%d\n",
589 9cc49695 2020-05-05 neels cc.left.start, cc.left.end, cc.right.start, cc.right.end);
590 66ea8e5a 2020-10-17 stsp output_unidiff_chunk(outinfo, dest, state, info, result,
591 dc306c6b 2023-08-29 stsp true, show_function_prototypes, &cc);
592 9cc49695 2020-05-05 neels cc = next;
593 9cc49695 2020-05-05 neels debug("new unprinted chunk is left %d-%d right %d-%d\n",
594 9cc49695 2020-05-05 neels cc.left.start, cc.left.end, cc.right.start, cc.right.end);
595 3b0f3d61 2020-01-22 neels }
596 3b0f3d61 2020-01-22 neels
597 cbf93b70 2020-10-16 stsp if (!diff_chunk_context_empty(&cc))
598 66ea8e5a 2020-10-17 stsp output_unidiff_chunk(outinfo, dest, state, info, result,
599 dc306c6b 2023-08-29 stsp true, show_function_prototypes, &cc);
600 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_free(state);
601 3b0f3d61 2020-01-22 neels return DIFF_RC_OK;
602 3b0f3d61 2020-01-22 neels }