Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
68 {
69 const struct got_error *err = NULL;
70 char *path;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
76 free(path);
77 return err;
78 }
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
82 {
83 const struct got_error *err = NULL;
84 FILE *tmpfile = NULL;
85 char *tmppath = NULL;
86 char *path = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
90 path = NULL;
91 goto done;
92 }
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
95 if (err)
96 goto done;
98 if (content) {
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
102 goto done;
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
108 unlink(tmppath);
109 goto done;
112 done:
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
115 free(tmppath);
116 return err;
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
123 char *path;
124 int fd = -1;
125 ssize_t n;
126 struct stat sb;
128 *content = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
132 path = NULL;
133 goto done;
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
137 if (fd == -1) {
138 if (errno == ENOENT)
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
140 else
141 err = got_error_from_errno2("open", path);
142 goto done;
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
147 goto done;
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
152 goto done;
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
157 goto done;
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
164 goto done;
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
168 goto done;
170 (*content)[sb.st_size - 1] = '\0';
172 done:
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
175 free(path);
176 if (err) {
177 free(*content);
178 *content = NULL;
180 return err;
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
187 char *refstr = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
191 if (refstr == NULL)
192 return got_error_from_errno("got_ref_to_str");
193 } else {
194 refstr = strdup(got_ref_get_name(head_ref));
195 if (refstr == NULL)
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 free(refstr);
200 return err;
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
209 uuid_t uuid;
210 uint32_t uuid_status;
211 int obj_type;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
220 goto done;
223 err = got_ref_resolve(&commit_id, repo, head_ref);
224 if (err)
225 return err;
226 err = got_object_get_type(&obj_type, repo, commit_id);
227 if (err)
228 return err;
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
240 goto done;
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
246 goto done;
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
250 goto done;
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
255 if (err)
256 goto done;
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
260 if (err)
261 goto done;
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
265 if (err)
266 goto done;
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
270 if (err)
271 goto done;
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 if (err)
274 goto done;
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
279 if (err)
280 goto done;
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
285 if (err)
286 goto done;
288 /* Generate UUID. */
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
292 goto done;
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
297 goto done;
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 if (err)
301 goto done;
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
306 goto done;
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
309 if (err)
310 goto done;
312 done:
313 free(commit_id);
314 free(path_got);
315 free(formatstr);
316 free(absprefix);
317 free(basestr);
318 free(uuidstr);
319 return err;
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
326 char *path_got;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
332 const char *errstr;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
336 *worktree = NULL;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
340 path_got = NULL;
341 goto done;
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
346 path_lock = NULL;
347 goto done;
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 if (fd == -1) {
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
354 goto done;
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 if (err)
359 goto done;
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 if (errstr) {
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
365 goto done;
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
369 goto done;
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
375 goto done;
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = realpath(path, NULL);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno2("realpath", path);
382 goto done;
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
386 if (err)
387 goto done;
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
391 if (err)
392 goto done;
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
396 if (err)
397 goto done;
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 if (err)
401 goto done;
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
405 goto done;
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
409 if (err)
410 goto done;
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 base_commit_id_str);
414 if (err)
415 goto done;
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
419 done:
420 if (repo)
421 got_repo_close(repo);
422 free(path_got);
423 free(path_lock);
424 free(base_commit_id_str);
425 free(uuidstr);
426 free(formatstr);
427 if (err) {
428 if (fd != -1)
429 close(fd);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
432 *worktree = NULL;
433 } else
434 (*worktree)->lockfd = fd;
436 return err;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
444 do {
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 return err;
448 if (*worktree)
449 return NULL;
450 path = dirname(path);
451 if (path == NULL)
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
471 free(worktree);
472 return err;
475 const char *
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
481 const char *
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
487 const char *
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
505 free(absprefix);
506 return NULL;
509 const char *
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
525 path_got = NULL;
526 goto done;
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
532 goto done;
535 err = write_head_ref(path_got, head_ref);
536 if (err)
537 goto done;
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
541 done:
542 free(path_got);
543 if (err)
544 free(head_ref_name);
545 return err;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
560 char *id_str = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
566 path_got = NULL;
567 goto done;
570 err = got_object_open(&obj, repo, commit_id);
571 if (err)
572 return err;
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
576 goto done;
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
581 if (err)
582 goto done;
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
584 if (err)
585 goto done;
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
591 goto done;
593 done:
594 if (obj)
595 got_object_close(obj);
596 free(id_str);
597 free(path_got);
598 return err;
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
608 return NULL;
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
615 char *abspath;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 struct stat sb;
623 err = NULL;
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
631 free(abspath);
632 return err;
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
639 uint8_t fbuf1[8192];
640 uint8_t fbuf2[8192];
641 size_t flen1 = 0, flen2 = 0;
643 *same = 1;
645 for (;;) {
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
649 break;
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
654 break;
656 if (flen1 == 0) {
657 if (flen2 != 0)
658 *same = 0;
659 break;
660 } else if (flen2 == 0) {
661 if (flen1 != 0)
662 *same = 0;
663 break;
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
666 *same = 0;
667 break;
669 } else {
670 *same = 0;
671 break;
675 return err;
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
682 struct stat sb;
683 size_t size1, size2;
684 FILE *f1 = NULL, *f2 = NULL;
686 *same = 1;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
690 goto done;
692 size1 = sb.st_size;
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
696 goto done;
698 size2 = sb.st_size;
700 if (size1 != size2) {
701 *same = 0;
702 return NULL;
705 f1 = fopen(f1_path, "r");
706 if (f1 == NULL)
707 return got_error_from_errno2("fopen", f1_path);
709 f2 = fopen(f2_path, "r");
710 if (f2 == NULL) {
711 err = got_error_from_errno2("fopen", f2_path);
712 goto done;
715 err = check_file_contents_equal(same, f1, f2);
716 done:
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
722 return err;
725 /*
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
729 */
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
739 int merged_fd = -1;
740 FILE *f_orig = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
743 int overlapcnt = 0;
744 char *parent;
746 *local_changes_subsumed = 0;
748 parent = dirname(ondisk_path);
749 if (parent == NULL)
750 return got_error_from_errno2("dirname", ondisk_path);
752 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 return got_error_from_errno("asprintf");
755 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
756 if (err)
757 goto done;
759 free(base_path);
760 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 err = got_error_from_errno("asprintf");
762 base_path = NULL;
763 goto done;
766 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
767 if (err)
768 goto done;
769 if (blob_orig) {
770 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
771 blob_orig);
772 if (err)
773 goto done;
774 } else {
775 /*
776 * If the file has no blob, this is an "add vs add" conflict,
777 * and we simply use an empty ancestor file to make both files
778 * appear in the merged result in their entirety.
779 */
782 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
784 if (err)
785 goto done;
787 err = (*progress_cb)(progress_arg,
788 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
789 if (err)
790 goto done;
792 if (fsync(merged_fd) != 0) {
793 err = got_error_from_errno("fsync");
794 goto done;
797 /* Check if a clean merge has subsumed all local changes. */
798 if (overlapcnt == 0) {
799 err = check_files_equal(local_changes_subsumed, deriv_path,
800 merged_path);
801 if (err)
802 goto done;
805 if (fchmod(merged_fd, st_mode) != 0) {
806 err = got_error_from_errno2("fchmod", merged_path);
807 goto done;
810 if (rename(merged_path, ondisk_path) != 0) {
811 err = got_error_from_errno3("rename", merged_path,
812 ondisk_path);
813 goto done;
815 done:
816 if (err) {
817 if (merged_path)
818 unlink(merged_path);
820 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
821 err = got_error_from_errno("close");
822 if (f_orig && fclose(f_orig) != 0 && err == NULL)
823 err = got_error_from_errno("fclose");
824 free(merged_path);
825 free(base_path);
826 if (blob_orig_path) {
827 unlink(blob_orig_path);
828 free(blob_orig_path);
830 return err;
833 static const struct got_error *
834 update_symlink(const char *ondisk_path, const char *target_path,
835 size_t target_len)
837 /* This is not atomic but matches what 'ln -sf' does. */
838 if (unlink(ondisk_path) == -1)
839 return got_error_from_errno2("unlink", ondisk_path);
840 if (symlink(target_path, ondisk_path) == -1)
841 return got_error_from_errno3("symlink", target_path,
842 ondisk_path);
843 return NULL;
846 /*
847 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
848 * in the work tree with a file that contains conflict markers and the
849 * conflicting target paths of the original version and two derived versions
850 * of a symlink.
851 */
852 static const struct got_error *
853 install_symlink_conflict(const char *deriv_target,
854 struct got_object_id *deriv_base_commit_id, const char *orig_target,
855 const char *label_orig, const char *local_target, const char *ondisk_path)
857 const struct got_error *err;
858 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
859 FILE *f = NULL;
861 err = got_object_id_str(&id_str, deriv_base_commit_id);
862 if (err)
863 return got_error_from_errno("asprintf");
865 if (asprintf(&label_deriv, "%s: commit %s",
866 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
867 err = got_error_from_errno("asprintf");
868 goto done;
871 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
872 if (err)
873 goto done;
875 if (fprintf(f, "%s: Could not install symbolic link because of merge "
876 "conflict.\nln(1) may be used to fix the situation. If this is "
877 "intended to be a\nregular file instead then its expected "
878 "contents may be filled in.\nThe following conflicting symlink "
879 "target paths were found:\n"
880 "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n", getprogname(),
881 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv, deriv_target,
882 orig_target ? label_orig : "",
883 orig_target ? "\n" : "",
884 orig_target ? orig_target : "",
885 orig_target ? "\n" : "",
886 GOT_DIFF_CONFLICT_MARKER_SEP,
887 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
888 err = got_error_from_errno2("fprintf", path);
889 goto done;
892 if (unlink(ondisk_path) == -1) {
893 err = got_error_from_errno2("unlink", ondisk_path);
894 goto done;
896 if (rename(path, ondisk_path) == -1) {
897 err = got_error_from_errno3("rename", path, ondisk_path);
898 goto done;
900 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
901 err = got_error_from_errno2("chmod", ondisk_path);
902 goto done;
904 done:
905 if (f != NULL && fclose(f) == EOF && err == NULL)
906 err = got_error_from_errno2("fclose", path);
907 free(path);
908 free(id_str);
909 free(label_deriv);
910 return err;
913 /*
914 * Merge a symlink into the work tree, where blob_orig acts as the common
915 * ancestor, blob_deriv acts as the first derived version, and the symlink
916 * on disk acts as the second derived version.
917 * Assume that contents of both blobs represent symlinks.
918 */
919 static const struct got_error *
920 merge_symlink(struct got_worktree *worktree,
921 struct got_blob_object *blob_orig, const char *ondisk_path,
922 const char *path, uint16_t st_mode, const char *label_orig,
923 struct got_blob_object *blob_deriv,
924 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
925 got_worktree_checkout_cb progress_cb, void *progress_arg)
927 const struct got_error *err = NULL;
928 char *ancestor_target = NULL, *deriv_target = NULL;
929 struct stat sb;
930 ssize_t ondisk_len, deriv_len;
931 char ondisk_target[PATH_MAX];
932 int have_local_change = 0;
933 int have_incoming_change = 0;
935 if (lstat(ondisk_path, &sb) == -1)
936 return got_error_from_errno2("lstat", ondisk_path);
938 if (!S_ISLNK(sb.st_mode)) {
939 /* TODO symlink is obstructed; do something */
940 return got_error_path(ondisk_path, GOT_ERR_FILE_OBSTRUCTED);
943 ondisk_len = readlink(ondisk_path, ondisk_target,
944 sizeof(ondisk_target));
945 if (ondisk_len == -1) {
946 err = got_error_from_errno2("readlink",
947 ondisk_path);
948 goto done;
950 ondisk_target[ondisk_len] = '\0';
952 if (blob_orig) {
953 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
954 if (err)
955 goto done;
958 err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
959 if (err)
960 goto done;
962 if (ancestor_target == NULL ||
963 (ondisk_len != strlen(ancestor_target) ||
964 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
965 have_local_change = 1;
967 deriv_len = strlen(deriv_target);
968 if (ancestor_target == NULL ||
969 (deriv_len != strlen(ancestor_target) ||
970 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
971 have_incoming_change = 1;
973 if (!have_local_change && !have_incoming_change) {
974 if (ancestor_target) {
975 /* Both sides made the same change. */
976 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
977 path);
978 } else if (deriv_len == ondisk_len &&
979 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
980 /* Both sides added the same symlink. */
981 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
982 path);
983 } else {
984 /* Both sides added symlinks which don't match. */
985 err = install_symlink_conflict(deriv_target,
986 deriv_base_commit_id, ancestor_target,
987 label_orig, ondisk_target, ondisk_path);
988 if (err)
989 goto done;
990 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
991 path);
993 } else if (!have_local_change && have_incoming_change) {
994 /* Apply the incoming change. */
995 err = update_symlink(ondisk_path, deriv_target,
996 strlen(deriv_target));
997 if (err)
998 goto done;
999 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1000 } else if (have_local_change && have_incoming_change) {
1001 err = install_symlink_conflict(deriv_target,
1002 deriv_base_commit_id, ancestor_target, label_orig,
1003 ondisk_target, ondisk_path);
1004 if (err)
1005 goto done;
1006 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1007 path);
1010 done:
1011 free(ancestor_target);
1012 free(deriv_target);
1013 return err;
1017 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1018 * blob_deriv acts as the first derived version, and the file on disk
1019 * acts as the second derived version.
1021 static const struct got_error *
1022 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1023 struct got_blob_object *blob_orig, const char *ondisk_path,
1024 const char *path, uint16_t st_mode, const char *label_orig,
1025 struct got_blob_object *blob_deriv,
1026 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1027 got_worktree_checkout_cb progress_cb, void *progress_arg)
1029 const struct got_error *err = NULL;
1030 FILE *f_deriv = NULL;
1031 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1032 char *label_deriv = NULL, *parent;
1034 *local_changes_subsumed = 0;
1036 parent = dirname(ondisk_path);
1037 if (parent == NULL)
1038 return got_error_from_errno2("dirname", ondisk_path);
1040 free(base_path);
1041 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1042 err = got_error_from_errno("asprintf");
1043 base_path = NULL;
1044 goto done;
1047 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1048 if (err)
1049 goto done;
1050 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1051 blob_deriv);
1052 if (err)
1053 goto done;
1055 err = got_object_id_str(&id_str, deriv_base_commit_id);
1056 if (err)
1057 goto done;
1058 if (asprintf(&label_deriv, "%s: commit %s",
1059 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1060 err = got_error_from_errno("asprintf");
1061 goto done;
1064 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1065 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1066 label_deriv, repo, progress_cb, progress_arg);
1067 done:
1068 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1069 err = got_error_from_errno("fclose");
1070 free(base_path);
1071 if (blob_deriv_path) {
1072 unlink(blob_deriv_path);
1073 free(blob_deriv_path);
1075 free(id_str);
1076 free(label_deriv);
1077 return err;
1080 static const struct got_error *
1081 create_fileindex_entry(struct got_fileindex *fileindex,
1082 struct got_object_id *base_commit_id, const char *ondisk_path,
1083 const char *path, struct got_object_id *blob_id)
1085 const struct got_error *err = NULL;
1086 struct got_fileindex_entry *new_ie;
1088 err = got_fileindex_entry_alloc(&new_ie, path);
1089 if (err)
1090 return err;
1092 err = got_fileindex_entry_update(new_ie, ondisk_path,
1093 blob_id->sha1, base_commit_id->sha1, 1);
1094 if (err)
1095 goto done;
1097 err = got_fileindex_entry_add(fileindex, new_ie);
1098 done:
1099 if (err)
1100 got_fileindex_entry_free(new_ie);
1101 return err;
1104 static mode_t
1105 get_ondisk_perms(int executable, mode_t st_mode)
1107 mode_t xbits = S_IXUSR;
1109 if (executable) {
1110 /* Map read bits to execute bits. */
1111 if (st_mode & S_IRGRP)
1112 xbits |= S_IXGRP;
1113 if (st_mode & S_IROTH)
1114 xbits |= S_IXOTH;
1115 return st_mode | xbits;
1118 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1121 /* forward declaration */
1122 static const struct got_error *
1123 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1124 const char *path, mode_t te_mode, mode_t st_mode,
1125 struct got_blob_object *blob, int restoring_missing_file,
1126 int reverting_versioned_file, int installing_bad_symlink,
1127 struct got_repository *repo,
1128 got_worktree_checkout_cb progress_cb, void *progress_arg);
1131 * This function assumes that the provided symlink target points at a
1132 * safe location in the work tree!
1134 static const struct got_error *
1135 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1136 size_t target_len)
1138 const struct got_error *err = NULL;
1139 ssize_t elen;
1140 char etarget[PATH_MAX];
1141 int fd;
1144 * "Bad" symlinks (those pointing outside the work tree or into the
1145 * .got directory) are installed in the work tree as a regular file
1146 * which contains the bad symlink target path.
1147 * The new symlink target has already been checked for safety by our
1148 * caller. If we can successfully open a regular file then we simply
1149 * replace this file with a symlink below.
1151 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1152 if (fd == -1) {
1153 if (errno != ELOOP)
1154 return got_error_from_errno2("open", ondisk_path);
1156 /* We are updating an existing on-disk symlink. */
1157 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1158 if (elen == -1)
1159 return got_error_from_errno2("readlink", ondisk_path);
1161 if (elen == target_len &&
1162 memcmp(etarget, target_path, target_len) == 0)
1163 return NULL; /* nothing to do */
1166 err = update_symlink(ondisk_path, target_path, target_len);
1167 if (fd != -1 && close(fd) == -1 && err == NULL)
1168 err = got_error_from_errno2("close", ondisk_path);
1169 return err;
1172 static const struct got_error *
1173 install_symlink(struct got_worktree *worktree, const char *ondisk_path,
1174 const char *path, mode_t te_mode, mode_t st_mode,
1175 struct got_blob_object *blob, int restoring_missing_file,
1176 int reverting_versioned_file, struct got_repository *repo,
1177 got_worktree_checkout_cb progress_cb, void *progress_arg)
1179 const struct got_error *err = NULL;
1180 char target_path[PATH_MAX];
1181 size_t len, target_len = 0;
1182 char *resolved_path = NULL, *abspath = NULL;
1183 char *path_got = NULL;
1184 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1185 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1188 * Blob object content specifies the target path of the link.
1189 * If a symbolic link cannot be installed we instead create
1190 * a regular file which contains the link target path stored
1191 * in the blob object.
1193 do {
1194 err = got_object_blob_read_block(&len, blob);
1195 if (len + target_len >= sizeof(target_path)) {
1196 /* Path too long; install as a regular file. */
1197 got_object_blob_rewind(blob);
1198 return install_blob(worktree, ondisk_path, path,
1199 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1200 restoring_missing_file, reverting_versioned_file,
1201 1, repo, progress_cb, progress_arg);
1203 if (len > 0) {
1204 /* Skip blob object header first time around. */
1205 memcpy(target_path + target_len, buf + hdrlen,
1206 len - hdrlen);
1207 target_len += len - hdrlen;
1208 hdrlen = 0;
1210 } while (len != 0);
1211 target_path[target_len] = '\0';
1214 * Relative symlink target lookup should begin at the directory
1215 * in which the blob object is being installed.
1217 if (!got_path_is_absolute(target_path)) {
1218 char *parent = dirname(ondisk_path);
1219 if (parent == NULL) {
1220 err = got_error_from_errno2("dirname", ondisk_path);
1221 goto done;
1223 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1224 err = got_error_from_errno("asprintf");
1225 goto done;
1230 * unveil(2) restricts our view of paths in the filesystem.
1231 * ENOENT will occur if a link target path does not exist or
1232 * if it points outside our unveiled path space.
1234 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1235 if (resolved_path == NULL) {
1236 if (errno != ENOENT) {
1237 err = got_error_from_errno2("realpath", target_path);
1238 goto done;
1242 /* Only allow symlinks pointing at paths within the work tree. */
1243 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1244 abspath : target_path), worktree->root_path,
1245 strlen(worktree->root_path))) {
1246 /* install as a regular file */
1247 got_object_blob_rewind(blob);
1248 err = install_blob(worktree, ondisk_path, path,
1249 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1250 restoring_missing_file, reverting_versioned_file, 1,
1251 repo, progress_cb, progress_arg);
1252 goto done;
1255 /* Do not allow symlinks pointing into the .got directory. */
1256 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1257 GOT_WORKTREE_GOT_DIR) == -1) {
1258 err = got_error_from_errno("asprintf");
1259 goto done;
1261 if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1262 abspath : target_path), path_got, strlen(path_got))) {
1263 /* install as a regular file */
1264 got_object_blob_rewind(blob);
1265 err = install_blob(worktree, ondisk_path, path,
1266 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1267 restoring_missing_file, reverting_versioned_file, 1,
1268 repo, progress_cb, progress_arg);
1269 goto done;
1272 if (symlink(target_path, ondisk_path) == -1) {
1273 if (errno == EEXIST) {
1274 err = replace_existing_symlink(ondisk_path,
1275 target_path, target_len);
1276 if (err)
1277 goto done;
1278 if (progress_cb) {
1279 err = (*progress_cb)(progress_arg,
1280 GOT_STATUS_UPDATE, path);
1282 goto done; /* Nothing else to do. */
1285 if (errno == ENOENT) {
1286 char *parent = dirname(ondisk_path);
1287 if (parent == NULL) {
1288 err = got_error_from_errno2("dirname",
1289 ondisk_path);
1290 goto done;
1292 err = add_dir_on_disk(worktree, parent);
1293 if (err)
1294 goto done;
1296 * Retry, and fall through to error handling
1297 * below if this second attempt fails.
1299 if (symlink(target_path, ondisk_path) != -1) {
1300 err = NULL; /* success */
1301 goto done;
1305 /* Handle errors from first or second creation attempt. */
1306 if (errno == ENAMETOOLONG) {
1307 /* bad target path; install as a regular file */
1308 got_object_blob_rewind(blob);
1309 err = install_blob(worktree, ondisk_path, path,
1310 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1311 restoring_missing_file, reverting_versioned_file, 1,
1312 repo, progress_cb, progress_arg);
1313 } else if (errno == ENOTDIR) {
1314 err = got_error_path(ondisk_path,
1315 GOT_ERR_FILE_OBSTRUCTED);
1316 } else {
1317 err = got_error_from_errno3("symlink",
1318 target_path, ondisk_path);
1320 } else if (progress_cb)
1321 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1322 done:
1323 free(resolved_path);
1324 free(abspath);
1325 free(path_got);
1326 return err;
1329 static const struct got_error *
1330 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1331 const char *path, mode_t te_mode, mode_t st_mode,
1332 struct got_blob_object *blob, int restoring_missing_file,
1333 int reverting_versioned_file, int installing_bad_symlink,
1334 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1335 void *progress_arg)
1337 const struct got_error *err = NULL;
1338 int fd = -1;
1339 size_t len, hdrlen;
1340 int update = 0;
1341 char *tmppath = NULL;
1343 if (S_ISLNK(te_mode))
1344 return install_symlink(worktree, ondisk_path, path, te_mode,
1345 st_mode, blob, restoring_missing_file,
1346 reverting_versioned_file, repo, progress_cb, progress_arg);
1348 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1349 GOT_DEFAULT_FILE_MODE);
1350 if (fd == -1) {
1351 if (errno == ENOENT) {
1352 char *parent = dirname(path);
1353 if (parent == NULL)
1354 return got_error_from_errno2("dirname", path);
1355 err = add_dir_on_disk(worktree, parent);
1356 if (err)
1357 return err;
1358 fd = open(ondisk_path,
1359 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1360 GOT_DEFAULT_FILE_MODE);
1361 if (fd == -1)
1362 return got_error_from_errno2("open",
1363 ondisk_path);
1364 } else if (errno == EEXIST) {
1365 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1366 /* TODO file is obstructed; do something */
1367 err = got_error_path(ondisk_path,
1368 GOT_ERR_FILE_OBSTRUCTED);
1369 goto done;
1370 } else {
1371 err = got_opentemp_named_fd(&tmppath, &fd,
1372 ondisk_path);
1373 if (err)
1374 goto done;
1375 update = 1;
1377 } else
1378 return got_error_from_errno2("open", ondisk_path);
1381 if (progress_cb) {
1382 if (restoring_missing_file)
1383 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1384 path);
1385 else if (reverting_versioned_file)
1386 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1387 path);
1388 else
1389 err = (*progress_cb)(progress_arg,
1390 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1391 if (err)
1392 goto done;
1395 hdrlen = got_object_blob_get_hdrlen(blob);
1396 do {
1397 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1398 err = got_object_blob_read_block(&len, blob);
1399 if (err)
1400 break;
1401 if (len > 0) {
1402 /* Skip blob object header first time around. */
1403 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1404 if (outlen == -1) {
1405 err = got_error_from_errno("write");
1406 goto done;
1407 } else if (outlen != len - hdrlen) {
1408 err = got_error(GOT_ERR_IO);
1409 goto done;
1411 hdrlen = 0;
1413 } while (len != 0);
1415 if (fsync(fd) != 0) {
1416 err = got_error_from_errno("fsync");
1417 goto done;
1420 if (update) {
1421 if (rename(tmppath, ondisk_path) != 0) {
1422 err = got_error_from_errno3("rename", tmppath,
1423 ondisk_path);
1424 unlink(tmppath);
1425 goto done;
1429 if (chmod(ondisk_path,
1430 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1431 err = got_error_from_errno2("chmod", ondisk_path);
1432 goto done;
1435 done:
1436 if (fd != -1 && close(fd) != 0 && err == NULL)
1437 err = got_error_from_errno("close");
1438 free(tmppath);
1439 return err;
1442 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1443 static const struct got_error *
1444 get_modified_file_content_status(unsigned char *status, FILE *f)
1446 const struct got_error *err = NULL;
1447 const char *markers[3] = {
1448 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1449 GOT_DIFF_CONFLICT_MARKER_SEP,
1450 GOT_DIFF_CONFLICT_MARKER_END
1452 int i = 0;
1453 char *line;
1454 size_t len;
1455 const char delim[3] = {'\0', '\0', '\0'};
1457 while (*status == GOT_STATUS_MODIFY) {
1458 line = fparseln(f, &len, NULL, delim, 0);
1459 if (line == NULL) {
1460 if (feof(f))
1461 break;
1462 err = got_ferror(f, GOT_ERR_IO);
1463 break;
1466 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1467 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1468 == 0)
1469 *status = GOT_STATUS_CONFLICT;
1470 else
1471 i++;
1475 return err;
1478 static int
1479 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1481 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1482 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1485 static int
1486 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1488 return !(ie->ctime_sec == sb->st_ctime &&
1489 ie->ctime_nsec == sb->st_ctimensec &&
1490 ie->mtime_sec == sb->st_mtime &&
1491 ie->mtime_nsec == sb->st_mtimensec &&
1492 ie->size == (sb->st_size & 0xffffffff) &&
1493 !xbit_differs(ie, sb->st_mode));
1496 static unsigned char
1497 get_staged_status(struct got_fileindex_entry *ie)
1499 switch (got_fileindex_entry_stage_get(ie)) {
1500 case GOT_FILEIDX_STAGE_ADD:
1501 return GOT_STATUS_ADD;
1502 case GOT_FILEIDX_STAGE_DELETE:
1503 return GOT_STATUS_DELETE;
1504 case GOT_FILEIDX_STAGE_MODIFY:
1505 return GOT_STATUS_MODIFY;
1506 default:
1507 return GOT_STATUS_NO_CHANGE;
1511 static const struct got_error *
1512 get_symlink_status(unsigned char *status, struct stat *sb,
1513 struct got_fileindex_entry *ie, const char *abspath,
1514 int dirfd, const char *de_name, struct got_blob_object *blob)
1516 const struct got_error *err = NULL;
1517 char target_path[PATH_MAX];
1518 char etarget[PATH_MAX];
1519 ssize_t elen;
1520 size_t len, target_len = 0;
1521 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1522 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1524 *status = GOT_STATUS_NO_CHANGE;
1526 /* Blob object content specifies the target path of the link. */
1527 do {
1528 err = got_object_blob_read_block(&len, blob);
1529 if (err)
1530 return err;
1531 if (len + target_len >= sizeof(target_path)) {
1533 * Should not happen. The blob contents were OK
1534 * when this symlink was installed.
1536 return got_error(GOT_ERR_NO_SPACE);
1538 if (len > 0) {
1539 /* Skip blob object header first time around. */
1540 memcpy(target_path + target_len, buf + hdrlen,
1541 len - hdrlen);
1542 target_len += len - hdrlen;
1543 hdrlen = 0;
1545 } while (len != 0);
1546 target_path[target_len] = '\0';
1548 if (dirfd != -1) {
1549 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1550 if (elen == -1)
1551 return got_error_from_errno2("readlinkat", abspath);
1552 } else {
1553 elen = readlink(abspath, etarget, sizeof(etarget));
1554 if (elen == -1)
1555 return got_error_from_errno2("readlinkat", abspath);
1558 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1559 *status = GOT_STATUS_MODIFY;
1561 return NULL;
1564 static const struct got_error *
1565 get_file_status(unsigned char *status, struct stat *sb,
1566 struct got_fileindex_entry *ie, const char *abspath,
1567 int dirfd, const char *de_name, struct got_repository *repo)
1569 const struct got_error *err = NULL;
1570 struct got_object_id id;
1571 size_t hdrlen;
1572 int fd = -1;
1573 FILE *f = NULL;
1574 uint8_t fbuf[8192];
1575 struct got_blob_object *blob = NULL;
1576 size_t flen, blen;
1577 unsigned char staged_status = get_staged_status(ie);
1579 *status = GOT_STATUS_NO_CHANGE;
1582 * Whenever the caller provides a directory descriptor and a
1583 * directory entry name for the file, use them! This prevents
1584 * race conditions if filesystem paths change beneath our feet.
1586 if (dirfd != -1) {
1587 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1588 if (errno == ENOENT) {
1589 if (got_fileindex_entry_has_file_on_disk(ie))
1590 *status = GOT_STATUS_MISSING;
1591 else
1592 *status = GOT_STATUS_DELETE;
1593 goto done;
1595 err = got_error_from_errno2("fstatat", abspath);
1596 goto done;
1598 } else {
1599 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1600 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1601 return got_error_from_errno2("open", abspath);
1602 else if (fd == -1 && errno == ELOOP) {
1603 if (lstat(abspath, sb) == -1)
1604 return got_error_from_errno2("lstat", abspath);
1605 } else if (fd == -1 || fstat(fd, sb) == -1) {
1606 if (errno == ENOENT) {
1607 if (got_fileindex_entry_has_file_on_disk(ie))
1608 *status = GOT_STATUS_MISSING;
1609 else
1610 *status = GOT_STATUS_DELETE;
1611 goto done;
1613 err = got_error_from_errno2("fstat", abspath);
1614 goto done;
1618 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1619 *status = GOT_STATUS_OBSTRUCTED;
1620 goto done;
1623 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1624 *status = GOT_STATUS_DELETE;
1625 goto done;
1626 } else if (!got_fileindex_entry_has_blob(ie) &&
1627 staged_status != GOT_STATUS_ADD) {
1628 *status = GOT_STATUS_ADD;
1629 goto done;
1632 if (!stat_info_differs(ie, sb))
1633 goto done;
1635 if (staged_status == GOT_STATUS_MODIFY ||
1636 staged_status == GOT_STATUS_ADD)
1637 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1638 else
1639 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1641 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1642 if (err)
1643 goto done;
1645 if (S_ISLNK(sb->st_mode)) {
1646 /* Staging changes to symlinks is not yet(?) supported. */
1647 if (staged_status != GOT_STATUS_NO_CHANGE) {
1648 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1649 goto done;
1651 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1652 de_name, blob);
1653 goto done;
1657 if (dirfd != -1) {
1658 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1659 if (fd == -1) {
1660 err = got_error_from_errno2("openat", abspath);
1661 goto done;
1665 f = fdopen(fd, "r");
1666 if (f == NULL) {
1667 err = got_error_from_errno2("fdopen", abspath);
1668 goto done;
1670 fd = -1;
1671 hdrlen = got_object_blob_get_hdrlen(blob);
1672 for (;;) {
1673 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1674 err = got_object_blob_read_block(&blen, blob);
1675 if (err)
1676 goto done;
1677 /* Skip length of blob object header first time around. */
1678 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1679 if (flen == 0 && ferror(f)) {
1680 err = got_error_from_errno("fread");
1681 goto done;
1683 if (blen == 0) {
1684 if (flen != 0)
1685 *status = GOT_STATUS_MODIFY;
1686 break;
1687 } else if (flen == 0) {
1688 if (blen != 0)
1689 *status = GOT_STATUS_MODIFY;
1690 break;
1691 } else if (blen - hdrlen == flen) {
1692 /* Skip blob object header first time around. */
1693 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1694 *status = GOT_STATUS_MODIFY;
1695 break;
1697 } else {
1698 *status = GOT_STATUS_MODIFY;
1699 break;
1701 hdrlen = 0;
1704 if (*status == GOT_STATUS_MODIFY) {
1705 rewind(f);
1706 err = get_modified_file_content_status(status, f);
1707 } else if (xbit_differs(ie, sb->st_mode))
1708 *status = GOT_STATUS_MODE_CHANGE;
1709 done:
1710 if (blob)
1711 got_object_blob_close(blob);
1712 if (f != NULL && fclose(f) == EOF && err == NULL)
1713 err = got_error_from_errno2("fclose", abspath);
1714 if (fd != -1 && close(fd) == -1 && err == NULL)
1715 err = got_error_from_errno2("close", abspath);
1716 return err;
1720 * Update timestamps in the file index if a file is unmodified and
1721 * we had to run a full content comparison to find out.
1723 static const struct got_error *
1724 sync_timestamps(char *ondisk_path, unsigned char status,
1725 struct got_fileindex_entry *ie, struct stat *sb)
1727 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1728 return got_fileindex_entry_update(ie, ondisk_path,
1729 ie->blob_sha1, ie->commit_sha1, 1);
1731 return NULL;
1734 static const struct got_error *
1735 update_blob(struct got_worktree *worktree,
1736 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1737 struct got_tree_entry *te, const char *path,
1738 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1739 void *progress_arg)
1741 const struct got_error *err = NULL;
1742 struct got_blob_object *blob = NULL;
1743 char *ondisk_path;
1744 unsigned char status = GOT_STATUS_NO_CHANGE;
1745 struct stat sb;
1747 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1748 return got_error_from_errno("asprintf");
1750 if (ie) {
1751 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1752 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1753 goto done;
1755 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1756 repo);
1757 if (err)
1758 goto done;
1759 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1760 sb.st_mode = got_fileindex_perms_to_st(ie);
1761 } else
1762 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1764 if (status == GOT_STATUS_OBSTRUCTED) {
1765 err = (*progress_cb)(progress_arg, status, path);
1766 goto done;
1768 if (status == GOT_STATUS_CONFLICT) {
1769 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1770 path);
1771 goto done;
1774 if (ie && status != GOT_STATUS_MISSING &&
1775 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1776 if (got_fileindex_entry_has_commit(ie) &&
1777 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1778 SHA1_DIGEST_LENGTH) == 0) {
1779 err = sync_timestamps(ondisk_path, status, ie, &sb);
1780 if (err)
1781 goto done;
1782 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1783 path);
1784 goto done;
1786 if (got_fileindex_entry_has_blob(ie) &&
1787 memcmp(ie->blob_sha1, te->id.sha1,
1788 SHA1_DIGEST_LENGTH) == 0) {
1789 err = sync_timestamps(ondisk_path, status, ie, &sb);
1790 goto done;
1794 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1795 if (err)
1796 goto done;
1798 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1799 int update_timestamps;
1800 struct got_blob_object *blob2 = NULL;
1801 char *label_orig = NULL;
1802 if (got_fileindex_entry_has_blob(ie)) {
1803 struct got_object_id id2;
1804 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1805 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1806 if (err)
1807 goto done;
1809 if (got_fileindex_entry_has_commit(ie)) {
1810 char id_str[SHA1_DIGEST_STRING_LENGTH];
1811 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1812 sizeof(id_str)) == NULL) {
1813 err = got_error_path(id_str,
1814 GOT_ERR_BAD_OBJ_ID_STR);
1815 goto done;
1817 if (asprintf(&label_orig, "%s: commit %s",
1818 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1819 err = got_error_from_errno("asprintf");
1820 goto done;
1823 err = merge_blob(&update_timestamps, worktree, blob2,
1824 ondisk_path, path, sb.st_mode, label_orig, blob,
1825 worktree->base_commit_id, repo,
1826 progress_cb, progress_arg);
1827 free(label_orig);
1828 if (blob2)
1829 got_object_blob_close(blob2);
1830 if (err)
1831 goto done;
1833 * Do not update timestamps of files with local changes.
1834 * Otherwise, a future status walk would treat them as
1835 * unmodified files again.
1837 err = got_fileindex_entry_update(ie, ondisk_path,
1838 blob->id.sha1, worktree->base_commit_id->sha1,
1839 update_timestamps);
1840 } else if (status == GOT_STATUS_MODE_CHANGE) {
1841 err = got_fileindex_entry_update(ie, ondisk_path,
1842 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1843 } else if (status == GOT_STATUS_DELETE) {
1844 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1845 if (err)
1846 goto done;
1847 err = got_fileindex_entry_update(ie, ondisk_path,
1848 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1849 if (err)
1850 goto done;
1851 } else {
1852 err = install_blob(worktree, ondisk_path, path, te->mode,
1853 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0, 0,
1854 repo, progress_cb, progress_arg);
1855 if (err)
1856 goto done;
1857 if (ie) {
1858 err = got_fileindex_entry_update(ie, ondisk_path,
1859 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1860 } else {
1861 err = create_fileindex_entry(fileindex,
1862 worktree->base_commit_id, ondisk_path, path,
1863 &blob->id);
1865 if (err)
1866 goto done;
1868 got_object_blob_close(blob);
1869 done:
1870 free(ondisk_path);
1871 return err;
1874 static const struct got_error *
1875 remove_ondisk_file(const char *root_path, const char *path)
1877 const struct got_error *err = NULL;
1878 char *ondisk_path = NULL;
1880 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1881 return got_error_from_errno("asprintf");
1883 if (unlink(ondisk_path) == -1) {
1884 if (errno != ENOENT)
1885 err = got_error_from_errno2("unlink", ondisk_path);
1886 } else {
1887 char *parent = dirname(ondisk_path);
1888 while (parent && strcmp(parent, root_path) != 0) {
1889 if (rmdir(parent) == -1) {
1890 if (errno != ENOTEMPTY)
1891 err = got_error_from_errno2("rmdir",
1892 parent);
1893 break;
1895 parent = dirname(parent);
1898 free(ondisk_path);
1899 return err;
1902 static const struct got_error *
1903 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1904 struct got_fileindex_entry *ie, struct got_repository *repo,
1905 got_worktree_checkout_cb progress_cb, void *progress_arg)
1907 const struct got_error *err = NULL;
1908 unsigned char status;
1909 struct stat sb;
1910 char *ondisk_path;
1912 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1913 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1915 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1916 == -1)
1917 return got_error_from_errno("asprintf");
1919 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1920 if (err)
1921 goto done;
1923 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1924 status == GOT_STATUS_ADD) {
1925 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1926 if (err)
1927 goto done;
1929 * Preserve the working file and change the deleted blob's
1930 * entry into a schedule-add entry.
1932 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1933 0);
1934 } else {
1935 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1936 if (err)
1937 goto done;
1938 if (status == GOT_STATUS_NO_CHANGE) {
1939 err = remove_ondisk_file(worktree->root_path, ie->path);
1940 if (err)
1941 goto done;
1943 got_fileindex_entry_remove(fileindex, ie);
1945 done:
1946 free(ondisk_path);
1947 return err;
1950 struct diff_cb_arg {
1951 struct got_fileindex *fileindex;
1952 struct got_worktree *worktree;
1953 struct got_repository *repo;
1954 got_worktree_checkout_cb progress_cb;
1955 void *progress_arg;
1956 got_cancel_cb cancel_cb;
1957 void *cancel_arg;
1960 static const struct got_error *
1961 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1962 struct got_tree_entry *te, const char *parent_path)
1964 struct diff_cb_arg *a = arg;
1966 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1967 return got_error(GOT_ERR_CANCELLED);
1969 return update_blob(a->worktree, a->fileindex, ie, te,
1970 ie->path, a->repo, a->progress_cb, a->progress_arg);
1973 static const struct got_error *
1974 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1976 struct diff_cb_arg *a = arg;
1978 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1979 return got_error(GOT_ERR_CANCELLED);
1981 return delete_blob(a->worktree, a->fileindex, ie,
1982 a->repo, a->progress_cb, a->progress_arg);
1985 static const struct got_error *
1986 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1988 struct diff_cb_arg *a = arg;
1989 const struct got_error *err;
1990 char *path;
1992 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1993 return got_error(GOT_ERR_CANCELLED);
1995 if (got_object_tree_entry_is_submodule(te))
1996 return NULL;
1998 if (asprintf(&path, "%s%s%s", parent_path,
1999 parent_path[0] ? "/" : "", te->name)
2000 == -1)
2001 return got_error_from_errno("asprintf");
2003 if (S_ISDIR(te->mode))
2004 err = add_dir_on_disk(a->worktree, path);
2005 else
2006 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2007 a->repo, a->progress_cb, a->progress_arg);
2009 free(path);
2010 return err;
2013 static const struct got_error *
2014 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2016 const struct got_error *err = NULL;
2017 char *uuidstr = NULL;
2018 uint32_t uuid_status;
2020 *refname = NULL;
2022 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2023 if (uuid_status != uuid_s_ok)
2024 return got_error_uuid(uuid_status, "uuid_to_string");
2026 if (asprintf(refname, "%s-%s", prefix, uuidstr)
2027 == -1) {
2028 err = got_error_from_errno("asprintf");
2029 *refname = NULL;
2031 free(uuidstr);
2032 return err;
2035 const struct got_error *
2036 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2038 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2041 static const struct got_error *
2042 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2044 return get_ref_name(refname, worktree,
2045 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2048 static const struct got_error *
2049 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2051 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2054 static const struct got_error *
2055 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2057 return get_ref_name(refname, worktree,
2058 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2061 static const struct got_error *
2062 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2064 return get_ref_name(refname, worktree,
2065 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2068 static const struct got_error *
2069 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2071 return get_ref_name(refname, worktree,
2072 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2075 static const struct got_error *
2076 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2078 return get_ref_name(refname, worktree,
2079 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2082 static const struct got_error *
2083 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2085 return get_ref_name(refname, worktree,
2086 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2089 static const struct got_error *
2090 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2092 return get_ref_name(refname, worktree,
2093 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2096 const struct got_error *
2097 got_worktree_get_histedit_script_path(char **path,
2098 struct got_worktree *worktree)
2100 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2101 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2102 *path = NULL;
2103 return got_error_from_errno("asprintf");
2105 return NULL;
2109 * Prevent Git's garbage collector from deleting our base commit by
2110 * setting a reference to our base commit's ID.
2112 static const struct got_error *
2113 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2115 const struct got_error *err = NULL;
2116 struct got_reference *ref = NULL;
2117 char *refname;
2119 err = got_worktree_get_base_ref_name(&refname, worktree);
2120 if (err)
2121 return err;
2123 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2124 if (err)
2125 goto done;
2127 err = got_ref_write(ref, repo);
2128 done:
2129 free(refname);
2130 if (ref)
2131 got_ref_close(ref);
2132 return err;
2135 static const struct got_error *
2136 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2138 const struct got_error *err = NULL;
2140 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2141 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2142 err = got_error_from_errno("asprintf");
2143 *fileindex_path = NULL;
2145 return err;
2149 static const struct got_error *
2150 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2151 struct got_worktree *worktree)
2153 const struct got_error *err = NULL;
2154 FILE *index = NULL;
2156 *fileindex_path = NULL;
2157 *fileindex = got_fileindex_alloc();
2158 if (*fileindex == NULL)
2159 return got_error_from_errno("got_fileindex_alloc");
2161 err = get_fileindex_path(fileindex_path, worktree);
2162 if (err)
2163 goto done;
2165 index = fopen(*fileindex_path, "rb");
2166 if (index == NULL) {
2167 if (errno != ENOENT)
2168 err = got_error_from_errno2("fopen", *fileindex_path);
2169 } else {
2170 err = got_fileindex_read(*fileindex, index);
2171 if (fclose(index) != 0 && err == NULL)
2172 err = got_error_from_errno("fclose");
2174 done:
2175 if (err) {
2176 free(*fileindex_path);
2177 *fileindex_path = NULL;
2178 got_fileindex_free(*fileindex);
2179 *fileindex = NULL;
2181 return err;
2184 struct bump_base_commit_id_arg {
2185 struct got_object_id *base_commit_id;
2186 const char *path;
2187 size_t path_len;
2188 const char *entry_name;
2189 got_worktree_checkout_cb progress_cb;
2190 void *progress_arg;
2193 /* Bump base commit ID of all files within an updated part of the work tree. */
2194 static const struct got_error *
2195 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2197 const struct got_error *err;
2198 struct bump_base_commit_id_arg *a = arg;
2200 if (a->entry_name) {
2201 if (strcmp(ie->path, a->path) != 0)
2202 return NULL;
2203 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2204 return NULL;
2206 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2207 SHA1_DIGEST_LENGTH) == 0)
2208 return NULL;
2210 if (a->progress_cb) {
2211 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2212 ie->path);
2213 if (err)
2214 return err;
2216 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2217 return NULL;
2220 static const struct got_error *
2221 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2223 const struct got_error *err = NULL;
2224 char *new_fileindex_path = NULL;
2225 FILE *new_index = NULL;
2226 struct timespec timeout;
2228 err = got_opentemp_named(&new_fileindex_path, &new_index,
2229 fileindex_path);
2230 if (err)
2231 goto done;
2233 err = got_fileindex_write(fileindex, new_index);
2234 if (err)
2235 goto done;
2237 if (rename(new_fileindex_path, fileindex_path) != 0) {
2238 err = got_error_from_errno3("rename", new_fileindex_path,
2239 fileindex_path);
2240 unlink(new_fileindex_path);
2244 * Sleep for a short amount of time to ensure that files modified after
2245 * this program exits have a different time stamp from the one which
2246 * was recorded in the file index.
2248 timeout.tv_sec = 0;
2249 timeout.tv_nsec = 1;
2250 nanosleep(&timeout, NULL);
2251 done:
2252 if (new_index)
2253 fclose(new_index);
2254 free(new_fileindex_path);
2255 return err;
2258 static const struct got_error *
2259 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2260 struct got_object_id **tree_id, const char *wt_relpath,
2261 struct got_worktree *worktree, struct got_repository *repo)
2263 const struct got_error *err = NULL;
2264 struct got_object_id *id = NULL;
2265 char *in_repo_path = NULL;
2266 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2268 *entry_type = GOT_OBJ_TYPE_ANY;
2269 *tree_relpath = NULL;
2270 *tree_id = NULL;
2272 if (wt_relpath[0] == '\0') {
2273 /* Check out all files within the work tree. */
2274 *entry_type = GOT_OBJ_TYPE_TREE;
2275 *tree_relpath = strdup("");
2276 if (*tree_relpath == NULL) {
2277 err = got_error_from_errno("strdup");
2278 goto done;
2280 err = got_object_id_by_path(tree_id, repo,
2281 worktree->base_commit_id, worktree->path_prefix);
2282 if (err)
2283 goto done;
2284 return NULL;
2287 /* Check out a subset of files in the work tree. */
2289 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2290 is_root_wt ? "" : "/", wt_relpath) == -1) {
2291 err = got_error_from_errno("asprintf");
2292 goto done;
2295 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2296 in_repo_path);
2297 if (err)
2298 goto done;
2300 free(in_repo_path);
2301 in_repo_path = NULL;
2303 err = got_object_get_type(entry_type, repo, id);
2304 if (err)
2305 goto done;
2307 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2308 /* Check out a single file. */
2309 if (strchr(wt_relpath, '/') == NULL) {
2310 /* Check out a single file in work tree's root dir. */
2311 in_repo_path = strdup(worktree->path_prefix);
2312 if (in_repo_path == NULL) {
2313 err = got_error_from_errno("strdup");
2314 goto done;
2316 *tree_relpath = strdup("");
2317 if (*tree_relpath == NULL) {
2318 err = got_error_from_errno("strdup");
2319 goto done;
2321 } else {
2322 /* Check out a single file in a subdirectory. */
2323 err = got_path_dirname(tree_relpath, wt_relpath);
2324 if (err)
2325 return err;
2326 if (asprintf(&in_repo_path, "%s%s%s",
2327 worktree->path_prefix, is_root_wt ? "" : "/",
2328 *tree_relpath) == -1) {
2329 err = got_error_from_errno("asprintf");
2330 goto done;
2333 err = got_object_id_by_path(tree_id, repo,
2334 worktree->base_commit_id, in_repo_path);
2335 } else {
2336 /* Check out all files within a subdirectory. */
2337 *tree_id = got_object_id_dup(id);
2338 if (*tree_id == NULL) {
2339 err = got_error_from_errno("got_object_id_dup");
2340 goto done;
2342 *tree_relpath = strdup(wt_relpath);
2343 if (*tree_relpath == NULL) {
2344 err = got_error_from_errno("strdup");
2345 goto done;
2348 done:
2349 free(id);
2350 free(in_repo_path);
2351 if (err) {
2352 *entry_type = GOT_OBJ_TYPE_ANY;
2353 free(*tree_relpath);
2354 *tree_relpath = NULL;
2355 free(*tree_id);
2356 *tree_id = NULL;
2358 return err;
2361 static const struct got_error *
2362 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2363 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2364 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2365 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2367 const struct got_error *err = NULL;
2368 struct got_commit_object *commit = NULL;
2369 struct got_tree_object *tree = NULL;
2370 struct got_fileindex_diff_tree_cb diff_cb;
2371 struct diff_cb_arg arg;
2373 err = ref_base_commit(worktree, repo);
2374 if (err) {
2375 if (!(err->code == GOT_ERR_ERRNO &&
2376 (errno == EACCES || errno == EROFS)))
2377 goto done;
2378 err = (*progress_cb)(progress_arg,
2379 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2380 if (err)
2381 return err;
2384 err = got_object_open_as_commit(&commit, repo,
2385 worktree->base_commit_id);
2386 if (err)
2387 goto done;
2389 err = got_object_open_as_tree(&tree, repo, tree_id);
2390 if (err)
2391 goto done;
2393 if (entry_name &&
2394 got_object_tree_find_entry(tree, entry_name) == NULL) {
2395 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2396 goto done;
2399 diff_cb.diff_old_new = diff_old_new;
2400 diff_cb.diff_old = diff_old;
2401 diff_cb.diff_new = diff_new;
2402 arg.fileindex = fileindex;
2403 arg.worktree = worktree;
2404 arg.repo = repo;
2405 arg.progress_cb = progress_cb;
2406 arg.progress_arg = progress_arg;
2407 arg.cancel_cb = cancel_cb;
2408 arg.cancel_arg = cancel_arg;
2409 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2410 entry_name, repo, &diff_cb, &arg);
2411 done:
2412 if (tree)
2413 got_object_tree_close(tree);
2414 if (commit)
2415 got_object_commit_close(commit);
2416 return err;
2419 const struct got_error *
2420 got_worktree_checkout_files(struct got_worktree *worktree,
2421 struct got_pathlist_head *paths, struct got_repository *repo,
2422 got_worktree_checkout_cb progress_cb, void *progress_arg,
2423 got_cancel_cb cancel_cb, void *cancel_arg)
2425 const struct got_error *err = NULL, *sync_err, *unlockerr;
2426 struct got_commit_object *commit = NULL;
2427 struct got_tree_object *tree = NULL;
2428 struct got_fileindex *fileindex = NULL;
2429 char *fileindex_path = NULL;
2430 struct got_pathlist_entry *pe;
2431 struct tree_path_data {
2432 SIMPLEQ_ENTRY(tree_path_data) entry;
2433 struct got_object_id *tree_id;
2434 int entry_type;
2435 char *relpath;
2436 char *entry_name;
2437 } *tpd = NULL;
2438 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2440 SIMPLEQ_INIT(&tree_paths);
2442 err = lock_worktree(worktree, LOCK_EX);
2443 if (err)
2444 return err;
2446 /* Map all specified paths to in-repository trees. */
2447 TAILQ_FOREACH(pe, paths, entry) {
2448 tpd = malloc(sizeof(*tpd));
2449 if (tpd == NULL) {
2450 err = got_error_from_errno("malloc");
2451 goto done;
2454 err = find_tree_entry_for_checkout(&tpd->entry_type,
2455 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2456 if (err) {
2457 free(tpd);
2458 goto done;
2461 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2462 err = got_path_basename(&tpd->entry_name, pe->path);
2463 if (err) {
2464 free(tpd->relpath);
2465 free(tpd->tree_id);
2466 free(tpd);
2467 goto done;
2469 } else
2470 tpd->entry_name = NULL;
2472 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2476 * Read the file index.
2477 * Checking out files is supposed to be an idempotent operation.
2478 * If the on-disk file index is incomplete we will try to complete it.
2480 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2481 if (err)
2482 goto done;
2484 tpd = SIMPLEQ_FIRST(&tree_paths);
2485 TAILQ_FOREACH(pe, paths, entry) {
2486 struct bump_base_commit_id_arg bbc_arg;
2488 err = checkout_files(worktree, fileindex, tpd->relpath,
2489 tpd->tree_id, tpd->entry_name, repo,
2490 progress_cb, progress_arg, cancel_cb, cancel_arg);
2491 if (err)
2492 break;
2494 bbc_arg.base_commit_id = worktree->base_commit_id;
2495 bbc_arg.entry_name = tpd->entry_name;
2496 bbc_arg.path = pe->path;
2497 bbc_arg.path_len = pe->path_len;
2498 bbc_arg.progress_cb = progress_cb;
2499 bbc_arg.progress_arg = progress_arg;
2500 err = got_fileindex_for_each_entry_safe(fileindex,
2501 bump_base_commit_id, &bbc_arg);
2502 if (err)
2503 break;
2505 tpd = SIMPLEQ_NEXT(tpd, entry);
2507 sync_err = sync_fileindex(fileindex, fileindex_path);
2508 if (sync_err && err == NULL)
2509 err = sync_err;
2510 done:
2511 free(fileindex_path);
2512 if (tree)
2513 got_object_tree_close(tree);
2514 if (commit)
2515 got_object_commit_close(commit);
2516 if (fileindex)
2517 got_fileindex_free(fileindex);
2518 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2519 tpd = SIMPLEQ_FIRST(&tree_paths);
2520 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2521 free(tpd->relpath);
2522 free(tpd->tree_id);
2523 free(tpd);
2525 unlockerr = lock_worktree(worktree, LOCK_SH);
2526 if (unlockerr && err == NULL)
2527 err = unlockerr;
2528 return err;
2531 struct merge_file_cb_arg {
2532 struct got_worktree *worktree;
2533 struct got_fileindex *fileindex;
2534 got_worktree_checkout_cb progress_cb;
2535 void *progress_arg;
2536 got_cancel_cb cancel_cb;
2537 void *cancel_arg;
2538 const char *label_orig;
2539 struct got_object_id *commit_id2;
2542 static const struct got_error *
2543 merge_file_cb(void *arg, struct got_blob_object *blob1,
2544 struct got_blob_object *blob2, struct got_object_id *id1,
2545 struct got_object_id *id2, const char *path1, const char *path2,
2546 mode_t mode1, mode_t mode2, struct got_repository *repo)
2548 static const struct got_error *err = NULL;
2549 struct merge_file_cb_arg *a = arg;
2550 struct got_fileindex_entry *ie;
2551 char *ondisk_path = NULL;
2552 struct stat sb;
2553 unsigned char status;
2554 int local_changes_subsumed;
2556 if (blob1 && blob2) {
2557 ie = got_fileindex_entry_get(a->fileindex, path2,
2558 strlen(path2));
2559 if (ie == NULL)
2560 return (*a->progress_cb)(a->progress_arg,
2561 GOT_STATUS_MISSING, path2);
2563 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2564 path2) == -1)
2565 return got_error_from_errno("asprintf");
2567 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2568 repo);
2569 if (err)
2570 goto done;
2572 if (status == GOT_STATUS_DELETE) {
2573 err = (*a->progress_cb)(a->progress_arg,
2574 GOT_STATUS_MERGE, path2);
2575 goto done;
2577 if (status != GOT_STATUS_NO_CHANGE &&
2578 status != GOT_STATUS_MODIFY &&
2579 status != GOT_STATUS_CONFLICT &&
2580 status != GOT_STATUS_ADD) {
2581 err = (*a->progress_cb)(a->progress_arg, status, path2);
2582 goto done;
2585 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2586 err = merge_symlink(a->worktree, blob1,
2587 ondisk_path, path2, sb.st_mode, a->label_orig,
2588 blob2, a->commit_id2, repo, a->progress_cb,
2589 a->progress_arg);
2590 } else {
2591 err = merge_blob(&local_changes_subsumed, a->worktree,
2592 blob1, ondisk_path, path2, sb.st_mode,
2593 a->label_orig, blob2, a->commit_id2, repo,
2594 a->progress_cb, a->progress_arg);
2596 } else if (blob1) {
2597 ie = got_fileindex_entry_get(a->fileindex, path1,
2598 strlen(path1));
2599 if (ie == NULL)
2600 return (*a->progress_cb)(a->progress_arg,
2601 GOT_STATUS_MISSING, path1);
2603 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2604 path1) == -1)
2605 return got_error_from_errno("asprintf");
2607 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2608 repo);
2609 if (err)
2610 goto done;
2612 switch (status) {
2613 case GOT_STATUS_NO_CHANGE:
2614 err = (*a->progress_cb)(a->progress_arg,
2615 GOT_STATUS_DELETE, path1);
2616 if (err)
2617 goto done;
2618 err = remove_ondisk_file(a->worktree->root_path, path1);
2619 if (err)
2620 goto done;
2621 if (ie)
2622 got_fileindex_entry_mark_deleted_from_disk(ie);
2623 break;
2624 case GOT_STATUS_DELETE:
2625 case GOT_STATUS_MISSING:
2626 err = (*a->progress_cb)(a->progress_arg,
2627 GOT_STATUS_DELETE, path1);
2628 if (err)
2629 goto done;
2630 if (ie)
2631 got_fileindex_entry_mark_deleted_from_disk(ie);
2632 break;
2633 case GOT_STATUS_ADD:
2634 case GOT_STATUS_MODIFY:
2635 case GOT_STATUS_CONFLICT:
2636 err = (*a->progress_cb)(a->progress_arg,
2637 GOT_STATUS_CANNOT_DELETE, path1);
2638 if (err)
2639 goto done;
2640 break;
2641 case GOT_STATUS_OBSTRUCTED:
2642 err = (*a->progress_cb)(a->progress_arg, status, path1);
2643 if (err)
2644 goto done;
2645 break;
2646 default:
2647 break;
2649 } else if (blob2) {
2650 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2651 path2) == -1)
2652 return got_error_from_errno("asprintf");
2653 ie = got_fileindex_entry_get(a->fileindex, path2,
2654 strlen(path2));
2655 if (ie) {
2656 err = get_file_status(&status, &sb, ie, ondisk_path,
2657 -1, NULL, repo);
2658 if (err)
2659 goto done;
2660 if (status != GOT_STATUS_NO_CHANGE &&
2661 status != GOT_STATUS_MODIFY &&
2662 status != GOT_STATUS_CONFLICT &&
2663 status != GOT_STATUS_ADD) {
2664 err = (*a->progress_cb)(a->progress_arg,
2665 status, path2);
2666 goto done;
2668 if (S_ISLNK(mode2)) {
2669 err = merge_symlink(a->worktree, NULL,
2670 ondisk_path, path2, sb.st_mode,
2671 a->label_orig, blob2, a->commit_id2,
2672 repo, a->progress_cb, a->progress_arg);
2673 if (err)
2674 goto done;
2675 } else {
2676 err = merge_blob(&local_changes_subsumed,
2677 a->worktree, NULL, ondisk_path, path2,
2678 sb.st_mode, a->label_orig, blob2,
2679 a->commit_id2, repo, a->progress_cb,
2680 a->progress_arg);
2681 if (err)
2682 goto done;
2684 if (status == GOT_STATUS_DELETE) {
2685 err = got_fileindex_entry_update(ie,
2686 ondisk_path, blob2->id.sha1,
2687 a->worktree->base_commit_id->sha1, 0);
2688 if (err)
2689 goto done;
2691 } else {
2692 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2693 err = install_blob(a->worktree, ondisk_path, path2,
2694 /* XXX get this from parent tree! */
2695 GOT_DEFAULT_FILE_MODE,
2696 sb.st_mode, blob2, 0, 0, 0, repo,
2697 a->progress_cb, a->progress_arg);
2698 if (err)
2699 goto done;
2700 err = got_fileindex_entry_alloc(&ie, path2);
2701 if (err)
2702 goto done;
2703 err = got_fileindex_entry_update(ie, ondisk_path,
2704 NULL, NULL, 1);
2705 if (err) {
2706 got_fileindex_entry_free(ie);
2707 goto done;
2709 err = got_fileindex_entry_add(a->fileindex, ie);
2710 if (err) {
2711 got_fileindex_entry_free(ie);
2712 goto done;
2716 done:
2717 free(ondisk_path);
2718 return err;
2721 struct check_merge_ok_arg {
2722 struct got_worktree *worktree;
2723 struct got_repository *repo;
2726 static const struct got_error *
2727 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2729 const struct got_error *err = NULL;
2730 struct check_merge_ok_arg *a = arg;
2731 unsigned char status;
2732 struct stat sb;
2733 char *ondisk_path;
2735 /* Reject merges into a work tree with mixed base commits. */
2736 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2737 SHA1_DIGEST_LENGTH))
2738 return got_error(GOT_ERR_MIXED_COMMITS);
2740 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2741 == -1)
2742 return got_error_from_errno("asprintf");
2744 /* Reject merges into a work tree with conflicted files. */
2745 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2746 if (err)
2747 return err;
2748 if (status == GOT_STATUS_CONFLICT)
2749 return got_error(GOT_ERR_CONFLICTS);
2751 return NULL;
2754 static const struct got_error *
2755 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2756 const char *fileindex_path, struct got_object_id *commit_id1,
2757 struct got_object_id *commit_id2, struct got_repository *repo,
2758 got_worktree_checkout_cb progress_cb, void *progress_arg,
2759 got_cancel_cb cancel_cb, void *cancel_arg)
2761 const struct got_error *err = NULL, *sync_err;
2762 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2763 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2764 struct merge_file_cb_arg arg;
2765 char *label_orig = NULL;
2767 if (commit_id1) {
2768 char *id_str;
2770 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2771 worktree->path_prefix);
2772 if (err)
2773 goto done;
2775 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2776 if (err)
2777 goto done;
2779 err = got_object_id_str(&id_str, commit_id1);
2780 if (err)
2781 goto done;
2783 if (asprintf(&label_orig, "%s: commit %s",
2784 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2785 err = got_error_from_errno("asprintf");
2786 free(id_str);
2787 goto done;
2789 free(id_str);
2792 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2793 worktree->path_prefix);
2794 if (err)
2795 goto done;
2797 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2798 if (err)
2799 goto done;
2801 arg.worktree = worktree;
2802 arg.fileindex = fileindex;
2803 arg.progress_cb = progress_cb;
2804 arg.progress_arg = progress_arg;
2805 arg.cancel_cb = cancel_cb;
2806 arg.cancel_arg = cancel_arg;
2807 arg.label_orig = label_orig;
2808 arg.commit_id2 = commit_id2;
2809 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2810 sync_err = sync_fileindex(fileindex, fileindex_path);
2811 if (sync_err && err == NULL)
2812 err = sync_err;
2813 done:
2814 if (tree1)
2815 got_object_tree_close(tree1);
2816 if (tree2)
2817 got_object_tree_close(tree2);
2818 free(label_orig);
2819 return err;
2822 const struct got_error *
2823 got_worktree_merge_files(struct got_worktree *worktree,
2824 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2825 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2826 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2828 const struct got_error *err, *unlockerr;
2829 char *fileindex_path = NULL;
2830 struct got_fileindex *fileindex = NULL;
2831 struct check_merge_ok_arg mok_arg;
2833 err = lock_worktree(worktree, LOCK_EX);
2834 if (err)
2835 return err;
2837 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2838 if (err)
2839 goto done;
2841 mok_arg.worktree = worktree;
2842 mok_arg.repo = repo;
2843 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2844 &mok_arg);
2845 if (err)
2846 goto done;
2848 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2849 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2850 done:
2851 if (fileindex)
2852 got_fileindex_free(fileindex);
2853 free(fileindex_path);
2854 unlockerr = lock_worktree(worktree, LOCK_SH);
2855 if (unlockerr && err == NULL)
2856 err = unlockerr;
2857 return err;
2860 struct diff_dir_cb_arg {
2861 struct got_fileindex *fileindex;
2862 struct got_worktree *worktree;
2863 const char *status_path;
2864 size_t status_path_len;
2865 struct got_repository *repo;
2866 got_worktree_status_cb status_cb;
2867 void *status_arg;
2868 got_cancel_cb cancel_cb;
2869 void *cancel_arg;
2870 /* A pathlist containing per-directory pathlists of ignore patterns. */
2871 struct got_pathlist_head ignores;
2872 int report_unchanged;
2873 int no_ignores;
2876 static const struct got_error *
2877 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2878 int dirfd, const char *de_name,
2879 got_worktree_status_cb status_cb, void *status_arg,
2880 struct got_repository *repo, int report_unchanged)
2882 const struct got_error *err = NULL;
2883 unsigned char status = GOT_STATUS_NO_CHANGE;
2884 unsigned char staged_status = get_staged_status(ie);
2885 struct stat sb;
2886 struct got_object_id blob_id, commit_id, staged_blob_id;
2887 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2888 struct got_object_id *staged_blob_idp = NULL;
2890 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2891 if (err)
2892 return err;
2894 if (status == GOT_STATUS_NO_CHANGE &&
2895 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2896 return NULL;
2898 if (got_fileindex_entry_has_blob(ie)) {
2899 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2900 blob_idp = &blob_id;
2902 if (got_fileindex_entry_has_commit(ie)) {
2903 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2904 commit_idp = &commit_id;
2906 if (staged_status == GOT_STATUS_ADD ||
2907 staged_status == GOT_STATUS_MODIFY) {
2908 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2909 SHA1_DIGEST_LENGTH);
2910 staged_blob_idp = &staged_blob_id;
2913 return (*status_cb)(status_arg, status, staged_status,
2914 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2917 static const struct got_error *
2918 status_old_new(void *arg, struct got_fileindex_entry *ie,
2919 struct dirent *de, const char *parent_path, int dirfd)
2921 const struct got_error *err = NULL;
2922 struct diff_dir_cb_arg *a = arg;
2923 char *abspath;
2925 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2926 return got_error(GOT_ERR_CANCELLED);
2928 if (got_path_cmp(parent_path, a->status_path,
2929 strlen(parent_path), a->status_path_len) != 0 &&
2930 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2931 return NULL;
2933 if (parent_path[0]) {
2934 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2935 parent_path, de->d_name) == -1)
2936 return got_error_from_errno("asprintf");
2937 } else {
2938 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2939 de->d_name) == -1)
2940 return got_error_from_errno("asprintf");
2943 err = report_file_status(ie, abspath, dirfd, de->d_name,
2944 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2945 free(abspath);
2946 return err;
2949 static const struct got_error *
2950 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2952 struct diff_dir_cb_arg *a = arg;
2953 struct got_object_id blob_id, commit_id;
2954 unsigned char status;
2956 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2957 return got_error(GOT_ERR_CANCELLED);
2959 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2960 return NULL;
2962 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2963 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2964 if (got_fileindex_entry_has_file_on_disk(ie))
2965 status = GOT_STATUS_MISSING;
2966 else
2967 status = GOT_STATUS_DELETE;
2968 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2969 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2972 void
2973 free_ignorelist(struct got_pathlist_head *ignorelist)
2975 struct got_pathlist_entry *pe;
2977 TAILQ_FOREACH(pe, ignorelist, entry)
2978 free((char *)pe->path);
2979 got_pathlist_free(ignorelist);
2982 void
2983 free_ignores(struct got_pathlist_head *ignores)
2985 struct got_pathlist_entry *pe;
2987 TAILQ_FOREACH(pe, ignores, entry) {
2988 struct got_pathlist_head *ignorelist = pe->data;
2989 free_ignorelist(ignorelist);
2990 free((char *)pe->path);
2992 got_pathlist_free(ignores);
2995 static const struct got_error *
2996 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2998 const struct got_error *err = NULL;
2999 struct got_pathlist_entry *pe = NULL;
3000 struct got_pathlist_head *ignorelist;
3001 char *line = NULL, *pattern, *dirpath = NULL;
3002 size_t linesize = 0;
3003 ssize_t linelen;
3005 ignorelist = calloc(1, sizeof(*ignorelist));
3006 if (ignorelist == NULL)
3007 return got_error_from_errno("calloc");
3008 TAILQ_INIT(ignorelist);
3010 while ((linelen = getline(&line, &linesize, f)) != -1) {
3011 if (linelen > 0 && line[linelen - 1] == '\n')
3012 line[linelen - 1] = '\0';
3014 /* Git's ignores may contain comments. */
3015 if (line[0] == '#')
3016 continue;
3018 /* Git's negated patterns are not (yet?) supported. */
3019 if (line[0] == '!')
3020 continue;
3022 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3023 line) == -1) {
3024 err = got_error_from_errno("asprintf");
3025 goto done;
3027 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3028 if (err)
3029 goto done;
3031 if (ferror(f)) {
3032 err = got_error_from_errno("getline");
3033 goto done;
3036 dirpath = strdup(path);
3037 if (dirpath == NULL) {
3038 err = got_error_from_errno("strdup");
3039 goto done;
3041 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3042 done:
3043 free(line);
3044 if (err || pe == NULL) {
3045 free(dirpath);
3046 free_ignorelist(ignorelist);
3048 return err;
3051 int
3052 match_ignores(struct got_pathlist_head *ignores, const char *path)
3054 struct got_pathlist_entry *pe;
3056 /* Handle patterns which match in all directories. */
3057 TAILQ_FOREACH(pe, ignores, entry) {
3058 struct got_pathlist_head *ignorelist = pe->data;
3059 struct got_pathlist_entry *pi;
3061 TAILQ_FOREACH(pi, ignorelist, entry) {
3062 const char *p, *pattern = pi->path;
3064 if (strncmp(pattern, "**/", 3) != 0)
3065 continue;
3066 pattern += 3;
3067 p = path;
3068 while (*p) {
3069 if (fnmatch(pattern, p,
3070 FNM_PATHNAME | FNM_LEADING_DIR)) {
3071 /* Retry in next directory. */
3072 while (*p && *p != '/')
3073 p++;
3074 while (*p == '/')
3075 p++;
3076 continue;
3078 return 1;
3084 * The ignores pathlist contains ignore lists from children before
3085 * parents, so we can find the most specific ignorelist by walking
3086 * ignores backwards.
3088 pe = TAILQ_LAST(ignores, got_pathlist_head);
3089 while (pe) {
3090 if (got_path_is_child(path, pe->path, pe->path_len)) {
3091 struct got_pathlist_head *ignorelist = pe->data;
3092 struct got_pathlist_entry *pi;
3093 TAILQ_FOREACH(pi, ignorelist, entry) {
3094 const char *pattern = pi->path;
3095 int flags = FNM_LEADING_DIR;
3096 if (strstr(pattern, "/**/") == NULL)
3097 flags |= FNM_PATHNAME;
3098 if (fnmatch(pattern, path, flags))
3099 continue;
3100 return 1;
3103 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3106 return 0;
3109 static const struct got_error *
3110 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3111 const char *path, int dirfd, const char *ignores_filename)
3113 const struct got_error *err = NULL;
3114 char *ignorespath;
3115 int fd = -1;
3116 FILE *ignoresfile = NULL;
3118 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3119 path[0] ? "/" : "", ignores_filename) == -1)
3120 return got_error_from_errno("asprintf");
3122 if (dirfd != -1) {
3123 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3124 if (fd == -1) {
3125 if (errno != ENOENT && errno != EACCES)
3126 err = got_error_from_errno2("openat",
3127 ignorespath);
3128 } else {
3129 ignoresfile = fdopen(fd, "r");
3130 if (ignoresfile == NULL)
3131 err = got_error_from_errno2("fdopen",
3132 ignorespath);
3133 else {
3134 fd = -1;
3135 err = read_ignores(ignores, path, ignoresfile);
3138 } else {
3139 ignoresfile = fopen(ignorespath, "r");
3140 if (ignoresfile == NULL) {
3141 if (errno != ENOENT && errno != EACCES)
3142 err = got_error_from_errno2("fopen",
3143 ignorespath);
3144 } else
3145 err = read_ignores(ignores, path, ignoresfile);
3148 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3149 err = got_error_from_errno2("fclose", path);
3150 if (fd != -1 && close(fd) == -1 && err == NULL)
3151 err = got_error_from_errno2("close", path);
3152 free(ignorespath);
3153 return err;
3156 static const struct got_error *
3157 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3159 const struct got_error *err = NULL;
3160 struct diff_dir_cb_arg *a = arg;
3161 char *path = NULL;
3163 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3164 return got_error(GOT_ERR_CANCELLED);
3166 if (parent_path[0]) {
3167 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3168 return got_error_from_errno("asprintf");
3169 } else {
3170 path = de->d_name;
3173 if (de->d_type != DT_DIR &&
3174 got_path_is_child(path, a->status_path, a->status_path_len)
3175 && !match_ignores(&a->ignores, path))
3176 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3177 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3178 if (parent_path[0])
3179 free(path);
3180 return err;
3183 static const struct got_error *
3184 status_traverse(void *arg, const char *path, int dirfd)
3186 const struct got_error *err = NULL;
3187 struct diff_dir_cb_arg *a = arg;
3189 if (a->no_ignores)
3190 return NULL;
3192 err = add_ignores(&a->ignores, a->worktree->root_path,
3193 path, dirfd, ".cvsignore");
3194 if (err)
3195 return err;
3197 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3198 dirfd, ".gitignore");
3200 return err;
3203 static const struct got_error *
3204 report_single_file_status(const char *path, const char *ondisk_path,
3205 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3206 void *status_arg, struct got_repository *repo, int report_unchanged)
3208 struct got_fileindex_entry *ie;
3209 struct stat sb;
3211 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3212 if (ie)
3213 return report_file_status(ie, ondisk_path, -1, NULL,
3214 status_cb, status_arg, repo, report_unchanged);
3216 if (lstat(ondisk_path, &sb) == -1) {
3217 if (errno != ENOENT)
3218 return got_error_from_errno2("lstat", ondisk_path);
3219 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3220 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3221 return NULL;
3224 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3225 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3226 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3228 return NULL;
3231 static const struct got_error *
3232 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3233 const char *root_path, const char *path)
3235 const struct got_error *err;
3236 char *parent_path, *next_parent_path = NULL;
3238 err = add_ignores(ignores, root_path, "", -1,
3239 ".cvsignore");
3240 if (err)
3241 return err;
3243 err = add_ignores(ignores, root_path, "", -1,
3244 ".gitignore");
3245 if (err)
3246 return err;
3248 err = got_path_dirname(&parent_path, path);
3249 if (err) {
3250 if (err->code == GOT_ERR_BAD_PATH)
3251 return NULL; /* cannot traverse parent */
3252 return err;
3254 for (;;) {
3255 err = add_ignores(ignores, root_path, parent_path, -1,
3256 ".cvsignore");
3257 if (err)
3258 break;
3259 err = add_ignores(ignores, root_path, parent_path, -1,
3260 ".gitignore");
3261 if (err)
3262 break;
3263 err = got_path_dirname(&next_parent_path, parent_path);
3264 if (err) {
3265 if (err->code == GOT_ERR_BAD_PATH)
3266 err = NULL; /* traversed everything */
3267 break;
3269 free(parent_path);
3270 parent_path = next_parent_path;
3271 next_parent_path = NULL;
3274 free(parent_path);
3275 free(next_parent_path);
3276 return err;
3279 static const struct got_error *
3280 worktree_status(struct got_worktree *worktree, const char *path,
3281 struct got_fileindex *fileindex, struct got_repository *repo,
3282 got_worktree_status_cb status_cb, void *status_arg,
3283 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3284 int report_unchanged)
3286 const struct got_error *err = NULL;
3287 int fd = -1;
3288 struct got_fileindex_diff_dir_cb fdiff_cb;
3289 struct diff_dir_cb_arg arg;
3290 char *ondisk_path = NULL;
3292 TAILQ_INIT(&arg.ignores);
3294 if (asprintf(&ondisk_path, "%s%s%s",
3295 worktree->root_path, path[0] ? "/" : "", path) == -1)
3296 return got_error_from_errno("asprintf");
3298 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3299 if (fd == -1) {
3300 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3301 errno != ELOOP)
3302 err = got_error_from_errno2("open", ondisk_path);
3303 else
3304 err = report_single_file_status(path, ondisk_path,
3305 fileindex, status_cb, status_arg, repo,
3306 report_unchanged);
3307 } else {
3308 fdiff_cb.diff_old_new = status_old_new;
3309 fdiff_cb.diff_old = status_old;
3310 fdiff_cb.diff_new = status_new;
3311 fdiff_cb.diff_traverse = status_traverse;
3312 arg.fileindex = fileindex;
3313 arg.worktree = worktree;
3314 arg.status_path = path;
3315 arg.status_path_len = strlen(path);
3316 arg.repo = repo;
3317 arg.status_cb = status_cb;
3318 arg.status_arg = status_arg;
3319 arg.cancel_cb = cancel_cb;
3320 arg.cancel_arg = cancel_arg;
3321 arg.report_unchanged = report_unchanged;
3322 arg.no_ignores = no_ignores;
3323 if (!no_ignores) {
3324 err = add_ignores_from_parent_paths(&arg.ignores,
3325 worktree->root_path, path);
3326 if (err)
3327 goto done;
3329 err = got_fileindex_diff_dir(fileindex, fd,
3330 worktree->root_path, path, repo, &fdiff_cb, &arg);
3332 done:
3333 free_ignores(&arg.ignores);
3334 if (fd != -1 && close(fd) != 0 && err == NULL)
3335 err = got_error_from_errno("close");
3336 free(ondisk_path);
3337 return err;
3340 const struct got_error *
3341 got_worktree_status(struct got_worktree *worktree,
3342 struct got_pathlist_head *paths, struct got_repository *repo,
3343 got_worktree_status_cb status_cb, void *status_arg,
3344 got_cancel_cb cancel_cb, void *cancel_arg)
3346 const struct got_error *err = NULL;
3347 char *fileindex_path = NULL;
3348 struct got_fileindex *fileindex = NULL;
3349 struct got_pathlist_entry *pe;
3351 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3352 if (err)
3353 return err;
3355 TAILQ_FOREACH(pe, paths, entry) {
3356 err = worktree_status(worktree, pe->path, fileindex, repo,
3357 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3358 if (err)
3359 break;
3361 free(fileindex_path);
3362 got_fileindex_free(fileindex);
3363 return err;
3366 const struct got_error *
3367 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3368 const char *arg)
3370 const struct got_error *err = NULL;
3371 char *resolved = NULL, *cwd = NULL, *path = NULL;
3372 size_t len;
3373 struct stat sb;
3375 *wt_path = NULL;
3377 cwd = getcwd(NULL, 0);
3378 if (cwd == NULL)
3379 return got_error_from_errno("getcwd");
3381 if (lstat(arg, &sb) == -1) {
3382 if (errno != ENOENT) {
3383 err = got_error_from_errno2("lstat", arg);
3384 goto done;
3387 if (S_ISLNK(sb.st_mode)) {
3389 * We cannot use realpath(3) with symlinks since we want to
3390 * operate on the symlink itself.
3391 * But we can make the path absolute, assuming it is relative
3392 * to the current working directory, and then canonicalize it.
3394 char *abspath = NULL;
3395 char canonpath[PATH_MAX];
3396 if (!got_path_is_absolute(arg)) {
3397 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3398 err = got_error_from_errno("asprintf");
3399 goto done;
3403 err = got_canonpath(abspath ? abspath : arg, canonpath,
3404 sizeof(canonpath));
3405 if (err)
3406 goto done;
3407 resolved = strdup(canonpath);
3408 if (resolved == NULL) {
3409 err = got_error_from_errno("strdup");
3410 goto done;
3412 } else {
3413 resolved = realpath(arg, NULL);
3414 if (resolved == NULL) {
3415 if (errno != ENOENT) {
3416 err = got_error_from_errno2("realpath", arg);
3417 goto done;
3419 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3420 err = got_error_from_errno("asprintf");
3421 goto done;
3426 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3427 strlen(got_worktree_get_root_path(worktree)))) {
3428 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3429 goto done;
3432 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3433 err = got_path_skip_common_ancestor(&path,
3434 got_worktree_get_root_path(worktree), resolved);
3435 if (err)
3436 goto done;
3437 } else {
3438 path = strdup("");
3439 if (path == NULL) {
3440 err = got_error_from_errno("strdup");
3441 goto done;
3445 /* XXX status walk can't deal with trailing slash! */
3446 len = strlen(path);
3447 while (len > 0 && path[len - 1] == '/') {
3448 path[len - 1] = '\0';
3449 len--;
3451 done:
3452 free(resolved);
3453 free(cwd);
3454 if (err == NULL)
3455 *wt_path = path;
3456 else
3457 free(path);
3458 return err;
3461 struct schedule_addition_args {
3462 struct got_worktree *worktree;
3463 struct got_fileindex *fileindex;
3464 got_worktree_checkout_cb progress_cb;
3465 void *progress_arg;
3466 struct got_repository *repo;
3469 static const struct got_error *
3470 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3471 const char *relpath, struct got_object_id *blob_id,
3472 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3473 int dirfd, const char *de_name)
3475 struct schedule_addition_args *a = arg;
3476 const struct got_error *err = NULL;
3477 struct got_fileindex_entry *ie;
3478 struct stat sb;
3479 char *ondisk_path;
3481 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3482 relpath) == -1)
3483 return got_error_from_errno("asprintf");
3485 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3486 if (ie) {
3487 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3488 de_name, a->repo);
3489 if (err)
3490 goto done;
3491 /* Re-adding an existing entry is a no-op. */
3492 if (status == GOT_STATUS_ADD)
3493 goto done;
3494 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3495 if (err)
3496 goto done;
3499 if (status != GOT_STATUS_UNVERSIONED) {
3500 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3501 goto done;
3504 err = got_fileindex_entry_alloc(&ie, relpath);
3505 if (err)
3506 goto done;
3507 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3508 if (err) {
3509 got_fileindex_entry_free(ie);
3510 goto done;
3512 err = got_fileindex_entry_add(a->fileindex, ie);
3513 if (err) {
3514 got_fileindex_entry_free(ie);
3515 goto done;
3517 done:
3518 free(ondisk_path);
3519 if (err)
3520 return err;
3521 if (status == GOT_STATUS_ADD)
3522 return NULL;
3523 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3526 const struct got_error *
3527 got_worktree_schedule_add(struct got_worktree *worktree,
3528 struct got_pathlist_head *paths,
3529 got_worktree_checkout_cb progress_cb, void *progress_arg,
3530 struct got_repository *repo, int no_ignores)
3532 struct got_fileindex *fileindex = NULL;
3533 char *fileindex_path = NULL;
3534 const struct got_error *err = NULL, *sync_err, *unlockerr;
3535 struct got_pathlist_entry *pe;
3536 struct schedule_addition_args saa;
3538 err = lock_worktree(worktree, LOCK_EX);
3539 if (err)
3540 return err;
3542 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3543 if (err)
3544 goto done;
3546 saa.worktree = worktree;
3547 saa.fileindex = fileindex;
3548 saa.progress_cb = progress_cb;
3549 saa.progress_arg = progress_arg;
3550 saa.repo = repo;
3552 TAILQ_FOREACH(pe, paths, entry) {
3553 err = worktree_status(worktree, pe->path, fileindex, repo,
3554 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3555 if (err)
3556 break;
3558 sync_err = sync_fileindex(fileindex, fileindex_path);
3559 if (sync_err && err == NULL)
3560 err = sync_err;
3561 done:
3562 free(fileindex_path);
3563 if (fileindex)
3564 got_fileindex_free(fileindex);
3565 unlockerr = lock_worktree(worktree, LOCK_SH);
3566 if (unlockerr && err == NULL)
3567 err = unlockerr;
3568 return err;
3571 struct schedule_deletion_args {
3572 struct got_worktree *worktree;
3573 struct got_fileindex *fileindex;
3574 got_worktree_delete_cb progress_cb;
3575 void *progress_arg;
3576 struct got_repository *repo;
3577 int delete_local_mods;
3578 int keep_on_disk;
3581 static const struct got_error *
3582 schedule_for_deletion(void *arg, unsigned char status,
3583 unsigned char staged_status, const char *relpath,
3584 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3585 struct got_object_id *commit_id, int dirfd, const char *de_name)
3587 struct schedule_deletion_args *a = arg;
3588 const struct got_error *err = NULL;
3589 struct got_fileindex_entry *ie = NULL;
3590 struct stat sb;
3591 char *ondisk_path, *parent = NULL;
3593 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3594 if (ie == NULL)
3595 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3597 staged_status = get_staged_status(ie);
3598 if (staged_status != GOT_STATUS_NO_CHANGE) {
3599 if (staged_status == GOT_STATUS_DELETE)
3600 return NULL;
3601 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3604 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3605 relpath) == -1)
3606 return got_error_from_errno("asprintf");
3608 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3609 a->repo);
3610 if (err)
3611 goto done;
3613 if (status != GOT_STATUS_NO_CHANGE) {
3614 if (status == GOT_STATUS_DELETE)
3615 goto done;
3616 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3617 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3618 goto done;
3620 if (status != GOT_STATUS_MODIFY &&
3621 status != GOT_STATUS_MISSING) {
3622 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3623 goto done;
3627 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3628 if (dirfd != -1) {
3629 if (unlinkat(dirfd, de_name, 0) != 0) {
3630 err = got_error_from_errno2("unlinkat",
3631 ondisk_path);
3632 goto done;
3634 } else if (unlink(ondisk_path) != 0) {
3635 err = got_error_from_errno2("unlink", ondisk_path);
3636 goto done;
3639 parent = dirname(ondisk_path);
3641 if (parent == NULL) {
3642 err = got_error_from_errno2("dirname", ondisk_path);
3643 goto done;
3645 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3646 if (rmdir(parent) == -1) {
3647 if (errno != ENOTEMPTY)
3648 err = got_error_from_errno2("rmdir",
3649 parent);
3650 break;
3652 parent = dirname(parent);
3653 if (parent == NULL) {
3654 err = got_error_from_errno2("dirname", parent);
3655 goto done;
3660 got_fileindex_entry_mark_deleted_from_disk(ie);
3661 done:
3662 free(ondisk_path);
3663 if (err)
3664 return err;
3665 if (status == GOT_STATUS_DELETE)
3666 return NULL;
3667 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3668 staged_status, relpath);
3671 const struct got_error *
3672 got_worktree_schedule_delete(struct got_worktree *worktree,
3673 struct got_pathlist_head *paths, int delete_local_mods,
3674 got_worktree_delete_cb progress_cb, void *progress_arg,
3675 struct got_repository *repo, int keep_on_disk)
3677 struct got_fileindex *fileindex = NULL;
3678 char *fileindex_path = NULL;
3679 const struct got_error *err = NULL, *sync_err, *unlockerr;
3680 struct got_pathlist_entry *pe;
3681 struct schedule_deletion_args sda;
3683 err = lock_worktree(worktree, LOCK_EX);
3684 if (err)
3685 return err;
3687 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3688 if (err)
3689 goto done;
3691 sda.worktree = worktree;
3692 sda.fileindex = fileindex;
3693 sda.progress_cb = progress_cb;
3694 sda.progress_arg = progress_arg;
3695 sda.repo = repo;
3696 sda.delete_local_mods = delete_local_mods;
3697 sda.keep_on_disk = keep_on_disk;
3699 TAILQ_FOREACH(pe, paths, entry) {
3700 err = worktree_status(worktree, pe->path, fileindex, repo,
3701 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3702 if (err)
3703 break;
3705 sync_err = sync_fileindex(fileindex, fileindex_path);
3706 if (sync_err && err == NULL)
3707 err = sync_err;
3708 done:
3709 free(fileindex_path);
3710 if (fileindex)
3711 got_fileindex_free(fileindex);
3712 unlockerr = lock_worktree(worktree, LOCK_SH);
3713 if (unlockerr && err == NULL)
3714 err = unlockerr;
3715 return err;
3718 static const struct got_error *
3719 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3721 const struct got_error *err = NULL;
3722 char *line = NULL;
3723 size_t linesize = 0, n;
3724 ssize_t linelen;
3726 linelen = getline(&line, &linesize, infile);
3727 if (linelen == -1) {
3728 if (ferror(infile)) {
3729 err = got_error_from_errno("getline");
3730 goto done;
3732 return NULL;
3734 if (outfile) {
3735 n = fwrite(line, 1, linelen, outfile);
3736 if (n != linelen) {
3737 err = got_ferror(outfile, GOT_ERR_IO);
3738 goto done;
3741 if (rejectfile) {
3742 n = fwrite(line, 1, linelen, rejectfile);
3743 if (n != linelen)
3744 err = got_ferror(outfile, GOT_ERR_IO);
3746 done:
3747 free(line);
3748 return err;
3751 static const struct got_error *
3752 skip_one_line(FILE *f)
3754 char *line = NULL;
3755 size_t linesize = 0;
3756 ssize_t linelen;
3758 linelen = getline(&line, &linesize, f);
3759 if (linelen == -1) {
3760 if (ferror(f))
3761 return got_error_from_errno("getline");
3762 return NULL;
3764 free(line);
3765 return NULL;
3768 static const struct got_error *
3769 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3770 int start_old, int end_old, int start_new, int end_new,
3771 FILE *outfile, FILE *rejectfile)
3773 const struct got_error *err;
3775 /* Copy old file's lines leading up to patch. */
3776 while (!feof(f1) && *line_cur1 < start_old) {
3777 err = copy_one_line(f1, outfile, NULL);
3778 if (err)
3779 return err;
3780 (*line_cur1)++;
3782 /* Skip new file's lines leading up to patch. */
3783 while (!feof(f2) && *line_cur2 < start_new) {
3784 if (rejectfile)
3785 err = copy_one_line(f2, NULL, rejectfile);
3786 else
3787 err = skip_one_line(f2);
3788 if (err)
3789 return err;
3790 (*line_cur2)++;
3792 /* Copy patched lines. */
3793 while (!feof(f2) && *line_cur2 <= end_new) {
3794 err = copy_one_line(f2, outfile, NULL);
3795 if (err)
3796 return err;
3797 (*line_cur2)++;
3799 /* Skip over old file's replaced lines. */
3800 while (!feof(f1) && *line_cur1 <= end_old) {
3801 if (rejectfile)
3802 err = copy_one_line(f1, NULL, rejectfile);
3803 else
3804 err = skip_one_line(f1);
3805 if (err)
3806 return err;
3807 (*line_cur1)++;
3810 return NULL;
3813 static const struct got_error *
3814 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3815 FILE *outfile, FILE *rejectfile)
3817 const struct got_error *err;
3819 if (outfile) {
3820 /* Copy old file's lines until EOF. */
3821 while (!feof(f1)) {
3822 err = copy_one_line(f1, outfile, NULL);
3823 if (err)
3824 return err;
3825 (*line_cur1)++;
3828 if (rejectfile) {
3829 /* Copy new file's lines until EOF. */
3830 while (!feof(f2)) {
3831 err = copy_one_line(f2, NULL, rejectfile);
3832 if (err)
3833 return err;
3834 (*line_cur2)++;
3838 return NULL;
3841 static const struct got_error *
3842 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3843 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3844 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3845 int *line_cur2, FILE *outfile, FILE *rejectfile,
3846 got_worktree_patch_cb patch_cb, void *patch_arg)
3848 const struct got_error *err = NULL;
3849 int start_old = change->cv.a;
3850 int end_old = change->cv.b;
3851 int start_new = change->cv.c;
3852 int end_new = change->cv.d;
3853 long pos1, pos2;
3854 FILE *hunkfile;
3856 *choice = GOT_PATCH_CHOICE_NONE;
3858 hunkfile = got_opentemp();
3859 if (hunkfile == NULL)
3860 return got_error_from_errno("got_opentemp");
3862 pos1 = ftell(f1);
3863 pos2 = ftell(f2);
3865 /* XXX TODO needs error checking */
3866 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3868 if (fseek(f1, pos1, SEEK_SET) == -1) {
3869 err = got_ferror(f1, GOT_ERR_IO);
3870 goto done;
3872 if (fseek(f2, pos2, SEEK_SET) == -1) {
3873 err = got_ferror(f1, GOT_ERR_IO);
3874 goto done;
3876 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3877 err = got_ferror(hunkfile, GOT_ERR_IO);
3878 goto done;
3881 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3882 hunkfile, n, nchanges);
3883 if (err)
3884 goto done;
3886 switch (*choice) {
3887 case GOT_PATCH_CHOICE_YES:
3888 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3889 end_old, start_new, end_new, outfile, rejectfile);
3890 break;
3891 case GOT_PATCH_CHOICE_NO:
3892 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3893 end_old, start_new, end_new, rejectfile, outfile);
3894 break;
3895 case GOT_PATCH_CHOICE_QUIT:
3896 break;
3897 default:
3898 err = got_error(GOT_ERR_PATCH_CHOICE);
3899 break;
3901 done:
3902 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3903 err = got_error_from_errno("fclose");
3904 return err;
3907 struct revert_file_args {
3908 struct got_worktree *worktree;
3909 struct got_fileindex *fileindex;
3910 got_worktree_checkout_cb progress_cb;
3911 void *progress_arg;
3912 got_worktree_patch_cb patch_cb;
3913 void *patch_arg;
3914 struct got_repository *repo;
3917 static const struct got_error *
3918 create_patched_content(char **path_outfile, int reverse_patch,
3919 struct got_object_id *blob_id, const char *path2,
3920 int dirfd2, const char *de_name2,
3921 const char *relpath, struct got_repository *repo,
3922 got_worktree_patch_cb patch_cb, void *patch_arg)
3924 const struct got_error *err;
3925 struct got_blob_object *blob = NULL;
3926 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3927 int fd2 = -1;
3928 char *path1 = NULL, *id_str = NULL;
3929 struct stat sb1, sb2;
3930 struct got_diff_changes *changes = NULL;
3931 struct got_diff_state *ds = NULL;
3932 struct got_diff_args *args = NULL;
3933 struct got_diff_change *change;
3934 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3935 int n = 0;
3937 *path_outfile = NULL;
3939 err = got_object_id_str(&id_str, blob_id);
3940 if (err)
3941 return err;
3943 if (dirfd2 != -1) {
3944 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3945 if (fd2 == -1) {
3946 err = got_error_from_errno2("openat", path2);
3947 goto done;
3949 } else {
3950 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3951 if (fd2 == -1) {
3952 err = got_error_from_errno2("open", path2);
3953 goto done;
3956 if (fstat(fd2, &sb2) == -1) {
3957 err = got_error_from_errno2("fstat", path2);
3958 goto done;
3961 f2 = fdopen(fd2, "r");
3962 if (f2 == NULL) {
3963 err = got_error_from_errno2("fdopen", path2);
3964 goto done;
3966 fd2 = -1;
3968 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3969 if (err)
3970 goto done;
3972 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3973 if (err)
3974 goto done;
3976 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3977 if (err)
3978 goto done;
3980 if (stat(path1, &sb1) == -1) {
3981 err = got_error_from_errno2("stat", path1);
3982 goto done;
3985 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3986 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3987 if (err)
3988 goto done;
3990 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3991 if (err)
3992 goto done;
3994 if (fseek(f1, 0L, SEEK_SET) == -1)
3995 return got_ferror(f1, GOT_ERR_IO);
3996 if (fseek(f2, 0L, SEEK_SET) == -1)
3997 return got_ferror(f2, GOT_ERR_IO);
3998 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3999 int choice;
4000 err = apply_or_reject_change(&choice, change, ++n,
4001 changes->nchanges, ds, args, diff_flags, relpath,
4002 f1, f2, &line_cur1, &line_cur2,
4003 reverse_patch ? NULL : outfile,
4004 reverse_patch ? outfile : NULL,
4005 patch_cb, patch_arg);
4006 if (err)
4007 goto done;
4008 if (choice == GOT_PATCH_CHOICE_YES)
4009 have_content = 1;
4010 else if (choice == GOT_PATCH_CHOICE_QUIT)
4011 break;
4013 if (have_content) {
4014 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4015 reverse_patch ? NULL : outfile,
4016 reverse_patch ? outfile : NULL);
4017 if (err)
4018 goto done;
4020 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4021 err = got_error_from_errno2("chmod", path2);
4022 goto done;
4025 done:
4026 free(id_str);
4027 if (blob)
4028 got_object_blob_close(blob);
4029 if (f1 && fclose(f1) == EOF && err == NULL)
4030 err = got_error_from_errno2("fclose", path1);
4031 if (f2 && fclose(f2) == EOF && err == NULL)
4032 err = got_error_from_errno2("fclose", path2);
4033 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4034 err = got_error_from_errno2("close", path2);
4035 if (outfile && fclose(outfile) == EOF && err == NULL)
4036 err = got_error_from_errno2("fclose", *path_outfile);
4037 if (path1 && unlink(path1) == -1 && err == NULL)
4038 err = got_error_from_errno2("unlink", path1);
4039 if (err || !have_content) {
4040 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4041 err = got_error_from_errno2("unlink", *path_outfile);
4042 free(*path_outfile);
4043 *path_outfile = NULL;
4045 free(args);
4046 if (ds) {
4047 got_diff_state_free(ds);
4048 free(ds);
4050 if (changes)
4051 got_diff_free_changes(changes);
4052 free(path1);
4053 return err;
4056 static const struct got_error *
4057 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4058 const char *relpath, struct got_object_id *blob_id,
4059 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4060 int dirfd, const char *de_name)
4062 struct revert_file_args *a = arg;
4063 const struct got_error *err = NULL;
4064 char *parent_path = NULL;
4065 struct got_fileindex_entry *ie;
4066 struct got_tree_object *tree = NULL;
4067 struct got_object_id *tree_id = NULL;
4068 const struct got_tree_entry *te = NULL;
4069 char *tree_path = NULL, *te_name;
4070 char *ondisk_path = NULL, *path_content = NULL;
4071 struct got_blob_object *blob = NULL;
4073 /* Reverting a staged deletion is a no-op. */
4074 if (status == GOT_STATUS_DELETE &&
4075 staged_status != GOT_STATUS_NO_CHANGE)
4076 return NULL;
4078 if (status == GOT_STATUS_UNVERSIONED)
4079 return (*a->progress_cb)(a->progress_arg,
4080 GOT_STATUS_UNVERSIONED, relpath);
4082 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4083 if (ie == NULL)
4084 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4086 /* Construct in-repository path of tree which contains this blob. */
4087 err = got_path_dirname(&parent_path, ie->path);
4088 if (err) {
4089 if (err->code != GOT_ERR_BAD_PATH)
4090 goto done;
4091 parent_path = strdup("/");
4092 if (parent_path == NULL) {
4093 err = got_error_from_errno("strdup");
4094 goto done;
4097 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4098 tree_path = strdup(parent_path);
4099 if (tree_path == NULL) {
4100 err = got_error_from_errno("strdup");
4101 goto done;
4103 } else {
4104 if (got_path_is_root_dir(parent_path)) {
4105 tree_path = strdup(a->worktree->path_prefix);
4106 if (tree_path == NULL) {
4107 err = got_error_from_errno("strdup");
4108 goto done;
4110 } else {
4111 if (asprintf(&tree_path, "%s/%s",
4112 a->worktree->path_prefix, parent_path) == -1) {
4113 err = got_error_from_errno("asprintf");
4114 goto done;
4119 err = got_object_id_by_path(&tree_id, a->repo,
4120 a->worktree->base_commit_id, tree_path);
4121 if (err) {
4122 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4123 (status == GOT_STATUS_ADD ||
4124 staged_status == GOT_STATUS_ADD)))
4125 goto done;
4126 } else {
4127 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4128 if (err)
4129 goto done;
4131 te_name = basename(ie->path);
4132 if (te_name == NULL) {
4133 err = got_error_from_errno2("basename", ie->path);
4134 goto done;
4137 te = got_object_tree_find_entry(tree, te_name);
4138 if (te == NULL && status != GOT_STATUS_ADD &&
4139 staged_status != GOT_STATUS_ADD) {
4140 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4141 goto done;
4145 switch (status) {
4146 case GOT_STATUS_ADD:
4147 if (a->patch_cb) {
4148 int choice = GOT_PATCH_CHOICE_NONE;
4149 err = (*a->patch_cb)(&choice, a->patch_arg,
4150 status, ie->path, NULL, 1, 1);
4151 if (err)
4152 goto done;
4153 if (choice != GOT_PATCH_CHOICE_YES)
4154 break;
4156 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4157 ie->path);
4158 if (err)
4159 goto done;
4160 got_fileindex_entry_remove(a->fileindex, ie);
4161 break;
4162 case GOT_STATUS_DELETE:
4163 if (a->patch_cb) {
4164 int choice = GOT_PATCH_CHOICE_NONE;
4165 err = (*a->patch_cb)(&choice, a->patch_arg,
4166 status, ie->path, NULL, 1, 1);
4167 if (err)
4168 goto done;
4169 if (choice != GOT_PATCH_CHOICE_YES)
4170 break;
4172 /* fall through */
4173 case GOT_STATUS_MODIFY:
4174 case GOT_STATUS_MODE_CHANGE:
4175 case GOT_STATUS_CONFLICT:
4176 case GOT_STATUS_MISSING: {
4177 struct got_object_id id;
4178 if (staged_status == GOT_STATUS_ADD ||
4179 staged_status == GOT_STATUS_MODIFY) {
4180 memcpy(id.sha1, ie->staged_blob_sha1,
4181 SHA1_DIGEST_LENGTH);
4182 } else
4183 memcpy(id.sha1, ie->blob_sha1,
4184 SHA1_DIGEST_LENGTH);
4185 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4186 if (err)
4187 goto done;
4189 if (asprintf(&ondisk_path, "%s/%s",
4190 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4191 err = got_error_from_errno("asprintf");
4192 goto done;
4195 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4196 status == GOT_STATUS_CONFLICT)) {
4197 err = create_patched_content(&path_content, 1, &id,
4198 ondisk_path, dirfd, de_name, ie->path, a->repo,
4199 a->patch_cb, a->patch_arg);
4200 if (err || path_content == NULL)
4201 break;
4202 if (rename(path_content, ondisk_path) == -1) {
4203 err = got_error_from_errno3("rename",
4204 path_content, ondisk_path);
4205 goto done;
4207 } else {
4208 err = install_blob(a->worktree, ondisk_path, ie->path,
4209 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4210 got_fileindex_perms_to_st(ie), blob, 0, 1, 0,
4211 a->repo, a->progress_cb, a->progress_arg);
4212 if (err)
4213 goto done;
4214 if (status == GOT_STATUS_DELETE ||
4215 status == GOT_STATUS_MODE_CHANGE) {
4216 err = got_fileindex_entry_update(ie,
4217 ondisk_path, blob->id.sha1,
4218 a->worktree->base_commit_id->sha1, 1);
4219 if (err)
4220 goto done;
4223 break;
4225 default:
4226 break;
4228 done:
4229 free(ondisk_path);
4230 free(path_content);
4231 free(parent_path);
4232 free(tree_path);
4233 if (blob)
4234 got_object_blob_close(blob);
4235 if (tree)
4236 got_object_tree_close(tree);
4237 free(tree_id);
4238 return err;
4241 const struct got_error *
4242 got_worktree_revert(struct got_worktree *worktree,
4243 struct got_pathlist_head *paths,
4244 got_worktree_checkout_cb progress_cb, void *progress_arg,
4245 got_worktree_patch_cb patch_cb, void *patch_arg,
4246 struct got_repository *repo)
4248 struct got_fileindex *fileindex = NULL;
4249 char *fileindex_path = NULL;
4250 const struct got_error *err = NULL, *unlockerr = NULL;
4251 const struct got_error *sync_err = NULL;
4252 struct got_pathlist_entry *pe;
4253 struct revert_file_args rfa;
4255 err = lock_worktree(worktree, LOCK_EX);
4256 if (err)
4257 return err;
4259 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4260 if (err)
4261 goto done;
4263 rfa.worktree = worktree;
4264 rfa.fileindex = fileindex;
4265 rfa.progress_cb = progress_cb;
4266 rfa.progress_arg = progress_arg;
4267 rfa.patch_cb = patch_cb;
4268 rfa.patch_arg = patch_arg;
4269 rfa.repo = repo;
4270 TAILQ_FOREACH(pe, paths, entry) {
4271 err = worktree_status(worktree, pe->path, fileindex, repo,
4272 revert_file, &rfa, NULL, NULL, 0, 0);
4273 if (err)
4274 break;
4276 sync_err = sync_fileindex(fileindex, fileindex_path);
4277 if (sync_err && err == NULL)
4278 err = sync_err;
4279 done:
4280 free(fileindex_path);
4281 if (fileindex)
4282 got_fileindex_free(fileindex);
4283 unlockerr = lock_worktree(worktree, LOCK_SH);
4284 if (unlockerr && err == NULL)
4285 err = unlockerr;
4286 return err;
4289 static void
4290 free_commitable(struct got_commitable *ct)
4292 free(ct->path);
4293 free(ct->in_repo_path);
4294 free(ct->ondisk_path);
4295 free(ct->blob_id);
4296 free(ct->base_blob_id);
4297 free(ct->staged_blob_id);
4298 free(ct->base_commit_id);
4299 free(ct);
4302 struct collect_commitables_arg {
4303 struct got_pathlist_head *commitable_paths;
4304 struct got_repository *repo;
4305 struct got_worktree *worktree;
4306 int have_staged_files;
4309 static const struct got_error *
4310 collect_commitables(void *arg, unsigned char status,
4311 unsigned char staged_status, const char *relpath,
4312 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4313 struct got_object_id *commit_id, int dirfd, const char *de_name)
4315 struct collect_commitables_arg *a = arg;
4316 const struct got_error *err = NULL;
4317 struct got_commitable *ct = NULL;
4318 struct got_pathlist_entry *new = NULL;
4319 char *parent_path = NULL, *path = NULL;
4320 struct stat sb;
4322 if (a->have_staged_files) {
4323 if (staged_status != GOT_STATUS_MODIFY &&
4324 staged_status != GOT_STATUS_ADD &&
4325 staged_status != GOT_STATUS_DELETE)
4326 return NULL;
4327 } else {
4328 if (status == GOT_STATUS_CONFLICT)
4329 return got_error(GOT_ERR_COMMIT_CONFLICT);
4331 if (status != GOT_STATUS_MODIFY &&
4332 status != GOT_STATUS_MODE_CHANGE &&
4333 status != GOT_STATUS_ADD &&
4334 status != GOT_STATUS_DELETE)
4335 return NULL;
4338 if (asprintf(&path, "/%s", relpath) == -1) {
4339 err = got_error_from_errno("asprintf");
4340 goto done;
4342 if (strcmp(path, "/") == 0) {
4343 parent_path = strdup("");
4344 if (parent_path == NULL)
4345 return got_error_from_errno("strdup");
4346 } else {
4347 err = got_path_dirname(&parent_path, path);
4348 if (err)
4349 return err;
4352 ct = calloc(1, sizeof(*ct));
4353 if (ct == NULL) {
4354 err = got_error_from_errno("calloc");
4355 goto done;
4358 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4359 relpath) == -1) {
4360 err = got_error_from_errno("asprintf");
4361 goto done;
4363 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4364 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4365 } else {
4366 if (dirfd != -1) {
4367 if (fstatat(dirfd, de_name, &sb,
4368 AT_SYMLINK_NOFOLLOW) == -1) {
4369 err = got_error_from_errno2("fstatat",
4370 ct->ondisk_path);
4371 goto done;
4373 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4374 err = got_error_from_errno2("lstat", ct->ondisk_path);
4375 goto done;
4377 ct->mode = sb.st_mode;
4380 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4381 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4382 relpath) == -1) {
4383 err = got_error_from_errno("asprintf");
4384 goto done;
4387 ct->status = status;
4388 ct->staged_status = staged_status;
4389 ct->blob_id = NULL; /* will be filled in when blob gets created */
4390 if (ct->status != GOT_STATUS_ADD &&
4391 ct->staged_status != GOT_STATUS_ADD) {
4392 ct->base_blob_id = got_object_id_dup(blob_id);
4393 if (ct->base_blob_id == NULL) {
4394 err = got_error_from_errno("got_object_id_dup");
4395 goto done;
4397 ct->base_commit_id = got_object_id_dup(commit_id);
4398 if (ct->base_commit_id == NULL) {
4399 err = got_error_from_errno("got_object_id_dup");
4400 goto done;
4403 if (ct->staged_status == GOT_STATUS_ADD ||
4404 ct->staged_status == GOT_STATUS_MODIFY) {
4405 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4406 if (ct->staged_blob_id == NULL) {
4407 err = got_error_from_errno("got_object_id_dup");
4408 goto done;
4411 ct->path = strdup(path);
4412 if (ct->path == NULL) {
4413 err = got_error_from_errno("strdup");
4414 goto done;
4416 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4417 done:
4418 if (ct && (err || new == NULL))
4419 free_commitable(ct);
4420 free(parent_path);
4421 free(path);
4422 return err;
4425 static const struct got_error *write_tree(struct got_object_id **, int *,
4426 struct got_tree_object *, const char *, struct got_pathlist_head *,
4427 got_worktree_status_cb status_cb, void *status_arg,
4428 struct got_repository *);
4430 static const struct got_error *
4431 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4432 struct got_tree_entry *te, const char *parent_path,
4433 struct got_pathlist_head *commitable_paths,
4434 got_worktree_status_cb status_cb, void *status_arg,
4435 struct got_repository *repo)
4437 const struct got_error *err = NULL;
4438 struct got_tree_object *subtree;
4439 char *subpath;
4441 if (asprintf(&subpath, "%s%s%s", parent_path,
4442 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4443 return got_error_from_errno("asprintf");
4445 err = got_object_open_as_tree(&subtree, repo, &te->id);
4446 if (err)
4447 return err;
4449 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4450 commitable_paths, status_cb, status_arg, repo);
4451 got_object_tree_close(subtree);
4452 free(subpath);
4453 return err;
4456 static const struct got_error *
4457 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4459 const struct got_error *err = NULL;
4460 char *ct_parent_path = NULL;
4462 *match = 0;
4464 if (strchr(ct->in_repo_path, '/') == NULL) {
4465 *match = got_path_is_root_dir(path);
4466 return NULL;
4469 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4470 if (err)
4471 return err;
4472 *match = (strcmp(path, ct_parent_path) == 0);
4473 free(ct_parent_path);
4474 return err;
4477 static mode_t
4478 get_ct_file_mode(struct got_commitable *ct)
4480 if (S_ISLNK(ct->mode))
4481 return S_IFLNK;
4483 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4486 static const struct got_error *
4487 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4488 struct got_tree_entry *te, struct got_commitable *ct)
4490 const struct got_error *err = NULL;
4492 *new_te = NULL;
4494 err = got_object_tree_entry_dup(new_te, te);
4495 if (err)
4496 goto done;
4498 (*new_te)->mode = get_ct_file_mode(ct);
4500 if (ct->staged_status == GOT_STATUS_MODIFY)
4501 memcpy(&(*new_te)->id, ct->staged_blob_id,
4502 sizeof((*new_te)->id));
4503 else
4504 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4505 done:
4506 if (err && *new_te) {
4507 free(*new_te);
4508 *new_te = NULL;
4510 return err;
4513 static const struct got_error *
4514 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4515 struct got_commitable *ct)
4517 const struct got_error *err = NULL;
4518 char *ct_name;
4520 *new_te = NULL;
4522 *new_te = calloc(1, sizeof(**new_te));
4523 if (*new_te == NULL)
4524 return got_error_from_errno("calloc");
4526 ct_name = basename(ct->path);
4527 if (ct_name == NULL) {
4528 err = got_error_from_errno2("basename", ct->path);
4529 goto done;
4531 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4532 sizeof((*new_te)->name)) {
4533 err = got_error(GOT_ERR_NO_SPACE);
4534 goto done;
4537 (*new_te)->mode = get_ct_file_mode(ct);
4539 if (ct->staged_status == GOT_STATUS_ADD)
4540 memcpy(&(*new_te)->id, ct->staged_blob_id,
4541 sizeof((*new_te)->id));
4542 else
4543 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4544 done:
4545 if (err && *new_te) {
4546 free(*new_te);
4547 *new_te = NULL;
4549 return err;
4552 static const struct got_error *
4553 insert_tree_entry(struct got_tree_entry *new_te,
4554 struct got_pathlist_head *paths)
4556 const struct got_error *err = NULL;
4557 struct got_pathlist_entry *new_pe;
4559 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4560 if (err)
4561 return err;
4562 if (new_pe == NULL)
4563 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4564 return NULL;
4567 static const struct got_error *
4568 report_ct_status(struct got_commitable *ct,
4569 got_worktree_status_cb status_cb, void *status_arg)
4571 const char *ct_path = ct->path;
4572 unsigned char status;
4574 while (ct_path[0] == '/')
4575 ct_path++;
4577 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4578 status = ct->staged_status;
4579 else
4580 status = ct->status;
4582 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4583 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4586 static const struct got_error *
4587 match_modified_subtree(int *modified, struct got_tree_entry *te,
4588 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4590 const struct got_error *err = NULL;
4591 struct got_pathlist_entry *pe;
4592 char *te_path;
4594 *modified = 0;
4596 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4597 got_path_is_root_dir(base_tree_path) ? "" : "/",
4598 te->name) == -1)
4599 return got_error_from_errno("asprintf");
4601 TAILQ_FOREACH(pe, commitable_paths, entry) {
4602 struct got_commitable *ct = pe->data;
4603 *modified = got_path_is_child(ct->in_repo_path, te_path,
4604 strlen(te_path));
4605 if (*modified)
4606 break;
4609 free(te_path);
4610 return err;
4613 static const struct got_error *
4614 match_deleted_or_modified_ct(struct got_commitable **ctp,
4615 struct got_tree_entry *te, const char *base_tree_path,
4616 struct got_pathlist_head *commitable_paths)
4618 const struct got_error *err = NULL;
4619 struct got_pathlist_entry *pe;
4621 *ctp = NULL;
4623 TAILQ_FOREACH(pe, commitable_paths, entry) {
4624 struct got_commitable *ct = pe->data;
4625 char *ct_name = NULL;
4626 int path_matches;
4628 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4629 if (ct->status != GOT_STATUS_MODIFY &&
4630 ct->status != GOT_STATUS_MODE_CHANGE &&
4631 ct->status != GOT_STATUS_DELETE)
4632 continue;
4633 } else {
4634 if (ct->staged_status != GOT_STATUS_MODIFY &&
4635 ct->staged_status != GOT_STATUS_DELETE)
4636 continue;
4639 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4640 continue;
4642 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4643 if (err)
4644 return err;
4645 if (!path_matches)
4646 continue;
4648 ct_name = basename(pe->path);
4649 if (ct_name == NULL)
4650 return got_error_from_errno2("basename", pe->path);
4652 if (strcmp(te->name, ct_name) != 0)
4653 continue;
4655 *ctp = ct;
4656 break;
4659 return err;
4662 static const struct got_error *
4663 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4664 const char *child_path, const char *path_base_tree,
4665 struct got_pathlist_head *commitable_paths,
4666 got_worktree_status_cb status_cb, void *status_arg,
4667 struct got_repository *repo)
4669 const struct got_error *err = NULL;
4670 struct got_tree_entry *new_te;
4671 char *subtree_path;
4672 struct got_object_id *id = NULL;
4673 int nentries;
4675 *new_tep = NULL;
4677 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4678 got_path_is_root_dir(path_base_tree) ? "" : "/",
4679 child_path) == -1)
4680 return got_error_from_errno("asprintf");
4682 new_te = calloc(1, sizeof(*new_te));
4683 if (new_te == NULL)
4684 return got_error_from_errno("calloc");
4685 new_te->mode = S_IFDIR;
4687 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4688 sizeof(new_te->name)) {
4689 err = got_error(GOT_ERR_NO_SPACE);
4690 goto done;
4692 err = write_tree(&id, &nentries, NULL, subtree_path,
4693 commitable_paths, status_cb, status_arg, repo);
4694 if (err) {
4695 free(new_te);
4696 goto done;
4698 memcpy(&new_te->id, id, sizeof(new_te->id));
4699 done:
4700 free(id);
4701 free(subtree_path);
4702 if (err == NULL)
4703 *new_tep = new_te;
4704 return err;
4707 static const struct got_error *
4708 write_tree(struct got_object_id **new_tree_id, int *nentries,
4709 struct got_tree_object *base_tree, const char *path_base_tree,
4710 struct got_pathlist_head *commitable_paths,
4711 got_worktree_status_cb status_cb, void *status_arg,
4712 struct got_repository *repo)
4714 const struct got_error *err = NULL;
4715 struct got_pathlist_head paths;
4716 struct got_tree_entry *te, *new_te = NULL;
4717 struct got_pathlist_entry *pe;
4719 TAILQ_INIT(&paths);
4720 *nentries = 0;
4722 /* Insert, and recurse into, newly added entries first. */
4723 TAILQ_FOREACH(pe, commitable_paths, entry) {
4724 struct got_commitable *ct = pe->data;
4725 char *child_path = NULL, *slash;
4727 if ((ct->status != GOT_STATUS_ADD &&
4728 ct->staged_status != GOT_STATUS_ADD) ||
4729 (ct->flags & GOT_COMMITABLE_ADDED))
4730 continue;
4732 if (!got_path_is_child(pe->path, path_base_tree,
4733 strlen(path_base_tree)))
4734 continue;
4736 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4737 pe->path);
4738 if (err)
4739 goto done;
4741 slash = strchr(child_path, '/');
4742 if (slash == NULL) {
4743 err = alloc_added_blob_tree_entry(&new_te, ct);
4744 if (err)
4745 goto done;
4746 err = report_ct_status(ct, status_cb, status_arg);
4747 if (err)
4748 goto done;
4749 ct->flags |= GOT_COMMITABLE_ADDED;
4750 err = insert_tree_entry(new_te, &paths);
4751 if (err)
4752 goto done;
4753 (*nentries)++;
4754 } else {
4755 *slash = '\0'; /* trim trailing path components */
4756 if (base_tree == NULL ||
4757 got_object_tree_find_entry(base_tree, child_path)
4758 == NULL) {
4759 err = make_subtree_for_added_blob(&new_te,
4760 child_path, path_base_tree,
4761 commitable_paths, status_cb, status_arg,
4762 repo);
4763 if (err)
4764 goto done;
4765 err = insert_tree_entry(new_te, &paths);
4766 if (err)
4767 goto done;
4768 (*nentries)++;
4773 if (base_tree) {
4774 int i, nbase_entries;
4775 /* Handle modified and deleted entries. */
4776 nbase_entries = got_object_tree_get_nentries(base_tree);
4777 for (i = 0; i < nbase_entries; i++) {
4778 struct got_commitable *ct = NULL;
4780 te = got_object_tree_get_entry(base_tree, i);
4781 if (got_object_tree_entry_is_submodule(te)) {
4782 /* Entry is a submodule; just copy it. */
4783 err = got_object_tree_entry_dup(&new_te, te);
4784 if (err)
4785 goto done;
4786 err = insert_tree_entry(new_te, &paths);
4787 if (err)
4788 goto done;
4789 (*nentries)++;
4790 continue;
4793 if (S_ISDIR(te->mode)) {
4794 int modified;
4795 err = got_object_tree_entry_dup(&new_te, te);
4796 if (err)
4797 goto done;
4798 err = match_modified_subtree(&modified, te,
4799 path_base_tree, commitable_paths);
4800 if (err)
4801 goto done;
4802 /* Avoid recursion into unmodified subtrees. */
4803 if (modified) {
4804 struct got_object_id *new_id;
4805 int nsubentries;
4806 err = write_subtree(&new_id,
4807 &nsubentries, te,
4808 path_base_tree, commitable_paths,
4809 status_cb, status_arg, repo);
4810 if (err)
4811 goto done;
4812 if (nsubentries == 0) {
4813 /* All entries were deleted. */
4814 free(new_id);
4815 continue;
4817 memcpy(&new_te->id, new_id,
4818 sizeof(new_te->id));
4819 free(new_id);
4821 err = insert_tree_entry(new_te, &paths);
4822 if (err)
4823 goto done;
4824 (*nentries)++;
4825 continue;
4828 err = match_deleted_or_modified_ct(&ct, te,
4829 path_base_tree, commitable_paths);
4830 if (err)
4831 goto done;
4832 if (ct) {
4833 /* NB: Deleted entries get dropped here. */
4834 if (ct->status == GOT_STATUS_MODIFY ||
4835 ct->status == GOT_STATUS_MODE_CHANGE ||
4836 ct->staged_status == GOT_STATUS_MODIFY) {
4837 err = alloc_modified_blob_tree_entry(
4838 &new_te, te, ct);
4839 if (err)
4840 goto done;
4841 err = insert_tree_entry(new_te, &paths);
4842 if (err)
4843 goto done;
4844 (*nentries)++;
4846 err = report_ct_status(ct, status_cb,
4847 status_arg);
4848 if (err)
4849 goto done;
4850 } else {
4851 /* Entry is unchanged; just copy it. */
4852 err = got_object_tree_entry_dup(&new_te, te);
4853 if (err)
4854 goto done;
4855 err = insert_tree_entry(new_te, &paths);
4856 if (err)
4857 goto done;
4858 (*nentries)++;
4863 /* Write new list of entries; deleted entries have been dropped. */
4864 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4865 done:
4866 got_pathlist_free(&paths);
4867 return err;
4870 static const struct got_error *
4871 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4872 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4873 int have_staged_files)
4875 const struct got_error *err = NULL;
4876 struct got_pathlist_entry *pe;
4878 TAILQ_FOREACH(pe, commitable_paths, entry) {
4879 struct got_fileindex_entry *ie;
4880 struct got_commitable *ct = pe->data;
4882 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4883 if (ie) {
4884 if (ct->status == GOT_STATUS_DELETE ||
4885 ct->staged_status == GOT_STATUS_DELETE) {
4886 got_fileindex_entry_remove(fileindex, ie);
4887 } else if (ct->staged_status == GOT_STATUS_ADD ||
4888 ct->staged_status == GOT_STATUS_MODIFY) {
4889 got_fileindex_entry_stage_set(ie,
4890 GOT_FILEIDX_STAGE_NONE);
4891 err = got_fileindex_entry_update(ie,
4892 ct->ondisk_path, ct->staged_blob_id->sha1,
4893 new_base_commit_id->sha1,
4894 !have_staged_files);
4895 } else
4896 err = got_fileindex_entry_update(ie,
4897 ct->ondisk_path, ct->blob_id->sha1,
4898 new_base_commit_id->sha1,
4899 !have_staged_files);
4900 } else {
4901 err = got_fileindex_entry_alloc(&ie, pe->path);
4902 if (err)
4903 break;
4904 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4905 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4906 if (err) {
4907 got_fileindex_entry_free(ie);
4908 break;
4910 err = got_fileindex_entry_add(fileindex, ie);
4911 if (err) {
4912 got_fileindex_entry_free(ie);
4913 break;
4917 return err;
4921 static const struct got_error *
4922 check_out_of_date(const char *in_repo_path, unsigned char status,
4923 unsigned char staged_status, struct got_object_id *base_blob_id,
4924 struct got_object_id *base_commit_id,
4925 struct got_object_id *head_commit_id, struct got_repository *repo,
4926 int ood_errcode)
4928 const struct got_error *err = NULL;
4929 struct got_object_id *id = NULL;
4931 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4932 /* Trivial case: base commit == head commit */
4933 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4934 return NULL;
4936 * Ensure file content which local changes were based
4937 * on matches file content in the branch head.
4939 err = got_object_id_by_path(&id, repo, head_commit_id,
4940 in_repo_path);
4941 if (err) {
4942 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4943 err = got_error(ood_errcode);
4944 goto done;
4945 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4946 err = got_error(ood_errcode);
4947 } else {
4948 /* Require that added files don't exist in the branch head. */
4949 err = got_object_id_by_path(&id, repo, head_commit_id,
4950 in_repo_path);
4951 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4952 goto done;
4953 err = id ? got_error(ood_errcode) : NULL;
4955 done:
4956 free(id);
4957 return err;
4960 const struct got_error *
4961 commit_worktree(struct got_object_id **new_commit_id,
4962 struct got_pathlist_head *commitable_paths,
4963 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4964 const char *author, const char *committer,
4965 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4966 got_worktree_status_cb status_cb, void *status_arg,
4967 struct got_repository *repo)
4969 const struct got_error *err = NULL, *unlockerr = NULL;
4970 struct got_pathlist_entry *pe;
4971 const char *head_ref_name = NULL;
4972 struct got_commit_object *head_commit = NULL;
4973 struct got_reference *head_ref2 = NULL;
4974 struct got_object_id *head_commit_id2 = NULL;
4975 struct got_tree_object *head_tree = NULL;
4976 struct got_object_id *new_tree_id = NULL;
4977 int nentries;
4978 struct got_object_id_queue parent_ids;
4979 struct got_object_qid *pid = NULL;
4980 char *logmsg = NULL;
4982 *new_commit_id = NULL;
4984 SIMPLEQ_INIT(&parent_ids);
4986 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4987 if (err)
4988 goto done;
4990 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4991 if (err)
4992 goto done;
4994 if (commit_msg_cb != NULL) {
4995 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4996 if (err)
4997 goto done;
5000 if (logmsg == NULL || strlen(logmsg) == 0) {
5001 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5002 goto done;
5005 /* Create blobs from added and modified files and record their IDs. */
5006 TAILQ_FOREACH(pe, commitable_paths, entry) {
5007 struct got_commitable *ct = pe->data;
5008 char *ondisk_path;
5010 /* Blobs for staged files already exist. */
5011 if (ct->staged_status == GOT_STATUS_ADD ||
5012 ct->staged_status == GOT_STATUS_MODIFY)
5013 continue;
5015 if (ct->status != GOT_STATUS_ADD &&
5016 ct->status != GOT_STATUS_MODIFY &&
5017 ct->status != GOT_STATUS_MODE_CHANGE)
5018 continue;
5020 if (asprintf(&ondisk_path, "%s/%s",
5021 worktree->root_path, pe->path) == -1) {
5022 err = got_error_from_errno("asprintf");
5023 goto done;
5025 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5026 if (err) {
5027 free(ondisk_path);
5028 goto done;
5032 * When comitting a symlink we convert "bad" symlinks (those
5033 * which point outside the work tree or into .got) to regular
5034 * files. This way, the post-commit work tree state matches
5035 * a fresh checkout of the tree which was committed.
5037 if (S_ISLNK(get_ct_file_mode(ct))) {
5038 struct got_blob_object *blob;
5039 err = got_object_open_as_blob(&blob, repo, ct->blob_id,
5040 PATH_MAX);
5041 if (err) {
5042 free(ondisk_path);
5043 goto done;
5045 err = install_symlink(worktree, ondisk_path, ct->path,
5046 get_ct_file_mode(ct), GOT_DEFAULT_FILE_MODE, blob,
5047 0, 0, repo, NULL, NULL);
5048 got_object_blob_close(blob);
5049 if (err) {
5050 free(ondisk_path);
5051 goto done;
5054 free(ondisk_path);
5057 /* Recursively write new tree objects. */
5058 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5059 commitable_paths, status_cb, status_arg, repo);
5060 if (err)
5061 goto done;
5063 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5064 if (err)
5065 goto done;
5066 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5067 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5068 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5069 got_object_qid_free(pid);
5070 if (logmsg != NULL)
5071 free(logmsg);
5072 if (err)
5073 goto done;
5075 /* Check if a concurrent commit to our branch has occurred. */
5076 head_ref_name = got_worktree_get_head_ref_name(worktree);
5077 if (head_ref_name == NULL) {
5078 err = got_error_from_errno("got_worktree_get_head_ref_name");
5079 goto done;
5081 /* Lock the reference here to prevent concurrent modification. */
5082 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5083 if (err)
5084 goto done;
5085 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5086 if (err)
5087 goto done;
5088 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5089 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5090 goto done;
5092 /* Update branch head in repository. */
5093 err = got_ref_change_ref(head_ref2, *new_commit_id);
5094 if (err)
5095 goto done;
5096 err = got_ref_write(head_ref2, repo);
5097 if (err)
5098 goto done;
5100 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5101 if (err)
5102 goto done;
5104 err = ref_base_commit(worktree, repo);
5105 if (err)
5106 goto done;
5107 done:
5108 if (head_tree)
5109 got_object_tree_close(head_tree);
5110 if (head_commit)
5111 got_object_commit_close(head_commit);
5112 free(head_commit_id2);
5113 if (head_ref2) {
5114 unlockerr = got_ref_unlock(head_ref2);
5115 if (unlockerr && err == NULL)
5116 err = unlockerr;
5117 got_ref_close(head_ref2);
5119 return err;
5122 static const struct got_error *
5123 check_path_is_commitable(const char *path,
5124 struct got_pathlist_head *commitable_paths)
5126 struct got_pathlist_entry *cpe = NULL;
5127 size_t path_len = strlen(path);
5129 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5130 struct got_commitable *ct = cpe->data;
5131 const char *ct_path = ct->path;
5133 while (ct_path[0] == '/')
5134 ct_path++;
5136 if (strcmp(path, ct_path) == 0 ||
5137 got_path_is_child(ct_path, path, path_len))
5138 break;
5141 if (cpe == NULL)
5142 return got_error_path(path, GOT_ERR_BAD_PATH);
5144 return NULL;
5147 static const struct got_error *
5148 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5150 int *have_staged_files = arg;
5152 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5153 *have_staged_files = 1;
5154 return got_error(GOT_ERR_CANCELLED);
5157 return NULL;
5160 static const struct got_error *
5161 check_non_staged_files(struct got_fileindex *fileindex,
5162 struct got_pathlist_head *paths)
5164 struct got_pathlist_entry *pe;
5165 struct got_fileindex_entry *ie;
5167 TAILQ_FOREACH(pe, paths, entry) {
5168 if (pe->path[0] == '\0')
5169 continue;
5170 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5171 if (ie == NULL)
5172 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5173 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5174 return got_error_path(pe->path,
5175 GOT_ERR_FILE_NOT_STAGED);
5178 return NULL;
5181 const struct got_error *
5182 got_worktree_commit(struct got_object_id **new_commit_id,
5183 struct got_worktree *worktree, struct got_pathlist_head *paths,
5184 const char *author, const char *committer,
5185 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5186 got_worktree_status_cb status_cb, void *status_arg,
5187 struct got_repository *repo)
5189 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5190 struct got_fileindex *fileindex = NULL;
5191 char *fileindex_path = NULL;
5192 struct got_pathlist_head commitable_paths;
5193 struct collect_commitables_arg cc_arg;
5194 struct got_pathlist_entry *pe;
5195 struct got_reference *head_ref = NULL;
5196 struct got_object_id *head_commit_id = NULL;
5197 int have_staged_files = 0;
5199 *new_commit_id = NULL;
5201 TAILQ_INIT(&commitable_paths);
5203 err = lock_worktree(worktree, LOCK_EX);
5204 if (err)
5205 goto done;
5207 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5208 if (err)
5209 goto done;
5211 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5212 if (err)
5213 goto done;
5215 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5216 if (err)
5217 goto done;
5219 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5220 &have_staged_files);
5221 if (err && err->code != GOT_ERR_CANCELLED)
5222 goto done;
5223 if (have_staged_files) {
5224 err = check_non_staged_files(fileindex, paths);
5225 if (err)
5226 goto done;
5229 cc_arg.commitable_paths = &commitable_paths;
5230 cc_arg.worktree = worktree;
5231 cc_arg.repo = repo;
5232 cc_arg.have_staged_files = have_staged_files;
5233 TAILQ_FOREACH(pe, paths, entry) {
5234 err = worktree_status(worktree, pe->path, fileindex, repo,
5235 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5236 if (err)
5237 goto done;
5240 if (TAILQ_EMPTY(&commitable_paths)) {
5241 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5242 goto done;
5245 TAILQ_FOREACH(pe, paths, entry) {
5246 err = check_path_is_commitable(pe->path, &commitable_paths);
5247 if (err)
5248 goto done;
5251 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5252 struct got_commitable *ct = pe->data;
5253 const char *ct_path = ct->in_repo_path;
5255 while (ct_path[0] == '/')
5256 ct_path++;
5257 err = check_out_of_date(ct_path, ct->status,
5258 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5259 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5260 if (err)
5261 goto done;
5265 err = commit_worktree(new_commit_id, &commitable_paths,
5266 head_commit_id, worktree, author, committer,
5267 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5268 if (err)
5269 goto done;
5271 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5272 fileindex, have_staged_files);
5273 sync_err = sync_fileindex(fileindex, fileindex_path);
5274 if (sync_err && err == NULL)
5275 err = sync_err;
5276 done:
5277 if (fileindex)
5278 got_fileindex_free(fileindex);
5279 free(fileindex_path);
5280 unlockerr = lock_worktree(worktree, LOCK_SH);
5281 if (unlockerr && err == NULL)
5282 err = unlockerr;
5283 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5284 struct got_commitable *ct = pe->data;
5285 free_commitable(ct);
5287 got_pathlist_free(&commitable_paths);
5288 return err;
5291 const char *
5292 got_commitable_get_path(struct got_commitable *ct)
5294 return ct->path;
5297 unsigned int
5298 got_commitable_get_status(struct got_commitable *ct)
5300 return ct->status;
5303 struct check_rebase_ok_arg {
5304 struct got_worktree *worktree;
5305 struct got_repository *repo;
5308 static const struct got_error *
5309 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5311 const struct got_error *err = NULL;
5312 struct check_rebase_ok_arg *a = arg;
5313 unsigned char status;
5314 struct stat sb;
5315 char *ondisk_path;
5317 /* Reject rebase of a work tree with mixed base commits. */
5318 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5319 SHA1_DIGEST_LENGTH))
5320 return got_error(GOT_ERR_MIXED_COMMITS);
5322 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5323 == -1)
5324 return got_error_from_errno("asprintf");
5326 /* Reject rebase of a work tree with modified or staged files. */
5327 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5328 free(ondisk_path);
5329 if (err)
5330 return err;
5332 if (status != GOT_STATUS_NO_CHANGE)
5333 return got_error(GOT_ERR_MODIFIED);
5334 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5335 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5337 return NULL;
5340 const struct got_error *
5341 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5342 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5343 struct got_worktree *worktree, struct got_reference *branch,
5344 struct got_repository *repo)
5346 const struct got_error *err = NULL;
5347 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5348 char *branch_ref_name = NULL;
5349 char *fileindex_path = NULL;
5350 struct check_rebase_ok_arg ok_arg;
5351 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5352 struct got_object_id *wt_branch_tip = NULL;
5354 *new_base_branch_ref = NULL;
5355 *tmp_branch = NULL;
5356 *fileindex = NULL;
5358 err = lock_worktree(worktree, LOCK_EX);
5359 if (err)
5360 return err;
5362 err = open_fileindex(fileindex, &fileindex_path, worktree);
5363 if (err)
5364 goto done;
5366 ok_arg.worktree = worktree;
5367 ok_arg.repo = repo;
5368 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5369 &ok_arg);
5370 if (err)
5371 goto done;
5373 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5374 if (err)
5375 goto done;
5377 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5378 if (err)
5379 goto done;
5381 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5382 if (err)
5383 goto done;
5385 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5386 0);
5387 if (err)
5388 goto done;
5390 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5391 if (err)
5392 goto done;
5393 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5394 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5395 goto done;
5398 err = got_ref_alloc_symref(new_base_branch_ref,
5399 new_base_branch_ref_name, wt_branch);
5400 if (err)
5401 goto done;
5402 err = got_ref_write(*new_base_branch_ref, repo);
5403 if (err)
5404 goto done;
5406 /* TODO Lock original branch's ref while rebasing? */
5408 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5409 if (err)
5410 goto done;
5412 err = got_ref_write(branch_ref, repo);
5413 if (err)
5414 goto done;
5416 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5417 worktree->base_commit_id);
5418 if (err)
5419 goto done;
5420 err = got_ref_write(*tmp_branch, repo);
5421 if (err)
5422 goto done;
5424 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5425 if (err)
5426 goto done;
5427 done:
5428 free(fileindex_path);
5429 free(tmp_branch_name);
5430 free(new_base_branch_ref_name);
5431 free(branch_ref_name);
5432 if (branch_ref)
5433 got_ref_close(branch_ref);
5434 if (wt_branch)
5435 got_ref_close(wt_branch);
5436 free(wt_branch_tip);
5437 if (err) {
5438 if (*new_base_branch_ref) {
5439 got_ref_close(*new_base_branch_ref);
5440 *new_base_branch_ref = NULL;
5442 if (*tmp_branch) {
5443 got_ref_close(*tmp_branch);
5444 *tmp_branch = NULL;
5446 if (*fileindex) {
5447 got_fileindex_free(*fileindex);
5448 *fileindex = NULL;
5450 lock_worktree(worktree, LOCK_SH);
5452 return err;
5455 const struct got_error *
5456 got_worktree_rebase_continue(struct got_object_id **commit_id,
5457 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5458 struct got_reference **branch, struct got_fileindex **fileindex,
5459 struct got_worktree *worktree, struct got_repository *repo)
5461 const struct got_error *err;
5462 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5463 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5464 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5465 char *fileindex_path = NULL;
5466 int have_staged_files = 0;
5468 *commit_id = NULL;
5469 *new_base_branch = NULL;
5470 *tmp_branch = NULL;
5471 *branch = NULL;
5472 *fileindex = NULL;
5474 err = lock_worktree(worktree, LOCK_EX);
5475 if (err)
5476 return err;
5478 err = open_fileindex(fileindex, &fileindex_path, worktree);
5479 if (err)
5480 goto done;
5482 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5483 &have_staged_files);
5484 if (err && err->code != GOT_ERR_CANCELLED)
5485 goto done;
5486 if (have_staged_files) {
5487 err = got_error(GOT_ERR_STAGED_PATHS);
5488 goto done;
5491 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5492 if (err)
5493 goto done;
5495 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5496 if (err)
5497 goto done;
5499 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5500 if (err)
5501 goto done;
5503 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5504 if (err)
5505 goto done;
5507 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5508 if (err)
5509 goto done;
5511 err = got_ref_open(branch, repo,
5512 got_ref_get_symref_target(branch_ref), 0);
5513 if (err)
5514 goto done;
5516 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5517 if (err)
5518 goto done;
5520 err = got_ref_resolve(commit_id, repo, commit_ref);
5521 if (err)
5522 goto done;
5524 err = got_ref_open(new_base_branch, repo,
5525 new_base_branch_ref_name, 0);
5526 if (err)
5527 goto done;
5529 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5530 if (err)
5531 goto done;
5532 done:
5533 free(commit_ref_name);
5534 free(branch_ref_name);
5535 free(fileindex_path);
5536 if (commit_ref)
5537 got_ref_close(commit_ref);
5538 if (branch_ref)
5539 got_ref_close(branch_ref);
5540 if (err) {
5541 free(*commit_id);
5542 *commit_id = NULL;
5543 if (*tmp_branch) {
5544 got_ref_close(*tmp_branch);
5545 *tmp_branch = NULL;
5547 if (*new_base_branch) {
5548 got_ref_close(*new_base_branch);
5549 *new_base_branch = NULL;
5551 if (*branch) {
5552 got_ref_close(*branch);
5553 *branch = NULL;
5555 if (*fileindex) {
5556 got_fileindex_free(*fileindex);
5557 *fileindex = NULL;
5559 lock_worktree(worktree, LOCK_SH);
5561 return err;
5564 const struct got_error *
5565 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5567 const struct got_error *err;
5568 char *tmp_branch_name = NULL;
5570 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5571 if (err)
5572 return err;
5574 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5575 free(tmp_branch_name);
5576 return NULL;
5579 static const struct got_error *
5580 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5581 char **logmsg, void *arg)
5583 *logmsg = arg;
5584 return NULL;
5587 static const struct got_error *
5588 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5589 const char *path, struct got_object_id *blob_id,
5590 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5591 int dirfd, const char *de_name)
5593 return NULL;
5596 struct collect_merged_paths_arg {
5597 got_worktree_checkout_cb progress_cb;
5598 void *progress_arg;
5599 struct got_pathlist_head *merged_paths;
5602 static const struct got_error *
5603 collect_merged_paths(void *arg, unsigned char status, const char *path)
5605 const struct got_error *err;
5606 struct collect_merged_paths_arg *a = arg;
5607 char *p;
5608 struct got_pathlist_entry *new;
5610 err = (*a->progress_cb)(a->progress_arg, status, path);
5611 if (err)
5612 return err;
5614 if (status != GOT_STATUS_MERGE &&
5615 status != GOT_STATUS_ADD &&
5616 status != GOT_STATUS_DELETE &&
5617 status != GOT_STATUS_CONFLICT)
5618 return NULL;
5620 p = strdup(path);
5621 if (p == NULL)
5622 return got_error_from_errno("strdup");
5624 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5625 if (err || new == NULL)
5626 free(p);
5627 return err;
5630 void
5631 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5633 struct got_pathlist_entry *pe;
5635 TAILQ_FOREACH(pe, merged_paths, entry)
5636 free((char *)pe->path);
5638 got_pathlist_free(merged_paths);
5641 static const struct got_error *
5642 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5643 int is_rebase, struct got_repository *repo)
5645 const struct got_error *err;
5646 struct got_reference *commit_ref = NULL;
5648 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5649 if (err) {
5650 if (err->code != GOT_ERR_NOT_REF)
5651 goto done;
5652 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5653 if (err)
5654 goto done;
5655 err = got_ref_write(commit_ref, repo);
5656 if (err)
5657 goto done;
5658 } else if (is_rebase) {
5659 struct got_object_id *stored_id;
5660 int cmp;
5662 err = got_ref_resolve(&stored_id, repo, commit_ref);
5663 if (err)
5664 goto done;
5665 cmp = got_object_id_cmp(commit_id, stored_id);
5666 free(stored_id);
5667 if (cmp != 0) {
5668 err = got_error(GOT_ERR_REBASE_COMMITID);
5669 goto done;
5672 done:
5673 if (commit_ref)
5674 got_ref_close(commit_ref);
5675 return err;
5678 static const struct got_error *
5679 rebase_merge_files(struct got_pathlist_head *merged_paths,
5680 const char *commit_ref_name, struct got_worktree *worktree,
5681 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5682 struct got_object_id *commit_id, struct got_repository *repo,
5683 got_worktree_checkout_cb progress_cb, void *progress_arg,
5684 got_cancel_cb cancel_cb, void *cancel_arg)
5686 const struct got_error *err;
5687 struct got_reference *commit_ref = NULL;
5688 struct collect_merged_paths_arg cmp_arg;
5689 char *fileindex_path;
5691 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5693 err = get_fileindex_path(&fileindex_path, worktree);
5694 if (err)
5695 return err;
5697 cmp_arg.progress_cb = progress_cb;
5698 cmp_arg.progress_arg = progress_arg;
5699 cmp_arg.merged_paths = merged_paths;
5700 err = merge_files(worktree, fileindex, fileindex_path,
5701 parent_commit_id, commit_id, repo, collect_merged_paths,
5702 &cmp_arg, cancel_cb, cancel_arg);
5703 if (commit_ref)
5704 got_ref_close(commit_ref);
5705 return err;
5708 const struct got_error *
5709 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5710 struct got_worktree *worktree, struct got_fileindex *fileindex,
5711 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5712 struct got_repository *repo,
5713 got_worktree_checkout_cb progress_cb, void *progress_arg,
5714 got_cancel_cb cancel_cb, void *cancel_arg)
5716 const struct got_error *err;
5717 char *commit_ref_name;
5719 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5720 if (err)
5721 return err;
5723 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5724 if (err)
5725 goto done;
5727 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5728 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5729 progress_arg, cancel_cb, cancel_arg);
5730 done:
5731 free(commit_ref_name);
5732 return err;
5735 const struct got_error *
5736 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5737 struct got_worktree *worktree, struct got_fileindex *fileindex,
5738 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5739 struct got_repository *repo,
5740 got_worktree_checkout_cb progress_cb, void *progress_arg,
5741 got_cancel_cb cancel_cb, void *cancel_arg)
5743 const struct got_error *err;
5744 char *commit_ref_name;
5746 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5747 if (err)
5748 return err;
5750 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5751 if (err)
5752 goto done;
5754 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5755 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5756 progress_arg, cancel_cb, cancel_arg);
5757 done:
5758 free(commit_ref_name);
5759 return err;
5762 static const struct got_error *
5763 rebase_commit(struct got_object_id **new_commit_id,
5764 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5765 struct got_worktree *worktree, struct got_fileindex *fileindex,
5766 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5767 const char *new_logmsg, struct got_repository *repo)
5769 const struct got_error *err, *sync_err;
5770 struct got_pathlist_head commitable_paths;
5771 struct collect_commitables_arg cc_arg;
5772 char *fileindex_path = NULL;
5773 struct got_reference *head_ref = NULL;
5774 struct got_object_id *head_commit_id = NULL;
5775 char *logmsg = NULL;
5777 TAILQ_INIT(&commitable_paths);
5778 *new_commit_id = NULL;
5780 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5782 err = get_fileindex_path(&fileindex_path, worktree);
5783 if (err)
5784 return err;
5786 cc_arg.commitable_paths = &commitable_paths;
5787 cc_arg.worktree = worktree;
5788 cc_arg.repo = repo;
5789 cc_arg.have_staged_files = 0;
5791 * If possible get the status of individual files directly to
5792 * avoid crawling the entire work tree once per rebased commit.
5793 * TODO: Ideally, merged_paths would contain a list of commitables
5794 * we could use so we could skip worktree_status() entirely.
5796 if (merged_paths) {
5797 struct got_pathlist_entry *pe;
5798 TAILQ_FOREACH(pe, merged_paths, entry) {
5799 err = worktree_status(worktree, pe->path, fileindex,
5800 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5801 0);
5802 if (err)
5803 goto done;
5805 } else {
5806 err = worktree_status(worktree, "", fileindex, repo,
5807 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5808 if (err)
5809 goto done;
5812 if (TAILQ_EMPTY(&commitable_paths)) {
5813 /* No-op change; commit will be elided. */
5814 err = got_ref_delete(commit_ref, repo);
5815 if (err)
5816 goto done;
5817 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5818 goto done;
5821 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5822 if (err)
5823 goto done;
5825 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5826 if (err)
5827 goto done;
5829 if (new_logmsg) {
5830 logmsg = strdup(new_logmsg);
5831 if (logmsg == NULL) {
5832 err = got_error_from_errno("strdup");
5833 goto done;
5835 } else {
5836 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5837 if (err)
5838 goto done;
5841 /* NB: commit_worktree will call free(logmsg) */
5842 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5843 worktree, got_object_commit_get_author(orig_commit),
5844 got_object_commit_get_committer(orig_commit),
5845 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5846 if (err)
5847 goto done;
5849 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5850 if (err)
5851 goto done;
5853 err = got_ref_delete(commit_ref, repo);
5854 if (err)
5855 goto done;
5857 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5858 fileindex, 0);
5859 sync_err = sync_fileindex(fileindex, fileindex_path);
5860 if (sync_err && err == NULL)
5861 err = sync_err;
5862 done:
5863 free(fileindex_path);
5864 free(head_commit_id);
5865 if (head_ref)
5866 got_ref_close(head_ref);
5867 if (err) {
5868 free(*new_commit_id);
5869 *new_commit_id = NULL;
5871 return err;
5874 const struct got_error *
5875 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5876 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5877 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5878 struct got_commit_object *orig_commit,
5879 struct got_object_id *orig_commit_id, struct got_repository *repo)
5881 const struct got_error *err;
5882 char *commit_ref_name;
5883 struct got_reference *commit_ref = NULL;
5884 struct got_object_id *commit_id = NULL;
5886 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5887 if (err)
5888 return err;
5890 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5891 if (err)
5892 goto done;
5893 err = got_ref_resolve(&commit_id, repo, commit_ref);
5894 if (err)
5895 goto done;
5896 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5897 err = got_error(GOT_ERR_REBASE_COMMITID);
5898 goto done;
5901 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5902 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5903 done:
5904 if (commit_ref)
5905 got_ref_close(commit_ref);
5906 free(commit_ref_name);
5907 free(commit_id);
5908 return err;
5911 const struct got_error *
5912 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5913 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5914 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5915 struct got_commit_object *orig_commit,
5916 struct got_object_id *orig_commit_id, const char *new_logmsg,
5917 struct got_repository *repo)
5919 const struct got_error *err;
5920 char *commit_ref_name;
5921 struct got_reference *commit_ref = NULL;
5923 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5924 if (err)
5925 return err;
5927 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5928 if (err)
5929 goto done;
5931 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5932 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5933 done:
5934 if (commit_ref)
5935 got_ref_close(commit_ref);
5936 free(commit_ref_name);
5937 return err;
5940 const struct got_error *
5941 got_worktree_rebase_postpone(struct got_worktree *worktree,
5942 struct got_fileindex *fileindex)
5944 if (fileindex)
5945 got_fileindex_free(fileindex);
5946 return lock_worktree(worktree, LOCK_SH);
5949 static const struct got_error *
5950 delete_ref(const char *name, struct got_repository *repo)
5952 const struct got_error *err;
5953 struct got_reference *ref;
5955 err = got_ref_open(&ref, repo, name, 0);
5956 if (err) {
5957 if (err->code == GOT_ERR_NOT_REF)
5958 return NULL;
5959 return err;
5962 err = got_ref_delete(ref, repo);
5963 got_ref_close(ref);
5964 return err;
5967 static const struct got_error *
5968 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5970 const struct got_error *err;
5971 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5972 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5974 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5975 if (err)
5976 goto done;
5977 err = delete_ref(tmp_branch_name, repo);
5978 if (err)
5979 goto done;
5981 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5982 if (err)
5983 goto done;
5984 err = delete_ref(new_base_branch_ref_name, repo);
5985 if (err)
5986 goto done;
5988 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5989 if (err)
5990 goto done;
5991 err = delete_ref(branch_ref_name, repo);
5992 if (err)
5993 goto done;
5995 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5996 if (err)
5997 goto done;
5998 err = delete_ref(commit_ref_name, repo);
5999 if (err)
6000 goto done;
6002 done:
6003 free(tmp_branch_name);
6004 free(new_base_branch_ref_name);
6005 free(branch_ref_name);
6006 free(commit_ref_name);
6007 return err;
6010 const struct got_error *
6011 got_worktree_rebase_complete(struct got_worktree *worktree,
6012 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6013 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6014 struct got_repository *repo)
6016 const struct got_error *err, *unlockerr;
6017 struct got_object_id *new_head_commit_id = NULL;
6019 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6020 if (err)
6021 return err;
6023 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6024 if (err)
6025 goto done;
6027 err = got_ref_write(rebased_branch, repo);
6028 if (err)
6029 goto done;
6031 err = got_worktree_set_head_ref(worktree, rebased_branch);
6032 if (err)
6033 goto done;
6035 err = delete_rebase_refs(worktree, repo);
6036 done:
6037 if (fileindex)
6038 got_fileindex_free(fileindex);
6039 free(new_head_commit_id);
6040 unlockerr = lock_worktree(worktree, LOCK_SH);
6041 if (unlockerr && err == NULL)
6042 err = unlockerr;
6043 return err;
6046 const struct got_error *
6047 got_worktree_rebase_abort(struct got_worktree *worktree,
6048 struct got_fileindex *fileindex, struct got_repository *repo,
6049 struct got_reference *new_base_branch,
6050 got_worktree_checkout_cb progress_cb, void *progress_arg)
6052 const struct got_error *err, *unlockerr, *sync_err;
6053 struct got_reference *resolved = NULL;
6054 struct got_object_id *commit_id = NULL;
6055 char *fileindex_path = NULL;
6056 struct revert_file_args rfa;
6057 struct got_object_id *tree_id = NULL;
6059 err = lock_worktree(worktree, LOCK_EX);
6060 if (err)
6061 return err;
6063 err = got_ref_open(&resolved, repo,
6064 got_ref_get_symref_target(new_base_branch), 0);
6065 if (err)
6066 goto done;
6068 err = got_worktree_set_head_ref(worktree, resolved);
6069 if (err)
6070 goto done;
6073 * XXX commits to the base branch could have happened while
6074 * we were busy rebasing; should we store the original commit ID
6075 * when rebase begins and read it back here?
6077 err = got_ref_resolve(&commit_id, repo, resolved);
6078 if (err)
6079 goto done;
6081 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6082 if (err)
6083 goto done;
6085 err = got_object_id_by_path(&tree_id, repo,
6086 worktree->base_commit_id, worktree->path_prefix);
6087 if (err)
6088 goto done;
6090 err = delete_rebase_refs(worktree, repo);
6091 if (err)
6092 goto done;
6094 err = get_fileindex_path(&fileindex_path, worktree);
6095 if (err)
6096 goto done;
6098 rfa.worktree = worktree;
6099 rfa.fileindex = fileindex;
6100 rfa.progress_cb = progress_cb;
6101 rfa.progress_arg = progress_arg;
6102 rfa.patch_cb = NULL;
6103 rfa.patch_arg = NULL;
6104 rfa.repo = repo;
6105 err = worktree_status(worktree, "", fileindex, repo,
6106 revert_file, &rfa, NULL, NULL, 0, 0);
6107 if (err)
6108 goto sync;
6110 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6111 repo, progress_cb, progress_arg, NULL, NULL);
6112 sync:
6113 sync_err = sync_fileindex(fileindex, fileindex_path);
6114 if (sync_err && err == NULL)
6115 err = sync_err;
6116 done:
6117 got_ref_close(resolved);
6118 free(tree_id);
6119 free(commit_id);
6120 if (fileindex)
6121 got_fileindex_free(fileindex);
6122 free(fileindex_path);
6124 unlockerr = lock_worktree(worktree, LOCK_SH);
6125 if (unlockerr && err == NULL)
6126 err = unlockerr;
6127 return err;
6130 const struct got_error *
6131 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6132 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6133 struct got_fileindex **fileindex, struct got_worktree *worktree,
6134 struct got_repository *repo)
6136 const struct got_error *err = NULL;
6137 char *tmp_branch_name = NULL;
6138 char *branch_ref_name = NULL;
6139 char *base_commit_ref_name = NULL;
6140 char *fileindex_path = NULL;
6141 struct check_rebase_ok_arg ok_arg;
6142 struct got_reference *wt_branch = NULL;
6143 struct got_reference *base_commit_ref = NULL;
6145 *tmp_branch = NULL;
6146 *branch_ref = NULL;
6147 *base_commit_id = NULL;
6148 *fileindex = NULL;
6150 err = lock_worktree(worktree, LOCK_EX);
6151 if (err)
6152 return err;
6154 err = open_fileindex(fileindex, &fileindex_path, worktree);
6155 if (err)
6156 goto done;
6158 ok_arg.worktree = worktree;
6159 ok_arg.repo = repo;
6160 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6161 &ok_arg);
6162 if (err)
6163 goto done;
6165 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6166 if (err)
6167 goto done;
6169 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6170 if (err)
6171 goto done;
6173 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6174 worktree);
6175 if (err)
6176 goto done;
6178 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6179 0);
6180 if (err)
6181 goto done;
6183 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6184 if (err)
6185 goto done;
6187 err = got_ref_write(*branch_ref, repo);
6188 if (err)
6189 goto done;
6191 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6192 worktree->base_commit_id);
6193 if (err)
6194 goto done;
6195 err = got_ref_write(base_commit_ref, repo);
6196 if (err)
6197 goto done;
6198 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6199 if (*base_commit_id == NULL) {
6200 err = got_error_from_errno("got_object_id_dup");
6201 goto done;
6204 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6205 worktree->base_commit_id);
6206 if (err)
6207 goto done;
6208 err = got_ref_write(*tmp_branch, repo);
6209 if (err)
6210 goto done;
6212 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6213 if (err)
6214 goto done;
6215 done:
6216 free(fileindex_path);
6217 free(tmp_branch_name);
6218 free(branch_ref_name);
6219 free(base_commit_ref_name);
6220 if (wt_branch)
6221 got_ref_close(wt_branch);
6222 if (err) {
6223 if (*branch_ref) {
6224 got_ref_close(*branch_ref);
6225 *branch_ref = NULL;
6227 if (*tmp_branch) {
6228 got_ref_close(*tmp_branch);
6229 *tmp_branch = NULL;
6231 free(*base_commit_id);
6232 if (*fileindex) {
6233 got_fileindex_free(*fileindex);
6234 *fileindex = NULL;
6236 lock_worktree(worktree, LOCK_SH);
6238 return err;
6241 const struct got_error *
6242 got_worktree_histedit_postpone(struct got_worktree *worktree,
6243 struct got_fileindex *fileindex)
6245 if (fileindex)
6246 got_fileindex_free(fileindex);
6247 return lock_worktree(worktree, LOCK_SH);
6250 const struct got_error *
6251 got_worktree_histedit_in_progress(int *in_progress,
6252 struct got_worktree *worktree)
6254 const struct got_error *err;
6255 char *tmp_branch_name = NULL;
6257 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6258 if (err)
6259 return err;
6261 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6262 free(tmp_branch_name);
6263 return NULL;
6266 const struct got_error *
6267 got_worktree_histedit_continue(struct got_object_id **commit_id,
6268 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6269 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6270 struct got_worktree *worktree, struct got_repository *repo)
6272 const struct got_error *err;
6273 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6274 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6275 struct got_reference *commit_ref = NULL;
6276 struct got_reference *base_commit_ref = NULL;
6277 char *fileindex_path = NULL;
6278 int have_staged_files = 0;
6280 *commit_id = NULL;
6281 *tmp_branch = NULL;
6282 *base_commit_id = NULL;
6283 *fileindex = NULL;
6285 err = lock_worktree(worktree, LOCK_EX);
6286 if (err)
6287 return err;
6289 err = open_fileindex(fileindex, &fileindex_path, worktree);
6290 if (err)
6291 goto done;
6293 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6294 &have_staged_files);
6295 if (err && err->code != GOT_ERR_CANCELLED)
6296 goto done;
6297 if (have_staged_files) {
6298 err = got_error(GOT_ERR_STAGED_PATHS);
6299 goto done;
6302 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6303 if (err)
6304 goto done;
6306 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6307 if (err)
6308 goto done;
6310 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6311 if (err)
6312 goto done;
6314 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6315 worktree);
6316 if (err)
6317 goto done;
6319 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6320 if (err)
6321 goto done;
6323 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6324 if (err)
6325 goto done;
6326 err = got_ref_resolve(commit_id, repo, commit_ref);
6327 if (err)
6328 goto done;
6330 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6331 if (err)
6332 goto done;
6333 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6334 if (err)
6335 goto done;
6337 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6338 if (err)
6339 goto done;
6340 done:
6341 free(commit_ref_name);
6342 free(branch_ref_name);
6343 free(fileindex_path);
6344 if (commit_ref)
6345 got_ref_close(commit_ref);
6346 if (base_commit_ref)
6347 got_ref_close(base_commit_ref);
6348 if (err) {
6349 free(*commit_id);
6350 *commit_id = NULL;
6351 free(*base_commit_id);
6352 *base_commit_id = NULL;
6353 if (*tmp_branch) {
6354 got_ref_close(*tmp_branch);
6355 *tmp_branch = NULL;
6357 if (*fileindex) {
6358 got_fileindex_free(*fileindex);
6359 *fileindex = NULL;
6361 lock_worktree(worktree, LOCK_EX);
6363 return err;
6366 static const struct got_error *
6367 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6369 const struct got_error *err;
6370 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6371 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6373 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6374 if (err)
6375 goto done;
6376 err = delete_ref(tmp_branch_name, repo);
6377 if (err)
6378 goto done;
6380 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6381 worktree);
6382 if (err)
6383 goto done;
6384 err = delete_ref(base_commit_ref_name, repo);
6385 if (err)
6386 goto done;
6388 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6389 if (err)
6390 goto done;
6391 err = delete_ref(branch_ref_name, repo);
6392 if (err)
6393 goto done;
6395 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6396 if (err)
6397 goto done;
6398 err = delete_ref(commit_ref_name, repo);
6399 if (err)
6400 goto done;
6401 done:
6402 free(tmp_branch_name);
6403 free(base_commit_ref_name);
6404 free(branch_ref_name);
6405 free(commit_ref_name);
6406 return err;
6409 const struct got_error *
6410 got_worktree_histedit_abort(struct got_worktree *worktree,
6411 struct got_fileindex *fileindex, struct got_repository *repo,
6412 struct got_reference *branch, struct got_object_id *base_commit_id,
6413 got_worktree_checkout_cb progress_cb, void *progress_arg)
6415 const struct got_error *err, *unlockerr, *sync_err;
6416 struct got_reference *resolved = NULL;
6417 char *fileindex_path = NULL;
6418 struct got_object_id *tree_id = NULL;
6419 struct revert_file_args rfa;
6421 err = lock_worktree(worktree, LOCK_EX);
6422 if (err)
6423 return err;
6425 err = got_ref_open(&resolved, repo,
6426 got_ref_get_symref_target(branch), 0);
6427 if (err)
6428 goto done;
6430 err = got_worktree_set_head_ref(worktree, resolved);
6431 if (err)
6432 goto done;
6434 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6435 if (err)
6436 goto done;
6438 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6439 worktree->path_prefix);
6440 if (err)
6441 goto done;
6443 err = delete_histedit_refs(worktree, repo);
6444 if (err)
6445 goto done;
6447 err = get_fileindex_path(&fileindex_path, worktree);
6448 if (err)
6449 goto done;
6451 rfa.worktree = worktree;
6452 rfa.fileindex = fileindex;
6453 rfa.progress_cb = progress_cb;
6454 rfa.progress_arg = progress_arg;
6455 rfa.patch_cb = NULL;
6456 rfa.patch_arg = NULL;
6457 rfa.repo = repo;
6458 err = worktree_status(worktree, "", fileindex, repo,
6459 revert_file, &rfa, NULL, NULL, 0, 0);
6460 if (err)
6461 goto sync;
6463 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6464 repo, progress_cb, progress_arg, NULL, NULL);
6465 sync:
6466 sync_err = sync_fileindex(fileindex, fileindex_path);
6467 if (sync_err && err == NULL)
6468 err = sync_err;
6469 done:
6470 got_ref_close(resolved);
6471 free(tree_id);
6472 free(fileindex_path);
6474 unlockerr = lock_worktree(worktree, LOCK_SH);
6475 if (unlockerr && err == NULL)
6476 err = unlockerr;
6477 return err;
6480 const struct got_error *
6481 got_worktree_histedit_complete(struct got_worktree *worktree,
6482 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6483 struct got_reference *edited_branch, struct got_repository *repo)
6485 const struct got_error *err, *unlockerr;
6486 struct got_object_id *new_head_commit_id = NULL;
6487 struct got_reference *resolved = NULL;
6489 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6490 if (err)
6491 return err;
6493 err = got_ref_open(&resolved, repo,
6494 got_ref_get_symref_target(edited_branch), 0);
6495 if (err)
6496 goto done;
6498 err = got_ref_change_ref(resolved, new_head_commit_id);
6499 if (err)
6500 goto done;
6502 err = got_ref_write(resolved, repo);
6503 if (err)
6504 goto done;
6506 err = got_worktree_set_head_ref(worktree, resolved);
6507 if (err)
6508 goto done;
6510 err = delete_histedit_refs(worktree, repo);
6511 done:
6512 if (fileindex)
6513 got_fileindex_free(fileindex);
6514 free(new_head_commit_id);
6515 unlockerr = lock_worktree(worktree, LOCK_SH);
6516 if (unlockerr && err == NULL)
6517 err = unlockerr;
6518 return err;
6521 const struct got_error *
6522 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6523 struct got_object_id *commit_id, struct got_repository *repo)
6525 const struct got_error *err;
6526 char *commit_ref_name;
6528 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6529 if (err)
6530 return err;
6532 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6533 if (err)
6534 goto done;
6536 err = delete_ref(commit_ref_name, repo);
6537 done:
6538 free(commit_ref_name);
6539 return err;
6542 const struct got_error *
6543 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6544 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6545 struct got_worktree *worktree, const char *refname,
6546 struct got_repository *repo)
6548 const struct got_error *err = NULL;
6549 char *fileindex_path = NULL;
6550 struct check_rebase_ok_arg ok_arg;
6552 *fileindex = NULL;
6553 *branch_ref = NULL;
6554 *base_branch_ref = NULL;
6556 err = lock_worktree(worktree, LOCK_EX);
6557 if (err)
6558 return err;
6560 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6561 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6562 "cannot integrate a branch into itself; "
6563 "update -b or different branch name required");
6564 goto done;
6567 err = open_fileindex(fileindex, &fileindex_path, worktree);
6568 if (err)
6569 goto done;
6571 /* Preconditions are the same as for rebase. */
6572 ok_arg.worktree = worktree;
6573 ok_arg.repo = repo;
6574 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6575 &ok_arg);
6576 if (err)
6577 goto done;
6579 err = got_ref_open(branch_ref, repo, refname, 1);
6580 if (err)
6581 goto done;
6583 err = got_ref_open(base_branch_ref, repo,
6584 got_worktree_get_head_ref_name(worktree), 1);
6585 done:
6586 if (err) {
6587 if (*branch_ref) {
6588 got_ref_close(*branch_ref);
6589 *branch_ref = NULL;
6591 if (*base_branch_ref) {
6592 got_ref_close(*base_branch_ref);
6593 *base_branch_ref = NULL;
6595 if (*fileindex) {
6596 got_fileindex_free(*fileindex);
6597 *fileindex = NULL;
6599 lock_worktree(worktree, LOCK_SH);
6601 return err;
6604 const struct got_error *
6605 got_worktree_integrate_continue(struct got_worktree *worktree,
6606 struct got_fileindex *fileindex, struct got_repository *repo,
6607 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6608 got_worktree_checkout_cb progress_cb, void *progress_arg,
6609 got_cancel_cb cancel_cb, void *cancel_arg)
6611 const struct got_error *err = NULL, *sync_err, *unlockerr;
6612 char *fileindex_path = NULL;
6613 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6615 err = get_fileindex_path(&fileindex_path, worktree);
6616 if (err)
6617 goto done;
6619 err = got_ref_resolve(&commit_id, repo, branch_ref);
6620 if (err)
6621 goto done;
6623 err = got_object_id_by_path(&tree_id, repo, commit_id,
6624 worktree->path_prefix);
6625 if (err)
6626 goto done;
6628 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6629 if (err)
6630 goto done;
6632 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6633 progress_cb, progress_arg, cancel_cb, cancel_arg);
6634 if (err)
6635 goto sync;
6637 err = got_ref_change_ref(base_branch_ref, commit_id);
6638 if (err)
6639 goto sync;
6641 err = got_ref_write(base_branch_ref, repo);
6642 sync:
6643 sync_err = sync_fileindex(fileindex, fileindex_path);
6644 if (sync_err && err == NULL)
6645 err = sync_err;
6647 done:
6648 unlockerr = got_ref_unlock(branch_ref);
6649 if (unlockerr && err == NULL)
6650 err = unlockerr;
6651 got_ref_close(branch_ref);
6653 unlockerr = got_ref_unlock(base_branch_ref);
6654 if (unlockerr && err == NULL)
6655 err = unlockerr;
6656 got_ref_close(base_branch_ref);
6658 got_fileindex_free(fileindex);
6659 free(fileindex_path);
6660 free(tree_id);
6662 unlockerr = lock_worktree(worktree, LOCK_SH);
6663 if (unlockerr && err == NULL)
6664 err = unlockerr;
6665 return err;
6668 const struct got_error *
6669 got_worktree_integrate_abort(struct got_worktree *worktree,
6670 struct got_fileindex *fileindex, struct got_repository *repo,
6671 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6673 const struct got_error *err = NULL, *unlockerr = NULL;
6675 got_fileindex_free(fileindex);
6677 err = lock_worktree(worktree, LOCK_SH);
6679 unlockerr = got_ref_unlock(branch_ref);
6680 if (unlockerr && err == NULL)
6681 err = unlockerr;
6682 got_ref_close(branch_ref);
6684 unlockerr = got_ref_unlock(base_branch_ref);
6685 if (unlockerr && err == NULL)
6686 err = unlockerr;
6687 got_ref_close(base_branch_ref);
6689 return err;
6692 struct check_stage_ok_arg {
6693 struct got_object_id *head_commit_id;
6694 struct got_worktree *worktree;
6695 struct got_fileindex *fileindex;
6696 struct got_repository *repo;
6697 int have_changes;
6700 const struct got_error *
6701 check_stage_ok(void *arg, unsigned char status,
6702 unsigned char staged_status, const char *relpath,
6703 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6704 struct got_object_id *commit_id, int dirfd, const char *de_name)
6706 struct check_stage_ok_arg *a = arg;
6707 const struct got_error *err = NULL;
6708 struct got_fileindex_entry *ie;
6709 struct got_object_id base_commit_id;
6710 struct got_object_id *base_commit_idp = NULL;
6711 char *in_repo_path = NULL, *p;
6713 if (status == GOT_STATUS_UNVERSIONED ||
6714 status == GOT_STATUS_NO_CHANGE)
6715 return NULL;
6716 if (status == GOT_STATUS_NONEXISTENT)
6717 return got_error_set_errno(ENOENT, relpath);
6719 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6720 if (ie == NULL)
6721 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6723 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6724 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6725 relpath) == -1)
6726 return got_error_from_errno("asprintf");
6728 if (got_fileindex_entry_has_commit(ie)) {
6729 memcpy(base_commit_id.sha1, ie->commit_sha1,
6730 SHA1_DIGEST_LENGTH);
6731 base_commit_idp = &base_commit_id;
6734 if (status == GOT_STATUS_CONFLICT) {
6735 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6736 goto done;
6737 } else if (status != GOT_STATUS_ADD &&
6738 status != GOT_STATUS_MODIFY &&
6739 status != GOT_STATUS_DELETE) {
6740 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6741 goto done;
6744 a->have_changes = 1;
6746 p = in_repo_path;
6747 while (p[0] == '/')
6748 p++;
6749 err = check_out_of_date(p, status, staged_status,
6750 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6751 GOT_ERR_STAGE_OUT_OF_DATE);
6752 done:
6753 free(in_repo_path);
6754 return err;
6757 struct stage_path_arg {
6758 struct got_worktree *worktree;
6759 struct got_fileindex *fileindex;
6760 struct got_repository *repo;
6761 got_worktree_status_cb status_cb;
6762 void *status_arg;
6763 got_worktree_patch_cb patch_cb;
6764 void *patch_arg;
6765 int staged_something;
6768 static const struct got_error *
6769 stage_path(void *arg, unsigned char status,
6770 unsigned char staged_status, const char *relpath,
6771 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6772 struct got_object_id *commit_id, int dirfd, const char *de_name)
6774 struct stage_path_arg *a = arg;
6775 const struct got_error *err = NULL;
6776 struct got_fileindex_entry *ie;
6777 char *ondisk_path = NULL, *path_content = NULL;
6778 uint32_t stage;
6779 struct got_object_id *new_staged_blob_id = NULL;
6781 if (status == GOT_STATUS_UNVERSIONED)
6782 return NULL;
6784 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6785 if (ie == NULL)
6786 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6788 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6789 relpath)== -1)
6790 return got_error_from_errno("asprintf");
6792 switch (status) {
6793 case GOT_STATUS_ADD:
6794 case GOT_STATUS_MODIFY:
6795 if (a->patch_cb) {
6796 if (status == GOT_STATUS_ADD) {
6797 int choice = GOT_PATCH_CHOICE_NONE;
6798 err = (*a->patch_cb)(&choice, a->patch_arg,
6799 status, ie->path, NULL, 1, 1);
6800 if (err)
6801 break;
6802 if (choice != GOT_PATCH_CHOICE_YES)
6803 break;
6804 } else {
6805 err = create_patched_content(&path_content, 0,
6806 staged_blob_id ? staged_blob_id : blob_id,
6807 ondisk_path, dirfd, de_name, ie->path,
6808 a->repo, a->patch_cb, a->patch_arg);
6809 if (err || path_content == NULL)
6810 break;
6813 err = got_object_blob_create(&new_staged_blob_id,
6814 path_content ? path_content : ondisk_path, a->repo);
6815 if (err)
6816 break;
6817 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6818 SHA1_DIGEST_LENGTH);
6819 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6820 stage = GOT_FILEIDX_STAGE_ADD;
6821 else
6822 stage = GOT_FILEIDX_STAGE_MODIFY;
6823 got_fileindex_entry_stage_set(ie, stage);
6824 a->staged_something = 1;
6825 if (a->status_cb == NULL)
6826 break;
6827 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6828 get_staged_status(ie), relpath, blob_id,
6829 new_staged_blob_id, NULL, dirfd, de_name);
6830 break;
6831 case GOT_STATUS_DELETE:
6832 if (staged_status == GOT_STATUS_DELETE)
6833 break;
6834 if (a->patch_cb) {
6835 int choice = GOT_PATCH_CHOICE_NONE;
6836 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6837 ie->path, NULL, 1, 1);
6838 if (err)
6839 break;
6840 if (choice == GOT_PATCH_CHOICE_NO)
6841 break;
6842 if (choice != GOT_PATCH_CHOICE_YES) {
6843 err = got_error(GOT_ERR_PATCH_CHOICE);
6844 break;
6847 stage = GOT_FILEIDX_STAGE_DELETE;
6848 got_fileindex_entry_stage_set(ie, stage);
6849 a->staged_something = 1;
6850 if (a->status_cb == NULL)
6851 break;
6852 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6853 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6854 de_name);
6855 break;
6856 case GOT_STATUS_NO_CHANGE:
6857 break;
6858 case GOT_STATUS_CONFLICT:
6859 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6860 break;
6861 case GOT_STATUS_NONEXISTENT:
6862 err = got_error_set_errno(ENOENT, relpath);
6863 break;
6864 default:
6865 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6866 break;
6869 if (path_content && unlink(path_content) == -1 && err == NULL)
6870 err = got_error_from_errno2("unlink", path_content);
6871 free(path_content);
6872 free(ondisk_path);
6873 free(new_staged_blob_id);
6874 return err;
6877 const struct got_error *
6878 got_worktree_stage(struct got_worktree *worktree,
6879 struct got_pathlist_head *paths,
6880 got_worktree_status_cb status_cb, void *status_arg,
6881 got_worktree_patch_cb patch_cb, void *patch_arg,
6882 struct got_repository *repo)
6884 const struct got_error *err = NULL, *sync_err, *unlockerr;
6885 struct got_pathlist_entry *pe;
6886 struct got_fileindex *fileindex = NULL;
6887 char *fileindex_path = NULL;
6888 struct got_reference *head_ref = NULL;
6889 struct got_object_id *head_commit_id = NULL;
6890 struct check_stage_ok_arg oka;
6891 struct stage_path_arg spa;
6893 err = lock_worktree(worktree, LOCK_EX);
6894 if (err)
6895 return err;
6897 err = got_ref_open(&head_ref, repo,
6898 got_worktree_get_head_ref_name(worktree), 0);
6899 if (err)
6900 goto done;
6901 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6902 if (err)
6903 goto done;
6904 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6905 if (err)
6906 goto done;
6908 /* Check pre-conditions before staging anything. */
6909 oka.head_commit_id = head_commit_id;
6910 oka.worktree = worktree;
6911 oka.fileindex = fileindex;
6912 oka.repo = repo;
6913 oka.have_changes = 0;
6914 TAILQ_FOREACH(pe, paths, entry) {
6915 err = worktree_status(worktree, pe->path, fileindex, repo,
6916 check_stage_ok, &oka, NULL, NULL, 0, 0);
6917 if (err)
6918 goto done;
6920 if (!oka.have_changes) {
6921 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6922 goto done;
6925 spa.worktree = worktree;
6926 spa.fileindex = fileindex;
6927 spa.repo = repo;
6928 spa.patch_cb = patch_cb;
6929 spa.patch_arg = patch_arg;
6930 spa.status_cb = status_cb;
6931 spa.status_arg = status_arg;
6932 spa.staged_something = 0;
6933 TAILQ_FOREACH(pe, paths, entry) {
6934 err = worktree_status(worktree, pe->path, fileindex, repo,
6935 stage_path, &spa, NULL, NULL, 0, 0);
6936 if (err)
6937 goto done;
6939 if (!spa.staged_something) {
6940 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6941 goto done;
6944 sync_err = sync_fileindex(fileindex, fileindex_path);
6945 if (sync_err && err == NULL)
6946 err = sync_err;
6947 done:
6948 if (head_ref)
6949 got_ref_close(head_ref);
6950 free(head_commit_id);
6951 free(fileindex_path);
6952 if (fileindex)
6953 got_fileindex_free(fileindex);
6954 unlockerr = lock_worktree(worktree, LOCK_SH);
6955 if (unlockerr && err == NULL)
6956 err = unlockerr;
6957 return err;
6960 struct unstage_path_arg {
6961 struct got_worktree *worktree;
6962 struct got_fileindex *fileindex;
6963 struct got_repository *repo;
6964 got_worktree_checkout_cb progress_cb;
6965 void *progress_arg;
6966 got_worktree_patch_cb patch_cb;
6967 void *patch_arg;
6970 static const struct got_error *
6971 create_unstaged_content(char **path_unstaged_content,
6972 char **path_new_staged_content, struct got_object_id *blob_id,
6973 struct got_object_id *staged_blob_id, const char *relpath,
6974 struct got_repository *repo,
6975 got_worktree_patch_cb patch_cb, void *patch_arg)
6977 const struct got_error *err;
6978 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6979 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6980 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6981 struct stat sb1, sb2;
6982 struct got_diff_changes *changes = NULL;
6983 struct got_diff_state *ds = NULL;
6984 struct got_diff_args *args = NULL;
6985 struct got_diff_change *change;
6986 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6987 int have_content = 0, have_rejected_content = 0;
6989 *path_unstaged_content = NULL;
6990 *path_new_staged_content = NULL;
6992 err = got_object_id_str(&label1, blob_id);
6993 if (err)
6994 return err;
6995 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6996 if (err)
6997 goto done;
6999 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7000 if (err)
7001 goto done;
7003 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7004 if (err)
7005 goto done;
7007 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7008 if (err)
7009 goto done;
7011 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7012 if (err)
7013 goto done;
7015 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7016 if (err)
7017 goto done;
7019 if (stat(path1, &sb1) == -1) {
7020 err = got_error_from_errno2("stat", path1);
7021 goto done;
7024 if (stat(path2, &sb2) == -1) {
7025 err = got_error_from_errno2("stat", path2);
7026 goto done;
7029 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7030 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7031 if (err)
7032 goto done;
7034 err = got_opentemp_named(path_unstaged_content, &outfile,
7035 "got-unstaged-content");
7036 if (err)
7037 goto done;
7038 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7039 "got-new-staged-content");
7040 if (err)
7041 goto done;
7043 if (fseek(f1, 0L, SEEK_SET) == -1) {
7044 err = got_ferror(f1, GOT_ERR_IO);
7045 goto done;
7047 if (fseek(f2, 0L, SEEK_SET) == -1) {
7048 err = got_ferror(f2, GOT_ERR_IO);
7049 goto done;
7051 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7052 int choice;
7053 err = apply_or_reject_change(&choice, change, ++n,
7054 changes->nchanges, ds, args, diff_flags, relpath,
7055 f1, f2, &line_cur1, &line_cur2,
7056 outfile, rejectfile, patch_cb, patch_arg);
7057 if (err)
7058 goto done;
7059 if (choice == GOT_PATCH_CHOICE_YES)
7060 have_content = 1;
7061 else
7062 have_rejected_content = 1;
7063 if (choice == GOT_PATCH_CHOICE_QUIT)
7064 break;
7066 if (have_content || have_rejected_content)
7067 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7068 outfile, rejectfile);
7069 done:
7070 free(label1);
7071 if (blob)
7072 got_object_blob_close(blob);
7073 if (staged_blob)
7074 got_object_blob_close(staged_blob);
7075 if (f1 && fclose(f1) == EOF && err == NULL)
7076 err = got_error_from_errno2("fclose", path1);
7077 if (f2 && fclose(f2) == EOF && err == NULL)
7078 err = got_error_from_errno2("fclose", path2);
7079 if (outfile && fclose(outfile) == EOF && err == NULL)
7080 err = got_error_from_errno2("fclose", *path_unstaged_content);
7081 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7082 err = got_error_from_errno2("fclose", *path_new_staged_content);
7083 if (path1 && unlink(path1) == -1 && err == NULL)
7084 err = got_error_from_errno2("unlink", path1);
7085 if (path2 && unlink(path2) == -1 && err == NULL)
7086 err = got_error_from_errno2("unlink", path2);
7087 if (err || !have_content) {
7088 if (*path_unstaged_content &&
7089 unlink(*path_unstaged_content) == -1 && err == NULL)
7090 err = got_error_from_errno2("unlink",
7091 *path_unstaged_content);
7092 free(*path_unstaged_content);
7093 *path_unstaged_content = NULL;
7095 if (err || !have_rejected_content) {
7096 if (*path_new_staged_content &&
7097 unlink(*path_new_staged_content) == -1 && err == NULL)
7098 err = got_error_from_errno2("unlink",
7099 *path_new_staged_content);
7100 free(*path_new_staged_content);
7101 *path_new_staged_content = NULL;
7103 free(args);
7104 if (ds) {
7105 got_diff_state_free(ds);
7106 free(ds);
7108 if (changes)
7109 got_diff_free_changes(changes);
7110 free(path1);
7111 free(path2);
7112 return err;
7115 static const struct got_error *
7116 unstage_path(void *arg, unsigned char status,
7117 unsigned char staged_status, const char *relpath,
7118 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7119 struct got_object_id *commit_id, int dirfd, const char *de_name)
7121 const struct got_error *err = NULL;
7122 struct unstage_path_arg *a = arg;
7123 struct got_fileindex_entry *ie;
7124 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7125 char *ondisk_path = NULL, *path_unstaged_content = NULL;
7126 char *path_new_staged_content = NULL;
7127 char *id_str = NULL, *label_orig = NULL;
7128 int local_changes_subsumed;
7129 struct stat sb;
7131 if (staged_status != GOT_STATUS_ADD &&
7132 staged_status != GOT_STATUS_MODIFY &&
7133 staged_status != GOT_STATUS_DELETE)
7134 return NULL;
7136 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7137 if (ie == NULL)
7138 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7140 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7141 == -1)
7142 return got_error_from_errno("asprintf");
7144 err = got_object_id_str(&id_str,
7145 commit_id ? commit_id : a->worktree->base_commit_id);
7146 if (err)
7147 goto done;
7148 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7149 id_str) == -1) {
7150 err = got_error_from_errno("asprintf");
7151 goto done;
7154 switch (staged_status) {
7155 case GOT_STATUS_MODIFY:
7156 err = got_object_open_as_blob(&blob_base, a->repo,
7157 blob_id, 8192);
7158 if (err)
7159 break;
7160 /* fall through */
7161 case GOT_STATUS_ADD:
7162 if (a->patch_cb) {
7163 if (staged_status == GOT_STATUS_ADD) {
7164 int choice = GOT_PATCH_CHOICE_NONE;
7165 err = (*a->patch_cb)(&choice, a->patch_arg,
7166 staged_status, ie->path, NULL, 1, 1);
7167 if (err)
7168 break;
7169 if (choice != GOT_PATCH_CHOICE_YES)
7170 break;
7171 } else {
7172 err = create_unstaged_content(
7173 &path_unstaged_content,
7174 &path_new_staged_content, blob_id,
7175 staged_blob_id, ie->path, a->repo,
7176 a->patch_cb, a->patch_arg);
7177 if (err || path_unstaged_content == NULL)
7178 break;
7179 if (path_new_staged_content) {
7180 err = got_object_blob_create(
7181 &staged_blob_id,
7182 path_new_staged_content,
7183 a->repo);
7184 if (err)
7185 break;
7186 memcpy(ie->staged_blob_sha1,
7187 staged_blob_id->sha1,
7188 SHA1_DIGEST_LENGTH);
7190 err = merge_file(&local_changes_subsumed,
7191 a->worktree, blob_base, ondisk_path,
7192 relpath, got_fileindex_perms_to_st(ie),
7193 path_unstaged_content, label_orig,
7194 "unstaged", a->repo, a->progress_cb,
7195 a->progress_arg);
7196 if (err == NULL &&
7197 path_new_staged_content == NULL)
7198 got_fileindex_entry_stage_set(ie,
7199 GOT_FILEIDX_STAGE_NONE);
7200 break; /* Done with this file. */
7203 err = got_object_open_as_blob(&blob_staged, a->repo,
7204 staged_blob_id, 8192);
7205 if (err)
7206 break;
7207 err = merge_blob(&local_changes_subsumed, a->worktree,
7208 blob_base, ondisk_path, relpath,
7209 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7210 commit_id ? commit_id : a->worktree->base_commit_id,
7211 a->repo, a->progress_cb, a->progress_arg);
7212 if (err == NULL)
7213 got_fileindex_entry_stage_set(ie,
7214 GOT_FILEIDX_STAGE_NONE);
7215 break;
7216 case GOT_STATUS_DELETE:
7217 if (a->patch_cb) {
7218 int choice = GOT_PATCH_CHOICE_NONE;
7219 err = (*a->patch_cb)(&choice, a->patch_arg,
7220 staged_status, ie->path, NULL, 1, 1);
7221 if (err)
7222 break;
7223 if (choice == GOT_PATCH_CHOICE_NO)
7224 break;
7225 if (choice != GOT_PATCH_CHOICE_YES) {
7226 err = got_error(GOT_ERR_PATCH_CHOICE);
7227 break;
7230 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7231 err = get_file_status(&status, &sb, ie, ondisk_path,
7232 dirfd, de_name, a->repo);
7233 if (err)
7234 break;
7235 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7236 break;
7238 done:
7239 free(ondisk_path);
7240 if (path_unstaged_content &&
7241 unlink(path_unstaged_content) == -1 && err == NULL)
7242 err = got_error_from_errno2("unlink", path_unstaged_content);
7243 if (path_new_staged_content &&
7244 unlink(path_new_staged_content) == -1 && err == NULL)
7245 err = got_error_from_errno2("unlink", path_new_staged_content);
7246 free(path_unstaged_content);
7247 free(path_new_staged_content);
7248 if (blob_base)
7249 got_object_blob_close(blob_base);
7250 if (blob_staged)
7251 got_object_blob_close(blob_staged);
7252 free(id_str);
7253 free(label_orig);
7254 return err;
7257 const struct got_error *
7258 got_worktree_unstage(struct got_worktree *worktree,
7259 struct got_pathlist_head *paths,
7260 got_worktree_checkout_cb progress_cb, void *progress_arg,
7261 got_worktree_patch_cb patch_cb, void *patch_arg,
7262 struct got_repository *repo)
7264 const struct got_error *err = NULL, *sync_err, *unlockerr;
7265 struct got_pathlist_entry *pe;
7266 struct got_fileindex *fileindex = NULL;
7267 char *fileindex_path = NULL;
7268 struct unstage_path_arg upa;
7270 err = lock_worktree(worktree, LOCK_EX);
7271 if (err)
7272 return err;
7274 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7275 if (err)
7276 goto done;
7278 upa.worktree = worktree;
7279 upa.fileindex = fileindex;
7280 upa.repo = repo;
7281 upa.progress_cb = progress_cb;
7282 upa.progress_arg = progress_arg;
7283 upa.patch_cb = patch_cb;
7284 upa.patch_arg = patch_arg;
7285 TAILQ_FOREACH(pe, paths, entry) {
7286 err = worktree_status(worktree, pe->path, fileindex, repo,
7287 unstage_path, &upa, NULL, NULL, 0, 0);
7288 if (err)
7289 goto done;
7292 sync_err = sync_fileindex(fileindex, fileindex_path);
7293 if (sync_err && err == NULL)
7294 err = sync_err;
7295 done:
7296 free(fileindex_path);
7297 if (fileindex)
7298 got_fileindex_free(fileindex);
7299 unlockerr = lock_worktree(worktree, LOCK_SH);
7300 if (unlockerr && err == NULL)
7301 err = unlockerr;
7302 return err;