Blob


1 /* Produce a unidiff output from a diff_result. */
2 /*
3 * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <errno.h>
19 #include <inttypes.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
24 #include <diff/arraylist.h>
25 #include <diff/diff_main.h>
26 #include <diff/diff_output.h>
28 #include "debug.h"
30 enum chunk_type {
31 CHUNK_EMPTY,
32 CHUNK_PLUS,
33 CHUNK_MINUS,
34 CHUNK_SAME,
35 CHUNK_WEIRD,
36 };
38 static inline enum chunk_type
39 chunk_type(const struct diff_chunk *chunk)
40 {
41 if (!chunk->left_count && !chunk->right_count)
42 return CHUNK_EMPTY;
43 if (!chunk->solved)
44 return CHUNK_WEIRD;
45 if (!chunk->right_count)
46 return CHUNK_MINUS;
47 if (!chunk->left_count)
48 return CHUNK_PLUS;
49 if (chunk->left_count != chunk->right_count)
50 return CHUNK_WEIRD;
51 return CHUNK_SAME;
52 }
54 struct chunk_context {
55 struct diff_range chunk;
56 struct diff_range left, right;
57 };
59 static bool
60 chunk_context_empty(const struct chunk_context *cc)
61 {
62 return diff_range_empty(&cc->chunk);
63 }
65 static void
66 chunk_context_get(struct chunk_context *cc, const struct diff_result *r,
67 int chunk_idx, int context_lines)
68 {
69 const struct diff_chunk *c = &r->chunks.head[chunk_idx];
70 int left_start = diff_atom_root_idx(&r->left, c->left_start);
71 int left_end = MIN(r->left.atoms.len,
72 left_start + c->left_count + context_lines);
73 int right_start = diff_atom_root_idx(&r->right, c->right_start);
74 int right_end = MIN(r->right.atoms.len,
75 right_start + c->right_count + context_lines);
77 left_start = MAX(0, left_start - context_lines);
78 right_start = MAX(0, right_start - context_lines);
80 *cc = (struct chunk_context){
81 .chunk = {
82 .start = chunk_idx,
83 .end = chunk_idx + 1,
84 },
85 .left = {
86 .start = left_start,
87 .end = left_end,
88 },
89 .right = {
90 .start = right_start,
91 .end = right_end,
92 },
93 };
94 }
96 static bool
97 chunk_contexts_touch(const struct chunk_context *cc,
98 const struct chunk_context *other)
99 {
100 return diff_ranges_touch(&cc->chunk, &other->chunk)
101 || diff_ranges_touch(&cc->left, &other->left)
102 || diff_ranges_touch(&cc->right, &other->right);
105 static void
106 chunk_contexts_merge(struct chunk_context *cc,
107 const struct chunk_context *other)
109 diff_ranges_merge(&cc->chunk, &other->chunk);
110 diff_ranges_merge(&cc->left, &other->left);
111 diff_ranges_merge(&cc->right, &other->right);
114 static void
115 diff_output_unidiff_chunk(FILE *dest, bool *header_printed,
116 const struct diff_input_info *info,
117 const struct diff_result *result,
118 const struct chunk_context *cc)
120 if (diff_range_empty(&cc->left) && diff_range_empty(&cc->right))
121 return;
123 if (!(*header_printed)) {
124 fprintf(dest, "--- %s\n+++ %s\n",
125 info->left_path ? : "a",
126 info->right_path ? : "b");
127 *header_printed = true;
130 fprintf(dest, "@@ -%d,%d +%d,%d @@\n",
131 cc->left.start + 1, cc->left.end - cc->left.start,
132 cc->right.start + 1, cc->right.end - cc->right.start);
134 /* Got the absolute line numbers where to start printing, and the index
135 * of the interesting (non-context) chunk.
136 * To print context lines above the interesting chunk, nipping on the
137 * previous chunk index may be necessary.
138 * It is guaranteed to be only context lines where left == right, so it
139 * suffices to look on the left. */
140 const struct diff_chunk *first_chunk;
141 int chunk_start_line;
142 first_chunk = &result->chunks.head[cc->chunk.start];
143 chunk_start_line = diff_atom_root_idx(&result->left,
144 first_chunk->left_start);
145 if (cc->left.start < chunk_start_line)
146 diff_output_lines(dest, " ",
147 &result->left.atoms.head[cc->left.start],
148 chunk_start_line - cc->left.start);
150 /* Now write out all the joined chunks and contexts between them */
151 int c_idx;
152 for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
153 const struct diff_chunk *c = &result->chunks.head[c_idx];
155 if (c->left_count && c->right_count)
156 diff_output_lines(dest,
157 c->solved ? " " : "?",
158 c->left_start, c->left_count);
159 else if (c->left_count && !c->right_count)
160 diff_output_lines(dest,
161 c->solved ? "-" : "?",
162 c->left_start, c->left_count);
163 else if (c->right_count && !c->left_count)
164 diff_output_lines(dest,
165 c->solved ? "+" : "?",
166 c->right_start, c->right_count);
169 /* Trailing context? */
170 const struct diff_chunk *last_chunk;
171 int chunk_end_line;
172 last_chunk = &result->chunks.head[cc->chunk.end - 1];
173 chunk_end_line = diff_atom_root_idx(&result->left,
174 last_chunk->left_start
175 + last_chunk->left_count);
176 if (cc->left.end > chunk_end_line)
177 diff_output_lines(dest, " ",
178 &result->left.atoms.head[chunk_end_line],
179 cc->left.end - chunk_end_line);
182 int
183 diff_output_unidiff(FILE *dest, const struct diff_input_info *info,
184 const struct diff_result *result,
185 unsigned int context_lines)
187 if (!result)
188 return EINVAL;
189 if (result->rc != DIFF_RC_OK)
190 return result->rc;
192 struct chunk_context cc = {};
193 bool header_printed = false;
195 int i;
196 for (i = 0; i < result->chunks.len; i++) {
197 struct diff_chunk *c = &result->chunks.head[i];
198 enum chunk_type t = chunk_type(c);
199 struct chunk_context next;
201 if (t != CHUNK_MINUS && t != CHUNK_PLUS)
202 continue;
204 if (chunk_context_empty(&cc)) {
205 /* These are the first lines being printed.
206 * Note down the start point, any number of subsequent
207 * chunks may be joined up to this unidiff chunk by
208 * context lines or by being directly adjacent. */
209 chunk_context_get(&cc, result, i, context_lines);
210 debug("new chunk to be printed:"
211 " chunk %d-%d left %d-%d right %d-%d\n",
212 cc.chunk.start, cc.chunk.end,
213 cc.left.start, cc.left.end,
214 cc.right.start, cc.right.end);
215 continue;
218 /* There already is a previous chunk noted down for being
219 * printed. Does it join up with this one? */
220 chunk_context_get(&next, result, i, context_lines);
221 debug("new chunk to be printed:"
222 " chunk %d-%d left %d-%d right %d-%d\n",
223 next.chunk.start, next.chunk.end,
224 next.left.start, next.left.end,
225 next.right.start, next.right.end);
227 if (chunk_contexts_touch(&cc, &next)) {
228 /* This next context touches or overlaps the previous
229 * one, join. */
230 chunk_contexts_merge(&cc, &next);
231 debug("new chunk to be printed touches previous chunk,"
232 " now: left %d-%d right %d-%d\n",
233 cc.left.start, cc.left.end,
234 cc.right.start, cc.right.end);
235 continue;
238 /* No touching, so the previous context is complete with a gap
239 * between it and this next one. Print the previous one and
240 * start fresh here. */
241 debug("new chunk to be printed does not touch previous chunk;"
242 " print left %d-%d right %d-%d\n",
243 cc.left.start, cc.left.end, cc.right.start, cc.right.end);
244 diff_output_unidiff_chunk(dest, &header_printed, info, result,
245 &cc);
246 cc = next;
247 debug("new unprinted chunk is left %d-%d right %d-%d\n",
248 cc.left.start, cc.left.end, cc.right.start, cc.right.end);
251 if (!chunk_context_empty(&cc))
252 diff_output_unidiff_chunk(dest, &header_printed, info, result,
253 &cc);
254 return DIFF_RC_OK;