Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
69 {
70 const struct got_error *err = NULL;
71 char *path;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
77 free(path);
78 return err;
79 }
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
83 {
84 const struct got_error *err = NULL;
85 FILE *tmpfile = NULL;
86 char *tmppath = NULL;
87 char *path = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
91 path = NULL;
92 goto done;
93 }
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
96 if (err)
97 goto done;
99 if (content) {
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
103 goto done;
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
109 unlink(tmppath);
110 goto done;
113 done:
114 if (fclose(tmpfile) == EOF && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
116 free(tmppath);
117 return err;
120 static const struct got_error *
121 read_meta_file(char **content, const char *path_got, const char *name)
123 const struct got_error *err = NULL;
124 char *path;
125 int fd = -1;
126 ssize_t n;
127 struct stat sb;
129 *content = NULL;
131 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 err = got_error_from_errno("asprintf");
133 path = NULL;
134 goto done;
137 fd = open(path, O_RDONLY | O_NOFOLLOW);
138 if (fd == -1) {
139 if (errno == ENOENT)
140 err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 else
142 err = got_error_from_errno2("open", path);
143 goto done;
145 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 : got_error_from_errno2("flock", path));
148 goto done;
151 if (fstat(fd, &sb) != 0) {
152 err = got_error_from_errno2("fstat", path);
153 goto done;
155 *content = calloc(1, sb.st_size);
156 if (*content == NULL) {
157 err = got_error_from_errno("calloc");
158 goto done;
161 n = read(fd, *content, sb.st_size);
162 if (n != sb.st_size) {
163 err = (n == -1 ? got_error_from_errno2("read", path) :
164 got_error_path(path, GOT_ERR_WORKTREE_META));
165 goto done;
167 if ((*content)[sb.st_size - 1] != '\n') {
168 err = got_error_path(path, GOT_ERR_WORKTREE_META);
169 goto done;
171 (*content)[sb.st_size - 1] = '\0';
173 done:
174 if (fd != -1 && close(fd) == -1 && err == NULL)
175 err = got_error_from_errno2("close", path_got);
176 free(path);
177 if (err) {
178 free(*content);
179 *content = NULL;
181 return err;
184 static const struct got_error *
185 write_head_ref(const char *path_got, struct got_reference *head_ref)
187 const struct got_error *err = NULL;
188 char *refstr = NULL;
190 if (got_ref_is_symbolic(head_ref)) {
191 refstr = got_ref_to_str(head_ref);
192 if (refstr == NULL)
193 return got_error_from_errno("got_ref_to_str");
194 } else {
195 refstr = strdup(got_ref_get_name(head_ref));
196 if (refstr == NULL)
197 return got_error_from_errno("strdup");
199 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
200 free(refstr);
201 return err;
204 const struct got_error *
205 got_worktree_init(const char *path, struct got_reference *head_ref,
206 const char *prefix, struct got_repository *repo)
208 const struct got_error *err = NULL;
209 struct got_object_id *commit_id = NULL;
210 uuid_t uuid;
211 uint32_t uuid_status;
212 int obj_type;
213 char *path_got = NULL;
214 char *formatstr = NULL;
215 char *absprefix = NULL;
216 char *basestr = NULL;
217 char *uuidstr = NULL;
219 if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 err = got_error(GOT_ERR_WORKTREE_REPO);
221 goto done;
224 err = got_ref_resolve(&commit_id, repo, head_ref);
225 if (err)
226 return err;
227 err = got_object_get_type(&obj_type, repo, commit_id);
228 if (err)
229 return err;
230 if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 return got_error(GOT_ERR_OBJ_TYPE);
233 if (!got_path_is_absolute(prefix)) {
234 if (asprintf(&absprefix, "/%s", prefix) == -1)
235 return got_error_from_errno("asprintf");
238 /* Create top-level directory (may already exist). */
239 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 err = got_error_from_errno2("mkdir", path);
241 goto done;
244 /* Create .got directory (may already exist). */
245 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 err = got_error_from_errno("asprintf");
247 goto done;
249 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 err = got_error_from_errno2("mkdir", path_got);
251 goto done;
254 /* Create an empty lock file. */
255 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
256 if (err)
257 goto done;
259 /* Create an empty file index. */
260 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
261 if (err)
262 goto done;
264 /* Write the HEAD reference. */
265 err = write_head_ref(path_got, head_ref);
266 if (err)
267 goto done;
269 /* Record our base commit. */
270 err = got_object_id_str(&basestr, commit_id);
271 if (err)
272 goto done;
273 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
274 if (err)
275 goto done;
277 /* Store path to repository. */
278 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 got_repo_get_path(repo));
280 if (err)
281 goto done;
283 /* Store in-repository path prefix. */
284 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 absprefix ? absprefix : prefix);
286 if (err)
287 goto done;
289 /* Generate UUID. */
290 uuid_create(&uuid, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status, "uuid_create");
293 goto done;
295 uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 if (uuid_status != uuid_s_ok) {
297 err = got_error_uuid(uuid_status, "uuid_to_string");
298 goto done;
300 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
301 if (err)
302 goto done;
304 /* Stamp work tree with format file. */
305 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 err = got_error_from_errno("asprintf");
307 goto done;
309 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
310 if (err)
311 goto done;
313 done:
314 free(commit_id);
315 free(path_got);
316 free(formatstr);
317 free(absprefix);
318 free(basestr);
319 free(uuidstr);
320 return err;
323 static const struct got_error *
324 open_worktree(struct got_worktree **worktree, const char *path)
326 const struct got_error *err = NULL;
327 char *path_got;
328 char *formatstr = NULL;
329 char *uuidstr = NULL;
330 char *path_lock = NULL;
331 char *base_commit_id_str = NULL;
332 int version, fd = -1;
333 const char *errstr;
334 struct got_repository *repo = NULL;
335 uint32_t uuid_status;
337 *worktree = NULL;
339 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 err = got_error_from_errno("asprintf");
341 path_got = NULL;
342 goto done;
345 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 err = got_error_from_errno("asprintf");
347 path_lock = NULL;
348 goto done;
351 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 if (fd == -1) {
353 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 : got_error_from_errno2("open", path_lock));
355 goto done;
358 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
359 if (err)
360 goto done;
362 version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 if (errstr) {
364 err = got_error_msg(GOT_ERR_WORKTREE_META,
365 "could not parse work tree format version number");
366 goto done;
368 if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 err = got_error(GOT_ERR_WORKTREE_VERS);
370 goto done;
373 *worktree = calloc(1, sizeof(**worktree));
374 if (*worktree == NULL) {
375 err = got_error_from_errno("calloc");
376 goto done;
378 (*worktree)->lockfd = -1;
380 (*worktree)->root_path = realpath(path, NULL);
381 if ((*worktree)->root_path == NULL) {
382 err = got_error_from_errno2("realpath", path);
383 goto done;
385 err = read_meta_file(&(*worktree)->repo_path, path_got,
386 GOT_WORKTREE_REPOSITORY);
387 if (err)
388 goto done;
390 err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 GOT_WORKTREE_PATH_PREFIX);
392 if (err)
393 goto done;
395 err = read_meta_file(&base_commit_id_str, path_got,
396 GOT_WORKTREE_BASE_COMMIT);
397 if (err)
398 goto done;
400 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
401 if (err)
402 goto done;
403 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 if (uuid_status != uuid_s_ok) {
405 err = got_error_uuid(uuid_status, "uuid_from_string");
406 goto done;
409 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
410 if (err)
411 goto done;
413 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 base_commit_id_str);
415 if (err)
416 goto done;
418 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 GOT_WORKTREE_HEAD_REF);
420 if (err)
421 goto done;
423 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 (*worktree)->root_path,
425 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 err = got_error_from_errno("asprintf");
427 goto done;
430 err = got_gotconfig_read(&(*worktree)->gotconfig,
431 (*worktree)->gotconfig_path);
433 (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
434 if ((*worktree)->root_fd == -1) {
435 err = got_error_from_errno2("open", (*worktree)->root_path);
436 goto done;
438 done:
439 if (repo) {
440 const struct got_error *close_err = got_repo_close(repo);
441 if (err == NULL)
442 err = close_err;
444 free(path_got);
445 free(path_lock);
446 free(base_commit_id_str);
447 free(uuidstr);
448 free(formatstr);
449 if (err) {
450 if (fd != -1)
451 close(fd);
452 if (*worktree != NULL)
453 got_worktree_close(*worktree);
454 *worktree = NULL;
455 } else
456 (*worktree)->lockfd = fd;
458 return err;
461 const struct got_error *
462 got_worktree_open(struct got_worktree **worktree, const char *path)
464 const struct got_error *err = NULL;
465 char *worktree_path;
467 worktree_path = strdup(path);
468 if (worktree_path == NULL)
469 return got_error_from_errno("strdup");
471 for (;;) {
472 char *parent_path;
474 err = open_worktree(worktree, worktree_path);
475 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
476 free(worktree_path);
477 return err;
479 if (*worktree) {
480 free(worktree_path);
481 return NULL;
483 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
484 break;
485 err = got_path_dirname(&parent_path, worktree_path);
486 if (err) {
487 if (err->code != GOT_ERR_BAD_PATH) {
488 free(worktree_path);
489 return err;
491 break;
493 free(worktree_path);
494 worktree_path = parent_path;
497 free(worktree_path);
498 return got_error(GOT_ERR_NOT_WORKTREE);
501 const struct got_error *
502 got_worktree_close(struct got_worktree *worktree)
504 const struct got_error *err = NULL;
506 if (worktree->lockfd != -1) {
507 if (close(worktree->lockfd) == -1)
508 err = got_error_from_errno2("close",
509 got_worktree_get_root_path(worktree));
511 if (close(worktree->root_fd) == -1 && err == NULL)
512 err = got_error_from_errno2("close",
513 got_worktree_get_root_path(worktree));
514 free(worktree->repo_path);
515 free(worktree->path_prefix);
516 free(worktree->base_commit_id);
517 free(worktree->head_ref_name);
518 free(worktree->root_path);
519 free(worktree->gotconfig_path);
520 got_gotconfig_free(worktree->gotconfig);
521 free(worktree);
522 return err;
525 const char *
526 got_worktree_get_root_path(struct got_worktree *worktree)
528 return worktree->root_path;
531 const char *
532 got_worktree_get_repo_path(struct got_worktree *worktree)
534 return worktree->repo_path;
536 const char *
537 got_worktree_get_path_prefix(struct got_worktree *worktree)
539 return worktree->path_prefix;
542 const struct got_error *
543 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
544 const char *path_prefix)
546 char *absprefix = NULL;
548 if (!got_path_is_absolute(path_prefix)) {
549 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
550 return got_error_from_errno("asprintf");
552 *match = (strcmp(absprefix ? absprefix : path_prefix,
553 worktree->path_prefix) == 0);
554 free(absprefix);
555 return NULL;
558 const char *
559 got_worktree_get_head_ref_name(struct got_worktree *worktree)
561 return worktree->head_ref_name;
564 const struct got_error *
565 got_worktree_set_head_ref(struct got_worktree *worktree,
566 struct got_reference *head_ref)
568 const struct got_error *err = NULL;
569 char *path_got = NULL, *head_ref_name = NULL;
571 if (asprintf(&path_got, "%s/%s", worktree->root_path,
572 GOT_WORKTREE_GOT_DIR) == -1) {
573 err = got_error_from_errno("asprintf");
574 path_got = NULL;
575 goto done;
578 head_ref_name = strdup(got_ref_get_name(head_ref));
579 if (head_ref_name == NULL) {
580 err = got_error_from_errno("strdup");
581 goto done;
584 err = write_head_ref(path_got, head_ref);
585 if (err)
586 goto done;
588 free(worktree->head_ref_name);
589 worktree->head_ref_name = head_ref_name;
590 done:
591 free(path_got);
592 if (err)
593 free(head_ref_name);
594 return err;
597 struct got_object_id *
598 got_worktree_get_base_commit_id(struct got_worktree *worktree)
600 return worktree->base_commit_id;
603 const struct got_error *
604 got_worktree_set_base_commit_id(struct got_worktree *worktree,
605 struct got_repository *repo, struct got_object_id *commit_id)
607 const struct got_error *err;
608 struct got_object *obj = NULL;
609 char *id_str = NULL;
610 char *path_got = NULL;
612 if (asprintf(&path_got, "%s/%s", worktree->root_path,
613 GOT_WORKTREE_GOT_DIR) == -1) {
614 err = got_error_from_errno("asprintf");
615 path_got = NULL;
616 goto done;
619 err = got_object_open(&obj, repo, commit_id);
620 if (err)
621 return err;
623 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
624 err = got_error(GOT_ERR_OBJ_TYPE);
625 goto done;
628 /* Record our base commit. */
629 err = got_object_id_str(&id_str, commit_id);
630 if (err)
631 goto done;
632 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
633 if (err)
634 goto done;
636 free(worktree->base_commit_id);
637 worktree->base_commit_id = got_object_id_dup(commit_id);
638 if (worktree->base_commit_id == NULL) {
639 err = got_error_from_errno("got_object_id_dup");
640 goto done;
642 done:
643 if (obj)
644 got_object_close(obj);
645 free(id_str);
646 free(path_got);
647 return err;
650 const struct got_gotconfig *
651 got_worktree_get_gotconfig(struct got_worktree *worktree)
653 return worktree->gotconfig;
656 static const struct got_error *
657 lock_worktree(struct got_worktree *worktree, int operation)
659 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
660 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
661 : got_error_from_errno2("flock",
662 got_worktree_get_root_path(worktree)));
663 return NULL;
666 static const struct got_error *
667 add_dir_on_disk(struct got_worktree *worktree, const char *path)
669 const struct got_error *err = NULL;
670 char *abspath;
672 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
673 return got_error_from_errno("asprintf");
675 err = got_path_mkdir(abspath);
676 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
677 struct stat sb;
678 err = NULL;
679 if (lstat(abspath, &sb) == -1) {
680 err = got_error_from_errno2("lstat", abspath);
681 } else if (!S_ISDIR(sb.st_mode)) {
682 /* TODO directory is obstructed; do something */
683 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
686 free(abspath);
687 return err;
690 static const struct got_error *
691 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
693 const struct got_error *err = NULL;
694 uint8_t fbuf1[8192];
695 uint8_t fbuf2[8192];
696 size_t flen1 = 0, flen2 = 0;
698 *same = 1;
700 for (;;) {
701 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
702 if (flen1 == 0 && ferror(f1)) {
703 err = got_error_from_errno("fread");
704 break;
706 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
707 if (flen2 == 0 && ferror(f2)) {
708 err = got_error_from_errno("fread");
709 break;
711 if (flen1 == 0) {
712 if (flen2 != 0)
713 *same = 0;
714 break;
715 } else if (flen2 == 0) {
716 if (flen1 != 0)
717 *same = 0;
718 break;
719 } else if (flen1 == flen2) {
720 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
721 *same = 0;
722 break;
724 } else {
725 *same = 0;
726 break;
730 return err;
733 static const struct got_error *
734 check_files_equal(int *same, FILE *f1, FILE *f2)
736 struct stat sb;
737 size_t size1, size2;
739 *same = 1;
741 if (fstat(fileno(f1), &sb) != 0)
742 return got_error_from_errno("fstat");
743 size1 = sb.st_size;
745 if (fstat(fileno(f2), &sb) != 0)
746 return got_error_from_errno("fstat");
747 size2 = sb.st_size;
749 if (size1 != size2) {
750 *same = 0;
751 return NULL;
754 if (fseek(f1, 0L, SEEK_SET) == -1)
755 return got_ferror(f1, GOT_ERR_IO);
756 if (fseek(f2, 0L, SEEK_SET) == -1)
757 return got_ferror(f2, GOT_ERR_IO);
759 return check_file_contents_equal(same, f1, f2);
762 /*
763 * Perform a 3-way merge where the file f_orig acts as the common
764 * ancestor, the file f_deriv acts as the first derived version,
765 * and the file f_deriv2 acts as the second derived version.
766 * The merge result will be written to a new file at ondisk_path; any
767 * existing file at this path will be replaced.
768 */
769 static const struct got_error *
770 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
771 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
772 const char *path, uint16_t st_mode,
773 const char *label_orig, const char *label_deriv, const char *label_deriv2,
774 enum got_diff_algorithm diff_algo, struct got_repository *repo,
775 got_worktree_checkout_cb progress_cb, void *progress_arg)
777 const struct got_error *err = NULL;
778 int merged_fd = -1;
779 FILE *f_merged = NULL;
780 char *merged_path = NULL, *base_path = NULL;
781 int overlapcnt = 0;
782 char *parent = NULL;
784 *local_changes_subsumed = 0;
786 err = got_path_dirname(&parent, ondisk_path);
787 if (err)
788 return err;
790 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
791 err = got_error_from_errno("asprintf");
792 goto done;
795 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
796 if (err)
797 goto done;
799 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
800 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
801 if (err)
802 goto done;
804 err = (*progress_cb)(progress_arg,
805 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
806 if (err)
807 goto done;
809 if (fsync(merged_fd) != 0) {
810 err = got_error_from_errno("fsync");
811 goto done;
814 f_merged = fdopen(merged_fd, "r");
815 if (f_merged == NULL) {
816 err = got_error_from_errno("fdopen");
817 goto done;
819 merged_fd = -1;
821 /* Check if a clean merge has subsumed all local changes. */
822 if (overlapcnt == 0) {
823 err = check_files_equal(local_changes_subsumed, f_deriv,
824 f_merged);
825 if (err)
826 goto done;
829 if (fchmod(fileno(f_merged), st_mode) != 0) {
830 err = got_error_from_errno2("fchmod", merged_path);
831 goto done;
834 if (rename(merged_path, ondisk_path) != 0) {
835 err = got_error_from_errno3("rename", merged_path,
836 ondisk_path);
837 goto done;
839 done:
840 if (err) {
841 if (merged_path)
842 unlink(merged_path);
844 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
845 err = got_error_from_errno("close");
846 if (f_merged && fclose(f_merged) == EOF && err == NULL)
847 err = got_error_from_errno("fclose");
848 free(merged_path);
849 free(base_path);
850 free(parent);
851 return err;
854 static const struct got_error *
855 update_symlink(const char *ondisk_path, const char *target_path,
856 size_t target_len)
858 /* This is not atomic but matches what 'ln -sf' does. */
859 if (unlink(ondisk_path) == -1)
860 return got_error_from_errno2("unlink", ondisk_path);
861 if (symlink(target_path, ondisk_path) == -1)
862 return got_error_from_errno3("symlink", target_path,
863 ondisk_path);
864 return NULL;
867 /*
868 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
869 * in the work tree with a file that contains conflict markers and the
870 * conflicting target paths of the original version, a "derived version"
871 * of a symlink from an incoming change, and a local version of the symlink.
873 * The original versions's target path can be NULL if it is not available,
874 * such as if both derived versions added a new symlink at the same path.
876 * The incoming derived symlink target is NULL in case the incoming change
877 * has deleted this symlink.
878 */
879 static const struct got_error *
880 install_symlink_conflict(const char *deriv_target,
881 struct got_object_id *deriv_base_commit_id, const char *orig_target,
882 const char *label_orig, const char *local_target, const char *ondisk_path)
884 const struct got_error *err;
885 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
886 FILE *f = NULL;
888 err = got_object_id_str(&id_str, deriv_base_commit_id);
889 if (err)
890 return got_error_from_errno("asprintf");
892 if (asprintf(&label_deriv, "%s: commit %s",
893 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
894 err = got_error_from_errno("asprintf");
895 goto done;
898 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
899 if (err)
900 goto done;
902 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
903 err = got_error_from_errno2("fchmod", path);
904 goto done;
907 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
908 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
909 deriv_target ? deriv_target : "(symlink was deleted)",
910 orig_target ? label_orig : "",
911 orig_target ? "\n" : "",
912 orig_target ? orig_target : "",
913 orig_target ? "\n" : "",
914 GOT_DIFF_CONFLICT_MARKER_SEP,
915 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
916 err = got_error_from_errno2("fprintf", path);
917 goto done;
920 if (unlink(ondisk_path) == -1) {
921 err = got_error_from_errno2("unlink", ondisk_path);
922 goto done;
924 if (rename(path, ondisk_path) == -1) {
925 err = got_error_from_errno3("rename", path, ondisk_path);
926 goto done;
928 done:
929 if (f != NULL && fclose(f) == EOF && err == NULL)
930 err = got_error_from_errno2("fclose", path);
931 free(path);
932 free(id_str);
933 free(label_deriv);
934 return err;
937 /* forward declaration */
938 static const struct got_error *
939 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
940 const char *, const char *, uint16_t, const char *,
941 struct got_blob_object *, struct got_object_id *,
942 struct got_repository *, got_worktree_checkout_cb, void *);
944 /*
945 * Merge a symlink into the work tree, where blob_orig acts as the common
946 * ancestor, deriv_target is the link target of the first derived version,
947 * and the symlink on disk acts as the second derived version.
948 * Assume that contents of both blobs represent symlinks.
949 */
950 static const struct got_error *
951 merge_symlink(struct got_worktree *worktree,
952 struct got_blob_object *blob_orig, const char *ondisk_path,
953 const char *path, const char *label_orig, const char *deriv_target,
954 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
955 got_worktree_checkout_cb progress_cb, void *progress_arg)
957 const struct got_error *err = NULL;
958 char *ancestor_target = NULL;
959 struct stat sb;
960 ssize_t ondisk_len, deriv_len;
961 char ondisk_target[PATH_MAX];
962 int have_local_change = 0;
963 int have_incoming_change = 0;
965 if (lstat(ondisk_path, &sb) == -1)
966 return got_error_from_errno2("lstat", ondisk_path);
968 ondisk_len = readlink(ondisk_path, ondisk_target,
969 sizeof(ondisk_target));
970 if (ondisk_len == -1) {
971 err = got_error_from_errno2("readlink",
972 ondisk_path);
973 goto done;
975 ondisk_target[ondisk_len] = '\0';
977 if (blob_orig) {
978 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
979 if (err)
980 goto done;
983 if (ancestor_target == NULL ||
984 (ondisk_len != strlen(ancestor_target) ||
985 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
986 have_local_change = 1;
988 deriv_len = strlen(deriv_target);
989 if (ancestor_target == NULL ||
990 (deriv_len != strlen(ancestor_target) ||
991 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
992 have_incoming_change = 1;
994 if (!have_local_change && !have_incoming_change) {
995 if (ancestor_target) {
996 /* Both sides made the same change. */
997 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
998 path);
999 } else if (deriv_len == ondisk_len &&
1000 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1001 /* Both sides added the same symlink. */
1002 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1003 path);
1004 } else {
1005 /* Both sides added symlinks which don't match. */
1006 err = install_symlink_conflict(deriv_target,
1007 deriv_base_commit_id, ancestor_target,
1008 label_orig, ondisk_target, ondisk_path);
1009 if (err)
1010 goto done;
1011 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1012 path);
1014 } else if (!have_local_change && have_incoming_change) {
1015 /* Apply the incoming change. */
1016 err = update_symlink(ondisk_path, deriv_target,
1017 strlen(deriv_target));
1018 if (err)
1019 goto done;
1020 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1021 } else if (have_local_change && have_incoming_change) {
1022 if (deriv_len == ondisk_len &&
1023 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1024 /* Both sides made the same change. */
1025 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1026 path);
1027 } else {
1028 err = install_symlink_conflict(deriv_target,
1029 deriv_base_commit_id, ancestor_target, label_orig,
1030 ondisk_target, ondisk_path);
1031 if (err)
1032 goto done;
1033 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1034 path);
1038 done:
1039 free(ancestor_target);
1040 return err;
1043 static const struct got_error *
1044 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
1046 const struct got_error *err = NULL;
1047 char target_path[PATH_MAX];
1048 ssize_t target_len;
1049 size_t n;
1050 FILE *f;
1052 *outfile = NULL;
1054 f = got_opentemp();
1055 if (f == NULL)
1056 return got_error_from_errno("got_opentemp");
1057 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
1058 if (target_len == -1) {
1059 err = got_error_from_errno2("readlink", ondisk_path);
1060 goto done;
1062 n = fwrite(target_path, 1, target_len, f);
1063 if (n != target_len) {
1064 err = got_ferror(f, GOT_ERR_IO);
1065 goto done;
1067 if (fflush(f) == EOF) {
1068 err = got_error_from_errno("fflush");
1069 goto done;
1071 if (fseek(f, 0L, SEEK_SET) == -1) {
1072 err = got_ferror(f, GOT_ERR_IO);
1073 goto done;
1075 done:
1076 if (err)
1077 fclose(f);
1078 else
1079 *outfile = f;
1080 return err;
1084 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1085 * blob_deriv acts as the first derived version, and the file on disk
1086 * acts as the second derived version.
1088 static const struct got_error *
1089 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1090 struct got_blob_object *blob_orig, const char *ondisk_path,
1091 const char *path, uint16_t st_mode, const char *label_orig,
1092 struct got_blob_object *blob_deriv,
1093 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1094 got_worktree_checkout_cb progress_cb, void *progress_arg)
1096 const struct got_error *err = NULL;
1097 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
1098 char *blob_orig_path = NULL;
1099 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1100 char *label_deriv = NULL, *parent = NULL;
1102 *local_changes_subsumed = 0;
1104 err = got_path_dirname(&parent, ondisk_path);
1105 if (err)
1106 return err;
1108 if (blob_orig) {
1109 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1110 parent) == -1) {
1111 err = got_error_from_errno("asprintf");
1112 base_path = NULL;
1113 goto done;
1116 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
1117 if (err)
1118 goto done;
1119 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1120 blob_orig);
1121 if (err)
1122 goto done;
1123 free(base_path);
1124 } else {
1126 * No common ancestor exists. This is an "add vs add" conflict
1127 * and we simply use an empty ancestor file to make both files
1128 * appear in the merged result in their entirety.
1130 f_orig = got_opentemp();
1131 if (f_orig == NULL) {
1132 err = got_error_from_errno("got_opentemp");
1133 goto done;
1137 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1138 err = got_error_from_errno("asprintf");
1139 base_path = NULL;
1140 goto done;
1143 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1144 if (err)
1145 goto done;
1146 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1147 blob_deriv);
1148 if (err)
1149 goto done;
1151 err = got_object_id_str(&id_str, deriv_base_commit_id);
1152 if (err)
1153 goto done;
1154 if (asprintf(&label_deriv, "%s: commit %s",
1155 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1156 err = got_error_from_errno("asprintf");
1157 goto done;
1161 * In order the run a 3-way merge with a symlink we copy the symlink's
1162 * target path into a temporary file and use that file with diff3.
1164 if (S_ISLNK(st_mode)) {
1165 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1166 if (err)
1167 goto done;
1168 } else {
1169 int fd;
1170 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
1171 if (fd == -1) {
1172 err = got_error_from_errno2("open", ondisk_path);
1173 goto done;
1175 f_deriv2 = fdopen(fd, "r");
1176 if (f_deriv2 == NULL) {
1177 err = got_error_from_errno2("fdopen", ondisk_path);
1178 close(fd);
1179 goto done;
1183 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1184 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1185 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1186 done:
1187 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1188 err = got_error_from_errno("fclose");
1189 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1190 err = got_error_from_errno("fclose");
1191 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1192 err = got_error_from_errno("fclose");
1193 free(base_path);
1194 if (blob_orig_path) {
1195 unlink(blob_orig_path);
1196 free(blob_orig_path);
1198 if (blob_deriv_path) {
1199 unlink(blob_deriv_path);
1200 free(blob_deriv_path);
1202 free(id_str);
1203 free(label_deriv);
1204 free(parent);
1205 return err;
1208 static const struct got_error *
1209 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1210 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1211 int wt_fd, const char *path, struct got_object_id *blob_id)
1213 const struct got_error *err = NULL;
1214 struct got_fileindex_entry *new_ie;
1216 *new_iep = NULL;
1218 err = got_fileindex_entry_alloc(&new_ie, path);
1219 if (err)
1220 return err;
1222 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1223 blob_id->sha1, base_commit_id->sha1, 1);
1224 if (err)
1225 goto done;
1227 err = got_fileindex_entry_add(fileindex, new_ie);
1228 done:
1229 if (err)
1230 got_fileindex_entry_free(new_ie);
1231 else
1232 *new_iep = new_ie;
1233 return err;
1236 static mode_t
1237 get_ondisk_perms(int executable, mode_t st_mode)
1239 mode_t xbits = S_IXUSR;
1241 if (executable) {
1242 /* Map read bits to execute bits. */
1243 if (st_mode & S_IRGRP)
1244 xbits |= S_IXGRP;
1245 if (st_mode & S_IROTH)
1246 xbits |= S_IXOTH;
1247 return st_mode | xbits;
1250 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1253 /* forward declaration */
1254 static const struct got_error *
1255 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1256 const char *path, mode_t te_mode, mode_t st_mode,
1257 struct got_blob_object *blob, int restoring_missing_file,
1258 int reverting_versioned_file, int installing_bad_symlink,
1259 int path_is_unversioned, struct got_repository *repo,
1260 got_worktree_checkout_cb progress_cb, void *progress_arg);
1263 * This function assumes that the provided symlink target points at a
1264 * safe location in the work tree!
1266 static const struct got_error *
1267 replace_existing_symlink(int *did_something, const char *ondisk_path,
1268 const char *target_path, size_t target_len)
1270 const struct got_error *err = NULL;
1271 ssize_t elen;
1272 char etarget[PATH_MAX];
1273 int fd;
1275 *did_something = 0;
1278 * "Bad" symlinks (those pointing outside the work tree or into the
1279 * .got directory) are installed in the work tree as a regular file
1280 * which contains the bad symlink target path.
1281 * The new symlink target has already been checked for safety by our
1282 * caller. If we can successfully open a regular file then we simply
1283 * replace this file with a symlink below.
1285 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1286 if (fd == -1) {
1287 if (!got_err_open_nofollow_on_symlink())
1288 return got_error_from_errno2("open", ondisk_path);
1290 /* We are updating an existing on-disk symlink. */
1291 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1292 if (elen == -1)
1293 return got_error_from_errno2("readlink", ondisk_path);
1295 if (elen == target_len &&
1296 memcmp(etarget, target_path, target_len) == 0)
1297 return NULL; /* nothing to do */
1300 *did_something = 1;
1301 err = update_symlink(ondisk_path, target_path, target_len);
1302 if (fd != -1 && close(fd) == -1 && err == NULL)
1303 err = got_error_from_errno2("close", ondisk_path);
1304 return err;
1307 static const struct got_error *
1308 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1309 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1311 const struct got_error *err = NULL;
1312 char canonpath[PATH_MAX];
1313 char *path_got = NULL;
1315 *is_bad_symlink = 0;
1317 if (target_len >= sizeof(canonpath)) {
1318 *is_bad_symlink = 1;
1319 return NULL;
1323 * We do not use realpath(3) to resolve the symlink's target
1324 * path because we don't want to resolve symlinks recursively.
1325 * Instead we make the path absolute and then canonicalize it.
1326 * Relative symlink target lookup should begin at the directory
1327 * in which the blob object is being installed.
1329 if (!got_path_is_absolute(target_path)) {
1330 char *abspath, *parent;
1331 err = got_path_dirname(&parent, ondisk_path);
1332 if (err)
1333 return err;
1334 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1335 free(parent);
1336 return got_error_from_errno("asprintf");
1338 free(parent);
1339 if (strlen(abspath) >= sizeof(canonpath)) {
1340 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1341 free(abspath);
1342 return err;
1344 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1345 free(abspath);
1346 if (err)
1347 return err;
1348 } else {
1349 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1350 if (err)
1351 return err;
1354 /* Only allow symlinks pointing at paths within the work tree. */
1355 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1356 *is_bad_symlink = 1;
1357 return NULL;
1360 /* Do not allow symlinks pointing into the .got directory. */
1361 if (asprintf(&path_got, "%s/%s", wtroot_path,
1362 GOT_WORKTREE_GOT_DIR) == -1)
1363 return got_error_from_errno("asprintf");
1364 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1365 *is_bad_symlink = 1;
1367 free(path_got);
1368 return NULL;
1371 static const struct got_error *
1372 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1373 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1374 int restoring_missing_file, int reverting_versioned_file,
1375 int path_is_unversioned, int allow_bad_symlinks,
1376 struct got_repository *repo,
1377 got_worktree_checkout_cb progress_cb, void *progress_arg)
1379 const struct got_error *err = NULL;
1380 char target_path[PATH_MAX];
1381 size_t len, target_len = 0;
1382 char *path_got = NULL;
1383 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1384 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1386 *is_bad_symlink = 0;
1389 * Blob object content specifies the target path of the link.
1390 * If a symbolic link cannot be installed we instead create
1391 * a regular file which contains the link target path stored
1392 * in the blob object.
1394 do {
1395 err = got_object_blob_read_block(&len, blob);
1396 if (len + target_len >= sizeof(target_path)) {
1397 /* Path too long; install as a regular file. */
1398 *is_bad_symlink = 1;
1399 got_object_blob_rewind(blob);
1400 return install_blob(worktree, ondisk_path, path,
1401 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1402 restoring_missing_file, reverting_versioned_file,
1403 1, path_is_unversioned, repo, progress_cb,
1404 progress_arg);
1406 if (len > 0) {
1407 /* Skip blob object header first time around. */
1408 memcpy(target_path + target_len, buf + hdrlen,
1409 len - hdrlen);
1410 target_len += len - hdrlen;
1411 hdrlen = 0;
1413 } while (len != 0);
1414 target_path[target_len] = '\0';
1416 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1417 ondisk_path, worktree->root_path);
1418 if (err)
1419 return err;
1421 if (*is_bad_symlink && !allow_bad_symlinks) {
1422 /* install as a regular file */
1423 got_object_blob_rewind(blob);
1424 err = install_blob(worktree, ondisk_path, path,
1425 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1426 restoring_missing_file, reverting_versioned_file, 1,
1427 path_is_unversioned, repo, progress_cb, progress_arg);
1428 goto done;
1431 if (symlink(target_path, ondisk_path) == -1) {
1432 if (errno == EEXIST) {
1433 int symlink_replaced;
1434 if (path_is_unversioned) {
1435 err = (*progress_cb)(progress_arg,
1436 GOT_STATUS_UNVERSIONED, path);
1437 goto done;
1439 err = replace_existing_symlink(&symlink_replaced,
1440 ondisk_path, target_path, target_len);
1441 if (err)
1442 goto done;
1443 if (progress_cb) {
1444 if (symlink_replaced) {
1445 err = (*progress_cb)(progress_arg,
1446 reverting_versioned_file ?
1447 GOT_STATUS_REVERT :
1448 GOT_STATUS_UPDATE, path);
1449 } else {
1450 err = (*progress_cb)(progress_arg,
1451 GOT_STATUS_EXISTS, path);
1454 goto done; /* Nothing else to do. */
1457 if (errno == ENOENT) {
1458 char *parent;
1459 err = got_path_dirname(&parent, ondisk_path);
1460 if (err)
1461 goto done;
1462 err = add_dir_on_disk(worktree, parent);
1463 free(parent);
1464 if (err)
1465 goto done;
1467 * Retry, and fall through to error handling
1468 * below if this second attempt fails.
1470 if (symlink(target_path, ondisk_path) != -1) {
1471 err = NULL; /* success */
1472 goto done;
1476 /* Handle errors from first or second creation attempt. */
1477 if (errno == ENAMETOOLONG) {
1478 /* bad target path; install as a regular file */
1479 *is_bad_symlink = 1;
1480 got_object_blob_rewind(blob);
1481 err = install_blob(worktree, ondisk_path, path,
1482 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1483 restoring_missing_file, reverting_versioned_file, 1,
1484 path_is_unversioned, repo,
1485 progress_cb, progress_arg);
1486 } else if (errno == ENOTDIR) {
1487 err = got_error_path(ondisk_path,
1488 GOT_ERR_FILE_OBSTRUCTED);
1489 } else {
1490 err = got_error_from_errno3("symlink",
1491 target_path, ondisk_path);
1493 } else if (progress_cb)
1494 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1495 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1496 done:
1497 free(path_got);
1498 return err;
1501 static const struct got_error *
1502 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1503 const char *path, mode_t te_mode, mode_t st_mode,
1504 struct got_blob_object *blob, int restoring_missing_file,
1505 int reverting_versioned_file, int installing_bad_symlink,
1506 int path_is_unversioned, struct got_repository *repo,
1507 got_worktree_checkout_cb progress_cb, void *progress_arg)
1509 const struct got_error *err = NULL;
1510 int fd = -1;
1511 size_t len, hdrlen;
1512 int update = 0;
1513 char *tmppath = NULL;
1515 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1516 GOT_DEFAULT_FILE_MODE);
1517 if (fd == -1) {
1518 if (errno == ENOENT) {
1519 char *parent;
1520 err = got_path_dirname(&parent, path);
1521 if (err)
1522 return err;
1523 err = add_dir_on_disk(worktree, parent);
1524 free(parent);
1525 if (err)
1526 return err;
1527 fd = open(ondisk_path,
1528 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1529 GOT_DEFAULT_FILE_MODE);
1530 if (fd == -1)
1531 return got_error_from_errno2("open",
1532 ondisk_path);
1533 } else if (errno == EEXIST) {
1534 if (path_is_unversioned) {
1535 err = (*progress_cb)(progress_arg,
1536 GOT_STATUS_UNVERSIONED, path);
1537 goto done;
1539 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1540 !S_ISREG(st_mode) && !installing_bad_symlink) {
1541 /* TODO file is obstructed; do something */
1542 err = got_error_path(ondisk_path,
1543 GOT_ERR_FILE_OBSTRUCTED);
1544 goto done;
1545 } else {
1546 err = got_opentemp_named_fd(&tmppath, &fd,
1547 ondisk_path);
1548 if (err)
1549 goto done;
1550 update = 1;
1552 } else
1553 return got_error_from_errno2("open", ondisk_path);
1556 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1557 err = got_error_from_errno2("fchmod",
1558 update ? tmppath : ondisk_path);
1559 goto done;
1562 if (progress_cb) {
1563 if (restoring_missing_file)
1564 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1565 path);
1566 else if (reverting_versioned_file)
1567 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1568 path);
1569 else
1570 err = (*progress_cb)(progress_arg,
1571 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1572 if (err)
1573 goto done;
1576 hdrlen = got_object_blob_get_hdrlen(blob);
1577 do {
1578 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1579 err = got_object_blob_read_block(&len, blob);
1580 if (err)
1581 break;
1582 if (len > 0) {
1583 /* Skip blob object header first time around. */
1584 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1585 if (outlen == -1) {
1586 err = got_error_from_errno("write");
1587 goto done;
1588 } else if (outlen != len - hdrlen) {
1589 err = got_error(GOT_ERR_IO);
1590 goto done;
1592 hdrlen = 0;
1594 } while (len != 0);
1596 if (fsync(fd) != 0) {
1597 err = got_error_from_errno("fsync");
1598 goto done;
1601 if (update) {
1602 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1603 err = got_error_from_errno2("unlink", ondisk_path);
1604 goto done;
1606 if (rename(tmppath, ondisk_path) != 0) {
1607 err = got_error_from_errno3("rename", tmppath,
1608 ondisk_path);
1609 goto done;
1611 free(tmppath);
1612 tmppath = NULL;
1615 done:
1616 if (fd != -1 && close(fd) == -1 && err == NULL)
1617 err = got_error_from_errno("close");
1618 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1619 err = got_error_from_errno2("unlink", tmppath);
1620 free(tmppath);
1621 return err;
1624 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1625 static const struct got_error *
1626 get_modified_file_content_status(unsigned char *status, FILE *f)
1628 const struct got_error *err = NULL;
1629 const char *markers[3] = {
1630 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1631 GOT_DIFF_CONFLICT_MARKER_SEP,
1632 GOT_DIFF_CONFLICT_MARKER_END
1634 int i = 0;
1635 char *line = NULL;
1636 size_t linesize = 0;
1637 ssize_t linelen;
1639 while (*status == GOT_STATUS_MODIFY) {
1640 linelen = getline(&line, &linesize, f);
1641 if (linelen == -1) {
1642 if (feof(f))
1643 break;
1644 err = got_ferror(f, GOT_ERR_IO);
1645 break;
1648 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1649 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1650 == 0)
1651 *status = GOT_STATUS_CONFLICT;
1652 else
1653 i++;
1656 free(line);
1658 return err;
1661 static int
1662 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1664 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1665 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1668 static int
1669 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1671 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1672 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1673 ie->mtime_sec == sb->st_mtim.tv_sec &&
1674 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1675 ie->size == (sb->st_size & 0xffffffff) &&
1676 !xbit_differs(ie, sb->st_mode));
1679 static unsigned char
1680 get_staged_status(struct got_fileindex_entry *ie)
1682 switch (got_fileindex_entry_stage_get(ie)) {
1683 case GOT_FILEIDX_STAGE_ADD:
1684 return GOT_STATUS_ADD;
1685 case GOT_FILEIDX_STAGE_DELETE:
1686 return GOT_STATUS_DELETE;
1687 case GOT_FILEIDX_STAGE_MODIFY:
1688 return GOT_STATUS_MODIFY;
1689 default:
1690 return GOT_STATUS_NO_CHANGE;
1694 static const struct got_error *
1695 get_symlink_modification_status(unsigned char *status,
1696 struct got_fileindex_entry *ie, const char *abspath,
1697 int dirfd, const char *de_name, struct got_blob_object *blob)
1699 const struct got_error *err = NULL;
1700 char target_path[PATH_MAX];
1701 char etarget[PATH_MAX];
1702 ssize_t elen;
1703 size_t len, target_len = 0;
1704 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1705 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1707 *status = GOT_STATUS_NO_CHANGE;
1709 /* Blob object content specifies the target path of the link. */
1710 do {
1711 err = got_object_blob_read_block(&len, blob);
1712 if (err)
1713 return err;
1714 if (len + target_len >= sizeof(target_path)) {
1716 * Should not happen. The blob contents were OK
1717 * when this symlink was installed.
1719 return got_error(GOT_ERR_NO_SPACE);
1721 if (len > 0) {
1722 /* Skip blob object header first time around. */
1723 memcpy(target_path + target_len, buf + hdrlen,
1724 len - hdrlen);
1725 target_len += len - hdrlen;
1726 hdrlen = 0;
1728 } while (len != 0);
1729 target_path[target_len] = '\0';
1731 if (dirfd != -1) {
1732 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1733 if (elen == -1)
1734 return got_error_from_errno2("readlinkat", abspath);
1735 } else {
1736 elen = readlink(abspath, etarget, sizeof(etarget));
1737 if (elen == -1)
1738 return got_error_from_errno2("readlink", abspath);
1741 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1742 *status = GOT_STATUS_MODIFY;
1744 return NULL;
1747 static const struct got_error *
1748 get_file_status(unsigned char *status, struct stat *sb,
1749 struct got_fileindex_entry *ie, const char *abspath,
1750 int dirfd, const char *de_name, struct got_repository *repo)
1752 const struct got_error *err = NULL;
1753 struct got_object_id id;
1754 size_t hdrlen;
1755 int fd = -1;
1756 FILE *f = NULL;
1757 uint8_t fbuf[8192];
1758 struct got_blob_object *blob = NULL;
1759 size_t flen, blen;
1760 unsigned char staged_status = get_staged_status(ie);
1762 *status = GOT_STATUS_NO_CHANGE;
1763 memset(sb, 0, sizeof(*sb));
1766 * Whenever the caller provides a directory descriptor and a
1767 * directory entry name for the file, use them! This prevents
1768 * race conditions if filesystem paths change beneath our feet.
1770 if (dirfd != -1) {
1771 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1772 if (errno == ENOENT) {
1773 if (got_fileindex_entry_has_file_on_disk(ie))
1774 *status = GOT_STATUS_MISSING;
1775 else
1776 *status = GOT_STATUS_DELETE;
1777 goto done;
1779 err = got_error_from_errno2("fstatat", abspath);
1780 goto done;
1782 } else {
1783 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1784 if (fd == -1 && errno != ENOENT &&
1785 !got_err_open_nofollow_on_symlink())
1786 return got_error_from_errno2("open", abspath);
1787 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1788 if (lstat(abspath, sb) == -1)
1789 return got_error_from_errno2("lstat", abspath);
1790 } else if (fd == -1 || fstat(fd, sb) == -1) {
1791 if (errno == ENOENT) {
1792 if (got_fileindex_entry_has_file_on_disk(ie))
1793 *status = GOT_STATUS_MISSING;
1794 else
1795 *status = GOT_STATUS_DELETE;
1796 goto done;
1798 err = got_error_from_errno2("fstat", abspath);
1799 goto done;
1803 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1804 *status = GOT_STATUS_OBSTRUCTED;
1805 goto done;
1808 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1809 *status = GOT_STATUS_DELETE;
1810 goto done;
1811 } else if (!got_fileindex_entry_has_blob(ie) &&
1812 staged_status != GOT_STATUS_ADD) {
1813 *status = GOT_STATUS_ADD;
1814 goto done;
1817 if (!stat_info_differs(ie, sb))
1818 goto done;
1820 if (S_ISLNK(sb->st_mode) &&
1821 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1822 *status = GOT_STATUS_MODIFY;
1823 goto done;
1826 if (staged_status == GOT_STATUS_MODIFY ||
1827 staged_status == GOT_STATUS_ADD)
1828 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1829 else
1830 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1832 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1833 if (err)
1834 goto done;
1836 if (S_ISLNK(sb->st_mode)) {
1837 err = get_symlink_modification_status(status, ie,
1838 abspath, dirfd, de_name, blob);
1839 goto done;
1842 if (dirfd != -1) {
1843 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1844 if (fd == -1) {
1845 err = got_error_from_errno2("openat", abspath);
1846 goto done;
1850 f = fdopen(fd, "r");
1851 if (f == NULL) {
1852 err = got_error_from_errno2("fdopen", abspath);
1853 goto done;
1855 fd = -1;
1856 hdrlen = got_object_blob_get_hdrlen(blob);
1857 for (;;) {
1858 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1859 err = got_object_blob_read_block(&blen, blob);
1860 if (err)
1861 goto done;
1862 /* Skip length of blob object header first time around. */
1863 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1864 if (flen == 0 && ferror(f)) {
1865 err = got_error_from_errno("fread");
1866 goto done;
1868 if (blen - hdrlen == 0) {
1869 if (flen != 0)
1870 *status = GOT_STATUS_MODIFY;
1871 break;
1872 } else if (flen == 0) {
1873 if (blen - hdrlen != 0)
1874 *status = GOT_STATUS_MODIFY;
1875 break;
1876 } else if (blen - hdrlen == flen) {
1877 /* Skip blob object header first time around. */
1878 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1879 *status = GOT_STATUS_MODIFY;
1880 break;
1882 } else {
1883 *status = GOT_STATUS_MODIFY;
1884 break;
1886 hdrlen = 0;
1889 if (*status == GOT_STATUS_MODIFY) {
1890 rewind(f);
1891 err = get_modified_file_content_status(status, f);
1892 } else if (xbit_differs(ie, sb->st_mode))
1893 *status = GOT_STATUS_MODE_CHANGE;
1894 done:
1895 if (blob)
1896 got_object_blob_close(blob);
1897 if (f != NULL && fclose(f) == EOF && err == NULL)
1898 err = got_error_from_errno2("fclose", abspath);
1899 if (fd != -1 && close(fd) == -1 && err == NULL)
1900 err = got_error_from_errno2("close", abspath);
1901 return err;
1905 * Update timestamps in the file index if a file is unmodified and
1906 * we had to run a full content comparison to find out.
1908 static const struct got_error *
1909 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1910 struct got_fileindex_entry *ie, struct stat *sb)
1912 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1913 return got_fileindex_entry_update(ie, wt_fd, path,
1914 ie->blob_sha1, ie->commit_sha1, 1);
1916 return NULL;
1919 static const struct got_error *
1920 update_blob(struct got_worktree *worktree,
1921 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1922 struct got_tree_entry *te, const char *path,
1923 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1924 void *progress_arg)
1926 const struct got_error *err = NULL;
1927 struct got_blob_object *blob = NULL;
1928 char *ondisk_path;
1929 unsigned char status = GOT_STATUS_NO_CHANGE;
1930 struct stat sb;
1932 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1933 return got_error_from_errno("asprintf");
1935 if (ie) {
1936 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1937 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1938 goto done;
1940 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1941 repo);
1942 if (err)
1943 goto done;
1944 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1945 sb.st_mode = got_fileindex_perms_to_st(ie);
1946 } else {
1947 if (stat(ondisk_path, &sb) == -1) {
1948 if (errno != ENOENT) {
1949 err = got_error_from_errno2("stat",
1950 ondisk_path);
1951 goto done;
1953 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1954 status = GOT_STATUS_UNVERSIONED;
1955 } else {
1956 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1957 status = GOT_STATUS_UNVERSIONED;
1958 else
1959 status = GOT_STATUS_OBSTRUCTED;
1963 if (status == GOT_STATUS_OBSTRUCTED) {
1964 if (ie)
1965 got_fileindex_entry_mark_skipped(ie);
1966 err = (*progress_cb)(progress_arg, status, path);
1967 goto done;
1969 if (status == GOT_STATUS_CONFLICT) {
1970 if (ie)
1971 got_fileindex_entry_mark_skipped(ie);
1972 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1973 path);
1974 goto done;
1977 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1978 (S_ISLNK(te->mode) ||
1979 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1981 * This is a regular file or an installed bad symlink.
1982 * If the file index indicates that this file is already
1983 * up-to-date with respect to the repository we can skip
1984 * updating contents of this file.
1986 if (got_fileindex_entry_has_commit(ie) &&
1987 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1988 SHA1_DIGEST_LENGTH) == 0) {
1989 /* Same commit. */
1990 err = sync_timestamps(worktree->root_fd,
1991 path, status, ie, &sb);
1992 if (err)
1993 goto done;
1994 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1995 path);
1996 goto done;
1998 if (got_fileindex_entry_has_blob(ie) &&
1999 memcmp(ie->blob_sha1, te->id.sha1,
2000 SHA1_DIGEST_LENGTH) == 0) {
2001 /* Different commit but the same blob. */
2002 err = sync_timestamps(worktree->root_fd,
2003 path, status, ie, &sb);
2004 if (err)
2005 goto done;
2006 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2007 path);
2008 goto done;
2012 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
2013 if (err)
2014 goto done;
2016 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2017 int update_timestamps;
2018 struct got_blob_object *blob2 = NULL;
2019 char *label_orig = NULL;
2020 if (got_fileindex_entry_has_blob(ie)) {
2021 struct got_object_id id2;
2022 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2023 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2024 if (err)
2025 goto done;
2027 if (got_fileindex_entry_has_commit(ie)) {
2028 char id_str[SHA1_DIGEST_STRING_LENGTH];
2029 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2030 sizeof(id_str)) == NULL) {
2031 err = got_error_path(id_str,
2032 GOT_ERR_BAD_OBJ_ID_STR);
2033 goto done;
2035 if (asprintf(&label_orig, "%s: commit %s",
2036 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2037 err = got_error_from_errno("asprintf");
2038 goto done;
2041 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2042 char *link_target;
2043 err = got_object_blob_read_to_str(&link_target, blob);
2044 if (err)
2045 goto done;
2046 err = merge_symlink(worktree, blob2, ondisk_path, path,
2047 label_orig, link_target, worktree->base_commit_id,
2048 repo, progress_cb, progress_arg);
2049 free(link_target);
2050 } else {
2051 err = merge_blob(&update_timestamps, worktree, blob2,
2052 ondisk_path, path, sb.st_mode, label_orig, blob,
2053 worktree->base_commit_id, repo,
2054 progress_cb, progress_arg);
2056 free(label_orig);
2057 if (blob2)
2058 got_object_blob_close(blob2);
2059 if (err)
2060 goto done;
2062 * Do not update timestamps of files with local changes.
2063 * Otherwise, a future status walk would treat them as
2064 * unmodified files again.
2066 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2067 blob->id.sha1, worktree->base_commit_id->sha1,
2068 update_timestamps);
2069 } else if (status == GOT_STATUS_MODE_CHANGE) {
2070 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2071 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2072 } else if (status == GOT_STATUS_DELETE) {
2073 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2074 if (err)
2075 goto done;
2076 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2077 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2078 if (err)
2079 goto done;
2080 } else {
2081 int is_bad_symlink = 0;
2082 if (S_ISLNK(te->mode)) {
2083 err = install_symlink(&is_bad_symlink, worktree,
2084 ondisk_path, path, blob,
2085 status == GOT_STATUS_MISSING, 0,
2086 status == GOT_STATUS_UNVERSIONED, 0,
2087 repo, progress_cb, progress_arg);
2088 } else {
2089 err = install_blob(worktree, ondisk_path, path,
2090 te->mode, sb.st_mode, blob,
2091 status == GOT_STATUS_MISSING, 0, 0,
2092 status == GOT_STATUS_UNVERSIONED, repo,
2093 progress_cb, progress_arg);
2095 if (err)
2096 goto done;
2098 if (ie) {
2099 err = got_fileindex_entry_update(ie,
2100 worktree->root_fd, path, blob->id.sha1,
2101 worktree->base_commit_id->sha1, 1);
2102 } else {
2103 err = create_fileindex_entry(&ie, fileindex,
2104 worktree->base_commit_id, worktree->root_fd, path,
2105 &blob->id);
2107 if (err)
2108 goto done;
2110 if (is_bad_symlink) {
2111 got_fileindex_entry_filetype_set(ie,
2112 GOT_FILEIDX_MODE_BAD_SYMLINK);
2115 got_object_blob_close(blob);
2116 done:
2117 free(ondisk_path);
2118 return err;
2121 static const struct got_error *
2122 remove_ondisk_file(const char *root_path, const char *path)
2124 const struct got_error *err = NULL;
2125 char *ondisk_path = NULL, *parent = NULL;
2127 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2128 return got_error_from_errno("asprintf");
2130 if (unlink(ondisk_path) == -1) {
2131 if (errno != ENOENT)
2132 err = got_error_from_errno2("unlink", ondisk_path);
2133 } else {
2134 size_t root_len = strlen(root_path);
2135 err = got_path_dirname(&parent, ondisk_path);
2136 if (err)
2137 goto done;
2138 while (got_path_cmp(parent, root_path,
2139 strlen(parent), root_len) != 0) {
2140 free(ondisk_path);
2141 ondisk_path = parent;
2142 parent = NULL;
2143 if (rmdir(ondisk_path) == -1) {
2144 if (errno != ENOTEMPTY)
2145 err = got_error_from_errno2("rmdir",
2146 ondisk_path);
2147 break;
2149 err = got_path_dirname(&parent, ondisk_path);
2150 if (err)
2151 break;
2154 done:
2155 free(ondisk_path);
2156 free(parent);
2157 return err;
2160 static const struct got_error *
2161 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2162 struct got_fileindex_entry *ie, struct got_repository *repo,
2163 got_worktree_checkout_cb progress_cb, void *progress_arg)
2165 const struct got_error *err = NULL;
2166 unsigned char status;
2167 struct stat sb;
2168 char *ondisk_path;
2170 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2171 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2173 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2174 == -1)
2175 return got_error_from_errno("asprintf");
2177 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2178 if (err)
2179 goto done;
2181 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2182 char ondisk_target[PATH_MAX];
2183 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2184 sizeof(ondisk_target));
2185 if (ondisk_len == -1) {
2186 err = got_error_from_errno2("readlink", ondisk_path);
2187 goto done;
2189 ondisk_target[ondisk_len] = '\0';
2190 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2191 NULL, NULL, /* XXX pass common ancestor info? */
2192 ondisk_target, ondisk_path);
2193 if (err)
2194 goto done;
2195 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2196 ie->path);
2197 goto done;
2200 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2201 status == GOT_STATUS_ADD) {
2202 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2203 if (err)
2204 goto done;
2206 * Preserve the working file and change the deleted blob's
2207 * entry into a schedule-add entry.
2209 err = got_fileindex_entry_update(ie, worktree->root_fd,
2210 ie->path, NULL, NULL, 0);
2211 } else {
2212 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2213 if (err)
2214 goto done;
2215 if (status == GOT_STATUS_NO_CHANGE) {
2216 err = remove_ondisk_file(worktree->root_path, ie->path);
2217 if (err)
2218 goto done;
2220 got_fileindex_entry_remove(fileindex, ie);
2222 done:
2223 free(ondisk_path);
2224 return err;
2227 struct diff_cb_arg {
2228 struct got_fileindex *fileindex;
2229 struct got_worktree *worktree;
2230 struct got_repository *repo;
2231 got_worktree_checkout_cb progress_cb;
2232 void *progress_arg;
2233 got_cancel_cb cancel_cb;
2234 void *cancel_arg;
2237 static const struct got_error *
2238 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2239 struct got_tree_entry *te, const char *parent_path)
2241 struct diff_cb_arg *a = arg;
2243 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2244 return got_error(GOT_ERR_CANCELLED);
2246 return update_blob(a->worktree, a->fileindex, ie, te,
2247 ie->path, a->repo, a->progress_cb, a->progress_arg);
2250 static const struct got_error *
2251 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2253 struct diff_cb_arg *a = arg;
2255 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2256 return got_error(GOT_ERR_CANCELLED);
2258 return delete_blob(a->worktree, a->fileindex, ie,
2259 a->repo, a->progress_cb, a->progress_arg);
2262 static const struct got_error *
2263 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2265 struct diff_cb_arg *a = arg;
2266 const struct got_error *err;
2267 char *path;
2269 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2270 return got_error(GOT_ERR_CANCELLED);
2272 if (got_object_tree_entry_is_submodule(te))
2273 return NULL;
2275 if (asprintf(&path, "%s%s%s", parent_path,
2276 parent_path[0] ? "/" : "", te->name)
2277 == -1)
2278 return got_error_from_errno("asprintf");
2280 if (S_ISDIR(te->mode))
2281 err = add_dir_on_disk(a->worktree, path);
2282 else
2283 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2284 a->repo, a->progress_cb, a->progress_arg);
2286 free(path);
2287 return err;
2290 const struct got_error *
2291 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2293 uint32_t uuid_status;
2295 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2296 if (uuid_status != uuid_s_ok) {
2297 *uuidstr = NULL;
2298 return got_error_uuid(uuid_status, "uuid_to_string");
2301 return NULL;
2304 static const struct got_error *
2305 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2307 const struct got_error *err = NULL;
2308 char *uuidstr = NULL;
2310 *refname = NULL;
2312 err = got_worktree_get_uuid(&uuidstr, worktree);
2313 if (err)
2314 return err;
2316 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2317 err = got_error_from_errno("asprintf");
2318 *refname = NULL;
2320 free(uuidstr);
2321 return err;
2324 const struct got_error *
2325 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2327 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2330 static const struct got_error *
2331 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2333 return get_ref_name(refname, worktree,
2334 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2337 static const struct got_error *
2338 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2340 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2343 static const struct got_error *
2344 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2346 return get_ref_name(refname, worktree,
2347 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2350 static const struct got_error *
2351 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2353 return get_ref_name(refname, worktree,
2354 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2357 static const struct got_error *
2358 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2360 return get_ref_name(refname, worktree,
2361 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2364 static const struct got_error *
2365 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2367 return get_ref_name(refname, worktree,
2368 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2371 static const struct got_error *
2372 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2374 return get_ref_name(refname, worktree,
2375 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2378 static const struct got_error *
2379 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2381 return get_ref_name(refname, worktree,
2382 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2385 const struct got_error *
2386 got_worktree_get_histedit_script_path(char **path,
2387 struct got_worktree *worktree)
2389 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2390 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2391 *path = NULL;
2392 return got_error_from_errno("asprintf");
2394 return NULL;
2397 static const struct got_error *
2398 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2400 return get_ref_name(refname, worktree,
2401 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2404 static const struct got_error *
2405 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2407 return get_ref_name(refname, worktree,
2408 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2412 * Prevent Git's garbage collector from deleting our base commit by
2413 * setting a reference to our base commit's ID.
2415 static const struct got_error *
2416 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2418 const struct got_error *err = NULL;
2419 struct got_reference *ref = NULL;
2420 char *refname;
2422 err = got_worktree_get_base_ref_name(&refname, worktree);
2423 if (err)
2424 return err;
2426 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2427 if (err)
2428 goto done;
2430 err = got_ref_write(ref, repo);
2431 done:
2432 free(refname);
2433 if (ref)
2434 got_ref_close(ref);
2435 return err;
2438 static const struct got_error *
2439 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2441 const struct got_error *err = NULL;
2443 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2444 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2445 err = got_error_from_errno("asprintf");
2446 *fileindex_path = NULL;
2448 return err;
2452 static const struct got_error *
2453 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2454 struct got_worktree *worktree)
2456 const struct got_error *err = NULL;
2457 FILE *index = NULL;
2459 *fileindex_path = NULL;
2460 *fileindex = got_fileindex_alloc();
2461 if (*fileindex == NULL)
2462 return got_error_from_errno("got_fileindex_alloc");
2464 err = get_fileindex_path(fileindex_path, worktree);
2465 if (err)
2466 goto done;
2468 index = fopen(*fileindex_path, "rb");
2469 if (index == NULL) {
2470 if (errno != ENOENT)
2471 err = got_error_from_errno2("fopen", *fileindex_path);
2472 } else {
2473 err = got_fileindex_read(*fileindex, index);
2474 if (fclose(index) == EOF && err == NULL)
2475 err = got_error_from_errno("fclose");
2477 done:
2478 if (err) {
2479 free(*fileindex_path);
2480 *fileindex_path = NULL;
2481 got_fileindex_free(*fileindex);
2482 *fileindex = NULL;
2484 return err;
2487 struct bump_base_commit_id_arg {
2488 struct got_object_id *base_commit_id;
2489 const char *path;
2490 size_t path_len;
2491 const char *entry_name;
2492 got_worktree_checkout_cb progress_cb;
2493 void *progress_arg;
2496 /* Bump base commit ID of all files within an updated part of the work tree. */
2497 static const struct got_error *
2498 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2500 const struct got_error *err;
2501 struct bump_base_commit_id_arg *a = arg;
2503 if (a->entry_name) {
2504 if (strcmp(ie->path, a->path) != 0)
2505 return NULL;
2506 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2507 return NULL;
2509 if (got_fileindex_entry_was_skipped(ie))
2510 return NULL;
2512 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2513 SHA1_DIGEST_LENGTH) == 0)
2514 return NULL;
2516 if (a->progress_cb) {
2517 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2518 ie->path);
2519 if (err)
2520 return err;
2522 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2523 return NULL;
2526 static const struct got_error *
2527 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2528 struct got_fileindex *fileindex,
2529 got_worktree_checkout_cb progress_cb, void *progress_arg)
2531 struct bump_base_commit_id_arg bbc_arg;
2533 bbc_arg.base_commit_id = worktree->base_commit_id;
2534 bbc_arg.entry_name = NULL;
2535 bbc_arg.path = "";
2536 bbc_arg.path_len = 0;
2537 bbc_arg.progress_cb = progress_cb;
2538 bbc_arg.progress_arg = progress_arg;
2540 return got_fileindex_for_each_entry_safe(fileindex,
2541 bump_base_commit_id, &bbc_arg);
2544 static const struct got_error *
2545 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2547 const struct got_error *err = NULL;
2548 char *new_fileindex_path = NULL;
2549 FILE *new_index = NULL;
2550 struct timespec timeout;
2552 err = got_opentemp_named(&new_fileindex_path, &new_index,
2553 fileindex_path);
2554 if (err)
2555 goto done;
2557 err = got_fileindex_write(fileindex, new_index);
2558 if (err)
2559 goto done;
2561 if (rename(new_fileindex_path, fileindex_path) != 0) {
2562 err = got_error_from_errno3("rename", new_fileindex_path,
2563 fileindex_path);
2564 unlink(new_fileindex_path);
2568 * Sleep for a short amount of time to ensure that files modified after
2569 * this program exits have a different time stamp from the one which
2570 * was recorded in the file index.
2572 timeout.tv_sec = 0;
2573 timeout.tv_nsec = 1;
2574 nanosleep(&timeout, NULL);
2575 done:
2576 if (new_index)
2577 fclose(new_index);
2578 free(new_fileindex_path);
2579 return err;
2582 static const struct got_error *
2583 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2584 struct got_object_id **tree_id, const char *wt_relpath,
2585 struct got_worktree *worktree, struct got_repository *repo)
2587 const struct got_error *err = NULL;
2588 struct got_object_id *id = NULL;
2589 char *in_repo_path = NULL;
2590 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2592 *entry_type = GOT_OBJ_TYPE_ANY;
2593 *tree_relpath = NULL;
2594 *tree_id = NULL;
2596 if (wt_relpath[0] == '\0') {
2597 /* Check out all files within the work tree. */
2598 *entry_type = GOT_OBJ_TYPE_TREE;
2599 *tree_relpath = strdup("");
2600 if (*tree_relpath == NULL) {
2601 err = got_error_from_errno("strdup");
2602 goto done;
2604 err = got_object_id_by_path(tree_id, repo,
2605 worktree->base_commit_id, worktree->path_prefix);
2606 if (err)
2607 goto done;
2608 return NULL;
2611 /* Check out a subset of files in the work tree. */
2613 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2614 is_root_wt ? "" : "/", wt_relpath) == -1) {
2615 err = got_error_from_errno("asprintf");
2616 goto done;
2619 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2620 in_repo_path);
2621 if (err)
2622 goto done;
2624 free(in_repo_path);
2625 in_repo_path = NULL;
2627 err = got_object_get_type(entry_type, repo, id);
2628 if (err)
2629 goto done;
2631 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2632 /* Check out a single file. */
2633 if (strchr(wt_relpath, '/') == NULL) {
2634 /* Check out a single file in work tree's root dir. */
2635 in_repo_path = strdup(worktree->path_prefix);
2636 if (in_repo_path == NULL) {
2637 err = got_error_from_errno("strdup");
2638 goto done;
2640 *tree_relpath = strdup("");
2641 if (*tree_relpath == NULL) {
2642 err = got_error_from_errno("strdup");
2643 goto done;
2645 } else {
2646 /* Check out a single file in a subdirectory. */
2647 err = got_path_dirname(tree_relpath, wt_relpath);
2648 if (err)
2649 return err;
2650 if (asprintf(&in_repo_path, "%s%s%s",
2651 worktree->path_prefix, is_root_wt ? "" : "/",
2652 *tree_relpath) == -1) {
2653 err = got_error_from_errno("asprintf");
2654 goto done;
2657 err = got_object_id_by_path(tree_id, repo,
2658 worktree->base_commit_id, in_repo_path);
2659 } else {
2660 /* Check out all files within a subdirectory. */
2661 *tree_id = got_object_id_dup(id);
2662 if (*tree_id == NULL) {
2663 err = got_error_from_errno("got_object_id_dup");
2664 goto done;
2666 *tree_relpath = strdup(wt_relpath);
2667 if (*tree_relpath == NULL) {
2668 err = got_error_from_errno("strdup");
2669 goto done;
2672 done:
2673 free(id);
2674 free(in_repo_path);
2675 if (err) {
2676 *entry_type = GOT_OBJ_TYPE_ANY;
2677 free(*tree_relpath);
2678 *tree_relpath = NULL;
2679 free(*tree_id);
2680 *tree_id = NULL;
2682 return err;
2685 static const struct got_error *
2686 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2687 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2688 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2689 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2691 const struct got_error *err = NULL;
2692 struct got_commit_object *commit = NULL;
2693 struct got_tree_object *tree = NULL;
2694 struct got_fileindex_diff_tree_cb diff_cb;
2695 struct diff_cb_arg arg;
2697 err = ref_base_commit(worktree, repo);
2698 if (err) {
2699 if (!(err->code == GOT_ERR_ERRNO &&
2700 (errno == EACCES || errno == EROFS)))
2701 goto done;
2702 err = (*progress_cb)(progress_arg,
2703 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2704 if (err)
2705 return err;
2708 err = got_object_open_as_commit(&commit, repo,
2709 worktree->base_commit_id);
2710 if (err)
2711 goto done;
2713 err = got_object_open_as_tree(&tree, repo, tree_id);
2714 if (err)
2715 goto done;
2717 if (entry_name &&
2718 got_object_tree_find_entry(tree, entry_name) == NULL) {
2719 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2720 goto done;
2723 diff_cb.diff_old_new = diff_old_new;
2724 diff_cb.diff_old = diff_old;
2725 diff_cb.diff_new = diff_new;
2726 arg.fileindex = fileindex;
2727 arg.worktree = worktree;
2728 arg.repo = repo;
2729 arg.progress_cb = progress_cb;
2730 arg.progress_arg = progress_arg;
2731 arg.cancel_cb = cancel_cb;
2732 arg.cancel_arg = cancel_arg;
2733 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2734 entry_name, repo, &diff_cb, &arg);
2735 done:
2736 if (tree)
2737 got_object_tree_close(tree);
2738 if (commit)
2739 got_object_commit_close(commit);
2740 return err;
2743 const struct got_error *
2744 got_worktree_checkout_files(struct got_worktree *worktree,
2745 struct got_pathlist_head *paths, struct got_repository *repo,
2746 got_worktree_checkout_cb progress_cb, void *progress_arg,
2747 got_cancel_cb cancel_cb, void *cancel_arg)
2749 const struct got_error *err = NULL, *sync_err, *unlockerr;
2750 struct got_commit_object *commit = NULL;
2751 struct got_tree_object *tree = NULL;
2752 struct got_fileindex *fileindex = NULL;
2753 char *fileindex_path = NULL;
2754 struct got_pathlist_entry *pe;
2755 struct tree_path_data {
2756 STAILQ_ENTRY(tree_path_data) entry;
2757 struct got_object_id *tree_id;
2758 int entry_type;
2759 char *relpath;
2760 char *entry_name;
2761 } *tpd = NULL;
2762 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2764 STAILQ_INIT(&tree_paths);
2766 err = lock_worktree(worktree, LOCK_EX);
2767 if (err)
2768 return err;
2770 /* Map all specified paths to in-repository trees. */
2771 TAILQ_FOREACH(pe, paths, entry) {
2772 tpd = malloc(sizeof(*tpd));
2773 if (tpd == NULL) {
2774 err = got_error_from_errno("malloc");
2775 goto done;
2778 err = find_tree_entry_for_checkout(&tpd->entry_type,
2779 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2780 if (err) {
2781 free(tpd);
2782 goto done;
2785 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2786 err = got_path_basename(&tpd->entry_name, pe->path);
2787 if (err) {
2788 free(tpd->relpath);
2789 free(tpd->tree_id);
2790 free(tpd);
2791 goto done;
2793 } else
2794 tpd->entry_name = NULL;
2796 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2800 * Read the file index.
2801 * Checking out files is supposed to be an idempotent operation.
2802 * If the on-disk file index is incomplete we will try to complete it.
2804 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2805 if (err)
2806 goto done;
2808 tpd = STAILQ_FIRST(&tree_paths);
2809 TAILQ_FOREACH(pe, paths, entry) {
2810 struct bump_base_commit_id_arg bbc_arg;
2812 err = checkout_files(worktree, fileindex, tpd->relpath,
2813 tpd->tree_id, tpd->entry_name, repo,
2814 progress_cb, progress_arg, cancel_cb, cancel_arg);
2815 if (err)
2816 break;
2818 bbc_arg.base_commit_id = worktree->base_commit_id;
2819 bbc_arg.entry_name = tpd->entry_name;
2820 bbc_arg.path = pe->path;
2821 bbc_arg.path_len = pe->path_len;
2822 bbc_arg.progress_cb = progress_cb;
2823 bbc_arg.progress_arg = progress_arg;
2824 err = got_fileindex_for_each_entry_safe(fileindex,
2825 bump_base_commit_id, &bbc_arg);
2826 if (err)
2827 break;
2829 tpd = STAILQ_NEXT(tpd, entry);
2831 sync_err = sync_fileindex(fileindex, fileindex_path);
2832 if (sync_err && err == NULL)
2833 err = sync_err;
2834 done:
2835 free(fileindex_path);
2836 if (tree)
2837 got_object_tree_close(tree);
2838 if (commit)
2839 got_object_commit_close(commit);
2840 if (fileindex)
2841 got_fileindex_free(fileindex);
2842 while (!STAILQ_EMPTY(&tree_paths)) {
2843 tpd = STAILQ_FIRST(&tree_paths);
2844 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2845 free(tpd->relpath);
2846 free(tpd->tree_id);
2847 free(tpd);
2849 unlockerr = lock_worktree(worktree, LOCK_SH);
2850 if (unlockerr && err == NULL)
2851 err = unlockerr;
2852 return err;
2855 struct merge_file_cb_arg {
2856 struct got_worktree *worktree;
2857 struct got_fileindex *fileindex;
2858 got_worktree_checkout_cb progress_cb;
2859 void *progress_arg;
2860 got_cancel_cb cancel_cb;
2861 void *cancel_arg;
2862 const char *label_orig;
2863 struct got_object_id *commit_id2;
2864 int allow_bad_symlinks;
2867 static const struct got_error *
2868 merge_file_cb(void *arg, struct got_blob_object *blob1,
2869 struct got_blob_object *blob2, struct got_object_id *id1,
2870 struct got_object_id *id2, const char *path1, const char *path2,
2871 mode_t mode1, mode_t mode2, struct got_repository *repo)
2873 static const struct got_error *err = NULL;
2874 struct merge_file_cb_arg *a = arg;
2875 struct got_fileindex_entry *ie;
2876 char *ondisk_path = NULL;
2877 struct stat sb;
2878 unsigned char status;
2879 int local_changes_subsumed;
2880 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2881 char *id_str = NULL, *label_deriv2 = NULL;
2883 if (blob1 && blob2) {
2884 ie = got_fileindex_entry_get(a->fileindex, path2,
2885 strlen(path2));
2886 if (ie == NULL)
2887 return (*a->progress_cb)(a->progress_arg,
2888 GOT_STATUS_MISSING, path2);
2890 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2891 path2) == -1)
2892 return got_error_from_errno("asprintf");
2894 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2895 repo);
2896 if (err)
2897 goto done;
2899 if (status == GOT_STATUS_DELETE) {
2900 err = (*a->progress_cb)(a->progress_arg,
2901 GOT_STATUS_MERGE, path2);
2902 goto done;
2904 if (status != GOT_STATUS_NO_CHANGE &&
2905 status != GOT_STATUS_MODIFY &&
2906 status != GOT_STATUS_CONFLICT &&
2907 status != GOT_STATUS_ADD) {
2908 err = (*a->progress_cb)(a->progress_arg, status, path2);
2909 goto done;
2912 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2913 char *link_target2;
2914 err = got_object_blob_read_to_str(&link_target2, blob2);
2915 if (err)
2916 goto done;
2917 err = merge_symlink(a->worktree, blob1, ondisk_path,
2918 path2, a->label_orig, link_target2, a->commit_id2,
2919 repo, a->progress_cb, a->progress_arg);
2920 free(link_target2);
2921 } else {
2922 int fd;
2924 f_orig = got_opentemp();
2925 if (f_orig == NULL) {
2926 err = got_error_from_errno("got_opentemp");
2927 goto done;
2929 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2930 f_orig, blob1);
2931 if (err)
2932 goto done;
2934 f_deriv2 = got_opentemp();
2935 if (f_deriv2 == NULL)
2936 goto done;
2937 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2938 f_deriv2, blob2);
2939 if (err)
2940 goto done;
2942 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2943 if (fd == -1) {
2944 err = got_error_from_errno2("open",
2945 ondisk_path);
2946 goto done;
2948 f_deriv = fdopen(fd, "r");
2949 if (f_deriv == NULL) {
2950 err = got_error_from_errno2("fdopen",
2951 ondisk_path);
2952 close(fd);
2953 goto done;
2955 err = got_object_id_str(&id_str, a->commit_id2);
2956 if (err)
2957 goto done;
2958 if (asprintf(&label_deriv2, "%s: commit %s",
2959 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2960 err = got_error_from_errno("asprintf");
2961 goto done;
2963 err = merge_file(&local_changes_subsumed, a->worktree,
2964 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2965 sb.st_mode, a->label_orig, NULL, label_deriv2,
2966 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2967 a->progress_cb, a->progress_arg);
2969 } else if (blob1) {
2970 ie = got_fileindex_entry_get(a->fileindex, path1,
2971 strlen(path1));
2972 if (ie == NULL)
2973 return (*a->progress_cb)(a->progress_arg,
2974 GOT_STATUS_MISSING, path1);
2976 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2977 path1) == -1)
2978 return got_error_from_errno("asprintf");
2980 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2981 repo);
2982 if (err)
2983 goto done;
2985 switch (status) {
2986 case GOT_STATUS_NO_CHANGE:
2987 err = (*a->progress_cb)(a->progress_arg,
2988 GOT_STATUS_DELETE, path1);
2989 if (err)
2990 goto done;
2991 err = remove_ondisk_file(a->worktree->root_path, path1);
2992 if (err)
2993 goto done;
2994 if (ie)
2995 got_fileindex_entry_mark_deleted_from_disk(ie);
2996 break;
2997 case GOT_STATUS_DELETE:
2998 case GOT_STATUS_MISSING:
2999 err = (*a->progress_cb)(a->progress_arg,
3000 GOT_STATUS_DELETE, path1);
3001 if (err)
3002 goto done;
3003 if (ie)
3004 got_fileindex_entry_mark_deleted_from_disk(ie);
3005 break;
3006 case GOT_STATUS_ADD: {
3007 struct got_object_id *id;
3008 FILE *blob1_f;
3010 * Delete the added file only if its content already
3011 * exists in the repository.
3013 err = got_object_blob_file_create(&id, &blob1_f, path1);
3014 if (err)
3015 goto done;
3016 if (got_object_id_cmp(id, id1) == 0) {
3017 err = (*a->progress_cb)(a->progress_arg,
3018 GOT_STATUS_DELETE, path1);
3019 if (err)
3020 goto done;
3021 err = remove_ondisk_file(a->worktree->root_path,
3022 path1);
3023 if (err)
3024 goto done;
3025 if (ie)
3026 got_fileindex_entry_remove(a->fileindex,
3027 ie);
3028 } else {
3029 err = (*a->progress_cb)(a->progress_arg,
3030 GOT_STATUS_CANNOT_DELETE, path1);
3032 if (fclose(blob1_f) == EOF && err == NULL)
3033 err = got_error_from_errno("fclose");
3034 free(id);
3035 if (err)
3036 goto done;
3037 break;
3039 case GOT_STATUS_MODIFY:
3040 case GOT_STATUS_CONFLICT:
3041 err = (*a->progress_cb)(a->progress_arg,
3042 GOT_STATUS_CANNOT_DELETE, path1);
3043 if (err)
3044 goto done;
3045 break;
3046 case GOT_STATUS_OBSTRUCTED:
3047 err = (*a->progress_cb)(a->progress_arg, status, path1);
3048 if (err)
3049 goto done;
3050 break;
3051 default:
3052 break;
3054 } else if (blob2) {
3055 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3056 path2) == -1)
3057 return got_error_from_errno("asprintf");
3058 ie = got_fileindex_entry_get(a->fileindex, path2,
3059 strlen(path2));
3060 if (ie) {
3061 err = get_file_status(&status, &sb, ie, ondisk_path,
3062 -1, NULL, repo);
3063 if (err)
3064 goto done;
3065 if (status != GOT_STATUS_NO_CHANGE &&
3066 status != GOT_STATUS_MODIFY &&
3067 status != GOT_STATUS_CONFLICT &&
3068 status != GOT_STATUS_ADD) {
3069 err = (*a->progress_cb)(a->progress_arg,
3070 status, path2);
3071 goto done;
3073 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3074 char *link_target2;
3075 err = got_object_blob_read_to_str(&link_target2,
3076 blob2);
3077 if (err)
3078 goto done;
3079 err = merge_symlink(a->worktree, NULL,
3080 ondisk_path, path2, a->label_orig,
3081 link_target2, a->commit_id2, repo,
3082 a->progress_cb, a->progress_arg);
3083 free(link_target2);
3084 } else if (S_ISREG(sb.st_mode)) {
3085 err = merge_blob(&local_changes_subsumed,
3086 a->worktree, NULL, ondisk_path, path2,
3087 sb.st_mode, a->label_orig, blob2,
3088 a->commit_id2, repo, a->progress_cb,
3089 a->progress_arg);
3090 } else {
3091 err = got_error_path(ondisk_path,
3092 GOT_ERR_FILE_OBSTRUCTED);
3094 if (err)
3095 goto done;
3096 if (status == GOT_STATUS_DELETE) {
3097 err = got_fileindex_entry_update(ie,
3098 a->worktree->root_fd, path2, blob2->id.sha1,
3099 a->worktree->base_commit_id->sha1, 0);
3100 if (err)
3101 goto done;
3103 } else {
3104 int is_bad_symlink = 0;
3105 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3106 if (S_ISLNK(mode2)) {
3107 err = install_symlink(&is_bad_symlink,
3108 a->worktree, ondisk_path, path2, blob2, 0,
3109 0, 1, a->allow_bad_symlinks, repo,
3110 a->progress_cb, a->progress_arg);
3111 } else {
3112 err = install_blob(a->worktree, ondisk_path, path2,
3113 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3114 a->progress_cb, a->progress_arg);
3116 if (err)
3117 goto done;
3118 err = got_fileindex_entry_alloc(&ie, path2);
3119 if (err)
3120 goto done;
3121 err = got_fileindex_entry_update(ie,
3122 a->worktree->root_fd, path2, NULL, NULL, 1);
3123 if (err) {
3124 got_fileindex_entry_free(ie);
3125 goto done;
3127 err = got_fileindex_entry_add(a->fileindex, ie);
3128 if (err) {
3129 got_fileindex_entry_free(ie);
3130 goto done;
3132 if (is_bad_symlink) {
3133 got_fileindex_entry_filetype_set(ie,
3134 GOT_FILEIDX_MODE_BAD_SYMLINK);
3138 done:
3139 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3140 err = got_error_from_errno("fclose");
3141 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3142 err = got_error_from_errno("fclose");
3143 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3144 err = got_error_from_errno("fclose");
3145 free(id_str);
3146 free(label_deriv2);
3147 free(ondisk_path);
3148 return err;
3151 static const struct got_error *
3152 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3154 struct got_worktree *worktree = arg;
3156 /* Reject merges into a work tree with mixed base commits. */
3157 if (got_fileindex_entry_has_commit(ie) &&
3158 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3159 SHA1_DIGEST_LENGTH) != 0)
3160 return got_error(GOT_ERR_MIXED_COMMITS);
3162 return NULL;
3165 struct check_merge_conflicts_arg {
3166 struct got_worktree *worktree;
3167 struct got_fileindex *fileindex;
3168 struct got_repository *repo;
3171 static const struct got_error *
3172 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3173 struct got_blob_object *blob2, struct got_object_id *id1,
3174 struct got_object_id *id2, const char *path1, const char *path2,
3175 mode_t mode1, mode_t mode2, struct got_repository *repo)
3177 const struct got_error *err = NULL;
3178 struct check_merge_conflicts_arg *a = arg;
3179 unsigned char status;
3180 struct stat sb;
3181 struct got_fileindex_entry *ie;
3182 const char *path = path2 ? path2 : path1;
3183 struct got_object_id *id = id2 ? id2 : id1;
3184 char *ondisk_path;
3186 if (id == NULL)
3187 return NULL;
3189 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3190 if (ie == NULL)
3191 return NULL;
3193 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3194 == -1)
3195 return got_error_from_errno("asprintf");
3197 /* Reject merges into a work tree with conflicted files. */
3198 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3199 free(ondisk_path);
3200 if (err)
3201 return err;
3202 if (status == GOT_STATUS_CONFLICT)
3203 return got_error(GOT_ERR_CONFLICTS);
3205 return NULL;
3208 static const struct got_error *
3209 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3210 const char *fileindex_path, struct got_object_id *commit_id1,
3211 struct got_object_id *commit_id2, struct got_repository *repo,
3212 got_worktree_checkout_cb progress_cb, void *progress_arg,
3213 got_cancel_cb cancel_cb, void *cancel_arg)
3215 const struct got_error *err = NULL, *sync_err;
3216 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3217 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3218 struct check_merge_conflicts_arg cmc_arg;
3219 struct merge_file_cb_arg arg;
3220 char *label_orig = NULL;
3222 if (commit_id1) {
3223 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3224 worktree->path_prefix);
3225 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3226 goto done;
3228 if (tree_id1) {
3229 char *id_str;
3231 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3232 if (err)
3233 goto done;
3235 err = got_object_id_str(&id_str, commit_id1);
3236 if (err)
3237 goto done;
3239 if (asprintf(&label_orig, "%s: commit %s",
3240 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3241 err = got_error_from_errno("asprintf");
3242 free(id_str);
3243 goto done;
3245 free(id_str);
3248 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3249 worktree->path_prefix);
3250 if (err)
3251 goto done;
3253 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3254 if (err)
3255 goto done;
3257 cmc_arg.worktree = worktree;
3258 cmc_arg.fileindex = fileindex;
3259 cmc_arg.repo = repo;
3260 err = got_diff_tree(tree1, tree2, "", "", repo,
3261 check_merge_conflicts, &cmc_arg, 0);
3262 if (err)
3263 goto done;
3265 arg.worktree = worktree;
3266 arg.fileindex = fileindex;
3267 arg.progress_cb = progress_cb;
3268 arg.progress_arg = progress_arg;
3269 arg.cancel_cb = cancel_cb;
3270 arg.cancel_arg = cancel_arg;
3271 arg.label_orig = label_orig;
3272 arg.commit_id2 = commit_id2;
3273 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3274 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3275 sync_err = sync_fileindex(fileindex, fileindex_path);
3276 if (sync_err && err == NULL)
3277 err = sync_err;
3278 done:
3279 if (tree1)
3280 got_object_tree_close(tree1);
3281 if (tree2)
3282 got_object_tree_close(tree2);
3283 free(label_orig);
3284 return err;
3287 const struct got_error *
3288 got_worktree_merge_files(struct got_worktree *worktree,
3289 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3290 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3291 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3293 const struct got_error *err, *unlockerr;
3294 char *fileindex_path = NULL;
3295 struct got_fileindex *fileindex = NULL;
3297 err = lock_worktree(worktree, LOCK_EX);
3298 if (err)
3299 return err;
3301 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3302 if (err)
3303 goto done;
3305 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3306 worktree);
3307 if (err)
3308 goto done;
3310 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3311 commit_id2, repo, progress_cb, progress_arg,
3312 cancel_cb, cancel_arg);
3313 done:
3314 if (fileindex)
3315 got_fileindex_free(fileindex);
3316 free(fileindex_path);
3317 unlockerr = lock_worktree(worktree, LOCK_SH);
3318 if (unlockerr && err == NULL)
3319 err = unlockerr;
3320 return err;
3323 struct diff_dir_cb_arg {
3324 struct got_fileindex *fileindex;
3325 struct got_worktree *worktree;
3326 const char *status_path;
3327 size_t status_path_len;
3328 struct got_repository *repo;
3329 got_worktree_status_cb status_cb;
3330 void *status_arg;
3331 got_cancel_cb cancel_cb;
3332 void *cancel_arg;
3333 /* A pathlist containing per-directory pathlists of ignore patterns. */
3334 struct got_pathlist_head *ignores;
3335 int report_unchanged;
3336 int no_ignores;
3339 static const struct got_error *
3340 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3341 int dirfd, const char *de_name,
3342 got_worktree_status_cb status_cb, void *status_arg,
3343 struct got_repository *repo, int report_unchanged)
3345 const struct got_error *err = NULL;
3346 unsigned char status = GOT_STATUS_NO_CHANGE;
3347 unsigned char staged_status = get_staged_status(ie);
3348 struct stat sb;
3349 struct got_object_id blob_id, commit_id, staged_blob_id;
3350 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3351 struct got_object_id *staged_blob_idp = NULL;
3353 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3354 if (err)
3355 return err;
3357 if (status == GOT_STATUS_NO_CHANGE &&
3358 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3359 return NULL;
3361 if (got_fileindex_entry_has_blob(ie)) {
3362 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3363 blob_idp = &blob_id;
3365 if (got_fileindex_entry_has_commit(ie)) {
3366 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3367 commit_idp = &commit_id;
3369 if (staged_status == GOT_STATUS_ADD ||
3370 staged_status == GOT_STATUS_MODIFY) {
3371 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3372 SHA1_DIGEST_LENGTH);
3373 staged_blob_idp = &staged_blob_id;
3376 return (*status_cb)(status_arg, status, staged_status,
3377 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3380 static const struct got_error *
3381 status_old_new(void *arg, struct got_fileindex_entry *ie,
3382 struct dirent *de, const char *parent_path, int dirfd)
3384 const struct got_error *err = NULL;
3385 struct diff_dir_cb_arg *a = arg;
3386 char *abspath;
3388 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3389 return got_error(GOT_ERR_CANCELLED);
3391 if (got_path_cmp(parent_path, a->status_path,
3392 strlen(parent_path), a->status_path_len) != 0 &&
3393 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3394 return NULL;
3396 if (parent_path[0]) {
3397 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3398 parent_path, de->d_name) == -1)
3399 return got_error_from_errno("asprintf");
3400 } else {
3401 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3402 de->d_name) == -1)
3403 return got_error_from_errno("asprintf");
3406 err = report_file_status(ie, abspath, dirfd, de->d_name,
3407 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3408 free(abspath);
3409 return err;
3412 static const struct got_error *
3413 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3415 struct diff_dir_cb_arg *a = arg;
3416 struct got_object_id blob_id, commit_id;
3417 unsigned char status;
3419 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3420 return got_error(GOT_ERR_CANCELLED);
3422 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3423 return NULL;
3425 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3426 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3427 if (got_fileindex_entry_has_file_on_disk(ie))
3428 status = GOT_STATUS_MISSING;
3429 else
3430 status = GOT_STATUS_DELETE;
3431 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3432 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3435 void
3436 free_ignorelist(struct got_pathlist_head *ignorelist)
3438 struct got_pathlist_entry *pe;
3440 TAILQ_FOREACH(pe, ignorelist, entry)
3441 free((char *)pe->path);
3442 got_pathlist_free(ignorelist);
3445 void
3446 free_ignores(struct got_pathlist_head *ignores)
3448 struct got_pathlist_entry *pe;
3450 TAILQ_FOREACH(pe, ignores, entry) {
3451 struct got_pathlist_head *ignorelist = pe->data;
3452 free_ignorelist(ignorelist);
3453 free((char *)pe->path);
3455 got_pathlist_free(ignores);
3458 static const struct got_error *
3459 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3461 const struct got_error *err = NULL;
3462 struct got_pathlist_entry *pe = NULL;
3463 struct got_pathlist_head *ignorelist;
3464 char *line = NULL, *pattern, *dirpath = NULL;
3465 size_t linesize = 0;
3466 ssize_t linelen;
3468 ignorelist = calloc(1, sizeof(*ignorelist));
3469 if (ignorelist == NULL)
3470 return got_error_from_errno("calloc");
3471 TAILQ_INIT(ignorelist);
3473 while ((linelen = getline(&line, &linesize, f)) != -1) {
3474 if (linelen > 0 && line[linelen - 1] == '\n')
3475 line[linelen - 1] = '\0';
3477 /* Git's ignores may contain comments. */
3478 if (line[0] == '#')
3479 continue;
3481 /* Git's negated patterns are not (yet?) supported. */
3482 if (line[0] == '!')
3483 continue;
3485 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3486 line) == -1) {
3487 err = got_error_from_errno("asprintf");
3488 goto done;
3490 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3491 if (err)
3492 goto done;
3494 if (ferror(f)) {
3495 err = got_error_from_errno("getline");
3496 goto done;
3499 dirpath = strdup(path);
3500 if (dirpath == NULL) {
3501 err = got_error_from_errno("strdup");
3502 goto done;
3504 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3505 done:
3506 free(line);
3507 if (err || pe == NULL) {
3508 free(dirpath);
3509 free_ignorelist(ignorelist);
3511 return err;
3514 int
3515 match_ignores(struct got_pathlist_head *ignores, const char *path)
3517 struct got_pathlist_entry *pe;
3519 /* Handle patterns which match in all directories. */
3520 TAILQ_FOREACH(pe, ignores, entry) {
3521 struct got_pathlist_head *ignorelist = pe->data;
3522 struct got_pathlist_entry *pi;
3524 TAILQ_FOREACH(pi, ignorelist, entry) {
3525 const char *p, *pattern = pi->path;
3527 if (strncmp(pattern, "**/", 3) != 0)
3528 continue;
3529 pattern += 3;
3530 p = path;
3531 while (*p) {
3532 if (fnmatch(pattern, p,
3533 FNM_PATHNAME | FNM_LEADING_DIR)) {
3534 /* Retry in next directory. */
3535 while (*p && *p != '/')
3536 p++;
3537 while (*p == '/')
3538 p++;
3539 continue;
3541 return 1;
3547 * The ignores pathlist contains ignore lists from children before
3548 * parents, so we can find the most specific ignorelist by walking
3549 * ignores backwards.
3551 pe = TAILQ_LAST(ignores, got_pathlist_head);
3552 while (pe) {
3553 if (got_path_is_child(path, pe->path, pe->path_len)) {
3554 struct got_pathlist_head *ignorelist = pe->data;
3555 struct got_pathlist_entry *pi;
3556 TAILQ_FOREACH(pi, ignorelist, entry) {
3557 const char *pattern = pi->path;
3558 int flags = FNM_LEADING_DIR;
3559 if (strstr(pattern, "/**/") == NULL)
3560 flags |= FNM_PATHNAME;
3561 if (fnmatch(pattern, path, flags))
3562 continue;
3563 return 1;
3566 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3569 return 0;
3572 static const struct got_error *
3573 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3574 const char *path, int dirfd, const char *ignores_filename)
3576 const struct got_error *err = NULL;
3577 char *ignorespath;
3578 int fd = -1;
3579 FILE *ignoresfile = NULL;
3581 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3582 path[0] ? "/" : "", ignores_filename) == -1)
3583 return got_error_from_errno("asprintf");
3585 if (dirfd != -1) {
3586 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3587 if (fd == -1) {
3588 if (errno != ENOENT && errno != EACCES)
3589 err = got_error_from_errno2("openat",
3590 ignorespath);
3591 } else {
3592 ignoresfile = fdopen(fd, "r");
3593 if (ignoresfile == NULL)
3594 err = got_error_from_errno2("fdopen",
3595 ignorespath);
3596 else {
3597 fd = -1;
3598 err = read_ignores(ignores, path, ignoresfile);
3601 } else {
3602 ignoresfile = fopen(ignorespath, "r");
3603 if (ignoresfile == NULL) {
3604 if (errno != ENOENT && errno != EACCES)
3605 err = got_error_from_errno2("fopen",
3606 ignorespath);
3607 } else
3608 err = read_ignores(ignores, path, ignoresfile);
3611 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3612 err = got_error_from_errno2("fclose", path);
3613 if (fd != -1 && close(fd) == -1 && err == NULL)
3614 err = got_error_from_errno2("close", path);
3615 free(ignorespath);
3616 return err;
3619 static const struct got_error *
3620 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3622 const struct got_error *err = NULL;
3623 struct diff_dir_cb_arg *a = arg;
3624 char *path = NULL;
3626 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3627 return got_error(GOT_ERR_CANCELLED);
3629 if (parent_path[0]) {
3630 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3631 return got_error_from_errno("asprintf");
3632 } else {
3633 path = de->d_name;
3636 if (de->d_type != DT_DIR &&
3637 got_path_is_child(path, a->status_path, a->status_path_len)
3638 && !match_ignores(a->ignores, path))
3639 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3640 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3641 if (parent_path[0])
3642 free(path);
3643 return err;
3646 static const struct got_error *
3647 status_traverse(void *arg, const char *path, int dirfd)
3649 const struct got_error *err = NULL;
3650 struct diff_dir_cb_arg *a = arg;
3652 if (a->no_ignores)
3653 return NULL;
3655 err = add_ignores(a->ignores, a->worktree->root_path,
3656 path, dirfd, ".cvsignore");
3657 if (err)
3658 return err;
3660 err = add_ignores(a->ignores, a->worktree->root_path, path,
3661 dirfd, ".gitignore");
3663 return err;
3666 static const struct got_error *
3667 report_single_file_status(const char *path, const char *ondisk_path,
3668 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3669 void *status_arg, struct got_repository *repo, int report_unchanged,
3670 struct got_pathlist_head *ignores, int no_ignores)
3672 struct got_fileindex_entry *ie;
3673 struct stat sb;
3675 if (!no_ignores && match_ignores(ignores, path))
3676 return NULL;
3678 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3679 if (ie)
3680 return report_file_status(ie, ondisk_path, -1, NULL,
3681 status_cb, status_arg, repo, report_unchanged);
3683 if (lstat(ondisk_path, &sb) == -1) {
3684 if (errno != ENOENT)
3685 return got_error_from_errno2("lstat", ondisk_path);
3686 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3687 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3688 return NULL;
3691 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3692 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3693 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3695 return NULL;
3698 static const struct got_error *
3699 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3700 const char *root_path, const char *path)
3702 const struct got_error *err;
3703 char *parent_path, *next_parent_path = NULL;
3705 err = add_ignores(ignores, root_path, "", -1,
3706 ".cvsignore");
3707 if (err)
3708 return err;
3710 err = add_ignores(ignores, root_path, "", -1,
3711 ".gitignore");
3712 if (err)
3713 return err;
3715 err = got_path_dirname(&parent_path, path);
3716 if (err) {
3717 if (err->code == GOT_ERR_BAD_PATH)
3718 return NULL; /* cannot traverse parent */
3719 return err;
3721 for (;;) {
3722 err = add_ignores(ignores, root_path, parent_path, -1,
3723 ".cvsignore");
3724 if (err)
3725 break;
3726 err = add_ignores(ignores, root_path, parent_path, -1,
3727 ".gitignore");
3728 if (err)
3729 break;
3730 err = got_path_dirname(&next_parent_path, parent_path);
3731 if (err) {
3732 if (err->code == GOT_ERR_BAD_PATH)
3733 err = NULL; /* traversed everything */
3734 break;
3736 if (got_path_is_root_dir(parent_path))
3737 break;
3738 free(parent_path);
3739 parent_path = next_parent_path;
3740 next_parent_path = NULL;
3743 free(parent_path);
3744 free(next_parent_path);
3745 return err;
3748 static const struct got_error *
3749 worktree_status(struct got_worktree *worktree, const char *path,
3750 struct got_fileindex *fileindex, struct got_repository *repo,
3751 got_worktree_status_cb status_cb, void *status_arg,
3752 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3753 int report_unchanged)
3755 const struct got_error *err = NULL;
3756 int fd = -1;
3757 struct got_fileindex_diff_dir_cb fdiff_cb;
3758 struct diff_dir_cb_arg arg;
3759 char *ondisk_path = NULL;
3760 struct got_pathlist_head ignores;
3762 TAILQ_INIT(&ignores);
3764 if (asprintf(&ondisk_path, "%s%s%s",
3765 worktree->root_path, path[0] ? "/" : "", path) == -1)
3766 return got_error_from_errno("asprintf");
3768 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3769 if (fd == -1) {
3770 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3771 !got_err_open_nofollow_on_symlink())
3772 err = got_error_from_errno2("open", ondisk_path);
3773 else {
3774 if (!no_ignores) {
3775 err = add_ignores_from_parent_paths(&ignores,
3776 worktree->root_path, ondisk_path);
3777 if (err)
3778 goto done;
3780 err = report_single_file_status(path, ondisk_path,
3781 fileindex, status_cb, status_arg, repo,
3782 report_unchanged, &ignores, no_ignores);
3784 } else {
3785 fdiff_cb.diff_old_new = status_old_new;
3786 fdiff_cb.diff_old = status_old;
3787 fdiff_cb.diff_new = status_new;
3788 fdiff_cb.diff_traverse = status_traverse;
3789 arg.fileindex = fileindex;
3790 arg.worktree = worktree;
3791 arg.status_path = path;
3792 arg.status_path_len = strlen(path);
3793 arg.repo = repo;
3794 arg.status_cb = status_cb;
3795 arg.status_arg = status_arg;
3796 arg.cancel_cb = cancel_cb;
3797 arg.cancel_arg = cancel_arg;
3798 arg.report_unchanged = report_unchanged;
3799 arg.no_ignores = no_ignores;
3800 if (!no_ignores) {
3801 err = add_ignores_from_parent_paths(&ignores,
3802 worktree->root_path, path);
3803 if (err)
3804 goto done;
3806 arg.ignores = &ignores;
3807 err = got_fileindex_diff_dir(fileindex, fd,
3808 worktree->root_path, path, repo, &fdiff_cb, &arg);
3810 done:
3811 free_ignores(&ignores);
3812 if (fd != -1 && close(fd) == -1 && err == NULL)
3813 err = got_error_from_errno("close");
3814 free(ondisk_path);
3815 return err;
3818 const struct got_error *
3819 got_worktree_status(struct got_worktree *worktree,
3820 struct got_pathlist_head *paths, struct got_repository *repo,
3821 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3822 got_cancel_cb cancel_cb, void *cancel_arg)
3824 const struct got_error *err = NULL;
3825 char *fileindex_path = NULL;
3826 struct got_fileindex *fileindex = NULL;
3827 struct got_pathlist_entry *pe;
3829 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3830 if (err)
3831 return err;
3833 TAILQ_FOREACH(pe, paths, entry) {
3834 err = worktree_status(worktree, pe->path, fileindex, repo,
3835 status_cb, status_arg, cancel_cb, cancel_arg,
3836 no_ignores, 0);
3837 if (err)
3838 break;
3840 free(fileindex_path);
3841 got_fileindex_free(fileindex);
3842 return err;
3845 const struct got_error *
3846 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3847 const char *arg)
3849 const struct got_error *err = NULL;
3850 char *resolved = NULL, *cwd = NULL, *path = NULL;
3851 size_t len;
3852 struct stat sb;
3853 char *abspath = NULL;
3854 char canonpath[PATH_MAX];
3856 *wt_path = NULL;
3858 cwd = getcwd(NULL, 0);
3859 if (cwd == NULL)
3860 return got_error_from_errno("getcwd");
3862 if (lstat(arg, &sb) == -1) {
3863 if (errno != ENOENT) {
3864 err = got_error_from_errno2("lstat", arg);
3865 goto done;
3867 sb.st_mode = 0;
3869 if (S_ISLNK(sb.st_mode)) {
3871 * We cannot use realpath(3) with symlinks since we want to
3872 * operate on the symlink itself.
3873 * But we can make the path absolute, assuming it is relative
3874 * to the current working directory, and then canonicalize it.
3876 if (!got_path_is_absolute(arg)) {
3877 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3878 err = got_error_from_errno("asprintf");
3879 goto done;
3883 err = got_canonpath(abspath ? abspath : arg, canonpath,
3884 sizeof(canonpath));
3885 if (err)
3886 goto done;
3887 resolved = strdup(canonpath);
3888 if (resolved == NULL) {
3889 err = got_error_from_errno("strdup");
3890 goto done;
3892 } else {
3893 resolved = realpath(arg, NULL);
3894 if (resolved == NULL) {
3895 if (errno != ENOENT) {
3896 err = got_error_from_errno2("realpath", arg);
3897 goto done;
3899 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3900 err = got_error_from_errno("asprintf");
3901 goto done;
3903 err = got_canonpath(abspath, canonpath,
3904 sizeof(canonpath));
3905 if (err)
3906 goto done;
3907 resolved = strdup(canonpath);
3908 if (resolved == NULL) {
3909 err = got_error_from_errno("strdup");
3910 goto done;
3915 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3916 strlen(got_worktree_get_root_path(worktree)))) {
3917 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3918 goto done;
3921 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3922 err = got_path_skip_common_ancestor(&path,
3923 got_worktree_get_root_path(worktree), resolved);
3924 if (err)
3925 goto done;
3926 } else {
3927 path = strdup("");
3928 if (path == NULL) {
3929 err = got_error_from_errno("strdup");
3930 goto done;
3934 /* XXX status walk can't deal with trailing slash! */
3935 len = strlen(path);
3936 while (len > 0 && path[len - 1] == '/') {
3937 path[len - 1] = '\0';
3938 len--;
3940 done:
3941 free(abspath);
3942 free(resolved);
3943 free(cwd);
3944 if (err == NULL)
3945 *wt_path = path;
3946 else
3947 free(path);
3948 return err;
3951 struct schedule_addition_args {
3952 struct got_worktree *worktree;
3953 struct got_fileindex *fileindex;
3954 got_worktree_checkout_cb progress_cb;
3955 void *progress_arg;
3956 struct got_repository *repo;
3959 static const struct got_error *
3960 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3961 const char *relpath, struct got_object_id *blob_id,
3962 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3963 int dirfd, const char *de_name)
3965 struct schedule_addition_args *a = arg;
3966 const struct got_error *err = NULL;
3967 struct got_fileindex_entry *ie;
3968 struct stat sb;
3969 char *ondisk_path;
3971 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3972 relpath) == -1)
3973 return got_error_from_errno("asprintf");
3975 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3976 if (ie) {
3977 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3978 de_name, a->repo);
3979 if (err)
3980 goto done;
3981 /* Re-adding an existing entry is a no-op. */
3982 if (status == GOT_STATUS_ADD)
3983 goto done;
3984 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3985 if (err)
3986 goto done;
3989 if (status != GOT_STATUS_UNVERSIONED) {
3990 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3991 goto done;
3994 err = got_fileindex_entry_alloc(&ie, relpath);
3995 if (err)
3996 goto done;
3997 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3998 relpath, NULL, NULL, 1);
3999 if (err) {
4000 got_fileindex_entry_free(ie);
4001 goto done;
4003 err = got_fileindex_entry_add(a->fileindex, ie);
4004 if (err) {
4005 got_fileindex_entry_free(ie);
4006 goto done;
4008 done:
4009 free(ondisk_path);
4010 if (err)
4011 return err;
4012 if (status == GOT_STATUS_ADD)
4013 return NULL;
4014 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4017 const struct got_error *
4018 got_worktree_schedule_add(struct got_worktree *worktree,
4019 struct got_pathlist_head *paths,
4020 got_worktree_checkout_cb progress_cb, void *progress_arg,
4021 struct got_repository *repo, int no_ignores)
4023 struct got_fileindex *fileindex = NULL;
4024 char *fileindex_path = NULL;
4025 const struct got_error *err = NULL, *sync_err, *unlockerr;
4026 struct got_pathlist_entry *pe;
4027 struct schedule_addition_args saa;
4029 err = lock_worktree(worktree, LOCK_EX);
4030 if (err)
4031 return err;
4033 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4034 if (err)
4035 goto done;
4037 saa.worktree = worktree;
4038 saa.fileindex = fileindex;
4039 saa.progress_cb = progress_cb;
4040 saa.progress_arg = progress_arg;
4041 saa.repo = repo;
4043 TAILQ_FOREACH(pe, paths, entry) {
4044 err = worktree_status(worktree, pe->path, fileindex, repo,
4045 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4046 if (err)
4047 break;
4049 sync_err = sync_fileindex(fileindex, fileindex_path);
4050 if (sync_err && err == NULL)
4051 err = sync_err;
4052 done:
4053 free(fileindex_path);
4054 if (fileindex)
4055 got_fileindex_free(fileindex);
4056 unlockerr = lock_worktree(worktree, LOCK_SH);
4057 if (unlockerr && err == NULL)
4058 err = unlockerr;
4059 return err;
4062 struct schedule_deletion_args {
4063 struct got_worktree *worktree;
4064 struct got_fileindex *fileindex;
4065 got_worktree_delete_cb progress_cb;
4066 void *progress_arg;
4067 struct got_repository *repo;
4068 int delete_local_mods;
4069 int keep_on_disk;
4070 const char *status_codes;
4073 static const struct got_error *
4074 schedule_for_deletion(void *arg, unsigned char status,
4075 unsigned char staged_status, const char *relpath,
4076 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4077 struct got_object_id *commit_id, int dirfd, const char *de_name)
4079 struct schedule_deletion_args *a = arg;
4080 const struct got_error *err = NULL;
4081 struct got_fileindex_entry *ie = NULL;
4082 struct stat sb;
4083 char *ondisk_path;
4085 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4086 if (ie == NULL)
4087 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4089 staged_status = get_staged_status(ie);
4090 if (staged_status != GOT_STATUS_NO_CHANGE) {
4091 if (staged_status == GOT_STATUS_DELETE)
4092 return NULL;
4093 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4096 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4097 relpath) == -1)
4098 return got_error_from_errno("asprintf");
4100 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4101 a->repo);
4102 if (err)
4103 goto done;
4105 if (a->status_codes) {
4106 size_t ncodes = strlen(a->status_codes);
4107 int i;
4108 for (i = 0; i < ncodes ; i++) {
4109 if (status == a->status_codes[i])
4110 break;
4112 if (i == ncodes) {
4113 /* Do not delete files in non-matching status. */
4114 free(ondisk_path);
4115 return NULL;
4117 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4118 a->status_codes[i] != GOT_STATUS_MISSING) {
4119 static char msg[64];
4120 snprintf(msg, sizeof(msg),
4121 "invalid status code '%c'", a->status_codes[i]);
4122 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4123 goto done;
4127 if (status != GOT_STATUS_NO_CHANGE) {
4128 if (status == GOT_STATUS_DELETE)
4129 goto done;
4130 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4131 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4132 goto done;
4134 if (status != GOT_STATUS_MODIFY &&
4135 status != GOT_STATUS_MISSING) {
4136 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4137 goto done;
4141 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4142 size_t root_len;
4144 if (dirfd != -1) {
4145 if (unlinkat(dirfd, de_name, 0) != 0) {
4146 err = got_error_from_errno2("unlinkat",
4147 ondisk_path);
4148 goto done;
4150 } else if (unlink(ondisk_path) != 0) {
4151 err = got_error_from_errno2("unlink", ondisk_path);
4152 goto done;
4155 root_len = strlen(a->worktree->root_path);
4156 do {
4157 char *parent;
4158 err = got_path_dirname(&parent, ondisk_path);
4159 if (err)
4160 goto done;
4161 free(ondisk_path);
4162 ondisk_path = parent;
4163 if (rmdir(ondisk_path) == -1) {
4164 if (errno != ENOTEMPTY)
4165 err = got_error_from_errno2("rmdir",
4166 ondisk_path);
4167 break;
4169 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4170 strlen(ondisk_path), root_len) != 0);
4173 got_fileindex_entry_mark_deleted_from_disk(ie);
4174 done:
4175 free(ondisk_path);
4176 if (err)
4177 return err;
4178 if (status == GOT_STATUS_DELETE)
4179 return NULL;
4180 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4181 staged_status, relpath);
4184 const struct got_error *
4185 got_worktree_schedule_delete(struct got_worktree *worktree,
4186 struct got_pathlist_head *paths, int delete_local_mods,
4187 const char *status_codes,
4188 got_worktree_delete_cb progress_cb, void *progress_arg,
4189 struct got_repository *repo, int keep_on_disk)
4191 struct got_fileindex *fileindex = NULL;
4192 char *fileindex_path = NULL;
4193 const struct got_error *err = NULL, *sync_err, *unlockerr;
4194 struct got_pathlist_entry *pe;
4195 struct schedule_deletion_args sda;
4197 err = lock_worktree(worktree, LOCK_EX);
4198 if (err)
4199 return err;
4201 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4202 if (err)
4203 goto done;
4205 sda.worktree = worktree;
4206 sda.fileindex = fileindex;
4207 sda.progress_cb = progress_cb;
4208 sda.progress_arg = progress_arg;
4209 sda.repo = repo;
4210 sda.delete_local_mods = delete_local_mods;
4211 sda.keep_on_disk = keep_on_disk;
4212 sda.status_codes = status_codes;
4214 TAILQ_FOREACH(pe, paths, entry) {
4215 err = worktree_status(worktree, pe->path, fileindex, repo,
4216 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4217 if (err)
4218 break;
4220 sync_err = sync_fileindex(fileindex, fileindex_path);
4221 if (sync_err && err == NULL)
4222 err = sync_err;
4223 done:
4224 free(fileindex_path);
4225 if (fileindex)
4226 got_fileindex_free(fileindex);
4227 unlockerr = lock_worktree(worktree, LOCK_SH);
4228 if (unlockerr && err == NULL)
4229 err = unlockerr;
4230 return err;
4233 static const struct got_error *
4234 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4236 const struct got_error *err = NULL;
4237 char *line = NULL;
4238 size_t linesize = 0, n;
4239 ssize_t linelen;
4241 linelen = getline(&line, &linesize, infile);
4242 if (linelen == -1) {
4243 if (ferror(infile)) {
4244 err = got_error_from_errno("getline");
4245 goto done;
4247 return NULL;
4249 if (outfile) {
4250 n = fwrite(line, 1, linelen, outfile);
4251 if (n != linelen) {
4252 err = got_ferror(outfile, GOT_ERR_IO);
4253 goto done;
4256 if (rejectfile) {
4257 n = fwrite(line, 1, linelen, rejectfile);
4258 if (n != linelen)
4259 err = got_ferror(outfile, GOT_ERR_IO);
4261 done:
4262 free(line);
4263 return err;
4266 static const struct got_error *
4267 skip_one_line(FILE *f)
4269 char *line = NULL;
4270 size_t linesize = 0;
4271 ssize_t linelen;
4273 linelen = getline(&line, &linesize, f);
4274 if (linelen == -1) {
4275 if (ferror(f))
4276 return got_error_from_errno("getline");
4277 return NULL;
4279 free(line);
4280 return NULL;
4283 static const struct got_error *
4284 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4285 int start_old, int end_old, int start_new, int end_new,
4286 FILE *outfile, FILE *rejectfile)
4288 const struct got_error *err;
4290 /* Copy old file's lines leading up to patch. */
4291 while (!feof(f1) && *line_cur1 < start_old) {
4292 err = copy_one_line(f1, outfile, NULL);
4293 if (err)
4294 return err;
4295 (*line_cur1)++;
4297 /* Skip new file's lines leading up to patch. */
4298 while (!feof(f2) && *line_cur2 < start_new) {
4299 if (rejectfile)
4300 err = copy_one_line(f2, NULL, rejectfile);
4301 else
4302 err = skip_one_line(f2);
4303 if (err)
4304 return err;
4305 (*line_cur2)++;
4307 /* Copy patched lines. */
4308 while (!feof(f2) && *line_cur2 <= end_new) {
4309 err = copy_one_line(f2, outfile, NULL);
4310 if (err)
4311 return err;
4312 (*line_cur2)++;
4314 /* Skip over old file's replaced lines. */
4315 while (!feof(f1) && *line_cur1 <= end_old) {
4316 if (rejectfile)
4317 err = copy_one_line(f1, NULL, rejectfile);
4318 else
4319 err = skip_one_line(f1);
4320 if (err)
4321 return err;
4322 (*line_cur1)++;
4325 return NULL;
4328 static const struct got_error *
4329 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4330 FILE *outfile, FILE *rejectfile)
4332 const struct got_error *err;
4334 if (outfile) {
4335 /* Copy old file's lines until EOF. */
4336 while (!feof(f1)) {
4337 err = copy_one_line(f1, outfile, NULL);
4338 if (err)
4339 return err;
4340 (*line_cur1)++;
4343 if (rejectfile) {
4344 /* Copy new file's lines until EOF. */
4345 while (!feof(f2)) {
4346 err = copy_one_line(f2, NULL, rejectfile);
4347 if (err)
4348 return err;
4349 (*line_cur2)++;
4353 return NULL;
4356 static const struct got_error *
4357 apply_or_reject_change(int *choice, int *nchunks_used,
4358 struct diff_result *diff_result, int n,
4359 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4360 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4361 got_worktree_patch_cb patch_cb, void *patch_arg)
4363 const struct got_error *err = NULL;
4364 struct diff_chunk_context cc = {};
4365 int start_old, end_old, start_new, end_new;
4366 FILE *hunkfile;
4367 struct diff_output_unidiff_state *diff_state;
4368 struct diff_input_info diff_info;
4369 int rc;
4371 *choice = GOT_PATCH_CHOICE_NONE;
4373 /* Get changed line numbers without context lines for copy_change(). */
4374 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4375 start_old = cc.left.start;
4376 end_old = cc.left.end;
4377 start_new = cc.right.start;
4378 end_new = cc.right.end;
4380 /* Get the same change with context lines for display. */
4381 memset(&cc, 0, sizeof(cc));
4382 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4384 memset(&diff_info, 0, sizeof(diff_info));
4385 diff_info.left_path = relpath;
4386 diff_info.right_path = relpath;
4388 diff_state = diff_output_unidiff_state_alloc();
4389 if (diff_state == NULL)
4390 return got_error_set_errno(ENOMEM,
4391 "diff_output_unidiff_state_alloc");
4393 hunkfile = got_opentemp();
4394 if (hunkfile == NULL) {
4395 err = got_error_from_errno("got_opentemp");
4396 goto done;
4399 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4400 diff_result, &cc);
4401 if (rc != DIFF_RC_OK) {
4402 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4403 goto done;
4406 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4407 err = got_ferror(hunkfile, GOT_ERR_IO);
4408 goto done;
4411 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4412 hunkfile, changeno, nchanges);
4413 if (err)
4414 goto done;
4416 switch (*choice) {
4417 case GOT_PATCH_CHOICE_YES:
4418 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4419 end_old, start_new, end_new, outfile, rejectfile);
4420 break;
4421 case GOT_PATCH_CHOICE_NO:
4422 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4423 end_old, start_new, end_new, rejectfile, outfile);
4424 break;
4425 case GOT_PATCH_CHOICE_QUIT:
4426 break;
4427 default:
4428 err = got_error(GOT_ERR_PATCH_CHOICE);
4429 break;
4431 done:
4432 diff_output_unidiff_state_free(diff_state);
4433 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4434 err = got_error_from_errno("fclose");
4435 return err;
4438 struct revert_file_args {
4439 struct got_worktree *worktree;
4440 struct got_fileindex *fileindex;
4441 got_worktree_checkout_cb progress_cb;
4442 void *progress_arg;
4443 got_worktree_patch_cb patch_cb;
4444 void *patch_arg;
4445 struct got_repository *repo;
4446 int unlink_added_files;
4449 static const struct got_error *
4450 create_patched_content(char **path_outfile, int reverse_patch,
4451 struct got_object_id *blob_id, const char *path2,
4452 int dirfd2, const char *de_name2,
4453 const char *relpath, struct got_repository *repo,
4454 got_worktree_patch_cb patch_cb, void *patch_arg)
4456 const struct got_error *err, *free_err;
4457 struct got_blob_object *blob = NULL;
4458 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4459 int fd2 = -1;
4460 char link_target[PATH_MAX];
4461 ssize_t link_len = 0;
4462 char *path1 = NULL, *id_str = NULL;
4463 struct stat sb2;
4464 struct got_diffreg_result *diffreg_result = NULL;
4465 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4466 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4468 *path_outfile = NULL;
4470 err = got_object_id_str(&id_str, blob_id);
4471 if (err)
4472 return err;
4474 if (dirfd2 != -1) {
4475 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4476 if (fd2 == -1) {
4477 if (!got_err_open_nofollow_on_symlink()) {
4478 err = got_error_from_errno2("openat", path2);
4479 goto done;
4481 link_len = readlinkat(dirfd2, de_name2,
4482 link_target, sizeof(link_target));
4483 if (link_len == -1)
4484 return got_error_from_errno2("readlinkat", path2);
4485 sb2.st_mode = S_IFLNK;
4486 sb2.st_size = link_len;
4488 } else {
4489 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4490 if (fd2 == -1) {
4491 if (!got_err_open_nofollow_on_symlink()) {
4492 err = got_error_from_errno2("open", path2);
4493 goto done;
4495 link_len = readlink(path2, link_target,
4496 sizeof(link_target));
4497 if (link_len == -1)
4498 return got_error_from_errno2("readlink", path2);
4499 sb2.st_mode = S_IFLNK;
4500 sb2.st_size = link_len;
4503 if (fd2 != -1) {
4504 if (fstat(fd2, &sb2) == -1) {
4505 err = got_error_from_errno2("fstat", path2);
4506 goto done;
4509 f2 = fdopen(fd2, "r");
4510 if (f2 == NULL) {
4511 err = got_error_from_errno2("fdopen", path2);
4512 goto done;
4514 fd2 = -1;
4515 } else {
4516 size_t n;
4517 f2 = got_opentemp();
4518 if (f2 == NULL) {
4519 err = got_error_from_errno2("got_opentemp", path2);
4520 goto done;
4522 n = fwrite(link_target, 1, link_len, f2);
4523 if (n != link_len) {
4524 err = got_ferror(f2, GOT_ERR_IO);
4525 goto done;
4527 if (fflush(f2) == EOF) {
4528 err = got_error_from_errno("fflush");
4529 goto done;
4531 rewind(f2);
4534 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4535 if (err)
4536 goto done;
4538 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4539 if (err)
4540 goto done;
4542 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4543 if (err)
4544 goto done;
4546 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4547 NULL);
4548 if (err)
4549 goto done;
4551 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4552 if (err)
4553 goto done;
4555 if (fseek(f1, 0L, SEEK_SET) == -1)
4556 return got_ferror(f1, GOT_ERR_IO);
4557 if (fseek(f2, 0L, SEEK_SET) == -1)
4558 return got_ferror(f2, GOT_ERR_IO);
4560 /* Count the number of actual changes in the diff result. */
4561 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4562 struct diff_chunk_context cc = {};
4563 diff_chunk_context_load_change(&cc, &nchunks_used,
4564 diffreg_result->result, n, 0);
4565 nchanges++;
4567 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4568 int choice;
4569 err = apply_or_reject_change(&choice, &nchunks_used,
4570 diffreg_result->result, n, relpath, f1, f2,
4571 &line_cur1, &line_cur2,
4572 reverse_patch ? NULL : outfile,
4573 reverse_patch ? outfile : NULL,
4574 ++i, nchanges, patch_cb, patch_arg);
4575 if (err)
4576 goto done;
4577 if (choice == GOT_PATCH_CHOICE_YES)
4578 have_content = 1;
4579 else if (choice == GOT_PATCH_CHOICE_QUIT)
4580 break;
4582 if (have_content) {
4583 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4584 reverse_patch ? NULL : outfile,
4585 reverse_patch ? outfile : NULL);
4586 if (err)
4587 goto done;
4589 if (!S_ISLNK(sb2.st_mode)) {
4590 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4591 err = got_error_from_errno2("fchmod", path2);
4592 goto done;
4596 done:
4597 free(id_str);
4598 if (blob)
4599 got_object_blob_close(blob);
4600 free_err = got_diffreg_result_free(diffreg_result);
4601 if (err == NULL)
4602 err = free_err;
4603 if (f1 && fclose(f1) == EOF && err == NULL)
4604 err = got_error_from_errno2("fclose", path1);
4605 if (f2 && fclose(f2) == EOF && err == NULL)
4606 err = got_error_from_errno2("fclose", path2);
4607 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4608 err = got_error_from_errno2("close", path2);
4609 if (outfile && fclose(outfile) == EOF && err == NULL)
4610 err = got_error_from_errno2("fclose", *path_outfile);
4611 if (path1 && unlink(path1) == -1 && err == NULL)
4612 err = got_error_from_errno2("unlink", path1);
4613 if (err || !have_content) {
4614 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4615 err = got_error_from_errno2("unlink", *path_outfile);
4616 free(*path_outfile);
4617 *path_outfile = NULL;
4619 free(path1);
4620 return err;
4623 static const struct got_error *
4624 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4625 const char *relpath, struct got_object_id *blob_id,
4626 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4627 int dirfd, const char *de_name)
4629 struct revert_file_args *a = arg;
4630 const struct got_error *err = NULL;
4631 char *parent_path = NULL;
4632 struct got_fileindex_entry *ie;
4633 struct got_tree_object *tree = NULL;
4634 struct got_object_id *tree_id = NULL;
4635 const struct got_tree_entry *te = NULL;
4636 char *tree_path = NULL, *te_name;
4637 char *ondisk_path = NULL, *path_content = NULL;
4638 struct got_blob_object *blob = NULL;
4640 /* Reverting a staged deletion is a no-op. */
4641 if (status == GOT_STATUS_DELETE &&
4642 staged_status != GOT_STATUS_NO_CHANGE)
4643 return NULL;
4645 if (status == GOT_STATUS_UNVERSIONED)
4646 return (*a->progress_cb)(a->progress_arg,
4647 GOT_STATUS_UNVERSIONED, relpath);
4649 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4650 if (ie == NULL)
4651 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4653 /* Construct in-repository path of tree which contains this blob. */
4654 err = got_path_dirname(&parent_path, ie->path);
4655 if (err) {
4656 if (err->code != GOT_ERR_BAD_PATH)
4657 goto done;
4658 parent_path = strdup("/");
4659 if (parent_path == NULL) {
4660 err = got_error_from_errno("strdup");
4661 goto done;
4664 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4665 tree_path = strdup(parent_path);
4666 if (tree_path == NULL) {
4667 err = got_error_from_errno("strdup");
4668 goto done;
4670 } else {
4671 if (got_path_is_root_dir(parent_path)) {
4672 tree_path = strdup(a->worktree->path_prefix);
4673 if (tree_path == NULL) {
4674 err = got_error_from_errno("strdup");
4675 goto done;
4677 } else {
4678 if (asprintf(&tree_path, "%s/%s",
4679 a->worktree->path_prefix, parent_path) == -1) {
4680 err = got_error_from_errno("asprintf");
4681 goto done;
4686 err = got_object_id_by_path(&tree_id, a->repo,
4687 a->worktree->base_commit_id, tree_path);
4688 if (err) {
4689 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4690 (status == GOT_STATUS_ADD ||
4691 staged_status == GOT_STATUS_ADD)))
4692 goto done;
4693 } else {
4694 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4695 if (err)
4696 goto done;
4698 err = got_path_basename(&te_name, ie->path);
4699 if (err)
4700 goto done;
4702 te = got_object_tree_find_entry(tree, te_name);
4703 free(te_name);
4704 if (te == NULL && status != GOT_STATUS_ADD &&
4705 staged_status != GOT_STATUS_ADD) {
4706 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4707 goto done;
4711 switch (status) {
4712 case GOT_STATUS_ADD:
4713 if (a->patch_cb) {
4714 int choice = GOT_PATCH_CHOICE_NONE;
4715 err = (*a->patch_cb)(&choice, a->patch_arg,
4716 status, ie->path, NULL, 1, 1);
4717 if (err)
4718 goto done;
4719 if (choice != GOT_PATCH_CHOICE_YES)
4720 break;
4722 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4723 ie->path);
4724 if (err)
4725 goto done;
4726 got_fileindex_entry_remove(a->fileindex, ie);
4727 if (a->unlink_added_files) {
4728 if (asprintf(&ondisk_path, "%s/%s",
4729 got_worktree_get_root_path(a->worktree),
4730 relpath) == -1) {
4731 err = got_error_from_errno("asprintf");
4732 goto done;
4734 if (unlink(ondisk_path) == -1) {
4735 err = got_error_from_errno2("unlink",
4736 ondisk_path);
4737 break;
4740 break;
4741 case GOT_STATUS_DELETE:
4742 if (a->patch_cb) {
4743 int choice = GOT_PATCH_CHOICE_NONE;
4744 err = (*a->patch_cb)(&choice, a->patch_arg,
4745 status, ie->path, NULL, 1, 1);
4746 if (err)
4747 goto done;
4748 if (choice != GOT_PATCH_CHOICE_YES)
4749 break;
4751 /* fall through */
4752 case GOT_STATUS_MODIFY:
4753 case GOT_STATUS_MODE_CHANGE:
4754 case GOT_STATUS_CONFLICT:
4755 case GOT_STATUS_MISSING: {
4756 struct got_object_id id;
4757 if (staged_status == GOT_STATUS_ADD ||
4758 staged_status == GOT_STATUS_MODIFY) {
4759 memcpy(id.sha1, ie->staged_blob_sha1,
4760 SHA1_DIGEST_LENGTH);
4761 } else
4762 memcpy(id.sha1, ie->blob_sha1,
4763 SHA1_DIGEST_LENGTH);
4764 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4765 if (err)
4766 goto done;
4768 if (asprintf(&ondisk_path, "%s/%s",
4769 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4770 err = got_error_from_errno("asprintf");
4771 goto done;
4774 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4775 status == GOT_STATUS_CONFLICT)) {
4776 int is_bad_symlink = 0;
4777 err = create_patched_content(&path_content, 1, &id,
4778 ondisk_path, dirfd, de_name, ie->path, a->repo,
4779 a->patch_cb, a->patch_arg);
4780 if (err || path_content == NULL)
4781 break;
4782 if (te && S_ISLNK(te->mode)) {
4783 if (unlink(path_content) == -1) {
4784 err = got_error_from_errno2("unlink",
4785 path_content);
4786 break;
4788 err = install_symlink(&is_bad_symlink,
4789 a->worktree, ondisk_path, ie->path,
4790 blob, 0, 1, 0, 0, a->repo,
4791 a->progress_cb, a->progress_arg);
4792 } else {
4793 if (rename(path_content, ondisk_path) == -1) {
4794 err = got_error_from_errno3("rename",
4795 path_content, ondisk_path);
4796 goto done;
4799 } else {
4800 int is_bad_symlink = 0;
4801 if (te && S_ISLNK(te->mode)) {
4802 err = install_symlink(&is_bad_symlink,
4803 a->worktree, ondisk_path, ie->path,
4804 blob, 0, 1, 0, 0, a->repo,
4805 a->progress_cb, a->progress_arg);
4806 } else {
4807 err = install_blob(a->worktree, ondisk_path,
4808 ie->path,
4809 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4810 got_fileindex_perms_to_st(ie), blob,
4811 0, 1, 0, 0, a->repo,
4812 a->progress_cb, a->progress_arg);
4814 if (err)
4815 goto done;
4816 if (status == GOT_STATUS_DELETE ||
4817 status == GOT_STATUS_MODE_CHANGE) {
4818 err = got_fileindex_entry_update(ie,
4819 a->worktree->root_fd, relpath,
4820 blob->id.sha1,
4821 a->worktree->base_commit_id->sha1, 1);
4822 if (err)
4823 goto done;
4825 if (is_bad_symlink) {
4826 got_fileindex_entry_filetype_set(ie,
4827 GOT_FILEIDX_MODE_BAD_SYMLINK);
4830 break;
4832 default:
4833 break;
4835 done:
4836 free(ondisk_path);
4837 free(path_content);
4838 free(parent_path);
4839 free(tree_path);
4840 if (blob)
4841 got_object_blob_close(blob);
4842 if (tree)
4843 got_object_tree_close(tree);
4844 free(tree_id);
4845 return err;
4848 const struct got_error *
4849 got_worktree_revert(struct got_worktree *worktree,
4850 struct got_pathlist_head *paths,
4851 got_worktree_checkout_cb progress_cb, void *progress_arg,
4852 got_worktree_patch_cb patch_cb, void *patch_arg,
4853 struct got_repository *repo)
4855 struct got_fileindex *fileindex = NULL;
4856 char *fileindex_path = NULL;
4857 const struct got_error *err = NULL, *unlockerr = NULL;
4858 const struct got_error *sync_err = NULL;
4859 struct got_pathlist_entry *pe;
4860 struct revert_file_args rfa;
4862 err = lock_worktree(worktree, LOCK_EX);
4863 if (err)
4864 return err;
4866 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4867 if (err)
4868 goto done;
4870 rfa.worktree = worktree;
4871 rfa.fileindex = fileindex;
4872 rfa.progress_cb = progress_cb;
4873 rfa.progress_arg = progress_arg;
4874 rfa.patch_cb = patch_cb;
4875 rfa.patch_arg = patch_arg;
4876 rfa.repo = repo;
4877 rfa.unlink_added_files = 0;
4878 TAILQ_FOREACH(pe, paths, entry) {
4879 err = worktree_status(worktree, pe->path, fileindex, repo,
4880 revert_file, &rfa, NULL, NULL, 0, 0);
4881 if (err)
4882 break;
4884 sync_err = sync_fileindex(fileindex, fileindex_path);
4885 if (sync_err && err == NULL)
4886 err = sync_err;
4887 done:
4888 free(fileindex_path);
4889 if (fileindex)
4890 got_fileindex_free(fileindex);
4891 unlockerr = lock_worktree(worktree, LOCK_SH);
4892 if (unlockerr && err == NULL)
4893 err = unlockerr;
4894 return err;
4897 static void
4898 free_commitable(struct got_commitable *ct)
4900 free(ct->path);
4901 free(ct->in_repo_path);
4902 free(ct->ondisk_path);
4903 free(ct->blob_id);
4904 free(ct->base_blob_id);
4905 free(ct->staged_blob_id);
4906 free(ct->base_commit_id);
4907 free(ct);
4910 struct collect_commitables_arg {
4911 struct got_pathlist_head *commitable_paths;
4912 struct got_repository *repo;
4913 struct got_worktree *worktree;
4914 struct got_fileindex *fileindex;
4915 int have_staged_files;
4916 int allow_bad_symlinks;
4919 static const struct got_error *
4920 collect_commitables(void *arg, unsigned char status,
4921 unsigned char staged_status, const char *relpath,
4922 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4923 struct got_object_id *commit_id, int dirfd, const char *de_name)
4925 struct collect_commitables_arg *a = arg;
4926 const struct got_error *err = NULL;
4927 struct got_commitable *ct = NULL;
4928 struct got_pathlist_entry *new = NULL;
4929 char *parent_path = NULL, *path = NULL;
4930 struct stat sb;
4932 if (a->have_staged_files) {
4933 if (staged_status != GOT_STATUS_MODIFY &&
4934 staged_status != GOT_STATUS_ADD &&
4935 staged_status != GOT_STATUS_DELETE)
4936 return NULL;
4937 } else {
4938 if (status == GOT_STATUS_CONFLICT)
4939 return got_error(GOT_ERR_COMMIT_CONFLICT);
4941 if (status != GOT_STATUS_MODIFY &&
4942 status != GOT_STATUS_MODE_CHANGE &&
4943 status != GOT_STATUS_ADD &&
4944 status != GOT_STATUS_DELETE)
4945 return NULL;
4948 if (asprintf(&path, "/%s", relpath) == -1) {
4949 err = got_error_from_errno("asprintf");
4950 goto done;
4952 if (strcmp(path, "/") == 0) {
4953 parent_path = strdup("");
4954 if (parent_path == NULL)
4955 return got_error_from_errno("strdup");
4956 } else {
4957 err = got_path_dirname(&parent_path, path);
4958 if (err)
4959 return err;
4962 ct = calloc(1, sizeof(*ct));
4963 if (ct == NULL) {
4964 err = got_error_from_errno("calloc");
4965 goto done;
4968 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4969 relpath) == -1) {
4970 err = got_error_from_errno("asprintf");
4971 goto done;
4974 if (staged_status == GOT_STATUS_ADD ||
4975 staged_status == GOT_STATUS_MODIFY) {
4976 struct got_fileindex_entry *ie;
4977 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4978 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4979 case GOT_FILEIDX_MODE_REGULAR_FILE:
4980 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4981 ct->mode = S_IFREG;
4982 break;
4983 case GOT_FILEIDX_MODE_SYMLINK:
4984 ct->mode = S_IFLNK;
4985 break;
4986 default:
4987 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4988 goto done;
4990 ct->mode |= got_fileindex_entry_perms_get(ie);
4991 } else if (status != GOT_STATUS_DELETE &&
4992 staged_status != GOT_STATUS_DELETE) {
4993 if (dirfd != -1) {
4994 if (fstatat(dirfd, de_name, &sb,
4995 AT_SYMLINK_NOFOLLOW) == -1) {
4996 err = got_error_from_errno2("fstatat",
4997 ct->ondisk_path);
4998 goto done;
5000 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5001 err = got_error_from_errno2("lstat", ct->ondisk_path);
5002 goto done;
5004 ct->mode = sb.st_mode;
5007 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5008 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5009 relpath) == -1) {
5010 err = got_error_from_errno("asprintf");
5011 goto done;
5014 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5015 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5016 int is_bad_symlink;
5017 char target_path[PATH_MAX];
5018 ssize_t target_len;
5019 target_len = readlink(ct->ondisk_path, target_path,
5020 sizeof(target_path));
5021 if (target_len == -1) {
5022 err = got_error_from_errno2("readlink",
5023 ct->ondisk_path);
5024 goto done;
5026 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5027 target_len, ct->ondisk_path, a->worktree->root_path);
5028 if (err)
5029 goto done;
5030 if (is_bad_symlink) {
5031 err = got_error_path(ct->ondisk_path,
5032 GOT_ERR_BAD_SYMLINK);
5033 goto done;
5038 ct->status = status;
5039 ct->staged_status = staged_status;
5040 ct->blob_id = NULL; /* will be filled in when blob gets created */
5041 if (ct->status != GOT_STATUS_ADD &&
5042 ct->staged_status != GOT_STATUS_ADD) {
5043 ct->base_blob_id = got_object_id_dup(blob_id);
5044 if (ct->base_blob_id == NULL) {
5045 err = got_error_from_errno("got_object_id_dup");
5046 goto done;
5048 ct->base_commit_id = got_object_id_dup(commit_id);
5049 if (ct->base_commit_id == NULL) {
5050 err = got_error_from_errno("got_object_id_dup");
5051 goto done;
5054 if (ct->staged_status == GOT_STATUS_ADD ||
5055 ct->staged_status == GOT_STATUS_MODIFY) {
5056 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5057 if (ct->staged_blob_id == NULL) {
5058 err = got_error_from_errno("got_object_id_dup");
5059 goto done;
5062 ct->path = strdup(path);
5063 if (ct->path == NULL) {
5064 err = got_error_from_errno("strdup");
5065 goto done;
5067 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5068 done:
5069 if (ct && (err || new == NULL))
5070 free_commitable(ct);
5071 free(parent_path);
5072 free(path);
5073 return err;
5076 static const struct got_error *write_tree(struct got_object_id **, int *,
5077 struct got_tree_object *, const char *, struct got_pathlist_head *,
5078 got_worktree_status_cb status_cb, void *status_arg,
5079 struct got_repository *);
5081 static const struct got_error *
5082 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5083 struct got_tree_entry *te, const char *parent_path,
5084 struct got_pathlist_head *commitable_paths,
5085 got_worktree_status_cb status_cb, void *status_arg,
5086 struct got_repository *repo)
5088 const struct got_error *err = NULL;
5089 struct got_tree_object *subtree;
5090 char *subpath;
5092 if (asprintf(&subpath, "%s%s%s", parent_path,
5093 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5094 return got_error_from_errno("asprintf");
5096 err = got_object_open_as_tree(&subtree, repo, &te->id);
5097 if (err)
5098 return err;
5100 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5101 commitable_paths, status_cb, status_arg, repo);
5102 got_object_tree_close(subtree);
5103 free(subpath);
5104 return err;
5107 static const struct got_error *
5108 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5110 const struct got_error *err = NULL;
5111 char *ct_parent_path = NULL;
5113 *match = 0;
5115 if (strchr(ct->in_repo_path, '/') == NULL) {
5116 *match = got_path_is_root_dir(path);
5117 return NULL;
5120 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5121 if (err)
5122 return err;
5123 *match = (strcmp(path, ct_parent_path) == 0);
5124 free(ct_parent_path);
5125 return err;
5128 static mode_t
5129 get_ct_file_mode(struct got_commitable *ct)
5131 if (S_ISLNK(ct->mode))
5132 return S_IFLNK;
5134 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5137 static const struct got_error *
5138 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5139 struct got_tree_entry *te, struct got_commitable *ct)
5141 const struct got_error *err = NULL;
5143 *new_te = NULL;
5145 err = got_object_tree_entry_dup(new_te, te);
5146 if (err)
5147 goto done;
5149 (*new_te)->mode = get_ct_file_mode(ct);
5151 if (ct->staged_status == GOT_STATUS_MODIFY)
5152 memcpy(&(*new_te)->id, ct->staged_blob_id,
5153 sizeof((*new_te)->id));
5154 else
5155 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5156 done:
5157 if (err && *new_te) {
5158 free(*new_te);
5159 *new_te = NULL;
5161 return err;
5164 static const struct got_error *
5165 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5166 struct got_commitable *ct)
5168 const struct got_error *err = NULL;
5169 char *ct_name = NULL;
5171 *new_te = NULL;
5173 *new_te = calloc(1, sizeof(**new_te));
5174 if (*new_te == NULL)
5175 return got_error_from_errno("calloc");
5177 err = got_path_basename(&ct_name, ct->path);
5178 if (err)
5179 goto done;
5180 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5181 sizeof((*new_te)->name)) {
5182 err = got_error(GOT_ERR_NO_SPACE);
5183 goto done;
5186 (*new_te)->mode = get_ct_file_mode(ct);
5188 if (ct->staged_status == GOT_STATUS_ADD)
5189 memcpy(&(*new_te)->id, ct->staged_blob_id,
5190 sizeof((*new_te)->id));
5191 else
5192 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5193 done:
5194 free(ct_name);
5195 if (err && *new_te) {
5196 free(*new_te);
5197 *new_te = NULL;
5199 return err;
5202 static const struct got_error *
5203 insert_tree_entry(struct got_tree_entry *new_te,
5204 struct got_pathlist_head *paths)
5206 const struct got_error *err = NULL;
5207 struct got_pathlist_entry *new_pe;
5209 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5210 if (err)
5211 return err;
5212 if (new_pe == NULL)
5213 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5214 return NULL;
5217 static const struct got_error *
5218 report_ct_status(struct got_commitable *ct,
5219 got_worktree_status_cb status_cb, void *status_arg)
5221 const char *ct_path = ct->path;
5222 unsigned char status;
5224 if (status_cb == NULL) /* no commit progress output desired */
5225 return NULL;
5227 while (ct_path[0] == '/')
5228 ct_path++;
5230 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5231 status = ct->staged_status;
5232 else
5233 status = ct->status;
5235 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5236 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5239 static const struct got_error *
5240 match_modified_subtree(int *modified, struct got_tree_entry *te,
5241 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5243 const struct got_error *err = NULL;
5244 struct got_pathlist_entry *pe;
5245 char *te_path;
5247 *modified = 0;
5249 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5250 got_path_is_root_dir(base_tree_path) ? "" : "/",
5251 te->name) == -1)
5252 return got_error_from_errno("asprintf");
5254 TAILQ_FOREACH(pe, commitable_paths, entry) {
5255 struct got_commitable *ct = pe->data;
5256 *modified = got_path_is_child(ct->in_repo_path, te_path,
5257 strlen(te_path));
5258 if (*modified)
5259 break;
5262 free(te_path);
5263 return err;
5266 static const struct got_error *
5267 match_deleted_or_modified_ct(struct got_commitable **ctp,
5268 struct got_tree_entry *te, const char *base_tree_path,
5269 struct got_pathlist_head *commitable_paths)
5271 const struct got_error *err = NULL;
5272 struct got_pathlist_entry *pe;
5274 *ctp = NULL;
5276 TAILQ_FOREACH(pe, commitable_paths, entry) {
5277 struct got_commitable *ct = pe->data;
5278 char *ct_name = NULL;
5279 int path_matches;
5281 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5282 if (ct->status != GOT_STATUS_MODIFY &&
5283 ct->status != GOT_STATUS_MODE_CHANGE &&
5284 ct->status != GOT_STATUS_DELETE)
5285 continue;
5286 } else {
5287 if (ct->staged_status != GOT_STATUS_MODIFY &&
5288 ct->staged_status != GOT_STATUS_DELETE)
5289 continue;
5292 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5293 continue;
5295 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5296 if (err)
5297 return err;
5298 if (!path_matches)
5299 continue;
5301 err = got_path_basename(&ct_name, pe->path);
5302 if (err)
5303 return err;
5305 if (strcmp(te->name, ct_name) != 0) {
5306 free(ct_name);
5307 continue;
5309 free(ct_name);
5311 *ctp = ct;
5312 break;
5315 return err;
5318 static const struct got_error *
5319 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5320 const char *child_path, const char *path_base_tree,
5321 struct got_pathlist_head *commitable_paths,
5322 got_worktree_status_cb status_cb, void *status_arg,
5323 struct got_repository *repo)
5325 const struct got_error *err = NULL;
5326 struct got_tree_entry *new_te;
5327 char *subtree_path;
5328 struct got_object_id *id = NULL;
5329 int nentries;
5331 *new_tep = NULL;
5333 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5334 got_path_is_root_dir(path_base_tree) ? "" : "/",
5335 child_path) == -1)
5336 return got_error_from_errno("asprintf");
5338 new_te = calloc(1, sizeof(*new_te));
5339 if (new_te == NULL)
5340 return got_error_from_errno("calloc");
5341 new_te->mode = S_IFDIR;
5343 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5344 sizeof(new_te->name)) {
5345 err = got_error(GOT_ERR_NO_SPACE);
5346 goto done;
5348 err = write_tree(&id, &nentries, NULL, subtree_path,
5349 commitable_paths, status_cb, status_arg, repo);
5350 if (err) {
5351 free(new_te);
5352 goto done;
5354 memcpy(&new_te->id, id, sizeof(new_te->id));
5355 done:
5356 free(id);
5357 free(subtree_path);
5358 if (err == NULL)
5359 *new_tep = new_te;
5360 return err;
5363 static const struct got_error *
5364 write_tree(struct got_object_id **new_tree_id, int *nentries,
5365 struct got_tree_object *base_tree, const char *path_base_tree,
5366 struct got_pathlist_head *commitable_paths,
5367 got_worktree_status_cb status_cb, void *status_arg,
5368 struct got_repository *repo)
5370 const struct got_error *err = NULL;
5371 struct got_pathlist_head paths;
5372 struct got_tree_entry *te, *new_te = NULL;
5373 struct got_pathlist_entry *pe;
5375 TAILQ_INIT(&paths);
5376 *nentries = 0;
5378 /* Insert, and recurse into, newly added entries first. */
5379 TAILQ_FOREACH(pe, commitable_paths, entry) {
5380 struct got_commitable *ct = pe->data;
5381 char *child_path = NULL, *slash;
5383 if ((ct->status != GOT_STATUS_ADD &&
5384 ct->staged_status != GOT_STATUS_ADD) ||
5385 (ct->flags & GOT_COMMITABLE_ADDED))
5386 continue;
5388 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5389 strlen(path_base_tree)))
5390 continue;
5392 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5393 ct->in_repo_path);
5394 if (err)
5395 goto done;
5397 slash = strchr(child_path, '/');
5398 if (slash == NULL) {
5399 err = alloc_added_blob_tree_entry(&new_te, ct);
5400 if (err)
5401 goto done;
5402 err = report_ct_status(ct, status_cb, status_arg);
5403 if (err)
5404 goto done;
5405 ct->flags |= GOT_COMMITABLE_ADDED;
5406 err = insert_tree_entry(new_te, &paths);
5407 if (err)
5408 goto done;
5409 (*nentries)++;
5410 } else {
5411 *slash = '\0'; /* trim trailing path components */
5412 if (base_tree == NULL ||
5413 got_object_tree_find_entry(base_tree, child_path)
5414 == NULL) {
5415 err = make_subtree_for_added_blob(&new_te,
5416 child_path, path_base_tree,
5417 commitable_paths, status_cb, status_arg,
5418 repo);
5419 if (err)
5420 goto done;
5421 err = insert_tree_entry(new_te, &paths);
5422 if (err)
5423 goto done;
5424 (*nentries)++;
5429 if (base_tree) {
5430 int i, nbase_entries;
5431 /* Handle modified and deleted entries. */
5432 nbase_entries = got_object_tree_get_nentries(base_tree);
5433 for (i = 0; i < nbase_entries; i++) {
5434 struct got_commitable *ct = NULL;
5436 te = got_object_tree_get_entry(base_tree, i);
5437 if (got_object_tree_entry_is_submodule(te)) {
5438 /* Entry is a submodule; just copy it. */
5439 err = got_object_tree_entry_dup(&new_te, te);
5440 if (err)
5441 goto done;
5442 err = insert_tree_entry(new_te, &paths);
5443 if (err)
5444 goto done;
5445 (*nentries)++;
5446 continue;
5449 if (S_ISDIR(te->mode)) {
5450 int modified;
5451 err = got_object_tree_entry_dup(&new_te, te);
5452 if (err)
5453 goto done;
5454 err = match_modified_subtree(&modified, te,
5455 path_base_tree, commitable_paths);
5456 if (err)
5457 goto done;
5458 /* Avoid recursion into unmodified subtrees. */
5459 if (modified) {
5460 struct got_object_id *new_id;
5461 int nsubentries;
5462 err = write_subtree(&new_id,
5463 &nsubentries, te,
5464 path_base_tree, commitable_paths,
5465 status_cb, status_arg, repo);
5466 if (err)
5467 goto done;
5468 if (nsubentries == 0) {
5469 /* All entries were deleted. */
5470 free(new_id);
5471 continue;
5473 memcpy(&new_te->id, new_id,
5474 sizeof(new_te->id));
5475 free(new_id);
5477 err = insert_tree_entry(new_te, &paths);
5478 if (err)
5479 goto done;
5480 (*nentries)++;
5481 continue;
5484 err = match_deleted_or_modified_ct(&ct, te,
5485 path_base_tree, commitable_paths);
5486 if (err)
5487 goto done;
5488 if (ct) {
5489 /* NB: Deleted entries get dropped here. */
5490 if (ct->status == GOT_STATUS_MODIFY ||
5491 ct->status == GOT_STATUS_MODE_CHANGE ||
5492 ct->staged_status == GOT_STATUS_MODIFY) {
5493 err = alloc_modified_blob_tree_entry(
5494 &new_te, te, ct);
5495 if (err)
5496 goto done;
5497 err = insert_tree_entry(new_te, &paths);
5498 if (err)
5499 goto done;
5500 (*nentries)++;
5502 err = report_ct_status(ct, status_cb,
5503 status_arg);
5504 if (err)
5505 goto done;
5506 } else {
5507 /* Entry is unchanged; just copy it. */
5508 err = got_object_tree_entry_dup(&new_te, te);
5509 if (err)
5510 goto done;
5511 err = insert_tree_entry(new_te, &paths);
5512 if (err)
5513 goto done;
5514 (*nentries)++;
5519 /* Write new list of entries; deleted entries have been dropped. */
5520 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5521 done:
5522 got_pathlist_free(&paths);
5523 return err;
5526 static const struct got_error *
5527 update_fileindex_after_commit(struct got_worktree *worktree,
5528 struct got_pathlist_head *commitable_paths,
5529 struct got_object_id *new_base_commit_id,
5530 struct got_fileindex *fileindex, int have_staged_files)
5532 const struct got_error *err = NULL;
5533 struct got_pathlist_entry *pe;
5534 char *relpath = NULL;
5536 TAILQ_FOREACH(pe, commitable_paths, entry) {
5537 struct got_fileindex_entry *ie;
5538 struct got_commitable *ct = pe->data;
5540 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5542 err = got_path_skip_common_ancestor(&relpath,
5543 worktree->root_path, ct->ondisk_path);
5544 if (err)
5545 goto done;
5547 if (ie) {
5548 if (ct->status == GOT_STATUS_DELETE ||
5549 ct->staged_status == GOT_STATUS_DELETE) {
5550 got_fileindex_entry_remove(fileindex, ie);
5551 } else if (ct->staged_status == GOT_STATUS_ADD ||
5552 ct->staged_status == GOT_STATUS_MODIFY) {
5553 got_fileindex_entry_stage_set(ie,
5554 GOT_FILEIDX_STAGE_NONE);
5555 got_fileindex_entry_staged_filetype_set(ie, 0);
5557 err = got_fileindex_entry_update(ie,
5558 worktree->root_fd, relpath,
5559 ct->staged_blob_id->sha1,
5560 new_base_commit_id->sha1,
5561 !have_staged_files);
5562 } else
5563 err = got_fileindex_entry_update(ie,
5564 worktree->root_fd, relpath,
5565 ct->blob_id->sha1,
5566 new_base_commit_id->sha1,
5567 !have_staged_files);
5568 } else {
5569 err = got_fileindex_entry_alloc(&ie, pe->path);
5570 if (err)
5571 goto done;
5572 err = got_fileindex_entry_update(ie,
5573 worktree->root_fd, relpath, ct->blob_id->sha1,
5574 new_base_commit_id->sha1, 1);
5575 if (err) {
5576 got_fileindex_entry_free(ie);
5577 goto done;
5579 err = got_fileindex_entry_add(fileindex, ie);
5580 if (err) {
5581 got_fileindex_entry_free(ie);
5582 goto done;
5585 free(relpath);
5586 relpath = NULL;
5588 done:
5589 free(relpath);
5590 return err;
5594 static const struct got_error *
5595 check_out_of_date(const char *in_repo_path, unsigned char status,
5596 unsigned char staged_status, struct got_object_id *base_blob_id,
5597 struct got_object_id *base_commit_id,
5598 struct got_object_id *head_commit_id, struct got_repository *repo,
5599 int ood_errcode)
5601 const struct got_error *err = NULL;
5602 struct got_object_id *id = NULL;
5604 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5605 /* Trivial case: base commit == head commit */
5606 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5607 return NULL;
5609 * Ensure file content which local changes were based
5610 * on matches file content in the branch head.
5612 err = got_object_id_by_path(&id, repo, head_commit_id,
5613 in_repo_path);
5614 if (err) {
5615 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5616 err = got_error(ood_errcode);
5617 goto done;
5618 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5619 err = got_error(ood_errcode);
5620 } else {
5621 /* Require that added files don't exist in the branch head. */
5622 err = got_object_id_by_path(&id, repo, head_commit_id,
5623 in_repo_path);
5624 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5625 goto done;
5626 err = id ? got_error(ood_errcode) : NULL;
5628 done:
5629 free(id);
5630 return err;
5633 const struct got_error *
5634 commit_worktree(struct got_object_id **new_commit_id,
5635 struct got_pathlist_head *commitable_paths,
5636 struct got_object_id *head_commit_id,
5637 struct got_object_id *parent_id2,
5638 struct got_worktree *worktree,
5639 const char *author, const char *committer,
5640 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5641 got_worktree_status_cb status_cb, void *status_arg,
5642 struct got_repository *repo)
5644 const struct got_error *err = NULL, *unlockerr = NULL;
5645 struct got_pathlist_entry *pe;
5646 const char *head_ref_name = NULL;
5647 struct got_commit_object *head_commit = NULL;
5648 struct got_reference *head_ref2 = NULL;
5649 struct got_object_id *head_commit_id2 = NULL;
5650 struct got_tree_object *head_tree = NULL;
5651 struct got_object_id *new_tree_id = NULL;
5652 int nentries, nparents = 0;
5653 struct got_object_id_queue parent_ids;
5654 struct got_object_qid *pid = NULL;
5655 char *logmsg = NULL;
5657 *new_commit_id = NULL;
5659 STAILQ_INIT(&parent_ids);
5661 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5662 if (err)
5663 goto done;
5665 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5666 if (err)
5667 goto done;
5669 if (commit_msg_cb != NULL) {
5670 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5671 if (err)
5672 goto done;
5675 if (logmsg == NULL || strlen(logmsg) == 0) {
5676 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5677 goto done;
5680 /* Create blobs from added and modified files and record their IDs. */
5681 TAILQ_FOREACH(pe, commitable_paths, entry) {
5682 struct got_commitable *ct = pe->data;
5683 char *ondisk_path;
5685 /* Blobs for staged files already exist. */
5686 if (ct->staged_status == GOT_STATUS_ADD ||
5687 ct->staged_status == GOT_STATUS_MODIFY)
5688 continue;
5690 if (ct->status != GOT_STATUS_ADD &&
5691 ct->status != GOT_STATUS_MODIFY &&
5692 ct->status != GOT_STATUS_MODE_CHANGE)
5693 continue;
5695 if (asprintf(&ondisk_path, "%s/%s",
5696 worktree->root_path, pe->path) == -1) {
5697 err = got_error_from_errno("asprintf");
5698 goto done;
5700 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5701 free(ondisk_path);
5702 if (err)
5703 goto done;
5706 /* Recursively write new tree objects. */
5707 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5708 commitable_paths, status_cb, status_arg, repo);
5709 if (err)
5710 goto done;
5712 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5713 if (err)
5714 goto done;
5715 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5716 nparents++;
5717 if (parent_id2) {
5718 err = got_object_qid_alloc(&pid, parent_id2);
5719 if (err)
5720 goto done;
5721 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5722 nparents++;
5724 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5725 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5726 if (logmsg != NULL)
5727 free(logmsg);
5728 if (err)
5729 goto done;
5731 /* Check if a concurrent commit to our branch has occurred. */
5732 head_ref_name = got_worktree_get_head_ref_name(worktree);
5733 if (head_ref_name == NULL) {
5734 err = got_error_from_errno("got_worktree_get_head_ref_name");
5735 goto done;
5737 /* Lock the reference here to prevent concurrent modification. */
5738 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5739 if (err)
5740 goto done;
5741 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5742 if (err)
5743 goto done;
5744 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5745 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5746 goto done;
5748 /* Update branch head in repository. */
5749 err = got_ref_change_ref(head_ref2, *new_commit_id);
5750 if (err)
5751 goto done;
5752 err = got_ref_write(head_ref2, repo);
5753 if (err)
5754 goto done;
5756 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5757 if (err)
5758 goto done;
5760 err = ref_base_commit(worktree, repo);
5761 if (err)
5762 goto done;
5763 done:
5764 got_object_id_queue_free(&parent_ids);
5765 if (head_tree)
5766 got_object_tree_close(head_tree);
5767 if (head_commit)
5768 got_object_commit_close(head_commit);
5769 free(head_commit_id2);
5770 if (head_ref2) {
5771 unlockerr = got_ref_unlock(head_ref2);
5772 if (unlockerr && err == NULL)
5773 err = unlockerr;
5774 got_ref_close(head_ref2);
5776 return err;
5779 static const struct got_error *
5780 check_path_is_commitable(const char *path,
5781 struct got_pathlist_head *commitable_paths)
5783 struct got_pathlist_entry *cpe = NULL;
5784 size_t path_len = strlen(path);
5786 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5787 struct got_commitable *ct = cpe->data;
5788 const char *ct_path = ct->path;
5790 while (ct_path[0] == '/')
5791 ct_path++;
5793 if (strcmp(path, ct_path) == 0 ||
5794 got_path_is_child(ct_path, path, path_len))
5795 break;
5798 if (cpe == NULL)
5799 return got_error_path(path, GOT_ERR_BAD_PATH);
5801 return NULL;
5804 static const struct got_error *
5805 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5807 int *have_staged_files = arg;
5809 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5810 *have_staged_files = 1;
5811 return got_error(GOT_ERR_CANCELLED);
5814 return NULL;
5817 static const struct got_error *
5818 check_non_staged_files(struct got_fileindex *fileindex,
5819 struct got_pathlist_head *paths)
5821 struct got_pathlist_entry *pe;
5822 struct got_fileindex_entry *ie;
5824 TAILQ_FOREACH(pe, paths, entry) {
5825 if (pe->path[0] == '\0')
5826 continue;
5827 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5828 if (ie == NULL)
5829 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5830 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5831 return got_error_path(pe->path,
5832 GOT_ERR_FILE_NOT_STAGED);
5835 return NULL;
5838 const struct got_error *
5839 got_worktree_commit(struct got_object_id **new_commit_id,
5840 struct got_worktree *worktree, struct got_pathlist_head *paths,
5841 const char *author, const char *committer, int allow_bad_symlinks,
5842 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5843 got_worktree_status_cb status_cb, void *status_arg,
5844 struct got_repository *repo)
5846 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5847 struct got_fileindex *fileindex = NULL;
5848 char *fileindex_path = NULL;
5849 struct got_pathlist_head commitable_paths;
5850 struct collect_commitables_arg cc_arg;
5851 struct got_pathlist_entry *pe;
5852 struct got_reference *head_ref = NULL;
5853 struct got_object_id *head_commit_id = NULL;
5854 int have_staged_files = 0;
5856 *new_commit_id = NULL;
5858 TAILQ_INIT(&commitable_paths);
5860 err = lock_worktree(worktree, LOCK_EX);
5861 if (err)
5862 goto done;
5864 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5865 if (err)
5866 goto done;
5868 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5869 if (err)
5870 goto done;
5872 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5873 if (err)
5874 goto done;
5876 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5877 &have_staged_files);
5878 if (err && err->code != GOT_ERR_CANCELLED)
5879 goto done;
5880 if (have_staged_files) {
5881 err = check_non_staged_files(fileindex, paths);
5882 if (err)
5883 goto done;
5886 cc_arg.commitable_paths = &commitable_paths;
5887 cc_arg.worktree = worktree;
5888 cc_arg.fileindex = fileindex;
5889 cc_arg.repo = repo;
5890 cc_arg.have_staged_files = have_staged_files;
5891 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5892 TAILQ_FOREACH(pe, paths, entry) {
5893 err = worktree_status(worktree, pe->path, fileindex, repo,
5894 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5895 if (err)
5896 goto done;
5899 if (TAILQ_EMPTY(&commitable_paths)) {
5900 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5901 goto done;
5904 TAILQ_FOREACH(pe, paths, entry) {
5905 err = check_path_is_commitable(pe->path, &commitable_paths);
5906 if (err)
5907 goto done;
5910 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5911 struct got_commitable *ct = pe->data;
5912 const char *ct_path = ct->in_repo_path;
5914 while (ct_path[0] == '/')
5915 ct_path++;
5916 err = check_out_of_date(ct_path, ct->status,
5917 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5918 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5919 if (err)
5920 goto done;
5924 err = commit_worktree(new_commit_id, &commitable_paths,
5925 head_commit_id, NULL, worktree, author, committer,
5926 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5927 if (err)
5928 goto done;
5930 err = update_fileindex_after_commit(worktree, &commitable_paths,
5931 *new_commit_id, fileindex, have_staged_files);
5932 sync_err = sync_fileindex(fileindex, fileindex_path);
5933 if (sync_err && err == NULL)
5934 err = sync_err;
5935 done:
5936 if (fileindex)
5937 got_fileindex_free(fileindex);
5938 free(fileindex_path);
5939 unlockerr = lock_worktree(worktree, LOCK_SH);
5940 if (unlockerr && err == NULL)
5941 err = unlockerr;
5942 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5943 struct got_commitable *ct = pe->data;
5944 free_commitable(ct);
5946 got_pathlist_free(&commitable_paths);
5947 return err;
5950 const char *
5951 got_commitable_get_path(struct got_commitable *ct)
5953 return ct->path;
5956 unsigned int
5957 got_commitable_get_status(struct got_commitable *ct)
5959 return ct->status;
5962 struct check_rebase_ok_arg {
5963 struct got_worktree *worktree;
5964 struct got_repository *repo;
5967 static const struct got_error *
5968 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5970 const struct got_error *err = NULL;
5971 struct check_rebase_ok_arg *a = arg;
5972 unsigned char status;
5973 struct stat sb;
5974 char *ondisk_path;
5976 /* Reject rebase of a work tree with mixed base commits. */
5977 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5978 SHA1_DIGEST_LENGTH))
5979 return got_error(GOT_ERR_MIXED_COMMITS);
5981 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5982 == -1)
5983 return got_error_from_errno("asprintf");
5985 /* Reject rebase of a work tree with modified or staged files. */
5986 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5987 free(ondisk_path);
5988 if (err)
5989 return err;
5991 if (status != GOT_STATUS_NO_CHANGE)
5992 return got_error(GOT_ERR_MODIFIED);
5993 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5994 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5996 return NULL;
5999 const struct got_error *
6000 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6001 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6002 struct got_worktree *worktree, struct got_reference *branch,
6003 struct got_repository *repo)
6005 const struct got_error *err = NULL;
6006 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6007 char *branch_ref_name = NULL;
6008 char *fileindex_path = NULL;
6009 struct check_rebase_ok_arg ok_arg;
6010 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6011 struct got_object_id *wt_branch_tip = NULL;
6013 *new_base_branch_ref = NULL;
6014 *tmp_branch = NULL;
6015 *fileindex = NULL;
6017 err = lock_worktree(worktree, LOCK_EX);
6018 if (err)
6019 return err;
6021 err = open_fileindex(fileindex, &fileindex_path, worktree);
6022 if (err)
6023 goto done;
6025 ok_arg.worktree = worktree;
6026 ok_arg.repo = repo;
6027 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6028 &ok_arg);
6029 if (err)
6030 goto done;
6032 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6033 if (err)
6034 goto done;
6036 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6037 if (err)
6038 goto done;
6040 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6041 if (err)
6042 goto done;
6044 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6045 0);
6046 if (err)
6047 goto done;
6049 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6050 if (err)
6051 goto done;
6052 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6053 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6054 goto done;
6057 err = got_ref_alloc_symref(new_base_branch_ref,
6058 new_base_branch_ref_name, wt_branch);
6059 if (err)
6060 goto done;
6061 err = got_ref_write(*new_base_branch_ref, repo);
6062 if (err)
6063 goto done;
6065 /* TODO Lock original branch's ref while rebasing? */
6067 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6068 if (err)
6069 goto done;
6071 err = got_ref_write(branch_ref, repo);
6072 if (err)
6073 goto done;
6075 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6076 worktree->base_commit_id);
6077 if (err)
6078 goto done;
6079 err = got_ref_write(*tmp_branch, repo);
6080 if (err)
6081 goto done;
6083 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6084 if (err)
6085 goto done;
6086 done:
6087 free(fileindex_path);
6088 free(tmp_branch_name);
6089 free(new_base_branch_ref_name);
6090 free(branch_ref_name);
6091 if (branch_ref)
6092 got_ref_close(branch_ref);
6093 if (wt_branch)
6094 got_ref_close(wt_branch);
6095 free(wt_branch_tip);
6096 if (err) {
6097 if (*new_base_branch_ref) {
6098 got_ref_close(*new_base_branch_ref);
6099 *new_base_branch_ref = NULL;
6101 if (*tmp_branch) {
6102 got_ref_close(*tmp_branch);
6103 *tmp_branch = NULL;
6105 if (*fileindex) {
6106 got_fileindex_free(*fileindex);
6107 *fileindex = NULL;
6109 lock_worktree(worktree, LOCK_SH);
6111 return err;
6114 const struct got_error *
6115 got_worktree_rebase_continue(struct got_object_id **commit_id,
6116 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6117 struct got_reference **branch, struct got_fileindex **fileindex,
6118 struct got_worktree *worktree, struct got_repository *repo)
6120 const struct got_error *err;
6121 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6122 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6123 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6124 char *fileindex_path = NULL;
6125 int have_staged_files = 0;
6127 *commit_id = NULL;
6128 *new_base_branch = NULL;
6129 *tmp_branch = NULL;
6130 *branch = NULL;
6131 *fileindex = NULL;
6133 err = lock_worktree(worktree, LOCK_EX);
6134 if (err)
6135 return err;
6137 err = open_fileindex(fileindex, &fileindex_path, worktree);
6138 if (err)
6139 goto done;
6141 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6142 &have_staged_files);
6143 if (err && err->code != GOT_ERR_CANCELLED)
6144 goto done;
6145 if (have_staged_files) {
6146 err = got_error(GOT_ERR_STAGED_PATHS);
6147 goto done;
6150 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6151 if (err)
6152 goto done;
6154 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6155 if (err)
6156 goto done;
6158 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6159 if (err)
6160 goto done;
6162 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6163 if (err)
6164 goto done;
6166 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6167 if (err)
6168 goto done;
6170 err = got_ref_open(branch, repo,
6171 got_ref_get_symref_target(branch_ref), 0);
6172 if (err)
6173 goto done;
6175 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6176 if (err)
6177 goto done;
6179 err = got_ref_resolve(commit_id, repo, commit_ref);
6180 if (err)
6181 goto done;
6183 err = got_ref_open(new_base_branch, repo,
6184 new_base_branch_ref_name, 0);
6185 if (err)
6186 goto done;
6188 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6189 if (err)
6190 goto done;
6191 done:
6192 free(commit_ref_name);
6193 free(branch_ref_name);
6194 free(fileindex_path);
6195 if (commit_ref)
6196 got_ref_close(commit_ref);
6197 if (branch_ref)
6198 got_ref_close(branch_ref);
6199 if (err) {
6200 free(*commit_id);
6201 *commit_id = NULL;
6202 if (*tmp_branch) {
6203 got_ref_close(*tmp_branch);
6204 *tmp_branch = NULL;
6206 if (*new_base_branch) {
6207 got_ref_close(*new_base_branch);
6208 *new_base_branch = NULL;
6210 if (*branch) {
6211 got_ref_close(*branch);
6212 *branch = NULL;
6214 if (*fileindex) {
6215 got_fileindex_free(*fileindex);
6216 *fileindex = NULL;
6218 lock_worktree(worktree, LOCK_SH);
6220 return err;
6223 const struct got_error *
6224 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6226 const struct got_error *err;
6227 char *tmp_branch_name = NULL;
6229 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6230 if (err)
6231 return err;
6233 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6234 free(tmp_branch_name);
6235 return NULL;
6238 static const struct got_error *
6239 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6240 char **logmsg, void *arg)
6242 *logmsg = arg;
6243 return NULL;
6246 static const struct got_error *
6247 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6248 const char *path, struct got_object_id *blob_id,
6249 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6250 int dirfd, const char *de_name)
6252 return NULL;
6255 struct collect_merged_paths_arg {
6256 got_worktree_checkout_cb progress_cb;
6257 void *progress_arg;
6258 struct got_pathlist_head *merged_paths;
6261 static const struct got_error *
6262 collect_merged_paths(void *arg, unsigned char status, const char *path)
6264 const struct got_error *err;
6265 struct collect_merged_paths_arg *a = arg;
6266 char *p;
6267 struct got_pathlist_entry *new;
6269 err = (*a->progress_cb)(a->progress_arg, status, path);
6270 if (err)
6271 return err;
6273 if (status != GOT_STATUS_MERGE &&
6274 status != GOT_STATUS_ADD &&
6275 status != GOT_STATUS_DELETE &&
6276 status != GOT_STATUS_CONFLICT)
6277 return NULL;
6279 p = strdup(path);
6280 if (p == NULL)
6281 return got_error_from_errno("strdup");
6283 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6284 if (err || new == NULL)
6285 free(p);
6286 return err;
6289 void
6290 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6292 struct got_pathlist_entry *pe;
6294 TAILQ_FOREACH(pe, merged_paths, entry)
6295 free((char *)pe->path);
6297 got_pathlist_free(merged_paths);
6300 static const struct got_error *
6301 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6302 int is_rebase, struct got_repository *repo)
6304 const struct got_error *err;
6305 struct got_reference *commit_ref = NULL;
6307 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6308 if (err) {
6309 if (err->code != GOT_ERR_NOT_REF)
6310 goto done;
6311 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6312 if (err)
6313 goto done;
6314 err = got_ref_write(commit_ref, repo);
6315 if (err)
6316 goto done;
6317 } else if (is_rebase) {
6318 struct got_object_id *stored_id;
6319 int cmp;
6321 err = got_ref_resolve(&stored_id, repo, commit_ref);
6322 if (err)
6323 goto done;
6324 cmp = got_object_id_cmp(commit_id, stored_id);
6325 free(stored_id);
6326 if (cmp != 0) {
6327 err = got_error(GOT_ERR_REBASE_COMMITID);
6328 goto done;
6331 done:
6332 if (commit_ref)
6333 got_ref_close(commit_ref);
6334 return err;
6337 static const struct got_error *
6338 rebase_merge_files(struct got_pathlist_head *merged_paths,
6339 const char *commit_ref_name, struct got_worktree *worktree,
6340 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6341 struct got_object_id *commit_id, struct got_repository *repo,
6342 got_worktree_checkout_cb progress_cb, void *progress_arg,
6343 got_cancel_cb cancel_cb, void *cancel_arg)
6345 const struct got_error *err;
6346 struct got_reference *commit_ref = NULL;
6347 struct collect_merged_paths_arg cmp_arg;
6348 char *fileindex_path;
6350 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6352 err = get_fileindex_path(&fileindex_path, worktree);
6353 if (err)
6354 return err;
6356 cmp_arg.progress_cb = progress_cb;
6357 cmp_arg.progress_arg = progress_arg;
6358 cmp_arg.merged_paths = merged_paths;
6359 err = merge_files(worktree, fileindex, fileindex_path,
6360 parent_commit_id, commit_id, repo, collect_merged_paths,
6361 &cmp_arg, cancel_cb, cancel_arg);
6362 if (commit_ref)
6363 got_ref_close(commit_ref);
6364 return err;
6367 const struct got_error *
6368 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6369 struct got_worktree *worktree, struct got_fileindex *fileindex,
6370 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6371 struct got_repository *repo,
6372 got_worktree_checkout_cb progress_cb, void *progress_arg,
6373 got_cancel_cb cancel_cb, void *cancel_arg)
6375 const struct got_error *err;
6376 char *commit_ref_name;
6378 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6379 if (err)
6380 return err;
6382 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6383 if (err)
6384 goto done;
6386 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6387 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6388 progress_arg, cancel_cb, cancel_arg);
6389 done:
6390 free(commit_ref_name);
6391 return err;
6394 const struct got_error *
6395 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6396 struct got_worktree *worktree, struct got_fileindex *fileindex,
6397 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6398 struct got_repository *repo,
6399 got_worktree_checkout_cb progress_cb, void *progress_arg,
6400 got_cancel_cb cancel_cb, void *cancel_arg)
6402 const struct got_error *err;
6403 char *commit_ref_name;
6405 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6406 if (err)
6407 return err;
6409 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6410 if (err)
6411 goto done;
6413 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6414 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6415 progress_arg, cancel_cb, cancel_arg);
6416 done:
6417 free(commit_ref_name);
6418 return err;
6421 static const struct got_error *
6422 rebase_commit(struct got_object_id **new_commit_id,
6423 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6424 struct got_worktree *worktree, struct got_fileindex *fileindex,
6425 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6426 const char *new_logmsg, struct got_repository *repo)
6428 const struct got_error *err, *sync_err;
6429 struct got_pathlist_head commitable_paths;
6430 struct collect_commitables_arg cc_arg;
6431 char *fileindex_path = NULL;
6432 struct got_reference *head_ref = NULL;
6433 struct got_object_id *head_commit_id = NULL;
6434 char *logmsg = NULL;
6436 TAILQ_INIT(&commitable_paths);
6437 *new_commit_id = NULL;
6439 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6441 err = get_fileindex_path(&fileindex_path, worktree);
6442 if (err)
6443 return err;
6445 cc_arg.commitable_paths = &commitable_paths;
6446 cc_arg.worktree = worktree;
6447 cc_arg.repo = repo;
6448 cc_arg.have_staged_files = 0;
6450 * If possible get the status of individual files directly to
6451 * avoid crawling the entire work tree once per rebased commit.
6453 * Ideally, merged_paths would contain a list of commitables
6454 * we could use so we could skip worktree_status() entirely.
6455 * However, we would then need carefully keep track of cumulative
6456 * effects of operations such as file additions and deletions
6457 * in 'got histedit -f' (folding multiple commits into one),
6458 * and this extra complexity is not really worth it.
6460 if (merged_paths) {
6461 struct got_pathlist_entry *pe;
6462 TAILQ_FOREACH(pe, merged_paths, entry) {
6463 err = worktree_status(worktree, pe->path, fileindex,
6464 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6465 0);
6466 if (err)
6467 goto done;
6469 } else {
6470 err = worktree_status(worktree, "", fileindex, repo,
6471 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6472 if (err)
6473 goto done;
6476 if (TAILQ_EMPTY(&commitable_paths)) {
6477 /* No-op change; commit will be elided. */
6478 err = got_ref_delete(commit_ref, repo);
6479 if (err)
6480 goto done;
6481 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6482 goto done;
6485 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6486 if (err)
6487 goto done;
6489 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6490 if (err)
6491 goto done;
6493 if (new_logmsg) {
6494 logmsg = strdup(new_logmsg);
6495 if (logmsg == NULL) {
6496 err = got_error_from_errno("strdup");
6497 goto done;
6499 } else {
6500 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6501 if (err)
6502 goto done;
6505 /* NB: commit_worktree will call free(logmsg) */
6506 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6507 NULL, worktree, got_object_commit_get_author(orig_commit),
6508 got_object_commit_get_committer(orig_commit),
6509 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6510 if (err)
6511 goto done;
6513 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6514 if (err)
6515 goto done;
6517 err = got_ref_delete(commit_ref, repo);
6518 if (err)
6519 goto done;
6521 err = update_fileindex_after_commit(worktree, &commitable_paths,
6522 *new_commit_id, fileindex, 0);
6523 sync_err = sync_fileindex(fileindex, fileindex_path);
6524 if (sync_err && err == NULL)
6525 err = sync_err;
6526 done:
6527 free(fileindex_path);
6528 free(head_commit_id);
6529 if (head_ref)
6530 got_ref_close(head_ref);
6531 if (err) {
6532 free(*new_commit_id);
6533 *new_commit_id = NULL;
6535 return err;
6538 const struct got_error *
6539 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6540 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6541 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6542 struct got_commit_object *orig_commit,
6543 struct got_object_id *orig_commit_id, struct got_repository *repo)
6545 const struct got_error *err;
6546 char *commit_ref_name;
6547 struct got_reference *commit_ref = NULL;
6548 struct got_object_id *commit_id = NULL;
6550 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6551 if (err)
6552 return err;
6554 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6555 if (err)
6556 goto done;
6557 err = got_ref_resolve(&commit_id, repo, commit_ref);
6558 if (err)
6559 goto done;
6560 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6561 err = got_error(GOT_ERR_REBASE_COMMITID);
6562 goto done;
6565 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6566 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6567 done:
6568 if (commit_ref)
6569 got_ref_close(commit_ref);
6570 free(commit_ref_name);
6571 free(commit_id);
6572 return err;
6575 const struct got_error *
6576 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6577 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6578 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6579 struct got_commit_object *orig_commit,
6580 struct got_object_id *orig_commit_id, const char *new_logmsg,
6581 struct got_repository *repo)
6583 const struct got_error *err;
6584 char *commit_ref_name;
6585 struct got_reference *commit_ref = NULL;
6587 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6588 if (err)
6589 return err;
6591 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6592 if (err)
6593 goto done;
6595 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6596 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6597 done:
6598 if (commit_ref)
6599 got_ref_close(commit_ref);
6600 free(commit_ref_name);
6601 return err;
6604 const struct got_error *
6605 got_worktree_rebase_postpone(struct got_worktree *worktree,
6606 struct got_fileindex *fileindex)
6608 if (fileindex)
6609 got_fileindex_free(fileindex);
6610 return lock_worktree(worktree, LOCK_SH);
6613 static const struct got_error *
6614 delete_ref(const char *name, struct got_repository *repo)
6616 const struct got_error *err;
6617 struct got_reference *ref;
6619 err = got_ref_open(&ref, repo, name, 0);
6620 if (err) {
6621 if (err->code == GOT_ERR_NOT_REF)
6622 return NULL;
6623 return err;
6626 err = got_ref_delete(ref, repo);
6627 got_ref_close(ref);
6628 return err;
6631 static const struct got_error *
6632 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6634 const struct got_error *err;
6635 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6636 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6638 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6639 if (err)
6640 goto done;
6641 err = delete_ref(tmp_branch_name, repo);
6642 if (err)
6643 goto done;
6645 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6646 if (err)
6647 goto done;
6648 err = delete_ref(new_base_branch_ref_name, repo);
6649 if (err)
6650 goto done;
6652 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6653 if (err)
6654 goto done;
6655 err = delete_ref(branch_ref_name, repo);
6656 if (err)
6657 goto done;
6659 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6660 if (err)
6661 goto done;
6662 err = delete_ref(commit_ref_name, repo);
6663 if (err)
6664 goto done;
6666 done:
6667 free(tmp_branch_name);
6668 free(new_base_branch_ref_name);
6669 free(branch_ref_name);
6670 free(commit_ref_name);
6671 return err;
6674 const struct got_error *
6675 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6676 struct got_object_id *new_commit_id, struct got_repository *repo)
6678 const struct got_error *err;
6679 struct got_reference *ref = NULL;
6680 struct got_object_id *old_commit_id = NULL;
6681 const char *branch_name = NULL;
6682 char *new_id_str = NULL;
6683 char *refname = NULL;
6685 branch_name = got_ref_get_name(branch);
6686 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6687 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6688 branch_name += 11;
6690 err = got_object_id_str(&new_id_str, new_commit_id);
6691 if (err)
6692 return err;
6694 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6695 new_id_str) == -1) {
6696 err = got_error_from_errno("asprintf");
6697 goto done;
6700 err = got_ref_resolve(&old_commit_id, repo, branch);
6701 if (err)
6702 goto done;
6704 err = got_ref_alloc(&ref, refname, old_commit_id);
6705 if (err)
6706 goto done;
6708 err = got_ref_write(ref, repo);
6709 done:
6710 free(new_id_str);
6711 free(refname);
6712 free(old_commit_id);
6713 if (ref)
6714 got_ref_close(ref);
6715 return err;
6718 const struct got_error *
6719 got_worktree_rebase_complete(struct got_worktree *worktree,
6720 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6721 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6722 struct got_repository *repo, int create_backup)
6724 const struct got_error *err, *unlockerr, *sync_err;
6725 struct got_object_id *new_head_commit_id = NULL;
6726 char *fileindex_path = NULL;
6728 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6729 if (err)
6730 return err;
6732 if (create_backup) {
6733 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6734 rebased_branch, new_head_commit_id, repo);
6735 if (err)
6736 goto done;
6739 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6740 if (err)
6741 goto done;
6743 err = got_ref_write(rebased_branch, repo);
6744 if (err)
6745 goto done;
6747 err = got_worktree_set_head_ref(worktree, rebased_branch);
6748 if (err)
6749 goto done;
6751 err = delete_rebase_refs(worktree, repo);
6752 if (err)
6753 goto done;
6755 err = get_fileindex_path(&fileindex_path, worktree);
6756 if (err)
6757 goto done;
6758 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6759 sync_err = sync_fileindex(fileindex, fileindex_path);
6760 if (sync_err && err == NULL)
6761 err = sync_err;
6762 done:
6763 got_fileindex_free(fileindex);
6764 free(fileindex_path);
6765 free(new_head_commit_id);
6766 unlockerr = lock_worktree(worktree, LOCK_SH);
6767 if (unlockerr && err == NULL)
6768 err = unlockerr;
6769 return err;
6772 const struct got_error *
6773 got_worktree_rebase_abort(struct got_worktree *worktree,
6774 struct got_fileindex *fileindex, struct got_repository *repo,
6775 struct got_reference *new_base_branch,
6776 got_worktree_checkout_cb progress_cb, void *progress_arg)
6778 const struct got_error *err, *unlockerr, *sync_err;
6779 struct got_reference *resolved = NULL;
6780 struct got_object_id *commit_id = NULL;
6781 char *fileindex_path = NULL;
6782 struct revert_file_args rfa;
6783 struct got_object_id *tree_id = NULL;
6785 err = lock_worktree(worktree, LOCK_EX);
6786 if (err)
6787 return err;
6789 err = got_ref_open(&resolved, repo,
6790 got_ref_get_symref_target(new_base_branch), 0);
6791 if (err)
6792 goto done;
6794 err = got_worktree_set_head_ref(worktree, resolved);
6795 if (err)
6796 goto done;
6799 * XXX commits to the base branch could have happened while
6800 * we were busy rebasing; should we store the original commit ID
6801 * when rebase begins and read it back here?
6803 err = got_ref_resolve(&commit_id, repo, resolved);
6804 if (err)
6805 goto done;
6807 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6808 if (err)
6809 goto done;
6811 err = got_object_id_by_path(&tree_id, repo,
6812 worktree->base_commit_id, worktree->path_prefix);
6813 if (err)
6814 goto done;
6816 err = delete_rebase_refs(worktree, repo);
6817 if (err)
6818 goto done;
6820 err = get_fileindex_path(&fileindex_path, worktree);
6821 if (err)
6822 goto done;
6824 rfa.worktree = worktree;
6825 rfa.fileindex = fileindex;
6826 rfa.progress_cb = progress_cb;
6827 rfa.progress_arg = progress_arg;
6828 rfa.patch_cb = NULL;
6829 rfa.patch_arg = NULL;
6830 rfa.repo = repo;
6831 rfa.unlink_added_files = 0;
6832 err = worktree_status(worktree, "", fileindex, repo,
6833 revert_file, &rfa, NULL, NULL, 0, 0);
6834 if (err)
6835 goto sync;
6837 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6838 repo, progress_cb, progress_arg, NULL, NULL);
6839 sync:
6840 sync_err = sync_fileindex(fileindex, fileindex_path);
6841 if (sync_err && err == NULL)
6842 err = sync_err;
6843 done:
6844 got_ref_close(resolved);
6845 free(tree_id);
6846 free(commit_id);
6847 if (fileindex)
6848 got_fileindex_free(fileindex);
6849 free(fileindex_path);
6851 unlockerr = lock_worktree(worktree, LOCK_SH);
6852 if (unlockerr && err == NULL)
6853 err = unlockerr;
6854 return err;
6857 const struct got_error *
6858 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6859 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6860 struct got_fileindex **fileindex, struct got_worktree *worktree,
6861 struct got_repository *repo)
6863 const struct got_error *err = NULL;
6864 char *tmp_branch_name = NULL;
6865 char *branch_ref_name = NULL;
6866 char *base_commit_ref_name = NULL;
6867 char *fileindex_path = NULL;
6868 struct check_rebase_ok_arg ok_arg;
6869 struct got_reference *wt_branch = NULL;
6870 struct got_reference *base_commit_ref = NULL;
6872 *tmp_branch = NULL;
6873 *branch_ref = NULL;
6874 *base_commit_id = NULL;
6875 *fileindex = NULL;
6877 err = lock_worktree(worktree, LOCK_EX);
6878 if (err)
6879 return err;
6881 err = open_fileindex(fileindex, &fileindex_path, worktree);
6882 if (err)
6883 goto done;
6885 ok_arg.worktree = worktree;
6886 ok_arg.repo = repo;
6887 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6888 &ok_arg);
6889 if (err)
6890 goto done;
6892 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6893 if (err)
6894 goto done;
6896 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6897 if (err)
6898 goto done;
6900 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6901 worktree);
6902 if (err)
6903 goto done;
6905 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6906 0);
6907 if (err)
6908 goto done;
6910 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6911 if (err)
6912 goto done;
6914 err = got_ref_write(*branch_ref, repo);
6915 if (err)
6916 goto done;
6918 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6919 worktree->base_commit_id);
6920 if (err)
6921 goto done;
6922 err = got_ref_write(base_commit_ref, repo);
6923 if (err)
6924 goto done;
6925 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6926 if (*base_commit_id == NULL) {
6927 err = got_error_from_errno("got_object_id_dup");
6928 goto done;
6931 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6932 worktree->base_commit_id);
6933 if (err)
6934 goto done;
6935 err = got_ref_write(*tmp_branch, repo);
6936 if (err)
6937 goto done;
6939 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6940 if (err)
6941 goto done;
6942 done:
6943 free(fileindex_path);
6944 free(tmp_branch_name);
6945 free(branch_ref_name);
6946 free(base_commit_ref_name);
6947 if (wt_branch)
6948 got_ref_close(wt_branch);
6949 if (err) {
6950 if (*branch_ref) {
6951 got_ref_close(*branch_ref);
6952 *branch_ref = NULL;
6954 if (*tmp_branch) {
6955 got_ref_close(*tmp_branch);
6956 *tmp_branch = NULL;
6958 free(*base_commit_id);
6959 if (*fileindex) {
6960 got_fileindex_free(*fileindex);
6961 *fileindex = NULL;
6963 lock_worktree(worktree, LOCK_SH);
6965 return err;
6968 const struct got_error *
6969 got_worktree_histedit_postpone(struct got_worktree *worktree,
6970 struct got_fileindex *fileindex)
6972 if (fileindex)
6973 got_fileindex_free(fileindex);
6974 return lock_worktree(worktree, LOCK_SH);
6977 const struct got_error *
6978 got_worktree_histedit_in_progress(int *in_progress,
6979 struct got_worktree *worktree)
6981 const struct got_error *err;
6982 char *tmp_branch_name = NULL;
6984 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6985 if (err)
6986 return err;
6988 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6989 free(tmp_branch_name);
6990 return NULL;
6993 const struct got_error *
6994 got_worktree_histedit_continue(struct got_object_id **commit_id,
6995 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6996 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6997 struct got_worktree *worktree, struct got_repository *repo)
6999 const struct got_error *err;
7000 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7001 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7002 struct got_reference *commit_ref = NULL;
7003 struct got_reference *base_commit_ref = NULL;
7004 char *fileindex_path = NULL;
7005 int have_staged_files = 0;
7007 *commit_id = NULL;
7008 *tmp_branch = NULL;
7009 *base_commit_id = NULL;
7010 *fileindex = NULL;
7012 err = lock_worktree(worktree, LOCK_EX);
7013 if (err)
7014 return err;
7016 err = open_fileindex(fileindex, &fileindex_path, worktree);
7017 if (err)
7018 goto done;
7020 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7021 &have_staged_files);
7022 if (err && err->code != GOT_ERR_CANCELLED)
7023 goto done;
7024 if (have_staged_files) {
7025 err = got_error(GOT_ERR_STAGED_PATHS);
7026 goto done;
7029 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7030 if (err)
7031 goto done;
7033 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7034 if (err)
7035 goto done;
7037 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7038 if (err)
7039 goto done;
7041 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7042 worktree);
7043 if (err)
7044 goto done;
7046 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7047 if (err)
7048 goto done;
7050 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7051 if (err)
7052 goto done;
7053 err = got_ref_resolve(commit_id, repo, commit_ref);
7054 if (err)
7055 goto done;
7057 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7058 if (err)
7059 goto done;
7060 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7061 if (err)
7062 goto done;
7064 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7065 if (err)
7066 goto done;
7067 done:
7068 free(commit_ref_name);
7069 free(branch_ref_name);
7070 free(fileindex_path);
7071 if (commit_ref)
7072 got_ref_close(commit_ref);
7073 if (base_commit_ref)
7074 got_ref_close(base_commit_ref);
7075 if (err) {
7076 free(*commit_id);
7077 *commit_id = NULL;
7078 free(*base_commit_id);
7079 *base_commit_id = NULL;
7080 if (*tmp_branch) {
7081 got_ref_close(*tmp_branch);
7082 *tmp_branch = NULL;
7084 if (*fileindex) {
7085 got_fileindex_free(*fileindex);
7086 *fileindex = NULL;
7088 lock_worktree(worktree, LOCK_EX);
7090 return err;
7093 static const struct got_error *
7094 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7096 const struct got_error *err;
7097 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7098 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7100 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7101 if (err)
7102 goto done;
7103 err = delete_ref(tmp_branch_name, repo);
7104 if (err)
7105 goto done;
7107 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7108 worktree);
7109 if (err)
7110 goto done;
7111 err = delete_ref(base_commit_ref_name, repo);
7112 if (err)
7113 goto done;
7115 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7116 if (err)
7117 goto done;
7118 err = delete_ref(branch_ref_name, repo);
7119 if (err)
7120 goto done;
7122 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7123 if (err)
7124 goto done;
7125 err = delete_ref(commit_ref_name, repo);
7126 if (err)
7127 goto done;
7128 done:
7129 free(tmp_branch_name);
7130 free(base_commit_ref_name);
7131 free(branch_ref_name);
7132 free(commit_ref_name);
7133 return err;
7136 const struct got_error *
7137 got_worktree_histedit_abort(struct got_worktree *worktree,
7138 struct got_fileindex *fileindex, struct got_repository *repo,
7139 struct got_reference *branch, struct got_object_id *base_commit_id,
7140 got_worktree_checkout_cb progress_cb, void *progress_arg)
7142 const struct got_error *err, *unlockerr, *sync_err;
7143 struct got_reference *resolved = NULL;
7144 char *fileindex_path = NULL;
7145 struct got_object_id *tree_id = NULL;
7146 struct revert_file_args rfa;
7148 err = lock_worktree(worktree, LOCK_EX);
7149 if (err)
7150 return err;
7152 err = got_ref_open(&resolved, repo,
7153 got_ref_get_symref_target(branch), 0);
7154 if (err)
7155 goto done;
7157 err = got_worktree_set_head_ref(worktree, resolved);
7158 if (err)
7159 goto done;
7161 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7162 if (err)
7163 goto done;
7165 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7166 worktree->path_prefix);
7167 if (err)
7168 goto done;
7170 err = delete_histedit_refs(worktree, repo);
7171 if (err)
7172 goto done;
7174 err = get_fileindex_path(&fileindex_path, worktree);
7175 if (err)
7176 goto done;
7178 rfa.worktree = worktree;
7179 rfa.fileindex = fileindex;
7180 rfa.progress_cb = progress_cb;
7181 rfa.progress_arg = progress_arg;
7182 rfa.patch_cb = NULL;
7183 rfa.patch_arg = NULL;
7184 rfa.repo = repo;
7185 rfa.unlink_added_files = 0;
7186 err = worktree_status(worktree, "", fileindex, repo,
7187 revert_file, &rfa, NULL, NULL, 0, 0);
7188 if (err)
7189 goto sync;
7191 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7192 repo, progress_cb, progress_arg, NULL, NULL);
7193 sync:
7194 sync_err = sync_fileindex(fileindex, fileindex_path);
7195 if (sync_err && err == NULL)
7196 err = sync_err;
7197 done:
7198 got_ref_close(resolved);
7199 free(tree_id);
7200 free(fileindex_path);
7202 unlockerr = lock_worktree(worktree, LOCK_SH);
7203 if (unlockerr && err == NULL)
7204 err = unlockerr;
7205 return err;
7208 const struct got_error *
7209 got_worktree_histedit_complete(struct got_worktree *worktree,
7210 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7211 struct got_reference *edited_branch, struct got_repository *repo)
7213 const struct got_error *err, *unlockerr, *sync_err;
7214 struct got_object_id *new_head_commit_id = NULL;
7215 struct got_reference *resolved = NULL;
7216 char *fileindex_path = NULL;
7218 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7219 if (err)
7220 return err;
7222 err = got_ref_open(&resolved, repo,
7223 got_ref_get_symref_target(edited_branch), 0);
7224 if (err)
7225 goto done;
7227 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7228 resolved, new_head_commit_id, repo);
7229 if (err)
7230 goto done;
7232 err = got_ref_change_ref(resolved, new_head_commit_id);
7233 if (err)
7234 goto done;
7236 err = got_ref_write(resolved, repo);
7237 if (err)
7238 goto done;
7240 err = got_worktree_set_head_ref(worktree, resolved);
7241 if (err)
7242 goto done;
7244 err = delete_histedit_refs(worktree, repo);
7245 if (err)
7246 goto done;
7248 err = get_fileindex_path(&fileindex_path, worktree);
7249 if (err)
7250 goto done;
7251 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7252 sync_err = sync_fileindex(fileindex, fileindex_path);
7253 if (sync_err && err == NULL)
7254 err = sync_err;
7255 done:
7256 got_fileindex_free(fileindex);
7257 free(fileindex_path);
7258 free(new_head_commit_id);
7259 unlockerr = lock_worktree(worktree, LOCK_SH);
7260 if (unlockerr && err == NULL)
7261 err = unlockerr;
7262 return err;
7265 const struct got_error *
7266 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7267 struct got_object_id *commit_id, struct got_repository *repo)
7269 const struct got_error *err;
7270 char *commit_ref_name;
7272 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7273 if (err)
7274 return err;
7276 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7277 if (err)
7278 goto done;
7280 err = delete_ref(commit_ref_name, repo);
7281 done:
7282 free(commit_ref_name);
7283 return err;
7286 const struct got_error *
7287 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7288 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7289 struct got_worktree *worktree, const char *refname,
7290 struct got_repository *repo)
7292 const struct got_error *err = NULL;
7293 char *fileindex_path = NULL;
7294 struct check_rebase_ok_arg ok_arg;
7296 *fileindex = NULL;
7297 *branch_ref = NULL;
7298 *base_branch_ref = NULL;
7300 err = lock_worktree(worktree, LOCK_EX);
7301 if (err)
7302 return err;
7304 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7305 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7306 "cannot integrate a branch into itself; "
7307 "update -b or different branch name required");
7308 goto done;
7311 err = open_fileindex(fileindex, &fileindex_path, worktree);
7312 if (err)
7313 goto done;
7315 /* Preconditions are the same as for rebase. */
7316 ok_arg.worktree = worktree;
7317 ok_arg.repo = repo;
7318 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7319 &ok_arg);
7320 if (err)
7321 goto done;
7323 err = got_ref_open(branch_ref, repo, refname, 1);
7324 if (err)
7325 goto done;
7327 err = got_ref_open(base_branch_ref, repo,
7328 got_worktree_get_head_ref_name(worktree), 1);
7329 done:
7330 if (err) {
7331 if (*branch_ref) {
7332 got_ref_close(*branch_ref);
7333 *branch_ref = NULL;
7335 if (*base_branch_ref) {
7336 got_ref_close(*base_branch_ref);
7337 *base_branch_ref = NULL;
7339 if (*fileindex) {
7340 got_fileindex_free(*fileindex);
7341 *fileindex = NULL;
7343 lock_worktree(worktree, LOCK_SH);
7345 return err;
7348 const struct got_error *
7349 got_worktree_integrate_continue(struct got_worktree *worktree,
7350 struct got_fileindex *fileindex, struct got_repository *repo,
7351 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7352 got_worktree_checkout_cb progress_cb, void *progress_arg,
7353 got_cancel_cb cancel_cb, void *cancel_arg)
7355 const struct got_error *err = NULL, *sync_err, *unlockerr;
7356 char *fileindex_path = NULL;
7357 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7359 err = get_fileindex_path(&fileindex_path, worktree);
7360 if (err)
7361 goto done;
7363 err = got_ref_resolve(&commit_id, repo, branch_ref);
7364 if (err)
7365 goto done;
7367 err = got_object_id_by_path(&tree_id, repo, commit_id,
7368 worktree->path_prefix);
7369 if (err)
7370 goto done;
7372 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7373 if (err)
7374 goto done;
7376 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7377 progress_cb, progress_arg, cancel_cb, cancel_arg);
7378 if (err)
7379 goto sync;
7381 err = got_ref_change_ref(base_branch_ref, commit_id);
7382 if (err)
7383 goto sync;
7385 err = got_ref_write(base_branch_ref, repo);
7386 if (err)
7387 goto sync;
7389 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7390 sync:
7391 sync_err = sync_fileindex(fileindex, fileindex_path);
7392 if (sync_err && err == NULL)
7393 err = sync_err;
7395 done:
7396 unlockerr = got_ref_unlock(branch_ref);
7397 if (unlockerr && err == NULL)
7398 err = unlockerr;
7399 got_ref_close(branch_ref);
7401 unlockerr = got_ref_unlock(base_branch_ref);
7402 if (unlockerr && err == NULL)
7403 err = unlockerr;
7404 got_ref_close(base_branch_ref);
7406 got_fileindex_free(fileindex);
7407 free(fileindex_path);
7408 free(tree_id);
7410 unlockerr = lock_worktree(worktree, LOCK_SH);
7411 if (unlockerr && err == NULL)
7412 err = unlockerr;
7413 return err;
7416 const struct got_error *
7417 got_worktree_integrate_abort(struct got_worktree *worktree,
7418 struct got_fileindex *fileindex, struct got_repository *repo,
7419 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7421 const struct got_error *err = NULL, *unlockerr = NULL;
7423 got_fileindex_free(fileindex);
7425 err = lock_worktree(worktree, LOCK_SH);
7427 unlockerr = got_ref_unlock(branch_ref);
7428 if (unlockerr && err == NULL)
7429 err = unlockerr;
7430 got_ref_close(branch_ref);
7432 unlockerr = got_ref_unlock(base_branch_ref);
7433 if (unlockerr && err == NULL)
7434 err = unlockerr;
7435 got_ref_close(base_branch_ref);
7437 return err;
7440 const struct got_error *
7441 got_worktree_merge_postpone(struct got_worktree *worktree,
7442 struct got_fileindex *fileindex)
7444 const struct got_error *err, *sync_err;
7445 char *fileindex_path = NULL;
7447 err = get_fileindex_path(&fileindex_path, worktree);
7448 if (err)
7449 goto done;
7451 sync_err = sync_fileindex(fileindex, fileindex_path);
7453 err = lock_worktree(worktree, LOCK_SH);
7454 if (sync_err && err == NULL)
7455 err = sync_err;
7456 done:
7457 got_fileindex_free(fileindex);
7458 free(fileindex_path);
7459 return err;
7462 static const struct got_error *
7463 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7465 const struct got_error *err;
7466 char *branch_refname = NULL, *commit_refname = NULL;
7468 err = get_merge_branch_ref_name(&branch_refname, worktree);
7469 if (err)
7470 goto done;
7471 err = delete_ref(branch_refname, repo);
7472 if (err)
7473 goto done;
7475 err = get_merge_commit_ref_name(&commit_refname, worktree);
7476 if (err)
7477 goto done;
7478 err = delete_ref(commit_refname, repo);
7479 if (err)
7480 goto done;
7482 done:
7483 free(branch_refname);
7484 free(commit_refname);
7485 return err;
7488 struct merge_commit_msg_arg {
7489 struct got_worktree *worktree;
7490 const char *branch_name;
7493 static const struct got_error *
7494 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7495 void *arg)
7497 struct merge_commit_msg_arg *a = arg;
7499 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7500 got_worktree_get_head_ref_name(a->worktree)) == -1)
7501 return got_error_from_errno("asprintf");
7503 return NULL;
7507 const struct got_error *
7508 got_worktree_merge_branch(struct got_worktree *worktree,
7509 struct got_fileindex *fileindex,
7510 struct got_object_id *yca_commit_id,
7511 struct got_object_id *branch_tip,
7512 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7513 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7515 const struct got_error *err;
7516 char *fileindex_path = NULL;
7518 err = get_fileindex_path(&fileindex_path, worktree);
7519 if (err)
7520 goto done;
7522 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7523 worktree);
7524 if (err)
7525 goto done;
7527 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7528 branch_tip, repo, progress_cb, progress_arg,
7529 cancel_cb, cancel_arg);
7530 done:
7531 free(fileindex_path);
7532 return err;
7535 const struct got_error *
7536 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7537 struct got_worktree *worktree, struct got_fileindex *fileindex,
7538 const char *author, const char *committer, int allow_bad_symlinks,
7539 struct got_object_id *branch_tip, const char *branch_name,
7540 struct got_repository *repo,
7541 got_worktree_status_cb status_cb, void *status_arg)
7544 const struct got_error *err = NULL, *sync_err;
7545 struct got_pathlist_head commitable_paths;
7546 struct collect_commitables_arg cc_arg;
7547 struct got_pathlist_entry *pe;
7548 struct got_reference *head_ref = NULL;
7549 struct got_object_id *head_commit_id = NULL;
7550 int have_staged_files = 0;
7551 struct merge_commit_msg_arg mcm_arg;
7552 char *fileindex_path = NULL;
7554 *new_commit_id = NULL;
7556 TAILQ_INIT(&commitable_paths);
7558 err = get_fileindex_path(&fileindex_path, worktree);
7559 if (err)
7560 goto done;
7562 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7563 if (err)
7564 goto done;
7566 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7567 if (err)
7568 goto done;
7570 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7571 &have_staged_files);
7572 if (err && err->code != GOT_ERR_CANCELLED)
7573 goto done;
7574 if (have_staged_files) {
7575 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7576 goto done;
7579 cc_arg.commitable_paths = &commitable_paths;
7580 cc_arg.worktree = worktree;
7581 cc_arg.fileindex = fileindex;
7582 cc_arg.repo = repo;
7583 cc_arg.have_staged_files = have_staged_files;
7584 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7585 err = worktree_status(worktree, "", fileindex, repo,
7586 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
7587 if (err)
7588 goto done;
7590 if (TAILQ_EMPTY(&commitable_paths)) {
7591 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7592 "merge of %s cannot proceed", branch_name);
7593 goto done;
7596 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7597 struct got_commitable *ct = pe->data;
7598 const char *ct_path = ct->in_repo_path;
7600 while (ct_path[0] == '/')
7601 ct_path++;
7602 err = check_out_of_date(ct_path, ct->status,
7603 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7604 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7605 if (err)
7606 goto done;
7610 mcm_arg.worktree = worktree;
7611 mcm_arg.branch_name = branch_name;
7612 err = commit_worktree(new_commit_id, &commitable_paths,
7613 head_commit_id, branch_tip, worktree, author, committer,
7614 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7615 if (err)
7616 goto done;
7618 err = update_fileindex_after_commit(worktree, &commitable_paths,
7619 *new_commit_id, fileindex, have_staged_files);
7620 sync_err = sync_fileindex(fileindex, fileindex_path);
7621 if (sync_err && err == NULL)
7622 err = sync_err;
7623 done:
7624 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7625 struct got_commitable *ct = pe->data;
7626 free_commitable(ct);
7628 got_pathlist_free(&commitable_paths);
7629 free(fileindex_path);
7630 return err;
7633 const struct got_error *
7634 got_worktree_merge_complete(struct got_worktree *worktree,
7635 struct got_fileindex *fileindex, struct got_repository *repo)
7637 const struct got_error *err, *unlockerr, *sync_err;
7638 char *fileindex_path = NULL;
7640 err = delete_merge_refs(worktree, repo);
7641 if (err)
7642 goto done;
7644 err = get_fileindex_path(&fileindex_path, worktree);
7645 if (err)
7646 goto done;
7647 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7648 sync_err = sync_fileindex(fileindex, fileindex_path);
7649 if (sync_err && err == NULL)
7650 err = sync_err;
7651 done:
7652 got_fileindex_free(fileindex);
7653 free(fileindex_path);
7654 unlockerr = lock_worktree(worktree, LOCK_SH);
7655 if (unlockerr && err == NULL)
7656 err = unlockerr;
7657 return err;
7660 const struct got_error *
7661 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7662 struct got_repository *repo)
7664 const struct got_error *err;
7665 char *branch_refname = NULL;
7666 struct got_reference *branch_ref = NULL;
7668 *in_progress = 0;
7670 err = get_merge_branch_ref_name(&branch_refname, worktree);
7671 if (err)
7672 return err;
7673 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7674 free(branch_refname);
7675 if (err) {
7676 if (err->code != GOT_ERR_NOT_REF)
7677 return err;
7678 } else
7679 *in_progress = 1;
7681 return NULL;
7684 const struct got_error *got_worktree_merge_prepare(
7685 struct got_fileindex **fileindex, struct got_worktree *worktree,
7686 struct got_reference *branch, struct got_repository *repo)
7688 const struct got_error *err = NULL;
7689 char *fileindex_path = NULL;
7690 char *branch_refname = NULL, *commit_refname = NULL;
7691 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7692 struct got_reference *commit_ref = NULL;
7693 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7694 struct check_rebase_ok_arg ok_arg;
7696 *fileindex = NULL;
7698 err = lock_worktree(worktree, LOCK_EX);
7699 if (err)
7700 return err;
7702 err = open_fileindex(fileindex, &fileindex_path, worktree);
7703 if (err)
7704 goto done;
7706 /* Preconditions are the same as for rebase. */
7707 ok_arg.worktree = worktree;
7708 ok_arg.repo = repo;
7709 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7710 &ok_arg);
7711 if (err)
7712 goto done;
7714 err = get_merge_branch_ref_name(&branch_refname, worktree);
7715 if (err)
7716 return err;
7718 err = get_merge_commit_ref_name(&commit_refname, worktree);
7719 if (err)
7720 return err;
7722 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7723 0);
7724 if (err)
7725 goto done;
7727 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7728 if (err)
7729 goto done;
7731 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7732 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7733 goto done;
7736 err = got_ref_resolve(&branch_tip, repo, branch);
7737 if (err)
7738 goto done;
7740 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7741 if (err)
7742 goto done;
7743 err = got_ref_write(branch_ref, repo);
7744 if (err)
7745 goto done;
7747 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7748 if (err)
7749 goto done;
7750 err = got_ref_write(commit_ref, repo);
7751 if (err)
7752 goto done;
7754 done:
7755 free(branch_refname);
7756 free(commit_refname);
7757 free(fileindex_path);
7758 if (branch_ref)
7759 got_ref_close(branch_ref);
7760 if (commit_ref)
7761 got_ref_close(commit_ref);
7762 if (wt_branch)
7763 got_ref_close(wt_branch);
7764 free(wt_branch_tip);
7765 if (err) {
7766 if (*fileindex) {
7767 got_fileindex_free(*fileindex);
7768 *fileindex = NULL;
7770 lock_worktree(worktree, LOCK_SH);
7772 return err;
7775 const struct got_error *
7776 got_worktree_merge_continue(char **branch_name,
7777 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7778 struct got_worktree *worktree, struct got_repository *repo)
7780 const struct got_error *err;
7781 char *commit_refname = NULL, *branch_refname = NULL;
7782 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7783 char *fileindex_path = NULL;
7784 int have_staged_files = 0;
7786 *branch_name = NULL;
7787 *branch_tip = NULL;
7788 *fileindex = NULL;
7790 err = lock_worktree(worktree, LOCK_EX);
7791 if (err)
7792 return err;
7794 err = open_fileindex(fileindex, &fileindex_path, worktree);
7795 if (err)
7796 goto done;
7798 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7799 &have_staged_files);
7800 if (err && err->code != GOT_ERR_CANCELLED)
7801 goto done;
7802 if (have_staged_files) {
7803 err = got_error(GOT_ERR_STAGED_PATHS);
7804 goto done;
7807 err = get_merge_branch_ref_name(&branch_refname, worktree);
7808 if (err)
7809 goto done;
7811 err = get_merge_commit_ref_name(&commit_refname, worktree);
7812 if (err)
7813 goto done;
7815 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7816 if (err)
7817 goto done;
7819 if (!got_ref_is_symbolic(branch_ref)) {
7820 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7821 "%s is not a symbolic reference",
7822 got_ref_get_name(branch_ref));
7823 goto done;
7825 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7826 if (*branch_name == NULL) {
7827 err = got_error_from_errno("strdup");
7828 goto done;
7831 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7832 if (err)
7833 goto done;
7835 err = got_ref_resolve(branch_tip, repo, commit_ref);
7836 if (err)
7837 goto done;
7838 done:
7839 free(commit_refname);
7840 free(branch_refname);
7841 free(fileindex_path);
7842 if (commit_ref)
7843 got_ref_close(commit_ref);
7844 if (branch_ref)
7845 got_ref_close(branch_ref);
7846 if (err) {
7847 if (*branch_name) {
7848 free(*branch_name);
7849 *branch_name = NULL;
7851 free(*branch_tip);
7852 *branch_tip = NULL;
7853 if (*fileindex) {
7854 got_fileindex_free(*fileindex);
7855 *fileindex = NULL;
7857 lock_worktree(worktree, LOCK_SH);
7859 return err;
7862 const struct got_error *
7863 got_worktree_merge_abort(struct got_worktree *worktree,
7864 struct got_fileindex *fileindex, struct got_repository *repo,
7865 got_worktree_checkout_cb progress_cb, void *progress_arg)
7867 const struct got_error *err, *unlockerr, *sync_err;
7868 struct got_object_id *commit_id = NULL;
7869 char *fileindex_path = NULL;
7870 struct revert_file_args rfa;
7871 struct got_object_id *tree_id = NULL;
7873 err = got_object_id_by_path(&tree_id, repo,
7874 worktree->base_commit_id, worktree->path_prefix);
7875 if (err)
7876 goto done;
7878 err = delete_merge_refs(worktree, repo);
7879 if (err)
7880 goto done;
7882 err = get_fileindex_path(&fileindex_path, worktree);
7883 if (err)
7884 goto done;
7886 rfa.worktree = worktree;
7887 rfa.fileindex = fileindex;
7888 rfa.progress_cb = progress_cb;
7889 rfa.progress_arg = progress_arg;
7890 rfa.patch_cb = NULL;
7891 rfa.patch_arg = NULL;
7892 rfa.repo = repo;
7893 rfa.unlink_added_files = 1;
7894 err = worktree_status(worktree, "", fileindex, repo,
7895 revert_file, &rfa, NULL, NULL, 0, 0);
7896 if (err)
7897 goto sync;
7899 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7900 repo, progress_cb, progress_arg, NULL, NULL);
7901 sync:
7902 sync_err = sync_fileindex(fileindex, fileindex_path);
7903 if (sync_err && err == NULL)
7904 err = sync_err;
7905 done:
7906 free(tree_id);
7907 free(commit_id);
7908 if (fileindex)
7909 got_fileindex_free(fileindex);
7910 free(fileindex_path);
7912 unlockerr = lock_worktree(worktree, LOCK_SH);
7913 if (unlockerr && err == NULL)
7914 err = unlockerr;
7915 return err;
7918 struct check_stage_ok_arg {
7919 struct got_object_id *head_commit_id;
7920 struct got_worktree *worktree;
7921 struct got_fileindex *fileindex;
7922 struct got_repository *repo;
7923 int have_changes;
7926 const struct got_error *
7927 check_stage_ok(void *arg, unsigned char status,
7928 unsigned char staged_status, const char *relpath,
7929 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7930 struct got_object_id *commit_id, int dirfd, const char *de_name)
7932 struct check_stage_ok_arg *a = arg;
7933 const struct got_error *err = NULL;
7934 struct got_fileindex_entry *ie;
7935 struct got_object_id base_commit_id;
7936 struct got_object_id *base_commit_idp = NULL;
7937 char *in_repo_path = NULL, *p;
7939 if (status == GOT_STATUS_UNVERSIONED ||
7940 status == GOT_STATUS_NO_CHANGE)
7941 return NULL;
7942 if (status == GOT_STATUS_NONEXISTENT)
7943 return got_error_set_errno(ENOENT, relpath);
7945 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7946 if (ie == NULL)
7947 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7949 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7950 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7951 relpath) == -1)
7952 return got_error_from_errno("asprintf");
7954 if (got_fileindex_entry_has_commit(ie)) {
7955 memcpy(base_commit_id.sha1, ie->commit_sha1,
7956 SHA1_DIGEST_LENGTH);
7957 base_commit_idp = &base_commit_id;
7960 if (status == GOT_STATUS_CONFLICT) {
7961 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7962 goto done;
7963 } else if (status != GOT_STATUS_ADD &&
7964 status != GOT_STATUS_MODIFY &&
7965 status != GOT_STATUS_DELETE) {
7966 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7967 goto done;
7970 a->have_changes = 1;
7972 p = in_repo_path;
7973 while (p[0] == '/')
7974 p++;
7975 err = check_out_of_date(p, status, staged_status,
7976 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7977 GOT_ERR_STAGE_OUT_OF_DATE);
7978 done:
7979 free(in_repo_path);
7980 return err;
7983 struct stage_path_arg {
7984 struct got_worktree *worktree;
7985 struct got_fileindex *fileindex;
7986 struct got_repository *repo;
7987 got_worktree_status_cb status_cb;
7988 void *status_arg;
7989 got_worktree_patch_cb patch_cb;
7990 void *patch_arg;
7991 int staged_something;
7992 int allow_bad_symlinks;
7995 static const struct got_error *
7996 stage_path(void *arg, unsigned char status,
7997 unsigned char staged_status, const char *relpath,
7998 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7999 struct got_object_id *commit_id, int dirfd, const char *de_name)
8001 struct stage_path_arg *a = arg;
8002 const struct got_error *err = NULL;
8003 struct got_fileindex_entry *ie;
8004 char *ondisk_path = NULL, *path_content = NULL;
8005 uint32_t stage;
8006 struct got_object_id *new_staged_blob_id = NULL;
8007 struct stat sb;
8009 if (status == GOT_STATUS_UNVERSIONED)
8010 return NULL;
8012 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8013 if (ie == NULL)
8014 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8016 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8017 relpath)== -1)
8018 return got_error_from_errno("asprintf");
8020 switch (status) {
8021 case GOT_STATUS_ADD:
8022 case GOT_STATUS_MODIFY:
8023 /* XXX could sb.st_mode be passed in by our caller? */
8024 if (lstat(ondisk_path, &sb) == -1) {
8025 err = got_error_from_errno2("lstat", ondisk_path);
8026 break;
8028 if (a->patch_cb) {
8029 if (status == GOT_STATUS_ADD) {
8030 int choice = GOT_PATCH_CHOICE_NONE;
8031 err = (*a->patch_cb)(&choice, a->patch_arg,
8032 status, ie->path, NULL, 1, 1);
8033 if (err)
8034 break;
8035 if (choice != GOT_PATCH_CHOICE_YES)
8036 break;
8037 } else {
8038 err = create_patched_content(&path_content, 0,
8039 staged_blob_id ? staged_blob_id : blob_id,
8040 ondisk_path, dirfd, de_name, ie->path,
8041 a->repo, a->patch_cb, a->patch_arg);
8042 if (err || path_content == NULL)
8043 break;
8046 err = got_object_blob_create(&new_staged_blob_id,
8047 path_content ? path_content : ondisk_path, a->repo);
8048 if (err)
8049 break;
8050 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8051 SHA1_DIGEST_LENGTH);
8052 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8053 stage = GOT_FILEIDX_STAGE_ADD;
8054 else
8055 stage = GOT_FILEIDX_STAGE_MODIFY;
8056 got_fileindex_entry_stage_set(ie, stage);
8057 if (S_ISLNK(sb.st_mode)) {
8058 int is_bad_symlink = 0;
8059 if (!a->allow_bad_symlinks) {
8060 char target_path[PATH_MAX];
8061 ssize_t target_len;
8062 target_len = readlink(ondisk_path, target_path,
8063 sizeof(target_path));
8064 if (target_len == -1) {
8065 err = got_error_from_errno2("readlink",
8066 ondisk_path);
8067 break;
8069 err = is_bad_symlink_target(&is_bad_symlink,
8070 target_path, target_len, ondisk_path,
8071 a->worktree->root_path);
8072 if (err)
8073 break;
8074 if (is_bad_symlink) {
8075 err = got_error_path(ondisk_path,
8076 GOT_ERR_BAD_SYMLINK);
8077 break;
8080 if (is_bad_symlink)
8081 got_fileindex_entry_staged_filetype_set(ie,
8082 GOT_FILEIDX_MODE_BAD_SYMLINK);
8083 else
8084 got_fileindex_entry_staged_filetype_set(ie,
8085 GOT_FILEIDX_MODE_SYMLINK);
8086 } else {
8087 got_fileindex_entry_staged_filetype_set(ie,
8088 GOT_FILEIDX_MODE_REGULAR_FILE);
8090 a->staged_something = 1;
8091 if (a->status_cb == NULL)
8092 break;
8093 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8094 get_staged_status(ie), relpath, blob_id,
8095 new_staged_blob_id, NULL, dirfd, de_name);
8096 break;
8097 case GOT_STATUS_DELETE:
8098 if (staged_status == GOT_STATUS_DELETE)
8099 break;
8100 if (a->patch_cb) {
8101 int choice = GOT_PATCH_CHOICE_NONE;
8102 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8103 ie->path, NULL, 1, 1);
8104 if (err)
8105 break;
8106 if (choice == GOT_PATCH_CHOICE_NO)
8107 break;
8108 if (choice != GOT_PATCH_CHOICE_YES) {
8109 err = got_error(GOT_ERR_PATCH_CHOICE);
8110 break;
8113 stage = GOT_FILEIDX_STAGE_DELETE;
8114 got_fileindex_entry_stage_set(ie, stage);
8115 a->staged_something = 1;
8116 if (a->status_cb == NULL)
8117 break;
8118 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8119 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8120 de_name);
8121 break;
8122 case GOT_STATUS_NO_CHANGE:
8123 break;
8124 case GOT_STATUS_CONFLICT:
8125 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8126 break;
8127 case GOT_STATUS_NONEXISTENT:
8128 err = got_error_set_errno(ENOENT, relpath);
8129 break;
8130 default:
8131 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8132 break;
8135 if (path_content && unlink(path_content) == -1 && err == NULL)
8136 err = got_error_from_errno2("unlink", path_content);
8137 free(path_content);
8138 free(ondisk_path);
8139 free(new_staged_blob_id);
8140 return err;
8143 const struct got_error *
8144 got_worktree_stage(struct got_worktree *worktree,
8145 struct got_pathlist_head *paths,
8146 got_worktree_status_cb status_cb, void *status_arg,
8147 got_worktree_patch_cb patch_cb, void *patch_arg,
8148 int allow_bad_symlinks, struct got_repository *repo)
8150 const struct got_error *err = NULL, *sync_err, *unlockerr;
8151 struct got_pathlist_entry *pe;
8152 struct got_fileindex *fileindex = NULL;
8153 char *fileindex_path = NULL;
8154 struct got_reference *head_ref = NULL;
8155 struct got_object_id *head_commit_id = NULL;
8156 struct check_stage_ok_arg oka;
8157 struct stage_path_arg spa;
8159 err = lock_worktree(worktree, LOCK_EX);
8160 if (err)
8161 return err;
8163 err = got_ref_open(&head_ref, repo,
8164 got_worktree_get_head_ref_name(worktree), 0);
8165 if (err)
8166 goto done;
8167 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8168 if (err)
8169 goto done;
8170 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8171 if (err)
8172 goto done;
8174 /* Check pre-conditions before staging anything. */
8175 oka.head_commit_id = head_commit_id;
8176 oka.worktree = worktree;
8177 oka.fileindex = fileindex;
8178 oka.repo = repo;
8179 oka.have_changes = 0;
8180 TAILQ_FOREACH(pe, paths, entry) {
8181 err = worktree_status(worktree, pe->path, fileindex, repo,
8182 check_stage_ok, &oka, NULL, NULL, 0, 0);
8183 if (err)
8184 goto done;
8186 if (!oka.have_changes) {
8187 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8188 goto done;
8191 spa.worktree = worktree;
8192 spa.fileindex = fileindex;
8193 spa.repo = repo;
8194 spa.patch_cb = patch_cb;
8195 spa.patch_arg = patch_arg;
8196 spa.status_cb = status_cb;
8197 spa.status_arg = status_arg;
8198 spa.staged_something = 0;
8199 spa.allow_bad_symlinks = allow_bad_symlinks;
8200 TAILQ_FOREACH(pe, paths, entry) {
8201 err = worktree_status(worktree, pe->path, fileindex, repo,
8202 stage_path, &spa, NULL, NULL, 0, 0);
8203 if (err)
8204 goto done;
8206 if (!spa.staged_something) {
8207 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8208 goto done;
8211 sync_err = sync_fileindex(fileindex, fileindex_path);
8212 if (sync_err && err == NULL)
8213 err = sync_err;
8214 done:
8215 if (head_ref)
8216 got_ref_close(head_ref);
8217 free(head_commit_id);
8218 free(fileindex_path);
8219 if (fileindex)
8220 got_fileindex_free(fileindex);
8221 unlockerr = lock_worktree(worktree, LOCK_SH);
8222 if (unlockerr && err == NULL)
8223 err = unlockerr;
8224 return err;
8227 struct unstage_path_arg {
8228 struct got_worktree *worktree;
8229 struct got_fileindex *fileindex;
8230 struct got_repository *repo;
8231 got_worktree_checkout_cb progress_cb;
8232 void *progress_arg;
8233 got_worktree_patch_cb patch_cb;
8234 void *patch_arg;
8237 static const struct got_error *
8238 create_unstaged_content(char **path_unstaged_content,
8239 char **path_new_staged_content, struct got_object_id *blob_id,
8240 struct got_object_id *staged_blob_id, const char *relpath,
8241 struct got_repository *repo,
8242 got_worktree_patch_cb patch_cb, void *patch_arg)
8244 const struct got_error *err, *free_err;
8245 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8246 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8247 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8248 struct got_diffreg_result *diffreg_result = NULL;
8249 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8250 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8252 *path_unstaged_content = NULL;
8253 *path_new_staged_content = NULL;
8255 err = got_object_id_str(&label1, blob_id);
8256 if (err)
8257 return err;
8258 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8259 if (err)
8260 goto done;
8262 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8263 if (err)
8264 goto done;
8266 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8267 if (err)
8268 goto done;
8270 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8271 if (err)
8272 goto done;
8274 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8275 if (err)
8276 goto done;
8278 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8279 if (err)
8280 goto done;
8282 err = got_diff_files(&diffreg_result, f1, label1, f2,
8283 path2, 3, 0, 1, NULL);
8284 if (err)
8285 goto done;
8287 err = got_opentemp_named(path_unstaged_content, &outfile,
8288 "got-unstaged-content");
8289 if (err)
8290 goto done;
8291 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8292 "got-new-staged-content");
8293 if (err)
8294 goto done;
8296 if (fseek(f1, 0L, SEEK_SET) == -1) {
8297 err = got_ferror(f1, GOT_ERR_IO);
8298 goto done;
8300 if (fseek(f2, 0L, SEEK_SET) == -1) {
8301 err = got_ferror(f2, GOT_ERR_IO);
8302 goto done;
8304 /* Count the number of actual changes in the diff result. */
8305 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8306 struct diff_chunk_context cc = {};
8307 diff_chunk_context_load_change(&cc, &nchunks_used,
8308 diffreg_result->result, n, 0);
8309 nchanges++;
8311 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8312 int choice;
8313 err = apply_or_reject_change(&choice, &nchunks_used,
8314 diffreg_result->result, n, relpath, f1, f2,
8315 &line_cur1, &line_cur2,
8316 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8317 if (err)
8318 goto done;
8319 if (choice == GOT_PATCH_CHOICE_YES)
8320 have_content = 1;
8321 else
8322 have_rejected_content = 1;
8323 if (choice == GOT_PATCH_CHOICE_QUIT)
8324 break;
8326 if (have_content || have_rejected_content)
8327 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8328 outfile, rejectfile);
8329 done:
8330 free(label1);
8331 if (blob)
8332 got_object_blob_close(blob);
8333 if (staged_blob)
8334 got_object_blob_close(staged_blob);
8335 free_err = got_diffreg_result_free(diffreg_result);
8336 if (free_err && err == NULL)
8337 err = free_err;
8338 if (f1 && fclose(f1) == EOF && err == NULL)
8339 err = got_error_from_errno2("fclose", path1);
8340 if (f2 && fclose(f2) == EOF && err == NULL)
8341 err = got_error_from_errno2("fclose", path2);
8342 if (outfile && fclose(outfile) == EOF && err == NULL)
8343 err = got_error_from_errno2("fclose", *path_unstaged_content);
8344 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8345 err = got_error_from_errno2("fclose", *path_new_staged_content);
8346 if (path1 && unlink(path1) == -1 && err == NULL)
8347 err = got_error_from_errno2("unlink", path1);
8348 if (path2 && unlink(path2) == -1 && err == NULL)
8349 err = got_error_from_errno2("unlink", path2);
8350 if (err || !have_content) {
8351 if (*path_unstaged_content &&
8352 unlink(*path_unstaged_content) == -1 && err == NULL)
8353 err = got_error_from_errno2("unlink",
8354 *path_unstaged_content);
8355 free(*path_unstaged_content);
8356 *path_unstaged_content = NULL;
8358 if (err || !have_content || !have_rejected_content) {
8359 if (*path_new_staged_content &&
8360 unlink(*path_new_staged_content) == -1 && err == NULL)
8361 err = got_error_from_errno2("unlink",
8362 *path_new_staged_content);
8363 free(*path_new_staged_content);
8364 *path_new_staged_content = NULL;
8366 free(path1);
8367 free(path2);
8368 return err;
8371 static const struct got_error *
8372 unstage_hunks(struct got_object_id *staged_blob_id,
8373 struct got_blob_object *blob_base,
8374 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8375 const char *ondisk_path, const char *label_orig,
8376 struct got_worktree *worktree, struct got_repository *repo,
8377 got_worktree_patch_cb patch_cb, void *patch_arg,
8378 got_worktree_checkout_cb progress_cb, void *progress_arg)
8380 const struct got_error *err = NULL;
8381 char *path_unstaged_content = NULL;
8382 char *path_new_staged_content = NULL;
8383 char *parent = NULL, *base_path = NULL;
8384 char *blob_base_path = NULL;
8385 struct got_object_id *new_staged_blob_id = NULL;
8386 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8387 struct stat sb;
8389 err = create_unstaged_content(&path_unstaged_content,
8390 &path_new_staged_content, blob_id, staged_blob_id,
8391 ie->path, repo, patch_cb, patch_arg);
8392 if (err)
8393 return err;
8395 if (path_unstaged_content == NULL)
8396 return NULL;
8398 if (path_new_staged_content) {
8399 err = got_object_blob_create(&new_staged_blob_id,
8400 path_new_staged_content, repo);
8401 if (err)
8402 goto done;
8405 f = fopen(path_unstaged_content, "r");
8406 if (f == NULL) {
8407 err = got_error_from_errno2("fopen",
8408 path_unstaged_content);
8409 goto done;
8411 if (fstat(fileno(f), &sb) == -1) {
8412 err = got_error_from_errno2("fstat", path_unstaged_content);
8413 goto done;
8415 if (got_fileindex_entry_staged_filetype_get(ie) ==
8416 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8417 char link_target[PATH_MAX];
8418 size_t r;
8419 r = fread(link_target, 1, sizeof(link_target), f);
8420 if (r == 0 && ferror(f)) {
8421 err = got_error_from_errno("fread");
8422 goto done;
8424 if (r >= sizeof(link_target)) { /* should not happen */
8425 err = got_error(GOT_ERR_NO_SPACE);
8426 goto done;
8428 link_target[r] = '\0';
8429 err = merge_symlink(worktree, blob_base,
8430 ondisk_path, ie->path, label_orig, link_target,
8431 worktree->base_commit_id, repo, progress_cb,
8432 progress_arg);
8433 } else {
8434 int local_changes_subsumed;
8436 err = got_path_dirname(&parent, ondisk_path);
8437 if (err)
8438 return err;
8440 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8441 parent) == -1) {
8442 err = got_error_from_errno("asprintf");
8443 base_path = NULL;
8444 goto done;
8447 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8448 if (err)
8449 goto done;
8450 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8451 blob_base);
8452 if (err)
8453 goto done;
8456 * In order the run a 3-way merge with a symlink we copy the symlink's
8457 * target path into a temporary file and use that file with diff3.
8459 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8460 err = dump_symlink_target_path_to_file(&f_deriv2,
8461 ondisk_path);
8462 if (err)
8463 goto done;
8464 } else {
8465 int fd;
8466 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
8467 if (fd == -1) {
8468 err = got_error_from_errno2("open", ondisk_path);
8469 goto done;
8471 f_deriv2 = fdopen(fd, "r");
8472 if (f_deriv2 == NULL) {
8473 err = got_error_from_errno2("fdopen", ondisk_path);
8474 close(fd);
8475 goto done;
8479 err = merge_file(&local_changes_subsumed, worktree,
8480 f_base, f, f_deriv2, ondisk_path, ie->path,
8481 got_fileindex_perms_to_st(ie),
8482 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8483 repo, progress_cb, progress_arg);
8485 if (err)
8486 goto done;
8488 if (new_staged_blob_id) {
8489 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8490 SHA1_DIGEST_LENGTH);
8491 } else {
8492 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8493 got_fileindex_entry_staged_filetype_set(ie, 0);
8495 done:
8496 free(new_staged_blob_id);
8497 if (path_unstaged_content &&
8498 unlink(path_unstaged_content) == -1 && err == NULL)
8499 err = got_error_from_errno2("unlink", path_unstaged_content);
8500 if (path_new_staged_content &&
8501 unlink(path_new_staged_content) == -1 && err == NULL)
8502 err = got_error_from_errno2("unlink", path_new_staged_content);
8503 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8504 err = got_error_from_errno2("unlink", blob_base_path);
8505 if (f_base && fclose(f_base) == EOF && err == NULL)
8506 err = got_error_from_errno2("fclose", path_unstaged_content);
8507 if (f && fclose(f) == EOF && err == NULL)
8508 err = got_error_from_errno2("fclose", path_unstaged_content);
8509 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8510 err = got_error_from_errno2("fclose", ondisk_path);
8511 free(path_unstaged_content);
8512 free(path_new_staged_content);
8513 free(blob_base_path);
8514 free(parent);
8515 free(base_path);
8516 return err;
8519 static const struct got_error *
8520 unstage_path(void *arg, unsigned char status,
8521 unsigned char staged_status, const char *relpath,
8522 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8523 struct got_object_id *commit_id, int dirfd, const char *de_name)
8525 const struct got_error *err = NULL;
8526 struct unstage_path_arg *a = arg;
8527 struct got_fileindex_entry *ie;
8528 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8529 char *ondisk_path = NULL;
8530 char *id_str = NULL, *label_orig = NULL;
8531 int local_changes_subsumed;
8532 struct stat sb;
8534 if (staged_status != GOT_STATUS_ADD &&
8535 staged_status != GOT_STATUS_MODIFY &&
8536 staged_status != GOT_STATUS_DELETE)
8537 return NULL;
8539 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8540 if (ie == NULL)
8541 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8543 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8544 == -1)
8545 return got_error_from_errno("asprintf");
8547 err = got_object_id_str(&id_str,
8548 commit_id ? commit_id : a->worktree->base_commit_id);
8549 if (err)
8550 goto done;
8551 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8552 id_str) == -1) {
8553 err = got_error_from_errno("asprintf");
8554 goto done;
8557 switch (staged_status) {
8558 case GOT_STATUS_MODIFY:
8559 err = got_object_open_as_blob(&blob_base, a->repo,
8560 blob_id, 8192);
8561 if (err)
8562 break;
8563 /* fall through */
8564 case GOT_STATUS_ADD:
8565 if (a->patch_cb) {
8566 if (staged_status == GOT_STATUS_ADD) {
8567 int choice = GOT_PATCH_CHOICE_NONE;
8568 err = (*a->patch_cb)(&choice, a->patch_arg,
8569 staged_status, ie->path, NULL, 1, 1);
8570 if (err)
8571 break;
8572 if (choice != GOT_PATCH_CHOICE_YES)
8573 break;
8574 } else {
8575 err = unstage_hunks(staged_blob_id,
8576 blob_base, blob_id, ie, ondisk_path,
8577 label_orig, a->worktree, a->repo,
8578 a->patch_cb, a->patch_arg,
8579 a->progress_cb, a->progress_arg);
8580 break; /* Done with this file. */
8583 err = got_object_open_as_blob(&blob_staged, a->repo,
8584 staged_blob_id, 8192);
8585 if (err)
8586 break;
8587 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8588 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8589 case GOT_FILEIDX_MODE_REGULAR_FILE:
8590 err = merge_blob(&local_changes_subsumed, a->worktree,
8591 blob_base, ondisk_path, relpath,
8592 got_fileindex_perms_to_st(ie), label_orig,
8593 blob_staged, commit_id ? commit_id :
8594 a->worktree->base_commit_id, a->repo,
8595 a->progress_cb, a->progress_arg);
8596 break;
8597 case GOT_FILEIDX_MODE_SYMLINK:
8598 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8599 char *staged_target;
8600 err = got_object_blob_read_to_str(
8601 &staged_target, blob_staged);
8602 if (err)
8603 goto done;
8604 err = merge_symlink(a->worktree, blob_base,
8605 ondisk_path, relpath, label_orig,
8606 staged_target, commit_id ? commit_id :
8607 a->worktree->base_commit_id,
8608 a->repo, a->progress_cb, a->progress_arg);
8609 free(staged_target);
8610 } else {
8611 err = merge_blob(&local_changes_subsumed,
8612 a->worktree, blob_base, ondisk_path,
8613 relpath, got_fileindex_perms_to_st(ie),
8614 label_orig, blob_staged,
8615 commit_id ? commit_id :
8616 a->worktree->base_commit_id, a->repo,
8617 a->progress_cb, a->progress_arg);
8619 break;
8620 default:
8621 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8622 break;
8624 if (err == NULL) {
8625 got_fileindex_entry_stage_set(ie,
8626 GOT_FILEIDX_STAGE_NONE);
8627 got_fileindex_entry_staged_filetype_set(ie, 0);
8629 break;
8630 case GOT_STATUS_DELETE:
8631 if (a->patch_cb) {
8632 int choice = GOT_PATCH_CHOICE_NONE;
8633 err = (*a->patch_cb)(&choice, a->patch_arg,
8634 staged_status, ie->path, NULL, 1, 1);
8635 if (err)
8636 break;
8637 if (choice == GOT_PATCH_CHOICE_NO)
8638 break;
8639 if (choice != GOT_PATCH_CHOICE_YES) {
8640 err = got_error(GOT_ERR_PATCH_CHOICE);
8641 break;
8644 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8645 got_fileindex_entry_staged_filetype_set(ie, 0);
8646 err = get_file_status(&status, &sb, ie, ondisk_path,
8647 dirfd, de_name, a->repo);
8648 if (err)
8649 break;
8650 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8651 break;
8653 done:
8654 free(ondisk_path);
8655 if (blob_base)
8656 got_object_blob_close(blob_base);
8657 if (blob_staged)
8658 got_object_blob_close(blob_staged);
8659 free(id_str);
8660 free(label_orig);
8661 return err;
8664 const struct got_error *
8665 got_worktree_unstage(struct got_worktree *worktree,
8666 struct got_pathlist_head *paths,
8667 got_worktree_checkout_cb progress_cb, void *progress_arg,
8668 got_worktree_patch_cb patch_cb, void *patch_arg,
8669 struct got_repository *repo)
8671 const struct got_error *err = NULL, *sync_err, *unlockerr;
8672 struct got_pathlist_entry *pe;
8673 struct got_fileindex *fileindex = NULL;
8674 char *fileindex_path = NULL;
8675 struct unstage_path_arg upa;
8677 err = lock_worktree(worktree, LOCK_EX);
8678 if (err)
8679 return err;
8681 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8682 if (err)
8683 goto done;
8685 upa.worktree = worktree;
8686 upa.fileindex = fileindex;
8687 upa.repo = repo;
8688 upa.progress_cb = progress_cb;
8689 upa.progress_arg = progress_arg;
8690 upa.patch_cb = patch_cb;
8691 upa.patch_arg = patch_arg;
8692 TAILQ_FOREACH(pe, paths, entry) {
8693 err = worktree_status(worktree, pe->path, fileindex, repo,
8694 unstage_path, &upa, NULL, NULL, 0, 0);
8695 if (err)
8696 goto done;
8699 sync_err = sync_fileindex(fileindex, fileindex_path);
8700 if (sync_err && err == NULL)
8701 err = sync_err;
8702 done:
8703 free(fileindex_path);
8704 if (fileindex)
8705 got_fileindex_free(fileindex);
8706 unlockerr = lock_worktree(worktree, LOCK_SH);
8707 if (unlockerr && err == NULL)
8708 err = unlockerr;
8709 return err;
8712 struct report_file_info_arg {
8713 struct got_worktree *worktree;
8714 got_worktree_path_info_cb info_cb;
8715 void *info_arg;
8716 struct got_pathlist_head *paths;
8717 got_cancel_cb cancel_cb;
8718 void *cancel_arg;
8721 static const struct got_error *
8722 report_file_info(void *arg, struct got_fileindex_entry *ie)
8724 struct report_file_info_arg *a = arg;
8725 struct got_pathlist_entry *pe;
8726 struct got_object_id blob_id, staged_blob_id, commit_id;
8727 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8728 struct got_object_id *commit_idp = NULL;
8729 int stage;
8731 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8732 return got_error(GOT_ERR_CANCELLED);
8734 TAILQ_FOREACH(pe, a->paths, entry) {
8735 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8736 got_path_is_child(ie->path, pe->path, pe->path_len))
8737 break;
8739 if (pe == NULL) /* not found */
8740 return NULL;
8742 if (got_fileindex_entry_has_blob(ie)) {
8743 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8744 blob_idp = &blob_id;
8746 stage = got_fileindex_entry_stage_get(ie);
8747 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8748 stage == GOT_FILEIDX_STAGE_ADD) {
8749 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8750 SHA1_DIGEST_LENGTH);
8751 staged_blob_idp = &staged_blob_id;
8754 if (got_fileindex_entry_has_commit(ie)) {
8755 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8756 commit_idp = &commit_id;
8759 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8760 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8763 const struct got_error *
8764 got_worktree_path_info(struct got_worktree *worktree,
8765 struct got_pathlist_head *paths,
8766 got_worktree_path_info_cb info_cb, void *info_arg,
8767 got_cancel_cb cancel_cb, void *cancel_arg)
8770 const struct got_error *err = NULL, *unlockerr;
8771 struct got_fileindex *fileindex = NULL;
8772 char *fileindex_path = NULL;
8773 struct report_file_info_arg arg;
8775 err = lock_worktree(worktree, LOCK_SH);
8776 if (err)
8777 return err;
8779 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8780 if (err)
8781 goto done;
8783 arg.worktree = worktree;
8784 arg.info_cb = info_cb;
8785 arg.info_arg = info_arg;
8786 arg.paths = paths;
8787 arg.cancel_cb = cancel_cb;
8788 arg.cancel_arg = cancel_arg;
8789 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8790 &arg);
8791 done:
8792 free(fileindex_path);
8793 if (fileindex)
8794 got_fileindex_free(fileindex);
8795 unlockerr = lock_worktree(worktree, LOCK_UN);
8796 if (unlockerr && err == NULL)
8797 err = unlockerr;
8798 return err;