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 a7852263 2017-11-30 stsp
36 7d283eee 2017-11-29 stsp const struct got_error *
37 7d283eee 2017-11-29 stsp got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
38 474b4f94 2017-11-30 stsp const char *label1, const char *label2, FILE *outfile)
39 7d283eee 2017-11-29 stsp {
40 ed9e98a8 2017-11-29 stsp struct got_diff_state ds;
41 8ba9a219 2017-11-29 stsp struct got_diff_args args;
42 7d283eee 2017-11-29 stsp const struct got_error *err = NULL;
43 4e22badc 2017-11-30 stsp FILE *f1 = NULL, *f2 = NULL;
44 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
45 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
46 98abbc84 2017-11-30 stsp char *idstr1 = NULL, *idstr2 = NULL;
47 f9d67749 2017-11-30 stsp size_t len, hdrlen;
48 f934cf2c 2018-02-12 stsp size_t size1, size2;
49 4e22badc 2017-11-30 stsp int res, flags = 0;
50 7d283eee 2017-11-29 stsp
51 4e22badc 2017-11-30 stsp if (blob1) {
52 a1fd68d8 2018-01-12 stsp f1 = got_opentemp();
53 4e22badc 2017-11-30 stsp if (f1 == NULL)
54 4e22badc 2017-11-30 stsp return got_error(GOT_ERR_FILE_OPEN);
55 4e22badc 2017-11-30 stsp } else
56 4e22badc 2017-11-30 stsp flags |= D_EMPTY1;
57 7d283eee 2017-11-29 stsp
58 4e22badc 2017-11-30 stsp if (blob2) {
59 a1fd68d8 2018-01-12 stsp f2 = got_opentemp();
60 4e22badc 2017-11-30 stsp if (f2 == NULL) {
61 4e22badc 2017-11-30 stsp fclose(f1);
62 4e22badc 2017-11-30 stsp return got_error(GOT_ERR_FILE_OPEN);
63 4e22badc 2017-11-30 stsp }
64 4e22badc 2017-11-30 stsp } else
65 4e22badc 2017-11-30 stsp flags |= D_EMPTY2;
66 7d283eee 2017-11-29 stsp
67 f934cf2c 2018-02-12 stsp size1 = 0;
68 98abbc84 2017-11-30 stsp if (blob1) {
69 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
70 f934cf2c 2018-02-12 stsp hdrlen = got_object_blob_get_hdrlen(blob1);
71 4e22badc 2017-11-30 stsp do {
72 eb651edf 2018-02-11 stsp err = got_object_blob_read_block(&len, blob1);
73 4e22badc 2017-11-30 stsp if (err)
74 4e22badc 2017-11-30 stsp goto done;
75 eba5c6bb 2018-02-11 stsp if (len == 0)
76 eba5c6bb 2018-02-11 stsp break;
77 f934cf2c 2018-02-12 stsp size1 += len;
78 4e22badc 2017-11-30 stsp /* Skip blob object header first time around. */
79 dfb54902 2018-04-02 stsp fwrite(got_object_blob_get_read_buf(blob1) + hdrlen,
80 dfb54902 2018-04-02 stsp len - hdrlen, 1, f1);
81 4e22badc 2017-11-30 stsp hdrlen = 0;
82 4e22badc 2017-11-30 stsp } while (len != 0);
83 98abbc84 2017-11-30 stsp } else
84 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
85 7d283eee 2017-11-29 stsp
86 f934cf2c 2018-02-12 stsp size2 = 0;
87 98abbc84 2017-11-30 stsp if (blob2) {
88 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
89 f934cf2c 2018-02-12 stsp hdrlen = got_object_blob_get_hdrlen(blob2);
90 98abbc84 2017-11-30 stsp do {
91 eb651edf 2018-02-11 stsp err = got_object_blob_read_block(&len, blob2);
92 98abbc84 2017-11-30 stsp if (err)
93 98abbc84 2017-11-30 stsp goto done;
94 eba5c6bb 2018-02-11 stsp if (len == 0)
95 eba5c6bb 2018-02-11 stsp break;
96 f934cf2c 2018-02-12 stsp size2 += len;
97 98abbc84 2017-11-30 stsp /* Skip blob object header first time around. */
98 dfb54902 2018-04-02 stsp fwrite(got_object_blob_get_read_buf(blob2) + hdrlen,
99 dfb54902 2018-04-02 stsp len - hdrlen, 1, f2);
100 98abbc84 2017-11-30 stsp hdrlen = 0;
101 98abbc84 2017-11-30 stsp } while (len != 0);
102 98abbc84 2017-11-30 stsp } else
103 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
104 7d283eee 2017-11-29 stsp
105 eba5c6bb 2018-02-11 stsp if (f1) {
106 eba5c6bb 2018-02-11 stsp fflush(f1);
107 eba5c6bb 2018-02-11 stsp rewind(f1);
108 eba5c6bb 2018-02-11 stsp }
109 eba5c6bb 2018-02-11 stsp if (f2) {
110 98abbc84 2017-11-30 stsp fflush(f2);
111 eba5c6bb 2018-02-11 stsp rewind(f2);
112 eba5c6bb 2018-02-11 stsp }
113 7d283eee 2017-11-29 stsp
114 ed9e98a8 2017-11-29 stsp memset(&ds, 0, sizeof(ds));
115 f9d67749 2017-11-30 stsp /* XXX should stat buffers be passed in args instead of ds? */
116 f9d67749 2017-11-30 stsp ds.stb1.st_mode = S_IFREG;
117 98abbc84 2017-11-30 stsp if (blob1)
118 f934cf2c 2018-02-12 stsp ds.stb1.st_size = size1;
119 f9d67749 2017-11-30 stsp ds.stb1.st_mtime = 0; /* XXX */
120 8ba9a219 2017-11-29 stsp
121 f9d67749 2017-11-30 stsp ds.stb2.st_mode = S_IFREG;
122 98abbc84 2017-11-30 stsp if (blob2)
123 f934cf2c 2018-02-12 stsp ds.stb2.st_size = size2;
124 f9d67749 2017-11-30 stsp ds.stb2.st_mtime = 0; /* XXX */
125 f9d67749 2017-11-30 stsp
126 f9d67749 2017-11-30 stsp memset(&args, 0, sizeof(args));
127 8ba9a219 2017-11-29 stsp args.diff_format = D_UNIFIED;
128 98abbc84 2017-11-30 stsp args.label[0] = label1 ? label1 : idstr1;
129 98abbc84 2017-11-30 stsp args.label[1] = label2 ? label2 : idstr2;
130 c2c21d46 2018-03-27 stsp args.diff_context = 3;
131 84eb021e 2018-03-27 stsp flags |= D_PROTOTYPE;
132 62136d3a 2017-11-29 stsp
133 cb74ff21 2017-11-30 stsp err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile);
134 7d283eee 2017-11-29 stsp done:
135 98abbc84 2017-11-30 stsp if (f1)
136 98abbc84 2017-11-30 stsp fclose(f1);
137 98abbc84 2017-11-30 stsp if (f2)
138 98abbc84 2017-11-30 stsp fclose(f2);
139 7d283eee 2017-11-29 stsp return err;
140 7d283eee 2017-11-29 stsp }
141 474b4f94 2017-11-30 stsp
142 a3e2cbea 2017-12-01 stsp struct got_tree_entry *
143 a3e2cbea 2017-12-01 stsp match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
144 474b4f94 2017-11-30 stsp {
145 a3e2cbea 2017-12-01 stsp struct got_tree_entry *te2;
146 a3e2cbea 2017-12-01 stsp
147 a3e2cbea 2017-12-01 stsp SIMPLEQ_FOREACH(te2, &tree2->entries, entry) {
148 a3e2cbea 2017-12-01 stsp if (strcmp(te1->name, te2->name) == 0)
149 a3e2cbea 2017-12-01 stsp return te2;
150 a3e2cbea 2017-12-01 stsp }
151 474b4f94 2017-11-30 stsp return NULL;
152 474b4f94 2017-11-30 stsp }
153 474b4f94 2017-11-30 stsp
154 474b4f94 2017-11-30 stsp static const struct got_error *
155 74671950 2018-02-11 stsp diff_added_blob(struct got_object_id *id, struct got_repository *repo,
156 74671950 2018-02-11 stsp FILE *outfile)
157 474b4f94 2017-11-30 stsp {
158 4e22badc 2017-11-30 stsp const struct got_error *err;
159 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
160 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
161 4e22badc 2017-11-30 stsp
162 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
163 4e22badc 2017-11-30 stsp if (err)
164 4e22badc 2017-11-30 stsp return err;
165 4e22badc 2017-11-30 stsp
166 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
167 2acfca77 2018-04-01 stsp if (err)
168 2acfca77 2018-04-01 stsp goto done;
169 2acfca77 2018-04-01 stsp err = got_diff_blob(NULL, blob, NULL, NULL, outfile);
170 2acfca77 2018-04-01 stsp done:
171 2acfca77 2018-04-01 stsp got_object_close(obj);
172 2acfca77 2018-04-01 stsp if (blob)
173 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
174 2acfca77 2018-04-01 stsp return err;
175 474b4f94 2017-11-30 stsp }
176 474b4f94 2017-11-30 stsp
177 474b4f94 2017-11-30 stsp static const struct got_error *
178 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
179 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
180 474b4f94 2017-11-30 stsp {
181 6a213ccb 2017-11-30 stsp const struct got_error *err;
182 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
183 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
184 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
185 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
186 6a213ccb 2017-11-30 stsp
187 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
188 6a213ccb 2017-11-30 stsp if (err)
189 730a8aa0 2018-04-24 stsp return err;
190 eef6493a 2018-01-19 stsp if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) {
191 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
192 6a213ccb 2017-11-30 stsp goto done;
193 6a213ccb 2017-11-30 stsp }
194 6a213ccb 2017-11-30 stsp
195 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
196 730a8aa0 2018-04-24 stsp if (err)
197 6a213ccb 2017-11-30 stsp goto done;
198 eef6493a 2018-01-19 stsp if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) {
199 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
200 6a213ccb 2017-11-30 stsp goto done;
201 6a213ccb 2017-11-30 stsp }
202 6a213ccb 2017-11-30 stsp
203 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
204 2acfca77 2018-04-01 stsp if (err)
205 6a213ccb 2017-11-30 stsp goto done;
206 6a213ccb 2017-11-30 stsp
207 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
208 2acfca77 2018-04-01 stsp if (err)
209 6a213ccb 2017-11-30 stsp goto done;
210 6a213ccb 2017-11-30 stsp
211 74671950 2018-02-11 stsp err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
212 6a213ccb 2017-11-30 stsp
213 6a213ccb 2017-11-30 stsp done:
214 a3e2cbea 2017-12-01 stsp if (obj1)
215 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
216 a3e2cbea 2017-12-01 stsp if (obj2)
217 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
218 a3e2cbea 2017-12-01 stsp if (blob1)
219 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
220 a3e2cbea 2017-12-01 stsp if (blob2)
221 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
222 6a213ccb 2017-11-30 stsp return err;
223 474b4f94 2017-11-30 stsp }
224 474b4f94 2017-11-30 stsp
225 474b4f94 2017-11-30 stsp static const struct got_error *
226 74671950 2018-02-11 stsp diff_deleted_blob(struct got_object_id *id, struct got_repository *repo,
227 74671950 2018-02-11 stsp FILE *outfile)
228 474b4f94 2017-11-30 stsp {
229 365fb436 2017-11-30 stsp const struct got_error *err;
230 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
231 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
232 365fb436 2017-11-30 stsp
233 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
234 365fb436 2017-11-30 stsp if (err)
235 365fb436 2017-11-30 stsp return err;
236 365fb436 2017-11-30 stsp
237 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
238 2acfca77 2018-04-01 stsp if (err)
239 2acfca77 2018-04-01 stsp goto done;
240 2acfca77 2018-04-01 stsp err = got_diff_blob(blob, NULL, NULL, NULL, outfile);
241 2acfca77 2018-04-01 stsp done:
242 2acfca77 2018-04-01 stsp got_object_close(obj);
243 2acfca77 2018-04-01 stsp if (blob)
244 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
245 2acfca77 2018-04-01 stsp return err;
246 474b4f94 2017-11-30 stsp }
247 474b4f94 2017-11-30 stsp
248 474b4f94 2017-11-30 stsp static const struct got_error *
249 74671950 2018-02-11 stsp diff_added_tree(struct got_object_id *id, struct got_repository *repo,
250 74671950 2018-02-11 stsp FILE *outfile)
251 474b4f94 2017-11-30 stsp {
252 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
253 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
254 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
255 9c70d4c3 2017-11-30 stsp
256 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
257 9c70d4c3 2017-11-30 stsp if (err)
258 9c70d4c3 2017-11-30 stsp goto done;
259 9c70d4c3 2017-11-30 stsp
260 b107e67f 2018-01-19 stsp if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
261 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
262 9c70d4c3 2017-11-30 stsp goto done;
263 9c70d4c3 2017-11-30 stsp }
264 9c70d4c3 2017-11-30 stsp
265 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
266 9c70d4c3 2017-11-30 stsp if (err)
267 9c70d4c3 2017-11-30 stsp goto done;
268 9c70d4c3 2017-11-30 stsp
269 74671950 2018-02-11 stsp err = got_diff_tree(NULL, tree, repo, outfile);
270 9c70d4c3 2017-11-30 stsp
271 9c70d4c3 2017-11-30 stsp done:
272 9c70d4c3 2017-11-30 stsp if (tree)
273 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
274 9c70d4c3 2017-11-30 stsp if (treeobj)
275 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
276 9c70d4c3 2017-11-30 stsp return err;
277 474b4f94 2017-11-30 stsp }
278 474b4f94 2017-11-30 stsp
279 474b4f94 2017-11-30 stsp static const struct got_error *
280 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
281 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
282 474b4f94 2017-11-30 stsp {
283 789689b5 2017-11-30 stsp const struct got_error *err = NULL;
284 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
285 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
286 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
287 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
288 789689b5 2017-11-30 stsp
289 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
290 789689b5 2017-11-30 stsp if (err)
291 789689b5 2017-11-30 stsp goto done;
292 789689b5 2017-11-30 stsp
293 eef6493a 2018-01-19 stsp if (got_object_get_type(treeobj1) != GOT_OBJ_TYPE_TREE) {
294 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
295 789689b5 2017-11-30 stsp goto done;
296 789689b5 2017-11-30 stsp }
297 789689b5 2017-11-30 stsp
298 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
299 789689b5 2017-11-30 stsp if (err)
300 789689b5 2017-11-30 stsp goto done;
301 789689b5 2017-11-30 stsp
302 eef6493a 2018-01-19 stsp if (got_object_get_type(treeobj2) != GOT_OBJ_TYPE_TREE) {
303 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
304 789689b5 2017-11-30 stsp goto done;
305 789689b5 2017-11-30 stsp }
306 789689b5 2017-11-30 stsp
307 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
308 789689b5 2017-11-30 stsp if (err)
309 789689b5 2017-11-30 stsp goto done;
310 789689b5 2017-11-30 stsp
311 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
312 789689b5 2017-11-30 stsp if (err)
313 789689b5 2017-11-30 stsp goto done;
314 789689b5 2017-11-30 stsp
315 74671950 2018-02-11 stsp err = got_diff_tree(tree1, tree2, repo, outfile);
316 789689b5 2017-11-30 stsp
317 789689b5 2017-11-30 stsp done:
318 789689b5 2017-11-30 stsp if (tree1)
319 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
320 789689b5 2017-11-30 stsp if (tree2)
321 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
322 789689b5 2017-11-30 stsp if (treeobj1)
323 789689b5 2017-11-30 stsp got_object_close(treeobj1);
324 789689b5 2017-11-30 stsp if (treeobj2)
325 789689b5 2017-11-30 stsp got_object_close(treeobj2);
326 789689b5 2017-11-30 stsp return err;
327 474b4f94 2017-11-30 stsp }
328 474b4f94 2017-11-30 stsp
329 474b4f94 2017-11-30 stsp static const struct got_error *
330 74671950 2018-02-11 stsp diff_deleted_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
331 474b4f94 2017-11-30 stsp {
332 2c56f2ce 2017-11-30 stsp const struct got_error *err = NULL;
333 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
334 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
335 2c56f2ce 2017-11-30 stsp
336 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
337 2c56f2ce 2017-11-30 stsp if (err)
338 2c56f2ce 2017-11-30 stsp goto done;
339 2c56f2ce 2017-11-30 stsp
340 b107e67f 2018-01-19 stsp if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
341 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
342 2c56f2ce 2017-11-30 stsp goto done;
343 2c56f2ce 2017-11-30 stsp }
344 2c56f2ce 2017-11-30 stsp
345 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
346 2c56f2ce 2017-11-30 stsp if (err)
347 2c56f2ce 2017-11-30 stsp goto done;
348 2c56f2ce 2017-11-30 stsp
349 74671950 2018-02-11 stsp err = got_diff_tree(tree, NULL, repo, outfile);
350 2c56f2ce 2017-11-30 stsp
351 2c56f2ce 2017-11-30 stsp done:
352 2c56f2ce 2017-11-30 stsp if (tree)
353 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
354 2c56f2ce 2017-11-30 stsp if (treeobj)
355 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
356 2c56f2ce 2017-11-30 stsp return err;
357 474b4f94 2017-11-30 stsp }
358 474b4f94 2017-11-30 stsp
359 474b4f94 2017-11-30 stsp static const struct got_error *
360 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
361 74671950 2018-02-11 stsp FILE *outfile)
362 474b4f94 2017-11-30 stsp {
363 013404a9 2017-11-30 stsp /* XXX TODO */
364 474b4f94 2017-11-30 stsp return NULL;
365 474b4f94 2017-11-30 stsp }
366 474b4f94 2017-11-30 stsp
367 474b4f94 2017-11-30 stsp static const struct got_error *
368 6a213ccb 2017-11-30 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
369 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
370 474b4f94 2017-11-30 stsp {
371 07ccb00b 2018-03-27 stsp struct got_tree_entry *te2 = NULL;
372 474b4f94 2017-11-30 stsp
373 07ccb00b 2018-03-27 stsp if (tree2)
374 07ccb00b 2018-03-27 stsp te2 = match_entry_by_name(te1, tree2);
375 474b4f94 2017-11-30 stsp if (te2 == NULL) {
376 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
377 59ece79d 2018-02-12 stsp return diff_deleted_tree(te1->id, repo, outfile);
378 59ece79d 2018-02-12 stsp return diff_deleted_blob(te1->id, repo, outfile);
379 474b4f94 2017-11-30 stsp }
380 474b4f94 2017-11-30 stsp
381 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
382 59ece79d 2018-02-12 stsp if (got_object_id_cmp(te1->id, te2->id) != 0)
383 59ece79d 2018-02-12 stsp return diff_modified_tree(te1->id, te2->id, repo,
384 74671950 2018-02-11 stsp outfile);
385 4209f790 2017-11-30 stsp } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
386 59ece79d 2018-02-12 stsp if (got_object_id_cmp(te1->id, te2->id) != 0)
387 59ece79d 2018-02-12 stsp return diff_modified_blob(te1->id, te2->id, repo,
388 74671950 2018-02-11 stsp outfile);
389 413ea19d 2017-11-30 stsp }
390 474b4f94 2017-11-30 stsp
391 59ece79d 2018-02-12 stsp return diff_kind_mismatch(te1->id, te2->id, outfile);
392 474b4f94 2017-11-30 stsp }
393 474b4f94 2017-11-30 stsp
394 474b4f94 2017-11-30 stsp static const struct got_error *
395 4e22badc 2017-11-30 stsp diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
396 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
397 474b4f94 2017-11-30 stsp {
398 07ccb00b 2018-03-27 stsp if (tree1) {
399 07ccb00b 2018-03-27 stsp struct got_tree_entry *te1 = match_entry_by_name(te2, tree1);
400 07ccb00b 2018-03-27 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
401 07ccb00b 2018-03-27 stsp return NULL;
402 07ccb00b 2018-03-27 stsp }
403 474b4f94 2017-11-30 stsp
404 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
405 59ece79d 2018-02-12 stsp return diff_added_tree(te2->id, repo, outfile);
406 59ece79d 2018-02-12 stsp return diff_added_blob(te2->id, repo, outfile);
407 474b4f94 2017-11-30 stsp }
408 474b4f94 2017-11-30 stsp
409 474b4f94 2017-11-30 stsp const struct got_error *
410 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
411 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
412 474b4f94 2017-11-30 stsp {
413 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
414 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
415 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
416 474b4f94 2017-11-30 stsp
417 789689b5 2017-11-30 stsp if (tree1)
418 789689b5 2017-11-30 stsp te1 = SIMPLEQ_FIRST(&tree1->entries);
419 789689b5 2017-11-30 stsp if (tree2)
420 789689b5 2017-11-30 stsp te2 = SIMPLEQ_FIRST(&tree2->entries);
421 474b4f94 2017-11-30 stsp
422 474b4f94 2017-11-30 stsp do {
423 474b4f94 2017-11-30 stsp if (te1) {
424 74671950 2018-02-11 stsp err = diff_entry_old_new(te1, tree2, repo, outfile);
425 474b4f94 2017-11-30 stsp if (err)
426 474b4f94 2017-11-30 stsp break;
427 474b4f94 2017-11-30 stsp }
428 474b4f94 2017-11-30 stsp
429 474b4f94 2017-11-30 stsp if (te2) {
430 74671950 2018-02-11 stsp err = diff_entry_new_old(te2, tree1, repo, outfile);
431 474b4f94 2017-11-30 stsp if (err)
432 474b4f94 2017-11-30 stsp break;
433 474b4f94 2017-11-30 stsp }
434 474b4f94 2017-11-30 stsp
435 474b4f94 2017-11-30 stsp if (te1)
436 474b4f94 2017-11-30 stsp te1 = SIMPLEQ_NEXT(te1, entry);
437 474b4f94 2017-11-30 stsp if (te2)
438 474b4f94 2017-11-30 stsp te2 = SIMPLEQ_NEXT(te2, entry);
439 474b4f94 2017-11-30 stsp } while (te1 || te2);
440 11528a82 2018-05-19 stsp
441 11528a82 2018-05-19 stsp return err;
442 11528a82 2018-05-19 stsp }
443 11528a82 2018-05-19 stsp
444 11528a82 2018-05-19 stsp const struct got_error *
445 11528a82 2018-05-19 stsp got_diff_objects_as_blobs(struct got_object *obj1, struct got_object *obj2,
446 11528a82 2018-05-19 stsp struct got_repository *repo, FILE *outfile)
447 11528a82 2018-05-19 stsp {
448 11528a82 2018-05-19 stsp const struct got_error *err;
449 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
450 b74c7625 2018-05-20 stsp
451 b74c7625 2018-05-20 stsp if (obj1 == NULL && obj2 == NULL)
452 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
453 11528a82 2018-05-19 stsp
454 cd0acaa7 2018-05-20 stsp if (obj1) {
455 cd0acaa7 2018-05-20 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
456 cd0acaa7 2018-05-20 stsp if (err)
457 cd0acaa7 2018-05-20 stsp goto done;
458 cd0acaa7 2018-05-20 stsp }
459 cd0acaa7 2018-05-20 stsp if (obj2) {
460 cd0acaa7 2018-05-20 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
461 cd0acaa7 2018-05-20 stsp if (err)
462 cd0acaa7 2018-05-20 stsp goto done;
463 cd0acaa7 2018-05-20 stsp }
464 11528a82 2018-05-19 stsp err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
465 11528a82 2018-05-19 stsp done:
466 11528a82 2018-05-19 stsp if (blob1)
467 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
468 11528a82 2018-05-19 stsp if (blob2)
469 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
470 474b4f94 2017-11-30 stsp return err;
471 474b4f94 2017-11-30 stsp }
472 11528a82 2018-05-19 stsp
473 11528a82 2018-05-19 stsp const struct got_error *
474 11528a82 2018-05-19 stsp got_diff_objects_as_trees(struct got_object *obj1, struct got_object *obj2,
475 11528a82 2018-05-19 stsp struct got_repository *repo, FILE *outfile)
476 11528a82 2018-05-19 stsp {
477 11528a82 2018-05-19 stsp const struct got_error *err;
478 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
479 11528a82 2018-05-19 stsp
480 b74c7625 2018-05-20 stsp if (obj1 == NULL && obj2 == NULL)
481 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
482 b74c7625 2018-05-20 stsp
483 cd0acaa7 2018-05-20 stsp if (obj1) {
484 cd0acaa7 2018-05-20 stsp err = got_object_tree_open(&tree1, repo, obj1);
485 cd0acaa7 2018-05-20 stsp if (err)
486 cd0acaa7 2018-05-20 stsp goto done;
487 cd0acaa7 2018-05-20 stsp }
488 cd0acaa7 2018-05-20 stsp if (obj2) {
489 cd0acaa7 2018-05-20 stsp err = got_object_tree_open(&tree2, repo, obj2);
490 cd0acaa7 2018-05-20 stsp if (err)
491 cd0acaa7 2018-05-20 stsp goto done;
492 cd0acaa7 2018-05-20 stsp }
493 11528a82 2018-05-19 stsp err = got_diff_tree(tree1, tree2, repo, outfile);
494 11528a82 2018-05-19 stsp done:
495 11528a82 2018-05-19 stsp if (tree1)
496 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
497 11528a82 2018-05-19 stsp if (tree2)
498 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
499 11528a82 2018-05-19 stsp return err;
500 11528a82 2018-05-19 stsp }
501 11528a82 2018-05-19 stsp
502 11528a82 2018-05-19 stsp const struct got_error *
503 11528a82 2018-05-19 stsp got_diff_objects_as_commits(struct got_object *obj1, struct got_object *obj2,
504 11528a82 2018-05-19 stsp struct got_repository *repo, FILE *outfile)
505 11528a82 2018-05-19 stsp {
506 11528a82 2018-05-19 stsp const struct got_error *err;
507 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
508 11528a82 2018-05-19 stsp struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL;
509 9b697879 2018-05-20 stsp char *id_str;
510 11528a82 2018-05-19 stsp
511 9b697879 2018-05-20 stsp if (obj2 == NULL)
512 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
513 b74c7625 2018-05-20 stsp
514 cd0acaa7 2018-05-20 stsp if (obj1) {
515 cd0acaa7 2018-05-20 stsp err = got_object_commit_open(&commit1, repo, obj1);
516 cd0acaa7 2018-05-20 stsp if (err)
517 cd0acaa7 2018-05-20 stsp goto done;
518 cd0acaa7 2018-05-20 stsp err = got_object_open(&tree_obj1, repo, commit1->tree_id);
519 cd0acaa7 2018-05-20 stsp if (err)
520 cd0acaa7 2018-05-20 stsp goto done;
521 cd0acaa7 2018-05-20 stsp }
522 bacc9935 2018-05-20 stsp
523 9b697879 2018-05-20 stsp err = got_object_commit_open(&commit2, repo, obj2);
524 9b697879 2018-05-20 stsp if (err)
525 9b697879 2018-05-20 stsp goto done;
526 9b697879 2018-05-20 stsp err = got_object_open(&tree_obj2, repo, commit2->tree_id);
527 9b697879 2018-05-20 stsp if (err)
528 9b697879 2018-05-20 stsp goto done;
529 9b697879 2018-05-20 stsp err = got_object_get_id_str(&id_str, obj2);
530 9b697879 2018-05-20 stsp if (err)
531 9b697879 2018-05-20 stsp goto done;
532 9fc8d6a2 2018-05-20 stsp if (fprintf(outfile, "commit: %s\n", id_str) < 0) {
533 9fc8d6a2 2018-05-20 stsp err = got_error_from_errno();
534 9fc8d6a2 2018-05-20 stsp free(id_str);
535 9fc8d6a2 2018-05-20 stsp goto done;
536 9fc8d6a2 2018-05-20 stsp }
537 9b697879 2018-05-20 stsp free(id_str);
538 9fc8d6a2 2018-05-20 stsp if (fprintf(outfile, "author: %s\n", commit2->author) < 0) {
539 9fc8d6a2 2018-05-20 stsp err = got_error_from_errno();
540 9fc8d6a2 2018-05-20 stsp goto done;
541 9fc8d6a2 2018-05-20 stsp }
542 9fc8d6a2 2018-05-20 stsp if (strcmp(commit2->author, commit2->committer) != 0 &&
543 9fc8d6a2 2018-05-20 stsp fprintf(outfile, "committer: %s\n", commit2->committer) < 0) {
544 9fc8d6a2 2018-05-20 stsp err = got_error_from_errno();
545 9fc8d6a2 2018-05-20 stsp goto done;
546 9fc8d6a2 2018-05-20 stsp }
547 9fc8d6a2 2018-05-20 stsp if (fprintf(outfile, "\n%s\n", commit2->logmsg) < 0) {
548 9fc8d6a2 2018-05-20 stsp err = got_error_from_errno();
549 9fc8d6a2 2018-05-20 stsp goto done;
550 9fc8d6a2 2018-05-20 stsp }
551 9b697879 2018-05-20 stsp
552 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(tree_obj1, tree_obj2, repo, outfile);
553 11528a82 2018-05-19 stsp done:
554 11528a82 2018-05-19 stsp if (tree_obj1)
555 11528a82 2018-05-19 stsp got_object_close(tree_obj1);
556 11528a82 2018-05-19 stsp if (tree_obj2)
557 11528a82 2018-05-19 stsp got_object_close(tree_obj2);
558 11528a82 2018-05-19 stsp if (commit1)
559 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
560 11528a82 2018-05-19 stsp if (commit2)
561 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
562 11528a82 2018-05-19 stsp return err;
563 11528a82 2018-05-19 stsp }