Blame


1 c672cd52 2020-10-30 neels /*
2 c672cd52 2020-10-30 neels * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 c672cd52 2020-10-30 neels *
4 c672cd52 2020-10-30 neels * Permission to use, copy, modify, and distribute this software for any
5 c672cd52 2020-10-30 neels * purpose with or without fee is hereby granted, provided that the above
6 c672cd52 2020-10-30 neels * copyright notice and this permission notice appear in all copies.
7 c672cd52 2020-10-30 neels *
8 c672cd52 2020-10-30 neels * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 c672cd52 2020-10-30 neels * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 c672cd52 2020-10-30 neels * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 c672cd52 2020-10-30 neels * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 c672cd52 2020-10-30 neels * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 c672cd52 2020-10-30 neels * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 c672cd52 2020-10-30 neels * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 c672cd52 2020-10-30 neels */
16 c672cd52 2020-10-30 neels
17 c672cd52 2020-10-30 neels #include <sys/queue.h>
18 c672cd52 2020-10-30 neels #include <sys/stat.h>
19 c672cd52 2020-10-30 neels
20 c672cd52 2020-10-30 neels #include <stdbool.h>
21 c672cd52 2020-10-30 neels #include <stdio.h>
22 c672cd52 2020-10-30 neels #include <stdlib.h>
23 c672cd52 2020-10-30 neels #include <string.h>
24 c672cd52 2020-10-30 neels #include <limits.h>
25 c672cd52 2020-10-30 neels #include <sha1.h>
26 c672cd52 2020-10-30 neels #include <zlib.h>
27 c672cd52 2020-10-30 neels
28 c672cd52 2020-10-30 neels #include "got_object.h"
29 c672cd52 2020-10-30 neels #include "got_repository.h"
30 c672cd52 2020-10-30 neels #include "got_error.h"
31 c672cd52 2020-10-30 neels #include "got_diff.h"
32 c672cd52 2020-10-30 neels #include "got_opentemp.h"
33 c672cd52 2020-10-30 neels #include "got_path.h"
34 c672cd52 2020-10-30 neels #include "got_cancel.h"
35 c672cd52 2020-10-30 neels #include "got_worktree.h"
36 c672cd52 2020-10-30 neels
37 c672cd52 2020-10-30 neels #include "got_lib_diff.h"
38 c672cd52 2020-10-30 neels #include "got_lib_delta.h"
39 c672cd52 2020-10-30 neels #include "got_lib_inflate.h"
40 c672cd52 2020-10-30 neels #include "got_lib_object.h"
41 c672cd52 2020-10-30 neels
42 c672cd52 2020-10-30 neels static const struct got_error *
43 c672cd52 2020-10-30 neels add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
44 c672cd52 2020-10-30 neels {
45 c672cd52 2020-10-30 neels off_t *p;
46 c672cd52 2020-10-30 neels
47 c672cd52 2020-10-30 neels p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
48 c672cd52 2020-10-30 neels if (p == NULL)
49 c672cd52 2020-10-30 neels return got_error_from_errno("reallocarray");
50 c672cd52 2020-10-30 neels *line_offsets = p;
51 c672cd52 2020-10-30 neels (*line_offsets)[*nlines] = off;
52 c672cd52 2020-10-30 neels (*nlines)++;
53 c672cd52 2020-10-30 neels return NULL;
54 c672cd52 2020-10-30 neels }
55 c672cd52 2020-10-30 neels
56 c672cd52 2020-10-30 neels static const struct got_error *
57 c672cd52 2020-10-30 neels diff_blobs(off_t **line_offsets, size_t *nlines,
58 c672cd52 2020-10-30 neels struct got_diffreg_result **resultp, struct got_blob_object *blob1,
59 c672cd52 2020-10-30 neels struct got_blob_object *blob2,
60 c672cd52 2020-10-30 neels const char *label1, const char *label2, mode_t mode1, mode_t mode2,
61 c672cd52 2020-10-30 neels int diff_context, int ignore_whitespace, FILE *outfile)
62 c672cd52 2020-10-30 neels {
63 c672cd52 2020-10-30 neels const struct got_error *err = NULL, *free_err;
64 c672cd52 2020-10-30 neels FILE *f1 = NULL, *f2 = NULL;
65 c672cd52 2020-10-30 neels char hex1[SHA1_DIGEST_STRING_LENGTH];
66 c672cd52 2020-10-30 neels char hex2[SHA1_DIGEST_STRING_LENGTH];
67 c672cd52 2020-10-30 neels char *idstr1 = NULL, *idstr2 = NULL;
68 c672cd52 2020-10-30 neels size_t size1, size2;
69 c672cd52 2020-10-30 neels struct got_diffreg_result *result;
70 c672cd52 2020-10-30 neels off_t outoff = 0;
71 c672cd52 2020-10-30 neels int n;
72 c672cd52 2020-10-30 neels
73 c672cd52 2020-10-30 neels if (line_offsets && *line_offsets && *nlines > 0)
74 c672cd52 2020-10-30 neels outoff = (*line_offsets)[*nlines - 1];
75 c672cd52 2020-10-30 neels
76 c672cd52 2020-10-30 neels if (resultp)
77 c672cd52 2020-10-30 neels *resultp = NULL;
78 c672cd52 2020-10-30 neels
79 c672cd52 2020-10-30 neels if (blob1) {
80 c672cd52 2020-10-30 neels f1 = got_opentemp();
81 c672cd52 2020-10-30 neels if (f1 == NULL)
82 c672cd52 2020-10-30 neels return got_error_from_errno("got_opentemp");
83 c672cd52 2020-10-30 neels }
84 c672cd52 2020-10-30 neels
85 c672cd52 2020-10-30 neels if (blob2) {
86 c672cd52 2020-10-30 neels f2 = got_opentemp();
87 c672cd52 2020-10-30 neels if (f2 == NULL) {
88 c672cd52 2020-10-30 neels err = got_error_from_errno("got_opentemp");
89 c672cd52 2020-10-30 neels fclose(f1);
90 c672cd52 2020-10-30 neels return err;
91 c672cd52 2020-10-30 neels }
92 c672cd52 2020-10-30 neels }
93 c672cd52 2020-10-30 neels
94 c672cd52 2020-10-30 neels size1 = 0;
95 c672cd52 2020-10-30 neels if (blob1) {
96 c672cd52 2020-10-30 neels idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
97 c672cd52 2020-10-30 neels err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
98 c672cd52 2020-10-30 neels blob1);
99 c672cd52 2020-10-30 neels if (err)
100 c672cd52 2020-10-30 neels goto done;
101 c672cd52 2020-10-30 neels } else
102 c672cd52 2020-10-30 neels idstr1 = "/dev/null";
103 c672cd52 2020-10-30 neels
104 c672cd52 2020-10-30 neels size2 = 0;
105 c672cd52 2020-10-30 neels if (blob2) {
106 c672cd52 2020-10-30 neels idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
107 c672cd52 2020-10-30 neels err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
108 c672cd52 2020-10-30 neels blob2);
109 c672cd52 2020-10-30 neels if (err)
110 c672cd52 2020-10-30 neels goto done;
111 c672cd52 2020-10-30 neels } else
112 c672cd52 2020-10-30 neels idstr2 = "/dev/null";
113 c672cd52 2020-10-30 neels
114 c672cd52 2020-10-30 neels if (outfile) {
115 c672cd52 2020-10-30 neels char *modestr1 = NULL, *modestr2 = NULL;
116 c672cd52 2020-10-30 neels int modebits;
117 c672cd52 2020-10-30 neels if (mode1 && mode1 != mode2) {
118 c672cd52 2020-10-30 neels if (S_ISLNK(mode1))
119 c672cd52 2020-10-30 neels modebits = S_IFLNK;
120 c672cd52 2020-10-30 neels else
121 c672cd52 2020-10-30 neels modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
122 c672cd52 2020-10-30 neels if (asprintf(&modestr1, " (mode %o)",
123 c672cd52 2020-10-30 neels mode1 & modebits) == -1) {
124 c672cd52 2020-10-30 neels err = got_error_from_errno("asprintf");
125 c672cd52 2020-10-30 neels goto done;
126 c672cd52 2020-10-30 neels }
127 c672cd52 2020-10-30 neels }
128 c672cd52 2020-10-30 neels if (mode2 && mode1 != mode2) {
129 c672cd52 2020-10-30 neels if (S_ISLNK(mode2))
130 c672cd52 2020-10-30 neels modebits = S_IFLNK;
131 c672cd52 2020-10-30 neels else
132 c672cd52 2020-10-30 neels modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
133 c672cd52 2020-10-30 neels if (asprintf(&modestr2, " (mode %o)",
134 c672cd52 2020-10-30 neels mode2 & modebits) == -1) {
135 c672cd52 2020-10-30 neels err = got_error_from_errno("asprintf");
136 c672cd52 2020-10-30 neels goto done;
137 c672cd52 2020-10-30 neels }
138 c672cd52 2020-10-30 neels }
139 c672cd52 2020-10-30 neels n = fprintf(outfile, "blob - %s%s\n", idstr1,
140 c672cd52 2020-10-30 neels modestr1 ? modestr1 : "");
141 c672cd52 2020-10-30 neels if (n < 0) {
142 c672cd52 2020-10-30 neels err = got_error_from_errno("fprintf");
143 c672cd52 2020-10-30 neels goto done;
144 c672cd52 2020-10-30 neels }
145 c672cd52 2020-10-30 neels outoff += n;
146 c672cd52 2020-10-30 neels if (line_offsets) {
147 c672cd52 2020-10-30 neels err = add_line_offset(line_offsets, nlines, outoff);
148 c672cd52 2020-10-30 neels if (err)
149 c672cd52 2020-10-30 neels goto done;
150 c672cd52 2020-10-30 neels }
151 c672cd52 2020-10-30 neels
152 c672cd52 2020-10-30 neels n = fprintf(outfile, "blob + %s%s\n", idstr2,
153 c672cd52 2020-10-30 neels modestr2 ? modestr2 : "");
154 c672cd52 2020-10-30 neels if (n < 0) {
155 c672cd52 2020-10-30 neels err = got_error_from_errno("fprintf");
156 c672cd52 2020-10-30 neels goto done;
157 c672cd52 2020-10-30 neels }
158 c672cd52 2020-10-30 neels outoff += n;
159 c672cd52 2020-10-30 neels if (line_offsets) {
160 c672cd52 2020-10-30 neels err = add_line_offset(line_offsets, nlines, outoff);
161 c672cd52 2020-10-30 neels if (err)
162 c672cd52 2020-10-30 neels goto done;
163 c672cd52 2020-10-30 neels }
164 c672cd52 2020-10-30 neels
165 c672cd52 2020-10-30 neels free(modestr1);
166 c672cd52 2020-10-30 neels free(modestr2);
167 c672cd52 2020-10-30 neels }
168 c672cd52 2020-10-30 neels err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_MYERS,
169 c672cd52 2020-10-30 neels ignore_whitespace);
170 c672cd52 2020-10-30 neels if (err)
171 c672cd52 2020-10-30 neels goto done;
172 c672cd52 2020-10-30 neels
173 c672cd52 2020-10-30 neels if (outfile) {
174 c672cd52 2020-10-30 neels err = got_diffreg_output(line_offsets, nlines, result, f1, f2,
175 c672cd52 2020-10-30 neels label1 ? label1 : idstr1,
176 c672cd52 2020-10-30 neels label2 ? label2 : idstr2,
177 c672cd52 2020-10-30 neels GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
178 c672cd52 2020-10-30 neels if (err)
179 c672cd52 2020-10-30 neels goto done;
180 c672cd52 2020-10-30 neels }
181 c672cd52 2020-10-30 neels
182 c672cd52 2020-10-30 neels if (resultp && err == NULL)
183 c672cd52 2020-10-30 neels *resultp = result;
184 c672cd52 2020-10-30 neels else {
185 c672cd52 2020-10-30 neels free_err = got_diffreg_result_free(result);
186 c672cd52 2020-10-30 neels if (free_err && err == NULL)
187 c672cd52 2020-10-30 neels err = free_err;
188 c672cd52 2020-10-30 neels }
189 c672cd52 2020-10-30 neels done:
190 c672cd52 2020-10-30 neels if (f1 && fclose(f1) != 0 && err == NULL)
191 c672cd52 2020-10-30 neels err = got_error_from_errno("fclose");
192 c672cd52 2020-10-30 neels if (f2 && fclose(f2) != 0 && err == NULL)
193 c672cd52 2020-10-30 neels err = got_error_from_errno("fclose");
194 c672cd52 2020-10-30 neels return err;
195 c672cd52 2020-10-30 neels }
196 c672cd52 2020-10-30 neels
197 c672cd52 2020-10-30 neels const struct got_error *
198 c672cd52 2020-10-30 neels got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
199 c672cd52 2020-10-30 neels struct got_blob_object *blob2, struct got_object_id *id1,
200 c672cd52 2020-10-30 neels struct got_object_id *id2, const char *label1, const char *label2,
201 c672cd52 2020-10-30 neels mode_t mode1, mode_t mode2, struct got_repository *repo)
202 c672cd52 2020-10-30 neels {
203 c672cd52 2020-10-30 neels struct got_diff_blob_output_unidiff_arg *a = arg;
204 c672cd52 2020-10-30 neels
205 c672cd52 2020-10-30 neels return diff_blobs(&a->line_offsets, &a->nlines, NULL,
206 c672cd52 2020-10-30 neels blob1, blob2, label1, label2, mode1, mode2, a->diff_context,
207 c672cd52 2020-10-30 neels a->ignore_whitespace, a->outfile);
208 c672cd52 2020-10-30 neels }
209 c672cd52 2020-10-30 neels
210 c672cd52 2020-10-30 neels const struct got_error *
211 c672cd52 2020-10-30 neels got_diff_blob(off_t **line_offsets, size_t *nlines,
212 c672cd52 2020-10-30 neels struct got_blob_object *blob1, struct got_blob_object *blob2,
213 c672cd52 2020-10-30 neels const char *label1, const char *label2, int diff_context,
214 c672cd52 2020-10-30 neels int ignore_whitespace, FILE *outfile)
215 c672cd52 2020-10-30 neels {
216 c672cd52 2020-10-30 neels return diff_blobs(line_offsets, nlines, NULL, blob1, blob2,
217 c672cd52 2020-10-30 neels label1, label2, 0, 0, diff_context, ignore_whitespace, outfile);
218 c672cd52 2020-10-30 neels }
219 c672cd52 2020-10-30 neels
220 c672cd52 2020-10-30 neels static const struct got_error *
221 c672cd52 2020-10-30 neels diff_blob_file(struct got_diffreg_result **resultp,
222 c672cd52 2020-10-30 neels struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
223 c672cd52 2020-10-30 neels const char *label2, int diff_context, int ignore_whitespace, FILE *outfile)
224 c672cd52 2020-10-30 neels {
225 c672cd52 2020-10-30 neels const struct got_error *err = NULL, *free_err;
226 c672cd52 2020-10-30 neels FILE *f1 = NULL;
227 c672cd52 2020-10-30 neels char hex1[SHA1_DIGEST_STRING_LENGTH];
228 c672cd52 2020-10-30 neels char *idstr1 = NULL;
229 c672cd52 2020-10-30 neels size_t size1;
230 c672cd52 2020-10-30 neels struct got_diffreg_result *result = NULL;
231 c672cd52 2020-10-30 neels
232 c672cd52 2020-10-30 neels if (resultp)
233 c672cd52 2020-10-30 neels *resultp = NULL;
234 c672cd52 2020-10-30 neels
235 c672cd52 2020-10-30 neels size1 = 0;
236 c672cd52 2020-10-30 neels if (blob1) {
237 c672cd52 2020-10-30 neels f1 = got_opentemp();
238 c672cd52 2020-10-30 neels if (f1 == NULL)
239 c672cd52 2020-10-30 neels return got_error_from_errno("got_opentemp");
240 c672cd52 2020-10-30 neels idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
241 c672cd52 2020-10-30 neels err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
242 c672cd52 2020-10-30 neels blob1);
243 c672cd52 2020-10-30 neels if (err)
244 c672cd52 2020-10-30 neels goto done;
245 c672cd52 2020-10-30 neels } else {
246 c672cd52 2020-10-30 neels idstr1 = "/dev/null";
247 c672cd52 2020-10-30 neels }
248 c672cd52 2020-10-30 neels
249 c672cd52 2020-10-30 neels if (outfile) {
250 c672cd52 2020-10-30 neels fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
251 c672cd52 2020-10-30 neels fprintf(outfile, "file + %s\n",
252 c672cd52 2020-10-30 neels f2 == NULL ? "/dev/null" : label2);
253 c672cd52 2020-10-30 neels }
254 c672cd52 2020-10-30 neels
255 c672cd52 2020-10-30 neels err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_MYERS,
256 c672cd52 2020-10-30 neels ignore_whitespace);
257 c672cd52 2020-10-30 neels if (err)
258 c672cd52 2020-10-30 neels goto done;
259 c672cd52 2020-10-30 neels
260 c672cd52 2020-10-30 neels if (outfile) {
261 c672cd52 2020-10-30 neels err = got_diffreg_output(NULL, NULL, result, f1, f2,
262 c672cd52 2020-10-30 neels label2, label2, GOT_DIFF_OUTPUT_UNIDIFF, diff_context,
263 c672cd52 2020-10-30 neels outfile);
264 c672cd52 2020-10-30 neels if (err)
265 c672cd52 2020-10-30 neels goto done;
266 c672cd52 2020-10-30 neels }
267 c672cd52 2020-10-30 neels
268 c672cd52 2020-10-30 neels if (resultp && err == NULL)
269 c672cd52 2020-10-30 neels *resultp = result;
270 c672cd52 2020-10-30 neels else if (result) {
271 c672cd52 2020-10-30 neels free_err = got_diffreg_result_free(result);
272 c672cd52 2020-10-30 neels if (free_err && err == NULL)
273 c672cd52 2020-10-30 neels err = free_err;
274 c672cd52 2020-10-30 neels }
275 c672cd52 2020-10-30 neels done:
276 c672cd52 2020-10-30 neels if (f1 && fclose(f1) != 0 && err == NULL)
277 c672cd52 2020-10-30 neels err = got_error_from_errno("fclose");
278 c672cd52 2020-10-30 neels return err;
279 c672cd52 2020-10-30 neels }
280 c672cd52 2020-10-30 neels
281 c672cd52 2020-10-30 neels const struct got_error *
282 c672cd52 2020-10-30 neels got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
283 c672cd52 2020-10-30 neels FILE *f2, size_t size2, const char *label2, int diff_context,
284 c672cd52 2020-10-30 neels int ignore_whitespace, FILE *outfile)
285 c672cd52 2020-10-30 neels {
286 c672cd52 2020-10-30 neels return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
287 c672cd52 2020-10-30 neels diff_context, ignore_whitespace, outfile);
288 c672cd52 2020-10-30 neels }
289 c672cd52 2020-10-30 neels
290 c672cd52 2020-10-30 neels const struct got_error *
291 c672cd52 2020-10-30 neels got_diff_blob_prepared_file(struct got_diffreg_result **resultp,
292 c672cd52 2020-10-30 neels struct diff_data *data1, struct got_blob_object *blob1,
293 c672cd52 2020-10-30 neels struct diff_data *data2, FILE *f2, char *p2, size_t size2,
294 c672cd52 2020-10-30 neels const struct diff_config *cfg, int ignore_whitespace)
295 c672cd52 2020-10-30 neels {
296 c672cd52 2020-10-30 neels const struct got_error *err = NULL, *free_err;
297 c672cd52 2020-10-30 neels FILE *f1 = NULL;
298 c672cd52 2020-10-30 neels char hex1[SHA1_DIGEST_STRING_LENGTH];
299 c672cd52 2020-10-30 neels char *idstr1 = NULL, *p1 = NULL;
300 c672cd52 2020-10-30 neels size_t size1, size;
301 c672cd52 2020-10-30 neels struct got_diffreg_result *result = NULL;
302 c672cd52 2020-10-30 neels int f1_created = 0;
303 c672cd52 2020-10-30 neels
304 c672cd52 2020-10-30 neels *resultp = NULL;
305 c672cd52 2020-10-30 neels
306 c672cd52 2020-10-30 neels size1 = 0;
307 c672cd52 2020-10-30 neels if (blob1) {
308 c672cd52 2020-10-30 neels f1 = got_opentemp();
309 c672cd52 2020-10-30 neels if (f1 == NULL)
310 c672cd52 2020-10-30 neels return got_error_from_errno("got_opentemp");
311 c672cd52 2020-10-30 neels idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
312 c672cd52 2020-10-30 neels err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
313 c672cd52 2020-10-30 neels blob1);
314 c672cd52 2020-10-30 neels if (err)
315 c672cd52 2020-10-30 neels goto done;
316 c672cd52 2020-10-30 neels } else {
317 c672cd52 2020-10-30 neels idstr1 = "/dev/null";
318 c672cd52 2020-10-30 neels }
319 c672cd52 2020-10-30 neels
320 c672cd52 2020-10-30 neels err = got_diff_prepare_file(&f1, &p1, &f1_created, &size,
321 c672cd52 2020-10-30 neels data1, cfg, ignore_whitespace);
322 c672cd52 2020-10-30 neels if (err)
323 c672cd52 2020-10-30 neels goto done;
324 c672cd52 2020-10-30 neels
325 c672cd52 2020-10-30 neels err = got_diffreg_prepared_files(&result, cfg, data1, f1,
326 c672cd52 2020-10-30 neels p1, size1, data2, f2, p2, size2);
327 c672cd52 2020-10-30 neels if (err)
328 c672cd52 2020-10-30 neels goto done;
329 c672cd52 2020-10-30 neels
330 c672cd52 2020-10-30 neels *resultp = result;
331 c672cd52 2020-10-30 neels done:
332 c672cd52 2020-10-30 neels if (err) {
333 c672cd52 2020-10-30 neels if (result)
334 c672cd52 2020-10-30 neels free_err = got_diffreg_result_free_left(result);
335 c672cd52 2020-10-30 neels else
336 c672cd52 2020-10-30 neels free_err = got_diffreg_close(f1, p1, size1, NULL,
337 c672cd52 2020-10-30 neels NULL, 0);
338 c672cd52 2020-10-30 neels if (free_err && err == NULL)
339 c672cd52 2020-10-30 neels err = free_err;
340 c672cd52 2020-10-30 neels }
341 c672cd52 2020-10-30 neels return err;
342 c672cd52 2020-10-30 neels }
343 c672cd52 2020-10-30 neels
344 c672cd52 2020-10-30 neels static const struct got_error *
345 c672cd52 2020-10-30 neels diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
346 c672cd52 2020-10-30 neels struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
347 c672cd52 2020-10-30 neels {
348 c672cd52 2020-10-30 neels const struct got_error *err;
349 c672cd52 2020-10-30 neels struct got_blob_object *blob = NULL;
350 c672cd52 2020-10-30 neels struct got_object *obj = NULL;
351 c672cd52 2020-10-30 neels
352 c672cd52 2020-10-30 neels err = got_object_open(&obj, repo, id);
353 c672cd52 2020-10-30 neels if (err)
354 c672cd52 2020-10-30 neels return err;
355 c672cd52 2020-10-30 neels
356 c672cd52 2020-10-30 neels err = got_object_blob_open(&blob, repo, obj, 8192);
357 c672cd52 2020-10-30 neels if (err)
358 c672cd52 2020-10-30 neels goto done;
359 c672cd52 2020-10-30 neels err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
360 c672cd52 2020-10-30 neels done:
361 c672cd52 2020-10-30 neels got_object_close(obj);
362 c672cd52 2020-10-30 neels if (blob)
363 c672cd52 2020-10-30 neels got_object_blob_close(blob);
364 c672cd52 2020-10-30 neels return err;
365 c672cd52 2020-10-30 neels }
366 c672cd52 2020-10-30 neels
367 c672cd52 2020-10-30 neels static const struct got_error *
368 c672cd52 2020-10-30 neels diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
369 c672cd52 2020-10-30 neels const char *label1, const char *label2, mode_t mode1, mode_t mode2,
370 c672cd52 2020-10-30 neels struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
371 c672cd52 2020-10-30 neels {
372 c672cd52 2020-10-30 neels const struct got_error *err;
373 c672cd52 2020-10-30 neels struct got_object *obj1 = NULL;
374 c672cd52 2020-10-30 neels struct got_object *obj2 = NULL;
375 c672cd52 2020-10-30 neels struct got_blob_object *blob1 = NULL;
376 c672cd52 2020-10-30 neels struct got_blob_object *blob2 = NULL;
377 c672cd52 2020-10-30 neels
378 c672cd52 2020-10-30 neels err = got_object_open(&obj1, repo, id1);
379 c672cd52 2020-10-30 neels if (err)
380 c672cd52 2020-10-30 neels return err;
381 c672cd52 2020-10-30 neels if (obj1->type != GOT_OBJ_TYPE_BLOB) {
382 c672cd52 2020-10-30 neels err = got_error(GOT_ERR_OBJ_TYPE);
383 c672cd52 2020-10-30 neels goto done;
384 c672cd52 2020-10-30 neels }
385 c672cd52 2020-10-30 neels
386 c672cd52 2020-10-30 neels err = got_object_open(&obj2, repo, id2);
387 c672cd52 2020-10-30 neels if (err)
388 c672cd52 2020-10-30 neels goto done;
389 c672cd52 2020-10-30 neels if (obj2->type != GOT_OBJ_TYPE_BLOB) {
390 c672cd52 2020-10-30 neels err = got_error(GOT_ERR_BAD_OBJ_DATA);
391 c672cd52 2020-10-30 neels goto done;
392 c672cd52 2020-10-30 neels }
393 c672cd52 2020-10-30 neels
394 c672cd52 2020-10-30 neels err = got_object_blob_open(&blob1, repo, obj1, 8192);
395 c672cd52 2020-10-30 neels if (err)
396 c672cd52 2020-10-30 neels goto done;
397 c672cd52 2020-10-30 neels
398 c672cd52 2020-10-30 neels err = got_object_blob_open(&blob2, repo, obj2, 8192);
399 c672cd52 2020-10-30 neels if (err)
400 c672cd52 2020-10-30 neels goto done;
401 c672cd52 2020-10-30 neels
402 c672cd52 2020-10-30 neels err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
403 c672cd52 2020-10-30 neels repo);
404 c672cd52 2020-10-30 neels done:
405 c672cd52 2020-10-30 neels if (obj1)
406 c672cd52 2020-10-30 neels got_object_close(obj1);
407 c672cd52 2020-10-30 neels if (obj2)
408 c672cd52 2020-10-30 neels got_object_close(obj2);
409 c672cd52 2020-10-30 neels if (blob1)
410 c672cd52 2020-10-30 neels got_object_blob_close(blob1);
411 c672cd52 2020-10-30 neels if (blob2)
412 c672cd52 2020-10-30 neels got_object_blob_close(blob2);
413 c672cd52 2020-10-30 neels return err;
414 c672cd52 2020-10-30 neels }
415 c672cd52 2020-10-30 neels
416 c672cd52 2020-10-30 neels static const struct got_error *
417 c672cd52 2020-10-30 neels diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
418 c672cd52 2020-10-30 neels struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
419 c672cd52 2020-10-30 neels {
420 c672cd52 2020-10-30 neels const struct got_error *err;
421 c672cd52 2020-10-30 neels struct got_blob_object *blob = NULL;
422 c672cd52 2020-10-30 neels struct got_object *obj = NULL;
423 c672cd52 2020-10-30 neels
424 c672cd52 2020-10-30 neels err = got_object_open(&obj, repo, id);
425 c672cd52 2020-10-30 neels if (err)
426 c672cd52 2020-10-30 neels return err;
427 c672cd52 2020-10-30 neels
428 c672cd52 2020-10-30 neels err = got_object_blob_open(&blob, repo, obj, 8192);
429 c672cd52 2020-10-30 neels if (err)
430 c672cd52 2020-10-30 neels goto done;
431 c672cd52 2020-10-30 neels err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
432 c672cd52 2020-10-30 neels done:
433 c672cd52 2020-10-30 neels got_object_close(obj);
434 c672cd52 2020-10-30 neels if (blob)
435 c672cd52 2020-10-30 neels got_object_blob_close(blob);
436 c672cd52 2020-10-30 neels return err;
437 c672cd52 2020-10-30 neels }
438 c672cd52 2020-10-30 neels
439 c672cd52 2020-10-30 neels static const struct got_error *
440 c672cd52 2020-10-30 neels diff_added_tree(struct got_object_id *id, const char *label,
441 c672cd52 2020-10-30 neels struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
442 c672cd52 2020-10-30 neels int diff_content)
443 c672cd52 2020-10-30 neels {
444 c672cd52 2020-10-30 neels const struct got_error *err = NULL;
445 c672cd52 2020-10-30 neels struct got_object *treeobj = NULL;
446 c672cd52 2020-10-30 neels struct got_tree_object *tree = NULL;
447 c672cd52 2020-10-30 neels
448 c672cd52 2020-10-30 neels err = got_object_open(&treeobj, repo, id);
449 c672cd52 2020-10-30 neels if (err)
450 c672cd52 2020-10-30 neels goto done;
451 c672cd52 2020-10-30 neels
452 c672cd52 2020-10-30 neels if (treeobj->type != GOT_OBJ_TYPE_TREE) {
453 c672cd52 2020-10-30 neels err = got_error(GOT_ERR_OBJ_TYPE);
454 c672cd52 2020-10-30 neels goto done;
455 c672cd52 2020-10-30 neels }
456 c672cd52 2020-10-30 neels
457 c672cd52 2020-10-30 neels err = got_object_tree_open(&tree, repo, treeobj);
458 c672cd52 2020-10-30 neels if (err)
459 c672cd52 2020-10-30 neels goto done;
460 c672cd52 2020-10-30 neels
461 c672cd52 2020-10-30 neels err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
462 c672cd52 2020-10-30 neels diff_content);
463 c672cd52 2020-10-30 neels done:
464 c672cd52 2020-10-30 neels if (tree)
465 c672cd52 2020-10-30 neels got_object_tree_close(tree);
466 c672cd52 2020-10-30 neels if (treeobj)
467 c672cd52 2020-10-30 neels got_object_close(treeobj);
468 c672cd52 2020-10-30 neels return err;
469 c672cd52 2020-10-30 neels }
470 c672cd52 2020-10-30 neels
471 c672cd52 2020-10-30 neels static const struct got_error *
472 c672cd52 2020-10-30 neels diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
473 c672cd52 2020-10-30 neels const char *label1, const char *label2, struct got_repository *repo,
474 c672cd52 2020-10-30 neels got_diff_blob_cb cb, void *cb_arg, int diff_content)
475 c672cd52 2020-10-30 neels {
476 c672cd52 2020-10-30 neels const struct got_error *err;
477 c672cd52 2020-10-30 neels struct got_object *treeobj1 = NULL;
478 c672cd52 2020-10-30 neels struct got_object *treeobj2 = NULL;
479 c672cd52 2020-10-30 neels struct got_tree_object *tree1 = NULL;
480 c672cd52 2020-10-30 neels struct got_tree_object *tree2 = NULL;
481 c672cd52 2020-10-30 neels
482 c672cd52 2020-10-30 neels err = got_object_open(&treeobj1, repo, id1);
483 c672cd52 2020-10-30 neels if (err)
484 c672cd52 2020-10-30 neels goto done;
485 c672cd52 2020-10-30 neels
486 c672cd52 2020-10-30 neels if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
487 c672cd52 2020-10-30 neels err = got_error(GOT_ERR_OBJ_TYPE);
488 c672cd52 2020-10-30 neels goto done;
489 c672cd52 2020-10-30 neels }
490 c672cd52 2020-10-30 neels
491 c672cd52 2020-10-30 neels err = got_object_open(&treeobj2, repo, id2);
492 c672cd52 2020-10-30 neels if (err)
493 c672cd52 2020-10-30 neels goto done;
494 c672cd52 2020-10-30 neels
495 c672cd52 2020-10-30 neels if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
496 c672cd52 2020-10-30 neels err = got_error(GOT_ERR_OBJ_TYPE);
497 c672cd52 2020-10-30 neels goto done;
498 c672cd52 2020-10-30 neels }
499 c672cd52 2020-10-30 neels
500 c672cd52 2020-10-30 neels err = got_object_tree_open(&tree1, repo, treeobj1);
501 c672cd52 2020-10-30 neels if (err)
502 c672cd52 2020-10-30 neels goto done;
503 c672cd52 2020-10-30 neels
504 c672cd52 2020-10-30 neels err = got_object_tree_open(&tree2, repo, treeobj2);
505 c672cd52 2020-10-30 neels if (err)
506 c672cd52 2020-10-30 neels goto done;
507 c672cd52 2020-10-30 neels
508 c672cd52 2020-10-30 neels err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
509 c672cd52 2020-10-30 neels diff_content);
510 c672cd52 2020-10-30 neels
511 c672cd52 2020-10-30 neels done:
512 c672cd52 2020-10-30 neels if (tree1)
513 c672cd52 2020-10-30 neels got_object_tree_close(tree1);
514 c672cd52 2020-10-30 neels if (tree2)
515 c672cd52 2020-10-30 neels got_object_tree_close(tree2);
516 c672cd52 2020-10-30 neels if (treeobj1)
517 c672cd52 2020-10-30 neels got_object_close(treeobj1);
518 c672cd52 2020-10-30 neels if (treeobj2)
519 c672cd52 2020-10-30 neels got_object_close(treeobj2);
520 c672cd52 2020-10-30 neels return err;
521 c672cd52 2020-10-30 neels }
522 c672cd52 2020-10-30 neels
523 c672cd52 2020-10-30 neels static const struct got_error *
524 c672cd52 2020-10-30 neels diff_deleted_tree(struct got_object_id *id, const char *label,
525 c672cd52 2020-10-30 neels struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
526 c672cd52 2020-10-30 neels int diff_content)
527 c672cd52 2020-10-30 neels {
528 c672cd52 2020-10-30 neels const struct got_error *err;
529 c672cd52 2020-10-30 neels struct got_object *treeobj = NULL;
530 c672cd52 2020-10-30 neels struct got_tree_object *tree = NULL;
531 c672cd52 2020-10-30 neels
532 c672cd52 2020-10-30 neels err = got_object_open(&treeobj, repo, id);
533 c672cd52 2020-10-30 neels if (err)
534 c672cd52 2020-10-30 neels goto done;
535 c672cd52 2020-10-30 neels
536 c672cd52 2020-10-30 neels if (treeobj->type != GOT_OBJ_TYPE_TREE) {
537 c672cd52 2020-10-30 neels err = got_error(GOT_ERR_OBJ_TYPE);
538 c672cd52 2020-10-30 neels goto done;
539 c672cd52 2020-10-30 neels }
540 c672cd52 2020-10-30 neels
541 c672cd52 2020-10-30 neels err = got_object_tree_open(&tree, repo, treeobj);
542 c672cd52 2020-10-30 neels if (err)
543 c672cd52 2020-10-30 neels goto done;
544 c672cd52 2020-10-30 neels
545 c672cd52 2020-10-30 neels err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
546 c672cd52 2020-10-30 neels diff_content);
547 c672cd52 2020-10-30 neels done:
548 c672cd52 2020-10-30 neels if (tree)
549 c672cd52 2020-10-30 neels got_object_tree_close(tree);
550 c672cd52 2020-10-30 neels if (treeobj)
551 c672cd52 2020-10-30 neels got_object_close(treeobj);
552 c672cd52 2020-10-30 neels return err;
553 c672cd52 2020-10-30 neels }
554 c672cd52 2020-10-30 neels
555 c672cd52 2020-10-30 neels static const struct got_error *
556 c672cd52 2020-10-30 neels diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
557 c672cd52 2020-10-30 neels const char *label1, const char *label2, struct got_repository *repo,
558 c672cd52 2020-10-30 neels got_diff_blob_cb cb, void *cb_arg)
559 c672cd52 2020-10-30 neels {
560 c672cd52 2020-10-30 neels /* XXX TODO */
561 c672cd52 2020-10-30 neels return NULL;
562 c672cd52 2020-10-30 neels }
563 c672cd52 2020-10-30 neels
564 c672cd52 2020-10-30 neels static const struct got_error *
565 c672cd52 2020-10-30 neels diff_entry_old_new(struct got_tree_entry *te1,
566 c672cd52 2020-10-30 neels struct got_tree_entry *te2, const char *label1, const char *label2,
567 c672cd52 2020-10-30 neels struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
568 c672cd52 2020-10-30 neels int diff_content)
569 c672cd52 2020-10-30 neels {
570 c672cd52 2020-10-30 neels const struct got_error *err = NULL;
571 c672cd52 2020-10-30 neels int id_match;
572 c672cd52 2020-10-30 neels
573 c672cd52 2020-10-30 neels if (got_object_tree_entry_is_submodule(te1))
574 c672cd52 2020-10-30 neels return NULL;
575 c672cd52 2020-10-30 neels
576 c672cd52 2020-10-30 neels if (te2 == NULL) {
577 c672cd52 2020-10-30 neels if (S_ISDIR(te1->mode))
578 c672cd52 2020-10-30 neels err = diff_deleted_tree(&te1->id, label1, repo,
579 c672cd52 2020-10-30 neels cb, cb_arg, diff_content);
580 c672cd52 2020-10-30 neels else {
581 c672cd52 2020-10-30 neels if (diff_content)
582 c672cd52 2020-10-30 neels err = diff_deleted_blob(&te1->id, label1,
583 c672cd52 2020-10-30 neels te1->mode, repo, cb, cb_arg);
584 c672cd52 2020-10-30 neels else
585 c672cd52 2020-10-30 neels err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
586 c672cd52 2020-10-30 neels label1, NULL, te1->mode, 0, repo);
587 c672cd52 2020-10-30 neels }
588 c672cd52 2020-10-30 neels return err;
589 c672cd52 2020-10-30 neels } else if (got_object_tree_entry_is_submodule(te2))
590 c672cd52 2020-10-30 neels return NULL;
591 c672cd52 2020-10-30 neels
592 c672cd52 2020-10-30 neels id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
593 c672cd52 2020-10-30 neels if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
594 c672cd52 2020-10-30 neels if (!id_match)
595 c672cd52 2020-10-30 neels return diff_modified_tree(&te1->id, &te2->id,
596 c672cd52 2020-10-30 neels label1, label2, repo, cb, cb_arg, diff_content);
597 c672cd52 2020-10-30 neels } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
598 c672cd52 2020-10-30 neels (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
599 c672cd52 2020-10-30 neels if (!id_match ||
600 c672cd52 2020-10-30 neels ((te1->mode & (S_IFLNK | S_IXUSR))) !=
601 c672cd52 2020-10-30 neels (te2->mode & (S_IFLNK | S_IXUSR))) {
602 c672cd52 2020-10-30 neels if (diff_content)
603 c672cd52 2020-10-30 neels return diff_modified_blob(&te1->id, &te2->id,
604 c672cd52 2020-10-30 neels label1, label2, te1->mode, te2->mode,
605 c672cd52 2020-10-30 neels repo, cb, cb_arg);
606 c672cd52 2020-10-30 neels else
607 c672cd52 2020-10-30 neels return cb(cb_arg, NULL, NULL, &te1->id,
608 c672cd52 2020-10-30 neels &te2->id, label1, label2, te1->mode,
609 c672cd52 2020-10-30 neels te2->mode, repo);
610 c672cd52 2020-10-30 neels }
611 c672cd52 2020-10-30 neels }
612 c672cd52 2020-10-30 neels
613 c672cd52 2020-10-30 neels if (id_match)
614 c672cd52 2020-10-30 neels return NULL;
615 c672cd52 2020-10-30 neels
616 c672cd52 2020-10-30 neels return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
617 c672cd52 2020-10-30 neels cb, cb_arg);
618 c672cd52 2020-10-30 neels }
619 c672cd52 2020-10-30 neels
620 c672cd52 2020-10-30 neels static const struct got_error *
621 c672cd52 2020-10-30 neels diff_entry_new_old(struct got_tree_entry *te2,
622 c672cd52 2020-10-30 neels struct got_tree_entry *te1, const char *label2,
623 c672cd52 2020-10-30 neels struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
624 c672cd52 2020-10-30 neels int diff_content)
625 c672cd52 2020-10-30 neels {
626 c672cd52 2020-10-30 neels if (te1 != NULL) /* handled by diff_entry_old_new() */
627 c672cd52 2020-10-30 neels return NULL;
628 c672cd52 2020-10-30 neels
629 c672cd52 2020-10-30 neels if (got_object_tree_entry_is_submodule(te2))
630 c672cd52 2020-10-30 neels return NULL;
631 c672cd52 2020-10-30 neels
632 c672cd52 2020-10-30 neels if (S_ISDIR(te2->mode))
633 c672cd52 2020-10-30 neels return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
634 c672cd52 2020-10-30 neels diff_content);
635 c672cd52 2020-10-30 neels
636 c672cd52 2020-10-30 neels if (diff_content)
637 c672cd52 2020-10-30 neels return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
638 c672cd52 2020-10-30 neels cb_arg);
639 c672cd52 2020-10-30 neels
640 c672cd52 2020-10-30 neels return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
641 c672cd52 2020-10-30 neels te2->mode, repo);
642 c672cd52 2020-10-30 neels }
643 c672cd52 2020-10-30 neels
644 c672cd52 2020-10-30 neels const struct got_error *
645 c672cd52 2020-10-30 neels got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
646 c672cd52 2020-10-30 neels struct got_blob_object *blob2, struct got_object_id *id1,
647 c672cd52 2020-10-30 neels struct got_object_id *id2, const char *label1, const char *label2,
648 c672cd52 2020-10-30 neels mode_t mode1, mode_t mode2, struct got_repository *repo)
649 c672cd52 2020-10-30 neels {
650 c672cd52 2020-10-30 neels const struct got_error *err = NULL;
651 c672cd52 2020-10-30 neels struct got_pathlist_head *paths = arg;
652 c672cd52 2020-10-30 neels struct got_diff_changed_path *change = NULL;
653 c672cd52 2020-10-30 neels char *path = NULL;
654 c672cd52 2020-10-30 neels
655 c672cd52 2020-10-30 neels path = strdup(label2 ? label2 : label1);
656 c672cd52 2020-10-30 neels if (path == NULL)
657 c672cd52 2020-10-30 neels return got_error_from_errno("malloc");
658 c672cd52 2020-10-30 neels
659 c672cd52 2020-10-30 neels change = malloc(sizeof(*change));
660 c672cd52 2020-10-30 neels if (change == NULL) {
661 c672cd52 2020-10-30 neels err = got_error_from_errno("malloc");
662 c672cd52 2020-10-30 neels goto done;
663 c672cd52 2020-10-30 neels }
664 c672cd52 2020-10-30 neels
665 c672cd52 2020-10-30 neels change->status = GOT_STATUS_NO_CHANGE;
666 c672cd52 2020-10-30 neels if (id1 == NULL)
667 c672cd52 2020-10-30 neels change->status = GOT_STATUS_ADD;
668 c672cd52 2020-10-30 neels else if (id2 == NULL)
669 c672cd52 2020-10-30 neels change->status = GOT_STATUS_DELETE;
670 c672cd52 2020-10-30 neels else {
671 c672cd52 2020-10-30 neels if (got_object_id_cmp(id1, id2) != 0)
672 c672cd52 2020-10-30 neels change->status = GOT_STATUS_MODIFY;
673 c672cd52 2020-10-30 neels else if (mode1 != mode2)
674 c672cd52 2020-10-30 neels change->status = GOT_STATUS_MODE_CHANGE;
675 c672cd52 2020-10-30 neels }
676 c672cd52 2020-10-30 neels
677 c672cd52 2020-10-30 neels err = got_pathlist_insert(NULL, paths, path, change);
678 c672cd52 2020-10-30 neels done:
679 c672cd52 2020-10-30 neels if (err) {
680 c672cd52 2020-10-30 neels free(path);
681 c672cd52 2020-10-30 neels free(change);
682 c672cd52 2020-10-30 neels }
683 c672cd52 2020-10-30 neels return err;
684 c672cd52 2020-10-30 neels }
685 c672cd52 2020-10-30 neels
686 c672cd52 2020-10-30 neels const struct got_error *
687 c672cd52 2020-10-30 neels got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
688 c672cd52 2020-10-30 neels const char *label1, const char *label2, struct got_repository *repo,
689 c672cd52 2020-10-30 neels got_diff_blob_cb cb, void *cb_arg, int diff_content)
690 c672cd52 2020-10-30 neels {
691 c672cd52 2020-10-30 neels const struct got_error *err = NULL;
692 c672cd52 2020-10-30 neels struct got_tree_entry *te1 = NULL;
693 c672cd52 2020-10-30 neels struct got_tree_entry *te2 = NULL;
694 c672cd52 2020-10-30 neels char *l1 = NULL, *l2 = NULL;
695 c672cd52 2020-10-30 neels int tidx1 = 0, tidx2 = 0;
696 c672cd52 2020-10-30 neels
697 c672cd52 2020-10-30 neels if (tree1) {
698 c672cd52 2020-10-30 neels te1 = got_object_tree_get_entry(tree1, 0);
699 c672cd52 2020-10-30 neels if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
700 c672cd52 2020-10-30 neels te1->name) == -1)
701 c672cd52 2020-10-30 neels return got_error_from_errno("asprintf");
702 c672cd52 2020-10-30 neels }
703 c672cd52 2020-10-30 neels if (tree2) {
704 c672cd52 2020-10-30 neels te2 = got_object_tree_get_entry(tree2, 0);
705 c672cd52 2020-10-30 neels if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
706 c672cd52 2020-10-30 neels te2->name) == -1)
707 c672cd52 2020-10-30 neels return got_error_from_errno("asprintf");
708 c672cd52 2020-10-30 neels }
709 c672cd52 2020-10-30 neels
710 c672cd52 2020-10-30 neels do {
711 c672cd52 2020-10-30 neels if (te1) {
712 c672cd52 2020-10-30 neels struct got_tree_entry *te = NULL;
713 c672cd52 2020-10-30 neels if (tree2)
714 c672cd52 2020-10-30 neels te = got_object_tree_find_entry(tree2,
715 c672cd52 2020-10-30 neels te1->name);
716 c672cd52 2020-10-30 neels if (te) {
717 c672cd52 2020-10-30 neels free(l2);
718 c672cd52 2020-10-30 neels l2 = NULL;
719 c672cd52 2020-10-30 neels if (te && asprintf(&l2, "%s%s%s", label2,
720 c672cd52 2020-10-30 neels label2[0] ? "/" : "", te->name) == -1)
721 c672cd52 2020-10-30 neels return
722 c672cd52 2020-10-30 neels got_error_from_errno("asprintf");
723 c672cd52 2020-10-30 neels }
724 c672cd52 2020-10-30 neels err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
725 c672cd52 2020-10-30 neels cb_arg, diff_content);
726 c672cd52 2020-10-30 neels if (err)
727 c672cd52 2020-10-30 neels break;
728 c672cd52 2020-10-30 neels }
729 c672cd52 2020-10-30 neels
730 c672cd52 2020-10-30 neels if (te2) {
731 c672cd52 2020-10-30 neels struct got_tree_entry *te = NULL;
732 c672cd52 2020-10-30 neels if (tree1)
733 c672cd52 2020-10-30 neels te = got_object_tree_find_entry(tree1,
734 c672cd52 2020-10-30 neels te2->name);
735 c672cd52 2020-10-30 neels free(l2);
736 c672cd52 2020-10-30 neels if (te) {
737 c672cd52 2020-10-30 neels if (asprintf(&l2, "%s%s%s", label2,
738 c672cd52 2020-10-30 neels label2[0] ? "/" : "", te->name) == -1)
739 c672cd52 2020-10-30 neels return
740 c672cd52 2020-10-30 neels got_error_from_errno("asprintf");
741 c672cd52 2020-10-30 neels } else {
742 c672cd52 2020-10-30 neels if (asprintf(&l2, "%s%s%s", label2,
743 c672cd52 2020-10-30 neels label2[0] ? "/" : "", te2->name) == -1)
744 c672cd52 2020-10-30 neels return
745 c672cd52 2020-10-30 neels got_error_from_errno("asprintf");
746 c672cd52 2020-10-30 neels }
747 c672cd52 2020-10-30 neels err = diff_entry_new_old(te2, te, l2, repo,
748 c672cd52 2020-10-30 neels cb, cb_arg, diff_content);
749 c672cd52 2020-10-30 neels if (err)
750 c672cd52 2020-10-30 neels break;
751 c672cd52 2020-10-30 neels }
752 c672cd52 2020-10-30 neels
753 c672cd52 2020-10-30 neels free(l1);
754 c672cd52 2020-10-30 neels l1 = NULL;
755 c672cd52 2020-10-30 neels if (te1) {
756 c672cd52 2020-10-30 neels tidx1++;
757 c672cd52 2020-10-30 neels te1 = got_object_tree_get_entry(tree1, tidx1);
758 c672cd52 2020-10-30 neels if (te1 &&
759 c672cd52 2020-10-30 neels asprintf(&l1, "%s%s%s", label1,
760 c672cd52 2020-10-30 neels label1[0] ? "/" : "", te1->name) == -1)
761 c672cd52 2020-10-30 neels return got_error_from_errno("asprintf");
762 c672cd52 2020-10-30 neels }
763 c672cd52 2020-10-30 neels free(l2);
764 c672cd52 2020-10-30 neels l2 = NULL;
765 c672cd52 2020-10-30 neels if (te2) {
766 c672cd52 2020-10-30 neels tidx2++;
767 c672cd52 2020-10-30 neels te2 = got_object_tree_get_entry(tree2, tidx2);
768 c672cd52 2020-10-30 neels if (te2 &&
769 c672cd52 2020-10-30 neels asprintf(&l2, "%s%s%s", label2,
770 c672cd52 2020-10-30 neels label2[0] ? "/" : "", te2->name) == -1)
771 c672cd52 2020-10-30 neels return got_error_from_errno("asprintf");
772 c672cd52 2020-10-30 neels }
773 c672cd52 2020-10-30 neels } while (te1 || te2);
774 c672cd52 2020-10-30 neels
775 c672cd52 2020-10-30 neels return err;
776 c672cd52 2020-10-30 neels }
777 c672cd52 2020-10-30 neels
778 c672cd52 2020-10-30 neels const struct got_error *
779 c672cd52 2020-10-30 neels got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
780 c672cd52 2020-10-30 neels struct got_object_id *id1, struct got_object_id *id2,
781 c672cd52 2020-10-30 neels const char *label1, const char *label2, int diff_context,
782 c672cd52 2020-10-30 neels int ignore_whitespace, struct got_repository *repo, FILE *outfile)
783 c672cd52 2020-10-30 neels {
784 c672cd52 2020-10-30 neels const struct got_error *err;
785 c672cd52 2020-10-30 neels struct got_blob_object *blob1 = NULL, *blob2 = NULL;
786 c672cd52 2020-10-30 neels
787 c672cd52 2020-10-30 neels if (id1 == NULL && id2 == NULL)
788 c672cd52 2020-10-30 neels return got_error(GOT_ERR_NO_OBJ);
789 c672cd52 2020-10-30 neels
790 c672cd52 2020-10-30 neels if (id1) {
791 c672cd52 2020-10-30 neels err = got_object_open_as_blob(&blob1, repo, id1, 8192);
792 c672cd52 2020-10-30 neels if (err)
793 c672cd52 2020-10-30 neels goto done;
794 c672cd52 2020-10-30 neels }
795 c672cd52 2020-10-30 neels if (id2) {
796 c672cd52 2020-10-30 neels err = got_object_open_as_blob(&blob2, repo, id2, 8192);
797 c672cd52 2020-10-30 neels if (err)
798 c672cd52 2020-10-30 neels goto done;
799 c672cd52 2020-10-30 neels }
800 c672cd52 2020-10-30 neels err = got_diff_blob(line_offsets, nlines, blob1, blob2,
801 c672cd52 2020-10-30 neels label1, label2, diff_context, ignore_whitespace, outfile);
802 c672cd52 2020-10-30 neels done:
803 c672cd52 2020-10-30 neels if (blob1)
804 c672cd52 2020-10-30 neels got_object_blob_close(blob1);
805 c672cd52 2020-10-30 neels if (blob2)
806 c672cd52 2020-10-30 neels got_object_blob_close(blob2);
807 c672cd52 2020-10-30 neels return err;
808 c672cd52 2020-10-30 neels }
809 c672cd52 2020-10-30 neels
810 c672cd52 2020-10-30 neels const struct got_error *
811 c672cd52 2020-10-30 neels got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
812 c672cd52 2020-10-30 neels struct got_object_id *id1, struct got_object_id *id2,
813 c672cd52 2020-10-30 neels char *label1, char *label2, int diff_context, int ignore_whitespace,
814 c672cd52 2020-10-30 neels struct got_repository *repo, FILE *outfile)
815 c672cd52 2020-10-30 neels {
816 c672cd52 2020-10-30 neels const struct got_error *err;
817 c672cd52 2020-10-30 neels struct got_tree_object *tree1 = NULL, *tree2 = NULL;
818 c672cd52 2020-10-30 neels struct got_diff_blob_output_unidiff_arg arg;
819 c672cd52 2020-10-30 neels int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
820 c672cd52 2020-10-30 neels
821 c672cd52 2020-10-30 neels if (id1 == NULL && id2 == NULL)
822 c672cd52 2020-10-30 neels return got_error(GOT_ERR_NO_OBJ);
823 c672cd52 2020-10-30 neels
824 c672cd52 2020-10-30 neels if (id1) {
825 c672cd52 2020-10-30 neels err = got_object_open_as_tree(&tree1, repo, id1);
826 c672cd52 2020-10-30 neels if (err)
827 c672cd52 2020-10-30 neels goto done;
828 c672cd52 2020-10-30 neels }
829 c672cd52 2020-10-30 neels if (id2) {
830 c672cd52 2020-10-30 neels err = got_object_open_as_tree(&tree2, repo, id2);
831 c672cd52 2020-10-30 neels if (err)
832 c672cd52 2020-10-30 neels goto done;
833 c672cd52 2020-10-30 neels }
834 c672cd52 2020-10-30 neels arg.diff_context = diff_context;
835 c672cd52 2020-10-30 neels arg.ignore_whitespace = ignore_whitespace;
836 c672cd52 2020-10-30 neels arg.outfile = outfile;
837 c672cd52 2020-10-30 neels if (want_lineoffsets) {
838 c672cd52 2020-10-30 neels arg.line_offsets = *line_offsets;
839 c672cd52 2020-10-30 neels arg.nlines = *nlines;
840 c672cd52 2020-10-30 neels } else {
841 c672cd52 2020-10-30 neels arg.line_offsets = NULL;
842 c672cd52 2020-10-30 neels arg.nlines = 0;
843 c672cd52 2020-10-30 neels }
844 c672cd52 2020-10-30 neels err = got_diff_tree(tree1, tree2, label1, label2, repo,
845 c672cd52 2020-10-30 neels got_diff_blob_output_unidiff, &arg, 1);
846 c672cd52 2020-10-30 neels
847 c672cd52 2020-10-30 neels if (want_lineoffsets) {
848 c672cd52 2020-10-30 neels *line_offsets = arg.line_offsets; /* was likely re-allocated */
849 c672cd52 2020-10-30 neels *nlines = arg.nlines;
850 c672cd52 2020-10-30 neels }
851 c672cd52 2020-10-30 neels done:
852 c672cd52 2020-10-30 neels if (tree1)
853 c672cd52 2020-10-30 neels got_object_tree_close(tree1);
854 c672cd52 2020-10-30 neels if (tree2)
855 c672cd52 2020-10-30 neels got_object_tree_close(tree2);
856 c672cd52 2020-10-30 neels return err;
857 c672cd52 2020-10-30 neels }
858 c672cd52 2020-10-30 neels
859 c672cd52 2020-10-30 neels const struct got_error *
860 c672cd52 2020-10-30 neels got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
861 c672cd52 2020-10-30 neels struct got_object_id *id1, struct got_object_id *id2,
862 c672cd52 2020-10-30 neels int diff_context, int ignore_whitespace,
863 c672cd52 2020-10-30 neels struct got_repository *repo, FILE *outfile)
864 c672cd52 2020-10-30 neels {
865 c672cd52 2020-10-30 neels const struct got_error *err;
866 c672cd52 2020-10-30 neels struct got_commit_object *commit1 = NULL, *commit2 = NULL;
867 c672cd52 2020-10-30 neels
868 c672cd52 2020-10-30 neels if (id2 == NULL)
869 c672cd52 2020-10-30 neels return got_error(GOT_ERR_NO_OBJ);
870 c672cd52 2020-10-30 neels
871 c672cd52 2020-10-30 neels if (id1) {
872 c672cd52 2020-10-30 neels err = got_object_open_as_commit(&commit1, repo, id1);
873 c672cd52 2020-10-30 neels if (err)
874 c672cd52 2020-10-30 neels goto done;
875 c672cd52 2020-10-30 neels }
876 c672cd52 2020-10-30 neels
877 c672cd52 2020-10-30 neels err = got_object_open_as_commit(&commit2, repo, id2);
878 c672cd52 2020-10-30 neels if (err)
879 c672cd52 2020-10-30 neels goto done;
880 c672cd52 2020-10-30 neels
881 c672cd52 2020-10-30 neels err = got_diff_objects_as_trees(line_offsets, nlines,
882 c672cd52 2020-10-30 neels commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
883 c672cd52 2020-10-30 neels got_object_commit_get_tree_id(commit2), "", "", diff_context,
884 c672cd52 2020-10-30 neels ignore_whitespace, repo, outfile);
885 c672cd52 2020-10-30 neels done:
886 c672cd52 2020-10-30 neels if (commit1)
887 c672cd52 2020-10-30 neels got_object_commit_close(commit1);
888 c672cd52 2020-10-30 neels if (commit2)
889 c672cd52 2020-10-30 neels got_object_commit_close(commit2);
890 c672cd52 2020-10-30 neels return err;
891 c672cd52 2020-10-30 neels }
892 c672cd52 2020-10-30 neels
893 c672cd52 2020-10-30 neels const struct got_error *
894 c672cd52 2020-10-30 neels got_diff_files(struct got_diffreg_result **resultp,
895 c672cd52 2020-10-30 neels FILE *f1, const char *label1, FILE *f2, const char *label2,
896 c672cd52 2020-10-30 neels int diff_context, int ignore_whitespace, FILE *outfile)
897 c672cd52 2020-10-30 neels {
898 c672cd52 2020-10-30 neels const struct got_error *err = NULL;
899 c672cd52 2020-10-30 neels struct got_diffreg_result *diffreg_result = NULL;
900 c672cd52 2020-10-30 neels
901 c672cd52 2020-10-30 neels if (resultp)
902 c672cd52 2020-10-30 neels *resultp = NULL;
903 c672cd52 2020-10-30 neels
904 c672cd52 2020-10-30 neels if (outfile) {
905 c672cd52 2020-10-30 neels fprintf(outfile, "file - %s\n",
906 c672cd52 2020-10-30 neels f1 == NULL ? "/dev/null" : label1);
907 c672cd52 2020-10-30 neels fprintf(outfile, "file + %s\n",
908 c672cd52 2020-10-30 neels f2 == NULL ? "/dev/null" : label2);
909 c672cd52 2020-10-30 neels }
910 c672cd52 2020-10-30 neels
911 c672cd52 2020-10-30 neels err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_MYERS,
912 c672cd52 2020-10-30 neels ignore_whitespace);
913 c672cd52 2020-10-30 neels if (err)
914 c672cd52 2020-10-30 neels goto done;
915 c672cd52 2020-10-30 neels
916 c672cd52 2020-10-30 neels if (outfile) {
917 c672cd52 2020-10-30 neels err = got_diffreg_output(NULL, NULL, diffreg_result,
918 c672cd52 2020-10-30 neels f1, f2, label1, label2, GOT_DIFF_OUTPUT_UNIDIFF,
919 c672cd52 2020-10-30 neels diff_context, outfile);
920 c672cd52 2020-10-30 neels if (err)
921 c672cd52 2020-10-30 neels goto done;
922 c672cd52 2020-10-30 neels }
923 c672cd52 2020-10-30 neels
924 c672cd52 2020-10-30 neels done:
925 c672cd52 2020-10-30 neels if (resultp && err == NULL)
926 c672cd52 2020-10-30 neels *resultp = diffreg_result;
927 c672cd52 2020-10-30 neels else if (diffreg_result) {
928 c672cd52 2020-10-30 neels const struct got_error *free_err;
929 c672cd52 2020-10-30 neels free_err = got_diffreg_result_free(diffreg_result);
930 c672cd52 2020-10-30 neels if (free_err && err == NULL)
931 c672cd52 2020-10-30 neels err = free_err;
932 c672cd52 2020-10-30 neels }
933 c672cd52 2020-10-30 neels
934 c672cd52 2020-10-30 neels return err;
935 c672cd52 2020-10-30 neels }