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