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