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 /* forward declaration */
921 static const struct got_error *
922 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
923 const char *, const char *, uint16_t, const char *,
924 struct got_blob_object *, struct got_object_id *,
925 struct got_repository *, got_worktree_checkout_cb, void *);
927 /*
928 * Merge a symlink into the work tree, where blob_orig acts as the common
929 * ancestor, blob_deriv acts as the first derived version, and the symlink
930 * on disk acts as the second derived version.
931 * Assume that contents of both blobs represent symlinks.
932 */
933 static const struct got_error *
934 merge_symlink(struct got_worktree *worktree,
935 struct got_blob_object *blob_orig, const char *ondisk_path,
936 const char *path, uint16_t st_mode, const char *label_orig,
937 struct got_blob_object *blob_deriv,
938 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
939 got_worktree_checkout_cb progress_cb, void *progress_arg)
941 const struct got_error *err = NULL;
942 char *ancestor_target = NULL, *deriv_target = NULL;
943 struct stat sb;
944 ssize_t ondisk_len, deriv_len;
945 char ondisk_target[PATH_MAX];
946 int have_local_change = 0;
947 int have_incoming_change = 0;
949 if (lstat(ondisk_path, &sb) == -1)
950 return got_error_from_errno2("lstat", ondisk_path);
952 if (!S_ISLNK(sb.st_mode)) {
953 /*
954 * If there is a regular file on disk, merge the symlink
955 * target path into this file, which will usually cause
956 * a merge conflict.
957 */
958 if (S_ISREG(sb.st_mode)) {
959 int local_changes_subsumed;
960 return merge_blob(&local_changes_subsumed, worktree,
961 NULL, ondisk_path, path, sb.st_mode, label_orig,
962 blob_deriv, deriv_base_commit_id,
963 repo, progress_cb, progress_arg);
966 /* TODO symlink is obstructed; do something */
967 return got_error_path(ondisk_path, GOT_ERR_FILE_OBSTRUCTED);
970 ondisk_len = readlink(ondisk_path, ondisk_target,
971 sizeof(ondisk_target));
972 if (ondisk_len == -1) {
973 err = got_error_from_errno2("readlink",
974 ondisk_path);
975 goto done;
977 ondisk_target[ondisk_len] = '\0';
979 if (blob_orig) {
980 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
981 if (err)
982 goto done;
985 err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
986 if (err)
987 goto done;
989 if (ancestor_target == NULL ||
990 (ondisk_len != strlen(ancestor_target) ||
991 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
992 have_local_change = 1;
994 deriv_len = strlen(deriv_target);
995 if (ancestor_target == NULL ||
996 (deriv_len != strlen(ancestor_target) ||
997 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
998 have_incoming_change = 1;
1000 if (!have_local_change && !have_incoming_change) {
1001 if (ancestor_target) {
1002 /* Both sides made the same change. */
1003 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1004 path);
1005 } else if (deriv_len == ondisk_len &&
1006 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1007 /* Both sides added the same symlink. */
1008 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1009 path);
1010 } else {
1011 /* Both sides added symlinks which don't match. */
1012 err = install_symlink_conflict(deriv_target,
1013 deriv_base_commit_id, ancestor_target,
1014 label_orig, ondisk_target, ondisk_path);
1015 if (err)
1016 goto done;
1017 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1018 path);
1020 } else if (!have_local_change && have_incoming_change) {
1021 /* Apply the incoming change. */
1022 err = update_symlink(ondisk_path, deriv_target,
1023 strlen(deriv_target));
1024 if (err)
1025 goto done;
1026 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1027 } else if (have_local_change && have_incoming_change) {
1028 err = install_symlink_conflict(deriv_target,
1029 deriv_base_commit_id, ancestor_target, label_orig,
1030 ondisk_target, ondisk_path);
1031 if (err)
1032 goto done;
1033 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1034 path);
1037 done:
1038 free(ancestor_target);
1039 free(deriv_target);
1040 return err;
1044 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1045 * blob_deriv acts as the first derived version, and the file on disk
1046 * acts as the second derived version.
1048 static const struct got_error *
1049 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1050 struct got_blob_object *blob_orig, const char *ondisk_path,
1051 const char *path, uint16_t st_mode, const char *label_orig,
1052 struct got_blob_object *blob_deriv,
1053 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1054 got_worktree_checkout_cb progress_cb, void *progress_arg)
1056 const struct got_error *err = NULL;
1057 FILE *f_deriv = NULL;
1058 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1059 char *label_deriv = NULL, *parent;
1061 *local_changes_subsumed = 0;
1063 parent = dirname(ondisk_path);
1064 if (parent == NULL)
1065 return got_error_from_errno2("dirname", ondisk_path);
1067 free(base_path);
1068 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1069 err = got_error_from_errno("asprintf");
1070 base_path = NULL;
1071 goto done;
1074 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1075 if (err)
1076 goto done;
1077 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1078 blob_deriv);
1079 if (err)
1080 goto done;
1082 err = got_object_id_str(&id_str, deriv_base_commit_id);
1083 if (err)
1084 goto done;
1085 if (asprintf(&label_deriv, "%s: commit %s",
1086 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1087 err = got_error_from_errno("asprintf");
1088 goto done;
1091 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1092 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1093 label_deriv, repo, progress_cb, progress_arg);
1094 done:
1095 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1096 err = got_error_from_errno("fclose");
1097 free(base_path);
1098 if (blob_deriv_path) {
1099 unlink(blob_deriv_path);
1100 free(blob_deriv_path);
1102 free(id_str);
1103 free(label_deriv);
1104 return err;
1107 static const struct got_error *
1108 create_fileindex_entry(struct got_fileindex *fileindex,
1109 struct got_object_id *base_commit_id, const char *ondisk_path,
1110 const char *path, struct got_object_id *blob_id)
1112 const struct got_error *err = NULL;
1113 struct got_fileindex_entry *new_ie;
1115 err = got_fileindex_entry_alloc(&new_ie, path);
1116 if (err)
1117 return err;
1119 err = got_fileindex_entry_update(new_ie, ondisk_path,
1120 blob_id->sha1, base_commit_id->sha1, 1);
1121 if (err)
1122 goto done;
1124 err = got_fileindex_entry_add(fileindex, new_ie);
1125 done:
1126 if (err)
1127 got_fileindex_entry_free(new_ie);
1128 return err;
1131 static mode_t
1132 get_ondisk_perms(int executable, mode_t st_mode)
1134 mode_t xbits = S_IXUSR;
1136 if (executable) {
1137 /* Map read bits to execute bits. */
1138 if (st_mode & S_IRGRP)
1139 xbits |= S_IXGRP;
1140 if (st_mode & S_IROTH)
1141 xbits |= S_IXOTH;
1142 return st_mode | xbits;
1145 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1148 /* forward declaration */
1149 static const struct got_error *
1150 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1151 const char *path, mode_t te_mode, mode_t st_mode,
1152 struct got_blob_object *blob, int restoring_missing_file,
1153 int reverting_versioned_file, int installing_bad_symlink,
1154 struct got_repository *repo,
1155 got_worktree_checkout_cb progress_cb, void *progress_arg);
1158 * This function assumes that the provided symlink target points at a
1159 * safe location in the work tree!
1161 static const struct got_error *
1162 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1163 size_t target_len)
1165 const struct got_error *err = NULL;
1166 ssize_t elen;
1167 char etarget[PATH_MAX];
1168 int fd;
1171 * "Bad" symlinks (those pointing outside the work tree or into the
1172 * .got directory) are installed in the work tree as a regular file
1173 * which contains the bad symlink target path.
1174 * The new symlink target has already been checked for safety by our
1175 * caller. If we can successfully open a regular file then we simply
1176 * replace this file with a symlink below.
1178 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1179 if (fd == -1) {
1180 if (errno != ELOOP)
1181 return got_error_from_errno2("open", ondisk_path);
1183 /* We are updating an existing on-disk symlink. */
1184 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1185 if (elen == -1)
1186 return got_error_from_errno2("readlink", ondisk_path);
1188 if (elen == target_len &&
1189 memcmp(etarget, target_path, target_len) == 0)
1190 return NULL; /* nothing to do */
1193 err = update_symlink(ondisk_path, target_path, target_len);
1194 if (fd != -1 && close(fd) == -1 && err == NULL)
1195 err = got_error_from_errno2("close", ondisk_path);
1196 return err;
1199 static const struct got_error *
1200 install_symlink(struct got_worktree *worktree, const char *ondisk_path,
1201 const char *path, mode_t te_mode, mode_t st_mode,
1202 struct got_blob_object *blob, int restoring_missing_file,
1203 int reverting_versioned_file, struct got_repository *repo,
1204 got_worktree_checkout_cb progress_cb, void *progress_arg)
1206 const struct got_error *err = NULL;
1207 char target_path[PATH_MAX];
1208 size_t len, target_len = 0;
1209 char *resolved_path = NULL, *abspath = NULL;
1210 char *path_got = NULL;
1211 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1212 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1215 * Blob object content specifies the target path of the link.
1216 * If a symbolic link cannot be installed we instead create
1217 * a regular file which contains the link target path stored
1218 * in the blob object.
1220 do {
1221 err = got_object_blob_read_block(&len, blob);
1222 if (len + target_len >= sizeof(target_path)) {
1223 /* Path too long; install as a regular file. */
1224 got_object_blob_rewind(blob);
1225 return install_blob(worktree, ondisk_path, path,
1226 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1227 restoring_missing_file, reverting_versioned_file,
1228 1, repo, progress_cb, progress_arg);
1230 if (len > 0) {
1231 /* Skip blob object header first time around. */
1232 memcpy(target_path + target_len, buf + hdrlen,
1233 len - hdrlen);
1234 target_len += len - hdrlen;
1235 hdrlen = 0;
1237 } while (len != 0);
1238 target_path[target_len] = '\0';
1241 * Relative symlink target lookup should begin at the directory
1242 * in which the blob object is being installed.
1244 if (!got_path_is_absolute(target_path)) {
1245 char *parent = dirname(ondisk_path);
1246 if (parent == NULL) {
1247 err = got_error_from_errno2("dirname", ondisk_path);
1248 goto done;
1250 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1251 err = got_error_from_errno("asprintf");
1252 goto done;
1257 * unveil(2) restricts our view of paths in the filesystem.
1258 * ENOENT will occur if a link target path does not exist or
1259 * if it points outside our unveiled path space.
1261 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1262 if (resolved_path == NULL) {
1263 if (errno != ENOENT) {
1264 err = got_error_from_errno2("realpath", target_path);
1265 goto done;
1269 /* Only allow symlinks pointing at paths within the work tree. */
1270 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1271 abspath : target_path), worktree->root_path,
1272 strlen(worktree->root_path))) {
1273 /* install as a regular file */
1274 got_object_blob_rewind(blob);
1275 err = install_blob(worktree, ondisk_path, path,
1276 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1277 restoring_missing_file, reverting_versioned_file, 1,
1278 repo, progress_cb, progress_arg);
1279 goto done;
1282 /* Do not allow symlinks pointing into the .got directory. */
1283 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1284 GOT_WORKTREE_GOT_DIR) == -1) {
1285 err = got_error_from_errno("asprintf");
1286 goto done;
1288 if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1289 abspath : target_path), path_got, strlen(path_got))) {
1290 /* install as a regular file */
1291 got_object_blob_rewind(blob);
1292 err = install_blob(worktree, ondisk_path, path,
1293 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1294 restoring_missing_file, reverting_versioned_file, 1,
1295 repo, progress_cb, progress_arg);
1296 goto done;
1299 if (symlink(target_path, ondisk_path) == -1) {
1300 if (errno == EEXIST) {
1301 err = replace_existing_symlink(ondisk_path,
1302 target_path, target_len);
1303 if (err)
1304 goto done;
1305 if (progress_cb) {
1306 err = (*progress_cb)(progress_arg,
1307 GOT_STATUS_UPDATE, path);
1309 goto done; /* Nothing else to do. */
1312 if (errno == ENOENT) {
1313 char *parent = dirname(ondisk_path);
1314 if (parent == NULL) {
1315 err = got_error_from_errno2("dirname",
1316 ondisk_path);
1317 goto done;
1319 err = add_dir_on_disk(worktree, parent);
1320 if (err)
1321 goto done;
1323 * Retry, and fall through to error handling
1324 * below if this second attempt fails.
1326 if (symlink(target_path, ondisk_path) != -1) {
1327 err = NULL; /* success */
1328 goto done;
1332 /* Handle errors from first or second creation attempt. */
1333 if (errno == ENAMETOOLONG) {
1334 /* bad target path; install as a regular file */
1335 got_object_blob_rewind(blob);
1336 err = install_blob(worktree, ondisk_path, path,
1337 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1338 restoring_missing_file, reverting_versioned_file, 1,
1339 repo, progress_cb, progress_arg);
1340 } else if (errno == ENOTDIR) {
1341 err = got_error_path(ondisk_path,
1342 GOT_ERR_FILE_OBSTRUCTED);
1343 } else {
1344 err = got_error_from_errno3("symlink",
1345 target_path, ondisk_path);
1347 } else if (progress_cb)
1348 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1349 done:
1350 free(resolved_path);
1351 free(abspath);
1352 free(path_got);
1353 return err;
1356 static const struct got_error *
1357 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1358 const char *path, mode_t te_mode, mode_t st_mode,
1359 struct got_blob_object *blob, int restoring_missing_file,
1360 int reverting_versioned_file, int installing_bad_symlink,
1361 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1362 void *progress_arg)
1364 const struct got_error *err = NULL;
1365 int fd = -1;
1366 size_t len, hdrlen;
1367 int update = 0;
1368 char *tmppath = NULL;
1370 if (S_ISLNK(te_mode))
1371 return install_symlink(worktree, ondisk_path, path, te_mode,
1372 st_mode, blob, restoring_missing_file,
1373 reverting_versioned_file, repo, progress_cb, progress_arg);
1375 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1376 GOT_DEFAULT_FILE_MODE);
1377 if (fd == -1) {
1378 if (errno == ENOENT) {
1379 char *parent = dirname(path);
1380 if (parent == NULL)
1381 return got_error_from_errno2("dirname", path);
1382 err = add_dir_on_disk(worktree, parent);
1383 if (err)
1384 return err;
1385 fd = open(ondisk_path,
1386 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1387 GOT_DEFAULT_FILE_MODE);
1388 if (fd == -1)
1389 return got_error_from_errno2("open",
1390 ondisk_path);
1391 } else if (errno == EEXIST) {
1392 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1393 /* TODO file is obstructed; do something */
1394 err = got_error_path(ondisk_path,
1395 GOT_ERR_FILE_OBSTRUCTED);
1396 goto done;
1397 } else {
1398 err = got_opentemp_named_fd(&tmppath, &fd,
1399 ondisk_path);
1400 if (err)
1401 goto done;
1402 update = 1;
1404 } else
1405 return got_error_from_errno2("open", ondisk_path);
1408 if (progress_cb) {
1409 if (restoring_missing_file)
1410 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1411 path);
1412 else if (reverting_versioned_file)
1413 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1414 path);
1415 else
1416 err = (*progress_cb)(progress_arg,
1417 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1418 if (err)
1419 goto done;
1422 hdrlen = got_object_blob_get_hdrlen(blob);
1423 do {
1424 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1425 err = got_object_blob_read_block(&len, blob);
1426 if (err)
1427 break;
1428 if (len > 0) {
1429 /* Skip blob object header first time around. */
1430 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1431 if (outlen == -1) {
1432 err = got_error_from_errno("write");
1433 goto done;
1434 } else if (outlen != len - hdrlen) {
1435 err = got_error(GOT_ERR_IO);
1436 goto done;
1438 hdrlen = 0;
1440 } while (len != 0);
1442 if (fsync(fd) != 0) {
1443 err = got_error_from_errno("fsync");
1444 goto done;
1447 if (update) {
1448 if (rename(tmppath, ondisk_path) != 0) {
1449 err = got_error_from_errno3("rename", tmppath,
1450 ondisk_path);
1451 unlink(tmppath);
1452 goto done;
1456 if (chmod(ondisk_path,
1457 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1458 err = got_error_from_errno2("chmod", ondisk_path);
1459 goto done;
1462 done:
1463 if (fd != -1 && close(fd) != 0 && err == NULL)
1464 err = got_error_from_errno("close");
1465 free(tmppath);
1466 return err;
1469 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1470 static const struct got_error *
1471 get_modified_file_content_status(unsigned char *status, FILE *f)
1473 const struct got_error *err = NULL;
1474 const char *markers[3] = {
1475 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1476 GOT_DIFF_CONFLICT_MARKER_SEP,
1477 GOT_DIFF_CONFLICT_MARKER_END
1479 int i = 0;
1480 char *line;
1481 size_t len;
1482 const char delim[3] = {'\0', '\0', '\0'};
1484 while (*status == GOT_STATUS_MODIFY) {
1485 line = fparseln(f, &len, NULL, delim, 0);
1486 if (line == NULL) {
1487 if (feof(f))
1488 break;
1489 err = got_ferror(f, GOT_ERR_IO);
1490 break;
1493 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1494 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1495 == 0)
1496 *status = GOT_STATUS_CONFLICT;
1497 else
1498 i++;
1502 return err;
1505 static int
1506 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1508 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1509 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1512 static int
1513 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1515 return !(ie->ctime_sec == sb->st_ctime &&
1516 ie->ctime_nsec == sb->st_ctimensec &&
1517 ie->mtime_sec == sb->st_mtime &&
1518 ie->mtime_nsec == sb->st_mtimensec &&
1519 ie->size == (sb->st_size & 0xffffffff) &&
1520 !xbit_differs(ie, sb->st_mode));
1523 static unsigned char
1524 get_staged_status(struct got_fileindex_entry *ie)
1526 switch (got_fileindex_entry_stage_get(ie)) {
1527 case GOT_FILEIDX_STAGE_ADD:
1528 return GOT_STATUS_ADD;
1529 case GOT_FILEIDX_STAGE_DELETE:
1530 return GOT_STATUS_DELETE;
1531 case GOT_FILEIDX_STAGE_MODIFY:
1532 return GOT_STATUS_MODIFY;
1533 default:
1534 return GOT_STATUS_NO_CHANGE;
1538 static const struct got_error *
1539 get_symlink_status(unsigned char *status, struct stat *sb,
1540 struct got_fileindex_entry *ie, const char *abspath,
1541 int dirfd, const char *de_name, struct got_blob_object *blob)
1543 const struct got_error *err = NULL;
1544 char target_path[PATH_MAX];
1545 char etarget[PATH_MAX];
1546 ssize_t elen;
1547 size_t len, target_len = 0;
1548 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1549 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1551 *status = GOT_STATUS_NO_CHANGE;
1553 /* Blob object content specifies the target path of the link. */
1554 do {
1555 err = got_object_blob_read_block(&len, blob);
1556 if (err)
1557 return err;
1558 if (len + target_len >= sizeof(target_path)) {
1560 * Should not happen. The blob contents were OK
1561 * when this symlink was installed.
1563 return got_error(GOT_ERR_NO_SPACE);
1565 if (len > 0) {
1566 /* Skip blob object header first time around. */
1567 memcpy(target_path + target_len, buf + hdrlen,
1568 len - hdrlen);
1569 target_len += len - hdrlen;
1570 hdrlen = 0;
1572 } while (len != 0);
1573 target_path[target_len] = '\0';
1575 if (dirfd != -1) {
1576 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1577 if (elen == -1)
1578 return got_error_from_errno2("readlinkat", abspath);
1579 } else {
1580 elen = readlink(abspath, etarget, sizeof(etarget));
1581 if (elen == -1)
1582 return got_error_from_errno2("readlinkat", abspath);
1585 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1586 *status = GOT_STATUS_MODIFY;
1588 return NULL;
1591 static const struct got_error *
1592 get_file_status(unsigned char *status, struct stat *sb,
1593 struct got_fileindex_entry *ie, const char *abspath,
1594 int dirfd, const char *de_name, struct got_repository *repo)
1596 const struct got_error *err = NULL;
1597 struct got_object_id id;
1598 size_t hdrlen;
1599 int fd = -1;
1600 FILE *f = NULL;
1601 uint8_t fbuf[8192];
1602 struct got_blob_object *blob = NULL;
1603 size_t flen, blen;
1604 unsigned char staged_status = get_staged_status(ie);
1606 *status = GOT_STATUS_NO_CHANGE;
1609 * Whenever the caller provides a directory descriptor and a
1610 * directory entry name for the file, use them! This prevents
1611 * race conditions if filesystem paths change beneath our feet.
1613 if (dirfd != -1) {
1614 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1615 if (errno == ENOENT) {
1616 if (got_fileindex_entry_has_file_on_disk(ie))
1617 *status = GOT_STATUS_MISSING;
1618 else
1619 *status = GOT_STATUS_DELETE;
1620 goto done;
1622 err = got_error_from_errno2("fstatat", abspath);
1623 goto done;
1625 } else {
1626 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1627 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1628 return got_error_from_errno2("open", abspath);
1629 else if (fd == -1 && errno == ELOOP) {
1630 if (lstat(abspath, sb) == -1)
1631 return got_error_from_errno2("lstat", abspath);
1632 } else if (fd == -1 || fstat(fd, sb) == -1) {
1633 if (errno == ENOENT) {
1634 if (got_fileindex_entry_has_file_on_disk(ie))
1635 *status = GOT_STATUS_MISSING;
1636 else
1637 *status = GOT_STATUS_DELETE;
1638 goto done;
1640 err = got_error_from_errno2("fstat", abspath);
1641 goto done;
1645 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1646 *status = GOT_STATUS_OBSTRUCTED;
1647 goto done;
1650 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1651 *status = GOT_STATUS_DELETE;
1652 goto done;
1653 } else if (!got_fileindex_entry_has_blob(ie) &&
1654 staged_status != GOT_STATUS_ADD) {
1655 *status = GOT_STATUS_ADD;
1656 goto done;
1659 if (!stat_info_differs(ie, sb))
1660 goto done;
1662 if (staged_status == GOT_STATUS_MODIFY ||
1663 staged_status == GOT_STATUS_ADD)
1664 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1665 else
1666 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1668 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1669 if (err)
1670 goto done;
1672 if (S_ISLNK(sb->st_mode)) {
1673 /* Staging changes to symlinks is not yet(?) supported. */
1674 if (staged_status != GOT_STATUS_NO_CHANGE) {
1675 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1676 goto done;
1678 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1679 de_name, blob);
1680 goto done;
1684 if (dirfd != -1) {
1685 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1686 if (fd == -1) {
1687 err = got_error_from_errno2("openat", abspath);
1688 goto done;
1692 f = fdopen(fd, "r");
1693 if (f == NULL) {
1694 err = got_error_from_errno2("fdopen", abspath);
1695 goto done;
1697 fd = -1;
1698 hdrlen = got_object_blob_get_hdrlen(blob);
1699 for (;;) {
1700 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1701 err = got_object_blob_read_block(&blen, blob);
1702 if (err)
1703 goto done;
1704 /* Skip length of blob object header first time around. */
1705 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1706 if (flen == 0 && ferror(f)) {
1707 err = got_error_from_errno("fread");
1708 goto done;
1710 if (blen == 0) {
1711 if (flen != 0)
1712 *status = GOT_STATUS_MODIFY;
1713 break;
1714 } else if (flen == 0) {
1715 if (blen != 0)
1716 *status = GOT_STATUS_MODIFY;
1717 break;
1718 } else if (blen - hdrlen == flen) {
1719 /* Skip blob object header first time around. */
1720 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1721 *status = GOT_STATUS_MODIFY;
1722 break;
1724 } else {
1725 *status = GOT_STATUS_MODIFY;
1726 break;
1728 hdrlen = 0;
1731 if (*status == GOT_STATUS_MODIFY) {
1732 rewind(f);
1733 err = get_modified_file_content_status(status, f);
1734 } else if (xbit_differs(ie, sb->st_mode))
1735 *status = GOT_STATUS_MODE_CHANGE;
1736 done:
1737 if (blob)
1738 got_object_blob_close(blob);
1739 if (f != NULL && fclose(f) == EOF && err == NULL)
1740 err = got_error_from_errno2("fclose", abspath);
1741 if (fd != -1 && close(fd) == -1 && err == NULL)
1742 err = got_error_from_errno2("close", abspath);
1743 return err;
1747 * Update timestamps in the file index if a file is unmodified and
1748 * we had to run a full content comparison to find out.
1750 static const struct got_error *
1751 sync_timestamps(char *ondisk_path, unsigned char status,
1752 struct got_fileindex_entry *ie, struct stat *sb)
1754 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1755 return got_fileindex_entry_update(ie, ondisk_path,
1756 ie->blob_sha1, ie->commit_sha1, 1);
1758 return NULL;
1761 static const struct got_error *
1762 update_blob(struct got_worktree *worktree,
1763 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1764 struct got_tree_entry *te, const char *path,
1765 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1766 void *progress_arg)
1768 const struct got_error *err = NULL;
1769 struct got_blob_object *blob = NULL;
1770 char *ondisk_path;
1771 unsigned char status = GOT_STATUS_NO_CHANGE;
1772 struct stat sb;
1774 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1775 return got_error_from_errno("asprintf");
1777 if (ie) {
1778 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1779 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1780 goto done;
1782 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1783 repo);
1784 if (err)
1785 goto done;
1786 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1787 sb.st_mode = got_fileindex_perms_to_st(ie);
1788 } else
1789 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1791 if (status == GOT_STATUS_OBSTRUCTED) {
1792 err = (*progress_cb)(progress_arg, status, path);
1793 goto done;
1795 if (status == GOT_STATUS_CONFLICT) {
1796 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1797 path);
1798 goto done;
1801 if (ie && status != GOT_STATUS_MISSING &&
1802 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1803 if (got_fileindex_entry_has_commit(ie) &&
1804 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1805 SHA1_DIGEST_LENGTH) == 0) {
1806 err = sync_timestamps(ondisk_path, status, ie, &sb);
1807 if (err)
1808 goto done;
1809 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1810 path);
1811 goto done;
1813 if (got_fileindex_entry_has_blob(ie) &&
1814 memcmp(ie->blob_sha1, te->id.sha1,
1815 SHA1_DIGEST_LENGTH) == 0) {
1816 err = sync_timestamps(ondisk_path, status, ie, &sb);
1817 goto done;
1821 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1822 if (err)
1823 goto done;
1825 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1826 int update_timestamps;
1827 struct got_blob_object *blob2 = NULL;
1828 char *label_orig = NULL;
1829 if (got_fileindex_entry_has_blob(ie)) {
1830 struct got_object_id id2;
1831 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1832 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1833 if (err)
1834 goto done;
1836 if (got_fileindex_entry_has_commit(ie)) {
1837 char id_str[SHA1_DIGEST_STRING_LENGTH];
1838 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1839 sizeof(id_str)) == NULL) {
1840 err = got_error_path(id_str,
1841 GOT_ERR_BAD_OBJ_ID_STR);
1842 goto done;
1844 if (asprintf(&label_orig, "%s: commit %s",
1845 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1846 err = got_error_from_errno("asprintf");
1847 goto done;
1850 if (S_ISLNK(te->mode)) {
1851 err = merge_symlink(worktree, blob2,
1852 ondisk_path, path, sb.st_mode, label_orig,
1853 blob, worktree->base_commit_id, repo,
1854 progress_cb, progress_arg);
1855 } else {
1856 err = merge_blob(&update_timestamps, worktree, blob2,
1857 ondisk_path, path, sb.st_mode, label_orig, blob,
1858 worktree->base_commit_id, repo,
1859 progress_cb, progress_arg);
1861 free(label_orig);
1862 if (blob2)
1863 got_object_blob_close(blob2);
1864 if (err)
1865 goto done;
1867 * Do not update timestamps of files with local changes.
1868 * Otherwise, a future status walk would treat them as
1869 * unmodified files again.
1871 err = got_fileindex_entry_update(ie, ondisk_path,
1872 blob->id.sha1, worktree->base_commit_id->sha1,
1873 update_timestamps);
1874 } else if (status == GOT_STATUS_MODE_CHANGE) {
1875 err = got_fileindex_entry_update(ie, ondisk_path,
1876 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1877 } else if (status == GOT_STATUS_DELETE) {
1878 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1879 if (err)
1880 goto done;
1881 err = got_fileindex_entry_update(ie, ondisk_path,
1882 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1883 if (err)
1884 goto done;
1885 } else {
1886 err = install_blob(worktree, ondisk_path, path, te->mode,
1887 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0, 0,
1888 repo, progress_cb, progress_arg);
1889 if (err)
1890 goto done;
1891 if (ie) {
1892 err = got_fileindex_entry_update(ie, ondisk_path,
1893 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1894 } else {
1895 err = create_fileindex_entry(fileindex,
1896 worktree->base_commit_id, ondisk_path, path,
1897 &blob->id);
1899 if (err)
1900 goto done;
1902 got_object_blob_close(blob);
1903 done:
1904 free(ondisk_path);
1905 return err;
1908 static const struct got_error *
1909 remove_ondisk_file(const char *root_path, const char *path)
1911 const struct got_error *err = NULL;
1912 char *ondisk_path = NULL;
1914 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1915 return got_error_from_errno("asprintf");
1917 if (unlink(ondisk_path) == -1) {
1918 if (errno != ENOENT)
1919 err = got_error_from_errno2("unlink", ondisk_path);
1920 } else {
1921 char *parent = dirname(ondisk_path);
1922 while (parent && strcmp(parent, root_path) != 0) {
1923 if (rmdir(parent) == -1) {
1924 if (errno != ENOTEMPTY)
1925 err = got_error_from_errno2("rmdir",
1926 parent);
1927 break;
1929 parent = dirname(parent);
1932 free(ondisk_path);
1933 return err;
1936 static const struct got_error *
1937 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1938 struct got_fileindex_entry *ie, struct got_repository *repo,
1939 got_worktree_checkout_cb progress_cb, void *progress_arg)
1941 const struct got_error *err = NULL;
1942 unsigned char status;
1943 struct stat sb;
1944 char *ondisk_path;
1946 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1947 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1949 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1950 == -1)
1951 return got_error_from_errno("asprintf");
1953 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1954 if (err)
1955 goto done;
1957 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
1958 char ondisk_target[PATH_MAX];
1959 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
1960 sizeof(ondisk_target));
1961 if (ondisk_len == -1) {
1962 err = got_error_from_errno2("readlink", ondisk_path);
1963 goto done;
1965 ondisk_target[ondisk_len] = '\0';
1966 err = install_symlink_conflict(NULL, worktree->base_commit_id,
1967 NULL, NULL, /* XXX pass common ancestor info? */
1968 ondisk_target, ondisk_path);
1969 if (err)
1970 goto done;
1971 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1972 ie->path);
1973 goto done;
1976 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1977 status == GOT_STATUS_ADD) {
1978 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1979 if (err)
1980 goto done;
1982 * Preserve the working file and change the deleted blob's
1983 * entry into a schedule-add entry.
1985 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1986 0);
1987 } else {
1988 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1989 if (err)
1990 goto done;
1991 if (status == GOT_STATUS_NO_CHANGE) {
1992 err = remove_ondisk_file(worktree->root_path, ie->path);
1993 if (err)
1994 goto done;
1996 got_fileindex_entry_remove(fileindex, ie);
1998 done:
1999 free(ondisk_path);
2000 return err;
2003 struct diff_cb_arg {
2004 struct got_fileindex *fileindex;
2005 struct got_worktree *worktree;
2006 struct got_repository *repo;
2007 got_worktree_checkout_cb progress_cb;
2008 void *progress_arg;
2009 got_cancel_cb cancel_cb;
2010 void *cancel_arg;
2013 static const struct got_error *
2014 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2015 struct got_tree_entry *te, const char *parent_path)
2017 struct diff_cb_arg *a = arg;
2019 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2020 return got_error(GOT_ERR_CANCELLED);
2022 return update_blob(a->worktree, a->fileindex, ie, te,
2023 ie->path, a->repo, a->progress_cb, a->progress_arg);
2026 static const struct got_error *
2027 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2029 struct diff_cb_arg *a = arg;
2031 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2032 return got_error(GOT_ERR_CANCELLED);
2034 return delete_blob(a->worktree, a->fileindex, ie,
2035 a->repo, a->progress_cb, a->progress_arg);
2038 static const struct got_error *
2039 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2041 struct diff_cb_arg *a = arg;
2042 const struct got_error *err;
2043 char *path;
2045 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2046 return got_error(GOT_ERR_CANCELLED);
2048 if (got_object_tree_entry_is_submodule(te))
2049 return NULL;
2051 if (asprintf(&path, "%s%s%s", parent_path,
2052 parent_path[0] ? "/" : "", te->name)
2053 == -1)
2054 return got_error_from_errno("asprintf");
2056 if (S_ISDIR(te->mode))
2057 err = add_dir_on_disk(a->worktree, path);
2058 else
2059 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2060 a->repo, a->progress_cb, a->progress_arg);
2062 free(path);
2063 return err;
2066 static const struct got_error *
2067 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2069 const struct got_error *err = NULL;
2070 char *uuidstr = NULL;
2071 uint32_t uuid_status;
2073 *refname = NULL;
2075 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2076 if (uuid_status != uuid_s_ok)
2077 return got_error_uuid(uuid_status, "uuid_to_string");
2079 if (asprintf(refname, "%s-%s", prefix, uuidstr)
2080 == -1) {
2081 err = got_error_from_errno("asprintf");
2082 *refname = NULL;
2084 free(uuidstr);
2085 return err;
2088 const struct got_error *
2089 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2091 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2094 static const struct got_error *
2095 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2097 return get_ref_name(refname, worktree,
2098 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2101 static const struct got_error *
2102 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2104 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2107 static const struct got_error *
2108 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2110 return get_ref_name(refname, worktree,
2111 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2114 static const struct got_error *
2115 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2117 return get_ref_name(refname, worktree,
2118 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2121 static const struct got_error *
2122 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2124 return get_ref_name(refname, worktree,
2125 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2128 static const struct got_error *
2129 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2131 return get_ref_name(refname, worktree,
2132 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2135 static const struct got_error *
2136 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2138 return get_ref_name(refname, worktree,
2139 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2142 static const struct got_error *
2143 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2145 return get_ref_name(refname, worktree,
2146 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2149 const struct got_error *
2150 got_worktree_get_histedit_script_path(char **path,
2151 struct got_worktree *worktree)
2153 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2154 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2155 *path = NULL;
2156 return got_error_from_errno("asprintf");
2158 return NULL;
2162 * Prevent Git's garbage collector from deleting our base commit by
2163 * setting a reference to our base commit's ID.
2165 static const struct got_error *
2166 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2168 const struct got_error *err = NULL;
2169 struct got_reference *ref = NULL;
2170 char *refname;
2172 err = got_worktree_get_base_ref_name(&refname, worktree);
2173 if (err)
2174 return err;
2176 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2177 if (err)
2178 goto done;
2180 err = got_ref_write(ref, repo);
2181 done:
2182 free(refname);
2183 if (ref)
2184 got_ref_close(ref);
2185 return err;
2188 static const struct got_error *
2189 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2191 const struct got_error *err = NULL;
2193 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2194 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2195 err = got_error_from_errno("asprintf");
2196 *fileindex_path = NULL;
2198 return err;
2202 static const struct got_error *
2203 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2204 struct got_worktree *worktree)
2206 const struct got_error *err = NULL;
2207 FILE *index = NULL;
2209 *fileindex_path = NULL;
2210 *fileindex = got_fileindex_alloc();
2211 if (*fileindex == NULL)
2212 return got_error_from_errno("got_fileindex_alloc");
2214 err = get_fileindex_path(fileindex_path, worktree);
2215 if (err)
2216 goto done;
2218 index = fopen(*fileindex_path, "rb");
2219 if (index == NULL) {
2220 if (errno != ENOENT)
2221 err = got_error_from_errno2("fopen", *fileindex_path);
2222 } else {
2223 err = got_fileindex_read(*fileindex, index);
2224 if (fclose(index) != 0 && err == NULL)
2225 err = got_error_from_errno("fclose");
2227 done:
2228 if (err) {
2229 free(*fileindex_path);
2230 *fileindex_path = NULL;
2231 got_fileindex_free(*fileindex);
2232 *fileindex = NULL;
2234 return err;
2237 struct bump_base_commit_id_arg {
2238 struct got_object_id *base_commit_id;
2239 const char *path;
2240 size_t path_len;
2241 const char *entry_name;
2242 got_worktree_checkout_cb progress_cb;
2243 void *progress_arg;
2246 /* Bump base commit ID of all files within an updated part of the work tree. */
2247 static const struct got_error *
2248 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2250 const struct got_error *err;
2251 struct bump_base_commit_id_arg *a = arg;
2253 if (a->entry_name) {
2254 if (strcmp(ie->path, a->path) != 0)
2255 return NULL;
2256 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2257 return NULL;
2259 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2260 SHA1_DIGEST_LENGTH) == 0)
2261 return NULL;
2263 if (a->progress_cb) {
2264 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2265 ie->path);
2266 if (err)
2267 return err;
2269 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2270 return NULL;
2273 static const struct got_error *
2274 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2276 const struct got_error *err = NULL;
2277 char *new_fileindex_path = NULL;
2278 FILE *new_index = NULL;
2279 struct timespec timeout;
2281 err = got_opentemp_named(&new_fileindex_path, &new_index,
2282 fileindex_path);
2283 if (err)
2284 goto done;
2286 err = got_fileindex_write(fileindex, new_index);
2287 if (err)
2288 goto done;
2290 if (rename(new_fileindex_path, fileindex_path) != 0) {
2291 err = got_error_from_errno3("rename", new_fileindex_path,
2292 fileindex_path);
2293 unlink(new_fileindex_path);
2297 * Sleep for a short amount of time to ensure that files modified after
2298 * this program exits have a different time stamp from the one which
2299 * was recorded in the file index.
2301 timeout.tv_sec = 0;
2302 timeout.tv_nsec = 1;
2303 nanosleep(&timeout, NULL);
2304 done:
2305 if (new_index)
2306 fclose(new_index);
2307 free(new_fileindex_path);
2308 return err;
2311 static const struct got_error *
2312 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2313 struct got_object_id **tree_id, const char *wt_relpath,
2314 struct got_worktree *worktree, struct got_repository *repo)
2316 const struct got_error *err = NULL;
2317 struct got_object_id *id = NULL;
2318 char *in_repo_path = NULL;
2319 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2321 *entry_type = GOT_OBJ_TYPE_ANY;
2322 *tree_relpath = NULL;
2323 *tree_id = NULL;
2325 if (wt_relpath[0] == '\0') {
2326 /* Check out all files within the work tree. */
2327 *entry_type = GOT_OBJ_TYPE_TREE;
2328 *tree_relpath = strdup("");
2329 if (*tree_relpath == NULL) {
2330 err = got_error_from_errno("strdup");
2331 goto done;
2333 err = got_object_id_by_path(tree_id, repo,
2334 worktree->base_commit_id, worktree->path_prefix);
2335 if (err)
2336 goto done;
2337 return NULL;
2340 /* Check out a subset of files in the work tree. */
2342 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2343 is_root_wt ? "" : "/", wt_relpath) == -1) {
2344 err = got_error_from_errno("asprintf");
2345 goto done;
2348 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2349 in_repo_path);
2350 if (err)
2351 goto done;
2353 free(in_repo_path);
2354 in_repo_path = NULL;
2356 err = got_object_get_type(entry_type, repo, id);
2357 if (err)
2358 goto done;
2360 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2361 /* Check out a single file. */
2362 if (strchr(wt_relpath, '/') == NULL) {
2363 /* Check out a single file in work tree's root dir. */
2364 in_repo_path = strdup(worktree->path_prefix);
2365 if (in_repo_path == NULL) {
2366 err = got_error_from_errno("strdup");
2367 goto done;
2369 *tree_relpath = strdup("");
2370 if (*tree_relpath == NULL) {
2371 err = got_error_from_errno("strdup");
2372 goto done;
2374 } else {
2375 /* Check out a single file in a subdirectory. */
2376 err = got_path_dirname(tree_relpath, wt_relpath);
2377 if (err)
2378 return err;
2379 if (asprintf(&in_repo_path, "%s%s%s",
2380 worktree->path_prefix, is_root_wt ? "" : "/",
2381 *tree_relpath) == -1) {
2382 err = got_error_from_errno("asprintf");
2383 goto done;
2386 err = got_object_id_by_path(tree_id, repo,
2387 worktree->base_commit_id, in_repo_path);
2388 } else {
2389 /* Check out all files within a subdirectory. */
2390 *tree_id = got_object_id_dup(id);
2391 if (*tree_id == NULL) {
2392 err = got_error_from_errno("got_object_id_dup");
2393 goto done;
2395 *tree_relpath = strdup(wt_relpath);
2396 if (*tree_relpath == NULL) {
2397 err = got_error_from_errno("strdup");
2398 goto done;
2401 done:
2402 free(id);
2403 free(in_repo_path);
2404 if (err) {
2405 *entry_type = GOT_OBJ_TYPE_ANY;
2406 free(*tree_relpath);
2407 *tree_relpath = NULL;
2408 free(*tree_id);
2409 *tree_id = NULL;
2411 return err;
2414 static const struct got_error *
2415 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2416 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2417 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2418 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2420 const struct got_error *err = NULL;
2421 struct got_commit_object *commit = NULL;
2422 struct got_tree_object *tree = NULL;
2423 struct got_fileindex_diff_tree_cb diff_cb;
2424 struct diff_cb_arg arg;
2426 err = ref_base_commit(worktree, repo);
2427 if (err) {
2428 if (!(err->code == GOT_ERR_ERRNO &&
2429 (errno == EACCES || errno == EROFS)))
2430 goto done;
2431 err = (*progress_cb)(progress_arg,
2432 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2433 if (err)
2434 return err;
2437 err = got_object_open_as_commit(&commit, repo,
2438 worktree->base_commit_id);
2439 if (err)
2440 goto done;
2442 err = got_object_open_as_tree(&tree, repo, tree_id);
2443 if (err)
2444 goto done;
2446 if (entry_name &&
2447 got_object_tree_find_entry(tree, entry_name) == NULL) {
2448 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2449 goto done;
2452 diff_cb.diff_old_new = diff_old_new;
2453 diff_cb.diff_old = diff_old;
2454 diff_cb.diff_new = diff_new;
2455 arg.fileindex = fileindex;
2456 arg.worktree = worktree;
2457 arg.repo = repo;
2458 arg.progress_cb = progress_cb;
2459 arg.progress_arg = progress_arg;
2460 arg.cancel_cb = cancel_cb;
2461 arg.cancel_arg = cancel_arg;
2462 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2463 entry_name, repo, &diff_cb, &arg);
2464 done:
2465 if (tree)
2466 got_object_tree_close(tree);
2467 if (commit)
2468 got_object_commit_close(commit);
2469 return err;
2472 const struct got_error *
2473 got_worktree_checkout_files(struct got_worktree *worktree,
2474 struct got_pathlist_head *paths, struct got_repository *repo,
2475 got_worktree_checkout_cb progress_cb, void *progress_arg,
2476 got_cancel_cb cancel_cb, void *cancel_arg)
2478 const struct got_error *err = NULL, *sync_err, *unlockerr;
2479 struct got_commit_object *commit = NULL;
2480 struct got_tree_object *tree = NULL;
2481 struct got_fileindex *fileindex = NULL;
2482 char *fileindex_path = NULL;
2483 struct got_pathlist_entry *pe;
2484 struct tree_path_data {
2485 SIMPLEQ_ENTRY(tree_path_data) entry;
2486 struct got_object_id *tree_id;
2487 int entry_type;
2488 char *relpath;
2489 char *entry_name;
2490 } *tpd = NULL;
2491 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2493 SIMPLEQ_INIT(&tree_paths);
2495 err = lock_worktree(worktree, LOCK_EX);
2496 if (err)
2497 return err;
2499 /* Map all specified paths to in-repository trees. */
2500 TAILQ_FOREACH(pe, paths, entry) {
2501 tpd = malloc(sizeof(*tpd));
2502 if (tpd == NULL) {
2503 err = got_error_from_errno("malloc");
2504 goto done;
2507 err = find_tree_entry_for_checkout(&tpd->entry_type,
2508 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2509 if (err) {
2510 free(tpd);
2511 goto done;
2514 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2515 err = got_path_basename(&tpd->entry_name, pe->path);
2516 if (err) {
2517 free(tpd->relpath);
2518 free(tpd->tree_id);
2519 free(tpd);
2520 goto done;
2522 } else
2523 tpd->entry_name = NULL;
2525 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2529 * Read the file index.
2530 * Checking out files is supposed to be an idempotent operation.
2531 * If the on-disk file index is incomplete we will try to complete it.
2533 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2534 if (err)
2535 goto done;
2537 tpd = SIMPLEQ_FIRST(&tree_paths);
2538 TAILQ_FOREACH(pe, paths, entry) {
2539 struct bump_base_commit_id_arg bbc_arg;
2541 err = checkout_files(worktree, fileindex, tpd->relpath,
2542 tpd->tree_id, tpd->entry_name, repo,
2543 progress_cb, progress_arg, cancel_cb, cancel_arg);
2544 if (err)
2545 break;
2547 bbc_arg.base_commit_id = worktree->base_commit_id;
2548 bbc_arg.entry_name = tpd->entry_name;
2549 bbc_arg.path = pe->path;
2550 bbc_arg.path_len = pe->path_len;
2551 bbc_arg.progress_cb = progress_cb;
2552 bbc_arg.progress_arg = progress_arg;
2553 err = got_fileindex_for_each_entry_safe(fileindex,
2554 bump_base_commit_id, &bbc_arg);
2555 if (err)
2556 break;
2558 tpd = SIMPLEQ_NEXT(tpd, entry);
2560 sync_err = sync_fileindex(fileindex, fileindex_path);
2561 if (sync_err && err == NULL)
2562 err = sync_err;
2563 done:
2564 free(fileindex_path);
2565 if (tree)
2566 got_object_tree_close(tree);
2567 if (commit)
2568 got_object_commit_close(commit);
2569 if (fileindex)
2570 got_fileindex_free(fileindex);
2571 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2572 tpd = SIMPLEQ_FIRST(&tree_paths);
2573 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2574 free(tpd->relpath);
2575 free(tpd->tree_id);
2576 free(tpd);
2578 unlockerr = lock_worktree(worktree, LOCK_SH);
2579 if (unlockerr && err == NULL)
2580 err = unlockerr;
2581 return err;
2584 struct merge_file_cb_arg {
2585 struct got_worktree *worktree;
2586 struct got_fileindex *fileindex;
2587 got_worktree_checkout_cb progress_cb;
2588 void *progress_arg;
2589 got_cancel_cb cancel_cb;
2590 void *cancel_arg;
2591 const char *label_orig;
2592 struct got_object_id *commit_id2;
2595 static const struct got_error *
2596 merge_file_cb(void *arg, struct got_blob_object *blob1,
2597 struct got_blob_object *blob2, struct got_object_id *id1,
2598 struct got_object_id *id2, const char *path1, const char *path2,
2599 mode_t mode1, mode_t mode2, struct got_repository *repo)
2601 static const struct got_error *err = NULL;
2602 struct merge_file_cb_arg *a = arg;
2603 struct got_fileindex_entry *ie;
2604 char *ondisk_path = NULL;
2605 struct stat sb;
2606 unsigned char status;
2607 int local_changes_subsumed;
2609 if (blob1 && blob2) {
2610 ie = got_fileindex_entry_get(a->fileindex, path2,
2611 strlen(path2));
2612 if (ie == NULL)
2613 return (*a->progress_cb)(a->progress_arg,
2614 GOT_STATUS_MISSING, path2);
2616 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2617 path2) == -1)
2618 return got_error_from_errno("asprintf");
2620 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2621 repo);
2622 if (err)
2623 goto done;
2625 if (status == GOT_STATUS_DELETE) {
2626 err = (*a->progress_cb)(a->progress_arg,
2627 GOT_STATUS_MERGE, path2);
2628 goto done;
2630 if (status != GOT_STATUS_NO_CHANGE &&
2631 status != GOT_STATUS_MODIFY &&
2632 status != GOT_STATUS_CONFLICT &&
2633 status != GOT_STATUS_ADD) {
2634 err = (*a->progress_cb)(a->progress_arg, status, path2);
2635 goto done;
2638 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2639 err = merge_symlink(a->worktree, blob1,
2640 ondisk_path, path2, sb.st_mode, a->label_orig,
2641 blob2, a->commit_id2, repo, a->progress_cb,
2642 a->progress_arg);
2643 } else {
2644 err = merge_blob(&local_changes_subsumed, a->worktree,
2645 blob1, ondisk_path, path2, sb.st_mode,
2646 a->label_orig, blob2, a->commit_id2, repo,
2647 a->progress_cb, a->progress_arg);
2649 } else if (blob1) {
2650 ie = got_fileindex_entry_get(a->fileindex, path1,
2651 strlen(path1));
2652 if (ie == NULL)
2653 return (*a->progress_cb)(a->progress_arg,
2654 GOT_STATUS_MISSING, path1);
2656 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2657 path1) == -1)
2658 return got_error_from_errno("asprintf");
2660 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2661 repo);
2662 if (err)
2663 goto done;
2665 switch (status) {
2666 case GOT_STATUS_NO_CHANGE:
2667 err = (*a->progress_cb)(a->progress_arg,
2668 GOT_STATUS_DELETE, path1);
2669 if (err)
2670 goto done;
2671 err = remove_ondisk_file(a->worktree->root_path, path1);
2672 if (err)
2673 goto done;
2674 if (ie)
2675 got_fileindex_entry_mark_deleted_from_disk(ie);
2676 break;
2677 case GOT_STATUS_DELETE:
2678 case GOT_STATUS_MISSING:
2679 err = (*a->progress_cb)(a->progress_arg,
2680 GOT_STATUS_DELETE, path1);
2681 if (err)
2682 goto done;
2683 if (ie)
2684 got_fileindex_entry_mark_deleted_from_disk(ie);
2685 break;
2686 case GOT_STATUS_ADD:
2687 case GOT_STATUS_MODIFY:
2688 case GOT_STATUS_CONFLICT:
2689 err = (*a->progress_cb)(a->progress_arg,
2690 GOT_STATUS_CANNOT_DELETE, path1);
2691 if (err)
2692 goto done;
2693 break;
2694 case GOT_STATUS_OBSTRUCTED:
2695 err = (*a->progress_cb)(a->progress_arg, status, path1);
2696 if (err)
2697 goto done;
2698 break;
2699 default:
2700 break;
2702 } else if (blob2) {
2703 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2704 path2) == -1)
2705 return got_error_from_errno("asprintf");
2706 ie = got_fileindex_entry_get(a->fileindex, path2,
2707 strlen(path2));
2708 if (ie) {
2709 err = get_file_status(&status, &sb, ie, ondisk_path,
2710 -1, NULL, repo);
2711 if (err)
2712 goto done;
2713 if (status != GOT_STATUS_NO_CHANGE &&
2714 status != GOT_STATUS_MODIFY &&
2715 status != GOT_STATUS_CONFLICT &&
2716 status != GOT_STATUS_ADD) {
2717 err = (*a->progress_cb)(a->progress_arg,
2718 status, path2);
2719 goto done;
2721 if (S_ISLNK(mode2)) {
2722 err = merge_symlink(a->worktree, NULL,
2723 ondisk_path, path2, sb.st_mode,
2724 a->label_orig, blob2, a->commit_id2,
2725 repo, a->progress_cb, a->progress_arg);
2726 if (err)
2727 goto done;
2728 } else {
2729 err = merge_blob(&local_changes_subsumed,
2730 a->worktree, NULL, ondisk_path, path2,
2731 sb.st_mode, a->label_orig, blob2,
2732 a->commit_id2, repo, a->progress_cb,
2733 a->progress_arg);
2734 if (err)
2735 goto done;
2737 if (status == GOT_STATUS_DELETE) {
2738 err = got_fileindex_entry_update(ie,
2739 ondisk_path, blob2->id.sha1,
2740 a->worktree->base_commit_id->sha1, 0);
2741 if (err)
2742 goto done;
2744 } else {
2745 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2746 err = install_blob(a->worktree, ondisk_path, path2,
2747 mode2, sb.st_mode, blob2, 0, 0, 0, repo,
2748 a->progress_cb, a->progress_arg);
2749 if (err)
2750 goto done;
2751 err = got_fileindex_entry_alloc(&ie, path2);
2752 if (err)
2753 goto done;
2754 err = got_fileindex_entry_update(ie, ondisk_path,
2755 NULL, NULL, 1);
2756 if (err) {
2757 got_fileindex_entry_free(ie);
2758 goto done;
2760 err = got_fileindex_entry_add(a->fileindex, ie);
2761 if (err) {
2762 got_fileindex_entry_free(ie);
2763 goto done;
2767 done:
2768 free(ondisk_path);
2769 return err;
2772 struct check_merge_ok_arg {
2773 struct got_worktree *worktree;
2774 struct got_repository *repo;
2777 static const struct got_error *
2778 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2780 const struct got_error *err = NULL;
2781 struct check_merge_ok_arg *a = arg;
2782 unsigned char status;
2783 struct stat sb;
2784 char *ondisk_path;
2786 /* Reject merges into a work tree with mixed base commits. */
2787 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2788 SHA1_DIGEST_LENGTH))
2789 return got_error(GOT_ERR_MIXED_COMMITS);
2791 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2792 == -1)
2793 return got_error_from_errno("asprintf");
2795 /* Reject merges into a work tree with conflicted files. */
2796 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2797 if (err)
2798 return err;
2799 if (status == GOT_STATUS_CONFLICT)
2800 return got_error(GOT_ERR_CONFLICTS);
2802 return NULL;
2805 static const struct got_error *
2806 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2807 const char *fileindex_path, struct got_object_id *commit_id1,
2808 struct got_object_id *commit_id2, struct got_repository *repo,
2809 got_worktree_checkout_cb progress_cb, void *progress_arg,
2810 got_cancel_cb cancel_cb, void *cancel_arg)
2812 const struct got_error *err = NULL, *sync_err;
2813 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2814 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2815 struct merge_file_cb_arg arg;
2816 char *label_orig = NULL;
2818 if (commit_id1) {
2819 char *id_str;
2821 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2822 worktree->path_prefix);
2823 if (err)
2824 goto done;
2826 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2827 if (err)
2828 goto done;
2830 err = got_object_id_str(&id_str, commit_id1);
2831 if (err)
2832 goto done;
2834 if (asprintf(&label_orig, "%s: commit %s",
2835 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2836 err = got_error_from_errno("asprintf");
2837 free(id_str);
2838 goto done;
2840 free(id_str);
2843 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2844 worktree->path_prefix);
2845 if (err)
2846 goto done;
2848 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2849 if (err)
2850 goto done;
2852 arg.worktree = worktree;
2853 arg.fileindex = fileindex;
2854 arg.progress_cb = progress_cb;
2855 arg.progress_arg = progress_arg;
2856 arg.cancel_cb = cancel_cb;
2857 arg.cancel_arg = cancel_arg;
2858 arg.label_orig = label_orig;
2859 arg.commit_id2 = commit_id2;
2860 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2861 sync_err = sync_fileindex(fileindex, fileindex_path);
2862 if (sync_err && err == NULL)
2863 err = sync_err;
2864 done:
2865 if (tree1)
2866 got_object_tree_close(tree1);
2867 if (tree2)
2868 got_object_tree_close(tree2);
2869 free(label_orig);
2870 return err;
2873 const struct got_error *
2874 got_worktree_merge_files(struct got_worktree *worktree,
2875 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2876 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2877 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2879 const struct got_error *err, *unlockerr;
2880 char *fileindex_path = NULL;
2881 struct got_fileindex *fileindex = NULL;
2882 struct check_merge_ok_arg mok_arg;
2884 err = lock_worktree(worktree, LOCK_EX);
2885 if (err)
2886 return err;
2888 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2889 if (err)
2890 goto done;
2892 mok_arg.worktree = worktree;
2893 mok_arg.repo = repo;
2894 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2895 &mok_arg);
2896 if (err)
2897 goto done;
2899 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2900 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2901 done:
2902 if (fileindex)
2903 got_fileindex_free(fileindex);
2904 free(fileindex_path);
2905 unlockerr = lock_worktree(worktree, LOCK_SH);
2906 if (unlockerr && err == NULL)
2907 err = unlockerr;
2908 return err;
2911 struct diff_dir_cb_arg {
2912 struct got_fileindex *fileindex;
2913 struct got_worktree *worktree;
2914 const char *status_path;
2915 size_t status_path_len;
2916 struct got_repository *repo;
2917 got_worktree_status_cb status_cb;
2918 void *status_arg;
2919 got_cancel_cb cancel_cb;
2920 void *cancel_arg;
2921 /* A pathlist containing per-directory pathlists of ignore patterns. */
2922 struct got_pathlist_head ignores;
2923 int report_unchanged;
2924 int no_ignores;
2927 static const struct got_error *
2928 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2929 int dirfd, const char *de_name,
2930 got_worktree_status_cb status_cb, void *status_arg,
2931 struct got_repository *repo, int report_unchanged)
2933 const struct got_error *err = NULL;
2934 unsigned char status = GOT_STATUS_NO_CHANGE;
2935 unsigned char staged_status = get_staged_status(ie);
2936 struct stat sb;
2937 struct got_object_id blob_id, commit_id, staged_blob_id;
2938 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2939 struct got_object_id *staged_blob_idp = NULL;
2941 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2942 if (err)
2943 return err;
2945 if (status == GOT_STATUS_NO_CHANGE &&
2946 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2947 return NULL;
2949 if (got_fileindex_entry_has_blob(ie)) {
2950 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2951 blob_idp = &blob_id;
2953 if (got_fileindex_entry_has_commit(ie)) {
2954 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2955 commit_idp = &commit_id;
2957 if (staged_status == GOT_STATUS_ADD ||
2958 staged_status == GOT_STATUS_MODIFY) {
2959 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2960 SHA1_DIGEST_LENGTH);
2961 staged_blob_idp = &staged_blob_id;
2964 return (*status_cb)(status_arg, status, staged_status,
2965 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2968 static const struct got_error *
2969 status_old_new(void *arg, struct got_fileindex_entry *ie,
2970 struct dirent *de, const char *parent_path, int dirfd)
2972 const struct got_error *err = NULL;
2973 struct diff_dir_cb_arg *a = arg;
2974 char *abspath;
2976 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2977 return got_error(GOT_ERR_CANCELLED);
2979 if (got_path_cmp(parent_path, a->status_path,
2980 strlen(parent_path), a->status_path_len) != 0 &&
2981 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2982 return NULL;
2984 if (parent_path[0]) {
2985 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2986 parent_path, de->d_name) == -1)
2987 return got_error_from_errno("asprintf");
2988 } else {
2989 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2990 de->d_name) == -1)
2991 return got_error_from_errno("asprintf");
2994 err = report_file_status(ie, abspath, dirfd, de->d_name,
2995 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2996 free(abspath);
2997 return err;
3000 static const struct got_error *
3001 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3003 struct diff_dir_cb_arg *a = arg;
3004 struct got_object_id blob_id, commit_id;
3005 unsigned char status;
3007 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3008 return got_error(GOT_ERR_CANCELLED);
3010 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3011 return NULL;
3013 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3014 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3015 if (got_fileindex_entry_has_file_on_disk(ie))
3016 status = GOT_STATUS_MISSING;
3017 else
3018 status = GOT_STATUS_DELETE;
3019 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3020 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3023 void
3024 free_ignorelist(struct got_pathlist_head *ignorelist)
3026 struct got_pathlist_entry *pe;
3028 TAILQ_FOREACH(pe, ignorelist, entry)
3029 free((char *)pe->path);
3030 got_pathlist_free(ignorelist);
3033 void
3034 free_ignores(struct got_pathlist_head *ignores)
3036 struct got_pathlist_entry *pe;
3038 TAILQ_FOREACH(pe, ignores, entry) {
3039 struct got_pathlist_head *ignorelist = pe->data;
3040 free_ignorelist(ignorelist);
3041 free((char *)pe->path);
3043 got_pathlist_free(ignores);
3046 static const struct got_error *
3047 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3049 const struct got_error *err = NULL;
3050 struct got_pathlist_entry *pe = NULL;
3051 struct got_pathlist_head *ignorelist;
3052 char *line = NULL, *pattern, *dirpath = NULL;
3053 size_t linesize = 0;
3054 ssize_t linelen;
3056 ignorelist = calloc(1, sizeof(*ignorelist));
3057 if (ignorelist == NULL)
3058 return got_error_from_errno("calloc");
3059 TAILQ_INIT(ignorelist);
3061 while ((linelen = getline(&line, &linesize, f)) != -1) {
3062 if (linelen > 0 && line[linelen - 1] == '\n')
3063 line[linelen - 1] = '\0';
3065 /* Git's ignores may contain comments. */
3066 if (line[0] == '#')
3067 continue;
3069 /* Git's negated patterns are not (yet?) supported. */
3070 if (line[0] == '!')
3071 continue;
3073 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3074 line) == -1) {
3075 err = got_error_from_errno("asprintf");
3076 goto done;
3078 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3079 if (err)
3080 goto done;
3082 if (ferror(f)) {
3083 err = got_error_from_errno("getline");
3084 goto done;
3087 dirpath = strdup(path);
3088 if (dirpath == NULL) {
3089 err = got_error_from_errno("strdup");
3090 goto done;
3092 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3093 done:
3094 free(line);
3095 if (err || pe == NULL) {
3096 free(dirpath);
3097 free_ignorelist(ignorelist);
3099 return err;
3102 int
3103 match_ignores(struct got_pathlist_head *ignores, const char *path)
3105 struct got_pathlist_entry *pe;
3107 /* Handle patterns which match in all directories. */
3108 TAILQ_FOREACH(pe, ignores, entry) {
3109 struct got_pathlist_head *ignorelist = pe->data;
3110 struct got_pathlist_entry *pi;
3112 TAILQ_FOREACH(pi, ignorelist, entry) {
3113 const char *p, *pattern = pi->path;
3115 if (strncmp(pattern, "**/", 3) != 0)
3116 continue;
3117 pattern += 3;
3118 p = path;
3119 while (*p) {
3120 if (fnmatch(pattern, p,
3121 FNM_PATHNAME | FNM_LEADING_DIR)) {
3122 /* Retry in next directory. */
3123 while (*p && *p != '/')
3124 p++;
3125 while (*p == '/')
3126 p++;
3127 continue;
3129 return 1;
3135 * The ignores pathlist contains ignore lists from children before
3136 * parents, so we can find the most specific ignorelist by walking
3137 * ignores backwards.
3139 pe = TAILQ_LAST(ignores, got_pathlist_head);
3140 while (pe) {
3141 if (got_path_is_child(path, pe->path, pe->path_len)) {
3142 struct got_pathlist_head *ignorelist = pe->data;
3143 struct got_pathlist_entry *pi;
3144 TAILQ_FOREACH(pi, ignorelist, entry) {
3145 const char *pattern = pi->path;
3146 int flags = FNM_LEADING_DIR;
3147 if (strstr(pattern, "/**/") == NULL)
3148 flags |= FNM_PATHNAME;
3149 if (fnmatch(pattern, path, flags))
3150 continue;
3151 return 1;
3154 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3157 return 0;
3160 static const struct got_error *
3161 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3162 const char *path, int dirfd, const char *ignores_filename)
3164 const struct got_error *err = NULL;
3165 char *ignorespath;
3166 int fd = -1;
3167 FILE *ignoresfile = NULL;
3169 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3170 path[0] ? "/" : "", ignores_filename) == -1)
3171 return got_error_from_errno("asprintf");
3173 if (dirfd != -1) {
3174 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3175 if (fd == -1) {
3176 if (errno != ENOENT && errno != EACCES)
3177 err = got_error_from_errno2("openat",
3178 ignorespath);
3179 } else {
3180 ignoresfile = fdopen(fd, "r");
3181 if (ignoresfile == NULL)
3182 err = got_error_from_errno2("fdopen",
3183 ignorespath);
3184 else {
3185 fd = -1;
3186 err = read_ignores(ignores, path, ignoresfile);
3189 } else {
3190 ignoresfile = fopen(ignorespath, "r");
3191 if (ignoresfile == NULL) {
3192 if (errno != ENOENT && errno != EACCES)
3193 err = got_error_from_errno2("fopen",
3194 ignorespath);
3195 } else
3196 err = read_ignores(ignores, path, ignoresfile);
3199 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3200 err = got_error_from_errno2("fclose", path);
3201 if (fd != -1 && close(fd) == -1 && err == NULL)
3202 err = got_error_from_errno2("close", path);
3203 free(ignorespath);
3204 return err;
3207 static const struct got_error *
3208 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3210 const struct got_error *err = NULL;
3211 struct diff_dir_cb_arg *a = arg;
3212 char *path = NULL;
3214 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3215 return got_error(GOT_ERR_CANCELLED);
3217 if (parent_path[0]) {
3218 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3219 return got_error_from_errno("asprintf");
3220 } else {
3221 path = de->d_name;
3224 if (de->d_type != DT_DIR &&
3225 got_path_is_child(path, a->status_path, a->status_path_len)
3226 && !match_ignores(&a->ignores, path))
3227 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3228 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3229 if (parent_path[0])
3230 free(path);
3231 return err;
3234 static const struct got_error *
3235 status_traverse(void *arg, const char *path, int dirfd)
3237 const struct got_error *err = NULL;
3238 struct diff_dir_cb_arg *a = arg;
3240 if (a->no_ignores)
3241 return NULL;
3243 err = add_ignores(&a->ignores, a->worktree->root_path,
3244 path, dirfd, ".cvsignore");
3245 if (err)
3246 return err;
3248 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3249 dirfd, ".gitignore");
3251 return err;
3254 static const struct got_error *
3255 report_single_file_status(const char *path, const char *ondisk_path,
3256 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3257 void *status_arg, struct got_repository *repo, int report_unchanged)
3259 struct got_fileindex_entry *ie;
3260 struct stat sb;
3262 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3263 if (ie)
3264 return report_file_status(ie, ondisk_path, -1, NULL,
3265 status_cb, status_arg, repo, report_unchanged);
3267 if (lstat(ondisk_path, &sb) == -1) {
3268 if (errno != ENOENT)
3269 return got_error_from_errno2("lstat", ondisk_path);
3270 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3271 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3272 return NULL;
3275 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3276 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3277 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3279 return NULL;
3282 static const struct got_error *
3283 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3284 const char *root_path, const char *path)
3286 const struct got_error *err;
3287 char *parent_path, *next_parent_path = NULL;
3289 err = add_ignores(ignores, root_path, "", -1,
3290 ".cvsignore");
3291 if (err)
3292 return err;
3294 err = add_ignores(ignores, root_path, "", -1,
3295 ".gitignore");
3296 if (err)
3297 return err;
3299 err = got_path_dirname(&parent_path, path);
3300 if (err) {
3301 if (err->code == GOT_ERR_BAD_PATH)
3302 return NULL; /* cannot traverse parent */
3303 return err;
3305 for (;;) {
3306 err = add_ignores(ignores, root_path, parent_path, -1,
3307 ".cvsignore");
3308 if (err)
3309 break;
3310 err = add_ignores(ignores, root_path, parent_path, -1,
3311 ".gitignore");
3312 if (err)
3313 break;
3314 err = got_path_dirname(&next_parent_path, parent_path);
3315 if (err) {
3316 if (err->code == GOT_ERR_BAD_PATH)
3317 err = NULL; /* traversed everything */
3318 break;
3320 free(parent_path);
3321 parent_path = next_parent_path;
3322 next_parent_path = NULL;
3325 free(parent_path);
3326 free(next_parent_path);
3327 return err;
3330 static const struct got_error *
3331 worktree_status(struct got_worktree *worktree, const char *path,
3332 struct got_fileindex *fileindex, struct got_repository *repo,
3333 got_worktree_status_cb status_cb, void *status_arg,
3334 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3335 int report_unchanged)
3337 const struct got_error *err = NULL;
3338 int fd = -1;
3339 struct got_fileindex_diff_dir_cb fdiff_cb;
3340 struct diff_dir_cb_arg arg;
3341 char *ondisk_path = NULL;
3343 TAILQ_INIT(&arg.ignores);
3345 if (asprintf(&ondisk_path, "%s%s%s",
3346 worktree->root_path, path[0] ? "/" : "", path) == -1)
3347 return got_error_from_errno("asprintf");
3349 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3350 if (fd == -1) {
3351 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3352 errno != ELOOP)
3353 err = got_error_from_errno2("open", ondisk_path);
3354 else
3355 err = report_single_file_status(path, ondisk_path,
3356 fileindex, status_cb, status_arg, repo,
3357 report_unchanged);
3358 } else {
3359 fdiff_cb.diff_old_new = status_old_new;
3360 fdiff_cb.diff_old = status_old;
3361 fdiff_cb.diff_new = status_new;
3362 fdiff_cb.diff_traverse = status_traverse;
3363 arg.fileindex = fileindex;
3364 arg.worktree = worktree;
3365 arg.status_path = path;
3366 arg.status_path_len = strlen(path);
3367 arg.repo = repo;
3368 arg.status_cb = status_cb;
3369 arg.status_arg = status_arg;
3370 arg.cancel_cb = cancel_cb;
3371 arg.cancel_arg = cancel_arg;
3372 arg.report_unchanged = report_unchanged;
3373 arg.no_ignores = no_ignores;
3374 if (!no_ignores) {
3375 err = add_ignores_from_parent_paths(&arg.ignores,
3376 worktree->root_path, path);
3377 if (err)
3378 goto done;
3380 err = got_fileindex_diff_dir(fileindex, fd,
3381 worktree->root_path, path, repo, &fdiff_cb, &arg);
3383 done:
3384 free_ignores(&arg.ignores);
3385 if (fd != -1 && close(fd) != 0 && err == NULL)
3386 err = got_error_from_errno("close");
3387 free(ondisk_path);
3388 return err;
3391 const struct got_error *
3392 got_worktree_status(struct got_worktree *worktree,
3393 struct got_pathlist_head *paths, struct got_repository *repo,
3394 got_worktree_status_cb status_cb, void *status_arg,
3395 got_cancel_cb cancel_cb, void *cancel_arg)
3397 const struct got_error *err = NULL;
3398 char *fileindex_path = NULL;
3399 struct got_fileindex *fileindex = NULL;
3400 struct got_pathlist_entry *pe;
3402 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3403 if (err)
3404 return err;
3406 TAILQ_FOREACH(pe, paths, entry) {
3407 err = worktree_status(worktree, pe->path, fileindex, repo,
3408 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3409 if (err)
3410 break;
3412 free(fileindex_path);
3413 got_fileindex_free(fileindex);
3414 return err;
3417 const struct got_error *
3418 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3419 const char *arg)
3421 const struct got_error *err = NULL;
3422 char *resolved = NULL, *cwd = NULL, *path = NULL;
3423 size_t len;
3424 struct stat sb;
3426 *wt_path = NULL;
3428 cwd = getcwd(NULL, 0);
3429 if (cwd == NULL)
3430 return got_error_from_errno("getcwd");
3432 if (lstat(arg, &sb) == -1) {
3433 if (errno != ENOENT) {
3434 err = got_error_from_errno2("lstat", arg);
3435 goto done;
3438 if (S_ISLNK(sb.st_mode)) {
3440 * We cannot use realpath(3) with symlinks since we want to
3441 * operate on the symlink itself.
3442 * But we can make the path absolute, assuming it is relative
3443 * to the current working directory, and then canonicalize it.
3445 char *abspath = NULL;
3446 char canonpath[PATH_MAX];
3447 if (!got_path_is_absolute(arg)) {
3448 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3449 err = got_error_from_errno("asprintf");
3450 goto done;
3454 err = got_canonpath(abspath ? abspath : arg, canonpath,
3455 sizeof(canonpath));
3456 if (err)
3457 goto done;
3458 resolved = strdup(canonpath);
3459 if (resolved == NULL) {
3460 err = got_error_from_errno("strdup");
3461 goto done;
3463 } else {
3464 resolved = realpath(arg, NULL);
3465 if (resolved == NULL) {
3466 if (errno != ENOENT) {
3467 err = got_error_from_errno2("realpath", arg);
3468 goto done;
3470 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3471 err = got_error_from_errno("asprintf");
3472 goto done;
3477 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3478 strlen(got_worktree_get_root_path(worktree)))) {
3479 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3480 goto done;
3483 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3484 err = got_path_skip_common_ancestor(&path,
3485 got_worktree_get_root_path(worktree), resolved);
3486 if (err)
3487 goto done;
3488 } else {
3489 path = strdup("");
3490 if (path == NULL) {
3491 err = got_error_from_errno("strdup");
3492 goto done;
3496 /* XXX status walk can't deal with trailing slash! */
3497 len = strlen(path);
3498 while (len > 0 && path[len - 1] == '/') {
3499 path[len - 1] = '\0';
3500 len--;
3502 done:
3503 free(resolved);
3504 free(cwd);
3505 if (err == NULL)
3506 *wt_path = path;
3507 else
3508 free(path);
3509 return err;
3512 struct schedule_addition_args {
3513 struct got_worktree *worktree;
3514 struct got_fileindex *fileindex;
3515 got_worktree_checkout_cb progress_cb;
3516 void *progress_arg;
3517 struct got_repository *repo;
3520 static const struct got_error *
3521 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3522 const char *relpath, struct got_object_id *blob_id,
3523 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3524 int dirfd, const char *de_name)
3526 struct schedule_addition_args *a = arg;
3527 const struct got_error *err = NULL;
3528 struct got_fileindex_entry *ie;
3529 struct stat sb;
3530 char *ondisk_path;
3532 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3533 relpath) == -1)
3534 return got_error_from_errno("asprintf");
3536 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3537 if (ie) {
3538 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3539 de_name, a->repo);
3540 if (err)
3541 goto done;
3542 /* Re-adding an existing entry is a no-op. */
3543 if (status == GOT_STATUS_ADD)
3544 goto done;
3545 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3546 if (err)
3547 goto done;
3550 if (status != GOT_STATUS_UNVERSIONED) {
3551 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3552 goto done;
3555 err = got_fileindex_entry_alloc(&ie, relpath);
3556 if (err)
3557 goto done;
3558 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3559 if (err) {
3560 got_fileindex_entry_free(ie);
3561 goto done;
3563 err = got_fileindex_entry_add(a->fileindex, ie);
3564 if (err) {
3565 got_fileindex_entry_free(ie);
3566 goto done;
3568 done:
3569 free(ondisk_path);
3570 if (err)
3571 return err;
3572 if (status == GOT_STATUS_ADD)
3573 return NULL;
3574 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3577 const struct got_error *
3578 got_worktree_schedule_add(struct got_worktree *worktree,
3579 struct got_pathlist_head *paths,
3580 got_worktree_checkout_cb progress_cb, void *progress_arg,
3581 struct got_repository *repo, int no_ignores)
3583 struct got_fileindex *fileindex = NULL;
3584 char *fileindex_path = NULL;
3585 const struct got_error *err = NULL, *sync_err, *unlockerr;
3586 struct got_pathlist_entry *pe;
3587 struct schedule_addition_args saa;
3589 err = lock_worktree(worktree, LOCK_EX);
3590 if (err)
3591 return err;
3593 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3594 if (err)
3595 goto done;
3597 saa.worktree = worktree;
3598 saa.fileindex = fileindex;
3599 saa.progress_cb = progress_cb;
3600 saa.progress_arg = progress_arg;
3601 saa.repo = repo;
3603 TAILQ_FOREACH(pe, paths, entry) {
3604 err = worktree_status(worktree, pe->path, fileindex, repo,
3605 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3606 if (err)
3607 break;
3609 sync_err = sync_fileindex(fileindex, fileindex_path);
3610 if (sync_err && err == NULL)
3611 err = sync_err;
3612 done:
3613 free(fileindex_path);
3614 if (fileindex)
3615 got_fileindex_free(fileindex);
3616 unlockerr = lock_worktree(worktree, LOCK_SH);
3617 if (unlockerr && err == NULL)
3618 err = unlockerr;
3619 return err;
3622 struct schedule_deletion_args {
3623 struct got_worktree *worktree;
3624 struct got_fileindex *fileindex;
3625 got_worktree_delete_cb progress_cb;
3626 void *progress_arg;
3627 struct got_repository *repo;
3628 int delete_local_mods;
3629 int keep_on_disk;
3632 static const struct got_error *
3633 schedule_for_deletion(void *arg, unsigned char status,
3634 unsigned char staged_status, const char *relpath,
3635 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3636 struct got_object_id *commit_id, int dirfd, const char *de_name)
3638 struct schedule_deletion_args *a = arg;
3639 const struct got_error *err = NULL;
3640 struct got_fileindex_entry *ie = NULL;
3641 struct stat sb;
3642 char *ondisk_path, *parent = NULL;
3644 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3645 if (ie == NULL)
3646 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3648 staged_status = get_staged_status(ie);
3649 if (staged_status != GOT_STATUS_NO_CHANGE) {
3650 if (staged_status == GOT_STATUS_DELETE)
3651 return NULL;
3652 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3655 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3656 relpath) == -1)
3657 return got_error_from_errno("asprintf");
3659 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3660 a->repo);
3661 if (err)
3662 goto done;
3664 if (status != GOT_STATUS_NO_CHANGE) {
3665 if (status == GOT_STATUS_DELETE)
3666 goto done;
3667 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3668 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3669 goto done;
3671 if (status != GOT_STATUS_MODIFY &&
3672 status != GOT_STATUS_MISSING) {
3673 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3674 goto done;
3678 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3679 if (dirfd != -1) {
3680 if (unlinkat(dirfd, de_name, 0) != 0) {
3681 err = got_error_from_errno2("unlinkat",
3682 ondisk_path);
3683 goto done;
3685 } else if (unlink(ondisk_path) != 0) {
3686 err = got_error_from_errno2("unlink", ondisk_path);
3687 goto done;
3690 parent = dirname(ondisk_path);
3692 if (parent == NULL) {
3693 err = got_error_from_errno2("dirname", ondisk_path);
3694 goto done;
3696 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3697 if (rmdir(parent) == -1) {
3698 if (errno != ENOTEMPTY)
3699 err = got_error_from_errno2("rmdir",
3700 parent);
3701 break;
3703 parent = dirname(parent);
3704 if (parent == NULL) {
3705 err = got_error_from_errno2("dirname", parent);
3706 goto done;
3711 got_fileindex_entry_mark_deleted_from_disk(ie);
3712 done:
3713 free(ondisk_path);
3714 if (err)
3715 return err;
3716 if (status == GOT_STATUS_DELETE)
3717 return NULL;
3718 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3719 staged_status, relpath);
3722 const struct got_error *
3723 got_worktree_schedule_delete(struct got_worktree *worktree,
3724 struct got_pathlist_head *paths, int delete_local_mods,
3725 got_worktree_delete_cb progress_cb, void *progress_arg,
3726 struct got_repository *repo, int keep_on_disk)
3728 struct got_fileindex *fileindex = NULL;
3729 char *fileindex_path = NULL;
3730 const struct got_error *err = NULL, *sync_err, *unlockerr;
3731 struct got_pathlist_entry *pe;
3732 struct schedule_deletion_args sda;
3734 err = lock_worktree(worktree, LOCK_EX);
3735 if (err)
3736 return err;
3738 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3739 if (err)
3740 goto done;
3742 sda.worktree = worktree;
3743 sda.fileindex = fileindex;
3744 sda.progress_cb = progress_cb;
3745 sda.progress_arg = progress_arg;
3746 sda.repo = repo;
3747 sda.delete_local_mods = delete_local_mods;
3748 sda.keep_on_disk = keep_on_disk;
3750 TAILQ_FOREACH(pe, paths, entry) {
3751 err = worktree_status(worktree, pe->path, fileindex, repo,
3752 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3753 if (err)
3754 break;
3756 sync_err = sync_fileindex(fileindex, fileindex_path);
3757 if (sync_err && err == NULL)
3758 err = sync_err;
3759 done:
3760 free(fileindex_path);
3761 if (fileindex)
3762 got_fileindex_free(fileindex);
3763 unlockerr = lock_worktree(worktree, LOCK_SH);
3764 if (unlockerr && err == NULL)
3765 err = unlockerr;
3766 return err;
3769 static const struct got_error *
3770 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3772 const struct got_error *err = NULL;
3773 char *line = NULL;
3774 size_t linesize = 0, n;
3775 ssize_t linelen;
3777 linelen = getline(&line, &linesize, infile);
3778 if (linelen == -1) {
3779 if (ferror(infile)) {
3780 err = got_error_from_errno("getline");
3781 goto done;
3783 return NULL;
3785 if (outfile) {
3786 n = fwrite(line, 1, linelen, outfile);
3787 if (n != linelen) {
3788 err = got_ferror(outfile, GOT_ERR_IO);
3789 goto done;
3792 if (rejectfile) {
3793 n = fwrite(line, 1, linelen, rejectfile);
3794 if (n != linelen)
3795 err = got_ferror(outfile, GOT_ERR_IO);
3797 done:
3798 free(line);
3799 return err;
3802 static const struct got_error *
3803 skip_one_line(FILE *f)
3805 char *line = NULL;
3806 size_t linesize = 0;
3807 ssize_t linelen;
3809 linelen = getline(&line, &linesize, f);
3810 if (linelen == -1) {
3811 if (ferror(f))
3812 return got_error_from_errno("getline");
3813 return NULL;
3815 free(line);
3816 return NULL;
3819 static const struct got_error *
3820 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3821 int start_old, int end_old, int start_new, int end_new,
3822 FILE *outfile, FILE *rejectfile)
3824 const struct got_error *err;
3826 /* Copy old file's lines leading up to patch. */
3827 while (!feof(f1) && *line_cur1 < start_old) {
3828 err = copy_one_line(f1, outfile, NULL);
3829 if (err)
3830 return err;
3831 (*line_cur1)++;
3833 /* Skip new file's lines leading up to patch. */
3834 while (!feof(f2) && *line_cur2 < start_new) {
3835 if (rejectfile)
3836 err = copy_one_line(f2, NULL, rejectfile);
3837 else
3838 err = skip_one_line(f2);
3839 if (err)
3840 return err;
3841 (*line_cur2)++;
3843 /* Copy patched lines. */
3844 while (!feof(f2) && *line_cur2 <= end_new) {
3845 err = copy_one_line(f2, outfile, NULL);
3846 if (err)
3847 return err;
3848 (*line_cur2)++;
3850 /* Skip over old file's replaced lines. */
3851 while (!feof(f1) && *line_cur1 <= end_old) {
3852 if (rejectfile)
3853 err = copy_one_line(f1, NULL, rejectfile);
3854 else
3855 err = skip_one_line(f1);
3856 if (err)
3857 return err;
3858 (*line_cur1)++;
3861 return NULL;
3864 static const struct got_error *
3865 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3866 FILE *outfile, FILE *rejectfile)
3868 const struct got_error *err;
3870 if (outfile) {
3871 /* Copy old file's lines until EOF. */
3872 while (!feof(f1)) {
3873 err = copy_one_line(f1, outfile, NULL);
3874 if (err)
3875 return err;
3876 (*line_cur1)++;
3879 if (rejectfile) {
3880 /* Copy new file's lines until EOF. */
3881 while (!feof(f2)) {
3882 err = copy_one_line(f2, NULL, rejectfile);
3883 if (err)
3884 return err;
3885 (*line_cur2)++;
3889 return NULL;
3892 static const struct got_error *
3893 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3894 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3895 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3896 int *line_cur2, FILE *outfile, FILE *rejectfile,
3897 got_worktree_patch_cb patch_cb, void *patch_arg)
3899 const struct got_error *err = NULL;
3900 int start_old = change->cv.a;
3901 int end_old = change->cv.b;
3902 int start_new = change->cv.c;
3903 int end_new = change->cv.d;
3904 long pos1, pos2;
3905 FILE *hunkfile;
3907 *choice = GOT_PATCH_CHOICE_NONE;
3909 hunkfile = got_opentemp();
3910 if (hunkfile == NULL)
3911 return got_error_from_errno("got_opentemp");
3913 pos1 = ftell(f1);
3914 pos2 = ftell(f2);
3916 /* XXX TODO needs error checking */
3917 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3919 if (fseek(f1, pos1, SEEK_SET) == -1) {
3920 err = got_ferror(f1, GOT_ERR_IO);
3921 goto done;
3923 if (fseek(f2, pos2, SEEK_SET) == -1) {
3924 err = got_ferror(f1, GOT_ERR_IO);
3925 goto done;
3927 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3928 err = got_ferror(hunkfile, GOT_ERR_IO);
3929 goto done;
3932 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3933 hunkfile, n, nchanges);
3934 if (err)
3935 goto done;
3937 switch (*choice) {
3938 case GOT_PATCH_CHOICE_YES:
3939 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3940 end_old, start_new, end_new, outfile, rejectfile);
3941 break;
3942 case GOT_PATCH_CHOICE_NO:
3943 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3944 end_old, start_new, end_new, rejectfile, outfile);
3945 break;
3946 case GOT_PATCH_CHOICE_QUIT:
3947 break;
3948 default:
3949 err = got_error(GOT_ERR_PATCH_CHOICE);
3950 break;
3952 done:
3953 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3954 err = got_error_from_errno("fclose");
3955 return err;
3958 struct revert_file_args {
3959 struct got_worktree *worktree;
3960 struct got_fileindex *fileindex;
3961 got_worktree_checkout_cb progress_cb;
3962 void *progress_arg;
3963 got_worktree_patch_cb patch_cb;
3964 void *patch_arg;
3965 struct got_repository *repo;
3968 static const struct got_error *
3969 create_patched_content(char **path_outfile, int reverse_patch,
3970 struct got_object_id *blob_id, const char *path2,
3971 int dirfd2, const char *de_name2,
3972 const char *relpath, struct got_repository *repo,
3973 got_worktree_patch_cb patch_cb, void *patch_arg)
3975 const struct got_error *err;
3976 struct got_blob_object *blob = NULL;
3977 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3978 int fd2 = -1;
3979 char *path1 = NULL, *id_str = NULL;
3980 struct stat sb1, sb2;
3981 struct got_diff_changes *changes = NULL;
3982 struct got_diff_state *ds = NULL;
3983 struct got_diff_args *args = NULL;
3984 struct got_diff_change *change;
3985 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3986 int n = 0;
3988 *path_outfile = NULL;
3990 err = got_object_id_str(&id_str, blob_id);
3991 if (err)
3992 return err;
3994 if (dirfd2 != -1) {
3995 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3996 if (fd2 == -1) {
3997 err = got_error_from_errno2("openat", path2);
3998 goto done;
4000 } else {
4001 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4002 if (fd2 == -1) {
4003 err = got_error_from_errno2("open", path2);
4004 goto done;
4007 if (fstat(fd2, &sb2) == -1) {
4008 err = got_error_from_errno2("fstat", path2);
4009 goto done;
4012 f2 = fdopen(fd2, "r");
4013 if (f2 == NULL) {
4014 err = got_error_from_errno2("fdopen", path2);
4015 goto done;
4017 fd2 = -1;
4019 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4020 if (err)
4021 goto done;
4023 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4024 if (err)
4025 goto done;
4027 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4028 if (err)
4029 goto done;
4031 if (stat(path1, &sb1) == -1) {
4032 err = got_error_from_errno2("stat", path1);
4033 goto done;
4036 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4037 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4038 if (err)
4039 goto done;
4041 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4042 if (err)
4043 goto done;
4045 if (fseek(f1, 0L, SEEK_SET) == -1)
4046 return got_ferror(f1, GOT_ERR_IO);
4047 if (fseek(f2, 0L, SEEK_SET) == -1)
4048 return got_ferror(f2, GOT_ERR_IO);
4049 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4050 int choice;
4051 err = apply_or_reject_change(&choice, change, ++n,
4052 changes->nchanges, ds, args, diff_flags, relpath,
4053 f1, f2, &line_cur1, &line_cur2,
4054 reverse_patch ? NULL : outfile,
4055 reverse_patch ? outfile : NULL,
4056 patch_cb, patch_arg);
4057 if (err)
4058 goto done;
4059 if (choice == GOT_PATCH_CHOICE_YES)
4060 have_content = 1;
4061 else if (choice == GOT_PATCH_CHOICE_QUIT)
4062 break;
4064 if (have_content) {
4065 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4066 reverse_patch ? NULL : outfile,
4067 reverse_patch ? outfile : NULL);
4068 if (err)
4069 goto done;
4071 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4072 err = got_error_from_errno2("chmod", path2);
4073 goto done;
4076 done:
4077 free(id_str);
4078 if (blob)
4079 got_object_blob_close(blob);
4080 if (f1 && fclose(f1) == EOF && err == NULL)
4081 err = got_error_from_errno2("fclose", path1);
4082 if (f2 && fclose(f2) == EOF && err == NULL)
4083 err = got_error_from_errno2("fclose", path2);
4084 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4085 err = got_error_from_errno2("close", path2);
4086 if (outfile && fclose(outfile) == EOF && err == NULL)
4087 err = got_error_from_errno2("fclose", *path_outfile);
4088 if (path1 && unlink(path1) == -1 && err == NULL)
4089 err = got_error_from_errno2("unlink", path1);
4090 if (err || !have_content) {
4091 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4092 err = got_error_from_errno2("unlink", *path_outfile);
4093 free(*path_outfile);
4094 *path_outfile = NULL;
4096 free(args);
4097 if (ds) {
4098 got_diff_state_free(ds);
4099 free(ds);
4101 if (changes)
4102 got_diff_free_changes(changes);
4103 free(path1);
4104 return err;
4107 static const struct got_error *
4108 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4109 const char *relpath, struct got_object_id *blob_id,
4110 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4111 int dirfd, const char *de_name)
4113 struct revert_file_args *a = arg;
4114 const struct got_error *err = NULL;
4115 char *parent_path = NULL;
4116 struct got_fileindex_entry *ie;
4117 struct got_tree_object *tree = NULL;
4118 struct got_object_id *tree_id = NULL;
4119 const struct got_tree_entry *te = NULL;
4120 char *tree_path = NULL, *te_name;
4121 char *ondisk_path = NULL, *path_content = NULL;
4122 struct got_blob_object *blob = NULL;
4124 /* Reverting a staged deletion is a no-op. */
4125 if (status == GOT_STATUS_DELETE &&
4126 staged_status != GOT_STATUS_NO_CHANGE)
4127 return NULL;
4129 if (status == GOT_STATUS_UNVERSIONED)
4130 return (*a->progress_cb)(a->progress_arg,
4131 GOT_STATUS_UNVERSIONED, relpath);
4133 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4134 if (ie == NULL)
4135 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4137 /* Construct in-repository path of tree which contains this blob. */
4138 err = got_path_dirname(&parent_path, ie->path);
4139 if (err) {
4140 if (err->code != GOT_ERR_BAD_PATH)
4141 goto done;
4142 parent_path = strdup("/");
4143 if (parent_path == NULL) {
4144 err = got_error_from_errno("strdup");
4145 goto done;
4148 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4149 tree_path = strdup(parent_path);
4150 if (tree_path == NULL) {
4151 err = got_error_from_errno("strdup");
4152 goto done;
4154 } else {
4155 if (got_path_is_root_dir(parent_path)) {
4156 tree_path = strdup(a->worktree->path_prefix);
4157 if (tree_path == NULL) {
4158 err = got_error_from_errno("strdup");
4159 goto done;
4161 } else {
4162 if (asprintf(&tree_path, "%s/%s",
4163 a->worktree->path_prefix, parent_path) == -1) {
4164 err = got_error_from_errno("asprintf");
4165 goto done;
4170 err = got_object_id_by_path(&tree_id, a->repo,
4171 a->worktree->base_commit_id, tree_path);
4172 if (err) {
4173 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4174 (status == GOT_STATUS_ADD ||
4175 staged_status == GOT_STATUS_ADD)))
4176 goto done;
4177 } else {
4178 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4179 if (err)
4180 goto done;
4182 te_name = basename(ie->path);
4183 if (te_name == NULL) {
4184 err = got_error_from_errno2("basename", ie->path);
4185 goto done;
4188 te = got_object_tree_find_entry(tree, te_name);
4189 if (te == NULL && status != GOT_STATUS_ADD &&
4190 staged_status != GOT_STATUS_ADD) {
4191 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4192 goto done;
4196 switch (status) {
4197 case GOT_STATUS_ADD:
4198 if (a->patch_cb) {
4199 int choice = GOT_PATCH_CHOICE_NONE;
4200 err = (*a->patch_cb)(&choice, a->patch_arg,
4201 status, ie->path, NULL, 1, 1);
4202 if (err)
4203 goto done;
4204 if (choice != GOT_PATCH_CHOICE_YES)
4205 break;
4207 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4208 ie->path);
4209 if (err)
4210 goto done;
4211 got_fileindex_entry_remove(a->fileindex, ie);
4212 break;
4213 case GOT_STATUS_DELETE:
4214 if (a->patch_cb) {
4215 int choice = GOT_PATCH_CHOICE_NONE;
4216 err = (*a->patch_cb)(&choice, a->patch_arg,
4217 status, ie->path, NULL, 1, 1);
4218 if (err)
4219 goto done;
4220 if (choice != GOT_PATCH_CHOICE_YES)
4221 break;
4223 /* fall through */
4224 case GOT_STATUS_MODIFY:
4225 case GOT_STATUS_MODE_CHANGE:
4226 case GOT_STATUS_CONFLICT:
4227 case GOT_STATUS_MISSING: {
4228 struct got_object_id id;
4229 if (staged_status == GOT_STATUS_ADD ||
4230 staged_status == GOT_STATUS_MODIFY) {
4231 memcpy(id.sha1, ie->staged_blob_sha1,
4232 SHA1_DIGEST_LENGTH);
4233 } else
4234 memcpy(id.sha1, ie->blob_sha1,
4235 SHA1_DIGEST_LENGTH);
4236 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4237 if (err)
4238 goto done;
4240 if (asprintf(&ondisk_path, "%s/%s",
4241 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4242 err = got_error_from_errno("asprintf");
4243 goto done;
4246 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4247 status == GOT_STATUS_CONFLICT)) {
4248 err = create_patched_content(&path_content, 1, &id,
4249 ondisk_path, dirfd, de_name, ie->path, a->repo,
4250 a->patch_cb, a->patch_arg);
4251 if (err || path_content == NULL)
4252 break;
4253 if (rename(path_content, ondisk_path) == -1) {
4254 err = got_error_from_errno3("rename",
4255 path_content, ondisk_path);
4256 goto done;
4258 } else {
4259 err = install_blob(a->worktree, ondisk_path, ie->path,
4260 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4261 got_fileindex_perms_to_st(ie), blob, 0, 1, 0,
4262 a->repo, a->progress_cb, a->progress_arg);
4263 if (err)
4264 goto done;
4265 if (status == GOT_STATUS_DELETE ||
4266 status == GOT_STATUS_MODE_CHANGE) {
4267 err = got_fileindex_entry_update(ie,
4268 ondisk_path, blob->id.sha1,
4269 a->worktree->base_commit_id->sha1, 1);
4270 if (err)
4271 goto done;
4274 break;
4276 default:
4277 break;
4279 done:
4280 free(ondisk_path);
4281 free(path_content);
4282 free(parent_path);
4283 free(tree_path);
4284 if (blob)
4285 got_object_blob_close(blob);
4286 if (tree)
4287 got_object_tree_close(tree);
4288 free(tree_id);
4289 return err;
4292 const struct got_error *
4293 got_worktree_revert(struct got_worktree *worktree,
4294 struct got_pathlist_head *paths,
4295 got_worktree_checkout_cb progress_cb, void *progress_arg,
4296 got_worktree_patch_cb patch_cb, void *patch_arg,
4297 struct got_repository *repo)
4299 struct got_fileindex *fileindex = NULL;
4300 char *fileindex_path = NULL;
4301 const struct got_error *err = NULL, *unlockerr = NULL;
4302 const struct got_error *sync_err = NULL;
4303 struct got_pathlist_entry *pe;
4304 struct revert_file_args rfa;
4306 err = lock_worktree(worktree, LOCK_EX);
4307 if (err)
4308 return err;
4310 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4311 if (err)
4312 goto done;
4314 rfa.worktree = worktree;
4315 rfa.fileindex = fileindex;
4316 rfa.progress_cb = progress_cb;
4317 rfa.progress_arg = progress_arg;
4318 rfa.patch_cb = patch_cb;
4319 rfa.patch_arg = patch_arg;
4320 rfa.repo = repo;
4321 TAILQ_FOREACH(pe, paths, entry) {
4322 err = worktree_status(worktree, pe->path, fileindex, repo,
4323 revert_file, &rfa, NULL, NULL, 0, 0);
4324 if (err)
4325 break;
4327 sync_err = sync_fileindex(fileindex, fileindex_path);
4328 if (sync_err && err == NULL)
4329 err = sync_err;
4330 done:
4331 free(fileindex_path);
4332 if (fileindex)
4333 got_fileindex_free(fileindex);
4334 unlockerr = lock_worktree(worktree, LOCK_SH);
4335 if (unlockerr && err == NULL)
4336 err = unlockerr;
4337 return err;
4340 static void
4341 free_commitable(struct got_commitable *ct)
4343 free(ct->path);
4344 free(ct->in_repo_path);
4345 free(ct->ondisk_path);
4346 free(ct->blob_id);
4347 free(ct->base_blob_id);
4348 free(ct->staged_blob_id);
4349 free(ct->base_commit_id);
4350 free(ct);
4353 struct collect_commitables_arg {
4354 struct got_pathlist_head *commitable_paths;
4355 struct got_repository *repo;
4356 struct got_worktree *worktree;
4357 int have_staged_files;
4360 static const struct got_error *
4361 collect_commitables(void *arg, unsigned char status,
4362 unsigned char staged_status, const char *relpath,
4363 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4364 struct got_object_id *commit_id, int dirfd, const char *de_name)
4366 struct collect_commitables_arg *a = arg;
4367 const struct got_error *err = NULL;
4368 struct got_commitable *ct = NULL;
4369 struct got_pathlist_entry *new = NULL;
4370 char *parent_path = NULL, *path = NULL;
4371 struct stat sb;
4373 if (a->have_staged_files) {
4374 if (staged_status != GOT_STATUS_MODIFY &&
4375 staged_status != GOT_STATUS_ADD &&
4376 staged_status != GOT_STATUS_DELETE)
4377 return NULL;
4378 } else {
4379 if (status == GOT_STATUS_CONFLICT)
4380 return got_error(GOT_ERR_COMMIT_CONFLICT);
4382 if (status != GOT_STATUS_MODIFY &&
4383 status != GOT_STATUS_MODE_CHANGE &&
4384 status != GOT_STATUS_ADD &&
4385 status != GOT_STATUS_DELETE)
4386 return NULL;
4389 if (asprintf(&path, "/%s", relpath) == -1) {
4390 err = got_error_from_errno("asprintf");
4391 goto done;
4393 if (strcmp(path, "/") == 0) {
4394 parent_path = strdup("");
4395 if (parent_path == NULL)
4396 return got_error_from_errno("strdup");
4397 } else {
4398 err = got_path_dirname(&parent_path, path);
4399 if (err)
4400 return err;
4403 ct = calloc(1, sizeof(*ct));
4404 if (ct == NULL) {
4405 err = got_error_from_errno("calloc");
4406 goto done;
4409 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4410 relpath) == -1) {
4411 err = got_error_from_errno("asprintf");
4412 goto done;
4414 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4415 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4416 } else {
4417 if (dirfd != -1) {
4418 if (fstatat(dirfd, de_name, &sb,
4419 AT_SYMLINK_NOFOLLOW) == -1) {
4420 err = got_error_from_errno2("fstatat",
4421 ct->ondisk_path);
4422 goto done;
4424 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4425 err = got_error_from_errno2("lstat", ct->ondisk_path);
4426 goto done;
4428 ct->mode = sb.st_mode;
4431 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4432 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4433 relpath) == -1) {
4434 err = got_error_from_errno("asprintf");
4435 goto done;
4438 ct->status = status;
4439 ct->staged_status = staged_status;
4440 ct->blob_id = NULL; /* will be filled in when blob gets created */
4441 if (ct->status != GOT_STATUS_ADD &&
4442 ct->staged_status != GOT_STATUS_ADD) {
4443 ct->base_blob_id = got_object_id_dup(blob_id);
4444 if (ct->base_blob_id == NULL) {
4445 err = got_error_from_errno("got_object_id_dup");
4446 goto done;
4448 ct->base_commit_id = got_object_id_dup(commit_id);
4449 if (ct->base_commit_id == NULL) {
4450 err = got_error_from_errno("got_object_id_dup");
4451 goto done;
4454 if (ct->staged_status == GOT_STATUS_ADD ||
4455 ct->staged_status == GOT_STATUS_MODIFY) {
4456 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4457 if (ct->staged_blob_id == NULL) {
4458 err = got_error_from_errno("got_object_id_dup");
4459 goto done;
4462 ct->path = strdup(path);
4463 if (ct->path == NULL) {
4464 err = got_error_from_errno("strdup");
4465 goto done;
4467 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4468 done:
4469 if (ct && (err || new == NULL))
4470 free_commitable(ct);
4471 free(parent_path);
4472 free(path);
4473 return err;
4476 static const struct got_error *write_tree(struct got_object_id **, int *,
4477 struct got_tree_object *, const char *, struct got_pathlist_head *,
4478 got_worktree_status_cb status_cb, void *status_arg,
4479 struct got_repository *);
4481 static const struct got_error *
4482 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4483 struct got_tree_entry *te, const char *parent_path,
4484 struct got_pathlist_head *commitable_paths,
4485 got_worktree_status_cb status_cb, void *status_arg,
4486 struct got_repository *repo)
4488 const struct got_error *err = NULL;
4489 struct got_tree_object *subtree;
4490 char *subpath;
4492 if (asprintf(&subpath, "%s%s%s", parent_path,
4493 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4494 return got_error_from_errno("asprintf");
4496 err = got_object_open_as_tree(&subtree, repo, &te->id);
4497 if (err)
4498 return err;
4500 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4501 commitable_paths, status_cb, status_arg, repo);
4502 got_object_tree_close(subtree);
4503 free(subpath);
4504 return err;
4507 static const struct got_error *
4508 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4510 const struct got_error *err = NULL;
4511 char *ct_parent_path = NULL;
4513 *match = 0;
4515 if (strchr(ct->in_repo_path, '/') == NULL) {
4516 *match = got_path_is_root_dir(path);
4517 return NULL;
4520 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4521 if (err)
4522 return err;
4523 *match = (strcmp(path, ct_parent_path) == 0);
4524 free(ct_parent_path);
4525 return err;
4528 static mode_t
4529 get_ct_file_mode(struct got_commitable *ct)
4531 if (S_ISLNK(ct->mode))
4532 return S_IFLNK;
4534 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4537 static const struct got_error *
4538 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4539 struct got_tree_entry *te, struct got_commitable *ct)
4541 const struct got_error *err = NULL;
4543 *new_te = NULL;
4545 err = got_object_tree_entry_dup(new_te, te);
4546 if (err)
4547 goto done;
4549 (*new_te)->mode = get_ct_file_mode(ct);
4551 if (ct->staged_status == GOT_STATUS_MODIFY)
4552 memcpy(&(*new_te)->id, ct->staged_blob_id,
4553 sizeof((*new_te)->id));
4554 else
4555 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4556 done:
4557 if (err && *new_te) {
4558 free(*new_te);
4559 *new_te = NULL;
4561 return err;
4564 static const struct got_error *
4565 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4566 struct got_commitable *ct)
4568 const struct got_error *err = NULL;
4569 char *ct_name;
4571 *new_te = NULL;
4573 *new_te = calloc(1, sizeof(**new_te));
4574 if (*new_te == NULL)
4575 return got_error_from_errno("calloc");
4577 ct_name = basename(ct->path);
4578 if (ct_name == NULL) {
4579 err = got_error_from_errno2("basename", ct->path);
4580 goto done;
4582 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4583 sizeof((*new_te)->name)) {
4584 err = got_error(GOT_ERR_NO_SPACE);
4585 goto done;
4588 (*new_te)->mode = get_ct_file_mode(ct);
4590 if (ct->staged_status == GOT_STATUS_ADD)
4591 memcpy(&(*new_te)->id, ct->staged_blob_id,
4592 sizeof((*new_te)->id));
4593 else
4594 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4595 done:
4596 if (err && *new_te) {
4597 free(*new_te);
4598 *new_te = NULL;
4600 return err;
4603 static const struct got_error *
4604 insert_tree_entry(struct got_tree_entry *new_te,
4605 struct got_pathlist_head *paths)
4607 const struct got_error *err = NULL;
4608 struct got_pathlist_entry *new_pe;
4610 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4611 if (err)
4612 return err;
4613 if (new_pe == NULL)
4614 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4615 return NULL;
4618 static const struct got_error *
4619 report_ct_status(struct got_commitable *ct,
4620 got_worktree_status_cb status_cb, void *status_arg)
4622 const char *ct_path = ct->path;
4623 unsigned char status;
4625 while (ct_path[0] == '/')
4626 ct_path++;
4628 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4629 status = ct->staged_status;
4630 else
4631 status = ct->status;
4633 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4634 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4637 static const struct got_error *
4638 match_modified_subtree(int *modified, struct got_tree_entry *te,
4639 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4641 const struct got_error *err = NULL;
4642 struct got_pathlist_entry *pe;
4643 char *te_path;
4645 *modified = 0;
4647 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4648 got_path_is_root_dir(base_tree_path) ? "" : "/",
4649 te->name) == -1)
4650 return got_error_from_errno("asprintf");
4652 TAILQ_FOREACH(pe, commitable_paths, entry) {
4653 struct got_commitable *ct = pe->data;
4654 *modified = got_path_is_child(ct->in_repo_path, te_path,
4655 strlen(te_path));
4656 if (*modified)
4657 break;
4660 free(te_path);
4661 return err;
4664 static const struct got_error *
4665 match_deleted_or_modified_ct(struct got_commitable **ctp,
4666 struct got_tree_entry *te, const char *base_tree_path,
4667 struct got_pathlist_head *commitable_paths)
4669 const struct got_error *err = NULL;
4670 struct got_pathlist_entry *pe;
4672 *ctp = NULL;
4674 TAILQ_FOREACH(pe, commitable_paths, entry) {
4675 struct got_commitable *ct = pe->data;
4676 char *ct_name = NULL;
4677 int path_matches;
4679 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4680 if (ct->status != GOT_STATUS_MODIFY &&
4681 ct->status != GOT_STATUS_MODE_CHANGE &&
4682 ct->status != GOT_STATUS_DELETE)
4683 continue;
4684 } else {
4685 if (ct->staged_status != GOT_STATUS_MODIFY &&
4686 ct->staged_status != GOT_STATUS_DELETE)
4687 continue;
4690 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4691 continue;
4693 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4694 if (err)
4695 return err;
4696 if (!path_matches)
4697 continue;
4699 ct_name = basename(pe->path);
4700 if (ct_name == NULL)
4701 return got_error_from_errno2("basename", pe->path);
4703 if (strcmp(te->name, ct_name) != 0)
4704 continue;
4706 *ctp = ct;
4707 break;
4710 return err;
4713 static const struct got_error *
4714 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4715 const char *child_path, const char *path_base_tree,
4716 struct got_pathlist_head *commitable_paths,
4717 got_worktree_status_cb status_cb, void *status_arg,
4718 struct got_repository *repo)
4720 const struct got_error *err = NULL;
4721 struct got_tree_entry *new_te;
4722 char *subtree_path;
4723 struct got_object_id *id = NULL;
4724 int nentries;
4726 *new_tep = NULL;
4728 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4729 got_path_is_root_dir(path_base_tree) ? "" : "/",
4730 child_path) == -1)
4731 return got_error_from_errno("asprintf");
4733 new_te = calloc(1, sizeof(*new_te));
4734 if (new_te == NULL)
4735 return got_error_from_errno("calloc");
4736 new_te->mode = S_IFDIR;
4738 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4739 sizeof(new_te->name)) {
4740 err = got_error(GOT_ERR_NO_SPACE);
4741 goto done;
4743 err = write_tree(&id, &nentries, NULL, subtree_path,
4744 commitable_paths, status_cb, status_arg, repo);
4745 if (err) {
4746 free(new_te);
4747 goto done;
4749 memcpy(&new_te->id, id, sizeof(new_te->id));
4750 done:
4751 free(id);
4752 free(subtree_path);
4753 if (err == NULL)
4754 *new_tep = new_te;
4755 return err;
4758 static const struct got_error *
4759 write_tree(struct got_object_id **new_tree_id, int *nentries,
4760 struct got_tree_object *base_tree, const char *path_base_tree,
4761 struct got_pathlist_head *commitable_paths,
4762 got_worktree_status_cb status_cb, void *status_arg,
4763 struct got_repository *repo)
4765 const struct got_error *err = NULL;
4766 struct got_pathlist_head paths;
4767 struct got_tree_entry *te, *new_te = NULL;
4768 struct got_pathlist_entry *pe;
4770 TAILQ_INIT(&paths);
4771 *nentries = 0;
4773 /* Insert, and recurse into, newly added entries first. */
4774 TAILQ_FOREACH(pe, commitable_paths, entry) {
4775 struct got_commitable *ct = pe->data;
4776 char *child_path = NULL, *slash;
4778 if ((ct->status != GOT_STATUS_ADD &&
4779 ct->staged_status != GOT_STATUS_ADD) ||
4780 (ct->flags & GOT_COMMITABLE_ADDED))
4781 continue;
4783 if (!got_path_is_child(pe->path, path_base_tree,
4784 strlen(path_base_tree)))
4785 continue;
4787 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4788 pe->path);
4789 if (err)
4790 goto done;
4792 slash = strchr(child_path, '/');
4793 if (slash == NULL) {
4794 err = alloc_added_blob_tree_entry(&new_te, ct);
4795 if (err)
4796 goto done;
4797 err = report_ct_status(ct, status_cb, status_arg);
4798 if (err)
4799 goto done;
4800 ct->flags |= GOT_COMMITABLE_ADDED;
4801 err = insert_tree_entry(new_te, &paths);
4802 if (err)
4803 goto done;
4804 (*nentries)++;
4805 } else {
4806 *slash = '\0'; /* trim trailing path components */
4807 if (base_tree == NULL ||
4808 got_object_tree_find_entry(base_tree, child_path)
4809 == NULL) {
4810 err = make_subtree_for_added_blob(&new_te,
4811 child_path, path_base_tree,
4812 commitable_paths, status_cb, status_arg,
4813 repo);
4814 if (err)
4815 goto done;
4816 err = insert_tree_entry(new_te, &paths);
4817 if (err)
4818 goto done;
4819 (*nentries)++;
4824 if (base_tree) {
4825 int i, nbase_entries;
4826 /* Handle modified and deleted entries. */
4827 nbase_entries = got_object_tree_get_nentries(base_tree);
4828 for (i = 0; i < nbase_entries; i++) {
4829 struct got_commitable *ct = NULL;
4831 te = got_object_tree_get_entry(base_tree, i);
4832 if (got_object_tree_entry_is_submodule(te)) {
4833 /* Entry is a submodule; just copy it. */
4834 err = got_object_tree_entry_dup(&new_te, te);
4835 if (err)
4836 goto done;
4837 err = insert_tree_entry(new_te, &paths);
4838 if (err)
4839 goto done;
4840 (*nentries)++;
4841 continue;
4844 if (S_ISDIR(te->mode)) {
4845 int modified;
4846 err = got_object_tree_entry_dup(&new_te, te);
4847 if (err)
4848 goto done;
4849 err = match_modified_subtree(&modified, te,
4850 path_base_tree, commitable_paths);
4851 if (err)
4852 goto done;
4853 /* Avoid recursion into unmodified subtrees. */
4854 if (modified) {
4855 struct got_object_id *new_id;
4856 int nsubentries;
4857 err = write_subtree(&new_id,
4858 &nsubentries, te,
4859 path_base_tree, commitable_paths,
4860 status_cb, status_arg, repo);
4861 if (err)
4862 goto done;
4863 if (nsubentries == 0) {
4864 /* All entries were deleted. */
4865 free(new_id);
4866 continue;
4868 memcpy(&new_te->id, new_id,
4869 sizeof(new_te->id));
4870 free(new_id);
4872 err = insert_tree_entry(new_te, &paths);
4873 if (err)
4874 goto done;
4875 (*nentries)++;
4876 continue;
4879 err = match_deleted_or_modified_ct(&ct, te,
4880 path_base_tree, commitable_paths);
4881 if (err)
4882 goto done;
4883 if (ct) {
4884 /* NB: Deleted entries get dropped here. */
4885 if (ct->status == GOT_STATUS_MODIFY ||
4886 ct->status == GOT_STATUS_MODE_CHANGE ||
4887 ct->staged_status == GOT_STATUS_MODIFY) {
4888 err = alloc_modified_blob_tree_entry(
4889 &new_te, te, ct);
4890 if (err)
4891 goto done;
4892 err = insert_tree_entry(new_te, &paths);
4893 if (err)
4894 goto done;
4895 (*nentries)++;
4897 err = report_ct_status(ct, status_cb,
4898 status_arg);
4899 if (err)
4900 goto done;
4901 } else {
4902 /* Entry is unchanged; just copy it. */
4903 err = got_object_tree_entry_dup(&new_te, te);
4904 if (err)
4905 goto done;
4906 err = insert_tree_entry(new_te, &paths);
4907 if (err)
4908 goto done;
4909 (*nentries)++;
4914 /* Write new list of entries; deleted entries have been dropped. */
4915 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4916 done:
4917 got_pathlist_free(&paths);
4918 return err;
4921 static const struct got_error *
4922 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4923 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4924 int have_staged_files)
4926 const struct got_error *err = NULL;
4927 struct got_pathlist_entry *pe;
4929 TAILQ_FOREACH(pe, commitable_paths, entry) {
4930 struct got_fileindex_entry *ie;
4931 struct got_commitable *ct = pe->data;
4933 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4934 if (ie) {
4935 if (ct->status == GOT_STATUS_DELETE ||
4936 ct->staged_status == GOT_STATUS_DELETE) {
4937 got_fileindex_entry_remove(fileindex, ie);
4938 } else if (ct->staged_status == GOT_STATUS_ADD ||
4939 ct->staged_status == GOT_STATUS_MODIFY) {
4940 got_fileindex_entry_stage_set(ie,
4941 GOT_FILEIDX_STAGE_NONE);
4942 err = got_fileindex_entry_update(ie,
4943 ct->ondisk_path, ct->staged_blob_id->sha1,
4944 new_base_commit_id->sha1,
4945 !have_staged_files);
4946 } else
4947 err = got_fileindex_entry_update(ie,
4948 ct->ondisk_path, ct->blob_id->sha1,
4949 new_base_commit_id->sha1,
4950 !have_staged_files);
4951 } else {
4952 err = got_fileindex_entry_alloc(&ie, pe->path);
4953 if (err)
4954 break;
4955 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4956 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4957 if (err) {
4958 got_fileindex_entry_free(ie);
4959 break;
4961 err = got_fileindex_entry_add(fileindex, ie);
4962 if (err) {
4963 got_fileindex_entry_free(ie);
4964 break;
4968 return err;
4972 static const struct got_error *
4973 check_out_of_date(const char *in_repo_path, unsigned char status,
4974 unsigned char staged_status, struct got_object_id *base_blob_id,
4975 struct got_object_id *base_commit_id,
4976 struct got_object_id *head_commit_id, struct got_repository *repo,
4977 int ood_errcode)
4979 const struct got_error *err = NULL;
4980 struct got_object_id *id = NULL;
4982 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4983 /* Trivial case: base commit == head commit */
4984 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4985 return NULL;
4987 * Ensure file content which local changes were based
4988 * on matches file content in the branch head.
4990 err = got_object_id_by_path(&id, repo, head_commit_id,
4991 in_repo_path);
4992 if (err) {
4993 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4994 err = got_error(ood_errcode);
4995 goto done;
4996 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4997 err = got_error(ood_errcode);
4998 } else {
4999 /* Require that added files don't exist in the branch head. */
5000 err = got_object_id_by_path(&id, repo, head_commit_id,
5001 in_repo_path);
5002 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5003 goto done;
5004 err = id ? got_error(ood_errcode) : NULL;
5006 done:
5007 free(id);
5008 return err;
5011 const struct got_error *
5012 commit_worktree(struct got_object_id **new_commit_id,
5013 struct got_pathlist_head *commitable_paths,
5014 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5015 const char *author, const char *committer,
5016 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5017 got_worktree_status_cb status_cb, void *status_arg,
5018 struct got_repository *repo)
5020 const struct got_error *err = NULL, *unlockerr = NULL;
5021 struct got_pathlist_entry *pe;
5022 const char *head_ref_name = NULL;
5023 struct got_commit_object *head_commit = NULL;
5024 struct got_reference *head_ref2 = NULL;
5025 struct got_object_id *head_commit_id2 = NULL;
5026 struct got_tree_object *head_tree = NULL;
5027 struct got_object_id *new_tree_id = NULL;
5028 int nentries;
5029 struct got_object_id_queue parent_ids;
5030 struct got_object_qid *pid = NULL;
5031 char *logmsg = NULL;
5033 *new_commit_id = NULL;
5035 SIMPLEQ_INIT(&parent_ids);
5037 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5038 if (err)
5039 goto done;
5041 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5042 if (err)
5043 goto done;
5045 if (commit_msg_cb != NULL) {
5046 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5047 if (err)
5048 goto done;
5051 if (logmsg == NULL || strlen(logmsg) == 0) {
5052 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5053 goto done;
5056 /* Create blobs from added and modified files and record their IDs. */
5057 TAILQ_FOREACH(pe, commitable_paths, entry) {
5058 struct got_commitable *ct = pe->data;
5059 char *ondisk_path;
5061 /* Blobs for staged files already exist. */
5062 if (ct->staged_status == GOT_STATUS_ADD ||
5063 ct->staged_status == GOT_STATUS_MODIFY)
5064 continue;
5066 if (ct->status != GOT_STATUS_ADD &&
5067 ct->status != GOT_STATUS_MODIFY &&
5068 ct->status != GOT_STATUS_MODE_CHANGE)
5069 continue;
5071 if (asprintf(&ondisk_path, "%s/%s",
5072 worktree->root_path, pe->path) == -1) {
5073 err = got_error_from_errno("asprintf");
5074 goto done;
5076 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5077 if (err) {
5078 free(ondisk_path);
5079 goto done;
5083 * When comitting a symlink we convert "bad" symlinks (those
5084 * which point outside the work tree or into .got) to regular
5085 * files. This way, the post-commit work tree state matches
5086 * a fresh checkout of the tree which was committed.
5088 if (S_ISLNK(get_ct_file_mode(ct))) {
5089 struct got_blob_object *blob;
5090 err = got_object_open_as_blob(&blob, repo, ct->blob_id,
5091 PATH_MAX);
5092 if (err) {
5093 free(ondisk_path);
5094 goto done;
5096 err = install_symlink(worktree, ondisk_path, ct->path,
5097 get_ct_file_mode(ct), GOT_DEFAULT_FILE_MODE, blob,
5098 0, 0, repo, NULL, NULL);
5099 got_object_blob_close(blob);
5100 if (err) {
5101 free(ondisk_path);
5102 goto done;
5105 free(ondisk_path);
5108 /* Recursively write new tree objects. */
5109 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5110 commitable_paths, status_cb, status_arg, repo);
5111 if (err)
5112 goto done;
5114 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5115 if (err)
5116 goto done;
5117 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5118 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5119 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5120 got_object_qid_free(pid);
5121 if (logmsg != NULL)
5122 free(logmsg);
5123 if (err)
5124 goto done;
5126 /* Check if a concurrent commit to our branch has occurred. */
5127 head_ref_name = got_worktree_get_head_ref_name(worktree);
5128 if (head_ref_name == NULL) {
5129 err = got_error_from_errno("got_worktree_get_head_ref_name");
5130 goto done;
5132 /* Lock the reference here to prevent concurrent modification. */
5133 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5134 if (err)
5135 goto done;
5136 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5137 if (err)
5138 goto done;
5139 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5140 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5141 goto done;
5143 /* Update branch head in repository. */
5144 err = got_ref_change_ref(head_ref2, *new_commit_id);
5145 if (err)
5146 goto done;
5147 err = got_ref_write(head_ref2, repo);
5148 if (err)
5149 goto done;
5151 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5152 if (err)
5153 goto done;
5155 err = ref_base_commit(worktree, repo);
5156 if (err)
5157 goto done;
5158 done:
5159 if (head_tree)
5160 got_object_tree_close(head_tree);
5161 if (head_commit)
5162 got_object_commit_close(head_commit);
5163 free(head_commit_id2);
5164 if (head_ref2) {
5165 unlockerr = got_ref_unlock(head_ref2);
5166 if (unlockerr && err == NULL)
5167 err = unlockerr;
5168 got_ref_close(head_ref2);
5170 return err;
5173 static const struct got_error *
5174 check_path_is_commitable(const char *path,
5175 struct got_pathlist_head *commitable_paths)
5177 struct got_pathlist_entry *cpe = NULL;
5178 size_t path_len = strlen(path);
5180 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5181 struct got_commitable *ct = cpe->data;
5182 const char *ct_path = ct->path;
5184 while (ct_path[0] == '/')
5185 ct_path++;
5187 if (strcmp(path, ct_path) == 0 ||
5188 got_path_is_child(ct_path, path, path_len))
5189 break;
5192 if (cpe == NULL)
5193 return got_error_path(path, GOT_ERR_BAD_PATH);
5195 return NULL;
5198 static const struct got_error *
5199 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5201 int *have_staged_files = arg;
5203 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5204 *have_staged_files = 1;
5205 return got_error(GOT_ERR_CANCELLED);
5208 return NULL;
5211 static const struct got_error *
5212 check_non_staged_files(struct got_fileindex *fileindex,
5213 struct got_pathlist_head *paths)
5215 struct got_pathlist_entry *pe;
5216 struct got_fileindex_entry *ie;
5218 TAILQ_FOREACH(pe, paths, entry) {
5219 if (pe->path[0] == '\0')
5220 continue;
5221 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5222 if (ie == NULL)
5223 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5224 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5225 return got_error_path(pe->path,
5226 GOT_ERR_FILE_NOT_STAGED);
5229 return NULL;
5232 const struct got_error *
5233 got_worktree_commit(struct got_object_id **new_commit_id,
5234 struct got_worktree *worktree, struct got_pathlist_head *paths,
5235 const char *author, const char *committer,
5236 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5237 got_worktree_status_cb status_cb, void *status_arg,
5238 struct got_repository *repo)
5240 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5241 struct got_fileindex *fileindex = NULL;
5242 char *fileindex_path = NULL;
5243 struct got_pathlist_head commitable_paths;
5244 struct collect_commitables_arg cc_arg;
5245 struct got_pathlist_entry *pe;
5246 struct got_reference *head_ref = NULL;
5247 struct got_object_id *head_commit_id = NULL;
5248 int have_staged_files = 0;
5250 *new_commit_id = NULL;
5252 TAILQ_INIT(&commitable_paths);
5254 err = lock_worktree(worktree, LOCK_EX);
5255 if (err)
5256 goto done;
5258 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5259 if (err)
5260 goto done;
5262 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5263 if (err)
5264 goto done;
5266 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5267 if (err)
5268 goto done;
5270 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5271 &have_staged_files);
5272 if (err && err->code != GOT_ERR_CANCELLED)
5273 goto done;
5274 if (have_staged_files) {
5275 err = check_non_staged_files(fileindex, paths);
5276 if (err)
5277 goto done;
5280 cc_arg.commitable_paths = &commitable_paths;
5281 cc_arg.worktree = worktree;
5282 cc_arg.repo = repo;
5283 cc_arg.have_staged_files = have_staged_files;
5284 TAILQ_FOREACH(pe, paths, entry) {
5285 err = worktree_status(worktree, pe->path, fileindex, repo,
5286 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5287 if (err)
5288 goto done;
5291 if (TAILQ_EMPTY(&commitable_paths)) {
5292 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5293 goto done;
5296 TAILQ_FOREACH(pe, paths, entry) {
5297 err = check_path_is_commitable(pe->path, &commitable_paths);
5298 if (err)
5299 goto done;
5302 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5303 struct got_commitable *ct = pe->data;
5304 const char *ct_path = ct->in_repo_path;
5306 while (ct_path[0] == '/')
5307 ct_path++;
5308 err = check_out_of_date(ct_path, ct->status,
5309 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5310 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5311 if (err)
5312 goto done;
5316 err = commit_worktree(new_commit_id, &commitable_paths,
5317 head_commit_id, worktree, author, committer,
5318 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5319 if (err)
5320 goto done;
5322 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5323 fileindex, have_staged_files);
5324 sync_err = sync_fileindex(fileindex, fileindex_path);
5325 if (sync_err && err == NULL)
5326 err = sync_err;
5327 done:
5328 if (fileindex)
5329 got_fileindex_free(fileindex);
5330 free(fileindex_path);
5331 unlockerr = lock_worktree(worktree, LOCK_SH);
5332 if (unlockerr && err == NULL)
5333 err = unlockerr;
5334 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5335 struct got_commitable *ct = pe->data;
5336 free_commitable(ct);
5338 got_pathlist_free(&commitable_paths);
5339 return err;
5342 const char *
5343 got_commitable_get_path(struct got_commitable *ct)
5345 return ct->path;
5348 unsigned int
5349 got_commitable_get_status(struct got_commitable *ct)
5351 return ct->status;
5354 struct check_rebase_ok_arg {
5355 struct got_worktree *worktree;
5356 struct got_repository *repo;
5359 static const struct got_error *
5360 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5362 const struct got_error *err = NULL;
5363 struct check_rebase_ok_arg *a = arg;
5364 unsigned char status;
5365 struct stat sb;
5366 char *ondisk_path;
5368 /* Reject rebase of a work tree with mixed base commits. */
5369 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5370 SHA1_DIGEST_LENGTH))
5371 return got_error(GOT_ERR_MIXED_COMMITS);
5373 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5374 == -1)
5375 return got_error_from_errno("asprintf");
5377 /* Reject rebase of a work tree with modified or staged files. */
5378 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5379 free(ondisk_path);
5380 if (err)
5381 return err;
5383 if (status != GOT_STATUS_NO_CHANGE)
5384 return got_error(GOT_ERR_MODIFIED);
5385 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5386 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5388 return NULL;
5391 const struct got_error *
5392 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5393 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5394 struct got_worktree *worktree, struct got_reference *branch,
5395 struct got_repository *repo)
5397 const struct got_error *err = NULL;
5398 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5399 char *branch_ref_name = NULL;
5400 char *fileindex_path = NULL;
5401 struct check_rebase_ok_arg ok_arg;
5402 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5403 struct got_object_id *wt_branch_tip = NULL;
5405 *new_base_branch_ref = NULL;
5406 *tmp_branch = NULL;
5407 *fileindex = NULL;
5409 err = lock_worktree(worktree, LOCK_EX);
5410 if (err)
5411 return err;
5413 err = open_fileindex(fileindex, &fileindex_path, worktree);
5414 if (err)
5415 goto done;
5417 ok_arg.worktree = worktree;
5418 ok_arg.repo = repo;
5419 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5420 &ok_arg);
5421 if (err)
5422 goto done;
5424 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5425 if (err)
5426 goto done;
5428 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5429 if (err)
5430 goto done;
5432 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5433 if (err)
5434 goto done;
5436 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5437 0);
5438 if (err)
5439 goto done;
5441 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5442 if (err)
5443 goto done;
5444 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5445 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5446 goto done;
5449 err = got_ref_alloc_symref(new_base_branch_ref,
5450 new_base_branch_ref_name, wt_branch);
5451 if (err)
5452 goto done;
5453 err = got_ref_write(*new_base_branch_ref, repo);
5454 if (err)
5455 goto done;
5457 /* TODO Lock original branch's ref while rebasing? */
5459 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5460 if (err)
5461 goto done;
5463 err = got_ref_write(branch_ref, repo);
5464 if (err)
5465 goto done;
5467 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5468 worktree->base_commit_id);
5469 if (err)
5470 goto done;
5471 err = got_ref_write(*tmp_branch, repo);
5472 if (err)
5473 goto done;
5475 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5476 if (err)
5477 goto done;
5478 done:
5479 free(fileindex_path);
5480 free(tmp_branch_name);
5481 free(new_base_branch_ref_name);
5482 free(branch_ref_name);
5483 if (branch_ref)
5484 got_ref_close(branch_ref);
5485 if (wt_branch)
5486 got_ref_close(wt_branch);
5487 free(wt_branch_tip);
5488 if (err) {
5489 if (*new_base_branch_ref) {
5490 got_ref_close(*new_base_branch_ref);
5491 *new_base_branch_ref = NULL;
5493 if (*tmp_branch) {
5494 got_ref_close(*tmp_branch);
5495 *tmp_branch = NULL;
5497 if (*fileindex) {
5498 got_fileindex_free(*fileindex);
5499 *fileindex = NULL;
5501 lock_worktree(worktree, LOCK_SH);
5503 return err;
5506 const struct got_error *
5507 got_worktree_rebase_continue(struct got_object_id **commit_id,
5508 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5509 struct got_reference **branch, struct got_fileindex **fileindex,
5510 struct got_worktree *worktree, struct got_repository *repo)
5512 const struct got_error *err;
5513 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5514 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5515 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5516 char *fileindex_path = NULL;
5517 int have_staged_files = 0;
5519 *commit_id = NULL;
5520 *new_base_branch = NULL;
5521 *tmp_branch = NULL;
5522 *branch = NULL;
5523 *fileindex = NULL;
5525 err = lock_worktree(worktree, LOCK_EX);
5526 if (err)
5527 return err;
5529 err = open_fileindex(fileindex, &fileindex_path, worktree);
5530 if (err)
5531 goto done;
5533 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5534 &have_staged_files);
5535 if (err && err->code != GOT_ERR_CANCELLED)
5536 goto done;
5537 if (have_staged_files) {
5538 err = got_error(GOT_ERR_STAGED_PATHS);
5539 goto done;
5542 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5543 if (err)
5544 goto done;
5546 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5547 if (err)
5548 goto done;
5550 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5551 if (err)
5552 goto done;
5554 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5555 if (err)
5556 goto done;
5558 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5559 if (err)
5560 goto done;
5562 err = got_ref_open(branch, repo,
5563 got_ref_get_symref_target(branch_ref), 0);
5564 if (err)
5565 goto done;
5567 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5568 if (err)
5569 goto done;
5571 err = got_ref_resolve(commit_id, repo, commit_ref);
5572 if (err)
5573 goto done;
5575 err = got_ref_open(new_base_branch, repo,
5576 new_base_branch_ref_name, 0);
5577 if (err)
5578 goto done;
5580 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5581 if (err)
5582 goto done;
5583 done:
5584 free(commit_ref_name);
5585 free(branch_ref_name);
5586 free(fileindex_path);
5587 if (commit_ref)
5588 got_ref_close(commit_ref);
5589 if (branch_ref)
5590 got_ref_close(branch_ref);
5591 if (err) {
5592 free(*commit_id);
5593 *commit_id = NULL;
5594 if (*tmp_branch) {
5595 got_ref_close(*tmp_branch);
5596 *tmp_branch = NULL;
5598 if (*new_base_branch) {
5599 got_ref_close(*new_base_branch);
5600 *new_base_branch = NULL;
5602 if (*branch) {
5603 got_ref_close(*branch);
5604 *branch = NULL;
5606 if (*fileindex) {
5607 got_fileindex_free(*fileindex);
5608 *fileindex = NULL;
5610 lock_worktree(worktree, LOCK_SH);
5612 return err;
5615 const struct got_error *
5616 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5618 const struct got_error *err;
5619 char *tmp_branch_name = NULL;
5621 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5622 if (err)
5623 return err;
5625 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5626 free(tmp_branch_name);
5627 return NULL;
5630 static const struct got_error *
5631 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5632 char **logmsg, void *arg)
5634 *logmsg = arg;
5635 return NULL;
5638 static const struct got_error *
5639 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5640 const char *path, struct got_object_id *blob_id,
5641 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5642 int dirfd, const char *de_name)
5644 return NULL;
5647 struct collect_merged_paths_arg {
5648 got_worktree_checkout_cb progress_cb;
5649 void *progress_arg;
5650 struct got_pathlist_head *merged_paths;
5653 static const struct got_error *
5654 collect_merged_paths(void *arg, unsigned char status, const char *path)
5656 const struct got_error *err;
5657 struct collect_merged_paths_arg *a = arg;
5658 char *p;
5659 struct got_pathlist_entry *new;
5661 err = (*a->progress_cb)(a->progress_arg, status, path);
5662 if (err)
5663 return err;
5665 if (status != GOT_STATUS_MERGE &&
5666 status != GOT_STATUS_ADD &&
5667 status != GOT_STATUS_DELETE &&
5668 status != GOT_STATUS_CONFLICT)
5669 return NULL;
5671 p = strdup(path);
5672 if (p == NULL)
5673 return got_error_from_errno("strdup");
5675 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5676 if (err || new == NULL)
5677 free(p);
5678 return err;
5681 void
5682 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5684 struct got_pathlist_entry *pe;
5686 TAILQ_FOREACH(pe, merged_paths, entry)
5687 free((char *)pe->path);
5689 got_pathlist_free(merged_paths);
5692 static const struct got_error *
5693 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5694 int is_rebase, struct got_repository *repo)
5696 const struct got_error *err;
5697 struct got_reference *commit_ref = NULL;
5699 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5700 if (err) {
5701 if (err->code != GOT_ERR_NOT_REF)
5702 goto done;
5703 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5704 if (err)
5705 goto done;
5706 err = got_ref_write(commit_ref, repo);
5707 if (err)
5708 goto done;
5709 } else if (is_rebase) {
5710 struct got_object_id *stored_id;
5711 int cmp;
5713 err = got_ref_resolve(&stored_id, repo, commit_ref);
5714 if (err)
5715 goto done;
5716 cmp = got_object_id_cmp(commit_id, stored_id);
5717 free(stored_id);
5718 if (cmp != 0) {
5719 err = got_error(GOT_ERR_REBASE_COMMITID);
5720 goto done;
5723 done:
5724 if (commit_ref)
5725 got_ref_close(commit_ref);
5726 return err;
5729 static const struct got_error *
5730 rebase_merge_files(struct got_pathlist_head *merged_paths,
5731 const char *commit_ref_name, struct got_worktree *worktree,
5732 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5733 struct got_object_id *commit_id, struct got_repository *repo,
5734 got_worktree_checkout_cb progress_cb, void *progress_arg,
5735 got_cancel_cb cancel_cb, void *cancel_arg)
5737 const struct got_error *err;
5738 struct got_reference *commit_ref = NULL;
5739 struct collect_merged_paths_arg cmp_arg;
5740 char *fileindex_path;
5742 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5744 err = get_fileindex_path(&fileindex_path, worktree);
5745 if (err)
5746 return err;
5748 cmp_arg.progress_cb = progress_cb;
5749 cmp_arg.progress_arg = progress_arg;
5750 cmp_arg.merged_paths = merged_paths;
5751 err = merge_files(worktree, fileindex, fileindex_path,
5752 parent_commit_id, commit_id, repo, collect_merged_paths,
5753 &cmp_arg, cancel_cb, cancel_arg);
5754 if (commit_ref)
5755 got_ref_close(commit_ref);
5756 return err;
5759 const struct got_error *
5760 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5761 struct got_worktree *worktree, struct got_fileindex *fileindex,
5762 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5763 struct got_repository *repo,
5764 got_worktree_checkout_cb progress_cb, void *progress_arg,
5765 got_cancel_cb cancel_cb, void *cancel_arg)
5767 const struct got_error *err;
5768 char *commit_ref_name;
5770 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5771 if (err)
5772 return err;
5774 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5775 if (err)
5776 goto done;
5778 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5779 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5780 progress_arg, cancel_cb, cancel_arg);
5781 done:
5782 free(commit_ref_name);
5783 return err;
5786 const struct got_error *
5787 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5788 struct got_worktree *worktree, struct got_fileindex *fileindex,
5789 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5790 struct got_repository *repo,
5791 got_worktree_checkout_cb progress_cb, void *progress_arg,
5792 got_cancel_cb cancel_cb, void *cancel_arg)
5794 const struct got_error *err;
5795 char *commit_ref_name;
5797 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5798 if (err)
5799 return err;
5801 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5802 if (err)
5803 goto done;
5805 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5806 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5807 progress_arg, cancel_cb, cancel_arg);
5808 done:
5809 free(commit_ref_name);
5810 return err;
5813 static const struct got_error *
5814 rebase_commit(struct got_object_id **new_commit_id,
5815 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5816 struct got_worktree *worktree, struct got_fileindex *fileindex,
5817 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5818 const char *new_logmsg, struct got_repository *repo)
5820 const struct got_error *err, *sync_err;
5821 struct got_pathlist_head commitable_paths;
5822 struct collect_commitables_arg cc_arg;
5823 char *fileindex_path = NULL;
5824 struct got_reference *head_ref = NULL;
5825 struct got_object_id *head_commit_id = NULL;
5826 char *logmsg = NULL;
5828 TAILQ_INIT(&commitable_paths);
5829 *new_commit_id = NULL;
5831 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5833 err = get_fileindex_path(&fileindex_path, worktree);
5834 if (err)
5835 return err;
5837 cc_arg.commitable_paths = &commitable_paths;
5838 cc_arg.worktree = worktree;
5839 cc_arg.repo = repo;
5840 cc_arg.have_staged_files = 0;
5842 * If possible get the status of individual files directly to
5843 * avoid crawling the entire work tree once per rebased commit.
5844 * TODO: Ideally, merged_paths would contain a list of commitables
5845 * we could use so we could skip worktree_status() entirely.
5847 if (merged_paths) {
5848 struct got_pathlist_entry *pe;
5849 TAILQ_FOREACH(pe, merged_paths, entry) {
5850 err = worktree_status(worktree, pe->path, fileindex,
5851 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5852 0);
5853 if (err)
5854 goto done;
5856 } else {
5857 err = worktree_status(worktree, "", fileindex, repo,
5858 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5859 if (err)
5860 goto done;
5863 if (TAILQ_EMPTY(&commitable_paths)) {
5864 /* No-op change; commit will be elided. */
5865 err = got_ref_delete(commit_ref, repo);
5866 if (err)
5867 goto done;
5868 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5869 goto done;
5872 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5873 if (err)
5874 goto done;
5876 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5877 if (err)
5878 goto done;
5880 if (new_logmsg) {
5881 logmsg = strdup(new_logmsg);
5882 if (logmsg == NULL) {
5883 err = got_error_from_errno("strdup");
5884 goto done;
5886 } else {
5887 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5888 if (err)
5889 goto done;
5892 /* NB: commit_worktree will call free(logmsg) */
5893 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5894 worktree, got_object_commit_get_author(orig_commit),
5895 got_object_commit_get_committer(orig_commit),
5896 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5897 if (err)
5898 goto done;
5900 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5901 if (err)
5902 goto done;
5904 err = got_ref_delete(commit_ref, repo);
5905 if (err)
5906 goto done;
5908 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5909 fileindex, 0);
5910 sync_err = sync_fileindex(fileindex, fileindex_path);
5911 if (sync_err && err == NULL)
5912 err = sync_err;
5913 done:
5914 free(fileindex_path);
5915 free(head_commit_id);
5916 if (head_ref)
5917 got_ref_close(head_ref);
5918 if (err) {
5919 free(*new_commit_id);
5920 *new_commit_id = NULL;
5922 return err;
5925 const struct got_error *
5926 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5927 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5928 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5929 struct got_commit_object *orig_commit,
5930 struct got_object_id *orig_commit_id, struct got_repository *repo)
5932 const struct got_error *err;
5933 char *commit_ref_name;
5934 struct got_reference *commit_ref = NULL;
5935 struct got_object_id *commit_id = NULL;
5937 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5938 if (err)
5939 return err;
5941 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5942 if (err)
5943 goto done;
5944 err = got_ref_resolve(&commit_id, repo, commit_ref);
5945 if (err)
5946 goto done;
5947 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5948 err = got_error(GOT_ERR_REBASE_COMMITID);
5949 goto done;
5952 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5953 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5954 done:
5955 if (commit_ref)
5956 got_ref_close(commit_ref);
5957 free(commit_ref_name);
5958 free(commit_id);
5959 return err;
5962 const struct got_error *
5963 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5964 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5965 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5966 struct got_commit_object *orig_commit,
5967 struct got_object_id *orig_commit_id, const char *new_logmsg,
5968 struct got_repository *repo)
5970 const struct got_error *err;
5971 char *commit_ref_name;
5972 struct got_reference *commit_ref = NULL;
5974 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5975 if (err)
5976 return err;
5978 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5979 if (err)
5980 goto done;
5982 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5983 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5984 done:
5985 if (commit_ref)
5986 got_ref_close(commit_ref);
5987 free(commit_ref_name);
5988 return err;
5991 const struct got_error *
5992 got_worktree_rebase_postpone(struct got_worktree *worktree,
5993 struct got_fileindex *fileindex)
5995 if (fileindex)
5996 got_fileindex_free(fileindex);
5997 return lock_worktree(worktree, LOCK_SH);
6000 static const struct got_error *
6001 delete_ref(const char *name, struct got_repository *repo)
6003 const struct got_error *err;
6004 struct got_reference *ref;
6006 err = got_ref_open(&ref, repo, name, 0);
6007 if (err) {
6008 if (err->code == GOT_ERR_NOT_REF)
6009 return NULL;
6010 return err;
6013 err = got_ref_delete(ref, repo);
6014 got_ref_close(ref);
6015 return err;
6018 static const struct got_error *
6019 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6021 const struct got_error *err;
6022 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6023 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6025 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6026 if (err)
6027 goto done;
6028 err = delete_ref(tmp_branch_name, repo);
6029 if (err)
6030 goto done;
6032 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6033 if (err)
6034 goto done;
6035 err = delete_ref(new_base_branch_ref_name, repo);
6036 if (err)
6037 goto done;
6039 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6040 if (err)
6041 goto done;
6042 err = delete_ref(branch_ref_name, repo);
6043 if (err)
6044 goto done;
6046 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6047 if (err)
6048 goto done;
6049 err = delete_ref(commit_ref_name, repo);
6050 if (err)
6051 goto done;
6053 done:
6054 free(tmp_branch_name);
6055 free(new_base_branch_ref_name);
6056 free(branch_ref_name);
6057 free(commit_ref_name);
6058 return err;
6061 const struct got_error *
6062 got_worktree_rebase_complete(struct got_worktree *worktree,
6063 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6064 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6065 struct got_repository *repo)
6067 const struct got_error *err, *unlockerr;
6068 struct got_object_id *new_head_commit_id = NULL;
6070 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6071 if (err)
6072 return err;
6074 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6075 if (err)
6076 goto done;
6078 err = got_ref_write(rebased_branch, repo);
6079 if (err)
6080 goto done;
6082 err = got_worktree_set_head_ref(worktree, rebased_branch);
6083 if (err)
6084 goto done;
6086 err = delete_rebase_refs(worktree, repo);
6087 done:
6088 if (fileindex)
6089 got_fileindex_free(fileindex);
6090 free(new_head_commit_id);
6091 unlockerr = lock_worktree(worktree, LOCK_SH);
6092 if (unlockerr && err == NULL)
6093 err = unlockerr;
6094 return err;
6097 const struct got_error *
6098 got_worktree_rebase_abort(struct got_worktree *worktree,
6099 struct got_fileindex *fileindex, struct got_repository *repo,
6100 struct got_reference *new_base_branch,
6101 got_worktree_checkout_cb progress_cb, void *progress_arg)
6103 const struct got_error *err, *unlockerr, *sync_err;
6104 struct got_reference *resolved = NULL;
6105 struct got_object_id *commit_id = NULL;
6106 char *fileindex_path = NULL;
6107 struct revert_file_args rfa;
6108 struct got_object_id *tree_id = NULL;
6110 err = lock_worktree(worktree, LOCK_EX);
6111 if (err)
6112 return err;
6114 err = got_ref_open(&resolved, repo,
6115 got_ref_get_symref_target(new_base_branch), 0);
6116 if (err)
6117 goto done;
6119 err = got_worktree_set_head_ref(worktree, resolved);
6120 if (err)
6121 goto done;
6124 * XXX commits to the base branch could have happened while
6125 * we were busy rebasing; should we store the original commit ID
6126 * when rebase begins and read it back here?
6128 err = got_ref_resolve(&commit_id, repo, resolved);
6129 if (err)
6130 goto done;
6132 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6133 if (err)
6134 goto done;
6136 err = got_object_id_by_path(&tree_id, repo,
6137 worktree->base_commit_id, worktree->path_prefix);
6138 if (err)
6139 goto done;
6141 err = delete_rebase_refs(worktree, repo);
6142 if (err)
6143 goto done;
6145 err = get_fileindex_path(&fileindex_path, worktree);
6146 if (err)
6147 goto done;
6149 rfa.worktree = worktree;
6150 rfa.fileindex = fileindex;
6151 rfa.progress_cb = progress_cb;
6152 rfa.progress_arg = progress_arg;
6153 rfa.patch_cb = NULL;
6154 rfa.patch_arg = NULL;
6155 rfa.repo = repo;
6156 err = worktree_status(worktree, "", fileindex, repo,
6157 revert_file, &rfa, NULL, NULL, 0, 0);
6158 if (err)
6159 goto sync;
6161 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6162 repo, progress_cb, progress_arg, NULL, NULL);
6163 sync:
6164 sync_err = sync_fileindex(fileindex, fileindex_path);
6165 if (sync_err && err == NULL)
6166 err = sync_err;
6167 done:
6168 got_ref_close(resolved);
6169 free(tree_id);
6170 free(commit_id);
6171 if (fileindex)
6172 got_fileindex_free(fileindex);
6173 free(fileindex_path);
6175 unlockerr = lock_worktree(worktree, LOCK_SH);
6176 if (unlockerr && err == NULL)
6177 err = unlockerr;
6178 return err;
6181 const struct got_error *
6182 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6183 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6184 struct got_fileindex **fileindex, struct got_worktree *worktree,
6185 struct got_repository *repo)
6187 const struct got_error *err = NULL;
6188 char *tmp_branch_name = NULL;
6189 char *branch_ref_name = NULL;
6190 char *base_commit_ref_name = NULL;
6191 char *fileindex_path = NULL;
6192 struct check_rebase_ok_arg ok_arg;
6193 struct got_reference *wt_branch = NULL;
6194 struct got_reference *base_commit_ref = NULL;
6196 *tmp_branch = NULL;
6197 *branch_ref = NULL;
6198 *base_commit_id = NULL;
6199 *fileindex = NULL;
6201 err = lock_worktree(worktree, LOCK_EX);
6202 if (err)
6203 return err;
6205 err = open_fileindex(fileindex, &fileindex_path, worktree);
6206 if (err)
6207 goto done;
6209 ok_arg.worktree = worktree;
6210 ok_arg.repo = repo;
6211 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6212 &ok_arg);
6213 if (err)
6214 goto done;
6216 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6217 if (err)
6218 goto done;
6220 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6221 if (err)
6222 goto done;
6224 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6225 worktree);
6226 if (err)
6227 goto done;
6229 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6230 0);
6231 if (err)
6232 goto done;
6234 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6235 if (err)
6236 goto done;
6238 err = got_ref_write(*branch_ref, repo);
6239 if (err)
6240 goto done;
6242 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6243 worktree->base_commit_id);
6244 if (err)
6245 goto done;
6246 err = got_ref_write(base_commit_ref, repo);
6247 if (err)
6248 goto done;
6249 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6250 if (*base_commit_id == NULL) {
6251 err = got_error_from_errno("got_object_id_dup");
6252 goto done;
6255 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6256 worktree->base_commit_id);
6257 if (err)
6258 goto done;
6259 err = got_ref_write(*tmp_branch, repo);
6260 if (err)
6261 goto done;
6263 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6264 if (err)
6265 goto done;
6266 done:
6267 free(fileindex_path);
6268 free(tmp_branch_name);
6269 free(branch_ref_name);
6270 free(base_commit_ref_name);
6271 if (wt_branch)
6272 got_ref_close(wt_branch);
6273 if (err) {
6274 if (*branch_ref) {
6275 got_ref_close(*branch_ref);
6276 *branch_ref = NULL;
6278 if (*tmp_branch) {
6279 got_ref_close(*tmp_branch);
6280 *tmp_branch = NULL;
6282 free(*base_commit_id);
6283 if (*fileindex) {
6284 got_fileindex_free(*fileindex);
6285 *fileindex = NULL;
6287 lock_worktree(worktree, LOCK_SH);
6289 return err;
6292 const struct got_error *
6293 got_worktree_histedit_postpone(struct got_worktree *worktree,
6294 struct got_fileindex *fileindex)
6296 if (fileindex)
6297 got_fileindex_free(fileindex);
6298 return lock_worktree(worktree, LOCK_SH);
6301 const struct got_error *
6302 got_worktree_histedit_in_progress(int *in_progress,
6303 struct got_worktree *worktree)
6305 const struct got_error *err;
6306 char *tmp_branch_name = NULL;
6308 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6309 if (err)
6310 return err;
6312 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6313 free(tmp_branch_name);
6314 return NULL;
6317 const struct got_error *
6318 got_worktree_histedit_continue(struct got_object_id **commit_id,
6319 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6320 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6321 struct got_worktree *worktree, struct got_repository *repo)
6323 const struct got_error *err;
6324 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6325 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6326 struct got_reference *commit_ref = NULL;
6327 struct got_reference *base_commit_ref = NULL;
6328 char *fileindex_path = NULL;
6329 int have_staged_files = 0;
6331 *commit_id = NULL;
6332 *tmp_branch = NULL;
6333 *base_commit_id = NULL;
6334 *fileindex = NULL;
6336 err = lock_worktree(worktree, LOCK_EX);
6337 if (err)
6338 return err;
6340 err = open_fileindex(fileindex, &fileindex_path, worktree);
6341 if (err)
6342 goto done;
6344 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6345 &have_staged_files);
6346 if (err && err->code != GOT_ERR_CANCELLED)
6347 goto done;
6348 if (have_staged_files) {
6349 err = got_error(GOT_ERR_STAGED_PATHS);
6350 goto done;
6353 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6354 if (err)
6355 goto done;
6357 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6358 if (err)
6359 goto done;
6361 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6362 if (err)
6363 goto done;
6365 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6366 worktree);
6367 if (err)
6368 goto done;
6370 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6371 if (err)
6372 goto done;
6374 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6375 if (err)
6376 goto done;
6377 err = got_ref_resolve(commit_id, repo, commit_ref);
6378 if (err)
6379 goto done;
6381 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6382 if (err)
6383 goto done;
6384 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6385 if (err)
6386 goto done;
6388 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6389 if (err)
6390 goto done;
6391 done:
6392 free(commit_ref_name);
6393 free(branch_ref_name);
6394 free(fileindex_path);
6395 if (commit_ref)
6396 got_ref_close(commit_ref);
6397 if (base_commit_ref)
6398 got_ref_close(base_commit_ref);
6399 if (err) {
6400 free(*commit_id);
6401 *commit_id = NULL;
6402 free(*base_commit_id);
6403 *base_commit_id = NULL;
6404 if (*tmp_branch) {
6405 got_ref_close(*tmp_branch);
6406 *tmp_branch = NULL;
6408 if (*fileindex) {
6409 got_fileindex_free(*fileindex);
6410 *fileindex = NULL;
6412 lock_worktree(worktree, LOCK_EX);
6414 return err;
6417 static const struct got_error *
6418 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6420 const struct got_error *err;
6421 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6422 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6424 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6425 if (err)
6426 goto done;
6427 err = delete_ref(tmp_branch_name, repo);
6428 if (err)
6429 goto done;
6431 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6432 worktree);
6433 if (err)
6434 goto done;
6435 err = delete_ref(base_commit_ref_name, repo);
6436 if (err)
6437 goto done;
6439 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6440 if (err)
6441 goto done;
6442 err = delete_ref(branch_ref_name, repo);
6443 if (err)
6444 goto done;
6446 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6447 if (err)
6448 goto done;
6449 err = delete_ref(commit_ref_name, repo);
6450 if (err)
6451 goto done;
6452 done:
6453 free(tmp_branch_name);
6454 free(base_commit_ref_name);
6455 free(branch_ref_name);
6456 free(commit_ref_name);
6457 return err;
6460 const struct got_error *
6461 got_worktree_histedit_abort(struct got_worktree *worktree,
6462 struct got_fileindex *fileindex, struct got_repository *repo,
6463 struct got_reference *branch, struct got_object_id *base_commit_id,
6464 got_worktree_checkout_cb progress_cb, void *progress_arg)
6466 const struct got_error *err, *unlockerr, *sync_err;
6467 struct got_reference *resolved = NULL;
6468 char *fileindex_path = NULL;
6469 struct got_object_id *tree_id = NULL;
6470 struct revert_file_args rfa;
6472 err = lock_worktree(worktree, LOCK_EX);
6473 if (err)
6474 return err;
6476 err = got_ref_open(&resolved, repo,
6477 got_ref_get_symref_target(branch), 0);
6478 if (err)
6479 goto done;
6481 err = got_worktree_set_head_ref(worktree, resolved);
6482 if (err)
6483 goto done;
6485 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6486 if (err)
6487 goto done;
6489 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6490 worktree->path_prefix);
6491 if (err)
6492 goto done;
6494 err = delete_histedit_refs(worktree, repo);
6495 if (err)
6496 goto done;
6498 err = get_fileindex_path(&fileindex_path, worktree);
6499 if (err)
6500 goto done;
6502 rfa.worktree = worktree;
6503 rfa.fileindex = fileindex;
6504 rfa.progress_cb = progress_cb;
6505 rfa.progress_arg = progress_arg;
6506 rfa.patch_cb = NULL;
6507 rfa.patch_arg = NULL;
6508 rfa.repo = repo;
6509 err = worktree_status(worktree, "", fileindex, repo,
6510 revert_file, &rfa, NULL, NULL, 0, 0);
6511 if (err)
6512 goto sync;
6514 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6515 repo, progress_cb, progress_arg, NULL, NULL);
6516 sync:
6517 sync_err = sync_fileindex(fileindex, fileindex_path);
6518 if (sync_err && err == NULL)
6519 err = sync_err;
6520 done:
6521 got_ref_close(resolved);
6522 free(tree_id);
6523 free(fileindex_path);
6525 unlockerr = lock_worktree(worktree, LOCK_SH);
6526 if (unlockerr && err == NULL)
6527 err = unlockerr;
6528 return err;
6531 const struct got_error *
6532 got_worktree_histedit_complete(struct got_worktree *worktree,
6533 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6534 struct got_reference *edited_branch, struct got_repository *repo)
6536 const struct got_error *err, *unlockerr;
6537 struct got_object_id *new_head_commit_id = NULL;
6538 struct got_reference *resolved = NULL;
6540 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6541 if (err)
6542 return err;
6544 err = got_ref_open(&resolved, repo,
6545 got_ref_get_symref_target(edited_branch), 0);
6546 if (err)
6547 goto done;
6549 err = got_ref_change_ref(resolved, new_head_commit_id);
6550 if (err)
6551 goto done;
6553 err = got_ref_write(resolved, repo);
6554 if (err)
6555 goto done;
6557 err = got_worktree_set_head_ref(worktree, resolved);
6558 if (err)
6559 goto done;
6561 err = delete_histedit_refs(worktree, repo);
6562 done:
6563 if (fileindex)
6564 got_fileindex_free(fileindex);
6565 free(new_head_commit_id);
6566 unlockerr = lock_worktree(worktree, LOCK_SH);
6567 if (unlockerr && err == NULL)
6568 err = unlockerr;
6569 return err;
6572 const struct got_error *
6573 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6574 struct got_object_id *commit_id, struct got_repository *repo)
6576 const struct got_error *err;
6577 char *commit_ref_name;
6579 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6580 if (err)
6581 return err;
6583 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6584 if (err)
6585 goto done;
6587 err = delete_ref(commit_ref_name, repo);
6588 done:
6589 free(commit_ref_name);
6590 return err;
6593 const struct got_error *
6594 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6595 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6596 struct got_worktree *worktree, const char *refname,
6597 struct got_repository *repo)
6599 const struct got_error *err = NULL;
6600 char *fileindex_path = NULL;
6601 struct check_rebase_ok_arg ok_arg;
6603 *fileindex = NULL;
6604 *branch_ref = NULL;
6605 *base_branch_ref = NULL;
6607 err = lock_worktree(worktree, LOCK_EX);
6608 if (err)
6609 return err;
6611 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6612 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6613 "cannot integrate a branch into itself; "
6614 "update -b or different branch name required");
6615 goto done;
6618 err = open_fileindex(fileindex, &fileindex_path, worktree);
6619 if (err)
6620 goto done;
6622 /* Preconditions are the same as for rebase. */
6623 ok_arg.worktree = worktree;
6624 ok_arg.repo = repo;
6625 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6626 &ok_arg);
6627 if (err)
6628 goto done;
6630 err = got_ref_open(branch_ref, repo, refname, 1);
6631 if (err)
6632 goto done;
6634 err = got_ref_open(base_branch_ref, repo,
6635 got_worktree_get_head_ref_name(worktree), 1);
6636 done:
6637 if (err) {
6638 if (*branch_ref) {
6639 got_ref_close(*branch_ref);
6640 *branch_ref = NULL;
6642 if (*base_branch_ref) {
6643 got_ref_close(*base_branch_ref);
6644 *base_branch_ref = NULL;
6646 if (*fileindex) {
6647 got_fileindex_free(*fileindex);
6648 *fileindex = NULL;
6650 lock_worktree(worktree, LOCK_SH);
6652 return err;
6655 const struct got_error *
6656 got_worktree_integrate_continue(struct got_worktree *worktree,
6657 struct got_fileindex *fileindex, struct got_repository *repo,
6658 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6659 got_worktree_checkout_cb progress_cb, void *progress_arg,
6660 got_cancel_cb cancel_cb, void *cancel_arg)
6662 const struct got_error *err = NULL, *sync_err, *unlockerr;
6663 char *fileindex_path = NULL;
6664 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6666 err = get_fileindex_path(&fileindex_path, worktree);
6667 if (err)
6668 goto done;
6670 err = got_ref_resolve(&commit_id, repo, branch_ref);
6671 if (err)
6672 goto done;
6674 err = got_object_id_by_path(&tree_id, repo, commit_id,
6675 worktree->path_prefix);
6676 if (err)
6677 goto done;
6679 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6680 if (err)
6681 goto done;
6683 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6684 progress_cb, progress_arg, cancel_cb, cancel_arg);
6685 if (err)
6686 goto sync;
6688 err = got_ref_change_ref(base_branch_ref, commit_id);
6689 if (err)
6690 goto sync;
6692 err = got_ref_write(base_branch_ref, repo);
6693 sync:
6694 sync_err = sync_fileindex(fileindex, fileindex_path);
6695 if (sync_err && err == NULL)
6696 err = sync_err;
6698 done:
6699 unlockerr = got_ref_unlock(branch_ref);
6700 if (unlockerr && err == NULL)
6701 err = unlockerr;
6702 got_ref_close(branch_ref);
6704 unlockerr = got_ref_unlock(base_branch_ref);
6705 if (unlockerr && err == NULL)
6706 err = unlockerr;
6707 got_ref_close(base_branch_ref);
6709 got_fileindex_free(fileindex);
6710 free(fileindex_path);
6711 free(tree_id);
6713 unlockerr = lock_worktree(worktree, LOCK_SH);
6714 if (unlockerr && err == NULL)
6715 err = unlockerr;
6716 return err;
6719 const struct got_error *
6720 got_worktree_integrate_abort(struct got_worktree *worktree,
6721 struct got_fileindex *fileindex, struct got_repository *repo,
6722 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6724 const struct got_error *err = NULL, *unlockerr = NULL;
6726 got_fileindex_free(fileindex);
6728 err = lock_worktree(worktree, LOCK_SH);
6730 unlockerr = got_ref_unlock(branch_ref);
6731 if (unlockerr && err == NULL)
6732 err = unlockerr;
6733 got_ref_close(branch_ref);
6735 unlockerr = got_ref_unlock(base_branch_ref);
6736 if (unlockerr && err == NULL)
6737 err = unlockerr;
6738 got_ref_close(base_branch_ref);
6740 return err;
6743 struct check_stage_ok_arg {
6744 struct got_object_id *head_commit_id;
6745 struct got_worktree *worktree;
6746 struct got_fileindex *fileindex;
6747 struct got_repository *repo;
6748 int have_changes;
6751 const struct got_error *
6752 check_stage_ok(void *arg, unsigned char status,
6753 unsigned char staged_status, const char *relpath,
6754 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6755 struct got_object_id *commit_id, int dirfd, const char *de_name)
6757 struct check_stage_ok_arg *a = arg;
6758 const struct got_error *err = NULL;
6759 struct got_fileindex_entry *ie;
6760 struct got_object_id base_commit_id;
6761 struct got_object_id *base_commit_idp = NULL;
6762 char *in_repo_path = NULL, *p;
6764 if (status == GOT_STATUS_UNVERSIONED ||
6765 status == GOT_STATUS_NO_CHANGE)
6766 return NULL;
6767 if (status == GOT_STATUS_NONEXISTENT)
6768 return got_error_set_errno(ENOENT, relpath);
6770 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6771 if (ie == NULL)
6772 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6774 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6775 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6776 relpath) == -1)
6777 return got_error_from_errno("asprintf");
6779 if (got_fileindex_entry_has_commit(ie)) {
6780 memcpy(base_commit_id.sha1, ie->commit_sha1,
6781 SHA1_DIGEST_LENGTH);
6782 base_commit_idp = &base_commit_id;
6785 if (status == GOT_STATUS_CONFLICT) {
6786 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6787 goto done;
6788 } else if (status != GOT_STATUS_ADD &&
6789 status != GOT_STATUS_MODIFY &&
6790 status != GOT_STATUS_DELETE) {
6791 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6792 goto done;
6795 a->have_changes = 1;
6797 p = in_repo_path;
6798 while (p[0] == '/')
6799 p++;
6800 err = check_out_of_date(p, status, staged_status,
6801 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6802 GOT_ERR_STAGE_OUT_OF_DATE);
6803 done:
6804 free(in_repo_path);
6805 return err;
6808 struct stage_path_arg {
6809 struct got_worktree *worktree;
6810 struct got_fileindex *fileindex;
6811 struct got_repository *repo;
6812 got_worktree_status_cb status_cb;
6813 void *status_arg;
6814 got_worktree_patch_cb patch_cb;
6815 void *patch_arg;
6816 int staged_something;
6819 static const struct got_error *
6820 stage_path(void *arg, unsigned char status,
6821 unsigned char staged_status, const char *relpath,
6822 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6823 struct got_object_id *commit_id, int dirfd, const char *de_name)
6825 struct stage_path_arg *a = arg;
6826 const struct got_error *err = NULL;
6827 struct got_fileindex_entry *ie;
6828 char *ondisk_path = NULL, *path_content = NULL;
6829 uint32_t stage;
6830 struct got_object_id *new_staged_blob_id = NULL;
6832 if (status == GOT_STATUS_UNVERSIONED)
6833 return NULL;
6835 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6836 if (ie == NULL)
6837 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6839 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6840 relpath)== -1)
6841 return got_error_from_errno("asprintf");
6843 switch (status) {
6844 case GOT_STATUS_ADD:
6845 case GOT_STATUS_MODIFY:
6846 if (a->patch_cb) {
6847 if (status == GOT_STATUS_ADD) {
6848 int choice = GOT_PATCH_CHOICE_NONE;
6849 err = (*a->patch_cb)(&choice, a->patch_arg,
6850 status, ie->path, NULL, 1, 1);
6851 if (err)
6852 break;
6853 if (choice != GOT_PATCH_CHOICE_YES)
6854 break;
6855 } else {
6856 err = create_patched_content(&path_content, 0,
6857 staged_blob_id ? staged_blob_id : blob_id,
6858 ondisk_path, dirfd, de_name, ie->path,
6859 a->repo, a->patch_cb, a->patch_arg);
6860 if (err || path_content == NULL)
6861 break;
6864 err = got_object_blob_create(&new_staged_blob_id,
6865 path_content ? path_content : ondisk_path, a->repo);
6866 if (err)
6867 break;
6868 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6869 SHA1_DIGEST_LENGTH);
6870 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6871 stage = GOT_FILEIDX_STAGE_ADD;
6872 else
6873 stage = GOT_FILEIDX_STAGE_MODIFY;
6874 got_fileindex_entry_stage_set(ie, stage);
6875 a->staged_something = 1;
6876 if (a->status_cb == NULL)
6877 break;
6878 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6879 get_staged_status(ie), relpath, blob_id,
6880 new_staged_blob_id, NULL, dirfd, de_name);
6881 break;
6882 case GOT_STATUS_DELETE:
6883 if (staged_status == GOT_STATUS_DELETE)
6884 break;
6885 if (a->patch_cb) {
6886 int choice = GOT_PATCH_CHOICE_NONE;
6887 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6888 ie->path, NULL, 1, 1);
6889 if (err)
6890 break;
6891 if (choice == GOT_PATCH_CHOICE_NO)
6892 break;
6893 if (choice != GOT_PATCH_CHOICE_YES) {
6894 err = got_error(GOT_ERR_PATCH_CHOICE);
6895 break;
6898 stage = GOT_FILEIDX_STAGE_DELETE;
6899 got_fileindex_entry_stage_set(ie, stage);
6900 a->staged_something = 1;
6901 if (a->status_cb == NULL)
6902 break;
6903 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6904 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6905 de_name);
6906 break;
6907 case GOT_STATUS_NO_CHANGE:
6908 break;
6909 case GOT_STATUS_CONFLICT:
6910 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6911 break;
6912 case GOT_STATUS_NONEXISTENT:
6913 err = got_error_set_errno(ENOENT, relpath);
6914 break;
6915 default:
6916 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6917 break;
6920 if (path_content && unlink(path_content) == -1 && err == NULL)
6921 err = got_error_from_errno2("unlink", path_content);
6922 free(path_content);
6923 free(ondisk_path);
6924 free(new_staged_blob_id);
6925 return err;
6928 const struct got_error *
6929 got_worktree_stage(struct got_worktree *worktree,
6930 struct got_pathlist_head *paths,
6931 got_worktree_status_cb status_cb, void *status_arg,
6932 got_worktree_patch_cb patch_cb, void *patch_arg,
6933 struct got_repository *repo)
6935 const struct got_error *err = NULL, *sync_err, *unlockerr;
6936 struct got_pathlist_entry *pe;
6937 struct got_fileindex *fileindex = NULL;
6938 char *fileindex_path = NULL;
6939 struct got_reference *head_ref = NULL;
6940 struct got_object_id *head_commit_id = NULL;
6941 struct check_stage_ok_arg oka;
6942 struct stage_path_arg spa;
6944 err = lock_worktree(worktree, LOCK_EX);
6945 if (err)
6946 return err;
6948 err = got_ref_open(&head_ref, repo,
6949 got_worktree_get_head_ref_name(worktree), 0);
6950 if (err)
6951 goto done;
6952 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6953 if (err)
6954 goto done;
6955 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6956 if (err)
6957 goto done;
6959 /* Check pre-conditions before staging anything. */
6960 oka.head_commit_id = head_commit_id;
6961 oka.worktree = worktree;
6962 oka.fileindex = fileindex;
6963 oka.repo = repo;
6964 oka.have_changes = 0;
6965 TAILQ_FOREACH(pe, paths, entry) {
6966 err = worktree_status(worktree, pe->path, fileindex, repo,
6967 check_stage_ok, &oka, NULL, NULL, 0, 0);
6968 if (err)
6969 goto done;
6971 if (!oka.have_changes) {
6972 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6973 goto done;
6976 spa.worktree = worktree;
6977 spa.fileindex = fileindex;
6978 spa.repo = repo;
6979 spa.patch_cb = patch_cb;
6980 spa.patch_arg = patch_arg;
6981 spa.status_cb = status_cb;
6982 spa.status_arg = status_arg;
6983 spa.staged_something = 0;
6984 TAILQ_FOREACH(pe, paths, entry) {
6985 err = worktree_status(worktree, pe->path, fileindex, repo,
6986 stage_path, &spa, NULL, NULL, 0, 0);
6987 if (err)
6988 goto done;
6990 if (!spa.staged_something) {
6991 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6992 goto done;
6995 sync_err = sync_fileindex(fileindex, fileindex_path);
6996 if (sync_err && err == NULL)
6997 err = sync_err;
6998 done:
6999 if (head_ref)
7000 got_ref_close(head_ref);
7001 free(head_commit_id);
7002 free(fileindex_path);
7003 if (fileindex)
7004 got_fileindex_free(fileindex);
7005 unlockerr = lock_worktree(worktree, LOCK_SH);
7006 if (unlockerr && err == NULL)
7007 err = unlockerr;
7008 return err;
7011 struct unstage_path_arg {
7012 struct got_worktree *worktree;
7013 struct got_fileindex *fileindex;
7014 struct got_repository *repo;
7015 got_worktree_checkout_cb progress_cb;
7016 void *progress_arg;
7017 got_worktree_patch_cb patch_cb;
7018 void *patch_arg;
7021 static const struct got_error *
7022 create_unstaged_content(char **path_unstaged_content,
7023 char **path_new_staged_content, struct got_object_id *blob_id,
7024 struct got_object_id *staged_blob_id, const char *relpath,
7025 struct got_repository *repo,
7026 got_worktree_patch_cb patch_cb, void *patch_arg)
7028 const struct got_error *err;
7029 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7030 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7031 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7032 struct stat sb1, sb2;
7033 struct got_diff_changes *changes = NULL;
7034 struct got_diff_state *ds = NULL;
7035 struct got_diff_args *args = NULL;
7036 struct got_diff_change *change;
7037 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7038 int have_content = 0, have_rejected_content = 0;
7040 *path_unstaged_content = NULL;
7041 *path_new_staged_content = NULL;
7043 err = got_object_id_str(&label1, blob_id);
7044 if (err)
7045 return err;
7046 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7047 if (err)
7048 goto done;
7050 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7051 if (err)
7052 goto done;
7054 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7055 if (err)
7056 goto done;
7058 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7059 if (err)
7060 goto done;
7062 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7063 if (err)
7064 goto done;
7066 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7067 if (err)
7068 goto done;
7070 if (stat(path1, &sb1) == -1) {
7071 err = got_error_from_errno2("stat", path1);
7072 goto done;
7075 if (stat(path2, &sb2) == -1) {
7076 err = got_error_from_errno2("stat", path2);
7077 goto done;
7080 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7081 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7082 if (err)
7083 goto done;
7085 err = got_opentemp_named(path_unstaged_content, &outfile,
7086 "got-unstaged-content");
7087 if (err)
7088 goto done;
7089 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7090 "got-new-staged-content");
7091 if (err)
7092 goto done;
7094 if (fseek(f1, 0L, SEEK_SET) == -1) {
7095 err = got_ferror(f1, GOT_ERR_IO);
7096 goto done;
7098 if (fseek(f2, 0L, SEEK_SET) == -1) {
7099 err = got_ferror(f2, GOT_ERR_IO);
7100 goto done;
7102 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7103 int choice;
7104 err = apply_or_reject_change(&choice, change, ++n,
7105 changes->nchanges, ds, args, diff_flags, relpath,
7106 f1, f2, &line_cur1, &line_cur2,
7107 outfile, rejectfile, patch_cb, patch_arg);
7108 if (err)
7109 goto done;
7110 if (choice == GOT_PATCH_CHOICE_YES)
7111 have_content = 1;
7112 else
7113 have_rejected_content = 1;
7114 if (choice == GOT_PATCH_CHOICE_QUIT)
7115 break;
7117 if (have_content || have_rejected_content)
7118 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7119 outfile, rejectfile);
7120 done:
7121 free(label1);
7122 if (blob)
7123 got_object_blob_close(blob);
7124 if (staged_blob)
7125 got_object_blob_close(staged_blob);
7126 if (f1 && fclose(f1) == EOF && err == NULL)
7127 err = got_error_from_errno2("fclose", path1);
7128 if (f2 && fclose(f2) == EOF && err == NULL)
7129 err = got_error_from_errno2("fclose", path2);
7130 if (outfile && fclose(outfile) == EOF && err == NULL)
7131 err = got_error_from_errno2("fclose", *path_unstaged_content);
7132 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7133 err = got_error_from_errno2("fclose", *path_new_staged_content);
7134 if (path1 && unlink(path1) == -1 && err == NULL)
7135 err = got_error_from_errno2("unlink", path1);
7136 if (path2 && unlink(path2) == -1 && err == NULL)
7137 err = got_error_from_errno2("unlink", path2);
7138 if (err || !have_content) {
7139 if (*path_unstaged_content &&
7140 unlink(*path_unstaged_content) == -1 && err == NULL)
7141 err = got_error_from_errno2("unlink",
7142 *path_unstaged_content);
7143 free(*path_unstaged_content);
7144 *path_unstaged_content = NULL;
7146 if (err || !have_rejected_content) {
7147 if (*path_new_staged_content &&
7148 unlink(*path_new_staged_content) == -1 && err == NULL)
7149 err = got_error_from_errno2("unlink",
7150 *path_new_staged_content);
7151 free(*path_new_staged_content);
7152 *path_new_staged_content = NULL;
7154 free(args);
7155 if (ds) {
7156 got_diff_state_free(ds);
7157 free(ds);
7159 if (changes)
7160 got_diff_free_changes(changes);
7161 free(path1);
7162 free(path2);
7163 return err;
7166 static const struct got_error *
7167 unstage_path(void *arg, unsigned char status,
7168 unsigned char staged_status, const char *relpath,
7169 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7170 struct got_object_id *commit_id, int dirfd, const char *de_name)
7172 const struct got_error *err = NULL;
7173 struct unstage_path_arg *a = arg;
7174 struct got_fileindex_entry *ie;
7175 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7176 char *ondisk_path = NULL, *path_unstaged_content = NULL;
7177 char *path_new_staged_content = NULL;
7178 char *id_str = NULL, *label_orig = NULL;
7179 int local_changes_subsumed;
7180 struct stat sb;
7182 if (staged_status != GOT_STATUS_ADD &&
7183 staged_status != GOT_STATUS_MODIFY &&
7184 staged_status != GOT_STATUS_DELETE)
7185 return NULL;
7187 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7188 if (ie == NULL)
7189 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7191 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7192 == -1)
7193 return got_error_from_errno("asprintf");
7195 err = got_object_id_str(&id_str,
7196 commit_id ? commit_id : a->worktree->base_commit_id);
7197 if (err)
7198 goto done;
7199 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7200 id_str) == -1) {
7201 err = got_error_from_errno("asprintf");
7202 goto done;
7205 switch (staged_status) {
7206 case GOT_STATUS_MODIFY:
7207 err = got_object_open_as_blob(&blob_base, a->repo,
7208 blob_id, 8192);
7209 if (err)
7210 break;
7211 /* fall through */
7212 case GOT_STATUS_ADD:
7213 if (a->patch_cb) {
7214 if (staged_status == GOT_STATUS_ADD) {
7215 int choice = GOT_PATCH_CHOICE_NONE;
7216 err = (*a->patch_cb)(&choice, a->patch_arg,
7217 staged_status, ie->path, NULL, 1, 1);
7218 if (err)
7219 break;
7220 if (choice != GOT_PATCH_CHOICE_YES)
7221 break;
7222 } else {
7223 err = create_unstaged_content(
7224 &path_unstaged_content,
7225 &path_new_staged_content, blob_id,
7226 staged_blob_id, ie->path, a->repo,
7227 a->patch_cb, a->patch_arg);
7228 if (err || path_unstaged_content == NULL)
7229 break;
7230 if (path_new_staged_content) {
7231 err = got_object_blob_create(
7232 &staged_blob_id,
7233 path_new_staged_content,
7234 a->repo);
7235 if (err)
7236 break;
7237 memcpy(ie->staged_blob_sha1,
7238 staged_blob_id->sha1,
7239 SHA1_DIGEST_LENGTH);
7241 err = merge_file(&local_changes_subsumed,
7242 a->worktree, blob_base, ondisk_path,
7243 relpath, got_fileindex_perms_to_st(ie),
7244 path_unstaged_content, label_orig,
7245 "unstaged", a->repo, a->progress_cb,
7246 a->progress_arg);
7247 if (err == NULL &&
7248 path_new_staged_content == NULL)
7249 got_fileindex_entry_stage_set(ie,
7250 GOT_FILEIDX_STAGE_NONE);
7251 break; /* Done with this file. */
7254 err = got_object_open_as_blob(&blob_staged, a->repo,
7255 staged_blob_id, 8192);
7256 if (err)
7257 break;
7258 err = merge_blob(&local_changes_subsumed, a->worktree,
7259 blob_base, ondisk_path, relpath,
7260 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7261 commit_id ? commit_id : a->worktree->base_commit_id,
7262 a->repo, a->progress_cb, a->progress_arg);
7263 if (err == NULL)
7264 got_fileindex_entry_stage_set(ie,
7265 GOT_FILEIDX_STAGE_NONE);
7266 break;
7267 case GOT_STATUS_DELETE:
7268 if (a->patch_cb) {
7269 int choice = GOT_PATCH_CHOICE_NONE;
7270 err = (*a->patch_cb)(&choice, a->patch_arg,
7271 staged_status, ie->path, NULL, 1, 1);
7272 if (err)
7273 break;
7274 if (choice == GOT_PATCH_CHOICE_NO)
7275 break;
7276 if (choice != GOT_PATCH_CHOICE_YES) {
7277 err = got_error(GOT_ERR_PATCH_CHOICE);
7278 break;
7281 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7282 err = get_file_status(&status, &sb, ie, ondisk_path,
7283 dirfd, de_name, a->repo);
7284 if (err)
7285 break;
7286 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7287 break;
7289 done:
7290 free(ondisk_path);
7291 if (path_unstaged_content &&
7292 unlink(path_unstaged_content) == -1 && err == NULL)
7293 err = got_error_from_errno2("unlink", path_unstaged_content);
7294 if (path_new_staged_content &&
7295 unlink(path_new_staged_content) == -1 && err == NULL)
7296 err = got_error_from_errno2("unlink", path_new_staged_content);
7297 free(path_unstaged_content);
7298 free(path_new_staged_content);
7299 if (blob_base)
7300 got_object_blob_close(blob_base);
7301 if (blob_staged)
7302 got_object_blob_close(blob_staged);
7303 free(id_str);
7304 free(label_orig);
7305 return err;
7308 const struct got_error *
7309 got_worktree_unstage(struct got_worktree *worktree,
7310 struct got_pathlist_head *paths,
7311 got_worktree_checkout_cb progress_cb, void *progress_arg,
7312 got_worktree_patch_cb patch_cb, void *patch_arg,
7313 struct got_repository *repo)
7315 const struct got_error *err = NULL, *sync_err, *unlockerr;
7316 struct got_pathlist_entry *pe;
7317 struct got_fileindex *fileindex = NULL;
7318 char *fileindex_path = NULL;
7319 struct unstage_path_arg upa;
7321 err = lock_worktree(worktree, LOCK_EX);
7322 if (err)
7323 return err;
7325 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7326 if (err)
7327 goto done;
7329 upa.worktree = worktree;
7330 upa.fileindex = fileindex;
7331 upa.repo = repo;
7332 upa.progress_cb = progress_cb;
7333 upa.progress_arg = progress_arg;
7334 upa.patch_cb = patch_cb;
7335 upa.patch_arg = patch_arg;
7336 TAILQ_FOREACH(pe, paths, entry) {
7337 err = worktree_status(worktree, pe->path, fileindex, repo,
7338 unstage_path, &upa, NULL, NULL, 0, 0);
7339 if (err)
7340 goto done;
7343 sync_err = sync_fileindex(fileindex, fileindex_path);
7344 if (sync_err && err == NULL)
7345 err = sync_err;
7346 done:
7347 free(fileindex_path);
7348 if (fileindex)
7349 got_fileindex_free(fileindex);
7350 unlockerr = lock_worktree(worktree, LOCK_SH);
7351 if (unlockerr && err == NULL)
7352 err = unlockerr;
7353 return err;