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