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 are still missing:
19 e9ce266e 2022-03-07 op * + "No final newline" handling
21 e9ce266e 2022-03-07 op * Things that we may want to support:
22 e9ce266e 2022-03-07 op * + support indented patches?
23 e9ce266e 2022-03-07 op * + support other kinds of patches?
26 e9ce266e 2022-03-07 op #include <sys/types.h>
27 e9ce266e 2022-03-07 op #include <sys/queue.h>
28 e9ce266e 2022-03-07 op #include <sys/socket.h>
29 5b67f96e 2022-03-13 op #include <sys/stat.h>
30 e9ce266e 2022-03-07 op #include <sys/uio.h>
32 dbda770b 2022-03-13 op #include <errno.h>
33 e9ce266e 2022-03-07 op #include <limits.h>
34 e9ce266e 2022-03-07 op #include <sha1.h>
35 e9ce266e 2022-03-07 op #include <stdint.h>
36 e9ce266e 2022-03-07 op #include <stdio.h>
37 e9ce266e 2022-03-07 op #include <stdlib.h>
38 e9ce266e 2022-03-07 op #include <string.h>
39 e9ce266e 2022-03-07 op #include <unistd.h>
40 e9ce266e 2022-03-07 op #include <imsg.h>
42 e9ce266e 2022-03-07 op #include "got_error.h"
43 e9ce266e 2022-03-07 op #include "got_object.h"
44 e9ce266e 2022-03-07 op #include "got_path.h"
45 e9ce266e 2022-03-07 op #include "got_reference.h"
46 e9ce266e 2022-03-07 op #include "got_cancel.h"
47 e9ce266e 2022-03-07 op #include "got_worktree.h"
48 e9ce266e 2022-03-07 op #include "got_opentemp.h"
49 e9ce266e 2022-03-07 op #include "got_patch.h"
51 e9ce266e 2022-03-07 op #include "got_lib_delta.h"
52 e9ce266e 2022-03-07 op #include "got_lib_object.h"
53 e9ce266e 2022-03-07 op #include "got_lib_privsep.h"
55 e9ce266e 2022-03-07 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
57 e9ce266e 2022-03-07 op struct got_patch_hunk {
58 e9ce266e 2022-03-07 op STAILQ_ENTRY(got_patch_hunk) entries;
59 e9ce266e 2022-03-07 op long old_from;
60 e9ce266e 2022-03-07 op long old_lines;
61 e9ce266e 2022-03-07 op long new_from;
62 e9ce266e 2022-03-07 op long new_lines;
68 e9ce266e 2022-03-07 op struct got_patch {
71 e9ce266e 2022-03-07 op STAILQ_HEAD(, got_patch_hunk) head;
74 e9ce266e 2022-03-07 op static const struct got_error *
75 e9ce266e 2022-03-07 op send_patch(struct imsgbuf *ibuf, int fd)
77 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
79 e9ce266e 2022-03-07 op if (imsg_compose(ibuf, GOT_IMSG_PATCH_FILE, 0, 0, fd,
80 e9ce266e 2022-03-07 op NULL, 0) == -1) {
81 e9ce266e 2022-03-07 op err = got_error_from_errno(
82 e9ce266e 2022-03-07 op "imsg_compose GOT_IMSG_PATCH_FILE");
87 e9ce266e 2022-03-07 op if (imsg_flush(ibuf) == -1) {
88 e9ce266e 2022-03-07 op err = got_error_from_errno("imsg_flush");
89 e9ce266e 2022-03-07 op imsg_clear(ibuf);
96 e9ce266e 2022-03-07 op patch_free(struct got_patch *p)
98 e9ce266e 2022-03-07 op struct got_patch_hunk *h;
101 e9ce266e 2022-03-07 op while (!STAILQ_EMPTY(&p->head)) {
102 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
103 e9ce266e 2022-03-07 op STAILQ_REMOVE_HEAD(&p->head, entries);
105 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i)
106 e9ce266e 2022-03-07 op free(h->lines[i]);
107 e9ce266e 2022-03-07 op free(h->lines);
111 e9ce266e 2022-03-07 op free(p->new);
112 e9ce266e 2022-03-07 op free(p->old);
115 e9ce266e 2022-03-07 op static const struct got_error *
116 e9ce266e 2022-03-07 op pushline(struct got_patch_hunk *h, const char *line)
119 e9ce266e 2022-03-07 op size_t newcap;
121 e9ce266e 2022-03-07 op if (h->len == h->cap) {
122 e9ce266e 2022-03-07 op if ((newcap = h->cap * 1.5) == 0)
124 e9ce266e 2022-03-07 op t = recallocarray(h->lines, h->cap, newcap,
125 e9ce266e 2022-03-07 op sizeof(h->lines[0]));
126 e9ce266e 2022-03-07 op if (t == NULL)
127 e9ce266e 2022-03-07 op return got_error_from_errno("recallocarray");
128 e9ce266e 2022-03-07 op h->lines = t;
129 e9ce266e 2022-03-07 op h->cap = newcap;
132 e9ce266e 2022-03-07 op if ((t = strdup(line)) == NULL)
133 e9ce266e 2022-03-07 op return got_error_from_errno("strdup");
135 e9ce266e 2022-03-07 op h->lines[h->len++] = t;
139 e9ce266e 2022-03-07 op static const struct got_error *
140 e9ce266e 2022-03-07 op recv_patch(struct imsgbuf *ibuf, int *done, struct got_patch *p)
142 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
143 e9ce266e 2022-03-07 op struct imsg imsg;
144 e9ce266e 2022-03-07 op struct got_imsg_patch_hunk hdr;
145 e9ce266e 2022-03-07 op struct got_imsg_patch patch;
146 e9ce266e 2022-03-07 op struct got_patch_hunk *h = NULL;
147 e9ce266e 2022-03-07 op size_t datalen;
149 e9ce266e 2022-03-07 op memset(p, 0, sizeof(*p));
150 e9ce266e 2022-03-07 op STAILQ_INIT(&p->head);
152 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
155 e9ce266e 2022-03-07 op if (imsg.hdr.type == GOT_IMSG_PATCH_EOF) {
159 e9ce266e 2022-03-07 op if (imsg.hdr.type != GOT_IMSG_PATCH) {
160 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
163 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
164 e9ce266e 2022-03-07 op if (datalen != sizeof(patch)) {
165 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
168 e9ce266e 2022-03-07 op memcpy(&patch, imsg.data, sizeof(patch));
169 e9ce266e 2022-03-07 op if (*patch.old != '\0' && (p->old = strdup(patch.old)) == NULL) {
170 e9ce266e 2022-03-07 op err = got_error_from_errno("strdup");
173 e9ce266e 2022-03-07 op if (*patch.new != '\0' && (p->new = strdup(patch.new)) == NULL) {
174 e9ce266e 2022-03-07 op err = got_error_from_errno("strdup");
177 b95c53df 2022-03-12 op if (p->old == NULL && p->new == NULL) {
178 b95c53df 2022-03-12 op err = got_error(GOT_ERR_PATCH_MALFORMED);
182 e9ce266e 2022-03-07 op imsg_free(&imsg);
187 e9ce266e 2022-03-07 op err = got_privsep_recv_imsg(&imsg, ibuf, 0);
191 e9ce266e 2022-03-07 op switch (imsg.hdr.type) {
192 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_DONE:
194 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_HUNK:
195 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
196 e9ce266e 2022-03-07 op if (datalen != sizeof(hdr)) {
197 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_LEN);
200 e9ce266e 2022-03-07 op memcpy(&hdr, imsg.data, sizeof(hdr));
201 e9ce266e 2022-03-07 op if ((h = calloc(1, sizeof(*h))) == NULL) {
202 e9ce266e 2022-03-07 op err = got_error_from_errno("calloc");
205 e9ce266e 2022-03-07 op h->old_from = hdr.oldfrom;
206 e9ce266e 2022-03-07 op h->old_lines = hdr.oldlines;
207 e9ce266e 2022-03-07 op h->new_from = hdr.newfrom;
208 e9ce266e 2022-03-07 op h->new_lines = hdr.newlines;
209 e9ce266e 2022-03-07 op STAILQ_INSERT_TAIL(&p->head, h, entries);
211 e9ce266e 2022-03-07 op case GOT_IMSG_PATCH_LINE:
212 e9ce266e 2022-03-07 op if (h == NULL) {
213 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
216 e9ce266e 2022-03-07 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
217 e9ce266e 2022-03-07 op t = imsg.data;
218 e9ce266e 2022-03-07 op /* at least one char plus newline */
219 e9ce266e 2022-03-07 op if (datalen < 2 || t[datalen-1] != '\0') {
220 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
223 e9ce266e 2022-03-07 op if (*t != ' ' && *t != '-' && *t != '+') {
224 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
227 e9ce266e 2022-03-07 op err = pushline(h, t);
232 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PRIVSEP_MSG);
236 e9ce266e 2022-03-07 op imsg_free(&imsg);
240 e9ce266e 2022-03-07 op imsg_free(&imsg);
245 e9ce266e 2022-03-07 op * Copy data from orig starting at copypos until pos into tmp.
246 e9ce266e 2022-03-07 op * If pos is -1, copy until EOF.
248 e9ce266e 2022-03-07 op static const struct got_error *
249 e9ce266e 2022-03-07 op copy(FILE *tmp, FILE *orig, off_t copypos, off_t pos)
251 e9ce266e 2022-03-07 op char buf[BUFSIZ];
252 e9ce266e 2022-03-07 op size_t len, r, w;
254 e9ce266e 2022-03-07 op if (fseek(orig, copypos, SEEK_SET) == -1)
255 e9ce266e 2022-03-07 op return got_error_from_errno("fseek");
257 e9ce266e 2022-03-07 op while (pos == -1 || copypos < pos) {
258 e9ce266e 2022-03-07 op len = sizeof(buf);
260 e9ce266e 2022-03-07 op len = MIN(len, (size_t)pos - copypos);
261 e9ce266e 2022-03-07 op r = fread(buf, 1, len, orig);
262 e9ce266e 2022-03-07 op if (r != len && ferror(orig))
263 e9ce266e 2022-03-07 op return got_error_from_errno("fread");
264 e9ce266e 2022-03-07 op w = fwrite(buf, 1, r, tmp);
266 e9ce266e 2022-03-07 op return got_error_from_errno("fwrite");
267 e9ce266e 2022-03-07 op copypos += len;
268 e9ce266e 2022-03-07 op if (r != len && feof(orig)) {
269 e9ce266e 2022-03-07 op if (pos == -1)
271 e9ce266e 2022-03-07 op return got_error(GOT_ERR_PATCH_DONT_APPLY);
277 e9ce266e 2022-03-07 op static const struct got_error *
278 33df9995 2022-03-11 op locate_hunk(FILE *orig, struct got_patch_hunk *h, off_t *pos, long *lineno)
280 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
281 e9ce266e 2022-03-07 op char *line = NULL;
282 e9ce266e 2022-03-07 op char mode = *h->lines[0];
283 e9ce266e 2022-03-07 op size_t linesize = 0;
284 e9ce266e 2022-03-07 op ssize_t linelen;
285 e9ce266e 2022-03-07 op off_t match = -1;
286 e9ce266e 2022-03-07 op long match_lineno = -1;
289 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
290 e9ce266e 2022-03-07 op if (linelen == -1) {
291 e9ce266e 2022-03-07 op if (ferror(orig))
292 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
293 e9ce266e 2022-03-07 op else if (match == -1)
294 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PATCH_DONT_APPLY);
299 e9ce266e 2022-03-07 op if ((mode == ' ' && !strcmp(h->lines[0]+1, line)) ||
300 e9ce266e 2022-03-07 op (mode == '-' && !strcmp(h->lines[0]+1, line)) ||
301 e9ce266e 2022-03-07 op (mode == '+' && *lineno == h->old_from)) {
302 e9ce266e 2022-03-07 op match = ftello(orig);
303 e9ce266e 2022-03-07 op if (match == -1) {
304 e9ce266e 2022-03-07 op err = got_error_from_errno("ftello");
307 e9ce266e 2022-03-07 op match -= linelen;
308 e9ce266e 2022-03-07 op match_lineno = (*lineno)-1;
311 e9ce266e 2022-03-07 op if (*lineno >= h->old_from && match != -1)
315 e9ce266e 2022-03-07 op if (err == NULL) {
316 33df9995 2022-03-11 op *pos = match;
317 e9ce266e 2022-03-07 op *lineno = match_lineno;
318 e9ce266e 2022-03-07 op if (fseek(orig, match, SEEK_SET) == -1)
319 e9ce266e 2022-03-07 op err = got_error_from_errno("fseek");
326 e9ce266e 2022-03-07 op static const struct got_error *
327 e9ce266e 2022-03-07 op test_hunk(FILE *orig, struct got_patch_hunk *h)
329 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
330 e9ce266e 2022-03-07 op char *line = NULL;
331 e9ce266e 2022-03-07 op size_t linesize = 0, i = 0;
332 e9ce266e 2022-03-07 op ssize_t linelen;
334 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
335 e9ce266e 2022-03-07 op switch (*h->lines[i]) {
340 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
341 e9ce266e 2022-03-07 op if (linelen == -1) {
342 e9ce266e 2022-03-07 op if (ferror(orig))
343 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
345 e9ce266e 2022-03-07 op err = got_error(
346 e9ce266e 2022-03-07 op GOT_ERR_PATCH_DONT_APPLY);
349 e9ce266e 2022-03-07 op if (strcmp(h->lines[i]+1, line)) {
350 e9ce266e 2022-03-07 op err = got_error(GOT_ERR_PATCH_DONT_APPLY);
362 e9ce266e 2022-03-07 op static const struct got_error *
363 e9ce266e 2022-03-07 op apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
365 e9ce266e 2022-03-07 op size_t i = 0;
367 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
368 e9ce266e 2022-03-07 op switch (*h->lines[i]) {
370 e9ce266e 2022-03-07 op if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
371 e9ce266e 2022-03-07 op return got_error_from_errno("fprintf");
372 e9ce266e 2022-03-07 op /* fallthrough */
377 e9ce266e 2022-03-07 op if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
378 e9ce266e 2022-03-07 op return got_error_from_errno("fprintf");
385 6e96b326 2022-03-12 op static const struct got_error *
386 6e96b326 2022-03-12 op patch_file(struct got_patch *p, const char *path, FILE *tmp)
388 6e96b326 2022-03-12 op const struct got_error *err = NULL;
389 6e96b326 2022-03-12 op struct got_patch_hunk *h;
391 6e96b326 2022-03-12 op long lineno = 0;
393 6e96b326 2022-03-12 op off_t copypos, pos;
394 6e96b326 2022-03-12 op char *line = NULL;
395 6e96b326 2022-03-12 op size_t linesize = 0;
396 6e96b326 2022-03-12 op ssize_t linelen;
398 e9ce266e 2022-03-07 op if (p->old == NULL) { /* create */
399 e9ce266e 2022-03-07 op h = STAILQ_FIRST(&p->head);
400 6e96b326 2022-03-12 op if (h == NULL || STAILQ_NEXT(h, entries) != NULL)
401 6e96b326 2022-03-12 op return got_error(GOT_ERR_PATCH_MALFORMED);
402 e9ce266e 2022-03-07 op for (i = 0; i < h->len; ++i) {
403 6e96b326 2022-03-12 op if (fprintf(tmp, "%s", h->lines[i]+1) < 0)
404 6e96b326 2022-03-12 op return got_error_from_errno("fprintf");
409 e9ce266e 2022-03-07 op if ((orig = fopen(path, "r")) == NULL) {
410 e9ce266e 2022-03-07 op err = got_error_from_errno2("fopen", path);
415 e9ce266e 2022-03-07 op STAILQ_FOREACH(h, &p->head, entries) {
416 6e96b326 2022-03-12 op if (h->lines == NULL)
420 33df9995 2022-03-11 op err = locate_hunk(orig, h, &pos, &lineno);
421 e9ce266e 2022-03-07 op if (err != NULL)
423 e9ce266e 2022-03-07 op err = copy(tmp, orig, copypos, pos);
424 e9ce266e 2022-03-07 op if (err != NULL)
426 e9ce266e 2022-03-07 op copypos = pos;
428 e9ce266e 2022-03-07 op err = test_hunk(orig, h);
429 e9ce266e 2022-03-07 op if (err != NULL && err->code == GOT_ERR_PATCH_DONT_APPLY) {
431 e9ce266e 2022-03-07 op * try to apply the hunk again starting the search
432 e9ce266e 2022-03-07 op * after the previous partial match.
434 e9ce266e 2022-03-07 op if (fseek(orig, pos, SEEK_SET) == -1) {
435 e9ce266e 2022-03-07 op err = got_error_from_errno("fseek");
438 e9ce266e 2022-03-07 op linelen = getline(&line, &linesize, orig);
439 e9ce266e 2022-03-07 op if (linelen == -1) {
440 e9ce266e 2022-03-07 op err = got_error_from_errno("getline");
444 e9ce266e 2022-03-07 op goto tryagain;
446 e9ce266e 2022-03-07 op if (err != NULL)
449 e9ce266e 2022-03-07 op err = apply_hunk(tmp, h, &lineno);
450 e9ce266e 2022-03-07 op if (err != NULL)
453 e9ce266e 2022-03-07 op copypos = ftello(orig);
454 e9ce266e 2022-03-07 op if (copypos == -1) {
455 e9ce266e 2022-03-07 op err = got_error_from_errno("ftello");
461 5b67f96e 2022-03-13 op if (p->new == NULL) {
462 5b67f96e 2022-03-13 op struct stat sb;
464 5b67f96e 2022-03-13 op if (fstat(fileno(orig), &sb) == -1)
465 5b67f96e 2022-03-13 op err = got_error_from_errno("fstat");
466 5b67f96e 2022-03-13 op else if (sb.st_size != copypos)
467 5b67f96e 2022-03-13 op err = got_error(GOT_ERR_PATCH_DONT_APPLY);
468 5b67f96e 2022-03-13 op } else if (!feof(orig))
469 e9ce266e 2022-03-07 op err = copy(tmp, orig, copypos, -1);
472 6e96b326 2022-03-12 op if (orig != NULL)
473 6e96b326 2022-03-12 op fclose(orig);
477 6e96b326 2022-03-12 op static const struct got_error *
478 dbda770b 2022-03-13 op build_pathlist(const char *p, char **path, struct got_pathlist_head *head,
479 dbda770b 2022-03-13 op struct got_worktree *worktree)
481 dbda770b 2022-03-13 op const struct got_error *err;
482 dbda770b 2022-03-13 op struct got_pathlist_entry *pe;
484 dbda770b 2022-03-13 op err = got_worktree_resolve_path(path, worktree, p);
485 dbda770b 2022-03-13 op if (err == NULL)
486 dbda770b 2022-03-13 op err = got_pathlist_insert(&pe, head, *path, NULL);
490 dbda770b 2022-03-13 op static const struct got_error *
491 dbda770b 2022-03-13 op can_rm(void *arg, unsigned char status, unsigned char staged_status,
492 dbda770b 2022-03-13 op const char *path, struct got_object_id *blob_id,
493 dbda770b 2022-03-13 op struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
494 dbda770b 2022-03-13 op int dirfd, const char *de_name)
496 dbda770b 2022-03-13 op if (status == GOT_STATUS_NONEXISTENT)
497 dbda770b 2022-03-13 op return got_error_set_errno(ENOENT, path);
498 dbda770b 2022-03-13 op if (status != GOT_STATUS_NO_CHANGE &&
499 dbda770b 2022-03-13 op status != GOT_STATUS_ADD &&
500 dbda770b 2022-03-13 op status != GOT_STATUS_MODIFY &&
501 dbda770b 2022-03-13 op status != GOT_STATUS_MODE_CHANGE)
502 dbda770b 2022-03-13 op return got_error_path(path, GOT_ERR_FILE_STATUS);
503 dbda770b 2022-03-13 op if (staged_status == GOT_STATUS_DELETE)
504 dbda770b 2022-03-13 op return got_error_path(path, GOT_ERR_FILE_STATUS);
508 dbda770b 2022-03-13 op static const struct got_error *
509 dbda770b 2022-03-13 op can_add(void *arg, unsigned char status, unsigned char staged_status,
510 dbda770b 2022-03-13 op const char *path, struct got_object_id *blob_id,
511 dbda770b 2022-03-13 op struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
512 dbda770b 2022-03-13 op int dirfd, const char *de_name)
514 dbda770b 2022-03-13 op if (status != GOT_STATUS_NONEXISTENT)
515 dbda770b 2022-03-13 op return got_error_path(path, GOT_ERR_FILE_STATUS);
519 dbda770b 2022-03-13 op static const struct got_error *
520 dbda770b 2022-03-13 op can_edit(void *arg, unsigned char status, unsigned char staged_status,
521 dbda770b 2022-03-13 op const char *path, struct got_object_id *blob_id,
522 dbda770b 2022-03-13 op struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
523 dbda770b 2022-03-13 op int dirfd, const char *de_name)
525 dbda770b 2022-03-13 op if (status == GOT_STATUS_NONEXISTENT)
526 dbda770b 2022-03-13 op return got_error_set_errno(ENOENT, path);
527 dbda770b 2022-03-13 op if (status != GOT_STATUS_NO_CHANGE &&
528 dbda770b 2022-03-13 op status != GOT_STATUS_ADD &&
529 dbda770b 2022-03-13 op status != GOT_STATUS_MODIFY)
530 dbda770b 2022-03-13 op return got_error_path(path, GOT_ERR_FILE_STATUS);
531 dbda770b 2022-03-13 op if (staged_status == GOT_STATUS_DELETE)
532 dbda770b 2022-03-13 op return got_error_path(path, GOT_ERR_FILE_STATUS);
536 dbda770b 2022-03-13 op static const struct got_error *
537 dbda770b 2022-03-13 op check_file_status(struct got_patch *p, int file_renamed,
538 dbda770b 2022-03-13 op struct got_worktree *worktree, struct got_repository *repo,
539 dbda770b 2022-03-13 op struct got_pathlist_head *old, struct got_pathlist_head *new,
540 dbda770b 2022-03-13 op got_cancel_cb cancel_cb, void *cancel_arg)
542 dbda770b 2022-03-13 op static const struct got_error *err;
544 dbda770b 2022-03-13 op if (p->old != NULL && p->new == NULL)
545 dbda770b 2022-03-13 op return got_worktree_status(worktree, old, repo, 0,
546 dbda770b 2022-03-13 op can_rm, NULL, cancel_cb, cancel_arg);
547 dbda770b 2022-03-13 op else if (file_renamed) {
548 dbda770b 2022-03-13 op err = got_worktree_status(worktree, old, repo, 0,
549 dbda770b 2022-03-13 op can_rm, NULL, cancel_cb, cancel_arg);
552 dbda770b 2022-03-13 op return got_worktree_status(worktree, new, repo, 0,
553 dbda770b 2022-03-13 op can_add, NULL, cancel_cb, cancel_arg);
554 dbda770b 2022-03-13 op } else if (p->old == NULL)
555 dbda770b 2022-03-13 op return got_worktree_status(worktree, new, repo, 0,
556 dbda770b 2022-03-13 op can_add, NULL, cancel_cb, cancel_arg);
558 dbda770b 2022-03-13 op return got_worktree_status(worktree, new, repo, 0,
559 dbda770b 2022-03-13 op can_edit, NULL, cancel_cb, cancel_arg);
562 dbda770b 2022-03-13 op static const struct got_error *
563 6e96b326 2022-03-12 op apply_patch(struct got_worktree *worktree, struct got_repository *repo,
564 6e96b326 2022-03-12 op struct got_patch *p, got_worktree_delete_cb delete_cb, void *delete_arg,
565 dbda770b 2022-03-13 op got_worktree_checkout_cb add_cb, void *add_arg, got_cancel_cb cancel_cb,
566 dbda770b 2022-03-13 op void *cancel_arg)
568 6e96b326 2022-03-12 op const struct got_error *err = NULL;
569 dbda770b 2022-03-13 op struct got_pathlist_head oldpaths, newpaths;
570 6e96b326 2022-03-12 op int file_renamed = 0;
571 6e96b326 2022-03-12 op char *oldpath = NULL, *newpath = NULL;
572 6e96b326 2022-03-12 op char *tmppath = NULL, *template = NULL;
573 6e96b326 2022-03-12 op FILE *tmp = NULL;
575 dbda770b 2022-03-13 op TAILQ_INIT(&oldpaths);
576 dbda770b 2022-03-13 op TAILQ_INIT(&newpaths);
578 dbda770b 2022-03-13 op err = build_pathlist(p->old != NULL ? p->old : p->new, &oldpath,
579 dbda770b 2022-03-13 op &oldpaths, worktree);
583 dbda770b 2022-03-13 op err = build_pathlist(p->new != NULL ? p->new : p->old, &newpath,
584 dbda770b 2022-03-13 op &newpaths, worktree);
588 dbda770b 2022-03-13 op if (p->old != NULL && p->new != NULL && strcmp(p->old, p->new))
589 dbda770b 2022-03-13 op file_renamed = 1;
591 dbda770b 2022-03-13 op err = check_file_status(p, file_renamed, worktree, repo, &oldpaths,
592 dbda770b 2022-03-13 op &newpaths, cancel_cb, cancel_arg);
596 6e96b326 2022-03-12 op if (asprintf(&template, "%s/got-patch",
597 6e96b326 2022-03-12 op got_worktree_get_root_path(worktree)) == -1) {
598 6e96b326 2022-03-12 op err = got_error_from_errno(template);
602 6e96b326 2022-03-12 op err = got_opentemp_named(&tmppath, &tmp, template);
605 6e96b326 2022-03-12 op err = patch_file(p, oldpath, tmp);
609 5b67f96e 2022-03-13 op if (p->old != NULL && p->new == NULL) {
610 5b67f96e 2022-03-13 op err = got_worktree_schedule_delete(worktree, &oldpaths,
611 5b67f96e 2022-03-13 op 0, NULL, delete_cb, delete_arg, repo, 0, 0);
615 6e96b326 2022-03-12 op if (rename(tmppath, newpath) == -1) {
616 6e96b326 2022-03-12 op err = got_error_from_errno3("rename", tmppath, newpath);
620 6e96b326 2022-03-12 op if (file_renamed) {
621 dbda770b 2022-03-13 op err = got_worktree_schedule_delete(worktree, &oldpaths,
622 dbda770b 2022-03-13 op 0, NULL, delete_cb, delete_arg, repo, 0, 0);
623 6e96b326 2022-03-12 op if (err == NULL)
624 dbda770b 2022-03-13 op err = got_worktree_schedule_add(worktree, &newpaths,
625 dbda770b 2022-03-13 op add_cb, add_arg, repo, 1);
626 6e96b326 2022-03-12 op } else if (p->old == NULL)
627 dbda770b 2022-03-13 op err = got_worktree_schedule_add(worktree, &newpaths,
628 dbda770b 2022-03-13 op add_cb, add_arg, repo, 1);
630 6e96b326 2022-03-12 op printf("M %s\n", oldpath); /* XXX */
633 dbda770b 2022-03-13 op if (err != NULL && newpath != NULL && (file_renamed || p->old == NULL))
634 6e96b326 2022-03-12 op unlink(newpath);
635 6f5cb1bd 2022-03-08 op free(template);
636 e9ce266e 2022-03-07 op if (tmppath != NULL)
637 e9ce266e 2022-03-07 op unlink(tmppath);
638 e9ce266e 2022-03-07 op free(tmppath);
639 dbda770b 2022-03-13 op got_pathlist_free(&oldpaths);
640 dbda770b 2022-03-13 op got_pathlist_free(&newpaths);
641 6e96b326 2022-03-12 op free(oldpath);
642 6e96b326 2022-03-12 op free(newpath);
646 e9ce266e 2022-03-07 op const struct got_error *
647 e9ce266e 2022-03-07 op got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
648 d955343d 2022-03-08 op got_worktree_delete_cb delete_cb, void *delete_arg,
649 dbda770b 2022-03-13 op got_worktree_checkout_cb add_cb, void *add_arg, got_cancel_cb cancel_cb,
650 dbda770b 2022-03-13 op void *cancel_arg)
652 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
653 e9ce266e 2022-03-07 op struct imsgbuf *ibuf;
654 e9ce266e 2022-03-07 op int imsg_fds[2] = {-1, -1};
655 e9ce266e 2022-03-07 op int done = 0;
658 e9ce266e 2022-03-07 op ibuf = calloc(1, sizeof(*ibuf));
659 e9ce266e 2022-03-07 op if (ibuf == NULL) {
660 e9ce266e 2022-03-07 op err = got_error_from_errno("calloc");
664 e9ce266e 2022-03-07 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
665 e9ce266e 2022-03-07 op err = got_error_from_errno("socketpair");
669 e9ce266e 2022-03-07 op pid = fork();
670 e9ce266e 2022-03-07 op if (pid == -1) {
671 e9ce266e 2022-03-07 op err = got_error_from_errno("fork");
673 e9ce266e 2022-03-07 op } else if (pid == 0) {
674 e9ce266e 2022-03-07 op got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PATCH,
676 e9ce266e 2022-03-07 op /* not reached */
679 e9ce266e 2022-03-07 op if (close(imsg_fds[1]) == -1) {
680 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
683 e9ce266e 2022-03-07 op imsg_fds[1] = -1;
684 e9ce266e 2022-03-07 op imsg_init(ibuf, imsg_fds[0]);
686 e9ce266e 2022-03-07 op err = send_patch(ibuf, fd);
691 e9ce266e 2022-03-07 op while (!done && err == NULL) {
692 e9ce266e 2022-03-07 op struct got_patch p;
694 e9ce266e 2022-03-07 op err = recv_patch(ibuf, &done, &p);
695 e9ce266e 2022-03-07 op if (err || done)
698 d955343d 2022-03-08 op err = apply_patch(worktree, repo, &p, delete_cb, delete_arg,
699 dbda770b 2022-03-13 op add_cb, add_arg, cancel_cb, cancel_arg);
700 e9ce266e 2022-03-07 op patch_free(&p);
706 e9ce266e 2022-03-07 op if (fd != -1 && close(fd) == -1 && err == NULL)
707 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
708 e9ce266e 2022-03-07 op if (ibuf != NULL)
709 e9ce266e 2022-03-07 op imsg_clear(ibuf);
710 e9ce266e 2022-03-07 op if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
711 e9ce266e 2022-03-07 op err = got_error_from_errno("close");
712 e9ce266e 2022-03-07 op if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
713 e9ce266e 2022-03-07 op err = got_error_from_errno("close");