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