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, struct got_repository *repo,
1029 got_worktree_checkout_cb progress_cb, void *progress_arg);
1031 static const struct got_error *
1032 install_symlink(struct got_worktree *worktree, const char *ondisk_path,
1033 const char *path, mode_t te_mode, mode_t st_mode,
1034 struct got_blob_object *blob, int restoring_missing_file,
1035 int reverting_versioned_file, struct got_repository *repo,
1036 got_worktree_checkout_cb progress_cb, void *progress_arg)
1038 const struct got_error *err = NULL;
1039 char target_path[PATH_MAX];
1040 size_t len, target_len = 0;
1041 char *resolved_path = NULL, *abspath = NULL;
1042 char *path_got = NULL;
1043 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1044 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1047 * Blob object content specifies the target path of the link.
1048 * If a symbolic link cannot be installed we instead create
1049 * a regular file which contains the link target path stored
1050 * in the blob object.
1052 do {
1053 err = got_object_blob_read_block(&len, blob);
1054 if (len + target_len >= sizeof(target_path)) {
1055 /* Path too long; install as a regular file. */
1056 got_object_blob_rewind(blob);
1057 return install_blob(worktree, ondisk_path, path,
1058 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1059 restoring_missing_file, reverting_versioned_file,
1060 repo, progress_cb, progress_arg);
1062 if (len > 0) {
1063 /* Skip blob object header first time around. */
1064 memcpy(target_path + target_len, buf + hdrlen,
1065 len - hdrlen);
1066 target_len += len - hdrlen;
1067 hdrlen = 0;
1069 } while (len != 0);
1070 target_path[target_len] = '\0';
1073 * Relative symlink target lookup should begin at the directory
1074 * in which the blob object is being installed.
1076 if (!got_path_is_absolute(target_path)) {
1077 char *parent = dirname(ondisk_path);
1078 if (parent == NULL) {
1079 err = got_error_from_errno2("dirname", ondisk_path);
1080 goto done;
1082 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1083 err = got_error_from_errno("asprintf");
1084 goto done;
1089 * unveil(2) restricts our view of paths in the filesystem.
1090 * ENOENT will occur if a link target path does not exist or
1091 * if it points outside our unveiled path space.
1093 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1094 if (resolved_path == NULL) {
1095 if (errno != ENOENT) {
1096 err = got_error_from_errno2("realpath", target_path);
1097 goto done;
1101 /* Only allow symlinks pointing at paths within the work tree. */
1102 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1103 abspath : target_path), worktree->root_path,
1104 strlen(worktree->root_path))) {
1105 /* install as a regular file */
1106 got_object_blob_rewind(blob);
1107 err = install_blob(worktree, ondisk_path, path,
1108 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1109 restoring_missing_file, reverting_versioned_file,
1110 repo, progress_cb, progress_arg);
1111 goto done;
1114 /* Do not allow symlinks pointing into the .got directory. */
1115 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1116 GOT_WORKTREE_GOT_DIR) == -1) {
1117 err = got_error_from_errno("asprintf");
1118 goto done;
1120 if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1121 abspath : target_path), path_got, strlen(path_got))) {
1122 /* install as a regular file */
1123 got_object_blob_rewind(blob);
1124 err = install_blob(worktree, ondisk_path, path,
1125 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1126 restoring_missing_file, reverting_versioned_file,
1127 repo, progress_cb, progress_arg);
1128 goto done;
1131 if (symlink(target_path, ondisk_path) == -1) {
1132 if (errno == EEXIST) {
1133 struct stat sb;
1134 ssize_t elen;
1135 char etarget[PATH_MAX];
1136 if (lstat(ondisk_path, &sb) == -1) {
1137 err = got_error_from_errno2("lstat",
1138 ondisk_path);
1139 goto done;
1141 if (!S_ISLNK(sb.st_mode)) {
1142 err = got_error_path(ondisk_path,
1143 GOT_ERR_FILE_OBSTRUCTED);
1144 goto done;
1146 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1147 if (elen == -1) {
1148 err = got_error_from_errno2("readlink",
1149 ondisk_path);
1150 goto done;
1152 if (elen == target_len &&
1153 memcmp(etarget, target_path, target_len) == 0) {
1154 err = NULL; /* nothing to do */
1155 goto done;
1156 } else {
1157 err = update_symlink(ondisk_path, target_path,
1158 target_len);
1159 if (err)
1160 goto done;
1161 err = (*progress_cb)(progress_arg,
1162 GOT_STATUS_UPDATE, path);
1163 goto done;
1167 if (errno == ENOENT) {
1168 char *parent = dirname(ondisk_path);
1169 if (parent == NULL) {
1170 err = got_error_from_errno2("dirname",
1171 ondisk_path);
1172 goto done;
1174 err = add_dir_on_disk(worktree, parent);
1175 if (err)
1176 goto done;
1178 * Retry, and fall through to error handling
1179 * below if this second attempt fails.
1181 if (symlink(target_path, ondisk_path) != -1) {
1182 err = NULL; /* success */
1183 goto done;
1187 /* Handle errors from first or second creation attempt. */
1188 if (errno == ENAMETOOLONG) {
1189 /* bad target path; install as a regular file */
1190 got_object_blob_rewind(blob);
1191 err = install_blob(worktree, ondisk_path, path,
1192 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1193 restoring_missing_file, reverting_versioned_file,
1194 repo, progress_cb, progress_arg);
1195 } else if (errno == ENOTDIR) {
1196 err = got_error_path(ondisk_path,
1197 GOT_ERR_FILE_OBSTRUCTED);
1198 } else {
1199 err = got_error_from_errno3("symlink",
1200 target_path, ondisk_path);
1202 } else
1203 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1204 done:
1205 free(resolved_path);
1206 free(abspath);
1207 free(path_got);
1208 return err;
1211 static const struct got_error *
1212 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1213 const char *path, mode_t te_mode, mode_t st_mode,
1214 struct got_blob_object *blob, int restoring_missing_file,
1215 int reverting_versioned_file, struct got_repository *repo,
1216 got_worktree_checkout_cb progress_cb, void *progress_arg)
1218 const struct got_error *err = NULL;
1219 int fd = -1;
1220 size_t len, hdrlen;
1221 int update = 0;
1222 char *tmppath = NULL;
1224 if (S_ISLNK(te_mode))
1225 return install_symlink(worktree, ondisk_path, path, te_mode,
1226 st_mode, blob, restoring_missing_file,
1227 reverting_versioned_file, repo, progress_cb, progress_arg);
1229 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1230 GOT_DEFAULT_FILE_MODE);
1231 if (fd == -1) {
1232 if (errno == ENOENT) {
1233 char *parent = dirname(path);
1234 if (parent == NULL)
1235 return got_error_from_errno2("dirname", path);
1236 err = add_dir_on_disk(worktree, parent);
1237 if (err)
1238 return err;
1239 fd = open(ondisk_path,
1240 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1241 GOT_DEFAULT_FILE_MODE);
1242 if (fd == -1)
1243 return got_error_from_errno2("open",
1244 ondisk_path);
1245 } else if (errno == EEXIST) {
1246 if (!S_ISREG(st_mode)) {
1247 /* TODO file is obstructed; do something */
1248 err = got_error_path(ondisk_path,
1249 GOT_ERR_FILE_OBSTRUCTED);
1250 goto done;
1251 } else {
1252 err = got_opentemp_named_fd(&tmppath, &fd,
1253 ondisk_path);
1254 if (err)
1255 goto done;
1256 update = 1;
1258 } else
1259 return got_error_from_errno2("open", ondisk_path);
1262 if (restoring_missing_file)
1263 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
1264 else if (reverting_versioned_file)
1265 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
1266 else
1267 err = (*progress_cb)(progress_arg,
1268 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1269 if (err)
1270 goto done;
1272 hdrlen = got_object_blob_get_hdrlen(blob);
1273 do {
1274 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1275 err = got_object_blob_read_block(&len, blob);
1276 if (err)
1277 break;
1278 if (len > 0) {
1279 /* Skip blob object header first time around. */
1280 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1281 if (outlen == -1) {
1282 err = got_error_from_errno("write");
1283 goto done;
1284 } else if (outlen != len - hdrlen) {
1285 err = got_error(GOT_ERR_IO);
1286 goto done;
1288 hdrlen = 0;
1290 } while (len != 0);
1292 if (fsync(fd) != 0) {
1293 err = got_error_from_errno("fsync");
1294 goto done;
1297 if (update) {
1298 if (rename(tmppath, ondisk_path) != 0) {
1299 err = got_error_from_errno3("rename", tmppath,
1300 ondisk_path);
1301 unlink(tmppath);
1302 goto done;
1306 if (chmod(ondisk_path,
1307 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1308 err = got_error_from_errno2("chmod", ondisk_path);
1309 goto done;
1312 done:
1313 if (fd != -1 && close(fd) != 0 && err == NULL)
1314 err = got_error_from_errno("close");
1315 free(tmppath);
1316 return err;
1319 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1320 static const struct got_error *
1321 get_modified_file_content_status(unsigned char *status, FILE *f)
1323 const struct got_error *err = NULL;
1324 const char *markers[3] = {
1325 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1326 GOT_DIFF_CONFLICT_MARKER_SEP,
1327 GOT_DIFF_CONFLICT_MARKER_END
1329 int i = 0;
1330 char *line;
1331 size_t len;
1332 const char delim[3] = {'\0', '\0', '\0'};
1334 while (*status == GOT_STATUS_MODIFY) {
1335 line = fparseln(f, &len, NULL, delim, 0);
1336 if (line == NULL) {
1337 if (feof(f))
1338 break;
1339 err = got_ferror(f, GOT_ERR_IO);
1340 break;
1343 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1344 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1345 == 0)
1346 *status = GOT_STATUS_CONFLICT;
1347 else
1348 i++;
1352 return err;
1355 static int
1356 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1358 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1359 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1362 static int
1363 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1365 return !(ie->ctime_sec == sb->st_ctime &&
1366 ie->ctime_nsec == sb->st_ctimensec &&
1367 ie->mtime_sec == sb->st_mtime &&
1368 ie->mtime_nsec == sb->st_mtimensec &&
1369 ie->size == (sb->st_size & 0xffffffff) &&
1370 !xbit_differs(ie, sb->st_mode));
1373 static unsigned char
1374 get_staged_status(struct got_fileindex_entry *ie)
1376 switch (got_fileindex_entry_stage_get(ie)) {
1377 case GOT_FILEIDX_STAGE_ADD:
1378 return GOT_STATUS_ADD;
1379 case GOT_FILEIDX_STAGE_DELETE:
1380 return GOT_STATUS_DELETE;
1381 case GOT_FILEIDX_STAGE_MODIFY:
1382 return GOT_STATUS_MODIFY;
1383 default:
1384 return GOT_STATUS_NO_CHANGE;
1388 static const struct got_error *
1389 get_symlink_status(unsigned char *status, struct stat *sb,
1390 struct got_fileindex_entry *ie, const char *abspath,
1391 int dirfd, const char *de_name, struct got_blob_object *blob)
1393 const struct got_error *err = NULL;
1394 char target_path[PATH_MAX];
1395 char etarget[PATH_MAX];
1396 ssize_t elen;
1397 size_t len, target_len = 0;
1398 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1399 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1401 *status = GOT_STATUS_NO_CHANGE;
1403 /* Blob object content specifies the target path of the link. */
1404 do {
1405 err = got_object_blob_read_block(&len, blob);
1406 if (err)
1407 return err;
1408 if (len + target_len >= sizeof(target_path)) {
1410 * Should not happen. The blob contents were OK
1411 * when this symlink was installed.
1413 return got_error(GOT_ERR_NO_SPACE);
1415 if (len > 0) {
1416 /* Skip blob object header first time around. */
1417 memcpy(target_path + target_len, buf + hdrlen,
1418 len - hdrlen);
1419 target_len += len - hdrlen;
1420 hdrlen = 0;
1422 } while (len != 0);
1423 target_path[target_len] = '\0';
1425 if (dirfd != -1) {
1426 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1427 if (elen == -1)
1428 return got_error_from_errno2("readlinkat", abspath);
1429 } else {
1430 elen = readlink(abspath, etarget, sizeof(etarget));
1431 if (elen == -1)
1432 return got_error_from_errno2("readlinkat", abspath);
1435 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1436 *status = GOT_STATUS_MODIFY;
1438 return NULL;
1441 static const struct got_error *
1442 get_file_status(unsigned char *status, struct stat *sb,
1443 struct got_fileindex_entry *ie, const char *abspath,
1444 int dirfd, const char *de_name, struct got_repository *repo)
1446 const struct got_error *err = NULL;
1447 struct got_object_id id;
1448 size_t hdrlen;
1449 int fd = -1;
1450 FILE *f = NULL;
1451 uint8_t fbuf[8192];
1452 struct got_blob_object *blob = NULL;
1453 size_t flen, blen;
1454 unsigned char staged_status = get_staged_status(ie);
1456 *status = GOT_STATUS_NO_CHANGE;
1459 * Whenever the caller provides a directory descriptor and a
1460 * directory entry name for the file, use them! This prevents
1461 * race conditions if filesystem paths change beneath our feet.
1463 if (dirfd != -1) {
1464 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1465 if (errno == ENOENT) {
1466 if (got_fileindex_entry_has_file_on_disk(ie))
1467 *status = GOT_STATUS_MISSING;
1468 else
1469 *status = GOT_STATUS_DELETE;
1470 goto done;
1472 err = got_error_from_errno2("fstatat", abspath);
1473 goto done;
1475 } else {
1476 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1477 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1478 return got_error_from_errno2("open", abspath);
1479 else if (fd == -1 && errno == ELOOP) {
1480 if (lstat(abspath, sb) == -1)
1481 return got_error_from_errno2("lstat", abspath);
1482 } else if (fd == -1 || fstat(fd, sb) == -1) {
1483 if (errno == ENOENT) {
1484 if (got_fileindex_entry_has_file_on_disk(ie))
1485 *status = GOT_STATUS_MISSING;
1486 else
1487 *status = GOT_STATUS_DELETE;
1488 goto done;
1490 err = got_error_from_errno2("fstat", abspath);
1491 goto done;
1495 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1496 *status = GOT_STATUS_OBSTRUCTED;
1497 goto done;
1500 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1501 *status = GOT_STATUS_DELETE;
1502 goto done;
1503 } else if (!got_fileindex_entry_has_blob(ie) &&
1504 staged_status != GOT_STATUS_ADD) {
1505 *status = GOT_STATUS_ADD;
1506 goto done;
1509 if (!stat_info_differs(ie, sb))
1510 goto done;
1512 if (staged_status == GOT_STATUS_MODIFY ||
1513 staged_status == GOT_STATUS_ADD)
1514 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1515 else
1516 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1518 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1519 if (err)
1520 goto done;
1522 if (S_ISLNK(sb->st_mode)) {
1523 /* Staging changes to symlinks is not yet(?) supported. */
1524 if (staged_status != GOT_STATUS_NO_CHANGE) {
1525 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1526 goto done;
1528 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1529 de_name, blob);
1530 goto done;
1534 if (dirfd != -1) {
1535 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1536 if (fd == -1) {
1537 err = got_error_from_errno2("openat", abspath);
1538 goto done;
1542 f = fdopen(fd, "r");
1543 if (f == NULL) {
1544 err = got_error_from_errno2("fdopen", abspath);
1545 goto done;
1547 fd = -1;
1548 hdrlen = got_object_blob_get_hdrlen(blob);
1549 for (;;) {
1550 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1551 err = got_object_blob_read_block(&blen, blob);
1552 if (err)
1553 goto done;
1554 /* Skip length of blob object header first time around. */
1555 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1556 if (flen == 0 && ferror(f)) {
1557 err = got_error_from_errno("fread");
1558 goto done;
1560 if (blen == 0) {
1561 if (flen != 0)
1562 *status = GOT_STATUS_MODIFY;
1563 break;
1564 } else if (flen == 0) {
1565 if (blen != 0)
1566 *status = GOT_STATUS_MODIFY;
1567 break;
1568 } else if (blen - hdrlen == flen) {
1569 /* Skip blob object header first time around. */
1570 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1571 *status = GOT_STATUS_MODIFY;
1572 break;
1574 } else {
1575 *status = GOT_STATUS_MODIFY;
1576 break;
1578 hdrlen = 0;
1581 if (*status == GOT_STATUS_MODIFY) {
1582 rewind(f);
1583 err = get_modified_file_content_status(status, f);
1584 } else if (xbit_differs(ie, sb->st_mode))
1585 *status = GOT_STATUS_MODE_CHANGE;
1586 done:
1587 if (blob)
1588 got_object_blob_close(blob);
1589 if (f != NULL && fclose(f) == EOF && err == NULL)
1590 err = got_error_from_errno2("fclose", abspath);
1591 if (fd != -1 && close(fd) == -1 && err == NULL)
1592 err = got_error_from_errno2("close", abspath);
1593 return err;
1597 * Update timestamps in the file index if a file is unmodified and
1598 * we had to run a full content comparison to find out.
1600 static const struct got_error *
1601 sync_timestamps(char *ondisk_path, unsigned char status,
1602 struct got_fileindex_entry *ie, struct stat *sb)
1604 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1605 return got_fileindex_entry_update(ie, ondisk_path,
1606 ie->blob_sha1, ie->commit_sha1, 1);
1608 return NULL;
1611 static const struct got_error *
1612 update_blob(struct got_worktree *worktree,
1613 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1614 struct got_tree_entry *te, const char *path,
1615 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1616 void *progress_arg)
1618 const struct got_error *err = NULL;
1619 struct got_blob_object *blob = NULL;
1620 char *ondisk_path;
1621 unsigned char status = GOT_STATUS_NO_CHANGE;
1622 struct stat sb;
1624 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1625 return got_error_from_errno("asprintf");
1627 if (ie) {
1628 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1629 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1630 goto done;
1632 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1633 repo);
1634 if (err)
1635 goto done;
1636 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1637 sb.st_mode = got_fileindex_perms_to_st(ie);
1638 } else
1639 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1641 if (status == GOT_STATUS_OBSTRUCTED) {
1642 err = (*progress_cb)(progress_arg, status, path);
1643 goto done;
1645 if (status == GOT_STATUS_CONFLICT) {
1646 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1647 path);
1648 goto done;
1651 if (ie && status != GOT_STATUS_MISSING &&
1652 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1653 if (got_fileindex_entry_has_commit(ie) &&
1654 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1655 SHA1_DIGEST_LENGTH) == 0) {
1656 err = sync_timestamps(ondisk_path, status, ie, &sb);
1657 if (err)
1658 goto done;
1659 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1660 path);
1661 goto done;
1663 if (got_fileindex_entry_has_blob(ie) &&
1664 memcmp(ie->blob_sha1, te->id.sha1,
1665 SHA1_DIGEST_LENGTH) == 0) {
1666 err = sync_timestamps(ondisk_path, status, ie, &sb);
1667 goto done;
1671 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1672 if (err)
1673 goto done;
1675 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1676 int update_timestamps;
1677 struct got_blob_object *blob2 = NULL;
1678 char *label_orig = NULL;
1679 if (got_fileindex_entry_has_blob(ie)) {
1680 struct got_object_id id2;
1681 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1682 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1683 if (err)
1684 goto done;
1686 if (got_fileindex_entry_has_commit(ie)) {
1687 char id_str[SHA1_DIGEST_STRING_LENGTH];
1688 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1689 sizeof(id_str)) == NULL) {
1690 err = got_error_path(id_str,
1691 GOT_ERR_BAD_OBJ_ID_STR);
1692 goto done;
1694 if (asprintf(&label_orig, "%s: commit %s",
1695 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1696 err = got_error_from_errno("asprintf");
1697 goto done;
1700 err = merge_blob(&update_timestamps, worktree, blob2,
1701 ondisk_path, path, sb.st_mode, label_orig, blob,
1702 worktree->base_commit_id, repo,
1703 progress_cb, progress_arg);
1704 free(label_orig);
1705 if (blob2)
1706 got_object_blob_close(blob2);
1707 if (err)
1708 goto done;
1710 * Do not update timestamps of files with local changes.
1711 * Otherwise, a future status walk would treat them as
1712 * unmodified files again.
1714 err = got_fileindex_entry_update(ie, ondisk_path,
1715 blob->id.sha1, worktree->base_commit_id->sha1,
1716 update_timestamps);
1717 } else if (status == GOT_STATUS_MODE_CHANGE) {
1718 err = got_fileindex_entry_update(ie, ondisk_path,
1719 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1720 } else if (status == GOT_STATUS_DELETE) {
1721 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1722 if (err)
1723 goto done;
1724 err = got_fileindex_entry_update(ie, ondisk_path,
1725 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1726 if (err)
1727 goto done;
1728 } else {
1729 err = install_blob(worktree, ondisk_path, path, te->mode,
1730 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1731 repo, progress_cb, progress_arg);
1732 if (err)
1733 goto done;
1734 if (ie) {
1735 err = got_fileindex_entry_update(ie, ondisk_path,
1736 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1737 } else {
1738 err = create_fileindex_entry(fileindex,
1739 worktree->base_commit_id, ondisk_path, path,
1740 &blob->id);
1742 if (err)
1743 goto done;
1745 got_object_blob_close(blob);
1746 done:
1747 free(ondisk_path);
1748 return err;
1751 static const struct got_error *
1752 remove_ondisk_file(const char *root_path, const char *path)
1754 const struct got_error *err = NULL;
1755 char *ondisk_path = NULL;
1757 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1758 return got_error_from_errno("asprintf");
1760 if (unlink(ondisk_path) == -1) {
1761 if (errno != ENOENT)
1762 err = got_error_from_errno2("unlink", ondisk_path);
1763 } else {
1764 char *parent = dirname(ondisk_path);
1765 while (parent && strcmp(parent, root_path) != 0) {
1766 if (rmdir(parent) == -1) {
1767 if (errno != ENOTEMPTY)
1768 err = got_error_from_errno2("rmdir",
1769 parent);
1770 break;
1772 parent = dirname(parent);
1775 free(ondisk_path);
1776 return err;
1779 static const struct got_error *
1780 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1781 struct got_fileindex_entry *ie, struct got_repository *repo,
1782 got_worktree_checkout_cb progress_cb, void *progress_arg)
1784 const struct got_error *err = NULL;
1785 unsigned char status;
1786 struct stat sb;
1787 char *ondisk_path;
1789 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1790 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1792 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1793 == -1)
1794 return got_error_from_errno("asprintf");
1796 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1797 if (err)
1798 goto done;
1800 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1801 status == GOT_STATUS_ADD) {
1802 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1803 if (err)
1804 goto done;
1806 * Preserve the working file and change the deleted blob's
1807 * entry into a schedule-add entry.
1809 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1810 0);
1811 } else {
1812 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1813 if (err)
1814 goto done;
1815 if (status == GOT_STATUS_NO_CHANGE) {
1816 err = remove_ondisk_file(worktree->root_path, ie->path);
1817 if (err)
1818 goto done;
1820 got_fileindex_entry_remove(fileindex, ie);
1822 done:
1823 free(ondisk_path);
1824 return err;
1827 struct diff_cb_arg {
1828 struct got_fileindex *fileindex;
1829 struct got_worktree *worktree;
1830 struct got_repository *repo;
1831 got_worktree_checkout_cb progress_cb;
1832 void *progress_arg;
1833 got_cancel_cb cancel_cb;
1834 void *cancel_arg;
1837 static const struct got_error *
1838 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1839 struct got_tree_entry *te, const char *parent_path)
1841 struct diff_cb_arg *a = arg;
1843 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1844 return got_error(GOT_ERR_CANCELLED);
1846 return update_blob(a->worktree, a->fileindex, ie, te,
1847 ie->path, a->repo, a->progress_cb, a->progress_arg);
1850 static const struct got_error *
1851 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1853 struct diff_cb_arg *a = arg;
1855 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1856 return got_error(GOT_ERR_CANCELLED);
1858 return delete_blob(a->worktree, a->fileindex, ie,
1859 a->repo, a->progress_cb, a->progress_arg);
1862 static const struct got_error *
1863 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1865 struct diff_cb_arg *a = arg;
1866 const struct got_error *err;
1867 char *path;
1869 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1870 return got_error(GOT_ERR_CANCELLED);
1872 if (got_object_tree_entry_is_submodule(te))
1873 return NULL;
1875 if (asprintf(&path, "%s%s%s", parent_path,
1876 parent_path[0] ? "/" : "", te->name)
1877 == -1)
1878 return got_error_from_errno("asprintf");
1880 if (S_ISDIR(te->mode))
1881 err = add_dir_on_disk(a->worktree, path);
1882 else
1883 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1884 a->repo, a->progress_cb, a->progress_arg);
1886 free(path);
1887 return err;
1890 static const struct got_error *
1891 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1893 const struct got_error *err = NULL;
1894 char *uuidstr = NULL;
1895 uint32_t uuid_status;
1897 *refname = NULL;
1899 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1900 if (uuid_status != uuid_s_ok)
1901 return got_error_uuid(uuid_status, "uuid_to_string");
1903 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1904 == -1) {
1905 err = got_error_from_errno("asprintf");
1906 *refname = NULL;
1908 free(uuidstr);
1909 return err;
1912 const struct got_error *
1913 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1915 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1918 static const struct got_error *
1919 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1921 return get_ref_name(refname, worktree,
1922 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1925 static const struct got_error *
1926 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1928 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1931 static const struct got_error *
1932 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1934 return get_ref_name(refname, worktree,
1935 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1938 static const struct got_error *
1939 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1941 return get_ref_name(refname, worktree,
1942 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1945 static const struct got_error *
1946 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1948 return get_ref_name(refname, worktree,
1949 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1952 static const struct got_error *
1953 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1955 return get_ref_name(refname, worktree,
1956 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1959 static const struct got_error *
1960 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1962 return get_ref_name(refname, worktree,
1963 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1966 static const struct got_error *
1967 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1969 return get_ref_name(refname, worktree,
1970 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1973 const struct got_error *
1974 got_worktree_get_histedit_script_path(char **path,
1975 struct got_worktree *worktree)
1977 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1978 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1979 *path = NULL;
1980 return got_error_from_errno("asprintf");
1982 return NULL;
1986 * Prevent Git's garbage collector from deleting our base commit by
1987 * setting a reference to our base commit's ID.
1989 static const struct got_error *
1990 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1992 const struct got_error *err = NULL;
1993 struct got_reference *ref = NULL;
1994 char *refname;
1996 err = got_worktree_get_base_ref_name(&refname, worktree);
1997 if (err)
1998 return err;
2000 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2001 if (err)
2002 goto done;
2004 err = got_ref_write(ref, repo);
2005 done:
2006 free(refname);
2007 if (ref)
2008 got_ref_close(ref);
2009 return err;
2012 static const struct got_error *
2013 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2015 const struct got_error *err = NULL;
2017 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2018 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2019 err = got_error_from_errno("asprintf");
2020 *fileindex_path = NULL;
2022 return err;
2026 static const struct got_error *
2027 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2028 struct got_worktree *worktree)
2030 const struct got_error *err = NULL;
2031 FILE *index = NULL;
2033 *fileindex_path = NULL;
2034 *fileindex = got_fileindex_alloc();
2035 if (*fileindex == NULL)
2036 return got_error_from_errno("got_fileindex_alloc");
2038 err = get_fileindex_path(fileindex_path, worktree);
2039 if (err)
2040 goto done;
2042 index = fopen(*fileindex_path, "rb");
2043 if (index == NULL) {
2044 if (errno != ENOENT)
2045 err = got_error_from_errno2("fopen", *fileindex_path);
2046 } else {
2047 err = got_fileindex_read(*fileindex, index);
2048 if (fclose(index) != 0 && err == NULL)
2049 err = got_error_from_errno("fclose");
2051 done:
2052 if (err) {
2053 free(*fileindex_path);
2054 *fileindex_path = NULL;
2055 got_fileindex_free(*fileindex);
2056 *fileindex = NULL;
2058 return err;
2061 struct bump_base_commit_id_arg {
2062 struct got_object_id *base_commit_id;
2063 const char *path;
2064 size_t path_len;
2065 const char *entry_name;
2066 got_worktree_checkout_cb progress_cb;
2067 void *progress_arg;
2070 /* Bump base commit ID of all files within an updated part of the work tree. */
2071 static const struct got_error *
2072 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2074 const struct got_error *err;
2075 struct bump_base_commit_id_arg *a = arg;
2077 if (a->entry_name) {
2078 if (strcmp(ie->path, a->path) != 0)
2079 return NULL;
2080 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2081 return NULL;
2083 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2084 SHA1_DIGEST_LENGTH) == 0)
2085 return NULL;
2087 if (a->progress_cb) {
2088 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2089 ie->path);
2090 if (err)
2091 return err;
2093 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2094 return NULL;
2097 static const struct got_error *
2098 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2100 const struct got_error *err = NULL;
2101 char *new_fileindex_path = NULL;
2102 FILE *new_index = NULL;
2103 struct timespec timeout;
2105 err = got_opentemp_named(&new_fileindex_path, &new_index,
2106 fileindex_path);
2107 if (err)
2108 goto done;
2110 err = got_fileindex_write(fileindex, new_index);
2111 if (err)
2112 goto done;
2114 if (rename(new_fileindex_path, fileindex_path) != 0) {
2115 err = got_error_from_errno3("rename", new_fileindex_path,
2116 fileindex_path);
2117 unlink(new_fileindex_path);
2121 * Sleep for a short amount of time to ensure that files modified after
2122 * this program exits have a different time stamp from the one which
2123 * was recorded in the file index.
2125 timeout.tv_sec = 0;
2126 timeout.tv_nsec = 1;
2127 nanosleep(&timeout, NULL);
2128 done:
2129 if (new_index)
2130 fclose(new_index);
2131 free(new_fileindex_path);
2132 return err;
2135 static const struct got_error *
2136 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2137 struct got_object_id **tree_id, const char *wt_relpath,
2138 struct got_worktree *worktree, struct got_repository *repo)
2140 const struct got_error *err = NULL;
2141 struct got_object_id *id = NULL;
2142 char *in_repo_path = NULL;
2143 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2145 *entry_type = GOT_OBJ_TYPE_ANY;
2146 *tree_relpath = NULL;
2147 *tree_id = NULL;
2149 if (wt_relpath[0] == '\0') {
2150 /* Check out all files within the work tree. */
2151 *entry_type = GOT_OBJ_TYPE_TREE;
2152 *tree_relpath = strdup("");
2153 if (*tree_relpath == NULL) {
2154 err = got_error_from_errno("strdup");
2155 goto done;
2157 err = got_object_id_by_path(tree_id, repo,
2158 worktree->base_commit_id, worktree->path_prefix);
2159 if (err)
2160 goto done;
2161 return NULL;
2164 /* Check out a subset of files in the work tree. */
2166 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2167 is_root_wt ? "" : "/", wt_relpath) == -1) {
2168 err = got_error_from_errno("asprintf");
2169 goto done;
2172 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2173 in_repo_path);
2174 if (err)
2175 goto done;
2177 free(in_repo_path);
2178 in_repo_path = NULL;
2180 err = got_object_get_type(entry_type, repo, id);
2181 if (err)
2182 goto done;
2184 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2185 /* Check out a single file. */
2186 if (strchr(wt_relpath, '/') == NULL) {
2187 /* Check out a single file in work tree's root dir. */
2188 in_repo_path = strdup(worktree->path_prefix);
2189 if (in_repo_path == NULL) {
2190 err = got_error_from_errno("strdup");
2191 goto done;
2193 *tree_relpath = strdup("");
2194 if (*tree_relpath == NULL) {
2195 err = got_error_from_errno("strdup");
2196 goto done;
2198 } else {
2199 /* Check out a single file in a subdirectory. */
2200 err = got_path_dirname(tree_relpath, wt_relpath);
2201 if (err)
2202 return err;
2203 if (asprintf(&in_repo_path, "%s%s%s",
2204 worktree->path_prefix, is_root_wt ? "" : "/",
2205 *tree_relpath) == -1) {
2206 err = got_error_from_errno("asprintf");
2207 goto done;
2210 err = got_object_id_by_path(tree_id, repo,
2211 worktree->base_commit_id, in_repo_path);
2212 } else {
2213 /* Check out all files within a subdirectory. */
2214 *tree_id = got_object_id_dup(id);
2215 if (*tree_id == NULL) {
2216 err = got_error_from_errno("got_object_id_dup");
2217 goto done;
2219 *tree_relpath = strdup(wt_relpath);
2220 if (*tree_relpath == NULL) {
2221 err = got_error_from_errno("strdup");
2222 goto done;
2225 done:
2226 free(id);
2227 free(in_repo_path);
2228 if (err) {
2229 *entry_type = GOT_OBJ_TYPE_ANY;
2230 free(*tree_relpath);
2231 *tree_relpath = NULL;
2232 free(*tree_id);
2233 *tree_id = NULL;
2235 return err;
2238 static const struct got_error *
2239 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2240 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2241 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2242 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2244 const struct got_error *err = NULL;
2245 struct got_commit_object *commit = NULL;
2246 struct got_tree_object *tree = NULL;
2247 struct got_fileindex_diff_tree_cb diff_cb;
2248 struct diff_cb_arg arg;
2250 err = ref_base_commit(worktree, repo);
2251 if (err) {
2252 if (!(err->code == GOT_ERR_ERRNO &&
2253 (errno == EACCES || errno == EROFS)))
2254 goto done;
2255 err = (*progress_cb)(progress_arg,
2256 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2257 if (err)
2258 return err;
2261 err = got_object_open_as_commit(&commit, repo,
2262 worktree->base_commit_id);
2263 if (err)
2264 goto done;
2266 err = got_object_open_as_tree(&tree, repo, tree_id);
2267 if (err)
2268 goto done;
2270 if (entry_name &&
2271 got_object_tree_find_entry(tree, entry_name) == NULL) {
2272 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2273 goto done;
2276 diff_cb.diff_old_new = diff_old_new;
2277 diff_cb.diff_old = diff_old;
2278 diff_cb.diff_new = diff_new;
2279 arg.fileindex = fileindex;
2280 arg.worktree = worktree;
2281 arg.repo = repo;
2282 arg.progress_cb = progress_cb;
2283 arg.progress_arg = progress_arg;
2284 arg.cancel_cb = cancel_cb;
2285 arg.cancel_arg = cancel_arg;
2286 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2287 entry_name, repo, &diff_cb, &arg);
2288 done:
2289 if (tree)
2290 got_object_tree_close(tree);
2291 if (commit)
2292 got_object_commit_close(commit);
2293 return err;
2296 const struct got_error *
2297 got_worktree_checkout_files(struct got_worktree *worktree,
2298 struct got_pathlist_head *paths, struct got_repository *repo,
2299 got_worktree_checkout_cb progress_cb, void *progress_arg,
2300 got_cancel_cb cancel_cb, void *cancel_arg)
2302 const struct got_error *err = NULL, *sync_err, *unlockerr;
2303 struct got_commit_object *commit = NULL;
2304 struct got_tree_object *tree = NULL;
2305 struct got_fileindex *fileindex = NULL;
2306 char *fileindex_path = NULL;
2307 struct got_pathlist_entry *pe;
2308 struct tree_path_data {
2309 SIMPLEQ_ENTRY(tree_path_data) entry;
2310 struct got_object_id *tree_id;
2311 int entry_type;
2312 char *relpath;
2313 char *entry_name;
2314 } *tpd = NULL;
2315 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2317 SIMPLEQ_INIT(&tree_paths);
2319 err = lock_worktree(worktree, LOCK_EX);
2320 if (err)
2321 return err;
2323 /* Map all specified paths to in-repository trees. */
2324 TAILQ_FOREACH(pe, paths, entry) {
2325 tpd = malloc(sizeof(*tpd));
2326 if (tpd == NULL) {
2327 err = got_error_from_errno("malloc");
2328 goto done;
2331 err = find_tree_entry_for_checkout(&tpd->entry_type,
2332 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2333 if (err) {
2334 free(tpd);
2335 goto done;
2338 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2339 err = got_path_basename(&tpd->entry_name, pe->path);
2340 if (err) {
2341 free(tpd->relpath);
2342 free(tpd->tree_id);
2343 free(tpd);
2344 goto done;
2346 } else
2347 tpd->entry_name = NULL;
2349 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2353 * Read the file index.
2354 * Checking out files is supposed to be an idempotent operation.
2355 * If the on-disk file index is incomplete we will try to complete it.
2357 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2358 if (err)
2359 goto done;
2361 tpd = SIMPLEQ_FIRST(&tree_paths);
2362 TAILQ_FOREACH(pe, paths, entry) {
2363 struct bump_base_commit_id_arg bbc_arg;
2365 err = checkout_files(worktree, fileindex, tpd->relpath,
2366 tpd->tree_id, tpd->entry_name, repo,
2367 progress_cb, progress_arg, cancel_cb, cancel_arg);
2368 if (err)
2369 break;
2371 bbc_arg.base_commit_id = worktree->base_commit_id;
2372 bbc_arg.entry_name = tpd->entry_name;
2373 bbc_arg.path = pe->path;
2374 bbc_arg.path_len = pe->path_len;
2375 bbc_arg.progress_cb = progress_cb;
2376 bbc_arg.progress_arg = progress_arg;
2377 err = got_fileindex_for_each_entry_safe(fileindex,
2378 bump_base_commit_id, &bbc_arg);
2379 if (err)
2380 break;
2382 tpd = SIMPLEQ_NEXT(tpd, entry);
2384 sync_err = sync_fileindex(fileindex, fileindex_path);
2385 if (sync_err && err == NULL)
2386 err = sync_err;
2387 done:
2388 free(fileindex_path);
2389 if (tree)
2390 got_object_tree_close(tree);
2391 if (commit)
2392 got_object_commit_close(commit);
2393 if (fileindex)
2394 got_fileindex_free(fileindex);
2395 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2396 tpd = SIMPLEQ_FIRST(&tree_paths);
2397 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2398 free(tpd->relpath);
2399 free(tpd->tree_id);
2400 free(tpd);
2402 unlockerr = lock_worktree(worktree, LOCK_SH);
2403 if (unlockerr && err == NULL)
2404 err = unlockerr;
2405 return err;
2408 struct merge_file_cb_arg {
2409 struct got_worktree *worktree;
2410 struct got_fileindex *fileindex;
2411 got_worktree_checkout_cb progress_cb;
2412 void *progress_arg;
2413 got_cancel_cb cancel_cb;
2414 void *cancel_arg;
2415 const char *label_orig;
2416 struct got_object_id *commit_id2;
2419 static const struct got_error *
2420 merge_file_cb(void *arg, struct got_blob_object *blob1,
2421 struct got_blob_object *blob2, struct got_object_id *id1,
2422 struct got_object_id *id2, const char *path1, const char *path2,
2423 mode_t mode1, mode_t mode2, struct got_repository *repo)
2425 static const struct got_error *err = NULL;
2426 struct merge_file_cb_arg *a = arg;
2427 struct got_fileindex_entry *ie;
2428 char *ondisk_path = NULL;
2429 struct stat sb;
2430 unsigned char status;
2431 int local_changes_subsumed;
2433 if (blob1 && blob2) {
2434 ie = got_fileindex_entry_get(a->fileindex, path2,
2435 strlen(path2));
2436 if (ie == NULL)
2437 return (*a->progress_cb)(a->progress_arg,
2438 GOT_STATUS_MISSING, path2);
2440 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2441 path2) == -1)
2442 return got_error_from_errno("asprintf");
2444 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2445 repo);
2446 if (err)
2447 goto done;
2449 if (status == GOT_STATUS_DELETE) {
2450 err = (*a->progress_cb)(a->progress_arg,
2451 GOT_STATUS_MERGE, path2);
2452 goto done;
2454 if (status != GOT_STATUS_NO_CHANGE &&
2455 status != GOT_STATUS_MODIFY &&
2456 status != GOT_STATUS_CONFLICT &&
2457 status != GOT_STATUS_ADD) {
2458 err = (*a->progress_cb)(a->progress_arg, status, path2);
2459 goto done;
2462 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2463 err = merge_symlink(a->worktree, blob1,
2464 ondisk_path, path2, sb.st_mode, a->label_orig,
2465 blob2, a->commit_id2, repo, a->progress_cb,
2466 a->progress_arg);
2467 } else {
2468 err = merge_blob(&local_changes_subsumed, a->worktree,
2469 blob1, ondisk_path, path2, sb.st_mode,
2470 a->label_orig, blob2, a->commit_id2, repo,
2471 a->progress_cb, a->progress_arg);
2473 } else if (blob1) {
2474 ie = got_fileindex_entry_get(a->fileindex, path1,
2475 strlen(path1));
2476 if (ie == NULL)
2477 return (*a->progress_cb)(a->progress_arg,
2478 GOT_STATUS_MISSING, path1);
2480 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2481 path1) == -1)
2482 return got_error_from_errno("asprintf");
2484 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2485 repo);
2486 if (err)
2487 goto done;
2489 switch (status) {
2490 case GOT_STATUS_NO_CHANGE:
2491 err = (*a->progress_cb)(a->progress_arg,
2492 GOT_STATUS_DELETE, path1);
2493 if (err)
2494 goto done;
2495 err = remove_ondisk_file(a->worktree->root_path, path1);
2496 if (err)
2497 goto done;
2498 if (ie)
2499 got_fileindex_entry_mark_deleted_from_disk(ie);
2500 break;
2501 case GOT_STATUS_DELETE:
2502 case GOT_STATUS_MISSING:
2503 err = (*a->progress_cb)(a->progress_arg,
2504 GOT_STATUS_DELETE, path1);
2505 if (err)
2506 goto done;
2507 if (ie)
2508 got_fileindex_entry_mark_deleted_from_disk(ie);
2509 break;
2510 case GOT_STATUS_ADD:
2511 case GOT_STATUS_MODIFY:
2512 case GOT_STATUS_CONFLICT:
2513 err = (*a->progress_cb)(a->progress_arg,
2514 GOT_STATUS_CANNOT_DELETE, path1);
2515 if (err)
2516 goto done;
2517 break;
2518 case GOT_STATUS_OBSTRUCTED:
2519 err = (*a->progress_cb)(a->progress_arg, status, path1);
2520 if (err)
2521 goto done;
2522 break;
2523 default:
2524 break;
2526 } else if (blob2) {
2527 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2528 path2) == -1)
2529 return got_error_from_errno("asprintf");
2530 ie = got_fileindex_entry_get(a->fileindex, path2,
2531 strlen(path2));
2532 if (ie) {
2533 err = get_file_status(&status, &sb, ie, ondisk_path,
2534 -1, NULL, repo);
2535 if (err)
2536 goto done;
2537 if (status != GOT_STATUS_NO_CHANGE &&
2538 status != GOT_STATUS_MODIFY &&
2539 status != GOT_STATUS_CONFLICT &&
2540 status != GOT_STATUS_ADD) {
2541 err = (*a->progress_cb)(a->progress_arg,
2542 status, path2);
2543 goto done;
2545 err = merge_blob(&local_changes_subsumed, a->worktree,
2546 NULL, ondisk_path, path2, sb.st_mode,
2547 a->label_orig, blob2, a->commit_id2, repo,
2548 a->progress_cb,
2549 a->progress_arg);
2550 if (status == GOT_STATUS_DELETE) {
2551 err = got_fileindex_entry_update(ie,
2552 ondisk_path, blob2->id.sha1,
2553 a->worktree->base_commit_id->sha1, 0);
2554 if (err)
2555 goto done;
2557 } else {
2558 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2559 err = install_blob(a->worktree, ondisk_path, path2,
2560 /* XXX get this from parent tree! */
2561 GOT_DEFAULT_FILE_MODE,
2562 sb.st_mode, blob2, 0, 0, repo,
2563 a->progress_cb, a->progress_arg);
2564 if (err)
2565 goto done;
2566 err = got_fileindex_entry_alloc(&ie, path2);
2567 if (err)
2568 goto done;
2569 err = got_fileindex_entry_update(ie, ondisk_path,
2570 NULL, NULL, 1);
2571 if (err) {
2572 got_fileindex_entry_free(ie);
2573 goto done;
2575 err = got_fileindex_entry_add(a->fileindex, ie);
2576 if (err) {
2577 got_fileindex_entry_free(ie);
2578 goto done;
2582 done:
2583 free(ondisk_path);
2584 return err;
2587 struct check_merge_ok_arg {
2588 struct got_worktree *worktree;
2589 struct got_repository *repo;
2592 static const struct got_error *
2593 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2595 const struct got_error *err = NULL;
2596 struct check_merge_ok_arg *a = arg;
2597 unsigned char status;
2598 struct stat sb;
2599 char *ondisk_path;
2601 /* Reject merges into a work tree with mixed base commits. */
2602 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2603 SHA1_DIGEST_LENGTH))
2604 return got_error(GOT_ERR_MIXED_COMMITS);
2606 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2607 == -1)
2608 return got_error_from_errno("asprintf");
2610 /* Reject merges into a work tree with conflicted files. */
2611 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2612 if (err)
2613 return err;
2614 if (status == GOT_STATUS_CONFLICT)
2615 return got_error(GOT_ERR_CONFLICTS);
2617 return NULL;
2620 static const struct got_error *
2621 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2622 const char *fileindex_path, struct got_object_id *commit_id1,
2623 struct got_object_id *commit_id2, struct got_repository *repo,
2624 got_worktree_checkout_cb progress_cb, void *progress_arg,
2625 got_cancel_cb cancel_cb, void *cancel_arg)
2627 const struct got_error *err = NULL, *sync_err;
2628 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2629 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2630 struct merge_file_cb_arg arg;
2631 char *label_orig = NULL;
2633 if (commit_id1) {
2634 char *id_str;
2636 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2637 worktree->path_prefix);
2638 if (err)
2639 goto done;
2641 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2642 if (err)
2643 goto done;
2645 err = got_object_id_str(&id_str, commit_id1);
2646 if (err)
2647 goto done;
2649 if (asprintf(&label_orig, "%s: commit %s",
2650 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2651 err = got_error_from_errno("asprintf");
2652 free(id_str);
2653 goto done;
2655 free(id_str);
2658 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2659 worktree->path_prefix);
2660 if (err)
2661 goto done;
2663 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2664 if (err)
2665 goto done;
2667 arg.worktree = worktree;
2668 arg.fileindex = fileindex;
2669 arg.progress_cb = progress_cb;
2670 arg.progress_arg = progress_arg;
2671 arg.cancel_cb = cancel_cb;
2672 arg.cancel_arg = cancel_arg;
2673 arg.label_orig = label_orig;
2674 arg.commit_id2 = commit_id2;
2675 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2676 sync_err = sync_fileindex(fileindex, fileindex_path);
2677 if (sync_err && err == NULL)
2678 err = sync_err;
2679 done:
2680 if (tree1)
2681 got_object_tree_close(tree1);
2682 if (tree2)
2683 got_object_tree_close(tree2);
2684 free(label_orig);
2685 return err;
2688 const struct got_error *
2689 got_worktree_merge_files(struct got_worktree *worktree,
2690 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2691 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2692 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2694 const struct got_error *err, *unlockerr;
2695 char *fileindex_path = NULL;
2696 struct got_fileindex *fileindex = NULL;
2697 struct check_merge_ok_arg mok_arg;
2699 err = lock_worktree(worktree, LOCK_EX);
2700 if (err)
2701 return err;
2703 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2704 if (err)
2705 goto done;
2707 mok_arg.worktree = worktree;
2708 mok_arg.repo = repo;
2709 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2710 &mok_arg);
2711 if (err)
2712 goto done;
2714 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2715 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2716 done:
2717 if (fileindex)
2718 got_fileindex_free(fileindex);
2719 free(fileindex_path);
2720 unlockerr = lock_worktree(worktree, LOCK_SH);
2721 if (unlockerr && err == NULL)
2722 err = unlockerr;
2723 return err;
2726 struct diff_dir_cb_arg {
2727 struct got_fileindex *fileindex;
2728 struct got_worktree *worktree;
2729 const char *status_path;
2730 size_t status_path_len;
2731 struct got_repository *repo;
2732 got_worktree_status_cb status_cb;
2733 void *status_arg;
2734 got_cancel_cb cancel_cb;
2735 void *cancel_arg;
2736 /* A pathlist containing per-directory pathlists of ignore patterns. */
2737 struct got_pathlist_head ignores;
2738 int report_unchanged;
2739 int no_ignores;
2742 static const struct got_error *
2743 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2744 int dirfd, const char *de_name,
2745 got_worktree_status_cb status_cb, void *status_arg,
2746 struct got_repository *repo, int report_unchanged)
2748 const struct got_error *err = NULL;
2749 unsigned char status = GOT_STATUS_NO_CHANGE;
2750 unsigned char staged_status = get_staged_status(ie);
2751 struct stat sb;
2752 struct got_object_id blob_id, commit_id, staged_blob_id;
2753 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2754 struct got_object_id *staged_blob_idp = NULL;
2756 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2757 if (err)
2758 return err;
2760 if (status == GOT_STATUS_NO_CHANGE &&
2761 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2762 return NULL;
2764 if (got_fileindex_entry_has_blob(ie)) {
2765 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2766 blob_idp = &blob_id;
2768 if (got_fileindex_entry_has_commit(ie)) {
2769 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2770 commit_idp = &commit_id;
2772 if (staged_status == GOT_STATUS_ADD ||
2773 staged_status == GOT_STATUS_MODIFY) {
2774 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2775 SHA1_DIGEST_LENGTH);
2776 staged_blob_idp = &staged_blob_id;
2779 return (*status_cb)(status_arg, status, staged_status,
2780 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2783 static const struct got_error *
2784 status_old_new(void *arg, struct got_fileindex_entry *ie,
2785 struct dirent *de, const char *parent_path, int dirfd)
2787 const struct got_error *err = NULL;
2788 struct diff_dir_cb_arg *a = arg;
2789 char *abspath;
2791 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2792 return got_error(GOT_ERR_CANCELLED);
2794 if (got_path_cmp(parent_path, a->status_path,
2795 strlen(parent_path), a->status_path_len) != 0 &&
2796 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2797 return NULL;
2799 if (parent_path[0]) {
2800 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2801 parent_path, de->d_name) == -1)
2802 return got_error_from_errno("asprintf");
2803 } else {
2804 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2805 de->d_name) == -1)
2806 return got_error_from_errno("asprintf");
2809 err = report_file_status(ie, abspath, dirfd, de->d_name,
2810 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2811 free(abspath);
2812 return err;
2815 static const struct got_error *
2816 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2818 struct diff_dir_cb_arg *a = arg;
2819 struct got_object_id blob_id, commit_id;
2820 unsigned char status;
2822 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2823 return got_error(GOT_ERR_CANCELLED);
2825 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2826 return NULL;
2828 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2829 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2830 if (got_fileindex_entry_has_file_on_disk(ie))
2831 status = GOT_STATUS_MISSING;
2832 else
2833 status = GOT_STATUS_DELETE;
2834 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2835 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2838 void
2839 free_ignorelist(struct got_pathlist_head *ignorelist)
2841 struct got_pathlist_entry *pe;
2843 TAILQ_FOREACH(pe, ignorelist, entry)
2844 free((char *)pe->path);
2845 got_pathlist_free(ignorelist);
2848 void
2849 free_ignores(struct got_pathlist_head *ignores)
2851 struct got_pathlist_entry *pe;
2853 TAILQ_FOREACH(pe, ignores, entry) {
2854 struct got_pathlist_head *ignorelist = pe->data;
2855 free_ignorelist(ignorelist);
2856 free((char *)pe->path);
2858 got_pathlist_free(ignores);
2861 static const struct got_error *
2862 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2864 const struct got_error *err = NULL;
2865 struct got_pathlist_entry *pe = NULL;
2866 struct got_pathlist_head *ignorelist;
2867 char *line = NULL, *pattern, *dirpath = NULL;
2868 size_t linesize = 0;
2869 ssize_t linelen;
2871 ignorelist = calloc(1, sizeof(*ignorelist));
2872 if (ignorelist == NULL)
2873 return got_error_from_errno("calloc");
2874 TAILQ_INIT(ignorelist);
2876 while ((linelen = getline(&line, &linesize, f)) != -1) {
2877 if (linelen > 0 && line[linelen - 1] == '\n')
2878 line[linelen - 1] = '\0';
2880 /* Git's ignores may contain comments. */
2881 if (line[0] == '#')
2882 continue;
2884 /* Git's negated patterns are not (yet?) supported. */
2885 if (line[0] == '!')
2886 continue;
2888 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2889 line) == -1) {
2890 err = got_error_from_errno("asprintf");
2891 goto done;
2893 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2894 if (err)
2895 goto done;
2897 if (ferror(f)) {
2898 err = got_error_from_errno("getline");
2899 goto done;
2902 dirpath = strdup(path);
2903 if (dirpath == NULL) {
2904 err = got_error_from_errno("strdup");
2905 goto done;
2907 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2908 done:
2909 free(line);
2910 if (err || pe == NULL) {
2911 free(dirpath);
2912 free_ignorelist(ignorelist);
2914 return err;
2917 int
2918 match_ignores(struct got_pathlist_head *ignores, const char *path)
2920 struct got_pathlist_entry *pe;
2922 /* Handle patterns which match in all directories. */
2923 TAILQ_FOREACH(pe, ignores, entry) {
2924 struct got_pathlist_head *ignorelist = pe->data;
2925 struct got_pathlist_entry *pi;
2927 TAILQ_FOREACH(pi, ignorelist, entry) {
2928 const char *p, *pattern = pi->path;
2930 if (strncmp(pattern, "**/", 3) != 0)
2931 continue;
2932 pattern += 3;
2933 p = path;
2934 while (*p) {
2935 if (fnmatch(pattern, p,
2936 FNM_PATHNAME | FNM_LEADING_DIR)) {
2937 /* Retry in next directory. */
2938 while (*p && *p != '/')
2939 p++;
2940 while (*p == '/')
2941 p++;
2942 continue;
2944 return 1;
2950 * The ignores pathlist contains ignore lists from children before
2951 * parents, so we can find the most specific ignorelist by walking
2952 * ignores backwards.
2954 pe = TAILQ_LAST(ignores, got_pathlist_head);
2955 while (pe) {
2956 if (got_path_is_child(path, pe->path, pe->path_len)) {
2957 struct got_pathlist_head *ignorelist = pe->data;
2958 struct got_pathlist_entry *pi;
2959 TAILQ_FOREACH(pi, ignorelist, entry) {
2960 const char *pattern = pi->path;
2961 int flags = FNM_LEADING_DIR;
2962 if (strstr(pattern, "/**/") == NULL)
2963 flags |= FNM_PATHNAME;
2964 if (fnmatch(pattern, path, flags))
2965 continue;
2966 return 1;
2969 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2972 return 0;
2975 static const struct got_error *
2976 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2977 const char *path, int dirfd, const char *ignores_filename)
2979 const struct got_error *err = NULL;
2980 char *ignorespath;
2981 int fd = -1;
2982 FILE *ignoresfile = NULL;
2984 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2985 path[0] ? "/" : "", ignores_filename) == -1)
2986 return got_error_from_errno("asprintf");
2988 if (dirfd != -1) {
2989 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2990 if (fd == -1) {
2991 if (errno != ENOENT && errno != EACCES)
2992 err = got_error_from_errno2("openat",
2993 ignorespath);
2994 } else {
2995 ignoresfile = fdopen(fd, "r");
2996 if (ignoresfile == NULL)
2997 err = got_error_from_errno2("fdopen",
2998 ignorespath);
2999 else {
3000 fd = -1;
3001 err = read_ignores(ignores, path, ignoresfile);
3004 } else {
3005 ignoresfile = fopen(ignorespath, "r");
3006 if (ignoresfile == NULL) {
3007 if (errno != ENOENT && errno != EACCES)
3008 err = got_error_from_errno2("fopen",
3009 ignorespath);
3010 } else
3011 err = read_ignores(ignores, path, ignoresfile);
3014 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3015 err = got_error_from_errno2("fclose", path);
3016 if (fd != -1 && close(fd) == -1 && err == NULL)
3017 err = got_error_from_errno2("close", path);
3018 free(ignorespath);
3019 return err;
3022 static const struct got_error *
3023 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3025 const struct got_error *err = NULL;
3026 struct diff_dir_cb_arg *a = arg;
3027 char *path = NULL;
3029 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3030 return got_error(GOT_ERR_CANCELLED);
3032 if (parent_path[0]) {
3033 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3034 return got_error_from_errno("asprintf");
3035 } else {
3036 path = de->d_name;
3039 if (de->d_type != DT_DIR &&
3040 got_path_is_child(path, a->status_path, a->status_path_len)
3041 && !match_ignores(&a->ignores, path))
3042 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3043 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3044 if (parent_path[0])
3045 free(path);
3046 return err;
3049 static const struct got_error *
3050 status_traverse(void *arg, const char *path, int dirfd)
3052 const struct got_error *err = NULL;
3053 struct diff_dir_cb_arg *a = arg;
3055 if (a->no_ignores)
3056 return NULL;
3058 err = add_ignores(&a->ignores, a->worktree->root_path,
3059 path, dirfd, ".cvsignore");
3060 if (err)
3061 return err;
3063 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3064 dirfd, ".gitignore");
3066 return err;
3069 static const struct got_error *
3070 report_single_file_status(const char *path, const char *ondisk_path,
3071 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3072 void *status_arg, struct got_repository *repo, int report_unchanged)
3074 struct got_fileindex_entry *ie;
3075 struct stat sb;
3077 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3078 if (ie)
3079 return report_file_status(ie, ondisk_path, -1, NULL,
3080 status_cb, status_arg, repo, report_unchanged);
3082 if (lstat(ondisk_path, &sb) == -1) {
3083 if (errno != ENOENT)
3084 return got_error_from_errno2("lstat", ondisk_path);
3085 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3086 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3087 return NULL;
3090 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3091 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3092 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3094 return NULL;
3097 static const struct got_error *
3098 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3099 const char *root_path, const char *path)
3101 const struct got_error *err;
3102 char *parent_path, *next_parent_path = NULL;
3104 err = add_ignores(ignores, root_path, "", -1,
3105 ".cvsignore");
3106 if (err)
3107 return err;
3109 err = add_ignores(ignores, root_path, "", -1,
3110 ".gitignore");
3111 if (err)
3112 return err;
3114 err = got_path_dirname(&parent_path, path);
3115 if (err) {
3116 if (err->code == GOT_ERR_BAD_PATH)
3117 return NULL; /* cannot traverse parent */
3118 return err;
3120 for (;;) {
3121 err = add_ignores(ignores, root_path, parent_path, -1,
3122 ".cvsignore");
3123 if (err)
3124 break;
3125 err = add_ignores(ignores, root_path, parent_path, -1,
3126 ".gitignore");
3127 if (err)
3128 break;
3129 err = got_path_dirname(&next_parent_path, parent_path);
3130 if (err) {
3131 if (err->code == GOT_ERR_BAD_PATH)
3132 err = NULL; /* traversed everything */
3133 break;
3135 free(parent_path);
3136 parent_path = next_parent_path;
3137 next_parent_path = NULL;
3140 free(parent_path);
3141 free(next_parent_path);
3142 return err;
3145 static const struct got_error *
3146 worktree_status(struct got_worktree *worktree, const char *path,
3147 struct got_fileindex *fileindex, struct got_repository *repo,
3148 got_worktree_status_cb status_cb, void *status_arg,
3149 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3150 int report_unchanged)
3152 const struct got_error *err = NULL;
3153 int fd = -1;
3154 struct got_fileindex_diff_dir_cb fdiff_cb;
3155 struct diff_dir_cb_arg arg;
3156 char *ondisk_path = NULL;
3158 TAILQ_INIT(&arg.ignores);
3160 if (asprintf(&ondisk_path, "%s%s%s",
3161 worktree->root_path, path[0] ? "/" : "", path) == -1)
3162 return got_error_from_errno("asprintf");
3164 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3165 if (fd == -1) {
3166 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3167 errno != ELOOP)
3168 err = got_error_from_errno2("open", ondisk_path);
3169 else
3170 err = report_single_file_status(path, ondisk_path,
3171 fileindex, status_cb, status_arg, repo,
3172 report_unchanged);
3173 } else {
3174 fdiff_cb.diff_old_new = status_old_new;
3175 fdiff_cb.diff_old = status_old;
3176 fdiff_cb.diff_new = status_new;
3177 fdiff_cb.diff_traverse = status_traverse;
3178 arg.fileindex = fileindex;
3179 arg.worktree = worktree;
3180 arg.status_path = path;
3181 arg.status_path_len = strlen(path);
3182 arg.repo = repo;
3183 arg.status_cb = status_cb;
3184 arg.status_arg = status_arg;
3185 arg.cancel_cb = cancel_cb;
3186 arg.cancel_arg = cancel_arg;
3187 arg.report_unchanged = report_unchanged;
3188 arg.no_ignores = no_ignores;
3189 if (!no_ignores) {
3190 err = add_ignores_from_parent_paths(&arg.ignores,
3191 worktree->root_path, path);
3192 if (err)
3193 goto done;
3195 err = got_fileindex_diff_dir(fileindex, fd,
3196 worktree->root_path, path, repo, &fdiff_cb, &arg);
3198 done:
3199 free_ignores(&arg.ignores);
3200 if (fd != -1 && close(fd) != 0 && err == NULL)
3201 err = got_error_from_errno("close");
3202 free(ondisk_path);
3203 return err;
3206 const struct got_error *
3207 got_worktree_status(struct got_worktree *worktree,
3208 struct got_pathlist_head *paths, struct got_repository *repo,
3209 got_worktree_status_cb status_cb, void *status_arg,
3210 got_cancel_cb cancel_cb, void *cancel_arg)
3212 const struct got_error *err = NULL;
3213 char *fileindex_path = NULL;
3214 struct got_fileindex *fileindex = NULL;
3215 struct got_pathlist_entry *pe;
3217 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3218 if (err)
3219 return err;
3221 TAILQ_FOREACH(pe, paths, entry) {
3222 err = worktree_status(worktree, pe->path, fileindex, repo,
3223 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3224 if (err)
3225 break;
3227 free(fileindex_path);
3228 got_fileindex_free(fileindex);
3229 return err;
3232 const struct got_error *
3233 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3234 const char *arg)
3236 const struct got_error *err = NULL;
3237 char *resolved = NULL, *cwd = NULL, *path = NULL;
3238 size_t len;
3239 struct stat sb;
3241 *wt_path = NULL;
3243 cwd = getcwd(NULL, 0);
3244 if (cwd == NULL)
3245 return got_error_from_errno("getcwd");
3247 if (lstat(arg, &sb) == -1) {
3248 if (errno != ENOENT) {
3249 err = got_error_from_errno2("lstat", arg);
3250 goto done;
3253 if (S_ISLNK(sb.st_mode)) {
3255 * We cannot use realpath(3) with symlinks since we want to
3256 * operate on the symlink itself.
3257 * But we can make the path absolute, assuming it is relative
3258 * to the current working directory, and then canonicalize it.
3260 char *abspath = NULL;
3261 char canonpath[PATH_MAX];
3262 if (!got_path_is_absolute(arg)) {
3263 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3264 err = got_error_from_errno("asprintf");
3265 goto done;
3269 err = got_canonpath(abspath ? abspath : arg, canonpath,
3270 sizeof(canonpath));
3271 if (err)
3272 goto done;
3273 resolved = strdup(canonpath);
3274 if (resolved == NULL) {
3275 err = got_error_from_errno("strdup");
3276 goto done;
3278 } else {
3279 resolved = realpath(arg, NULL);
3280 if (resolved == NULL) {
3281 if (errno != ENOENT) {
3282 err = got_error_from_errno2("realpath", arg);
3283 goto done;
3285 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3286 err = got_error_from_errno("asprintf");
3287 goto done;
3292 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3293 strlen(got_worktree_get_root_path(worktree)))) {
3294 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3295 goto done;
3298 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3299 err = got_path_skip_common_ancestor(&path,
3300 got_worktree_get_root_path(worktree), resolved);
3301 if (err)
3302 goto done;
3303 } else {
3304 path = strdup("");
3305 if (path == NULL) {
3306 err = got_error_from_errno("strdup");
3307 goto done;
3311 /* XXX status walk can't deal with trailing slash! */
3312 len = strlen(path);
3313 while (len > 0 && path[len - 1] == '/') {
3314 path[len - 1] = '\0';
3315 len--;
3317 done:
3318 free(resolved);
3319 free(cwd);
3320 if (err == NULL)
3321 *wt_path = path;
3322 else
3323 free(path);
3324 return err;
3327 struct schedule_addition_args {
3328 struct got_worktree *worktree;
3329 struct got_fileindex *fileindex;
3330 got_worktree_checkout_cb progress_cb;
3331 void *progress_arg;
3332 struct got_repository *repo;
3335 static const struct got_error *
3336 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3337 const char *relpath, struct got_object_id *blob_id,
3338 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3339 int dirfd, const char *de_name)
3341 struct schedule_addition_args *a = arg;
3342 const struct got_error *err = NULL;
3343 struct got_fileindex_entry *ie;
3344 struct stat sb;
3345 char *ondisk_path;
3347 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3348 relpath) == -1)
3349 return got_error_from_errno("asprintf");
3351 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3352 if (ie) {
3353 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3354 de_name, a->repo);
3355 if (err)
3356 goto done;
3357 /* Re-adding an existing entry is a no-op. */
3358 if (status == GOT_STATUS_ADD)
3359 goto done;
3360 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3361 if (err)
3362 goto done;
3365 if (status != GOT_STATUS_UNVERSIONED) {
3366 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3367 goto done;
3370 err = got_fileindex_entry_alloc(&ie, relpath);
3371 if (err)
3372 goto done;
3373 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3374 if (err) {
3375 got_fileindex_entry_free(ie);
3376 goto done;
3378 err = got_fileindex_entry_add(a->fileindex, ie);
3379 if (err) {
3380 got_fileindex_entry_free(ie);
3381 goto done;
3383 done:
3384 free(ondisk_path);
3385 if (err)
3386 return err;
3387 if (status == GOT_STATUS_ADD)
3388 return NULL;
3389 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3392 const struct got_error *
3393 got_worktree_schedule_add(struct got_worktree *worktree,
3394 struct got_pathlist_head *paths,
3395 got_worktree_checkout_cb progress_cb, void *progress_arg,
3396 struct got_repository *repo, int no_ignores)
3398 struct got_fileindex *fileindex = NULL;
3399 char *fileindex_path = NULL;
3400 const struct got_error *err = NULL, *sync_err, *unlockerr;
3401 struct got_pathlist_entry *pe;
3402 struct schedule_addition_args saa;
3404 err = lock_worktree(worktree, LOCK_EX);
3405 if (err)
3406 return err;
3408 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3409 if (err)
3410 goto done;
3412 saa.worktree = worktree;
3413 saa.fileindex = fileindex;
3414 saa.progress_cb = progress_cb;
3415 saa.progress_arg = progress_arg;
3416 saa.repo = repo;
3418 TAILQ_FOREACH(pe, paths, entry) {
3419 err = worktree_status(worktree, pe->path, fileindex, repo,
3420 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3421 if (err)
3422 break;
3424 sync_err = sync_fileindex(fileindex, fileindex_path);
3425 if (sync_err && err == NULL)
3426 err = sync_err;
3427 done:
3428 free(fileindex_path);
3429 if (fileindex)
3430 got_fileindex_free(fileindex);
3431 unlockerr = lock_worktree(worktree, LOCK_SH);
3432 if (unlockerr && err == NULL)
3433 err = unlockerr;
3434 return err;
3437 struct schedule_deletion_args {
3438 struct got_worktree *worktree;
3439 struct got_fileindex *fileindex;
3440 got_worktree_delete_cb progress_cb;
3441 void *progress_arg;
3442 struct got_repository *repo;
3443 int delete_local_mods;
3444 int keep_on_disk;
3447 static const struct got_error *
3448 schedule_for_deletion(void *arg, unsigned char status,
3449 unsigned char staged_status, const char *relpath,
3450 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3451 struct got_object_id *commit_id, int dirfd, const char *de_name)
3453 struct schedule_deletion_args *a = arg;
3454 const struct got_error *err = NULL;
3455 struct got_fileindex_entry *ie = NULL;
3456 struct stat sb;
3457 char *ondisk_path, *parent = NULL;
3459 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3460 if (ie == NULL)
3461 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3463 staged_status = get_staged_status(ie);
3464 if (staged_status != GOT_STATUS_NO_CHANGE) {
3465 if (staged_status == GOT_STATUS_DELETE)
3466 return NULL;
3467 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3470 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3471 relpath) == -1)
3472 return got_error_from_errno("asprintf");
3474 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3475 a->repo);
3476 if (err)
3477 goto done;
3479 if (status != GOT_STATUS_NO_CHANGE) {
3480 if (status == GOT_STATUS_DELETE)
3481 goto done;
3482 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3483 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3484 goto done;
3486 if (status != GOT_STATUS_MODIFY &&
3487 status != GOT_STATUS_MISSING) {
3488 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3489 goto done;
3493 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3494 if (dirfd != -1) {
3495 if (unlinkat(dirfd, de_name, 0) != 0) {
3496 err = got_error_from_errno2("unlinkat",
3497 ondisk_path);
3498 goto done;
3500 } else if (unlink(ondisk_path) != 0) {
3501 err = got_error_from_errno2("unlink", ondisk_path);
3502 goto done;
3505 parent = dirname(ondisk_path);
3507 if (parent == NULL) {
3508 err = got_error_from_errno2("dirname", ondisk_path);
3509 goto done;
3511 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3512 if (rmdir(parent) == -1) {
3513 if (errno != ENOTEMPTY)
3514 err = got_error_from_errno2("rmdir",
3515 parent);
3516 break;
3518 parent = dirname(parent);
3519 if (parent == NULL) {
3520 err = got_error_from_errno2("dirname", parent);
3521 goto done;
3526 got_fileindex_entry_mark_deleted_from_disk(ie);
3527 done:
3528 free(ondisk_path);
3529 if (err)
3530 return err;
3531 if (status == GOT_STATUS_DELETE)
3532 return NULL;
3533 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3534 staged_status, relpath);
3537 const struct got_error *
3538 got_worktree_schedule_delete(struct got_worktree *worktree,
3539 struct got_pathlist_head *paths, int delete_local_mods,
3540 got_worktree_delete_cb progress_cb, void *progress_arg,
3541 struct got_repository *repo, int keep_on_disk)
3543 struct got_fileindex *fileindex = NULL;
3544 char *fileindex_path = NULL;
3545 const struct got_error *err = NULL, *sync_err, *unlockerr;
3546 struct got_pathlist_entry *pe;
3547 struct schedule_deletion_args sda;
3549 err = lock_worktree(worktree, LOCK_EX);
3550 if (err)
3551 return err;
3553 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3554 if (err)
3555 goto done;
3557 sda.worktree = worktree;
3558 sda.fileindex = fileindex;
3559 sda.progress_cb = progress_cb;
3560 sda.progress_arg = progress_arg;
3561 sda.repo = repo;
3562 sda.delete_local_mods = delete_local_mods;
3563 sda.keep_on_disk = keep_on_disk;
3565 TAILQ_FOREACH(pe, paths, entry) {
3566 err = worktree_status(worktree, pe->path, fileindex, repo,
3567 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3568 if (err)
3569 break;
3571 sync_err = sync_fileindex(fileindex, fileindex_path);
3572 if (sync_err && err == NULL)
3573 err = sync_err;
3574 done:
3575 free(fileindex_path);
3576 if (fileindex)
3577 got_fileindex_free(fileindex);
3578 unlockerr = lock_worktree(worktree, LOCK_SH);
3579 if (unlockerr && err == NULL)
3580 err = unlockerr;
3581 return err;
3584 static const struct got_error *
3585 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3587 const struct got_error *err = NULL;
3588 char *line = NULL;
3589 size_t linesize = 0, n;
3590 ssize_t linelen;
3592 linelen = getline(&line, &linesize, infile);
3593 if (linelen == -1) {
3594 if (ferror(infile)) {
3595 err = got_error_from_errno("getline");
3596 goto done;
3598 return NULL;
3600 if (outfile) {
3601 n = fwrite(line, 1, linelen, outfile);
3602 if (n != linelen) {
3603 err = got_ferror(outfile, GOT_ERR_IO);
3604 goto done;
3607 if (rejectfile) {
3608 n = fwrite(line, 1, linelen, rejectfile);
3609 if (n != linelen)
3610 err = got_ferror(outfile, GOT_ERR_IO);
3612 done:
3613 free(line);
3614 return err;
3617 static const struct got_error *
3618 skip_one_line(FILE *f)
3620 char *line = NULL;
3621 size_t linesize = 0;
3622 ssize_t linelen;
3624 linelen = getline(&line, &linesize, f);
3625 if (linelen == -1) {
3626 if (ferror(f))
3627 return got_error_from_errno("getline");
3628 return NULL;
3630 free(line);
3631 return NULL;
3634 static const struct got_error *
3635 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3636 int start_old, int end_old, int start_new, int end_new,
3637 FILE *outfile, FILE *rejectfile)
3639 const struct got_error *err;
3641 /* Copy old file's lines leading up to patch. */
3642 while (!feof(f1) && *line_cur1 < start_old) {
3643 err = copy_one_line(f1, outfile, NULL);
3644 if (err)
3645 return err;
3646 (*line_cur1)++;
3648 /* Skip new file's lines leading up to patch. */
3649 while (!feof(f2) && *line_cur2 < start_new) {
3650 if (rejectfile)
3651 err = copy_one_line(f2, NULL, rejectfile);
3652 else
3653 err = skip_one_line(f2);
3654 if (err)
3655 return err;
3656 (*line_cur2)++;
3658 /* Copy patched lines. */
3659 while (!feof(f2) && *line_cur2 <= end_new) {
3660 err = copy_one_line(f2, outfile, NULL);
3661 if (err)
3662 return err;
3663 (*line_cur2)++;
3665 /* Skip over old file's replaced lines. */
3666 while (!feof(f1) && *line_cur1 <= end_old) {
3667 if (rejectfile)
3668 err = copy_one_line(f1, NULL, rejectfile);
3669 else
3670 err = skip_one_line(f1);
3671 if (err)
3672 return err;
3673 (*line_cur1)++;
3676 return NULL;
3679 static const struct got_error *
3680 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3681 FILE *outfile, FILE *rejectfile)
3683 const struct got_error *err;
3685 if (outfile) {
3686 /* Copy old file's lines until EOF. */
3687 while (!feof(f1)) {
3688 err = copy_one_line(f1, outfile, NULL);
3689 if (err)
3690 return err;
3691 (*line_cur1)++;
3694 if (rejectfile) {
3695 /* Copy new file's lines until EOF. */
3696 while (!feof(f2)) {
3697 err = copy_one_line(f2, NULL, rejectfile);
3698 if (err)
3699 return err;
3700 (*line_cur2)++;
3704 return NULL;
3707 static const struct got_error *
3708 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3709 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3710 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3711 int *line_cur2, FILE *outfile, FILE *rejectfile,
3712 got_worktree_patch_cb patch_cb, void *patch_arg)
3714 const struct got_error *err = NULL;
3715 int start_old = change->cv.a;
3716 int end_old = change->cv.b;
3717 int start_new = change->cv.c;
3718 int end_new = change->cv.d;
3719 long pos1, pos2;
3720 FILE *hunkfile;
3722 *choice = GOT_PATCH_CHOICE_NONE;
3724 hunkfile = got_opentemp();
3725 if (hunkfile == NULL)
3726 return got_error_from_errno("got_opentemp");
3728 pos1 = ftell(f1);
3729 pos2 = ftell(f2);
3731 /* XXX TODO needs error checking */
3732 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3734 if (fseek(f1, pos1, SEEK_SET) == -1) {
3735 err = got_ferror(f1, GOT_ERR_IO);
3736 goto done;
3738 if (fseek(f2, pos2, SEEK_SET) == -1) {
3739 err = got_ferror(f1, GOT_ERR_IO);
3740 goto done;
3742 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3743 err = got_ferror(hunkfile, GOT_ERR_IO);
3744 goto done;
3747 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3748 hunkfile, n, nchanges);
3749 if (err)
3750 goto done;
3752 switch (*choice) {
3753 case GOT_PATCH_CHOICE_YES:
3754 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3755 end_old, start_new, end_new, outfile, rejectfile);
3756 break;
3757 case GOT_PATCH_CHOICE_NO:
3758 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3759 end_old, start_new, end_new, rejectfile, outfile);
3760 break;
3761 case GOT_PATCH_CHOICE_QUIT:
3762 break;
3763 default:
3764 err = got_error(GOT_ERR_PATCH_CHOICE);
3765 break;
3767 done:
3768 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3769 err = got_error_from_errno("fclose");
3770 return err;
3773 struct revert_file_args {
3774 struct got_worktree *worktree;
3775 struct got_fileindex *fileindex;
3776 got_worktree_checkout_cb progress_cb;
3777 void *progress_arg;
3778 got_worktree_patch_cb patch_cb;
3779 void *patch_arg;
3780 struct got_repository *repo;
3783 static const struct got_error *
3784 create_patched_content(char **path_outfile, int reverse_patch,
3785 struct got_object_id *blob_id, const char *path2,
3786 int dirfd2, const char *de_name2,
3787 const char *relpath, struct got_repository *repo,
3788 got_worktree_patch_cb patch_cb, void *patch_arg)
3790 const struct got_error *err;
3791 struct got_blob_object *blob = NULL;
3792 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3793 int fd2 = -1;
3794 char *path1 = NULL, *id_str = NULL;
3795 struct stat sb1, sb2;
3796 struct got_diff_changes *changes = NULL;
3797 struct got_diff_state *ds = NULL;
3798 struct got_diff_args *args = NULL;
3799 struct got_diff_change *change;
3800 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3801 int n = 0;
3803 *path_outfile = NULL;
3805 err = got_object_id_str(&id_str, blob_id);
3806 if (err)
3807 return err;
3809 if (dirfd2 != -1) {
3810 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3811 if (fd2 == -1) {
3812 err = got_error_from_errno2("openat", path2);
3813 goto done;
3815 } else {
3816 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3817 if (fd2 == -1) {
3818 err = got_error_from_errno2("open", path2);
3819 goto done;
3822 if (fstat(fd2, &sb2) == -1) {
3823 err = got_error_from_errno2("fstat", path2);
3824 goto done;
3827 f2 = fdopen(fd2, "r");
3828 if (f2 == NULL) {
3829 err = got_error_from_errno2("fdopen", path2);
3830 goto done;
3832 fd2 = -1;
3834 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3835 if (err)
3836 goto done;
3838 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3839 if (err)
3840 goto done;
3842 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3843 if (err)
3844 goto done;
3846 if (stat(path1, &sb1) == -1) {
3847 err = got_error_from_errno2("stat", path1);
3848 goto done;
3851 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3852 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3853 if (err)
3854 goto done;
3856 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3857 if (err)
3858 goto done;
3860 if (fseek(f1, 0L, SEEK_SET) == -1)
3861 return got_ferror(f1, GOT_ERR_IO);
3862 if (fseek(f2, 0L, SEEK_SET) == -1)
3863 return got_ferror(f2, GOT_ERR_IO);
3864 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3865 int choice;
3866 err = apply_or_reject_change(&choice, change, ++n,
3867 changes->nchanges, ds, args, diff_flags, relpath,
3868 f1, f2, &line_cur1, &line_cur2,
3869 reverse_patch ? NULL : outfile,
3870 reverse_patch ? outfile : NULL,
3871 patch_cb, patch_arg);
3872 if (err)
3873 goto done;
3874 if (choice == GOT_PATCH_CHOICE_YES)
3875 have_content = 1;
3876 else if (choice == GOT_PATCH_CHOICE_QUIT)
3877 break;
3879 if (have_content) {
3880 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3881 reverse_patch ? NULL : outfile,
3882 reverse_patch ? outfile : NULL);
3883 if (err)
3884 goto done;
3886 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3887 err = got_error_from_errno2("chmod", path2);
3888 goto done;
3891 done:
3892 free(id_str);
3893 if (blob)
3894 got_object_blob_close(blob);
3895 if (f1 && fclose(f1) == EOF && err == NULL)
3896 err = got_error_from_errno2("fclose", path1);
3897 if (f2 && fclose(f2) == EOF && err == NULL)
3898 err = got_error_from_errno2("fclose", path2);
3899 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3900 err = got_error_from_errno2("close", path2);
3901 if (outfile && fclose(outfile) == EOF && err == NULL)
3902 err = got_error_from_errno2("fclose", *path_outfile);
3903 if (path1 && unlink(path1) == -1 && err == NULL)
3904 err = got_error_from_errno2("unlink", path1);
3905 if (err || !have_content) {
3906 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3907 err = got_error_from_errno2("unlink", *path_outfile);
3908 free(*path_outfile);
3909 *path_outfile = NULL;
3911 free(args);
3912 if (ds) {
3913 got_diff_state_free(ds);
3914 free(ds);
3916 if (changes)
3917 got_diff_free_changes(changes);
3918 free(path1);
3919 return err;
3922 static const struct got_error *
3923 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3924 const char *relpath, struct got_object_id *blob_id,
3925 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3926 int dirfd, const char *de_name)
3928 struct revert_file_args *a = arg;
3929 const struct got_error *err = NULL;
3930 char *parent_path = NULL;
3931 struct got_fileindex_entry *ie;
3932 struct got_tree_object *tree = NULL;
3933 struct got_object_id *tree_id = NULL;
3934 const struct got_tree_entry *te = NULL;
3935 char *tree_path = NULL, *te_name;
3936 char *ondisk_path = NULL, *path_content = NULL;
3937 struct got_blob_object *blob = NULL;
3939 /* Reverting a staged deletion is a no-op. */
3940 if (status == GOT_STATUS_DELETE &&
3941 staged_status != GOT_STATUS_NO_CHANGE)
3942 return NULL;
3944 if (status == GOT_STATUS_UNVERSIONED)
3945 return (*a->progress_cb)(a->progress_arg,
3946 GOT_STATUS_UNVERSIONED, relpath);
3948 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3949 if (ie == NULL)
3950 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3952 /* Construct in-repository path of tree which contains this blob. */
3953 err = got_path_dirname(&parent_path, ie->path);
3954 if (err) {
3955 if (err->code != GOT_ERR_BAD_PATH)
3956 goto done;
3957 parent_path = strdup("/");
3958 if (parent_path == NULL) {
3959 err = got_error_from_errno("strdup");
3960 goto done;
3963 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3964 tree_path = strdup(parent_path);
3965 if (tree_path == NULL) {
3966 err = got_error_from_errno("strdup");
3967 goto done;
3969 } else {
3970 if (got_path_is_root_dir(parent_path)) {
3971 tree_path = strdup(a->worktree->path_prefix);
3972 if (tree_path == NULL) {
3973 err = got_error_from_errno("strdup");
3974 goto done;
3976 } else {
3977 if (asprintf(&tree_path, "%s/%s",
3978 a->worktree->path_prefix, parent_path) == -1) {
3979 err = got_error_from_errno("asprintf");
3980 goto done;
3985 err = got_object_id_by_path(&tree_id, a->repo,
3986 a->worktree->base_commit_id, tree_path);
3987 if (err) {
3988 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3989 (status == GOT_STATUS_ADD ||
3990 staged_status == GOT_STATUS_ADD)))
3991 goto done;
3992 } else {
3993 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3994 if (err)
3995 goto done;
3997 te_name = basename(ie->path);
3998 if (te_name == NULL) {
3999 err = got_error_from_errno2("basename", ie->path);
4000 goto done;
4003 te = got_object_tree_find_entry(tree, te_name);
4004 if (te == NULL && status != GOT_STATUS_ADD &&
4005 staged_status != GOT_STATUS_ADD) {
4006 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4007 goto done;
4011 switch (status) {
4012 case GOT_STATUS_ADD:
4013 if (a->patch_cb) {
4014 int choice = GOT_PATCH_CHOICE_NONE;
4015 err = (*a->patch_cb)(&choice, a->patch_arg,
4016 status, ie->path, NULL, 1, 1);
4017 if (err)
4018 goto done;
4019 if (choice != GOT_PATCH_CHOICE_YES)
4020 break;
4022 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4023 ie->path);
4024 if (err)
4025 goto done;
4026 got_fileindex_entry_remove(a->fileindex, ie);
4027 break;
4028 case GOT_STATUS_DELETE:
4029 if (a->patch_cb) {
4030 int choice = GOT_PATCH_CHOICE_NONE;
4031 err = (*a->patch_cb)(&choice, a->patch_arg,
4032 status, ie->path, NULL, 1, 1);
4033 if (err)
4034 goto done;
4035 if (choice != GOT_PATCH_CHOICE_YES)
4036 break;
4038 /* fall through */
4039 case GOT_STATUS_MODIFY:
4040 case GOT_STATUS_MODE_CHANGE:
4041 case GOT_STATUS_CONFLICT:
4042 case GOT_STATUS_MISSING: {
4043 struct got_object_id id;
4044 if (staged_status == GOT_STATUS_ADD ||
4045 staged_status == GOT_STATUS_MODIFY) {
4046 memcpy(id.sha1, ie->staged_blob_sha1,
4047 SHA1_DIGEST_LENGTH);
4048 } else
4049 memcpy(id.sha1, ie->blob_sha1,
4050 SHA1_DIGEST_LENGTH);
4051 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4052 if (err)
4053 goto done;
4055 if (asprintf(&ondisk_path, "%s/%s",
4056 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4057 err = got_error_from_errno("asprintf");
4058 goto done;
4061 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4062 status == GOT_STATUS_CONFLICT)) {
4063 err = create_patched_content(&path_content, 1, &id,
4064 ondisk_path, dirfd, de_name, ie->path, a->repo,
4065 a->patch_cb, a->patch_arg);
4066 if (err || path_content == NULL)
4067 break;
4068 if (rename(path_content, ondisk_path) == -1) {
4069 err = got_error_from_errno3("rename",
4070 path_content, ondisk_path);
4071 goto done;
4073 } else {
4074 err = install_blob(a->worktree, ondisk_path, ie->path,
4075 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4076 got_fileindex_perms_to_st(ie), blob, 0, 1,
4077 a->repo, a->progress_cb, a->progress_arg);
4078 if (err)
4079 goto done;
4080 if (status == GOT_STATUS_DELETE ||
4081 status == GOT_STATUS_MODE_CHANGE) {
4082 err = got_fileindex_entry_update(ie,
4083 ondisk_path, blob->id.sha1,
4084 a->worktree->base_commit_id->sha1, 1);
4085 if (err)
4086 goto done;
4089 break;
4091 default:
4092 break;
4094 done:
4095 free(ondisk_path);
4096 free(path_content);
4097 free(parent_path);
4098 free(tree_path);
4099 if (blob)
4100 got_object_blob_close(blob);
4101 if (tree)
4102 got_object_tree_close(tree);
4103 free(tree_id);
4104 return err;
4107 const struct got_error *
4108 got_worktree_revert(struct got_worktree *worktree,
4109 struct got_pathlist_head *paths,
4110 got_worktree_checkout_cb progress_cb, void *progress_arg,
4111 got_worktree_patch_cb patch_cb, void *patch_arg,
4112 struct got_repository *repo)
4114 struct got_fileindex *fileindex = NULL;
4115 char *fileindex_path = NULL;
4116 const struct got_error *err = NULL, *unlockerr = NULL;
4117 const struct got_error *sync_err = NULL;
4118 struct got_pathlist_entry *pe;
4119 struct revert_file_args rfa;
4121 err = lock_worktree(worktree, LOCK_EX);
4122 if (err)
4123 return err;
4125 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4126 if (err)
4127 goto done;
4129 rfa.worktree = worktree;
4130 rfa.fileindex = fileindex;
4131 rfa.progress_cb = progress_cb;
4132 rfa.progress_arg = progress_arg;
4133 rfa.patch_cb = patch_cb;
4134 rfa.patch_arg = patch_arg;
4135 rfa.repo = repo;
4136 TAILQ_FOREACH(pe, paths, entry) {
4137 err = worktree_status(worktree, pe->path, fileindex, repo,
4138 revert_file, &rfa, NULL, NULL, 0, 0);
4139 if (err)
4140 break;
4142 sync_err = sync_fileindex(fileindex, fileindex_path);
4143 if (sync_err && err == NULL)
4144 err = sync_err;
4145 done:
4146 free(fileindex_path);
4147 if (fileindex)
4148 got_fileindex_free(fileindex);
4149 unlockerr = lock_worktree(worktree, LOCK_SH);
4150 if (unlockerr && err == NULL)
4151 err = unlockerr;
4152 return err;
4155 static void
4156 free_commitable(struct got_commitable *ct)
4158 free(ct->path);
4159 free(ct->in_repo_path);
4160 free(ct->ondisk_path);
4161 free(ct->blob_id);
4162 free(ct->base_blob_id);
4163 free(ct->staged_blob_id);
4164 free(ct->base_commit_id);
4165 free(ct);
4168 struct collect_commitables_arg {
4169 struct got_pathlist_head *commitable_paths;
4170 struct got_repository *repo;
4171 struct got_worktree *worktree;
4172 int have_staged_files;
4175 static const struct got_error *
4176 collect_commitables(void *arg, unsigned char status,
4177 unsigned char staged_status, const char *relpath,
4178 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4179 struct got_object_id *commit_id, int dirfd, const char *de_name)
4181 struct collect_commitables_arg *a = arg;
4182 const struct got_error *err = NULL;
4183 struct got_commitable *ct = NULL;
4184 struct got_pathlist_entry *new = NULL;
4185 char *parent_path = NULL, *path = NULL;
4186 struct stat sb;
4188 if (a->have_staged_files) {
4189 if (staged_status != GOT_STATUS_MODIFY &&
4190 staged_status != GOT_STATUS_ADD &&
4191 staged_status != GOT_STATUS_DELETE)
4192 return NULL;
4193 } else {
4194 if (status == GOT_STATUS_CONFLICT)
4195 return got_error(GOT_ERR_COMMIT_CONFLICT);
4197 if (status != GOT_STATUS_MODIFY &&
4198 status != GOT_STATUS_MODE_CHANGE &&
4199 status != GOT_STATUS_ADD &&
4200 status != GOT_STATUS_DELETE)
4201 return NULL;
4204 if (asprintf(&path, "/%s", relpath) == -1) {
4205 err = got_error_from_errno("asprintf");
4206 goto done;
4208 if (strcmp(path, "/") == 0) {
4209 parent_path = strdup("");
4210 if (parent_path == NULL)
4211 return got_error_from_errno("strdup");
4212 } else {
4213 err = got_path_dirname(&parent_path, path);
4214 if (err)
4215 return err;
4218 ct = calloc(1, sizeof(*ct));
4219 if (ct == NULL) {
4220 err = got_error_from_errno("calloc");
4221 goto done;
4224 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4225 relpath) == -1) {
4226 err = got_error_from_errno("asprintf");
4227 goto done;
4229 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4230 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4231 } else {
4232 if (dirfd != -1) {
4233 if (fstatat(dirfd, de_name, &sb,
4234 AT_SYMLINK_NOFOLLOW) == -1) {
4235 err = got_error_from_errno2("fstatat",
4236 ct->ondisk_path);
4237 goto done;
4239 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4240 err = got_error_from_errno2("lstat", ct->ondisk_path);
4241 goto done;
4243 ct->mode = sb.st_mode;
4246 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4247 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4248 relpath) == -1) {
4249 err = got_error_from_errno("asprintf");
4250 goto done;
4253 ct->status = status;
4254 ct->staged_status = staged_status;
4255 ct->blob_id = NULL; /* will be filled in when blob gets created */
4256 if (ct->status != GOT_STATUS_ADD &&
4257 ct->staged_status != GOT_STATUS_ADD) {
4258 ct->base_blob_id = got_object_id_dup(blob_id);
4259 if (ct->base_blob_id == NULL) {
4260 err = got_error_from_errno("got_object_id_dup");
4261 goto done;
4263 ct->base_commit_id = got_object_id_dup(commit_id);
4264 if (ct->base_commit_id == NULL) {
4265 err = got_error_from_errno("got_object_id_dup");
4266 goto done;
4269 if (ct->staged_status == GOT_STATUS_ADD ||
4270 ct->staged_status == GOT_STATUS_MODIFY) {
4271 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4272 if (ct->staged_blob_id == NULL) {
4273 err = got_error_from_errno("got_object_id_dup");
4274 goto done;
4277 ct->path = strdup(path);
4278 if (ct->path == NULL) {
4279 err = got_error_from_errno("strdup");
4280 goto done;
4282 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4283 done:
4284 if (ct && (err || new == NULL))
4285 free_commitable(ct);
4286 free(parent_path);
4287 free(path);
4288 return err;
4291 static const struct got_error *write_tree(struct got_object_id **, int *,
4292 struct got_tree_object *, const char *, struct got_pathlist_head *,
4293 got_worktree_status_cb status_cb, void *status_arg,
4294 struct got_repository *);
4296 static const struct got_error *
4297 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4298 struct got_tree_entry *te, const char *parent_path,
4299 struct got_pathlist_head *commitable_paths,
4300 got_worktree_status_cb status_cb, void *status_arg,
4301 struct got_repository *repo)
4303 const struct got_error *err = NULL;
4304 struct got_tree_object *subtree;
4305 char *subpath;
4307 if (asprintf(&subpath, "%s%s%s", parent_path,
4308 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4309 return got_error_from_errno("asprintf");
4311 err = got_object_open_as_tree(&subtree, repo, &te->id);
4312 if (err)
4313 return err;
4315 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4316 commitable_paths, status_cb, status_arg, repo);
4317 got_object_tree_close(subtree);
4318 free(subpath);
4319 return err;
4322 static const struct got_error *
4323 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4325 const struct got_error *err = NULL;
4326 char *ct_parent_path = NULL;
4328 *match = 0;
4330 if (strchr(ct->in_repo_path, '/') == NULL) {
4331 *match = got_path_is_root_dir(path);
4332 return NULL;
4335 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4336 if (err)
4337 return err;
4338 *match = (strcmp(path, ct_parent_path) == 0);
4339 free(ct_parent_path);
4340 return err;
4343 static mode_t
4344 get_ct_file_mode(struct got_commitable *ct)
4346 if (S_ISLNK(ct->mode))
4347 return S_IFLNK;
4349 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4352 static const struct got_error *
4353 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4354 struct got_tree_entry *te, struct got_commitable *ct)
4356 const struct got_error *err = NULL;
4358 *new_te = NULL;
4360 err = got_object_tree_entry_dup(new_te, te);
4361 if (err)
4362 goto done;
4364 (*new_te)->mode = get_ct_file_mode(ct);
4366 if (ct->staged_status == GOT_STATUS_MODIFY)
4367 memcpy(&(*new_te)->id, ct->staged_blob_id,
4368 sizeof((*new_te)->id));
4369 else
4370 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4371 done:
4372 if (err && *new_te) {
4373 free(*new_te);
4374 *new_te = NULL;
4376 return err;
4379 static const struct got_error *
4380 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4381 struct got_commitable *ct)
4383 const struct got_error *err = NULL;
4384 char *ct_name;
4386 *new_te = NULL;
4388 *new_te = calloc(1, sizeof(**new_te));
4389 if (*new_te == NULL)
4390 return got_error_from_errno("calloc");
4392 ct_name = basename(ct->path);
4393 if (ct_name == NULL) {
4394 err = got_error_from_errno2("basename", ct->path);
4395 goto done;
4397 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4398 sizeof((*new_te)->name)) {
4399 err = got_error(GOT_ERR_NO_SPACE);
4400 goto done;
4403 (*new_te)->mode = get_ct_file_mode(ct);
4405 if (ct->staged_status == GOT_STATUS_ADD)
4406 memcpy(&(*new_te)->id, ct->staged_blob_id,
4407 sizeof((*new_te)->id));
4408 else
4409 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4410 done:
4411 if (err && *new_te) {
4412 free(*new_te);
4413 *new_te = NULL;
4415 return err;
4418 static const struct got_error *
4419 insert_tree_entry(struct got_tree_entry *new_te,
4420 struct got_pathlist_head *paths)
4422 const struct got_error *err = NULL;
4423 struct got_pathlist_entry *new_pe;
4425 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4426 if (err)
4427 return err;
4428 if (new_pe == NULL)
4429 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4430 return NULL;
4433 static const struct got_error *
4434 report_ct_status(struct got_commitable *ct,
4435 got_worktree_status_cb status_cb, void *status_arg)
4437 const char *ct_path = ct->path;
4438 unsigned char status;
4440 while (ct_path[0] == '/')
4441 ct_path++;
4443 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4444 status = ct->staged_status;
4445 else
4446 status = ct->status;
4448 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4449 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4452 static const struct got_error *
4453 match_modified_subtree(int *modified, struct got_tree_entry *te,
4454 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4456 const struct got_error *err = NULL;
4457 struct got_pathlist_entry *pe;
4458 char *te_path;
4460 *modified = 0;
4462 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4463 got_path_is_root_dir(base_tree_path) ? "" : "/",
4464 te->name) == -1)
4465 return got_error_from_errno("asprintf");
4467 TAILQ_FOREACH(pe, commitable_paths, entry) {
4468 struct got_commitable *ct = pe->data;
4469 *modified = got_path_is_child(ct->in_repo_path, te_path,
4470 strlen(te_path));
4471 if (*modified)
4472 break;
4475 free(te_path);
4476 return err;
4479 static const struct got_error *
4480 match_deleted_or_modified_ct(struct got_commitable **ctp,
4481 struct got_tree_entry *te, const char *base_tree_path,
4482 struct got_pathlist_head *commitable_paths)
4484 const struct got_error *err = NULL;
4485 struct got_pathlist_entry *pe;
4487 *ctp = NULL;
4489 TAILQ_FOREACH(pe, commitable_paths, entry) {
4490 struct got_commitable *ct = pe->data;
4491 char *ct_name = NULL;
4492 int path_matches;
4494 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4495 if (ct->status != GOT_STATUS_MODIFY &&
4496 ct->status != GOT_STATUS_MODE_CHANGE &&
4497 ct->status != GOT_STATUS_DELETE)
4498 continue;
4499 } else {
4500 if (ct->staged_status != GOT_STATUS_MODIFY &&
4501 ct->staged_status != GOT_STATUS_DELETE)
4502 continue;
4505 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4506 continue;
4508 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4509 if (err)
4510 return err;
4511 if (!path_matches)
4512 continue;
4514 ct_name = basename(pe->path);
4515 if (ct_name == NULL)
4516 return got_error_from_errno2("basename", pe->path);
4518 if (strcmp(te->name, ct_name) != 0)
4519 continue;
4521 *ctp = ct;
4522 break;
4525 return err;
4528 static const struct got_error *
4529 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4530 const char *child_path, const char *path_base_tree,
4531 struct got_pathlist_head *commitable_paths,
4532 got_worktree_status_cb status_cb, void *status_arg,
4533 struct got_repository *repo)
4535 const struct got_error *err = NULL;
4536 struct got_tree_entry *new_te;
4537 char *subtree_path;
4538 struct got_object_id *id = NULL;
4539 int nentries;
4541 *new_tep = NULL;
4543 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4544 got_path_is_root_dir(path_base_tree) ? "" : "/",
4545 child_path) == -1)
4546 return got_error_from_errno("asprintf");
4548 new_te = calloc(1, sizeof(*new_te));
4549 if (new_te == NULL)
4550 return got_error_from_errno("calloc");
4551 new_te->mode = S_IFDIR;
4553 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4554 sizeof(new_te->name)) {
4555 err = got_error(GOT_ERR_NO_SPACE);
4556 goto done;
4558 err = write_tree(&id, &nentries, NULL, subtree_path,
4559 commitable_paths, status_cb, status_arg, repo);
4560 if (err) {
4561 free(new_te);
4562 goto done;
4564 memcpy(&new_te->id, id, sizeof(new_te->id));
4565 done:
4566 free(id);
4567 free(subtree_path);
4568 if (err == NULL)
4569 *new_tep = new_te;
4570 return err;
4573 static const struct got_error *
4574 write_tree(struct got_object_id **new_tree_id, int *nentries,
4575 struct got_tree_object *base_tree, const char *path_base_tree,
4576 struct got_pathlist_head *commitable_paths,
4577 got_worktree_status_cb status_cb, void *status_arg,
4578 struct got_repository *repo)
4580 const struct got_error *err = NULL;
4581 struct got_pathlist_head paths;
4582 struct got_tree_entry *te, *new_te = NULL;
4583 struct got_pathlist_entry *pe;
4585 TAILQ_INIT(&paths);
4586 *nentries = 0;
4588 /* Insert, and recurse into, newly added entries first. */
4589 TAILQ_FOREACH(pe, commitable_paths, entry) {
4590 struct got_commitable *ct = pe->data;
4591 char *child_path = NULL, *slash;
4593 if ((ct->status != GOT_STATUS_ADD &&
4594 ct->staged_status != GOT_STATUS_ADD) ||
4595 (ct->flags & GOT_COMMITABLE_ADDED))
4596 continue;
4598 if (!got_path_is_child(pe->path, path_base_tree,
4599 strlen(path_base_tree)))
4600 continue;
4602 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4603 pe->path);
4604 if (err)
4605 goto done;
4607 slash = strchr(child_path, '/');
4608 if (slash == NULL) {
4609 err = alloc_added_blob_tree_entry(&new_te, ct);
4610 if (err)
4611 goto done;
4612 err = report_ct_status(ct, status_cb, status_arg);
4613 if (err)
4614 goto done;
4615 ct->flags |= GOT_COMMITABLE_ADDED;
4616 err = insert_tree_entry(new_te, &paths);
4617 if (err)
4618 goto done;
4619 (*nentries)++;
4620 } else {
4621 *slash = '\0'; /* trim trailing path components */
4622 if (base_tree == NULL ||
4623 got_object_tree_find_entry(base_tree, child_path)
4624 == NULL) {
4625 err = make_subtree_for_added_blob(&new_te,
4626 child_path, path_base_tree,
4627 commitable_paths, status_cb, status_arg,
4628 repo);
4629 if (err)
4630 goto done;
4631 err = insert_tree_entry(new_te, &paths);
4632 if (err)
4633 goto done;
4634 (*nentries)++;
4639 if (base_tree) {
4640 int i, nbase_entries;
4641 /* Handle modified and deleted entries. */
4642 nbase_entries = got_object_tree_get_nentries(base_tree);
4643 for (i = 0; i < nbase_entries; i++) {
4644 struct got_commitable *ct = NULL;
4646 te = got_object_tree_get_entry(base_tree, i);
4647 if (got_object_tree_entry_is_submodule(te)) {
4648 /* Entry is a submodule; just copy it. */
4649 err = got_object_tree_entry_dup(&new_te, te);
4650 if (err)
4651 goto done;
4652 err = insert_tree_entry(new_te, &paths);
4653 if (err)
4654 goto done;
4655 (*nentries)++;
4656 continue;
4659 if (S_ISDIR(te->mode)) {
4660 int modified;
4661 err = got_object_tree_entry_dup(&new_te, te);
4662 if (err)
4663 goto done;
4664 err = match_modified_subtree(&modified, te,
4665 path_base_tree, commitable_paths);
4666 if (err)
4667 goto done;
4668 /* Avoid recursion into unmodified subtrees. */
4669 if (modified) {
4670 struct got_object_id *new_id;
4671 int nsubentries;
4672 err = write_subtree(&new_id,
4673 &nsubentries, te,
4674 path_base_tree, commitable_paths,
4675 status_cb, status_arg, repo);
4676 if (err)
4677 goto done;
4678 if (nsubentries == 0) {
4679 /* All entries were deleted. */
4680 free(new_id);
4681 continue;
4683 memcpy(&new_te->id, new_id,
4684 sizeof(new_te->id));
4685 free(new_id);
4687 err = insert_tree_entry(new_te, &paths);
4688 if (err)
4689 goto done;
4690 (*nentries)++;
4691 continue;
4694 err = match_deleted_or_modified_ct(&ct, te,
4695 path_base_tree, commitable_paths);
4696 if (err)
4697 goto done;
4698 if (ct) {
4699 /* NB: Deleted entries get dropped here. */
4700 if (ct->status == GOT_STATUS_MODIFY ||
4701 ct->status == GOT_STATUS_MODE_CHANGE ||
4702 ct->staged_status == GOT_STATUS_MODIFY) {
4703 err = alloc_modified_blob_tree_entry(
4704 &new_te, te, ct);
4705 if (err)
4706 goto done;
4707 err = insert_tree_entry(new_te, &paths);
4708 if (err)
4709 goto done;
4710 (*nentries)++;
4712 err = report_ct_status(ct, status_cb,
4713 status_arg);
4714 if (err)
4715 goto done;
4716 } else {
4717 /* Entry is unchanged; just copy it. */
4718 err = got_object_tree_entry_dup(&new_te, te);
4719 if (err)
4720 goto done;
4721 err = insert_tree_entry(new_te, &paths);
4722 if (err)
4723 goto done;
4724 (*nentries)++;
4729 /* Write new list of entries; deleted entries have been dropped. */
4730 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4731 done:
4732 got_pathlist_free(&paths);
4733 return err;
4736 static const struct got_error *
4737 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4738 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4739 int have_staged_files)
4741 const struct got_error *err = NULL;
4742 struct got_pathlist_entry *pe;
4744 TAILQ_FOREACH(pe, commitable_paths, entry) {
4745 struct got_fileindex_entry *ie;
4746 struct got_commitable *ct = pe->data;
4748 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4749 if (ie) {
4750 if (ct->status == GOT_STATUS_DELETE ||
4751 ct->staged_status == GOT_STATUS_DELETE) {
4752 got_fileindex_entry_remove(fileindex, ie);
4753 } else if (ct->staged_status == GOT_STATUS_ADD ||
4754 ct->staged_status == GOT_STATUS_MODIFY) {
4755 got_fileindex_entry_stage_set(ie,
4756 GOT_FILEIDX_STAGE_NONE);
4757 err = got_fileindex_entry_update(ie,
4758 ct->ondisk_path, ct->staged_blob_id->sha1,
4759 new_base_commit_id->sha1,
4760 !have_staged_files);
4761 } else
4762 err = got_fileindex_entry_update(ie,
4763 ct->ondisk_path, ct->blob_id->sha1,
4764 new_base_commit_id->sha1,
4765 !have_staged_files);
4766 } else {
4767 err = got_fileindex_entry_alloc(&ie, pe->path);
4768 if (err)
4769 break;
4770 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4771 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4772 if (err) {
4773 got_fileindex_entry_free(ie);
4774 break;
4776 err = got_fileindex_entry_add(fileindex, ie);
4777 if (err) {
4778 got_fileindex_entry_free(ie);
4779 break;
4783 return err;
4787 static const struct got_error *
4788 check_out_of_date(const char *in_repo_path, unsigned char status,
4789 unsigned char staged_status, struct got_object_id *base_blob_id,
4790 struct got_object_id *base_commit_id,
4791 struct got_object_id *head_commit_id, struct got_repository *repo,
4792 int ood_errcode)
4794 const struct got_error *err = NULL;
4795 struct got_object_id *id = NULL;
4797 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4798 /* Trivial case: base commit == head commit */
4799 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4800 return NULL;
4802 * Ensure file content which local changes were based
4803 * on matches file content in the branch head.
4805 err = got_object_id_by_path(&id, repo, head_commit_id,
4806 in_repo_path);
4807 if (err) {
4808 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4809 err = got_error(ood_errcode);
4810 goto done;
4811 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4812 err = got_error(ood_errcode);
4813 } else {
4814 /* Require that added files don't exist in the branch head. */
4815 err = got_object_id_by_path(&id, repo, head_commit_id,
4816 in_repo_path);
4817 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4818 goto done;
4819 err = id ? got_error(ood_errcode) : NULL;
4821 done:
4822 free(id);
4823 return err;
4826 const struct got_error *
4827 commit_worktree(struct got_object_id **new_commit_id,
4828 struct got_pathlist_head *commitable_paths,
4829 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4830 const char *author, const char *committer,
4831 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4832 got_worktree_status_cb status_cb, void *status_arg,
4833 struct got_repository *repo)
4835 const struct got_error *err = NULL, *unlockerr = NULL;
4836 struct got_pathlist_entry *pe;
4837 const char *head_ref_name = NULL;
4838 struct got_commit_object *head_commit = NULL;
4839 struct got_reference *head_ref2 = NULL;
4840 struct got_object_id *head_commit_id2 = NULL;
4841 struct got_tree_object *head_tree = NULL;
4842 struct got_object_id *new_tree_id = NULL;
4843 int nentries;
4844 struct got_object_id_queue parent_ids;
4845 struct got_object_qid *pid = NULL;
4846 char *logmsg = NULL;
4848 *new_commit_id = NULL;
4850 SIMPLEQ_INIT(&parent_ids);
4852 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4853 if (err)
4854 goto done;
4856 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4857 if (err)
4858 goto done;
4860 if (commit_msg_cb != NULL) {
4861 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4862 if (err)
4863 goto done;
4866 if (logmsg == NULL || strlen(logmsg) == 0) {
4867 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4868 goto done;
4871 /* Create blobs from added and modified files and record their IDs. */
4872 TAILQ_FOREACH(pe, commitable_paths, entry) {
4873 struct got_commitable *ct = pe->data;
4874 char *ondisk_path;
4876 /* Blobs for staged files already exist. */
4877 if (ct->staged_status == GOT_STATUS_ADD ||
4878 ct->staged_status == GOT_STATUS_MODIFY)
4879 continue;
4881 if (ct->status != GOT_STATUS_ADD &&
4882 ct->status != GOT_STATUS_MODIFY &&
4883 ct->status != GOT_STATUS_MODE_CHANGE)
4884 continue;
4886 if (asprintf(&ondisk_path, "%s/%s",
4887 worktree->root_path, pe->path) == -1) {
4888 err = got_error_from_errno("asprintf");
4889 goto done;
4891 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4892 free(ondisk_path);
4893 if (err)
4894 goto done;
4897 /* Recursively write new tree objects. */
4898 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4899 commitable_paths, status_cb, status_arg, repo);
4900 if (err)
4901 goto done;
4903 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4904 if (err)
4905 goto done;
4906 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4907 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4908 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4909 got_object_qid_free(pid);
4910 if (logmsg != NULL)
4911 free(logmsg);
4912 if (err)
4913 goto done;
4915 /* Check if a concurrent commit to our branch has occurred. */
4916 head_ref_name = got_worktree_get_head_ref_name(worktree);
4917 if (head_ref_name == NULL) {
4918 err = got_error_from_errno("got_worktree_get_head_ref_name");
4919 goto done;
4921 /* Lock the reference here to prevent concurrent modification. */
4922 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4923 if (err)
4924 goto done;
4925 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4926 if (err)
4927 goto done;
4928 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4929 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4930 goto done;
4932 /* Update branch head in repository. */
4933 err = got_ref_change_ref(head_ref2, *new_commit_id);
4934 if (err)
4935 goto done;
4936 err = got_ref_write(head_ref2, repo);
4937 if (err)
4938 goto done;
4940 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4941 if (err)
4942 goto done;
4944 err = ref_base_commit(worktree, repo);
4945 if (err)
4946 goto done;
4947 done:
4948 if (head_tree)
4949 got_object_tree_close(head_tree);
4950 if (head_commit)
4951 got_object_commit_close(head_commit);
4952 free(head_commit_id2);
4953 if (head_ref2) {
4954 unlockerr = got_ref_unlock(head_ref2);
4955 if (unlockerr && err == NULL)
4956 err = unlockerr;
4957 got_ref_close(head_ref2);
4959 return err;
4962 static const struct got_error *
4963 check_path_is_commitable(const char *path,
4964 struct got_pathlist_head *commitable_paths)
4966 struct got_pathlist_entry *cpe = NULL;
4967 size_t path_len = strlen(path);
4969 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4970 struct got_commitable *ct = cpe->data;
4971 const char *ct_path = ct->path;
4973 while (ct_path[0] == '/')
4974 ct_path++;
4976 if (strcmp(path, ct_path) == 0 ||
4977 got_path_is_child(ct_path, path, path_len))
4978 break;
4981 if (cpe == NULL)
4982 return got_error_path(path, GOT_ERR_BAD_PATH);
4984 return NULL;
4987 static const struct got_error *
4988 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4990 int *have_staged_files = arg;
4992 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4993 *have_staged_files = 1;
4994 return got_error(GOT_ERR_CANCELLED);
4997 return NULL;
5000 static const struct got_error *
5001 check_non_staged_files(struct got_fileindex *fileindex,
5002 struct got_pathlist_head *paths)
5004 struct got_pathlist_entry *pe;
5005 struct got_fileindex_entry *ie;
5007 TAILQ_FOREACH(pe, paths, entry) {
5008 if (pe->path[0] == '\0')
5009 continue;
5010 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5011 if (ie == NULL)
5012 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5013 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5014 return got_error_path(pe->path,
5015 GOT_ERR_FILE_NOT_STAGED);
5018 return NULL;
5021 const struct got_error *
5022 got_worktree_commit(struct got_object_id **new_commit_id,
5023 struct got_worktree *worktree, struct got_pathlist_head *paths,
5024 const char *author, const char *committer,
5025 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5026 got_worktree_status_cb status_cb, void *status_arg,
5027 struct got_repository *repo)
5029 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5030 struct got_fileindex *fileindex = NULL;
5031 char *fileindex_path = NULL;
5032 struct got_pathlist_head commitable_paths;
5033 struct collect_commitables_arg cc_arg;
5034 struct got_pathlist_entry *pe;
5035 struct got_reference *head_ref = NULL;
5036 struct got_object_id *head_commit_id = NULL;
5037 int have_staged_files = 0;
5039 *new_commit_id = NULL;
5041 TAILQ_INIT(&commitable_paths);
5043 err = lock_worktree(worktree, LOCK_EX);
5044 if (err)
5045 goto done;
5047 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5048 if (err)
5049 goto done;
5051 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5052 if (err)
5053 goto done;
5055 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5056 if (err)
5057 goto done;
5059 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5060 &have_staged_files);
5061 if (err && err->code != GOT_ERR_CANCELLED)
5062 goto done;
5063 if (have_staged_files) {
5064 err = check_non_staged_files(fileindex, paths);
5065 if (err)
5066 goto done;
5069 cc_arg.commitable_paths = &commitable_paths;
5070 cc_arg.worktree = worktree;
5071 cc_arg.repo = repo;
5072 cc_arg.have_staged_files = have_staged_files;
5073 TAILQ_FOREACH(pe, paths, entry) {
5074 err = worktree_status(worktree, pe->path, fileindex, repo,
5075 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5076 if (err)
5077 goto done;
5080 if (TAILQ_EMPTY(&commitable_paths)) {
5081 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5082 goto done;
5085 TAILQ_FOREACH(pe, paths, entry) {
5086 err = check_path_is_commitable(pe->path, &commitable_paths);
5087 if (err)
5088 goto done;
5091 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5092 struct got_commitable *ct = pe->data;
5093 const char *ct_path = ct->in_repo_path;
5095 while (ct_path[0] == '/')
5096 ct_path++;
5097 err = check_out_of_date(ct_path, ct->status,
5098 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5099 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5100 if (err)
5101 goto done;
5105 err = commit_worktree(new_commit_id, &commitable_paths,
5106 head_commit_id, worktree, author, committer,
5107 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5108 if (err)
5109 goto done;
5111 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5112 fileindex, have_staged_files);
5113 sync_err = sync_fileindex(fileindex, fileindex_path);
5114 if (sync_err && err == NULL)
5115 err = sync_err;
5116 done:
5117 if (fileindex)
5118 got_fileindex_free(fileindex);
5119 free(fileindex_path);
5120 unlockerr = lock_worktree(worktree, LOCK_SH);
5121 if (unlockerr && err == NULL)
5122 err = unlockerr;
5123 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5124 struct got_commitable *ct = pe->data;
5125 free_commitable(ct);
5127 got_pathlist_free(&commitable_paths);
5128 return err;
5131 const char *
5132 got_commitable_get_path(struct got_commitable *ct)
5134 return ct->path;
5137 unsigned int
5138 got_commitable_get_status(struct got_commitable *ct)
5140 return ct->status;
5143 struct check_rebase_ok_arg {
5144 struct got_worktree *worktree;
5145 struct got_repository *repo;
5148 static const struct got_error *
5149 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5151 const struct got_error *err = NULL;
5152 struct check_rebase_ok_arg *a = arg;
5153 unsigned char status;
5154 struct stat sb;
5155 char *ondisk_path;
5157 /* Reject rebase of a work tree with mixed base commits. */
5158 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5159 SHA1_DIGEST_LENGTH))
5160 return got_error(GOT_ERR_MIXED_COMMITS);
5162 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5163 == -1)
5164 return got_error_from_errno("asprintf");
5166 /* Reject rebase of a work tree with modified or staged files. */
5167 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5168 free(ondisk_path);
5169 if (err)
5170 return err;
5172 if (status != GOT_STATUS_NO_CHANGE)
5173 return got_error(GOT_ERR_MODIFIED);
5174 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5175 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5177 return NULL;
5180 const struct got_error *
5181 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5182 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5183 struct got_worktree *worktree, struct got_reference *branch,
5184 struct got_repository *repo)
5186 const struct got_error *err = NULL;
5187 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5188 char *branch_ref_name = NULL;
5189 char *fileindex_path = NULL;
5190 struct check_rebase_ok_arg ok_arg;
5191 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5192 struct got_object_id *wt_branch_tip = NULL;
5194 *new_base_branch_ref = NULL;
5195 *tmp_branch = NULL;
5196 *fileindex = NULL;
5198 err = lock_worktree(worktree, LOCK_EX);
5199 if (err)
5200 return err;
5202 err = open_fileindex(fileindex, &fileindex_path, worktree);
5203 if (err)
5204 goto done;
5206 ok_arg.worktree = worktree;
5207 ok_arg.repo = repo;
5208 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5209 &ok_arg);
5210 if (err)
5211 goto done;
5213 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5214 if (err)
5215 goto done;
5217 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5218 if (err)
5219 goto done;
5221 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5222 if (err)
5223 goto done;
5225 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5226 0);
5227 if (err)
5228 goto done;
5230 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5231 if (err)
5232 goto done;
5233 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5234 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5235 goto done;
5238 err = got_ref_alloc_symref(new_base_branch_ref,
5239 new_base_branch_ref_name, wt_branch);
5240 if (err)
5241 goto done;
5242 err = got_ref_write(*new_base_branch_ref, repo);
5243 if (err)
5244 goto done;
5246 /* TODO Lock original branch's ref while rebasing? */
5248 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5249 if (err)
5250 goto done;
5252 err = got_ref_write(branch_ref, repo);
5253 if (err)
5254 goto done;
5256 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5257 worktree->base_commit_id);
5258 if (err)
5259 goto done;
5260 err = got_ref_write(*tmp_branch, repo);
5261 if (err)
5262 goto done;
5264 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5265 if (err)
5266 goto done;
5267 done:
5268 free(fileindex_path);
5269 free(tmp_branch_name);
5270 free(new_base_branch_ref_name);
5271 free(branch_ref_name);
5272 if (branch_ref)
5273 got_ref_close(branch_ref);
5274 if (wt_branch)
5275 got_ref_close(wt_branch);
5276 free(wt_branch_tip);
5277 if (err) {
5278 if (*new_base_branch_ref) {
5279 got_ref_close(*new_base_branch_ref);
5280 *new_base_branch_ref = NULL;
5282 if (*tmp_branch) {
5283 got_ref_close(*tmp_branch);
5284 *tmp_branch = NULL;
5286 if (*fileindex) {
5287 got_fileindex_free(*fileindex);
5288 *fileindex = NULL;
5290 lock_worktree(worktree, LOCK_SH);
5292 return err;
5295 const struct got_error *
5296 got_worktree_rebase_continue(struct got_object_id **commit_id,
5297 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5298 struct got_reference **branch, struct got_fileindex **fileindex,
5299 struct got_worktree *worktree, struct got_repository *repo)
5301 const struct got_error *err;
5302 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5303 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5304 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5305 char *fileindex_path = NULL;
5306 int have_staged_files = 0;
5308 *commit_id = NULL;
5309 *new_base_branch = NULL;
5310 *tmp_branch = NULL;
5311 *branch = NULL;
5312 *fileindex = NULL;
5314 err = lock_worktree(worktree, LOCK_EX);
5315 if (err)
5316 return err;
5318 err = open_fileindex(fileindex, &fileindex_path, worktree);
5319 if (err)
5320 goto done;
5322 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5323 &have_staged_files);
5324 if (err && err->code != GOT_ERR_CANCELLED)
5325 goto done;
5326 if (have_staged_files) {
5327 err = got_error(GOT_ERR_STAGED_PATHS);
5328 goto done;
5331 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5332 if (err)
5333 goto done;
5335 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5336 if (err)
5337 goto done;
5339 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5340 if (err)
5341 goto done;
5343 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5344 if (err)
5345 goto done;
5347 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5348 if (err)
5349 goto done;
5351 err = got_ref_open(branch, repo,
5352 got_ref_get_symref_target(branch_ref), 0);
5353 if (err)
5354 goto done;
5356 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5357 if (err)
5358 goto done;
5360 err = got_ref_resolve(commit_id, repo, commit_ref);
5361 if (err)
5362 goto done;
5364 err = got_ref_open(new_base_branch, repo,
5365 new_base_branch_ref_name, 0);
5366 if (err)
5367 goto done;
5369 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5370 if (err)
5371 goto done;
5372 done:
5373 free(commit_ref_name);
5374 free(branch_ref_name);
5375 free(fileindex_path);
5376 if (commit_ref)
5377 got_ref_close(commit_ref);
5378 if (branch_ref)
5379 got_ref_close(branch_ref);
5380 if (err) {
5381 free(*commit_id);
5382 *commit_id = NULL;
5383 if (*tmp_branch) {
5384 got_ref_close(*tmp_branch);
5385 *tmp_branch = NULL;
5387 if (*new_base_branch) {
5388 got_ref_close(*new_base_branch);
5389 *new_base_branch = NULL;
5391 if (*branch) {
5392 got_ref_close(*branch);
5393 *branch = NULL;
5395 if (*fileindex) {
5396 got_fileindex_free(*fileindex);
5397 *fileindex = NULL;
5399 lock_worktree(worktree, LOCK_SH);
5401 return err;
5404 const struct got_error *
5405 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5407 const struct got_error *err;
5408 char *tmp_branch_name = NULL;
5410 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5411 if (err)
5412 return err;
5414 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5415 free(tmp_branch_name);
5416 return NULL;
5419 static const struct got_error *
5420 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5421 char **logmsg, void *arg)
5423 *logmsg = arg;
5424 return NULL;
5427 static const struct got_error *
5428 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5429 const char *path, struct got_object_id *blob_id,
5430 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5431 int dirfd, const char *de_name)
5433 return NULL;
5436 struct collect_merged_paths_arg {
5437 got_worktree_checkout_cb progress_cb;
5438 void *progress_arg;
5439 struct got_pathlist_head *merged_paths;
5442 static const struct got_error *
5443 collect_merged_paths(void *arg, unsigned char status, const char *path)
5445 const struct got_error *err;
5446 struct collect_merged_paths_arg *a = arg;
5447 char *p;
5448 struct got_pathlist_entry *new;
5450 err = (*a->progress_cb)(a->progress_arg, status, path);
5451 if (err)
5452 return err;
5454 if (status != GOT_STATUS_MERGE &&
5455 status != GOT_STATUS_ADD &&
5456 status != GOT_STATUS_DELETE &&
5457 status != GOT_STATUS_CONFLICT)
5458 return NULL;
5460 p = strdup(path);
5461 if (p == NULL)
5462 return got_error_from_errno("strdup");
5464 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5465 if (err || new == NULL)
5466 free(p);
5467 return err;
5470 void
5471 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5473 struct got_pathlist_entry *pe;
5475 TAILQ_FOREACH(pe, merged_paths, entry)
5476 free((char *)pe->path);
5478 got_pathlist_free(merged_paths);
5481 static const struct got_error *
5482 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5483 int is_rebase, struct got_repository *repo)
5485 const struct got_error *err;
5486 struct got_reference *commit_ref = NULL;
5488 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5489 if (err) {
5490 if (err->code != GOT_ERR_NOT_REF)
5491 goto done;
5492 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5493 if (err)
5494 goto done;
5495 err = got_ref_write(commit_ref, repo);
5496 if (err)
5497 goto done;
5498 } else if (is_rebase) {
5499 struct got_object_id *stored_id;
5500 int cmp;
5502 err = got_ref_resolve(&stored_id, repo, commit_ref);
5503 if (err)
5504 goto done;
5505 cmp = got_object_id_cmp(commit_id, stored_id);
5506 free(stored_id);
5507 if (cmp != 0) {
5508 err = got_error(GOT_ERR_REBASE_COMMITID);
5509 goto done;
5512 done:
5513 if (commit_ref)
5514 got_ref_close(commit_ref);
5515 return err;
5518 static const struct got_error *
5519 rebase_merge_files(struct got_pathlist_head *merged_paths,
5520 const char *commit_ref_name, struct got_worktree *worktree,
5521 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5522 struct got_object_id *commit_id, struct got_repository *repo,
5523 got_worktree_checkout_cb progress_cb, void *progress_arg,
5524 got_cancel_cb cancel_cb, void *cancel_arg)
5526 const struct got_error *err;
5527 struct got_reference *commit_ref = NULL;
5528 struct collect_merged_paths_arg cmp_arg;
5529 char *fileindex_path;
5531 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5533 err = get_fileindex_path(&fileindex_path, worktree);
5534 if (err)
5535 return err;
5537 cmp_arg.progress_cb = progress_cb;
5538 cmp_arg.progress_arg = progress_arg;
5539 cmp_arg.merged_paths = merged_paths;
5540 err = merge_files(worktree, fileindex, fileindex_path,
5541 parent_commit_id, commit_id, repo, collect_merged_paths,
5542 &cmp_arg, cancel_cb, cancel_arg);
5543 if (commit_ref)
5544 got_ref_close(commit_ref);
5545 return err;
5548 const struct got_error *
5549 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5550 struct got_worktree *worktree, struct got_fileindex *fileindex,
5551 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5552 struct got_repository *repo,
5553 got_worktree_checkout_cb progress_cb, void *progress_arg,
5554 got_cancel_cb cancel_cb, void *cancel_arg)
5556 const struct got_error *err;
5557 char *commit_ref_name;
5559 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5560 if (err)
5561 return err;
5563 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5564 if (err)
5565 goto done;
5567 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5568 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5569 progress_arg, cancel_cb, cancel_arg);
5570 done:
5571 free(commit_ref_name);
5572 return err;
5575 const struct got_error *
5576 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5577 struct got_worktree *worktree, struct got_fileindex *fileindex,
5578 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5579 struct got_repository *repo,
5580 got_worktree_checkout_cb progress_cb, void *progress_arg,
5581 got_cancel_cb cancel_cb, void *cancel_arg)
5583 const struct got_error *err;
5584 char *commit_ref_name;
5586 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5587 if (err)
5588 return err;
5590 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5591 if (err)
5592 goto done;
5594 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5595 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5596 progress_arg, cancel_cb, cancel_arg);
5597 done:
5598 free(commit_ref_name);
5599 return err;
5602 static const struct got_error *
5603 rebase_commit(struct got_object_id **new_commit_id,
5604 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5605 struct got_worktree *worktree, struct got_fileindex *fileindex,
5606 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5607 const char *new_logmsg, struct got_repository *repo)
5609 const struct got_error *err, *sync_err;
5610 struct got_pathlist_head commitable_paths;
5611 struct collect_commitables_arg cc_arg;
5612 char *fileindex_path = NULL;
5613 struct got_reference *head_ref = NULL;
5614 struct got_object_id *head_commit_id = NULL;
5615 char *logmsg = NULL;
5617 TAILQ_INIT(&commitable_paths);
5618 *new_commit_id = NULL;
5620 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5622 err = get_fileindex_path(&fileindex_path, worktree);
5623 if (err)
5624 return err;
5626 cc_arg.commitable_paths = &commitable_paths;
5627 cc_arg.worktree = worktree;
5628 cc_arg.repo = repo;
5629 cc_arg.have_staged_files = 0;
5631 * If possible get the status of individual files directly to
5632 * avoid crawling the entire work tree once per rebased commit.
5633 * TODO: Ideally, merged_paths would contain a list of commitables
5634 * we could use so we could skip worktree_status() entirely.
5636 if (merged_paths) {
5637 struct got_pathlist_entry *pe;
5638 TAILQ_FOREACH(pe, merged_paths, entry) {
5639 err = worktree_status(worktree, pe->path, fileindex,
5640 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5641 0);
5642 if (err)
5643 goto done;
5645 } else {
5646 err = worktree_status(worktree, "", fileindex, repo,
5647 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5648 if (err)
5649 goto done;
5652 if (TAILQ_EMPTY(&commitable_paths)) {
5653 /* No-op change; commit will be elided. */
5654 err = got_ref_delete(commit_ref, repo);
5655 if (err)
5656 goto done;
5657 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5658 goto done;
5661 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5662 if (err)
5663 goto done;
5665 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5666 if (err)
5667 goto done;
5669 if (new_logmsg) {
5670 logmsg = strdup(new_logmsg);
5671 if (logmsg == NULL) {
5672 err = got_error_from_errno("strdup");
5673 goto done;
5675 } else {
5676 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5677 if (err)
5678 goto done;
5681 /* NB: commit_worktree will call free(logmsg) */
5682 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5683 worktree, got_object_commit_get_author(orig_commit),
5684 got_object_commit_get_committer(orig_commit),
5685 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5686 if (err)
5687 goto done;
5689 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5690 if (err)
5691 goto done;
5693 err = got_ref_delete(commit_ref, repo);
5694 if (err)
5695 goto done;
5697 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5698 fileindex, 0);
5699 sync_err = sync_fileindex(fileindex, fileindex_path);
5700 if (sync_err && err == NULL)
5701 err = sync_err;
5702 done:
5703 free(fileindex_path);
5704 free(head_commit_id);
5705 if (head_ref)
5706 got_ref_close(head_ref);
5707 if (err) {
5708 free(*new_commit_id);
5709 *new_commit_id = NULL;
5711 return err;
5714 const struct got_error *
5715 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5716 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5717 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5718 struct got_commit_object *orig_commit,
5719 struct got_object_id *orig_commit_id, struct got_repository *repo)
5721 const struct got_error *err;
5722 char *commit_ref_name;
5723 struct got_reference *commit_ref = NULL;
5724 struct got_object_id *commit_id = NULL;
5726 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5727 if (err)
5728 return err;
5730 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5731 if (err)
5732 goto done;
5733 err = got_ref_resolve(&commit_id, repo, commit_ref);
5734 if (err)
5735 goto done;
5736 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5737 err = got_error(GOT_ERR_REBASE_COMMITID);
5738 goto done;
5741 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5742 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5743 done:
5744 if (commit_ref)
5745 got_ref_close(commit_ref);
5746 free(commit_ref_name);
5747 free(commit_id);
5748 return err;
5751 const struct got_error *
5752 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5753 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5754 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5755 struct got_commit_object *orig_commit,
5756 struct got_object_id *orig_commit_id, const char *new_logmsg,
5757 struct got_repository *repo)
5759 const struct got_error *err;
5760 char *commit_ref_name;
5761 struct got_reference *commit_ref = NULL;
5763 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5764 if (err)
5765 return err;
5767 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5768 if (err)
5769 goto done;
5771 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5772 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5773 done:
5774 if (commit_ref)
5775 got_ref_close(commit_ref);
5776 free(commit_ref_name);
5777 return err;
5780 const struct got_error *
5781 got_worktree_rebase_postpone(struct got_worktree *worktree,
5782 struct got_fileindex *fileindex)
5784 if (fileindex)
5785 got_fileindex_free(fileindex);
5786 return lock_worktree(worktree, LOCK_SH);
5789 static const struct got_error *
5790 delete_ref(const char *name, struct got_repository *repo)
5792 const struct got_error *err;
5793 struct got_reference *ref;
5795 err = got_ref_open(&ref, repo, name, 0);
5796 if (err) {
5797 if (err->code == GOT_ERR_NOT_REF)
5798 return NULL;
5799 return err;
5802 err = got_ref_delete(ref, repo);
5803 got_ref_close(ref);
5804 return err;
5807 static const struct got_error *
5808 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5810 const struct got_error *err;
5811 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5812 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5814 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5815 if (err)
5816 goto done;
5817 err = delete_ref(tmp_branch_name, repo);
5818 if (err)
5819 goto done;
5821 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5822 if (err)
5823 goto done;
5824 err = delete_ref(new_base_branch_ref_name, repo);
5825 if (err)
5826 goto done;
5828 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5829 if (err)
5830 goto done;
5831 err = delete_ref(branch_ref_name, repo);
5832 if (err)
5833 goto done;
5835 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5836 if (err)
5837 goto done;
5838 err = delete_ref(commit_ref_name, repo);
5839 if (err)
5840 goto done;
5842 done:
5843 free(tmp_branch_name);
5844 free(new_base_branch_ref_name);
5845 free(branch_ref_name);
5846 free(commit_ref_name);
5847 return err;
5850 const struct got_error *
5851 got_worktree_rebase_complete(struct got_worktree *worktree,
5852 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5853 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5854 struct got_repository *repo)
5856 const struct got_error *err, *unlockerr;
5857 struct got_object_id *new_head_commit_id = NULL;
5859 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5860 if (err)
5861 return err;
5863 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5864 if (err)
5865 goto done;
5867 err = got_ref_write(rebased_branch, repo);
5868 if (err)
5869 goto done;
5871 err = got_worktree_set_head_ref(worktree, rebased_branch);
5872 if (err)
5873 goto done;
5875 err = delete_rebase_refs(worktree, repo);
5876 done:
5877 if (fileindex)
5878 got_fileindex_free(fileindex);
5879 free(new_head_commit_id);
5880 unlockerr = lock_worktree(worktree, LOCK_SH);
5881 if (unlockerr && err == NULL)
5882 err = unlockerr;
5883 return err;
5886 const struct got_error *
5887 got_worktree_rebase_abort(struct got_worktree *worktree,
5888 struct got_fileindex *fileindex, struct got_repository *repo,
5889 struct got_reference *new_base_branch,
5890 got_worktree_checkout_cb progress_cb, void *progress_arg)
5892 const struct got_error *err, *unlockerr, *sync_err;
5893 struct got_reference *resolved = NULL;
5894 struct got_object_id *commit_id = NULL;
5895 char *fileindex_path = NULL;
5896 struct revert_file_args rfa;
5897 struct got_object_id *tree_id = NULL;
5899 err = lock_worktree(worktree, LOCK_EX);
5900 if (err)
5901 return err;
5903 err = got_ref_open(&resolved, repo,
5904 got_ref_get_symref_target(new_base_branch), 0);
5905 if (err)
5906 goto done;
5908 err = got_worktree_set_head_ref(worktree, resolved);
5909 if (err)
5910 goto done;
5913 * XXX commits to the base branch could have happened while
5914 * we were busy rebasing; should we store the original commit ID
5915 * when rebase begins and read it back here?
5917 err = got_ref_resolve(&commit_id, repo, resolved);
5918 if (err)
5919 goto done;
5921 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5922 if (err)
5923 goto done;
5925 err = got_object_id_by_path(&tree_id, repo,
5926 worktree->base_commit_id, worktree->path_prefix);
5927 if (err)
5928 goto done;
5930 err = delete_rebase_refs(worktree, repo);
5931 if (err)
5932 goto done;
5934 err = get_fileindex_path(&fileindex_path, worktree);
5935 if (err)
5936 goto done;
5938 rfa.worktree = worktree;
5939 rfa.fileindex = fileindex;
5940 rfa.progress_cb = progress_cb;
5941 rfa.progress_arg = progress_arg;
5942 rfa.patch_cb = NULL;
5943 rfa.patch_arg = NULL;
5944 rfa.repo = repo;
5945 err = worktree_status(worktree, "", fileindex, repo,
5946 revert_file, &rfa, NULL, NULL, 0, 0);
5947 if (err)
5948 goto sync;
5950 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5951 repo, progress_cb, progress_arg, NULL, NULL);
5952 sync:
5953 sync_err = sync_fileindex(fileindex, fileindex_path);
5954 if (sync_err && err == NULL)
5955 err = sync_err;
5956 done:
5957 got_ref_close(resolved);
5958 free(tree_id);
5959 free(commit_id);
5960 if (fileindex)
5961 got_fileindex_free(fileindex);
5962 free(fileindex_path);
5964 unlockerr = lock_worktree(worktree, LOCK_SH);
5965 if (unlockerr && err == NULL)
5966 err = unlockerr;
5967 return err;
5970 const struct got_error *
5971 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5972 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5973 struct got_fileindex **fileindex, struct got_worktree *worktree,
5974 struct got_repository *repo)
5976 const struct got_error *err = NULL;
5977 char *tmp_branch_name = NULL;
5978 char *branch_ref_name = NULL;
5979 char *base_commit_ref_name = NULL;
5980 char *fileindex_path = NULL;
5981 struct check_rebase_ok_arg ok_arg;
5982 struct got_reference *wt_branch = NULL;
5983 struct got_reference *base_commit_ref = NULL;
5985 *tmp_branch = NULL;
5986 *branch_ref = NULL;
5987 *base_commit_id = NULL;
5988 *fileindex = NULL;
5990 err = lock_worktree(worktree, LOCK_EX);
5991 if (err)
5992 return err;
5994 err = open_fileindex(fileindex, &fileindex_path, worktree);
5995 if (err)
5996 goto done;
5998 ok_arg.worktree = worktree;
5999 ok_arg.repo = repo;
6000 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6001 &ok_arg);
6002 if (err)
6003 goto done;
6005 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6006 if (err)
6007 goto done;
6009 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6010 if (err)
6011 goto done;
6013 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6014 worktree);
6015 if (err)
6016 goto done;
6018 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6019 0);
6020 if (err)
6021 goto done;
6023 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6024 if (err)
6025 goto done;
6027 err = got_ref_write(*branch_ref, repo);
6028 if (err)
6029 goto done;
6031 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6032 worktree->base_commit_id);
6033 if (err)
6034 goto done;
6035 err = got_ref_write(base_commit_ref, repo);
6036 if (err)
6037 goto done;
6038 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6039 if (*base_commit_id == NULL) {
6040 err = got_error_from_errno("got_object_id_dup");
6041 goto done;
6044 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6045 worktree->base_commit_id);
6046 if (err)
6047 goto done;
6048 err = got_ref_write(*tmp_branch, repo);
6049 if (err)
6050 goto done;
6052 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6053 if (err)
6054 goto done;
6055 done:
6056 free(fileindex_path);
6057 free(tmp_branch_name);
6058 free(branch_ref_name);
6059 free(base_commit_ref_name);
6060 if (wt_branch)
6061 got_ref_close(wt_branch);
6062 if (err) {
6063 if (*branch_ref) {
6064 got_ref_close(*branch_ref);
6065 *branch_ref = NULL;
6067 if (*tmp_branch) {
6068 got_ref_close(*tmp_branch);
6069 *tmp_branch = NULL;
6071 free(*base_commit_id);
6072 if (*fileindex) {
6073 got_fileindex_free(*fileindex);
6074 *fileindex = NULL;
6076 lock_worktree(worktree, LOCK_SH);
6078 return err;
6081 const struct got_error *
6082 got_worktree_histedit_postpone(struct got_worktree *worktree,
6083 struct got_fileindex *fileindex)
6085 if (fileindex)
6086 got_fileindex_free(fileindex);
6087 return lock_worktree(worktree, LOCK_SH);
6090 const struct got_error *
6091 got_worktree_histedit_in_progress(int *in_progress,
6092 struct got_worktree *worktree)
6094 const struct got_error *err;
6095 char *tmp_branch_name = NULL;
6097 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6098 if (err)
6099 return err;
6101 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6102 free(tmp_branch_name);
6103 return NULL;
6106 const struct got_error *
6107 got_worktree_histedit_continue(struct got_object_id **commit_id,
6108 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6109 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6110 struct got_worktree *worktree, struct got_repository *repo)
6112 const struct got_error *err;
6113 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6114 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6115 struct got_reference *commit_ref = NULL;
6116 struct got_reference *base_commit_ref = NULL;
6117 char *fileindex_path = NULL;
6118 int have_staged_files = 0;
6120 *commit_id = NULL;
6121 *tmp_branch = NULL;
6122 *base_commit_id = NULL;
6123 *fileindex = NULL;
6125 err = lock_worktree(worktree, LOCK_EX);
6126 if (err)
6127 return err;
6129 err = open_fileindex(fileindex, &fileindex_path, worktree);
6130 if (err)
6131 goto done;
6133 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6134 &have_staged_files);
6135 if (err && err->code != GOT_ERR_CANCELLED)
6136 goto done;
6137 if (have_staged_files) {
6138 err = got_error(GOT_ERR_STAGED_PATHS);
6139 goto done;
6142 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6143 if (err)
6144 goto done;
6146 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6147 if (err)
6148 goto done;
6150 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6151 if (err)
6152 goto done;
6154 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6155 worktree);
6156 if (err)
6157 goto done;
6159 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6160 if (err)
6161 goto done;
6163 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6164 if (err)
6165 goto done;
6166 err = got_ref_resolve(commit_id, repo, commit_ref);
6167 if (err)
6168 goto done;
6170 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6171 if (err)
6172 goto done;
6173 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6174 if (err)
6175 goto done;
6177 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6178 if (err)
6179 goto done;
6180 done:
6181 free(commit_ref_name);
6182 free(branch_ref_name);
6183 free(fileindex_path);
6184 if (commit_ref)
6185 got_ref_close(commit_ref);
6186 if (base_commit_ref)
6187 got_ref_close(base_commit_ref);
6188 if (err) {
6189 free(*commit_id);
6190 *commit_id = NULL;
6191 free(*base_commit_id);
6192 *base_commit_id = NULL;
6193 if (*tmp_branch) {
6194 got_ref_close(*tmp_branch);
6195 *tmp_branch = NULL;
6197 if (*fileindex) {
6198 got_fileindex_free(*fileindex);
6199 *fileindex = NULL;
6201 lock_worktree(worktree, LOCK_EX);
6203 return err;
6206 static const struct got_error *
6207 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6209 const struct got_error *err;
6210 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6211 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6213 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6214 if (err)
6215 goto done;
6216 err = delete_ref(tmp_branch_name, repo);
6217 if (err)
6218 goto done;
6220 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6221 worktree);
6222 if (err)
6223 goto done;
6224 err = delete_ref(base_commit_ref_name, repo);
6225 if (err)
6226 goto done;
6228 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6229 if (err)
6230 goto done;
6231 err = delete_ref(branch_ref_name, repo);
6232 if (err)
6233 goto done;
6235 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6236 if (err)
6237 goto done;
6238 err = delete_ref(commit_ref_name, repo);
6239 if (err)
6240 goto done;
6241 done:
6242 free(tmp_branch_name);
6243 free(base_commit_ref_name);
6244 free(branch_ref_name);
6245 free(commit_ref_name);
6246 return err;
6249 const struct got_error *
6250 got_worktree_histedit_abort(struct got_worktree *worktree,
6251 struct got_fileindex *fileindex, struct got_repository *repo,
6252 struct got_reference *branch, struct got_object_id *base_commit_id,
6253 got_worktree_checkout_cb progress_cb, void *progress_arg)
6255 const struct got_error *err, *unlockerr, *sync_err;
6256 struct got_reference *resolved = NULL;
6257 char *fileindex_path = NULL;
6258 struct got_object_id *tree_id = NULL;
6259 struct revert_file_args rfa;
6261 err = lock_worktree(worktree, LOCK_EX);
6262 if (err)
6263 return err;
6265 err = got_ref_open(&resolved, repo,
6266 got_ref_get_symref_target(branch), 0);
6267 if (err)
6268 goto done;
6270 err = got_worktree_set_head_ref(worktree, resolved);
6271 if (err)
6272 goto done;
6274 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6275 if (err)
6276 goto done;
6278 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6279 worktree->path_prefix);
6280 if (err)
6281 goto done;
6283 err = delete_histedit_refs(worktree, repo);
6284 if (err)
6285 goto done;
6287 err = get_fileindex_path(&fileindex_path, worktree);
6288 if (err)
6289 goto done;
6291 rfa.worktree = worktree;
6292 rfa.fileindex = fileindex;
6293 rfa.progress_cb = progress_cb;
6294 rfa.progress_arg = progress_arg;
6295 rfa.patch_cb = NULL;
6296 rfa.patch_arg = NULL;
6297 rfa.repo = repo;
6298 err = worktree_status(worktree, "", fileindex, repo,
6299 revert_file, &rfa, NULL, NULL, 0, 0);
6300 if (err)
6301 goto sync;
6303 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6304 repo, progress_cb, progress_arg, NULL, NULL);
6305 sync:
6306 sync_err = sync_fileindex(fileindex, fileindex_path);
6307 if (sync_err && err == NULL)
6308 err = sync_err;
6309 done:
6310 got_ref_close(resolved);
6311 free(tree_id);
6312 free(fileindex_path);
6314 unlockerr = lock_worktree(worktree, LOCK_SH);
6315 if (unlockerr && err == NULL)
6316 err = unlockerr;
6317 return err;
6320 const struct got_error *
6321 got_worktree_histedit_complete(struct got_worktree *worktree,
6322 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6323 struct got_reference *edited_branch, struct got_repository *repo)
6325 const struct got_error *err, *unlockerr;
6326 struct got_object_id *new_head_commit_id = NULL;
6327 struct got_reference *resolved = NULL;
6329 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6330 if (err)
6331 return err;
6333 err = got_ref_open(&resolved, repo,
6334 got_ref_get_symref_target(edited_branch), 0);
6335 if (err)
6336 goto done;
6338 err = got_ref_change_ref(resolved, new_head_commit_id);
6339 if (err)
6340 goto done;
6342 err = got_ref_write(resolved, repo);
6343 if (err)
6344 goto done;
6346 err = got_worktree_set_head_ref(worktree, resolved);
6347 if (err)
6348 goto done;
6350 err = delete_histedit_refs(worktree, repo);
6351 done:
6352 if (fileindex)
6353 got_fileindex_free(fileindex);
6354 free(new_head_commit_id);
6355 unlockerr = lock_worktree(worktree, LOCK_SH);
6356 if (unlockerr && err == NULL)
6357 err = unlockerr;
6358 return err;
6361 const struct got_error *
6362 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6363 struct got_object_id *commit_id, struct got_repository *repo)
6365 const struct got_error *err;
6366 char *commit_ref_name;
6368 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6369 if (err)
6370 return err;
6372 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6373 if (err)
6374 goto done;
6376 err = delete_ref(commit_ref_name, repo);
6377 done:
6378 free(commit_ref_name);
6379 return err;
6382 const struct got_error *
6383 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6384 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6385 struct got_worktree *worktree, const char *refname,
6386 struct got_repository *repo)
6388 const struct got_error *err = NULL;
6389 char *fileindex_path = NULL;
6390 struct check_rebase_ok_arg ok_arg;
6392 *fileindex = NULL;
6393 *branch_ref = NULL;
6394 *base_branch_ref = NULL;
6396 err = lock_worktree(worktree, LOCK_EX);
6397 if (err)
6398 return err;
6400 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6401 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6402 "cannot integrate a branch into itself; "
6403 "update -b or different branch name required");
6404 goto done;
6407 err = open_fileindex(fileindex, &fileindex_path, worktree);
6408 if (err)
6409 goto done;
6411 /* Preconditions are the same as for rebase. */
6412 ok_arg.worktree = worktree;
6413 ok_arg.repo = repo;
6414 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6415 &ok_arg);
6416 if (err)
6417 goto done;
6419 err = got_ref_open(branch_ref, repo, refname, 1);
6420 if (err)
6421 goto done;
6423 err = got_ref_open(base_branch_ref, repo,
6424 got_worktree_get_head_ref_name(worktree), 1);
6425 done:
6426 if (err) {
6427 if (*branch_ref) {
6428 got_ref_close(*branch_ref);
6429 *branch_ref = NULL;
6431 if (*base_branch_ref) {
6432 got_ref_close(*base_branch_ref);
6433 *base_branch_ref = NULL;
6435 if (*fileindex) {
6436 got_fileindex_free(*fileindex);
6437 *fileindex = NULL;
6439 lock_worktree(worktree, LOCK_SH);
6441 return err;
6444 const struct got_error *
6445 got_worktree_integrate_continue(struct got_worktree *worktree,
6446 struct got_fileindex *fileindex, struct got_repository *repo,
6447 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6448 got_worktree_checkout_cb progress_cb, void *progress_arg,
6449 got_cancel_cb cancel_cb, void *cancel_arg)
6451 const struct got_error *err = NULL, *sync_err, *unlockerr;
6452 char *fileindex_path = NULL;
6453 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6455 err = get_fileindex_path(&fileindex_path, worktree);
6456 if (err)
6457 goto done;
6459 err = got_ref_resolve(&commit_id, repo, branch_ref);
6460 if (err)
6461 goto done;
6463 err = got_object_id_by_path(&tree_id, repo, commit_id,
6464 worktree->path_prefix);
6465 if (err)
6466 goto done;
6468 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6469 if (err)
6470 goto done;
6472 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6473 progress_cb, progress_arg, cancel_cb, cancel_arg);
6474 if (err)
6475 goto sync;
6477 err = got_ref_change_ref(base_branch_ref, commit_id);
6478 if (err)
6479 goto sync;
6481 err = got_ref_write(base_branch_ref, repo);
6482 sync:
6483 sync_err = sync_fileindex(fileindex, fileindex_path);
6484 if (sync_err && err == NULL)
6485 err = sync_err;
6487 done:
6488 unlockerr = got_ref_unlock(branch_ref);
6489 if (unlockerr && err == NULL)
6490 err = unlockerr;
6491 got_ref_close(branch_ref);
6493 unlockerr = got_ref_unlock(base_branch_ref);
6494 if (unlockerr && err == NULL)
6495 err = unlockerr;
6496 got_ref_close(base_branch_ref);
6498 got_fileindex_free(fileindex);
6499 free(fileindex_path);
6500 free(tree_id);
6502 unlockerr = lock_worktree(worktree, LOCK_SH);
6503 if (unlockerr && err == NULL)
6504 err = unlockerr;
6505 return err;
6508 const struct got_error *
6509 got_worktree_integrate_abort(struct got_worktree *worktree,
6510 struct got_fileindex *fileindex, struct got_repository *repo,
6511 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6513 const struct got_error *err = NULL, *unlockerr = NULL;
6515 got_fileindex_free(fileindex);
6517 err = lock_worktree(worktree, LOCK_SH);
6519 unlockerr = got_ref_unlock(branch_ref);
6520 if (unlockerr && err == NULL)
6521 err = unlockerr;
6522 got_ref_close(branch_ref);
6524 unlockerr = got_ref_unlock(base_branch_ref);
6525 if (unlockerr && err == NULL)
6526 err = unlockerr;
6527 got_ref_close(base_branch_ref);
6529 return err;
6532 struct check_stage_ok_arg {
6533 struct got_object_id *head_commit_id;
6534 struct got_worktree *worktree;
6535 struct got_fileindex *fileindex;
6536 struct got_repository *repo;
6537 int have_changes;
6540 const struct got_error *
6541 check_stage_ok(void *arg, unsigned char status,
6542 unsigned char staged_status, const char *relpath,
6543 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6544 struct got_object_id *commit_id, int dirfd, const char *de_name)
6546 struct check_stage_ok_arg *a = arg;
6547 const struct got_error *err = NULL;
6548 struct got_fileindex_entry *ie;
6549 struct got_object_id base_commit_id;
6550 struct got_object_id *base_commit_idp = NULL;
6551 char *in_repo_path = NULL, *p;
6553 if (status == GOT_STATUS_UNVERSIONED ||
6554 status == GOT_STATUS_NO_CHANGE)
6555 return NULL;
6556 if (status == GOT_STATUS_NONEXISTENT)
6557 return got_error_set_errno(ENOENT, relpath);
6559 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6560 if (ie == NULL)
6561 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6563 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6564 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6565 relpath) == -1)
6566 return got_error_from_errno("asprintf");
6568 if (got_fileindex_entry_has_commit(ie)) {
6569 memcpy(base_commit_id.sha1, ie->commit_sha1,
6570 SHA1_DIGEST_LENGTH);
6571 base_commit_idp = &base_commit_id;
6574 if (status == GOT_STATUS_CONFLICT) {
6575 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6576 goto done;
6577 } else if (status != GOT_STATUS_ADD &&
6578 status != GOT_STATUS_MODIFY &&
6579 status != GOT_STATUS_DELETE) {
6580 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6581 goto done;
6584 a->have_changes = 1;
6586 p = in_repo_path;
6587 while (p[0] == '/')
6588 p++;
6589 err = check_out_of_date(p, status, staged_status,
6590 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6591 GOT_ERR_STAGE_OUT_OF_DATE);
6592 done:
6593 free(in_repo_path);
6594 return err;
6597 struct stage_path_arg {
6598 struct got_worktree *worktree;
6599 struct got_fileindex *fileindex;
6600 struct got_repository *repo;
6601 got_worktree_status_cb status_cb;
6602 void *status_arg;
6603 got_worktree_patch_cb patch_cb;
6604 void *patch_arg;
6605 int staged_something;
6608 static const struct got_error *
6609 stage_path(void *arg, unsigned char status,
6610 unsigned char staged_status, const char *relpath,
6611 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6612 struct got_object_id *commit_id, int dirfd, const char *de_name)
6614 struct stage_path_arg *a = arg;
6615 const struct got_error *err = NULL;
6616 struct got_fileindex_entry *ie;
6617 char *ondisk_path = NULL, *path_content = NULL;
6618 uint32_t stage;
6619 struct got_object_id *new_staged_blob_id = NULL;
6621 if (status == GOT_STATUS_UNVERSIONED)
6622 return NULL;
6624 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6625 if (ie == NULL)
6626 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6628 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6629 relpath)== -1)
6630 return got_error_from_errno("asprintf");
6632 switch (status) {
6633 case GOT_STATUS_ADD:
6634 case GOT_STATUS_MODIFY:
6635 if (a->patch_cb) {
6636 if (status == GOT_STATUS_ADD) {
6637 int choice = GOT_PATCH_CHOICE_NONE;
6638 err = (*a->patch_cb)(&choice, a->patch_arg,
6639 status, ie->path, NULL, 1, 1);
6640 if (err)
6641 break;
6642 if (choice != GOT_PATCH_CHOICE_YES)
6643 break;
6644 } else {
6645 err = create_patched_content(&path_content, 0,
6646 staged_blob_id ? staged_blob_id : blob_id,
6647 ondisk_path, dirfd, de_name, ie->path,
6648 a->repo, a->patch_cb, a->patch_arg);
6649 if (err || path_content == NULL)
6650 break;
6653 err = got_object_blob_create(&new_staged_blob_id,
6654 path_content ? path_content : ondisk_path, a->repo);
6655 if (err)
6656 break;
6657 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6658 SHA1_DIGEST_LENGTH);
6659 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6660 stage = GOT_FILEIDX_STAGE_ADD;
6661 else
6662 stage = GOT_FILEIDX_STAGE_MODIFY;
6663 got_fileindex_entry_stage_set(ie, stage);
6664 a->staged_something = 1;
6665 if (a->status_cb == NULL)
6666 break;
6667 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6668 get_staged_status(ie), relpath, blob_id,
6669 new_staged_blob_id, NULL, dirfd, de_name);
6670 break;
6671 case GOT_STATUS_DELETE:
6672 if (staged_status == GOT_STATUS_DELETE)
6673 break;
6674 if (a->patch_cb) {
6675 int choice = GOT_PATCH_CHOICE_NONE;
6676 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6677 ie->path, NULL, 1, 1);
6678 if (err)
6679 break;
6680 if (choice == GOT_PATCH_CHOICE_NO)
6681 break;
6682 if (choice != GOT_PATCH_CHOICE_YES) {
6683 err = got_error(GOT_ERR_PATCH_CHOICE);
6684 break;
6687 stage = GOT_FILEIDX_STAGE_DELETE;
6688 got_fileindex_entry_stage_set(ie, stage);
6689 a->staged_something = 1;
6690 if (a->status_cb == NULL)
6691 break;
6692 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6693 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6694 de_name);
6695 break;
6696 case GOT_STATUS_NO_CHANGE:
6697 break;
6698 case GOT_STATUS_CONFLICT:
6699 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6700 break;
6701 case GOT_STATUS_NONEXISTENT:
6702 err = got_error_set_errno(ENOENT, relpath);
6703 break;
6704 default:
6705 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6706 break;
6709 if (path_content && unlink(path_content) == -1 && err == NULL)
6710 err = got_error_from_errno2("unlink", path_content);
6711 free(path_content);
6712 free(ondisk_path);
6713 free(new_staged_blob_id);
6714 return err;
6717 const struct got_error *
6718 got_worktree_stage(struct got_worktree *worktree,
6719 struct got_pathlist_head *paths,
6720 got_worktree_status_cb status_cb, void *status_arg,
6721 got_worktree_patch_cb patch_cb, void *patch_arg,
6722 struct got_repository *repo)
6724 const struct got_error *err = NULL, *sync_err, *unlockerr;
6725 struct got_pathlist_entry *pe;
6726 struct got_fileindex *fileindex = NULL;
6727 char *fileindex_path = NULL;
6728 struct got_reference *head_ref = NULL;
6729 struct got_object_id *head_commit_id = NULL;
6730 struct check_stage_ok_arg oka;
6731 struct stage_path_arg spa;
6733 err = lock_worktree(worktree, LOCK_EX);
6734 if (err)
6735 return err;
6737 err = got_ref_open(&head_ref, repo,
6738 got_worktree_get_head_ref_name(worktree), 0);
6739 if (err)
6740 goto done;
6741 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6742 if (err)
6743 goto done;
6744 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6745 if (err)
6746 goto done;
6748 /* Check pre-conditions before staging anything. */
6749 oka.head_commit_id = head_commit_id;
6750 oka.worktree = worktree;
6751 oka.fileindex = fileindex;
6752 oka.repo = repo;
6753 oka.have_changes = 0;
6754 TAILQ_FOREACH(pe, paths, entry) {
6755 err = worktree_status(worktree, pe->path, fileindex, repo,
6756 check_stage_ok, &oka, NULL, NULL, 0, 0);
6757 if (err)
6758 goto done;
6760 if (!oka.have_changes) {
6761 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6762 goto done;
6765 spa.worktree = worktree;
6766 spa.fileindex = fileindex;
6767 spa.repo = repo;
6768 spa.patch_cb = patch_cb;
6769 spa.patch_arg = patch_arg;
6770 spa.status_cb = status_cb;
6771 spa.status_arg = status_arg;
6772 spa.staged_something = 0;
6773 TAILQ_FOREACH(pe, paths, entry) {
6774 err = worktree_status(worktree, pe->path, fileindex, repo,
6775 stage_path, &spa, NULL, NULL, 0, 0);
6776 if (err)
6777 goto done;
6779 if (!spa.staged_something) {
6780 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6781 goto done;
6784 sync_err = sync_fileindex(fileindex, fileindex_path);
6785 if (sync_err && err == NULL)
6786 err = sync_err;
6787 done:
6788 if (head_ref)
6789 got_ref_close(head_ref);
6790 free(head_commit_id);
6791 free(fileindex_path);
6792 if (fileindex)
6793 got_fileindex_free(fileindex);
6794 unlockerr = lock_worktree(worktree, LOCK_SH);
6795 if (unlockerr && err == NULL)
6796 err = unlockerr;
6797 return err;
6800 struct unstage_path_arg {
6801 struct got_worktree *worktree;
6802 struct got_fileindex *fileindex;
6803 struct got_repository *repo;
6804 got_worktree_checkout_cb progress_cb;
6805 void *progress_arg;
6806 got_worktree_patch_cb patch_cb;
6807 void *patch_arg;
6810 static const struct got_error *
6811 create_unstaged_content(char **path_unstaged_content,
6812 char **path_new_staged_content, struct got_object_id *blob_id,
6813 struct got_object_id *staged_blob_id, const char *relpath,
6814 struct got_repository *repo,
6815 got_worktree_patch_cb patch_cb, void *patch_arg)
6817 const struct got_error *err;
6818 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6819 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6820 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6821 struct stat sb1, sb2;
6822 struct got_diff_changes *changes = NULL;
6823 struct got_diff_state *ds = NULL;
6824 struct got_diff_args *args = NULL;
6825 struct got_diff_change *change;
6826 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6827 int have_content = 0, have_rejected_content = 0;
6829 *path_unstaged_content = NULL;
6830 *path_new_staged_content = NULL;
6832 err = got_object_id_str(&label1, blob_id);
6833 if (err)
6834 return err;
6835 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6836 if (err)
6837 goto done;
6839 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6840 if (err)
6841 goto done;
6843 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6844 if (err)
6845 goto done;
6847 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6848 if (err)
6849 goto done;
6851 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6852 if (err)
6853 goto done;
6855 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6856 if (err)
6857 goto done;
6859 if (stat(path1, &sb1) == -1) {
6860 err = got_error_from_errno2("stat", path1);
6861 goto done;
6864 if (stat(path2, &sb2) == -1) {
6865 err = got_error_from_errno2("stat", path2);
6866 goto done;
6869 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6870 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6871 if (err)
6872 goto done;
6874 err = got_opentemp_named(path_unstaged_content, &outfile,
6875 "got-unstaged-content");
6876 if (err)
6877 goto done;
6878 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6879 "got-new-staged-content");
6880 if (err)
6881 goto done;
6883 if (fseek(f1, 0L, SEEK_SET) == -1) {
6884 err = got_ferror(f1, GOT_ERR_IO);
6885 goto done;
6887 if (fseek(f2, 0L, SEEK_SET) == -1) {
6888 err = got_ferror(f2, GOT_ERR_IO);
6889 goto done;
6891 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6892 int choice;
6893 err = apply_or_reject_change(&choice, change, ++n,
6894 changes->nchanges, ds, args, diff_flags, relpath,
6895 f1, f2, &line_cur1, &line_cur2,
6896 outfile, rejectfile, patch_cb, patch_arg);
6897 if (err)
6898 goto done;
6899 if (choice == GOT_PATCH_CHOICE_YES)
6900 have_content = 1;
6901 else
6902 have_rejected_content = 1;
6903 if (choice == GOT_PATCH_CHOICE_QUIT)
6904 break;
6906 if (have_content || have_rejected_content)
6907 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6908 outfile, rejectfile);
6909 done:
6910 free(label1);
6911 if (blob)
6912 got_object_blob_close(blob);
6913 if (staged_blob)
6914 got_object_blob_close(staged_blob);
6915 if (f1 && fclose(f1) == EOF && err == NULL)
6916 err = got_error_from_errno2("fclose", path1);
6917 if (f2 && fclose(f2) == EOF && err == NULL)
6918 err = got_error_from_errno2("fclose", path2);
6919 if (outfile && fclose(outfile) == EOF && err == NULL)
6920 err = got_error_from_errno2("fclose", *path_unstaged_content);
6921 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6922 err = got_error_from_errno2("fclose", *path_new_staged_content);
6923 if (path1 && unlink(path1) == -1 && err == NULL)
6924 err = got_error_from_errno2("unlink", path1);
6925 if (path2 && unlink(path2) == -1 && err == NULL)
6926 err = got_error_from_errno2("unlink", path2);
6927 if (err || !have_content) {
6928 if (*path_unstaged_content &&
6929 unlink(*path_unstaged_content) == -1 && err == NULL)
6930 err = got_error_from_errno2("unlink",
6931 *path_unstaged_content);
6932 free(*path_unstaged_content);
6933 *path_unstaged_content = NULL;
6935 if (err || !have_rejected_content) {
6936 if (*path_new_staged_content &&
6937 unlink(*path_new_staged_content) == -1 && err == NULL)
6938 err = got_error_from_errno2("unlink",
6939 *path_new_staged_content);
6940 free(*path_new_staged_content);
6941 *path_new_staged_content = NULL;
6943 free(args);
6944 if (ds) {
6945 got_diff_state_free(ds);
6946 free(ds);
6948 if (changes)
6949 got_diff_free_changes(changes);
6950 free(path1);
6951 free(path2);
6952 return err;
6955 static const struct got_error *
6956 unstage_path(void *arg, unsigned char status,
6957 unsigned char staged_status, const char *relpath,
6958 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6959 struct got_object_id *commit_id, int dirfd, const char *de_name)
6961 const struct got_error *err = NULL;
6962 struct unstage_path_arg *a = arg;
6963 struct got_fileindex_entry *ie;
6964 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6965 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6966 char *path_new_staged_content = NULL;
6967 char *id_str = NULL, *label_orig = NULL;
6968 int local_changes_subsumed;
6969 struct stat sb;
6971 if (staged_status != GOT_STATUS_ADD &&
6972 staged_status != GOT_STATUS_MODIFY &&
6973 staged_status != GOT_STATUS_DELETE)
6974 return NULL;
6976 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6977 if (ie == NULL)
6978 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6980 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6981 == -1)
6982 return got_error_from_errno("asprintf");
6984 err = got_object_id_str(&id_str,
6985 commit_id ? commit_id : a->worktree->base_commit_id);
6986 if (err)
6987 goto done;
6988 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6989 id_str) == -1) {
6990 err = got_error_from_errno("asprintf");
6991 goto done;
6994 switch (staged_status) {
6995 case GOT_STATUS_MODIFY:
6996 err = got_object_open_as_blob(&blob_base, a->repo,
6997 blob_id, 8192);
6998 if (err)
6999 break;
7000 /* fall through */
7001 case GOT_STATUS_ADD:
7002 if (a->patch_cb) {
7003 if (staged_status == GOT_STATUS_ADD) {
7004 int choice = GOT_PATCH_CHOICE_NONE;
7005 err = (*a->patch_cb)(&choice, a->patch_arg,
7006 staged_status, ie->path, NULL, 1, 1);
7007 if (err)
7008 break;
7009 if (choice != GOT_PATCH_CHOICE_YES)
7010 break;
7011 } else {
7012 err = create_unstaged_content(
7013 &path_unstaged_content,
7014 &path_new_staged_content, blob_id,
7015 staged_blob_id, ie->path, a->repo,
7016 a->patch_cb, a->patch_arg);
7017 if (err || path_unstaged_content == NULL)
7018 break;
7019 if (path_new_staged_content) {
7020 err = got_object_blob_create(
7021 &staged_blob_id,
7022 path_new_staged_content,
7023 a->repo);
7024 if (err)
7025 break;
7026 memcpy(ie->staged_blob_sha1,
7027 staged_blob_id->sha1,
7028 SHA1_DIGEST_LENGTH);
7030 err = merge_file(&local_changes_subsumed,
7031 a->worktree, blob_base, ondisk_path,
7032 relpath, got_fileindex_perms_to_st(ie),
7033 path_unstaged_content, label_orig,
7034 "unstaged", a->repo, a->progress_cb,
7035 a->progress_arg);
7036 if (err == NULL &&
7037 path_new_staged_content == NULL)
7038 got_fileindex_entry_stage_set(ie,
7039 GOT_FILEIDX_STAGE_NONE);
7040 break; /* Done with this file. */
7043 err = got_object_open_as_blob(&blob_staged, a->repo,
7044 staged_blob_id, 8192);
7045 if (err)
7046 break;
7047 err = merge_blob(&local_changes_subsumed, a->worktree,
7048 blob_base, ondisk_path, relpath,
7049 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7050 commit_id ? commit_id : a->worktree->base_commit_id,
7051 a->repo, a->progress_cb, a->progress_arg);
7052 if (err == NULL)
7053 got_fileindex_entry_stage_set(ie,
7054 GOT_FILEIDX_STAGE_NONE);
7055 break;
7056 case GOT_STATUS_DELETE:
7057 if (a->patch_cb) {
7058 int choice = GOT_PATCH_CHOICE_NONE;
7059 err = (*a->patch_cb)(&choice, a->patch_arg,
7060 staged_status, ie->path, NULL, 1, 1);
7061 if (err)
7062 break;
7063 if (choice == GOT_PATCH_CHOICE_NO)
7064 break;
7065 if (choice != GOT_PATCH_CHOICE_YES) {
7066 err = got_error(GOT_ERR_PATCH_CHOICE);
7067 break;
7070 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7071 err = get_file_status(&status, &sb, ie, ondisk_path,
7072 dirfd, de_name, a->repo);
7073 if (err)
7074 break;
7075 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7076 break;
7078 done:
7079 free(ondisk_path);
7080 if (path_unstaged_content &&
7081 unlink(path_unstaged_content) == -1 && err == NULL)
7082 err = got_error_from_errno2("unlink", path_unstaged_content);
7083 if (path_new_staged_content &&
7084 unlink(path_new_staged_content) == -1 && err == NULL)
7085 err = got_error_from_errno2("unlink", path_new_staged_content);
7086 free(path_unstaged_content);
7087 free(path_new_staged_content);
7088 if (blob_base)
7089 got_object_blob_close(blob_base);
7090 if (blob_staged)
7091 got_object_blob_close(blob_staged);
7092 free(id_str);
7093 free(label_orig);
7094 return err;
7097 const struct got_error *
7098 got_worktree_unstage(struct got_worktree *worktree,
7099 struct got_pathlist_head *paths,
7100 got_worktree_checkout_cb progress_cb, void *progress_arg,
7101 got_worktree_patch_cb patch_cb, void *patch_arg,
7102 struct got_repository *repo)
7104 const struct got_error *err = NULL, *sync_err, *unlockerr;
7105 struct got_pathlist_entry *pe;
7106 struct got_fileindex *fileindex = NULL;
7107 char *fileindex_path = NULL;
7108 struct unstage_path_arg upa;
7110 err = lock_worktree(worktree, LOCK_EX);
7111 if (err)
7112 return err;
7114 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7115 if (err)
7116 goto done;
7118 upa.worktree = worktree;
7119 upa.fileindex = fileindex;
7120 upa.repo = repo;
7121 upa.progress_cb = progress_cb;
7122 upa.progress_arg = progress_arg;
7123 upa.patch_cb = patch_cb;
7124 upa.patch_arg = patch_arg;
7125 TAILQ_FOREACH(pe, paths, entry) {
7126 err = worktree_status(worktree, pe->path, fileindex, repo,
7127 unstage_path, &upa, NULL, NULL, 0, 0);
7128 if (err)
7129 goto done;
7132 sync_err = sync_fileindex(fileindex, fileindex_path);
7133 if (sync_err && err == NULL)
7134 err = sync_err;
7135 done:
7136 free(fileindex_path);
7137 if (fileindex)
7138 got_fileindex_free(fileindex);
7139 unlockerr = lock_worktree(worktree, LOCK_SH);
7140 if (unlockerr && err == NULL)
7141 err = unlockerr;
7142 return err;