Blame


1 e9ce266e 2022-03-07 op /*
2 e9ce266e 2022-03-07 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 e9ce266e 2022-03-07 op *
4 e9ce266e 2022-03-07 op * Permission to use, copy, modify, and distribute this software for any
5 e9ce266e 2022-03-07 op * purpose with or without fee is hereby granted, provided that the above
6 e9ce266e 2022-03-07 op * copyright notice and this permission notice appear in all copies.
7 e9ce266e 2022-03-07 op *
8 e9ce266e 2022-03-07 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 e9ce266e 2022-03-07 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 e9ce266e 2022-03-07 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 e9ce266e 2022-03-07 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 e9ce266e 2022-03-07 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 e9ce266e 2022-03-07 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 e9ce266e 2022-03-07 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 e9ce266e 2022-03-07 op *
16 e9ce266e 2022-03-07 op * Apply patches.
17 e9ce266e 2022-03-07 op *
18 e9ce266e 2022-03-07 op * Things that we may want to support:
19 e9ce266e 2022-03-07 op * + support indented patches?
20 e9ce266e 2022-03-07 op * + support other kinds of patches?
21 e9ce266e 2022-03-07 op */
22 e9ce266e 2022-03-07 op
23 e9ce266e 2022-03-07 op #include <sys/types.h>
24 e9ce266e 2022-03-07 op #include <sys/queue.h>
25 e9ce266e 2022-03-07 op #include <sys/socket.h>
26 5b67f96e 2022-03-13 op #include <sys/stat.h>
27 e9ce266e 2022-03-07 op #include <sys/uio.h>
28 e9ce266e 2022-03-07 op
29 dbda770b 2022-03-13 op #include <errno.h>
30 e9ce266e 2022-03-07 op #include <limits.h>
31 e9ce266e 2022-03-07 op #include <sha1.h>
32 e9ce266e 2022-03-07 op #include <stdint.h>
33 e9ce266e 2022-03-07 op #include <stdio.h>
34 e9ce266e 2022-03-07 op #include <stdlib.h>
35 e9ce266e 2022-03-07 op #include <string.h>
36 e9ce266e 2022-03-07 op #include <unistd.h>
37 e9ce266e 2022-03-07 op #include <imsg.h>
38 e9ce266e 2022-03-07 op
39 e9ce266e 2022-03-07 op #include "got_error.h"
40 e9ce266e 2022-03-07 op #include "got_object.h"
41 e9ce266e 2022-03-07 op #include "got_path.h"
42 e9ce266e 2022-03-07 op #include "got_reference.h"
43 e9ce266e 2022-03-07 op #include "got_cancel.h"
44 e9ce266e 2022-03-07 op #include "got_worktree.h"
45 55e9459f 2022-06-19 op #include "got_repository.h"
46 e9ce266e 2022-03-07 op #include "got_opentemp.h"
47 e9ce266e 2022-03-07 op #include "got_patch.h"
48 e9ce266e 2022-03-07 op
49 e9ce266e 2022-03-07 op #include "got_lib_delta.h"
50 55e9459f 2022-06-19 op #include "got_lib_diff.h"
51 e9ce266e 2022-03-07 op #include "got_lib_object.h"
52 e9ce266e 2022-03-07 op #include "got_lib_privsep.h"
53 55e9459f 2022-06-19 op #include "got_lib_sha1.h"
54 e9ce266e 2022-03-07 op
55 e9ce266e 2022-03-07 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
56 e9ce266e 2022-03-07 op
57 e9ce266e 2022-03-07 op struct got_patch_hunk {
58 e9ce266e 2022-03-07 op STAILQ_ENTRY(got_patch_hunk) entries;
59 60aa1fa0 2022-03-17 op const struct got_error *err;
60 35095610 2022-06-14 op int offset;
61 b2832778 2022-04-23 op int old_nonl;
62 b2832778 2022-04-23 op int new_nonl;
63 35095610 2022-06-14 op int old_from;
64 35095610 2022-06-14 op int old_lines;
65 35095610 2022-06-14 op int new_from;
66 35095610 2022-06-14 op int new_lines;
67 e9ce266e 2022-03-07 op size_t len;
68 e9ce266e 2022-03-07 op size_t cap;
69 e9ce266e 2022-03-07 op char **lines;
70 e9ce266e 2022-03-07 op };
71 e9ce266e 2022-03-07 op
72 60aa1fa0 2022-03-17 op STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
73 e9ce266e 2022-03-07 op struct got_patch {
74 e9ce266e 2022-03-07 op char *old;
75 e9ce266e 2022-03-07 op char *new;
76 d8b5af43 2022-06-19 op char cid[41];
77 55e9459f 2022-06-19 op char blob[41];
78 60aa1fa0 2022-03-17 op struct got_patch_hunk_head head;
79 b22138f5 2022-03-16 op };
80 b22138f5 2022-03-16 op
81 b22138f5 2022-03-16 op struct patch_args {
82 b22138f5 2022-03-16 op got_patch_progress_cb progress_cb;
83 b22138f5 2022-03-16 op void *progress_arg;
84 60aa1fa0 2022-03-17 op struct got_patch_hunk_head *head;
85 e9ce266e 2022-03-07 op };
86 e9ce266e 2022-03-07 op
87 e9ce266e 2022-03-07 op static const struct got_error *
88 e9ce266e 2022-03-07 op send_patch(struct imsgbuf *ibuf, int fd)
89 e9ce266e 2022-03-07 op {
90 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
91 e9ce266e 2022-03-07 op
92 e9ce266e 2022-03-07 op if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
93 e9ce266e 2022-03-07 op NULL, 0) == -1) {
94 e9ce266e 2022-03-07 op err = got_error_from_errno(
95 e9ce266e 2022-03-07 op "imsg_compose GOT_IMSG_PATCH_FILE");
96 e9ce266e 2022-03-07 op close(fd);
97 e9ce266e 2022-03-07 op return err;
98 e9ce266e 2022-03-07 op }
99 e9ce266e 2022-03-07 op
100 e9ce266e 2022-03-07 op if (imsg_flush(ibuf) == -1) {
101 e9ce266e 2022-03-07 op err = got_error_from_errno("imsg_flush");
102 e9ce266e 2022-03-07 op imsg_clear(ibuf);
103 e9ce266e 2022-03-07 op }
104 e9ce266e 2022-03-07 op
105 e9ce266e 2022-03-07 op return err;
106 e9ce266e 2022-03-07 op }
107 e9ce266e 2022-03-07 op
108 e9ce266e 2022-03-07 op static void
109 e9ce266e 2022-03-07 op patch_free(struct got_patch *p)
110 e9ce266e 2022-03-07 op {
111 e9ce266e 2022-03-07 op struct got_patch_hunk *h;
112 e9ce266e 2022-03-07 op size_t i;
113 e9ce266e 2022-03-07 op
114 e9ce266e 2022-03-07 op while (!STAILQ_EMPTY(&p->head)) {
115 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
116 e9ce266e 2022-03-07 op STAILQ_REMOVE_HEAD(&p->head, entries);
117 e9ce266e 2022-03-07 op
118 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i)
119 e9ce266e 2022-03-07 op free(h->lines[i]);
120 e9ce266e 2022-03-07 op free(h->lines);
121 e9ce266e 2022-03-07 op free(h);
122 e9ce266e 2022-03-07 op }
123 e9ce266e 2022-03-07 op
124 e9ce266e 2022-03-07 op free(p->new);
125 e9ce266e 2022-03-07 op free(p->old);
126 0e07a2a1 2022-06-13 op
127 0e07a2a1 2022-06-13 op memset(p, 0, sizeof(*p));
128 0e07a2a1 2022-06-13 op STAILQ_INIT(&p->head);
129 e9ce266e 2022-03-07 op }
130 e9ce266e 2022-03-07 op
131 e9ce266e 2022-03-07 op static const struct got_error *
132 e9ce266e 2022-03-07 op pushline(struct got_patch_hunk *h, const char *line)
133 e9ce266e 2022-03-07 op {
134 e9ce266e 2022-03-07 op void *t;
135 e9ce266e 2022-03-07 op size_t newcap;
136 e9ce266e 2022-03-07 op
137 e9ce266e 2022-03-07 op if (h->len == h->cap) {
138 e9ce266e 2022-03-07 op if ((newcap = h->cap * 1.5) == 0)
139 e9ce266e 2022-03-07 op newcap = 16;
140 e9ce266e 2022-03-07 op t = recallocarray(h->lines, h->cap, newcap,
141 e9ce266e 2022-03-07 op sizeof(h->lines[0]));
142 e9ce266e 2022-03-07 op if (t == NULL)
143 e9ce266e 2022-03-07 op return got_error_from_errno("recallocarray");
144 e9ce266e 2022-03-07 op h->lines = t;
145 e9ce266e 2022-03-07 op h->cap = newcap;
146 e9ce266e 2022-03-07 op }
147 e9ce266e 2022-03-07 op
148 e9ce266e 2022-03-07 op if ((t = strdup(line)) == NULL)
149 e9ce266e 2022-03-07 op return got_error_from_errno("strdup");
150 e9ce266e 2022-03-07 op
151 e9ce266e 2022-03-07 op h->lines[h->len++] = t;
152 e9ce266e 2022-03-07 op return NULL;
153 e9ce266e 2022-03-07 op }
154 e9ce266e 2022-03-07 op
155 e9ce266e 2022-03-07 op static const struct got_error *
156 9d6cabd5 2022-04-07 op recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
157 e9ce266e 2022-03-07 op {
158 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
159 e9ce266e 2022-03-07 op struct imsg imsg;
160 e9ce266e 2022-03-07 op struct got_imsg_patch_hunk hdr;
161 e9ce266e 2022-03-07 op struct got_imsg_patch patch;
162 e9ce266e 2022-03-07 op struct got_patch_hunk *h = NULL;
163 e9ce266e 2022-03-07 op size_t datalen;
164 b2832778 2022-04-23 op int lastmode = -1;
165 e9ce266e 2022-03-07 op
166 e9ce266e 2022-03-07 op memset(p, 0, sizeof(*p));
167 e9ce266e 2022-03-07 op STAILQ_INIT(&p->head);
168 e9ce266e 2022-03-07 op
169 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
170 e9ce266e 2022-03-07 op if (err)
171 e9ce266e 2022-03-07 op return err;
172 e9ce266e 2022-03-07 op if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
173 e9ce266e 2022-03-07 op *done = 1;
174 e9ce266e 2022-03-07 op goto done;
175 e9ce266e 2022-03-07 op }
176 e9ce266e 2022-03-07 op if (imsg.hdr.type != GOT_IMSG_PATCH) {
177 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
178 e9ce266e 2022-03-07 op goto done;
179 e9ce266e 2022-03-07 op }
180 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
181 e9ce266e 2022-03-07 op if (datalen != sizeof(patch)) {
182 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
183 e9ce266e 2022-03-07 op goto done;
184 e9ce266e 2022-03-07 op }
185 e9ce266e 2022-03-07 op memcpy(&patch, imsg.data, sizeof(patch));
186 99b94ad7 2022-06-13 op
187 99b94ad7 2022-06-13 op if (patch.old[sizeof(patch.old)-1] != '\0' ||
188 55e9459f 2022-06-19 op patch.new[sizeof(patch.new)-1] != '\0' ||
189 d8b5af43 2022-06-19 op patch.cid[sizeof(patch.cid)-1] != '\0' ||
190 55e9459f 2022-06-19 op patch.blob[sizeof(patch.blob)-1] != '\0') {
191 99b94ad7 2022-06-13 op err = got_error(GOT_ERR_PRIVSEP_LEN);
192 99b94ad7 2022-06-13 op goto done;
193 99b94ad7 2022-06-13 op }
194 55e9459f 2022-06-19 op
195 497a5915 2022-06-27 op if (*patch.cid != '\0')
196 d8b5af43 2022-06-19 op strlcpy(p->cid, patch.cid, sizeof(p->cid));
197 497a5915 2022-06-27 op
198 497a5915 2022-06-27 op if (*patch.blob != '\0')
199 d8b5af43 2022-06-19 op strlcpy(p->blob, patch.blob, sizeof(p->blob));
200 9d6cabd5 2022-04-07 op
201 9d6cabd5 2022-04-07 op /* automatically set strip=1 for git-style diffs */
202 9d6cabd5 2022-04-07 op if (strip == -1 && patch.git &&
203 9d6cabd5 2022-04-07 op (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
204 9d6cabd5 2022-04-07 op (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
205 9d6cabd5 2022-04-07 op strip = 1;
206 9d6cabd5 2022-04-07 op
207 9d6cabd5 2022-04-07 op /* prefer the new name if not /dev/null for not git-style diffs */
208 9d6cabd5 2022-04-07 op if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
209 9d6cabd5 2022-04-07 op err = got_path_strip(&p->old, patch.new, strip);
210 9d6cabd5 2022-04-07 op if (err)
211 9d6cabd5 2022-04-07 op goto done;
212 9d6cabd5 2022-04-07 op } else if (*patch.old != '\0') {
213 9d6cabd5 2022-04-07 op err = got_path_strip(&p->old, patch.old, strip);
214 9d6cabd5 2022-04-07 op if (err)
215 9d6cabd5 2022-04-07 op goto done;
216 e9ce266e 2022-03-07 op }
217 9d6cabd5 2022-04-07 op
218 9d6cabd5 2022-04-07 op if (*patch.new != '\0') {
219 9d6cabd5 2022-04-07 op err = got_path_strip(&p->new, patch.new, strip);
220 9d6cabd5 2022-04-07 op if (err)
221 9d6cabd5 2022-04-07 op goto done;
222 9d6cabd5 2022-04-07 op }
223 9d6cabd5 2022-04-07 op
224 b95c53df 2022-03-12 op if (p->old == NULL && p->new == NULL) {
225 b95c53df 2022-03-12 op err = got_error(GOT_ERR_PATCH_MALFORMED);
226 b95c53df 2022-03-12 op goto done;
227 b95c53df 2022-03-12 op }
228 e9ce266e 2022-03-07 op
229 e9ce266e 2022-03-07 op imsg_free(&imsg);
230 e9ce266e 2022-03-07 op
231 e9ce266e 2022-03-07 op for (;;) {
232 e9ce266e 2022-03-07 op char *t;
233 e9ce266e 2022-03-07 op
234 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
235 0e07a2a1 2022-06-13 op if (err) {
236 0e07a2a1 2022-06-13 op patch_free(p);
237 e9ce266e 2022-03-07 op return err;
238 0e07a2a1 2022-06-13 op }
239 e9ce266e 2022-03-07 op
240 2399b53d 2022-06-14 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
241 e9ce266e 2022-03-07 op switch (imsg.hdr.type) {
242 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_DONE:
243 be33dff7 2022-05-13 op if (h != NULL && h->len == 0)
244 be33dff7 2022-05-13 op err = got_error(GOT_ERR_PATCH_MALFORMED);
245 e9ce266e 2022-03-07 op goto done;
246 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_HUNK:
247 be33dff7 2022-05-13 op if (h != NULL &&
248 be33dff7 2022-05-13 op (h->len == 0 || h->old_nonl || h->new_nonl)) {
249 b3c57ab2 2022-03-22 op err = got_error(GOT_ERR_PATCH_MALFORMED);
250 b3c57ab2 2022-03-22 op goto done;
251 b3c57ab2 2022-03-22 op }
252 b2832778 2022-04-23 op lastmode = -1;
253 e9ce266e 2022-03-07 op if (datalen != sizeof(hdr)) {
254 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
255 e9ce266e 2022-03-07 op goto done;
256 e9ce266e 2022-03-07 op }
257 e9ce266e 2022-03-07 op memcpy(&hdr, imsg.data, sizeof(hdr));
258 5b7126c5 2022-06-14 op if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
259 5b7126c5 2022-06-14 op err = got_error(GOT_ERR_PRIVSEP_LEN);
260 71393b5c 2022-06-14 op goto done;
261 71393b5c 2022-06-14 op }
262 5b7126c5 2022-06-14 op if ((h = calloc(1, sizeof(*h))) == NULL) {
263 5b7126c5 2022-06-14 op err = got_error_from_errno("calloc");
264 e9ce266e 2022-03-07 op goto done;
265 e9ce266e 2022-03-07 op }
266 e9ce266e 2022-03-07 op h->old_from = hdr.oldfrom;
267 e9ce266e 2022-03-07 op h->old_lines = hdr.oldlines;
268 e9ce266e 2022-03-07 op h->new_from = hdr.newfrom;
269 e9ce266e 2022-03-07 op h->new_lines = hdr.newlines;
270 e9ce266e 2022-03-07 op STAILQ_INSERT_TAIL(&p->head, h, entries);
271 e9ce266e 2022-03-07 op break;
272 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_LINE:
273 e9ce266e 2022-03-07 op if (h == NULL) {
274 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
275 e9ce266e 2022-03-07 op goto done;
276 e9ce266e 2022-03-07 op }
277 e9ce266e 2022-03-07 op t = imsg.data;
278 b3c57ab2 2022-03-22 op /* at least one char */
279 e9ce266e 2022-03-07 op if (datalen < 2 || t[datalen-1] != '\0') {
280 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
281 e9ce266e 2022-03-07 op goto done;
282 e9ce266e 2022-03-07 op }
283 b3c57ab2 2022-03-22 op if (*t != ' ' && *t != '-' && *t != '+' &&
284 b3c57ab2 2022-03-22 op *t != '\\') {
285 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
286 e9ce266e 2022-03-07 op goto done;
287 e9ce266e 2022-03-07 op }
288 b2832778 2022-04-23 op
289 b2832778 2022-04-23 op if (*t != '\\')
290 b3c57ab2 2022-03-22 op err = pushline(h, t);
291 b2832778 2022-04-23 op else if (lastmode == '-')
292 b2832778 2022-04-23 op h->old_nonl = 1;
293 b2832778 2022-04-23 op else if (lastmode == '+')
294 b2832778 2022-04-23 op h->new_nonl = 1;
295 b2832778 2022-04-23 op else
296 b2832778 2022-04-23 op err = got_error(GOT_ERR_PATCH_MALFORMED);
297 b2832778 2022-04-23 op
298 e9ce266e 2022-03-07 op if (err)
299 e9ce266e 2022-03-07 op goto done;
300 b2832778 2022-04-23 op
301 b2832778 2022-04-23 op lastmode = *t;
302 e9ce266e 2022-03-07 op break;
303 e9ce266e 2022-03-07 op default:
304 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
305 e9ce266e 2022-03-07 op goto done;
306 e9ce266e 2022-03-07 op }
307 e9ce266e 2022-03-07 op
308 e9ce266e 2022-03-07 op imsg_free(&imsg);
309 e9ce266e 2022-03-07 op }
310 e9ce266e 2022-03-07 op
311 e9ce266e 2022-03-07 op done:
312 0e07a2a1 2022-06-13 op if (err)
313 0e07a2a1 2022-06-13 op patch_free(p);
314 0e07a2a1 2022-06-13 op
315 e9ce266e 2022-03-07 op imsg_free(&imsg);
316 e9ce266e 2022-03-07 op return err;
317 e9ce266e 2022-03-07 op }
318 e9ce266e 2022-03-07 op
319 e9ce266e 2022-03-07 op /*
320 e9ce266e 2022-03-07 op * Copy data from orig starting at copypos until pos into tmp.
321 e9ce266e 2022-03-07 op * If pos is -1, copy until EOF.
322 e9ce266e 2022-03-07 op */
323 e9ce266e 2022-03-07 op static const struct got_error *
324 e9ce266e 2022-03-07 op copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
325 e9ce266e 2022-03-07 op {
326 e9ce266e 2022-03-07 op char buf[BUFSIZ];
327 e9ce266e 2022-03-07 op size_t len, r, w;
328 e9ce266e 2022-03-07 op
329 e45f7eba 2022-05-14 naddy if (fseeko(orig, copypos, SEEK_SET) == -1)
330 e45f7eba 2022-05-14 naddy return got_error_from_errno("fseeko");
331 e9ce266e 2022-03-07 op
332 e9ce266e 2022-03-07 op while (pos == -1 || copypos < pos) {
333 e9ce266e 2022-03-07 op len = sizeof(buf);
334 e9ce266e 2022-03-07 op if (pos > 0)
335 e9ce266e 2022-03-07 op len = MIN(len, (size_t)pos - copypos);
336 e9ce266e 2022-03-07 op r = fread(buf, 1, len, orig);
337 e9ce266e 2022-03-07 op if (r != len && ferror(orig))
338 e9ce266e 2022-03-07 op return got_error_from_errno("fread");
339 e9ce266e 2022-03-07 op w = fwrite(buf, 1, r, tmp);
340 e9ce266e 2022-03-07 op if (w != r)
341 e9ce266e 2022-03-07 op return got_error_from_errno("fwrite");
342 e9ce266e 2022-03-07 op copypos += len;
343 e9ce266e 2022-03-07 op if (r != len && feof(orig)) {
344 e9ce266e 2022-03-07 op if (pos == -1)
345 e9ce266e 2022-03-07 op return NULL;
346 60aa1fa0 2022-03-17 op return got_error(GOT_ERR_HUNK_FAILED);
347 e9ce266e 2022-03-07 op }
348 e9ce266e 2022-03-07 op }
349 e9ce266e 2022-03-07 op return NULL;
350 e9ce266e 2022-03-07 op }
351 e9ce266e 2022-03-07 op
352 e9ce266e 2022-03-07 op static const struct got_error *
353 35095610 2022-06-14 op locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
354 e9ce266e 2022-03-07 op {
355 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
356 e9ce266e 2022-03-07 op char *line = NULL;
357 e9ce266e 2022-03-07 op char mode = *h->lines[0];
358 e9ce266e 2022-03-07 op size_t linesize = 0;
359 e9ce266e 2022-03-07 op ssize_t linelen;
360 e9ce266e 2022-03-07 op off_t match = -1;
361 35095610 2022-06-14 op int match_lineno = -1;
362 e9ce266e 2022-03-07 op
363 e9ce266e 2022-03-07 op for (;;) {
364 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
365 e9ce266e 2022-03-07 op if (linelen == -1) {
366 e9ce266e 2022-03-07 op if (ferror(orig))
367 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
368 e9ce266e 2022-03-07 op else if (match == -1)
369 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_HUNK_FAILED);
370 e9ce266e 2022-03-07 op break;
371 e9ce266e 2022-03-07 op }
372 b3c57ab2 2022-03-22 op if (line[linelen - 1] == '\n')
373 b3c57ab2 2022-03-22 op line[linelen - 1] = '\0';
374 e9ce266e 2022-03-07 op (*lineno)++;
375 e9ce266e 2022-03-07 op
376 46ebad13 2022-03-17 op if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
377 46ebad13 2022-03-17 op (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
378 e9ce266e 2022-03-07 op (mode == '+' && *lineno == h->old_from)) {
379 e9ce266e 2022-03-07 op match = ftello(orig);
380 e9ce266e 2022-03-07 op if (match == -1) {
381 e9ce266e 2022-03-07 op err = got_error_from_errno("ftello");
382 e9ce266e 2022-03-07 op break;
383 e9ce266e 2022-03-07 op }
384 e9ce266e 2022-03-07 op match -= linelen;
385 e9ce266e 2022-03-07 op match_lineno = (*lineno)-1;
386 e9ce266e 2022-03-07 op }
387 e9ce266e 2022-03-07 op
388 e9ce266e 2022-03-07 op if (*lineno >= h->old_from && match != -1)
389 e9ce266e 2022-03-07 op break;
390 e9ce266e 2022-03-07 op }
391 e9ce266e 2022-03-07 op
392 e9ce266e 2022-03-07 op if (err == NULL) {
393 33df9995 2022-03-11 op *pos = match;
394 e9ce266e 2022-03-07 op *lineno = match_lineno;
395 e45f7eba 2022-05-14 naddy if (fseeko(orig, match, SEEK_SET) == -1)
396 e45f7eba 2022-05-14 naddy err = got_error_from_errno("fseeko");
397 e9ce266e 2022-03-07 op }
398 e9ce266e 2022-03-07 op
399 e9ce266e 2022-03-07 op free(line);
400 e9ce266e 2022-03-07 op return err;
401 e9ce266e 2022-03-07 op }
402 e9ce266e 2022-03-07 op
403 e9ce266e 2022-03-07 op static const struct got_error *
404 e9ce266e 2022-03-07 op test_hunk(FILE *orig, struct got_patch_hunk *h)
405 e9ce266e 2022-03-07 op {
406 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
407 e9ce266e 2022-03-07 op char *line = NULL;
408 e9ce266e 2022-03-07 op size_t linesize = 0, i = 0;
409 e9ce266e 2022-03-07 op ssize_t linelen;
410 e9ce266e 2022-03-07 op
411 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
412 e9ce266e 2022-03-07 op switch (*h->lines[i]) {
413 e9ce266e 2022-03-07 op case '+':
414 e9ce266e 2022-03-07 op continue;
415 e9ce266e 2022-03-07 op case ' ':
416 e9ce266e 2022-03-07 op case '-':
417 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
418 e9ce266e 2022-03-07 op if (linelen == -1) {
419 e9ce266e 2022-03-07 op if (ferror(orig))
420 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
421 e9ce266e 2022-03-07 op else
422 e9ce266e 2022-03-07 op err = got_error(
423 60aa1fa0 2022-03-17 op GOT_ERR_HUNK_FAILED);
424 e9ce266e 2022-03-07 op goto done;
425 e9ce266e 2022-03-07 op }
426 b3c57ab2 2022-03-22 op if (line[linelen - 1] == '\n')
427 b3c57ab2 2022-03-22 op line[linelen - 1] = '\0';
428 46ebad13 2022-03-17 op if (strcmp(h->lines[i] + 1, line)) {
429 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_HUNK_FAILED);
430 e9ce266e 2022-03-07 op goto done;
431 e9ce266e 2022-03-07 op }
432 e9ce266e 2022-03-07 op break;
433 e9ce266e 2022-03-07 op }
434 e9ce266e 2022-03-07 op }
435 e9ce266e 2022-03-07 op
436 e9ce266e 2022-03-07 op done:
437 e9ce266e 2022-03-07 op free(line);
438 e9ce266e 2022-03-07 op return err;
439 e9ce266e 2022-03-07 op }
440 e9ce266e 2022-03-07 op
441 e9ce266e 2022-03-07 op static const struct got_error *
442 35095610 2022-06-14 op apply_hunk(FILE *tmp, struct got_patch_hunk *h, int *lineno)
443 e9ce266e 2022-03-07 op {
444 b2832778 2022-04-23 op size_t i, new = 0;
445 e9ce266e 2022-03-07 op
446 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
447 e9ce266e 2022-03-07 op switch (*h->lines[i]) {
448 e9ce266e 2022-03-07 op case ' ':
449 b3c57ab2 2022-03-22 op if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
450 e9ce266e 2022-03-07 op return got_error_from_errno("fprintf");
451 e9ce266e 2022-03-07 op /* fallthrough */
452 e9ce266e 2022-03-07 op case '-':
453 e9ce266e 2022-03-07 op (*lineno)++;
454 e9ce266e 2022-03-07 op break;
455 e9ce266e 2022-03-07 op case '+':
456 b2832778 2022-04-23 op new++;
457 46ebad13 2022-03-17 op if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
458 e9ce266e 2022-03-07 op return got_error_from_errno("fprintf");
459 b2832778 2022-04-23 op if (new != h->new_lines || !h->new_nonl) {
460 b3c57ab2 2022-03-22 op if (fprintf(tmp, "\n") < 0)
461 b3c57ab2 2022-03-22 op return got_error_from_errno(
462 b3c57ab2 2022-03-22 op "fprintf");
463 b3c57ab2 2022-03-22 op }
464 e9ce266e 2022-03-07 op break;
465 e9ce266e 2022-03-07 op }
466 e9ce266e 2022-03-07 op }
467 e9ce266e 2022-03-07 op return NULL;
468 6e96b326 2022-03-12 op }
469 e9ce266e 2022-03-07 op
470 6e96b326 2022-03-12 op static const struct got_error *
471 05737d49 2022-06-19 op patch_file(struct got_patch *p, FILE *orig, FILE *tmp, mode_t *mode)
472 6e96b326 2022-03-12 op {
473 6e96b326 2022-03-12 op const struct got_error *err = NULL;
474 6e96b326 2022-03-12 op struct got_patch_hunk *h;
475 2be5e1a2 2022-03-16 op struct stat sb;
476 35095610 2022-06-14 op int lineno = 0;
477 6e96b326 2022-03-12 op off_t copypos, pos;
478 6e96b326 2022-03-12 op char *line = NULL;
479 6e96b326 2022-03-12 op size_t linesize = 0;
480 6e96b326 2022-03-12 op ssize_t linelen;
481 6f5cb1bd 2022-03-08 op
482 e9ce266e 2022-03-07 op if (p->old == NULL) { /* create */
483 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
484 6e96b326 2022-03-12 op if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
485 6e96b326 2022-03-12 op return got_error(GOT_ERR_PATCH_MALFORMED);
486 4027dbc2 2022-03-22 op return apply_hunk(tmp, h, &lineno);
487 e9ce266e 2022-03-07 op }
488 e9ce266e 2022-03-07 op
489 827bcd6c 2022-06-19 op if (fstat(fileno(orig), &sb) == -1)
490 827bcd6c 2022-06-19 op return got_error_from_errno("fstat");
491 2be5e1a2 2022-03-16 op *mode = sb.st_mode;
492 2be5e1a2 2022-03-16 op
493 e9ce266e 2022-03-07 op copypos = 0;
494 e9ce266e 2022-03-07 op STAILQ_FOREACH(h, &p->head, entries) {
495 e9ce266e 2022-03-07 op tryagain:
496 33df9995 2022-03-11 op err = locate_hunk(orig, h, &pos, &lineno);
497 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
498 60aa1fa0 2022-03-17 op h->err = err;
499 e9ce266e 2022-03-07 op if (err != NULL)
500 827bcd6c 2022-06-19 op return err;
501 05737d49 2022-06-19 op err = copy(tmp, orig, copypos, pos);
502 e9ce266e 2022-03-07 op if (err != NULL)
503 827bcd6c 2022-06-19 op return err;
504 e9ce266e 2022-03-07 op copypos = pos;
505 e9ce266e 2022-03-07 op
506 e9ce266e 2022-03-07 op err = test_hunk(orig, h);
507 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
508 e9ce266e 2022-03-07 op /*
509 e9ce266e 2022-03-07 op * try to apply the hunk again starting the search
510 e9ce266e 2022-03-07 op * after the previous partial match.
511 e9ce266e 2022-03-07 op */
512 827bcd6c 2022-06-19 op if (fseeko(orig, pos, SEEK_SET) == -1)
513 827bcd6c 2022-06-19 op return got_error_from_errno("fseeko");
514 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
515 827bcd6c 2022-06-19 op if (linelen == -1)
516 827bcd6c 2022-06-19 op return got_error_from_errno("getline");
517 e9ce266e 2022-03-07 op lineno++;
518 e9ce266e 2022-03-07 op goto tryagain;
519 e9ce266e 2022-03-07 op }
520 e9ce266e 2022-03-07 op if (err != NULL)
521 827bcd6c 2022-06-19 op return err;
522 e9ce266e 2022-03-07 op
523 60aa1fa0 2022-03-17 op if (lineno + 1 != h->old_from)
524 60aa1fa0 2022-03-17 op h->offset = lineno + 1 - h->old_from;
525 60aa1fa0 2022-03-17 op
526 05737d49 2022-06-19 op err = apply_hunk(tmp, h, &lineno);
527 e9ce266e 2022-03-07 op if (err != NULL)
528 827bcd6c 2022-06-19 op return err;
529 fbbb53b9 2022-03-25 op
530 e9ce266e 2022-03-07 op copypos = ftello(orig);
531 827bcd6c 2022-06-19 op if (copypos == -1)
532 827bcd6c 2022-06-19 op return got_error_from_errno("ftello");
533 e9ce266e 2022-03-07 op }
534 e9ce266e 2022-03-07 op
535 60aa1fa0 2022-03-17 op if (p->new == NULL && sb.st_size != copypos) {
536 60aa1fa0 2022-03-17 op h = STAILQ_FIRST(&p->head);
537 60aa1fa0 2022-03-17 op h->err = got_error(GOT_ERR_HUNK_FAILED);
538 60aa1fa0 2022-03-17 op err = h->err;
539 05737d49 2022-06-19 op } else if (!feof(orig))
540 e9ce266e 2022-03-07 op err = copy(tmp, orig, copypos, -1);
541 6e96b326 2022-03-12 op
542 dbda770b 2022-03-13 op return err;
543 dbda770b 2022-03-13 op }
544 dbda770b 2022-03-13 op
545 dbda770b 2022-03-13 op static const struct got_error *
546 60aa1fa0 2022-03-17 op report_progress(struct patch_args *pa, const char *old, const char *new,
547 60aa1fa0 2022-03-17 op unsigned char status, const struct got_error *orig_error)
548 60aa1fa0 2022-03-17 op {
549 60aa1fa0 2022-03-17 op const struct got_error *err;
550 60aa1fa0 2022-03-17 op struct got_patch_hunk *h;
551 60aa1fa0 2022-03-17 op
552 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, status,
553 60aa1fa0 2022-03-17 op orig_error, 0, 0, 0, 0, 0, NULL);
554 60aa1fa0 2022-03-17 op if (err)
555 60aa1fa0 2022-03-17 op return err;
556 60aa1fa0 2022-03-17 op
557 60aa1fa0 2022-03-17 op STAILQ_FOREACH(h, pa->head, entries) {
558 60aa1fa0 2022-03-17 op if (h->offset == 0 && h->err == NULL)
559 60aa1fa0 2022-03-17 op continue;
560 60aa1fa0 2022-03-17 op
561 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
562 60aa1fa0 2022-03-17 op h->old_from, h->old_lines, h->new_from, h->new_lines,
563 60aa1fa0 2022-03-17 op h->offset, h->err);
564 60aa1fa0 2022-03-17 op if (err)
565 60aa1fa0 2022-03-17 op return err;
566 60aa1fa0 2022-03-17 op }
567 60aa1fa0 2022-03-17 op
568 60aa1fa0 2022-03-17 op return NULL;
569 60aa1fa0 2022-03-17 op }
570 60aa1fa0 2022-03-17 op
571 60aa1fa0 2022-03-17 op static const struct got_error *
572 b22138f5 2022-03-16 op patch_delete(void *arg, unsigned char status, unsigned char staged_status,
573 b22138f5 2022-03-16 op const char *path)
574 b22138f5 2022-03-16 op {
575 60aa1fa0 2022-03-17 op return report_progress(arg, path, NULL, status, NULL);
576 b22138f5 2022-03-16 op }
577 b22138f5 2022-03-16 op
578 b22138f5 2022-03-16 op static const struct got_error *
579 b22138f5 2022-03-16 op patch_add(void *arg, unsigned char status, const char *path)
580 b22138f5 2022-03-16 op {
581 60aa1fa0 2022-03-17 op return report_progress(arg, NULL, path, status, NULL);
582 b22138f5 2022-03-16 op }
583 b22138f5 2022-03-16 op
584 b22138f5 2022-03-16 op static const struct got_error *
585 55e9459f 2022-06-19 op open_blob(char **path, FILE **fp, const char *blobid,
586 55e9459f 2022-06-19 op struct got_repository *repo)
587 6e96b326 2022-03-12 op {
588 6e96b326 2022-03-12 op const struct got_error *err = NULL;
589 55e9459f 2022-06-19 op struct got_blob_object *blob = NULL;
590 db0dfdd7 2022-06-27 op struct got_object_id id, *idptr, *matched_id = NULL;
591 eb81bc23 2022-06-28 tracey int fd = -1;
592 55e9459f 2022-06-19 op
593 55e9459f 2022-06-19 op *fp = NULL;
594 55e9459f 2022-06-19 op *path = NULL;
595 55e9459f 2022-06-19 op
596 db0dfdd7 2022-06-27 op if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
597 db0dfdd7 2022-06-27 op err = got_repo_match_object_id(&matched_id, NULL, blobid,
598 db0dfdd7 2022-06-27 op GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
599 db0dfdd7 2022-06-27 op repo);
600 db0dfdd7 2022-06-27 op if (err)
601 db0dfdd7 2022-06-27 op return err;
602 db0dfdd7 2022-06-27 op idptr = matched_id;
603 db0dfdd7 2022-06-27 op } else {
604 db0dfdd7 2022-06-27 op if (!got_parse_sha1_digest(id.sha1, blobid))
605 db0dfdd7 2022-06-27 op return got_error(GOT_ERR_BAD_OBJ_ID_STR);
606 db0dfdd7 2022-06-27 op idptr = &id;
607 db0dfdd7 2022-06-27 op }
608 55e9459f 2022-06-19 op
609 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
610 eb81bc23 2022-06-28 tracey if (fd == -1) {
611 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
612 eb81bc23 2022-06-28 tracey goto done;
613 eb81bc23 2022-06-28 tracey }
614 eb81bc23 2022-06-28 tracey
615 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
616 55e9459f 2022-06-19 op if (err)
617 55e9459f 2022-06-19 op goto done;
618 55e9459f 2022-06-19 op
619 55e9459f 2022-06-19 op err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
620 55e9459f 2022-06-19 op if (err)
621 55e9459f 2022-06-19 op goto done;
622 55e9459f 2022-06-19 op
623 55e9459f 2022-06-19 op err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
624 55e9459f 2022-06-19 op if (err)
625 55e9459f 2022-06-19 op goto done;
626 55e9459f 2022-06-19 op
627 55e9459f 2022-06-19 op done:
628 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
629 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
630 55e9459f 2022-06-19 op if (blob)
631 55e9459f 2022-06-19 op got_object_blob_close(blob);
632 db0dfdd7 2022-06-27 op if (matched_id != NULL)
633 db0dfdd7 2022-06-27 op free(matched_id);
634 55e9459f 2022-06-19 op if (err) {
635 55e9459f 2022-06-19 op if (*fp != NULL)
636 55e9459f 2022-06-19 op fclose(*fp);
637 55e9459f 2022-06-19 op if (*path != NULL)
638 55e9459f 2022-06-19 op unlink(*path);
639 55e9459f 2022-06-19 op free(*path);
640 55e9459f 2022-06-19 op *fp = NULL;
641 55e9459f 2022-06-19 op *path = NULL;
642 55e9459f 2022-06-19 op }
643 55e9459f 2022-06-19 op return err;
644 55e9459f 2022-06-19 op }
645 55e9459f 2022-06-19 op
646 55e9459f 2022-06-19 op static const struct got_error *
647 55e9459f 2022-06-19 op apply_patch(int *overlapcnt, struct got_worktree *worktree,
648 55e9459f 2022-06-19 op struct got_repository *repo, struct got_fileindex *fileindex,
649 55e9459f 2022-06-19 op const char *old, const char *new, struct got_patch *p, int nop,
650 55e9459f 2022-06-19 op struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
651 55e9459f 2022-06-19 op {
652 55e9459f 2022-06-19 op const struct got_error *err = NULL;
653 55e9459f 2022-06-19 op int do_merge = 0, file_renamed = 0;
654 d8b5af43 2022-06-19 op char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
655 ed3bff83 2022-04-23 op char *oldpath = NULL, *newpath = NULL;
656 1b1b91ab 2022-06-15 op char *tmppath = NULL, *template = NULL, *parent = NULL;
657 55e9459f 2022-06-19 op char *apath = NULL, *mergepath = NULL;
658 55e9459f 2022-06-19 op FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
659 55e9459f 2022-06-19 op int outfd;
660 55e9459f 2022-06-19 op const char *outpath;
661 2be5e1a2 2022-03-16 op mode_t mode = GOT_DEFAULT_FILE_MODE;
662 ed3bff83 2022-04-23 op
663 55e9459f 2022-06-19 op *overlapcnt = 0;
664 55e9459f 2022-06-19 op
665 55e9459f 2022-06-19 op /* don't run the diff3 merge on creations/deletions */
666 55e9459f 2022-06-19 op if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
667 55e9459f 2022-06-19 op err = open_blob(&apath, &afile, p->blob, repo);
668 dbc68eed 2022-06-21 op /*
669 dbc68eed 2022-06-21 op * ignore failures to open this blob, we might have
670 dbc68eed 2022-06-21 op * parsed gibberish.
671 dbc68eed 2022-06-21 op */
672 db0dfdd7 2022-06-27 op if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
673 db0dfdd7 2022-06-27 op err->code != GOT_ERR_NO_OBJ)
674 55e9459f 2022-06-19 op return err;
675 55e9459f 2022-06-19 op else if (err == NULL)
676 55e9459f 2022-06-19 op do_merge = 1;
677 55e9459f 2022-06-19 op err = NULL;
678 55e9459f 2022-06-19 op }
679 55e9459f 2022-06-19 op
680 ed3bff83 2022-04-23 op if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
681 ed3bff83 2022-04-23 op old) == -1) {
682 ed3bff83 2022-04-23 op err = got_error_from_errno("asprintf");
683 78f5ac24 2022-03-19 op goto done;
684 ed3bff83 2022-04-23 op }
685 dbda770b 2022-03-13 op
686 ed3bff83 2022-04-23 op if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
687 ed3bff83 2022-04-23 op new) == -1) {
688 ed3bff83 2022-04-23 op err = got_error_from_errno("asprintf");
689 ed3bff83 2022-04-23 op goto done;
690 ed3bff83 2022-04-23 op }
691 ed3bff83 2022-04-23 op
692 78f5ac24 2022-03-19 op file_renamed = strcmp(oldpath, newpath);
693 78f5ac24 2022-03-19 op
694 6e96b326 2022-03-12 op if (asprintf(&template, "%s/got-patch",
695 6e96b326 2022-03-12 op got_worktree_get_root_path(worktree)) == -1) {
696 6e96b326 2022-03-12 op err = got_error_from_errno(template);
697 e9ce266e 2022-03-07 op goto done;
698 e9ce266e 2022-03-07 op }
699 e9ce266e 2022-03-07 op
700 827bcd6c 2022-06-19 op if (p->old != NULL && (oldfile = fopen(oldpath, "r")) == NULL) {
701 827bcd6c 2022-06-19 op err = got_error_from_errno2("open", oldpath);
702 827bcd6c 2022-06-19 op goto done;
703 827bcd6c 2022-06-19 op }
704 827bcd6c 2022-06-19 op
705 55e9459f 2022-06-19 op err = got_opentemp_named(&tmppath, &tmpfile, template);
706 6e96b326 2022-03-12 op if (err)
707 6e96b326 2022-03-12 op goto done;
708 55e9459f 2022-06-19 op outpath = tmppath;
709 55e9459f 2022-06-19 op outfd = fileno(tmpfile);
710 55e9459f 2022-06-19 op err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile, &mode);
711 6e96b326 2022-03-12 op if (err)
712 6e96b326 2022-03-12 op goto done;
713 6e96b326 2022-03-12 op
714 55e9459f 2022-06-19 op if (do_merge) {
715 497a5915 2022-06-27 op const char *type, *id;
716 497a5915 2022-06-27 op
717 55e9459f 2022-06-19 op if (fseeko(afile, 0, SEEK_SET) == -1 ||
718 55e9459f 2022-06-19 op fseeko(oldfile, 0, SEEK_SET) == -1 ||
719 55e9459f 2022-06-19 op fseeko(tmpfile, 0, SEEK_SET) == -1) {
720 55e9459f 2022-06-19 op err = got_error_from_errno("fseeko");
721 55e9459f 2022-06-19 op goto done;
722 55e9459f 2022-06-19 op }
723 55e9459f 2022-06-19 op
724 55e9459f 2022-06-19 op if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
725 55e9459f 2022-06-19 op err = got_error_from_errno("asprintf");
726 55e9459f 2022-06-19 op oldlabel = NULL;
727 55e9459f 2022-06-19 op goto done;
728 55e9459f 2022-06-19 op }
729 55e9459f 2022-06-19 op
730 55e9459f 2022-06-19 op if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
731 55e9459f 2022-06-19 op err = got_error_from_errno("asprintf");
732 55e9459f 2022-06-19 op newlabel = NULL;
733 55e9459f 2022-06-19 op goto done;
734 55e9459f 2022-06-19 op }
735 55e9459f 2022-06-19 op
736 497a5915 2022-06-27 op if (*p->cid != '\0') {
737 497a5915 2022-06-27 op type = "commit";
738 497a5915 2022-06-27 op id = p->cid;
739 497a5915 2022-06-27 op } else {
740 497a5915 2022-06-27 op type = "blob";
741 497a5915 2022-06-27 op id = p->blob;
742 497a5915 2022-06-27 op }
743 497a5915 2022-06-27 op
744 497a5915 2022-06-27 op if (asprintf(&anclabel, "%s %s", type, id) == -1) {
745 d8b5af43 2022-06-19 op err = got_error_from_errno("asprintf");
746 d8b5af43 2022-06-19 op anclabel = NULL;
747 d8b5af43 2022-06-19 op goto done;
748 d8b5af43 2022-06-19 op }
749 d8b5af43 2022-06-19 op
750 55e9459f 2022-06-19 op err = got_opentemp_named(&mergepath, &mergefile, template);
751 55e9459f 2022-06-19 op if (err)
752 55e9459f 2022-06-19 op goto done;
753 55e9459f 2022-06-19 op outpath = mergepath;
754 55e9459f 2022-06-19 op outfd = fileno(mergefile);
755 55e9459f 2022-06-19 op
756 55e9459f 2022-06-19 op err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
757 d8b5af43 2022-06-19 op oldfile, oldlabel, anclabel, newlabel,
758 55e9459f 2022-06-19 op GOT_DIFF_ALGORITHM_PATIENCE);
759 55e9459f 2022-06-19 op if (err)
760 55e9459f 2022-06-19 op goto done;
761 55e9459f 2022-06-19 op }
762 55e9459f 2022-06-19 op
763 b22138f5 2022-03-16 op if (nop)
764 899fcfdf 2022-03-13 op goto done;
765 899fcfdf 2022-03-13 op
766 5b67f96e 2022-03-13 op if (p->old != NULL && p->new == NULL) {
767 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_rm(old, repo, worktree,
768 f2dd7807 2022-05-17 op fileindex, patch_delete, pa);
769 5b67f96e 2022-03-13 op goto done;
770 5b67f96e 2022-03-13 op }
771 5b67f96e 2022-03-13 op
772 55e9459f 2022-06-19 op if (fchmod(outfd, mode) == -1) {
773 41a37a44 2022-04-23 op err = got_error_from_errno2("chmod", tmppath);
774 2be5e1a2 2022-03-16 op goto done;
775 2be5e1a2 2022-03-16 op }
776 2be5e1a2 2022-03-16 op
777 55e9459f 2022-06-19 op if (rename(outpath, newpath) == -1) {
778 95d68340 2022-03-16 op if (errno != ENOENT) {
779 55e9459f 2022-06-19 op err = got_error_from_errno3("rename", outpath,
780 95d68340 2022-03-16 op newpath);
781 95d68340 2022-03-16 op goto done;
782 95d68340 2022-03-16 op }
783 95d68340 2022-03-16 op
784 95d68340 2022-03-16 op err = got_path_dirname(&parent, newpath);
785 95d68340 2022-03-16 op if (err != NULL)
786 95d68340 2022-03-16 op goto done;
787 95d68340 2022-03-16 op err = got_path_mkdir(parent);
788 95d68340 2022-03-16 op if (err != NULL)
789 95d68340 2022-03-16 op goto done;
790 55e9459f 2022-06-19 op if (rename(outpath, newpath) == -1) {
791 55e9459f 2022-06-19 op err = got_error_from_errno3("rename", outpath,
792 95d68340 2022-03-16 op newpath);
793 95d68340 2022-03-16 op goto done;
794 95d68340 2022-03-16 op }
795 6e96b326 2022-03-12 op }
796 6e96b326 2022-03-12 op
797 6e96b326 2022-03-12 op if (file_renamed) {
798 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_rm(old, repo, worktree,
799 f2dd7807 2022-05-17 op fileindex, patch_delete, pa);
800 6e96b326 2022-03-12 op if (err == NULL)
801 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_add(new, repo,
802 f2dd7807 2022-05-17 op worktree, fileindex, patch_add,
803 f2dd7807 2022-05-17 op pa);
804 78f5ac24 2022-03-19 op if (err)
805 78f5ac24 2022-03-19 op unlink(newpath);
806 78f5ac24 2022-03-19 op } else if (p->old == NULL) {
807 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_add(new, repo, worktree,
808 f2dd7807 2022-05-17 op fileindex, patch_add, pa);
809 78f5ac24 2022-03-19 op if (err)
810 78f5ac24 2022-03-19 op unlink(newpath);
811 55e9459f 2022-06-19 op } else if (*overlapcnt != 0)
812 55e9459f 2022-06-19 op err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
813 9802c41c 2022-06-21 op else if (do_merge)
814 9802c41c 2022-06-21 op err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
815 55e9459f 2022-06-19 op else
816 ed3bff83 2022-04-23 op err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
817 6e96b326 2022-03-12 op
818 e9ce266e 2022-03-07 op done:
819 95d68340 2022-03-16 op free(parent);
820 6f5cb1bd 2022-03-08 op free(template);
821 55e9459f 2022-06-19 op
822 e9ce266e 2022-03-07 op if (tmppath != NULL)
823 e9ce266e 2022-03-07 op unlink(tmppath);
824 55e9459f 2022-06-19 op if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
825 06c44edc 2022-06-15 stsp err = got_error_from_errno("fclose");
826 e9ce266e 2022-03-07 op free(tmppath);
827 55e9459f 2022-06-19 op
828 ed3bff83 2022-04-23 op free(oldpath);
829 827bcd6c 2022-06-19 op if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
830 827bcd6c 2022-06-19 op err = got_error_from_errno("fclose");
831 55e9459f 2022-06-19 op
832 55e9459f 2022-06-19 op if (apath != NULL)
833 55e9459f 2022-06-19 op unlink(apath);
834 55e9459f 2022-06-19 op if (afile != NULL && fclose(afile) == EOF && err == NULL)
835 55e9459f 2022-06-19 op err = got_error_from_errno("fclose");
836 55e9459f 2022-06-19 op free(apath);
837 55e9459f 2022-06-19 op
838 55e9459f 2022-06-19 op if (mergepath != NULL)
839 55e9459f 2022-06-19 op unlink(mergepath);
840 55e9459f 2022-06-19 op if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
841 55e9459f 2022-06-19 op err = got_error_from_errno("fclose");
842 55e9459f 2022-06-19 op free(mergepath);
843 55e9459f 2022-06-19 op
844 ed3bff83 2022-04-23 op free(newpath);
845 55e9459f 2022-06-19 op free(oldlabel);
846 55e9459f 2022-06-19 op free(newlabel);
847 d8b5af43 2022-06-19 op free(anclabel);
848 e9ce266e 2022-03-07 op return err;
849 bad961bf 2022-04-23 op }
850 bad961bf 2022-04-23 op
851 bad961bf 2022-04-23 op static void
852 bad961bf 2022-04-23 op reverse_patch(struct got_patch *p)
853 bad961bf 2022-04-23 op {
854 bad961bf 2022-04-23 op struct got_patch_hunk *h;
855 bad961bf 2022-04-23 op size_t i;
856 35095610 2022-06-14 op int tmp;
857 bad961bf 2022-04-23 op
858 bad961bf 2022-04-23 op STAILQ_FOREACH(h, &p->head, entries) {
859 bad961bf 2022-04-23 op tmp = h->old_from;
860 bad961bf 2022-04-23 op h->old_from = h->new_from;
861 bad961bf 2022-04-23 op h->new_from = tmp;
862 bad961bf 2022-04-23 op
863 bad961bf 2022-04-23 op tmp = h->old_lines;
864 bad961bf 2022-04-23 op h->old_lines = h->new_lines;
865 bad961bf 2022-04-23 op h->new_lines = tmp;
866 bad961bf 2022-04-23 op
867 bad961bf 2022-04-23 op tmp = h->old_nonl;
868 bad961bf 2022-04-23 op h->old_nonl = h->new_nonl;
869 bad961bf 2022-04-23 op h->new_nonl = tmp;
870 bad961bf 2022-04-23 op
871 bad961bf 2022-04-23 op for (i = 0; i < h->len; ++i) {
872 bad961bf 2022-04-23 op if (*h->lines[i] == '+')
873 bad961bf 2022-04-23 op *h->lines[i] = '-';
874 bad961bf 2022-04-23 op else if (*h->lines[i] == '-')
875 bad961bf 2022-04-23 op *h->lines[i] = '+';
876 bad961bf 2022-04-23 op }
877 bad961bf 2022-04-23 op }
878 e9ce266e 2022-03-07 op }
879 e9ce266e 2022-03-07 op
880 e9ce266e 2022-03-07 op const struct got_error *
881 e9ce266e 2022-03-07 op got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
882 bad961bf 2022-04-23 op int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
883 bad961bf 2022-04-23 op void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
884 e9ce266e 2022-03-07 op {
885 4bcdc895 2022-05-17 op const struct got_error *err = NULL, *complete_err = NULL;
886 78f5ac24 2022-03-19 op struct got_fileindex *fileindex = NULL;
887 f2dd7807 2022-05-17 op char *fileindex_path = NULL;
888 60aa1fa0 2022-03-17 op char *oldpath, *newpath;
889 e9ce266e 2022-03-07 op struct imsgbuf *ibuf;
890 e9ce266e 2022-03-07 op int imsg_fds[2] = {-1, -1};
891 55e9459f 2022-06-19 op int overlapcnt, done = 0, failed = 0;
892 e9ce266e 2022-03-07 op pid_t pid;
893 e9ce266e 2022-03-07 op
894 e9ce266e 2022-03-07 op ibuf = calloc(1, sizeof(*ibuf));
895 e9ce266e 2022-03-07 op if (ibuf == NULL) {
896 e9ce266e 2022-03-07 op err = got_error_from_errno("calloc");
897 e9ce266e 2022-03-07 op goto done;
898 e9ce266e 2022-03-07 op }
899 e9ce266e 2022-03-07 op
900 e9ce266e 2022-03-07 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
901 e9ce266e 2022-03-07 op err = got_error_from_errno("socketpair");
902 e9ce266e 2022-03-07 op goto done;
903 e9ce266e 2022-03-07 op }
904 e9ce266e 2022-03-07 op
905 e9ce266e 2022-03-07 op pid = fork();
906 e9ce266e 2022-03-07 op if (pid == -1) {
907 e9ce266e 2022-03-07 op err = got_error_from_errno("fork");
908 e9ce266e 2022-03-07 op goto done;
909 e9ce266e 2022-03-07 op } else if (pid == 0) {
910 e9ce266e 2022-03-07 op got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
911 e9ce266e 2022-03-07 op NULL);
912 e9ce266e 2022-03-07 op /* not reached */
913 e9ce266e 2022-03-07 op }
914 e9ce266e 2022-03-07 op
915 e9ce266e 2022-03-07 op if (close(imsg_fds[1]) == -1) {
916 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
917 e9ce266e 2022-03-07 op goto done;
918 e9ce266e 2022-03-07 op }
919 e9ce266e 2022-03-07 op imsg_fds[1] = -1;
920 e9ce266e 2022-03-07 op imsg_init(ibuf, imsg_fds[0]);
921 e9ce266e 2022-03-07 op
922 e9ce266e 2022-03-07 op err = send_patch(ibuf, fd);
923 e9ce266e 2022-03-07 op fd = -1;
924 e9ce266e 2022-03-07 op if (err)
925 e9ce266e 2022-03-07 op goto done;
926 e9ce266e 2022-03-07 op
927 f2dd7807 2022-05-17 op err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
928 f2dd7807 2022-05-17 op worktree);
929 78f5ac24 2022-03-19 op if (err)
930 78f5ac24 2022-03-19 op goto done;
931 78f5ac24 2022-03-19 op
932 e9ce266e 2022-03-07 op while (!done && err == NULL) {
933 e9ce266e 2022-03-07 op struct got_patch p;
934 60aa1fa0 2022-03-17 op struct patch_args pa;
935 e9ce266e 2022-03-07 op
936 60aa1fa0 2022-03-17 op pa.progress_cb = progress_cb;
937 60aa1fa0 2022-03-17 op pa.progress_arg = progress_arg;
938 60aa1fa0 2022-03-17 op pa.head = &p.head;
939 60aa1fa0 2022-03-17 op
940 9d6cabd5 2022-04-07 op err = recv_patch(ibuf, &done, &p, strip);
941 e9ce266e 2022-03-07 op if (err || done)
942 e9ce266e 2022-03-07 op break;
943 e9ce266e 2022-03-07 op
944 bad961bf 2022-04-23 op if (reverse)
945 bad961bf 2022-04-23 op reverse_patch(&p);
946 bad961bf 2022-04-23 op
947 78f5ac24 2022-03-19 op err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
948 78f5ac24 2022-03-19 op &newpath, worktree, repo, fileindex);
949 78f5ac24 2022-03-19 op if (err == NULL)
950 55e9459f 2022-06-19 op err = apply_patch(&overlapcnt, worktree, repo,
951 55e9459f 2022-06-19 op fileindex, oldpath, newpath, &p, nop, &pa,
952 55e9459f 2022-06-19 op cancel_cb, cancel_arg);
953 60aa1fa0 2022-03-17 op if (err != NULL) {
954 60aa1fa0 2022-03-17 op failed = 1;
955 60aa1fa0 2022-03-17 op /* recoverable errors */
956 60aa1fa0 2022-03-17 op if (err->code == GOT_ERR_FILE_STATUS ||
957 60aa1fa0 2022-03-17 op (err->code == GOT_ERR_ERRNO && errno == ENOENT))
958 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
959 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, err);
960 60aa1fa0 2022-03-17 op else if (err->code == GOT_ERR_HUNK_FAILED)
961 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
962 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, NULL);
963 60aa1fa0 2022-03-17 op }
964 55e9459f 2022-06-19 op if (overlapcnt != 0)
965 55e9459f 2022-06-19 op failed = 1;
966 60aa1fa0 2022-03-17 op
967 60aa1fa0 2022-03-17 op free(oldpath);
968 60aa1fa0 2022-03-17 op free(newpath);
969 e9ce266e 2022-03-07 op patch_free(&p);
970 60aa1fa0 2022-03-17 op
971 e9ce266e 2022-03-07 op if (err)
972 e9ce266e 2022-03-07 op break;
973 e9ce266e 2022-03-07 op }
974 e9ce266e 2022-03-07 op
975 e9ce266e 2022-03-07 op done:
976 4bcdc895 2022-05-17 op if (fileindex != NULL)
977 4bcdc895 2022-05-17 op complete_err = got_worktree_patch_complete(fileindex,
978 4bcdc895 2022-05-17 op fileindex_path);
979 f2dd7807 2022-05-17 op if (complete_err && err == NULL)
980 f2dd7807 2022-05-17 op err = complete_err;
981 4bcdc895 2022-05-17 op free(fileindex_path);
982 e9ce266e 2022-03-07 op if (fd != -1 && close(fd) == -1 && err == NULL)
983 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
984 e9ce266e 2022-03-07 op if (ibuf != NULL)
985 e9ce266e 2022-03-07 op imsg_clear(ibuf);
986 e9ce266e 2022-03-07 op if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
987 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
988 e9ce266e 2022-03-07 op if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
989 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
990 60aa1fa0 2022-03-17 op if (err == NULL && failed)
991 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_PATCH_FAILED);
992 e9ce266e 2022-03-07 op return err;
993 e9ce266e 2022-03-07 op }