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