Blob


1 /*
2 * Copyright (c) 2018, 2019 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 = strdup(path);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno("strdup");
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(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("open", f1_path);
709 f2 = fopen(f2_path, "r");
710 if (f2 == NULL) {
711 err = got_error_from_errno2("open", 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 (chmod(merged_path, st_mode) != 0) {
806 err = got_error_from_errno2("chmod", 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 unlink(merged_path);
814 goto done;
817 done:
818 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
819 err = got_error_from_errno("close");
820 if (f_orig && fclose(f_orig) != 0 && err == NULL)
821 err = got_error_from_errno("fclose");
822 free(merged_path);
823 free(base_path);
824 if (blob_orig_path) {
825 unlink(blob_orig_path);
826 free(blob_orig_path);
828 return err;
831 /*
832 * Perform a 3-way merge where blob_orig acts as the common ancestor,
833 * blob_deriv acts as the first derived version, and the file on disk
834 * acts as the second derived version.
835 */
836 static const struct got_error *
837 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
838 struct got_blob_object *blob_orig, const char *ondisk_path,
839 const char *path, uint16_t st_mode, const char *label_orig,
840 struct got_blob_object *blob_deriv,
841 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
842 got_worktree_checkout_cb progress_cb, void *progress_arg)
844 const struct got_error *err = NULL;
845 FILE *f_deriv = NULL;
846 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
847 char *label_deriv = NULL, *parent;
849 *local_changes_subsumed = 0;
851 parent = dirname(ondisk_path);
852 if (parent == NULL)
853 return got_error_from_errno2("dirname", ondisk_path);
855 free(base_path);
856 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
857 err = got_error_from_errno("asprintf");
858 base_path = NULL;
859 goto done;
862 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
863 if (err)
864 goto done;
865 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
866 blob_deriv);
867 if (err)
868 goto done;
870 err = got_object_id_str(&id_str, deriv_base_commit_id);
871 if (err)
872 goto done;
873 if (asprintf(&label_deriv, "%s: commit %s",
874 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
875 err = got_error_from_errno("asprintf");
876 goto done;
879 err = merge_file(local_changes_subsumed, worktree, blob_orig,
880 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
881 label_deriv, repo, progress_cb, progress_arg);
882 done:
883 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
884 err = got_error_from_errno("fclose");
885 free(base_path);
886 if (blob_deriv_path) {
887 unlink(blob_deriv_path);
888 free(blob_deriv_path);
890 free(id_str);
891 free(label_deriv);
892 return err;
895 static const struct got_error *
896 update_blob_fileindex_entry(struct got_worktree *worktree,
897 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
898 const char *ondisk_path, const char *path, struct got_blob_object *blob,
899 int update_timestamps)
901 const struct got_error *err = NULL;
903 if (ie == NULL)
904 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
905 if (ie)
906 err = got_fileindex_entry_update(ie, ondisk_path,
907 blob->id.sha1, worktree->base_commit_id->sha1,
908 update_timestamps);
909 else {
910 struct got_fileindex_entry *new_ie;
911 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
912 path, blob->id.sha1, worktree->base_commit_id->sha1);
913 if (!err)
914 err = got_fileindex_entry_add(fileindex, new_ie);
916 return err;
919 static mode_t
920 get_ondisk_perms(int executable, mode_t st_mode)
922 mode_t xbits = S_IXUSR;
924 if (executable) {
925 /* Map read bits to execute bits. */
926 if (st_mode & S_IRGRP)
927 xbits |= S_IXGRP;
928 if (st_mode & S_IROTH)
929 xbits |= S_IXOTH;
930 return st_mode | xbits;
933 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
936 static const struct got_error *
937 install_blob(struct got_worktree *worktree, const char *ondisk_path,
938 const char *path, uint16_t te_mode, uint16_t st_mode,
939 struct got_blob_object *blob, int restoring_missing_file,
940 int reverting_versioned_file, struct got_repository *repo,
941 got_worktree_checkout_cb progress_cb, void *progress_arg)
943 const struct got_error *err = NULL;
944 int fd = -1;
945 size_t len, hdrlen;
946 int update = 0;
947 char *tmppath = NULL;
949 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
950 GOT_DEFAULT_FILE_MODE);
951 if (fd == -1) {
952 if (errno == ENOENT) {
953 char *parent = dirname(path);
954 if (parent == NULL)
955 return got_error_from_errno2("dirname", path);
956 err = add_dir_on_disk(worktree, parent);
957 if (err)
958 return err;
959 fd = open(ondisk_path,
960 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
961 GOT_DEFAULT_FILE_MODE);
962 if (fd == -1)
963 return got_error_from_errno2("open",
964 ondisk_path);
965 } else if (errno == EEXIST) {
966 if (!S_ISREG(st_mode)) {
967 /* TODO file is obstructed; do something */
968 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
969 goto done;
970 } else {
971 err = got_opentemp_named_fd(&tmppath, &fd,
972 ondisk_path);
973 if (err)
974 goto done;
975 update = 1;
977 } else
978 return got_error_from_errno2("open", ondisk_path);
981 if (restoring_missing_file)
982 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
983 else if (reverting_versioned_file)
984 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
985 else
986 err = (*progress_cb)(progress_arg,
987 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
988 if (err)
989 goto done;
991 hdrlen = got_object_blob_get_hdrlen(blob);
992 do {
993 const uint8_t *buf = got_object_blob_get_read_buf(blob);
994 err = got_object_blob_read_block(&len, blob);
995 if (err)
996 break;
997 if (len > 0) {
998 /* Skip blob object header first time around. */
999 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1000 if (outlen == -1) {
1001 err = got_error_from_errno("write");
1002 goto done;
1003 } else if (outlen != len - hdrlen) {
1004 err = got_error(GOT_ERR_IO);
1005 goto done;
1007 hdrlen = 0;
1009 } while (len != 0);
1011 if (fsync(fd) != 0) {
1012 err = got_error_from_errno("fsync");
1013 goto done;
1016 if (update) {
1017 if (rename(tmppath, ondisk_path) != 0) {
1018 err = got_error_from_errno3("rename", tmppath,
1019 ondisk_path);
1020 unlink(tmppath);
1021 goto done;
1025 if (chmod(ondisk_path,
1026 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1027 err = got_error_from_errno2("chmod", ondisk_path);
1028 goto done;
1031 done:
1032 if (fd != -1 && close(fd) != 0 && err == NULL)
1033 err = got_error_from_errno("close");
1034 free(tmppath);
1035 return err;
1038 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1039 static const struct got_error *
1040 get_modified_file_content_status(unsigned char *status, FILE *f)
1042 const struct got_error *err = NULL;
1043 const char *markers[3] = {
1044 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1045 GOT_DIFF_CONFLICT_MARKER_SEP,
1046 GOT_DIFF_CONFLICT_MARKER_END
1048 int i = 0;
1049 char *line;
1050 size_t len;
1051 const char delim[3] = {'\0', '\0', '\0'};
1053 while (*status == GOT_STATUS_MODIFY) {
1054 line = fparseln(f, &len, NULL, delim, 0);
1055 if (line == NULL) {
1056 if (feof(f))
1057 break;
1058 err = got_ferror(f, GOT_ERR_IO);
1059 break;
1062 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1063 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1064 == 0)
1065 *status = GOT_STATUS_CONFLICT;
1066 else
1067 i++;
1071 return err;
1074 static int
1075 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1077 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1078 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1081 static int
1082 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1084 return !(ie->ctime_sec == sb->st_ctime &&
1085 ie->ctime_nsec == sb->st_ctimensec &&
1086 ie->mtime_sec == sb->st_mtime &&
1087 ie->mtime_nsec == sb->st_mtimensec &&
1088 ie->size == (sb->st_size & 0xffffffff) &&
1089 !xbit_differs(ie, sb->st_mode));
1092 static unsigned char
1093 get_staged_status(struct got_fileindex_entry *ie)
1095 switch (got_fileindex_entry_stage_get(ie)) {
1096 case GOT_FILEIDX_STAGE_ADD:
1097 return GOT_STATUS_ADD;
1098 case GOT_FILEIDX_STAGE_DELETE:
1099 return GOT_STATUS_DELETE;
1100 case GOT_FILEIDX_STAGE_MODIFY:
1101 return GOT_STATUS_MODIFY;
1102 default:
1103 return GOT_STATUS_NO_CHANGE;
1107 static const struct got_error *
1108 get_file_status(unsigned char *status, struct stat *sb,
1109 struct got_fileindex_entry *ie, const char *abspath,
1110 int dirfd, const char *de_name, struct got_repository *repo)
1112 const struct got_error *err = NULL;
1113 struct got_object_id id;
1114 size_t hdrlen;
1115 int fd = -1;
1116 FILE *f = NULL;
1117 uint8_t fbuf[8192];
1118 struct got_blob_object *blob = NULL;
1119 size_t flen, blen;
1120 unsigned char staged_status = get_staged_status(ie);
1122 *status = GOT_STATUS_NO_CHANGE;
1125 * Whenever the caller provides a directory descriptor and a
1126 * directory entry name for the file, use them! This prevents
1127 * race conditions if filesystem paths change beneath our feet.
1129 if (dirfd != -1) {
1130 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1131 err = got_error_from_errno2("fstatat", abspath);
1132 goto done;
1134 } else {
1135 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1136 if (fd == -1 && errno != ENOENT)
1137 return got_error_from_errno2("open", abspath);
1138 if (fd == -1 || fstat(fd, sb) == -1) {
1139 if (errno == ENOENT) {
1140 if (got_fileindex_entry_has_file_on_disk(ie))
1141 *status = GOT_STATUS_MISSING;
1142 else
1143 *status = GOT_STATUS_DELETE;
1144 goto done;
1146 err = got_error_from_errno2("fstat", abspath);
1147 goto done;
1151 if (!S_ISREG(sb->st_mode)) {
1152 *status = GOT_STATUS_OBSTRUCTED;
1153 goto done;
1156 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1157 *status = GOT_STATUS_DELETE;
1158 goto done;
1159 } else if (!got_fileindex_entry_has_blob(ie) &&
1160 staged_status != GOT_STATUS_ADD) {
1161 *status = GOT_STATUS_ADD;
1162 goto done;
1165 if (!stat_info_differs(ie, sb))
1166 goto done;
1168 if (staged_status == GOT_STATUS_MODIFY ||
1169 staged_status == GOT_STATUS_ADD)
1170 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1171 else
1172 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1174 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1175 if (err)
1176 goto done;
1178 if (dirfd != -1) {
1179 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1180 if (fd == -1 && errno != ENOENT)
1181 return got_error_from_errno2("openat", abspath);
1184 f = fdopen(fd, "r");
1185 if (f == NULL) {
1186 err = got_error_from_errno2("fopen", abspath);
1187 goto done;
1189 fd = -1;
1190 hdrlen = got_object_blob_get_hdrlen(blob);
1191 for (;;) {
1192 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1193 err = got_object_blob_read_block(&blen, blob);
1194 if (err)
1195 goto done;
1196 /* Skip length of blob object header first time around. */
1197 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1198 if (flen == 0 && ferror(f)) {
1199 err = got_error_from_errno("fread");
1200 goto done;
1202 if (blen == 0) {
1203 if (flen != 0)
1204 *status = GOT_STATUS_MODIFY;
1205 break;
1206 } else if (flen == 0) {
1207 if (blen != 0)
1208 *status = GOT_STATUS_MODIFY;
1209 break;
1210 } else if (blen - hdrlen == flen) {
1211 /* Skip blob object header first time around. */
1212 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1213 *status = GOT_STATUS_MODIFY;
1214 break;
1216 } else {
1217 *status = GOT_STATUS_MODIFY;
1218 break;
1220 hdrlen = 0;
1223 if (*status == GOT_STATUS_MODIFY) {
1224 rewind(f);
1225 err = get_modified_file_content_status(status, f);
1226 } else if (xbit_differs(ie, sb->st_mode))
1227 *status = GOT_STATUS_MODE_CHANGE;
1228 done:
1229 if (blob)
1230 got_object_blob_close(blob);
1231 if (f != NULL && fclose(f) == EOF && err == NULL)
1232 err = got_error_from_errno2("fclose", abspath);
1233 if (fd != -1 && close(fd) == -1 && err == NULL)
1234 err = got_error_from_errno2("close", abspath);
1235 return err;
1239 * Update timestamps in the file index if a file is unmodified and
1240 * we had to run a full content comparison to find out.
1242 static const struct got_error *
1243 sync_timestamps(char *ondisk_path, unsigned char status,
1244 struct got_fileindex_entry *ie, struct stat *sb)
1246 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1247 return got_fileindex_entry_update(ie, ondisk_path,
1248 ie->blob_sha1, ie->commit_sha1, 1);
1250 return NULL;
1253 static const struct got_error *
1254 update_blob(struct got_worktree *worktree,
1255 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1256 struct got_tree_entry *te, const char *path,
1257 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1258 void *progress_arg)
1260 const struct got_error *err = NULL;
1261 struct got_blob_object *blob = NULL;
1262 char *ondisk_path;
1263 unsigned char status = GOT_STATUS_NO_CHANGE;
1264 struct stat sb;
1266 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1267 return got_error_from_errno("asprintf");
1269 if (ie) {
1270 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1271 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1272 goto done;
1274 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1275 repo);
1276 if (err)
1277 goto done;
1278 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1279 sb.st_mode = got_fileindex_perms_to_st(ie);
1280 } else
1281 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1283 if (status == GOT_STATUS_OBSTRUCTED) {
1284 err = (*progress_cb)(progress_arg, status, path);
1285 goto done;
1288 if (ie && status != GOT_STATUS_MISSING &&
1289 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1290 if (got_fileindex_entry_has_commit(ie) &&
1291 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1292 SHA1_DIGEST_LENGTH) == 0) {
1293 err = sync_timestamps(ondisk_path, status, ie, &sb);
1294 if (err)
1295 goto done;
1296 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1297 path);
1298 goto done;
1300 if (got_fileindex_entry_has_blob(ie) &&
1301 memcmp(ie->blob_sha1, te->id.sha1,
1302 SHA1_DIGEST_LENGTH) == 0) {
1303 err = sync_timestamps(ondisk_path, status, ie, &sb);
1304 goto done;
1308 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1309 if (err)
1310 goto done;
1312 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1313 int update_timestamps;
1314 struct got_blob_object *blob2 = NULL;
1315 char *label_orig = NULL;
1316 if (got_fileindex_entry_has_blob(ie)) {
1317 struct got_object_id id2;
1318 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1319 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1320 if (err)
1321 goto done;
1323 if (got_fileindex_entry_has_commit(ie)) {
1324 char id_str[SHA1_DIGEST_STRING_LENGTH];
1325 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1326 sizeof(id_str)) == NULL) {
1327 err = got_error_path(id_str,
1328 GOT_ERR_BAD_OBJ_ID_STR);
1329 goto done;
1331 if (asprintf(&label_orig, "%s: commit %s",
1332 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1333 err = got_error_from_errno("asprintf");
1334 goto done;
1337 err = merge_blob(&update_timestamps, worktree, blob2,
1338 ondisk_path, path, sb.st_mode, label_orig, blob,
1339 worktree->base_commit_id, repo,
1340 progress_cb, progress_arg);
1341 free(label_orig);
1342 if (blob2)
1343 got_object_blob_close(blob2);
1344 if (err)
1345 goto done;
1347 * Do not update timestamps of files with local changes.
1348 * Otherwise, a future status walk would treat them as
1349 * unmodified files again.
1351 err = got_fileindex_entry_update(ie, ondisk_path,
1352 blob->id.sha1, worktree->base_commit_id->sha1,
1353 update_timestamps);
1354 } else if (status == GOT_STATUS_MODE_CHANGE) {
1355 err = got_fileindex_entry_update(ie, ondisk_path,
1356 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1357 } else if (status == GOT_STATUS_DELETE) {
1358 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1359 if (err)
1360 goto done;
1361 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1362 ondisk_path, path, blob, 0);
1363 if (err)
1364 goto done;
1365 } else {
1366 err = install_blob(worktree, ondisk_path, path, te->mode,
1367 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1368 repo, progress_cb, progress_arg);
1369 if (err)
1370 goto done;
1371 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1372 ondisk_path, path, blob, 1);
1373 if (err)
1374 goto done;
1376 got_object_blob_close(blob);
1377 done:
1378 free(ondisk_path);
1379 return err;
1382 static const struct got_error *
1383 remove_ondisk_file(const char *root_path, const char *path)
1385 const struct got_error *err = NULL;
1386 char *ondisk_path = NULL;
1388 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1389 return got_error_from_errno("asprintf");
1391 if (unlink(ondisk_path) == -1) {
1392 if (errno != ENOENT)
1393 err = got_error_from_errno2("unlink", ondisk_path);
1394 } else {
1395 char *parent = dirname(ondisk_path);
1396 while (parent && strcmp(parent, root_path) != 0) {
1397 if (rmdir(parent) == -1) {
1398 if (errno != ENOTEMPTY)
1399 err = got_error_from_errno2("rmdir",
1400 parent);
1401 break;
1403 parent = dirname(parent);
1406 free(ondisk_path);
1407 return err;
1410 static const struct got_error *
1411 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1412 struct got_fileindex_entry *ie, struct got_repository *repo,
1413 got_worktree_checkout_cb progress_cb, void *progress_arg)
1415 const struct got_error *err = NULL;
1416 unsigned char status;
1417 struct stat sb;
1418 char *ondisk_path;
1420 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1421 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1423 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1424 == -1)
1425 return got_error_from_errno("asprintf");
1427 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1428 if (err)
1429 return err;
1431 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1432 status == GOT_STATUS_ADD) {
1433 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1434 if (err)
1435 return err;
1437 * Preserve the working file and change the deleted blob's
1438 * entry into a schedule-add entry.
1440 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1441 0);
1442 if (err)
1443 return err;
1444 } else {
1445 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1446 if (err)
1447 return err;
1448 if (status == GOT_STATUS_NO_CHANGE) {
1449 err = remove_ondisk_file(worktree->root_path, ie->path);
1450 if (err)
1451 return err;
1453 got_fileindex_entry_remove(fileindex, ie);
1456 return err;
1459 struct diff_cb_arg {
1460 struct got_fileindex *fileindex;
1461 struct got_worktree *worktree;
1462 struct got_repository *repo;
1463 got_worktree_checkout_cb progress_cb;
1464 void *progress_arg;
1465 got_cancel_cb cancel_cb;
1466 void *cancel_arg;
1469 static const struct got_error *
1470 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1471 struct got_tree_entry *te, const char *parent_path)
1473 struct diff_cb_arg *a = arg;
1475 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1476 return got_error(GOT_ERR_CANCELLED);
1478 return update_blob(a->worktree, a->fileindex, ie, te,
1479 ie->path, a->repo, a->progress_cb, a->progress_arg);
1482 static const struct got_error *
1483 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1485 struct diff_cb_arg *a = arg;
1487 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1488 return got_error(GOT_ERR_CANCELLED);
1490 return delete_blob(a->worktree, a->fileindex, ie,
1491 a->repo, a->progress_cb, a->progress_arg);
1494 static const struct got_error *
1495 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1497 struct diff_cb_arg *a = arg;
1498 const struct got_error *err;
1499 char *path;
1501 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1502 return got_error(GOT_ERR_CANCELLED);
1504 if (got_object_tree_entry_is_submodule(te))
1505 return NULL;
1507 if (asprintf(&path, "%s%s%s", parent_path,
1508 parent_path[0] ? "/" : "", te->name)
1509 == -1)
1510 return got_error_from_errno("asprintf");
1512 if (S_ISDIR(te->mode))
1513 err = add_dir_on_disk(a->worktree, path);
1514 else
1515 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1516 a->repo, a->progress_cb, a->progress_arg);
1518 free(path);
1519 return err;
1522 static const struct got_error *
1523 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1525 const struct got_error *err = NULL;
1526 char *uuidstr = NULL;
1527 uint32_t uuid_status;
1529 *refname = NULL;
1531 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1532 if (uuid_status != uuid_s_ok)
1533 return got_error_uuid(uuid_status, "uuid_to_string");
1535 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1536 == -1) {
1537 err = got_error_from_errno("asprintf");
1538 *refname = NULL;
1540 free(uuidstr);
1541 return err;
1544 const struct got_error *
1545 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1547 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1550 static const struct got_error *
1551 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1553 return get_ref_name(refname, worktree,
1554 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1557 static const struct got_error *
1558 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1560 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1563 static const struct got_error *
1564 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1566 return get_ref_name(refname, worktree,
1567 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1570 static const struct got_error *
1571 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1573 return get_ref_name(refname, worktree,
1574 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1577 static const struct got_error *
1578 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1580 return get_ref_name(refname, worktree,
1581 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1584 static const struct got_error *
1585 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1587 return get_ref_name(refname, worktree,
1588 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1591 static const struct got_error *
1592 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1594 return get_ref_name(refname, worktree,
1595 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1598 static const struct got_error *
1599 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1601 return get_ref_name(refname, worktree,
1602 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1605 const struct got_error *
1606 got_worktree_get_histedit_script_path(char **path,
1607 struct got_worktree *worktree)
1609 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1610 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1611 *path = NULL;
1612 return got_error_from_errno("asprintf");
1614 return NULL;
1618 * Prevent Git's garbage collector from deleting our base commit by
1619 * setting a reference to our base commit's ID.
1621 static const struct got_error *
1622 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1624 const struct got_error *err = NULL;
1625 struct got_reference *ref = NULL;
1626 char *refname;
1628 err = got_worktree_get_base_ref_name(&refname, worktree);
1629 if (err)
1630 return err;
1632 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1633 if (err)
1634 goto done;
1636 err = got_ref_write(ref, repo);
1637 done:
1638 free(refname);
1639 if (ref)
1640 got_ref_close(ref);
1641 return err;
1644 static const struct got_error *
1645 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1647 const struct got_error *err = NULL;
1649 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1650 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1651 err = got_error_from_errno("asprintf");
1652 *fileindex_path = NULL;
1654 return err;
1658 static const struct got_error *
1659 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1660 struct got_worktree *worktree)
1662 const struct got_error *err = NULL;
1663 FILE *index = NULL;
1665 *fileindex_path = NULL;
1666 *fileindex = got_fileindex_alloc();
1667 if (*fileindex == NULL)
1668 return got_error_from_errno("got_fileindex_alloc");
1670 err = get_fileindex_path(fileindex_path, worktree);
1671 if (err)
1672 goto done;
1674 index = fopen(*fileindex_path, "rb");
1675 if (index == NULL) {
1676 if (errno != ENOENT)
1677 err = got_error_from_errno2("fopen", *fileindex_path);
1678 } else {
1679 err = got_fileindex_read(*fileindex, index);
1680 if (fclose(index) != 0 && err == NULL)
1681 err = got_error_from_errno("fclose");
1683 done:
1684 if (err) {
1685 free(*fileindex_path);
1686 *fileindex_path = NULL;
1687 got_fileindex_free(*fileindex);
1688 *fileindex = NULL;
1690 return err;
1693 struct bump_base_commit_id_arg {
1694 struct got_object_id *base_commit_id;
1695 const char *path;
1696 size_t path_len;
1697 const char *entry_name;
1698 got_worktree_checkout_cb progress_cb;
1699 void *progress_arg;
1702 /* Bump base commit ID of all files within an updated part of the work tree. */
1703 static const struct got_error *
1704 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1706 const struct got_error *err;
1707 struct bump_base_commit_id_arg *a = arg;
1709 if (a->entry_name) {
1710 if (strcmp(ie->path, a->path) != 0)
1711 return NULL;
1712 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1713 return NULL;
1715 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1716 SHA1_DIGEST_LENGTH) == 0)
1717 return NULL;
1719 if (a->progress_cb) {
1720 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1721 ie->path);
1722 if (err)
1723 return err;
1725 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1726 return NULL;
1729 static const struct got_error *
1730 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1732 const struct got_error *err = NULL;
1733 char *new_fileindex_path = NULL;
1734 FILE *new_index = NULL;
1736 err = got_opentemp_named(&new_fileindex_path, &new_index,
1737 fileindex_path);
1738 if (err)
1739 goto done;
1741 err = got_fileindex_write(fileindex, new_index);
1742 if (err)
1743 goto done;
1745 if (rename(new_fileindex_path, fileindex_path) != 0) {
1746 err = got_error_from_errno3("rename", new_fileindex_path,
1747 fileindex_path);
1748 unlink(new_fileindex_path);
1750 done:
1751 if (new_index)
1752 fclose(new_index);
1753 free(new_fileindex_path);
1754 return err;
1757 static const struct got_error *
1758 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1759 struct got_object_id **tree_id, const char *wt_relpath,
1760 struct got_worktree *worktree, struct got_repository *repo)
1762 const struct got_error *err = NULL;
1763 struct got_object_id *id = NULL;
1764 char *in_repo_path = NULL;
1765 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1767 *entry_type = GOT_OBJ_TYPE_ANY;
1768 *tree_relpath = NULL;
1769 *tree_id = NULL;
1771 if (wt_relpath[0] == '\0') {
1772 /* Check out all files within the work tree. */
1773 *entry_type = GOT_OBJ_TYPE_TREE;
1774 *tree_relpath = strdup("");
1775 if (*tree_relpath == NULL) {
1776 err = got_error_from_errno("strdup");
1777 goto done;
1779 err = got_object_id_by_path(tree_id, repo,
1780 worktree->base_commit_id, worktree->path_prefix);
1781 if (err)
1782 goto done;
1783 return NULL;
1786 /* Check out a subset of files in the work tree. */
1788 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1789 is_root_wt ? "" : "/", wt_relpath) == -1) {
1790 err = got_error_from_errno("asprintf");
1791 goto done;
1794 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1795 in_repo_path);
1796 if (err)
1797 goto done;
1799 free(in_repo_path);
1800 in_repo_path = NULL;
1802 err = got_object_get_type(entry_type, repo, id);
1803 if (err)
1804 goto done;
1806 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1807 /* Check out a single file. */
1808 if (strchr(wt_relpath, '/') == NULL) {
1809 /* Check out a single file in work tree's root dir. */
1810 in_repo_path = strdup(worktree->path_prefix);
1811 if (in_repo_path == NULL) {
1812 err = got_error_from_errno("strdup");
1813 goto done;
1815 *tree_relpath = strdup("");
1816 if (*tree_relpath == NULL) {
1817 err = got_error_from_errno("strdup");
1818 goto done;
1820 } else {
1821 /* Check out a single file in a subdirectory. */
1822 err = got_path_dirname(tree_relpath, wt_relpath);
1823 if (err)
1824 return err;
1825 if (asprintf(&in_repo_path, "%s%s%s",
1826 worktree->path_prefix, is_root_wt ? "" : "/",
1827 *tree_relpath) == -1) {
1828 err = got_error_from_errno("asprintf");
1829 goto done;
1832 err = got_object_id_by_path(tree_id, repo,
1833 worktree->base_commit_id, in_repo_path);
1834 } else {
1835 /* Check out all files within a subdirectory. */
1836 *tree_id = got_object_id_dup(id);
1837 if (*tree_id == NULL) {
1838 err = got_error_from_errno("got_object_id_dup");
1839 goto done;
1841 *tree_relpath = strdup(wt_relpath);
1842 if (*tree_relpath == NULL) {
1843 err = got_error_from_errno("strdup");
1844 goto done;
1847 done:
1848 free(id);
1849 free(in_repo_path);
1850 if (err) {
1851 *entry_type = GOT_OBJ_TYPE_ANY;
1852 free(*tree_relpath);
1853 *tree_relpath = NULL;
1854 free(*tree_id);
1855 *tree_id = NULL;
1857 return err;
1860 static const struct got_error *
1861 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1862 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1863 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1864 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1866 const struct got_error *err = NULL;
1867 struct got_commit_object *commit = NULL;
1868 struct got_tree_object *tree = NULL;
1869 struct got_fileindex_diff_tree_cb diff_cb;
1870 struct diff_cb_arg arg;
1872 err = ref_base_commit(worktree, repo);
1873 if (err)
1874 goto done;
1876 err = got_object_open_as_commit(&commit, repo,
1877 worktree->base_commit_id);
1878 if (err)
1879 goto done;
1881 err = got_object_open_as_tree(&tree, repo, tree_id);
1882 if (err)
1883 goto done;
1885 if (entry_name &&
1886 got_object_tree_find_entry(tree, entry_name) == NULL) {
1887 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1888 goto done;
1891 diff_cb.diff_old_new = diff_old_new;
1892 diff_cb.diff_old = diff_old;
1893 diff_cb.diff_new = diff_new;
1894 arg.fileindex = fileindex;
1895 arg.worktree = worktree;
1896 arg.repo = repo;
1897 arg.progress_cb = progress_cb;
1898 arg.progress_arg = progress_arg;
1899 arg.cancel_cb = cancel_cb;
1900 arg.cancel_arg = cancel_arg;
1901 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1902 entry_name, repo, &diff_cb, &arg);
1903 done:
1904 if (tree)
1905 got_object_tree_close(tree);
1906 if (commit)
1907 got_object_commit_close(commit);
1908 return err;
1911 const struct got_error *
1912 got_worktree_checkout_files(struct got_worktree *worktree,
1913 struct got_pathlist_head *paths, struct got_repository *repo,
1914 got_worktree_checkout_cb progress_cb, void *progress_arg,
1915 got_cancel_cb cancel_cb, void *cancel_arg)
1917 const struct got_error *err = NULL, *sync_err, *unlockerr;
1918 struct got_commit_object *commit = NULL;
1919 struct got_tree_object *tree = NULL;
1920 struct got_fileindex *fileindex = NULL;
1921 char *fileindex_path = NULL;
1922 struct got_pathlist_entry *pe;
1923 struct tree_path_data {
1924 SIMPLEQ_ENTRY(tree_path_data) entry;
1925 struct got_object_id *tree_id;
1926 int entry_type;
1927 char *relpath;
1928 char *entry_name;
1929 } *tpd = NULL;
1930 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1932 SIMPLEQ_INIT(&tree_paths);
1934 err = lock_worktree(worktree, LOCK_EX);
1935 if (err)
1936 return err;
1938 /* Map all specified paths to in-repository trees. */
1939 TAILQ_FOREACH(pe, paths, entry) {
1940 tpd = malloc(sizeof(*tpd));
1941 if (tpd == NULL) {
1942 err = got_error_from_errno("malloc");
1943 goto done;
1946 err = find_tree_entry_for_checkout(&tpd->entry_type,
1947 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1948 if (err) {
1949 free(tpd);
1950 goto done;
1953 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1954 err = got_path_basename(&tpd->entry_name, pe->path);
1955 if (err) {
1956 free(tpd->relpath);
1957 free(tpd->tree_id);
1958 free(tpd);
1959 goto done;
1961 } else
1962 tpd->entry_name = NULL;
1964 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1968 * Read the file index.
1969 * Checking out files is supposed to be an idempotent operation.
1970 * If the on-disk file index is incomplete we will try to complete it.
1972 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1973 if (err)
1974 goto done;
1976 tpd = SIMPLEQ_FIRST(&tree_paths);
1977 TAILQ_FOREACH(pe, paths, entry) {
1978 struct bump_base_commit_id_arg bbc_arg;
1980 err = checkout_files(worktree, fileindex, tpd->relpath,
1981 tpd->tree_id, tpd->entry_name, repo,
1982 progress_cb, progress_arg, cancel_cb, cancel_arg);
1983 if (err)
1984 break;
1986 bbc_arg.base_commit_id = worktree->base_commit_id;
1987 bbc_arg.entry_name = tpd->entry_name;
1988 bbc_arg.path = pe->path;
1989 bbc_arg.path_len = pe->path_len;
1990 bbc_arg.progress_cb = progress_cb;
1991 bbc_arg.progress_arg = progress_arg;
1992 err = got_fileindex_for_each_entry_safe(fileindex,
1993 bump_base_commit_id, &bbc_arg);
1994 if (err)
1995 break;
1997 tpd = SIMPLEQ_NEXT(tpd, entry);
1999 sync_err = sync_fileindex(fileindex, fileindex_path);
2000 if (sync_err && err == NULL)
2001 err = sync_err;
2002 done:
2003 free(fileindex_path);
2004 if (tree)
2005 got_object_tree_close(tree);
2006 if (commit)
2007 got_object_commit_close(commit);
2008 if (fileindex)
2009 got_fileindex_free(fileindex);
2010 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2011 tpd = SIMPLEQ_FIRST(&tree_paths);
2012 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2013 free(tpd->relpath);
2014 free(tpd->tree_id);
2015 free(tpd);
2017 unlockerr = lock_worktree(worktree, LOCK_SH);
2018 if (unlockerr && err == NULL)
2019 err = unlockerr;
2020 return err;
2023 struct merge_file_cb_arg {
2024 struct got_worktree *worktree;
2025 struct got_fileindex *fileindex;
2026 got_worktree_checkout_cb progress_cb;
2027 void *progress_arg;
2028 got_cancel_cb cancel_cb;
2029 void *cancel_arg;
2030 const char *label_orig;
2031 struct got_object_id *commit_id2;
2034 static const struct got_error *
2035 merge_file_cb(void *arg, struct got_blob_object *blob1,
2036 struct got_blob_object *blob2, struct got_object_id *id1,
2037 struct got_object_id *id2, const char *path1, const char *path2,
2038 mode_t mode1, mode_t mode2, struct got_repository *repo)
2040 static const struct got_error *err = NULL;
2041 struct merge_file_cb_arg *a = arg;
2042 struct got_fileindex_entry *ie;
2043 char *ondisk_path = NULL;
2044 struct stat sb;
2045 unsigned char status;
2046 int local_changes_subsumed;
2048 if (blob1 && blob2) {
2049 ie = got_fileindex_entry_get(a->fileindex, path2,
2050 strlen(path2));
2051 if (ie == NULL)
2052 return (*a->progress_cb)(a->progress_arg,
2053 GOT_STATUS_MISSING, path2);
2055 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2056 path2) == -1)
2057 return got_error_from_errno("asprintf");
2059 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2060 repo);
2061 if (err)
2062 goto done;
2064 if (status == GOT_STATUS_DELETE) {
2065 err = (*a->progress_cb)(a->progress_arg,
2066 GOT_STATUS_MERGE, path2);
2067 goto done;
2069 if (status != GOT_STATUS_NO_CHANGE &&
2070 status != GOT_STATUS_MODIFY &&
2071 status != GOT_STATUS_CONFLICT &&
2072 status != GOT_STATUS_ADD) {
2073 err = (*a->progress_cb)(a->progress_arg, status, path2);
2074 goto done;
2077 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2078 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2079 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2080 } else if (blob1) {
2081 ie = got_fileindex_entry_get(a->fileindex, path1,
2082 strlen(path1));
2083 if (ie == NULL)
2084 return (*a->progress_cb)(a->progress_arg,
2085 GOT_STATUS_MISSING, path2);
2087 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2088 path1) == -1)
2089 return got_error_from_errno("asprintf");
2091 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2092 repo);
2093 if (err)
2094 goto done;
2096 switch (status) {
2097 case GOT_STATUS_NO_CHANGE:
2098 err = (*a->progress_cb)(a->progress_arg,
2099 GOT_STATUS_DELETE, path1);
2100 if (err)
2101 goto done;
2102 err = remove_ondisk_file(a->worktree->root_path, path1);
2103 if (err)
2104 goto done;
2105 if (ie)
2106 got_fileindex_entry_mark_deleted_from_disk(ie);
2107 break;
2108 case GOT_STATUS_DELETE:
2109 case GOT_STATUS_MISSING:
2110 err = (*a->progress_cb)(a->progress_arg,
2111 GOT_STATUS_DELETE, path1);
2112 if (err)
2113 goto done;
2114 if (ie)
2115 got_fileindex_entry_mark_deleted_from_disk(ie);
2116 break;
2117 case GOT_STATUS_ADD:
2118 case GOT_STATUS_MODIFY:
2119 case GOT_STATUS_CONFLICT:
2120 err = (*a->progress_cb)(a->progress_arg,
2121 GOT_STATUS_CANNOT_DELETE, path1);
2122 if (err)
2123 goto done;
2124 break;
2125 case GOT_STATUS_OBSTRUCTED:
2126 err = (*a->progress_cb)(a->progress_arg, status, path1);
2127 if (err)
2128 goto done;
2129 break;
2130 default:
2131 break;
2133 } else if (blob2) {
2134 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2135 path2) == -1)
2136 return got_error_from_errno("asprintf");
2137 ie = got_fileindex_entry_get(a->fileindex, path2,
2138 strlen(path2));
2139 if (ie) {
2140 err = get_file_status(&status, &sb, ie, ondisk_path,
2141 -1, NULL, repo);
2142 if (err)
2143 goto done;
2144 if (status != GOT_STATUS_NO_CHANGE &&
2145 status != GOT_STATUS_MODIFY &&
2146 status != GOT_STATUS_CONFLICT &&
2147 status != GOT_STATUS_ADD) {
2148 err = (*a->progress_cb)(a->progress_arg,
2149 status, path2);
2150 goto done;
2152 err = merge_blob(&local_changes_subsumed, a->worktree,
2153 NULL, ondisk_path, path2, sb.st_mode,
2154 a->label_orig, blob2, a->commit_id2, repo,
2155 a->progress_cb,
2156 a->progress_arg);
2157 if (status == GOT_STATUS_DELETE) {
2158 err = update_blob_fileindex_entry(a->worktree,
2159 a->fileindex, ie, ondisk_path, ie->path,
2160 blob2, 0);
2161 if (err)
2162 goto done;
2164 } else {
2165 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2166 err = install_blob(a->worktree, ondisk_path, path2,
2167 /* XXX get this from parent tree! */
2168 GOT_DEFAULT_FILE_MODE,
2169 sb.st_mode, blob2, 0, 0, repo,
2170 a->progress_cb, a->progress_arg);
2171 if (err)
2172 goto done;
2173 err = got_fileindex_entry_alloc(&ie,
2174 ondisk_path, path2, NULL, NULL);
2175 if (err)
2176 goto done;
2177 err = got_fileindex_entry_add(a->fileindex, ie);
2178 if (err) {
2179 got_fileindex_entry_free(ie);
2180 goto done;
2184 done:
2185 free(ondisk_path);
2186 return err;
2189 struct check_merge_ok_arg {
2190 struct got_worktree *worktree;
2191 struct got_repository *repo;
2194 static const struct got_error *
2195 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2197 const struct got_error *err = NULL;
2198 struct check_merge_ok_arg *a = arg;
2199 unsigned char status;
2200 struct stat sb;
2201 char *ondisk_path;
2203 /* Reject merges into a work tree with mixed base commits. */
2204 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2205 SHA1_DIGEST_LENGTH))
2206 return got_error(GOT_ERR_MIXED_COMMITS);
2208 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2209 == -1)
2210 return got_error_from_errno("asprintf");
2212 /* Reject merges into a work tree with conflicted files. */
2213 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2214 if (err)
2215 return err;
2216 if (status == GOT_STATUS_CONFLICT)
2217 return got_error(GOT_ERR_CONFLICTS);
2219 return NULL;
2222 static const struct got_error *
2223 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2224 const char *fileindex_path, struct got_object_id *commit_id1,
2225 struct got_object_id *commit_id2, struct got_repository *repo,
2226 got_worktree_checkout_cb progress_cb, void *progress_arg,
2227 got_cancel_cb cancel_cb, void *cancel_arg)
2229 const struct got_error *err = NULL, *sync_err;
2230 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2231 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2232 struct merge_file_cb_arg arg;
2233 char *label_orig = NULL;
2235 if (commit_id1) {
2236 char *id_str;
2238 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2239 worktree->path_prefix);
2240 if (err)
2241 goto done;
2243 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2244 if (err)
2245 goto done;
2247 err = got_object_id_str(&id_str, commit_id1);
2248 if (err)
2249 goto done;
2251 if (asprintf(&label_orig, "%s: commit %s",
2252 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2253 err = got_error_from_errno("asprintf");
2254 free(id_str);
2255 goto done;
2257 free(id_str);
2260 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2261 worktree->path_prefix);
2262 if (err)
2263 goto done;
2265 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2266 if (err)
2267 goto done;
2269 arg.worktree = worktree;
2270 arg.fileindex = fileindex;
2271 arg.progress_cb = progress_cb;
2272 arg.progress_arg = progress_arg;
2273 arg.cancel_cb = cancel_cb;
2274 arg.cancel_arg = cancel_arg;
2275 arg.label_orig = label_orig;
2276 arg.commit_id2 = commit_id2;
2277 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2278 sync_err = sync_fileindex(fileindex, fileindex_path);
2279 if (sync_err && err == NULL)
2280 err = sync_err;
2281 done:
2282 if (tree1)
2283 got_object_tree_close(tree1);
2284 if (tree2)
2285 got_object_tree_close(tree2);
2286 free(label_orig);
2287 return err;
2290 const struct got_error *
2291 got_worktree_merge_files(struct got_worktree *worktree,
2292 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2293 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2294 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2296 const struct got_error *err, *unlockerr;
2297 char *fileindex_path = NULL;
2298 struct got_fileindex *fileindex = NULL;
2299 struct check_merge_ok_arg mok_arg;
2301 err = lock_worktree(worktree, LOCK_EX);
2302 if (err)
2303 return err;
2305 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2306 if (err)
2307 goto done;
2309 mok_arg.worktree = worktree;
2310 mok_arg.repo = repo;
2311 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2312 &mok_arg);
2313 if (err)
2314 goto done;
2316 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2317 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2318 done:
2319 if (fileindex)
2320 got_fileindex_free(fileindex);
2321 free(fileindex_path);
2322 unlockerr = lock_worktree(worktree, LOCK_SH);
2323 if (unlockerr && err == NULL)
2324 err = unlockerr;
2325 return err;
2328 struct diff_dir_cb_arg {
2329 struct got_fileindex *fileindex;
2330 struct got_worktree *worktree;
2331 const char *status_path;
2332 size_t status_path_len;
2333 struct got_repository *repo;
2334 got_worktree_status_cb status_cb;
2335 void *status_arg;
2336 got_cancel_cb cancel_cb;
2337 void *cancel_arg;
2338 /* A pathlist containing per-directory pathlists of ignore patterns. */
2339 struct got_pathlist_head ignores;
2340 int report_unchanged;
2343 static const struct got_error *
2344 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2345 int dirfd, const char *de_name,
2346 got_worktree_status_cb status_cb, void *status_arg,
2347 struct got_repository *repo, int report_unchanged)
2349 const struct got_error *err = NULL;
2350 unsigned char status = GOT_STATUS_NO_CHANGE;
2351 unsigned char staged_status = get_staged_status(ie);
2352 struct stat sb;
2353 struct got_object_id blob_id, commit_id, staged_blob_id;
2354 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2355 struct got_object_id *staged_blob_idp = NULL;
2357 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2358 if (err)
2359 return err;
2361 if (status == GOT_STATUS_NO_CHANGE &&
2362 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2363 return NULL;
2365 if (got_fileindex_entry_has_blob(ie)) {
2366 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2367 blob_idp = &blob_id;
2369 if (got_fileindex_entry_has_commit(ie)) {
2370 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2371 commit_idp = &commit_id;
2373 if (staged_status == GOT_STATUS_ADD ||
2374 staged_status == GOT_STATUS_MODIFY) {
2375 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2376 SHA1_DIGEST_LENGTH);
2377 staged_blob_idp = &staged_blob_id;
2380 return (*status_cb)(status_arg, status, staged_status,
2381 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2384 static const struct got_error *
2385 status_old_new(void *arg, struct got_fileindex_entry *ie,
2386 struct dirent *de, const char *parent_path, int dirfd)
2388 const struct got_error *err = NULL;
2389 struct diff_dir_cb_arg *a = arg;
2390 char *abspath;
2392 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2393 return got_error(GOT_ERR_CANCELLED);
2395 if (got_path_cmp(parent_path, a->status_path,
2396 strlen(parent_path), a->status_path_len) != 0 &&
2397 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2398 return NULL;
2400 if (parent_path[0]) {
2401 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2402 parent_path, de->d_name) == -1)
2403 return got_error_from_errno("asprintf");
2404 } else {
2405 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2406 de->d_name) == -1)
2407 return got_error_from_errno("asprintf");
2410 err = report_file_status(ie, abspath, dirfd, de->d_name,
2411 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2412 free(abspath);
2413 return err;
2416 static const struct got_error *
2417 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2419 struct diff_dir_cb_arg *a = arg;
2420 struct got_object_id blob_id, commit_id;
2421 unsigned char status;
2423 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2424 return got_error(GOT_ERR_CANCELLED);
2426 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2427 return NULL;
2429 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2430 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2431 if (got_fileindex_entry_has_file_on_disk(ie))
2432 status = GOT_STATUS_MISSING;
2433 else
2434 status = GOT_STATUS_DELETE;
2435 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2436 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2439 void
2440 free_ignorelist(struct got_pathlist_head *ignorelist)
2442 struct got_pathlist_entry *pe;
2444 TAILQ_FOREACH(pe, ignorelist, entry)
2445 free((char *)pe->path);
2446 got_pathlist_free(ignorelist);
2449 void
2450 free_ignores(struct got_pathlist_head *ignores)
2452 struct got_pathlist_entry *pe;
2454 TAILQ_FOREACH(pe, ignores, entry) {
2455 struct got_pathlist_head *ignorelist = pe->data;
2456 free_ignorelist(ignorelist);
2457 free((char *)pe->path);
2459 got_pathlist_free(ignores);
2462 static const struct got_error *
2463 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2465 const struct got_error *err = NULL;
2466 struct got_pathlist_entry *pe = NULL;
2467 struct got_pathlist_head *ignorelist;
2468 char *line = NULL, *pattern, *dirpath = NULL;
2469 size_t linesize = 0;
2470 ssize_t linelen;
2472 ignorelist = calloc(1, sizeof(*ignorelist));
2473 if (ignorelist == NULL)
2474 return got_error_from_errno("calloc");
2475 TAILQ_INIT(ignorelist);
2477 while ((linelen = getline(&line, &linesize, f)) != -1) {
2478 if (linelen > 0 && line[linelen - 1] == '\n')
2479 line[linelen - 1] = '\0';
2481 /* Git's ignores may contain comments. */
2482 if (line[0] == '#')
2483 continue;
2485 /* Git's negated patterns are not (yet?) supported. */
2486 if (line[0] == '!')
2487 continue;
2489 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2490 line) == -1) {
2491 err = got_error_from_errno("asprintf");
2492 goto done;
2494 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2495 if (err)
2496 goto done;
2498 if (ferror(f)) {
2499 err = got_error_from_errno("getline");
2500 goto done;
2503 dirpath = strdup(path);
2504 if (dirpath == NULL) {
2505 err = got_error_from_errno("strdup");
2506 goto done;
2508 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2509 done:
2510 free(line);
2511 if (err || pe == NULL) {
2512 free(dirpath);
2513 free_ignorelist(ignorelist);
2515 return err;
2518 int
2519 match_ignores(struct got_pathlist_head *ignores, const char *path)
2521 struct got_pathlist_entry *pe;
2523 /* Handle patterns which match in all directories. */
2524 TAILQ_FOREACH(pe, ignores, entry) {
2525 struct got_pathlist_head *ignorelist = pe->data;
2526 struct got_pathlist_entry *pi;
2528 TAILQ_FOREACH(pi, ignorelist, entry) {
2529 const char *p, *pattern = pi->path;
2531 if (strncmp(pattern, "**/", 3) != 0)
2532 continue;
2533 pattern += 3;
2534 p = path;
2535 while (*p) {
2536 if (fnmatch(pattern, p,
2537 FNM_PATHNAME | FNM_LEADING_DIR)) {
2538 /* Retry in next directory. */
2539 while (*p && *p != '/')
2540 p++;
2541 while (*p == '/')
2542 p++;
2543 continue;
2545 return 1;
2551 * The ignores pathlist contains ignore lists from children before
2552 * parents, so we can find the most specific ignorelist by walking
2553 * ignores backwards.
2555 pe = TAILQ_LAST(ignores, got_pathlist_head);
2556 while (pe) {
2557 if (got_path_is_child(path, pe->path, pe->path_len)) {
2558 struct got_pathlist_head *ignorelist = pe->data;
2559 struct got_pathlist_entry *pi;
2560 TAILQ_FOREACH(pi, ignorelist, entry) {
2561 const char *pattern = pi->path;
2562 int flags = FNM_LEADING_DIR;
2563 if (strstr(pattern, "/**/") == NULL)
2564 flags |= FNM_PATHNAME;
2565 if (fnmatch(pattern, path, flags))
2566 continue;
2567 return 1;
2570 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2573 return 0;
2576 static const struct got_error *
2577 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2578 const char *path, const char *ignores_filename)
2580 const struct got_error *err = NULL;
2581 char *ignorespath;
2582 FILE *ignoresfile = NULL;
2584 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2585 path[0] ? "/" : "", ignores_filename) == -1)
2586 return got_error_from_errno("asprintf");
2588 ignoresfile = fopen(ignorespath, "r");
2589 if (ignoresfile == NULL) {
2590 if (errno != ENOENT && errno != EACCES)
2591 err = got_error_from_errno2("fopen",
2592 ignorespath);
2593 } else
2594 err = read_ignores(ignores, path, ignoresfile);
2596 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2597 err = got_error_from_errno2("fclose", path);
2598 free(ignorespath);
2599 return err;
2602 static const struct got_error *
2603 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2605 const struct got_error *err = NULL;
2606 struct diff_dir_cb_arg *a = arg;
2607 char *path = NULL;
2609 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2610 return got_error(GOT_ERR_CANCELLED);
2612 /* XXX ignore symlinks for now */
2613 if (de->d_type == DT_LNK)
2614 return NULL;
2616 if (parent_path[0]) {
2617 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2618 return got_error_from_errno("asprintf");
2619 } else {
2620 path = de->d_name;
2623 if (de->d_type == DT_DIR) {
2624 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2625 ".cvsignore");
2626 if (err == NULL)
2627 err = add_ignores(&a->ignores, a->worktree->root_path,
2628 path, ".gitignore");
2630 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2631 && !match_ignores(&a->ignores, path))
2632 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2633 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2634 if (parent_path[0])
2635 free(path);
2636 return err;
2639 static const struct got_error *
2640 report_single_file_status(const char *path, const char *ondisk_path,
2641 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2642 void *status_arg, struct got_repository *repo, int report_unchanged)
2644 struct got_fileindex_entry *ie;
2645 struct stat sb;
2647 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2648 if (ie)
2649 return report_file_status(ie, ondisk_path, -1, NULL,
2650 status_cb, status_arg, repo, report_unchanged);
2652 if (lstat(ondisk_path, &sb) == -1) {
2653 if (errno != ENOENT)
2654 return got_error_from_errno2("lstat", ondisk_path);
2655 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2656 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2657 return NULL;
2660 if (S_ISREG(sb.st_mode))
2661 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2662 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2664 return NULL;
2667 static const struct got_error *
2668 worktree_status(struct got_worktree *worktree, const char *path,
2669 struct got_fileindex *fileindex, struct got_repository *repo,
2670 got_worktree_status_cb status_cb, void *status_arg,
2671 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2672 int report_unchanged)
2674 const struct got_error *err = NULL;
2675 int fd = -1;
2676 struct got_fileindex_diff_dir_cb fdiff_cb;
2677 struct diff_dir_cb_arg arg;
2678 char *ondisk_path = NULL;
2680 if (asprintf(&ondisk_path, "%s%s%s",
2681 worktree->root_path, path[0] ? "/" : "", path) == -1)
2682 return got_error_from_errno("asprintf");
2684 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2685 if (fd == -1) {
2686 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2687 err = got_error_from_errno2("open", ondisk_path);
2688 else
2689 err = report_single_file_status(path, ondisk_path,
2690 fileindex, status_cb, status_arg, repo,
2691 report_unchanged);
2692 } else {
2693 fdiff_cb.diff_old_new = status_old_new;
2694 fdiff_cb.diff_old = status_old;
2695 fdiff_cb.diff_new = status_new;
2696 arg.fileindex = fileindex;
2697 arg.worktree = worktree;
2698 arg.status_path = path;
2699 arg.status_path_len = strlen(path);
2700 arg.repo = repo;
2701 arg.status_cb = status_cb;
2702 arg.status_arg = status_arg;
2703 arg.cancel_cb = cancel_cb;
2704 arg.cancel_arg = cancel_arg;
2705 arg.report_unchanged = report_unchanged;
2706 TAILQ_INIT(&arg.ignores);
2707 if (!no_ignores) {
2708 err = add_ignores(&arg.ignores, worktree->root_path,
2709 path, ".cvsignore");
2710 if (err == NULL)
2711 err = add_ignores(&arg.ignores,
2712 worktree->root_path, path, ".gitignore");
2714 if (err == NULL)
2715 err = got_fileindex_diff_dir(fileindex, fd,
2716 worktree->root_path, path, repo, &fdiff_cb, &arg);
2717 free_ignores(&arg.ignores);
2720 if (fd != -1 && close(fd) != 0 && err == NULL)
2721 err = got_error_from_errno("close");
2722 free(ondisk_path);
2723 return err;
2726 const struct got_error *
2727 got_worktree_status(struct got_worktree *worktree,
2728 struct got_pathlist_head *paths, struct got_repository *repo,
2729 got_worktree_status_cb status_cb, void *status_arg,
2730 got_cancel_cb cancel_cb, void *cancel_arg)
2732 const struct got_error *err = NULL;
2733 char *fileindex_path = NULL;
2734 struct got_fileindex *fileindex = NULL;
2735 struct got_pathlist_entry *pe;
2737 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2738 if (err)
2739 return err;
2741 TAILQ_FOREACH(pe, paths, entry) {
2742 err = worktree_status(worktree, pe->path, fileindex, repo,
2743 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
2744 if (err)
2745 break;
2747 free(fileindex_path);
2748 got_fileindex_free(fileindex);
2749 return err;
2752 const struct got_error *
2753 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2754 const char *arg)
2756 const struct got_error *err = NULL;
2757 char *resolved, *cwd = NULL, *path = NULL;
2758 size_t len;
2760 *wt_path = NULL;
2762 resolved = realpath(arg, NULL);
2763 if (resolved == NULL) {
2764 if (errno != ENOENT)
2765 return got_error_from_errno2("realpath", arg);
2766 cwd = getcwd(NULL, 0);
2767 if (cwd == NULL)
2768 return got_error_from_errno("getcwd");
2769 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2770 err = got_error_from_errno("asprintf");
2771 goto done;
2775 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2776 strlen(got_worktree_get_root_path(worktree)))) {
2777 err = got_error(GOT_ERR_BAD_PATH);
2778 goto done;
2781 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2782 err = got_path_skip_common_ancestor(&path,
2783 got_worktree_get_root_path(worktree), resolved);
2784 if (err)
2785 goto done;
2786 } else {
2787 path = strdup("");
2788 if (path == NULL) {
2789 err = got_error_from_errno("strdup");
2790 goto done;
2794 /* XXX status walk can't deal with trailing slash! */
2795 len = strlen(path);
2796 while (len > 0 && path[len - 1] == '/') {
2797 path[len - 1] = '\0';
2798 len--;
2800 done:
2801 free(resolved);
2802 free(cwd);
2803 if (err == NULL)
2804 *wt_path = path;
2805 else
2806 free(path);
2807 return err;
2810 struct schedule_addition_args {
2811 struct got_worktree *worktree;
2812 struct got_fileindex *fileindex;
2813 got_worktree_checkout_cb progress_cb;
2814 void *progress_arg;
2815 struct got_repository *repo;
2818 static const struct got_error *
2819 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2820 const char *relpath, struct got_object_id *blob_id,
2821 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2822 int dirfd, const char *de_name)
2824 struct schedule_addition_args *a = arg;
2825 const struct got_error *err = NULL;
2826 struct got_fileindex_entry *ie;
2827 struct stat sb;
2828 char *ondisk_path;
2830 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2831 relpath) == -1)
2832 return got_error_from_errno("asprintf");
2834 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2835 if (ie) {
2836 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
2837 de_name, a->repo);
2838 if (err)
2839 goto done;
2840 /* Re-adding an existing entry is a no-op. */
2841 if (status == GOT_STATUS_ADD)
2842 goto done;
2843 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2844 if (err)
2845 goto done;
2848 if (status != GOT_STATUS_UNVERSIONED) {
2849 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2850 goto done;
2853 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2854 if (err)
2855 goto done;
2857 err = got_fileindex_entry_add(a->fileindex, ie);
2858 if (err) {
2859 got_fileindex_entry_free(ie);
2860 goto done;
2862 done:
2863 free(ondisk_path);
2864 if (err)
2865 return err;
2866 if (status == GOT_STATUS_ADD)
2867 return NULL;
2868 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2871 const struct got_error *
2872 got_worktree_schedule_add(struct got_worktree *worktree,
2873 struct got_pathlist_head *paths,
2874 got_worktree_checkout_cb progress_cb, void *progress_arg,
2875 struct got_repository *repo, int no_ignores)
2877 struct got_fileindex *fileindex = NULL;
2878 char *fileindex_path = NULL;
2879 const struct got_error *err = NULL, *sync_err, *unlockerr;
2880 struct got_pathlist_entry *pe;
2881 struct schedule_addition_args saa;
2883 err = lock_worktree(worktree, LOCK_EX);
2884 if (err)
2885 return err;
2887 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2888 if (err)
2889 goto done;
2891 saa.worktree = worktree;
2892 saa.fileindex = fileindex;
2893 saa.progress_cb = progress_cb;
2894 saa.progress_arg = progress_arg;
2895 saa.repo = repo;
2897 TAILQ_FOREACH(pe, paths, entry) {
2898 err = worktree_status(worktree, pe->path, fileindex, repo,
2899 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
2900 if (err)
2901 break;
2903 sync_err = sync_fileindex(fileindex, fileindex_path);
2904 if (sync_err && err == NULL)
2905 err = sync_err;
2906 done:
2907 free(fileindex_path);
2908 if (fileindex)
2909 got_fileindex_free(fileindex);
2910 unlockerr = lock_worktree(worktree, LOCK_SH);
2911 if (unlockerr && err == NULL)
2912 err = unlockerr;
2913 return err;
2916 struct schedule_deletion_args {
2917 struct got_worktree *worktree;
2918 struct got_fileindex *fileindex;
2919 got_worktree_delete_cb progress_cb;
2920 void *progress_arg;
2921 struct got_repository *repo;
2922 int delete_local_mods;
2925 static const struct got_error *
2926 schedule_for_deletion(void *arg, unsigned char status,
2927 unsigned char staged_status, const char *relpath,
2928 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2929 struct got_object_id *commit_id, int dirfd, const char *de_name)
2931 struct schedule_deletion_args *a = arg;
2932 const struct got_error *err = NULL;
2933 struct got_fileindex_entry *ie = NULL;
2934 struct stat sb;
2935 char *ondisk_path;
2937 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2938 if (ie == NULL)
2939 return got_error(GOT_ERR_BAD_PATH);
2941 staged_status = get_staged_status(ie);
2942 if (staged_status != GOT_STATUS_NO_CHANGE) {
2943 if (staged_status == GOT_STATUS_DELETE)
2944 return NULL;
2945 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2948 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2949 relpath) == -1)
2950 return got_error_from_errno("asprintf");
2952 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
2953 a->repo);
2954 if (err)
2955 goto done;
2957 if (status != GOT_STATUS_NO_CHANGE) {
2958 if (status == GOT_STATUS_DELETE)
2959 goto done;
2960 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
2961 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2962 goto done;
2964 if (status != GOT_STATUS_MODIFY &&
2965 status != GOT_STATUS_MISSING) {
2966 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2967 goto done;
2971 if (status != GOT_STATUS_MISSING) {
2972 if (dirfd != -1) {
2973 if (unlinkat(dirfd, de_name, 0) != 0) {
2974 err = got_error_from_errno2("unlinkat",
2975 ondisk_path);
2976 goto done;
2978 } else if (unlink(ondisk_path) != 0) {
2979 err = got_error_from_errno2("unlink", ondisk_path);
2980 goto done;
2984 got_fileindex_entry_mark_deleted_from_disk(ie);
2985 done:
2986 free(ondisk_path);
2987 if (err)
2988 return err;
2989 if (status == GOT_STATUS_DELETE)
2990 return NULL;
2991 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
2992 staged_status, relpath);
2995 const struct got_error *
2996 got_worktree_schedule_delete(struct got_worktree *worktree,
2997 struct got_pathlist_head *paths, int delete_local_mods,
2998 got_worktree_delete_cb progress_cb, void *progress_arg,
2999 struct got_repository *repo)
3001 struct got_fileindex *fileindex = NULL;
3002 char *fileindex_path = NULL;
3003 const struct got_error *err = NULL, *sync_err, *unlockerr;
3004 struct got_pathlist_entry *pe;
3005 struct schedule_deletion_args sda;
3007 err = lock_worktree(worktree, LOCK_EX);
3008 if (err)
3009 return err;
3011 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3012 if (err)
3013 goto done;
3015 sda.worktree = worktree;
3016 sda.fileindex = fileindex;
3017 sda.progress_cb = progress_cb;
3018 sda.progress_arg = progress_arg;
3019 sda.repo = repo;
3020 sda.delete_local_mods = delete_local_mods;
3022 TAILQ_FOREACH(pe, paths, entry) {
3023 err = worktree_status(worktree, pe->path, fileindex, repo,
3024 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3025 if (err)
3026 break;
3028 sync_err = sync_fileindex(fileindex, fileindex_path);
3029 if (sync_err && err == NULL)
3030 err = sync_err;
3031 done:
3032 free(fileindex_path);
3033 if (fileindex)
3034 got_fileindex_free(fileindex);
3035 unlockerr = lock_worktree(worktree, LOCK_SH);
3036 if (unlockerr && err == NULL)
3037 err = unlockerr;
3038 return err;
3041 static const struct got_error *
3042 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3044 const struct got_error *err = NULL;
3045 char *line = NULL;
3046 size_t linesize = 0, n;
3047 ssize_t linelen;
3049 linelen = getline(&line, &linesize, infile);
3050 if (linelen == -1) {
3051 if (ferror(infile)) {
3052 err = got_error_from_errno("getline");
3053 goto done;
3055 return NULL;
3057 if (outfile) {
3058 n = fwrite(line, 1, linelen, outfile);
3059 if (n != linelen) {
3060 err = got_ferror(outfile, GOT_ERR_IO);
3061 goto done;
3064 if (rejectfile) {
3065 n = fwrite(line, 1, linelen, rejectfile);
3066 if (n != linelen)
3067 err = got_ferror(outfile, GOT_ERR_IO);
3069 done:
3070 free(line);
3071 return err;
3074 static const struct got_error *
3075 skip_one_line(FILE *f)
3077 char *line = NULL;
3078 size_t linesize = 0;
3079 ssize_t linelen;
3081 linelen = getline(&line, &linesize, f);
3082 if (linelen == -1) {
3083 if (ferror(f))
3084 return got_error_from_errno("getline");
3085 return NULL;
3087 free(line);
3088 return NULL;
3091 static const struct got_error *
3092 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3093 int start_old, int end_old, int start_new, int end_new,
3094 FILE *outfile, FILE *rejectfile)
3096 const struct got_error *err;
3098 /* Copy old file's lines leading up to patch. */
3099 while (!feof(f1) && *line_cur1 < start_old) {
3100 err = copy_one_line(f1, outfile, NULL);
3101 if (err)
3102 return err;
3103 (*line_cur1)++;
3105 /* Skip new file's lines leading up to patch. */
3106 while (!feof(f2) && *line_cur2 < start_new) {
3107 if (rejectfile)
3108 err = copy_one_line(f2, NULL, rejectfile);
3109 else
3110 err = skip_one_line(f2);
3111 if (err)
3112 return err;
3113 (*line_cur2)++;
3115 /* Copy patched lines. */
3116 while (!feof(f2) && *line_cur2 <= end_new) {
3117 err = copy_one_line(f2, outfile, NULL);
3118 if (err)
3119 return err;
3120 (*line_cur2)++;
3122 /* Skip over old file's replaced lines. */
3123 while (!feof(f1) && *line_cur1 <= end_old) {
3124 if (rejectfile)
3125 err = copy_one_line(f1, NULL, rejectfile);
3126 else
3127 err = skip_one_line(f1);
3128 if (err)
3129 return err;
3130 (*line_cur1)++;
3133 return NULL;
3136 static const struct got_error *
3137 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3138 FILE *outfile, FILE *rejectfile)
3140 const struct got_error *err;
3142 if (outfile) {
3143 /* Copy old file's lines until EOF. */
3144 while (!feof(f1)) {
3145 err = copy_one_line(f1, outfile, NULL);
3146 if (err)
3147 return err;
3148 (*line_cur1)++;
3151 if (rejectfile) {
3152 /* Copy new file's lines until EOF. */
3153 while (!feof(f2)) {
3154 err = copy_one_line(f2, NULL, rejectfile);
3155 if (err)
3156 return err;
3157 (*line_cur2)++;
3161 return NULL;
3164 static const struct got_error *
3165 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3166 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3167 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3168 int *line_cur2, FILE *outfile, FILE *rejectfile,
3169 got_worktree_patch_cb patch_cb, void *patch_arg)
3171 const struct got_error *err = NULL;
3172 int start_old = change->cv.a;
3173 int end_old = change->cv.b;
3174 int start_new = change->cv.c;
3175 int end_new = change->cv.d;
3176 long pos1, pos2;
3177 FILE *hunkfile;
3179 *choice = GOT_PATCH_CHOICE_NONE;
3181 hunkfile = got_opentemp();
3182 if (hunkfile == NULL)
3183 return got_error_from_errno("got_opentemp");
3185 pos1 = ftell(f1);
3186 pos2 = ftell(f2);
3188 /* XXX TODO needs error checking */
3189 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3191 if (fseek(f1, pos1, SEEK_SET) == -1) {
3192 err = got_ferror(f1, GOT_ERR_IO);
3193 goto done;
3195 if (fseek(f2, pos2, SEEK_SET) == -1) {
3196 err = got_ferror(f1, GOT_ERR_IO);
3197 goto done;
3199 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3200 err = got_ferror(hunkfile, GOT_ERR_IO);
3201 goto done;
3204 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3205 hunkfile, n, nchanges);
3206 if (err)
3207 goto done;
3209 switch (*choice) {
3210 case GOT_PATCH_CHOICE_YES:
3211 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3212 end_old, start_new, end_new, outfile, rejectfile);
3213 break;
3214 case GOT_PATCH_CHOICE_NO:
3215 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3216 end_old, start_new, end_new, rejectfile, outfile);
3217 break;
3218 case GOT_PATCH_CHOICE_QUIT:
3219 break;
3220 default:
3221 err = got_error(GOT_ERR_PATCH_CHOICE);
3222 break;
3224 done:
3225 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3226 err = got_error_from_errno("fclose");
3227 return err;
3230 struct revert_file_args {
3231 struct got_worktree *worktree;
3232 struct got_fileindex *fileindex;
3233 got_worktree_checkout_cb progress_cb;
3234 void *progress_arg;
3235 got_worktree_patch_cb patch_cb;
3236 void *patch_arg;
3237 struct got_repository *repo;
3240 static const struct got_error *
3241 create_patched_content(char **path_outfile, int reverse_patch,
3242 struct got_object_id *blob_id, const char *path2,
3243 int dirfd2, const char *de_name2,
3244 const char *relpath, struct got_repository *repo,
3245 got_worktree_patch_cb patch_cb, void *patch_arg)
3247 const struct got_error *err;
3248 struct got_blob_object *blob = NULL;
3249 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3250 int fd2 = -1;
3251 char *path1 = NULL, *id_str = NULL;
3252 struct stat sb1, sb2;
3253 struct got_diff_changes *changes = NULL;
3254 struct got_diff_state *ds = NULL;
3255 struct got_diff_args *args = NULL;
3256 struct got_diff_change *change;
3257 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3258 int n = 0;
3260 *path_outfile = NULL;
3262 err = got_object_id_str(&id_str, blob_id);
3263 if (err)
3264 return err;
3266 if (dirfd2 != -1) {
3267 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3268 if (fd2 == -1) {
3269 err = got_error_from_errno2("openat", path2);
3270 goto done;
3272 } else {
3273 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3274 if (fd2 == -1) {
3275 err = got_error_from_errno2("open", path2);
3276 goto done;
3279 if (fstat(fd2, &sb2) == -1) {
3280 err = got_error_from_errno2("fstat", path2);
3281 goto done;
3284 f2 = fdopen(fd2, "r");
3285 if (f2 == NULL) {
3286 err = got_error_from_errno2("fopen", path2);
3287 goto done;
3289 fd2 = -1;
3291 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3292 if (err)
3293 goto done;
3295 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3296 if (err)
3297 goto done;
3299 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3300 if (err)
3301 goto done;
3303 if (stat(path1, &sb1) == -1) {
3304 err = got_error_from_errno2("stat", path1);
3305 goto done;
3308 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3309 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3310 if (err)
3311 goto done;
3313 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3314 if (err)
3315 goto done;
3317 if (fseek(f1, 0L, SEEK_SET) == -1)
3318 return got_ferror(f1, GOT_ERR_IO);
3319 if (fseek(f2, 0L, SEEK_SET) == -1)
3320 return got_ferror(f2, GOT_ERR_IO);
3321 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3322 int choice;
3323 err = apply_or_reject_change(&choice, change, ++n,
3324 changes->nchanges, ds, args, diff_flags, relpath,
3325 f1, f2, &line_cur1, &line_cur2,
3326 reverse_patch ? NULL : outfile,
3327 reverse_patch ? outfile : NULL,
3328 patch_cb, patch_arg);
3329 if (err)
3330 goto done;
3331 if (choice == GOT_PATCH_CHOICE_YES)
3332 have_content = 1;
3333 else if (choice == GOT_PATCH_CHOICE_QUIT)
3334 break;
3336 if (have_content) {
3337 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3338 reverse_patch ? NULL : outfile,
3339 reverse_patch ? outfile : NULL);
3340 if (err)
3341 goto done;
3343 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3344 err = got_error_from_errno2("chmod", path2);
3345 goto done;
3348 done:
3349 free(id_str);
3350 if (blob)
3351 got_object_blob_close(blob);
3352 if (f1 && fclose(f1) == EOF && err == NULL)
3353 err = got_error_from_errno2("fclose", path1);
3354 if (f2 && fclose(f2) == EOF && err == NULL)
3355 err = got_error_from_errno2("fclose", path2);
3356 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3357 err = got_error_from_errno2("close", path2);
3358 if (outfile && fclose(outfile) == EOF && err == NULL)
3359 err = got_error_from_errno2("fclose", *path_outfile);
3360 if (path1 && unlink(path1) == -1 && err == NULL)
3361 err = got_error_from_errno2("unlink", path1);
3362 if (err || !have_content) {
3363 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3364 err = got_error_from_errno2("unlink", *path_outfile);
3365 free(*path_outfile);
3366 *path_outfile = NULL;
3368 free(args);
3369 if (ds) {
3370 got_diff_state_free(ds);
3371 free(ds);
3373 if (changes)
3374 got_diff_free_changes(changes);
3375 free(path1);
3376 return err;
3379 static const struct got_error *
3380 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3381 const char *relpath, struct got_object_id *blob_id,
3382 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3383 int dirfd, const char *de_name)
3385 struct revert_file_args *a = arg;
3386 const struct got_error *err = NULL;
3387 char *parent_path = NULL;
3388 struct got_fileindex_entry *ie;
3389 struct got_tree_object *tree = NULL;
3390 struct got_object_id *tree_id = NULL;
3391 const struct got_tree_entry *te = NULL;
3392 char *tree_path = NULL, *te_name;
3393 char *ondisk_path = NULL, *path_content = NULL;
3394 struct got_blob_object *blob = NULL;
3396 /* Reverting a staged deletion is a no-op. */
3397 if (status == GOT_STATUS_DELETE &&
3398 staged_status != GOT_STATUS_NO_CHANGE)
3399 return NULL;
3401 if (status == GOT_STATUS_UNVERSIONED)
3402 return (*a->progress_cb)(a->progress_arg,
3403 GOT_STATUS_UNVERSIONED, relpath);
3405 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3406 if (ie == NULL)
3407 return got_error(GOT_ERR_BAD_PATH);
3409 /* Construct in-repository path of tree which contains this blob. */
3410 err = got_path_dirname(&parent_path, ie->path);
3411 if (err) {
3412 if (err->code != GOT_ERR_BAD_PATH)
3413 goto done;
3414 parent_path = strdup("/");
3415 if (parent_path == NULL) {
3416 err = got_error_from_errno("strdup");
3417 goto done;
3420 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3421 tree_path = strdup(parent_path);
3422 if (tree_path == NULL) {
3423 err = got_error_from_errno("strdup");
3424 goto done;
3426 } else {
3427 if (got_path_is_root_dir(parent_path)) {
3428 tree_path = strdup(a->worktree->path_prefix);
3429 if (tree_path == NULL) {
3430 err = got_error_from_errno("strdup");
3431 goto done;
3433 } else {
3434 if (asprintf(&tree_path, "%s/%s",
3435 a->worktree->path_prefix, parent_path) == -1) {
3436 err = got_error_from_errno("asprintf");
3437 goto done;
3442 err = got_object_id_by_path(&tree_id, a->repo,
3443 a->worktree->base_commit_id, tree_path);
3444 if (err) {
3445 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3446 (status == GOT_STATUS_ADD ||
3447 staged_status == GOT_STATUS_ADD)))
3448 goto done;
3449 } else {
3450 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3451 if (err)
3452 goto done;
3454 te_name = basename(ie->path);
3455 if (te_name == NULL) {
3456 err = got_error_from_errno2("basename", ie->path);
3457 goto done;
3460 te = got_object_tree_find_entry(tree, te_name);
3461 if (te == NULL && status != GOT_STATUS_ADD &&
3462 staged_status != GOT_STATUS_ADD) {
3463 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3464 goto done;
3468 switch (status) {
3469 case GOT_STATUS_ADD:
3470 if (a->patch_cb) {
3471 int choice = GOT_PATCH_CHOICE_NONE;
3472 err = (*a->patch_cb)(&choice, a->patch_arg,
3473 status, ie->path, NULL, 1, 1);
3474 if (err)
3475 goto done;
3476 if (choice != GOT_PATCH_CHOICE_YES)
3477 break;
3479 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3480 ie->path);
3481 if (err)
3482 goto done;
3483 got_fileindex_entry_remove(a->fileindex, ie);
3484 break;
3485 case GOT_STATUS_DELETE:
3486 if (a->patch_cb) {
3487 int choice = GOT_PATCH_CHOICE_NONE;
3488 err = (*a->patch_cb)(&choice, a->patch_arg,
3489 status, ie->path, NULL, 1, 1);
3490 if (err)
3491 goto done;
3492 if (choice != GOT_PATCH_CHOICE_YES)
3493 break;
3495 /* fall through */
3496 case GOT_STATUS_MODIFY:
3497 case GOT_STATUS_MODE_CHANGE:
3498 case GOT_STATUS_CONFLICT:
3499 case GOT_STATUS_MISSING: {
3500 struct got_object_id id;
3501 if (staged_status == GOT_STATUS_ADD ||
3502 staged_status == GOT_STATUS_MODIFY) {
3503 memcpy(id.sha1, ie->staged_blob_sha1,
3504 SHA1_DIGEST_LENGTH);
3505 } else
3506 memcpy(id.sha1, ie->blob_sha1,
3507 SHA1_DIGEST_LENGTH);
3508 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3509 if (err)
3510 goto done;
3512 if (asprintf(&ondisk_path, "%s/%s",
3513 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3514 err = got_error_from_errno("asprintf");
3515 goto done;
3518 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3519 status == GOT_STATUS_CONFLICT)) {
3520 err = create_patched_content(&path_content, 1, &id,
3521 ondisk_path, dirfd, de_name, ie->path, a->repo,
3522 a->patch_cb, a->patch_arg);
3523 if (err || path_content == NULL)
3524 break;
3525 if (rename(path_content, ondisk_path) == -1) {
3526 err = got_error_from_errno3("rename",
3527 path_content, ondisk_path);
3528 goto done;
3530 } else {
3531 err = install_blob(a->worktree, ondisk_path, ie->path,
3532 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3533 got_fileindex_perms_to_st(ie), blob, 0, 1,
3534 a->repo, a->progress_cb, a->progress_arg);
3535 if (err)
3536 goto done;
3537 if (status == GOT_STATUS_DELETE ||
3538 status == GOT_STATUS_MODE_CHANGE) {
3539 err = update_blob_fileindex_entry(a->worktree,
3540 a->fileindex, ie, ondisk_path, ie->path,
3541 blob, 1);
3542 if (err)
3543 goto done;
3546 break;
3548 default:
3549 break;
3551 done:
3552 free(ondisk_path);
3553 free(path_content);
3554 free(parent_path);
3555 free(tree_path);
3556 if (blob)
3557 got_object_blob_close(blob);
3558 if (tree)
3559 got_object_tree_close(tree);
3560 free(tree_id);
3561 return err;
3564 const struct got_error *
3565 got_worktree_revert(struct got_worktree *worktree,
3566 struct got_pathlist_head *paths,
3567 got_worktree_checkout_cb progress_cb, void *progress_arg,
3568 got_worktree_patch_cb patch_cb, void *patch_arg,
3569 struct got_repository *repo)
3571 struct got_fileindex *fileindex = NULL;
3572 char *fileindex_path = NULL;
3573 const struct got_error *err = NULL, *unlockerr = NULL;
3574 const struct got_error *sync_err = NULL;
3575 struct got_pathlist_entry *pe;
3576 struct revert_file_args rfa;
3578 err = lock_worktree(worktree, LOCK_EX);
3579 if (err)
3580 return err;
3582 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3583 if (err)
3584 goto done;
3586 rfa.worktree = worktree;
3587 rfa.fileindex = fileindex;
3588 rfa.progress_cb = progress_cb;
3589 rfa.progress_arg = progress_arg;
3590 rfa.patch_cb = patch_cb;
3591 rfa.patch_arg = patch_arg;
3592 rfa.repo = repo;
3593 TAILQ_FOREACH(pe, paths, entry) {
3594 err = worktree_status(worktree, pe->path, fileindex, repo,
3595 revert_file, &rfa, NULL, NULL, 0, 0);
3596 if (err)
3597 break;
3599 sync_err = sync_fileindex(fileindex, fileindex_path);
3600 if (sync_err && err == NULL)
3601 err = sync_err;
3602 done:
3603 free(fileindex_path);
3604 if (fileindex)
3605 got_fileindex_free(fileindex);
3606 unlockerr = lock_worktree(worktree, LOCK_SH);
3607 if (unlockerr && err == NULL)
3608 err = unlockerr;
3609 return err;
3612 static void
3613 free_commitable(struct got_commitable *ct)
3615 free(ct->path);
3616 free(ct->in_repo_path);
3617 free(ct->ondisk_path);
3618 free(ct->blob_id);
3619 free(ct->base_blob_id);
3620 free(ct->staged_blob_id);
3621 free(ct->base_commit_id);
3622 free(ct);
3625 struct collect_commitables_arg {
3626 struct got_pathlist_head *commitable_paths;
3627 struct got_repository *repo;
3628 struct got_worktree *worktree;
3629 int have_staged_files;
3632 static const struct got_error *
3633 collect_commitables(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 collect_commitables_arg *a = arg;
3639 const struct got_error *err = NULL;
3640 struct got_commitable *ct = NULL;
3641 struct got_pathlist_entry *new = NULL;
3642 char *parent_path = NULL, *path = NULL;
3643 struct stat sb;
3645 if (a->have_staged_files) {
3646 if (staged_status != GOT_STATUS_MODIFY &&
3647 staged_status != GOT_STATUS_ADD &&
3648 staged_status != GOT_STATUS_DELETE)
3649 return NULL;
3650 } else {
3651 if (status == GOT_STATUS_CONFLICT)
3652 return got_error(GOT_ERR_COMMIT_CONFLICT);
3654 if (status != GOT_STATUS_MODIFY &&
3655 status != GOT_STATUS_MODE_CHANGE &&
3656 status != GOT_STATUS_ADD &&
3657 status != GOT_STATUS_DELETE)
3658 return NULL;
3661 if (asprintf(&path, "/%s", relpath) == -1) {
3662 err = got_error_from_errno("asprintf");
3663 goto done;
3665 if (strcmp(path, "/") == 0) {
3666 parent_path = strdup("");
3667 if (parent_path == NULL)
3668 return got_error_from_errno("strdup");
3669 } else {
3670 err = got_path_dirname(&parent_path, path);
3671 if (err)
3672 return err;
3675 ct = calloc(1, sizeof(*ct));
3676 if (ct == NULL) {
3677 err = got_error_from_errno("calloc");
3678 goto done;
3681 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3682 relpath) == -1) {
3683 err = got_error_from_errno("asprintf");
3684 goto done;
3686 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3687 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3688 } else {
3689 if (dirfd != -1) {
3690 if (fstatat(dirfd, de_name, &sb,
3691 AT_SYMLINK_NOFOLLOW) == -1) {
3692 err = got_error_from_errno2("fstatat",
3693 ct->ondisk_path);
3694 goto done;
3696 } else if (lstat(ct->ondisk_path, &sb) == -1) {
3697 err = got_error_from_errno2("lstat", ct->ondisk_path);
3698 goto done;
3700 ct->mode = sb.st_mode;
3703 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3704 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3705 relpath) == -1) {
3706 err = got_error_from_errno("asprintf");
3707 goto done;
3710 ct->status = status;
3711 ct->staged_status = staged_status;
3712 ct->blob_id = NULL; /* will be filled in when blob gets created */
3713 if (ct->status != GOT_STATUS_ADD &&
3714 ct->staged_status != GOT_STATUS_ADD) {
3715 ct->base_blob_id = got_object_id_dup(blob_id);
3716 if (ct->base_blob_id == NULL) {
3717 err = got_error_from_errno("got_object_id_dup");
3718 goto done;
3720 ct->base_commit_id = got_object_id_dup(commit_id);
3721 if (ct->base_commit_id == NULL) {
3722 err = got_error_from_errno("got_object_id_dup");
3723 goto done;
3726 if (ct->staged_status == GOT_STATUS_ADD ||
3727 ct->staged_status == GOT_STATUS_MODIFY) {
3728 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3729 if (ct->staged_blob_id == NULL) {
3730 err = got_error_from_errno("got_object_id_dup");
3731 goto done;
3734 ct->path = strdup(path);
3735 if (ct->path == NULL) {
3736 err = got_error_from_errno("strdup");
3737 goto done;
3739 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3740 done:
3741 if (ct && (err || new == NULL))
3742 free_commitable(ct);
3743 free(parent_path);
3744 free(path);
3745 return err;
3748 static const struct got_error *write_tree(struct got_object_id **,
3749 struct got_tree_object *, const char *, struct got_pathlist_head *,
3750 got_worktree_status_cb status_cb, void *status_arg,
3751 struct got_repository *);
3753 static const struct got_error *
3754 write_subtree(struct got_object_id **new_subtree_id,
3755 struct got_tree_entry *te, const char *parent_path,
3756 struct got_pathlist_head *commitable_paths,
3757 got_worktree_status_cb status_cb, void *status_arg,
3758 struct got_repository *repo)
3760 const struct got_error *err = NULL;
3761 struct got_tree_object *subtree;
3762 char *subpath;
3764 if (asprintf(&subpath, "%s%s%s", parent_path,
3765 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3766 return got_error_from_errno("asprintf");
3768 err = got_object_open_as_tree(&subtree, repo, &te->id);
3769 if (err)
3770 return err;
3772 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3773 status_cb, status_arg, repo);
3774 got_object_tree_close(subtree);
3775 free(subpath);
3776 return err;
3779 static const struct got_error *
3780 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3782 const struct got_error *err = NULL;
3783 char *ct_parent_path = NULL;
3785 *match = 0;
3787 if (strchr(ct->in_repo_path, '/') == NULL) {
3788 *match = got_path_is_root_dir(path);
3789 return NULL;
3792 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3793 if (err)
3794 return err;
3795 *match = (strcmp(path, ct_parent_path) == 0);
3796 free(ct_parent_path);
3797 return err;
3800 static mode_t
3801 get_ct_file_mode(struct got_commitable *ct)
3803 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3806 static const struct got_error *
3807 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3808 struct got_tree_entry *te, struct got_commitable *ct)
3810 const struct got_error *err = NULL;
3812 *new_te = NULL;
3814 err = got_object_tree_entry_dup(new_te, te);
3815 if (err)
3816 goto done;
3818 (*new_te)->mode = get_ct_file_mode(ct);
3820 if (ct->staged_status == GOT_STATUS_MODIFY)
3821 memcpy(&(*new_te)->id, ct->staged_blob_id,
3822 sizeof((*new_te)->id));
3823 else
3824 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3825 done:
3826 if (err && *new_te) {
3827 free(*new_te);
3828 *new_te = NULL;
3830 return err;
3833 static const struct got_error *
3834 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3835 struct got_commitable *ct)
3837 const struct got_error *err = NULL;
3838 char *ct_name;
3840 *new_te = NULL;
3842 *new_te = calloc(1, sizeof(**new_te));
3843 if (*new_te == NULL)
3844 return got_error_from_errno("calloc");
3846 ct_name = basename(ct->path);
3847 if (ct_name == NULL) {
3848 err = got_error_from_errno2("basename", ct->path);
3849 goto done;
3851 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3852 sizeof((*new_te)->name)) {
3853 err = got_error(GOT_ERR_NO_SPACE);
3854 goto done;
3857 (*new_te)->mode = get_ct_file_mode(ct);
3859 if (ct->staged_status == GOT_STATUS_ADD)
3860 memcpy(&(*new_te)->id, ct->staged_blob_id,
3861 sizeof((*new_te)->id));
3862 else
3863 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3864 done:
3865 if (err && *new_te) {
3866 free(*new_te);
3867 *new_te = NULL;
3869 return err;
3872 static const struct got_error *
3873 insert_tree_entry(struct got_tree_entry *new_te,
3874 struct got_pathlist_head *paths)
3876 const struct got_error *err = NULL;
3877 struct got_pathlist_entry *new_pe;
3879 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3880 if (err)
3881 return err;
3882 if (new_pe == NULL)
3883 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3884 return NULL;
3887 static const struct got_error *
3888 report_ct_status(struct got_commitable *ct,
3889 got_worktree_status_cb status_cb, void *status_arg)
3891 const char *ct_path = ct->path;
3892 unsigned char status;
3894 while (ct_path[0] == '/')
3895 ct_path++;
3897 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3898 status = ct->staged_status;
3899 else
3900 status = ct->status;
3902 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3903 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
3906 static const struct got_error *
3907 match_modified_subtree(int *modified, struct got_tree_entry *te,
3908 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3910 const struct got_error *err = NULL;
3911 struct got_pathlist_entry *pe;
3912 char *te_path;
3914 *modified = 0;
3916 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3917 got_path_is_root_dir(base_tree_path) ? "" : "/",
3918 te->name) == -1)
3919 return got_error_from_errno("asprintf");
3921 TAILQ_FOREACH(pe, commitable_paths, entry) {
3922 struct got_commitable *ct = pe->data;
3923 *modified = got_path_is_child(ct->in_repo_path, te_path,
3924 strlen(te_path));
3925 if (*modified)
3926 break;
3929 free(te_path);
3930 return err;
3933 static const struct got_error *
3934 match_deleted_or_modified_ct(struct got_commitable **ctp,
3935 struct got_tree_entry *te, const char *base_tree_path,
3936 struct got_pathlist_head *commitable_paths)
3938 const struct got_error *err = NULL;
3939 struct got_pathlist_entry *pe;
3941 *ctp = NULL;
3943 TAILQ_FOREACH(pe, commitable_paths, entry) {
3944 struct got_commitable *ct = pe->data;
3945 char *ct_name = NULL;
3946 int path_matches;
3948 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3949 if (ct->status != GOT_STATUS_MODIFY &&
3950 ct->status != GOT_STATUS_MODE_CHANGE &&
3951 ct->status != GOT_STATUS_DELETE)
3952 continue;
3953 } else {
3954 if (ct->staged_status != GOT_STATUS_MODIFY &&
3955 ct->staged_status != GOT_STATUS_DELETE)
3956 continue;
3959 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
3960 continue;
3962 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3963 if (err)
3964 return err;
3965 if (!path_matches)
3966 continue;
3968 ct_name = basename(pe->path);
3969 if (ct_name == NULL)
3970 return got_error_from_errno2("basename", pe->path);
3972 if (strcmp(te->name, ct_name) != 0)
3973 continue;
3975 *ctp = ct;
3976 break;
3979 return err;
3982 static const struct got_error *
3983 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3984 const char *child_path, const char *path_base_tree,
3985 struct got_pathlist_head *commitable_paths,
3986 got_worktree_status_cb status_cb, void *status_arg,
3987 struct got_repository *repo)
3989 const struct got_error *err = NULL;
3990 struct got_tree_entry *new_te;
3991 char *subtree_path;
3992 struct got_object_id *id = NULL;
3994 *new_tep = NULL;
3996 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3997 got_path_is_root_dir(path_base_tree) ? "" : "/",
3998 child_path) == -1)
3999 return got_error_from_errno("asprintf");
4001 new_te = calloc(1, sizeof(*new_te));
4002 if (new_te == NULL)
4003 return got_error_from_errno("calloc");
4004 new_te->mode = S_IFDIR;
4006 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4007 sizeof(new_te->name)) {
4008 err = got_error(GOT_ERR_NO_SPACE);
4009 goto done;
4011 err = write_tree(&id, NULL, subtree_path,
4012 commitable_paths, status_cb, status_arg, repo);
4013 if (err) {
4014 free(new_te);
4015 goto done;
4017 memcpy(&new_te->id, id, sizeof(new_te->id));
4018 done:
4019 free(id);
4020 free(subtree_path);
4021 if (err == NULL)
4022 *new_tep = new_te;
4023 return err;
4026 static const struct got_error *
4027 write_tree(struct got_object_id **new_tree_id,
4028 struct got_tree_object *base_tree, const char *path_base_tree,
4029 struct got_pathlist_head *commitable_paths,
4030 got_worktree_status_cb status_cb, void *status_arg,
4031 struct got_repository *repo)
4033 const struct got_error *err = NULL;
4034 struct got_pathlist_head paths;
4035 struct got_tree_entry *te, *new_te = NULL;
4036 struct got_pathlist_entry *pe;
4037 int nentries = 0;
4039 TAILQ_INIT(&paths);
4041 /* Insert, and recurse into, newly added entries first. */
4042 TAILQ_FOREACH(pe, commitable_paths, entry) {
4043 struct got_commitable *ct = pe->data;
4044 char *child_path = NULL, *slash;
4046 if ((ct->status != GOT_STATUS_ADD &&
4047 ct->staged_status != GOT_STATUS_ADD) ||
4048 (ct->flags & GOT_COMMITABLE_ADDED))
4049 continue;
4051 if (!got_path_is_child(pe->path, path_base_tree,
4052 strlen(path_base_tree)))
4053 continue;
4055 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4056 pe->path);
4057 if (err)
4058 goto done;
4060 slash = strchr(child_path, '/');
4061 if (slash == NULL) {
4062 err = alloc_added_blob_tree_entry(&new_te, ct);
4063 if (err)
4064 goto done;
4065 err = report_ct_status(ct, status_cb, status_arg);
4066 if (err)
4067 goto done;
4068 ct->flags |= GOT_COMMITABLE_ADDED;
4069 err = insert_tree_entry(new_te, &paths);
4070 if (err)
4071 goto done;
4072 nentries++;
4073 } else {
4074 *slash = '\0'; /* trim trailing path components */
4075 if (base_tree == NULL ||
4076 got_object_tree_find_entry(base_tree, child_path)
4077 == NULL) {
4078 err = make_subtree_for_added_blob(&new_te,
4079 child_path, path_base_tree,
4080 commitable_paths, status_cb, status_arg,
4081 repo);
4082 if (err)
4083 goto done;
4084 err = insert_tree_entry(new_te, &paths);
4085 if (err)
4086 goto done;
4087 nentries++;
4092 if (base_tree) {
4093 int i, nbase_entries;
4094 /* Handle modified and deleted entries. */
4095 nbase_entries = got_object_tree_get_nentries(base_tree);
4096 for (i = 0; i < nbase_entries; i++) {
4097 struct got_commitable *ct = NULL;
4099 te = got_object_tree_get_entry(base_tree, i);
4100 if (got_object_tree_entry_is_submodule(te)) {
4101 /* Entry is a submodule; just copy it. */
4102 err = got_object_tree_entry_dup(&new_te, te);
4103 if (err)
4104 goto done;
4105 err = insert_tree_entry(new_te, &paths);
4106 if (err)
4107 goto done;
4108 nentries++;
4109 continue;
4112 if (S_ISDIR(te->mode)) {
4113 int modified;
4114 err = got_object_tree_entry_dup(&new_te, te);
4115 if (err)
4116 goto done;
4117 err = match_modified_subtree(&modified, te,
4118 path_base_tree, commitable_paths);
4119 if (err)
4120 goto done;
4121 /* Avoid recursion into unmodified subtrees. */
4122 if (modified) {
4123 struct got_object_id *new_id;
4124 err = write_subtree(&new_id, te,
4125 path_base_tree, commitable_paths,
4126 status_cb, status_arg, repo);
4127 if (err)
4128 goto done;
4129 memcpy(&new_te->id, new_id,
4130 sizeof(new_te->id));
4131 free(new_id);
4133 err = insert_tree_entry(new_te, &paths);
4134 if (err)
4135 goto done;
4136 nentries++;
4137 continue;
4140 err = match_deleted_or_modified_ct(&ct, te,
4141 path_base_tree, commitable_paths);
4142 if (err)
4143 goto done;
4144 if (ct) {
4145 /* NB: Deleted entries get dropped here. */
4146 if (ct->status == GOT_STATUS_MODIFY ||
4147 ct->status == GOT_STATUS_MODE_CHANGE ||
4148 ct->staged_status == GOT_STATUS_MODIFY) {
4149 err = alloc_modified_blob_tree_entry(
4150 &new_te, te, ct);
4151 if (err)
4152 goto done;
4153 err = insert_tree_entry(new_te, &paths);
4154 if (err)
4155 goto done;
4156 nentries++;
4158 err = report_ct_status(ct, status_cb,
4159 status_arg);
4160 if (err)
4161 goto done;
4162 } else {
4163 /* Entry is unchanged; just copy it. */
4164 err = got_object_tree_entry_dup(&new_te, te);
4165 if (err)
4166 goto done;
4167 err = insert_tree_entry(new_te, &paths);
4168 if (err)
4169 goto done;
4170 nentries++;
4175 /* Write new list of entries; deleted entries have been dropped. */
4176 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
4177 done:
4178 got_pathlist_free(&paths);
4179 return err;
4182 static const struct got_error *
4183 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4184 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4185 int have_staged_files)
4187 const struct got_error *err = NULL;
4188 struct got_pathlist_entry *pe;
4190 TAILQ_FOREACH(pe, commitable_paths, entry) {
4191 struct got_fileindex_entry *ie;
4192 struct got_commitable *ct = pe->data;
4194 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4195 if (ie) {
4196 if (ct->status == GOT_STATUS_DELETE ||
4197 ct->staged_status == GOT_STATUS_DELETE) {
4198 got_fileindex_entry_remove(fileindex, ie);
4199 got_fileindex_entry_free(ie);
4200 } else if (ct->staged_status == GOT_STATUS_ADD ||
4201 ct->staged_status == GOT_STATUS_MODIFY) {
4202 got_fileindex_entry_stage_set(ie,
4203 GOT_FILEIDX_STAGE_NONE);
4204 err = got_fileindex_entry_update(ie,
4205 ct->ondisk_path, ct->staged_blob_id->sha1,
4206 new_base_commit_id->sha1,
4207 !have_staged_files);
4208 } else
4209 err = got_fileindex_entry_update(ie,
4210 ct->ondisk_path, ct->blob_id->sha1,
4211 new_base_commit_id->sha1,
4212 !have_staged_files);
4213 } else {
4214 err = got_fileindex_entry_alloc(&ie,
4215 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4216 new_base_commit_id->sha1);
4217 if (err)
4218 break;
4219 err = got_fileindex_entry_add(fileindex, ie);
4220 if (err)
4221 break;
4224 return err;
4228 static const struct got_error *
4229 check_out_of_date(const char *in_repo_path, unsigned char status,
4230 unsigned char staged_status, struct got_object_id *base_blob_id,
4231 struct got_object_id *base_commit_id,
4232 struct got_object_id *head_commit_id, struct got_repository *repo,
4233 int ood_errcode)
4235 const struct got_error *err = NULL;
4236 struct got_object_id *id = NULL;
4238 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4239 /* Trivial case: base commit == head commit */
4240 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4241 return NULL;
4243 * Ensure file content which local changes were based
4244 * on matches file content in the branch head.
4246 err = got_object_id_by_path(&id, repo, head_commit_id,
4247 in_repo_path);
4248 if (err) {
4249 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4250 err = got_error(ood_errcode);
4251 goto done;
4252 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4253 err = got_error(ood_errcode);
4254 } else {
4255 /* Require that added files don't exist in the branch head. */
4256 err = got_object_id_by_path(&id, repo, head_commit_id,
4257 in_repo_path);
4258 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4259 goto done;
4260 err = id ? got_error(ood_errcode) : NULL;
4262 done:
4263 free(id);
4264 return err;
4267 const struct got_error *
4268 commit_worktree(struct got_object_id **new_commit_id,
4269 struct got_pathlist_head *commitable_paths,
4270 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4271 const char *author, const char *committer,
4272 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4273 got_worktree_status_cb status_cb, void *status_arg,
4274 struct got_repository *repo)
4276 const struct got_error *err = NULL, *unlockerr = NULL;
4277 struct got_pathlist_entry *pe;
4278 const char *head_ref_name = NULL;
4279 struct got_commit_object *head_commit = NULL;
4280 struct got_reference *head_ref2 = NULL;
4281 struct got_object_id *head_commit_id2 = NULL;
4282 struct got_tree_object *head_tree = NULL;
4283 struct got_object_id *new_tree_id = NULL;
4284 struct got_object_id_queue parent_ids;
4285 struct got_object_qid *pid = NULL;
4286 char *logmsg = NULL;
4288 *new_commit_id = NULL;
4290 SIMPLEQ_INIT(&parent_ids);
4292 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4293 if (err)
4294 goto done;
4296 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4297 if (err)
4298 goto done;
4300 if (commit_msg_cb != NULL) {
4301 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4302 if (err)
4303 goto done;
4306 if (logmsg == NULL || strlen(logmsg) == 0) {
4307 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4308 goto done;
4311 /* Create blobs from added and modified files and record their IDs. */
4312 TAILQ_FOREACH(pe, commitable_paths, entry) {
4313 struct got_commitable *ct = pe->data;
4314 char *ondisk_path;
4316 /* Blobs for staged files already exist. */
4317 if (ct->staged_status == GOT_STATUS_ADD ||
4318 ct->staged_status == GOT_STATUS_MODIFY)
4319 continue;
4321 if (ct->status != GOT_STATUS_ADD &&
4322 ct->status != GOT_STATUS_MODIFY &&
4323 ct->status != GOT_STATUS_MODE_CHANGE)
4324 continue;
4326 if (asprintf(&ondisk_path, "%s/%s",
4327 worktree->root_path, pe->path) == -1) {
4328 err = got_error_from_errno("asprintf");
4329 goto done;
4331 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4332 free(ondisk_path);
4333 if (err)
4334 goto done;
4337 /* Recursively write new tree objects. */
4338 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4339 status_cb, status_arg, repo);
4340 if (err)
4341 goto done;
4343 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4344 if (err)
4345 goto done;
4346 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4347 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4348 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4349 got_object_qid_free(pid);
4350 if (logmsg != NULL)
4351 free(logmsg);
4352 if (err)
4353 goto done;
4355 /* Check if a concurrent commit to our branch has occurred. */
4356 head_ref_name = got_worktree_get_head_ref_name(worktree);
4357 if (head_ref_name == NULL) {
4358 err = got_error_from_errno("got_worktree_get_head_ref_name");
4359 goto done;
4361 /* Lock the reference here to prevent concurrent modification. */
4362 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4363 if (err)
4364 goto done;
4365 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4366 if (err)
4367 goto done;
4368 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4369 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4370 goto done;
4372 /* Update branch head in repository. */
4373 err = got_ref_change_ref(head_ref2, *new_commit_id);
4374 if (err)
4375 goto done;
4376 err = got_ref_write(head_ref2, repo);
4377 if (err)
4378 goto done;
4380 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4381 if (err)
4382 goto done;
4384 err = ref_base_commit(worktree, repo);
4385 if (err)
4386 goto done;
4387 done:
4388 if (head_tree)
4389 got_object_tree_close(head_tree);
4390 if (head_commit)
4391 got_object_commit_close(head_commit);
4392 free(head_commit_id2);
4393 if (head_ref2) {
4394 unlockerr = got_ref_unlock(head_ref2);
4395 if (unlockerr && err == NULL)
4396 err = unlockerr;
4397 got_ref_close(head_ref2);
4399 return err;
4402 static const struct got_error *
4403 check_path_is_commitable(const char *path,
4404 struct got_pathlist_head *commitable_paths)
4406 struct got_pathlist_entry *cpe = NULL;
4407 size_t path_len = strlen(path);
4409 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4410 struct got_commitable *ct = cpe->data;
4411 const char *ct_path = ct->path;
4413 while (ct_path[0] == '/')
4414 ct_path++;
4416 if (strcmp(path, ct_path) == 0 ||
4417 got_path_is_child(ct_path, path, path_len))
4418 break;
4421 if (cpe == NULL)
4422 return got_error_path(path, GOT_ERR_BAD_PATH);
4424 return NULL;
4427 static const struct got_error *
4428 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4430 int *have_staged_files = arg;
4432 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4433 *have_staged_files = 1;
4434 return got_error(GOT_ERR_CANCELLED);
4437 return NULL;
4440 static const struct got_error *
4441 check_non_staged_files(struct got_fileindex *fileindex,
4442 struct got_pathlist_head *paths)
4444 struct got_pathlist_entry *pe;
4445 struct got_fileindex_entry *ie;
4447 TAILQ_FOREACH(pe, paths, entry) {
4448 if (pe->path[0] == '\0')
4449 continue;
4450 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4451 if (ie == NULL)
4452 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4453 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4454 return got_error_path(pe->path,
4455 GOT_ERR_FILE_NOT_STAGED);
4458 return NULL;
4461 const struct got_error *
4462 got_worktree_commit(struct got_object_id **new_commit_id,
4463 struct got_worktree *worktree, struct got_pathlist_head *paths,
4464 const char *author, const char *committer,
4465 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4466 got_worktree_status_cb status_cb, void *status_arg,
4467 struct got_repository *repo)
4469 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4470 struct got_fileindex *fileindex = NULL;
4471 char *fileindex_path = NULL;
4472 struct got_pathlist_head commitable_paths;
4473 struct collect_commitables_arg cc_arg;
4474 struct got_pathlist_entry *pe;
4475 struct got_reference *head_ref = NULL;
4476 struct got_object_id *head_commit_id = NULL;
4477 int have_staged_files = 0;
4479 *new_commit_id = NULL;
4481 TAILQ_INIT(&commitable_paths);
4483 err = lock_worktree(worktree, LOCK_EX);
4484 if (err)
4485 goto done;
4487 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4488 if (err)
4489 goto done;
4491 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4492 if (err)
4493 goto done;
4495 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4496 if (err)
4497 goto done;
4499 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4500 &have_staged_files);
4501 if (err && err->code != GOT_ERR_CANCELLED)
4502 goto done;
4503 if (have_staged_files) {
4504 err = check_non_staged_files(fileindex, paths);
4505 if (err)
4506 goto done;
4509 cc_arg.commitable_paths = &commitable_paths;
4510 cc_arg.worktree = worktree;
4511 cc_arg.repo = repo;
4512 cc_arg.have_staged_files = have_staged_files;
4513 TAILQ_FOREACH(pe, paths, entry) {
4514 err = worktree_status(worktree, pe->path, fileindex, repo,
4515 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4516 if (err)
4517 goto done;
4520 if (TAILQ_EMPTY(&commitable_paths)) {
4521 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4522 goto done;
4525 TAILQ_FOREACH(pe, paths, entry) {
4526 err = check_path_is_commitable(pe->path, &commitable_paths);
4527 if (err)
4528 goto done;
4531 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4532 struct got_commitable *ct = pe->data;
4533 const char *ct_path = ct->in_repo_path;
4535 while (ct_path[0] == '/')
4536 ct_path++;
4537 err = check_out_of_date(ct_path, ct->status,
4538 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4539 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4540 if (err)
4541 goto done;
4545 err = commit_worktree(new_commit_id, &commitable_paths,
4546 head_commit_id, worktree, author, committer,
4547 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4548 if (err)
4549 goto done;
4551 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4552 fileindex, have_staged_files);
4553 sync_err = sync_fileindex(fileindex, fileindex_path);
4554 if (sync_err && err == NULL)
4555 err = sync_err;
4556 done:
4557 if (fileindex)
4558 got_fileindex_free(fileindex);
4559 free(fileindex_path);
4560 unlockerr = lock_worktree(worktree, LOCK_SH);
4561 if (unlockerr && err == NULL)
4562 err = unlockerr;
4563 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4564 struct got_commitable *ct = pe->data;
4565 free_commitable(ct);
4567 got_pathlist_free(&commitable_paths);
4568 return err;
4571 const char *
4572 got_commitable_get_path(struct got_commitable *ct)
4574 return ct->path;
4577 unsigned int
4578 got_commitable_get_status(struct got_commitable *ct)
4580 return ct->status;
4583 struct check_rebase_ok_arg {
4584 struct got_worktree *worktree;
4585 struct got_repository *repo;
4588 static const struct got_error *
4589 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4591 const struct got_error *err = NULL;
4592 struct check_rebase_ok_arg *a = arg;
4593 unsigned char status;
4594 struct stat sb;
4595 char *ondisk_path;
4597 /* Reject rebase of a work tree with mixed base commits. */
4598 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4599 SHA1_DIGEST_LENGTH))
4600 return got_error(GOT_ERR_MIXED_COMMITS);
4602 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4603 == -1)
4604 return got_error_from_errno("asprintf");
4606 /* Reject rebase of a work tree with modified or staged files. */
4607 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4608 free(ondisk_path);
4609 if (err)
4610 return err;
4612 if (status != GOT_STATUS_NO_CHANGE)
4613 return got_error(GOT_ERR_MODIFIED);
4614 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4615 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4617 return NULL;
4620 const struct got_error *
4621 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4622 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4623 struct got_worktree *worktree, struct got_reference *branch,
4624 struct got_repository *repo)
4626 const struct got_error *err = NULL;
4627 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4628 char *branch_ref_name = NULL;
4629 char *fileindex_path = NULL;
4630 struct check_rebase_ok_arg ok_arg;
4631 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4633 *new_base_branch_ref = NULL;
4634 *tmp_branch = NULL;
4635 *fileindex = NULL;
4637 err = lock_worktree(worktree, LOCK_EX);
4638 if (err)
4639 return err;
4641 err = open_fileindex(fileindex, &fileindex_path, worktree);
4642 if (err)
4643 goto done;
4645 ok_arg.worktree = worktree;
4646 ok_arg.repo = repo;
4647 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4648 &ok_arg);
4649 if (err)
4650 goto done;
4652 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4653 if (err)
4654 goto done;
4656 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4657 if (err)
4658 goto done;
4660 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4661 if (err)
4662 goto done;
4664 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4665 0);
4666 if (err)
4667 goto done;
4669 err = got_ref_alloc_symref(new_base_branch_ref,
4670 new_base_branch_ref_name, wt_branch);
4671 if (err)
4672 goto done;
4673 err = got_ref_write(*new_base_branch_ref, repo);
4674 if (err)
4675 goto done;
4677 /* TODO Lock original branch's ref while rebasing? */
4679 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4680 if (err)
4681 goto done;
4683 err = got_ref_write(branch_ref, repo);
4684 if (err)
4685 goto done;
4687 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4688 worktree->base_commit_id);
4689 if (err)
4690 goto done;
4691 err = got_ref_write(*tmp_branch, repo);
4692 if (err)
4693 goto done;
4695 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4696 if (err)
4697 goto done;
4698 done:
4699 free(fileindex_path);
4700 free(tmp_branch_name);
4701 free(new_base_branch_ref_name);
4702 free(branch_ref_name);
4703 if (branch_ref)
4704 got_ref_close(branch_ref);
4705 if (wt_branch)
4706 got_ref_close(wt_branch);
4707 if (err) {
4708 if (*new_base_branch_ref) {
4709 got_ref_close(*new_base_branch_ref);
4710 *new_base_branch_ref = NULL;
4712 if (*tmp_branch) {
4713 got_ref_close(*tmp_branch);
4714 *tmp_branch = NULL;
4716 if (*fileindex) {
4717 got_fileindex_free(*fileindex);
4718 *fileindex = NULL;
4720 lock_worktree(worktree, LOCK_SH);
4722 return err;
4725 const struct got_error *
4726 got_worktree_rebase_continue(struct got_object_id **commit_id,
4727 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4728 struct got_reference **branch, struct got_fileindex **fileindex,
4729 struct got_worktree *worktree, struct got_repository *repo)
4731 const struct got_error *err;
4732 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4733 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4734 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4735 char *fileindex_path = NULL;
4736 int have_staged_files = 0;
4738 *commit_id = NULL;
4739 *new_base_branch = NULL;
4740 *tmp_branch = NULL;
4741 *branch = NULL;
4742 *fileindex = NULL;
4744 err = lock_worktree(worktree, LOCK_EX);
4745 if (err)
4746 return err;
4748 err = open_fileindex(fileindex, &fileindex_path, worktree);
4749 if (err)
4750 goto done;
4752 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4753 &have_staged_files);
4754 if (err && err->code != GOT_ERR_CANCELLED)
4755 goto done;
4756 if (have_staged_files) {
4757 err = got_error(GOT_ERR_STAGED_PATHS);
4758 goto done;
4761 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4762 if (err)
4763 goto done;
4765 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4766 if (err)
4767 goto done;
4769 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4770 if (err)
4771 goto done;
4773 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4774 if (err)
4775 goto done;
4777 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4778 if (err)
4779 goto done;
4781 err = got_ref_open(branch, repo,
4782 got_ref_get_symref_target(branch_ref), 0);
4783 if (err)
4784 goto done;
4786 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4787 if (err)
4788 goto done;
4790 err = got_ref_resolve(commit_id, repo, commit_ref);
4791 if (err)
4792 goto done;
4794 err = got_ref_open(new_base_branch, repo,
4795 new_base_branch_ref_name, 0);
4796 if (err)
4797 goto done;
4799 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4800 if (err)
4801 goto done;
4802 done:
4803 free(commit_ref_name);
4804 free(branch_ref_name);
4805 free(fileindex_path);
4806 if (commit_ref)
4807 got_ref_close(commit_ref);
4808 if (branch_ref)
4809 got_ref_close(branch_ref);
4810 if (err) {
4811 free(*commit_id);
4812 *commit_id = NULL;
4813 if (*tmp_branch) {
4814 got_ref_close(*tmp_branch);
4815 *tmp_branch = NULL;
4817 if (*new_base_branch) {
4818 got_ref_close(*new_base_branch);
4819 *new_base_branch = NULL;
4821 if (*branch) {
4822 got_ref_close(*branch);
4823 *branch = NULL;
4825 if (*fileindex) {
4826 got_fileindex_free(*fileindex);
4827 *fileindex = NULL;
4829 lock_worktree(worktree, LOCK_SH);
4831 return err;
4834 const struct got_error *
4835 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4837 const struct got_error *err;
4838 char *tmp_branch_name = NULL;
4840 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4841 if (err)
4842 return err;
4844 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4845 free(tmp_branch_name);
4846 return NULL;
4849 static const struct got_error *
4850 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4851 char **logmsg, void *arg)
4853 *logmsg = arg;
4854 return NULL;
4857 static const struct got_error *
4858 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4859 const char *path, struct got_object_id *blob_id,
4860 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4861 int dirfd, const char *de_name)
4863 return NULL;
4866 struct collect_merged_paths_arg {
4867 got_worktree_checkout_cb progress_cb;
4868 void *progress_arg;
4869 struct got_pathlist_head *merged_paths;
4872 static const struct got_error *
4873 collect_merged_paths(void *arg, unsigned char status, const char *path)
4875 const struct got_error *err;
4876 struct collect_merged_paths_arg *a = arg;
4877 char *p;
4878 struct got_pathlist_entry *new;
4880 err = (*a->progress_cb)(a->progress_arg, status, path);
4881 if (err)
4882 return err;
4884 if (status != GOT_STATUS_MERGE &&
4885 status != GOT_STATUS_ADD &&
4886 status != GOT_STATUS_DELETE &&
4887 status != GOT_STATUS_CONFLICT)
4888 return NULL;
4890 p = strdup(path);
4891 if (p == NULL)
4892 return got_error_from_errno("strdup");
4894 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4895 if (err || new == NULL)
4896 free(p);
4897 return err;
4900 void
4901 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4903 struct got_pathlist_entry *pe;
4905 TAILQ_FOREACH(pe, merged_paths, entry)
4906 free((char *)pe->path);
4908 got_pathlist_free(merged_paths);
4911 static const struct got_error *
4912 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4913 struct got_repository *repo)
4915 const struct got_error *err;
4916 struct got_reference *commit_ref = NULL;
4918 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4919 if (err) {
4920 if (err->code != GOT_ERR_NOT_REF)
4921 goto done;
4922 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4923 if (err)
4924 goto done;
4925 err = got_ref_write(commit_ref, repo);
4926 if (err)
4927 goto done;
4928 } else {
4929 struct got_object_id *stored_id;
4930 int cmp;
4932 err = got_ref_resolve(&stored_id, repo, commit_ref);
4933 if (err)
4934 goto done;
4935 cmp = got_object_id_cmp(commit_id, stored_id);
4936 free(stored_id);
4937 if (cmp != 0) {
4938 err = got_error(GOT_ERR_REBASE_COMMITID);
4939 goto done;
4942 done:
4943 if (commit_ref)
4944 got_ref_close(commit_ref);
4945 return err;
4948 static const struct got_error *
4949 rebase_merge_files(struct got_pathlist_head *merged_paths,
4950 const char *commit_ref_name, struct got_worktree *worktree,
4951 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4952 struct got_object_id *commit_id, struct got_repository *repo,
4953 got_worktree_checkout_cb progress_cb, void *progress_arg,
4954 got_cancel_cb cancel_cb, void *cancel_arg)
4956 const struct got_error *err;
4957 struct got_reference *commit_ref = NULL;
4958 struct collect_merged_paths_arg cmp_arg;
4959 char *fileindex_path;
4961 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4963 err = get_fileindex_path(&fileindex_path, worktree);
4964 if (err)
4965 return err;
4967 cmp_arg.progress_cb = progress_cb;
4968 cmp_arg.progress_arg = progress_arg;
4969 cmp_arg.merged_paths = merged_paths;
4970 err = merge_files(worktree, fileindex, fileindex_path,
4971 parent_commit_id, commit_id, repo, collect_merged_paths,
4972 &cmp_arg, cancel_cb, cancel_arg);
4973 if (commit_ref)
4974 got_ref_close(commit_ref);
4975 return err;
4978 const struct got_error *
4979 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4980 struct got_worktree *worktree, struct got_fileindex *fileindex,
4981 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4982 struct got_repository *repo,
4983 got_worktree_checkout_cb progress_cb, void *progress_arg,
4984 got_cancel_cb cancel_cb, void *cancel_arg)
4986 const struct got_error *err;
4987 char *commit_ref_name;
4989 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4990 if (err)
4991 return err;
4993 err = store_commit_id(commit_ref_name, commit_id, repo);
4994 if (err)
4995 goto done;
4997 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4998 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4999 progress_arg, cancel_cb, cancel_arg);
5000 done:
5001 free(commit_ref_name);
5002 return err;
5005 const struct got_error *
5006 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5007 struct got_worktree *worktree, struct got_fileindex *fileindex,
5008 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5009 struct got_repository *repo,
5010 got_worktree_checkout_cb progress_cb, void *progress_arg,
5011 got_cancel_cb cancel_cb, void *cancel_arg)
5013 const struct got_error *err;
5014 char *commit_ref_name;
5016 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5017 if (err)
5018 return err;
5020 err = store_commit_id(commit_ref_name, commit_id, repo);
5021 if (err)
5022 goto done;
5024 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5025 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5026 progress_arg, cancel_cb, cancel_arg);
5027 done:
5028 free(commit_ref_name);
5029 return err;
5032 static const struct got_error *
5033 rebase_commit(struct got_object_id **new_commit_id,
5034 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5035 struct got_worktree *worktree, struct got_fileindex *fileindex,
5036 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5037 const char *new_logmsg, struct got_repository *repo)
5039 const struct got_error *err, *sync_err;
5040 struct got_pathlist_head commitable_paths;
5041 struct collect_commitables_arg cc_arg;
5042 char *fileindex_path = NULL;
5043 struct got_reference *head_ref = NULL;
5044 struct got_object_id *head_commit_id = NULL;
5045 char *logmsg = NULL;
5047 TAILQ_INIT(&commitable_paths);
5048 *new_commit_id = NULL;
5050 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5052 err = get_fileindex_path(&fileindex_path, worktree);
5053 if (err)
5054 return err;
5056 cc_arg.commitable_paths = &commitable_paths;
5057 cc_arg.worktree = worktree;
5058 cc_arg.repo = repo;
5059 cc_arg.have_staged_files = 0;
5061 * If possible get the status of individual files directly to
5062 * avoid crawling the entire work tree once per rebased commit.
5063 * TODO: Ideally, merged_paths would contain a list of commitables
5064 * we could use so we could skip worktree_status() entirely.
5066 if (merged_paths) {
5067 struct got_pathlist_entry *pe;
5068 if (TAILQ_EMPTY(merged_paths)) {
5069 err = got_error(GOT_ERR_NO_MERGED_PATHS);
5070 goto done;
5072 TAILQ_FOREACH(pe, merged_paths, entry) {
5073 err = worktree_status(worktree, pe->path, fileindex,
5074 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5075 0);
5076 if (err)
5077 goto done;
5079 } else {
5080 err = worktree_status(worktree, "", fileindex, repo,
5081 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5082 if (err)
5083 goto done;
5086 if (TAILQ_EMPTY(&commitable_paths)) {
5087 /* No-op change; commit will be elided. */
5088 err = got_ref_delete(commit_ref, repo);
5089 if (err)
5090 goto done;
5091 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5092 goto done;
5095 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5096 if (err)
5097 goto done;
5099 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5100 if (err)
5101 goto done;
5103 if (new_logmsg) {
5104 logmsg = strdup(new_logmsg);
5105 if (logmsg == NULL) {
5106 err = got_error_from_errno("strdup");
5107 goto done;
5109 } else {
5110 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5111 if (err)
5112 goto done;
5115 /* NB: commit_worktree will call free(logmsg) */
5116 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5117 worktree, got_object_commit_get_author(orig_commit),
5118 got_object_commit_get_committer(orig_commit),
5119 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5120 if (err)
5121 goto done;
5123 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5124 if (err)
5125 goto done;
5127 err = got_ref_delete(commit_ref, repo);
5128 if (err)
5129 goto done;
5131 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5132 fileindex, 0);
5133 sync_err = sync_fileindex(fileindex, fileindex_path);
5134 if (sync_err && err == NULL)
5135 err = sync_err;
5136 done:
5137 free(fileindex_path);
5138 free(head_commit_id);
5139 if (head_ref)
5140 got_ref_close(head_ref);
5141 if (err) {
5142 free(*new_commit_id);
5143 *new_commit_id = NULL;
5145 return err;
5148 const struct got_error *
5149 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5150 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5151 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5152 struct got_commit_object *orig_commit,
5153 struct got_object_id *orig_commit_id, struct got_repository *repo)
5155 const struct got_error *err;
5156 char *commit_ref_name;
5157 struct got_reference *commit_ref = NULL;
5158 struct got_object_id *commit_id = NULL;
5160 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5161 if (err)
5162 return err;
5164 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5165 if (err)
5166 goto done;
5167 err = got_ref_resolve(&commit_id, repo, commit_ref);
5168 if (err)
5169 goto done;
5170 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5171 err = got_error(GOT_ERR_REBASE_COMMITID);
5172 goto done;
5175 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5176 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5177 done:
5178 if (commit_ref)
5179 got_ref_close(commit_ref);
5180 free(commit_ref_name);
5181 free(commit_id);
5182 return err;
5185 const struct got_error *
5186 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5187 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5188 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5189 struct got_commit_object *orig_commit,
5190 struct got_object_id *orig_commit_id, const char *new_logmsg,
5191 struct got_repository *repo)
5193 const struct got_error *err;
5194 char *commit_ref_name;
5195 struct got_reference *commit_ref = NULL;
5196 struct got_object_id *commit_id = NULL;
5198 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5199 if (err)
5200 return err;
5202 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5203 if (err)
5204 goto done;
5205 err = got_ref_resolve(&commit_id, repo, commit_ref);
5206 if (err)
5207 goto done;
5208 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5209 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5210 goto done;
5213 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5214 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5215 done:
5216 if (commit_ref)
5217 got_ref_close(commit_ref);
5218 free(commit_ref_name);
5219 free(commit_id);
5220 return err;
5223 const struct got_error *
5224 got_worktree_rebase_postpone(struct got_worktree *worktree,
5225 struct got_fileindex *fileindex)
5227 if (fileindex)
5228 got_fileindex_free(fileindex);
5229 return lock_worktree(worktree, LOCK_SH);
5232 static const struct got_error *
5233 delete_ref(const char *name, struct got_repository *repo)
5235 const struct got_error *err;
5236 struct got_reference *ref;
5238 err = got_ref_open(&ref, repo, name, 0);
5239 if (err) {
5240 if (err->code == GOT_ERR_NOT_REF)
5241 return NULL;
5242 return err;
5245 err = got_ref_delete(ref, repo);
5246 got_ref_close(ref);
5247 return err;
5250 static const struct got_error *
5251 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5253 const struct got_error *err;
5254 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5255 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5257 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5258 if (err)
5259 goto done;
5260 err = delete_ref(tmp_branch_name, repo);
5261 if (err)
5262 goto done;
5264 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5265 if (err)
5266 goto done;
5267 err = delete_ref(new_base_branch_ref_name, repo);
5268 if (err)
5269 goto done;
5271 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5272 if (err)
5273 goto done;
5274 err = delete_ref(branch_ref_name, repo);
5275 if (err)
5276 goto done;
5278 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5279 if (err)
5280 goto done;
5281 err = delete_ref(commit_ref_name, repo);
5282 if (err)
5283 goto done;
5285 done:
5286 free(tmp_branch_name);
5287 free(new_base_branch_ref_name);
5288 free(branch_ref_name);
5289 free(commit_ref_name);
5290 return err;
5293 const struct got_error *
5294 got_worktree_rebase_complete(struct got_worktree *worktree,
5295 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5296 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5297 struct got_repository *repo)
5299 const struct got_error *err, *unlockerr;
5300 struct got_object_id *new_head_commit_id = NULL;
5302 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5303 if (err)
5304 return err;
5306 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5307 if (err)
5308 goto done;
5310 err = got_ref_write(rebased_branch, repo);
5311 if (err)
5312 goto done;
5314 err = got_worktree_set_head_ref(worktree, rebased_branch);
5315 if (err)
5316 goto done;
5318 err = delete_rebase_refs(worktree, repo);
5319 done:
5320 if (fileindex)
5321 got_fileindex_free(fileindex);
5322 free(new_head_commit_id);
5323 unlockerr = lock_worktree(worktree, LOCK_SH);
5324 if (unlockerr && err == NULL)
5325 err = unlockerr;
5326 return err;
5329 const struct got_error *
5330 got_worktree_rebase_abort(struct got_worktree *worktree,
5331 struct got_fileindex *fileindex, struct got_repository *repo,
5332 struct got_reference *new_base_branch,
5333 got_worktree_checkout_cb progress_cb, void *progress_arg)
5335 const struct got_error *err, *unlockerr, *sync_err;
5336 struct got_reference *resolved = NULL;
5337 struct got_object_id *commit_id = NULL;
5338 char *fileindex_path = NULL;
5339 struct revert_file_args rfa;
5340 struct got_object_id *tree_id = NULL;
5342 err = lock_worktree(worktree, LOCK_EX);
5343 if (err)
5344 return err;
5346 err = got_ref_open(&resolved, repo,
5347 got_ref_get_symref_target(new_base_branch), 0);
5348 if (err)
5349 goto done;
5351 err = got_worktree_set_head_ref(worktree, resolved);
5352 if (err)
5353 goto done;
5356 * XXX commits to the base branch could have happened while
5357 * we were busy rebasing; should we store the original commit ID
5358 * when rebase begins and read it back here?
5360 err = got_ref_resolve(&commit_id, repo, resolved);
5361 if (err)
5362 goto done;
5364 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5365 if (err)
5366 goto done;
5368 err = got_object_id_by_path(&tree_id, repo,
5369 worktree->base_commit_id, worktree->path_prefix);
5370 if (err)
5371 goto done;
5373 err = delete_rebase_refs(worktree, repo);
5374 if (err)
5375 goto done;
5377 err = get_fileindex_path(&fileindex_path, worktree);
5378 if (err)
5379 goto done;
5381 rfa.worktree = worktree;
5382 rfa.fileindex = fileindex;
5383 rfa.progress_cb = progress_cb;
5384 rfa.progress_arg = progress_arg;
5385 rfa.patch_cb = NULL;
5386 rfa.patch_arg = NULL;
5387 rfa.repo = repo;
5388 err = worktree_status(worktree, "", fileindex, repo,
5389 revert_file, &rfa, NULL, NULL, 0, 0);
5390 if (err)
5391 goto sync;
5393 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5394 repo, progress_cb, progress_arg, NULL, NULL);
5395 sync:
5396 sync_err = sync_fileindex(fileindex, fileindex_path);
5397 if (sync_err && err == NULL)
5398 err = sync_err;
5399 done:
5400 got_ref_close(resolved);
5401 free(tree_id);
5402 free(commit_id);
5403 if (fileindex)
5404 got_fileindex_free(fileindex);
5405 free(fileindex_path);
5407 unlockerr = lock_worktree(worktree, LOCK_SH);
5408 if (unlockerr && err == NULL)
5409 err = unlockerr;
5410 return err;
5413 const struct got_error *
5414 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5415 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5416 struct got_fileindex **fileindex, struct got_worktree *worktree,
5417 struct got_repository *repo)
5419 const struct got_error *err = NULL;
5420 char *tmp_branch_name = NULL;
5421 char *branch_ref_name = NULL;
5422 char *base_commit_ref_name = NULL;
5423 char *fileindex_path = NULL;
5424 struct check_rebase_ok_arg ok_arg;
5425 struct got_reference *wt_branch = NULL;
5426 struct got_reference *base_commit_ref = NULL;
5428 *tmp_branch = NULL;
5429 *branch_ref = NULL;
5430 *base_commit_id = NULL;
5431 *fileindex = NULL;
5433 err = lock_worktree(worktree, LOCK_EX);
5434 if (err)
5435 return err;
5437 err = open_fileindex(fileindex, &fileindex_path, worktree);
5438 if (err)
5439 goto done;
5441 ok_arg.worktree = worktree;
5442 ok_arg.repo = repo;
5443 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5444 &ok_arg);
5445 if (err)
5446 goto done;
5448 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5449 if (err)
5450 goto done;
5452 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5453 if (err)
5454 goto done;
5456 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5457 worktree);
5458 if (err)
5459 goto done;
5461 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5462 0);
5463 if (err)
5464 goto done;
5466 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5467 if (err)
5468 goto done;
5470 err = got_ref_write(*branch_ref, repo);
5471 if (err)
5472 goto done;
5474 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5475 worktree->base_commit_id);
5476 if (err)
5477 goto done;
5478 err = got_ref_write(base_commit_ref, repo);
5479 if (err)
5480 goto done;
5481 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5482 if (*base_commit_id == NULL) {
5483 err = got_error_from_errno("got_object_id_dup");
5484 goto done;
5487 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5488 worktree->base_commit_id);
5489 if (err)
5490 goto done;
5491 err = got_ref_write(*tmp_branch, repo);
5492 if (err)
5493 goto done;
5495 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5496 if (err)
5497 goto done;
5498 done:
5499 free(fileindex_path);
5500 free(tmp_branch_name);
5501 free(branch_ref_name);
5502 free(base_commit_ref_name);
5503 if (wt_branch)
5504 got_ref_close(wt_branch);
5505 if (err) {
5506 if (*branch_ref) {
5507 got_ref_close(*branch_ref);
5508 *branch_ref = NULL;
5510 if (*tmp_branch) {
5511 got_ref_close(*tmp_branch);
5512 *tmp_branch = NULL;
5514 free(*base_commit_id);
5515 if (*fileindex) {
5516 got_fileindex_free(*fileindex);
5517 *fileindex = NULL;
5519 lock_worktree(worktree, LOCK_SH);
5521 return err;
5524 const struct got_error *
5525 got_worktree_histedit_postpone(struct got_worktree *worktree,
5526 struct got_fileindex *fileindex)
5528 if (fileindex)
5529 got_fileindex_free(fileindex);
5530 return lock_worktree(worktree, LOCK_SH);
5533 const struct got_error *
5534 got_worktree_histedit_in_progress(int *in_progress,
5535 struct got_worktree *worktree)
5537 const struct got_error *err;
5538 char *tmp_branch_name = NULL;
5540 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5541 if (err)
5542 return err;
5544 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5545 free(tmp_branch_name);
5546 return NULL;
5549 const struct got_error *
5550 got_worktree_histedit_continue(struct got_object_id **commit_id,
5551 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5552 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5553 struct got_worktree *worktree, struct got_repository *repo)
5555 const struct got_error *err;
5556 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5557 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5558 struct got_reference *commit_ref = NULL;
5559 struct got_reference *base_commit_ref = NULL;
5560 char *fileindex_path = NULL;
5561 int have_staged_files = 0;
5563 *commit_id = NULL;
5564 *tmp_branch = NULL;
5565 *base_commit_id = NULL;
5566 *fileindex = NULL;
5568 err = lock_worktree(worktree, LOCK_EX);
5569 if (err)
5570 return err;
5572 err = open_fileindex(fileindex, &fileindex_path, worktree);
5573 if (err)
5574 goto done;
5576 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5577 &have_staged_files);
5578 if (err && err->code != GOT_ERR_CANCELLED)
5579 goto done;
5580 if (have_staged_files) {
5581 err = got_error(GOT_ERR_STAGED_PATHS);
5582 goto done;
5585 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5586 if (err)
5587 goto done;
5589 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5590 if (err)
5591 goto done;
5593 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5594 if (err)
5595 goto done;
5597 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5598 worktree);
5599 if (err)
5600 goto done;
5602 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5603 if (err)
5604 goto done;
5606 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5607 if (err)
5608 goto done;
5609 err = got_ref_resolve(commit_id, repo, commit_ref);
5610 if (err)
5611 goto done;
5613 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5614 if (err)
5615 goto done;
5616 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5617 if (err)
5618 goto done;
5620 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5621 if (err)
5622 goto done;
5623 done:
5624 free(commit_ref_name);
5625 free(branch_ref_name);
5626 free(fileindex_path);
5627 if (commit_ref)
5628 got_ref_close(commit_ref);
5629 if (base_commit_ref)
5630 got_ref_close(base_commit_ref);
5631 if (err) {
5632 free(*commit_id);
5633 *commit_id = NULL;
5634 free(*base_commit_id);
5635 *base_commit_id = NULL;
5636 if (*tmp_branch) {
5637 got_ref_close(*tmp_branch);
5638 *tmp_branch = NULL;
5640 if (*fileindex) {
5641 got_fileindex_free(*fileindex);
5642 *fileindex = NULL;
5644 lock_worktree(worktree, LOCK_EX);
5646 return err;
5649 static const struct got_error *
5650 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5652 const struct got_error *err;
5653 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5654 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5656 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5657 if (err)
5658 goto done;
5659 err = delete_ref(tmp_branch_name, repo);
5660 if (err)
5661 goto done;
5663 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5664 worktree);
5665 if (err)
5666 goto done;
5667 err = delete_ref(base_commit_ref_name, repo);
5668 if (err)
5669 goto done;
5671 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5672 if (err)
5673 goto done;
5674 err = delete_ref(branch_ref_name, repo);
5675 if (err)
5676 goto done;
5678 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5679 if (err)
5680 goto done;
5681 err = delete_ref(commit_ref_name, repo);
5682 if (err)
5683 goto done;
5684 done:
5685 free(tmp_branch_name);
5686 free(base_commit_ref_name);
5687 free(branch_ref_name);
5688 free(commit_ref_name);
5689 return err;
5692 const struct got_error *
5693 got_worktree_histedit_abort(struct got_worktree *worktree,
5694 struct got_fileindex *fileindex, struct got_repository *repo,
5695 struct got_reference *branch, struct got_object_id *base_commit_id,
5696 got_worktree_checkout_cb progress_cb, void *progress_arg)
5698 const struct got_error *err, *unlockerr, *sync_err;
5699 struct got_reference *resolved = NULL;
5700 char *fileindex_path = NULL;
5701 struct got_object_id *tree_id = NULL;
5702 struct revert_file_args rfa;
5704 err = lock_worktree(worktree, LOCK_EX);
5705 if (err)
5706 return err;
5708 err = got_ref_open(&resolved, repo,
5709 got_ref_get_symref_target(branch), 0);
5710 if (err)
5711 goto done;
5713 err = got_worktree_set_head_ref(worktree, resolved);
5714 if (err)
5715 goto done;
5717 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5718 if (err)
5719 goto done;
5721 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5722 worktree->path_prefix);
5723 if (err)
5724 goto done;
5726 err = delete_histedit_refs(worktree, repo);
5727 if (err)
5728 goto done;
5730 err = get_fileindex_path(&fileindex_path, worktree);
5731 if (err)
5732 goto done;
5734 rfa.worktree = worktree;
5735 rfa.fileindex = fileindex;
5736 rfa.progress_cb = progress_cb;
5737 rfa.progress_arg = progress_arg;
5738 rfa.patch_cb = NULL;
5739 rfa.patch_arg = NULL;
5740 rfa.repo = repo;
5741 err = worktree_status(worktree, "", fileindex, repo,
5742 revert_file, &rfa, NULL, NULL, 0, 0);
5743 if (err)
5744 goto sync;
5746 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5747 repo, progress_cb, progress_arg, NULL, NULL);
5748 sync:
5749 sync_err = sync_fileindex(fileindex, fileindex_path);
5750 if (sync_err && err == NULL)
5751 err = sync_err;
5752 done:
5753 got_ref_close(resolved);
5754 free(tree_id);
5755 free(fileindex_path);
5757 unlockerr = lock_worktree(worktree, LOCK_SH);
5758 if (unlockerr && err == NULL)
5759 err = unlockerr;
5760 return err;
5763 const struct got_error *
5764 got_worktree_histedit_complete(struct got_worktree *worktree,
5765 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5766 struct got_reference *edited_branch, struct got_repository *repo)
5768 const struct got_error *err, *unlockerr;
5769 struct got_object_id *new_head_commit_id = NULL;
5770 struct got_reference *resolved = NULL;
5772 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5773 if (err)
5774 return err;
5776 err = got_ref_open(&resolved, repo,
5777 got_ref_get_symref_target(edited_branch), 0);
5778 if (err)
5779 goto done;
5781 err = got_ref_change_ref(resolved, new_head_commit_id);
5782 if (err)
5783 goto done;
5785 err = got_ref_write(resolved, repo);
5786 if (err)
5787 goto done;
5789 err = got_worktree_set_head_ref(worktree, resolved);
5790 if (err)
5791 goto done;
5793 err = delete_histedit_refs(worktree, repo);
5794 done:
5795 if (fileindex)
5796 got_fileindex_free(fileindex);
5797 free(new_head_commit_id);
5798 unlockerr = lock_worktree(worktree, LOCK_SH);
5799 if (unlockerr && err == NULL)
5800 err = unlockerr;
5801 return err;
5804 const struct got_error *
5805 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5806 struct got_object_id *commit_id, struct got_repository *repo)
5808 const struct got_error *err;
5809 char *commit_ref_name;
5811 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5812 if (err)
5813 return err;
5815 err = store_commit_id(commit_ref_name, commit_id, repo);
5816 if (err)
5817 goto done;
5819 err = delete_ref(commit_ref_name, repo);
5820 done:
5821 free(commit_ref_name);
5822 return err;
5825 const struct got_error *
5826 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5827 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5828 struct got_worktree *worktree, const char *refname,
5829 struct got_repository *repo)
5831 const struct got_error *err = NULL;
5832 char *fileindex_path = NULL;
5833 struct check_rebase_ok_arg ok_arg;
5835 *fileindex = NULL;
5836 *branch_ref = NULL;
5837 *base_branch_ref = NULL;
5839 err = lock_worktree(worktree, LOCK_EX);
5840 if (err)
5841 return err;
5843 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5844 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5845 "cannot integrate a branch into itself; "
5846 "update -b or different branch name required");
5847 goto done;
5850 err = open_fileindex(fileindex, &fileindex_path, worktree);
5851 if (err)
5852 goto done;
5854 /* Preconditions are the same as for rebase. */
5855 ok_arg.worktree = worktree;
5856 ok_arg.repo = repo;
5857 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5858 &ok_arg);
5859 if (err)
5860 goto done;
5862 err = got_ref_open(branch_ref, repo, refname, 1);
5863 if (err)
5864 goto done;
5866 err = got_ref_open(base_branch_ref, repo,
5867 got_worktree_get_head_ref_name(worktree), 1);
5868 done:
5869 if (err) {
5870 if (*branch_ref) {
5871 got_ref_close(*branch_ref);
5872 *branch_ref = NULL;
5874 if (*base_branch_ref) {
5875 got_ref_close(*base_branch_ref);
5876 *base_branch_ref = NULL;
5878 if (*fileindex) {
5879 got_fileindex_free(*fileindex);
5880 *fileindex = NULL;
5882 lock_worktree(worktree, LOCK_SH);
5884 return err;
5887 const struct got_error *
5888 got_worktree_integrate_continue(struct got_worktree *worktree,
5889 struct got_fileindex *fileindex, struct got_repository *repo,
5890 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5891 got_worktree_checkout_cb progress_cb, void *progress_arg,
5892 got_cancel_cb cancel_cb, void *cancel_arg)
5894 const struct got_error *err = NULL, *sync_err, *unlockerr;
5895 char *fileindex_path = NULL;
5896 struct got_object_id *tree_id = NULL, *commit_id = NULL;
5898 err = get_fileindex_path(&fileindex_path, worktree);
5899 if (err)
5900 goto done;
5902 err = got_ref_resolve(&commit_id, repo, branch_ref);
5903 if (err)
5904 goto done;
5906 err = got_object_id_by_path(&tree_id, repo, commit_id,
5907 worktree->path_prefix);
5908 if (err)
5909 goto done;
5911 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5912 if (err)
5913 goto done;
5915 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5916 progress_cb, progress_arg, cancel_cb, cancel_arg);
5917 if (err)
5918 goto sync;
5920 err = got_ref_change_ref(base_branch_ref, commit_id);
5921 if (err)
5922 goto sync;
5924 err = got_ref_write(base_branch_ref, repo);
5925 sync:
5926 sync_err = sync_fileindex(fileindex, fileindex_path);
5927 if (sync_err && err == NULL)
5928 err = sync_err;
5930 done:
5931 unlockerr = got_ref_unlock(branch_ref);
5932 if (unlockerr && err == NULL)
5933 err = unlockerr;
5934 got_ref_close(branch_ref);
5936 unlockerr = got_ref_unlock(base_branch_ref);
5937 if (unlockerr && err == NULL)
5938 err = unlockerr;
5939 got_ref_close(base_branch_ref);
5941 got_fileindex_free(fileindex);
5942 free(fileindex_path);
5943 free(tree_id);
5945 unlockerr = lock_worktree(worktree, LOCK_SH);
5946 if (unlockerr && err == NULL)
5947 err = unlockerr;
5948 return err;
5951 const struct got_error *
5952 got_worktree_integrate_abort(struct got_worktree *worktree,
5953 struct got_fileindex *fileindex, struct got_repository *repo,
5954 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
5956 const struct got_error *err = NULL, *unlockerr = NULL;
5958 got_fileindex_free(fileindex);
5960 err = lock_worktree(worktree, LOCK_SH);
5962 unlockerr = got_ref_unlock(branch_ref);
5963 if (unlockerr && err == NULL)
5964 err = unlockerr;
5965 got_ref_close(branch_ref);
5967 unlockerr = got_ref_unlock(base_branch_ref);
5968 if (unlockerr && err == NULL)
5969 err = unlockerr;
5970 got_ref_close(base_branch_ref);
5972 return err;
5975 struct check_stage_ok_arg {
5976 struct got_object_id *head_commit_id;
5977 struct got_worktree *worktree;
5978 struct got_fileindex *fileindex;
5979 struct got_repository *repo;
5980 int have_changes;
5983 const struct got_error *
5984 check_stage_ok(void *arg, unsigned char status,
5985 unsigned char staged_status, const char *relpath,
5986 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5987 struct got_object_id *commit_id, int dirfd, const char *de_name)
5989 struct check_stage_ok_arg *a = arg;
5990 const struct got_error *err = NULL;
5991 struct got_fileindex_entry *ie;
5992 struct got_object_id base_commit_id;
5993 struct got_object_id *base_commit_idp = NULL;
5994 char *in_repo_path = NULL, *p;
5996 if (status == GOT_STATUS_UNVERSIONED ||
5997 status == GOT_STATUS_NO_CHANGE)
5998 return NULL;
5999 if (status == GOT_STATUS_NONEXISTENT)
6000 return got_error_set_errno(ENOENT, relpath);
6002 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6003 if (ie == NULL)
6004 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6006 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6007 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6008 relpath) == -1)
6009 return got_error_from_errno("asprintf");
6011 if (got_fileindex_entry_has_commit(ie)) {
6012 memcpy(base_commit_id.sha1, ie->commit_sha1,
6013 SHA1_DIGEST_LENGTH);
6014 base_commit_idp = &base_commit_id;
6017 if (status == GOT_STATUS_CONFLICT) {
6018 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6019 goto done;
6020 } else if (status != GOT_STATUS_ADD &&
6021 status != GOT_STATUS_MODIFY &&
6022 status != GOT_STATUS_DELETE) {
6023 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6024 goto done;
6027 a->have_changes = 1;
6029 p = in_repo_path;
6030 while (p[0] == '/')
6031 p++;
6032 err = check_out_of_date(p, status, staged_status,
6033 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6034 GOT_ERR_STAGE_OUT_OF_DATE);
6035 done:
6036 free(in_repo_path);
6037 return err;
6040 struct stage_path_arg {
6041 struct got_worktree *worktree;
6042 struct got_fileindex *fileindex;
6043 struct got_repository *repo;
6044 got_worktree_status_cb status_cb;
6045 void *status_arg;
6046 got_worktree_patch_cb patch_cb;
6047 void *patch_arg;
6048 int staged_something;
6051 static const struct got_error *
6052 stage_path(void *arg, unsigned char status,
6053 unsigned char staged_status, const char *relpath,
6054 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6055 struct got_object_id *commit_id, int dirfd, const char *de_name)
6057 struct stage_path_arg *a = arg;
6058 const struct got_error *err = NULL;
6059 struct got_fileindex_entry *ie;
6060 char *ondisk_path = NULL, *path_content = NULL;
6061 uint32_t stage;
6062 struct got_object_id *new_staged_blob_id = NULL;
6064 if (status == GOT_STATUS_UNVERSIONED)
6065 return NULL;
6067 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6068 if (ie == NULL)
6069 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6071 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6072 relpath)== -1)
6073 return got_error_from_errno("asprintf");
6075 switch (status) {
6076 case GOT_STATUS_ADD:
6077 case GOT_STATUS_MODIFY:
6078 if (a->patch_cb) {
6079 if (status == GOT_STATUS_ADD) {
6080 int choice = GOT_PATCH_CHOICE_NONE;
6081 err = (*a->patch_cb)(&choice, a->patch_arg,
6082 status, ie->path, NULL, 1, 1);
6083 if (err)
6084 break;
6085 if (choice != GOT_PATCH_CHOICE_YES)
6086 break;
6087 } else {
6088 err = create_patched_content(&path_content, 0,
6089 staged_blob_id ? staged_blob_id : blob_id,
6090 ondisk_path, dirfd, de_name, ie->path,
6091 a->repo, a->patch_cb, a->patch_arg);
6092 if (err || path_content == NULL)
6093 break;
6096 err = got_object_blob_create(&new_staged_blob_id,
6097 path_content ? path_content : ondisk_path, a->repo);
6098 if (err)
6099 break;
6100 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6101 SHA1_DIGEST_LENGTH);
6102 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6103 stage = GOT_FILEIDX_STAGE_ADD;
6104 else
6105 stage = GOT_FILEIDX_STAGE_MODIFY;
6106 got_fileindex_entry_stage_set(ie, stage);
6107 a->staged_something = 1;
6108 if (a->status_cb == NULL)
6109 break;
6110 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6111 get_staged_status(ie), relpath, blob_id,
6112 new_staged_blob_id, NULL, dirfd, de_name);
6113 break;
6114 case GOT_STATUS_DELETE:
6115 if (staged_status == GOT_STATUS_DELETE)
6116 break;
6117 if (a->patch_cb) {
6118 int choice = GOT_PATCH_CHOICE_NONE;
6119 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6120 ie->path, NULL, 1, 1);
6121 if (err)
6122 break;
6123 if (choice == GOT_PATCH_CHOICE_NO)
6124 break;
6125 if (choice != GOT_PATCH_CHOICE_YES) {
6126 err = got_error(GOT_ERR_PATCH_CHOICE);
6127 break;
6130 stage = GOT_FILEIDX_STAGE_DELETE;
6131 got_fileindex_entry_stage_set(ie, stage);
6132 a->staged_something = 1;
6133 if (a->status_cb == NULL)
6134 break;
6135 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6136 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6137 de_name);
6138 break;
6139 case GOT_STATUS_NO_CHANGE:
6140 break;
6141 case GOT_STATUS_CONFLICT:
6142 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6143 break;
6144 case GOT_STATUS_NONEXISTENT:
6145 err = got_error_set_errno(ENOENT, relpath);
6146 break;
6147 default:
6148 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6149 break;
6152 if (path_content && unlink(path_content) == -1 && err == NULL)
6153 err = got_error_from_errno2("unlink", path_content);
6154 free(path_content);
6155 free(ondisk_path);
6156 free(new_staged_blob_id);
6157 return err;
6160 const struct got_error *
6161 got_worktree_stage(struct got_worktree *worktree,
6162 struct got_pathlist_head *paths,
6163 got_worktree_status_cb status_cb, void *status_arg,
6164 got_worktree_patch_cb patch_cb, void *patch_arg,
6165 struct got_repository *repo)
6167 const struct got_error *err = NULL, *sync_err, *unlockerr;
6168 struct got_pathlist_entry *pe;
6169 struct got_fileindex *fileindex = NULL;
6170 char *fileindex_path = NULL;
6171 struct got_reference *head_ref = NULL;
6172 struct got_object_id *head_commit_id = NULL;
6173 struct check_stage_ok_arg oka;
6174 struct stage_path_arg spa;
6176 err = lock_worktree(worktree, LOCK_EX);
6177 if (err)
6178 return err;
6180 err = got_ref_open(&head_ref, repo,
6181 got_worktree_get_head_ref_name(worktree), 0);
6182 if (err)
6183 goto done;
6184 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6185 if (err)
6186 goto done;
6187 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6188 if (err)
6189 goto done;
6191 /* Check pre-conditions before staging anything. */
6192 oka.head_commit_id = head_commit_id;
6193 oka.worktree = worktree;
6194 oka.fileindex = fileindex;
6195 oka.repo = repo;
6196 oka.have_changes = 0;
6197 TAILQ_FOREACH(pe, paths, entry) {
6198 err = worktree_status(worktree, pe->path, fileindex, repo,
6199 check_stage_ok, &oka, NULL, NULL, 0, 0);
6200 if (err)
6201 goto done;
6203 if (!oka.have_changes) {
6204 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6205 goto done;
6208 spa.worktree = worktree;
6209 spa.fileindex = fileindex;
6210 spa.repo = repo;
6211 spa.patch_cb = patch_cb;
6212 spa.patch_arg = patch_arg;
6213 spa.status_cb = status_cb;
6214 spa.status_arg = status_arg;
6215 spa.staged_something = 0;
6216 TAILQ_FOREACH(pe, paths, entry) {
6217 err = worktree_status(worktree, pe->path, fileindex, repo,
6218 stage_path, &spa, NULL, NULL, 0, 0);
6219 if (err)
6220 goto done;
6222 if (!spa.staged_something) {
6223 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6224 goto done;
6227 sync_err = sync_fileindex(fileindex, fileindex_path);
6228 if (sync_err && err == NULL)
6229 err = sync_err;
6230 done:
6231 if (head_ref)
6232 got_ref_close(head_ref);
6233 free(head_commit_id);
6234 free(fileindex_path);
6235 if (fileindex)
6236 got_fileindex_free(fileindex);
6237 unlockerr = lock_worktree(worktree, LOCK_SH);
6238 if (unlockerr && err == NULL)
6239 err = unlockerr;
6240 return err;
6243 struct unstage_path_arg {
6244 struct got_worktree *worktree;
6245 struct got_fileindex *fileindex;
6246 struct got_repository *repo;
6247 got_worktree_checkout_cb progress_cb;
6248 void *progress_arg;
6249 got_worktree_patch_cb patch_cb;
6250 void *patch_arg;
6253 static const struct got_error *
6254 create_unstaged_content(char **path_unstaged_content,
6255 char **path_new_staged_content, struct got_object_id *blob_id,
6256 struct got_object_id *staged_blob_id, const char *relpath,
6257 struct got_repository *repo,
6258 got_worktree_patch_cb patch_cb, void *patch_arg)
6260 const struct got_error *err;
6261 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6262 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6263 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6264 struct stat sb1, sb2;
6265 struct got_diff_changes *changes = NULL;
6266 struct got_diff_state *ds = NULL;
6267 struct got_diff_args *args = NULL;
6268 struct got_diff_change *change;
6269 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6270 int have_content = 0, have_rejected_content = 0;
6272 *path_unstaged_content = NULL;
6273 *path_new_staged_content = NULL;
6275 err = got_object_id_str(&label1, blob_id);
6276 if (err)
6277 return err;
6278 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6279 if (err)
6280 goto done;
6282 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6283 if (err)
6284 goto done;
6286 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6287 if (err)
6288 goto done;
6290 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6291 if (err)
6292 goto done;
6294 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6295 if (err)
6296 goto done;
6298 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6299 if (err)
6300 goto done;
6302 if (stat(path1, &sb1) == -1) {
6303 err = got_error_from_errno2("stat", path1);
6304 goto done;
6307 if (stat(path2, &sb2) == -1) {
6308 err = got_error_from_errno2("stat", path2);
6309 goto done;
6312 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6313 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6314 if (err)
6315 goto done;
6317 err = got_opentemp_named(path_unstaged_content, &outfile,
6318 "got-unstaged-content");
6319 if (err)
6320 goto done;
6321 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6322 "got-new-staged-content");
6323 if (err)
6324 goto done;
6326 if (fseek(f1, 0L, SEEK_SET) == -1) {
6327 err = got_ferror(f1, GOT_ERR_IO);
6328 goto done;
6330 if (fseek(f2, 0L, SEEK_SET) == -1) {
6331 err = got_ferror(f2, GOT_ERR_IO);
6332 goto done;
6334 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6335 int choice;
6336 err = apply_or_reject_change(&choice, change, ++n,
6337 changes->nchanges, ds, args, diff_flags, relpath,
6338 f1, f2, &line_cur1, &line_cur2,
6339 outfile, rejectfile, patch_cb, patch_arg);
6340 if (err)
6341 goto done;
6342 if (choice == GOT_PATCH_CHOICE_YES)
6343 have_content = 1;
6344 else
6345 have_rejected_content = 1;
6346 if (choice == GOT_PATCH_CHOICE_QUIT)
6347 break;
6349 if (have_content || have_rejected_content)
6350 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6351 outfile, rejectfile);
6352 done:
6353 free(label1);
6354 if (blob)
6355 got_object_blob_close(blob);
6356 if (staged_blob)
6357 got_object_blob_close(staged_blob);
6358 if (f1 && fclose(f1) == EOF && err == NULL)
6359 err = got_error_from_errno2("fclose", path1);
6360 if (f2 && fclose(f2) == EOF && err == NULL)
6361 err = got_error_from_errno2("fclose", path2);
6362 if (outfile && fclose(outfile) == EOF && err == NULL)
6363 err = got_error_from_errno2("fclose", *path_unstaged_content);
6364 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6365 err = got_error_from_errno2("fclose", *path_new_staged_content);
6366 if (path1 && unlink(path1) == -1 && err == NULL)
6367 err = got_error_from_errno2("unlink", path1);
6368 if (path2 && unlink(path2) == -1 && err == NULL)
6369 err = got_error_from_errno2("unlink", path2);
6370 if (err || !have_content) {
6371 if (*path_unstaged_content &&
6372 unlink(*path_unstaged_content) == -1 && err == NULL)
6373 err = got_error_from_errno2("unlink",
6374 *path_unstaged_content);
6375 free(*path_unstaged_content);
6376 *path_unstaged_content = NULL;
6378 if (err || !have_rejected_content) {
6379 if (*path_new_staged_content &&
6380 unlink(*path_new_staged_content) == -1 && err == NULL)
6381 err = got_error_from_errno2("unlink",
6382 *path_new_staged_content);
6383 free(*path_new_staged_content);
6384 *path_new_staged_content = NULL;
6386 free(args);
6387 if (ds) {
6388 got_diff_state_free(ds);
6389 free(ds);
6391 if (changes)
6392 got_diff_free_changes(changes);
6393 free(path1);
6394 free(path2);
6395 return err;
6398 static const struct got_error *
6399 unstage_path(void *arg, unsigned char status,
6400 unsigned char staged_status, const char *relpath,
6401 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6402 struct got_object_id *commit_id, int dirfd, const char *de_name)
6404 const struct got_error *err = NULL;
6405 struct unstage_path_arg *a = arg;
6406 struct got_fileindex_entry *ie;
6407 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6408 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6409 char *path_new_staged_content = NULL;
6410 char *id_str = NULL, *label_orig = NULL;
6411 int local_changes_subsumed;
6412 struct stat sb;
6414 if (staged_status != GOT_STATUS_ADD &&
6415 staged_status != GOT_STATUS_MODIFY &&
6416 staged_status != GOT_STATUS_DELETE)
6417 return NULL;
6419 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6420 if (ie == NULL)
6421 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6423 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6424 == -1)
6425 return got_error_from_errno("asprintf");
6427 err = got_object_id_str(&id_str,
6428 commit_id ? commit_id : a->worktree->base_commit_id);
6429 if (err)
6430 goto done;
6431 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6432 id_str) == -1) {
6433 err = got_error_from_errno("asprintf");
6434 goto done;
6437 switch (staged_status) {
6438 case GOT_STATUS_MODIFY:
6439 err = got_object_open_as_blob(&blob_base, a->repo,
6440 blob_id, 8192);
6441 if (err)
6442 break;
6443 /* fall through */
6444 case GOT_STATUS_ADD:
6445 if (a->patch_cb) {
6446 if (staged_status == GOT_STATUS_ADD) {
6447 int choice = GOT_PATCH_CHOICE_NONE;
6448 err = (*a->patch_cb)(&choice, a->patch_arg,
6449 staged_status, ie->path, NULL, 1, 1);
6450 if (err)
6451 break;
6452 if (choice != GOT_PATCH_CHOICE_YES)
6453 break;
6454 } else {
6455 err = create_unstaged_content(
6456 &path_unstaged_content,
6457 &path_new_staged_content, blob_id,
6458 staged_blob_id, ie->path, a->repo,
6459 a->patch_cb, a->patch_arg);
6460 if (err || path_unstaged_content == NULL)
6461 break;
6462 if (path_new_staged_content) {
6463 err = got_object_blob_create(
6464 &staged_blob_id,
6465 path_new_staged_content,
6466 a->repo);
6467 if (err)
6468 break;
6469 memcpy(ie->staged_blob_sha1,
6470 staged_blob_id->sha1,
6471 SHA1_DIGEST_LENGTH);
6473 err = merge_file(&local_changes_subsumed,
6474 a->worktree, blob_base, ondisk_path,
6475 relpath, got_fileindex_perms_to_st(ie),
6476 path_unstaged_content, label_orig,
6477 "unstaged", a->repo, a->progress_cb,
6478 a->progress_arg);
6479 if (err == NULL &&
6480 path_new_staged_content == NULL)
6481 got_fileindex_entry_stage_set(ie,
6482 GOT_FILEIDX_STAGE_NONE);
6483 break; /* Done with this file. */
6486 err = got_object_open_as_blob(&blob_staged, a->repo,
6487 staged_blob_id, 8192);
6488 if (err)
6489 break;
6490 err = merge_blob(&local_changes_subsumed, a->worktree,
6491 blob_base, ondisk_path, relpath,
6492 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6493 commit_id ? commit_id : a->worktree->base_commit_id,
6494 a->repo, a->progress_cb, a->progress_arg);
6495 if (err == NULL)
6496 got_fileindex_entry_stage_set(ie,
6497 GOT_FILEIDX_STAGE_NONE);
6498 break;
6499 case GOT_STATUS_DELETE:
6500 if (a->patch_cb) {
6501 int choice = GOT_PATCH_CHOICE_NONE;
6502 err = (*a->patch_cb)(&choice, a->patch_arg,
6503 staged_status, ie->path, NULL, 1, 1);
6504 if (err)
6505 break;
6506 if (choice == GOT_PATCH_CHOICE_NO)
6507 break;
6508 if (choice != GOT_PATCH_CHOICE_YES) {
6509 err = got_error(GOT_ERR_PATCH_CHOICE);
6510 break;
6513 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6514 err = get_file_status(&status, &sb, ie, ondisk_path,
6515 dirfd, de_name, a->repo);
6516 if (err)
6517 break;
6518 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6519 break;
6521 done:
6522 free(ondisk_path);
6523 if (path_unstaged_content &&
6524 unlink(path_unstaged_content) == -1 && err == NULL)
6525 err = got_error_from_errno2("unlink", path_unstaged_content);
6526 if (path_new_staged_content &&
6527 unlink(path_new_staged_content) == -1 && err == NULL)
6528 err = got_error_from_errno2("unlink", path_new_staged_content);
6529 free(path_unstaged_content);
6530 free(path_new_staged_content);
6531 if (blob_base)
6532 got_object_blob_close(blob_base);
6533 if (blob_staged)
6534 got_object_blob_close(blob_staged);
6535 free(id_str);
6536 free(label_orig);
6537 return err;
6540 const struct got_error *
6541 got_worktree_unstage(struct got_worktree *worktree,
6542 struct got_pathlist_head *paths,
6543 got_worktree_checkout_cb progress_cb, void *progress_arg,
6544 got_worktree_patch_cb patch_cb, void *patch_arg,
6545 struct got_repository *repo)
6547 const struct got_error *err = NULL, *sync_err, *unlockerr;
6548 struct got_pathlist_entry *pe;
6549 struct got_fileindex *fileindex = NULL;
6550 char *fileindex_path = NULL;
6551 struct unstage_path_arg upa;
6553 err = lock_worktree(worktree, LOCK_EX);
6554 if (err)
6555 return err;
6557 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6558 if (err)
6559 goto done;
6561 upa.worktree = worktree;
6562 upa.fileindex = fileindex;
6563 upa.repo = repo;
6564 upa.progress_cb = progress_cb;
6565 upa.progress_arg = progress_arg;
6566 upa.patch_cb = patch_cb;
6567 upa.patch_arg = patch_arg;
6568 TAILQ_FOREACH(pe, paths, entry) {
6569 err = worktree_status(worktree, pe->path, fileindex, repo,
6570 unstage_path, &upa, NULL, NULL, 0, 0);
6571 if (err)
6572 goto done;
6575 sync_err = sync_fileindex(fileindex, fileindex_path);
6576 if (sync_err && err == NULL)
6577 err = sync_err;
6578 done:
6579 free(fileindex_path);
6580 if (fileindex)
6581 got_fileindex_free(fileindex);
6582 unlockerr = lock_worktree(worktree, LOCK_SH);
6583 if (unlockerr && err == NULL)
6584 err = unlockerr;
6585 return err;