Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
68 {
69 const struct got_error *err = NULL;
70 char *path;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
76 free(path);
77 return err;
78 }
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
82 {
83 const struct got_error *err = NULL;
84 FILE *tmpfile = NULL;
85 char *tmppath = NULL;
86 char *path = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
90 path = NULL;
91 goto done;
92 }
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
95 if (err)
96 goto done;
98 if (content) {
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
102 goto done;
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
108 unlink(tmppath);
109 goto done;
112 done:
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
115 free(tmppath);
116 return err;
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
123 char *path;
124 int fd = -1;
125 ssize_t n;
126 struct stat sb;
128 *content = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
132 path = NULL;
133 goto done;
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
137 if (fd == -1) {
138 if (errno == ENOENT)
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
140 else
141 err = got_error_from_errno2("open", path);
142 goto done;
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
147 goto done;
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
152 goto done;
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
157 goto done;
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
164 goto done;
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
168 goto done;
170 (*content)[sb.st_size - 1] = '\0';
172 done:
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
175 free(path);
176 if (err) {
177 free(*content);
178 *content = NULL;
180 return err;
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
187 char *refstr = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
191 if (refstr == NULL)
192 return got_error_from_errno("got_ref_to_str");
193 } else {
194 refstr = strdup(got_ref_get_name(head_ref));
195 if (refstr == NULL)
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 free(refstr);
200 return err;
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
209 uuid_t uuid;
210 uint32_t uuid_status;
211 int obj_type;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
220 goto done;
223 err = got_ref_resolve(&commit_id, repo, head_ref);
224 if (err)
225 return err;
226 err = got_object_get_type(&obj_type, repo, commit_id);
227 if (err)
228 return err;
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
240 goto done;
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
246 goto done;
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
250 goto done;
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
255 if (err)
256 goto done;
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
260 if (err)
261 goto done;
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
265 if (err)
266 goto done;
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
270 if (err)
271 goto done;
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 if (err)
274 goto done;
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
279 if (err)
280 goto done;
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
285 if (err)
286 goto done;
288 /* Generate UUID. */
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
292 goto done;
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
297 goto done;
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 if (err)
301 goto done;
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
306 goto done;
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
309 if (err)
310 goto done;
312 done:
313 free(commit_id);
314 free(path_got);
315 free(formatstr);
316 free(absprefix);
317 free(basestr);
318 free(uuidstr);
319 return err;
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
326 char *path_got;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
332 const char *errstr;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
336 *worktree = NULL;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
340 path_got = NULL;
341 goto done;
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
346 path_lock = NULL;
347 goto done;
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 if (fd == -1) {
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
354 goto done;
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 if (err)
359 goto done;
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 if (errstr) {
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
365 goto done;
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
369 goto done;
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
375 goto done;
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = realpath(path, NULL);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno2("realpath", path);
382 goto done;
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
386 if (err)
387 goto done;
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
391 if (err)
392 goto done;
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
396 if (err)
397 goto done;
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 if (err)
401 goto done;
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
405 goto done;
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
409 if (err)
410 goto done;
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 base_commit_id_str);
414 if (err)
415 goto done;
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
419 done:
420 if (repo)
421 got_repo_close(repo);
422 free(path_got);
423 free(path_lock);
424 free(base_commit_id_str);
425 free(uuidstr);
426 free(formatstr);
427 if (err) {
428 if (fd != -1)
429 close(fd);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
432 *worktree = NULL;
433 } else
434 (*worktree)->lockfd = fd;
436 return err;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
444 do {
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 return err;
448 if (*worktree)
449 return NULL;
450 path = dirname(path);
451 if (path == NULL)
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
471 free(worktree);
472 return err;
475 const char *
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
481 const char *
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
487 const char *
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
505 free(absprefix);
506 return NULL;
509 const char *
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
525 path_got = NULL;
526 goto done;
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
532 goto done;
535 err = write_head_ref(path_got, head_ref);
536 if (err)
537 goto done;
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
541 done:
542 free(path_got);
543 if (err)
544 free(head_ref_name);
545 return err;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
560 char *id_str = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
566 path_got = NULL;
567 goto done;
570 err = got_object_open(&obj, repo, commit_id);
571 if (err)
572 return err;
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
576 goto done;
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
581 if (err)
582 goto done;
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
584 if (err)
585 goto done;
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
591 goto done;
593 done:
594 if (obj)
595 got_object_close(obj);
596 free(id_str);
597 free(path_got);
598 return err;
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
608 return NULL;
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
615 char *abspath;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 struct stat sb;
623 err = NULL;
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
631 free(abspath);
632 return err;
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
639 uint8_t fbuf1[8192];
640 uint8_t fbuf2[8192];
641 size_t flen1 = 0, flen2 = 0;
643 *same = 1;
645 for (;;) {
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
649 break;
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
654 break;
656 if (flen1 == 0) {
657 if (flen2 != 0)
658 *same = 0;
659 break;
660 } else if (flen2 == 0) {
661 if (flen1 != 0)
662 *same = 0;
663 break;
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
666 *same = 0;
667 break;
669 } else {
670 *same = 0;
671 break;
675 return err;
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
682 struct stat sb;
683 size_t size1, size2;
684 FILE *f1 = NULL, *f2 = NULL;
686 *same = 1;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
690 goto done;
692 size1 = sb.st_size;
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
696 goto done;
698 size2 = sb.st_size;
700 if (size1 != size2) {
701 *same = 0;
702 return NULL;
705 f1 = fopen(f1_path, "r");
706 if (f1 == NULL)
707 return got_error_from_errno2("fopen", f1_path);
709 f2 = fopen(f2_path, "r");
710 if (f2 == NULL) {
711 err = got_error_from_errno2("fopen", f2_path);
712 goto done;
715 err = check_file_contents_equal(same, f1, f2);
716 done:
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
722 return err;
725 /*
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
729 */
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
739 int merged_fd = -1;
740 FILE *f_orig = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
743 int overlapcnt = 0;
744 char *parent;
746 *local_changes_subsumed = 0;
748 parent = dirname(ondisk_path);
749 if (parent == NULL)
750 return got_error_from_errno2("dirname", ondisk_path);
752 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 return got_error_from_errno("asprintf");
755 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
756 if (err)
757 goto done;
759 free(base_path);
760 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 err = got_error_from_errno("asprintf");
762 base_path = NULL;
763 goto done;
766 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
767 if (err)
768 goto done;
769 if (blob_orig) {
770 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
771 blob_orig);
772 if (err)
773 goto done;
774 } else {
775 /*
776 * If the file has no blob, this is an "add vs add" conflict,
777 * and we simply use an empty ancestor file to make both files
778 * appear in the merged result in their entirety.
779 */
782 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
784 if (err)
785 goto done;
787 err = (*progress_cb)(progress_arg,
788 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
789 if (err)
790 goto done;
792 if (fsync(merged_fd) != 0) {
793 err = got_error_from_errno("fsync");
794 goto done;
797 /* Check if a clean merge has subsumed all local changes. */
798 if (overlapcnt == 0) {
799 err = check_files_equal(local_changes_subsumed, deriv_path,
800 merged_path);
801 if (err)
802 goto done;
805 if (fchmod(merged_fd, st_mode) != 0) {
806 err = got_error_from_errno2("fchmod", merged_path);
807 goto done;
810 if (rename(merged_path, ondisk_path) != 0) {
811 err = got_error_from_errno3("rename", merged_path,
812 ondisk_path);
813 goto done;
815 done:
816 if (err) {
817 if (merged_path)
818 unlink(merged_path);
820 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
821 err = got_error_from_errno("close");
822 if (f_orig && fclose(f_orig) != 0 && err == NULL)
823 err = got_error_from_errno("fclose");
824 free(merged_path);
825 free(base_path);
826 if (blob_orig_path) {
827 unlink(blob_orig_path);
828 free(blob_orig_path);
830 return err;
833 static const struct got_error *
834 update_symlink(const char *ondisk_path, const char *target_path,
835 size_t target_len)
837 /* This is not atomic but matches what 'ln -sf' does. */
838 if (unlink(ondisk_path) == -1)
839 return got_error_from_errno2("unlink", ondisk_path);
840 if (symlink(target_path, ondisk_path) == -1)
841 return got_error_from_errno3("symlink", target_path,
842 ondisk_path);
843 return NULL;
846 /*
847 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
848 * in the work tree with a file that contains conflict markers and the
849 * conflicting target paths of the original version, a "derived version"
850 * of a symlink from an incoming change, and a local version of the symlink.
852 * The original versions's target path can be NULL if it is not available,
853 * such as if both derived versions added a new symlink at the same path.
855 * The incoming derived symlink target is NULL in case the incoming change
856 * has deleted this symlink.
857 */
858 static const struct got_error *
859 install_symlink_conflict(const char *deriv_target,
860 struct got_object_id *deriv_base_commit_id, const char *orig_target,
861 const char *label_orig, const char *local_target, const char *ondisk_path)
863 const struct got_error *err;
864 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
865 FILE *f = NULL;
867 err = got_object_id_str(&id_str, deriv_base_commit_id);
868 if (err)
869 return got_error_from_errno("asprintf");
871 if (asprintf(&label_deriv, "%s: commit %s",
872 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
873 err = got_error_from_errno("asprintf");
874 goto done;
877 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
878 if (err)
879 goto done;
881 if (fprintf(f, "%s: Could not install symbolic link because of merge "
882 "conflict.\nln(1) may be used to fix the situation. If this is "
883 "intended to be a\nregular file instead then its expected "
884 "contents may be filled in.\nThe following conflicting symlink "
885 "target paths were found:\n"
886 "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n", getprogname(),
887 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
888 deriv_target ? deriv_target : "(symlink was deleted)",
889 orig_target ? label_orig : "",
890 orig_target ? "\n" : "",
891 orig_target ? orig_target : "",
892 orig_target ? "\n" : "",
893 GOT_DIFF_CONFLICT_MARKER_SEP,
894 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
895 err = got_error_from_errno2("fprintf", path);
896 goto done;
899 if (unlink(ondisk_path) == -1) {
900 err = got_error_from_errno2("unlink", ondisk_path);
901 goto done;
903 if (rename(path, ondisk_path) == -1) {
904 err = got_error_from_errno3("rename", path, ondisk_path);
905 goto done;
907 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
908 err = got_error_from_errno2("chmod", ondisk_path);
909 goto done;
911 done:
912 if (f != NULL && fclose(f) == EOF && err == NULL)
913 err = got_error_from_errno2("fclose", path);
914 free(path);
915 free(id_str);
916 free(label_deriv);
917 return err;
920 /* forward declaration */
921 static const struct got_error *
922 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
923 const char *, const char *, uint16_t, const char *,
924 struct got_blob_object *, struct got_object_id *,
925 struct got_repository *, got_worktree_checkout_cb, void *);
927 /*
928 * Merge a symlink into the work tree, where blob_orig acts as the common
929 * ancestor, blob_deriv acts as the first derived version, and the symlink
930 * on disk acts as the second derived version.
931 * Assume that contents of both blobs represent symlinks.
932 */
933 static const struct got_error *
934 merge_symlink(struct got_worktree *worktree,
935 struct got_blob_object *blob_orig, const char *ondisk_path,
936 const char *path, uint16_t st_mode, const char *label_orig,
937 struct got_blob_object *blob_deriv,
938 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
939 got_worktree_checkout_cb progress_cb, void *progress_arg)
941 const struct got_error *err = NULL;
942 char *ancestor_target = NULL, *deriv_target = NULL;
943 struct stat sb;
944 ssize_t ondisk_len, deriv_len;
945 char ondisk_target[PATH_MAX];
946 int have_local_change = 0;
947 int have_incoming_change = 0;
949 if (lstat(ondisk_path, &sb) == -1)
950 return got_error_from_errno2("lstat", ondisk_path);
952 if (!S_ISLNK(sb.st_mode)) {
953 /*
954 * If there is a regular file on disk, merge the symlink
955 * target path into this file, which will usually cause
956 * a merge conflict.
957 */
958 if (S_ISREG(sb.st_mode)) {
959 int local_changes_subsumed;
960 return merge_blob(&local_changes_subsumed, worktree,
961 NULL, ondisk_path, path, sb.st_mode, label_orig,
962 blob_deriv, deriv_base_commit_id,
963 repo, progress_cb, progress_arg);
966 /* TODO symlink is obstructed; do something */
967 return got_error_path(ondisk_path, GOT_ERR_FILE_OBSTRUCTED);
970 ondisk_len = readlink(ondisk_path, ondisk_target,
971 sizeof(ondisk_target));
972 if (ondisk_len == -1) {
973 err = got_error_from_errno2("readlink",
974 ondisk_path);
975 goto done;
977 ondisk_target[ondisk_len] = '\0';
979 if (blob_orig) {
980 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
981 if (err)
982 goto done;
985 err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
986 if (err)
987 goto done;
989 if (ancestor_target == NULL ||
990 (ondisk_len != strlen(ancestor_target) ||
991 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
992 have_local_change = 1;
994 deriv_len = strlen(deriv_target);
995 if (ancestor_target == NULL ||
996 (deriv_len != strlen(ancestor_target) ||
997 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
998 have_incoming_change = 1;
1000 if (!have_local_change && !have_incoming_change) {
1001 if (ancestor_target) {
1002 /* Both sides made the same change. */
1003 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1004 path);
1005 } else if (deriv_len == ondisk_len &&
1006 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1007 /* Both sides added the same symlink. */
1008 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1009 path);
1010 } else {
1011 /* Both sides added symlinks which don't match. */
1012 err = install_symlink_conflict(deriv_target,
1013 deriv_base_commit_id, ancestor_target,
1014 label_orig, ondisk_target, ondisk_path);
1015 if (err)
1016 goto done;
1017 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1018 path);
1020 } else if (!have_local_change && have_incoming_change) {
1021 /* Apply the incoming change. */
1022 err = update_symlink(ondisk_path, deriv_target,
1023 strlen(deriv_target));
1024 if (err)
1025 goto done;
1026 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1027 } else if (have_local_change && have_incoming_change) {
1028 err = install_symlink_conflict(deriv_target,
1029 deriv_base_commit_id, ancestor_target, label_orig,
1030 ondisk_target, ondisk_path);
1031 if (err)
1032 goto done;
1033 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1034 path);
1037 done:
1038 free(ancestor_target);
1039 free(deriv_target);
1040 return err;
1044 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1045 * blob_deriv acts as the first derived version, and the file on disk
1046 * acts as the second derived version.
1048 static const struct got_error *
1049 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1050 struct got_blob_object *blob_orig, const char *ondisk_path,
1051 const char *path, uint16_t st_mode, const char *label_orig,
1052 struct got_blob_object *blob_deriv,
1053 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1054 got_worktree_checkout_cb progress_cb, void *progress_arg)
1056 const struct got_error *err = NULL;
1057 FILE *f_deriv = NULL;
1058 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1059 char *label_deriv = NULL, *parent;
1061 *local_changes_subsumed = 0;
1063 parent = dirname(ondisk_path);
1064 if (parent == NULL)
1065 return got_error_from_errno2("dirname", ondisk_path);
1067 free(base_path);
1068 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1069 err = got_error_from_errno("asprintf");
1070 base_path = NULL;
1071 goto done;
1074 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1075 if (err)
1076 goto done;
1077 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1078 blob_deriv);
1079 if (err)
1080 goto done;
1082 err = got_object_id_str(&id_str, deriv_base_commit_id);
1083 if (err)
1084 goto done;
1085 if (asprintf(&label_deriv, "%s: commit %s",
1086 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1087 err = got_error_from_errno("asprintf");
1088 goto done;
1091 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1092 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1093 label_deriv, repo, progress_cb, progress_arg);
1094 done:
1095 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1096 err = got_error_from_errno("fclose");
1097 free(base_path);
1098 if (blob_deriv_path) {
1099 unlink(blob_deriv_path);
1100 free(blob_deriv_path);
1102 free(id_str);
1103 free(label_deriv);
1104 return err;
1107 static const struct got_error *
1108 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1109 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1110 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1112 const struct got_error *err = NULL;
1113 struct got_fileindex_entry *new_ie;
1115 *new_iep = NULL;
1117 err = got_fileindex_entry_alloc(&new_ie, path);
1118 if (err)
1119 return err;
1121 err = got_fileindex_entry_update(new_ie, ondisk_path,
1122 blob_id->sha1, base_commit_id->sha1, 1);
1123 if (err)
1124 goto done;
1126 err = got_fileindex_entry_add(fileindex, new_ie);
1127 done:
1128 if (err)
1129 got_fileindex_entry_free(new_ie);
1130 else
1131 *new_iep = new_ie;
1132 return err;
1135 static mode_t
1136 get_ondisk_perms(int executable, mode_t st_mode)
1138 mode_t xbits = S_IXUSR;
1140 if (executable) {
1141 /* Map read bits to execute bits. */
1142 if (st_mode & S_IRGRP)
1143 xbits |= S_IXGRP;
1144 if (st_mode & S_IROTH)
1145 xbits |= S_IXOTH;
1146 return st_mode | xbits;
1149 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1152 /* forward declaration */
1153 static const struct got_error *
1154 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1155 const char *path, mode_t te_mode, mode_t st_mode,
1156 struct got_blob_object *blob, int restoring_missing_file,
1157 int reverting_versioned_file, int installing_bad_symlink,
1158 struct got_repository *repo,
1159 got_worktree_checkout_cb progress_cb, void *progress_arg);
1162 * This function assumes that the provided symlink target points at a
1163 * safe location in the work tree!
1165 static const struct got_error *
1166 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1167 size_t target_len)
1169 const struct got_error *err = NULL;
1170 ssize_t elen;
1171 char etarget[PATH_MAX];
1172 int fd;
1175 * "Bad" symlinks (those pointing outside the work tree or into the
1176 * .got directory) are installed in the work tree as a regular file
1177 * which contains the bad symlink target path.
1178 * The new symlink target has already been checked for safety by our
1179 * caller. If we can successfully open a regular file then we simply
1180 * replace this file with a symlink below.
1182 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1183 if (fd == -1) {
1184 if (errno != ELOOP)
1185 return got_error_from_errno2("open", ondisk_path);
1187 /* We are updating an existing on-disk symlink. */
1188 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1189 if (elen == -1)
1190 return got_error_from_errno2("readlink", ondisk_path);
1192 if (elen == target_len &&
1193 memcmp(etarget, target_path, target_len) == 0)
1194 return NULL; /* nothing to do */
1197 err = update_symlink(ondisk_path, target_path, target_len);
1198 if (fd != -1 && close(fd) == -1 && err == NULL)
1199 err = got_error_from_errno2("close", ondisk_path);
1200 return err;
1203 static const struct got_error *
1204 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1205 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1206 int restoring_missing_file, int reverting_versioned_file,
1207 struct got_repository *repo,
1208 got_worktree_checkout_cb progress_cb, void *progress_arg)
1210 const struct got_error *err = NULL;
1211 char target_path[PATH_MAX];
1212 size_t len, target_len = 0;
1213 char *resolved_path = NULL, *abspath = NULL;
1214 char *path_got = NULL;
1215 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1216 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1218 *is_bad_symlink = 0;
1221 * Blob object content specifies the target path of the link.
1222 * If a symbolic link cannot be installed we instead create
1223 * a regular file which contains the link target path stored
1224 * in the blob object.
1226 do {
1227 err = got_object_blob_read_block(&len, blob);
1228 if (len + target_len >= sizeof(target_path)) {
1229 /* Path too long; install as a regular file. */
1230 *is_bad_symlink = 1;
1231 got_object_blob_rewind(blob);
1232 return install_blob(worktree, ondisk_path, path,
1233 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1234 restoring_missing_file, reverting_versioned_file,
1235 1, repo, progress_cb, progress_arg);
1237 if (len > 0) {
1238 /* Skip blob object header first time around. */
1239 memcpy(target_path + target_len, buf + hdrlen,
1240 len - hdrlen);
1241 target_len += len - hdrlen;
1242 hdrlen = 0;
1244 } while (len != 0);
1245 target_path[target_len] = '\0';
1248 * Relative symlink target lookup should begin at the directory
1249 * in which the blob object is being installed.
1251 if (!got_path_is_absolute(target_path)) {
1252 char *parent = dirname(ondisk_path);
1253 if (parent == NULL) {
1254 err = got_error_from_errno2("dirname", ondisk_path);
1255 goto done;
1257 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1258 err = got_error_from_errno("asprintf");
1259 goto done;
1264 * unveil(2) restricts our view of paths in the filesystem.
1265 * ENOENT will occur if a link target path does not exist or
1266 * if it points outside our unveiled path space.
1268 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1269 if (resolved_path == NULL) {
1270 if (errno != ENOENT) {
1271 err = got_error_from_errno2("realpath", target_path);
1272 goto done;
1276 /* Only allow symlinks pointing at paths within the work tree. */
1277 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1278 abspath : target_path), worktree->root_path,
1279 strlen(worktree->root_path))) {
1280 /* install as a regular file */
1281 *is_bad_symlink = 1;
1282 got_object_blob_rewind(blob);
1283 err = install_blob(worktree, ondisk_path, path,
1284 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1285 restoring_missing_file, reverting_versioned_file, 1,
1286 repo, progress_cb, progress_arg);
1287 goto done;
1290 /* Do not allow symlinks pointing into the .got directory. */
1291 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1292 GOT_WORKTREE_GOT_DIR) == -1) {
1293 err = got_error_from_errno("asprintf");
1294 goto done;
1296 if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1297 abspath : target_path), path_got, strlen(path_got))) {
1298 /* install as a regular file */
1299 *is_bad_symlink = 1;
1300 got_object_blob_rewind(blob);
1301 err = install_blob(worktree, ondisk_path, path,
1302 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1303 restoring_missing_file, reverting_versioned_file, 1,
1304 repo, progress_cb, progress_arg);
1305 goto done;
1308 if (symlink(target_path, ondisk_path) == -1) {
1309 if (errno == EEXIST) {
1310 err = replace_existing_symlink(ondisk_path,
1311 target_path, target_len);
1312 if (err)
1313 goto done;
1314 if (progress_cb) {
1315 err = (*progress_cb)(progress_arg,
1316 GOT_STATUS_UPDATE, path);
1318 goto done; /* Nothing else to do. */
1321 if (errno == ENOENT) {
1322 char *parent = dirname(ondisk_path);
1323 if (parent == NULL) {
1324 err = got_error_from_errno2("dirname",
1325 ondisk_path);
1326 goto done;
1328 err = add_dir_on_disk(worktree, parent);
1329 if (err)
1330 goto done;
1332 * Retry, and fall through to error handling
1333 * below if this second attempt fails.
1335 if (symlink(target_path, ondisk_path) != -1) {
1336 err = NULL; /* success */
1337 goto done;
1341 /* Handle errors from first or second creation attempt. */
1342 if (errno == ENAMETOOLONG) {
1343 /* bad target path; install as a regular file */
1344 *is_bad_symlink = 1;
1345 got_object_blob_rewind(blob);
1346 err = install_blob(worktree, ondisk_path, path,
1347 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1348 restoring_missing_file, reverting_versioned_file, 1,
1349 repo, progress_cb, progress_arg);
1350 } else if (errno == ENOTDIR) {
1351 err = got_error_path(ondisk_path,
1352 GOT_ERR_FILE_OBSTRUCTED);
1353 } else {
1354 err = got_error_from_errno3("symlink",
1355 target_path, ondisk_path);
1357 } else if (progress_cb)
1358 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1359 done:
1360 free(resolved_path);
1361 free(abspath);
1362 free(path_got);
1363 return err;
1366 static const struct got_error *
1367 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1368 const char *path, mode_t te_mode, mode_t st_mode,
1369 struct got_blob_object *blob, int restoring_missing_file,
1370 int reverting_versioned_file, int installing_bad_symlink,
1371 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1372 void *progress_arg)
1374 const struct got_error *err = NULL;
1375 int fd = -1;
1376 size_t len, hdrlen;
1377 int update = 0;
1378 char *tmppath = NULL;
1380 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1381 GOT_DEFAULT_FILE_MODE);
1382 if (fd == -1) {
1383 if (errno == ENOENT) {
1384 char *parent = dirname(path);
1385 if (parent == NULL)
1386 return got_error_from_errno2("dirname", path);
1387 err = add_dir_on_disk(worktree, parent);
1388 if (err)
1389 return err;
1390 fd = open(ondisk_path,
1391 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1392 GOT_DEFAULT_FILE_MODE);
1393 if (fd == -1)
1394 return got_error_from_errno2("open",
1395 ondisk_path);
1396 } else if (errno == EEXIST) {
1397 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1398 /* TODO file is obstructed; do something */
1399 err = got_error_path(ondisk_path,
1400 GOT_ERR_FILE_OBSTRUCTED);
1401 goto done;
1402 } else {
1403 err = got_opentemp_named_fd(&tmppath, &fd,
1404 ondisk_path);
1405 if (err)
1406 goto done;
1407 update = 1;
1409 } else
1410 return got_error_from_errno2("open", ondisk_path);
1413 if (progress_cb) {
1414 if (restoring_missing_file)
1415 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1416 path);
1417 else if (reverting_versioned_file)
1418 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1419 path);
1420 else
1421 err = (*progress_cb)(progress_arg,
1422 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1423 if (err)
1424 goto done;
1427 hdrlen = got_object_blob_get_hdrlen(blob);
1428 do {
1429 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1430 err = got_object_blob_read_block(&len, blob);
1431 if (err)
1432 break;
1433 if (len > 0) {
1434 /* Skip blob object header first time around. */
1435 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1436 if (outlen == -1) {
1437 err = got_error_from_errno("write");
1438 goto done;
1439 } else if (outlen != len - hdrlen) {
1440 err = got_error(GOT_ERR_IO);
1441 goto done;
1443 hdrlen = 0;
1445 } while (len != 0);
1447 if (fsync(fd) != 0) {
1448 err = got_error_from_errno("fsync");
1449 goto done;
1452 if (update) {
1453 if (rename(tmppath, ondisk_path) != 0) {
1454 err = got_error_from_errno3("rename", tmppath,
1455 ondisk_path);
1456 unlink(tmppath);
1457 goto done;
1461 if (chmod(ondisk_path,
1462 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1463 err = got_error_from_errno2("chmod", ondisk_path);
1464 goto done;
1467 done:
1468 if (fd != -1 && close(fd) != 0 && err == NULL)
1469 err = got_error_from_errno("close");
1470 free(tmppath);
1471 return err;
1474 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1475 static const struct got_error *
1476 get_modified_file_content_status(unsigned char *status, FILE *f)
1478 const struct got_error *err = NULL;
1479 const char *markers[3] = {
1480 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1481 GOT_DIFF_CONFLICT_MARKER_SEP,
1482 GOT_DIFF_CONFLICT_MARKER_END
1484 int i = 0;
1485 char *line;
1486 size_t len;
1487 const char delim[3] = {'\0', '\0', '\0'};
1489 while (*status == GOT_STATUS_MODIFY) {
1490 line = fparseln(f, &len, NULL, delim, 0);
1491 if (line == NULL) {
1492 if (feof(f))
1493 break;
1494 err = got_ferror(f, GOT_ERR_IO);
1495 break;
1498 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1499 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1500 == 0)
1501 *status = GOT_STATUS_CONFLICT;
1502 else
1503 i++;
1507 return err;
1510 static int
1511 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1513 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1514 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1517 static int
1518 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1520 return !(ie->ctime_sec == sb->st_ctime &&
1521 ie->ctime_nsec == sb->st_ctimensec &&
1522 ie->mtime_sec == sb->st_mtime &&
1523 ie->mtime_nsec == sb->st_mtimensec &&
1524 ie->size == (sb->st_size & 0xffffffff) &&
1525 !xbit_differs(ie, sb->st_mode));
1528 static unsigned char
1529 get_staged_status(struct got_fileindex_entry *ie)
1531 switch (got_fileindex_entry_stage_get(ie)) {
1532 case GOT_FILEIDX_STAGE_ADD:
1533 return GOT_STATUS_ADD;
1534 case GOT_FILEIDX_STAGE_DELETE:
1535 return GOT_STATUS_DELETE;
1536 case GOT_FILEIDX_STAGE_MODIFY:
1537 return GOT_STATUS_MODIFY;
1538 default:
1539 return GOT_STATUS_NO_CHANGE;
1543 static const struct got_error *
1544 get_symlink_status(unsigned char *status, struct stat *sb,
1545 struct got_fileindex_entry *ie, const char *abspath,
1546 int dirfd, const char *de_name, struct got_blob_object *blob)
1548 const struct got_error *err = NULL;
1549 char target_path[PATH_MAX];
1550 char etarget[PATH_MAX];
1551 ssize_t elen;
1552 size_t len, target_len = 0;
1553 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1554 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1556 *status = GOT_STATUS_NO_CHANGE;
1558 /* Blob object content specifies the target path of the link. */
1559 do {
1560 err = got_object_blob_read_block(&len, blob);
1561 if (err)
1562 return err;
1563 if (len + target_len >= sizeof(target_path)) {
1565 * Should not happen. The blob contents were OK
1566 * when this symlink was installed.
1568 return got_error(GOT_ERR_NO_SPACE);
1570 if (len > 0) {
1571 /* Skip blob object header first time around. */
1572 memcpy(target_path + target_len, buf + hdrlen,
1573 len - hdrlen);
1574 target_len += len - hdrlen;
1575 hdrlen = 0;
1577 } while (len != 0);
1578 target_path[target_len] = '\0';
1580 if (dirfd != -1) {
1581 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1582 if (elen == -1)
1583 return got_error_from_errno2("readlinkat", abspath);
1584 } else {
1585 elen = readlink(abspath, etarget, sizeof(etarget));
1586 if (elen == -1)
1587 return got_error_from_errno2("readlinkat", abspath);
1590 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1591 *status = GOT_STATUS_MODIFY;
1593 return NULL;
1596 static const struct got_error *
1597 get_file_status(unsigned char *status, struct stat *sb,
1598 struct got_fileindex_entry *ie, const char *abspath,
1599 int dirfd, const char *de_name, struct got_repository *repo)
1601 const struct got_error *err = NULL;
1602 struct got_object_id id;
1603 size_t hdrlen;
1604 int fd = -1;
1605 FILE *f = NULL;
1606 uint8_t fbuf[8192];
1607 struct got_blob_object *blob = NULL;
1608 size_t flen, blen;
1609 unsigned char staged_status = get_staged_status(ie);
1611 *status = GOT_STATUS_NO_CHANGE;
1614 * Whenever the caller provides a directory descriptor and a
1615 * directory entry name for the file, use them! This prevents
1616 * race conditions if filesystem paths change beneath our feet.
1618 if (dirfd != -1) {
1619 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1620 if (errno == ENOENT) {
1621 if (got_fileindex_entry_has_file_on_disk(ie))
1622 *status = GOT_STATUS_MISSING;
1623 else
1624 *status = GOT_STATUS_DELETE;
1625 goto done;
1627 err = got_error_from_errno2("fstatat", abspath);
1628 goto done;
1630 } else {
1631 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1632 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1633 return got_error_from_errno2("open", abspath);
1634 else if (fd == -1 && errno == ELOOP) {
1635 if (lstat(abspath, sb) == -1)
1636 return got_error_from_errno2("lstat", abspath);
1637 } else if (fd == -1 || fstat(fd, sb) == -1) {
1638 if (errno == ENOENT) {
1639 if (got_fileindex_entry_has_file_on_disk(ie))
1640 *status = GOT_STATUS_MISSING;
1641 else
1642 *status = GOT_STATUS_DELETE;
1643 goto done;
1645 err = got_error_from_errno2("fstat", abspath);
1646 goto done;
1650 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1651 *status = GOT_STATUS_OBSTRUCTED;
1652 goto done;
1655 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1656 *status = GOT_STATUS_DELETE;
1657 goto done;
1658 } else if (!got_fileindex_entry_has_blob(ie) &&
1659 staged_status != GOT_STATUS_ADD) {
1660 *status = GOT_STATUS_ADD;
1661 goto done;
1664 if (!stat_info_differs(ie, sb))
1665 goto done;
1667 if (staged_status == GOT_STATUS_MODIFY ||
1668 staged_status == GOT_STATUS_ADD)
1669 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1670 else
1671 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1673 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1674 if (err)
1675 goto done;
1677 if (S_ISLNK(sb->st_mode)) {
1678 /* Staging changes to symlinks is not yet(?) supported. */
1679 if (staged_status != GOT_STATUS_NO_CHANGE) {
1680 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1681 goto done;
1683 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1684 de_name, blob);
1685 goto done;
1689 if (dirfd != -1) {
1690 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1691 if (fd == -1) {
1692 err = got_error_from_errno2("openat", abspath);
1693 goto done;
1697 f = fdopen(fd, "r");
1698 if (f == NULL) {
1699 err = got_error_from_errno2("fdopen", abspath);
1700 goto done;
1702 fd = -1;
1703 hdrlen = got_object_blob_get_hdrlen(blob);
1704 for (;;) {
1705 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1706 err = got_object_blob_read_block(&blen, blob);
1707 if (err)
1708 goto done;
1709 /* Skip length of blob object header first time around. */
1710 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1711 if (flen == 0 && ferror(f)) {
1712 err = got_error_from_errno("fread");
1713 goto done;
1715 if (blen == 0) {
1716 if (flen != 0)
1717 *status = GOT_STATUS_MODIFY;
1718 break;
1719 } else if (flen == 0) {
1720 if (blen != 0)
1721 *status = GOT_STATUS_MODIFY;
1722 break;
1723 } else if (blen - hdrlen == flen) {
1724 /* Skip blob object header first time around. */
1725 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1726 *status = GOT_STATUS_MODIFY;
1727 break;
1729 } else {
1730 *status = GOT_STATUS_MODIFY;
1731 break;
1733 hdrlen = 0;
1736 if (*status == GOT_STATUS_MODIFY) {
1737 rewind(f);
1738 err = get_modified_file_content_status(status, f);
1739 } else if (xbit_differs(ie, sb->st_mode))
1740 *status = GOT_STATUS_MODE_CHANGE;
1741 done:
1742 if (blob)
1743 got_object_blob_close(blob);
1744 if (f != NULL && fclose(f) == EOF && err == NULL)
1745 err = got_error_from_errno2("fclose", abspath);
1746 if (fd != -1 && close(fd) == -1 && err == NULL)
1747 err = got_error_from_errno2("close", abspath);
1748 return err;
1752 * Update timestamps in the file index if a file is unmodified and
1753 * we had to run a full content comparison to find out.
1755 static const struct got_error *
1756 sync_timestamps(char *ondisk_path, unsigned char status,
1757 struct got_fileindex_entry *ie, struct stat *sb)
1759 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1760 return got_fileindex_entry_update(ie, ondisk_path,
1761 ie->blob_sha1, ie->commit_sha1, 1);
1763 return NULL;
1766 static const struct got_error *
1767 update_blob(struct got_worktree *worktree,
1768 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1769 struct got_tree_entry *te, const char *path,
1770 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1771 void *progress_arg)
1773 const struct got_error *err = NULL;
1774 struct got_blob_object *blob = NULL;
1775 char *ondisk_path;
1776 unsigned char status = GOT_STATUS_NO_CHANGE;
1777 struct stat sb;
1779 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1780 return got_error_from_errno("asprintf");
1782 if (ie) {
1783 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1784 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1785 goto done;
1787 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1788 repo);
1789 if (err)
1790 goto done;
1791 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1792 sb.st_mode = got_fileindex_perms_to_st(ie);
1793 } else
1794 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1796 if (status == GOT_STATUS_OBSTRUCTED) {
1797 err = (*progress_cb)(progress_arg, status, path);
1798 goto done;
1800 if (status == GOT_STATUS_CONFLICT) {
1801 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1802 path);
1803 goto done;
1806 if (ie && status != GOT_STATUS_MISSING &&
1807 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1808 if (got_fileindex_entry_has_commit(ie) &&
1809 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1810 SHA1_DIGEST_LENGTH) == 0) {
1811 err = sync_timestamps(ondisk_path, status, ie, &sb);
1812 if (err)
1813 goto done;
1814 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1815 path);
1816 goto done;
1818 if (got_fileindex_entry_has_blob(ie) &&
1819 memcmp(ie->blob_sha1, te->id.sha1,
1820 SHA1_DIGEST_LENGTH) == 0) {
1821 err = sync_timestamps(ondisk_path, status, ie, &sb);
1822 goto done;
1826 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1827 if (err)
1828 goto done;
1830 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1831 int update_timestamps;
1832 struct got_blob_object *blob2 = NULL;
1833 char *label_orig = NULL;
1834 if (got_fileindex_entry_has_blob(ie)) {
1835 struct got_object_id id2;
1836 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1837 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1838 if (err)
1839 goto done;
1841 if (got_fileindex_entry_has_commit(ie)) {
1842 char id_str[SHA1_DIGEST_STRING_LENGTH];
1843 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1844 sizeof(id_str)) == NULL) {
1845 err = got_error_path(id_str,
1846 GOT_ERR_BAD_OBJ_ID_STR);
1847 goto done;
1849 if (asprintf(&label_orig, "%s: commit %s",
1850 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1851 err = got_error_from_errno("asprintf");
1852 goto done;
1855 if (S_ISLNK(te->mode)) {
1856 err = merge_symlink(worktree, blob2,
1857 ondisk_path, path, sb.st_mode, label_orig,
1858 blob, worktree->base_commit_id, repo,
1859 progress_cb, progress_arg);
1860 } else {
1861 err = merge_blob(&update_timestamps, worktree, blob2,
1862 ondisk_path, path, sb.st_mode, label_orig, blob,
1863 worktree->base_commit_id, repo,
1864 progress_cb, progress_arg);
1866 free(label_orig);
1867 if (blob2)
1868 got_object_blob_close(blob2);
1869 if (err)
1870 goto done;
1872 * Do not update timestamps of files with local changes.
1873 * Otherwise, a future status walk would treat them as
1874 * unmodified files again.
1876 err = got_fileindex_entry_update(ie, ondisk_path,
1877 blob->id.sha1, worktree->base_commit_id->sha1,
1878 update_timestamps);
1879 } else if (status == GOT_STATUS_MODE_CHANGE) {
1880 err = got_fileindex_entry_update(ie, ondisk_path,
1881 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1882 } else if (status == GOT_STATUS_DELETE) {
1883 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1884 if (err)
1885 goto done;
1886 err = got_fileindex_entry_update(ie, ondisk_path,
1887 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1888 if (err)
1889 goto done;
1890 } else {
1891 int is_bad_symlink = 0;
1892 if (S_ISLNK(te->mode)) {
1893 err = install_symlink(&is_bad_symlink, worktree,
1894 ondisk_path, path, blob,
1895 status == GOT_STATUS_MISSING, 0, repo,
1896 progress_cb, progress_arg);
1897 } else {
1898 err = install_blob(worktree, ondisk_path, path,
1899 te->mode, sb.st_mode, blob,
1900 status == GOT_STATUS_MISSING, 0, 0, repo,
1901 progress_cb, progress_arg);
1903 if (err)
1904 goto done;
1906 if (ie) {
1907 err = got_fileindex_entry_update(ie, ondisk_path,
1908 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1909 } else {
1910 err = create_fileindex_entry(&ie, fileindex,
1911 worktree->base_commit_id, ondisk_path, path,
1912 &blob->id);
1914 if (err)
1915 goto done;
1917 if (is_bad_symlink) {
1918 err = got_fileindex_entry_filetype_set(ie,
1919 GOT_FILEIDX_MODE_BAD_SYMLINK);
1920 if (err)
1921 goto done;
1924 got_object_blob_close(blob);
1925 done:
1926 free(ondisk_path);
1927 return err;
1930 static const struct got_error *
1931 remove_ondisk_file(const char *root_path, const char *path)
1933 const struct got_error *err = NULL;
1934 char *ondisk_path = NULL;
1936 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1937 return got_error_from_errno("asprintf");
1939 if (unlink(ondisk_path) == -1) {
1940 if (errno != ENOENT)
1941 err = got_error_from_errno2("unlink", ondisk_path);
1942 } else {
1943 char *parent = dirname(ondisk_path);
1944 while (parent && strcmp(parent, root_path) != 0) {
1945 if (rmdir(parent) == -1) {
1946 if (errno != ENOTEMPTY)
1947 err = got_error_from_errno2("rmdir",
1948 parent);
1949 break;
1951 parent = dirname(parent);
1954 free(ondisk_path);
1955 return err;
1958 static const struct got_error *
1959 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1960 struct got_fileindex_entry *ie, struct got_repository *repo,
1961 got_worktree_checkout_cb progress_cb, void *progress_arg)
1963 const struct got_error *err = NULL;
1964 unsigned char status;
1965 struct stat sb;
1966 char *ondisk_path;
1968 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1969 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1971 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1972 == -1)
1973 return got_error_from_errno("asprintf");
1975 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1976 if (err)
1977 goto done;
1979 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
1980 char ondisk_target[PATH_MAX];
1981 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
1982 sizeof(ondisk_target));
1983 if (ondisk_len == -1) {
1984 err = got_error_from_errno2("readlink", ondisk_path);
1985 goto done;
1987 ondisk_target[ondisk_len] = '\0';
1988 err = install_symlink_conflict(NULL, worktree->base_commit_id,
1989 NULL, NULL, /* XXX pass common ancestor info? */
1990 ondisk_target, ondisk_path);
1991 if (err)
1992 goto done;
1993 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1994 ie->path);
1995 goto done;
1998 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1999 status == GOT_STATUS_ADD) {
2000 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2001 if (err)
2002 goto done;
2004 * Preserve the working file and change the deleted blob's
2005 * entry into a schedule-add entry.
2007 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2008 0);
2009 } else {
2010 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2011 if (err)
2012 goto done;
2013 if (status == GOT_STATUS_NO_CHANGE) {
2014 err = remove_ondisk_file(worktree->root_path, ie->path);
2015 if (err)
2016 goto done;
2018 got_fileindex_entry_remove(fileindex, ie);
2020 done:
2021 free(ondisk_path);
2022 return err;
2025 struct diff_cb_arg {
2026 struct got_fileindex *fileindex;
2027 struct got_worktree *worktree;
2028 struct got_repository *repo;
2029 got_worktree_checkout_cb progress_cb;
2030 void *progress_arg;
2031 got_cancel_cb cancel_cb;
2032 void *cancel_arg;
2035 static const struct got_error *
2036 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2037 struct got_tree_entry *te, const char *parent_path)
2039 struct diff_cb_arg *a = arg;
2041 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2042 return got_error(GOT_ERR_CANCELLED);
2044 return update_blob(a->worktree, a->fileindex, ie, te,
2045 ie->path, a->repo, a->progress_cb, a->progress_arg);
2048 static const struct got_error *
2049 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2051 struct diff_cb_arg *a = arg;
2053 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2054 return got_error(GOT_ERR_CANCELLED);
2056 return delete_blob(a->worktree, a->fileindex, ie,
2057 a->repo, a->progress_cb, a->progress_arg);
2060 static const struct got_error *
2061 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2063 struct diff_cb_arg *a = arg;
2064 const struct got_error *err;
2065 char *path;
2067 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2068 return got_error(GOT_ERR_CANCELLED);
2070 if (got_object_tree_entry_is_submodule(te))
2071 return NULL;
2073 if (asprintf(&path, "%s%s%s", parent_path,
2074 parent_path[0] ? "/" : "", te->name)
2075 == -1)
2076 return got_error_from_errno("asprintf");
2078 if (S_ISDIR(te->mode))
2079 err = add_dir_on_disk(a->worktree, path);
2080 else
2081 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2082 a->repo, a->progress_cb, a->progress_arg);
2084 free(path);
2085 return err;
2088 static const struct got_error *
2089 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2091 const struct got_error *err = NULL;
2092 char *uuidstr = NULL;
2093 uint32_t uuid_status;
2095 *refname = NULL;
2097 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2098 if (uuid_status != uuid_s_ok)
2099 return got_error_uuid(uuid_status, "uuid_to_string");
2101 if (asprintf(refname, "%s-%s", prefix, uuidstr)
2102 == -1) {
2103 err = got_error_from_errno("asprintf");
2104 *refname = NULL;
2106 free(uuidstr);
2107 return err;
2110 const struct got_error *
2111 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2113 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2116 static const struct got_error *
2117 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2119 return get_ref_name(refname, worktree,
2120 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2123 static const struct got_error *
2124 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2126 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2129 static const struct got_error *
2130 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2132 return get_ref_name(refname, worktree,
2133 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2136 static const struct got_error *
2137 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2139 return get_ref_name(refname, worktree,
2140 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2143 static const struct got_error *
2144 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2146 return get_ref_name(refname, worktree,
2147 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2150 static const struct got_error *
2151 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2153 return get_ref_name(refname, worktree,
2154 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2157 static const struct got_error *
2158 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2160 return get_ref_name(refname, worktree,
2161 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2164 static const struct got_error *
2165 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2167 return get_ref_name(refname, worktree,
2168 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2171 const struct got_error *
2172 got_worktree_get_histedit_script_path(char **path,
2173 struct got_worktree *worktree)
2175 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2176 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2177 *path = NULL;
2178 return got_error_from_errno("asprintf");
2180 return NULL;
2184 * Prevent Git's garbage collector from deleting our base commit by
2185 * setting a reference to our base commit's ID.
2187 static const struct got_error *
2188 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2190 const struct got_error *err = NULL;
2191 struct got_reference *ref = NULL;
2192 char *refname;
2194 err = got_worktree_get_base_ref_name(&refname, worktree);
2195 if (err)
2196 return err;
2198 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2199 if (err)
2200 goto done;
2202 err = got_ref_write(ref, repo);
2203 done:
2204 free(refname);
2205 if (ref)
2206 got_ref_close(ref);
2207 return err;
2210 static const struct got_error *
2211 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2213 const struct got_error *err = NULL;
2215 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2216 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2217 err = got_error_from_errno("asprintf");
2218 *fileindex_path = NULL;
2220 return err;
2224 static const struct got_error *
2225 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2226 struct got_worktree *worktree)
2228 const struct got_error *err = NULL;
2229 FILE *index = NULL;
2231 *fileindex_path = NULL;
2232 *fileindex = got_fileindex_alloc();
2233 if (*fileindex == NULL)
2234 return got_error_from_errno("got_fileindex_alloc");
2236 err = get_fileindex_path(fileindex_path, worktree);
2237 if (err)
2238 goto done;
2240 index = fopen(*fileindex_path, "rb");
2241 if (index == NULL) {
2242 if (errno != ENOENT)
2243 err = got_error_from_errno2("fopen", *fileindex_path);
2244 } else {
2245 err = got_fileindex_read(*fileindex, index);
2246 if (fclose(index) != 0 && err == NULL)
2247 err = got_error_from_errno("fclose");
2249 done:
2250 if (err) {
2251 free(*fileindex_path);
2252 *fileindex_path = NULL;
2253 got_fileindex_free(*fileindex);
2254 *fileindex = NULL;
2256 return err;
2259 struct bump_base_commit_id_arg {
2260 struct got_object_id *base_commit_id;
2261 const char *path;
2262 size_t path_len;
2263 const char *entry_name;
2264 got_worktree_checkout_cb progress_cb;
2265 void *progress_arg;
2268 /* Bump base commit ID of all files within an updated part of the work tree. */
2269 static const struct got_error *
2270 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2272 const struct got_error *err;
2273 struct bump_base_commit_id_arg *a = arg;
2275 if (a->entry_name) {
2276 if (strcmp(ie->path, a->path) != 0)
2277 return NULL;
2278 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2279 return NULL;
2281 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2282 SHA1_DIGEST_LENGTH) == 0)
2283 return NULL;
2285 if (a->progress_cb) {
2286 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2287 ie->path);
2288 if (err)
2289 return err;
2291 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2292 return NULL;
2295 static const struct got_error *
2296 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2298 const struct got_error *err = NULL;
2299 char *new_fileindex_path = NULL;
2300 FILE *new_index = NULL;
2301 struct timespec timeout;
2303 err = got_opentemp_named(&new_fileindex_path, &new_index,
2304 fileindex_path);
2305 if (err)
2306 goto done;
2308 err = got_fileindex_write(fileindex, new_index);
2309 if (err)
2310 goto done;
2312 if (rename(new_fileindex_path, fileindex_path) != 0) {
2313 err = got_error_from_errno3("rename", new_fileindex_path,
2314 fileindex_path);
2315 unlink(new_fileindex_path);
2319 * Sleep for a short amount of time to ensure that files modified after
2320 * this program exits have a different time stamp from the one which
2321 * was recorded in the file index.
2323 timeout.tv_sec = 0;
2324 timeout.tv_nsec = 1;
2325 nanosleep(&timeout, NULL);
2326 done:
2327 if (new_index)
2328 fclose(new_index);
2329 free(new_fileindex_path);
2330 return err;
2333 static const struct got_error *
2334 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2335 struct got_object_id **tree_id, const char *wt_relpath,
2336 struct got_worktree *worktree, struct got_repository *repo)
2338 const struct got_error *err = NULL;
2339 struct got_object_id *id = NULL;
2340 char *in_repo_path = NULL;
2341 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2343 *entry_type = GOT_OBJ_TYPE_ANY;
2344 *tree_relpath = NULL;
2345 *tree_id = NULL;
2347 if (wt_relpath[0] == '\0') {
2348 /* Check out all files within the work tree. */
2349 *entry_type = GOT_OBJ_TYPE_TREE;
2350 *tree_relpath = strdup("");
2351 if (*tree_relpath == NULL) {
2352 err = got_error_from_errno("strdup");
2353 goto done;
2355 err = got_object_id_by_path(tree_id, repo,
2356 worktree->base_commit_id, worktree->path_prefix);
2357 if (err)
2358 goto done;
2359 return NULL;
2362 /* Check out a subset of files in the work tree. */
2364 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2365 is_root_wt ? "" : "/", wt_relpath) == -1) {
2366 err = got_error_from_errno("asprintf");
2367 goto done;
2370 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2371 in_repo_path);
2372 if (err)
2373 goto done;
2375 free(in_repo_path);
2376 in_repo_path = NULL;
2378 err = got_object_get_type(entry_type, repo, id);
2379 if (err)
2380 goto done;
2382 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2383 /* Check out a single file. */
2384 if (strchr(wt_relpath, '/') == NULL) {
2385 /* Check out a single file in work tree's root dir. */
2386 in_repo_path = strdup(worktree->path_prefix);
2387 if (in_repo_path == NULL) {
2388 err = got_error_from_errno("strdup");
2389 goto done;
2391 *tree_relpath = strdup("");
2392 if (*tree_relpath == NULL) {
2393 err = got_error_from_errno("strdup");
2394 goto done;
2396 } else {
2397 /* Check out a single file in a subdirectory. */
2398 err = got_path_dirname(tree_relpath, wt_relpath);
2399 if (err)
2400 return err;
2401 if (asprintf(&in_repo_path, "%s%s%s",
2402 worktree->path_prefix, is_root_wt ? "" : "/",
2403 *tree_relpath) == -1) {
2404 err = got_error_from_errno("asprintf");
2405 goto done;
2408 err = got_object_id_by_path(tree_id, repo,
2409 worktree->base_commit_id, in_repo_path);
2410 } else {
2411 /* Check out all files within a subdirectory. */
2412 *tree_id = got_object_id_dup(id);
2413 if (*tree_id == NULL) {
2414 err = got_error_from_errno("got_object_id_dup");
2415 goto done;
2417 *tree_relpath = strdup(wt_relpath);
2418 if (*tree_relpath == NULL) {
2419 err = got_error_from_errno("strdup");
2420 goto done;
2423 done:
2424 free(id);
2425 free(in_repo_path);
2426 if (err) {
2427 *entry_type = GOT_OBJ_TYPE_ANY;
2428 free(*tree_relpath);
2429 *tree_relpath = NULL;
2430 free(*tree_id);
2431 *tree_id = NULL;
2433 return err;
2436 static const struct got_error *
2437 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2438 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2439 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2440 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2442 const struct got_error *err = NULL;
2443 struct got_commit_object *commit = NULL;
2444 struct got_tree_object *tree = NULL;
2445 struct got_fileindex_diff_tree_cb diff_cb;
2446 struct diff_cb_arg arg;
2448 err = ref_base_commit(worktree, repo);
2449 if (err) {
2450 if (!(err->code == GOT_ERR_ERRNO &&
2451 (errno == EACCES || errno == EROFS)))
2452 goto done;
2453 err = (*progress_cb)(progress_arg,
2454 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2455 if (err)
2456 return err;
2459 err = got_object_open_as_commit(&commit, repo,
2460 worktree->base_commit_id);
2461 if (err)
2462 goto done;
2464 err = got_object_open_as_tree(&tree, repo, tree_id);
2465 if (err)
2466 goto done;
2468 if (entry_name &&
2469 got_object_tree_find_entry(tree, entry_name) == NULL) {
2470 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2471 goto done;
2474 diff_cb.diff_old_new = diff_old_new;
2475 diff_cb.diff_old = diff_old;
2476 diff_cb.diff_new = diff_new;
2477 arg.fileindex = fileindex;
2478 arg.worktree = worktree;
2479 arg.repo = repo;
2480 arg.progress_cb = progress_cb;
2481 arg.progress_arg = progress_arg;
2482 arg.cancel_cb = cancel_cb;
2483 arg.cancel_arg = cancel_arg;
2484 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2485 entry_name, repo, &diff_cb, &arg);
2486 done:
2487 if (tree)
2488 got_object_tree_close(tree);
2489 if (commit)
2490 got_object_commit_close(commit);
2491 return err;
2494 const struct got_error *
2495 got_worktree_checkout_files(struct got_worktree *worktree,
2496 struct got_pathlist_head *paths, struct got_repository *repo,
2497 got_worktree_checkout_cb progress_cb, void *progress_arg,
2498 got_cancel_cb cancel_cb, void *cancel_arg)
2500 const struct got_error *err = NULL, *sync_err, *unlockerr;
2501 struct got_commit_object *commit = NULL;
2502 struct got_tree_object *tree = NULL;
2503 struct got_fileindex *fileindex = NULL;
2504 char *fileindex_path = NULL;
2505 struct got_pathlist_entry *pe;
2506 struct tree_path_data {
2507 SIMPLEQ_ENTRY(tree_path_data) entry;
2508 struct got_object_id *tree_id;
2509 int entry_type;
2510 char *relpath;
2511 char *entry_name;
2512 } *tpd = NULL;
2513 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2515 SIMPLEQ_INIT(&tree_paths);
2517 err = lock_worktree(worktree, LOCK_EX);
2518 if (err)
2519 return err;
2521 /* Map all specified paths to in-repository trees. */
2522 TAILQ_FOREACH(pe, paths, entry) {
2523 tpd = malloc(sizeof(*tpd));
2524 if (tpd == NULL) {
2525 err = got_error_from_errno("malloc");
2526 goto done;
2529 err = find_tree_entry_for_checkout(&tpd->entry_type,
2530 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2531 if (err) {
2532 free(tpd);
2533 goto done;
2536 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2537 err = got_path_basename(&tpd->entry_name, pe->path);
2538 if (err) {
2539 free(tpd->relpath);
2540 free(tpd->tree_id);
2541 free(tpd);
2542 goto done;
2544 } else
2545 tpd->entry_name = NULL;
2547 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2551 * Read the file index.
2552 * Checking out files is supposed to be an idempotent operation.
2553 * If the on-disk file index is incomplete we will try to complete it.
2555 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2556 if (err)
2557 goto done;
2559 tpd = SIMPLEQ_FIRST(&tree_paths);
2560 TAILQ_FOREACH(pe, paths, entry) {
2561 struct bump_base_commit_id_arg bbc_arg;
2563 err = checkout_files(worktree, fileindex, tpd->relpath,
2564 tpd->tree_id, tpd->entry_name, repo,
2565 progress_cb, progress_arg, cancel_cb, cancel_arg);
2566 if (err)
2567 break;
2569 bbc_arg.base_commit_id = worktree->base_commit_id;
2570 bbc_arg.entry_name = tpd->entry_name;
2571 bbc_arg.path = pe->path;
2572 bbc_arg.path_len = pe->path_len;
2573 bbc_arg.progress_cb = progress_cb;
2574 bbc_arg.progress_arg = progress_arg;
2575 err = got_fileindex_for_each_entry_safe(fileindex,
2576 bump_base_commit_id, &bbc_arg);
2577 if (err)
2578 break;
2580 tpd = SIMPLEQ_NEXT(tpd, entry);
2582 sync_err = sync_fileindex(fileindex, fileindex_path);
2583 if (sync_err && err == NULL)
2584 err = sync_err;
2585 done:
2586 free(fileindex_path);
2587 if (tree)
2588 got_object_tree_close(tree);
2589 if (commit)
2590 got_object_commit_close(commit);
2591 if (fileindex)
2592 got_fileindex_free(fileindex);
2593 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2594 tpd = SIMPLEQ_FIRST(&tree_paths);
2595 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2596 free(tpd->relpath);
2597 free(tpd->tree_id);
2598 free(tpd);
2600 unlockerr = lock_worktree(worktree, LOCK_SH);
2601 if (unlockerr && err == NULL)
2602 err = unlockerr;
2603 return err;
2606 struct merge_file_cb_arg {
2607 struct got_worktree *worktree;
2608 struct got_fileindex *fileindex;
2609 got_worktree_checkout_cb progress_cb;
2610 void *progress_arg;
2611 got_cancel_cb cancel_cb;
2612 void *cancel_arg;
2613 const char *label_orig;
2614 struct got_object_id *commit_id2;
2617 static const struct got_error *
2618 merge_file_cb(void *arg, struct got_blob_object *blob1,
2619 struct got_blob_object *blob2, struct got_object_id *id1,
2620 struct got_object_id *id2, const char *path1, const char *path2,
2621 mode_t mode1, mode_t mode2, struct got_repository *repo)
2623 static const struct got_error *err = NULL;
2624 struct merge_file_cb_arg *a = arg;
2625 struct got_fileindex_entry *ie;
2626 char *ondisk_path = NULL;
2627 struct stat sb;
2628 unsigned char status;
2629 int local_changes_subsumed;
2631 if (blob1 && blob2) {
2632 ie = got_fileindex_entry_get(a->fileindex, path2,
2633 strlen(path2));
2634 if (ie == NULL)
2635 return (*a->progress_cb)(a->progress_arg,
2636 GOT_STATUS_MISSING, path2);
2638 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2639 path2) == -1)
2640 return got_error_from_errno("asprintf");
2642 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2643 repo);
2644 if (err)
2645 goto done;
2647 if (status == GOT_STATUS_DELETE) {
2648 err = (*a->progress_cb)(a->progress_arg,
2649 GOT_STATUS_MERGE, path2);
2650 goto done;
2652 if (status != GOT_STATUS_NO_CHANGE &&
2653 status != GOT_STATUS_MODIFY &&
2654 status != GOT_STATUS_CONFLICT &&
2655 status != GOT_STATUS_ADD) {
2656 err = (*a->progress_cb)(a->progress_arg, status, path2);
2657 goto done;
2660 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2661 err = merge_symlink(a->worktree, blob1,
2662 ondisk_path, path2, sb.st_mode, a->label_orig,
2663 blob2, a->commit_id2, repo, a->progress_cb,
2664 a->progress_arg);
2665 } else {
2666 err = merge_blob(&local_changes_subsumed, a->worktree,
2667 blob1, ondisk_path, path2, sb.st_mode,
2668 a->label_orig, blob2, a->commit_id2, repo,
2669 a->progress_cb, a->progress_arg);
2671 } else if (blob1) {
2672 ie = got_fileindex_entry_get(a->fileindex, path1,
2673 strlen(path1));
2674 if (ie == NULL)
2675 return (*a->progress_cb)(a->progress_arg,
2676 GOT_STATUS_MISSING, path1);
2678 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2679 path1) == -1)
2680 return got_error_from_errno("asprintf");
2682 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2683 repo);
2684 if (err)
2685 goto done;
2687 switch (status) {
2688 case GOT_STATUS_NO_CHANGE:
2689 err = (*a->progress_cb)(a->progress_arg,
2690 GOT_STATUS_DELETE, path1);
2691 if (err)
2692 goto done;
2693 err = remove_ondisk_file(a->worktree->root_path, path1);
2694 if (err)
2695 goto done;
2696 if (ie)
2697 got_fileindex_entry_mark_deleted_from_disk(ie);
2698 break;
2699 case GOT_STATUS_DELETE:
2700 case GOT_STATUS_MISSING:
2701 err = (*a->progress_cb)(a->progress_arg,
2702 GOT_STATUS_DELETE, path1);
2703 if (err)
2704 goto done;
2705 if (ie)
2706 got_fileindex_entry_mark_deleted_from_disk(ie);
2707 break;
2708 case GOT_STATUS_ADD:
2709 case GOT_STATUS_MODIFY:
2710 case GOT_STATUS_CONFLICT:
2711 err = (*a->progress_cb)(a->progress_arg,
2712 GOT_STATUS_CANNOT_DELETE, path1);
2713 if (err)
2714 goto done;
2715 break;
2716 case GOT_STATUS_OBSTRUCTED:
2717 err = (*a->progress_cb)(a->progress_arg, status, path1);
2718 if (err)
2719 goto done;
2720 break;
2721 default:
2722 break;
2724 } else if (blob2) {
2725 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2726 path2) == -1)
2727 return got_error_from_errno("asprintf");
2728 ie = got_fileindex_entry_get(a->fileindex, path2,
2729 strlen(path2));
2730 if (ie) {
2731 err = get_file_status(&status, &sb, ie, ondisk_path,
2732 -1, NULL, repo);
2733 if (err)
2734 goto done;
2735 if (status != GOT_STATUS_NO_CHANGE &&
2736 status != GOT_STATUS_MODIFY &&
2737 status != GOT_STATUS_CONFLICT &&
2738 status != GOT_STATUS_ADD) {
2739 err = (*a->progress_cb)(a->progress_arg,
2740 status, path2);
2741 goto done;
2743 if (S_ISLNK(mode2)) {
2744 err = merge_symlink(a->worktree, NULL,
2745 ondisk_path, path2, sb.st_mode,
2746 a->label_orig, blob2, a->commit_id2,
2747 repo, a->progress_cb, a->progress_arg);
2748 if (err)
2749 goto done;
2750 } else {
2751 err = merge_blob(&local_changes_subsumed,
2752 a->worktree, NULL, ondisk_path, path2,
2753 sb.st_mode, a->label_orig, blob2,
2754 a->commit_id2, repo, a->progress_cb,
2755 a->progress_arg);
2756 if (err)
2757 goto done;
2759 if (status == GOT_STATUS_DELETE) {
2760 err = got_fileindex_entry_update(ie,
2761 ondisk_path, blob2->id.sha1,
2762 a->worktree->base_commit_id->sha1, 0);
2763 if (err)
2764 goto done;
2766 } else {
2767 int is_bad_symlink = 0;
2768 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2769 if (S_ISLNK(mode2)) {
2770 err = install_symlink(&is_bad_symlink,
2771 a->worktree, ondisk_path, path2, blob2, 0,
2772 0, repo, a->progress_cb, a->progress_arg);
2773 } else {
2774 err = install_blob(a->worktree, ondisk_path, path2,
2775 mode2, sb.st_mode, blob2, 0, 0, 0, repo,
2776 a->progress_cb, a->progress_arg);
2778 if (err)
2779 goto done;
2780 err = got_fileindex_entry_alloc(&ie, path2);
2781 if (err)
2782 goto done;
2783 err = got_fileindex_entry_update(ie, ondisk_path,
2784 NULL, NULL, 1);
2785 if (err) {
2786 got_fileindex_entry_free(ie);
2787 goto done;
2789 err = got_fileindex_entry_add(a->fileindex, ie);
2790 if (err) {
2791 got_fileindex_entry_free(ie);
2792 goto done;
2794 if (is_bad_symlink) {
2795 err = got_fileindex_entry_filetype_set(ie,
2796 GOT_FILEIDX_MODE_BAD_SYMLINK);
2797 if (err)
2798 goto done;
2802 done:
2803 free(ondisk_path);
2804 return err;
2807 struct check_merge_ok_arg {
2808 struct got_worktree *worktree;
2809 struct got_repository *repo;
2812 static const struct got_error *
2813 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2815 const struct got_error *err = NULL;
2816 struct check_merge_ok_arg *a = arg;
2817 unsigned char status;
2818 struct stat sb;
2819 char *ondisk_path;
2821 /* Reject merges into a work tree with mixed base commits. */
2822 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2823 SHA1_DIGEST_LENGTH))
2824 return got_error(GOT_ERR_MIXED_COMMITS);
2826 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2827 == -1)
2828 return got_error_from_errno("asprintf");
2830 /* Reject merges into a work tree with conflicted files. */
2831 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2832 if (err)
2833 return err;
2834 if (status == GOT_STATUS_CONFLICT)
2835 return got_error(GOT_ERR_CONFLICTS);
2837 return NULL;
2840 static const struct got_error *
2841 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2842 const char *fileindex_path, struct got_object_id *commit_id1,
2843 struct got_object_id *commit_id2, struct got_repository *repo,
2844 got_worktree_checkout_cb progress_cb, void *progress_arg,
2845 got_cancel_cb cancel_cb, void *cancel_arg)
2847 const struct got_error *err = NULL, *sync_err;
2848 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2849 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2850 struct merge_file_cb_arg arg;
2851 char *label_orig = NULL;
2853 if (commit_id1) {
2854 char *id_str;
2856 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2857 worktree->path_prefix);
2858 if (err)
2859 goto done;
2861 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2862 if (err)
2863 goto done;
2865 err = got_object_id_str(&id_str, commit_id1);
2866 if (err)
2867 goto done;
2869 if (asprintf(&label_orig, "%s: commit %s",
2870 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2871 err = got_error_from_errno("asprintf");
2872 free(id_str);
2873 goto done;
2875 free(id_str);
2878 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2879 worktree->path_prefix);
2880 if (err)
2881 goto done;
2883 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2884 if (err)
2885 goto done;
2887 arg.worktree = worktree;
2888 arg.fileindex = fileindex;
2889 arg.progress_cb = progress_cb;
2890 arg.progress_arg = progress_arg;
2891 arg.cancel_cb = cancel_cb;
2892 arg.cancel_arg = cancel_arg;
2893 arg.label_orig = label_orig;
2894 arg.commit_id2 = commit_id2;
2895 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2896 sync_err = sync_fileindex(fileindex, fileindex_path);
2897 if (sync_err && err == NULL)
2898 err = sync_err;
2899 done:
2900 if (tree1)
2901 got_object_tree_close(tree1);
2902 if (tree2)
2903 got_object_tree_close(tree2);
2904 free(label_orig);
2905 return err;
2908 const struct got_error *
2909 got_worktree_merge_files(struct got_worktree *worktree,
2910 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2911 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2912 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2914 const struct got_error *err, *unlockerr;
2915 char *fileindex_path = NULL;
2916 struct got_fileindex *fileindex = NULL;
2917 struct check_merge_ok_arg mok_arg;
2919 err = lock_worktree(worktree, LOCK_EX);
2920 if (err)
2921 return err;
2923 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2924 if (err)
2925 goto done;
2927 mok_arg.worktree = worktree;
2928 mok_arg.repo = repo;
2929 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2930 &mok_arg);
2931 if (err)
2932 goto done;
2934 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2935 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2936 done:
2937 if (fileindex)
2938 got_fileindex_free(fileindex);
2939 free(fileindex_path);
2940 unlockerr = lock_worktree(worktree, LOCK_SH);
2941 if (unlockerr && err == NULL)
2942 err = unlockerr;
2943 return err;
2946 struct diff_dir_cb_arg {
2947 struct got_fileindex *fileindex;
2948 struct got_worktree *worktree;
2949 const char *status_path;
2950 size_t status_path_len;
2951 struct got_repository *repo;
2952 got_worktree_status_cb status_cb;
2953 void *status_arg;
2954 got_cancel_cb cancel_cb;
2955 void *cancel_arg;
2956 /* A pathlist containing per-directory pathlists of ignore patterns. */
2957 struct got_pathlist_head ignores;
2958 int report_unchanged;
2959 int no_ignores;
2962 static const struct got_error *
2963 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2964 int dirfd, const char *de_name,
2965 got_worktree_status_cb status_cb, void *status_arg,
2966 struct got_repository *repo, int report_unchanged)
2968 const struct got_error *err = NULL;
2969 unsigned char status = GOT_STATUS_NO_CHANGE;
2970 unsigned char staged_status = get_staged_status(ie);
2971 struct stat sb;
2972 struct got_object_id blob_id, commit_id, staged_blob_id;
2973 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2974 struct got_object_id *staged_blob_idp = NULL;
2976 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2977 if (err)
2978 return err;
2980 if (status == GOT_STATUS_NO_CHANGE &&
2981 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2982 return NULL;
2984 if (got_fileindex_entry_has_blob(ie)) {
2985 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2986 blob_idp = &blob_id;
2988 if (got_fileindex_entry_has_commit(ie)) {
2989 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2990 commit_idp = &commit_id;
2992 if (staged_status == GOT_STATUS_ADD ||
2993 staged_status == GOT_STATUS_MODIFY) {
2994 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2995 SHA1_DIGEST_LENGTH);
2996 staged_blob_idp = &staged_blob_id;
2999 return (*status_cb)(status_arg, status, staged_status,
3000 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3003 static const struct got_error *
3004 status_old_new(void *arg, struct got_fileindex_entry *ie,
3005 struct dirent *de, const char *parent_path, int dirfd)
3007 const struct got_error *err = NULL;
3008 struct diff_dir_cb_arg *a = arg;
3009 char *abspath;
3011 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3012 return got_error(GOT_ERR_CANCELLED);
3014 if (got_path_cmp(parent_path, a->status_path,
3015 strlen(parent_path), a->status_path_len) != 0 &&
3016 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3017 return NULL;
3019 if (parent_path[0]) {
3020 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3021 parent_path, de->d_name) == -1)
3022 return got_error_from_errno("asprintf");
3023 } else {
3024 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3025 de->d_name) == -1)
3026 return got_error_from_errno("asprintf");
3029 err = report_file_status(ie, abspath, dirfd, de->d_name,
3030 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3031 free(abspath);
3032 return err;
3035 static const struct got_error *
3036 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3038 struct diff_dir_cb_arg *a = arg;
3039 struct got_object_id blob_id, commit_id;
3040 unsigned char status;
3042 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3043 return got_error(GOT_ERR_CANCELLED);
3045 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3046 return NULL;
3048 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3049 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3050 if (got_fileindex_entry_has_file_on_disk(ie))
3051 status = GOT_STATUS_MISSING;
3052 else
3053 status = GOT_STATUS_DELETE;
3054 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3055 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3058 void
3059 free_ignorelist(struct got_pathlist_head *ignorelist)
3061 struct got_pathlist_entry *pe;
3063 TAILQ_FOREACH(pe, ignorelist, entry)
3064 free((char *)pe->path);
3065 got_pathlist_free(ignorelist);
3068 void
3069 free_ignores(struct got_pathlist_head *ignores)
3071 struct got_pathlist_entry *pe;
3073 TAILQ_FOREACH(pe, ignores, entry) {
3074 struct got_pathlist_head *ignorelist = pe->data;
3075 free_ignorelist(ignorelist);
3076 free((char *)pe->path);
3078 got_pathlist_free(ignores);
3081 static const struct got_error *
3082 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3084 const struct got_error *err = NULL;
3085 struct got_pathlist_entry *pe = NULL;
3086 struct got_pathlist_head *ignorelist;
3087 char *line = NULL, *pattern, *dirpath = NULL;
3088 size_t linesize = 0;
3089 ssize_t linelen;
3091 ignorelist = calloc(1, sizeof(*ignorelist));
3092 if (ignorelist == NULL)
3093 return got_error_from_errno("calloc");
3094 TAILQ_INIT(ignorelist);
3096 while ((linelen = getline(&line, &linesize, f)) != -1) {
3097 if (linelen > 0 && line[linelen - 1] == '\n')
3098 line[linelen - 1] = '\0';
3100 /* Git's ignores may contain comments. */
3101 if (line[0] == '#')
3102 continue;
3104 /* Git's negated patterns are not (yet?) supported. */
3105 if (line[0] == '!')
3106 continue;
3108 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3109 line) == -1) {
3110 err = got_error_from_errno("asprintf");
3111 goto done;
3113 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3114 if (err)
3115 goto done;
3117 if (ferror(f)) {
3118 err = got_error_from_errno("getline");
3119 goto done;
3122 dirpath = strdup(path);
3123 if (dirpath == NULL) {
3124 err = got_error_from_errno("strdup");
3125 goto done;
3127 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3128 done:
3129 free(line);
3130 if (err || pe == NULL) {
3131 free(dirpath);
3132 free_ignorelist(ignorelist);
3134 return err;
3137 int
3138 match_ignores(struct got_pathlist_head *ignores, const char *path)
3140 struct got_pathlist_entry *pe;
3142 /* Handle patterns which match in all directories. */
3143 TAILQ_FOREACH(pe, ignores, entry) {
3144 struct got_pathlist_head *ignorelist = pe->data;
3145 struct got_pathlist_entry *pi;
3147 TAILQ_FOREACH(pi, ignorelist, entry) {
3148 const char *p, *pattern = pi->path;
3150 if (strncmp(pattern, "**/", 3) != 0)
3151 continue;
3152 pattern += 3;
3153 p = path;
3154 while (*p) {
3155 if (fnmatch(pattern, p,
3156 FNM_PATHNAME | FNM_LEADING_DIR)) {
3157 /* Retry in next directory. */
3158 while (*p && *p != '/')
3159 p++;
3160 while (*p == '/')
3161 p++;
3162 continue;
3164 return 1;
3170 * The ignores pathlist contains ignore lists from children before
3171 * parents, so we can find the most specific ignorelist by walking
3172 * ignores backwards.
3174 pe = TAILQ_LAST(ignores, got_pathlist_head);
3175 while (pe) {
3176 if (got_path_is_child(path, pe->path, pe->path_len)) {
3177 struct got_pathlist_head *ignorelist = pe->data;
3178 struct got_pathlist_entry *pi;
3179 TAILQ_FOREACH(pi, ignorelist, entry) {
3180 const char *pattern = pi->path;
3181 int flags = FNM_LEADING_DIR;
3182 if (strstr(pattern, "/**/") == NULL)
3183 flags |= FNM_PATHNAME;
3184 if (fnmatch(pattern, path, flags))
3185 continue;
3186 return 1;
3189 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3192 return 0;
3195 static const struct got_error *
3196 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3197 const char *path, int dirfd, const char *ignores_filename)
3199 const struct got_error *err = NULL;
3200 char *ignorespath;
3201 int fd = -1;
3202 FILE *ignoresfile = NULL;
3204 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3205 path[0] ? "/" : "", ignores_filename) == -1)
3206 return got_error_from_errno("asprintf");
3208 if (dirfd != -1) {
3209 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3210 if (fd == -1) {
3211 if (errno != ENOENT && errno != EACCES)
3212 err = got_error_from_errno2("openat",
3213 ignorespath);
3214 } else {
3215 ignoresfile = fdopen(fd, "r");
3216 if (ignoresfile == NULL)
3217 err = got_error_from_errno2("fdopen",
3218 ignorespath);
3219 else {
3220 fd = -1;
3221 err = read_ignores(ignores, path, ignoresfile);
3224 } else {
3225 ignoresfile = fopen(ignorespath, "r");
3226 if (ignoresfile == NULL) {
3227 if (errno != ENOENT && errno != EACCES)
3228 err = got_error_from_errno2("fopen",
3229 ignorespath);
3230 } else
3231 err = read_ignores(ignores, path, ignoresfile);
3234 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3235 err = got_error_from_errno2("fclose", path);
3236 if (fd != -1 && close(fd) == -1 && err == NULL)
3237 err = got_error_from_errno2("close", path);
3238 free(ignorespath);
3239 return err;
3242 static const struct got_error *
3243 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3245 const struct got_error *err = NULL;
3246 struct diff_dir_cb_arg *a = arg;
3247 char *path = NULL;
3249 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3250 return got_error(GOT_ERR_CANCELLED);
3252 if (parent_path[0]) {
3253 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3254 return got_error_from_errno("asprintf");
3255 } else {
3256 path = de->d_name;
3259 if (de->d_type != DT_DIR &&
3260 got_path_is_child(path, a->status_path, a->status_path_len)
3261 && !match_ignores(&a->ignores, path))
3262 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3263 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3264 if (parent_path[0])
3265 free(path);
3266 return err;
3269 static const struct got_error *
3270 status_traverse(void *arg, const char *path, int dirfd)
3272 const struct got_error *err = NULL;
3273 struct diff_dir_cb_arg *a = arg;
3275 if (a->no_ignores)
3276 return NULL;
3278 err = add_ignores(&a->ignores, a->worktree->root_path,
3279 path, dirfd, ".cvsignore");
3280 if (err)
3281 return err;
3283 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3284 dirfd, ".gitignore");
3286 return err;
3289 static const struct got_error *
3290 report_single_file_status(const char *path, const char *ondisk_path,
3291 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3292 void *status_arg, struct got_repository *repo, int report_unchanged)
3294 struct got_fileindex_entry *ie;
3295 struct stat sb;
3297 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3298 if (ie)
3299 return report_file_status(ie, ondisk_path, -1, NULL,
3300 status_cb, status_arg, repo, report_unchanged);
3302 if (lstat(ondisk_path, &sb) == -1) {
3303 if (errno != ENOENT)
3304 return got_error_from_errno2("lstat", ondisk_path);
3305 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3306 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3307 return NULL;
3310 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3311 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3312 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3314 return NULL;
3317 static const struct got_error *
3318 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3319 const char *root_path, const char *path)
3321 const struct got_error *err;
3322 char *parent_path, *next_parent_path = NULL;
3324 err = add_ignores(ignores, root_path, "", -1,
3325 ".cvsignore");
3326 if (err)
3327 return err;
3329 err = add_ignores(ignores, root_path, "", -1,
3330 ".gitignore");
3331 if (err)
3332 return err;
3334 err = got_path_dirname(&parent_path, path);
3335 if (err) {
3336 if (err->code == GOT_ERR_BAD_PATH)
3337 return NULL; /* cannot traverse parent */
3338 return err;
3340 for (;;) {
3341 err = add_ignores(ignores, root_path, parent_path, -1,
3342 ".cvsignore");
3343 if (err)
3344 break;
3345 err = add_ignores(ignores, root_path, parent_path, -1,
3346 ".gitignore");
3347 if (err)
3348 break;
3349 err = got_path_dirname(&next_parent_path, parent_path);
3350 if (err) {
3351 if (err->code == GOT_ERR_BAD_PATH)
3352 err = NULL; /* traversed everything */
3353 break;
3355 free(parent_path);
3356 parent_path = next_parent_path;
3357 next_parent_path = NULL;
3360 free(parent_path);
3361 free(next_parent_path);
3362 return err;
3365 static const struct got_error *
3366 worktree_status(struct got_worktree *worktree, const char *path,
3367 struct got_fileindex *fileindex, struct got_repository *repo,
3368 got_worktree_status_cb status_cb, void *status_arg,
3369 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3370 int report_unchanged)
3372 const struct got_error *err = NULL;
3373 int fd = -1;
3374 struct got_fileindex_diff_dir_cb fdiff_cb;
3375 struct diff_dir_cb_arg arg;
3376 char *ondisk_path = NULL;
3378 TAILQ_INIT(&arg.ignores);
3380 if (asprintf(&ondisk_path, "%s%s%s",
3381 worktree->root_path, path[0] ? "/" : "", path) == -1)
3382 return got_error_from_errno("asprintf");
3384 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3385 if (fd == -1) {
3386 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3387 errno != ELOOP)
3388 err = got_error_from_errno2("open", ondisk_path);
3389 else
3390 err = report_single_file_status(path, ondisk_path,
3391 fileindex, status_cb, status_arg, repo,
3392 report_unchanged);
3393 } else {
3394 fdiff_cb.diff_old_new = status_old_new;
3395 fdiff_cb.diff_old = status_old;
3396 fdiff_cb.diff_new = status_new;
3397 fdiff_cb.diff_traverse = status_traverse;
3398 arg.fileindex = fileindex;
3399 arg.worktree = worktree;
3400 arg.status_path = path;
3401 arg.status_path_len = strlen(path);
3402 arg.repo = repo;
3403 arg.status_cb = status_cb;
3404 arg.status_arg = status_arg;
3405 arg.cancel_cb = cancel_cb;
3406 arg.cancel_arg = cancel_arg;
3407 arg.report_unchanged = report_unchanged;
3408 arg.no_ignores = no_ignores;
3409 if (!no_ignores) {
3410 err = add_ignores_from_parent_paths(&arg.ignores,
3411 worktree->root_path, path);
3412 if (err)
3413 goto done;
3415 err = got_fileindex_diff_dir(fileindex, fd,
3416 worktree->root_path, path, repo, &fdiff_cb, &arg);
3418 done:
3419 free_ignores(&arg.ignores);
3420 if (fd != -1 && close(fd) != 0 && err == NULL)
3421 err = got_error_from_errno("close");
3422 free(ondisk_path);
3423 return err;
3426 const struct got_error *
3427 got_worktree_status(struct got_worktree *worktree,
3428 struct got_pathlist_head *paths, struct got_repository *repo,
3429 got_worktree_status_cb status_cb, void *status_arg,
3430 got_cancel_cb cancel_cb, void *cancel_arg)
3432 const struct got_error *err = NULL;
3433 char *fileindex_path = NULL;
3434 struct got_fileindex *fileindex = NULL;
3435 struct got_pathlist_entry *pe;
3437 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3438 if (err)
3439 return err;
3441 TAILQ_FOREACH(pe, paths, entry) {
3442 err = worktree_status(worktree, pe->path, fileindex, repo,
3443 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3444 if (err)
3445 break;
3447 free(fileindex_path);
3448 got_fileindex_free(fileindex);
3449 return err;
3452 const struct got_error *
3453 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3454 const char *arg)
3456 const struct got_error *err = NULL;
3457 char *resolved = NULL, *cwd = NULL, *path = NULL;
3458 size_t len;
3459 struct stat sb;
3461 *wt_path = NULL;
3463 cwd = getcwd(NULL, 0);
3464 if (cwd == NULL)
3465 return got_error_from_errno("getcwd");
3467 if (lstat(arg, &sb) == -1) {
3468 if (errno != ENOENT) {
3469 err = got_error_from_errno2("lstat", arg);
3470 goto done;
3473 if (S_ISLNK(sb.st_mode)) {
3475 * We cannot use realpath(3) with symlinks since we want to
3476 * operate on the symlink itself.
3477 * But we can make the path absolute, assuming it is relative
3478 * to the current working directory, and then canonicalize it.
3480 char *abspath = NULL;
3481 char canonpath[PATH_MAX];
3482 if (!got_path_is_absolute(arg)) {
3483 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3484 err = got_error_from_errno("asprintf");
3485 goto done;
3489 err = got_canonpath(abspath ? abspath : arg, canonpath,
3490 sizeof(canonpath));
3491 if (err)
3492 goto done;
3493 resolved = strdup(canonpath);
3494 if (resolved == NULL) {
3495 err = got_error_from_errno("strdup");
3496 goto done;
3498 } else {
3499 resolved = realpath(arg, NULL);
3500 if (resolved == NULL) {
3501 if (errno != ENOENT) {
3502 err = got_error_from_errno2("realpath", arg);
3503 goto done;
3505 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3506 err = got_error_from_errno("asprintf");
3507 goto done;
3512 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3513 strlen(got_worktree_get_root_path(worktree)))) {
3514 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3515 goto done;
3518 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3519 err = got_path_skip_common_ancestor(&path,
3520 got_worktree_get_root_path(worktree), resolved);
3521 if (err)
3522 goto done;
3523 } else {
3524 path = strdup("");
3525 if (path == NULL) {
3526 err = got_error_from_errno("strdup");
3527 goto done;
3531 /* XXX status walk can't deal with trailing slash! */
3532 len = strlen(path);
3533 while (len > 0 && path[len - 1] == '/') {
3534 path[len - 1] = '\0';
3535 len--;
3537 done:
3538 free(resolved);
3539 free(cwd);
3540 if (err == NULL)
3541 *wt_path = path;
3542 else
3543 free(path);
3544 return err;
3547 struct schedule_addition_args {
3548 struct got_worktree *worktree;
3549 struct got_fileindex *fileindex;
3550 got_worktree_checkout_cb progress_cb;
3551 void *progress_arg;
3552 struct got_repository *repo;
3555 static const struct got_error *
3556 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3557 const char *relpath, struct got_object_id *blob_id,
3558 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3559 int dirfd, const char *de_name)
3561 struct schedule_addition_args *a = arg;
3562 const struct got_error *err = NULL;
3563 struct got_fileindex_entry *ie;
3564 struct stat sb;
3565 char *ondisk_path;
3567 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3568 relpath) == -1)
3569 return got_error_from_errno("asprintf");
3571 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3572 if (ie) {
3573 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3574 de_name, a->repo);
3575 if (err)
3576 goto done;
3577 /* Re-adding an existing entry is a no-op. */
3578 if (status == GOT_STATUS_ADD)
3579 goto done;
3580 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3581 if (err)
3582 goto done;
3585 if (status != GOT_STATUS_UNVERSIONED) {
3586 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3587 goto done;
3590 err = got_fileindex_entry_alloc(&ie, relpath);
3591 if (err)
3592 goto done;
3593 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3594 if (err) {
3595 got_fileindex_entry_free(ie);
3596 goto done;
3598 err = got_fileindex_entry_add(a->fileindex, ie);
3599 if (err) {
3600 got_fileindex_entry_free(ie);
3601 goto done;
3603 done:
3604 free(ondisk_path);
3605 if (err)
3606 return err;
3607 if (status == GOT_STATUS_ADD)
3608 return NULL;
3609 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3612 const struct got_error *
3613 got_worktree_schedule_add(struct got_worktree *worktree,
3614 struct got_pathlist_head *paths,
3615 got_worktree_checkout_cb progress_cb, void *progress_arg,
3616 struct got_repository *repo, int no_ignores)
3618 struct got_fileindex *fileindex = NULL;
3619 char *fileindex_path = NULL;
3620 const struct got_error *err = NULL, *sync_err, *unlockerr;
3621 struct got_pathlist_entry *pe;
3622 struct schedule_addition_args saa;
3624 err = lock_worktree(worktree, LOCK_EX);
3625 if (err)
3626 return err;
3628 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3629 if (err)
3630 goto done;
3632 saa.worktree = worktree;
3633 saa.fileindex = fileindex;
3634 saa.progress_cb = progress_cb;
3635 saa.progress_arg = progress_arg;
3636 saa.repo = repo;
3638 TAILQ_FOREACH(pe, paths, entry) {
3639 err = worktree_status(worktree, pe->path, fileindex, repo,
3640 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3641 if (err)
3642 break;
3644 sync_err = sync_fileindex(fileindex, fileindex_path);
3645 if (sync_err && err == NULL)
3646 err = sync_err;
3647 done:
3648 free(fileindex_path);
3649 if (fileindex)
3650 got_fileindex_free(fileindex);
3651 unlockerr = lock_worktree(worktree, LOCK_SH);
3652 if (unlockerr && err == NULL)
3653 err = unlockerr;
3654 return err;
3657 struct schedule_deletion_args {
3658 struct got_worktree *worktree;
3659 struct got_fileindex *fileindex;
3660 got_worktree_delete_cb progress_cb;
3661 void *progress_arg;
3662 struct got_repository *repo;
3663 int delete_local_mods;
3664 int keep_on_disk;
3667 static const struct got_error *
3668 schedule_for_deletion(void *arg, unsigned char status,
3669 unsigned char staged_status, const char *relpath,
3670 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3671 struct got_object_id *commit_id, int dirfd, const char *de_name)
3673 struct schedule_deletion_args *a = arg;
3674 const struct got_error *err = NULL;
3675 struct got_fileindex_entry *ie = NULL;
3676 struct stat sb;
3677 char *ondisk_path, *parent = NULL;
3679 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3680 if (ie == NULL)
3681 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3683 staged_status = get_staged_status(ie);
3684 if (staged_status != GOT_STATUS_NO_CHANGE) {
3685 if (staged_status == GOT_STATUS_DELETE)
3686 return NULL;
3687 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3690 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3691 relpath) == -1)
3692 return got_error_from_errno("asprintf");
3694 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3695 a->repo);
3696 if (err)
3697 goto done;
3699 if (status != GOT_STATUS_NO_CHANGE) {
3700 if (status == GOT_STATUS_DELETE)
3701 goto done;
3702 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3703 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3704 goto done;
3706 if (status != GOT_STATUS_MODIFY &&
3707 status != GOT_STATUS_MISSING) {
3708 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3709 goto done;
3713 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3714 if (dirfd != -1) {
3715 if (unlinkat(dirfd, de_name, 0) != 0) {
3716 err = got_error_from_errno2("unlinkat",
3717 ondisk_path);
3718 goto done;
3720 } else if (unlink(ondisk_path) != 0) {
3721 err = got_error_from_errno2("unlink", ondisk_path);
3722 goto done;
3725 parent = dirname(ondisk_path);
3727 if (parent == NULL) {
3728 err = got_error_from_errno2("dirname", ondisk_path);
3729 goto done;
3731 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3732 if (rmdir(parent) == -1) {
3733 if (errno != ENOTEMPTY)
3734 err = got_error_from_errno2("rmdir",
3735 parent);
3736 break;
3738 parent = dirname(parent);
3739 if (parent == NULL) {
3740 err = got_error_from_errno2("dirname", parent);
3741 goto done;
3746 got_fileindex_entry_mark_deleted_from_disk(ie);
3747 done:
3748 free(ondisk_path);
3749 if (err)
3750 return err;
3751 if (status == GOT_STATUS_DELETE)
3752 return NULL;
3753 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3754 staged_status, relpath);
3757 const struct got_error *
3758 got_worktree_schedule_delete(struct got_worktree *worktree,
3759 struct got_pathlist_head *paths, int delete_local_mods,
3760 got_worktree_delete_cb progress_cb, void *progress_arg,
3761 struct got_repository *repo, int keep_on_disk)
3763 struct got_fileindex *fileindex = NULL;
3764 char *fileindex_path = NULL;
3765 const struct got_error *err = NULL, *sync_err, *unlockerr;
3766 struct got_pathlist_entry *pe;
3767 struct schedule_deletion_args sda;
3769 err = lock_worktree(worktree, LOCK_EX);
3770 if (err)
3771 return err;
3773 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3774 if (err)
3775 goto done;
3777 sda.worktree = worktree;
3778 sda.fileindex = fileindex;
3779 sda.progress_cb = progress_cb;
3780 sda.progress_arg = progress_arg;
3781 sda.repo = repo;
3782 sda.delete_local_mods = delete_local_mods;
3783 sda.keep_on_disk = keep_on_disk;
3785 TAILQ_FOREACH(pe, paths, entry) {
3786 err = worktree_status(worktree, pe->path, fileindex, repo,
3787 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3788 if (err)
3789 break;
3791 sync_err = sync_fileindex(fileindex, fileindex_path);
3792 if (sync_err && err == NULL)
3793 err = sync_err;
3794 done:
3795 free(fileindex_path);
3796 if (fileindex)
3797 got_fileindex_free(fileindex);
3798 unlockerr = lock_worktree(worktree, LOCK_SH);
3799 if (unlockerr && err == NULL)
3800 err = unlockerr;
3801 return err;
3804 static const struct got_error *
3805 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3807 const struct got_error *err = NULL;
3808 char *line = NULL;
3809 size_t linesize = 0, n;
3810 ssize_t linelen;
3812 linelen = getline(&line, &linesize, infile);
3813 if (linelen == -1) {
3814 if (ferror(infile)) {
3815 err = got_error_from_errno("getline");
3816 goto done;
3818 return NULL;
3820 if (outfile) {
3821 n = fwrite(line, 1, linelen, outfile);
3822 if (n != linelen) {
3823 err = got_ferror(outfile, GOT_ERR_IO);
3824 goto done;
3827 if (rejectfile) {
3828 n = fwrite(line, 1, linelen, rejectfile);
3829 if (n != linelen)
3830 err = got_ferror(outfile, GOT_ERR_IO);
3832 done:
3833 free(line);
3834 return err;
3837 static const struct got_error *
3838 skip_one_line(FILE *f)
3840 char *line = NULL;
3841 size_t linesize = 0;
3842 ssize_t linelen;
3844 linelen = getline(&line, &linesize, f);
3845 if (linelen == -1) {
3846 if (ferror(f))
3847 return got_error_from_errno("getline");
3848 return NULL;
3850 free(line);
3851 return NULL;
3854 static const struct got_error *
3855 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3856 int start_old, int end_old, int start_new, int end_new,
3857 FILE *outfile, FILE *rejectfile)
3859 const struct got_error *err;
3861 /* Copy old file's lines leading up to patch. */
3862 while (!feof(f1) && *line_cur1 < start_old) {
3863 err = copy_one_line(f1, outfile, NULL);
3864 if (err)
3865 return err;
3866 (*line_cur1)++;
3868 /* Skip new file's lines leading up to patch. */
3869 while (!feof(f2) && *line_cur2 < start_new) {
3870 if (rejectfile)
3871 err = copy_one_line(f2, NULL, rejectfile);
3872 else
3873 err = skip_one_line(f2);
3874 if (err)
3875 return err;
3876 (*line_cur2)++;
3878 /* Copy patched lines. */
3879 while (!feof(f2) && *line_cur2 <= end_new) {
3880 err = copy_one_line(f2, outfile, NULL);
3881 if (err)
3882 return err;
3883 (*line_cur2)++;
3885 /* Skip over old file's replaced lines. */
3886 while (!feof(f1) && *line_cur1 <= end_old) {
3887 if (rejectfile)
3888 err = copy_one_line(f1, NULL, rejectfile);
3889 else
3890 err = skip_one_line(f1);
3891 if (err)
3892 return err;
3893 (*line_cur1)++;
3896 return NULL;
3899 static const struct got_error *
3900 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3901 FILE *outfile, FILE *rejectfile)
3903 const struct got_error *err;
3905 if (outfile) {
3906 /* Copy old file's lines until EOF. */
3907 while (!feof(f1)) {
3908 err = copy_one_line(f1, outfile, NULL);
3909 if (err)
3910 return err;
3911 (*line_cur1)++;
3914 if (rejectfile) {
3915 /* Copy new file's lines until EOF. */
3916 while (!feof(f2)) {
3917 err = copy_one_line(f2, NULL, rejectfile);
3918 if (err)
3919 return err;
3920 (*line_cur2)++;
3924 return NULL;
3927 static const struct got_error *
3928 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3929 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3930 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3931 int *line_cur2, FILE *outfile, FILE *rejectfile,
3932 got_worktree_patch_cb patch_cb, void *patch_arg)
3934 const struct got_error *err = NULL;
3935 int start_old = change->cv.a;
3936 int end_old = change->cv.b;
3937 int start_new = change->cv.c;
3938 int end_new = change->cv.d;
3939 long pos1, pos2;
3940 FILE *hunkfile;
3942 *choice = GOT_PATCH_CHOICE_NONE;
3944 hunkfile = got_opentemp();
3945 if (hunkfile == NULL)
3946 return got_error_from_errno("got_opentemp");
3948 pos1 = ftell(f1);
3949 pos2 = ftell(f2);
3951 /* XXX TODO needs error checking */
3952 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3954 if (fseek(f1, pos1, SEEK_SET) == -1) {
3955 err = got_ferror(f1, GOT_ERR_IO);
3956 goto done;
3958 if (fseek(f2, pos2, SEEK_SET) == -1) {
3959 err = got_ferror(f1, GOT_ERR_IO);
3960 goto done;
3962 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3963 err = got_ferror(hunkfile, GOT_ERR_IO);
3964 goto done;
3967 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3968 hunkfile, n, nchanges);
3969 if (err)
3970 goto done;
3972 switch (*choice) {
3973 case GOT_PATCH_CHOICE_YES:
3974 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3975 end_old, start_new, end_new, outfile, rejectfile);
3976 break;
3977 case GOT_PATCH_CHOICE_NO:
3978 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3979 end_old, start_new, end_new, rejectfile, outfile);
3980 break;
3981 case GOT_PATCH_CHOICE_QUIT:
3982 break;
3983 default:
3984 err = got_error(GOT_ERR_PATCH_CHOICE);
3985 break;
3987 done:
3988 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3989 err = got_error_from_errno("fclose");
3990 return err;
3993 struct revert_file_args {
3994 struct got_worktree *worktree;
3995 struct got_fileindex *fileindex;
3996 got_worktree_checkout_cb progress_cb;
3997 void *progress_arg;
3998 got_worktree_patch_cb patch_cb;
3999 void *patch_arg;
4000 struct got_repository *repo;
4003 static const struct got_error *
4004 create_patched_content(char **path_outfile, int reverse_patch,
4005 struct got_object_id *blob_id, const char *path2,
4006 int dirfd2, const char *de_name2,
4007 const char *relpath, struct got_repository *repo,
4008 got_worktree_patch_cb patch_cb, void *patch_arg)
4010 const struct got_error *err;
4011 struct got_blob_object *blob = NULL;
4012 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4013 int fd2 = -1;
4014 char *path1 = NULL, *id_str = NULL;
4015 struct stat sb1, sb2;
4016 struct got_diff_changes *changes = NULL;
4017 struct got_diff_state *ds = NULL;
4018 struct got_diff_args *args = NULL;
4019 struct got_diff_change *change;
4020 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4021 int n = 0;
4023 *path_outfile = NULL;
4025 err = got_object_id_str(&id_str, blob_id);
4026 if (err)
4027 return err;
4029 if (dirfd2 != -1) {
4030 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4031 if (fd2 == -1) {
4032 err = got_error_from_errno2("openat", path2);
4033 goto done;
4035 } else {
4036 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4037 if (fd2 == -1) {
4038 err = got_error_from_errno2("open", path2);
4039 goto done;
4042 if (fstat(fd2, &sb2) == -1) {
4043 err = got_error_from_errno2("fstat", path2);
4044 goto done;
4047 f2 = fdopen(fd2, "r");
4048 if (f2 == NULL) {
4049 err = got_error_from_errno2("fdopen", path2);
4050 goto done;
4052 fd2 = -1;
4054 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4055 if (err)
4056 goto done;
4058 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4059 if (err)
4060 goto done;
4062 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4063 if (err)
4064 goto done;
4066 if (stat(path1, &sb1) == -1) {
4067 err = got_error_from_errno2("stat", path1);
4068 goto done;
4071 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4072 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4073 if (err)
4074 goto done;
4076 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4077 if (err)
4078 goto done;
4080 if (fseek(f1, 0L, SEEK_SET) == -1)
4081 return got_ferror(f1, GOT_ERR_IO);
4082 if (fseek(f2, 0L, SEEK_SET) == -1)
4083 return got_ferror(f2, GOT_ERR_IO);
4084 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4085 int choice;
4086 err = apply_or_reject_change(&choice, change, ++n,
4087 changes->nchanges, ds, args, diff_flags, relpath,
4088 f1, f2, &line_cur1, &line_cur2,
4089 reverse_patch ? NULL : outfile,
4090 reverse_patch ? outfile : NULL,
4091 patch_cb, patch_arg);
4092 if (err)
4093 goto done;
4094 if (choice == GOT_PATCH_CHOICE_YES)
4095 have_content = 1;
4096 else if (choice == GOT_PATCH_CHOICE_QUIT)
4097 break;
4099 if (have_content) {
4100 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4101 reverse_patch ? NULL : outfile,
4102 reverse_patch ? outfile : NULL);
4103 if (err)
4104 goto done;
4106 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4107 err = got_error_from_errno2("chmod", path2);
4108 goto done;
4111 done:
4112 free(id_str);
4113 if (blob)
4114 got_object_blob_close(blob);
4115 if (f1 && fclose(f1) == EOF && err == NULL)
4116 err = got_error_from_errno2("fclose", path1);
4117 if (f2 && fclose(f2) == EOF && err == NULL)
4118 err = got_error_from_errno2("fclose", path2);
4119 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4120 err = got_error_from_errno2("close", path2);
4121 if (outfile && fclose(outfile) == EOF && err == NULL)
4122 err = got_error_from_errno2("fclose", *path_outfile);
4123 if (path1 && unlink(path1) == -1 && err == NULL)
4124 err = got_error_from_errno2("unlink", path1);
4125 if (err || !have_content) {
4126 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4127 err = got_error_from_errno2("unlink", *path_outfile);
4128 free(*path_outfile);
4129 *path_outfile = NULL;
4131 free(args);
4132 if (ds) {
4133 got_diff_state_free(ds);
4134 free(ds);
4136 if (changes)
4137 got_diff_free_changes(changes);
4138 free(path1);
4139 return err;
4142 static const struct got_error *
4143 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4144 const char *relpath, struct got_object_id *blob_id,
4145 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4146 int dirfd, const char *de_name)
4148 struct revert_file_args *a = arg;
4149 const struct got_error *err = NULL;
4150 char *parent_path = NULL;
4151 struct got_fileindex_entry *ie;
4152 struct got_tree_object *tree = NULL;
4153 struct got_object_id *tree_id = NULL;
4154 const struct got_tree_entry *te = NULL;
4155 char *tree_path = NULL, *te_name;
4156 char *ondisk_path = NULL, *path_content = NULL;
4157 struct got_blob_object *blob = NULL;
4159 /* Reverting a staged deletion is a no-op. */
4160 if (status == GOT_STATUS_DELETE &&
4161 staged_status != GOT_STATUS_NO_CHANGE)
4162 return NULL;
4164 if (status == GOT_STATUS_UNVERSIONED)
4165 return (*a->progress_cb)(a->progress_arg,
4166 GOT_STATUS_UNVERSIONED, relpath);
4168 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4169 if (ie == NULL)
4170 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4172 /* Construct in-repository path of tree which contains this blob. */
4173 err = got_path_dirname(&parent_path, ie->path);
4174 if (err) {
4175 if (err->code != GOT_ERR_BAD_PATH)
4176 goto done;
4177 parent_path = strdup("/");
4178 if (parent_path == NULL) {
4179 err = got_error_from_errno("strdup");
4180 goto done;
4183 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4184 tree_path = strdup(parent_path);
4185 if (tree_path == NULL) {
4186 err = got_error_from_errno("strdup");
4187 goto done;
4189 } else {
4190 if (got_path_is_root_dir(parent_path)) {
4191 tree_path = strdup(a->worktree->path_prefix);
4192 if (tree_path == NULL) {
4193 err = got_error_from_errno("strdup");
4194 goto done;
4196 } else {
4197 if (asprintf(&tree_path, "%s/%s",
4198 a->worktree->path_prefix, parent_path) == -1) {
4199 err = got_error_from_errno("asprintf");
4200 goto done;
4205 err = got_object_id_by_path(&tree_id, a->repo,
4206 a->worktree->base_commit_id, tree_path);
4207 if (err) {
4208 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4209 (status == GOT_STATUS_ADD ||
4210 staged_status == GOT_STATUS_ADD)))
4211 goto done;
4212 } else {
4213 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4214 if (err)
4215 goto done;
4217 te_name = basename(ie->path);
4218 if (te_name == NULL) {
4219 err = got_error_from_errno2("basename", ie->path);
4220 goto done;
4223 te = got_object_tree_find_entry(tree, te_name);
4224 if (te == NULL && status != GOT_STATUS_ADD &&
4225 staged_status != GOT_STATUS_ADD) {
4226 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4227 goto done;
4231 switch (status) {
4232 case GOT_STATUS_ADD:
4233 if (a->patch_cb) {
4234 int choice = GOT_PATCH_CHOICE_NONE;
4235 err = (*a->patch_cb)(&choice, a->patch_arg,
4236 status, ie->path, NULL, 1, 1);
4237 if (err)
4238 goto done;
4239 if (choice != GOT_PATCH_CHOICE_YES)
4240 break;
4242 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4243 ie->path);
4244 if (err)
4245 goto done;
4246 got_fileindex_entry_remove(a->fileindex, ie);
4247 break;
4248 case GOT_STATUS_DELETE:
4249 if (a->patch_cb) {
4250 int choice = GOT_PATCH_CHOICE_NONE;
4251 err = (*a->patch_cb)(&choice, a->patch_arg,
4252 status, ie->path, NULL, 1, 1);
4253 if (err)
4254 goto done;
4255 if (choice != GOT_PATCH_CHOICE_YES)
4256 break;
4258 /* fall through */
4259 case GOT_STATUS_MODIFY:
4260 case GOT_STATUS_MODE_CHANGE:
4261 case GOT_STATUS_CONFLICT:
4262 case GOT_STATUS_MISSING: {
4263 struct got_object_id id;
4264 if (staged_status == GOT_STATUS_ADD ||
4265 staged_status == GOT_STATUS_MODIFY) {
4266 memcpy(id.sha1, ie->staged_blob_sha1,
4267 SHA1_DIGEST_LENGTH);
4268 } else
4269 memcpy(id.sha1, ie->blob_sha1,
4270 SHA1_DIGEST_LENGTH);
4271 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4272 if (err)
4273 goto done;
4275 if (asprintf(&ondisk_path, "%s/%s",
4276 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4277 err = got_error_from_errno("asprintf");
4278 goto done;
4281 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4282 status == GOT_STATUS_CONFLICT)) {
4283 err = create_patched_content(&path_content, 1, &id,
4284 ondisk_path, dirfd, de_name, ie->path, a->repo,
4285 a->patch_cb, a->patch_arg);
4286 if (err || path_content == NULL)
4287 break;
4288 if (rename(path_content, ondisk_path) == -1) {
4289 err = got_error_from_errno3("rename",
4290 path_content, ondisk_path);
4291 goto done;
4293 } else {
4294 int is_bad_symlink = 0;
4295 if (te && S_ISLNK(te->mode)) {
4296 err = install_symlink(&is_bad_symlink,
4297 a->worktree, ondisk_path, ie->path,
4298 blob, 0, 1, a->repo,
4299 a->progress_cb, a->progress_arg);
4300 } else {
4301 err = install_blob(a->worktree, ondisk_path,
4302 ie->path,
4303 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4304 got_fileindex_perms_to_st(ie), blob, 0, 1, 0,
4305 a->repo, a->progress_cb, a->progress_arg);
4307 if (err)
4308 goto done;
4309 if (status == GOT_STATUS_DELETE ||
4310 status == GOT_STATUS_MODE_CHANGE) {
4311 err = got_fileindex_entry_update(ie,
4312 ondisk_path, blob->id.sha1,
4313 a->worktree->base_commit_id->sha1, 1);
4314 if (err)
4315 goto done;
4317 if (is_bad_symlink) {
4318 err = got_fileindex_entry_filetype_set(ie,
4319 GOT_FILEIDX_MODE_BAD_SYMLINK);
4320 if (err)
4321 goto done;
4324 break;
4326 default:
4327 break;
4329 done:
4330 free(ondisk_path);
4331 free(path_content);
4332 free(parent_path);
4333 free(tree_path);
4334 if (blob)
4335 got_object_blob_close(blob);
4336 if (tree)
4337 got_object_tree_close(tree);
4338 free(tree_id);
4339 return err;
4342 const struct got_error *
4343 got_worktree_revert(struct got_worktree *worktree,
4344 struct got_pathlist_head *paths,
4345 got_worktree_checkout_cb progress_cb, void *progress_arg,
4346 got_worktree_patch_cb patch_cb, void *patch_arg,
4347 struct got_repository *repo)
4349 struct got_fileindex *fileindex = NULL;
4350 char *fileindex_path = NULL;
4351 const struct got_error *err = NULL, *unlockerr = NULL;
4352 const struct got_error *sync_err = NULL;
4353 struct got_pathlist_entry *pe;
4354 struct revert_file_args rfa;
4356 err = lock_worktree(worktree, LOCK_EX);
4357 if (err)
4358 return err;
4360 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4361 if (err)
4362 goto done;
4364 rfa.worktree = worktree;
4365 rfa.fileindex = fileindex;
4366 rfa.progress_cb = progress_cb;
4367 rfa.progress_arg = progress_arg;
4368 rfa.patch_cb = patch_cb;
4369 rfa.patch_arg = patch_arg;
4370 rfa.repo = repo;
4371 TAILQ_FOREACH(pe, paths, entry) {
4372 err = worktree_status(worktree, pe->path, fileindex, repo,
4373 revert_file, &rfa, NULL, NULL, 0, 0);
4374 if (err)
4375 break;
4377 sync_err = sync_fileindex(fileindex, fileindex_path);
4378 if (sync_err && err == NULL)
4379 err = sync_err;
4380 done:
4381 free(fileindex_path);
4382 if (fileindex)
4383 got_fileindex_free(fileindex);
4384 unlockerr = lock_worktree(worktree, LOCK_SH);
4385 if (unlockerr && err == NULL)
4386 err = unlockerr;
4387 return err;
4390 static void
4391 free_commitable(struct got_commitable *ct)
4393 free(ct->path);
4394 free(ct->in_repo_path);
4395 free(ct->ondisk_path);
4396 free(ct->blob_id);
4397 free(ct->base_blob_id);
4398 free(ct->staged_blob_id);
4399 free(ct->base_commit_id);
4400 free(ct);
4403 struct collect_commitables_arg {
4404 struct got_pathlist_head *commitable_paths;
4405 struct got_repository *repo;
4406 struct got_worktree *worktree;
4407 int have_staged_files;
4410 static const struct got_error *
4411 collect_commitables(void *arg, unsigned char status,
4412 unsigned char staged_status, const char *relpath,
4413 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4414 struct got_object_id *commit_id, int dirfd, const char *de_name)
4416 struct collect_commitables_arg *a = arg;
4417 const struct got_error *err = NULL;
4418 struct got_commitable *ct = NULL;
4419 struct got_pathlist_entry *new = NULL;
4420 char *parent_path = NULL, *path = NULL;
4421 struct stat sb;
4423 if (a->have_staged_files) {
4424 if (staged_status != GOT_STATUS_MODIFY &&
4425 staged_status != GOT_STATUS_ADD &&
4426 staged_status != GOT_STATUS_DELETE)
4427 return NULL;
4428 } else {
4429 if (status == GOT_STATUS_CONFLICT)
4430 return got_error(GOT_ERR_COMMIT_CONFLICT);
4432 if (status != GOT_STATUS_MODIFY &&
4433 status != GOT_STATUS_MODE_CHANGE &&
4434 status != GOT_STATUS_ADD &&
4435 status != GOT_STATUS_DELETE)
4436 return NULL;
4439 if (asprintf(&path, "/%s", relpath) == -1) {
4440 err = got_error_from_errno("asprintf");
4441 goto done;
4443 if (strcmp(path, "/") == 0) {
4444 parent_path = strdup("");
4445 if (parent_path == NULL)
4446 return got_error_from_errno("strdup");
4447 } else {
4448 err = got_path_dirname(&parent_path, path);
4449 if (err)
4450 return err;
4453 ct = calloc(1, sizeof(*ct));
4454 if (ct == NULL) {
4455 err = got_error_from_errno("calloc");
4456 goto done;
4459 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4460 relpath) == -1) {
4461 err = got_error_from_errno("asprintf");
4462 goto done;
4464 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4465 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4466 } else {
4467 if (dirfd != -1) {
4468 if (fstatat(dirfd, de_name, &sb,
4469 AT_SYMLINK_NOFOLLOW) == -1) {
4470 err = got_error_from_errno2("fstatat",
4471 ct->ondisk_path);
4472 goto done;
4474 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4475 err = got_error_from_errno2("lstat", ct->ondisk_path);
4476 goto done;
4478 ct->mode = sb.st_mode;
4481 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4482 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4483 relpath) == -1) {
4484 err = got_error_from_errno("asprintf");
4485 goto done;
4488 ct->status = status;
4489 ct->staged_status = staged_status;
4490 ct->blob_id = NULL; /* will be filled in when blob gets created */
4491 if (ct->status != GOT_STATUS_ADD &&
4492 ct->staged_status != GOT_STATUS_ADD) {
4493 ct->base_blob_id = got_object_id_dup(blob_id);
4494 if (ct->base_blob_id == NULL) {
4495 err = got_error_from_errno("got_object_id_dup");
4496 goto done;
4498 ct->base_commit_id = got_object_id_dup(commit_id);
4499 if (ct->base_commit_id == NULL) {
4500 err = got_error_from_errno("got_object_id_dup");
4501 goto done;
4504 if (ct->staged_status == GOT_STATUS_ADD ||
4505 ct->staged_status == GOT_STATUS_MODIFY) {
4506 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4507 if (ct->staged_blob_id == NULL) {
4508 err = got_error_from_errno("got_object_id_dup");
4509 goto done;
4512 ct->path = strdup(path);
4513 if (ct->path == NULL) {
4514 err = got_error_from_errno("strdup");
4515 goto done;
4517 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4518 done:
4519 if (ct && (err || new == NULL))
4520 free_commitable(ct);
4521 free(parent_path);
4522 free(path);
4523 return err;
4526 static const struct got_error *write_tree(struct got_object_id **, int *,
4527 struct got_tree_object *, const char *, struct got_pathlist_head *,
4528 got_worktree_status_cb status_cb, void *status_arg,
4529 struct got_repository *);
4531 static const struct got_error *
4532 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4533 struct got_tree_entry *te, const char *parent_path,
4534 struct got_pathlist_head *commitable_paths,
4535 got_worktree_status_cb status_cb, void *status_arg,
4536 struct got_repository *repo)
4538 const struct got_error *err = NULL;
4539 struct got_tree_object *subtree;
4540 char *subpath;
4542 if (asprintf(&subpath, "%s%s%s", parent_path,
4543 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4544 return got_error_from_errno("asprintf");
4546 err = got_object_open_as_tree(&subtree, repo, &te->id);
4547 if (err)
4548 return err;
4550 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4551 commitable_paths, status_cb, status_arg, repo);
4552 got_object_tree_close(subtree);
4553 free(subpath);
4554 return err;
4557 static const struct got_error *
4558 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4560 const struct got_error *err = NULL;
4561 char *ct_parent_path = NULL;
4563 *match = 0;
4565 if (strchr(ct->in_repo_path, '/') == NULL) {
4566 *match = got_path_is_root_dir(path);
4567 return NULL;
4570 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4571 if (err)
4572 return err;
4573 *match = (strcmp(path, ct_parent_path) == 0);
4574 free(ct_parent_path);
4575 return err;
4578 static mode_t
4579 get_ct_file_mode(struct got_commitable *ct)
4581 if (S_ISLNK(ct->mode))
4582 return S_IFLNK;
4584 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4587 static const struct got_error *
4588 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4589 struct got_tree_entry *te, struct got_commitable *ct)
4591 const struct got_error *err = NULL;
4593 *new_te = NULL;
4595 err = got_object_tree_entry_dup(new_te, te);
4596 if (err)
4597 goto done;
4599 (*new_te)->mode = get_ct_file_mode(ct);
4601 if (ct->staged_status == GOT_STATUS_MODIFY)
4602 memcpy(&(*new_te)->id, ct->staged_blob_id,
4603 sizeof((*new_te)->id));
4604 else
4605 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4606 done:
4607 if (err && *new_te) {
4608 free(*new_te);
4609 *new_te = NULL;
4611 return err;
4614 static const struct got_error *
4615 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4616 struct got_commitable *ct)
4618 const struct got_error *err = NULL;
4619 char *ct_name;
4621 *new_te = NULL;
4623 *new_te = calloc(1, sizeof(**new_te));
4624 if (*new_te == NULL)
4625 return got_error_from_errno("calloc");
4627 ct_name = basename(ct->path);
4628 if (ct_name == NULL) {
4629 err = got_error_from_errno2("basename", ct->path);
4630 goto done;
4632 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4633 sizeof((*new_te)->name)) {
4634 err = got_error(GOT_ERR_NO_SPACE);
4635 goto done;
4638 (*new_te)->mode = get_ct_file_mode(ct);
4640 if (ct->staged_status == GOT_STATUS_ADD)
4641 memcpy(&(*new_te)->id, ct->staged_blob_id,
4642 sizeof((*new_te)->id));
4643 else
4644 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4645 done:
4646 if (err && *new_te) {
4647 free(*new_te);
4648 *new_te = NULL;
4650 return err;
4653 static const struct got_error *
4654 insert_tree_entry(struct got_tree_entry *new_te,
4655 struct got_pathlist_head *paths)
4657 const struct got_error *err = NULL;
4658 struct got_pathlist_entry *new_pe;
4660 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4661 if (err)
4662 return err;
4663 if (new_pe == NULL)
4664 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4665 return NULL;
4668 static const struct got_error *
4669 report_ct_status(struct got_commitable *ct,
4670 got_worktree_status_cb status_cb, void *status_arg)
4672 const char *ct_path = ct->path;
4673 unsigned char status;
4675 while (ct_path[0] == '/')
4676 ct_path++;
4678 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4679 status = ct->staged_status;
4680 else
4681 status = ct->status;
4683 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4684 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4687 static const struct got_error *
4688 match_modified_subtree(int *modified, struct got_tree_entry *te,
4689 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4691 const struct got_error *err = NULL;
4692 struct got_pathlist_entry *pe;
4693 char *te_path;
4695 *modified = 0;
4697 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4698 got_path_is_root_dir(base_tree_path) ? "" : "/",
4699 te->name) == -1)
4700 return got_error_from_errno("asprintf");
4702 TAILQ_FOREACH(pe, commitable_paths, entry) {
4703 struct got_commitable *ct = pe->data;
4704 *modified = got_path_is_child(ct->in_repo_path, te_path,
4705 strlen(te_path));
4706 if (*modified)
4707 break;
4710 free(te_path);
4711 return err;
4714 static const struct got_error *
4715 match_deleted_or_modified_ct(struct got_commitable **ctp,
4716 struct got_tree_entry *te, const char *base_tree_path,
4717 struct got_pathlist_head *commitable_paths)
4719 const struct got_error *err = NULL;
4720 struct got_pathlist_entry *pe;
4722 *ctp = NULL;
4724 TAILQ_FOREACH(pe, commitable_paths, entry) {
4725 struct got_commitable *ct = pe->data;
4726 char *ct_name = NULL;
4727 int path_matches;
4729 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4730 if (ct->status != GOT_STATUS_MODIFY &&
4731 ct->status != GOT_STATUS_MODE_CHANGE &&
4732 ct->status != GOT_STATUS_DELETE)
4733 continue;
4734 } else {
4735 if (ct->staged_status != GOT_STATUS_MODIFY &&
4736 ct->staged_status != GOT_STATUS_DELETE)
4737 continue;
4740 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4741 continue;
4743 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4744 if (err)
4745 return err;
4746 if (!path_matches)
4747 continue;
4749 ct_name = basename(pe->path);
4750 if (ct_name == NULL)
4751 return got_error_from_errno2("basename", pe->path);
4753 if (strcmp(te->name, ct_name) != 0)
4754 continue;
4756 *ctp = ct;
4757 break;
4760 return err;
4763 static const struct got_error *
4764 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4765 const char *child_path, const char *path_base_tree,
4766 struct got_pathlist_head *commitable_paths,
4767 got_worktree_status_cb status_cb, void *status_arg,
4768 struct got_repository *repo)
4770 const struct got_error *err = NULL;
4771 struct got_tree_entry *new_te;
4772 char *subtree_path;
4773 struct got_object_id *id = NULL;
4774 int nentries;
4776 *new_tep = NULL;
4778 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4779 got_path_is_root_dir(path_base_tree) ? "" : "/",
4780 child_path) == -1)
4781 return got_error_from_errno("asprintf");
4783 new_te = calloc(1, sizeof(*new_te));
4784 if (new_te == NULL)
4785 return got_error_from_errno("calloc");
4786 new_te->mode = S_IFDIR;
4788 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4789 sizeof(new_te->name)) {
4790 err = got_error(GOT_ERR_NO_SPACE);
4791 goto done;
4793 err = write_tree(&id, &nentries, NULL, subtree_path,
4794 commitable_paths, status_cb, status_arg, repo);
4795 if (err) {
4796 free(new_te);
4797 goto done;
4799 memcpy(&new_te->id, id, sizeof(new_te->id));
4800 done:
4801 free(id);
4802 free(subtree_path);
4803 if (err == NULL)
4804 *new_tep = new_te;
4805 return err;
4808 static const struct got_error *
4809 write_tree(struct got_object_id **new_tree_id, int *nentries,
4810 struct got_tree_object *base_tree, const char *path_base_tree,
4811 struct got_pathlist_head *commitable_paths,
4812 got_worktree_status_cb status_cb, void *status_arg,
4813 struct got_repository *repo)
4815 const struct got_error *err = NULL;
4816 struct got_pathlist_head paths;
4817 struct got_tree_entry *te, *new_te = NULL;
4818 struct got_pathlist_entry *pe;
4820 TAILQ_INIT(&paths);
4821 *nentries = 0;
4823 /* Insert, and recurse into, newly added entries first. */
4824 TAILQ_FOREACH(pe, commitable_paths, entry) {
4825 struct got_commitable *ct = pe->data;
4826 char *child_path = NULL, *slash;
4828 if ((ct->status != GOT_STATUS_ADD &&
4829 ct->staged_status != GOT_STATUS_ADD) ||
4830 (ct->flags & GOT_COMMITABLE_ADDED))
4831 continue;
4833 if (!got_path_is_child(pe->path, path_base_tree,
4834 strlen(path_base_tree)))
4835 continue;
4837 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4838 pe->path);
4839 if (err)
4840 goto done;
4842 slash = strchr(child_path, '/');
4843 if (slash == NULL) {
4844 err = alloc_added_blob_tree_entry(&new_te, ct);
4845 if (err)
4846 goto done;
4847 err = report_ct_status(ct, status_cb, status_arg);
4848 if (err)
4849 goto done;
4850 ct->flags |= GOT_COMMITABLE_ADDED;
4851 err = insert_tree_entry(new_te, &paths);
4852 if (err)
4853 goto done;
4854 (*nentries)++;
4855 } else {
4856 *slash = '\0'; /* trim trailing path components */
4857 if (base_tree == NULL ||
4858 got_object_tree_find_entry(base_tree, child_path)
4859 == NULL) {
4860 err = make_subtree_for_added_blob(&new_te,
4861 child_path, path_base_tree,
4862 commitable_paths, status_cb, status_arg,
4863 repo);
4864 if (err)
4865 goto done;
4866 err = insert_tree_entry(new_te, &paths);
4867 if (err)
4868 goto done;
4869 (*nentries)++;
4874 if (base_tree) {
4875 int i, nbase_entries;
4876 /* Handle modified and deleted entries. */
4877 nbase_entries = got_object_tree_get_nentries(base_tree);
4878 for (i = 0; i < nbase_entries; i++) {
4879 struct got_commitable *ct = NULL;
4881 te = got_object_tree_get_entry(base_tree, i);
4882 if (got_object_tree_entry_is_submodule(te)) {
4883 /* Entry is a submodule; just copy it. */
4884 err = got_object_tree_entry_dup(&new_te, te);
4885 if (err)
4886 goto done;
4887 err = insert_tree_entry(new_te, &paths);
4888 if (err)
4889 goto done;
4890 (*nentries)++;
4891 continue;
4894 if (S_ISDIR(te->mode)) {
4895 int modified;
4896 err = got_object_tree_entry_dup(&new_te, te);
4897 if (err)
4898 goto done;
4899 err = match_modified_subtree(&modified, te,
4900 path_base_tree, commitable_paths);
4901 if (err)
4902 goto done;
4903 /* Avoid recursion into unmodified subtrees. */
4904 if (modified) {
4905 struct got_object_id *new_id;
4906 int nsubentries;
4907 err = write_subtree(&new_id,
4908 &nsubentries, te,
4909 path_base_tree, commitable_paths,
4910 status_cb, status_arg, repo);
4911 if (err)
4912 goto done;
4913 if (nsubentries == 0) {
4914 /* All entries were deleted. */
4915 free(new_id);
4916 continue;
4918 memcpy(&new_te->id, new_id,
4919 sizeof(new_te->id));
4920 free(new_id);
4922 err = insert_tree_entry(new_te, &paths);
4923 if (err)
4924 goto done;
4925 (*nentries)++;
4926 continue;
4929 err = match_deleted_or_modified_ct(&ct, te,
4930 path_base_tree, commitable_paths);
4931 if (err)
4932 goto done;
4933 if (ct) {
4934 /* NB: Deleted entries get dropped here. */
4935 if (ct->status == GOT_STATUS_MODIFY ||
4936 ct->status == GOT_STATUS_MODE_CHANGE ||
4937 ct->staged_status == GOT_STATUS_MODIFY) {
4938 err = alloc_modified_blob_tree_entry(
4939 &new_te, te, ct);
4940 if (err)
4941 goto done;
4942 err = insert_tree_entry(new_te, &paths);
4943 if (err)
4944 goto done;
4945 (*nentries)++;
4947 err = report_ct_status(ct, status_cb,
4948 status_arg);
4949 if (err)
4950 goto done;
4951 } else {
4952 /* Entry is unchanged; just copy it. */
4953 err = got_object_tree_entry_dup(&new_te, te);
4954 if (err)
4955 goto done;
4956 err = insert_tree_entry(new_te, &paths);
4957 if (err)
4958 goto done;
4959 (*nentries)++;
4964 /* Write new list of entries; deleted entries have been dropped. */
4965 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4966 done:
4967 got_pathlist_free(&paths);
4968 return err;
4971 static const struct got_error *
4972 reinstall_symlink_after_commit(int *is_bad_symlink, struct got_commitable *ct,
4973 struct got_object_id *new_base_commit_id, struct got_worktree *worktree,
4974 struct got_repository *repo)
4976 const struct got_error *err = NULL;
4977 struct got_blob_object *blob = NULL;
4978 struct got_object_id *tree_id = NULL;
4979 char *tree_path = NULL;
4980 struct got_tree_object *tree = NULL;
4981 struct got_tree_entry *te;
4982 char *entry_name;
4984 err = got_object_open_as_blob(&blob, repo, ct->blob_id, PATH_MAX);
4985 if (err)
4986 return err;
4988 err = got_path_dirname(&tree_path, ct->in_repo_path);
4989 if (err) {
4990 if (err->code != GOT_ERR_BAD_PATH)
4991 goto done;
4992 err = got_object_id_by_path(&tree_id, repo,
4993 new_base_commit_id, "");
4994 if (err)
4995 goto done;
4996 } else {
4997 err = got_object_id_by_path(&tree_id, repo,
4998 new_base_commit_id, tree_path);
4999 if (err)
5000 goto done;
5003 err = got_object_open_as_tree(&tree, repo, tree_id);
5004 if (err)
5005 goto done;
5007 entry_name = basename(ct->path);
5008 if (entry_name == NULL) {
5009 err = got_error_from_errno2("basename", ct->path);
5010 goto done;
5013 te = got_object_tree_find_entry(tree, entry_name);
5014 if (te == NULL) {
5015 err = got_error_path(ct->path, GOT_ERR_NO_TREE_ENTRY);
5016 goto done;
5019 err = install_symlink(is_bad_symlink, worktree, ct->ondisk_path,
5020 ct->path, blob, 0, 0, repo, NULL, NULL);
5021 done:
5022 if (blob)
5023 got_object_blob_close(blob);
5024 if (tree)
5025 got_object_tree_close(tree);
5026 free(tree_id);
5027 free(tree_path);
5028 return err;
5032 * After comitting a symlink we have a chance to convert "bad" symlinks
5033 * (those which point outside the work tree or into .got) to regular files.
5034 * This way, the post-commit work tree state matches a fresh checkout of
5035 * the tree which was just committed. We also mark such newly committed
5036 * symlinks as "bad" in the work tree's fileindex.
5038 static const struct got_error *
5039 reinstall_symlinks_after_commit(struct got_pathlist_head *commitable_paths,
5040 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5041 struct got_worktree *worktree, struct got_repository *repo)
5043 const struct got_error *err = NULL;
5044 struct got_pathlist_entry *pe;
5046 TAILQ_FOREACH(pe, commitable_paths, entry) {
5047 struct got_commitable *ct = pe->data;
5048 struct got_fileindex_entry *ie;
5049 int is_bad_symlink = 0;
5051 if (!S_ISLNK(get_ct_file_mode(ct)))
5052 continue;
5054 err = reinstall_symlink_after_commit(&is_bad_symlink,
5055 ct, new_base_commit_id, worktree, repo);
5056 if (err)
5057 break;
5058 ie = got_fileindex_entry_get(fileindex, ct->path,
5059 strlen(ct->path));
5060 if (ie && is_bad_symlink) {
5061 err = got_fileindex_entry_filetype_set(ie,
5062 GOT_FILEIDX_MODE_BAD_SYMLINK);
5063 if (err)
5064 break;
5068 return err;
5071 static const struct got_error *
5072 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5073 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5074 int have_staged_files)
5076 const struct got_error *err = NULL;
5077 struct got_pathlist_entry *pe;
5079 TAILQ_FOREACH(pe, commitable_paths, entry) {
5080 struct got_fileindex_entry *ie;
5081 struct got_commitable *ct = pe->data;
5083 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5084 if (ie) {
5085 if (ct->status == GOT_STATUS_DELETE ||
5086 ct->staged_status == GOT_STATUS_DELETE) {
5087 got_fileindex_entry_remove(fileindex, ie);
5088 } else if (ct->staged_status == GOT_STATUS_ADD ||
5089 ct->staged_status == GOT_STATUS_MODIFY) {
5090 got_fileindex_entry_stage_set(ie,
5091 GOT_FILEIDX_STAGE_NONE);
5092 err = got_fileindex_entry_update(ie,
5093 ct->ondisk_path, ct->staged_blob_id->sha1,
5094 new_base_commit_id->sha1,
5095 !have_staged_files);
5096 } else
5097 err = got_fileindex_entry_update(ie,
5098 ct->ondisk_path, ct->blob_id->sha1,
5099 new_base_commit_id->sha1,
5100 !have_staged_files);
5101 } else {
5102 err = got_fileindex_entry_alloc(&ie, pe->path);
5103 if (err)
5104 break;
5105 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5106 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5107 if (err) {
5108 got_fileindex_entry_free(ie);
5109 break;
5111 err = got_fileindex_entry_add(fileindex, ie);
5112 if (err) {
5113 got_fileindex_entry_free(ie);
5114 break;
5118 return err;
5122 static const struct got_error *
5123 check_out_of_date(const char *in_repo_path, unsigned char status,
5124 unsigned char staged_status, struct got_object_id *base_blob_id,
5125 struct got_object_id *base_commit_id,
5126 struct got_object_id *head_commit_id, struct got_repository *repo,
5127 int ood_errcode)
5129 const struct got_error *err = NULL;
5130 struct got_object_id *id = NULL;
5132 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5133 /* Trivial case: base commit == head commit */
5134 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5135 return NULL;
5137 * Ensure file content which local changes were based
5138 * on matches file content in the branch head.
5140 err = got_object_id_by_path(&id, repo, head_commit_id,
5141 in_repo_path);
5142 if (err) {
5143 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5144 err = got_error(ood_errcode);
5145 goto done;
5146 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5147 err = got_error(ood_errcode);
5148 } else {
5149 /* Require that added files don't exist in the branch head. */
5150 err = got_object_id_by_path(&id, repo, head_commit_id,
5151 in_repo_path);
5152 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5153 goto done;
5154 err = id ? got_error(ood_errcode) : NULL;
5156 done:
5157 free(id);
5158 return err;
5161 const struct got_error *
5162 commit_worktree(struct got_object_id **new_commit_id,
5163 struct got_pathlist_head *commitable_paths,
5164 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5165 const char *author, const char *committer,
5166 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5167 got_worktree_status_cb status_cb, void *status_arg,
5168 struct got_repository *repo)
5170 const struct got_error *err = NULL, *unlockerr = NULL;
5171 struct got_pathlist_entry *pe;
5172 const char *head_ref_name = NULL;
5173 struct got_commit_object *head_commit = NULL;
5174 struct got_reference *head_ref2 = NULL;
5175 struct got_object_id *head_commit_id2 = NULL;
5176 struct got_tree_object *head_tree = NULL;
5177 struct got_object_id *new_tree_id = NULL;
5178 int nentries;
5179 struct got_object_id_queue parent_ids;
5180 struct got_object_qid *pid = NULL;
5181 char *logmsg = NULL;
5183 *new_commit_id = NULL;
5185 SIMPLEQ_INIT(&parent_ids);
5187 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5188 if (err)
5189 goto done;
5191 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5192 if (err)
5193 goto done;
5195 if (commit_msg_cb != NULL) {
5196 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5197 if (err)
5198 goto done;
5201 if (logmsg == NULL || strlen(logmsg) == 0) {
5202 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5203 goto done;
5206 /* Create blobs from added and modified files and record their IDs. */
5207 TAILQ_FOREACH(pe, commitable_paths, entry) {
5208 struct got_commitable *ct = pe->data;
5209 char *ondisk_path;
5211 /* Blobs for staged files already exist. */
5212 if (ct->staged_status == GOT_STATUS_ADD ||
5213 ct->staged_status == GOT_STATUS_MODIFY)
5214 continue;
5216 if (ct->status != GOT_STATUS_ADD &&
5217 ct->status != GOT_STATUS_MODIFY &&
5218 ct->status != GOT_STATUS_MODE_CHANGE)
5219 continue;
5221 if (asprintf(&ondisk_path, "%s/%s",
5222 worktree->root_path, pe->path) == -1) {
5223 err = got_error_from_errno("asprintf");
5224 goto done;
5226 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5227 free(ondisk_path);
5228 if (err)
5229 goto done;
5232 /* Recursively write new tree objects. */
5233 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5234 commitable_paths, status_cb, status_arg, repo);
5235 if (err)
5236 goto done;
5238 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5239 if (err)
5240 goto done;
5241 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5242 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5243 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5244 got_object_qid_free(pid);
5245 if (logmsg != NULL)
5246 free(logmsg);
5247 if (err)
5248 goto done;
5250 /* Check if a concurrent commit to our branch has occurred. */
5251 head_ref_name = got_worktree_get_head_ref_name(worktree);
5252 if (head_ref_name == NULL) {
5253 err = got_error_from_errno("got_worktree_get_head_ref_name");
5254 goto done;
5256 /* Lock the reference here to prevent concurrent modification. */
5257 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5258 if (err)
5259 goto done;
5260 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5261 if (err)
5262 goto done;
5263 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5264 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5265 goto done;
5267 /* Update branch head in repository. */
5268 err = got_ref_change_ref(head_ref2, *new_commit_id);
5269 if (err)
5270 goto done;
5271 err = got_ref_write(head_ref2, repo);
5272 if (err)
5273 goto done;
5275 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5276 if (err)
5277 goto done;
5279 err = ref_base_commit(worktree, repo);
5280 if (err)
5281 goto done;
5282 done:
5283 if (head_tree)
5284 got_object_tree_close(head_tree);
5285 if (head_commit)
5286 got_object_commit_close(head_commit);
5287 free(head_commit_id2);
5288 if (head_ref2) {
5289 unlockerr = got_ref_unlock(head_ref2);
5290 if (unlockerr && err == NULL)
5291 err = unlockerr;
5292 got_ref_close(head_ref2);
5294 return err;
5297 static const struct got_error *
5298 check_path_is_commitable(const char *path,
5299 struct got_pathlist_head *commitable_paths)
5301 struct got_pathlist_entry *cpe = NULL;
5302 size_t path_len = strlen(path);
5304 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5305 struct got_commitable *ct = cpe->data;
5306 const char *ct_path = ct->path;
5308 while (ct_path[0] == '/')
5309 ct_path++;
5311 if (strcmp(path, ct_path) == 0 ||
5312 got_path_is_child(ct_path, path, path_len))
5313 break;
5316 if (cpe == NULL)
5317 return got_error_path(path, GOT_ERR_BAD_PATH);
5319 return NULL;
5322 static const struct got_error *
5323 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5325 int *have_staged_files = arg;
5327 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5328 *have_staged_files = 1;
5329 return got_error(GOT_ERR_CANCELLED);
5332 return NULL;
5335 static const struct got_error *
5336 check_non_staged_files(struct got_fileindex *fileindex,
5337 struct got_pathlist_head *paths)
5339 struct got_pathlist_entry *pe;
5340 struct got_fileindex_entry *ie;
5342 TAILQ_FOREACH(pe, paths, entry) {
5343 if (pe->path[0] == '\0')
5344 continue;
5345 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5346 if (ie == NULL)
5347 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5348 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5349 return got_error_path(pe->path,
5350 GOT_ERR_FILE_NOT_STAGED);
5353 return NULL;
5356 const struct got_error *
5357 got_worktree_commit(struct got_object_id **new_commit_id,
5358 struct got_worktree *worktree, struct got_pathlist_head *paths,
5359 const char *author, const char *committer,
5360 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5361 got_worktree_status_cb status_cb, void *status_arg,
5362 struct got_repository *repo)
5364 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5365 struct got_fileindex *fileindex = NULL;
5366 char *fileindex_path = NULL;
5367 struct got_pathlist_head commitable_paths;
5368 struct collect_commitables_arg cc_arg;
5369 struct got_pathlist_entry *pe;
5370 struct got_reference *head_ref = NULL;
5371 struct got_object_id *head_commit_id = NULL;
5372 int have_staged_files = 0;
5374 *new_commit_id = NULL;
5376 TAILQ_INIT(&commitable_paths);
5378 err = lock_worktree(worktree, LOCK_EX);
5379 if (err)
5380 goto done;
5382 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5383 if (err)
5384 goto done;
5386 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5387 if (err)
5388 goto done;
5390 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5391 if (err)
5392 goto done;
5394 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5395 &have_staged_files);
5396 if (err && err->code != GOT_ERR_CANCELLED)
5397 goto done;
5398 if (have_staged_files) {
5399 err = check_non_staged_files(fileindex, paths);
5400 if (err)
5401 goto done;
5404 cc_arg.commitable_paths = &commitable_paths;
5405 cc_arg.worktree = worktree;
5406 cc_arg.repo = repo;
5407 cc_arg.have_staged_files = have_staged_files;
5408 TAILQ_FOREACH(pe, paths, entry) {
5409 err = worktree_status(worktree, pe->path, fileindex, repo,
5410 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5411 if (err)
5412 goto done;
5415 if (TAILQ_EMPTY(&commitable_paths)) {
5416 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5417 goto done;
5420 TAILQ_FOREACH(pe, paths, entry) {
5421 err = check_path_is_commitable(pe->path, &commitable_paths);
5422 if (err)
5423 goto done;
5426 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5427 struct got_commitable *ct = pe->data;
5428 const char *ct_path = ct->in_repo_path;
5430 while (ct_path[0] == '/')
5431 ct_path++;
5432 err = check_out_of_date(ct_path, ct->status,
5433 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5434 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5435 if (err)
5436 goto done;
5440 err = commit_worktree(new_commit_id, &commitable_paths,
5441 head_commit_id, worktree, author, committer,
5442 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5443 if (err)
5444 goto done;
5446 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5447 fileindex, have_staged_files);
5448 if (err == NULL) {
5449 err = reinstall_symlinks_after_commit(&commitable_paths,
5450 *new_commit_id, fileindex, worktree, repo);
5452 sync_err = sync_fileindex(fileindex, fileindex_path);
5453 if (sync_err && err == NULL)
5454 err = sync_err;
5455 done:
5456 if (fileindex)
5457 got_fileindex_free(fileindex);
5458 free(fileindex_path);
5459 unlockerr = lock_worktree(worktree, LOCK_SH);
5460 if (unlockerr && err == NULL)
5461 err = unlockerr;
5462 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5463 struct got_commitable *ct = pe->data;
5464 free_commitable(ct);
5466 got_pathlist_free(&commitable_paths);
5467 return err;
5470 const char *
5471 got_commitable_get_path(struct got_commitable *ct)
5473 return ct->path;
5476 unsigned int
5477 got_commitable_get_status(struct got_commitable *ct)
5479 return ct->status;
5482 struct check_rebase_ok_arg {
5483 struct got_worktree *worktree;
5484 struct got_repository *repo;
5487 static const struct got_error *
5488 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5490 const struct got_error *err = NULL;
5491 struct check_rebase_ok_arg *a = arg;
5492 unsigned char status;
5493 struct stat sb;
5494 char *ondisk_path;
5496 /* Reject rebase of a work tree with mixed base commits. */
5497 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5498 SHA1_DIGEST_LENGTH))
5499 return got_error(GOT_ERR_MIXED_COMMITS);
5501 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5502 == -1)
5503 return got_error_from_errno("asprintf");
5505 /* Reject rebase of a work tree with modified or staged files. */
5506 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5507 free(ondisk_path);
5508 if (err)
5509 return err;
5511 if (status != GOT_STATUS_NO_CHANGE)
5512 return got_error(GOT_ERR_MODIFIED);
5513 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5514 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5516 return NULL;
5519 const struct got_error *
5520 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5521 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5522 struct got_worktree *worktree, struct got_reference *branch,
5523 struct got_repository *repo)
5525 const struct got_error *err = NULL;
5526 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5527 char *branch_ref_name = NULL;
5528 char *fileindex_path = NULL;
5529 struct check_rebase_ok_arg ok_arg;
5530 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5531 struct got_object_id *wt_branch_tip = NULL;
5533 *new_base_branch_ref = NULL;
5534 *tmp_branch = NULL;
5535 *fileindex = NULL;
5537 err = lock_worktree(worktree, LOCK_EX);
5538 if (err)
5539 return err;
5541 err = open_fileindex(fileindex, &fileindex_path, worktree);
5542 if (err)
5543 goto done;
5545 ok_arg.worktree = worktree;
5546 ok_arg.repo = repo;
5547 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5548 &ok_arg);
5549 if (err)
5550 goto done;
5552 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5553 if (err)
5554 goto done;
5556 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5557 if (err)
5558 goto done;
5560 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5561 if (err)
5562 goto done;
5564 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5565 0);
5566 if (err)
5567 goto done;
5569 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5570 if (err)
5571 goto done;
5572 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5573 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5574 goto done;
5577 err = got_ref_alloc_symref(new_base_branch_ref,
5578 new_base_branch_ref_name, wt_branch);
5579 if (err)
5580 goto done;
5581 err = got_ref_write(*new_base_branch_ref, repo);
5582 if (err)
5583 goto done;
5585 /* TODO Lock original branch's ref while rebasing? */
5587 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5588 if (err)
5589 goto done;
5591 err = got_ref_write(branch_ref, repo);
5592 if (err)
5593 goto done;
5595 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5596 worktree->base_commit_id);
5597 if (err)
5598 goto done;
5599 err = got_ref_write(*tmp_branch, repo);
5600 if (err)
5601 goto done;
5603 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5604 if (err)
5605 goto done;
5606 done:
5607 free(fileindex_path);
5608 free(tmp_branch_name);
5609 free(new_base_branch_ref_name);
5610 free(branch_ref_name);
5611 if (branch_ref)
5612 got_ref_close(branch_ref);
5613 if (wt_branch)
5614 got_ref_close(wt_branch);
5615 free(wt_branch_tip);
5616 if (err) {
5617 if (*new_base_branch_ref) {
5618 got_ref_close(*new_base_branch_ref);
5619 *new_base_branch_ref = NULL;
5621 if (*tmp_branch) {
5622 got_ref_close(*tmp_branch);
5623 *tmp_branch = NULL;
5625 if (*fileindex) {
5626 got_fileindex_free(*fileindex);
5627 *fileindex = NULL;
5629 lock_worktree(worktree, LOCK_SH);
5631 return err;
5634 const struct got_error *
5635 got_worktree_rebase_continue(struct got_object_id **commit_id,
5636 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5637 struct got_reference **branch, struct got_fileindex **fileindex,
5638 struct got_worktree *worktree, struct got_repository *repo)
5640 const struct got_error *err;
5641 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5642 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5643 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5644 char *fileindex_path = NULL;
5645 int have_staged_files = 0;
5647 *commit_id = NULL;
5648 *new_base_branch = NULL;
5649 *tmp_branch = NULL;
5650 *branch = NULL;
5651 *fileindex = NULL;
5653 err = lock_worktree(worktree, LOCK_EX);
5654 if (err)
5655 return err;
5657 err = open_fileindex(fileindex, &fileindex_path, worktree);
5658 if (err)
5659 goto done;
5661 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5662 &have_staged_files);
5663 if (err && err->code != GOT_ERR_CANCELLED)
5664 goto done;
5665 if (have_staged_files) {
5666 err = got_error(GOT_ERR_STAGED_PATHS);
5667 goto done;
5670 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5671 if (err)
5672 goto done;
5674 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5675 if (err)
5676 goto done;
5678 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5679 if (err)
5680 goto done;
5682 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5683 if (err)
5684 goto done;
5686 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5687 if (err)
5688 goto done;
5690 err = got_ref_open(branch, repo,
5691 got_ref_get_symref_target(branch_ref), 0);
5692 if (err)
5693 goto done;
5695 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5696 if (err)
5697 goto done;
5699 err = got_ref_resolve(commit_id, repo, commit_ref);
5700 if (err)
5701 goto done;
5703 err = got_ref_open(new_base_branch, repo,
5704 new_base_branch_ref_name, 0);
5705 if (err)
5706 goto done;
5708 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5709 if (err)
5710 goto done;
5711 done:
5712 free(commit_ref_name);
5713 free(branch_ref_name);
5714 free(fileindex_path);
5715 if (commit_ref)
5716 got_ref_close(commit_ref);
5717 if (branch_ref)
5718 got_ref_close(branch_ref);
5719 if (err) {
5720 free(*commit_id);
5721 *commit_id = NULL;
5722 if (*tmp_branch) {
5723 got_ref_close(*tmp_branch);
5724 *tmp_branch = NULL;
5726 if (*new_base_branch) {
5727 got_ref_close(*new_base_branch);
5728 *new_base_branch = NULL;
5730 if (*branch) {
5731 got_ref_close(*branch);
5732 *branch = NULL;
5734 if (*fileindex) {
5735 got_fileindex_free(*fileindex);
5736 *fileindex = NULL;
5738 lock_worktree(worktree, LOCK_SH);
5740 return err;
5743 const struct got_error *
5744 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5746 const struct got_error *err;
5747 char *tmp_branch_name = NULL;
5749 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5750 if (err)
5751 return err;
5753 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5754 free(tmp_branch_name);
5755 return NULL;
5758 static const struct got_error *
5759 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5760 char **logmsg, void *arg)
5762 *logmsg = arg;
5763 return NULL;
5766 static const struct got_error *
5767 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5768 const char *path, struct got_object_id *blob_id,
5769 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5770 int dirfd, const char *de_name)
5772 return NULL;
5775 struct collect_merged_paths_arg {
5776 got_worktree_checkout_cb progress_cb;
5777 void *progress_arg;
5778 struct got_pathlist_head *merged_paths;
5781 static const struct got_error *
5782 collect_merged_paths(void *arg, unsigned char status, const char *path)
5784 const struct got_error *err;
5785 struct collect_merged_paths_arg *a = arg;
5786 char *p;
5787 struct got_pathlist_entry *new;
5789 err = (*a->progress_cb)(a->progress_arg, status, path);
5790 if (err)
5791 return err;
5793 if (status != GOT_STATUS_MERGE &&
5794 status != GOT_STATUS_ADD &&
5795 status != GOT_STATUS_DELETE &&
5796 status != GOT_STATUS_CONFLICT)
5797 return NULL;
5799 p = strdup(path);
5800 if (p == NULL)
5801 return got_error_from_errno("strdup");
5803 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5804 if (err || new == NULL)
5805 free(p);
5806 return err;
5809 void
5810 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5812 struct got_pathlist_entry *pe;
5814 TAILQ_FOREACH(pe, merged_paths, entry)
5815 free((char *)pe->path);
5817 got_pathlist_free(merged_paths);
5820 static const struct got_error *
5821 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5822 int is_rebase, struct got_repository *repo)
5824 const struct got_error *err;
5825 struct got_reference *commit_ref = NULL;
5827 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5828 if (err) {
5829 if (err->code != GOT_ERR_NOT_REF)
5830 goto done;
5831 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5832 if (err)
5833 goto done;
5834 err = got_ref_write(commit_ref, repo);
5835 if (err)
5836 goto done;
5837 } else if (is_rebase) {
5838 struct got_object_id *stored_id;
5839 int cmp;
5841 err = got_ref_resolve(&stored_id, repo, commit_ref);
5842 if (err)
5843 goto done;
5844 cmp = got_object_id_cmp(commit_id, stored_id);
5845 free(stored_id);
5846 if (cmp != 0) {
5847 err = got_error(GOT_ERR_REBASE_COMMITID);
5848 goto done;
5851 done:
5852 if (commit_ref)
5853 got_ref_close(commit_ref);
5854 return err;
5857 static const struct got_error *
5858 rebase_merge_files(struct got_pathlist_head *merged_paths,
5859 const char *commit_ref_name, struct got_worktree *worktree,
5860 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5861 struct got_object_id *commit_id, struct got_repository *repo,
5862 got_worktree_checkout_cb progress_cb, void *progress_arg,
5863 got_cancel_cb cancel_cb, void *cancel_arg)
5865 const struct got_error *err;
5866 struct got_reference *commit_ref = NULL;
5867 struct collect_merged_paths_arg cmp_arg;
5868 char *fileindex_path;
5870 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5872 err = get_fileindex_path(&fileindex_path, worktree);
5873 if (err)
5874 return err;
5876 cmp_arg.progress_cb = progress_cb;
5877 cmp_arg.progress_arg = progress_arg;
5878 cmp_arg.merged_paths = merged_paths;
5879 err = merge_files(worktree, fileindex, fileindex_path,
5880 parent_commit_id, commit_id, repo, collect_merged_paths,
5881 &cmp_arg, cancel_cb, cancel_arg);
5882 if (commit_ref)
5883 got_ref_close(commit_ref);
5884 return err;
5887 const struct got_error *
5888 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5889 struct got_worktree *worktree, struct got_fileindex *fileindex,
5890 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5891 struct got_repository *repo,
5892 got_worktree_checkout_cb progress_cb, void *progress_arg,
5893 got_cancel_cb cancel_cb, void *cancel_arg)
5895 const struct got_error *err;
5896 char *commit_ref_name;
5898 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5899 if (err)
5900 return err;
5902 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5903 if (err)
5904 goto done;
5906 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5907 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5908 progress_arg, cancel_cb, cancel_arg);
5909 done:
5910 free(commit_ref_name);
5911 return err;
5914 const struct got_error *
5915 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5916 struct got_worktree *worktree, struct got_fileindex *fileindex,
5917 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5918 struct got_repository *repo,
5919 got_worktree_checkout_cb progress_cb, void *progress_arg,
5920 got_cancel_cb cancel_cb, void *cancel_arg)
5922 const struct got_error *err;
5923 char *commit_ref_name;
5925 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5926 if (err)
5927 return err;
5929 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5930 if (err)
5931 goto done;
5933 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5934 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5935 progress_arg, cancel_cb, cancel_arg);
5936 done:
5937 free(commit_ref_name);
5938 return err;
5941 static const struct got_error *
5942 rebase_commit(struct got_object_id **new_commit_id,
5943 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5944 struct got_worktree *worktree, struct got_fileindex *fileindex,
5945 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5946 const char *new_logmsg, struct got_repository *repo)
5948 const struct got_error *err, *sync_err;
5949 struct got_pathlist_head commitable_paths;
5950 struct collect_commitables_arg cc_arg;
5951 char *fileindex_path = NULL;
5952 struct got_reference *head_ref = NULL;
5953 struct got_object_id *head_commit_id = NULL;
5954 char *logmsg = NULL;
5956 TAILQ_INIT(&commitable_paths);
5957 *new_commit_id = NULL;
5959 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5961 err = get_fileindex_path(&fileindex_path, worktree);
5962 if (err)
5963 return err;
5965 cc_arg.commitable_paths = &commitable_paths;
5966 cc_arg.worktree = worktree;
5967 cc_arg.repo = repo;
5968 cc_arg.have_staged_files = 0;
5970 * If possible get the status of individual files directly to
5971 * avoid crawling the entire work tree once per rebased commit.
5972 * TODO: Ideally, merged_paths would contain a list of commitables
5973 * we could use so we could skip worktree_status() entirely.
5975 if (merged_paths) {
5976 struct got_pathlist_entry *pe;
5977 TAILQ_FOREACH(pe, merged_paths, entry) {
5978 err = worktree_status(worktree, pe->path, fileindex,
5979 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5980 0);
5981 if (err)
5982 goto done;
5984 } else {
5985 err = worktree_status(worktree, "", fileindex, repo,
5986 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5987 if (err)
5988 goto done;
5991 if (TAILQ_EMPTY(&commitable_paths)) {
5992 /* No-op change; commit will be elided. */
5993 err = got_ref_delete(commit_ref, repo);
5994 if (err)
5995 goto done;
5996 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5997 goto done;
6000 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6001 if (err)
6002 goto done;
6004 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6005 if (err)
6006 goto done;
6008 if (new_logmsg) {
6009 logmsg = strdup(new_logmsg);
6010 if (logmsg == NULL) {
6011 err = got_error_from_errno("strdup");
6012 goto done;
6014 } else {
6015 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6016 if (err)
6017 goto done;
6020 /* NB: commit_worktree will call free(logmsg) */
6021 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6022 worktree, got_object_commit_get_author(orig_commit),
6023 got_object_commit_get_committer(orig_commit),
6024 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6025 if (err)
6026 goto done;
6028 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6029 if (err)
6030 goto done;
6032 err = got_ref_delete(commit_ref, repo);
6033 if (err)
6034 goto done;
6036 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6037 fileindex, 0);
6038 sync_err = sync_fileindex(fileindex, fileindex_path);
6039 if (sync_err && err == NULL)
6040 err = sync_err;
6041 done:
6042 free(fileindex_path);
6043 free(head_commit_id);
6044 if (head_ref)
6045 got_ref_close(head_ref);
6046 if (err) {
6047 free(*new_commit_id);
6048 *new_commit_id = NULL;
6050 return err;
6053 const struct got_error *
6054 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6055 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6056 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6057 struct got_commit_object *orig_commit,
6058 struct got_object_id *orig_commit_id, struct got_repository *repo)
6060 const struct got_error *err;
6061 char *commit_ref_name;
6062 struct got_reference *commit_ref = NULL;
6063 struct got_object_id *commit_id = NULL;
6065 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6066 if (err)
6067 return err;
6069 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6070 if (err)
6071 goto done;
6072 err = got_ref_resolve(&commit_id, repo, commit_ref);
6073 if (err)
6074 goto done;
6075 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6076 err = got_error(GOT_ERR_REBASE_COMMITID);
6077 goto done;
6080 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6081 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6082 done:
6083 if (commit_ref)
6084 got_ref_close(commit_ref);
6085 free(commit_ref_name);
6086 free(commit_id);
6087 return err;
6090 const struct got_error *
6091 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6092 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6093 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6094 struct got_commit_object *orig_commit,
6095 struct got_object_id *orig_commit_id, const char *new_logmsg,
6096 struct got_repository *repo)
6098 const struct got_error *err;
6099 char *commit_ref_name;
6100 struct got_reference *commit_ref = NULL;
6102 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6103 if (err)
6104 return err;
6106 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6107 if (err)
6108 goto done;
6110 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6111 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6112 done:
6113 if (commit_ref)
6114 got_ref_close(commit_ref);
6115 free(commit_ref_name);
6116 return err;
6119 const struct got_error *
6120 got_worktree_rebase_postpone(struct got_worktree *worktree,
6121 struct got_fileindex *fileindex)
6123 if (fileindex)
6124 got_fileindex_free(fileindex);
6125 return lock_worktree(worktree, LOCK_SH);
6128 static const struct got_error *
6129 delete_ref(const char *name, struct got_repository *repo)
6131 const struct got_error *err;
6132 struct got_reference *ref;
6134 err = got_ref_open(&ref, repo, name, 0);
6135 if (err) {
6136 if (err->code == GOT_ERR_NOT_REF)
6137 return NULL;
6138 return err;
6141 err = got_ref_delete(ref, repo);
6142 got_ref_close(ref);
6143 return err;
6146 static const struct got_error *
6147 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6149 const struct got_error *err;
6150 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6151 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6153 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6154 if (err)
6155 goto done;
6156 err = delete_ref(tmp_branch_name, repo);
6157 if (err)
6158 goto done;
6160 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6161 if (err)
6162 goto done;
6163 err = delete_ref(new_base_branch_ref_name, repo);
6164 if (err)
6165 goto done;
6167 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6168 if (err)
6169 goto done;
6170 err = delete_ref(branch_ref_name, repo);
6171 if (err)
6172 goto done;
6174 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6175 if (err)
6176 goto done;
6177 err = delete_ref(commit_ref_name, repo);
6178 if (err)
6179 goto done;
6181 done:
6182 free(tmp_branch_name);
6183 free(new_base_branch_ref_name);
6184 free(branch_ref_name);
6185 free(commit_ref_name);
6186 return err;
6189 const struct got_error *
6190 got_worktree_rebase_complete(struct got_worktree *worktree,
6191 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6192 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6193 struct got_repository *repo)
6195 const struct got_error *err, *unlockerr;
6196 struct got_object_id *new_head_commit_id = NULL;
6198 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6199 if (err)
6200 return err;
6202 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6203 if (err)
6204 goto done;
6206 err = got_ref_write(rebased_branch, repo);
6207 if (err)
6208 goto done;
6210 err = got_worktree_set_head_ref(worktree, rebased_branch);
6211 if (err)
6212 goto done;
6214 err = delete_rebase_refs(worktree, repo);
6215 done:
6216 if (fileindex)
6217 got_fileindex_free(fileindex);
6218 free(new_head_commit_id);
6219 unlockerr = lock_worktree(worktree, LOCK_SH);
6220 if (unlockerr && err == NULL)
6221 err = unlockerr;
6222 return err;
6225 const struct got_error *
6226 got_worktree_rebase_abort(struct got_worktree *worktree,
6227 struct got_fileindex *fileindex, struct got_repository *repo,
6228 struct got_reference *new_base_branch,
6229 got_worktree_checkout_cb progress_cb, void *progress_arg)
6231 const struct got_error *err, *unlockerr, *sync_err;
6232 struct got_reference *resolved = NULL;
6233 struct got_object_id *commit_id = NULL;
6234 char *fileindex_path = NULL;
6235 struct revert_file_args rfa;
6236 struct got_object_id *tree_id = NULL;
6238 err = lock_worktree(worktree, LOCK_EX);
6239 if (err)
6240 return err;
6242 err = got_ref_open(&resolved, repo,
6243 got_ref_get_symref_target(new_base_branch), 0);
6244 if (err)
6245 goto done;
6247 err = got_worktree_set_head_ref(worktree, resolved);
6248 if (err)
6249 goto done;
6252 * XXX commits to the base branch could have happened while
6253 * we were busy rebasing; should we store the original commit ID
6254 * when rebase begins and read it back here?
6256 err = got_ref_resolve(&commit_id, repo, resolved);
6257 if (err)
6258 goto done;
6260 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6261 if (err)
6262 goto done;
6264 err = got_object_id_by_path(&tree_id, repo,
6265 worktree->base_commit_id, worktree->path_prefix);
6266 if (err)
6267 goto done;
6269 err = delete_rebase_refs(worktree, repo);
6270 if (err)
6271 goto done;
6273 err = get_fileindex_path(&fileindex_path, worktree);
6274 if (err)
6275 goto done;
6277 rfa.worktree = worktree;
6278 rfa.fileindex = fileindex;
6279 rfa.progress_cb = progress_cb;
6280 rfa.progress_arg = progress_arg;
6281 rfa.patch_cb = NULL;
6282 rfa.patch_arg = NULL;
6283 rfa.repo = repo;
6284 err = worktree_status(worktree, "", fileindex, repo,
6285 revert_file, &rfa, NULL, NULL, 0, 0);
6286 if (err)
6287 goto sync;
6289 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6290 repo, progress_cb, progress_arg, NULL, NULL);
6291 sync:
6292 sync_err = sync_fileindex(fileindex, fileindex_path);
6293 if (sync_err && err == NULL)
6294 err = sync_err;
6295 done:
6296 got_ref_close(resolved);
6297 free(tree_id);
6298 free(commit_id);
6299 if (fileindex)
6300 got_fileindex_free(fileindex);
6301 free(fileindex_path);
6303 unlockerr = lock_worktree(worktree, LOCK_SH);
6304 if (unlockerr && err == NULL)
6305 err = unlockerr;
6306 return err;
6309 const struct got_error *
6310 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6311 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6312 struct got_fileindex **fileindex, struct got_worktree *worktree,
6313 struct got_repository *repo)
6315 const struct got_error *err = NULL;
6316 char *tmp_branch_name = NULL;
6317 char *branch_ref_name = NULL;
6318 char *base_commit_ref_name = NULL;
6319 char *fileindex_path = NULL;
6320 struct check_rebase_ok_arg ok_arg;
6321 struct got_reference *wt_branch = NULL;
6322 struct got_reference *base_commit_ref = NULL;
6324 *tmp_branch = NULL;
6325 *branch_ref = NULL;
6326 *base_commit_id = NULL;
6327 *fileindex = NULL;
6329 err = lock_worktree(worktree, LOCK_EX);
6330 if (err)
6331 return err;
6333 err = open_fileindex(fileindex, &fileindex_path, worktree);
6334 if (err)
6335 goto done;
6337 ok_arg.worktree = worktree;
6338 ok_arg.repo = repo;
6339 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6340 &ok_arg);
6341 if (err)
6342 goto done;
6344 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6345 if (err)
6346 goto done;
6348 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6349 if (err)
6350 goto done;
6352 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6353 worktree);
6354 if (err)
6355 goto done;
6357 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6358 0);
6359 if (err)
6360 goto done;
6362 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6363 if (err)
6364 goto done;
6366 err = got_ref_write(*branch_ref, repo);
6367 if (err)
6368 goto done;
6370 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6371 worktree->base_commit_id);
6372 if (err)
6373 goto done;
6374 err = got_ref_write(base_commit_ref, repo);
6375 if (err)
6376 goto done;
6377 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6378 if (*base_commit_id == NULL) {
6379 err = got_error_from_errno("got_object_id_dup");
6380 goto done;
6383 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6384 worktree->base_commit_id);
6385 if (err)
6386 goto done;
6387 err = got_ref_write(*tmp_branch, repo);
6388 if (err)
6389 goto done;
6391 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6392 if (err)
6393 goto done;
6394 done:
6395 free(fileindex_path);
6396 free(tmp_branch_name);
6397 free(branch_ref_name);
6398 free(base_commit_ref_name);
6399 if (wt_branch)
6400 got_ref_close(wt_branch);
6401 if (err) {
6402 if (*branch_ref) {
6403 got_ref_close(*branch_ref);
6404 *branch_ref = NULL;
6406 if (*tmp_branch) {
6407 got_ref_close(*tmp_branch);
6408 *tmp_branch = NULL;
6410 free(*base_commit_id);
6411 if (*fileindex) {
6412 got_fileindex_free(*fileindex);
6413 *fileindex = NULL;
6415 lock_worktree(worktree, LOCK_SH);
6417 return err;
6420 const struct got_error *
6421 got_worktree_histedit_postpone(struct got_worktree *worktree,
6422 struct got_fileindex *fileindex)
6424 if (fileindex)
6425 got_fileindex_free(fileindex);
6426 return lock_worktree(worktree, LOCK_SH);
6429 const struct got_error *
6430 got_worktree_histedit_in_progress(int *in_progress,
6431 struct got_worktree *worktree)
6433 const struct got_error *err;
6434 char *tmp_branch_name = NULL;
6436 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6437 if (err)
6438 return err;
6440 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6441 free(tmp_branch_name);
6442 return NULL;
6445 const struct got_error *
6446 got_worktree_histedit_continue(struct got_object_id **commit_id,
6447 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6448 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6449 struct got_worktree *worktree, struct got_repository *repo)
6451 const struct got_error *err;
6452 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6453 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6454 struct got_reference *commit_ref = NULL;
6455 struct got_reference *base_commit_ref = NULL;
6456 char *fileindex_path = NULL;
6457 int have_staged_files = 0;
6459 *commit_id = NULL;
6460 *tmp_branch = NULL;
6461 *base_commit_id = NULL;
6462 *fileindex = NULL;
6464 err = lock_worktree(worktree, LOCK_EX);
6465 if (err)
6466 return err;
6468 err = open_fileindex(fileindex, &fileindex_path, worktree);
6469 if (err)
6470 goto done;
6472 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6473 &have_staged_files);
6474 if (err && err->code != GOT_ERR_CANCELLED)
6475 goto done;
6476 if (have_staged_files) {
6477 err = got_error(GOT_ERR_STAGED_PATHS);
6478 goto done;
6481 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6482 if (err)
6483 goto done;
6485 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6486 if (err)
6487 goto done;
6489 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6490 if (err)
6491 goto done;
6493 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6494 worktree);
6495 if (err)
6496 goto done;
6498 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6499 if (err)
6500 goto done;
6502 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6503 if (err)
6504 goto done;
6505 err = got_ref_resolve(commit_id, repo, commit_ref);
6506 if (err)
6507 goto done;
6509 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6510 if (err)
6511 goto done;
6512 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6513 if (err)
6514 goto done;
6516 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6517 if (err)
6518 goto done;
6519 done:
6520 free(commit_ref_name);
6521 free(branch_ref_name);
6522 free(fileindex_path);
6523 if (commit_ref)
6524 got_ref_close(commit_ref);
6525 if (base_commit_ref)
6526 got_ref_close(base_commit_ref);
6527 if (err) {
6528 free(*commit_id);
6529 *commit_id = NULL;
6530 free(*base_commit_id);
6531 *base_commit_id = NULL;
6532 if (*tmp_branch) {
6533 got_ref_close(*tmp_branch);
6534 *tmp_branch = NULL;
6536 if (*fileindex) {
6537 got_fileindex_free(*fileindex);
6538 *fileindex = NULL;
6540 lock_worktree(worktree, LOCK_EX);
6542 return err;
6545 static const struct got_error *
6546 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6548 const struct got_error *err;
6549 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6550 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6552 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6553 if (err)
6554 goto done;
6555 err = delete_ref(tmp_branch_name, repo);
6556 if (err)
6557 goto done;
6559 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6560 worktree);
6561 if (err)
6562 goto done;
6563 err = delete_ref(base_commit_ref_name, repo);
6564 if (err)
6565 goto done;
6567 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6568 if (err)
6569 goto done;
6570 err = delete_ref(branch_ref_name, repo);
6571 if (err)
6572 goto done;
6574 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6575 if (err)
6576 goto done;
6577 err = delete_ref(commit_ref_name, repo);
6578 if (err)
6579 goto done;
6580 done:
6581 free(tmp_branch_name);
6582 free(base_commit_ref_name);
6583 free(branch_ref_name);
6584 free(commit_ref_name);
6585 return err;
6588 const struct got_error *
6589 got_worktree_histedit_abort(struct got_worktree *worktree,
6590 struct got_fileindex *fileindex, struct got_repository *repo,
6591 struct got_reference *branch, struct got_object_id *base_commit_id,
6592 got_worktree_checkout_cb progress_cb, void *progress_arg)
6594 const struct got_error *err, *unlockerr, *sync_err;
6595 struct got_reference *resolved = NULL;
6596 char *fileindex_path = NULL;
6597 struct got_object_id *tree_id = NULL;
6598 struct revert_file_args rfa;
6600 err = lock_worktree(worktree, LOCK_EX);
6601 if (err)
6602 return err;
6604 err = got_ref_open(&resolved, repo,
6605 got_ref_get_symref_target(branch), 0);
6606 if (err)
6607 goto done;
6609 err = got_worktree_set_head_ref(worktree, resolved);
6610 if (err)
6611 goto done;
6613 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6614 if (err)
6615 goto done;
6617 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6618 worktree->path_prefix);
6619 if (err)
6620 goto done;
6622 err = delete_histedit_refs(worktree, repo);
6623 if (err)
6624 goto done;
6626 err = get_fileindex_path(&fileindex_path, worktree);
6627 if (err)
6628 goto done;
6630 rfa.worktree = worktree;
6631 rfa.fileindex = fileindex;
6632 rfa.progress_cb = progress_cb;
6633 rfa.progress_arg = progress_arg;
6634 rfa.patch_cb = NULL;
6635 rfa.patch_arg = NULL;
6636 rfa.repo = repo;
6637 err = worktree_status(worktree, "", fileindex, repo,
6638 revert_file, &rfa, NULL, NULL, 0, 0);
6639 if (err)
6640 goto sync;
6642 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6643 repo, progress_cb, progress_arg, NULL, NULL);
6644 sync:
6645 sync_err = sync_fileindex(fileindex, fileindex_path);
6646 if (sync_err && err == NULL)
6647 err = sync_err;
6648 done:
6649 got_ref_close(resolved);
6650 free(tree_id);
6651 free(fileindex_path);
6653 unlockerr = lock_worktree(worktree, LOCK_SH);
6654 if (unlockerr && err == NULL)
6655 err = unlockerr;
6656 return err;
6659 const struct got_error *
6660 got_worktree_histedit_complete(struct got_worktree *worktree,
6661 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6662 struct got_reference *edited_branch, struct got_repository *repo)
6664 const struct got_error *err, *unlockerr;
6665 struct got_object_id *new_head_commit_id = NULL;
6666 struct got_reference *resolved = NULL;
6668 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6669 if (err)
6670 return err;
6672 err = got_ref_open(&resolved, repo,
6673 got_ref_get_symref_target(edited_branch), 0);
6674 if (err)
6675 goto done;
6677 err = got_ref_change_ref(resolved, new_head_commit_id);
6678 if (err)
6679 goto done;
6681 err = got_ref_write(resolved, repo);
6682 if (err)
6683 goto done;
6685 err = got_worktree_set_head_ref(worktree, resolved);
6686 if (err)
6687 goto done;
6689 err = delete_histedit_refs(worktree, repo);
6690 done:
6691 if (fileindex)
6692 got_fileindex_free(fileindex);
6693 free(new_head_commit_id);
6694 unlockerr = lock_worktree(worktree, LOCK_SH);
6695 if (unlockerr && err == NULL)
6696 err = unlockerr;
6697 return err;
6700 const struct got_error *
6701 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6702 struct got_object_id *commit_id, struct got_repository *repo)
6704 const struct got_error *err;
6705 char *commit_ref_name;
6707 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6708 if (err)
6709 return err;
6711 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6712 if (err)
6713 goto done;
6715 err = delete_ref(commit_ref_name, repo);
6716 done:
6717 free(commit_ref_name);
6718 return err;
6721 const struct got_error *
6722 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6723 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6724 struct got_worktree *worktree, const char *refname,
6725 struct got_repository *repo)
6727 const struct got_error *err = NULL;
6728 char *fileindex_path = NULL;
6729 struct check_rebase_ok_arg ok_arg;
6731 *fileindex = NULL;
6732 *branch_ref = NULL;
6733 *base_branch_ref = NULL;
6735 err = lock_worktree(worktree, LOCK_EX);
6736 if (err)
6737 return err;
6739 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6740 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6741 "cannot integrate a branch into itself; "
6742 "update -b or different branch name required");
6743 goto done;
6746 err = open_fileindex(fileindex, &fileindex_path, worktree);
6747 if (err)
6748 goto done;
6750 /* Preconditions are the same as for rebase. */
6751 ok_arg.worktree = worktree;
6752 ok_arg.repo = repo;
6753 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6754 &ok_arg);
6755 if (err)
6756 goto done;
6758 err = got_ref_open(branch_ref, repo, refname, 1);
6759 if (err)
6760 goto done;
6762 err = got_ref_open(base_branch_ref, repo,
6763 got_worktree_get_head_ref_name(worktree), 1);
6764 done:
6765 if (err) {
6766 if (*branch_ref) {
6767 got_ref_close(*branch_ref);
6768 *branch_ref = NULL;
6770 if (*base_branch_ref) {
6771 got_ref_close(*base_branch_ref);
6772 *base_branch_ref = NULL;
6774 if (*fileindex) {
6775 got_fileindex_free(*fileindex);
6776 *fileindex = NULL;
6778 lock_worktree(worktree, LOCK_SH);
6780 return err;
6783 const struct got_error *
6784 got_worktree_integrate_continue(struct got_worktree *worktree,
6785 struct got_fileindex *fileindex, struct got_repository *repo,
6786 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6787 got_worktree_checkout_cb progress_cb, void *progress_arg,
6788 got_cancel_cb cancel_cb, void *cancel_arg)
6790 const struct got_error *err = NULL, *sync_err, *unlockerr;
6791 char *fileindex_path = NULL;
6792 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6794 err = get_fileindex_path(&fileindex_path, worktree);
6795 if (err)
6796 goto done;
6798 err = got_ref_resolve(&commit_id, repo, branch_ref);
6799 if (err)
6800 goto done;
6802 err = got_object_id_by_path(&tree_id, repo, commit_id,
6803 worktree->path_prefix);
6804 if (err)
6805 goto done;
6807 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6808 if (err)
6809 goto done;
6811 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6812 progress_cb, progress_arg, cancel_cb, cancel_arg);
6813 if (err)
6814 goto sync;
6816 err = got_ref_change_ref(base_branch_ref, commit_id);
6817 if (err)
6818 goto sync;
6820 err = got_ref_write(base_branch_ref, repo);
6821 sync:
6822 sync_err = sync_fileindex(fileindex, fileindex_path);
6823 if (sync_err && err == NULL)
6824 err = sync_err;
6826 done:
6827 unlockerr = got_ref_unlock(branch_ref);
6828 if (unlockerr && err == NULL)
6829 err = unlockerr;
6830 got_ref_close(branch_ref);
6832 unlockerr = got_ref_unlock(base_branch_ref);
6833 if (unlockerr && err == NULL)
6834 err = unlockerr;
6835 got_ref_close(base_branch_ref);
6837 got_fileindex_free(fileindex);
6838 free(fileindex_path);
6839 free(tree_id);
6841 unlockerr = lock_worktree(worktree, LOCK_SH);
6842 if (unlockerr && err == NULL)
6843 err = unlockerr;
6844 return err;
6847 const struct got_error *
6848 got_worktree_integrate_abort(struct got_worktree *worktree,
6849 struct got_fileindex *fileindex, struct got_repository *repo,
6850 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6852 const struct got_error *err = NULL, *unlockerr = NULL;
6854 got_fileindex_free(fileindex);
6856 err = lock_worktree(worktree, LOCK_SH);
6858 unlockerr = got_ref_unlock(branch_ref);
6859 if (unlockerr && err == NULL)
6860 err = unlockerr;
6861 got_ref_close(branch_ref);
6863 unlockerr = got_ref_unlock(base_branch_ref);
6864 if (unlockerr && err == NULL)
6865 err = unlockerr;
6866 got_ref_close(base_branch_ref);
6868 return err;
6871 struct check_stage_ok_arg {
6872 struct got_object_id *head_commit_id;
6873 struct got_worktree *worktree;
6874 struct got_fileindex *fileindex;
6875 struct got_repository *repo;
6876 int have_changes;
6879 const struct got_error *
6880 check_stage_ok(void *arg, unsigned char status,
6881 unsigned char staged_status, const char *relpath,
6882 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6883 struct got_object_id *commit_id, int dirfd, const char *de_name)
6885 struct check_stage_ok_arg *a = arg;
6886 const struct got_error *err = NULL;
6887 struct got_fileindex_entry *ie;
6888 struct got_object_id base_commit_id;
6889 struct got_object_id *base_commit_idp = NULL;
6890 char *in_repo_path = NULL, *p;
6892 if (status == GOT_STATUS_UNVERSIONED ||
6893 status == GOT_STATUS_NO_CHANGE)
6894 return NULL;
6895 if (status == GOT_STATUS_NONEXISTENT)
6896 return got_error_set_errno(ENOENT, relpath);
6898 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6899 if (ie == NULL)
6900 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6902 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6903 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6904 relpath) == -1)
6905 return got_error_from_errno("asprintf");
6907 if (got_fileindex_entry_has_commit(ie)) {
6908 memcpy(base_commit_id.sha1, ie->commit_sha1,
6909 SHA1_DIGEST_LENGTH);
6910 base_commit_idp = &base_commit_id;
6913 if (status == GOT_STATUS_CONFLICT) {
6914 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6915 goto done;
6916 } else if (status != GOT_STATUS_ADD &&
6917 status != GOT_STATUS_MODIFY &&
6918 status != GOT_STATUS_DELETE) {
6919 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6920 goto done;
6923 a->have_changes = 1;
6925 p = in_repo_path;
6926 while (p[0] == '/')
6927 p++;
6928 err = check_out_of_date(p, status, staged_status,
6929 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6930 GOT_ERR_STAGE_OUT_OF_DATE);
6931 done:
6932 free(in_repo_path);
6933 return err;
6936 struct stage_path_arg {
6937 struct got_worktree *worktree;
6938 struct got_fileindex *fileindex;
6939 struct got_repository *repo;
6940 got_worktree_status_cb status_cb;
6941 void *status_arg;
6942 got_worktree_patch_cb patch_cb;
6943 void *patch_arg;
6944 int staged_something;
6947 static const struct got_error *
6948 stage_path(void *arg, unsigned char status,
6949 unsigned char staged_status, const char *relpath,
6950 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6951 struct got_object_id *commit_id, int dirfd, const char *de_name)
6953 struct stage_path_arg *a = arg;
6954 const struct got_error *err = NULL;
6955 struct got_fileindex_entry *ie;
6956 char *ondisk_path = NULL, *path_content = NULL;
6957 uint32_t stage;
6958 struct got_object_id *new_staged_blob_id = NULL;
6960 if (status == GOT_STATUS_UNVERSIONED)
6961 return NULL;
6963 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6964 if (ie == NULL)
6965 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6967 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6968 relpath)== -1)
6969 return got_error_from_errno("asprintf");
6971 switch (status) {
6972 case GOT_STATUS_ADD:
6973 case GOT_STATUS_MODIFY:
6974 if (a->patch_cb) {
6975 if (status == GOT_STATUS_ADD) {
6976 int choice = GOT_PATCH_CHOICE_NONE;
6977 err = (*a->patch_cb)(&choice, a->patch_arg,
6978 status, ie->path, NULL, 1, 1);
6979 if (err)
6980 break;
6981 if (choice != GOT_PATCH_CHOICE_YES)
6982 break;
6983 } else {
6984 err = create_patched_content(&path_content, 0,
6985 staged_blob_id ? staged_blob_id : blob_id,
6986 ondisk_path, dirfd, de_name, ie->path,
6987 a->repo, a->patch_cb, a->patch_arg);
6988 if (err || path_content == NULL)
6989 break;
6992 err = got_object_blob_create(&new_staged_blob_id,
6993 path_content ? path_content : ondisk_path, a->repo);
6994 if (err)
6995 break;
6996 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6997 SHA1_DIGEST_LENGTH);
6998 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6999 stage = GOT_FILEIDX_STAGE_ADD;
7000 else
7001 stage = GOT_FILEIDX_STAGE_MODIFY;
7002 got_fileindex_entry_stage_set(ie, stage);
7003 a->staged_something = 1;
7004 if (a->status_cb == NULL)
7005 break;
7006 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7007 get_staged_status(ie), relpath, blob_id,
7008 new_staged_blob_id, NULL, dirfd, de_name);
7009 break;
7010 case GOT_STATUS_DELETE:
7011 if (staged_status == GOT_STATUS_DELETE)
7012 break;
7013 if (a->patch_cb) {
7014 int choice = GOT_PATCH_CHOICE_NONE;
7015 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7016 ie->path, NULL, 1, 1);
7017 if (err)
7018 break;
7019 if (choice == GOT_PATCH_CHOICE_NO)
7020 break;
7021 if (choice != GOT_PATCH_CHOICE_YES) {
7022 err = got_error(GOT_ERR_PATCH_CHOICE);
7023 break;
7026 stage = GOT_FILEIDX_STAGE_DELETE;
7027 got_fileindex_entry_stage_set(ie, stage);
7028 a->staged_something = 1;
7029 if (a->status_cb == NULL)
7030 break;
7031 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7032 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7033 de_name);
7034 break;
7035 case GOT_STATUS_NO_CHANGE:
7036 break;
7037 case GOT_STATUS_CONFLICT:
7038 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7039 break;
7040 case GOT_STATUS_NONEXISTENT:
7041 err = got_error_set_errno(ENOENT, relpath);
7042 break;
7043 default:
7044 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7045 break;
7048 if (path_content && unlink(path_content) == -1 && err == NULL)
7049 err = got_error_from_errno2("unlink", path_content);
7050 free(path_content);
7051 free(ondisk_path);
7052 free(new_staged_blob_id);
7053 return err;
7056 const struct got_error *
7057 got_worktree_stage(struct got_worktree *worktree,
7058 struct got_pathlist_head *paths,
7059 got_worktree_status_cb status_cb, void *status_arg,
7060 got_worktree_patch_cb patch_cb, void *patch_arg,
7061 struct got_repository *repo)
7063 const struct got_error *err = NULL, *sync_err, *unlockerr;
7064 struct got_pathlist_entry *pe;
7065 struct got_fileindex *fileindex = NULL;
7066 char *fileindex_path = NULL;
7067 struct got_reference *head_ref = NULL;
7068 struct got_object_id *head_commit_id = NULL;
7069 struct check_stage_ok_arg oka;
7070 struct stage_path_arg spa;
7072 err = lock_worktree(worktree, LOCK_EX);
7073 if (err)
7074 return err;
7076 err = got_ref_open(&head_ref, repo,
7077 got_worktree_get_head_ref_name(worktree), 0);
7078 if (err)
7079 goto done;
7080 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7081 if (err)
7082 goto done;
7083 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7084 if (err)
7085 goto done;
7087 /* Check pre-conditions before staging anything. */
7088 oka.head_commit_id = head_commit_id;
7089 oka.worktree = worktree;
7090 oka.fileindex = fileindex;
7091 oka.repo = repo;
7092 oka.have_changes = 0;
7093 TAILQ_FOREACH(pe, paths, entry) {
7094 err = worktree_status(worktree, pe->path, fileindex, repo,
7095 check_stage_ok, &oka, NULL, NULL, 0, 0);
7096 if (err)
7097 goto done;
7099 if (!oka.have_changes) {
7100 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7101 goto done;
7104 spa.worktree = worktree;
7105 spa.fileindex = fileindex;
7106 spa.repo = repo;
7107 spa.patch_cb = patch_cb;
7108 spa.patch_arg = patch_arg;
7109 spa.status_cb = status_cb;
7110 spa.status_arg = status_arg;
7111 spa.staged_something = 0;
7112 TAILQ_FOREACH(pe, paths, entry) {
7113 err = worktree_status(worktree, pe->path, fileindex, repo,
7114 stage_path, &spa, NULL, NULL, 0, 0);
7115 if (err)
7116 goto done;
7118 if (!spa.staged_something) {
7119 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7120 goto done;
7123 sync_err = sync_fileindex(fileindex, fileindex_path);
7124 if (sync_err && err == NULL)
7125 err = sync_err;
7126 done:
7127 if (head_ref)
7128 got_ref_close(head_ref);
7129 free(head_commit_id);
7130 free(fileindex_path);
7131 if (fileindex)
7132 got_fileindex_free(fileindex);
7133 unlockerr = lock_worktree(worktree, LOCK_SH);
7134 if (unlockerr && err == NULL)
7135 err = unlockerr;
7136 return err;
7139 struct unstage_path_arg {
7140 struct got_worktree *worktree;
7141 struct got_fileindex *fileindex;
7142 struct got_repository *repo;
7143 got_worktree_checkout_cb progress_cb;
7144 void *progress_arg;
7145 got_worktree_patch_cb patch_cb;
7146 void *patch_arg;
7149 static const struct got_error *
7150 create_unstaged_content(char **path_unstaged_content,
7151 char **path_new_staged_content, struct got_object_id *blob_id,
7152 struct got_object_id *staged_blob_id, const char *relpath,
7153 struct got_repository *repo,
7154 got_worktree_patch_cb patch_cb, void *patch_arg)
7156 const struct got_error *err;
7157 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7158 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7159 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7160 struct stat sb1, sb2;
7161 struct got_diff_changes *changes = NULL;
7162 struct got_diff_state *ds = NULL;
7163 struct got_diff_args *args = NULL;
7164 struct got_diff_change *change;
7165 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7166 int have_content = 0, have_rejected_content = 0;
7168 *path_unstaged_content = NULL;
7169 *path_new_staged_content = NULL;
7171 err = got_object_id_str(&label1, blob_id);
7172 if (err)
7173 return err;
7174 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7175 if (err)
7176 goto done;
7178 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7179 if (err)
7180 goto done;
7182 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7183 if (err)
7184 goto done;
7186 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7187 if (err)
7188 goto done;
7190 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7191 if (err)
7192 goto done;
7194 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7195 if (err)
7196 goto done;
7198 if (stat(path1, &sb1) == -1) {
7199 err = got_error_from_errno2("stat", path1);
7200 goto done;
7203 if (stat(path2, &sb2) == -1) {
7204 err = got_error_from_errno2("stat", path2);
7205 goto done;
7208 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7209 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7210 if (err)
7211 goto done;
7213 err = got_opentemp_named(path_unstaged_content, &outfile,
7214 "got-unstaged-content");
7215 if (err)
7216 goto done;
7217 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7218 "got-new-staged-content");
7219 if (err)
7220 goto done;
7222 if (fseek(f1, 0L, SEEK_SET) == -1) {
7223 err = got_ferror(f1, GOT_ERR_IO);
7224 goto done;
7226 if (fseek(f2, 0L, SEEK_SET) == -1) {
7227 err = got_ferror(f2, GOT_ERR_IO);
7228 goto done;
7230 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7231 int choice;
7232 err = apply_or_reject_change(&choice, change, ++n,
7233 changes->nchanges, ds, args, diff_flags, relpath,
7234 f1, f2, &line_cur1, &line_cur2,
7235 outfile, rejectfile, patch_cb, patch_arg);
7236 if (err)
7237 goto done;
7238 if (choice == GOT_PATCH_CHOICE_YES)
7239 have_content = 1;
7240 else
7241 have_rejected_content = 1;
7242 if (choice == GOT_PATCH_CHOICE_QUIT)
7243 break;
7245 if (have_content || have_rejected_content)
7246 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7247 outfile, rejectfile);
7248 done:
7249 free(label1);
7250 if (blob)
7251 got_object_blob_close(blob);
7252 if (staged_blob)
7253 got_object_blob_close(staged_blob);
7254 if (f1 && fclose(f1) == EOF && err == NULL)
7255 err = got_error_from_errno2("fclose", path1);
7256 if (f2 && fclose(f2) == EOF && err == NULL)
7257 err = got_error_from_errno2("fclose", path2);
7258 if (outfile && fclose(outfile) == EOF && err == NULL)
7259 err = got_error_from_errno2("fclose", *path_unstaged_content);
7260 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7261 err = got_error_from_errno2("fclose", *path_new_staged_content);
7262 if (path1 && unlink(path1) == -1 && err == NULL)
7263 err = got_error_from_errno2("unlink", path1);
7264 if (path2 && unlink(path2) == -1 && err == NULL)
7265 err = got_error_from_errno2("unlink", path2);
7266 if (err || !have_content) {
7267 if (*path_unstaged_content &&
7268 unlink(*path_unstaged_content) == -1 && err == NULL)
7269 err = got_error_from_errno2("unlink",
7270 *path_unstaged_content);
7271 free(*path_unstaged_content);
7272 *path_unstaged_content = NULL;
7274 if (err || !have_rejected_content) {
7275 if (*path_new_staged_content &&
7276 unlink(*path_new_staged_content) == -1 && err == NULL)
7277 err = got_error_from_errno2("unlink",
7278 *path_new_staged_content);
7279 free(*path_new_staged_content);
7280 *path_new_staged_content = NULL;
7282 free(args);
7283 if (ds) {
7284 got_diff_state_free(ds);
7285 free(ds);
7287 if (changes)
7288 got_diff_free_changes(changes);
7289 free(path1);
7290 free(path2);
7291 return err;
7294 static const struct got_error *
7295 unstage_path(void *arg, unsigned char status,
7296 unsigned char staged_status, const char *relpath,
7297 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7298 struct got_object_id *commit_id, int dirfd, const char *de_name)
7300 const struct got_error *err = NULL;
7301 struct unstage_path_arg *a = arg;
7302 struct got_fileindex_entry *ie;
7303 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7304 char *ondisk_path = NULL, *path_unstaged_content = NULL;
7305 char *path_new_staged_content = NULL;
7306 char *id_str = NULL, *label_orig = NULL;
7307 int local_changes_subsumed;
7308 struct stat sb;
7310 if (staged_status != GOT_STATUS_ADD &&
7311 staged_status != GOT_STATUS_MODIFY &&
7312 staged_status != GOT_STATUS_DELETE)
7313 return NULL;
7315 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7316 if (ie == NULL)
7317 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7319 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7320 == -1)
7321 return got_error_from_errno("asprintf");
7323 err = got_object_id_str(&id_str,
7324 commit_id ? commit_id : a->worktree->base_commit_id);
7325 if (err)
7326 goto done;
7327 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7328 id_str) == -1) {
7329 err = got_error_from_errno("asprintf");
7330 goto done;
7333 switch (staged_status) {
7334 case GOT_STATUS_MODIFY:
7335 err = got_object_open_as_blob(&blob_base, a->repo,
7336 blob_id, 8192);
7337 if (err)
7338 break;
7339 /* fall through */
7340 case GOT_STATUS_ADD:
7341 if (a->patch_cb) {
7342 if (staged_status == GOT_STATUS_ADD) {
7343 int choice = GOT_PATCH_CHOICE_NONE;
7344 err = (*a->patch_cb)(&choice, a->patch_arg,
7345 staged_status, ie->path, NULL, 1, 1);
7346 if (err)
7347 break;
7348 if (choice != GOT_PATCH_CHOICE_YES)
7349 break;
7350 } else {
7351 err = create_unstaged_content(
7352 &path_unstaged_content,
7353 &path_new_staged_content, blob_id,
7354 staged_blob_id, ie->path, a->repo,
7355 a->patch_cb, a->patch_arg);
7356 if (err || path_unstaged_content == NULL)
7357 break;
7358 if (path_new_staged_content) {
7359 err = got_object_blob_create(
7360 &staged_blob_id,
7361 path_new_staged_content,
7362 a->repo);
7363 if (err)
7364 break;
7365 memcpy(ie->staged_blob_sha1,
7366 staged_blob_id->sha1,
7367 SHA1_DIGEST_LENGTH);
7369 err = merge_file(&local_changes_subsumed,
7370 a->worktree, blob_base, ondisk_path,
7371 relpath, got_fileindex_perms_to_st(ie),
7372 path_unstaged_content, label_orig,
7373 "unstaged", a->repo, a->progress_cb,
7374 a->progress_arg);
7375 if (err == NULL &&
7376 path_new_staged_content == NULL)
7377 got_fileindex_entry_stage_set(ie,
7378 GOT_FILEIDX_STAGE_NONE);
7379 break; /* Done with this file. */
7382 err = got_object_open_as_blob(&blob_staged, a->repo,
7383 staged_blob_id, 8192);
7384 if (err)
7385 break;
7386 err = merge_blob(&local_changes_subsumed, a->worktree,
7387 blob_base, ondisk_path, relpath,
7388 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7389 commit_id ? commit_id : a->worktree->base_commit_id,
7390 a->repo, a->progress_cb, a->progress_arg);
7391 if (err == NULL)
7392 got_fileindex_entry_stage_set(ie,
7393 GOT_FILEIDX_STAGE_NONE);
7394 break;
7395 case GOT_STATUS_DELETE:
7396 if (a->patch_cb) {
7397 int choice = GOT_PATCH_CHOICE_NONE;
7398 err = (*a->patch_cb)(&choice, a->patch_arg,
7399 staged_status, ie->path, NULL, 1, 1);
7400 if (err)
7401 break;
7402 if (choice == GOT_PATCH_CHOICE_NO)
7403 break;
7404 if (choice != GOT_PATCH_CHOICE_YES) {
7405 err = got_error(GOT_ERR_PATCH_CHOICE);
7406 break;
7409 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7410 err = get_file_status(&status, &sb, ie, ondisk_path,
7411 dirfd, de_name, a->repo);
7412 if (err)
7413 break;
7414 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7415 break;
7417 done:
7418 free(ondisk_path);
7419 if (path_unstaged_content &&
7420 unlink(path_unstaged_content) == -1 && err == NULL)
7421 err = got_error_from_errno2("unlink", path_unstaged_content);
7422 if (path_new_staged_content &&
7423 unlink(path_new_staged_content) == -1 && err == NULL)
7424 err = got_error_from_errno2("unlink", path_new_staged_content);
7425 free(path_unstaged_content);
7426 free(path_new_staged_content);
7427 if (blob_base)
7428 got_object_blob_close(blob_base);
7429 if (blob_staged)
7430 got_object_blob_close(blob_staged);
7431 free(id_str);
7432 free(label_orig);
7433 return err;
7436 const struct got_error *
7437 got_worktree_unstage(struct got_worktree *worktree,
7438 struct got_pathlist_head *paths,
7439 got_worktree_checkout_cb progress_cb, void *progress_arg,
7440 got_worktree_patch_cb patch_cb, void *patch_arg,
7441 struct got_repository *repo)
7443 const struct got_error *err = NULL, *sync_err, *unlockerr;
7444 struct got_pathlist_entry *pe;
7445 struct got_fileindex *fileindex = NULL;
7446 char *fileindex_path = NULL;
7447 struct unstage_path_arg upa;
7449 err = lock_worktree(worktree, LOCK_EX);
7450 if (err)
7451 return err;
7453 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7454 if (err)
7455 goto done;
7457 upa.worktree = worktree;
7458 upa.fileindex = fileindex;
7459 upa.repo = repo;
7460 upa.progress_cb = progress_cb;
7461 upa.progress_arg = progress_arg;
7462 upa.patch_cb = patch_cb;
7463 upa.patch_arg = patch_arg;
7464 TAILQ_FOREACH(pe, paths, entry) {
7465 err = worktree_status(worktree, pe->path, fileindex, repo,
7466 unstage_path, &upa, NULL, NULL, 0, 0);
7467 if (err)
7468 goto done;
7471 sync_err = sync_fileindex(fileindex, fileindex_path);
7472 if (sync_err && err == NULL)
7473 err = sync_err;
7474 done:
7475 free(fileindex_path);
7476 if (fileindex)
7477 got_fileindex_free(fileindex);
7478 unlockerr = lock_worktree(worktree, LOCK_SH);
7479 if (unlockerr && err == NULL)
7480 err = unlockerr;
7481 return err;