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 84451b3e 2018-07-10 stsp err = got_object_blob_dump_to_file(&size1, NULL, 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 84451b3e 2018-07-10 stsp err = got_object_blob_dump_to_file(&size2, NULL, 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 3b8ef1a8 2018-09-13 stsp fprintf(outfile, "blob - %s\n", idstr1);
105 3b8ef1a8 2018-09-13 stsp fprintf(outfile, "blob + %s\n", idstr2);
106 8f97f261 2018-09-13 stsp
107 404c43c4 2018-06-21 stsp err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
108 7d283eee 2017-11-29 stsp done:
109 98abbc84 2017-11-30 stsp if (f1)
110 98abbc84 2017-11-30 stsp fclose(f1);
111 98abbc84 2017-11-30 stsp if (f2)
112 98abbc84 2017-11-30 stsp fclose(f2);
113 7d283eee 2017-11-29 stsp return err;
114 7d283eee 2017-11-29 stsp }
115 474b4f94 2017-11-30 stsp
116 404c43c4 2018-06-21 stsp const struct got_error *
117 404c43c4 2018-06-21 stsp got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
118 404c43c4 2018-06-21 stsp const char *label1, const char *label2, FILE *outfile)
119 404c43c4 2018-06-21 stsp {
120 404c43c4 2018-06-21 stsp return diff_blobs(blob1, blob2, label1, label2, outfile, NULL);
121 404c43c4 2018-06-21 stsp }
122 404c43c4 2018-06-21 stsp
123 404c43c4 2018-06-21 stsp const struct got_error *
124 404c43c4 2018-06-21 stsp got_diff_blob_lines_changed(struct got_diff_changes **changes,
125 404c43c4 2018-06-21 stsp struct got_blob_object *blob1, struct got_blob_object *blob2)
126 404c43c4 2018-06-21 stsp {
127 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
128 404c43c4 2018-06-21 stsp
129 404c43c4 2018-06-21 stsp *changes = calloc(1, sizeof(**changes));
130 404c43c4 2018-06-21 stsp if (*changes == NULL)
131 404c43c4 2018-06-21 stsp return got_error_from_errno();
132 404c43c4 2018-06-21 stsp SIMPLEQ_INIT(&(*changes)->entries);
133 404c43c4 2018-06-21 stsp
134 404c43c4 2018-06-21 stsp err = diff_blobs(blob1, blob2, NULL, NULL, NULL, *changes);
135 404c43c4 2018-06-21 stsp if (err) {
136 404c43c4 2018-06-21 stsp got_diff_free_changes(*changes);
137 404c43c4 2018-06-21 stsp *changes = NULL;
138 404c43c4 2018-06-21 stsp }
139 404c43c4 2018-06-21 stsp return err;
140 404c43c4 2018-06-21 stsp }
141 404c43c4 2018-06-21 stsp
142 404c43c4 2018-06-21 stsp void
143 404c43c4 2018-06-21 stsp got_diff_free_changes(struct got_diff_changes *changes)
144 404c43c4 2018-06-21 stsp {
145 404c43c4 2018-06-21 stsp struct got_diff_change *change;
146 404c43c4 2018-06-21 stsp while (!SIMPLEQ_EMPTY(&changes->entries)) {
147 404c43c4 2018-06-21 stsp change = SIMPLEQ_FIRST(&changes->entries);
148 404c43c4 2018-06-21 stsp SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
149 404c43c4 2018-06-21 stsp free(change);
150 404c43c4 2018-06-21 stsp }
151 404c43c4 2018-06-21 stsp free(changes);
152 404c43c4 2018-06-21 stsp }
153 404c43c4 2018-06-21 stsp
154 a3e2cbea 2017-12-01 stsp struct got_tree_entry *
155 a3e2cbea 2017-12-01 stsp match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
156 474b4f94 2017-11-30 stsp {
157 a3e2cbea 2017-12-01 stsp struct got_tree_entry *te2;
158 883f0469 2018-06-23 stsp const struct got_tree_entries *entries2;
159 a3e2cbea 2017-12-01 stsp
160 883f0469 2018-06-23 stsp entries2 = got_object_tree_get_entries(tree2);
161 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te2, &entries2->head, entry) {
162 a3e2cbea 2017-12-01 stsp if (strcmp(te1->name, te2->name) == 0)
163 a3e2cbea 2017-12-01 stsp return te2;
164 a3e2cbea 2017-12-01 stsp }
165 474b4f94 2017-11-30 stsp return NULL;
166 474b4f94 2017-11-30 stsp }
167 474b4f94 2017-11-30 stsp
168 474b4f94 2017-11-30 stsp static const struct got_error *
169 f6861a81 2018-09-13 stsp diff_added_blob(struct got_object_id *id, const char *label,
170 f6861a81 2018-09-13 stsp struct got_repository *repo, FILE *outfile)
171 474b4f94 2017-11-30 stsp {
172 4e22badc 2017-11-30 stsp const struct got_error *err;
173 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
174 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
175 4e22badc 2017-11-30 stsp
176 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
177 4e22badc 2017-11-30 stsp if (err)
178 4e22badc 2017-11-30 stsp return err;
179 4e22badc 2017-11-30 stsp
180 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
181 2acfca77 2018-04-01 stsp if (err)
182 2acfca77 2018-04-01 stsp goto done;
183 f6861a81 2018-09-13 stsp err = got_diff_blob(NULL, blob, NULL, label, outfile);
184 2acfca77 2018-04-01 stsp done:
185 2acfca77 2018-04-01 stsp got_object_close(obj);
186 2acfca77 2018-04-01 stsp if (blob)
187 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
188 2acfca77 2018-04-01 stsp return err;
189 474b4f94 2017-11-30 stsp }
190 474b4f94 2017-11-30 stsp
191 474b4f94 2017-11-30 stsp static const struct got_error *
192 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
193 f6861a81 2018-09-13 stsp const char *label1, const char *label2, struct got_repository *repo,
194 f6861a81 2018-09-13 stsp FILE *outfile)
195 474b4f94 2017-11-30 stsp {
196 6a213ccb 2017-11-30 stsp const struct got_error *err;
197 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
198 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
199 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
200 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
201 6a213ccb 2017-11-30 stsp
202 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
203 6a213ccb 2017-11-30 stsp if (err)
204 730a8aa0 2018-04-24 stsp return err;
205 eef6493a 2018-01-19 stsp if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) {
206 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
207 6a213ccb 2017-11-30 stsp goto done;
208 6a213ccb 2017-11-30 stsp }
209 6a213ccb 2017-11-30 stsp
210 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
211 730a8aa0 2018-04-24 stsp if (err)
212 6a213ccb 2017-11-30 stsp goto done;
213 eef6493a 2018-01-19 stsp if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) {
214 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
215 6a213ccb 2017-11-30 stsp goto done;
216 6a213ccb 2017-11-30 stsp }
217 6a213ccb 2017-11-30 stsp
218 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
219 2acfca77 2018-04-01 stsp if (err)
220 6a213ccb 2017-11-30 stsp goto done;
221 6a213ccb 2017-11-30 stsp
222 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
223 2acfca77 2018-04-01 stsp if (err)
224 6a213ccb 2017-11-30 stsp goto done;
225 6a213ccb 2017-11-30 stsp
226 f6861a81 2018-09-13 stsp err = got_diff_blob(blob1, blob2, label1, label2, outfile);
227 6a213ccb 2017-11-30 stsp
228 6a213ccb 2017-11-30 stsp done:
229 a3e2cbea 2017-12-01 stsp if (obj1)
230 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
231 a3e2cbea 2017-12-01 stsp if (obj2)
232 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
233 a3e2cbea 2017-12-01 stsp if (blob1)
234 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
235 a3e2cbea 2017-12-01 stsp if (blob2)
236 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
237 6a213ccb 2017-11-30 stsp return err;
238 474b4f94 2017-11-30 stsp }
239 474b4f94 2017-11-30 stsp
240 474b4f94 2017-11-30 stsp static const struct got_error *
241 f6861a81 2018-09-13 stsp diff_deleted_blob(struct got_object_id *id, const char *label,
242 f6861a81 2018-09-13 stsp struct got_repository *repo, FILE *outfile)
243 474b4f94 2017-11-30 stsp {
244 365fb436 2017-11-30 stsp const struct got_error *err;
245 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
246 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
247 365fb436 2017-11-30 stsp
248 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
249 365fb436 2017-11-30 stsp if (err)
250 365fb436 2017-11-30 stsp return err;
251 365fb436 2017-11-30 stsp
252 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
253 2acfca77 2018-04-01 stsp if (err)
254 2acfca77 2018-04-01 stsp goto done;
255 f6861a81 2018-09-13 stsp err = got_diff_blob(blob, NULL, label, NULL, outfile);
256 2acfca77 2018-04-01 stsp done:
257 2acfca77 2018-04-01 stsp got_object_close(obj);
258 2acfca77 2018-04-01 stsp if (blob)
259 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
260 2acfca77 2018-04-01 stsp return err;
261 474b4f94 2017-11-30 stsp }
262 474b4f94 2017-11-30 stsp
263 474b4f94 2017-11-30 stsp static const struct got_error *
264 f6861a81 2018-09-13 stsp diff_added_tree(struct got_object_id *id, const char *label,
265 f6861a81 2018-09-13 stsp struct got_repository *repo, FILE *outfile)
266 474b4f94 2017-11-30 stsp {
267 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
268 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
269 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
270 9c70d4c3 2017-11-30 stsp
271 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
272 9c70d4c3 2017-11-30 stsp if (err)
273 9c70d4c3 2017-11-30 stsp goto done;
274 9c70d4c3 2017-11-30 stsp
275 b107e67f 2018-01-19 stsp if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
276 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
277 9c70d4c3 2017-11-30 stsp goto done;
278 9c70d4c3 2017-11-30 stsp }
279 9c70d4c3 2017-11-30 stsp
280 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
281 9c70d4c3 2017-11-30 stsp if (err)
282 9c70d4c3 2017-11-30 stsp goto done;
283 9c70d4c3 2017-11-30 stsp
284 f6861a81 2018-09-13 stsp err = got_diff_tree(NULL, tree, NULL, label, repo, outfile);
285 9c70d4c3 2017-11-30 stsp
286 9c70d4c3 2017-11-30 stsp done:
287 9c70d4c3 2017-11-30 stsp if (tree)
288 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
289 9c70d4c3 2017-11-30 stsp if (treeobj)
290 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
291 9c70d4c3 2017-11-30 stsp return err;
292 474b4f94 2017-11-30 stsp }
293 474b4f94 2017-11-30 stsp
294 474b4f94 2017-11-30 stsp static const struct got_error *
295 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
296 f6861a81 2018-09-13 stsp const char *label1, const char *label2, struct got_repository *repo,
297 f6861a81 2018-09-13 stsp FILE *outfile)
298 474b4f94 2017-11-30 stsp {
299 f6861a81 2018-09-13 stsp const struct got_error *err;
300 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
301 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
302 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
303 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
304 789689b5 2017-11-30 stsp
305 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
306 789689b5 2017-11-30 stsp if (err)
307 789689b5 2017-11-30 stsp goto done;
308 789689b5 2017-11-30 stsp
309 eef6493a 2018-01-19 stsp if (got_object_get_type(treeobj1) != GOT_OBJ_TYPE_TREE) {
310 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
311 789689b5 2017-11-30 stsp goto done;
312 789689b5 2017-11-30 stsp }
313 789689b5 2017-11-30 stsp
314 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
315 789689b5 2017-11-30 stsp if (err)
316 789689b5 2017-11-30 stsp goto done;
317 789689b5 2017-11-30 stsp
318 eef6493a 2018-01-19 stsp if (got_object_get_type(treeobj2) != GOT_OBJ_TYPE_TREE) {
319 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
320 789689b5 2017-11-30 stsp goto done;
321 789689b5 2017-11-30 stsp }
322 789689b5 2017-11-30 stsp
323 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
324 789689b5 2017-11-30 stsp if (err)
325 789689b5 2017-11-30 stsp goto done;
326 789689b5 2017-11-30 stsp
327 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
328 789689b5 2017-11-30 stsp if (err)
329 789689b5 2017-11-30 stsp goto done;
330 789689b5 2017-11-30 stsp
331 f6861a81 2018-09-13 stsp err = got_diff_tree(tree1, tree2, label1, label2, repo, outfile);
332 789689b5 2017-11-30 stsp
333 789689b5 2017-11-30 stsp done:
334 789689b5 2017-11-30 stsp if (tree1)
335 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
336 789689b5 2017-11-30 stsp if (tree2)
337 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
338 789689b5 2017-11-30 stsp if (treeobj1)
339 789689b5 2017-11-30 stsp got_object_close(treeobj1);
340 789689b5 2017-11-30 stsp if (treeobj2)
341 789689b5 2017-11-30 stsp got_object_close(treeobj2);
342 789689b5 2017-11-30 stsp return err;
343 474b4f94 2017-11-30 stsp }
344 474b4f94 2017-11-30 stsp
345 474b4f94 2017-11-30 stsp static const struct got_error *
346 f6861a81 2018-09-13 stsp diff_deleted_tree(struct got_object_id *id, const char *label,
347 f6861a81 2018-09-13 stsp struct got_repository *repo, FILE *outfile)
348 474b4f94 2017-11-30 stsp {
349 f6861a81 2018-09-13 stsp const struct got_error *err;
350 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
351 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
352 2c56f2ce 2017-11-30 stsp
353 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
354 2c56f2ce 2017-11-30 stsp if (err)
355 2c56f2ce 2017-11-30 stsp goto done;
356 2c56f2ce 2017-11-30 stsp
357 b107e67f 2018-01-19 stsp if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
358 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
359 2c56f2ce 2017-11-30 stsp goto done;
360 2c56f2ce 2017-11-30 stsp }
361 2c56f2ce 2017-11-30 stsp
362 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
363 2c56f2ce 2017-11-30 stsp if (err)
364 2c56f2ce 2017-11-30 stsp goto done;
365 2c56f2ce 2017-11-30 stsp
366 f6861a81 2018-09-13 stsp err = got_diff_tree(tree, NULL, label, NULL, repo, outfile);
367 2c56f2ce 2017-11-30 stsp done:
368 2c56f2ce 2017-11-30 stsp if (tree)
369 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
370 2c56f2ce 2017-11-30 stsp if (treeobj)
371 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
372 2c56f2ce 2017-11-30 stsp return err;
373 474b4f94 2017-11-30 stsp }
374 474b4f94 2017-11-30 stsp
375 474b4f94 2017-11-30 stsp static const struct got_error *
376 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
377 f6861a81 2018-09-13 stsp const char *label1, const char *label2, FILE *outfile)
378 474b4f94 2017-11-30 stsp {
379 013404a9 2017-11-30 stsp /* XXX TODO */
380 474b4f94 2017-11-30 stsp return NULL;
381 474b4f94 2017-11-30 stsp }
382 474b4f94 2017-11-30 stsp
383 474b4f94 2017-11-30 stsp static const struct got_error *
384 f6861a81 2018-09-13 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
385 f6861a81 2018-09-13 stsp const char *label1, const char *label2, struct got_repository *repo,
386 f6861a81 2018-09-13 stsp FILE *outfile)
387 474b4f94 2017-11-30 stsp {
388 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
389 474b4f94 2017-11-30 stsp
390 474b4f94 2017-11-30 stsp if (te2 == NULL) {
391 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
392 f6861a81 2018-09-13 stsp err = diff_deleted_tree(te1->id, label1, repo, outfile);
393 f6861a81 2018-09-13 stsp else
394 f6861a81 2018-09-13 stsp err = diff_deleted_blob(te1->id, label1, repo, outfile);
395 f6861a81 2018-09-13 stsp return err;
396 474b4f94 2017-11-30 stsp }
397 474b4f94 2017-11-30 stsp
398 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
399 59ece79d 2018-02-12 stsp if (got_object_id_cmp(te1->id, te2->id) != 0)
400 f6861a81 2018-09-13 stsp return diff_modified_tree(te1->id, te2->id,
401 f6861a81 2018-09-13 stsp label1, label2, repo, outfile);
402 4209f790 2017-11-30 stsp } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
403 59ece79d 2018-02-12 stsp if (got_object_id_cmp(te1->id, te2->id) != 0)
404 f6861a81 2018-09-13 stsp return diff_modified_blob(te1->id, te2->id,
405 f6861a81 2018-09-13 stsp label1, label2, repo, outfile);
406 413ea19d 2017-11-30 stsp }
407 474b4f94 2017-11-30 stsp
408 f6861a81 2018-09-13 stsp if (got_object_id_cmp(te1->id, te2->id) == 0)
409 f6861a81 2018-09-13 stsp return NULL;
410 f6861a81 2018-09-13 stsp
411 f6861a81 2018-09-13 stsp return diff_kind_mismatch(te1->id, te2->id, label1, label2, outfile);
412 474b4f94 2017-11-30 stsp }
413 474b4f94 2017-11-30 stsp
414 474b4f94 2017-11-30 stsp static const struct got_error *
415 f6861a81 2018-09-13 stsp diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_entry *te1,
416 f6861a81 2018-09-13 stsp const char *label2, struct got_repository *repo, FILE *outfile)
417 474b4f94 2017-11-30 stsp {
418 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
419 f6861a81 2018-09-13 stsp return NULL;
420 474b4f94 2017-11-30 stsp
421 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
422 f6861a81 2018-09-13 stsp return diff_added_tree(te2->id, label2, repo, outfile);
423 f6861a81 2018-09-13 stsp
424 f6861a81 2018-09-13 stsp return diff_added_blob(te2->id, label2, repo, outfile);
425 474b4f94 2017-11-30 stsp }
426 474b4f94 2017-11-30 stsp
427 474b4f94 2017-11-30 stsp const struct got_error *
428 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
429 f6861a81 2018-09-13 stsp const char *label1, const char *label2, struct got_repository *repo,
430 f6861a81 2018-09-13 stsp FILE *outfile)
431 474b4f94 2017-11-30 stsp {
432 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
433 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
434 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
435 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
436 474b4f94 2017-11-30 stsp
437 883f0469 2018-06-23 stsp if (tree1) {
438 883f0469 2018-06-23 stsp const struct got_tree_entries *entries;
439 883f0469 2018-06-23 stsp entries = got_object_tree_get_entries(tree1);
440 883f0469 2018-06-23 stsp te1 = SIMPLEQ_FIRST(&entries->head);
441 f6861a81 2018-09-13 stsp if (asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
442 f6861a81 2018-09-13 stsp te1->name) == -1)
443 f6861a81 2018-09-13 stsp return got_error_from_errno();
444 883f0469 2018-06-23 stsp }
445 883f0469 2018-06-23 stsp if (tree2) {
446 883f0469 2018-06-23 stsp const struct got_tree_entries *entries;
447 883f0469 2018-06-23 stsp entries = got_object_tree_get_entries(tree2);
448 883f0469 2018-06-23 stsp te2 = SIMPLEQ_FIRST(&entries->head);
449 f6861a81 2018-09-13 stsp if (asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
450 f6861a81 2018-09-13 stsp te2->name) == -1)
451 f6861a81 2018-09-13 stsp return got_error_from_errno();
452 883f0469 2018-06-23 stsp }
453 474b4f94 2017-11-30 stsp
454 474b4f94 2017-11-30 stsp do {
455 474b4f94 2017-11-30 stsp if (te1) {
456 f6861a81 2018-09-13 stsp struct got_tree_entry *te = NULL;
457 f6861a81 2018-09-13 stsp if (tree2)
458 f6861a81 2018-09-13 stsp te = match_entry_by_name(te1, tree2);
459 f6861a81 2018-09-13 stsp if (te) {
460 f6861a81 2018-09-13 stsp free(l2);
461 f6861a81 2018-09-13 stsp l2 = NULL;
462 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
463 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
464 f6861a81 2018-09-13 stsp return got_error_from_errno();
465 f6861a81 2018-09-13 stsp }
466 f6861a81 2018-09-13 stsp err = diff_entry_old_new(te1, te, l1, l2, repo,
467 f6861a81 2018-09-13 stsp outfile);
468 474b4f94 2017-11-30 stsp if (err)
469 474b4f94 2017-11-30 stsp break;
470 474b4f94 2017-11-30 stsp }
471 474b4f94 2017-11-30 stsp
472 474b4f94 2017-11-30 stsp if (te2) {
473 f6861a81 2018-09-13 stsp struct got_tree_entry *te = NULL;
474 f6861a81 2018-09-13 stsp if (tree1)
475 f6861a81 2018-09-13 stsp te = match_entry_by_name(te2, tree1);
476 f6861a81 2018-09-13 stsp err = diff_entry_new_old(te2, te, l2, repo, outfile);
477 474b4f94 2017-11-30 stsp if (err)
478 474b4f94 2017-11-30 stsp break;
479 474b4f94 2017-11-30 stsp }
480 474b4f94 2017-11-30 stsp
481 f6861a81 2018-09-13 stsp free(l1);
482 f6861a81 2018-09-13 stsp l1 = NULL;
483 f6861a81 2018-09-13 stsp if (te1) {
484 474b4f94 2017-11-30 stsp te1 = SIMPLEQ_NEXT(te1, entry);
485 f6861a81 2018-09-13 stsp if (te1 &&
486 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
487 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
488 f6861a81 2018-09-13 stsp return got_error_from_errno();
489 f6861a81 2018-09-13 stsp }
490 f6861a81 2018-09-13 stsp free(l2);
491 f6861a81 2018-09-13 stsp l2 = NULL;
492 f6861a81 2018-09-13 stsp if (te2) {
493 474b4f94 2017-11-30 stsp te2 = SIMPLEQ_NEXT(te2, entry);
494 f6861a81 2018-09-13 stsp if (te2 &&
495 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
496 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
497 f6861a81 2018-09-13 stsp return got_error_from_errno();
498 f6861a81 2018-09-13 stsp }
499 474b4f94 2017-11-30 stsp } while (te1 || te2);
500 11528a82 2018-05-19 stsp
501 11528a82 2018-05-19 stsp return err;
502 11528a82 2018-05-19 stsp }
503 11528a82 2018-05-19 stsp
504 11528a82 2018-05-19 stsp const struct got_error *
505 11528a82 2018-05-19 stsp got_diff_objects_as_blobs(struct got_object *obj1, struct got_object *obj2,
506 f6861a81 2018-09-13 stsp const char *label1, const char *label2, struct got_repository *repo,
507 f6861a81 2018-09-13 stsp FILE *outfile)
508 11528a82 2018-05-19 stsp {
509 11528a82 2018-05-19 stsp const struct got_error *err;
510 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
511 b74c7625 2018-05-20 stsp
512 b74c7625 2018-05-20 stsp if (obj1 == NULL && obj2 == NULL)
513 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
514 11528a82 2018-05-19 stsp
515 cd0acaa7 2018-05-20 stsp if (obj1) {
516 cd0acaa7 2018-05-20 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
517 cd0acaa7 2018-05-20 stsp if (err)
518 cd0acaa7 2018-05-20 stsp goto done;
519 cd0acaa7 2018-05-20 stsp }
520 cd0acaa7 2018-05-20 stsp if (obj2) {
521 cd0acaa7 2018-05-20 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
522 cd0acaa7 2018-05-20 stsp if (err)
523 cd0acaa7 2018-05-20 stsp goto done;
524 cd0acaa7 2018-05-20 stsp }
525 f6861a81 2018-09-13 stsp err = got_diff_blob(blob1, blob2, label1, label2, outfile);
526 11528a82 2018-05-19 stsp done:
527 11528a82 2018-05-19 stsp if (blob1)
528 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
529 11528a82 2018-05-19 stsp if (blob2)
530 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
531 474b4f94 2017-11-30 stsp return err;
532 474b4f94 2017-11-30 stsp }
533 11528a82 2018-05-19 stsp
534 11528a82 2018-05-19 stsp const struct got_error *
535 11528a82 2018-05-19 stsp got_diff_objects_as_trees(struct got_object *obj1, struct got_object *obj2,
536 f6861a81 2018-09-13 stsp char *label1, char *label2, struct got_repository *repo, FILE *outfile)
537 11528a82 2018-05-19 stsp {
538 11528a82 2018-05-19 stsp const struct got_error *err;
539 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
540 11528a82 2018-05-19 stsp
541 b74c7625 2018-05-20 stsp if (obj1 == NULL && obj2 == NULL)
542 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
543 b74c7625 2018-05-20 stsp
544 cd0acaa7 2018-05-20 stsp if (obj1) {
545 cd0acaa7 2018-05-20 stsp err = got_object_tree_open(&tree1, repo, obj1);
546 cd0acaa7 2018-05-20 stsp if (err)
547 cd0acaa7 2018-05-20 stsp goto done;
548 cd0acaa7 2018-05-20 stsp }
549 cd0acaa7 2018-05-20 stsp if (obj2) {
550 cd0acaa7 2018-05-20 stsp err = got_object_tree_open(&tree2, repo, obj2);
551 cd0acaa7 2018-05-20 stsp if (err)
552 cd0acaa7 2018-05-20 stsp goto done;
553 cd0acaa7 2018-05-20 stsp }
554 f6861a81 2018-09-13 stsp err = got_diff_tree(tree1, tree2, label1, label2, repo, outfile);
555 11528a82 2018-05-19 stsp done:
556 11528a82 2018-05-19 stsp if (tree1)
557 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
558 11528a82 2018-05-19 stsp if (tree2)
559 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
560 11528a82 2018-05-19 stsp return err;
561 4bb494d5 2018-06-16 stsp }
562 4bb494d5 2018-06-16 stsp
563 4bb494d5 2018-06-16 stsp static char *
564 4bb494d5 2018-06-16 stsp get_datestr(time_t *time, char *datebuf)
565 4bb494d5 2018-06-16 stsp {
566 4bb494d5 2018-06-16 stsp char *p, *s = ctime_r(time, datebuf);
567 4bb494d5 2018-06-16 stsp p = strchr(s, '\n');
568 4bb494d5 2018-06-16 stsp if (p)
569 4bb494d5 2018-06-16 stsp *p = '\0';
570 4bb494d5 2018-06-16 stsp return s;
571 11528a82 2018-05-19 stsp }
572 11528a82 2018-05-19 stsp
573 11528a82 2018-05-19 stsp const struct got_error *
574 11528a82 2018-05-19 stsp got_diff_objects_as_commits(struct got_object *obj1, struct got_object *obj2,
575 11528a82 2018-05-19 stsp struct got_repository *repo, FILE *outfile)
576 11528a82 2018-05-19 stsp {
577 11528a82 2018-05-19 stsp const struct got_error *err;
578 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
579 11528a82 2018-05-19 stsp struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL;
580 9b697879 2018-05-20 stsp char *id_str;
581 4bb494d5 2018-06-16 stsp char datebuf[26];
582 4bb494d5 2018-06-16 stsp time_t time;
583 11528a82 2018-05-19 stsp
584 9b697879 2018-05-20 stsp if (obj2 == NULL)
585 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
586 b74c7625 2018-05-20 stsp
587 cd0acaa7 2018-05-20 stsp if (obj1) {
588 cd0acaa7 2018-05-20 stsp err = got_object_commit_open(&commit1, repo, obj1);
589 cd0acaa7 2018-05-20 stsp if (err)
590 cd0acaa7 2018-05-20 stsp goto done;
591 cd0acaa7 2018-05-20 stsp err = got_object_open(&tree_obj1, repo, commit1->tree_id);
592 cd0acaa7 2018-05-20 stsp if (err)
593 cd0acaa7 2018-05-20 stsp goto done;
594 cd0acaa7 2018-05-20 stsp }
595 bacc9935 2018-05-20 stsp
596 9b697879 2018-05-20 stsp err = got_object_commit_open(&commit2, repo, obj2);
597 9b697879 2018-05-20 stsp if (err)
598 9b697879 2018-05-20 stsp goto done;
599 9b697879 2018-05-20 stsp err = got_object_open(&tree_obj2, repo, commit2->tree_id);
600 9b697879 2018-05-20 stsp if (err)
601 9b697879 2018-05-20 stsp goto done;
602 9b697879 2018-05-20 stsp err = got_object_get_id_str(&id_str, obj2);
603 9b697879 2018-05-20 stsp if (err)
604 9b697879 2018-05-20 stsp goto done;
605 9fc8d6a2 2018-05-20 stsp if (fprintf(outfile, "commit: %s\n", id_str) < 0) {
606 9fc8d6a2 2018-05-20 stsp err = got_error_from_errno();
607 9fc8d6a2 2018-05-20 stsp free(id_str);
608 9fc8d6a2 2018-05-20 stsp goto done;
609 9fc8d6a2 2018-05-20 stsp }
610 9b697879 2018-05-20 stsp free(id_str);
611 4bb494d5 2018-06-16 stsp time = mktime(&commit2->tm_author);
612 4bb494d5 2018-06-16 stsp if (fprintf(outfile, "author: %s %s UTC\n", commit2->author,
613 4bb494d5 2018-06-16 stsp get_datestr(&time, datebuf)) < 0) {
614 9fc8d6a2 2018-05-20 stsp err = got_error_from_errno();
615 9fc8d6a2 2018-05-20 stsp goto done;
616 9fc8d6a2 2018-05-20 stsp }
617 4bb494d5 2018-06-16 stsp time = mktime(&commit2->tm_committer);
618 9fc8d6a2 2018-05-20 stsp if (strcmp(commit2->author, commit2->committer) != 0 &&
619 4bb494d5 2018-06-16 stsp fprintf(outfile, "committer: %s %s UTC\n", commit2->committer,
620 4bb494d5 2018-06-16 stsp get_datestr(&time, datebuf)) < 0) {
621 9fc8d6a2 2018-05-20 stsp err = got_error_from_errno();
622 9fc8d6a2 2018-05-20 stsp goto done;
623 9fc8d6a2 2018-05-20 stsp }
624 ede67fd9 2018-07-10 stsp if (fprintf(outfile, "%s\n", commit2->logmsg) < 0) {
625 9fc8d6a2 2018-05-20 stsp err = got_error_from_errno();
626 9fc8d6a2 2018-05-20 stsp goto done;
627 9fc8d6a2 2018-05-20 stsp }
628 9b697879 2018-05-20 stsp
629 f6861a81 2018-09-13 stsp err = got_diff_objects_as_trees(tree_obj1, tree_obj2, "", "", repo,
630 f6861a81 2018-09-13 stsp outfile);
631 11528a82 2018-05-19 stsp done:
632 11528a82 2018-05-19 stsp if (tree_obj1)
633 11528a82 2018-05-19 stsp got_object_close(tree_obj1);
634 11528a82 2018-05-19 stsp if (tree_obj2)
635 11528a82 2018-05-19 stsp got_object_close(tree_obj2);
636 11528a82 2018-05-19 stsp if (commit1)
637 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
638 11528a82 2018-05-19 stsp if (commit2)
639 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
640 11528a82 2018-05-19 stsp return err;
641 11528a82 2018-05-19 stsp }