Blame


1 fe621944 2020-11-10 stsp /* Output all lines of a diff_result. */
2 fe621944 2020-11-10 stsp /*
3 fe621944 2020-11-10 stsp * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
4 fe621944 2020-11-10 stsp *
5 fe621944 2020-11-10 stsp * Permission to use, copy, modify, and distribute this software for any
6 fe621944 2020-11-10 stsp * purpose with or without fee is hereby granted, provided that the above
7 fe621944 2020-11-10 stsp * copyright notice and this permission notice appear in all copies.
8 fe621944 2020-11-10 stsp *
9 fe621944 2020-11-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 fe621944 2020-11-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 fe621944 2020-11-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 fe621944 2020-11-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 fe621944 2020-11-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 fe621944 2020-11-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 fe621944 2020-11-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 fe621944 2020-11-10 stsp */
17 fe621944 2020-11-10 stsp
18 fe621944 2020-11-10 stsp #include <errno.h>
19 f3c44083 2020-11-14 naddy #include <stdint.h>
20 fe621944 2020-11-10 stsp #include <stdio.h>
21 fe621944 2020-11-10 stsp #include <stdbool.h>
22 fe621944 2020-11-10 stsp #include <stdlib.h>
23 fe621944 2020-11-10 stsp
24 fe621944 2020-11-10 stsp #include <arraylist.h>
25 fe621944 2020-11-10 stsp #include <diff_main.h>
26 fe621944 2020-11-10 stsp #include <diff_output.h>
27 fe621944 2020-11-10 stsp
28 fe621944 2020-11-10 stsp #include "diff_internal.h"
29 fe621944 2020-11-10 stsp
30 4b083231 2022-09-02 thomas static int
31 4b083231 2022-09-02 thomas output_plain_chunk(struct diff_output_info *outinfo,
32 4b083231 2022-09-02 thomas FILE *dest, const struct diff_input_info *info,
33 4b083231 2022-09-02 thomas const struct diff_result *result,
34 7a800a02 2022-09-03 thomas struct diff_chunk_context *cc, off_t *outoff, bool headers_only)
35 4b083231 2022-09-02 thomas {
36 7a800a02 2022-09-03 thomas off_t *offp;
37 4b083231 2022-09-02 thomas int left_start, left_len, right_start, right_len;
38 4b083231 2022-09-02 thomas int rc;
39 4b083231 2022-09-02 thomas bool change = false;
40 4b083231 2022-09-02 thomas
41 4b083231 2022-09-02 thomas left_len = cc->left.end - cc->left.start;
42 4b083231 2022-09-02 thomas if (left_len < 0)
43 4b083231 2022-09-02 thomas return EINVAL;
44 4b083231 2022-09-02 thomas else if (result->left->atoms.len == 0)
45 4b083231 2022-09-02 thomas left_start = 0;
46 4b083231 2022-09-02 thomas else if (left_len == 0 && cc->left.start > 0)
47 4b083231 2022-09-02 thomas left_start = cc->left.start;
48 4b083231 2022-09-02 thomas else if (cc->left.end > 0)
49 4b083231 2022-09-02 thomas left_start = cc->left.start + 1;
50 4b083231 2022-09-02 thomas else
51 4b083231 2022-09-02 thomas left_start = cc->left.start;
52 4b083231 2022-09-02 thomas
53 4b083231 2022-09-02 thomas right_len = cc->right.end - cc->right.start;
54 4b083231 2022-09-02 thomas if (right_len < 0)
55 4b083231 2022-09-02 thomas return EINVAL;
56 4b083231 2022-09-02 thomas else if (result->right->atoms.len == 0)
57 4b083231 2022-09-02 thomas right_start = 0;
58 4b083231 2022-09-02 thomas else if (right_len == 0 && cc->right.start > 0)
59 4b083231 2022-09-02 thomas right_start = cc->right.start;
60 4b083231 2022-09-02 thomas else if (cc->right.end > 0)
61 4b083231 2022-09-02 thomas right_start = cc->right.start + 1;
62 4b083231 2022-09-02 thomas else
63 4b083231 2022-09-02 thomas right_start = cc->right.start;
64 4b083231 2022-09-02 thomas
65 4b083231 2022-09-02 thomas if (left_len == 0) {
66 4b083231 2022-09-02 thomas /* addition */
67 4b083231 2022-09-02 thomas if (right_len == 1) {
68 4b083231 2022-09-02 thomas rc = fprintf(dest, "%da%d\n", left_start, right_start);
69 4b083231 2022-09-02 thomas } else {
70 4b083231 2022-09-02 thomas rc = fprintf(dest, "%da%d,%d\n", left_start,
71 4b083231 2022-09-02 thomas right_start, cc->right.end);
72 4b083231 2022-09-02 thomas }
73 4b083231 2022-09-02 thomas } else if (right_len == 0) {
74 4b083231 2022-09-02 thomas /* deletion */
75 4b083231 2022-09-02 thomas if (left_len == 1) {
76 4b083231 2022-09-02 thomas rc = fprintf(dest, "%dd%d\n", left_start,
77 4b083231 2022-09-02 thomas right_start);
78 4b083231 2022-09-02 thomas } else {
79 4b083231 2022-09-02 thomas rc = fprintf(dest, "%d,%dd%d\n", left_start,
80 4b083231 2022-09-02 thomas cc->left.end, right_start);
81 4b083231 2022-09-02 thomas }
82 4b083231 2022-09-02 thomas } else {
83 4b083231 2022-09-02 thomas /* change */
84 4b083231 2022-09-02 thomas change = true;
85 4b083231 2022-09-02 thomas if (left_len == 1 && right_len == 1) {
86 4b083231 2022-09-02 thomas rc = fprintf(dest, "%dc%d\n", left_start, right_start);
87 4b083231 2022-09-02 thomas } else if (left_len == 1) {
88 4b083231 2022-09-02 thomas rc = fprintf(dest, "%dc%d,%d\n", left_start,
89 4b083231 2022-09-02 thomas right_start, cc->right.end);
90 4b083231 2022-09-02 thomas } else if (right_len == 1) {
91 4b083231 2022-09-02 thomas rc = fprintf(dest, "%d,%dc%d\n", left_start,
92 4b083231 2022-09-02 thomas cc->left.end, right_start);
93 4b083231 2022-09-02 thomas } else {
94 4b083231 2022-09-02 thomas rc = fprintf(dest, "%d,%dc%d,%d\n", left_start,
95 4b083231 2022-09-02 thomas cc->left.end, right_start, cc->right.end);
96 4b083231 2022-09-02 thomas }
97 4b083231 2022-09-02 thomas }
98 7a800a02 2022-09-03 thomas if (rc < 0)
99 7a800a02 2022-09-03 thomas return errno;
100 7a800a02 2022-09-03 thomas if (outinfo) {
101 7a800a02 2022-09-03 thomas ARRAYLIST_ADD(offp, outinfo->line_offsets);
102 7a800a02 2022-09-03 thomas if (offp == NULL)
103 7a800a02 2022-09-03 thomas return ENOMEM;
104 7a800a02 2022-09-03 thomas *outoff += rc;
105 7a800a02 2022-09-03 thomas *offp = *outoff;
106 7a800a02 2022-09-03 thomas }
107 4b083231 2022-09-02 thomas
108 4b083231 2022-09-02 thomas /*
109 4b083231 2022-09-02 thomas * Now write out all the joined chunks.
110 4b083231 2022-09-02 thomas *
111 4b083231 2022-09-02 thomas * If the hunk denotes a change, it will come in the form of a deletion
112 4b083231 2022-09-02 thomas * chunk followed by a addition chunk. Print a marker to break up the
113 4b083231 2022-09-02 thomas * additions and deletions when this happens.
114 4b083231 2022-09-02 thomas */
115 4b083231 2022-09-02 thomas int c_idx;
116 7a800a02 2022-09-03 thomas for (c_idx = cc->chunk.start; !headers_only && c_idx < cc->chunk.end;
117 7a800a02 2022-09-03 thomas c_idx++) {
118 4b083231 2022-09-02 thomas const struct diff_chunk *c = &result->chunks.head[c_idx];
119 4b083231 2022-09-02 thomas if (c->left_count && !c->right_count)
120 4b083231 2022-09-02 thomas rc = diff_output_lines(outinfo, dest,
121 4b083231 2022-09-02 thomas c->solved ? "< " : "?",
122 4b083231 2022-09-02 thomas c->left_start, c->left_count);
123 4b083231 2022-09-02 thomas else if (c->right_count && !c->left_count) {
124 7a800a02 2022-09-03 thomas if (change) {
125 7a800a02 2022-09-03 thomas rc = fprintf(dest, "---\n");
126 7a800a02 2022-09-03 thomas if (rc < 0)
127 7a800a02 2022-09-03 thomas return errno;
128 7a800a02 2022-09-03 thomas if (outinfo) {
129 7a800a02 2022-09-03 thomas ARRAYLIST_ADD(offp,
130 7a800a02 2022-09-03 thomas outinfo->line_offsets);
131 7a800a02 2022-09-03 thomas if (offp == NULL)
132 7a800a02 2022-09-03 thomas return ENOMEM;
133 7a800a02 2022-09-03 thomas *outoff += rc;
134 7a800a02 2022-09-03 thomas *offp = *outoff;
135 7a800a02 2022-09-03 thomas }
136 7a800a02 2022-09-03 thomas }
137 4b083231 2022-09-02 thomas rc = diff_output_lines(outinfo, dest,
138 4b083231 2022-09-02 thomas c->solved ? "> " : "?",
139 4b083231 2022-09-02 thomas c->right_start, c->right_count);
140 4b083231 2022-09-02 thomas }
141 4b083231 2022-09-02 thomas if (rc)
142 4b083231 2022-09-02 thomas return rc;
143 4b083231 2022-09-02 thomas if (cc->chunk.end == result->chunks.len) {
144 4b083231 2022-09-02 thomas rc = diff_output_trailing_newline_msg(outinfo, dest, c);
145 4b083231 2022-09-02 thomas if (rc != DIFF_RC_OK)
146 4b083231 2022-09-02 thomas return rc;
147 4b083231 2022-09-02 thomas }
148 4b083231 2022-09-02 thomas }
149 4b083231 2022-09-02 thomas
150 4b083231 2022-09-02 thomas return DIFF_RC_OK;
151 4b083231 2022-09-02 thomas }
152 4b083231 2022-09-02 thomas
153 fe621944 2020-11-10 stsp int
154 4b083231 2022-09-02 thomas diff_output_plain(struct diff_output_info **output_info,
155 4b083231 2022-09-02 thomas FILE *dest, const struct diff_input_info *info,
156 7a800a02 2022-09-03 thomas const struct diff_result *result, int hunk_headers_only)
157 fe621944 2020-11-10 stsp {
158 fe621944 2020-11-10 stsp struct diff_output_info *outinfo = NULL;
159 4b083231 2022-09-02 thomas struct diff_chunk_context cc = {};
160 4b083231 2022-09-02 thomas int atomizer_flags = (result->left->atomizer_flags|
161 4b083231 2022-09-02 thomas result->right->atomizer_flags);
162 4b083231 2022-09-02 thomas int flags = (result->left->root->diff_flags |
163 4b083231 2022-09-02 thomas result->right->root->diff_flags);
164 4b083231 2022-09-02 thomas bool force_text = (flags & DIFF_FLAG_FORCE_TEXT_DATA);
165 4b083231 2022-09-02 thomas bool have_binary = (atomizer_flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
166 fe621944 2020-11-10 stsp int i, rc;
167 7a800a02 2022-09-03 thomas off_t outoff = 0, *offp;
168 fe621944 2020-11-10 stsp
169 fe621944 2020-11-10 stsp if (!result)
170 fe621944 2020-11-10 stsp return EINVAL;
171 fe621944 2020-11-10 stsp if (result->rc != DIFF_RC_OK)
172 fe621944 2020-11-10 stsp return result->rc;
173 4b083231 2022-09-02 thomas
174 fe621944 2020-11-10 stsp if (output_info) {
175 fe621944 2020-11-10 stsp *output_info = diff_output_info_alloc();
176 fe621944 2020-11-10 stsp if (*output_info == NULL)
177 4b083231 2022-09-02 thomas return ENOMEM;
178 fe621944 2020-11-10 stsp outinfo = *output_info;
179 fe621944 2020-11-10 stsp }
180 fe621944 2020-11-10 stsp
181 4b083231 2022-09-02 thomas if (have_binary && !force_text) {
182 4b083231 2022-09-02 thomas for (i = 0; i < result->chunks.len; i++) {
183 4b083231 2022-09-02 thomas struct diff_chunk *c = &result->chunks.head[i];
184 4b083231 2022-09-02 thomas enum diff_chunk_type t = diff_chunk_type(c);
185 4b083231 2022-09-02 thomas
186 4b083231 2022-09-02 thomas if (t != CHUNK_MINUS && t != CHUNK_PLUS)
187 4b083231 2022-09-02 thomas continue;
188 4b083231 2022-09-02 thomas
189 7a800a02 2022-09-03 thomas rc = fprintf(dest, "Binary files %s and %s differ\n",
190 4b083231 2022-09-02 thomas diff_output_get_label_left(info),
191 4b083231 2022-09-02 thomas diff_output_get_label_right(info));
192 7a800a02 2022-09-03 thomas if (rc < 0)
193 7a800a02 2022-09-03 thomas return errno;
194 7a800a02 2022-09-03 thomas if (outinfo) {
195 7a800a02 2022-09-03 thomas ARRAYLIST_ADD(offp, outinfo->line_offsets);
196 7a800a02 2022-09-03 thomas if (offp == NULL)
197 7a800a02 2022-09-03 thomas return ENOMEM;
198 7a800a02 2022-09-03 thomas outoff += rc;
199 7a800a02 2022-09-03 thomas *offp = outoff;
200 7a800a02 2022-09-03 thomas }
201 4b083231 2022-09-02 thomas break;
202 4b083231 2022-09-02 thomas }
203 4b083231 2022-09-02 thomas
204 4b083231 2022-09-02 thomas return DIFF_RC_OK;
205 4b083231 2022-09-02 thomas }
206 4b083231 2022-09-02 thomas
207 fe621944 2020-11-10 stsp for (i = 0; i < result->chunks.len; i++) {
208 4b083231 2022-09-02 thomas struct diff_chunk *chunk = &result->chunks.head[i];
209 4b083231 2022-09-02 thomas enum diff_chunk_type t = diff_chunk_type(chunk);
210 4b083231 2022-09-02 thomas struct diff_chunk_context next;
211 4b083231 2022-09-02 thomas
212 4b083231 2022-09-02 thomas if (t != CHUNK_MINUS && t != CHUNK_PLUS)
213 4b083231 2022-09-02 thomas continue;
214 4b083231 2022-09-02 thomas
215 4b083231 2022-09-02 thomas if (diff_chunk_context_empty(&cc)) {
216 4b083231 2022-09-02 thomas /* Note down the start point, any number of subsequent
217 4b083231 2022-09-02 thomas * chunks may be joined up to this chunk by being
218 4b083231 2022-09-02 thomas * directly adjacent. */
219 4b083231 2022-09-02 thomas diff_chunk_context_get(&cc, result, i, 0);
220 4b083231 2022-09-02 thomas continue;
221 4b083231 2022-09-02 thomas }
222 4b083231 2022-09-02 thomas
223 4b083231 2022-09-02 thomas /* There already is a previous chunk noted down for being
224 4b083231 2022-09-02 thomas * printed. Does it join up with this one? */
225 4b083231 2022-09-02 thomas diff_chunk_context_get(&next, result, i, 0);
226 4b083231 2022-09-02 thomas
227 4b083231 2022-09-02 thomas if (diff_chunk_contexts_touch(&cc, &next)) {
228 4b083231 2022-09-02 thomas /* This next context touches or overlaps the previous
229 4b083231 2022-09-02 thomas * one, join. */
230 4b083231 2022-09-02 thomas diff_chunk_contexts_merge(&cc, &next);
231 4b083231 2022-09-02 thomas /* When we merge the last chunk we can end up with one
232 4b083231 2022-09-02 thomas * hanging chunk and have to come back for it after the
233 4b083231 2022-09-02 thomas * loop */
234 4b083231 2022-09-02 thomas continue;
235 4b083231 2022-09-02 thomas }
236 7a800a02 2022-09-03 thomas rc = output_plain_chunk(outinfo, dest, info, result, &cc,
237 7a800a02 2022-09-03 thomas &outoff, hunk_headers_only);
238 4b083231 2022-09-02 thomas if (rc != DIFF_RC_OK)
239 fe621944 2020-11-10 stsp return rc;
240 4b083231 2022-09-02 thomas cc = next;
241 fe621944 2020-11-10 stsp }
242 4b083231 2022-09-02 thomas if (!diff_chunk_context_empty(&cc))
243 7a800a02 2022-09-03 thomas return output_plain_chunk(outinfo, dest, info, result, &cc,
244 7a800a02 2022-09-03 thomas &outoff, hunk_headers_only);
245 fe621944 2020-11-10 stsp return DIFF_RC_OK;
246 fe621944 2020-11-10 stsp }