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