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 * Merge a symlink into the work tree, where blob_orig acts as the common
848 * ancestor, blob_deriv acts as the first derived version, and the symlink
849 * on disk acts as the second derived version.
850 * Assume that contents of both blobs represent symlinks.
851 */
852 static const struct got_error *
853 merge_symlink(struct got_worktree *worktree,
854 struct got_blob_object *blob_orig, const char *ondisk_path,
855 const char *path, uint16_t st_mode, const char *label_orig,
856 struct got_blob_object *blob_deriv,
857 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
858 got_worktree_checkout_cb progress_cb, void *progress_arg)
860 const struct got_error *err = NULL;
861 char *ancestor_target = NULL, *deriv_target = NULL;
862 struct stat sb;
863 ssize_t ondisk_len;
864 char ondisk_target[PATH_MAX];
866 if (lstat(ondisk_path, &sb) == -1)
867 return got_error_from_errno2("lstat", ondisk_path);
869 if (!S_ISLNK(sb.st_mode)) {
870 /* TODO symlink is obstructed; do something */
871 return got_error_path(ondisk_path, GOT_ERR_FILE_OBSTRUCTED);
874 ondisk_len = readlink(ondisk_path, ondisk_target,
875 sizeof(ondisk_target));
876 if (ondisk_len == -1) {
877 err = got_error_from_errno2("readlink",
878 ondisk_path);
879 goto done;
882 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
883 if (err)
884 goto done;
886 err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
887 if (err)
888 goto done;
890 if (ondisk_len != strlen(ancestor_target) ||
891 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0) {
892 /*
893 * The symlink has changed on-disk (second derived version).
894 * Keep that change and discard the incoming change (first
895 * derived version).
896 * TODO: Need tree-conflict resolution to handle this.
897 */
898 err = (*progress_cb)(progress_arg, GOT_STATUS_OBSTRUCTED,
899 path);
900 } else if (ondisk_len == strlen(deriv_target) &&
901 memcmp(ondisk_target, deriv_target, ondisk_len) == 0) {
902 /* Both versions made the same change. */
903 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
904 } else {
905 /* Apply the incoming change. */
906 err = update_symlink(ondisk_path, deriv_target,
907 strlen(deriv_target));
908 if (err)
909 goto done;
910 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
912 done:
913 free(ancestor_target);
914 free(deriv_target);
915 return err;
918 /*
919 * Perform a 3-way merge where blob_orig acts as the common ancestor,
920 * blob_deriv acts as the first derived version, and the file on disk
921 * acts as the second derived version.
922 */
923 static const struct got_error *
924 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
925 struct got_blob_object *blob_orig, const char *ondisk_path,
926 const char *path, uint16_t st_mode, const char *label_orig,
927 struct got_blob_object *blob_deriv,
928 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
929 got_worktree_checkout_cb progress_cb, void *progress_arg)
931 const struct got_error *err = NULL;
932 FILE *f_deriv = NULL;
933 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
934 char *label_deriv = NULL, *parent;
936 *local_changes_subsumed = 0;
938 parent = dirname(ondisk_path);
939 if (parent == NULL)
940 return got_error_from_errno2("dirname", ondisk_path);
942 free(base_path);
943 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
944 err = got_error_from_errno("asprintf");
945 base_path = NULL;
946 goto done;
949 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
950 if (err)
951 goto done;
952 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
953 blob_deriv);
954 if (err)
955 goto done;
957 err = got_object_id_str(&id_str, deriv_base_commit_id);
958 if (err)
959 goto done;
960 if (asprintf(&label_deriv, "%s: commit %s",
961 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
962 err = got_error_from_errno("asprintf");
963 goto done;
966 err = merge_file(local_changes_subsumed, worktree, blob_orig,
967 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
968 label_deriv, repo, progress_cb, progress_arg);
969 done:
970 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
971 err = got_error_from_errno("fclose");
972 free(base_path);
973 if (blob_deriv_path) {
974 unlink(blob_deriv_path);
975 free(blob_deriv_path);
977 free(id_str);
978 free(label_deriv);
979 return err;
982 static const struct got_error *
983 create_fileindex_entry(struct got_fileindex *fileindex,
984 struct got_object_id *base_commit_id, const char *ondisk_path,
985 const char *path, struct got_object_id *blob_id)
987 const struct got_error *err = NULL;
988 struct got_fileindex_entry *new_ie;
990 err = got_fileindex_entry_alloc(&new_ie, path);
991 if (err)
992 return err;
994 err = got_fileindex_entry_update(new_ie, ondisk_path,
995 blob_id->sha1, base_commit_id->sha1, 1);
996 if (err)
997 goto done;
999 err = got_fileindex_entry_add(fileindex, new_ie);
1000 done:
1001 if (err)
1002 got_fileindex_entry_free(new_ie);
1003 return err;
1006 static mode_t
1007 get_ondisk_perms(int executable, mode_t st_mode)
1009 mode_t xbits = S_IXUSR;
1011 if (executable) {
1012 /* Map read bits to execute bits. */
1013 if (st_mode & S_IRGRP)
1014 xbits |= S_IXGRP;
1015 if (st_mode & S_IROTH)
1016 xbits |= S_IXOTH;
1017 return st_mode | xbits;
1020 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1023 /* forward declaration */
1024 static const struct got_error *
1025 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1026 const char *path, mode_t te_mode, mode_t st_mode,
1027 struct got_blob_object *blob, int restoring_missing_file,
1028 int reverting_versioned_file, int installing_bad_symlink,
1029 struct got_repository *repo,
1030 got_worktree_checkout_cb progress_cb, void *progress_arg);
1032 static const struct got_error *
1033 install_symlink(struct got_worktree *worktree, const char *ondisk_path,
1034 const char *path, mode_t te_mode, mode_t st_mode,
1035 struct got_blob_object *blob, int restoring_missing_file,
1036 int reverting_versioned_file, struct got_repository *repo,
1037 got_worktree_checkout_cb progress_cb, void *progress_arg)
1039 const struct got_error *err = NULL;
1040 char target_path[PATH_MAX];
1041 size_t len, target_len = 0;
1042 char *resolved_path = NULL, *abspath = NULL;
1043 char *path_got = NULL;
1044 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1045 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1048 * Blob object content specifies the target path of the link.
1049 * If a symbolic link cannot be installed we instead create
1050 * a regular file which contains the link target path stored
1051 * in the blob object.
1053 do {
1054 err = got_object_blob_read_block(&len, blob);
1055 if (len + target_len >= sizeof(target_path)) {
1056 /* Path too long; install as a regular file. */
1057 got_object_blob_rewind(blob);
1058 return install_blob(worktree, ondisk_path, path,
1059 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1060 restoring_missing_file, reverting_versioned_file,
1061 1, repo, progress_cb, progress_arg);
1063 if (len > 0) {
1064 /* Skip blob object header first time around. */
1065 memcpy(target_path + target_len, buf + hdrlen,
1066 len - hdrlen);
1067 target_len += len - hdrlen;
1068 hdrlen = 0;
1070 } while (len != 0);
1071 target_path[target_len] = '\0';
1074 * Relative symlink target lookup should begin at the directory
1075 * in which the blob object is being installed.
1077 if (!got_path_is_absolute(target_path)) {
1078 char *parent = dirname(ondisk_path);
1079 if (parent == NULL) {
1080 err = got_error_from_errno2("dirname", ondisk_path);
1081 goto done;
1083 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1084 err = got_error_from_errno("asprintf");
1085 goto done;
1090 * unveil(2) restricts our view of paths in the filesystem.
1091 * ENOENT will occur if a link target path does not exist or
1092 * if it points outside our unveiled path space.
1094 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1095 if (resolved_path == NULL) {
1096 if (errno != ENOENT) {
1097 err = got_error_from_errno2("realpath", target_path);
1098 goto done;
1102 /* Only allow symlinks pointing at paths within the work tree. */
1103 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1104 abspath : target_path), worktree->root_path,
1105 strlen(worktree->root_path))) {
1106 /* install as a regular file */
1107 got_object_blob_rewind(blob);
1108 err = install_blob(worktree, ondisk_path, path,
1109 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1110 restoring_missing_file, reverting_versioned_file, 1,
1111 repo, progress_cb, progress_arg);
1112 goto done;
1115 /* Do not allow symlinks pointing into the .got directory. */
1116 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1117 GOT_WORKTREE_GOT_DIR) == -1) {
1118 err = got_error_from_errno("asprintf");
1119 goto done;
1121 if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1122 abspath : target_path), path_got, strlen(path_got))) {
1123 /* install as a regular file */
1124 got_object_blob_rewind(blob);
1125 err = install_blob(worktree, ondisk_path, path,
1126 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1127 restoring_missing_file, reverting_versioned_file, 1,
1128 repo, progress_cb, progress_arg);
1129 goto done;
1132 if (symlink(target_path, ondisk_path) == -1) {
1133 if (errno == EEXIST) {
1134 struct stat sb;
1135 ssize_t elen;
1136 char etarget[PATH_MAX];
1137 if (lstat(ondisk_path, &sb) == -1) {
1138 err = got_error_from_errno2("lstat",
1139 ondisk_path);
1140 goto done;
1142 if (!S_ISLNK(sb.st_mode)) {
1143 err = got_error_path(ondisk_path,
1144 GOT_ERR_FILE_OBSTRUCTED);
1145 goto done;
1147 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1148 if (elen == -1) {
1149 err = got_error_from_errno2("readlink",
1150 ondisk_path);
1151 goto done;
1153 if (elen == target_len &&
1154 memcmp(etarget, target_path, target_len) == 0) {
1155 err = NULL; /* nothing to do */
1156 goto done;
1157 } else {
1158 err = update_symlink(ondisk_path, target_path,
1159 target_len);
1160 if (err)
1161 goto done;
1162 if (progress_cb) {
1163 err = (*progress_cb)(progress_arg,
1164 GOT_STATUS_UPDATE, path);
1166 goto done;
1170 if (errno == ENOENT) {
1171 char *parent = dirname(ondisk_path);
1172 if (parent == NULL) {
1173 err = got_error_from_errno2("dirname",
1174 ondisk_path);
1175 goto done;
1177 err = add_dir_on_disk(worktree, parent);
1178 if (err)
1179 goto done;
1181 * Retry, and fall through to error handling
1182 * below if this second attempt fails.
1184 if (symlink(target_path, ondisk_path) != -1) {
1185 err = NULL; /* success */
1186 goto done;
1190 /* Handle errors from first or second creation attempt. */
1191 if (errno == ENAMETOOLONG) {
1192 /* bad target path; install as a regular file */
1193 got_object_blob_rewind(blob);
1194 err = install_blob(worktree, ondisk_path, path,
1195 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1196 restoring_missing_file, reverting_versioned_file, 1,
1197 repo, progress_cb, progress_arg);
1198 } else if (errno == ENOTDIR) {
1199 err = got_error_path(ondisk_path,
1200 GOT_ERR_FILE_OBSTRUCTED);
1201 } else {
1202 err = got_error_from_errno3("symlink",
1203 target_path, ondisk_path);
1205 } else if (progress_cb)
1206 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1207 done:
1208 free(resolved_path);
1209 free(abspath);
1210 free(path_got);
1211 return err;
1214 static const struct got_error *
1215 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1216 const char *path, mode_t te_mode, mode_t st_mode,
1217 struct got_blob_object *blob, int restoring_missing_file,
1218 int reverting_versioned_file, int installing_bad_symlink,
1219 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1220 void *progress_arg)
1222 const struct got_error *err = NULL;
1223 int fd = -1;
1224 size_t len, hdrlen;
1225 int update = 0;
1226 char *tmppath = NULL;
1228 if (S_ISLNK(te_mode))
1229 return install_symlink(worktree, ondisk_path, path, te_mode,
1230 st_mode, blob, restoring_missing_file,
1231 reverting_versioned_file, repo, progress_cb, progress_arg);
1233 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1234 GOT_DEFAULT_FILE_MODE);
1235 if (fd == -1) {
1236 if (errno == ENOENT) {
1237 char *parent = dirname(path);
1238 if (parent == NULL)
1239 return got_error_from_errno2("dirname", path);
1240 err = add_dir_on_disk(worktree, parent);
1241 if (err)
1242 return err;
1243 fd = open(ondisk_path,
1244 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1245 GOT_DEFAULT_FILE_MODE);
1246 if (fd == -1)
1247 return got_error_from_errno2("open",
1248 ondisk_path);
1249 } else if (errno == EEXIST) {
1250 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1251 /* TODO file is obstructed; do something */
1252 err = got_error_path(ondisk_path,
1253 GOT_ERR_FILE_OBSTRUCTED);
1254 goto done;
1255 } else {
1256 err = got_opentemp_named_fd(&tmppath, &fd,
1257 ondisk_path);
1258 if (err)
1259 goto done;
1260 update = 1;
1262 } else
1263 return got_error_from_errno2("open", ondisk_path);
1266 if (progress_cb) {
1267 if (restoring_missing_file)
1268 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1269 path);
1270 else if (reverting_versioned_file)
1271 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1272 path);
1273 else
1274 err = (*progress_cb)(progress_arg,
1275 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1276 if (err)
1277 goto done;
1280 hdrlen = got_object_blob_get_hdrlen(blob);
1281 do {
1282 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1283 err = got_object_blob_read_block(&len, blob);
1284 if (err)
1285 break;
1286 if (len > 0) {
1287 /* Skip blob object header first time around. */
1288 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1289 if (outlen == -1) {
1290 err = got_error_from_errno("write");
1291 goto done;
1292 } else if (outlen != len - hdrlen) {
1293 err = got_error(GOT_ERR_IO);
1294 goto done;
1296 hdrlen = 0;
1298 } while (len != 0);
1300 if (fsync(fd) != 0) {
1301 err = got_error_from_errno("fsync");
1302 goto done;
1305 if (update) {
1306 if (rename(tmppath, ondisk_path) != 0) {
1307 err = got_error_from_errno3("rename", tmppath,
1308 ondisk_path);
1309 unlink(tmppath);
1310 goto done;
1314 if (chmod(ondisk_path,
1315 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1316 err = got_error_from_errno2("chmod", ondisk_path);
1317 goto done;
1320 done:
1321 if (fd != -1 && close(fd) != 0 && err == NULL)
1322 err = got_error_from_errno("close");
1323 free(tmppath);
1324 return err;
1327 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1328 static const struct got_error *
1329 get_modified_file_content_status(unsigned char *status, FILE *f)
1331 const struct got_error *err = NULL;
1332 const char *markers[3] = {
1333 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1334 GOT_DIFF_CONFLICT_MARKER_SEP,
1335 GOT_DIFF_CONFLICT_MARKER_END
1337 int i = 0;
1338 char *line;
1339 size_t len;
1340 const char delim[3] = {'\0', '\0', '\0'};
1342 while (*status == GOT_STATUS_MODIFY) {
1343 line = fparseln(f, &len, NULL, delim, 0);
1344 if (line == NULL) {
1345 if (feof(f))
1346 break;
1347 err = got_ferror(f, GOT_ERR_IO);
1348 break;
1351 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1352 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1353 == 0)
1354 *status = GOT_STATUS_CONFLICT;
1355 else
1356 i++;
1360 return err;
1363 static int
1364 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1366 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1367 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1370 static int
1371 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1373 return !(ie->ctime_sec == sb->st_ctime &&
1374 ie->ctime_nsec == sb->st_ctimensec &&
1375 ie->mtime_sec == sb->st_mtime &&
1376 ie->mtime_nsec == sb->st_mtimensec &&
1377 ie->size == (sb->st_size & 0xffffffff) &&
1378 !xbit_differs(ie, sb->st_mode));
1381 static unsigned char
1382 get_staged_status(struct got_fileindex_entry *ie)
1384 switch (got_fileindex_entry_stage_get(ie)) {
1385 case GOT_FILEIDX_STAGE_ADD:
1386 return GOT_STATUS_ADD;
1387 case GOT_FILEIDX_STAGE_DELETE:
1388 return GOT_STATUS_DELETE;
1389 case GOT_FILEIDX_STAGE_MODIFY:
1390 return GOT_STATUS_MODIFY;
1391 default:
1392 return GOT_STATUS_NO_CHANGE;
1396 static const struct got_error *
1397 get_symlink_status(unsigned char *status, struct stat *sb,
1398 struct got_fileindex_entry *ie, const char *abspath,
1399 int dirfd, const char *de_name, struct got_blob_object *blob)
1401 const struct got_error *err = NULL;
1402 char target_path[PATH_MAX];
1403 char etarget[PATH_MAX];
1404 ssize_t elen;
1405 size_t len, target_len = 0;
1406 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1407 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1409 *status = GOT_STATUS_NO_CHANGE;
1411 /* Blob object content specifies the target path of the link. */
1412 do {
1413 err = got_object_blob_read_block(&len, blob);
1414 if (err)
1415 return err;
1416 if (len + target_len >= sizeof(target_path)) {
1418 * Should not happen. The blob contents were OK
1419 * when this symlink was installed.
1421 return got_error(GOT_ERR_NO_SPACE);
1423 if (len > 0) {
1424 /* Skip blob object header first time around. */
1425 memcpy(target_path + target_len, buf + hdrlen,
1426 len - hdrlen);
1427 target_len += len - hdrlen;
1428 hdrlen = 0;
1430 } while (len != 0);
1431 target_path[target_len] = '\0';
1433 if (dirfd != -1) {
1434 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1435 if (elen == -1)
1436 return got_error_from_errno2("readlinkat", abspath);
1437 } else {
1438 elen = readlink(abspath, etarget, sizeof(etarget));
1439 if (elen == -1)
1440 return got_error_from_errno2("readlinkat", abspath);
1443 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1444 *status = GOT_STATUS_MODIFY;
1446 return NULL;
1449 static const struct got_error *
1450 get_file_status(unsigned char *status, struct stat *sb,
1451 struct got_fileindex_entry *ie, const char *abspath,
1452 int dirfd, const char *de_name, struct got_repository *repo)
1454 const struct got_error *err = NULL;
1455 struct got_object_id id;
1456 size_t hdrlen;
1457 int fd = -1;
1458 FILE *f = NULL;
1459 uint8_t fbuf[8192];
1460 struct got_blob_object *blob = NULL;
1461 size_t flen, blen;
1462 unsigned char staged_status = get_staged_status(ie);
1464 *status = GOT_STATUS_NO_CHANGE;
1467 * Whenever the caller provides a directory descriptor and a
1468 * directory entry name for the file, use them! This prevents
1469 * race conditions if filesystem paths change beneath our feet.
1471 if (dirfd != -1) {
1472 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1473 if (errno == ENOENT) {
1474 if (got_fileindex_entry_has_file_on_disk(ie))
1475 *status = GOT_STATUS_MISSING;
1476 else
1477 *status = GOT_STATUS_DELETE;
1478 goto done;
1480 err = got_error_from_errno2("fstatat", abspath);
1481 goto done;
1483 } else {
1484 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1485 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1486 return got_error_from_errno2("open", abspath);
1487 else if (fd == -1 && errno == ELOOP) {
1488 if (lstat(abspath, sb) == -1)
1489 return got_error_from_errno2("lstat", abspath);
1490 } else if (fd == -1 || fstat(fd, sb) == -1) {
1491 if (errno == ENOENT) {
1492 if (got_fileindex_entry_has_file_on_disk(ie))
1493 *status = GOT_STATUS_MISSING;
1494 else
1495 *status = GOT_STATUS_DELETE;
1496 goto done;
1498 err = got_error_from_errno2("fstat", abspath);
1499 goto done;
1503 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1504 *status = GOT_STATUS_OBSTRUCTED;
1505 goto done;
1508 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1509 *status = GOT_STATUS_DELETE;
1510 goto done;
1511 } else if (!got_fileindex_entry_has_blob(ie) &&
1512 staged_status != GOT_STATUS_ADD) {
1513 *status = GOT_STATUS_ADD;
1514 goto done;
1517 if (!stat_info_differs(ie, sb))
1518 goto done;
1520 if (staged_status == GOT_STATUS_MODIFY ||
1521 staged_status == GOT_STATUS_ADD)
1522 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1523 else
1524 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1526 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1527 if (err)
1528 goto done;
1530 if (S_ISLNK(sb->st_mode)) {
1531 /* Staging changes to symlinks is not yet(?) supported. */
1532 if (staged_status != GOT_STATUS_NO_CHANGE) {
1533 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1534 goto done;
1536 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1537 de_name, blob);
1538 goto done;
1542 if (dirfd != -1) {
1543 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1544 if (fd == -1) {
1545 err = got_error_from_errno2("openat", abspath);
1546 goto done;
1550 f = fdopen(fd, "r");
1551 if (f == NULL) {
1552 err = got_error_from_errno2("fdopen", abspath);
1553 goto done;
1555 fd = -1;
1556 hdrlen = got_object_blob_get_hdrlen(blob);
1557 for (;;) {
1558 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1559 err = got_object_blob_read_block(&blen, blob);
1560 if (err)
1561 goto done;
1562 /* Skip length of blob object header first time around. */
1563 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1564 if (flen == 0 && ferror(f)) {
1565 err = got_error_from_errno("fread");
1566 goto done;
1568 if (blen == 0) {
1569 if (flen != 0)
1570 *status = GOT_STATUS_MODIFY;
1571 break;
1572 } else if (flen == 0) {
1573 if (blen != 0)
1574 *status = GOT_STATUS_MODIFY;
1575 break;
1576 } else if (blen - hdrlen == flen) {
1577 /* Skip blob object header first time around. */
1578 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1579 *status = GOT_STATUS_MODIFY;
1580 break;
1582 } else {
1583 *status = GOT_STATUS_MODIFY;
1584 break;
1586 hdrlen = 0;
1589 if (*status == GOT_STATUS_MODIFY) {
1590 rewind(f);
1591 err = get_modified_file_content_status(status, f);
1592 } else if (xbit_differs(ie, sb->st_mode))
1593 *status = GOT_STATUS_MODE_CHANGE;
1594 done:
1595 if (blob)
1596 got_object_blob_close(blob);
1597 if (f != NULL && fclose(f) == EOF && err == NULL)
1598 err = got_error_from_errno2("fclose", abspath);
1599 if (fd != -1 && close(fd) == -1 && err == NULL)
1600 err = got_error_from_errno2("close", abspath);
1601 return err;
1605 * Update timestamps in the file index if a file is unmodified and
1606 * we had to run a full content comparison to find out.
1608 static const struct got_error *
1609 sync_timestamps(char *ondisk_path, unsigned char status,
1610 struct got_fileindex_entry *ie, struct stat *sb)
1612 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1613 return got_fileindex_entry_update(ie, ondisk_path,
1614 ie->blob_sha1, ie->commit_sha1, 1);
1616 return NULL;
1619 static const struct got_error *
1620 update_blob(struct got_worktree *worktree,
1621 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1622 struct got_tree_entry *te, const char *path,
1623 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1624 void *progress_arg)
1626 const struct got_error *err = NULL;
1627 struct got_blob_object *blob = NULL;
1628 char *ondisk_path;
1629 unsigned char status = GOT_STATUS_NO_CHANGE;
1630 struct stat sb;
1632 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1633 return got_error_from_errno("asprintf");
1635 if (ie) {
1636 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1637 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1638 goto done;
1640 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1641 repo);
1642 if (err)
1643 goto done;
1644 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1645 sb.st_mode = got_fileindex_perms_to_st(ie);
1646 } else
1647 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1649 if (status == GOT_STATUS_OBSTRUCTED) {
1650 err = (*progress_cb)(progress_arg, status, path);
1651 goto done;
1653 if (status == GOT_STATUS_CONFLICT) {
1654 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1655 path);
1656 goto done;
1659 if (ie && status != GOT_STATUS_MISSING &&
1660 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1661 if (got_fileindex_entry_has_commit(ie) &&
1662 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1663 SHA1_DIGEST_LENGTH) == 0) {
1664 err = sync_timestamps(ondisk_path, status, ie, &sb);
1665 if (err)
1666 goto done;
1667 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1668 path);
1669 goto done;
1671 if (got_fileindex_entry_has_blob(ie) &&
1672 memcmp(ie->blob_sha1, te->id.sha1,
1673 SHA1_DIGEST_LENGTH) == 0) {
1674 err = sync_timestamps(ondisk_path, status, ie, &sb);
1675 goto done;
1679 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1680 if (err)
1681 goto done;
1683 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1684 int update_timestamps;
1685 struct got_blob_object *blob2 = NULL;
1686 char *label_orig = NULL;
1687 if (got_fileindex_entry_has_blob(ie)) {
1688 struct got_object_id id2;
1689 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1690 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1691 if (err)
1692 goto done;
1694 if (got_fileindex_entry_has_commit(ie)) {
1695 char id_str[SHA1_DIGEST_STRING_LENGTH];
1696 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1697 sizeof(id_str)) == NULL) {
1698 err = got_error_path(id_str,
1699 GOT_ERR_BAD_OBJ_ID_STR);
1700 goto done;
1702 if (asprintf(&label_orig, "%s: commit %s",
1703 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1704 err = got_error_from_errno("asprintf");
1705 goto done;
1708 err = merge_blob(&update_timestamps, worktree, blob2,
1709 ondisk_path, path, sb.st_mode, label_orig, blob,
1710 worktree->base_commit_id, repo,
1711 progress_cb, progress_arg);
1712 free(label_orig);
1713 if (blob2)
1714 got_object_blob_close(blob2);
1715 if (err)
1716 goto done;
1718 * Do not update timestamps of files with local changes.
1719 * Otherwise, a future status walk would treat them as
1720 * unmodified files again.
1722 err = got_fileindex_entry_update(ie, ondisk_path,
1723 blob->id.sha1, worktree->base_commit_id->sha1,
1724 update_timestamps);
1725 } else if (status == GOT_STATUS_MODE_CHANGE) {
1726 err = got_fileindex_entry_update(ie, ondisk_path,
1727 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1728 } else if (status == GOT_STATUS_DELETE) {
1729 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1730 if (err)
1731 goto done;
1732 err = got_fileindex_entry_update(ie, ondisk_path,
1733 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1734 if (err)
1735 goto done;
1736 } else {
1737 err = install_blob(worktree, ondisk_path, path, te->mode,
1738 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0, 0,
1739 repo, progress_cb, progress_arg);
1740 if (err)
1741 goto done;
1742 if (ie) {
1743 err = got_fileindex_entry_update(ie, ondisk_path,
1744 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1745 } else {
1746 err = create_fileindex_entry(fileindex,
1747 worktree->base_commit_id, ondisk_path, path,
1748 &blob->id);
1750 if (err)
1751 goto done;
1753 got_object_blob_close(blob);
1754 done:
1755 free(ondisk_path);
1756 return err;
1759 static const struct got_error *
1760 remove_ondisk_file(const char *root_path, const char *path)
1762 const struct got_error *err = NULL;
1763 char *ondisk_path = NULL;
1765 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1766 return got_error_from_errno("asprintf");
1768 if (unlink(ondisk_path) == -1) {
1769 if (errno != ENOENT)
1770 err = got_error_from_errno2("unlink", ondisk_path);
1771 } else {
1772 char *parent = dirname(ondisk_path);
1773 while (parent && strcmp(parent, root_path) != 0) {
1774 if (rmdir(parent) == -1) {
1775 if (errno != ENOTEMPTY)
1776 err = got_error_from_errno2("rmdir",
1777 parent);
1778 break;
1780 parent = dirname(parent);
1783 free(ondisk_path);
1784 return err;
1787 static const struct got_error *
1788 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1789 struct got_fileindex_entry *ie, struct got_repository *repo,
1790 got_worktree_checkout_cb progress_cb, void *progress_arg)
1792 const struct got_error *err = NULL;
1793 unsigned char status;
1794 struct stat sb;
1795 char *ondisk_path;
1797 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1798 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1800 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1801 == -1)
1802 return got_error_from_errno("asprintf");
1804 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1805 if (err)
1806 goto done;
1808 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1809 status == GOT_STATUS_ADD) {
1810 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1811 if (err)
1812 goto done;
1814 * Preserve the working file and change the deleted blob's
1815 * entry into a schedule-add entry.
1817 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1818 0);
1819 } else {
1820 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1821 if (err)
1822 goto done;
1823 if (status == GOT_STATUS_NO_CHANGE) {
1824 err = remove_ondisk_file(worktree->root_path, ie->path);
1825 if (err)
1826 goto done;
1828 got_fileindex_entry_remove(fileindex, ie);
1830 done:
1831 free(ondisk_path);
1832 return err;
1835 struct diff_cb_arg {
1836 struct got_fileindex *fileindex;
1837 struct got_worktree *worktree;
1838 struct got_repository *repo;
1839 got_worktree_checkout_cb progress_cb;
1840 void *progress_arg;
1841 got_cancel_cb cancel_cb;
1842 void *cancel_arg;
1845 static const struct got_error *
1846 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1847 struct got_tree_entry *te, const char *parent_path)
1849 struct diff_cb_arg *a = arg;
1851 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1852 return got_error(GOT_ERR_CANCELLED);
1854 return update_blob(a->worktree, a->fileindex, ie, te,
1855 ie->path, a->repo, a->progress_cb, a->progress_arg);
1858 static const struct got_error *
1859 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1861 struct diff_cb_arg *a = arg;
1863 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1864 return got_error(GOT_ERR_CANCELLED);
1866 return delete_blob(a->worktree, a->fileindex, ie,
1867 a->repo, a->progress_cb, a->progress_arg);
1870 static const struct got_error *
1871 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1873 struct diff_cb_arg *a = arg;
1874 const struct got_error *err;
1875 char *path;
1877 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1878 return got_error(GOT_ERR_CANCELLED);
1880 if (got_object_tree_entry_is_submodule(te))
1881 return NULL;
1883 if (asprintf(&path, "%s%s%s", parent_path,
1884 parent_path[0] ? "/" : "", te->name)
1885 == -1)
1886 return got_error_from_errno("asprintf");
1888 if (S_ISDIR(te->mode))
1889 err = add_dir_on_disk(a->worktree, path);
1890 else
1891 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1892 a->repo, a->progress_cb, a->progress_arg);
1894 free(path);
1895 return err;
1898 static const struct got_error *
1899 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1901 const struct got_error *err = NULL;
1902 char *uuidstr = NULL;
1903 uint32_t uuid_status;
1905 *refname = NULL;
1907 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1908 if (uuid_status != uuid_s_ok)
1909 return got_error_uuid(uuid_status, "uuid_to_string");
1911 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1912 == -1) {
1913 err = got_error_from_errno("asprintf");
1914 *refname = NULL;
1916 free(uuidstr);
1917 return err;
1920 const struct got_error *
1921 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1923 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1926 static const struct got_error *
1927 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1929 return get_ref_name(refname, worktree,
1930 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1933 static const struct got_error *
1934 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1936 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1939 static const struct got_error *
1940 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1942 return get_ref_name(refname, worktree,
1943 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1946 static const struct got_error *
1947 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1949 return get_ref_name(refname, worktree,
1950 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1953 static const struct got_error *
1954 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1956 return get_ref_name(refname, worktree,
1957 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1960 static const struct got_error *
1961 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1963 return get_ref_name(refname, worktree,
1964 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1967 static const struct got_error *
1968 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1970 return get_ref_name(refname, worktree,
1971 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1974 static const struct got_error *
1975 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1977 return get_ref_name(refname, worktree,
1978 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1981 const struct got_error *
1982 got_worktree_get_histedit_script_path(char **path,
1983 struct got_worktree *worktree)
1985 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1986 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1987 *path = NULL;
1988 return got_error_from_errno("asprintf");
1990 return NULL;
1994 * Prevent Git's garbage collector from deleting our base commit by
1995 * setting a reference to our base commit's ID.
1997 static const struct got_error *
1998 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2000 const struct got_error *err = NULL;
2001 struct got_reference *ref = NULL;
2002 char *refname;
2004 err = got_worktree_get_base_ref_name(&refname, worktree);
2005 if (err)
2006 return err;
2008 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2009 if (err)
2010 goto done;
2012 err = got_ref_write(ref, repo);
2013 done:
2014 free(refname);
2015 if (ref)
2016 got_ref_close(ref);
2017 return err;
2020 static const struct got_error *
2021 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2023 const struct got_error *err = NULL;
2025 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2026 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2027 err = got_error_from_errno("asprintf");
2028 *fileindex_path = NULL;
2030 return err;
2034 static const struct got_error *
2035 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2036 struct got_worktree *worktree)
2038 const struct got_error *err = NULL;
2039 FILE *index = NULL;
2041 *fileindex_path = NULL;
2042 *fileindex = got_fileindex_alloc();
2043 if (*fileindex == NULL)
2044 return got_error_from_errno("got_fileindex_alloc");
2046 err = get_fileindex_path(fileindex_path, worktree);
2047 if (err)
2048 goto done;
2050 index = fopen(*fileindex_path, "rb");
2051 if (index == NULL) {
2052 if (errno != ENOENT)
2053 err = got_error_from_errno2("fopen", *fileindex_path);
2054 } else {
2055 err = got_fileindex_read(*fileindex, index);
2056 if (fclose(index) != 0 && err == NULL)
2057 err = got_error_from_errno("fclose");
2059 done:
2060 if (err) {
2061 free(*fileindex_path);
2062 *fileindex_path = NULL;
2063 got_fileindex_free(*fileindex);
2064 *fileindex = NULL;
2066 return err;
2069 struct bump_base_commit_id_arg {
2070 struct got_object_id *base_commit_id;
2071 const char *path;
2072 size_t path_len;
2073 const char *entry_name;
2074 got_worktree_checkout_cb progress_cb;
2075 void *progress_arg;
2078 /* Bump base commit ID of all files within an updated part of the work tree. */
2079 static const struct got_error *
2080 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2082 const struct got_error *err;
2083 struct bump_base_commit_id_arg *a = arg;
2085 if (a->entry_name) {
2086 if (strcmp(ie->path, a->path) != 0)
2087 return NULL;
2088 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2089 return NULL;
2091 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2092 SHA1_DIGEST_LENGTH) == 0)
2093 return NULL;
2095 if (a->progress_cb) {
2096 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2097 ie->path);
2098 if (err)
2099 return err;
2101 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2102 return NULL;
2105 static const struct got_error *
2106 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2108 const struct got_error *err = NULL;
2109 char *new_fileindex_path = NULL;
2110 FILE *new_index = NULL;
2111 struct timespec timeout;
2113 err = got_opentemp_named(&new_fileindex_path, &new_index,
2114 fileindex_path);
2115 if (err)
2116 goto done;
2118 err = got_fileindex_write(fileindex, new_index);
2119 if (err)
2120 goto done;
2122 if (rename(new_fileindex_path, fileindex_path) != 0) {
2123 err = got_error_from_errno3("rename", new_fileindex_path,
2124 fileindex_path);
2125 unlink(new_fileindex_path);
2129 * Sleep for a short amount of time to ensure that files modified after
2130 * this program exits have a different time stamp from the one which
2131 * was recorded in the file index.
2133 timeout.tv_sec = 0;
2134 timeout.tv_nsec = 1;
2135 nanosleep(&timeout, NULL);
2136 done:
2137 if (new_index)
2138 fclose(new_index);
2139 free(new_fileindex_path);
2140 return err;
2143 static const struct got_error *
2144 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2145 struct got_object_id **tree_id, const char *wt_relpath,
2146 struct got_worktree *worktree, struct got_repository *repo)
2148 const struct got_error *err = NULL;
2149 struct got_object_id *id = NULL;
2150 char *in_repo_path = NULL;
2151 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2153 *entry_type = GOT_OBJ_TYPE_ANY;
2154 *tree_relpath = NULL;
2155 *tree_id = NULL;
2157 if (wt_relpath[0] == '\0') {
2158 /* Check out all files within the work tree. */
2159 *entry_type = GOT_OBJ_TYPE_TREE;
2160 *tree_relpath = strdup("");
2161 if (*tree_relpath == NULL) {
2162 err = got_error_from_errno("strdup");
2163 goto done;
2165 err = got_object_id_by_path(tree_id, repo,
2166 worktree->base_commit_id, worktree->path_prefix);
2167 if (err)
2168 goto done;
2169 return NULL;
2172 /* Check out a subset of files in the work tree. */
2174 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2175 is_root_wt ? "" : "/", wt_relpath) == -1) {
2176 err = got_error_from_errno("asprintf");
2177 goto done;
2180 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2181 in_repo_path);
2182 if (err)
2183 goto done;
2185 free(in_repo_path);
2186 in_repo_path = NULL;
2188 err = got_object_get_type(entry_type, repo, id);
2189 if (err)
2190 goto done;
2192 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2193 /* Check out a single file. */
2194 if (strchr(wt_relpath, '/') == NULL) {
2195 /* Check out a single file in work tree's root dir. */
2196 in_repo_path = strdup(worktree->path_prefix);
2197 if (in_repo_path == NULL) {
2198 err = got_error_from_errno("strdup");
2199 goto done;
2201 *tree_relpath = strdup("");
2202 if (*tree_relpath == NULL) {
2203 err = got_error_from_errno("strdup");
2204 goto done;
2206 } else {
2207 /* Check out a single file in a subdirectory. */
2208 err = got_path_dirname(tree_relpath, wt_relpath);
2209 if (err)
2210 return err;
2211 if (asprintf(&in_repo_path, "%s%s%s",
2212 worktree->path_prefix, is_root_wt ? "" : "/",
2213 *tree_relpath) == -1) {
2214 err = got_error_from_errno("asprintf");
2215 goto done;
2218 err = got_object_id_by_path(tree_id, repo,
2219 worktree->base_commit_id, in_repo_path);
2220 } else {
2221 /* Check out all files within a subdirectory. */
2222 *tree_id = got_object_id_dup(id);
2223 if (*tree_id == NULL) {
2224 err = got_error_from_errno("got_object_id_dup");
2225 goto done;
2227 *tree_relpath = strdup(wt_relpath);
2228 if (*tree_relpath == NULL) {
2229 err = got_error_from_errno("strdup");
2230 goto done;
2233 done:
2234 free(id);
2235 free(in_repo_path);
2236 if (err) {
2237 *entry_type = GOT_OBJ_TYPE_ANY;
2238 free(*tree_relpath);
2239 *tree_relpath = NULL;
2240 free(*tree_id);
2241 *tree_id = NULL;
2243 return err;
2246 static const struct got_error *
2247 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2248 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2249 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2250 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2252 const struct got_error *err = NULL;
2253 struct got_commit_object *commit = NULL;
2254 struct got_tree_object *tree = NULL;
2255 struct got_fileindex_diff_tree_cb diff_cb;
2256 struct diff_cb_arg arg;
2258 err = ref_base_commit(worktree, repo);
2259 if (err) {
2260 if (!(err->code == GOT_ERR_ERRNO &&
2261 (errno == EACCES || errno == EROFS)))
2262 goto done;
2263 err = (*progress_cb)(progress_arg,
2264 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2265 if (err)
2266 return err;
2269 err = got_object_open_as_commit(&commit, repo,
2270 worktree->base_commit_id);
2271 if (err)
2272 goto done;
2274 err = got_object_open_as_tree(&tree, repo, tree_id);
2275 if (err)
2276 goto done;
2278 if (entry_name &&
2279 got_object_tree_find_entry(tree, entry_name) == NULL) {
2280 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2281 goto done;
2284 diff_cb.diff_old_new = diff_old_new;
2285 diff_cb.diff_old = diff_old;
2286 diff_cb.diff_new = diff_new;
2287 arg.fileindex = fileindex;
2288 arg.worktree = worktree;
2289 arg.repo = repo;
2290 arg.progress_cb = progress_cb;
2291 arg.progress_arg = progress_arg;
2292 arg.cancel_cb = cancel_cb;
2293 arg.cancel_arg = cancel_arg;
2294 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2295 entry_name, repo, &diff_cb, &arg);
2296 done:
2297 if (tree)
2298 got_object_tree_close(tree);
2299 if (commit)
2300 got_object_commit_close(commit);
2301 return err;
2304 const struct got_error *
2305 got_worktree_checkout_files(struct got_worktree *worktree,
2306 struct got_pathlist_head *paths, struct got_repository *repo,
2307 got_worktree_checkout_cb progress_cb, void *progress_arg,
2308 got_cancel_cb cancel_cb, void *cancel_arg)
2310 const struct got_error *err = NULL, *sync_err, *unlockerr;
2311 struct got_commit_object *commit = NULL;
2312 struct got_tree_object *tree = NULL;
2313 struct got_fileindex *fileindex = NULL;
2314 char *fileindex_path = NULL;
2315 struct got_pathlist_entry *pe;
2316 struct tree_path_data {
2317 SIMPLEQ_ENTRY(tree_path_data) entry;
2318 struct got_object_id *tree_id;
2319 int entry_type;
2320 char *relpath;
2321 char *entry_name;
2322 } *tpd = NULL;
2323 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2325 SIMPLEQ_INIT(&tree_paths);
2327 err = lock_worktree(worktree, LOCK_EX);
2328 if (err)
2329 return err;
2331 /* Map all specified paths to in-repository trees. */
2332 TAILQ_FOREACH(pe, paths, entry) {
2333 tpd = malloc(sizeof(*tpd));
2334 if (tpd == NULL) {
2335 err = got_error_from_errno("malloc");
2336 goto done;
2339 err = find_tree_entry_for_checkout(&tpd->entry_type,
2340 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2341 if (err) {
2342 free(tpd);
2343 goto done;
2346 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2347 err = got_path_basename(&tpd->entry_name, pe->path);
2348 if (err) {
2349 free(tpd->relpath);
2350 free(tpd->tree_id);
2351 free(tpd);
2352 goto done;
2354 } else
2355 tpd->entry_name = NULL;
2357 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2361 * Read the file index.
2362 * Checking out files is supposed to be an idempotent operation.
2363 * If the on-disk file index is incomplete we will try to complete it.
2365 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2366 if (err)
2367 goto done;
2369 tpd = SIMPLEQ_FIRST(&tree_paths);
2370 TAILQ_FOREACH(pe, paths, entry) {
2371 struct bump_base_commit_id_arg bbc_arg;
2373 err = checkout_files(worktree, fileindex, tpd->relpath,
2374 tpd->tree_id, tpd->entry_name, repo,
2375 progress_cb, progress_arg, cancel_cb, cancel_arg);
2376 if (err)
2377 break;
2379 bbc_arg.base_commit_id = worktree->base_commit_id;
2380 bbc_arg.entry_name = tpd->entry_name;
2381 bbc_arg.path = pe->path;
2382 bbc_arg.path_len = pe->path_len;
2383 bbc_arg.progress_cb = progress_cb;
2384 bbc_arg.progress_arg = progress_arg;
2385 err = got_fileindex_for_each_entry_safe(fileindex,
2386 bump_base_commit_id, &bbc_arg);
2387 if (err)
2388 break;
2390 tpd = SIMPLEQ_NEXT(tpd, entry);
2392 sync_err = sync_fileindex(fileindex, fileindex_path);
2393 if (sync_err && err == NULL)
2394 err = sync_err;
2395 done:
2396 free(fileindex_path);
2397 if (tree)
2398 got_object_tree_close(tree);
2399 if (commit)
2400 got_object_commit_close(commit);
2401 if (fileindex)
2402 got_fileindex_free(fileindex);
2403 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2404 tpd = SIMPLEQ_FIRST(&tree_paths);
2405 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2406 free(tpd->relpath);
2407 free(tpd->tree_id);
2408 free(tpd);
2410 unlockerr = lock_worktree(worktree, LOCK_SH);
2411 if (unlockerr && err == NULL)
2412 err = unlockerr;
2413 return err;
2416 struct merge_file_cb_arg {
2417 struct got_worktree *worktree;
2418 struct got_fileindex *fileindex;
2419 got_worktree_checkout_cb progress_cb;
2420 void *progress_arg;
2421 got_cancel_cb cancel_cb;
2422 void *cancel_arg;
2423 const char *label_orig;
2424 struct got_object_id *commit_id2;
2427 static const struct got_error *
2428 merge_file_cb(void *arg, struct got_blob_object *blob1,
2429 struct got_blob_object *blob2, struct got_object_id *id1,
2430 struct got_object_id *id2, const char *path1, const char *path2,
2431 mode_t mode1, mode_t mode2, struct got_repository *repo)
2433 static const struct got_error *err = NULL;
2434 struct merge_file_cb_arg *a = arg;
2435 struct got_fileindex_entry *ie;
2436 char *ondisk_path = NULL;
2437 struct stat sb;
2438 unsigned char status;
2439 int local_changes_subsumed;
2441 if (blob1 && blob2) {
2442 ie = got_fileindex_entry_get(a->fileindex, path2,
2443 strlen(path2));
2444 if (ie == NULL)
2445 return (*a->progress_cb)(a->progress_arg,
2446 GOT_STATUS_MISSING, path2);
2448 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2449 path2) == -1)
2450 return got_error_from_errno("asprintf");
2452 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2453 repo);
2454 if (err)
2455 goto done;
2457 if (status == GOT_STATUS_DELETE) {
2458 err = (*a->progress_cb)(a->progress_arg,
2459 GOT_STATUS_MERGE, path2);
2460 goto done;
2462 if (status != GOT_STATUS_NO_CHANGE &&
2463 status != GOT_STATUS_MODIFY &&
2464 status != GOT_STATUS_CONFLICT &&
2465 status != GOT_STATUS_ADD) {
2466 err = (*a->progress_cb)(a->progress_arg, status, path2);
2467 goto done;
2470 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2471 err = merge_symlink(a->worktree, blob1,
2472 ondisk_path, path2, sb.st_mode, a->label_orig,
2473 blob2, a->commit_id2, repo, a->progress_cb,
2474 a->progress_arg);
2475 } else {
2476 err = merge_blob(&local_changes_subsumed, a->worktree,
2477 blob1, ondisk_path, path2, sb.st_mode,
2478 a->label_orig, blob2, a->commit_id2, repo,
2479 a->progress_cb, a->progress_arg);
2481 } else if (blob1) {
2482 ie = got_fileindex_entry_get(a->fileindex, path1,
2483 strlen(path1));
2484 if (ie == NULL)
2485 return (*a->progress_cb)(a->progress_arg,
2486 GOT_STATUS_MISSING, path1);
2488 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2489 path1) == -1)
2490 return got_error_from_errno("asprintf");
2492 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2493 repo);
2494 if (err)
2495 goto done;
2497 switch (status) {
2498 case GOT_STATUS_NO_CHANGE:
2499 err = (*a->progress_cb)(a->progress_arg,
2500 GOT_STATUS_DELETE, path1);
2501 if (err)
2502 goto done;
2503 err = remove_ondisk_file(a->worktree->root_path, path1);
2504 if (err)
2505 goto done;
2506 if (ie)
2507 got_fileindex_entry_mark_deleted_from_disk(ie);
2508 break;
2509 case GOT_STATUS_DELETE:
2510 case GOT_STATUS_MISSING:
2511 err = (*a->progress_cb)(a->progress_arg,
2512 GOT_STATUS_DELETE, path1);
2513 if (err)
2514 goto done;
2515 if (ie)
2516 got_fileindex_entry_mark_deleted_from_disk(ie);
2517 break;
2518 case GOT_STATUS_ADD:
2519 case GOT_STATUS_MODIFY:
2520 case GOT_STATUS_CONFLICT:
2521 err = (*a->progress_cb)(a->progress_arg,
2522 GOT_STATUS_CANNOT_DELETE, path1);
2523 if (err)
2524 goto done;
2525 break;
2526 case GOT_STATUS_OBSTRUCTED:
2527 err = (*a->progress_cb)(a->progress_arg, status, path1);
2528 if (err)
2529 goto done;
2530 break;
2531 default:
2532 break;
2534 } else if (blob2) {
2535 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2536 path2) == -1)
2537 return got_error_from_errno("asprintf");
2538 ie = got_fileindex_entry_get(a->fileindex, path2,
2539 strlen(path2));
2540 if (ie) {
2541 err = get_file_status(&status, &sb, ie, ondisk_path,
2542 -1, NULL, repo);
2543 if (err)
2544 goto done;
2545 if (status != GOT_STATUS_NO_CHANGE &&
2546 status != GOT_STATUS_MODIFY &&
2547 status != GOT_STATUS_CONFLICT &&
2548 status != GOT_STATUS_ADD) {
2549 err = (*a->progress_cb)(a->progress_arg,
2550 status, path2);
2551 goto done;
2553 err = merge_blob(&local_changes_subsumed, a->worktree,
2554 NULL, ondisk_path, path2, sb.st_mode,
2555 a->label_orig, blob2, a->commit_id2, repo,
2556 a->progress_cb,
2557 a->progress_arg);
2558 if (status == GOT_STATUS_DELETE) {
2559 err = got_fileindex_entry_update(ie,
2560 ondisk_path, blob2->id.sha1,
2561 a->worktree->base_commit_id->sha1, 0);
2562 if (err)
2563 goto done;
2565 } else {
2566 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2567 err = install_blob(a->worktree, ondisk_path, path2,
2568 /* XXX get this from parent tree! */
2569 GOT_DEFAULT_FILE_MODE,
2570 sb.st_mode, blob2, 0, 0, 0, repo,
2571 a->progress_cb, a->progress_arg);
2572 if (err)
2573 goto done;
2574 err = got_fileindex_entry_alloc(&ie, path2);
2575 if (err)
2576 goto done;
2577 err = got_fileindex_entry_update(ie, ondisk_path,
2578 NULL, NULL, 1);
2579 if (err) {
2580 got_fileindex_entry_free(ie);
2581 goto done;
2583 err = got_fileindex_entry_add(a->fileindex, ie);
2584 if (err) {
2585 got_fileindex_entry_free(ie);
2586 goto done;
2590 done:
2591 free(ondisk_path);
2592 return err;
2595 struct check_merge_ok_arg {
2596 struct got_worktree *worktree;
2597 struct got_repository *repo;
2600 static const struct got_error *
2601 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2603 const struct got_error *err = NULL;
2604 struct check_merge_ok_arg *a = arg;
2605 unsigned char status;
2606 struct stat sb;
2607 char *ondisk_path;
2609 /* Reject merges into a work tree with mixed base commits. */
2610 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2611 SHA1_DIGEST_LENGTH))
2612 return got_error(GOT_ERR_MIXED_COMMITS);
2614 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2615 == -1)
2616 return got_error_from_errno("asprintf");
2618 /* Reject merges into a work tree with conflicted files. */
2619 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2620 if (err)
2621 return err;
2622 if (status == GOT_STATUS_CONFLICT)
2623 return got_error(GOT_ERR_CONFLICTS);
2625 return NULL;
2628 static const struct got_error *
2629 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2630 const char *fileindex_path, struct got_object_id *commit_id1,
2631 struct got_object_id *commit_id2, struct got_repository *repo,
2632 got_worktree_checkout_cb progress_cb, void *progress_arg,
2633 got_cancel_cb cancel_cb, void *cancel_arg)
2635 const struct got_error *err = NULL, *sync_err;
2636 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2637 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2638 struct merge_file_cb_arg arg;
2639 char *label_orig = NULL;
2641 if (commit_id1) {
2642 char *id_str;
2644 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2645 worktree->path_prefix);
2646 if (err)
2647 goto done;
2649 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2650 if (err)
2651 goto done;
2653 err = got_object_id_str(&id_str, commit_id1);
2654 if (err)
2655 goto done;
2657 if (asprintf(&label_orig, "%s: commit %s",
2658 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2659 err = got_error_from_errno("asprintf");
2660 free(id_str);
2661 goto done;
2663 free(id_str);
2666 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2667 worktree->path_prefix);
2668 if (err)
2669 goto done;
2671 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2672 if (err)
2673 goto done;
2675 arg.worktree = worktree;
2676 arg.fileindex = fileindex;
2677 arg.progress_cb = progress_cb;
2678 arg.progress_arg = progress_arg;
2679 arg.cancel_cb = cancel_cb;
2680 arg.cancel_arg = cancel_arg;
2681 arg.label_orig = label_orig;
2682 arg.commit_id2 = commit_id2;
2683 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2684 sync_err = sync_fileindex(fileindex, fileindex_path);
2685 if (sync_err && err == NULL)
2686 err = sync_err;
2687 done:
2688 if (tree1)
2689 got_object_tree_close(tree1);
2690 if (tree2)
2691 got_object_tree_close(tree2);
2692 free(label_orig);
2693 return err;
2696 const struct got_error *
2697 got_worktree_merge_files(struct got_worktree *worktree,
2698 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2699 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2700 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2702 const struct got_error *err, *unlockerr;
2703 char *fileindex_path = NULL;
2704 struct got_fileindex *fileindex = NULL;
2705 struct check_merge_ok_arg mok_arg;
2707 err = lock_worktree(worktree, LOCK_EX);
2708 if (err)
2709 return err;
2711 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2712 if (err)
2713 goto done;
2715 mok_arg.worktree = worktree;
2716 mok_arg.repo = repo;
2717 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2718 &mok_arg);
2719 if (err)
2720 goto done;
2722 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2723 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2724 done:
2725 if (fileindex)
2726 got_fileindex_free(fileindex);
2727 free(fileindex_path);
2728 unlockerr = lock_worktree(worktree, LOCK_SH);
2729 if (unlockerr && err == NULL)
2730 err = unlockerr;
2731 return err;
2734 struct diff_dir_cb_arg {
2735 struct got_fileindex *fileindex;
2736 struct got_worktree *worktree;
2737 const char *status_path;
2738 size_t status_path_len;
2739 struct got_repository *repo;
2740 got_worktree_status_cb status_cb;
2741 void *status_arg;
2742 got_cancel_cb cancel_cb;
2743 void *cancel_arg;
2744 /* A pathlist containing per-directory pathlists of ignore patterns. */
2745 struct got_pathlist_head ignores;
2746 int report_unchanged;
2747 int no_ignores;
2750 static const struct got_error *
2751 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2752 int dirfd, const char *de_name,
2753 got_worktree_status_cb status_cb, void *status_arg,
2754 struct got_repository *repo, int report_unchanged)
2756 const struct got_error *err = NULL;
2757 unsigned char status = GOT_STATUS_NO_CHANGE;
2758 unsigned char staged_status = get_staged_status(ie);
2759 struct stat sb;
2760 struct got_object_id blob_id, commit_id, staged_blob_id;
2761 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2762 struct got_object_id *staged_blob_idp = NULL;
2764 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2765 if (err)
2766 return err;
2768 if (status == GOT_STATUS_NO_CHANGE &&
2769 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2770 return NULL;
2772 if (got_fileindex_entry_has_blob(ie)) {
2773 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2774 blob_idp = &blob_id;
2776 if (got_fileindex_entry_has_commit(ie)) {
2777 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2778 commit_idp = &commit_id;
2780 if (staged_status == GOT_STATUS_ADD ||
2781 staged_status == GOT_STATUS_MODIFY) {
2782 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2783 SHA1_DIGEST_LENGTH);
2784 staged_blob_idp = &staged_blob_id;
2787 return (*status_cb)(status_arg, status, staged_status,
2788 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2791 static const struct got_error *
2792 status_old_new(void *arg, struct got_fileindex_entry *ie,
2793 struct dirent *de, const char *parent_path, int dirfd)
2795 const struct got_error *err = NULL;
2796 struct diff_dir_cb_arg *a = arg;
2797 char *abspath;
2799 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2800 return got_error(GOT_ERR_CANCELLED);
2802 if (got_path_cmp(parent_path, a->status_path,
2803 strlen(parent_path), a->status_path_len) != 0 &&
2804 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2805 return NULL;
2807 if (parent_path[0]) {
2808 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2809 parent_path, de->d_name) == -1)
2810 return got_error_from_errno("asprintf");
2811 } else {
2812 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2813 de->d_name) == -1)
2814 return got_error_from_errno("asprintf");
2817 err = report_file_status(ie, abspath, dirfd, de->d_name,
2818 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2819 free(abspath);
2820 return err;
2823 static const struct got_error *
2824 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2826 struct diff_dir_cb_arg *a = arg;
2827 struct got_object_id blob_id, commit_id;
2828 unsigned char status;
2830 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2831 return got_error(GOT_ERR_CANCELLED);
2833 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2834 return NULL;
2836 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2837 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2838 if (got_fileindex_entry_has_file_on_disk(ie))
2839 status = GOT_STATUS_MISSING;
2840 else
2841 status = GOT_STATUS_DELETE;
2842 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2843 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2846 void
2847 free_ignorelist(struct got_pathlist_head *ignorelist)
2849 struct got_pathlist_entry *pe;
2851 TAILQ_FOREACH(pe, ignorelist, entry)
2852 free((char *)pe->path);
2853 got_pathlist_free(ignorelist);
2856 void
2857 free_ignores(struct got_pathlist_head *ignores)
2859 struct got_pathlist_entry *pe;
2861 TAILQ_FOREACH(pe, ignores, entry) {
2862 struct got_pathlist_head *ignorelist = pe->data;
2863 free_ignorelist(ignorelist);
2864 free((char *)pe->path);
2866 got_pathlist_free(ignores);
2869 static const struct got_error *
2870 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2872 const struct got_error *err = NULL;
2873 struct got_pathlist_entry *pe = NULL;
2874 struct got_pathlist_head *ignorelist;
2875 char *line = NULL, *pattern, *dirpath = NULL;
2876 size_t linesize = 0;
2877 ssize_t linelen;
2879 ignorelist = calloc(1, sizeof(*ignorelist));
2880 if (ignorelist == NULL)
2881 return got_error_from_errno("calloc");
2882 TAILQ_INIT(ignorelist);
2884 while ((linelen = getline(&line, &linesize, f)) != -1) {
2885 if (linelen > 0 && line[linelen - 1] == '\n')
2886 line[linelen - 1] = '\0';
2888 /* Git's ignores may contain comments. */
2889 if (line[0] == '#')
2890 continue;
2892 /* Git's negated patterns are not (yet?) supported. */
2893 if (line[0] == '!')
2894 continue;
2896 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2897 line) == -1) {
2898 err = got_error_from_errno("asprintf");
2899 goto done;
2901 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2902 if (err)
2903 goto done;
2905 if (ferror(f)) {
2906 err = got_error_from_errno("getline");
2907 goto done;
2910 dirpath = strdup(path);
2911 if (dirpath == NULL) {
2912 err = got_error_from_errno("strdup");
2913 goto done;
2915 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2916 done:
2917 free(line);
2918 if (err || pe == NULL) {
2919 free(dirpath);
2920 free_ignorelist(ignorelist);
2922 return err;
2925 int
2926 match_ignores(struct got_pathlist_head *ignores, const char *path)
2928 struct got_pathlist_entry *pe;
2930 /* Handle patterns which match in all directories. */
2931 TAILQ_FOREACH(pe, ignores, entry) {
2932 struct got_pathlist_head *ignorelist = pe->data;
2933 struct got_pathlist_entry *pi;
2935 TAILQ_FOREACH(pi, ignorelist, entry) {
2936 const char *p, *pattern = pi->path;
2938 if (strncmp(pattern, "**/", 3) != 0)
2939 continue;
2940 pattern += 3;
2941 p = path;
2942 while (*p) {
2943 if (fnmatch(pattern, p,
2944 FNM_PATHNAME | FNM_LEADING_DIR)) {
2945 /* Retry in next directory. */
2946 while (*p && *p != '/')
2947 p++;
2948 while (*p == '/')
2949 p++;
2950 continue;
2952 return 1;
2958 * The ignores pathlist contains ignore lists from children before
2959 * parents, so we can find the most specific ignorelist by walking
2960 * ignores backwards.
2962 pe = TAILQ_LAST(ignores, got_pathlist_head);
2963 while (pe) {
2964 if (got_path_is_child(path, pe->path, pe->path_len)) {
2965 struct got_pathlist_head *ignorelist = pe->data;
2966 struct got_pathlist_entry *pi;
2967 TAILQ_FOREACH(pi, ignorelist, entry) {
2968 const char *pattern = pi->path;
2969 int flags = FNM_LEADING_DIR;
2970 if (strstr(pattern, "/**/") == NULL)
2971 flags |= FNM_PATHNAME;
2972 if (fnmatch(pattern, path, flags))
2973 continue;
2974 return 1;
2977 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2980 return 0;
2983 static const struct got_error *
2984 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2985 const char *path, int dirfd, const char *ignores_filename)
2987 const struct got_error *err = NULL;
2988 char *ignorespath;
2989 int fd = -1;
2990 FILE *ignoresfile = NULL;
2992 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2993 path[0] ? "/" : "", ignores_filename) == -1)
2994 return got_error_from_errno("asprintf");
2996 if (dirfd != -1) {
2997 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2998 if (fd == -1) {
2999 if (errno != ENOENT && errno != EACCES)
3000 err = got_error_from_errno2("openat",
3001 ignorespath);
3002 } else {
3003 ignoresfile = fdopen(fd, "r");
3004 if (ignoresfile == NULL)
3005 err = got_error_from_errno2("fdopen",
3006 ignorespath);
3007 else {
3008 fd = -1;
3009 err = read_ignores(ignores, path, ignoresfile);
3012 } else {
3013 ignoresfile = fopen(ignorespath, "r");
3014 if (ignoresfile == NULL) {
3015 if (errno != ENOENT && errno != EACCES)
3016 err = got_error_from_errno2("fopen",
3017 ignorespath);
3018 } else
3019 err = read_ignores(ignores, path, ignoresfile);
3022 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3023 err = got_error_from_errno2("fclose", path);
3024 if (fd != -1 && close(fd) == -1 && err == NULL)
3025 err = got_error_from_errno2("close", path);
3026 free(ignorespath);
3027 return err;
3030 static const struct got_error *
3031 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3033 const struct got_error *err = NULL;
3034 struct diff_dir_cb_arg *a = arg;
3035 char *path = NULL;
3037 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3038 return got_error(GOT_ERR_CANCELLED);
3040 if (parent_path[0]) {
3041 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3042 return got_error_from_errno("asprintf");
3043 } else {
3044 path = de->d_name;
3047 if (de->d_type != DT_DIR &&
3048 got_path_is_child(path, a->status_path, a->status_path_len)
3049 && !match_ignores(&a->ignores, path))
3050 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3051 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3052 if (parent_path[0])
3053 free(path);
3054 return err;
3057 static const struct got_error *
3058 status_traverse(void *arg, const char *path, int dirfd)
3060 const struct got_error *err = NULL;
3061 struct diff_dir_cb_arg *a = arg;
3063 if (a->no_ignores)
3064 return NULL;
3066 err = add_ignores(&a->ignores, a->worktree->root_path,
3067 path, dirfd, ".cvsignore");
3068 if (err)
3069 return err;
3071 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3072 dirfd, ".gitignore");
3074 return err;
3077 static const struct got_error *
3078 report_single_file_status(const char *path, const char *ondisk_path,
3079 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3080 void *status_arg, struct got_repository *repo, int report_unchanged)
3082 struct got_fileindex_entry *ie;
3083 struct stat sb;
3085 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3086 if (ie)
3087 return report_file_status(ie, ondisk_path, -1, NULL,
3088 status_cb, status_arg, repo, report_unchanged);
3090 if (lstat(ondisk_path, &sb) == -1) {
3091 if (errno != ENOENT)
3092 return got_error_from_errno2("lstat", ondisk_path);
3093 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3094 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3095 return NULL;
3098 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3099 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3100 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3102 return NULL;
3105 static const struct got_error *
3106 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3107 const char *root_path, const char *path)
3109 const struct got_error *err;
3110 char *parent_path, *next_parent_path = NULL;
3112 err = add_ignores(ignores, root_path, "", -1,
3113 ".cvsignore");
3114 if (err)
3115 return err;
3117 err = add_ignores(ignores, root_path, "", -1,
3118 ".gitignore");
3119 if (err)
3120 return err;
3122 err = got_path_dirname(&parent_path, path);
3123 if (err) {
3124 if (err->code == GOT_ERR_BAD_PATH)
3125 return NULL; /* cannot traverse parent */
3126 return err;
3128 for (;;) {
3129 err = add_ignores(ignores, root_path, parent_path, -1,
3130 ".cvsignore");
3131 if (err)
3132 break;
3133 err = add_ignores(ignores, root_path, parent_path, -1,
3134 ".gitignore");
3135 if (err)
3136 break;
3137 err = got_path_dirname(&next_parent_path, parent_path);
3138 if (err) {
3139 if (err->code == GOT_ERR_BAD_PATH)
3140 err = NULL; /* traversed everything */
3141 break;
3143 free(parent_path);
3144 parent_path = next_parent_path;
3145 next_parent_path = NULL;
3148 free(parent_path);
3149 free(next_parent_path);
3150 return err;
3153 static const struct got_error *
3154 worktree_status(struct got_worktree *worktree, const char *path,
3155 struct got_fileindex *fileindex, struct got_repository *repo,
3156 got_worktree_status_cb status_cb, void *status_arg,
3157 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3158 int report_unchanged)
3160 const struct got_error *err = NULL;
3161 int fd = -1;
3162 struct got_fileindex_diff_dir_cb fdiff_cb;
3163 struct diff_dir_cb_arg arg;
3164 char *ondisk_path = NULL;
3166 TAILQ_INIT(&arg.ignores);
3168 if (asprintf(&ondisk_path, "%s%s%s",
3169 worktree->root_path, path[0] ? "/" : "", path) == -1)
3170 return got_error_from_errno("asprintf");
3172 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3173 if (fd == -1) {
3174 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3175 errno != ELOOP)
3176 err = got_error_from_errno2("open", ondisk_path);
3177 else
3178 err = report_single_file_status(path, ondisk_path,
3179 fileindex, status_cb, status_arg, repo,
3180 report_unchanged);
3181 } else {
3182 fdiff_cb.diff_old_new = status_old_new;
3183 fdiff_cb.diff_old = status_old;
3184 fdiff_cb.diff_new = status_new;
3185 fdiff_cb.diff_traverse = status_traverse;
3186 arg.fileindex = fileindex;
3187 arg.worktree = worktree;
3188 arg.status_path = path;
3189 arg.status_path_len = strlen(path);
3190 arg.repo = repo;
3191 arg.status_cb = status_cb;
3192 arg.status_arg = status_arg;
3193 arg.cancel_cb = cancel_cb;
3194 arg.cancel_arg = cancel_arg;
3195 arg.report_unchanged = report_unchanged;
3196 arg.no_ignores = no_ignores;
3197 if (!no_ignores) {
3198 err = add_ignores_from_parent_paths(&arg.ignores,
3199 worktree->root_path, path);
3200 if (err)
3201 goto done;
3203 err = got_fileindex_diff_dir(fileindex, fd,
3204 worktree->root_path, path, repo, &fdiff_cb, &arg);
3206 done:
3207 free_ignores(&arg.ignores);
3208 if (fd != -1 && close(fd) != 0 && err == NULL)
3209 err = got_error_from_errno("close");
3210 free(ondisk_path);
3211 return err;
3214 const struct got_error *
3215 got_worktree_status(struct got_worktree *worktree,
3216 struct got_pathlist_head *paths, struct got_repository *repo,
3217 got_worktree_status_cb status_cb, void *status_arg,
3218 got_cancel_cb cancel_cb, void *cancel_arg)
3220 const struct got_error *err = NULL;
3221 char *fileindex_path = NULL;
3222 struct got_fileindex *fileindex = NULL;
3223 struct got_pathlist_entry *pe;
3225 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3226 if (err)
3227 return err;
3229 TAILQ_FOREACH(pe, paths, entry) {
3230 err = worktree_status(worktree, pe->path, fileindex, repo,
3231 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3232 if (err)
3233 break;
3235 free(fileindex_path);
3236 got_fileindex_free(fileindex);
3237 return err;
3240 const struct got_error *
3241 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3242 const char *arg)
3244 const struct got_error *err = NULL;
3245 char *resolved = NULL, *cwd = NULL, *path = NULL;
3246 size_t len;
3247 struct stat sb;
3249 *wt_path = NULL;
3251 cwd = getcwd(NULL, 0);
3252 if (cwd == NULL)
3253 return got_error_from_errno("getcwd");
3255 if (lstat(arg, &sb) == -1) {
3256 if (errno != ENOENT) {
3257 err = got_error_from_errno2("lstat", arg);
3258 goto done;
3261 if (S_ISLNK(sb.st_mode)) {
3263 * We cannot use realpath(3) with symlinks since we want to
3264 * operate on the symlink itself.
3265 * But we can make the path absolute, assuming it is relative
3266 * to the current working directory, and then canonicalize it.
3268 char *abspath = NULL;
3269 char canonpath[PATH_MAX];
3270 if (!got_path_is_absolute(arg)) {
3271 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3272 err = got_error_from_errno("asprintf");
3273 goto done;
3277 err = got_canonpath(abspath ? abspath : arg, canonpath,
3278 sizeof(canonpath));
3279 if (err)
3280 goto done;
3281 resolved = strdup(canonpath);
3282 if (resolved == NULL) {
3283 err = got_error_from_errno("strdup");
3284 goto done;
3286 } else {
3287 resolved = realpath(arg, NULL);
3288 if (resolved == NULL) {
3289 if (errno != ENOENT) {
3290 err = got_error_from_errno2("realpath", arg);
3291 goto done;
3293 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3294 err = got_error_from_errno("asprintf");
3295 goto done;
3300 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3301 strlen(got_worktree_get_root_path(worktree)))) {
3302 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3303 goto done;
3306 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3307 err = got_path_skip_common_ancestor(&path,
3308 got_worktree_get_root_path(worktree), resolved);
3309 if (err)
3310 goto done;
3311 } else {
3312 path = strdup("");
3313 if (path == NULL) {
3314 err = got_error_from_errno("strdup");
3315 goto done;
3319 /* XXX status walk can't deal with trailing slash! */
3320 len = strlen(path);
3321 while (len > 0 && path[len - 1] == '/') {
3322 path[len - 1] = '\0';
3323 len--;
3325 done:
3326 free(resolved);
3327 free(cwd);
3328 if (err == NULL)
3329 *wt_path = path;
3330 else
3331 free(path);
3332 return err;
3335 struct schedule_addition_args {
3336 struct got_worktree *worktree;
3337 struct got_fileindex *fileindex;
3338 got_worktree_checkout_cb progress_cb;
3339 void *progress_arg;
3340 struct got_repository *repo;
3343 static const struct got_error *
3344 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3345 const char *relpath, struct got_object_id *blob_id,
3346 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3347 int dirfd, const char *de_name)
3349 struct schedule_addition_args *a = arg;
3350 const struct got_error *err = NULL;
3351 struct got_fileindex_entry *ie;
3352 struct stat sb;
3353 char *ondisk_path;
3355 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3356 relpath) == -1)
3357 return got_error_from_errno("asprintf");
3359 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3360 if (ie) {
3361 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3362 de_name, a->repo);
3363 if (err)
3364 goto done;
3365 /* Re-adding an existing entry is a no-op. */
3366 if (status == GOT_STATUS_ADD)
3367 goto done;
3368 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3369 if (err)
3370 goto done;
3373 if (status != GOT_STATUS_UNVERSIONED) {
3374 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3375 goto done;
3378 err = got_fileindex_entry_alloc(&ie, relpath);
3379 if (err)
3380 goto done;
3381 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3382 if (err) {
3383 got_fileindex_entry_free(ie);
3384 goto done;
3386 err = got_fileindex_entry_add(a->fileindex, ie);
3387 if (err) {
3388 got_fileindex_entry_free(ie);
3389 goto done;
3391 done:
3392 free(ondisk_path);
3393 if (err)
3394 return err;
3395 if (status == GOT_STATUS_ADD)
3396 return NULL;
3397 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3400 const struct got_error *
3401 got_worktree_schedule_add(struct got_worktree *worktree,
3402 struct got_pathlist_head *paths,
3403 got_worktree_checkout_cb progress_cb, void *progress_arg,
3404 struct got_repository *repo, int no_ignores)
3406 struct got_fileindex *fileindex = NULL;
3407 char *fileindex_path = NULL;
3408 const struct got_error *err = NULL, *sync_err, *unlockerr;
3409 struct got_pathlist_entry *pe;
3410 struct schedule_addition_args saa;
3412 err = lock_worktree(worktree, LOCK_EX);
3413 if (err)
3414 return err;
3416 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3417 if (err)
3418 goto done;
3420 saa.worktree = worktree;
3421 saa.fileindex = fileindex;
3422 saa.progress_cb = progress_cb;
3423 saa.progress_arg = progress_arg;
3424 saa.repo = repo;
3426 TAILQ_FOREACH(pe, paths, entry) {
3427 err = worktree_status(worktree, pe->path, fileindex, repo,
3428 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3429 if (err)
3430 break;
3432 sync_err = sync_fileindex(fileindex, fileindex_path);
3433 if (sync_err && err == NULL)
3434 err = sync_err;
3435 done:
3436 free(fileindex_path);
3437 if (fileindex)
3438 got_fileindex_free(fileindex);
3439 unlockerr = lock_worktree(worktree, LOCK_SH);
3440 if (unlockerr && err == NULL)
3441 err = unlockerr;
3442 return err;
3445 struct schedule_deletion_args {
3446 struct got_worktree *worktree;
3447 struct got_fileindex *fileindex;
3448 got_worktree_delete_cb progress_cb;
3449 void *progress_arg;
3450 struct got_repository *repo;
3451 int delete_local_mods;
3452 int keep_on_disk;
3455 static const struct got_error *
3456 schedule_for_deletion(void *arg, unsigned char status,
3457 unsigned char staged_status, const char *relpath,
3458 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3459 struct got_object_id *commit_id, int dirfd, const char *de_name)
3461 struct schedule_deletion_args *a = arg;
3462 const struct got_error *err = NULL;
3463 struct got_fileindex_entry *ie = NULL;
3464 struct stat sb;
3465 char *ondisk_path, *parent = NULL;
3467 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3468 if (ie == NULL)
3469 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3471 staged_status = get_staged_status(ie);
3472 if (staged_status != GOT_STATUS_NO_CHANGE) {
3473 if (staged_status == GOT_STATUS_DELETE)
3474 return NULL;
3475 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3478 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3479 relpath) == -1)
3480 return got_error_from_errno("asprintf");
3482 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3483 a->repo);
3484 if (err)
3485 goto done;
3487 if (status != GOT_STATUS_NO_CHANGE) {
3488 if (status == GOT_STATUS_DELETE)
3489 goto done;
3490 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3491 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3492 goto done;
3494 if (status != GOT_STATUS_MODIFY &&
3495 status != GOT_STATUS_MISSING) {
3496 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3497 goto done;
3501 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3502 if (dirfd != -1) {
3503 if (unlinkat(dirfd, de_name, 0) != 0) {
3504 err = got_error_from_errno2("unlinkat",
3505 ondisk_path);
3506 goto done;
3508 } else if (unlink(ondisk_path) != 0) {
3509 err = got_error_from_errno2("unlink", ondisk_path);
3510 goto done;
3513 parent = dirname(ondisk_path);
3515 if (parent == NULL) {
3516 err = got_error_from_errno2("dirname", ondisk_path);
3517 goto done;
3519 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3520 if (rmdir(parent) == -1) {
3521 if (errno != ENOTEMPTY)
3522 err = got_error_from_errno2("rmdir",
3523 parent);
3524 break;
3526 parent = dirname(parent);
3527 if (parent == NULL) {
3528 err = got_error_from_errno2("dirname", parent);
3529 goto done;
3534 got_fileindex_entry_mark_deleted_from_disk(ie);
3535 done:
3536 free(ondisk_path);
3537 if (err)
3538 return err;
3539 if (status == GOT_STATUS_DELETE)
3540 return NULL;
3541 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3542 staged_status, relpath);
3545 const struct got_error *
3546 got_worktree_schedule_delete(struct got_worktree *worktree,
3547 struct got_pathlist_head *paths, int delete_local_mods,
3548 got_worktree_delete_cb progress_cb, void *progress_arg,
3549 struct got_repository *repo, int keep_on_disk)
3551 struct got_fileindex *fileindex = NULL;
3552 char *fileindex_path = NULL;
3553 const struct got_error *err = NULL, *sync_err, *unlockerr;
3554 struct got_pathlist_entry *pe;
3555 struct schedule_deletion_args sda;
3557 err = lock_worktree(worktree, LOCK_EX);
3558 if (err)
3559 return err;
3561 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3562 if (err)
3563 goto done;
3565 sda.worktree = worktree;
3566 sda.fileindex = fileindex;
3567 sda.progress_cb = progress_cb;
3568 sda.progress_arg = progress_arg;
3569 sda.repo = repo;
3570 sda.delete_local_mods = delete_local_mods;
3571 sda.keep_on_disk = keep_on_disk;
3573 TAILQ_FOREACH(pe, paths, entry) {
3574 err = worktree_status(worktree, pe->path, fileindex, repo,
3575 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3576 if (err)
3577 break;
3579 sync_err = sync_fileindex(fileindex, fileindex_path);
3580 if (sync_err && err == NULL)
3581 err = sync_err;
3582 done:
3583 free(fileindex_path);
3584 if (fileindex)
3585 got_fileindex_free(fileindex);
3586 unlockerr = lock_worktree(worktree, LOCK_SH);
3587 if (unlockerr && err == NULL)
3588 err = unlockerr;
3589 return err;
3592 static const struct got_error *
3593 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3595 const struct got_error *err = NULL;
3596 char *line = NULL;
3597 size_t linesize = 0, n;
3598 ssize_t linelen;
3600 linelen = getline(&line, &linesize, infile);
3601 if (linelen == -1) {
3602 if (ferror(infile)) {
3603 err = got_error_from_errno("getline");
3604 goto done;
3606 return NULL;
3608 if (outfile) {
3609 n = fwrite(line, 1, linelen, outfile);
3610 if (n != linelen) {
3611 err = got_ferror(outfile, GOT_ERR_IO);
3612 goto done;
3615 if (rejectfile) {
3616 n = fwrite(line, 1, linelen, rejectfile);
3617 if (n != linelen)
3618 err = got_ferror(outfile, GOT_ERR_IO);
3620 done:
3621 free(line);
3622 return err;
3625 static const struct got_error *
3626 skip_one_line(FILE *f)
3628 char *line = NULL;
3629 size_t linesize = 0;
3630 ssize_t linelen;
3632 linelen = getline(&line, &linesize, f);
3633 if (linelen == -1) {
3634 if (ferror(f))
3635 return got_error_from_errno("getline");
3636 return NULL;
3638 free(line);
3639 return NULL;
3642 static const struct got_error *
3643 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3644 int start_old, int end_old, int start_new, int end_new,
3645 FILE *outfile, FILE *rejectfile)
3647 const struct got_error *err;
3649 /* Copy old file's lines leading up to patch. */
3650 while (!feof(f1) && *line_cur1 < start_old) {
3651 err = copy_one_line(f1, outfile, NULL);
3652 if (err)
3653 return err;
3654 (*line_cur1)++;
3656 /* Skip new file's lines leading up to patch. */
3657 while (!feof(f2) && *line_cur2 < start_new) {
3658 if (rejectfile)
3659 err = copy_one_line(f2, NULL, rejectfile);
3660 else
3661 err = skip_one_line(f2);
3662 if (err)
3663 return err;
3664 (*line_cur2)++;
3666 /* Copy patched lines. */
3667 while (!feof(f2) && *line_cur2 <= end_new) {
3668 err = copy_one_line(f2, outfile, NULL);
3669 if (err)
3670 return err;
3671 (*line_cur2)++;
3673 /* Skip over old file's replaced lines. */
3674 while (!feof(f1) && *line_cur1 <= end_old) {
3675 if (rejectfile)
3676 err = copy_one_line(f1, NULL, rejectfile);
3677 else
3678 err = skip_one_line(f1);
3679 if (err)
3680 return err;
3681 (*line_cur1)++;
3684 return NULL;
3687 static const struct got_error *
3688 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3689 FILE *outfile, FILE *rejectfile)
3691 const struct got_error *err;
3693 if (outfile) {
3694 /* Copy old file's lines until EOF. */
3695 while (!feof(f1)) {
3696 err = copy_one_line(f1, outfile, NULL);
3697 if (err)
3698 return err;
3699 (*line_cur1)++;
3702 if (rejectfile) {
3703 /* Copy new file's lines until EOF. */
3704 while (!feof(f2)) {
3705 err = copy_one_line(f2, NULL, rejectfile);
3706 if (err)
3707 return err;
3708 (*line_cur2)++;
3712 return NULL;
3715 static const struct got_error *
3716 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3717 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3718 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3719 int *line_cur2, FILE *outfile, FILE *rejectfile,
3720 got_worktree_patch_cb patch_cb, void *patch_arg)
3722 const struct got_error *err = NULL;
3723 int start_old = change->cv.a;
3724 int end_old = change->cv.b;
3725 int start_new = change->cv.c;
3726 int end_new = change->cv.d;
3727 long pos1, pos2;
3728 FILE *hunkfile;
3730 *choice = GOT_PATCH_CHOICE_NONE;
3732 hunkfile = got_opentemp();
3733 if (hunkfile == NULL)
3734 return got_error_from_errno("got_opentemp");
3736 pos1 = ftell(f1);
3737 pos2 = ftell(f2);
3739 /* XXX TODO needs error checking */
3740 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3742 if (fseek(f1, pos1, SEEK_SET) == -1) {
3743 err = got_ferror(f1, GOT_ERR_IO);
3744 goto done;
3746 if (fseek(f2, pos2, SEEK_SET) == -1) {
3747 err = got_ferror(f1, GOT_ERR_IO);
3748 goto done;
3750 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3751 err = got_ferror(hunkfile, GOT_ERR_IO);
3752 goto done;
3755 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3756 hunkfile, n, nchanges);
3757 if (err)
3758 goto done;
3760 switch (*choice) {
3761 case GOT_PATCH_CHOICE_YES:
3762 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3763 end_old, start_new, end_new, outfile, rejectfile);
3764 break;
3765 case GOT_PATCH_CHOICE_NO:
3766 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3767 end_old, start_new, end_new, rejectfile, outfile);
3768 break;
3769 case GOT_PATCH_CHOICE_QUIT:
3770 break;
3771 default:
3772 err = got_error(GOT_ERR_PATCH_CHOICE);
3773 break;
3775 done:
3776 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3777 err = got_error_from_errno("fclose");
3778 return err;
3781 struct revert_file_args {
3782 struct got_worktree *worktree;
3783 struct got_fileindex *fileindex;
3784 got_worktree_checkout_cb progress_cb;
3785 void *progress_arg;
3786 got_worktree_patch_cb patch_cb;
3787 void *patch_arg;
3788 struct got_repository *repo;
3791 static const struct got_error *
3792 create_patched_content(char **path_outfile, int reverse_patch,
3793 struct got_object_id *blob_id, const char *path2,
3794 int dirfd2, const char *de_name2,
3795 const char *relpath, struct got_repository *repo,
3796 got_worktree_patch_cb patch_cb, void *patch_arg)
3798 const struct got_error *err;
3799 struct got_blob_object *blob = NULL;
3800 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3801 int fd2 = -1;
3802 char *path1 = NULL, *id_str = NULL;
3803 struct stat sb1, sb2;
3804 struct got_diff_changes *changes = NULL;
3805 struct got_diff_state *ds = NULL;
3806 struct got_diff_args *args = NULL;
3807 struct got_diff_change *change;
3808 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3809 int n = 0;
3811 *path_outfile = NULL;
3813 err = got_object_id_str(&id_str, blob_id);
3814 if (err)
3815 return err;
3817 if (dirfd2 != -1) {
3818 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3819 if (fd2 == -1) {
3820 err = got_error_from_errno2("openat", path2);
3821 goto done;
3823 } else {
3824 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3825 if (fd2 == -1) {
3826 err = got_error_from_errno2("open", path2);
3827 goto done;
3830 if (fstat(fd2, &sb2) == -1) {
3831 err = got_error_from_errno2("fstat", path2);
3832 goto done;
3835 f2 = fdopen(fd2, "r");
3836 if (f2 == NULL) {
3837 err = got_error_from_errno2("fdopen", path2);
3838 goto done;
3840 fd2 = -1;
3842 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3843 if (err)
3844 goto done;
3846 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3847 if (err)
3848 goto done;
3850 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3851 if (err)
3852 goto done;
3854 if (stat(path1, &sb1) == -1) {
3855 err = got_error_from_errno2("stat", path1);
3856 goto done;
3859 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3860 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3861 if (err)
3862 goto done;
3864 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3865 if (err)
3866 goto done;
3868 if (fseek(f1, 0L, SEEK_SET) == -1)
3869 return got_ferror(f1, GOT_ERR_IO);
3870 if (fseek(f2, 0L, SEEK_SET) == -1)
3871 return got_ferror(f2, GOT_ERR_IO);
3872 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3873 int choice;
3874 err = apply_or_reject_change(&choice, change, ++n,
3875 changes->nchanges, ds, args, diff_flags, relpath,
3876 f1, f2, &line_cur1, &line_cur2,
3877 reverse_patch ? NULL : outfile,
3878 reverse_patch ? outfile : NULL,
3879 patch_cb, patch_arg);
3880 if (err)
3881 goto done;
3882 if (choice == GOT_PATCH_CHOICE_YES)
3883 have_content = 1;
3884 else if (choice == GOT_PATCH_CHOICE_QUIT)
3885 break;
3887 if (have_content) {
3888 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3889 reverse_patch ? NULL : outfile,
3890 reverse_patch ? outfile : NULL);
3891 if (err)
3892 goto done;
3894 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3895 err = got_error_from_errno2("chmod", path2);
3896 goto done;
3899 done:
3900 free(id_str);
3901 if (blob)
3902 got_object_blob_close(blob);
3903 if (f1 && fclose(f1) == EOF && err == NULL)
3904 err = got_error_from_errno2("fclose", path1);
3905 if (f2 && fclose(f2) == EOF && err == NULL)
3906 err = got_error_from_errno2("fclose", path2);
3907 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3908 err = got_error_from_errno2("close", path2);
3909 if (outfile && fclose(outfile) == EOF && err == NULL)
3910 err = got_error_from_errno2("fclose", *path_outfile);
3911 if (path1 && unlink(path1) == -1 && err == NULL)
3912 err = got_error_from_errno2("unlink", path1);
3913 if (err || !have_content) {
3914 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3915 err = got_error_from_errno2("unlink", *path_outfile);
3916 free(*path_outfile);
3917 *path_outfile = NULL;
3919 free(args);
3920 if (ds) {
3921 got_diff_state_free(ds);
3922 free(ds);
3924 if (changes)
3925 got_diff_free_changes(changes);
3926 free(path1);
3927 return err;
3930 static const struct got_error *
3931 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3932 const char *relpath, struct got_object_id *blob_id,
3933 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3934 int dirfd, const char *de_name)
3936 struct revert_file_args *a = arg;
3937 const struct got_error *err = NULL;
3938 char *parent_path = NULL;
3939 struct got_fileindex_entry *ie;
3940 struct got_tree_object *tree = NULL;
3941 struct got_object_id *tree_id = NULL;
3942 const struct got_tree_entry *te = NULL;
3943 char *tree_path = NULL, *te_name;
3944 char *ondisk_path = NULL, *path_content = NULL;
3945 struct got_blob_object *blob = NULL;
3947 /* Reverting a staged deletion is a no-op. */
3948 if (status == GOT_STATUS_DELETE &&
3949 staged_status != GOT_STATUS_NO_CHANGE)
3950 return NULL;
3952 if (status == GOT_STATUS_UNVERSIONED)
3953 return (*a->progress_cb)(a->progress_arg,
3954 GOT_STATUS_UNVERSIONED, relpath);
3956 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3957 if (ie == NULL)
3958 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3960 /* Construct in-repository path of tree which contains this blob. */
3961 err = got_path_dirname(&parent_path, ie->path);
3962 if (err) {
3963 if (err->code != GOT_ERR_BAD_PATH)
3964 goto done;
3965 parent_path = strdup("/");
3966 if (parent_path == NULL) {
3967 err = got_error_from_errno("strdup");
3968 goto done;
3971 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3972 tree_path = strdup(parent_path);
3973 if (tree_path == NULL) {
3974 err = got_error_from_errno("strdup");
3975 goto done;
3977 } else {
3978 if (got_path_is_root_dir(parent_path)) {
3979 tree_path = strdup(a->worktree->path_prefix);
3980 if (tree_path == NULL) {
3981 err = got_error_from_errno("strdup");
3982 goto done;
3984 } else {
3985 if (asprintf(&tree_path, "%s/%s",
3986 a->worktree->path_prefix, parent_path) == -1) {
3987 err = got_error_from_errno("asprintf");
3988 goto done;
3993 err = got_object_id_by_path(&tree_id, a->repo,
3994 a->worktree->base_commit_id, tree_path);
3995 if (err) {
3996 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3997 (status == GOT_STATUS_ADD ||
3998 staged_status == GOT_STATUS_ADD)))
3999 goto done;
4000 } else {
4001 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4002 if (err)
4003 goto done;
4005 te_name = basename(ie->path);
4006 if (te_name == NULL) {
4007 err = got_error_from_errno2("basename", ie->path);
4008 goto done;
4011 te = got_object_tree_find_entry(tree, te_name);
4012 if (te == NULL && status != GOT_STATUS_ADD &&
4013 staged_status != GOT_STATUS_ADD) {
4014 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4015 goto done;
4019 switch (status) {
4020 case GOT_STATUS_ADD:
4021 if (a->patch_cb) {
4022 int choice = GOT_PATCH_CHOICE_NONE;
4023 err = (*a->patch_cb)(&choice, a->patch_arg,
4024 status, ie->path, NULL, 1, 1);
4025 if (err)
4026 goto done;
4027 if (choice != GOT_PATCH_CHOICE_YES)
4028 break;
4030 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4031 ie->path);
4032 if (err)
4033 goto done;
4034 got_fileindex_entry_remove(a->fileindex, ie);
4035 break;
4036 case GOT_STATUS_DELETE:
4037 if (a->patch_cb) {
4038 int choice = GOT_PATCH_CHOICE_NONE;
4039 err = (*a->patch_cb)(&choice, a->patch_arg,
4040 status, ie->path, NULL, 1, 1);
4041 if (err)
4042 goto done;
4043 if (choice != GOT_PATCH_CHOICE_YES)
4044 break;
4046 /* fall through */
4047 case GOT_STATUS_MODIFY:
4048 case GOT_STATUS_MODE_CHANGE:
4049 case GOT_STATUS_CONFLICT:
4050 case GOT_STATUS_MISSING: {
4051 struct got_object_id id;
4052 if (staged_status == GOT_STATUS_ADD ||
4053 staged_status == GOT_STATUS_MODIFY) {
4054 memcpy(id.sha1, ie->staged_blob_sha1,
4055 SHA1_DIGEST_LENGTH);
4056 } else
4057 memcpy(id.sha1, ie->blob_sha1,
4058 SHA1_DIGEST_LENGTH);
4059 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4060 if (err)
4061 goto done;
4063 if (asprintf(&ondisk_path, "%s/%s",
4064 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4065 err = got_error_from_errno("asprintf");
4066 goto done;
4069 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4070 status == GOT_STATUS_CONFLICT)) {
4071 err = create_patched_content(&path_content, 1, &id,
4072 ondisk_path, dirfd, de_name, ie->path, a->repo,
4073 a->patch_cb, a->patch_arg);
4074 if (err || path_content == NULL)
4075 break;
4076 if (rename(path_content, ondisk_path) == -1) {
4077 err = got_error_from_errno3("rename",
4078 path_content, ondisk_path);
4079 goto done;
4081 } else {
4082 err = install_blob(a->worktree, ondisk_path, ie->path,
4083 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4084 got_fileindex_perms_to_st(ie), blob, 0, 1, 0,
4085 a->repo, a->progress_cb, a->progress_arg);
4086 if (err)
4087 goto done;
4088 if (status == GOT_STATUS_DELETE ||
4089 status == GOT_STATUS_MODE_CHANGE) {
4090 err = got_fileindex_entry_update(ie,
4091 ondisk_path, blob->id.sha1,
4092 a->worktree->base_commit_id->sha1, 1);
4093 if (err)
4094 goto done;
4097 break;
4099 default:
4100 break;
4102 done:
4103 free(ondisk_path);
4104 free(path_content);
4105 free(parent_path);
4106 free(tree_path);
4107 if (blob)
4108 got_object_blob_close(blob);
4109 if (tree)
4110 got_object_tree_close(tree);
4111 free(tree_id);
4112 return err;
4115 const struct got_error *
4116 got_worktree_revert(struct got_worktree *worktree,
4117 struct got_pathlist_head *paths,
4118 got_worktree_checkout_cb progress_cb, void *progress_arg,
4119 got_worktree_patch_cb patch_cb, void *patch_arg,
4120 struct got_repository *repo)
4122 struct got_fileindex *fileindex = NULL;
4123 char *fileindex_path = NULL;
4124 const struct got_error *err = NULL, *unlockerr = NULL;
4125 const struct got_error *sync_err = NULL;
4126 struct got_pathlist_entry *pe;
4127 struct revert_file_args rfa;
4129 err = lock_worktree(worktree, LOCK_EX);
4130 if (err)
4131 return err;
4133 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4134 if (err)
4135 goto done;
4137 rfa.worktree = worktree;
4138 rfa.fileindex = fileindex;
4139 rfa.progress_cb = progress_cb;
4140 rfa.progress_arg = progress_arg;
4141 rfa.patch_cb = patch_cb;
4142 rfa.patch_arg = patch_arg;
4143 rfa.repo = repo;
4144 TAILQ_FOREACH(pe, paths, entry) {
4145 err = worktree_status(worktree, pe->path, fileindex, repo,
4146 revert_file, &rfa, NULL, NULL, 0, 0);
4147 if (err)
4148 break;
4150 sync_err = sync_fileindex(fileindex, fileindex_path);
4151 if (sync_err && err == NULL)
4152 err = sync_err;
4153 done:
4154 free(fileindex_path);
4155 if (fileindex)
4156 got_fileindex_free(fileindex);
4157 unlockerr = lock_worktree(worktree, LOCK_SH);
4158 if (unlockerr && err == NULL)
4159 err = unlockerr;
4160 return err;
4163 static void
4164 free_commitable(struct got_commitable *ct)
4166 free(ct->path);
4167 free(ct->in_repo_path);
4168 free(ct->ondisk_path);
4169 free(ct->blob_id);
4170 free(ct->base_blob_id);
4171 free(ct->staged_blob_id);
4172 free(ct->base_commit_id);
4173 free(ct);
4176 struct collect_commitables_arg {
4177 struct got_pathlist_head *commitable_paths;
4178 struct got_repository *repo;
4179 struct got_worktree *worktree;
4180 int have_staged_files;
4183 static const struct got_error *
4184 collect_commitables(void *arg, unsigned char status,
4185 unsigned char staged_status, const char *relpath,
4186 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4187 struct got_object_id *commit_id, int dirfd, const char *de_name)
4189 struct collect_commitables_arg *a = arg;
4190 const struct got_error *err = NULL;
4191 struct got_commitable *ct = NULL;
4192 struct got_pathlist_entry *new = NULL;
4193 char *parent_path = NULL, *path = NULL;
4194 struct stat sb;
4196 if (a->have_staged_files) {
4197 if (staged_status != GOT_STATUS_MODIFY &&
4198 staged_status != GOT_STATUS_ADD &&
4199 staged_status != GOT_STATUS_DELETE)
4200 return NULL;
4201 } else {
4202 if (status == GOT_STATUS_CONFLICT)
4203 return got_error(GOT_ERR_COMMIT_CONFLICT);
4205 if (status != GOT_STATUS_MODIFY &&
4206 status != GOT_STATUS_MODE_CHANGE &&
4207 status != GOT_STATUS_ADD &&
4208 status != GOT_STATUS_DELETE)
4209 return NULL;
4212 if (asprintf(&path, "/%s", relpath) == -1) {
4213 err = got_error_from_errno("asprintf");
4214 goto done;
4216 if (strcmp(path, "/") == 0) {
4217 parent_path = strdup("");
4218 if (parent_path == NULL)
4219 return got_error_from_errno("strdup");
4220 } else {
4221 err = got_path_dirname(&parent_path, path);
4222 if (err)
4223 return err;
4226 ct = calloc(1, sizeof(*ct));
4227 if (ct == NULL) {
4228 err = got_error_from_errno("calloc");
4229 goto done;
4232 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4233 relpath) == -1) {
4234 err = got_error_from_errno("asprintf");
4235 goto done;
4237 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4238 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4239 } else {
4240 if (dirfd != -1) {
4241 if (fstatat(dirfd, de_name, &sb,
4242 AT_SYMLINK_NOFOLLOW) == -1) {
4243 err = got_error_from_errno2("fstatat",
4244 ct->ondisk_path);
4245 goto done;
4247 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4248 err = got_error_from_errno2("lstat", ct->ondisk_path);
4249 goto done;
4251 ct->mode = sb.st_mode;
4254 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4255 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4256 relpath) == -1) {
4257 err = got_error_from_errno("asprintf");
4258 goto done;
4261 ct->status = status;
4262 ct->staged_status = staged_status;
4263 ct->blob_id = NULL; /* will be filled in when blob gets created */
4264 if (ct->status != GOT_STATUS_ADD &&
4265 ct->staged_status != GOT_STATUS_ADD) {
4266 ct->base_blob_id = got_object_id_dup(blob_id);
4267 if (ct->base_blob_id == NULL) {
4268 err = got_error_from_errno("got_object_id_dup");
4269 goto done;
4271 ct->base_commit_id = got_object_id_dup(commit_id);
4272 if (ct->base_commit_id == NULL) {
4273 err = got_error_from_errno("got_object_id_dup");
4274 goto done;
4277 if (ct->staged_status == GOT_STATUS_ADD ||
4278 ct->staged_status == GOT_STATUS_MODIFY) {
4279 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4280 if (ct->staged_blob_id == NULL) {
4281 err = got_error_from_errno("got_object_id_dup");
4282 goto done;
4285 ct->path = strdup(path);
4286 if (ct->path == NULL) {
4287 err = got_error_from_errno("strdup");
4288 goto done;
4290 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4291 done:
4292 if (ct && (err || new == NULL))
4293 free_commitable(ct);
4294 free(parent_path);
4295 free(path);
4296 return err;
4299 static const struct got_error *write_tree(struct got_object_id **, int *,
4300 struct got_tree_object *, const char *, struct got_pathlist_head *,
4301 got_worktree_status_cb status_cb, void *status_arg,
4302 struct got_repository *);
4304 static const struct got_error *
4305 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4306 struct got_tree_entry *te, const char *parent_path,
4307 struct got_pathlist_head *commitable_paths,
4308 got_worktree_status_cb status_cb, void *status_arg,
4309 struct got_repository *repo)
4311 const struct got_error *err = NULL;
4312 struct got_tree_object *subtree;
4313 char *subpath;
4315 if (asprintf(&subpath, "%s%s%s", parent_path,
4316 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4317 return got_error_from_errno("asprintf");
4319 err = got_object_open_as_tree(&subtree, repo, &te->id);
4320 if (err)
4321 return err;
4323 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4324 commitable_paths, status_cb, status_arg, repo);
4325 got_object_tree_close(subtree);
4326 free(subpath);
4327 return err;
4330 static const struct got_error *
4331 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4333 const struct got_error *err = NULL;
4334 char *ct_parent_path = NULL;
4336 *match = 0;
4338 if (strchr(ct->in_repo_path, '/') == NULL) {
4339 *match = got_path_is_root_dir(path);
4340 return NULL;
4343 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4344 if (err)
4345 return err;
4346 *match = (strcmp(path, ct_parent_path) == 0);
4347 free(ct_parent_path);
4348 return err;
4351 static mode_t
4352 get_ct_file_mode(struct got_commitable *ct)
4354 if (S_ISLNK(ct->mode))
4355 return S_IFLNK;
4357 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4360 static const struct got_error *
4361 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4362 struct got_tree_entry *te, struct got_commitable *ct)
4364 const struct got_error *err = NULL;
4366 *new_te = NULL;
4368 err = got_object_tree_entry_dup(new_te, te);
4369 if (err)
4370 goto done;
4372 (*new_te)->mode = get_ct_file_mode(ct);
4374 if (ct->staged_status == GOT_STATUS_MODIFY)
4375 memcpy(&(*new_te)->id, ct->staged_blob_id,
4376 sizeof((*new_te)->id));
4377 else
4378 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4379 done:
4380 if (err && *new_te) {
4381 free(*new_te);
4382 *new_te = NULL;
4384 return err;
4387 static const struct got_error *
4388 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4389 struct got_commitable *ct)
4391 const struct got_error *err = NULL;
4392 char *ct_name;
4394 *new_te = NULL;
4396 *new_te = calloc(1, sizeof(**new_te));
4397 if (*new_te == NULL)
4398 return got_error_from_errno("calloc");
4400 ct_name = basename(ct->path);
4401 if (ct_name == NULL) {
4402 err = got_error_from_errno2("basename", ct->path);
4403 goto done;
4405 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4406 sizeof((*new_te)->name)) {
4407 err = got_error(GOT_ERR_NO_SPACE);
4408 goto done;
4411 (*new_te)->mode = get_ct_file_mode(ct);
4413 if (ct->staged_status == GOT_STATUS_ADD)
4414 memcpy(&(*new_te)->id, ct->staged_blob_id,
4415 sizeof((*new_te)->id));
4416 else
4417 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4418 done:
4419 if (err && *new_te) {
4420 free(*new_te);
4421 *new_te = NULL;
4423 return err;
4426 static const struct got_error *
4427 insert_tree_entry(struct got_tree_entry *new_te,
4428 struct got_pathlist_head *paths)
4430 const struct got_error *err = NULL;
4431 struct got_pathlist_entry *new_pe;
4433 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4434 if (err)
4435 return err;
4436 if (new_pe == NULL)
4437 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4438 return NULL;
4441 static const struct got_error *
4442 report_ct_status(struct got_commitable *ct,
4443 got_worktree_status_cb status_cb, void *status_arg)
4445 const char *ct_path = ct->path;
4446 unsigned char status;
4448 while (ct_path[0] == '/')
4449 ct_path++;
4451 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4452 status = ct->staged_status;
4453 else
4454 status = ct->status;
4456 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4457 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4460 static const struct got_error *
4461 match_modified_subtree(int *modified, struct got_tree_entry *te,
4462 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4464 const struct got_error *err = NULL;
4465 struct got_pathlist_entry *pe;
4466 char *te_path;
4468 *modified = 0;
4470 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4471 got_path_is_root_dir(base_tree_path) ? "" : "/",
4472 te->name) == -1)
4473 return got_error_from_errno("asprintf");
4475 TAILQ_FOREACH(pe, commitable_paths, entry) {
4476 struct got_commitable *ct = pe->data;
4477 *modified = got_path_is_child(ct->in_repo_path, te_path,
4478 strlen(te_path));
4479 if (*modified)
4480 break;
4483 free(te_path);
4484 return err;
4487 static const struct got_error *
4488 match_deleted_or_modified_ct(struct got_commitable **ctp,
4489 struct got_tree_entry *te, const char *base_tree_path,
4490 struct got_pathlist_head *commitable_paths)
4492 const struct got_error *err = NULL;
4493 struct got_pathlist_entry *pe;
4495 *ctp = NULL;
4497 TAILQ_FOREACH(pe, commitable_paths, entry) {
4498 struct got_commitable *ct = pe->data;
4499 char *ct_name = NULL;
4500 int path_matches;
4502 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4503 if (ct->status != GOT_STATUS_MODIFY &&
4504 ct->status != GOT_STATUS_MODE_CHANGE &&
4505 ct->status != GOT_STATUS_DELETE)
4506 continue;
4507 } else {
4508 if (ct->staged_status != GOT_STATUS_MODIFY &&
4509 ct->staged_status != GOT_STATUS_DELETE)
4510 continue;
4513 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4514 continue;
4516 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4517 if (err)
4518 return err;
4519 if (!path_matches)
4520 continue;
4522 ct_name = basename(pe->path);
4523 if (ct_name == NULL)
4524 return got_error_from_errno2("basename", pe->path);
4526 if (strcmp(te->name, ct_name) != 0)
4527 continue;
4529 *ctp = ct;
4530 break;
4533 return err;
4536 static const struct got_error *
4537 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4538 const char *child_path, const char *path_base_tree,
4539 struct got_pathlist_head *commitable_paths,
4540 got_worktree_status_cb status_cb, void *status_arg,
4541 struct got_repository *repo)
4543 const struct got_error *err = NULL;
4544 struct got_tree_entry *new_te;
4545 char *subtree_path;
4546 struct got_object_id *id = NULL;
4547 int nentries;
4549 *new_tep = NULL;
4551 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4552 got_path_is_root_dir(path_base_tree) ? "" : "/",
4553 child_path) == -1)
4554 return got_error_from_errno("asprintf");
4556 new_te = calloc(1, sizeof(*new_te));
4557 if (new_te == NULL)
4558 return got_error_from_errno("calloc");
4559 new_te->mode = S_IFDIR;
4561 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4562 sizeof(new_te->name)) {
4563 err = got_error(GOT_ERR_NO_SPACE);
4564 goto done;
4566 err = write_tree(&id, &nentries, NULL, subtree_path,
4567 commitable_paths, status_cb, status_arg, repo);
4568 if (err) {
4569 free(new_te);
4570 goto done;
4572 memcpy(&new_te->id, id, sizeof(new_te->id));
4573 done:
4574 free(id);
4575 free(subtree_path);
4576 if (err == NULL)
4577 *new_tep = new_te;
4578 return err;
4581 static const struct got_error *
4582 write_tree(struct got_object_id **new_tree_id, int *nentries,
4583 struct got_tree_object *base_tree, const char *path_base_tree,
4584 struct got_pathlist_head *commitable_paths,
4585 got_worktree_status_cb status_cb, void *status_arg,
4586 struct got_repository *repo)
4588 const struct got_error *err = NULL;
4589 struct got_pathlist_head paths;
4590 struct got_tree_entry *te, *new_te = NULL;
4591 struct got_pathlist_entry *pe;
4593 TAILQ_INIT(&paths);
4594 *nentries = 0;
4596 /* Insert, and recurse into, newly added entries first. */
4597 TAILQ_FOREACH(pe, commitable_paths, entry) {
4598 struct got_commitable *ct = pe->data;
4599 char *child_path = NULL, *slash;
4601 if ((ct->status != GOT_STATUS_ADD &&
4602 ct->staged_status != GOT_STATUS_ADD) ||
4603 (ct->flags & GOT_COMMITABLE_ADDED))
4604 continue;
4606 if (!got_path_is_child(pe->path, path_base_tree,
4607 strlen(path_base_tree)))
4608 continue;
4610 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4611 pe->path);
4612 if (err)
4613 goto done;
4615 slash = strchr(child_path, '/');
4616 if (slash == NULL) {
4617 err = alloc_added_blob_tree_entry(&new_te, ct);
4618 if (err)
4619 goto done;
4620 err = report_ct_status(ct, status_cb, status_arg);
4621 if (err)
4622 goto done;
4623 ct->flags |= GOT_COMMITABLE_ADDED;
4624 err = insert_tree_entry(new_te, &paths);
4625 if (err)
4626 goto done;
4627 (*nentries)++;
4628 } else {
4629 *slash = '\0'; /* trim trailing path components */
4630 if (base_tree == NULL ||
4631 got_object_tree_find_entry(base_tree, child_path)
4632 == NULL) {
4633 err = make_subtree_for_added_blob(&new_te,
4634 child_path, path_base_tree,
4635 commitable_paths, status_cb, status_arg,
4636 repo);
4637 if (err)
4638 goto done;
4639 err = insert_tree_entry(new_te, &paths);
4640 if (err)
4641 goto done;
4642 (*nentries)++;
4647 if (base_tree) {
4648 int i, nbase_entries;
4649 /* Handle modified and deleted entries. */
4650 nbase_entries = got_object_tree_get_nentries(base_tree);
4651 for (i = 0; i < nbase_entries; i++) {
4652 struct got_commitable *ct = NULL;
4654 te = got_object_tree_get_entry(base_tree, i);
4655 if (got_object_tree_entry_is_submodule(te)) {
4656 /* Entry is a submodule; just copy it. */
4657 err = got_object_tree_entry_dup(&new_te, te);
4658 if (err)
4659 goto done;
4660 err = insert_tree_entry(new_te, &paths);
4661 if (err)
4662 goto done;
4663 (*nentries)++;
4664 continue;
4667 if (S_ISDIR(te->mode)) {
4668 int modified;
4669 err = got_object_tree_entry_dup(&new_te, te);
4670 if (err)
4671 goto done;
4672 err = match_modified_subtree(&modified, te,
4673 path_base_tree, commitable_paths);
4674 if (err)
4675 goto done;
4676 /* Avoid recursion into unmodified subtrees. */
4677 if (modified) {
4678 struct got_object_id *new_id;
4679 int nsubentries;
4680 err = write_subtree(&new_id,
4681 &nsubentries, te,
4682 path_base_tree, commitable_paths,
4683 status_cb, status_arg, repo);
4684 if (err)
4685 goto done;
4686 if (nsubentries == 0) {
4687 /* All entries were deleted. */
4688 free(new_id);
4689 continue;
4691 memcpy(&new_te->id, new_id,
4692 sizeof(new_te->id));
4693 free(new_id);
4695 err = insert_tree_entry(new_te, &paths);
4696 if (err)
4697 goto done;
4698 (*nentries)++;
4699 continue;
4702 err = match_deleted_or_modified_ct(&ct, te,
4703 path_base_tree, commitable_paths);
4704 if (err)
4705 goto done;
4706 if (ct) {
4707 /* NB: Deleted entries get dropped here. */
4708 if (ct->status == GOT_STATUS_MODIFY ||
4709 ct->status == GOT_STATUS_MODE_CHANGE ||
4710 ct->staged_status == GOT_STATUS_MODIFY) {
4711 err = alloc_modified_blob_tree_entry(
4712 &new_te, te, ct);
4713 if (err)
4714 goto done;
4715 err = insert_tree_entry(new_te, &paths);
4716 if (err)
4717 goto done;
4718 (*nentries)++;
4720 err = report_ct_status(ct, status_cb,
4721 status_arg);
4722 if (err)
4723 goto done;
4724 } else {
4725 /* Entry is unchanged; just copy it. */
4726 err = got_object_tree_entry_dup(&new_te, te);
4727 if (err)
4728 goto done;
4729 err = insert_tree_entry(new_te, &paths);
4730 if (err)
4731 goto done;
4732 (*nentries)++;
4737 /* Write new list of entries; deleted entries have been dropped. */
4738 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4739 done:
4740 got_pathlist_free(&paths);
4741 return err;
4744 static const struct got_error *
4745 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4746 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4747 int have_staged_files)
4749 const struct got_error *err = NULL;
4750 struct got_pathlist_entry *pe;
4752 TAILQ_FOREACH(pe, commitable_paths, entry) {
4753 struct got_fileindex_entry *ie;
4754 struct got_commitable *ct = pe->data;
4756 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4757 if (ie) {
4758 if (ct->status == GOT_STATUS_DELETE ||
4759 ct->staged_status == GOT_STATUS_DELETE) {
4760 got_fileindex_entry_remove(fileindex, ie);
4761 } else if (ct->staged_status == GOT_STATUS_ADD ||
4762 ct->staged_status == GOT_STATUS_MODIFY) {
4763 got_fileindex_entry_stage_set(ie,
4764 GOT_FILEIDX_STAGE_NONE);
4765 err = got_fileindex_entry_update(ie,
4766 ct->ondisk_path, ct->staged_blob_id->sha1,
4767 new_base_commit_id->sha1,
4768 !have_staged_files);
4769 } else
4770 err = got_fileindex_entry_update(ie,
4771 ct->ondisk_path, ct->blob_id->sha1,
4772 new_base_commit_id->sha1,
4773 !have_staged_files);
4774 } else {
4775 err = got_fileindex_entry_alloc(&ie, pe->path);
4776 if (err)
4777 break;
4778 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4779 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4780 if (err) {
4781 got_fileindex_entry_free(ie);
4782 break;
4784 err = got_fileindex_entry_add(fileindex, ie);
4785 if (err) {
4786 got_fileindex_entry_free(ie);
4787 break;
4791 return err;
4795 static const struct got_error *
4796 check_out_of_date(const char *in_repo_path, unsigned char status,
4797 unsigned char staged_status, struct got_object_id *base_blob_id,
4798 struct got_object_id *base_commit_id,
4799 struct got_object_id *head_commit_id, struct got_repository *repo,
4800 int ood_errcode)
4802 const struct got_error *err = NULL;
4803 struct got_object_id *id = NULL;
4805 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4806 /* Trivial case: base commit == head commit */
4807 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4808 return NULL;
4810 * Ensure file content which local changes were based
4811 * on matches file content in the branch head.
4813 err = got_object_id_by_path(&id, repo, head_commit_id,
4814 in_repo_path);
4815 if (err) {
4816 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4817 err = got_error(ood_errcode);
4818 goto done;
4819 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4820 err = got_error(ood_errcode);
4821 } else {
4822 /* Require that added files don't exist in the branch head. */
4823 err = got_object_id_by_path(&id, repo, head_commit_id,
4824 in_repo_path);
4825 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4826 goto done;
4827 err = id ? got_error(ood_errcode) : NULL;
4829 done:
4830 free(id);
4831 return err;
4834 const struct got_error *
4835 commit_worktree(struct got_object_id **new_commit_id,
4836 struct got_pathlist_head *commitable_paths,
4837 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4838 const char *author, const char *committer,
4839 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4840 got_worktree_status_cb status_cb, void *status_arg,
4841 struct got_repository *repo)
4843 const struct got_error *err = NULL, *unlockerr = NULL;
4844 struct got_pathlist_entry *pe;
4845 const char *head_ref_name = NULL;
4846 struct got_commit_object *head_commit = NULL;
4847 struct got_reference *head_ref2 = NULL;
4848 struct got_object_id *head_commit_id2 = NULL;
4849 struct got_tree_object *head_tree = NULL;
4850 struct got_object_id *new_tree_id = NULL;
4851 int nentries;
4852 struct got_object_id_queue parent_ids;
4853 struct got_object_qid *pid = NULL;
4854 char *logmsg = NULL;
4856 *new_commit_id = NULL;
4858 SIMPLEQ_INIT(&parent_ids);
4860 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4861 if (err)
4862 goto done;
4864 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4865 if (err)
4866 goto done;
4868 if (commit_msg_cb != NULL) {
4869 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4870 if (err)
4871 goto done;
4874 if (logmsg == NULL || strlen(logmsg) == 0) {
4875 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4876 goto done;
4879 /* Create blobs from added and modified files and record their IDs. */
4880 TAILQ_FOREACH(pe, commitable_paths, entry) {
4881 struct got_commitable *ct = pe->data;
4882 char *ondisk_path;
4884 /* Blobs for staged files already exist. */
4885 if (ct->staged_status == GOT_STATUS_ADD ||
4886 ct->staged_status == GOT_STATUS_MODIFY)
4887 continue;
4889 if (ct->status != GOT_STATUS_ADD &&
4890 ct->status != GOT_STATUS_MODIFY &&
4891 ct->status != GOT_STATUS_MODE_CHANGE)
4892 continue;
4894 if (asprintf(&ondisk_path, "%s/%s",
4895 worktree->root_path, pe->path) == -1) {
4896 err = got_error_from_errno("asprintf");
4897 goto done;
4899 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4900 if (err) {
4901 free(ondisk_path);
4902 goto done;
4906 * When comitting a symlink we convert "bad" symlinks (those
4907 * which point outside the work tree or into .got) to regular
4908 * files. This way, the post-commit work tree state matches
4909 * a fresh checkout of the tree which was committed.
4911 if (S_ISLNK(get_ct_file_mode(ct))) {
4912 struct got_blob_object *blob;
4913 err = got_object_open_as_blob(&blob, repo, ct->blob_id,
4914 PATH_MAX);
4915 if (err) {
4916 free(ondisk_path);
4917 goto done;
4919 err = install_symlink(worktree, ondisk_path, ct->path,
4920 get_ct_file_mode(ct), GOT_DEFAULT_FILE_MODE, blob,
4921 0, 0, repo, NULL, NULL);
4922 got_object_blob_close(blob);
4923 if (err) {
4924 free(ondisk_path);
4925 goto done;
4928 free(ondisk_path);
4931 /* Recursively write new tree objects. */
4932 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4933 commitable_paths, status_cb, status_arg, repo);
4934 if (err)
4935 goto done;
4937 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4938 if (err)
4939 goto done;
4940 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4941 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4942 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4943 got_object_qid_free(pid);
4944 if (logmsg != NULL)
4945 free(logmsg);
4946 if (err)
4947 goto done;
4949 /* Check if a concurrent commit to our branch has occurred. */
4950 head_ref_name = got_worktree_get_head_ref_name(worktree);
4951 if (head_ref_name == NULL) {
4952 err = got_error_from_errno("got_worktree_get_head_ref_name");
4953 goto done;
4955 /* Lock the reference here to prevent concurrent modification. */
4956 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4957 if (err)
4958 goto done;
4959 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4960 if (err)
4961 goto done;
4962 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4963 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4964 goto done;
4966 /* Update branch head in repository. */
4967 err = got_ref_change_ref(head_ref2, *new_commit_id);
4968 if (err)
4969 goto done;
4970 err = got_ref_write(head_ref2, repo);
4971 if (err)
4972 goto done;
4974 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4975 if (err)
4976 goto done;
4978 err = ref_base_commit(worktree, repo);
4979 if (err)
4980 goto done;
4981 done:
4982 if (head_tree)
4983 got_object_tree_close(head_tree);
4984 if (head_commit)
4985 got_object_commit_close(head_commit);
4986 free(head_commit_id2);
4987 if (head_ref2) {
4988 unlockerr = got_ref_unlock(head_ref2);
4989 if (unlockerr && err == NULL)
4990 err = unlockerr;
4991 got_ref_close(head_ref2);
4993 return err;
4996 static const struct got_error *
4997 check_path_is_commitable(const char *path,
4998 struct got_pathlist_head *commitable_paths)
5000 struct got_pathlist_entry *cpe = NULL;
5001 size_t path_len = strlen(path);
5003 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5004 struct got_commitable *ct = cpe->data;
5005 const char *ct_path = ct->path;
5007 while (ct_path[0] == '/')
5008 ct_path++;
5010 if (strcmp(path, ct_path) == 0 ||
5011 got_path_is_child(ct_path, path, path_len))
5012 break;
5015 if (cpe == NULL)
5016 return got_error_path(path, GOT_ERR_BAD_PATH);
5018 return NULL;
5021 static const struct got_error *
5022 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5024 int *have_staged_files = arg;
5026 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5027 *have_staged_files = 1;
5028 return got_error(GOT_ERR_CANCELLED);
5031 return NULL;
5034 static const struct got_error *
5035 check_non_staged_files(struct got_fileindex *fileindex,
5036 struct got_pathlist_head *paths)
5038 struct got_pathlist_entry *pe;
5039 struct got_fileindex_entry *ie;
5041 TAILQ_FOREACH(pe, paths, entry) {
5042 if (pe->path[0] == '\0')
5043 continue;
5044 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5045 if (ie == NULL)
5046 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5047 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5048 return got_error_path(pe->path,
5049 GOT_ERR_FILE_NOT_STAGED);
5052 return NULL;
5055 const struct got_error *
5056 got_worktree_commit(struct got_object_id **new_commit_id,
5057 struct got_worktree *worktree, struct got_pathlist_head *paths,
5058 const char *author, const char *committer,
5059 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5060 got_worktree_status_cb status_cb, void *status_arg,
5061 struct got_repository *repo)
5063 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5064 struct got_fileindex *fileindex = NULL;
5065 char *fileindex_path = NULL;
5066 struct got_pathlist_head commitable_paths;
5067 struct collect_commitables_arg cc_arg;
5068 struct got_pathlist_entry *pe;
5069 struct got_reference *head_ref = NULL;
5070 struct got_object_id *head_commit_id = NULL;
5071 int have_staged_files = 0;
5073 *new_commit_id = NULL;
5075 TAILQ_INIT(&commitable_paths);
5077 err = lock_worktree(worktree, LOCK_EX);
5078 if (err)
5079 goto done;
5081 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5082 if (err)
5083 goto done;
5085 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5086 if (err)
5087 goto done;
5089 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5090 if (err)
5091 goto done;
5093 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5094 &have_staged_files);
5095 if (err && err->code != GOT_ERR_CANCELLED)
5096 goto done;
5097 if (have_staged_files) {
5098 err = check_non_staged_files(fileindex, paths);
5099 if (err)
5100 goto done;
5103 cc_arg.commitable_paths = &commitable_paths;
5104 cc_arg.worktree = worktree;
5105 cc_arg.repo = repo;
5106 cc_arg.have_staged_files = have_staged_files;
5107 TAILQ_FOREACH(pe, paths, entry) {
5108 err = worktree_status(worktree, pe->path, fileindex, repo,
5109 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5110 if (err)
5111 goto done;
5114 if (TAILQ_EMPTY(&commitable_paths)) {
5115 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5116 goto done;
5119 TAILQ_FOREACH(pe, paths, entry) {
5120 err = check_path_is_commitable(pe->path, &commitable_paths);
5121 if (err)
5122 goto done;
5125 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5126 struct got_commitable *ct = pe->data;
5127 const char *ct_path = ct->in_repo_path;
5129 while (ct_path[0] == '/')
5130 ct_path++;
5131 err = check_out_of_date(ct_path, ct->status,
5132 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5133 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5134 if (err)
5135 goto done;
5139 err = commit_worktree(new_commit_id, &commitable_paths,
5140 head_commit_id, worktree, author, committer,
5141 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5142 if (err)
5143 goto done;
5145 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5146 fileindex, have_staged_files);
5147 sync_err = sync_fileindex(fileindex, fileindex_path);
5148 if (sync_err && err == NULL)
5149 err = sync_err;
5150 done:
5151 if (fileindex)
5152 got_fileindex_free(fileindex);
5153 free(fileindex_path);
5154 unlockerr = lock_worktree(worktree, LOCK_SH);
5155 if (unlockerr && err == NULL)
5156 err = unlockerr;
5157 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5158 struct got_commitable *ct = pe->data;
5159 free_commitable(ct);
5161 got_pathlist_free(&commitable_paths);
5162 return err;
5165 const char *
5166 got_commitable_get_path(struct got_commitable *ct)
5168 return ct->path;
5171 unsigned int
5172 got_commitable_get_status(struct got_commitable *ct)
5174 return ct->status;
5177 struct check_rebase_ok_arg {
5178 struct got_worktree *worktree;
5179 struct got_repository *repo;
5182 static const struct got_error *
5183 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5185 const struct got_error *err = NULL;
5186 struct check_rebase_ok_arg *a = arg;
5187 unsigned char status;
5188 struct stat sb;
5189 char *ondisk_path;
5191 /* Reject rebase of a work tree with mixed base commits. */
5192 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5193 SHA1_DIGEST_LENGTH))
5194 return got_error(GOT_ERR_MIXED_COMMITS);
5196 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5197 == -1)
5198 return got_error_from_errno("asprintf");
5200 /* Reject rebase of a work tree with modified or staged files. */
5201 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5202 free(ondisk_path);
5203 if (err)
5204 return err;
5206 if (status != GOT_STATUS_NO_CHANGE)
5207 return got_error(GOT_ERR_MODIFIED);
5208 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5209 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5211 return NULL;
5214 const struct got_error *
5215 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5216 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5217 struct got_worktree *worktree, struct got_reference *branch,
5218 struct got_repository *repo)
5220 const struct got_error *err = NULL;
5221 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5222 char *branch_ref_name = NULL;
5223 char *fileindex_path = NULL;
5224 struct check_rebase_ok_arg ok_arg;
5225 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5226 struct got_object_id *wt_branch_tip = NULL;
5228 *new_base_branch_ref = NULL;
5229 *tmp_branch = NULL;
5230 *fileindex = NULL;
5232 err = lock_worktree(worktree, LOCK_EX);
5233 if (err)
5234 return err;
5236 err = open_fileindex(fileindex, &fileindex_path, worktree);
5237 if (err)
5238 goto done;
5240 ok_arg.worktree = worktree;
5241 ok_arg.repo = repo;
5242 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5243 &ok_arg);
5244 if (err)
5245 goto done;
5247 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5248 if (err)
5249 goto done;
5251 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5252 if (err)
5253 goto done;
5255 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5256 if (err)
5257 goto done;
5259 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5260 0);
5261 if (err)
5262 goto done;
5264 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5265 if (err)
5266 goto done;
5267 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5268 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5269 goto done;
5272 err = got_ref_alloc_symref(new_base_branch_ref,
5273 new_base_branch_ref_name, wt_branch);
5274 if (err)
5275 goto done;
5276 err = got_ref_write(*new_base_branch_ref, repo);
5277 if (err)
5278 goto done;
5280 /* TODO Lock original branch's ref while rebasing? */
5282 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5283 if (err)
5284 goto done;
5286 err = got_ref_write(branch_ref, repo);
5287 if (err)
5288 goto done;
5290 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5291 worktree->base_commit_id);
5292 if (err)
5293 goto done;
5294 err = got_ref_write(*tmp_branch, repo);
5295 if (err)
5296 goto done;
5298 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5299 if (err)
5300 goto done;
5301 done:
5302 free(fileindex_path);
5303 free(tmp_branch_name);
5304 free(new_base_branch_ref_name);
5305 free(branch_ref_name);
5306 if (branch_ref)
5307 got_ref_close(branch_ref);
5308 if (wt_branch)
5309 got_ref_close(wt_branch);
5310 free(wt_branch_tip);
5311 if (err) {
5312 if (*new_base_branch_ref) {
5313 got_ref_close(*new_base_branch_ref);
5314 *new_base_branch_ref = NULL;
5316 if (*tmp_branch) {
5317 got_ref_close(*tmp_branch);
5318 *tmp_branch = NULL;
5320 if (*fileindex) {
5321 got_fileindex_free(*fileindex);
5322 *fileindex = NULL;
5324 lock_worktree(worktree, LOCK_SH);
5326 return err;
5329 const struct got_error *
5330 got_worktree_rebase_continue(struct got_object_id **commit_id,
5331 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5332 struct got_reference **branch, struct got_fileindex **fileindex,
5333 struct got_worktree *worktree, struct got_repository *repo)
5335 const struct got_error *err;
5336 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5337 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5338 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5339 char *fileindex_path = NULL;
5340 int have_staged_files = 0;
5342 *commit_id = NULL;
5343 *new_base_branch = NULL;
5344 *tmp_branch = NULL;
5345 *branch = NULL;
5346 *fileindex = NULL;
5348 err = lock_worktree(worktree, LOCK_EX);
5349 if (err)
5350 return err;
5352 err = open_fileindex(fileindex, &fileindex_path, worktree);
5353 if (err)
5354 goto done;
5356 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5357 &have_staged_files);
5358 if (err && err->code != GOT_ERR_CANCELLED)
5359 goto done;
5360 if (have_staged_files) {
5361 err = got_error(GOT_ERR_STAGED_PATHS);
5362 goto done;
5365 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5366 if (err)
5367 goto done;
5369 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5370 if (err)
5371 goto done;
5373 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5374 if (err)
5375 goto done;
5377 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5378 if (err)
5379 goto done;
5381 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5382 if (err)
5383 goto done;
5385 err = got_ref_open(branch, repo,
5386 got_ref_get_symref_target(branch_ref), 0);
5387 if (err)
5388 goto done;
5390 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5391 if (err)
5392 goto done;
5394 err = got_ref_resolve(commit_id, repo, commit_ref);
5395 if (err)
5396 goto done;
5398 err = got_ref_open(new_base_branch, repo,
5399 new_base_branch_ref_name, 0);
5400 if (err)
5401 goto done;
5403 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5404 if (err)
5405 goto done;
5406 done:
5407 free(commit_ref_name);
5408 free(branch_ref_name);
5409 free(fileindex_path);
5410 if (commit_ref)
5411 got_ref_close(commit_ref);
5412 if (branch_ref)
5413 got_ref_close(branch_ref);
5414 if (err) {
5415 free(*commit_id);
5416 *commit_id = NULL;
5417 if (*tmp_branch) {
5418 got_ref_close(*tmp_branch);
5419 *tmp_branch = NULL;
5421 if (*new_base_branch) {
5422 got_ref_close(*new_base_branch);
5423 *new_base_branch = NULL;
5425 if (*branch) {
5426 got_ref_close(*branch);
5427 *branch = NULL;
5429 if (*fileindex) {
5430 got_fileindex_free(*fileindex);
5431 *fileindex = NULL;
5433 lock_worktree(worktree, LOCK_SH);
5435 return err;
5438 const struct got_error *
5439 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5441 const struct got_error *err;
5442 char *tmp_branch_name = NULL;
5444 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5445 if (err)
5446 return err;
5448 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5449 free(tmp_branch_name);
5450 return NULL;
5453 static const struct got_error *
5454 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5455 char **logmsg, void *arg)
5457 *logmsg = arg;
5458 return NULL;
5461 static const struct got_error *
5462 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5463 const char *path, struct got_object_id *blob_id,
5464 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5465 int dirfd, const char *de_name)
5467 return NULL;
5470 struct collect_merged_paths_arg {
5471 got_worktree_checkout_cb progress_cb;
5472 void *progress_arg;
5473 struct got_pathlist_head *merged_paths;
5476 static const struct got_error *
5477 collect_merged_paths(void *arg, unsigned char status, const char *path)
5479 const struct got_error *err;
5480 struct collect_merged_paths_arg *a = arg;
5481 char *p;
5482 struct got_pathlist_entry *new;
5484 err = (*a->progress_cb)(a->progress_arg, status, path);
5485 if (err)
5486 return err;
5488 if (status != GOT_STATUS_MERGE &&
5489 status != GOT_STATUS_ADD &&
5490 status != GOT_STATUS_DELETE &&
5491 status != GOT_STATUS_CONFLICT)
5492 return NULL;
5494 p = strdup(path);
5495 if (p == NULL)
5496 return got_error_from_errno("strdup");
5498 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5499 if (err || new == NULL)
5500 free(p);
5501 return err;
5504 void
5505 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5507 struct got_pathlist_entry *pe;
5509 TAILQ_FOREACH(pe, merged_paths, entry)
5510 free((char *)pe->path);
5512 got_pathlist_free(merged_paths);
5515 static const struct got_error *
5516 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5517 int is_rebase, struct got_repository *repo)
5519 const struct got_error *err;
5520 struct got_reference *commit_ref = NULL;
5522 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5523 if (err) {
5524 if (err->code != GOT_ERR_NOT_REF)
5525 goto done;
5526 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5527 if (err)
5528 goto done;
5529 err = got_ref_write(commit_ref, repo);
5530 if (err)
5531 goto done;
5532 } else if (is_rebase) {
5533 struct got_object_id *stored_id;
5534 int cmp;
5536 err = got_ref_resolve(&stored_id, repo, commit_ref);
5537 if (err)
5538 goto done;
5539 cmp = got_object_id_cmp(commit_id, stored_id);
5540 free(stored_id);
5541 if (cmp != 0) {
5542 err = got_error(GOT_ERR_REBASE_COMMITID);
5543 goto done;
5546 done:
5547 if (commit_ref)
5548 got_ref_close(commit_ref);
5549 return err;
5552 static const struct got_error *
5553 rebase_merge_files(struct got_pathlist_head *merged_paths,
5554 const char *commit_ref_name, struct got_worktree *worktree,
5555 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5556 struct got_object_id *commit_id, struct got_repository *repo,
5557 got_worktree_checkout_cb progress_cb, void *progress_arg,
5558 got_cancel_cb cancel_cb, void *cancel_arg)
5560 const struct got_error *err;
5561 struct got_reference *commit_ref = NULL;
5562 struct collect_merged_paths_arg cmp_arg;
5563 char *fileindex_path;
5565 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5567 err = get_fileindex_path(&fileindex_path, worktree);
5568 if (err)
5569 return err;
5571 cmp_arg.progress_cb = progress_cb;
5572 cmp_arg.progress_arg = progress_arg;
5573 cmp_arg.merged_paths = merged_paths;
5574 err = merge_files(worktree, fileindex, fileindex_path,
5575 parent_commit_id, commit_id, repo, collect_merged_paths,
5576 &cmp_arg, cancel_cb, cancel_arg);
5577 if (commit_ref)
5578 got_ref_close(commit_ref);
5579 return err;
5582 const struct got_error *
5583 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5584 struct got_worktree *worktree, struct got_fileindex *fileindex,
5585 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5586 struct got_repository *repo,
5587 got_worktree_checkout_cb progress_cb, void *progress_arg,
5588 got_cancel_cb cancel_cb, void *cancel_arg)
5590 const struct got_error *err;
5591 char *commit_ref_name;
5593 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5594 if (err)
5595 return err;
5597 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5598 if (err)
5599 goto done;
5601 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5602 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5603 progress_arg, cancel_cb, cancel_arg);
5604 done:
5605 free(commit_ref_name);
5606 return err;
5609 const struct got_error *
5610 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5611 struct got_worktree *worktree, struct got_fileindex *fileindex,
5612 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5613 struct got_repository *repo,
5614 got_worktree_checkout_cb progress_cb, void *progress_arg,
5615 got_cancel_cb cancel_cb, void *cancel_arg)
5617 const struct got_error *err;
5618 char *commit_ref_name;
5620 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5621 if (err)
5622 return err;
5624 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5625 if (err)
5626 goto done;
5628 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5629 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5630 progress_arg, cancel_cb, cancel_arg);
5631 done:
5632 free(commit_ref_name);
5633 return err;
5636 static const struct got_error *
5637 rebase_commit(struct got_object_id **new_commit_id,
5638 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5639 struct got_worktree *worktree, struct got_fileindex *fileindex,
5640 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5641 const char *new_logmsg, struct got_repository *repo)
5643 const struct got_error *err, *sync_err;
5644 struct got_pathlist_head commitable_paths;
5645 struct collect_commitables_arg cc_arg;
5646 char *fileindex_path = NULL;
5647 struct got_reference *head_ref = NULL;
5648 struct got_object_id *head_commit_id = NULL;
5649 char *logmsg = NULL;
5651 TAILQ_INIT(&commitable_paths);
5652 *new_commit_id = NULL;
5654 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5656 err = get_fileindex_path(&fileindex_path, worktree);
5657 if (err)
5658 return err;
5660 cc_arg.commitable_paths = &commitable_paths;
5661 cc_arg.worktree = worktree;
5662 cc_arg.repo = repo;
5663 cc_arg.have_staged_files = 0;
5665 * If possible get the status of individual files directly to
5666 * avoid crawling the entire work tree once per rebased commit.
5667 * TODO: Ideally, merged_paths would contain a list of commitables
5668 * we could use so we could skip worktree_status() entirely.
5670 if (merged_paths) {
5671 struct got_pathlist_entry *pe;
5672 TAILQ_FOREACH(pe, merged_paths, entry) {
5673 err = worktree_status(worktree, pe->path, fileindex,
5674 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5675 0);
5676 if (err)
5677 goto done;
5679 } else {
5680 err = worktree_status(worktree, "", fileindex, repo,
5681 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5682 if (err)
5683 goto done;
5686 if (TAILQ_EMPTY(&commitable_paths)) {
5687 /* No-op change; commit will be elided. */
5688 err = got_ref_delete(commit_ref, repo);
5689 if (err)
5690 goto done;
5691 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5692 goto done;
5695 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5696 if (err)
5697 goto done;
5699 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5700 if (err)
5701 goto done;
5703 if (new_logmsg) {
5704 logmsg = strdup(new_logmsg);
5705 if (logmsg == NULL) {
5706 err = got_error_from_errno("strdup");
5707 goto done;
5709 } else {
5710 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5711 if (err)
5712 goto done;
5715 /* NB: commit_worktree will call free(logmsg) */
5716 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5717 worktree, got_object_commit_get_author(orig_commit),
5718 got_object_commit_get_committer(orig_commit),
5719 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5720 if (err)
5721 goto done;
5723 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5724 if (err)
5725 goto done;
5727 err = got_ref_delete(commit_ref, repo);
5728 if (err)
5729 goto done;
5731 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5732 fileindex, 0);
5733 sync_err = sync_fileindex(fileindex, fileindex_path);
5734 if (sync_err && err == NULL)
5735 err = sync_err;
5736 done:
5737 free(fileindex_path);
5738 free(head_commit_id);
5739 if (head_ref)
5740 got_ref_close(head_ref);
5741 if (err) {
5742 free(*new_commit_id);
5743 *new_commit_id = NULL;
5745 return err;
5748 const struct got_error *
5749 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5750 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5751 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5752 struct got_commit_object *orig_commit,
5753 struct got_object_id *orig_commit_id, struct got_repository *repo)
5755 const struct got_error *err;
5756 char *commit_ref_name;
5757 struct got_reference *commit_ref = NULL;
5758 struct got_object_id *commit_id = NULL;
5760 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5761 if (err)
5762 return err;
5764 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5765 if (err)
5766 goto done;
5767 err = got_ref_resolve(&commit_id, repo, commit_ref);
5768 if (err)
5769 goto done;
5770 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5771 err = got_error(GOT_ERR_REBASE_COMMITID);
5772 goto done;
5775 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5776 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5777 done:
5778 if (commit_ref)
5779 got_ref_close(commit_ref);
5780 free(commit_ref_name);
5781 free(commit_id);
5782 return err;
5785 const struct got_error *
5786 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5787 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5788 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5789 struct got_commit_object *orig_commit,
5790 struct got_object_id *orig_commit_id, const char *new_logmsg,
5791 struct got_repository *repo)
5793 const struct got_error *err;
5794 char *commit_ref_name;
5795 struct got_reference *commit_ref = NULL;
5797 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5798 if (err)
5799 return err;
5801 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5802 if (err)
5803 goto done;
5805 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5806 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5807 done:
5808 if (commit_ref)
5809 got_ref_close(commit_ref);
5810 free(commit_ref_name);
5811 return err;
5814 const struct got_error *
5815 got_worktree_rebase_postpone(struct got_worktree *worktree,
5816 struct got_fileindex *fileindex)
5818 if (fileindex)
5819 got_fileindex_free(fileindex);
5820 return lock_worktree(worktree, LOCK_SH);
5823 static const struct got_error *
5824 delete_ref(const char *name, struct got_repository *repo)
5826 const struct got_error *err;
5827 struct got_reference *ref;
5829 err = got_ref_open(&ref, repo, name, 0);
5830 if (err) {
5831 if (err->code == GOT_ERR_NOT_REF)
5832 return NULL;
5833 return err;
5836 err = got_ref_delete(ref, repo);
5837 got_ref_close(ref);
5838 return err;
5841 static const struct got_error *
5842 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5844 const struct got_error *err;
5845 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5846 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5848 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5849 if (err)
5850 goto done;
5851 err = delete_ref(tmp_branch_name, repo);
5852 if (err)
5853 goto done;
5855 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5856 if (err)
5857 goto done;
5858 err = delete_ref(new_base_branch_ref_name, repo);
5859 if (err)
5860 goto done;
5862 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5863 if (err)
5864 goto done;
5865 err = delete_ref(branch_ref_name, repo);
5866 if (err)
5867 goto done;
5869 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5870 if (err)
5871 goto done;
5872 err = delete_ref(commit_ref_name, repo);
5873 if (err)
5874 goto done;
5876 done:
5877 free(tmp_branch_name);
5878 free(new_base_branch_ref_name);
5879 free(branch_ref_name);
5880 free(commit_ref_name);
5881 return err;
5884 const struct got_error *
5885 got_worktree_rebase_complete(struct got_worktree *worktree,
5886 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5887 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5888 struct got_repository *repo)
5890 const struct got_error *err, *unlockerr;
5891 struct got_object_id *new_head_commit_id = NULL;
5893 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5894 if (err)
5895 return err;
5897 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5898 if (err)
5899 goto done;
5901 err = got_ref_write(rebased_branch, repo);
5902 if (err)
5903 goto done;
5905 err = got_worktree_set_head_ref(worktree, rebased_branch);
5906 if (err)
5907 goto done;
5909 err = delete_rebase_refs(worktree, repo);
5910 done:
5911 if (fileindex)
5912 got_fileindex_free(fileindex);
5913 free(new_head_commit_id);
5914 unlockerr = lock_worktree(worktree, LOCK_SH);
5915 if (unlockerr && err == NULL)
5916 err = unlockerr;
5917 return err;
5920 const struct got_error *
5921 got_worktree_rebase_abort(struct got_worktree *worktree,
5922 struct got_fileindex *fileindex, struct got_repository *repo,
5923 struct got_reference *new_base_branch,
5924 got_worktree_checkout_cb progress_cb, void *progress_arg)
5926 const struct got_error *err, *unlockerr, *sync_err;
5927 struct got_reference *resolved = NULL;
5928 struct got_object_id *commit_id = NULL;
5929 char *fileindex_path = NULL;
5930 struct revert_file_args rfa;
5931 struct got_object_id *tree_id = NULL;
5933 err = lock_worktree(worktree, LOCK_EX);
5934 if (err)
5935 return err;
5937 err = got_ref_open(&resolved, repo,
5938 got_ref_get_symref_target(new_base_branch), 0);
5939 if (err)
5940 goto done;
5942 err = got_worktree_set_head_ref(worktree, resolved);
5943 if (err)
5944 goto done;
5947 * XXX commits to the base branch could have happened while
5948 * we were busy rebasing; should we store the original commit ID
5949 * when rebase begins and read it back here?
5951 err = got_ref_resolve(&commit_id, repo, resolved);
5952 if (err)
5953 goto done;
5955 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5956 if (err)
5957 goto done;
5959 err = got_object_id_by_path(&tree_id, repo,
5960 worktree->base_commit_id, worktree->path_prefix);
5961 if (err)
5962 goto done;
5964 err = delete_rebase_refs(worktree, repo);
5965 if (err)
5966 goto done;
5968 err = get_fileindex_path(&fileindex_path, worktree);
5969 if (err)
5970 goto done;
5972 rfa.worktree = worktree;
5973 rfa.fileindex = fileindex;
5974 rfa.progress_cb = progress_cb;
5975 rfa.progress_arg = progress_arg;
5976 rfa.patch_cb = NULL;
5977 rfa.patch_arg = NULL;
5978 rfa.repo = repo;
5979 err = worktree_status(worktree, "", fileindex, repo,
5980 revert_file, &rfa, NULL, NULL, 0, 0);
5981 if (err)
5982 goto sync;
5984 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5985 repo, progress_cb, progress_arg, NULL, NULL);
5986 sync:
5987 sync_err = sync_fileindex(fileindex, fileindex_path);
5988 if (sync_err && err == NULL)
5989 err = sync_err;
5990 done:
5991 got_ref_close(resolved);
5992 free(tree_id);
5993 free(commit_id);
5994 if (fileindex)
5995 got_fileindex_free(fileindex);
5996 free(fileindex_path);
5998 unlockerr = lock_worktree(worktree, LOCK_SH);
5999 if (unlockerr && err == NULL)
6000 err = unlockerr;
6001 return err;
6004 const struct got_error *
6005 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6006 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6007 struct got_fileindex **fileindex, struct got_worktree *worktree,
6008 struct got_repository *repo)
6010 const struct got_error *err = NULL;
6011 char *tmp_branch_name = NULL;
6012 char *branch_ref_name = NULL;
6013 char *base_commit_ref_name = NULL;
6014 char *fileindex_path = NULL;
6015 struct check_rebase_ok_arg ok_arg;
6016 struct got_reference *wt_branch = NULL;
6017 struct got_reference *base_commit_ref = NULL;
6019 *tmp_branch = NULL;
6020 *branch_ref = NULL;
6021 *base_commit_id = NULL;
6022 *fileindex = NULL;
6024 err = lock_worktree(worktree, LOCK_EX);
6025 if (err)
6026 return err;
6028 err = open_fileindex(fileindex, &fileindex_path, worktree);
6029 if (err)
6030 goto done;
6032 ok_arg.worktree = worktree;
6033 ok_arg.repo = repo;
6034 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6035 &ok_arg);
6036 if (err)
6037 goto done;
6039 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6040 if (err)
6041 goto done;
6043 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6044 if (err)
6045 goto done;
6047 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6048 worktree);
6049 if (err)
6050 goto done;
6052 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6053 0);
6054 if (err)
6055 goto done;
6057 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6058 if (err)
6059 goto done;
6061 err = got_ref_write(*branch_ref, repo);
6062 if (err)
6063 goto done;
6065 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6066 worktree->base_commit_id);
6067 if (err)
6068 goto done;
6069 err = got_ref_write(base_commit_ref, repo);
6070 if (err)
6071 goto done;
6072 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6073 if (*base_commit_id == NULL) {
6074 err = got_error_from_errno("got_object_id_dup");
6075 goto done;
6078 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6079 worktree->base_commit_id);
6080 if (err)
6081 goto done;
6082 err = got_ref_write(*tmp_branch, repo);
6083 if (err)
6084 goto done;
6086 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6087 if (err)
6088 goto done;
6089 done:
6090 free(fileindex_path);
6091 free(tmp_branch_name);
6092 free(branch_ref_name);
6093 free(base_commit_ref_name);
6094 if (wt_branch)
6095 got_ref_close(wt_branch);
6096 if (err) {
6097 if (*branch_ref) {
6098 got_ref_close(*branch_ref);
6099 *branch_ref = NULL;
6101 if (*tmp_branch) {
6102 got_ref_close(*tmp_branch);
6103 *tmp_branch = NULL;
6105 free(*base_commit_id);
6106 if (*fileindex) {
6107 got_fileindex_free(*fileindex);
6108 *fileindex = NULL;
6110 lock_worktree(worktree, LOCK_SH);
6112 return err;
6115 const struct got_error *
6116 got_worktree_histedit_postpone(struct got_worktree *worktree,
6117 struct got_fileindex *fileindex)
6119 if (fileindex)
6120 got_fileindex_free(fileindex);
6121 return lock_worktree(worktree, LOCK_SH);
6124 const struct got_error *
6125 got_worktree_histedit_in_progress(int *in_progress,
6126 struct got_worktree *worktree)
6128 const struct got_error *err;
6129 char *tmp_branch_name = NULL;
6131 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6132 if (err)
6133 return err;
6135 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6136 free(tmp_branch_name);
6137 return NULL;
6140 const struct got_error *
6141 got_worktree_histedit_continue(struct got_object_id **commit_id,
6142 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6143 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6144 struct got_worktree *worktree, struct got_repository *repo)
6146 const struct got_error *err;
6147 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6148 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6149 struct got_reference *commit_ref = NULL;
6150 struct got_reference *base_commit_ref = NULL;
6151 char *fileindex_path = NULL;
6152 int have_staged_files = 0;
6154 *commit_id = NULL;
6155 *tmp_branch = NULL;
6156 *base_commit_id = NULL;
6157 *fileindex = NULL;
6159 err = lock_worktree(worktree, LOCK_EX);
6160 if (err)
6161 return err;
6163 err = open_fileindex(fileindex, &fileindex_path, worktree);
6164 if (err)
6165 goto done;
6167 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6168 &have_staged_files);
6169 if (err && err->code != GOT_ERR_CANCELLED)
6170 goto done;
6171 if (have_staged_files) {
6172 err = got_error(GOT_ERR_STAGED_PATHS);
6173 goto done;
6176 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6177 if (err)
6178 goto done;
6180 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6181 if (err)
6182 goto done;
6184 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6185 if (err)
6186 goto done;
6188 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6189 worktree);
6190 if (err)
6191 goto done;
6193 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6194 if (err)
6195 goto done;
6197 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6198 if (err)
6199 goto done;
6200 err = got_ref_resolve(commit_id, repo, commit_ref);
6201 if (err)
6202 goto done;
6204 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6205 if (err)
6206 goto done;
6207 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6208 if (err)
6209 goto done;
6211 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6212 if (err)
6213 goto done;
6214 done:
6215 free(commit_ref_name);
6216 free(branch_ref_name);
6217 free(fileindex_path);
6218 if (commit_ref)
6219 got_ref_close(commit_ref);
6220 if (base_commit_ref)
6221 got_ref_close(base_commit_ref);
6222 if (err) {
6223 free(*commit_id);
6224 *commit_id = NULL;
6225 free(*base_commit_id);
6226 *base_commit_id = NULL;
6227 if (*tmp_branch) {
6228 got_ref_close(*tmp_branch);
6229 *tmp_branch = NULL;
6231 if (*fileindex) {
6232 got_fileindex_free(*fileindex);
6233 *fileindex = NULL;
6235 lock_worktree(worktree, LOCK_EX);
6237 return err;
6240 static const struct got_error *
6241 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6243 const struct got_error *err;
6244 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6245 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6247 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6248 if (err)
6249 goto done;
6250 err = delete_ref(tmp_branch_name, repo);
6251 if (err)
6252 goto done;
6254 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6255 worktree);
6256 if (err)
6257 goto done;
6258 err = delete_ref(base_commit_ref_name, repo);
6259 if (err)
6260 goto done;
6262 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6263 if (err)
6264 goto done;
6265 err = delete_ref(branch_ref_name, repo);
6266 if (err)
6267 goto done;
6269 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6270 if (err)
6271 goto done;
6272 err = delete_ref(commit_ref_name, repo);
6273 if (err)
6274 goto done;
6275 done:
6276 free(tmp_branch_name);
6277 free(base_commit_ref_name);
6278 free(branch_ref_name);
6279 free(commit_ref_name);
6280 return err;
6283 const struct got_error *
6284 got_worktree_histedit_abort(struct got_worktree *worktree,
6285 struct got_fileindex *fileindex, struct got_repository *repo,
6286 struct got_reference *branch, struct got_object_id *base_commit_id,
6287 got_worktree_checkout_cb progress_cb, void *progress_arg)
6289 const struct got_error *err, *unlockerr, *sync_err;
6290 struct got_reference *resolved = NULL;
6291 char *fileindex_path = NULL;
6292 struct got_object_id *tree_id = NULL;
6293 struct revert_file_args rfa;
6295 err = lock_worktree(worktree, LOCK_EX);
6296 if (err)
6297 return err;
6299 err = got_ref_open(&resolved, repo,
6300 got_ref_get_symref_target(branch), 0);
6301 if (err)
6302 goto done;
6304 err = got_worktree_set_head_ref(worktree, resolved);
6305 if (err)
6306 goto done;
6308 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6309 if (err)
6310 goto done;
6312 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6313 worktree->path_prefix);
6314 if (err)
6315 goto done;
6317 err = delete_histedit_refs(worktree, repo);
6318 if (err)
6319 goto done;
6321 err = get_fileindex_path(&fileindex_path, worktree);
6322 if (err)
6323 goto done;
6325 rfa.worktree = worktree;
6326 rfa.fileindex = fileindex;
6327 rfa.progress_cb = progress_cb;
6328 rfa.progress_arg = progress_arg;
6329 rfa.patch_cb = NULL;
6330 rfa.patch_arg = NULL;
6331 rfa.repo = repo;
6332 err = worktree_status(worktree, "", fileindex, repo,
6333 revert_file, &rfa, NULL, NULL, 0, 0);
6334 if (err)
6335 goto sync;
6337 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6338 repo, progress_cb, progress_arg, NULL, NULL);
6339 sync:
6340 sync_err = sync_fileindex(fileindex, fileindex_path);
6341 if (sync_err && err == NULL)
6342 err = sync_err;
6343 done:
6344 got_ref_close(resolved);
6345 free(tree_id);
6346 free(fileindex_path);
6348 unlockerr = lock_worktree(worktree, LOCK_SH);
6349 if (unlockerr && err == NULL)
6350 err = unlockerr;
6351 return err;
6354 const struct got_error *
6355 got_worktree_histedit_complete(struct got_worktree *worktree,
6356 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6357 struct got_reference *edited_branch, struct got_repository *repo)
6359 const struct got_error *err, *unlockerr;
6360 struct got_object_id *new_head_commit_id = NULL;
6361 struct got_reference *resolved = NULL;
6363 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6364 if (err)
6365 return err;
6367 err = got_ref_open(&resolved, repo,
6368 got_ref_get_symref_target(edited_branch), 0);
6369 if (err)
6370 goto done;
6372 err = got_ref_change_ref(resolved, new_head_commit_id);
6373 if (err)
6374 goto done;
6376 err = got_ref_write(resolved, repo);
6377 if (err)
6378 goto done;
6380 err = got_worktree_set_head_ref(worktree, resolved);
6381 if (err)
6382 goto done;
6384 err = delete_histedit_refs(worktree, repo);
6385 done:
6386 if (fileindex)
6387 got_fileindex_free(fileindex);
6388 free(new_head_commit_id);
6389 unlockerr = lock_worktree(worktree, LOCK_SH);
6390 if (unlockerr && err == NULL)
6391 err = unlockerr;
6392 return err;
6395 const struct got_error *
6396 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6397 struct got_object_id *commit_id, struct got_repository *repo)
6399 const struct got_error *err;
6400 char *commit_ref_name;
6402 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6403 if (err)
6404 return err;
6406 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6407 if (err)
6408 goto done;
6410 err = delete_ref(commit_ref_name, repo);
6411 done:
6412 free(commit_ref_name);
6413 return err;
6416 const struct got_error *
6417 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6418 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6419 struct got_worktree *worktree, const char *refname,
6420 struct got_repository *repo)
6422 const struct got_error *err = NULL;
6423 char *fileindex_path = NULL;
6424 struct check_rebase_ok_arg ok_arg;
6426 *fileindex = NULL;
6427 *branch_ref = NULL;
6428 *base_branch_ref = NULL;
6430 err = lock_worktree(worktree, LOCK_EX);
6431 if (err)
6432 return err;
6434 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6435 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6436 "cannot integrate a branch into itself; "
6437 "update -b or different branch name required");
6438 goto done;
6441 err = open_fileindex(fileindex, &fileindex_path, worktree);
6442 if (err)
6443 goto done;
6445 /* Preconditions are the same as for rebase. */
6446 ok_arg.worktree = worktree;
6447 ok_arg.repo = repo;
6448 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6449 &ok_arg);
6450 if (err)
6451 goto done;
6453 err = got_ref_open(branch_ref, repo, refname, 1);
6454 if (err)
6455 goto done;
6457 err = got_ref_open(base_branch_ref, repo,
6458 got_worktree_get_head_ref_name(worktree), 1);
6459 done:
6460 if (err) {
6461 if (*branch_ref) {
6462 got_ref_close(*branch_ref);
6463 *branch_ref = NULL;
6465 if (*base_branch_ref) {
6466 got_ref_close(*base_branch_ref);
6467 *base_branch_ref = NULL;
6469 if (*fileindex) {
6470 got_fileindex_free(*fileindex);
6471 *fileindex = NULL;
6473 lock_worktree(worktree, LOCK_SH);
6475 return err;
6478 const struct got_error *
6479 got_worktree_integrate_continue(struct got_worktree *worktree,
6480 struct got_fileindex *fileindex, struct got_repository *repo,
6481 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6482 got_worktree_checkout_cb progress_cb, void *progress_arg,
6483 got_cancel_cb cancel_cb, void *cancel_arg)
6485 const struct got_error *err = NULL, *sync_err, *unlockerr;
6486 char *fileindex_path = NULL;
6487 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6489 err = get_fileindex_path(&fileindex_path, worktree);
6490 if (err)
6491 goto done;
6493 err = got_ref_resolve(&commit_id, repo, branch_ref);
6494 if (err)
6495 goto done;
6497 err = got_object_id_by_path(&tree_id, repo, commit_id,
6498 worktree->path_prefix);
6499 if (err)
6500 goto done;
6502 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6503 if (err)
6504 goto done;
6506 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6507 progress_cb, progress_arg, cancel_cb, cancel_arg);
6508 if (err)
6509 goto sync;
6511 err = got_ref_change_ref(base_branch_ref, commit_id);
6512 if (err)
6513 goto sync;
6515 err = got_ref_write(base_branch_ref, repo);
6516 sync:
6517 sync_err = sync_fileindex(fileindex, fileindex_path);
6518 if (sync_err && err == NULL)
6519 err = sync_err;
6521 done:
6522 unlockerr = got_ref_unlock(branch_ref);
6523 if (unlockerr && err == NULL)
6524 err = unlockerr;
6525 got_ref_close(branch_ref);
6527 unlockerr = got_ref_unlock(base_branch_ref);
6528 if (unlockerr && err == NULL)
6529 err = unlockerr;
6530 got_ref_close(base_branch_ref);
6532 got_fileindex_free(fileindex);
6533 free(fileindex_path);
6534 free(tree_id);
6536 unlockerr = lock_worktree(worktree, LOCK_SH);
6537 if (unlockerr && err == NULL)
6538 err = unlockerr;
6539 return err;
6542 const struct got_error *
6543 got_worktree_integrate_abort(struct got_worktree *worktree,
6544 struct got_fileindex *fileindex, struct got_repository *repo,
6545 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6547 const struct got_error *err = NULL, *unlockerr = NULL;
6549 got_fileindex_free(fileindex);
6551 err = lock_worktree(worktree, LOCK_SH);
6553 unlockerr = got_ref_unlock(branch_ref);
6554 if (unlockerr && err == NULL)
6555 err = unlockerr;
6556 got_ref_close(branch_ref);
6558 unlockerr = got_ref_unlock(base_branch_ref);
6559 if (unlockerr && err == NULL)
6560 err = unlockerr;
6561 got_ref_close(base_branch_ref);
6563 return err;
6566 struct check_stage_ok_arg {
6567 struct got_object_id *head_commit_id;
6568 struct got_worktree *worktree;
6569 struct got_fileindex *fileindex;
6570 struct got_repository *repo;
6571 int have_changes;
6574 const struct got_error *
6575 check_stage_ok(void *arg, unsigned char status,
6576 unsigned char staged_status, const char *relpath,
6577 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6578 struct got_object_id *commit_id, int dirfd, const char *de_name)
6580 struct check_stage_ok_arg *a = arg;
6581 const struct got_error *err = NULL;
6582 struct got_fileindex_entry *ie;
6583 struct got_object_id base_commit_id;
6584 struct got_object_id *base_commit_idp = NULL;
6585 char *in_repo_path = NULL, *p;
6587 if (status == GOT_STATUS_UNVERSIONED ||
6588 status == GOT_STATUS_NO_CHANGE)
6589 return NULL;
6590 if (status == GOT_STATUS_NONEXISTENT)
6591 return got_error_set_errno(ENOENT, relpath);
6593 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6594 if (ie == NULL)
6595 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6597 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6598 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6599 relpath) == -1)
6600 return got_error_from_errno("asprintf");
6602 if (got_fileindex_entry_has_commit(ie)) {
6603 memcpy(base_commit_id.sha1, ie->commit_sha1,
6604 SHA1_DIGEST_LENGTH);
6605 base_commit_idp = &base_commit_id;
6608 if (status == GOT_STATUS_CONFLICT) {
6609 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6610 goto done;
6611 } else if (status != GOT_STATUS_ADD &&
6612 status != GOT_STATUS_MODIFY &&
6613 status != GOT_STATUS_DELETE) {
6614 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6615 goto done;
6618 a->have_changes = 1;
6620 p = in_repo_path;
6621 while (p[0] == '/')
6622 p++;
6623 err = check_out_of_date(p, status, staged_status,
6624 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6625 GOT_ERR_STAGE_OUT_OF_DATE);
6626 done:
6627 free(in_repo_path);
6628 return err;
6631 struct stage_path_arg {
6632 struct got_worktree *worktree;
6633 struct got_fileindex *fileindex;
6634 struct got_repository *repo;
6635 got_worktree_status_cb status_cb;
6636 void *status_arg;
6637 got_worktree_patch_cb patch_cb;
6638 void *patch_arg;
6639 int staged_something;
6642 static const struct got_error *
6643 stage_path(void *arg, unsigned char status,
6644 unsigned char staged_status, const char *relpath,
6645 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6646 struct got_object_id *commit_id, int dirfd, const char *de_name)
6648 struct stage_path_arg *a = arg;
6649 const struct got_error *err = NULL;
6650 struct got_fileindex_entry *ie;
6651 char *ondisk_path = NULL, *path_content = NULL;
6652 uint32_t stage;
6653 struct got_object_id *new_staged_blob_id = NULL;
6655 if (status == GOT_STATUS_UNVERSIONED)
6656 return NULL;
6658 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6659 if (ie == NULL)
6660 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6662 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6663 relpath)== -1)
6664 return got_error_from_errno("asprintf");
6666 switch (status) {
6667 case GOT_STATUS_ADD:
6668 case GOT_STATUS_MODIFY:
6669 if (a->patch_cb) {
6670 if (status == GOT_STATUS_ADD) {
6671 int choice = GOT_PATCH_CHOICE_NONE;
6672 err = (*a->patch_cb)(&choice, a->patch_arg,
6673 status, ie->path, NULL, 1, 1);
6674 if (err)
6675 break;
6676 if (choice != GOT_PATCH_CHOICE_YES)
6677 break;
6678 } else {
6679 err = create_patched_content(&path_content, 0,
6680 staged_blob_id ? staged_blob_id : blob_id,
6681 ondisk_path, dirfd, de_name, ie->path,
6682 a->repo, a->patch_cb, a->patch_arg);
6683 if (err || path_content == NULL)
6684 break;
6687 err = got_object_blob_create(&new_staged_blob_id,
6688 path_content ? path_content : ondisk_path, a->repo);
6689 if (err)
6690 break;
6691 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6692 SHA1_DIGEST_LENGTH);
6693 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6694 stage = GOT_FILEIDX_STAGE_ADD;
6695 else
6696 stage = GOT_FILEIDX_STAGE_MODIFY;
6697 got_fileindex_entry_stage_set(ie, stage);
6698 a->staged_something = 1;
6699 if (a->status_cb == NULL)
6700 break;
6701 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6702 get_staged_status(ie), relpath, blob_id,
6703 new_staged_blob_id, NULL, dirfd, de_name);
6704 break;
6705 case GOT_STATUS_DELETE:
6706 if (staged_status == GOT_STATUS_DELETE)
6707 break;
6708 if (a->patch_cb) {
6709 int choice = GOT_PATCH_CHOICE_NONE;
6710 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6711 ie->path, NULL, 1, 1);
6712 if (err)
6713 break;
6714 if (choice == GOT_PATCH_CHOICE_NO)
6715 break;
6716 if (choice != GOT_PATCH_CHOICE_YES) {
6717 err = got_error(GOT_ERR_PATCH_CHOICE);
6718 break;
6721 stage = GOT_FILEIDX_STAGE_DELETE;
6722 got_fileindex_entry_stage_set(ie, stage);
6723 a->staged_something = 1;
6724 if (a->status_cb == NULL)
6725 break;
6726 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6727 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6728 de_name);
6729 break;
6730 case GOT_STATUS_NO_CHANGE:
6731 break;
6732 case GOT_STATUS_CONFLICT:
6733 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6734 break;
6735 case GOT_STATUS_NONEXISTENT:
6736 err = got_error_set_errno(ENOENT, relpath);
6737 break;
6738 default:
6739 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6740 break;
6743 if (path_content && unlink(path_content) == -1 && err == NULL)
6744 err = got_error_from_errno2("unlink", path_content);
6745 free(path_content);
6746 free(ondisk_path);
6747 free(new_staged_blob_id);
6748 return err;
6751 const struct got_error *
6752 got_worktree_stage(struct got_worktree *worktree,
6753 struct got_pathlist_head *paths,
6754 got_worktree_status_cb status_cb, void *status_arg,
6755 got_worktree_patch_cb patch_cb, void *patch_arg,
6756 struct got_repository *repo)
6758 const struct got_error *err = NULL, *sync_err, *unlockerr;
6759 struct got_pathlist_entry *pe;
6760 struct got_fileindex *fileindex = NULL;
6761 char *fileindex_path = NULL;
6762 struct got_reference *head_ref = NULL;
6763 struct got_object_id *head_commit_id = NULL;
6764 struct check_stage_ok_arg oka;
6765 struct stage_path_arg spa;
6767 err = lock_worktree(worktree, LOCK_EX);
6768 if (err)
6769 return err;
6771 err = got_ref_open(&head_ref, repo,
6772 got_worktree_get_head_ref_name(worktree), 0);
6773 if (err)
6774 goto done;
6775 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6776 if (err)
6777 goto done;
6778 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6779 if (err)
6780 goto done;
6782 /* Check pre-conditions before staging anything. */
6783 oka.head_commit_id = head_commit_id;
6784 oka.worktree = worktree;
6785 oka.fileindex = fileindex;
6786 oka.repo = repo;
6787 oka.have_changes = 0;
6788 TAILQ_FOREACH(pe, paths, entry) {
6789 err = worktree_status(worktree, pe->path, fileindex, repo,
6790 check_stage_ok, &oka, NULL, NULL, 0, 0);
6791 if (err)
6792 goto done;
6794 if (!oka.have_changes) {
6795 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6796 goto done;
6799 spa.worktree = worktree;
6800 spa.fileindex = fileindex;
6801 spa.repo = repo;
6802 spa.patch_cb = patch_cb;
6803 spa.patch_arg = patch_arg;
6804 spa.status_cb = status_cb;
6805 spa.status_arg = status_arg;
6806 spa.staged_something = 0;
6807 TAILQ_FOREACH(pe, paths, entry) {
6808 err = worktree_status(worktree, pe->path, fileindex, repo,
6809 stage_path, &spa, NULL, NULL, 0, 0);
6810 if (err)
6811 goto done;
6813 if (!spa.staged_something) {
6814 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6815 goto done;
6818 sync_err = sync_fileindex(fileindex, fileindex_path);
6819 if (sync_err && err == NULL)
6820 err = sync_err;
6821 done:
6822 if (head_ref)
6823 got_ref_close(head_ref);
6824 free(head_commit_id);
6825 free(fileindex_path);
6826 if (fileindex)
6827 got_fileindex_free(fileindex);
6828 unlockerr = lock_worktree(worktree, LOCK_SH);
6829 if (unlockerr && err == NULL)
6830 err = unlockerr;
6831 return err;
6834 struct unstage_path_arg {
6835 struct got_worktree *worktree;
6836 struct got_fileindex *fileindex;
6837 struct got_repository *repo;
6838 got_worktree_checkout_cb progress_cb;
6839 void *progress_arg;
6840 got_worktree_patch_cb patch_cb;
6841 void *patch_arg;
6844 static const struct got_error *
6845 create_unstaged_content(char **path_unstaged_content,
6846 char **path_new_staged_content, struct got_object_id *blob_id,
6847 struct got_object_id *staged_blob_id, const char *relpath,
6848 struct got_repository *repo,
6849 got_worktree_patch_cb patch_cb, void *patch_arg)
6851 const struct got_error *err;
6852 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6853 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6854 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6855 struct stat sb1, sb2;
6856 struct got_diff_changes *changes = NULL;
6857 struct got_diff_state *ds = NULL;
6858 struct got_diff_args *args = NULL;
6859 struct got_diff_change *change;
6860 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6861 int have_content = 0, have_rejected_content = 0;
6863 *path_unstaged_content = NULL;
6864 *path_new_staged_content = NULL;
6866 err = got_object_id_str(&label1, blob_id);
6867 if (err)
6868 return err;
6869 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6870 if (err)
6871 goto done;
6873 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6874 if (err)
6875 goto done;
6877 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6878 if (err)
6879 goto done;
6881 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6882 if (err)
6883 goto done;
6885 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6886 if (err)
6887 goto done;
6889 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6890 if (err)
6891 goto done;
6893 if (stat(path1, &sb1) == -1) {
6894 err = got_error_from_errno2("stat", path1);
6895 goto done;
6898 if (stat(path2, &sb2) == -1) {
6899 err = got_error_from_errno2("stat", path2);
6900 goto done;
6903 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6904 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6905 if (err)
6906 goto done;
6908 err = got_opentemp_named(path_unstaged_content, &outfile,
6909 "got-unstaged-content");
6910 if (err)
6911 goto done;
6912 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6913 "got-new-staged-content");
6914 if (err)
6915 goto done;
6917 if (fseek(f1, 0L, SEEK_SET) == -1) {
6918 err = got_ferror(f1, GOT_ERR_IO);
6919 goto done;
6921 if (fseek(f2, 0L, SEEK_SET) == -1) {
6922 err = got_ferror(f2, GOT_ERR_IO);
6923 goto done;
6925 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6926 int choice;
6927 err = apply_or_reject_change(&choice, change, ++n,
6928 changes->nchanges, ds, args, diff_flags, relpath,
6929 f1, f2, &line_cur1, &line_cur2,
6930 outfile, rejectfile, patch_cb, patch_arg);
6931 if (err)
6932 goto done;
6933 if (choice == GOT_PATCH_CHOICE_YES)
6934 have_content = 1;
6935 else
6936 have_rejected_content = 1;
6937 if (choice == GOT_PATCH_CHOICE_QUIT)
6938 break;
6940 if (have_content || have_rejected_content)
6941 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6942 outfile, rejectfile);
6943 done:
6944 free(label1);
6945 if (blob)
6946 got_object_blob_close(blob);
6947 if (staged_blob)
6948 got_object_blob_close(staged_blob);
6949 if (f1 && fclose(f1) == EOF && err == NULL)
6950 err = got_error_from_errno2("fclose", path1);
6951 if (f2 && fclose(f2) == EOF && err == NULL)
6952 err = got_error_from_errno2("fclose", path2);
6953 if (outfile && fclose(outfile) == EOF && err == NULL)
6954 err = got_error_from_errno2("fclose", *path_unstaged_content);
6955 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6956 err = got_error_from_errno2("fclose", *path_new_staged_content);
6957 if (path1 && unlink(path1) == -1 && err == NULL)
6958 err = got_error_from_errno2("unlink", path1);
6959 if (path2 && unlink(path2) == -1 && err == NULL)
6960 err = got_error_from_errno2("unlink", path2);
6961 if (err || !have_content) {
6962 if (*path_unstaged_content &&
6963 unlink(*path_unstaged_content) == -1 && err == NULL)
6964 err = got_error_from_errno2("unlink",
6965 *path_unstaged_content);
6966 free(*path_unstaged_content);
6967 *path_unstaged_content = NULL;
6969 if (err || !have_rejected_content) {
6970 if (*path_new_staged_content &&
6971 unlink(*path_new_staged_content) == -1 && err == NULL)
6972 err = got_error_from_errno2("unlink",
6973 *path_new_staged_content);
6974 free(*path_new_staged_content);
6975 *path_new_staged_content = NULL;
6977 free(args);
6978 if (ds) {
6979 got_diff_state_free(ds);
6980 free(ds);
6982 if (changes)
6983 got_diff_free_changes(changes);
6984 free(path1);
6985 free(path2);
6986 return err;
6989 static const struct got_error *
6990 unstage_path(void *arg, unsigned char status,
6991 unsigned char staged_status, const char *relpath,
6992 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6993 struct got_object_id *commit_id, int dirfd, const char *de_name)
6995 const struct got_error *err = NULL;
6996 struct unstage_path_arg *a = arg;
6997 struct got_fileindex_entry *ie;
6998 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6999 char *ondisk_path = NULL, *path_unstaged_content = NULL;
7000 char *path_new_staged_content = NULL;
7001 char *id_str = NULL, *label_orig = NULL;
7002 int local_changes_subsumed;
7003 struct stat sb;
7005 if (staged_status != GOT_STATUS_ADD &&
7006 staged_status != GOT_STATUS_MODIFY &&
7007 staged_status != GOT_STATUS_DELETE)
7008 return NULL;
7010 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7011 if (ie == NULL)
7012 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7014 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7015 == -1)
7016 return got_error_from_errno("asprintf");
7018 err = got_object_id_str(&id_str,
7019 commit_id ? commit_id : a->worktree->base_commit_id);
7020 if (err)
7021 goto done;
7022 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7023 id_str) == -1) {
7024 err = got_error_from_errno("asprintf");
7025 goto done;
7028 switch (staged_status) {
7029 case GOT_STATUS_MODIFY:
7030 err = got_object_open_as_blob(&blob_base, a->repo,
7031 blob_id, 8192);
7032 if (err)
7033 break;
7034 /* fall through */
7035 case GOT_STATUS_ADD:
7036 if (a->patch_cb) {
7037 if (staged_status == GOT_STATUS_ADD) {
7038 int choice = GOT_PATCH_CHOICE_NONE;
7039 err = (*a->patch_cb)(&choice, a->patch_arg,
7040 staged_status, ie->path, NULL, 1, 1);
7041 if (err)
7042 break;
7043 if (choice != GOT_PATCH_CHOICE_YES)
7044 break;
7045 } else {
7046 err = create_unstaged_content(
7047 &path_unstaged_content,
7048 &path_new_staged_content, blob_id,
7049 staged_blob_id, ie->path, a->repo,
7050 a->patch_cb, a->patch_arg);
7051 if (err || path_unstaged_content == NULL)
7052 break;
7053 if (path_new_staged_content) {
7054 err = got_object_blob_create(
7055 &staged_blob_id,
7056 path_new_staged_content,
7057 a->repo);
7058 if (err)
7059 break;
7060 memcpy(ie->staged_blob_sha1,
7061 staged_blob_id->sha1,
7062 SHA1_DIGEST_LENGTH);
7064 err = merge_file(&local_changes_subsumed,
7065 a->worktree, blob_base, ondisk_path,
7066 relpath, got_fileindex_perms_to_st(ie),
7067 path_unstaged_content, label_orig,
7068 "unstaged", a->repo, a->progress_cb,
7069 a->progress_arg);
7070 if (err == NULL &&
7071 path_new_staged_content == NULL)
7072 got_fileindex_entry_stage_set(ie,
7073 GOT_FILEIDX_STAGE_NONE);
7074 break; /* Done with this file. */
7077 err = got_object_open_as_blob(&blob_staged, a->repo,
7078 staged_blob_id, 8192);
7079 if (err)
7080 break;
7081 err = merge_blob(&local_changes_subsumed, a->worktree,
7082 blob_base, ondisk_path, relpath,
7083 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7084 commit_id ? commit_id : a->worktree->base_commit_id,
7085 a->repo, a->progress_cb, a->progress_arg);
7086 if (err == NULL)
7087 got_fileindex_entry_stage_set(ie,
7088 GOT_FILEIDX_STAGE_NONE);
7089 break;
7090 case GOT_STATUS_DELETE:
7091 if (a->patch_cb) {
7092 int choice = GOT_PATCH_CHOICE_NONE;
7093 err = (*a->patch_cb)(&choice, a->patch_arg,
7094 staged_status, ie->path, NULL, 1, 1);
7095 if (err)
7096 break;
7097 if (choice == GOT_PATCH_CHOICE_NO)
7098 break;
7099 if (choice != GOT_PATCH_CHOICE_YES) {
7100 err = got_error(GOT_ERR_PATCH_CHOICE);
7101 break;
7104 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7105 err = get_file_status(&status, &sb, ie, ondisk_path,
7106 dirfd, de_name, a->repo);
7107 if (err)
7108 break;
7109 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7110 break;
7112 done:
7113 free(ondisk_path);
7114 if (path_unstaged_content &&
7115 unlink(path_unstaged_content) == -1 && err == NULL)
7116 err = got_error_from_errno2("unlink", path_unstaged_content);
7117 if (path_new_staged_content &&
7118 unlink(path_new_staged_content) == -1 && err == NULL)
7119 err = got_error_from_errno2("unlink", path_new_staged_content);
7120 free(path_unstaged_content);
7121 free(path_new_staged_content);
7122 if (blob_base)
7123 got_object_blob_close(blob_base);
7124 if (blob_staged)
7125 got_object_blob_close(blob_staged);
7126 free(id_str);
7127 free(label_orig);
7128 return err;
7131 const struct got_error *
7132 got_worktree_unstage(struct got_worktree *worktree,
7133 struct got_pathlist_head *paths,
7134 got_worktree_checkout_cb progress_cb, void *progress_arg,
7135 got_worktree_patch_cb patch_cb, void *patch_arg,
7136 struct got_repository *repo)
7138 const struct got_error *err = NULL, *sync_err, *unlockerr;
7139 struct got_pathlist_entry *pe;
7140 struct got_fileindex *fileindex = NULL;
7141 char *fileindex_path = NULL;
7142 struct unstage_path_arg upa;
7144 err = lock_worktree(worktree, LOCK_EX);
7145 if (err)
7146 return err;
7148 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7149 if (err)
7150 goto done;
7152 upa.worktree = worktree;
7153 upa.fileindex = fileindex;
7154 upa.repo = repo;
7155 upa.progress_cb = progress_cb;
7156 upa.progress_arg = progress_arg;
7157 upa.patch_cb = patch_cb;
7158 upa.patch_arg = patch_arg;
7159 TAILQ_FOREACH(pe, paths, entry) {
7160 err = worktree_status(worktree, pe->path, fileindex, repo,
7161 unstage_path, &upa, NULL, NULL, 0, 0);
7162 if (err)
7163 goto done;
7166 sync_err = sync_fileindex(fileindex, fileindex_path);
7167 if (sync_err && err == NULL)
7168 err = sync_err;
7169 done:
7170 free(fileindex_path);
7171 if (fileindex)
7172 got_fileindex_free(fileindex);
7173 unlockerr = lock_worktree(worktree, LOCK_SH);
7174 if (unlockerr && err == NULL)
7175 err = unlockerr;
7176 return err;