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 f068d119 2022-09-02 stsp static int
31 f068d119 2022-09-02 stsp output_plain_chunk(struct diff_output_info *outinfo,
32 f068d119 2022-09-02 stsp FILE *dest, const struct diff_input_info *info,
33 f068d119 2022-09-02 stsp const struct diff_result *result,
34 e72b0983 2022-09-03 stsp struct diff_chunk_context *cc, off_t *outoff, bool headers_only)
35 f068d119 2022-09-02 stsp {
36 e72b0983 2022-09-03 stsp off_t *offp;
37 f068d119 2022-09-02 stsp int left_start, left_len, right_start, right_len;
38 f068d119 2022-09-02 stsp int rc;
39 f068d119 2022-09-02 stsp bool change = false;
40 f068d119 2022-09-02 stsp
41 f068d119 2022-09-02 stsp left_len = cc->left.end - cc->left.start;
42 f068d119 2022-09-02 stsp if (left_len < 0)
43 f068d119 2022-09-02 stsp return EINVAL;
44 f068d119 2022-09-02 stsp else if (result->left->atoms.len == 0)
45 f068d119 2022-09-02 stsp left_start = 0;
46 f068d119 2022-09-02 stsp else if (left_len == 0 && cc->left.start > 0)
47 f068d119 2022-09-02 stsp left_start = cc->left.start;
48 f068d119 2022-09-02 stsp else if (cc->left.end > 0)
49 f068d119 2022-09-02 stsp left_start = cc->left.start + 1;
50 f068d119 2022-09-02 stsp else
51 f068d119 2022-09-02 stsp left_start = cc->left.start;
52 f068d119 2022-09-02 stsp
53 f068d119 2022-09-02 stsp right_len = cc->right.end - cc->right.start;
54 f068d119 2022-09-02 stsp if (right_len < 0)
55 f068d119 2022-09-02 stsp return EINVAL;
56 f068d119 2022-09-02 stsp else if (result->right->atoms.len == 0)
57 f068d119 2022-09-02 stsp right_start = 0;
58 f068d119 2022-09-02 stsp else if (right_len == 0 && cc->right.start > 0)
59 f068d119 2022-09-02 stsp right_start = cc->right.start;
60 f068d119 2022-09-02 stsp else if (cc->right.end > 0)
61 f068d119 2022-09-02 stsp right_start = cc->right.start + 1;
62 f068d119 2022-09-02 stsp else
63 f068d119 2022-09-02 stsp right_start = cc->right.start;
64 f068d119 2022-09-02 stsp
65 f068d119 2022-09-02 stsp if (left_len == 0) {
66 f068d119 2022-09-02 stsp /* addition */
67 f068d119 2022-09-02 stsp if (right_len == 1) {
68 f068d119 2022-09-02 stsp rc = fprintf(dest, "%da%d\n", left_start, right_start);
69 f068d119 2022-09-02 stsp } else {
70 f068d119 2022-09-02 stsp rc = fprintf(dest, "%da%d,%d\n", left_start,
71 f068d119 2022-09-02 stsp right_start, cc->right.end);
72 f068d119 2022-09-02 stsp }
73 f068d119 2022-09-02 stsp } else if (right_len == 0) {
74 f068d119 2022-09-02 stsp /* deletion */
75 f068d119 2022-09-02 stsp if (left_len == 1) {
76 f068d119 2022-09-02 stsp rc = fprintf(dest, "%dd%d\n", left_start,
77 f068d119 2022-09-02 stsp right_start);
78 f068d119 2022-09-02 stsp } else {
79 f068d119 2022-09-02 stsp rc = fprintf(dest, "%d,%dd%d\n", left_start,
80 f068d119 2022-09-02 stsp cc->left.end, right_start);
81 f068d119 2022-09-02 stsp }
82 f068d119 2022-09-02 stsp } else {
83 f068d119 2022-09-02 stsp /* change */
84 f068d119 2022-09-02 stsp change = true;
85 f068d119 2022-09-02 stsp if (left_len == 1 && right_len == 1) {
86 f068d119 2022-09-02 stsp rc = fprintf(dest, "%dc%d\n", left_start, right_start);
87 f068d119 2022-09-02 stsp } else if (left_len == 1) {
88 f068d119 2022-09-02 stsp rc = fprintf(dest, "%dc%d,%d\n", left_start,
89 f068d119 2022-09-02 stsp right_start, cc->right.end);
90 f068d119 2022-09-02 stsp } else if (right_len == 1) {
91 f068d119 2022-09-02 stsp rc = fprintf(dest, "%d,%dc%d\n", left_start,
92 f068d119 2022-09-02 stsp cc->left.end, right_start);
93 f068d119 2022-09-02 stsp } else {
94 f068d119 2022-09-02 stsp rc = fprintf(dest, "%d,%dc%d,%d\n", left_start,
95 f068d119 2022-09-02 stsp cc->left.end, right_start, cc->right.end);
96 f068d119 2022-09-02 stsp }
97 f068d119 2022-09-02 stsp }
98 e72b0983 2022-09-03 stsp if (rc < 0)
99 e72b0983 2022-09-03 stsp return errno;
100 e72b0983 2022-09-03 stsp if (outinfo) {
101 e72b0983 2022-09-03 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
102 e72b0983 2022-09-03 stsp if (offp == NULL)
103 e72b0983 2022-09-03 stsp return ENOMEM;
104 e72b0983 2022-09-03 stsp *outoff += rc;
105 e72b0983 2022-09-03 stsp *offp = *outoff;
106 e72b0983 2022-09-03 stsp }
107 f068d119 2022-09-02 stsp
108 f068d119 2022-09-02 stsp /*
109 f068d119 2022-09-02 stsp * Now write out all the joined chunks.
110 f068d119 2022-09-02 stsp *
111 f068d119 2022-09-02 stsp * If the hunk denotes a change, it will come in the form of a deletion
112 f068d119 2022-09-02 stsp * chunk followed by a addition chunk. Print a marker to break up the
113 f068d119 2022-09-02 stsp * additions and deletions when this happens.
114 f068d119 2022-09-02 stsp */
115 f068d119 2022-09-02 stsp int c_idx;
116 e72b0983 2022-09-03 stsp for (c_idx = cc->chunk.start; !headers_only && c_idx < cc->chunk.end;
117 e72b0983 2022-09-03 stsp c_idx++) {
118 f068d119 2022-09-02 stsp const struct diff_chunk *c = &result->chunks.head[c_idx];
119 f068d119 2022-09-02 stsp if (c->left_count && !c->right_count)
120 f068d119 2022-09-02 stsp rc = diff_output_lines(outinfo, dest,
121 f068d119 2022-09-02 stsp c->solved ? "< " : "?",
122 f068d119 2022-09-02 stsp c->left_start, c->left_count);
123 f068d119 2022-09-02 stsp else if (c->right_count && !c->left_count) {
124 e72b0983 2022-09-03 stsp if (change) {
125 e72b0983 2022-09-03 stsp rc = fprintf(dest, "---\n");
126 e72b0983 2022-09-03 stsp if (rc < 0)
127 e72b0983 2022-09-03 stsp return errno;
128 e72b0983 2022-09-03 stsp if (outinfo) {
129 e72b0983 2022-09-03 stsp ARRAYLIST_ADD(offp,
130 e72b0983 2022-09-03 stsp outinfo->line_offsets);
131 e72b0983 2022-09-03 stsp if (offp == NULL)
132 e72b0983 2022-09-03 stsp return ENOMEM;
133 e72b0983 2022-09-03 stsp *outoff += rc;
134 e72b0983 2022-09-03 stsp *offp = *outoff;
135 e72b0983 2022-09-03 stsp }
136 e72b0983 2022-09-03 stsp }
137 f068d119 2022-09-02 stsp rc = diff_output_lines(outinfo, dest,
138 f068d119 2022-09-02 stsp c->solved ? "> " : "?",
139 f068d119 2022-09-02 stsp c->right_start, c->right_count);
140 f068d119 2022-09-02 stsp }
141 f068d119 2022-09-02 stsp if (rc)
142 f068d119 2022-09-02 stsp return rc;
143 f068d119 2022-09-02 stsp if (cc->chunk.end == result->chunks.len) {
144 f068d119 2022-09-02 stsp rc = diff_output_trailing_newline_msg(outinfo, dest, c);
145 f068d119 2022-09-02 stsp if (rc != DIFF_RC_OK)
146 f068d119 2022-09-02 stsp return rc;
147 f068d119 2022-09-02 stsp }
148 f068d119 2022-09-02 stsp }
149 f068d119 2022-09-02 stsp
150 f068d119 2022-09-02 stsp return DIFF_RC_OK;
151 f068d119 2022-09-02 stsp }
152 f068d119 2022-09-02 stsp
153 fe621944 2020-11-10 stsp int
154 f068d119 2022-09-02 stsp diff_output_plain(struct diff_output_info **output_info,
155 f068d119 2022-09-02 stsp FILE *dest, const struct diff_input_info *info,
156 e72b0983 2022-09-03 stsp 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 f068d119 2022-09-02 stsp struct diff_chunk_context cc = {};
160 f068d119 2022-09-02 stsp int atomizer_flags = (result->left->atomizer_flags|
161 f068d119 2022-09-02 stsp result->right->atomizer_flags);
162 f068d119 2022-09-02 stsp int flags = (result->left->root->diff_flags |
163 f068d119 2022-09-02 stsp result->right->root->diff_flags);
164 f068d119 2022-09-02 stsp bool force_text = (flags & DIFF_FLAG_FORCE_TEXT_DATA);
165 f068d119 2022-09-02 stsp bool have_binary = (atomizer_flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
166 fe621944 2020-11-10 stsp int i, rc;
167 e72b0983 2022-09-03 stsp 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 f068d119 2022-09-02 stsp
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 f068d119 2022-09-02 stsp return ENOMEM;
178 fe621944 2020-11-10 stsp outinfo = *output_info;
179 fe621944 2020-11-10 stsp }
180 fe621944 2020-11-10 stsp
181 f068d119 2022-09-02 stsp if (have_binary && !force_text) {
182 f068d119 2022-09-02 stsp for (i = 0; i < result->chunks.len; i++) {
183 f068d119 2022-09-02 stsp struct diff_chunk *c = &result->chunks.head[i];
184 f068d119 2022-09-02 stsp enum diff_chunk_type t = diff_chunk_type(c);
185 f068d119 2022-09-02 stsp
186 f068d119 2022-09-02 stsp if (t != CHUNK_MINUS && t != CHUNK_PLUS)
187 f068d119 2022-09-02 stsp continue;
188 f068d119 2022-09-02 stsp
189 e72b0983 2022-09-03 stsp rc = fprintf(dest, "Binary files %s and %s differ\n",
190 f068d119 2022-09-02 stsp diff_output_get_label_left(info),
191 f068d119 2022-09-02 stsp diff_output_get_label_right(info));
192 e72b0983 2022-09-03 stsp if (rc < 0)
193 e72b0983 2022-09-03 stsp return errno;
194 e72b0983 2022-09-03 stsp if (outinfo) {
195 e72b0983 2022-09-03 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
196 e72b0983 2022-09-03 stsp if (offp == NULL)
197 e72b0983 2022-09-03 stsp return ENOMEM;
198 e72b0983 2022-09-03 stsp outoff += rc;
199 e72b0983 2022-09-03 stsp *offp = outoff;
200 e72b0983 2022-09-03 stsp }
201 f068d119 2022-09-02 stsp break;
202 f068d119 2022-09-02 stsp }
203 f068d119 2022-09-02 stsp
204 f068d119 2022-09-02 stsp return DIFF_RC_OK;
205 f068d119 2022-09-02 stsp }
206 f068d119 2022-09-02 stsp
207 fe621944 2020-11-10 stsp for (i = 0; i < result->chunks.len; i++) {
208 f068d119 2022-09-02 stsp struct diff_chunk *chunk = &result->chunks.head[i];
209 f068d119 2022-09-02 stsp enum diff_chunk_type t = diff_chunk_type(chunk);
210 f068d119 2022-09-02 stsp struct diff_chunk_context next;
211 f068d119 2022-09-02 stsp
212 f068d119 2022-09-02 stsp if (t != CHUNK_MINUS && t != CHUNK_PLUS)
213 f068d119 2022-09-02 stsp continue;
214 f068d119 2022-09-02 stsp
215 f068d119 2022-09-02 stsp if (diff_chunk_context_empty(&cc)) {
216 f068d119 2022-09-02 stsp /* Note down the start point, any number of subsequent
217 f068d119 2022-09-02 stsp * chunks may be joined up to this chunk by being
218 f068d119 2022-09-02 stsp * directly adjacent. */
219 f068d119 2022-09-02 stsp diff_chunk_context_get(&cc, result, i, 0);
220 f068d119 2022-09-02 stsp continue;
221 f068d119 2022-09-02 stsp }
222 f068d119 2022-09-02 stsp
223 f068d119 2022-09-02 stsp /* There already is a previous chunk noted down for being
224 f068d119 2022-09-02 stsp * printed. Does it join up with this one? */
225 f068d119 2022-09-02 stsp diff_chunk_context_get(&next, result, i, 0);
226 f068d119 2022-09-02 stsp
227 f068d119 2022-09-02 stsp if (diff_chunk_contexts_touch(&cc, &next)) {
228 f068d119 2022-09-02 stsp /* This next context touches or overlaps the previous
229 f068d119 2022-09-02 stsp * one, join. */
230 f068d119 2022-09-02 stsp diff_chunk_contexts_merge(&cc, &next);
231 f068d119 2022-09-02 stsp /* When we merge the last chunk we can end up with one
232 f068d119 2022-09-02 stsp * hanging chunk and have to come back for it after the
233 f068d119 2022-09-02 stsp * loop */
234 f068d119 2022-09-02 stsp continue;
235 f068d119 2022-09-02 stsp }
236 e72b0983 2022-09-03 stsp rc = output_plain_chunk(outinfo, dest, info, result, &cc,
237 e72b0983 2022-09-03 stsp &outoff, hunk_headers_only);
238 f068d119 2022-09-02 stsp if (rc != DIFF_RC_OK)
239 fe621944 2020-11-10 stsp return rc;
240 f068d119 2022-09-02 stsp cc = next;
241 fe621944 2020-11-10 stsp }
242 f068d119 2022-09-02 stsp if (!diff_chunk_context_empty(&cc))
243 e72b0983 2022-09-03 stsp return output_plain_chunk(outinfo, dest, info, result, &cc,
244 e72b0983 2022-09-03 stsp &outoff, hunk_headers_only);
245 fe621944 2020-11-10 stsp return DIFF_RC_OK;
246 fe621944 2020-11-10 stsp }