Blame


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