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"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
68 {
69 const struct got_error *err = NULL;
70 char *path;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
76 free(path);
77 return err;
78 }
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
82 {
83 const struct got_error *err = NULL;
84 FILE *tmpfile = NULL;
85 char *tmppath = NULL;
86 char *path = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
90 path = NULL;
91 goto done;
92 }
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
95 if (err)
96 goto done;
98 if (content) {
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
102 goto done;
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
108 unlink(tmppath);
109 goto done;
112 done:
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
115 free(tmppath);
116 return err;
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
123 char *path;
124 int fd = -1;
125 ssize_t n;
126 struct stat sb;
128 *content = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
132 path = NULL;
133 goto done;
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
137 if (fd == -1) {
138 if (errno == ENOENT)
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
140 else
141 err = got_error_from_errno2("open", path);
142 goto done;
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
147 goto done;
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
152 goto done;
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
157 goto done;
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
164 goto done;
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
168 goto done;
170 (*content)[sb.st_size - 1] = '\0';
172 done:
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
175 free(path);
176 if (err) {
177 free(*content);
178 *content = NULL;
180 return err;
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
187 char *refstr = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
191 if (refstr == NULL)
192 return got_error_from_errno("got_ref_to_str");
193 } else {
194 refstr = strdup(got_ref_get_name(head_ref));
195 if (refstr == NULL)
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 free(refstr);
200 return err;
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
209 uuid_t uuid;
210 uint32_t uuid_status;
211 int obj_type;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
220 goto done;
223 err = got_ref_resolve(&commit_id, repo, head_ref);
224 if (err)
225 return err;
226 err = got_object_get_type(&obj_type, repo, commit_id);
227 if (err)
228 return err;
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
240 goto done;
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
246 goto done;
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
250 goto done;
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
255 if (err)
256 goto done;
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
260 if (err)
261 goto done;
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
265 if (err)
266 goto done;
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
270 if (err)
271 goto done;
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 if (err)
274 goto done;
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
279 if (err)
280 goto done;
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
285 if (err)
286 goto done;
288 /* Generate UUID. */
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
292 goto done;
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
297 goto done;
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 if (err)
301 goto done;
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
306 goto done;
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
309 if (err)
310 goto done;
312 done:
313 free(commit_id);
314 free(path_got);
315 free(formatstr);
316 free(absprefix);
317 free(basestr);
318 free(uuidstr);
319 return err;
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
326 char *path_got;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
332 const char *errstr;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
336 *worktree = NULL;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
340 path_got = NULL;
341 goto done;
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
346 path_lock = NULL;
347 goto done;
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 if (fd == -1) {
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
354 goto done;
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 if (err)
359 goto done;
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 if (errstr) {
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
365 goto done;
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
369 goto done;
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
375 goto done;
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = realpath(path, NULL);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno2("realpath", path);
382 goto done;
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
386 if (err)
387 goto done;
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
391 if (err)
392 goto done;
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
396 if (err)
397 goto done;
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 if (err)
401 goto done;
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
405 goto done;
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
409 if (err)
410 goto done;
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 base_commit_id_str);
414 if (err)
415 goto done;
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
419 done:
420 if (repo)
421 got_repo_close(repo);
422 free(path_got);
423 free(path_lock);
424 free(base_commit_id_str);
425 free(uuidstr);
426 free(formatstr);
427 if (err) {
428 if (fd != -1)
429 close(fd);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
432 *worktree = NULL;
433 } else
434 (*worktree)->lockfd = fd;
436 return err;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
444 do {
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 return err;
448 if (*worktree)
449 return NULL;
450 path = dirname(path);
451 if (path == NULL)
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
471 free(worktree);
472 return err;
475 const char *
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
481 const char *
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
487 const char *
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
505 free(absprefix);
506 return NULL;
509 const char *
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
525 path_got = NULL;
526 goto done;
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
532 goto done;
535 err = write_head_ref(path_got, head_ref);
536 if (err)
537 goto done;
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
541 done:
542 free(path_got);
543 if (err)
544 free(head_ref_name);
545 return err;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
560 char *id_str = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
566 path_got = NULL;
567 goto done;
570 err = got_object_open(&obj, repo, commit_id);
571 if (err)
572 return err;
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
576 goto done;
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
581 if (err)
582 goto done;
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
584 if (err)
585 goto done;
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
591 goto done;
593 done:
594 if (obj)
595 got_object_close(obj);
596 free(id_str);
597 free(path_got);
598 return err;
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
608 return NULL;
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
615 char *abspath;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 struct stat sb;
623 err = NULL;
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
631 free(abspath);
632 return err;
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
639 uint8_t fbuf1[8192];
640 uint8_t fbuf2[8192];
641 size_t flen1 = 0, flen2 = 0;
643 *same = 1;
645 for (;;) {
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
649 break;
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
654 break;
656 if (flen1 == 0) {
657 if (flen2 != 0)
658 *same = 0;
659 break;
660 } else if (flen2 == 0) {
661 if (flen1 != 0)
662 *same = 0;
663 break;
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
666 *same = 0;
667 break;
669 } else {
670 *same = 0;
671 break;
675 return err;
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
682 struct stat sb;
683 size_t size1, size2;
684 FILE *f1 = NULL, *f2 = NULL;
686 *same = 1;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
690 goto done;
692 size1 = sb.st_size;
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
696 goto done;
698 size2 = sb.st_size;
700 if (size1 != size2) {
701 *same = 0;
702 return NULL;
705 f1 = fopen(f1_path, "r");
706 if (f1 == NULL)
707 return got_error_from_errno2("fopen", f1_path);
709 f2 = fopen(f2_path, "r");
710 if (f2 == NULL) {
711 err = got_error_from_errno2("fopen", f2_path);
712 goto done;
715 err = check_file_contents_equal(same, f1, f2);
716 done:
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
722 return err;
725 /*
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
729 */
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
739 int merged_fd = -1;
740 FILE *f_orig = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
743 int overlapcnt = 0;
744 char *parent;
746 *local_changes_subsumed = 0;
748 parent = dirname(ondisk_path);
749 if (parent == NULL)
750 return got_error_from_errno2("dirname", ondisk_path);
752 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 return got_error_from_errno("asprintf");
755 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
756 if (err)
757 goto done;
759 free(base_path);
760 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 err = got_error_from_errno("asprintf");
762 base_path = NULL;
763 goto done;
766 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
767 if (err)
768 goto done;
769 if (blob_orig) {
770 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
771 blob_orig);
772 if (err)
773 goto done;
774 } else {
775 /*
776 * If the file has no blob, this is an "add vs add" conflict,
777 * and we simply use an empty ancestor file to make both files
778 * appear in the merged result in their entirety.
779 */
782 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
784 if (err)
785 goto done;
787 err = (*progress_cb)(progress_arg,
788 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
789 if (err)
790 goto done;
792 if (fsync(merged_fd) != 0) {
793 err = got_error_from_errno("fsync");
794 goto done;
797 /* Check if a clean merge has subsumed all local changes. */
798 if (overlapcnt == 0) {
799 err = check_files_equal(local_changes_subsumed, deriv_path,
800 merged_path);
801 if (err)
802 goto done;
805 if (fchmod(merged_fd, st_mode) != 0) {
806 err = got_error_from_errno2("fchmod", merged_path);
807 goto done;
810 if (rename(merged_path, ondisk_path) != 0) {
811 err = got_error_from_errno3("rename", merged_path,
812 ondisk_path);
813 goto done;
815 done:
816 if (err) {
817 if (merged_path)
818 unlink(merged_path);
820 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
821 err = got_error_from_errno("close");
822 if (f_orig && fclose(f_orig) != 0 && err == NULL)
823 err = got_error_from_errno("fclose");
824 free(merged_path);
825 free(base_path);
826 if (blob_orig_path) {
827 unlink(blob_orig_path);
828 free(blob_orig_path);
830 return err;
833 static const struct got_error *
834 update_symlink(const char *ondisk_path, const char *target_path,
835 size_t target_len)
837 /* This is not atomic but matches what 'ln -sf' does. */
838 if (unlink(ondisk_path) == -1)
839 return got_error_from_errno2("unlink", ondisk_path);
840 if (symlink(target_path, ondisk_path) == -1)
841 return got_error_from_errno3("symlink", target_path,
842 ondisk_path);
843 return NULL;
846 /*
847 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
848 * in the work tree with a file that contains conflict markers and the
849 * conflicting target paths of the original version, a "derived version"
850 * of a symlink from an incoming change, and a local version of the symlink.
852 * The original versions's target path can be NULL if it is not available,
853 * such as if both derived versions added a new symlink at the same path.
855 * The incoming derived symlink target is NULL in case the incoming change
856 * has deleted this symlink.
857 */
858 static const struct got_error *
859 install_symlink_conflict(const char *deriv_target,
860 struct got_object_id *deriv_base_commit_id, const char *orig_target,
861 const char *label_orig, const char *local_target, const char *ondisk_path)
863 const struct got_error *err;
864 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
865 FILE *f = NULL;
867 err = got_object_id_str(&id_str, deriv_base_commit_id);
868 if (err)
869 return got_error_from_errno("asprintf");
871 if (asprintf(&label_deriv, "%s: commit %s",
872 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
873 err = got_error_from_errno("asprintf");
874 goto done;
877 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
878 if (err)
879 goto done;
881 if (fprintf(f, "%s: Could not install symbolic link because of merge "
882 "conflict.\nln(1) may be used to fix the situation. If this is "
883 "intended to be a\nregular file instead then its expected "
884 "contents may be filled in.\nThe following conflicting symlink "
885 "target paths were found:\n"
886 "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n", getprogname(),
887 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
888 deriv_target ? deriv_target : "(symlink was deleted)",
889 orig_target ? label_orig : "",
890 orig_target ? "\n" : "",
891 orig_target ? orig_target : "",
892 orig_target ? "\n" : "",
893 GOT_DIFF_CONFLICT_MARKER_SEP,
894 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
895 err = got_error_from_errno2("fprintf", path);
896 goto done;
899 if (unlink(ondisk_path) == -1) {
900 err = got_error_from_errno2("unlink", ondisk_path);
901 goto done;
903 if (rename(path, ondisk_path) == -1) {
904 err = got_error_from_errno3("rename", path, ondisk_path);
905 goto done;
907 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
908 err = got_error_from_errno2("chmod", ondisk_path);
909 goto done;
911 done:
912 if (f != NULL && fclose(f) == EOF && err == NULL)
913 err = got_error_from_errno2("fclose", path);
914 free(path);
915 free(id_str);
916 free(label_deriv);
917 return err;
920 /*
921 * Merge a symlink into the work tree, where blob_orig acts as the common
922 * ancestor, blob_deriv acts as the first derived version, and the symlink
923 * on disk acts as the second derived version.
924 * Assume that contents of both blobs represent symlinks.
925 */
926 static const struct got_error *
927 merge_symlink(struct got_worktree *worktree,
928 struct got_blob_object *blob_orig, const char *ondisk_path,
929 const char *path, uint16_t st_mode, const char *label_orig,
930 struct got_blob_object *blob_deriv,
931 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
932 got_worktree_checkout_cb progress_cb, void *progress_arg)
934 const struct got_error *err = NULL;
935 char *ancestor_target = NULL, *deriv_target = NULL;
936 struct stat sb;
937 ssize_t ondisk_len, deriv_len;
938 char ondisk_target[PATH_MAX];
939 int have_local_change = 0;
940 int have_incoming_change = 0;
942 if (lstat(ondisk_path, &sb) == -1)
943 return got_error_from_errno2("lstat", ondisk_path);
945 if (!S_ISLNK(sb.st_mode)) {
946 /* TODO symlink is obstructed; do something */
947 return got_error_path(ondisk_path, GOT_ERR_FILE_OBSTRUCTED);
950 ondisk_len = readlink(ondisk_path, ondisk_target,
951 sizeof(ondisk_target));
952 if (ondisk_len == -1) {
953 err = got_error_from_errno2("readlink",
954 ondisk_path);
955 goto done;
957 ondisk_target[ondisk_len] = '\0';
959 if (blob_orig) {
960 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
961 if (err)
962 goto done;
965 err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
966 if (err)
967 goto done;
969 if (ancestor_target == NULL ||
970 (ondisk_len != strlen(ancestor_target) ||
971 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
972 have_local_change = 1;
974 deriv_len = strlen(deriv_target);
975 if (ancestor_target == NULL ||
976 (deriv_len != strlen(ancestor_target) ||
977 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
978 have_incoming_change = 1;
980 if (!have_local_change && !have_incoming_change) {
981 if (ancestor_target) {
982 /* Both sides made the same change. */
983 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
984 path);
985 } else if (deriv_len == ondisk_len &&
986 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
987 /* Both sides added the same symlink. */
988 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
989 path);
990 } else {
991 /* Both sides added symlinks which don't match. */
992 err = install_symlink_conflict(deriv_target,
993 deriv_base_commit_id, ancestor_target,
994 label_orig, ondisk_target, ondisk_path);
995 if (err)
996 goto done;
997 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
998 path);
1000 } else if (!have_local_change && have_incoming_change) {
1001 /* Apply the incoming change. */
1002 err = update_symlink(ondisk_path, deriv_target,
1003 strlen(deriv_target));
1004 if (err)
1005 goto done;
1006 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1007 } else if (have_local_change && have_incoming_change) {
1008 err = install_symlink_conflict(deriv_target,
1009 deriv_base_commit_id, ancestor_target, label_orig,
1010 ondisk_target, ondisk_path);
1011 if (err)
1012 goto done;
1013 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1014 path);
1017 done:
1018 free(ancestor_target);
1019 free(deriv_target);
1020 return err;
1024 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1025 * blob_deriv acts as the first derived version, and the file on disk
1026 * acts as the second derived version.
1028 static const struct got_error *
1029 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1030 struct got_blob_object *blob_orig, const char *ondisk_path,
1031 const char *path, uint16_t st_mode, const char *label_orig,
1032 struct got_blob_object *blob_deriv,
1033 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1034 got_worktree_checkout_cb progress_cb, void *progress_arg)
1036 const struct got_error *err = NULL;
1037 FILE *f_deriv = NULL;
1038 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1039 char *label_deriv = NULL, *parent;
1041 *local_changes_subsumed = 0;
1043 parent = dirname(ondisk_path);
1044 if (parent == NULL)
1045 return got_error_from_errno2("dirname", ondisk_path);
1047 free(base_path);
1048 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1049 err = got_error_from_errno("asprintf");
1050 base_path = NULL;
1051 goto done;
1054 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1055 if (err)
1056 goto done;
1057 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1058 blob_deriv);
1059 if (err)
1060 goto done;
1062 err = got_object_id_str(&id_str, deriv_base_commit_id);
1063 if (err)
1064 goto done;
1065 if (asprintf(&label_deriv, "%s: commit %s",
1066 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1067 err = got_error_from_errno("asprintf");
1068 goto done;
1071 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1072 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1073 label_deriv, repo, progress_cb, progress_arg);
1074 done:
1075 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1076 err = got_error_from_errno("fclose");
1077 free(base_path);
1078 if (blob_deriv_path) {
1079 unlink(blob_deriv_path);
1080 free(blob_deriv_path);
1082 free(id_str);
1083 free(label_deriv);
1084 return err;
1087 static const struct got_error *
1088 create_fileindex_entry(struct got_fileindex *fileindex,
1089 struct got_object_id *base_commit_id, const char *ondisk_path,
1090 const char *path, struct got_object_id *blob_id)
1092 const struct got_error *err = NULL;
1093 struct got_fileindex_entry *new_ie;
1095 err = got_fileindex_entry_alloc(&new_ie, path);
1096 if (err)
1097 return err;
1099 err = got_fileindex_entry_update(new_ie, ondisk_path,
1100 blob_id->sha1, base_commit_id->sha1, 1);
1101 if (err)
1102 goto done;
1104 err = got_fileindex_entry_add(fileindex, new_ie);
1105 done:
1106 if (err)
1107 got_fileindex_entry_free(new_ie);
1108 return err;
1111 static mode_t
1112 get_ondisk_perms(int executable, mode_t st_mode)
1114 mode_t xbits = S_IXUSR;
1116 if (executable) {
1117 /* Map read bits to execute bits. */
1118 if (st_mode & S_IRGRP)
1119 xbits |= S_IXGRP;
1120 if (st_mode & S_IROTH)
1121 xbits |= S_IXOTH;
1122 return st_mode | xbits;
1125 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1128 /* forward declaration */
1129 static const struct got_error *
1130 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1131 const char *path, mode_t te_mode, mode_t st_mode,
1132 struct got_blob_object *blob, int restoring_missing_file,
1133 int reverting_versioned_file, int installing_bad_symlink,
1134 struct got_repository *repo,
1135 got_worktree_checkout_cb progress_cb, void *progress_arg);
1138 * This function assumes that the provided symlink target points at a
1139 * safe location in the work tree!
1141 static const struct got_error *
1142 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1143 size_t target_len)
1145 const struct got_error *err = NULL;
1146 ssize_t elen;
1147 char etarget[PATH_MAX];
1148 int fd;
1151 * "Bad" symlinks (those pointing outside the work tree or into the
1152 * .got directory) are installed in the work tree as a regular file
1153 * which contains the bad symlink target path.
1154 * The new symlink target has already been checked for safety by our
1155 * caller. If we can successfully open a regular file then we simply
1156 * replace this file with a symlink below.
1158 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1159 if (fd == -1) {
1160 if (errno != ELOOP)
1161 return got_error_from_errno2("open", ondisk_path);
1163 /* We are updating an existing on-disk symlink. */
1164 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1165 if (elen == -1)
1166 return got_error_from_errno2("readlink", ondisk_path);
1168 if (elen == target_len &&
1169 memcmp(etarget, target_path, target_len) == 0)
1170 return NULL; /* nothing to do */
1173 err = update_symlink(ondisk_path, target_path, target_len);
1174 if (fd != -1 && close(fd) == -1 && err == NULL)
1175 err = got_error_from_errno2("close", ondisk_path);
1176 return err;
1179 static const struct got_error *
1180 install_symlink(struct got_worktree *worktree, const char *ondisk_path,
1181 const char *path, mode_t te_mode, mode_t st_mode,
1182 struct got_blob_object *blob, int restoring_missing_file,
1183 int reverting_versioned_file, struct got_repository *repo,
1184 got_worktree_checkout_cb progress_cb, void *progress_arg)
1186 const struct got_error *err = NULL;
1187 char target_path[PATH_MAX];
1188 size_t len, target_len = 0;
1189 char *resolved_path = NULL, *abspath = NULL;
1190 char *path_got = NULL;
1191 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1192 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1195 * Blob object content specifies the target path of the link.
1196 * If a symbolic link cannot be installed we instead create
1197 * a regular file which contains the link target path stored
1198 * in the blob object.
1200 do {
1201 err = got_object_blob_read_block(&len, blob);
1202 if (len + target_len >= sizeof(target_path)) {
1203 /* Path too long; install as a regular file. */
1204 got_object_blob_rewind(blob);
1205 return install_blob(worktree, ondisk_path, path,
1206 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1207 restoring_missing_file, reverting_versioned_file,
1208 1, repo, progress_cb, progress_arg);
1210 if (len > 0) {
1211 /* Skip blob object header first time around. */
1212 memcpy(target_path + target_len, buf + hdrlen,
1213 len - hdrlen);
1214 target_len += len - hdrlen;
1215 hdrlen = 0;
1217 } while (len != 0);
1218 target_path[target_len] = '\0';
1221 * Relative symlink target lookup should begin at the directory
1222 * in which the blob object is being installed.
1224 if (!got_path_is_absolute(target_path)) {
1225 char *parent = dirname(ondisk_path);
1226 if (parent == NULL) {
1227 err = got_error_from_errno2("dirname", ondisk_path);
1228 goto done;
1230 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1231 err = got_error_from_errno("asprintf");
1232 goto done;
1237 * unveil(2) restricts our view of paths in the filesystem.
1238 * ENOENT will occur if a link target path does not exist or
1239 * if it points outside our unveiled path space.
1241 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1242 if (resolved_path == NULL) {
1243 if (errno != ENOENT) {
1244 err = got_error_from_errno2("realpath", target_path);
1245 goto done;
1249 /* Only allow symlinks pointing at paths within the work tree. */
1250 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1251 abspath : target_path), worktree->root_path,
1252 strlen(worktree->root_path))) {
1253 /* install as a regular file */
1254 got_object_blob_rewind(blob);
1255 err = install_blob(worktree, ondisk_path, path,
1256 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1257 restoring_missing_file, reverting_versioned_file, 1,
1258 repo, progress_cb, progress_arg);
1259 goto done;
1262 /* Do not allow symlinks pointing into the .got directory. */
1263 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1264 GOT_WORKTREE_GOT_DIR) == -1) {
1265 err = got_error_from_errno("asprintf");
1266 goto done;
1268 if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1269 abspath : target_path), path_got, strlen(path_got))) {
1270 /* install as a regular file */
1271 got_object_blob_rewind(blob);
1272 err = install_blob(worktree, ondisk_path, path,
1273 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1274 restoring_missing_file, reverting_versioned_file, 1,
1275 repo, progress_cb, progress_arg);
1276 goto done;
1279 if (symlink(target_path, ondisk_path) == -1) {
1280 if (errno == EEXIST) {
1281 err = replace_existing_symlink(ondisk_path,
1282 target_path, target_len);
1283 if (err)
1284 goto done;
1285 if (progress_cb) {
1286 err = (*progress_cb)(progress_arg,
1287 GOT_STATUS_UPDATE, path);
1289 goto done; /* Nothing else to do. */
1292 if (errno == ENOENT) {
1293 char *parent = dirname(ondisk_path);
1294 if (parent == NULL) {
1295 err = got_error_from_errno2("dirname",
1296 ondisk_path);
1297 goto done;
1299 err = add_dir_on_disk(worktree, parent);
1300 if (err)
1301 goto done;
1303 * Retry, and fall through to error handling
1304 * below if this second attempt fails.
1306 if (symlink(target_path, ondisk_path) != -1) {
1307 err = NULL; /* success */
1308 goto done;
1312 /* Handle errors from first or second creation attempt. */
1313 if (errno == ENAMETOOLONG) {
1314 /* bad target path; install as a regular file */
1315 got_object_blob_rewind(blob);
1316 err = install_blob(worktree, ondisk_path, path,
1317 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1318 restoring_missing_file, reverting_versioned_file, 1,
1319 repo, progress_cb, progress_arg);
1320 } else if (errno == ENOTDIR) {
1321 err = got_error_path(ondisk_path,
1322 GOT_ERR_FILE_OBSTRUCTED);
1323 } else {
1324 err = got_error_from_errno3("symlink",
1325 target_path, ondisk_path);
1327 } else if (progress_cb)
1328 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1329 done:
1330 free(resolved_path);
1331 free(abspath);
1332 free(path_got);
1333 return err;
1336 static const struct got_error *
1337 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1338 const char *path, mode_t te_mode, mode_t st_mode,
1339 struct got_blob_object *blob, int restoring_missing_file,
1340 int reverting_versioned_file, int installing_bad_symlink,
1341 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1342 void *progress_arg)
1344 const struct got_error *err = NULL;
1345 int fd = -1;
1346 size_t len, hdrlen;
1347 int update = 0;
1348 char *tmppath = NULL;
1350 if (S_ISLNK(te_mode))
1351 return install_symlink(worktree, ondisk_path, path, te_mode,
1352 st_mode, blob, restoring_missing_file,
1353 reverting_versioned_file, repo, progress_cb, progress_arg);
1355 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1356 GOT_DEFAULT_FILE_MODE);
1357 if (fd == -1) {
1358 if (errno == ENOENT) {
1359 char *parent = dirname(path);
1360 if (parent == NULL)
1361 return got_error_from_errno2("dirname", path);
1362 err = add_dir_on_disk(worktree, parent);
1363 if (err)
1364 return err;
1365 fd = open(ondisk_path,
1366 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1367 GOT_DEFAULT_FILE_MODE);
1368 if (fd == -1)
1369 return got_error_from_errno2("open",
1370 ondisk_path);
1371 } else if (errno == EEXIST) {
1372 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1373 /* TODO file is obstructed; do something */
1374 err = got_error_path(ondisk_path,
1375 GOT_ERR_FILE_OBSTRUCTED);
1376 goto done;
1377 } else {
1378 err = got_opentemp_named_fd(&tmppath, &fd,
1379 ondisk_path);
1380 if (err)
1381 goto done;
1382 update = 1;
1384 } else
1385 return got_error_from_errno2("open", ondisk_path);
1388 if (progress_cb) {
1389 if (restoring_missing_file)
1390 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1391 path);
1392 else if (reverting_versioned_file)
1393 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1394 path);
1395 else
1396 err = (*progress_cb)(progress_arg,
1397 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1398 if (err)
1399 goto done;
1402 hdrlen = got_object_blob_get_hdrlen(blob);
1403 do {
1404 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1405 err = got_object_blob_read_block(&len, blob);
1406 if (err)
1407 break;
1408 if (len > 0) {
1409 /* Skip blob object header first time around. */
1410 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1411 if (outlen == -1) {
1412 err = got_error_from_errno("write");
1413 goto done;
1414 } else if (outlen != len - hdrlen) {
1415 err = got_error(GOT_ERR_IO);
1416 goto done;
1418 hdrlen = 0;
1420 } while (len != 0);
1422 if (fsync(fd) != 0) {
1423 err = got_error_from_errno("fsync");
1424 goto done;
1427 if (update) {
1428 if (rename(tmppath, ondisk_path) != 0) {
1429 err = got_error_from_errno3("rename", tmppath,
1430 ondisk_path);
1431 unlink(tmppath);
1432 goto done;
1436 if (chmod(ondisk_path,
1437 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1438 err = got_error_from_errno2("chmod", ondisk_path);
1439 goto done;
1442 done:
1443 if (fd != -1 && close(fd) != 0 && err == NULL)
1444 err = got_error_from_errno("close");
1445 free(tmppath);
1446 return err;
1449 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1450 static const struct got_error *
1451 get_modified_file_content_status(unsigned char *status, FILE *f)
1453 const struct got_error *err = NULL;
1454 const char *markers[3] = {
1455 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1456 GOT_DIFF_CONFLICT_MARKER_SEP,
1457 GOT_DIFF_CONFLICT_MARKER_END
1459 int i = 0;
1460 char *line;
1461 size_t len;
1462 const char delim[3] = {'\0', '\0', '\0'};
1464 while (*status == GOT_STATUS_MODIFY) {
1465 line = fparseln(f, &len, NULL, delim, 0);
1466 if (line == NULL) {
1467 if (feof(f))
1468 break;
1469 err = got_ferror(f, GOT_ERR_IO);
1470 break;
1473 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1474 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1475 == 0)
1476 *status = GOT_STATUS_CONFLICT;
1477 else
1478 i++;
1482 return err;
1485 static int
1486 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1488 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1489 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1492 static int
1493 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1495 return !(ie->ctime_sec == sb->st_ctime &&
1496 ie->ctime_nsec == sb->st_ctimensec &&
1497 ie->mtime_sec == sb->st_mtime &&
1498 ie->mtime_nsec == sb->st_mtimensec &&
1499 ie->size == (sb->st_size & 0xffffffff) &&
1500 !xbit_differs(ie, sb->st_mode));
1503 static unsigned char
1504 get_staged_status(struct got_fileindex_entry *ie)
1506 switch (got_fileindex_entry_stage_get(ie)) {
1507 case GOT_FILEIDX_STAGE_ADD:
1508 return GOT_STATUS_ADD;
1509 case GOT_FILEIDX_STAGE_DELETE:
1510 return GOT_STATUS_DELETE;
1511 case GOT_FILEIDX_STAGE_MODIFY:
1512 return GOT_STATUS_MODIFY;
1513 default:
1514 return GOT_STATUS_NO_CHANGE;
1518 static const struct got_error *
1519 get_symlink_status(unsigned char *status, struct stat *sb,
1520 struct got_fileindex_entry *ie, const char *abspath,
1521 int dirfd, const char *de_name, struct got_blob_object *blob)
1523 const struct got_error *err = NULL;
1524 char target_path[PATH_MAX];
1525 char etarget[PATH_MAX];
1526 ssize_t elen;
1527 size_t len, target_len = 0;
1528 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1529 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1531 *status = GOT_STATUS_NO_CHANGE;
1533 /* Blob object content specifies the target path of the link. */
1534 do {
1535 err = got_object_blob_read_block(&len, blob);
1536 if (err)
1537 return err;
1538 if (len + target_len >= sizeof(target_path)) {
1540 * Should not happen. The blob contents were OK
1541 * when this symlink was installed.
1543 return got_error(GOT_ERR_NO_SPACE);
1545 if (len > 0) {
1546 /* Skip blob object header first time around. */
1547 memcpy(target_path + target_len, buf + hdrlen,
1548 len - hdrlen);
1549 target_len += len - hdrlen;
1550 hdrlen = 0;
1552 } while (len != 0);
1553 target_path[target_len] = '\0';
1555 if (dirfd != -1) {
1556 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1557 if (elen == -1)
1558 return got_error_from_errno2("readlinkat", abspath);
1559 } else {
1560 elen = readlink(abspath, etarget, sizeof(etarget));
1561 if (elen == -1)
1562 return got_error_from_errno2("readlinkat", abspath);
1565 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1566 *status = GOT_STATUS_MODIFY;
1568 return NULL;
1571 static const struct got_error *
1572 get_file_status(unsigned char *status, struct stat *sb,
1573 struct got_fileindex_entry *ie, const char *abspath,
1574 int dirfd, const char *de_name, struct got_repository *repo)
1576 const struct got_error *err = NULL;
1577 struct got_object_id id;
1578 size_t hdrlen;
1579 int fd = -1;
1580 FILE *f = NULL;
1581 uint8_t fbuf[8192];
1582 struct got_blob_object *blob = NULL;
1583 size_t flen, blen;
1584 unsigned char staged_status = get_staged_status(ie);
1586 *status = GOT_STATUS_NO_CHANGE;
1589 * Whenever the caller provides a directory descriptor and a
1590 * directory entry name for the file, use them! This prevents
1591 * race conditions if filesystem paths change beneath our feet.
1593 if (dirfd != -1) {
1594 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1595 if (errno == ENOENT) {
1596 if (got_fileindex_entry_has_file_on_disk(ie))
1597 *status = GOT_STATUS_MISSING;
1598 else
1599 *status = GOT_STATUS_DELETE;
1600 goto done;
1602 err = got_error_from_errno2("fstatat", abspath);
1603 goto done;
1605 } else {
1606 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1607 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1608 return got_error_from_errno2("open", abspath);
1609 else if (fd == -1 && errno == ELOOP) {
1610 if (lstat(abspath, sb) == -1)
1611 return got_error_from_errno2("lstat", abspath);
1612 } else if (fd == -1 || fstat(fd, sb) == -1) {
1613 if (errno == ENOENT) {
1614 if (got_fileindex_entry_has_file_on_disk(ie))
1615 *status = GOT_STATUS_MISSING;
1616 else
1617 *status = GOT_STATUS_DELETE;
1618 goto done;
1620 err = got_error_from_errno2("fstat", abspath);
1621 goto done;
1625 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1626 *status = GOT_STATUS_OBSTRUCTED;
1627 goto done;
1630 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1631 *status = GOT_STATUS_DELETE;
1632 goto done;
1633 } else if (!got_fileindex_entry_has_blob(ie) &&
1634 staged_status != GOT_STATUS_ADD) {
1635 *status = GOT_STATUS_ADD;
1636 goto done;
1639 if (!stat_info_differs(ie, sb))
1640 goto done;
1642 if (staged_status == GOT_STATUS_MODIFY ||
1643 staged_status == GOT_STATUS_ADD)
1644 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1645 else
1646 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1648 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1649 if (err)
1650 goto done;
1652 if (S_ISLNK(sb->st_mode)) {
1653 /* Staging changes to symlinks is not yet(?) supported. */
1654 if (staged_status != GOT_STATUS_NO_CHANGE) {
1655 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1656 goto done;
1658 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1659 de_name, blob);
1660 goto done;
1664 if (dirfd != -1) {
1665 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1666 if (fd == -1) {
1667 err = got_error_from_errno2("openat", abspath);
1668 goto done;
1672 f = fdopen(fd, "r");
1673 if (f == NULL) {
1674 err = got_error_from_errno2("fdopen", abspath);
1675 goto done;
1677 fd = -1;
1678 hdrlen = got_object_blob_get_hdrlen(blob);
1679 for (;;) {
1680 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1681 err = got_object_blob_read_block(&blen, blob);
1682 if (err)
1683 goto done;
1684 /* Skip length of blob object header first time around. */
1685 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1686 if (flen == 0 && ferror(f)) {
1687 err = got_error_from_errno("fread");
1688 goto done;
1690 if (blen == 0) {
1691 if (flen != 0)
1692 *status = GOT_STATUS_MODIFY;
1693 break;
1694 } else if (flen == 0) {
1695 if (blen != 0)
1696 *status = GOT_STATUS_MODIFY;
1697 break;
1698 } else if (blen - hdrlen == flen) {
1699 /* Skip blob object header first time around. */
1700 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1701 *status = GOT_STATUS_MODIFY;
1702 break;
1704 } else {
1705 *status = GOT_STATUS_MODIFY;
1706 break;
1708 hdrlen = 0;
1711 if (*status == GOT_STATUS_MODIFY) {
1712 rewind(f);
1713 err = get_modified_file_content_status(status, f);
1714 } else if (xbit_differs(ie, sb->st_mode))
1715 *status = GOT_STATUS_MODE_CHANGE;
1716 done:
1717 if (blob)
1718 got_object_blob_close(blob);
1719 if (f != NULL && fclose(f) == EOF && err == NULL)
1720 err = got_error_from_errno2("fclose", abspath);
1721 if (fd != -1 && close(fd) == -1 && err == NULL)
1722 err = got_error_from_errno2("close", abspath);
1723 return err;
1727 * Update timestamps in the file index if a file is unmodified and
1728 * we had to run a full content comparison to find out.
1730 static const struct got_error *
1731 sync_timestamps(char *ondisk_path, unsigned char status,
1732 struct got_fileindex_entry *ie, struct stat *sb)
1734 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1735 return got_fileindex_entry_update(ie, ondisk_path,
1736 ie->blob_sha1, ie->commit_sha1, 1);
1738 return NULL;
1741 static const struct got_error *
1742 update_blob(struct got_worktree *worktree,
1743 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1744 struct got_tree_entry *te, const char *path,
1745 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1746 void *progress_arg)
1748 const struct got_error *err = NULL;
1749 struct got_blob_object *blob = NULL;
1750 char *ondisk_path;
1751 unsigned char status = GOT_STATUS_NO_CHANGE;
1752 struct stat sb;
1754 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1755 return got_error_from_errno("asprintf");
1757 if (ie) {
1758 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1759 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1760 goto done;
1762 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1763 repo);
1764 if (err)
1765 goto done;
1766 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1767 sb.st_mode = got_fileindex_perms_to_st(ie);
1768 } else
1769 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1771 if (status == GOT_STATUS_OBSTRUCTED) {
1772 err = (*progress_cb)(progress_arg, status, path);
1773 goto done;
1775 if (status == GOT_STATUS_CONFLICT) {
1776 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1777 path);
1778 goto done;
1781 if (ie && status != GOT_STATUS_MISSING &&
1782 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1783 if (got_fileindex_entry_has_commit(ie) &&
1784 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1785 SHA1_DIGEST_LENGTH) == 0) {
1786 err = sync_timestamps(ondisk_path, status, ie, &sb);
1787 if (err)
1788 goto done;
1789 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1790 path);
1791 goto done;
1793 if (got_fileindex_entry_has_blob(ie) &&
1794 memcmp(ie->blob_sha1, te->id.sha1,
1795 SHA1_DIGEST_LENGTH) == 0) {
1796 err = sync_timestamps(ondisk_path, status, ie, &sb);
1797 goto done;
1801 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1802 if (err)
1803 goto done;
1805 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1806 int update_timestamps;
1807 struct got_blob_object *blob2 = NULL;
1808 char *label_orig = NULL;
1809 if (got_fileindex_entry_has_blob(ie)) {
1810 struct got_object_id id2;
1811 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1812 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1813 if (err)
1814 goto done;
1816 if (got_fileindex_entry_has_commit(ie)) {
1817 char id_str[SHA1_DIGEST_STRING_LENGTH];
1818 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1819 sizeof(id_str)) == NULL) {
1820 err = got_error_path(id_str,
1821 GOT_ERR_BAD_OBJ_ID_STR);
1822 goto done;
1824 if (asprintf(&label_orig, "%s: commit %s",
1825 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1826 err = got_error_from_errno("asprintf");
1827 goto done;
1830 if (S_ISLNK(te->mode)) {
1831 err = merge_symlink(worktree, blob2,
1832 ondisk_path, path, sb.st_mode, label_orig,
1833 blob, worktree->base_commit_id, repo,
1834 progress_cb, progress_arg);
1835 } else {
1836 err = merge_blob(&update_timestamps, worktree, blob2,
1837 ondisk_path, path, sb.st_mode, label_orig, blob,
1838 worktree->base_commit_id, repo,
1839 progress_cb, progress_arg);
1841 free(label_orig);
1842 if (blob2)
1843 got_object_blob_close(blob2);
1844 if (err)
1845 goto done;
1847 * Do not update timestamps of files with local changes.
1848 * Otherwise, a future status walk would treat them as
1849 * unmodified files again.
1851 err = got_fileindex_entry_update(ie, ondisk_path,
1852 blob->id.sha1, worktree->base_commit_id->sha1,
1853 update_timestamps);
1854 } else if (status == GOT_STATUS_MODE_CHANGE) {
1855 err = got_fileindex_entry_update(ie, ondisk_path,
1856 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1857 } else if (status == GOT_STATUS_DELETE) {
1858 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1859 if (err)
1860 goto done;
1861 err = got_fileindex_entry_update(ie, ondisk_path,
1862 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1863 if (err)
1864 goto done;
1865 } else {
1866 err = install_blob(worktree, ondisk_path, path, te->mode,
1867 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0, 0,
1868 repo, progress_cb, progress_arg);
1869 if (err)
1870 goto done;
1871 if (ie) {
1872 err = got_fileindex_entry_update(ie, ondisk_path,
1873 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1874 } else {
1875 err = create_fileindex_entry(fileindex,
1876 worktree->base_commit_id, ondisk_path, path,
1877 &blob->id);
1879 if (err)
1880 goto done;
1882 got_object_blob_close(blob);
1883 done:
1884 free(ondisk_path);
1885 return err;
1888 static const struct got_error *
1889 remove_ondisk_file(const char *root_path, const char *path)
1891 const struct got_error *err = NULL;
1892 char *ondisk_path = NULL;
1894 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1895 return got_error_from_errno("asprintf");
1897 if (unlink(ondisk_path) == -1) {
1898 if (errno != ENOENT)
1899 err = got_error_from_errno2("unlink", ondisk_path);
1900 } else {
1901 char *parent = dirname(ondisk_path);
1902 while (parent && strcmp(parent, root_path) != 0) {
1903 if (rmdir(parent) == -1) {
1904 if (errno != ENOTEMPTY)
1905 err = got_error_from_errno2("rmdir",
1906 parent);
1907 break;
1909 parent = dirname(parent);
1912 free(ondisk_path);
1913 return err;
1916 static const struct got_error *
1917 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1918 struct got_fileindex_entry *ie, struct got_repository *repo,
1919 got_worktree_checkout_cb progress_cb, void *progress_arg)
1921 const struct got_error *err = NULL;
1922 unsigned char status;
1923 struct stat sb;
1924 char *ondisk_path;
1926 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1927 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1929 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1930 == -1)
1931 return got_error_from_errno("asprintf");
1933 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1934 if (err)
1935 goto done;
1937 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
1938 char ondisk_target[PATH_MAX];
1939 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
1940 sizeof(ondisk_target));
1941 if (ondisk_len == -1) {
1942 err = got_error_from_errno2("readlink", ondisk_path);
1943 goto done;
1945 ondisk_target[ondisk_len] = '\0';
1946 err = install_symlink_conflict(NULL, worktree->base_commit_id,
1947 NULL, NULL, /* XXX pass common ancestor info? */
1948 ondisk_target, ondisk_path);
1949 if (err)
1950 goto done;
1951 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1952 ie->path);
1953 goto done;
1956 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1957 status == GOT_STATUS_ADD) {
1958 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1959 if (err)
1960 goto done;
1962 * Preserve the working file and change the deleted blob's
1963 * entry into a schedule-add entry.
1965 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1966 0);
1967 } else {
1968 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1969 if (err)
1970 goto done;
1971 if (status == GOT_STATUS_NO_CHANGE) {
1972 err = remove_ondisk_file(worktree->root_path, ie->path);
1973 if (err)
1974 goto done;
1976 got_fileindex_entry_remove(fileindex, ie);
1978 done:
1979 free(ondisk_path);
1980 return err;
1983 struct diff_cb_arg {
1984 struct got_fileindex *fileindex;
1985 struct got_worktree *worktree;
1986 struct got_repository *repo;
1987 got_worktree_checkout_cb progress_cb;
1988 void *progress_arg;
1989 got_cancel_cb cancel_cb;
1990 void *cancel_arg;
1993 static const struct got_error *
1994 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1995 struct got_tree_entry *te, const char *parent_path)
1997 struct diff_cb_arg *a = arg;
1999 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2000 return got_error(GOT_ERR_CANCELLED);
2002 return update_blob(a->worktree, a->fileindex, ie, te,
2003 ie->path, a->repo, a->progress_cb, a->progress_arg);
2006 static const struct got_error *
2007 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2009 struct diff_cb_arg *a = arg;
2011 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2012 return got_error(GOT_ERR_CANCELLED);
2014 return delete_blob(a->worktree, a->fileindex, ie,
2015 a->repo, a->progress_cb, a->progress_arg);
2018 static const struct got_error *
2019 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2021 struct diff_cb_arg *a = arg;
2022 const struct got_error *err;
2023 char *path;
2025 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2026 return got_error(GOT_ERR_CANCELLED);
2028 if (got_object_tree_entry_is_submodule(te))
2029 return NULL;
2031 if (asprintf(&path, "%s%s%s", parent_path,
2032 parent_path[0] ? "/" : "", te->name)
2033 == -1)
2034 return got_error_from_errno("asprintf");
2036 if (S_ISDIR(te->mode))
2037 err = add_dir_on_disk(a->worktree, path);
2038 else
2039 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2040 a->repo, a->progress_cb, a->progress_arg);
2042 free(path);
2043 return err;
2046 static const struct got_error *
2047 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2049 const struct got_error *err = NULL;
2050 char *uuidstr = NULL;
2051 uint32_t uuid_status;
2053 *refname = NULL;
2055 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2056 if (uuid_status != uuid_s_ok)
2057 return got_error_uuid(uuid_status, "uuid_to_string");
2059 if (asprintf(refname, "%s-%s", prefix, uuidstr)
2060 == -1) {
2061 err = got_error_from_errno("asprintf");
2062 *refname = NULL;
2064 free(uuidstr);
2065 return err;
2068 const struct got_error *
2069 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2071 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2074 static const struct got_error *
2075 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2077 return get_ref_name(refname, worktree,
2078 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2081 static const struct got_error *
2082 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2084 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2087 static const struct got_error *
2088 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2090 return get_ref_name(refname, worktree,
2091 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2094 static const struct got_error *
2095 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2097 return get_ref_name(refname, worktree,
2098 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2101 static const struct got_error *
2102 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2104 return get_ref_name(refname, worktree,
2105 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2108 static const struct got_error *
2109 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2111 return get_ref_name(refname, worktree,
2112 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2115 static const struct got_error *
2116 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2118 return get_ref_name(refname, worktree,
2119 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2122 static const struct got_error *
2123 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2125 return get_ref_name(refname, worktree,
2126 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2129 const struct got_error *
2130 got_worktree_get_histedit_script_path(char **path,
2131 struct got_worktree *worktree)
2133 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2134 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2135 *path = NULL;
2136 return got_error_from_errno("asprintf");
2138 return NULL;
2142 * Prevent Git's garbage collector from deleting our base commit by
2143 * setting a reference to our base commit's ID.
2145 static const struct got_error *
2146 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2148 const struct got_error *err = NULL;
2149 struct got_reference *ref = NULL;
2150 char *refname;
2152 err = got_worktree_get_base_ref_name(&refname, worktree);
2153 if (err)
2154 return err;
2156 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2157 if (err)
2158 goto done;
2160 err = got_ref_write(ref, repo);
2161 done:
2162 free(refname);
2163 if (ref)
2164 got_ref_close(ref);
2165 return err;
2168 static const struct got_error *
2169 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2171 const struct got_error *err = NULL;
2173 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2174 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2175 err = got_error_from_errno("asprintf");
2176 *fileindex_path = NULL;
2178 return err;
2182 static const struct got_error *
2183 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2184 struct got_worktree *worktree)
2186 const struct got_error *err = NULL;
2187 FILE *index = NULL;
2189 *fileindex_path = NULL;
2190 *fileindex = got_fileindex_alloc();
2191 if (*fileindex == NULL)
2192 return got_error_from_errno("got_fileindex_alloc");
2194 err = get_fileindex_path(fileindex_path, worktree);
2195 if (err)
2196 goto done;
2198 index = fopen(*fileindex_path, "rb");
2199 if (index == NULL) {
2200 if (errno != ENOENT)
2201 err = got_error_from_errno2("fopen", *fileindex_path);
2202 } else {
2203 err = got_fileindex_read(*fileindex, index);
2204 if (fclose(index) != 0 && err == NULL)
2205 err = got_error_from_errno("fclose");
2207 done:
2208 if (err) {
2209 free(*fileindex_path);
2210 *fileindex_path = NULL;
2211 got_fileindex_free(*fileindex);
2212 *fileindex = NULL;
2214 return err;
2217 struct bump_base_commit_id_arg {
2218 struct got_object_id *base_commit_id;
2219 const char *path;
2220 size_t path_len;
2221 const char *entry_name;
2222 got_worktree_checkout_cb progress_cb;
2223 void *progress_arg;
2226 /* Bump base commit ID of all files within an updated part of the work tree. */
2227 static const struct got_error *
2228 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2230 const struct got_error *err;
2231 struct bump_base_commit_id_arg *a = arg;
2233 if (a->entry_name) {
2234 if (strcmp(ie->path, a->path) != 0)
2235 return NULL;
2236 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2237 return NULL;
2239 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2240 SHA1_DIGEST_LENGTH) == 0)
2241 return NULL;
2243 if (a->progress_cb) {
2244 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2245 ie->path);
2246 if (err)
2247 return err;
2249 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2250 return NULL;
2253 static const struct got_error *
2254 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2256 const struct got_error *err = NULL;
2257 char *new_fileindex_path = NULL;
2258 FILE *new_index = NULL;
2259 struct timespec timeout;
2261 err = got_opentemp_named(&new_fileindex_path, &new_index,
2262 fileindex_path);
2263 if (err)
2264 goto done;
2266 err = got_fileindex_write(fileindex, new_index);
2267 if (err)
2268 goto done;
2270 if (rename(new_fileindex_path, fileindex_path) != 0) {
2271 err = got_error_from_errno3("rename", new_fileindex_path,
2272 fileindex_path);
2273 unlink(new_fileindex_path);
2277 * Sleep for a short amount of time to ensure that files modified after
2278 * this program exits have a different time stamp from the one which
2279 * was recorded in the file index.
2281 timeout.tv_sec = 0;
2282 timeout.tv_nsec = 1;
2283 nanosleep(&timeout, NULL);
2284 done:
2285 if (new_index)
2286 fclose(new_index);
2287 free(new_fileindex_path);
2288 return err;
2291 static const struct got_error *
2292 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2293 struct got_object_id **tree_id, const char *wt_relpath,
2294 struct got_worktree *worktree, struct got_repository *repo)
2296 const struct got_error *err = NULL;
2297 struct got_object_id *id = NULL;
2298 char *in_repo_path = NULL;
2299 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2301 *entry_type = GOT_OBJ_TYPE_ANY;
2302 *tree_relpath = NULL;
2303 *tree_id = NULL;
2305 if (wt_relpath[0] == '\0') {
2306 /* Check out all files within the work tree. */
2307 *entry_type = GOT_OBJ_TYPE_TREE;
2308 *tree_relpath = strdup("");
2309 if (*tree_relpath == NULL) {
2310 err = got_error_from_errno("strdup");
2311 goto done;
2313 err = got_object_id_by_path(tree_id, repo,
2314 worktree->base_commit_id, worktree->path_prefix);
2315 if (err)
2316 goto done;
2317 return NULL;
2320 /* Check out a subset of files in the work tree. */
2322 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2323 is_root_wt ? "" : "/", wt_relpath) == -1) {
2324 err = got_error_from_errno("asprintf");
2325 goto done;
2328 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2329 in_repo_path);
2330 if (err)
2331 goto done;
2333 free(in_repo_path);
2334 in_repo_path = NULL;
2336 err = got_object_get_type(entry_type, repo, id);
2337 if (err)
2338 goto done;
2340 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2341 /* Check out a single file. */
2342 if (strchr(wt_relpath, '/') == NULL) {
2343 /* Check out a single file in work tree's root dir. */
2344 in_repo_path = strdup(worktree->path_prefix);
2345 if (in_repo_path == NULL) {
2346 err = got_error_from_errno("strdup");
2347 goto done;
2349 *tree_relpath = strdup("");
2350 if (*tree_relpath == NULL) {
2351 err = got_error_from_errno("strdup");
2352 goto done;
2354 } else {
2355 /* Check out a single file in a subdirectory. */
2356 err = got_path_dirname(tree_relpath, wt_relpath);
2357 if (err)
2358 return err;
2359 if (asprintf(&in_repo_path, "%s%s%s",
2360 worktree->path_prefix, is_root_wt ? "" : "/",
2361 *tree_relpath) == -1) {
2362 err = got_error_from_errno("asprintf");
2363 goto done;
2366 err = got_object_id_by_path(tree_id, repo,
2367 worktree->base_commit_id, in_repo_path);
2368 } else {
2369 /* Check out all files within a subdirectory. */
2370 *tree_id = got_object_id_dup(id);
2371 if (*tree_id == NULL) {
2372 err = got_error_from_errno("got_object_id_dup");
2373 goto done;
2375 *tree_relpath = strdup(wt_relpath);
2376 if (*tree_relpath == NULL) {
2377 err = got_error_from_errno("strdup");
2378 goto done;
2381 done:
2382 free(id);
2383 free(in_repo_path);
2384 if (err) {
2385 *entry_type = GOT_OBJ_TYPE_ANY;
2386 free(*tree_relpath);
2387 *tree_relpath = NULL;
2388 free(*tree_id);
2389 *tree_id = NULL;
2391 return err;
2394 static const struct got_error *
2395 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2396 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2397 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2398 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2400 const struct got_error *err = NULL;
2401 struct got_commit_object *commit = NULL;
2402 struct got_tree_object *tree = NULL;
2403 struct got_fileindex_diff_tree_cb diff_cb;
2404 struct diff_cb_arg arg;
2406 err = ref_base_commit(worktree, repo);
2407 if (err) {
2408 if (!(err->code == GOT_ERR_ERRNO &&
2409 (errno == EACCES || errno == EROFS)))
2410 goto done;
2411 err = (*progress_cb)(progress_arg,
2412 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2413 if (err)
2414 return err;
2417 err = got_object_open_as_commit(&commit, repo,
2418 worktree->base_commit_id);
2419 if (err)
2420 goto done;
2422 err = got_object_open_as_tree(&tree, repo, tree_id);
2423 if (err)
2424 goto done;
2426 if (entry_name &&
2427 got_object_tree_find_entry(tree, entry_name) == NULL) {
2428 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2429 goto done;
2432 diff_cb.diff_old_new = diff_old_new;
2433 diff_cb.diff_old = diff_old;
2434 diff_cb.diff_new = diff_new;
2435 arg.fileindex = fileindex;
2436 arg.worktree = worktree;
2437 arg.repo = repo;
2438 arg.progress_cb = progress_cb;
2439 arg.progress_arg = progress_arg;
2440 arg.cancel_cb = cancel_cb;
2441 arg.cancel_arg = cancel_arg;
2442 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2443 entry_name, repo, &diff_cb, &arg);
2444 done:
2445 if (tree)
2446 got_object_tree_close(tree);
2447 if (commit)
2448 got_object_commit_close(commit);
2449 return err;
2452 const struct got_error *
2453 got_worktree_checkout_files(struct got_worktree *worktree,
2454 struct got_pathlist_head *paths, struct got_repository *repo,
2455 got_worktree_checkout_cb progress_cb, void *progress_arg,
2456 got_cancel_cb cancel_cb, void *cancel_arg)
2458 const struct got_error *err = NULL, *sync_err, *unlockerr;
2459 struct got_commit_object *commit = NULL;
2460 struct got_tree_object *tree = NULL;
2461 struct got_fileindex *fileindex = NULL;
2462 char *fileindex_path = NULL;
2463 struct got_pathlist_entry *pe;
2464 struct tree_path_data {
2465 SIMPLEQ_ENTRY(tree_path_data) entry;
2466 struct got_object_id *tree_id;
2467 int entry_type;
2468 char *relpath;
2469 char *entry_name;
2470 } *tpd = NULL;
2471 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2473 SIMPLEQ_INIT(&tree_paths);
2475 err = lock_worktree(worktree, LOCK_EX);
2476 if (err)
2477 return err;
2479 /* Map all specified paths to in-repository trees. */
2480 TAILQ_FOREACH(pe, paths, entry) {
2481 tpd = malloc(sizeof(*tpd));
2482 if (tpd == NULL) {
2483 err = got_error_from_errno("malloc");
2484 goto done;
2487 err = find_tree_entry_for_checkout(&tpd->entry_type,
2488 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2489 if (err) {
2490 free(tpd);
2491 goto done;
2494 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2495 err = got_path_basename(&tpd->entry_name, pe->path);
2496 if (err) {
2497 free(tpd->relpath);
2498 free(tpd->tree_id);
2499 free(tpd);
2500 goto done;
2502 } else
2503 tpd->entry_name = NULL;
2505 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2509 * Read the file index.
2510 * Checking out files is supposed to be an idempotent operation.
2511 * If the on-disk file index is incomplete we will try to complete it.
2513 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2514 if (err)
2515 goto done;
2517 tpd = SIMPLEQ_FIRST(&tree_paths);
2518 TAILQ_FOREACH(pe, paths, entry) {
2519 struct bump_base_commit_id_arg bbc_arg;
2521 err = checkout_files(worktree, fileindex, tpd->relpath,
2522 tpd->tree_id, tpd->entry_name, repo,
2523 progress_cb, progress_arg, cancel_cb, cancel_arg);
2524 if (err)
2525 break;
2527 bbc_arg.base_commit_id = worktree->base_commit_id;
2528 bbc_arg.entry_name = tpd->entry_name;
2529 bbc_arg.path = pe->path;
2530 bbc_arg.path_len = pe->path_len;
2531 bbc_arg.progress_cb = progress_cb;
2532 bbc_arg.progress_arg = progress_arg;
2533 err = got_fileindex_for_each_entry_safe(fileindex,
2534 bump_base_commit_id, &bbc_arg);
2535 if (err)
2536 break;
2538 tpd = SIMPLEQ_NEXT(tpd, entry);
2540 sync_err = sync_fileindex(fileindex, fileindex_path);
2541 if (sync_err && err == NULL)
2542 err = sync_err;
2543 done:
2544 free(fileindex_path);
2545 if (tree)
2546 got_object_tree_close(tree);
2547 if (commit)
2548 got_object_commit_close(commit);
2549 if (fileindex)
2550 got_fileindex_free(fileindex);
2551 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2552 tpd = SIMPLEQ_FIRST(&tree_paths);
2553 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2554 free(tpd->relpath);
2555 free(tpd->tree_id);
2556 free(tpd);
2558 unlockerr = lock_worktree(worktree, LOCK_SH);
2559 if (unlockerr && err == NULL)
2560 err = unlockerr;
2561 return err;
2564 struct merge_file_cb_arg {
2565 struct got_worktree *worktree;
2566 struct got_fileindex *fileindex;
2567 got_worktree_checkout_cb progress_cb;
2568 void *progress_arg;
2569 got_cancel_cb cancel_cb;
2570 void *cancel_arg;
2571 const char *label_orig;
2572 struct got_object_id *commit_id2;
2575 static const struct got_error *
2576 merge_file_cb(void *arg, struct got_blob_object *blob1,
2577 struct got_blob_object *blob2, struct got_object_id *id1,
2578 struct got_object_id *id2, const char *path1, const char *path2,
2579 mode_t mode1, mode_t mode2, struct got_repository *repo)
2581 static const struct got_error *err = NULL;
2582 struct merge_file_cb_arg *a = arg;
2583 struct got_fileindex_entry *ie;
2584 char *ondisk_path = NULL;
2585 struct stat sb;
2586 unsigned char status;
2587 int local_changes_subsumed;
2589 if (blob1 && blob2) {
2590 ie = got_fileindex_entry_get(a->fileindex, path2,
2591 strlen(path2));
2592 if (ie == NULL)
2593 return (*a->progress_cb)(a->progress_arg,
2594 GOT_STATUS_MISSING, path2);
2596 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2597 path2) == -1)
2598 return got_error_from_errno("asprintf");
2600 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2601 repo);
2602 if (err)
2603 goto done;
2605 if (status == GOT_STATUS_DELETE) {
2606 err = (*a->progress_cb)(a->progress_arg,
2607 GOT_STATUS_MERGE, path2);
2608 goto done;
2610 if (status != GOT_STATUS_NO_CHANGE &&
2611 status != GOT_STATUS_MODIFY &&
2612 status != GOT_STATUS_CONFLICT &&
2613 status != GOT_STATUS_ADD) {
2614 err = (*a->progress_cb)(a->progress_arg, status, path2);
2615 goto done;
2618 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2619 err = merge_symlink(a->worktree, blob1,
2620 ondisk_path, path2, sb.st_mode, a->label_orig,
2621 blob2, a->commit_id2, repo, a->progress_cb,
2622 a->progress_arg);
2623 } else {
2624 err = merge_blob(&local_changes_subsumed, a->worktree,
2625 blob1, ondisk_path, path2, sb.st_mode,
2626 a->label_orig, blob2, a->commit_id2, repo,
2627 a->progress_cb, a->progress_arg);
2629 } else if (blob1) {
2630 ie = got_fileindex_entry_get(a->fileindex, path1,
2631 strlen(path1));
2632 if (ie == NULL)
2633 return (*a->progress_cb)(a->progress_arg,
2634 GOT_STATUS_MISSING, path1);
2636 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2637 path1) == -1)
2638 return got_error_from_errno("asprintf");
2640 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2641 repo);
2642 if (err)
2643 goto done;
2645 switch (status) {
2646 case GOT_STATUS_NO_CHANGE:
2647 err = (*a->progress_cb)(a->progress_arg,
2648 GOT_STATUS_DELETE, path1);
2649 if (err)
2650 goto done;
2651 err = remove_ondisk_file(a->worktree->root_path, path1);
2652 if (err)
2653 goto done;
2654 if (ie)
2655 got_fileindex_entry_mark_deleted_from_disk(ie);
2656 break;
2657 case GOT_STATUS_DELETE:
2658 case GOT_STATUS_MISSING:
2659 err = (*a->progress_cb)(a->progress_arg,
2660 GOT_STATUS_DELETE, path1);
2661 if (err)
2662 goto done;
2663 if (ie)
2664 got_fileindex_entry_mark_deleted_from_disk(ie);
2665 break;
2666 case GOT_STATUS_ADD:
2667 case GOT_STATUS_MODIFY:
2668 case GOT_STATUS_CONFLICT:
2669 err = (*a->progress_cb)(a->progress_arg,
2670 GOT_STATUS_CANNOT_DELETE, path1);
2671 if (err)
2672 goto done;
2673 break;
2674 case GOT_STATUS_OBSTRUCTED:
2675 err = (*a->progress_cb)(a->progress_arg, status, path1);
2676 if (err)
2677 goto done;
2678 break;
2679 default:
2680 break;
2682 } else if (blob2) {
2683 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2684 path2) == -1)
2685 return got_error_from_errno("asprintf");
2686 ie = got_fileindex_entry_get(a->fileindex, path2,
2687 strlen(path2));
2688 if (ie) {
2689 err = get_file_status(&status, &sb, ie, ondisk_path,
2690 -1, NULL, repo);
2691 if (err)
2692 goto done;
2693 if (status != GOT_STATUS_NO_CHANGE &&
2694 status != GOT_STATUS_MODIFY &&
2695 status != GOT_STATUS_CONFLICT &&
2696 status != GOT_STATUS_ADD) {
2697 err = (*a->progress_cb)(a->progress_arg,
2698 status, path2);
2699 goto done;
2701 if (S_ISLNK(mode2)) {
2702 err = merge_symlink(a->worktree, NULL,
2703 ondisk_path, path2, sb.st_mode,
2704 a->label_orig, blob2, a->commit_id2,
2705 repo, a->progress_cb, a->progress_arg);
2706 if (err)
2707 goto done;
2708 } else {
2709 err = merge_blob(&local_changes_subsumed,
2710 a->worktree, NULL, ondisk_path, path2,
2711 sb.st_mode, a->label_orig, blob2,
2712 a->commit_id2, repo, a->progress_cb,
2713 a->progress_arg);
2714 if (err)
2715 goto done;
2717 if (status == GOT_STATUS_DELETE) {
2718 err = got_fileindex_entry_update(ie,
2719 ondisk_path, blob2->id.sha1,
2720 a->worktree->base_commit_id->sha1, 0);
2721 if (err)
2722 goto done;
2724 } else {
2725 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2726 err = install_blob(a->worktree, ondisk_path, path2,
2727 /* XXX get this from parent tree! */
2728 GOT_DEFAULT_FILE_MODE,
2729 sb.st_mode, blob2, 0, 0, 0, repo,
2730 a->progress_cb, a->progress_arg);
2731 if (err)
2732 goto done;
2733 err = got_fileindex_entry_alloc(&ie, path2);
2734 if (err)
2735 goto done;
2736 err = got_fileindex_entry_update(ie, ondisk_path,
2737 NULL, NULL, 1);
2738 if (err) {
2739 got_fileindex_entry_free(ie);
2740 goto done;
2742 err = got_fileindex_entry_add(a->fileindex, ie);
2743 if (err) {
2744 got_fileindex_entry_free(ie);
2745 goto done;
2749 done:
2750 free(ondisk_path);
2751 return err;
2754 struct check_merge_ok_arg {
2755 struct got_worktree *worktree;
2756 struct got_repository *repo;
2759 static const struct got_error *
2760 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2762 const struct got_error *err = NULL;
2763 struct check_merge_ok_arg *a = arg;
2764 unsigned char status;
2765 struct stat sb;
2766 char *ondisk_path;
2768 /* Reject merges into a work tree with mixed base commits. */
2769 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2770 SHA1_DIGEST_LENGTH))
2771 return got_error(GOT_ERR_MIXED_COMMITS);
2773 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2774 == -1)
2775 return got_error_from_errno("asprintf");
2777 /* Reject merges into a work tree with conflicted files. */
2778 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2779 if (err)
2780 return err;
2781 if (status == GOT_STATUS_CONFLICT)
2782 return got_error(GOT_ERR_CONFLICTS);
2784 return NULL;
2787 static const struct got_error *
2788 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2789 const char *fileindex_path, struct got_object_id *commit_id1,
2790 struct got_object_id *commit_id2, struct got_repository *repo,
2791 got_worktree_checkout_cb progress_cb, void *progress_arg,
2792 got_cancel_cb cancel_cb, void *cancel_arg)
2794 const struct got_error *err = NULL, *sync_err;
2795 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2796 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2797 struct merge_file_cb_arg arg;
2798 char *label_orig = NULL;
2800 if (commit_id1) {
2801 char *id_str;
2803 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2804 worktree->path_prefix);
2805 if (err)
2806 goto done;
2808 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2809 if (err)
2810 goto done;
2812 err = got_object_id_str(&id_str, commit_id1);
2813 if (err)
2814 goto done;
2816 if (asprintf(&label_orig, "%s: commit %s",
2817 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2818 err = got_error_from_errno("asprintf");
2819 free(id_str);
2820 goto done;
2822 free(id_str);
2825 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2826 worktree->path_prefix);
2827 if (err)
2828 goto done;
2830 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2831 if (err)
2832 goto done;
2834 arg.worktree = worktree;
2835 arg.fileindex = fileindex;
2836 arg.progress_cb = progress_cb;
2837 arg.progress_arg = progress_arg;
2838 arg.cancel_cb = cancel_cb;
2839 arg.cancel_arg = cancel_arg;
2840 arg.label_orig = label_orig;
2841 arg.commit_id2 = commit_id2;
2842 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2843 sync_err = sync_fileindex(fileindex, fileindex_path);
2844 if (sync_err && err == NULL)
2845 err = sync_err;
2846 done:
2847 if (tree1)
2848 got_object_tree_close(tree1);
2849 if (tree2)
2850 got_object_tree_close(tree2);
2851 free(label_orig);
2852 return err;
2855 const struct got_error *
2856 got_worktree_merge_files(struct got_worktree *worktree,
2857 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2858 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2859 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2861 const struct got_error *err, *unlockerr;
2862 char *fileindex_path = NULL;
2863 struct got_fileindex *fileindex = NULL;
2864 struct check_merge_ok_arg mok_arg;
2866 err = lock_worktree(worktree, LOCK_EX);
2867 if (err)
2868 return err;
2870 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2871 if (err)
2872 goto done;
2874 mok_arg.worktree = worktree;
2875 mok_arg.repo = repo;
2876 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2877 &mok_arg);
2878 if (err)
2879 goto done;
2881 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2882 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2883 done:
2884 if (fileindex)
2885 got_fileindex_free(fileindex);
2886 free(fileindex_path);
2887 unlockerr = lock_worktree(worktree, LOCK_SH);
2888 if (unlockerr && err == NULL)
2889 err = unlockerr;
2890 return err;
2893 struct diff_dir_cb_arg {
2894 struct got_fileindex *fileindex;
2895 struct got_worktree *worktree;
2896 const char *status_path;
2897 size_t status_path_len;
2898 struct got_repository *repo;
2899 got_worktree_status_cb status_cb;
2900 void *status_arg;
2901 got_cancel_cb cancel_cb;
2902 void *cancel_arg;
2903 /* A pathlist containing per-directory pathlists of ignore patterns. */
2904 struct got_pathlist_head ignores;
2905 int report_unchanged;
2906 int no_ignores;
2909 static const struct got_error *
2910 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2911 int dirfd, const char *de_name,
2912 got_worktree_status_cb status_cb, void *status_arg,
2913 struct got_repository *repo, int report_unchanged)
2915 const struct got_error *err = NULL;
2916 unsigned char status = GOT_STATUS_NO_CHANGE;
2917 unsigned char staged_status = get_staged_status(ie);
2918 struct stat sb;
2919 struct got_object_id blob_id, commit_id, staged_blob_id;
2920 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2921 struct got_object_id *staged_blob_idp = NULL;
2923 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2924 if (err)
2925 return err;
2927 if (status == GOT_STATUS_NO_CHANGE &&
2928 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2929 return NULL;
2931 if (got_fileindex_entry_has_blob(ie)) {
2932 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2933 blob_idp = &blob_id;
2935 if (got_fileindex_entry_has_commit(ie)) {
2936 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2937 commit_idp = &commit_id;
2939 if (staged_status == GOT_STATUS_ADD ||
2940 staged_status == GOT_STATUS_MODIFY) {
2941 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2942 SHA1_DIGEST_LENGTH);
2943 staged_blob_idp = &staged_blob_id;
2946 return (*status_cb)(status_arg, status, staged_status,
2947 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2950 static const struct got_error *
2951 status_old_new(void *arg, struct got_fileindex_entry *ie,
2952 struct dirent *de, const char *parent_path, int dirfd)
2954 const struct got_error *err = NULL;
2955 struct diff_dir_cb_arg *a = arg;
2956 char *abspath;
2958 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2959 return got_error(GOT_ERR_CANCELLED);
2961 if (got_path_cmp(parent_path, a->status_path,
2962 strlen(parent_path), a->status_path_len) != 0 &&
2963 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2964 return NULL;
2966 if (parent_path[0]) {
2967 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2968 parent_path, de->d_name) == -1)
2969 return got_error_from_errno("asprintf");
2970 } else {
2971 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2972 de->d_name) == -1)
2973 return got_error_from_errno("asprintf");
2976 err = report_file_status(ie, abspath, dirfd, de->d_name,
2977 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2978 free(abspath);
2979 return err;
2982 static const struct got_error *
2983 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2985 struct diff_dir_cb_arg *a = arg;
2986 struct got_object_id blob_id, commit_id;
2987 unsigned char status;
2989 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2990 return got_error(GOT_ERR_CANCELLED);
2992 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2993 return NULL;
2995 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2996 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2997 if (got_fileindex_entry_has_file_on_disk(ie))
2998 status = GOT_STATUS_MISSING;
2999 else
3000 status = GOT_STATUS_DELETE;
3001 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3002 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3005 void
3006 free_ignorelist(struct got_pathlist_head *ignorelist)
3008 struct got_pathlist_entry *pe;
3010 TAILQ_FOREACH(pe, ignorelist, entry)
3011 free((char *)pe->path);
3012 got_pathlist_free(ignorelist);
3015 void
3016 free_ignores(struct got_pathlist_head *ignores)
3018 struct got_pathlist_entry *pe;
3020 TAILQ_FOREACH(pe, ignores, entry) {
3021 struct got_pathlist_head *ignorelist = pe->data;
3022 free_ignorelist(ignorelist);
3023 free((char *)pe->path);
3025 got_pathlist_free(ignores);
3028 static const struct got_error *
3029 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3031 const struct got_error *err = NULL;
3032 struct got_pathlist_entry *pe = NULL;
3033 struct got_pathlist_head *ignorelist;
3034 char *line = NULL, *pattern, *dirpath = NULL;
3035 size_t linesize = 0;
3036 ssize_t linelen;
3038 ignorelist = calloc(1, sizeof(*ignorelist));
3039 if (ignorelist == NULL)
3040 return got_error_from_errno("calloc");
3041 TAILQ_INIT(ignorelist);
3043 while ((linelen = getline(&line, &linesize, f)) != -1) {
3044 if (linelen > 0 && line[linelen - 1] == '\n')
3045 line[linelen - 1] = '\0';
3047 /* Git's ignores may contain comments. */
3048 if (line[0] == '#')
3049 continue;
3051 /* Git's negated patterns are not (yet?) supported. */
3052 if (line[0] == '!')
3053 continue;
3055 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3056 line) == -1) {
3057 err = got_error_from_errno("asprintf");
3058 goto done;
3060 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3061 if (err)
3062 goto done;
3064 if (ferror(f)) {
3065 err = got_error_from_errno("getline");
3066 goto done;
3069 dirpath = strdup(path);
3070 if (dirpath == NULL) {
3071 err = got_error_from_errno("strdup");
3072 goto done;
3074 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3075 done:
3076 free(line);
3077 if (err || pe == NULL) {
3078 free(dirpath);
3079 free_ignorelist(ignorelist);
3081 return err;
3084 int
3085 match_ignores(struct got_pathlist_head *ignores, const char *path)
3087 struct got_pathlist_entry *pe;
3089 /* Handle patterns which match in all directories. */
3090 TAILQ_FOREACH(pe, ignores, entry) {
3091 struct got_pathlist_head *ignorelist = pe->data;
3092 struct got_pathlist_entry *pi;
3094 TAILQ_FOREACH(pi, ignorelist, entry) {
3095 const char *p, *pattern = pi->path;
3097 if (strncmp(pattern, "**/", 3) != 0)
3098 continue;
3099 pattern += 3;
3100 p = path;
3101 while (*p) {
3102 if (fnmatch(pattern, p,
3103 FNM_PATHNAME | FNM_LEADING_DIR)) {
3104 /* Retry in next directory. */
3105 while (*p && *p != '/')
3106 p++;
3107 while (*p == '/')
3108 p++;
3109 continue;
3111 return 1;
3117 * The ignores pathlist contains ignore lists from children before
3118 * parents, so we can find the most specific ignorelist by walking
3119 * ignores backwards.
3121 pe = TAILQ_LAST(ignores, got_pathlist_head);
3122 while (pe) {
3123 if (got_path_is_child(path, pe->path, pe->path_len)) {
3124 struct got_pathlist_head *ignorelist = pe->data;
3125 struct got_pathlist_entry *pi;
3126 TAILQ_FOREACH(pi, ignorelist, entry) {
3127 const char *pattern = pi->path;
3128 int flags = FNM_LEADING_DIR;
3129 if (strstr(pattern, "/**/") == NULL)
3130 flags |= FNM_PATHNAME;
3131 if (fnmatch(pattern, path, flags))
3132 continue;
3133 return 1;
3136 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3139 return 0;
3142 static const struct got_error *
3143 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3144 const char *path, int dirfd, const char *ignores_filename)
3146 const struct got_error *err = NULL;
3147 char *ignorespath;
3148 int fd = -1;
3149 FILE *ignoresfile = NULL;
3151 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3152 path[0] ? "/" : "", ignores_filename) == -1)
3153 return got_error_from_errno("asprintf");
3155 if (dirfd != -1) {
3156 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3157 if (fd == -1) {
3158 if (errno != ENOENT && errno != EACCES)
3159 err = got_error_from_errno2("openat",
3160 ignorespath);
3161 } else {
3162 ignoresfile = fdopen(fd, "r");
3163 if (ignoresfile == NULL)
3164 err = got_error_from_errno2("fdopen",
3165 ignorespath);
3166 else {
3167 fd = -1;
3168 err = read_ignores(ignores, path, ignoresfile);
3171 } else {
3172 ignoresfile = fopen(ignorespath, "r");
3173 if (ignoresfile == NULL) {
3174 if (errno != ENOENT && errno != EACCES)
3175 err = got_error_from_errno2("fopen",
3176 ignorespath);
3177 } else
3178 err = read_ignores(ignores, path, ignoresfile);
3181 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3182 err = got_error_from_errno2("fclose", path);
3183 if (fd != -1 && close(fd) == -1 && err == NULL)
3184 err = got_error_from_errno2("close", path);
3185 free(ignorespath);
3186 return err;
3189 static const struct got_error *
3190 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3192 const struct got_error *err = NULL;
3193 struct diff_dir_cb_arg *a = arg;
3194 char *path = NULL;
3196 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3197 return got_error(GOT_ERR_CANCELLED);
3199 if (parent_path[0]) {
3200 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3201 return got_error_from_errno("asprintf");
3202 } else {
3203 path = de->d_name;
3206 if (de->d_type != DT_DIR &&
3207 got_path_is_child(path, a->status_path, a->status_path_len)
3208 && !match_ignores(&a->ignores, path))
3209 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3210 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3211 if (parent_path[0])
3212 free(path);
3213 return err;
3216 static const struct got_error *
3217 status_traverse(void *arg, const char *path, int dirfd)
3219 const struct got_error *err = NULL;
3220 struct diff_dir_cb_arg *a = arg;
3222 if (a->no_ignores)
3223 return NULL;
3225 err = add_ignores(&a->ignores, a->worktree->root_path,
3226 path, dirfd, ".cvsignore");
3227 if (err)
3228 return err;
3230 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3231 dirfd, ".gitignore");
3233 return err;
3236 static const struct got_error *
3237 report_single_file_status(const char *path, const char *ondisk_path,
3238 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3239 void *status_arg, struct got_repository *repo, int report_unchanged)
3241 struct got_fileindex_entry *ie;
3242 struct stat sb;
3244 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3245 if (ie)
3246 return report_file_status(ie, ondisk_path, -1, NULL,
3247 status_cb, status_arg, repo, report_unchanged);
3249 if (lstat(ondisk_path, &sb) == -1) {
3250 if (errno != ENOENT)
3251 return got_error_from_errno2("lstat", ondisk_path);
3252 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3253 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3254 return NULL;
3257 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3258 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3259 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3261 return NULL;
3264 static const struct got_error *
3265 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3266 const char *root_path, const char *path)
3268 const struct got_error *err;
3269 char *parent_path, *next_parent_path = NULL;
3271 err = add_ignores(ignores, root_path, "", -1,
3272 ".cvsignore");
3273 if (err)
3274 return err;
3276 err = add_ignores(ignores, root_path, "", -1,
3277 ".gitignore");
3278 if (err)
3279 return err;
3281 err = got_path_dirname(&parent_path, path);
3282 if (err) {
3283 if (err->code == GOT_ERR_BAD_PATH)
3284 return NULL; /* cannot traverse parent */
3285 return err;
3287 for (;;) {
3288 err = add_ignores(ignores, root_path, parent_path, -1,
3289 ".cvsignore");
3290 if (err)
3291 break;
3292 err = add_ignores(ignores, root_path, parent_path, -1,
3293 ".gitignore");
3294 if (err)
3295 break;
3296 err = got_path_dirname(&next_parent_path, parent_path);
3297 if (err) {
3298 if (err->code == GOT_ERR_BAD_PATH)
3299 err = NULL; /* traversed everything */
3300 break;
3302 free(parent_path);
3303 parent_path = next_parent_path;
3304 next_parent_path = NULL;
3307 free(parent_path);
3308 free(next_parent_path);
3309 return err;
3312 static const struct got_error *
3313 worktree_status(struct got_worktree *worktree, const char *path,
3314 struct got_fileindex *fileindex, struct got_repository *repo,
3315 got_worktree_status_cb status_cb, void *status_arg,
3316 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3317 int report_unchanged)
3319 const struct got_error *err = NULL;
3320 int fd = -1;
3321 struct got_fileindex_diff_dir_cb fdiff_cb;
3322 struct diff_dir_cb_arg arg;
3323 char *ondisk_path = NULL;
3325 TAILQ_INIT(&arg.ignores);
3327 if (asprintf(&ondisk_path, "%s%s%s",
3328 worktree->root_path, path[0] ? "/" : "", path) == -1)
3329 return got_error_from_errno("asprintf");
3331 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3332 if (fd == -1) {
3333 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3334 errno != ELOOP)
3335 err = got_error_from_errno2("open", ondisk_path);
3336 else
3337 err = report_single_file_status(path, ondisk_path,
3338 fileindex, status_cb, status_arg, repo,
3339 report_unchanged);
3340 } else {
3341 fdiff_cb.diff_old_new = status_old_new;
3342 fdiff_cb.diff_old = status_old;
3343 fdiff_cb.diff_new = status_new;
3344 fdiff_cb.diff_traverse = status_traverse;
3345 arg.fileindex = fileindex;
3346 arg.worktree = worktree;
3347 arg.status_path = path;
3348 arg.status_path_len = strlen(path);
3349 arg.repo = repo;
3350 arg.status_cb = status_cb;
3351 arg.status_arg = status_arg;
3352 arg.cancel_cb = cancel_cb;
3353 arg.cancel_arg = cancel_arg;
3354 arg.report_unchanged = report_unchanged;
3355 arg.no_ignores = no_ignores;
3356 if (!no_ignores) {
3357 err = add_ignores_from_parent_paths(&arg.ignores,
3358 worktree->root_path, path);
3359 if (err)
3360 goto done;
3362 err = got_fileindex_diff_dir(fileindex, fd,
3363 worktree->root_path, path, repo, &fdiff_cb, &arg);
3365 done:
3366 free_ignores(&arg.ignores);
3367 if (fd != -1 && close(fd) != 0 && err == NULL)
3368 err = got_error_from_errno("close");
3369 free(ondisk_path);
3370 return err;
3373 const struct got_error *
3374 got_worktree_status(struct got_worktree *worktree,
3375 struct got_pathlist_head *paths, struct got_repository *repo,
3376 got_worktree_status_cb status_cb, void *status_arg,
3377 got_cancel_cb cancel_cb, void *cancel_arg)
3379 const struct got_error *err = NULL;
3380 char *fileindex_path = NULL;
3381 struct got_fileindex *fileindex = NULL;
3382 struct got_pathlist_entry *pe;
3384 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3385 if (err)
3386 return err;
3388 TAILQ_FOREACH(pe, paths, entry) {
3389 err = worktree_status(worktree, pe->path, fileindex, repo,
3390 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3391 if (err)
3392 break;
3394 free(fileindex_path);
3395 got_fileindex_free(fileindex);
3396 return err;
3399 const struct got_error *
3400 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3401 const char *arg)
3403 const struct got_error *err = NULL;
3404 char *resolved = NULL, *cwd = NULL, *path = NULL;
3405 size_t len;
3406 struct stat sb;
3408 *wt_path = NULL;
3410 cwd = getcwd(NULL, 0);
3411 if (cwd == NULL)
3412 return got_error_from_errno("getcwd");
3414 if (lstat(arg, &sb) == -1) {
3415 if (errno != ENOENT) {
3416 err = got_error_from_errno2("lstat", arg);
3417 goto done;
3420 if (S_ISLNK(sb.st_mode)) {
3422 * We cannot use realpath(3) with symlinks since we want to
3423 * operate on the symlink itself.
3424 * But we can make the path absolute, assuming it is relative
3425 * to the current working directory, and then canonicalize it.
3427 char *abspath = NULL;
3428 char canonpath[PATH_MAX];
3429 if (!got_path_is_absolute(arg)) {
3430 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3431 err = got_error_from_errno("asprintf");
3432 goto done;
3436 err = got_canonpath(abspath ? abspath : arg, canonpath,
3437 sizeof(canonpath));
3438 if (err)
3439 goto done;
3440 resolved = strdup(canonpath);
3441 if (resolved == NULL) {
3442 err = got_error_from_errno("strdup");
3443 goto done;
3445 } else {
3446 resolved = realpath(arg, NULL);
3447 if (resolved == NULL) {
3448 if (errno != ENOENT) {
3449 err = got_error_from_errno2("realpath", arg);
3450 goto done;
3452 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3453 err = got_error_from_errno("asprintf");
3454 goto done;
3459 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3460 strlen(got_worktree_get_root_path(worktree)))) {
3461 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3462 goto done;
3465 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3466 err = got_path_skip_common_ancestor(&path,
3467 got_worktree_get_root_path(worktree), resolved);
3468 if (err)
3469 goto done;
3470 } else {
3471 path = strdup("");
3472 if (path == NULL) {
3473 err = got_error_from_errno("strdup");
3474 goto done;
3478 /* XXX status walk can't deal with trailing slash! */
3479 len = strlen(path);
3480 while (len > 0 && path[len - 1] == '/') {
3481 path[len - 1] = '\0';
3482 len--;
3484 done:
3485 free(resolved);
3486 free(cwd);
3487 if (err == NULL)
3488 *wt_path = path;
3489 else
3490 free(path);
3491 return err;
3494 struct schedule_addition_args {
3495 struct got_worktree *worktree;
3496 struct got_fileindex *fileindex;
3497 got_worktree_checkout_cb progress_cb;
3498 void *progress_arg;
3499 struct got_repository *repo;
3502 static const struct got_error *
3503 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3504 const char *relpath, struct got_object_id *blob_id,
3505 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3506 int dirfd, const char *de_name)
3508 struct schedule_addition_args *a = arg;
3509 const struct got_error *err = NULL;
3510 struct got_fileindex_entry *ie;
3511 struct stat sb;
3512 char *ondisk_path;
3514 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3515 relpath) == -1)
3516 return got_error_from_errno("asprintf");
3518 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3519 if (ie) {
3520 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3521 de_name, a->repo);
3522 if (err)
3523 goto done;
3524 /* Re-adding an existing entry is a no-op. */
3525 if (status == GOT_STATUS_ADD)
3526 goto done;
3527 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3528 if (err)
3529 goto done;
3532 if (status != GOT_STATUS_UNVERSIONED) {
3533 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3534 goto done;
3537 err = got_fileindex_entry_alloc(&ie, relpath);
3538 if (err)
3539 goto done;
3540 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3541 if (err) {
3542 got_fileindex_entry_free(ie);
3543 goto done;
3545 err = got_fileindex_entry_add(a->fileindex, ie);
3546 if (err) {
3547 got_fileindex_entry_free(ie);
3548 goto done;
3550 done:
3551 free(ondisk_path);
3552 if (err)
3553 return err;
3554 if (status == GOT_STATUS_ADD)
3555 return NULL;
3556 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3559 const struct got_error *
3560 got_worktree_schedule_add(struct got_worktree *worktree,
3561 struct got_pathlist_head *paths,
3562 got_worktree_checkout_cb progress_cb, void *progress_arg,
3563 struct got_repository *repo, int no_ignores)
3565 struct got_fileindex *fileindex = NULL;
3566 char *fileindex_path = NULL;
3567 const struct got_error *err = NULL, *sync_err, *unlockerr;
3568 struct got_pathlist_entry *pe;
3569 struct schedule_addition_args saa;
3571 err = lock_worktree(worktree, LOCK_EX);
3572 if (err)
3573 return err;
3575 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3576 if (err)
3577 goto done;
3579 saa.worktree = worktree;
3580 saa.fileindex = fileindex;
3581 saa.progress_cb = progress_cb;
3582 saa.progress_arg = progress_arg;
3583 saa.repo = repo;
3585 TAILQ_FOREACH(pe, paths, entry) {
3586 err = worktree_status(worktree, pe->path, fileindex, repo,
3587 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3588 if (err)
3589 break;
3591 sync_err = sync_fileindex(fileindex, fileindex_path);
3592 if (sync_err && err == NULL)
3593 err = sync_err;
3594 done:
3595 free(fileindex_path);
3596 if (fileindex)
3597 got_fileindex_free(fileindex);
3598 unlockerr = lock_worktree(worktree, LOCK_SH);
3599 if (unlockerr && err == NULL)
3600 err = unlockerr;
3601 return err;
3604 struct schedule_deletion_args {
3605 struct got_worktree *worktree;
3606 struct got_fileindex *fileindex;
3607 got_worktree_delete_cb progress_cb;
3608 void *progress_arg;
3609 struct got_repository *repo;
3610 int delete_local_mods;
3611 int keep_on_disk;
3614 static const struct got_error *
3615 schedule_for_deletion(void *arg, unsigned char status,
3616 unsigned char staged_status, const char *relpath,
3617 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3618 struct got_object_id *commit_id, int dirfd, const char *de_name)
3620 struct schedule_deletion_args *a = arg;
3621 const struct got_error *err = NULL;
3622 struct got_fileindex_entry *ie = NULL;
3623 struct stat sb;
3624 char *ondisk_path, *parent = NULL;
3626 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3627 if (ie == NULL)
3628 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3630 staged_status = get_staged_status(ie);
3631 if (staged_status != GOT_STATUS_NO_CHANGE) {
3632 if (staged_status == GOT_STATUS_DELETE)
3633 return NULL;
3634 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3637 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3638 relpath) == -1)
3639 return got_error_from_errno("asprintf");
3641 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3642 a->repo);
3643 if (err)
3644 goto done;
3646 if (status != GOT_STATUS_NO_CHANGE) {
3647 if (status == GOT_STATUS_DELETE)
3648 goto done;
3649 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3650 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3651 goto done;
3653 if (status != GOT_STATUS_MODIFY &&
3654 status != GOT_STATUS_MISSING) {
3655 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3656 goto done;
3660 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3661 if (dirfd != -1) {
3662 if (unlinkat(dirfd, de_name, 0) != 0) {
3663 err = got_error_from_errno2("unlinkat",
3664 ondisk_path);
3665 goto done;
3667 } else if (unlink(ondisk_path) != 0) {
3668 err = got_error_from_errno2("unlink", ondisk_path);
3669 goto done;
3672 parent = dirname(ondisk_path);
3674 if (parent == NULL) {
3675 err = got_error_from_errno2("dirname", ondisk_path);
3676 goto done;
3678 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3679 if (rmdir(parent) == -1) {
3680 if (errno != ENOTEMPTY)
3681 err = got_error_from_errno2("rmdir",
3682 parent);
3683 break;
3685 parent = dirname(parent);
3686 if (parent == NULL) {
3687 err = got_error_from_errno2("dirname", parent);
3688 goto done;
3693 got_fileindex_entry_mark_deleted_from_disk(ie);
3694 done:
3695 free(ondisk_path);
3696 if (err)
3697 return err;
3698 if (status == GOT_STATUS_DELETE)
3699 return NULL;
3700 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3701 staged_status, relpath);
3704 const struct got_error *
3705 got_worktree_schedule_delete(struct got_worktree *worktree,
3706 struct got_pathlist_head *paths, int delete_local_mods,
3707 got_worktree_delete_cb progress_cb, void *progress_arg,
3708 struct got_repository *repo, int keep_on_disk)
3710 struct got_fileindex *fileindex = NULL;
3711 char *fileindex_path = NULL;
3712 const struct got_error *err = NULL, *sync_err, *unlockerr;
3713 struct got_pathlist_entry *pe;
3714 struct schedule_deletion_args sda;
3716 err = lock_worktree(worktree, LOCK_EX);
3717 if (err)
3718 return err;
3720 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3721 if (err)
3722 goto done;
3724 sda.worktree = worktree;
3725 sda.fileindex = fileindex;
3726 sda.progress_cb = progress_cb;
3727 sda.progress_arg = progress_arg;
3728 sda.repo = repo;
3729 sda.delete_local_mods = delete_local_mods;
3730 sda.keep_on_disk = keep_on_disk;
3732 TAILQ_FOREACH(pe, paths, entry) {
3733 err = worktree_status(worktree, pe->path, fileindex, repo,
3734 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3735 if (err)
3736 break;
3738 sync_err = sync_fileindex(fileindex, fileindex_path);
3739 if (sync_err && err == NULL)
3740 err = sync_err;
3741 done:
3742 free(fileindex_path);
3743 if (fileindex)
3744 got_fileindex_free(fileindex);
3745 unlockerr = lock_worktree(worktree, LOCK_SH);
3746 if (unlockerr && err == NULL)
3747 err = unlockerr;
3748 return err;
3751 static const struct got_error *
3752 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3754 const struct got_error *err = NULL;
3755 char *line = NULL;
3756 size_t linesize = 0, n;
3757 ssize_t linelen;
3759 linelen = getline(&line, &linesize, infile);
3760 if (linelen == -1) {
3761 if (ferror(infile)) {
3762 err = got_error_from_errno("getline");
3763 goto done;
3765 return NULL;
3767 if (outfile) {
3768 n = fwrite(line, 1, linelen, outfile);
3769 if (n != linelen) {
3770 err = got_ferror(outfile, GOT_ERR_IO);
3771 goto done;
3774 if (rejectfile) {
3775 n = fwrite(line, 1, linelen, rejectfile);
3776 if (n != linelen)
3777 err = got_ferror(outfile, GOT_ERR_IO);
3779 done:
3780 free(line);
3781 return err;
3784 static const struct got_error *
3785 skip_one_line(FILE *f)
3787 char *line = NULL;
3788 size_t linesize = 0;
3789 ssize_t linelen;
3791 linelen = getline(&line, &linesize, f);
3792 if (linelen == -1) {
3793 if (ferror(f))
3794 return got_error_from_errno("getline");
3795 return NULL;
3797 free(line);
3798 return NULL;
3801 static const struct got_error *
3802 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3803 int start_old, int end_old, int start_new, int end_new,
3804 FILE *outfile, FILE *rejectfile)
3806 const struct got_error *err;
3808 /* Copy old file's lines leading up to patch. */
3809 while (!feof(f1) && *line_cur1 < start_old) {
3810 err = copy_one_line(f1, outfile, NULL);
3811 if (err)
3812 return err;
3813 (*line_cur1)++;
3815 /* Skip new file's lines leading up to patch. */
3816 while (!feof(f2) && *line_cur2 < start_new) {
3817 if (rejectfile)
3818 err = copy_one_line(f2, NULL, rejectfile);
3819 else
3820 err = skip_one_line(f2);
3821 if (err)
3822 return err;
3823 (*line_cur2)++;
3825 /* Copy patched lines. */
3826 while (!feof(f2) && *line_cur2 <= end_new) {
3827 err = copy_one_line(f2, outfile, NULL);
3828 if (err)
3829 return err;
3830 (*line_cur2)++;
3832 /* Skip over old file's replaced lines. */
3833 while (!feof(f1) && *line_cur1 <= end_old) {
3834 if (rejectfile)
3835 err = copy_one_line(f1, NULL, rejectfile);
3836 else
3837 err = skip_one_line(f1);
3838 if (err)
3839 return err;
3840 (*line_cur1)++;
3843 return NULL;
3846 static const struct got_error *
3847 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3848 FILE *outfile, FILE *rejectfile)
3850 const struct got_error *err;
3852 if (outfile) {
3853 /* Copy old file's lines until EOF. */
3854 while (!feof(f1)) {
3855 err = copy_one_line(f1, outfile, NULL);
3856 if (err)
3857 return err;
3858 (*line_cur1)++;
3861 if (rejectfile) {
3862 /* Copy new file's lines until EOF. */
3863 while (!feof(f2)) {
3864 err = copy_one_line(f2, NULL, rejectfile);
3865 if (err)
3866 return err;
3867 (*line_cur2)++;
3871 return NULL;
3874 static const struct got_error *
3875 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3876 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3877 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3878 int *line_cur2, FILE *outfile, FILE *rejectfile,
3879 got_worktree_patch_cb patch_cb, void *patch_arg)
3881 const struct got_error *err = NULL;
3882 int start_old = change->cv.a;
3883 int end_old = change->cv.b;
3884 int start_new = change->cv.c;
3885 int end_new = change->cv.d;
3886 long pos1, pos2;
3887 FILE *hunkfile;
3889 *choice = GOT_PATCH_CHOICE_NONE;
3891 hunkfile = got_opentemp();
3892 if (hunkfile == NULL)
3893 return got_error_from_errno("got_opentemp");
3895 pos1 = ftell(f1);
3896 pos2 = ftell(f2);
3898 /* XXX TODO needs error checking */
3899 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3901 if (fseek(f1, pos1, SEEK_SET) == -1) {
3902 err = got_ferror(f1, GOT_ERR_IO);
3903 goto done;
3905 if (fseek(f2, pos2, SEEK_SET) == -1) {
3906 err = got_ferror(f1, GOT_ERR_IO);
3907 goto done;
3909 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3910 err = got_ferror(hunkfile, GOT_ERR_IO);
3911 goto done;
3914 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3915 hunkfile, n, nchanges);
3916 if (err)
3917 goto done;
3919 switch (*choice) {
3920 case GOT_PATCH_CHOICE_YES:
3921 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3922 end_old, start_new, end_new, outfile, rejectfile);
3923 break;
3924 case GOT_PATCH_CHOICE_NO:
3925 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3926 end_old, start_new, end_new, rejectfile, outfile);
3927 break;
3928 case GOT_PATCH_CHOICE_QUIT:
3929 break;
3930 default:
3931 err = got_error(GOT_ERR_PATCH_CHOICE);
3932 break;
3934 done:
3935 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3936 err = got_error_from_errno("fclose");
3937 return err;
3940 struct revert_file_args {
3941 struct got_worktree *worktree;
3942 struct got_fileindex *fileindex;
3943 got_worktree_checkout_cb progress_cb;
3944 void *progress_arg;
3945 got_worktree_patch_cb patch_cb;
3946 void *patch_arg;
3947 struct got_repository *repo;
3950 static const struct got_error *
3951 create_patched_content(char **path_outfile, int reverse_patch,
3952 struct got_object_id *blob_id, const char *path2,
3953 int dirfd2, const char *de_name2,
3954 const char *relpath, struct got_repository *repo,
3955 got_worktree_patch_cb patch_cb, void *patch_arg)
3957 const struct got_error *err;
3958 struct got_blob_object *blob = NULL;
3959 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3960 int fd2 = -1;
3961 char *path1 = NULL, *id_str = NULL;
3962 struct stat sb1, sb2;
3963 struct got_diff_changes *changes = NULL;
3964 struct got_diff_state *ds = NULL;
3965 struct got_diff_args *args = NULL;
3966 struct got_diff_change *change;
3967 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3968 int n = 0;
3970 *path_outfile = NULL;
3972 err = got_object_id_str(&id_str, blob_id);
3973 if (err)
3974 return err;
3976 if (dirfd2 != -1) {
3977 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3978 if (fd2 == -1) {
3979 err = got_error_from_errno2("openat", path2);
3980 goto done;
3982 } else {
3983 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3984 if (fd2 == -1) {
3985 err = got_error_from_errno2("open", path2);
3986 goto done;
3989 if (fstat(fd2, &sb2) == -1) {
3990 err = got_error_from_errno2("fstat", path2);
3991 goto done;
3994 f2 = fdopen(fd2, "r");
3995 if (f2 == NULL) {
3996 err = got_error_from_errno2("fdopen", path2);
3997 goto done;
3999 fd2 = -1;
4001 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4002 if (err)
4003 goto done;
4005 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4006 if (err)
4007 goto done;
4009 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4010 if (err)
4011 goto done;
4013 if (stat(path1, &sb1) == -1) {
4014 err = got_error_from_errno2("stat", path1);
4015 goto done;
4018 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4019 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4020 if (err)
4021 goto done;
4023 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4024 if (err)
4025 goto done;
4027 if (fseek(f1, 0L, SEEK_SET) == -1)
4028 return got_ferror(f1, GOT_ERR_IO);
4029 if (fseek(f2, 0L, SEEK_SET) == -1)
4030 return got_ferror(f2, GOT_ERR_IO);
4031 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4032 int choice;
4033 err = apply_or_reject_change(&choice, change, ++n,
4034 changes->nchanges, ds, args, diff_flags, relpath,
4035 f1, f2, &line_cur1, &line_cur2,
4036 reverse_patch ? NULL : outfile,
4037 reverse_patch ? outfile : NULL,
4038 patch_cb, patch_arg);
4039 if (err)
4040 goto done;
4041 if (choice == GOT_PATCH_CHOICE_YES)
4042 have_content = 1;
4043 else if (choice == GOT_PATCH_CHOICE_QUIT)
4044 break;
4046 if (have_content) {
4047 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4048 reverse_patch ? NULL : outfile,
4049 reverse_patch ? outfile : NULL);
4050 if (err)
4051 goto done;
4053 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4054 err = got_error_from_errno2("chmod", path2);
4055 goto done;
4058 done:
4059 free(id_str);
4060 if (blob)
4061 got_object_blob_close(blob);
4062 if (f1 && fclose(f1) == EOF && err == NULL)
4063 err = got_error_from_errno2("fclose", path1);
4064 if (f2 && fclose(f2) == EOF && err == NULL)
4065 err = got_error_from_errno2("fclose", path2);
4066 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4067 err = got_error_from_errno2("close", path2);
4068 if (outfile && fclose(outfile) == EOF && err == NULL)
4069 err = got_error_from_errno2("fclose", *path_outfile);
4070 if (path1 && unlink(path1) == -1 && err == NULL)
4071 err = got_error_from_errno2("unlink", path1);
4072 if (err || !have_content) {
4073 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4074 err = got_error_from_errno2("unlink", *path_outfile);
4075 free(*path_outfile);
4076 *path_outfile = NULL;
4078 free(args);
4079 if (ds) {
4080 got_diff_state_free(ds);
4081 free(ds);
4083 if (changes)
4084 got_diff_free_changes(changes);
4085 free(path1);
4086 return err;
4089 static const struct got_error *
4090 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4091 const char *relpath, struct got_object_id *blob_id,
4092 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4093 int dirfd, const char *de_name)
4095 struct revert_file_args *a = arg;
4096 const struct got_error *err = NULL;
4097 char *parent_path = NULL;
4098 struct got_fileindex_entry *ie;
4099 struct got_tree_object *tree = NULL;
4100 struct got_object_id *tree_id = NULL;
4101 const struct got_tree_entry *te = NULL;
4102 char *tree_path = NULL, *te_name;
4103 char *ondisk_path = NULL, *path_content = NULL;
4104 struct got_blob_object *blob = NULL;
4106 /* Reverting a staged deletion is a no-op. */
4107 if (status == GOT_STATUS_DELETE &&
4108 staged_status != GOT_STATUS_NO_CHANGE)
4109 return NULL;
4111 if (status == GOT_STATUS_UNVERSIONED)
4112 return (*a->progress_cb)(a->progress_arg,
4113 GOT_STATUS_UNVERSIONED, relpath);
4115 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4116 if (ie == NULL)
4117 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4119 /* Construct in-repository path of tree which contains this blob. */
4120 err = got_path_dirname(&parent_path, ie->path);
4121 if (err) {
4122 if (err->code != GOT_ERR_BAD_PATH)
4123 goto done;
4124 parent_path = strdup("/");
4125 if (parent_path == NULL) {
4126 err = got_error_from_errno("strdup");
4127 goto done;
4130 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4131 tree_path = strdup(parent_path);
4132 if (tree_path == NULL) {
4133 err = got_error_from_errno("strdup");
4134 goto done;
4136 } else {
4137 if (got_path_is_root_dir(parent_path)) {
4138 tree_path = strdup(a->worktree->path_prefix);
4139 if (tree_path == NULL) {
4140 err = got_error_from_errno("strdup");
4141 goto done;
4143 } else {
4144 if (asprintf(&tree_path, "%s/%s",
4145 a->worktree->path_prefix, parent_path) == -1) {
4146 err = got_error_from_errno("asprintf");
4147 goto done;
4152 err = got_object_id_by_path(&tree_id, a->repo,
4153 a->worktree->base_commit_id, tree_path);
4154 if (err) {
4155 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4156 (status == GOT_STATUS_ADD ||
4157 staged_status == GOT_STATUS_ADD)))
4158 goto done;
4159 } else {
4160 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4161 if (err)
4162 goto done;
4164 te_name = basename(ie->path);
4165 if (te_name == NULL) {
4166 err = got_error_from_errno2("basename", ie->path);
4167 goto done;
4170 te = got_object_tree_find_entry(tree, te_name);
4171 if (te == NULL && status != GOT_STATUS_ADD &&
4172 staged_status != GOT_STATUS_ADD) {
4173 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4174 goto done;
4178 switch (status) {
4179 case GOT_STATUS_ADD:
4180 if (a->patch_cb) {
4181 int choice = GOT_PATCH_CHOICE_NONE;
4182 err = (*a->patch_cb)(&choice, a->patch_arg,
4183 status, ie->path, NULL, 1, 1);
4184 if (err)
4185 goto done;
4186 if (choice != GOT_PATCH_CHOICE_YES)
4187 break;
4189 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4190 ie->path);
4191 if (err)
4192 goto done;
4193 got_fileindex_entry_remove(a->fileindex, ie);
4194 break;
4195 case GOT_STATUS_DELETE:
4196 if (a->patch_cb) {
4197 int choice = GOT_PATCH_CHOICE_NONE;
4198 err = (*a->patch_cb)(&choice, a->patch_arg,
4199 status, ie->path, NULL, 1, 1);
4200 if (err)
4201 goto done;
4202 if (choice != GOT_PATCH_CHOICE_YES)
4203 break;
4205 /* fall through */
4206 case GOT_STATUS_MODIFY:
4207 case GOT_STATUS_MODE_CHANGE:
4208 case GOT_STATUS_CONFLICT:
4209 case GOT_STATUS_MISSING: {
4210 struct got_object_id id;
4211 if (staged_status == GOT_STATUS_ADD ||
4212 staged_status == GOT_STATUS_MODIFY) {
4213 memcpy(id.sha1, ie->staged_blob_sha1,
4214 SHA1_DIGEST_LENGTH);
4215 } else
4216 memcpy(id.sha1, ie->blob_sha1,
4217 SHA1_DIGEST_LENGTH);
4218 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4219 if (err)
4220 goto done;
4222 if (asprintf(&ondisk_path, "%s/%s",
4223 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4224 err = got_error_from_errno("asprintf");
4225 goto done;
4228 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4229 status == GOT_STATUS_CONFLICT)) {
4230 err = create_patched_content(&path_content, 1, &id,
4231 ondisk_path, dirfd, de_name, ie->path, a->repo,
4232 a->patch_cb, a->patch_arg);
4233 if (err || path_content == NULL)
4234 break;
4235 if (rename(path_content, ondisk_path) == -1) {
4236 err = got_error_from_errno3("rename",
4237 path_content, ondisk_path);
4238 goto done;
4240 } else {
4241 err = install_blob(a->worktree, ondisk_path, ie->path,
4242 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4243 got_fileindex_perms_to_st(ie), blob, 0, 1, 0,
4244 a->repo, a->progress_cb, a->progress_arg);
4245 if (err)
4246 goto done;
4247 if (status == GOT_STATUS_DELETE ||
4248 status == GOT_STATUS_MODE_CHANGE) {
4249 err = got_fileindex_entry_update(ie,
4250 ondisk_path, blob->id.sha1,
4251 a->worktree->base_commit_id->sha1, 1);
4252 if (err)
4253 goto done;
4256 break;
4258 default:
4259 break;
4261 done:
4262 free(ondisk_path);
4263 free(path_content);
4264 free(parent_path);
4265 free(tree_path);
4266 if (blob)
4267 got_object_blob_close(blob);
4268 if (tree)
4269 got_object_tree_close(tree);
4270 free(tree_id);
4271 return err;
4274 const struct got_error *
4275 got_worktree_revert(struct got_worktree *worktree,
4276 struct got_pathlist_head *paths,
4277 got_worktree_checkout_cb progress_cb, void *progress_arg,
4278 got_worktree_patch_cb patch_cb, void *patch_arg,
4279 struct got_repository *repo)
4281 struct got_fileindex *fileindex = NULL;
4282 char *fileindex_path = NULL;
4283 const struct got_error *err = NULL, *unlockerr = NULL;
4284 const struct got_error *sync_err = NULL;
4285 struct got_pathlist_entry *pe;
4286 struct revert_file_args rfa;
4288 err = lock_worktree(worktree, LOCK_EX);
4289 if (err)
4290 return err;
4292 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4293 if (err)
4294 goto done;
4296 rfa.worktree = worktree;
4297 rfa.fileindex = fileindex;
4298 rfa.progress_cb = progress_cb;
4299 rfa.progress_arg = progress_arg;
4300 rfa.patch_cb = patch_cb;
4301 rfa.patch_arg = patch_arg;
4302 rfa.repo = repo;
4303 TAILQ_FOREACH(pe, paths, entry) {
4304 err = worktree_status(worktree, pe->path, fileindex, repo,
4305 revert_file, &rfa, NULL, NULL, 0, 0);
4306 if (err)
4307 break;
4309 sync_err = sync_fileindex(fileindex, fileindex_path);
4310 if (sync_err && err == NULL)
4311 err = sync_err;
4312 done:
4313 free(fileindex_path);
4314 if (fileindex)
4315 got_fileindex_free(fileindex);
4316 unlockerr = lock_worktree(worktree, LOCK_SH);
4317 if (unlockerr && err == NULL)
4318 err = unlockerr;
4319 return err;
4322 static void
4323 free_commitable(struct got_commitable *ct)
4325 free(ct->path);
4326 free(ct->in_repo_path);
4327 free(ct->ondisk_path);
4328 free(ct->blob_id);
4329 free(ct->base_blob_id);
4330 free(ct->staged_blob_id);
4331 free(ct->base_commit_id);
4332 free(ct);
4335 struct collect_commitables_arg {
4336 struct got_pathlist_head *commitable_paths;
4337 struct got_repository *repo;
4338 struct got_worktree *worktree;
4339 int have_staged_files;
4342 static const struct got_error *
4343 collect_commitables(void *arg, unsigned char status,
4344 unsigned char staged_status, const char *relpath,
4345 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4346 struct got_object_id *commit_id, int dirfd, const char *de_name)
4348 struct collect_commitables_arg *a = arg;
4349 const struct got_error *err = NULL;
4350 struct got_commitable *ct = NULL;
4351 struct got_pathlist_entry *new = NULL;
4352 char *parent_path = NULL, *path = NULL;
4353 struct stat sb;
4355 if (a->have_staged_files) {
4356 if (staged_status != GOT_STATUS_MODIFY &&
4357 staged_status != GOT_STATUS_ADD &&
4358 staged_status != GOT_STATUS_DELETE)
4359 return NULL;
4360 } else {
4361 if (status == GOT_STATUS_CONFLICT)
4362 return got_error(GOT_ERR_COMMIT_CONFLICT);
4364 if (status != GOT_STATUS_MODIFY &&
4365 status != GOT_STATUS_MODE_CHANGE &&
4366 status != GOT_STATUS_ADD &&
4367 status != GOT_STATUS_DELETE)
4368 return NULL;
4371 if (asprintf(&path, "/%s", relpath) == -1) {
4372 err = got_error_from_errno("asprintf");
4373 goto done;
4375 if (strcmp(path, "/") == 0) {
4376 parent_path = strdup("");
4377 if (parent_path == NULL)
4378 return got_error_from_errno("strdup");
4379 } else {
4380 err = got_path_dirname(&parent_path, path);
4381 if (err)
4382 return err;
4385 ct = calloc(1, sizeof(*ct));
4386 if (ct == NULL) {
4387 err = got_error_from_errno("calloc");
4388 goto done;
4391 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4392 relpath) == -1) {
4393 err = got_error_from_errno("asprintf");
4394 goto done;
4396 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4397 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4398 } else {
4399 if (dirfd != -1) {
4400 if (fstatat(dirfd, de_name, &sb,
4401 AT_SYMLINK_NOFOLLOW) == -1) {
4402 err = got_error_from_errno2("fstatat",
4403 ct->ondisk_path);
4404 goto done;
4406 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4407 err = got_error_from_errno2("lstat", ct->ondisk_path);
4408 goto done;
4410 ct->mode = sb.st_mode;
4413 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4414 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4415 relpath) == -1) {
4416 err = got_error_from_errno("asprintf");
4417 goto done;
4420 ct->status = status;
4421 ct->staged_status = staged_status;
4422 ct->blob_id = NULL; /* will be filled in when blob gets created */
4423 if (ct->status != GOT_STATUS_ADD &&
4424 ct->staged_status != GOT_STATUS_ADD) {
4425 ct->base_blob_id = got_object_id_dup(blob_id);
4426 if (ct->base_blob_id == NULL) {
4427 err = got_error_from_errno("got_object_id_dup");
4428 goto done;
4430 ct->base_commit_id = got_object_id_dup(commit_id);
4431 if (ct->base_commit_id == NULL) {
4432 err = got_error_from_errno("got_object_id_dup");
4433 goto done;
4436 if (ct->staged_status == GOT_STATUS_ADD ||
4437 ct->staged_status == GOT_STATUS_MODIFY) {
4438 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4439 if (ct->staged_blob_id == NULL) {
4440 err = got_error_from_errno("got_object_id_dup");
4441 goto done;
4444 ct->path = strdup(path);
4445 if (ct->path == NULL) {
4446 err = got_error_from_errno("strdup");
4447 goto done;
4449 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4450 done:
4451 if (ct && (err || new == NULL))
4452 free_commitable(ct);
4453 free(parent_path);
4454 free(path);
4455 return err;
4458 static const struct got_error *write_tree(struct got_object_id **, int *,
4459 struct got_tree_object *, const char *, struct got_pathlist_head *,
4460 got_worktree_status_cb status_cb, void *status_arg,
4461 struct got_repository *);
4463 static const struct got_error *
4464 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4465 struct got_tree_entry *te, const char *parent_path,
4466 struct got_pathlist_head *commitable_paths,
4467 got_worktree_status_cb status_cb, void *status_arg,
4468 struct got_repository *repo)
4470 const struct got_error *err = NULL;
4471 struct got_tree_object *subtree;
4472 char *subpath;
4474 if (asprintf(&subpath, "%s%s%s", parent_path,
4475 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4476 return got_error_from_errno("asprintf");
4478 err = got_object_open_as_tree(&subtree, repo, &te->id);
4479 if (err)
4480 return err;
4482 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4483 commitable_paths, status_cb, status_arg, repo);
4484 got_object_tree_close(subtree);
4485 free(subpath);
4486 return err;
4489 static const struct got_error *
4490 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4492 const struct got_error *err = NULL;
4493 char *ct_parent_path = NULL;
4495 *match = 0;
4497 if (strchr(ct->in_repo_path, '/') == NULL) {
4498 *match = got_path_is_root_dir(path);
4499 return NULL;
4502 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4503 if (err)
4504 return err;
4505 *match = (strcmp(path, ct_parent_path) == 0);
4506 free(ct_parent_path);
4507 return err;
4510 static mode_t
4511 get_ct_file_mode(struct got_commitable *ct)
4513 if (S_ISLNK(ct->mode))
4514 return S_IFLNK;
4516 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4519 static const struct got_error *
4520 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4521 struct got_tree_entry *te, struct got_commitable *ct)
4523 const struct got_error *err = NULL;
4525 *new_te = NULL;
4527 err = got_object_tree_entry_dup(new_te, te);
4528 if (err)
4529 goto done;
4531 (*new_te)->mode = get_ct_file_mode(ct);
4533 if (ct->staged_status == GOT_STATUS_MODIFY)
4534 memcpy(&(*new_te)->id, ct->staged_blob_id,
4535 sizeof((*new_te)->id));
4536 else
4537 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4538 done:
4539 if (err && *new_te) {
4540 free(*new_te);
4541 *new_te = NULL;
4543 return err;
4546 static const struct got_error *
4547 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4548 struct got_commitable *ct)
4550 const struct got_error *err = NULL;
4551 char *ct_name;
4553 *new_te = NULL;
4555 *new_te = calloc(1, sizeof(**new_te));
4556 if (*new_te == NULL)
4557 return got_error_from_errno("calloc");
4559 ct_name = basename(ct->path);
4560 if (ct_name == NULL) {
4561 err = got_error_from_errno2("basename", ct->path);
4562 goto done;
4564 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4565 sizeof((*new_te)->name)) {
4566 err = got_error(GOT_ERR_NO_SPACE);
4567 goto done;
4570 (*new_te)->mode = get_ct_file_mode(ct);
4572 if (ct->staged_status == GOT_STATUS_ADD)
4573 memcpy(&(*new_te)->id, ct->staged_blob_id,
4574 sizeof((*new_te)->id));
4575 else
4576 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4577 done:
4578 if (err && *new_te) {
4579 free(*new_te);
4580 *new_te = NULL;
4582 return err;
4585 static const struct got_error *
4586 insert_tree_entry(struct got_tree_entry *new_te,
4587 struct got_pathlist_head *paths)
4589 const struct got_error *err = NULL;
4590 struct got_pathlist_entry *new_pe;
4592 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4593 if (err)
4594 return err;
4595 if (new_pe == NULL)
4596 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4597 return NULL;
4600 static const struct got_error *
4601 report_ct_status(struct got_commitable *ct,
4602 got_worktree_status_cb status_cb, void *status_arg)
4604 const char *ct_path = ct->path;
4605 unsigned char status;
4607 while (ct_path[0] == '/')
4608 ct_path++;
4610 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4611 status = ct->staged_status;
4612 else
4613 status = ct->status;
4615 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4616 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4619 static const struct got_error *
4620 match_modified_subtree(int *modified, struct got_tree_entry *te,
4621 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4623 const struct got_error *err = NULL;
4624 struct got_pathlist_entry *pe;
4625 char *te_path;
4627 *modified = 0;
4629 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4630 got_path_is_root_dir(base_tree_path) ? "" : "/",
4631 te->name) == -1)
4632 return got_error_from_errno("asprintf");
4634 TAILQ_FOREACH(pe, commitable_paths, entry) {
4635 struct got_commitable *ct = pe->data;
4636 *modified = got_path_is_child(ct->in_repo_path, te_path,
4637 strlen(te_path));
4638 if (*modified)
4639 break;
4642 free(te_path);
4643 return err;
4646 static const struct got_error *
4647 match_deleted_or_modified_ct(struct got_commitable **ctp,
4648 struct got_tree_entry *te, const char *base_tree_path,
4649 struct got_pathlist_head *commitable_paths)
4651 const struct got_error *err = NULL;
4652 struct got_pathlist_entry *pe;
4654 *ctp = NULL;
4656 TAILQ_FOREACH(pe, commitable_paths, entry) {
4657 struct got_commitable *ct = pe->data;
4658 char *ct_name = NULL;
4659 int path_matches;
4661 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4662 if (ct->status != GOT_STATUS_MODIFY &&
4663 ct->status != GOT_STATUS_MODE_CHANGE &&
4664 ct->status != GOT_STATUS_DELETE)
4665 continue;
4666 } else {
4667 if (ct->staged_status != GOT_STATUS_MODIFY &&
4668 ct->staged_status != GOT_STATUS_DELETE)
4669 continue;
4672 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4673 continue;
4675 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4676 if (err)
4677 return err;
4678 if (!path_matches)
4679 continue;
4681 ct_name = basename(pe->path);
4682 if (ct_name == NULL)
4683 return got_error_from_errno2("basename", pe->path);
4685 if (strcmp(te->name, ct_name) != 0)
4686 continue;
4688 *ctp = ct;
4689 break;
4692 return err;
4695 static const struct got_error *
4696 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4697 const char *child_path, const char *path_base_tree,
4698 struct got_pathlist_head *commitable_paths,
4699 got_worktree_status_cb status_cb, void *status_arg,
4700 struct got_repository *repo)
4702 const struct got_error *err = NULL;
4703 struct got_tree_entry *new_te;
4704 char *subtree_path;
4705 struct got_object_id *id = NULL;
4706 int nentries;
4708 *new_tep = NULL;
4710 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4711 got_path_is_root_dir(path_base_tree) ? "" : "/",
4712 child_path) == -1)
4713 return got_error_from_errno("asprintf");
4715 new_te = calloc(1, sizeof(*new_te));
4716 if (new_te == NULL)
4717 return got_error_from_errno("calloc");
4718 new_te->mode = S_IFDIR;
4720 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4721 sizeof(new_te->name)) {
4722 err = got_error(GOT_ERR_NO_SPACE);
4723 goto done;
4725 err = write_tree(&id, &nentries, NULL, subtree_path,
4726 commitable_paths, status_cb, status_arg, repo);
4727 if (err) {
4728 free(new_te);
4729 goto done;
4731 memcpy(&new_te->id, id, sizeof(new_te->id));
4732 done:
4733 free(id);
4734 free(subtree_path);
4735 if (err == NULL)
4736 *new_tep = new_te;
4737 return err;
4740 static const struct got_error *
4741 write_tree(struct got_object_id **new_tree_id, int *nentries,
4742 struct got_tree_object *base_tree, const char *path_base_tree,
4743 struct got_pathlist_head *commitable_paths,
4744 got_worktree_status_cb status_cb, void *status_arg,
4745 struct got_repository *repo)
4747 const struct got_error *err = NULL;
4748 struct got_pathlist_head paths;
4749 struct got_tree_entry *te, *new_te = NULL;
4750 struct got_pathlist_entry *pe;
4752 TAILQ_INIT(&paths);
4753 *nentries = 0;
4755 /* Insert, and recurse into, newly added entries first. */
4756 TAILQ_FOREACH(pe, commitable_paths, entry) {
4757 struct got_commitable *ct = pe->data;
4758 char *child_path = NULL, *slash;
4760 if ((ct->status != GOT_STATUS_ADD &&
4761 ct->staged_status != GOT_STATUS_ADD) ||
4762 (ct->flags & GOT_COMMITABLE_ADDED))
4763 continue;
4765 if (!got_path_is_child(pe->path, path_base_tree,
4766 strlen(path_base_tree)))
4767 continue;
4769 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4770 pe->path);
4771 if (err)
4772 goto done;
4774 slash = strchr(child_path, '/');
4775 if (slash == NULL) {
4776 err = alloc_added_blob_tree_entry(&new_te, ct);
4777 if (err)
4778 goto done;
4779 err = report_ct_status(ct, status_cb, status_arg);
4780 if (err)
4781 goto done;
4782 ct->flags |= GOT_COMMITABLE_ADDED;
4783 err = insert_tree_entry(new_te, &paths);
4784 if (err)
4785 goto done;
4786 (*nentries)++;
4787 } else {
4788 *slash = '\0'; /* trim trailing path components */
4789 if (base_tree == NULL ||
4790 got_object_tree_find_entry(base_tree, child_path)
4791 == NULL) {
4792 err = make_subtree_for_added_blob(&new_te,
4793 child_path, path_base_tree,
4794 commitable_paths, status_cb, status_arg,
4795 repo);
4796 if (err)
4797 goto done;
4798 err = insert_tree_entry(new_te, &paths);
4799 if (err)
4800 goto done;
4801 (*nentries)++;
4806 if (base_tree) {
4807 int i, nbase_entries;
4808 /* Handle modified and deleted entries. */
4809 nbase_entries = got_object_tree_get_nentries(base_tree);
4810 for (i = 0; i < nbase_entries; i++) {
4811 struct got_commitable *ct = NULL;
4813 te = got_object_tree_get_entry(base_tree, i);
4814 if (got_object_tree_entry_is_submodule(te)) {
4815 /* Entry is a submodule; just copy it. */
4816 err = got_object_tree_entry_dup(&new_te, te);
4817 if (err)
4818 goto done;
4819 err = insert_tree_entry(new_te, &paths);
4820 if (err)
4821 goto done;
4822 (*nentries)++;
4823 continue;
4826 if (S_ISDIR(te->mode)) {
4827 int modified;
4828 err = got_object_tree_entry_dup(&new_te, te);
4829 if (err)
4830 goto done;
4831 err = match_modified_subtree(&modified, te,
4832 path_base_tree, commitable_paths);
4833 if (err)
4834 goto done;
4835 /* Avoid recursion into unmodified subtrees. */
4836 if (modified) {
4837 struct got_object_id *new_id;
4838 int nsubentries;
4839 err = write_subtree(&new_id,
4840 &nsubentries, te,
4841 path_base_tree, commitable_paths,
4842 status_cb, status_arg, repo);
4843 if (err)
4844 goto done;
4845 if (nsubentries == 0) {
4846 /* All entries were deleted. */
4847 free(new_id);
4848 continue;
4850 memcpy(&new_te->id, new_id,
4851 sizeof(new_te->id));
4852 free(new_id);
4854 err = insert_tree_entry(new_te, &paths);
4855 if (err)
4856 goto done;
4857 (*nentries)++;
4858 continue;
4861 err = match_deleted_or_modified_ct(&ct, te,
4862 path_base_tree, commitable_paths);
4863 if (err)
4864 goto done;
4865 if (ct) {
4866 /* NB: Deleted entries get dropped here. */
4867 if (ct->status == GOT_STATUS_MODIFY ||
4868 ct->status == GOT_STATUS_MODE_CHANGE ||
4869 ct->staged_status == GOT_STATUS_MODIFY) {
4870 err = alloc_modified_blob_tree_entry(
4871 &new_te, te, ct);
4872 if (err)
4873 goto done;
4874 err = insert_tree_entry(new_te, &paths);
4875 if (err)
4876 goto done;
4877 (*nentries)++;
4879 err = report_ct_status(ct, status_cb,
4880 status_arg);
4881 if (err)
4882 goto done;
4883 } else {
4884 /* Entry is unchanged; just copy it. */
4885 err = got_object_tree_entry_dup(&new_te, te);
4886 if (err)
4887 goto done;
4888 err = insert_tree_entry(new_te, &paths);
4889 if (err)
4890 goto done;
4891 (*nentries)++;
4896 /* Write new list of entries; deleted entries have been dropped. */
4897 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4898 done:
4899 got_pathlist_free(&paths);
4900 return err;
4903 static const struct got_error *
4904 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4905 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4906 int have_staged_files)
4908 const struct got_error *err = NULL;
4909 struct got_pathlist_entry *pe;
4911 TAILQ_FOREACH(pe, commitable_paths, entry) {
4912 struct got_fileindex_entry *ie;
4913 struct got_commitable *ct = pe->data;
4915 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4916 if (ie) {
4917 if (ct->status == GOT_STATUS_DELETE ||
4918 ct->staged_status == GOT_STATUS_DELETE) {
4919 got_fileindex_entry_remove(fileindex, ie);
4920 } else if (ct->staged_status == GOT_STATUS_ADD ||
4921 ct->staged_status == GOT_STATUS_MODIFY) {
4922 got_fileindex_entry_stage_set(ie,
4923 GOT_FILEIDX_STAGE_NONE);
4924 err = got_fileindex_entry_update(ie,
4925 ct->ondisk_path, ct->staged_blob_id->sha1,
4926 new_base_commit_id->sha1,
4927 !have_staged_files);
4928 } else
4929 err = got_fileindex_entry_update(ie,
4930 ct->ondisk_path, ct->blob_id->sha1,
4931 new_base_commit_id->sha1,
4932 !have_staged_files);
4933 } else {
4934 err = got_fileindex_entry_alloc(&ie, pe->path);
4935 if (err)
4936 break;
4937 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4938 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4939 if (err) {
4940 got_fileindex_entry_free(ie);
4941 break;
4943 err = got_fileindex_entry_add(fileindex, ie);
4944 if (err) {
4945 got_fileindex_entry_free(ie);
4946 break;
4950 return err;
4954 static const struct got_error *
4955 check_out_of_date(const char *in_repo_path, unsigned char status,
4956 unsigned char staged_status, struct got_object_id *base_blob_id,
4957 struct got_object_id *base_commit_id,
4958 struct got_object_id *head_commit_id, struct got_repository *repo,
4959 int ood_errcode)
4961 const struct got_error *err = NULL;
4962 struct got_object_id *id = NULL;
4964 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4965 /* Trivial case: base commit == head commit */
4966 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4967 return NULL;
4969 * Ensure file content which local changes were based
4970 * on matches file content in the branch head.
4972 err = got_object_id_by_path(&id, repo, head_commit_id,
4973 in_repo_path);
4974 if (err) {
4975 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4976 err = got_error(ood_errcode);
4977 goto done;
4978 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4979 err = got_error(ood_errcode);
4980 } else {
4981 /* Require that added files don't exist in the branch head. */
4982 err = got_object_id_by_path(&id, repo, head_commit_id,
4983 in_repo_path);
4984 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4985 goto done;
4986 err = id ? got_error(ood_errcode) : NULL;
4988 done:
4989 free(id);
4990 return err;
4993 const struct got_error *
4994 commit_worktree(struct got_object_id **new_commit_id,
4995 struct got_pathlist_head *commitable_paths,
4996 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4997 const char *author, const char *committer,
4998 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4999 got_worktree_status_cb status_cb, void *status_arg,
5000 struct got_repository *repo)
5002 const struct got_error *err = NULL, *unlockerr = NULL;
5003 struct got_pathlist_entry *pe;
5004 const char *head_ref_name = NULL;
5005 struct got_commit_object *head_commit = NULL;
5006 struct got_reference *head_ref2 = NULL;
5007 struct got_object_id *head_commit_id2 = NULL;
5008 struct got_tree_object *head_tree = NULL;
5009 struct got_object_id *new_tree_id = NULL;
5010 int nentries;
5011 struct got_object_id_queue parent_ids;
5012 struct got_object_qid *pid = NULL;
5013 char *logmsg = NULL;
5015 *new_commit_id = NULL;
5017 SIMPLEQ_INIT(&parent_ids);
5019 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5020 if (err)
5021 goto done;
5023 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5024 if (err)
5025 goto done;
5027 if (commit_msg_cb != NULL) {
5028 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5029 if (err)
5030 goto done;
5033 if (logmsg == NULL || strlen(logmsg) == 0) {
5034 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5035 goto done;
5038 /* Create blobs from added and modified files and record their IDs. */
5039 TAILQ_FOREACH(pe, commitable_paths, entry) {
5040 struct got_commitable *ct = pe->data;
5041 char *ondisk_path;
5043 /* Blobs for staged files already exist. */
5044 if (ct->staged_status == GOT_STATUS_ADD ||
5045 ct->staged_status == GOT_STATUS_MODIFY)
5046 continue;
5048 if (ct->status != GOT_STATUS_ADD &&
5049 ct->status != GOT_STATUS_MODIFY &&
5050 ct->status != GOT_STATUS_MODE_CHANGE)
5051 continue;
5053 if (asprintf(&ondisk_path, "%s/%s",
5054 worktree->root_path, pe->path) == -1) {
5055 err = got_error_from_errno("asprintf");
5056 goto done;
5058 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5059 if (err) {
5060 free(ondisk_path);
5061 goto done;
5065 * When comitting a symlink we convert "bad" symlinks (those
5066 * which point outside the work tree or into .got) to regular
5067 * files. This way, the post-commit work tree state matches
5068 * a fresh checkout of the tree which was committed.
5070 if (S_ISLNK(get_ct_file_mode(ct))) {
5071 struct got_blob_object *blob;
5072 err = got_object_open_as_blob(&blob, repo, ct->blob_id,
5073 PATH_MAX);
5074 if (err) {
5075 free(ondisk_path);
5076 goto done;
5078 err = install_symlink(worktree, ondisk_path, ct->path,
5079 get_ct_file_mode(ct), GOT_DEFAULT_FILE_MODE, blob,
5080 0, 0, repo, NULL, NULL);
5081 got_object_blob_close(blob);
5082 if (err) {
5083 free(ondisk_path);
5084 goto done;
5087 free(ondisk_path);
5090 /* Recursively write new tree objects. */
5091 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5092 commitable_paths, status_cb, status_arg, repo);
5093 if (err)
5094 goto done;
5096 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5097 if (err)
5098 goto done;
5099 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5100 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5101 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5102 got_object_qid_free(pid);
5103 if (logmsg != NULL)
5104 free(logmsg);
5105 if (err)
5106 goto done;
5108 /* Check if a concurrent commit to our branch has occurred. */
5109 head_ref_name = got_worktree_get_head_ref_name(worktree);
5110 if (head_ref_name == NULL) {
5111 err = got_error_from_errno("got_worktree_get_head_ref_name");
5112 goto done;
5114 /* Lock the reference here to prevent concurrent modification. */
5115 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5116 if (err)
5117 goto done;
5118 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5119 if (err)
5120 goto done;
5121 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5122 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5123 goto done;
5125 /* Update branch head in repository. */
5126 err = got_ref_change_ref(head_ref2, *new_commit_id);
5127 if (err)
5128 goto done;
5129 err = got_ref_write(head_ref2, repo);
5130 if (err)
5131 goto done;
5133 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5134 if (err)
5135 goto done;
5137 err = ref_base_commit(worktree, repo);
5138 if (err)
5139 goto done;
5140 done:
5141 if (head_tree)
5142 got_object_tree_close(head_tree);
5143 if (head_commit)
5144 got_object_commit_close(head_commit);
5145 free(head_commit_id2);
5146 if (head_ref2) {
5147 unlockerr = got_ref_unlock(head_ref2);
5148 if (unlockerr && err == NULL)
5149 err = unlockerr;
5150 got_ref_close(head_ref2);
5152 return err;
5155 static const struct got_error *
5156 check_path_is_commitable(const char *path,
5157 struct got_pathlist_head *commitable_paths)
5159 struct got_pathlist_entry *cpe = NULL;
5160 size_t path_len = strlen(path);
5162 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5163 struct got_commitable *ct = cpe->data;
5164 const char *ct_path = ct->path;
5166 while (ct_path[0] == '/')
5167 ct_path++;
5169 if (strcmp(path, ct_path) == 0 ||
5170 got_path_is_child(ct_path, path, path_len))
5171 break;
5174 if (cpe == NULL)
5175 return got_error_path(path, GOT_ERR_BAD_PATH);
5177 return NULL;
5180 static const struct got_error *
5181 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5183 int *have_staged_files = arg;
5185 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5186 *have_staged_files = 1;
5187 return got_error(GOT_ERR_CANCELLED);
5190 return NULL;
5193 static const struct got_error *
5194 check_non_staged_files(struct got_fileindex *fileindex,
5195 struct got_pathlist_head *paths)
5197 struct got_pathlist_entry *pe;
5198 struct got_fileindex_entry *ie;
5200 TAILQ_FOREACH(pe, paths, entry) {
5201 if (pe->path[0] == '\0')
5202 continue;
5203 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5204 if (ie == NULL)
5205 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5206 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5207 return got_error_path(pe->path,
5208 GOT_ERR_FILE_NOT_STAGED);
5211 return NULL;
5214 const struct got_error *
5215 got_worktree_commit(struct got_object_id **new_commit_id,
5216 struct got_worktree *worktree, struct got_pathlist_head *paths,
5217 const char *author, const char *committer,
5218 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5219 got_worktree_status_cb status_cb, void *status_arg,
5220 struct got_repository *repo)
5222 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5223 struct got_fileindex *fileindex = NULL;
5224 char *fileindex_path = NULL;
5225 struct got_pathlist_head commitable_paths;
5226 struct collect_commitables_arg cc_arg;
5227 struct got_pathlist_entry *pe;
5228 struct got_reference *head_ref = NULL;
5229 struct got_object_id *head_commit_id = NULL;
5230 int have_staged_files = 0;
5232 *new_commit_id = NULL;
5234 TAILQ_INIT(&commitable_paths);
5236 err = lock_worktree(worktree, LOCK_EX);
5237 if (err)
5238 goto done;
5240 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5241 if (err)
5242 goto done;
5244 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5245 if (err)
5246 goto done;
5248 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5249 if (err)
5250 goto done;
5252 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5253 &have_staged_files);
5254 if (err && err->code != GOT_ERR_CANCELLED)
5255 goto done;
5256 if (have_staged_files) {
5257 err = check_non_staged_files(fileindex, paths);
5258 if (err)
5259 goto done;
5262 cc_arg.commitable_paths = &commitable_paths;
5263 cc_arg.worktree = worktree;
5264 cc_arg.repo = repo;
5265 cc_arg.have_staged_files = have_staged_files;
5266 TAILQ_FOREACH(pe, paths, entry) {
5267 err = worktree_status(worktree, pe->path, fileindex, repo,
5268 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5269 if (err)
5270 goto done;
5273 if (TAILQ_EMPTY(&commitable_paths)) {
5274 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5275 goto done;
5278 TAILQ_FOREACH(pe, paths, entry) {
5279 err = check_path_is_commitable(pe->path, &commitable_paths);
5280 if (err)
5281 goto done;
5284 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5285 struct got_commitable *ct = pe->data;
5286 const char *ct_path = ct->in_repo_path;
5288 while (ct_path[0] == '/')
5289 ct_path++;
5290 err = check_out_of_date(ct_path, ct->status,
5291 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5292 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5293 if (err)
5294 goto done;
5298 err = commit_worktree(new_commit_id, &commitable_paths,
5299 head_commit_id, worktree, author, committer,
5300 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5301 if (err)
5302 goto done;
5304 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5305 fileindex, have_staged_files);
5306 sync_err = sync_fileindex(fileindex, fileindex_path);
5307 if (sync_err && err == NULL)
5308 err = sync_err;
5309 done:
5310 if (fileindex)
5311 got_fileindex_free(fileindex);
5312 free(fileindex_path);
5313 unlockerr = lock_worktree(worktree, LOCK_SH);
5314 if (unlockerr && err == NULL)
5315 err = unlockerr;
5316 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5317 struct got_commitable *ct = pe->data;
5318 free_commitable(ct);
5320 got_pathlist_free(&commitable_paths);
5321 return err;
5324 const char *
5325 got_commitable_get_path(struct got_commitable *ct)
5327 return ct->path;
5330 unsigned int
5331 got_commitable_get_status(struct got_commitable *ct)
5333 return ct->status;
5336 struct check_rebase_ok_arg {
5337 struct got_worktree *worktree;
5338 struct got_repository *repo;
5341 static const struct got_error *
5342 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5344 const struct got_error *err = NULL;
5345 struct check_rebase_ok_arg *a = arg;
5346 unsigned char status;
5347 struct stat sb;
5348 char *ondisk_path;
5350 /* Reject rebase of a work tree with mixed base commits. */
5351 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5352 SHA1_DIGEST_LENGTH))
5353 return got_error(GOT_ERR_MIXED_COMMITS);
5355 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5356 == -1)
5357 return got_error_from_errno("asprintf");
5359 /* Reject rebase of a work tree with modified or staged files. */
5360 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5361 free(ondisk_path);
5362 if (err)
5363 return err;
5365 if (status != GOT_STATUS_NO_CHANGE)
5366 return got_error(GOT_ERR_MODIFIED);
5367 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5368 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5370 return NULL;
5373 const struct got_error *
5374 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5375 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5376 struct got_worktree *worktree, struct got_reference *branch,
5377 struct got_repository *repo)
5379 const struct got_error *err = NULL;
5380 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5381 char *branch_ref_name = NULL;
5382 char *fileindex_path = NULL;
5383 struct check_rebase_ok_arg ok_arg;
5384 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5385 struct got_object_id *wt_branch_tip = NULL;
5387 *new_base_branch_ref = NULL;
5388 *tmp_branch = NULL;
5389 *fileindex = NULL;
5391 err = lock_worktree(worktree, LOCK_EX);
5392 if (err)
5393 return err;
5395 err = open_fileindex(fileindex, &fileindex_path, worktree);
5396 if (err)
5397 goto done;
5399 ok_arg.worktree = worktree;
5400 ok_arg.repo = repo;
5401 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5402 &ok_arg);
5403 if (err)
5404 goto done;
5406 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5407 if (err)
5408 goto done;
5410 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5411 if (err)
5412 goto done;
5414 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5415 if (err)
5416 goto done;
5418 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5419 0);
5420 if (err)
5421 goto done;
5423 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5424 if (err)
5425 goto done;
5426 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5427 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5428 goto done;
5431 err = got_ref_alloc_symref(new_base_branch_ref,
5432 new_base_branch_ref_name, wt_branch);
5433 if (err)
5434 goto done;
5435 err = got_ref_write(*new_base_branch_ref, repo);
5436 if (err)
5437 goto done;
5439 /* TODO Lock original branch's ref while rebasing? */
5441 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5442 if (err)
5443 goto done;
5445 err = got_ref_write(branch_ref, repo);
5446 if (err)
5447 goto done;
5449 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5450 worktree->base_commit_id);
5451 if (err)
5452 goto done;
5453 err = got_ref_write(*tmp_branch, repo);
5454 if (err)
5455 goto done;
5457 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5458 if (err)
5459 goto done;
5460 done:
5461 free(fileindex_path);
5462 free(tmp_branch_name);
5463 free(new_base_branch_ref_name);
5464 free(branch_ref_name);
5465 if (branch_ref)
5466 got_ref_close(branch_ref);
5467 if (wt_branch)
5468 got_ref_close(wt_branch);
5469 free(wt_branch_tip);
5470 if (err) {
5471 if (*new_base_branch_ref) {
5472 got_ref_close(*new_base_branch_ref);
5473 *new_base_branch_ref = NULL;
5475 if (*tmp_branch) {
5476 got_ref_close(*tmp_branch);
5477 *tmp_branch = NULL;
5479 if (*fileindex) {
5480 got_fileindex_free(*fileindex);
5481 *fileindex = NULL;
5483 lock_worktree(worktree, LOCK_SH);
5485 return err;
5488 const struct got_error *
5489 got_worktree_rebase_continue(struct got_object_id **commit_id,
5490 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5491 struct got_reference **branch, struct got_fileindex **fileindex,
5492 struct got_worktree *worktree, struct got_repository *repo)
5494 const struct got_error *err;
5495 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5496 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5497 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5498 char *fileindex_path = NULL;
5499 int have_staged_files = 0;
5501 *commit_id = NULL;
5502 *new_base_branch = NULL;
5503 *tmp_branch = NULL;
5504 *branch = NULL;
5505 *fileindex = NULL;
5507 err = lock_worktree(worktree, LOCK_EX);
5508 if (err)
5509 return err;
5511 err = open_fileindex(fileindex, &fileindex_path, worktree);
5512 if (err)
5513 goto done;
5515 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5516 &have_staged_files);
5517 if (err && err->code != GOT_ERR_CANCELLED)
5518 goto done;
5519 if (have_staged_files) {
5520 err = got_error(GOT_ERR_STAGED_PATHS);
5521 goto done;
5524 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5525 if (err)
5526 goto done;
5528 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5529 if (err)
5530 goto done;
5532 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5533 if (err)
5534 goto done;
5536 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5537 if (err)
5538 goto done;
5540 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5541 if (err)
5542 goto done;
5544 err = got_ref_open(branch, repo,
5545 got_ref_get_symref_target(branch_ref), 0);
5546 if (err)
5547 goto done;
5549 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5550 if (err)
5551 goto done;
5553 err = got_ref_resolve(commit_id, repo, commit_ref);
5554 if (err)
5555 goto done;
5557 err = got_ref_open(new_base_branch, repo,
5558 new_base_branch_ref_name, 0);
5559 if (err)
5560 goto done;
5562 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5563 if (err)
5564 goto done;
5565 done:
5566 free(commit_ref_name);
5567 free(branch_ref_name);
5568 free(fileindex_path);
5569 if (commit_ref)
5570 got_ref_close(commit_ref);
5571 if (branch_ref)
5572 got_ref_close(branch_ref);
5573 if (err) {
5574 free(*commit_id);
5575 *commit_id = NULL;
5576 if (*tmp_branch) {
5577 got_ref_close(*tmp_branch);
5578 *tmp_branch = NULL;
5580 if (*new_base_branch) {
5581 got_ref_close(*new_base_branch);
5582 *new_base_branch = NULL;
5584 if (*branch) {
5585 got_ref_close(*branch);
5586 *branch = NULL;
5588 if (*fileindex) {
5589 got_fileindex_free(*fileindex);
5590 *fileindex = NULL;
5592 lock_worktree(worktree, LOCK_SH);
5594 return err;
5597 const struct got_error *
5598 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5600 const struct got_error *err;
5601 char *tmp_branch_name = NULL;
5603 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5604 if (err)
5605 return err;
5607 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5608 free(tmp_branch_name);
5609 return NULL;
5612 static const struct got_error *
5613 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5614 char **logmsg, void *arg)
5616 *logmsg = arg;
5617 return NULL;
5620 static const struct got_error *
5621 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5622 const char *path, struct got_object_id *blob_id,
5623 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5624 int dirfd, const char *de_name)
5626 return NULL;
5629 struct collect_merged_paths_arg {
5630 got_worktree_checkout_cb progress_cb;
5631 void *progress_arg;
5632 struct got_pathlist_head *merged_paths;
5635 static const struct got_error *
5636 collect_merged_paths(void *arg, unsigned char status, const char *path)
5638 const struct got_error *err;
5639 struct collect_merged_paths_arg *a = arg;
5640 char *p;
5641 struct got_pathlist_entry *new;
5643 err = (*a->progress_cb)(a->progress_arg, status, path);
5644 if (err)
5645 return err;
5647 if (status != GOT_STATUS_MERGE &&
5648 status != GOT_STATUS_ADD &&
5649 status != GOT_STATUS_DELETE &&
5650 status != GOT_STATUS_CONFLICT)
5651 return NULL;
5653 p = strdup(path);
5654 if (p == NULL)
5655 return got_error_from_errno("strdup");
5657 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5658 if (err || new == NULL)
5659 free(p);
5660 return err;
5663 void
5664 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5666 struct got_pathlist_entry *pe;
5668 TAILQ_FOREACH(pe, merged_paths, entry)
5669 free((char *)pe->path);
5671 got_pathlist_free(merged_paths);
5674 static const struct got_error *
5675 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5676 int is_rebase, struct got_repository *repo)
5678 const struct got_error *err;
5679 struct got_reference *commit_ref = NULL;
5681 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5682 if (err) {
5683 if (err->code != GOT_ERR_NOT_REF)
5684 goto done;
5685 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5686 if (err)
5687 goto done;
5688 err = got_ref_write(commit_ref, repo);
5689 if (err)
5690 goto done;
5691 } else if (is_rebase) {
5692 struct got_object_id *stored_id;
5693 int cmp;
5695 err = got_ref_resolve(&stored_id, repo, commit_ref);
5696 if (err)
5697 goto done;
5698 cmp = got_object_id_cmp(commit_id, stored_id);
5699 free(stored_id);
5700 if (cmp != 0) {
5701 err = got_error(GOT_ERR_REBASE_COMMITID);
5702 goto done;
5705 done:
5706 if (commit_ref)
5707 got_ref_close(commit_ref);
5708 return err;
5711 static const struct got_error *
5712 rebase_merge_files(struct got_pathlist_head *merged_paths,
5713 const char *commit_ref_name, struct got_worktree *worktree,
5714 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5715 struct got_object_id *commit_id, struct got_repository *repo,
5716 got_worktree_checkout_cb progress_cb, void *progress_arg,
5717 got_cancel_cb cancel_cb, void *cancel_arg)
5719 const struct got_error *err;
5720 struct got_reference *commit_ref = NULL;
5721 struct collect_merged_paths_arg cmp_arg;
5722 char *fileindex_path;
5724 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5726 err = get_fileindex_path(&fileindex_path, worktree);
5727 if (err)
5728 return err;
5730 cmp_arg.progress_cb = progress_cb;
5731 cmp_arg.progress_arg = progress_arg;
5732 cmp_arg.merged_paths = merged_paths;
5733 err = merge_files(worktree, fileindex, fileindex_path,
5734 parent_commit_id, commit_id, repo, collect_merged_paths,
5735 &cmp_arg, cancel_cb, cancel_arg);
5736 if (commit_ref)
5737 got_ref_close(commit_ref);
5738 return err;
5741 const struct got_error *
5742 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5743 struct got_worktree *worktree, struct got_fileindex *fileindex,
5744 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5745 struct got_repository *repo,
5746 got_worktree_checkout_cb progress_cb, void *progress_arg,
5747 got_cancel_cb cancel_cb, void *cancel_arg)
5749 const struct got_error *err;
5750 char *commit_ref_name;
5752 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5753 if (err)
5754 return err;
5756 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5757 if (err)
5758 goto done;
5760 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5761 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5762 progress_arg, cancel_cb, cancel_arg);
5763 done:
5764 free(commit_ref_name);
5765 return err;
5768 const struct got_error *
5769 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5770 struct got_worktree *worktree, struct got_fileindex *fileindex,
5771 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5772 struct got_repository *repo,
5773 got_worktree_checkout_cb progress_cb, void *progress_arg,
5774 got_cancel_cb cancel_cb, void *cancel_arg)
5776 const struct got_error *err;
5777 char *commit_ref_name;
5779 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5780 if (err)
5781 return err;
5783 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5784 if (err)
5785 goto done;
5787 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5788 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5789 progress_arg, cancel_cb, cancel_arg);
5790 done:
5791 free(commit_ref_name);
5792 return err;
5795 static const struct got_error *
5796 rebase_commit(struct got_object_id **new_commit_id,
5797 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5798 struct got_worktree *worktree, struct got_fileindex *fileindex,
5799 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5800 const char *new_logmsg, struct got_repository *repo)
5802 const struct got_error *err, *sync_err;
5803 struct got_pathlist_head commitable_paths;
5804 struct collect_commitables_arg cc_arg;
5805 char *fileindex_path = NULL;
5806 struct got_reference *head_ref = NULL;
5807 struct got_object_id *head_commit_id = NULL;
5808 char *logmsg = NULL;
5810 TAILQ_INIT(&commitable_paths);
5811 *new_commit_id = NULL;
5813 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5815 err = get_fileindex_path(&fileindex_path, worktree);
5816 if (err)
5817 return err;
5819 cc_arg.commitable_paths = &commitable_paths;
5820 cc_arg.worktree = worktree;
5821 cc_arg.repo = repo;
5822 cc_arg.have_staged_files = 0;
5824 * If possible get the status of individual files directly to
5825 * avoid crawling the entire work tree once per rebased commit.
5826 * TODO: Ideally, merged_paths would contain a list of commitables
5827 * we could use so we could skip worktree_status() entirely.
5829 if (merged_paths) {
5830 struct got_pathlist_entry *pe;
5831 TAILQ_FOREACH(pe, merged_paths, entry) {
5832 err = worktree_status(worktree, pe->path, fileindex,
5833 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5834 0);
5835 if (err)
5836 goto done;
5838 } else {
5839 err = worktree_status(worktree, "", fileindex, repo,
5840 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5841 if (err)
5842 goto done;
5845 if (TAILQ_EMPTY(&commitable_paths)) {
5846 /* No-op change; commit will be elided. */
5847 err = got_ref_delete(commit_ref, repo);
5848 if (err)
5849 goto done;
5850 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5851 goto done;
5854 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5855 if (err)
5856 goto done;
5858 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5859 if (err)
5860 goto done;
5862 if (new_logmsg) {
5863 logmsg = strdup(new_logmsg);
5864 if (logmsg == NULL) {
5865 err = got_error_from_errno("strdup");
5866 goto done;
5868 } else {
5869 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5870 if (err)
5871 goto done;
5874 /* NB: commit_worktree will call free(logmsg) */
5875 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5876 worktree, got_object_commit_get_author(orig_commit),
5877 got_object_commit_get_committer(orig_commit),
5878 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5879 if (err)
5880 goto done;
5882 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5883 if (err)
5884 goto done;
5886 err = got_ref_delete(commit_ref, repo);
5887 if (err)
5888 goto done;
5890 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5891 fileindex, 0);
5892 sync_err = sync_fileindex(fileindex, fileindex_path);
5893 if (sync_err && err == NULL)
5894 err = sync_err;
5895 done:
5896 free(fileindex_path);
5897 free(head_commit_id);
5898 if (head_ref)
5899 got_ref_close(head_ref);
5900 if (err) {
5901 free(*new_commit_id);
5902 *new_commit_id = NULL;
5904 return err;
5907 const struct got_error *
5908 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5909 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5910 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5911 struct got_commit_object *orig_commit,
5912 struct got_object_id *orig_commit_id, struct got_repository *repo)
5914 const struct got_error *err;
5915 char *commit_ref_name;
5916 struct got_reference *commit_ref = NULL;
5917 struct got_object_id *commit_id = NULL;
5919 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5920 if (err)
5921 return err;
5923 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5924 if (err)
5925 goto done;
5926 err = got_ref_resolve(&commit_id, repo, commit_ref);
5927 if (err)
5928 goto done;
5929 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5930 err = got_error(GOT_ERR_REBASE_COMMITID);
5931 goto done;
5934 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5935 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5936 done:
5937 if (commit_ref)
5938 got_ref_close(commit_ref);
5939 free(commit_ref_name);
5940 free(commit_id);
5941 return err;
5944 const struct got_error *
5945 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5946 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5947 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5948 struct got_commit_object *orig_commit,
5949 struct got_object_id *orig_commit_id, const char *new_logmsg,
5950 struct got_repository *repo)
5952 const struct got_error *err;
5953 char *commit_ref_name;
5954 struct got_reference *commit_ref = NULL;
5956 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5957 if (err)
5958 return err;
5960 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5961 if (err)
5962 goto done;
5964 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5965 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5966 done:
5967 if (commit_ref)
5968 got_ref_close(commit_ref);
5969 free(commit_ref_name);
5970 return err;
5973 const struct got_error *
5974 got_worktree_rebase_postpone(struct got_worktree *worktree,
5975 struct got_fileindex *fileindex)
5977 if (fileindex)
5978 got_fileindex_free(fileindex);
5979 return lock_worktree(worktree, LOCK_SH);
5982 static const struct got_error *
5983 delete_ref(const char *name, struct got_repository *repo)
5985 const struct got_error *err;
5986 struct got_reference *ref;
5988 err = got_ref_open(&ref, repo, name, 0);
5989 if (err) {
5990 if (err->code == GOT_ERR_NOT_REF)
5991 return NULL;
5992 return err;
5995 err = got_ref_delete(ref, repo);
5996 got_ref_close(ref);
5997 return err;
6000 static const struct got_error *
6001 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6003 const struct got_error *err;
6004 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6005 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6007 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6008 if (err)
6009 goto done;
6010 err = delete_ref(tmp_branch_name, repo);
6011 if (err)
6012 goto done;
6014 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6015 if (err)
6016 goto done;
6017 err = delete_ref(new_base_branch_ref_name, repo);
6018 if (err)
6019 goto done;
6021 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6022 if (err)
6023 goto done;
6024 err = delete_ref(branch_ref_name, repo);
6025 if (err)
6026 goto done;
6028 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6029 if (err)
6030 goto done;
6031 err = delete_ref(commit_ref_name, repo);
6032 if (err)
6033 goto done;
6035 done:
6036 free(tmp_branch_name);
6037 free(new_base_branch_ref_name);
6038 free(branch_ref_name);
6039 free(commit_ref_name);
6040 return err;
6043 const struct got_error *
6044 got_worktree_rebase_complete(struct got_worktree *worktree,
6045 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6046 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6047 struct got_repository *repo)
6049 const struct got_error *err, *unlockerr;
6050 struct got_object_id *new_head_commit_id = NULL;
6052 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6053 if (err)
6054 return err;
6056 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6057 if (err)
6058 goto done;
6060 err = got_ref_write(rebased_branch, repo);
6061 if (err)
6062 goto done;
6064 err = got_worktree_set_head_ref(worktree, rebased_branch);
6065 if (err)
6066 goto done;
6068 err = delete_rebase_refs(worktree, repo);
6069 done:
6070 if (fileindex)
6071 got_fileindex_free(fileindex);
6072 free(new_head_commit_id);
6073 unlockerr = lock_worktree(worktree, LOCK_SH);
6074 if (unlockerr && err == NULL)
6075 err = unlockerr;
6076 return err;
6079 const struct got_error *
6080 got_worktree_rebase_abort(struct got_worktree *worktree,
6081 struct got_fileindex *fileindex, struct got_repository *repo,
6082 struct got_reference *new_base_branch,
6083 got_worktree_checkout_cb progress_cb, void *progress_arg)
6085 const struct got_error *err, *unlockerr, *sync_err;
6086 struct got_reference *resolved = NULL;
6087 struct got_object_id *commit_id = NULL;
6088 char *fileindex_path = NULL;
6089 struct revert_file_args rfa;
6090 struct got_object_id *tree_id = NULL;
6092 err = lock_worktree(worktree, LOCK_EX);
6093 if (err)
6094 return err;
6096 err = got_ref_open(&resolved, repo,
6097 got_ref_get_symref_target(new_base_branch), 0);
6098 if (err)
6099 goto done;
6101 err = got_worktree_set_head_ref(worktree, resolved);
6102 if (err)
6103 goto done;
6106 * XXX commits to the base branch could have happened while
6107 * we were busy rebasing; should we store the original commit ID
6108 * when rebase begins and read it back here?
6110 err = got_ref_resolve(&commit_id, repo, resolved);
6111 if (err)
6112 goto done;
6114 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6115 if (err)
6116 goto done;
6118 err = got_object_id_by_path(&tree_id, repo,
6119 worktree->base_commit_id, worktree->path_prefix);
6120 if (err)
6121 goto done;
6123 err = delete_rebase_refs(worktree, repo);
6124 if (err)
6125 goto done;
6127 err = get_fileindex_path(&fileindex_path, worktree);
6128 if (err)
6129 goto done;
6131 rfa.worktree = worktree;
6132 rfa.fileindex = fileindex;
6133 rfa.progress_cb = progress_cb;
6134 rfa.progress_arg = progress_arg;
6135 rfa.patch_cb = NULL;
6136 rfa.patch_arg = NULL;
6137 rfa.repo = repo;
6138 err = worktree_status(worktree, "", fileindex, repo,
6139 revert_file, &rfa, NULL, NULL, 0, 0);
6140 if (err)
6141 goto sync;
6143 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6144 repo, progress_cb, progress_arg, NULL, NULL);
6145 sync:
6146 sync_err = sync_fileindex(fileindex, fileindex_path);
6147 if (sync_err && err == NULL)
6148 err = sync_err;
6149 done:
6150 got_ref_close(resolved);
6151 free(tree_id);
6152 free(commit_id);
6153 if (fileindex)
6154 got_fileindex_free(fileindex);
6155 free(fileindex_path);
6157 unlockerr = lock_worktree(worktree, LOCK_SH);
6158 if (unlockerr && err == NULL)
6159 err = unlockerr;
6160 return err;
6163 const struct got_error *
6164 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6165 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6166 struct got_fileindex **fileindex, struct got_worktree *worktree,
6167 struct got_repository *repo)
6169 const struct got_error *err = NULL;
6170 char *tmp_branch_name = NULL;
6171 char *branch_ref_name = NULL;
6172 char *base_commit_ref_name = NULL;
6173 char *fileindex_path = NULL;
6174 struct check_rebase_ok_arg ok_arg;
6175 struct got_reference *wt_branch = NULL;
6176 struct got_reference *base_commit_ref = NULL;
6178 *tmp_branch = NULL;
6179 *branch_ref = NULL;
6180 *base_commit_id = NULL;
6181 *fileindex = NULL;
6183 err = lock_worktree(worktree, LOCK_EX);
6184 if (err)
6185 return err;
6187 err = open_fileindex(fileindex, &fileindex_path, worktree);
6188 if (err)
6189 goto done;
6191 ok_arg.worktree = worktree;
6192 ok_arg.repo = repo;
6193 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6194 &ok_arg);
6195 if (err)
6196 goto done;
6198 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6199 if (err)
6200 goto done;
6202 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6203 if (err)
6204 goto done;
6206 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6207 worktree);
6208 if (err)
6209 goto done;
6211 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6212 0);
6213 if (err)
6214 goto done;
6216 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6217 if (err)
6218 goto done;
6220 err = got_ref_write(*branch_ref, repo);
6221 if (err)
6222 goto done;
6224 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6225 worktree->base_commit_id);
6226 if (err)
6227 goto done;
6228 err = got_ref_write(base_commit_ref, repo);
6229 if (err)
6230 goto done;
6231 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6232 if (*base_commit_id == NULL) {
6233 err = got_error_from_errno("got_object_id_dup");
6234 goto done;
6237 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6238 worktree->base_commit_id);
6239 if (err)
6240 goto done;
6241 err = got_ref_write(*tmp_branch, repo);
6242 if (err)
6243 goto done;
6245 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6246 if (err)
6247 goto done;
6248 done:
6249 free(fileindex_path);
6250 free(tmp_branch_name);
6251 free(branch_ref_name);
6252 free(base_commit_ref_name);
6253 if (wt_branch)
6254 got_ref_close(wt_branch);
6255 if (err) {
6256 if (*branch_ref) {
6257 got_ref_close(*branch_ref);
6258 *branch_ref = NULL;
6260 if (*tmp_branch) {
6261 got_ref_close(*tmp_branch);
6262 *tmp_branch = NULL;
6264 free(*base_commit_id);
6265 if (*fileindex) {
6266 got_fileindex_free(*fileindex);
6267 *fileindex = NULL;
6269 lock_worktree(worktree, LOCK_SH);
6271 return err;
6274 const struct got_error *
6275 got_worktree_histedit_postpone(struct got_worktree *worktree,
6276 struct got_fileindex *fileindex)
6278 if (fileindex)
6279 got_fileindex_free(fileindex);
6280 return lock_worktree(worktree, LOCK_SH);
6283 const struct got_error *
6284 got_worktree_histedit_in_progress(int *in_progress,
6285 struct got_worktree *worktree)
6287 const struct got_error *err;
6288 char *tmp_branch_name = NULL;
6290 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6291 if (err)
6292 return err;
6294 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6295 free(tmp_branch_name);
6296 return NULL;
6299 const struct got_error *
6300 got_worktree_histedit_continue(struct got_object_id **commit_id,
6301 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6302 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6303 struct got_worktree *worktree, struct got_repository *repo)
6305 const struct got_error *err;
6306 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6307 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6308 struct got_reference *commit_ref = NULL;
6309 struct got_reference *base_commit_ref = NULL;
6310 char *fileindex_path = NULL;
6311 int have_staged_files = 0;
6313 *commit_id = NULL;
6314 *tmp_branch = NULL;
6315 *base_commit_id = NULL;
6316 *fileindex = NULL;
6318 err = lock_worktree(worktree, LOCK_EX);
6319 if (err)
6320 return err;
6322 err = open_fileindex(fileindex, &fileindex_path, worktree);
6323 if (err)
6324 goto done;
6326 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6327 &have_staged_files);
6328 if (err && err->code != GOT_ERR_CANCELLED)
6329 goto done;
6330 if (have_staged_files) {
6331 err = got_error(GOT_ERR_STAGED_PATHS);
6332 goto done;
6335 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6336 if (err)
6337 goto done;
6339 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6340 if (err)
6341 goto done;
6343 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6344 if (err)
6345 goto done;
6347 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6348 worktree);
6349 if (err)
6350 goto done;
6352 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6353 if (err)
6354 goto done;
6356 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6357 if (err)
6358 goto done;
6359 err = got_ref_resolve(commit_id, repo, commit_ref);
6360 if (err)
6361 goto done;
6363 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6364 if (err)
6365 goto done;
6366 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6367 if (err)
6368 goto done;
6370 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6371 if (err)
6372 goto done;
6373 done:
6374 free(commit_ref_name);
6375 free(branch_ref_name);
6376 free(fileindex_path);
6377 if (commit_ref)
6378 got_ref_close(commit_ref);
6379 if (base_commit_ref)
6380 got_ref_close(base_commit_ref);
6381 if (err) {
6382 free(*commit_id);
6383 *commit_id = NULL;
6384 free(*base_commit_id);
6385 *base_commit_id = NULL;
6386 if (*tmp_branch) {
6387 got_ref_close(*tmp_branch);
6388 *tmp_branch = NULL;
6390 if (*fileindex) {
6391 got_fileindex_free(*fileindex);
6392 *fileindex = NULL;
6394 lock_worktree(worktree, LOCK_EX);
6396 return err;
6399 static const struct got_error *
6400 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6402 const struct got_error *err;
6403 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6404 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6406 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6407 if (err)
6408 goto done;
6409 err = delete_ref(tmp_branch_name, repo);
6410 if (err)
6411 goto done;
6413 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6414 worktree);
6415 if (err)
6416 goto done;
6417 err = delete_ref(base_commit_ref_name, repo);
6418 if (err)
6419 goto done;
6421 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6422 if (err)
6423 goto done;
6424 err = delete_ref(branch_ref_name, repo);
6425 if (err)
6426 goto done;
6428 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6429 if (err)
6430 goto done;
6431 err = delete_ref(commit_ref_name, repo);
6432 if (err)
6433 goto done;
6434 done:
6435 free(tmp_branch_name);
6436 free(base_commit_ref_name);
6437 free(branch_ref_name);
6438 free(commit_ref_name);
6439 return err;
6442 const struct got_error *
6443 got_worktree_histedit_abort(struct got_worktree *worktree,
6444 struct got_fileindex *fileindex, struct got_repository *repo,
6445 struct got_reference *branch, struct got_object_id *base_commit_id,
6446 got_worktree_checkout_cb progress_cb, void *progress_arg)
6448 const struct got_error *err, *unlockerr, *sync_err;
6449 struct got_reference *resolved = NULL;
6450 char *fileindex_path = NULL;
6451 struct got_object_id *tree_id = NULL;
6452 struct revert_file_args rfa;
6454 err = lock_worktree(worktree, LOCK_EX);
6455 if (err)
6456 return err;
6458 err = got_ref_open(&resolved, repo,
6459 got_ref_get_symref_target(branch), 0);
6460 if (err)
6461 goto done;
6463 err = got_worktree_set_head_ref(worktree, resolved);
6464 if (err)
6465 goto done;
6467 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6468 if (err)
6469 goto done;
6471 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6472 worktree->path_prefix);
6473 if (err)
6474 goto done;
6476 err = delete_histedit_refs(worktree, repo);
6477 if (err)
6478 goto done;
6480 err = get_fileindex_path(&fileindex_path, worktree);
6481 if (err)
6482 goto done;
6484 rfa.worktree = worktree;
6485 rfa.fileindex = fileindex;
6486 rfa.progress_cb = progress_cb;
6487 rfa.progress_arg = progress_arg;
6488 rfa.patch_cb = NULL;
6489 rfa.patch_arg = NULL;
6490 rfa.repo = repo;
6491 err = worktree_status(worktree, "", fileindex, repo,
6492 revert_file, &rfa, NULL, NULL, 0, 0);
6493 if (err)
6494 goto sync;
6496 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6497 repo, progress_cb, progress_arg, NULL, NULL);
6498 sync:
6499 sync_err = sync_fileindex(fileindex, fileindex_path);
6500 if (sync_err && err == NULL)
6501 err = sync_err;
6502 done:
6503 got_ref_close(resolved);
6504 free(tree_id);
6505 free(fileindex_path);
6507 unlockerr = lock_worktree(worktree, LOCK_SH);
6508 if (unlockerr && err == NULL)
6509 err = unlockerr;
6510 return err;
6513 const struct got_error *
6514 got_worktree_histedit_complete(struct got_worktree *worktree,
6515 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6516 struct got_reference *edited_branch, struct got_repository *repo)
6518 const struct got_error *err, *unlockerr;
6519 struct got_object_id *new_head_commit_id = NULL;
6520 struct got_reference *resolved = NULL;
6522 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6523 if (err)
6524 return err;
6526 err = got_ref_open(&resolved, repo,
6527 got_ref_get_symref_target(edited_branch), 0);
6528 if (err)
6529 goto done;
6531 err = got_ref_change_ref(resolved, new_head_commit_id);
6532 if (err)
6533 goto done;
6535 err = got_ref_write(resolved, repo);
6536 if (err)
6537 goto done;
6539 err = got_worktree_set_head_ref(worktree, resolved);
6540 if (err)
6541 goto done;
6543 err = delete_histedit_refs(worktree, repo);
6544 done:
6545 if (fileindex)
6546 got_fileindex_free(fileindex);
6547 free(new_head_commit_id);
6548 unlockerr = lock_worktree(worktree, LOCK_SH);
6549 if (unlockerr && err == NULL)
6550 err = unlockerr;
6551 return err;
6554 const struct got_error *
6555 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6556 struct got_object_id *commit_id, struct got_repository *repo)
6558 const struct got_error *err;
6559 char *commit_ref_name;
6561 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6562 if (err)
6563 return err;
6565 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6566 if (err)
6567 goto done;
6569 err = delete_ref(commit_ref_name, repo);
6570 done:
6571 free(commit_ref_name);
6572 return err;
6575 const struct got_error *
6576 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6577 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6578 struct got_worktree *worktree, const char *refname,
6579 struct got_repository *repo)
6581 const struct got_error *err = NULL;
6582 char *fileindex_path = NULL;
6583 struct check_rebase_ok_arg ok_arg;
6585 *fileindex = NULL;
6586 *branch_ref = NULL;
6587 *base_branch_ref = NULL;
6589 err = lock_worktree(worktree, LOCK_EX);
6590 if (err)
6591 return err;
6593 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6594 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6595 "cannot integrate a branch into itself; "
6596 "update -b or different branch name required");
6597 goto done;
6600 err = open_fileindex(fileindex, &fileindex_path, worktree);
6601 if (err)
6602 goto done;
6604 /* Preconditions are the same as for rebase. */
6605 ok_arg.worktree = worktree;
6606 ok_arg.repo = repo;
6607 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6608 &ok_arg);
6609 if (err)
6610 goto done;
6612 err = got_ref_open(branch_ref, repo, refname, 1);
6613 if (err)
6614 goto done;
6616 err = got_ref_open(base_branch_ref, repo,
6617 got_worktree_get_head_ref_name(worktree), 1);
6618 done:
6619 if (err) {
6620 if (*branch_ref) {
6621 got_ref_close(*branch_ref);
6622 *branch_ref = NULL;
6624 if (*base_branch_ref) {
6625 got_ref_close(*base_branch_ref);
6626 *base_branch_ref = NULL;
6628 if (*fileindex) {
6629 got_fileindex_free(*fileindex);
6630 *fileindex = NULL;
6632 lock_worktree(worktree, LOCK_SH);
6634 return err;
6637 const struct got_error *
6638 got_worktree_integrate_continue(struct got_worktree *worktree,
6639 struct got_fileindex *fileindex, struct got_repository *repo,
6640 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6641 got_worktree_checkout_cb progress_cb, void *progress_arg,
6642 got_cancel_cb cancel_cb, void *cancel_arg)
6644 const struct got_error *err = NULL, *sync_err, *unlockerr;
6645 char *fileindex_path = NULL;
6646 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6648 err = get_fileindex_path(&fileindex_path, worktree);
6649 if (err)
6650 goto done;
6652 err = got_ref_resolve(&commit_id, repo, branch_ref);
6653 if (err)
6654 goto done;
6656 err = got_object_id_by_path(&tree_id, repo, commit_id,
6657 worktree->path_prefix);
6658 if (err)
6659 goto done;
6661 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6662 if (err)
6663 goto done;
6665 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6666 progress_cb, progress_arg, cancel_cb, cancel_arg);
6667 if (err)
6668 goto sync;
6670 err = got_ref_change_ref(base_branch_ref, commit_id);
6671 if (err)
6672 goto sync;
6674 err = got_ref_write(base_branch_ref, repo);
6675 sync:
6676 sync_err = sync_fileindex(fileindex, fileindex_path);
6677 if (sync_err && err == NULL)
6678 err = sync_err;
6680 done:
6681 unlockerr = got_ref_unlock(branch_ref);
6682 if (unlockerr && err == NULL)
6683 err = unlockerr;
6684 got_ref_close(branch_ref);
6686 unlockerr = got_ref_unlock(base_branch_ref);
6687 if (unlockerr && err == NULL)
6688 err = unlockerr;
6689 got_ref_close(base_branch_ref);
6691 got_fileindex_free(fileindex);
6692 free(fileindex_path);
6693 free(tree_id);
6695 unlockerr = lock_worktree(worktree, LOCK_SH);
6696 if (unlockerr && err == NULL)
6697 err = unlockerr;
6698 return err;
6701 const struct got_error *
6702 got_worktree_integrate_abort(struct got_worktree *worktree,
6703 struct got_fileindex *fileindex, struct got_repository *repo,
6704 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6706 const struct got_error *err = NULL, *unlockerr = NULL;
6708 got_fileindex_free(fileindex);
6710 err = lock_worktree(worktree, LOCK_SH);
6712 unlockerr = got_ref_unlock(branch_ref);
6713 if (unlockerr && err == NULL)
6714 err = unlockerr;
6715 got_ref_close(branch_ref);
6717 unlockerr = got_ref_unlock(base_branch_ref);
6718 if (unlockerr && err == NULL)
6719 err = unlockerr;
6720 got_ref_close(base_branch_ref);
6722 return err;
6725 struct check_stage_ok_arg {
6726 struct got_object_id *head_commit_id;
6727 struct got_worktree *worktree;
6728 struct got_fileindex *fileindex;
6729 struct got_repository *repo;
6730 int have_changes;
6733 const struct got_error *
6734 check_stage_ok(void *arg, unsigned char status,
6735 unsigned char staged_status, const char *relpath,
6736 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6737 struct got_object_id *commit_id, int dirfd, const char *de_name)
6739 struct check_stage_ok_arg *a = arg;
6740 const struct got_error *err = NULL;
6741 struct got_fileindex_entry *ie;
6742 struct got_object_id base_commit_id;
6743 struct got_object_id *base_commit_idp = NULL;
6744 char *in_repo_path = NULL, *p;
6746 if (status == GOT_STATUS_UNVERSIONED ||
6747 status == GOT_STATUS_NO_CHANGE)
6748 return NULL;
6749 if (status == GOT_STATUS_NONEXISTENT)
6750 return got_error_set_errno(ENOENT, relpath);
6752 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6753 if (ie == NULL)
6754 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6756 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6757 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6758 relpath) == -1)
6759 return got_error_from_errno("asprintf");
6761 if (got_fileindex_entry_has_commit(ie)) {
6762 memcpy(base_commit_id.sha1, ie->commit_sha1,
6763 SHA1_DIGEST_LENGTH);
6764 base_commit_idp = &base_commit_id;
6767 if (status == GOT_STATUS_CONFLICT) {
6768 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6769 goto done;
6770 } else if (status != GOT_STATUS_ADD &&
6771 status != GOT_STATUS_MODIFY &&
6772 status != GOT_STATUS_DELETE) {
6773 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6774 goto done;
6777 a->have_changes = 1;
6779 p = in_repo_path;
6780 while (p[0] == '/')
6781 p++;
6782 err = check_out_of_date(p, status, staged_status,
6783 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6784 GOT_ERR_STAGE_OUT_OF_DATE);
6785 done:
6786 free(in_repo_path);
6787 return err;
6790 struct stage_path_arg {
6791 struct got_worktree *worktree;
6792 struct got_fileindex *fileindex;
6793 struct got_repository *repo;
6794 got_worktree_status_cb status_cb;
6795 void *status_arg;
6796 got_worktree_patch_cb patch_cb;
6797 void *patch_arg;
6798 int staged_something;
6801 static const struct got_error *
6802 stage_path(void *arg, unsigned char status,
6803 unsigned char staged_status, const char *relpath,
6804 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6805 struct got_object_id *commit_id, int dirfd, const char *de_name)
6807 struct stage_path_arg *a = arg;
6808 const struct got_error *err = NULL;
6809 struct got_fileindex_entry *ie;
6810 char *ondisk_path = NULL, *path_content = NULL;
6811 uint32_t stage;
6812 struct got_object_id *new_staged_blob_id = NULL;
6814 if (status == GOT_STATUS_UNVERSIONED)
6815 return NULL;
6817 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6818 if (ie == NULL)
6819 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6821 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6822 relpath)== -1)
6823 return got_error_from_errno("asprintf");
6825 switch (status) {
6826 case GOT_STATUS_ADD:
6827 case GOT_STATUS_MODIFY:
6828 if (a->patch_cb) {
6829 if (status == GOT_STATUS_ADD) {
6830 int choice = GOT_PATCH_CHOICE_NONE;
6831 err = (*a->patch_cb)(&choice, a->patch_arg,
6832 status, ie->path, NULL, 1, 1);
6833 if (err)
6834 break;
6835 if (choice != GOT_PATCH_CHOICE_YES)
6836 break;
6837 } else {
6838 err = create_patched_content(&path_content, 0,
6839 staged_blob_id ? staged_blob_id : blob_id,
6840 ondisk_path, dirfd, de_name, ie->path,
6841 a->repo, a->patch_cb, a->patch_arg);
6842 if (err || path_content == NULL)
6843 break;
6846 err = got_object_blob_create(&new_staged_blob_id,
6847 path_content ? path_content : ondisk_path, a->repo);
6848 if (err)
6849 break;
6850 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6851 SHA1_DIGEST_LENGTH);
6852 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6853 stage = GOT_FILEIDX_STAGE_ADD;
6854 else
6855 stage = GOT_FILEIDX_STAGE_MODIFY;
6856 got_fileindex_entry_stage_set(ie, stage);
6857 a->staged_something = 1;
6858 if (a->status_cb == NULL)
6859 break;
6860 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6861 get_staged_status(ie), relpath, blob_id,
6862 new_staged_blob_id, NULL, dirfd, de_name);
6863 break;
6864 case GOT_STATUS_DELETE:
6865 if (staged_status == GOT_STATUS_DELETE)
6866 break;
6867 if (a->patch_cb) {
6868 int choice = GOT_PATCH_CHOICE_NONE;
6869 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6870 ie->path, NULL, 1, 1);
6871 if (err)
6872 break;
6873 if (choice == GOT_PATCH_CHOICE_NO)
6874 break;
6875 if (choice != GOT_PATCH_CHOICE_YES) {
6876 err = got_error(GOT_ERR_PATCH_CHOICE);
6877 break;
6880 stage = GOT_FILEIDX_STAGE_DELETE;
6881 got_fileindex_entry_stage_set(ie, stage);
6882 a->staged_something = 1;
6883 if (a->status_cb == NULL)
6884 break;
6885 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6886 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6887 de_name);
6888 break;
6889 case GOT_STATUS_NO_CHANGE:
6890 break;
6891 case GOT_STATUS_CONFLICT:
6892 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6893 break;
6894 case GOT_STATUS_NONEXISTENT:
6895 err = got_error_set_errno(ENOENT, relpath);
6896 break;
6897 default:
6898 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6899 break;
6902 if (path_content && unlink(path_content) == -1 && err == NULL)
6903 err = got_error_from_errno2("unlink", path_content);
6904 free(path_content);
6905 free(ondisk_path);
6906 free(new_staged_blob_id);
6907 return err;
6910 const struct got_error *
6911 got_worktree_stage(struct got_worktree *worktree,
6912 struct got_pathlist_head *paths,
6913 got_worktree_status_cb status_cb, void *status_arg,
6914 got_worktree_patch_cb patch_cb, void *patch_arg,
6915 struct got_repository *repo)
6917 const struct got_error *err = NULL, *sync_err, *unlockerr;
6918 struct got_pathlist_entry *pe;
6919 struct got_fileindex *fileindex = NULL;
6920 char *fileindex_path = NULL;
6921 struct got_reference *head_ref = NULL;
6922 struct got_object_id *head_commit_id = NULL;
6923 struct check_stage_ok_arg oka;
6924 struct stage_path_arg spa;
6926 err = lock_worktree(worktree, LOCK_EX);
6927 if (err)
6928 return err;
6930 err = got_ref_open(&head_ref, repo,
6931 got_worktree_get_head_ref_name(worktree), 0);
6932 if (err)
6933 goto done;
6934 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6935 if (err)
6936 goto done;
6937 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6938 if (err)
6939 goto done;
6941 /* Check pre-conditions before staging anything. */
6942 oka.head_commit_id = head_commit_id;
6943 oka.worktree = worktree;
6944 oka.fileindex = fileindex;
6945 oka.repo = repo;
6946 oka.have_changes = 0;
6947 TAILQ_FOREACH(pe, paths, entry) {
6948 err = worktree_status(worktree, pe->path, fileindex, repo,
6949 check_stage_ok, &oka, NULL, NULL, 0, 0);
6950 if (err)
6951 goto done;
6953 if (!oka.have_changes) {
6954 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6955 goto done;
6958 spa.worktree = worktree;
6959 spa.fileindex = fileindex;
6960 spa.repo = repo;
6961 spa.patch_cb = patch_cb;
6962 spa.patch_arg = patch_arg;
6963 spa.status_cb = status_cb;
6964 spa.status_arg = status_arg;
6965 spa.staged_something = 0;
6966 TAILQ_FOREACH(pe, paths, entry) {
6967 err = worktree_status(worktree, pe->path, fileindex, repo,
6968 stage_path, &spa, NULL, NULL, 0, 0);
6969 if (err)
6970 goto done;
6972 if (!spa.staged_something) {
6973 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6974 goto done;
6977 sync_err = sync_fileindex(fileindex, fileindex_path);
6978 if (sync_err && err == NULL)
6979 err = sync_err;
6980 done:
6981 if (head_ref)
6982 got_ref_close(head_ref);
6983 free(head_commit_id);
6984 free(fileindex_path);
6985 if (fileindex)
6986 got_fileindex_free(fileindex);
6987 unlockerr = lock_worktree(worktree, LOCK_SH);
6988 if (unlockerr && err == NULL)
6989 err = unlockerr;
6990 return err;
6993 struct unstage_path_arg {
6994 struct got_worktree *worktree;
6995 struct got_fileindex *fileindex;
6996 struct got_repository *repo;
6997 got_worktree_checkout_cb progress_cb;
6998 void *progress_arg;
6999 got_worktree_patch_cb patch_cb;
7000 void *patch_arg;
7003 static const struct got_error *
7004 create_unstaged_content(char **path_unstaged_content,
7005 char **path_new_staged_content, struct got_object_id *blob_id,
7006 struct got_object_id *staged_blob_id, const char *relpath,
7007 struct got_repository *repo,
7008 got_worktree_patch_cb patch_cb, void *patch_arg)
7010 const struct got_error *err;
7011 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7012 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7013 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7014 struct stat sb1, sb2;
7015 struct got_diff_changes *changes = NULL;
7016 struct got_diff_state *ds = NULL;
7017 struct got_diff_args *args = NULL;
7018 struct got_diff_change *change;
7019 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7020 int have_content = 0, have_rejected_content = 0;
7022 *path_unstaged_content = NULL;
7023 *path_new_staged_content = NULL;
7025 err = got_object_id_str(&label1, blob_id);
7026 if (err)
7027 return err;
7028 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7029 if (err)
7030 goto done;
7032 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7033 if (err)
7034 goto done;
7036 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7037 if (err)
7038 goto done;
7040 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7041 if (err)
7042 goto done;
7044 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7045 if (err)
7046 goto done;
7048 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7049 if (err)
7050 goto done;
7052 if (stat(path1, &sb1) == -1) {
7053 err = got_error_from_errno2("stat", path1);
7054 goto done;
7057 if (stat(path2, &sb2) == -1) {
7058 err = got_error_from_errno2("stat", path2);
7059 goto done;
7062 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7063 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7064 if (err)
7065 goto done;
7067 err = got_opentemp_named(path_unstaged_content, &outfile,
7068 "got-unstaged-content");
7069 if (err)
7070 goto done;
7071 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7072 "got-new-staged-content");
7073 if (err)
7074 goto done;
7076 if (fseek(f1, 0L, SEEK_SET) == -1) {
7077 err = got_ferror(f1, GOT_ERR_IO);
7078 goto done;
7080 if (fseek(f2, 0L, SEEK_SET) == -1) {
7081 err = got_ferror(f2, GOT_ERR_IO);
7082 goto done;
7084 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7085 int choice;
7086 err = apply_or_reject_change(&choice, change, ++n,
7087 changes->nchanges, ds, args, diff_flags, relpath,
7088 f1, f2, &line_cur1, &line_cur2,
7089 outfile, rejectfile, patch_cb, patch_arg);
7090 if (err)
7091 goto done;
7092 if (choice == GOT_PATCH_CHOICE_YES)
7093 have_content = 1;
7094 else
7095 have_rejected_content = 1;
7096 if (choice == GOT_PATCH_CHOICE_QUIT)
7097 break;
7099 if (have_content || have_rejected_content)
7100 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7101 outfile, rejectfile);
7102 done:
7103 free(label1);
7104 if (blob)
7105 got_object_blob_close(blob);
7106 if (staged_blob)
7107 got_object_blob_close(staged_blob);
7108 if (f1 && fclose(f1) == EOF && err == NULL)
7109 err = got_error_from_errno2("fclose", path1);
7110 if (f2 && fclose(f2) == EOF && err == NULL)
7111 err = got_error_from_errno2("fclose", path2);
7112 if (outfile && fclose(outfile) == EOF && err == NULL)
7113 err = got_error_from_errno2("fclose", *path_unstaged_content);
7114 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7115 err = got_error_from_errno2("fclose", *path_new_staged_content);
7116 if (path1 && unlink(path1) == -1 && err == NULL)
7117 err = got_error_from_errno2("unlink", path1);
7118 if (path2 && unlink(path2) == -1 && err == NULL)
7119 err = got_error_from_errno2("unlink", path2);
7120 if (err || !have_content) {
7121 if (*path_unstaged_content &&
7122 unlink(*path_unstaged_content) == -1 && err == NULL)
7123 err = got_error_from_errno2("unlink",
7124 *path_unstaged_content);
7125 free(*path_unstaged_content);
7126 *path_unstaged_content = NULL;
7128 if (err || !have_rejected_content) {
7129 if (*path_new_staged_content &&
7130 unlink(*path_new_staged_content) == -1 && err == NULL)
7131 err = got_error_from_errno2("unlink",
7132 *path_new_staged_content);
7133 free(*path_new_staged_content);
7134 *path_new_staged_content = NULL;
7136 free(args);
7137 if (ds) {
7138 got_diff_state_free(ds);
7139 free(ds);
7141 if (changes)
7142 got_diff_free_changes(changes);
7143 free(path1);
7144 free(path2);
7145 return err;
7148 static const struct got_error *
7149 unstage_path(void *arg, unsigned char status,
7150 unsigned char staged_status, const char *relpath,
7151 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7152 struct got_object_id *commit_id, int dirfd, const char *de_name)
7154 const struct got_error *err = NULL;
7155 struct unstage_path_arg *a = arg;
7156 struct got_fileindex_entry *ie;
7157 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7158 char *ondisk_path = NULL, *path_unstaged_content = NULL;
7159 char *path_new_staged_content = NULL;
7160 char *id_str = NULL, *label_orig = NULL;
7161 int local_changes_subsumed;
7162 struct stat sb;
7164 if (staged_status != GOT_STATUS_ADD &&
7165 staged_status != GOT_STATUS_MODIFY &&
7166 staged_status != GOT_STATUS_DELETE)
7167 return NULL;
7169 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7170 if (ie == NULL)
7171 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7173 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7174 == -1)
7175 return got_error_from_errno("asprintf");
7177 err = got_object_id_str(&id_str,
7178 commit_id ? commit_id : a->worktree->base_commit_id);
7179 if (err)
7180 goto done;
7181 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7182 id_str) == -1) {
7183 err = got_error_from_errno("asprintf");
7184 goto done;
7187 switch (staged_status) {
7188 case GOT_STATUS_MODIFY:
7189 err = got_object_open_as_blob(&blob_base, a->repo,
7190 blob_id, 8192);
7191 if (err)
7192 break;
7193 /* fall through */
7194 case GOT_STATUS_ADD:
7195 if (a->patch_cb) {
7196 if (staged_status == GOT_STATUS_ADD) {
7197 int choice = GOT_PATCH_CHOICE_NONE;
7198 err = (*a->patch_cb)(&choice, a->patch_arg,
7199 staged_status, ie->path, NULL, 1, 1);
7200 if (err)
7201 break;
7202 if (choice != GOT_PATCH_CHOICE_YES)
7203 break;
7204 } else {
7205 err = create_unstaged_content(
7206 &path_unstaged_content,
7207 &path_new_staged_content, blob_id,
7208 staged_blob_id, ie->path, a->repo,
7209 a->patch_cb, a->patch_arg);
7210 if (err || path_unstaged_content == NULL)
7211 break;
7212 if (path_new_staged_content) {
7213 err = got_object_blob_create(
7214 &staged_blob_id,
7215 path_new_staged_content,
7216 a->repo);
7217 if (err)
7218 break;
7219 memcpy(ie->staged_blob_sha1,
7220 staged_blob_id->sha1,
7221 SHA1_DIGEST_LENGTH);
7223 err = merge_file(&local_changes_subsumed,
7224 a->worktree, blob_base, ondisk_path,
7225 relpath, got_fileindex_perms_to_st(ie),
7226 path_unstaged_content, label_orig,
7227 "unstaged", a->repo, a->progress_cb,
7228 a->progress_arg);
7229 if (err == NULL &&
7230 path_new_staged_content == NULL)
7231 got_fileindex_entry_stage_set(ie,
7232 GOT_FILEIDX_STAGE_NONE);
7233 break; /* Done with this file. */
7236 err = got_object_open_as_blob(&blob_staged, a->repo,
7237 staged_blob_id, 8192);
7238 if (err)
7239 break;
7240 err = merge_blob(&local_changes_subsumed, a->worktree,
7241 blob_base, ondisk_path, relpath,
7242 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7243 commit_id ? commit_id : a->worktree->base_commit_id,
7244 a->repo, a->progress_cb, a->progress_arg);
7245 if (err == NULL)
7246 got_fileindex_entry_stage_set(ie,
7247 GOT_FILEIDX_STAGE_NONE);
7248 break;
7249 case GOT_STATUS_DELETE:
7250 if (a->patch_cb) {
7251 int choice = GOT_PATCH_CHOICE_NONE;
7252 err = (*a->patch_cb)(&choice, a->patch_arg,
7253 staged_status, ie->path, NULL, 1, 1);
7254 if (err)
7255 break;
7256 if (choice == GOT_PATCH_CHOICE_NO)
7257 break;
7258 if (choice != GOT_PATCH_CHOICE_YES) {
7259 err = got_error(GOT_ERR_PATCH_CHOICE);
7260 break;
7263 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7264 err = get_file_status(&status, &sb, ie, ondisk_path,
7265 dirfd, de_name, a->repo);
7266 if (err)
7267 break;
7268 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7269 break;
7271 done:
7272 free(ondisk_path);
7273 if (path_unstaged_content &&
7274 unlink(path_unstaged_content) == -1 && err == NULL)
7275 err = got_error_from_errno2("unlink", path_unstaged_content);
7276 if (path_new_staged_content &&
7277 unlink(path_new_staged_content) == -1 && err == NULL)
7278 err = got_error_from_errno2("unlink", path_new_staged_content);
7279 free(path_unstaged_content);
7280 free(path_new_staged_content);
7281 if (blob_base)
7282 got_object_blob_close(blob_base);
7283 if (blob_staged)
7284 got_object_blob_close(blob_staged);
7285 free(id_str);
7286 free(label_orig);
7287 return err;
7290 const struct got_error *
7291 got_worktree_unstage(struct got_worktree *worktree,
7292 struct got_pathlist_head *paths,
7293 got_worktree_checkout_cb progress_cb, void *progress_arg,
7294 got_worktree_patch_cb patch_cb, void *patch_arg,
7295 struct got_repository *repo)
7297 const struct got_error *err = NULL, *sync_err, *unlockerr;
7298 struct got_pathlist_entry *pe;
7299 struct got_fileindex *fileindex = NULL;
7300 char *fileindex_path = NULL;
7301 struct unstage_path_arg upa;
7303 err = lock_worktree(worktree, LOCK_EX);
7304 if (err)
7305 return err;
7307 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7308 if (err)
7309 goto done;
7311 upa.worktree = worktree;
7312 upa.fileindex = fileindex;
7313 upa.repo = repo;
7314 upa.progress_cb = progress_cb;
7315 upa.progress_arg = progress_arg;
7316 upa.patch_cb = patch_cb;
7317 upa.patch_arg = patch_arg;
7318 TAILQ_FOREACH(pe, paths, entry) {
7319 err = worktree_status(worktree, pe->path, fileindex, repo,
7320 unstage_path, &upa, NULL, NULL, 0, 0);
7321 if (err)
7322 goto done;
7325 sync_err = sync_fileindex(fileindex, fileindex_path);
7326 if (sync_err && err == NULL)
7327 err = sync_err;
7328 done:
7329 free(fileindex_path);
7330 if (fileindex)
7331 got_fileindex_free(fileindex);
7332 unlockerr = lock_worktree(worktree, LOCK_SH);
7333 if (unlockerr && err == NULL)
7334 err = unlockerr;
7335 return err;