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