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;
951 if (blob_orig) {
952 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
953 if (err)
954 goto done;
957 err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
958 if (err)
959 goto done;
961 if (ancestor_target == NULL ||
962 (ondisk_len != strlen(ancestor_target) ||
963 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
964 have_local_change = 1;
966 deriv_len = strlen(deriv_target);
967 if (ancestor_target == NULL ||
968 (deriv_len != strlen(ancestor_target) ||
969 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
970 have_incoming_change = 1;
972 if (!have_local_change && !have_incoming_change) {
973 if (ancestor_target) {
974 /* Both sides made the same change. */
975 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
976 path);
977 } else if (deriv_len == ondisk_len &&
978 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
979 /* Both sides added the same symlink. */
980 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
981 path);
982 } else {
983 /* Both sides added symlinks which don't match. */
984 err = install_symlink_conflict(deriv_target,
985 deriv_base_commit_id, ancestor_target,
986 label_orig, ondisk_target, ondisk_path);
987 if (err)
988 goto done;
989 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
990 path);
992 } else if (!have_local_change && have_incoming_change) {
993 /* Apply the incoming change. */
994 err = update_symlink(ondisk_path, deriv_target,
995 strlen(deriv_target));
996 if (err)
997 goto done;
998 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
999 } else if (have_local_change && have_incoming_change) {
1000 err = install_symlink_conflict(deriv_target,
1001 deriv_base_commit_id, ancestor_target, label_orig,
1002 ondisk_target, ondisk_path);
1003 if (err)
1004 goto done;
1005 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1006 path);
1009 done:
1010 free(ancestor_target);
1011 free(deriv_target);
1012 return err;
1016 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1017 * blob_deriv acts as the first derived version, and the file on disk
1018 * acts as the second derived version.
1020 static const struct got_error *
1021 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1022 struct got_blob_object *blob_orig, const char *ondisk_path,
1023 const char *path, uint16_t st_mode, const char *label_orig,
1024 struct got_blob_object *blob_deriv,
1025 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1026 got_worktree_checkout_cb progress_cb, void *progress_arg)
1028 const struct got_error *err = NULL;
1029 FILE *f_deriv = NULL;
1030 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1031 char *label_deriv = NULL, *parent;
1033 *local_changes_subsumed = 0;
1035 parent = dirname(ondisk_path);
1036 if (parent == NULL)
1037 return got_error_from_errno2("dirname", ondisk_path);
1039 free(base_path);
1040 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1041 err = got_error_from_errno("asprintf");
1042 base_path = NULL;
1043 goto done;
1046 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1047 if (err)
1048 goto done;
1049 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1050 blob_deriv);
1051 if (err)
1052 goto done;
1054 err = got_object_id_str(&id_str, deriv_base_commit_id);
1055 if (err)
1056 goto done;
1057 if (asprintf(&label_deriv, "%s: commit %s",
1058 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1059 err = got_error_from_errno("asprintf");
1060 goto done;
1063 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1064 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1065 label_deriv, repo, progress_cb, progress_arg);
1066 done:
1067 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1068 err = got_error_from_errno("fclose");
1069 free(base_path);
1070 if (blob_deriv_path) {
1071 unlink(blob_deriv_path);
1072 free(blob_deriv_path);
1074 free(id_str);
1075 free(label_deriv);
1076 return err;
1079 static const struct got_error *
1080 create_fileindex_entry(struct got_fileindex *fileindex,
1081 struct got_object_id *base_commit_id, const char *ondisk_path,
1082 const char *path, struct got_object_id *blob_id)
1084 const struct got_error *err = NULL;
1085 struct got_fileindex_entry *new_ie;
1087 err = got_fileindex_entry_alloc(&new_ie, path);
1088 if (err)
1089 return err;
1091 err = got_fileindex_entry_update(new_ie, ondisk_path,
1092 blob_id->sha1, base_commit_id->sha1, 1);
1093 if (err)
1094 goto done;
1096 err = got_fileindex_entry_add(fileindex, new_ie);
1097 done:
1098 if (err)
1099 got_fileindex_entry_free(new_ie);
1100 return err;
1103 static mode_t
1104 get_ondisk_perms(int executable, mode_t st_mode)
1106 mode_t xbits = S_IXUSR;
1108 if (executable) {
1109 /* Map read bits to execute bits. */
1110 if (st_mode & S_IRGRP)
1111 xbits |= S_IXGRP;
1112 if (st_mode & S_IROTH)
1113 xbits |= S_IXOTH;
1114 return st_mode | xbits;
1117 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1120 /* forward declaration */
1121 static const struct got_error *
1122 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1123 const char *path, mode_t te_mode, mode_t st_mode,
1124 struct got_blob_object *blob, int restoring_missing_file,
1125 int reverting_versioned_file, int installing_bad_symlink,
1126 struct got_repository *repo,
1127 got_worktree_checkout_cb progress_cb, void *progress_arg);
1130 * This function assumes that the provided symlink target points at a
1131 * safe location in the work tree!
1133 static const struct got_error *
1134 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1135 size_t target_len)
1137 const struct got_error *err = NULL;
1138 ssize_t elen;
1139 char etarget[PATH_MAX];
1140 int fd;
1143 * "Bad" symlinks (those pointing outside the work tree or into the
1144 * .got directory) are installed in the work tree as a regular file
1145 * which contains the bad symlink target path.
1146 * The new symlink target has already been checked for safety by our
1147 * caller. If we can successfully open a regular file then we simply
1148 * replace this file with a symlink below.
1150 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1151 if (fd == -1) {
1152 if (errno != ELOOP)
1153 return got_error_from_errno2("open", ondisk_path);
1155 /* We are updating an existing on-disk symlink. */
1156 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1157 if (elen == -1)
1158 return got_error_from_errno2("readlink", ondisk_path);
1160 if (elen == target_len &&
1161 memcmp(etarget, target_path, target_len) == 0)
1162 return NULL; /* nothing to do */
1165 err = update_symlink(ondisk_path, target_path, target_len);
1166 if (fd != -1 && close(fd) == -1 && err == NULL)
1167 err = got_error_from_errno2("close", ondisk_path);
1168 return err;
1171 static const struct got_error *
1172 install_symlink(struct got_worktree *worktree, const char *ondisk_path,
1173 const char *path, mode_t te_mode, mode_t st_mode,
1174 struct got_blob_object *blob, int restoring_missing_file,
1175 int reverting_versioned_file, struct got_repository *repo,
1176 got_worktree_checkout_cb progress_cb, void *progress_arg)
1178 const struct got_error *err = NULL;
1179 char target_path[PATH_MAX];
1180 size_t len, target_len = 0;
1181 char *resolved_path = NULL, *abspath = NULL;
1182 char *path_got = NULL;
1183 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1184 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1187 * Blob object content specifies the target path of the link.
1188 * If a symbolic link cannot be installed we instead create
1189 * a regular file which contains the link target path stored
1190 * in the blob object.
1192 do {
1193 err = got_object_blob_read_block(&len, blob);
1194 if (len + target_len >= sizeof(target_path)) {
1195 /* Path too long; install as a regular file. */
1196 got_object_blob_rewind(blob);
1197 return install_blob(worktree, ondisk_path, path,
1198 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1199 restoring_missing_file, reverting_versioned_file,
1200 1, repo, progress_cb, progress_arg);
1202 if (len > 0) {
1203 /* Skip blob object header first time around. */
1204 memcpy(target_path + target_len, buf + hdrlen,
1205 len - hdrlen);
1206 target_len += len - hdrlen;
1207 hdrlen = 0;
1209 } while (len != 0);
1210 target_path[target_len] = '\0';
1213 * Relative symlink target lookup should begin at the directory
1214 * in which the blob object is being installed.
1216 if (!got_path_is_absolute(target_path)) {
1217 char *parent = dirname(ondisk_path);
1218 if (parent == NULL) {
1219 err = got_error_from_errno2("dirname", ondisk_path);
1220 goto done;
1222 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1223 err = got_error_from_errno("asprintf");
1224 goto done;
1229 * unveil(2) restricts our view of paths in the filesystem.
1230 * ENOENT will occur if a link target path does not exist or
1231 * if it points outside our unveiled path space.
1233 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1234 if (resolved_path == NULL) {
1235 if (errno != ENOENT) {
1236 err = got_error_from_errno2("realpath", target_path);
1237 goto done;
1241 /* Only allow symlinks pointing at paths within the work tree. */
1242 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1243 abspath : target_path), worktree->root_path,
1244 strlen(worktree->root_path))) {
1245 /* install as a regular file */
1246 got_object_blob_rewind(blob);
1247 err = install_blob(worktree, ondisk_path, path,
1248 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1249 restoring_missing_file, reverting_versioned_file, 1,
1250 repo, progress_cb, progress_arg);
1251 goto done;
1254 /* Do not allow symlinks pointing into the .got directory. */
1255 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1256 GOT_WORKTREE_GOT_DIR) == -1) {
1257 err = got_error_from_errno("asprintf");
1258 goto done;
1260 if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1261 abspath : target_path), path_got, strlen(path_got))) {
1262 /* install as a regular file */
1263 got_object_blob_rewind(blob);
1264 err = install_blob(worktree, ondisk_path, path,
1265 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1266 restoring_missing_file, reverting_versioned_file, 1,
1267 repo, progress_cb, progress_arg);
1268 goto done;
1271 if (symlink(target_path, ondisk_path) == -1) {
1272 if (errno == EEXIST) {
1273 err = replace_existing_symlink(ondisk_path,
1274 target_path, target_len);
1275 if (err)
1276 goto done;
1277 if (progress_cb) {
1278 err = (*progress_cb)(progress_arg,
1279 GOT_STATUS_UPDATE, path);
1281 goto done; /* Nothing else to do. */
1284 if (errno == ENOENT) {
1285 char *parent = dirname(ondisk_path);
1286 if (parent == NULL) {
1287 err = got_error_from_errno2("dirname",
1288 ondisk_path);
1289 goto done;
1291 err = add_dir_on_disk(worktree, parent);
1292 if (err)
1293 goto done;
1295 * Retry, and fall through to error handling
1296 * below if this second attempt fails.
1298 if (symlink(target_path, ondisk_path) != -1) {
1299 err = NULL; /* success */
1300 goto done;
1304 /* Handle errors from first or second creation attempt. */
1305 if (errno == ENAMETOOLONG) {
1306 /* bad target path; install as a regular file */
1307 got_object_blob_rewind(blob);
1308 err = install_blob(worktree, ondisk_path, path,
1309 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1310 restoring_missing_file, reverting_versioned_file, 1,
1311 repo, progress_cb, progress_arg);
1312 } else if (errno == ENOTDIR) {
1313 err = got_error_path(ondisk_path,
1314 GOT_ERR_FILE_OBSTRUCTED);
1315 } else {
1316 err = got_error_from_errno3("symlink",
1317 target_path, ondisk_path);
1319 } else if (progress_cb)
1320 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1321 done:
1322 free(resolved_path);
1323 free(abspath);
1324 free(path_got);
1325 return err;
1328 static const struct got_error *
1329 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1330 const char *path, mode_t te_mode, mode_t st_mode,
1331 struct got_blob_object *blob, int restoring_missing_file,
1332 int reverting_versioned_file, int installing_bad_symlink,
1333 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1334 void *progress_arg)
1336 const struct got_error *err = NULL;
1337 int fd = -1;
1338 size_t len, hdrlen;
1339 int update = 0;
1340 char *tmppath = NULL;
1342 if (S_ISLNK(te_mode))
1343 return install_symlink(worktree, ondisk_path, path, te_mode,
1344 st_mode, blob, restoring_missing_file,
1345 reverting_versioned_file, repo, progress_cb, progress_arg);
1347 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1348 GOT_DEFAULT_FILE_MODE);
1349 if (fd == -1) {
1350 if (errno == ENOENT) {
1351 char *parent = dirname(path);
1352 if (parent == NULL)
1353 return got_error_from_errno2("dirname", path);
1354 err = add_dir_on_disk(worktree, parent);
1355 if (err)
1356 return err;
1357 fd = open(ondisk_path,
1358 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1359 GOT_DEFAULT_FILE_MODE);
1360 if (fd == -1)
1361 return got_error_from_errno2("open",
1362 ondisk_path);
1363 } else if (errno == EEXIST) {
1364 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1365 /* TODO file is obstructed; do something */
1366 err = got_error_path(ondisk_path,
1367 GOT_ERR_FILE_OBSTRUCTED);
1368 goto done;
1369 } else {
1370 err = got_opentemp_named_fd(&tmppath, &fd,
1371 ondisk_path);
1372 if (err)
1373 goto done;
1374 update = 1;
1376 } else
1377 return got_error_from_errno2("open", ondisk_path);
1380 if (progress_cb) {
1381 if (restoring_missing_file)
1382 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1383 path);
1384 else if (reverting_versioned_file)
1385 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1386 path);
1387 else
1388 err = (*progress_cb)(progress_arg,
1389 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1390 if (err)
1391 goto done;
1394 hdrlen = got_object_blob_get_hdrlen(blob);
1395 do {
1396 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1397 err = got_object_blob_read_block(&len, blob);
1398 if (err)
1399 break;
1400 if (len > 0) {
1401 /* Skip blob object header first time around. */
1402 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1403 if (outlen == -1) {
1404 err = got_error_from_errno("write");
1405 goto done;
1406 } else if (outlen != len - hdrlen) {
1407 err = got_error(GOT_ERR_IO);
1408 goto done;
1410 hdrlen = 0;
1412 } while (len != 0);
1414 if (fsync(fd) != 0) {
1415 err = got_error_from_errno("fsync");
1416 goto done;
1419 if (update) {
1420 if (rename(tmppath, ondisk_path) != 0) {
1421 err = got_error_from_errno3("rename", tmppath,
1422 ondisk_path);
1423 unlink(tmppath);
1424 goto done;
1428 if (chmod(ondisk_path,
1429 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1430 err = got_error_from_errno2("chmod", ondisk_path);
1431 goto done;
1434 done:
1435 if (fd != -1 && close(fd) != 0 && err == NULL)
1436 err = got_error_from_errno("close");
1437 free(tmppath);
1438 return err;
1441 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1442 static const struct got_error *
1443 get_modified_file_content_status(unsigned char *status, FILE *f)
1445 const struct got_error *err = NULL;
1446 const char *markers[3] = {
1447 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1448 GOT_DIFF_CONFLICT_MARKER_SEP,
1449 GOT_DIFF_CONFLICT_MARKER_END
1451 int i = 0;
1452 char *line;
1453 size_t len;
1454 const char delim[3] = {'\0', '\0', '\0'};
1456 while (*status == GOT_STATUS_MODIFY) {
1457 line = fparseln(f, &len, NULL, delim, 0);
1458 if (line == NULL) {
1459 if (feof(f))
1460 break;
1461 err = got_ferror(f, GOT_ERR_IO);
1462 break;
1465 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1466 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1467 == 0)
1468 *status = GOT_STATUS_CONFLICT;
1469 else
1470 i++;
1474 return err;
1477 static int
1478 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1480 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1481 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1484 static int
1485 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1487 return !(ie->ctime_sec == sb->st_ctime &&
1488 ie->ctime_nsec == sb->st_ctimensec &&
1489 ie->mtime_sec == sb->st_mtime &&
1490 ie->mtime_nsec == sb->st_mtimensec &&
1491 ie->size == (sb->st_size & 0xffffffff) &&
1492 !xbit_differs(ie, sb->st_mode));
1495 static unsigned char
1496 get_staged_status(struct got_fileindex_entry *ie)
1498 switch (got_fileindex_entry_stage_get(ie)) {
1499 case GOT_FILEIDX_STAGE_ADD:
1500 return GOT_STATUS_ADD;
1501 case GOT_FILEIDX_STAGE_DELETE:
1502 return GOT_STATUS_DELETE;
1503 case GOT_FILEIDX_STAGE_MODIFY:
1504 return GOT_STATUS_MODIFY;
1505 default:
1506 return GOT_STATUS_NO_CHANGE;
1510 static const struct got_error *
1511 get_symlink_status(unsigned char *status, struct stat *sb,
1512 struct got_fileindex_entry *ie, const char *abspath,
1513 int dirfd, const char *de_name, struct got_blob_object *blob)
1515 const struct got_error *err = NULL;
1516 char target_path[PATH_MAX];
1517 char etarget[PATH_MAX];
1518 ssize_t elen;
1519 size_t len, target_len = 0;
1520 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1521 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1523 *status = GOT_STATUS_NO_CHANGE;
1525 /* Blob object content specifies the target path of the link. */
1526 do {
1527 err = got_object_blob_read_block(&len, blob);
1528 if (err)
1529 return err;
1530 if (len + target_len >= sizeof(target_path)) {
1532 * Should not happen. The blob contents were OK
1533 * when this symlink was installed.
1535 return got_error(GOT_ERR_NO_SPACE);
1537 if (len > 0) {
1538 /* Skip blob object header first time around. */
1539 memcpy(target_path + target_len, buf + hdrlen,
1540 len - hdrlen);
1541 target_len += len - hdrlen;
1542 hdrlen = 0;
1544 } while (len != 0);
1545 target_path[target_len] = '\0';
1547 if (dirfd != -1) {
1548 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1549 if (elen == -1)
1550 return got_error_from_errno2("readlinkat", abspath);
1551 } else {
1552 elen = readlink(abspath, etarget, sizeof(etarget));
1553 if (elen == -1)
1554 return got_error_from_errno2("readlinkat", abspath);
1557 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1558 *status = GOT_STATUS_MODIFY;
1560 return NULL;
1563 static const struct got_error *
1564 get_file_status(unsigned char *status, struct stat *sb,
1565 struct got_fileindex_entry *ie, const char *abspath,
1566 int dirfd, const char *de_name, struct got_repository *repo)
1568 const struct got_error *err = NULL;
1569 struct got_object_id id;
1570 size_t hdrlen;
1571 int fd = -1;
1572 FILE *f = NULL;
1573 uint8_t fbuf[8192];
1574 struct got_blob_object *blob = NULL;
1575 size_t flen, blen;
1576 unsigned char staged_status = get_staged_status(ie);
1578 *status = GOT_STATUS_NO_CHANGE;
1581 * Whenever the caller provides a directory descriptor and a
1582 * directory entry name for the file, use them! This prevents
1583 * race conditions if filesystem paths change beneath our feet.
1585 if (dirfd != -1) {
1586 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1587 if (errno == ENOENT) {
1588 if (got_fileindex_entry_has_file_on_disk(ie))
1589 *status = GOT_STATUS_MISSING;
1590 else
1591 *status = GOT_STATUS_DELETE;
1592 goto done;
1594 err = got_error_from_errno2("fstatat", abspath);
1595 goto done;
1597 } else {
1598 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1599 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1600 return got_error_from_errno2("open", abspath);
1601 else if (fd == -1 && errno == ELOOP) {
1602 if (lstat(abspath, sb) == -1)
1603 return got_error_from_errno2("lstat", abspath);
1604 } else if (fd == -1 || fstat(fd, sb) == -1) {
1605 if (errno == ENOENT) {
1606 if (got_fileindex_entry_has_file_on_disk(ie))
1607 *status = GOT_STATUS_MISSING;
1608 else
1609 *status = GOT_STATUS_DELETE;
1610 goto done;
1612 err = got_error_from_errno2("fstat", abspath);
1613 goto done;
1617 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1618 *status = GOT_STATUS_OBSTRUCTED;
1619 goto done;
1622 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1623 *status = GOT_STATUS_DELETE;
1624 goto done;
1625 } else if (!got_fileindex_entry_has_blob(ie) &&
1626 staged_status != GOT_STATUS_ADD) {
1627 *status = GOT_STATUS_ADD;
1628 goto done;
1631 if (!stat_info_differs(ie, sb))
1632 goto done;
1634 if (staged_status == GOT_STATUS_MODIFY ||
1635 staged_status == GOT_STATUS_ADD)
1636 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1637 else
1638 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1640 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1641 if (err)
1642 goto done;
1644 if (S_ISLNK(sb->st_mode)) {
1645 /* Staging changes to symlinks is not yet(?) supported. */
1646 if (staged_status != GOT_STATUS_NO_CHANGE) {
1647 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1648 goto done;
1650 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1651 de_name, blob);
1652 goto done;
1656 if (dirfd != -1) {
1657 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1658 if (fd == -1) {
1659 err = got_error_from_errno2("openat", abspath);
1660 goto done;
1664 f = fdopen(fd, "r");
1665 if (f == NULL) {
1666 err = got_error_from_errno2("fdopen", abspath);
1667 goto done;
1669 fd = -1;
1670 hdrlen = got_object_blob_get_hdrlen(blob);
1671 for (;;) {
1672 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1673 err = got_object_blob_read_block(&blen, blob);
1674 if (err)
1675 goto done;
1676 /* Skip length of blob object header first time around. */
1677 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1678 if (flen == 0 && ferror(f)) {
1679 err = got_error_from_errno("fread");
1680 goto done;
1682 if (blen == 0) {
1683 if (flen != 0)
1684 *status = GOT_STATUS_MODIFY;
1685 break;
1686 } else if (flen == 0) {
1687 if (blen != 0)
1688 *status = GOT_STATUS_MODIFY;
1689 break;
1690 } else if (blen - hdrlen == flen) {
1691 /* Skip blob object header first time around. */
1692 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1693 *status = GOT_STATUS_MODIFY;
1694 break;
1696 } else {
1697 *status = GOT_STATUS_MODIFY;
1698 break;
1700 hdrlen = 0;
1703 if (*status == GOT_STATUS_MODIFY) {
1704 rewind(f);
1705 err = get_modified_file_content_status(status, f);
1706 } else if (xbit_differs(ie, sb->st_mode))
1707 *status = GOT_STATUS_MODE_CHANGE;
1708 done:
1709 if (blob)
1710 got_object_blob_close(blob);
1711 if (f != NULL && fclose(f) == EOF && err == NULL)
1712 err = got_error_from_errno2("fclose", abspath);
1713 if (fd != -1 && close(fd) == -1 && err == NULL)
1714 err = got_error_from_errno2("close", abspath);
1715 return err;
1719 * Update timestamps in the file index if a file is unmodified and
1720 * we had to run a full content comparison to find out.
1722 static const struct got_error *
1723 sync_timestamps(char *ondisk_path, unsigned char status,
1724 struct got_fileindex_entry *ie, struct stat *sb)
1726 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1727 return got_fileindex_entry_update(ie, ondisk_path,
1728 ie->blob_sha1, ie->commit_sha1, 1);
1730 return NULL;
1733 static const struct got_error *
1734 update_blob(struct got_worktree *worktree,
1735 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1736 struct got_tree_entry *te, const char *path,
1737 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1738 void *progress_arg)
1740 const struct got_error *err = NULL;
1741 struct got_blob_object *blob = NULL;
1742 char *ondisk_path;
1743 unsigned char status = GOT_STATUS_NO_CHANGE;
1744 struct stat sb;
1746 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1747 return got_error_from_errno("asprintf");
1749 if (ie) {
1750 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1751 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1752 goto done;
1754 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1755 repo);
1756 if (err)
1757 goto done;
1758 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1759 sb.st_mode = got_fileindex_perms_to_st(ie);
1760 } else
1761 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1763 if (status == GOT_STATUS_OBSTRUCTED) {
1764 err = (*progress_cb)(progress_arg, status, path);
1765 goto done;
1767 if (status == GOT_STATUS_CONFLICT) {
1768 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1769 path);
1770 goto done;
1773 if (ie && status != GOT_STATUS_MISSING &&
1774 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1775 if (got_fileindex_entry_has_commit(ie) &&
1776 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1777 SHA1_DIGEST_LENGTH) == 0) {
1778 err = sync_timestamps(ondisk_path, status, ie, &sb);
1779 if (err)
1780 goto done;
1781 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1782 path);
1783 goto done;
1785 if (got_fileindex_entry_has_blob(ie) &&
1786 memcmp(ie->blob_sha1, te->id.sha1,
1787 SHA1_DIGEST_LENGTH) == 0) {
1788 err = sync_timestamps(ondisk_path, status, ie, &sb);
1789 goto done;
1793 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1794 if (err)
1795 goto done;
1797 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1798 int update_timestamps;
1799 struct got_blob_object *blob2 = NULL;
1800 char *label_orig = NULL;
1801 if (got_fileindex_entry_has_blob(ie)) {
1802 struct got_object_id id2;
1803 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1804 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1805 if (err)
1806 goto done;
1808 if (got_fileindex_entry_has_commit(ie)) {
1809 char id_str[SHA1_DIGEST_STRING_LENGTH];
1810 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1811 sizeof(id_str)) == NULL) {
1812 err = got_error_path(id_str,
1813 GOT_ERR_BAD_OBJ_ID_STR);
1814 goto done;
1816 if (asprintf(&label_orig, "%s: commit %s",
1817 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1818 err = got_error_from_errno("asprintf");
1819 goto done;
1822 err = merge_blob(&update_timestamps, worktree, blob2,
1823 ondisk_path, path, sb.st_mode, label_orig, blob,
1824 worktree->base_commit_id, repo,
1825 progress_cb, progress_arg);
1826 free(label_orig);
1827 if (blob2)
1828 got_object_blob_close(blob2);
1829 if (err)
1830 goto done;
1832 * Do not update timestamps of files with local changes.
1833 * Otherwise, a future status walk would treat them as
1834 * unmodified files again.
1836 err = got_fileindex_entry_update(ie, ondisk_path,
1837 blob->id.sha1, worktree->base_commit_id->sha1,
1838 update_timestamps);
1839 } else if (status == GOT_STATUS_MODE_CHANGE) {
1840 err = got_fileindex_entry_update(ie, ondisk_path,
1841 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1842 } else if (status == GOT_STATUS_DELETE) {
1843 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1844 if (err)
1845 goto done;
1846 err = got_fileindex_entry_update(ie, ondisk_path,
1847 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1848 if (err)
1849 goto done;
1850 } else {
1851 err = install_blob(worktree, ondisk_path, path, te->mode,
1852 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0, 0,
1853 repo, progress_cb, progress_arg);
1854 if (err)
1855 goto done;
1856 if (ie) {
1857 err = got_fileindex_entry_update(ie, ondisk_path,
1858 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1859 } else {
1860 err = create_fileindex_entry(fileindex,
1861 worktree->base_commit_id, ondisk_path, path,
1862 &blob->id);
1864 if (err)
1865 goto done;
1867 got_object_blob_close(blob);
1868 done:
1869 free(ondisk_path);
1870 return err;
1873 static const struct got_error *
1874 remove_ondisk_file(const char *root_path, const char *path)
1876 const struct got_error *err = NULL;
1877 char *ondisk_path = NULL;
1879 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1880 return got_error_from_errno("asprintf");
1882 if (unlink(ondisk_path) == -1) {
1883 if (errno != ENOENT)
1884 err = got_error_from_errno2("unlink", ondisk_path);
1885 } else {
1886 char *parent = dirname(ondisk_path);
1887 while (parent && strcmp(parent, root_path) != 0) {
1888 if (rmdir(parent) == -1) {
1889 if (errno != ENOTEMPTY)
1890 err = got_error_from_errno2("rmdir",
1891 parent);
1892 break;
1894 parent = dirname(parent);
1897 free(ondisk_path);
1898 return err;
1901 static const struct got_error *
1902 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1903 struct got_fileindex_entry *ie, struct got_repository *repo,
1904 got_worktree_checkout_cb progress_cb, void *progress_arg)
1906 const struct got_error *err = NULL;
1907 unsigned char status;
1908 struct stat sb;
1909 char *ondisk_path;
1911 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1912 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1914 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1915 == -1)
1916 return got_error_from_errno("asprintf");
1918 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1919 if (err)
1920 goto done;
1922 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1923 status == GOT_STATUS_ADD) {
1924 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1925 if (err)
1926 goto done;
1928 * Preserve the working file and change the deleted blob's
1929 * entry into a schedule-add entry.
1931 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1932 0);
1933 } else {
1934 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1935 if (err)
1936 goto done;
1937 if (status == GOT_STATUS_NO_CHANGE) {
1938 err = remove_ondisk_file(worktree->root_path, ie->path);
1939 if (err)
1940 goto done;
1942 got_fileindex_entry_remove(fileindex, ie);
1944 done:
1945 free(ondisk_path);
1946 return err;
1949 struct diff_cb_arg {
1950 struct got_fileindex *fileindex;
1951 struct got_worktree *worktree;
1952 struct got_repository *repo;
1953 got_worktree_checkout_cb progress_cb;
1954 void *progress_arg;
1955 got_cancel_cb cancel_cb;
1956 void *cancel_arg;
1959 static const struct got_error *
1960 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1961 struct got_tree_entry *te, const char *parent_path)
1963 struct diff_cb_arg *a = arg;
1965 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1966 return got_error(GOT_ERR_CANCELLED);
1968 return update_blob(a->worktree, a->fileindex, ie, te,
1969 ie->path, a->repo, a->progress_cb, a->progress_arg);
1972 static const struct got_error *
1973 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1975 struct diff_cb_arg *a = arg;
1977 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1978 return got_error(GOT_ERR_CANCELLED);
1980 return delete_blob(a->worktree, a->fileindex, ie,
1981 a->repo, a->progress_cb, a->progress_arg);
1984 static const struct got_error *
1985 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1987 struct diff_cb_arg *a = arg;
1988 const struct got_error *err;
1989 char *path;
1991 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1992 return got_error(GOT_ERR_CANCELLED);
1994 if (got_object_tree_entry_is_submodule(te))
1995 return NULL;
1997 if (asprintf(&path, "%s%s%s", parent_path,
1998 parent_path[0] ? "/" : "", te->name)
1999 == -1)
2000 return got_error_from_errno("asprintf");
2002 if (S_ISDIR(te->mode))
2003 err = add_dir_on_disk(a->worktree, path);
2004 else
2005 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2006 a->repo, a->progress_cb, a->progress_arg);
2008 free(path);
2009 return err;
2012 static const struct got_error *
2013 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2015 const struct got_error *err = NULL;
2016 char *uuidstr = NULL;
2017 uint32_t uuid_status;
2019 *refname = NULL;
2021 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2022 if (uuid_status != uuid_s_ok)
2023 return got_error_uuid(uuid_status, "uuid_to_string");
2025 if (asprintf(refname, "%s-%s", prefix, uuidstr)
2026 == -1) {
2027 err = got_error_from_errno("asprintf");
2028 *refname = NULL;
2030 free(uuidstr);
2031 return err;
2034 const struct got_error *
2035 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2037 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2040 static const struct got_error *
2041 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2043 return get_ref_name(refname, worktree,
2044 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2047 static const struct got_error *
2048 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2050 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2053 static const struct got_error *
2054 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2056 return get_ref_name(refname, worktree,
2057 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2060 static const struct got_error *
2061 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2063 return get_ref_name(refname, worktree,
2064 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2067 static const struct got_error *
2068 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2070 return get_ref_name(refname, worktree,
2071 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2074 static const struct got_error *
2075 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2077 return get_ref_name(refname, worktree,
2078 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2081 static const struct got_error *
2082 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2084 return get_ref_name(refname, worktree,
2085 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2088 static const struct got_error *
2089 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2091 return get_ref_name(refname, worktree,
2092 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2095 const struct got_error *
2096 got_worktree_get_histedit_script_path(char **path,
2097 struct got_worktree *worktree)
2099 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2100 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2101 *path = NULL;
2102 return got_error_from_errno("asprintf");
2104 return NULL;
2108 * Prevent Git's garbage collector from deleting our base commit by
2109 * setting a reference to our base commit's ID.
2111 static const struct got_error *
2112 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2114 const struct got_error *err = NULL;
2115 struct got_reference *ref = NULL;
2116 char *refname;
2118 err = got_worktree_get_base_ref_name(&refname, worktree);
2119 if (err)
2120 return err;
2122 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2123 if (err)
2124 goto done;
2126 err = got_ref_write(ref, repo);
2127 done:
2128 free(refname);
2129 if (ref)
2130 got_ref_close(ref);
2131 return err;
2134 static const struct got_error *
2135 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2137 const struct got_error *err = NULL;
2139 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2140 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2141 err = got_error_from_errno("asprintf");
2142 *fileindex_path = NULL;
2144 return err;
2148 static const struct got_error *
2149 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2150 struct got_worktree *worktree)
2152 const struct got_error *err = NULL;
2153 FILE *index = NULL;
2155 *fileindex_path = NULL;
2156 *fileindex = got_fileindex_alloc();
2157 if (*fileindex == NULL)
2158 return got_error_from_errno("got_fileindex_alloc");
2160 err = get_fileindex_path(fileindex_path, worktree);
2161 if (err)
2162 goto done;
2164 index = fopen(*fileindex_path, "rb");
2165 if (index == NULL) {
2166 if (errno != ENOENT)
2167 err = got_error_from_errno2("fopen", *fileindex_path);
2168 } else {
2169 err = got_fileindex_read(*fileindex, index);
2170 if (fclose(index) != 0 && err == NULL)
2171 err = got_error_from_errno("fclose");
2173 done:
2174 if (err) {
2175 free(*fileindex_path);
2176 *fileindex_path = NULL;
2177 got_fileindex_free(*fileindex);
2178 *fileindex = NULL;
2180 return err;
2183 struct bump_base_commit_id_arg {
2184 struct got_object_id *base_commit_id;
2185 const char *path;
2186 size_t path_len;
2187 const char *entry_name;
2188 got_worktree_checkout_cb progress_cb;
2189 void *progress_arg;
2192 /* Bump base commit ID of all files within an updated part of the work tree. */
2193 static const struct got_error *
2194 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2196 const struct got_error *err;
2197 struct bump_base_commit_id_arg *a = arg;
2199 if (a->entry_name) {
2200 if (strcmp(ie->path, a->path) != 0)
2201 return NULL;
2202 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2203 return NULL;
2205 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2206 SHA1_DIGEST_LENGTH) == 0)
2207 return NULL;
2209 if (a->progress_cb) {
2210 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2211 ie->path);
2212 if (err)
2213 return err;
2215 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2216 return NULL;
2219 static const struct got_error *
2220 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2222 const struct got_error *err = NULL;
2223 char *new_fileindex_path = NULL;
2224 FILE *new_index = NULL;
2225 struct timespec timeout;
2227 err = got_opentemp_named(&new_fileindex_path, &new_index,
2228 fileindex_path);
2229 if (err)
2230 goto done;
2232 err = got_fileindex_write(fileindex, new_index);
2233 if (err)
2234 goto done;
2236 if (rename(new_fileindex_path, fileindex_path) != 0) {
2237 err = got_error_from_errno3("rename", new_fileindex_path,
2238 fileindex_path);
2239 unlink(new_fileindex_path);
2243 * Sleep for a short amount of time to ensure that files modified after
2244 * this program exits have a different time stamp from the one which
2245 * was recorded in the file index.
2247 timeout.tv_sec = 0;
2248 timeout.tv_nsec = 1;
2249 nanosleep(&timeout, NULL);
2250 done:
2251 if (new_index)
2252 fclose(new_index);
2253 free(new_fileindex_path);
2254 return err;
2257 static const struct got_error *
2258 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2259 struct got_object_id **tree_id, const char *wt_relpath,
2260 struct got_worktree *worktree, struct got_repository *repo)
2262 const struct got_error *err = NULL;
2263 struct got_object_id *id = NULL;
2264 char *in_repo_path = NULL;
2265 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2267 *entry_type = GOT_OBJ_TYPE_ANY;
2268 *tree_relpath = NULL;
2269 *tree_id = NULL;
2271 if (wt_relpath[0] == '\0') {
2272 /* Check out all files within the work tree. */
2273 *entry_type = GOT_OBJ_TYPE_TREE;
2274 *tree_relpath = strdup("");
2275 if (*tree_relpath == NULL) {
2276 err = got_error_from_errno("strdup");
2277 goto done;
2279 err = got_object_id_by_path(tree_id, repo,
2280 worktree->base_commit_id, worktree->path_prefix);
2281 if (err)
2282 goto done;
2283 return NULL;
2286 /* Check out a subset of files in the work tree. */
2288 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2289 is_root_wt ? "" : "/", wt_relpath) == -1) {
2290 err = got_error_from_errno("asprintf");
2291 goto done;
2294 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2295 in_repo_path);
2296 if (err)
2297 goto done;
2299 free(in_repo_path);
2300 in_repo_path = NULL;
2302 err = got_object_get_type(entry_type, repo, id);
2303 if (err)
2304 goto done;
2306 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2307 /* Check out a single file. */
2308 if (strchr(wt_relpath, '/') == NULL) {
2309 /* Check out a single file in work tree's root dir. */
2310 in_repo_path = strdup(worktree->path_prefix);
2311 if (in_repo_path == NULL) {
2312 err = got_error_from_errno("strdup");
2313 goto done;
2315 *tree_relpath = strdup("");
2316 if (*tree_relpath == NULL) {
2317 err = got_error_from_errno("strdup");
2318 goto done;
2320 } else {
2321 /* Check out a single file in a subdirectory. */
2322 err = got_path_dirname(tree_relpath, wt_relpath);
2323 if (err)
2324 return err;
2325 if (asprintf(&in_repo_path, "%s%s%s",
2326 worktree->path_prefix, is_root_wt ? "" : "/",
2327 *tree_relpath) == -1) {
2328 err = got_error_from_errno("asprintf");
2329 goto done;
2332 err = got_object_id_by_path(tree_id, repo,
2333 worktree->base_commit_id, in_repo_path);
2334 } else {
2335 /* Check out all files within a subdirectory. */
2336 *tree_id = got_object_id_dup(id);
2337 if (*tree_id == NULL) {
2338 err = got_error_from_errno("got_object_id_dup");
2339 goto done;
2341 *tree_relpath = strdup(wt_relpath);
2342 if (*tree_relpath == NULL) {
2343 err = got_error_from_errno("strdup");
2344 goto done;
2347 done:
2348 free(id);
2349 free(in_repo_path);
2350 if (err) {
2351 *entry_type = GOT_OBJ_TYPE_ANY;
2352 free(*tree_relpath);
2353 *tree_relpath = NULL;
2354 free(*tree_id);
2355 *tree_id = NULL;
2357 return err;
2360 static const struct got_error *
2361 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2362 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2363 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2364 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2366 const struct got_error *err = NULL;
2367 struct got_commit_object *commit = NULL;
2368 struct got_tree_object *tree = NULL;
2369 struct got_fileindex_diff_tree_cb diff_cb;
2370 struct diff_cb_arg arg;
2372 err = ref_base_commit(worktree, repo);
2373 if (err) {
2374 if (!(err->code == GOT_ERR_ERRNO &&
2375 (errno == EACCES || errno == EROFS)))
2376 goto done;
2377 err = (*progress_cb)(progress_arg,
2378 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2379 if (err)
2380 return err;
2383 err = got_object_open_as_commit(&commit, repo,
2384 worktree->base_commit_id);
2385 if (err)
2386 goto done;
2388 err = got_object_open_as_tree(&tree, repo, tree_id);
2389 if (err)
2390 goto done;
2392 if (entry_name &&
2393 got_object_tree_find_entry(tree, entry_name) == NULL) {
2394 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2395 goto done;
2398 diff_cb.diff_old_new = diff_old_new;
2399 diff_cb.diff_old = diff_old;
2400 diff_cb.diff_new = diff_new;
2401 arg.fileindex = fileindex;
2402 arg.worktree = worktree;
2403 arg.repo = repo;
2404 arg.progress_cb = progress_cb;
2405 arg.progress_arg = progress_arg;
2406 arg.cancel_cb = cancel_cb;
2407 arg.cancel_arg = cancel_arg;
2408 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2409 entry_name, repo, &diff_cb, &arg);
2410 done:
2411 if (tree)
2412 got_object_tree_close(tree);
2413 if (commit)
2414 got_object_commit_close(commit);
2415 return err;
2418 const struct got_error *
2419 got_worktree_checkout_files(struct got_worktree *worktree,
2420 struct got_pathlist_head *paths, struct got_repository *repo,
2421 got_worktree_checkout_cb progress_cb, void *progress_arg,
2422 got_cancel_cb cancel_cb, void *cancel_arg)
2424 const struct got_error *err = NULL, *sync_err, *unlockerr;
2425 struct got_commit_object *commit = NULL;
2426 struct got_tree_object *tree = NULL;
2427 struct got_fileindex *fileindex = NULL;
2428 char *fileindex_path = NULL;
2429 struct got_pathlist_entry *pe;
2430 struct tree_path_data {
2431 SIMPLEQ_ENTRY(tree_path_data) entry;
2432 struct got_object_id *tree_id;
2433 int entry_type;
2434 char *relpath;
2435 char *entry_name;
2436 } *tpd = NULL;
2437 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2439 SIMPLEQ_INIT(&tree_paths);
2441 err = lock_worktree(worktree, LOCK_EX);
2442 if (err)
2443 return err;
2445 /* Map all specified paths to in-repository trees. */
2446 TAILQ_FOREACH(pe, paths, entry) {
2447 tpd = malloc(sizeof(*tpd));
2448 if (tpd == NULL) {
2449 err = got_error_from_errno("malloc");
2450 goto done;
2453 err = find_tree_entry_for_checkout(&tpd->entry_type,
2454 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2455 if (err) {
2456 free(tpd);
2457 goto done;
2460 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2461 err = got_path_basename(&tpd->entry_name, pe->path);
2462 if (err) {
2463 free(tpd->relpath);
2464 free(tpd->tree_id);
2465 free(tpd);
2466 goto done;
2468 } else
2469 tpd->entry_name = NULL;
2471 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2475 * Read the file index.
2476 * Checking out files is supposed to be an idempotent operation.
2477 * If the on-disk file index is incomplete we will try to complete it.
2479 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2480 if (err)
2481 goto done;
2483 tpd = SIMPLEQ_FIRST(&tree_paths);
2484 TAILQ_FOREACH(pe, paths, entry) {
2485 struct bump_base_commit_id_arg bbc_arg;
2487 err = checkout_files(worktree, fileindex, tpd->relpath,
2488 tpd->tree_id, tpd->entry_name, repo,
2489 progress_cb, progress_arg, cancel_cb, cancel_arg);
2490 if (err)
2491 break;
2493 bbc_arg.base_commit_id = worktree->base_commit_id;
2494 bbc_arg.entry_name = tpd->entry_name;
2495 bbc_arg.path = pe->path;
2496 bbc_arg.path_len = pe->path_len;
2497 bbc_arg.progress_cb = progress_cb;
2498 bbc_arg.progress_arg = progress_arg;
2499 err = got_fileindex_for_each_entry_safe(fileindex,
2500 bump_base_commit_id, &bbc_arg);
2501 if (err)
2502 break;
2504 tpd = SIMPLEQ_NEXT(tpd, entry);
2506 sync_err = sync_fileindex(fileindex, fileindex_path);
2507 if (sync_err && err == NULL)
2508 err = sync_err;
2509 done:
2510 free(fileindex_path);
2511 if (tree)
2512 got_object_tree_close(tree);
2513 if (commit)
2514 got_object_commit_close(commit);
2515 if (fileindex)
2516 got_fileindex_free(fileindex);
2517 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2518 tpd = SIMPLEQ_FIRST(&tree_paths);
2519 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2520 free(tpd->relpath);
2521 free(tpd->tree_id);
2522 free(tpd);
2524 unlockerr = lock_worktree(worktree, LOCK_SH);
2525 if (unlockerr && err == NULL)
2526 err = unlockerr;
2527 return err;
2530 struct merge_file_cb_arg {
2531 struct got_worktree *worktree;
2532 struct got_fileindex *fileindex;
2533 got_worktree_checkout_cb progress_cb;
2534 void *progress_arg;
2535 got_cancel_cb cancel_cb;
2536 void *cancel_arg;
2537 const char *label_orig;
2538 struct got_object_id *commit_id2;
2541 static const struct got_error *
2542 merge_file_cb(void *arg, struct got_blob_object *blob1,
2543 struct got_blob_object *blob2, struct got_object_id *id1,
2544 struct got_object_id *id2, const char *path1, const char *path2,
2545 mode_t mode1, mode_t mode2, struct got_repository *repo)
2547 static const struct got_error *err = NULL;
2548 struct merge_file_cb_arg *a = arg;
2549 struct got_fileindex_entry *ie;
2550 char *ondisk_path = NULL;
2551 struct stat sb;
2552 unsigned char status;
2553 int local_changes_subsumed;
2555 if (blob1 && blob2) {
2556 ie = got_fileindex_entry_get(a->fileindex, path2,
2557 strlen(path2));
2558 if (ie == NULL)
2559 return (*a->progress_cb)(a->progress_arg,
2560 GOT_STATUS_MISSING, path2);
2562 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2563 path2) == -1)
2564 return got_error_from_errno("asprintf");
2566 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2567 repo);
2568 if (err)
2569 goto done;
2571 if (status == GOT_STATUS_DELETE) {
2572 err = (*a->progress_cb)(a->progress_arg,
2573 GOT_STATUS_MERGE, path2);
2574 goto done;
2576 if (status != GOT_STATUS_NO_CHANGE &&
2577 status != GOT_STATUS_MODIFY &&
2578 status != GOT_STATUS_CONFLICT &&
2579 status != GOT_STATUS_ADD) {
2580 err = (*a->progress_cb)(a->progress_arg, status, path2);
2581 goto done;
2584 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2585 err = merge_symlink(a->worktree, blob1,
2586 ondisk_path, path2, sb.st_mode, a->label_orig,
2587 blob2, a->commit_id2, repo, a->progress_cb,
2588 a->progress_arg);
2589 } else {
2590 err = merge_blob(&local_changes_subsumed, a->worktree,
2591 blob1, ondisk_path, path2, sb.st_mode,
2592 a->label_orig, blob2, a->commit_id2, repo,
2593 a->progress_cb, a->progress_arg);
2595 } else if (blob1) {
2596 ie = got_fileindex_entry_get(a->fileindex, path1,
2597 strlen(path1));
2598 if (ie == NULL)
2599 return (*a->progress_cb)(a->progress_arg,
2600 GOT_STATUS_MISSING, path1);
2602 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2603 path1) == -1)
2604 return got_error_from_errno("asprintf");
2606 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2607 repo);
2608 if (err)
2609 goto done;
2611 switch (status) {
2612 case GOT_STATUS_NO_CHANGE:
2613 err = (*a->progress_cb)(a->progress_arg,
2614 GOT_STATUS_DELETE, path1);
2615 if (err)
2616 goto done;
2617 err = remove_ondisk_file(a->worktree->root_path, path1);
2618 if (err)
2619 goto done;
2620 if (ie)
2621 got_fileindex_entry_mark_deleted_from_disk(ie);
2622 break;
2623 case GOT_STATUS_DELETE:
2624 case GOT_STATUS_MISSING:
2625 err = (*a->progress_cb)(a->progress_arg,
2626 GOT_STATUS_DELETE, path1);
2627 if (err)
2628 goto done;
2629 if (ie)
2630 got_fileindex_entry_mark_deleted_from_disk(ie);
2631 break;
2632 case GOT_STATUS_ADD:
2633 case GOT_STATUS_MODIFY:
2634 case GOT_STATUS_CONFLICT:
2635 err = (*a->progress_cb)(a->progress_arg,
2636 GOT_STATUS_CANNOT_DELETE, path1);
2637 if (err)
2638 goto done;
2639 break;
2640 case GOT_STATUS_OBSTRUCTED:
2641 err = (*a->progress_cb)(a->progress_arg, status, path1);
2642 if (err)
2643 goto done;
2644 break;
2645 default:
2646 break;
2648 } else if (blob2) {
2649 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2650 path2) == -1)
2651 return got_error_from_errno("asprintf");
2652 ie = got_fileindex_entry_get(a->fileindex, path2,
2653 strlen(path2));
2654 if (ie) {
2655 err = get_file_status(&status, &sb, ie, ondisk_path,
2656 -1, NULL, repo);
2657 if (err)
2658 goto done;
2659 if (status != GOT_STATUS_NO_CHANGE &&
2660 status != GOT_STATUS_MODIFY &&
2661 status != GOT_STATUS_CONFLICT &&
2662 status != GOT_STATUS_ADD) {
2663 err = (*a->progress_cb)(a->progress_arg,
2664 status, path2);
2665 goto done;
2667 if (S_ISLNK(mode2)) {
2668 err = merge_symlink(a->worktree, NULL,
2669 ondisk_path, path2, sb.st_mode,
2670 a->label_orig, blob2, a->commit_id2,
2671 repo, a->progress_cb, a->progress_arg);
2672 if (err)
2673 goto done;
2674 } else {
2675 err = merge_blob(&local_changes_subsumed,
2676 a->worktree, NULL, ondisk_path, path2,
2677 sb.st_mode, a->label_orig, blob2,
2678 a->commit_id2, repo, a->progress_cb,
2679 a->progress_arg);
2680 if (err)
2681 goto done;
2683 if (status == GOT_STATUS_DELETE) {
2684 err = got_fileindex_entry_update(ie,
2685 ondisk_path, blob2->id.sha1,
2686 a->worktree->base_commit_id->sha1, 0);
2687 if (err)
2688 goto done;
2690 } else {
2691 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2692 err = install_blob(a->worktree, ondisk_path, path2,
2693 /* XXX get this from parent tree! */
2694 GOT_DEFAULT_FILE_MODE,
2695 sb.st_mode, blob2, 0, 0, 0, repo,
2696 a->progress_cb, a->progress_arg);
2697 if (err)
2698 goto done;
2699 err = got_fileindex_entry_alloc(&ie, path2);
2700 if (err)
2701 goto done;
2702 err = got_fileindex_entry_update(ie, ondisk_path,
2703 NULL, NULL, 1);
2704 if (err) {
2705 got_fileindex_entry_free(ie);
2706 goto done;
2708 err = got_fileindex_entry_add(a->fileindex, ie);
2709 if (err) {
2710 got_fileindex_entry_free(ie);
2711 goto done;
2715 done:
2716 free(ondisk_path);
2717 return err;
2720 struct check_merge_ok_arg {
2721 struct got_worktree *worktree;
2722 struct got_repository *repo;
2725 static const struct got_error *
2726 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2728 const struct got_error *err = NULL;
2729 struct check_merge_ok_arg *a = arg;
2730 unsigned char status;
2731 struct stat sb;
2732 char *ondisk_path;
2734 /* Reject merges into a work tree with mixed base commits. */
2735 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2736 SHA1_DIGEST_LENGTH))
2737 return got_error(GOT_ERR_MIXED_COMMITS);
2739 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2740 == -1)
2741 return got_error_from_errno("asprintf");
2743 /* Reject merges into a work tree with conflicted files. */
2744 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2745 if (err)
2746 return err;
2747 if (status == GOT_STATUS_CONFLICT)
2748 return got_error(GOT_ERR_CONFLICTS);
2750 return NULL;
2753 static const struct got_error *
2754 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2755 const char *fileindex_path, struct got_object_id *commit_id1,
2756 struct got_object_id *commit_id2, struct got_repository *repo,
2757 got_worktree_checkout_cb progress_cb, void *progress_arg,
2758 got_cancel_cb cancel_cb, void *cancel_arg)
2760 const struct got_error *err = NULL, *sync_err;
2761 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2762 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2763 struct merge_file_cb_arg arg;
2764 char *label_orig = NULL;
2766 if (commit_id1) {
2767 char *id_str;
2769 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2770 worktree->path_prefix);
2771 if (err)
2772 goto done;
2774 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2775 if (err)
2776 goto done;
2778 err = got_object_id_str(&id_str, commit_id1);
2779 if (err)
2780 goto done;
2782 if (asprintf(&label_orig, "%s: commit %s",
2783 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2784 err = got_error_from_errno("asprintf");
2785 free(id_str);
2786 goto done;
2788 free(id_str);
2791 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2792 worktree->path_prefix);
2793 if (err)
2794 goto done;
2796 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2797 if (err)
2798 goto done;
2800 arg.worktree = worktree;
2801 arg.fileindex = fileindex;
2802 arg.progress_cb = progress_cb;
2803 arg.progress_arg = progress_arg;
2804 arg.cancel_cb = cancel_cb;
2805 arg.cancel_arg = cancel_arg;
2806 arg.label_orig = label_orig;
2807 arg.commit_id2 = commit_id2;
2808 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2809 sync_err = sync_fileindex(fileindex, fileindex_path);
2810 if (sync_err && err == NULL)
2811 err = sync_err;
2812 done:
2813 if (tree1)
2814 got_object_tree_close(tree1);
2815 if (tree2)
2816 got_object_tree_close(tree2);
2817 free(label_orig);
2818 return err;
2821 const struct got_error *
2822 got_worktree_merge_files(struct got_worktree *worktree,
2823 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2824 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2825 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2827 const struct got_error *err, *unlockerr;
2828 char *fileindex_path = NULL;
2829 struct got_fileindex *fileindex = NULL;
2830 struct check_merge_ok_arg mok_arg;
2832 err = lock_worktree(worktree, LOCK_EX);
2833 if (err)
2834 return err;
2836 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2837 if (err)
2838 goto done;
2840 mok_arg.worktree = worktree;
2841 mok_arg.repo = repo;
2842 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2843 &mok_arg);
2844 if (err)
2845 goto done;
2847 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2848 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2849 done:
2850 if (fileindex)
2851 got_fileindex_free(fileindex);
2852 free(fileindex_path);
2853 unlockerr = lock_worktree(worktree, LOCK_SH);
2854 if (unlockerr && err == NULL)
2855 err = unlockerr;
2856 return err;
2859 struct diff_dir_cb_arg {
2860 struct got_fileindex *fileindex;
2861 struct got_worktree *worktree;
2862 const char *status_path;
2863 size_t status_path_len;
2864 struct got_repository *repo;
2865 got_worktree_status_cb status_cb;
2866 void *status_arg;
2867 got_cancel_cb cancel_cb;
2868 void *cancel_arg;
2869 /* A pathlist containing per-directory pathlists of ignore patterns. */
2870 struct got_pathlist_head ignores;
2871 int report_unchanged;
2872 int no_ignores;
2875 static const struct got_error *
2876 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2877 int dirfd, const char *de_name,
2878 got_worktree_status_cb status_cb, void *status_arg,
2879 struct got_repository *repo, int report_unchanged)
2881 const struct got_error *err = NULL;
2882 unsigned char status = GOT_STATUS_NO_CHANGE;
2883 unsigned char staged_status = get_staged_status(ie);
2884 struct stat sb;
2885 struct got_object_id blob_id, commit_id, staged_blob_id;
2886 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2887 struct got_object_id *staged_blob_idp = NULL;
2889 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2890 if (err)
2891 return err;
2893 if (status == GOT_STATUS_NO_CHANGE &&
2894 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2895 return NULL;
2897 if (got_fileindex_entry_has_blob(ie)) {
2898 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2899 blob_idp = &blob_id;
2901 if (got_fileindex_entry_has_commit(ie)) {
2902 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2903 commit_idp = &commit_id;
2905 if (staged_status == GOT_STATUS_ADD ||
2906 staged_status == GOT_STATUS_MODIFY) {
2907 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2908 SHA1_DIGEST_LENGTH);
2909 staged_blob_idp = &staged_blob_id;
2912 return (*status_cb)(status_arg, status, staged_status,
2913 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2916 static const struct got_error *
2917 status_old_new(void *arg, struct got_fileindex_entry *ie,
2918 struct dirent *de, const char *parent_path, int dirfd)
2920 const struct got_error *err = NULL;
2921 struct diff_dir_cb_arg *a = arg;
2922 char *abspath;
2924 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2925 return got_error(GOT_ERR_CANCELLED);
2927 if (got_path_cmp(parent_path, a->status_path,
2928 strlen(parent_path), a->status_path_len) != 0 &&
2929 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2930 return NULL;
2932 if (parent_path[0]) {
2933 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2934 parent_path, de->d_name) == -1)
2935 return got_error_from_errno("asprintf");
2936 } else {
2937 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2938 de->d_name) == -1)
2939 return got_error_from_errno("asprintf");
2942 err = report_file_status(ie, abspath, dirfd, de->d_name,
2943 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2944 free(abspath);
2945 return err;
2948 static const struct got_error *
2949 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2951 struct diff_dir_cb_arg *a = arg;
2952 struct got_object_id blob_id, commit_id;
2953 unsigned char status;
2955 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2956 return got_error(GOT_ERR_CANCELLED);
2958 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2959 return NULL;
2961 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2962 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2963 if (got_fileindex_entry_has_file_on_disk(ie))
2964 status = GOT_STATUS_MISSING;
2965 else
2966 status = GOT_STATUS_DELETE;
2967 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2968 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2971 void
2972 free_ignorelist(struct got_pathlist_head *ignorelist)
2974 struct got_pathlist_entry *pe;
2976 TAILQ_FOREACH(pe, ignorelist, entry)
2977 free((char *)pe->path);
2978 got_pathlist_free(ignorelist);
2981 void
2982 free_ignores(struct got_pathlist_head *ignores)
2984 struct got_pathlist_entry *pe;
2986 TAILQ_FOREACH(pe, ignores, entry) {
2987 struct got_pathlist_head *ignorelist = pe->data;
2988 free_ignorelist(ignorelist);
2989 free((char *)pe->path);
2991 got_pathlist_free(ignores);
2994 static const struct got_error *
2995 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2997 const struct got_error *err = NULL;
2998 struct got_pathlist_entry *pe = NULL;
2999 struct got_pathlist_head *ignorelist;
3000 char *line = NULL, *pattern, *dirpath = NULL;
3001 size_t linesize = 0;
3002 ssize_t linelen;
3004 ignorelist = calloc(1, sizeof(*ignorelist));
3005 if (ignorelist == NULL)
3006 return got_error_from_errno("calloc");
3007 TAILQ_INIT(ignorelist);
3009 while ((linelen = getline(&line, &linesize, f)) != -1) {
3010 if (linelen > 0 && line[linelen - 1] == '\n')
3011 line[linelen - 1] = '\0';
3013 /* Git's ignores may contain comments. */
3014 if (line[0] == '#')
3015 continue;
3017 /* Git's negated patterns are not (yet?) supported. */
3018 if (line[0] == '!')
3019 continue;
3021 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3022 line) == -1) {
3023 err = got_error_from_errno("asprintf");
3024 goto done;
3026 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3027 if (err)
3028 goto done;
3030 if (ferror(f)) {
3031 err = got_error_from_errno("getline");
3032 goto done;
3035 dirpath = strdup(path);
3036 if (dirpath == NULL) {
3037 err = got_error_from_errno("strdup");
3038 goto done;
3040 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3041 done:
3042 free(line);
3043 if (err || pe == NULL) {
3044 free(dirpath);
3045 free_ignorelist(ignorelist);
3047 return err;
3050 int
3051 match_ignores(struct got_pathlist_head *ignores, const char *path)
3053 struct got_pathlist_entry *pe;
3055 /* Handle patterns which match in all directories. */
3056 TAILQ_FOREACH(pe, ignores, entry) {
3057 struct got_pathlist_head *ignorelist = pe->data;
3058 struct got_pathlist_entry *pi;
3060 TAILQ_FOREACH(pi, ignorelist, entry) {
3061 const char *p, *pattern = pi->path;
3063 if (strncmp(pattern, "**/", 3) != 0)
3064 continue;
3065 pattern += 3;
3066 p = path;
3067 while (*p) {
3068 if (fnmatch(pattern, p,
3069 FNM_PATHNAME | FNM_LEADING_DIR)) {
3070 /* Retry in next directory. */
3071 while (*p && *p != '/')
3072 p++;
3073 while (*p == '/')
3074 p++;
3075 continue;
3077 return 1;
3083 * The ignores pathlist contains ignore lists from children before
3084 * parents, so we can find the most specific ignorelist by walking
3085 * ignores backwards.
3087 pe = TAILQ_LAST(ignores, got_pathlist_head);
3088 while (pe) {
3089 if (got_path_is_child(path, pe->path, pe->path_len)) {
3090 struct got_pathlist_head *ignorelist = pe->data;
3091 struct got_pathlist_entry *pi;
3092 TAILQ_FOREACH(pi, ignorelist, entry) {
3093 const char *pattern = pi->path;
3094 int flags = FNM_LEADING_DIR;
3095 if (strstr(pattern, "/**/") == NULL)
3096 flags |= FNM_PATHNAME;
3097 if (fnmatch(pattern, path, flags))
3098 continue;
3099 return 1;
3102 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3105 return 0;
3108 static const struct got_error *
3109 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3110 const char *path, int dirfd, const char *ignores_filename)
3112 const struct got_error *err = NULL;
3113 char *ignorespath;
3114 int fd = -1;
3115 FILE *ignoresfile = NULL;
3117 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3118 path[0] ? "/" : "", ignores_filename) == -1)
3119 return got_error_from_errno("asprintf");
3121 if (dirfd != -1) {
3122 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3123 if (fd == -1) {
3124 if (errno != ENOENT && errno != EACCES)
3125 err = got_error_from_errno2("openat",
3126 ignorespath);
3127 } else {
3128 ignoresfile = fdopen(fd, "r");
3129 if (ignoresfile == NULL)
3130 err = got_error_from_errno2("fdopen",
3131 ignorespath);
3132 else {
3133 fd = -1;
3134 err = read_ignores(ignores, path, ignoresfile);
3137 } else {
3138 ignoresfile = fopen(ignorespath, "r");
3139 if (ignoresfile == NULL) {
3140 if (errno != ENOENT && errno != EACCES)
3141 err = got_error_from_errno2("fopen",
3142 ignorespath);
3143 } else
3144 err = read_ignores(ignores, path, ignoresfile);
3147 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3148 err = got_error_from_errno2("fclose", path);
3149 if (fd != -1 && close(fd) == -1 && err == NULL)
3150 err = got_error_from_errno2("close", path);
3151 free(ignorespath);
3152 return err;
3155 static const struct got_error *
3156 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3158 const struct got_error *err = NULL;
3159 struct diff_dir_cb_arg *a = arg;
3160 char *path = NULL;
3162 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3163 return got_error(GOT_ERR_CANCELLED);
3165 if (parent_path[0]) {
3166 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3167 return got_error_from_errno("asprintf");
3168 } else {
3169 path = de->d_name;
3172 if (de->d_type != DT_DIR &&
3173 got_path_is_child(path, a->status_path, a->status_path_len)
3174 && !match_ignores(&a->ignores, path))
3175 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3176 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3177 if (parent_path[0])
3178 free(path);
3179 return err;
3182 static const struct got_error *
3183 status_traverse(void *arg, const char *path, int dirfd)
3185 const struct got_error *err = NULL;
3186 struct diff_dir_cb_arg *a = arg;
3188 if (a->no_ignores)
3189 return NULL;
3191 err = add_ignores(&a->ignores, a->worktree->root_path,
3192 path, dirfd, ".cvsignore");
3193 if (err)
3194 return err;
3196 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3197 dirfd, ".gitignore");
3199 return err;
3202 static const struct got_error *
3203 report_single_file_status(const char *path, const char *ondisk_path,
3204 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3205 void *status_arg, struct got_repository *repo, int report_unchanged)
3207 struct got_fileindex_entry *ie;
3208 struct stat sb;
3210 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3211 if (ie)
3212 return report_file_status(ie, ondisk_path, -1, NULL,
3213 status_cb, status_arg, repo, report_unchanged);
3215 if (lstat(ondisk_path, &sb) == -1) {
3216 if (errno != ENOENT)
3217 return got_error_from_errno2("lstat", ondisk_path);
3218 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3219 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3220 return NULL;
3223 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3224 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3225 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3227 return NULL;
3230 static const struct got_error *
3231 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3232 const char *root_path, const char *path)
3234 const struct got_error *err;
3235 char *parent_path, *next_parent_path = NULL;
3237 err = add_ignores(ignores, root_path, "", -1,
3238 ".cvsignore");
3239 if (err)
3240 return err;
3242 err = add_ignores(ignores, root_path, "", -1,
3243 ".gitignore");
3244 if (err)
3245 return err;
3247 err = got_path_dirname(&parent_path, path);
3248 if (err) {
3249 if (err->code == GOT_ERR_BAD_PATH)
3250 return NULL; /* cannot traverse parent */
3251 return err;
3253 for (;;) {
3254 err = add_ignores(ignores, root_path, parent_path, -1,
3255 ".cvsignore");
3256 if (err)
3257 break;
3258 err = add_ignores(ignores, root_path, parent_path, -1,
3259 ".gitignore");
3260 if (err)
3261 break;
3262 err = got_path_dirname(&next_parent_path, parent_path);
3263 if (err) {
3264 if (err->code == GOT_ERR_BAD_PATH)
3265 err = NULL; /* traversed everything */
3266 break;
3268 free(parent_path);
3269 parent_path = next_parent_path;
3270 next_parent_path = NULL;
3273 free(parent_path);
3274 free(next_parent_path);
3275 return err;
3278 static const struct got_error *
3279 worktree_status(struct got_worktree *worktree, const char *path,
3280 struct got_fileindex *fileindex, struct got_repository *repo,
3281 got_worktree_status_cb status_cb, void *status_arg,
3282 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3283 int report_unchanged)
3285 const struct got_error *err = NULL;
3286 int fd = -1;
3287 struct got_fileindex_diff_dir_cb fdiff_cb;
3288 struct diff_dir_cb_arg arg;
3289 char *ondisk_path = NULL;
3291 TAILQ_INIT(&arg.ignores);
3293 if (asprintf(&ondisk_path, "%s%s%s",
3294 worktree->root_path, path[0] ? "/" : "", path) == -1)
3295 return got_error_from_errno("asprintf");
3297 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3298 if (fd == -1) {
3299 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3300 errno != ELOOP)
3301 err = got_error_from_errno2("open", ondisk_path);
3302 else
3303 err = report_single_file_status(path, ondisk_path,
3304 fileindex, status_cb, status_arg, repo,
3305 report_unchanged);
3306 } else {
3307 fdiff_cb.diff_old_new = status_old_new;
3308 fdiff_cb.diff_old = status_old;
3309 fdiff_cb.diff_new = status_new;
3310 fdiff_cb.diff_traverse = status_traverse;
3311 arg.fileindex = fileindex;
3312 arg.worktree = worktree;
3313 arg.status_path = path;
3314 arg.status_path_len = strlen(path);
3315 arg.repo = repo;
3316 arg.status_cb = status_cb;
3317 arg.status_arg = status_arg;
3318 arg.cancel_cb = cancel_cb;
3319 arg.cancel_arg = cancel_arg;
3320 arg.report_unchanged = report_unchanged;
3321 arg.no_ignores = no_ignores;
3322 if (!no_ignores) {
3323 err = add_ignores_from_parent_paths(&arg.ignores,
3324 worktree->root_path, path);
3325 if (err)
3326 goto done;
3328 err = got_fileindex_diff_dir(fileindex, fd,
3329 worktree->root_path, path, repo, &fdiff_cb, &arg);
3331 done:
3332 free_ignores(&arg.ignores);
3333 if (fd != -1 && close(fd) != 0 && err == NULL)
3334 err = got_error_from_errno("close");
3335 free(ondisk_path);
3336 return err;
3339 const struct got_error *
3340 got_worktree_status(struct got_worktree *worktree,
3341 struct got_pathlist_head *paths, struct got_repository *repo,
3342 got_worktree_status_cb status_cb, void *status_arg,
3343 got_cancel_cb cancel_cb, void *cancel_arg)
3345 const struct got_error *err = NULL;
3346 char *fileindex_path = NULL;
3347 struct got_fileindex *fileindex = NULL;
3348 struct got_pathlist_entry *pe;
3350 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3351 if (err)
3352 return err;
3354 TAILQ_FOREACH(pe, paths, entry) {
3355 err = worktree_status(worktree, pe->path, fileindex, repo,
3356 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3357 if (err)
3358 break;
3360 free(fileindex_path);
3361 got_fileindex_free(fileindex);
3362 return err;
3365 const struct got_error *
3366 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3367 const char *arg)
3369 const struct got_error *err = NULL;
3370 char *resolved = NULL, *cwd = NULL, *path = NULL;
3371 size_t len;
3372 struct stat sb;
3374 *wt_path = NULL;
3376 cwd = getcwd(NULL, 0);
3377 if (cwd == NULL)
3378 return got_error_from_errno("getcwd");
3380 if (lstat(arg, &sb) == -1) {
3381 if (errno != ENOENT) {
3382 err = got_error_from_errno2("lstat", arg);
3383 goto done;
3386 if (S_ISLNK(sb.st_mode)) {
3388 * We cannot use realpath(3) with symlinks since we want to
3389 * operate on the symlink itself.
3390 * But we can make the path absolute, assuming it is relative
3391 * to the current working directory, and then canonicalize it.
3393 char *abspath = NULL;
3394 char canonpath[PATH_MAX];
3395 if (!got_path_is_absolute(arg)) {
3396 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3397 err = got_error_from_errno("asprintf");
3398 goto done;
3402 err = got_canonpath(abspath ? abspath : arg, canonpath,
3403 sizeof(canonpath));
3404 if (err)
3405 goto done;
3406 resolved = strdup(canonpath);
3407 if (resolved == NULL) {
3408 err = got_error_from_errno("strdup");
3409 goto done;
3411 } else {
3412 resolved = realpath(arg, NULL);
3413 if (resolved == NULL) {
3414 if (errno != ENOENT) {
3415 err = got_error_from_errno2("realpath", arg);
3416 goto done;
3418 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3419 err = got_error_from_errno("asprintf");
3420 goto done;
3425 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3426 strlen(got_worktree_get_root_path(worktree)))) {
3427 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3428 goto done;
3431 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3432 err = got_path_skip_common_ancestor(&path,
3433 got_worktree_get_root_path(worktree), resolved);
3434 if (err)
3435 goto done;
3436 } else {
3437 path = strdup("");
3438 if (path == NULL) {
3439 err = got_error_from_errno("strdup");
3440 goto done;
3444 /* XXX status walk can't deal with trailing slash! */
3445 len = strlen(path);
3446 while (len > 0 && path[len - 1] == '/') {
3447 path[len - 1] = '\0';
3448 len--;
3450 done:
3451 free(resolved);
3452 free(cwd);
3453 if (err == NULL)
3454 *wt_path = path;
3455 else
3456 free(path);
3457 return err;
3460 struct schedule_addition_args {
3461 struct got_worktree *worktree;
3462 struct got_fileindex *fileindex;
3463 got_worktree_checkout_cb progress_cb;
3464 void *progress_arg;
3465 struct got_repository *repo;
3468 static const struct got_error *
3469 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3470 const char *relpath, struct got_object_id *blob_id,
3471 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3472 int dirfd, const char *de_name)
3474 struct schedule_addition_args *a = arg;
3475 const struct got_error *err = NULL;
3476 struct got_fileindex_entry *ie;
3477 struct stat sb;
3478 char *ondisk_path;
3480 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3481 relpath) == -1)
3482 return got_error_from_errno("asprintf");
3484 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3485 if (ie) {
3486 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3487 de_name, a->repo);
3488 if (err)
3489 goto done;
3490 /* Re-adding an existing entry is a no-op. */
3491 if (status == GOT_STATUS_ADD)
3492 goto done;
3493 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3494 if (err)
3495 goto done;
3498 if (status != GOT_STATUS_UNVERSIONED) {
3499 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3500 goto done;
3503 err = got_fileindex_entry_alloc(&ie, relpath);
3504 if (err)
3505 goto done;
3506 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3507 if (err) {
3508 got_fileindex_entry_free(ie);
3509 goto done;
3511 err = got_fileindex_entry_add(a->fileindex, ie);
3512 if (err) {
3513 got_fileindex_entry_free(ie);
3514 goto done;
3516 done:
3517 free(ondisk_path);
3518 if (err)
3519 return err;
3520 if (status == GOT_STATUS_ADD)
3521 return NULL;
3522 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3525 const struct got_error *
3526 got_worktree_schedule_add(struct got_worktree *worktree,
3527 struct got_pathlist_head *paths,
3528 got_worktree_checkout_cb progress_cb, void *progress_arg,
3529 struct got_repository *repo, int no_ignores)
3531 struct got_fileindex *fileindex = NULL;
3532 char *fileindex_path = NULL;
3533 const struct got_error *err = NULL, *sync_err, *unlockerr;
3534 struct got_pathlist_entry *pe;
3535 struct schedule_addition_args saa;
3537 err = lock_worktree(worktree, LOCK_EX);
3538 if (err)
3539 return err;
3541 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3542 if (err)
3543 goto done;
3545 saa.worktree = worktree;
3546 saa.fileindex = fileindex;
3547 saa.progress_cb = progress_cb;
3548 saa.progress_arg = progress_arg;
3549 saa.repo = repo;
3551 TAILQ_FOREACH(pe, paths, entry) {
3552 err = worktree_status(worktree, pe->path, fileindex, repo,
3553 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3554 if (err)
3555 break;
3557 sync_err = sync_fileindex(fileindex, fileindex_path);
3558 if (sync_err && err == NULL)
3559 err = sync_err;
3560 done:
3561 free(fileindex_path);
3562 if (fileindex)
3563 got_fileindex_free(fileindex);
3564 unlockerr = lock_worktree(worktree, LOCK_SH);
3565 if (unlockerr && err == NULL)
3566 err = unlockerr;
3567 return err;
3570 struct schedule_deletion_args {
3571 struct got_worktree *worktree;
3572 struct got_fileindex *fileindex;
3573 got_worktree_delete_cb progress_cb;
3574 void *progress_arg;
3575 struct got_repository *repo;
3576 int delete_local_mods;
3577 int keep_on_disk;
3580 static const struct got_error *
3581 schedule_for_deletion(void *arg, unsigned char status,
3582 unsigned char staged_status, const char *relpath,
3583 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3584 struct got_object_id *commit_id, int dirfd, const char *de_name)
3586 struct schedule_deletion_args *a = arg;
3587 const struct got_error *err = NULL;
3588 struct got_fileindex_entry *ie = NULL;
3589 struct stat sb;
3590 char *ondisk_path, *parent = NULL;
3592 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3593 if (ie == NULL)
3594 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3596 staged_status = get_staged_status(ie);
3597 if (staged_status != GOT_STATUS_NO_CHANGE) {
3598 if (staged_status == GOT_STATUS_DELETE)
3599 return NULL;
3600 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3603 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3604 relpath) == -1)
3605 return got_error_from_errno("asprintf");
3607 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3608 a->repo);
3609 if (err)
3610 goto done;
3612 if (status != GOT_STATUS_NO_CHANGE) {
3613 if (status == GOT_STATUS_DELETE)
3614 goto done;
3615 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3616 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3617 goto done;
3619 if (status != GOT_STATUS_MODIFY &&
3620 status != GOT_STATUS_MISSING) {
3621 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3622 goto done;
3626 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3627 if (dirfd != -1) {
3628 if (unlinkat(dirfd, de_name, 0) != 0) {
3629 err = got_error_from_errno2("unlinkat",
3630 ondisk_path);
3631 goto done;
3633 } else if (unlink(ondisk_path) != 0) {
3634 err = got_error_from_errno2("unlink", ondisk_path);
3635 goto done;
3638 parent = dirname(ondisk_path);
3640 if (parent == NULL) {
3641 err = got_error_from_errno2("dirname", ondisk_path);
3642 goto done;
3644 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3645 if (rmdir(parent) == -1) {
3646 if (errno != ENOTEMPTY)
3647 err = got_error_from_errno2("rmdir",
3648 parent);
3649 break;
3651 parent = dirname(parent);
3652 if (parent == NULL) {
3653 err = got_error_from_errno2("dirname", parent);
3654 goto done;
3659 got_fileindex_entry_mark_deleted_from_disk(ie);
3660 done:
3661 free(ondisk_path);
3662 if (err)
3663 return err;
3664 if (status == GOT_STATUS_DELETE)
3665 return NULL;
3666 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3667 staged_status, relpath);
3670 const struct got_error *
3671 got_worktree_schedule_delete(struct got_worktree *worktree,
3672 struct got_pathlist_head *paths, int delete_local_mods,
3673 got_worktree_delete_cb progress_cb, void *progress_arg,
3674 struct got_repository *repo, int keep_on_disk)
3676 struct got_fileindex *fileindex = NULL;
3677 char *fileindex_path = NULL;
3678 const struct got_error *err = NULL, *sync_err, *unlockerr;
3679 struct got_pathlist_entry *pe;
3680 struct schedule_deletion_args sda;
3682 err = lock_worktree(worktree, LOCK_EX);
3683 if (err)
3684 return err;
3686 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3687 if (err)
3688 goto done;
3690 sda.worktree = worktree;
3691 sda.fileindex = fileindex;
3692 sda.progress_cb = progress_cb;
3693 sda.progress_arg = progress_arg;
3694 sda.repo = repo;
3695 sda.delete_local_mods = delete_local_mods;
3696 sda.keep_on_disk = keep_on_disk;
3698 TAILQ_FOREACH(pe, paths, entry) {
3699 err = worktree_status(worktree, pe->path, fileindex, repo,
3700 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3701 if (err)
3702 break;
3704 sync_err = sync_fileindex(fileindex, fileindex_path);
3705 if (sync_err && err == NULL)
3706 err = sync_err;
3707 done:
3708 free(fileindex_path);
3709 if (fileindex)
3710 got_fileindex_free(fileindex);
3711 unlockerr = lock_worktree(worktree, LOCK_SH);
3712 if (unlockerr && err == NULL)
3713 err = unlockerr;
3714 return err;
3717 static const struct got_error *
3718 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3720 const struct got_error *err = NULL;
3721 char *line = NULL;
3722 size_t linesize = 0, n;
3723 ssize_t linelen;
3725 linelen = getline(&line, &linesize, infile);
3726 if (linelen == -1) {
3727 if (ferror(infile)) {
3728 err = got_error_from_errno("getline");
3729 goto done;
3731 return NULL;
3733 if (outfile) {
3734 n = fwrite(line, 1, linelen, outfile);
3735 if (n != linelen) {
3736 err = got_ferror(outfile, GOT_ERR_IO);
3737 goto done;
3740 if (rejectfile) {
3741 n = fwrite(line, 1, linelen, rejectfile);
3742 if (n != linelen)
3743 err = got_ferror(outfile, GOT_ERR_IO);
3745 done:
3746 free(line);
3747 return err;
3750 static const struct got_error *
3751 skip_one_line(FILE *f)
3753 char *line = NULL;
3754 size_t linesize = 0;
3755 ssize_t linelen;
3757 linelen = getline(&line, &linesize, f);
3758 if (linelen == -1) {
3759 if (ferror(f))
3760 return got_error_from_errno("getline");
3761 return NULL;
3763 free(line);
3764 return NULL;
3767 static const struct got_error *
3768 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3769 int start_old, int end_old, int start_new, int end_new,
3770 FILE *outfile, FILE *rejectfile)
3772 const struct got_error *err;
3774 /* Copy old file's lines leading up to patch. */
3775 while (!feof(f1) && *line_cur1 < start_old) {
3776 err = copy_one_line(f1, outfile, NULL);
3777 if (err)
3778 return err;
3779 (*line_cur1)++;
3781 /* Skip new file's lines leading up to patch. */
3782 while (!feof(f2) && *line_cur2 < start_new) {
3783 if (rejectfile)
3784 err = copy_one_line(f2, NULL, rejectfile);
3785 else
3786 err = skip_one_line(f2);
3787 if (err)
3788 return err;
3789 (*line_cur2)++;
3791 /* Copy patched lines. */
3792 while (!feof(f2) && *line_cur2 <= end_new) {
3793 err = copy_one_line(f2, outfile, NULL);
3794 if (err)
3795 return err;
3796 (*line_cur2)++;
3798 /* Skip over old file's replaced lines. */
3799 while (!feof(f1) && *line_cur1 <= end_old) {
3800 if (rejectfile)
3801 err = copy_one_line(f1, NULL, rejectfile);
3802 else
3803 err = skip_one_line(f1);
3804 if (err)
3805 return err;
3806 (*line_cur1)++;
3809 return NULL;
3812 static const struct got_error *
3813 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3814 FILE *outfile, FILE *rejectfile)
3816 const struct got_error *err;
3818 if (outfile) {
3819 /* Copy old file's lines until EOF. */
3820 while (!feof(f1)) {
3821 err = copy_one_line(f1, outfile, NULL);
3822 if (err)
3823 return err;
3824 (*line_cur1)++;
3827 if (rejectfile) {
3828 /* Copy new file's lines until EOF. */
3829 while (!feof(f2)) {
3830 err = copy_one_line(f2, NULL, rejectfile);
3831 if (err)
3832 return err;
3833 (*line_cur2)++;
3837 return NULL;
3840 static const struct got_error *
3841 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3842 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3843 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3844 int *line_cur2, FILE *outfile, FILE *rejectfile,
3845 got_worktree_patch_cb patch_cb, void *patch_arg)
3847 const struct got_error *err = NULL;
3848 int start_old = change->cv.a;
3849 int end_old = change->cv.b;
3850 int start_new = change->cv.c;
3851 int end_new = change->cv.d;
3852 long pos1, pos2;
3853 FILE *hunkfile;
3855 *choice = GOT_PATCH_CHOICE_NONE;
3857 hunkfile = got_opentemp();
3858 if (hunkfile == NULL)
3859 return got_error_from_errno("got_opentemp");
3861 pos1 = ftell(f1);
3862 pos2 = ftell(f2);
3864 /* XXX TODO needs error checking */
3865 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3867 if (fseek(f1, pos1, SEEK_SET) == -1) {
3868 err = got_ferror(f1, GOT_ERR_IO);
3869 goto done;
3871 if (fseek(f2, pos2, SEEK_SET) == -1) {
3872 err = got_ferror(f1, GOT_ERR_IO);
3873 goto done;
3875 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3876 err = got_ferror(hunkfile, GOT_ERR_IO);
3877 goto done;
3880 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3881 hunkfile, n, nchanges);
3882 if (err)
3883 goto done;
3885 switch (*choice) {
3886 case GOT_PATCH_CHOICE_YES:
3887 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3888 end_old, start_new, end_new, outfile, rejectfile);
3889 break;
3890 case GOT_PATCH_CHOICE_NO:
3891 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3892 end_old, start_new, end_new, rejectfile, outfile);
3893 break;
3894 case GOT_PATCH_CHOICE_QUIT:
3895 break;
3896 default:
3897 err = got_error(GOT_ERR_PATCH_CHOICE);
3898 break;
3900 done:
3901 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3902 err = got_error_from_errno("fclose");
3903 return err;
3906 struct revert_file_args {
3907 struct got_worktree *worktree;
3908 struct got_fileindex *fileindex;
3909 got_worktree_checkout_cb progress_cb;
3910 void *progress_arg;
3911 got_worktree_patch_cb patch_cb;
3912 void *patch_arg;
3913 struct got_repository *repo;
3916 static const struct got_error *
3917 create_patched_content(char **path_outfile, int reverse_patch,
3918 struct got_object_id *blob_id, const char *path2,
3919 int dirfd2, const char *de_name2,
3920 const char *relpath, struct got_repository *repo,
3921 got_worktree_patch_cb patch_cb, void *patch_arg)
3923 const struct got_error *err;
3924 struct got_blob_object *blob = NULL;
3925 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3926 int fd2 = -1;
3927 char *path1 = NULL, *id_str = NULL;
3928 struct stat sb1, sb2;
3929 struct got_diff_changes *changes = NULL;
3930 struct got_diff_state *ds = NULL;
3931 struct got_diff_args *args = NULL;
3932 struct got_diff_change *change;
3933 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3934 int n = 0;
3936 *path_outfile = NULL;
3938 err = got_object_id_str(&id_str, blob_id);
3939 if (err)
3940 return err;
3942 if (dirfd2 != -1) {
3943 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3944 if (fd2 == -1) {
3945 err = got_error_from_errno2("openat", path2);
3946 goto done;
3948 } else {
3949 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3950 if (fd2 == -1) {
3951 err = got_error_from_errno2("open", path2);
3952 goto done;
3955 if (fstat(fd2, &sb2) == -1) {
3956 err = got_error_from_errno2("fstat", path2);
3957 goto done;
3960 f2 = fdopen(fd2, "r");
3961 if (f2 == NULL) {
3962 err = got_error_from_errno2("fdopen", path2);
3963 goto done;
3965 fd2 = -1;
3967 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3968 if (err)
3969 goto done;
3971 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3972 if (err)
3973 goto done;
3975 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3976 if (err)
3977 goto done;
3979 if (stat(path1, &sb1) == -1) {
3980 err = got_error_from_errno2("stat", path1);
3981 goto done;
3984 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3985 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3986 if (err)
3987 goto done;
3989 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3990 if (err)
3991 goto done;
3993 if (fseek(f1, 0L, SEEK_SET) == -1)
3994 return got_ferror(f1, GOT_ERR_IO);
3995 if (fseek(f2, 0L, SEEK_SET) == -1)
3996 return got_ferror(f2, GOT_ERR_IO);
3997 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3998 int choice;
3999 err = apply_or_reject_change(&choice, change, ++n,
4000 changes->nchanges, ds, args, diff_flags, relpath,
4001 f1, f2, &line_cur1, &line_cur2,
4002 reverse_patch ? NULL : outfile,
4003 reverse_patch ? outfile : NULL,
4004 patch_cb, patch_arg);
4005 if (err)
4006 goto done;
4007 if (choice == GOT_PATCH_CHOICE_YES)
4008 have_content = 1;
4009 else if (choice == GOT_PATCH_CHOICE_QUIT)
4010 break;
4012 if (have_content) {
4013 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4014 reverse_patch ? NULL : outfile,
4015 reverse_patch ? outfile : NULL);
4016 if (err)
4017 goto done;
4019 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4020 err = got_error_from_errno2("chmod", path2);
4021 goto done;
4024 done:
4025 free(id_str);
4026 if (blob)
4027 got_object_blob_close(blob);
4028 if (f1 && fclose(f1) == EOF && err == NULL)
4029 err = got_error_from_errno2("fclose", path1);
4030 if (f2 && fclose(f2) == EOF && err == NULL)
4031 err = got_error_from_errno2("fclose", path2);
4032 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4033 err = got_error_from_errno2("close", path2);
4034 if (outfile && fclose(outfile) == EOF && err == NULL)
4035 err = got_error_from_errno2("fclose", *path_outfile);
4036 if (path1 && unlink(path1) == -1 && err == NULL)
4037 err = got_error_from_errno2("unlink", path1);
4038 if (err || !have_content) {
4039 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4040 err = got_error_from_errno2("unlink", *path_outfile);
4041 free(*path_outfile);
4042 *path_outfile = NULL;
4044 free(args);
4045 if (ds) {
4046 got_diff_state_free(ds);
4047 free(ds);
4049 if (changes)
4050 got_diff_free_changes(changes);
4051 free(path1);
4052 return err;
4055 static const struct got_error *
4056 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4057 const char *relpath, struct got_object_id *blob_id,
4058 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4059 int dirfd, const char *de_name)
4061 struct revert_file_args *a = arg;
4062 const struct got_error *err = NULL;
4063 char *parent_path = NULL;
4064 struct got_fileindex_entry *ie;
4065 struct got_tree_object *tree = NULL;
4066 struct got_object_id *tree_id = NULL;
4067 const struct got_tree_entry *te = NULL;
4068 char *tree_path = NULL, *te_name;
4069 char *ondisk_path = NULL, *path_content = NULL;
4070 struct got_blob_object *blob = NULL;
4072 /* Reverting a staged deletion is a no-op. */
4073 if (status == GOT_STATUS_DELETE &&
4074 staged_status != GOT_STATUS_NO_CHANGE)
4075 return NULL;
4077 if (status == GOT_STATUS_UNVERSIONED)
4078 return (*a->progress_cb)(a->progress_arg,
4079 GOT_STATUS_UNVERSIONED, relpath);
4081 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4082 if (ie == NULL)
4083 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4085 /* Construct in-repository path of tree which contains this blob. */
4086 err = got_path_dirname(&parent_path, ie->path);
4087 if (err) {
4088 if (err->code != GOT_ERR_BAD_PATH)
4089 goto done;
4090 parent_path = strdup("/");
4091 if (parent_path == NULL) {
4092 err = got_error_from_errno("strdup");
4093 goto done;
4096 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4097 tree_path = strdup(parent_path);
4098 if (tree_path == NULL) {
4099 err = got_error_from_errno("strdup");
4100 goto done;
4102 } else {
4103 if (got_path_is_root_dir(parent_path)) {
4104 tree_path = strdup(a->worktree->path_prefix);
4105 if (tree_path == NULL) {
4106 err = got_error_from_errno("strdup");
4107 goto done;
4109 } else {
4110 if (asprintf(&tree_path, "%s/%s",
4111 a->worktree->path_prefix, parent_path) == -1) {
4112 err = got_error_from_errno("asprintf");
4113 goto done;
4118 err = got_object_id_by_path(&tree_id, a->repo,
4119 a->worktree->base_commit_id, tree_path);
4120 if (err) {
4121 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4122 (status == GOT_STATUS_ADD ||
4123 staged_status == GOT_STATUS_ADD)))
4124 goto done;
4125 } else {
4126 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4127 if (err)
4128 goto done;
4130 te_name = basename(ie->path);
4131 if (te_name == NULL) {
4132 err = got_error_from_errno2("basename", ie->path);
4133 goto done;
4136 te = got_object_tree_find_entry(tree, te_name);
4137 if (te == NULL && status != GOT_STATUS_ADD &&
4138 staged_status != GOT_STATUS_ADD) {
4139 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4140 goto done;
4144 switch (status) {
4145 case GOT_STATUS_ADD:
4146 if (a->patch_cb) {
4147 int choice = GOT_PATCH_CHOICE_NONE;
4148 err = (*a->patch_cb)(&choice, a->patch_arg,
4149 status, ie->path, NULL, 1, 1);
4150 if (err)
4151 goto done;
4152 if (choice != GOT_PATCH_CHOICE_YES)
4153 break;
4155 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4156 ie->path);
4157 if (err)
4158 goto done;
4159 got_fileindex_entry_remove(a->fileindex, ie);
4160 break;
4161 case GOT_STATUS_DELETE:
4162 if (a->patch_cb) {
4163 int choice = GOT_PATCH_CHOICE_NONE;
4164 err = (*a->patch_cb)(&choice, a->patch_arg,
4165 status, ie->path, NULL, 1, 1);
4166 if (err)
4167 goto done;
4168 if (choice != GOT_PATCH_CHOICE_YES)
4169 break;
4171 /* fall through */
4172 case GOT_STATUS_MODIFY:
4173 case GOT_STATUS_MODE_CHANGE:
4174 case GOT_STATUS_CONFLICT:
4175 case GOT_STATUS_MISSING: {
4176 struct got_object_id id;
4177 if (staged_status == GOT_STATUS_ADD ||
4178 staged_status == GOT_STATUS_MODIFY) {
4179 memcpy(id.sha1, ie->staged_blob_sha1,
4180 SHA1_DIGEST_LENGTH);
4181 } else
4182 memcpy(id.sha1, ie->blob_sha1,
4183 SHA1_DIGEST_LENGTH);
4184 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4185 if (err)
4186 goto done;
4188 if (asprintf(&ondisk_path, "%s/%s",
4189 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4190 err = got_error_from_errno("asprintf");
4191 goto done;
4194 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4195 status == GOT_STATUS_CONFLICT)) {
4196 err = create_patched_content(&path_content, 1, &id,
4197 ondisk_path, dirfd, de_name, ie->path, a->repo,
4198 a->patch_cb, a->patch_arg);
4199 if (err || path_content == NULL)
4200 break;
4201 if (rename(path_content, ondisk_path) == -1) {
4202 err = got_error_from_errno3("rename",
4203 path_content, ondisk_path);
4204 goto done;
4206 } else {
4207 err = install_blob(a->worktree, ondisk_path, ie->path,
4208 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4209 got_fileindex_perms_to_st(ie), blob, 0, 1, 0,
4210 a->repo, a->progress_cb, a->progress_arg);
4211 if (err)
4212 goto done;
4213 if (status == GOT_STATUS_DELETE ||
4214 status == GOT_STATUS_MODE_CHANGE) {
4215 err = got_fileindex_entry_update(ie,
4216 ondisk_path, blob->id.sha1,
4217 a->worktree->base_commit_id->sha1, 1);
4218 if (err)
4219 goto done;
4222 break;
4224 default:
4225 break;
4227 done:
4228 free(ondisk_path);
4229 free(path_content);
4230 free(parent_path);
4231 free(tree_path);
4232 if (blob)
4233 got_object_blob_close(blob);
4234 if (tree)
4235 got_object_tree_close(tree);
4236 free(tree_id);
4237 return err;
4240 const struct got_error *
4241 got_worktree_revert(struct got_worktree *worktree,
4242 struct got_pathlist_head *paths,
4243 got_worktree_checkout_cb progress_cb, void *progress_arg,
4244 got_worktree_patch_cb patch_cb, void *patch_arg,
4245 struct got_repository *repo)
4247 struct got_fileindex *fileindex = NULL;
4248 char *fileindex_path = NULL;
4249 const struct got_error *err = NULL, *unlockerr = NULL;
4250 const struct got_error *sync_err = NULL;
4251 struct got_pathlist_entry *pe;
4252 struct revert_file_args rfa;
4254 err = lock_worktree(worktree, LOCK_EX);
4255 if (err)
4256 return err;
4258 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4259 if (err)
4260 goto done;
4262 rfa.worktree = worktree;
4263 rfa.fileindex = fileindex;
4264 rfa.progress_cb = progress_cb;
4265 rfa.progress_arg = progress_arg;
4266 rfa.patch_cb = patch_cb;
4267 rfa.patch_arg = patch_arg;
4268 rfa.repo = repo;
4269 TAILQ_FOREACH(pe, paths, entry) {
4270 err = worktree_status(worktree, pe->path, fileindex, repo,
4271 revert_file, &rfa, NULL, NULL, 0, 0);
4272 if (err)
4273 break;
4275 sync_err = sync_fileindex(fileindex, fileindex_path);
4276 if (sync_err && err == NULL)
4277 err = sync_err;
4278 done:
4279 free(fileindex_path);
4280 if (fileindex)
4281 got_fileindex_free(fileindex);
4282 unlockerr = lock_worktree(worktree, LOCK_SH);
4283 if (unlockerr && err == NULL)
4284 err = unlockerr;
4285 return err;
4288 static void
4289 free_commitable(struct got_commitable *ct)
4291 free(ct->path);
4292 free(ct->in_repo_path);
4293 free(ct->ondisk_path);
4294 free(ct->blob_id);
4295 free(ct->base_blob_id);
4296 free(ct->staged_blob_id);
4297 free(ct->base_commit_id);
4298 free(ct);
4301 struct collect_commitables_arg {
4302 struct got_pathlist_head *commitable_paths;
4303 struct got_repository *repo;
4304 struct got_worktree *worktree;
4305 int have_staged_files;
4308 static const struct got_error *
4309 collect_commitables(void *arg, unsigned char status,
4310 unsigned char staged_status, const char *relpath,
4311 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4312 struct got_object_id *commit_id, int dirfd, const char *de_name)
4314 struct collect_commitables_arg *a = arg;
4315 const struct got_error *err = NULL;
4316 struct got_commitable *ct = NULL;
4317 struct got_pathlist_entry *new = NULL;
4318 char *parent_path = NULL, *path = NULL;
4319 struct stat sb;
4321 if (a->have_staged_files) {
4322 if (staged_status != GOT_STATUS_MODIFY &&
4323 staged_status != GOT_STATUS_ADD &&
4324 staged_status != GOT_STATUS_DELETE)
4325 return NULL;
4326 } else {
4327 if (status == GOT_STATUS_CONFLICT)
4328 return got_error(GOT_ERR_COMMIT_CONFLICT);
4330 if (status != GOT_STATUS_MODIFY &&
4331 status != GOT_STATUS_MODE_CHANGE &&
4332 status != GOT_STATUS_ADD &&
4333 status != GOT_STATUS_DELETE)
4334 return NULL;
4337 if (asprintf(&path, "/%s", relpath) == -1) {
4338 err = got_error_from_errno("asprintf");
4339 goto done;
4341 if (strcmp(path, "/") == 0) {
4342 parent_path = strdup("");
4343 if (parent_path == NULL)
4344 return got_error_from_errno("strdup");
4345 } else {
4346 err = got_path_dirname(&parent_path, path);
4347 if (err)
4348 return err;
4351 ct = calloc(1, sizeof(*ct));
4352 if (ct == NULL) {
4353 err = got_error_from_errno("calloc");
4354 goto done;
4357 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4358 relpath) == -1) {
4359 err = got_error_from_errno("asprintf");
4360 goto done;
4362 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4363 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4364 } else {
4365 if (dirfd != -1) {
4366 if (fstatat(dirfd, de_name, &sb,
4367 AT_SYMLINK_NOFOLLOW) == -1) {
4368 err = got_error_from_errno2("fstatat",
4369 ct->ondisk_path);
4370 goto done;
4372 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4373 err = got_error_from_errno2("lstat", ct->ondisk_path);
4374 goto done;
4376 ct->mode = sb.st_mode;
4379 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4380 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4381 relpath) == -1) {
4382 err = got_error_from_errno("asprintf");
4383 goto done;
4386 ct->status = status;
4387 ct->staged_status = staged_status;
4388 ct->blob_id = NULL; /* will be filled in when blob gets created */
4389 if (ct->status != GOT_STATUS_ADD &&
4390 ct->staged_status != GOT_STATUS_ADD) {
4391 ct->base_blob_id = got_object_id_dup(blob_id);
4392 if (ct->base_blob_id == NULL) {
4393 err = got_error_from_errno("got_object_id_dup");
4394 goto done;
4396 ct->base_commit_id = got_object_id_dup(commit_id);
4397 if (ct->base_commit_id == NULL) {
4398 err = got_error_from_errno("got_object_id_dup");
4399 goto done;
4402 if (ct->staged_status == GOT_STATUS_ADD ||
4403 ct->staged_status == GOT_STATUS_MODIFY) {
4404 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4405 if (ct->staged_blob_id == NULL) {
4406 err = got_error_from_errno("got_object_id_dup");
4407 goto done;
4410 ct->path = strdup(path);
4411 if (ct->path == NULL) {
4412 err = got_error_from_errno("strdup");
4413 goto done;
4415 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4416 done:
4417 if (ct && (err || new == NULL))
4418 free_commitable(ct);
4419 free(parent_path);
4420 free(path);
4421 return err;
4424 static const struct got_error *write_tree(struct got_object_id **, int *,
4425 struct got_tree_object *, const char *, struct got_pathlist_head *,
4426 got_worktree_status_cb status_cb, void *status_arg,
4427 struct got_repository *);
4429 static const struct got_error *
4430 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4431 struct got_tree_entry *te, const char *parent_path,
4432 struct got_pathlist_head *commitable_paths,
4433 got_worktree_status_cb status_cb, void *status_arg,
4434 struct got_repository *repo)
4436 const struct got_error *err = NULL;
4437 struct got_tree_object *subtree;
4438 char *subpath;
4440 if (asprintf(&subpath, "%s%s%s", parent_path,
4441 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4442 return got_error_from_errno("asprintf");
4444 err = got_object_open_as_tree(&subtree, repo, &te->id);
4445 if (err)
4446 return err;
4448 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4449 commitable_paths, status_cb, status_arg, repo);
4450 got_object_tree_close(subtree);
4451 free(subpath);
4452 return err;
4455 static const struct got_error *
4456 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4458 const struct got_error *err = NULL;
4459 char *ct_parent_path = NULL;
4461 *match = 0;
4463 if (strchr(ct->in_repo_path, '/') == NULL) {
4464 *match = got_path_is_root_dir(path);
4465 return NULL;
4468 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4469 if (err)
4470 return err;
4471 *match = (strcmp(path, ct_parent_path) == 0);
4472 free(ct_parent_path);
4473 return err;
4476 static mode_t
4477 get_ct_file_mode(struct got_commitable *ct)
4479 if (S_ISLNK(ct->mode))
4480 return S_IFLNK;
4482 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4485 static const struct got_error *
4486 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4487 struct got_tree_entry *te, struct got_commitable *ct)
4489 const struct got_error *err = NULL;
4491 *new_te = NULL;
4493 err = got_object_tree_entry_dup(new_te, te);
4494 if (err)
4495 goto done;
4497 (*new_te)->mode = get_ct_file_mode(ct);
4499 if (ct->staged_status == GOT_STATUS_MODIFY)
4500 memcpy(&(*new_te)->id, ct->staged_blob_id,
4501 sizeof((*new_te)->id));
4502 else
4503 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4504 done:
4505 if (err && *new_te) {
4506 free(*new_te);
4507 *new_te = NULL;
4509 return err;
4512 static const struct got_error *
4513 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4514 struct got_commitable *ct)
4516 const struct got_error *err = NULL;
4517 char *ct_name;
4519 *new_te = NULL;
4521 *new_te = calloc(1, sizeof(**new_te));
4522 if (*new_te == NULL)
4523 return got_error_from_errno("calloc");
4525 ct_name = basename(ct->path);
4526 if (ct_name == NULL) {
4527 err = got_error_from_errno2("basename", ct->path);
4528 goto done;
4530 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4531 sizeof((*new_te)->name)) {
4532 err = got_error(GOT_ERR_NO_SPACE);
4533 goto done;
4536 (*new_te)->mode = get_ct_file_mode(ct);
4538 if (ct->staged_status == GOT_STATUS_ADD)
4539 memcpy(&(*new_te)->id, ct->staged_blob_id,
4540 sizeof((*new_te)->id));
4541 else
4542 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4543 done:
4544 if (err && *new_te) {
4545 free(*new_te);
4546 *new_te = NULL;
4548 return err;
4551 static const struct got_error *
4552 insert_tree_entry(struct got_tree_entry *new_te,
4553 struct got_pathlist_head *paths)
4555 const struct got_error *err = NULL;
4556 struct got_pathlist_entry *new_pe;
4558 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4559 if (err)
4560 return err;
4561 if (new_pe == NULL)
4562 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4563 return NULL;
4566 static const struct got_error *
4567 report_ct_status(struct got_commitable *ct,
4568 got_worktree_status_cb status_cb, void *status_arg)
4570 const char *ct_path = ct->path;
4571 unsigned char status;
4573 while (ct_path[0] == '/')
4574 ct_path++;
4576 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4577 status = ct->staged_status;
4578 else
4579 status = ct->status;
4581 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4582 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4585 static const struct got_error *
4586 match_modified_subtree(int *modified, struct got_tree_entry *te,
4587 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4589 const struct got_error *err = NULL;
4590 struct got_pathlist_entry *pe;
4591 char *te_path;
4593 *modified = 0;
4595 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4596 got_path_is_root_dir(base_tree_path) ? "" : "/",
4597 te->name) == -1)
4598 return got_error_from_errno("asprintf");
4600 TAILQ_FOREACH(pe, commitable_paths, entry) {
4601 struct got_commitable *ct = pe->data;
4602 *modified = got_path_is_child(ct->in_repo_path, te_path,
4603 strlen(te_path));
4604 if (*modified)
4605 break;
4608 free(te_path);
4609 return err;
4612 static const struct got_error *
4613 match_deleted_or_modified_ct(struct got_commitable **ctp,
4614 struct got_tree_entry *te, const char *base_tree_path,
4615 struct got_pathlist_head *commitable_paths)
4617 const struct got_error *err = NULL;
4618 struct got_pathlist_entry *pe;
4620 *ctp = NULL;
4622 TAILQ_FOREACH(pe, commitable_paths, entry) {
4623 struct got_commitable *ct = pe->data;
4624 char *ct_name = NULL;
4625 int path_matches;
4627 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4628 if (ct->status != GOT_STATUS_MODIFY &&
4629 ct->status != GOT_STATUS_MODE_CHANGE &&
4630 ct->status != GOT_STATUS_DELETE)
4631 continue;
4632 } else {
4633 if (ct->staged_status != GOT_STATUS_MODIFY &&
4634 ct->staged_status != GOT_STATUS_DELETE)
4635 continue;
4638 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4639 continue;
4641 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4642 if (err)
4643 return err;
4644 if (!path_matches)
4645 continue;
4647 ct_name = basename(pe->path);
4648 if (ct_name == NULL)
4649 return got_error_from_errno2("basename", pe->path);
4651 if (strcmp(te->name, ct_name) != 0)
4652 continue;
4654 *ctp = ct;
4655 break;
4658 return err;
4661 static const struct got_error *
4662 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4663 const char *child_path, const char *path_base_tree,
4664 struct got_pathlist_head *commitable_paths,
4665 got_worktree_status_cb status_cb, void *status_arg,
4666 struct got_repository *repo)
4668 const struct got_error *err = NULL;
4669 struct got_tree_entry *new_te;
4670 char *subtree_path;
4671 struct got_object_id *id = NULL;
4672 int nentries;
4674 *new_tep = NULL;
4676 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4677 got_path_is_root_dir(path_base_tree) ? "" : "/",
4678 child_path) == -1)
4679 return got_error_from_errno("asprintf");
4681 new_te = calloc(1, sizeof(*new_te));
4682 if (new_te == NULL)
4683 return got_error_from_errno("calloc");
4684 new_te->mode = S_IFDIR;
4686 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4687 sizeof(new_te->name)) {
4688 err = got_error(GOT_ERR_NO_SPACE);
4689 goto done;
4691 err = write_tree(&id, &nentries, NULL, subtree_path,
4692 commitable_paths, status_cb, status_arg, repo);
4693 if (err) {
4694 free(new_te);
4695 goto done;
4697 memcpy(&new_te->id, id, sizeof(new_te->id));
4698 done:
4699 free(id);
4700 free(subtree_path);
4701 if (err == NULL)
4702 *new_tep = new_te;
4703 return err;
4706 static const struct got_error *
4707 write_tree(struct got_object_id **new_tree_id, int *nentries,
4708 struct got_tree_object *base_tree, const char *path_base_tree,
4709 struct got_pathlist_head *commitable_paths,
4710 got_worktree_status_cb status_cb, void *status_arg,
4711 struct got_repository *repo)
4713 const struct got_error *err = NULL;
4714 struct got_pathlist_head paths;
4715 struct got_tree_entry *te, *new_te = NULL;
4716 struct got_pathlist_entry *pe;
4718 TAILQ_INIT(&paths);
4719 *nentries = 0;
4721 /* Insert, and recurse into, newly added entries first. */
4722 TAILQ_FOREACH(pe, commitable_paths, entry) {
4723 struct got_commitable *ct = pe->data;
4724 char *child_path = NULL, *slash;
4726 if ((ct->status != GOT_STATUS_ADD &&
4727 ct->staged_status != GOT_STATUS_ADD) ||
4728 (ct->flags & GOT_COMMITABLE_ADDED))
4729 continue;
4731 if (!got_path_is_child(pe->path, path_base_tree,
4732 strlen(path_base_tree)))
4733 continue;
4735 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4736 pe->path);
4737 if (err)
4738 goto done;
4740 slash = strchr(child_path, '/');
4741 if (slash == NULL) {
4742 err = alloc_added_blob_tree_entry(&new_te, ct);
4743 if (err)
4744 goto done;
4745 err = report_ct_status(ct, status_cb, status_arg);
4746 if (err)
4747 goto done;
4748 ct->flags |= GOT_COMMITABLE_ADDED;
4749 err = insert_tree_entry(new_te, &paths);
4750 if (err)
4751 goto done;
4752 (*nentries)++;
4753 } else {
4754 *slash = '\0'; /* trim trailing path components */
4755 if (base_tree == NULL ||
4756 got_object_tree_find_entry(base_tree, child_path)
4757 == NULL) {
4758 err = make_subtree_for_added_blob(&new_te,
4759 child_path, path_base_tree,
4760 commitable_paths, status_cb, status_arg,
4761 repo);
4762 if (err)
4763 goto done;
4764 err = insert_tree_entry(new_te, &paths);
4765 if (err)
4766 goto done;
4767 (*nentries)++;
4772 if (base_tree) {
4773 int i, nbase_entries;
4774 /* Handle modified and deleted entries. */
4775 nbase_entries = got_object_tree_get_nentries(base_tree);
4776 for (i = 0; i < nbase_entries; i++) {
4777 struct got_commitable *ct = NULL;
4779 te = got_object_tree_get_entry(base_tree, i);
4780 if (got_object_tree_entry_is_submodule(te)) {
4781 /* Entry is a submodule; just copy it. */
4782 err = got_object_tree_entry_dup(&new_te, te);
4783 if (err)
4784 goto done;
4785 err = insert_tree_entry(new_te, &paths);
4786 if (err)
4787 goto done;
4788 (*nentries)++;
4789 continue;
4792 if (S_ISDIR(te->mode)) {
4793 int modified;
4794 err = got_object_tree_entry_dup(&new_te, te);
4795 if (err)
4796 goto done;
4797 err = match_modified_subtree(&modified, te,
4798 path_base_tree, commitable_paths);
4799 if (err)
4800 goto done;
4801 /* Avoid recursion into unmodified subtrees. */
4802 if (modified) {
4803 struct got_object_id *new_id;
4804 int nsubentries;
4805 err = write_subtree(&new_id,
4806 &nsubentries, te,
4807 path_base_tree, commitable_paths,
4808 status_cb, status_arg, repo);
4809 if (err)
4810 goto done;
4811 if (nsubentries == 0) {
4812 /* All entries were deleted. */
4813 free(new_id);
4814 continue;
4816 memcpy(&new_te->id, new_id,
4817 sizeof(new_te->id));
4818 free(new_id);
4820 err = insert_tree_entry(new_te, &paths);
4821 if (err)
4822 goto done;
4823 (*nentries)++;
4824 continue;
4827 err = match_deleted_or_modified_ct(&ct, te,
4828 path_base_tree, commitable_paths);
4829 if (err)
4830 goto done;
4831 if (ct) {
4832 /* NB: Deleted entries get dropped here. */
4833 if (ct->status == GOT_STATUS_MODIFY ||
4834 ct->status == GOT_STATUS_MODE_CHANGE ||
4835 ct->staged_status == GOT_STATUS_MODIFY) {
4836 err = alloc_modified_blob_tree_entry(
4837 &new_te, te, ct);
4838 if (err)
4839 goto done;
4840 err = insert_tree_entry(new_te, &paths);
4841 if (err)
4842 goto done;
4843 (*nentries)++;
4845 err = report_ct_status(ct, status_cb,
4846 status_arg);
4847 if (err)
4848 goto done;
4849 } else {
4850 /* Entry is unchanged; just copy it. */
4851 err = got_object_tree_entry_dup(&new_te, te);
4852 if (err)
4853 goto done;
4854 err = insert_tree_entry(new_te, &paths);
4855 if (err)
4856 goto done;
4857 (*nentries)++;
4862 /* Write new list of entries; deleted entries have been dropped. */
4863 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4864 done:
4865 got_pathlist_free(&paths);
4866 return err;
4869 static const struct got_error *
4870 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4871 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4872 int have_staged_files)
4874 const struct got_error *err = NULL;
4875 struct got_pathlist_entry *pe;
4877 TAILQ_FOREACH(pe, commitable_paths, entry) {
4878 struct got_fileindex_entry *ie;
4879 struct got_commitable *ct = pe->data;
4881 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4882 if (ie) {
4883 if (ct->status == GOT_STATUS_DELETE ||
4884 ct->staged_status == GOT_STATUS_DELETE) {
4885 got_fileindex_entry_remove(fileindex, ie);
4886 } else if (ct->staged_status == GOT_STATUS_ADD ||
4887 ct->staged_status == GOT_STATUS_MODIFY) {
4888 got_fileindex_entry_stage_set(ie,
4889 GOT_FILEIDX_STAGE_NONE);
4890 err = got_fileindex_entry_update(ie,
4891 ct->ondisk_path, ct->staged_blob_id->sha1,
4892 new_base_commit_id->sha1,
4893 !have_staged_files);
4894 } else
4895 err = got_fileindex_entry_update(ie,
4896 ct->ondisk_path, ct->blob_id->sha1,
4897 new_base_commit_id->sha1,
4898 !have_staged_files);
4899 } else {
4900 err = got_fileindex_entry_alloc(&ie, pe->path);
4901 if (err)
4902 break;
4903 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4904 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4905 if (err) {
4906 got_fileindex_entry_free(ie);
4907 break;
4909 err = got_fileindex_entry_add(fileindex, ie);
4910 if (err) {
4911 got_fileindex_entry_free(ie);
4912 break;
4916 return err;
4920 static const struct got_error *
4921 check_out_of_date(const char *in_repo_path, unsigned char status,
4922 unsigned char staged_status, struct got_object_id *base_blob_id,
4923 struct got_object_id *base_commit_id,
4924 struct got_object_id *head_commit_id, struct got_repository *repo,
4925 int ood_errcode)
4927 const struct got_error *err = NULL;
4928 struct got_object_id *id = NULL;
4930 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4931 /* Trivial case: base commit == head commit */
4932 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4933 return NULL;
4935 * Ensure file content which local changes were based
4936 * on matches file content in the branch head.
4938 err = got_object_id_by_path(&id, repo, head_commit_id,
4939 in_repo_path);
4940 if (err) {
4941 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4942 err = got_error(ood_errcode);
4943 goto done;
4944 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4945 err = got_error(ood_errcode);
4946 } else {
4947 /* Require that added files don't exist in the branch head. */
4948 err = got_object_id_by_path(&id, repo, head_commit_id,
4949 in_repo_path);
4950 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4951 goto done;
4952 err = id ? got_error(ood_errcode) : NULL;
4954 done:
4955 free(id);
4956 return err;
4959 const struct got_error *
4960 commit_worktree(struct got_object_id **new_commit_id,
4961 struct got_pathlist_head *commitable_paths,
4962 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4963 const char *author, const char *committer,
4964 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4965 got_worktree_status_cb status_cb, void *status_arg,
4966 struct got_repository *repo)
4968 const struct got_error *err = NULL, *unlockerr = NULL;
4969 struct got_pathlist_entry *pe;
4970 const char *head_ref_name = NULL;
4971 struct got_commit_object *head_commit = NULL;
4972 struct got_reference *head_ref2 = NULL;
4973 struct got_object_id *head_commit_id2 = NULL;
4974 struct got_tree_object *head_tree = NULL;
4975 struct got_object_id *new_tree_id = NULL;
4976 int nentries;
4977 struct got_object_id_queue parent_ids;
4978 struct got_object_qid *pid = NULL;
4979 char *logmsg = NULL;
4981 *new_commit_id = NULL;
4983 SIMPLEQ_INIT(&parent_ids);
4985 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4986 if (err)
4987 goto done;
4989 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4990 if (err)
4991 goto done;
4993 if (commit_msg_cb != NULL) {
4994 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4995 if (err)
4996 goto done;
4999 if (logmsg == NULL || strlen(logmsg) == 0) {
5000 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5001 goto done;
5004 /* Create blobs from added and modified files and record their IDs. */
5005 TAILQ_FOREACH(pe, commitable_paths, entry) {
5006 struct got_commitable *ct = pe->data;
5007 char *ondisk_path;
5009 /* Blobs for staged files already exist. */
5010 if (ct->staged_status == GOT_STATUS_ADD ||
5011 ct->staged_status == GOT_STATUS_MODIFY)
5012 continue;
5014 if (ct->status != GOT_STATUS_ADD &&
5015 ct->status != GOT_STATUS_MODIFY &&
5016 ct->status != GOT_STATUS_MODE_CHANGE)
5017 continue;
5019 if (asprintf(&ondisk_path, "%s/%s",
5020 worktree->root_path, pe->path) == -1) {
5021 err = got_error_from_errno("asprintf");
5022 goto done;
5024 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5025 if (err) {
5026 free(ondisk_path);
5027 goto done;
5031 * When comitting a symlink we convert "bad" symlinks (those
5032 * which point outside the work tree or into .got) to regular
5033 * files. This way, the post-commit work tree state matches
5034 * a fresh checkout of the tree which was committed.
5036 if (S_ISLNK(get_ct_file_mode(ct))) {
5037 struct got_blob_object *blob;
5038 err = got_object_open_as_blob(&blob, repo, ct->blob_id,
5039 PATH_MAX);
5040 if (err) {
5041 free(ondisk_path);
5042 goto done;
5044 err = install_symlink(worktree, ondisk_path, ct->path,
5045 get_ct_file_mode(ct), GOT_DEFAULT_FILE_MODE, blob,
5046 0, 0, repo, NULL, NULL);
5047 got_object_blob_close(blob);
5048 if (err) {
5049 free(ondisk_path);
5050 goto done;
5053 free(ondisk_path);
5056 /* Recursively write new tree objects. */
5057 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5058 commitable_paths, status_cb, status_arg, repo);
5059 if (err)
5060 goto done;
5062 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5063 if (err)
5064 goto done;
5065 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5066 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5067 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5068 got_object_qid_free(pid);
5069 if (logmsg != NULL)
5070 free(logmsg);
5071 if (err)
5072 goto done;
5074 /* Check if a concurrent commit to our branch has occurred. */
5075 head_ref_name = got_worktree_get_head_ref_name(worktree);
5076 if (head_ref_name == NULL) {
5077 err = got_error_from_errno("got_worktree_get_head_ref_name");
5078 goto done;
5080 /* Lock the reference here to prevent concurrent modification. */
5081 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5082 if (err)
5083 goto done;
5084 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5085 if (err)
5086 goto done;
5087 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5088 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5089 goto done;
5091 /* Update branch head in repository. */
5092 err = got_ref_change_ref(head_ref2, *new_commit_id);
5093 if (err)
5094 goto done;
5095 err = got_ref_write(head_ref2, repo);
5096 if (err)
5097 goto done;
5099 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5100 if (err)
5101 goto done;
5103 err = ref_base_commit(worktree, repo);
5104 if (err)
5105 goto done;
5106 done:
5107 if (head_tree)
5108 got_object_tree_close(head_tree);
5109 if (head_commit)
5110 got_object_commit_close(head_commit);
5111 free(head_commit_id2);
5112 if (head_ref2) {
5113 unlockerr = got_ref_unlock(head_ref2);
5114 if (unlockerr && err == NULL)
5115 err = unlockerr;
5116 got_ref_close(head_ref2);
5118 return err;
5121 static const struct got_error *
5122 check_path_is_commitable(const char *path,
5123 struct got_pathlist_head *commitable_paths)
5125 struct got_pathlist_entry *cpe = NULL;
5126 size_t path_len = strlen(path);
5128 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5129 struct got_commitable *ct = cpe->data;
5130 const char *ct_path = ct->path;
5132 while (ct_path[0] == '/')
5133 ct_path++;
5135 if (strcmp(path, ct_path) == 0 ||
5136 got_path_is_child(ct_path, path, path_len))
5137 break;
5140 if (cpe == NULL)
5141 return got_error_path(path, GOT_ERR_BAD_PATH);
5143 return NULL;
5146 static const struct got_error *
5147 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5149 int *have_staged_files = arg;
5151 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5152 *have_staged_files = 1;
5153 return got_error(GOT_ERR_CANCELLED);
5156 return NULL;
5159 static const struct got_error *
5160 check_non_staged_files(struct got_fileindex *fileindex,
5161 struct got_pathlist_head *paths)
5163 struct got_pathlist_entry *pe;
5164 struct got_fileindex_entry *ie;
5166 TAILQ_FOREACH(pe, paths, entry) {
5167 if (pe->path[0] == '\0')
5168 continue;
5169 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5170 if (ie == NULL)
5171 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5172 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5173 return got_error_path(pe->path,
5174 GOT_ERR_FILE_NOT_STAGED);
5177 return NULL;
5180 const struct got_error *
5181 got_worktree_commit(struct got_object_id **new_commit_id,
5182 struct got_worktree *worktree, struct got_pathlist_head *paths,
5183 const char *author, const char *committer,
5184 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5185 got_worktree_status_cb status_cb, void *status_arg,
5186 struct got_repository *repo)
5188 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5189 struct got_fileindex *fileindex = NULL;
5190 char *fileindex_path = NULL;
5191 struct got_pathlist_head commitable_paths;
5192 struct collect_commitables_arg cc_arg;
5193 struct got_pathlist_entry *pe;
5194 struct got_reference *head_ref = NULL;
5195 struct got_object_id *head_commit_id = NULL;
5196 int have_staged_files = 0;
5198 *new_commit_id = NULL;
5200 TAILQ_INIT(&commitable_paths);
5202 err = lock_worktree(worktree, LOCK_EX);
5203 if (err)
5204 goto done;
5206 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5207 if (err)
5208 goto done;
5210 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5211 if (err)
5212 goto done;
5214 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5215 if (err)
5216 goto done;
5218 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5219 &have_staged_files);
5220 if (err && err->code != GOT_ERR_CANCELLED)
5221 goto done;
5222 if (have_staged_files) {
5223 err = check_non_staged_files(fileindex, paths);
5224 if (err)
5225 goto done;
5228 cc_arg.commitable_paths = &commitable_paths;
5229 cc_arg.worktree = worktree;
5230 cc_arg.repo = repo;
5231 cc_arg.have_staged_files = have_staged_files;
5232 TAILQ_FOREACH(pe, paths, entry) {
5233 err = worktree_status(worktree, pe->path, fileindex, repo,
5234 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5235 if (err)
5236 goto done;
5239 if (TAILQ_EMPTY(&commitable_paths)) {
5240 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5241 goto done;
5244 TAILQ_FOREACH(pe, paths, entry) {
5245 err = check_path_is_commitable(pe->path, &commitable_paths);
5246 if (err)
5247 goto done;
5250 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5251 struct got_commitable *ct = pe->data;
5252 const char *ct_path = ct->in_repo_path;
5254 while (ct_path[0] == '/')
5255 ct_path++;
5256 err = check_out_of_date(ct_path, ct->status,
5257 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5258 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5259 if (err)
5260 goto done;
5264 err = commit_worktree(new_commit_id, &commitable_paths,
5265 head_commit_id, worktree, author, committer,
5266 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5267 if (err)
5268 goto done;
5270 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5271 fileindex, have_staged_files);
5272 sync_err = sync_fileindex(fileindex, fileindex_path);
5273 if (sync_err && err == NULL)
5274 err = sync_err;
5275 done:
5276 if (fileindex)
5277 got_fileindex_free(fileindex);
5278 free(fileindex_path);
5279 unlockerr = lock_worktree(worktree, LOCK_SH);
5280 if (unlockerr && err == NULL)
5281 err = unlockerr;
5282 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5283 struct got_commitable *ct = pe->data;
5284 free_commitable(ct);
5286 got_pathlist_free(&commitable_paths);
5287 return err;
5290 const char *
5291 got_commitable_get_path(struct got_commitable *ct)
5293 return ct->path;
5296 unsigned int
5297 got_commitable_get_status(struct got_commitable *ct)
5299 return ct->status;
5302 struct check_rebase_ok_arg {
5303 struct got_worktree *worktree;
5304 struct got_repository *repo;
5307 static const struct got_error *
5308 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5310 const struct got_error *err = NULL;
5311 struct check_rebase_ok_arg *a = arg;
5312 unsigned char status;
5313 struct stat sb;
5314 char *ondisk_path;
5316 /* Reject rebase of a work tree with mixed base commits. */
5317 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5318 SHA1_DIGEST_LENGTH))
5319 return got_error(GOT_ERR_MIXED_COMMITS);
5321 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5322 == -1)
5323 return got_error_from_errno("asprintf");
5325 /* Reject rebase of a work tree with modified or staged files. */
5326 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5327 free(ondisk_path);
5328 if (err)
5329 return err;
5331 if (status != GOT_STATUS_NO_CHANGE)
5332 return got_error(GOT_ERR_MODIFIED);
5333 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5334 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5336 return NULL;
5339 const struct got_error *
5340 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5341 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5342 struct got_worktree *worktree, struct got_reference *branch,
5343 struct got_repository *repo)
5345 const struct got_error *err = NULL;
5346 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5347 char *branch_ref_name = NULL;
5348 char *fileindex_path = NULL;
5349 struct check_rebase_ok_arg ok_arg;
5350 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5351 struct got_object_id *wt_branch_tip = NULL;
5353 *new_base_branch_ref = NULL;
5354 *tmp_branch = NULL;
5355 *fileindex = NULL;
5357 err = lock_worktree(worktree, LOCK_EX);
5358 if (err)
5359 return err;
5361 err = open_fileindex(fileindex, &fileindex_path, worktree);
5362 if (err)
5363 goto done;
5365 ok_arg.worktree = worktree;
5366 ok_arg.repo = repo;
5367 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5368 &ok_arg);
5369 if (err)
5370 goto done;
5372 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5373 if (err)
5374 goto done;
5376 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5377 if (err)
5378 goto done;
5380 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5381 if (err)
5382 goto done;
5384 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5385 0);
5386 if (err)
5387 goto done;
5389 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5390 if (err)
5391 goto done;
5392 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5393 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5394 goto done;
5397 err = got_ref_alloc_symref(new_base_branch_ref,
5398 new_base_branch_ref_name, wt_branch);
5399 if (err)
5400 goto done;
5401 err = got_ref_write(*new_base_branch_ref, repo);
5402 if (err)
5403 goto done;
5405 /* TODO Lock original branch's ref while rebasing? */
5407 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5408 if (err)
5409 goto done;
5411 err = got_ref_write(branch_ref, repo);
5412 if (err)
5413 goto done;
5415 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5416 worktree->base_commit_id);
5417 if (err)
5418 goto done;
5419 err = got_ref_write(*tmp_branch, repo);
5420 if (err)
5421 goto done;
5423 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5424 if (err)
5425 goto done;
5426 done:
5427 free(fileindex_path);
5428 free(tmp_branch_name);
5429 free(new_base_branch_ref_name);
5430 free(branch_ref_name);
5431 if (branch_ref)
5432 got_ref_close(branch_ref);
5433 if (wt_branch)
5434 got_ref_close(wt_branch);
5435 free(wt_branch_tip);
5436 if (err) {
5437 if (*new_base_branch_ref) {
5438 got_ref_close(*new_base_branch_ref);
5439 *new_base_branch_ref = NULL;
5441 if (*tmp_branch) {
5442 got_ref_close(*tmp_branch);
5443 *tmp_branch = NULL;
5445 if (*fileindex) {
5446 got_fileindex_free(*fileindex);
5447 *fileindex = NULL;
5449 lock_worktree(worktree, LOCK_SH);
5451 return err;
5454 const struct got_error *
5455 got_worktree_rebase_continue(struct got_object_id **commit_id,
5456 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5457 struct got_reference **branch, struct got_fileindex **fileindex,
5458 struct got_worktree *worktree, struct got_repository *repo)
5460 const struct got_error *err;
5461 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5462 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5463 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5464 char *fileindex_path = NULL;
5465 int have_staged_files = 0;
5467 *commit_id = NULL;
5468 *new_base_branch = NULL;
5469 *tmp_branch = NULL;
5470 *branch = NULL;
5471 *fileindex = NULL;
5473 err = lock_worktree(worktree, LOCK_EX);
5474 if (err)
5475 return err;
5477 err = open_fileindex(fileindex, &fileindex_path, worktree);
5478 if (err)
5479 goto done;
5481 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5482 &have_staged_files);
5483 if (err && err->code != GOT_ERR_CANCELLED)
5484 goto done;
5485 if (have_staged_files) {
5486 err = got_error(GOT_ERR_STAGED_PATHS);
5487 goto done;
5490 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5491 if (err)
5492 goto done;
5494 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5495 if (err)
5496 goto done;
5498 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5499 if (err)
5500 goto done;
5502 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5503 if (err)
5504 goto done;
5506 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5507 if (err)
5508 goto done;
5510 err = got_ref_open(branch, repo,
5511 got_ref_get_symref_target(branch_ref), 0);
5512 if (err)
5513 goto done;
5515 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5516 if (err)
5517 goto done;
5519 err = got_ref_resolve(commit_id, repo, commit_ref);
5520 if (err)
5521 goto done;
5523 err = got_ref_open(new_base_branch, repo,
5524 new_base_branch_ref_name, 0);
5525 if (err)
5526 goto done;
5528 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5529 if (err)
5530 goto done;
5531 done:
5532 free(commit_ref_name);
5533 free(branch_ref_name);
5534 free(fileindex_path);
5535 if (commit_ref)
5536 got_ref_close(commit_ref);
5537 if (branch_ref)
5538 got_ref_close(branch_ref);
5539 if (err) {
5540 free(*commit_id);
5541 *commit_id = NULL;
5542 if (*tmp_branch) {
5543 got_ref_close(*tmp_branch);
5544 *tmp_branch = NULL;
5546 if (*new_base_branch) {
5547 got_ref_close(*new_base_branch);
5548 *new_base_branch = NULL;
5550 if (*branch) {
5551 got_ref_close(*branch);
5552 *branch = NULL;
5554 if (*fileindex) {
5555 got_fileindex_free(*fileindex);
5556 *fileindex = NULL;
5558 lock_worktree(worktree, LOCK_SH);
5560 return err;
5563 const struct got_error *
5564 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5566 const struct got_error *err;
5567 char *tmp_branch_name = NULL;
5569 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5570 if (err)
5571 return err;
5573 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5574 free(tmp_branch_name);
5575 return NULL;
5578 static const struct got_error *
5579 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5580 char **logmsg, void *arg)
5582 *logmsg = arg;
5583 return NULL;
5586 static const struct got_error *
5587 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5588 const char *path, struct got_object_id *blob_id,
5589 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5590 int dirfd, const char *de_name)
5592 return NULL;
5595 struct collect_merged_paths_arg {
5596 got_worktree_checkout_cb progress_cb;
5597 void *progress_arg;
5598 struct got_pathlist_head *merged_paths;
5601 static const struct got_error *
5602 collect_merged_paths(void *arg, unsigned char status, const char *path)
5604 const struct got_error *err;
5605 struct collect_merged_paths_arg *a = arg;
5606 char *p;
5607 struct got_pathlist_entry *new;
5609 err = (*a->progress_cb)(a->progress_arg, status, path);
5610 if (err)
5611 return err;
5613 if (status != GOT_STATUS_MERGE &&
5614 status != GOT_STATUS_ADD &&
5615 status != GOT_STATUS_DELETE &&
5616 status != GOT_STATUS_CONFLICT)
5617 return NULL;
5619 p = strdup(path);
5620 if (p == NULL)
5621 return got_error_from_errno("strdup");
5623 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5624 if (err || new == NULL)
5625 free(p);
5626 return err;
5629 void
5630 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5632 struct got_pathlist_entry *pe;
5634 TAILQ_FOREACH(pe, merged_paths, entry)
5635 free((char *)pe->path);
5637 got_pathlist_free(merged_paths);
5640 static const struct got_error *
5641 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5642 int is_rebase, struct got_repository *repo)
5644 const struct got_error *err;
5645 struct got_reference *commit_ref = NULL;
5647 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5648 if (err) {
5649 if (err->code != GOT_ERR_NOT_REF)
5650 goto done;
5651 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5652 if (err)
5653 goto done;
5654 err = got_ref_write(commit_ref, repo);
5655 if (err)
5656 goto done;
5657 } else if (is_rebase) {
5658 struct got_object_id *stored_id;
5659 int cmp;
5661 err = got_ref_resolve(&stored_id, repo, commit_ref);
5662 if (err)
5663 goto done;
5664 cmp = got_object_id_cmp(commit_id, stored_id);
5665 free(stored_id);
5666 if (cmp != 0) {
5667 err = got_error(GOT_ERR_REBASE_COMMITID);
5668 goto done;
5671 done:
5672 if (commit_ref)
5673 got_ref_close(commit_ref);
5674 return err;
5677 static const struct got_error *
5678 rebase_merge_files(struct got_pathlist_head *merged_paths,
5679 const char *commit_ref_name, struct got_worktree *worktree,
5680 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5681 struct got_object_id *commit_id, struct got_repository *repo,
5682 got_worktree_checkout_cb progress_cb, void *progress_arg,
5683 got_cancel_cb cancel_cb, void *cancel_arg)
5685 const struct got_error *err;
5686 struct got_reference *commit_ref = NULL;
5687 struct collect_merged_paths_arg cmp_arg;
5688 char *fileindex_path;
5690 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5692 err = get_fileindex_path(&fileindex_path, worktree);
5693 if (err)
5694 return err;
5696 cmp_arg.progress_cb = progress_cb;
5697 cmp_arg.progress_arg = progress_arg;
5698 cmp_arg.merged_paths = merged_paths;
5699 err = merge_files(worktree, fileindex, fileindex_path,
5700 parent_commit_id, commit_id, repo, collect_merged_paths,
5701 &cmp_arg, cancel_cb, cancel_arg);
5702 if (commit_ref)
5703 got_ref_close(commit_ref);
5704 return err;
5707 const struct got_error *
5708 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5709 struct got_worktree *worktree, struct got_fileindex *fileindex,
5710 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5711 struct got_repository *repo,
5712 got_worktree_checkout_cb progress_cb, void *progress_arg,
5713 got_cancel_cb cancel_cb, void *cancel_arg)
5715 const struct got_error *err;
5716 char *commit_ref_name;
5718 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5719 if (err)
5720 return err;
5722 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5723 if (err)
5724 goto done;
5726 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5727 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5728 progress_arg, cancel_cb, cancel_arg);
5729 done:
5730 free(commit_ref_name);
5731 return err;
5734 const struct got_error *
5735 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5736 struct got_worktree *worktree, struct got_fileindex *fileindex,
5737 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5738 struct got_repository *repo,
5739 got_worktree_checkout_cb progress_cb, void *progress_arg,
5740 got_cancel_cb cancel_cb, void *cancel_arg)
5742 const struct got_error *err;
5743 char *commit_ref_name;
5745 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5746 if (err)
5747 return err;
5749 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5750 if (err)
5751 goto done;
5753 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5754 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5755 progress_arg, cancel_cb, cancel_arg);
5756 done:
5757 free(commit_ref_name);
5758 return err;
5761 static const struct got_error *
5762 rebase_commit(struct got_object_id **new_commit_id,
5763 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5764 struct got_worktree *worktree, struct got_fileindex *fileindex,
5765 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5766 const char *new_logmsg, struct got_repository *repo)
5768 const struct got_error *err, *sync_err;
5769 struct got_pathlist_head commitable_paths;
5770 struct collect_commitables_arg cc_arg;
5771 char *fileindex_path = NULL;
5772 struct got_reference *head_ref = NULL;
5773 struct got_object_id *head_commit_id = NULL;
5774 char *logmsg = NULL;
5776 TAILQ_INIT(&commitable_paths);
5777 *new_commit_id = NULL;
5779 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5781 err = get_fileindex_path(&fileindex_path, worktree);
5782 if (err)
5783 return err;
5785 cc_arg.commitable_paths = &commitable_paths;
5786 cc_arg.worktree = worktree;
5787 cc_arg.repo = repo;
5788 cc_arg.have_staged_files = 0;
5790 * If possible get the status of individual files directly to
5791 * avoid crawling the entire work tree once per rebased commit.
5792 * TODO: Ideally, merged_paths would contain a list of commitables
5793 * we could use so we could skip worktree_status() entirely.
5795 if (merged_paths) {
5796 struct got_pathlist_entry *pe;
5797 TAILQ_FOREACH(pe, merged_paths, entry) {
5798 err = worktree_status(worktree, pe->path, fileindex,
5799 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5800 0);
5801 if (err)
5802 goto done;
5804 } else {
5805 err = worktree_status(worktree, "", fileindex, repo,
5806 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5807 if (err)
5808 goto done;
5811 if (TAILQ_EMPTY(&commitable_paths)) {
5812 /* No-op change; commit will be elided. */
5813 err = got_ref_delete(commit_ref, repo);
5814 if (err)
5815 goto done;
5816 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5817 goto done;
5820 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5821 if (err)
5822 goto done;
5824 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5825 if (err)
5826 goto done;
5828 if (new_logmsg) {
5829 logmsg = strdup(new_logmsg);
5830 if (logmsg == NULL) {
5831 err = got_error_from_errno("strdup");
5832 goto done;
5834 } else {
5835 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5836 if (err)
5837 goto done;
5840 /* NB: commit_worktree will call free(logmsg) */
5841 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5842 worktree, got_object_commit_get_author(orig_commit),
5843 got_object_commit_get_committer(orig_commit),
5844 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5845 if (err)
5846 goto done;
5848 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5849 if (err)
5850 goto done;
5852 err = got_ref_delete(commit_ref, repo);
5853 if (err)
5854 goto done;
5856 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5857 fileindex, 0);
5858 sync_err = sync_fileindex(fileindex, fileindex_path);
5859 if (sync_err && err == NULL)
5860 err = sync_err;
5861 done:
5862 free(fileindex_path);
5863 free(head_commit_id);
5864 if (head_ref)
5865 got_ref_close(head_ref);
5866 if (err) {
5867 free(*new_commit_id);
5868 *new_commit_id = NULL;
5870 return err;
5873 const struct got_error *
5874 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5875 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5876 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5877 struct got_commit_object *orig_commit,
5878 struct got_object_id *orig_commit_id, struct got_repository *repo)
5880 const struct got_error *err;
5881 char *commit_ref_name;
5882 struct got_reference *commit_ref = NULL;
5883 struct got_object_id *commit_id = NULL;
5885 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5886 if (err)
5887 return err;
5889 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5890 if (err)
5891 goto done;
5892 err = got_ref_resolve(&commit_id, repo, commit_ref);
5893 if (err)
5894 goto done;
5895 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5896 err = got_error(GOT_ERR_REBASE_COMMITID);
5897 goto done;
5900 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5901 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5902 done:
5903 if (commit_ref)
5904 got_ref_close(commit_ref);
5905 free(commit_ref_name);
5906 free(commit_id);
5907 return err;
5910 const struct got_error *
5911 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5912 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5913 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5914 struct got_commit_object *orig_commit,
5915 struct got_object_id *orig_commit_id, const char *new_logmsg,
5916 struct got_repository *repo)
5918 const struct got_error *err;
5919 char *commit_ref_name;
5920 struct got_reference *commit_ref = NULL;
5922 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5923 if (err)
5924 return err;
5926 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5927 if (err)
5928 goto done;
5930 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5931 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5932 done:
5933 if (commit_ref)
5934 got_ref_close(commit_ref);
5935 free(commit_ref_name);
5936 return err;
5939 const struct got_error *
5940 got_worktree_rebase_postpone(struct got_worktree *worktree,
5941 struct got_fileindex *fileindex)
5943 if (fileindex)
5944 got_fileindex_free(fileindex);
5945 return lock_worktree(worktree, LOCK_SH);
5948 static const struct got_error *
5949 delete_ref(const char *name, struct got_repository *repo)
5951 const struct got_error *err;
5952 struct got_reference *ref;
5954 err = got_ref_open(&ref, repo, name, 0);
5955 if (err) {
5956 if (err->code == GOT_ERR_NOT_REF)
5957 return NULL;
5958 return err;
5961 err = got_ref_delete(ref, repo);
5962 got_ref_close(ref);
5963 return err;
5966 static const struct got_error *
5967 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5969 const struct got_error *err;
5970 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5971 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5973 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5974 if (err)
5975 goto done;
5976 err = delete_ref(tmp_branch_name, repo);
5977 if (err)
5978 goto done;
5980 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5981 if (err)
5982 goto done;
5983 err = delete_ref(new_base_branch_ref_name, repo);
5984 if (err)
5985 goto done;
5987 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5988 if (err)
5989 goto done;
5990 err = delete_ref(branch_ref_name, repo);
5991 if (err)
5992 goto done;
5994 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5995 if (err)
5996 goto done;
5997 err = delete_ref(commit_ref_name, repo);
5998 if (err)
5999 goto done;
6001 done:
6002 free(tmp_branch_name);
6003 free(new_base_branch_ref_name);
6004 free(branch_ref_name);
6005 free(commit_ref_name);
6006 return err;
6009 const struct got_error *
6010 got_worktree_rebase_complete(struct got_worktree *worktree,
6011 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6012 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6013 struct got_repository *repo)
6015 const struct got_error *err, *unlockerr;
6016 struct got_object_id *new_head_commit_id = NULL;
6018 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6019 if (err)
6020 return err;
6022 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6023 if (err)
6024 goto done;
6026 err = got_ref_write(rebased_branch, repo);
6027 if (err)
6028 goto done;
6030 err = got_worktree_set_head_ref(worktree, rebased_branch);
6031 if (err)
6032 goto done;
6034 err = delete_rebase_refs(worktree, repo);
6035 done:
6036 if (fileindex)
6037 got_fileindex_free(fileindex);
6038 free(new_head_commit_id);
6039 unlockerr = lock_worktree(worktree, LOCK_SH);
6040 if (unlockerr && err == NULL)
6041 err = unlockerr;
6042 return err;
6045 const struct got_error *
6046 got_worktree_rebase_abort(struct got_worktree *worktree,
6047 struct got_fileindex *fileindex, struct got_repository *repo,
6048 struct got_reference *new_base_branch,
6049 got_worktree_checkout_cb progress_cb, void *progress_arg)
6051 const struct got_error *err, *unlockerr, *sync_err;
6052 struct got_reference *resolved = NULL;
6053 struct got_object_id *commit_id = NULL;
6054 char *fileindex_path = NULL;
6055 struct revert_file_args rfa;
6056 struct got_object_id *tree_id = NULL;
6058 err = lock_worktree(worktree, LOCK_EX);
6059 if (err)
6060 return err;
6062 err = got_ref_open(&resolved, repo,
6063 got_ref_get_symref_target(new_base_branch), 0);
6064 if (err)
6065 goto done;
6067 err = got_worktree_set_head_ref(worktree, resolved);
6068 if (err)
6069 goto done;
6072 * XXX commits to the base branch could have happened while
6073 * we were busy rebasing; should we store the original commit ID
6074 * when rebase begins and read it back here?
6076 err = got_ref_resolve(&commit_id, repo, resolved);
6077 if (err)
6078 goto done;
6080 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6081 if (err)
6082 goto done;
6084 err = got_object_id_by_path(&tree_id, repo,
6085 worktree->base_commit_id, worktree->path_prefix);
6086 if (err)
6087 goto done;
6089 err = delete_rebase_refs(worktree, repo);
6090 if (err)
6091 goto done;
6093 err = get_fileindex_path(&fileindex_path, worktree);
6094 if (err)
6095 goto done;
6097 rfa.worktree = worktree;
6098 rfa.fileindex = fileindex;
6099 rfa.progress_cb = progress_cb;
6100 rfa.progress_arg = progress_arg;
6101 rfa.patch_cb = NULL;
6102 rfa.patch_arg = NULL;
6103 rfa.repo = repo;
6104 err = worktree_status(worktree, "", fileindex, repo,
6105 revert_file, &rfa, NULL, NULL, 0, 0);
6106 if (err)
6107 goto sync;
6109 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6110 repo, progress_cb, progress_arg, NULL, NULL);
6111 sync:
6112 sync_err = sync_fileindex(fileindex, fileindex_path);
6113 if (sync_err && err == NULL)
6114 err = sync_err;
6115 done:
6116 got_ref_close(resolved);
6117 free(tree_id);
6118 free(commit_id);
6119 if (fileindex)
6120 got_fileindex_free(fileindex);
6121 free(fileindex_path);
6123 unlockerr = lock_worktree(worktree, LOCK_SH);
6124 if (unlockerr && err == NULL)
6125 err = unlockerr;
6126 return err;
6129 const struct got_error *
6130 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6131 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6132 struct got_fileindex **fileindex, struct got_worktree *worktree,
6133 struct got_repository *repo)
6135 const struct got_error *err = NULL;
6136 char *tmp_branch_name = NULL;
6137 char *branch_ref_name = NULL;
6138 char *base_commit_ref_name = NULL;
6139 char *fileindex_path = NULL;
6140 struct check_rebase_ok_arg ok_arg;
6141 struct got_reference *wt_branch = NULL;
6142 struct got_reference *base_commit_ref = NULL;
6144 *tmp_branch = NULL;
6145 *branch_ref = NULL;
6146 *base_commit_id = NULL;
6147 *fileindex = NULL;
6149 err = lock_worktree(worktree, LOCK_EX);
6150 if (err)
6151 return err;
6153 err = open_fileindex(fileindex, &fileindex_path, worktree);
6154 if (err)
6155 goto done;
6157 ok_arg.worktree = worktree;
6158 ok_arg.repo = repo;
6159 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6160 &ok_arg);
6161 if (err)
6162 goto done;
6164 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6165 if (err)
6166 goto done;
6168 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6169 if (err)
6170 goto done;
6172 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6173 worktree);
6174 if (err)
6175 goto done;
6177 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6178 0);
6179 if (err)
6180 goto done;
6182 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6183 if (err)
6184 goto done;
6186 err = got_ref_write(*branch_ref, repo);
6187 if (err)
6188 goto done;
6190 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6191 worktree->base_commit_id);
6192 if (err)
6193 goto done;
6194 err = got_ref_write(base_commit_ref, repo);
6195 if (err)
6196 goto done;
6197 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6198 if (*base_commit_id == NULL) {
6199 err = got_error_from_errno("got_object_id_dup");
6200 goto done;
6203 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6204 worktree->base_commit_id);
6205 if (err)
6206 goto done;
6207 err = got_ref_write(*tmp_branch, repo);
6208 if (err)
6209 goto done;
6211 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6212 if (err)
6213 goto done;
6214 done:
6215 free(fileindex_path);
6216 free(tmp_branch_name);
6217 free(branch_ref_name);
6218 free(base_commit_ref_name);
6219 if (wt_branch)
6220 got_ref_close(wt_branch);
6221 if (err) {
6222 if (*branch_ref) {
6223 got_ref_close(*branch_ref);
6224 *branch_ref = NULL;
6226 if (*tmp_branch) {
6227 got_ref_close(*tmp_branch);
6228 *tmp_branch = NULL;
6230 free(*base_commit_id);
6231 if (*fileindex) {
6232 got_fileindex_free(*fileindex);
6233 *fileindex = NULL;
6235 lock_worktree(worktree, LOCK_SH);
6237 return err;
6240 const struct got_error *
6241 got_worktree_histedit_postpone(struct got_worktree *worktree,
6242 struct got_fileindex *fileindex)
6244 if (fileindex)
6245 got_fileindex_free(fileindex);
6246 return lock_worktree(worktree, LOCK_SH);
6249 const struct got_error *
6250 got_worktree_histedit_in_progress(int *in_progress,
6251 struct got_worktree *worktree)
6253 const struct got_error *err;
6254 char *tmp_branch_name = NULL;
6256 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6257 if (err)
6258 return err;
6260 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6261 free(tmp_branch_name);
6262 return NULL;
6265 const struct got_error *
6266 got_worktree_histedit_continue(struct got_object_id **commit_id,
6267 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6268 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6269 struct got_worktree *worktree, struct got_repository *repo)
6271 const struct got_error *err;
6272 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6273 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6274 struct got_reference *commit_ref = NULL;
6275 struct got_reference *base_commit_ref = NULL;
6276 char *fileindex_path = NULL;
6277 int have_staged_files = 0;
6279 *commit_id = NULL;
6280 *tmp_branch = NULL;
6281 *base_commit_id = NULL;
6282 *fileindex = NULL;
6284 err = lock_worktree(worktree, LOCK_EX);
6285 if (err)
6286 return err;
6288 err = open_fileindex(fileindex, &fileindex_path, worktree);
6289 if (err)
6290 goto done;
6292 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6293 &have_staged_files);
6294 if (err && err->code != GOT_ERR_CANCELLED)
6295 goto done;
6296 if (have_staged_files) {
6297 err = got_error(GOT_ERR_STAGED_PATHS);
6298 goto done;
6301 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6302 if (err)
6303 goto done;
6305 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6306 if (err)
6307 goto done;
6309 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6310 if (err)
6311 goto done;
6313 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6314 worktree);
6315 if (err)
6316 goto done;
6318 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6319 if (err)
6320 goto done;
6322 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6323 if (err)
6324 goto done;
6325 err = got_ref_resolve(commit_id, repo, commit_ref);
6326 if (err)
6327 goto done;
6329 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6330 if (err)
6331 goto done;
6332 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6333 if (err)
6334 goto done;
6336 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6337 if (err)
6338 goto done;
6339 done:
6340 free(commit_ref_name);
6341 free(branch_ref_name);
6342 free(fileindex_path);
6343 if (commit_ref)
6344 got_ref_close(commit_ref);
6345 if (base_commit_ref)
6346 got_ref_close(base_commit_ref);
6347 if (err) {
6348 free(*commit_id);
6349 *commit_id = NULL;
6350 free(*base_commit_id);
6351 *base_commit_id = NULL;
6352 if (*tmp_branch) {
6353 got_ref_close(*tmp_branch);
6354 *tmp_branch = NULL;
6356 if (*fileindex) {
6357 got_fileindex_free(*fileindex);
6358 *fileindex = NULL;
6360 lock_worktree(worktree, LOCK_EX);
6362 return err;
6365 static const struct got_error *
6366 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6368 const struct got_error *err;
6369 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6370 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6372 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6373 if (err)
6374 goto done;
6375 err = delete_ref(tmp_branch_name, repo);
6376 if (err)
6377 goto done;
6379 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6380 worktree);
6381 if (err)
6382 goto done;
6383 err = delete_ref(base_commit_ref_name, repo);
6384 if (err)
6385 goto done;
6387 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6388 if (err)
6389 goto done;
6390 err = delete_ref(branch_ref_name, repo);
6391 if (err)
6392 goto done;
6394 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6395 if (err)
6396 goto done;
6397 err = delete_ref(commit_ref_name, repo);
6398 if (err)
6399 goto done;
6400 done:
6401 free(tmp_branch_name);
6402 free(base_commit_ref_name);
6403 free(branch_ref_name);
6404 free(commit_ref_name);
6405 return err;
6408 const struct got_error *
6409 got_worktree_histedit_abort(struct got_worktree *worktree,
6410 struct got_fileindex *fileindex, struct got_repository *repo,
6411 struct got_reference *branch, struct got_object_id *base_commit_id,
6412 got_worktree_checkout_cb progress_cb, void *progress_arg)
6414 const struct got_error *err, *unlockerr, *sync_err;
6415 struct got_reference *resolved = NULL;
6416 char *fileindex_path = NULL;
6417 struct got_object_id *tree_id = NULL;
6418 struct revert_file_args rfa;
6420 err = lock_worktree(worktree, LOCK_EX);
6421 if (err)
6422 return err;
6424 err = got_ref_open(&resolved, repo,
6425 got_ref_get_symref_target(branch), 0);
6426 if (err)
6427 goto done;
6429 err = got_worktree_set_head_ref(worktree, resolved);
6430 if (err)
6431 goto done;
6433 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6434 if (err)
6435 goto done;
6437 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6438 worktree->path_prefix);
6439 if (err)
6440 goto done;
6442 err = delete_histedit_refs(worktree, repo);
6443 if (err)
6444 goto done;
6446 err = get_fileindex_path(&fileindex_path, worktree);
6447 if (err)
6448 goto done;
6450 rfa.worktree = worktree;
6451 rfa.fileindex = fileindex;
6452 rfa.progress_cb = progress_cb;
6453 rfa.progress_arg = progress_arg;
6454 rfa.patch_cb = NULL;
6455 rfa.patch_arg = NULL;
6456 rfa.repo = repo;
6457 err = worktree_status(worktree, "", fileindex, repo,
6458 revert_file, &rfa, NULL, NULL, 0, 0);
6459 if (err)
6460 goto sync;
6462 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6463 repo, progress_cb, progress_arg, NULL, NULL);
6464 sync:
6465 sync_err = sync_fileindex(fileindex, fileindex_path);
6466 if (sync_err && err == NULL)
6467 err = sync_err;
6468 done:
6469 got_ref_close(resolved);
6470 free(tree_id);
6471 free(fileindex_path);
6473 unlockerr = lock_worktree(worktree, LOCK_SH);
6474 if (unlockerr && err == NULL)
6475 err = unlockerr;
6476 return err;
6479 const struct got_error *
6480 got_worktree_histedit_complete(struct got_worktree *worktree,
6481 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6482 struct got_reference *edited_branch, struct got_repository *repo)
6484 const struct got_error *err, *unlockerr;
6485 struct got_object_id *new_head_commit_id = NULL;
6486 struct got_reference *resolved = NULL;
6488 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6489 if (err)
6490 return err;
6492 err = got_ref_open(&resolved, repo,
6493 got_ref_get_symref_target(edited_branch), 0);
6494 if (err)
6495 goto done;
6497 err = got_ref_change_ref(resolved, new_head_commit_id);
6498 if (err)
6499 goto done;
6501 err = got_ref_write(resolved, repo);
6502 if (err)
6503 goto done;
6505 err = got_worktree_set_head_ref(worktree, resolved);
6506 if (err)
6507 goto done;
6509 err = delete_histedit_refs(worktree, repo);
6510 done:
6511 if (fileindex)
6512 got_fileindex_free(fileindex);
6513 free(new_head_commit_id);
6514 unlockerr = lock_worktree(worktree, LOCK_SH);
6515 if (unlockerr && err == NULL)
6516 err = unlockerr;
6517 return err;
6520 const struct got_error *
6521 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6522 struct got_object_id *commit_id, struct got_repository *repo)
6524 const struct got_error *err;
6525 char *commit_ref_name;
6527 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6528 if (err)
6529 return err;
6531 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6532 if (err)
6533 goto done;
6535 err = delete_ref(commit_ref_name, repo);
6536 done:
6537 free(commit_ref_name);
6538 return err;
6541 const struct got_error *
6542 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6543 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6544 struct got_worktree *worktree, const char *refname,
6545 struct got_repository *repo)
6547 const struct got_error *err = NULL;
6548 char *fileindex_path = NULL;
6549 struct check_rebase_ok_arg ok_arg;
6551 *fileindex = NULL;
6552 *branch_ref = NULL;
6553 *base_branch_ref = NULL;
6555 err = lock_worktree(worktree, LOCK_EX);
6556 if (err)
6557 return err;
6559 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6560 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6561 "cannot integrate a branch into itself; "
6562 "update -b or different branch name required");
6563 goto done;
6566 err = open_fileindex(fileindex, &fileindex_path, worktree);
6567 if (err)
6568 goto done;
6570 /* Preconditions are the same as for rebase. */
6571 ok_arg.worktree = worktree;
6572 ok_arg.repo = repo;
6573 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6574 &ok_arg);
6575 if (err)
6576 goto done;
6578 err = got_ref_open(branch_ref, repo, refname, 1);
6579 if (err)
6580 goto done;
6582 err = got_ref_open(base_branch_ref, repo,
6583 got_worktree_get_head_ref_name(worktree), 1);
6584 done:
6585 if (err) {
6586 if (*branch_ref) {
6587 got_ref_close(*branch_ref);
6588 *branch_ref = NULL;
6590 if (*base_branch_ref) {
6591 got_ref_close(*base_branch_ref);
6592 *base_branch_ref = NULL;
6594 if (*fileindex) {
6595 got_fileindex_free(*fileindex);
6596 *fileindex = NULL;
6598 lock_worktree(worktree, LOCK_SH);
6600 return err;
6603 const struct got_error *
6604 got_worktree_integrate_continue(struct got_worktree *worktree,
6605 struct got_fileindex *fileindex, struct got_repository *repo,
6606 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6607 got_worktree_checkout_cb progress_cb, void *progress_arg,
6608 got_cancel_cb cancel_cb, void *cancel_arg)
6610 const struct got_error *err = NULL, *sync_err, *unlockerr;
6611 char *fileindex_path = NULL;
6612 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6614 err = get_fileindex_path(&fileindex_path, worktree);
6615 if (err)
6616 goto done;
6618 err = got_ref_resolve(&commit_id, repo, branch_ref);
6619 if (err)
6620 goto done;
6622 err = got_object_id_by_path(&tree_id, repo, commit_id,
6623 worktree->path_prefix);
6624 if (err)
6625 goto done;
6627 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6628 if (err)
6629 goto done;
6631 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6632 progress_cb, progress_arg, cancel_cb, cancel_arg);
6633 if (err)
6634 goto sync;
6636 err = got_ref_change_ref(base_branch_ref, commit_id);
6637 if (err)
6638 goto sync;
6640 err = got_ref_write(base_branch_ref, repo);
6641 sync:
6642 sync_err = sync_fileindex(fileindex, fileindex_path);
6643 if (sync_err && err == NULL)
6644 err = sync_err;
6646 done:
6647 unlockerr = got_ref_unlock(branch_ref);
6648 if (unlockerr && err == NULL)
6649 err = unlockerr;
6650 got_ref_close(branch_ref);
6652 unlockerr = got_ref_unlock(base_branch_ref);
6653 if (unlockerr && err == NULL)
6654 err = unlockerr;
6655 got_ref_close(base_branch_ref);
6657 got_fileindex_free(fileindex);
6658 free(fileindex_path);
6659 free(tree_id);
6661 unlockerr = lock_worktree(worktree, LOCK_SH);
6662 if (unlockerr && err == NULL)
6663 err = unlockerr;
6664 return err;
6667 const struct got_error *
6668 got_worktree_integrate_abort(struct got_worktree *worktree,
6669 struct got_fileindex *fileindex, struct got_repository *repo,
6670 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6672 const struct got_error *err = NULL, *unlockerr = NULL;
6674 got_fileindex_free(fileindex);
6676 err = lock_worktree(worktree, LOCK_SH);
6678 unlockerr = got_ref_unlock(branch_ref);
6679 if (unlockerr && err == NULL)
6680 err = unlockerr;
6681 got_ref_close(branch_ref);
6683 unlockerr = got_ref_unlock(base_branch_ref);
6684 if (unlockerr && err == NULL)
6685 err = unlockerr;
6686 got_ref_close(base_branch_ref);
6688 return err;
6691 struct check_stage_ok_arg {
6692 struct got_object_id *head_commit_id;
6693 struct got_worktree *worktree;
6694 struct got_fileindex *fileindex;
6695 struct got_repository *repo;
6696 int have_changes;
6699 const struct got_error *
6700 check_stage_ok(void *arg, unsigned char status,
6701 unsigned char staged_status, const char *relpath,
6702 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6703 struct got_object_id *commit_id, int dirfd, const char *de_name)
6705 struct check_stage_ok_arg *a = arg;
6706 const struct got_error *err = NULL;
6707 struct got_fileindex_entry *ie;
6708 struct got_object_id base_commit_id;
6709 struct got_object_id *base_commit_idp = NULL;
6710 char *in_repo_path = NULL, *p;
6712 if (status == GOT_STATUS_UNVERSIONED ||
6713 status == GOT_STATUS_NO_CHANGE)
6714 return NULL;
6715 if (status == GOT_STATUS_NONEXISTENT)
6716 return got_error_set_errno(ENOENT, relpath);
6718 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6719 if (ie == NULL)
6720 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6722 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6723 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6724 relpath) == -1)
6725 return got_error_from_errno("asprintf");
6727 if (got_fileindex_entry_has_commit(ie)) {
6728 memcpy(base_commit_id.sha1, ie->commit_sha1,
6729 SHA1_DIGEST_LENGTH);
6730 base_commit_idp = &base_commit_id;
6733 if (status == GOT_STATUS_CONFLICT) {
6734 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6735 goto done;
6736 } else if (status != GOT_STATUS_ADD &&
6737 status != GOT_STATUS_MODIFY &&
6738 status != GOT_STATUS_DELETE) {
6739 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6740 goto done;
6743 a->have_changes = 1;
6745 p = in_repo_path;
6746 while (p[0] == '/')
6747 p++;
6748 err = check_out_of_date(p, status, staged_status,
6749 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6750 GOT_ERR_STAGE_OUT_OF_DATE);
6751 done:
6752 free(in_repo_path);
6753 return err;
6756 struct stage_path_arg {
6757 struct got_worktree *worktree;
6758 struct got_fileindex *fileindex;
6759 struct got_repository *repo;
6760 got_worktree_status_cb status_cb;
6761 void *status_arg;
6762 got_worktree_patch_cb patch_cb;
6763 void *patch_arg;
6764 int staged_something;
6767 static const struct got_error *
6768 stage_path(void *arg, unsigned char status,
6769 unsigned char staged_status, const char *relpath,
6770 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6771 struct got_object_id *commit_id, int dirfd, const char *de_name)
6773 struct stage_path_arg *a = arg;
6774 const struct got_error *err = NULL;
6775 struct got_fileindex_entry *ie;
6776 char *ondisk_path = NULL, *path_content = NULL;
6777 uint32_t stage;
6778 struct got_object_id *new_staged_blob_id = NULL;
6780 if (status == GOT_STATUS_UNVERSIONED)
6781 return NULL;
6783 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6784 if (ie == NULL)
6785 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6787 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6788 relpath)== -1)
6789 return got_error_from_errno("asprintf");
6791 switch (status) {
6792 case GOT_STATUS_ADD:
6793 case GOT_STATUS_MODIFY:
6794 if (a->patch_cb) {
6795 if (status == GOT_STATUS_ADD) {
6796 int choice = GOT_PATCH_CHOICE_NONE;
6797 err = (*a->patch_cb)(&choice, a->patch_arg,
6798 status, ie->path, NULL, 1, 1);
6799 if (err)
6800 break;
6801 if (choice != GOT_PATCH_CHOICE_YES)
6802 break;
6803 } else {
6804 err = create_patched_content(&path_content, 0,
6805 staged_blob_id ? staged_blob_id : blob_id,
6806 ondisk_path, dirfd, de_name, ie->path,
6807 a->repo, a->patch_cb, a->patch_arg);
6808 if (err || path_content == NULL)
6809 break;
6812 err = got_object_blob_create(&new_staged_blob_id,
6813 path_content ? path_content : ondisk_path, a->repo);
6814 if (err)
6815 break;
6816 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6817 SHA1_DIGEST_LENGTH);
6818 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6819 stage = GOT_FILEIDX_STAGE_ADD;
6820 else
6821 stage = GOT_FILEIDX_STAGE_MODIFY;
6822 got_fileindex_entry_stage_set(ie, stage);
6823 a->staged_something = 1;
6824 if (a->status_cb == NULL)
6825 break;
6826 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6827 get_staged_status(ie), relpath, blob_id,
6828 new_staged_blob_id, NULL, dirfd, de_name);
6829 break;
6830 case GOT_STATUS_DELETE:
6831 if (staged_status == GOT_STATUS_DELETE)
6832 break;
6833 if (a->patch_cb) {
6834 int choice = GOT_PATCH_CHOICE_NONE;
6835 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6836 ie->path, NULL, 1, 1);
6837 if (err)
6838 break;
6839 if (choice == GOT_PATCH_CHOICE_NO)
6840 break;
6841 if (choice != GOT_PATCH_CHOICE_YES) {
6842 err = got_error(GOT_ERR_PATCH_CHOICE);
6843 break;
6846 stage = GOT_FILEIDX_STAGE_DELETE;
6847 got_fileindex_entry_stage_set(ie, stage);
6848 a->staged_something = 1;
6849 if (a->status_cb == NULL)
6850 break;
6851 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6852 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6853 de_name);
6854 break;
6855 case GOT_STATUS_NO_CHANGE:
6856 break;
6857 case GOT_STATUS_CONFLICT:
6858 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6859 break;
6860 case GOT_STATUS_NONEXISTENT:
6861 err = got_error_set_errno(ENOENT, relpath);
6862 break;
6863 default:
6864 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6865 break;
6868 if (path_content && unlink(path_content) == -1 && err == NULL)
6869 err = got_error_from_errno2("unlink", path_content);
6870 free(path_content);
6871 free(ondisk_path);
6872 free(new_staged_blob_id);
6873 return err;
6876 const struct got_error *
6877 got_worktree_stage(struct got_worktree *worktree,
6878 struct got_pathlist_head *paths,
6879 got_worktree_status_cb status_cb, void *status_arg,
6880 got_worktree_patch_cb patch_cb, void *patch_arg,
6881 struct got_repository *repo)
6883 const struct got_error *err = NULL, *sync_err, *unlockerr;
6884 struct got_pathlist_entry *pe;
6885 struct got_fileindex *fileindex = NULL;
6886 char *fileindex_path = NULL;
6887 struct got_reference *head_ref = NULL;
6888 struct got_object_id *head_commit_id = NULL;
6889 struct check_stage_ok_arg oka;
6890 struct stage_path_arg spa;
6892 err = lock_worktree(worktree, LOCK_EX);
6893 if (err)
6894 return err;
6896 err = got_ref_open(&head_ref, repo,
6897 got_worktree_get_head_ref_name(worktree), 0);
6898 if (err)
6899 goto done;
6900 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6901 if (err)
6902 goto done;
6903 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6904 if (err)
6905 goto done;
6907 /* Check pre-conditions before staging anything. */
6908 oka.head_commit_id = head_commit_id;
6909 oka.worktree = worktree;
6910 oka.fileindex = fileindex;
6911 oka.repo = repo;
6912 oka.have_changes = 0;
6913 TAILQ_FOREACH(pe, paths, entry) {
6914 err = worktree_status(worktree, pe->path, fileindex, repo,
6915 check_stage_ok, &oka, NULL, NULL, 0, 0);
6916 if (err)
6917 goto done;
6919 if (!oka.have_changes) {
6920 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6921 goto done;
6924 spa.worktree = worktree;
6925 spa.fileindex = fileindex;
6926 spa.repo = repo;
6927 spa.patch_cb = patch_cb;
6928 spa.patch_arg = patch_arg;
6929 spa.status_cb = status_cb;
6930 spa.status_arg = status_arg;
6931 spa.staged_something = 0;
6932 TAILQ_FOREACH(pe, paths, entry) {
6933 err = worktree_status(worktree, pe->path, fileindex, repo,
6934 stage_path, &spa, NULL, NULL, 0, 0);
6935 if (err)
6936 goto done;
6938 if (!spa.staged_something) {
6939 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6940 goto done;
6943 sync_err = sync_fileindex(fileindex, fileindex_path);
6944 if (sync_err && err == NULL)
6945 err = sync_err;
6946 done:
6947 if (head_ref)
6948 got_ref_close(head_ref);
6949 free(head_commit_id);
6950 free(fileindex_path);
6951 if (fileindex)
6952 got_fileindex_free(fileindex);
6953 unlockerr = lock_worktree(worktree, LOCK_SH);
6954 if (unlockerr && err == NULL)
6955 err = unlockerr;
6956 return err;
6959 struct unstage_path_arg {
6960 struct got_worktree *worktree;
6961 struct got_fileindex *fileindex;
6962 struct got_repository *repo;
6963 got_worktree_checkout_cb progress_cb;
6964 void *progress_arg;
6965 got_worktree_patch_cb patch_cb;
6966 void *patch_arg;
6969 static const struct got_error *
6970 create_unstaged_content(char **path_unstaged_content,
6971 char **path_new_staged_content, struct got_object_id *blob_id,
6972 struct got_object_id *staged_blob_id, const char *relpath,
6973 struct got_repository *repo,
6974 got_worktree_patch_cb patch_cb, void *patch_arg)
6976 const struct got_error *err;
6977 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6978 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6979 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6980 struct stat sb1, sb2;
6981 struct got_diff_changes *changes = NULL;
6982 struct got_diff_state *ds = NULL;
6983 struct got_diff_args *args = NULL;
6984 struct got_diff_change *change;
6985 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6986 int have_content = 0, have_rejected_content = 0;
6988 *path_unstaged_content = NULL;
6989 *path_new_staged_content = NULL;
6991 err = got_object_id_str(&label1, blob_id);
6992 if (err)
6993 return err;
6994 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6995 if (err)
6996 goto done;
6998 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6999 if (err)
7000 goto done;
7002 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7003 if (err)
7004 goto done;
7006 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7007 if (err)
7008 goto done;
7010 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7011 if (err)
7012 goto done;
7014 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7015 if (err)
7016 goto done;
7018 if (stat(path1, &sb1) == -1) {
7019 err = got_error_from_errno2("stat", path1);
7020 goto done;
7023 if (stat(path2, &sb2) == -1) {
7024 err = got_error_from_errno2("stat", path2);
7025 goto done;
7028 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7029 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7030 if (err)
7031 goto done;
7033 err = got_opentemp_named(path_unstaged_content, &outfile,
7034 "got-unstaged-content");
7035 if (err)
7036 goto done;
7037 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7038 "got-new-staged-content");
7039 if (err)
7040 goto done;
7042 if (fseek(f1, 0L, SEEK_SET) == -1) {
7043 err = got_ferror(f1, GOT_ERR_IO);
7044 goto done;
7046 if (fseek(f2, 0L, SEEK_SET) == -1) {
7047 err = got_ferror(f2, GOT_ERR_IO);
7048 goto done;
7050 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7051 int choice;
7052 err = apply_or_reject_change(&choice, change, ++n,
7053 changes->nchanges, ds, args, diff_flags, relpath,
7054 f1, f2, &line_cur1, &line_cur2,
7055 outfile, rejectfile, patch_cb, patch_arg);
7056 if (err)
7057 goto done;
7058 if (choice == GOT_PATCH_CHOICE_YES)
7059 have_content = 1;
7060 else
7061 have_rejected_content = 1;
7062 if (choice == GOT_PATCH_CHOICE_QUIT)
7063 break;
7065 if (have_content || have_rejected_content)
7066 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7067 outfile, rejectfile);
7068 done:
7069 free(label1);
7070 if (blob)
7071 got_object_blob_close(blob);
7072 if (staged_blob)
7073 got_object_blob_close(staged_blob);
7074 if (f1 && fclose(f1) == EOF && err == NULL)
7075 err = got_error_from_errno2("fclose", path1);
7076 if (f2 && fclose(f2) == EOF && err == NULL)
7077 err = got_error_from_errno2("fclose", path2);
7078 if (outfile && fclose(outfile) == EOF && err == NULL)
7079 err = got_error_from_errno2("fclose", *path_unstaged_content);
7080 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7081 err = got_error_from_errno2("fclose", *path_new_staged_content);
7082 if (path1 && unlink(path1) == -1 && err == NULL)
7083 err = got_error_from_errno2("unlink", path1);
7084 if (path2 && unlink(path2) == -1 && err == NULL)
7085 err = got_error_from_errno2("unlink", path2);
7086 if (err || !have_content) {
7087 if (*path_unstaged_content &&
7088 unlink(*path_unstaged_content) == -1 && err == NULL)
7089 err = got_error_from_errno2("unlink",
7090 *path_unstaged_content);
7091 free(*path_unstaged_content);
7092 *path_unstaged_content = NULL;
7094 if (err || !have_rejected_content) {
7095 if (*path_new_staged_content &&
7096 unlink(*path_new_staged_content) == -1 && err == NULL)
7097 err = got_error_from_errno2("unlink",
7098 *path_new_staged_content);
7099 free(*path_new_staged_content);
7100 *path_new_staged_content = NULL;
7102 free(args);
7103 if (ds) {
7104 got_diff_state_free(ds);
7105 free(ds);
7107 if (changes)
7108 got_diff_free_changes(changes);
7109 free(path1);
7110 free(path2);
7111 return err;
7114 static const struct got_error *
7115 unstage_path(void *arg, unsigned char status,
7116 unsigned char staged_status, const char *relpath,
7117 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7118 struct got_object_id *commit_id, int dirfd, const char *de_name)
7120 const struct got_error *err = NULL;
7121 struct unstage_path_arg *a = arg;
7122 struct got_fileindex_entry *ie;
7123 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7124 char *ondisk_path = NULL, *path_unstaged_content = NULL;
7125 char *path_new_staged_content = NULL;
7126 char *id_str = NULL, *label_orig = NULL;
7127 int local_changes_subsumed;
7128 struct stat sb;
7130 if (staged_status != GOT_STATUS_ADD &&
7131 staged_status != GOT_STATUS_MODIFY &&
7132 staged_status != GOT_STATUS_DELETE)
7133 return NULL;
7135 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7136 if (ie == NULL)
7137 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7139 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7140 == -1)
7141 return got_error_from_errno("asprintf");
7143 err = got_object_id_str(&id_str,
7144 commit_id ? commit_id : a->worktree->base_commit_id);
7145 if (err)
7146 goto done;
7147 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7148 id_str) == -1) {
7149 err = got_error_from_errno("asprintf");
7150 goto done;
7153 switch (staged_status) {
7154 case GOT_STATUS_MODIFY:
7155 err = got_object_open_as_blob(&blob_base, a->repo,
7156 blob_id, 8192);
7157 if (err)
7158 break;
7159 /* fall through */
7160 case GOT_STATUS_ADD:
7161 if (a->patch_cb) {
7162 if (staged_status == GOT_STATUS_ADD) {
7163 int choice = GOT_PATCH_CHOICE_NONE;
7164 err = (*a->patch_cb)(&choice, a->patch_arg,
7165 staged_status, ie->path, NULL, 1, 1);
7166 if (err)
7167 break;
7168 if (choice != GOT_PATCH_CHOICE_YES)
7169 break;
7170 } else {
7171 err = create_unstaged_content(
7172 &path_unstaged_content,
7173 &path_new_staged_content, blob_id,
7174 staged_blob_id, ie->path, a->repo,
7175 a->patch_cb, a->patch_arg);
7176 if (err || path_unstaged_content == NULL)
7177 break;
7178 if (path_new_staged_content) {
7179 err = got_object_blob_create(
7180 &staged_blob_id,
7181 path_new_staged_content,
7182 a->repo);
7183 if (err)
7184 break;
7185 memcpy(ie->staged_blob_sha1,
7186 staged_blob_id->sha1,
7187 SHA1_DIGEST_LENGTH);
7189 err = merge_file(&local_changes_subsumed,
7190 a->worktree, blob_base, ondisk_path,
7191 relpath, got_fileindex_perms_to_st(ie),
7192 path_unstaged_content, label_orig,
7193 "unstaged", a->repo, a->progress_cb,
7194 a->progress_arg);
7195 if (err == NULL &&
7196 path_new_staged_content == NULL)
7197 got_fileindex_entry_stage_set(ie,
7198 GOT_FILEIDX_STAGE_NONE);
7199 break; /* Done with this file. */
7202 err = got_object_open_as_blob(&blob_staged, a->repo,
7203 staged_blob_id, 8192);
7204 if (err)
7205 break;
7206 err = merge_blob(&local_changes_subsumed, a->worktree,
7207 blob_base, ondisk_path, relpath,
7208 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7209 commit_id ? commit_id : a->worktree->base_commit_id,
7210 a->repo, a->progress_cb, a->progress_arg);
7211 if (err == NULL)
7212 got_fileindex_entry_stage_set(ie,
7213 GOT_FILEIDX_STAGE_NONE);
7214 break;
7215 case GOT_STATUS_DELETE:
7216 if (a->patch_cb) {
7217 int choice = GOT_PATCH_CHOICE_NONE;
7218 err = (*a->patch_cb)(&choice, a->patch_arg,
7219 staged_status, ie->path, NULL, 1, 1);
7220 if (err)
7221 break;
7222 if (choice == GOT_PATCH_CHOICE_NO)
7223 break;
7224 if (choice != GOT_PATCH_CHOICE_YES) {
7225 err = got_error(GOT_ERR_PATCH_CHOICE);
7226 break;
7229 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7230 err = get_file_status(&status, &sb, ie, ondisk_path,
7231 dirfd, de_name, a->repo);
7232 if (err)
7233 break;
7234 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7235 break;
7237 done:
7238 free(ondisk_path);
7239 if (path_unstaged_content &&
7240 unlink(path_unstaged_content) == -1 && err == NULL)
7241 err = got_error_from_errno2("unlink", path_unstaged_content);
7242 if (path_new_staged_content &&
7243 unlink(path_new_staged_content) == -1 && err == NULL)
7244 err = got_error_from_errno2("unlink", path_new_staged_content);
7245 free(path_unstaged_content);
7246 free(path_new_staged_content);
7247 if (blob_base)
7248 got_object_blob_close(blob_base);
7249 if (blob_staged)
7250 got_object_blob_close(blob_staged);
7251 free(id_str);
7252 free(label_orig);
7253 return err;
7256 const struct got_error *
7257 got_worktree_unstage(struct got_worktree *worktree,
7258 struct got_pathlist_head *paths,
7259 got_worktree_checkout_cb progress_cb, void *progress_arg,
7260 got_worktree_patch_cb patch_cb, void *patch_arg,
7261 struct got_repository *repo)
7263 const struct got_error *err = NULL, *sync_err, *unlockerr;
7264 struct got_pathlist_entry *pe;
7265 struct got_fileindex *fileindex = NULL;
7266 char *fileindex_path = NULL;
7267 struct unstage_path_arg upa;
7269 err = lock_worktree(worktree, LOCK_EX);
7270 if (err)
7271 return err;
7273 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7274 if (err)
7275 goto done;
7277 upa.worktree = worktree;
7278 upa.fileindex = fileindex;
7279 upa.repo = repo;
7280 upa.progress_cb = progress_cb;
7281 upa.progress_arg = progress_arg;
7282 upa.patch_cb = patch_cb;
7283 upa.patch_arg = patch_arg;
7284 TAILQ_FOREACH(pe, paths, entry) {
7285 err = worktree_status(worktree, pe->path, fileindex, repo,
7286 unstage_path, &upa, NULL, NULL, 0, 0);
7287 if (err)
7288 goto done;
7291 sync_err = sync_fileindex(fileindex, fileindex_path);
7292 if (sync_err && err == NULL)
7293 err = sync_err;
7294 done:
7295 free(fileindex_path);
7296 if (fileindex)
7297 got_fileindex_free(fileindex);
7298 unlockerr = lock_worktree(worktree, LOCK_SH);
7299 if (unlockerr && err == NULL)
7300 err = unlockerr;
7301 return err;