Blame


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