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 <inttypes.h>
20 e10a628a 2020-09-16 stsp #include <stdbool.h>
21 e10a628a 2020-09-16 stsp #include <stdio.h>
22 e10a628a 2020-09-16 stsp #include <stdlib.h>
23 e10a628a 2020-09-16 stsp
24 1dfba055 2020-10-07 stsp #include <arraylist.h>
25 1dfba055 2020-10-07 stsp #include <diff_main.h>
26 1dfba055 2020-10-07 stsp #include <diff_output.h>
27 3b0f3d61 2020-01-22 neels
28 85ab4559 2020-09-22 stsp #include "diff_internal.h"
29 2a1b94d0 2020-09-26 stsp #include "diff_debug.h"
30 3b0f3d61 2020-01-22 neels
31 cbf93b70 2020-10-16 stsp bool
32 cbf93b70 2020-10-16 stsp diff_chunk_context_empty(const struct diff_chunk_context *cc)
33 3b0f3d61 2020-01-22 neels {
34 d362ea2e 2020-07-25 stsp return diff_range_empty(&cc->chunk);
35 3b0f3d61 2020-01-22 neels }
36 3b0f3d61 2020-01-22 neels
37 fe8af0d6 2020-10-06 stsp int
38 fe8af0d6 2020-10-06 stsp diff_chunk_get_left_start(const struct diff_chunk *c,
39 fe8af0d6 2020-10-06 stsp const struct diff_result *r, int context_lines)
40 fe8af0d6 2020-10-06 stsp {
41 fe8af0d6 2020-10-06 stsp int left_start = diff_atom_root_idx(&r->left, c->left_start);
42 fe8af0d6 2020-10-06 stsp return MAX(0, left_start - context_lines);
43 fe8af0d6 2020-10-06 stsp }
44 fe8af0d6 2020-10-06 stsp
45 fe8af0d6 2020-10-06 stsp int
46 fe8af0d6 2020-10-06 stsp diff_chunk_get_left_end(const struct diff_chunk *c,
47 fe8af0d6 2020-10-06 stsp const struct diff_result *r, int context_lines)
48 fe8af0d6 2020-10-06 stsp {
49 fe8af0d6 2020-10-06 stsp int left_start = diff_chunk_get_left_start(c, r, 0);
50 fe8af0d6 2020-10-06 stsp return MIN(r->left.atoms.len,
51 fe8af0d6 2020-10-06 stsp left_start + c->left_count + context_lines);
52 fe8af0d6 2020-10-06 stsp }
53 fe8af0d6 2020-10-06 stsp
54 fe8af0d6 2020-10-06 stsp int
55 fe8af0d6 2020-10-06 stsp diff_chunk_get_right_start(const struct diff_chunk *c,
56 fe8af0d6 2020-10-06 stsp const struct diff_result *r, int context_lines)
57 fe8af0d6 2020-10-06 stsp {
58 fe8af0d6 2020-10-06 stsp int right_start = diff_atom_root_idx(&r->right, c->right_start);
59 fe8af0d6 2020-10-06 stsp return MAX(0, right_start - context_lines);
60 fe8af0d6 2020-10-06 stsp }
61 fe8af0d6 2020-10-06 stsp
62 fe8af0d6 2020-10-06 stsp int
63 fe8af0d6 2020-10-06 stsp diff_chunk_get_right_end(const struct diff_chunk *c,
64 fe8af0d6 2020-10-06 stsp const struct diff_result *r, int context_lines)
65 fe8af0d6 2020-10-06 stsp {
66 fe8af0d6 2020-10-06 stsp int right_start = diff_chunk_get_right_start(c, r, 0);
67 fe8af0d6 2020-10-06 stsp return MIN(r->right.atoms.len,
68 fe8af0d6 2020-10-06 stsp right_start + c->right_count + context_lines);
69 fe8af0d6 2020-10-06 stsp }
70 fe8af0d6 2020-10-06 stsp
71 f374e913 2020-09-22 stsp void
72 f374e913 2020-09-22 stsp diff_chunk_context_get(struct diff_chunk_context *cc, const struct diff_result *r,
73 0d27172a 2020-05-06 neels int chunk_idx, int context_lines)
74 3b0f3d61 2020-01-22 neels {
75 3b0f3d61 2020-01-22 neels const struct diff_chunk *c = &r->chunks.head[chunk_idx];
76 fe8af0d6 2020-10-06 stsp int left_start = diff_chunk_get_left_start(c, r, context_lines);
77 fe8af0d6 2020-10-06 stsp int left_end = diff_chunk_get_left_end(c, r, context_lines);
78 fe8af0d6 2020-10-06 stsp int right_start = diff_chunk_get_right_start(c, r, context_lines);
79 fe8af0d6 2020-10-06 stsp int right_end = diff_chunk_get_right_end(c, r, context_lines);
80 0d27172a 2020-05-06 neels
81 f374e913 2020-09-22 stsp *cc = (struct diff_chunk_context){
82 3b0f3d61 2020-01-22 neels .chunk = {
83 3b0f3d61 2020-01-22 neels .start = chunk_idx,
84 3b0f3d61 2020-01-22 neels .end = chunk_idx + 1,
85 3b0f3d61 2020-01-22 neels },
86 3b0f3d61 2020-01-22 neels .left = {
87 0d27172a 2020-05-06 neels .start = left_start,
88 0d27172a 2020-05-06 neels .end = left_end,
89 3b0f3d61 2020-01-22 neels },
90 3b0f3d61 2020-01-22 neels .right = {
91 0d27172a 2020-05-06 neels .start = right_start,
92 0d27172a 2020-05-06 neels .end = right_end,
93 3b0f3d61 2020-01-22 neels },
94 3b0f3d61 2020-01-22 neels };
95 3b0f3d61 2020-01-22 neels }
96 3b0f3d61 2020-01-22 neels
97 26595c7d 2020-10-15 stsp bool
98 26595c7d 2020-10-15 stsp diff_chunk_contexts_touch(const struct diff_chunk_context *cc,
99 26595c7d 2020-10-15 stsp const struct diff_chunk_context *other)
100 3b0f3d61 2020-01-22 neels {
101 d362ea2e 2020-07-25 stsp return diff_ranges_touch(&cc->chunk, &other->chunk)
102 d362ea2e 2020-07-25 stsp || diff_ranges_touch(&cc->left, &other->left)
103 d362ea2e 2020-07-25 stsp || diff_ranges_touch(&cc->right, &other->right);
104 3b0f3d61 2020-01-22 neels }
105 3b0f3d61 2020-01-22 neels
106 26595c7d 2020-10-15 stsp void
107 26595c7d 2020-10-15 stsp diff_chunk_contexts_merge(struct diff_chunk_context *cc,
108 26595c7d 2020-10-15 stsp const struct diff_chunk_context *other)
109 3b0f3d61 2020-01-22 neels {
110 d362ea2e 2020-07-25 stsp diff_ranges_merge(&cc->chunk, &other->chunk);
111 d362ea2e 2020-07-25 stsp diff_ranges_merge(&cc->left, &other->left);
112 d362ea2e 2020-07-25 stsp diff_ranges_merge(&cc->right, &other->right);
113 3b0f3d61 2020-01-22 neels }
114 3b0f3d61 2020-01-22 neels
115 11caa5cc 2020-09-22 stsp struct diff_output_unidiff_state {
116 11caa5cc 2020-09-22 stsp bool header_printed;
117 11caa5cc 2020-09-22 stsp };
118 11caa5cc 2020-09-22 stsp
119 11caa5cc 2020-09-22 stsp struct diff_output_unidiff_state *
120 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_alloc(void)
121 11caa5cc 2020-09-22 stsp {
122 11caa5cc 2020-09-22 stsp struct diff_output_unidiff_state *state;
123 11caa5cc 2020-09-22 stsp
124 11caa5cc 2020-09-22 stsp state = calloc(1, sizeof(struct diff_output_unidiff_state));
125 11caa5cc 2020-09-22 stsp if (state != NULL)
126 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_reset(state);
127 11caa5cc 2020-09-22 stsp return state;
128 11caa5cc 2020-09-22 stsp }
129 11caa5cc 2020-09-22 stsp
130 f374e913 2020-09-22 stsp void
131 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_reset(struct diff_output_unidiff_state *state)
132 11caa5cc 2020-09-22 stsp {
133 11caa5cc 2020-09-22 stsp state->header_printed = false;
134 11caa5cc 2020-09-22 stsp }
135 11caa5cc 2020-09-22 stsp
136 11caa5cc 2020-09-22 stsp void
137 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_free(struct diff_output_unidiff_state *state)
138 11caa5cc 2020-09-22 stsp {
139 11caa5cc 2020-09-22 stsp free(state);
140 11caa5cc 2020-09-22 stsp }
141 11caa5cc 2020-09-22 stsp
142 2c20a3ed 2020-09-22 stsp static int
143 2c20a3ed 2020-09-22 stsp output_unidiff_chunk(struct diff_output_info *outinfo, FILE *dest,
144 2c20a3ed 2020-09-22 stsp struct diff_output_unidiff_state *state,
145 2c20a3ed 2020-09-22 stsp const struct diff_input_info *info,
146 2c20a3ed 2020-09-22 stsp const struct diff_result *result,
147 2c20a3ed 2020-09-22 stsp const struct diff_chunk_context *cc)
148 3b0f3d61 2020-01-22 neels {
149 b6adedb6 2020-10-07 stsp int rc, left_start, left_len, right_start, right_len;
150 ab528e22 2020-09-22 stsp off_t outoff = 0, *offp;
151 2c20a3ed 2020-09-22 stsp
152 d362ea2e 2020-07-25 stsp if (diff_range_empty(&cc->left) && diff_range_empty(&cc->right))
153 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
154 3b0f3d61 2020-01-22 neels
155 2c20a3ed 2020-09-22 stsp if (outinfo && outinfo->line_offsets.len > 0) {
156 2c20a3ed 2020-09-22 stsp unsigned int idx = outinfo->line_offsets.len - 1;
157 2c20a3ed 2020-09-22 stsp outoff = outinfo->line_offsets.head[idx];
158 2c20a3ed 2020-09-22 stsp }
159 2c20a3ed 2020-09-22 stsp
160 11caa5cc 2020-09-22 stsp if (!(state->header_printed)) {
161 2c20a3ed 2020-09-22 stsp rc = fprintf(dest, "--- %s\n", info->left_path ? : "a");
162 2c20a3ed 2020-09-22 stsp if (rc < 0)
163 2c20a3ed 2020-09-22 stsp return errno;
164 2c20a3ed 2020-09-22 stsp if (outinfo) {
165 2c20a3ed 2020-09-22 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
166 dabc1008 2020-09-22 stsp if (offp == NULL)
167 dabc1008 2020-09-22 stsp return ENOMEM;
168 2c20a3ed 2020-09-22 stsp outoff += rc;
169 2c20a3ed 2020-09-22 stsp *offp = outoff;
170 2c20a3ed 2020-09-22 stsp
171 2c20a3ed 2020-09-22 stsp }
172 2c20a3ed 2020-09-22 stsp rc = fprintf(dest, "+++ %s\n", info->right_path ? : "b");
173 2c20a3ed 2020-09-22 stsp if (rc < 0)
174 2c20a3ed 2020-09-22 stsp return errno;
175 2c20a3ed 2020-09-22 stsp if (outinfo) {
176 2c20a3ed 2020-09-22 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
177 dabc1008 2020-09-22 stsp if (offp == NULL)
178 dabc1008 2020-09-22 stsp return ENOMEM;
179 2c20a3ed 2020-09-22 stsp outoff += rc;
180 2c20a3ed 2020-09-22 stsp *offp = outoff;
181 2c20a3ed 2020-09-22 stsp
182 2c20a3ed 2020-09-22 stsp }
183 11caa5cc 2020-09-22 stsp state->header_printed = true;
184 3b0f3d61 2020-01-22 neels }
185 3b0f3d61 2020-01-22 neels
186 b6adedb6 2020-10-07 stsp left_len = cc->left.end - cc->left.start;
187 11d9f2f7 2020-10-07 stsp if (result->left.atoms.len == 0)
188 11d9f2f7 2020-10-07 stsp left_start = 0;
189 11d9f2f7 2020-10-07 stsp else if (left_len == 0 && cc->left.start > 0)
190 b6adedb6 2020-10-07 stsp left_start = cc->left.start;
191 b6adedb6 2020-10-07 stsp else
192 b6adedb6 2020-10-07 stsp left_start = cc->left.start + 1;
193 b6adedb6 2020-10-07 stsp
194 b6adedb6 2020-10-07 stsp right_len = cc->right.end - cc->right.start;
195 11d9f2f7 2020-10-07 stsp if (result->right.atoms.len == 0)
196 11d9f2f7 2020-10-07 stsp right_start = 0;
197 11d9f2f7 2020-10-07 stsp else if (right_len == 0 && cc->right.start > 0)
198 b6adedb6 2020-10-07 stsp right_start = cc->right.start;
199 b6adedb6 2020-10-07 stsp else
200 b6adedb6 2020-10-07 stsp right_start = cc->right.start + 1;
201 b6adedb6 2020-10-07 stsp
202 d2dfa2ec 2020-10-07 stsp if (left_len == 1 && right_len == 1) {
203 d2dfa2ec 2020-10-07 stsp rc = fprintf(dest, "@@ -%d +%d @@\n",
204 d2dfa2ec 2020-10-07 stsp left_start, right_start);
205 d2dfa2ec 2020-10-07 stsp } else if (left_len == 1 && right_len != 1) {
206 b6adedb6 2020-10-07 stsp rc = fprintf(dest, "@@ -%d +%d,%d @@\n",
207 b6adedb6 2020-10-07 stsp left_start, right_start, right_len);
208 b6adedb6 2020-10-07 stsp } else if (left_len != 1 && right_len == 1) {
209 b6adedb6 2020-10-07 stsp rc = fprintf(dest, "@@ -%d,%d +%d @@\n",
210 b6adedb6 2020-10-07 stsp left_start, left_len, right_start);
211 b6adedb6 2020-10-07 stsp } else {
212 b6adedb6 2020-10-07 stsp rc = fprintf(dest, "@@ -%d,%d +%d,%d @@\n",
213 b6adedb6 2020-10-07 stsp left_start, left_len, right_start, right_len);
214 b6adedb6 2020-10-07 stsp }
215 2c20a3ed 2020-09-22 stsp if (rc < 0)
216 2c20a3ed 2020-09-22 stsp return errno;
217 2c20a3ed 2020-09-22 stsp if (outinfo) {
218 2c20a3ed 2020-09-22 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
219 dabc1008 2020-09-22 stsp if (offp == NULL)
220 dabc1008 2020-09-22 stsp return ENOMEM;
221 2c20a3ed 2020-09-22 stsp outoff += rc;
222 2c20a3ed 2020-09-22 stsp *offp = outoff;
223 3b0f3d61 2020-01-22 neels
224 2c20a3ed 2020-09-22 stsp }
225 2c20a3ed 2020-09-22 stsp
226 0d27172a 2020-05-06 neels /* Got the absolute line numbers where to start printing, and the index
227 0d27172a 2020-05-06 neels * of the interesting (non-context) chunk.
228 0d27172a 2020-05-06 neels * To print context lines above the interesting chunk, nipping on the
229 0d27172a 2020-05-06 neels * previous chunk index may be necessary.
230 0d27172a 2020-05-06 neels * It is guaranteed to be only context lines where left == right, so it
231 0d27172a 2020-05-06 neels * suffices to look on the left. */
232 0d27172a 2020-05-06 neels const struct diff_chunk *first_chunk;
233 0d27172a 2020-05-06 neels int chunk_start_line;
234 0d27172a 2020-05-06 neels first_chunk = &result->chunks.head[cc->chunk.start];
235 0d27172a 2020-05-06 neels chunk_start_line = diff_atom_root_idx(&result->left,
236 0d27172a 2020-05-06 neels first_chunk->left_start);
237 2c20a3ed 2020-09-22 stsp if (cc->left.start < chunk_start_line) {
238 2c20a3ed 2020-09-22 stsp rc = diff_output_lines(outinfo, dest, " ",
239 0d27172a 2020-05-06 neels &result->left.atoms.head[cc->left.start],
240 3b0f3d61 2020-01-22 neels chunk_start_line - cc->left.start);
241 2c20a3ed 2020-09-22 stsp if (rc)
242 2c20a3ed 2020-09-22 stsp return rc;
243 2c20a3ed 2020-09-22 stsp }
244 3b0f3d61 2020-01-22 neels
245 3b0f3d61 2020-01-22 neels /* Now write out all the joined chunks and contexts between them */
246 3b0f3d61 2020-01-22 neels int c_idx;
247 3b0f3d61 2020-01-22 neels for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
248 3b0f3d61 2020-01-22 neels const struct diff_chunk *c = &result->chunks.head[c_idx];
249 3b0f3d61 2020-01-22 neels
250 3b0f3d61 2020-01-22 neels if (c->left_count && c->right_count)
251 2c20a3ed 2020-09-22 stsp rc = diff_output_lines(outinfo, dest,
252 0d27172a 2020-05-06 neels c->solved ? " " : "?",
253 0d27172a 2020-05-06 neels c->left_start, c->left_count);
254 3b0f3d61 2020-01-22 neels else if (c->left_count && !c->right_count)
255 2c20a3ed 2020-09-22 stsp rc = diff_output_lines(outinfo, dest,
256 0d27172a 2020-05-06 neels c->solved ? "-" : "?",
257 0d27172a 2020-05-06 neels c->left_start, c->left_count);
258 3b0f3d61 2020-01-22 neels else if (c->right_count && !c->left_count)
259 2c20a3ed 2020-09-22 stsp rc = diff_output_lines(outinfo, dest,
260 0d27172a 2020-05-06 neels c->solved ? "+" : "?",
261 0d27172a 2020-05-06 neels c->right_start, c->right_count);
262 2c20a3ed 2020-09-22 stsp if (rc)
263 2c20a3ed 2020-09-22 stsp return rc;
264 7021523c 2020-10-16 stsp
265 7021523c 2020-10-16 stsp if (cc->chunk.end == result->chunks.len) {
266 7021523c 2020-10-16 stsp rc = diff_output_trailing_newline_msg(outinfo, dest, c);
267 7021523c 2020-10-16 stsp if (rc != DIFF_RC_OK)
268 7021523c 2020-10-16 stsp return rc;
269 7021523c 2020-10-16 stsp }
270 3b0f3d61 2020-01-22 neels }
271 3b0f3d61 2020-01-22 neels
272 3b0f3d61 2020-01-22 neels /* Trailing context? */
273 0d27172a 2020-05-06 neels const struct diff_chunk *last_chunk;
274 0d27172a 2020-05-06 neels int chunk_end_line;
275 0d27172a 2020-05-06 neels last_chunk = &result->chunks.head[cc->chunk.end - 1];
276 0d27172a 2020-05-06 neels chunk_end_line = diff_atom_root_idx(&result->left,
277 0d27172a 2020-05-06 neels last_chunk->left_start
278 0d27172a 2020-05-06 neels + last_chunk->left_count);
279 2c20a3ed 2020-09-22 stsp if (cc->left.end > chunk_end_line) {
280 2c20a3ed 2020-09-22 stsp rc = diff_output_lines(outinfo, dest, " ",
281 0d27172a 2020-05-06 neels &result->left.atoms.head[chunk_end_line],
282 3b0f3d61 2020-01-22 neels cc->left.end - chunk_end_line);
283 2c20a3ed 2020-09-22 stsp if (rc)
284 2c20a3ed 2020-09-22 stsp return rc;
285 2c20a3ed 2020-09-22 stsp }
286 2c20a3ed 2020-09-22 stsp
287 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
288 3b0f3d61 2020-01-22 neels }
289 3b0f3d61 2020-01-22 neels
290 3e6cba3a 2020-08-13 stsp int
291 2c20a3ed 2020-09-22 stsp diff_output_unidiff_chunk(struct diff_output_info **output_info, FILE *dest,
292 2c20a3ed 2020-09-22 stsp struct diff_output_unidiff_state *state,
293 2c20a3ed 2020-09-22 stsp const struct diff_input_info *info,
294 2c20a3ed 2020-09-22 stsp const struct diff_result *result,
295 2c20a3ed 2020-09-22 stsp const struct diff_chunk_context *cc)
296 2c20a3ed 2020-09-22 stsp {
297 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
298 2c20a3ed 2020-09-22 stsp
299 2c20a3ed 2020-09-22 stsp if (output_info) {
300 2c20a3ed 2020-09-22 stsp *output_info = diff_output_info_alloc();
301 2c20a3ed 2020-09-22 stsp if (*output_info == NULL)
302 2c20a3ed 2020-09-22 stsp return ENOMEM;
303 2c20a3ed 2020-09-22 stsp outinfo = *output_info;
304 2c20a3ed 2020-09-22 stsp }
305 2c20a3ed 2020-09-22 stsp
306 2c20a3ed 2020-09-22 stsp return output_unidiff_chunk(outinfo, dest, state, info,
307 2c20a3ed 2020-09-22 stsp result, cc);
308 2c20a3ed 2020-09-22 stsp }
309 2c20a3ed 2020-09-22 stsp
310 2c20a3ed 2020-09-22 stsp int
311 2c20a3ed 2020-09-22 stsp diff_output_unidiff(struct diff_output_info **output_info,
312 2c20a3ed 2020-09-22 stsp FILE *dest, const struct diff_input_info *info,
313 0d27172a 2020-05-06 neels const struct diff_result *result,
314 0d27172a 2020-05-06 neels unsigned int context_lines)
315 3b0f3d61 2020-01-22 neels {
316 11caa5cc 2020-09-22 stsp struct diff_output_unidiff_state *state;
317 11caa5cc 2020-09-22 stsp struct diff_chunk_context cc = {};
318 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
319 11caa5cc 2020-09-22 stsp int i;
320 11caa5cc 2020-09-22 stsp
321 3b0f3d61 2020-01-22 neels if (!result)
322 3e6cba3a 2020-08-13 stsp return EINVAL;
323 3b0f3d61 2020-01-22 neels if (result->rc != DIFF_RC_OK)
324 3b0f3d61 2020-01-22 neels return result->rc;
325 3b0f3d61 2020-01-22 neels
326 2c20a3ed 2020-09-22 stsp if (output_info) {
327 2c20a3ed 2020-09-22 stsp *output_info = diff_output_info_alloc();
328 2c20a3ed 2020-09-22 stsp if (*output_info == NULL)
329 2c20a3ed 2020-09-22 stsp return ENOMEM;
330 2c20a3ed 2020-09-22 stsp outinfo = *output_info;
331 2c20a3ed 2020-09-22 stsp }
332 2c20a3ed 2020-09-22 stsp
333 11caa5cc 2020-09-22 stsp state = diff_output_unidiff_state_alloc();
334 2c20a3ed 2020-09-22 stsp if (state == NULL) {
335 2c20a3ed 2020-09-22 stsp if (output_info) {
336 2c20a3ed 2020-09-22 stsp diff_output_info_free(*output_info);
337 2c20a3ed 2020-09-22 stsp *output_info = NULL;
338 2c20a3ed 2020-09-22 stsp }
339 11caa5cc 2020-09-22 stsp return ENOMEM;
340 2c20a3ed 2020-09-22 stsp }
341 3b0f3d61 2020-01-22 neels
342 5ff75996 2020-10-11 neels #if DEBUG
343 5ff75996 2020-10-11 neels for (i = 0; i < result->chunks.len; i++) {
344 5ff75996 2020-10-11 neels struct diff_chunk *c = &result->chunks.head[i];
345 5ff75996 2020-10-11 neels enum diff_chunk_type t = diff_chunk_type(c);
346 2c20a3ed 2020-09-22 stsp
347 5ff75996 2020-10-11 neels debug("[%d] %s lines L%d R%d @L %d @R %d\n",
348 5ff75996 2020-10-11 neels i, (t == CHUNK_MINUS ? "minus" :
349 5ff75996 2020-10-11 neels (t == CHUNK_PLUS ? "plus" :
350 5ff75996 2020-10-11 neels (t == CHUNK_SAME ? "same" : "?"))),
351 5ff75996 2020-10-11 neels c->left_count,
352 5ff75996 2020-10-11 neels c->right_count,
353 5ff75996 2020-10-11 neels c->left_start ? diff_atom_root_idx(&result->left, c->left_start) : -1,
354 5ff75996 2020-10-11 neels c->right_start ? diff_atom_root_idx(&result->right, c->right_start) : -1);
355 5ff75996 2020-10-11 neels }
356 5ff75996 2020-10-11 neels #endif
357 5ff75996 2020-10-11 neels
358 3b0f3d61 2020-01-22 neels for (i = 0; i < result->chunks.len; i++) {
359 3b0f3d61 2020-01-22 neels struct diff_chunk *c = &result->chunks.head[i];
360 8546b045 2020-09-20 neels enum diff_chunk_type t = diff_chunk_type(c);
361 f374e913 2020-09-22 stsp struct diff_chunk_context next;
362 3b0f3d61 2020-01-22 neels
363 9cc49695 2020-05-05 neels if (t != CHUNK_MINUS && t != CHUNK_PLUS)
364 9cc49695 2020-05-05 neels continue;
365 9cc49695 2020-05-05 neels
366 cbf93b70 2020-10-16 stsp if (diff_chunk_context_empty(&cc)) {
367 9cc49695 2020-05-05 neels /* These are the first lines being printed.
368 0d27172a 2020-05-06 neels * Note down the start point, any number of subsequent
369 0d27172a 2020-05-06 neels * chunks may be joined up to this unidiff chunk by
370 0d27172a 2020-05-06 neels * context lines or by being directly adjacent. */
371 f374e913 2020-09-22 stsp diff_chunk_context_get(&cc, result, i, context_lines);
372 0d27172a 2020-05-06 neels debug("new chunk to be printed:"
373 0d27172a 2020-05-06 neels " chunk %d-%d left %d-%d right %d-%d\n",
374 9cc49695 2020-05-05 neels cc.chunk.start, cc.chunk.end,
375 9cc49695 2020-05-05 neels cc.left.start, cc.left.end,
376 9cc49695 2020-05-05 neels cc.right.start, cc.right.end);
377 9cc49695 2020-05-05 neels continue;
378 3b0f3d61 2020-01-22 neels }
379 3b0f3d61 2020-01-22 neels
380 0d27172a 2020-05-06 neels /* There already is a previous chunk noted down for being
381 0d27172a 2020-05-06 neels * printed. Does it join up with this one? */
382 f374e913 2020-09-22 stsp diff_chunk_context_get(&next, result, i, context_lines);
383 0d27172a 2020-05-06 neels debug("new chunk to be printed:"
384 0d27172a 2020-05-06 neels " chunk %d-%d left %d-%d right %d-%d\n",
385 9cc49695 2020-05-05 neels next.chunk.start, next.chunk.end,
386 9cc49695 2020-05-05 neels next.left.start, next.left.end,
387 9cc49695 2020-05-05 neels next.right.start, next.right.end);
388 9cc49695 2020-05-05 neels
389 26595c7d 2020-10-15 stsp if (diff_chunk_contexts_touch(&cc, &next)) {
390 0d27172a 2020-05-06 neels /* This next context touches or overlaps the previous
391 0d27172a 2020-05-06 neels * one, join. */
392 26595c7d 2020-10-15 stsp diff_chunk_contexts_merge(&cc, &next);
393 0d27172a 2020-05-06 neels debug("new chunk to be printed touches previous chunk,"
394 0d27172a 2020-05-06 neels " now: left %d-%d right %d-%d\n",
395 9cc49695 2020-05-05 neels cc.left.start, cc.left.end,
396 9cc49695 2020-05-05 neels cc.right.start, cc.right.end);
397 9cc49695 2020-05-05 neels continue;
398 9cc49695 2020-05-05 neels }
399 9cc49695 2020-05-05 neels
400 0d27172a 2020-05-06 neels /* No touching, so the previous context is complete with a gap
401 0d27172a 2020-05-06 neels * between it and this next one. Print the previous one and
402 0d27172a 2020-05-06 neels * start fresh here. */
403 0d27172a 2020-05-06 neels debug("new chunk to be printed does not touch previous chunk;"
404 0d27172a 2020-05-06 neels " print left %d-%d right %d-%d\n",
405 9cc49695 2020-05-05 neels cc.left.start, cc.left.end, cc.right.start, cc.right.end);
406 2c20a3ed 2020-09-22 stsp output_unidiff_chunk(outinfo, dest, state, info, result, &cc);
407 9cc49695 2020-05-05 neels cc = next;
408 9cc49695 2020-05-05 neels debug("new unprinted chunk is left %d-%d right %d-%d\n",
409 9cc49695 2020-05-05 neels cc.left.start, cc.left.end, cc.right.start, cc.right.end);
410 3b0f3d61 2020-01-22 neels }
411 3b0f3d61 2020-01-22 neels
412 cbf93b70 2020-10-16 stsp if (!diff_chunk_context_empty(&cc))
413 2c20a3ed 2020-09-22 stsp output_unidiff_chunk(outinfo, dest, state, info, result, &cc);
414 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_free(state);
415 3b0f3d61 2020-01-22 neels return DIFF_RC_OK;
416 3b0f3d61 2020-01-22 neels }