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