1 74b37681 2019-02-07 stsp /* $OpenBSD: diff3.c,v 1.41 2016/10/18 21:06:52 millert Exp $ */
4 74b37681 2019-02-07 stsp * Copyright (C) Caldera International Inc. 2001-2002.
5 74b37681 2019-02-07 stsp * All rights reserved.
7 74b37681 2019-02-07 stsp * Redistribution and use in source and binary forms, with or without
8 74b37681 2019-02-07 stsp * modification, are permitted provided that the following conditions
10 74b37681 2019-02-07 stsp * 1. Redistributions of source code and documentation must retain the above
11 74b37681 2019-02-07 stsp * copyright notice, this list of conditions and the following disclaimer.
12 74b37681 2019-02-07 stsp * 2. Redistributions in binary form must reproduce the above copyright
13 74b37681 2019-02-07 stsp * notice, this list of conditions and the following disclaimer in the
14 74b37681 2019-02-07 stsp * documentation and/or other materials provided with the distribution.
15 74b37681 2019-02-07 stsp * 3. All advertising materials mentioning features or use of this software
16 74b37681 2019-02-07 stsp * must display the following acknowledgement:
17 74b37681 2019-02-07 stsp * This product includes software developed or owned by Caldera
18 74b37681 2019-02-07 stsp * International, Inc.
19 74b37681 2019-02-07 stsp * 4. Neither the name of Caldera International, Inc. nor the names of other
20 74b37681 2019-02-07 stsp * contributors may be used to endorse or promote products derived from
21 74b37681 2019-02-07 stsp * this software without specific prior written permission.
23 74b37681 2019-02-07 stsp * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
24 74b37681 2019-02-07 stsp * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
25 74b37681 2019-02-07 stsp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 74b37681 2019-02-07 stsp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 74b37681 2019-02-07 stsp * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
28 74b37681 2019-02-07 stsp * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 74b37681 2019-02-07 stsp * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 74b37681 2019-02-07 stsp * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 74b37681 2019-02-07 stsp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32 74b37681 2019-02-07 stsp * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33 74b37681 2019-02-07 stsp * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 74b37681 2019-02-07 stsp * POSSIBILITY OF SUCH DAMAGE.
37 74b37681 2019-02-07 stsp * Copyright (c) 1991, 1993
38 74b37681 2019-02-07 stsp * The Regents of the University of California. All rights reserved.
40 74b37681 2019-02-07 stsp * Redistribution and use in source and binary forms, with or without
41 74b37681 2019-02-07 stsp * modification, are permitted provided that the following conditions
43 74b37681 2019-02-07 stsp * 1. Redistributions of source code must retain the above copyright
44 74b37681 2019-02-07 stsp * notice, this list of conditions and the following disclaimer.
45 74b37681 2019-02-07 stsp * 2. Redistributions in binary form must reproduce the above copyright
46 74b37681 2019-02-07 stsp * notice, this list of conditions and the following disclaimer in the
47 74b37681 2019-02-07 stsp * documentation and/or other materials provided with the distribution.
48 74b37681 2019-02-07 stsp * 3. Neither the name of the University nor the names of its contributors
49 74b37681 2019-02-07 stsp * may be used to endorse or promote products derived from this software
50 74b37681 2019-02-07 stsp * without specific prior written permission.
52 74b37681 2019-02-07 stsp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 74b37681 2019-02-07 stsp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 74b37681 2019-02-07 stsp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 74b37681 2019-02-07 stsp * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 74b37681 2019-02-07 stsp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 74b37681 2019-02-07 stsp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 74b37681 2019-02-07 stsp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 74b37681 2019-02-07 stsp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 74b37681 2019-02-07 stsp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 74b37681 2019-02-07 stsp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 74b37681 2019-02-07 stsp * SUCH DAMAGE.
64 74b37681 2019-02-07 stsp * @(#)diff3.c 8.1 (Berkeley) 6/6/93
67 c03c6cb3 2019-02-08 stsp #include <sys/stat.h>
68 c03c6cb3 2019-02-08 stsp #include <sys/queue.h>
70 74b37681 2019-02-07 stsp #include <ctype.h>
71 c03c6cb3 2019-02-08 stsp #include <limits.h>
72 d7b5a0e8 2022-04-20 stsp #include <sha1.h>
73 5822e79e 2023-02-23 op #include <sha2.h>
74 74b37681 2019-02-07 stsp #include <stdio.h>
75 c03c6cb3 2019-02-08 stsp #include <stdarg.h>
76 74b37681 2019-02-07 stsp #include <stdlib.h>
77 74b37681 2019-02-07 stsp #include <string.h>
78 74b37681 2019-02-07 stsp #include <time.h>
79 74b37681 2019-02-07 stsp #include <unistd.h>
81 c03c6cb3 2019-02-08 stsp #include "got_error.h"
82 c03c6cb3 2019-02-08 stsp #include "got_opentemp.h"
83 c03c6cb3 2019-02-08 stsp #include "got_object.h"
84 4b752015 2022-06-30 stsp #include "got_diff.h"
86 c03c6cb3 2019-02-08 stsp #include "buf.h"
87 c03c6cb3 2019-02-08 stsp #include "rcsutil.h"
88 c03c6cb3 2019-02-08 stsp #include "got_lib_diff.h"
90 0d26ce36 2019-02-08 stsp #ifndef nitems
91 0d26ce36 2019-02-08 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
94 74b37681 2019-02-07 stsp /* diff3 - 3-way differential file comparison */
96 74b37681 2019-02-07 stsp /* diff3 [-ex3EX] d13 d23 f1 f2 f3 [m1 m3]
98 74b37681 2019-02-07 stsp * d13 = diff report on f1 vs f3
99 74b37681 2019-02-07 stsp * d23 = diff report on f2 vs f3
100 74b37681 2019-02-07 stsp * f1, f2, f3 the 3 files
101 74b37681 2019-02-07 stsp * if changes in f1 overlap with changes in f3, m1 and m3 are used
102 74b37681 2019-02-07 stsp * to mark the overlaps; otherwise, the file names f1 and f3 are used
103 74b37681 2019-02-07 stsp * (only for options E and X).
107 74b37681 2019-02-07 stsp * "from" is first in range of changed lines; "to" is last+1
108 74b37681 2019-02-07 stsp * from=to=line after point of insertion for added lines.
110 b2338eff 2019-10-09 stsp struct line_range {
115 b2338eff 2019-10-09 stsp struct off_range {
116 b2338eff 2019-10-09 stsp off_t from;
120 74b37681 2019-02-07 stsp struct diff {
121 b2338eff 2019-10-09 stsp struct line_range old;
122 b2338eff 2019-10-09 stsp struct line_range new;
123 d136cfcb 2019-10-12 stsp struct off_range oldo;
124 b2338eff 2019-10-09 stsp struct off_range newo;
127 078e971d 2019-02-08 stsp struct diff3_state {
128 078e971d 2019-02-08 stsp size_t szchanges;
130 078e971d 2019-02-08 stsp struct diff *d13;
131 078e971d 2019-02-08 stsp struct diff *d23;
134 078e971d 2019-02-08 stsp * "de" is used to gather editing scripts. These are later spewed out
135 078e971d 2019-02-08 stsp * in reverse order. Its first element must be all zero, the "new"
136 d136cfcb 2019-10-12 stsp * component of "de" contains line positions, and "oldo" and "newo"
137 d136cfcb 2019-10-12 stsp * components contain byte positions.
138 b2338eff 2019-10-09 stsp * Array overlap indicates which sections in "de" correspond to lines
139 b2338eff 2019-10-09 stsp * that are different in all three files.
141 078e971d 2019-02-08 stsp struct diff *de;
142 078e971d 2019-02-08 stsp char *overlap;
143 078e971d 2019-02-08 stsp int overlapcnt;
144 078e971d 2019-02-08 stsp FILE *fp[3];
145 078e971d 2019-02-08 stsp int cline[3]; /* # of the last-read line in each file (0-2) */
148 078e971d 2019-02-08 stsp * the latest known correspondence between line numbers of the 3 files
149 078e971d 2019-02-08 stsp * is stored in last[1-3];
151 078e971d 2019-02-08 stsp int last[4];
152 f69721c3 2019-10-21 stsp char f1mark[PATH_MAX];
153 f69721c3 2019-10-21 stsp char f2mark[PATH_MAX];
154 f69721c3 2019-10-21 stsp char f3mark[PATH_MAX];
158 5176f942 2019-02-08 stsp BUF *diffbuf;
162 d136cfcb 2019-10-12 stsp static const struct got_error *duplicate(int *, int, struct line_range *,
163 b2338eff 2019-10-09 stsp struct line_range *, struct diff3_state *);
164 02b87977 2019-10-06 stsp static const struct got_error *edit(struct diff *, int, int *,
165 02b87977 2019-10-06 stsp struct diff3_state *);
166 02b87977 2019-10-06 stsp static const struct got_error *getchange(char **, FILE *, struct diff3_state *);
167 02b87977 2019-10-06 stsp static const struct got_error *get_line(char **, FILE *, size_t *,
168 02b87977 2019-10-06 stsp struct diff3_state *);
169 74b37681 2019-02-07 stsp static int number(char **);
170 b90524b1 2019-02-08 stsp static const struct got_error *readin(size_t *, char *, struct diff **,
171 b90524b1 2019-02-08 stsp struct diff3_state *);
172 c03c6cb3 2019-02-08 stsp static int ed_patch_lines(struct rcs_lines *, struct rcs_lines *);
173 88775814 2019-10-09 stsp static const struct got_error *skip(size_t *, int, int, struct diff3_state *);
174 180dce26 2019-10-06 stsp static const struct got_error *edscript(int, struct diff3_state *);
175 180dce26 2019-10-06 stsp static const struct got_error *merge(size_t, size_t, struct diff3_state *);
176 c0df5966 2021-12-31 stsp static const struct got_error *prange(struct line_range *,
177 c0df5966 2021-12-31 stsp struct diff3_state *);
178 f571bb85 2019-10-06 stsp static const struct got_error *repos(int, struct diff3_state *);
179 078e971d 2019-02-08 stsp static const struct got_error *increase(struct diff3_state *);
180 c03c6cb3 2019-02-08 stsp static const struct got_error *diff3_internal(char *, char *, char *,
181 a551f215 2019-02-08 stsp char *, char *, const char *, const char *, struct diff3_state *,
182 f69721c3 2019-10-21 stsp const char *, const char *, const char *);
184 c03c6cb3 2019-02-08 stsp static const struct got_error *
185 5176f942 2019-02-08 stsp diff_output(BUF *diffbuf, const char *fmt, ...)
187 346b301a 2019-10-06 stsp const struct got_error *err = NULL;
188 c03c6cb3 2019-02-08 stsp va_list vap;
191 c03c6cb3 2019-02-08 stsp size_t newsize;
193 c03c6cb3 2019-02-08 stsp va_start(vap, fmt);
194 c03c6cb3 2019-02-08 stsp i = vasprintf(&str, fmt, vap);
195 c03c6cb3 2019-02-08 stsp va_end(vap);
196 c03c6cb3 2019-02-08 stsp if (i == -1)
197 638f9024 2019-05-13 stsp return got_error_from_errno("vasprintf");
198 346b301a 2019-10-06 stsp err = buf_append(&newsize, diffbuf, str, strlen(str));
200 346b301a 2019-10-06 stsp return err;
203 c03c6cb3 2019-02-08 stsp static const struct got_error*
204 fdf3c2d3 2021-06-17 stsp diffreg(BUF **d, const char *path1, const char *path2,
205 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo)
207 c03c6cb3 2019-02-08 stsp const struct got_error *err = NULL;
208 c03c6cb3 2019-02-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
209 c03c6cb3 2019-02-08 stsp char *outpath = NULL;
210 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
214 00fe21f2 2021-12-31 stsp f1 = fopen(path1, "re");
215 c03c6cb3 2019-02-08 stsp if (f1 == NULL) {
216 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", path1);
219 00fe21f2 2021-12-31 stsp f2 = fopen(path2, "re");
220 c03c6cb3 2019-02-08 stsp if (f1 == NULL) {
221 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", path2);
225 bb63914a 2020-02-17 stsp err = got_opentemp_named(&outpath, &outfile,
226 b90054ed 2022-11-01 stsp GOT_TMPDIR_STR "/got-diffreg", "");
230 0e039681 2021-11-15 stsp err = got_diffreg(&diffreg_result, f1, f2, diff_algo, 0, 0);
234 0e039681 2021-11-15 stsp if (diffreg_result) {
235 0e039681 2021-11-15 stsp struct diff_result *diff_result = diffreg_result->result;
236 0e039681 2021-11-15 stsp int atomizer_flags = (diff_result->left->atomizer_flags |
237 0e039681 2021-11-15 stsp diff_result->right->atomizer_flags);
238 0e039681 2021-11-15 stsp if ((atomizer_flags & DIFF_ATOMIZER_FOUND_BINARY_DATA)) {
239 0e039681 2021-11-15 stsp err = got_error(GOT_ERR_FILE_BINARY);
244 1cb46f00 2020-11-21 stsp err = got_diffreg_output(NULL, NULL, diffreg_result, 1, 1, "", "",
245 d671c313 2022-09-02 stsp GOT_DIFF_OUTPUT_PLAIN, 0, outfile);
249 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0) {
250 638f9024 2019-05-13 stsp err = got_error_from_errno2("fflush", outpath);
253 db590691 2021-06-02 stsp if (fseek(outfile, 0L, SEEK_SET) == -1) {
254 db590691 2021-06-02 stsp err = got_ferror(outfile, GOT_ERR_IO);
258 db590691 2021-06-02 stsp err = buf_load(d, outfile);
260 a551f215 2019-02-08 stsp if (outpath) {
261 68bda3b1 2019-10-09 stsp if (unlink(outpath) == -1 && err == NULL)
262 68bda3b1 2019-10-09 stsp err = got_error_from_errno2("unlink", outpath);
263 a551f215 2019-02-08 stsp free(outpath);
265 56b63ca4 2021-01-22 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
266 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
267 56b63ca4 2021-01-22 stsp if (f1 && fclose(f1) == EOF && err == NULL)
268 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
269 56b63ca4 2021-01-22 stsp if (f2 && fclose(f2) == EOF && err == NULL)
270 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
271 c03c6cb3 2019-02-08 stsp return err;
275 74b37681 2019-02-07 stsp * For merge(1).
277 c03c6cb3 2019-02-08 stsp const struct got_error *
278 db590691 2021-06-02 stsp got_merge_diff3(int *overlapcnt, int outfd, FILE *f1, FILE *f2,
279 fdf3c2d3 2021-06-17 stsp FILE *f3, const char *label1, const char *label2, const char *label3,
280 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo)
282 c03c6cb3 2019-02-08 stsp const struct got_error *err = NULL;
283 c03c6cb3 2019-02-08 stsp char *dp13, *dp23, *path1, *path2, *path3;
284 74b37681 2019-02-07 stsp BUF *b1, *b2, *b3, *d1, *d2, *diffb;
285 74b37681 2019-02-07 stsp u_char *data, *patch;
286 16aeacf7 2020-11-26 stsp size_t dlen, plen, i;
287 078e971d 2019-02-08 stsp struct diff3_state *d3s;
289 57ee5d50 2019-02-08 stsp *overlapcnt = 0;
291 078e971d 2019-02-08 stsp d3s = calloc(1, sizeof(*d3s));
292 078e971d 2019-02-08 stsp if (d3s == NULL)
293 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
295 74b37681 2019-02-07 stsp b1 = b2 = b3 = d1 = d2 = diffb = NULL;
296 74b37681 2019-02-07 stsp dp13 = dp23 = path1 = path2 = path3 = NULL;
297 74b37681 2019-02-07 stsp data = patch = NULL;
299 db590691 2021-06-02 stsp err = buf_load(&b1, f1);
302 db590691 2021-06-02 stsp err = buf_load(&b2, f2);
305 db590691 2021-06-02 stsp err = buf_load(&b3, f3);
309 575e8218 2019-10-07 stsp err = buf_alloc(&diffb, 128);
313 ea2819be 2023-05-29 op if (asprintf(&path1, GOT_TMPDIR_STR "/got-diff1.XXXXXXXXXX") == -1) {
314 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
317 ea2819be 2023-05-29 op if (asprintf(&path2, GOT_TMPDIR_STR "/got-diff2.XXXXXXXXXX") == -1) {
318 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
321 ea2819be 2023-05-29 op if (asprintf(&path3, GOT_TMPDIR_STR "/got-diff3.XXXXXXXXXX") == -1) {
322 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
326 96cbb597 2019-10-09 stsp err = buf_write_stmp(b1, path1);
329 96cbb597 2019-10-09 stsp err = buf_write_stmp(b2, path2);
332 96cbb597 2019-10-09 stsp err = buf_write_stmp(b3, path3);
336 74b37681 2019-02-07 stsp buf_free(b2);
339 fdf3c2d3 2021-06-17 stsp err = diffreg(&d1, path1, path3, diff_algo);
341 74b37681 2019-02-07 stsp buf_free(diffb);
342 74b37681 2019-02-07 stsp diffb = NULL;
346 fdf3c2d3 2021-06-17 stsp err = diffreg(&d2, path2, path3, diff_algo);
348 c03c6cb3 2019-02-08 stsp buf_free(diffb);
349 c03c6cb3 2019-02-08 stsp diffb = NULL;
353 bb63914a 2020-02-17 stsp if (asprintf(&dp13, GOT_TMPDIR_STR "/got-d13.XXXXXXXXXX") == -1) {
354 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
357 96cbb597 2019-10-09 stsp err = buf_write_stmp(d1, dp13);
361 74b37681 2019-02-07 stsp buf_free(d1);
364 bb63914a 2020-02-17 stsp if (asprintf(&dp23, GOT_TMPDIR_STR "/got-d23.XXXXXXXXXX") == -1) {
365 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
368 96cbb597 2019-10-09 stsp err = buf_write_stmp(d2, dp23);
372 74b37681 2019-02-07 stsp buf_free(d2);
375 5176f942 2019-02-08 stsp d3s->diffbuf = diffb;
376 a551f215 2019-02-08 stsp err = diff3_internal(dp13, dp23, path1, path2, path3,
377 f69721c3 2019-10-21 stsp label1, label3, d3s, label1, label2, label3);
379 74b37681 2019-02-07 stsp buf_free(diffb);
380 74b37681 2019-02-07 stsp diffb = NULL;
384 74b37681 2019-02-07 stsp plen = buf_len(diffb);
385 74b37681 2019-02-07 stsp patch = buf_release(diffb);
386 74b37681 2019-02-07 stsp dlen = buf_len(b1);
387 74b37681 2019-02-07 stsp data = buf_release(b1);
389 5a3ad9e0 2019-02-08 stsp diffb = rcs_patchfile(data, dlen, patch, plen, ed_patch_lines);
391 74b37681 2019-02-07 stsp buf_free(b2);
392 74b37681 2019-02-07 stsp buf_free(b3);
393 74b37681 2019-02-07 stsp buf_free(d1);
394 74b37681 2019-02-07 stsp buf_free(d2);
396 68bda3b1 2019-10-09 stsp if (unlink(path1) == -1 && err == NULL)
397 68bda3b1 2019-10-09 stsp err = got_error_from_errno2("unlink", path1);
398 68bda3b1 2019-10-09 stsp if (unlink(path2) == -1 && err == NULL)
399 68bda3b1 2019-10-09 stsp err = got_error_from_errno2("unlink", path2);
400 68bda3b1 2019-10-09 stsp if (unlink(path3) == -1 && err == NULL)
401 68bda3b1 2019-10-09 stsp err = got_error_from_errno2("unlink", path3);
402 68bda3b1 2019-10-09 stsp if (unlink(dp13) == -1 && err == NULL)
403 68bda3b1 2019-10-09 stsp err = got_error_from_errno2("unlink", dp13);
404 68bda3b1 2019-10-09 stsp if (unlink(dp23) == -1 && err == NULL)
405 68bda3b1 2019-10-09 stsp err = got_error_from_errno2("unlink", dp23);
407 74b37681 2019-02-07 stsp free(path1);
408 74b37681 2019-02-07 stsp free(path2);
409 74b37681 2019-02-07 stsp free(path3);
410 74b37681 2019-02-07 stsp free(dp13);
411 74b37681 2019-02-07 stsp free(dp23);
412 74b37681 2019-02-07 stsp free(data);
413 74b37681 2019-02-07 stsp free(patch);
415 0d26ce36 2019-02-08 stsp for (i = 0; i < nitems(d3s->fp); i++) {
416 56b63ca4 2021-01-22 stsp if (d3s->fp[i] && fclose(d3s->fp[i]) == EOF && err == NULL)
417 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
419 5a3ad9e0 2019-02-08 stsp if (err == NULL && diffb) {
420 62f59172 2019-02-08 stsp if (buf_write_fd(diffb, outfd) < 0)
421 638f9024 2019-05-13 stsp err = got_error_from_errno("buf_write_fd");
422 57ee5d50 2019-02-08 stsp *overlapcnt = d3s->overlapcnt;
425 62f59172 2019-02-08 stsp buf_free(diffb);
426 c03c6cb3 2019-02-08 stsp return err;
429 c03c6cb3 2019-02-08 stsp static const struct got_error *
430 c03c6cb3 2019-02-08 stsp diff3_internal(char *dp13, char *dp23, char *path1, char *path2, char *path3,
431 a551f215 2019-02-08 stsp const char *fmark, const char *rmark, struct diff3_state *d3s,
432 f69721c3 2019-10-21 stsp const char *label1, const char *label2, const char *label3)
434 c03c6cb3 2019-02-08 stsp const struct got_error *err = NULL;
435 74b37681 2019-02-07 stsp ssize_t m, n;
438 01a44956 2019-03-27 stsp i = snprintf(d3s->f1mark, sizeof(d3s->f1mark),
439 f69721c3 2019-10-21 stsp "%s%s%s", GOT_DIFF_CONFLICT_MARKER_BEGIN,
440 f69721c3 2019-10-21 stsp label1 ? " " : "", label1 ? label1 : "");
441 078e971d 2019-02-08 stsp if (i < 0 || i >= (int)sizeof(d3s->f1mark))
442 c03c6cb3 2019-02-08 stsp return got_error(GOT_ERR_NO_SPACE);
444 f69721c3 2019-10-21 stsp i = snprintf(d3s->f2mark, sizeof(d3s->f2mark),
445 f69721c3 2019-10-21 stsp "%s%s%s", GOT_DIFF_CONFLICT_MARKER_ORIG,
446 f69721c3 2019-10-21 stsp label2 ? " " : "", label2 ? label2 : "");
447 f69721c3 2019-10-21 stsp if (i < 0 || i >= (int)sizeof(d3s->f2mark))
448 f69721c3 2019-10-21 stsp return got_error(GOT_ERR_NO_SPACE);
450 01a44956 2019-03-27 stsp i = snprintf(d3s->f3mark, sizeof(d3s->f3mark),
451 f69721c3 2019-10-21 stsp "%s%s%s", GOT_DIFF_CONFLICT_MARKER_END,
452 f69721c3 2019-10-21 stsp label3 ? " " : "", label3 ? label3 : "");
453 078e971d 2019-02-08 stsp if (i < 0 || i >= (int)sizeof(d3s->f3mark))
454 c03c6cb3 2019-02-08 stsp return got_error(GOT_ERR_NO_SPACE);
456 078e971d 2019-02-08 stsp err = increase(d3s);
458 c03c6cb3 2019-02-08 stsp return err;
460 b90524b1 2019-02-08 stsp err = readin(&m, dp13, &d3s->d13, d3s);
462 b90524b1 2019-02-08 stsp return err;
463 b90524b1 2019-02-08 stsp err = readin(&n, dp23, &d3s->d23, d3s);
465 b90524b1 2019-02-08 stsp return err;
467 00fe21f2 2021-12-31 stsp if ((d3s->fp[0] = fopen(path1, "re")) == NULL)
468 00fe21f2 2021-12-31 stsp return got_error_from_errno2("fopen", path1);
469 00fe21f2 2021-12-31 stsp if ((d3s->fp[1] = fopen(path2, "re")) == NULL)
470 638f9024 2019-05-13 stsp return got_error_from_errno2("fopen", path2);
471 00fe21f2 2021-12-31 stsp if ((d3s->fp[2] = fopen(path3, "re")) == NULL)
472 638f9024 2019-05-13 stsp return got_error_from_errno2("fopen", path3);
474 180dce26 2019-10-06 stsp return merge(m, n, d3s);
478 74b37681 2019-02-07 stsp ed_patch_lines(struct rcs_lines *dlines, struct rcs_lines *plines)
480 74b37681 2019-02-07 stsp char op, *ep;
481 74b37681 2019-02-07 stsp struct rcs_line *sort, *lp, *dlp, *ndlp, *insert_after;
482 74b37681 2019-02-07 stsp int start, end, i, lineno;
483 74b37681 2019-02-07 stsp u_char tmp;
485 74b37681 2019-02-07 stsp dlp = TAILQ_FIRST(&(dlines->l_lines));
486 74b37681 2019-02-07 stsp lp = TAILQ_FIRST(&(plines->l_lines));
489 74b37681 2019-02-07 stsp for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
490 74b37681 2019-02-07 stsp lp = TAILQ_NEXT(lp, l_list)) {
491 74b37681 2019-02-07 stsp /* Skip blank lines */
492 74b37681 2019-02-07 stsp if (lp->l_len < 2)
495 74b37681 2019-02-07 stsp /* NUL-terminate line buffer for strtol() safety. */
496 74b37681 2019-02-07 stsp tmp = lp->l_line[lp->l_len - 1];
497 74b37681 2019-02-07 stsp lp->l_line[lp->l_len - 1] = '\0';
499 74b37681 2019-02-07 stsp /* len - 1 is NUL terminator so we use len - 2 for 'op' */
500 74b37681 2019-02-07 stsp op = lp->l_line[lp->l_len - 2];
501 74b37681 2019-02-07 stsp start = (int)strtol(lp->l_line, &ep, 10);
503 74b37681 2019-02-07 stsp /* Restore the last byte of the buffer */
504 74b37681 2019-02-07 stsp lp->l_line[lp->l_len - 1] = tmp;
506 74b37681 2019-02-07 stsp if (op == 'a') {
507 74b37681 2019-02-07 stsp if (start > dlines->l_nblines ||
508 74b37681 2019-02-07 stsp start < 0 || *ep != 'a')
510 74b37681 2019-02-07 stsp } else if (op == 'c') {
511 74b37681 2019-02-07 stsp if (start > dlines->l_nblines ||
512 74b37681 2019-02-07 stsp start < 0 || (*ep != ',' && *ep != 'c'))
515 74b37681 2019-02-07 stsp if (*ep == ',') {
517 74b37681 2019-02-07 stsp end = (int)strtol(ep, &ep, 10);
518 74b37681 2019-02-07 stsp if (end < 0 || *ep != 'c')
521 74b37681 2019-02-07 stsp end = start;
527 74b37681 2019-02-07 stsp if (dlp == NULL)
529 74b37681 2019-02-07 stsp if (dlp->l_lineno == start)
531 74b37681 2019-02-07 stsp if (dlp->l_lineno > start) {
532 74b37681 2019-02-07 stsp dlp = TAILQ_PREV(dlp, tqh, l_list);
533 74b37681 2019-02-07 stsp } else if (dlp->l_lineno < start) {
534 74b37681 2019-02-07 stsp ndlp = TAILQ_NEXT(dlp, l_list);
535 74b37681 2019-02-07 stsp if (ndlp->l_lineno > start)
537 74b37681 2019-02-07 stsp dlp = ndlp;
541 74b37681 2019-02-07 stsp if (dlp == NULL)
545 74b37681 2019-02-07 stsp if (op == 'c') {
546 74b37681 2019-02-07 stsp insert_after = TAILQ_PREV(dlp, tqh, l_list);
547 74b37681 2019-02-07 stsp for (i = 0; i <= (end - start); i++) {
548 74b37681 2019-02-07 stsp ndlp = TAILQ_NEXT(dlp, l_list);
549 74b37681 2019-02-07 stsp TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
550 74b37681 2019-02-07 stsp dlp = ndlp;
552 74b37681 2019-02-07 stsp dlp = insert_after;
555 74b37681 2019-02-07 stsp if (op == 'a' || op == 'c') {
558 74b37681 2019-02-07 stsp lp = TAILQ_NEXT(lp, l_list);
559 74b37681 2019-02-07 stsp if (lp == NULL)
562 acb4e1d1 2019-08-10 stsp if (lp->l_len == 2 &&
563 acb4e1d1 2019-08-10 stsp lp->l_line[0] == '.' &&
564 acb4e1d1 2019-08-10 stsp lp->l_line[1] == '\n')
567 f10244c0 2021-10-11 stsp if (lp->l_line[0] == ':') {
568 f10244c0 2021-10-11 stsp lp->l_line++;
569 f10244c0 2021-10-11 stsp lp->l_len--;
571 74b37681 2019-02-07 stsp TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
572 74b37681 2019-02-07 stsp TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
573 74b37681 2019-02-07 stsp lp, l_list);
576 74b37681 2019-02-07 stsp lp->l_lineno = start;
582 74b37681 2019-02-07 stsp * always resort lines as the markers might be put at the
583 74b37681 2019-02-07 stsp * same line as we first started editing.
585 74b37681 2019-02-07 stsp lineno = 0;
586 74b37681 2019-02-07 stsp TAILQ_FOREACH(sort, &(dlines->l_lines), l_list)
587 74b37681 2019-02-07 stsp sort->l_lineno = lineno++;
588 74b37681 2019-02-07 stsp dlines->l_nblines = lineno - 1;
591 74b37681 2019-02-07 stsp return (0);
595 74b37681 2019-02-07 stsp * Pick up the line numbers of all changes from one change file.
596 74b37681 2019-02-07 stsp * (This puts the numbers in a vector, which is not strictly necessary,
597 74b37681 2019-02-07 stsp * since the vector is processed in one sequential pass.
598 74b37681 2019-02-07 stsp * The vector could be optimized out of existence)
600 b90524b1 2019-02-08 stsp static const struct got_error *
601 b90524b1 2019-02-08 stsp readin(size_t *n, char *name, struct diff **dd, struct diff3_state *d3s)
603 b90524b1 2019-02-08 stsp const struct got_error *err = NULL;
605 74b37681 2019-02-07 stsp int a, b, c, d;
606 74b37681 2019-02-07 stsp char kind, *p;
607 937e0e58 2019-10-21 stsp size_t i = 0;
611 00fe21f2 2021-12-31 stsp f = fopen(name, "re");
612 971405a7 2019-10-09 stsp if (f == NULL)
613 638f9024 2019-05-13 stsp return got_error_from_errno2("fopen", name);
614 971405a7 2019-10-09 stsp err = getchange(&p, f, d3s);
617 02b87977 2019-10-06 stsp for (i = 0; p; i++) {
618 b90524b1 2019-02-08 stsp if (i >= d3s->szchanges - 1) {
619 b90524b1 2019-02-08 stsp err = increase(d3s);
623 74b37681 2019-02-07 stsp a = b = number(&p);
624 74b37681 2019-02-07 stsp if (*p == ',') {
626 74b37681 2019-02-07 stsp b = number(&p);
628 74b37681 2019-02-07 stsp kind = *p++;
629 74b37681 2019-02-07 stsp c = d = number(&p);
630 e19660d5 2019-10-06 stsp if (*p == ',') {
632 74b37681 2019-02-07 stsp d = number(&p);
634 74b37681 2019-02-07 stsp if (kind == 'a')
636 74b37681 2019-02-07 stsp if (kind == 'd')
640 74b37681 2019-02-07 stsp (*dd)[i].old.from = a;
641 74b37681 2019-02-07 stsp (*dd)[i].old.to = b;
642 74b37681 2019-02-07 stsp (*dd)[i].new.from = c;
643 74b37681 2019-02-07 stsp (*dd)[i].new.to = d;
645 971405a7 2019-10-09 stsp err = getchange(&p, f, d3s);
651 e19660d5 2019-10-06 stsp (*dd)[i].old.from = (*dd)[i - 1].old.to;
652 e19660d5 2019-10-06 stsp (*dd)[i].new.from = (*dd)[i - 1].new.to;
655 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF && err == NULL)
656 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
657 e4531130 2019-10-09 stsp if (err == NULL)
659 fb43ecf1 2019-02-11 stsp return err;
663 74b37681 2019-02-07 stsp number(char **lc)
668 74b37681 2019-02-07 stsp while (isdigit((unsigned char)(**lc)))
669 74b37681 2019-02-07 stsp nn = nn*10 + *(*lc)++ - '0';
671 74b37681 2019-02-07 stsp return (nn);
674 02b87977 2019-10-06 stsp static const struct got_error *
675 02b87977 2019-10-06 stsp getchange(char **line, FILE *b, struct diff3_state *d3s)
677 02b87977 2019-10-06 stsp const struct got_error *err = NULL;
679 02b87977 2019-10-06 stsp *line = NULL;
681 02b87977 2019-10-06 stsp if (*line && isdigit((unsigned char)(*line)[0]))
682 02b87977 2019-10-06 stsp return NULL;
683 02b87977 2019-10-06 stsp err = get_line(line, b, NULL, d3s);
685 02b87977 2019-10-06 stsp return err;
686 02b87977 2019-10-06 stsp } while (*line);
688 02b87977 2019-10-06 stsp return NULL;
691 02b87977 2019-10-06 stsp static const struct got_error *
692 02b87977 2019-10-06 stsp get_line(char **ret, FILE *b, size_t *n, struct diff3_state *d3s)
694 02b87977 2019-10-06 stsp const struct got_error *err = NULL;
695 6dea73df 2019-09-23 stsp char *cp = NULL;
696 5c5396e5 2023-02-27 op size_t size = 0;
697 500467ff 2019-09-25 hiltjo ssize_t len;
700 02b87977 2019-10-06 stsp *ret = NULL;
701 8383a50b 2021-10-17 stsp if (n != NULL)
704 6dea73df 2019-09-23 stsp len = getline(&cp, &size, b);
705 02b87977 2019-10-06 stsp if (len == -1) {
706 02b87977 2019-10-06 stsp if (ferror(b))
707 02b87977 2019-10-06 stsp err = got_error_from_errno("getline");
711 6dea73df 2019-09-23 stsp if (cp[len - 1] != '\n') {
713 6dea73df 2019-09-23 stsp if (len + 1 > size) {
714 6dea73df 2019-09-23 stsp new = realloc(cp, len + 1);
715 02b87977 2019-10-06 stsp if (new == NULL) {
716 02b87977 2019-10-06 stsp err = got_error_from_errno("realloc");
721 6dea73df 2019-09-23 stsp cp[len - 1] = '\n';
722 6dea73df 2019-09-23 stsp cp[len] = '\0';
725 6dea73df 2019-09-23 stsp free(d3s->buf);
726 02b87977 2019-10-06 stsp *ret = d3s->buf = cp;
728 74b37681 2019-02-07 stsp if (n != NULL)
732 02b87977 2019-10-06 stsp return err;
735 180dce26 2019-10-06 stsp static const struct got_error *
736 078e971d 2019-02-08 stsp merge(size_t m1, size_t m2, struct diff3_state *d3s)
738 180dce26 2019-10-06 stsp const struct got_error *err = NULL;
739 18b9ebab 2019-10-06 stsp struct diff *d1, *d2;
740 74b37681 2019-02-07 stsp int dpl, j, t1, t2;
742 078e971d 2019-02-08 stsp d1 = d3s->d13;
743 078e971d 2019-02-08 stsp d2 = d3s->d23;
746 078e971d 2019-02-08 stsp t1 = (d1 < d3s->d13 + m1);
747 078e971d 2019-02-08 stsp t2 = (d2 < d3s->d23 + m2);
748 74b37681 2019-02-07 stsp if (!t1 && !t2)
751 74b37681 2019-02-07 stsp /* first file is different from others */
752 74b37681 2019-02-07 stsp if (!t2 || (t1 && d1->new.to < d2->new.from)) {
753 74b37681 2019-02-07 stsp /* stuff peculiar to 1st file */
758 74b37681 2019-02-07 stsp /* second file is different from others */
759 74b37681 2019-02-07 stsp if (!t1 || (t2 && d2->new.to < d1->new.from)) {
765 74b37681 2019-02-07 stsp * Merge overlapping changes in first file
766 74b37681 2019-02-07 stsp * this happens after extension (see below).
768 078e971d 2019-02-08 stsp if (d1 + 1 < d3s->d13 + m1 && d1->new.to >= d1[1].new.from) {
769 74b37681 2019-02-07 stsp d1[1].old.from = d1->old.from;
770 74b37681 2019-02-07 stsp d1[1].new.from = d1->new.from;
775 74b37681 2019-02-07 stsp /* merge overlapping changes in second */
776 078e971d 2019-02-08 stsp if (d2 + 1 < d3s->d23 + m2 && d2->new.to >= d2[1].new.from) {
777 74b37681 2019-02-07 stsp d2[1].old.from = d2->old.from;
778 74b37681 2019-02-07 stsp d2[1].new.from = d2->new.from;
782 74b37681 2019-02-07 stsp /* stuff peculiar to third file or different in all */
783 74b37681 2019-02-07 stsp if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
784 d136cfcb 2019-10-12 stsp err = duplicate(&dpl, j, &d1->old, &d2->old, d3s);
786 180dce26 2019-10-06 stsp return err;
789 74b37681 2019-02-07 stsp * dpl = 0 means all files differ
790 74b37681 2019-02-07 stsp * dpl = 1 means files 1 and 2 identical
792 18b9ebab 2019-10-06 stsp err = edit(d1, dpl, &j, d3s);
794 18b9ebab 2019-10-06 stsp return err;
801 74b37681 2019-02-07 stsp * Overlapping changes from file 1 and 2; extend changes
802 74b37681 2019-02-07 stsp * appropriately to make them coincide.
804 74b37681 2019-02-07 stsp if (d1->new.from < d2->new.from) {
805 e19660d5 2019-10-06 stsp d2->old.from -= d2->new.from - d1->new.from;
806 74b37681 2019-02-07 stsp d2->new.from = d1->new.from;
807 74b37681 2019-02-07 stsp } else if (d2->new.from < d1->new.from) {
808 e19660d5 2019-10-06 stsp d1->old.from -= d1->new.from - d2->new.from;
809 74b37681 2019-02-07 stsp d1->new.from = d2->new.from;
811 74b37681 2019-02-07 stsp if (d1->new.to > d2->new.to) {
812 74b37681 2019-02-07 stsp d2->old.to += d1->new.to - d2->new.to;
813 74b37681 2019-02-07 stsp d2->new.to = d1->new.to;
814 74b37681 2019-02-07 stsp } else if (d2->new.to > d1->new.to) {
815 74b37681 2019-02-07 stsp d1->old.to += d2->new.to - d1->new.to;
816 74b37681 2019-02-07 stsp d1->new.to = d2->new.to;
820 078e971d 2019-02-08 stsp return (edscript(j, d3s));
824 74b37681 2019-02-07 stsp * print the range of line numbers, rold.from thru rold.to, as n1,n2 or n1
826 ce09ab8d 2019-10-06 stsp static const struct got_error *
827 b2338eff 2019-10-09 stsp prange(struct line_range *rold, struct diff3_state *d3s)
829 ce09ab8d 2019-10-06 stsp const struct got_error *err = NULL;
831 ce09ab8d 2019-10-06 stsp if (rold->to <= rold->from) {
832 ce09ab8d 2019-10-06 stsp err = diff_output(d3s->diffbuf, "%da\n", rold->from - 1);
834 ce09ab8d 2019-10-06 stsp return err;
836 ce09ab8d 2019-10-06 stsp err = diff_output(d3s->diffbuf, "%d", rold->from);
838 ce09ab8d 2019-10-06 stsp return err;
839 ce09ab8d 2019-10-06 stsp if (rold->to > rold->from + 1) {
840 ce09ab8d 2019-10-06 stsp err = diff_output(d3s->diffbuf, ",%d", rold->to - 1);
842 ce09ab8d 2019-10-06 stsp return err;
844 ce09ab8d 2019-10-06 stsp err = diff_output(d3s->diffbuf, "c\n");
846 ce09ab8d 2019-10-06 stsp return err;
849 ce09ab8d 2019-10-06 stsp return NULL;
853 88775814 2019-10-09 stsp * Skip to just before line number from in file "i".
854 88775814 2019-10-09 stsp * Return the number of bytes skipped in *nskipped.
856 02b87977 2019-10-06 stsp static const struct got_error *
857 88775814 2019-10-09 stsp skip(size_t *nskipped, int i, int from, struct diff3_state *d3s)
859 02b87977 2019-10-06 stsp const struct got_error *err = NULL;
860 9d4a5e89 2019-10-09 stsp size_t len, n;
861 74b37681 2019-02-07 stsp char *line;
863 02b87977 2019-10-06 stsp *nskipped = 0;
864 9d4a5e89 2019-10-09 stsp for (n = 0; d3s->cline[i] < from - 1; n += len) {
865 9d4a5e89 2019-10-09 stsp err = get_line(&line, d3s->fp[i], &len, d3s);
867 02b87977 2019-10-06 stsp return err;
868 078e971d 2019-02-08 stsp d3s->cline[i]++;
870 02b87977 2019-10-06 stsp *nskipped = n;
871 02b87977 2019-10-06 stsp return NULL;
875 180dce26 2019-10-06 stsp * Set *dpl to 1 or 0 according as the old range (in file 1) contains exactly
876 74b37681 2019-02-07 stsp * the same data as the new range (in file 2).
878 d136cfcb 2019-10-12 stsp * If this change could overlap, remember start/end offsets in file 2 so we
879 d136cfcb 2019-10-12 stsp * can write out the original lines of text if a merge conflict occurs.
881 180dce26 2019-10-06 stsp static const struct got_error *
882 d136cfcb 2019-10-12 stsp duplicate(int *dpl, int j, struct line_range *r1, struct line_range *r2,
883 b2338eff 2019-10-09 stsp struct diff3_state *d3s)
885 f571bb85 2019-10-06 stsp const struct got_error *err = NULL;
889 88775814 2019-10-09 stsp size_t nskipped;
894 e19660d5 2019-10-06 stsp if (r1->to - r1->from != r2->to - r2->from)
895 180dce26 2019-10-06 stsp return NULL;
897 50351dee 2019-10-09 stsp err = skip(&nskipped, 0, r1->from, d3s);
899 02b87977 2019-10-06 stsp return err;
900 50351dee 2019-10-09 stsp err = skip(&nskipped, 1, r2->from, d3s);
902 02b87977 2019-10-06 stsp return err;
904 d136cfcb 2019-10-12 stsp off = ftello(d3s->fp[1]);
905 d136cfcb 2019-10-12 stsp if (off == -1)
906 d136cfcb 2019-10-12 stsp return got_error_from_errno("ftello");
907 d136cfcb 2019-10-12 stsp d3s->de[j + 1].oldo.from = off; /* original lines start here */
910 e19660d5 2019-10-06 stsp for (nline = 0; nline < r1->to - r1->from; nline++) {
912 078e971d 2019-02-08 stsp c = getc(d3s->fp[0]);
913 078e971d 2019-02-08 stsp d = getc(d3s->fp[1]);
914 1fee9f40 2021-04-13 stsp if (c == EOF && d == EOF)
916 1fee9f40 2021-04-13 stsp else if (c == EOF)
917 1fee9f40 2021-04-13 stsp return got_ferror(d3s->fp[0], GOT_ERR_EOF);
918 1fee9f40 2021-04-13 stsp else if (d == EOF)
919 180dce26 2019-10-06 stsp return got_ferror(d3s->fp[1], GOT_ERR_EOF);
921 d136cfcb 2019-10-12 stsp if (c != d) {
922 d136cfcb 2019-10-12 stsp long orig_line_len = nchar;
923 d136cfcb 2019-10-12 stsp while (d != '\n') {
924 d136cfcb 2019-10-12 stsp d = getc(d3s->fp[1]);
925 d136cfcb 2019-10-12 stsp if (d == EOF)
927 d136cfcb 2019-10-12 stsp orig_line_len++;
929 d136cfcb 2019-10-12 stsp if (orig_line_len > nchar &&
930 d136cfcb 2019-10-12 stsp fseek(d3s->fp[1], -(orig_line_len - nchar),
931 d136cfcb 2019-10-12 stsp SEEK_CUR) == -1)
932 d136cfcb 2019-10-12 stsp return got_ferror(d3s->fp[1],
933 d136cfcb 2019-10-12 stsp GOT_ERR_IO);
934 abc59930 2021-09-05 naddy /* original lines end here */
935 d136cfcb 2019-10-12 stsp d3s->de[j + 1].oldo.to = off + orig_line_len;
936 d136cfcb 2019-10-12 stsp err = repos(nchar, d3s);
938 d136cfcb 2019-10-12 stsp return err;
939 d136cfcb 2019-10-12 stsp return NULL;
941 74b37681 2019-02-07 stsp } while (c != '\n');
944 15a5c949 2021-10-17 stsp /* original lines end here */
945 15a5c949 2021-10-17 stsp d3s->de[j + 1].oldo.to = off + nchar;
947 f571bb85 2019-10-06 stsp err = repos(nchar, d3s);
949 f571bb85 2019-10-06 stsp return err;
951 180dce26 2019-10-06 stsp return NULL;
954 f571bb85 2019-10-06 stsp static const struct got_error *
955 078e971d 2019-02-08 stsp repos(int nchar, struct diff3_state *d3s)
959 f571bb85 2019-10-06 stsp for (i = 0; i < 2; i++) {
960 f571bb85 2019-10-06 stsp if (fseek(d3s->fp[i], (long)-nchar, SEEK_CUR) == -1)
961 f571bb85 2019-10-06 stsp return got_ferror(d3s->fp[i], GOT_ERR_IO);
964 f571bb85 2019-10-06 stsp return NULL;
968 74b37681 2019-02-07 stsp * collect an editing script for later regurgitation
970 02b87977 2019-10-06 stsp static const struct got_error *
971 02b87977 2019-10-06 stsp edit(struct diff *diff, int fdup, int *j, struct diff3_state *d3s)
973 02b87977 2019-10-06 stsp const struct got_error *err = NULL;
974 88775814 2019-10-09 stsp size_t nskipped;
976 18b9ebab 2019-10-06 stsp if (((fdup + 1) & 3) == 0)
977 02b87977 2019-10-06 stsp return NULL;
979 02b87977 2019-10-06 stsp d3s->overlap[*j] = !fdup;
981 078e971d 2019-02-08 stsp d3s->overlapcnt++;
982 02b87977 2019-10-06 stsp d3s->de[*j].old.from = diff->old.from;
983 02b87977 2019-10-06 stsp d3s->de[*j].old.to = diff->old.to;
985 50351dee 2019-10-09 stsp err = skip(&nskipped, 2, diff->new.from, d3s);
987 02b87977 2019-10-06 stsp return err;
988 b2338eff 2019-10-09 stsp d3s->de[*j].newo.from = d3s->de[*j - 1].newo.to + nskipped;
990 50351dee 2019-10-09 stsp err = skip(&nskipped, 2, diff->new.to, d3s);
992 b2338eff 2019-10-09 stsp return err;
993 b2338eff 2019-10-09 stsp d3s->de[*j].newo.to = d3s->de[*j].newo.from + nskipped;
994 02b87977 2019-10-06 stsp return NULL;
997 74b37681 2019-02-07 stsp /* regurgitate */
998 180dce26 2019-10-06 stsp static const struct got_error *
999 078e971d 2019-02-08 stsp edscript(int n, struct diff3_state *d3s)
1001 ce09ab8d 2019-10-06 stsp const struct got_error *err = NULL;
1002 f10244c0 2021-10-11 stsp off_t len;
1003 f10244c0 2021-10-11 stsp char *line = NULL;
1004 f10244c0 2021-10-11 stsp size_t linesize = 0;
1005 90de04bc 2021-10-11 stsp ssize_t linelen = 0, k;
1007 74b37681 2019-02-07 stsp for (; n > 0; n--) {
1008 ce09ab8d 2019-10-06 stsp if (!d3s->overlap[n]) {
1009 ce09ab8d 2019-10-06 stsp err = prange(&d3s->de[n].old, d3s);
1011 ce09ab8d 2019-10-06 stsp return err;
1012 d136cfcb 2019-10-12 stsp } else if (d3s->de[n].oldo.from < d3s->de[n].oldo.to) {
1013 d136cfcb 2019-10-12 stsp /* Output a block of 3-way diff base file content. */
1014 f10244c0 2021-10-11 stsp err = diff_output(d3s->diffbuf, "%da\n:%s\n",
1015 f69721c3 2019-10-21 stsp d3s->de[n].old.to - 1, d3s->f2mark);
1017 d136cfcb 2019-10-12 stsp return err;
1018 d136cfcb 2019-10-12 stsp if (fseeko(d3s->fp[1], d3s->de[n].oldo.from, SEEK_SET)
1020 d136cfcb 2019-10-12 stsp return got_error_from_errno("fseeko");
1021 f10244c0 2021-10-11 stsp len = (d3s->de[n].oldo.to - d3s->de[n].oldo.from);
1022 f10244c0 2021-10-11 stsp for (k = 0; k < (ssize_t)len; k += linelen) {
1023 f10244c0 2021-10-11 stsp linelen = getline(&line, &linesize, d3s->fp[1]);
1024 f10244c0 2021-10-11 stsp if (linelen == -1) {
1025 21c17f12 2020-06-26 stsp if (feof(d3s->fp[1]))
1027 f10244c0 2021-10-11 stsp err = got_ferror(d3s->fp[1],
1028 d136cfcb 2019-10-12 stsp GOT_ERR_IO);
1029 f10244c0 2021-10-11 stsp goto done;
1031 f10244c0 2021-10-11 stsp err = diff_output(d3s->diffbuf, ":%s", line);
1033 f10244c0 2021-10-11 stsp goto done;
1035 f10244c0 2021-10-11 stsp err = diff_output(d3s->diffbuf, "%s%s\n",
1036 90de04bc 2021-10-11 stsp linelen > 0 && line[linelen] == '\n' ? ":" : "",
1037 d136cfcb 2019-10-12 stsp GOT_DIFF_CONFLICT_MARKER_SEP);
1039 f10244c0 2021-10-11 stsp goto done;
1041 f10244c0 2021-10-11 stsp err = diff_output(d3s->diffbuf, "%da\n:%s\n",
1042 01a44956 2019-03-27 stsp d3s->de[n].old.to -1, GOT_DIFF_CONFLICT_MARKER_SEP);
1044 f10244c0 2021-10-11 stsp goto done;
1046 b2338eff 2019-10-09 stsp if (fseeko(d3s->fp[2], d3s->de[n].newo.from, SEEK_SET)
1048 f10244c0 2021-10-11 stsp err = got_error_from_errno("fseek");
1049 f10244c0 2021-10-11 stsp goto done;
1051 f10244c0 2021-10-11 stsp len = (d3s->de[n].newo.to - d3s->de[n].newo.from);
1052 f10244c0 2021-10-11 stsp for (k = 0; k < (ssize_t)len; k += linelen) {
1053 f10244c0 2021-10-11 stsp linelen = getline(&line, &linesize, d3s->fp[2]);
1054 f10244c0 2021-10-11 stsp if (linelen == -1) {
1055 21c17f12 2020-06-26 stsp if (feof(d3s->fp[2]))
1057 f10244c0 2021-10-11 stsp err = got_ferror(d3s->fp[2], GOT_ERR_IO);
1058 f10244c0 2021-10-11 stsp goto done;
1060 f10244c0 2021-10-11 stsp err = diff_output(d3s->diffbuf, ":%s", line);
1062 f10244c0 2021-10-11 stsp goto done;
1065 ce09ab8d 2019-10-06 stsp if (!d3s->overlap[n]) {
1066 ce09ab8d 2019-10-06 stsp err = diff_output(d3s->diffbuf, ".\n");
1068 f10244c0 2021-10-11 stsp goto done;
1070 f10244c0 2021-10-11 stsp err = diff_output(d3s->diffbuf, "%s%s\n.\n",
1071 4f18c400 2021-10-16 naddy linelen > 0 && line[linelen] == '\n' ? ":" : "",
1072 f10244c0 2021-10-11 stsp d3s->f3mark);
1074 f10244c0 2021-10-11 stsp goto done;
1075 f10244c0 2021-10-11 stsp err = diff_output(d3s->diffbuf, "%da\n:%s\n.\n",
1076 5176f942 2019-02-08 stsp d3s->de[n].old.from - 1, d3s->f1mark);
1078 f10244c0 2021-10-11 stsp goto done;
1082 f10244c0 2021-10-11 stsp free(line);
1083 f10244c0 2021-10-11 stsp return err;
1086 c03c6cb3 2019-02-08 stsp static const struct got_error *
1087 078e971d 2019-02-08 stsp increase(struct diff3_state *d3s)
1089 74b37681 2019-02-07 stsp size_t newsz, incr;
1090 c03c6cb3 2019-02-08 stsp struct diff *d;
1093 74b37681 2019-02-07 stsp /* are the memset(3) calls needed? */
1094 078e971d 2019-02-08 stsp newsz = d3s->szchanges == 0 ? 64 : 2 * d3s->szchanges;
1095 078e971d 2019-02-08 stsp incr = newsz - d3s->szchanges;
1097 078e971d 2019-02-08 stsp d = reallocarray(d3s->d13, newsz, sizeof(*d3s->d13));
1098 c03c6cb3 2019-02-08 stsp if (d == NULL)
1099 638f9024 2019-05-13 stsp return got_error_from_errno("reallocarray");
1100 078e971d 2019-02-08 stsp d3s->d13 = d;
1101 078e971d 2019-02-08 stsp memset(d3s->d13 + d3s->szchanges, 0, incr * sizeof(*d3s->d13));
1103 078e971d 2019-02-08 stsp d = reallocarray(d3s->d23, newsz, sizeof(*d3s->d23));
1104 c03c6cb3 2019-02-08 stsp if (d == NULL)
1105 638f9024 2019-05-13 stsp return got_error_from_errno("reallocarray");
1106 078e971d 2019-02-08 stsp d3s->d23 = d;
1107 078e971d 2019-02-08 stsp memset(d3s->d23 + d3s->szchanges, 0, incr * sizeof(*d3s->d23));
1109 078e971d 2019-02-08 stsp d = reallocarray(d3s->de, newsz, sizeof(*d3s->de));
1110 c03c6cb3 2019-02-08 stsp if (d == NULL)
1111 638f9024 2019-05-13 stsp return got_error_from_errno("reallocarray");
1112 078e971d 2019-02-08 stsp d3s->de = d;
1113 078e971d 2019-02-08 stsp memset(d3s->de + d3s->szchanges, 0, incr * sizeof(*d3s->de));
1115 078e971d 2019-02-08 stsp s = reallocarray(d3s->overlap, newsz, sizeof(*d3s->overlap));
1116 c03c6cb3 2019-02-08 stsp if (s == NULL)
1117 638f9024 2019-05-13 stsp return got_error_from_errno("reallocarray");
1118 078e971d 2019-02-08 stsp d3s->overlap = s;
1119 078e971d 2019-02-08 stsp memset(d3s->overlap + d3s->szchanges, 0, incr * sizeof(*d3s->overlap));
1120 078e971d 2019-02-08 stsp d3s->szchanges = newsz;
1122 c03c6cb3 2019-02-08 stsp return NULL;