Blame


1 7d283eee 2017-11-29 stsp /*
2 7d283eee 2017-11-29 stsp * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 7d283eee 2017-11-29 stsp *
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.
7 7d283eee 2017-11-29 stsp *
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.
15 7d283eee 2017-11-29 stsp */
16 7d283eee 2017-11-29 stsp
17 7d283eee 2017-11-29 stsp #include <sys/queue.h>
18 1c7f0520 2017-11-29 stsp #include <sys/stat.h>
19 7d283eee 2017-11-29 stsp
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>
26 7d283eee 2017-11-29 stsp
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 7d283eee 2017-11-29 stsp
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"
38 a7852263 2017-11-30 stsp
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 54156555 2018-12-24 stsp const char *label1, const char *label2, int diff_context, FILE *outfile,
42 54156555 2018-12-24 stsp struct got_diff_changes *changes)
43 7d283eee 2017-11-29 stsp {
44 ed9e98a8 2017-11-29 stsp struct got_diff_state ds;
45 8ba9a219 2017-11-29 stsp struct got_diff_args args;
46 7d283eee 2017-11-29 stsp const struct got_error *err = NULL;
47 4e22badc 2017-11-30 stsp FILE *f1 = NULL, *f2 = NULL;
48 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
49 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
50 98abbc84 2017-11-30 stsp char *idstr1 = NULL, *idstr2 = NULL;
51 f934cf2c 2018-02-12 stsp size_t size1, size2;
52 4e22badc 2017-11-30 stsp int res, flags = 0;
53 7d283eee 2017-11-29 stsp
54 4e22badc 2017-11-30 stsp if (blob1) {
55 a1fd68d8 2018-01-12 stsp f1 = got_opentemp();
56 4e22badc 2017-11-30 stsp if (f1 == NULL)
57 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentemp");
58 4e22badc 2017-11-30 stsp } else
59 4e22badc 2017-11-30 stsp flags |= D_EMPTY1;
60 7d283eee 2017-11-29 stsp
61 4e22badc 2017-11-30 stsp if (blob2) {
62 a1fd68d8 2018-01-12 stsp f2 = got_opentemp();
63 4e22badc 2017-11-30 stsp if (f2 == NULL) {
64 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
65 4e22badc 2017-11-30 stsp fclose(f1);
66 fb43ecf1 2019-02-11 stsp return err;
67 4e22badc 2017-11-30 stsp }
68 4e22badc 2017-11-30 stsp } else
69 4e22badc 2017-11-30 stsp flags |= D_EMPTY2;
70 7d283eee 2017-11-29 stsp
71 f934cf2c 2018-02-12 stsp size1 = 0;
72 98abbc84 2017-11-30 stsp if (blob1) {
73 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
74 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
75 6c4c42e0 2019-06-24 stsp blob1);
76 35e9ba5d 2018-06-21 stsp if (err)
77 35e9ba5d 2018-06-21 stsp goto done;
78 98abbc84 2017-11-30 stsp } else
79 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
80 7d283eee 2017-11-29 stsp
81 f934cf2c 2018-02-12 stsp size2 = 0;
82 98abbc84 2017-11-30 stsp if (blob2) {
83 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
84 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
85 6c4c42e0 2019-06-24 stsp blob2);
86 35e9ba5d 2018-06-21 stsp if (err)
87 35e9ba5d 2018-06-21 stsp goto done;
88 98abbc84 2017-11-30 stsp } else
89 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
90 7d283eee 2017-11-29 stsp
91 ed9e98a8 2017-11-29 stsp memset(&ds, 0, sizeof(ds));
92 f9d67749 2017-11-30 stsp /* XXX should stat buffers be passed in args instead of ds? */
93 f9d67749 2017-11-30 stsp ds.stb1.st_mode = S_IFREG;
94 98abbc84 2017-11-30 stsp if (blob1)
95 f934cf2c 2018-02-12 stsp ds.stb1.st_size = size1;
96 f9d67749 2017-11-30 stsp ds.stb1.st_mtime = 0; /* XXX */
97 8ba9a219 2017-11-29 stsp
98 f9d67749 2017-11-30 stsp ds.stb2.st_mode = S_IFREG;
99 98abbc84 2017-11-30 stsp if (blob2)
100 f934cf2c 2018-02-12 stsp ds.stb2.st_size = size2;
101 f9d67749 2017-11-30 stsp ds.stb2.st_mtime = 0; /* XXX */
102 f9d67749 2017-11-30 stsp
103 54156555 2018-12-24 stsp memset(&args, 0, sizeof(args));
104 8ba9a219 2017-11-29 stsp args.diff_format = D_UNIFIED;
105 54156555 2018-12-24 stsp args.label[0] = label1 ? label1 : idstr1;
106 54156555 2018-12-24 stsp args.label[1] = label2 ? label2 : idstr2;
107 df2871d2 2018-10-18 stsp args.diff_context = diff_context;
108 84eb021e 2018-03-27 stsp flags |= D_PROTOTYPE;
109 62136d3a 2017-11-29 stsp
110 09de383e 2018-12-24 stsp if (outfile) {
111 09de383e 2018-12-24 stsp fprintf(outfile, "blob - %s\n", idstr1);
112 09de383e 2018-12-24 stsp fprintf(outfile, "blob + %s\n", idstr2);
113 09de383e 2018-12-24 stsp }
114 404c43c4 2018-06-21 stsp err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
115 7d283eee 2017-11-29 stsp done:
116 fb43ecf1 2019-02-11 stsp if (f1 && fclose(f1) != 0 && err == NULL)
117 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
118 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
119 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
120 7d283eee 2017-11-29 stsp return err;
121 aaa13589 2019-06-01 stsp }
122 aaa13589 2019-06-01 stsp
123 aaa13589 2019-06-01 stsp const struct got_error *
124 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
125 aaa13589 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
126 aaa13589 2019-06-01 stsp struct got_object_id *id2, const char *label1, const char *label2,
127 aaa13589 2019-06-01 stsp struct got_repository *repo)
128 aaa13589 2019-06-01 stsp {
129 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
130 aaa13589 2019-06-01 stsp
131 aaa13589 2019-06-01 stsp return diff_blobs(blob1, blob2, label1, label2, a->diff_context,
132 aaa13589 2019-06-01 stsp a->outfile, NULL);
133 7d283eee 2017-11-29 stsp }
134 474b4f94 2017-11-30 stsp
135 404c43c4 2018-06-21 stsp const struct got_error *
136 404c43c4 2018-06-21 stsp got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
137 54156555 2018-12-24 stsp const char *label1, const char *label2, int diff_context, FILE *outfile)
138 404c43c4 2018-06-21 stsp {
139 54156555 2018-12-24 stsp return diff_blobs(blob1, blob2, label1, label2, diff_context, outfile,
140 54156555 2018-12-24 stsp NULL);
141 404c43c4 2018-06-21 stsp }
142 404c43c4 2018-06-21 stsp
143 7f1f93af 2019-08-06 stsp static const struct got_error *
144 7f1f93af 2019-08-06 stsp alloc_changes(struct got_diff_changes **changes)
145 7f1f93af 2019-08-06 stsp {
146 7f1f93af 2019-08-06 stsp *changes = calloc(1, sizeof(**changes));
147 7f1f93af 2019-08-06 stsp if (*changes == NULL)
148 7f1f93af 2019-08-06 stsp return got_error_from_errno("calloc");
149 7f1f93af 2019-08-06 stsp SIMPLEQ_INIT(&(*changes)->entries);
150 7f1f93af 2019-08-06 stsp return NULL;
151 7f1f93af 2019-08-06 stsp }
152 7f1f93af 2019-08-06 stsp
153 7f1f93af 2019-08-06 stsp static const struct got_error *
154 7f1f93af 2019-08-06 stsp diff_blob_file(struct got_diff_changes **changes,
155 7f1f93af 2019-08-06 stsp struct got_blob_object *blob1, FILE *f2, size_t size2,
156 b72f483a 2019-02-05 stsp const char *label2, int diff_context, FILE *outfile)
157 b72f483a 2019-02-05 stsp {
158 b72f483a 2019-02-05 stsp struct got_diff_state ds;
159 b72f483a 2019-02-05 stsp struct got_diff_args args;
160 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
161 b72f483a 2019-02-05 stsp FILE *f1 = NULL;
162 b72f483a 2019-02-05 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
163 b72f483a 2019-02-05 stsp char *idstr1 = NULL;
164 b72f483a 2019-02-05 stsp size_t size1;
165 b72f483a 2019-02-05 stsp int res, flags = 0;
166 b72f483a 2019-02-05 stsp
167 7f1f93af 2019-08-06 stsp if (changes)
168 7f1f93af 2019-08-06 stsp *changes = NULL;
169 7f1f93af 2019-08-06 stsp
170 b72f483a 2019-02-05 stsp size1 = 0;
171 b72f483a 2019-02-05 stsp if (blob1) {
172 b72f483a 2019-02-05 stsp f1 = got_opentemp();
173 b72f483a 2019-02-05 stsp if (f1 == NULL)
174 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentemp");
175 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
176 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
177 6c4c42e0 2019-06-24 stsp blob1);
178 b72f483a 2019-02-05 stsp if (err)
179 b72f483a 2019-02-05 stsp goto done;
180 b72f483a 2019-02-05 stsp } else {
181 b72f483a 2019-02-05 stsp flags |= D_EMPTY1;
182 b72f483a 2019-02-05 stsp idstr1 = "/dev/null";
183 b72f483a 2019-02-05 stsp }
184 b72f483a 2019-02-05 stsp
185 b72f483a 2019-02-05 stsp if (f2 == NULL)
186 b72f483a 2019-02-05 stsp flags |= D_EMPTY2;
187 b72f483a 2019-02-05 stsp
188 b72f483a 2019-02-05 stsp memset(&ds, 0, sizeof(ds));
189 b72f483a 2019-02-05 stsp /* XXX should stat buffers be passed in args instead of ds? */
190 b72f483a 2019-02-05 stsp ds.stb1.st_mode = S_IFREG;
191 b72f483a 2019-02-05 stsp if (blob1)
192 b72f483a 2019-02-05 stsp ds.stb1.st_size = size1;
193 b72f483a 2019-02-05 stsp ds.stb1.st_mtime = 0; /* XXX */
194 b72f483a 2019-02-05 stsp
195 b72f483a 2019-02-05 stsp ds.stb2.st_mode = S_IFREG;
196 b72f483a 2019-02-05 stsp ds.stb2.st_size = size2;
197 b72f483a 2019-02-05 stsp ds.stb2.st_mtime = 0; /* XXX */
198 b72f483a 2019-02-05 stsp
199 b72f483a 2019-02-05 stsp memset(&args, 0, sizeof(args));
200 b72f483a 2019-02-05 stsp args.diff_format = D_UNIFIED;
201 b72f483a 2019-02-05 stsp args.label[0] = label2;
202 b72f483a 2019-02-05 stsp args.label[1] = label2;
203 b72f483a 2019-02-05 stsp args.diff_context = diff_context;
204 b72f483a 2019-02-05 stsp flags |= D_PROTOTYPE;
205 b72f483a 2019-02-05 stsp
206 7f1f93af 2019-08-06 stsp if (outfile) {
207 7f1f93af 2019-08-06 stsp fprintf(outfile, "blob - %s\n", idstr1);
208 7f1f93af 2019-08-06 stsp fprintf(outfile, "file + %s\n",
209 7f1f93af 2019-08-06 stsp f2 == NULL ? "/dev/null" : label2);
210 7f1f93af 2019-08-06 stsp }
211 7f1f93af 2019-08-06 stsp if (changes) {
212 7f1f93af 2019-08-06 stsp err = alloc_changes(changes);
213 7f1f93af 2019-08-06 stsp if (err)
214 7f1f93af 2019-08-06 stsp return err;
215 7f1f93af 2019-08-06 stsp }
216 7f1f93af 2019-08-06 stsp err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile,
217 7f1f93af 2019-08-06 stsp changes ? *changes : NULL);
218 b72f483a 2019-02-05 stsp done:
219 fb43ecf1 2019-02-11 stsp if (f1 && fclose(f1) != 0 && err == NULL)
220 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
221 b72f483a 2019-02-05 stsp return err;
222 7f1f93af 2019-08-06 stsp }
223 7f1f93af 2019-08-06 stsp
224 7f1f93af 2019-08-06 stsp const struct got_error *
225 7f1f93af 2019-08-06 stsp got_diff_blob_file(struct got_blob_object *blob1, FILE *f2, size_t size2,
226 7f1f93af 2019-08-06 stsp const char *label2, int diff_context, FILE *outfile)
227 7f1f93af 2019-08-06 stsp {
228 7f1f93af 2019-08-06 stsp return diff_blob_file(NULL, blob1, f2, size2, label2, diff_context,
229 7f1f93af 2019-08-06 stsp outfile);
230 7f1f93af 2019-08-06 stsp }
231 7f1f93af 2019-08-06 stsp
232 7f1f93af 2019-08-06 stsp const struct got_error *
233 7f1f93af 2019-08-06 stsp got_diff_blob_file_lines_changed(struct got_diff_changes **changes,
234 7f1f93af 2019-08-06 stsp struct got_blob_object *blob1, FILE *f2, size_t size2,
235 7f1f93af 2019-08-06 stsp const char *label2, int diff_context)
236 7f1f93af 2019-08-06 stsp {
237 7f1f93af 2019-08-06 stsp return diff_blob_file(changes, blob1, f2, size2, label2, diff_context,
238 7f1f93af 2019-08-06 stsp NULL);
239 b72f483a 2019-02-05 stsp }
240 b72f483a 2019-02-05 stsp
241 b72f483a 2019-02-05 stsp const struct got_error *
242 404c43c4 2018-06-21 stsp got_diff_blob_lines_changed(struct got_diff_changes **changes,
243 404c43c4 2018-06-21 stsp struct got_blob_object *blob1, struct got_blob_object *blob2)
244 404c43c4 2018-06-21 stsp {
245 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
246 404c43c4 2018-06-21 stsp
247 7f1f93af 2019-08-06 stsp err = alloc_changes(changes);
248 7f1f93af 2019-08-06 stsp if (err)
249 7f1f93af 2019-08-06 stsp return err;
250 404c43c4 2018-06-21 stsp
251 54156555 2018-12-24 stsp err = diff_blobs(blob1, blob2, NULL, NULL, 3, NULL, *changes);
252 404c43c4 2018-06-21 stsp if (err) {
253 404c43c4 2018-06-21 stsp got_diff_free_changes(*changes);
254 404c43c4 2018-06-21 stsp *changes = NULL;
255 404c43c4 2018-06-21 stsp }
256 404c43c4 2018-06-21 stsp return err;
257 404c43c4 2018-06-21 stsp }
258 404c43c4 2018-06-21 stsp
259 404c43c4 2018-06-21 stsp void
260 404c43c4 2018-06-21 stsp got_diff_free_changes(struct got_diff_changes *changes)
261 404c43c4 2018-06-21 stsp {
262 404c43c4 2018-06-21 stsp struct got_diff_change *change;
263 404c43c4 2018-06-21 stsp while (!SIMPLEQ_EMPTY(&changes->entries)) {
264 404c43c4 2018-06-21 stsp change = SIMPLEQ_FIRST(&changes->entries);
265 404c43c4 2018-06-21 stsp SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
266 404c43c4 2018-06-21 stsp free(change);
267 404c43c4 2018-06-21 stsp }
268 404c43c4 2018-06-21 stsp free(changes);
269 474b4f94 2017-11-30 stsp }
270 474b4f94 2017-11-30 stsp
271 474b4f94 2017-11-30 stsp static const struct got_error *
272 54156555 2018-12-24 stsp diff_added_blob(struct got_object_id *id, const char *label,
273 aaa13589 2019-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
274 474b4f94 2017-11-30 stsp {
275 4e22badc 2017-11-30 stsp const struct got_error *err;
276 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
277 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
278 4e22badc 2017-11-30 stsp
279 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
280 4e22badc 2017-11-30 stsp if (err)
281 4e22badc 2017-11-30 stsp return err;
282 4e22badc 2017-11-30 stsp
283 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
284 2acfca77 2018-04-01 stsp if (err)
285 2acfca77 2018-04-01 stsp goto done;
286 aaa13589 2019-06-01 stsp err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, repo);
287 2acfca77 2018-04-01 stsp done:
288 2acfca77 2018-04-01 stsp got_object_close(obj);
289 2acfca77 2018-04-01 stsp if (blob)
290 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
291 2acfca77 2018-04-01 stsp return err;
292 474b4f94 2017-11-30 stsp }
293 474b4f94 2017-11-30 stsp
294 474b4f94 2017-11-30 stsp static const struct got_error *
295 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
296 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
297 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
298 474b4f94 2017-11-30 stsp {
299 6a213ccb 2017-11-30 stsp const struct got_error *err;
300 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
301 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
302 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
303 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
304 6a213ccb 2017-11-30 stsp
305 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
306 6a213ccb 2017-11-30 stsp if (err)
307 730a8aa0 2018-04-24 stsp return err;
308 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
309 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
310 6a213ccb 2017-11-30 stsp goto done;
311 6a213ccb 2017-11-30 stsp }
312 6a213ccb 2017-11-30 stsp
313 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
314 730a8aa0 2018-04-24 stsp if (err)
315 6a213ccb 2017-11-30 stsp goto done;
316 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
317 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
318 6a213ccb 2017-11-30 stsp goto done;
319 6a213ccb 2017-11-30 stsp }
320 6a213ccb 2017-11-30 stsp
321 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
322 2acfca77 2018-04-01 stsp if (err)
323 6a213ccb 2017-11-30 stsp goto done;
324 6a213ccb 2017-11-30 stsp
325 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
326 2acfca77 2018-04-01 stsp if (err)
327 6a213ccb 2017-11-30 stsp goto done;
328 6a213ccb 2017-11-30 stsp
329 aaa13589 2019-06-01 stsp err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, repo);
330 6a213ccb 2017-11-30 stsp done:
331 a3e2cbea 2017-12-01 stsp if (obj1)
332 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
333 a3e2cbea 2017-12-01 stsp if (obj2)
334 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
335 a3e2cbea 2017-12-01 stsp if (blob1)
336 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
337 a3e2cbea 2017-12-01 stsp if (blob2)
338 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
339 6a213ccb 2017-11-30 stsp return err;
340 474b4f94 2017-11-30 stsp }
341 474b4f94 2017-11-30 stsp
342 474b4f94 2017-11-30 stsp static const struct got_error *
343 f6861a81 2018-09-13 stsp diff_deleted_blob(struct got_object_id *id, const char *label,
344 aaa13589 2019-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
345 474b4f94 2017-11-30 stsp {
346 365fb436 2017-11-30 stsp const struct got_error *err;
347 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
348 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
349 365fb436 2017-11-30 stsp
350 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
351 365fb436 2017-11-30 stsp if (err)
352 365fb436 2017-11-30 stsp return err;
353 365fb436 2017-11-30 stsp
354 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
355 2acfca77 2018-04-01 stsp if (err)
356 2acfca77 2018-04-01 stsp goto done;
357 aaa13589 2019-06-01 stsp err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, repo);
358 2acfca77 2018-04-01 stsp done:
359 2acfca77 2018-04-01 stsp got_object_close(obj);
360 2acfca77 2018-04-01 stsp if (blob)
361 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
362 2acfca77 2018-04-01 stsp return err;
363 474b4f94 2017-11-30 stsp }
364 474b4f94 2017-11-30 stsp
365 474b4f94 2017-11-30 stsp static const struct got_error *
366 54156555 2018-12-24 stsp diff_added_tree(struct got_object_id *id, const char *label,
367 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
368 31b4484f 2019-07-27 stsp int diff_content)
369 474b4f94 2017-11-30 stsp {
370 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
371 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
372 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
373 9c70d4c3 2017-11-30 stsp
374 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
375 9c70d4c3 2017-11-30 stsp if (err)
376 9c70d4c3 2017-11-30 stsp goto done;
377 9c70d4c3 2017-11-30 stsp
378 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
379 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
380 9c70d4c3 2017-11-30 stsp goto done;
381 9c70d4c3 2017-11-30 stsp }
382 9c70d4c3 2017-11-30 stsp
383 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
384 9c70d4c3 2017-11-30 stsp if (err)
385 9c70d4c3 2017-11-30 stsp goto done;
386 9c70d4c3 2017-11-30 stsp
387 31b4484f 2019-07-27 stsp err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
388 31b4484f 2019-07-27 stsp diff_content);
389 9c70d4c3 2017-11-30 stsp done:
390 9c70d4c3 2017-11-30 stsp if (tree)
391 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
392 9c70d4c3 2017-11-30 stsp if (treeobj)
393 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
394 9c70d4c3 2017-11-30 stsp return err;
395 474b4f94 2017-11-30 stsp }
396 474b4f94 2017-11-30 stsp
397 474b4f94 2017-11-30 stsp static const struct got_error *
398 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
399 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
400 31b4484f 2019-07-27 stsp got_diff_blob_cb cb, void *cb_arg, int diff_content)
401 474b4f94 2017-11-30 stsp {
402 f6861a81 2018-09-13 stsp const struct got_error *err;
403 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
404 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
405 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
406 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
407 789689b5 2017-11-30 stsp
408 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
409 789689b5 2017-11-30 stsp if (err)
410 789689b5 2017-11-30 stsp goto done;
411 789689b5 2017-11-30 stsp
412 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
413 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
414 789689b5 2017-11-30 stsp goto done;
415 789689b5 2017-11-30 stsp }
416 789689b5 2017-11-30 stsp
417 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
418 789689b5 2017-11-30 stsp if (err)
419 789689b5 2017-11-30 stsp goto done;
420 789689b5 2017-11-30 stsp
421 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
422 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
423 789689b5 2017-11-30 stsp goto done;
424 789689b5 2017-11-30 stsp }
425 789689b5 2017-11-30 stsp
426 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
427 789689b5 2017-11-30 stsp if (err)
428 789689b5 2017-11-30 stsp goto done;
429 789689b5 2017-11-30 stsp
430 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
431 789689b5 2017-11-30 stsp if (err)
432 789689b5 2017-11-30 stsp goto done;
433 789689b5 2017-11-30 stsp
434 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
435 31b4484f 2019-07-27 stsp diff_content);
436 789689b5 2017-11-30 stsp
437 789689b5 2017-11-30 stsp done:
438 789689b5 2017-11-30 stsp if (tree1)
439 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
440 789689b5 2017-11-30 stsp if (tree2)
441 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
442 789689b5 2017-11-30 stsp if (treeobj1)
443 789689b5 2017-11-30 stsp got_object_close(treeobj1);
444 789689b5 2017-11-30 stsp if (treeobj2)
445 789689b5 2017-11-30 stsp got_object_close(treeobj2);
446 789689b5 2017-11-30 stsp return err;
447 474b4f94 2017-11-30 stsp }
448 474b4f94 2017-11-30 stsp
449 474b4f94 2017-11-30 stsp static const struct got_error *
450 54156555 2018-12-24 stsp diff_deleted_tree(struct got_object_id *id, const char *label,
451 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
452 31b4484f 2019-07-27 stsp int diff_content)
453 474b4f94 2017-11-30 stsp {
454 f6861a81 2018-09-13 stsp const struct got_error *err;
455 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
456 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
457 2c56f2ce 2017-11-30 stsp
458 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
459 2c56f2ce 2017-11-30 stsp if (err)
460 2c56f2ce 2017-11-30 stsp goto done;
461 2c56f2ce 2017-11-30 stsp
462 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
463 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
464 2c56f2ce 2017-11-30 stsp goto done;
465 2c56f2ce 2017-11-30 stsp }
466 2c56f2ce 2017-11-30 stsp
467 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
468 2c56f2ce 2017-11-30 stsp if (err)
469 2c56f2ce 2017-11-30 stsp goto done;
470 2c56f2ce 2017-11-30 stsp
471 31b4484f 2019-07-27 stsp err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
472 31b4484f 2019-07-27 stsp diff_content);
473 2c56f2ce 2017-11-30 stsp done:
474 2c56f2ce 2017-11-30 stsp if (tree)
475 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
476 2c56f2ce 2017-11-30 stsp if (treeobj)
477 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
478 2c56f2ce 2017-11-30 stsp return err;
479 474b4f94 2017-11-30 stsp }
480 474b4f94 2017-11-30 stsp
481 474b4f94 2017-11-30 stsp static const struct got_error *
482 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
483 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
484 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
485 474b4f94 2017-11-30 stsp {
486 013404a9 2017-11-30 stsp /* XXX TODO */
487 474b4f94 2017-11-30 stsp return NULL;
488 474b4f94 2017-11-30 stsp }
489 474b4f94 2017-11-30 stsp
490 474b4f94 2017-11-30 stsp static const struct got_error *
491 1de5e065 2019-06-01 stsp diff_entry_old_new(const struct got_tree_entry *te1,
492 1de5e065 2019-06-01 stsp const struct got_tree_entry *te2, const char *label1, const char *label2,
493 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
494 31b4484f 2019-07-27 stsp int diff_content)
495 474b4f94 2017-11-30 stsp {
496 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
497 19ae6da1 2018-11-05 stsp int id_match;
498 474b4f94 2017-11-30 stsp
499 474b4f94 2017-11-30 stsp if (te2 == NULL) {
500 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
501 aaa13589 2019-06-01 stsp err = diff_deleted_tree(te1->id, label1, repo,
502 31b4484f 2019-07-27 stsp cb, cb_arg, diff_content);
503 31b4484f 2019-07-27 stsp else {
504 31b4484f 2019-07-27 stsp if (diff_content)
505 31b4484f 2019-07-27 stsp err = diff_deleted_blob(te1->id, label1, repo,
506 31b4484f 2019-07-27 stsp cb, cb_arg);
507 31b4484f 2019-07-27 stsp else
508 31b4484f 2019-07-27 stsp err = cb(cb_arg, NULL, NULL, te1->id, NULL,
509 31b4484f 2019-07-27 stsp label1, NULL, repo);
510 31b4484f 2019-07-27 stsp }
511 f6861a81 2018-09-13 stsp return err;
512 474b4f94 2017-11-30 stsp }
513 474b4f94 2017-11-30 stsp
514 19ae6da1 2018-11-05 stsp id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
515 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
516 19ae6da1 2018-11-05 stsp if (!id_match)
517 f6861a81 2018-09-13 stsp return diff_modified_tree(te1->id, te2->id,
518 31b4484f 2019-07-27 stsp label1, label2, repo, cb, cb_arg, diff_content);
519 4209f790 2017-11-30 stsp } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
520 31b4484f 2019-07-27 stsp if (!id_match) {
521 31b4484f 2019-07-27 stsp if (diff_content)
522 31b4484f 2019-07-27 stsp return diff_modified_blob(te1->id, te2->id,
523 31b4484f 2019-07-27 stsp label1, label2, repo, cb, cb_arg);
524 31b4484f 2019-07-27 stsp else
525 31b4484f 2019-07-27 stsp return cb(cb_arg, NULL, NULL, te1->id,
526 31b4484f 2019-07-27 stsp te2->id, label1, label2, repo);
527 31b4484f 2019-07-27 stsp }
528 413ea19d 2017-11-30 stsp }
529 474b4f94 2017-11-30 stsp
530 19ae6da1 2018-11-05 stsp if (id_match)
531 f6861a81 2018-09-13 stsp return NULL;
532 f6861a81 2018-09-13 stsp
533 aaa13589 2019-06-01 stsp return diff_kind_mismatch(te1->id, te2->id, label1, label2, repo,
534 aaa13589 2019-06-01 stsp cb, cb_arg);
535 474b4f94 2017-11-30 stsp }
536 474b4f94 2017-11-30 stsp
537 474b4f94 2017-11-30 stsp static const struct got_error *
538 1de5e065 2019-06-01 stsp diff_entry_new_old(const struct got_tree_entry *te2,
539 aaa13589 2019-06-01 stsp const struct got_tree_entry *te1, const char *label2,
540 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
541 31b4484f 2019-07-27 stsp int diff_content)
542 474b4f94 2017-11-30 stsp {
543 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
544 f6861a81 2018-09-13 stsp return NULL;
545 474b4f94 2017-11-30 stsp
546 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
547 31b4484f 2019-07-27 stsp return diff_added_tree(te2->id, label2, repo, cb, cb_arg,
548 31b4484f 2019-07-27 stsp diff_content);
549 f6861a81 2018-09-13 stsp
550 31b4484f 2019-07-27 stsp if (diff_content)
551 31b4484f 2019-07-27 stsp return diff_added_blob(te2->id, label2, repo, cb, cb_arg);
552 31b4484f 2019-07-27 stsp
553 31b4484f 2019-07-27 stsp return cb(cb_arg, NULL, NULL, NULL, te2->id, NULL, label2, repo);
554 474b4f94 2017-11-30 stsp }
555 474b4f94 2017-11-30 stsp
556 474b4f94 2017-11-30 stsp const struct got_error *
557 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
558 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
559 31b4484f 2019-07-27 stsp got_diff_blob_cb cb, void *cb_arg, int diff_content)
560 474b4f94 2017-11-30 stsp {
561 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
562 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
563 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
564 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
565 474b4f94 2017-11-30 stsp
566 883f0469 2018-06-23 stsp if (tree1) {
567 883f0469 2018-06-23 stsp const struct got_tree_entries *entries;
568 883f0469 2018-06-23 stsp entries = got_object_tree_get_entries(tree1);
569 883f0469 2018-06-23 stsp te1 = SIMPLEQ_FIRST(&entries->head);
570 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
571 f6861a81 2018-09-13 stsp te1->name) == -1)
572 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
573 883f0469 2018-06-23 stsp }
574 883f0469 2018-06-23 stsp if (tree2) {
575 883f0469 2018-06-23 stsp const struct got_tree_entries *entries;
576 883f0469 2018-06-23 stsp entries = got_object_tree_get_entries(tree2);
577 883f0469 2018-06-23 stsp te2 = SIMPLEQ_FIRST(&entries->head);
578 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
579 f6861a81 2018-09-13 stsp te2->name) == -1)
580 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
581 883f0469 2018-06-23 stsp }
582 474b4f94 2017-11-30 stsp
583 474b4f94 2017-11-30 stsp do {
584 474b4f94 2017-11-30 stsp if (te1) {
585 1de5e065 2019-06-01 stsp const struct got_tree_entry *te = NULL;
586 f6861a81 2018-09-13 stsp if (tree2)
587 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
588 1de5e065 2019-06-01 stsp te1->name);
589 f6861a81 2018-09-13 stsp if (te) {
590 f6861a81 2018-09-13 stsp free(l2);
591 f6861a81 2018-09-13 stsp l2 = NULL;
592 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
593 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
594 230a42bd 2019-05-11 jcs return
595 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
596 f6861a81 2018-09-13 stsp }
597 aaa13589 2019-06-01 stsp err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
598 31b4484f 2019-07-27 stsp cb_arg, diff_content);
599 474b4f94 2017-11-30 stsp if (err)
600 474b4f94 2017-11-30 stsp break;
601 474b4f94 2017-11-30 stsp }
602 474b4f94 2017-11-30 stsp
603 474b4f94 2017-11-30 stsp if (te2) {
604 1de5e065 2019-06-01 stsp const struct got_tree_entry *te = NULL;
605 f6861a81 2018-09-13 stsp if (tree1)
606 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
607 1de5e065 2019-06-01 stsp te2->name);
608 d6ce02f1 2018-11-17 stsp free(l2);
609 d6ce02f1 2018-11-17 stsp if (te) {
610 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
611 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
612 230a42bd 2019-05-11 jcs return
613 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
614 d6ce02f1 2018-11-17 stsp } else {
615 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
616 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->name) == -1)
617 230a42bd 2019-05-11 jcs return
618 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
619 d6ce02f1 2018-11-17 stsp }
620 31b4484f 2019-07-27 stsp err = diff_entry_new_old(te2, te, l2, repo,
621 31b4484f 2019-07-27 stsp cb, cb_arg, diff_content);
622 474b4f94 2017-11-30 stsp if (err)
623 474b4f94 2017-11-30 stsp break;
624 474b4f94 2017-11-30 stsp }
625 474b4f94 2017-11-30 stsp
626 f6861a81 2018-09-13 stsp free(l1);
627 f6861a81 2018-09-13 stsp l1 = NULL;
628 f6861a81 2018-09-13 stsp if (te1) {
629 474b4f94 2017-11-30 stsp te1 = SIMPLEQ_NEXT(te1, entry);
630 f6861a81 2018-09-13 stsp if (te1 &&
631 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
632 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
633 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
634 f6861a81 2018-09-13 stsp }
635 f6861a81 2018-09-13 stsp free(l2);
636 f6861a81 2018-09-13 stsp l2 = NULL;
637 f6861a81 2018-09-13 stsp if (te2) {
638 474b4f94 2017-11-30 stsp te2 = SIMPLEQ_NEXT(te2, entry);
639 f6861a81 2018-09-13 stsp if (te2 &&
640 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
641 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
642 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
643 f6861a81 2018-09-13 stsp }
644 474b4f94 2017-11-30 stsp } while (te1 || te2);
645 11528a82 2018-05-19 stsp
646 11528a82 2018-05-19 stsp return err;
647 11528a82 2018-05-19 stsp }
648 11528a82 2018-05-19 stsp
649 11528a82 2018-05-19 stsp const struct got_error *
650 15a94983 2018-12-23 stsp got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
651 54156555 2018-12-24 stsp const char *label1, const char *label2, int diff_context,
652 54156555 2018-12-24 stsp struct got_repository *repo, FILE *outfile)
653 11528a82 2018-05-19 stsp {
654 11528a82 2018-05-19 stsp const struct got_error *err;
655 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
656 b74c7625 2018-05-20 stsp
657 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
658 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
659 11528a82 2018-05-19 stsp
660 15a94983 2018-12-23 stsp if (id1) {
661 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob1, repo, id1, 8192);
662 cd0acaa7 2018-05-20 stsp if (err)
663 cd0acaa7 2018-05-20 stsp goto done;
664 cd0acaa7 2018-05-20 stsp }
665 15a94983 2018-12-23 stsp if (id2) {
666 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob2, repo, id2, 8192);
667 cd0acaa7 2018-05-20 stsp if (err)
668 cd0acaa7 2018-05-20 stsp goto done;
669 cd0acaa7 2018-05-20 stsp }
670 54156555 2018-12-24 stsp err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
671 54156555 2018-12-24 stsp outfile);
672 11528a82 2018-05-19 stsp done:
673 11528a82 2018-05-19 stsp if (blob1)
674 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
675 11528a82 2018-05-19 stsp if (blob2)
676 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
677 474b4f94 2017-11-30 stsp return err;
678 474b4f94 2017-11-30 stsp }
679 11528a82 2018-05-19 stsp
680 11528a82 2018-05-19 stsp const struct got_error *
681 15a94983 2018-12-23 stsp got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
682 54156555 2018-12-24 stsp char *label1, char *label2, int diff_context, struct got_repository *repo,
683 54156555 2018-12-24 stsp FILE *outfile)
684 11528a82 2018-05-19 stsp {
685 11528a82 2018-05-19 stsp const struct got_error *err;
686 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
687 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
688 11528a82 2018-05-19 stsp
689 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
690 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
691 b74c7625 2018-05-20 stsp
692 15a94983 2018-12-23 stsp if (id1) {
693 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
694 cd0acaa7 2018-05-20 stsp if (err)
695 cd0acaa7 2018-05-20 stsp goto done;
696 cd0acaa7 2018-05-20 stsp }
697 15a94983 2018-12-23 stsp if (id2) {
698 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
699 cd0acaa7 2018-05-20 stsp if (err)
700 cd0acaa7 2018-05-20 stsp goto done;
701 cd0acaa7 2018-05-20 stsp }
702 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
703 aaa13589 2019-06-01 stsp arg.outfile = outfile;
704 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, label1, label2, repo,
705 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
706 11528a82 2018-05-19 stsp done:
707 11528a82 2018-05-19 stsp if (tree1)
708 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
709 11528a82 2018-05-19 stsp if (tree2)
710 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
711 11528a82 2018-05-19 stsp return err;
712 11528a82 2018-05-19 stsp }
713 11528a82 2018-05-19 stsp
714 11528a82 2018-05-19 stsp const struct got_error *
715 15a94983 2018-12-23 stsp got_diff_objects_as_commits(struct got_object_id *id1,
716 15a94983 2018-12-23 stsp struct got_object_id *id2, int diff_context,
717 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
718 11528a82 2018-05-19 stsp {
719 11528a82 2018-05-19 stsp const struct got_error *err;
720 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
721 11528a82 2018-05-19 stsp
722 15a94983 2018-12-23 stsp if (id2 == NULL)
723 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
724 b74c7625 2018-05-20 stsp
725 15a94983 2018-12-23 stsp if (id1) {
726 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
727 cd0acaa7 2018-05-20 stsp if (err)
728 cd0acaa7 2018-05-20 stsp goto done;
729 cd0acaa7 2018-05-20 stsp }
730 bacc9935 2018-05-20 stsp
731 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
732 9b697879 2018-05-20 stsp if (err)
733 9b697879 2018-05-20 stsp goto done;
734 9b697879 2018-05-20 stsp
735 15a94983 2018-12-23 stsp err = got_diff_objects_as_trees(
736 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
737 54156555 2018-12-24 stsp got_object_commit_get_tree_id(commit2), "", "", diff_context, repo,
738 54156555 2018-12-24 stsp outfile);
739 11528a82 2018-05-19 stsp done:
740 11528a82 2018-05-19 stsp if (commit1)
741 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
742 11528a82 2018-05-19 stsp if (commit2)
743 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
744 11528a82 2018-05-19 stsp return err;
745 11528a82 2018-05-19 stsp }