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