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