2 7d283eee 2017-11-29 stsp * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
4 7d283eee 2017-11-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 7d283eee 2017-11-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 7d283eee 2017-11-29 stsp * copyright notice and this permission notice appear in all copies.
8 7d283eee 2017-11-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7d283eee 2017-11-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7d283eee 2017-11-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7d283eee 2017-11-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7d283eee 2017-11-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7d283eee 2017-11-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7d283eee 2017-11-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 7d283eee 2017-11-29 stsp #include <sys/queue.h>
18 1c7f0520 2017-11-29 stsp #include <sys/stat.h>
20 7d283eee 2017-11-29 stsp #include <stdio.h>
21 7d283eee 2017-11-29 stsp #include <stdlib.h>
22 7d283eee 2017-11-29 stsp #include <string.h>
23 f9d67749 2017-11-30 stsp #include <limits.h>
24 7d283eee 2017-11-29 stsp #include <sha1.h>
25 7d283eee 2017-11-29 stsp #include <zlib.h>
27 7d283eee 2017-11-29 stsp #include "got_object.h"
28 e09a504c 2019-06-28 stsp #include "got_repository.h"
29 7d283eee 2017-11-29 stsp #include "got_error.h"
30 789689b5 2017-11-30 stsp #include "got_diff.h"
31 511a516b 2018-05-19 stsp #include "got_opentemp.h"
32 324d37e7 2019-05-11 stsp #include "got_path.h"
33 0208f208 2020-05-05 stsp #include "got_cancel.h"
34 0208f208 2020-05-05 stsp #include "got_worktree.h"
36 718b3ab0 2018-03-17 stsp #include "got_lib_diff.h"
37 15a94983 2018-12-23 stsp #include "got_lib_delta.h"
38 15a94983 2018-12-23 stsp #include "got_lib_inflate.h"
39 15a94983 2018-12-23 stsp #include "got_lib_object.h"
41 404c43c4 2018-06-21 stsp static const struct got_error *
42 404c43c4 2018-06-21 stsp diff_blobs(struct got_blob_object *blob1, struct got_blob_object *blob2,
43 46f68b20 2019-10-19 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
44 46f68b20 2019-10-19 stsp int diff_context, int ignore_whitespace, FILE *outfile,
45 46f68b20 2019-10-19 stsp struct got_diff_changes *changes)
47 ed9e98a8 2017-11-29 stsp struct got_diff_state ds;
48 8ba9a219 2017-11-29 stsp struct got_diff_args args;
49 7d283eee 2017-11-29 stsp const struct got_error *err = NULL;
50 4e22badc 2017-11-30 stsp FILE *f1 = NULL, *f2 = NULL;
51 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
52 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
53 98abbc84 2017-11-30 stsp char *idstr1 = NULL, *idstr2 = NULL;
54 f934cf2c 2018-02-12 stsp size_t size1, size2;
55 4e22badc 2017-11-30 stsp int res, flags = 0;
57 4e22badc 2017-11-30 stsp if (blob1) {
58 a1fd68d8 2018-01-12 stsp f1 = got_opentemp();
59 4e22badc 2017-11-30 stsp if (f1 == NULL)
60 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentemp");
62 4e22badc 2017-11-30 stsp flags |= D_EMPTY1;
64 4e22badc 2017-11-30 stsp if (blob2) {
65 a1fd68d8 2018-01-12 stsp f2 = got_opentemp();
66 4e22badc 2017-11-30 stsp if (f2 == NULL) {
67 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
72 4e22badc 2017-11-30 stsp flags |= D_EMPTY2;
75 98abbc84 2017-11-30 stsp if (blob1) {
76 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
77 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
82 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
85 98abbc84 2017-11-30 stsp if (blob2) {
86 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
87 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
92 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
94 ed9e98a8 2017-11-29 stsp memset(&ds, 0, sizeof(ds));
95 f9d67749 2017-11-30 stsp /* XXX should stat buffers be passed in args instead of ds? */
96 f9d67749 2017-11-30 stsp ds.stb1.st_mode = S_IFREG;
98 f934cf2c 2018-02-12 stsp ds.stb1.st_size = size1;
99 f9d67749 2017-11-30 stsp ds.stb1.st_mtime = 0; /* XXX */
101 f9d67749 2017-11-30 stsp ds.stb2.st_mode = S_IFREG;
103 f934cf2c 2018-02-12 stsp ds.stb2.st_size = size2;
104 f9d67749 2017-11-30 stsp ds.stb2.st_mtime = 0; /* XXX */
106 54156555 2018-12-24 stsp memset(&args, 0, sizeof(args));
107 8ba9a219 2017-11-29 stsp args.diff_format = D_UNIFIED;
108 54156555 2018-12-24 stsp args.label[0] = label1 ? label1 : idstr1;
109 54156555 2018-12-24 stsp args.label[1] = label2 ? label2 : idstr2;
110 df2871d2 2018-10-18 stsp args.diff_context = diff_context;
111 84eb021e 2018-03-27 stsp flags |= D_PROTOTYPE;
112 63035f9f 2019-10-06 stsp if (ignore_whitespace)
113 63035f9f 2019-10-06 stsp flags |= D_IGNOREBLANKS;
115 09de383e 2018-12-24 stsp if (outfile) {
116 46f68b20 2019-10-19 stsp char *modestr1 = NULL, *modestr2 = NULL;
117 46f68b20 2019-10-19 stsp if (mode1 && mode1 != mode2) {
118 46f68b20 2019-10-19 stsp if (asprintf(&modestr1, " (mode %o)",
119 46f68b20 2019-10-19 stsp mode1 & (S_IRWXU | S_IRWXG | S_IRWXO)) == -1) {
120 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
124 46f68b20 2019-10-19 stsp if (mode2 && mode1 != mode2) {
125 46f68b20 2019-10-19 stsp if (asprintf(&modestr2, " (mode %o)",
126 46f68b20 2019-10-19 stsp mode2 & (S_IRWXU | S_IRWXG | S_IRWXO)) == -1) {
127 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
131 46f68b20 2019-10-19 stsp fprintf(outfile, "blob - %s%s\n", idstr1,
132 46f68b20 2019-10-19 stsp modestr1 ? modestr1 : "");
133 46f68b20 2019-10-19 stsp fprintf(outfile, "blob + %s%s\n", idstr2,
134 46f68b20 2019-10-19 stsp modestr2 ? modestr2 : "");
135 46f68b20 2019-10-19 stsp free(modestr1);
136 46f68b20 2019-10-19 stsp free(modestr2);
138 404c43c4 2018-06-21 stsp err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
139 dc424a06 2019-08-07 stsp got_diff_state_free(&ds);
141 fb43ecf1 2019-02-11 stsp if (f1 && fclose(f1) != 0 && err == NULL)
142 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
143 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
144 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
145 7d283eee 2017-11-29 stsp return err;
148 aaa13589 2019-06-01 stsp const struct got_error *
149 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
150 aaa13589 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
151 aaa13589 2019-06-01 stsp struct got_object_id *id2, const char *label1, const char *label2,
152 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
154 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
156 46f68b20 2019-10-19 stsp return diff_blobs(blob1, blob2, label1, label2, mode1, mode2,
157 46f68b20 2019-10-19 stsp a->diff_context, a->ignore_whitespace, a->outfile, NULL);
160 404c43c4 2018-06-21 stsp const struct got_error *
161 404c43c4 2018-06-21 stsp got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
162 63035f9f 2019-10-06 stsp const char *label1, const char *label2, int diff_context,
163 63035f9f 2019-10-06 stsp int ignore_whitespace, FILE *outfile)
165 46f68b20 2019-10-19 stsp return diff_blobs(blob1, blob2, label1, label2, 0, 0, diff_context,
166 63035f9f 2019-10-06 stsp ignore_whitespace, outfile, NULL);
169 7f1f93af 2019-08-06 stsp static const struct got_error *
170 7f1f93af 2019-08-06 stsp alloc_changes(struct got_diff_changes **changes)
172 7f1f93af 2019-08-06 stsp *changes = calloc(1, sizeof(**changes));
173 7f1f93af 2019-08-06 stsp if (*changes == NULL)
174 7f1f93af 2019-08-06 stsp return got_error_from_errno("calloc");
175 7f1f93af 2019-08-06 stsp SIMPLEQ_INIT(&(*changes)->entries);
176 7f1f93af 2019-08-06 stsp return NULL;
179 7f1f93af 2019-08-06 stsp static const struct got_error *
180 7f1f93af 2019-08-06 stsp diff_blob_file(struct got_diff_changes **changes,
181 4ce46740 2019-08-08 stsp struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
182 63035f9f 2019-10-06 stsp const char *label2, int diff_context, int ignore_whitespace, FILE *outfile)
184 b72f483a 2019-02-05 stsp struct got_diff_state ds;
185 b72f483a 2019-02-05 stsp struct got_diff_args args;
186 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
187 b72f483a 2019-02-05 stsp FILE *f1 = NULL;
188 b72f483a 2019-02-05 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
189 b72f483a 2019-02-05 stsp char *idstr1 = NULL;
190 b72f483a 2019-02-05 stsp size_t size1;
191 b72f483a 2019-02-05 stsp int res, flags = 0;
193 7f1f93af 2019-08-06 stsp if (changes)
194 7f1f93af 2019-08-06 stsp *changes = NULL;
197 b72f483a 2019-02-05 stsp if (blob1) {
198 b72f483a 2019-02-05 stsp f1 = got_opentemp();
199 b72f483a 2019-02-05 stsp if (f1 == NULL)
200 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentemp");
201 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
202 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
207 b72f483a 2019-02-05 stsp flags |= D_EMPTY1;
208 b72f483a 2019-02-05 stsp idstr1 = "/dev/null";
211 b72f483a 2019-02-05 stsp if (f2 == NULL)
212 b72f483a 2019-02-05 stsp flags |= D_EMPTY2;
214 b72f483a 2019-02-05 stsp memset(&ds, 0, sizeof(ds));
215 b72f483a 2019-02-05 stsp /* XXX should stat buffers be passed in args instead of ds? */
216 b72f483a 2019-02-05 stsp ds.stb1.st_mode = S_IFREG;
218 b72f483a 2019-02-05 stsp ds.stb1.st_size = size1;
219 b72f483a 2019-02-05 stsp ds.stb1.st_mtime = 0; /* XXX */
221 b72f483a 2019-02-05 stsp ds.stb2.st_mode = S_IFREG;
222 b72f483a 2019-02-05 stsp ds.stb2.st_size = size2;
223 b72f483a 2019-02-05 stsp ds.stb2.st_mtime = 0; /* XXX */
225 b72f483a 2019-02-05 stsp memset(&args, 0, sizeof(args));
226 b72f483a 2019-02-05 stsp args.diff_format = D_UNIFIED;
227 b72f483a 2019-02-05 stsp args.label[0] = label2;
228 b72f483a 2019-02-05 stsp args.label[1] = label2;
229 b72f483a 2019-02-05 stsp args.diff_context = diff_context;
230 b72f483a 2019-02-05 stsp flags |= D_PROTOTYPE;
231 63035f9f 2019-10-06 stsp if (ignore_whitespace)
232 63035f9f 2019-10-06 stsp flags |= D_IGNOREBLANKS;
234 7f1f93af 2019-08-06 stsp if (outfile) {
235 4ce46740 2019-08-08 stsp fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
236 7f1f93af 2019-08-06 stsp fprintf(outfile, "file + %s\n",
237 7f1f93af 2019-08-06 stsp f2 == NULL ? "/dev/null" : label2);
239 7f1f93af 2019-08-06 stsp if (changes) {
240 7f1f93af 2019-08-06 stsp err = alloc_changes(changes);
242 7f1f93af 2019-08-06 stsp return err;
244 7f1f93af 2019-08-06 stsp err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile,
245 7f1f93af 2019-08-06 stsp changes ? *changes : NULL);
246 dc424a06 2019-08-07 stsp got_diff_state_free(&ds);
248 fb43ecf1 2019-02-11 stsp if (f1 && fclose(f1) != 0 && err == NULL)
249 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
250 b72f483a 2019-02-05 stsp return err;
253 7f1f93af 2019-08-06 stsp const struct got_error *
254 4ce46740 2019-08-08 stsp got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
255 4ce46740 2019-08-08 stsp FILE *f2, size_t size2, const char *label2, int diff_context,
256 63035f9f 2019-10-06 stsp int ignore_whitespace, FILE *outfile)
258 4ce46740 2019-08-08 stsp return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
259 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, outfile);
262 4c9641fd 2019-08-21 stsp const struct got_error *
263 4c9641fd 2019-08-21 stsp got_diff_blob_file_lines_changed(struct got_diff_changes **changes,
264 4c9641fd 2019-08-21 stsp struct got_blob_object *blob1, FILE *f2, size_t size2)
266 4c9641fd 2019-08-21 stsp return diff_blob_file(changes, blob1, NULL, f2, size2, NULL,
267 63035f9f 2019-10-06 stsp 0, 0, NULL);
270 7f1f93af 2019-08-06 stsp const struct got_error *
271 404c43c4 2018-06-21 stsp got_diff_blob_lines_changed(struct got_diff_changes **changes,
272 404c43c4 2018-06-21 stsp struct got_blob_object *blob1, struct got_blob_object *blob2)
274 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
276 7f1f93af 2019-08-06 stsp err = alloc_changes(changes);
278 7f1f93af 2019-08-06 stsp return err;
280 46f68b20 2019-10-19 stsp err = diff_blobs(blob1, blob2, NULL, NULL, 0, 0, 3, 0, NULL, *changes);
282 404c43c4 2018-06-21 stsp got_diff_free_changes(*changes);
283 404c43c4 2018-06-21 stsp *changes = NULL;
285 404c43c4 2018-06-21 stsp return err;
289 404c43c4 2018-06-21 stsp got_diff_free_changes(struct got_diff_changes *changes)
291 404c43c4 2018-06-21 stsp struct got_diff_change *change;
292 404c43c4 2018-06-21 stsp while (!SIMPLEQ_EMPTY(&changes->entries)) {
293 404c43c4 2018-06-21 stsp change = SIMPLEQ_FIRST(&changes->entries);
294 404c43c4 2018-06-21 stsp SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
295 404c43c4 2018-06-21 stsp free(change);
297 404c43c4 2018-06-21 stsp free(changes);
300 474b4f94 2017-11-30 stsp static const struct got_error *
301 46f68b20 2019-10-19 stsp diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
302 aaa13589 2019-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
304 4e22badc 2017-11-30 stsp const struct got_error *err;
305 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
306 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
308 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
310 4e22badc 2017-11-30 stsp return err;
312 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
315 46f68b20 2019-10-19 stsp err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
317 2acfca77 2018-04-01 stsp got_object_close(obj);
319 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
320 2acfca77 2018-04-01 stsp return err;
323 474b4f94 2017-11-30 stsp static const struct got_error *
324 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
325 46f68b20 2019-10-19 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
326 46f68b20 2019-10-19 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
328 6a213ccb 2017-11-30 stsp const struct got_error *err;
329 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
330 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
331 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
332 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
334 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
336 730a8aa0 2018-04-24 stsp return err;
337 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
338 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
342 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
345 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
346 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
350 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
354 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
358 46f68b20 2019-10-19 stsp err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
362 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
364 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
366 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
368 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
369 6a213ccb 2017-11-30 stsp return err;
372 474b4f94 2017-11-30 stsp static const struct got_error *
373 46f68b20 2019-10-19 stsp diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
374 aaa13589 2019-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
376 365fb436 2017-11-30 stsp const struct got_error *err;
377 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
378 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
380 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
382 365fb436 2017-11-30 stsp return err;
384 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
387 46f68b20 2019-10-19 stsp err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
389 2acfca77 2018-04-01 stsp got_object_close(obj);
391 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
392 2acfca77 2018-04-01 stsp return err;
395 474b4f94 2017-11-30 stsp static const struct got_error *
396 54156555 2018-12-24 stsp diff_added_tree(struct got_object_id *id, const char *label,
397 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
398 31b4484f 2019-07-27 stsp int diff_content)
400 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
401 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
402 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
404 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
408 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
409 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
413 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
417 31b4484f 2019-07-27 stsp err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
418 31b4484f 2019-07-27 stsp diff_content);
421 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
422 9c70d4c3 2017-11-30 stsp if (treeobj)
423 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
424 9c70d4c3 2017-11-30 stsp return err;
427 474b4f94 2017-11-30 stsp static const struct got_error *
428 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
429 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
430 31b4484f 2019-07-27 stsp got_diff_blob_cb cb, void *cb_arg, int diff_content)
432 f6861a81 2018-09-13 stsp const struct got_error *err;
433 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
434 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
435 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
436 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
438 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
442 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
443 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
447 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
451 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
452 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
456 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
460 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
464 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
465 31b4484f 2019-07-27 stsp diff_content);
469 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
471 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
472 789689b5 2017-11-30 stsp if (treeobj1)
473 789689b5 2017-11-30 stsp got_object_close(treeobj1);
474 789689b5 2017-11-30 stsp if (treeobj2)
475 789689b5 2017-11-30 stsp got_object_close(treeobj2);
476 789689b5 2017-11-30 stsp return err;
479 474b4f94 2017-11-30 stsp static const struct got_error *
480 54156555 2018-12-24 stsp diff_deleted_tree(struct got_object_id *id, const char *label,
481 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
482 31b4484f 2019-07-27 stsp int diff_content)
484 f6861a81 2018-09-13 stsp const struct got_error *err;
485 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
486 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
488 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
492 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
493 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
497 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
501 31b4484f 2019-07-27 stsp err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
502 31b4484f 2019-07-27 stsp diff_content);
505 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
506 2c56f2ce 2017-11-30 stsp if (treeobj)
507 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
508 2c56f2ce 2017-11-30 stsp return err;
511 474b4f94 2017-11-30 stsp static const struct got_error *
512 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
513 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
514 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
516 013404a9 2017-11-30 stsp /* XXX TODO */
517 474b4f94 2017-11-30 stsp return NULL;
520 474b4f94 2017-11-30 stsp static const struct got_error *
521 56e0773d 2019-11-28 stsp diff_entry_old_new(struct got_tree_entry *te1,
522 56e0773d 2019-11-28 stsp struct got_tree_entry *te2, const char *label1, const char *label2,
523 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
524 31b4484f 2019-07-27 stsp int diff_content)
526 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
527 19ae6da1 2018-11-05 stsp int id_match;
529 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te1))
530 63c5ca5d 2019-08-24 stsp return NULL;
532 474b4f94 2017-11-30 stsp if (te2 == NULL) {
533 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
534 56e0773d 2019-11-28 stsp err = diff_deleted_tree(&te1->id, label1, repo,
535 31b4484f 2019-07-27 stsp cb, cb_arg, diff_content);
537 31b4484f 2019-07-27 stsp if (diff_content)
538 56e0773d 2019-11-28 stsp err = diff_deleted_blob(&te1->id, label1,
539 46f68b20 2019-10-19 stsp te1->mode, repo, cb, cb_arg);
541 56e0773d 2019-11-28 stsp err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
542 46f68b20 2019-10-19 stsp label1, NULL, te1->mode, 0, repo);
544 f6861a81 2018-09-13 stsp return err;
545 63c5ca5d 2019-08-24 stsp } else if (got_object_tree_entry_is_submodule(te2))
546 63c5ca5d 2019-08-24 stsp return NULL;
548 56e0773d 2019-11-28 stsp id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
549 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
550 19ae6da1 2018-11-05 stsp if (!id_match)
551 56e0773d 2019-11-28 stsp return diff_modified_tree(&te1->id, &te2->id,
552 31b4484f 2019-07-27 stsp label1, label2, repo, cb, cb_arg, diff_content);
553 4209f790 2017-11-30 stsp } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
554 46f68b20 2019-10-19 stsp if (!id_match ||
555 46f68b20 2019-10-19 stsp (te1->mode & S_IXUSR) != (te2->mode & S_IXUSR)) {
556 31b4484f 2019-07-27 stsp if (diff_content)
557 56e0773d 2019-11-28 stsp return diff_modified_blob(&te1->id, &te2->id,
558 46f68b20 2019-10-19 stsp label1, label2, te1->mode, te2->mode,
559 46f68b20 2019-10-19 stsp repo, cb, cb_arg);
561 56e0773d 2019-11-28 stsp return cb(cb_arg, NULL, NULL, &te1->id,
562 56e0773d 2019-11-28 stsp &te2->id, label1, label2, te1->mode,
563 46f68b20 2019-10-19 stsp te2->mode, repo);
567 19ae6da1 2018-11-05 stsp if (id_match)
568 f6861a81 2018-09-13 stsp return NULL;
570 56e0773d 2019-11-28 stsp return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
571 aaa13589 2019-06-01 stsp cb, cb_arg);
574 474b4f94 2017-11-30 stsp static const struct got_error *
575 56e0773d 2019-11-28 stsp diff_entry_new_old(struct got_tree_entry *te2,
576 56e0773d 2019-11-28 stsp struct got_tree_entry *te1, const char *label2,
577 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
578 31b4484f 2019-07-27 stsp int diff_content)
580 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
581 63c5ca5d 2019-08-24 stsp return NULL;
583 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te2))
584 f6861a81 2018-09-13 stsp return NULL;
586 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
587 56e0773d 2019-11-28 stsp return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
588 31b4484f 2019-07-27 stsp diff_content);
590 31b4484f 2019-07-27 stsp if (diff_content)
591 56e0773d 2019-11-28 stsp return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
594 56e0773d 2019-11-28 stsp return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
595 46f68b20 2019-10-19 stsp te2->mode, repo);
598 0208f208 2020-05-05 stsp const struct got_error *
599 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
600 0208f208 2020-05-05 stsp struct got_blob_object *blob2, struct got_object_id *id1,
601 0208f208 2020-05-05 stsp struct got_object_id *id2, const char *label1, const char *label2,
602 0208f208 2020-05-05 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
604 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
605 0208f208 2020-05-05 stsp struct got_pathlist_head *paths = arg;
606 0208f208 2020-05-05 stsp struct got_diff_changed_path *change = NULL;
607 0208f208 2020-05-05 stsp char *path = NULL;
609 0208f208 2020-05-05 stsp path = strdup(label2 ? label2 : label1);
610 0208f208 2020-05-05 stsp if (path == NULL)
611 0208f208 2020-05-05 stsp return got_error_from_errno("malloc");
613 0208f208 2020-05-05 stsp change = malloc(sizeof(*change));
614 0208f208 2020-05-05 stsp if (change == NULL) {
615 0208f208 2020-05-05 stsp err = got_error_from_errno("malloc");
619 0208f208 2020-05-05 stsp change->status = GOT_STATUS_NO_CHANGE;
620 0208f208 2020-05-05 stsp if (id1 == NULL)
621 0208f208 2020-05-05 stsp change->status = GOT_STATUS_ADD;
622 0208f208 2020-05-05 stsp else if (id2 == NULL)
623 0208f208 2020-05-05 stsp change->status = GOT_STATUS_DELETE;
625 0208f208 2020-05-05 stsp if (got_object_id_cmp(id1, id2) != 0)
626 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODIFY;
627 0208f208 2020-05-05 stsp else if (mode1 != mode2)
628 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODE_CHANGE;
631 0208f208 2020-05-05 stsp err = got_pathlist_insert(NULL, paths, path, change);
634 0208f208 2020-05-05 stsp free(path);
635 0208f208 2020-05-05 stsp free(change);
637 0208f208 2020-05-05 stsp return err;
640 474b4f94 2017-11-30 stsp const struct got_error *
641 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
642 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
643 31b4484f 2019-07-27 stsp got_diff_blob_cb cb, void *cb_arg, int diff_content)
645 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
646 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
647 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
648 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
649 56e0773d 2019-11-28 stsp int tidx1 = 0, tidx2 = 0;
651 883f0469 2018-06-23 stsp if (tree1) {
652 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, 0);
653 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
654 f6861a81 2018-09-13 stsp te1->name) == -1)
655 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
657 883f0469 2018-06-23 stsp if (tree2) {
658 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, 0);
659 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
660 f6861a81 2018-09-13 stsp te2->name) == -1)
661 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
666 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
668 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
669 1de5e065 2019-06-01 stsp te1->name);
673 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
674 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
676 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
678 aaa13589 2019-06-01 stsp err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
679 31b4484f 2019-07-27 stsp cb_arg, diff_content);
685 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
687 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
688 1de5e065 2019-06-01 stsp te2->name);
691 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
692 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
694 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
696 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
697 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->name) == -1)
699 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
701 31b4484f 2019-07-27 stsp err = diff_entry_new_old(te2, te, l2, repo,
702 31b4484f 2019-07-27 stsp cb, cb_arg, diff_content);
711 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, tidx1);
713 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
714 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
715 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
721 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, tidx2);
723 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
724 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
725 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
727 474b4f94 2017-11-30 stsp } while (te1 || te2);
729 11528a82 2018-05-19 stsp return err;
732 11528a82 2018-05-19 stsp const struct got_error *
733 15a94983 2018-12-23 stsp got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
734 54156555 2018-12-24 stsp const char *label1, const char *label2, int diff_context,
735 63035f9f 2019-10-06 stsp int ignore_whitespace, struct got_repository *repo, FILE *outfile)
737 11528a82 2018-05-19 stsp const struct got_error *err;
738 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
740 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
741 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
744 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob1, repo, id1, 8192);
749 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob2, repo, id2, 8192);
753 54156555 2018-12-24 stsp err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
754 63035f9f 2019-10-06 stsp ignore_whitespace, outfile);
757 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
759 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
760 474b4f94 2017-11-30 stsp return err;
763 11528a82 2018-05-19 stsp const struct got_error *
764 15a94983 2018-12-23 stsp got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
765 63035f9f 2019-10-06 stsp char *label1, char *label2, int diff_context, int ignore_whitespace,
766 63035f9f 2019-10-06 stsp struct got_repository *repo, FILE *outfile)
768 11528a82 2018-05-19 stsp const struct got_error *err;
769 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
770 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
772 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
773 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
776 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
781 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
785 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
786 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
787 aaa13589 2019-06-01 stsp arg.outfile = outfile;
788 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, label1, label2, repo,
789 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
792 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
794 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
795 11528a82 2018-05-19 stsp return err;
798 11528a82 2018-05-19 stsp const struct got_error *
799 15a94983 2018-12-23 stsp got_diff_objects_as_commits(struct got_object_id *id1,
800 63035f9f 2019-10-06 stsp struct got_object_id *id2, int diff_context, int ignore_whitespace,
801 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
803 11528a82 2018-05-19 stsp const struct got_error *err;
804 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
806 15a94983 2018-12-23 stsp if (id2 == NULL)
807 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
810 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
815 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
819 15a94983 2018-12-23 stsp err = got_diff_objects_as_trees(
820 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
821 63035f9f 2019-10-06 stsp got_object_commit_get_tree_id(commit2), "", "", diff_context,
822 63035f9f 2019-10-06 stsp ignore_whitespace, repo, outfile);
824 11528a82 2018-05-19 stsp if (commit1)
825 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
826 11528a82 2018-05-19 stsp if (commit2)
827 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
828 11528a82 2018-05-19 stsp return err;
831 dc424a06 2019-08-07 stsp const struct got_error *
832 dc424a06 2019-08-07 stsp got_diff_files(struct got_diff_changes **changes,
833 dc424a06 2019-08-07 stsp struct got_diff_state **ds,
834 dc424a06 2019-08-07 stsp struct got_diff_args **args,
835 dc424a06 2019-08-07 stsp int *flags,
836 dc424a06 2019-08-07 stsp FILE *f1, size_t size1, const char *label1,
837 dc424a06 2019-08-07 stsp FILE *f2, size_t size2, const char *label2,
838 dc424a06 2019-08-07 stsp int diff_context, FILE *outfile)
840 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
843 dc424a06 2019-08-07 stsp *flags = 0;
844 dc424a06 2019-08-07 stsp *ds = calloc(1, sizeof(**ds));
845 dc424a06 2019-08-07 stsp if (*ds == NULL)
846 dc424a06 2019-08-07 stsp return got_error_from_errno("calloc");
847 dc424a06 2019-08-07 stsp *args = calloc(1, sizeof(**args));
848 dc424a06 2019-08-07 stsp if (*args == NULL) {
849 dc424a06 2019-08-07 stsp err = got_error_from_errno("calloc");
853 dc424a06 2019-08-07 stsp if (changes)
854 dc424a06 2019-08-07 stsp *changes = NULL;
856 dc424a06 2019-08-07 stsp if (f1 == NULL)
857 dc424a06 2019-08-07 stsp *flags |= D_EMPTY1;
859 dc424a06 2019-08-07 stsp if (f2 == NULL)
860 dc424a06 2019-08-07 stsp *flags |= D_EMPTY2;
862 dc424a06 2019-08-07 stsp /* XXX should stat buffers be passed in args instead of ds? */
863 dc424a06 2019-08-07 stsp (*ds)->stb1.st_mode = S_IFREG;
864 dc424a06 2019-08-07 stsp (*ds)->stb1.st_size = size1;
865 dc424a06 2019-08-07 stsp (*ds)->stb1.st_mtime = 0; /* XXX */
867 dc424a06 2019-08-07 stsp (*ds)->stb2.st_mode = S_IFREG;
868 dc424a06 2019-08-07 stsp (*ds)->stb2.st_size = size2;
869 dc424a06 2019-08-07 stsp (*ds)->stb2.st_mtime = 0; /* XXX */
871 dc424a06 2019-08-07 stsp (*args)->diff_format = D_UNIFIED;
872 dc424a06 2019-08-07 stsp (*args)->label[0] = label1;
873 dc424a06 2019-08-07 stsp (*args)->label[1] = label2;
874 dc424a06 2019-08-07 stsp (*args)->diff_context = diff_context;
875 dc424a06 2019-08-07 stsp *flags |= D_PROTOTYPE;
877 dc424a06 2019-08-07 stsp if (outfile) {
878 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
879 dc424a06 2019-08-07 stsp f1 == NULL ? "/dev/null" : label1);
880 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
881 dc424a06 2019-08-07 stsp f2 == NULL ? "/dev/null" : label2);
883 dc424a06 2019-08-07 stsp if (changes) {
884 dc424a06 2019-08-07 stsp err = alloc_changes(changes);
888 dc424a06 2019-08-07 stsp err = got_diffreg(&res, f1, f2, *flags, *args, *ds, outfile,
889 dc424a06 2019-08-07 stsp changes ? *changes : NULL);
893 dc424a06 2019-08-07 stsp got_diff_state_free(*ds);
895 dc424a06 2019-08-07 stsp *ds = NULL;
897 dc424a06 2019-08-07 stsp if (*args) {
898 dc424a06 2019-08-07 stsp free(*args);
899 dc424a06 2019-08-07 stsp *args = NULL;
901 dc424a06 2019-08-07 stsp if (changes) {
902 dc424a06 2019-08-07 stsp if (*changes)
903 dc424a06 2019-08-07 stsp got_diff_free_changes(*changes);
904 dc424a06 2019-08-07 stsp *changes = NULL;
907 dc424a06 2019-08-07 stsp return err;