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 <diff/diff_output.h>
20 #include "debug.h"
22 enum chunk_type {
23 CHUNK_EMPTY,
24 CHUNK_PLUS,
25 CHUNK_MINUS,
26 CHUNK_SAME,
27 CHUNK_WEIRD,
28 };
30 static inline enum chunk_type chunk_type(const struct diff_chunk *chunk)
31 {
32 if (!chunk->left_count && !chunk->right_count)
33 return CHUNK_EMPTY;
34 if (!chunk->solved)
35 return CHUNK_WEIRD;
36 if (!chunk->right_count)
37 return CHUNK_MINUS;
38 if (!chunk->left_count)
39 return CHUNK_PLUS;
40 if (chunk->left_count != chunk->right_count)
41 return CHUNK_WEIRD;
42 return CHUNK_SAME;
43 }
45 struct chunk_context {
46 struct range chunk;
47 struct range left, right;
48 };
50 static bool chunk_context_empty(const struct chunk_context *cc)
51 {
52 return range_empty(&cc->chunk);
53 }
55 static void chunk_context_get(struct chunk_context *cc, const struct diff_result *r, int chunk_idx,
56 int context_lines)
57 {
58 const struct diff_chunk *c = &r->chunks.head[chunk_idx];
59 int left_start = diff_atom_root_idx(&r->left, c->left_start);
60 int right_start = diff_atom_root_idx(&r->right, c->right_start);
62 *cc = (struct chunk_context){
63 .chunk = {
64 .start = chunk_idx,
65 .end = chunk_idx + 1,
66 },
67 .left = {
68 .start = MAX(0, left_start - context_lines),
69 .end = MIN(r->left.atoms.len, left_start + c->left_count + context_lines),
70 },
71 .right = {
72 .start = MAX(0, right_start - context_lines),
73 .end = MIN(r->right.atoms.len, right_start + c->right_count + context_lines),
74 },
75 };
76 }
78 static bool chunk_contexts_touch(const struct chunk_context *cc, const struct chunk_context *other)
79 {
80 return ranges_touch(&cc->chunk, &other->chunk)
81 || ranges_touch(&cc->left, &other->left)
82 || ranges_touch(&cc->right, &other->right);
83 }
85 static void chunk_contexts_merge(struct chunk_context *cc, const struct chunk_context *other)
86 {
87 ranges_merge(&cc->chunk, &other->chunk);
88 ranges_merge(&cc->left, &other->left);
89 ranges_merge(&cc->right, &other->right);
90 }
92 static void diff_output_unidiff_chunk(FILE *dest, bool *header_printed, const struct diff_input_info *info,
93 const struct diff_result *result, const struct chunk_context *cc)
94 {
95 if (range_empty(&cc->left) && range_empty(&cc->right))
96 return;
98 if (!(*header_printed)) {
99 fprintf(dest, "--- %s\n+++ %s\n",
100 info->left_path ? : "a",
101 info->right_path ? : "b");
102 *header_printed = true;
105 fprintf(dest, "@@ -%d,%d +%d,%d @@\n",
106 cc->left.start + 1, cc->left.end - cc->left.start,
107 cc->right.start + 1, cc->right.end - cc->right.start);
109 /* Got the absolute line numbers where to start printing, and the index of the interesting (non-context) chunk.
110 * To print context lines above the interesting chunk, nipping on the previous chunk index may be necessary.
111 * It is guaranteed to be only context lines where left == right, so it suffices to look on the left. */
112 const struct diff_chunk *first_chunk = &result->chunks.head[cc->chunk.start];
113 int chunk_start_line = diff_atom_root_idx(&result->left, first_chunk->left_start);
114 if (cc->left.start < chunk_start_line)
115 diff_output_lines(dest, " ", &result->left.atoms.head[cc->left.start],
116 chunk_start_line - cc->left.start);
118 /* Now write out all the joined chunks and contexts between them */
119 int c_idx;
120 for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
121 const struct diff_chunk *c = &result->chunks.head[c_idx];
123 if (c->left_count && c->right_count)
124 diff_output_lines(dest, c->solved ? " " : "?", c->left_start, c->left_count);
125 else if (c->left_count && !c->right_count)
126 diff_output_lines(dest, c->solved ? "-" : "?", c->left_start, c->left_count);
127 else if (c->right_count && !c->left_count)
128 diff_output_lines(dest, c->solved ? "+" : "?", c->right_start, c->right_count);
131 /* Trailing context? */
132 const struct diff_chunk *last_chunk = &result->chunks.head[cc->chunk.end - 1];
133 int chunk_end_line = diff_atom_root_idx(&result->left, last_chunk->left_start + last_chunk->left_count);
134 if (cc->left.end > chunk_end_line)
135 diff_output_lines(dest, " ", &result->left.atoms.head[chunk_end_line],
136 cc->left.end - chunk_end_line);
139 enum diff_rc diff_output_unidiff(FILE *dest, const struct diff_input_info *info,
140 const struct diff_result *result, unsigned int context_lines)
142 if (!result)
143 return DIFF_RC_EINVAL;
144 if (result->rc != DIFF_RC_OK)
145 return result->rc;
147 struct chunk_context cc = {};
148 bool header_printed = false;
150 int i;
151 for (i = 0; i < result->chunks.len; i++) {
152 struct diff_chunk *c = &result->chunks.head[i];
153 enum chunk_type t = chunk_type(c);
154 struct chunk_context next;
156 if (t != CHUNK_MINUS && t != CHUNK_PLUS)
157 continue;
159 if (chunk_context_empty(&cc)) {
160 /* These are the first lines being printed.
161 * Note down the start point, any number of subsequent chunks may be joined up to this
162 * unidiff chunk by context lines or by being directly adjacent. */
163 chunk_context_get(&cc, result, i, context_lines);
164 debug("new chunk to be printed: chunk %d-%d left %d-%d right %d-%d\n",
165 cc.chunk.start, cc.chunk.end,
166 cc.left.start, cc.left.end,
167 cc.right.start, cc.right.end);
168 continue;
171 /* There already is a previous chunk noted down for being printed.
172 * Does it join up with this one? */
173 chunk_context_get(&next, result, i, context_lines);
174 debug("new chunk to be printed: chunk %d-%d left %d-%d right %d-%d\n",
175 next.chunk.start, next.chunk.end,
176 next.left.start, next.left.end,
177 next.right.start, next.right.end);
179 if (chunk_contexts_touch(&cc, &next)) {
180 /* This next context touches or overlaps the previous one, join. */
181 chunk_contexts_merge(&cc, &next);
182 debug("new chunk to be printed touches previous chunk, now: left %d-%d right %d-%d\n",
183 cc.left.start, cc.left.end,
184 cc.right.start, cc.right.end);
185 continue;
188 /* No touching, so the previous context is complete with a gap between it and
189 * this next one. Print the previous one and start fresh here. */
190 debug("new chunk to be printed does not touch previous chunk; print left %d-%d right %d-%d\n",
191 cc.left.start, cc.left.end, cc.right.start, cc.right.end);
192 diff_output_unidiff_chunk(dest, &header_printed, info, result, &cc);
193 cc = next;
194 debug("new unprinted chunk is left %d-%d right %d-%d\n",
195 cc.left.start, cc.left.end, cc.right.start, cc.right.end);
198 if (!chunk_context_empty(&cc))
199 diff_output_unidiff_chunk(dest, &header_printed, info, result, &cc);
200 return DIFF_RC_OK;