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