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