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