2 e9ce266e 2022-03-07 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
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.
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.
16 e9ce266e 2022-03-07 op * Apply patches.
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?
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>
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 5822e79e 2023-02-23 op #include <sha2.h>
34 e9ce266e 2022-03-07 op #include <stdint.h>
35 e9ce266e 2022-03-07 op #include <stdio.h>
36 e9ce266e 2022-03-07 op #include <stdlib.h>
37 e9ce266e 2022-03-07 op #include <string.h>
38 e9ce266e 2022-03-07 op #include <unistd.h>
39 e9ce266e 2022-03-07 op #include <imsg.h>
41 e9ce266e 2022-03-07 op #include "got_error.h"
42 e9ce266e 2022-03-07 op #include "got_object.h"
43 e9ce266e 2022-03-07 op #include "got_path.h"
44 e9ce266e 2022-03-07 op #include "got_reference.h"
45 e9ce266e 2022-03-07 op #include "got_cancel.h"
46 e9ce266e 2022-03-07 op #include "got_worktree.h"
47 55e9459f 2022-06-19 op #include "got_repository.h"
48 e9ce266e 2022-03-07 op #include "got_opentemp.h"
49 e9ce266e 2022-03-07 op #include "got_patch.h"
50 4b752015 2022-06-30 stsp #include "got_diff.h"
52 e9ce266e 2022-03-07 op #include "got_lib_delta.h"
53 55e9459f 2022-06-19 op #include "got_lib_diff.h"
54 e9ce266e 2022-03-07 op #include "got_lib_object.h"
55 e9ce266e 2022-03-07 op #include "got_lib_privsep.h"
56 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
58 e9ce266e 2022-03-07 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
60 e9ce266e 2022-03-07 op struct got_patch_hunk {
61 e9ce266e 2022-03-07 op STAILQ_ENTRY(got_patch_hunk) entries;
62 60aa1fa0 2022-03-17 op const struct got_error *err;
63 a92a2042 2022-07-02 op int ws_mangled;
68 35095610 2022-06-14 op int old_lines;
70 35095610 2022-06-14 op int new_lines;
76 60aa1fa0 2022-03-17 op STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
77 e9ce266e 2022-03-07 op struct got_patch {
82 55e9459f 2022-06-19 op char blob[41];
83 60aa1fa0 2022-03-17 op struct got_patch_hunk_head head;
86 b22138f5 2022-03-16 op struct patch_args {
87 b22138f5 2022-03-16 op got_patch_progress_cb progress_cb;
88 b22138f5 2022-03-16 op void *progress_arg;
89 60aa1fa0 2022-03-17 op struct got_patch_hunk_head *head;
93 b2b3fce1 2022-10-29 op apply_umask(mode_t mode)
97 b2b3fce1 2022-10-29 op um = umask(000);
99 b2b3fce1 2022-10-29 op return mode & ~um;
102 e9ce266e 2022-03-07 op static const struct got_error *
103 e9ce266e 2022-03-07 op send_patch(struct imsgbuf *ibuf, int fd)
105 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
107 e9ce266e 2022-03-07 op if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
108 e9ce266e 2022-03-07 op NULL, 0) == -1) {
109 e9ce266e 2022-03-07 op err = got_error_from_errno(
110 e9ce266e 2022-03-07 op "imsg_compose GOT_IMSG_PATCH_FILE");
115 08f44789 2022-07-07 op return got_privsep_flush_imsg(ibuf);
119 e9ce266e 2022-03-07 op patch_free(struct got_patch *p)
121 e9ce266e 2022-03-07 op struct got_patch_hunk *h;
124 e9ce266e 2022-03-07 op while (!STAILQ_EMPTY(&p->head)) {
125 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
126 e9ce266e 2022-03-07 op STAILQ_REMOVE_HEAD(&p->head, entries);
128 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i)
129 e9ce266e 2022-03-07 op free(h->lines[i]);
130 e9ce266e 2022-03-07 op free(h->lines);
134 e9ce266e 2022-03-07 op free(p->new);
135 e9ce266e 2022-03-07 op free(p->old);
137 0e07a2a1 2022-06-13 op memset(p, 0, sizeof(*p));
138 0e07a2a1 2022-06-13 op STAILQ_INIT(&p->head);
141 e9ce266e 2022-03-07 op static const struct got_error *
142 e9ce266e 2022-03-07 op pushline(struct got_patch_hunk *h, const char *line)
145 e9ce266e 2022-03-07 op size_t newcap;
147 e9ce266e 2022-03-07 op if (h->len == h->cap) {
148 e9ce266e 2022-03-07 op if ((newcap = h->cap * 1.5) == 0)
150 e9ce266e 2022-03-07 op t = recallocarray(h->lines, h->cap, newcap,
151 e9ce266e 2022-03-07 op sizeof(h->lines[0]));
152 e9ce266e 2022-03-07 op if (t == NULL)
153 e9ce266e 2022-03-07 op return got_error_from_errno("recallocarray");
154 e9ce266e 2022-03-07 op h->lines = t;
155 e9ce266e 2022-03-07 op h->cap = newcap;
158 e9ce266e 2022-03-07 op if ((t = strdup(line)) == NULL)
159 e9ce266e 2022-03-07 op return got_error_from_errno("strdup");
161 e9ce266e 2022-03-07 op h->lines[h->len++] = t;
165 e9ce266e 2022-03-07 op static const struct got_error *
166 9d6cabd5 2022-04-07 op recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
168 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
169 e9ce266e 2022-03-07 op struct imsg imsg;
170 e9ce266e 2022-03-07 op struct got_imsg_patch_hunk hdr;
171 e9ce266e 2022-03-07 op struct got_imsg_patch patch;
172 e9ce266e 2022-03-07 op struct got_patch_hunk *h = NULL;
173 e9ce266e 2022-03-07 op size_t datalen;
174 b2832778 2022-04-23 op int lastmode = -1;
176 e9ce266e 2022-03-07 op memset(p, 0, sizeof(*p));
177 e9ce266e 2022-03-07 op STAILQ_INIT(&p->head);
179 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
182 e9ce266e 2022-03-07 op if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
186 e9ce266e 2022-03-07 op if (imsg.hdr.type != GOT_IMSG_PATCH) {
187 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
190 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
191 e9ce266e 2022-03-07 op if (datalen != sizeof(patch)) {
192 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
195 e9ce266e 2022-03-07 op memcpy(&patch, imsg.data, sizeof(patch));
197 99b94ad7 2022-06-13 op if (patch.old[sizeof(patch.old)-1] != '\0' ||
198 55e9459f 2022-06-19 op patch.new[sizeof(patch.new)-1] != '\0' ||
199 d8b5af43 2022-06-19 op patch.cid[sizeof(patch.cid)-1] != '\0' ||
200 55e9459f 2022-06-19 op patch.blob[sizeof(patch.blob)-1] != '\0') {
201 99b94ad7 2022-06-13 op err = got_error(GOT_ERR_PRIVSEP_LEN);
205 497a5915 2022-06-27 op if (*patch.cid != '\0')
206 d8b5af43 2022-06-19 op strlcpy(p->cid, patch.cid, sizeof(p->cid));
208 497a5915 2022-06-27 op if (*patch.blob != '\0')
209 d8b5af43 2022-06-19 op strlcpy(p->blob, patch.blob, sizeof(p->blob));
211 611e5fc2 2022-09-21 mark p->xbit = patch.xbit;
213 9d6cabd5 2022-04-07 op /* automatically set strip=1 for git-style diffs */
214 9d6cabd5 2022-04-07 op if (strip == -1 && patch.git &&
215 9d6cabd5 2022-04-07 op (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
216 9d6cabd5 2022-04-07 op (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
219 9d6cabd5 2022-04-07 op /* prefer the new name if not /dev/null for not git-style diffs */
220 9d6cabd5 2022-04-07 op if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
221 9d6cabd5 2022-04-07 op err = got_path_strip(&p->old, patch.new, strip);
224 9d6cabd5 2022-04-07 op } else if (*patch.old != '\0') {
225 9d6cabd5 2022-04-07 op err = got_path_strip(&p->old, patch.old, strip);
230 9d6cabd5 2022-04-07 op if (*patch.new != '\0') {
231 9d6cabd5 2022-04-07 op err = got_path_strip(&p->new, patch.new, strip);
236 b95c53df 2022-03-12 op if (p->old == NULL && p->new == NULL) {
237 b95c53df 2022-03-12 op err = got_error(GOT_ERR_PATCH_MALFORMED);
241 e9ce266e 2022-03-07 op imsg_free(&imsg);
246 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
248 0e07a2a1 2022-06-13 op patch_free(p);
252 2399b53d 2022-06-14 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
253 e9ce266e 2022-03-07 op switch (imsg.hdr.type) {
254 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_DONE:
255 be33dff7 2022-05-13 op if (h != NULL && h->len == 0)
256 be33dff7 2022-05-13 op err = got_error(GOT_ERR_PATCH_MALFORMED);
258 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_HUNK:
259 be33dff7 2022-05-13 op if (h != NULL &&
260 be33dff7 2022-05-13 op (h->len == 0 || h->old_nonl || h->new_nonl)) {
261 b3c57ab2 2022-03-22 op err = got_error(GOT_ERR_PATCH_MALFORMED);
264 b2832778 2022-04-23 op lastmode = -1;
265 e9ce266e 2022-03-07 op if (datalen != sizeof(hdr)) {
266 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
269 e9ce266e 2022-03-07 op memcpy(&hdr, imsg.data, sizeof(hdr));
270 5b7126c5 2022-06-14 op if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
271 5b7126c5 2022-06-14 op err = got_error(GOT_ERR_PRIVSEP_LEN);
274 5b7126c5 2022-06-14 op if ((h = calloc(1, sizeof(*h))) == NULL) {
275 5b7126c5 2022-06-14 op err = got_error_from_errno("calloc");
278 e9ce266e 2022-03-07 op h->old_from = hdr.oldfrom;
279 e9ce266e 2022-03-07 op h->old_lines = hdr.oldlines;
280 e9ce266e 2022-03-07 op h->new_from = hdr.newfrom;
281 e9ce266e 2022-03-07 op h->new_lines = hdr.newlines;
282 e9ce266e 2022-03-07 op STAILQ_INSERT_TAIL(&p->head, h, entries);
284 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_LINE:
285 e9ce266e 2022-03-07 op if (h == NULL) {
286 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
289 e9ce266e 2022-03-07 op t = imsg.data;
290 b3c57ab2 2022-03-22 op /* at least one char */
291 e9ce266e 2022-03-07 op if (datalen < 2 || t[datalen-1] != '\0') {
292 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
295 b3c57ab2 2022-03-22 op if (*t != ' ' && *t != '-' && *t != '+' &&
296 b3c57ab2 2022-03-22 op *t != '\\') {
297 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
301 b2832778 2022-04-23 op if (*t != '\\')
302 b3c57ab2 2022-03-22 op err = pushline(h, t);
303 b2832778 2022-04-23 op else if (lastmode == '-')
304 b2832778 2022-04-23 op h->old_nonl = 1;
305 b2832778 2022-04-23 op else if (lastmode == '+')
306 b2832778 2022-04-23 op h->new_nonl = 1;
308 b2832778 2022-04-23 op err = got_error(GOT_ERR_PATCH_MALFORMED);
313 b2832778 2022-04-23 op lastmode = *t;
316 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
320 e9ce266e 2022-03-07 op imsg_free(&imsg);
325 0e07a2a1 2022-06-13 op patch_free(p);
327 e9ce266e 2022-03-07 op imsg_free(&imsg);
332 38d61ead 2022-07-23 op reverse_patch(struct got_patch *p)
334 38d61ead 2022-07-23 op struct got_patch_hunk *h;
338 38d61ead 2022-07-23 op STAILQ_FOREACH(h, &p->head, entries) {
339 38d61ead 2022-07-23 op tmp = h->old_from;
340 38d61ead 2022-07-23 op h->old_from = h->new_from;
341 38d61ead 2022-07-23 op h->new_from = tmp;
343 38d61ead 2022-07-23 op tmp = h->old_lines;
344 38d61ead 2022-07-23 op h->old_lines = h->new_lines;
345 38d61ead 2022-07-23 op h->new_lines = tmp;
347 38d61ead 2022-07-23 op tmp = h->old_nonl;
348 38d61ead 2022-07-23 op h->old_nonl = h->new_nonl;
349 38d61ead 2022-07-23 op h->new_nonl = tmp;
351 38d61ead 2022-07-23 op for (i = 0; i < h->len; ++i) {
352 38d61ead 2022-07-23 op if (*h->lines[i] == '+')
353 38d61ead 2022-07-23 op *h->lines[i] = '-';
354 38d61ead 2022-07-23 op else if (*h->lines[i] == '-')
355 38d61ead 2022-07-23 op *h->lines[i] = '+';
361 e9ce266e 2022-03-07 op * Copy data from orig starting at copypos until pos into tmp.
362 e9ce266e 2022-03-07 op * If pos is -1, copy until EOF.
364 e9ce266e 2022-03-07 op static const struct got_error *
365 e9ce266e 2022-03-07 op copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
367 e9ce266e 2022-03-07 op char buf[BUFSIZ];
368 e9ce266e 2022-03-07 op size_t len, r, w;
370 e45f7eba 2022-05-14 naddy if (fseeko(orig, copypos, SEEK_SET) == -1)
371 e45f7eba 2022-05-14 naddy return got_error_from_errno("fseeko");
373 e9ce266e 2022-03-07 op while (pos == -1 || copypos < pos) {
374 e9ce266e 2022-03-07 op len = sizeof(buf);
376 e9ce266e 2022-03-07 op len = MIN(len, (size_t)pos - copypos);
377 e9ce266e 2022-03-07 op r = fread(buf, 1, len, orig);
378 e9ce266e 2022-03-07 op if (r != len && ferror(orig))
379 e9ce266e 2022-03-07 op return got_error_from_errno("fread");
380 e9ce266e 2022-03-07 op w = fwrite(buf, 1, r, tmp);
382 e9ce266e 2022-03-07 op return got_error_from_errno("fwrite");
383 e9ce266e 2022-03-07 op copypos += len;
384 e9ce266e 2022-03-07 op if (r != len && feof(orig)) {
385 e9ce266e 2022-03-07 op if (pos == -1)
387 60aa1fa0 2022-03-17 op return got_error(GOT_ERR_HUNK_FAILED);
393 445d38d7 2022-08-01 op static int linecmp(const char *, const char *, int *);
395 e9ce266e 2022-03-07 op static const struct got_error *
396 35095610 2022-06-14 op locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
398 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
399 e9ce266e 2022-03-07 op char *line = NULL;
400 e9ce266e 2022-03-07 op char mode = *h->lines[0];
401 e9ce266e 2022-03-07 op size_t linesize = 0;
402 e9ce266e 2022-03-07 op ssize_t linelen;
403 e9ce266e 2022-03-07 op off_t match = -1;
404 445d38d7 2022-08-01 op int mangled = 0, match_lineno = -1;
407 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
408 e9ce266e 2022-03-07 op if (linelen == -1) {
409 e9ce266e 2022-03-07 op if (ferror(orig))
410 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
411 e9ce266e 2022-03-07 op else if (match == -1)
412 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_HUNK_FAILED);
415 b3c57ab2 2022-03-22 op if (line[linelen - 1] == '\n')
416 b3c57ab2 2022-03-22 op line[linelen - 1] = '\0';
419 445d38d7 2022-08-01 op if ((mode == ' ' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
420 445d38d7 2022-08-01 op (mode == '-' && !linecmp(h->lines[0] + 1, line, &mangled)) ||
421 e9ce266e 2022-03-07 op (mode == '+' && *lineno == h->old_from)) {
422 e9ce266e 2022-03-07 op match = ftello(orig);
423 e9ce266e 2022-03-07 op if (match == -1) {
424 e9ce266e 2022-03-07 op err = got_error_from_errno("ftello");
427 e9ce266e 2022-03-07 op match -= linelen;
428 e9ce266e 2022-03-07 op match_lineno = (*lineno)-1;
431 d7c808b7 2022-08-01 op if (*lineno >= h->old_from && match != -1) {
433 d7c808b7 2022-08-01 op h->ws_mangled = 1;
438 e9ce266e 2022-03-07 op if (err == NULL) {
439 33df9995 2022-03-11 op *pos = match;
440 e9ce266e 2022-03-07 op *lineno = match_lineno;
441 e45f7eba 2022-05-14 naddy if (fseeko(orig, match, SEEK_SET) == -1)
442 e45f7eba 2022-05-14 naddy err = got_error_from_errno("fseeko");
450 a92a2042 2022-07-02 op linecmp(const char *a, const char *b, int *mangled)
454 a92a2042 2022-07-02 op *mangled = 0;
455 a92a2042 2022-07-02 op c = strcmp(a, b);
459 a92a2042 2022-07-02 op *mangled = 1;
461 2c986b8f 2022-07-04 op while (*a == '\t' || *a == ' ' || *a == '\f')
463 2c986b8f 2022-07-04 op while (*b == '\t' || *b == ' ' || *b == '\f')
465 2c986b8f 2022-07-04 op if (*a == '\0' || *a != *b)
470 a92a2042 2022-07-02 op return *a - *b;
473 e9ce266e 2022-03-07 op static const struct got_error *
474 e9ce266e 2022-03-07 op test_hunk(FILE *orig, struct got_patch_hunk *h)
476 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
477 e9ce266e 2022-03-07 op char *line = NULL;
478 e9ce266e 2022-03-07 op size_t linesize = 0, i = 0;
479 e9ce266e 2022-03-07 op ssize_t linelen;
482 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
483 e9ce266e 2022-03-07 op switch (*h->lines[i]) {
488 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
489 e9ce266e 2022-03-07 op if (linelen == -1) {
490 e9ce266e 2022-03-07 op if (ferror(orig))
491 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
493 e9ce266e 2022-03-07 op err = got_error(
494 60aa1fa0 2022-03-17 op GOT_ERR_HUNK_FAILED);
497 b3c57ab2 2022-03-22 op if (line[linelen - 1] == '\n')
498 b3c57ab2 2022-03-22 op line[linelen - 1] = '\0';
499 a92a2042 2022-07-02 op if (linecmp(h->lines[i] + 1, line, &mangled)) {
500 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_HUNK_FAILED);
504 a92a2042 2022-07-02 op h->ws_mangled = 1;
514 e9ce266e 2022-03-07 op static const struct got_error *
515 a92a2042 2022-07-02 op apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
518 a92a2042 2022-07-02 op const struct got_error *err = NULL;
519 a92a2042 2022-07-02 op const char *t;
520 a92a2042 2022-07-02 op size_t linesize = 0, i, new = 0;
521 a92a2042 2022-07-02 op char *line = NULL;
523 a92a2042 2022-07-02 op ssize_t linelen;
525 a92a2042 2022-07-02 op if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
526 a92a2042 2022-07-02 op return got_error_from_errno("fseeko");
528 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
529 a92a2042 2022-07-02 op switch (mode = *h->lines[i]) {
533 a92a2042 2022-07-02 op if (orig != NULL) {
534 a92a2042 2022-07-02 op linelen = getline(&line, &linesize, orig);
535 a92a2042 2022-07-02 op if (linelen == -1) {
536 a92a2042 2022-07-02 op err = got_error_from_errno("getline");
539 a92a2042 2022-07-02 op if (line[linelen - 1] == '\n')
540 a92a2042 2022-07-02 op line[linelen - 1] = '\0';
543 a92a2042 2022-07-02 op t = h->lines[i] + 1;
544 a92a2042 2022-07-02 op if (mode == '-')
546 a92a2042 2022-07-02 op if (fprintf(tmp, "%s\n", t) < 0) {
547 a92a2042 2022-07-02 op err = got_error_from_errno("fprintf");
553 a92a2042 2022-07-02 op if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
554 a92a2042 2022-07-02 op err = got_error_from_errno("fprintf");
557 b2832778 2022-04-23 op if (new != h->new_lines || !h->new_nonl) {
558 a92a2042 2022-07-02 op if (fprintf(tmp, "\n") < 0) {
559 a92a2042 2022-07-02 op err = got_error_from_errno("fprintf");
572 6e96b326 2022-03-12 op static const struct got_error *
573 5dffb1a1 2022-07-02 op patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
575 6e96b326 2022-03-12 op const struct got_error *err = NULL;
576 6e96b326 2022-03-12 op struct got_patch_hunk *h;
577 2be5e1a2 2022-03-16 op struct stat sb;
578 35095610 2022-06-14 op int lineno = 0;
579 6e96b326 2022-03-12 op off_t copypos, pos;
580 6e96b326 2022-03-12 op char *line = NULL;
581 6e96b326 2022-03-12 op size_t linesize = 0;
582 6e96b326 2022-03-12 op ssize_t linelen;
584 e9ce266e 2022-03-07 op if (p->old == NULL) { /* create */
585 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
586 6e96b326 2022-03-12 op if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
587 6e96b326 2022-03-12 op return got_error(GOT_ERR_PATCH_MALFORMED);
588 a92a2042 2022-07-02 op return apply_hunk(orig, tmp, h, &lineno, 0);
591 684a9a6c 2022-12-31 op /* When deleting binary files there are no hunks to apply. */
592 684a9a6c 2022-12-31 op if (p->new == NULL && STAILQ_EMPTY(&p->head))
595 827bcd6c 2022-06-19 op if (fstat(fileno(orig), &sb) == -1)
596 827bcd6c 2022-06-19 op return got_error_from_errno("fstat");
599 e9ce266e 2022-03-07 op STAILQ_FOREACH(h, &p->head, entries) {
601 33df9995 2022-03-11 op err = locate_hunk(orig, h, &pos, &lineno);
602 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
603 60aa1fa0 2022-03-17 op h->err = err;
604 e9ce266e 2022-03-07 op if (err != NULL)
606 05737d49 2022-06-19 op err = copy(tmp, orig, copypos, pos);
607 e9ce266e 2022-03-07 op if (err != NULL)
609 e9ce266e 2022-03-07 op copypos = pos;
611 e9ce266e 2022-03-07 op err = test_hunk(orig, h);
612 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
614 e9ce266e 2022-03-07 op * try to apply the hunk again starting the search
615 e9ce266e 2022-03-07 op * after the previous partial match.
617 827bcd6c 2022-06-19 op if (fseeko(orig, pos, SEEK_SET) == -1)
618 827bcd6c 2022-06-19 op return got_error_from_errno("fseeko");
619 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
620 827bcd6c 2022-06-19 op if (linelen == -1)
621 827bcd6c 2022-06-19 op return got_error_from_errno("getline");
623 e9ce266e 2022-03-07 op goto tryagain;
625 e9ce266e 2022-03-07 op if (err != NULL)
628 60aa1fa0 2022-03-17 op if (lineno + 1 != h->old_from)
629 60aa1fa0 2022-03-17 op h->offset = lineno + 1 - h->old_from;
631 a92a2042 2022-07-02 op err = apply_hunk(orig, tmp, h, &lineno, pos);
632 e9ce266e 2022-03-07 op if (err != NULL)
635 e9ce266e 2022-03-07 op copypos = ftello(orig);
636 827bcd6c 2022-06-19 op if (copypos == -1)
637 827bcd6c 2022-06-19 op return got_error_from_errno("ftello");
640 60aa1fa0 2022-03-17 op if (p->new == NULL && sb.st_size != copypos) {
641 60aa1fa0 2022-03-17 op h = STAILQ_FIRST(&p->head);
642 60aa1fa0 2022-03-17 op h->err = got_error(GOT_ERR_HUNK_FAILED);
643 60aa1fa0 2022-03-17 op err = h->err;
644 05737d49 2022-06-19 op } else if (!feof(orig))
645 e9ce266e 2022-03-07 op err = copy(tmp, orig, copypos, -1);
650 dbda770b 2022-03-13 op static const struct got_error *
651 60aa1fa0 2022-03-17 op report_progress(struct patch_args *pa, const char *old, const char *new,
652 60aa1fa0 2022-03-17 op unsigned char status, const struct got_error *orig_error)
654 60aa1fa0 2022-03-17 op const struct got_error *err;
655 60aa1fa0 2022-03-17 op struct got_patch_hunk *h;
657 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, status,
658 a92a2042 2022-07-02 op orig_error, 0, 0, 0, 0, 0, 0, NULL);
662 60aa1fa0 2022-03-17 op STAILQ_FOREACH(h, pa->head, entries) {
663 a92a2042 2022-07-02 op if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
666 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
667 60aa1fa0 2022-03-17 op h->old_from, h->old_lines, h->new_from, h->new_lines,
668 a92a2042 2022-07-02 op h->offset, h->ws_mangled, h->err);
676 60aa1fa0 2022-03-17 op static const struct got_error *
677 b22138f5 2022-03-16 op patch_delete(void *arg, unsigned char status, unsigned char staged_status,
678 b22138f5 2022-03-16 op const char *path)
680 60aa1fa0 2022-03-17 op return report_progress(arg, path, NULL, status, NULL);
683 b22138f5 2022-03-16 op static const struct got_error *
684 b22138f5 2022-03-16 op patch_add(void *arg, unsigned char status, const char *path)
686 60aa1fa0 2022-03-17 op return report_progress(arg, NULL, path, status, NULL);
689 b22138f5 2022-03-16 op static const struct got_error *
690 55e9459f 2022-06-19 op open_blob(char **path, FILE **fp, const char *blobid,
691 55e9459f 2022-06-19 op struct got_repository *repo)
693 6e96b326 2022-03-12 op const struct got_error *err = NULL;
694 55e9459f 2022-06-19 op struct got_blob_object *blob = NULL;
695 db0dfdd7 2022-06-27 op struct got_object_id id, *idptr, *matched_id = NULL;
696 eb81bc23 2022-06-28 tracey int fd = -1;
699 55e9459f 2022-06-19 op *path = NULL;
701 db0dfdd7 2022-06-27 op if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
702 db0dfdd7 2022-06-27 op err = got_repo_match_object_id(&matched_id, NULL, blobid,
703 db0dfdd7 2022-06-27 op GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
707 db0dfdd7 2022-06-27 op idptr = matched_id;
709 87a3ab84 2023-02-23 op if (!got_parse_object_id(&id, blobid, GOT_HASH_SHA1))
710 db0dfdd7 2022-06-27 op return got_error(GOT_ERR_BAD_OBJ_ID_STR);
714 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
715 eb81bc23 2022-06-28 tracey if (fd == -1) {
716 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
717 eb81bc23 2022-06-28 tracey goto done;
720 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
724 b90054ed 2022-11-01 stsp err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob",
729 55e9459f 2022-06-19 op err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
734 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
735 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
737 55e9459f 2022-06-19 op got_object_blob_close(blob);
738 db0dfdd7 2022-06-27 op if (matched_id != NULL)
739 db0dfdd7 2022-06-27 op free(matched_id);
741 55e9459f 2022-06-19 op if (*fp != NULL)
743 55e9459f 2022-06-19 op if (*path != NULL)
744 55e9459f 2022-06-19 op unlink(*path);
747 55e9459f 2022-06-19 op *path = NULL;
752 55e9459f 2022-06-19 op static const struct got_error *
753 5f56d41e 2022-07-28 op prepare_merge(int *do_merge, char **apath, FILE **afile,
754 5f56d41e 2022-07-28 op struct got_worktree *worktree, struct got_repository *repo,
755 5f56d41e 2022-07-28 op struct got_patch *p, struct got_object_id *commit_id,
756 5f56d41e 2022-07-28 op struct got_tree_object *tree, const char *path)
758 5f56d41e 2022-07-28 op const struct got_error *err = NULL;
760 5f56d41e 2022-07-28 op *do_merge = 0;
761 5f56d41e 2022-07-28 op *apath = NULL;
762 5f56d41e 2022-07-28 op *afile = NULL;
764 5f56d41e 2022-07-28 op /* don't run the diff3 merge on creations/deletions */
765 5f56d41e 2022-07-28 op if (p->old == NULL || p->new == NULL)
768 5f56d41e 2022-07-28 op if (commit_id) {
769 5f56d41e 2022-07-28 op struct got_object_id *id;
771 5f56d41e 2022-07-28 op err = got_object_tree_find_path(&id, NULL, repo, tree, path);
774 5f56d41e 2022-07-28 op got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
775 5f56d41e 2022-07-28 op got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
777 5f56d41e 2022-07-28 op err = open_blob(apath, afile, p->blob, repo);
778 5f56d41e 2022-07-28 op *do_merge = err == NULL;
779 5f56d41e 2022-07-28 op } else if (*p->blob != '\0') {
780 5f56d41e 2022-07-28 op err = open_blob(apath, afile, p->blob, repo);
782 5f56d41e 2022-07-28 op * ignore failures to open this blob, we might have
783 5f56d41e 2022-07-28 op * parsed gibberish.
785 5f56d41e 2022-07-28 op if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
786 5f56d41e 2022-07-28 op err->code != GOT_ERR_NO_OBJ)
788 5f56d41e 2022-07-28 op *do_merge = err == NULL;
795 5f56d41e 2022-07-28 op static const struct got_error *
796 55e9459f 2022-06-19 op apply_patch(int *overlapcnt, struct got_worktree *worktree,
797 55e9459f 2022-06-19 op struct got_repository *repo, struct got_fileindex *fileindex,
798 55e9459f 2022-06-19 op const char *old, const char *new, struct got_patch *p, int nop,
799 5f56d41e 2022-07-28 op int reverse, struct got_object_id *commit_id,
800 5f56d41e 2022-07-28 op struct got_tree_object *tree, struct patch_args *pa,
801 38d61ead 2022-07-23 op got_cancel_cb cancel_cb, void *cancel_arg)
803 55e9459f 2022-06-19 op const struct got_error *err = NULL;
804 5dffb1a1 2022-07-02 op struct stat sb;
805 55e9459f 2022-06-19 op int do_merge = 0, file_renamed = 0;
806 d8b5af43 2022-06-19 op char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
807 ed3bff83 2022-04-23 op char *oldpath = NULL, *newpath = NULL;
808 a6072ec9 2022-10-15 stsp char *tmppath = NULL, *template = NULL;
809 55e9459f 2022-06-19 op char *apath = NULL, *mergepath = NULL;
810 55e9459f 2022-06-19 op FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
812 2be5e1a2 2022-03-16 op mode_t mode = GOT_DEFAULT_FILE_MODE;
814 55e9459f 2022-06-19 op *overlapcnt = 0;
816 5f56d41e 2022-07-28 op err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
817 5f56d41e 2022-07-28 op commit_id, tree, old);
821 615e455c 2022-07-27 op if (reverse && !do_merge)
822 615e455c 2022-07-27 op reverse_patch(p);
824 ed3bff83 2022-04-23 op if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
825 ed3bff83 2022-04-23 op old) == -1) {
826 ed3bff83 2022-04-23 op err = got_error_from_errno("asprintf");
830 ed3bff83 2022-04-23 op if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
831 ed3bff83 2022-04-23 op new) == -1) {
832 ed3bff83 2022-04-23 op err = got_error_from_errno("asprintf");
836 78f5ac24 2022-03-19 op file_renamed = strcmp(oldpath, newpath);
838 6e96b326 2022-03-12 op if (asprintf(&template, "%s/got-patch",
839 6e96b326 2022-03-12 op got_worktree_get_root_path(worktree)) == -1) {
840 6e96b326 2022-03-12 op err = got_error_from_errno(template);
844 5dffb1a1 2022-07-02 op if (p->old != NULL) {
845 5dffb1a1 2022-07-02 op if ((oldfile = fopen(oldpath, "r")) == NULL) {
846 5dffb1a1 2022-07-02 op err = got_error_from_errno2("open", oldpath);
849 5dffb1a1 2022-07-02 op if (fstat(fileno(oldfile), &sb) == -1) {
850 5dffb1a1 2022-07-02 op err = got_error_from_errno2("fstat", oldpath);
853 5dffb1a1 2022-07-02 op mode = sb.st_mode;
854 611e5fc2 2022-09-21 mark } else if (p->xbit)
855 611e5fc2 2022-09-21 mark mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
857 b90054ed 2022-11-01 stsp err = got_opentemp_named(&tmppath, &tmpfile, template, "");
860 55e9459f 2022-06-19 op outfd = fileno(tmpfile);
861 5dffb1a1 2022-07-02 op err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
865 55e9459f 2022-06-19 op if (do_merge) {
866 497a5915 2022-06-27 op const char *type, *id;
868 55e9459f 2022-06-19 op if (fseeko(afile, 0, SEEK_SET) == -1 ||
869 55e9459f 2022-06-19 op fseeko(oldfile, 0, SEEK_SET) == -1 ||
870 55e9459f 2022-06-19 op fseeko(tmpfile, 0, SEEK_SET) == -1) {
871 55e9459f 2022-06-19 op err = got_error_from_errno("fseeko");
875 55e9459f 2022-06-19 op if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
876 55e9459f 2022-06-19 op err = got_error_from_errno("asprintf");
877 55e9459f 2022-06-19 op oldlabel = NULL;
881 55e9459f 2022-06-19 op if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
882 55e9459f 2022-06-19 op err = got_error_from_errno("asprintf");
883 55e9459f 2022-06-19 op newlabel = NULL;
887 497a5915 2022-06-27 op if (*p->cid != '\0') {
888 497a5915 2022-06-27 op type = "commit";
891 497a5915 2022-06-27 op type = "blob";
892 497a5915 2022-06-27 op id = p->blob;
895 497a5915 2022-06-27 op if (asprintf(&anclabel, "%s %s", type, id) == -1) {
896 d8b5af43 2022-06-19 op err = got_error_from_errno("asprintf");
897 d8b5af43 2022-06-19 op anclabel = NULL;
901 38d61ead 2022-07-23 op if (reverse) {
905 38d61ead 2022-07-23 op s = anclabel;
906 38d61ead 2022-07-23 op anclabel = newlabel;
907 38d61ead 2022-07-23 op newlabel = s;
910 38d61ead 2022-07-23 op afile = tmpfile;
914 b90054ed 2022-11-01 stsp err = got_opentemp_named(&mergepath, &mergefile, template, "");
917 55e9459f 2022-06-19 op outfd = fileno(mergefile);
919 55e9459f 2022-06-19 op err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
920 d8b5af43 2022-06-19 op oldfile, oldlabel, anclabel, newlabel,
921 55e9459f 2022-06-19 op GOT_DIFF_ALGORITHM_PATIENCE);
929 5b67f96e 2022-03-13 op if (p->old != NULL && p->new == NULL) {
930 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_rm(old, repo, worktree,
931 f2dd7807 2022-05-17 op fileindex, patch_delete, pa);
935 b2b3fce1 2022-10-29 op if (fchmod(outfd, apply_umask(mode)) == -1) {
936 41a37a44 2022-04-23 op err = got_error_from_errno2("chmod", tmppath);
940 a6072ec9 2022-10-15 stsp if (mergepath) {
941 a6072ec9 2022-10-15 stsp err = got_path_move_file(mergepath, newpath);
944 a6072ec9 2022-10-15 stsp free(mergepath);
945 a6072ec9 2022-10-15 stsp mergepath = NULL;
947 a6072ec9 2022-10-15 stsp err = got_path_move_file(tmppath, newpath);
950 a6072ec9 2022-10-15 stsp free(tmppath);
951 a6072ec9 2022-10-15 stsp tmppath = NULL;
954 6e96b326 2022-03-12 op if (file_renamed) {
955 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_rm(old, repo, worktree,
956 f2dd7807 2022-05-17 op fileindex, patch_delete, pa);
957 6e96b326 2022-03-12 op if (err == NULL)
958 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_add(new, repo,
959 f2dd7807 2022-05-17 op worktree, fileindex, patch_add,
962 78f5ac24 2022-03-19 op unlink(newpath);
963 78f5ac24 2022-03-19 op } else if (p->old == NULL) {
964 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_add(new, repo, worktree,
965 f2dd7807 2022-05-17 op fileindex, patch_add, pa);
967 78f5ac24 2022-03-19 op unlink(newpath);
968 55e9459f 2022-06-19 op } else if (*overlapcnt != 0)
969 55e9459f 2022-06-19 op err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
970 9802c41c 2022-06-21 op else if (do_merge)
971 9802c41c 2022-06-21 op err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
973 ed3bff83 2022-04-23 op err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
976 6f5cb1bd 2022-03-08 op free(template);
978 a6072ec9 2022-10-15 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
979 a6072ec9 2022-10-15 stsp err = got_error_from_errno("unlink");
980 55e9459f 2022-06-19 op if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
981 06c44edc 2022-06-15 stsp err = got_error_from_errno("fclose");
982 e9ce266e 2022-03-07 op free(tmppath);
984 ed3bff83 2022-04-23 op free(oldpath);
985 827bcd6c 2022-06-19 op if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
986 827bcd6c 2022-06-19 op err = got_error_from_errno("fclose");
988 a6072ec9 2022-10-15 stsp if (apath != NULL && unlink(apath) == -1 && err == NULL)
989 a6072ec9 2022-10-15 stsp err = got_error_from_errno("unlink");
990 55e9459f 2022-06-19 op if (afile != NULL && fclose(afile) == EOF && err == NULL)
991 55e9459f 2022-06-19 op err = got_error_from_errno("fclose");
994 a6072ec9 2022-10-15 stsp if (mergepath != NULL && unlink(mergepath) == -1 && err == NULL)
995 a6072ec9 2022-10-15 stsp err = got_error_from_errno("unlink");
996 55e9459f 2022-06-19 op if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
997 55e9459f 2022-06-19 op err = got_error_from_errno("fclose");
998 55e9459f 2022-06-19 op free(mergepath);
1000 ed3bff83 2022-04-23 op free(newpath);
1001 55e9459f 2022-06-19 op free(oldlabel);
1002 55e9459f 2022-06-19 op free(newlabel);
1003 d8b5af43 2022-06-19 op free(anclabel);
1007 e9ce266e 2022-03-07 op const struct got_error *
1008 e9ce266e 2022-03-07 op got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
1009 5f56d41e 2022-07-28 op int nop, int strip, int reverse, struct got_object_id *commit_id,
1010 5f56d41e 2022-07-28 op got_patch_progress_cb progress_cb, void *progress_arg,
1011 5f56d41e 2022-07-28 op got_cancel_cb cancel_cb, void *cancel_arg)
1013 4bcdc895 2022-05-17 op const struct got_error *err = NULL, *complete_err = NULL;
1014 78f5ac24 2022-03-19 op struct got_fileindex *fileindex = NULL;
1015 5f56d41e 2022-07-28 op struct got_commit_object *commit = NULL;
1016 5f56d41e 2022-07-28 op struct got_tree_object *tree = NULL;
1017 f2dd7807 2022-05-17 op char *fileindex_path = NULL;
1018 60aa1fa0 2022-03-17 op char *oldpath, *newpath;
1019 e9ce266e 2022-03-07 op struct imsgbuf *ibuf;
1020 e9ce266e 2022-03-07 op int imsg_fds[2] = {-1, -1};
1021 55e9459f 2022-06-19 op int overlapcnt, done = 0, failed = 0;
1024 e9ce266e 2022-03-07 op ibuf = calloc(1, sizeof(*ibuf));
1025 e9ce266e 2022-03-07 op if (ibuf == NULL) {
1026 e9ce266e 2022-03-07 op err = got_error_from_errno("calloc");
1030 e9ce266e 2022-03-07 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1031 e9ce266e 2022-03-07 op err = got_error_from_errno("socketpair");
1035 e9ce266e 2022-03-07 op pid = fork();
1036 e9ce266e 2022-03-07 op if (pid == -1) {
1037 e9ce266e 2022-03-07 op err = got_error_from_errno("fork");
1039 e9ce266e 2022-03-07 op } else if (pid == 0) {
1040 e9ce266e 2022-03-07 op got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1042 e9ce266e 2022-03-07 op /* not reached */
1045 e9ce266e 2022-03-07 op if (close(imsg_fds[1]) == -1) {
1046 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
1049 e9ce266e 2022-03-07 op imsg_fds[1] = -1;
1050 e9ce266e 2022-03-07 op imsg_init(ibuf, imsg_fds[0]);
1052 e9ce266e 2022-03-07 op err = send_patch(ibuf, fd);
1057 f2dd7807 2022-05-17 op err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1062 5f56d41e 2022-07-28 op if (commit_id) {
1063 5f56d41e 2022-07-28 op err = got_object_open_as_commit(&commit, repo, commit_id);
1067 5f56d41e 2022-07-28 op err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1072 e9ce266e 2022-03-07 op while (!done && err == NULL) {
1073 e9ce266e 2022-03-07 op struct got_patch p;
1074 60aa1fa0 2022-03-17 op struct patch_args pa;
1076 60aa1fa0 2022-03-17 op pa.progress_cb = progress_cb;
1077 60aa1fa0 2022-03-17 op pa.progress_arg = progress_arg;
1078 60aa1fa0 2022-03-17 op pa.head = &p.head;
1080 9d6cabd5 2022-04-07 op err = recv_patch(ibuf, &done, &p, strip);
1081 e9ce266e 2022-03-07 op if (err || done)
1084 78f5ac24 2022-03-19 op err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1085 78f5ac24 2022-03-19 op &newpath, worktree, repo, fileindex);
1086 78f5ac24 2022-03-19 op if (err == NULL)
1087 55e9459f 2022-06-19 op err = apply_patch(&overlapcnt, worktree, repo,
1088 38d61ead 2022-07-23 op fileindex, oldpath, newpath, &p, nop, reverse,
1089 5f56d41e 2022-07-28 op commit_id, tree, &pa, cancel_cb, cancel_arg);
1090 60aa1fa0 2022-03-17 op if (err != NULL) {
1092 60aa1fa0 2022-03-17 op /* recoverable errors */
1093 60aa1fa0 2022-03-17 op if (err->code == GOT_ERR_FILE_STATUS ||
1094 60aa1fa0 2022-03-17 op (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1095 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
1096 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, err);
1097 60aa1fa0 2022-03-17 op else if (err->code == GOT_ERR_HUNK_FAILED)
1098 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
1099 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, NULL);
1101 55e9459f 2022-06-19 op if (overlapcnt != 0)
1104 60aa1fa0 2022-03-17 op free(oldpath);
1105 60aa1fa0 2022-03-17 op free(newpath);
1106 e9ce266e 2022-03-07 op patch_free(&p);
1113 4bcdc895 2022-05-17 op if (fileindex != NULL)
1114 4bcdc895 2022-05-17 op complete_err = got_worktree_patch_complete(fileindex,
1115 4bcdc895 2022-05-17 op fileindex_path);
1116 f2dd7807 2022-05-17 op if (complete_err && err == NULL)
1117 f2dd7807 2022-05-17 op err = complete_err;
1118 4bcdc895 2022-05-17 op free(fileindex_path);
1120 5f56d41e 2022-07-28 op got_object_tree_close(tree);
1122 5f56d41e 2022-07-28 op got_object_commit_close(commit);
1123 e9ce266e 2022-03-07 op if (fd != -1 && close(fd) == -1 && err == NULL)
1124 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
1125 e9ce266e 2022-03-07 op if (ibuf != NULL)
1126 e9ce266e 2022-03-07 op imsg_clear(ibuf);
1127 e9ce266e 2022-03-07 op if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1128 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
1129 e9ce266e 2022-03-07 op if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1130 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
1131 60aa1fa0 2022-03-17 op if (err == NULL && failed)
1132 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_PATCH_FAILED);