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 5dffb1a1 2022-07-02 op patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
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
493 e9ce266e 2022-03-07 op copypos = 0;
494 e9ce266e 2022-03-07 op STAILQ_FOREACH(h, &p->head, entries) {
495 e9ce266e 2022-03-07 op tryagain:
496 33df9995 2022-03-11 op err = locate_hunk(orig, h, &pos, &lineno);
497 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
498 60aa1fa0 2022-03-17 op h->err = err;
499 e9ce266e 2022-03-07 op if (err != NULL)
500 827bcd6c 2022-06-19 op return err;
501 05737d49 2022-06-19 op err = copy(tmp, orig, copypos, pos);
502 e9ce266e 2022-03-07 op if (err != NULL)
503 827bcd6c 2022-06-19 op return err;
504 e9ce266e 2022-03-07 op copypos = pos;
505 e9ce266e 2022-03-07 op
506 e9ce266e 2022-03-07 op err = test_hunk(orig, h);
507 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
508 e9ce266e 2022-03-07 op /*
509 e9ce266e 2022-03-07 op * try to apply the hunk again starting the search
510 e9ce266e 2022-03-07 op * after the previous partial match.
511 e9ce266e 2022-03-07 op */
512 827bcd6c 2022-06-19 op if (fseeko(orig, pos, SEEK_SET) == -1)
513 827bcd6c 2022-06-19 op return got_error_from_errno("fseeko");
514 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
515 827bcd6c 2022-06-19 op if (linelen == -1)
516 827bcd6c 2022-06-19 op return got_error_from_errno("getline");
517 e9ce266e 2022-03-07 op lineno++;
518 e9ce266e 2022-03-07 op goto tryagain;
519 e9ce266e 2022-03-07 op }
520 e9ce266e 2022-03-07 op if (err != NULL)
521 827bcd6c 2022-06-19 op return err;
522 e9ce266e 2022-03-07 op
523 60aa1fa0 2022-03-17 op if (lineno + 1 != h->old_from)
524 60aa1fa0 2022-03-17 op h->offset = lineno + 1 - h->old_from;
525 60aa1fa0 2022-03-17 op
526 05737d49 2022-06-19 op err = apply_hunk(tmp, h, &lineno);
527 e9ce266e 2022-03-07 op if (err != NULL)
528 827bcd6c 2022-06-19 op return err;
529 fbbb53b9 2022-03-25 op
530 e9ce266e 2022-03-07 op copypos = ftello(orig);
531 827bcd6c 2022-06-19 op if (copypos == -1)
532 827bcd6c 2022-06-19 op return got_error_from_errno("ftello");
533 e9ce266e 2022-03-07 op }
534 e9ce266e 2022-03-07 op
535 60aa1fa0 2022-03-17 op if (p->new == NULL && sb.st_size != copypos) {
536 60aa1fa0 2022-03-17 op h = STAILQ_FIRST(&p->head);
537 60aa1fa0 2022-03-17 op h->err = got_error(GOT_ERR_HUNK_FAILED);
538 60aa1fa0 2022-03-17 op err = h->err;
539 05737d49 2022-06-19 op } else if (!feof(orig))
540 e9ce266e 2022-03-07 op err = copy(tmp, orig, copypos, -1);
541 6e96b326 2022-03-12 op
542 dbda770b 2022-03-13 op return err;
543 dbda770b 2022-03-13 op }
544 dbda770b 2022-03-13 op
545 dbda770b 2022-03-13 op static const struct got_error *
546 60aa1fa0 2022-03-17 op report_progress(struct patch_args *pa, const char *old, const char *new,
547 60aa1fa0 2022-03-17 op unsigned char status, const struct got_error *orig_error)
548 60aa1fa0 2022-03-17 op {
549 60aa1fa0 2022-03-17 op const struct got_error *err;
550 60aa1fa0 2022-03-17 op struct got_patch_hunk *h;
551 60aa1fa0 2022-03-17 op
552 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, status,
553 60aa1fa0 2022-03-17 op orig_error, 0, 0, 0, 0, 0, NULL);
554 60aa1fa0 2022-03-17 op if (err)
555 60aa1fa0 2022-03-17 op return err;
556 60aa1fa0 2022-03-17 op
557 60aa1fa0 2022-03-17 op STAILQ_FOREACH(h, pa->head, entries) {
558 60aa1fa0 2022-03-17 op if (h->offset == 0 && h->err == NULL)
559 60aa1fa0 2022-03-17 op continue;
560 60aa1fa0 2022-03-17 op
561 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
562 60aa1fa0 2022-03-17 op h->old_from, h->old_lines, h->new_from, h->new_lines,
563 60aa1fa0 2022-03-17 op h->offset, h->err);
564 60aa1fa0 2022-03-17 op if (err)
565 60aa1fa0 2022-03-17 op return err;
566 60aa1fa0 2022-03-17 op }
567 60aa1fa0 2022-03-17 op
568 60aa1fa0 2022-03-17 op return NULL;
569 60aa1fa0 2022-03-17 op }
570 60aa1fa0 2022-03-17 op
571 60aa1fa0 2022-03-17 op static const struct got_error *
572 b22138f5 2022-03-16 op patch_delete(void *arg, unsigned char status, unsigned char staged_status,
573 b22138f5 2022-03-16 op const char *path)
574 b22138f5 2022-03-16 op {
575 60aa1fa0 2022-03-17 op return report_progress(arg, path, NULL, status, NULL);
576 b22138f5 2022-03-16 op }
577 b22138f5 2022-03-16 op
578 b22138f5 2022-03-16 op static const struct got_error *
579 b22138f5 2022-03-16 op patch_add(void *arg, unsigned char status, const char *path)
580 b22138f5 2022-03-16 op {
581 60aa1fa0 2022-03-17 op return report_progress(arg, NULL, path, status, NULL);
582 b22138f5 2022-03-16 op }
583 b22138f5 2022-03-16 op
584 b22138f5 2022-03-16 op static const struct got_error *
585 55e9459f 2022-06-19 op open_blob(char **path, FILE **fp, const char *blobid,
586 55e9459f 2022-06-19 op struct got_repository *repo)
587 6e96b326 2022-03-12 op {
588 6e96b326 2022-03-12 op const struct got_error *err = NULL;
589 55e9459f 2022-06-19 op struct got_blob_object *blob = NULL;
590 db0dfdd7 2022-06-27 op struct got_object_id id, *idptr, *matched_id = NULL;
591 eb81bc23 2022-06-28 tracey int fd = -1;
592 55e9459f 2022-06-19 op
593 55e9459f 2022-06-19 op *fp = NULL;
594 55e9459f 2022-06-19 op *path = NULL;
595 55e9459f 2022-06-19 op
596 db0dfdd7 2022-06-27 op if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
597 db0dfdd7 2022-06-27 op err = got_repo_match_object_id(&matched_id, NULL, blobid,
598 db0dfdd7 2022-06-27 op GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
599 db0dfdd7 2022-06-27 op repo);
600 db0dfdd7 2022-06-27 op if (err)
601 db0dfdd7 2022-06-27 op return err;
602 db0dfdd7 2022-06-27 op idptr = matched_id;
603 db0dfdd7 2022-06-27 op } else {
604 db0dfdd7 2022-06-27 op if (!got_parse_sha1_digest(id.sha1, blobid))
605 db0dfdd7 2022-06-27 op return got_error(GOT_ERR_BAD_OBJ_ID_STR);
606 db0dfdd7 2022-06-27 op idptr = &id;
607 db0dfdd7 2022-06-27 op }
608 55e9459f 2022-06-19 op
609 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
610 eb81bc23 2022-06-28 tracey if (fd == -1) {
611 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
612 eb81bc23 2022-06-28 tracey goto done;
613 eb81bc23 2022-06-28 tracey }
614 eb81bc23 2022-06-28 tracey
615 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
616 55e9459f 2022-06-19 op if (err)
617 55e9459f 2022-06-19 op goto done;
618 55e9459f 2022-06-19 op
619 55e9459f 2022-06-19 op err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
620 55e9459f 2022-06-19 op if (err)
621 55e9459f 2022-06-19 op goto done;
622 55e9459f 2022-06-19 op
623 55e9459f 2022-06-19 op err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
624 55e9459f 2022-06-19 op if (err)
625 55e9459f 2022-06-19 op goto done;
626 55e9459f 2022-06-19 op
627 55e9459f 2022-06-19 op done:
628 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
629 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
630 55e9459f 2022-06-19 op if (blob)
631 55e9459f 2022-06-19 op got_object_blob_close(blob);
632 db0dfdd7 2022-06-27 op if (matched_id != NULL)
633 db0dfdd7 2022-06-27 op free(matched_id);
634 55e9459f 2022-06-19 op if (err) {
635 55e9459f 2022-06-19 op if (*fp != NULL)
636 55e9459f 2022-06-19 op fclose(*fp);
637 55e9459f 2022-06-19 op if (*path != NULL)
638 55e9459f 2022-06-19 op unlink(*path);
639 55e9459f 2022-06-19 op free(*path);
640 55e9459f 2022-06-19 op *fp = NULL;
641 55e9459f 2022-06-19 op *path = NULL;
642 55e9459f 2022-06-19 op }
643 55e9459f 2022-06-19 op return err;
644 55e9459f 2022-06-19 op }
645 55e9459f 2022-06-19 op
646 55e9459f 2022-06-19 op static const struct got_error *
647 55e9459f 2022-06-19 op apply_patch(int *overlapcnt, struct got_worktree *worktree,
648 55e9459f 2022-06-19 op struct got_repository *repo, struct got_fileindex *fileindex,
649 55e9459f 2022-06-19 op const char *old, const char *new, struct got_patch *p, int nop,
650 55e9459f 2022-06-19 op struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
651 55e9459f 2022-06-19 op {
652 55e9459f 2022-06-19 op const struct got_error *err = NULL;
653 5dffb1a1 2022-07-02 op struct stat sb;
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 5dffb1a1 2022-07-02 op if (p->old != NULL) {
702 5dffb1a1 2022-07-02 op if ((oldfile = fopen(oldpath, "r")) == NULL) {
703 5dffb1a1 2022-07-02 op err = got_error_from_errno2("open", oldpath);
704 5dffb1a1 2022-07-02 op goto done;
705 5dffb1a1 2022-07-02 op }
706 5dffb1a1 2022-07-02 op if (fstat(fileno(oldfile), &sb) == -1) {
707 5dffb1a1 2022-07-02 op err = got_error_from_errno2("fstat", oldpath);
708 5dffb1a1 2022-07-02 op goto done;
709 5dffb1a1 2022-07-02 op }
710 5dffb1a1 2022-07-02 op mode = sb.st_mode;
711 827bcd6c 2022-06-19 op }
712 827bcd6c 2022-06-19 op
713 55e9459f 2022-06-19 op err = got_opentemp_named(&tmppath, &tmpfile, template);
714 6e96b326 2022-03-12 op if (err)
715 6e96b326 2022-03-12 op goto done;
716 55e9459f 2022-06-19 op outpath = tmppath;
717 55e9459f 2022-06-19 op outfd = fileno(tmpfile);
718 5dffb1a1 2022-07-02 op err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
719 6e96b326 2022-03-12 op if (err)
720 6e96b326 2022-03-12 op goto done;
721 6e96b326 2022-03-12 op
722 55e9459f 2022-06-19 op if (do_merge) {
723 497a5915 2022-06-27 op const char *type, *id;
724 497a5915 2022-06-27 op
725 55e9459f 2022-06-19 op if (fseeko(afile, 0, SEEK_SET) == -1 ||
726 55e9459f 2022-06-19 op fseeko(oldfile, 0, SEEK_SET) == -1 ||
727 55e9459f 2022-06-19 op fseeko(tmpfile, 0, SEEK_SET) == -1) {
728 55e9459f 2022-06-19 op err = got_error_from_errno("fseeko");
729 55e9459f 2022-06-19 op goto done;
730 55e9459f 2022-06-19 op }
731 55e9459f 2022-06-19 op
732 55e9459f 2022-06-19 op if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
733 55e9459f 2022-06-19 op err = got_error_from_errno("asprintf");
734 55e9459f 2022-06-19 op oldlabel = NULL;
735 55e9459f 2022-06-19 op goto done;
736 55e9459f 2022-06-19 op }
737 55e9459f 2022-06-19 op
738 55e9459f 2022-06-19 op if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
739 55e9459f 2022-06-19 op err = got_error_from_errno("asprintf");
740 55e9459f 2022-06-19 op newlabel = NULL;
741 55e9459f 2022-06-19 op goto done;
742 55e9459f 2022-06-19 op }
743 55e9459f 2022-06-19 op
744 497a5915 2022-06-27 op if (*p->cid != '\0') {
745 497a5915 2022-06-27 op type = "commit";
746 497a5915 2022-06-27 op id = p->cid;
747 497a5915 2022-06-27 op } else {
748 497a5915 2022-06-27 op type = "blob";
749 497a5915 2022-06-27 op id = p->blob;
750 497a5915 2022-06-27 op }
751 497a5915 2022-06-27 op
752 497a5915 2022-06-27 op if (asprintf(&anclabel, "%s %s", type, id) == -1) {
753 d8b5af43 2022-06-19 op err = got_error_from_errno("asprintf");
754 d8b5af43 2022-06-19 op anclabel = NULL;
755 d8b5af43 2022-06-19 op goto done;
756 d8b5af43 2022-06-19 op }
757 d8b5af43 2022-06-19 op
758 55e9459f 2022-06-19 op err = got_opentemp_named(&mergepath, &mergefile, template);
759 55e9459f 2022-06-19 op if (err)
760 55e9459f 2022-06-19 op goto done;
761 55e9459f 2022-06-19 op outpath = mergepath;
762 55e9459f 2022-06-19 op outfd = fileno(mergefile);
763 55e9459f 2022-06-19 op
764 55e9459f 2022-06-19 op err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
765 d8b5af43 2022-06-19 op oldfile, oldlabel, anclabel, newlabel,
766 55e9459f 2022-06-19 op GOT_DIFF_ALGORITHM_PATIENCE);
767 55e9459f 2022-06-19 op if (err)
768 55e9459f 2022-06-19 op goto done;
769 55e9459f 2022-06-19 op }
770 55e9459f 2022-06-19 op
771 b22138f5 2022-03-16 op if (nop)
772 899fcfdf 2022-03-13 op goto done;
773 899fcfdf 2022-03-13 op
774 5b67f96e 2022-03-13 op if (p->old != NULL && p->new == NULL) {
775 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_rm(old, repo, worktree,
776 f2dd7807 2022-05-17 op fileindex, patch_delete, pa);
777 5b67f96e 2022-03-13 op goto done;
778 5b67f96e 2022-03-13 op }
779 5b67f96e 2022-03-13 op
780 55e9459f 2022-06-19 op if (fchmod(outfd, mode) == -1) {
781 41a37a44 2022-04-23 op err = got_error_from_errno2("chmod", tmppath);
782 2be5e1a2 2022-03-16 op goto done;
783 2be5e1a2 2022-03-16 op }
784 2be5e1a2 2022-03-16 op
785 55e9459f 2022-06-19 op if (rename(outpath, newpath) == -1) {
786 95d68340 2022-03-16 op if (errno != ENOENT) {
787 55e9459f 2022-06-19 op err = got_error_from_errno3("rename", outpath,
788 95d68340 2022-03-16 op newpath);
789 95d68340 2022-03-16 op goto done;
790 95d68340 2022-03-16 op }
791 95d68340 2022-03-16 op
792 95d68340 2022-03-16 op err = got_path_dirname(&parent, newpath);
793 95d68340 2022-03-16 op if (err != NULL)
794 95d68340 2022-03-16 op goto done;
795 95d68340 2022-03-16 op err = got_path_mkdir(parent);
796 95d68340 2022-03-16 op if (err != NULL)
797 95d68340 2022-03-16 op goto done;
798 55e9459f 2022-06-19 op if (rename(outpath, newpath) == -1) {
799 55e9459f 2022-06-19 op err = got_error_from_errno3("rename", outpath,
800 95d68340 2022-03-16 op newpath);
801 95d68340 2022-03-16 op goto done;
802 95d68340 2022-03-16 op }
803 6e96b326 2022-03-12 op }
804 6e96b326 2022-03-12 op
805 6e96b326 2022-03-12 op if (file_renamed) {
806 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_rm(old, repo, worktree,
807 f2dd7807 2022-05-17 op fileindex, patch_delete, pa);
808 6e96b326 2022-03-12 op if (err == NULL)
809 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_add(new, repo,
810 f2dd7807 2022-05-17 op worktree, fileindex, patch_add,
811 f2dd7807 2022-05-17 op pa);
812 78f5ac24 2022-03-19 op if (err)
813 78f5ac24 2022-03-19 op unlink(newpath);
814 78f5ac24 2022-03-19 op } else if (p->old == NULL) {
815 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_add(new, repo, worktree,
816 f2dd7807 2022-05-17 op fileindex, patch_add, pa);
817 78f5ac24 2022-03-19 op if (err)
818 78f5ac24 2022-03-19 op unlink(newpath);
819 55e9459f 2022-06-19 op } else if (*overlapcnt != 0)
820 55e9459f 2022-06-19 op err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
821 9802c41c 2022-06-21 op else if (do_merge)
822 9802c41c 2022-06-21 op err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
823 55e9459f 2022-06-19 op else
824 ed3bff83 2022-04-23 op err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
825 6e96b326 2022-03-12 op
826 e9ce266e 2022-03-07 op done:
827 95d68340 2022-03-16 op free(parent);
828 6f5cb1bd 2022-03-08 op free(template);
829 55e9459f 2022-06-19 op
830 e9ce266e 2022-03-07 op if (tmppath != NULL)
831 e9ce266e 2022-03-07 op unlink(tmppath);
832 55e9459f 2022-06-19 op if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
833 06c44edc 2022-06-15 stsp err = got_error_from_errno("fclose");
834 e9ce266e 2022-03-07 op free(tmppath);
835 55e9459f 2022-06-19 op
836 ed3bff83 2022-04-23 op free(oldpath);
837 827bcd6c 2022-06-19 op if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
838 827bcd6c 2022-06-19 op err = got_error_from_errno("fclose");
839 55e9459f 2022-06-19 op
840 55e9459f 2022-06-19 op if (apath != NULL)
841 55e9459f 2022-06-19 op unlink(apath);
842 55e9459f 2022-06-19 op if (afile != NULL && fclose(afile) == EOF && err == NULL)
843 55e9459f 2022-06-19 op err = got_error_from_errno("fclose");
844 55e9459f 2022-06-19 op free(apath);
845 55e9459f 2022-06-19 op
846 55e9459f 2022-06-19 op if (mergepath != NULL)
847 55e9459f 2022-06-19 op unlink(mergepath);
848 55e9459f 2022-06-19 op if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
849 55e9459f 2022-06-19 op err = got_error_from_errno("fclose");
850 55e9459f 2022-06-19 op free(mergepath);
851 55e9459f 2022-06-19 op
852 ed3bff83 2022-04-23 op free(newpath);
853 55e9459f 2022-06-19 op free(oldlabel);
854 55e9459f 2022-06-19 op free(newlabel);
855 d8b5af43 2022-06-19 op free(anclabel);
856 e9ce266e 2022-03-07 op return err;
857 bad961bf 2022-04-23 op }
858 bad961bf 2022-04-23 op
859 bad961bf 2022-04-23 op static void
860 bad961bf 2022-04-23 op reverse_patch(struct got_patch *p)
861 bad961bf 2022-04-23 op {
862 bad961bf 2022-04-23 op struct got_patch_hunk *h;
863 bad961bf 2022-04-23 op size_t i;
864 35095610 2022-06-14 op int tmp;
865 bad961bf 2022-04-23 op
866 bad961bf 2022-04-23 op STAILQ_FOREACH(h, &p->head, entries) {
867 bad961bf 2022-04-23 op tmp = h->old_from;
868 bad961bf 2022-04-23 op h->old_from = h->new_from;
869 bad961bf 2022-04-23 op h->new_from = tmp;
870 bad961bf 2022-04-23 op
871 bad961bf 2022-04-23 op tmp = h->old_lines;
872 bad961bf 2022-04-23 op h->old_lines = h->new_lines;
873 bad961bf 2022-04-23 op h->new_lines = tmp;
874 bad961bf 2022-04-23 op
875 bad961bf 2022-04-23 op tmp = h->old_nonl;
876 bad961bf 2022-04-23 op h->old_nonl = h->new_nonl;
877 bad961bf 2022-04-23 op h->new_nonl = tmp;
878 bad961bf 2022-04-23 op
879 bad961bf 2022-04-23 op for (i = 0; i < h->len; ++i) {
880 bad961bf 2022-04-23 op if (*h->lines[i] == '+')
881 bad961bf 2022-04-23 op *h->lines[i] = '-';
882 bad961bf 2022-04-23 op else if (*h->lines[i] == '-')
883 bad961bf 2022-04-23 op *h->lines[i] = '+';
884 bad961bf 2022-04-23 op }
885 bad961bf 2022-04-23 op }
886 e9ce266e 2022-03-07 op }
887 e9ce266e 2022-03-07 op
888 e9ce266e 2022-03-07 op const struct got_error *
889 e9ce266e 2022-03-07 op got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
890 bad961bf 2022-04-23 op int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
891 bad961bf 2022-04-23 op void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
892 e9ce266e 2022-03-07 op {
893 4bcdc895 2022-05-17 op const struct got_error *err = NULL, *complete_err = NULL;
894 78f5ac24 2022-03-19 op struct got_fileindex *fileindex = NULL;
895 f2dd7807 2022-05-17 op char *fileindex_path = NULL;
896 60aa1fa0 2022-03-17 op char *oldpath, *newpath;
897 e9ce266e 2022-03-07 op struct imsgbuf *ibuf;
898 e9ce266e 2022-03-07 op int imsg_fds[2] = {-1, -1};
899 55e9459f 2022-06-19 op int overlapcnt, done = 0, failed = 0;
900 e9ce266e 2022-03-07 op pid_t pid;
901 e9ce266e 2022-03-07 op
902 e9ce266e 2022-03-07 op ibuf = calloc(1, sizeof(*ibuf));
903 e9ce266e 2022-03-07 op if (ibuf == NULL) {
904 e9ce266e 2022-03-07 op err = got_error_from_errno("calloc");
905 e9ce266e 2022-03-07 op goto done;
906 e9ce266e 2022-03-07 op }
907 e9ce266e 2022-03-07 op
908 e9ce266e 2022-03-07 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
909 e9ce266e 2022-03-07 op err = got_error_from_errno("socketpair");
910 e9ce266e 2022-03-07 op goto done;
911 e9ce266e 2022-03-07 op }
912 e9ce266e 2022-03-07 op
913 e9ce266e 2022-03-07 op pid = fork();
914 e9ce266e 2022-03-07 op if (pid == -1) {
915 e9ce266e 2022-03-07 op err = got_error_from_errno("fork");
916 e9ce266e 2022-03-07 op goto done;
917 e9ce266e 2022-03-07 op } else if (pid == 0) {
918 e9ce266e 2022-03-07 op got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
919 e9ce266e 2022-03-07 op NULL);
920 e9ce266e 2022-03-07 op /* not reached */
921 e9ce266e 2022-03-07 op }
922 e9ce266e 2022-03-07 op
923 e9ce266e 2022-03-07 op if (close(imsg_fds[1]) == -1) {
924 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
925 e9ce266e 2022-03-07 op goto done;
926 e9ce266e 2022-03-07 op }
927 e9ce266e 2022-03-07 op imsg_fds[1] = -1;
928 e9ce266e 2022-03-07 op imsg_init(ibuf, imsg_fds[0]);
929 e9ce266e 2022-03-07 op
930 e9ce266e 2022-03-07 op err = send_patch(ibuf, fd);
931 e9ce266e 2022-03-07 op fd = -1;
932 e9ce266e 2022-03-07 op if (err)
933 e9ce266e 2022-03-07 op goto done;
934 e9ce266e 2022-03-07 op
935 f2dd7807 2022-05-17 op err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
936 f2dd7807 2022-05-17 op worktree);
937 78f5ac24 2022-03-19 op if (err)
938 78f5ac24 2022-03-19 op goto done;
939 78f5ac24 2022-03-19 op
940 e9ce266e 2022-03-07 op while (!done && err == NULL) {
941 e9ce266e 2022-03-07 op struct got_patch p;
942 60aa1fa0 2022-03-17 op struct patch_args pa;
943 e9ce266e 2022-03-07 op
944 60aa1fa0 2022-03-17 op pa.progress_cb = progress_cb;
945 60aa1fa0 2022-03-17 op pa.progress_arg = progress_arg;
946 60aa1fa0 2022-03-17 op pa.head = &p.head;
947 60aa1fa0 2022-03-17 op
948 9d6cabd5 2022-04-07 op err = recv_patch(ibuf, &done, &p, strip);
949 e9ce266e 2022-03-07 op if (err || done)
950 e9ce266e 2022-03-07 op break;
951 e9ce266e 2022-03-07 op
952 bad961bf 2022-04-23 op if (reverse)
953 bad961bf 2022-04-23 op reverse_patch(&p);
954 bad961bf 2022-04-23 op
955 78f5ac24 2022-03-19 op err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
956 78f5ac24 2022-03-19 op &newpath, worktree, repo, fileindex);
957 78f5ac24 2022-03-19 op if (err == NULL)
958 55e9459f 2022-06-19 op err = apply_patch(&overlapcnt, worktree, repo,
959 55e9459f 2022-06-19 op fileindex, oldpath, newpath, &p, nop, &pa,
960 55e9459f 2022-06-19 op cancel_cb, cancel_arg);
961 60aa1fa0 2022-03-17 op if (err != NULL) {
962 60aa1fa0 2022-03-17 op failed = 1;
963 60aa1fa0 2022-03-17 op /* recoverable errors */
964 60aa1fa0 2022-03-17 op if (err->code == GOT_ERR_FILE_STATUS ||
965 60aa1fa0 2022-03-17 op (err->code == GOT_ERR_ERRNO && errno == ENOENT))
966 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
967 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, err);
968 60aa1fa0 2022-03-17 op else if (err->code == GOT_ERR_HUNK_FAILED)
969 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
970 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, NULL);
971 60aa1fa0 2022-03-17 op }
972 55e9459f 2022-06-19 op if (overlapcnt != 0)
973 55e9459f 2022-06-19 op failed = 1;
974 60aa1fa0 2022-03-17 op
975 60aa1fa0 2022-03-17 op free(oldpath);
976 60aa1fa0 2022-03-17 op free(newpath);
977 e9ce266e 2022-03-07 op patch_free(&p);
978 60aa1fa0 2022-03-17 op
979 e9ce266e 2022-03-07 op if (err)
980 e9ce266e 2022-03-07 op break;
981 e9ce266e 2022-03-07 op }
982 e9ce266e 2022-03-07 op
983 e9ce266e 2022-03-07 op done:
984 4bcdc895 2022-05-17 op if (fileindex != NULL)
985 4bcdc895 2022-05-17 op complete_err = got_worktree_patch_complete(fileindex,
986 4bcdc895 2022-05-17 op fileindex_path);
987 f2dd7807 2022-05-17 op if (complete_err && err == NULL)
988 f2dd7807 2022-05-17 op err = complete_err;
989 4bcdc895 2022-05-17 op free(fileindex_path);
990 e9ce266e 2022-03-07 op if (fd != -1 && close(fd) == -1 && err == NULL)
991 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
992 e9ce266e 2022-03-07 op if (ibuf != NULL)
993 e9ce266e 2022-03-07 op imsg_clear(ibuf);
994 e9ce266e 2022-03-07 op if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
995 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
996 e9ce266e 2022-03-07 op if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
997 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
998 60aa1fa0 2022-03-17 op if (err == NULL && failed)
999 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_PATCH_FAILED);
1000 e9ce266e 2022-03-07 op return err;
1001 e9ce266e 2022-03-07 op }