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 7d283eee 2017-11-29 stsp
32 7d283eee 2017-11-29 stsp #include "diff.h"
33 a1fd68d8 2018-01-12 stsp #include "path.h"
34 a7852263 2017-11-30 stsp
35 7d283eee 2017-11-29 stsp const struct got_error *
36 7d283eee 2017-11-29 stsp got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
37 474b4f94 2017-11-30 stsp const char *label1, const char *label2, FILE *outfile)
38 7d283eee 2017-11-29 stsp {
39 ed9e98a8 2017-11-29 stsp struct got_diff_state ds;
40 8ba9a219 2017-11-29 stsp struct got_diff_args args;
41 7d283eee 2017-11-29 stsp const struct got_error *err = NULL;
42 4e22badc 2017-11-30 stsp FILE *f1 = NULL, *f2 = NULL;
43 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
44 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
45 98abbc84 2017-11-30 stsp char *idstr1 = NULL, *idstr2 = NULL;
46 f9d67749 2017-11-30 stsp size_t len, hdrlen;
47 4e22badc 2017-11-30 stsp int res, flags = 0;
48 7d283eee 2017-11-29 stsp
49 4e22badc 2017-11-30 stsp if (blob1) {
50 a1fd68d8 2018-01-12 stsp f1 = got_opentemp();
51 4e22badc 2017-11-30 stsp if (f1 == NULL)
52 4e22badc 2017-11-30 stsp return got_error(GOT_ERR_FILE_OPEN);
53 4e22badc 2017-11-30 stsp } else
54 4e22badc 2017-11-30 stsp flags |= D_EMPTY1;
55 7d283eee 2017-11-29 stsp
56 4e22badc 2017-11-30 stsp if (blob2) {
57 a1fd68d8 2018-01-12 stsp f2 = got_opentemp();
58 4e22badc 2017-11-30 stsp if (f2 == NULL) {
59 4e22badc 2017-11-30 stsp fclose(f1);
60 4e22badc 2017-11-30 stsp return got_error(GOT_ERR_FILE_OPEN);
61 4e22badc 2017-11-30 stsp }
62 4e22badc 2017-11-30 stsp } else
63 4e22badc 2017-11-30 stsp flags |= D_EMPTY2;
64 7d283eee 2017-11-29 stsp
65 98abbc84 2017-11-30 stsp if (blob1) {
66 98abbc84 2017-11-30 stsp idstr1 = got_object_id_str(&blob1->id, hex1, sizeof(hex1));
67 4e22badc 2017-11-30 stsp hdrlen = blob1->hdrlen;
68 4e22badc 2017-11-30 stsp do {
69 eb651edf 2018-02-11 stsp err = got_object_blob_read_block(&len, blob1);
70 4e22badc 2017-11-30 stsp if (err)
71 4e22badc 2017-11-30 stsp goto done;
72 eba5c6bb 2018-02-11 stsp if (len == 0)
73 eba5c6bb 2018-02-11 stsp break;
74 4e22badc 2017-11-30 stsp /* Skip blob object header first time around. */
75 eb651edf 2018-02-11 stsp fwrite(blob1->read_buf + hdrlen, len - hdrlen, 1, f1);
76 4e22badc 2017-11-30 stsp hdrlen = 0;
77 4e22badc 2017-11-30 stsp } while (len != 0);
78 98abbc84 2017-11-30 stsp } else
79 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
80 7d283eee 2017-11-29 stsp
81 98abbc84 2017-11-30 stsp if (blob2) {
82 98abbc84 2017-11-30 stsp idstr2 = got_object_id_str(&blob2->id, hex2, sizeof(hex2));
83 98abbc84 2017-11-30 stsp hdrlen = blob2->hdrlen;
84 98abbc84 2017-11-30 stsp do {
85 eb651edf 2018-02-11 stsp err = got_object_blob_read_block(&len, blob2);
86 98abbc84 2017-11-30 stsp if (err)
87 98abbc84 2017-11-30 stsp goto done;
88 eba5c6bb 2018-02-11 stsp if (len == 0)
89 eba5c6bb 2018-02-11 stsp break;
90 98abbc84 2017-11-30 stsp /* Skip blob object header first time around. */
91 eb651edf 2018-02-11 stsp fwrite(blob2->read_buf + hdrlen, len - hdrlen, 1, f2);
92 98abbc84 2017-11-30 stsp hdrlen = 0;
93 98abbc84 2017-11-30 stsp } while (len != 0);
94 98abbc84 2017-11-30 stsp } else
95 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
96 7d283eee 2017-11-29 stsp
97 eba5c6bb 2018-02-11 stsp if (f1) {
98 eba5c6bb 2018-02-11 stsp fflush(f1);
99 eba5c6bb 2018-02-11 stsp rewind(f1);
100 eba5c6bb 2018-02-11 stsp }
101 eba5c6bb 2018-02-11 stsp if (f2) {
102 98abbc84 2017-11-30 stsp fflush(f2);
103 eba5c6bb 2018-02-11 stsp rewind(f2);
104 eba5c6bb 2018-02-11 stsp }
105 7d283eee 2017-11-29 stsp
106 ed9e98a8 2017-11-29 stsp memset(&ds, 0, sizeof(ds));
107 f9d67749 2017-11-30 stsp /* XXX should stat buffers be passed in args instead of ds? */
108 f9d67749 2017-11-30 stsp ds.stb1.st_mode = S_IFREG;
109 98abbc84 2017-11-30 stsp if (blob1)
110 98abbc84 2017-11-30 stsp ds.stb1.st_size = blob1->zb.z.total_out;
111 f9d67749 2017-11-30 stsp ds.stb1.st_mtime = 0; /* XXX */
112 8ba9a219 2017-11-29 stsp
113 f9d67749 2017-11-30 stsp ds.stb2.st_mode = S_IFREG;
114 98abbc84 2017-11-30 stsp if (blob2)
115 98abbc84 2017-11-30 stsp ds.stb2.st_size = blob2->zb.z.total_out;
116 f9d67749 2017-11-30 stsp ds.stb2.st_mtime = 0; /* XXX */
117 f9d67749 2017-11-30 stsp
118 f9d67749 2017-11-30 stsp memset(&args, 0, sizeof(args));
119 8ba9a219 2017-11-29 stsp args.diff_format = D_UNIFIED;
120 98abbc84 2017-11-30 stsp args.label[0] = label1 ? label1 : idstr1;
121 98abbc84 2017-11-30 stsp args.label[1] = label2 ? label2 : idstr2;
122 62136d3a 2017-11-29 stsp
123 cb74ff21 2017-11-30 stsp err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile);
124 7d283eee 2017-11-29 stsp done:
125 98abbc84 2017-11-30 stsp if (f1)
126 98abbc84 2017-11-30 stsp fclose(f1);
127 98abbc84 2017-11-30 stsp if (f2)
128 98abbc84 2017-11-30 stsp fclose(f2);
129 7d283eee 2017-11-29 stsp return err;
130 7d283eee 2017-11-29 stsp }
131 474b4f94 2017-11-30 stsp
132 a3e2cbea 2017-12-01 stsp struct got_tree_entry *
133 a3e2cbea 2017-12-01 stsp match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
134 474b4f94 2017-11-30 stsp {
135 a3e2cbea 2017-12-01 stsp struct got_tree_entry *te2;
136 a3e2cbea 2017-12-01 stsp
137 a3e2cbea 2017-12-01 stsp SIMPLEQ_FOREACH(te2, &tree2->entries, entry) {
138 a3e2cbea 2017-12-01 stsp if (strcmp(te1->name, te2->name) == 0)
139 a3e2cbea 2017-12-01 stsp return te2;
140 a3e2cbea 2017-12-01 stsp }
141 474b4f94 2017-11-30 stsp return NULL;
142 474b4f94 2017-11-30 stsp }
143 474b4f94 2017-11-30 stsp
144 474b4f94 2017-11-30 stsp static const struct got_error *
145 74671950 2018-02-11 stsp diff_added_blob(struct got_object_id *id, struct got_repository *repo,
146 74671950 2018-02-11 stsp FILE *outfile)
147 474b4f94 2017-11-30 stsp {
148 4e22badc 2017-11-30 stsp const struct got_error *err;
149 4e22badc 2017-11-30 stsp struct got_blob_object *blob;
150 4e22badc 2017-11-30 stsp struct got_object *obj;
151 4e22badc 2017-11-30 stsp
152 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
153 4e22badc 2017-11-30 stsp if (err)
154 4e22badc 2017-11-30 stsp return err;
155 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
156 4e22badc 2017-11-30 stsp if (err != NULL)
157 4e22badc 2017-11-30 stsp return err;
158 4e22badc 2017-11-30 stsp
159 74671950 2018-02-11 stsp return got_diff_blob(NULL, blob, NULL, NULL, outfile);
160 474b4f94 2017-11-30 stsp }
161 474b4f94 2017-11-30 stsp
162 474b4f94 2017-11-30 stsp static const struct got_error *
163 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
164 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
165 474b4f94 2017-11-30 stsp {
166 6a213ccb 2017-11-30 stsp const struct got_error *err;
167 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
168 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
169 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
170 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
171 6a213ccb 2017-11-30 stsp
172 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
173 6a213ccb 2017-11-30 stsp if (err)
174 6a213ccb 2017-11-30 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
175 eef6493a 2018-01-19 stsp if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) {
176 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
177 6a213ccb 2017-11-30 stsp goto done;
178 6a213ccb 2017-11-30 stsp }
179 6a213ccb 2017-11-30 stsp
180 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
181 6a213ccb 2017-11-30 stsp if (err) {
182 6a213ccb 2017-11-30 stsp err= got_error(GOT_ERR_BAD_OBJ_HDR);
183 6a213ccb 2017-11-30 stsp goto done;
184 6a213ccb 2017-11-30 stsp }
185 eef6493a 2018-01-19 stsp if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) {
186 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
187 6a213ccb 2017-11-30 stsp goto done;
188 6a213ccb 2017-11-30 stsp }
189 6a213ccb 2017-11-30 stsp
190 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
191 6a213ccb 2017-11-30 stsp if (err != NULL) {
192 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_FILE_OPEN);
193 6a213ccb 2017-11-30 stsp goto done;
194 6a213ccb 2017-11-30 stsp }
195 6a213ccb 2017-11-30 stsp
196 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
197 6a213ccb 2017-11-30 stsp if (err != NULL) {
198 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_FILE_OPEN);
199 6a213ccb 2017-11-30 stsp goto done;
200 6a213ccb 2017-11-30 stsp }
201 6a213ccb 2017-11-30 stsp
202 74671950 2018-02-11 stsp err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
203 6a213ccb 2017-11-30 stsp
204 6a213ccb 2017-11-30 stsp done:
205 a3e2cbea 2017-12-01 stsp if (obj1)
206 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
207 a3e2cbea 2017-12-01 stsp if (obj2)
208 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
209 a3e2cbea 2017-12-01 stsp if (blob1)
210 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
211 a3e2cbea 2017-12-01 stsp if (blob2)
212 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
213 6a213ccb 2017-11-30 stsp return err;
214 474b4f94 2017-11-30 stsp }
215 474b4f94 2017-11-30 stsp
216 474b4f94 2017-11-30 stsp static const struct got_error *
217 74671950 2018-02-11 stsp diff_deleted_blob(struct got_object_id *id, struct got_repository *repo,
218 74671950 2018-02-11 stsp FILE *outfile)
219 474b4f94 2017-11-30 stsp {
220 365fb436 2017-11-30 stsp const struct got_error *err;
221 365fb436 2017-11-30 stsp struct got_blob_object *blob;
222 365fb436 2017-11-30 stsp struct got_object *obj;
223 365fb436 2017-11-30 stsp
224 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
225 365fb436 2017-11-30 stsp if (err)
226 365fb436 2017-11-30 stsp return err;
227 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
228 365fb436 2017-11-30 stsp if (err != NULL)
229 365fb436 2017-11-30 stsp return err;
230 365fb436 2017-11-30 stsp
231 74671950 2018-02-11 stsp return got_diff_blob(blob, NULL, NULL, NULL, outfile);
232 474b4f94 2017-11-30 stsp }
233 474b4f94 2017-11-30 stsp
234 474b4f94 2017-11-30 stsp static const struct got_error *
235 74671950 2018-02-11 stsp diff_added_tree(struct got_object_id *id, struct got_repository *repo,
236 74671950 2018-02-11 stsp FILE *outfile)
237 474b4f94 2017-11-30 stsp {
238 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
239 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
240 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
241 9c70d4c3 2017-11-30 stsp
242 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
243 9c70d4c3 2017-11-30 stsp if (err)
244 9c70d4c3 2017-11-30 stsp goto done;
245 9c70d4c3 2017-11-30 stsp
246 b107e67f 2018-01-19 stsp if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
247 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
248 9c70d4c3 2017-11-30 stsp goto done;
249 9c70d4c3 2017-11-30 stsp }
250 9c70d4c3 2017-11-30 stsp
251 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
252 9c70d4c3 2017-11-30 stsp if (err)
253 9c70d4c3 2017-11-30 stsp goto done;
254 9c70d4c3 2017-11-30 stsp
255 74671950 2018-02-11 stsp err = got_diff_tree(NULL, tree, repo, outfile);
256 9c70d4c3 2017-11-30 stsp
257 9c70d4c3 2017-11-30 stsp done:
258 9c70d4c3 2017-11-30 stsp if (tree)
259 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
260 9c70d4c3 2017-11-30 stsp if (treeobj)
261 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
262 9c70d4c3 2017-11-30 stsp return err;
263 474b4f94 2017-11-30 stsp }
264 474b4f94 2017-11-30 stsp
265 474b4f94 2017-11-30 stsp static const struct got_error *
266 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
267 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
268 474b4f94 2017-11-30 stsp {
269 789689b5 2017-11-30 stsp const struct got_error *err = NULL;
270 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
271 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
272 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
273 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
274 789689b5 2017-11-30 stsp
275 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
276 789689b5 2017-11-30 stsp if (err)
277 789689b5 2017-11-30 stsp goto done;
278 789689b5 2017-11-30 stsp
279 eef6493a 2018-01-19 stsp if (got_object_get_type(treeobj1) != GOT_OBJ_TYPE_TREE) {
280 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
281 789689b5 2017-11-30 stsp goto done;
282 789689b5 2017-11-30 stsp }
283 789689b5 2017-11-30 stsp
284 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
285 789689b5 2017-11-30 stsp if (err)
286 789689b5 2017-11-30 stsp goto done;
287 789689b5 2017-11-30 stsp
288 eef6493a 2018-01-19 stsp if (got_object_get_type(treeobj2) != GOT_OBJ_TYPE_TREE) {
289 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
290 789689b5 2017-11-30 stsp goto done;
291 789689b5 2017-11-30 stsp }
292 789689b5 2017-11-30 stsp
293 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
294 789689b5 2017-11-30 stsp if (err)
295 789689b5 2017-11-30 stsp goto done;
296 789689b5 2017-11-30 stsp
297 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
298 789689b5 2017-11-30 stsp if (err)
299 789689b5 2017-11-30 stsp goto done;
300 789689b5 2017-11-30 stsp
301 74671950 2018-02-11 stsp err = got_diff_tree(tree1, tree2, repo, outfile);
302 789689b5 2017-11-30 stsp
303 789689b5 2017-11-30 stsp done:
304 789689b5 2017-11-30 stsp if (tree1)
305 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
306 789689b5 2017-11-30 stsp if (tree2)
307 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
308 789689b5 2017-11-30 stsp if (treeobj1)
309 789689b5 2017-11-30 stsp got_object_close(treeobj1);
310 789689b5 2017-11-30 stsp if (treeobj2)
311 789689b5 2017-11-30 stsp got_object_close(treeobj2);
312 789689b5 2017-11-30 stsp return err;
313 474b4f94 2017-11-30 stsp }
314 474b4f94 2017-11-30 stsp
315 474b4f94 2017-11-30 stsp static const struct got_error *
316 74671950 2018-02-11 stsp diff_deleted_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
317 474b4f94 2017-11-30 stsp {
318 2c56f2ce 2017-11-30 stsp const struct got_error *err = NULL;
319 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
320 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
321 2c56f2ce 2017-11-30 stsp
322 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
323 2c56f2ce 2017-11-30 stsp if (err)
324 2c56f2ce 2017-11-30 stsp goto done;
325 2c56f2ce 2017-11-30 stsp
326 b107e67f 2018-01-19 stsp if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
327 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
328 2c56f2ce 2017-11-30 stsp goto done;
329 2c56f2ce 2017-11-30 stsp }
330 2c56f2ce 2017-11-30 stsp
331 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
332 2c56f2ce 2017-11-30 stsp if (err)
333 2c56f2ce 2017-11-30 stsp goto done;
334 2c56f2ce 2017-11-30 stsp
335 74671950 2018-02-11 stsp err = got_diff_tree(tree, NULL, repo, outfile);
336 2c56f2ce 2017-11-30 stsp
337 2c56f2ce 2017-11-30 stsp done:
338 2c56f2ce 2017-11-30 stsp if (tree)
339 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
340 2c56f2ce 2017-11-30 stsp if (treeobj)
341 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
342 2c56f2ce 2017-11-30 stsp return err;
343 474b4f94 2017-11-30 stsp }
344 474b4f94 2017-11-30 stsp
345 474b4f94 2017-11-30 stsp static const struct got_error *
346 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
347 74671950 2018-02-11 stsp FILE *outfile)
348 474b4f94 2017-11-30 stsp {
349 013404a9 2017-11-30 stsp /* XXX TODO */
350 474b4f94 2017-11-30 stsp return NULL;
351 474b4f94 2017-11-30 stsp }
352 474b4f94 2017-11-30 stsp
353 474b4f94 2017-11-30 stsp static const struct got_error *
354 6a213ccb 2017-11-30 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
355 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
356 474b4f94 2017-11-30 stsp {
357 474b4f94 2017-11-30 stsp const struct got_error *err;
358 474b4f94 2017-11-30 stsp struct got_tree_entry *te2;
359 a3e2cbea 2017-12-01 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
360 474b4f94 2017-11-30 stsp
361 a3e2cbea 2017-12-01 stsp te2 = match_entry_by_name(te1, tree2);
362 474b4f94 2017-11-30 stsp if (te2 == NULL) {
363 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
364 74671950 2018-02-11 stsp return diff_deleted_tree(&te1->id, repo, outfile);
365 74671950 2018-02-11 stsp return diff_deleted_blob(&te1->id, repo, outfile);
366 474b4f94 2017-11-30 stsp }
367 474b4f94 2017-11-30 stsp
368 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
369 a1fd68d8 2018-01-12 stsp if (got_object_id_cmp(&te1->id, &te2->id) != 0)
370 74671950 2018-02-11 stsp return diff_modified_tree(&te1->id, &te2->id, repo,
371 74671950 2018-02-11 stsp outfile);
372 4209f790 2017-11-30 stsp } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
373 a1fd68d8 2018-01-12 stsp if (got_object_id_cmp(&te1->id, &te2->id) != 0)
374 74671950 2018-02-11 stsp return diff_modified_blob(&te1->id, &te2->id, repo,
375 74671950 2018-02-11 stsp outfile);
376 413ea19d 2017-11-30 stsp }
377 474b4f94 2017-11-30 stsp
378 74671950 2018-02-11 stsp return diff_kind_mismatch(&te1->id, &te2->id, outfile);
379 474b4f94 2017-11-30 stsp }
380 474b4f94 2017-11-30 stsp
381 474b4f94 2017-11-30 stsp static const struct got_error *
382 4e22badc 2017-11-30 stsp diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
383 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
384 474b4f94 2017-11-30 stsp {
385 474b4f94 2017-11-30 stsp const struct got_error *err;
386 474b4f94 2017-11-30 stsp struct got_tree_entry *te1;
387 474b4f94 2017-11-30 stsp
388 a3e2cbea 2017-12-01 stsp te1 = match_entry_by_name(te2, tree1);
389 474b4f94 2017-11-30 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
390 474b4f94 2017-11-30 stsp return NULL;
391 474b4f94 2017-11-30 stsp
392 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
393 74671950 2018-02-11 stsp return diff_added_tree(&te2->id, repo, outfile);
394 74671950 2018-02-11 stsp return diff_added_blob(&te2->id, repo, outfile);
395 474b4f94 2017-11-30 stsp }
396 474b4f94 2017-11-30 stsp
397 474b4f94 2017-11-30 stsp const struct got_error *
398 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
399 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
400 474b4f94 2017-11-30 stsp {
401 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
402 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
403 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
404 474b4f94 2017-11-30 stsp
405 789689b5 2017-11-30 stsp if (tree1)
406 789689b5 2017-11-30 stsp te1 = SIMPLEQ_FIRST(&tree1->entries);
407 789689b5 2017-11-30 stsp if (tree2)
408 789689b5 2017-11-30 stsp te2 = SIMPLEQ_FIRST(&tree2->entries);
409 474b4f94 2017-11-30 stsp
410 474b4f94 2017-11-30 stsp do {
411 474b4f94 2017-11-30 stsp if (te1) {
412 74671950 2018-02-11 stsp err = diff_entry_old_new(te1, tree2, repo, outfile);
413 474b4f94 2017-11-30 stsp if (err)
414 474b4f94 2017-11-30 stsp break;
415 474b4f94 2017-11-30 stsp }
416 474b4f94 2017-11-30 stsp
417 474b4f94 2017-11-30 stsp if (te2) {
418 74671950 2018-02-11 stsp err = diff_entry_new_old(te2, tree1, repo, outfile);
419 474b4f94 2017-11-30 stsp if (err)
420 474b4f94 2017-11-30 stsp break;
421 474b4f94 2017-11-30 stsp }
422 474b4f94 2017-11-30 stsp
423 474b4f94 2017-11-30 stsp if (te1)
424 474b4f94 2017-11-30 stsp te1 = SIMPLEQ_NEXT(te1, entry);
425 474b4f94 2017-11-30 stsp if (te2)
426 474b4f94 2017-11-30 stsp te2 = SIMPLEQ_NEXT(te2, entry);
427 474b4f94 2017-11-30 stsp } while (te1 || te2);
428 474b4f94 2017-11-30 stsp
429 474b4f94 2017-11-30 stsp return err;
430 474b4f94 2017-11-30 stsp }