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