Blame


1 74b37681 2019-02-07 stsp /* $OpenBSD: diff3.c,v 1.41 2016/10/18 21:06:52 millert Exp $ */
2 74b37681 2019-02-07 stsp
3 74b37681 2019-02-07 stsp /*
4 74b37681 2019-02-07 stsp * Copyright (C) Caldera International Inc. 2001-2002.
5 74b37681 2019-02-07 stsp * All rights reserved.
6 74b37681 2019-02-07 stsp *
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
9 74b37681 2019-02-07 stsp * are met:
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.
22 74b37681 2019-02-07 stsp *
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.
35 74b37681 2019-02-07 stsp */
36 74b37681 2019-02-07 stsp /*-
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.
39 74b37681 2019-02-07 stsp *
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
42 74b37681 2019-02-07 stsp * are met:
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.
51 74b37681 2019-02-07 stsp *
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.
63 74b37681 2019-02-07 stsp *
64 74b37681 2019-02-07 stsp * @(#)diff3.c 8.1 (Berkeley) 6/6/93
65 74b37681 2019-02-07 stsp */
66 74b37681 2019-02-07 stsp
67 c03c6cb3 2019-02-08 stsp #include <sys/stat.h>
68 c03c6cb3 2019-02-08 stsp #include <sys/queue.h>
69 c03c6cb3 2019-02-08 stsp
70 74b37681 2019-02-07 stsp #include <ctype.h>
71 c03c6cb3 2019-02-08 stsp #include <limits.h>
72 74b37681 2019-02-07 stsp #include <stdio.h>
73 c03c6cb3 2019-02-08 stsp #include <stdarg.h>
74 74b37681 2019-02-07 stsp #include <stdlib.h>
75 74b37681 2019-02-07 stsp #include <string.h>
76 74b37681 2019-02-07 stsp #include <time.h>
77 74b37681 2019-02-07 stsp #include <unistd.h>
78 74b37681 2019-02-07 stsp
79 c03c6cb3 2019-02-08 stsp #include "got_error.h"
80 c03c6cb3 2019-02-08 stsp #include "got_opentemp.h"
81 c03c6cb3 2019-02-08 stsp #include "got_object.h"
82 74b37681 2019-02-07 stsp
83 c03c6cb3 2019-02-08 stsp #include "buf.h"
84 c03c6cb3 2019-02-08 stsp #include "rcsutil.h"
85 c03c6cb3 2019-02-08 stsp #include "got_lib_diff.h"
86 c03c6cb3 2019-02-08 stsp
87 0d26ce36 2019-02-08 stsp #ifndef nitems
88 0d26ce36 2019-02-08 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
89 0d26ce36 2019-02-08 stsp #endif
90 c03c6cb3 2019-02-08 stsp
91 74b37681 2019-02-07 stsp /* diff3 - 3-way differential file comparison */
92 74b37681 2019-02-07 stsp
93 74b37681 2019-02-07 stsp /* diff3 [-ex3EX] d13 d23 f1 f2 f3 [m1 m3]
94 74b37681 2019-02-07 stsp *
95 74b37681 2019-02-07 stsp * d13 = diff report on f1 vs f3
96 74b37681 2019-02-07 stsp * d23 = diff report on f2 vs f3
97 74b37681 2019-02-07 stsp * f1, f2, f3 the 3 files
98 74b37681 2019-02-07 stsp * if changes in f1 overlap with changes in f3, m1 and m3 are used
99 74b37681 2019-02-07 stsp * to mark the overlaps; otherwise, the file names f1 and f3 are used
100 74b37681 2019-02-07 stsp * (only for options E and X).
101 74b37681 2019-02-07 stsp */
102 74b37681 2019-02-07 stsp
103 74b37681 2019-02-07 stsp /*
104 74b37681 2019-02-07 stsp * "from" is first in range of changed lines; "to" is last+1
105 74b37681 2019-02-07 stsp * from=to=line after point of insertion for added lines.
106 74b37681 2019-02-07 stsp */
107 b2338eff 2019-10-09 stsp struct line_range {
108 74b37681 2019-02-07 stsp int from;
109 74b37681 2019-02-07 stsp int to;
110 74b37681 2019-02-07 stsp };
111 74b37681 2019-02-07 stsp
112 b2338eff 2019-10-09 stsp struct off_range {
113 b2338eff 2019-10-09 stsp off_t from;
114 b2338eff 2019-10-09 stsp off_t to;
115 b2338eff 2019-10-09 stsp };
116 b2338eff 2019-10-09 stsp
117 74b37681 2019-02-07 stsp struct diff {
118 b2338eff 2019-10-09 stsp struct line_range old;
119 b2338eff 2019-10-09 stsp struct line_range new;
120 d136cfcb 2019-10-12 stsp struct off_range oldo;
121 b2338eff 2019-10-09 stsp struct off_range newo;
122 74b37681 2019-02-07 stsp };
123 74b37681 2019-02-07 stsp
124 078e971d 2019-02-08 stsp struct diff3_state {
125 078e971d 2019-02-08 stsp size_t szchanges;
126 74b37681 2019-02-07 stsp
127 078e971d 2019-02-08 stsp struct diff *d13;
128 078e971d 2019-02-08 stsp struct diff *d23;
129 74b37681 2019-02-07 stsp
130 078e971d 2019-02-08 stsp /*
131 078e971d 2019-02-08 stsp * "de" is used to gather editing scripts. These are later spewed out
132 078e971d 2019-02-08 stsp * in reverse order. Its first element must be all zero, the "new"
133 d136cfcb 2019-10-12 stsp * component of "de" contains line positions, and "oldo" and "newo"
134 d136cfcb 2019-10-12 stsp * components contain byte positions.
135 b2338eff 2019-10-09 stsp * Array overlap indicates which sections in "de" correspond to lines
136 b2338eff 2019-10-09 stsp * that are different in all three files.
137 078e971d 2019-02-08 stsp */
138 078e971d 2019-02-08 stsp struct diff *de;
139 078e971d 2019-02-08 stsp char *overlap;
140 078e971d 2019-02-08 stsp int overlapcnt;
141 078e971d 2019-02-08 stsp FILE *fp[3];
142 078e971d 2019-02-08 stsp int cline[3]; /* # of the last-read line in each file (0-2) */
143 74b37681 2019-02-07 stsp
144 078e971d 2019-02-08 stsp /*
145 078e971d 2019-02-08 stsp * the latest known correspondence between line numbers of the 3 files
146 078e971d 2019-02-08 stsp * is stored in last[1-3];
147 078e971d 2019-02-08 stsp */
148 078e971d 2019-02-08 stsp int last[4];
149 f69721c3 2019-10-21 stsp char f1mark[PATH_MAX];
150 f69721c3 2019-10-21 stsp char f2mark[PATH_MAX];
151 f69721c3 2019-10-21 stsp char f3mark[PATH_MAX];
152 dfc4f8fe 2019-02-08 stsp
153 dfc4f8fe 2019-02-08 stsp char *buf;
154 5176f942 2019-02-08 stsp
155 5176f942 2019-02-08 stsp BUF *diffbuf;
156 078e971d 2019-02-08 stsp };
157 078e971d 2019-02-08 stsp
158 c03c6cb3 2019-02-08 stsp
159 d136cfcb 2019-10-12 stsp static const struct got_error *duplicate(int *, int, struct line_range *,
160 b2338eff 2019-10-09 stsp struct line_range *, struct diff3_state *);
161 02b87977 2019-10-06 stsp static const struct got_error *edit(struct diff *, int, int *,
162 02b87977 2019-10-06 stsp struct diff3_state *);
163 02b87977 2019-10-06 stsp static const struct got_error *getchange(char **, FILE *, struct diff3_state *);
164 02b87977 2019-10-06 stsp static const struct got_error *get_line(char **, FILE *, size_t *,
165 02b87977 2019-10-06 stsp struct diff3_state *);
166 74b37681 2019-02-07 stsp static int number(char **);
167 b90524b1 2019-02-08 stsp static const struct got_error *readin(size_t *, char *, struct diff **,
168 b90524b1 2019-02-08 stsp struct diff3_state *);
169 c03c6cb3 2019-02-08 stsp static int ed_patch_lines(struct rcs_lines *, struct rcs_lines *);
170 88775814 2019-10-09 stsp static const struct got_error *skip(size_t *, int, int, struct diff3_state *);
171 180dce26 2019-10-06 stsp static const struct got_error *edscript(int, struct diff3_state *);
172 180dce26 2019-10-06 stsp static const struct got_error *merge(size_t, size_t, struct diff3_state *);
173 b2338eff 2019-10-09 stsp static const struct got_error *prange(struct line_range *, struct diff3_state *);
174 f571bb85 2019-10-06 stsp static const struct got_error *repos(int, struct diff3_state *);
175 078e971d 2019-02-08 stsp static const struct got_error *increase(struct diff3_state *);
176 c03c6cb3 2019-02-08 stsp static const struct got_error *diff3_internal(char *, char *, char *,
177 a551f215 2019-02-08 stsp char *, char *, const char *, const char *, struct diff3_state *,
178 f69721c3 2019-10-21 stsp const char *, const char *, const char *);
179 74b37681 2019-02-07 stsp
180 c03c6cb3 2019-02-08 stsp static const struct got_error *
181 5176f942 2019-02-08 stsp diff_output(BUF *diffbuf, const char *fmt, ...)
182 c03c6cb3 2019-02-08 stsp {
183 346b301a 2019-10-06 stsp const struct got_error *err = NULL;
184 c03c6cb3 2019-02-08 stsp va_list vap;
185 c03c6cb3 2019-02-08 stsp int i;
186 c03c6cb3 2019-02-08 stsp char *str;
187 c03c6cb3 2019-02-08 stsp size_t newsize;
188 c03c6cb3 2019-02-08 stsp
189 c03c6cb3 2019-02-08 stsp va_start(vap, fmt);
190 c03c6cb3 2019-02-08 stsp i = vasprintf(&str, fmt, vap);
191 c03c6cb3 2019-02-08 stsp va_end(vap);
192 c03c6cb3 2019-02-08 stsp if (i == -1)
193 638f9024 2019-05-13 stsp return got_error_from_errno("vasprintf");
194 346b301a 2019-10-06 stsp err = buf_append(&newsize, diffbuf, str, strlen(str));
195 c03c6cb3 2019-02-08 stsp free(str);
196 346b301a 2019-10-06 stsp return err;
197 c03c6cb3 2019-02-08 stsp }
198 c03c6cb3 2019-02-08 stsp
199 c03c6cb3 2019-02-08 stsp static const struct got_error*
200 fdf3c2d3 2021-06-17 stsp diffreg(BUF **d, const char *path1, const char *path2,
201 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo)
202 c03c6cb3 2019-02-08 stsp {
203 c03c6cb3 2019-02-08 stsp const struct got_error *err = NULL;
204 c03c6cb3 2019-02-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
205 c03c6cb3 2019-02-08 stsp char *outpath = NULL;
206 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
207 c03c6cb3 2019-02-08 stsp
208 c03c6cb3 2019-02-08 stsp *d = NULL;
209 c03c6cb3 2019-02-08 stsp
210 c03c6cb3 2019-02-08 stsp f1 = fopen(path1, "r");
211 c03c6cb3 2019-02-08 stsp if (f1 == NULL) {
212 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", path1);
213 c03c6cb3 2019-02-08 stsp goto done;
214 c03c6cb3 2019-02-08 stsp }
215 c03c6cb3 2019-02-08 stsp f2 = fopen(path2, "r");
216 c03c6cb3 2019-02-08 stsp if (f1 == NULL) {
217 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", path2);
218 c03c6cb3 2019-02-08 stsp goto done;
219 c03c6cb3 2019-02-08 stsp }
220 c03c6cb3 2019-02-08 stsp
221 bb63914a 2020-02-17 stsp err = got_opentemp_named(&outpath, &outfile,
222 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-diffreg");
223 c03c6cb3 2019-02-08 stsp if (err)
224 c03c6cb3 2019-02-08 stsp goto done;
225 c03c6cb3 2019-02-08 stsp
226 fdf3c2d3 2021-06-17 stsp err = got_diffreg(&diffreg_result, f1, f2, diff_algo, 0, 1);
227 fe621944 2020-11-10 stsp if (err)
228 fe621944 2020-11-10 stsp goto done;
229 c03c6cb3 2019-02-08 stsp
230 1cb46f00 2020-11-21 stsp err = got_diffreg_output(NULL, NULL, diffreg_result, 1, 1, "", "",
231 fe621944 2020-11-10 stsp GOT_DIFF_OUTPUT_EDSCRIPT, 0, outfile);
232 c03c6cb3 2019-02-08 stsp if (err)
233 c03c6cb3 2019-02-08 stsp goto done;
234 c03c6cb3 2019-02-08 stsp
235 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0) {
236 638f9024 2019-05-13 stsp err = got_error_from_errno2("fflush", outpath);
237 db590691 2021-06-02 stsp goto done;
238 db590691 2021-06-02 stsp }
239 db590691 2021-06-02 stsp if (fseek(outfile, 0L, SEEK_SET) == -1) {
240 db590691 2021-06-02 stsp err = got_ferror(outfile, GOT_ERR_IO);
241 cbe7f848 2019-02-11 stsp goto done;
242 cbe7f848 2019-02-11 stsp }
243 a551f215 2019-02-08 stsp
244 db590691 2021-06-02 stsp err = buf_load(d, outfile);
245 c03c6cb3 2019-02-08 stsp done:
246 a551f215 2019-02-08 stsp if (outpath) {
247 68bda3b1 2019-10-09 stsp if (unlink(outpath) == -1 && err == NULL)
248 68bda3b1 2019-10-09 stsp err = got_error_from_errno2("unlink", outpath);
249 a551f215 2019-02-08 stsp free(outpath);
250 a551f215 2019-02-08 stsp }
251 56b63ca4 2021-01-22 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
252 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
253 56b63ca4 2021-01-22 stsp if (f1 && fclose(f1) == EOF && err == NULL)
254 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
255 56b63ca4 2021-01-22 stsp if (f2 && fclose(f2) == EOF && err == NULL)
256 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
257 c03c6cb3 2019-02-08 stsp return err;
258 c03c6cb3 2019-02-08 stsp }
259 c03c6cb3 2019-02-08 stsp
260 74b37681 2019-02-07 stsp /*
261 74b37681 2019-02-07 stsp * For merge(1).
262 74b37681 2019-02-07 stsp */
263 c03c6cb3 2019-02-08 stsp const struct got_error *
264 db590691 2021-06-02 stsp got_merge_diff3(int *overlapcnt, int outfd, FILE *f1, FILE *f2,
265 fdf3c2d3 2021-06-17 stsp FILE *f3, const char *label1, const char *label2, const char *label3,
266 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo)
267 74b37681 2019-02-07 stsp {
268 c03c6cb3 2019-02-08 stsp const struct got_error *err = NULL;
269 c03c6cb3 2019-02-08 stsp char *dp13, *dp23, *path1, *path2, *path3;
270 74b37681 2019-02-07 stsp BUF *b1, *b2, *b3, *d1, *d2, *diffb;
271 74b37681 2019-02-07 stsp u_char *data, *patch;
272 16aeacf7 2020-11-26 stsp size_t dlen, plen, i;
273 078e971d 2019-02-08 stsp struct diff3_state *d3s;
274 5d731149 2019-02-08 stsp
275 57ee5d50 2019-02-08 stsp *overlapcnt = 0;
276 74b37681 2019-02-07 stsp
277 078e971d 2019-02-08 stsp d3s = calloc(1, sizeof(*d3s));
278 078e971d 2019-02-08 stsp if (d3s == NULL)
279 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
280 078e971d 2019-02-08 stsp
281 74b37681 2019-02-07 stsp b1 = b2 = b3 = d1 = d2 = diffb = NULL;
282 74b37681 2019-02-07 stsp dp13 = dp23 = path1 = path2 = path3 = NULL;
283 74b37681 2019-02-07 stsp data = patch = NULL;
284 74b37681 2019-02-07 stsp
285 db590691 2021-06-02 stsp err = buf_load(&b1, f1);
286 575e8218 2019-10-07 stsp if (err)
287 74b37681 2019-02-07 stsp goto out;
288 db590691 2021-06-02 stsp err = buf_load(&b2, f2);
289 575e8218 2019-10-07 stsp if (err)
290 74b37681 2019-02-07 stsp goto out;
291 db590691 2021-06-02 stsp err = buf_load(&b3, f3);
292 575e8218 2019-10-07 stsp if (err)
293 575e8218 2019-10-07 stsp goto out;
294 74b37681 2019-02-07 stsp
295 575e8218 2019-10-07 stsp err = buf_alloc(&diffb, 128);
296 575e8218 2019-10-07 stsp if (err)
297 575e8218 2019-10-07 stsp goto out;
298 74b37681 2019-02-07 stsp
299 bb63914a 2020-02-17 stsp if (asprintf(&path1, GOT_TMPDIR_STR "/got-diff1.XXXXXXXX") == -1) {
300 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
301 74b37681 2019-02-07 stsp goto out;
302 74b37681 2019-02-07 stsp }
303 bb63914a 2020-02-17 stsp if (asprintf(&path2, GOT_TMPDIR_STR "/got-diff2.XXXXXXXX") == -1) {
304 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
305 74b37681 2019-02-07 stsp goto out;
306 74b37681 2019-02-07 stsp }
307 bb63914a 2020-02-17 stsp if (asprintf(&path3, GOT_TMPDIR_STR "/got-diff3.XXXXXXXX") == -1) {
308 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
309 74b37681 2019-02-07 stsp goto out;
310 c03c6cb3 2019-02-08 stsp }
311 74b37681 2019-02-07 stsp
312 96cbb597 2019-10-09 stsp err = buf_write_stmp(b1, path1);
313 c03c6cb3 2019-02-08 stsp if (err)
314 74b37681 2019-02-07 stsp goto out;
315 96cbb597 2019-10-09 stsp err = buf_write_stmp(b2, path2);
316 c03c6cb3 2019-02-08 stsp if (err)
317 74b37681 2019-02-07 stsp goto out;
318 96cbb597 2019-10-09 stsp err = buf_write_stmp(b3, path3);
319 c03c6cb3 2019-02-08 stsp if (err)
320 74b37681 2019-02-07 stsp goto out;
321 74b37681 2019-02-07 stsp
322 74b37681 2019-02-07 stsp buf_free(b2);
323 74b37681 2019-02-07 stsp b2 = NULL;
324 74b37681 2019-02-07 stsp
325 fdf3c2d3 2021-06-17 stsp err = diffreg(&d1, path1, path3, diff_algo);
326 c03c6cb3 2019-02-08 stsp if (err) {
327 74b37681 2019-02-07 stsp buf_free(diffb);
328 74b37681 2019-02-07 stsp diffb = NULL;
329 74b37681 2019-02-07 stsp goto out;
330 c03c6cb3 2019-02-08 stsp
331 74b37681 2019-02-07 stsp }
332 fdf3c2d3 2021-06-17 stsp err = diffreg(&d2, path2, path3, diff_algo);
333 c03c6cb3 2019-02-08 stsp if (err) {
334 c03c6cb3 2019-02-08 stsp buf_free(diffb);
335 c03c6cb3 2019-02-08 stsp diffb = NULL;
336 c03c6cb3 2019-02-08 stsp goto out;
337 c03c6cb3 2019-02-08 stsp }
338 74b37681 2019-02-07 stsp
339 bb63914a 2020-02-17 stsp if (asprintf(&dp13, GOT_TMPDIR_STR "/got-d13.XXXXXXXXXX") == -1) {
340 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
341 c03c6cb3 2019-02-08 stsp goto out;
342 c03c6cb3 2019-02-08 stsp }
343 96cbb597 2019-10-09 stsp err = buf_write_stmp(d1, dp13);
344 c03c6cb3 2019-02-08 stsp if (err)
345 c03c6cb3 2019-02-08 stsp goto out;
346 74b37681 2019-02-07 stsp
347 74b37681 2019-02-07 stsp buf_free(d1);
348 74b37681 2019-02-07 stsp d1 = NULL;
349 74b37681 2019-02-07 stsp
350 bb63914a 2020-02-17 stsp if (asprintf(&dp23, GOT_TMPDIR_STR "/got-d23.XXXXXXXXXX") == -1) {
351 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
352 c03c6cb3 2019-02-08 stsp goto out;
353 c03c6cb3 2019-02-08 stsp }
354 96cbb597 2019-10-09 stsp err = buf_write_stmp(d2, dp23);
355 c03c6cb3 2019-02-08 stsp if (err)
356 c03c6cb3 2019-02-08 stsp goto out;
357 74b37681 2019-02-07 stsp
358 74b37681 2019-02-07 stsp buf_free(d2);
359 74b37681 2019-02-07 stsp d2 = NULL;
360 74b37681 2019-02-07 stsp
361 5176f942 2019-02-08 stsp d3s->diffbuf = diffb;
362 a551f215 2019-02-08 stsp err = diff3_internal(dp13, dp23, path1, path2, path3,
363 f69721c3 2019-10-21 stsp label1, label3, d3s, label1, label2, label3);
364 c03c6cb3 2019-02-08 stsp if (err) {
365 74b37681 2019-02-07 stsp buf_free(diffb);
366 74b37681 2019-02-07 stsp diffb = NULL;
367 74b37681 2019-02-07 stsp goto out;
368 74b37681 2019-02-07 stsp }
369 74b37681 2019-02-07 stsp
370 74b37681 2019-02-07 stsp plen = buf_len(diffb);
371 74b37681 2019-02-07 stsp patch = buf_release(diffb);
372 74b37681 2019-02-07 stsp dlen = buf_len(b1);
373 74b37681 2019-02-07 stsp data = buf_release(b1);
374 74b37681 2019-02-07 stsp
375 5a3ad9e0 2019-02-08 stsp diffb = rcs_patchfile(data, dlen, patch, plen, ed_patch_lines);
376 74b37681 2019-02-07 stsp out:
377 74b37681 2019-02-07 stsp buf_free(b2);
378 74b37681 2019-02-07 stsp buf_free(b3);
379 74b37681 2019-02-07 stsp buf_free(d1);
380 74b37681 2019-02-07 stsp buf_free(d2);
381 74b37681 2019-02-07 stsp
382 68bda3b1 2019-10-09 stsp if (unlink(path1) == -1 && err == NULL)
383 68bda3b1 2019-10-09 stsp err = got_error_from_errno2("unlink", path1);
384 68bda3b1 2019-10-09 stsp if (unlink(path2) == -1 && err == NULL)
385 68bda3b1 2019-10-09 stsp err = got_error_from_errno2("unlink", path2);
386 68bda3b1 2019-10-09 stsp if (unlink(path3) == -1 && err == NULL)
387 68bda3b1 2019-10-09 stsp err = got_error_from_errno2("unlink", path3);
388 68bda3b1 2019-10-09 stsp if (unlink(dp13) == -1 && err == NULL)
389 68bda3b1 2019-10-09 stsp err = got_error_from_errno2("unlink", dp13);
390 68bda3b1 2019-10-09 stsp if (unlink(dp23) == -1 && err == NULL)
391 68bda3b1 2019-10-09 stsp err = got_error_from_errno2("unlink", dp23);
392 74b37681 2019-02-07 stsp
393 74b37681 2019-02-07 stsp free(path1);
394 74b37681 2019-02-07 stsp free(path2);
395 74b37681 2019-02-07 stsp free(path3);
396 74b37681 2019-02-07 stsp free(dp13);
397 74b37681 2019-02-07 stsp free(dp23);
398 74b37681 2019-02-07 stsp free(data);
399 74b37681 2019-02-07 stsp free(patch);
400 c03c6cb3 2019-02-08 stsp
401 0d26ce36 2019-02-08 stsp for (i = 0; i < nitems(d3s->fp); i++) {
402 56b63ca4 2021-01-22 stsp if (d3s->fp[i] && fclose(d3s->fp[i]) == EOF && err == NULL)
403 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
404 0d26ce36 2019-02-08 stsp }
405 5a3ad9e0 2019-02-08 stsp if (err == NULL && diffb) {
406 62f59172 2019-02-08 stsp if (buf_write_fd(diffb, outfd) < 0)
407 638f9024 2019-05-13 stsp err = got_error_from_errno("buf_write_fd");
408 57ee5d50 2019-02-08 stsp *overlapcnt = d3s->overlapcnt;
409 62f59172 2019-02-08 stsp }
410 57ee5d50 2019-02-08 stsp free(d3s);
411 62f59172 2019-02-08 stsp buf_free(diffb);
412 c03c6cb3 2019-02-08 stsp return err;
413 74b37681 2019-02-07 stsp }
414 74b37681 2019-02-07 stsp
415 c03c6cb3 2019-02-08 stsp static const struct got_error *
416 c03c6cb3 2019-02-08 stsp diff3_internal(char *dp13, char *dp23, char *path1, char *path2, char *path3,
417 a551f215 2019-02-08 stsp const char *fmark, const char *rmark, struct diff3_state *d3s,
418 f69721c3 2019-10-21 stsp const char *label1, const char *label2, const char *label3)
419 74b37681 2019-02-07 stsp {
420 c03c6cb3 2019-02-08 stsp const struct got_error *err = NULL;
421 74b37681 2019-02-07 stsp ssize_t m, n;
422 74b37681 2019-02-07 stsp int i;
423 74b37681 2019-02-07 stsp
424 01a44956 2019-03-27 stsp i = snprintf(d3s->f1mark, sizeof(d3s->f1mark),
425 f69721c3 2019-10-21 stsp "%s%s%s", GOT_DIFF_CONFLICT_MARKER_BEGIN,
426 f69721c3 2019-10-21 stsp label1 ? " " : "", label1 ? label1 : "");
427 078e971d 2019-02-08 stsp if (i < 0 || i >= (int)sizeof(d3s->f1mark))
428 c03c6cb3 2019-02-08 stsp return got_error(GOT_ERR_NO_SPACE);
429 74b37681 2019-02-07 stsp
430 f69721c3 2019-10-21 stsp i = snprintf(d3s->f2mark, sizeof(d3s->f2mark),
431 f69721c3 2019-10-21 stsp "%s%s%s", GOT_DIFF_CONFLICT_MARKER_ORIG,
432 f69721c3 2019-10-21 stsp label2 ? " " : "", label2 ? label2 : "");
433 f69721c3 2019-10-21 stsp if (i < 0 || i >= (int)sizeof(d3s->f2mark))
434 f69721c3 2019-10-21 stsp return got_error(GOT_ERR_NO_SPACE);
435 f69721c3 2019-10-21 stsp
436 01a44956 2019-03-27 stsp i = snprintf(d3s->f3mark, sizeof(d3s->f3mark),
437 f69721c3 2019-10-21 stsp "%s%s%s", GOT_DIFF_CONFLICT_MARKER_END,
438 f69721c3 2019-10-21 stsp label3 ? " " : "", label3 ? label3 : "");
439 078e971d 2019-02-08 stsp if (i < 0 || i >= (int)sizeof(d3s->f3mark))
440 c03c6cb3 2019-02-08 stsp return got_error(GOT_ERR_NO_SPACE);
441 74b37681 2019-02-07 stsp
442 078e971d 2019-02-08 stsp err = increase(d3s);
443 c03c6cb3 2019-02-08 stsp if (err)
444 c03c6cb3 2019-02-08 stsp return err;
445 b90524b1 2019-02-08 stsp
446 b90524b1 2019-02-08 stsp err = readin(&m, dp13, &d3s->d13, d3s);
447 b90524b1 2019-02-08 stsp if (err)
448 b90524b1 2019-02-08 stsp return err;
449 b90524b1 2019-02-08 stsp err = readin(&n, dp23, &d3s->d23, d3s);
450 b90524b1 2019-02-08 stsp if (err)
451 b90524b1 2019-02-08 stsp return err;
452 74b37681 2019-02-07 stsp
453 078e971d 2019-02-08 stsp if ((d3s->fp[0] = fopen(path1, "r")) == NULL)
454 638f9024 2019-05-13 stsp return got_error_from_errno2("fopen", path1);
455 078e971d 2019-02-08 stsp if ((d3s->fp[1] = fopen(path2, "r")) == NULL)
456 638f9024 2019-05-13 stsp return got_error_from_errno2("fopen", path2);
457 078e971d 2019-02-08 stsp if ((d3s->fp[2] = fopen(path3, "r")) == NULL)
458 638f9024 2019-05-13 stsp return got_error_from_errno2("fopen", path3);
459 74b37681 2019-02-07 stsp
460 180dce26 2019-10-06 stsp return merge(m, n, d3s);
461 74b37681 2019-02-07 stsp }
462 74b37681 2019-02-07 stsp
463 c03c6cb3 2019-02-08 stsp static int
464 74b37681 2019-02-07 stsp ed_patch_lines(struct rcs_lines *dlines, struct rcs_lines *plines)
465 74b37681 2019-02-07 stsp {
466 74b37681 2019-02-07 stsp char op, *ep;
467 74b37681 2019-02-07 stsp struct rcs_line *sort, *lp, *dlp, *ndlp, *insert_after;
468 74b37681 2019-02-07 stsp int start, end, i, lineno;
469 74b37681 2019-02-07 stsp u_char tmp;
470 74b37681 2019-02-07 stsp
471 74b37681 2019-02-07 stsp dlp = TAILQ_FIRST(&(dlines->l_lines));
472 74b37681 2019-02-07 stsp lp = TAILQ_FIRST(&(plines->l_lines));
473 74b37681 2019-02-07 stsp
474 74b37681 2019-02-07 stsp end = 0;
475 74b37681 2019-02-07 stsp for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
476 74b37681 2019-02-07 stsp lp = TAILQ_NEXT(lp, l_list)) {
477 74b37681 2019-02-07 stsp /* Skip blank lines */
478 74b37681 2019-02-07 stsp if (lp->l_len < 2)
479 74b37681 2019-02-07 stsp continue;
480 74b37681 2019-02-07 stsp
481 74b37681 2019-02-07 stsp /* NUL-terminate line buffer for strtol() safety. */
482 74b37681 2019-02-07 stsp tmp = lp->l_line[lp->l_len - 1];
483 74b37681 2019-02-07 stsp lp->l_line[lp->l_len - 1] = '\0';
484 74b37681 2019-02-07 stsp
485 74b37681 2019-02-07 stsp /* len - 1 is NUL terminator so we use len - 2 for 'op' */
486 74b37681 2019-02-07 stsp op = lp->l_line[lp->l_len - 2];
487 74b37681 2019-02-07 stsp start = (int)strtol(lp->l_line, &ep, 10);
488 74b37681 2019-02-07 stsp
489 74b37681 2019-02-07 stsp /* Restore the last byte of the buffer */
490 74b37681 2019-02-07 stsp lp->l_line[lp->l_len - 1] = tmp;
491 74b37681 2019-02-07 stsp
492 74b37681 2019-02-07 stsp if (op == 'a') {
493 74b37681 2019-02-07 stsp if (start > dlines->l_nblines ||
494 74b37681 2019-02-07 stsp start < 0 || *ep != 'a')
495 c03c6cb3 2019-02-08 stsp return -1;
496 74b37681 2019-02-07 stsp } else if (op == 'c') {
497 74b37681 2019-02-07 stsp if (start > dlines->l_nblines ||
498 74b37681 2019-02-07 stsp start < 0 || (*ep != ',' && *ep != 'c'))
499 c03c6cb3 2019-02-08 stsp return -1;
500 74b37681 2019-02-07 stsp
501 74b37681 2019-02-07 stsp if (*ep == ',') {
502 74b37681 2019-02-07 stsp ep++;
503 74b37681 2019-02-07 stsp end = (int)strtol(ep, &ep, 10);
504 74b37681 2019-02-07 stsp if (end < 0 || *ep != 'c')
505 c03c6cb3 2019-02-08 stsp return -1;
506 74b37681 2019-02-07 stsp } else {
507 74b37681 2019-02-07 stsp end = start;
508 74b37681 2019-02-07 stsp }
509 74b37681 2019-02-07 stsp }
510 74b37681 2019-02-07 stsp
511 74b37681 2019-02-07 stsp
512 74b37681 2019-02-07 stsp for (;;) {
513 74b37681 2019-02-07 stsp if (dlp == NULL)
514 74b37681 2019-02-07 stsp break;
515 74b37681 2019-02-07 stsp if (dlp->l_lineno == start)
516 74b37681 2019-02-07 stsp break;
517 74b37681 2019-02-07 stsp if (dlp->l_lineno > start) {
518 74b37681 2019-02-07 stsp dlp = TAILQ_PREV(dlp, tqh, l_list);
519 74b37681 2019-02-07 stsp } else if (dlp->l_lineno < start) {
520 74b37681 2019-02-07 stsp ndlp = TAILQ_NEXT(dlp, l_list);
521 74b37681 2019-02-07 stsp if (ndlp->l_lineno > start)
522 74b37681 2019-02-07 stsp break;
523 74b37681 2019-02-07 stsp dlp = ndlp;
524 74b37681 2019-02-07 stsp }
525 74b37681 2019-02-07 stsp }
526 74b37681 2019-02-07 stsp
527 74b37681 2019-02-07 stsp if (dlp == NULL)
528 c03c6cb3 2019-02-08 stsp return -1;
529 74b37681 2019-02-07 stsp
530 74b37681 2019-02-07 stsp
531 74b37681 2019-02-07 stsp if (op == 'c') {
532 74b37681 2019-02-07 stsp insert_after = TAILQ_PREV(dlp, tqh, l_list);
533 74b37681 2019-02-07 stsp for (i = 0; i <= (end - start); i++) {
534 74b37681 2019-02-07 stsp ndlp = TAILQ_NEXT(dlp, l_list);
535 74b37681 2019-02-07 stsp TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
536 74b37681 2019-02-07 stsp dlp = ndlp;
537 74b37681 2019-02-07 stsp }
538 74b37681 2019-02-07 stsp dlp = insert_after;
539 74b37681 2019-02-07 stsp }
540 74b37681 2019-02-07 stsp
541 74b37681 2019-02-07 stsp if (op == 'a' || op == 'c') {
542 74b37681 2019-02-07 stsp for (;;) {
543 74b37681 2019-02-07 stsp ndlp = lp;
544 74b37681 2019-02-07 stsp lp = TAILQ_NEXT(lp, l_list);
545 74b37681 2019-02-07 stsp if (lp == NULL)
546 c03c6cb3 2019-02-08 stsp return -1;
547 74b37681 2019-02-07 stsp
548 acb4e1d1 2019-08-10 stsp if (lp->l_len == 2 &&
549 acb4e1d1 2019-08-10 stsp lp->l_line[0] == '.' &&
550 acb4e1d1 2019-08-10 stsp lp->l_line[1] == '\n')
551 74b37681 2019-02-07 stsp break;
552 74b37681 2019-02-07 stsp
553 f10244c0 2021-10-11 stsp if (lp->l_line[0] == ':') {
554 f10244c0 2021-10-11 stsp lp->l_line++;
555 f10244c0 2021-10-11 stsp lp->l_len--;
556 f10244c0 2021-10-11 stsp }
557 74b37681 2019-02-07 stsp TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
558 74b37681 2019-02-07 stsp TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
559 74b37681 2019-02-07 stsp lp, l_list);
560 74b37681 2019-02-07 stsp dlp = lp;
561 74b37681 2019-02-07 stsp
562 74b37681 2019-02-07 stsp lp->l_lineno = start;
563 74b37681 2019-02-07 stsp lp = ndlp;
564 74b37681 2019-02-07 stsp }
565 74b37681 2019-02-07 stsp }
566 74b37681 2019-02-07 stsp
567 74b37681 2019-02-07 stsp /*
568 74b37681 2019-02-07 stsp * always resort lines as the markers might be put at the
569 74b37681 2019-02-07 stsp * same line as we first started editing.
570 74b37681 2019-02-07 stsp */
571 74b37681 2019-02-07 stsp lineno = 0;
572 74b37681 2019-02-07 stsp TAILQ_FOREACH(sort, &(dlines->l_lines), l_list)
573 74b37681 2019-02-07 stsp sort->l_lineno = lineno++;
574 74b37681 2019-02-07 stsp dlines->l_nblines = lineno - 1;
575 74b37681 2019-02-07 stsp }
576 74b37681 2019-02-07 stsp
577 74b37681 2019-02-07 stsp return (0);
578 74b37681 2019-02-07 stsp }
579 74b37681 2019-02-07 stsp
580 74b37681 2019-02-07 stsp /*
581 74b37681 2019-02-07 stsp * Pick up the line numbers of all changes from one change file.
582 74b37681 2019-02-07 stsp * (This puts the numbers in a vector, which is not strictly necessary,
583 74b37681 2019-02-07 stsp * since the vector is processed in one sequential pass.
584 74b37681 2019-02-07 stsp * The vector could be optimized out of existence)
585 74b37681 2019-02-07 stsp */
586 b90524b1 2019-02-08 stsp static const struct got_error *
587 b90524b1 2019-02-08 stsp readin(size_t *n, char *name, struct diff **dd, struct diff3_state *d3s)
588 74b37681 2019-02-07 stsp {
589 b90524b1 2019-02-08 stsp const struct got_error *err = NULL;
590 971405a7 2019-10-09 stsp FILE *f;
591 74b37681 2019-02-07 stsp int a, b, c, d;
592 74b37681 2019-02-07 stsp char kind, *p;
593 937e0e58 2019-10-21 stsp size_t i = 0;
594 abc6a33f 2019-02-08 stsp
595 abc6a33f 2019-02-08 stsp *n = 0;
596 74b37681 2019-02-07 stsp
597 971405a7 2019-10-09 stsp f = fopen(name, "r");
598 971405a7 2019-10-09 stsp if (f == NULL)
599 638f9024 2019-05-13 stsp return got_error_from_errno2("fopen", name);
600 971405a7 2019-10-09 stsp err = getchange(&p, f, d3s);
601 02b87977 2019-10-06 stsp if (err)
602 e4531130 2019-10-09 stsp goto done;
603 02b87977 2019-10-06 stsp for (i = 0; p; i++) {
604 b90524b1 2019-02-08 stsp if (i >= d3s->szchanges - 1) {
605 b90524b1 2019-02-08 stsp err = increase(d3s);
606 b90524b1 2019-02-08 stsp if (err)
607 e4531130 2019-10-09 stsp goto done;
608 b90524b1 2019-02-08 stsp }
609 74b37681 2019-02-07 stsp a = b = number(&p);
610 74b37681 2019-02-07 stsp if (*p == ',') {
611 74b37681 2019-02-07 stsp p++;
612 74b37681 2019-02-07 stsp b = number(&p);
613 74b37681 2019-02-07 stsp }
614 74b37681 2019-02-07 stsp kind = *p++;
615 74b37681 2019-02-07 stsp c = d = number(&p);
616 e19660d5 2019-10-06 stsp if (*p == ',') {
617 74b37681 2019-02-07 stsp p++;
618 74b37681 2019-02-07 stsp d = number(&p);
619 74b37681 2019-02-07 stsp }
620 74b37681 2019-02-07 stsp if (kind == 'a')
621 74b37681 2019-02-07 stsp a++;
622 74b37681 2019-02-07 stsp if (kind == 'd')
623 74b37681 2019-02-07 stsp c++;
624 74b37681 2019-02-07 stsp b++;
625 74b37681 2019-02-07 stsp d++;
626 74b37681 2019-02-07 stsp (*dd)[i].old.from = a;
627 74b37681 2019-02-07 stsp (*dd)[i].old.to = b;
628 74b37681 2019-02-07 stsp (*dd)[i].new.from = c;
629 74b37681 2019-02-07 stsp (*dd)[i].new.to = d;
630 02b87977 2019-10-06 stsp
631 971405a7 2019-10-09 stsp err = getchange(&p, f, d3s);
632 02b87977 2019-10-06 stsp if (err)
633 e4531130 2019-10-09 stsp goto done;
634 74b37681 2019-02-07 stsp }
635 74b37681 2019-02-07 stsp
636 74b37681 2019-02-07 stsp if (i) {
637 e19660d5 2019-10-06 stsp (*dd)[i].old.from = (*dd)[i - 1].old.to;
638 e19660d5 2019-10-06 stsp (*dd)[i].new.from = (*dd)[i - 1].new.to;
639 74b37681 2019-02-07 stsp }
640 e4531130 2019-10-09 stsp done:
641 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF && err == NULL)
642 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
643 e4531130 2019-10-09 stsp if (err == NULL)
644 e4531130 2019-10-09 stsp *n = i;
645 fb43ecf1 2019-02-11 stsp return err;
646 74b37681 2019-02-07 stsp }
647 74b37681 2019-02-07 stsp
648 74b37681 2019-02-07 stsp static int
649 74b37681 2019-02-07 stsp number(char **lc)
650 74b37681 2019-02-07 stsp {
651 74b37681 2019-02-07 stsp int nn;
652 74b37681 2019-02-07 stsp
653 74b37681 2019-02-07 stsp nn = 0;
654 74b37681 2019-02-07 stsp while (isdigit((unsigned char)(**lc)))
655 74b37681 2019-02-07 stsp nn = nn*10 + *(*lc)++ - '0';
656 74b37681 2019-02-07 stsp
657 74b37681 2019-02-07 stsp return (nn);
658 74b37681 2019-02-07 stsp }
659 74b37681 2019-02-07 stsp
660 02b87977 2019-10-06 stsp static const struct got_error *
661 02b87977 2019-10-06 stsp getchange(char **line, FILE *b, struct diff3_state *d3s)
662 74b37681 2019-02-07 stsp {
663 02b87977 2019-10-06 stsp const struct got_error *err = NULL;
664 74b37681 2019-02-07 stsp
665 02b87977 2019-10-06 stsp *line = NULL;
666 02b87977 2019-10-06 stsp do {
667 02b87977 2019-10-06 stsp if (*line && isdigit((unsigned char)(*line)[0]))
668 02b87977 2019-10-06 stsp return NULL;
669 02b87977 2019-10-06 stsp err = get_line(line, b, NULL, d3s);
670 02b87977 2019-10-06 stsp if (err)
671 02b87977 2019-10-06 stsp return err;
672 02b87977 2019-10-06 stsp } while (*line);
673 74b37681 2019-02-07 stsp
674 02b87977 2019-10-06 stsp return NULL;
675 74b37681 2019-02-07 stsp }
676 74b37681 2019-02-07 stsp
677 02b87977 2019-10-06 stsp static const struct got_error *
678 02b87977 2019-10-06 stsp get_line(char **ret, FILE *b, size_t *n, struct diff3_state *d3s)
679 74b37681 2019-02-07 stsp {
680 02b87977 2019-10-06 stsp const struct got_error *err = NULL;
681 6dea73df 2019-09-23 stsp char *cp = NULL;
682 500467ff 2019-09-25 hiltjo size_t size;
683 500467ff 2019-09-25 hiltjo ssize_t len;
684 c03c6cb3 2019-02-08 stsp char *new;
685 74b37681 2019-02-07 stsp
686 02b87977 2019-10-06 stsp *ret = NULL;
687 8383a50b 2021-10-17 stsp if (n != NULL)
688 8383a50b 2021-10-17 stsp *n = 0;
689 02b87977 2019-10-06 stsp
690 6dea73df 2019-09-23 stsp len = getline(&cp, &size, b);
691 02b87977 2019-10-06 stsp if (len == -1) {
692 02b87977 2019-10-06 stsp if (ferror(b))
693 02b87977 2019-10-06 stsp err = got_error_from_errno("getline");
694 6dea73df 2019-09-23 stsp goto done;
695 02b87977 2019-10-06 stsp }
696 74b37681 2019-02-07 stsp
697 6dea73df 2019-09-23 stsp if (cp[len - 1] != '\n') {
698 74b37681 2019-02-07 stsp len++;
699 6dea73df 2019-09-23 stsp if (len + 1 > size) {
700 6dea73df 2019-09-23 stsp new = realloc(cp, len + 1);
701 02b87977 2019-10-06 stsp if (new == NULL) {
702 02b87977 2019-10-06 stsp err = got_error_from_errno("realloc");
703 6dea73df 2019-09-23 stsp goto done;
704 02b87977 2019-10-06 stsp }
705 6dea73df 2019-09-23 stsp cp = new;
706 6dea73df 2019-09-23 stsp }
707 6dea73df 2019-09-23 stsp cp[len - 1] = '\n';
708 6dea73df 2019-09-23 stsp cp[len] = '\0';
709 74b37681 2019-02-07 stsp }
710 6dea73df 2019-09-23 stsp
711 6dea73df 2019-09-23 stsp free(d3s->buf);
712 02b87977 2019-10-06 stsp *ret = d3s->buf = cp;
713 6dea73df 2019-09-23 stsp cp = NULL;
714 74b37681 2019-02-07 stsp if (n != NULL)
715 74b37681 2019-02-07 stsp *n = len;
716 6dea73df 2019-09-23 stsp done:
717 6dea73df 2019-09-23 stsp free(cp);
718 02b87977 2019-10-06 stsp return err;
719 74b37681 2019-02-07 stsp }
720 74b37681 2019-02-07 stsp
721 180dce26 2019-10-06 stsp static const struct got_error *
722 078e971d 2019-02-08 stsp merge(size_t m1, size_t m2, struct diff3_state *d3s)
723 74b37681 2019-02-07 stsp {
724 180dce26 2019-10-06 stsp const struct got_error *err = NULL;
725 18b9ebab 2019-10-06 stsp struct diff *d1, *d2;
726 74b37681 2019-02-07 stsp int dpl, j, t1, t2;
727 74b37681 2019-02-07 stsp
728 078e971d 2019-02-08 stsp d1 = d3s->d13;
729 078e971d 2019-02-08 stsp d2 = d3s->d23;
730 74b37681 2019-02-07 stsp j = 0;
731 74b37681 2019-02-07 stsp for (;;) {
732 078e971d 2019-02-08 stsp t1 = (d1 < d3s->d13 + m1);
733 078e971d 2019-02-08 stsp t2 = (d2 < d3s->d23 + m2);
734 74b37681 2019-02-07 stsp if (!t1 && !t2)
735 74b37681 2019-02-07 stsp break;
736 74b37681 2019-02-07 stsp
737 74b37681 2019-02-07 stsp /* first file is different from others */
738 74b37681 2019-02-07 stsp if (!t2 || (t1 && d1->new.to < d2->new.from)) {
739 74b37681 2019-02-07 stsp /* stuff peculiar to 1st file */
740 74b37681 2019-02-07 stsp d1++;
741 74b37681 2019-02-07 stsp continue;
742 74b37681 2019-02-07 stsp }
743 74b37681 2019-02-07 stsp
744 74b37681 2019-02-07 stsp /* second file is different from others */
745 74b37681 2019-02-07 stsp if (!t1 || (t2 && d2->new.to < d1->new.from)) {
746 74b37681 2019-02-07 stsp d2++;
747 74b37681 2019-02-07 stsp continue;
748 74b37681 2019-02-07 stsp }
749 74b37681 2019-02-07 stsp
750 74b37681 2019-02-07 stsp /*
751 74b37681 2019-02-07 stsp * Merge overlapping changes in first file
752 74b37681 2019-02-07 stsp * this happens after extension (see below).
753 74b37681 2019-02-07 stsp */
754 078e971d 2019-02-08 stsp if (d1 + 1 < d3s->d13 + m1 && d1->new.to >= d1[1].new.from) {
755 74b37681 2019-02-07 stsp d1[1].old.from = d1->old.from;
756 74b37681 2019-02-07 stsp d1[1].new.from = d1->new.from;
757 74b37681 2019-02-07 stsp d1++;
758 74b37681 2019-02-07 stsp continue;
759 74b37681 2019-02-07 stsp }
760 74b37681 2019-02-07 stsp
761 74b37681 2019-02-07 stsp /* merge overlapping changes in second */
762 078e971d 2019-02-08 stsp if (d2 + 1 < d3s->d23 + m2 && d2->new.to >= d2[1].new.from) {
763 74b37681 2019-02-07 stsp d2[1].old.from = d2->old.from;
764 74b37681 2019-02-07 stsp d2[1].new.from = d2->new.from;
765 74b37681 2019-02-07 stsp d2++;
766 74b37681 2019-02-07 stsp continue;
767 74b37681 2019-02-07 stsp }
768 74b37681 2019-02-07 stsp /* stuff peculiar to third file or different in all */
769 74b37681 2019-02-07 stsp if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
770 d136cfcb 2019-10-12 stsp err = duplicate(&dpl, j, &d1->old, &d2->old, d3s);
771 180dce26 2019-10-06 stsp if (err)
772 180dce26 2019-10-06 stsp return err;
773 74b37681 2019-02-07 stsp
774 74b37681 2019-02-07 stsp /*
775 74b37681 2019-02-07 stsp * dpl = 0 means all files differ
776 74b37681 2019-02-07 stsp * dpl = 1 means files 1 and 2 identical
777 74b37681 2019-02-07 stsp */
778 18b9ebab 2019-10-06 stsp err = edit(d1, dpl, &j, d3s);
779 18b9ebab 2019-10-06 stsp if (err)
780 18b9ebab 2019-10-06 stsp return err;
781 74b37681 2019-02-07 stsp d1++;
782 74b37681 2019-02-07 stsp d2++;
783 74b37681 2019-02-07 stsp continue;
784 74b37681 2019-02-07 stsp }
785 74b37681 2019-02-07 stsp
786 74b37681 2019-02-07 stsp /*
787 74b37681 2019-02-07 stsp * Overlapping changes from file 1 and 2; extend changes
788 74b37681 2019-02-07 stsp * appropriately to make them coincide.
789 74b37681 2019-02-07 stsp */
790 74b37681 2019-02-07 stsp if (d1->new.from < d2->new.from) {
791 e19660d5 2019-10-06 stsp d2->old.from -= d2->new.from - d1->new.from;
792 74b37681 2019-02-07 stsp d2->new.from = d1->new.from;
793 74b37681 2019-02-07 stsp } else if (d2->new.from < d1->new.from) {
794 e19660d5 2019-10-06 stsp d1->old.from -= d1->new.from - d2->new.from;
795 74b37681 2019-02-07 stsp d1->new.from = d2->new.from;
796 74b37681 2019-02-07 stsp }
797 74b37681 2019-02-07 stsp if (d1->new.to > d2->new.to) {
798 74b37681 2019-02-07 stsp d2->old.to += d1->new.to - d2->new.to;
799 74b37681 2019-02-07 stsp d2->new.to = d1->new.to;
800 74b37681 2019-02-07 stsp } else if (d2->new.to > d1->new.to) {
801 74b37681 2019-02-07 stsp d1->old.to += d2->new.to - d1->new.to;
802 74b37681 2019-02-07 stsp d1->new.to = d2->new.to;
803 74b37681 2019-02-07 stsp }
804 74b37681 2019-02-07 stsp }
805 74b37681 2019-02-07 stsp
806 078e971d 2019-02-08 stsp return (edscript(j, d3s));
807 74b37681 2019-02-07 stsp }
808 74b37681 2019-02-07 stsp
809 74b37681 2019-02-07 stsp /*
810 74b37681 2019-02-07 stsp * print the range of line numbers, rold.from thru rold.to, as n1,n2 or n1
811 74b37681 2019-02-07 stsp */
812 ce09ab8d 2019-10-06 stsp static const struct got_error *
813 b2338eff 2019-10-09 stsp prange(struct line_range *rold, struct diff3_state *d3s)
814 74b37681 2019-02-07 stsp {
815 ce09ab8d 2019-10-06 stsp const struct got_error *err = NULL;
816 ce09ab8d 2019-10-06 stsp
817 ce09ab8d 2019-10-06 stsp if (rold->to <= rold->from) {
818 ce09ab8d 2019-10-06 stsp err = diff_output(d3s->diffbuf, "%da\n", rold->from - 1);
819 ce09ab8d 2019-10-06 stsp if (err)
820 ce09ab8d 2019-10-06 stsp return err;
821 ce09ab8d 2019-10-06 stsp } else {
822 ce09ab8d 2019-10-06 stsp err = diff_output(d3s->diffbuf, "%d", rold->from);
823 ce09ab8d 2019-10-06 stsp if (err)
824 ce09ab8d 2019-10-06 stsp return err;
825 ce09ab8d 2019-10-06 stsp if (rold->to > rold->from + 1) {
826 ce09ab8d 2019-10-06 stsp err = diff_output(d3s->diffbuf, ",%d", rold->to - 1);
827 ce09ab8d 2019-10-06 stsp if (err)
828 ce09ab8d 2019-10-06 stsp return err;
829 ce09ab8d 2019-10-06 stsp }
830 ce09ab8d 2019-10-06 stsp err = diff_output(d3s->diffbuf, "c\n");
831 ce09ab8d 2019-10-06 stsp if (err)
832 ce09ab8d 2019-10-06 stsp return err;
833 74b37681 2019-02-07 stsp }
834 ce09ab8d 2019-10-06 stsp
835 ce09ab8d 2019-10-06 stsp return NULL;
836 74b37681 2019-02-07 stsp }
837 74b37681 2019-02-07 stsp
838 88775814 2019-10-09 stsp /*
839 88775814 2019-10-09 stsp * Skip to just before line number from in file "i".
840 88775814 2019-10-09 stsp * Return the number of bytes skipped in *nskipped.
841 88775814 2019-10-09 stsp */
842 02b87977 2019-10-06 stsp static const struct got_error *
843 88775814 2019-10-09 stsp skip(size_t *nskipped, int i, int from, struct diff3_state *d3s)
844 74b37681 2019-02-07 stsp {
845 02b87977 2019-10-06 stsp const struct got_error *err = NULL;
846 9d4a5e89 2019-10-09 stsp size_t len, n;
847 74b37681 2019-02-07 stsp char *line;
848 74b37681 2019-02-07 stsp
849 02b87977 2019-10-06 stsp *nskipped = 0;
850 9d4a5e89 2019-10-09 stsp for (n = 0; d3s->cline[i] < from - 1; n += len) {
851 9d4a5e89 2019-10-09 stsp err = get_line(&line, d3s->fp[i], &len, d3s);
852 02b87977 2019-10-06 stsp if (err)
853 02b87977 2019-10-06 stsp return err;
854 078e971d 2019-02-08 stsp d3s->cline[i]++;
855 74b37681 2019-02-07 stsp }
856 02b87977 2019-10-06 stsp *nskipped = n;
857 02b87977 2019-10-06 stsp return NULL;
858 74b37681 2019-02-07 stsp }
859 74b37681 2019-02-07 stsp
860 74b37681 2019-02-07 stsp /*
861 180dce26 2019-10-06 stsp * Set *dpl to 1 or 0 according as the old range (in file 1) contains exactly
862 74b37681 2019-02-07 stsp * the same data as the new range (in file 2).
863 d136cfcb 2019-10-12 stsp *
864 d136cfcb 2019-10-12 stsp * If this change could overlap, remember start/end offsets in file 2 so we
865 d136cfcb 2019-10-12 stsp * can write out the original lines of text if a merge conflict occurs.
866 74b37681 2019-02-07 stsp */
867 180dce26 2019-10-06 stsp static const struct got_error *
868 d136cfcb 2019-10-12 stsp duplicate(int *dpl, int j, struct line_range *r1, struct line_range *r2,
869 b2338eff 2019-10-09 stsp struct diff3_state *d3s)
870 74b37681 2019-02-07 stsp {
871 f571bb85 2019-10-06 stsp const struct got_error *err = NULL;
872 74b37681 2019-02-07 stsp int c,d;
873 74b37681 2019-02-07 stsp int nchar;
874 88775814 2019-10-09 stsp int nline;
875 88775814 2019-10-09 stsp size_t nskipped;
876 d136cfcb 2019-10-12 stsp off_t off;
877 74b37681 2019-02-07 stsp
878 180dce26 2019-10-06 stsp *dpl = 0;
879 180dce26 2019-10-06 stsp
880 e19660d5 2019-10-06 stsp if (r1->to - r1->from != r2->to - r2->from)
881 180dce26 2019-10-06 stsp return NULL;
882 180dce26 2019-10-06 stsp
883 50351dee 2019-10-09 stsp err = skip(&nskipped, 0, r1->from, d3s);
884 02b87977 2019-10-06 stsp if (err)
885 02b87977 2019-10-06 stsp return err;
886 50351dee 2019-10-09 stsp err = skip(&nskipped, 1, r2->from, d3s);
887 02b87977 2019-10-06 stsp if (err)
888 02b87977 2019-10-06 stsp return err;
889 d136cfcb 2019-10-12 stsp
890 d136cfcb 2019-10-12 stsp off = ftello(d3s->fp[1]);
891 d136cfcb 2019-10-12 stsp if (off == -1)
892 d136cfcb 2019-10-12 stsp return got_error_from_errno("ftello");
893 d136cfcb 2019-10-12 stsp d3s->de[j + 1].oldo.from = off; /* original lines start here */
894 d136cfcb 2019-10-12 stsp
895 74b37681 2019-02-07 stsp nchar = 0;
896 e19660d5 2019-10-06 stsp for (nline = 0; nline < r1->to - r1->from; nline++) {
897 74b37681 2019-02-07 stsp do {
898 078e971d 2019-02-08 stsp c = getc(d3s->fp[0]);
899 078e971d 2019-02-08 stsp d = getc(d3s->fp[1]);
900 1fee9f40 2021-04-13 stsp if (c == EOF && d == EOF)
901 1fee9f40 2021-04-13 stsp break;
902 1fee9f40 2021-04-13 stsp else if (c == EOF)
903 1fee9f40 2021-04-13 stsp return got_ferror(d3s->fp[0], GOT_ERR_EOF);
904 1fee9f40 2021-04-13 stsp else if (d == EOF)
905 180dce26 2019-10-06 stsp return got_ferror(d3s->fp[1], GOT_ERR_EOF);
906 74b37681 2019-02-07 stsp nchar++;
907 d136cfcb 2019-10-12 stsp if (c != d) {
908 d136cfcb 2019-10-12 stsp long orig_line_len = nchar;
909 d136cfcb 2019-10-12 stsp while (d != '\n') {
910 d136cfcb 2019-10-12 stsp d = getc(d3s->fp[1]);
911 d136cfcb 2019-10-12 stsp if (d == EOF)
912 d136cfcb 2019-10-12 stsp break;
913 d136cfcb 2019-10-12 stsp orig_line_len++;
914 d136cfcb 2019-10-12 stsp }
915 d136cfcb 2019-10-12 stsp if (orig_line_len > nchar &&
916 d136cfcb 2019-10-12 stsp fseek(d3s->fp[1], -(orig_line_len - nchar),
917 d136cfcb 2019-10-12 stsp SEEK_CUR) == -1)
918 d136cfcb 2019-10-12 stsp return got_ferror(d3s->fp[1],
919 d136cfcb 2019-10-12 stsp GOT_ERR_IO);
920 abc59930 2021-09-05 naddy /* original lines end here */
921 d136cfcb 2019-10-12 stsp d3s->de[j + 1].oldo.to = off + orig_line_len;
922 d136cfcb 2019-10-12 stsp err = repos(nchar, d3s);
923 d136cfcb 2019-10-12 stsp if (err)
924 d136cfcb 2019-10-12 stsp return err;
925 d136cfcb 2019-10-12 stsp return NULL;
926 d136cfcb 2019-10-12 stsp }
927 74b37681 2019-02-07 stsp } while (c != '\n');
928 74b37681 2019-02-07 stsp }
929 15a5c949 2021-10-17 stsp
930 15a5c949 2021-10-17 stsp /* original lines end here */
931 15a5c949 2021-10-17 stsp d3s->de[j + 1].oldo.to = off + nchar;
932 15a5c949 2021-10-17 stsp
933 f571bb85 2019-10-06 stsp err = repos(nchar, d3s);
934 f571bb85 2019-10-06 stsp if (err)
935 f571bb85 2019-10-06 stsp return err;
936 180dce26 2019-10-06 stsp *dpl = 1;
937 180dce26 2019-10-06 stsp return NULL;
938 74b37681 2019-02-07 stsp }
939 74b37681 2019-02-07 stsp
940 f571bb85 2019-10-06 stsp static const struct got_error *
941 078e971d 2019-02-08 stsp repos(int nchar, struct diff3_state *d3s)
942 74b37681 2019-02-07 stsp {
943 74b37681 2019-02-07 stsp int i;
944 74b37681 2019-02-07 stsp
945 f571bb85 2019-10-06 stsp for (i = 0; i < 2; i++) {
946 f571bb85 2019-10-06 stsp if (fseek(d3s->fp[i], (long)-nchar, SEEK_CUR) == -1)
947 f571bb85 2019-10-06 stsp return got_ferror(d3s->fp[i], GOT_ERR_IO);
948 f571bb85 2019-10-06 stsp }
949 f571bb85 2019-10-06 stsp
950 f571bb85 2019-10-06 stsp return NULL;
951 74b37681 2019-02-07 stsp }
952 74b37681 2019-02-07 stsp
953 74b37681 2019-02-07 stsp /*
954 74b37681 2019-02-07 stsp * collect an editing script for later regurgitation
955 74b37681 2019-02-07 stsp */
956 02b87977 2019-10-06 stsp static const struct got_error *
957 02b87977 2019-10-06 stsp edit(struct diff *diff, int fdup, int *j, struct diff3_state *d3s)
958 74b37681 2019-02-07 stsp {
959 02b87977 2019-10-06 stsp const struct got_error *err = NULL;
960 88775814 2019-10-09 stsp size_t nskipped;
961 02b87977 2019-10-06 stsp
962 18b9ebab 2019-10-06 stsp if (((fdup + 1) & 3) == 0)
963 02b87977 2019-10-06 stsp return NULL;
964 02b87977 2019-10-06 stsp (*j)++;
965 02b87977 2019-10-06 stsp d3s->overlap[*j] = !fdup;
966 74b37681 2019-02-07 stsp if (!fdup)
967 078e971d 2019-02-08 stsp d3s->overlapcnt++;
968 02b87977 2019-10-06 stsp d3s->de[*j].old.from = diff->old.from;
969 02b87977 2019-10-06 stsp d3s->de[*j].old.to = diff->old.to;
970 b2338eff 2019-10-09 stsp
971 50351dee 2019-10-09 stsp err = skip(&nskipped, 2, diff->new.from, d3s);
972 02b87977 2019-10-06 stsp if (err)
973 02b87977 2019-10-06 stsp return err;
974 b2338eff 2019-10-09 stsp d3s->de[*j].newo.from = d3s->de[*j - 1].newo.to + nskipped;
975 b2338eff 2019-10-09 stsp
976 50351dee 2019-10-09 stsp err = skip(&nskipped, 2, diff->new.to, d3s);
977 b2338eff 2019-10-09 stsp if (err)
978 b2338eff 2019-10-09 stsp return err;
979 b2338eff 2019-10-09 stsp d3s->de[*j].newo.to = d3s->de[*j].newo.from + nskipped;
980 02b87977 2019-10-06 stsp return NULL;
981 74b37681 2019-02-07 stsp }
982 74b37681 2019-02-07 stsp
983 74b37681 2019-02-07 stsp /* regurgitate */
984 180dce26 2019-10-06 stsp static const struct got_error *
985 078e971d 2019-02-08 stsp edscript(int n, struct diff3_state *d3s)
986 74b37681 2019-02-07 stsp {
987 ce09ab8d 2019-10-06 stsp const struct got_error *err = NULL;
988 f10244c0 2021-10-11 stsp off_t len;
989 f10244c0 2021-10-11 stsp char *line = NULL;
990 f10244c0 2021-10-11 stsp size_t linesize = 0;
991 90de04bc 2021-10-11 stsp ssize_t linelen = 0, k;
992 74b37681 2019-02-07 stsp
993 74b37681 2019-02-07 stsp for (; n > 0; n--) {
994 ce09ab8d 2019-10-06 stsp if (!d3s->overlap[n]) {
995 ce09ab8d 2019-10-06 stsp err = prange(&d3s->de[n].old, d3s);
996 ce09ab8d 2019-10-06 stsp if (err)
997 ce09ab8d 2019-10-06 stsp return err;
998 d136cfcb 2019-10-12 stsp } else if (d3s->de[n].oldo.from < d3s->de[n].oldo.to) {
999 d136cfcb 2019-10-12 stsp /* Output a block of 3-way diff base file content. */
1000 f10244c0 2021-10-11 stsp err = diff_output(d3s->diffbuf, "%da\n:%s\n",
1001 f69721c3 2019-10-21 stsp d3s->de[n].old.to - 1, d3s->f2mark);
1002 d136cfcb 2019-10-12 stsp if (err)
1003 d136cfcb 2019-10-12 stsp return err;
1004 d136cfcb 2019-10-12 stsp if (fseeko(d3s->fp[1], d3s->de[n].oldo.from, SEEK_SET)
1005 d136cfcb 2019-10-12 stsp == -1)
1006 d136cfcb 2019-10-12 stsp return got_error_from_errno("fseeko");
1007 f10244c0 2021-10-11 stsp len = (d3s->de[n].oldo.to - d3s->de[n].oldo.from);
1008 f10244c0 2021-10-11 stsp for (k = 0; k < (ssize_t)len; k += linelen) {
1009 f10244c0 2021-10-11 stsp linelen = getline(&line, &linesize, d3s->fp[1]);
1010 f10244c0 2021-10-11 stsp if (linelen == -1) {
1011 21c17f12 2020-06-26 stsp if (feof(d3s->fp[1]))
1012 21c17f12 2020-06-26 stsp break;
1013 f10244c0 2021-10-11 stsp err = got_ferror(d3s->fp[1],
1014 d136cfcb 2019-10-12 stsp GOT_ERR_IO);
1015 f10244c0 2021-10-11 stsp goto done;
1016 21c17f12 2020-06-26 stsp }
1017 f10244c0 2021-10-11 stsp err = diff_output(d3s->diffbuf, ":%s", line);
1018 d136cfcb 2019-10-12 stsp if (err)
1019 f10244c0 2021-10-11 stsp goto done;
1020 d136cfcb 2019-10-12 stsp }
1021 f10244c0 2021-10-11 stsp err = diff_output(d3s->diffbuf, "%s%s\n",
1022 90de04bc 2021-10-11 stsp linelen > 0 && line[linelen] == '\n' ? ":" : "",
1023 d136cfcb 2019-10-12 stsp GOT_DIFF_CONFLICT_MARKER_SEP);
1024 d136cfcb 2019-10-12 stsp if (err)
1025 f10244c0 2021-10-11 stsp goto done;
1026 abc59930 2021-09-05 naddy } else {
1027 f10244c0 2021-10-11 stsp err = diff_output(d3s->diffbuf, "%da\n:%s\n",
1028 01a44956 2019-03-27 stsp d3s->de[n].old.to -1, GOT_DIFF_CONFLICT_MARKER_SEP);
1029 ce09ab8d 2019-10-06 stsp if (err)
1030 f10244c0 2021-10-11 stsp goto done;
1031 ce09ab8d 2019-10-06 stsp }
1032 b2338eff 2019-10-09 stsp if (fseeko(d3s->fp[2], d3s->de[n].newo.from, SEEK_SET)
1033 f10244c0 2021-10-11 stsp == -1) {
1034 f10244c0 2021-10-11 stsp err = got_error_from_errno("fseek");
1035 f10244c0 2021-10-11 stsp goto done;
1036 f10244c0 2021-10-11 stsp }
1037 f10244c0 2021-10-11 stsp len = (d3s->de[n].newo.to - d3s->de[n].newo.from);
1038 f10244c0 2021-10-11 stsp for (k = 0; k < (ssize_t)len; k += linelen) {
1039 f10244c0 2021-10-11 stsp linelen = getline(&line, &linesize, d3s->fp[2]);
1040 f10244c0 2021-10-11 stsp if (linelen == -1) {
1041 21c17f12 2020-06-26 stsp if (feof(d3s->fp[2]))
1042 21c17f12 2020-06-26 stsp break;
1043 f10244c0 2021-10-11 stsp err = got_ferror(d3s->fp[2], GOT_ERR_IO);
1044 f10244c0 2021-10-11 stsp goto done;
1045 21c17f12 2020-06-26 stsp }
1046 f10244c0 2021-10-11 stsp err = diff_output(d3s->diffbuf, ":%s", line);
1047 ce09ab8d 2019-10-06 stsp if (err)
1048 f10244c0 2021-10-11 stsp goto done;
1049 74b37681 2019-02-07 stsp }
1050 74b37681 2019-02-07 stsp
1051 ce09ab8d 2019-10-06 stsp if (!d3s->overlap[n]) {
1052 ce09ab8d 2019-10-06 stsp err = diff_output(d3s->diffbuf, ".\n");
1053 ce09ab8d 2019-10-06 stsp if (err)
1054 f10244c0 2021-10-11 stsp goto done;
1055 ce09ab8d 2019-10-06 stsp } else {
1056 f10244c0 2021-10-11 stsp err = diff_output(d3s->diffbuf, "%s%s\n.\n",
1057 4f18c400 2021-10-16 naddy linelen > 0 && line[linelen] == '\n' ? ":" : "",
1058 f10244c0 2021-10-11 stsp d3s->f3mark);
1059 ce09ab8d 2019-10-06 stsp if (err)
1060 f10244c0 2021-10-11 stsp goto done;
1061 f10244c0 2021-10-11 stsp err = diff_output(d3s->diffbuf, "%da\n:%s\n.\n",
1062 5176f942 2019-02-08 stsp d3s->de[n].old.from - 1, d3s->f1mark);
1063 ce09ab8d 2019-10-06 stsp if (err)
1064 f10244c0 2021-10-11 stsp goto done;
1065 74b37681 2019-02-07 stsp }
1066 74b37681 2019-02-07 stsp }
1067 f10244c0 2021-10-11 stsp done:
1068 f10244c0 2021-10-11 stsp free(line);
1069 f10244c0 2021-10-11 stsp return err;
1070 74b37681 2019-02-07 stsp }
1071 74b37681 2019-02-07 stsp
1072 c03c6cb3 2019-02-08 stsp static const struct got_error *
1073 078e971d 2019-02-08 stsp increase(struct diff3_state *d3s)
1074 74b37681 2019-02-07 stsp {
1075 74b37681 2019-02-07 stsp size_t newsz, incr;
1076 c03c6cb3 2019-02-08 stsp struct diff *d;
1077 c03c6cb3 2019-02-08 stsp char *s;
1078 74b37681 2019-02-07 stsp
1079 74b37681 2019-02-07 stsp /* are the memset(3) calls needed? */
1080 078e971d 2019-02-08 stsp newsz = d3s->szchanges == 0 ? 64 : 2 * d3s->szchanges;
1081 078e971d 2019-02-08 stsp incr = newsz - d3s->szchanges;
1082 74b37681 2019-02-07 stsp
1083 078e971d 2019-02-08 stsp d = reallocarray(d3s->d13, newsz, sizeof(*d3s->d13));
1084 c03c6cb3 2019-02-08 stsp if (d == NULL)
1085 638f9024 2019-05-13 stsp return got_error_from_errno("reallocarray");
1086 078e971d 2019-02-08 stsp d3s->d13 = d;
1087 078e971d 2019-02-08 stsp memset(d3s->d13 + d3s->szchanges, 0, incr * sizeof(*d3s->d13));
1088 c03c6cb3 2019-02-08 stsp
1089 078e971d 2019-02-08 stsp d = reallocarray(d3s->d23, newsz, sizeof(*d3s->d23));
1090 c03c6cb3 2019-02-08 stsp if (d == NULL)
1091 638f9024 2019-05-13 stsp return got_error_from_errno("reallocarray");
1092 078e971d 2019-02-08 stsp d3s->d23 = d;
1093 078e971d 2019-02-08 stsp memset(d3s->d23 + d3s->szchanges, 0, incr * sizeof(*d3s->d23));
1094 c03c6cb3 2019-02-08 stsp
1095 078e971d 2019-02-08 stsp d = reallocarray(d3s->de, newsz, sizeof(*d3s->de));
1096 c03c6cb3 2019-02-08 stsp if (d == NULL)
1097 638f9024 2019-05-13 stsp return got_error_from_errno("reallocarray");
1098 078e971d 2019-02-08 stsp d3s->de = d;
1099 078e971d 2019-02-08 stsp memset(d3s->de + d3s->szchanges, 0, incr * sizeof(*d3s->de));
1100 c03c6cb3 2019-02-08 stsp
1101 078e971d 2019-02-08 stsp s = reallocarray(d3s->overlap, newsz, sizeof(*d3s->overlap));
1102 c03c6cb3 2019-02-08 stsp if (s == NULL)
1103 638f9024 2019-05-13 stsp return got_error_from_errno("reallocarray");
1104 078e971d 2019-02-08 stsp d3s->overlap = s;
1105 078e971d 2019-02-08 stsp memset(d3s->overlap + d3s->szchanges, 0, incr * sizeof(*d3s->overlap));
1106 078e971d 2019-02-08 stsp d3s->szchanges = newsz;
1107 c03c6cb3 2019-02-08 stsp
1108 c03c6cb3 2019-02-08 stsp return NULL;
1109 74b37681 2019-02-07 stsp }