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