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