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