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