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 3b0f3d61 2020-01-22 neels #include <diff/diff_output.h>
19 3b0f3d61 2020-01-22 neels
20 3b0f3d61 2020-01-22 neels #include "debug.h"
21 3b0f3d61 2020-01-22 neels
22 3b0f3d61 2020-01-22 neels enum chunk_type {
23 3b0f3d61 2020-01-22 neels CHUNK_EMPTY,
24 3b0f3d61 2020-01-22 neels CHUNK_PLUS,
25 3b0f3d61 2020-01-22 neels CHUNK_MINUS,
26 3b0f3d61 2020-01-22 neels CHUNK_SAME,
27 3b0f3d61 2020-01-22 neels CHUNK_WEIRD,
28 3b0f3d61 2020-01-22 neels };
29 3b0f3d61 2020-01-22 neels
30 61a7b578 2020-05-06 neels static inline enum chunk_type
31 61a7b578 2020-05-06 neels chunk_type(const struct diff_chunk *chunk)
32 3b0f3d61 2020-01-22 neels {
33 3b0f3d61 2020-01-22 neels if (!chunk->left_count && !chunk->right_count)
34 3b0f3d61 2020-01-22 neels return CHUNK_EMPTY;
35 3b0f3d61 2020-01-22 neels if (!chunk->solved)
36 3b0f3d61 2020-01-22 neels return CHUNK_WEIRD;
37 3b0f3d61 2020-01-22 neels if (!chunk->right_count)
38 3b0f3d61 2020-01-22 neels return CHUNK_MINUS;
39 3b0f3d61 2020-01-22 neels if (!chunk->left_count)
40 3b0f3d61 2020-01-22 neels return CHUNK_PLUS;
41 3b0f3d61 2020-01-22 neels if (chunk->left_count != chunk->right_count)
42 3b0f3d61 2020-01-22 neels return CHUNK_WEIRD;
43 3b0f3d61 2020-01-22 neels return CHUNK_SAME;
44 3b0f3d61 2020-01-22 neels }
45 3b0f3d61 2020-01-22 neels
46 3b0f3d61 2020-01-22 neels struct chunk_context {
47 d362ea2e 2020-07-25 stsp struct diff_range chunk;
48 d362ea2e 2020-07-25 stsp struct diff_range left, right;
49 3b0f3d61 2020-01-22 neels };
50 3b0f3d61 2020-01-22 neels
51 61a7b578 2020-05-06 neels static bool
52 61a7b578 2020-05-06 neels chunk_context_empty(const struct chunk_context *cc)
53 3b0f3d61 2020-01-22 neels {
54 d362ea2e 2020-07-25 stsp return diff_range_empty(&cc->chunk);
55 3b0f3d61 2020-01-22 neels }
56 3b0f3d61 2020-01-22 neels
57 61a7b578 2020-05-06 neels static void
58 0d27172a 2020-05-06 neels chunk_context_get(struct chunk_context *cc, const struct diff_result *r,
59 0d27172a 2020-05-06 neels int chunk_idx, int context_lines)
60 3b0f3d61 2020-01-22 neels {
61 3b0f3d61 2020-01-22 neels const struct diff_chunk *c = &r->chunks.head[chunk_idx];
62 3b0f3d61 2020-01-22 neels int left_start = diff_atom_root_idx(&r->left, c->left_start);
63 0d27172a 2020-05-06 neels int left_end = MIN(r->left.atoms.len,
64 0d27172a 2020-05-06 neels left_start + c->left_count + context_lines);
65 3b0f3d61 2020-01-22 neels int right_start = diff_atom_root_idx(&r->right, c->right_start);
66 0d27172a 2020-05-06 neels int right_end = MIN(r->right.atoms.len,
67 0d27172a 2020-05-06 neels right_start + c->right_count + context_lines);
68 3b0f3d61 2020-01-22 neels
69 0d27172a 2020-05-06 neels left_start = MAX(0, left_start - context_lines);
70 0d27172a 2020-05-06 neels right_start = MAX(0, right_start - context_lines);
71 0d27172a 2020-05-06 neels
72 3b0f3d61 2020-01-22 neels *cc = (struct chunk_context){
73 3b0f3d61 2020-01-22 neels .chunk = {
74 3b0f3d61 2020-01-22 neels .start = chunk_idx,
75 3b0f3d61 2020-01-22 neels .end = chunk_idx + 1,
76 3b0f3d61 2020-01-22 neels },
77 3b0f3d61 2020-01-22 neels .left = {
78 0d27172a 2020-05-06 neels .start = left_start,
79 0d27172a 2020-05-06 neels .end = left_end,
80 3b0f3d61 2020-01-22 neels },
81 3b0f3d61 2020-01-22 neels .right = {
82 0d27172a 2020-05-06 neels .start = right_start,
83 0d27172a 2020-05-06 neels .end = right_end,
84 3b0f3d61 2020-01-22 neels },
85 3b0f3d61 2020-01-22 neels };
86 3b0f3d61 2020-01-22 neels }
87 3b0f3d61 2020-01-22 neels
88 61a7b578 2020-05-06 neels static bool
89 0d27172a 2020-05-06 neels chunk_contexts_touch(const struct chunk_context *cc,
90 0d27172a 2020-05-06 neels const struct chunk_context *other)
91 3b0f3d61 2020-01-22 neels {
92 d362ea2e 2020-07-25 stsp return diff_ranges_touch(&cc->chunk, &other->chunk)
93 d362ea2e 2020-07-25 stsp || diff_ranges_touch(&cc->left, &other->left)
94 d362ea2e 2020-07-25 stsp || diff_ranges_touch(&cc->right, &other->right);
95 3b0f3d61 2020-01-22 neels }
96 3b0f3d61 2020-01-22 neels
97 61a7b578 2020-05-06 neels static void
98 0d27172a 2020-05-06 neels chunk_contexts_merge(struct chunk_context *cc,
99 0d27172a 2020-05-06 neels const struct chunk_context *other)
100 3b0f3d61 2020-01-22 neels {
101 d362ea2e 2020-07-25 stsp diff_ranges_merge(&cc->chunk, &other->chunk);
102 d362ea2e 2020-07-25 stsp diff_ranges_merge(&cc->left, &other->left);
103 d362ea2e 2020-07-25 stsp diff_ranges_merge(&cc->right, &other->right);
104 3b0f3d61 2020-01-22 neels }
105 3b0f3d61 2020-01-22 neels
106 61a7b578 2020-05-06 neels static void
107 0d27172a 2020-05-06 neels diff_output_unidiff_chunk(FILE *dest, bool *header_printed,
108 0d27172a 2020-05-06 neels const struct diff_input_info *info,
109 0d27172a 2020-05-06 neels const struct diff_result *result,
110 0d27172a 2020-05-06 neels const struct chunk_context *cc)
111 3b0f3d61 2020-01-22 neels {
112 d362ea2e 2020-07-25 stsp if (diff_range_empty(&cc->left) && diff_range_empty(&cc->right))
113 3b0f3d61 2020-01-22 neels return;
114 3b0f3d61 2020-01-22 neels
115 a5cbcf9c 2020-05-05 neels if (!(*header_printed)) {
116 a5cbcf9c 2020-05-05 neels fprintf(dest, "--- %s\n+++ %s\n",
117 a5cbcf9c 2020-05-05 neels info->left_path ? : "a",
118 a5cbcf9c 2020-05-05 neels info->right_path ? : "b");
119 a5cbcf9c 2020-05-05 neels *header_printed = true;
120 3b0f3d61 2020-01-22 neels }
121 3b0f3d61 2020-01-22 neels
122 3b0f3d61 2020-01-22 neels fprintf(dest, "@@ -%d,%d +%d,%d @@\n",
123 3b0f3d61 2020-01-22 neels cc->left.start + 1, cc->left.end - cc->left.start,
124 3b0f3d61 2020-01-22 neels cc->right.start + 1, cc->right.end - cc->right.start);
125 3b0f3d61 2020-01-22 neels
126 0d27172a 2020-05-06 neels /* Got the absolute line numbers where to start printing, and the index
127 0d27172a 2020-05-06 neels * of the interesting (non-context) chunk.
128 0d27172a 2020-05-06 neels * To print context lines above the interesting chunk, nipping on the
129 0d27172a 2020-05-06 neels * previous chunk index may be necessary.
130 0d27172a 2020-05-06 neels * It is guaranteed to be only context lines where left == right, so it
131 0d27172a 2020-05-06 neels * suffices to look on the left. */
132 0d27172a 2020-05-06 neels const struct diff_chunk *first_chunk;
133 0d27172a 2020-05-06 neels int chunk_start_line;
134 0d27172a 2020-05-06 neels first_chunk = &result->chunks.head[cc->chunk.start];
135 0d27172a 2020-05-06 neels chunk_start_line = diff_atom_root_idx(&result->left,
136 0d27172a 2020-05-06 neels first_chunk->left_start);
137 3b0f3d61 2020-01-22 neels if (cc->left.start < chunk_start_line)
138 0d27172a 2020-05-06 neels diff_output_lines(dest, " ",
139 0d27172a 2020-05-06 neels &result->left.atoms.head[cc->left.start],
140 3b0f3d61 2020-01-22 neels chunk_start_line - cc->left.start);
141 3b0f3d61 2020-01-22 neels
142 3b0f3d61 2020-01-22 neels /* Now write out all the joined chunks and contexts between them */
143 3b0f3d61 2020-01-22 neels int c_idx;
144 3b0f3d61 2020-01-22 neels for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
145 3b0f3d61 2020-01-22 neels const struct diff_chunk *c = &result->chunks.head[c_idx];
146 3b0f3d61 2020-01-22 neels
147 3b0f3d61 2020-01-22 neels if (c->left_count && c->right_count)
148 0d27172a 2020-05-06 neels diff_output_lines(dest,
149 0d27172a 2020-05-06 neels c->solved ? " " : "?",
150 0d27172a 2020-05-06 neels c->left_start, c->left_count);
151 3b0f3d61 2020-01-22 neels else if (c->left_count && !c->right_count)
152 0d27172a 2020-05-06 neels diff_output_lines(dest,
153 0d27172a 2020-05-06 neels c->solved ? "-" : "?",
154 0d27172a 2020-05-06 neels c->left_start, c->left_count);
155 3b0f3d61 2020-01-22 neels else if (c->right_count && !c->left_count)
156 0d27172a 2020-05-06 neels diff_output_lines(dest,
157 0d27172a 2020-05-06 neels c->solved ? "+" : "?",
158 0d27172a 2020-05-06 neels c->right_start, c->right_count);
159 3b0f3d61 2020-01-22 neels }
160 3b0f3d61 2020-01-22 neels
161 3b0f3d61 2020-01-22 neels /* Trailing context? */
162 0d27172a 2020-05-06 neels const struct diff_chunk *last_chunk;
163 0d27172a 2020-05-06 neels int chunk_end_line;
164 0d27172a 2020-05-06 neels last_chunk = &result->chunks.head[cc->chunk.end - 1];
165 0d27172a 2020-05-06 neels chunk_end_line = diff_atom_root_idx(&result->left,
166 0d27172a 2020-05-06 neels last_chunk->left_start
167 0d27172a 2020-05-06 neels + last_chunk->left_count);
168 3b0f3d61 2020-01-22 neels if (cc->left.end > chunk_end_line)
169 0d27172a 2020-05-06 neels diff_output_lines(dest, " ",
170 0d27172a 2020-05-06 neels &result->left.atoms.head[chunk_end_line],
171 3b0f3d61 2020-01-22 neels cc->left.end - chunk_end_line);
172 3b0f3d61 2020-01-22 neels }
173 3b0f3d61 2020-01-22 neels
174 61a7b578 2020-05-06 neels enum diff_rc
175 61a7b578 2020-05-06 neels diff_output_unidiff(FILE *dest, const struct diff_input_info *info,
176 0d27172a 2020-05-06 neels const struct diff_result *result,
177 0d27172a 2020-05-06 neels unsigned int context_lines)
178 3b0f3d61 2020-01-22 neels {
179 3b0f3d61 2020-01-22 neels if (!result)
180 3b0f3d61 2020-01-22 neels return DIFF_RC_EINVAL;
181 3b0f3d61 2020-01-22 neels if (result->rc != DIFF_RC_OK)
182 3b0f3d61 2020-01-22 neels return result->rc;
183 3b0f3d61 2020-01-22 neels
184 3b0f3d61 2020-01-22 neels struct chunk_context cc = {};
185 a5cbcf9c 2020-05-05 neels bool header_printed = false;
186 3b0f3d61 2020-01-22 neels
187 3b0f3d61 2020-01-22 neels int i;
188 3b0f3d61 2020-01-22 neels for (i = 0; i < result->chunks.len; i++) {
189 3b0f3d61 2020-01-22 neels struct diff_chunk *c = &result->chunks.head[i];
190 3b0f3d61 2020-01-22 neels enum chunk_type t = chunk_type(c);
191 9cc49695 2020-05-05 neels struct chunk_context next;
192 3b0f3d61 2020-01-22 neels
193 9cc49695 2020-05-05 neels if (t != CHUNK_MINUS && t != CHUNK_PLUS)
194 9cc49695 2020-05-05 neels continue;
195 9cc49695 2020-05-05 neels
196 9cc49695 2020-05-05 neels if (chunk_context_empty(&cc)) {
197 9cc49695 2020-05-05 neels /* These are the first lines being printed.
198 0d27172a 2020-05-06 neels * Note down the start point, any number of subsequent
199 0d27172a 2020-05-06 neels * chunks may be joined up to this unidiff chunk by
200 0d27172a 2020-05-06 neels * context lines or by being directly adjacent. */
201 9cc49695 2020-05-05 neels chunk_context_get(&cc, result, i, context_lines);
202 0d27172a 2020-05-06 neels debug("new chunk to be printed:"
203 0d27172a 2020-05-06 neels " chunk %d-%d left %d-%d right %d-%d\n",
204 9cc49695 2020-05-05 neels cc.chunk.start, cc.chunk.end,
205 9cc49695 2020-05-05 neels cc.left.start, cc.left.end,
206 9cc49695 2020-05-05 neels cc.right.start, cc.right.end);
207 9cc49695 2020-05-05 neels continue;
208 3b0f3d61 2020-01-22 neels }
209 3b0f3d61 2020-01-22 neels
210 0d27172a 2020-05-06 neels /* There already is a previous chunk noted down for being
211 0d27172a 2020-05-06 neels * printed. Does it join up with this one? */
212 9cc49695 2020-05-05 neels chunk_context_get(&next, result, i, context_lines);
213 0d27172a 2020-05-06 neels debug("new chunk to be printed:"
214 0d27172a 2020-05-06 neels " chunk %d-%d left %d-%d right %d-%d\n",
215 9cc49695 2020-05-05 neels next.chunk.start, next.chunk.end,
216 9cc49695 2020-05-05 neels next.left.start, next.left.end,
217 9cc49695 2020-05-05 neels next.right.start, next.right.end);
218 9cc49695 2020-05-05 neels
219 9cc49695 2020-05-05 neels if (chunk_contexts_touch(&cc, &next)) {
220 0d27172a 2020-05-06 neels /* This next context touches or overlaps the previous
221 0d27172a 2020-05-06 neels * one, join. */
222 9cc49695 2020-05-05 neels chunk_contexts_merge(&cc, &next);
223 0d27172a 2020-05-06 neels debug("new chunk to be printed touches previous chunk,"
224 0d27172a 2020-05-06 neels " now: left %d-%d right %d-%d\n",
225 9cc49695 2020-05-05 neels cc.left.start, cc.left.end,
226 9cc49695 2020-05-05 neels cc.right.start, cc.right.end);
227 9cc49695 2020-05-05 neels continue;
228 9cc49695 2020-05-05 neels }
229 9cc49695 2020-05-05 neels
230 0d27172a 2020-05-06 neels /* No touching, so the previous context is complete with a gap
231 0d27172a 2020-05-06 neels * between it and this next one. Print the previous one and
232 0d27172a 2020-05-06 neels * start fresh here. */
233 0d27172a 2020-05-06 neels debug("new chunk to be printed does not touch previous chunk;"
234 0d27172a 2020-05-06 neels " print left %d-%d right %d-%d\n",
235 9cc49695 2020-05-05 neels cc.left.start, cc.left.end, cc.right.start, cc.right.end);
236 0d27172a 2020-05-06 neels diff_output_unidiff_chunk(dest, &header_printed, info, result,
237 0d27172a 2020-05-06 neels &cc);
238 9cc49695 2020-05-05 neels cc = next;
239 9cc49695 2020-05-05 neels debug("new unprinted chunk is left %d-%d right %d-%d\n",
240 9cc49695 2020-05-05 neels cc.left.start, cc.left.end, cc.right.start, cc.right.end);
241 3b0f3d61 2020-01-22 neels }
242 3b0f3d61 2020-01-22 neels
243 3b0f3d61 2020-01-22 neels if (!chunk_context_empty(&cc))
244 0d27172a 2020-05-06 neels diff_output_unidiff_chunk(dest, &header_printed, info, result,
245 0d27172a 2020-05-06 neels &cc);
246 3b0f3d61 2020-01-22 neels return DIFF_RC_OK;
247 3b0f3d61 2020-01-22 neels }