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);
433 (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
434 if ((*worktree)->root_fd == -1) {
435 err = got_error_from_errno2("open", (*worktree)->root_path);
436 goto done;
438 done:
439 if (repo)
440 got_repo_close(repo);
441 free(path_got);
442 free(path_lock);
443 free(base_commit_id_str);
444 free(uuidstr);
445 free(formatstr);
446 if (err) {
447 if (fd != -1)
448 close(fd);
449 if (*worktree != NULL)
450 got_worktree_close(*worktree);
451 *worktree = NULL;
452 } else
453 (*worktree)->lockfd = fd;
455 return err;
458 const struct got_error *
459 got_worktree_open(struct got_worktree **worktree, const char *path)
461 const struct got_error *err = NULL;
462 char *worktree_path;
464 worktree_path = strdup(path);
465 if (worktree_path == NULL)
466 return got_error_from_errno("strdup");
468 for (;;) {
469 char *parent_path;
471 err = open_worktree(worktree, worktree_path);
472 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
473 free(worktree_path);
474 return err;
476 if (*worktree) {
477 free(worktree_path);
478 return NULL;
480 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
481 break;
482 err = got_path_dirname(&parent_path, worktree_path);
483 if (err) {
484 if (err->code != GOT_ERR_BAD_PATH) {
485 free(worktree_path);
486 return err;
488 break;
490 free(worktree_path);
491 worktree_path = parent_path;
494 free(worktree_path);
495 return got_error(GOT_ERR_NOT_WORKTREE);
498 const struct got_error *
499 got_worktree_close(struct got_worktree *worktree)
501 const struct got_error *err = NULL;
502 free(worktree->repo_path);
503 free(worktree->path_prefix);
504 free(worktree->base_commit_id);
505 free(worktree->head_ref_name);
506 if (worktree->lockfd != -1)
507 if (close(worktree->lockfd) != 0)
508 err = got_error_from_errno2("close",
509 got_worktree_get_root_path(worktree));
510 free(worktree->root_path);
511 free(worktree->gotconfig_path);
512 got_gotconfig_free(worktree->gotconfig);
513 free(worktree);
514 close(worktree->root_fd);
515 return err;
518 const char *
519 got_worktree_get_root_path(struct got_worktree *worktree)
521 return worktree->root_path;
524 const char *
525 got_worktree_get_repo_path(struct got_worktree *worktree)
527 return worktree->repo_path;
529 const char *
530 got_worktree_get_path_prefix(struct got_worktree *worktree)
532 return worktree->path_prefix;
535 const struct got_error *
536 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
537 const char *path_prefix)
539 char *absprefix = NULL;
541 if (!got_path_is_absolute(path_prefix)) {
542 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
543 return got_error_from_errno("asprintf");
545 *match = (strcmp(absprefix ? absprefix : path_prefix,
546 worktree->path_prefix) == 0);
547 free(absprefix);
548 return NULL;
551 const char *
552 got_worktree_get_head_ref_name(struct got_worktree *worktree)
554 return worktree->head_ref_name;
557 const struct got_error *
558 got_worktree_set_head_ref(struct got_worktree *worktree,
559 struct got_reference *head_ref)
561 const struct got_error *err = NULL;
562 char *path_got = NULL, *head_ref_name = NULL;
564 if (asprintf(&path_got, "%s/%s", worktree->root_path,
565 GOT_WORKTREE_GOT_DIR) == -1) {
566 err = got_error_from_errno("asprintf");
567 path_got = NULL;
568 goto done;
571 head_ref_name = strdup(got_ref_get_name(head_ref));
572 if (head_ref_name == NULL) {
573 err = got_error_from_errno("strdup");
574 goto done;
577 err = write_head_ref(path_got, head_ref);
578 if (err)
579 goto done;
581 free(worktree->head_ref_name);
582 worktree->head_ref_name = head_ref_name;
583 done:
584 free(path_got);
585 if (err)
586 free(head_ref_name);
587 return err;
590 struct got_object_id *
591 got_worktree_get_base_commit_id(struct got_worktree *worktree)
593 return worktree->base_commit_id;
596 const struct got_error *
597 got_worktree_set_base_commit_id(struct got_worktree *worktree,
598 struct got_repository *repo, struct got_object_id *commit_id)
600 const struct got_error *err;
601 struct got_object *obj = NULL;
602 char *id_str = NULL;
603 char *path_got = NULL;
605 if (asprintf(&path_got, "%s/%s", worktree->root_path,
606 GOT_WORKTREE_GOT_DIR) == -1) {
607 err = got_error_from_errno("asprintf");
608 path_got = NULL;
609 goto done;
612 err = got_object_open(&obj, repo, commit_id);
613 if (err)
614 return err;
616 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
617 err = got_error(GOT_ERR_OBJ_TYPE);
618 goto done;
621 /* Record our base commit. */
622 err = got_object_id_str(&id_str, commit_id);
623 if (err)
624 goto done;
625 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
626 if (err)
627 goto done;
629 free(worktree->base_commit_id);
630 worktree->base_commit_id = got_object_id_dup(commit_id);
631 if (worktree->base_commit_id == NULL) {
632 err = got_error_from_errno("got_object_id_dup");
633 goto done;
635 done:
636 if (obj)
637 got_object_close(obj);
638 free(id_str);
639 free(path_got);
640 return err;
643 const struct got_gotconfig *
644 got_worktree_get_gotconfig(struct got_worktree *worktree)
646 return worktree->gotconfig;
649 static const struct got_error *
650 lock_worktree(struct got_worktree *worktree, int operation)
652 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
653 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
654 : got_error_from_errno2("flock",
655 got_worktree_get_root_path(worktree)));
656 return NULL;
659 static const struct got_error *
660 add_dir_on_disk(struct got_worktree *worktree, const char *path)
662 const struct got_error *err = NULL;
663 char *abspath;
665 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
666 return got_error_from_errno("asprintf");
668 err = got_path_mkdir(abspath);
669 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
670 struct stat sb;
671 err = NULL;
672 if (lstat(abspath, &sb) == -1) {
673 err = got_error_from_errno2("lstat", abspath);
674 } else if (!S_ISDIR(sb.st_mode)) {
675 /* TODO directory is obstructed; do something */
676 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
679 free(abspath);
680 return err;
683 static const struct got_error *
684 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
686 const struct got_error *err = NULL;
687 uint8_t fbuf1[8192];
688 uint8_t fbuf2[8192];
689 size_t flen1 = 0, flen2 = 0;
691 *same = 1;
693 for (;;) {
694 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
695 if (flen1 == 0 && ferror(f1)) {
696 err = got_error_from_errno("fread");
697 break;
699 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
700 if (flen2 == 0 && ferror(f2)) {
701 err = got_error_from_errno("fread");
702 break;
704 if (flen1 == 0) {
705 if (flen2 != 0)
706 *same = 0;
707 break;
708 } else if (flen2 == 0) {
709 if (flen1 != 0)
710 *same = 0;
711 break;
712 } else if (flen1 == flen2) {
713 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
714 *same = 0;
715 break;
717 } else {
718 *same = 0;
719 break;
723 return err;
726 static const struct got_error *
727 check_files_equal(int *same, const char *f1_path, const char *f2_path)
729 const struct got_error *err = NULL;
730 struct stat sb;
731 size_t size1, size2;
732 FILE *f1 = NULL, *f2 = NULL;
734 *same = 1;
736 if (lstat(f1_path, &sb) != 0) {
737 err = got_error_from_errno2("lstat", f1_path);
738 goto done;
740 size1 = sb.st_size;
742 if (lstat(f2_path, &sb) != 0) {
743 err = got_error_from_errno2("lstat", f2_path);
744 goto done;
746 size2 = sb.st_size;
748 if (size1 != size2) {
749 *same = 0;
750 return NULL;
753 f1 = fopen(f1_path, "r");
754 if (f1 == NULL)
755 return got_error_from_errno2("fopen", f1_path);
757 f2 = fopen(f2_path, "r");
758 if (f2 == NULL) {
759 err = got_error_from_errno2("fopen", f2_path);
760 goto done;
763 err = check_file_contents_equal(same, f1, f2);
764 done:
765 if (f1 && fclose(f1) != 0 && err == NULL)
766 err = got_error_from_errno("fclose");
767 if (f2 && fclose(f2) != 0 && err == NULL)
768 err = got_error_from_errno("fclose");
770 return err;
773 /*
774 * Perform a 3-way merge where blob_orig acts as the common ancestor,
775 * the file at deriv_path acts as the first derived version, and the
776 * file on disk acts as the second derived version.
777 */
778 static const struct got_error *
779 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
780 struct got_blob_object *blob_orig, const char *ondisk_path,
781 const char *path, uint16_t st_mode, const char *deriv_path,
782 const char *label_orig, const char *label_deriv,
783 struct got_repository *repo,
784 got_worktree_checkout_cb progress_cb, void *progress_arg)
786 const struct got_error *err = NULL;
787 int merged_fd = -1;
788 FILE *f_orig = NULL;
789 char *blob_orig_path = NULL;
790 char *merged_path = NULL, *base_path = NULL;
791 int overlapcnt = 0;
792 char *parent = NULL;
793 char *symlink_path = NULL;
794 FILE *symlinkf = NULL;
796 *local_changes_subsumed = 0;
798 err = got_path_dirname(&parent, ondisk_path);
799 if (err)
800 return err;
802 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
803 err = got_error_from_errno("asprintf");
804 goto done;
807 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
808 if (err)
809 goto done;
811 free(base_path);
812 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
813 err = got_error_from_errno("asprintf");
814 base_path = NULL;
815 goto done;
818 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
819 if (err)
820 goto done;
821 if (blob_orig) {
822 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
823 blob_orig);
824 if (err)
825 goto done;
826 } else {
827 /*
828 * If the file has no blob, this is an "add vs add" conflict,
829 * and we simply use an empty ancestor file to make both files
830 * appear in the merged result in their entirety.
831 */
834 /*
835 * In order the run a 3-way merge with a symlink we copy the symlink's
836 * target path into a temporary file and use that file with diff3.
837 */
838 if (S_ISLNK(st_mode)) {
839 char target_path[PATH_MAX];
840 ssize_t target_len;
841 size_t n;
843 free(base_path);
844 if (asprintf(&base_path, "%s/got-symlink-merge",
845 parent) == -1) {
846 err = got_error_from_errno("asprintf");
847 base_path = NULL;
848 goto done;
850 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
851 if (err)
852 goto done;
853 target_len = readlink(ondisk_path, target_path,
854 sizeof(target_path));
855 if (target_len == -1) {
856 err = got_error_from_errno2("readlink", ondisk_path);
857 goto done;
859 n = fwrite(target_path, 1, target_len, symlinkf);
860 if (n != target_len) {
861 err = got_ferror(symlinkf, GOT_ERR_IO);
862 goto done;
864 if (fflush(symlinkf) == EOF) {
865 err = got_error_from_errno2("fflush", symlink_path);
866 goto done;
870 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
871 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
872 label_deriv, label_orig, NULL);
873 if (err)
874 goto done;
876 err = (*progress_cb)(progress_arg,
877 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
878 if (err)
879 goto done;
881 if (fsync(merged_fd) != 0) {
882 err = got_error_from_errno("fsync");
883 goto done;
886 /* Check if a clean merge has subsumed all local changes. */
887 if (overlapcnt == 0) {
888 err = check_files_equal(local_changes_subsumed, deriv_path,
889 merged_path);
890 if (err)
891 goto done;
894 if (fchmod(merged_fd, st_mode) != 0) {
895 err = got_error_from_errno2("fchmod", merged_path);
896 goto done;
899 if (rename(merged_path, ondisk_path) != 0) {
900 err = got_error_from_errno3("rename", merged_path,
901 ondisk_path);
902 goto done;
904 done:
905 if (err) {
906 if (merged_path)
907 unlink(merged_path);
909 if (symlink_path) {
910 if (unlink(symlink_path) == -1 && err == NULL)
911 err = got_error_from_errno2("unlink", symlink_path);
913 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
914 err = got_error_from_errno2("fclose", symlink_path);
915 free(symlink_path);
916 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
917 err = got_error_from_errno("close");
918 if (f_orig && fclose(f_orig) != 0 && err == NULL)
919 err = got_error_from_errno("fclose");
920 free(merged_path);
921 free(base_path);
922 if (blob_orig_path) {
923 unlink(blob_orig_path);
924 free(blob_orig_path);
926 free(parent);
927 return err;
930 static const struct got_error *
931 update_symlink(const char *ondisk_path, const char *target_path,
932 size_t target_len)
934 /* This is not atomic but matches what 'ln -sf' does. */
935 if (unlink(ondisk_path) == -1)
936 return got_error_from_errno2("unlink", ondisk_path);
937 if (symlink(target_path, ondisk_path) == -1)
938 return got_error_from_errno3("symlink", target_path,
939 ondisk_path);
940 return NULL;
943 /*
944 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
945 * in the work tree with a file that contains conflict markers and the
946 * conflicting target paths of the original version, a "derived version"
947 * of a symlink from an incoming change, and a local version of the symlink.
949 * The original versions's target path can be NULL if it is not available,
950 * such as if both derived versions added a new symlink at the same path.
952 * The incoming derived symlink target is NULL in case the incoming change
953 * has deleted this symlink.
954 */
955 static const struct got_error *
956 install_symlink_conflict(const char *deriv_target,
957 struct got_object_id *deriv_base_commit_id, const char *orig_target,
958 const char *label_orig, const char *local_target, const char *ondisk_path)
960 const struct got_error *err;
961 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
962 FILE *f = NULL;
964 err = got_object_id_str(&id_str, deriv_base_commit_id);
965 if (err)
966 return got_error_from_errno("asprintf");
968 if (asprintf(&label_deriv, "%s: commit %s",
969 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
970 err = got_error_from_errno("asprintf");
971 goto done;
974 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
975 if (err)
976 goto done;
978 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
979 err = got_error_from_errno2("fchmod", path);
980 goto done;
983 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
984 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
985 deriv_target ? deriv_target : "(symlink was deleted)",
986 orig_target ? label_orig : "",
987 orig_target ? "\n" : "",
988 orig_target ? orig_target : "",
989 orig_target ? "\n" : "",
990 GOT_DIFF_CONFLICT_MARKER_SEP,
991 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
992 err = got_error_from_errno2("fprintf", path);
993 goto done;
996 if (unlink(ondisk_path) == -1) {
997 err = got_error_from_errno2("unlink", ondisk_path);
998 goto done;
1000 if (rename(path, ondisk_path) == -1) {
1001 err = got_error_from_errno3("rename", path, ondisk_path);
1002 goto done;
1004 done:
1005 if (f != NULL && fclose(f) == EOF && err == NULL)
1006 err = got_error_from_errno2("fclose", path);
1007 free(path);
1008 free(id_str);
1009 free(label_deriv);
1010 return err;
1013 /* forward declaration */
1014 static const struct got_error *
1015 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1016 const char *, const char *, uint16_t, const char *,
1017 struct got_blob_object *, struct got_object_id *,
1018 struct got_repository *, got_worktree_checkout_cb, void *);
1021 * Merge a symlink into the work tree, where blob_orig acts as the common
1022 * ancestor, deriv_target is the link target of the first derived version,
1023 * and the symlink on disk acts as the second derived version.
1024 * Assume that contents of both blobs represent symlinks.
1026 static const struct got_error *
1027 merge_symlink(struct got_worktree *worktree,
1028 struct got_blob_object *blob_orig, const char *ondisk_path,
1029 const char *path, const char *label_orig, const char *deriv_target,
1030 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1031 got_worktree_checkout_cb progress_cb, void *progress_arg)
1033 const struct got_error *err = NULL;
1034 char *ancestor_target = NULL;
1035 struct stat sb;
1036 ssize_t ondisk_len, deriv_len;
1037 char ondisk_target[PATH_MAX];
1038 int have_local_change = 0;
1039 int have_incoming_change = 0;
1041 if (lstat(ondisk_path, &sb) == -1)
1042 return got_error_from_errno2("lstat", ondisk_path);
1044 ondisk_len = readlink(ondisk_path, ondisk_target,
1045 sizeof(ondisk_target));
1046 if (ondisk_len == -1) {
1047 err = got_error_from_errno2("readlink",
1048 ondisk_path);
1049 goto done;
1051 ondisk_target[ondisk_len] = '\0';
1053 if (blob_orig) {
1054 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1055 if (err)
1056 goto done;
1059 if (ancestor_target == NULL ||
1060 (ondisk_len != strlen(ancestor_target) ||
1061 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1062 have_local_change = 1;
1064 deriv_len = strlen(deriv_target);
1065 if (ancestor_target == NULL ||
1066 (deriv_len != strlen(ancestor_target) ||
1067 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1068 have_incoming_change = 1;
1070 if (!have_local_change && !have_incoming_change) {
1071 if (ancestor_target) {
1072 /* Both sides made the same change. */
1073 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1074 path);
1075 } else if (deriv_len == ondisk_len &&
1076 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1077 /* Both sides added the same symlink. */
1078 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1079 path);
1080 } else {
1081 /* Both sides added symlinks which don't match. */
1082 err = install_symlink_conflict(deriv_target,
1083 deriv_base_commit_id, ancestor_target,
1084 label_orig, ondisk_target, ondisk_path);
1085 if (err)
1086 goto done;
1087 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1088 path);
1090 } else if (!have_local_change && have_incoming_change) {
1091 /* Apply the incoming change. */
1092 err = update_symlink(ondisk_path, deriv_target,
1093 strlen(deriv_target));
1094 if (err)
1095 goto done;
1096 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1097 } else if (have_local_change && have_incoming_change) {
1098 if (deriv_len == ondisk_len &&
1099 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1100 /* Both sides made the same change. */
1101 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1102 path);
1103 } else {
1104 err = install_symlink_conflict(deriv_target,
1105 deriv_base_commit_id, ancestor_target, label_orig,
1106 ondisk_target, ondisk_path);
1107 if (err)
1108 goto done;
1109 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1110 path);
1114 done:
1115 free(ancestor_target);
1116 return err;
1120 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1121 * blob_deriv acts as the first derived version, and the file on disk
1122 * acts as the second derived version.
1124 static const struct got_error *
1125 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1126 struct got_blob_object *blob_orig, const char *ondisk_path,
1127 const char *path, uint16_t st_mode, const char *label_orig,
1128 struct got_blob_object *blob_deriv,
1129 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1130 got_worktree_checkout_cb progress_cb, void *progress_arg)
1132 const struct got_error *err = NULL;
1133 FILE *f_deriv = NULL;
1134 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1135 char *label_deriv = NULL, *parent = NULL;
1137 *local_changes_subsumed = 0;
1139 err = got_path_dirname(&parent, ondisk_path);
1140 if (err)
1141 return err;
1143 free(base_path);
1144 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1145 err = got_error_from_errno("asprintf");
1146 base_path = NULL;
1147 goto done;
1150 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1151 if (err)
1152 goto done;
1153 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1154 blob_deriv);
1155 if (err)
1156 goto done;
1158 err = got_object_id_str(&id_str, deriv_base_commit_id);
1159 if (err)
1160 goto done;
1161 if (asprintf(&label_deriv, "%s: commit %s",
1162 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1163 err = got_error_from_errno("asprintf");
1164 goto done;
1167 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1168 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1169 label_deriv, repo, progress_cb, progress_arg);
1170 done:
1171 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1172 err = got_error_from_errno("fclose");
1173 free(base_path);
1174 if (blob_deriv_path) {
1175 unlink(blob_deriv_path);
1176 free(blob_deriv_path);
1178 free(id_str);
1179 free(label_deriv);
1180 free(parent);
1181 return err;
1184 static const struct got_error *
1185 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1186 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1187 int wt_fd, const char *path, struct got_object_id *blob_id)
1189 const struct got_error *err = NULL;
1190 struct got_fileindex_entry *new_ie;
1192 *new_iep = NULL;
1194 err = got_fileindex_entry_alloc(&new_ie, path);
1195 if (err)
1196 return err;
1198 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1199 blob_id->sha1, base_commit_id->sha1, 1);
1200 if (err)
1201 goto done;
1203 err = got_fileindex_entry_add(fileindex, new_ie);
1204 done:
1205 if (err)
1206 got_fileindex_entry_free(new_ie);
1207 else
1208 *new_iep = new_ie;
1209 return err;
1212 static mode_t
1213 get_ondisk_perms(int executable, mode_t st_mode)
1215 mode_t xbits = S_IXUSR;
1217 if (executable) {
1218 /* Map read bits to execute bits. */
1219 if (st_mode & S_IRGRP)
1220 xbits |= S_IXGRP;
1221 if (st_mode & S_IROTH)
1222 xbits |= S_IXOTH;
1223 return st_mode | xbits;
1226 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1229 /* forward declaration */
1230 static const struct got_error *
1231 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1232 const char *path, mode_t te_mode, mode_t st_mode,
1233 struct got_blob_object *blob, int restoring_missing_file,
1234 int reverting_versioned_file, int installing_bad_symlink,
1235 int path_is_unversioned, struct got_repository *repo,
1236 got_worktree_checkout_cb progress_cb, void *progress_arg);
1239 * This function assumes that the provided symlink target points at a
1240 * safe location in the work tree!
1242 static const struct got_error *
1243 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1244 size_t target_len)
1246 const struct got_error *err = NULL;
1247 ssize_t elen;
1248 char etarget[PATH_MAX];
1249 int fd;
1252 * "Bad" symlinks (those pointing outside the work tree or into the
1253 * .got directory) are installed in the work tree as a regular file
1254 * which contains the bad symlink target path.
1255 * The new symlink target has already been checked for safety by our
1256 * caller. If we can successfully open a regular file then we simply
1257 * replace this file with a symlink below.
1259 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1260 if (fd == -1) {
1261 if (errno != ELOOP)
1262 return got_error_from_errno2("open", ondisk_path);
1264 /* We are updating an existing on-disk symlink. */
1265 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1266 if (elen == -1)
1267 return got_error_from_errno2("readlink", ondisk_path);
1269 if (elen == target_len &&
1270 memcmp(etarget, target_path, target_len) == 0)
1271 return NULL; /* nothing to do */
1274 err = update_symlink(ondisk_path, target_path, target_len);
1275 if (fd != -1 && close(fd) == -1 && err == NULL)
1276 err = got_error_from_errno2("close", ondisk_path);
1277 return err;
1280 static const struct got_error *
1281 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1282 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1284 const struct got_error *err = NULL;
1285 char canonpath[PATH_MAX];
1286 char *path_got = NULL;
1288 *is_bad_symlink = 0;
1290 if (target_len >= sizeof(canonpath)) {
1291 *is_bad_symlink = 1;
1292 return NULL;
1296 * We do not use realpath(3) to resolve the symlink's target
1297 * path because we don't want to resolve symlinks recursively.
1298 * Instead we make the path absolute and then canonicalize it.
1299 * Relative symlink target lookup should begin at the directory
1300 * in which the blob object is being installed.
1302 if (!got_path_is_absolute(target_path)) {
1303 char *abspath, *parent;
1304 err = got_path_dirname(&parent, ondisk_path);
1305 if (err)
1306 return err;
1307 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1308 free(parent);
1309 return got_error_from_errno("asprintf");
1311 free(parent);
1312 if (strlen(abspath) >= sizeof(canonpath)) {
1313 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1314 free(abspath);
1315 return err;
1317 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1318 free(abspath);
1319 if (err)
1320 return err;
1321 } else {
1322 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1323 if (err)
1324 return err;
1327 /* Only allow symlinks pointing at paths within the work tree. */
1328 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1329 *is_bad_symlink = 1;
1330 return NULL;
1333 /* Do not allow symlinks pointing into the .got directory. */
1334 if (asprintf(&path_got, "%s/%s", wtroot_path,
1335 GOT_WORKTREE_GOT_DIR) == -1)
1336 return got_error_from_errno("asprintf");
1337 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1338 *is_bad_symlink = 1;
1340 free(path_got);
1341 return NULL;
1344 static const struct got_error *
1345 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1346 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1347 int restoring_missing_file, int reverting_versioned_file,
1348 int path_is_unversioned, struct got_repository *repo,
1349 got_worktree_checkout_cb progress_cb, void *progress_arg)
1351 const struct got_error *err = NULL;
1352 char target_path[PATH_MAX];
1353 size_t len, target_len = 0;
1354 char *path_got = NULL;
1355 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1356 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1358 *is_bad_symlink = 0;
1361 * Blob object content specifies the target path of the link.
1362 * If a symbolic link cannot be installed we instead create
1363 * a regular file which contains the link target path stored
1364 * in the blob object.
1366 do {
1367 err = got_object_blob_read_block(&len, blob);
1368 if (len + target_len >= sizeof(target_path)) {
1369 /* Path too long; install as a regular file. */
1370 *is_bad_symlink = 1;
1371 got_object_blob_rewind(blob);
1372 return install_blob(worktree, ondisk_path, path,
1373 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1374 restoring_missing_file, reverting_versioned_file,
1375 1, path_is_unversioned, repo, progress_cb,
1376 progress_arg);
1378 if (len > 0) {
1379 /* Skip blob object header first time around. */
1380 memcpy(target_path + target_len, buf + hdrlen,
1381 len - hdrlen);
1382 target_len += len - hdrlen;
1383 hdrlen = 0;
1385 } while (len != 0);
1386 target_path[target_len] = '\0';
1388 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1389 ondisk_path, worktree->root_path);
1390 if (err)
1391 return err;
1393 if (*is_bad_symlink) {
1394 /* install as a regular file */
1395 *is_bad_symlink = 1;
1396 got_object_blob_rewind(blob);
1397 err = install_blob(worktree, ondisk_path, path,
1398 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1399 restoring_missing_file, reverting_versioned_file, 1,
1400 path_is_unversioned, repo, progress_cb, progress_arg);
1401 goto done;
1404 if (symlink(target_path, ondisk_path) == -1) {
1405 if (errno == EEXIST) {
1406 if (path_is_unversioned) {
1407 err = (*progress_cb)(progress_arg,
1408 GOT_STATUS_UNVERSIONED, path);
1409 goto done;
1411 err = replace_existing_symlink(ondisk_path,
1412 target_path, target_len);
1413 if (err)
1414 goto done;
1415 if (progress_cb) {
1416 err = (*progress_cb)(progress_arg,
1417 reverting_versioned_file ?
1418 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1419 path);
1421 goto done; /* Nothing else to do. */
1424 if (errno == ENOENT) {
1425 char *parent;
1426 err = got_path_dirname(&parent, ondisk_path);
1427 if (err)
1428 goto done;
1429 err = add_dir_on_disk(worktree, parent);
1430 free(parent);
1431 if (err)
1432 goto done;
1434 * Retry, and fall through to error handling
1435 * below if this second attempt fails.
1437 if (symlink(target_path, ondisk_path) != -1) {
1438 err = NULL; /* success */
1439 goto done;
1443 /* Handle errors from first or second creation attempt. */
1444 if (errno == ENAMETOOLONG) {
1445 /* bad target path; install as a regular file */
1446 *is_bad_symlink = 1;
1447 got_object_blob_rewind(blob);
1448 err = install_blob(worktree, ondisk_path, path,
1449 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1450 restoring_missing_file, reverting_versioned_file, 1,
1451 path_is_unversioned, repo,
1452 progress_cb, progress_arg);
1453 } else if (errno == ENOTDIR) {
1454 err = got_error_path(ondisk_path,
1455 GOT_ERR_FILE_OBSTRUCTED);
1456 } else {
1457 err = got_error_from_errno3("symlink",
1458 target_path, ondisk_path);
1460 } else if (progress_cb)
1461 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1462 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1463 done:
1464 free(path_got);
1465 return err;
1468 static const struct got_error *
1469 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1470 const char *path, mode_t te_mode, mode_t st_mode,
1471 struct got_blob_object *blob, int restoring_missing_file,
1472 int reverting_versioned_file, int installing_bad_symlink,
1473 int path_is_unversioned, struct got_repository *repo,
1474 got_worktree_checkout_cb progress_cb, void *progress_arg)
1476 const struct got_error *err = NULL;
1477 int fd = -1;
1478 size_t len, hdrlen;
1479 int update = 0;
1480 char *tmppath = NULL;
1482 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1483 GOT_DEFAULT_FILE_MODE);
1484 if (fd == -1) {
1485 if (errno == ENOENT) {
1486 char *parent;
1487 err = got_path_dirname(&parent, path);
1488 if (err)
1489 return err;
1490 err = add_dir_on_disk(worktree, parent);
1491 free(parent);
1492 if (err)
1493 return err;
1494 fd = open(ondisk_path,
1495 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1496 GOT_DEFAULT_FILE_MODE);
1497 if (fd == -1)
1498 return got_error_from_errno2("open",
1499 ondisk_path);
1500 } else if (errno == EEXIST) {
1501 if (path_is_unversioned) {
1502 err = (*progress_cb)(progress_arg,
1503 GOT_STATUS_UNVERSIONED, path);
1504 goto done;
1506 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1507 !S_ISREG(st_mode) && !installing_bad_symlink) {
1508 /* TODO file is obstructed; do something */
1509 err = got_error_path(ondisk_path,
1510 GOT_ERR_FILE_OBSTRUCTED);
1511 goto done;
1512 } else {
1513 err = got_opentemp_named_fd(&tmppath, &fd,
1514 ondisk_path);
1515 if (err)
1516 goto done;
1517 update = 1;
1519 } else
1520 return got_error_from_errno2("open", ondisk_path);
1523 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1524 err = got_error_from_errno2("fchmod",
1525 update ? tmppath : ondisk_path);
1526 goto done;
1529 if (progress_cb) {
1530 if (restoring_missing_file)
1531 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1532 path);
1533 else if (reverting_versioned_file)
1534 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1535 path);
1536 else
1537 err = (*progress_cb)(progress_arg,
1538 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1539 if (err)
1540 goto done;
1543 hdrlen = got_object_blob_get_hdrlen(blob);
1544 do {
1545 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1546 err = got_object_blob_read_block(&len, blob);
1547 if (err)
1548 break;
1549 if (len > 0) {
1550 /* Skip blob object header first time around. */
1551 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1552 if (outlen == -1) {
1553 err = got_error_from_errno("write");
1554 goto done;
1555 } else if (outlen != len - hdrlen) {
1556 err = got_error(GOT_ERR_IO);
1557 goto done;
1559 hdrlen = 0;
1561 } while (len != 0);
1563 if (fsync(fd) != 0) {
1564 err = got_error_from_errno("fsync");
1565 goto done;
1568 if (update) {
1569 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1570 err = got_error_from_errno2("unlink", ondisk_path);
1571 goto done;
1573 if (rename(tmppath, ondisk_path) != 0) {
1574 err = got_error_from_errno3("rename", tmppath,
1575 ondisk_path);
1576 goto done;
1578 free(tmppath);
1579 tmppath = NULL;
1582 done:
1583 if (fd != -1 && close(fd) != 0 && err == NULL)
1584 err = got_error_from_errno("close");
1585 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1586 err = got_error_from_errno2("unlink", tmppath);
1587 free(tmppath);
1588 return err;
1591 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1592 static const struct got_error *
1593 get_modified_file_content_status(unsigned char *status, FILE *f)
1595 const struct got_error *err = NULL;
1596 const char *markers[3] = {
1597 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1598 GOT_DIFF_CONFLICT_MARKER_SEP,
1599 GOT_DIFF_CONFLICT_MARKER_END
1601 int i = 0;
1602 char *line = NULL;
1603 size_t linesize = 0;
1604 ssize_t linelen;
1606 while (*status == GOT_STATUS_MODIFY) {
1607 linelen = getline(&line, &linesize, f);
1608 if (linelen == -1) {
1609 if (feof(f))
1610 break;
1611 err = got_ferror(f, GOT_ERR_IO);
1612 break;
1615 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1616 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1617 == 0)
1618 *status = GOT_STATUS_CONFLICT;
1619 else
1620 i++;
1623 free(line);
1625 return err;
1628 static int
1629 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1631 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1632 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1635 static int
1636 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1638 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1639 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1640 ie->mtime_sec == sb->st_mtim.tv_sec &&
1641 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1642 ie->size == (sb->st_size & 0xffffffff) &&
1643 !xbit_differs(ie, sb->st_mode));
1646 static unsigned char
1647 get_staged_status(struct got_fileindex_entry *ie)
1649 switch (got_fileindex_entry_stage_get(ie)) {
1650 case GOT_FILEIDX_STAGE_ADD:
1651 return GOT_STATUS_ADD;
1652 case GOT_FILEIDX_STAGE_DELETE:
1653 return GOT_STATUS_DELETE;
1654 case GOT_FILEIDX_STAGE_MODIFY:
1655 return GOT_STATUS_MODIFY;
1656 default:
1657 return GOT_STATUS_NO_CHANGE;
1661 static const struct got_error *
1662 get_symlink_modification_status(unsigned char *status,
1663 struct got_fileindex_entry *ie, const char *abspath,
1664 int dirfd, const char *de_name, struct got_blob_object *blob)
1666 const struct got_error *err = NULL;
1667 char target_path[PATH_MAX];
1668 char etarget[PATH_MAX];
1669 ssize_t elen;
1670 size_t len, target_len = 0;
1671 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1672 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1674 *status = GOT_STATUS_NO_CHANGE;
1676 /* Blob object content specifies the target path of the link. */
1677 do {
1678 err = got_object_blob_read_block(&len, blob);
1679 if (err)
1680 return err;
1681 if (len + target_len >= sizeof(target_path)) {
1683 * Should not happen. The blob contents were OK
1684 * when this symlink was installed.
1686 return got_error(GOT_ERR_NO_SPACE);
1688 if (len > 0) {
1689 /* Skip blob object header first time around. */
1690 memcpy(target_path + target_len, buf + hdrlen,
1691 len - hdrlen);
1692 target_len += len - hdrlen;
1693 hdrlen = 0;
1695 } while (len != 0);
1696 target_path[target_len] = '\0';
1698 if (dirfd != -1) {
1699 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1700 if (elen == -1)
1701 return got_error_from_errno2("readlinkat", abspath);
1702 } else {
1703 elen = readlink(abspath, etarget, sizeof(etarget));
1704 if (elen == -1)
1705 return got_error_from_errno2("readlink", abspath);
1708 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1709 *status = GOT_STATUS_MODIFY;
1711 return NULL;
1714 static const struct got_error *
1715 get_file_status(unsigned char *status, struct stat *sb,
1716 struct got_fileindex_entry *ie, const char *abspath,
1717 int dirfd, const char *de_name, struct got_repository *repo)
1719 const struct got_error *err = NULL;
1720 struct got_object_id id;
1721 size_t hdrlen;
1722 int fd = -1;
1723 FILE *f = NULL;
1724 uint8_t fbuf[8192];
1725 struct got_blob_object *blob = NULL;
1726 size_t flen, blen;
1727 unsigned char staged_status = get_staged_status(ie);
1729 *status = GOT_STATUS_NO_CHANGE;
1732 * Whenever the caller provides a directory descriptor and a
1733 * directory entry name for the file, use them! This prevents
1734 * race conditions if filesystem paths change beneath our feet.
1736 if (dirfd != -1) {
1737 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1738 if (errno == ENOENT) {
1739 if (got_fileindex_entry_has_file_on_disk(ie))
1740 *status = GOT_STATUS_MISSING;
1741 else
1742 *status = GOT_STATUS_DELETE;
1743 goto done;
1745 err = got_error_from_errno2("fstatat", abspath);
1746 goto done;
1748 } else {
1749 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1750 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1751 return got_error_from_errno2("open", abspath);
1752 else if (fd == -1 && errno == ELOOP) {
1753 if (lstat(abspath, sb) == -1)
1754 return got_error_from_errno2("lstat", abspath);
1755 } else if (fd == -1 || fstat(fd, sb) == -1) {
1756 if (errno == ENOENT) {
1757 if (got_fileindex_entry_has_file_on_disk(ie))
1758 *status = GOT_STATUS_MISSING;
1759 else
1760 *status = GOT_STATUS_DELETE;
1761 goto done;
1763 err = got_error_from_errno2("fstat", abspath);
1764 goto done;
1768 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1769 *status = GOT_STATUS_OBSTRUCTED;
1770 goto done;
1773 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1774 *status = GOT_STATUS_DELETE;
1775 goto done;
1776 } else if (!got_fileindex_entry_has_blob(ie) &&
1777 staged_status != GOT_STATUS_ADD) {
1778 *status = GOT_STATUS_ADD;
1779 goto done;
1782 if (!stat_info_differs(ie, sb))
1783 goto done;
1785 if (S_ISLNK(sb->st_mode) &&
1786 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1787 *status = GOT_STATUS_MODIFY;
1788 goto done;
1791 if (staged_status == GOT_STATUS_MODIFY ||
1792 staged_status == GOT_STATUS_ADD)
1793 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1794 else
1795 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1797 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1798 if (err)
1799 goto done;
1801 if (S_ISLNK(sb->st_mode)) {
1802 err = get_symlink_modification_status(status, ie,
1803 abspath, dirfd, de_name, blob);
1804 goto done;
1807 if (dirfd != -1) {
1808 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1809 if (fd == -1) {
1810 err = got_error_from_errno2("openat", abspath);
1811 goto done;
1815 f = fdopen(fd, "r");
1816 if (f == NULL) {
1817 err = got_error_from_errno2("fdopen", abspath);
1818 goto done;
1820 fd = -1;
1821 hdrlen = got_object_blob_get_hdrlen(blob);
1822 for (;;) {
1823 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1824 err = got_object_blob_read_block(&blen, blob);
1825 if (err)
1826 goto done;
1827 /* Skip length of blob object header first time around. */
1828 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1829 if (flen == 0 && ferror(f)) {
1830 err = got_error_from_errno("fread");
1831 goto done;
1833 if (blen - hdrlen == 0) {
1834 if (flen != 0)
1835 *status = GOT_STATUS_MODIFY;
1836 break;
1837 } else if (flen == 0) {
1838 if (blen - hdrlen != 0)
1839 *status = GOT_STATUS_MODIFY;
1840 break;
1841 } else if (blen - hdrlen == flen) {
1842 /* Skip blob object header first time around. */
1843 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1844 *status = GOT_STATUS_MODIFY;
1845 break;
1847 } else {
1848 *status = GOT_STATUS_MODIFY;
1849 break;
1851 hdrlen = 0;
1854 if (*status == GOT_STATUS_MODIFY) {
1855 rewind(f);
1856 err = get_modified_file_content_status(status, f);
1857 } else if (xbit_differs(ie, sb->st_mode))
1858 *status = GOT_STATUS_MODE_CHANGE;
1859 done:
1860 if (blob)
1861 got_object_blob_close(blob);
1862 if (f != NULL && fclose(f) == EOF && err == NULL)
1863 err = got_error_from_errno2("fclose", abspath);
1864 if (fd != -1 && close(fd) == -1 && err == NULL)
1865 err = got_error_from_errno2("close", abspath);
1866 return err;
1870 * Update timestamps in the file index if a file is unmodified and
1871 * we had to run a full content comparison to find out.
1873 static const struct got_error *
1874 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1875 struct got_fileindex_entry *ie, struct stat *sb)
1877 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1878 return got_fileindex_entry_update(ie, wt_fd, path,
1879 ie->blob_sha1, ie->commit_sha1, 1);
1881 return NULL;
1884 static const struct got_error *
1885 update_blob(struct got_worktree *worktree,
1886 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1887 struct got_tree_entry *te, const char *path,
1888 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1889 void *progress_arg)
1891 const struct got_error *err = NULL;
1892 struct got_blob_object *blob = NULL;
1893 char *ondisk_path;
1894 unsigned char status = GOT_STATUS_NO_CHANGE;
1895 struct stat sb;
1897 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1898 return got_error_from_errno("asprintf");
1900 if (ie) {
1901 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1902 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1903 goto done;
1905 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1906 repo);
1907 if (err)
1908 goto done;
1909 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1910 sb.st_mode = got_fileindex_perms_to_st(ie);
1911 } else {
1912 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1913 status = GOT_STATUS_UNVERSIONED;
1916 if (status == GOT_STATUS_OBSTRUCTED) {
1917 err = (*progress_cb)(progress_arg, status, path);
1918 goto done;
1920 if (status == GOT_STATUS_CONFLICT) {
1921 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1922 path);
1923 goto done;
1926 if (ie && status != GOT_STATUS_MISSING &&
1927 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1928 if (got_fileindex_entry_has_commit(ie) &&
1929 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1930 SHA1_DIGEST_LENGTH) == 0) {
1931 err = sync_timestamps(worktree->root_fd,
1932 path, status, ie, &sb);
1933 if (err)
1934 goto done;
1935 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1936 path);
1937 goto done;
1939 if (got_fileindex_entry_has_blob(ie) &&
1940 memcmp(ie->blob_sha1, te->id.sha1,
1941 SHA1_DIGEST_LENGTH) == 0) {
1942 err = sync_timestamps(worktree->root_fd,
1943 path, status, ie, &sb);
1944 goto done;
1948 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1949 if (err)
1950 goto done;
1952 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1953 int update_timestamps;
1954 struct got_blob_object *blob2 = NULL;
1955 char *label_orig = NULL;
1956 if (got_fileindex_entry_has_blob(ie)) {
1957 struct got_object_id id2;
1958 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1959 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1960 if (err)
1961 goto done;
1963 if (got_fileindex_entry_has_commit(ie)) {
1964 char id_str[SHA1_DIGEST_STRING_LENGTH];
1965 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1966 sizeof(id_str)) == NULL) {
1967 err = got_error_path(id_str,
1968 GOT_ERR_BAD_OBJ_ID_STR);
1969 goto done;
1971 if (asprintf(&label_orig, "%s: commit %s",
1972 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1973 err = got_error_from_errno("asprintf");
1974 goto done;
1977 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1978 char *link_target;
1979 err = got_object_blob_read_to_str(&link_target, blob);
1980 if (err)
1981 goto done;
1982 err = merge_symlink(worktree, blob2, ondisk_path, path,
1983 label_orig, link_target, worktree->base_commit_id,
1984 repo, progress_cb, progress_arg);
1985 free(link_target);
1986 } else {
1987 err = merge_blob(&update_timestamps, worktree, blob2,
1988 ondisk_path, path, sb.st_mode, label_orig, blob,
1989 worktree->base_commit_id, repo,
1990 progress_cb, progress_arg);
1992 free(label_orig);
1993 if (blob2)
1994 got_object_blob_close(blob2);
1995 if (err)
1996 goto done;
1998 * Do not update timestamps of files with local changes.
1999 * Otherwise, a future status walk would treat them as
2000 * unmodified files again.
2002 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2003 blob->id.sha1, worktree->base_commit_id->sha1,
2004 update_timestamps);
2005 } else if (status == GOT_STATUS_MODE_CHANGE) {
2006 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2007 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2008 } else if (status == GOT_STATUS_DELETE) {
2009 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2010 if (err)
2011 goto done;
2012 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2013 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2014 if (err)
2015 goto done;
2016 } else {
2017 int is_bad_symlink = 0;
2018 if (S_ISLNK(te->mode)) {
2019 err = install_symlink(&is_bad_symlink, worktree,
2020 ondisk_path, path, blob,
2021 status == GOT_STATUS_MISSING, 0,
2022 status == GOT_STATUS_UNVERSIONED, repo,
2023 progress_cb, progress_arg);
2024 } else {
2025 err = install_blob(worktree, ondisk_path, path,
2026 te->mode, sb.st_mode, blob,
2027 status == GOT_STATUS_MISSING, 0, 0,
2028 status == GOT_STATUS_UNVERSIONED, repo,
2029 progress_cb, progress_arg);
2031 if (err)
2032 goto done;
2034 if (ie) {
2035 err = got_fileindex_entry_update(ie,
2036 worktree->root_fd, path, blob->id.sha1,
2037 worktree->base_commit_id->sha1, 1);
2038 } else {
2039 err = create_fileindex_entry(&ie, fileindex,
2040 worktree->base_commit_id, worktree->root_fd, path,
2041 &blob->id);
2043 if (err)
2044 goto done;
2046 if (is_bad_symlink) {
2047 got_fileindex_entry_filetype_set(ie,
2048 GOT_FILEIDX_MODE_BAD_SYMLINK);
2051 got_object_blob_close(blob);
2052 done:
2053 free(ondisk_path);
2054 return err;
2057 static const struct got_error *
2058 remove_ondisk_file(const char *root_path, const char *path)
2060 const struct got_error *err = NULL;
2061 char *ondisk_path = NULL;
2063 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2064 return got_error_from_errno("asprintf");
2066 if (unlink(ondisk_path) == -1) {
2067 if (errno != ENOENT)
2068 err = got_error_from_errno2("unlink", ondisk_path);
2069 } else {
2070 size_t root_len = strlen(root_path);
2071 do {
2072 char *parent;
2073 err = got_path_dirname(&parent, ondisk_path);
2074 if (err)
2075 break;
2076 free(ondisk_path);
2077 ondisk_path = parent;
2078 if (rmdir(ondisk_path) == -1) {
2079 if (errno != ENOTEMPTY)
2080 err = got_error_from_errno2("rmdir",
2081 ondisk_path);
2082 break;
2084 } while (got_path_cmp(ondisk_path, root_path,
2085 strlen(ondisk_path), root_len) != 0);
2087 free(ondisk_path);
2088 return err;
2091 static const struct got_error *
2092 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2093 struct got_fileindex_entry *ie, struct got_repository *repo,
2094 got_worktree_checkout_cb progress_cb, void *progress_arg)
2096 const struct got_error *err = NULL;
2097 unsigned char status;
2098 struct stat sb;
2099 char *ondisk_path;
2101 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2102 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2104 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2105 == -1)
2106 return got_error_from_errno("asprintf");
2108 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2109 if (err)
2110 goto done;
2112 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2113 char ondisk_target[PATH_MAX];
2114 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2115 sizeof(ondisk_target));
2116 if (ondisk_len == -1) {
2117 err = got_error_from_errno2("readlink", ondisk_path);
2118 goto done;
2120 ondisk_target[ondisk_len] = '\0';
2121 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2122 NULL, NULL, /* XXX pass common ancestor info? */
2123 ondisk_target, ondisk_path);
2124 if (err)
2125 goto done;
2126 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2127 ie->path);
2128 goto done;
2131 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2132 status == GOT_STATUS_ADD) {
2133 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2134 if (err)
2135 goto done;
2137 * Preserve the working file and change the deleted blob's
2138 * entry into a schedule-add entry.
2140 err = got_fileindex_entry_update(ie, worktree->root_fd,
2141 ie->path, NULL, NULL, 0);
2142 } else {
2143 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2144 if (err)
2145 goto done;
2146 if (status == GOT_STATUS_NO_CHANGE) {
2147 err = remove_ondisk_file(worktree->root_path, ie->path);
2148 if (err)
2149 goto done;
2151 got_fileindex_entry_remove(fileindex, ie);
2153 done:
2154 free(ondisk_path);
2155 return err;
2158 struct diff_cb_arg {
2159 struct got_fileindex *fileindex;
2160 struct got_worktree *worktree;
2161 struct got_repository *repo;
2162 got_worktree_checkout_cb progress_cb;
2163 void *progress_arg;
2164 got_cancel_cb cancel_cb;
2165 void *cancel_arg;
2168 static const struct got_error *
2169 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2170 struct got_tree_entry *te, const char *parent_path)
2172 struct diff_cb_arg *a = arg;
2174 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2175 return got_error(GOT_ERR_CANCELLED);
2177 return update_blob(a->worktree, a->fileindex, ie, te,
2178 ie->path, a->repo, a->progress_cb, a->progress_arg);
2181 static const struct got_error *
2182 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2184 struct diff_cb_arg *a = arg;
2186 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2187 return got_error(GOT_ERR_CANCELLED);
2189 return delete_blob(a->worktree, a->fileindex, ie,
2190 a->repo, a->progress_cb, a->progress_arg);
2193 static const struct got_error *
2194 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2196 struct diff_cb_arg *a = arg;
2197 const struct got_error *err;
2198 char *path;
2200 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2201 return got_error(GOT_ERR_CANCELLED);
2203 if (got_object_tree_entry_is_submodule(te))
2204 return NULL;
2206 if (asprintf(&path, "%s%s%s", parent_path,
2207 parent_path[0] ? "/" : "", te->name)
2208 == -1)
2209 return got_error_from_errno("asprintf");
2211 if (S_ISDIR(te->mode))
2212 err = add_dir_on_disk(a->worktree, path);
2213 else
2214 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2215 a->repo, a->progress_cb, a->progress_arg);
2217 free(path);
2218 return err;
2221 const struct got_error *
2222 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2224 uint32_t uuid_status;
2226 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2227 if (uuid_status != uuid_s_ok) {
2228 *uuidstr = NULL;
2229 return got_error_uuid(uuid_status, "uuid_to_string");
2232 return NULL;
2235 static const struct got_error *
2236 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2238 const struct got_error *err = NULL;
2239 char *uuidstr = NULL;
2241 *refname = NULL;
2243 err = got_worktree_get_uuid(&uuidstr, worktree);
2244 if (err)
2245 return err;
2247 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2248 err = got_error_from_errno("asprintf");
2249 *refname = NULL;
2251 free(uuidstr);
2252 return err;
2255 const struct got_error *
2256 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2258 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2261 static const struct got_error *
2262 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2264 return get_ref_name(refname, worktree,
2265 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2268 static const struct got_error *
2269 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2271 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2274 static const struct got_error *
2275 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2277 return get_ref_name(refname, worktree,
2278 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2281 static const struct got_error *
2282 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2284 return get_ref_name(refname, worktree,
2285 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2288 static const struct got_error *
2289 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2291 return get_ref_name(refname, worktree,
2292 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2295 static const struct got_error *
2296 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2298 return get_ref_name(refname, worktree,
2299 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2302 static const struct got_error *
2303 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2305 return get_ref_name(refname, worktree,
2306 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2309 static const struct got_error *
2310 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2312 return get_ref_name(refname, worktree,
2313 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2316 const struct got_error *
2317 got_worktree_get_histedit_script_path(char **path,
2318 struct got_worktree *worktree)
2320 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2321 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2322 *path = NULL;
2323 return got_error_from_errno("asprintf");
2325 return NULL;
2329 * Prevent Git's garbage collector from deleting our base commit by
2330 * setting a reference to our base commit's ID.
2332 static const struct got_error *
2333 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2335 const struct got_error *err = NULL;
2336 struct got_reference *ref = NULL;
2337 char *refname;
2339 err = got_worktree_get_base_ref_name(&refname, worktree);
2340 if (err)
2341 return err;
2343 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2344 if (err)
2345 goto done;
2347 err = got_ref_write(ref, repo);
2348 done:
2349 free(refname);
2350 if (ref)
2351 got_ref_close(ref);
2352 return err;
2355 static const struct got_error *
2356 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2358 const struct got_error *err = NULL;
2360 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2361 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2362 err = got_error_from_errno("asprintf");
2363 *fileindex_path = NULL;
2365 return err;
2369 static const struct got_error *
2370 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2371 struct got_worktree *worktree)
2373 const struct got_error *err = NULL;
2374 FILE *index = NULL;
2376 *fileindex_path = NULL;
2377 *fileindex = got_fileindex_alloc();
2378 if (*fileindex == NULL)
2379 return got_error_from_errno("got_fileindex_alloc");
2381 err = get_fileindex_path(fileindex_path, worktree);
2382 if (err)
2383 goto done;
2385 index = fopen(*fileindex_path, "rb");
2386 if (index == NULL) {
2387 if (errno != ENOENT)
2388 err = got_error_from_errno2("fopen", *fileindex_path);
2389 } else {
2390 err = got_fileindex_read(*fileindex, index);
2391 if (fclose(index) != 0 && err == NULL)
2392 err = got_error_from_errno("fclose");
2394 done:
2395 if (err) {
2396 free(*fileindex_path);
2397 *fileindex_path = NULL;
2398 got_fileindex_free(*fileindex);
2399 *fileindex = NULL;
2401 return err;
2404 struct bump_base_commit_id_arg {
2405 struct got_object_id *base_commit_id;
2406 const char *path;
2407 size_t path_len;
2408 const char *entry_name;
2409 got_worktree_checkout_cb progress_cb;
2410 void *progress_arg;
2413 /* Bump base commit ID of all files within an updated part of the work tree. */
2414 static const struct got_error *
2415 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2417 const struct got_error *err;
2418 struct bump_base_commit_id_arg *a = arg;
2420 if (a->entry_name) {
2421 if (strcmp(ie->path, a->path) != 0)
2422 return NULL;
2423 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2424 return NULL;
2426 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2427 SHA1_DIGEST_LENGTH) == 0)
2428 return NULL;
2430 if (a->progress_cb) {
2431 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2432 ie->path);
2433 if (err)
2434 return err;
2436 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2437 return NULL;
2440 static const struct got_error *
2441 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2442 struct got_fileindex *fileindex,
2443 got_worktree_checkout_cb progress_cb, void *progress_arg)
2445 struct bump_base_commit_id_arg bbc_arg;
2447 bbc_arg.base_commit_id = worktree->base_commit_id;
2448 bbc_arg.entry_name = NULL;
2449 bbc_arg.path = "";
2450 bbc_arg.path_len = 0;
2451 bbc_arg.progress_cb = progress_cb;
2452 bbc_arg.progress_arg = progress_arg;
2454 return got_fileindex_for_each_entry_safe(fileindex,
2455 bump_base_commit_id, &bbc_arg);
2458 static const struct got_error *
2459 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2461 const struct got_error *err = NULL;
2462 char *new_fileindex_path = NULL;
2463 FILE *new_index = NULL;
2464 struct timespec timeout;
2466 err = got_opentemp_named(&new_fileindex_path, &new_index,
2467 fileindex_path);
2468 if (err)
2469 goto done;
2471 err = got_fileindex_write(fileindex, new_index);
2472 if (err)
2473 goto done;
2475 if (rename(new_fileindex_path, fileindex_path) != 0) {
2476 err = got_error_from_errno3("rename", new_fileindex_path,
2477 fileindex_path);
2478 unlink(new_fileindex_path);
2482 * Sleep for a short amount of time to ensure that files modified after
2483 * this program exits have a different time stamp from the one which
2484 * was recorded in the file index.
2486 timeout.tv_sec = 0;
2487 timeout.tv_nsec = 1;
2488 nanosleep(&timeout, NULL);
2489 done:
2490 if (new_index)
2491 fclose(new_index);
2492 free(new_fileindex_path);
2493 return err;
2496 static const struct got_error *
2497 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2498 struct got_object_id **tree_id, const char *wt_relpath,
2499 struct got_worktree *worktree, struct got_repository *repo)
2501 const struct got_error *err = NULL;
2502 struct got_object_id *id = NULL;
2503 char *in_repo_path = NULL;
2504 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2506 *entry_type = GOT_OBJ_TYPE_ANY;
2507 *tree_relpath = NULL;
2508 *tree_id = NULL;
2510 if (wt_relpath[0] == '\0') {
2511 /* Check out all files within the work tree. */
2512 *entry_type = GOT_OBJ_TYPE_TREE;
2513 *tree_relpath = strdup("");
2514 if (*tree_relpath == NULL) {
2515 err = got_error_from_errno("strdup");
2516 goto done;
2518 err = got_object_id_by_path(tree_id, repo,
2519 worktree->base_commit_id, worktree->path_prefix);
2520 if (err)
2521 goto done;
2522 return NULL;
2525 /* Check out a subset of files in the work tree. */
2527 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2528 is_root_wt ? "" : "/", wt_relpath) == -1) {
2529 err = got_error_from_errno("asprintf");
2530 goto done;
2533 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2534 in_repo_path);
2535 if (err)
2536 goto done;
2538 free(in_repo_path);
2539 in_repo_path = NULL;
2541 err = got_object_get_type(entry_type, repo, id);
2542 if (err)
2543 goto done;
2545 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2546 /* Check out a single file. */
2547 if (strchr(wt_relpath, '/') == NULL) {
2548 /* Check out a single file in work tree's root dir. */
2549 in_repo_path = strdup(worktree->path_prefix);
2550 if (in_repo_path == NULL) {
2551 err = got_error_from_errno("strdup");
2552 goto done;
2554 *tree_relpath = strdup("");
2555 if (*tree_relpath == NULL) {
2556 err = got_error_from_errno("strdup");
2557 goto done;
2559 } else {
2560 /* Check out a single file in a subdirectory. */
2561 err = got_path_dirname(tree_relpath, wt_relpath);
2562 if (err)
2563 return err;
2564 if (asprintf(&in_repo_path, "%s%s%s",
2565 worktree->path_prefix, is_root_wt ? "" : "/",
2566 *tree_relpath) == -1) {
2567 err = got_error_from_errno("asprintf");
2568 goto done;
2571 err = got_object_id_by_path(tree_id, repo,
2572 worktree->base_commit_id, in_repo_path);
2573 } else {
2574 /* Check out all files within a subdirectory. */
2575 *tree_id = got_object_id_dup(id);
2576 if (*tree_id == NULL) {
2577 err = got_error_from_errno("got_object_id_dup");
2578 goto done;
2580 *tree_relpath = strdup(wt_relpath);
2581 if (*tree_relpath == NULL) {
2582 err = got_error_from_errno("strdup");
2583 goto done;
2586 done:
2587 free(id);
2588 free(in_repo_path);
2589 if (err) {
2590 *entry_type = GOT_OBJ_TYPE_ANY;
2591 free(*tree_relpath);
2592 *tree_relpath = NULL;
2593 free(*tree_id);
2594 *tree_id = NULL;
2596 return err;
2599 static const struct got_error *
2600 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2601 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2602 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2603 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2605 const struct got_error *err = NULL;
2606 struct got_commit_object *commit = NULL;
2607 struct got_tree_object *tree = NULL;
2608 struct got_fileindex_diff_tree_cb diff_cb;
2609 struct diff_cb_arg arg;
2611 err = ref_base_commit(worktree, repo);
2612 if (err) {
2613 if (!(err->code == GOT_ERR_ERRNO &&
2614 (errno == EACCES || errno == EROFS)))
2615 goto done;
2616 err = (*progress_cb)(progress_arg,
2617 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2618 if (err)
2619 return err;
2622 err = got_object_open_as_commit(&commit, repo,
2623 worktree->base_commit_id);
2624 if (err)
2625 goto done;
2627 err = got_object_open_as_tree(&tree, repo, tree_id);
2628 if (err)
2629 goto done;
2631 if (entry_name &&
2632 got_object_tree_find_entry(tree, entry_name) == NULL) {
2633 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2634 goto done;
2637 diff_cb.diff_old_new = diff_old_new;
2638 diff_cb.diff_old = diff_old;
2639 diff_cb.diff_new = diff_new;
2640 arg.fileindex = fileindex;
2641 arg.worktree = worktree;
2642 arg.repo = repo;
2643 arg.progress_cb = progress_cb;
2644 arg.progress_arg = progress_arg;
2645 arg.cancel_cb = cancel_cb;
2646 arg.cancel_arg = cancel_arg;
2647 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2648 entry_name, repo, &diff_cb, &arg);
2649 done:
2650 if (tree)
2651 got_object_tree_close(tree);
2652 if (commit)
2653 got_object_commit_close(commit);
2654 return err;
2657 const struct got_error *
2658 got_worktree_checkout_files(struct got_worktree *worktree,
2659 struct got_pathlist_head *paths, struct got_repository *repo,
2660 got_worktree_checkout_cb progress_cb, void *progress_arg,
2661 got_cancel_cb cancel_cb, void *cancel_arg)
2663 const struct got_error *err = NULL, *sync_err, *unlockerr;
2664 struct got_commit_object *commit = NULL;
2665 struct got_tree_object *tree = NULL;
2666 struct got_fileindex *fileindex = NULL;
2667 char *fileindex_path = NULL;
2668 struct got_pathlist_entry *pe;
2669 struct tree_path_data {
2670 SIMPLEQ_ENTRY(tree_path_data) entry;
2671 struct got_object_id *tree_id;
2672 int entry_type;
2673 char *relpath;
2674 char *entry_name;
2675 } *tpd = NULL;
2676 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2678 SIMPLEQ_INIT(&tree_paths);
2680 err = lock_worktree(worktree, LOCK_EX);
2681 if (err)
2682 return err;
2684 /* Map all specified paths to in-repository trees. */
2685 TAILQ_FOREACH(pe, paths, entry) {
2686 tpd = malloc(sizeof(*tpd));
2687 if (tpd == NULL) {
2688 err = got_error_from_errno("malloc");
2689 goto done;
2692 err = find_tree_entry_for_checkout(&tpd->entry_type,
2693 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2694 if (err) {
2695 free(tpd);
2696 goto done;
2699 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2700 err = got_path_basename(&tpd->entry_name, pe->path);
2701 if (err) {
2702 free(tpd->relpath);
2703 free(tpd->tree_id);
2704 free(tpd);
2705 goto done;
2707 } else
2708 tpd->entry_name = NULL;
2710 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2714 * Read the file index.
2715 * Checking out files is supposed to be an idempotent operation.
2716 * If the on-disk file index is incomplete we will try to complete it.
2718 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2719 if (err)
2720 goto done;
2722 tpd = SIMPLEQ_FIRST(&tree_paths);
2723 TAILQ_FOREACH(pe, paths, entry) {
2724 struct bump_base_commit_id_arg bbc_arg;
2726 err = checkout_files(worktree, fileindex, tpd->relpath,
2727 tpd->tree_id, tpd->entry_name, repo,
2728 progress_cb, progress_arg, cancel_cb, cancel_arg);
2729 if (err)
2730 break;
2732 bbc_arg.base_commit_id = worktree->base_commit_id;
2733 bbc_arg.entry_name = tpd->entry_name;
2734 bbc_arg.path = pe->path;
2735 bbc_arg.path_len = pe->path_len;
2736 bbc_arg.progress_cb = progress_cb;
2737 bbc_arg.progress_arg = progress_arg;
2738 err = got_fileindex_for_each_entry_safe(fileindex,
2739 bump_base_commit_id, &bbc_arg);
2740 if (err)
2741 break;
2743 tpd = SIMPLEQ_NEXT(tpd, entry);
2745 sync_err = sync_fileindex(fileindex, fileindex_path);
2746 if (sync_err && err == NULL)
2747 err = sync_err;
2748 done:
2749 free(fileindex_path);
2750 if (tree)
2751 got_object_tree_close(tree);
2752 if (commit)
2753 got_object_commit_close(commit);
2754 if (fileindex)
2755 got_fileindex_free(fileindex);
2756 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2757 tpd = SIMPLEQ_FIRST(&tree_paths);
2758 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2759 free(tpd->relpath);
2760 free(tpd->tree_id);
2761 free(tpd);
2763 unlockerr = lock_worktree(worktree, LOCK_SH);
2764 if (unlockerr && err == NULL)
2765 err = unlockerr;
2766 return err;
2769 struct merge_file_cb_arg {
2770 struct got_worktree *worktree;
2771 struct got_fileindex *fileindex;
2772 got_worktree_checkout_cb progress_cb;
2773 void *progress_arg;
2774 got_cancel_cb cancel_cb;
2775 void *cancel_arg;
2776 const char *label_orig;
2777 struct got_object_id *commit_id2;
2780 static const struct got_error *
2781 merge_file_cb(void *arg, struct got_blob_object *blob1,
2782 struct got_blob_object *blob2, struct got_object_id *id1,
2783 struct got_object_id *id2, const char *path1, const char *path2,
2784 mode_t mode1, mode_t mode2, struct got_repository *repo)
2786 static const struct got_error *err = NULL;
2787 struct merge_file_cb_arg *a = arg;
2788 struct got_fileindex_entry *ie;
2789 char *ondisk_path = NULL;
2790 struct stat sb;
2791 unsigned char status;
2792 int local_changes_subsumed;
2794 if (blob1 && blob2) {
2795 ie = got_fileindex_entry_get(a->fileindex, path2,
2796 strlen(path2));
2797 if (ie == NULL)
2798 return (*a->progress_cb)(a->progress_arg,
2799 GOT_STATUS_MISSING, path2);
2801 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2802 path2) == -1)
2803 return got_error_from_errno("asprintf");
2805 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2806 repo);
2807 if (err)
2808 goto done;
2810 if (status == GOT_STATUS_DELETE) {
2811 err = (*a->progress_cb)(a->progress_arg,
2812 GOT_STATUS_MERGE, path2);
2813 goto done;
2815 if (status != GOT_STATUS_NO_CHANGE &&
2816 status != GOT_STATUS_MODIFY &&
2817 status != GOT_STATUS_CONFLICT &&
2818 status != GOT_STATUS_ADD) {
2819 err = (*a->progress_cb)(a->progress_arg, status, path2);
2820 goto done;
2823 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2824 char *link_target2;
2825 err = got_object_blob_read_to_str(&link_target2, blob2);
2826 if (err)
2827 goto done;
2828 err = merge_symlink(a->worktree, blob1, ondisk_path,
2829 path2, a->label_orig, link_target2, a->commit_id2,
2830 repo, a->progress_cb, a->progress_arg);
2831 free(link_target2);
2832 } else {
2833 err = merge_blob(&local_changes_subsumed, a->worktree,
2834 blob1, ondisk_path, path2, sb.st_mode,
2835 a->label_orig, blob2, a->commit_id2, repo,
2836 a->progress_cb, a->progress_arg);
2838 } else if (blob1) {
2839 ie = got_fileindex_entry_get(a->fileindex, path1,
2840 strlen(path1));
2841 if (ie == NULL)
2842 return (*a->progress_cb)(a->progress_arg,
2843 GOT_STATUS_MISSING, path1);
2845 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2846 path1) == -1)
2847 return got_error_from_errno("asprintf");
2849 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2850 repo);
2851 if (err)
2852 goto done;
2854 switch (status) {
2855 case GOT_STATUS_NO_CHANGE:
2856 err = (*a->progress_cb)(a->progress_arg,
2857 GOT_STATUS_DELETE, path1);
2858 if (err)
2859 goto done;
2860 err = remove_ondisk_file(a->worktree->root_path, path1);
2861 if (err)
2862 goto done;
2863 if (ie)
2864 got_fileindex_entry_mark_deleted_from_disk(ie);
2865 break;
2866 case GOT_STATUS_DELETE:
2867 case GOT_STATUS_MISSING:
2868 err = (*a->progress_cb)(a->progress_arg,
2869 GOT_STATUS_DELETE, path1);
2870 if (err)
2871 goto done;
2872 if (ie)
2873 got_fileindex_entry_mark_deleted_from_disk(ie);
2874 break;
2875 case GOT_STATUS_ADD: {
2876 struct got_object_id *id;
2877 FILE *blob1_f;
2879 * Delete the added file only if its content already
2880 * exists in the repository.
2882 err = got_object_blob_file_create(&id, &blob1_f, path1);
2883 if (err)
2884 goto done;
2885 if (got_object_id_cmp(id, id1) == 0) {
2886 err = (*a->progress_cb)(a->progress_arg,
2887 GOT_STATUS_DELETE, path1);
2888 if (err)
2889 goto done;
2890 err = remove_ondisk_file(a->worktree->root_path,
2891 path1);
2892 if (err)
2893 goto done;
2894 if (ie)
2895 got_fileindex_entry_remove(a->fileindex,
2896 ie);
2897 } else {
2898 err = (*a->progress_cb)(a->progress_arg,
2899 GOT_STATUS_CANNOT_DELETE, path1);
2901 if (fclose(blob1_f) == EOF && err == NULL)
2902 err = got_error_from_errno("fclose");
2903 free(id);
2904 if (err)
2905 goto done;
2906 break;
2908 case GOT_STATUS_MODIFY:
2909 case GOT_STATUS_CONFLICT:
2910 err = (*a->progress_cb)(a->progress_arg,
2911 GOT_STATUS_CANNOT_DELETE, path1);
2912 if (err)
2913 goto done;
2914 break;
2915 case GOT_STATUS_OBSTRUCTED:
2916 err = (*a->progress_cb)(a->progress_arg, status, path1);
2917 if (err)
2918 goto done;
2919 break;
2920 default:
2921 break;
2923 } else if (blob2) {
2924 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2925 path2) == -1)
2926 return got_error_from_errno("asprintf");
2927 ie = got_fileindex_entry_get(a->fileindex, path2,
2928 strlen(path2));
2929 if (ie) {
2930 err = get_file_status(&status, &sb, ie, ondisk_path,
2931 -1, NULL, repo);
2932 if (err)
2933 goto done;
2934 if (status != GOT_STATUS_NO_CHANGE &&
2935 status != GOT_STATUS_MODIFY &&
2936 status != GOT_STATUS_CONFLICT &&
2937 status != GOT_STATUS_ADD) {
2938 err = (*a->progress_cb)(a->progress_arg,
2939 status, path2);
2940 goto done;
2942 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2943 char *link_target2;
2944 err = got_object_blob_read_to_str(&link_target2,
2945 blob2);
2946 if (err)
2947 goto done;
2948 err = merge_symlink(a->worktree, NULL,
2949 ondisk_path, path2, a->label_orig,
2950 link_target2, a->commit_id2, repo,
2951 a->progress_cb, a->progress_arg);
2952 free(link_target2);
2953 } else if (S_ISREG(sb.st_mode)) {
2954 err = merge_blob(&local_changes_subsumed,
2955 a->worktree, NULL, ondisk_path, path2,
2956 sb.st_mode, a->label_orig, blob2,
2957 a->commit_id2, repo, a->progress_cb,
2958 a->progress_arg);
2959 } else {
2960 err = got_error_path(ondisk_path,
2961 GOT_ERR_FILE_OBSTRUCTED);
2963 if (err)
2964 goto done;
2965 if (status == GOT_STATUS_DELETE) {
2966 err = got_fileindex_entry_update(ie,
2967 a->worktree->root_fd, path2, blob2->id.sha1,
2968 a->worktree->base_commit_id->sha1, 0);
2969 if (err)
2970 goto done;
2972 } else {
2973 int is_bad_symlink = 0;
2974 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2975 if (S_ISLNK(mode2)) {
2976 err = install_symlink(&is_bad_symlink,
2977 a->worktree, ondisk_path, path2, blob2, 0,
2978 0, 1, repo, a->progress_cb, a->progress_arg);
2979 } else {
2980 err = install_blob(a->worktree, ondisk_path, path2,
2981 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2982 a->progress_cb, a->progress_arg);
2984 if (err)
2985 goto done;
2986 err = got_fileindex_entry_alloc(&ie, path2);
2987 if (err)
2988 goto done;
2989 err = got_fileindex_entry_update(ie,
2990 a->worktree->root_fd, path2, NULL, NULL, 1);
2991 if (err) {
2992 got_fileindex_entry_free(ie);
2993 goto done;
2995 err = got_fileindex_entry_add(a->fileindex, ie);
2996 if (err) {
2997 got_fileindex_entry_free(ie);
2998 goto done;
3000 if (is_bad_symlink) {
3001 got_fileindex_entry_filetype_set(ie,
3002 GOT_FILEIDX_MODE_BAD_SYMLINK);
3006 done:
3007 free(ondisk_path);
3008 return err;
3011 struct check_merge_ok_arg {
3012 struct got_worktree *worktree;
3013 struct got_repository *repo;
3016 static const struct got_error *
3017 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3019 const struct got_error *err = NULL;
3020 struct check_merge_ok_arg *a = arg;
3021 unsigned char status;
3022 struct stat sb;
3023 char *ondisk_path;
3025 /* Reject merges into a work tree with mixed base commits. */
3026 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3027 SHA1_DIGEST_LENGTH))
3028 return got_error(GOT_ERR_MIXED_COMMITS);
3030 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3031 == -1)
3032 return got_error_from_errno("asprintf");
3034 /* Reject merges into a work tree with conflicted files. */
3035 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3036 if (err)
3037 return err;
3038 if (status == GOT_STATUS_CONFLICT)
3039 return got_error(GOT_ERR_CONFLICTS);
3041 return NULL;
3044 static const struct got_error *
3045 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3046 const char *fileindex_path, struct got_object_id *commit_id1,
3047 struct got_object_id *commit_id2, struct got_repository *repo,
3048 got_worktree_checkout_cb progress_cb, void *progress_arg,
3049 got_cancel_cb cancel_cb, void *cancel_arg)
3051 const struct got_error *err = NULL, *sync_err;
3052 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3053 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3054 struct merge_file_cb_arg arg;
3055 char *label_orig = NULL;
3057 if (commit_id1) {
3058 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3059 worktree->path_prefix);
3060 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3061 goto done;
3063 if (tree_id1) {
3064 char *id_str;
3066 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3067 if (err)
3068 goto done;
3070 err = got_object_id_str(&id_str, commit_id1);
3071 if (err)
3072 goto done;
3074 if (asprintf(&label_orig, "%s: commit %s",
3075 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3076 err = got_error_from_errno("asprintf");
3077 free(id_str);
3078 goto done;
3080 free(id_str);
3083 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3084 worktree->path_prefix);
3085 if (err)
3086 goto done;
3088 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3089 if (err)
3090 goto done;
3092 arg.worktree = worktree;
3093 arg.fileindex = fileindex;
3094 arg.progress_cb = progress_cb;
3095 arg.progress_arg = progress_arg;
3096 arg.cancel_cb = cancel_cb;
3097 arg.cancel_arg = cancel_arg;
3098 arg.label_orig = label_orig;
3099 arg.commit_id2 = commit_id2;
3100 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3101 sync_err = sync_fileindex(fileindex, fileindex_path);
3102 if (sync_err && err == NULL)
3103 err = sync_err;
3104 done:
3105 if (tree1)
3106 got_object_tree_close(tree1);
3107 if (tree2)
3108 got_object_tree_close(tree2);
3109 free(label_orig);
3110 return err;
3113 const struct got_error *
3114 got_worktree_merge_files(struct got_worktree *worktree,
3115 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3116 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3117 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3119 const struct got_error *err, *unlockerr;
3120 char *fileindex_path = NULL;
3121 struct got_fileindex *fileindex = NULL;
3122 struct check_merge_ok_arg mok_arg;
3124 err = lock_worktree(worktree, LOCK_EX);
3125 if (err)
3126 return err;
3128 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3129 if (err)
3130 goto done;
3132 mok_arg.worktree = worktree;
3133 mok_arg.repo = repo;
3134 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3135 &mok_arg);
3136 if (err)
3137 goto done;
3139 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3140 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3141 done:
3142 if (fileindex)
3143 got_fileindex_free(fileindex);
3144 free(fileindex_path);
3145 unlockerr = lock_worktree(worktree, LOCK_SH);
3146 if (unlockerr && err == NULL)
3147 err = unlockerr;
3148 return err;
3151 struct diff_dir_cb_arg {
3152 struct got_fileindex *fileindex;
3153 struct got_worktree *worktree;
3154 const char *status_path;
3155 size_t status_path_len;
3156 struct got_repository *repo;
3157 got_worktree_status_cb status_cb;
3158 void *status_arg;
3159 got_cancel_cb cancel_cb;
3160 void *cancel_arg;
3161 /* A pathlist containing per-directory pathlists of ignore patterns. */
3162 struct got_pathlist_head ignores;
3163 int report_unchanged;
3164 int no_ignores;
3167 static const struct got_error *
3168 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3169 int dirfd, const char *de_name,
3170 got_worktree_status_cb status_cb, void *status_arg,
3171 struct got_repository *repo, int report_unchanged)
3173 const struct got_error *err = NULL;
3174 unsigned char status = GOT_STATUS_NO_CHANGE;
3175 unsigned char staged_status = get_staged_status(ie);
3176 struct stat sb;
3177 struct got_object_id blob_id, commit_id, staged_blob_id;
3178 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3179 struct got_object_id *staged_blob_idp = NULL;
3181 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3182 if (err)
3183 return err;
3185 if (status == GOT_STATUS_NO_CHANGE &&
3186 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3187 return NULL;
3189 if (got_fileindex_entry_has_blob(ie)) {
3190 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3191 blob_idp = &blob_id;
3193 if (got_fileindex_entry_has_commit(ie)) {
3194 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3195 commit_idp = &commit_id;
3197 if (staged_status == GOT_STATUS_ADD ||
3198 staged_status == GOT_STATUS_MODIFY) {
3199 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3200 SHA1_DIGEST_LENGTH);
3201 staged_blob_idp = &staged_blob_id;
3204 return (*status_cb)(status_arg, status, staged_status,
3205 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3208 static const struct got_error *
3209 status_old_new(void *arg, struct got_fileindex_entry *ie,
3210 struct dirent *de, const char *parent_path, int dirfd)
3212 const struct got_error *err = NULL;
3213 struct diff_dir_cb_arg *a = arg;
3214 char *abspath;
3216 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3217 return got_error(GOT_ERR_CANCELLED);
3219 if (got_path_cmp(parent_path, a->status_path,
3220 strlen(parent_path), a->status_path_len) != 0 &&
3221 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3222 return NULL;
3224 if (parent_path[0]) {
3225 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3226 parent_path, de->d_name) == -1)
3227 return got_error_from_errno("asprintf");
3228 } else {
3229 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3230 de->d_name) == -1)
3231 return got_error_from_errno("asprintf");
3234 err = report_file_status(ie, abspath, dirfd, de->d_name,
3235 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3236 free(abspath);
3237 return err;
3240 static const struct got_error *
3241 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3243 struct diff_dir_cb_arg *a = arg;
3244 struct got_object_id blob_id, commit_id;
3245 unsigned char status;
3247 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3248 return got_error(GOT_ERR_CANCELLED);
3250 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3251 return NULL;
3253 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3254 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3255 if (got_fileindex_entry_has_file_on_disk(ie))
3256 status = GOT_STATUS_MISSING;
3257 else
3258 status = GOT_STATUS_DELETE;
3259 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3260 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3263 void
3264 free_ignorelist(struct got_pathlist_head *ignorelist)
3266 struct got_pathlist_entry *pe;
3268 TAILQ_FOREACH(pe, ignorelist, entry)
3269 free((char *)pe->path);
3270 got_pathlist_free(ignorelist);
3273 void
3274 free_ignores(struct got_pathlist_head *ignores)
3276 struct got_pathlist_entry *pe;
3278 TAILQ_FOREACH(pe, ignores, entry) {
3279 struct got_pathlist_head *ignorelist = pe->data;
3280 free_ignorelist(ignorelist);
3281 free((char *)pe->path);
3283 got_pathlist_free(ignores);
3286 static const struct got_error *
3287 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3289 const struct got_error *err = NULL;
3290 struct got_pathlist_entry *pe = NULL;
3291 struct got_pathlist_head *ignorelist;
3292 char *line = NULL, *pattern, *dirpath = NULL;
3293 size_t linesize = 0;
3294 ssize_t linelen;
3296 ignorelist = calloc(1, sizeof(*ignorelist));
3297 if (ignorelist == NULL)
3298 return got_error_from_errno("calloc");
3299 TAILQ_INIT(ignorelist);
3301 while ((linelen = getline(&line, &linesize, f)) != -1) {
3302 if (linelen > 0 && line[linelen - 1] == '\n')
3303 line[linelen - 1] = '\0';
3305 /* Git's ignores may contain comments. */
3306 if (line[0] == '#')
3307 continue;
3309 /* Git's negated patterns are not (yet?) supported. */
3310 if (line[0] == '!')
3311 continue;
3313 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3314 line) == -1) {
3315 err = got_error_from_errno("asprintf");
3316 goto done;
3318 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3319 if (err)
3320 goto done;
3322 if (ferror(f)) {
3323 err = got_error_from_errno("getline");
3324 goto done;
3327 dirpath = strdup(path);
3328 if (dirpath == NULL) {
3329 err = got_error_from_errno("strdup");
3330 goto done;
3332 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3333 done:
3334 free(line);
3335 if (err || pe == NULL) {
3336 free(dirpath);
3337 free_ignorelist(ignorelist);
3339 return err;
3342 int
3343 match_ignores(struct got_pathlist_head *ignores, const char *path)
3345 struct got_pathlist_entry *pe;
3347 /* Handle patterns which match in all directories. */
3348 TAILQ_FOREACH(pe, ignores, entry) {
3349 struct got_pathlist_head *ignorelist = pe->data;
3350 struct got_pathlist_entry *pi;
3352 TAILQ_FOREACH(pi, ignorelist, entry) {
3353 const char *p, *pattern = pi->path;
3355 if (strncmp(pattern, "**/", 3) != 0)
3356 continue;
3357 pattern += 3;
3358 p = path;
3359 while (*p) {
3360 if (fnmatch(pattern, p,
3361 FNM_PATHNAME | FNM_LEADING_DIR)) {
3362 /* Retry in next directory. */
3363 while (*p && *p != '/')
3364 p++;
3365 while (*p == '/')
3366 p++;
3367 continue;
3369 return 1;
3375 * The ignores pathlist contains ignore lists from children before
3376 * parents, so we can find the most specific ignorelist by walking
3377 * ignores backwards.
3379 pe = TAILQ_LAST(ignores, got_pathlist_head);
3380 while (pe) {
3381 if (got_path_is_child(path, pe->path, pe->path_len)) {
3382 struct got_pathlist_head *ignorelist = pe->data;
3383 struct got_pathlist_entry *pi;
3384 TAILQ_FOREACH(pi, ignorelist, entry) {
3385 const char *pattern = pi->path;
3386 int flags = FNM_LEADING_DIR;
3387 if (strstr(pattern, "/**/") == NULL)
3388 flags |= FNM_PATHNAME;
3389 if (fnmatch(pattern, path, flags))
3390 continue;
3391 return 1;
3394 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3397 return 0;
3400 static const struct got_error *
3401 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3402 const char *path, int dirfd, const char *ignores_filename)
3404 const struct got_error *err = NULL;
3405 char *ignorespath;
3406 int fd = -1;
3407 FILE *ignoresfile = NULL;
3409 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3410 path[0] ? "/" : "", ignores_filename) == -1)
3411 return got_error_from_errno("asprintf");
3413 if (dirfd != -1) {
3414 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3415 if (fd == -1) {
3416 if (errno != ENOENT && errno != EACCES)
3417 err = got_error_from_errno2("openat",
3418 ignorespath);
3419 } else {
3420 ignoresfile = fdopen(fd, "r");
3421 if (ignoresfile == NULL)
3422 err = got_error_from_errno2("fdopen",
3423 ignorespath);
3424 else {
3425 fd = -1;
3426 err = read_ignores(ignores, path, ignoresfile);
3429 } else {
3430 ignoresfile = fopen(ignorespath, "r");
3431 if (ignoresfile == NULL) {
3432 if (errno != ENOENT && errno != EACCES)
3433 err = got_error_from_errno2("fopen",
3434 ignorespath);
3435 } else
3436 err = read_ignores(ignores, path, ignoresfile);
3439 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3440 err = got_error_from_errno2("fclose", path);
3441 if (fd != -1 && close(fd) == -1 && err == NULL)
3442 err = got_error_from_errno2("close", path);
3443 free(ignorespath);
3444 return err;
3447 static const struct got_error *
3448 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3450 const struct got_error *err = NULL;
3451 struct diff_dir_cb_arg *a = arg;
3452 char *path = NULL;
3454 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3455 return got_error(GOT_ERR_CANCELLED);
3457 if (parent_path[0]) {
3458 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3459 return got_error_from_errno("asprintf");
3460 } else {
3461 path = de->d_name;
3464 if (de->d_type != DT_DIR &&
3465 got_path_is_child(path, a->status_path, a->status_path_len)
3466 && !match_ignores(&a->ignores, path))
3467 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3468 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3469 if (parent_path[0])
3470 free(path);
3471 return err;
3474 static const struct got_error *
3475 status_traverse(void *arg, const char *path, int dirfd)
3477 const struct got_error *err = NULL;
3478 struct diff_dir_cb_arg *a = arg;
3480 if (a->no_ignores)
3481 return NULL;
3483 err = add_ignores(&a->ignores, a->worktree->root_path,
3484 path, dirfd, ".cvsignore");
3485 if (err)
3486 return err;
3488 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3489 dirfd, ".gitignore");
3491 return err;
3494 static const struct got_error *
3495 report_single_file_status(const char *path, const char *ondisk_path,
3496 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3497 void *status_arg, struct got_repository *repo, int report_unchanged)
3499 struct got_fileindex_entry *ie;
3500 struct stat sb;
3502 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3503 if (ie)
3504 return report_file_status(ie, ondisk_path, -1, NULL,
3505 status_cb, status_arg, repo, report_unchanged);
3507 if (lstat(ondisk_path, &sb) == -1) {
3508 if (errno != ENOENT)
3509 return got_error_from_errno2("lstat", ondisk_path);
3510 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3511 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3512 return NULL;
3515 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3516 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3517 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3519 return NULL;
3522 static const struct got_error *
3523 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3524 const char *root_path, const char *path)
3526 const struct got_error *err;
3527 char *parent_path, *next_parent_path = NULL;
3529 err = add_ignores(ignores, root_path, "", -1,
3530 ".cvsignore");
3531 if (err)
3532 return err;
3534 err = add_ignores(ignores, root_path, "", -1,
3535 ".gitignore");
3536 if (err)
3537 return err;
3539 err = got_path_dirname(&parent_path, path);
3540 if (err) {
3541 if (err->code == GOT_ERR_BAD_PATH)
3542 return NULL; /* cannot traverse parent */
3543 return err;
3545 for (;;) {
3546 err = add_ignores(ignores, root_path, parent_path, -1,
3547 ".cvsignore");
3548 if (err)
3549 break;
3550 err = add_ignores(ignores, root_path, parent_path, -1,
3551 ".gitignore");
3552 if (err)
3553 break;
3554 err = got_path_dirname(&next_parent_path, parent_path);
3555 if (err) {
3556 if (err->code == GOT_ERR_BAD_PATH)
3557 err = NULL; /* traversed everything */
3558 break;
3560 free(parent_path);
3561 parent_path = next_parent_path;
3562 next_parent_path = NULL;
3565 free(parent_path);
3566 free(next_parent_path);
3567 return err;
3570 static const struct got_error *
3571 worktree_status(struct got_worktree *worktree, const char *path,
3572 struct got_fileindex *fileindex, struct got_repository *repo,
3573 got_worktree_status_cb status_cb, void *status_arg,
3574 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3575 int report_unchanged)
3577 const struct got_error *err = NULL;
3578 int fd = -1;
3579 struct got_fileindex_diff_dir_cb fdiff_cb;
3580 struct diff_dir_cb_arg arg;
3581 char *ondisk_path = NULL;
3583 TAILQ_INIT(&arg.ignores);
3585 if (asprintf(&ondisk_path, "%s%s%s",
3586 worktree->root_path, path[0] ? "/" : "", path) == -1)
3587 return got_error_from_errno("asprintf");
3589 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3590 if (fd == -1) {
3591 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3592 errno != ELOOP)
3593 err = got_error_from_errno2("open", ondisk_path);
3594 else
3595 err = report_single_file_status(path, ondisk_path,
3596 fileindex, status_cb, status_arg, repo,
3597 report_unchanged);
3598 } else {
3599 fdiff_cb.diff_old_new = status_old_new;
3600 fdiff_cb.diff_old = status_old;
3601 fdiff_cb.diff_new = status_new;
3602 fdiff_cb.diff_traverse = status_traverse;
3603 arg.fileindex = fileindex;
3604 arg.worktree = worktree;
3605 arg.status_path = path;
3606 arg.status_path_len = strlen(path);
3607 arg.repo = repo;
3608 arg.status_cb = status_cb;
3609 arg.status_arg = status_arg;
3610 arg.cancel_cb = cancel_cb;
3611 arg.cancel_arg = cancel_arg;
3612 arg.report_unchanged = report_unchanged;
3613 arg.no_ignores = no_ignores;
3614 if (!no_ignores) {
3615 err = add_ignores_from_parent_paths(&arg.ignores,
3616 worktree->root_path, path);
3617 if (err)
3618 goto done;
3620 err = got_fileindex_diff_dir(fileindex, fd,
3621 worktree->root_path, path, repo, &fdiff_cb, &arg);
3623 done:
3624 free_ignores(&arg.ignores);
3625 if (fd != -1 && close(fd) != 0 && err == NULL)
3626 err = got_error_from_errno("close");
3627 free(ondisk_path);
3628 return err;
3631 const struct got_error *
3632 got_worktree_status(struct got_worktree *worktree,
3633 struct got_pathlist_head *paths, struct got_repository *repo,
3634 got_worktree_status_cb status_cb, void *status_arg,
3635 got_cancel_cb cancel_cb, void *cancel_arg)
3637 const struct got_error *err = NULL;
3638 char *fileindex_path = NULL;
3639 struct got_fileindex *fileindex = NULL;
3640 struct got_pathlist_entry *pe;
3642 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3643 if (err)
3644 return err;
3646 TAILQ_FOREACH(pe, paths, entry) {
3647 err = worktree_status(worktree, pe->path, fileindex, repo,
3648 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3649 if (err)
3650 break;
3652 free(fileindex_path);
3653 got_fileindex_free(fileindex);
3654 return err;
3657 const struct got_error *
3658 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3659 const char *arg)
3661 const struct got_error *err = NULL;
3662 char *resolved = NULL, *cwd = NULL, *path = NULL;
3663 size_t len;
3664 struct stat sb;
3665 char *abspath = NULL;
3666 char canonpath[PATH_MAX];
3668 *wt_path = NULL;
3670 cwd = getcwd(NULL, 0);
3671 if (cwd == NULL)
3672 return got_error_from_errno("getcwd");
3674 if (lstat(arg, &sb) == -1) {
3675 if (errno != ENOENT) {
3676 err = got_error_from_errno2("lstat", arg);
3677 goto done;
3679 sb.st_mode = 0;
3681 if (S_ISLNK(sb.st_mode)) {
3683 * We cannot use realpath(3) with symlinks since we want to
3684 * operate on the symlink itself.
3685 * But we can make the path absolute, assuming it is relative
3686 * to the current working directory, and then canonicalize it.
3688 if (!got_path_is_absolute(arg)) {
3689 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3690 err = got_error_from_errno("asprintf");
3691 goto done;
3695 err = got_canonpath(abspath ? abspath : arg, canonpath,
3696 sizeof(canonpath));
3697 if (err)
3698 goto done;
3699 resolved = strdup(canonpath);
3700 if (resolved == NULL) {
3701 err = got_error_from_errno("strdup");
3702 goto done;
3704 } else {
3705 resolved = realpath(arg, NULL);
3706 if (resolved == NULL) {
3707 if (errno != ENOENT) {
3708 err = got_error_from_errno2("realpath", arg);
3709 goto done;
3711 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3712 err = got_error_from_errno("asprintf");
3713 goto done;
3715 err = got_canonpath(abspath, canonpath,
3716 sizeof(canonpath));
3717 if (err)
3718 goto done;
3719 resolved = strdup(canonpath);
3720 if (resolved == NULL) {
3721 err = got_error_from_errno("strdup");
3722 goto done;
3727 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3728 strlen(got_worktree_get_root_path(worktree)))) {
3729 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3730 goto done;
3733 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3734 err = got_path_skip_common_ancestor(&path,
3735 got_worktree_get_root_path(worktree), resolved);
3736 if (err)
3737 goto done;
3738 } else {
3739 path = strdup("");
3740 if (path == NULL) {
3741 err = got_error_from_errno("strdup");
3742 goto done;
3746 /* XXX status walk can't deal with trailing slash! */
3747 len = strlen(path);
3748 while (len > 0 && path[len - 1] == '/') {
3749 path[len - 1] = '\0';
3750 len--;
3752 done:
3753 free(abspath);
3754 free(resolved);
3755 free(cwd);
3756 if (err == NULL)
3757 *wt_path = path;
3758 else
3759 free(path);
3760 return err;
3763 struct schedule_addition_args {
3764 struct got_worktree *worktree;
3765 struct got_fileindex *fileindex;
3766 got_worktree_checkout_cb progress_cb;
3767 void *progress_arg;
3768 struct got_repository *repo;
3771 static const struct got_error *
3772 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3773 const char *relpath, struct got_object_id *blob_id,
3774 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3775 int dirfd, const char *de_name)
3777 struct schedule_addition_args *a = arg;
3778 const struct got_error *err = NULL;
3779 struct got_fileindex_entry *ie;
3780 struct stat sb;
3781 char *ondisk_path;
3783 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3784 relpath) == -1)
3785 return got_error_from_errno("asprintf");
3787 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3788 if (ie) {
3789 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3790 de_name, a->repo);
3791 if (err)
3792 goto done;
3793 /* Re-adding an existing entry is a no-op. */
3794 if (status == GOT_STATUS_ADD)
3795 goto done;
3796 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3797 if (err)
3798 goto done;
3801 if (status != GOT_STATUS_UNVERSIONED) {
3802 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3803 goto done;
3806 err = got_fileindex_entry_alloc(&ie, relpath);
3807 if (err)
3808 goto done;
3809 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3810 relpath, NULL, NULL, 1);
3811 if (err) {
3812 got_fileindex_entry_free(ie);
3813 goto done;
3815 err = got_fileindex_entry_add(a->fileindex, ie);
3816 if (err) {
3817 got_fileindex_entry_free(ie);
3818 goto done;
3820 done:
3821 free(ondisk_path);
3822 if (err)
3823 return err;
3824 if (status == GOT_STATUS_ADD)
3825 return NULL;
3826 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3829 const struct got_error *
3830 got_worktree_schedule_add(struct got_worktree *worktree,
3831 struct got_pathlist_head *paths,
3832 got_worktree_checkout_cb progress_cb, void *progress_arg,
3833 struct got_repository *repo, int no_ignores)
3835 struct got_fileindex *fileindex = NULL;
3836 char *fileindex_path = NULL;
3837 const struct got_error *err = NULL, *sync_err, *unlockerr;
3838 struct got_pathlist_entry *pe;
3839 struct schedule_addition_args saa;
3841 err = lock_worktree(worktree, LOCK_EX);
3842 if (err)
3843 return err;
3845 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3846 if (err)
3847 goto done;
3849 saa.worktree = worktree;
3850 saa.fileindex = fileindex;
3851 saa.progress_cb = progress_cb;
3852 saa.progress_arg = progress_arg;
3853 saa.repo = repo;
3855 TAILQ_FOREACH(pe, paths, entry) {
3856 err = worktree_status(worktree, pe->path, fileindex, repo,
3857 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3858 if (err)
3859 break;
3861 sync_err = sync_fileindex(fileindex, fileindex_path);
3862 if (sync_err && err == NULL)
3863 err = sync_err;
3864 done:
3865 free(fileindex_path);
3866 if (fileindex)
3867 got_fileindex_free(fileindex);
3868 unlockerr = lock_worktree(worktree, LOCK_SH);
3869 if (unlockerr && err == NULL)
3870 err = unlockerr;
3871 return err;
3874 struct schedule_deletion_args {
3875 struct got_worktree *worktree;
3876 struct got_fileindex *fileindex;
3877 got_worktree_delete_cb progress_cb;
3878 void *progress_arg;
3879 struct got_repository *repo;
3880 int delete_local_mods;
3881 int keep_on_disk;
3882 const char *status_codes;
3885 static const struct got_error *
3886 schedule_for_deletion(void *arg, unsigned char status,
3887 unsigned char staged_status, const char *relpath,
3888 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3889 struct got_object_id *commit_id, int dirfd, const char *de_name)
3891 struct schedule_deletion_args *a = arg;
3892 const struct got_error *err = NULL;
3893 struct got_fileindex_entry *ie = NULL;
3894 struct stat sb;
3895 char *ondisk_path;
3897 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3898 if (ie == NULL)
3899 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3901 staged_status = get_staged_status(ie);
3902 if (staged_status != GOT_STATUS_NO_CHANGE) {
3903 if (staged_status == GOT_STATUS_DELETE)
3904 return NULL;
3905 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3908 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3909 relpath) == -1)
3910 return got_error_from_errno("asprintf");
3912 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3913 a->repo);
3914 if (err)
3915 goto done;
3917 if (a->status_codes) {
3918 size_t ncodes = strlen(a->status_codes);
3919 int i;
3920 for (i = 0; i < ncodes ; i++) {
3921 if (status == a->status_codes[i])
3922 break;
3924 if (i == ncodes) {
3925 /* Do not delete files in non-matching status. */
3926 free(ondisk_path);
3927 return NULL;
3929 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3930 a->status_codes[i] != GOT_STATUS_MISSING) {
3931 static char msg[64];
3932 snprintf(msg, sizeof(msg),
3933 "invalid status code '%c'", a->status_codes[i]);
3934 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3935 goto done;
3939 if (status != GOT_STATUS_NO_CHANGE) {
3940 if (status == GOT_STATUS_DELETE)
3941 goto done;
3942 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3943 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3944 goto done;
3946 if (status != GOT_STATUS_MODIFY &&
3947 status != GOT_STATUS_MISSING) {
3948 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3949 goto done;
3953 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3954 size_t root_len;
3956 if (dirfd != -1) {
3957 if (unlinkat(dirfd, de_name, 0) != 0) {
3958 err = got_error_from_errno2("unlinkat",
3959 ondisk_path);
3960 goto done;
3962 } else if (unlink(ondisk_path) != 0) {
3963 err = got_error_from_errno2("unlink", ondisk_path);
3964 goto done;
3967 root_len = strlen(a->worktree->root_path);
3968 do {
3969 char *parent;
3970 err = got_path_dirname(&parent, ondisk_path);
3971 if (err)
3972 goto done;
3973 free(ondisk_path);
3974 ondisk_path = parent;
3975 if (rmdir(ondisk_path) == -1) {
3976 if (errno != ENOTEMPTY)
3977 err = got_error_from_errno2("rmdir",
3978 ondisk_path);
3979 break;
3981 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3982 strlen(ondisk_path), root_len) != 0);
3985 got_fileindex_entry_mark_deleted_from_disk(ie);
3986 done:
3987 free(ondisk_path);
3988 if (err)
3989 return err;
3990 if (status == GOT_STATUS_DELETE)
3991 return NULL;
3992 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3993 staged_status, relpath);
3996 const struct got_error *
3997 got_worktree_schedule_delete(struct got_worktree *worktree,
3998 struct got_pathlist_head *paths, int delete_local_mods,
3999 const char *status_codes,
4000 got_worktree_delete_cb progress_cb, void *progress_arg,
4001 struct got_repository *repo, int keep_on_disk)
4003 struct got_fileindex *fileindex = NULL;
4004 char *fileindex_path = NULL;
4005 const struct got_error *err = NULL, *sync_err, *unlockerr;
4006 struct got_pathlist_entry *pe;
4007 struct schedule_deletion_args sda;
4009 err = lock_worktree(worktree, LOCK_EX);
4010 if (err)
4011 return err;
4013 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4014 if (err)
4015 goto done;
4017 sda.worktree = worktree;
4018 sda.fileindex = fileindex;
4019 sda.progress_cb = progress_cb;
4020 sda.progress_arg = progress_arg;
4021 sda.repo = repo;
4022 sda.delete_local_mods = delete_local_mods;
4023 sda.keep_on_disk = keep_on_disk;
4024 sda.status_codes = status_codes;
4026 TAILQ_FOREACH(pe, paths, entry) {
4027 err = worktree_status(worktree, pe->path, fileindex, repo,
4028 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4029 if (err)
4030 break;
4032 sync_err = sync_fileindex(fileindex, fileindex_path);
4033 if (sync_err && err == NULL)
4034 err = sync_err;
4035 done:
4036 free(fileindex_path);
4037 if (fileindex)
4038 got_fileindex_free(fileindex);
4039 unlockerr = lock_worktree(worktree, LOCK_SH);
4040 if (unlockerr && err == NULL)
4041 err = unlockerr;
4042 return err;
4045 static const struct got_error *
4046 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4048 const struct got_error *err = NULL;
4049 char *line = NULL;
4050 size_t linesize = 0, n;
4051 ssize_t linelen;
4053 linelen = getline(&line, &linesize, infile);
4054 if (linelen == -1) {
4055 if (ferror(infile)) {
4056 err = got_error_from_errno("getline");
4057 goto done;
4059 return NULL;
4061 if (outfile) {
4062 n = fwrite(line, 1, linelen, outfile);
4063 if (n != linelen) {
4064 err = got_ferror(outfile, GOT_ERR_IO);
4065 goto done;
4068 if (rejectfile) {
4069 n = fwrite(line, 1, linelen, rejectfile);
4070 if (n != linelen)
4071 err = got_ferror(outfile, GOT_ERR_IO);
4073 done:
4074 free(line);
4075 return err;
4078 static const struct got_error *
4079 skip_one_line(FILE *f)
4081 char *line = NULL;
4082 size_t linesize = 0;
4083 ssize_t linelen;
4085 linelen = getline(&line, &linesize, f);
4086 if (linelen == -1) {
4087 if (ferror(f))
4088 return got_error_from_errno("getline");
4089 return NULL;
4091 free(line);
4092 return NULL;
4095 static const struct got_error *
4096 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4097 int start_old, int end_old, int start_new, int end_new,
4098 FILE *outfile, FILE *rejectfile)
4100 const struct got_error *err;
4102 /* Copy old file's lines leading up to patch. */
4103 while (!feof(f1) && *line_cur1 < start_old) {
4104 err = copy_one_line(f1, outfile, NULL);
4105 if (err)
4106 return err;
4107 (*line_cur1)++;
4109 /* Skip new file's lines leading up to patch. */
4110 while (!feof(f2) && *line_cur2 < start_new) {
4111 if (rejectfile)
4112 err = copy_one_line(f2, NULL, rejectfile);
4113 else
4114 err = skip_one_line(f2);
4115 if (err)
4116 return err;
4117 (*line_cur2)++;
4119 /* Copy patched lines. */
4120 while (!feof(f2) && *line_cur2 <= end_new) {
4121 err = copy_one_line(f2, outfile, NULL);
4122 if (err)
4123 return err;
4124 (*line_cur2)++;
4126 /* Skip over old file's replaced lines. */
4127 while (!feof(f1) && *line_cur1 <= end_old) {
4128 if (rejectfile)
4129 err = copy_one_line(f1, NULL, rejectfile);
4130 else
4131 err = skip_one_line(f1);
4132 if (err)
4133 return err;
4134 (*line_cur1)++;
4137 return NULL;
4140 static const struct got_error *
4141 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4142 FILE *outfile, FILE *rejectfile)
4144 const struct got_error *err;
4146 if (outfile) {
4147 /* Copy old file's lines until EOF. */
4148 while (!feof(f1)) {
4149 err = copy_one_line(f1, outfile, NULL);
4150 if (err)
4151 return err;
4152 (*line_cur1)++;
4155 if (rejectfile) {
4156 /* Copy new file's lines until EOF. */
4157 while (!feof(f2)) {
4158 err = copy_one_line(f2, NULL, rejectfile);
4159 if (err)
4160 return err;
4161 (*line_cur2)++;
4165 return NULL;
4168 static const struct got_error *
4169 apply_or_reject_change(int *choice, int *nchunks_used,
4170 struct diff_result *diff_result, int n,
4171 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4172 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4173 got_worktree_patch_cb patch_cb, void *patch_arg)
4175 const struct got_error *err = NULL;
4176 struct diff_chunk_context cc = {};
4177 int start_old, end_old, start_new, end_new;
4178 FILE *hunkfile;
4179 struct diff_output_unidiff_state *diff_state;
4180 struct diff_input_info diff_info;
4181 int rc;
4183 *choice = GOT_PATCH_CHOICE_NONE;
4185 /* Get changed line numbers without context lines for copy_change(). */
4186 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4187 start_old = cc.left.start;
4188 end_old = cc.left.end;
4189 start_new = cc.right.start;
4190 end_new = cc.right.end;
4192 /* Get the same change with context lines for display. */
4193 memset(&cc, 0, sizeof(cc));
4194 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4196 memset(&diff_info, 0, sizeof(diff_info));
4197 diff_info.left_path = relpath;
4198 diff_info.right_path = relpath;
4200 diff_state = diff_output_unidiff_state_alloc();
4201 if (diff_state == NULL)
4202 return got_error_set_errno(ENOMEM,
4203 "diff_output_unidiff_state_alloc");
4205 hunkfile = got_opentemp();
4206 if (hunkfile == NULL) {
4207 err = got_error_from_errno("got_opentemp");
4208 goto done;
4211 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4212 diff_result, &cc);
4213 if (rc != DIFF_RC_OK) {
4214 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4215 goto done;
4218 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4219 err = got_ferror(hunkfile, GOT_ERR_IO);
4220 goto done;
4223 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4224 hunkfile, changeno, nchanges);
4225 if (err)
4226 goto done;
4228 switch (*choice) {
4229 case GOT_PATCH_CHOICE_YES:
4230 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4231 end_old, start_new, end_new, outfile, rejectfile);
4232 break;
4233 case GOT_PATCH_CHOICE_NO:
4234 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4235 end_old, start_new, end_new, rejectfile, outfile);
4236 break;
4237 case GOT_PATCH_CHOICE_QUIT:
4238 break;
4239 default:
4240 err = got_error(GOT_ERR_PATCH_CHOICE);
4241 break;
4243 done:
4244 diff_output_unidiff_state_free(diff_state);
4245 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4246 err = got_error_from_errno("fclose");
4247 return err;
4250 struct revert_file_args {
4251 struct got_worktree *worktree;
4252 struct got_fileindex *fileindex;
4253 got_worktree_checkout_cb progress_cb;
4254 void *progress_arg;
4255 got_worktree_patch_cb patch_cb;
4256 void *patch_arg;
4257 struct got_repository *repo;
4260 static const struct got_error *
4261 create_patched_content(char **path_outfile, int reverse_patch,
4262 struct got_object_id *blob_id, const char *path2,
4263 int dirfd2, const char *de_name2,
4264 const char *relpath, struct got_repository *repo,
4265 got_worktree_patch_cb patch_cb, void *patch_arg)
4267 const struct got_error *err, *free_err;
4268 struct got_blob_object *blob = NULL;
4269 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4270 int fd2 = -1;
4271 char link_target[PATH_MAX];
4272 ssize_t link_len = 0;
4273 char *path1 = NULL, *id_str = NULL;
4274 struct stat sb2;
4275 struct got_diffreg_result *diffreg_result = NULL;
4276 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4277 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4279 *path_outfile = NULL;
4281 err = got_object_id_str(&id_str, blob_id);
4282 if (err)
4283 return err;
4285 if (dirfd2 != -1) {
4286 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4287 if (fd2 == -1) {
4288 if (errno != ELOOP) {
4289 err = got_error_from_errno2("openat", path2);
4290 goto done;
4292 link_len = readlinkat(dirfd2, de_name2,
4293 link_target, sizeof(link_target));
4294 if (link_len == -1)
4295 return got_error_from_errno2("readlinkat", path2);
4296 sb2.st_mode = S_IFLNK;
4297 sb2.st_size = link_len;
4299 } else {
4300 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4301 if (fd2 == -1) {
4302 if (errno != ELOOP) {
4303 err = got_error_from_errno2("open", path2);
4304 goto done;
4306 link_len = readlink(path2, link_target,
4307 sizeof(link_target));
4308 if (link_len == -1)
4309 return got_error_from_errno2("readlink", path2);
4310 sb2.st_mode = S_IFLNK;
4311 sb2.st_size = link_len;
4314 if (fd2 != -1) {
4315 if (fstat(fd2, &sb2) == -1) {
4316 err = got_error_from_errno2("fstat", path2);
4317 goto done;
4320 f2 = fdopen(fd2, "r");
4321 if (f2 == NULL) {
4322 err = got_error_from_errno2("fdopen", path2);
4323 goto done;
4325 fd2 = -1;
4326 } else {
4327 size_t n;
4328 f2 = got_opentemp();
4329 if (f2 == NULL) {
4330 err = got_error_from_errno2("got_opentemp", path2);
4331 goto done;
4333 n = fwrite(link_target, 1, link_len, f2);
4334 if (n != link_len) {
4335 err = got_ferror(f2, GOT_ERR_IO);
4336 goto done;
4338 if (fflush(f2) == EOF) {
4339 err = got_error_from_errno("fflush");
4340 goto done;
4342 rewind(f2);
4345 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4346 if (err)
4347 goto done;
4349 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4350 if (err)
4351 goto done;
4353 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4354 if (err)
4355 goto done;
4357 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4358 NULL);
4359 if (err)
4360 goto done;
4362 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4363 if (err)
4364 goto done;
4366 if (fseek(f1, 0L, SEEK_SET) == -1)
4367 return got_ferror(f1, GOT_ERR_IO);
4368 if (fseek(f2, 0L, SEEK_SET) == -1)
4369 return got_ferror(f2, GOT_ERR_IO);
4371 /* Count the number of actual changes in the diff result. */
4372 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4373 struct diff_chunk_context cc = {};
4374 diff_chunk_context_load_change(&cc, &nchunks_used,
4375 diffreg_result->result, n, 0);
4376 nchanges++;
4378 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4379 int choice;
4380 err = apply_or_reject_change(&choice, &nchunks_used,
4381 diffreg_result->result, n, relpath, f1, f2,
4382 &line_cur1, &line_cur2,
4383 reverse_patch ? NULL : outfile,
4384 reverse_patch ? outfile : NULL,
4385 ++i, nchanges, patch_cb, patch_arg);
4386 if (err)
4387 goto done;
4388 if (choice == GOT_PATCH_CHOICE_YES)
4389 have_content = 1;
4390 else if (choice == GOT_PATCH_CHOICE_QUIT)
4391 break;
4393 if (have_content) {
4394 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4395 reverse_patch ? NULL : outfile,
4396 reverse_patch ? outfile : NULL);
4397 if (err)
4398 goto done;
4400 if (!S_ISLNK(sb2.st_mode)) {
4401 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4402 err = got_error_from_errno2("fchmod", path2);
4403 goto done;
4407 done:
4408 free(id_str);
4409 if (blob)
4410 got_object_blob_close(blob);
4411 free_err = got_diffreg_result_free(diffreg_result);
4412 if (err == NULL)
4413 err = free_err;
4414 if (f1 && fclose(f1) == EOF && err == NULL)
4415 err = got_error_from_errno2("fclose", path1);
4416 if (f2 && fclose(f2) == EOF && err == NULL)
4417 err = got_error_from_errno2("fclose", path2);
4418 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4419 err = got_error_from_errno2("close", path2);
4420 if (outfile && fclose(outfile) == EOF && err == NULL)
4421 err = got_error_from_errno2("fclose", *path_outfile);
4422 if (path1 && unlink(path1) == -1 && err == NULL)
4423 err = got_error_from_errno2("unlink", path1);
4424 if (err || !have_content) {
4425 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4426 err = got_error_from_errno2("unlink", *path_outfile);
4427 free(*path_outfile);
4428 *path_outfile = NULL;
4430 free(path1);
4431 return err;
4434 static const struct got_error *
4435 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4436 const char *relpath, struct got_object_id *blob_id,
4437 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4438 int dirfd, const char *de_name)
4440 struct revert_file_args *a = arg;
4441 const struct got_error *err = NULL;
4442 char *parent_path = NULL;
4443 struct got_fileindex_entry *ie;
4444 struct got_tree_object *tree = NULL;
4445 struct got_object_id *tree_id = NULL;
4446 const struct got_tree_entry *te = NULL;
4447 char *tree_path = NULL, *te_name;
4448 char *ondisk_path = NULL, *path_content = NULL;
4449 struct got_blob_object *blob = NULL;
4451 /* Reverting a staged deletion is a no-op. */
4452 if (status == GOT_STATUS_DELETE &&
4453 staged_status != GOT_STATUS_NO_CHANGE)
4454 return NULL;
4456 if (status == GOT_STATUS_UNVERSIONED)
4457 return (*a->progress_cb)(a->progress_arg,
4458 GOT_STATUS_UNVERSIONED, relpath);
4460 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4461 if (ie == NULL)
4462 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4464 /* Construct in-repository path of tree which contains this blob. */
4465 err = got_path_dirname(&parent_path, ie->path);
4466 if (err) {
4467 if (err->code != GOT_ERR_BAD_PATH)
4468 goto done;
4469 parent_path = strdup("/");
4470 if (parent_path == NULL) {
4471 err = got_error_from_errno("strdup");
4472 goto done;
4475 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4476 tree_path = strdup(parent_path);
4477 if (tree_path == NULL) {
4478 err = got_error_from_errno("strdup");
4479 goto done;
4481 } else {
4482 if (got_path_is_root_dir(parent_path)) {
4483 tree_path = strdup(a->worktree->path_prefix);
4484 if (tree_path == NULL) {
4485 err = got_error_from_errno("strdup");
4486 goto done;
4488 } else {
4489 if (asprintf(&tree_path, "%s/%s",
4490 a->worktree->path_prefix, parent_path) == -1) {
4491 err = got_error_from_errno("asprintf");
4492 goto done;
4497 err = got_object_id_by_path(&tree_id, a->repo,
4498 a->worktree->base_commit_id, tree_path);
4499 if (err) {
4500 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4501 (status == GOT_STATUS_ADD ||
4502 staged_status == GOT_STATUS_ADD)))
4503 goto done;
4504 } else {
4505 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4506 if (err)
4507 goto done;
4509 err = got_path_basename(&te_name, ie->path);
4510 if (err)
4511 goto done;
4513 te = got_object_tree_find_entry(tree, te_name);
4514 free(te_name);
4515 if (te == NULL && status != GOT_STATUS_ADD &&
4516 staged_status != GOT_STATUS_ADD) {
4517 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4518 goto done;
4522 switch (status) {
4523 case GOT_STATUS_ADD:
4524 if (a->patch_cb) {
4525 int choice = GOT_PATCH_CHOICE_NONE;
4526 err = (*a->patch_cb)(&choice, a->patch_arg,
4527 status, ie->path, NULL, 1, 1);
4528 if (err)
4529 goto done;
4530 if (choice != GOT_PATCH_CHOICE_YES)
4531 break;
4533 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4534 ie->path);
4535 if (err)
4536 goto done;
4537 got_fileindex_entry_remove(a->fileindex, ie);
4538 break;
4539 case GOT_STATUS_DELETE:
4540 if (a->patch_cb) {
4541 int choice = GOT_PATCH_CHOICE_NONE;
4542 err = (*a->patch_cb)(&choice, a->patch_arg,
4543 status, ie->path, NULL, 1, 1);
4544 if (err)
4545 goto done;
4546 if (choice != GOT_PATCH_CHOICE_YES)
4547 break;
4549 /* fall through */
4550 case GOT_STATUS_MODIFY:
4551 case GOT_STATUS_MODE_CHANGE:
4552 case GOT_STATUS_CONFLICT:
4553 case GOT_STATUS_MISSING: {
4554 struct got_object_id id;
4555 if (staged_status == GOT_STATUS_ADD ||
4556 staged_status == GOT_STATUS_MODIFY) {
4557 memcpy(id.sha1, ie->staged_blob_sha1,
4558 SHA1_DIGEST_LENGTH);
4559 } else
4560 memcpy(id.sha1, ie->blob_sha1,
4561 SHA1_DIGEST_LENGTH);
4562 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4563 if (err)
4564 goto done;
4566 if (asprintf(&ondisk_path, "%s/%s",
4567 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4568 err = got_error_from_errno("asprintf");
4569 goto done;
4572 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4573 status == GOT_STATUS_CONFLICT)) {
4574 int is_bad_symlink = 0;
4575 err = create_patched_content(&path_content, 1, &id,
4576 ondisk_path, dirfd, de_name, ie->path, a->repo,
4577 a->patch_cb, a->patch_arg);
4578 if (err || path_content == NULL)
4579 break;
4580 if (te && S_ISLNK(te->mode)) {
4581 if (unlink(path_content) == -1) {
4582 err = got_error_from_errno2("unlink",
4583 path_content);
4584 break;
4586 err = install_symlink(&is_bad_symlink,
4587 a->worktree, ondisk_path, ie->path,
4588 blob, 0, 1, 0, a->repo,
4589 a->progress_cb, a->progress_arg);
4590 } else {
4591 if (rename(path_content, ondisk_path) == -1) {
4592 err = got_error_from_errno3("rename",
4593 path_content, ondisk_path);
4594 goto done;
4597 } else {
4598 int is_bad_symlink = 0;
4599 if (te && S_ISLNK(te->mode)) {
4600 err = install_symlink(&is_bad_symlink,
4601 a->worktree, ondisk_path, ie->path,
4602 blob, 0, 1, 0, a->repo,
4603 a->progress_cb, a->progress_arg);
4604 } else {
4605 err = install_blob(a->worktree, ondisk_path,
4606 ie->path,
4607 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4608 got_fileindex_perms_to_st(ie), blob,
4609 0, 1, 0, 0, a->repo,
4610 a->progress_cb, a->progress_arg);
4612 if (err)
4613 goto done;
4614 if (status == GOT_STATUS_DELETE ||
4615 status == GOT_STATUS_MODE_CHANGE) {
4616 err = got_fileindex_entry_update(ie,
4617 a->worktree->root_fd, relpath,
4618 blob->id.sha1,
4619 a->worktree->base_commit_id->sha1, 1);
4620 if (err)
4621 goto done;
4623 if (is_bad_symlink) {
4624 got_fileindex_entry_filetype_set(ie,
4625 GOT_FILEIDX_MODE_BAD_SYMLINK);
4628 break;
4630 default:
4631 break;
4633 done:
4634 free(ondisk_path);
4635 free(path_content);
4636 free(parent_path);
4637 free(tree_path);
4638 if (blob)
4639 got_object_blob_close(blob);
4640 if (tree)
4641 got_object_tree_close(tree);
4642 free(tree_id);
4643 return err;
4646 const struct got_error *
4647 got_worktree_revert(struct got_worktree *worktree,
4648 struct got_pathlist_head *paths,
4649 got_worktree_checkout_cb progress_cb, void *progress_arg,
4650 got_worktree_patch_cb patch_cb, void *patch_arg,
4651 struct got_repository *repo)
4653 struct got_fileindex *fileindex = NULL;
4654 char *fileindex_path = NULL;
4655 const struct got_error *err = NULL, *unlockerr = NULL;
4656 const struct got_error *sync_err = NULL;
4657 struct got_pathlist_entry *pe;
4658 struct revert_file_args rfa;
4660 err = lock_worktree(worktree, LOCK_EX);
4661 if (err)
4662 return err;
4664 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4665 if (err)
4666 goto done;
4668 rfa.worktree = worktree;
4669 rfa.fileindex = fileindex;
4670 rfa.progress_cb = progress_cb;
4671 rfa.progress_arg = progress_arg;
4672 rfa.patch_cb = patch_cb;
4673 rfa.patch_arg = patch_arg;
4674 rfa.repo = repo;
4675 TAILQ_FOREACH(pe, paths, entry) {
4676 err = worktree_status(worktree, pe->path, fileindex, repo,
4677 revert_file, &rfa, NULL, NULL, 0, 0);
4678 if (err)
4679 break;
4681 sync_err = sync_fileindex(fileindex, fileindex_path);
4682 if (sync_err && err == NULL)
4683 err = sync_err;
4684 done:
4685 free(fileindex_path);
4686 if (fileindex)
4687 got_fileindex_free(fileindex);
4688 unlockerr = lock_worktree(worktree, LOCK_SH);
4689 if (unlockerr && err == NULL)
4690 err = unlockerr;
4691 return err;
4694 static void
4695 free_commitable(struct got_commitable *ct)
4697 free(ct->path);
4698 free(ct->in_repo_path);
4699 free(ct->ondisk_path);
4700 free(ct->blob_id);
4701 free(ct->base_blob_id);
4702 free(ct->staged_blob_id);
4703 free(ct->base_commit_id);
4704 free(ct);
4707 struct collect_commitables_arg {
4708 struct got_pathlist_head *commitable_paths;
4709 struct got_repository *repo;
4710 struct got_worktree *worktree;
4711 struct got_fileindex *fileindex;
4712 int have_staged_files;
4713 int allow_bad_symlinks;
4716 static const struct got_error *
4717 collect_commitables(void *arg, unsigned char status,
4718 unsigned char staged_status, const char *relpath,
4719 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4720 struct got_object_id *commit_id, int dirfd, const char *de_name)
4722 struct collect_commitables_arg *a = arg;
4723 const struct got_error *err = NULL;
4724 struct got_commitable *ct = NULL;
4725 struct got_pathlist_entry *new = NULL;
4726 char *parent_path = NULL, *path = NULL;
4727 struct stat sb;
4729 if (a->have_staged_files) {
4730 if (staged_status != GOT_STATUS_MODIFY &&
4731 staged_status != GOT_STATUS_ADD &&
4732 staged_status != GOT_STATUS_DELETE)
4733 return NULL;
4734 } else {
4735 if (status == GOT_STATUS_CONFLICT)
4736 return got_error(GOT_ERR_COMMIT_CONFLICT);
4738 if (status != GOT_STATUS_MODIFY &&
4739 status != GOT_STATUS_MODE_CHANGE &&
4740 status != GOT_STATUS_ADD &&
4741 status != GOT_STATUS_DELETE)
4742 return NULL;
4745 if (asprintf(&path, "/%s", relpath) == -1) {
4746 err = got_error_from_errno("asprintf");
4747 goto done;
4749 if (strcmp(path, "/") == 0) {
4750 parent_path = strdup("");
4751 if (parent_path == NULL)
4752 return got_error_from_errno("strdup");
4753 } else {
4754 err = got_path_dirname(&parent_path, path);
4755 if (err)
4756 return err;
4759 ct = calloc(1, sizeof(*ct));
4760 if (ct == NULL) {
4761 err = got_error_from_errno("calloc");
4762 goto done;
4765 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4766 relpath) == -1) {
4767 err = got_error_from_errno("asprintf");
4768 goto done;
4771 if (staged_status == GOT_STATUS_ADD ||
4772 staged_status == GOT_STATUS_MODIFY) {
4773 struct got_fileindex_entry *ie;
4774 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4775 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4776 case GOT_FILEIDX_MODE_REGULAR_FILE:
4777 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4778 ct->mode = S_IFREG;
4779 break;
4780 case GOT_FILEIDX_MODE_SYMLINK:
4781 ct->mode = S_IFLNK;
4782 break;
4783 default:
4784 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4785 goto done;
4787 ct->mode |= got_fileindex_entry_perms_get(ie);
4788 } else if (status != GOT_STATUS_DELETE &&
4789 staged_status != GOT_STATUS_DELETE) {
4790 if (dirfd != -1) {
4791 if (fstatat(dirfd, de_name, &sb,
4792 AT_SYMLINK_NOFOLLOW) == -1) {
4793 err = got_error_from_errno2("fstatat",
4794 ct->ondisk_path);
4795 goto done;
4797 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4798 err = got_error_from_errno2("lstat", ct->ondisk_path);
4799 goto done;
4801 ct->mode = sb.st_mode;
4804 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4805 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4806 relpath) == -1) {
4807 err = got_error_from_errno("asprintf");
4808 goto done;
4811 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4812 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4813 int is_bad_symlink;
4814 char target_path[PATH_MAX];
4815 ssize_t target_len;
4816 target_len = readlink(ct->ondisk_path, target_path,
4817 sizeof(target_path));
4818 if (target_len == -1) {
4819 err = got_error_from_errno2("readlink",
4820 ct->ondisk_path);
4821 goto done;
4823 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4824 target_len, ct->ondisk_path, a->worktree->root_path);
4825 if (err)
4826 goto done;
4827 if (is_bad_symlink) {
4828 err = got_error_path(ct->ondisk_path,
4829 GOT_ERR_BAD_SYMLINK);
4830 goto done;
4835 ct->status = status;
4836 ct->staged_status = staged_status;
4837 ct->blob_id = NULL; /* will be filled in when blob gets created */
4838 if (ct->status != GOT_STATUS_ADD &&
4839 ct->staged_status != GOT_STATUS_ADD) {
4840 ct->base_blob_id = got_object_id_dup(blob_id);
4841 if (ct->base_blob_id == NULL) {
4842 err = got_error_from_errno("got_object_id_dup");
4843 goto done;
4845 ct->base_commit_id = got_object_id_dup(commit_id);
4846 if (ct->base_commit_id == NULL) {
4847 err = got_error_from_errno("got_object_id_dup");
4848 goto done;
4851 if (ct->staged_status == GOT_STATUS_ADD ||
4852 ct->staged_status == GOT_STATUS_MODIFY) {
4853 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4854 if (ct->staged_blob_id == NULL) {
4855 err = got_error_from_errno("got_object_id_dup");
4856 goto done;
4859 ct->path = strdup(path);
4860 if (ct->path == NULL) {
4861 err = got_error_from_errno("strdup");
4862 goto done;
4864 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4865 done:
4866 if (ct && (err || new == NULL))
4867 free_commitable(ct);
4868 free(parent_path);
4869 free(path);
4870 return err;
4873 static const struct got_error *write_tree(struct got_object_id **, int *,
4874 struct got_tree_object *, const char *, struct got_pathlist_head *,
4875 got_worktree_status_cb status_cb, void *status_arg,
4876 struct got_repository *);
4878 static const struct got_error *
4879 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4880 struct got_tree_entry *te, const char *parent_path,
4881 struct got_pathlist_head *commitable_paths,
4882 got_worktree_status_cb status_cb, void *status_arg,
4883 struct got_repository *repo)
4885 const struct got_error *err = NULL;
4886 struct got_tree_object *subtree;
4887 char *subpath;
4889 if (asprintf(&subpath, "%s%s%s", parent_path,
4890 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4891 return got_error_from_errno("asprintf");
4893 err = got_object_open_as_tree(&subtree, repo, &te->id);
4894 if (err)
4895 return err;
4897 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4898 commitable_paths, status_cb, status_arg, repo);
4899 got_object_tree_close(subtree);
4900 free(subpath);
4901 return err;
4904 static const struct got_error *
4905 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4907 const struct got_error *err = NULL;
4908 char *ct_parent_path = NULL;
4910 *match = 0;
4912 if (strchr(ct->in_repo_path, '/') == NULL) {
4913 *match = got_path_is_root_dir(path);
4914 return NULL;
4917 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4918 if (err)
4919 return err;
4920 *match = (strcmp(path, ct_parent_path) == 0);
4921 free(ct_parent_path);
4922 return err;
4925 static mode_t
4926 get_ct_file_mode(struct got_commitable *ct)
4928 if (S_ISLNK(ct->mode))
4929 return S_IFLNK;
4931 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4934 static const struct got_error *
4935 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4936 struct got_tree_entry *te, struct got_commitable *ct)
4938 const struct got_error *err = NULL;
4940 *new_te = NULL;
4942 err = got_object_tree_entry_dup(new_te, te);
4943 if (err)
4944 goto done;
4946 (*new_te)->mode = get_ct_file_mode(ct);
4948 if (ct->staged_status == GOT_STATUS_MODIFY)
4949 memcpy(&(*new_te)->id, ct->staged_blob_id,
4950 sizeof((*new_te)->id));
4951 else
4952 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4953 done:
4954 if (err && *new_te) {
4955 free(*new_te);
4956 *new_te = NULL;
4958 return err;
4961 static const struct got_error *
4962 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4963 struct got_commitable *ct)
4965 const struct got_error *err = NULL;
4966 char *ct_name = NULL;
4968 *new_te = NULL;
4970 *new_te = calloc(1, sizeof(**new_te));
4971 if (*new_te == NULL)
4972 return got_error_from_errno("calloc");
4974 err = got_path_basename(&ct_name, ct->path);
4975 if (err)
4976 goto done;
4977 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4978 sizeof((*new_te)->name)) {
4979 err = got_error(GOT_ERR_NO_SPACE);
4980 goto done;
4983 (*new_te)->mode = get_ct_file_mode(ct);
4985 if (ct->staged_status == GOT_STATUS_ADD)
4986 memcpy(&(*new_te)->id, ct->staged_blob_id,
4987 sizeof((*new_te)->id));
4988 else
4989 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4990 done:
4991 free(ct_name);
4992 if (err && *new_te) {
4993 free(*new_te);
4994 *new_te = NULL;
4996 return err;
4999 static const struct got_error *
5000 insert_tree_entry(struct got_tree_entry *new_te,
5001 struct got_pathlist_head *paths)
5003 const struct got_error *err = NULL;
5004 struct got_pathlist_entry *new_pe;
5006 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5007 if (err)
5008 return err;
5009 if (new_pe == NULL)
5010 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5011 return NULL;
5014 static const struct got_error *
5015 report_ct_status(struct got_commitable *ct,
5016 got_worktree_status_cb status_cb, void *status_arg)
5018 const char *ct_path = ct->path;
5019 unsigned char status;
5021 while (ct_path[0] == '/')
5022 ct_path++;
5024 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5025 status = ct->staged_status;
5026 else
5027 status = ct->status;
5029 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5030 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5033 static const struct got_error *
5034 match_modified_subtree(int *modified, struct got_tree_entry *te,
5035 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5037 const struct got_error *err = NULL;
5038 struct got_pathlist_entry *pe;
5039 char *te_path;
5041 *modified = 0;
5043 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5044 got_path_is_root_dir(base_tree_path) ? "" : "/",
5045 te->name) == -1)
5046 return got_error_from_errno("asprintf");
5048 TAILQ_FOREACH(pe, commitable_paths, entry) {
5049 struct got_commitable *ct = pe->data;
5050 *modified = got_path_is_child(ct->in_repo_path, te_path,
5051 strlen(te_path));
5052 if (*modified)
5053 break;
5056 free(te_path);
5057 return err;
5060 static const struct got_error *
5061 match_deleted_or_modified_ct(struct got_commitable **ctp,
5062 struct got_tree_entry *te, const char *base_tree_path,
5063 struct got_pathlist_head *commitable_paths)
5065 const struct got_error *err = NULL;
5066 struct got_pathlist_entry *pe;
5068 *ctp = NULL;
5070 TAILQ_FOREACH(pe, commitable_paths, entry) {
5071 struct got_commitable *ct = pe->data;
5072 char *ct_name = NULL;
5073 int path_matches;
5075 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5076 if (ct->status != GOT_STATUS_MODIFY &&
5077 ct->status != GOT_STATUS_MODE_CHANGE &&
5078 ct->status != GOT_STATUS_DELETE)
5079 continue;
5080 } else {
5081 if (ct->staged_status != GOT_STATUS_MODIFY &&
5082 ct->staged_status != GOT_STATUS_DELETE)
5083 continue;
5086 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5087 continue;
5089 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5090 if (err)
5091 return err;
5092 if (!path_matches)
5093 continue;
5095 err = got_path_basename(&ct_name, pe->path);
5096 if (err)
5097 return err;
5099 if (strcmp(te->name, ct_name) != 0) {
5100 free(ct_name);
5101 continue;
5103 free(ct_name);
5105 *ctp = ct;
5106 break;
5109 return err;
5112 static const struct got_error *
5113 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5114 const char *child_path, const char *path_base_tree,
5115 struct got_pathlist_head *commitable_paths,
5116 got_worktree_status_cb status_cb, void *status_arg,
5117 struct got_repository *repo)
5119 const struct got_error *err = NULL;
5120 struct got_tree_entry *new_te;
5121 char *subtree_path;
5122 struct got_object_id *id = NULL;
5123 int nentries;
5125 *new_tep = NULL;
5127 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5128 got_path_is_root_dir(path_base_tree) ? "" : "/",
5129 child_path) == -1)
5130 return got_error_from_errno("asprintf");
5132 new_te = calloc(1, sizeof(*new_te));
5133 if (new_te == NULL)
5134 return got_error_from_errno("calloc");
5135 new_te->mode = S_IFDIR;
5137 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5138 sizeof(new_te->name)) {
5139 err = got_error(GOT_ERR_NO_SPACE);
5140 goto done;
5142 err = write_tree(&id, &nentries, NULL, subtree_path,
5143 commitable_paths, status_cb, status_arg, repo);
5144 if (err) {
5145 free(new_te);
5146 goto done;
5148 memcpy(&new_te->id, id, sizeof(new_te->id));
5149 done:
5150 free(id);
5151 free(subtree_path);
5152 if (err == NULL)
5153 *new_tep = new_te;
5154 return err;
5157 static const struct got_error *
5158 write_tree(struct got_object_id **new_tree_id, int *nentries,
5159 struct got_tree_object *base_tree, const char *path_base_tree,
5160 struct got_pathlist_head *commitable_paths,
5161 got_worktree_status_cb status_cb, void *status_arg,
5162 struct got_repository *repo)
5164 const struct got_error *err = NULL;
5165 struct got_pathlist_head paths;
5166 struct got_tree_entry *te, *new_te = NULL;
5167 struct got_pathlist_entry *pe;
5169 TAILQ_INIT(&paths);
5170 *nentries = 0;
5172 /* Insert, and recurse into, newly added entries first. */
5173 TAILQ_FOREACH(pe, commitable_paths, entry) {
5174 struct got_commitable *ct = pe->data;
5175 char *child_path = NULL, *slash;
5177 if ((ct->status != GOT_STATUS_ADD &&
5178 ct->staged_status != GOT_STATUS_ADD) ||
5179 (ct->flags & GOT_COMMITABLE_ADDED))
5180 continue;
5182 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5183 strlen(path_base_tree)))
5184 continue;
5186 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5187 ct->in_repo_path);
5188 if (err)
5189 goto done;
5191 slash = strchr(child_path, '/');
5192 if (slash == NULL) {
5193 err = alloc_added_blob_tree_entry(&new_te, ct);
5194 if (err)
5195 goto done;
5196 err = report_ct_status(ct, status_cb, status_arg);
5197 if (err)
5198 goto done;
5199 ct->flags |= GOT_COMMITABLE_ADDED;
5200 err = insert_tree_entry(new_te, &paths);
5201 if (err)
5202 goto done;
5203 (*nentries)++;
5204 } else {
5205 *slash = '\0'; /* trim trailing path components */
5206 if (base_tree == NULL ||
5207 got_object_tree_find_entry(base_tree, child_path)
5208 == NULL) {
5209 err = make_subtree_for_added_blob(&new_te,
5210 child_path, path_base_tree,
5211 commitable_paths, status_cb, status_arg,
5212 repo);
5213 if (err)
5214 goto done;
5215 err = insert_tree_entry(new_te, &paths);
5216 if (err)
5217 goto done;
5218 (*nentries)++;
5223 if (base_tree) {
5224 int i, nbase_entries;
5225 /* Handle modified and deleted entries. */
5226 nbase_entries = got_object_tree_get_nentries(base_tree);
5227 for (i = 0; i < nbase_entries; i++) {
5228 struct got_commitable *ct = NULL;
5230 te = got_object_tree_get_entry(base_tree, i);
5231 if (got_object_tree_entry_is_submodule(te)) {
5232 /* Entry is a submodule; just copy it. */
5233 err = got_object_tree_entry_dup(&new_te, te);
5234 if (err)
5235 goto done;
5236 err = insert_tree_entry(new_te, &paths);
5237 if (err)
5238 goto done;
5239 (*nentries)++;
5240 continue;
5243 if (S_ISDIR(te->mode)) {
5244 int modified;
5245 err = got_object_tree_entry_dup(&new_te, te);
5246 if (err)
5247 goto done;
5248 err = match_modified_subtree(&modified, te,
5249 path_base_tree, commitable_paths);
5250 if (err)
5251 goto done;
5252 /* Avoid recursion into unmodified subtrees. */
5253 if (modified) {
5254 struct got_object_id *new_id;
5255 int nsubentries;
5256 err = write_subtree(&new_id,
5257 &nsubentries, te,
5258 path_base_tree, commitable_paths,
5259 status_cb, status_arg, repo);
5260 if (err)
5261 goto done;
5262 if (nsubentries == 0) {
5263 /* All entries were deleted. */
5264 free(new_id);
5265 continue;
5267 memcpy(&new_te->id, new_id,
5268 sizeof(new_te->id));
5269 free(new_id);
5271 err = insert_tree_entry(new_te, &paths);
5272 if (err)
5273 goto done;
5274 (*nentries)++;
5275 continue;
5278 err = match_deleted_or_modified_ct(&ct, te,
5279 path_base_tree, commitable_paths);
5280 if (err)
5281 goto done;
5282 if (ct) {
5283 /* NB: Deleted entries get dropped here. */
5284 if (ct->status == GOT_STATUS_MODIFY ||
5285 ct->status == GOT_STATUS_MODE_CHANGE ||
5286 ct->staged_status == GOT_STATUS_MODIFY) {
5287 err = alloc_modified_blob_tree_entry(
5288 &new_te, te, ct);
5289 if (err)
5290 goto done;
5291 err = insert_tree_entry(new_te, &paths);
5292 if (err)
5293 goto done;
5294 (*nentries)++;
5296 err = report_ct_status(ct, status_cb,
5297 status_arg);
5298 if (err)
5299 goto done;
5300 } else {
5301 /* Entry is unchanged; just copy it. */
5302 err = got_object_tree_entry_dup(&new_te, te);
5303 if (err)
5304 goto done;
5305 err = insert_tree_entry(new_te, &paths);
5306 if (err)
5307 goto done;
5308 (*nentries)++;
5313 /* Write new list of entries; deleted entries have been dropped. */
5314 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5315 done:
5316 got_pathlist_free(&paths);
5317 return err;
5320 static const struct got_error *
5321 update_fileindex_after_commit(struct got_worktree *worktree,
5322 struct got_pathlist_head *commitable_paths,
5323 struct got_object_id *new_base_commit_id,
5324 struct got_fileindex *fileindex, int have_staged_files)
5326 const struct got_error *err = NULL;
5327 struct got_pathlist_entry *pe;
5328 char *relpath = NULL;
5330 TAILQ_FOREACH(pe, commitable_paths, entry) {
5331 struct got_fileindex_entry *ie;
5332 struct got_commitable *ct = pe->data;
5334 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5336 err = got_path_skip_common_ancestor(&relpath,
5337 worktree->root_path, ct->ondisk_path);
5338 if (err)
5339 goto done;
5341 if (ie) {
5342 if (ct->status == GOT_STATUS_DELETE ||
5343 ct->staged_status == GOT_STATUS_DELETE) {
5344 got_fileindex_entry_remove(fileindex, ie);
5345 } else if (ct->staged_status == GOT_STATUS_ADD ||
5346 ct->staged_status == GOT_STATUS_MODIFY) {
5347 got_fileindex_entry_stage_set(ie,
5348 GOT_FILEIDX_STAGE_NONE);
5349 got_fileindex_entry_staged_filetype_set(ie, 0);
5351 err = got_fileindex_entry_update(ie,
5352 worktree->root_fd, relpath,
5353 ct->staged_blob_id->sha1,
5354 new_base_commit_id->sha1,
5355 !have_staged_files);
5356 } else
5357 err = got_fileindex_entry_update(ie,
5358 worktree->root_fd, relpath,
5359 ct->blob_id->sha1,
5360 new_base_commit_id->sha1,
5361 !have_staged_files);
5362 } else {
5363 err = got_fileindex_entry_alloc(&ie, pe->path);
5364 if (err)
5365 goto done;
5366 err = got_fileindex_entry_update(ie,
5367 worktree->root_fd, relpath, ct->blob_id->sha1,
5368 new_base_commit_id->sha1, 1);
5369 if (err) {
5370 got_fileindex_entry_free(ie);
5371 goto done;
5373 err = got_fileindex_entry_add(fileindex, ie);
5374 if (err) {
5375 got_fileindex_entry_free(ie);
5376 goto done;
5379 free(relpath);
5380 relpath = NULL;
5382 done:
5383 free(relpath);
5384 return err;
5388 static const struct got_error *
5389 check_out_of_date(const char *in_repo_path, unsigned char status,
5390 unsigned char staged_status, struct got_object_id *base_blob_id,
5391 struct got_object_id *base_commit_id,
5392 struct got_object_id *head_commit_id, struct got_repository *repo,
5393 int ood_errcode)
5395 const struct got_error *err = NULL;
5396 struct got_object_id *id = NULL;
5398 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5399 /* Trivial case: base commit == head commit */
5400 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5401 return NULL;
5403 * Ensure file content which local changes were based
5404 * on matches file content in the branch head.
5406 err = got_object_id_by_path(&id, repo, head_commit_id,
5407 in_repo_path);
5408 if (err) {
5409 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5410 err = got_error(ood_errcode);
5411 goto done;
5412 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5413 err = got_error(ood_errcode);
5414 } else {
5415 /* Require that added files don't exist in the branch head. */
5416 err = got_object_id_by_path(&id, repo, head_commit_id,
5417 in_repo_path);
5418 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5419 goto done;
5420 err = id ? got_error(ood_errcode) : NULL;
5422 done:
5423 free(id);
5424 return err;
5427 const struct got_error *
5428 commit_worktree(struct got_object_id **new_commit_id,
5429 struct got_pathlist_head *commitable_paths,
5430 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5431 const char *author, const char *committer,
5432 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5433 got_worktree_status_cb status_cb, void *status_arg,
5434 struct got_repository *repo)
5436 const struct got_error *err = NULL, *unlockerr = NULL;
5437 struct got_pathlist_entry *pe;
5438 const char *head_ref_name = NULL;
5439 struct got_commit_object *head_commit = NULL;
5440 struct got_reference *head_ref2 = NULL;
5441 struct got_object_id *head_commit_id2 = NULL;
5442 struct got_tree_object *head_tree = NULL;
5443 struct got_object_id *new_tree_id = NULL;
5444 int nentries;
5445 struct got_object_id_queue parent_ids;
5446 struct got_object_qid *pid = NULL;
5447 char *logmsg = NULL;
5449 *new_commit_id = NULL;
5451 SIMPLEQ_INIT(&parent_ids);
5453 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5454 if (err)
5455 goto done;
5457 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5458 if (err)
5459 goto done;
5461 if (commit_msg_cb != NULL) {
5462 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5463 if (err)
5464 goto done;
5467 if (logmsg == NULL || strlen(logmsg) == 0) {
5468 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5469 goto done;
5472 /* Create blobs from added and modified files and record their IDs. */
5473 TAILQ_FOREACH(pe, commitable_paths, entry) {
5474 struct got_commitable *ct = pe->data;
5475 char *ondisk_path;
5477 /* Blobs for staged files already exist. */
5478 if (ct->staged_status == GOT_STATUS_ADD ||
5479 ct->staged_status == GOT_STATUS_MODIFY)
5480 continue;
5482 if (ct->status != GOT_STATUS_ADD &&
5483 ct->status != GOT_STATUS_MODIFY &&
5484 ct->status != GOT_STATUS_MODE_CHANGE)
5485 continue;
5487 if (asprintf(&ondisk_path, "%s/%s",
5488 worktree->root_path, pe->path) == -1) {
5489 err = got_error_from_errno("asprintf");
5490 goto done;
5492 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5493 free(ondisk_path);
5494 if (err)
5495 goto done;
5498 /* Recursively write new tree objects. */
5499 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5500 commitable_paths, status_cb, status_arg, repo);
5501 if (err)
5502 goto done;
5504 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5505 if (err)
5506 goto done;
5507 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5508 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5509 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5510 got_object_qid_free(pid);
5511 if (logmsg != NULL)
5512 free(logmsg);
5513 if (err)
5514 goto done;
5516 /* Check if a concurrent commit to our branch has occurred. */
5517 head_ref_name = got_worktree_get_head_ref_name(worktree);
5518 if (head_ref_name == NULL) {
5519 err = got_error_from_errno("got_worktree_get_head_ref_name");
5520 goto done;
5522 /* Lock the reference here to prevent concurrent modification. */
5523 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5524 if (err)
5525 goto done;
5526 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5527 if (err)
5528 goto done;
5529 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5530 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5531 goto done;
5533 /* Update branch head in repository. */
5534 err = got_ref_change_ref(head_ref2, *new_commit_id);
5535 if (err)
5536 goto done;
5537 err = got_ref_write(head_ref2, repo);
5538 if (err)
5539 goto done;
5541 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5542 if (err)
5543 goto done;
5545 err = ref_base_commit(worktree, repo);
5546 if (err)
5547 goto done;
5548 done:
5549 if (head_tree)
5550 got_object_tree_close(head_tree);
5551 if (head_commit)
5552 got_object_commit_close(head_commit);
5553 free(head_commit_id2);
5554 if (head_ref2) {
5555 unlockerr = got_ref_unlock(head_ref2);
5556 if (unlockerr && err == NULL)
5557 err = unlockerr;
5558 got_ref_close(head_ref2);
5560 return err;
5563 static const struct got_error *
5564 check_path_is_commitable(const char *path,
5565 struct got_pathlist_head *commitable_paths)
5567 struct got_pathlist_entry *cpe = NULL;
5568 size_t path_len = strlen(path);
5570 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5571 struct got_commitable *ct = cpe->data;
5572 const char *ct_path = ct->path;
5574 while (ct_path[0] == '/')
5575 ct_path++;
5577 if (strcmp(path, ct_path) == 0 ||
5578 got_path_is_child(ct_path, path, path_len))
5579 break;
5582 if (cpe == NULL)
5583 return got_error_path(path, GOT_ERR_BAD_PATH);
5585 return NULL;
5588 static const struct got_error *
5589 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5591 int *have_staged_files = arg;
5593 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5594 *have_staged_files = 1;
5595 return got_error(GOT_ERR_CANCELLED);
5598 return NULL;
5601 static const struct got_error *
5602 check_non_staged_files(struct got_fileindex *fileindex,
5603 struct got_pathlist_head *paths)
5605 struct got_pathlist_entry *pe;
5606 struct got_fileindex_entry *ie;
5608 TAILQ_FOREACH(pe, paths, entry) {
5609 if (pe->path[0] == '\0')
5610 continue;
5611 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5612 if (ie == NULL)
5613 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5614 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5615 return got_error_path(pe->path,
5616 GOT_ERR_FILE_NOT_STAGED);
5619 return NULL;
5622 const struct got_error *
5623 got_worktree_commit(struct got_object_id **new_commit_id,
5624 struct got_worktree *worktree, struct got_pathlist_head *paths,
5625 const char *author, const char *committer, int allow_bad_symlinks,
5626 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5627 got_worktree_status_cb status_cb, void *status_arg,
5628 struct got_repository *repo)
5630 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5631 struct got_fileindex *fileindex = NULL;
5632 char *fileindex_path = NULL;
5633 struct got_pathlist_head commitable_paths;
5634 struct collect_commitables_arg cc_arg;
5635 struct got_pathlist_entry *pe;
5636 struct got_reference *head_ref = NULL;
5637 struct got_object_id *head_commit_id = NULL;
5638 int have_staged_files = 0;
5640 *new_commit_id = NULL;
5642 TAILQ_INIT(&commitable_paths);
5644 err = lock_worktree(worktree, LOCK_EX);
5645 if (err)
5646 goto done;
5648 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5649 if (err)
5650 goto done;
5652 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5653 if (err)
5654 goto done;
5656 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5657 if (err)
5658 goto done;
5660 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5661 &have_staged_files);
5662 if (err && err->code != GOT_ERR_CANCELLED)
5663 goto done;
5664 if (have_staged_files) {
5665 err = check_non_staged_files(fileindex, paths);
5666 if (err)
5667 goto done;
5670 cc_arg.commitable_paths = &commitable_paths;
5671 cc_arg.worktree = worktree;
5672 cc_arg.fileindex = fileindex;
5673 cc_arg.repo = repo;
5674 cc_arg.have_staged_files = have_staged_files;
5675 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5676 TAILQ_FOREACH(pe, paths, entry) {
5677 err = worktree_status(worktree, pe->path, fileindex, repo,
5678 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5679 if (err)
5680 goto done;
5683 if (TAILQ_EMPTY(&commitable_paths)) {
5684 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5685 goto done;
5688 TAILQ_FOREACH(pe, paths, entry) {
5689 err = check_path_is_commitable(pe->path, &commitable_paths);
5690 if (err)
5691 goto done;
5694 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5695 struct got_commitable *ct = pe->data;
5696 const char *ct_path = ct->in_repo_path;
5698 while (ct_path[0] == '/')
5699 ct_path++;
5700 err = check_out_of_date(ct_path, ct->status,
5701 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5702 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5703 if (err)
5704 goto done;
5708 err = commit_worktree(new_commit_id, &commitable_paths,
5709 head_commit_id, worktree, author, committer,
5710 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5711 if (err)
5712 goto done;
5714 err = update_fileindex_after_commit(worktree, &commitable_paths,
5715 *new_commit_id, fileindex, have_staged_files);
5716 sync_err = sync_fileindex(fileindex, fileindex_path);
5717 if (sync_err && err == NULL)
5718 err = sync_err;
5719 done:
5720 if (fileindex)
5721 got_fileindex_free(fileindex);
5722 free(fileindex_path);
5723 unlockerr = lock_worktree(worktree, LOCK_SH);
5724 if (unlockerr && err == NULL)
5725 err = unlockerr;
5726 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5727 struct got_commitable *ct = pe->data;
5728 free_commitable(ct);
5730 got_pathlist_free(&commitable_paths);
5731 return err;
5734 const char *
5735 got_commitable_get_path(struct got_commitable *ct)
5737 return ct->path;
5740 unsigned int
5741 got_commitable_get_status(struct got_commitable *ct)
5743 return ct->status;
5746 struct check_rebase_ok_arg {
5747 struct got_worktree *worktree;
5748 struct got_repository *repo;
5751 static const struct got_error *
5752 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5754 const struct got_error *err = NULL;
5755 struct check_rebase_ok_arg *a = arg;
5756 unsigned char status;
5757 struct stat sb;
5758 char *ondisk_path;
5760 /* Reject rebase of a work tree with mixed base commits. */
5761 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5762 SHA1_DIGEST_LENGTH))
5763 return got_error(GOT_ERR_MIXED_COMMITS);
5765 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5766 == -1)
5767 return got_error_from_errno("asprintf");
5769 /* Reject rebase of a work tree with modified or staged files. */
5770 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5771 free(ondisk_path);
5772 if (err)
5773 return err;
5775 if (status != GOT_STATUS_NO_CHANGE)
5776 return got_error(GOT_ERR_MODIFIED);
5777 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5778 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5780 return NULL;
5783 const struct got_error *
5784 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5785 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5786 struct got_worktree *worktree, struct got_reference *branch,
5787 struct got_repository *repo)
5789 const struct got_error *err = NULL;
5790 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5791 char *branch_ref_name = NULL;
5792 char *fileindex_path = NULL;
5793 struct check_rebase_ok_arg ok_arg;
5794 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5795 struct got_object_id *wt_branch_tip = NULL;
5797 *new_base_branch_ref = NULL;
5798 *tmp_branch = NULL;
5799 *fileindex = NULL;
5801 err = lock_worktree(worktree, LOCK_EX);
5802 if (err)
5803 return err;
5805 err = open_fileindex(fileindex, &fileindex_path, worktree);
5806 if (err)
5807 goto done;
5809 ok_arg.worktree = worktree;
5810 ok_arg.repo = repo;
5811 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5812 &ok_arg);
5813 if (err)
5814 goto done;
5816 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5817 if (err)
5818 goto done;
5820 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5821 if (err)
5822 goto done;
5824 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5825 if (err)
5826 goto done;
5828 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5829 0);
5830 if (err)
5831 goto done;
5833 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5834 if (err)
5835 goto done;
5836 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5837 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5838 goto done;
5841 err = got_ref_alloc_symref(new_base_branch_ref,
5842 new_base_branch_ref_name, wt_branch);
5843 if (err)
5844 goto done;
5845 err = got_ref_write(*new_base_branch_ref, repo);
5846 if (err)
5847 goto done;
5849 /* TODO Lock original branch's ref while rebasing? */
5851 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5852 if (err)
5853 goto done;
5855 err = got_ref_write(branch_ref, repo);
5856 if (err)
5857 goto done;
5859 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5860 worktree->base_commit_id);
5861 if (err)
5862 goto done;
5863 err = got_ref_write(*tmp_branch, repo);
5864 if (err)
5865 goto done;
5867 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5868 if (err)
5869 goto done;
5870 done:
5871 free(fileindex_path);
5872 free(tmp_branch_name);
5873 free(new_base_branch_ref_name);
5874 free(branch_ref_name);
5875 if (branch_ref)
5876 got_ref_close(branch_ref);
5877 if (wt_branch)
5878 got_ref_close(wt_branch);
5879 free(wt_branch_tip);
5880 if (err) {
5881 if (*new_base_branch_ref) {
5882 got_ref_close(*new_base_branch_ref);
5883 *new_base_branch_ref = NULL;
5885 if (*tmp_branch) {
5886 got_ref_close(*tmp_branch);
5887 *tmp_branch = NULL;
5889 if (*fileindex) {
5890 got_fileindex_free(*fileindex);
5891 *fileindex = NULL;
5893 lock_worktree(worktree, LOCK_SH);
5895 return err;
5898 const struct got_error *
5899 got_worktree_rebase_continue(struct got_object_id **commit_id,
5900 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5901 struct got_reference **branch, struct got_fileindex **fileindex,
5902 struct got_worktree *worktree, struct got_repository *repo)
5904 const struct got_error *err;
5905 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5906 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5907 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5908 char *fileindex_path = NULL;
5909 int have_staged_files = 0;
5911 *commit_id = NULL;
5912 *new_base_branch = NULL;
5913 *tmp_branch = NULL;
5914 *branch = NULL;
5915 *fileindex = NULL;
5917 err = lock_worktree(worktree, LOCK_EX);
5918 if (err)
5919 return err;
5921 err = open_fileindex(fileindex, &fileindex_path, worktree);
5922 if (err)
5923 goto done;
5925 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5926 &have_staged_files);
5927 if (err && err->code != GOT_ERR_CANCELLED)
5928 goto done;
5929 if (have_staged_files) {
5930 err = got_error(GOT_ERR_STAGED_PATHS);
5931 goto done;
5934 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5935 if (err)
5936 goto done;
5938 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5939 if (err)
5940 goto done;
5942 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5943 if (err)
5944 goto done;
5946 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5947 if (err)
5948 goto done;
5950 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5951 if (err)
5952 goto done;
5954 err = got_ref_open(branch, repo,
5955 got_ref_get_symref_target(branch_ref), 0);
5956 if (err)
5957 goto done;
5959 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5960 if (err)
5961 goto done;
5963 err = got_ref_resolve(commit_id, repo, commit_ref);
5964 if (err)
5965 goto done;
5967 err = got_ref_open(new_base_branch, repo,
5968 new_base_branch_ref_name, 0);
5969 if (err)
5970 goto done;
5972 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5973 if (err)
5974 goto done;
5975 done:
5976 free(commit_ref_name);
5977 free(branch_ref_name);
5978 free(fileindex_path);
5979 if (commit_ref)
5980 got_ref_close(commit_ref);
5981 if (branch_ref)
5982 got_ref_close(branch_ref);
5983 if (err) {
5984 free(*commit_id);
5985 *commit_id = NULL;
5986 if (*tmp_branch) {
5987 got_ref_close(*tmp_branch);
5988 *tmp_branch = NULL;
5990 if (*new_base_branch) {
5991 got_ref_close(*new_base_branch);
5992 *new_base_branch = NULL;
5994 if (*branch) {
5995 got_ref_close(*branch);
5996 *branch = NULL;
5998 if (*fileindex) {
5999 got_fileindex_free(*fileindex);
6000 *fileindex = NULL;
6002 lock_worktree(worktree, LOCK_SH);
6004 return err;
6007 const struct got_error *
6008 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6010 const struct got_error *err;
6011 char *tmp_branch_name = NULL;
6013 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6014 if (err)
6015 return err;
6017 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6018 free(tmp_branch_name);
6019 return NULL;
6022 static const struct got_error *
6023 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6024 char **logmsg, void *arg)
6026 *logmsg = arg;
6027 return NULL;
6030 static const struct got_error *
6031 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6032 const char *path, struct got_object_id *blob_id,
6033 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6034 int dirfd, const char *de_name)
6036 return NULL;
6039 struct collect_merged_paths_arg {
6040 got_worktree_checkout_cb progress_cb;
6041 void *progress_arg;
6042 struct got_pathlist_head *merged_paths;
6045 static const struct got_error *
6046 collect_merged_paths(void *arg, unsigned char status, const char *path)
6048 const struct got_error *err;
6049 struct collect_merged_paths_arg *a = arg;
6050 char *p;
6051 struct got_pathlist_entry *new;
6053 err = (*a->progress_cb)(a->progress_arg, status, path);
6054 if (err)
6055 return err;
6057 if (status != GOT_STATUS_MERGE &&
6058 status != GOT_STATUS_ADD &&
6059 status != GOT_STATUS_DELETE &&
6060 status != GOT_STATUS_CONFLICT)
6061 return NULL;
6063 p = strdup(path);
6064 if (p == NULL)
6065 return got_error_from_errno("strdup");
6067 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6068 if (err || new == NULL)
6069 free(p);
6070 return err;
6073 void
6074 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6076 struct got_pathlist_entry *pe;
6078 TAILQ_FOREACH(pe, merged_paths, entry)
6079 free((char *)pe->path);
6081 got_pathlist_free(merged_paths);
6084 static const struct got_error *
6085 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6086 int is_rebase, struct got_repository *repo)
6088 const struct got_error *err;
6089 struct got_reference *commit_ref = NULL;
6091 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6092 if (err) {
6093 if (err->code != GOT_ERR_NOT_REF)
6094 goto done;
6095 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6096 if (err)
6097 goto done;
6098 err = got_ref_write(commit_ref, repo);
6099 if (err)
6100 goto done;
6101 } else if (is_rebase) {
6102 struct got_object_id *stored_id;
6103 int cmp;
6105 err = got_ref_resolve(&stored_id, repo, commit_ref);
6106 if (err)
6107 goto done;
6108 cmp = got_object_id_cmp(commit_id, stored_id);
6109 free(stored_id);
6110 if (cmp != 0) {
6111 err = got_error(GOT_ERR_REBASE_COMMITID);
6112 goto done;
6115 done:
6116 if (commit_ref)
6117 got_ref_close(commit_ref);
6118 return err;
6121 static const struct got_error *
6122 rebase_merge_files(struct got_pathlist_head *merged_paths,
6123 const char *commit_ref_name, struct got_worktree *worktree,
6124 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6125 struct got_object_id *commit_id, struct got_repository *repo,
6126 got_worktree_checkout_cb progress_cb, void *progress_arg,
6127 got_cancel_cb cancel_cb, void *cancel_arg)
6129 const struct got_error *err;
6130 struct got_reference *commit_ref = NULL;
6131 struct collect_merged_paths_arg cmp_arg;
6132 char *fileindex_path;
6134 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6136 err = get_fileindex_path(&fileindex_path, worktree);
6137 if (err)
6138 return err;
6140 cmp_arg.progress_cb = progress_cb;
6141 cmp_arg.progress_arg = progress_arg;
6142 cmp_arg.merged_paths = merged_paths;
6143 err = merge_files(worktree, fileindex, fileindex_path,
6144 parent_commit_id, commit_id, repo, collect_merged_paths,
6145 &cmp_arg, cancel_cb, cancel_arg);
6146 if (commit_ref)
6147 got_ref_close(commit_ref);
6148 return err;
6151 const struct got_error *
6152 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6153 struct got_worktree *worktree, struct got_fileindex *fileindex,
6154 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6155 struct got_repository *repo,
6156 got_worktree_checkout_cb progress_cb, void *progress_arg,
6157 got_cancel_cb cancel_cb, void *cancel_arg)
6159 const struct got_error *err;
6160 char *commit_ref_name;
6162 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6163 if (err)
6164 return err;
6166 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6167 if (err)
6168 goto done;
6170 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6171 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6172 progress_arg, cancel_cb, cancel_arg);
6173 done:
6174 free(commit_ref_name);
6175 return err;
6178 const struct got_error *
6179 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6180 struct got_worktree *worktree, struct got_fileindex *fileindex,
6181 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6182 struct got_repository *repo,
6183 got_worktree_checkout_cb progress_cb, void *progress_arg,
6184 got_cancel_cb cancel_cb, void *cancel_arg)
6186 const struct got_error *err;
6187 char *commit_ref_name;
6189 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6190 if (err)
6191 return err;
6193 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6194 if (err)
6195 goto done;
6197 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6198 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6199 progress_arg, cancel_cb, cancel_arg);
6200 done:
6201 free(commit_ref_name);
6202 return err;
6205 static const struct got_error *
6206 rebase_commit(struct got_object_id **new_commit_id,
6207 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6208 struct got_worktree *worktree, struct got_fileindex *fileindex,
6209 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6210 const char *new_logmsg, struct got_repository *repo)
6212 const struct got_error *err, *sync_err;
6213 struct got_pathlist_head commitable_paths;
6214 struct collect_commitables_arg cc_arg;
6215 char *fileindex_path = NULL;
6216 struct got_reference *head_ref = NULL;
6217 struct got_object_id *head_commit_id = NULL;
6218 char *logmsg = NULL;
6220 TAILQ_INIT(&commitable_paths);
6221 *new_commit_id = NULL;
6223 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6225 err = get_fileindex_path(&fileindex_path, worktree);
6226 if (err)
6227 return err;
6229 cc_arg.commitable_paths = &commitable_paths;
6230 cc_arg.worktree = worktree;
6231 cc_arg.repo = repo;
6232 cc_arg.have_staged_files = 0;
6234 * If possible get the status of individual files directly to
6235 * avoid crawling the entire work tree once per rebased commit.
6236 * TODO: Ideally, merged_paths would contain a list of commitables
6237 * we could use so we could skip worktree_status() entirely.
6239 if (merged_paths) {
6240 struct got_pathlist_entry *pe;
6241 TAILQ_FOREACH(pe, merged_paths, entry) {
6242 err = worktree_status(worktree, pe->path, fileindex,
6243 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6244 0);
6245 if (err)
6246 goto done;
6248 } else {
6249 err = worktree_status(worktree, "", fileindex, repo,
6250 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6251 if (err)
6252 goto done;
6255 if (TAILQ_EMPTY(&commitable_paths)) {
6256 /* No-op change; commit will be elided. */
6257 err = got_ref_delete(commit_ref, repo);
6258 if (err)
6259 goto done;
6260 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6261 goto done;
6264 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6265 if (err)
6266 goto done;
6268 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6269 if (err)
6270 goto done;
6272 if (new_logmsg) {
6273 logmsg = strdup(new_logmsg);
6274 if (logmsg == NULL) {
6275 err = got_error_from_errno("strdup");
6276 goto done;
6278 } else {
6279 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6280 if (err)
6281 goto done;
6284 /* NB: commit_worktree will call free(logmsg) */
6285 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6286 worktree, got_object_commit_get_author(orig_commit),
6287 got_object_commit_get_committer(orig_commit),
6288 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6289 if (err)
6290 goto done;
6292 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6293 if (err)
6294 goto done;
6296 err = got_ref_delete(commit_ref, repo);
6297 if (err)
6298 goto done;
6300 err = update_fileindex_after_commit(worktree, &commitable_paths,
6301 *new_commit_id, fileindex, 0);
6302 sync_err = sync_fileindex(fileindex, fileindex_path);
6303 if (sync_err && err == NULL)
6304 err = sync_err;
6305 done:
6306 free(fileindex_path);
6307 free(head_commit_id);
6308 if (head_ref)
6309 got_ref_close(head_ref);
6310 if (err) {
6311 free(*new_commit_id);
6312 *new_commit_id = NULL;
6314 return err;
6317 const struct got_error *
6318 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6319 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6320 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6321 struct got_commit_object *orig_commit,
6322 struct got_object_id *orig_commit_id, struct got_repository *repo)
6324 const struct got_error *err;
6325 char *commit_ref_name;
6326 struct got_reference *commit_ref = NULL;
6327 struct got_object_id *commit_id = NULL;
6329 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6330 if (err)
6331 return err;
6333 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6334 if (err)
6335 goto done;
6336 err = got_ref_resolve(&commit_id, repo, commit_ref);
6337 if (err)
6338 goto done;
6339 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6340 err = got_error(GOT_ERR_REBASE_COMMITID);
6341 goto done;
6344 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6345 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6346 done:
6347 if (commit_ref)
6348 got_ref_close(commit_ref);
6349 free(commit_ref_name);
6350 free(commit_id);
6351 return err;
6354 const struct got_error *
6355 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6356 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6357 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6358 struct got_commit_object *orig_commit,
6359 struct got_object_id *orig_commit_id, const char *new_logmsg,
6360 struct got_repository *repo)
6362 const struct got_error *err;
6363 char *commit_ref_name;
6364 struct got_reference *commit_ref = NULL;
6366 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6367 if (err)
6368 return err;
6370 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6371 if (err)
6372 goto done;
6374 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6375 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6376 done:
6377 if (commit_ref)
6378 got_ref_close(commit_ref);
6379 free(commit_ref_name);
6380 return err;
6383 const struct got_error *
6384 got_worktree_rebase_postpone(struct got_worktree *worktree,
6385 struct got_fileindex *fileindex)
6387 if (fileindex)
6388 got_fileindex_free(fileindex);
6389 return lock_worktree(worktree, LOCK_SH);
6392 static const struct got_error *
6393 delete_ref(const char *name, struct got_repository *repo)
6395 const struct got_error *err;
6396 struct got_reference *ref;
6398 err = got_ref_open(&ref, repo, name, 0);
6399 if (err) {
6400 if (err->code == GOT_ERR_NOT_REF)
6401 return NULL;
6402 return err;
6405 err = got_ref_delete(ref, repo);
6406 got_ref_close(ref);
6407 return err;
6410 static const struct got_error *
6411 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6413 const struct got_error *err;
6414 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6415 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6417 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6418 if (err)
6419 goto done;
6420 err = delete_ref(tmp_branch_name, repo);
6421 if (err)
6422 goto done;
6424 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6425 if (err)
6426 goto done;
6427 err = delete_ref(new_base_branch_ref_name, repo);
6428 if (err)
6429 goto done;
6431 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6432 if (err)
6433 goto done;
6434 err = delete_ref(branch_ref_name, repo);
6435 if (err)
6436 goto done;
6438 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6439 if (err)
6440 goto done;
6441 err = delete_ref(commit_ref_name, repo);
6442 if (err)
6443 goto done;
6445 done:
6446 free(tmp_branch_name);
6447 free(new_base_branch_ref_name);
6448 free(branch_ref_name);
6449 free(commit_ref_name);
6450 return err;
6453 const struct got_error *
6454 got_worktree_rebase_complete(struct got_worktree *worktree,
6455 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6456 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6457 struct got_repository *repo)
6459 const struct got_error *err, *unlockerr, *sync_err;
6460 struct got_object_id *new_head_commit_id = NULL;
6461 char *fileindex_path = NULL;
6463 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6464 if (err)
6465 return err;
6467 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6468 if (err)
6469 goto done;
6471 err = got_ref_write(rebased_branch, repo);
6472 if (err)
6473 goto done;
6475 err = got_worktree_set_head_ref(worktree, rebased_branch);
6476 if (err)
6477 goto done;
6479 err = delete_rebase_refs(worktree, repo);
6480 if (err)
6481 goto done;
6483 err = get_fileindex_path(&fileindex_path, worktree);
6484 if (err)
6485 goto done;
6486 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6487 sync_err = sync_fileindex(fileindex, fileindex_path);
6488 if (sync_err && err == NULL)
6489 err = sync_err;
6490 done:
6491 got_fileindex_free(fileindex);
6492 free(fileindex_path);
6493 free(new_head_commit_id);
6494 unlockerr = lock_worktree(worktree, LOCK_SH);
6495 if (unlockerr && err == NULL)
6496 err = unlockerr;
6497 return err;
6500 const struct got_error *
6501 got_worktree_rebase_abort(struct got_worktree *worktree,
6502 struct got_fileindex *fileindex, struct got_repository *repo,
6503 struct got_reference *new_base_branch,
6504 got_worktree_checkout_cb progress_cb, void *progress_arg)
6506 const struct got_error *err, *unlockerr, *sync_err;
6507 struct got_reference *resolved = NULL;
6508 struct got_object_id *commit_id = NULL;
6509 char *fileindex_path = NULL;
6510 struct revert_file_args rfa;
6511 struct got_object_id *tree_id = NULL;
6513 err = lock_worktree(worktree, LOCK_EX);
6514 if (err)
6515 return err;
6517 err = got_ref_open(&resolved, repo,
6518 got_ref_get_symref_target(new_base_branch), 0);
6519 if (err)
6520 goto done;
6522 err = got_worktree_set_head_ref(worktree, resolved);
6523 if (err)
6524 goto done;
6527 * XXX commits to the base branch could have happened while
6528 * we were busy rebasing; should we store the original commit ID
6529 * when rebase begins and read it back here?
6531 err = got_ref_resolve(&commit_id, repo, resolved);
6532 if (err)
6533 goto done;
6535 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6536 if (err)
6537 goto done;
6539 err = got_object_id_by_path(&tree_id, repo,
6540 worktree->base_commit_id, worktree->path_prefix);
6541 if (err)
6542 goto done;
6544 err = delete_rebase_refs(worktree, repo);
6545 if (err)
6546 goto done;
6548 err = get_fileindex_path(&fileindex_path, worktree);
6549 if (err)
6550 goto done;
6552 rfa.worktree = worktree;
6553 rfa.fileindex = fileindex;
6554 rfa.progress_cb = progress_cb;
6555 rfa.progress_arg = progress_arg;
6556 rfa.patch_cb = NULL;
6557 rfa.patch_arg = NULL;
6558 rfa.repo = repo;
6559 err = worktree_status(worktree, "", fileindex, repo,
6560 revert_file, &rfa, NULL, NULL, 0, 0);
6561 if (err)
6562 goto sync;
6564 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6565 repo, progress_cb, progress_arg, NULL, NULL);
6566 sync:
6567 sync_err = sync_fileindex(fileindex, fileindex_path);
6568 if (sync_err && err == NULL)
6569 err = sync_err;
6570 done:
6571 got_ref_close(resolved);
6572 free(tree_id);
6573 free(commit_id);
6574 if (fileindex)
6575 got_fileindex_free(fileindex);
6576 free(fileindex_path);
6578 unlockerr = lock_worktree(worktree, LOCK_SH);
6579 if (unlockerr && err == NULL)
6580 err = unlockerr;
6581 return err;
6584 const struct got_error *
6585 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6586 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6587 struct got_fileindex **fileindex, struct got_worktree *worktree,
6588 struct got_repository *repo)
6590 const struct got_error *err = NULL;
6591 char *tmp_branch_name = NULL;
6592 char *branch_ref_name = NULL;
6593 char *base_commit_ref_name = NULL;
6594 char *fileindex_path = NULL;
6595 struct check_rebase_ok_arg ok_arg;
6596 struct got_reference *wt_branch = NULL;
6597 struct got_reference *base_commit_ref = NULL;
6599 *tmp_branch = NULL;
6600 *branch_ref = NULL;
6601 *base_commit_id = NULL;
6602 *fileindex = NULL;
6604 err = lock_worktree(worktree, LOCK_EX);
6605 if (err)
6606 return err;
6608 err = open_fileindex(fileindex, &fileindex_path, worktree);
6609 if (err)
6610 goto done;
6612 ok_arg.worktree = worktree;
6613 ok_arg.repo = repo;
6614 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6615 &ok_arg);
6616 if (err)
6617 goto done;
6619 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6620 if (err)
6621 goto done;
6623 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6624 if (err)
6625 goto done;
6627 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6628 worktree);
6629 if (err)
6630 goto done;
6632 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6633 0);
6634 if (err)
6635 goto done;
6637 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6638 if (err)
6639 goto done;
6641 err = got_ref_write(*branch_ref, repo);
6642 if (err)
6643 goto done;
6645 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6646 worktree->base_commit_id);
6647 if (err)
6648 goto done;
6649 err = got_ref_write(base_commit_ref, repo);
6650 if (err)
6651 goto done;
6652 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6653 if (*base_commit_id == NULL) {
6654 err = got_error_from_errno("got_object_id_dup");
6655 goto done;
6658 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6659 worktree->base_commit_id);
6660 if (err)
6661 goto done;
6662 err = got_ref_write(*tmp_branch, repo);
6663 if (err)
6664 goto done;
6666 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6667 if (err)
6668 goto done;
6669 done:
6670 free(fileindex_path);
6671 free(tmp_branch_name);
6672 free(branch_ref_name);
6673 free(base_commit_ref_name);
6674 if (wt_branch)
6675 got_ref_close(wt_branch);
6676 if (err) {
6677 if (*branch_ref) {
6678 got_ref_close(*branch_ref);
6679 *branch_ref = NULL;
6681 if (*tmp_branch) {
6682 got_ref_close(*tmp_branch);
6683 *tmp_branch = NULL;
6685 free(*base_commit_id);
6686 if (*fileindex) {
6687 got_fileindex_free(*fileindex);
6688 *fileindex = NULL;
6690 lock_worktree(worktree, LOCK_SH);
6692 return err;
6695 const struct got_error *
6696 got_worktree_histedit_postpone(struct got_worktree *worktree,
6697 struct got_fileindex *fileindex)
6699 if (fileindex)
6700 got_fileindex_free(fileindex);
6701 return lock_worktree(worktree, LOCK_SH);
6704 const struct got_error *
6705 got_worktree_histedit_in_progress(int *in_progress,
6706 struct got_worktree *worktree)
6708 const struct got_error *err;
6709 char *tmp_branch_name = NULL;
6711 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6712 if (err)
6713 return err;
6715 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6716 free(tmp_branch_name);
6717 return NULL;
6720 const struct got_error *
6721 got_worktree_histedit_continue(struct got_object_id **commit_id,
6722 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6723 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6724 struct got_worktree *worktree, struct got_repository *repo)
6726 const struct got_error *err;
6727 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6728 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6729 struct got_reference *commit_ref = NULL;
6730 struct got_reference *base_commit_ref = NULL;
6731 char *fileindex_path = NULL;
6732 int have_staged_files = 0;
6734 *commit_id = NULL;
6735 *tmp_branch = NULL;
6736 *base_commit_id = NULL;
6737 *fileindex = NULL;
6739 err = lock_worktree(worktree, LOCK_EX);
6740 if (err)
6741 return err;
6743 err = open_fileindex(fileindex, &fileindex_path, worktree);
6744 if (err)
6745 goto done;
6747 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6748 &have_staged_files);
6749 if (err && err->code != GOT_ERR_CANCELLED)
6750 goto done;
6751 if (have_staged_files) {
6752 err = got_error(GOT_ERR_STAGED_PATHS);
6753 goto done;
6756 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6757 if (err)
6758 goto done;
6760 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6761 if (err)
6762 goto done;
6764 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6765 if (err)
6766 goto done;
6768 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6769 worktree);
6770 if (err)
6771 goto done;
6773 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6774 if (err)
6775 goto done;
6777 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6778 if (err)
6779 goto done;
6780 err = got_ref_resolve(commit_id, repo, commit_ref);
6781 if (err)
6782 goto done;
6784 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6785 if (err)
6786 goto done;
6787 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6788 if (err)
6789 goto done;
6791 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6792 if (err)
6793 goto done;
6794 done:
6795 free(commit_ref_name);
6796 free(branch_ref_name);
6797 free(fileindex_path);
6798 if (commit_ref)
6799 got_ref_close(commit_ref);
6800 if (base_commit_ref)
6801 got_ref_close(base_commit_ref);
6802 if (err) {
6803 free(*commit_id);
6804 *commit_id = NULL;
6805 free(*base_commit_id);
6806 *base_commit_id = NULL;
6807 if (*tmp_branch) {
6808 got_ref_close(*tmp_branch);
6809 *tmp_branch = NULL;
6811 if (*fileindex) {
6812 got_fileindex_free(*fileindex);
6813 *fileindex = NULL;
6815 lock_worktree(worktree, LOCK_EX);
6817 return err;
6820 static const struct got_error *
6821 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6823 const struct got_error *err;
6824 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6825 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6827 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6828 if (err)
6829 goto done;
6830 err = delete_ref(tmp_branch_name, repo);
6831 if (err)
6832 goto done;
6834 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6835 worktree);
6836 if (err)
6837 goto done;
6838 err = delete_ref(base_commit_ref_name, repo);
6839 if (err)
6840 goto done;
6842 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6843 if (err)
6844 goto done;
6845 err = delete_ref(branch_ref_name, repo);
6846 if (err)
6847 goto done;
6849 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6850 if (err)
6851 goto done;
6852 err = delete_ref(commit_ref_name, repo);
6853 if (err)
6854 goto done;
6855 done:
6856 free(tmp_branch_name);
6857 free(base_commit_ref_name);
6858 free(branch_ref_name);
6859 free(commit_ref_name);
6860 return err;
6863 const struct got_error *
6864 got_worktree_histedit_abort(struct got_worktree *worktree,
6865 struct got_fileindex *fileindex, struct got_repository *repo,
6866 struct got_reference *branch, struct got_object_id *base_commit_id,
6867 got_worktree_checkout_cb progress_cb, void *progress_arg)
6869 const struct got_error *err, *unlockerr, *sync_err;
6870 struct got_reference *resolved = NULL;
6871 char *fileindex_path = NULL;
6872 struct got_object_id *tree_id = NULL;
6873 struct revert_file_args rfa;
6875 err = lock_worktree(worktree, LOCK_EX);
6876 if (err)
6877 return err;
6879 err = got_ref_open(&resolved, repo,
6880 got_ref_get_symref_target(branch), 0);
6881 if (err)
6882 goto done;
6884 err = got_worktree_set_head_ref(worktree, resolved);
6885 if (err)
6886 goto done;
6888 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6889 if (err)
6890 goto done;
6892 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6893 worktree->path_prefix);
6894 if (err)
6895 goto done;
6897 err = delete_histedit_refs(worktree, repo);
6898 if (err)
6899 goto done;
6901 err = get_fileindex_path(&fileindex_path, worktree);
6902 if (err)
6903 goto done;
6905 rfa.worktree = worktree;
6906 rfa.fileindex = fileindex;
6907 rfa.progress_cb = progress_cb;
6908 rfa.progress_arg = progress_arg;
6909 rfa.patch_cb = NULL;
6910 rfa.patch_arg = NULL;
6911 rfa.repo = repo;
6912 err = worktree_status(worktree, "", fileindex, repo,
6913 revert_file, &rfa, NULL, NULL, 0, 0);
6914 if (err)
6915 goto sync;
6917 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6918 repo, progress_cb, progress_arg, NULL, NULL);
6919 sync:
6920 sync_err = sync_fileindex(fileindex, fileindex_path);
6921 if (sync_err && err == NULL)
6922 err = sync_err;
6923 done:
6924 got_ref_close(resolved);
6925 free(tree_id);
6926 free(fileindex_path);
6928 unlockerr = lock_worktree(worktree, LOCK_SH);
6929 if (unlockerr && err == NULL)
6930 err = unlockerr;
6931 return err;
6934 const struct got_error *
6935 got_worktree_histedit_complete(struct got_worktree *worktree,
6936 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6937 struct got_reference *edited_branch, struct got_repository *repo)
6939 const struct got_error *err, *unlockerr, *sync_err;
6940 struct got_object_id *new_head_commit_id = NULL;
6941 struct got_reference *resolved = NULL;
6942 char *fileindex_path = NULL;
6944 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6945 if (err)
6946 return err;
6948 err = got_ref_open(&resolved, repo,
6949 got_ref_get_symref_target(edited_branch), 0);
6950 if (err)
6951 goto done;
6953 err = got_ref_change_ref(resolved, new_head_commit_id);
6954 if (err)
6955 goto done;
6957 err = got_ref_write(resolved, repo);
6958 if (err)
6959 goto done;
6961 err = got_worktree_set_head_ref(worktree, resolved);
6962 if (err)
6963 goto done;
6965 err = delete_histedit_refs(worktree, repo);
6966 if (err)
6967 goto done;
6969 err = get_fileindex_path(&fileindex_path, worktree);
6970 if (err)
6971 goto done;
6972 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6973 sync_err = sync_fileindex(fileindex, fileindex_path);
6974 if (sync_err && err == NULL)
6975 err = sync_err;
6976 done:
6977 got_fileindex_free(fileindex);
6978 free(fileindex_path);
6979 free(new_head_commit_id);
6980 unlockerr = lock_worktree(worktree, LOCK_SH);
6981 if (unlockerr && err == NULL)
6982 err = unlockerr;
6983 return err;
6986 const struct got_error *
6987 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6988 struct got_object_id *commit_id, struct got_repository *repo)
6990 const struct got_error *err;
6991 char *commit_ref_name;
6993 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6994 if (err)
6995 return err;
6997 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6998 if (err)
6999 goto done;
7001 err = delete_ref(commit_ref_name, repo);
7002 done:
7003 free(commit_ref_name);
7004 return err;
7007 const struct got_error *
7008 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7009 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7010 struct got_worktree *worktree, const char *refname,
7011 struct got_repository *repo)
7013 const struct got_error *err = NULL;
7014 char *fileindex_path = NULL;
7015 struct check_rebase_ok_arg ok_arg;
7017 *fileindex = NULL;
7018 *branch_ref = NULL;
7019 *base_branch_ref = NULL;
7021 err = lock_worktree(worktree, LOCK_EX);
7022 if (err)
7023 return err;
7025 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7026 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7027 "cannot integrate a branch into itself; "
7028 "update -b or different branch name required");
7029 goto done;
7032 err = open_fileindex(fileindex, &fileindex_path, worktree);
7033 if (err)
7034 goto done;
7036 /* Preconditions are the same as for rebase. */
7037 ok_arg.worktree = worktree;
7038 ok_arg.repo = repo;
7039 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7040 &ok_arg);
7041 if (err)
7042 goto done;
7044 err = got_ref_open(branch_ref, repo, refname, 1);
7045 if (err)
7046 goto done;
7048 err = got_ref_open(base_branch_ref, repo,
7049 got_worktree_get_head_ref_name(worktree), 1);
7050 done:
7051 if (err) {
7052 if (*branch_ref) {
7053 got_ref_close(*branch_ref);
7054 *branch_ref = NULL;
7056 if (*base_branch_ref) {
7057 got_ref_close(*base_branch_ref);
7058 *base_branch_ref = NULL;
7060 if (*fileindex) {
7061 got_fileindex_free(*fileindex);
7062 *fileindex = NULL;
7064 lock_worktree(worktree, LOCK_SH);
7066 return err;
7069 const struct got_error *
7070 got_worktree_integrate_continue(struct got_worktree *worktree,
7071 struct got_fileindex *fileindex, struct got_repository *repo,
7072 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7073 got_worktree_checkout_cb progress_cb, void *progress_arg,
7074 got_cancel_cb cancel_cb, void *cancel_arg)
7076 const struct got_error *err = NULL, *sync_err, *unlockerr;
7077 char *fileindex_path = NULL;
7078 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7080 err = get_fileindex_path(&fileindex_path, worktree);
7081 if (err)
7082 goto done;
7084 err = got_ref_resolve(&commit_id, repo, branch_ref);
7085 if (err)
7086 goto done;
7088 err = got_object_id_by_path(&tree_id, repo, commit_id,
7089 worktree->path_prefix);
7090 if (err)
7091 goto done;
7093 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7094 if (err)
7095 goto done;
7097 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7098 progress_cb, progress_arg, cancel_cb, cancel_arg);
7099 if (err)
7100 goto sync;
7102 err = got_ref_change_ref(base_branch_ref, commit_id);
7103 if (err)
7104 goto sync;
7106 err = got_ref_write(base_branch_ref, repo);
7107 sync:
7108 sync_err = sync_fileindex(fileindex, fileindex_path);
7109 if (sync_err && err == NULL)
7110 err = sync_err;
7112 done:
7113 unlockerr = got_ref_unlock(branch_ref);
7114 if (unlockerr && err == NULL)
7115 err = unlockerr;
7116 got_ref_close(branch_ref);
7118 unlockerr = got_ref_unlock(base_branch_ref);
7119 if (unlockerr && err == NULL)
7120 err = unlockerr;
7121 got_ref_close(base_branch_ref);
7123 got_fileindex_free(fileindex);
7124 free(fileindex_path);
7125 free(tree_id);
7127 unlockerr = lock_worktree(worktree, LOCK_SH);
7128 if (unlockerr && err == NULL)
7129 err = unlockerr;
7130 return err;
7133 const struct got_error *
7134 got_worktree_integrate_abort(struct got_worktree *worktree,
7135 struct got_fileindex *fileindex, struct got_repository *repo,
7136 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7138 const struct got_error *err = NULL, *unlockerr = NULL;
7140 got_fileindex_free(fileindex);
7142 err = lock_worktree(worktree, LOCK_SH);
7144 unlockerr = got_ref_unlock(branch_ref);
7145 if (unlockerr && err == NULL)
7146 err = unlockerr;
7147 got_ref_close(branch_ref);
7149 unlockerr = got_ref_unlock(base_branch_ref);
7150 if (unlockerr && err == NULL)
7151 err = unlockerr;
7152 got_ref_close(base_branch_ref);
7154 return err;
7157 struct check_stage_ok_arg {
7158 struct got_object_id *head_commit_id;
7159 struct got_worktree *worktree;
7160 struct got_fileindex *fileindex;
7161 struct got_repository *repo;
7162 int have_changes;
7165 const struct got_error *
7166 check_stage_ok(void *arg, unsigned char status,
7167 unsigned char staged_status, const char *relpath,
7168 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7169 struct got_object_id *commit_id, int dirfd, const char *de_name)
7171 struct check_stage_ok_arg *a = arg;
7172 const struct got_error *err = NULL;
7173 struct got_fileindex_entry *ie;
7174 struct got_object_id base_commit_id;
7175 struct got_object_id *base_commit_idp = NULL;
7176 char *in_repo_path = NULL, *p;
7178 if (status == GOT_STATUS_UNVERSIONED ||
7179 status == GOT_STATUS_NO_CHANGE)
7180 return NULL;
7181 if (status == GOT_STATUS_NONEXISTENT)
7182 return got_error_set_errno(ENOENT, relpath);
7184 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7185 if (ie == NULL)
7186 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7188 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7189 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7190 relpath) == -1)
7191 return got_error_from_errno("asprintf");
7193 if (got_fileindex_entry_has_commit(ie)) {
7194 memcpy(base_commit_id.sha1, ie->commit_sha1,
7195 SHA1_DIGEST_LENGTH);
7196 base_commit_idp = &base_commit_id;
7199 if (status == GOT_STATUS_CONFLICT) {
7200 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7201 goto done;
7202 } else if (status != GOT_STATUS_ADD &&
7203 status != GOT_STATUS_MODIFY &&
7204 status != GOT_STATUS_DELETE) {
7205 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7206 goto done;
7209 a->have_changes = 1;
7211 p = in_repo_path;
7212 while (p[0] == '/')
7213 p++;
7214 err = check_out_of_date(p, status, staged_status,
7215 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7216 GOT_ERR_STAGE_OUT_OF_DATE);
7217 done:
7218 free(in_repo_path);
7219 return err;
7222 struct stage_path_arg {
7223 struct got_worktree *worktree;
7224 struct got_fileindex *fileindex;
7225 struct got_repository *repo;
7226 got_worktree_status_cb status_cb;
7227 void *status_arg;
7228 got_worktree_patch_cb patch_cb;
7229 void *patch_arg;
7230 int staged_something;
7231 int allow_bad_symlinks;
7234 static const struct got_error *
7235 stage_path(void *arg, unsigned char status,
7236 unsigned char staged_status, const char *relpath,
7237 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7238 struct got_object_id *commit_id, int dirfd, const char *de_name)
7240 struct stage_path_arg *a = arg;
7241 const struct got_error *err = NULL;
7242 struct got_fileindex_entry *ie;
7243 char *ondisk_path = NULL, *path_content = NULL;
7244 uint32_t stage;
7245 struct got_object_id *new_staged_blob_id = NULL;
7246 struct stat sb;
7248 if (status == GOT_STATUS_UNVERSIONED)
7249 return NULL;
7251 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7252 if (ie == NULL)
7253 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7255 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7256 relpath)== -1)
7257 return got_error_from_errno("asprintf");
7259 switch (status) {
7260 case GOT_STATUS_ADD:
7261 case GOT_STATUS_MODIFY:
7262 /* XXX could sb.st_mode be passed in by our caller? */
7263 if (lstat(ondisk_path, &sb) == -1) {
7264 err = got_error_from_errno2("lstat", ondisk_path);
7265 break;
7267 if (a->patch_cb) {
7268 if (status == GOT_STATUS_ADD) {
7269 int choice = GOT_PATCH_CHOICE_NONE;
7270 err = (*a->patch_cb)(&choice, a->patch_arg,
7271 status, ie->path, NULL, 1, 1);
7272 if (err)
7273 break;
7274 if (choice != GOT_PATCH_CHOICE_YES)
7275 break;
7276 } else {
7277 err = create_patched_content(&path_content, 0,
7278 staged_blob_id ? staged_blob_id : blob_id,
7279 ondisk_path, dirfd, de_name, ie->path,
7280 a->repo, a->patch_cb, a->patch_arg);
7281 if (err || path_content == NULL)
7282 break;
7285 err = got_object_blob_create(&new_staged_blob_id,
7286 path_content ? path_content : ondisk_path, a->repo);
7287 if (err)
7288 break;
7289 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7290 SHA1_DIGEST_LENGTH);
7291 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7292 stage = GOT_FILEIDX_STAGE_ADD;
7293 else
7294 stage = GOT_FILEIDX_STAGE_MODIFY;
7295 got_fileindex_entry_stage_set(ie, stage);
7296 if (S_ISLNK(sb.st_mode)) {
7297 int is_bad_symlink = 0;
7298 if (!a->allow_bad_symlinks) {
7299 char target_path[PATH_MAX];
7300 ssize_t target_len;
7301 target_len = readlink(ondisk_path, target_path,
7302 sizeof(target_path));
7303 if (target_len == -1) {
7304 err = got_error_from_errno2("readlink",
7305 ondisk_path);
7306 break;
7308 err = is_bad_symlink_target(&is_bad_symlink,
7309 target_path, target_len, ondisk_path,
7310 a->worktree->root_path);
7311 if (err)
7312 break;
7313 if (is_bad_symlink) {
7314 err = got_error_path(ondisk_path,
7315 GOT_ERR_BAD_SYMLINK);
7316 break;
7319 if (is_bad_symlink)
7320 got_fileindex_entry_staged_filetype_set(ie,
7321 GOT_FILEIDX_MODE_BAD_SYMLINK);
7322 else
7323 got_fileindex_entry_staged_filetype_set(ie,
7324 GOT_FILEIDX_MODE_SYMLINK);
7325 } else {
7326 got_fileindex_entry_staged_filetype_set(ie,
7327 GOT_FILEIDX_MODE_REGULAR_FILE);
7329 a->staged_something = 1;
7330 if (a->status_cb == NULL)
7331 break;
7332 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7333 get_staged_status(ie), relpath, blob_id,
7334 new_staged_blob_id, NULL, dirfd, de_name);
7335 break;
7336 case GOT_STATUS_DELETE:
7337 if (staged_status == GOT_STATUS_DELETE)
7338 break;
7339 if (a->patch_cb) {
7340 int choice = GOT_PATCH_CHOICE_NONE;
7341 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7342 ie->path, NULL, 1, 1);
7343 if (err)
7344 break;
7345 if (choice == GOT_PATCH_CHOICE_NO)
7346 break;
7347 if (choice != GOT_PATCH_CHOICE_YES) {
7348 err = got_error(GOT_ERR_PATCH_CHOICE);
7349 break;
7352 stage = GOT_FILEIDX_STAGE_DELETE;
7353 got_fileindex_entry_stage_set(ie, stage);
7354 a->staged_something = 1;
7355 if (a->status_cb == NULL)
7356 break;
7357 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7358 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7359 de_name);
7360 break;
7361 case GOT_STATUS_NO_CHANGE:
7362 break;
7363 case GOT_STATUS_CONFLICT:
7364 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7365 break;
7366 case GOT_STATUS_NONEXISTENT:
7367 err = got_error_set_errno(ENOENT, relpath);
7368 break;
7369 default:
7370 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7371 break;
7374 if (path_content && unlink(path_content) == -1 && err == NULL)
7375 err = got_error_from_errno2("unlink", path_content);
7376 free(path_content);
7377 free(ondisk_path);
7378 free(new_staged_blob_id);
7379 return err;
7382 const struct got_error *
7383 got_worktree_stage(struct got_worktree *worktree,
7384 struct got_pathlist_head *paths,
7385 got_worktree_status_cb status_cb, void *status_arg,
7386 got_worktree_patch_cb patch_cb, void *patch_arg,
7387 int allow_bad_symlinks, struct got_repository *repo)
7389 const struct got_error *err = NULL, *sync_err, *unlockerr;
7390 struct got_pathlist_entry *pe;
7391 struct got_fileindex *fileindex = NULL;
7392 char *fileindex_path = NULL;
7393 struct got_reference *head_ref = NULL;
7394 struct got_object_id *head_commit_id = NULL;
7395 struct check_stage_ok_arg oka;
7396 struct stage_path_arg spa;
7398 err = lock_worktree(worktree, LOCK_EX);
7399 if (err)
7400 return err;
7402 err = got_ref_open(&head_ref, repo,
7403 got_worktree_get_head_ref_name(worktree), 0);
7404 if (err)
7405 goto done;
7406 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7407 if (err)
7408 goto done;
7409 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7410 if (err)
7411 goto done;
7413 /* Check pre-conditions before staging anything. */
7414 oka.head_commit_id = head_commit_id;
7415 oka.worktree = worktree;
7416 oka.fileindex = fileindex;
7417 oka.repo = repo;
7418 oka.have_changes = 0;
7419 TAILQ_FOREACH(pe, paths, entry) {
7420 err = worktree_status(worktree, pe->path, fileindex, repo,
7421 check_stage_ok, &oka, NULL, NULL, 0, 0);
7422 if (err)
7423 goto done;
7425 if (!oka.have_changes) {
7426 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7427 goto done;
7430 spa.worktree = worktree;
7431 spa.fileindex = fileindex;
7432 spa.repo = repo;
7433 spa.patch_cb = patch_cb;
7434 spa.patch_arg = patch_arg;
7435 spa.status_cb = status_cb;
7436 spa.status_arg = status_arg;
7437 spa.staged_something = 0;
7438 spa.allow_bad_symlinks = allow_bad_symlinks;
7439 TAILQ_FOREACH(pe, paths, entry) {
7440 err = worktree_status(worktree, pe->path, fileindex, repo,
7441 stage_path, &spa, NULL, NULL, 0, 0);
7442 if (err)
7443 goto done;
7445 if (!spa.staged_something) {
7446 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7447 goto done;
7450 sync_err = sync_fileindex(fileindex, fileindex_path);
7451 if (sync_err && err == NULL)
7452 err = sync_err;
7453 done:
7454 if (head_ref)
7455 got_ref_close(head_ref);
7456 free(head_commit_id);
7457 free(fileindex_path);
7458 if (fileindex)
7459 got_fileindex_free(fileindex);
7460 unlockerr = lock_worktree(worktree, LOCK_SH);
7461 if (unlockerr && err == NULL)
7462 err = unlockerr;
7463 return err;
7466 struct unstage_path_arg {
7467 struct got_worktree *worktree;
7468 struct got_fileindex *fileindex;
7469 struct got_repository *repo;
7470 got_worktree_checkout_cb progress_cb;
7471 void *progress_arg;
7472 got_worktree_patch_cb patch_cb;
7473 void *patch_arg;
7476 static const struct got_error *
7477 create_unstaged_content(char **path_unstaged_content,
7478 char **path_new_staged_content, struct got_object_id *blob_id,
7479 struct got_object_id *staged_blob_id, const char *relpath,
7480 struct got_repository *repo,
7481 got_worktree_patch_cb patch_cb, void *patch_arg)
7483 const struct got_error *err, *free_err;
7484 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7485 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7486 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7487 struct got_diffreg_result *diffreg_result = NULL;
7488 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7489 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7491 *path_unstaged_content = NULL;
7492 *path_new_staged_content = NULL;
7494 err = got_object_id_str(&label1, blob_id);
7495 if (err)
7496 return err;
7497 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7498 if (err)
7499 goto done;
7501 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7502 if (err)
7503 goto done;
7505 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7506 if (err)
7507 goto done;
7509 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7510 if (err)
7511 goto done;
7513 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7514 if (err)
7515 goto done;
7517 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7518 if (err)
7519 goto done;
7521 err = got_diff_files(&diffreg_result, f1, label1, f2,
7522 path2, 3, 0, 1, NULL);
7523 if (err)
7524 goto done;
7526 err = got_opentemp_named(path_unstaged_content, &outfile,
7527 "got-unstaged-content");
7528 if (err)
7529 goto done;
7530 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7531 "got-new-staged-content");
7532 if (err)
7533 goto done;
7535 if (fseek(f1, 0L, SEEK_SET) == -1) {
7536 err = got_ferror(f1, GOT_ERR_IO);
7537 goto done;
7539 if (fseek(f2, 0L, SEEK_SET) == -1) {
7540 err = got_ferror(f2, GOT_ERR_IO);
7541 goto done;
7543 /* Count the number of actual changes in the diff result. */
7544 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7545 struct diff_chunk_context cc = {};
7546 diff_chunk_context_load_change(&cc, &nchunks_used,
7547 diffreg_result->result, n, 0);
7548 nchanges++;
7550 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7551 int choice;
7552 err = apply_or_reject_change(&choice, &nchunks_used,
7553 diffreg_result->result, n, relpath, f1, f2,
7554 &line_cur1, &line_cur2,
7555 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7556 if (err)
7557 goto done;
7558 if (choice == GOT_PATCH_CHOICE_YES)
7559 have_content = 1;
7560 else
7561 have_rejected_content = 1;
7562 if (choice == GOT_PATCH_CHOICE_QUIT)
7563 break;
7565 if (have_content || have_rejected_content)
7566 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7567 outfile, rejectfile);
7568 done:
7569 free(label1);
7570 if (blob)
7571 got_object_blob_close(blob);
7572 if (staged_blob)
7573 got_object_blob_close(staged_blob);
7574 free_err = got_diffreg_result_free(diffreg_result);
7575 if (free_err && err == NULL)
7576 err = free_err;
7577 if (f1 && fclose(f1) == EOF && err == NULL)
7578 err = got_error_from_errno2("fclose", path1);
7579 if (f2 && fclose(f2) == EOF && err == NULL)
7580 err = got_error_from_errno2("fclose", path2);
7581 if (outfile && fclose(outfile) == EOF && err == NULL)
7582 err = got_error_from_errno2("fclose", *path_unstaged_content);
7583 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7584 err = got_error_from_errno2("fclose", *path_new_staged_content);
7585 if (path1 && unlink(path1) == -1 && err == NULL)
7586 err = got_error_from_errno2("unlink", path1);
7587 if (path2 && unlink(path2) == -1 && err == NULL)
7588 err = got_error_from_errno2("unlink", path2);
7589 if (err || !have_content) {
7590 if (*path_unstaged_content &&
7591 unlink(*path_unstaged_content) == -1 && err == NULL)
7592 err = got_error_from_errno2("unlink",
7593 *path_unstaged_content);
7594 free(*path_unstaged_content);
7595 *path_unstaged_content = NULL;
7597 if (err || !have_content || !have_rejected_content) {
7598 if (*path_new_staged_content &&
7599 unlink(*path_new_staged_content) == -1 && err == NULL)
7600 err = got_error_from_errno2("unlink",
7601 *path_new_staged_content);
7602 free(*path_new_staged_content);
7603 *path_new_staged_content = NULL;
7605 free(path1);
7606 free(path2);
7607 return err;
7610 static const struct got_error *
7611 unstage_hunks(struct got_object_id *staged_blob_id,
7612 struct got_blob_object *blob_base,
7613 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7614 const char *ondisk_path, const char *label_orig,
7615 struct got_worktree *worktree, struct got_repository *repo,
7616 got_worktree_patch_cb patch_cb, void *patch_arg,
7617 got_worktree_checkout_cb progress_cb, void *progress_arg)
7619 const struct got_error *err = NULL;
7620 char *path_unstaged_content = NULL;
7621 char *path_new_staged_content = NULL;
7622 struct got_object_id *new_staged_blob_id = NULL;
7623 FILE *f = NULL;
7624 struct stat sb;
7626 err = create_unstaged_content(&path_unstaged_content,
7627 &path_new_staged_content, blob_id, staged_blob_id,
7628 ie->path, repo, patch_cb, patch_arg);
7629 if (err)
7630 return err;
7632 if (path_unstaged_content == NULL)
7633 return NULL;
7635 if (path_new_staged_content) {
7636 err = got_object_blob_create(&new_staged_blob_id,
7637 path_new_staged_content, repo);
7638 if (err)
7639 goto done;
7642 f = fopen(path_unstaged_content, "r");
7643 if (f == NULL) {
7644 err = got_error_from_errno2("fopen",
7645 path_unstaged_content);
7646 goto done;
7648 if (fstat(fileno(f), &sb) == -1) {
7649 err = got_error_from_errno2("fstat", path_unstaged_content);
7650 goto done;
7652 if (got_fileindex_entry_staged_filetype_get(ie) ==
7653 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7654 char link_target[PATH_MAX];
7655 size_t r;
7656 r = fread(link_target, 1, sizeof(link_target), f);
7657 if (r == 0 && ferror(f)) {
7658 err = got_error_from_errno("fread");
7659 goto done;
7661 if (r >= sizeof(link_target)) { /* should not happen */
7662 err = got_error(GOT_ERR_NO_SPACE);
7663 goto done;
7665 link_target[r] = '\0';
7666 err = merge_symlink(worktree, blob_base,
7667 ondisk_path, ie->path, label_orig, link_target,
7668 worktree->base_commit_id, repo, progress_cb,
7669 progress_arg);
7670 } else {
7671 int local_changes_subsumed;
7672 err = merge_file(&local_changes_subsumed, worktree,
7673 blob_base, ondisk_path, ie->path,
7674 got_fileindex_perms_to_st(ie),
7675 path_unstaged_content, label_orig, "unstaged",
7676 repo, progress_cb, progress_arg);
7678 if (err)
7679 goto done;
7681 if (new_staged_blob_id) {
7682 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7683 SHA1_DIGEST_LENGTH);
7684 } else {
7685 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7686 got_fileindex_entry_staged_filetype_set(ie, 0);
7688 done:
7689 free(new_staged_blob_id);
7690 if (path_unstaged_content &&
7691 unlink(path_unstaged_content) == -1 && err == NULL)
7692 err = got_error_from_errno2("unlink", path_unstaged_content);
7693 if (path_new_staged_content &&
7694 unlink(path_new_staged_content) == -1 && err == NULL)
7695 err = got_error_from_errno2("unlink", path_new_staged_content);
7696 if (f && fclose(f) != 0 && err == NULL)
7697 err = got_error_from_errno2("fclose", path_unstaged_content);
7698 free(path_unstaged_content);
7699 free(path_new_staged_content);
7700 return err;
7703 static const struct got_error *
7704 unstage_path(void *arg, unsigned char status,
7705 unsigned char staged_status, const char *relpath,
7706 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7707 struct got_object_id *commit_id, int dirfd, const char *de_name)
7709 const struct got_error *err = NULL;
7710 struct unstage_path_arg *a = arg;
7711 struct got_fileindex_entry *ie;
7712 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7713 char *ondisk_path = NULL;
7714 char *id_str = NULL, *label_orig = NULL;
7715 int local_changes_subsumed;
7716 struct stat sb;
7718 if (staged_status != GOT_STATUS_ADD &&
7719 staged_status != GOT_STATUS_MODIFY &&
7720 staged_status != GOT_STATUS_DELETE)
7721 return NULL;
7723 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7724 if (ie == NULL)
7725 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7727 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7728 == -1)
7729 return got_error_from_errno("asprintf");
7731 err = got_object_id_str(&id_str,
7732 commit_id ? commit_id : a->worktree->base_commit_id);
7733 if (err)
7734 goto done;
7735 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7736 id_str) == -1) {
7737 err = got_error_from_errno("asprintf");
7738 goto done;
7741 switch (staged_status) {
7742 case GOT_STATUS_MODIFY:
7743 err = got_object_open_as_blob(&blob_base, a->repo,
7744 blob_id, 8192);
7745 if (err)
7746 break;
7747 /* fall through */
7748 case GOT_STATUS_ADD:
7749 if (a->patch_cb) {
7750 if (staged_status == GOT_STATUS_ADD) {
7751 int choice = GOT_PATCH_CHOICE_NONE;
7752 err = (*a->patch_cb)(&choice, a->patch_arg,
7753 staged_status, ie->path, NULL, 1, 1);
7754 if (err)
7755 break;
7756 if (choice != GOT_PATCH_CHOICE_YES)
7757 break;
7758 } else {
7759 err = unstage_hunks(staged_blob_id,
7760 blob_base, blob_id, ie, ondisk_path,
7761 label_orig, a->worktree, a->repo,
7762 a->patch_cb, a->patch_arg,
7763 a->progress_cb, a->progress_arg);
7764 break; /* Done with this file. */
7767 err = got_object_open_as_blob(&blob_staged, a->repo,
7768 staged_blob_id, 8192);
7769 if (err)
7770 break;
7771 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7772 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7773 case GOT_FILEIDX_MODE_REGULAR_FILE:
7774 err = merge_blob(&local_changes_subsumed, a->worktree,
7775 blob_base, ondisk_path, relpath,
7776 got_fileindex_perms_to_st(ie), label_orig,
7777 blob_staged, commit_id ? commit_id :
7778 a->worktree->base_commit_id, a->repo,
7779 a->progress_cb, a->progress_arg);
7780 break;
7781 case GOT_FILEIDX_MODE_SYMLINK:
7782 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7783 char *staged_target;
7784 err = got_object_blob_read_to_str(
7785 &staged_target, blob_staged);
7786 if (err)
7787 goto done;
7788 err = merge_symlink(a->worktree, blob_base,
7789 ondisk_path, relpath, label_orig,
7790 staged_target, commit_id ? commit_id :
7791 a->worktree->base_commit_id,
7792 a->repo, a->progress_cb, a->progress_arg);
7793 free(staged_target);
7794 } else {
7795 err = merge_blob(&local_changes_subsumed,
7796 a->worktree, blob_base, ondisk_path,
7797 relpath, got_fileindex_perms_to_st(ie),
7798 label_orig, blob_staged,
7799 commit_id ? commit_id :
7800 a->worktree->base_commit_id, a->repo,
7801 a->progress_cb, a->progress_arg);
7803 break;
7804 default:
7805 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7806 break;
7808 if (err == NULL) {
7809 got_fileindex_entry_stage_set(ie,
7810 GOT_FILEIDX_STAGE_NONE);
7811 got_fileindex_entry_staged_filetype_set(ie, 0);
7813 break;
7814 case GOT_STATUS_DELETE:
7815 if (a->patch_cb) {
7816 int choice = GOT_PATCH_CHOICE_NONE;
7817 err = (*a->patch_cb)(&choice, a->patch_arg,
7818 staged_status, ie->path, NULL, 1, 1);
7819 if (err)
7820 break;
7821 if (choice == GOT_PATCH_CHOICE_NO)
7822 break;
7823 if (choice != GOT_PATCH_CHOICE_YES) {
7824 err = got_error(GOT_ERR_PATCH_CHOICE);
7825 break;
7828 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7829 got_fileindex_entry_staged_filetype_set(ie, 0);
7830 err = get_file_status(&status, &sb, ie, ondisk_path,
7831 dirfd, de_name, a->repo);
7832 if (err)
7833 break;
7834 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7835 break;
7837 done:
7838 free(ondisk_path);
7839 if (blob_base)
7840 got_object_blob_close(blob_base);
7841 if (blob_staged)
7842 got_object_blob_close(blob_staged);
7843 free(id_str);
7844 free(label_orig);
7845 return err;
7848 const struct got_error *
7849 got_worktree_unstage(struct got_worktree *worktree,
7850 struct got_pathlist_head *paths,
7851 got_worktree_checkout_cb progress_cb, void *progress_arg,
7852 got_worktree_patch_cb patch_cb, void *patch_arg,
7853 struct got_repository *repo)
7855 const struct got_error *err = NULL, *sync_err, *unlockerr;
7856 struct got_pathlist_entry *pe;
7857 struct got_fileindex *fileindex = NULL;
7858 char *fileindex_path = NULL;
7859 struct unstage_path_arg upa;
7861 err = lock_worktree(worktree, LOCK_EX);
7862 if (err)
7863 return err;
7865 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7866 if (err)
7867 goto done;
7869 upa.worktree = worktree;
7870 upa.fileindex = fileindex;
7871 upa.repo = repo;
7872 upa.progress_cb = progress_cb;
7873 upa.progress_arg = progress_arg;
7874 upa.patch_cb = patch_cb;
7875 upa.patch_arg = patch_arg;
7876 TAILQ_FOREACH(pe, paths, entry) {
7877 err = worktree_status(worktree, pe->path, fileindex, repo,
7878 unstage_path, &upa, NULL, NULL, 0, 0);
7879 if (err)
7880 goto done;
7883 sync_err = sync_fileindex(fileindex, fileindex_path);
7884 if (sync_err && err == NULL)
7885 err = sync_err;
7886 done:
7887 free(fileindex_path);
7888 if (fileindex)
7889 got_fileindex_free(fileindex);
7890 unlockerr = lock_worktree(worktree, LOCK_SH);
7891 if (unlockerr && err == NULL)
7892 err = unlockerr;
7893 return err;
7896 struct report_file_info_arg {
7897 struct got_worktree *worktree;
7898 got_worktree_path_info_cb info_cb;
7899 void *info_arg;
7900 struct got_pathlist_head *paths;
7901 got_cancel_cb cancel_cb;
7902 void *cancel_arg;
7905 static const struct got_error *
7906 report_file_info(void *arg, struct got_fileindex_entry *ie)
7908 struct report_file_info_arg *a = arg;
7909 struct got_pathlist_entry *pe;
7910 struct got_object_id blob_id, staged_blob_id, commit_id;
7911 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7912 struct got_object_id *commit_idp = NULL;
7913 int stage;
7915 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7916 return got_error(GOT_ERR_CANCELLED);
7918 TAILQ_FOREACH(pe, a->paths, entry) {
7919 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7920 got_path_is_child(ie->path, pe->path, pe->path_len))
7921 break;
7923 if (pe == NULL) /* not found */
7924 return NULL;
7926 if (got_fileindex_entry_has_blob(ie)) {
7927 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7928 blob_idp = &blob_id;
7930 stage = got_fileindex_entry_stage_get(ie);
7931 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7932 stage == GOT_FILEIDX_STAGE_ADD) {
7933 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7934 SHA1_DIGEST_LENGTH);
7935 staged_blob_idp = &staged_blob_id;
7938 if (got_fileindex_entry_has_commit(ie)) {
7939 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7940 commit_idp = &commit_id;
7943 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7944 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7947 const struct got_error *
7948 got_worktree_path_info(struct got_worktree *worktree,
7949 struct got_pathlist_head *paths,
7950 got_worktree_path_info_cb info_cb, void *info_arg,
7951 got_cancel_cb cancel_cb, void *cancel_arg)
7954 const struct got_error *err = NULL, *unlockerr;
7955 struct got_fileindex *fileindex = NULL;
7956 char *fileindex_path = NULL;
7957 struct report_file_info_arg arg;
7959 err = lock_worktree(worktree, LOCK_SH);
7960 if (err)
7961 return err;
7963 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7964 if (err)
7965 goto done;
7967 arg.worktree = worktree;
7968 arg.info_cb = info_cb;
7969 arg.info_arg = info_arg;
7970 arg.paths = paths;
7971 arg.cancel_cb = cancel_cb;
7972 arg.cancel_arg = cancel_arg;
7973 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7974 &arg);
7975 done:
7976 free(fileindex_path);
7977 if (fileindex)
7978 got_fileindex_free(fileindex);
7979 unlockerr = lock_worktree(worktree, LOCK_UN);
7980 if (unlockerr && err == NULL)
7981 err = unlockerr;
7982 return err;