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"
58 #include "got_lib_gotconfig.h"
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
69 {
70 const struct got_error *err = NULL;
71 char *path;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
77 free(path);
78 return err;
79 }
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
83 {
84 const struct got_error *err = NULL;
85 FILE *tmpfile = NULL;
86 char *tmppath = NULL;
87 char *path = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
91 path = NULL;
92 goto done;
93 }
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
96 if (err)
97 goto done;
99 if (content) {
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
103 goto done;
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
109 unlink(tmppath);
110 goto done;
113 done:
114 if (fclose(tmpfile) != 0 && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
116 free(tmppath);
117 return err;
120 static const struct got_error *
121 read_meta_file(char **content, const char *path_got, const char *name)
123 const struct got_error *err = NULL;
124 char *path;
125 int fd = -1;
126 ssize_t n;
127 struct stat sb;
129 *content = NULL;
131 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 err = got_error_from_errno("asprintf");
133 path = NULL;
134 goto done;
137 fd = open(path, O_RDONLY | O_NOFOLLOW);
138 if (fd == -1) {
139 if (errno == ENOENT)
140 err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 else
142 err = got_error_from_errno2("open", path);
143 goto done;
145 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 : got_error_from_errno2("flock", path));
148 goto done;
151 if (fstat(fd, &sb) != 0) {
152 err = got_error_from_errno2("fstat", path);
153 goto done;
155 *content = calloc(1, sb.st_size);
156 if (*content == NULL) {
157 err = got_error_from_errno("calloc");
158 goto done;
161 n = read(fd, *content, sb.st_size);
162 if (n != sb.st_size) {
163 err = (n == -1 ? got_error_from_errno2("read", path) :
164 got_error_path(path, GOT_ERR_WORKTREE_META));
165 goto done;
167 if ((*content)[sb.st_size - 1] != '\n') {
168 err = got_error_path(path, GOT_ERR_WORKTREE_META);
169 goto done;
171 (*content)[sb.st_size - 1] = '\0';
173 done:
174 if (fd != -1 && close(fd) == -1 && err == NULL)
175 err = got_error_from_errno2("close", path_got);
176 free(path);
177 if (err) {
178 free(*content);
179 *content = NULL;
181 return err;
184 static const struct got_error *
185 write_head_ref(const char *path_got, struct got_reference *head_ref)
187 const struct got_error *err = NULL;
188 char *refstr = NULL;
190 if (got_ref_is_symbolic(head_ref)) {
191 refstr = got_ref_to_str(head_ref);
192 if (refstr == NULL)
193 return got_error_from_errno("got_ref_to_str");
194 } else {
195 refstr = strdup(got_ref_get_name(head_ref));
196 if (refstr == NULL)
197 return got_error_from_errno("strdup");
199 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
200 free(refstr);
201 return err;
204 const struct got_error *
205 got_worktree_init(const char *path, struct got_reference *head_ref,
206 const char *prefix, struct got_repository *repo)
208 const struct got_error *err = NULL;
209 struct got_object_id *commit_id = NULL;
210 uuid_t uuid;
211 uint32_t uuid_status;
212 int obj_type;
213 char *path_got = NULL;
214 char *formatstr = NULL;
215 char *absprefix = NULL;
216 char *basestr = NULL;
217 char *uuidstr = NULL;
219 if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 err = got_error(GOT_ERR_WORKTREE_REPO);
221 goto done;
224 err = got_ref_resolve(&commit_id, repo, head_ref);
225 if (err)
226 return err;
227 err = got_object_get_type(&obj_type, repo, commit_id);
228 if (err)
229 return err;
230 if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 return got_error(GOT_ERR_OBJ_TYPE);
233 if (!got_path_is_absolute(prefix)) {
234 if (asprintf(&absprefix, "/%s", prefix) == -1)
235 return got_error_from_errno("asprintf");
238 /* Create top-level directory (may already exist). */
239 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 err = got_error_from_errno2("mkdir", path);
241 goto done;
244 /* Create .got directory (may already exist). */
245 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 err = got_error_from_errno("asprintf");
247 goto done;
249 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 err = got_error_from_errno2("mkdir", path_got);
251 goto done;
254 /* Create an empty lock file. */
255 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
256 if (err)
257 goto done;
259 /* Create an empty file index. */
260 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
261 if (err)
262 goto done;
264 /* Write the HEAD reference. */
265 err = write_head_ref(path_got, head_ref);
266 if (err)
267 goto done;
269 /* Record our base commit. */
270 err = got_object_id_str(&basestr, commit_id);
271 if (err)
272 goto done;
273 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
274 if (err)
275 goto done;
277 /* Store path to repository. */
278 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 got_repo_get_path(repo));
280 if (err)
281 goto done;
283 /* Store in-repository path prefix. */
284 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 absprefix ? absprefix : prefix);
286 if (err)
287 goto done;
289 /* Generate UUID. */
290 uuid_create(&uuid, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status, "uuid_create");
293 goto done;
295 uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 if (uuid_status != uuid_s_ok) {
297 err = got_error_uuid(uuid_status, "uuid_to_string");
298 goto done;
300 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
301 if (err)
302 goto done;
304 /* Stamp work tree with format file. */
305 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 err = got_error_from_errno("asprintf");
307 goto done;
309 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
310 if (err)
311 goto done;
313 done:
314 free(commit_id);
315 free(path_got);
316 free(formatstr);
317 free(absprefix);
318 free(basestr);
319 free(uuidstr);
320 return err;
323 static const struct got_error *
324 open_worktree(struct got_worktree **worktree, const char *path)
326 const struct got_error *err = NULL;
327 char *path_got;
328 char *formatstr = NULL;
329 char *uuidstr = NULL;
330 char *path_lock = NULL;
331 char *base_commit_id_str = NULL;
332 int version, fd = -1;
333 const char *errstr;
334 struct got_repository *repo = NULL;
335 uint32_t uuid_status;
337 *worktree = NULL;
339 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 err = got_error_from_errno("asprintf");
341 path_got = NULL;
342 goto done;
345 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 err = got_error_from_errno("asprintf");
347 path_lock = NULL;
348 goto done;
351 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 if (fd == -1) {
353 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 : got_error_from_errno2("open", path_lock));
355 goto done;
358 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
359 if (err)
360 goto done;
362 version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 if (errstr) {
364 err = got_error_msg(GOT_ERR_WORKTREE_META,
365 "could not parse work tree format version number");
366 goto done;
368 if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 err = got_error(GOT_ERR_WORKTREE_VERS);
370 goto done;
373 *worktree = calloc(1, sizeof(**worktree));
374 if (*worktree == NULL) {
375 err = got_error_from_errno("calloc");
376 goto done;
378 (*worktree)->lockfd = -1;
380 (*worktree)->root_path = realpath(path, NULL);
381 if ((*worktree)->root_path == NULL) {
382 err = got_error_from_errno2("realpath", path);
383 goto done;
385 err = read_meta_file(&(*worktree)->repo_path, path_got,
386 GOT_WORKTREE_REPOSITORY);
387 if (err)
388 goto done;
390 err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 GOT_WORKTREE_PATH_PREFIX);
392 if (err)
393 goto done;
395 err = read_meta_file(&base_commit_id_str, path_got,
396 GOT_WORKTREE_BASE_COMMIT);
397 if (err)
398 goto done;
400 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
401 if (err)
402 goto done;
403 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 if (uuid_status != uuid_s_ok) {
405 err = got_error_uuid(uuid_status, "uuid_from_string");
406 goto done;
409 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
410 if (err)
411 goto done;
413 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 base_commit_id_str);
415 if (err)
416 goto done;
418 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 GOT_WORKTREE_HEAD_REF);
420 if (err)
421 goto done;
423 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 (*worktree)->root_path,
425 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 err = got_error_from_errno("asprintf");
427 goto done;
430 err = got_gotconfig_read(&(*worktree)->gotconfig,
431 (*worktree)->gotconfig_path);
432 done:
433 if (repo)
434 got_repo_close(repo);
435 free(path_got);
436 free(path_lock);
437 free(base_commit_id_str);
438 free(uuidstr);
439 free(formatstr);
440 if (err) {
441 if (fd != -1)
442 close(fd);
443 if (*worktree != NULL)
444 got_worktree_close(*worktree);
445 *worktree = NULL;
446 } else
447 (*worktree)->lockfd = fd;
449 return err;
452 const struct got_error *
453 got_worktree_open(struct got_worktree **worktree, const char *path)
455 const struct got_error *err = NULL;
456 char *worktree_path;
458 worktree_path = strdup(path);
459 if (worktree_path == NULL)
460 return got_error_from_errno("strdup");
462 for (;;) {
463 char *parent_path;
465 err = open_worktree(worktree, worktree_path);
466 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
467 free(worktree_path);
468 return err;
470 if (*worktree) {
471 free(worktree_path);
472 return NULL;
474 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
475 break;
476 err = got_path_dirname(&parent_path, worktree_path);
477 if (err) {
478 if (err->code != GOT_ERR_BAD_PATH) {
479 free(worktree_path);
480 return err;
482 break;
484 free(worktree_path);
485 worktree_path = parent_path;
488 free(worktree_path);
489 return got_error(GOT_ERR_NOT_WORKTREE);
492 const struct got_error *
493 got_worktree_close(struct got_worktree *worktree)
495 const struct got_error *err = NULL;
496 free(worktree->repo_path);
497 free(worktree->path_prefix);
498 free(worktree->base_commit_id);
499 free(worktree->head_ref_name);
500 if (worktree->lockfd != -1)
501 if (close(worktree->lockfd) != 0)
502 err = got_error_from_errno2("close",
503 got_worktree_get_root_path(worktree));
504 free(worktree->root_path);
505 free(worktree->gotconfig_path);
506 got_gotconfig_free(worktree->gotconfig);
507 free(worktree);
508 return err;
511 const char *
512 got_worktree_get_root_path(struct got_worktree *worktree)
514 return worktree->root_path;
517 const char *
518 got_worktree_get_repo_path(struct got_worktree *worktree)
520 return worktree->repo_path;
522 const char *
523 got_worktree_get_path_prefix(struct got_worktree *worktree)
525 return worktree->path_prefix;
528 const struct got_error *
529 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
530 const char *path_prefix)
532 char *absprefix = NULL;
534 if (!got_path_is_absolute(path_prefix)) {
535 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
536 return got_error_from_errno("asprintf");
538 *match = (strcmp(absprefix ? absprefix : path_prefix,
539 worktree->path_prefix) == 0);
540 free(absprefix);
541 return NULL;
544 const char *
545 got_worktree_get_head_ref_name(struct got_worktree *worktree)
547 return worktree->head_ref_name;
550 const struct got_error *
551 got_worktree_set_head_ref(struct got_worktree *worktree,
552 struct got_reference *head_ref)
554 const struct got_error *err = NULL;
555 char *path_got = NULL, *head_ref_name = NULL;
557 if (asprintf(&path_got, "%s/%s", worktree->root_path,
558 GOT_WORKTREE_GOT_DIR) == -1) {
559 err = got_error_from_errno("asprintf");
560 path_got = NULL;
561 goto done;
564 head_ref_name = strdup(got_ref_get_name(head_ref));
565 if (head_ref_name == NULL) {
566 err = got_error_from_errno("strdup");
567 goto done;
570 err = write_head_ref(path_got, head_ref);
571 if (err)
572 goto done;
574 free(worktree->head_ref_name);
575 worktree->head_ref_name = head_ref_name;
576 done:
577 free(path_got);
578 if (err)
579 free(head_ref_name);
580 return err;
583 struct got_object_id *
584 got_worktree_get_base_commit_id(struct got_worktree *worktree)
586 return worktree->base_commit_id;
589 const struct got_error *
590 got_worktree_set_base_commit_id(struct got_worktree *worktree,
591 struct got_repository *repo, struct got_object_id *commit_id)
593 const struct got_error *err;
594 struct got_object *obj = NULL;
595 char *id_str = NULL;
596 char *path_got = NULL;
598 if (asprintf(&path_got, "%s/%s", worktree->root_path,
599 GOT_WORKTREE_GOT_DIR) == -1) {
600 err = got_error_from_errno("asprintf");
601 path_got = NULL;
602 goto done;
605 err = got_object_open(&obj, repo, commit_id);
606 if (err)
607 return err;
609 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
610 err = got_error(GOT_ERR_OBJ_TYPE);
611 goto done;
614 /* Record our base commit. */
615 err = got_object_id_str(&id_str, commit_id);
616 if (err)
617 goto done;
618 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
619 if (err)
620 goto done;
622 free(worktree->base_commit_id);
623 worktree->base_commit_id = got_object_id_dup(commit_id);
624 if (worktree->base_commit_id == NULL) {
625 err = got_error_from_errno("got_object_id_dup");
626 goto done;
628 done:
629 if (obj)
630 got_object_close(obj);
631 free(id_str);
632 free(path_got);
633 return err;
636 const struct got_gotconfig *
637 got_worktree_get_gotconfig(struct got_worktree *worktree)
639 return worktree->gotconfig;
642 static const struct got_error *
643 lock_worktree(struct got_worktree *worktree, int operation)
645 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
646 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
647 : got_error_from_errno2("flock",
648 got_worktree_get_root_path(worktree)));
649 return NULL;
652 static const struct got_error *
653 add_dir_on_disk(struct got_worktree *worktree, const char *path)
655 const struct got_error *err = NULL;
656 char *abspath;
658 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
659 return got_error_from_errno("asprintf");
661 err = got_path_mkdir(abspath);
662 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
663 struct stat sb;
664 err = NULL;
665 if (lstat(abspath, &sb) == -1) {
666 err = got_error_from_errno2("lstat", abspath);
667 } else if (!S_ISDIR(sb.st_mode)) {
668 /* TODO directory is obstructed; do something */
669 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
672 free(abspath);
673 return err;
676 static const struct got_error *
677 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
679 const struct got_error *err = NULL;
680 uint8_t fbuf1[8192];
681 uint8_t fbuf2[8192];
682 size_t flen1 = 0, flen2 = 0;
684 *same = 1;
686 for (;;) {
687 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
688 if (flen1 == 0 && ferror(f1)) {
689 err = got_error_from_errno("fread");
690 break;
692 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
693 if (flen2 == 0 && ferror(f2)) {
694 err = got_error_from_errno("fread");
695 break;
697 if (flen1 == 0) {
698 if (flen2 != 0)
699 *same = 0;
700 break;
701 } else if (flen2 == 0) {
702 if (flen1 != 0)
703 *same = 0;
704 break;
705 } else if (flen1 == flen2) {
706 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
707 *same = 0;
708 break;
710 } else {
711 *same = 0;
712 break;
716 return err;
719 static const struct got_error *
720 check_files_equal(int *same, const char *f1_path, const char *f2_path)
722 const struct got_error *err = NULL;
723 struct stat sb;
724 size_t size1, size2;
725 FILE *f1 = NULL, *f2 = NULL;
727 *same = 1;
729 if (lstat(f1_path, &sb) != 0) {
730 err = got_error_from_errno2("lstat", f1_path);
731 goto done;
733 size1 = sb.st_size;
735 if (lstat(f2_path, &sb) != 0) {
736 err = got_error_from_errno2("lstat", f2_path);
737 goto done;
739 size2 = sb.st_size;
741 if (size1 != size2) {
742 *same = 0;
743 return NULL;
746 f1 = fopen(f1_path, "r");
747 if (f1 == NULL)
748 return got_error_from_errno2("fopen", f1_path);
750 f2 = fopen(f2_path, "r");
751 if (f2 == NULL) {
752 err = got_error_from_errno2("fopen", f2_path);
753 goto done;
756 err = check_file_contents_equal(same, f1, f2);
757 done:
758 if (f1 && fclose(f1) != 0 && err == NULL)
759 err = got_error_from_errno("fclose");
760 if (f2 && fclose(f2) != 0 && err == NULL)
761 err = got_error_from_errno("fclose");
763 return err;
766 /*
767 * Perform a 3-way merge where blob_orig acts as the common ancestor,
768 * the file at deriv_path acts as the first derived version, and the
769 * file on disk acts as the second derived version.
770 */
771 static const struct got_error *
772 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
773 struct got_blob_object *blob_orig, const char *ondisk_path,
774 const char *path, uint16_t st_mode, const char *deriv_path,
775 const char *label_orig, const char *label_deriv,
776 struct got_repository *repo,
777 got_worktree_checkout_cb progress_cb, void *progress_arg)
779 const struct got_error *err = NULL;
780 int merged_fd = -1;
781 FILE *f_orig = NULL;
782 char *blob_orig_path = NULL;
783 char *merged_path = NULL, *base_path = NULL;
784 int overlapcnt = 0;
785 char *parent = NULL;
786 char *symlink_path = NULL;
787 FILE *symlinkf = NULL;
789 *local_changes_subsumed = 0;
791 err = got_path_dirname(&parent, ondisk_path);
792 if (err)
793 return err;
795 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
796 err = got_error_from_errno("asprintf");
797 goto done;
800 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
801 if (err)
802 goto done;
804 free(base_path);
805 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
806 err = got_error_from_errno("asprintf");
807 base_path = NULL;
808 goto done;
811 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
812 if (err)
813 goto done;
814 if (blob_orig) {
815 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
816 blob_orig);
817 if (err)
818 goto done;
819 } else {
820 /*
821 * If the file has no blob, this is an "add vs add" conflict,
822 * and we simply use an empty ancestor file to make both files
823 * appear in the merged result in their entirety.
824 */
827 /*
828 * In order the run a 3-way merge with a symlink we copy the symlink's
829 * target path into a temporary file and use that file with diff3.
830 */
831 if (S_ISLNK(st_mode)) {
832 char target_path[PATH_MAX];
833 ssize_t target_len;
834 size_t n;
836 free(base_path);
837 if (asprintf(&base_path, "%s/got-symlink-merge",
838 parent) == -1) {
839 err = got_error_from_errno("asprintf");
840 base_path = NULL;
841 goto done;
843 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
844 if (err)
845 goto done;
846 target_len = readlink(ondisk_path, target_path,
847 sizeof(target_path));
848 if (target_len == -1) {
849 err = got_error_from_errno2("readlink", ondisk_path);
850 goto done;
852 n = fwrite(target_path, 1, target_len, symlinkf);
853 if (n != target_len) {
854 err = got_ferror(symlinkf, GOT_ERR_IO);
855 goto done;
857 if (fflush(symlinkf) == EOF) {
858 err = got_error_from_errno2("fflush", symlink_path);
859 goto done;
863 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
864 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
865 label_deriv, label_orig, NULL);
866 if (err)
867 goto done;
869 err = (*progress_cb)(progress_arg,
870 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
871 if (err)
872 goto done;
874 if (fsync(merged_fd) != 0) {
875 err = got_error_from_errno("fsync");
876 goto done;
879 /* Check if a clean merge has subsumed all local changes. */
880 if (overlapcnt == 0) {
881 err = check_files_equal(local_changes_subsumed, deriv_path,
882 merged_path);
883 if (err)
884 goto done;
887 if (fchmod(merged_fd, st_mode) != 0) {
888 err = got_error_from_errno2("fchmod", merged_path);
889 goto done;
892 if (rename(merged_path, ondisk_path) != 0) {
893 err = got_error_from_errno3("rename", merged_path,
894 ondisk_path);
895 goto done;
897 done:
898 if (err) {
899 if (merged_path)
900 unlink(merged_path);
902 if (symlink_path) {
903 if (unlink(symlink_path) == -1 && err == NULL)
904 err = got_error_from_errno2("unlink", symlink_path);
906 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
907 err = got_error_from_errno2("fclose", symlink_path);
908 free(symlink_path);
909 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
910 err = got_error_from_errno("close");
911 if (f_orig && fclose(f_orig) != 0 && err == NULL)
912 err = got_error_from_errno("fclose");
913 free(merged_path);
914 free(base_path);
915 if (blob_orig_path) {
916 unlink(blob_orig_path);
917 free(blob_orig_path);
919 free(parent);
920 return err;
923 static const struct got_error *
924 update_symlink(const char *ondisk_path, const char *target_path,
925 size_t target_len)
927 /* This is not atomic but matches what 'ln -sf' does. */
928 if (unlink(ondisk_path) == -1)
929 return got_error_from_errno2("unlink", ondisk_path);
930 if (symlink(target_path, ondisk_path) == -1)
931 return got_error_from_errno3("symlink", target_path,
932 ondisk_path);
933 return NULL;
936 /*
937 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
938 * in the work tree with a file that contains conflict markers and the
939 * conflicting target paths of the original version, a "derived version"
940 * of a symlink from an incoming change, and a local version of the symlink.
942 * The original versions's target path can be NULL if it is not available,
943 * such as if both derived versions added a new symlink at the same path.
945 * The incoming derived symlink target is NULL in case the incoming change
946 * has deleted this symlink.
947 */
948 static const struct got_error *
949 install_symlink_conflict(const char *deriv_target,
950 struct got_object_id *deriv_base_commit_id, const char *orig_target,
951 const char *label_orig, const char *local_target, const char *ondisk_path)
953 const struct got_error *err;
954 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
955 FILE *f = NULL;
957 err = got_object_id_str(&id_str, deriv_base_commit_id);
958 if (err)
959 return got_error_from_errno("asprintf");
961 if (asprintf(&label_deriv, "%s: commit %s",
962 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
963 err = got_error_from_errno("asprintf");
964 goto done;
967 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
968 if (err)
969 goto done;
971 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
972 err = got_error_from_errno2("fchmod", path);
973 goto done;
976 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
977 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
978 deriv_target ? deriv_target : "(symlink was deleted)",
979 orig_target ? label_orig : "",
980 orig_target ? "\n" : "",
981 orig_target ? orig_target : "",
982 orig_target ? "\n" : "",
983 GOT_DIFF_CONFLICT_MARKER_SEP,
984 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
985 err = got_error_from_errno2("fprintf", path);
986 goto done;
989 if (unlink(ondisk_path) == -1) {
990 err = got_error_from_errno2("unlink", ondisk_path);
991 goto done;
993 if (rename(path, ondisk_path) == -1) {
994 err = got_error_from_errno3("rename", path, ondisk_path);
995 goto done;
997 done:
998 if (f != NULL && fclose(f) == EOF && err == NULL)
999 err = got_error_from_errno2("fclose", path);
1000 free(path);
1001 free(id_str);
1002 free(label_deriv);
1003 return err;
1006 /* forward declaration */
1007 static const struct got_error *
1008 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1009 const char *, const char *, uint16_t, const char *,
1010 struct got_blob_object *, struct got_object_id *,
1011 struct got_repository *, got_worktree_checkout_cb, void *);
1014 * Merge a symlink into the work tree, where blob_orig acts as the common
1015 * ancestor, deriv_target is the link target of the first derived version,
1016 * and the symlink on disk acts as the second derived version.
1017 * Assume that contents of both blobs represent symlinks.
1019 static const struct got_error *
1020 merge_symlink(struct got_worktree *worktree,
1021 struct got_blob_object *blob_orig, const char *ondisk_path,
1022 const char *path, const char *label_orig, const char *deriv_target,
1023 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1024 got_worktree_checkout_cb progress_cb, void *progress_arg)
1026 const struct got_error *err = NULL;
1027 char *ancestor_target = NULL;
1028 struct stat sb;
1029 ssize_t ondisk_len, deriv_len;
1030 char ondisk_target[PATH_MAX];
1031 int have_local_change = 0;
1032 int have_incoming_change = 0;
1034 if (lstat(ondisk_path, &sb) == -1)
1035 return got_error_from_errno2("lstat", ondisk_path);
1037 ondisk_len = readlink(ondisk_path, ondisk_target,
1038 sizeof(ondisk_target));
1039 if (ondisk_len == -1) {
1040 err = got_error_from_errno2("readlink",
1041 ondisk_path);
1042 goto done;
1044 ondisk_target[ondisk_len] = '\0';
1046 if (blob_orig) {
1047 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1048 if (err)
1049 goto done;
1052 if (ancestor_target == NULL ||
1053 (ondisk_len != strlen(ancestor_target) ||
1054 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1055 have_local_change = 1;
1057 deriv_len = strlen(deriv_target);
1058 if (ancestor_target == NULL ||
1059 (deriv_len != strlen(ancestor_target) ||
1060 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1061 have_incoming_change = 1;
1063 if (!have_local_change && !have_incoming_change) {
1064 if (ancestor_target) {
1065 /* Both sides made the same change. */
1066 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1067 path);
1068 } else if (deriv_len == ondisk_len &&
1069 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1070 /* Both sides added the same symlink. */
1071 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1072 path);
1073 } else {
1074 /* Both sides added symlinks which don't match. */
1075 err = install_symlink_conflict(deriv_target,
1076 deriv_base_commit_id, ancestor_target,
1077 label_orig, ondisk_target, ondisk_path);
1078 if (err)
1079 goto done;
1080 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1081 path);
1083 } else if (!have_local_change && have_incoming_change) {
1084 /* Apply the incoming change. */
1085 err = update_symlink(ondisk_path, deriv_target,
1086 strlen(deriv_target));
1087 if (err)
1088 goto done;
1089 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1090 } else if (have_local_change && have_incoming_change) {
1091 if (deriv_len == ondisk_len &&
1092 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1093 /* Both sides made the same change. */
1094 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1095 path);
1096 } else {
1097 err = install_symlink_conflict(deriv_target,
1098 deriv_base_commit_id, ancestor_target, label_orig,
1099 ondisk_target, ondisk_path);
1100 if (err)
1101 goto done;
1102 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1103 path);
1107 done:
1108 free(ancestor_target);
1109 return err;
1113 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1114 * blob_deriv acts as the first derived version, and the file on disk
1115 * acts as the second derived version.
1117 static const struct got_error *
1118 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1119 struct got_blob_object *blob_orig, const char *ondisk_path,
1120 const char *path, uint16_t st_mode, const char *label_orig,
1121 struct got_blob_object *blob_deriv,
1122 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1123 got_worktree_checkout_cb progress_cb, void *progress_arg)
1125 const struct got_error *err = NULL;
1126 FILE *f_deriv = NULL;
1127 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1128 char *label_deriv = NULL, *parent = NULL;
1130 *local_changes_subsumed = 0;
1132 err = got_path_dirname(&parent, ondisk_path);
1133 if (err)
1134 return err;
1136 free(base_path);
1137 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1138 err = got_error_from_errno("asprintf");
1139 base_path = NULL;
1140 goto done;
1143 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1144 if (err)
1145 goto done;
1146 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1147 blob_deriv);
1148 if (err)
1149 goto done;
1151 err = got_object_id_str(&id_str, deriv_base_commit_id);
1152 if (err)
1153 goto done;
1154 if (asprintf(&label_deriv, "%s: commit %s",
1155 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1156 err = got_error_from_errno("asprintf");
1157 goto done;
1160 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1161 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1162 label_deriv, repo, progress_cb, progress_arg);
1163 done:
1164 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1165 err = got_error_from_errno("fclose");
1166 free(base_path);
1167 if (blob_deriv_path) {
1168 unlink(blob_deriv_path);
1169 free(blob_deriv_path);
1171 free(id_str);
1172 free(label_deriv);
1173 free(parent);
1174 return err;
1177 static const struct got_error *
1178 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1179 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1180 const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1182 const struct got_error *err = NULL;
1183 struct got_fileindex_entry *new_ie;
1185 *new_iep = NULL;
1187 err = got_fileindex_entry_alloc(&new_ie, path);
1188 if (err)
1189 return err;
1191 err = got_fileindex_entry_update(new_ie, ondisk_path,
1192 blob_id->sha1, base_commit_id->sha1, 1);
1193 if (err)
1194 goto done;
1196 err = got_fileindex_entry_add(fileindex, new_ie);
1197 done:
1198 if (err)
1199 got_fileindex_entry_free(new_ie);
1200 else
1201 *new_iep = new_ie;
1202 return err;
1205 static mode_t
1206 get_ondisk_perms(int executable, mode_t st_mode)
1208 mode_t xbits = S_IXUSR;
1210 if (executable) {
1211 /* Map read bits to execute bits. */
1212 if (st_mode & S_IRGRP)
1213 xbits |= S_IXGRP;
1214 if (st_mode & S_IROTH)
1215 xbits |= S_IXOTH;
1216 return st_mode | xbits;
1219 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1222 /* forward declaration */
1223 static const struct got_error *
1224 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1225 const char *path, mode_t te_mode, mode_t st_mode,
1226 struct got_blob_object *blob, int restoring_missing_file,
1227 int reverting_versioned_file, int installing_bad_symlink,
1228 int path_is_unversioned, struct got_repository *repo,
1229 got_worktree_checkout_cb progress_cb, void *progress_arg);
1232 * This function assumes that the provided symlink target points at a
1233 * safe location in the work tree!
1235 static const struct got_error *
1236 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1237 size_t target_len)
1239 const struct got_error *err = NULL;
1240 ssize_t elen;
1241 char etarget[PATH_MAX];
1242 int fd;
1245 * "Bad" symlinks (those pointing outside the work tree or into the
1246 * .got directory) are installed in the work tree as a regular file
1247 * which contains the bad symlink target path.
1248 * The new symlink target has already been checked for safety by our
1249 * caller. If we can successfully open a regular file then we simply
1250 * replace this file with a symlink below.
1252 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1253 if (fd == -1) {
1254 if (errno != ELOOP)
1255 return got_error_from_errno2("open", ondisk_path);
1257 /* We are updating an existing on-disk symlink. */
1258 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1259 if (elen == -1)
1260 return got_error_from_errno2("readlink", ondisk_path);
1262 if (elen == target_len &&
1263 memcmp(etarget, target_path, target_len) == 0)
1264 return NULL; /* nothing to do */
1267 err = update_symlink(ondisk_path, target_path, target_len);
1268 if (fd != -1 && close(fd) == -1 && err == NULL)
1269 err = got_error_from_errno2("close", ondisk_path);
1270 return err;
1273 static const struct got_error *
1274 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1275 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1277 const struct got_error *err = NULL;
1278 char canonpath[PATH_MAX];
1279 char *path_got = NULL;
1281 *is_bad_symlink = 0;
1283 if (target_len >= sizeof(canonpath)) {
1284 *is_bad_symlink = 1;
1285 return NULL;
1289 * We do not use realpath(3) to resolve the symlink's target
1290 * path because we don't want to resolve symlinks recursively.
1291 * Instead we make the path absolute and then canonicalize it.
1292 * Relative symlink target lookup should begin at the directory
1293 * in which the blob object is being installed.
1295 if (!got_path_is_absolute(target_path)) {
1296 char *abspath, *parent;
1297 err = got_path_dirname(&parent, ondisk_path);
1298 if (err)
1299 return err;
1300 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1301 free(parent);
1302 return got_error_from_errno("asprintf");
1304 free(parent);
1305 if (strlen(abspath) >= sizeof(canonpath)) {
1306 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1307 free(abspath);
1308 return err;
1310 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1311 free(abspath);
1312 if (err)
1313 return err;
1314 } else {
1315 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1316 if (err)
1317 return err;
1320 /* Only allow symlinks pointing at paths within the work tree. */
1321 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1322 *is_bad_symlink = 1;
1323 return NULL;
1326 /* Do not allow symlinks pointing into the .got directory. */
1327 if (asprintf(&path_got, "%s/%s", wtroot_path,
1328 GOT_WORKTREE_GOT_DIR) == -1)
1329 return got_error_from_errno("asprintf");
1330 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1331 *is_bad_symlink = 1;
1333 free(path_got);
1334 return NULL;
1337 static const struct got_error *
1338 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1339 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1340 int restoring_missing_file, int reverting_versioned_file,
1341 int path_is_unversioned, struct got_repository *repo,
1342 got_worktree_checkout_cb progress_cb, void *progress_arg)
1344 const struct got_error *err = NULL;
1345 char target_path[PATH_MAX];
1346 size_t len, target_len = 0;
1347 char *path_got = NULL;
1348 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1349 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1351 *is_bad_symlink = 0;
1354 * Blob object content specifies the target path of the link.
1355 * If a symbolic link cannot be installed we instead create
1356 * a regular file which contains the link target path stored
1357 * in the blob object.
1359 do {
1360 err = got_object_blob_read_block(&len, blob);
1361 if (len + target_len >= sizeof(target_path)) {
1362 /* Path too long; install as a regular file. */
1363 *is_bad_symlink = 1;
1364 got_object_blob_rewind(blob);
1365 return install_blob(worktree, ondisk_path, path,
1366 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1367 restoring_missing_file, reverting_versioned_file,
1368 1, path_is_unversioned, repo, progress_cb,
1369 progress_arg);
1371 if (len > 0) {
1372 /* Skip blob object header first time around. */
1373 memcpy(target_path + target_len, buf + hdrlen,
1374 len - hdrlen);
1375 target_len += len - hdrlen;
1376 hdrlen = 0;
1378 } while (len != 0);
1379 target_path[target_len] = '\0';
1381 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1382 ondisk_path, worktree->root_path);
1383 if (err)
1384 return err;
1386 if (*is_bad_symlink) {
1387 /* install as a regular file */
1388 *is_bad_symlink = 1;
1389 got_object_blob_rewind(blob);
1390 err = install_blob(worktree, ondisk_path, path,
1391 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1392 restoring_missing_file, reverting_versioned_file, 1,
1393 path_is_unversioned, repo, progress_cb, progress_arg);
1394 goto done;
1397 if (symlink(target_path, ondisk_path) == -1) {
1398 if (errno == EEXIST) {
1399 if (path_is_unversioned) {
1400 err = (*progress_cb)(progress_arg,
1401 GOT_STATUS_UNVERSIONED, path);
1402 goto done;
1404 err = replace_existing_symlink(ondisk_path,
1405 target_path, target_len);
1406 if (err)
1407 goto done;
1408 if (progress_cb) {
1409 err = (*progress_cb)(progress_arg,
1410 reverting_versioned_file ?
1411 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1412 path);
1414 goto done; /* Nothing else to do. */
1417 if (errno == ENOENT) {
1418 char *parent;
1419 err = got_path_dirname(&parent, ondisk_path);
1420 if (err)
1421 goto done;
1422 err = add_dir_on_disk(worktree, parent);
1423 free(parent);
1424 if (err)
1425 goto done;
1427 * Retry, and fall through to error handling
1428 * below if this second attempt fails.
1430 if (symlink(target_path, ondisk_path) != -1) {
1431 err = NULL; /* success */
1432 goto done;
1436 /* Handle errors from first or second creation attempt. */
1437 if (errno == ENAMETOOLONG) {
1438 /* bad target path; install as a regular file */
1439 *is_bad_symlink = 1;
1440 got_object_blob_rewind(blob);
1441 err = install_blob(worktree, ondisk_path, path,
1442 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1443 restoring_missing_file, reverting_versioned_file, 1,
1444 path_is_unversioned, repo,
1445 progress_cb, progress_arg);
1446 } else if (errno == ENOTDIR) {
1447 err = got_error_path(ondisk_path,
1448 GOT_ERR_FILE_OBSTRUCTED);
1449 } else {
1450 err = got_error_from_errno3("symlink",
1451 target_path, ondisk_path);
1453 } else if (progress_cb)
1454 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1455 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1456 done:
1457 free(path_got);
1458 return err;
1461 static const struct got_error *
1462 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1463 const char *path, mode_t te_mode, mode_t st_mode,
1464 struct got_blob_object *blob, int restoring_missing_file,
1465 int reverting_versioned_file, int installing_bad_symlink,
1466 int path_is_unversioned, struct got_repository *repo,
1467 got_worktree_checkout_cb progress_cb, void *progress_arg)
1469 const struct got_error *err = NULL;
1470 int fd = -1;
1471 size_t len, hdrlen;
1472 int update = 0;
1473 char *tmppath = NULL;
1475 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1476 GOT_DEFAULT_FILE_MODE);
1477 if (fd == -1) {
1478 if (errno == ENOENT) {
1479 char *parent;
1480 err = got_path_dirname(&parent, path);
1481 if (err)
1482 return err;
1483 err = add_dir_on_disk(worktree, parent);
1484 free(parent);
1485 if (err)
1486 return err;
1487 fd = open(ondisk_path,
1488 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1489 GOT_DEFAULT_FILE_MODE);
1490 if (fd == -1)
1491 return got_error_from_errno2("open",
1492 ondisk_path);
1493 } else if (errno == EEXIST) {
1494 if (path_is_unversioned) {
1495 err = (*progress_cb)(progress_arg,
1496 GOT_STATUS_UNVERSIONED, path);
1497 goto done;
1499 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1500 /* TODO file is obstructed; do something */
1501 err = got_error_path(ondisk_path,
1502 GOT_ERR_FILE_OBSTRUCTED);
1503 goto done;
1504 } else {
1505 err = got_opentemp_named_fd(&tmppath, &fd,
1506 ondisk_path);
1507 if (err)
1508 goto done;
1509 update = 1;
1511 } else
1512 return got_error_from_errno2("open", ondisk_path);
1515 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1516 err = got_error_from_errno2("fchmod",
1517 update ? tmppath : ondisk_path);
1518 goto done;
1521 if (progress_cb) {
1522 if (restoring_missing_file)
1523 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1524 path);
1525 else if (reverting_versioned_file)
1526 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1527 path);
1528 else
1529 err = (*progress_cb)(progress_arg,
1530 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1531 if (err)
1532 goto done;
1535 hdrlen = got_object_blob_get_hdrlen(blob);
1536 do {
1537 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1538 err = got_object_blob_read_block(&len, blob);
1539 if (err)
1540 break;
1541 if (len > 0) {
1542 /* Skip blob object header first time around. */
1543 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1544 if (outlen == -1) {
1545 err = got_error_from_errno("write");
1546 goto done;
1547 } else if (outlen != len - hdrlen) {
1548 err = got_error(GOT_ERR_IO);
1549 goto done;
1551 hdrlen = 0;
1553 } while (len != 0);
1555 if (fsync(fd) != 0) {
1556 err = got_error_from_errno("fsync");
1557 goto done;
1560 if (update) {
1561 if (rename(tmppath, ondisk_path) != 0) {
1562 err = got_error_from_errno3("rename", tmppath,
1563 ondisk_path);
1564 goto done;
1566 free(tmppath);
1567 tmppath = NULL;
1570 done:
1571 if (fd != -1 && close(fd) != 0 && err == NULL)
1572 err = got_error_from_errno("close");
1573 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1574 err = got_error_from_errno2("unlink", tmppath);
1575 free(tmppath);
1576 return err;
1579 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1580 static const struct got_error *
1581 get_modified_file_content_status(unsigned char *status, FILE *f)
1583 const struct got_error *err = NULL;
1584 const char *markers[3] = {
1585 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1586 GOT_DIFF_CONFLICT_MARKER_SEP,
1587 GOT_DIFF_CONFLICT_MARKER_END
1589 int i = 0;
1590 char *line;
1591 size_t len;
1592 const char delim[3] = {'\0', '\0', '\0'};
1594 while (*status == GOT_STATUS_MODIFY) {
1595 line = fparseln(f, &len, NULL, delim, 0);
1596 if (line == NULL) {
1597 if (feof(f))
1598 break;
1599 err = got_ferror(f, GOT_ERR_IO);
1600 break;
1603 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1604 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1605 == 0)
1606 *status = GOT_STATUS_CONFLICT;
1607 else
1608 i++;
1612 return err;
1615 static int
1616 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1618 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1619 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1622 static int
1623 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1625 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1626 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1627 ie->mtime_sec == sb->st_mtim.tv_sec &&
1628 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1629 ie->size == (sb->st_size & 0xffffffff) &&
1630 !xbit_differs(ie, sb->st_mode));
1633 static unsigned char
1634 get_staged_status(struct got_fileindex_entry *ie)
1636 switch (got_fileindex_entry_stage_get(ie)) {
1637 case GOT_FILEIDX_STAGE_ADD:
1638 return GOT_STATUS_ADD;
1639 case GOT_FILEIDX_STAGE_DELETE:
1640 return GOT_STATUS_DELETE;
1641 case GOT_FILEIDX_STAGE_MODIFY:
1642 return GOT_STATUS_MODIFY;
1643 default:
1644 return GOT_STATUS_NO_CHANGE;
1648 static const struct got_error *
1649 get_symlink_modification_status(unsigned char *status,
1650 struct got_fileindex_entry *ie, const char *abspath,
1651 int dirfd, const char *de_name, struct got_blob_object *blob)
1653 const struct got_error *err = NULL;
1654 char target_path[PATH_MAX];
1655 char etarget[PATH_MAX];
1656 ssize_t elen;
1657 size_t len, target_len = 0;
1658 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1659 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1661 *status = GOT_STATUS_NO_CHANGE;
1663 /* Blob object content specifies the target path of the link. */
1664 do {
1665 err = got_object_blob_read_block(&len, blob);
1666 if (err)
1667 return err;
1668 if (len + target_len >= sizeof(target_path)) {
1670 * Should not happen. The blob contents were OK
1671 * when this symlink was installed.
1673 return got_error(GOT_ERR_NO_SPACE);
1675 if (len > 0) {
1676 /* Skip blob object header first time around. */
1677 memcpy(target_path + target_len, buf + hdrlen,
1678 len - hdrlen);
1679 target_len += len - hdrlen;
1680 hdrlen = 0;
1682 } while (len != 0);
1683 target_path[target_len] = '\0';
1685 if (dirfd != -1) {
1686 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1687 if (elen == -1)
1688 return got_error_from_errno2("readlinkat", abspath);
1689 } else {
1690 elen = readlink(abspath, etarget, sizeof(etarget));
1691 if (elen == -1)
1692 return got_error_from_errno2("readlink", abspath);
1695 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1696 *status = GOT_STATUS_MODIFY;
1698 return NULL;
1701 static const struct got_error *
1702 get_file_status(unsigned char *status, struct stat *sb,
1703 struct got_fileindex_entry *ie, const char *abspath,
1704 int dirfd, const char *de_name, struct got_repository *repo)
1706 const struct got_error *err = NULL;
1707 struct got_object_id id;
1708 size_t hdrlen;
1709 int fd = -1;
1710 FILE *f = NULL;
1711 uint8_t fbuf[8192];
1712 struct got_blob_object *blob = NULL;
1713 size_t flen, blen;
1714 unsigned char staged_status = get_staged_status(ie);
1716 *status = GOT_STATUS_NO_CHANGE;
1719 * Whenever the caller provides a directory descriptor and a
1720 * directory entry name for the file, use them! This prevents
1721 * race conditions if filesystem paths change beneath our feet.
1723 if (dirfd != -1) {
1724 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1725 if (errno == ENOENT) {
1726 if (got_fileindex_entry_has_file_on_disk(ie))
1727 *status = GOT_STATUS_MISSING;
1728 else
1729 *status = GOT_STATUS_DELETE;
1730 goto done;
1732 err = got_error_from_errno2("fstatat", abspath);
1733 goto done;
1735 } else {
1736 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1737 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1738 return got_error_from_errno2("open", abspath);
1739 else if (fd == -1 && errno == ELOOP) {
1740 if (lstat(abspath, sb) == -1)
1741 return got_error_from_errno2("lstat", abspath);
1742 } else if (fd == -1 || fstat(fd, sb) == -1) {
1743 if (errno == ENOENT) {
1744 if (got_fileindex_entry_has_file_on_disk(ie))
1745 *status = GOT_STATUS_MISSING;
1746 else
1747 *status = GOT_STATUS_DELETE;
1748 goto done;
1750 err = got_error_from_errno2("fstat", abspath);
1751 goto done;
1755 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1756 *status = GOT_STATUS_OBSTRUCTED;
1757 goto done;
1760 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1761 *status = GOT_STATUS_DELETE;
1762 goto done;
1763 } else if (!got_fileindex_entry_has_blob(ie) &&
1764 staged_status != GOT_STATUS_ADD) {
1765 *status = GOT_STATUS_ADD;
1766 goto done;
1769 if (!stat_info_differs(ie, sb))
1770 goto done;
1772 if (S_ISLNK(sb->st_mode) &&
1773 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1774 *status = GOT_STATUS_MODIFY;
1775 goto done;
1778 if (staged_status == GOT_STATUS_MODIFY ||
1779 staged_status == GOT_STATUS_ADD)
1780 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1781 else
1782 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1784 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1785 if (err)
1786 goto done;
1788 if (S_ISLNK(sb->st_mode)) {
1789 err = get_symlink_modification_status(status, ie,
1790 abspath, dirfd, de_name, blob);
1791 goto done;
1794 if (dirfd != -1) {
1795 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1796 if (fd == -1) {
1797 err = got_error_from_errno2("openat", abspath);
1798 goto done;
1802 f = fdopen(fd, "r");
1803 if (f == NULL) {
1804 err = got_error_from_errno2("fdopen", abspath);
1805 goto done;
1807 fd = -1;
1808 hdrlen = got_object_blob_get_hdrlen(blob);
1809 for (;;) {
1810 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1811 err = got_object_blob_read_block(&blen, blob);
1812 if (err)
1813 goto done;
1814 /* Skip length of blob object header first time around. */
1815 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1816 if (flen == 0 && ferror(f)) {
1817 err = got_error_from_errno("fread");
1818 goto done;
1820 if (blen - hdrlen == 0) {
1821 if (flen != 0)
1822 *status = GOT_STATUS_MODIFY;
1823 break;
1824 } else if (flen == 0) {
1825 if (blen - hdrlen != 0)
1826 *status = GOT_STATUS_MODIFY;
1827 break;
1828 } else if (blen - hdrlen == flen) {
1829 /* Skip blob object header first time around. */
1830 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1831 *status = GOT_STATUS_MODIFY;
1832 break;
1834 } else {
1835 *status = GOT_STATUS_MODIFY;
1836 break;
1838 hdrlen = 0;
1841 if (*status == GOT_STATUS_MODIFY) {
1842 rewind(f);
1843 err = get_modified_file_content_status(status, f);
1844 } else if (xbit_differs(ie, sb->st_mode))
1845 *status = GOT_STATUS_MODE_CHANGE;
1846 done:
1847 if (blob)
1848 got_object_blob_close(blob);
1849 if (f != NULL && fclose(f) == EOF && err == NULL)
1850 err = got_error_from_errno2("fclose", abspath);
1851 if (fd != -1 && close(fd) == -1 && err == NULL)
1852 err = got_error_from_errno2("close", abspath);
1853 return err;
1857 * Update timestamps in the file index if a file is unmodified and
1858 * we had to run a full content comparison to find out.
1860 static const struct got_error *
1861 sync_timestamps(char *ondisk_path, unsigned char status,
1862 struct got_fileindex_entry *ie, struct stat *sb)
1864 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1865 return got_fileindex_entry_update(ie, ondisk_path,
1866 ie->blob_sha1, ie->commit_sha1, 1);
1868 return NULL;
1871 static const struct got_error *
1872 update_blob(struct got_worktree *worktree,
1873 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1874 struct got_tree_entry *te, const char *path,
1875 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1876 void *progress_arg)
1878 const struct got_error *err = NULL;
1879 struct got_blob_object *blob = NULL;
1880 char *ondisk_path;
1881 unsigned char status = GOT_STATUS_NO_CHANGE;
1882 struct stat sb;
1884 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1885 return got_error_from_errno("asprintf");
1887 if (ie) {
1888 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1889 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1890 goto done;
1892 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1893 repo);
1894 if (err)
1895 goto done;
1896 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1897 sb.st_mode = got_fileindex_perms_to_st(ie);
1898 } else {
1899 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1900 status = GOT_STATUS_UNVERSIONED;
1903 if (status == GOT_STATUS_OBSTRUCTED) {
1904 err = (*progress_cb)(progress_arg, status, path);
1905 goto done;
1907 if (status == GOT_STATUS_CONFLICT) {
1908 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1909 path);
1910 goto done;
1913 if (ie && status != GOT_STATUS_MISSING &&
1914 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1915 if (got_fileindex_entry_has_commit(ie) &&
1916 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1917 SHA1_DIGEST_LENGTH) == 0) {
1918 err = sync_timestamps(ondisk_path, status, ie, &sb);
1919 if (err)
1920 goto done;
1921 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1922 path);
1923 goto done;
1925 if (got_fileindex_entry_has_blob(ie) &&
1926 memcmp(ie->blob_sha1, te->id.sha1,
1927 SHA1_DIGEST_LENGTH) == 0) {
1928 err = sync_timestamps(ondisk_path, status, ie, &sb);
1929 goto done;
1933 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1934 if (err)
1935 goto done;
1937 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1938 int update_timestamps;
1939 struct got_blob_object *blob2 = NULL;
1940 char *label_orig = NULL;
1941 if (got_fileindex_entry_has_blob(ie)) {
1942 struct got_object_id id2;
1943 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1944 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1945 if (err)
1946 goto done;
1948 if (got_fileindex_entry_has_commit(ie)) {
1949 char id_str[SHA1_DIGEST_STRING_LENGTH];
1950 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1951 sizeof(id_str)) == NULL) {
1952 err = got_error_path(id_str,
1953 GOT_ERR_BAD_OBJ_ID_STR);
1954 goto done;
1956 if (asprintf(&label_orig, "%s: commit %s",
1957 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1958 err = got_error_from_errno("asprintf");
1959 goto done;
1962 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1963 char *link_target;
1964 err = got_object_blob_read_to_str(&link_target, blob);
1965 if (err)
1966 goto done;
1967 err = merge_symlink(worktree, blob2, ondisk_path, path,
1968 label_orig, link_target, worktree->base_commit_id,
1969 repo, progress_cb, progress_arg);
1970 free(link_target);
1971 } else {
1972 err = merge_blob(&update_timestamps, worktree, blob2,
1973 ondisk_path, path, sb.st_mode, label_orig, blob,
1974 worktree->base_commit_id, repo,
1975 progress_cb, progress_arg);
1977 free(label_orig);
1978 if (blob2)
1979 got_object_blob_close(blob2);
1980 if (err)
1981 goto done;
1983 * Do not update timestamps of files with local changes.
1984 * Otherwise, a future status walk would treat them as
1985 * unmodified files again.
1987 err = got_fileindex_entry_update(ie, ondisk_path,
1988 blob->id.sha1, worktree->base_commit_id->sha1,
1989 update_timestamps);
1990 } else if (status == GOT_STATUS_MODE_CHANGE) {
1991 err = got_fileindex_entry_update(ie, ondisk_path,
1992 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1993 } else if (status == GOT_STATUS_DELETE) {
1994 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1995 if (err)
1996 goto done;
1997 err = got_fileindex_entry_update(ie, ondisk_path,
1998 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1999 if (err)
2000 goto done;
2001 } else {
2002 int is_bad_symlink = 0;
2003 if (S_ISLNK(te->mode)) {
2004 err = install_symlink(&is_bad_symlink, worktree,
2005 ondisk_path, path, blob,
2006 status == GOT_STATUS_MISSING, 0,
2007 status == GOT_STATUS_UNVERSIONED, repo,
2008 progress_cb, progress_arg);
2009 } else {
2010 err = install_blob(worktree, ondisk_path, path,
2011 te->mode, sb.st_mode, blob,
2012 status == GOT_STATUS_MISSING, 0, 0,
2013 status == GOT_STATUS_UNVERSIONED, repo,
2014 progress_cb, progress_arg);
2016 if (err)
2017 goto done;
2019 if (ie) {
2020 err = got_fileindex_entry_update(ie, ondisk_path,
2021 blob->id.sha1, worktree->base_commit_id->sha1, 1);
2022 } else {
2023 err = create_fileindex_entry(&ie, fileindex,
2024 worktree->base_commit_id, ondisk_path, path,
2025 &blob->id);
2027 if (err)
2028 goto done;
2030 if (is_bad_symlink) {
2031 got_fileindex_entry_filetype_set(ie,
2032 GOT_FILEIDX_MODE_BAD_SYMLINK);
2035 got_object_blob_close(blob);
2036 done:
2037 free(ondisk_path);
2038 return err;
2041 static const struct got_error *
2042 remove_ondisk_file(const char *root_path, const char *path)
2044 const struct got_error *err = NULL;
2045 char *ondisk_path = NULL;
2047 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2048 return got_error_from_errno("asprintf");
2050 if (unlink(ondisk_path) == -1) {
2051 if (errno != ENOENT)
2052 err = got_error_from_errno2("unlink", ondisk_path);
2053 } else {
2054 size_t root_len = strlen(root_path);
2055 do {
2056 char *parent;
2057 err = got_path_dirname(&parent, ondisk_path);
2058 if (err)
2059 break;
2060 free(ondisk_path);
2061 ondisk_path = parent;
2062 if (rmdir(ondisk_path) == -1) {
2063 if (errno != ENOTEMPTY)
2064 err = got_error_from_errno2("rmdir",
2065 ondisk_path);
2066 break;
2068 } while (got_path_cmp(ondisk_path, root_path,
2069 strlen(ondisk_path), root_len) != 0);
2071 free(ondisk_path);
2072 return err;
2075 static const struct got_error *
2076 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2077 struct got_fileindex_entry *ie, struct got_repository *repo,
2078 got_worktree_checkout_cb progress_cb, void *progress_arg)
2080 const struct got_error *err = NULL;
2081 unsigned char status;
2082 struct stat sb;
2083 char *ondisk_path;
2085 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2086 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2088 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2089 == -1)
2090 return got_error_from_errno("asprintf");
2092 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2093 if (err)
2094 goto done;
2096 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2097 char ondisk_target[PATH_MAX];
2098 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2099 sizeof(ondisk_target));
2100 if (ondisk_len == -1) {
2101 err = got_error_from_errno2("readlink", ondisk_path);
2102 goto done;
2104 ondisk_target[ondisk_len] = '\0';
2105 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2106 NULL, NULL, /* XXX pass common ancestor info? */
2107 ondisk_target, ondisk_path);
2108 if (err)
2109 goto done;
2110 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2111 ie->path);
2112 goto done;
2115 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2116 status == GOT_STATUS_ADD) {
2117 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2118 if (err)
2119 goto done;
2121 * Preserve the working file and change the deleted blob's
2122 * entry into a schedule-add entry.
2124 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2125 0);
2126 } else {
2127 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2128 if (err)
2129 goto done;
2130 if (status == GOT_STATUS_NO_CHANGE) {
2131 err = remove_ondisk_file(worktree->root_path, ie->path);
2132 if (err)
2133 goto done;
2135 got_fileindex_entry_remove(fileindex, ie);
2137 done:
2138 free(ondisk_path);
2139 return err;
2142 struct diff_cb_arg {
2143 struct got_fileindex *fileindex;
2144 struct got_worktree *worktree;
2145 struct got_repository *repo;
2146 got_worktree_checkout_cb progress_cb;
2147 void *progress_arg;
2148 got_cancel_cb cancel_cb;
2149 void *cancel_arg;
2152 static const struct got_error *
2153 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2154 struct got_tree_entry *te, const char *parent_path)
2156 struct diff_cb_arg *a = arg;
2158 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2159 return got_error(GOT_ERR_CANCELLED);
2161 return update_blob(a->worktree, a->fileindex, ie, te,
2162 ie->path, a->repo, a->progress_cb, a->progress_arg);
2165 static const struct got_error *
2166 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2168 struct diff_cb_arg *a = arg;
2170 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2171 return got_error(GOT_ERR_CANCELLED);
2173 return delete_blob(a->worktree, a->fileindex, ie,
2174 a->repo, a->progress_cb, a->progress_arg);
2177 static const struct got_error *
2178 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2180 struct diff_cb_arg *a = arg;
2181 const struct got_error *err;
2182 char *path;
2184 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2185 return got_error(GOT_ERR_CANCELLED);
2187 if (got_object_tree_entry_is_submodule(te))
2188 return NULL;
2190 if (asprintf(&path, "%s%s%s", parent_path,
2191 parent_path[0] ? "/" : "", te->name)
2192 == -1)
2193 return got_error_from_errno("asprintf");
2195 if (S_ISDIR(te->mode))
2196 err = add_dir_on_disk(a->worktree, path);
2197 else
2198 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2199 a->repo, a->progress_cb, a->progress_arg);
2201 free(path);
2202 return err;
2205 const struct got_error *
2206 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2208 uint32_t uuid_status;
2210 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2211 if (uuid_status != uuid_s_ok) {
2212 *uuidstr = NULL;
2213 return got_error_uuid(uuid_status, "uuid_to_string");
2216 return NULL;
2219 static const struct got_error *
2220 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2222 const struct got_error *err = NULL;
2223 char *uuidstr = NULL;
2225 *refname = NULL;
2227 err = got_worktree_get_uuid(&uuidstr, worktree);
2228 if (err)
2229 return err;
2231 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2232 err = got_error_from_errno("asprintf");
2233 *refname = NULL;
2235 free(uuidstr);
2236 return err;
2239 const struct got_error *
2240 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2242 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2245 static const struct got_error *
2246 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2248 return get_ref_name(refname, worktree,
2249 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2252 static const struct got_error *
2253 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2255 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2258 static const struct got_error *
2259 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2261 return get_ref_name(refname, worktree,
2262 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2265 static const struct got_error *
2266 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2268 return get_ref_name(refname, worktree,
2269 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2272 static const struct got_error *
2273 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2275 return get_ref_name(refname, worktree,
2276 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2279 static const struct got_error *
2280 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2282 return get_ref_name(refname, worktree,
2283 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2286 static const struct got_error *
2287 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2289 return get_ref_name(refname, worktree,
2290 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2293 static const struct got_error *
2294 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2296 return get_ref_name(refname, worktree,
2297 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2300 const struct got_error *
2301 got_worktree_get_histedit_script_path(char **path,
2302 struct got_worktree *worktree)
2304 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2305 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2306 *path = NULL;
2307 return got_error_from_errno("asprintf");
2309 return NULL;
2313 * Prevent Git's garbage collector from deleting our base commit by
2314 * setting a reference to our base commit's ID.
2316 static const struct got_error *
2317 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2319 const struct got_error *err = NULL;
2320 struct got_reference *ref = NULL;
2321 char *refname;
2323 err = got_worktree_get_base_ref_name(&refname, worktree);
2324 if (err)
2325 return err;
2327 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2328 if (err)
2329 goto done;
2331 err = got_ref_write(ref, repo);
2332 done:
2333 free(refname);
2334 if (ref)
2335 got_ref_close(ref);
2336 return err;
2339 static const struct got_error *
2340 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2342 const struct got_error *err = NULL;
2344 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2345 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2346 err = got_error_from_errno("asprintf");
2347 *fileindex_path = NULL;
2349 return err;
2353 static const struct got_error *
2354 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2355 struct got_worktree *worktree)
2357 const struct got_error *err = NULL;
2358 FILE *index = NULL;
2360 *fileindex_path = NULL;
2361 *fileindex = got_fileindex_alloc();
2362 if (*fileindex == NULL)
2363 return got_error_from_errno("got_fileindex_alloc");
2365 err = get_fileindex_path(fileindex_path, worktree);
2366 if (err)
2367 goto done;
2369 index = fopen(*fileindex_path, "rb");
2370 if (index == NULL) {
2371 if (errno != ENOENT)
2372 err = got_error_from_errno2("fopen", *fileindex_path);
2373 } else {
2374 err = got_fileindex_read(*fileindex, index);
2375 if (fclose(index) != 0 && err == NULL)
2376 err = got_error_from_errno("fclose");
2378 done:
2379 if (err) {
2380 free(*fileindex_path);
2381 *fileindex_path = NULL;
2382 got_fileindex_free(*fileindex);
2383 *fileindex = NULL;
2385 return err;
2388 struct bump_base_commit_id_arg {
2389 struct got_object_id *base_commit_id;
2390 const char *path;
2391 size_t path_len;
2392 const char *entry_name;
2393 got_worktree_checkout_cb progress_cb;
2394 void *progress_arg;
2397 /* Bump base commit ID of all files within an updated part of the work tree. */
2398 static const struct got_error *
2399 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2401 const struct got_error *err;
2402 struct bump_base_commit_id_arg *a = arg;
2404 if (a->entry_name) {
2405 if (strcmp(ie->path, a->path) != 0)
2406 return NULL;
2407 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2408 return NULL;
2410 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2411 SHA1_DIGEST_LENGTH) == 0)
2412 return NULL;
2414 if (a->progress_cb) {
2415 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2416 ie->path);
2417 if (err)
2418 return err;
2420 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2421 return NULL;
2424 static const struct got_error *
2425 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2427 const struct got_error *err = NULL;
2428 char *new_fileindex_path = NULL;
2429 FILE *new_index = NULL;
2430 struct timespec timeout;
2432 err = got_opentemp_named(&new_fileindex_path, &new_index,
2433 fileindex_path);
2434 if (err)
2435 goto done;
2437 err = got_fileindex_write(fileindex, new_index);
2438 if (err)
2439 goto done;
2441 if (rename(new_fileindex_path, fileindex_path) != 0) {
2442 err = got_error_from_errno3("rename", new_fileindex_path,
2443 fileindex_path);
2444 unlink(new_fileindex_path);
2448 * Sleep for a short amount of time to ensure that files modified after
2449 * this program exits have a different time stamp from the one which
2450 * was recorded in the file index.
2452 timeout.tv_sec = 0;
2453 timeout.tv_nsec = 1;
2454 nanosleep(&timeout, NULL);
2455 done:
2456 if (new_index)
2457 fclose(new_index);
2458 free(new_fileindex_path);
2459 return err;
2462 static const struct got_error *
2463 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2464 struct got_object_id **tree_id, const char *wt_relpath,
2465 struct got_worktree *worktree, struct got_repository *repo)
2467 const struct got_error *err = NULL;
2468 struct got_object_id *id = NULL;
2469 char *in_repo_path = NULL;
2470 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2472 *entry_type = GOT_OBJ_TYPE_ANY;
2473 *tree_relpath = NULL;
2474 *tree_id = NULL;
2476 if (wt_relpath[0] == '\0') {
2477 /* Check out all files within the work tree. */
2478 *entry_type = GOT_OBJ_TYPE_TREE;
2479 *tree_relpath = strdup("");
2480 if (*tree_relpath == NULL) {
2481 err = got_error_from_errno("strdup");
2482 goto done;
2484 err = got_object_id_by_path(tree_id, repo,
2485 worktree->base_commit_id, worktree->path_prefix);
2486 if (err)
2487 goto done;
2488 return NULL;
2491 /* Check out a subset of files in the work tree. */
2493 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2494 is_root_wt ? "" : "/", wt_relpath) == -1) {
2495 err = got_error_from_errno("asprintf");
2496 goto done;
2499 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2500 in_repo_path);
2501 if (err)
2502 goto done;
2504 free(in_repo_path);
2505 in_repo_path = NULL;
2507 err = got_object_get_type(entry_type, repo, id);
2508 if (err)
2509 goto done;
2511 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2512 /* Check out a single file. */
2513 if (strchr(wt_relpath, '/') == NULL) {
2514 /* Check out a single file in work tree's root dir. */
2515 in_repo_path = strdup(worktree->path_prefix);
2516 if (in_repo_path == NULL) {
2517 err = got_error_from_errno("strdup");
2518 goto done;
2520 *tree_relpath = strdup("");
2521 if (*tree_relpath == NULL) {
2522 err = got_error_from_errno("strdup");
2523 goto done;
2525 } else {
2526 /* Check out a single file in a subdirectory. */
2527 err = got_path_dirname(tree_relpath, wt_relpath);
2528 if (err)
2529 return err;
2530 if (asprintf(&in_repo_path, "%s%s%s",
2531 worktree->path_prefix, is_root_wt ? "" : "/",
2532 *tree_relpath) == -1) {
2533 err = got_error_from_errno("asprintf");
2534 goto done;
2537 err = got_object_id_by_path(tree_id, repo,
2538 worktree->base_commit_id, in_repo_path);
2539 } else {
2540 /* Check out all files within a subdirectory. */
2541 *tree_id = got_object_id_dup(id);
2542 if (*tree_id == NULL) {
2543 err = got_error_from_errno("got_object_id_dup");
2544 goto done;
2546 *tree_relpath = strdup(wt_relpath);
2547 if (*tree_relpath == NULL) {
2548 err = got_error_from_errno("strdup");
2549 goto done;
2552 done:
2553 free(id);
2554 free(in_repo_path);
2555 if (err) {
2556 *entry_type = GOT_OBJ_TYPE_ANY;
2557 free(*tree_relpath);
2558 *tree_relpath = NULL;
2559 free(*tree_id);
2560 *tree_id = NULL;
2562 return err;
2565 static const struct got_error *
2566 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2567 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2568 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2569 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2571 const struct got_error *err = NULL;
2572 struct got_commit_object *commit = NULL;
2573 struct got_tree_object *tree = NULL;
2574 struct got_fileindex_diff_tree_cb diff_cb;
2575 struct diff_cb_arg arg;
2577 err = ref_base_commit(worktree, repo);
2578 if (err) {
2579 if (!(err->code == GOT_ERR_ERRNO &&
2580 (errno == EACCES || errno == EROFS)))
2581 goto done;
2582 err = (*progress_cb)(progress_arg,
2583 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2584 if (err)
2585 return err;
2588 err = got_object_open_as_commit(&commit, repo,
2589 worktree->base_commit_id);
2590 if (err)
2591 goto done;
2593 err = got_object_open_as_tree(&tree, repo, tree_id);
2594 if (err)
2595 goto done;
2597 if (entry_name &&
2598 got_object_tree_find_entry(tree, entry_name) == NULL) {
2599 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2600 goto done;
2603 diff_cb.diff_old_new = diff_old_new;
2604 diff_cb.diff_old = diff_old;
2605 diff_cb.diff_new = diff_new;
2606 arg.fileindex = fileindex;
2607 arg.worktree = worktree;
2608 arg.repo = repo;
2609 arg.progress_cb = progress_cb;
2610 arg.progress_arg = progress_arg;
2611 arg.cancel_cb = cancel_cb;
2612 arg.cancel_arg = cancel_arg;
2613 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2614 entry_name, repo, &diff_cb, &arg);
2615 done:
2616 if (tree)
2617 got_object_tree_close(tree);
2618 if (commit)
2619 got_object_commit_close(commit);
2620 return err;
2623 const struct got_error *
2624 got_worktree_checkout_files(struct got_worktree *worktree,
2625 struct got_pathlist_head *paths, struct got_repository *repo,
2626 got_worktree_checkout_cb progress_cb, void *progress_arg,
2627 got_cancel_cb cancel_cb, void *cancel_arg)
2629 const struct got_error *err = NULL, *sync_err, *unlockerr;
2630 struct got_commit_object *commit = NULL;
2631 struct got_tree_object *tree = NULL;
2632 struct got_fileindex *fileindex = NULL;
2633 char *fileindex_path = NULL;
2634 struct got_pathlist_entry *pe;
2635 struct tree_path_data {
2636 SIMPLEQ_ENTRY(tree_path_data) entry;
2637 struct got_object_id *tree_id;
2638 int entry_type;
2639 char *relpath;
2640 char *entry_name;
2641 } *tpd = NULL;
2642 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2644 SIMPLEQ_INIT(&tree_paths);
2646 err = lock_worktree(worktree, LOCK_EX);
2647 if (err)
2648 return err;
2650 /* Map all specified paths to in-repository trees. */
2651 TAILQ_FOREACH(pe, paths, entry) {
2652 tpd = malloc(sizeof(*tpd));
2653 if (tpd == NULL) {
2654 err = got_error_from_errno("malloc");
2655 goto done;
2658 err = find_tree_entry_for_checkout(&tpd->entry_type,
2659 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2660 if (err) {
2661 free(tpd);
2662 goto done;
2665 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2666 err = got_path_basename(&tpd->entry_name, pe->path);
2667 if (err) {
2668 free(tpd->relpath);
2669 free(tpd->tree_id);
2670 free(tpd);
2671 goto done;
2673 } else
2674 tpd->entry_name = NULL;
2676 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2680 * Read the file index.
2681 * Checking out files is supposed to be an idempotent operation.
2682 * If the on-disk file index is incomplete we will try to complete it.
2684 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2685 if (err)
2686 goto done;
2688 tpd = SIMPLEQ_FIRST(&tree_paths);
2689 TAILQ_FOREACH(pe, paths, entry) {
2690 struct bump_base_commit_id_arg bbc_arg;
2692 err = checkout_files(worktree, fileindex, tpd->relpath,
2693 tpd->tree_id, tpd->entry_name, repo,
2694 progress_cb, progress_arg, cancel_cb, cancel_arg);
2695 if (err)
2696 break;
2698 bbc_arg.base_commit_id = worktree->base_commit_id;
2699 bbc_arg.entry_name = tpd->entry_name;
2700 bbc_arg.path = pe->path;
2701 bbc_arg.path_len = pe->path_len;
2702 bbc_arg.progress_cb = progress_cb;
2703 bbc_arg.progress_arg = progress_arg;
2704 err = got_fileindex_for_each_entry_safe(fileindex,
2705 bump_base_commit_id, &bbc_arg);
2706 if (err)
2707 break;
2709 tpd = SIMPLEQ_NEXT(tpd, entry);
2711 sync_err = sync_fileindex(fileindex, fileindex_path);
2712 if (sync_err && err == NULL)
2713 err = sync_err;
2714 done:
2715 free(fileindex_path);
2716 if (tree)
2717 got_object_tree_close(tree);
2718 if (commit)
2719 got_object_commit_close(commit);
2720 if (fileindex)
2721 got_fileindex_free(fileindex);
2722 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2723 tpd = SIMPLEQ_FIRST(&tree_paths);
2724 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2725 free(tpd->relpath);
2726 free(tpd->tree_id);
2727 free(tpd);
2729 unlockerr = lock_worktree(worktree, LOCK_SH);
2730 if (unlockerr && err == NULL)
2731 err = unlockerr;
2732 return err;
2735 struct merge_file_cb_arg {
2736 struct got_worktree *worktree;
2737 struct got_fileindex *fileindex;
2738 got_worktree_checkout_cb progress_cb;
2739 void *progress_arg;
2740 got_cancel_cb cancel_cb;
2741 void *cancel_arg;
2742 const char *label_orig;
2743 struct got_object_id *commit_id2;
2746 static const struct got_error *
2747 merge_file_cb(void *arg, struct got_blob_object *blob1,
2748 struct got_blob_object *blob2, struct got_object_id *id1,
2749 struct got_object_id *id2, const char *path1, const char *path2,
2750 mode_t mode1, mode_t mode2, struct got_repository *repo)
2752 static const struct got_error *err = NULL;
2753 struct merge_file_cb_arg *a = arg;
2754 struct got_fileindex_entry *ie;
2755 char *ondisk_path = NULL;
2756 struct stat sb;
2757 unsigned char status;
2758 int local_changes_subsumed;
2760 if (blob1 && blob2) {
2761 ie = got_fileindex_entry_get(a->fileindex, path2,
2762 strlen(path2));
2763 if (ie == NULL)
2764 return (*a->progress_cb)(a->progress_arg,
2765 GOT_STATUS_MISSING, path2);
2767 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2768 path2) == -1)
2769 return got_error_from_errno("asprintf");
2771 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2772 repo);
2773 if (err)
2774 goto done;
2776 if (status == GOT_STATUS_DELETE) {
2777 err = (*a->progress_cb)(a->progress_arg,
2778 GOT_STATUS_MERGE, path2);
2779 goto done;
2781 if (status != GOT_STATUS_NO_CHANGE &&
2782 status != GOT_STATUS_MODIFY &&
2783 status != GOT_STATUS_CONFLICT &&
2784 status != GOT_STATUS_ADD) {
2785 err = (*a->progress_cb)(a->progress_arg, status, path2);
2786 goto done;
2789 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2790 char *link_target2;
2791 err = got_object_blob_read_to_str(&link_target2, blob2);
2792 if (err)
2793 goto done;
2794 err = merge_symlink(a->worktree, blob1, ondisk_path,
2795 path2, a->label_orig, link_target2, a->commit_id2,
2796 repo, a->progress_cb, a->progress_arg);
2797 free(link_target2);
2798 } else {
2799 err = merge_blob(&local_changes_subsumed, a->worktree,
2800 blob1, ondisk_path, path2, sb.st_mode,
2801 a->label_orig, blob2, a->commit_id2, repo,
2802 a->progress_cb, a->progress_arg);
2804 } else if (blob1) {
2805 ie = got_fileindex_entry_get(a->fileindex, path1,
2806 strlen(path1));
2807 if (ie == NULL)
2808 return (*a->progress_cb)(a->progress_arg,
2809 GOT_STATUS_MISSING, path1);
2811 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2812 path1) == -1)
2813 return got_error_from_errno("asprintf");
2815 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2816 repo);
2817 if (err)
2818 goto done;
2820 switch (status) {
2821 case GOT_STATUS_NO_CHANGE:
2822 err = (*a->progress_cb)(a->progress_arg,
2823 GOT_STATUS_DELETE, path1);
2824 if (err)
2825 goto done;
2826 err = remove_ondisk_file(a->worktree->root_path, path1);
2827 if (err)
2828 goto done;
2829 if (ie)
2830 got_fileindex_entry_mark_deleted_from_disk(ie);
2831 break;
2832 case GOT_STATUS_DELETE:
2833 case GOT_STATUS_MISSING:
2834 err = (*a->progress_cb)(a->progress_arg,
2835 GOT_STATUS_DELETE, path1);
2836 if (err)
2837 goto done;
2838 if (ie)
2839 got_fileindex_entry_mark_deleted_from_disk(ie);
2840 break;
2841 case GOT_STATUS_ADD: {
2842 struct got_object_id *id;
2843 FILE *blob1_f;
2845 * Delete the added file only if its content already
2846 * exists in the repository.
2848 err = got_object_blob_file_create(&id, &blob1_f, path1);
2849 if (err)
2850 goto done;
2851 if (got_object_id_cmp(id, id1) == 0) {
2852 err = (*a->progress_cb)(a->progress_arg,
2853 GOT_STATUS_DELETE, path1);
2854 if (err)
2855 goto done;
2856 err = remove_ondisk_file(a->worktree->root_path,
2857 path1);
2858 if (err)
2859 goto done;
2860 if (ie)
2861 got_fileindex_entry_remove(a->fileindex,
2862 ie);
2863 } else {
2864 err = (*a->progress_cb)(a->progress_arg,
2865 GOT_STATUS_CANNOT_DELETE, path1);
2867 if (fclose(blob1_f) == EOF && err == NULL)
2868 err = got_error_from_errno("fclose");
2869 free(id);
2870 if (err)
2871 goto done;
2872 break;
2874 case GOT_STATUS_MODIFY:
2875 case GOT_STATUS_CONFLICT:
2876 err = (*a->progress_cb)(a->progress_arg,
2877 GOT_STATUS_CANNOT_DELETE, path1);
2878 if (err)
2879 goto done;
2880 break;
2881 case GOT_STATUS_OBSTRUCTED:
2882 err = (*a->progress_cb)(a->progress_arg, status, path1);
2883 if (err)
2884 goto done;
2885 break;
2886 default:
2887 break;
2889 } else if (blob2) {
2890 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2891 path2) == -1)
2892 return got_error_from_errno("asprintf");
2893 ie = got_fileindex_entry_get(a->fileindex, path2,
2894 strlen(path2));
2895 if (ie) {
2896 err = get_file_status(&status, &sb, ie, ondisk_path,
2897 -1, NULL, repo);
2898 if (err)
2899 goto done;
2900 if (status != GOT_STATUS_NO_CHANGE &&
2901 status != GOT_STATUS_MODIFY &&
2902 status != GOT_STATUS_CONFLICT &&
2903 status != GOT_STATUS_ADD) {
2904 err = (*a->progress_cb)(a->progress_arg,
2905 status, path2);
2906 goto done;
2908 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2909 char *link_target2;
2910 err = got_object_blob_read_to_str(&link_target2,
2911 blob2);
2912 if (err)
2913 goto done;
2914 err = merge_symlink(a->worktree, NULL,
2915 ondisk_path, path2, a->label_orig,
2916 link_target2, a->commit_id2, repo,
2917 a->progress_cb, a->progress_arg);
2918 free(link_target2);
2919 } else if (S_ISREG(sb.st_mode)) {
2920 err = merge_blob(&local_changes_subsumed,
2921 a->worktree, NULL, ondisk_path, path2,
2922 sb.st_mode, a->label_orig, blob2,
2923 a->commit_id2, repo, a->progress_cb,
2924 a->progress_arg);
2925 } else {
2926 err = got_error_path(ondisk_path,
2927 GOT_ERR_FILE_OBSTRUCTED);
2929 if (err)
2930 goto done;
2931 if (status == GOT_STATUS_DELETE) {
2932 err = got_fileindex_entry_update(ie,
2933 ondisk_path, blob2->id.sha1,
2934 a->worktree->base_commit_id->sha1, 0);
2935 if (err)
2936 goto done;
2938 } else {
2939 int is_bad_symlink = 0;
2940 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2941 if (S_ISLNK(mode2)) {
2942 err = install_symlink(&is_bad_symlink,
2943 a->worktree, ondisk_path, path2, blob2, 0,
2944 0, 1, repo, a->progress_cb, a->progress_arg);
2945 } else {
2946 err = install_blob(a->worktree, ondisk_path, path2,
2947 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2948 a->progress_cb, a->progress_arg);
2950 if (err)
2951 goto done;
2952 err = got_fileindex_entry_alloc(&ie, path2);
2953 if (err)
2954 goto done;
2955 err = got_fileindex_entry_update(ie, ondisk_path,
2956 NULL, NULL, 1);
2957 if (err) {
2958 got_fileindex_entry_free(ie);
2959 goto done;
2961 err = got_fileindex_entry_add(a->fileindex, ie);
2962 if (err) {
2963 got_fileindex_entry_free(ie);
2964 goto done;
2966 if (is_bad_symlink) {
2967 got_fileindex_entry_filetype_set(ie,
2968 GOT_FILEIDX_MODE_BAD_SYMLINK);
2972 done:
2973 free(ondisk_path);
2974 return err;
2977 struct check_merge_ok_arg {
2978 struct got_worktree *worktree;
2979 struct got_repository *repo;
2982 static const struct got_error *
2983 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2985 const struct got_error *err = NULL;
2986 struct check_merge_ok_arg *a = arg;
2987 unsigned char status;
2988 struct stat sb;
2989 char *ondisk_path;
2991 /* Reject merges into a work tree with mixed base commits. */
2992 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2993 SHA1_DIGEST_LENGTH))
2994 return got_error(GOT_ERR_MIXED_COMMITS);
2996 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2997 == -1)
2998 return got_error_from_errno("asprintf");
3000 /* Reject merges into a work tree with conflicted files. */
3001 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3002 if (err)
3003 return err;
3004 if (status == GOT_STATUS_CONFLICT)
3005 return got_error(GOT_ERR_CONFLICTS);
3007 return NULL;
3010 static const struct got_error *
3011 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3012 const char *fileindex_path, struct got_object_id *commit_id1,
3013 struct got_object_id *commit_id2, struct got_repository *repo,
3014 got_worktree_checkout_cb progress_cb, void *progress_arg,
3015 got_cancel_cb cancel_cb, void *cancel_arg)
3017 const struct got_error *err = NULL, *sync_err;
3018 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3019 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3020 struct merge_file_cb_arg arg;
3021 char *label_orig = NULL;
3023 if (commit_id1) {
3024 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3025 worktree->path_prefix);
3026 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3027 goto done;
3029 if (tree_id1) {
3030 char *id_str;
3032 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3033 if (err)
3034 goto done;
3036 err = got_object_id_str(&id_str, commit_id1);
3037 if (err)
3038 goto done;
3040 if (asprintf(&label_orig, "%s: commit %s",
3041 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3042 err = got_error_from_errno("asprintf");
3043 free(id_str);
3044 goto done;
3046 free(id_str);
3049 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3050 worktree->path_prefix);
3051 if (err)
3052 goto done;
3054 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3055 if (err)
3056 goto done;
3058 arg.worktree = worktree;
3059 arg.fileindex = fileindex;
3060 arg.progress_cb = progress_cb;
3061 arg.progress_arg = progress_arg;
3062 arg.cancel_cb = cancel_cb;
3063 arg.cancel_arg = cancel_arg;
3064 arg.label_orig = label_orig;
3065 arg.commit_id2 = commit_id2;
3066 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3067 sync_err = sync_fileindex(fileindex, fileindex_path);
3068 if (sync_err && err == NULL)
3069 err = sync_err;
3070 done:
3071 if (tree1)
3072 got_object_tree_close(tree1);
3073 if (tree2)
3074 got_object_tree_close(tree2);
3075 free(label_orig);
3076 return err;
3079 const struct got_error *
3080 got_worktree_merge_files(struct got_worktree *worktree,
3081 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3082 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3083 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3085 const struct got_error *err, *unlockerr;
3086 char *fileindex_path = NULL;
3087 struct got_fileindex *fileindex = NULL;
3088 struct check_merge_ok_arg mok_arg;
3090 err = lock_worktree(worktree, LOCK_EX);
3091 if (err)
3092 return err;
3094 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3095 if (err)
3096 goto done;
3098 mok_arg.worktree = worktree;
3099 mok_arg.repo = repo;
3100 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3101 &mok_arg);
3102 if (err)
3103 goto done;
3105 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3106 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3107 done:
3108 if (fileindex)
3109 got_fileindex_free(fileindex);
3110 free(fileindex_path);
3111 unlockerr = lock_worktree(worktree, LOCK_SH);
3112 if (unlockerr && err == NULL)
3113 err = unlockerr;
3114 return err;
3117 struct diff_dir_cb_arg {
3118 struct got_fileindex *fileindex;
3119 struct got_worktree *worktree;
3120 const char *status_path;
3121 size_t status_path_len;
3122 struct got_repository *repo;
3123 got_worktree_status_cb status_cb;
3124 void *status_arg;
3125 got_cancel_cb cancel_cb;
3126 void *cancel_arg;
3127 /* A pathlist containing per-directory pathlists of ignore patterns. */
3128 struct got_pathlist_head ignores;
3129 int report_unchanged;
3130 int no_ignores;
3133 static const struct got_error *
3134 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3135 int dirfd, const char *de_name,
3136 got_worktree_status_cb status_cb, void *status_arg,
3137 struct got_repository *repo, int report_unchanged)
3139 const struct got_error *err = NULL;
3140 unsigned char status = GOT_STATUS_NO_CHANGE;
3141 unsigned char staged_status = get_staged_status(ie);
3142 struct stat sb;
3143 struct got_object_id blob_id, commit_id, staged_blob_id;
3144 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3145 struct got_object_id *staged_blob_idp = NULL;
3147 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3148 if (err)
3149 return err;
3151 if (status == GOT_STATUS_NO_CHANGE &&
3152 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3153 return NULL;
3155 if (got_fileindex_entry_has_blob(ie)) {
3156 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3157 blob_idp = &blob_id;
3159 if (got_fileindex_entry_has_commit(ie)) {
3160 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3161 commit_idp = &commit_id;
3163 if (staged_status == GOT_STATUS_ADD ||
3164 staged_status == GOT_STATUS_MODIFY) {
3165 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3166 SHA1_DIGEST_LENGTH);
3167 staged_blob_idp = &staged_blob_id;
3170 return (*status_cb)(status_arg, status, staged_status,
3171 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3174 static const struct got_error *
3175 status_old_new(void *arg, struct got_fileindex_entry *ie,
3176 struct dirent *de, const char *parent_path, int dirfd)
3178 const struct got_error *err = NULL;
3179 struct diff_dir_cb_arg *a = arg;
3180 char *abspath;
3182 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3183 return got_error(GOT_ERR_CANCELLED);
3185 if (got_path_cmp(parent_path, a->status_path,
3186 strlen(parent_path), a->status_path_len) != 0 &&
3187 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3188 return NULL;
3190 if (parent_path[0]) {
3191 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3192 parent_path, de->d_name) == -1)
3193 return got_error_from_errno("asprintf");
3194 } else {
3195 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3196 de->d_name) == -1)
3197 return got_error_from_errno("asprintf");
3200 err = report_file_status(ie, abspath, dirfd, de->d_name,
3201 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3202 free(abspath);
3203 return err;
3206 static const struct got_error *
3207 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3209 struct diff_dir_cb_arg *a = arg;
3210 struct got_object_id blob_id, commit_id;
3211 unsigned char status;
3213 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3214 return got_error(GOT_ERR_CANCELLED);
3216 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3217 return NULL;
3219 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3220 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3221 if (got_fileindex_entry_has_file_on_disk(ie))
3222 status = GOT_STATUS_MISSING;
3223 else
3224 status = GOT_STATUS_DELETE;
3225 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3226 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3229 void
3230 free_ignorelist(struct got_pathlist_head *ignorelist)
3232 struct got_pathlist_entry *pe;
3234 TAILQ_FOREACH(pe, ignorelist, entry)
3235 free((char *)pe->path);
3236 got_pathlist_free(ignorelist);
3239 void
3240 free_ignores(struct got_pathlist_head *ignores)
3242 struct got_pathlist_entry *pe;
3244 TAILQ_FOREACH(pe, ignores, entry) {
3245 struct got_pathlist_head *ignorelist = pe->data;
3246 free_ignorelist(ignorelist);
3247 free((char *)pe->path);
3249 got_pathlist_free(ignores);
3252 static const struct got_error *
3253 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3255 const struct got_error *err = NULL;
3256 struct got_pathlist_entry *pe = NULL;
3257 struct got_pathlist_head *ignorelist;
3258 char *line = NULL, *pattern, *dirpath = NULL;
3259 size_t linesize = 0;
3260 ssize_t linelen;
3262 ignorelist = calloc(1, sizeof(*ignorelist));
3263 if (ignorelist == NULL)
3264 return got_error_from_errno("calloc");
3265 TAILQ_INIT(ignorelist);
3267 while ((linelen = getline(&line, &linesize, f)) != -1) {
3268 if (linelen > 0 && line[linelen - 1] == '\n')
3269 line[linelen - 1] = '\0';
3271 /* Git's ignores may contain comments. */
3272 if (line[0] == '#')
3273 continue;
3275 /* Git's negated patterns are not (yet?) supported. */
3276 if (line[0] == '!')
3277 continue;
3279 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3280 line) == -1) {
3281 err = got_error_from_errno("asprintf");
3282 goto done;
3284 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3285 if (err)
3286 goto done;
3288 if (ferror(f)) {
3289 err = got_error_from_errno("getline");
3290 goto done;
3293 dirpath = strdup(path);
3294 if (dirpath == NULL) {
3295 err = got_error_from_errno("strdup");
3296 goto done;
3298 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3299 done:
3300 free(line);
3301 if (err || pe == NULL) {
3302 free(dirpath);
3303 free_ignorelist(ignorelist);
3305 return err;
3308 int
3309 match_ignores(struct got_pathlist_head *ignores, const char *path)
3311 struct got_pathlist_entry *pe;
3313 /* Handle patterns which match in all directories. */
3314 TAILQ_FOREACH(pe, ignores, entry) {
3315 struct got_pathlist_head *ignorelist = pe->data;
3316 struct got_pathlist_entry *pi;
3318 TAILQ_FOREACH(pi, ignorelist, entry) {
3319 const char *p, *pattern = pi->path;
3321 if (strncmp(pattern, "**/", 3) != 0)
3322 continue;
3323 pattern += 3;
3324 p = path;
3325 while (*p) {
3326 if (fnmatch(pattern, p,
3327 FNM_PATHNAME | FNM_LEADING_DIR)) {
3328 /* Retry in next directory. */
3329 while (*p && *p != '/')
3330 p++;
3331 while (*p == '/')
3332 p++;
3333 continue;
3335 return 1;
3341 * The ignores pathlist contains ignore lists from children before
3342 * parents, so we can find the most specific ignorelist by walking
3343 * ignores backwards.
3345 pe = TAILQ_LAST(ignores, got_pathlist_head);
3346 while (pe) {
3347 if (got_path_is_child(path, pe->path, pe->path_len)) {
3348 struct got_pathlist_head *ignorelist = pe->data;
3349 struct got_pathlist_entry *pi;
3350 TAILQ_FOREACH(pi, ignorelist, entry) {
3351 const char *pattern = pi->path;
3352 int flags = FNM_LEADING_DIR;
3353 if (strstr(pattern, "/**/") == NULL)
3354 flags |= FNM_PATHNAME;
3355 if (fnmatch(pattern, path, flags))
3356 continue;
3357 return 1;
3360 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3363 return 0;
3366 static const struct got_error *
3367 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3368 const char *path, int dirfd, const char *ignores_filename)
3370 const struct got_error *err = NULL;
3371 char *ignorespath;
3372 int fd = -1;
3373 FILE *ignoresfile = NULL;
3375 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3376 path[0] ? "/" : "", ignores_filename) == -1)
3377 return got_error_from_errno("asprintf");
3379 if (dirfd != -1) {
3380 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3381 if (fd == -1) {
3382 if (errno != ENOENT && errno != EACCES)
3383 err = got_error_from_errno2("openat",
3384 ignorespath);
3385 } else {
3386 ignoresfile = fdopen(fd, "r");
3387 if (ignoresfile == NULL)
3388 err = got_error_from_errno2("fdopen",
3389 ignorespath);
3390 else {
3391 fd = -1;
3392 err = read_ignores(ignores, path, ignoresfile);
3395 } else {
3396 ignoresfile = fopen(ignorespath, "r");
3397 if (ignoresfile == NULL) {
3398 if (errno != ENOENT && errno != EACCES)
3399 err = got_error_from_errno2("fopen",
3400 ignorespath);
3401 } else
3402 err = read_ignores(ignores, path, ignoresfile);
3405 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3406 err = got_error_from_errno2("fclose", path);
3407 if (fd != -1 && close(fd) == -1 && err == NULL)
3408 err = got_error_from_errno2("close", path);
3409 free(ignorespath);
3410 return err;
3413 static const struct got_error *
3414 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3416 const struct got_error *err = NULL;
3417 struct diff_dir_cb_arg *a = arg;
3418 char *path = NULL;
3420 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3421 return got_error(GOT_ERR_CANCELLED);
3423 if (parent_path[0]) {
3424 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3425 return got_error_from_errno("asprintf");
3426 } else {
3427 path = de->d_name;
3430 if (de->d_type != DT_DIR &&
3431 got_path_is_child(path, a->status_path, a->status_path_len)
3432 && !match_ignores(&a->ignores, path))
3433 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3434 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3435 if (parent_path[0])
3436 free(path);
3437 return err;
3440 static const struct got_error *
3441 status_traverse(void *arg, const char *path, int dirfd)
3443 const struct got_error *err = NULL;
3444 struct diff_dir_cb_arg *a = arg;
3446 if (a->no_ignores)
3447 return NULL;
3449 err = add_ignores(&a->ignores, a->worktree->root_path,
3450 path, dirfd, ".cvsignore");
3451 if (err)
3452 return err;
3454 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3455 dirfd, ".gitignore");
3457 return err;
3460 static const struct got_error *
3461 report_single_file_status(const char *path, const char *ondisk_path,
3462 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3463 void *status_arg, struct got_repository *repo, int report_unchanged)
3465 struct got_fileindex_entry *ie;
3466 struct stat sb;
3468 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3469 if (ie)
3470 return report_file_status(ie, ondisk_path, -1, NULL,
3471 status_cb, status_arg, repo, report_unchanged);
3473 if (lstat(ondisk_path, &sb) == -1) {
3474 if (errno != ENOENT)
3475 return got_error_from_errno2("lstat", ondisk_path);
3476 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3477 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3478 return NULL;
3481 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3482 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3483 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3485 return NULL;
3488 static const struct got_error *
3489 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3490 const char *root_path, const char *path)
3492 const struct got_error *err;
3493 char *parent_path, *next_parent_path = NULL;
3495 err = add_ignores(ignores, root_path, "", -1,
3496 ".cvsignore");
3497 if (err)
3498 return err;
3500 err = add_ignores(ignores, root_path, "", -1,
3501 ".gitignore");
3502 if (err)
3503 return err;
3505 err = got_path_dirname(&parent_path, path);
3506 if (err) {
3507 if (err->code == GOT_ERR_BAD_PATH)
3508 return NULL; /* cannot traverse parent */
3509 return err;
3511 for (;;) {
3512 err = add_ignores(ignores, root_path, parent_path, -1,
3513 ".cvsignore");
3514 if (err)
3515 break;
3516 err = add_ignores(ignores, root_path, parent_path, -1,
3517 ".gitignore");
3518 if (err)
3519 break;
3520 err = got_path_dirname(&next_parent_path, parent_path);
3521 if (err) {
3522 if (err->code == GOT_ERR_BAD_PATH)
3523 err = NULL; /* traversed everything */
3524 break;
3526 free(parent_path);
3527 parent_path = next_parent_path;
3528 next_parent_path = NULL;
3531 free(parent_path);
3532 free(next_parent_path);
3533 return err;
3536 static const struct got_error *
3537 worktree_status(struct got_worktree *worktree, const char *path,
3538 struct got_fileindex *fileindex, struct got_repository *repo,
3539 got_worktree_status_cb status_cb, void *status_arg,
3540 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3541 int report_unchanged)
3543 const struct got_error *err = NULL;
3544 int fd = -1;
3545 struct got_fileindex_diff_dir_cb fdiff_cb;
3546 struct diff_dir_cb_arg arg;
3547 char *ondisk_path = NULL;
3549 TAILQ_INIT(&arg.ignores);
3551 if (asprintf(&ondisk_path, "%s%s%s",
3552 worktree->root_path, path[0] ? "/" : "", path) == -1)
3553 return got_error_from_errno("asprintf");
3555 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3556 if (fd == -1) {
3557 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3558 errno != ELOOP)
3559 err = got_error_from_errno2("open", ondisk_path);
3560 else
3561 err = report_single_file_status(path, ondisk_path,
3562 fileindex, status_cb, status_arg, repo,
3563 report_unchanged);
3564 } else {
3565 fdiff_cb.diff_old_new = status_old_new;
3566 fdiff_cb.diff_old = status_old;
3567 fdiff_cb.diff_new = status_new;
3568 fdiff_cb.diff_traverse = status_traverse;
3569 arg.fileindex = fileindex;
3570 arg.worktree = worktree;
3571 arg.status_path = path;
3572 arg.status_path_len = strlen(path);
3573 arg.repo = repo;
3574 arg.status_cb = status_cb;
3575 arg.status_arg = status_arg;
3576 arg.cancel_cb = cancel_cb;
3577 arg.cancel_arg = cancel_arg;
3578 arg.report_unchanged = report_unchanged;
3579 arg.no_ignores = no_ignores;
3580 if (!no_ignores) {
3581 err = add_ignores_from_parent_paths(&arg.ignores,
3582 worktree->root_path, path);
3583 if (err)
3584 goto done;
3586 err = got_fileindex_diff_dir(fileindex, fd,
3587 worktree->root_path, path, repo, &fdiff_cb, &arg);
3589 done:
3590 free_ignores(&arg.ignores);
3591 if (fd != -1 && close(fd) != 0 && err == NULL)
3592 err = got_error_from_errno("close");
3593 free(ondisk_path);
3594 return err;
3597 const struct got_error *
3598 got_worktree_status(struct got_worktree *worktree,
3599 struct got_pathlist_head *paths, struct got_repository *repo,
3600 got_worktree_status_cb status_cb, void *status_arg,
3601 got_cancel_cb cancel_cb, void *cancel_arg)
3603 const struct got_error *err = NULL;
3604 char *fileindex_path = NULL;
3605 struct got_fileindex *fileindex = NULL;
3606 struct got_pathlist_entry *pe;
3608 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3609 if (err)
3610 return err;
3612 TAILQ_FOREACH(pe, paths, entry) {
3613 err = worktree_status(worktree, pe->path, fileindex, repo,
3614 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3615 if (err)
3616 break;
3618 free(fileindex_path);
3619 got_fileindex_free(fileindex);
3620 return err;
3623 const struct got_error *
3624 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3625 const char *arg)
3627 const struct got_error *err = NULL;
3628 char *resolved = NULL, *cwd = NULL, *path = NULL;
3629 size_t len;
3630 struct stat sb;
3631 char *abspath = NULL;
3632 char canonpath[PATH_MAX];
3634 *wt_path = NULL;
3636 cwd = getcwd(NULL, 0);
3637 if (cwd == NULL)
3638 return got_error_from_errno("getcwd");
3640 if (lstat(arg, &sb) == -1) {
3641 if (errno != ENOENT) {
3642 err = got_error_from_errno2("lstat", arg);
3643 goto done;
3645 sb.st_mode = 0;
3647 if (S_ISLNK(sb.st_mode)) {
3649 * We cannot use realpath(3) with symlinks since we want to
3650 * operate on the symlink itself.
3651 * But we can make the path absolute, assuming it is relative
3652 * to the current working directory, and then canonicalize it.
3654 if (!got_path_is_absolute(arg)) {
3655 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3656 err = got_error_from_errno("asprintf");
3657 goto done;
3661 err = got_canonpath(abspath ? abspath : arg, canonpath,
3662 sizeof(canonpath));
3663 if (err)
3664 goto done;
3665 resolved = strdup(canonpath);
3666 if (resolved == NULL) {
3667 err = got_error_from_errno("strdup");
3668 goto done;
3670 } else {
3671 resolved = realpath(arg, NULL);
3672 if (resolved == NULL) {
3673 if (errno != ENOENT) {
3674 err = got_error_from_errno2("realpath", arg);
3675 goto done;
3677 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3678 err = got_error_from_errno("asprintf");
3679 goto done;
3681 err = got_canonpath(abspath, canonpath,
3682 sizeof(canonpath));
3683 if (err)
3684 goto done;
3685 resolved = strdup(canonpath);
3686 if (resolved == NULL) {
3687 err = got_error_from_errno("strdup");
3688 goto done;
3693 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3694 strlen(got_worktree_get_root_path(worktree)))) {
3695 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3696 goto done;
3699 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3700 err = got_path_skip_common_ancestor(&path,
3701 got_worktree_get_root_path(worktree), resolved);
3702 if (err)
3703 goto done;
3704 } else {
3705 path = strdup("");
3706 if (path == NULL) {
3707 err = got_error_from_errno("strdup");
3708 goto done;
3712 /* XXX status walk can't deal with trailing slash! */
3713 len = strlen(path);
3714 while (len > 0 && path[len - 1] == '/') {
3715 path[len - 1] = '\0';
3716 len--;
3718 done:
3719 free(abspath);
3720 free(resolved);
3721 free(cwd);
3722 if (err == NULL)
3723 *wt_path = path;
3724 else
3725 free(path);
3726 return err;
3729 struct schedule_addition_args {
3730 struct got_worktree *worktree;
3731 struct got_fileindex *fileindex;
3732 got_worktree_checkout_cb progress_cb;
3733 void *progress_arg;
3734 struct got_repository *repo;
3737 static const struct got_error *
3738 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3739 const char *relpath, struct got_object_id *blob_id,
3740 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3741 int dirfd, const char *de_name)
3743 struct schedule_addition_args *a = arg;
3744 const struct got_error *err = NULL;
3745 struct got_fileindex_entry *ie;
3746 struct stat sb;
3747 char *ondisk_path;
3749 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3750 relpath) == -1)
3751 return got_error_from_errno("asprintf");
3753 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3754 if (ie) {
3755 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3756 de_name, a->repo);
3757 if (err)
3758 goto done;
3759 /* Re-adding an existing entry is a no-op. */
3760 if (status == GOT_STATUS_ADD)
3761 goto done;
3762 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3763 if (err)
3764 goto done;
3767 if (status != GOT_STATUS_UNVERSIONED) {
3768 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3769 goto done;
3772 err = got_fileindex_entry_alloc(&ie, relpath);
3773 if (err)
3774 goto done;
3775 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3776 if (err) {
3777 got_fileindex_entry_free(ie);
3778 goto done;
3780 err = got_fileindex_entry_add(a->fileindex, ie);
3781 if (err) {
3782 got_fileindex_entry_free(ie);
3783 goto done;
3785 done:
3786 free(ondisk_path);
3787 if (err)
3788 return err;
3789 if (status == GOT_STATUS_ADD)
3790 return NULL;
3791 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3794 const struct got_error *
3795 got_worktree_schedule_add(struct got_worktree *worktree,
3796 struct got_pathlist_head *paths,
3797 got_worktree_checkout_cb progress_cb, void *progress_arg,
3798 struct got_repository *repo, int no_ignores)
3800 struct got_fileindex *fileindex = NULL;
3801 char *fileindex_path = NULL;
3802 const struct got_error *err = NULL, *sync_err, *unlockerr;
3803 struct got_pathlist_entry *pe;
3804 struct schedule_addition_args saa;
3806 err = lock_worktree(worktree, LOCK_EX);
3807 if (err)
3808 return err;
3810 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3811 if (err)
3812 goto done;
3814 saa.worktree = worktree;
3815 saa.fileindex = fileindex;
3816 saa.progress_cb = progress_cb;
3817 saa.progress_arg = progress_arg;
3818 saa.repo = repo;
3820 TAILQ_FOREACH(pe, paths, entry) {
3821 err = worktree_status(worktree, pe->path, fileindex, repo,
3822 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3823 if (err)
3824 break;
3826 sync_err = sync_fileindex(fileindex, fileindex_path);
3827 if (sync_err && err == NULL)
3828 err = sync_err;
3829 done:
3830 free(fileindex_path);
3831 if (fileindex)
3832 got_fileindex_free(fileindex);
3833 unlockerr = lock_worktree(worktree, LOCK_SH);
3834 if (unlockerr && err == NULL)
3835 err = unlockerr;
3836 return err;
3839 struct schedule_deletion_args {
3840 struct got_worktree *worktree;
3841 struct got_fileindex *fileindex;
3842 got_worktree_delete_cb progress_cb;
3843 void *progress_arg;
3844 struct got_repository *repo;
3845 int delete_local_mods;
3846 int keep_on_disk;
3847 const char *status_codes;
3850 static const struct got_error *
3851 schedule_for_deletion(void *arg, unsigned char status,
3852 unsigned char staged_status, const char *relpath,
3853 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3854 struct got_object_id *commit_id, int dirfd, const char *de_name)
3856 struct schedule_deletion_args *a = arg;
3857 const struct got_error *err = NULL;
3858 struct got_fileindex_entry *ie = NULL;
3859 struct stat sb;
3860 char *ondisk_path;
3862 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3863 if (ie == NULL)
3864 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3866 staged_status = get_staged_status(ie);
3867 if (staged_status != GOT_STATUS_NO_CHANGE) {
3868 if (staged_status == GOT_STATUS_DELETE)
3869 return NULL;
3870 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3873 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3874 relpath) == -1)
3875 return got_error_from_errno("asprintf");
3877 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3878 a->repo);
3879 if (err)
3880 goto done;
3882 if (a->status_codes) {
3883 size_t ncodes = strlen(a->status_codes);
3884 int i;
3885 for (i = 0; i < ncodes ; i++) {
3886 if (status == a->status_codes[i])
3887 break;
3889 if (i == ncodes) {
3890 /* Do not delete files in non-matching status. */
3891 free(ondisk_path);
3892 return NULL;
3894 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3895 a->status_codes[i] != GOT_STATUS_MISSING) {
3896 static char msg[64];
3897 snprintf(msg, sizeof(msg),
3898 "invalid status code '%c'", a->status_codes[i]);
3899 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3900 goto done;
3904 if (status != GOT_STATUS_NO_CHANGE) {
3905 if (status == GOT_STATUS_DELETE)
3906 goto done;
3907 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3908 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3909 goto done;
3911 if (status != GOT_STATUS_MODIFY &&
3912 status != GOT_STATUS_MISSING) {
3913 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3914 goto done;
3918 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3919 size_t root_len;
3921 if (dirfd != -1) {
3922 if (unlinkat(dirfd, de_name, 0) != 0) {
3923 err = got_error_from_errno2("unlinkat",
3924 ondisk_path);
3925 goto done;
3927 } else if (unlink(ondisk_path) != 0) {
3928 err = got_error_from_errno2("unlink", ondisk_path);
3929 goto done;
3932 root_len = strlen(a->worktree->root_path);
3933 do {
3934 char *parent;
3935 err = got_path_dirname(&parent, ondisk_path);
3936 if (err)
3937 goto done;
3938 free(ondisk_path);
3939 ondisk_path = parent;
3940 if (rmdir(ondisk_path) == -1) {
3941 if (errno != ENOTEMPTY)
3942 err = got_error_from_errno2("rmdir",
3943 ondisk_path);
3944 break;
3946 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3947 strlen(ondisk_path), root_len) != 0);
3950 got_fileindex_entry_mark_deleted_from_disk(ie);
3951 done:
3952 free(ondisk_path);
3953 if (err)
3954 return err;
3955 if (status == GOT_STATUS_DELETE)
3956 return NULL;
3957 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3958 staged_status, relpath);
3961 const struct got_error *
3962 got_worktree_schedule_delete(struct got_worktree *worktree,
3963 struct got_pathlist_head *paths, int delete_local_mods,
3964 const char *status_codes,
3965 got_worktree_delete_cb progress_cb, void *progress_arg,
3966 struct got_repository *repo, int keep_on_disk)
3968 struct got_fileindex *fileindex = NULL;
3969 char *fileindex_path = NULL;
3970 const struct got_error *err = NULL, *sync_err, *unlockerr;
3971 struct got_pathlist_entry *pe;
3972 struct schedule_deletion_args sda;
3974 err = lock_worktree(worktree, LOCK_EX);
3975 if (err)
3976 return err;
3978 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3979 if (err)
3980 goto done;
3982 sda.worktree = worktree;
3983 sda.fileindex = fileindex;
3984 sda.progress_cb = progress_cb;
3985 sda.progress_arg = progress_arg;
3986 sda.repo = repo;
3987 sda.delete_local_mods = delete_local_mods;
3988 sda.keep_on_disk = keep_on_disk;
3989 sda.status_codes = status_codes;
3991 TAILQ_FOREACH(pe, paths, entry) {
3992 err = worktree_status(worktree, pe->path, fileindex, repo,
3993 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3994 if (err)
3995 break;
3997 sync_err = sync_fileindex(fileindex, fileindex_path);
3998 if (sync_err && err == NULL)
3999 err = sync_err;
4000 done:
4001 free(fileindex_path);
4002 if (fileindex)
4003 got_fileindex_free(fileindex);
4004 unlockerr = lock_worktree(worktree, LOCK_SH);
4005 if (unlockerr && err == NULL)
4006 err = unlockerr;
4007 return err;
4010 static const struct got_error *
4011 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4013 const struct got_error *err = NULL;
4014 char *line = NULL;
4015 size_t linesize = 0, n;
4016 ssize_t linelen;
4018 linelen = getline(&line, &linesize, infile);
4019 if (linelen == -1) {
4020 if (ferror(infile)) {
4021 err = got_error_from_errno("getline");
4022 goto done;
4024 return NULL;
4026 if (outfile) {
4027 n = fwrite(line, 1, linelen, outfile);
4028 if (n != linelen) {
4029 err = got_ferror(outfile, GOT_ERR_IO);
4030 goto done;
4033 if (rejectfile) {
4034 n = fwrite(line, 1, linelen, rejectfile);
4035 if (n != linelen)
4036 err = got_ferror(outfile, GOT_ERR_IO);
4038 done:
4039 free(line);
4040 return err;
4043 static const struct got_error *
4044 skip_one_line(FILE *f)
4046 char *line = NULL;
4047 size_t linesize = 0;
4048 ssize_t linelen;
4050 linelen = getline(&line, &linesize, f);
4051 if (linelen == -1) {
4052 if (ferror(f))
4053 return got_error_from_errno("getline");
4054 return NULL;
4056 free(line);
4057 return NULL;
4060 static const struct got_error *
4061 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4062 int start_old, int end_old, int start_new, int end_new,
4063 FILE *outfile, FILE *rejectfile)
4065 const struct got_error *err;
4067 /* Copy old file's lines leading up to patch. */
4068 while (!feof(f1) && *line_cur1 < start_old) {
4069 err = copy_one_line(f1, outfile, NULL);
4070 if (err)
4071 return err;
4072 (*line_cur1)++;
4074 /* Skip new file's lines leading up to patch. */
4075 while (!feof(f2) && *line_cur2 < start_new) {
4076 if (rejectfile)
4077 err = copy_one_line(f2, NULL, rejectfile);
4078 else
4079 err = skip_one_line(f2);
4080 if (err)
4081 return err;
4082 (*line_cur2)++;
4084 /* Copy patched lines. */
4085 while (!feof(f2) && *line_cur2 <= end_new) {
4086 err = copy_one_line(f2, outfile, NULL);
4087 if (err)
4088 return err;
4089 (*line_cur2)++;
4091 /* Skip over old file's replaced lines. */
4092 while (!feof(f1) && *line_cur1 <= end_old) {
4093 if (rejectfile)
4094 err = copy_one_line(f1, NULL, rejectfile);
4095 else
4096 err = skip_one_line(f1);
4097 if (err)
4098 return err;
4099 (*line_cur1)++;
4102 return NULL;
4105 static const struct got_error *
4106 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4107 FILE *outfile, FILE *rejectfile)
4109 const struct got_error *err;
4111 if (outfile) {
4112 /* Copy old file's lines until EOF. */
4113 while (!feof(f1)) {
4114 err = copy_one_line(f1, outfile, NULL);
4115 if (err)
4116 return err;
4117 (*line_cur1)++;
4120 if (rejectfile) {
4121 /* Copy new file's lines until EOF. */
4122 while (!feof(f2)) {
4123 err = copy_one_line(f2, NULL, rejectfile);
4124 if (err)
4125 return err;
4126 (*line_cur2)++;
4130 return NULL;
4133 static const struct got_error *
4134 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4135 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4136 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4137 int *line_cur2, FILE *outfile, FILE *rejectfile,
4138 got_worktree_patch_cb patch_cb, void *patch_arg)
4140 const struct got_error *err = NULL;
4141 int start_old = change->cv.a;
4142 int end_old = change->cv.b;
4143 int start_new = change->cv.c;
4144 int end_new = change->cv.d;
4145 long pos1, pos2;
4146 FILE *hunkfile;
4148 *choice = GOT_PATCH_CHOICE_NONE;
4150 hunkfile = got_opentemp();
4151 if (hunkfile == NULL)
4152 return got_error_from_errno("got_opentemp");
4154 pos1 = ftell(f1);
4155 pos2 = ftell(f2);
4157 /* XXX TODO needs error checking */
4158 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4160 if (fseek(f1, pos1, SEEK_SET) == -1) {
4161 err = got_ferror(f1, GOT_ERR_IO);
4162 goto done;
4164 if (fseek(f2, pos2, SEEK_SET) == -1) {
4165 err = got_ferror(f1, GOT_ERR_IO);
4166 goto done;
4168 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4169 err = got_ferror(hunkfile, GOT_ERR_IO);
4170 goto done;
4173 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4174 hunkfile, n, nchanges);
4175 if (err)
4176 goto done;
4178 switch (*choice) {
4179 case GOT_PATCH_CHOICE_YES:
4180 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4181 end_old, start_new, end_new, outfile, rejectfile);
4182 break;
4183 case GOT_PATCH_CHOICE_NO:
4184 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4185 end_old, start_new, end_new, rejectfile, outfile);
4186 break;
4187 case GOT_PATCH_CHOICE_QUIT:
4188 break;
4189 default:
4190 err = got_error(GOT_ERR_PATCH_CHOICE);
4191 break;
4193 done:
4194 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4195 err = got_error_from_errno("fclose");
4196 return err;
4199 struct revert_file_args {
4200 struct got_worktree *worktree;
4201 struct got_fileindex *fileindex;
4202 got_worktree_checkout_cb progress_cb;
4203 void *progress_arg;
4204 got_worktree_patch_cb patch_cb;
4205 void *patch_arg;
4206 struct got_repository *repo;
4209 static const struct got_error *
4210 create_patched_content(char **path_outfile, int reverse_patch,
4211 struct got_object_id *blob_id, const char *path2,
4212 int dirfd2, const char *de_name2,
4213 const char *relpath, struct got_repository *repo,
4214 got_worktree_patch_cb patch_cb, void *patch_arg)
4216 const struct got_error *err;
4217 struct got_blob_object *blob = NULL;
4218 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4219 int fd2 = -1;
4220 char link_target[PATH_MAX];
4221 ssize_t link_len = 0;
4222 char *path1 = NULL, *id_str = NULL;
4223 struct stat sb1, sb2;
4224 struct got_diff_changes *changes = NULL;
4225 struct got_diff_state *ds = NULL;
4226 struct got_diff_args *args = NULL;
4227 struct got_diff_change *change;
4228 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4229 int n = 0;
4231 *path_outfile = NULL;
4233 err = got_object_id_str(&id_str, blob_id);
4234 if (err)
4235 return err;
4237 if (dirfd2 != -1) {
4238 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4239 if (fd2 == -1) {
4240 if (errno != ELOOP) {
4241 err = got_error_from_errno2("openat", path2);
4242 goto done;
4244 link_len = readlinkat(dirfd2, de_name2,
4245 link_target, sizeof(link_target));
4246 if (link_len == -1)
4247 return got_error_from_errno2("readlinkat", path2);
4248 sb2.st_mode = S_IFLNK;
4249 sb2.st_size = link_len;
4251 } else {
4252 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4253 if (fd2 == -1) {
4254 if (errno != ELOOP) {
4255 err = got_error_from_errno2("open", path2);
4256 goto done;
4258 link_len = readlink(path2, link_target,
4259 sizeof(link_target));
4260 if (link_len == -1)
4261 return got_error_from_errno2("readlink", path2);
4262 sb2.st_mode = S_IFLNK;
4263 sb2.st_size = link_len;
4266 if (fd2 != -1) {
4267 if (fstat(fd2, &sb2) == -1) {
4268 err = got_error_from_errno2("fstat", path2);
4269 goto done;
4272 f2 = fdopen(fd2, "r");
4273 if (f2 == NULL) {
4274 err = got_error_from_errno2("fdopen", path2);
4275 goto done;
4277 fd2 = -1;
4278 } else {
4279 size_t n;
4280 f2 = got_opentemp();
4281 if (f2 == NULL) {
4282 err = got_error_from_errno2("got_opentemp", path2);
4283 goto done;
4285 n = fwrite(link_target, 1, link_len, f2);
4286 if (n != link_len) {
4287 err = got_ferror(f2, GOT_ERR_IO);
4288 goto done;
4290 if (fflush(f2) == EOF) {
4291 err = got_error_from_errno("fflush");
4292 goto done;
4294 rewind(f2);
4297 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4298 if (err)
4299 goto done;
4301 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4302 if (err)
4303 goto done;
4305 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4306 if (err)
4307 goto done;
4309 if (stat(path1, &sb1) == -1) {
4310 err = got_error_from_errno2("stat", path1);
4311 goto done;
4314 err = got_diff_files(&changes, &ds, &args, &diff_flags,
4315 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4316 if (err)
4317 goto done;
4319 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4320 if (err)
4321 goto done;
4323 if (fseek(f1, 0L, SEEK_SET) == -1)
4324 return got_ferror(f1, GOT_ERR_IO);
4325 if (fseek(f2, 0L, SEEK_SET) == -1)
4326 return got_ferror(f2, GOT_ERR_IO);
4327 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4328 int choice;
4329 err = apply_or_reject_change(&choice, change, ++n,
4330 changes->nchanges, ds, args, diff_flags, relpath,
4331 f1, f2, &line_cur1, &line_cur2,
4332 reverse_patch ? NULL : outfile,
4333 reverse_patch ? outfile : NULL,
4334 patch_cb, patch_arg);
4335 if (err)
4336 goto done;
4337 if (choice == GOT_PATCH_CHOICE_YES)
4338 have_content = 1;
4339 else if (choice == GOT_PATCH_CHOICE_QUIT)
4340 break;
4342 if (have_content) {
4343 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4344 reverse_patch ? NULL : outfile,
4345 reverse_patch ? outfile : NULL);
4346 if (err)
4347 goto done;
4349 if (!S_ISLNK(sb2.st_mode)) {
4350 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4351 err = got_error_from_errno2("fchmod", path2);
4352 goto done;
4356 done:
4357 free(id_str);
4358 if (blob)
4359 got_object_blob_close(blob);
4360 if (f1 && fclose(f1) == EOF && err == NULL)
4361 err = got_error_from_errno2("fclose", path1);
4362 if (f2 && fclose(f2) == EOF && err == NULL)
4363 err = got_error_from_errno2("fclose", path2);
4364 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4365 err = got_error_from_errno2("close", path2);
4366 if (outfile && fclose(outfile) == EOF && err == NULL)
4367 err = got_error_from_errno2("fclose", *path_outfile);
4368 if (path1 && unlink(path1) == -1 && err == NULL)
4369 err = got_error_from_errno2("unlink", path1);
4370 if (err || !have_content) {
4371 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4372 err = got_error_from_errno2("unlink", *path_outfile);
4373 free(*path_outfile);
4374 *path_outfile = NULL;
4376 free(args);
4377 if (ds) {
4378 got_diff_state_free(ds);
4379 free(ds);
4381 if (changes)
4382 got_diff_free_changes(changes);
4383 free(path1);
4384 return err;
4387 static const struct got_error *
4388 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4389 const char *relpath, struct got_object_id *blob_id,
4390 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4391 int dirfd, const char *de_name)
4393 struct revert_file_args *a = arg;
4394 const struct got_error *err = NULL;
4395 char *parent_path = NULL;
4396 struct got_fileindex_entry *ie;
4397 struct got_tree_object *tree = NULL;
4398 struct got_object_id *tree_id = NULL;
4399 const struct got_tree_entry *te = NULL;
4400 char *tree_path = NULL, *te_name;
4401 char *ondisk_path = NULL, *path_content = NULL;
4402 struct got_blob_object *blob = NULL;
4404 /* Reverting a staged deletion is a no-op. */
4405 if (status == GOT_STATUS_DELETE &&
4406 staged_status != GOT_STATUS_NO_CHANGE)
4407 return NULL;
4409 if (status == GOT_STATUS_UNVERSIONED)
4410 return (*a->progress_cb)(a->progress_arg,
4411 GOT_STATUS_UNVERSIONED, relpath);
4413 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4414 if (ie == NULL)
4415 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4417 /* Construct in-repository path of tree which contains this blob. */
4418 err = got_path_dirname(&parent_path, ie->path);
4419 if (err) {
4420 if (err->code != GOT_ERR_BAD_PATH)
4421 goto done;
4422 parent_path = strdup("/");
4423 if (parent_path == NULL) {
4424 err = got_error_from_errno("strdup");
4425 goto done;
4428 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4429 tree_path = strdup(parent_path);
4430 if (tree_path == NULL) {
4431 err = got_error_from_errno("strdup");
4432 goto done;
4434 } else {
4435 if (got_path_is_root_dir(parent_path)) {
4436 tree_path = strdup(a->worktree->path_prefix);
4437 if (tree_path == NULL) {
4438 err = got_error_from_errno("strdup");
4439 goto done;
4441 } else {
4442 if (asprintf(&tree_path, "%s/%s",
4443 a->worktree->path_prefix, parent_path) == -1) {
4444 err = got_error_from_errno("asprintf");
4445 goto done;
4450 err = got_object_id_by_path(&tree_id, a->repo,
4451 a->worktree->base_commit_id, tree_path);
4452 if (err) {
4453 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4454 (status == GOT_STATUS_ADD ||
4455 staged_status == GOT_STATUS_ADD)))
4456 goto done;
4457 } else {
4458 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4459 if (err)
4460 goto done;
4462 err = got_path_basename(&te_name, ie->path);
4463 if (err)
4464 goto done;
4466 te = got_object_tree_find_entry(tree, te_name);
4467 free(te_name);
4468 if (te == NULL && status != GOT_STATUS_ADD &&
4469 staged_status != GOT_STATUS_ADD) {
4470 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4471 goto done;
4475 switch (status) {
4476 case GOT_STATUS_ADD:
4477 if (a->patch_cb) {
4478 int choice = GOT_PATCH_CHOICE_NONE;
4479 err = (*a->patch_cb)(&choice, a->patch_arg,
4480 status, ie->path, NULL, 1, 1);
4481 if (err)
4482 goto done;
4483 if (choice != GOT_PATCH_CHOICE_YES)
4484 break;
4486 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4487 ie->path);
4488 if (err)
4489 goto done;
4490 got_fileindex_entry_remove(a->fileindex, ie);
4491 break;
4492 case GOT_STATUS_DELETE:
4493 if (a->patch_cb) {
4494 int choice = GOT_PATCH_CHOICE_NONE;
4495 err = (*a->patch_cb)(&choice, a->patch_arg,
4496 status, ie->path, NULL, 1, 1);
4497 if (err)
4498 goto done;
4499 if (choice != GOT_PATCH_CHOICE_YES)
4500 break;
4502 /* fall through */
4503 case GOT_STATUS_MODIFY:
4504 case GOT_STATUS_MODE_CHANGE:
4505 case GOT_STATUS_CONFLICT:
4506 case GOT_STATUS_MISSING: {
4507 struct got_object_id id;
4508 if (staged_status == GOT_STATUS_ADD ||
4509 staged_status == GOT_STATUS_MODIFY) {
4510 memcpy(id.sha1, ie->staged_blob_sha1,
4511 SHA1_DIGEST_LENGTH);
4512 } else
4513 memcpy(id.sha1, ie->blob_sha1,
4514 SHA1_DIGEST_LENGTH);
4515 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4516 if (err)
4517 goto done;
4519 if (asprintf(&ondisk_path, "%s/%s",
4520 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4521 err = got_error_from_errno("asprintf");
4522 goto done;
4525 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4526 status == GOT_STATUS_CONFLICT)) {
4527 int is_bad_symlink = 0;
4528 err = create_patched_content(&path_content, 1, &id,
4529 ondisk_path, dirfd, de_name, ie->path, a->repo,
4530 a->patch_cb, a->patch_arg);
4531 if (err || path_content == NULL)
4532 break;
4533 if (te && S_ISLNK(te->mode)) {
4534 if (unlink(path_content) == -1) {
4535 err = got_error_from_errno2("unlink",
4536 path_content);
4537 break;
4539 err = install_symlink(&is_bad_symlink,
4540 a->worktree, ondisk_path, ie->path,
4541 blob, 0, 1, 0, a->repo,
4542 a->progress_cb, a->progress_arg);
4543 } else {
4544 if (rename(path_content, ondisk_path) == -1) {
4545 err = got_error_from_errno3("rename",
4546 path_content, ondisk_path);
4547 goto done;
4550 } else {
4551 int is_bad_symlink = 0;
4552 if (te && S_ISLNK(te->mode)) {
4553 err = install_symlink(&is_bad_symlink,
4554 a->worktree, ondisk_path, ie->path,
4555 blob, 0, 1, 0, a->repo,
4556 a->progress_cb, a->progress_arg);
4557 } else {
4558 err = install_blob(a->worktree, ondisk_path,
4559 ie->path,
4560 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4561 got_fileindex_perms_to_st(ie), blob,
4562 0, 1, 0, 0, a->repo,
4563 a->progress_cb, a->progress_arg);
4565 if (err)
4566 goto done;
4567 if (status == GOT_STATUS_DELETE ||
4568 status == GOT_STATUS_MODE_CHANGE) {
4569 err = got_fileindex_entry_update(ie,
4570 ondisk_path, blob->id.sha1,
4571 a->worktree->base_commit_id->sha1, 1);
4572 if (err)
4573 goto done;
4575 if (is_bad_symlink) {
4576 got_fileindex_entry_filetype_set(ie,
4577 GOT_FILEIDX_MODE_BAD_SYMLINK);
4580 break;
4582 default:
4583 break;
4585 done:
4586 free(ondisk_path);
4587 free(path_content);
4588 free(parent_path);
4589 free(tree_path);
4590 if (blob)
4591 got_object_blob_close(blob);
4592 if (tree)
4593 got_object_tree_close(tree);
4594 free(tree_id);
4595 return err;
4598 const struct got_error *
4599 got_worktree_revert(struct got_worktree *worktree,
4600 struct got_pathlist_head *paths,
4601 got_worktree_checkout_cb progress_cb, void *progress_arg,
4602 got_worktree_patch_cb patch_cb, void *patch_arg,
4603 struct got_repository *repo)
4605 struct got_fileindex *fileindex = NULL;
4606 char *fileindex_path = NULL;
4607 const struct got_error *err = NULL, *unlockerr = NULL;
4608 const struct got_error *sync_err = NULL;
4609 struct got_pathlist_entry *pe;
4610 struct revert_file_args rfa;
4612 err = lock_worktree(worktree, LOCK_EX);
4613 if (err)
4614 return err;
4616 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4617 if (err)
4618 goto done;
4620 rfa.worktree = worktree;
4621 rfa.fileindex = fileindex;
4622 rfa.progress_cb = progress_cb;
4623 rfa.progress_arg = progress_arg;
4624 rfa.patch_cb = patch_cb;
4625 rfa.patch_arg = patch_arg;
4626 rfa.repo = repo;
4627 TAILQ_FOREACH(pe, paths, entry) {
4628 err = worktree_status(worktree, pe->path, fileindex, repo,
4629 revert_file, &rfa, NULL, NULL, 0, 0);
4630 if (err)
4631 break;
4633 sync_err = sync_fileindex(fileindex, fileindex_path);
4634 if (sync_err && err == NULL)
4635 err = sync_err;
4636 done:
4637 free(fileindex_path);
4638 if (fileindex)
4639 got_fileindex_free(fileindex);
4640 unlockerr = lock_worktree(worktree, LOCK_SH);
4641 if (unlockerr && err == NULL)
4642 err = unlockerr;
4643 return err;
4646 static void
4647 free_commitable(struct got_commitable *ct)
4649 free(ct->path);
4650 free(ct->in_repo_path);
4651 free(ct->ondisk_path);
4652 free(ct->blob_id);
4653 free(ct->base_blob_id);
4654 free(ct->staged_blob_id);
4655 free(ct->base_commit_id);
4656 free(ct);
4659 struct collect_commitables_arg {
4660 struct got_pathlist_head *commitable_paths;
4661 struct got_repository *repo;
4662 struct got_worktree *worktree;
4663 struct got_fileindex *fileindex;
4664 int have_staged_files;
4665 int allow_bad_symlinks;
4668 static const struct got_error *
4669 collect_commitables(void *arg, unsigned char status,
4670 unsigned char staged_status, const char *relpath,
4671 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4672 struct got_object_id *commit_id, int dirfd, const char *de_name)
4674 struct collect_commitables_arg *a = arg;
4675 const struct got_error *err = NULL;
4676 struct got_commitable *ct = NULL;
4677 struct got_pathlist_entry *new = NULL;
4678 char *parent_path = NULL, *path = NULL;
4679 struct stat sb;
4681 if (a->have_staged_files) {
4682 if (staged_status != GOT_STATUS_MODIFY &&
4683 staged_status != GOT_STATUS_ADD &&
4684 staged_status != GOT_STATUS_DELETE)
4685 return NULL;
4686 } else {
4687 if (status == GOT_STATUS_CONFLICT)
4688 return got_error(GOT_ERR_COMMIT_CONFLICT);
4690 if (status != GOT_STATUS_MODIFY &&
4691 status != GOT_STATUS_MODE_CHANGE &&
4692 status != GOT_STATUS_ADD &&
4693 status != GOT_STATUS_DELETE)
4694 return NULL;
4697 if (asprintf(&path, "/%s", relpath) == -1) {
4698 err = got_error_from_errno("asprintf");
4699 goto done;
4701 if (strcmp(path, "/") == 0) {
4702 parent_path = strdup("");
4703 if (parent_path == NULL)
4704 return got_error_from_errno("strdup");
4705 } else {
4706 err = got_path_dirname(&parent_path, path);
4707 if (err)
4708 return err;
4711 ct = calloc(1, sizeof(*ct));
4712 if (ct == NULL) {
4713 err = got_error_from_errno("calloc");
4714 goto done;
4717 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4718 relpath) == -1) {
4719 err = got_error_from_errno("asprintf");
4720 goto done;
4723 if (staged_status == GOT_STATUS_ADD ||
4724 staged_status == GOT_STATUS_MODIFY) {
4725 struct got_fileindex_entry *ie;
4726 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4727 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4728 case GOT_FILEIDX_MODE_REGULAR_FILE:
4729 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4730 ct->mode = S_IFREG;
4731 break;
4732 case GOT_FILEIDX_MODE_SYMLINK:
4733 ct->mode = S_IFLNK;
4734 break;
4735 default:
4736 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4737 goto done;
4739 ct->mode |= got_fileindex_entry_perms_get(ie);
4740 } else if (status != GOT_STATUS_DELETE &&
4741 staged_status != GOT_STATUS_DELETE) {
4742 if (dirfd != -1) {
4743 if (fstatat(dirfd, de_name, &sb,
4744 AT_SYMLINK_NOFOLLOW) == -1) {
4745 err = got_error_from_errno2("fstatat",
4746 ct->ondisk_path);
4747 goto done;
4749 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4750 err = got_error_from_errno2("lstat", ct->ondisk_path);
4751 goto done;
4753 ct->mode = sb.st_mode;
4756 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4757 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4758 relpath) == -1) {
4759 err = got_error_from_errno("asprintf");
4760 goto done;
4763 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4764 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4765 int is_bad_symlink;
4766 char target_path[PATH_MAX];
4767 ssize_t target_len;
4768 target_len = readlink(ct->ondisk_path, target_path,
4769 sizeof(target_path));
4770 if (target_len == -1) {
4771 err = got_error_from_errno2("readlink",
4772 ct->ondisk_path);
4773 goto done;
4775 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4776 target_len, ct->ondisk_path, a->worktree->root_path);
4777 if (err)
4778 goto done;
4779 if (is_bad_symlink) {
4780 err = got_error_path(ct->ondisk_path,
4781 GOT_ERR_BAD_SYMLINK);
4782 goto done;
4787 ct->status = status;
4788 ct->staged_status = staged_status;
4789 ct->blob_id = NULL; /* will be filled in when blob gets created */
4790 if (ct->status != GOT_STATUS_ADD &&
4791 ct->staged_status != GOT_STATUS_ADD) {
4792 ct->base_blob_id = got_object_id_dup(blob_id);
4793 if (ct->base_blob_id == NULL) {
4794 err = got_error_from_errno("got_object_id_dup");
4795 goto done;
4797 ct->base_commit_id = got_object_id_dup(commit_id);
4798 if (ct->base_commit_id == NULL) {
4799 err = got_error_from_errno("got_object_id_dup");
4800 goto done;
4803 if (ct->staged_status == GOT_STATUS_ADD ||
4804 ct->staged_status == GOT_STATUS_MODIFY) {
4805 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4806 if (ct->staged_blob_id == NULL) {
4807 err = got_error_from_errno("got_object_id_dup");
4808 goto done;
4811 ct->path = strdup(path);
4812 if (ct->path == NULL) {
4813 err = got_error_from_errno("strdup");
4814 goto done;
4816 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4817 done:
4818 if (ct && (err || new == NULL))
4819 free_commitable(ct);
4820 free(parent_path);
4821 free(path);
4822 return err;
4825 static const struct got_error *write_tree(struct got_object_id **, int *,
4826 struct got_tree_object *, const char *, struct got_pathlist_head *,
4827 got_worktree_status_cb status_cb, void *status_arg,
4828 struct got_repository *);
4830 static const struct got_error *
4831 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4832 struct got_tree_entry *te, const char *parent_path,
4833 struct got_pathlist_head *commitable_paths,
4834 got_worktree_status_cb status_cb, void *status_arg,
4835 struct got_repository *repo)
4837 const struct got_error *err = NULL;
4838 struct got_tree_object *subtree;
4839 char *subpath;
4841 if (asprintf(&subpath, "%s%s%s", parent_path,
4842 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4843 return got_error_from_errno("asprintf");
4845 err = got_object_open_as_tree(&subtree, repo, &te->id);
4846 if (err)
4847 return err;
4849 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4850 commitable_paths, status_cb, status_arg, repo);
4851 got_object_tree_close(subtree);
4852 free(subpath);
4853 return err;
4856 static const struct got_error *
4857 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4859 const struct got_error *err = NULL;
4860 char *ct_parent_path = NULL;
4862 *match = 0;
4864 if (strchr(ct->in_repo_path, '/') == NULL) {
4865 *match = got_path_is_root_dir(path);
4866 return NULL;
4869 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4870 if (err)
4871 return err;
4872 *match = (strcmp(path, ct_parent_path) == 0);
4873 free(ct_parent_path);
4874 return err;
4877 static mode_t
4878 get_ct_file_mode(struct got_commitable *ct)
4880 if (S_ISLNK(ct->mode))
4881 return S_IFLNK;
4883 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4886 static const struct got_error *
4887 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4888 struct got_tree_entry *te, struct got_commitable *ct)
4890 const struct got_error *err = NULL;
4892 *new_te = NULL;
4894 err = got_object_tree_entry_dup(new_te, te);
4895 if (err)
4896 goto done;
4898 (*new_te)->mode = get_ct_file_mode(ct);
4900 if (ct->staged_status == GOT_STATUS_MODIFY)
4901 memcpy(&(*new_te)->id, ct->staged_blob_id,
4902 sizeof((*new_te)->id));
4903 else
4904 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4905 done:
4906 if (err && *new_te) {
4907 free(*new_te);
4908 *new_te = NULL;
4910 return err;
4913 static const struct got_error *
4914 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4915 struct got_commitable *ct)
4917 const struct got_error *err = NULL;
4918 char *ct_name = NULL;
4920 *new_te = NULL;
4922 *new_te = calloc(1, sizeof(**new_te));
4923 if (*new_te == NULL)
4924 return got_error_from_errno("calloc");
4926 err = got_path_basename(&ct_name, ct->path);
4927 if (err)
4928 goto done;
4929 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4930 sizeof((*new_te)->name)) {
4931 err = got_error(GOT_ERR_NO_SPACE);
4932 goto done;
4935 (*new_te)->mode = get_ct_file_mode(ct);
4937 if (ct->staged_status == GOT_STATUS_ADD)
4938 memcpy(&(*new_te)->id, ct->staged_blob_id,
4939 sizeof((*new_te)->id));
4940 else
4941 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4942 done:
4943 free(ct_name);
4944 if (err && *new_te) {
4945 free(*new_te);
4946 *new_te = NULL;
4948 return err;
4951 static const struct got_error *
4952 insert_tree_entry(struct got_tree_entry *new_te,
4953 struct got_pathlist_head *paths)
4955 const struct got_error *err = NULL;
4956 struct got_pathlist_entry *new_pe;
4958 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4959 if (err)
4960 return err;
4961 if (new_pe == NULL)
4962 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4963 return NULL;
4966 static const struct got_error *
4967 report_ct_status(struct got_commitable *ct,
4968 got_worktree_status_cb status_cb, void *status_arg)
4970 const char *ct_path = ct->path;
4971 unsigned char status;
4973 while (ct_path[0] == '/')
4974 ct_path++;
4976 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4977 status = ct->staged_status;
4978 else
4979 status = ct->status;
4981 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4982 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4985 static const struct got_error *
4986 match_modified_subtree(int *modified, struct got_tree_entry *te,
4987 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4989 const struct got_error *err = NULL;
4990 struct got_pathlist_entry *pe;
4991 char *te_path;
4993 *modified = 0;
4995 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4996 got_path_is_root_dir(base_tree_path) ? "" : "/",
4997 te->name) == -1)
4998 return got_error_from_errno("asprintf");
5000 TAILQ_FOREACH(pe, commitable_paths, entry) {
5001 struct got_commitable *ct = pe->data;
5002 *modified = got_path_is_child(ct->in_repo_path, te_path,
5003 strlen(te_path));
5004 if (*modified)
5005 break;
5008 free(te_path);
5009 return err;
5012 static const struct got_error *
5013 match_deleted_or_modified_ct(struct got_commitable **ctp,
5014 struct got_tree_entry *te, const char *base_tree_path,
5015 struct got_pathlist_head *commitable_paths)
5017 const struct got_error *err = NULL;
5018 struct got_pathlist_entry *pe;
5020 *ctp = NULL;
5022 TAILQ_FOREACH(pe, commitable_paths, entry) {
5023 struct got_commitable *ct = pe->data;
5024 char *ct_name = NULL;
5025 int path_matches;
5027 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5028 if (ct->status != GOT_STATUS_MODIFY &&
5029 ct->status != GOT_STATUS_MODE_CHANGE &&
5030 ct->status != GOT_STATUS_DELETE)
5031 continue;
5032 } else {
5033 if (ct->staged_status != GOT_STATUS_MODIFY &&
5034 ct->staged_status != GOT_STATUS_DELETE)
5035 continue;
5038 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5039 continue;
5041 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5042 if (err)
5043 return err;
5044 if (!path_matches)
5045 continue;
5047 err = got_path_basename(&ct_name, pe->path);
5048 if (err)
5049 return err;
5051 if (strcmp(te->name, ct_name) != 0) {
5052 free(ct_name);
5053 continue;
5055 free(ct_name);
5057 *ctp = ct;
5058 break;
5061 return err;
5064 static const struct got_error *
5065 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5066 const char *child_path, const char *path_base_tree,
5067 struct got_pathlist_head *commitable_paths,
5068 got_worktree_status_cb status_cb, void *status_arg,
5069 struct got_repository *repo)
5071 const struct got_error *err = NULL;
5072 struct got_tree_entry *new_te;
5073 char *subtree_path;
5074 struct got_object_id *id = NULL;
5075 int nentries;
5077 *new_tep = NULL;
5079 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5080 got_path_is_root_dir(path_base_tree) ? "" : "/",
5081 child_path) == -1)
5082 return got_error_from_errno("asprintf");
5084 new_te = calloc(1, sizeof(*new_te));
5085 if (new_te == NULL)
5086 return got_error_from_errno("calloc");
5087 new_te->mode = S_IFDIR;
5089 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5090 sizeof(new_te->name)) {
5091 err = got_error(GOT_ERR_NO_SPACE);
5092 goto done;
5094 err = write_tree(&id, &nentries, NULL, subtree_path,
5095 commitable_paths, status_cb, status_arg, repo);
5096 if (err) {
5097 free(new_te);
5098 goto done;
5100 memcpy(&new_te->id, id, sizeof(new_te->id));
5101 done:
5102 free(id);
5103 free(subtree_path);
5104 if (err == NULL)
5105 *new_tep = new_te;
5106 return err;
5109 static const struct got_error *
5110 write_tree(struct got_object_id **new_tree_id, int *nentries,
5111 struct got_tree_object *base_tree, const char *path_base_tree,
5112 struct got_pathlist_head *commitable_paths,
5113 got_worktree_status_cb status_cb, void *status_arg,
5114 struct got_repository *repo)
5116 const struct got_error *err = NULL;
5117 struct got_pathlist_head paths;
5118 struct got_tree_entry *te, *new_te = NULL;
5119 struct got_pathlist_entry *pe;
5121 TAILQ_INIT(&paths);
5122 *nentries = 0;
5124 /* Insert, and recurse into, newly added entries first. */
5125 TAILQ_FOREACH(pe, commitable_paths, entry) {
5126 struct got_commitable *ct = pe->data;
5127 char *child_path = NULL, *slash;
5129 if ((ct->status != GOT_STATUS_ADD &&
5130 ct->staged_status != GOT_STATUS_ADD) ||
5131 (ct->flags & GOT_COMMITABLE_ADDED))
5132 continue;
5134 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5135 strlen(path_base_tree)))
5136 continue;
5138 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5139 ct->in_repo_path);
5140 if (err)
5141 goto done;
5143 slash = strchr(child_path, '/');
5144 if (slash == NULL) {
5145 err = alloc_added_blob_tree_entry(&new_te, ct);
5146 if (err)
5147 goto done;
5148 err = report_ct_status(ct, status_cb, status_arg);
5149 if (err)
5150 goto done;
5151 ct->flags |= GOT_COMMITABLE_ADDED;
5152 err = insert_tree_entry(new_te, &paths);
5153 if (err)
5154 goto done;
5155 (*nentries)++;
5156 } else {
5157 *slash = '\0'; /* trim trailing path components */
5158 if (base_tree == NULL ||
5159 got_object_tree_find_entry(base_tree, child_path)
5160 == NULL) {
5161 err = make_subtree_for_added_blob(&new_te,
5162 child_path, path_base_tree,
5163 commitable_paths, status_cb, status_arg,
5164 repo);
5165 if (err)
5166 goto done;
5167 err = insert_tree_entry(new_te, &paths);
5168 if (err)
5169 goto done;
5170 (*nentries)++;
5175 if (base_tree) {
5176 int i, nbase_entries;
5177 /* Handle modified and deleted entries. */
5178 nbase_entries = got_object_tree_get_nentries(base_tree);
5179 for (i = 0; i < nbase_entries; i++) {
5180 struct got_commitable *ct = NULL;
5182 te = got_object_tree_get_entry(base_tree, i);
5183 if (got_object_tree_entry_is_submodule(te)) {
5184 /* Entry is a submodule; just copy it. */
5185 err = got_object_tree_entry_dup(&new_te, te);
5186 if (err)
5187 goto done;
5188 err = insert_tree_entry(new_te, &paths);
5189 if (err)
5190 goto done;
5191 (*nentries)++;
5192 continue;
5195 if (S_ISDIR(te->mode)) {
5196 int modified;
5197 err = got_object_tree_entry_dup(&new_te, te);
5198 if (err)
5199 goto done;
5200 err = match_modified_subtree(&modified, te,
5201 path_base_tree, commitable_paths);
5202 if (err)
5203 goto done;
5204 /* Avoid recursion into unmodified subtrees. */
5205 if (modified) {
5206 struct got_object_id *new_id;
5207 int nsubentries;
5208 err = write_subtree(&new_id,
5209 &nsubentries, te,
5210 path_base_tree, commitable_paths,
5211 status_cb, status_arg, repo);
5212 if (err)
5213 goto done;
5214 if (nsubentries == 0) {
5215 /* All entries were deleted. */
5216 free(new_id);
5217 continue;
5219 memcpy(&new_te->id, new_id,
5220 sizeof(new_te->id));
5221 free(new_id);
5223 err = insert_tree_entry(new_te, &paths);
5224 if (err)
5225 goto done;
5226 (*nentries)++;
5227 continue;
5230 err = match_deleted_or_modified_ct(&ct, te,
5231 path_base_tree, commitable_paths);
5232 if (err)
5233 goto done;
5234 if (ct) {
5235 /* NB: Deleted entries get dropped here. */
5236 if (ct->status == GOT_STATUS_MODIFY ||
5237 ct->status == GOT_STATUS_MODE_CHANGE ||
5238 ct->staged_status == GOT_STATUS_MODIFY) {
5239 err = alloc_modified_blob_tree_entry(
5240 &new_te, te, ct);
5241 if (err)
5242 goto done;
5243 err = insert_tree_entry(new_te, &paths);
5244 if (err)
5245 goto done;
5246 (*nentries)++;
5248 err = report_ct_status(ct, status_cb,
5249 status_arg);
5250 if (err)
5251 goto done;
5252 } else {
5253 /* Entry is unchanged; just copy it. */
5254 err = got_object_tree_entry_dup(&new_te, te);
5255 if (err)
5256 goto done;
5257 err = insert_tree_entry(new_te, &paths);
5258 if (err)
5259 goto done;
5260 (*nentries)++;
5265 /* Write new list of entries; deleted entries have been dropped. */
5266 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5267 done:
5268 got_pathlist_free(&paths);
5269 return err;
5272 static const struct got_error *
5273 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5274 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5275 int have_staged_files)
5277 const struct got_error *err = NULL;
5278 struct got_pathlist_entry *pe;
5280 TAILQ_FOREACH(pe, commitable_paths, entry) {
5281 struct got_fileindex_entry *ie;
5282 struct got_commitable *ct = pe->data;
5284 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5285 if (ie) {
5286 if (ct->status == GOT_STATUS_DELETE ||
5287 ct->staged_status == GOT_STATUS_DELETE) {
5288 got_fileindex_entry_remove(fileindex, ie);
5289 } else if (ct->staged_status == GOT_STATUS_ADD ||
5290 ct->staged_status == GOT_STATUS_MODIFY) {
5291 got_fileindex_entry_stage_set(ie,
5292 GOT_FILEIDX_STAGE_NONE);
5293 got_fileindex_entry_staged_filetype_set(ie, 0);
5294 err = got_fileindex_entry_update(ie,
5295 ct->ondisk_path, ct->staged_blob_id->sha1,
5296 new_base_commit_id->sha1,
5297 !have_staged_files);
5298 } else
5299 err = got_fileindex_entry_update(ie,
5300 ct->ondisk_path, ct->blob_id->sha1,
5301 new_base_commit_id->sha1,
5302 !have_staged_files);
5303 } else {
5304 err = got_fileindex_entry_alloc(&ie, pe->path);
5305 if (err)
5306 break;
5307 err = got_fileindex_entry_update(ie, ct->ondisk_path,
5308 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5309 if (err) {
5310 got_fileindex_entry_free(ie);
5311 break;
5313 err = got_fileindex_entry_add(fileindex, ie);
5314 if (err) {
5315 got_fileindex_entry_free(ie);
5316 break;
5320 return err;
5324 static const struct got_error *
5325 check_out_of_date(const char *in_repo_path, unsigned char status,
5326 unsigned char staged_status, struct got_object_id *base_blob_id,
5327 struct got_object_id *base_commit_id,
5328 struct got_object_id *head_commit_id, struct got_repository *repo,
5329 int ood_errcode)
5331 const struct got_error *err = NULL;
5332 struct got_object_id *id = NULL;
5334 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5335 /* Trivial case: base commit == head commit */
5336 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5337 return NULL;
5339 * Ensure file content which local changes were based
5340 * on matches file content in the branch head.
5342 err = got_object_id_by_path(&id, repo, head_commit_id,
5343 in_repo_path);
5344 if (err) {
5345 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5346 err = got_error(ood_errcode);
5347 goto done;
5348 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5349 err = got_error(ood_errcode);
5350 } else {
5351 /* Require that added files don't exist in the branch head. */
5352 err = got_object_id_by_path(&id, repo, head_commit_id,
5353 in_repo_path);
5354 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5355 goto done;
5356 err = id ? got_error(ood_errcode) : NULL;
5358 done:
5359 free(id);
5360 return err;
5363 const struct got_error *
5364 commit_worktree(struct got_object_id **new_commit_id,
5365 struct got_pathlist_head *commitable_paths,
5366 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5367 const char *author, const char *committer,
5368 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5369 got_worktree_status_cb status_cb, void *status_arg,
5370 struct got_repository *repo)
5372 const struct got_error *err = NULL, *unlockerr = NULL;
5373 struct got_pathlist_entry *pe;
5374 const char *head_ref_name = NULL;
5375 struct got_commit_object *head_commit = NULL;
5376 struct got_reference *head_ref2 = NULL;
5377 struct got_object_id *head_commit_id2 = NULL;
5378 struct got_tree_object *head_tree = NULL;
5379 struct got_object_id *new_tree_id = NULL;
5380 int nentries;
5381 struct got_object_id_queue parent_ids;
5382 struct got_object_qid *pid = NULL;
5383 char *logmsg = NULL;
5385 *new_commit_id = NULL;
5387 SIMPLEQ_INIT(&parent_ids);
5389 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5390 if (err)
5391 goto done;
5393 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5394 if (err)
5395 goto done;
5397 if (commit_msg_cb != NULL) {
5398 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5399 if (err)
5400 goto done;
5403 if (logmsg == NULL || strlen(logmsg) == 0) {
5404 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5405 goto done;
5408 /* Create blobs from added and modified files and record their IDs. */
5409 TAILQ_FOREACH(pe, commitable_paths, entry) {
5410 struct got_commitable *ct = pe->data;
5411 char *ondisk_path;
5413 /* Blobs for staged files already exist. */
5414 if (ct->staged_status == GOT_STATUS_ADD ||
5415 ct->staged_status == GOT_STATUS_MODIFY)
5416 continue;
5418 if (ct->status != GOT_STATUS_ADD &&
5419 ct->status != GOT_STATUS_MODIFY &&
5420 ct->status != GOT_STATUS_MODE_CHANGE)
5421 continue;
5423 if (asprintf(&ondisk_path, "%s/%s",
5424 worktree->root_path, pe->path) == -1) {
5425 err = got_error_from_errno("asprintf");
5426 goto done;
5428 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5429 free(ondisk_path);
5430 if (err)
5431 goto done;
5434 /* Recursively write new tree objects. */
5435 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5436 commitable_paths, status_cb, status_arg, repo);
5437 if (err)
5438 goto done;
5440 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5441 if (err)
5442 goto done;
5443 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5444 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5445 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5446 got_object_qid_free(pid);
5447 if (logmsg != NULL)
5448 free(logmsg);
5449 if (err)
5450 goto done;
5452 /* Check if a concurrent commit to our branch has occurred. */
5453 head_ref_name = got_worktree_get_head_ref_name(worktree);
5454 if (head_ref_name == NULL) {
5455 err = got_error_from_errno("got_worktree_get_head_ref_name");
5456 goto done;
5458 /* Lock the reference here to prevent concurrent modification. */
5459 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5460 if (err)
5461 goto done;
5462 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5463 if (err)
5464 goto done;
5465 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5466 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5467 goto done;
5469 /* Update branch head in repository. */
5470 err = got_ref_change_ref(head_ref2, *new_commit_id);
5471 if (err)
5472 goto done;
5473 err = got_ref_write(head_ref2, repo);
5474 if (err)
5475 goto done;
5477 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5478 if (err)
5479 goto done;
5481 err = ref_base_commit(worktree, repo);
5482 if (err)
5483 goto done;
5484 done:
5485 if (head_tree)
5486 got_object_tree_close(head_tree);
5487 if (head_commit)
5488 got_object_commit_close(head_commit);
5489 free(head_commit_id2);
5490 if (head_ref2) {
5491 unlockerr = got_ref_unlock(head_ref2);
5492 if (unlockerr && err == NULL)
5493 err = unlockerr;
5494 got_ref_close(head_ref2);
5496 return err;
5499 static const struct got_error *
5500 check_path_is_commitable(const char *path,
5501 struct got_pathlist_head *commitable_paths)
5503 struct got_pathlist_entry *cpe = NULL;
5504 size_t path_len = strlen(path);
5506 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5507 struct got_commitable *ct = cpe->data;
5508 const char *ct_path = ct->path;
5510 while (ct_path[0] == '/')
5511 ct_path++;
5513 if (strcmp(path, ct_path) == 0 ||
5514 got_path_is_child(ct_path, path, path_len))
5515 break;
5518 if (cpe == NULL)
5519 return got_error_path(path, GOT_ERR_BAD_PATH);
5521 return NULL;
5524 static const struct got_error *
5525 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5527 int *have_staged_files = arg;
5529 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5530 *have_staged_files = 1;
5531 return got_error(GOT_ERR_CANCELLED);
5534 return NULL;
5537 static const struct got_error *
5538 check_non_staged_files(struct got_fileindex *fileindex,
5539 struct got_pathlist_head *paths)
5541 struct got_pathlist_entry *pe;
5542 struct got_fileindex_entry *ie;
5544 TAILQ_FOREACH(pe, paths, entry) {
5545 if (pe->path[0] == '\0')
5546 continue;
5547 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5548 if (ie == NULL)
5549 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5550 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5551 return got_error_path(pe->path,
5552 GOT_ERR_FILE_NOT_STAGED);
5555 return NULL;
5558 const struct got_error *
5559 got_worktree_commit(struct got_object_id **new_commit_id,
5560 struct got_worktree *worktree, struct got_pathlist_head *paths,
5561 const char *author, const char *committer, int allow_bad_symlinks,
5562 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5563 got_worktree_status_cb status_cb, void *status_arg,
5564 struct got_repository *repo)
5566 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5567 struct got_fileindex *fileindex = NULL;
5568 char *fileindex_path = NULL;
5569 struct got_pathlist_head commitable_paths;
5570 struct collect_commitables_arg cc_arg;
5571 struct got_pathlist_entry *pe;
5572 struct got_reference *head_ref = NULL;
5573 struct got_object_id *head_commit_id = NULL;
5574 int have_staged_files = 0;
5576 *new_commit_id = NULL;
5578 TAILQ_INIT(&commitable_paths);
5580 err = lock_worktree(worktree, LOCK_EX);
5581 if (err)
5582 goto done;
5584 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5585 if (err)
5586 goto done;
5588 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5589 if (err)
5590 goto done;
5592 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5593 if (err)
5594 goto done;
5596 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5597 &have_staged_files);
5598 if (err && err->code != GOT_ERR_CANCELLED)
5599 goto done;
5600 if (have_staged_files) {
5601 err = check_non_staged_files(fileindex, paths);
5602 if (err)
5603 goto done;
5606 cc_arg.commitable_paths = &commitable_paths;
5607 cc_arg.worktree = worktree;
5608 cc_arg.fileindex = fileindex;
5609 cc_arg.repo = repo;
5610 cc_arg.have_staged_files = have_staged_files;
5611 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5612 TAILQ_FOREACH(pe, paths, entry) {
5613 err = worktree_status(worktree, pe->path, fileindex, repo,
5614 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5615 if (err)
5616 goto done;
5619 if (TAILQ_EMPTY(&commitable_paths)) {
5620 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5621 goto done;
5624 TAILQ_FOREACH(pe, paths, entry) {
5625 err = check_path_is_commitable(pe->path, &commitable_paths);
5626 if (err)
5627 goto done;
5630 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5631 struct got_commitable *ct = pe->data;
5632 const char *ct_path = ct->in_repo_path;
5634 while (ct_path[0] == '/')
5635 ct_path++;
5636 err = check_out_of_date(ct_path, ct->status,
5637 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5638 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5639 if (err)
5640 goto done;
5644 err = commit_worktree(new_commit_id, &commitable_paths,
5645 head_commit_id, worktree, author, committer,
5646 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5647 if (err)
5648 goto done;
5650 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5651 fileindex, have_staged_files);
5652 sync_err = sync_fileindex(fileindex, fileindex_path);
5653 if (sync_err && err == NULL)
5654 err = sync_err;
5655 done:
5656 if (fileindex)
5657 got_fileindex_free(fileindex);
5658 free(fileindex_path);
5659 unlockerr = lock_worktree(worktree, LOCK_SH);
5660 if (unlockerr && err == NULL)
5661 err = unlockerr;
5662 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5663 struct got_commitable *ct = pe->data;
5664 free_commitable(ct);
5666 got_pathlist_free(&commitable_paths);
5667 return err;
5670 const char *
5671 got_commitable_get_path(struct got_commitable *ct)
5673 return ct->path;
5676 unsigned int
5677 got_commitable_get_status(struct got_commitable *ct)
5679 return ct->status;
5682 struct check_rebase_ok_arg {
5683 struct got_worktree *worktree;
5684 struct got_repository *repo;
5687 static const struct got_error *
5688 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5690 const struct got_error *err = NULL;
5691 struct check_rebase_ok_arg *a = arg;
5692 unsigned char status;
5693 struct stat sb;
5694 char *ondisk_path;
5696 /* Reject rebase of a work tree with mixed base commits. */
5697 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5698 SHA1_DIGEST_LENGTH))
5699 return got_error(GOT_ERR_MIXED_COMMITS);
5701 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5702 == -1)
5703 return got_error_from_errno("asprintf");
5705 /* Reject rebase of a work tree with modified or staged files. */
5706 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5707 free(ondisk_path);
5708 if (err)
5709 return err;
5711 if (status != GOT_STATUS_NO_CHANGE)
5712 return got_error(GOT_ERR_MODIFIED);
5713 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5714 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5716 return NULL;
5719 const struct got_error *
5720 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5721 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5722 struct got_worktree *worktree, struct got_reference *branch,
5723 struct got_repository *repo)
5725 const struct got_error *err = NULL;
5726 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5727 char *branch_ref_name = NULL;
5728 char *fileindex_path = NULL;
5729 struct check_rebase_ok_arg ok_arg;
5730 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5731 struct got_object_id *wt_branch_tip = NULL;
5733 *new_base_branch_ref = NULL;
5734 *tmp_branch = NULL;
5735 *fileindex = NULL;
5737 err = lock_worktree(worktree, LOCK_EX);
5738 if (err)
5739 return err;
5741 err = open_fileindex(fileindex, &fileindex_path, worktree);
5742 if (err)
5743 goto done;
5745 ok_arg.worktree = worktree;
5746 ok_arg.repo = repo;
5747 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5748 &ok_arg);
5749 if (err)
5750 goto done;
5752 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5753 if (err)
5754 goto done;
5756 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5757 if (err)
5758 goto done;
5760 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5761 if (err)
5762 goto done;
5764 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5765 0);
5766 if (err)
5767 goto done;
5769 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5770 if (err)
5771 goto done;
5772 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5773 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5774 goto done;
5777 err = got_ref_alloc_symref(new_base_branch_ref,
5778 new_base_branch_ref_name, wt_branch);
5779 if (err)
5780 goto done;
5781 err = got_ref_write(*new_base_branch_ref, repo);
5782 if (err)
5783 goto done;
5785 /* TODO Lock original branch's ref while rebasing? */
5787 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5788 if (err)
5789 goto done;
5791 err = got_ref_write(branch_ref, repo);
5792 if (err)
5793 goto done;
5795 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5796 worktree->base_commit_id);
5797 if (err)
5798 goto done;
5799 err = got_ref_write(*tmp_branch, repo);
5800 if (err)
5801 goto done;
5803 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5804 if (err)
5805 goto done;
5806 done:
5807 free(fileindex_path);
5808 free(tmp_branch_name);
5809 free(new_base_branch_ref_name);
5810 free(branch_ref_name);
5811 if (branch_ref)
5812 got_ref_close(branch_ref);
5813 if (wt_branch)
5814 got_ref_close(wt_branch);
5815 free(wt_branch_tip);
5816 if (err) {
5817 if (*new_base_branch_ref) {
5818 got_ref_close(*new_base_branch_ref);
5819 *new_base_branch_ref = NULL;
5821 if (*tmp_branch) {
5822 got_ref_close(*tmp_branch);
5823 *tmp_branch = NULL;
5825 if (*fileindex) {
5826 got_fileindex_free(*fileindex);
5827 *fileindex = NULL;
5829 lock_worktree(worktree, LOCK_SH);
5831 return err;
5834 const struct got_error *
5835 got_worktree_rebase_continue(struct got_object_id **commit_id,
5836 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5837 struct got_reference **branch, struct got_fileindex **fileindex,
5838 struct got_worktree *worktree, struct got_repository *repo)
5840 const struct got_error *err;
5841 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5842 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5843 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5844 char *fileindex_path = NULL;
5845 int have_staged_files = 0;
5847 *commit_id = NULL;
5848 *new_base_branch = NULL;
5849 *tmp_branch = NULL;
5850 *branch = NULL;
5851 *fileindex = NULL;
5853 err = lock_worktree(worktree, LOCK_EX);
5854 if (err)
5855 return err;
5857 err = open_fileindex(fileindex, &fileindex_path, worktree);
5858 if (err)
5859 goto done;
5861 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5862 &have_staged_files);
5863 if (err && err->code != GOT_ERR_CANCELLED)
5864 goto done;
5865 if (have_staged_files) {
5866 err = got_error(GOT_ERR_STAGED_PATHS);
5867 goto done;
5870 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5871 if (err)
5872 goto done;
5874 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5875 if (err)
5876 goto done;
5878 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5879 if (err)
5880 goto done;
5882 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5883 if (err)
5884 goto done;
5886 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5887 if (err)
5888 goto done;
5890 err = got_ref_open(branch, repo,
5891 got_ref_get_symref_target(branch_ref), 0);
5892 if (err)
5893 goto done;
5895 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5896 if (err)
5897 goto done;
5899 err = got_ref_resolve(commit_id, repo, commit_ref);
5900 if (err)
5901 goto done;
5903 err = got_ref_open(new_base_branch, repo,
5904 new_base_branch_ref_name, 0);
5905 if (err)
5906 goto done;
5908 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5909 if (err)
5910 goto done;
5911 done:
5912 free(commit_ref_name);
5913 free(branch_ref_name);
5914 free(fileindex_path);
5915 if (commit_ref)
5916 got_ref_close(commit_ref);
5917 if (branch_ref)
5918 got_ref_close(branch_ref);
5919 if (err) {
5920 free(*commit_id);
5921 *commit_id = NULL;
5922 if (*tmp_branch) {
5923 got_ref_close(*tmp_branch);
5924 *tmp_branch = NULL;
5926 if (*new_base_branch) {
5927 got_ref_close(*new_base_branch);
5928 *new_base_branch = NULL;
5930 if (*branch) {
5931 got_ref_close(*branch);
5932 *branch = NULL;
5934 if (*fileindex) {
5935 got_fileindex_free(*fileindex);
5936 *fileindex = NULL;
5938 lock_worktree(worktree, LOCK_SH);
5940 return err;
5943 const struct got_error *
5944 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5946 const struct got_error *err;
5947 char *tmp_branch_name = NULL;
5949 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5950 if (err)
5951 return err;
5953 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5954 free(tmp_branch_name);
5955 return NULL;
5958 static const struct got_error *
5959 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5960 char **logmsg, void *arg)
5962 *logmsg = arg;
5963 return NULL;
5966 static const struct got_error *
5967 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5968 const char *path, struct got_object_id *blob_id,
5969 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5970 int dirfd, const char *de_name)
5972 return NULL;
5975 struct collect_merged_paths_arg {
5976 got_worktree_checkout_cb progress_cb;
5977 void *progress_arg;
5978 struct got_pathlist_head *merged_paths;
5981 static const struct got_error *
5982 collect_merged_paths(void *arg, unsigned char status, const char *path)
5984 const struct got_error *err;
5985 struct collect_merged_paths_arg *a = arg;
5986 char *p;
5987 struct got_pathlist_entry *new;
5989 err = (*a->progress_cb)(a->progress_arg, status, path);
5990 if (err)
5991 return err;
5993 if (status != GOT_STATUS_MERGE &&
5994 status != GOT_STATUS_ADD &&
5995 status != GOT_STATUS_DELETE &&
5996 status != GOT_STATUS_CONFLICT)
5997 return NULL;
5999 p = strdup(path);
6000 if (p == NULL)
6001 return got_error_from_errno("strdup");
6003 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6004 if (err || new == NULL)
6005 free(p);
6006 return err;
6009 void
6010 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6012 struct got_pathlist_entry *pe;
6014 TAILQ_FOREACH(pe, merged_paths, entry)
6015 free((char *)pe->path);
6017 got_pathlist_free(merged_paths);
6020 static const struct got_error *
6021 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6022 int is_rebase, struct got_repository *repo)
6024 const struct got_error *err;
6025 struct got_reference *commit_ref = NULL;
6027 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6028 if (err) {
6029 if (err->code != GOT_ERR_NOT_REF)
6030 goto done;
6031 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6032 if (err)
6033 goto done;
6034 err = got_ref_write(commit_ref, repo);
6035 if (err)
6036 goto done;
6037 } else if (is_rebase) {
6038 struct got_object_id *stored_id;
6039 int cmp;
6041 err = got_ref_resolve(&stored_id, repo, commit_ref);
6042 if (err)
6043 goto done;
6044 cmp = got_object_id_cmp(commit_id, stored_id);
6045 free(stored_id);
6046 if (cmp != 0) {
6047 err = got_error(GOT_ERR_REBASE_COMMITID);
6048 goto done;
6051 done:
6052 if (commit_ref)
6053 got_ref_close(commit_ref);
6054 return err;
6057 static const struct got_error *
6058 rebase_merge_files(struct got_pathlist_head *merged_paths,
6059 const char *commit_ref_name, struct got_worktree *worktree,
6060 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6061 struct got_object_id *commit_id, struct got_repository *repo,
6062 got_worktree_checkout_cb progress_cb, void *progress_arg,
6063 got_cancel_cb cancel_cb, void *cancel_arg)
6065 const struct got_error *err;
6066 struct got_reference *commit_ref = NULL;
6067 struct collect_merged_paths_arg cmp_arg;
6068 char *fileindex_path;
6070 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6072 err = get_fileindex_path(&fileindex_path, worktree);
6073 if (err)
6074 return err;
6076 cmp_arg.progress_cb = progress_cb;
6077 cmp_arg.progress_arg = progress_arg;
6078 cmp_arg.merged_paths = merged_paths;
6079 err = merge_files(worktree, fileindex, fileindex_path,
6080 parent_commit_id, commit_id, repo, collect_merged_paths,
6081 &cmp_arg, cancel_cb, cancel_arg);
6082 if (commit_ref)
6083 got_ref_close(commit_ref);
6084 return err;
6087 const struct got_error *
6088 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6089 struct got_worktree *worktree, struct got_fileindex *fileindex,
6090 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6091 struct got_repository *repo,
6092 got_worktree_checkout_cb progress_cb, void *progress_arg,
6093 got_cancel_cb cancel_cb, void *cancel_arg)
6095 const struct got_error *err;
6096 char *commit_ref_name;
6098 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6099 if (err)
6100 return err;
6102 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6103 if (err)
6104 goto done;
6106 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6107 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6108 progress_arg, cancel_cb, cancel_arg);
6109 done:
6110 free(commit_ref_name);
6111 return err;
6114 const struct got_error *
6115 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6116 struct got_worktree *worktree, struct got_fileindex *fileindex,
6117 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6118 struct got_repository *repo,
6119 got_worktree_checkout_cb progress_cb, void *progress_arg,
6120 got_cancel_cb cancel_cb, void *cancel_arg)
6122 const struct got_error *err;
6123 char *commit_ref_name;
6125 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6126 if (err)
6127 return err;
6129 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6130 if (err)
6131 goto done;
6133 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6134 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6135 progress_arg, cancel_cb, cancel_arg);
6136 done:
6137 free(commit_ref_name);
6138 return err;
6141 static const struct got_error *
6142 rebase_commit(struct got_object_id **new_commit_id,
6143 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6144 struct got_worktree *worktree, struct got_fileindex *fileindex,
6145 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6146 const char *new_logmsg, struct got_repository *repo)
6148 const struct got_error *err, *sync_err;
6149 struct got_pathlist_head commitable_paths;
6150 struct collect_commitables_arg cc_arg;
6151 char *fileindex_path = NULL;
6152 struct got_reference *head_ref = NULL;
6153 struct got_object_id *head_commit_id = NULL;
6154 char *logmsg = NULL;
6156 TAILQ_INIT(&commitable_paths);
6157 *new_commit_id = NULL;
6159 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6161 err = get_fileindex_path(&fileindex_path, worktree);
6162 if (err)
6163 return err;
6165 cc_arg.commitable_paths = &commitable_paths;
6166 cc_arg.worktree = worktree;
6167 cc_arg.repo = repo;
6168 cc_arg.have_staged_files = 0;
6170 * If possible get the status of individual files directly to
6171 * avoid crawling the entire work tree once per rebased commit.
6172 * TODO: Ideally, merged_paths would contain a list of commitables
6173 * we could use so we could skip worktree_status() entirely.
6175 if (merged_paths) {
6176 struct got_pathlist_entry *pe;
6177 TAILQ_FOREACH(pe, merged_paths, entry) {
6178 err = worktree_status(worktree, pe->path, fileindex,
6179 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6180 0);
6181 if (err)
6182 goto done;
6184 } else {
6185 err = worktree_status(worktree, "", fileindex, repo,
6186 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6187 if (err)
6188 goto done;
6191 if (TAILQ_EMPTY(&commitable_paths)) {
6192 /* No-op change; commit will be elided. */
6193 err = got_ref_delete(commit_ref, repo);
6194 if (err)
6195 goto done;
6196 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6197 goto done;
6200 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6201 if (err)
6202 goto done;
6204 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6205 if (err)
6206 goto done;
6208 if (new_logmsg) {
6209 logmsg = strdup(new_logmsg);
6210 if (logmsg == NULL) {
6211 err = got_error_from_errno("strdup");
6212 goto done;
6214 } else {
6215 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6216 if (err)
6217 goto done;
6220 /* NB: commit_worktree will call free(logmsg) */
6221 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6222 worktree, got_object_commit_get_author(orig_commit),
6223 got_object_commit_get_committer(orig_commit),
6224 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6225 if (err)
6226 goto done;
6228 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6229 if (err)
6230 goto done;
6232 err = got_ref_delete(commit_ref, repo);
6233 if (err)
6234 goto done;
6236 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6237 fileindex, 0);
6238 sync_err = sync_fileindex(fileindex, fileindex_path);
6239 if (sync_err && err == NULL)
6240 err = sync_err;
6241 done:
6242 free(fileindex_path);
6243 free(head_commit_id);
6244 if (head_ref)
6245 got_ref_close(head_ref);
6246 if (err) {
6247 free(*new_commit_id);
6248 *new_commit_id = NULL;
6250 return err;
6253 const struct got_error *
6254 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6255 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6256 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6257 struct got_commit_object *orig_commit,
6258 struct got_object_id *orig_commit_id, struct got_repository *repo)
6260 const struct got_error *err;
6261 char *commit_ref_name;
6262 struct got_reference *commit_ref = NULL;
6263 struct got_object_id *commit_id = NULL;
6265 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6266 if (err)
6267 return err;
6269 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6270 if (err)
6271 goto done;
6272 err = got_ref_resolve(&commit_id, repo, commit_ref);
6273 if (err)
6274 goto done;
6275 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6276 err = got_error(GOT_ERR_REBASE_COMMITID);
6277 goto done;
6280 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6281 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6282 done:
6283 if (commit_ref)
6284 got_ref_close(commit_ref);
6285 free(commit_ref_name);
6286 free(commit_id);
6287 return err;
6290 const struct got_error *
6291 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6292 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6293 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6294 struct got_commit_object *orig_commit,
6295 struct got_object_id *orig_commit_id, const char *new_logmsg,
6296 struct got_repository *repo)
6298 const struct got_error *err;
6299 char *commit_ref_name;
6300 struct got_reference *commit_ref = NULL;
6302 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6303 if (err)
6304 return err;
6306 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6307 if (err)
6308 goto done;
6310 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6311 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6312 done:
6313 if (commit_ref)
6314 got_ref_close(commit_ref);
6315 free(commit_ref_name);
6316 return err;
6319 const struct got_error *
6320 got_worktree_rebase_postpone(struct got_worktree *worktree,
6321 struct got_fileindex *fileindex)
6323 if (fileindex)
6324 got_fileindex_free(fileindex);
6325 return lock_worktree(worktree, LOCK_SH);
6328 static const struct got_error *
6329 delete_ref(const char *name, struct got_repository *repo)
6331 const struct got_error *err;
6332 struct got_reference *ref;
6334 err = got_ref_open(&ref, repo, name, 0);
6335 if (err) {
6336 if (err->code == GOT_ERR_NOT_REF)
6337 return NULL;
6338 return err;
6341 err = got_ref_delete(ref, repo);
6342 got_ref_close(ref);
6343 return err;
6346 static const struct got_error *
6347 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6349 const struct got_error *err;
6350 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6351 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6353 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6354 if (err)
6355 goto done;
6356 err = delete_ref(tmp_branch_name, repo);
6357 if (err)
6358 goto done;
6360 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6361 if (err)
6362 goto done;
6363 err = delete_ref(new_base_branch_ref_name, repo);
6364 if (err)
6365 goto done;
6367 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6368 if (err)
6369 goto done;
6370 err = delete_ref(branch_ref_name, repo);
6371 if (err)
6372 goto done;
6374 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6375 if (err)
6376 goto done;
6377 err = delete_ref(commit_ref_name, repo);
6378 if (err)
6379 goto done;
6381 done:
6382 free(tmp_branch_name);
6383 free(new_base_branch_ref_name);
6384 free(branch_ref_name);
6385 free(commit_ref_name);
6386 return err;
6389 const struct got_error *
6390 got_worktree_rebase_complete(struct got_worktree *worktree,
6391 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6392 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6393 struct got_repository *repo)
6395 const struct got_error *err, *unlockerr;
6396 struct got_object_id *new_head_commit_id = NULL;
6398 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6399 if (err)
6400 return err;
6402 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6403 if (err)
6404 goto done;
6406 err = got_ref_write(rebased_branch, repo);
6407 if (err)
6408 goto done;
6410 err = got_worktree_set_head_ref(worktree, rebased_branch);
6411 if (err)
6412 goto done;
6414 err = delete_rebase_refs(worktree, repo);
6415 done:
6416 if (fileindex)
6417 got_fileindex_free(fileindex);
6418 free(new_head_commit_id);
6419 unlockerr = lock_worktree(worktree, LOCK_SH);
6420 if (unlockerr && err == NULL)
6421 err = unlockerr;
6422 return err;
6425 const struct got_error *
6426 got_worktree_rebase_abort(struct got_worktree *worktree,
6427 struct got_fileindex *fileindex, struct got_repository *repo,
6428 struct got_reference *new_base_branch,
6429 got_worktree_checkout_cb progress_cb, void *progress_arg)
6431 const struct got_error *err, *unlockerr, *sync_err;
6432 struct got_reference *resolved = NULL;
6433 struct got_object_id *commit_id = NULL;
6434 char *fileindex_path = NULL;
6435 struct revert_file_args rfa;
6436 struct got_object_id *tree_id = NULL;
6438 err = lock_worktree(worktree, LOCK_EX);
6439 if (err)
6440 return err;
6442 err = got_ref_open(&resolved, repo,
6443 got_ref_get_symref_target(new_base_branch), 0);
6444 if (err)
6445 goto done;
6447 err = got_worktree_set_head_ref(worktree, resolved);
6448 if (err)
6449 goto done;
6452 * XXX commits to the base branch could have happened while
6453 * we were busy rebasing; should we store the original commit ID
6454 * when rebase begins and read it back here?
6456 err = got_ref_resolve(&commit_id, repo, resolved);
6457 if (err)
6458 goto done;
6460 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6461 if (err)
6462 goto done;
6464 err = got_object_id_by_path(&tree_id, repo,
6465 worktree->base_commit_id, worktree->path_prefix);
6466 if (err)
6467 goto done;
6469 err = delete_rebase_refs(worktree, repo);
6470 if (err)
6471 goto done;
6473 err = get_fileindex_path(&fileindex_path, worktree);
6474 if (err)
6475 goto done;
6477 rfa.worktree = worktree;
6478 rfa.fileindex = fileindex;
6479 rfa.progress_cb = progress_cb;
6480 rfa.progress_arg = progress_arg;
6481 rfa.patch_cb = NULL;
6482 rfa.patch_arg = NULL;
6483 rfa.repo = repo;
6484 err = worktree_status(worktree, "", fileindex, repo,
6485 revert_file, &rfa, NULL, NULL, 0, 0);
6486 if (err)
6487 goto sync;
6489 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6490 repo, progress_cb, progress_arg, NULL, NULL);
6491 sync:
6492 sync_err = sync_fileindex(fileindex, fileindex_path);
6493 if (sync_err && err == NULL)
6494 err = sync_err;
6495 done:
6496 got_ref_close(resolved);
6497 free(tree_id);
6498 free(commit_id);
6499 if (fileindex)
6500 got_fileindex_free(fileindex);
6501 free(fileindex_path);
6503 unlockerr = lock_worktree(worktree, LOCK_SH);
6504 if (unlockerr && err == NULL)
6505 err = unlockerr;
6506 return err;
6509 const struct got_error *
6510 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6511 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6512 struct got_fileindex **fileindex, struct got_worktree *worktree,
6513 struct got_repository *repo)
6515 const struct got_error *err = NULL;
6516 char *tmp_branch_name = NULL;
6517 char *branch_ref_name = NULL;
6518 char *base_commit_ref_name = NULL;
6519 char *fileindex_path = NULL;
6520 struct check_rebase_ok_arg ok_arg;
6521 struct got_reference *wt_branch = NULL;
6522 struct got_reference *base_commit_ref = NULL;
6524 *tmp_branch = NULL;
6525 *branch_ref = NULL;
6526 *base_commit_id = NULL;
6527 *fileindex = NULL;
6529 err = lock_worktree(worktree, LOCK_EX);
6530 if (err)
6531 return err;
6533 err = open_fileindex(fileindex, &fileindex_path, worktree);
6534 if (err)
6535 goto done;
6537 ok_arg.worktree = worktree;
6538 ok_arg.repo = repo;
6539 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6540 &ok_arg);
6541 if (err)
6542 goto done;
6544 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6545 if (err)
6546 goto done;
6548 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6549 if (err)
6550 goto done;
6552 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6553 worktree);
6554 if (err)
6555 goto done;
6557 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6558 0);
6559 if (err)
6560 goto done;
6562 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6563 if (err)
6564 goto done;
6566 err = got_ref_write(*branch_ref, repo);
6567 if (err)
6568 goto done;
6570 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6571 worktree->base_commit_id);
6572 if (err)
6573 goto done;
6574 err = got_ref_write(base_commit_ref, repo);
6575 if (err)
6576 goto done;
6577 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6578 if (*base_commit_id == NULL) {
6579 err = got_error_from_errno("got_object_id_dup");
6580 goto done;
6583 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6584 worktree->base_commit_id);
6585 if (err)
6586 goto done;
6587 err = got_ref_write(*tmp_branch, repo);
6588 if (err)
6589 goto done;
6591 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6592 if (err)
6593 goto done;
6594 done:
6595 free(fileindex_path);
6596 free(tmp_branch_name);
6597 free(branch_ref_name);
6598 free(base_commit_ref_name);
6599 if (wt_branch)
6600 got_ref_close(wt_branch);
6601 if (err) {
6602 if (*branch_ref) {
6603 got_ref_close(*branch_ref);
6604 *branch_ref = NULL;
6606 if (*tmp_branch) {
6607 got_ref_close(*tmp_branch);
6608 *tmp_branch = NULL;
6610 free(*base_commit_id);
6611 if (*fileindex) {
6612 got_fileindex_free(*fileindex);
6613 *fileindex = NULL;
6615 lock_worktree(worktree, LOCK_SH);
6617 return err;
6620 const struct got_error *
6621 got_worktree_histedit_postpone(struct got_worktree *worktree,
6622 struct got_fileindex *fileindex)
6624 if (fileindex)
6625 got_fileindex_free(fileindex);
6626 return lock_worktree(worktree, LOCK_SH);
6629 const struct got_error *
6630 got_worktree_histedit_in_progress(int *in_progress,
6631 struct got_worktree *worktree)
6633 const struct got_error *err;
6634 char *tmp_branch_name = NULL;
6636 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6637 if (err)
6638 return err;
6640 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6641 free(tmp_branch_name);
6642 return NULL;
6645 const struct got_error *
6646 got_worktree_histedit_continue(struct got_object_id **commit_id,
6647 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6648 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6649 struct got_worktree *worktree, struct got_repository *repo)
6651 const struct got_error *err;
6652 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6653 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6654 struct got_reference *commit_ref = NULL;
6655 struct got_reference *base_commit_ref = NULL;
6656 char *fileindex_path = NULL;
6657 int have_staged_files = 0;
6659 *commit_id = NULL;
6660 *tmp_branch = NULL;
6661 *base_commit_id = NULL;
6662 *fileindex = NULL;
6664 err = lock_worktree(worktree, LOCK_EX);
6665 if (err)
6666 return err;
6668 err = open_fileindex(fileindex, &fileindex_path, worktree);
6669 if (err)
6670 goto done;
6672 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6673 &have_staged_files);
6674 if (err && err->code != GOT_ERR_CANCELLED)
6675 goto done;
6676 if (have_staged_files) {
6677 err = got_error(GOT_ERR_STAGED_PATHS);
6678 goto done;
6681 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6682 if (err)
6683 goto done;
6685 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6686 if (err)
6687 goto done;
6689 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6690 if (err)
6691 goto done;
6693 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6694 worktree);
6695 if (err)
6696 goto done;
6698 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6699 if (err)
6700 goto done;
6702 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6703 if (err)
6704 goto done;
6705 err = got_ref_resolve(commit_id, repo, commit_ref);
6706 if (err)
6707 goto done;
6709 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6710 if (err)
6711 goto done;
6712 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6713 if (err)
6714 goto done;
6716 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6717 if (err)
6718 goto done;
6719 done:
6720 free(commit_ref_name);
6721 free(branch_ref_name);
6722 free(fileindex_path);
6723 if (commit_ref)
6724 got_ref_close(commit_ref);
6725 if (base_commit_ref)
6726 got_ref_close(base_commit_ref);
6727 if (err) {
6728 free(*commit_id);
6729 *commit_id = NULL;
6730 free(*base_commit_id);
6731 *base_commit_id = NULL;
6732 if (*tmp_branch) {
6733 got_ref_close(*tmp_branch);
6734 *tmp_branch = NULL;
6736 if (*fileindex) {
6737 got_fileindex_free(*fileindex);
6738 *fileindex = NULL;
6740 lock_worktree(worktree, LOCK_EX);
6742 return err;
6745 static const struct got_error *
6746 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6748 const struct got_error *err;
6749 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6750 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6752 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6753 if (err)
6754 goto done;
6755 err = delete_ref(tmp_branch_name, repo);
6756 if (err)
6757 goto done;
6759 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6760 worktree);
6761 if (err)
6762 goto done;
6763 err = delete_ref(base_commit_ref_name, repo);
6764 if (err)
6765 goto done;
6767 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6768 if (err)
6769 goto done;
6770 err = delete_ref(branch_ref_name, repo);
6771 if (err)
6772 goto done;
6774 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6775 if (err)
6776 goto done;
6777 err = delete_ref(commit_ref_name, repo);
6778 if (err)
6779 goto done;
6780 done:
6781 free(tmp_branch_name);
6782 free(base_commit_ref_name);
6783 free(branch_ref_name);
6784 free(commit_ref_name);
6785 return err;
6788 const struct got_error *
6789 got_worktree_histedit_abort(struct got_worktree *worktree,
6790 struct got_fileindex *fileindex, struct got_repository *repo,
6791 struct got_reference *branch, struct got_object_id *base_commit_id,
6792 got_worktree_checkout_cb progress_cb, void *progress_arg)
6794 const struct got_error *err, *unlockerr, *sync_err;
6795 struct got_reference *resolved = NULL;
6796 char *fileindex_path = NULL;
6797 struct got_object_id *tree_id = NULL;
6798 struct revert_file_args rfa;
6800 err = lock_worktree(worktree, LOCK_EX);
6801 if (err)
6802 return err;
6804 err = got_ref_open(&resolved, repo,
6805 got_ref_get_symref_target(branch), 0);
6806 if (err)
6807 goto done;
6809 err = got_worktree_set_head_ref(worktree, resolved);
6810 if (err)
6811 goto done;
6813 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6814 if (err)
6815 goto done;
6817 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6818 worktree->path_prefix);
6819 if (err)
6820 goto done;
6822 err = delete_histedit_refs(worktree, repo);
6823 if (err)
6824 goto done;
6826 err = get_fileindex_path(&fileindex_path, worktree);
6827 if (err)
6828 goto done;
6830 rfa.worktree = worktree;
6831 rfa.fileindex = fileindex;
6832 rfa.progress_cb = progress_cb;
6833 rfa.progress_arg = progress_arg;
6834 rfa.patch_cb = NULL;
6835 rfa.patch_arg = NULL;
6836 rfa.repo = repo;
6837 err = worktree_status(worktree, "", fileindex, repo,
6838 revert_file, &rfa, NULL, NULL, 0, 0);
6839 if (err)
6840 goto sync;
6842 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6843 repo, progress_cb, progress_arg, NULL, NULL);
6844 sync:
6845 sync_err = sync_fileindex(fileindex, fileindex_path);
6846 if (sync_err && err == NULL)
6847 err = sync_err;
6848 done:
6849 got_ref_close(resolved);
6850 free(tree_id);
6851 free(fileindex_path);
6853 unlockerr = lock_worktree(worktree, LOCK_SH);
6854 if (unlockerr && err == NULL)
6855 err = unlockerr;
6856 return err;
6859 const struct got_error *
6860 got_worktree_histedit_complete(struct got_worktree *worktree,
6861 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6862 struct got_reference *edited_branch, struct got_repository *repo)
6864 const struct got_error *err, *unlockerr;
6865 struct got_object_id *new_head_commit_id = NULL;
6866 struct got_reference *resolved = NULL;
6868 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6869 if (err)
6870 return err;
6872 err = got_ref_open(&resolved, repo,
6873 got_ref_get_symref_target(edited_branch), 0);
6874 if (err)
6875 goto done;
6877 err = got_ref_change_ref(resolved, new_head_commit_id);
6878 if (err)
6879 goto done;
6881 err = got_ref_write(resolved, repo);
6882 if (err)
6883 goto done;
6885 err = got_worktree_set_head_ref(worktree, resolved);
6886 if (err)
6887 goto done;
6889 err = delete_histedit_refs(worktree, repo);
6890 done:
6891 if (fileindex)
6892 got_fileindex_free(fileindex);
6893 free(new_head_commit_id);
6894 unlockerr = lock_worktree(worktree, LOCK_SH);
6895 if (unlockerr && err == NULL)
6896 err = unlockerr;
6897 return err;
6900 const struct got_error *
6901 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6902 struct got_object_id *commit_id, struct got_repository *repo)
6904 const struct got_error *err;
6905 char *commit_ref_name;
6907 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6908 if (err)
6909 return err;
6911 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6912 if (err)
6913 goto done;
6915 err = delete_ref(commit_ref_name, repo);
6916 done:
6917 free(commit_ref_name);
6918 return err;
6921 const struct got_error *
6922 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6923 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6924 struct got_worktree *worktree, const char *refname,
6925 struct got_repository *repo)
6927 const struct got_error *err = NULL;
6928 char *fileindex_path = NULL;
6929 struct check_rebase_ok_arg ok_arg;
6931 *fileindex = NULL;
6932 *branch_ref = NULL;
6933 *base_branch_ref = NULL;
6935 err = lock_worktree(worktree, LOCK_EX);
6936 if (err)
6937 return err;
6939 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6940 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6941 "cannot integrate a branch into itself; "
6942 "update -b or different branch name required");
6943 goto done;
6946 err = open_fileindex(fileindex, &fileindex_path, worktree);
6947 if (err)
6948 goto done;
6950 /* Preconditions are the same as for rebase. */
6951 ok_arg.worktree = worktree;
6952 ok_arg.repo = repo;
6953 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6954 &ok_arg);
6955 if (err)
6956 goto done;
6958 err = got_ref_open(branch_ref, repo, refname, 1);
6959 if (err)
6960 goto done;
6962 err = got_ref_open(base_branch_ref, repo,
6963 got_worktree_get_head_ref_name(worktree), 1);
6964 done:
6965 if (err) {
6966 if (*branch_ref) {
6967 got_ref_close(*branch_ref);
6968 *branch_ref = NULL;
6970 if (*base_branch_ref) {
6971 got_ref_close(*base_branch_ref);
6972 *base_branch_ref = NULL;
6974 if (*fileindex) {
6975 got_fileindex_free(*fileindex);
6976 *fileindex = NULL;
6978 lock_worktree(worktree, LOCK_SH);
6980 return err;
6983 const struct got_error *
6984 got_worktree_integrate_continue(struct got_worktree *worktree,
6985 struct got_fileindex *fileindex, struct got_repository *repo,
6986 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6987 got_worktree_checkout_cb progress_cb, void *progress_arg,
6988 got_cancel_cb cancel_cb, void *cancel_arg)
6990 const struct got_error *err = NULL, *sync_err, *unlockerr;
6991 char *fileindex_path = NULL;
6992 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6994 err = get_fileindex_path(&fileindex_path, worktree);
6995 if (err)
6996 goto done;
6998 err = got_ref_resolve(&commit_id, repo, branch_ref);
6999 if (err)
7000 goto done;
7002 err = got_object_id_by_path(&tree_id, repo, commit_id,
7003 worktree->path_prefix);
7004 if (err)
7005 goto done;
7007 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7008 if (err)
7009 goto done;
7011 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7012 progress_cb, progress_arg, cancel_cb, cancel_arg);
7013 if (err)
7014 goto sync;
7016 err = got_ref_change_ref(base_branch_ref, commit_id);
7017 if (err)
7018 goto sync;
7020 err = got_ref_write(base_branch_ref, repo);
7021 sync:
7022 sync_err = sync_fileindex(fileindex, fileindex_path);
7023 if (sync_err && err == NULL)
7024 err = sync_err;
7026 done:
7027 unlockerr = got_ref_unlock(branch_ref);
7028 if (unlockerr && err == NULL)
7029 err = unlockerr;
7030 got_ref_close(branch_ref);
7032 unlockerr = got_ref_unlock(base_branch_ref);
7033 if (unlockerr && err == NULL)
7034 err = unlockerr;
7035 got_ref_close(base_branch_ref);
7037 got_fileindex_free(fileindex);
7038 free(fileindex_path);
7039 free(tree_id);
7041 unlockerr = lock_worktree(worktree, LOCK_SH);
7042 if (unlockerr && err == NULL)
7043 err = unlockerr;
7044 return err;
7047 const struct got_error *
7048 got_worktree_integrate_abort(struct got_worktree *worktree,
7049 struct got_fileindex *fileindex, struct got_repository *repo,
7050 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7052 const struct got_error *err = NULL, *unlockerr = NULL;
7054 got_fileindex_free(fileindex);
7056 err = lock_worktree(worktree, LOCK_SH);
7058 unlockerr = got_ref_unlock(branch_ref);
7059 if (unlockerr && err == NULL)
7060 err = unlockerr;
7061 got_ref_close(branch_ref);
7063 unlockerr = got_ref_unlock(base_branch_ref);
7064 if (unlockerr && err == NULL)
7065 err = unlockerr;
7066 got_ref_close(base_branch_ref);
7068 return err;
7071 struct check_stage_ok_arg {
7072 struct got_object_id *head_commit_id;
7073 struct got_worktree *worktree;
7074 struct got_fileindex *fileindex;
7075 struct got_repository *repo;
7076 int have_changes;
7079 const struct got_error *
7080 check_stage_ok(void *arg, unsigned char status,
7081 unsigned char staged_status, const char *relpath,
7082 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7083 struct got_object_id *commit_id, int dirfd, const char *de_name)
7085 struct check_stage_ok_arg *a = arg;
7086 const struct got_error *err = NULL;
7087 struct got_fileindex_entry *ie;
7088 struct got_object_id base_commit_id;
7089 struct got_object_id *base_commit_idp = NULL;
7090 char *in_repo_path = NULL, *p;
7092 if (status == GOT_STATUS_UNVERSIONED ||
7093 status == GOT_STATUS_NO_CHANGE)
7094 return NULL;
7095 if (status == GOT_STATUS_NONEXISTENT)
7096 return got_error_set_errno(ENOENT, relpath);
7098 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7099 if (ie == NULL)
7100 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7102 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7103 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7104 relpath) == -1)
7105 return got_error_from_errno("asprintf");
7107 if (got_fileindex_entry_has_commit(ie)) {
7108 memcpy(base_commit_id.sha1, ie->commit_sha1,
7109 SHA1_DIGEST_LENGTH);
7110 base_commit_idp = &base_commit_id;
7113 if (status == GOT_STATUS_CONFLICT) {
7114 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7115 goto done;
7116 } else if (status != GOT_STATUS_ADD &&
7117 status != GOT_STATUS_MODIFY &&
7118 status != GOT_STATUS_DELETE) {
7119 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7120 goto done;
7123 a->have_changes = 1;
7125 p = in_repo_path;
7126 while (p[0] == '/')
7127 p++;
7128 err = check_out_of_date(p, status, staged_status,
7129 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7130 GOT_ERR_STAGE_OUT_OF_DATE);
7131 done:
7132 free(in_repo_path);
7133 return err;
7136 struct stage_path_arg {
7137 struct got_worktree *worktree;
7138 struct got_fileindex *fileindex;
7139 struct got_repository *repo;
7140 got_worktree_status_cb status_cb;
7141 void *status_arg;
7142 got_worktree_patch_cb patch_cb;
7143 void *patch_arg;
7144 int staged_something;
7145 int allow_bad_symlinks;
7148 static const struct got_error *
7149 stage_path(void *arg, unsigned char status,
7150 unsigned char staged_status, const char *relpath,
7151 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7152 struct got_object_id *commit_id, int dirfd, const char *de_name)
7154 struct stage_path_arg *a = arg;
7155 const struct got_error *err = NULL;
7156 struct got_fileindex_entry *ie;
7157 char *ondisk_path = NULL, *path_content = NULL;
7158 uint32_t stage;
7159 struct got_object_id *new_staged_blob_id = NULL;
7160 struct stat sb;
7162 if (status == GOT_STATUS_UNVERSIONED)
7163 return NULL;
7165 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7166 if (ie == NULL)
7167 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7169 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7170 relpath)== -1)
7171 return got_error_from_errno("asprintf");
7173 switch (status) {
7174 case GOT_STATUS_ADD:
7175 case GOT_STATUS_MODIFY:
7176 /* XXX could sb.st_mode be passed in by our caller? */
7177 if (lstat(ondisk_path, &sb) == -1) {
7178 err = got_error_from_errno2("lstat", ondisk_path);
7179 break;
7181 if (a->patch_cb) {
7182 if (status == GOT_STATUS_ADD) {
7183 int choice = GOT_PATCH_CHOICE_NONE;
7184 err = (*a->patch_cb)(&choice, a->patch_arg,
7185 status, ie->path, NULL, 1, 1);
7186 if (err)
7187 break;
7188 if (choice != GOT_PATCH_CHOICE_YES)
7189 break;
7190 } else {
7191 err = create_patched_content(&path_content, 0,
7192 staged_blob_id ? staged_blob_id : blob_id,
7193 ondisk_path, dirfd, de_name, ie->path,
7194 a->repo, a->patch_cb, a->patch_arg);
7195 if (err || path_content == NULL)
7196 break;
7199 err = got_object_blob_create(&new_staged_blob_id,
7200 path_content ? path_content : ondisk_path, a->repo);
7201 if (err)
7202 break;
7203 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7204 SHA1_DIGEST_LENGTH);
7205 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7206 stage = GOT_FILEIDX_STAGE_ADD;
7207 else
7208 stage = GOT_FILEIDX_STAGE_MODIFY;
7209 got_fileindex_entry_stage_set(ie, stage);
7210 if (S_ISLNK(sb.st_mode)) {
7211 int is_bad_symlink = 0;
7212 if (!a->allow_bad_symlinks) {
7213 char target_path[PATH_MAX];
7214 ssize_t target_len;
7215 target_len = readlink(ondisk_path, target_path,
7216 sizeof(target_path));
7217 if (target_len == -1) {
7218 err = got_error_from_errno2("readlink",
7219 ondisk_path);
7220 break;
7222 err = is_bad_symlink_target(&is_bad_symlink,
7223 target_path, target_len, ondisk_path,
7224 a->worktree->root_path);
7225 if (err)
7226 break;
7227 if (is_bad_symlink) {
7228 err = got_error_path(ondisk_path,
7229 GOT_ERR_BAD_SYMLINK);
7230 break;
7233 if (is_bad_symlink)
7234 got_fileindex_entry_staged_filetype_set(ie,
7235 GOT_FILEIDX_MODE_BAD_SYMLINK);
7236 else
7237 got_fileindex_entry_staged_filetype_set(ie,
7238 GOT_FILEIDX_MODE_SYMLINK);
7239 } else {
7240 got_fileindex_entry_staged_filetype_set(ie,
7241 GOT_FILEIDX_MODE_REGULAR_FILE);
7243 a->staged_something = 1;
7244 if (a->status_cb == NULL)
7245 break;
7246 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7247 get_staged_status(ie), relpath, blob_id,
7248 new_staged_blob_id, NULL, dirfd, de_name);
7249 break;
7250 case GOT_STATUS_DELETE:
7251 if (staged_status == GOT_STATUS_DELETE)
7252 break;
7253 if (a->patch_cb) {
7254 int choice = GOT_PATCH_CHOICE_NONE;
7255 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7256 ie->path, NULL, 1, 1);
7257 if (err)
7258 break;
7259 if (choice == GOT_PATCH_CHOICE_NO)
7260 break;
7261 if (choice != GOT_PATCH_CHOICE_YES) {
7262 err = got_error(GOT_ERR_PATCH_CHOICE);
7263 break;
7266 stage = GOT_FILEIDX_STAGE_DELETE;
7267 got_fileindex_entry_stage_set(ie, stage);
7268 a->staged_something = 1;
7269 if (a->status_cb == NULL)
7270 break;
7271 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7272 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7273 de_name);
7274 break;
7275 case GOT_STATUS_NO_CHANGE:
7276 break;
7277 case GOT_STATUS_CONFLICT:
7278 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7279 break;
7280 case GOT_STATUS_NONEXISTENT:
7281 err = got_error_set_errno(ENOENT, relpath);
7282 break;
7283 default:
7284 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7285 break;
7288 if (path_content && unlink(path_content) == -1 && err == NULL)
7289 err = got_error_from_errno2("unlink", path_content);
7290 free(path_content);
7291 free(ondisk_path);
7292 free(new_staged_blob_id);
7293 return err;
7296 const struct got_error *
7297 got_worktree_stage(struct got_worktree *worktree,
7298 struct got_pathlist_head *paths,
7299 got_worktree_status_cb status_cb, void *status_arg,
7300 got_worktree_patch_cb patch_cb, void *patch_arg,
7301 int allow_bad_symlinks, struct got_repository *repo)
7303 const struct got_error *err = NULL, *sync_err, *unlockerr;
7304 struct got_pathlist_entry *pe;
7305 struct got_fileindex *fileindex = NULL;
7306 char *fileindex_path = NULL;
7307 struct got_reference *head_ref = NULL;
7308 struct got_object_id *head_commit_id = NULL;
7309 struct check_stage_ok_arg oka;
7310 struct stage_path_arg spa;
7312 err = lock_worktree(worktree, LOCK_EX);
7313 if (err)
7314 return err;
7316 err = got_ref_open(&head_ref, repo,
7317 got_worktree_get_head_ref_name(worktree), 0);
7318 if (err)
7319 goto done;
7320 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7321 if (err)
7322 goto done;
7323 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7324 if (err)
7325 goto done;
7327 /* Check pre-conditions before staging anything. */
7328 oka.head_commit_id = head_commit_id;
7329 oka.worktree = worktree;
7330 oka.fileindex = fileindex;
7331 oka.repo = repo;
7332 oka.have_changes = 0;
7333 TAILQ_FOREACH(pe, paths, entry) {
7334 err = worktree_status(worktree, pe->path, fileindex, repo,
7335 check_stage_ok, &oka, NULL, NULL, 0, 0);
7336 if (err)
7337 goto done;
7339 if (!oka.have_changes) {
7340 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7341 goto done;
7344 spa.worktree = worktree;
7345 spa.fileindex = fileindex;
7346 spa.repo = repo;
7347 spa.patch_cb = patch_cb;
7348 spa.patch_arg = patch_arg;
7349 spa.status_cb = status_cb;
7350 spa.status_arg = status_arg;
7351 spa.staged_something = 0;
7352 spa.allow_bad_symlinks = allow_bad_symlinks;
7353 TAILQ_FOREACH(pe, paths, entry) {
7354 err = worktree_status(worktree, pe->path, fileindex, repo,
7355 stage_path, &spa, NULL, NULL, 0, 0);
7356 if (err)
7357 goto done;
7359 if (!spa.staged_something) {
7360 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7361 goto done;
7364 sync_err = sync_fileindex(fileindex, fileindex_path);
7365 if (sync_err && err == NULL)
7366 err = sync_err;
7367 done:
7368 if (head_ref)
7369 got_ref_close(head_ref);
7370 free(head_commit_id);
7371 free(fileindex_path);
7372 if (fileindex)
7373 got_fileindex_free(fileindex);
7374 unlockerr = lock_worktree(worktree, LOCK_SH);
7375 if (unlockerr && err == NULL)
7376 err = unlockerr;
7377 return err;
7380 struct unstage_path_arg {
7381 struct got_worktree *worktree;
7382 struct got_fileindex *fileindex;
7383 struct got_repository *repo;
7384 got_worktree_checkout_cb progress_cb;
7385 void *progress_arg;
7386 got_worktree_patch_cb patch_cb;
7387 void *patch_arg;
7390 static const struct got_error *
7391 create_unstaged_content(char **path_unstaged_content,
7392 char **path_new_staged_content, struct got_object_id *blob_id,
7393 struct got_object_id *staged_blob_id, const char *relpath,
7394 struct got_repository *repo,
7395 got_worktree_patch_cb patch_cb, void *patch_arg)
7397 const struct got_error *err;
7398 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7399 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7400 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7401 struct stat sb1, sb2;
7402 struct got_diff_changes *changes = NULL;
7403 struct got_diff_state *ds = NULL;
7404 struct got_diff_args *args = NULL;
7405 struct got_diff_change *change;
7406 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7407 int have_content = 0, have_rejected_content = 0;
7409 *path_unstaged_content = NULL;
7410 *path_new_staged_content = NULL;
7412 err = got_object_id_str(&label1, blob_id);
7413 if (err)
7414 return err;
7415 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7416 if (err)
7417 goto done;
7419 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7420 if (err)
7421 goto done;
7423 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7424 if (err)
7425 goto done;
7427 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7428 if (err)
7429 goto done;
7431 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7432 if (err)
7433 goto done;
7435 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7436 if (err)
7437 goto done;
7439 if (stat(path1, &sb1) == -1) {
7440 err = got_error_from_errno2("stat", path1);
7441 goto done;
7444 if (stat(path2, &sb2) == -1) {
7445 err = got_error_from_errno2("stat", path2);
7446 goto done;
7449 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7450 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7451 if (err)
7452 goto done;
7454 err = got_opentemp_named(path_unstaged_content, &outfile,
7455 "got-unstaged-content");
7456 if (err)
7457 goto done;
7458 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7459 "got-new-staged-content");
7460 if (err)
7461 goto done;
7463 if (fseek(f1, 0L, SEEK_SET) == -1) {
7464 err = got_ferror(f1, GOT_ERR_IO);
7465 goto done;
7467 if (fseek(f2, 0L, SEEK_SET) == -1) {
7468 err = got_ferror(f2, GOT_ERR_IO);
7469 goto done;
7471 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7472 int choice;
7473 err = apply_or_reject_change(&choice, change, ++n,
7474 changes->nchanges, ds, args, diff_flags, relpath,
7475 f1, f2, &line_cur1, &line_cur2,
7476 outfile, rejectfile, patch_cb, patch_arg);
7477 if (err)
7478 goto done;
7479 if (choice == GOT_PATCH_CHOICE_YES)
7480 have_content = 1;
7481 else
7482 have_rejected_content = 1;
7483 if (choice == GOT_PATCH_CHOICE_QUIT)
7484 break;
7486 if (have_content || have_rejected_content)
7487 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7488 outfile, rejectfile);
7489 done:
7490 free(label1);
7491 if (blob)
7492 got_object_blob_close(blob);
7493 if (staged_blob)
7494 got_object_blob_close(staged_blob);
7495 if (f1 && fclose(f1) == EOF && err == NULL)
7496 err = got_error_from_errno2("fclose", path1);
7497 if (f2 && fclose(f2) == EOF && err == NULL)
7498 err = got_error_from_errno2("fclose", path2);
7499 if (outfile && fclose(outfile) == EOF && err == NULL)
7500 err = got_error_from_errno2("fclose", *path_unstaged_content);
7501 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7502 err = got_error_from_errno2("fclose", *path_new_staged_content);
7503 if (path1 && unlink(path1) == -1 && err == NULL)
7504 err = got_error_from_errno2("unlink", path1);
7505 if (path2 && unlink(path2) == -1 && err == NULL)
7506 err = got_error_from_errno2("unlink", path2);
7507 if (err || !have_content) {
7508 if (*path_unstaged_content &&
7509 unlink(*path_unstaged_content) == -1 && err == NULL)
7510 err = got_error_from_errno2("unlink",
7511 *path_unstaged_content);
7512 free(*path_unstaged_content);
7513 *path_unstaged_content = NULL;
7515 if (err || !have_content || !have_rejected_content) {
7516 if (*path_new_staged_content &&
7517 unlink(*path_new_staged_content) == -1 && err == NULL)
7518 err = got_error_from_errno2("unlink",
7519 *path_new_staged_content);
7520 free(*path_new_staged_content);
7521 *path_new_staged_content = NULL;
7523 free(args);
7524 if (ds) {
7525 got_diff_state_free(ds);
7526 free(ds);
7528 if (changes)
7529 got_diff_free_changes(changes);
7530 free(path1);
7531 free(path2);
7532 return err;
7535 static const struct got_error *
7536 unstage_hunks(struct got_object_id *staged_blob_id,
7537 struct got_blob_object *blob_base,
7538 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7539 const char *ondisk_path, const char *label_orig,
7540 struct got_worktree *worktree, struct got_repository *repo,
7541 got_worktree_patch_cb patch_cb, void *patch_arg,
7542 got_worktree_checkout_cb progress_cb, void *progress_arg)
7544 const struct got_error *err = NULL;
7545 char *path_unstaged_content = NULL;
7546 char *path_new_staged_content = NULL;
7547 struct got_object_id *new_staged_blob_id = NULL;
7548 FILE *f = NULL;
7549 struct stat sb;
7551 err = create_unstaged_content(&path_unstaged_content,
7552 &path_new_staged_content, blob_id, staged_blob_id,
7553 ie->path, repo, patch_cb, patch_arg);
7554 if (err)
7555 return err;
7557 if (path_unstaged_content == NULL)
7558 return NULL;
7560 if (path_new_staged_content) {
7561 err = got_object_blob_create(&new_staged_blob_id,
7562 path_new_staged_content, repo);
7563 if (err)
7564 goto done;
7567 f = fopen(path_unstaged_content, "r");
7568 if (f == NULL) {
7569 err = got_error_from_errno2("fopen",
7570 path_unstaged_content);
7571 goto done;
7573 if (fstat(fileno(f), &sb) == -1) {
7574 err = got_error_from_errno2("fstat", path_unstaged_content);
7575 goto done;
7577 if (got_fileindex_entry_staged_filetype_get(ie) ==
7578 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7579 char link_target[PATH_MAX];
7580 size_t r;
7581 r = fread(link_target, 1, sizeof(link_target), f);
7582 if (r == 0 && ferror(f)) {
7583 err = got_error_from_errno("fread");
7584 goto done;
7586 if (r >= sizeof(link_target)) { /* should not happen */
7587 err = got_error(GOT_ERR_NO_SPACE);
7588 goto done;
7590 link_target[r] = '\0';
7591 err = merge_symlink(worktree, blob_base,
7592 ondisk_path, ie->path, label_orig, link_target,
7593 worktree->base_commit_id, repo, progress_cb,
7594 progress_arg);
7595 } else {
7596 int local_changes_subsumed;
7597 err = merge_file(&local_changes_subsumed, worktree,
7598 blob_base, ondisk_path, ie->path,
7599 got_fileindex_perms_to_st(ie),
7600 path_unstaged_content, label_orig, "unstaged",
7601 repo, progress_cb, progress_arg);
7603 if (err)
7604 goto done;
7606 if (new_staged_blob_id) {
7607 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7608 SHA1_DIGEST_LENGTH);
7609 } else {
7610 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7611 got_fileindex_entry_staged_filetype_set(ie, 0);
7613 done:
7614 free(new_staged_blob_id);
7615 if (path_unstaged_content &&
7616 unlink(path_unstaged_content) == -1 && err == NULL)
7617 err = got_error_from_errno2("unlink", path_unstaged_content);
7618 if (path_new_staged_content &&
7619 unlink(path_new_staged_content) == -1 && err == NULL)
7620 err = got_error_from_errno2("unlink", path_new_staged_content);
7621 if (f && fclose(f) != 0 && err == NULL)
7622 err = got_error_from_errno2("fclose", path_unstaged_content);
7623 free(path_unstaged_content);
7624 free(path_new_staged_content);
7625 return err;
7628 static const struct got_error *
7629 unstage_path(void *arg, unsigned char status,
7630 unsigned char staged_status, const char *relpath,
7631 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7632 struct got_object_id *commit_id, int dirfd, const char *de_name)
7634 const struct got_error *err = NULL;
7635 struct unstage_path_arg *a = arg;
7636 struct got_fileindex_entry *ie;
7637 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7638 char *ondisk_path = NULL;
7639 char *id_str = NULL, *label_orig = NULL;
7640 int local_changes_subsumed;
7641 struct stat sb;
7643 if (staged_status != GOT_STATUS_ADD &&
7644 staged_status != GOT_STATUS_MODIFY &&
7645 staged_status != GOT_STATUS_DELETE)
7646 return NULL;
7648 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7649 if (ie == NULL)
7650 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7652 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7653 == -1)
7654 return got_error_from_errno("asprintf");
7656 err = got_object_id_str(&id_str,
7657 commit_id ? commit_id : a->worktree->base_commit_id);
7658 if (err)
7659 goto done;
7660 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7661 id_str) == -1) {
7662 err = got_error_from_errno("asprintf");
7663 goto done;
7666 switch (staged_status) {
7667 case GOT_STATUS_MODIFY:
7668 err = got_object_open_as_blob(&blob_base, a->repo,
7669 blob_id, 8192);
7670 if (err)
7671 break;
7672 /* fall through */
7673 case GOT_STATUS_ADD:
7674 if (a->patch_cb) {
7675 if (staged_status == GOT_STATUS_ADD) {
7676 int choice = GOT_PATCH_CHOICE_NONE;
7677 err = (*a->patch_cb)(&choice, a->patch_arg,
7678 staged_status, ie->path, NULL, 1, 1);
7679 if (err)
7680 break;
7681 if (choice != GOT_PATCH_CHOICE_YES)
7682 break;
7683 } else {
7684 err = unstage_hunks(staged_blob_id,
7685 blob_base, blob_id, ie, ondisk_path,
7686 label_orig, a->worktree, a->repo,
7687 a->patch_cb, a->patch_arg,
7688 a->progress_cb, a->progress_arg);
7689 break; /* Done with this file. */
7692 err = got_object_open_as_blob(&blob_staged, a->repo,
7693 staged_blob_id, 8192);
7694 if (err)
7695 break;
7696 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7697 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7698 case GOT_FILEIDX_MODE_REGULAR_FILE:
7699 err = merge_blob(&local_changes_subsumed, a->worktree,
7700 blob_base, ondisk_path, relpath,
7701 got_fileindex_perms_to_st(ie), label_orig,
7702 blob_staged, commit_id ? commit_id :
7703 a->worktree->base_commit_id, a->repo,
7704 a->progress_cb, a->progress_arg);
7705 break;
7706 case GOT_FILEIDX_MODE_SYMLINK:
7707 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7708 char *staged_target;
7709 err = got_object_blob_read_to_str(
7710 &staged_target, blob_staged);
7711 if (err)
7712 goto done;
7713 err = merge_symlink(a->worktree, blob_base,
7714 ondisk_path, relpath, label_orig,
7715 staged_target, commit_id ? commit_id :
7716 a->worktree->base_commit_id,
7717 a->repo, a->progress_cb, a->progress_arg);
7718 free(staged_target);
7719 } else {
7720 err = merge_blob(&local_changes_subsumed,
7721 a->worktree, blob_base, ondisk_path,
7722 relpath, got_fileindex_perms_to_st(ie),
7723 label_orig, blob_staged,
7724 commit_id ? commit_id :
7725 a->worktree->base_commit_id, a->repo,
7726 a->progress_cb, a->progress_arg);
7728 break;
7729 default:
7730 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7731 break;
7733 if (err == NULL) {
7734 got_fileindex_entry_stage_set(ie,
7735 GOT_FILEIDX_STAGE_NONE);
7736 got_fileindex_entry_staged_filetype_set(ie, 0);
7738 break;
7739 case GOT_STATUS_DELETE:
7740 if (a->patch_cb) {
7741 int choice = GOT_PATCH_CHOICE_NONE;
7742 err = (*a->patch_cb)(&choice, a->patch_arg,
7743 staged_status, ie->path, NULL, 1, 1);
7744 if (err)
7745 break;
7746 if (choice == GOT_PATCH_CHOICE_NO)
7747 break;
7748 if (choice != GOT_PATCH_CHOICE_YES) {
7749 err = got_error(GOT_ERR_PATCH_CHOICE);
7750 break;
7753 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7754 got_fileindex_entry_staged_filetype_set(ie, 0);
7755 err = get_file_status(&status, &sb, ie, ondisk_path,
7756 dirfd, de_name, a->repo);
7757 if (err)
7758 break;
7759 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7760 break;
7762 done:
7763 free(ondisk_path);
7764 if (blob_base)
7765 got_object_blob_close(blob_base);
7766 if (blob_staged)
7767 got_object_blob_close(blob_staged);
7768 free(id_str);
7769 free(label_orig);
7770 return err;
7773 const struct got_error *
7774 got_worktree_unstage(struct got_worktree *worktree,
7775 struct got_pathlist_head *paths,
7776 got_worktree_checkout_cb progress_cb, void *progress_arg,
7777 got_worktree_patch_cb patch_cb, void *patch_arg,
7778 struct got_repository *repo)
7780 const struct got_error *err = NULL, *sync_err, *unlockerr;
7781 struct got_pathlist_entry *pe;
7782 struct got_fileindex *fileindex = NULL;
7783 char *fileindex_path = NULL;
7784 struct unstage_path_arg upa;
7786 err = lock_worktree(worktree, LOCK_EX);
7787 if (err)
7788 return err;
7790 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7791 if (err)
7792 goto done;
7794 upa.worktree = worktree;
7795 upa.fileindex = fileindex;
7796 upa.repo = repo;
7797 upa.progress_cb = progress_cb;
7798 upa.progress_arg = progress_arg;
7799 upa.patch_cb = patch_cb;
7800 upa.patch_arg = patch_arg;
7801 TAILQ_FOREACH(pe, paths, entry) {
7802 err = worktree_status(worktree, pe->path, fileindex, repo,
7803 unstage_path, &upa, NULL, NULL, 0, 0);
7804 if (err)
7805 goto done;
7808 sync_err = sync_fileindex(fileindex, fileindex_path);
7809 if (sync_err && err == NULL)
7810 err = sync_err;
7811 done:
7812 free(fileindex_path);
7813 if (fileindex)
7814 got_fileindex_free(fileindex);
7815 unlockerr = lock_worktree(worktree, LOCK_SH);
7816 if (unlockerr && err == NULL)
7817 err = unlockerr;
7818 return err;
7821 struct report_file_info_arg {
7822 struct got_worktree *worktree;
7823 got_worktree_path_info_cb info_cb;
7824 void *info_arg;
7825 struct got_pathlist_head *paths;
7826 got_cancel_cb cancel_cb;
7827 void *cancel_arg;
7830 static const struct got_error *
7831 report_file_info(void *arg, struct got_fileindex_entry *ie)
7833 struct report_file_info_arg *a = arg;
7834 struct got_pathlist_entry *pe;
7835 struct got_object_id blob_id, staged_blob_id, commit_id;
7836 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7837 struct got_object_id *commit_idp = NULL;
7838 int stage;
7840 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7841 return got_error(GOT_ERR_CANCELLED);
7843 TAILQ_FOREACH(pe, a->paths, entry) {
7844 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7845 got_path_is_child(ie->path, pe->path, pe->path_len))
7846 break;
7848 if (pe == NULL) /* not found */
7849 return NULL;
7851 if (got_fileindex_entry_has_blob(ie)) {
7852 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7853 blob_idp = &blob_id;
7855 stage = got_fileindex_entry_stage_get(ie);
7856 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7857 stage == GOT_FILEIDX_STAGE_ADD) {
7858 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7859 SHA1_DIGEST_LENGTH);
7860 staged_blob_idp = &staged_blob_id;
7863 if (got_fileindex_entry_has_commit(ie)) {
7864 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7865 commit_idp = &commit_id;
7868 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7869 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7872 const struct got_error *
7873 got_worktree_path_info(struct got_worktree *worktree,
7874 struct got_pathlist_head *paths,
7875 got_worktree_path_info_cb info_cb, void *info_arg,
7876 got_cancel_cb cancel_cb, void *cancel_arg)
7879 const struct got_error *err = NULL, *unlockerr;
7880 struct got_fileindex *fileindex = NULL;
7881 char *fileindex_path = NULL;
7882 struct report_file_info_arg arg;
7884 err = lock_worktree(worktree, LOCK_SH);
7885 if (err)
7886 return err;
7888 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7889 if (err)
7890 goto done;
7892 arg.worktree = worktree;
7893 arg.info_cb = info_cb;
7894 arg.info_arg = info_arg;
7895 arg.paths = paths;
7896 arg.cancel_cb = cancel_cb;
7897 arg.cancel_arg = cancel_arg;
7898 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7899 &arg);
7900 done:
7901 free(fileindex_path);
7902 if (fileindex)
7903 got_fileindex_free(fileindex);
7904 unlockerr = lock_worktree(worktree, LOCK_UN);
7905 if (unlockerr && err == NULL)
7906 err = unlockerr;
7907 return err;