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 4e22badc 2017-11-30 stsp return got_error(GOT_ERR_FILE_OPEN);
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 4e22badc 2017-11-30 stsp return got_error(GOT_ERR_FILE_OPEN);
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 404c43c4 2018-06-21 stsp got_diff_blob_lines_changed(struct got_diff_changes **changes,
130 404c43c4 2018-06-21 stsp struct got_blob_object *blob1, struct got_blob_object *blob2)
131 404c43c4 2018-06-21 stsp {
132 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
133 404c43c4 2018-06-21 stsp
134 404c43c4 2018-06-21 stsp *changes = calloc(1, sizeof(**changes));
135 404c43c4 2018-06-21 stsp if (*changes == NULL)
136 404c43c4 2018-06-21 stsp return got_error_from_errno();
137 404c43c4 2018-06-21 stsp SIMPLEQ_INIT(&(*changes)->entries);
138 404c43c4 2018-06-21 stsp
139 54156555 2018-12-24 stsp err = diff_blobs(blob1, blob2, NULL, NULL, 3, NULL, *changes);
140 404c43c4 2018-06-21 stsp if (err) {
141 404c43c4 2018-06-21 stsp got_diff_free_changes(*changes);
142 404c43c4 2018-06-21 stsp *changes = NULL;
143 404c43c4 2018-06-21 stsp }
144 404c43c4 2018-06-21 stsp return err;
145 404c43c4 2018-06-21 stsp }
146 404c43c4 2018-06-21 stsp
147 404c43c4 2018-06-21 stsp void
148 404c43c4 2018-06-21 stsp got_diff_free_changes(struct got_diff_changes *changes)
149 404c43c4 2018-06-21 stsp {
150 404c43c4 2018-06-21 stsp struct got_diff_change *change;
151 404c43c4 2018-06-21 stsp while (!SIMPLEQ_EMPTY(&changes->entries)) {
152 404c43c4 2018-06-21 stsp change = SIMPLEQ_FIRST(&changes->entries);
153 404c43c4 2018-06-21 stsp SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
154 404c43c4 2018-06-21 stsp free(change);
155 404c43c4 2018-06-21 stsp }
156 404c43c4 2018-06-21 stsp free(changes);
157 404c43c4 2018-06-21 stsp }
158 404c43c4 2018-06-21 stsp
159 a3e2cbea 2017-12-01 stsp struct got_tree_entry *
160 a3e2cbea 2017-12-01 stsp match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
161 474b4f94 2017-11-30 stsp {
162 a3e2cbea 2017-12-01 stsp struct got_tree_entry *te2;
163 883f0469 2018-06-23 stsp const struct got_tree_entries *entries2;
164 a3e2cbea 2017-12-01 stsp
165 883f0469 2018-06-23 stsp entries2 = got_object_tree_get_entries(tree2);
166 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te2, &entries2->head, entry) {
167 e20293f2 2018-11-17 stsp if (strcmp(te1->name, te2->name) == 0)
168 a3e2cbea 2017-12-01 stsp return te2;
169 a3e2cbea 2017-12-01 stsp }
170 474b4f94 2017-11-30 stsp return NULL;
171 474b4f94 2017-11-30 stsp }
172 474b4f94 2017-11-30 stsp
173 474b4f94 2017-11-30 stsp static const struct got_error *
174 54156555 2018-12-24 stsp diff_added_blob(struct got_object_id *id, const char *label,
175 df2871d2 2018-10-18 stsp int diff_context, struct got_repository *repo, FILE *outfile)
176 474b4f94 2017-11-30 stsp {
177 4e22badc 2017-11-30 stsp const struct got_error *err;
178 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
179 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
180 4e22badc 2017-11-30 stsp
181 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
182 4e22badc 2017-11-30 stsp if (err)
183 4e22badc 2017-11-30 stsp return err;
184 4e22badc 2017-11-30 stsp
185 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
186 2acfca77 2018-04-01 stsp if (err)
187 2acfca77 2018-04-01 stsp goto done;
188 54156555 2018-12-24 stsp err = got_diff_blob(NULL, blob, NULL, label, diff_context, outfile);
189 2acfca77 2018-04-01 stsp done:
190 2acfca77 2018-04-01 stsp got_object_close(obj);
191 2acfca77 2018-04-01 stsp if (blob)
192 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
193 2acfca77 2018-04-01 stsp return err;
194 474b4f94 2017-11-30 stsp }
195 474b4f94 2017-11-30 stsp
196 474b4f94 2017-11-30 stsp static const struct got_error *
197 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
198 54156555 2018-12-24 stsp const char *label1, const char *label2, int diff_context,
199 54156555 2018-12-24 stsp struct got_repository *repo, FILE *outfile)
200 474b4f94 2017-11-30 stsp {
201 6a213ccb 2017-11-30 stsp const struct got_error *err;
202 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
203 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
204 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
205 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
206 6a213ccb 2017-11-30 stsp
207 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
208 6a213ccb 2017-11-30 stsp if (err)
209 730a8aa0 2018-04-24 stsp return err;
210 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
211 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
212 6a213ccb 2017-11-30 stsp goto done;
213 6a213ccb 2017-11-30 stsp }
214 6a213ccb 2017-11-30 stsp
215 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
216 730a8aa0 2018-04-24 stsp if (err)
217 6a213ccb 2017-11-30 stsp goto done;
218 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
219 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
220 6a213ccb 2017-11-30 stsp goto done;
221 6a213ccb 2017-11-30 stsp }
222 6a213ccb 2017-11-30 stsp
223 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
224 2acfca77 2018-04-01 stsp if (err)
225 6a213ccb 2017-11-30 stsp goto done;
226 6a213ccb 2017-11-30 stsp
227 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
228 2acfca77 2018-04-01 stsp if (err)
229 6a213ccb 2017-11-30 stsp goto done;
230 6a213ccb 2017-11-30 stsp
231 54156555 2018-12-24 stsp err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
232 54156555 2018-12-24 stsp outfile);
233 6a213ccb 2017-11-30 stsp
234 6a213ccb 2017-11-30 stsp done:
235 a3e2cbea 2017-12-01 stsp if (obj1)
236 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
237 a3e2cbea 2017-12-01 stsp if (obj2)
238 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
239 a3e2cbea 2017-12-01 stsp if (blob1)
240 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
241 a3e2cbea 2017-12-01 stsp if (blob2)
242 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
243 6a213ccb 2017-11-30 stsp return err;
244 474b4f94 2017-11-30 stsp }
245 474b4f94 2017-11-30 stsp
246 474b4f94 2017-11-30 stsp static const struct got_error *
247 f6861a81 2018-09-13 stsp diff_deleted_blob(struct got_object_id *id, const char *label,
248 54156555 2018-12-24 stsp int diff_context, struct got_repository *repo, FILE *outfile)
249 474b4f94 2017-11-30 stsp {
250 365fb436 2017-11-30 stsp const struct got_error *err;
251 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
252 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
253 365fb436 2017-11-30 stsp
254 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
255 365fb436 2017-11-30 stsp if (err)
256 365fb436 2017-11-30 stsp return err;
257 365fb436 2017-11-30 stsp
258 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
259 2acfca77 2018-04-01 stsp if (err)
260 2acfca77 2018-04-01 stsp goto done;
261 54156555 2018-12-24 stsp err = got_diff_blob(blob, NULL, label, NULL, diff_context, outfile);
262 2acfca77 2018-04-01 stsp done:
263 2acfca77 2018-04-01 stsp got_object_close(obj);
264 2acfca77 2018-04-01 stsp if (blob)
265 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
266 2acfca77 2018-04-01 stsp return err;
267 474b4f94 2017-11-30 stsp }
268 474b4f94 2017-11-30 stsp
269 474b4f94 2017-11-30 stsp static const struct got_error *
270 54156555 2018-12-24 stsp diff_added_tree(struct got_object_id *id, const char *label,
271 df2871d2 2018-10-18 stsp int diff_context, struct got_repository *repo, FILE *outfile)
272 474b4f94 2017-11-30 stsp {
273 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
274 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
275 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
276 9c70d4c3 2017-11-30 stsp
277 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
278 9c70d4c3 2017-11-30 stsp if (err)
279 9c70d4c3 2017-11-30 stsp goto done;
280 9c70d4c3 2017-11-30 stsp
281 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
282 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
283 9c70d4c3 2017-11-30 stsp goto done;
284 9c70d4c3 2017-11-30 stsp }
285 9c70d4c3 2017-11-30 stsp
286 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
287 9c70d4c3 2017-11-30 stsp if (err)
288 9c70d4c3 2017-11-30 stsp goto done;
289 9c70d4c3 2017-11-30 stsp
290 54156555 2018-12-24 stsp err = got_diff_tree(NULL, tree, NULL, label, diff_context, repo,
291 54156555 2018-12-24 stsp outfile);
292 9c70d4c3 2017-11-30 stsp
293 9c70d4c3 2017-11-30 stsp done:
294 9c70d4c3 2017-11-30 stsp if (tree)
295 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
296 9c70d4c3 2017-11-30 stsp if (treeobj)
297 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
298 9c70d4c3 2017-11-30 stsp return err;
299 474b4f94 2017-11-30 stsp }
300 474b4f94 2017-11-30 stsp
301 474b4f94 2017-11-30 stsp static const struct got_error *
302 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
303 54156555 2018-12-24 stsp const char *label1, const char *label2, int diff_context,
304 54156555 2018-12-24 stsp struct got_repository *repo, FILE *outfile)
305 474b4f94 2017-11-30 stsp {
306 f6861a81 2018-09-13 stsp const struct got_error *err;
307 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
308 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
309 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
310 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
311 789689b5 2017-11-30 stsp
312 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
313 789689b5 2017-11-30 stsp if (err)
314 789689b5 2017-11-30 stsp goto done;
315 789689b5 2017-11-30 stsp
316 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
317 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
318 789689b5 2017-11-30 stsp goto done;
319 789689b5 2017-11-30 stsp }
320 789689b5 2017-11-30 stsp
321 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
322 789689b5 2017-11-30 stsp if (err)
323 789689b5 2017-11-30 stsp goto done;
324 789689b5 2017-11-30 stsp
325 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
326 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
327 789689b5 2017-11-30 stsp goto done;
328 789689b5 2017-11-30 stsp }
329 789689b5 2017-11-30 stsp
330 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
331 789689b5 2017-11-30 stsp if (err)
332 789689b5 2017-11-30 stsp goto done;
333 789689b5 2017-11-30 stsp
334 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
335 789689b5 2017-11-30 stsp if (err)
336 789689b5 2017-11-30 stsp goto done;
337 789689b5 2017-11-30 stsp
338 54156555 2018-12-24 stsp err = got_diff_tree(tree1, tree2, label1, label2, diff_context, repo,
339 54156555 2018-12-24 stsp outfile);
340 789689b5 2017-11-30 stsp
341 789689b5 2017-11-30 stsp done:
342 789689b5 2017-11-30 stsp if (tree1)
343 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
344 789689b5 2017-11-30 stsp if (tree2)
345 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
346 789689b5 2017-11-30 stsp if (treeobj1)
347 789689b5 2017-11-30 stsp got_object_close(treeobj1);
348 789689b5 2017-11-30 stsp if (treeobj2)
349 789689b5 2017-11-30 stsp got_object_close(treeobj2);
350 789689b5 2017-11-30 stsp return err;
351 474b4f94 2017-11-30 stsp }
352 474b4f94 2017-11-30 stsp
353 474b4f94 2017-11-30 stsp static const struct got_error *
354 54156555 2018-12-24 stsp diff_deleted_tree(struct got_object_id *id, const char *label,
355 df2871d2 2018-10-18 stsp int diff_context, struct got_repository *repo, FILE *outfile)
356 474b4f94 2017-11-30 stsp {
357 f6861a81 2018-09-13 stsp const struct got_error *err;
358 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
359 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
360 2c56f2ce 2017-11-30 stsp
361 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
362 2c56f2ce 2017-11-30 stsp if (err)
363 2c56f2ce 2017-11-30 stsp goto done;
364 2c56f2ce 2017-11-30 stsp
365 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
366 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
367 2c56f2ce 2017-11-30 stsp goto done;
368 2c56f2ce 2017-11-30 stsp }
369 2c56f2ce 2017-11-30 stsp
370 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
371 2c56f2ce 2017-11-30 stsp if (err)
372 2c56f2ce 2017-11-30 stsp goto done;
373 2c56f2ce 2017-11-30 stsp
374 54156555 2018-12-24 stsp err = got_diff_tree(tree, NULL, label, NULL, diff_context, repo,
375 54156555 2018-12-24 stsp outfile);
376 2c56f2ce 2017-11-30 stsp done:
377 2c56f2ce 2017-11-30 stsp if (tree)
378 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
379 2c56f2ce 2017-11-30 stsp if (treeobj)
380 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
381 2c56f2ce 2017-11-30 stsp return err;
382 474b4f94 2017-11-30 stsp }
383 474b4f94 2017-11-30 stsp
384 474b4f94 2017-11-30 stsp static const struct got_error *
385 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
386 f6861a81 2018-09-13 stsp const char *label1, const char *label2, FILE *outfile)
387 474b4f94 2017-11-30 stsp {
388 013404a9 2017-11-30 stsp /* XXX TODO */
389 474b4f94 2017-11-30 stsp return NULL;
390 474b4f94 2017-11-30 stsp }
391 474b4f94 2017-11-30 stsp
392 474b4f94 2017-11-30 stsp static const struct got_error *
393 f6861a81 2018-09-13 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
394 54156555 2018-12-24 stsp const char *label1, const char *label2, int diff_context,
395 54156555 2018-12-24 stsp struct got_repository *repo, FILE *outfile)
396 474b4f94 2017-11-30 stsp {
397 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
398 19ae6da1 2018-11-05 stsp int id_match;
399 474b4f94 2017-11-30 stsp
400 474b4f94 2017-11-30 stsp if (te2 == NULL) {
401 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
402 54156555 2018-12-24 stsp err = diff_deleted_tree(te1->id, label1, diff_context,
403 54156555 2018-12-24 stsp repo, outfile);
404 f6861a81 2018-09-13 stsp else
405 54156555 2018-12-24 stsp err = diff_deleted_blob(te1->id, label1, diff_context,
406 54156555 2018-12-24 stsp repo, outfile);
407 f6861a81 2018-09-13 stsp return err;
408 474b4f94 2017-11-30 stsp }
409 474b4f94 2017-11-30 stsp
410 19ae6da1 2018-11-05 stsp id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
411 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
412 19ae6da1 2018-11-05 stsp if (!id_match)
413 f6861a81 2018-09-13 stsp return diff_modified_tree(te1->id, te2->id,
414 54156555 2018-12-24 stsp label1, label2, diff_context, repo, outfile);
415 4209f790 2017-11-30 stsp } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
416 19ae6da1 2018-11-05 stsp if (!id_match)
417 f6861a81 2018-09-13 stsp return diff_modified_blob(te1->id, te2->id,
418 54156555 2018-12-24 stsp label1, label2, diff_context, repo, outfile);
419 413ea19d 2017-11-30 stsp }
420 474b4f94 2017-11-30 stsp
421 19ae6da1 2018-11-05 stsp if (id_match)
422 f6861a81 2018-09-13 stsp return NULL;
423 f6861a81 2018-09-13 stsp
424 f6861a81 2018-09-13 stsp return diff_kind_mismatch(te1->id, te2->id, label1, label2, outfile);
425 474b4f94 2017-11-30 stsp }
426 474b4f94 2017-11-30 stsp
427 474b4f94 2017-11-30 stsp static const struct got_error *
428 f6861a81 2018-09-13 stsp diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_entry *te1,
429 54156555 2018-12-24 stsp const char *label2, int diff_context, struct got_repository *repo,
430 54156555 2018-12-24 stsp FILE *outfile)
431 474b4f94 2017-11-30 stsp {
432 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
433 f6861a81 2018-09-13 stsp return NULL;
434 474b4f94 2017-11-30 stsp
435 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
436 54156555 2018-12-24 stsp return diff_added_tree(te2->id, label2, diff_context, repo,
437 54156555 2018-12-24 stsp outfile);
438 f6861a81 2018-09-13 stsp
439 54156555 2018-12-24 stsp return diff_added_blob(te2->id, label2, diff_context, repo, outfile);
440 474b4f94 2017-11-30 stsp }
441 474b4f94 2017-11-30 stsp
442 474b4f94 2017-11-30 stsp const struct got_error *
443 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
444 54156555 2018-12-24 stsp const char *label1, const char *label2, int diff_context,
445 54156555 2018-12-24 stsp struct got_repository *repo, FILE *outfile)
446 474b4f94 2017-11-30 stsp {
447 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
448 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
449 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
450 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
451 474b4f94 2017-11-30 stsp
452 883f0469 2018-06-23 stsp if (tree1) {
453 883f0469 2018-06-23 stsp const struct got_tree_entries *entries;
454 883f0469 2018-06-23 stsp entries = got_object_tree_get_entries(tree1);
455 883f0469 2018-06-23 stsp te1 = SIMPLEQ_FIRST(&entries->head);
456 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
457 f6861a81 2018-09-13 stsp te1->name) == -1)
458 f6861a81 2018-09-13 stsp return got_error_from_errno();
459 883f0469 2018-06-23 stsp }
460 883f0469 2018-06-23 stsp if (tree2) {
461 883f0469 2018-06-23 stsp const struct got_tree_entries *entries;
462 883f0469 2018-06-23 stsp entries = got_object_tree_get_entries(tree2);
463 883f0469 2018-06-23 stsp te2 = SIMPLEQ_FIRST(&entries->head);
464 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
465 f6861a81 2018-09-13 stsp te2->name) == -1)
466 f6861a81 2018-09-13 stsp return got_error_from_errno();
467 883f0469 2018-06-23 stsp }
468 474b4f94 2017-11-30 stsp
469 474b4f94 2017-11-30 stsp do {
470 474b4f94 2017-11-30 stsp if (te1) {
471 f6861a81 2018-09-13 stsp struct got_tree_entry *te = NULL;
472 f6861a81 2018-09-13 stsp if (tree2)
473 f6861a81 2018-09-13 stsp te = match_entry_by_name(te1, tree2);
474 f6861a81 2018-09-13 stsp if (te) {
475 f6861a81 2018-09-13 stsp free(l2);
476 f6861a81 2018-09-13 stsp l2 = NULL;
477 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
478 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
479 f6861a81 2018-09-13 stsp return got_error_from_errno();
480 f6861a81 2018-09-13 stsp }
481 54156555 2018-12-24 stsp err = diff_entry_old_new(te1, te, l1, l2, diff_context,
482 54156555 2018-12-24 stsp repo, outfile);
483 474b4f94 2017-11-30 stsp if (err)
484 474b4f94 2017-11-30 stsp break;
485 474b4f94 2017-11-30 stsp }
486 474b4f94 2017-11-30 stsp
487 474b4f94 2017-11-30 stsp if (te2) {
488 f6861a81 2018-09-13 stsp struct got_tree_entry *te = NULL;
489 f6861a81 2018-09-13 stsp if (tree1)
490 f6861a81 2018-09-13 stsp te = match_entry_by_name(te2, tree1);
491 d6ce02f1 2018-11-17 stsp free(l2);
492 d6ce02f1 2018-11-17 stsp if (te) {
493 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
494 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
495 d6ce02f1 2018-11-17 stsp return got_error_from_errno();
496 d6ce02f1 2018-11-17 stsp } else {
497 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
498 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->name) == -1)
499 d6ce02f1 2018-11-17 stsp return got_error_from_errno();
500 d6ce02f1 2018-11-17 stsp }
501 54156555 2018-12-24 stsp err = diff_entry_new_old(te2, te, l2, diff_context,
502 54156555 2018-12-24 stsp repo, outfile);
503 474b4f94 2017-11-30 stsp if (err)
504 474b4f94 2017-11-30 stsp break;
505 474b4f94 2017-11-30 stsp }
506 474b4f94 2017-11-30 stsp
507 f6861a81 2018-09-13 stsp free(l1);
508 f6861a81 2018-09-13 stsp l1 = NULL;
509 f6861a81 2018-09-13 stsp if (te1) {
510 474b4f94 2017-11-30 stsp te1 = SIMPLEQ_NEXT(te1, entry);
511 f6861a81 2018-09-13 stsp if (te1 &&
512 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
513 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
514 f6861a81 2018-09-13 stsp return got_error_from_errno();
515 f6861a81 2018-09-13 stsp }
516 f6861a81 2018-09-13 stsp free(l2);
517 f6861a81 2018-09-13 stsp l2 = NULL;
518 f6861a81 2018-09-13 stsp if (te2) {
519 474b4f94 2017-11-30 stsp te2 = SIMPLEQ_NEXT(te2, entry);
520 f6861a81 2018-09-13 stsp if (te2 &&
521 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
522 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
523 f6861a81 2018-09-13 stsp return got_error_from_errno();
524 f6861a81 2018-09-13 stsp }
525 474b4f94 2017-11-30 stsp } while (te1 || te2);
526 11528a82 2018-05-19 stsp
527 11528a82 2018-05-19 stsp return err;
528 11528a82 2018-05-19 stsp }
529 11528a82 2018-05-19 stsp
530 11528a82 2018-05-19 stsp const struct got_error *
531 15a94983 2018-12-23 stsp got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
532 54156555 2018-12-24 stsp const char *label1, const char *label2, int diff_context,
533 54156555 2018-12-24 stsp struct got_repository *repo, FILE *outfile)
534 11528a82 2018-05-19 stsp {
535 11528a82 2018-05-19 stsp const struct got_error *err;
536 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
537 b74c7625 2018-05-20 stsp
538 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
539 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
540 11528a82 2018-05-19 stsp
541 15a94983 2018-12-23 stsp if (id1) {
542 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob1, repo, id1, 8192);
543 cd0acaa7 2018-05-20 stsp if (err)
544 cd0acaa7 2018-05-20 stsp goto done;
545 cd0acaa7 2018-05-20 stsp }
546 15a94983 2018-12-23 stsp if (id2) {
547 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob2, repo, id2, 8192);
548 cd0acaa7 2018-05-20 stsp if (err)
549 cd0acaa7 2018-05-20 stsp goto done;
550 cd0acaa7 2018-05-20 stsp }
551 54156555 2018-12-24 stsp err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
552 54156555 2018-12-24 stsp outfile);
553 11528a82 2018-05-19 stsp done:
554 11528a82 2018-05-19 stsp if (blob1)
555 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
556 11528a82 2018-05-19 stsp if (blob2)
557 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
558 474b4f94 2017-11-30 stsp return err;
559 474b4f94 2017-11-30 stsp }
560 11528a82 2018-05-19 stsp
561 11528a82 2018-05-19 stsp const struct got_error *
562 15a94983 2018-12-23 stsp got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
563 54156555 2018-12-24 stsp char *label1, char *label2, int diff_context, struct got_repository *repo,
564 54156555 2018-12-24 stsp FILE *outfile)
565 11528a82 2018-05-19 stsp {
566 11528a82 2018-05-19 stsp const struct got_error *err;
567 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
568 11528a82 2018-05-19 stsp
569 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
570 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
571 b74c7625 2018-05-20 stsp
572 15a94983 2018-12-23 stsp if (id1) {
573 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
574 cd0acaa7 2018-05-20 stsp if (err)
575 cd0acaa7 2018-05-20 stsp goto done;
576 cd0acaa7 2018-05-20 stsp }
577 15a94983 2018-12-23 stsp if (id2) {
578 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
579 cd0acaa7 2018-05-20 stsp if (err)
580 cd0acaa7 2018-05-20 stsp goto done;
581 cd0acaa7 2018-05-20 stsp }
582 54156555 2018-12-24 stsp err = got_diff_tree(tree1, tree2, label1, label2, diff_context,
583 54156555 2018-12-24 stsp repo, outfile);
584 11528a82 2018-05-19 stsp done:
585 11528a82 2018-05-19 stsp if (tree1)
586 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
587 11528a82 2018-05-19 stsp if (tree2)
588 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
589 11528a82 2018-05-19 stsp return err;
590 11528a82 2018-05-19 stsp }
591 11528a82 2018-05-19 stsp
592 11528a82 2018-05-19 stsp const struct got_error *
593 15a94983 2018-12-23 stsp got_diff_objects_as_commits(struct got_object_id *id1,
594 15a94983 2018-12-23 stsp struct got_object_id *id2, int diff_context,
595 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
596 11528a82 2018-05-19 stsp {
597 11528a82 2018-05-19 stsp const struct got_error *err;
598 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
599 11528a82 2018-05-19 stsp
600 15a94983 2018-12-23 stsp if (id2 == NULL)
601 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
602 b74c7625 2018-05-20 stsp
603 15a94983 2018-12-23 stsp if (id1) {
604 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
605 cd0acaa7 2018-05-20 stsp if (err)
606 cd0acaa7 2018-05-20 stsp goto done;
607 cd0acaa7 2018-05-20 stsp }
608 bacc9935 2018-05-20 stsp
609 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
610 9b697879 2018-05-20 stsp if (err)
611 9b697879 2018-05-20 stsp goto done;
612 9b697879 2018-05-20 stsp
613 15a94983 2018-12-23 stsp err = got_diff_objects_as_trees(
614 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
615 54156555 2018-12-24 stsp got_object_commit_get_tree_id(commit2), "", "", diff_context, repo,
616 54156555 2018-12-24 stsp outfile);
617 11528a82 2018-05-19 stsp done:
618 11528a82 2018-05-19 stsp if (commit1)
619 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
620 11528a82 2018-05-19 stsp if (commit2)
621 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
622 11528a82 2018-05-19 stsp return err;
623 11528a82 2018-05-19 stsp }