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