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