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 8b925c6c 2022-07-16 thomas #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 <zlib.h>
25 7d283eee 2017-11-29 stsp
26 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
27 dd038bc6 2021-09-21 thomas.ad
28 7d283eee 2017-11-29 stsp #include "got_object.h"
29 e09a504c 2019-06-28 stsp #include "got_repository.h"
30 7d283eee 2017-11-29 stsp #include "got_error.h"
31 789689b5 2017-11-30 stsp #include "got_diff.h"
32 324d37e7 2019-05-11 stsp #include "got_path.h"
33 0208f208 2020-05-05 stsp #include "got_cancel.h"
34 0208f208 2020-05-05 stsp #include "got_worktree.h"
35 1758cce7 2022-06-13 thomas #include "got_opentemp.h"
36 7d283eee 2017-11-29 stsp
37 718b3ab0 2018-03-17 stsp #include "got_lib_diff.h"
38 15a94983 2018-12-23 stsp #include "got_lib_delta.h"
39 15a94983 2018-12-23 stsp #include "got_lib_inflate.h"
40 15a94983 2018-12-23 stsp #include "got_lib_object.h"
41 772fcad5 2023-01-07 thomas
42 772fcad5 2023-01-07 thomas #ifndef MAX
43 772fcad5 2023-01-07 thomas #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
44 772fcad5 2023-01-07 thomas #endif
45 a7852263 2017-11-30 stsp
46 404c43c4 2018-06-21 stsp static const struct got_error *
47 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
48 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
49 fe621944 2020-11-10 stsp {
50 82c78e96 2022-08-06 thomas struct got_diff_line *p;
51 fe621944 2020-11-10 stsp
52 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
53 fe621944 2020-11-10 stsp if (p == NULL)
54 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
55 82c78e96 2022-08-06 thomas *lines = p;
56 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
57 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
58 fe621944 2020-11-10 stsp (*nlines)++;
59 82c78e96 2022-08-06 thomas
60 fe621944 2020-11-10 stsp return NULL;
61 fe621944 2020-11-10 stsp }
62 fe621944 2020-11-10 stsp
63 fe621944 2020-11-10 stsp static const struct got_error *
64 82c78e96 2022-08-06 thomas diff_blobs(struct got_diff_line **lines, size_t *nlines,
65 fe621944 2020-11-10 stsp struct got_diffreg_result **resultp, struct got_blob_object *blob1,
66 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
67 46f68b20 2019-10-19 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
68 25ec7006 2022-07-01 thomas int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile,
69 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo)
70 7d283eee 2017-11-29 stsp {
71 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
72 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
73 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
74 0c6f49ba 2022-07-01 thomas const char *idstr1 = NULL, *idstr2 = NULL;
75 be659d10 2020-11-18 stsp off_t size1, size2;
76 a6d62598 2023-01-07 thomas struct got_diffreg_result *result = NULL;
77 fe621944 2020-11-10 stsp off_t outoff = 0;
78 fe621944 2020-11-10 stsp int n;
79 7d283eee 2017-11-29 stsp
80 3387ec82 2022-08-30 thomas if (lines && *lines && *nlines > 0)
81 82c78e96 2022-08-06 thomas outoff = (*lines)[*nlines - 1].offset;
82 82c78e96 2022-08-06 thomas else if (lines) {
83 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
84 9c659ea0 2020-11-22 stsp if (err)
85 9c659ea0 2020-11-22 stsp goto done;
86 9c659ea0 2020-11-22 stsp }
87 fe621944 2020-11-10 stsp
88 fe621944 2020-11-10 stsp if (resultp)
89 fe621944 2020-11-10 stsp *resultp = NULL;
90 fe621944 2020-11-10 stsp
91 a0f32f33 2022-06-13 thomas if (f1) {
92 1758cce7 2022-06-13 thomas err = got_opentemp_truncate(f1);
93 a0f32f33 2022-06-13 thomas if (err)
94 a0f32f33 2022-06-13 thomas goto done;
95 fe621944 2020-11-10 stsp }
96 a0f32f33 2022-06-13 thomas if (f2) {
97 1758cce7 2022-06-13 thomas err = got_opentemp_truncate(f2);
98 a0f32f33 2022-06-13 thomas if (err)
99 a0f32f33 2022-06-13 thomas goto done;
100 fe621944 2020-11-10 stsp }
101 7d283eee 2017-11-29 stsp
102 f934cf2c 2018-02-12 stsp size1 = 0;
103 98abbc84 2017-11-30 stsp if (blob1) {
104 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
105 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
106 6c4c42e0 2019-06-24 stsp blob1);
107 35e9ba5d 2018-06-21 stsp if (err)
108 35e9ba5d 2018-06-21 stsp goto done;
109 98abbc84 2017-11-30 stsp } else
110 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
111 7d283eee 2017-11-29 stsp
112 f934cf2c 2018-02-12 stsp size2 = 0;
113 98abbc84 2017-11-30 stsp if (blob2) {
114 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
115 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
116 6c4c42e0 2019-06-24 stsp blob2);
117 35e9ba5d 2018-06-21 stsp if (err)
118 35e9ba5d 2018-06-21 stsp goto done;
119 98abbc84 2017-11-30 stsp } else
120 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
121 7d283eee 2017-11-29 stsp
122 09de383e 2018-12-24 stsp if (outfile) {
123 46f68b20 2019-10-19 stsp char *modestr1 = NULL, *modestr2 = NULL;
124 40dde666 2020-07-23 stsp int modebits;
125 46f68b20 2019-10-19 stsp if (mode1 && mode1 != mode2) {
126 40dde666 2020-07-23 stsp if (S_ISLNK(mode1))
127 40dde666 2020-07-23 stsp modebits = S_IFLNK;
128 40dde666 2020-07-23 stsp else
129 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
130 46f68b20 2019-10-19 stsp if (asprintf(&modestr1, " (mode %o)",
131 40dde666 2020-07-23 stsp mode1 & modebits) == -1) {
132 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
133 46f68b20 2019-10-19 stsp goto done;
134 46f68b20 2019-10-19 stsp }
135 46f68b20 2019-10-19 stsp }
136 46f68b20 2019-10-19 stsp if (mode2 && mode1 != mode2) {
137 40dde666 2020-07-23 stsp if (S_ISLNK(mode2))
138 40dde666 2020-07-23 stsp modebits = S_IFLNK;
139 40dde666 2020-07-23 stsp else
140 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
141 46f68b20 2019-10-19 stsp if (asprintf(&modestr2, " (mode %o)",
142 40dde666 2020-07-23 stsp mode2 & modebits) == -1) {
143 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
144 46f68b20 2019-10-19 stsp goto done;
145 46f68b20 2019-10-19 stsp }
146 46f68b20 2019-10-19 stsp }
147 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob - %s%s\n", idstr1,
148 46f68b20 2019-10-19 stsp modestr1 ? modestr1 : "");
149 fe621944 2020-11-10 stsp if (n < 0)
150 fe621944 2020-11-10 stsp goto done;
151 fe621944 2020-11-10 stsp outoff += n;
152 82c78e96 2022-08-06 thomas if (lines) {
153 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
154 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_BLOB_MIN);
155 fe621944 2020-11-10 stsp if (err)
156 fe621944 2020-11-10 stsp goto done;
157 fe621944 2020-11-10 stsp }
158 fe621944 2020-11-10 stsp
159 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob + %s%s\n", idstr2,
160 46f68b20 2019-10-19 stsp modestr2 ? modestr2 : "");
161 fe621944 2020-11-10 stsp if (n < 0)
162 fe621944 2020-11-10 stsp goto done;
163 fe621944 2020-11-10 stsp outoff += n;
164 82c78e96 2022-08-06 thomas if (lines) {
165 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
166 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_BLOB_PLUS);
167 fe621944 2020-11-10 stsp if (err)
168 fe621944 2020-11-10 stsp goto done;
169 fe621944 2020-11-10 stsp }
170 fe621944 2020-11-10 stsp
171 46f68b20 2019-10-19 stsp free(modestr1);
172 46f68b20 2019-10-19 stsp free(modestr2);
173 09de383e 2018-12-24 stsp }
174 25ec7006 2022-07-01 thomas err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
175 25ec7006 2022-07-01 thomas force_text_diff);
176 fe621944 2020-11-10 stsp if (err)
177 fe621944 2020-11-10 stsp goto done;
178 fe621944 2020-11-10 stsp
179 fe621944 2020-11-10 stsp if (outfile) {
180 82c78e96 2022-08-06 thomas err = got_diffreg_output(lines, nlines, result,
181 1cb46f00 2020-11-21 stsp blob1 != NULL, blob2 != NULL,
182 fe621944 2020-11-10 stsp label1 ? label1 : idstr1,
183 fe621944 2020-11-10 stsp label2 ? label2 : idstr2,
184 fe621944 2020-11-10 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
185 fe621944 2020-11-10 stsp if (err)
186 fe621944 2020-11-10 stsp goto done;
187 fe621944 2020-11-10 stsp }
188 fe621944 2020-11-10 stsp
189 a6d62598 2023-01-07 thomas done:
190 fe621944 2020-11-10 stsp if (resultp && err == NULL)
191 fe621944 2020-11-10 stsp *resultp = result;
192 a6d62598 2023-01-07 thomas else if (result) {
193 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
194 fe621944 2020-11-10 stsp if (free_err && err == NULL)
195 fe621944 2020-11-10 stsp err = free_err;
196 fe621944 2020-11-10 stsp }
197 a6d62598 2023-01-07 thomas
198 7d283eee 2017-11-29 stsp return err;
199 aaa13589 2019-06-01 stsp }
200 aaa13589 2019-06-01 stsp
201 aaa13589 2019-06-01 stsp const struct got_error *
202 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
203 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
204 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
205 a0f32f33 2022-06-13 thomas const char *label1, const char *label2, mode_t mode1, mode_t mode2,
206 a0f32f33 2022-06-13 thomas struct got_repository *repo)
207 aaa13589 2019-06-01 stsp {
208 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
209 aaa13589 2019-06-01 stsp
210 82c78e96 2022-08-06 thomas return diff_blobs(&a->lines, &a->nlines, NULL,
211 a0f32f33 2022-06-13 thomas blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
212 25ec7006 2022-07-01 thomas a->ignore_whitespace, a->force_text_diff, a->outfile, a->diff_algo);
213 7d283eee 2017-11-29 stsp }
214 474b4f94 2017-11-30 stsp
215 404c43c4 2018-06-21 stsp const struct got_error *
216 82c78e96 2022-08-06 thomas got_diff_blob(struct got_diff_line **lines, size_t*nlines,
217 fe621944 2020-11-10 stsp struct got_blob_object *blob1, struct got_blob_object *blob2,
218 a0f32f33 2022-06-13 thomas FILE *f1, FILE *f2, const char *label1, const char *label2,
219 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context,
220 25ec7006 2022-07-01 thomas int ignore_whitespace, int force_text_diff, FILE *outfile)
221 404c43c4 2018-06-21 stsp {
222 82c78e96 2022-08-06 thomas return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
223 64453f7e 2020-11-21 stsp label1, label2, 0, 0, diff_context, ignore_whitespace,
224 25ec7006 2022-07-01 thomas force_text_diff, outfile, diff_algo);
225 404c43c4 2018-06-21 stsp }
226 404c43c4 2018-06-21 stsp
227 7f1f93af 2019-08-06 stsp static const struct got_error *
228 fe621944 2020-11-10 stsp diff_blob_file(struct got_diffreg_result **resultp,
229 a0f32f33 2022-06-13 thomas struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
230 6d054bb9 2022-09-23 thomas FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
231 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
232 25ec7006 2022-07-01 thomas int force_text_diff, FILE *outfile)
233 b72f483a 2019-02-05 stsp {
234 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
235 b72f483a 2019-02-05 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
236 0c6f49ba 2022-07-01 thomas const char *idstr1 = NULL;
237 fe621944 2020-11-10 stsp struct got_diffreg_result *result = NULL;
238 b72f483a 2019-02-05 stsp
239 fe621944 2020-11-10 stsp if (resultp)
240 fe621944 2020-11-10 stsp *resultp = NULL;
241 7f1f93af 2019-08-06 stsp
242 a0f32f33 2022-06-13 thomas if (blob1)
243 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
244 a0f32f33 2022-06-13 thomas else
245 b72f483a 2019-02-05 stsp idstr1 = "/dev/null";
246 b72f483a 2019-02-05 stsp
247 7f1f93af 2019-08-06 stsp if (outfile) {
248 6d054bb9 2022-09-23 thomas char *mode = NULL;
249 6d054bb9 2022-09-23 thomas
250 6d054bb9 2022-09-23 thomas /* display file mode for new added files only */
251 6d054bb9 2022-09-23 thomas if (f2_exists && blob1 == NULL) {
252 6d054bb9 2022-09-23 thomas int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
253 6d054bb9 2022-09-23 thomas
254 6d054bb9 2022-09-23 thomas if (S_ISLNK(sb2->st_mode))
255 6d054bb9 2022-09-23 thomas mmask = S_IFLNK;
256 6d054bb9 2022-09-23 thomas if (asprintf(&mode, " (mode %o)",
257 6d054bb9 2022-09-23 thomas sb2->st_mode & mmask) == -1)
258 6d054bb9 2022-09-23 thomas return got_error_from_errno("asprintf");
259 6d054bb9 2022-09-23 thomas }
260 4ce46740 2019-08-08 stsp fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
261 6d054bb9 2022-09-23 thomas fprintf(outfile, "file + %s%s\n",
262 6d054bb9 2022-09-23 thomas f2_exists ? label2 : "/dev/null", mode ? mode : "");
263 6d054bb9 2022-09-23 thomas free(mode);
264 7f1f93af 2019-08-06 stsp }
265 fe621944 2020-11-10 stsp
266 25ec7006 2022-07-01 thomas err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
267 25ec7006 2022-07-01 thomas force_text_diff);
268 fe621944 2020-11-10 stsp if (err)
269 fe621944 2020-11-10 stsp goto done;
270 fe621944 2020-11-10 stsp
271 fe621944 2020-11-10 stsp if (outfile) {
272 1cb46f00 2020-11-21 stsp err = got_diffreg_output(NULL, NULL, result,
273 dd2e2f52 2022-07-01 thomas blob1 != NULL, f2_exists,
274 1cb46f00 2020-11-21 stsp label2, /* show local file's path, not a blob ID */
275 1cb46f00 2020-11-21 stsp label2, GOT_DIFF_OUTPUT_UNIDIFF,
276 1cb46f00 2020-11-21 stsp diff_context, outfile);
277 7f1f93af 2019-08-06 stsp if (err)
278 fe621944 2020-11-10 stsp goto done;
279 7f1f93af 2019-08-06 stsp }
280 fe621944 2020-11-10 stsp
281 a6d62598 2023-01-07 thomas done:
282 fe621944 2020-11-10 stsp if (resultp && err == NULL)
283 fe621944 2020-11-10 stsp *resultp = result;
284 fe621944 2020-11-10 stsp else if (result) {
285 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
286 fe621944 2020-11-10 stsp if (free_err && err == NULL)
287 fe621944 2020-11-10 stsp err = free_err;
288 fe621944 2020-11-10 stsp }
289 b72f483a 2019-02-05 stsp return err;
290 7f1f93af 2019-08-06 stsp }
291 7f1f93af 2019-08-06 stsp
292 7f1f93af 2019-08-06 stsp const struct got_error *
293 a0f32f33 2022-06-13 thomas got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
294 6d054bb9 2022-09-23 thomas const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
295 25ec7006 2022-07-01 thomas const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
296 25ec7006 2022-07-01 thomas int ignore_whitespace, int force_text_diff, FILE *outfile)
297 7f1f93af 2019-08-06 stsp {
298 dd2e2f52 2022-07-01 thomas return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
299 6d054bb9 2022-09-23 thomas sb2, label2, diff_algo, diff_context, ignore_whitespace,
300 6d054bb9 2022-09-23 thomas force_text_diff, outfile);
301 474b4f94 2017-11-30 stsp }
302 474b4f94 2017-11-30 stsp
303 474b4f94 2017-11-30 stsp static const struct got_error *
304 dd2e2f52 2022-07-01 thomas diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
305 19a6a6b5 2022-07-01 thomas const char *label, mode_t mode, struct got_repository *repo,
306 a0f32f33 2022-06-13 thomas got_diff_blob_cb cb, void *cb_arg)
307 474b4f94 2017-11-30 stsp {
308 4e22badc 2017-11-30 stsp const struct got_error *err;
309 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
310 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
311 4e22badc 2017-11-30 stsp
312 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
313 4e22badc 2017-11-30 stsp if (err)
314 4e22badc 2017-11-30 stsp return err;
315 4e22badc 2017-11-30 stsp
316 dd2e2f52 2022-07-01 thomas err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
317 2acfca77 2018-04-01 stsp if (err)
318 2acfca77 2018-04-01 stsp goto done;
319 dd2e2f52 2022-07-01 thomas err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
320 a0f32f33 2022-06-13 thomas NULL, label, 0, mode, repo);
321 2acfca77 2018-04-01 stsp done:
322 2acfca77 2018-04-01 stsp got_object_close(obj);
323 2acfca77 2018-04-01 stsp if (blob)
324 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
325 2acfca77 2018-04-01 stsp return err;
326 474b4f94 2017-11-30 stsp }
327 474b4f94 2017-11-30 stsp
328 474b4f94 2017-11-30 stsp static const struct got_error *
329 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
330 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
331 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
332 a0f32f33 2022-06-13 thomas mode_t mode1, mode_t mode2, struct got_repository *repo,
333 a0f32f33 2022-06-13 thomas got_diff_blob_cb cb, void *cb_arg)
334 474b4f94 2017-11-30 stsp {
335 6a213ccb 2017-11-30 stsp const struct got_error *err;
336 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
337 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
338 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
339 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
340 6a213ccb 2017-11-30 stsp
341 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
342 6a213ccb 2017-11-30 stsp if (err)
343 730a8aa0 2018-04-24 stsp return err;
344 f4ae6ddb 2022-07-01 thomas
345 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
346 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
347 6a213ccb 2017-11-30 stsp goto done;
348 6a213ccb 2017-11-30 stsp }
349 6a213ccb 2017-11-30 stsp
350 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
351 730a8aa0 2018-04-24 stsp if (err)
352 6a213ccb 2017-11-30 stsp goto done;
353 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
354 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
355 6a213ccb 2017-11-30 stsp goto done;
356 6a213ccb 2017-11-30 stsp }
357 6a213ccb 2017-11-30 stsp
358 f4ae6ddb 2022-07-01 thomas err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
359 2acfca77 2018-04-01 stsp if (err)
360 6a213ccb 2017-11-30 stsp goto done;
361 6a213ccb 2017-11-30 stsp
362 f4ae6ddb 2022-07-01 thomas err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
363 2acfca77 2018-04-01 stsp if (err)
364 6a213ccb 2017-11-30 stsp goto done;
365 6a213ccb 2017-11-30 stsp
366 a0f32f33 2022-06-13 thomas err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
367 a0f32f33 2022-06-13 thomas mode1, mode2, repo);
368 6a213ccb 2017-11-30 stsp done:
369 a3e2cbea 2017-12-01 stsp if (obj1)
370 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
371 a3e2cbea 2017-12-01 stsp if (obj2)
372 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
373 a3e2cbea 2017-12-01 stsp if (blob1)
374 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
375 a3e2cbea 2017-12-01 stsp if (blob2)
376 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
377 6a213ccb 2017-11-30 stsp return err;
378 474b4f94 2017-11-30 stsp }
379 474b4f94 2017-11-30 stsp
380 474b4f94 2017-11-30 stsp static const struct got_error *
381 dd2e2f52 2022-07-01 thomas diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
382 dd2e2f52 2022-07-01 thomas FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
383 19a6a6b5 2022-07-01 thomas got_diff_blob_cb cb, void *cb_arg)
384 474b4f94 2017-11-30 stsp {
385 365fb436 2017-11-30 stsp const struct got_error *err;
386 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
387 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
388 365fb436 2017-11-30 stsp
389 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
390 365fb436 2017-11-30 stsp if (err)
391 365fb436 2017-11-30 stsp return err;
392 365fb436 2017-11-30 stsp
393 dd2e2f52 2022-07-01 thomas err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
394 2acfca77 2018-04-01 stsp if (err)
395 2acfca77 2018-04-01 stsp goto done;
396 dd2e2f52 2022-07-01 thomas err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
397 a0f32f33 2022-06-13 thomas mode, 0, repo);
398 2acfca77 2018-04-01 stsp done:
399 2acfca77 2018-04-01 stsp got_object_close(obj);
400 2acfca77 2018-04-01 stsp if (blob)
401 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
402 2acfca77 2018-04-01 stsp return err;
403 474b4f94 2017-11-30 stsp }
404 474b4f94 2017-11-30 stsp
405 474b4f94 2017-11-30 stsp static const struct got_error *
406 dd2e2f52 2022-07-01 thomas diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
407 dd2e2f52 2022-07-01 thomas const char *label, struct got_repository *repo, got_diff_blob_cb cb,
408 dd2e2f52 2022-07-01 thomas void *cb_arg, int diff_content)
409 474b4f94 2017-11-30 stsp {
410 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
411 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
412 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
413 9c70d4c3 2017-11-30 stsp
414 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
415 9c70d4c3 2017-11-30 stsp if (err)
416 9c70d4c3 2017-11-30 stsp goto done;
417 9c70d4c3 2017-11-30 stsp
418 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
419 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
420 9c70d4c3 2017-11-30 stsp goto done;
421 9c70d4c3 2017-11-30 stsp }
422 9c70d4c3 2017-11-30 stsp
423 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
424 9c70d4c3 2017-11-30 stsp if (err)
425 9c70d4c3 2017-11-30 stsp goto done;
426 9c70d4c3 2017-11-30 stsp
427 dd2e2f52 2022-07-01 thomas err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
428 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
429 9c70d4c3 2017-11-30 stsp done:
430 9c70d4c3 2017-11-30 stsp if (tree)
431 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
432 9c70d4c3 2017-11-30 stsp if (treeobj)
433 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
434 9c70d4c3 2017-11-30 stsp return err;
435 474b4f94 2017-11-30 stsp }
436 474b4f94 2017-11-30 stsp
437 474b4f94 2017-11-30 stsp static const struct got_error *
438 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
439 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
440 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
441 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
442 a0f32f33 2022-06-13 thomas int diff_content)
443 474b4f94 2017-11-30 stsp {
444 f6861a81 2018-09-13 stsp const struct got_error *err;
445 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
446 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
447 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
448 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
449 789689b5 2017-11-30 stsp
450 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
451 789689b5 2017-11-30 stsp if (err)
452 789689b5 2017-11-30 stsp goto done;
453 789689b5 2017-11-30 stsp
454 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
455 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
456 789689b5 2017-11-30 stsp goto done;
457 789689b5 2017-11-30 stsp }
458 789689b5 2017-11-30 stsp
459 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
460 789689b5 2017-11-30 stsp if (err)
461 789689b5 2017-11-30 stsp goto done;
462 789689b5 2017-11-30 stsp
463 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
464 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
465 789689b5 2017-11-30 stsp goto done;
466 789689b5 2017-11-30 stsp }
467 789689b5 2017-11-30 stsp
468 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
469 789689b5 2017-11-30 stsp if (err)
470 789689b5 2017-11-30 stsp goto done;
471 789689b5 2017-11-30 stsp
472 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
473 789689b5 2017-11-30 stsp if (err)
474 789689b5 2017-11-30 stsp goto done;
475 789689b5 2017-11-30 stsp
476 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
477 19a6a6b5 2022-07-01 thomas label1, label2, repo, cb, cb_arg, diff_content);
478 789689b5 2017-11-30 stsp
479 789689b5 2017-11-30 stsp done:
480 789689b5 2017-11-30 stsp if (tree1)
481 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
482 789689b5 2017-11-30 stsp if (tree2)
483 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
484 789689b5 2017-11-30 stsp if (treeobj1)
485 789689b5 2017-11-30 stsp got_object_close(treeobj1);
486 789689b5 2017-11-30 stsp if (treeobj2)
487 789689b5 2017-11-30 stsp got_object_close(treeobj2);
488 789689b5 2017-11-30 stsp return err;
489 474b4f94 2017-11-30 stsp }
490 474b4f94 2017-11-30 stsp
491 474b4f94 2017-11-30 stsp static const struct got_error *
492 dd2e2f52 2022-07-01 thomas diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
493 dd2e2f52 2022-07-01 thomas FILE *f2, const char *label, struct got_repository *repo,
494 19a6a6b5 2022-07-01 thomas got_diff_blob_cb cb, void *cb_arg, int diff_content)
495 474b4f94 2017-11-30 stsp {
496 f6861a81 2018-09-13 stsp const struct got_error *err;
497 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
498 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
499 2c56f2ce 2017-11-30 stsp
500 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
501 2c56f2ce 2017-11-30 stsp if (err)
502 2c56f2ce 2017-11-30 stsp goto done;
503 2c56f2ce 2017-11-30 stsp
504 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
505 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
506 2c56f2ce 2017-11-30 stsp goto done;
507 2c56f2ce 2017-11-30 stsp }
508 2c56f2ce 2017-11-30 stsp
509 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
510 2c56f2ce 2017-11-30 stsp if (err)
511 2c56f2ce 2017-11-30 stsp goto done;
512 2c56f2ce 2017-11-30 stsp
513 dd2e2f52 2022-07-01 thomas err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
514 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
515 2c56f2ce 2017-11-30 stsp done:
516 2c56f2ce 2017-11-30 stsp if (tree)
517 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
518 2c56f2ce 2017-11-30 stsp if (treeobj)
519 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
520 2c56f2ce 2017-11-30 stsp return err;
521 474b4f94 2017-11-30 stsp }
522 474b4f94 2017-11-30 stsp
523 474b4f94 2017-11-30 stsp static const struct got_error *
524 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
525 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
526 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
527 474b4f94 2017-11-30 stsp {
528 013404a9 2017-11-30 stsp /* XXX TODO */
529 474b4f94 2017-11-30 stsp return NULL;
530 474b4f94 2017-11-30 stsp }
531 474b4f94 2017-11-30 stsp
532 474b4f94 2017-11-30 stsp static const struct got_error *
533 a0f32f33 2022-06-13 thomas diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
534 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
535 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
536 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
537 31b4484f 2019-07-27 stsp int diff_content)
538 474b4f94 2017-11-30 stsp {
539 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
540 19ae6da1 2018-11-05 stsp int id_match;
541 63c5ca5d 2019-08-24 stsp
542 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te1))
543 63c5ca5d 2019-08-24 stsp return NULL;
544 474b4f94 2017-11-30 stsp
545 474b4f94 2017-11-30 stsp if (te2 == NULL) {
546 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
547 dd2e2f52 2022-07-01 thomas err = diff_deleted_tree(&te1->id, f1, fd1, f2,
548 dd2e2f52 2022-07-01 thomas label1, repo, cb, cb_arg, diff_content);
549 31b4484f 2019-07-27 stsp else {
550 31b4484f 2019-07-27 stsp if (diff_content)
551 19a6a6b5 2022-07-01 thomas err = diff_deleted_blob(&te1->id, f1, fd1,
552 dd2e2f52 2022-07-01 thomas f2, label1, te1->mode, repo, cb, cb_arg);
553 31b4484f 2019-07-27 stsp else
554 a0f32f33 2022-06-13 thomas err = cb(cb_arg, NULL, NULL, NULL, NULL,
555 a0f32f33 2022-06-13 thomas &te1->id, NULL, label1, NULL,
556 a0f32f33 2022-06-13 thomas te1->mode, 0, repo);
557 31b4484f 2019-07-27 stsp }
558 f6861a81 2018-09-13 stsp return err;
559 63c5ca5d 2019-08-24 stsp } else if (got_object_tree_entry_is_submodule(te2))
560 63c5ca5d 2019-08-24 stsp return NULL;
561 474b4f94 2017-11-30 stsp
562 56e0773d 2019-11-28 stsp id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
563 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
564 19ae6da1 2018-11-05 stsp if (!id_match)
565 a0f32f33 2022-06-13 thomas return diff_modified_tree(&te1->id, &te2->id, f1, f2,
566 19a6a6b5 2022-07-01 thomas fd1, fd2, label1, label2, repo, cb, cb_arg,
567 19a6a6b5 2022-07-01 thomas diff_content);
568 40dde666 2020-07-23 stsp } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
569 40dde666 2020-07-23 stsp (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
570 46f68b20 2019-10-19 stsp if (!id_match ||
571 40dde666 2020-07-23 stsp ((te1->mode & (S_IFLNK | S_IXUSR))) !=
572 40dde666 2020-07-23 stsp (te2->mode & (S_IFLNK | S_IXUSR))) {
573 31b4484f 2019-07-27 stsp if (diff_content)
574 56e0773d 2019-11-28 stsp return diff_modified_blob(&te1->id, &te2->id,
575 19a6a6b5 2022-07-01 thomas f1, f2, fd1, fd2, label1, label2,
576 19a6a6b5 2022-07-01 thomas te1->mode, te2->mode, repo, cb, cb_arg);
577 31b4484f 2019-07-27 stsp else
578 a0f32f33 2022-06-13 thomas return cb(cb_arg, NULL, NULL, NULL, NULL,
579 a0f32f33 2022-06-13 thomas &te1->id, &te2->id, label1, label2,
580 a0f32f33 2022-06-13 thomas te1->mode, te2->mode, repo);
581 31b4484f 2019-07-27 stsp }
582 413ea19d 2017-11-30 stsp }
583 474b4f94 2017-11-30 stsp
584 19ae6da1 2018-11-05 stsp if (id_match)
585 f6861a81 2018-09-13 stsp return NULL;
586 f6861a81 2018-09-13 stsp
587 56e0773d 2019-11-28 stsp return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
588 aaa13589 2019-06-01 stsp cb, cb_arg);
589 474b4f94 2017-11-30 stsp }
590 474b4f94 2017-11-30 stsp
591 474b4f94 2017-11-30 stsp static const struct got_error *
592 56e0773d 2019-11-28 stsp diff_entry_new_old(struct got_tree_entry *te2,
593 dd2e2f52 2022-07-01 thomas struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
594 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
595 31b4484f 2019-07-27 stsp int diff_content)
596 474b4f94 2017-11-30 stsp {
597 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
598 63c5ca5d 2019-08-24 stsp return NULL;
599 63c5ca5d 2019-08-24 stsp
600 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te2))
601 f6861a81 2018-09-13 stsp return NULL;
602 474b4f94 2017-11-30 stsp
603 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
604 dd2e2f52 2022-07-01 thomas return diff_added_tree(&te2->id, f1, f2, fd2, label2,
605 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
606 f6861a81 2018-09-13 stsp
607 31b4484f 2019-07-27 stsp if (diff_content)
608 dd2e2f52 2022-07-01 thomas return diff_added_blob(&te2->id, f1, f2, fd2,
609 19a6a6b5 2022-07-01 thomas label2, te2->mode, repo, cb, cb_arg);
610 31b4484f 2019-07-27 stsp
611 a0f32f33 2022-06-13 thomas return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
612 a0f32f33 2022-06-13 thomas NULL, label2, 0, te2->mode, repo);
613 0208f208 2020-05-05 stsp }
614 772fcad5 2023-01-07 thomas
615 772fcad5 2023-01-07 thomas static void
616 772fcad5 2023-01-07 thomas diffstat_field_width(size_t *maxlen, int *add_cols, int *rm_cols, size_t len,
617 772fcad5 2023-01-07 thomas uint32_t add, uint32_t rm)
618 772fcad5 2023-01-07 thomas {
619 772fcad5 2023-01-07 thomas int d1 = 1, d2 = 1;
620 0208f208 2020-05-05 stsp
621 772fcad5 2023-01-07 thomas *maxlen = MAX(*maxlen, len);
622 772fcad5 2023-01-07 thomas
623 772fcad5 2023-01-07 thomas while (add /= 10)
624 772fcad5 2023-01-07 thomas ++d1;
625 772fcad5 2023-01-07 thomas *add_cols = MAX(*add_cols, d1);
626 772fcad5 2023-01-07 thomas
627 772fcad5 2023-01-07 thomas while (rm /= 10)
628 772fcad5 2023-01-07 thomas ++d2;
629 772fcad5 2023-01-07 thomas *rm_cols = MAX(*rm_cols, d2);
630 772fcad5 2023-01-07 thomas }
631 772fcad5 2023-01-07 thomas
632 0208f208 2020-05-05 stsp const struct got_error *
633 772fcad5 2023-01-07 thomas got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
634 772fcad5 2023-01-07 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
635 772fcad5 2023-01-07 thomas struct got_object_id *id1, struct got_object_id *id2,
636 772fcad5 2023-01-07 thomas const char *label1, const char *label2,
637 772fcad5 2023-01-07 thomas mode_t mode1, mode_t mode2, struct got_repository *repo)
638 772fcad5 2023-01-07 thomas {
639 772fcad5 2023-01-07 thomas const struct got_error *err = NULL;
640 772fcad5 2023-01-07 thomas struct got_diffreg_result *result = NULL;
641 772fcad5 2023-01-07 thomas struct diff_result *r;
642 772fcad5 2023-01-07 thomas struct got_diff_changed_path *change = NULL;
643 772fcad5 2023-01-07 thomas struct got_diffstat_cb_arg *a = arg;
644 772fcad5 2023-01-07 thomas struct got_pathlist_entry *pe;
645 772fcad5 2023-01-07 thomas char *path = NULL;
646 772fcad5 2023-01-07 thomas int i;
647 772fcad5 2023-01-07 thomas
648 772fcad5 2023-01-07 thomas path = strdup(label2 ? label2 : label1);
649 772fcad5 2023-01-07 thomas if (path == NULL)
650 772fcad5 2023-01-07 thomas return got_error_from_errno("malloc");
651 772fcad5 2023-01-07 thomas
652 772fcad5 2023-01-07 thomas change = malloc(sizeof(*change));
653 772fcad5 2023-01-07 thomas if (change == NULL) {
654 772fcad5 2023-01-07 thomas err = got_error_from_errno("malloc");
655 772fcad5 2023-01-07 thomas goto done;
656 772fcad5 2023-01-07 thomas }
657 772fcad5 2023-01-07 thomas
658 772fcad5 2023-01-07 thomas change->add = 0;
659 772fcad5 2023-01-07 thomas change->rm = 0;
660 772fcad5 2023-01-07 thomas change->status = GOT_STATUS_NO_CHANGE;
661 772fcad5 2023-01-07 thomas if (id1 == NULL)
662 772fcad5 2023-01-07 thomas change->status = GOT_STATUS_ADD;
663 772fcad5 2023-01-07 thomas else if (id2 == NULL)
664 772fcad5 2023-01-07 thomas change->status = GOT_STATUS_DELETE;
665 772fcad5 2023-01-07 thomas else {
666 772fcad5 2023-01-07 thomas if (got_object_id_cmp(id1, id2) != 0)
667 772fcad5 2023-01-07 thomas change->status = GOT_STATUS_MODIFY;
668 772fcad5 2023-01-07 thomas else if (mode1 != mode2)
669 772fcad5 2023-01-07 thomas change->status = GOT_STATUS_MODE_CHANGE;
670 772fcad5 2023-01-07 thomas }
671 772fcad5 2023-01-07 thomas
672 772fcad5 2023-01-07 thomas if (f1) {
673 772fcad5 2023-01-07 thomas err = got_opentemp_truncate(f1);
674 772fcad5 2023-01-07 thomas if (err)
675 772fcad5 2023-01-07 thomas goto done;
676 772fcad5 2023-01-07 thomas }
677 772fcad5 2023-01-07 thomas if (f2) {
678 772fcad5 2023-01-07 thomas err = got_opentemp_truncate(f2);
679 772fcad5 2023-01-07 thomas if (err)
680 772fcad5 2023-01-07 thomas goto done;
681 772fcad5 2023-01-07 thomas }
682 772fcad5 2023-01-07 thomas
683 772fcad5 2023-01-07 thomas if (blob1) {
684 772fcad5 2023-01-07 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
685 772fcad5 2023-01-07 thomas blob1);
686 772fcad5 2023-01-07 thomas if (err)
687 772fcad5 2023-01-07 thomas goto done;
688 772fcad5 2023-01-07 thomas }
689 772fcad5 2023-01-07 thomas if (blob2) {
690 772fcad5 2023-01-07 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
691 772fcad5 2023-01-07 thomas blob2);
692 772fcad5 2023-01-07 thomas if (err)
693 772fcad5 2023-01-07 thomas goto done;
694 772fcad5 2023-01-07 thomas }
695 772fcad5 2023-01-07 thomas
696 772fcad5 2023-01-07 thomas err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
697 772fcad5 2023-01-07 thomas a->force_text);
698 772fcad5 2023-01-07 thomas if (err)
699 772fcad5 2023-01-07 thomas goto done;
700 772fcad5 2023-01-07 thomas
701 772fcad5 2023-01-07 thomas for (i = 0, r = result->result; i < r->chunks.len; ++i) {
702 772fcad5 2023-01-07 thomas int flags = (r->left->atomizer_flags | r->right->atomizer_flags);
703 772fcad5 2023-01-07 thomas int isbin = (flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
704 772fcad5 2023-01-07 thomas
705 772fcad5 2023-01-07 thomas if (!isbin || a->force_text) {
706 772fcad5 2023-01-07 thomas struct diff_chunk *c;
707 772fcad5 2023-01-07 thomas int clc, crc;
708 772fcad5 2023-01-07 thomas
709 772fcad5 2023-01-07 thomas c = diff_chunk_get(r, i);
710 772fcad5 2023-01-07 thomas clc = diff_chunk_get_left_count(c);
711 772fcad5 2023-01-07 thomas crc = diff_chunk_get_right_count(c);
712 772fcad5 2023-01-07 thomas
713 772fcad5 2023-01-07 thomas if (clc && !crc)
714 772fcad5 2023-01-07 thomas change->rm += clc;
715 772fcad5 2023-01-07 thomas else if (crc && !clc)
716 772fcad5 2023-01-07 thomas change->add += crc;
717 772fcad5 2023-01-07 thomas }
718 772fcad5 2023-01-07 thomas }
719 772fcad5 2023-01-07 thomas
720 772fcad5 2023-01-07 thomas err = got_pathlist_append(a->paths, path, change);
721 772fcad5 2023-01-07 thomas if (err)
722 772fcad5 2023-01-07 thomas goto done;
723 772fcad5 2023-01-07 thomas
724 772fcad5 2023-01-07 thomas pe = TAILQ_LAST(a->paths, got_pathlist_head);
725 772fcad5 2023-01-07 thomas diffstat_field_width(&a->max_path_len, &a->add_cols, &a->rm_cols,
726 772fcad5 2023-01-07 thomas pe->path_len, change->add, change->rm);
727 772fcad5 2023-01-07 thomas a->ins += change->add;
728 772fcad5 2023-01-07 thomas a->del += change->rm;
729 772fcad5 2023-01-07 thomas ++a->nfiles;
730 772fcad5 2023-01-07 thomas
731 772fcad5 2023-01-07 thomas done:
732 772fcad5 2023-01-07 thomas if (result) {
733 772fcad5 2023-01-07 thomas const struct got_error *free_err;
734 772fcad5 2023-01-07 thomas
735 772fcad5 2023-01-07 thomas free_err = got_diffreg_result_free(result);
736 772fcad5 2023-01-07 thomas if (free_err && err == NULL)
737 772fcad5 2023-01-07 thomas err = free_err;
738 772fcad5 2023-01-07 thomas }
739 772fcad5 2023-01-07 thomas if (err) {
740 772fcad5 2023-01-07 thomas free(path);
741 772fcad5 2023-01-07 thomas free(change);
742 772fcad5 2023-01-07 thomas }
743 772fcad5 2023-01-07 thomas return err;
744 772fcad5 2023-01-07 thomas }
745 772fcad5 2023-01-07 thomas
746 772fcad5 2023-01-07 thomas const struct got_error *
747 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
748 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
749 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
750 a0f32f33 2022-06-13 thomas const char *label1, const char *label2,
751 0208f208 2020-05-05 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
752 0208f208 2020-05-05 stsp {
753 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
754 0208f208 2020-05-05 stsp struct got_pathlist_head *paths = arg;
755 0208f208 2020-05-05 stsp struct got_diff_changed_path *change = NULL;
756 0208f208 2020-05-05 stsp char *path = NULL;
757 0208f208 2020-05-05 stsp
758 0208f208 2020-05-05 stsp path = strdup(label2 ? label2 : label1);
759 0208f208 2020-05-05 stsp if (path == NULL)
760 0208f208 2020-05-05 stsp return got_error_from_errno("malloc");
761 0208f208 2020-05-05 stsp
762 0208f208 2020-05-05 stsp change = malloc(sizeof(*change));
763 0208f208 2020-05-05 stsp if (change == NULL) {
764 0208f208 2020-05-05 stsp err = got_error_from_errno("malloc");
765 0208f208 2020-05-05 stsp goto done;
766 0208f208 2020-05-05 stsp }
767 0208f208 2020-05-05 stsp
768 0208f208 2020-05-05 stsp change->status = GOT_STATUS_NO_CHANGE;
769 0208f208 2020-05-05 stsp if (id1 == NULL)
770 0208f208 2020-05-05 stsp change->status = GOT_STATUS_ADD;
771 0208f208 2020-05-05 stsp else if (id2 == NULL)
772 0208f208 2020-05-05 stsp change->status = GOT_STATUS_DELETE;
773 0208f208 2020-05-05 stsp else {
774 0208f208 2020-05-05 stsp if (got_object_id_cmp(id1, id2) != 0)
775 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODIFY;
776 0208f208 2020-05-05 stsp else if (mode1 != mode2)
777 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODE_CHANGE;
778 0208f208 2020-05-05 stsp }
779 0208f208 2020-05-05 stsp
780 4c71f93b 2021-12-31 thomas err = got_pathlist_append(paths, path, change);
781 0208f208 2020-05-05 stsp done:
782 0208f208 2020-05-05 stsp if (err) {
783 0208f208 2020-05-05 stsp free(path);
784 0208f208 2020-05-05 stsp free(change);
785 0208f208 2020-05-05 stsp }
786 0208f208 2020-05-05 stsp return err;
787 474b4f94 2017-11-30 stsp }
788 474b4f94 2017-11-30 stsp
789 474b4f94 2017-11-30 stsp const struct got_error *
790 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
791 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
792 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
793 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
794 a0f32f33 2022-06-13 thomas int diff_content)
795 474b4f94 2017-11-30 stsp {
796 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
797 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
798 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
799 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
800 56e0773d 2019-11-28 stsp int tidx1 = 0, tidx2 = 0;
801 474b4f94 2017-11-30 stsp
802 883f0469 2018-06-23 stsp if (tree1) {
803 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, 0);
804 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
805 f6861a81 2018-09-13 stsp te1->name) == -1)
806 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
807 883f0469 2018-06-23 stsp }
808 883f0469 2018-06-23 stsp if (tree2) {
809 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, 0);
810 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
811 f6861a81 2018-09-13 stsp te2->name) == -1)
812 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
813 883f0469 2018-06-23 stsp }
814 474b4f94 2017-11-30 stsp
815 474b4f94 2017-11-30 stsp do {
816 474b4f94 2017-11-30 stsp if (te1) {
817 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
818 f6861a81 2018-09-13 stsp if (tree2)
819 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
820 1de5e065 2019-06-01 stsp te1->name);
821 f6861a81 2018-09-13 stsp if (te) {
822 f6861a81 2018-09-13 stsp free(l2);
823 f6861a81 2018-09-13 stsp l2 = NULL;
824 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
825 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
826 230a42bd 2019-05-11 jcs return
827 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
828 f6861a81 2018-09-13 stsp }
829 19a6a6b5 2022-07-01 thomas err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
830 19a6a6b5 2022-07-01 thomas l1, l2, repo, cb, cb_arg, diff_content);
831 474b4f94 2017-11-30 stsp if (err)
832 474b4f94 2017-11-30 stsp break;
833 474b4f94 2017-11-30 stsp }
834 474b4f94 2017-11-30 stsp
835 474b4f94 2017-11-30 stsp if (te2) {
836 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
837 f6861a81 2018-09-13 stsp if (tree1)
838 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
839 1de5e065 2019-06-01 stsp te2->name);
840 d6ce02f1 2018-11-17 stsp free(l2);
841 d6ce02f1 2018-11-17 stsp if (te) {
842 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
843 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
844 230a42bd 2019-05-11 jcs return
845 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
846 d6ce02f1 2018-11-17 stsp } else {
847 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
848 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->name) == -1)
849 230a42bd 2019-05-11 jcs return
850 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
851 d6ce02f1 2018-11-17 stsp }
852 dd2e2f52 2022-07-01 thomas err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
853 dd2e2f52 2022-07-01 thomas repo, cb, cb_arg, diff_content);
854 474b4f94 2017-11-30 stsp if (err)
855 474b4f94 2017-11-30 stsp break;
856 474b4f94 2017-11-30 stsp }
857 474b4f94 2017-11-30 stsp
858 f6861a81 2018-09-13 stsp free(l1);
859 f6861a81 2018-09-13 stsp l1 = NULL;
860 f6861a81 2018-09-13 stsp if (te1) {
861 56e0773d 2019-11-28 stsp tidx1++;
862 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, tidx1);
863 f6861a81 2018-09-13 stsp if (te1 &&
864 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
865 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
866 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
867 f6861a81 2018-09-13 stsp }
868 f6861a81 2018-09-13 stsp free(l2);
869 f6861a81 2018-09-13 stsp l2 = NULL;
870 f6861a81 2018-09-13 stsp if (te2) {
871 56e0773d 2019-11-28 stsp tidx2++;
872 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, tidx2);
873 f6861a81 2018-09-13 stsp if (te2 &&
874 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
875 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
876 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
877 f6861a81 2018-09-13 stsp }
878 474b4f94 2017-11-30 stsp } while (te1 || te2);
879 11528a82 2018-05-19 stsp
880 11528a82 2018-05-19 stsp return err;
881 11528a82 2018-05-19 stsp }
882 11528a82 2018-05-19 stsp
883 11528a82 2018-05-19 stsp const struct got_error *
884 82c78e96 2022-08-06 thomas got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
885 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
886 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
887 25ec7006 2022-07-01 thomas const char *label1, const char *label2,
888 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context,
889 64453f7e 2020-11-21 stsp int ignore_whitespace, int force_text_diff,
890 64453f7e 2020-11-21 stsp struct got_repository *repo, FILE *outfile)
891 11528a82 2018-05-19 stsp {
892 11528a82 2018-05-19 stsp const struct got_error *err;
893 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
894 b74c7625 2018-05-20 stsp
895 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
896 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
897 f4ae6ddb 2022-07-01 thomas
898 15a94983 2018-12-23 stsp if (id1) {
899 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
900 cd0acaa7 2018-05-20 stsp if (err)
901 cd0acaa7 2018-05-20 stsp goto done;
902 cd0acaa7 2018-05-20 stsp }
903 15a94983 2018-12-23 stsp if (id2) {
904 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
905 cd0acaa7 2018-05-20 stsp if (err)
906 cd0acaa7 2018-05-20 stsp goto done;
907 cd0acaa7 2018-05-20 stsp }
908 82c78e96 2022-08-06 thomas err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
909 82c78e96 2022-08-06 thomas diff_algo, diff_context, ignore_whitespace, force_text_diff,
910 82c78e96 2022-08-06 thomas outfile);
911 cc8021af 2021-10-12 thomas done:
912 cc8021af 2021-10-12 thomas if (blob1)
913 cc8021af 2021-10-12 thomas got_object_blob_close(blob1);
914 cc8021af 2021-10-12 thomas if (blob2)
915 cc8021af 2021-10-12 thomas got_object_blob_close(blob2);
916 cc8021af 2021-10-12 thomas return err;
917 cc8021af 2021-10-12 thomas }
918 cc8021af 2021-10-12 thomas
919 cc8021af 2021-10-12 thomas static const struct got_error *
920 cc8021af 2021-10-12 thomas diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
921 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
922 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
923 cc8021af 2021-10-12 thomas {
924 cc8021af 2021-10-12 thomas const struct got_error *err = NULL;
925 cc8021af 2021-10-12 thomas struct got_pathlist_entry *pe;
926 cc8021af 2021-10-12 thomas struct got_object_id *id1 = NULL, *id2 = NULL;
927 cc8021af 2021-10-12 thomas struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
928 cc8021af 2021-10-12 thomas struct got_blob_object *blob1 = NULL, *blob2 = NULL;
929 cc8021af 2021-10-12 thomas
930 cc8021af 2021-10-12 thomas TAILQ_FOREACH(pe, paths, entry) {
931 cc8021af 2021-10-12 thomas int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
932 cc8021af 2021-10-12 thomas mode_t mode1 = 0, mode2 = 0;
933 cc8021af 2021-10-12 thomas
934 cc8021af 2021-10-12 thomas free(id1);
935 cc8021af 2021-10-12 thomas id1 = NULL;
936 cc8021af 2021-10-12 thomas free(id2);
937 cc8021af 2021-10-12 thomas id2 = NULL;
938 cc8021af 2021-10-12 thomas if (subtree1) {
939 cc8021af 2021-10-12 thomas got_object_tree_close(subtree1);
940 cc8021af 2021-10-12 thomas subtree1 = NULL;
941 cc8021af 2021-10-12 thomas }
942 cc8021af 2021-10-12 thomas if (subtree2) {
943 cc8021af 2021-10-12 thomas got_object_tree_close(subtree2);
944 cc8021af 2021-10-12 thomas subtree2 = NULL;
945 cc8021af 2021-10-12 thomas }
946 cc8021af 2021-10-12 thomas if (blob1) {
947 cc8021af 2021-10-12 thomas got_object_blob_close(blob1);
948 cc8021af 2021-10-12 thomas blob1 = NULL;
949 cc8021af 2021-10-12 thomas }
950 cc8021af 2021-10-12 thomas if (blob2) {
951 cc8021af 2021-10-12 thomas got_object_blob_close(blob2);
952 cc8021af 2021-10-12 thomas blob2 = NULL;
953 cc8021af 2021-10-12 thomas }
954 cc8021af 2021-10-12 thomas
955 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
956 cc8021af 2021-10-12 thomas pe->path);
957 cc8021af 2021-10-12 thomas if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
958 cc8021af 2021-10-12 thomas goto done;
959 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
960 cc8021af 2021-10-12 thomas pe->path);
961 cc8021af 2021-10-12 thomas if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
962 cc8021af 2021-10-12 thomas goto done;
963 cc8021af 2021-10-12 thomas if (id1 == NULL && id2 == NULL) {
964 cc8021af 2021-10-12 thomas err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
965 cc8021af 2021-10-12 thomas goto done;
966 cc8021af 2021-10-12 thomas }
967 cc8021af 2021-10-12 thomas if (id1) {
968 cc8021af 2021-10-12 thomas err = got_object_get_type(&type1, repo, id1);
969 cc8021af 2021-10-12 thomas if (err)
970 cc8021af 2021-10-12 thomas goto done;
971 cc8021af 2021-10-12 thomas }
972 cc8021af 2021-10-12 thomas if (id2) {
973 cc8021af 2021-10-12 thomas err = got_object_get_type(&type2, repo, id2);
974 cc8021af 2021-10-12 thomas if (err)
975 cc8021af 2021-10-12 thomas goto done;
976 cc8021af 2021-10-12 thomas }
977 cc8021af 2021-10-12 thomas if (type1 == GOT_OBJ_TYPE_ANY &&
978 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_ANY) {
979 cc8021af 2021-10-12 thomas err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
980 cc8021af 2021-10-12 thomas goto done;
981 cc8021af 2021-10-12 thomas } else if (type1 != GOT_OBJ_TYPE_ANY &&
982 cc8021af 2021-10-12 thomas type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
983 cc8021af 2021-10-12 thomas err = got_error(GOT_ERR_OBJ_TYPE);
984 cc8021af 2021-10-12 thomas goto done;
985 cc8021af 2021-10-12 thomas }
986 cc8021af 2021-10-12 thomas
987 cc8021af 2021-10-12 thomas if (type1 == GOT_OBJ_TYPE_BLOB ||
988 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_BLOB) {
989 cc8021af 2021-10-12 thomas if (id1) {
990 cc8021af 2021-10-12 thomas err = got_object_open_as_blob(&blob1, repo,
991 f4ae6ddb 2022-07-01 thomas id1, 8192, fd1);
992 cc8021af 2021-10-12 thomas if (err)
993 cc8021af 2021-10-12 thomas goto done;
994 cc8021af 2021-10-12 thomas }
995 cc8021af 2021-10-12 thomas if (id2) {
996 cc8021af 2021-10-12 thomas err = got_object_open_as_blob(&blob2, repo,
997 f4ae6ddb 2022-07-01 thomas id2, 8192, fd2);
998 cc8021af 2021-10-12 thomas if (err)
999 cc8021af 2021-10-12 thomas goto done;
1000 cc8021af 2021-10-12 thomas }
1001 a0f32f33 2022-06-13 thomas err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1002 cc8021af 2021-10-12 thomas id1 ? pe->path : "/dev/null",
1003 cc8021af 2021-10-12 thomas id2 ? pe->path : "/dev/null",
1004 cc8021af 2021-10-12 thomas mode1, mode2, repo);
1005 cc8021af 2021-10-12 thomas if (err)
1006 cc8021af 2021-10-12 thomas goto done;
1007 cc8021af 2021-10-12 thomas } else if (type1 == GOT_OBJ_TYPE_TREE ||
1008 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_TREE) {
1009 cc8021af 2021-10-12 thomas if (id1) {
1010 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&subtree1, repo,
1011 cc8021af 2021-10-12 thomas id1);
1012 cc8021af 2021-10-12 thomas if (err)
1013 cc8021af 2021-10-12 thomas goto done;
1014 cc8021af 2021-10-12 thomas }
1015 cc8021af 2021-10-12 thomas if (id2) {
1016 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&subtree2, repo,
1017 cc8021af 2021-10-12 thomas id2);
1018 cc8021af 2021-10-12 thomas if (err)
1019 cc8021af 2021-10-12 thomas goto done;
1020 cc8021af 2021-10-12 thomas }
1021 a0f32f33 2022-06-13 thomas err = got_diff_tree(subtree1, subtree2, f1, f2,
1022 19a6a6b5 2022-07-01 thomas fd1, fd2,
1023 cc8021af 2021-10-12 thomas id1 ? pe->path : "/dev/null",
1024 cc8021af 2021-10-12 thomas id2 ? pe->path : "/dev/null",
1025 cc8021af 2021-10-12 thomas repo, cb, cb_arg, 1);
1026 cc8021af 2021-10-12 thomas if (err)
1027 cc8021af 2021-10-12 thomas goto done;
1028 cc8021af 2021-10-12 thomas } else {
1029 cc8021af 2021-10-12 thomas err = got_error(GOT_ERR_OBJ_TYPE);
1030 cc8021af 2021-10-12 thomas goto done;
1031 cc8021af 2021-10-12 thomas }
1032 cc8021af 2021-10-12 thomas }
1033 11528a82 2018-05-19 stsp done:
1034 cc8021af 2021-10-12 thomas free(id1);
1035 cc8021af 2021-10-12 thomas free(id2);
1036 cc8021af 2021-10-12 thomas if (subtree1)
1037 cc8021af 2021-10-12 thomas got_object_tree_close(subtree1);
1038 cc8021af 2021-10-12 thomas if (subtree2)
1039 cc8021af 2021-10-12 thomas got_object_tree_close(subtree2);
1040 11528a82 2018-05-19 stsp if (blob1)
1041 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
1042 11528a82 2018-05-19 stsp if (blob2)
1043 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
1044 474b4f94 2017-11-30 stsp return err;
1045 474b4f94 2017-11-30 stsp }
1046 11528a82 2018-05-19 stsp
1047 9b4458b4 2022-06-26 thomas static const struct got_error *
1048 82c78e96 2022-08-06 thomas show_object_id(struct got_diff_line **lines, size_t *nlines,
1049 82c78e96 2022-08-06 thomas const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1050 9b4458b4 2022-06-26 thomas {
1051 9b4458b4 2022-06-26 thomas const struct got_error *err;
1052 9b4458b4 2022-06-26 thomas int n;
1053 9b4458b4 2022-06-26 thomas off_t outoff = 0;
1054 9b4458b4 2022-06-26 thomas
1055 9b4458b4 2022-06-26 thomas n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1056 7e1f3a3d 2022-08-16 thomas if (n < 0)
1057 7e1f3a3d 2022-08-16 thomas return got_error_from_errno("fprintf");
1058 7e1f3a3d 2022-08-16 thomas
1059 82c78e96 2022-08-06 thomas if (lines != NULL && *lines != NULL) {
1060 9b4458b4 2022-06-26 thomas if (*nlines == 0) {
1061 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0,
1062 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
1063 9b4458b4 2022-06-26 thomas if (err)
1064 9b4458b4 2022-06-26 thomas return err;
1065 9b4458b4 2022-06-26 thomas } else
1066 82c78e96 2022-08-06 thomas outoff = (*lines)[*nlines - 1].offset;
1067 9b4458b4 2022-06-26 thomas
1068 9b4458b4 2022-06-26 thomas outoff += n;
1069 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
1070 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
1071 9b4458b4 2022-06-26 thomas if (err)
1072 9b4458b4 2022-06-26 thomas return err;
1073 9b4458b4 2022-06-26 thomas }
1074 9b4458b4 2022-06-26 thomas
1075 9b4458b4 2022-06-26 thomas return NULL;
1076 9b4458b4 2022-06-26 thomas }
1077 9b4458b4 2022-06-26 thomas
1078 9b4458b4 2022-06-26 thomas static const struct got_error *
1079 82c78e96 2022-08-06 thomas diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1080 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
1081 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
1082 0c6f49ba 2022-07-01 thomas struct got_pathlist_head *paths, const char *label1, const char *label2,
1083 0c6f49ba 2022-07-01 thomas int diff_context, int ignore_whitespace, int force_text_diff,
1084 25ec7006 2022-07-01 thomas struct got_repository *repo, FILE *outfile,
1085 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo)
1086 11528a82 2018-05-19 stsp {
1087 11528a82 2018-05-19 stsp const struct got_error *err;
1088 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1089 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1090 82c78e96 2022-08-06 thomas int want_linemeta = (lines != NULL && *lines != NULL);
1091 11528a82 2018-05-19 stsp
1092 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
1093 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1094 b74c7625 2018-05-20 stsp
1095 15a94983 2018-12-23 stsp if (id1) {
1096 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
1097 cd0acaa7 2018-05-20 stsp if (err)
1098 cd0acaa7 2018-05-20 stsp goto done;
1099 cd0acaa7 2018-05-20 stsp }
1100 15a94983 2018-12-23 stsp if (id2) {
1101 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
1102 cd0acaa7 2018-05-20 stsp if (err)
1103 cd0acaa7 2018-05-20 stsp goto done;
1104 cd0acaa7 2018-05-20 stsp }
1105 cc8021af 2021-10-12 thomas
1106 25ec7006 2022-07-01 thomas arg.diff_algo = diff_algo;
1107 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1108 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1109 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
1110 aaa13589 2019-06-01 stsp arg.outfile = outfile;
1111 82c78e96 2022-08-06 thomas if (want_linemeta) {
1112 82c78e96 2022-08-06 thomas arg.lines = *lines;
1113 fe621944 2020-11-10 stsp arg.nlines = *nlines;
1114 fe621944 2020-11-10 stsp } else {
1115 82c78e96 2022-08-06 thomas arg.lines = NULL;
1116 fe621944 2020-11-10 stsp arg.nlines = 0;
1117 fe621944 2020-11-10 stsp }
1118 cc8021af 2021-10-12 thomas if (paths == NULL || TAILQ_EMPTY(paths)) {
1119 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
1120 19a6a6b5 2022-07-01 thomas label1, label2, repo,
1121 19a6a6b5 2022-07-01 thomas got_diff_blob_output_unidiff, &arg, 1);
1122 cc8021af 2021-10-12 thomas } else {
1123 19a6a6b5 2022-07-01 thomas err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1124 cc8021af 2021-10-12 thomas got_diff_blob_output_unidiff, &arg);
1125 cc8021af 2021-10-12 thomas }
1126 82c78e96 2022-08-06 thomas if (want_linemeta) {
1127 82c78e96 2022-08-06 thomas *lines = arg.lines; /* was likely re-allocated */
1128 fe621944 2020-11-10 stsp *nlines = arg.nlines;
1129 fe621944 2020-11-10 stsp }
1130 11528a82 2018-05-19 stsp done:
1131 11528a82 2018-05-19 stsp if (tree1)
1132 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
1133 11528a82 2018-05-19 stsp if (tree2)
1134 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
1135 11528a82 2018-05-19 stsp return err;
1136 11528a82 2018-05-19 stsp }
1137 11528a82 2018-05-19 stsp
1138 11528a82 2018-05-19 stsp const struct got_error *
1139 82c78e96 2022-08-06 thomas got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1140 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
1141 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
1142 0c6f49ba 2022-07-01 thomas struct got_pathlist_head *paths, const char *label1, const char *label2,
1143 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1144 25ec7006 2022-07-01 thomas int force_text_diff, struct got_repository *repo, FILE *outfile)
1145 9b4458b4 2022-06-26 thomas {
1146 9b4458b4 2022-06-26 thomas const struct got_error *err;
1147 9b4458b4 2022-06-26 thomas char *idstr = NULL;
1148 9b4458b4 2022-06-26 thomas
1149 9b4458b4 2022-06-26 thomas if (id1 == NULL && id2 == NULL)
1150 9b4458b4 2022-06-26 thomas return got_error(GOT_ERR_NO_OBJ);
1151 9b4458b4 2022-06-26 thomas
1152 9b4458b4 2022-06-26 thomas if (id1) {
1153 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id1);
1154 9b4458b4 2022-06-26 thomas if (err)
1155 9b4458b4 2022-06-26 thomas goto done;
1156 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1157 9b4458b4 2022-06-26 thomas if (err)
1158 9b4458b4 2022-06-26 thomas goto done;
1159 9b4458b4 2022-06-26 thomas free(idstr);
1160 9b4458b4 2022-06-26 thomas idstr = NULL;
1161 9b4458b4 2022-06-26 thomas } else {
1162 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1163 82c78e96 2022-08-06 thomas outfile);
1164 9b4458b4 2022-06-26 thomas if (err)
1165 9b4458b4 2022-06-26 thomas goto done;
1166 9b4458b4 2022-06-26 thomas }
1167 9b4458b4 2022-06-26 thomas
1168 9b4458b4 2022-06-26 thomas if (id2) {
1169 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id2);
1170 9b4458b4 2022-06-26 thomas if (err)
1171 9b4458b4 2022-06-26 thomas goto done;
1172 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1173 9b4458b4 2022-06-26 thomas if (err)
1174 9b4458b4 2022-06-26 thomas goto done;
1175 9b4458b4 2022-06-26 thomas free(idstr);
1176 9b4458b4 2022-06-26 thomas idstr = NULL;
1177 9b4458b4 2022-06-26 thomas } else {
1178 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1179 82c78e96 2022-08-06 thomas outfile);
1180 9b4458b4 2022-06-26 thomas if (err)
1181 9b4458b4 2022-06-26 thomas goto done;
1182 9b4458b4 2022-06-26 thomas }
1183 9b4458b4 2022-06-26 thomas
1184 82c78e96 2022-08-06 thomas err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1185 82c78e96 2022-08-06 thomas paths, label1, label2, diff_context, ignore_whitespace,
1186 25ec7006 2022-07-01 thomas force_text_diff, repo, outfile, diff_algo);
1187 9b4458b4 2022-06-26 thomas done:
1188 9b4458b4 2022-06-26 thomas free(idstr);
1189 9b4458b4 2022-06-26 thomas return err;
1190 9b4458b4 2022-06-26 thomas }
1191 9b4458b4 2022-06-26 thomas
1192 9b4458b4 2022-06-26 thomas const struct got_error *
1193 82c78e96 2022-08-06 thomas got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1194 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
1195 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
1196 25ec7006 2022-07-01 thomas struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1197 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
1198 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
1199 11528a82 2018-05-19 stsp {
1200 11528a82 2018-05-19 stsp const struct got_error *err;
1201 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1202 9b4458b4 2022-06-26 thomas char *idstr = NULL;
1203 11528a82 2018-05-19 stsp
1204 15a94983 2018-12-23 stsp if (id2 == NULL)
1205 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1206 b74c7625 2018-05-20 stsp
1207 15a94983 2018-12-23 stsp if (id1) {
1208 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
1209 cd0acaa7 2018-05-20 stsp if (err)
1210 cd0acaa7 2018-05-20 stsp goto done;
1211 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id1);
1212 9b4458b4 2022-06-26 thomas if (err)
1213 9b4458b4 2022-06-26 thomas goto done;
1214 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "commit", '-', idstr,
1215 82c78e96 2022-08-06 thomas outfile);
1216 9b4458b4 2022-06-26 thomas if (err)
1217 9b4458b4 2022-06-26 thomas goto done;
1218 9b4458b4 2022-06-26 thomas free(idstr);
1219 9b4458b4 2022-06-26 thomas idstr = NULL;
1220 9b4458b4 2022-06-26 thomas } else {
1221 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1222 82c78e96 2022-08-06 thomas outfile);
1223 9b4458b4 2022-06-26 thomas if (err)
1224 9b4458b4 2022-06-26 thomas goto done;
1225 cd0acaa7 2018-05-20 stsp }
1226 bacc9935 2018-05-20 stsp
1227 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
1228 9b697879 2018-05-20 stsp if (err)
1229 9b697879 2018-05-20 stsp goto done;
1230 9b697879 2018-05-20 stsp
1231 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id2);
1232 9b4458b4 2022-06-26 thomas if (err)
1233 9b4458b4 2022-06-26 thomas goto done;
1234 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1235 9b4458b4 2022-06-26 thomas if (err)
1236 9b4458b4 2022-06-26 thomas goto done;
1237 9b4458b4 2022-06-26 thomas
1238 82c78e96 2022-08-06 thomas err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1239 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1240 cc8021af 2021-10-12 thomas got_object_commit_get_tree_id(commit2), paths, "", "",
1241 25ec7006 2022-07-01 thomas diff_context, ignore_whitespace, force_text_diff, repo, outfile,
1242 25ec7006 2022-07-01 thomas diff_algo);
1243 11528a82 2018-05-19 stsp done:
1244 11528a82 2018-05-19 stsp if (commit1)
1245 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
1246 11528a82 2018-05-19 stsp if (commit2)
1247 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
1248 9b4458b4 2022-06-26 thomas free(idstr);
1249 11528a82 2018-05-19 stsp return err;
1250 11528a82 2018-05-19 stsp }
1251 dc424a06 2019-08-07 stsp
1252 dc424a06 2019-08-07 stsp const struct got_error *
1253 fe621944 2020-11-10 stsp got_diff_files(struct got_diffreg_result **resultp,
1254 dd2e2f52 2022-07-01 thomas FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1255 dd2e2f52 2022-07-01 thomas const char *label2, int diff_context, int ignore_whitespace,
1256 25ec7006 2022-07-01 thomas int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1257 dc424a06 2019-08-07 stsp {
1258 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
1259 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
1260 dc424a06 2019-08-07 stsp
1261 fe621944 2020-11-10 stsp if (resultp)
1262 fe621944 2020-11-10 stsp *resultp = NULL;
1263 dc424a06 2019-08-07 stsp
1264 dc424a06 2019-08-07 stsp if (outfile) {
1265 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
1266 dd2e2f52 2022-07-01 thomas f1_exists ? label1 : "/dev/null");
1267 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
1268 dd2e2f52 2022-07-01 thomas f2_exists ? label2 : "/dev/null");
1269 dc424a06 2019-08-07 stsp }
1270 fe621944 2020-11-10 stsp
1271 25ec7006 2022-07-01 thomas err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1272 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
1273 fe621944 2020-11-10 stsp if (err)
1274 fe621944 2020-11-10 stsp goto done;
1275 fe621944 2020-11-10 stsp
1276 fe621944 2020-11-10 stsp if (outfile) {
1277 fe621944 2020-11-10 stsp err = got_diffreg_output(NULL, NULL, diffreg_result,
1278 dd2e2f52 2022-07-01 thomas f1_exists, f2_exists, label1, label2,
1279 1cb46f00 2020-11-21 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1280 dc424a06 2019-08-07 stsp if (err)
1281 dc424a06 2019-08-07 stsp goto done;
1282 dc424a06 2019-08-07 stsp }
1283 fe621944 2020-11-10 stsp
1284 dc424a06 2019-08-07 stsp done:
1285 fe621944 2020-11-10 stsp if (resultp && err == NULL)
1286 fe621944 2020-11-10 stsp *resultp = diffreg_result;
1287 fe621944 2020-11-10 stsp else if (diffreg_result) {
1288 fe621944 2020-11-10 stsp const struct got_error *free_err;
1289 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
1290 fe621944 2020-11-10 stsp if (free_err && err == NULL)
1291 fe621944 2020-11-10 stsp err = free_err;
1292 dc424a06 2019-08-07 stsp }
1293 fe621944 2020-11-10 stsp
1294 dc424a06 2019-08-07 stsp return err;
1295 dc424a06 2019-08-07 stsp }