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 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>
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"
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"
57 e9ce266e 2022-03-07 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
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;
67 35095610 2022-06-14 op int old_lines;
69 35095610 2022-06-14 op int new_lines;
75 60aa1fa0 2022-03-17 op STAILQ_HEAD(got_patch_hunk_head, got_patch_hunk);
76 e9ce266e 2022-03-07 op struct got_patch {
80 55e9459f 2022-06-19 op char blob[41];
81 60aa1fa0 2022-03-17 op struct got_patch_hunk_head head;
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;
90 e9ce266e 2022-03-07 op static const struct got_error *
91 e9ce266e 2022-03-07 op send_patch(struct imsgbuf *ibuf, int fd)
93 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
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");
103 08f44789 2022-07-07 op return got_privsep_flush_imsg(ibuf);
107 e9ce266e 2022-03-07 op patch_free(struct got_patch *p)
109 e9ce266e 2022-03-07 op struct got_patch_hunk *h;
112 e9ce266e 2022-03-07 op while (!STAILQ_EMPTY(&p->head)) {
113 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
114 e9ce266e 2022-03-07 op STAILQ_REMOVE_HEAD(&p->head, entries);
116 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i)
117 e9ce266e 2022-03-07 op free(h->lines[i]);
118 e9ce266e 2022-03-07 op free(h->lines);
122 e9ce266e 2022-03-07 op free(p->new);
123 e9ce266e 2022-03-07 op free(p->old);
125 0e07a2a1 2022-06-13 op memset(p, 0, sizeof(*p));
126 0e07a2a1 2022-06-13 op STAILQ_INIT(&p->head);
129 e9ce266e 2022-03-07 op static const struct got_error *
130 e9ce266e 2022-03-07 op pushline(struct got_patch_hunk *h, const char *line)
133 e9ce266e 2022-03-07 op size_t newcap;
135 e9ce266e 2022-03-07 op if (h->len == h->cap) {
136 e9ce266e 2022-03-07 op if ((newcap = h->cap * 1.5) == 0)
138 e9ce266e 2022-03-07 op t = recallocarray(h->lines, h->cap, newcap,
139 e9ce266e 2022-03-07 op sizeof(h->lines[0]));
140 e9ce266e 2022-03-07 op if (t == NULL)
141 e9ce266e 2022-03-07 op return got_error_from_errno("recallocarray");
142 e9ce266e 2022-03-07 op h->lines = t;
143 e9ce266e 2022-03-07 op h->cap = newcap;
146 e9ce266e 2022-03-07 op if ((t = strdup(line)) == NULL)
147 e9ce266e 2022-03-07 op return got_error_from_errno("strdup");
149 e9ce266e 2022-03-07 op h->lines[h->len++] = t;
153 e9ce266e 2022-03-07 op static const struct got_error *
154 9d6cabd5 2022-04-07 op recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p, int strip)
156 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
157 e9ce266e 2022-03-07 op struct imsg imsg;
158 e9ce266e 2022-03-07 op struct got_imsg_patch_hunk hdr;
159 e9ce266e 2022-03-07 op struct got_imsg_patch patch;
160 e9ce266e 2022-03-07 op struct got_patch_hunk *h = NULL;
161 e9ce266e 2022-03-07 op size_t datalen;
162 b2832778 2022-04-23 op int lastmode = -1;
164 e9ce266e 2022-03-07 op memset(p, 0, sizeof(*p));
165 e9ce266e 2022-03-07 op STAILQ_INIT(&p->head);
167 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
170 e9ce266e 2022-03-07 op if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
174 e9ce266e 2022-03-07 op if (imsg.hdr.type != GOT_IMSG_PATCH) {
175 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
178 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
179 e9ce266e 2022-03-07 op if (datalen != sizeof(patch)) {
180 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
183 e9ce266e 2022-03-07 op memcpy(&patch, imsg.data, sizeof(patch));
185 99b94ad7 2022-06-13 op if (patch.old[sizeof(patch.old)-1] != '\0' ||
186 55e9459f 2022-06-19 op patch.new[sizeof(patch.new)-1] != '\0' ||
187 d8b5af43 2022-06-19 op patch.cid[sizeof(patch.cid)-1] != '\0' ||
188 55e9459f 2022-06-19 op patch.blob[sizeof(patch.blob)-1] != '\0') {
189 99b94ad7 2022-06-13 op err = got_error(GOT_ERR_PRIVSEP_LEN);
193 497a5915 2022-06-27 op if (*patch.cid != '\0')
194 d8b5af43 2022-06-19 op strlcpy(p->cid, patch.cid, sizeof(p->cid));
196 497a5915 2022-06-27 op if (*patch.blob != '\0')
197 d8b5af43 2022-06-19 op strlcpy(p->blob, patch.blob, sizeof(p->blob));
199 9d6cabd5 2022-04-07 op /* automatically set strip=1 for git-style diffs */
200 9d6cabd5 2022-04-07 op if (strip == -1 && patch.git &&
201 9d6cabd5 2022-04-07 op (*patch.old == '\0' || !strncmp(patch.old, "a/", 2)) &&
202 9d6cabd5 2022-04-07 op (*patch.new == '\0' || !strncmp(patch.new, "b/", 2)))
205 9d6cabd5 2022-04-07 op /* prefer the new name if not /dev/null for not git-style diffs */
206 9d6cabd5 2022-04-07 op if (!patch.git && *patch.new != '\0' && *patch.old != '\0') {
207 9d6cabd5 2022-04-07 op err = got_path_strip(&p->old, patch.new, strip);
210 9d6cabd5 2022-04-07 op } else if (*patch.old != '\0') {
211 9d6cabd5 2022-04-07 op err = got_path_strip(&p->old, patch.old, strip);
216 9d6cabd5 2022-04-07 op if (*patch.new != '\0') {
217 9d6cabd5 2022-04-07 op err = got_path_strip(&p->new, patch.new, strip);
222 b95c53df 2022-03-12 op if (p->old == NULL && p->new == NULL) {
223 b95c53df 2022-03-12 op err = got_error(GOT_ERR_PATCH_MALFORMED);
227 e9ce266e 2022-03-07 op imsg_free(&imsg);
232 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
234 0e07a2a1 2022-06-13 op patch_free(p);
238 2399b53d 2022-06-14 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
239 e9ce266e 2022-03-07 op switch (imsg.hdr.type) {
240 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_DONE:
241 be33dff7 2022-05-13 op if (h != NULL && h->len == 0)
242 be33dff7 2022-05-13 op err = got_error(GOT_ERR_PATCH_MALFORMED);
244 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_HUNK:
245 be33dff7 2022-05-13 op if (h != NULL &&
246 be33dff7 2022-05-13 op (h->len == 0 || h->old_nonl || h->new_nonl)) {
247 b3c57ab2 2022-03-22 op err = got_error(GOT_ERR_PATCH_MALFORMED);
250 b2832778 2022-04-23 op lastmode = -1;
251 e9ce266e 2022-03-07 op if (datalen != sizeof(hdr)) {
252 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
255 e9ce266e 2022-03-07 op memcpy(&hdr, imsg.data, sizeof(hdr));
256 5b7126c5 2022-06-14 op if (hdr.oldfrom < 0 || hdr.newfrom < 0) {
257 5b7126c5 2022-06-14 op err = got_error(GOT_ERR_PRIVSEP_LEN);
260 5b7126c5 2022-06-14 op if ((h = calloc(1, sizeof(*h))) == NULL) {
261 5b7126c5 2022-06-14 op err = got_error_from_errno("calloc");
264 e9ce266e 2022-03-07 op h->old_from = hdr.oldfrom;
265 e9ce266e 2022-03-07 op h->old_lines = hdr.oldlines;
266 e9ce266e 2022-03-07 op h->new_from = hdr.newfrom;
267 e9ce266e 2022-03-07 op h->new_lines = hdr.newlines;
268 e9ce266e 2022-03-07 op STAILQ_INSERT_TAIL(&p->head, h, entries);
270 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_LINE:
271 e9ce266e 2022-03-07 op if (h == NULL) {
272 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
275 e9ce266e 2022-03-07 op t = imsg.data;
276 b3c57ab2 2022-03-22 op /* at least one char */
277 e9ce266e 2022-03-07 op if (datalen < 2 || t[datalen-1] != '\0') {
278 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
281 b3c57ab2 2022-03-22 op if (*t != ' ' && *t != '-' && *t != '+' &&
282 b3c57ab2 2022-03-22 op *t != '\\') {
283 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
287 b2832778 2022-04-23 op if (*t != '\\')
288 b3c57ab2 2022-03-22 op err = pushline(h, t);
289 b2832778 2022-04-23 op else if (lastmode == '-')
290 b2832778 2022-04-23 op h->old_nonl = 1;
291 b2832778 2022-04-23 op else if (lastmode == '+')
292 b2832778 2022-04-23 op h->new_nonl = 1;
294 b2832778 2022-04-23 op err = got_error(GOT_ERR_PATCH_MALFORMED);
299 b2832778 2022-04-23 op lastmode = *t;
302 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
306 e9ce266e 2022-03-07 op imsg_free(&imsg);
311 0e07a2a1 2022-06-13 op patch_free(p);
313 e9ce266e 2022-03-07 op imsg_free(&imsg);
318 38d61ead 2022-07-23 op reverse_patch(struct got_patch *p)
320 38d61ead 2022-07-23 op struct got_patch_hunk *h;
324 38d61ead 2022-07-23 op STAILQ_FOREACH(h, &p->head, entries) {
325 38d61ead 2022-07-23 op tmp = h->old_from;
326 38d61ead 2022-07-23 op h->old_from = h->new_from;
327 38d61ead 2022-07-23 op h->new_from = tmp;
329 38d61ead 2022-07-23 op tmp = h->old_lines;
330 38d61ead 2022-07-23 op h->old_lines = h->new_lines;
331 38d61ead 2022-07-23 op h->new_lines = tmp;
333 38d61ead 2022-07-23 op tmp = h->old_nonl;
334 38d61ead 2022-07-23 op h->old_nonl = h->new_nonl;
335 38d61ead 2022-07-23 op h->new_nonl = tmp;
337 38d61ead 2022-07-23 op for (i = 0; i < h->len; ++i) {
338 38d61ead 2022-07-23 op if (*h->lines[i] == '+')
339 38d61ead 2022-07-23 op *h->lines[i] = '-';
340 38d61ead 2022-07-23 op else if (*h->lines[i] == '-')
341 38d61ead 2022-07-23 op *h->lines[i] = '+';
347 e9ce266e 2022-03-07 op * Copy data from orig starting at copypos until pos into tmp.
348 e9ce266e 2022-03-07 op * If pos is -1, copy until EOF.
350 e9ce266e 2022-03-07 op static const struct got_error *
351 e9ce266e 2022-03-07 op copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
353 e9ce266e 2022-03-07 op char buf[BUFSIZ];
354 e9ce266e 2022-03-07 op size_t len, r, w;
356 e45f7eba 2022-05-14 naddy if (fseeko(orig, copypos, SEEK_SET) == -1)
357 e45f7eba 2022-05-14 naddy return got_error_from_errno("fseeko");
359 e9ce266e 2022-03-07 op while (pos == -1 || copypos < pos) {
360 e9ce266e 2022-03-07 op len = sizeof(buf);
362 e9ce266e 2022-03-07 op len = MIN(len, (size_t)pos - copypos);
363 e9ce266e 2022-03-07 op r = fread(buf, 1, len, orig);
364 e9ce266e 2022-03-07 op if (r != len && ferror(orig))
365 e9ce266e 2022-03-07 op return got_error_from_errno("fread");
366 e9ce266e 2022-03-07 op w = fwrite(buf, 1, r, tmp);
368 e9ce266e 2022-03-07 op return got_error_from_errno("fwrite");
369 e9ce266e 2022-03-07 op copypos += len;
370 e9ce266e 2022-03-07 op if (r != len && feof(orig)) {
371 e9ce266e 2022-03-07 op if (pos == -1)
373 60aa1fa0 2022-03-17 op return got_error(GOT_ERR_HUNK_FAILED);
379 e9ce266e 2022-03-07 op static const struct got_error *
380 35095610 2022-06-14 op locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, int *lineno)
382 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
383 e9ce266e 2022-03-07 op char *line = NULL;
384 e9ce266e 2022-03-07 op char mode = *h->lines[0];
385 e9ce266e 2022-03-07 op size_t linesize = 0;
386 e9ce266e 2022-03-07 op ssize_t linelen;
387 e9ce266e 2022-03-07 op off_t match = -1;
388 35095610 2022-06-14 op int match_lineno = -1;
391 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
392 e9ce266e 2022-03-07 op if (linelen == -1) {
393 e9ce266e 2022-03-07 op if (ferror(orig))
394 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
395 e9ce266e 2022-03-07 op else if (match == -1)
396 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_HUNK_FAILED);
399 b3c57ab2 2022-03-22 op if (line[linelen - 1] == '\n')
400 b3c57ab2 2022-03-22 op line[linelen - 1] = '\0';
403 46ebad13 2022-03-17 op if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
404 46ebad13 2022-03-17 op (mode == '-' && !strcmp(h->lines[0] + 1, line)) ||
405 e9ce266e 2022-03-07 op (mode == '+' && *lineno == h->old_from)) {
406 e9ce266e 2022-03-07 op match = ftello(orig);
407 e9ce266e 2022-03-07 op if (match == -1) {
408 e9ce266e 2022-03-07 op err = got_error_from_errno("ftello");
411 e9ce266e 2022-03-07 op match -= linelen;
412 e9ce266e 2022-03-07 op match_lineno = (*lineno)-1;
415 e9ce266e 2022-03-07 op if (*lineno >= h->old_from && match != -1)
419 e9ce266e 2022-03-07 op if (err == NULL) {
420 33df9995 2022-03-11 op *pos = match;
421 e9ce266e 2022-03-07 op *lineno = match_lineno;
422 e45f7eba 2022-05-14 naddy if (fseeko(orig, match, SEEK_SET) == -1)
423 e45f7eba 2022-05-14 naddy err = got_error_from_errno("fseeko");
431 a92a2042 2022-07-02 op linecmp(const char *a, const char *b, int *mangled)
435 a92a2042 2022-07-02 op *mangled = 0;
436 a92a2042 2022-07-02 op c = strcmp(a, b);
440 a92a2042 2022-07-02 op *mangled = 1;
442 2c986b8f 2022-07-04 op while (*a == '\t' || *a == ' ' || *a == '\f')
444 2c986b8f 2022-07-04 op while (*b == '\t' || *b == ' ' || *b == '\f')
446 2c986b8f 2022-07-04 op if (*a == '\0' || *a != *b)
451 a92a2042 2022-07-02 op return *a - *b;
454 e9ce266e 2022-03-07 op static const struct got_error *
455 e9ce266e 2022-03-07 op test_hunk(FILE *orig, struct got_patch_hunk *h)
457 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
458 e9ce266e 2022-03-07 op char *line = NULL;
459 e9ce266e 2022-03-07 op size_t linesize = 0, i = 0;
460 e9ce266e 2022-03-07 op ssize_t linelen;
463 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
464 e9ce266e 2022-03-07 op switch (*h->lines[i]) {
469 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
470 e9ce266e 2022-03-07 op if (linelen == -1) {
471 e9ce266e 2022-03-07 op if (ferror(orig))
472 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
474 e9ce266e 2022-03-07 op err = got_error(
475 60aa1fa0 2022-03-17 op GOT_ERR_HUNK_FAILED);
478 b3c57ab2 2022-03-22 op if (line[linelen - 1] == '\n')
479 b3c57ab2 2022-03-22 op line[linelen - 1] = '\0';
480 a92a2042 2022-07-02 op if (linecmp(h->lines[i] + 1, line, &mangled)) {
481 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_HUNK_FAILED);
485 a92a2042 2022-07-02 op h->ws_mangled = 1;
495 e9ce266e 2022-03-07 op static const struct got_error *
496 a92a2042 2022-07-02 op apply_hunk(FILE *orig, FILE *tmp, struct got_patch_hunk *h, int *lineno,
499 a92a2042 2022-07-02 op const struct got_error *err = NULL;
500 a92a2042 2022-07-02 op const char *t;
501 a92a2042 2022-07-02 op size_t linesize = 0, i, new = 0;
502 a92a2042 2022-07-02 op char *line = NULL;
504 a92a2042 2022-07-02 op ssize_t linelen;
506 a92a2042 2022-07-02 op if (orig != NULL && fseeko(orig, from, SEEK_SET) == -1)
507 a92a2042 2022-07-02 op return got_error_from_errno("fseeko");
509 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
510 a92a2042 2022-07-02 op switch (mode = *h->lines[i]) {
514 a92a2042 2022-07-02 op if (orig != NULL) {
515 a92a2042 2022-07-02 op linelen = getline(&line, &linesize, orig);
516 a92a2042 2022-07-02 op if (linelen == -1) {
517 a92a2042 2022-07-02 op err = got_error_from_errno("getline");
520 a92a2042 2022-07-02 op if (line[linelen - 1] == '\n')
521 a92a2042 2022-07-02 op line[linelen - 1] = '\0';
524 a92a2042 2022-07-02 op t = h->lines[i] + 1;
525 a92a2042 2022-07-02 op if (mode == '-')
527 a92a2042 2022-07-02 op if (fprintf(tmp, "%s\n", t) < 0) {
528 a92a2042 2022-07-02 op err = got_error_from_errno("fprintf");
534 a92a2042 2022-07-02 op if (fprintf(tmp, "%s", h->lines[i] + 1) < 0) {
535 a92a2042 2022-07-02 op err = got_error_from_errno("fprintf");
538 b2832778 2022-04-23 op if (new != h->new_lines || !h->new_nonl) {
539 a92a2042 2022-07-02 op if (fprintf(tmp, "\n") < 0) {
540 a92a2042 2022-07-02 op err = got_error_from_errno("fprintf");
553 6e96b326 2022-03-12 op static const struct got_error *
554 5dffb1a1 2022-07-02 op patch_file(struct got_patch *p, FILE *orig, FILE *tmp)
556 6e96b326 2022-03-12 op const struct got_error *err = NULL;
557 6e96b326 2022-03-12 op struct got_patch_hunk *h;
558 2be5e1a2 2022-03-16 op struct stat sb;
559 35095610 2022-06-14 op int lineno = 0;
560 6e96b326 2022-03-12 op off_t copypos, pos;
561 6e96b326 2022-03-12 op char *line = NULL;
562 6e96b326 2022-03-12 op size_t linesize = 0;
563 6e96b326 2022-03-12 op ssize_t linelen;
565 e9ce266e 2022-03-07 op if (p->old == NULL) { /* create */
566 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
567 6e96b326 2022-03-12 op if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
568 6e96b326 2022-03-12 op return got_error(GOT_ERR_PATCH_MALFORMED);
569 a92a2042 2022-07-02 op return apply_hunk(orig, tmp, h, &lineno, 0);
572 827bcd6c 2022-06-19 op if (fstat(fileno(orig), &sb) == -1)
573 827bcd6c 2022-06-19 op return got_error_from_errno("fstat");
576 e9ce266e 2022-03-07 op STAILQ_FOREACH(h, &p->head, entries) {
578 33df9995 2022-03-11 op err = locate_hunk(orig, h, &pos, &lineno);
579 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED)
580 60aa1fa0 2022-03-17 op h->err = err;
581 e9ce266e 2022-03-07 op if (err != NULL)
583 05737d49 2022-06-19 op err = copy(tmp, orig, copypos, pos);
584 e9ce266e 2022-03-07 op if (err != NULL)
586 e9ce266e 2022-03-07 op copypos = pos;
588 e9ce266e 2022-03-07 op err = test_hunk(orig, h);
589 60aa1fa0 2022-03-17 op if (err != NULL && err->code == GOT_ERR_HUNK_FAILED) {
591 e9ce266e 2022-03-07 op * try to apply the hunk again starting the search
592 e9ce266e 2022-03-07 op * after the previous partial match.
594 827bcd6c 2022-06-19 op if (fseeko(orig, pos, SEEK_SET) == -1)
595 827bcd6c 2022-06-19 op return got_error_from_errno("fseeko");
596 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
597 827bcd6c 2022-06-19 op if (linelen == -1)
598 827bcd6c 2022-06-19 op return got_error_from_errno("getline");
600 e9ce266e 2022-03-07 op goto tryagain;
602 e9ce266e 2022-03-07 op if (err != NULL)
605 60aa1fa0 2022-03-17 op if (lineno + 1 != h->old_from)
606 60aa1fa0 2022-03-17 op h->offset = lineno + 1 - h->old_from;
608 a92a2042 2022-07-02 op err = apply_hunk(orig, tmp, h, &lineno, pos);
609 e9ce266e 2022-03-07 op if (err != NULL)
612 e9ce266e 2022-03-07 op copypos = ftello(orig);
613 827bcd6c 2022-06-19 op if (copypos == -1)
614 827bcd6c 2022-06-19 op return got_error_from_errno("ftello");
617 60aa1fa0 2022-03-17 op if (p->new == NULL && sb.st_size != copypos) {
618 60aa1fa0 2022-03-17 op h = STAILQ_FIRST(&p->head);
619 60aa1fa0 2022-03-17 op h->err = got_error(GOT_ERR_HUNK_FAILED);
620 60aa1fa0 2022-03-17 op err = h->err;
621 05737d49 2022-06-19 op } else if (!feof(orig))
622 e9ce266e 2022-03-07 op err = copy(tmp, orig, copypos, -1);
627 dbda770b 2022-03-13 op static const struct got_error *
628 60aa1fa0 2022-03-17 op report_progress(struct patch_args *pa, const char *old, const char *new,
629 60aa1fa0 2022-03-17 op unsigned char status, const struct got_error *orig_error)
631 60aa1fa0 2022-03-17 op const struct got_error *err;
632 60aa1fa0 2022-03-17 op struct got_patch_hunk *h;
634 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, status,
635 a92a2042 2022-07-02 op orig_error, 0, 0, 0, 0, 0, 0, NULL);
639 60aa1fa0 2022-03-17 op STAILQ_FOREACH(h, pa->head, entries) {
640 a92a2042 2022-07-02 op if (h->offset == 0 && !h->ws_mangled && h->err == NULL)
643 60aa1fa0 2022-03-17 op err = pa->progress_cb(pa->progress_arg, old, new, 0, NULL,
644 60aa1fa0 2022-03-17 op h->old_from, h->old_lines, h->new_from, h->new_lines,
645 a92a2042 2022-07-02 op h->offset, h->ws_mangled, h->err);
653 60aa1fa0 2022-03-17 op static const struct got_error *
654 b22138f5 2022-03-16 op patch_delete(void *arg, unsigned char status, unsigned char staged_status,
655 b22138f5 2022-03-16 op const char *path)
657 60aa1fa0 2022-03-17 op return report_progress(arg, path, NULL, status, NULL);
660 b22138f5 2022-03-16 op static const struct got_error *
661 b22138f5 2022-03-16 op patch_add(void *arg, unsigned char status, const char *path)
663 60aa1fa0 2022-03-17 op return report_progress(arg, NULL, path, status, NULL);
666 b22138f5 2022-03-16 op static const struct got_error *
667 55e9459f 2022-06-19 op open_blob(char **path, FILE **fp, const char *blobid,
668 55e9459f 2022-06-19 op struct got_repository *repo)
670 6e96b326 2022-03-12 op const struct got_error *err = NULL;
671 55e9459f 2022-06-19 op struct got_blob_object *blob = NULL;
672 db0dfdd7 2022-06-27 op struct got_object_id id, *idptr, *matched_id = NULL;
673 eb81bc23 2022-06-28 tracey int fd = -1;
676 55e9459f 2022-06-19 op *path = NULL;
678 db0dfdd7 2022-06-27 op if (strlen(blobid) != SHA1_DIGEST_STRING_LENGTH - 1) {
679 db0dfdd7 2022-06-27 op err = got_repo_match_object_id(&matched_id, NULL, blobid,
680 db0dfdd7 2022-06-27 op GOT_OBJ_TYPE_BLOB, NULL /* do not resolve tags */,
684 db0dfdd7 2022-06-27 op idptr = matched_id;
686 db0dfdd7 2022-06-27 op if (!got_parse_sha1_digest(id.sha1, blobid))
687 db0dfdd7 2022-06-27 op return got_error(GOT_ERR_BAD_OBJ_ID_STR);
691 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
692 eb81bc23 2022-06-28 tracey if (fd == -1) {
693 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
694 eb81bc23 2022-06-28 tracey goto done;
697 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, idptr, 8192, fd);
701 55e9459f 2022-06-19 op err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
705 55e9459f 2022-06-19 op err = got_object_blob_dump_to_file(NULL, NULL, NULL, *fp, blob);
710 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
711 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
713 55e9459f 2022-06-19 op got_object_blob_close(blob);
714 db0dfdd7 2022-06-27 op if (matched_id != NULL)
715 db0dfdd7 2022-06-27 op free(matched_id);
717 55e9459f 2022-06-19 op if (*fp != NULL)
719 55e9459f 2022-06-19 op if (*path != NULL)
720 55e9459f 2022-06-19 op unlink(*path);
723 55e9459f 2022-06-19 op *path = NULL;
728 55e9459f 2022-06-19 op static const struct got_error *
729 5f56d41e 2022-07-28 op prepare_merge(int *do_merge, char **apath, FILE **afile,
730 5f56d41e 2022-07-28 op struct got_worktree *worktree, struct got_repository *repo,
731 5f56d41e 2022-07-28 op struct got_patch *p, struct got_object_id *commit_id,
732 5f56d41e 2022-07-28 op struct got_tree_object *tree, const char *path)
734 5f56d41e 2022-07-28 op const struct got_error *err = NULL;
736 5f56d41e 2022-07-28 op *do_merge = 0;
737 5f56d41e 2022-07-28 op *apath = NULL;
738 5f56d41e 2022-07-28 op *afile = NULL;
740 5f56d41e 2022-07-28 op /* don't run the diff3 merge on creations/deletions */
741 5f56d41e 2022-07-28 op if (p->old == NULL || p->new == NULL)
744 5f56d41e 2022-07-28 op if (commit_id) {
745 5f56d41e 2022-07-28 op struct got_object_id *id;
747 5f56d41e 2022-07-28 op err = got_object_tree_find_path(&id, NULL, repo, tree, path);
750 5f56d41e 2022-07-28 op got_sha1_digest_to_str(id->sha1, p->blob, sizeof(p->blob));
751 5f56d41e 2022-07-28 op got_sha1_digest_to_str(commit_id->sha1, p->cid, sizeof(p->cid));
753 5f56d41e 2022-07-28 op err = open_blob(apath, afile, p->blob, repo);
754 5f56d41e 2022-07-28 op *do_merge = err == NULL;
755 5f56d41e 2022-07-28 op } else if (*p->blob != '\0') {
756 5f56d41e 2022-07-28 op err = open_blob(apath, afile, p->blob, repo);
758 5f56d41e 2022-07-28 op * ignore failures to open this blob, we might have
759 5f56d41e 2022-07-28 op * parsed gibberish.
761 5f56d41e 2022-07-28 op if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT) &&
762 5f56d41e 2022-07-28 op err->code != GOT_ERR_NO_OBJ)
764 5f56d41e 2022-07-28 op *do_merge = err == NULL;
771 5f56d41e 2022-07-28 op static const struct got_error *
772 55e9459f 2022-06-19 op apply_patch(int *overlapcnt, struct got_worktree *worktree,
773 55e9459f 2022-06-19 op struct got_repository *repo, struct got_fileindex *fileindex,
774 55e9459f 2022-06-19 op const char *old, const char *new, struct got_patch *p, int nop,
775 5f56d41e 2022-07-28 op int reverse, struct got_object_id *commit_id,
776 5f56d41e 2022-07-28 op struct got_tree_object *tree, struct patch_args *pa,
777 38d61ead 2022-07-23 op got_cancel_cb cancel_cb, void *cancel_arg)
779 55e9459f 2022-06-19 op const struct got_error *err = NULL;
780 5dffb1a1 2022-07-02 op struct stat sb;
781 55e9459f 2022-06-19 op int do_merge = 0, file_renamed = 0;
782 d8b5af43 2022-06-19 op char *oldlabel = NULL, *newlabel = NULL, *anclabel = NULL;
783 ed3bff83 2022-04-23 op char *oldpath = NULL, *newpath = NULL;
784 1b1b91ab 2022-06-15 op char *tmppath = NULL, *template = NULL, *parent = NULL;
785 55e9459f 2022-06-19 op char *apath = NULL, *mergepath = NULL;
786 55e9459f 2022-06-19 op FILE *oldfile = NULL, *tmpfile = NULL, *afile = NULL, *mergefile = NULL;
788 55e9459f 2022-06-19 op const char *outpath;
789 2be5e1a2 2022-03-16 op mode_t mode = GOT_DEFAULT_FILE_MODE;
791 55e9459f 2022-06-19 op *overlapcnt = 0;
793 5f56d41e 2022-07-28 op err = prepare_merge(&do_merge, &apath, &afile, worktree, repo, p,
794 5f56d41e 2022-07-28 op commit_id, tree, old);
798 615e455c 2022-07-27 op if (reverse && !do_merge)
799 615e455c 2022-07-27 op reverse_patch(p);
801 ed3bff83 2022-04-23 op if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
802 ed3bff83 2022-04-23 op old) == -1) {
803 ed3bff83 2022-04-23 op err = got_error_from_errno("asprintf");
807 ed3bff83 2022-04-23 op if (asprintf(&newpath, "%s/%s", got_worktree_get_root_path(worktree),
808 ed3bff83 2022-04-23 op new) == -1) {
809 ed3bff83 2022-04-23 op err = got_error_from_errno("asprintf");
813 78f5ac24 2022-03-19 op file_renamed = strcmp(oldpath, newpath);
815 6e96b326 2022-03-12 op if (asprintf(&template, "%s/got-patch",
816 6e96b326 2022-03-12 op got_worktree_get_root_path(worktree)) == -1) {
817 6e96b326 2022-03-12 op err = got_error_from_errno(template);
821 5dffb1a1 2022-07-02 op if (p->old != NULL) {
822 5dffb1a1 2022-07-02 op if ((oldfile = fopen(oldpath, "r")) == NULL) {
823 5dffb1a1 2022-07-02 op err = got_error_from_errno2("open", oldpath);
826 5dffb1a1 2022-07-02 op if (fstat(fileno(oldfile), &sb) == -1) {
827 5dffb1a1 2022-07-02 op err = got_error_from_errno2("fstat", oldpath);
830 5dffb1a1 2022-07-02 op mode = sb.st_mode;
833 55e9459f 2022-06-19 op err = got_opentemp_named(&tmppath, &tmpfile, template);
836 55e9459f 2022-06-19 op outpath = tmppath;
837 55e9459f 2022-06-19 op outfd = fileno(tmpfile);
838 5dffb1a1 2022-07-02 op err = patch_file(p, afile != NULL ? afile : oldfile, tmpfile);
842 55e9459f 2022-06-19 op if (do_merge) {
843 497a5915 2022-06-27 op const char *type, *id;
845 55e9459f 2022-06-19 op if (fseeko(afile, 0, SEEK_SET) == -1 ||
846 55e9459f 2022-06-19 op fseeko(oldfile, 0, SEEK_SET) == -1 ||
847 55e9459f 2022-06-19 op fseeko(tmpfile, 0, SEEK_SET) == -1) {
848 55e9459f 2022-06-19 op err = got_error_from_errno("fseeko");
852 55e9459f 2022-06-19 op if (asprintf(&oldlabel, "--- %s", p->old) == -1) {
853 55e9459f 2022-06-19 op err = got_error_from_errno("asprintf");
854 55e9459f 2022-06-19 op oldlabel = NULL;
858 55e9459f 2022-06-19 op if (asprintf(&newlabel, "+++ %s", p->new) == -1) {
859 55e9459f 2022-06-19 op err = got_error_from_errno("asprintf");
860 55e9459f 2022-06-19 op newlabel = NULL;
864 497a5915 2022-06-27 op if (*p->cid != '\0') {
865 497a5915 2022-06-27 op type = "commit";
868 497a5915 2022-06-27 op type = "blob";
869 497a5915 2022-06-27 op id = p->blob;
872 497a5915 2022-06-27 op if (asprintf(&anclabel, "%s %s", type, id) == -1) {
873 d8b5af43 2022-06-19 op err = got_error_from_errno("asprintf");
874 d8b5af43 2022-06-19 op anclabel = NULL;
878 38d61ead 2022-07-23 op if (reverse) {
882 38d61ead 2022-07-23 op s = anclabel;
883 38d61ead 2022-07-23 op anclabel = newlabel;
884 38d61ead 2022-07-23 op newlabel = s;
887 38d61ead 2022-07-23 op afile = tmpfile;
891 55e9459f 2022-06-19 op err = got_opentemp_named(&mergepath, &mergefile, template);
894 55e9459f 2022-06-19 op outpath = mergepath;
895 55e9459f 2022-06-19 op outfd = fileno(mergefile);
897 55e9459f 2022-06-19 op err = got_merge_diff3(overlapcnt, outfd, tmpfile, afile,
898 d8b5af43 2022-06-19 op oldfile, oldlabel, anclabel, newlabel,
899 55e9459f 2022-06-19 op GOT_DIFF_ALGORITHM_PATIENCE);
907 5b67f96e 2022-03-13 op if (p->old != NULL && p->new == NULL) {
908 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_rm(old, repo, worktree,
909 f2dd7807 2022-05-17 op fileindex, patch_delete, pa);
913 55e9459f 2022-06-19 op if (fchmod(outfd, mode) == -1) {
914 41a37a44 2022-04-23 op err = got_error_from_errno2("chmod", tmppath);
918 55e9459f 2022-06-19 op if (rename(outpath, newpath) == -1) {
919 95d68340 2022-03-16 op if (errno != ENOENT) {
920 55e9459f 2022-06-19 op err = got_error_from_errno3("rename", outpath,
925 95d68340 2022-03-16 op err = got_path_dirname(&parent, newpath);
926 95d68340 2022-03-16 op if (err != NULL)
928 95d68340 2022-03-16 op err = got_path_mkdir(parent);
929 95d68340 2022-03-16 op if (err != NULL)
931 55e9459f 2022-06-19 op if (rename(outpath, newpath) == -1) {
932 55e9459f 2022-06-19 op err = got_error_from_errno3("rename", outpath,
938 6e96b326 2022-03-12 op if (file_renamed) {
939 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_rm(old, repo, worktree,
940 f2dd7807 2022-05-17 op fileindex, patch_delete, pa);
941 6e96b326 2022-03-12 op if (err == NULL)
942 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_add(new, repo,
943 f2dd7807 2022-05-17 op worktree, fileindex, patch_add,
946 78f5ac24 2022-03-19 op unlink(newpath);
947 78f5ac24 2022-03-19 op } else if (p->old == NULL) {
948 f2dd7807 2022-05-17 op err = got_worktree_patch_schedule_add(new, repo, worktree,
949 f2dd7807 2022-05-17 op fileindex, patch_add, pa);
951 78f5ac24 2022-03-19 op unlink(newpath);
952 55e9459f 2022-06-19 op } else if (*overlapcnt != 0)
953 55e9459f 2022-06-19 op err = report_progress(pa, old, new, GOT_STATUS_CONFLICT, NULL);
954 9802c41c 2022-06-21 op else if (do_merge)
955 9802c41c 2022-06-21 op err = report_progress(pa, old, new, GOT_STATUS_MERGE, NULL);
957 ed3bff83 2022-04-23 op err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
960 95d68340 2022-03-16 op free(parent);
961 6f5cb1bd 2022-03-08 op free(template);
963 e9ce266e 2022-03-07 op if (tmppath != NULL)
964 e9ce266e 2022-03-07 op unlink(tmppath);
965 55e9459f 2022-06-19 op if (tmpfile != NULL && fclose(tmpfile) == EOF && err == NULL)
966 06c44edc 2022-06-15 stsp err = got_error_from_errno("fclose");
967 e9ce266e 2022-03-07 op free(tmppath);
969 ed3bff83 2022-04-23 op free(oldpath);
970 827bcd6c 2022-06-19 op if (oldfile != NULL && fclose(oldfile) == EOF && err == NULL)
971 827bcd6c 2022-06-19 op err = got_error_from_errno("fclose");
973 55e9459f 2022-06-19 op if (apath != NULL)
974 55e9459f 2022-06-19 op unlink(apath);
975 55e9459f 2022-06-19 op if (afile != NULL && fclose(afile) == EOF && err == NULL)
976 55e9459f 2022-06-19 op err = got_error_from_errno("fclose");
979 55e9459f 2022-06-19 op if (mergepath != NULL)
980 55e9459f 2022-06-19 op unlink(mergepath);
981 55e9459f 2022-06-19 op if (mergefile != NULL && fclose(mergefile) == EOF && err == NULL)
982 55e9459f 2022-06-19 op err = got_error_from_errno("fclose");
983 55e9459f 2022-06-19 op free(mergepath);
985 ed3bff83 2022-04-23 op free(newpath);
986 55e9459f 2022-06-19 op free(oldlabel);
987 55e9459f 2022-06-19 op free(newlabel);
988 d8b5af43 2022-06-19 op free(anclabel);
992 e9ce266e 2022-03-07 op const struct got_error *
993 e9ce266e 2022-03-07 op got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
994 5f56d41e 2022-07-28 op int nop, int strip, int reverse, struct got_object_id *commit_id,
995 5f56d41e 2022-07-28 op got_patch_progress_cb progress_cb, void *progress_arg,
996 5f56d41e 2022-07-28 op got_cancel_cb cancel_cb, void *cancel_arg)
998 4bcdc895 2022-05-17 op const struct got_error *err = NULL, *complete_err = NULL;
999 78f5ac24 2022-03-19 op struct got_fileindex *fileindex = NULL;
1000 5f56d41e 2022-07-28 op struct got_commit_object *commit = NULL;
1001 5f56d41e 2022-07-28 op struct got_tree_object *tree = NULL;
1002 f2dd7807 2022-05-17 op char *fileindex_path = NULL;
1003 60aa1fa0 2022-03-17 op char *oldpath, *newpath;
1004 e9ce266e 2022-03-07 op struct imsgbuf *ibuf;
1005 e9ce266e 2022-03-07 op int imsg_fds[2] = {-1, -1};
1006 55e9459f 2022-06-19 op int overlapcnt, done = 0, failed = 0;
1009 e9ce266e 2022-03-07 op ibuf = calloc(1, sizeof(*ibuf));
1010 e9ce266e 2022-03-07 op if (ibuf == NULL) {
1011 e9ce266e 2022-03-07 op err = got_error_from_errno("calloc");
1015 e9ce266e 2022-03-07 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1016 e9ce266e 2022-03-07 op err = got_error_from_errno("socketpair");
1020 e9ce266e 2022-03-07 op pid = fork();
1021 e9ce266e 2022-03-07 op if (pid == -1) {
1022 e9ce266e 2022-03-07 op err = got_error_from_errno("fork");
1024 e9ce266e 2022-03-07 op } else if (pid == 0) {
1025 e9ce266e 2022-03-07 op got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
1027 e9ce266e 2022-03-07 op /* not reached */
1030 e9ce266e 2022-03-07 op if (close(imsg_fds[1]) == -1) {
1031 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
1034 e9ce266e 2022-03-07 op imsg_fds[1] = -1;
1035 e9ce266e 2022-03-07 op imsg_init(ibuf, imsg_fds[0]);
1037 e9ce266e 2022-03-07 op err = send_patch(ibuf, fd);
1042 f2dd7807 2022-05-17 op err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
1047 5f56d41e 2022-07-28 op if (commit_id) {
1048 5f56d41e 2022-07-28 op err = got_object_open_as_commit(&commit, repo, commit_id);
1052 5f56d41e 2022-07-28 op err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1057 e9ce266e 2022-03-07 op while (!done && err == NULL) {
1058 e9ce266e 2022-03-07 op struct got_patch p;
1059 60aa1fa0 2022-03-17 op struct patch_args pa;
1061 60aa1fa0 2022-03-17 op pa.progress_cb = progress_cb;
1062 60aa1fa0 2022-03-17 op pa.progress_arg = progress_arg;
1063 60aa1fa0 2022-03-17 op pa.head = &p.head;
1065 9d6cabd5 2022-04-07 op err = recv_patch(ibuf, &done, &p, strip);
1066 e9ce266e 2022-03-07 op if (err || done)
1069 78f5ac24 2022-03-19 op err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
1070 78f5ac24 2022-03-19 op &newpath, worktree, repo, fileindex);
1071 78f5ac24 2022-03-19 op if (err == NULL)
1072 55e9459f 2022-06-19 op err = apply_patch(&overlapcnt, worktree, repo,
1073 38d61ead 2022-07-23 op fileindex, oldpath, newpath, &p, nop, reverse,
1074 5f56d41e 2022-07-28 op commit_id, tree, &pa, cancel_cb, cancel_arg);
1075 60aa1fa0 2022-03-17 op if (err != NULL) {
1077 60aa1fa0 2022-03-17 op /* recoverable errors */
1078 60aa1fa0 2022-03-17 op if (err->code == GOT_ERR_FILE_STATUS ||
1079 60aa1fa0 2022-03-17 op (err->code == GOT_ERR_ERRNO && errno == ENOENT))
1080 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
1081 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, err);
1082 60aa1fa0 2022-03-17 op else if (err->code == GOT_ERR_HUNK_FAILED)
1083 60aa1fa0 2022-03-17 op err = report_progress(&pa, p.old, p.new,
1084 60aa1fa0 2022-03-17 op GOT_STATUS_CANNOT_UPDATE, NULL);
1086 55e9459f 2022-06-19 op if (overlapcnt != 0)
1089 60aa1fa0 2022-03-17 op free(oldpath);
1090 60aa1fa0 2022-03-17 op free(newpath);
1091 e9ce266e 2022-03-07 op patch_free(&p);
1098 4bcdc895 2022-05-17 op if (fileindex != NULL)
1099 4bcdc895 2022-05-17 op complete_err = got_worktree_patch_complete(fileindex,
1100 4bcdc895 2022-05-17 op fileindex_path);
1101 f2dd7807 2022-05-17 op if (complete_err && err == NULL)
1102 f2dd7807 2022-05-17 op err = complete_err;
1103 4bcdc895 2022-05-17 op free(fileindex_path);
1105 5f56d41e 2022-07-28 op got_object_tree_close(tree);
1107 5f56d41e 2022-07-28 op got_object_commit_close(commit);
1108 e9ce266e 2022-03-07 op if (fd != -1 && close(fd) == -1 && err == NULL)
1109 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
1110 e9ce266e 2022-03-07 op if (ibuf != NULL)
1111 e9ce266e 2022-03-07 op imsg_clear(ibuf);
1112 e9ce266e 2022-03-07 op if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
1113 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
1114 e9ce266e 2022-03-07 op if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
1115 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
1116 60aa1fa0 2022-03-17 op if (err == NULL && failed)
1117 60aa1fa0 2022-03-17 op err = got_error(GOT_ERR_PATCH_FAILED);