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"
34 718b3ab0 2018-03-17 stsp #include "got_lib_diff.h"
35 15a94983 2018-12-23 stsp #include "got_lib_delta.h"
36 15a94983 2018-12-23 stsp #include "got_lib_inflate.h"
37 15a94983 2018-12-23 stsp #include "got_lib_object.h"
39 404c43c4 2018-06-21 stsp static const struct got_error *
40 404c43c4 2018-06-21 stsp diff_blobs(struct got_blob_object *blob1, struct got_blob_object *blob2,
41 46f68b20 2019-10-19 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
42 46f68b20 2019-10-19 stsp int diff_context, int ignore_whitespace, FILE *outfile,
43 46f68b20 2019-10-19 stsp struct got_diff_changes *changes)
45 ed9e98a8 2017-11-29 stsp struct got_diff_state ds;
46 8ba9a219 2017-11-29 stsp struct got_diff_args args;
47 7d283eee 2017-11-29 stsp const struct got_error *err = NULL;
48 4e22badc 2017-11-30 stsp FILE *f1 = NULL, *f2 = NULL;
49 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
50 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
51 98abbc84 2017-11-30 stsp char *idstr1 = NULL, *idstr2 = NULL;
52 f934cf2c 2018-02-12 stsp size_t size1, size2;
53 4e22badc 2017-11-30 stsp int res, flags = 0;
55 4e22badc 2017-11-30 stsp if (blob1) {
56 a1fd68d8 2018-01-12 stsp f1 = got_opentemp();
57 4e22badc 2017-11-30 stsp if (f1 == NULL)
58 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentemp");
60 4e22badc 2017-11-30 stsp flags |= D_EMPTY1;
62 4e22badc 2017-11-30 stsp if (blob2) {
63 a1fd68d8 2018-01-12 stsp f2 = got_opentemp();
64 4e22badc 2017-11-30 stsp if (f2 == NULL) {
65 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
70 4e22badc 2017-11-30 stsp flags |= D_EMPTY2;
73 98abbc84 2017-11-30 stsp if (blob1) {
74 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
75 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
80 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
83 98abbc84 2017-11-30 stsp if (blob2) {
84 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
85 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
90 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
92 ed9e98a8 2017-11-29 stsp memset(&ds, 0, sizeof(ds));
93 f9d67749 2017-11-30 stsp /* XXX should stat buffers be passed in args instead of ds? */
94 f9d67749 2017-11-30 stsp ds.stb1.st_mode = S_IFREG;
96 f934cf2c 2018-02-12 stsp ds.stb1.st_size = size1;
97 f9d67749 2017-11-30 stsp ds.stb1.st_mtime = 0; /* XXX */
99 f9d67749 2017-11-30 stsp ds.stb2.st_mode = S_IFREG;
101 f934cf2c 2018-02-12 stsp ds.stb2.st_size = size2;
102 f9d67749 2017-11-30 stsp ds.stb2.st_mtime = 0; /* XXX */
104 54156555 2018-12-24 stsp memset(&args, 0, sizeof(args));
105 8ba9a219 2017-11-29 stsp args.diff_format = D_UNIFIED;
106 54156555 2018-12-24 stsp args.label[0] = label1 ? label1 : idstr1;
107 54156555 2018-12-24 stsp args.label[1] = label2 ? label2 : idstr2;
108 df2871d2 2018-10-18 stsp args.diff_context = diff_context;
109 84eb021e 2018-03-27 stsp flags |= D_PROTOTYPE;
110 63035f9f 2019-10-06 stsp if (ignore_whitespace)
111 63035f9f 2019-10-06 stsp flags |= D_IGNOREBLANKS;
113 09de383e 2018-12-24 stsp if (outfile) {
114 46f68b20 2019-10-19 stsp char *modestr1 = NULL, *modestr2 = NULL;
115 46f68b20 2019-10-19 stsp if (mode1 && mode1 != mode2) {
116 46f68b20 2019-10-19 stsp if (asprintf(&modestr1, " (mode %o)",
117 46f68b20 2019-10-19 stsp mode1 & (S_IRWXU | S_IRWXG | S_IRWXO)) == -1) {
118 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
122 46f68b20 2019-10-19 stsp if (mode2 && mode1 != mode2) {
123 46f68b20 2019-10-19 stsp if (asprintf(&modestr2, " (mode %o)",
124 46f68b20 2019-10-19 stsp mode2 & (S_IRWXU | S_IRWXG | S_IRWXO)) == -1) {
125 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
129 46f68b20 2019-10-19 stsp fprintf(outfile, "blob - %s%s\n", idstr1,
130 46f68b20 2019-10-19 stsp modestr1 ? modestr1 : "");
131 46f68b20 2019-10-19 stsp fprintf(outfile, "blob + %s%s\n", idstr2,
132 46f68b20 2019-10-19 stsp modestr2 ? modestr2 : "");
133 46f68b20 2019-10-19 stsp free(modestr1);
134 46f68b20 2019-10-19 stsp free(modestr2);
136 404c43c4 2018-06-21 stsp err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
137 dc424a06 2019-08-07 stsp got_diff_state_free(&ds);
139 fb43ecf1 2019-02-11 stsp if (f1 && fclose(f1) != 0 && err == NULL)
140 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
141 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
142 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
143 7d283eee 2017-11-29 stsp return err;
146 aaa13589 2019-06-01 stsp const struct got_error *
147 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
148 aaa13589 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
149 aaa13589 2019-06-01 stsp struct got_object_id *id2, const char *label1, const char *label2,
150 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
152 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
154 46f68b20 2019-10-19 stsp return diff_blobs(blob1, blob2, label1, label2, mode1, mode2,
155 46f68b20 2019-10-19 stsp a->diff_context, a->ignore_whitespace, a->outfile, NULL);
158 404c43c4 2018-06-21 stsp const struct got_error *
159 404c43c4 2018-06-21 stsp got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
160 63035f9f 2019-10-06 stsp const char *label1, const char *label2, int diff_context,
161 63035f9f 2019-10-06 stsp int ignore_whitespace, FILE *outfile)
163 46f68b20 2019-10-19 stsp return diff_blobs(blob1, blob2, label1, label2, 0, 0, diff_context,
164 63035f9f 2019-10-06 stsp ignore_whitespace, outfile, NULL);
167 7f1f93af 2019-08-06 stsp static const struct got_error *
168 7f1f93af 2019-08-06 stsp alloc_changes(struct got_diff_changes **changes)
170 7f1f93af 2019-08-06 stsp *changes = calloc(1, sizeof(**changes));
171 7f1f93af 2019-08-06 stsp if (*changes == NULL)
172 7f1f93af 2019-08-06 stsp return got_error_from_errno("calloc");
173 7f1f93af 2019-08-06 stsp SIMPLEQ_INIT(&(*changes)->entries);
174 7f1f93af 2019-08-06 stsp return NULL;
177 7f1f93af 2019-08-06 stsp static const struct got_error *
178 7f1f93af 2019-08-06 stsp diff_blob_file(struct got_diff_changes **changes,
179 4ce46740 2019-08-08 stsp struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
180 63035f9f 2019-10-06 stsp const char *label2, int diff_context, int ignore_whitespace, FILE *outfile)
182 b72f483a 2019-02-05 stsp struct got_diff_state ds;
183 b72f483a 2019-02-05 stsp struct got_diff_args args;
184 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
185 b72f483a 2019-02-05 stsp FILE *f1 = NULL;
186 b72f483a 2019-02-05 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
187 b72f483a 2019-02-05 stsp char *idstr1 = NULL;
188 b72f483a 2019-02-05 stsp size_t size1;
189 b72f483a 2019-02-05 stsp int res, flags = 0;
191 7f1f93af 2019-08-06 stsp if (changes)
192 7f1f93af 2019-08-06 stsp *changes = NULL;
195 b72f483a 2019-02-05 stsp if (blob1) {
196 b72f483a 2019-02-05 stsp f1 = got_opentemp();
197 b72f483a 2019-02-05 stsp if (f1 == NULL)
198 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentemp");
199 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
200 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
205 b72f483a 2019-02-05 stsp flags |= D_EMPTY1;
206 b72f483a 2019-02-05 stsp idstr1 = "/dev/null";
209 b72f483a 2019-02-05 stsp if (f2 == NULL)
210 b72f483a 2019-02-05 stsp flags |= D_EMPTY2;
212 b72f483a 2019-02-05 stsp memset(&ds, 0, sizeof(ds));
213 b72f483a 2019-02-05 stsp /* XXX should stat buffers be passed in args instead of ds? */
214 b72f483a 2019-02-05 stsp ds.stb1.st_mode = S_IFREG;
216 b72f483a 2019-02-05 stsp ds.stb1.st_size = size1;
217 b72f483a 2019-02-05 stsp ds.stb1.st_mtime = 0; /* XXX */
219 b72f483a 2019-02-05 stsp ds.stb2.st_mode = S_IFREG;
220 b72f483a 2019-02-05 stsp ds.stb2.st_size = size2;
221 b72f483a 2019-02-05 stsp ds.stb2.st_mtime = 0; /* XXX */
223 b72f483a 2019-02-05 stsp memset(&args, 0, sizeof(args));
224 b72f483a 2019-02-05 stsp args.diff_format = D_UNIFIED;
225 b72f483a 2019-02-05 stsp args.label[0] = label2;
226 b72f483a 2019-02-05 stsp args.label[1] = label2;
227 b72f483a 2019-02-05 stsp args.diff_context = diff_context;
228 b72f483a 2019-02-05 stsp flags |= D_PROTOTYPE;
229 63035f9f 2019-10-06 stsp if (ignore_whitespace)
230 63035f9f 2019-10-06 stsp flags |= D_IGNOREBLANKS;
232 7f1f93af 2019-08-06 stsp if (outfile) {
233 4ce46740 2019-08-08 stsp fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
234 7f1f93af 2019-08-06 stsp fprintf(outfile, "file + %s\n",
235 7f1f93af 2019-08-06 stsp f2 == NULL ? "/dev/null" : label2);
237 7f1f93af 2019-08-06 stsp if (changes) {
238 7f1f93af 2019-08-06 stsp err = alloc_changes(changes);
240 7f1f93af 2019-08-06 stsp return err;
242 7f1f93af 2019-08-06 stsp err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile,
243 7f1f93af 2019-08-06 stsp changes ? *changes : NULL);
244 dc424a06 2019-08-07 stsp got_diff_state_free(&ds);
246 fb43ecf1 2019-02-11 stsp if (f1 && fclose(f1) != 0 && err == NULL)
247 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
248 b72f483a 2019-02-05 stsp return err;
251 7f1f93af 2019-08-06 stsp const struct got_error *
252 4ce46740 2019-08-08 stsp got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
253 4ce46740 2019-08-08 stsp FILE *f2, size_t size2, const char *label2, int diff_context,
254 63035f9f 2019-10-06 stsp int ignore_whitespace, FILE *outfile)
256 4ce46740 2019-08-08 stsp return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
257 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, outfile);
260 4c9641fd 2019-08-21 stsp const struct got_error *
261 4c9641fd 2019-08-21 stsp got_diff_blob_file_lines_changed(struct got_diff_changes **changes,
262 4c9641fd 2019-08-21 stsp struct got_blob_object *blob1, FILE *f2, size_t size2)
264 4c9641fd 2019-08-21 stsp return diff_blob_file(changes, blob1, NULL, f2, size2, NULL,
265 63035f9f 2019-10-06 stsp 0, 0, NULL);
268 7f1f93af 2019-08-06 stsp const struct got_error *
269 404c43c4 2018-06-21 stsp got_diff_blob_lines_changed(struct got_diff_changes **changes,
270 404c43c4 2018-06-21 stsp struct got_blob_object *blob1, struct got_blob_object *blob2)
272 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
274 7f1f93af 2019-08-06 stsp err = alloc_changes(changes);
276 7f1f93af 2019-08-06 stsp return err;
278 46f68b20 2019-10-19 stsp err = diff_blobs(blob1, blob2, NULL, NULL, 0, 0, 3, 0, NULL, *changes);
280 404c43c4 2018-06-21 stsp got_diff_free_changes(*changes);
281 404c43c4 2018-06-21 stsp *changes = NULL;
283 404c43c4 2018-06-21 stsp return err;
287 404c43c4 2018-06-21 stsp got_diff_free_changes(struct got_diff_changes *changes)
289 404c43c4 2018-06-21 stsp struct got_diff_change *change;
290 404c43c4 2018-06-21 stsp while (!SIMPLEQ_EMPTY(&changes->entries)) {
291 404c43c4 2018-06-21 stsp change = SIMPLEQ_FIRST(&changes->entries);
292 404c43c4 2018-06-21 stsp SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
293 404c43c4 2018-06-21 stsp free(change);
295 404c43c4 2018-06-21 stsp free(changes);
298 474b4f94 2017-11-30 stsp static const struct got_error *
299 46f68b20 2019-10-19 stsp diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
300 aaa13589 2019-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
302 4e22badc 2017-11-30 stsp const struct got_error *err;
303 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
304 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
306 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
308 4e22badc 2017-11-30 stsp return err;
310 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
313 46f68b20 2019-10-19 stsp err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
315 2acfca77 2018-04-01 stsp got_object_close(obj);
317 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
318 2acfca77 2018-04-01 stsp return err;
321 474b4f94 2017-11-30 stsp static const struct got_error *
322 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
323 46f68b20 2019-10-19 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
324 46f68b20 2019-10-19 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
326 6a213ccb 2017-11-30 stsp const struct got_error *err;
327 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
328 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
329 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
330 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
332 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
334 730a8aa0 2018-04-24 stsp return err;
335 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
336 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
340 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
343 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
344 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
348 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
352 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
356 46f68b20 2019-10-19 stsp err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
360 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
362 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
364 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
366 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
367 6a213ccb 2017-11-30 stsp return err;
370 474b4f94 2017-11-30 stsp static const struct got_error *
371 46f68b20 2019-10-19 stsp diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
372 aaa13589 2019-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
374 365fb436 2017-11-30 stsp const struct got_error *err;
375 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
376 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
378 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
380 365fb436 2017-11-30 stsp return err;
382 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
385 46f68b20 2019-10-19 stsp err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
387 2acfca77 2018-04-01 stsp got_object_close(obj);
389 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
390 2acfca77 2018-04-01 stsp return err;
393 474b4f94 2017-11-30 stsp static const struct got_error *
394 54156555 2018-12-24 stsp diff_added_tree(struct got_object_id *id, const char *label,
395 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
396 31b4484f 2019-07-27 stsp int diff_content)
398 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
399 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
400 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
402 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
406 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
407 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
411 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
415 31b4484f 2019-07-27 stsp err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
416 31b4484f 2019-07-27 stsp diff_content);
419 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
420 9c70d4c3 2017-11-30 stsp if (treeobj)
421 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
422 9c70d4c3 2017-11-30 stsp return err;
425 474b4f94 2017-11-30 stsp static const struct got_error *
426 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
427 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
428 31b4484f 2019-07-27 stsp got_diff_blob_cb cb, void *cb_arg, int diff_content)
430 f6861a81 2018-09-13 stsp const struct got_error *err;
431 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
432 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
433 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
434 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
436 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
440 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
441 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
445 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
449 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
450 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
454 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
458 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
462 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
463 31b4484f 2019-07-27 stsp diff_content);
467 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
469 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
470 789689b5 2017-11-30 stsp if (treeobj1)
471 789689b5 2017-11-30 stsp got_object_close(treeobj1);
472 789689b5 2017-11-30 stsp if (treeobj2)
473 789689b5 2017-11-30 stsp got_object_close(treeobj2);
474 789689b5 2017-11-30 stsp return err;
477 474b4f94 2017-11-30 stsp static const struct got_error *
478 54156555 2018-12-24 stsp diff_deleted_tree(struct got_object_id *id, const char *label,
479 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
480 31b4484f 2019-07-27 stsp int diff_content)
482 f6861a81 2018-09-13 stsp const struct got_error *err;
483 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
484 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
486 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
490 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
491 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
495 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
499 31b4484f 2019-07-27 stsp err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
500 31b4484f 2019-07-27 stsp diff_content);
503 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
504 2c56f2ce 2017-11-30 stsp if (treeobj)
505 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
506 2c56f2ce 2017-11-30 stsp return err;
509 474b4f94 2017-11-30 stsp static const struct got_error *
510 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
511 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
512 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
514 013404a9 2017-11-30 stsp /* XXX TODO */
515 474b4f94 2017-11-30 stsp return NULL;
518 474b4f94 2017-11-30 stsp static const struct got_error *
519 56e0773d 2019-11-28 stsp diff_entry_old_new(struct got_tree_entry *te1,
520 56e0773d 2019-11-28 stsp struct got_tree_entry *te2, const char *label1, const char *label2,
521 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
522 31b4484f 2019-07-27 stsp int diff_content)
524 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
525 19ae6da1 2018-11-05 stsp int id_match;
527 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te1))
528 63c5ca5d 2019-08-24 stsp return NULL;
530 474b4f94 2017-11-30 stsp if (te2 == NULL) {
531 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
532 56e0773d 2019-11-28 stsp err = diff_deleted_tree(&te1->id, label1, repo,
533 31b4484f 2019-07-27 stsp cb, cb_arg, diff_content);
535 31b4484f 2019-07-27 stsp if (diff_content)
536 56e0773d 2019-11-28 stsp err = diff_deleted_blob(&te1->id, label1,
537 46f68b20 2019-10-19 stsp te1->mode, repo, cb, cb_arg);
539 56e0773d 2019-11-28 stsp err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
540 46f68b20 2019-10-19 stsp label1, NULL, te1->mode, 0, repo);
542 f6861a81 2018-09-13 stsp return err;
543 63c5ca5d 2019-08-24 stsp } else if (got_object_tree_entry_is_submodule(te2))
544 63c5ca5d 2019-08-24 stsp return NULL;
546 56e0773d 2019-11-28 stsp id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
547 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
548 19ae6da1 2018-11-05 stsp if (!id_match)
549 56e0773d 2019-11-28 stsp return diff_modified_tree(&te1->id, &te2->id,
550 31b4484f 2019-07-27 stsp label1, label2, repo, cb, cb_arg, diff_content);
551 4209f790 2017-11-30 stsp } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
552 46f68b20 2019-10-19 stsp if (!id_match ||
553 46f68b20 2019-10-19 stsp (te1->mode & S_IXUSR) != (te2->mode & S_IXUSR)) {
554 31b4484f 2019-07-27 stsp if (diff_content)
555 56e0773d 2019-11-28 stsp return diff_modified_blob(&te1->id, &te2->id,
556 46f68b20 2019-10-19 stsp label1, label2, te1->mode, te2->mode,
557 46f68b20 2019-10-19 stsp repo, cb, cb_arg);
559 56e0773d 2019-11-28 stsp return cb(cb_arg, NULL, NULL, &te1->id,
560 56e0773d 2019-11-28 stsp &te2->id, label1, label2, te1->mode,
561 46f68b20 2019-10-19 stsp te2->mode, repo);
565 19ae6da1 2018-11-05 stsp if (id_match)
566 f6861a81 2018-09-13 stsp return NULL;
568 56e0773d 2019-11-28 stsp return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
569 aaa13589 2019-06-01 stsp cb, cb_arg);
572 474b4f94 2017-11-30 stsp static const struct got_error *
573 56e0773d 2019-11-28 stsp diff_entry_new_old(struct got_tree_entry *te2,
574 56e0773d 2019-11-28 stsp struct got_tree_entry *te1, const char *label2,
575 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
576 31b4484f 2019-07-27 stsp int diff_content)
578 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
579 63c5ca5d 2019-08-24 stsp return NULL;
581 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te2))
582 f6861a81 2018-09-13 stsp return NULL;
584 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
585 56e0773d 2019-11-28 stsp return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
586 31b4484f 2019-07-27 stsp diff_content);
588 31b4484f 2019-07-27 stsp if (diff_content)
589 56e0773d 2019-11-28 stsp return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
592 56e0773d 2019-11-28 stsp return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
593 46f68b20 2019-10-19 stsp te2->mode, repo);
596 474b4f94 2017-11-30 stsp const struct got_error *
597 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
598 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
599 31b4484f 2019-07-27 stsp got_diff_blob_cb cb, void *cb_arg, int diff_content)
601 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
602 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
603 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
604 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
605 56e0773d 2019-11-28 stsp int tidx1 = 0, tidx2 = 0;
607 883f0469 2018-06-23 stsp if (tree1) {
608 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, 0);
609 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
610 f6861a81 2018-09-13 stsp te1->name) == -1)
611 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
613 883f0469 2018-06-23 stsp if (tree2) {
614 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, 0);
615 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
616 f6861a81 2018-09-13 stsp te2->name) == -1)
617 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
622 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
624 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
625 1de5e065 2019-06-01 stsp te1->name);
629 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
630 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
632 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
634 aaa13589 2019-06-01 stsp err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
635 31b4484f 2019-07-27 stsp cb_arg, diff_content);
641 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
643 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
644 1de5e065 2019-06-01 stsp te2->name);
647 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
648 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
650 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
652 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
653 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->name) == -1)
655 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
657 31b4484f 2019-07-27 stsp err = diff_entry_new_old(te2, te, l2, repo,
658 31b4484f 2019-07-27 stsp cb, cb_arg, diff_content);
667 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, tidx1);
669 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
670 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
671 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
677 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, tidx2);
679 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
680 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
681 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
683 474b4f94 2017-11-30 stsp } while (te1 || te2);
685 11528a82 2018-05-19 stsp return err;
688 11528a82 2018-05-19 stsp const struct got_error *
689 15a94983 2018-12-23 stsp got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
690 54156555 2018-12-24 stsp const char *label1, const char *label2, int diff_context,
691 63035f9f 2019-10-06 stsp int ignore_whitespace, struct got_repository *repo, FILE *outfile)
693 11528a82 2018-05-19 stsp const struct got_error *err;
694 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
696 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
697 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
700 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob1, repo, id1, 8192);
705 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob2, repo, id2, 8192);
709 54156555 2018-12-24 stsp err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
710 63035f9f 2019-10-06 stsp ignore_whitespace, outfile);
713 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
715 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
716 474b4f94 2017-11-30 stsp return err;
719 11528a82 2018-05-19 stsp const struct got_error *
720 15a94983 2018-12-23 stsp got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
721 63035f9f 2019-10-06 stsp char *label1, char *label2, int diff_context, int ignore_whitespace,
722 63035f9f 2019-10-06 stsp struct got_repository *repo, FILE *outfile)
724 11528a82 2018-05-19 stsp const struct got_error *err;
725 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
726 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
728 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
729 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
732 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
737 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
741 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
742 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
743 aaa13589 2019-06-01 stsp arg.outfile = outfile;
744 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, label1, label2, repo,
745 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
748 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
750 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
751 11528a82 2018-05-19 stsp return err;
754 11528a82 2018-05-19 stsp const struct got_error *
755 15a94983 2018-12-23 stsp got_diff_objects_as_commits(struct got_object_id *id1,
756 63035f9f 2019-10-06 stsp struct got_object_id *id2, int diff_context, int ignore_whitespace,
757 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
759 11528a82 2018-05-19 stsp const struct got_error *err;
760 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
762 15a94983 2018-12-23 stsp if (id2 == NULL)
763 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
766 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
771 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
775 15a94983 2018-12-23 stsp err = got_diff_objects_as_trees(
776 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
777 63035f9f 2019-10-06 stsp got_object_commit_get_tree_id(commit2), "", "", diff_context,
778 63035f9f 2019-10-06 stsp ignore_whitespace, repo, outfile);
780 11528a82 2018-05-19 stsp if (commit1)
781 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
782 11528a82 2018-05-19 stsp if (commit2)
783 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
784 11528a82 2018-05-19 stsp return err;
787 dc424a06 2019-08-07 stsp const struct got_error *
788 dc424a06 2019-08-07 stsp got_diff_files(struct got_diff_changes **changes,
789 dc424a06 2019-08-07 stsp struct got_diff_state **ds,
790 dc424a06 2019-08-07 stsp struct got_diff_args **args,
791 dc424a06 2019-08-07 stsp int *flags,
792 dc424a06 2019-08-07 stsp FILE *f1, size_t size1, const char *label1,
793 dc424a06 2019-08-07 stsp FILE *f2, size_t size2, const char *label2,
794 dc424a06 2019-08-07 stsp int diff_context, FILE *outfile)
796 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
799 dc424a06 2019-08-07 stsp *flags = 0;
800 dc424a06 2019-08-07 stsp *ds = calloc(1, sizeof(**ds));
801 dc424a06 2019-08-07 stsp if (*ds == NULL)
802 dc424a06 2019-08-07 stsp return got_error_from_errno("calloc");
803 dc424a06 2019-08-07 stsp *args = calloc(1, sizeof(**args));
804 dc424a06 2019-08-07 stsp if (*args == NULL) {
805 dc424a06 2019-08-07 stsp err = got_error_from_errno("calloc");
809 dc424a06 2019-08-07 stsp if (changes)
810 dc424a06 2019-08-07 stsp *changes = NULL;
812 dc424a06 2019-08-07 stsp if (f1 == NULL)
813 dc424a06 2019-08-07 stsp *flags |= D_EMPTY1;
815 dc424a06 2019-08-07 stsp if (f2 == NULL)
816 dc424a06 2019-08-07 stsp *flags |= D_EMPTY2;
818 dc424a06 2019-08-07 stsp /* XXX should stat buffers be passed in args instead of ds? */
819 dc424a06 2019-08-07 stsp (*ds)->stb1.st_mode = S_IFREG;
820 dc424a06 2019-08-07 stsp (*ds)->stb1.st_size = size1;
821 dc424a06 2019-08-07 stsp (*ds)->stb1.st_mtime = 0; /* XXX */
823 dc424a06 2019-08-07 stsp (*ds)->stb2.st_mode = S_IFREG;
824 dc424a06 2019-08-07 stsp (*ds)->stb2.st_size = size2;
825 dc424a06 2019-08-07 stsp (*ds)->stb2.st_mtime = 0; /* XXX */
827 dc424a06 2019-08-07 stsp (*args)->diff_format = D_UNIFIED;
828 dc424a06 2019-08-07 stsp (*args)->label[0] = label1;
829 dc424a06 2019-08-07 stsp (*args)->label[1] = label2;
830 dc424a06 2019-08-07 stsp (*args)->diff_context = diff_context;
831 dc424a06 2019-08-07 stsp *flags |= D_PROTOTYPE;
833 dc424a06 2019-08-07 stsp if (outfile) {
834 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
835 dc424a06 2019-08-07 stsp f1 == NULL ? "/dev/null" : label1);
836 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
837 dc424a06 2019-08-07 stsp f2 == NULL ? "/dev/null" : label2);
839 dc424a06 2019-08-07 stsp if (changes) {
840 dc424a06 2019-08-07 stsp err = alloc_changes(changes);
844 dc424a06 2019-08-07 stsp err = got_diffreg(&res, f1, f2, *flags, *args, *ds, outfile,
845 dc424a06 2019-08-07 stsp changes ? *changes : NULL);
849 dc424a06 2019-08-07 stsp got_diff_state_free(*ds);
851 dc424a06 2019-08-07 stsp *ds = NULL;
853 dc424a06 2019-08-07 stsp if (*args) {
854 dc424a06 2019-08-07 stsp free(*args);
855 dc424a06 2019-08-07 stsp *args = NULL;
857 dc424a06 2019-08-07 stsp if (changes) {
858 dc424a06 2019-08-07 stsp if (*changes)
859 dc424a06 2019-08-07 stsp got_diff_free_changes(*changes);
860 dc424a06 2019-08-07 stsp *changes = NULL;
863 dc424a06 2019-08-07 stsp return err;