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;
1603 size_t len;
1604 const char delim[3] = {'\0', '\0', '\0'};
1606 while (*status == GOT_STATUS_MODIFY) {
1607 line = fparseln(f, &len, NULL, delim, 0);
1608 if (line == NULL) {
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++;
1624 return err;
1627 static int
1628 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1630 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1631 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1634 static int
1635 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1637 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1638 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1639 ie->mtime_sec == sb->st_mtim.tv_sec &&
1640 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1641 ie->size == (sb->st_size & 0xffffffff) &&
1642 !xbit_differs(ie, sb->st_mode));
1645 static unsigned char
1646 get_staged_status(struct got_fileindex_entry *ie)
1648 switch (got_fileindex_entry_stage_get(ie)) {
1649 case GOT_FILEIDX_STAGE_ADD:
1650 return GOT_STATUS_ADD;
1651 case GOT_FILEIDX_STAGE_DELETE:
1652 return GOT_STATUS_DELETE;
1653 case GOT_FILEIDX_STAGE_MODIFY:
1654 return GOT_STATUS_MODIFY;
1655 default:
1656 return GOT_STATUS_NO_CHANGE;
1660 static const struct got_error *
1661 get_symlink_modification_status(unsigned char *status,
1662 struct got_fileindex_entry *ie, const char *abspath,
1663 int dirfd, const char *de_name, struct got_blob_object *blob)
1665 const struct got_error *err = NULL;
1666 char target_path[PATH_MAX];
1667 char etarget[PATH_MAX];
1668 ssize_t elen;
1669 size_t len, target_len = 0;
1670 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1671 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1673 *status = GOT_STATUS_NO_CHANGE;
1675 /* Blob object content specifies the target path of the link. */
1676 do {
1677 err = got_object_blob_read_block(&len, blob);
1678 if (err)
1679 return err;
1680 if (len + target_len >= sizeof(target_path)) {
1682 * Should not happen. The blob contents were OK
1683 * when this symlink was installed.
1685 return got_error(GOT_ERR_NO_SPACE);
1687 if (len > 0) {
1688 /* Skip blob object header first time around. */
1689 memcpy(target_path + target_len, buf + hdrlen,
1690 len - hdrlen);
1691 target_len += len - hdrlen;
1692 hdrlen = 0;
1694 } while (len != 0);
1695 target_path[target_len] = '\0';
1697 if (dirfd != -1) {
1698 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1699 if (elen == -1)
1700 return got_error_from_errno2("readlinkat", abspath);
1701 } else {
1702 elen = readlink(abspath, etarget, sizeof(etarget));
1703 if (elen == -1)
1704 return got_error_from_errno2("readlink", abspath);
1707 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1708 *status = GOT_STATUS_MODIFY;
1710 return NULL;
1713 static const struct got_error *
1714 get_file_status(unsigned char *status, struct stat *sb,
1715 struct got_fileindex_entry *ie, const char *abspath,
1716 int dirfd, const char *de_name, struct got_repository *repo)
1718 const struct got_error *err = NULL;
1719 struct got_object_id id;
1720 size_t hdrlen;
1721 int fd = -1;
1722 FILE *f = NULL;
1723 uint8_t fbuf[8192];
1724 struct got_blob_object *blob = NULL;
1725 size_t flen, blen;
1726 unsigned char staged_status = get_staged_status(ie);
1728 *status = GOT_STATUS_NO_CHANGE;
1731 * Whenever the caller provides a directory descriptor and a
1732 * directory entry name for the file, use them! This prevents
1733 * race conditions if filesystem paths change beneath our feet.
1735 if (dirfd != -1) {
1736 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1737 if (errno == ENOENT) {
1738 if (got_fileindex_entry_has_file_on_disk(ie))
1739 *status = GOT_STATUS_MISSING;
1740 else
1741 *status = GOT_STATUS_DELETE;
1742 goto done;
1744 err = got_error_from_errno2("fstatat", abspath);
1745 goto done;
1747 } else {
1748 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1749 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1750 return got_error_from_errno2("open", abspath);
1751 else if (fd == -1 && errno == ELOOP) {
1752 if (lstat(abspath, sb) == -1)
1753 return got_error_from_errno2("lstat", abspath);
1754 } else if (fd == -1 || fstat(fd, sb) == -1) {
1755 if (errno == ENOENT) {
1756 if (got_fileindex_entry_has_file_on_disk(ie))
1757 *status = GOT_STATUS_MISSING;
1758 else
1759 *status = GOT_STATUS_DELETE;
1760 goto done;
1762 err = got_error_from_errno2("fstat", abspath);
1763 goto done;
1767 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1768 *status = GOT_STATUS_OBSTRUCTED;
1769 goto done;
1772 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1773 *status = GOT_STATUS_DELETE;
1774 goto done;
1775 } else if (!got_fileindex_entry_has_blob(ie) &&
1776 staged_status != GOT_STATUS_ADD) {
1777 *status = GOT_STATUS_ADD;
1778 goto done;
1781 if (!stat_info_differs(ie, sb))
1782 goto done;
1784 if (S_ISLNK(sb->st_mode) &&
1785 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1786 *status = GOT_STATUS_MODIFY;
1787 goto done;
1790 if (staged_status == GOT_STATUS_MODIFY ||
1791 staged_status == GOT_STATUS_ADD)
1792 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1793 else
1794 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1796 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1797 if (err)
1798 goto done;
1800 if (S_ISLNK(sb->st_mode)) {
1801 err = get_symlink_modification_status(status, ie,
1802 abspath, dirfd, de_name, blob);
1803 goto done;
1806 if (dirfd != -1) {
1807 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1808 if (fd == -1) {
1809 err = got_error_from_errno2("openat", abspath);
1810 goto done;
1814 f = fdopen(fd, "r");
1815 if (f == NULL) {
1816 err = got_error_from_errno2("fdopen", abspath);
1817 goto done;
1819 fd = -1;
1820 hdrlen = got_object_blob_get_hdrlen(blob);
1821 for (;;) {
1822 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1823 err = got_object_blob_read_block(&blen, blob);
1824 if (err)
1825 goto done;
1826 /* Skip length of blob object header first time around. */
1827 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1828 if (flen == 0 && ferror(f)) {
1829 err = got_error_from_errno("fread");
1830 goto done;
1832 if (blen - hdrlen == 0) {
1833 if (flen != 0)
1834 *status = GOT_STATUS_MODIFY;
1835 break;
1836 } else if (flen == 0) {
1837 if (blen - hdrlen != 0)
1838 *status = GOT_STATUS_MODIFY;
1839 break;
1840 } else if (blen - hdrlen == flen) {
1841 /* Skip blob object header first time around. */
1842 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1843 *status = GOT_STATUS_MODIFY;
1844 break;
1846 } else {
1847 *status = GOT_STATUS_MODIFY;
1848 break;
1850 hdrlen = 0;
1853 if (*status == GOT_STATUS_MODIFY) {
1854 rewind(f);
1855 err = get_modified_file_content_status(status, f);
1856 } else if (xbit_differs(ie, sb->st_mode))
1857 *status = GOT_STATUS_MODE_CHANGE;
1858 done:
1859 if (blob)
1860 got_object_blob_close(blob);
1861 if (f != NULL && fclose(f) == EOF && err == NULL)
1862 err = got_error_from_errno2("fclose", abspath);
1863 if (fd != -1 && close(fd) == -1 && err == NULL)
1864 err = got_error_from_errno2("close", abspath);
1865 return err;
1869 * Update timestamps in the file index if a file is unmodified and
1870 * we had to run a full content comparison to find out.
1872 static const struct got_error *
1873 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1874 struct got_fileindex_entry *ie, struct stat *sb)
1876 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1877 return got_fileindex_entry_update(ie, wt_fd, path,
1878 ie->blob_sha1, ie->commit_sha1, 1);
1880 return NULL;
1883 static const struct got_error *
1884 update_blob(struct got_worktree *worktree,
1885 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1886 struct got_tree_entry *te, const char *path,
1887 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1888 void *progress_arg)
1890 const struct got_error *err = NULL;
1891 struct got_blob_object *blob = NULL;
1892 char *ondisk_path;
1893 unsigned char status = GOT_STATUS_NO_CHANGE;
1894 struct stat sb;
1896 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1897 return got_error_from_errno("asprintf");
1899 if (ie) {
1900 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1901 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1902 goto done;
1904 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1905 repo);
1906 if (err)
1907 goto done;
1908 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1909 sb.st_mode = got_fileindex_perms_to_st(ie);
1910 } else {
1911 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1912 status = GOT_STATUS_UNVERSIONED;
1915 if (status == GOT_STATUS_OBSTRUCTED) {
1916 err = (*progress_cb)(progress_arg, status, path);
1917 goto done;
1919 if (status == GOT_STATUS_CONFLICT) {
1920 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1921 path);
1922 goto done;
1925 if (ie && status != GOT_STATUS_MISSING &&
1926 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1927 if (got_fileindex_entry_has_commit(ie) &&
1928 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1929 SHA1_DIGEST_LENGTH) == 0) {
1930 err = sync_timestamps(worktree->root_fd,
1931 path, status, ie, &sb);
1932 if (err)
1933 goto done;
1934 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1935 path);
1936 goto done;
1938 if (got_fileindex_entry_has_blob(ie) &&
1939 memcmp(ie->blob_sha1, te->id.sha1,
1940 SHA1_DIGEST_LENGTH) == 0) {
1941 err = sync_timestamps(worktree->root_fd,
1942 path, status, ie, &sb);
1943 goto done;
1947 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1948 if (err)
1949 goto done;
1951 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1952 int update_timestamps;
1953 struct got_blob_object *blob2 = NULL;
1954 char *label_orig = NULL;
1955 if (got_fileindex_entry_has_blob(ie)) {
1956 struct got_object_id id2;
1957 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1958 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1959 if (err)
1960 goto done;
1962 if (got_fileindex_entry_has_commit(ie)) {
1963 char id_str[SHA1_DIGEST_STRING_LENGTH];
1964 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1965 sizeof(id_str)) == NULL) {
1966 err = got_error_path(id_str,
1967 GOT_ERR_BAD_OBJ_ID_STR);
1968 goto done;
1970 if (asprintf(&label_orig, "%s: commit %s",
1971 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1972 err = got_error_from_errno("asprintf");
1973 goto done;
1976 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1977 char *link_target;
1978 err = got_object_blob_read_to_str(&link_target, blob);
1979 if (err)
1980 goto done;
1981 err = merge_symlink(worktree, blob2, ondisk_path, path,
1982 label_orig, link_target, worktree->base_commit_id,
1983 repo, progress_cb, progress_arg);
1984 free(link_target);
1985 } else {
1986 err = merge_blob(&update_timestamps, worktree, blob2,
1987 ondisk_path, path, sb.st_mode, label_orig, blob,
1988 worktree->base_commit_id, repo,
1989 progress_cb, progress_arg);
1991 free(label_orig);
1992 if (blob2)
1993 got_object_blob_close(blob2);
1994 if (err)
1995 goto done;
1997 * Do not update timestamps of files with local changes.
1998 * Otherwise, a future status walk would treat them as
1999 * unmodified files again.
2001 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2002 blob->id.sha1, worktree->base_commit_id->sha1,
2003 update_timestamps);
2004 } else if (status == GOT_STATUS_MODE_CHANGE) {
2005 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2006 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2007 } else if (status == GOT_STATUS_DELETE) {
2008 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2009 if (err)
2010 goto done;
2011 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2012 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2013 if (err)
2014 goto done;
2015 } else {
2016 int is_bad_symlink = 0;
2017 if (S_ISLNK(te->mode)) {
2018 err = install_symlink(&is_bad_symlink, worktree,
2019 ondisk_path, path, blob,
2020 status == GOT_STATUS_MISSING, 0,
2021 status == GOT_STATUS_UNVERSIONED, repo,
2022 progress_cb, progress_arg);
2023 } else {
2024 err = install_blob(worktree, ondisk_path, path,
2025 te->mode, sb.st_mode, blob,
2026 status == GOT_STATUS_MISSING, 0, 0,
2027 status == GOT_STATUS_UNVERSIONED, repo,
2028 progress_cb, progress_arg);
2030 if (err)
2031 goto done;
2033 if (ie) {
2034 err = got_fileindex_entry_update(ie,
2035 worktree->root_fd, path, blob->id.sha1,
2036 worktree->base_commit_id->sha1, 1);
2037 } else {
2038 err = create_fileindex_entry(&ie, fileindex,
2039 worktree->base_commit_id, worktree->root_fd, path,
2040 &blob->id);
2042 if (err)
2043 goto done;
2045 if (is_bad_symlink) {
2046 got_fileindex_entry_filetype_set(ie,
2047 GOT_FILEIDX_MODE_BAD_SYMLINK);
2050 got_object_blob_close(blob);
2051 done:
2052 free(ondisk_path);
2053 return err;
2056 static const struct got_error *
2057 remove_ondisk_file(const char *root_path, const char *path)
2059 const struct got_error *err = NULL;
2060 char *ondisk_path = NULL;
2062 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2063 return got_error_from_errno("asprintf");
2065 if (unlink(ondisk_path) == -1) {
2066 if (errno != ENOENT)
2067 err = got_error_from_errno2("unlink", ondisk_path);
2068 } else {
2069 size_t root_len = strlen(root_path);
2070 do {
2071 char *parent;
2072 err = got_path_dirname(&parent, ondisk_path);
2073 if (err)
2074 break;
2075 free(ondisk_path);
2076 ondisk_path = parent;
2077 if (rmdir(ondisk_path) == -1) {
2078 if (errno != ENOTEMPTY)
2079 err = got_error_from_errno2("rmdir",
2080 ondisk_path);
2081 break;
2083 } while (got_path_cmp(ondisk_path, root_path,
2084 strlen(ondisk_path), root_len) != 0);
2086 free(ondisk_path);
2087 return err;
2090 static const struct got_error *
2091 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2092 struct got_fileindex_entry *ie, struct got_repository *repo,
2093 got_worktree_checkout_cb progress_cb, void *progress_arg)
2095 const struct got_error *err = NULL;
2096 unsigned char status;
2097 struct stat sb;
2098 char *ondisk_path;
2100 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2101 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2103 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2104 == -1)
2105 return got_error_from_errno("asprintf");
2107 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2108 if (err)
2109 goto done;
2111 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2112 char ondisk_target[PATH_MAX];
2113 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2114 sizeof(ondisk_target));
2115 if (ondisk_len == -1) {
2116 err = got_error_from_errno2("readlink", ondisk_path);
2117 goto done;
2119 ondisk_target[ondisk_len] = '\0';
2120 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2121 NULL, NULL, /* XXX pass common ancestor info? */
2122 ondisk_target, ondisk_path);
2123 if (err)
2124 goto done;
2125 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2126 ie->path);
2127 goto done;
2130 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2131 status == GOT_STATUS_ADD) {
2132 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2133 if (err)
2134 goto done;
2136 * Preserve the working file and change the deleted blob's
2137 * entry into a schedule-add entry.
2139 err = got_fileindex_entry_update(ie, worktree->root_fd,
2140 ie->path, NULL, NULL, 0);
2141 } else {
2142 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2143 if (err)
2144 goto done;
2145 if (status == GOT_STATUS_NO_CHANGE) {
2146 err = remove_ondisk_file(worktree->root_path, ie->path);
2147 if (err)
2148 goto done;
2150 got_fileindex_entry_remove(fileindex, ie);
2152 done:
2153 free(ondisk_path);
2154 return err;
2157 struct diff_cb_arg {
2158 struct got_fileindex *fileindex;
2159 struct got_worktree *worktree;
2160 struct got_repository *repo;
2161 got_worktree_checkout_cb progress_cb;
2162 void *progress_arg;
2163 got_cancel_cb cancel_cb;
2164 void *cancel_arg;
2167 static const struct got_error *
2168 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2169 struct got_tree_entry *te, const char *parent_path)
2171 struct diff_cb_arg *a = arg;
2173 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2174 return got_error(GOT_ERR_CANCELLED);
2176 return update_blob(a->worktree, a->fileindex, ie, te,
2177 ie->path, a->repo, a->progress_cb, a->progress_arg);
2180 static const struct got_error *
2181 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2183 struct diff_cb_arg *a = arg;
2185 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2186 return got_error(GOT_ERR_CANCELLED);
2188 return delete_blob(a->worktree, a->fileindex, ie,
2189 a->repo, a->progress_cb, a->progress_arg);
2192 static const struct got_error *
2193 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2195 struct diff_cb_arg *a = arg;
2196 const struct got_error *err;
2197 char *path;
2199 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2200 return got_error(GOT_ERR_CANCELLED);
2202 if (got_object_tree_entry_is_submodule(te))
2203 return NULL;
2205 if (asprintf(&path, "%s%s%s", parent_path,
2206 parent_path[0] ? "/" : "", te->name)
2207 == -1)
2208 return got_error_from_errno("asprintf");
2210 if (S_ISDIR(te->mode))
2211 err = add_dir_on_disk(a->worktree, path);
2212 else
2213 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2214 a->repo, a->progress_cb, a->progress_arg);
2216 free(path);
2217 return err;
2220 const struct got_error *
2221 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2223 uint32_t uuid_status;
2225 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2226 if (uuid_status != uuid_s_ok) {
2227 *uuidstr = NULL;
2228 return got_error_uuid(uuid_status, "uuid_to_string");
2231 return NULL;
2234 static const struct got_error *
2235 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2237 const struct got_error *err = NULL;
2238 char *uuidstr = NULL;
2240 *refname = NULL;
2242 err = got_worktree_get_uuid(&uuidstr, worktree);
2243 if (err)
2244 return err;
2246 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2247 err = got_error_from_errno("asprintf");
2248 *refname = NULL;
2250 free(uuidstr);
2251 return err;
2254 const struct got_error *
2255 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2257 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2260 static const struct got_error *
2261 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2263 return get_ref_name(refname, worktree,
2264 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2267 static const struct got_error *
2268 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2270 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2273 static const struct got_error *
2274 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2276 return get_ref_name(refname, worktree,
2277 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2280 static const struct got_error *
2281 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2283 return get_ref_name(refname, worktree,
2284 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2287 static const struct got_error *
2288 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2290 return get_ref_name(refname, worktree,
2291 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2294 static const struct got_error *
2295 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2297 return get_ref_name(refname, worktree,
2298 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2301 static const struct got_error *
2302 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2304 return get_ref_name(refname, worktree,
2305 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2308 static const struct got_error *
2309 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2311 return get_ref_name(refname, worktree,
2312 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2315 const struct got_error *
2316 got_worktree_get_histedit_script_path(char **path,
2317 struct got_worktree *worktree)
2319 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2320 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2321 *path = NULL;
2322 return got_error_from_errno("asprintf");
2324 return NULL;
2328 * Prevent Git's garbage collector from deleting our base commit by
2329 * setting a reference to our base commit's ID.
2331 static const struct got_error *
2332 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2334 const struct got_error *err = NULL;
2335 struct got_reference *ref = NULL;
2336 char *refname;
2338 err = got_worktree_get_base_ref_name(&refname, worktree);
2339 if (err)
2340 return err;
2342 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2343 if (err)
2344 goto done;
2346 err = got_ref_write(ref, repo);
2347 done:
2348 free(refname);
2349 if (ref)
2350 got_ref_close(ref);
2351 return err;
2354 static const struct got_error *
2355 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2357 const struct got_error *err = NULL;
2359 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2360 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2361 err = got_error_from_errno("asprintf");
2362 *fileindex_path = NULL;
2364 return err;
2368 static const struct got_error *
2369 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2370 struct got_worktree *worktree)
2372 const struct got_error *err = NULL;
2373 FILE *index = NULL;
2375 *fileindex_path = NULL;
2376 *fileindex = got_fileindex_alloc();
2377 if (*fileindex == NULL)
2378 return got_error_from_errno("got_fileindex_alloc");
2380 err = get_fileindex_path(fileindex_path, worktree);
2381 if (err)
2382 goto done;
2384 index = fopen(*fileindex_path, "rb");
2385 if (index == NULL) {
2386 if (errno != ENOENT)
2387 err = got_error_from_errno2("fopen", *fileindex_path);
2388 } else {
2389 err = got_fileindex_read(*fileindex, index);
2390 if (fclose(index) != 0 && err == NULL)
2391 err = got_error_from_errno("fclose");
2393 done:
2394 if (err) {
2395 free(*fileindex_path);
2396 *fileindex_path = NULL;
2397 got_fileindex_free(*fileindex);
2398 *fileindex = NULL;
2400 return err;
2403 struct bump_base_commit_id_arg {
2404 struct got_object_id *base_commit_id;
2405 const char *path;
2406 size_t path_len;
2407 const char *entry_name;
2408 got_worktree_checkout_cb progress_cb;
2409 void *progress_arg;
2412 /* Bump base commit ID of all files within an updated part of the work tree. */
2413 static const struct got_error *
2414 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2416 const struct got_error *err;
2417 struct bump_base_commit_id_arg *a = arg;
2419 if (a->entry_name) {
2420 if (strcmp(ie->path, a->path) != 0)
2421 return NULL;
2422 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2423 return NULL;
2425 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2426 SHA1_DIGEST_LENGTH) == 0)
2427 return NULL;
2429 if (a->progress_cb) {
2430 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2431 ie->path);
2432 if (err)
2433 return err;
2435 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2436 return NULL;
2439 static const struct got_error *
2440 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2441 struct got_fileindex *fileindex,
2442 got_worktree_checkout_cb progress_cb, void *progress_arg)
2444 struct bump_base_commit_id_arg bbc_arg;
2446 bbc_arg.base_commit_id = worktree->base_commit_id;
2447 bbc_arg.entry_name = NULL;
2448 bbc_arg.path = "";
2449 bbc_arg.path_len = 0;
2450 bbc_arg.progress_cb = progress_cb;
2451 bbc_arg.progress_arg = progress_arg;
2453 return got_fileindex_for_each_entry_safe(fileindex,
2454 bump_base_commit_id, &bbc_arg);
2457 static const struct got_error *
2458 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2460 const struct got_error *err = NULL;
2461 char *new_fileindex_path = NULL;
2462 FILE *new_index = NULL;
2463 struct timespec timeout;
2465 err = got_opentemp_named(&new_fileindex_path, &new_index,
2466 fileindex_path);
2467 if (err)
2468 goto done;
2470 err = got_fileindex_write(fileindex, new_index);
2471 if (err)
2472 goto done;
2474 if (rename(new_fileindex_path, fileindex_path) != 0) {
2475 err = got_error_from_errno3("rename", new_fileindex_path,
2476 fileindex_path);
2477 unlink(new_fileindex_path);
2481 * Sleep for a short amount of time to ensure that files modified after
2482 * this program exits have a different time stamp from the one which
2483 * was recorded in the file index.
2485 timeout.tv_sec = 0;
2486 timeout.tv_nsec = 1;
2487 nanosleep(&timeout, NULL);
2488 done:
2489 if (new_index)
2490 fclose(new_index);
2491 free(new_fileindex_path);
2492 return err;
2495 static const struct got_error *
2496 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2497 struct got_object_id **tree_id, const char *wt_relpath,
2498 struct got_worktree *worktree, struct got_repository *repo)
2500 const struct got_error *err = NULL;
2501 struct got_object_id *id = NULL;
2502 char *in_repo_path = NULL;
2503 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2505 *entry_type = GOT_OBJ_TYPE_ANY;
2506 *tree_relpath = NULL;
2507 *tree_id = NULL;
2509 if (wt_relpath[0] == '\0') {
2510 /* Check out all files within the work tree. */
2511 *entry_type = GOT_OBJ_TYPE_TREE;
2512 *tree_relpath = strdup("");
2513 if (*tree_relpath == NULL) {
2514 err = got_error_from_errno("strdup");
2515 goto done;
2517 err = got_object_id_by_path(tree_id, repo,
2518 worktree->base_commit_id, worktree->path_prefix);
2519 if (err)
2520 goto done;
2521 return NULL;
2524 /* Check out a subset of files in the work tree. */
2526 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2527 is_root_wt ? "" : "/", wt_relpath) == -1) {
2528 err = got_error_from_errno("asprintf");
2529 goto done;
2532 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2533 in_repo_path);
2534 if (err)
2535 goto done;
2537 free(in_repo_path);
2538 in_repo_path = NULL;
2540 err = got_object_get_type(entry_type, repo, id);
2541 if (err)
2542 goto done;
2544 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2545 /* Check out a single file. */
2546 if (strchr(wt_relpath, '/') == NULL) {
2547 /* Check out a single file in work tree's root dir. */
2548 in_repo_path = strdup(worktree->path_prefix);
2549 if (in_repo_path == NULL) {
2550 err = got_error_from_errno("strdup");
2551 goto done;
2553 *tree_relpath = strdup("");
2554 if (*tree_relpath == NULL) {
2555 err = got_error_from_errno("strdup");
2556 goto done;
2558 } else {
2559 /* Check out a single file in a subdirectory. */
2560 err = got_path_dirname(tree_relpath, wt_relpath);
2561 if (err)
2562 return err;
2563 if (asprintf(&in_repo_path, "%s%s%s",
2564 worktree->path_prefix, is_root_wt ? "" : "/",
2565 *tree_relpath) == -1) {
2566 err = got_error_from_errno("asprintf");
2567 goto done;
2570 err = got_object_id_by_path(tree_id, repo,
2571 worktree->base_commit_id, in_repo_path);
2572 } else {
2573 /* Check out all files within a subdirectory. */
2574 *tree_id = got_object_id_dup(id);
2575 if (*tree_id == NULL) {
2576 err = got_error_from_errno("got_object_id_dup");
2577 goto done;
2579 *tree_relpath = strdup(wt_relpath);
2580 if (*tree_relpath == NULL) {
2581 err = got_error_from_errno("strdup");
2582 goto done;
2585 done:
2586 free(id);
2587 free(in_repo_path);
2588 if (err) {
2589 *entry_type = GOT_OBJ_TYPE_ANY;
2590 free(*tree_relpath);
2591 *tree_relpath = NULL;
2592 free(*tree_id);
2593 *tree_id = NULL;
2595 return err;
2598 static const struct got_error *
2599 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2600 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2601 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2602 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2604 const struct got_error *err = NULL;
2605 struct got_commit_object *commit = NULL;
2606 struct got_tree_object *tree = NULL;
2607 struct got_fileindex_diff_tree_cb diff_cb;
2608 struct diff_cb_arg arg;
2610 err = ref_base_commit(worktree, repo);
2611 if (err) {
2612 if (!(err->code == GOT_ERR_ERRNO &&
2613 (errno == EACCES || errno == EROFS)))
2614 goto done;
2615 err = (*progress_cb)(progress_arg,
2616 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2617 if (err)
2618 return err;
2621 err = got_object_open_as_commit(&commit, repo,
2622 worktree->base_commit_id);
2623 if (err)
2624 goto done;
2626 err = got_object_open_as_tree(&tree, repo, tree_id);
2627 if (err)
2628 goto done;
2630 if (entry_name &&
2631 got_object_tree_find_entry(tree, entry_name) == NULL) {
2632 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2633 goto done;
2636 diff_cb.diff_old_new = diff_old_new;
2637 diff_cb.diff_old = diff_old;
2638 diff_cb.diff_new = diff_new;
2639 arg.fileindex = fileindex;
2640 arg.worktree = worktree;
2641 arg.repo = repo;
2642 arg.progress_cb = progress_cb;
2643 arg.progress_arg = progress_arg;
2644 arg.cancel_cb = cancel_cb;
2645 arg.cancel_arg = cancel_arg;
2646 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2647 entry_name, repo, &diff_cb, &arg);
2648 done:
2649 if (tree)
2650 got_object_tree_close(tree);
2651 if (commit)
2652 got_object_commit_close(commit);
2653 return err;
2656 const struct got_error *
2657 got_worktree_checkout_files(struct got_worktree *worktree,
2658 struct got_pathlist_head *paths, struct got_repository *repo,
2659 got_worktree_checkout_cb progress_cb, void *progress_arg,
2660 got_cancel_cb cancel_cb, void *cancel_arg)
2662 const struct got_error *err = NULL, *sync_err, *unlockerr;
2663 struct got_commit_object *commit = NULL;
2664 struct got_tree_object *tree = NULL;
2665 struct got_fileindex *fileindex = NULL;
2666 char *fileindex_path = NULL;
2667 struct got_pathlist_entry *pe;
2668 struct tree_path_data {
2669 SIMPLEQ_ENTRY(tree_path_data) entry;
2670 struct got_object_id *tree_id;
2671 int entry_type;
2672 char *relpath;
2673 char *entry_name;
2674 } *tpd = NULL;
2675 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2677 SIMPLEQ_INIT(&tree_paths);
2679 err = lock_worktree(worktree, LOCK_EX);
2680 if (err)
2681 return err;
2683 /* Map all specified paths to in-repository trees. */
2684 TAILQ_FOREACH(pe, paths, entry) {
2685 tpd = malloc(sizeof(*tpd));
2686 if (tpd == NULL) {
2687 err = got_error_from_errno("malloc");
2688 goto done;
2691 err = find_tree_entry_for_checkout(&tpd->entry_type,
2692 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2693 if (err) {
2694 free(tpd);
2695 goto done;
2698 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2699 err = got_path_basename(&tpd->entry_name, pe->path);
2700 if (err) {
2701 free(tpd->relpath);
2702 free(tpd->tree_id);
2703 free(tpd);
2704 goto done;
2706 } else
2707 tpd->entry_name = NULL;
2709 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2713 * Read the file index.
2714 * Checking out files is supposed to be an idempotent operation.
2715 * If the on-disk file index is incomplete we will try to complete it.
2717 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2718 if (err)
2719 goto done;
2721 tpd = SIMPLEQ_FIRST(&tree_paths);
2722 TAILQ_FOREACH(pe, paths, entry) {
2723 struct bump_base_commit_id_arg bbc_arg;
2725 err = checkout_files(worktree, fileindex, tpd->relpath,
2726 tpd->tree_id, tpd->entry_name, repo,
2727 progress_cb, progress_arg, cancel_cb, cancel_arg);
2728 if (err)
2729 break;
2731 bbc_arg.base_commit_id = worktree->base_commit_id;
2732 bbc_arg.entry_name = tpd->entry_name;
2733 bbc_arg.path = pe->path;
2734 bbc_arg.path_len = pe->path_len;
2735 bbc_arg.progress_cb = progress_cb;
2736 bbc_arg.progress_arg = progress_arg;
2737 err = got_fileindex_for_each_entry_safe(fileindex,
2738 bump_base_commit_id, &bbc_arg);
2739 if (err)
2740 break;
2742 tpd = SIMPLEQ_NEXT(tpd, entry);
2744 sync_err = sync_fileindex(fileindex, fileindex_path);
2745 if (sync_err && err == NULL)
2746 err = sync_err;
2747 done:
2748 free(fileindex_path);
2749 if (tree)
2750 got_object_tree_close(tree);
2751 if (commit)
2752 got_object_commit_close(commit);
2753 if (fileindex)
2754 got_fileindex_free(fileindex);
2755 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2756 tpd = SIMPLEQ_FIRST(&tree_paths);
2757 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2758 free(tpd->relpath);
2759 free(tpd->tree_id);
2760 free(tpd);
2762 unlockerr = lock_worktree(worktree, LOCK_SH);
2763 if (unlockerr && err == NULL)
2764 err = unlockerr;
2765 return err;
2768 struct merge_file_cb_arg {
2769 struct got_worktree *worktree;
2770 struct got_fileindex *fileindex;
2771 got_worktree_checkout_cb progress_cb;
2772 void *progress_arg;
2773 got_cancel_cb cancel_cb;
2774 void *cancel_arg;
2775 const char *label_orig;
2776 struct got_object_id *commit_id2;
2779 static const struct got_error *
2780 merge_file_cb(void *arg, struct got_blob_object *blob1,
2781 struct got_blob_object *blob2, struct got_object_id *id1,
2782 struct got_object_id *id2, const char *path1, const char *path2,
2783 mode_t mode1, mode_t mode2, struct got_repository *repo)
2785 static const struct got_error *err = NULL;
2786 struct merge_file_cb_arg *a = arg;
2787 struct got_fileindex_entry *ie;
2788 char *ondisk_path = NULL;
2789 struct stat sb;
2790 unsigned char status;
2791 int local_changes_subsumed;
2793 if (blob1 && blob2) {
2794 ie = got_fileindex_entry_get(a->fileindex, path2,
2795 strlen(path2));
2796 if (ie == NULL)
2797 return (*a->progress_cb)(a->progress_arg,
2798 GOT_STATUS_MISSING, path2);
2800 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2801 path2) == -1)
2802 return got_error_from_errno("asprintf");
2804 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2805 repo);
2806 if (err)
2807 goto done;
2809 if (status == GOT_STATUS_DELETE) {
2810 err = (*a->progress_cb)(a->progress_arg,
2811 GOT_STATUS_MERGE, path2);
2812 goto done;
2814 if (status != GOT_STATUS_NO_CHANGE &&
2815 status != GOT_STATUS_MODIFY &&
2816 status != GOT_STATUS_CONFLICT &&
2817 status != GOT_STATUS_ADD) {
2818 err = (*a->progress_cb)(a->progress_arg, status, path2);
2819 goto done;
2822 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2823 char *link_target2;
2824 err = got_object_blob_read_to_str(&link_target2, blob2);
2825 if (err)
2826 goto done;
2827 err = merge_symlink(a->worktree, blob1, ondisk_path,
2828 path2, a->label_orig, link_target2, a->commit_id2,
2829 repo, a->progress_cb, a->progress_arg);
2830 free(link_target2);
2831 } else {
2832 err = merge_blob(&local_changes_subsumed, a->worktree,
2833 blob1, ondisk_path, path2, sb.st_mode,
2834 a->label_orig, blob2, a->commit_id2, repo,
2835 a->progress_cb, a->progress_arg);
2837 } else if (blob1) {
2838 ie = got_fileindex_entry_get(a->fileindex, path1,
2839 strlen(path1));
2840 if (ie == NULL)
2841 return (*a->progress_cb)(a->progress_arg,
2842 GOT_STATUS_MISSING, path1);
2844 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2845 path1) == -1)
2846 return got_error_from_errno("asprintf");
2848 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2849 repo);
2850 if (err)
2851 goto done;
2853 switch (status) {
2854 case GOT_STATUS_NO_CHANGE:
2855 err = (*a->progress_cb)(a->progress_arg,
2856 GOT_STATUS_DELETE, path1);
2857 if (err)
2858 goto done;
2859 err = remove_ondisk_file(a->worktree->root_path, path1);
2860 if (err)
2861 goto done;
2862 if (ie)
2863 got_fileindex_entry_mark_deleted_from_disk(ie);
2864 break;
2865 case GOT_STATUS_DELETE:
2866 case GOT_STATUS_MISSING:
2867 err = (*a->progress_cb)(a->progress_arg,
2868 GOT_STATUS_DELETE, path1);
2869 if (err)
2870 goto done;
2871 if (ie)
2872 got_fileindex_entry_mark_deleted_from_disk(ie);
2873 break;
2874 case GOT_STATUS_ADD: {
2875 struct got_object_id *id;
2876 FILE *blob1_f;
2878 * Delete the added file only if its content already
2879 * exists in the repository.
2881 err = got_object_blob_file_create(&id, &blob1_f, path1);
2882 if (err)
2883 goto done;
2884 if (got_object_id_cmp(id, id1) == 0) {
2885 err = (*a->progress_cb)(a->progress_arg,
2886 GOT_STATUS_DELETE, path1);
2887 if (err)
2888 goto done;
2889 err = remove_ondisk_file(a->worktree->root_path,
2890 path1);
2891 if (err)
2892 goto done;
2893 if (ie)
2894 got_fileindex_entry_remove(a->fileindex,
2895 ie);
2896 } else {
2897 err = (*a->progress_cb)(a->progress_arg,
2898 GOT_STATUS_CANNOT_DELETE, path1);
2900 if (fclose(blob1_f) == EOF && err == NULL)
2901 err = got_error_from_errno("fclose");
2902 free(id);
2903 if (err)
2904 goto done;
2905 break;
2907 case GOT_STATUS_MODIFY:
2908 case GOT_STATUS_CONFLICT:
2909 err = (*a->progress_cb)(a->progress_arg,
2910 GOT_STATUS_CANNOT_DELETE, path1);
2911 if (err)
2912 goto done;
2913 break;
2914 case GOT_STATUS_OBSTRUCTED:
2915 err = (*a->progress_cb)(a->progress_arg, status, path1);
2916 if (err)
2917 goto done;
2918 break;
2919 default:
2920 break;
2922 } else if (blob2) {
2923 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2924 path2) == -1)
2925 return got_error_from_errno("asprintf");
2926 ie = got_fileindex_entry_get(a->fileindex, path2,
2927 strlen(path2));
2928 if (ie) {
2929 err = get_file_status(&status, &sb, ie, ondisk_path,
2930 -1, NULL, repo);
2931 if (err)
2932 goto done;
2933 if (status != GOT_STATUS_NO_CHANGE &&
2934 status != GOT_STATUS_MODIFY &&
2935 status != GOT_STATUS_CONFLICT &&
2936 status != GOT_STATUS_ADD) {
2937 err = (*a->progress_cb)(a->progress_arg,
2938 status, path2);
2939 goto done;
2941 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2942 char *link_target2;
2943 err = got_object_blob_read_to_str(&link_target2,
2944 blob2);
2945 if (err)
2946 goto done;
2947 err = merge_symlink(a->worktree, NULL,
2948 ondisk_path, path2, a->label_orig,
2949 link_target2, a->commit_id2, repo,
2950 a->progress_cb, a->progress_arg);
2951 free(link_target2);
2952 } else if (S_ISREG(sb.st_mode)) {
2953 err = merge_blob(&local_changes_subsumed,
2954 a->worktree, NULL, ondisk_path, path2,
2955 sb.st_mode, a->label_orig, blob2,
2956 a->commit_id2, repo, a->progress_cb,
2957 a->progress_arg);
2958 } else {
2959 err = got_error_path(ondisk_path,
2960 GOT_ERR_FILE_OBSTRUCTED);
2962 if (err)
2963 goto done;
2964 if (status == GOT_STATUS_DELETE) {
2965 err = got_fileindex_entry_update(ie,
2966 a->worktree->root_fd, path2, blob2->id.sha1,
2967 a->worktree->base_commit_id->sha1, 0);
2968 if (err)
2969 goto done;
2971 } else {
2972 int is_bad_symlink = 0;
2973 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2974 if (S_ISLNK(mode2)) {
2975 err = install_symlink(&is_bad_symlink,
2976 a->worktree, ondisk_path, path2, blob2, 0,
2977 0, 1, repo, a->progress_cb, a->progress_arg);
2978 } else {
2979 err = install_blob(a->worktree, ondisk_path, path2,
2980 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2981 a->progress_cb, a->progress_arg);
2983 if (err)
2984 goto done;
2985 err = got_fileindex_entry_alloc(&ie, path2);
2986 if (err)
2987 goto done;
2988 err = got_fileindex_entry_update(ie,
2989 a->worktree->root_fd, path2, NULL, NULL, 1);
2990 if (err) {
2991 got_fileindex_entry_free(ie);
2992 goto done;
2994 err = got_fileindex_entry_add(a->fileindex, ie);
2995 if (err) {
2996 got_fileindex_entry_free(ie);
2997 goto done;
2999 if (is_bad_symlink) {
3000 got_fileindex_entry_filetype_set(ie,
3001 GOT_FILEIDX_MODE_BAD_SYMLINK);
3005 done:
3006 free(ondisk_path);
3007 return err;
3010 struct check_merge_ok_arg {
3011 struct got_worktree *worktree;
3012 struct got_repository *repo;
3015 static const struct got_error *
3016 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3018 const struct got_error *err = NULL;
3019 struct check_merge_ok_arg *a = arg;
3020 unsigned char status;
3021 struct stat sb;
3022 char *ondisk_path;
3024 /* Reject merges into a work tree with mixed base commits. */
3025 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3026 SHA1_DIGEST_LENGTH))
3027 return got_error(GOT_ERR_MIXED_COMMITS);
3029 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3030 == -1)
3031 return got_error_from_errno("asprintf");
3033 /* Reject merges into a work tree with conflicted files. */
3034 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3035 if (err)
3036 return err;
3037 if (status == GOT_STATUS_CONFLICT)
3038 return got_error(GOT_ERR_CONFLICTS);
3040 return NULL;
3043 static const struct got_error *
3044 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3045 const char *fileindex_path, struct got_object_id *commit_id1,
3046 struct got_object_id *commit_id2, struct got_repository *repo,
3047 got_worktree_checkout_cb progress_cb, void *progress_arg,
3048 got_cancel_cb cancel_cb, void *cancel_arg)
3050 const struct got_error *err = NULL, *sync_err;
3051 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3052 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3053 struct merge_file_cb_arg arg;
3054 char *label_orig = NULL;
3056 if (commit_id1) {
3057 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3058 worktree->path_prefix);
3059 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3060 goto done;
3062 if (tree_id1) {
3063 char *id_str;
3065 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3066 if (err)
3067 goto done;
3069 err = got_object_id_str(&id_str, commit_id1);
3070 if (err)
3071 goto done;
3073 if (asprintf(&label_orig, "%s: commit %s",
3074 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3075 err = got_error_from_errno("asprintf");
3076 free(id_str);
3077 goto done;
3079 free(id_str);
3082 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3083 worktree->path_prefix);
3084 if (err)
3085 goto done;
3087 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3088 if (err)
3089 goto done;
3091 arg.worktree = worktree;
3092 arg.fileindex = fileindex;
3093 arg.progress_cb = progress_cb;
3094 arg.progress_arg = progress_arg;
3095 arg.cancel_cb = cancel_cb;
3096 arg.cancel_arg = cancel_arg;
3097 arg.label_orig = label_orig;
3098 arg.commit_id2 = commit_id2;
3099 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3100 sync_err = sync_fileindex(fileindex, fileindex_path);
3101 if (sync_err && err == NULL)
3102 err = sync_err;
3103 done:
3104 if (tree1)
3105 got_object_tree_close(tree1);
3106 if (tree2)
3107 got_object_tree_close(tree2);
3108 free(label_orig);
3109 return err;
3112 const struct got_error *
3113 got_worktree_merge_files(struct got_worktree *worktree,
3114 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3115 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3116 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3118 const struct got_error *err, *unlockerr;
3119 char *fileindex_path = NULL;
3120 struct got_fileindex *fileindex = NULL;
3121 struct check_merge_ok_arg mok_arg;
3123 err = lock_worktree(worktree, LOCK_EX);
3124 if (err)
3125 return err;
3127 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3128 if (err)
3129 goto done;
3131 mok_arg.worktree = worktree;
3132 mok_arg.repo = repo;
3133 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3134 &mok_arg);
3135 if (err)
3136 goto done;
3138 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3139 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3140 done:
3141 if (fileindex)
3142 got_fileindex_free(fileindex);
3143 free(fileindex_path);
3144 unlockerr = lock_worktree(worktree, LOCK_SH);
3145 if (unlockerr && err == NULL)
3146 err = unlockerr;
3147 return err;
3150 struct diff_dir_cb_arg {
3151 struct got_fileindex *fileindex;
3152 struct got_worktree *worktree;
3153 const char *status_path;
3154 size_t status_path_len;
3155 struct got_repository *repo;
3156 got_worktree_status_cb status_cb;
3157 void *status_arg;
3158 got_cancel_cb cancel_cb;
3159 void *cancel_arg;
3160 /* A pathlist containing per-directory pathlists of ignore patterns. */
3161 struct got_pathlist_head ignores;
3162 int report_unchanged;
3163 int no_ignores;
3166 static const struct got_error *
3167 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3168 int dirfd, const char *de_name,
3169 got_worktree_status_cb status_cb, void *status_arg,
3170 struct got_repository *repo, int report_unchanged)
3172 const struct got_error *err = NULL;
3173 unsigned char status = GOT_STATUS_NO_CHANGE;
3174 unsigned char staged_status = get_staged_status(ie);
3175 struct stat sb;
3176 struct got_object_id blob_id, commit_id, staged_blob_id;
3177 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3178 struct got_object_id *staged_blob_idp = NULL;
3180 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3181 if (err)
3182 return err;
3184 if (status == GOT_STATUS_NO_CHANGE &&
3185 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3186 return NULL;
3188 if (got_fileindex_entry_has_blob(ie)) {
3189 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3190 blob_idp = &blob_id;
3192 if (got_fileindex_entry_has_commit(ie)) {
3193 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3194 commit_idp = &commit_id;
3196 if (staged_status == GOT_STATUS_ADD ||
3197 staged_status == GOT_STATUS_MODIFY) {
3198 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3199 SHA1_DIGEST_LENGTH);
3200 staged_blob_idp = &staged_blob_id;
3203 return (*status_cb)(status_arg, status, staged_status,
3204 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3207 static const struct got_error *
3208 status_old_new(void *arg, struct got_fileindex_entry *ie,
3209 struct dirent *de, const char *parent_path, int dirfd)
3211 const struct got_error *err = NULL;
3212 struct diff_dir_cb_arg *a = arg;
3213 char *abspath;
3215 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3216 return got_error(GOT_ERR_CANCELLED);
3218 if (got_path_cmp(parent_path, a->status_path,
3219 strlen(parent_path), a->status_path_len) != 0 &&
3220 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3221 return NULL;
3223 if (parent_path[0]) {
3224 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3225 parent_path, de->d_name) == -1)
3226 return got_error_from_errno("asprintf");
3227 } else {
3228 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3229 de->d_name) == -1)
3230 return got_error_from_errno("asprintf");
3233 err = report_file_status(ie, abspath, dirfd, de->d_name,
3234 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3235 free(abspath);
3236 return err;
3239 static const struct got_error *
3240 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3242 struct diff_dir_cb_arg *a = arg;
3243 struct got_object_id blob_id, commit_id;
3244 unsigned char status;
3246 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3247 return got_error(GOT_ERR_CANCELLED);
3249 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3250 return NULL;
3252 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3253 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3254 if (got_fileindex_entry_has_file_on_disk(ie))
3255 status = GOT_STATUS_MISSING;
3256 else
3257 status = GOT_STATUS_DELETE;
3258 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3259 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3262 void
3263 free_ignorelist(struct got_pathlist_head *ignorelist)
3265 struct got_pathlist_entry *pe;
3267 TAILQ_FOREACH(pe, ignorelist, entry)
3268 free((char *)pe->path);
3269 got_pathlist_free(ignorelist);
3272 void
3273 free_ignores(struct got_pathlist_head *ignores)
3275 struct got_pathlist_entry *pe;
3277 TAILQ_FOREACH(pe, ignores, entry) {
3278 struct got_pathlist_head *ignorelist = pe->data;
3279 free_ignorelist(ignorelist);
3280 free((char *)pe->path);
3282 got_pathlist_free(ignores);
3285 static const struct got_error *
3286 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3288 const struct got_error *err = NULL;
3289 struct got_pathlist_entry *pe = NULL;
3290 struct got_pathlist_head *ignorelist;
3291 char *line = NULL, *pattern, *dirpath = NULL;
3292 size_t linesize = 0;
3293 ssize_t linelen;
3295 ignorelist = calloc(1, sizeof(*ignorelist));
3296 if (ignorelist == NULL)
3297 return got_error_from_errno("calloc");
3298 TAILQ_INIT(ignorelist);
3300 while ((linelen = getline(&line, &linesize, f)) != -1) {
3301 if (linelen > 0 && line[linelen - 1] == '\n')
3302 line[linelen - 1] = '\0';
3304 /* Git's ignores may contain comments. */
3305 if (line[0] == '#')
3306 continue;
3308 /* Git's negated patterns are not (yet?) supported. */
3309 if (line[0] == '!')
3310 continue;
3312 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3313 line) == -1) {
3314 err = got_error_from_errno("asprintf");
3315 goto done;
3317 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3318 if (err)
3319 goto done;
3321 if (ferror(f)) {
3322 err = got_error_from_errno("getline");
3323 goto done;
3326 dirpath = strdup(path);
3327 if (dirpath == NULL) {
3328 err = got_error_from_errno("strdup");
3329 goto done;
3331 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3332 done:
3333 free(line);
3334 if (err || pe == NULL) {
3335 free(dirpath);
3336 free_ignorelist(ignorelist);
3338 return err;
3341 int
3342 match_ignores(struct got_pathlist_head *ignores, const char *path)
3344 struct got_pathlist_entry *pe;
3346 /* Handle patterns which match in all directories. */
3347 TAILQ_FOREACH(pe, ignores, entry) {
3348 struct got_pathlist_head *ignorelist = pe->data;
3349 struct got_pathlist_entry *pi;
3351 TAILQ_FOREACH(pi, ignorelist, entry) {
3352 const char *p, *pattern = pi->path;
3354 if (strncmp(pattern, "**/", 3) != 0)
3355 continue;
3356 pattern += 3;
3357 p = path;
3358 while (*p) {
3359 if (fnmatch(pattern, p,
3360 FNM_PATHNAME | FNM_LEADING_DIR)) {
3361 /* Retry in next directory. */
3362 while (*p && *p != '/')
3363 p++;
3364 while (*p == '/')
3365 p++;
3366 continue;
3368 return 1;
3374 * The ignores pathlist contains ignore lists from children before
3375 * parents, so we can find the most specific ignorelist by walking
3376 * ignores backwards.
3378 pe = TAILQ_LAST(ignores, got_pathlist_head);
3379 while (pe) {
3380 if (got_path_is_child(path, pe->path, pe->path_len)) {
3381 struct got_pathlist_head *ignorelist = pe->data;
3382 struct got_pathlist_entry *pi;
3383 TAILQ_FOREACH(pi, ignorelist, entry) {
3384 const char *pattern = pi->path;
3385 int flags = FNM_LEADING_DIR;
3386 if (strstr(pattern, "/**/") == NULL)
3387 flags |= FNM_PATHNAME;
3388 if (fnmatch(pattern, path, flags))
3389 continue;
3390 return 1;
3393 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3396 return 0;
3399 static const struct got_error *
3400 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3401 const char *path, int dirfd, const char *ignores_filename)
3403 const struct got_error *err = NULL;
3404 char *ignorespath;
3405 int fd = -1;
3406 FILE *ignoresfile = NULL;
3408 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3409 path[0] ? "/" : "", ignores_filename) == -1)
3410 return got_error_from_errno("asprintf");
3412 if (dirfd != -1) {
3413 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3414 if (fd == -1) {
3415 if (errno != ENOENT && errno != EACCES)
3416 err = got_error_from_errno2("openat",
3417 ignorespath);
3418 } else {
3419 ignoresfile = fdopen(fd, "r");
3420 if (ignoresfile == NULL)
3421 err = got_error_from_errno2("fdopen",
3422 ignorespath);
3423 else {
3424 fd = -1;
3425 err = read_ignores(ignores, path, ignoresfile);
3428 } else {
3429 ignoresfile = fopen(ignorespath, "r");
3430 if (ignoresfile == NULL) {
3431 if (errno != ENOENT && errno != EACCES)
3432 err = got_error_from_errno2("fopen",
3433 ignorespath);
3434 } else
3435 err = read_ignores(ignores, path, ignoresfile);
3438 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3439 err = got_error_from_errno2("fclose", path);
3440 if (fd != -1 && close(fd) == -1 && err == NULL)
3441 err = got_error_from_errno2("close", path);
3442 free(ignorespath);
3443 return err;
3446 static const struct got_error *
3447 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3449 const struct got_error *err = NULL;
3450 struct diff_dir_cb_arg *a = arg;
3451 char *path = NULL;
3453 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3454 return got_error(GOT_ERR_CANCELLED);
3456 if (parent_path[0]) {
3457 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3458 return got_error_from_errno("asprintf");
3459 } else {
3460 path = de->d_name;
3463 if (de->d_type != DT_DIR &&
3464 got_path_is_child(path, a->status_path, a->status_path_len)
3465 && !match_ignores(&a->ignores, path))
3466 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3467 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3468 if (parent_path[0])
3469 free(path);
3470 return err;
3473 static const struct got_error *
3474 status_traverse(void *arg, const char *path, int dirfd)
3476 const struct got_error *err = NULL;
3477 struct diff_dir_cb_arg *a = arg;
3479 if (a->no_ignores)
3480 return NULL;
3482 err = add_ignores(&a->ignores, a->worktree->root_path,
3483 path, dirfd, ".cvsignore");
3484 if (err)
3485 return err;
3487 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3488 dirfd, ".gitignore");
3490 return err;
3493 static const struct got_error *
3494 report_single_file_status(const char *path, const char *ondisk_path,
3495 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3496 void *status_arg, struct got_repository *repo, int report_unchanged)
3498 struct got_fileindex_entry *ie;
3499 struct stat sb;
3501 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3502 if (ie)
3503 return report_file_status(ie, ondisk_path, -1, NULL,
3504 status_cb, status_arg, repo, report_unchanged);
3506 if (lstat(ondisk_path, &sb) == -1) {
3507 if (errno != ENOENT)
3508 return got_error_from_errno2("lstat", ondisk_path);
3509 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3510 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3511 return NULL;
3514 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3515 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3516 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3518 return NULL;
3521 static const struct got_error *
3522 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3523 const char *root_path, const char *path)
3525 const struct got_error *err;
3526 char *parent_path, *next_parent_path = NULL;
3528 err = add_ignores(ignores, root_path, "", -1,
3529 ".cvsignore");
3530 if (err)
3531 return err;
3533 err = add_ignores(ignores, root_path, "", -1,
3534 ".gitignore");
3535 if (err)
3536 return err;
3538 err = got_path_dirname(&parent_path, path);
3539 if (err) {
3540 if (err->code == GOT_ERR_BAD_PATH)
3541 return NULL; /* cannot traverse parent */
3542 return err;
3544 for (;;) {
3545 err = add_ignores(ignores, root_path, parent_path, -1,
3546 ".cvsignore");
3547 if (err)
3548 break;
3549 err = add_ignores(ignores, root_path, parent_path, -1,
3550 ".gitignore");
3551 if (err)
3552 break;
3553 err = got_path_dirname(&next_parent_path, parent_path);
3554 if (err) {
3555 if (err->code == GOT_ERR_BAD_PATH)
3556 err = NULL; /* traversed everything */
3557 break;
3559 free(parent_path);
3560 parent_path = next_parent_path;
3561 next_parent_path = NULL;
3564 free(parent_path);
3565 free(next_parent_path);
3566 return err;
3569 static const struct got_error *
3570 worktree_status(struct got_worktree *worktree, const char *path,
3571 struct got_fileindex *fileindex, struct got_repository *repo,
3572 got_worktree_status_cb status_cb, void *status_arg,
3573 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3574 int report_unchanged)
3576 const struct got_error *err = NULL;
3577 int fd = -1;
3578 struct got_fileindex_diff_dir_cb fdiff_cb;
3579 struct diff_dir_cb_arg arg;
3580 char *ondisk_path = NULL;
3582 TAILQ_INIT(&arg.ignores);
3584 if (asprintf(&ondisk_path, "%s%s%s",
3585 worktree->root_path, path[0] ? "/" : "", path) == -1)
3586 return got_error_from_errno("asprintf");
3588 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3589 if (fd == -1) {
3590 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3591 errno != ELOOP)
3592 err = got_error_from_errno2("open", ondisk_path);
3593 else
3594 err = report_single_file_status(path, ondisk_path,
3595 fileindex, status_cb, status_arg, repo,
3596 report_unchanged);
3597 } else {
3598 fdiff_cb.diff_old_new = status_old_new;
3599 fdiff_cb.diff_old = status_old;
3600 fdiff_cb.diff_new = status_new;
3601 fdiff_cb.diff_traverse = status_traverse;
3602 arg.fileindex = fileindex;
3603 arg.worktree = worktree;
3604 arg.status_path = path;
3605 arg.status_path_len = strlen(path);
3606 arg.repo = repo;
3607 arg.status_cb = status_cb;
3608 arg.status_arg = status_arg;
3609 arg.cancel_cb = cancel_cb;
3610 arg.cancel_arg = cancel_arg;
3611 arg.report_unchanged = report_unchanged;
3612 arg.no_ignores = no_ignores;
3613 if (!no_ignores) {
3614 err = add_ignores_from_parent_paths(&arg.ignores,
3615 worktree->root_path, path);
3616 if (err)
3617 goto done;
3619 err = got_fileindex_diff_dir(fileindex, fd,
3620 worktree->root_path, path, repo, &fdiff_cb, &arg);
3622 done:
3623 free_ignores(&arg.ignores);
3624 if (fd != -1 && close(fd) != 0 && err == NULL)
3625 err = got_error_from_errno("close");
3626 free(ondisk_path);
3627 return err;
3630 const struct got_error *
3631 got_worktree_status(struct got_worktree *worktree,
3632 struct got_pathlist_head *paths, struct got_repository *repo,
3633 got_worktree_status_cb status_cb, void *status_arg,
3634 got_cancel_cb cancel_cb, void *cancel_arg)
3636 const struct got_error *err = NULL;
3637 char *fileindex_path = NULL;
3638 struct got_fileindex *fileindex = NULL;
3639 struct got_pathlist_entry *pe;
3641 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3642 if (err)
3643 return err;
3645 TAILQ_FOREACH(pe, paths, entry) {
3646 err = worktree_status(worktree, pe->path, fileindex, repo,
3647 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3648 if (err)
3649 break;
3651 free(fileindex_path);
3652 got_fileindex_free(fileindex);
3653 return err;
3656 const struct got_error *
3657 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3658 const char *arg)
3660 const struct got_error *err = NULL;
3661 char *resolved = NULL, *cwd = NULL, *path = NULL;
3662 size_t len;
3663 struct stat sb;
3664 char *abspath = NULL;
3665 char canonpath[PATH_MAX];
3667 *wt_path = NULL;
3669 cwd = getcwd(NULL, 0);
3670 if (cwd == NULL)
3671 return got_error_from_errno("getcwd");
3673 if (lstat(arg, &sb) == -1) {
3674 if (errno != ENOENT) {
3675 err = got_error_from_errno2("lstat", arg);
3676 goto done;
3678 sb.st_mode = 0;
3680 if (S_ISLNK(sb.st_mode)) {
3682 * We cannot use realpath(3) with symlinks since we want to
3683 * operate on the symlink itself.
3684 * But we can make the path absolute, assuming it is relative
3685 * to the current working directory, and then canonicalize it.
3687 if (!got_path_is_absolute(arg)) {
3688 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3689 err = got_error_from_errno("asprintf");
3690 goto done;
3694 err = got_canonpath(abspath ? abspath : arg, canonpath,
3695 sizeof(canonpath));
3696 if (err)
3697 goto done;
3698 resolved = strdup(canonpath);
3699 if (resolved == NULL) {
3700 err = got_error_from_errno("strdup");
3701 goto done;
3703 } else {
3704 resolved = realpath(arg, NULL);
3705 if (resolved == NULL) {
3706 if (errno != ENOENT) {
3707 err = got_error_from_errno2("realpath", arg);
3708 goto done;
3710 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3711 err = got_error_from_errno("asprintf");
3712 goto done;
3714 err = got_canonpath(abspath, canonpath,
3715 sizeof(canonpath));
3716 if (err)
3717 goto done;
3718 resolved = strdup(canonpath);
3719 if (resolved == NULL) {
3720 err = got_error_from_errno("strdup");
3721 goto done;
3726 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3727 strlen(got_worktree_get_root_path(worktree)))) {
3728 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3729 goto done;
3732 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3733 err = got_path_skip_common_ancestor(&path,
3734 got_worktree_get_root_path(worktree), resolved);
3735 if (err)
3736 goto done;
3737 } else {
3738 path = strdup("");
3739 if (path == NULL) {
3740 err = got_error_from_errno("strdup");
3741 goto done;
3745 /* XXX status walk can't deal with trailing slash! */
3746 len = strlen(path);
3747 while (len > 0 && path[len - 1] == '/') {
3748 path[len - 1] = '\0';
3749 len--;
3751 done:
3752 free(abspath);
3753 free(resolved);
3754 free(cwd);
3755 if (err == NULL)
3756 *wt_path = path;
3757 else
3758 free(path);
3759 return err;
3762 struct schedule_addition_args {
3763 struct got_worktree *worktree;
3764 struct got_fileindex *fileindex;
3765 got_worktree_checkout_cb progress_cb;
3766 void *progress_arg;
3767 struct got_repository *repo;
3770 static const struct got_error *
3771 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3772 const char *relpath, struct got_object_id *blob_id,
3773 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3774 int dirfd, const char *de_name)
3776 struct schedule_addition_args *a = arg;
3777 const struct got_error *err = NULL;
3778 struct got_fileindex_entry *ie;
3779 struct stat sb;
3780 char *ondisk_path;
3782 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3783 relpath) == -1)
3784 return got_error_from_errno("asprintf");
3786 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3787 if (ie) {
3788 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3789 de_name, a->repo);
3790 if (err)
3791 goto done;
3792 /* Re-adding an existing entry is a no-op. */
3793 if (status == GOT_STATUS_ADD)
3794 goto done;
3795 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3796 if (err)
3797 goto done;
3800 if (status != GOT_STATUS_UNVERSIONED) {
3801 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3802 goto done;
3805 err = got_fileindex_entry_alloc(&ie, relpath);
3806 if (err)
3807 goto done;
3808 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3809 relpath, NULL, NULL, 1);
3810 if (err) {
3811 got_fileindex_entry_free(ie);
3812 goto done;
3814 err = got_fileindex_entry_add(a->fileindex, ie);
3815 if (err) {
3816 got_fileindex_entry_free(ie);
3817 goto done;
3819 done:
3820 free(ondisk_path);
3821 if (err)
3822 return err;
3823 if (status == GOT_STATUS_ADD)
3824 return NULL;
3825 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3828 const struct got_error *
3829 got_worktree_schedule_add(struct got_worktree *worktree,
3830 struct got_pathlist_head *paths,
3831 got_worktree_checkout_cb progress_cb, void *progress_arg,
3832 struct got_repository *repo, int no_ignores)
3834 struct got_fileindex *fileindex = NULL;
3835 char *fileindex_path = NULL;
3836 const struct got_error *err = NULL, *sync_err, *unlockerr;
3837 struct got_pathlist_entry *pe;
3838 struct schedule_addition_args saa;
3840 err = lock_worktree(worktree, LOCK_EX);
3841 if (err)
3842 return err;
3844 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3845 if (err)
3846 goto done;
3848 saa.worktree = worktree;
3849 saa.fileindex = fileindex;
3850 saa.progress_cb = progress_cb;
3851 saa.progress_arg = progress_arg;
3852 saa.repo = repo;
3854 TAILQ_FOREACH(pe, paths, entry) {
3855 err = worktree_status(worktree, pe->path, fileindex, repo,
3856 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3857 if (err)
3858 break;
3860 sync_err = sync_fileindex(fileindex, fileindex_path);
3861 if (sync_err && err == NULL)
3862 err = sync_err;
3863 done:
3864 free(fileindex_path);
3865 if (fileindex)
3866 got_fileindex_free(fileindex);
3867 unlockerr = lock_worktree(worktree, LOCK_SH);
3868 if (unlockerr && err == NULL)
3869 err = unlockerr;
3870 return err;
3873 struct schedule_deletion_args {
3874 struct got_worktree *worktree;
3875 struct got_fileindex *fileindex;
3876 got_worktree_delete_cb progress_cb;
3877 void *progress_arg;
3878 struct got_repository *repo;
3879 int delete_local_mods;
3880 int keep_on_disk;
3881 const char *status_codes;
3884 static const struct got_error *
3885 schedule_for_deletion(void *arg, unsigned char status,
3886 unsigned char staged_status, const char *relpath,
3887 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3888 struct got_object_id *commit_id, int dirfd, const char *de_name)
3890 struct schedule_deletion_args *a = arg;
3891 const struct got_error *err = NULL;
3892 struct got_fileindex_entry *ie = NULL;
3893 struct stat sb;
3894 char *ondisk_path;
3896 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3897 if (ie == NULL)
3898 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3900 staged_status = get_staged_status(ie);
3901 if (staged_status != GOT_STATUS_NO_CHANGE) {
3902 if (staged_status == GOT_STATUS_DELETE)
3903 return NULL;
3904 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3907 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3908 relpath) == -1)
3909 return got_error_from_errno("asprintf");
3911 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3912 a->repo);
3913 if (err)
3914 goto done;
3916 if (a->status_codes) {
3917 size_t ncodes = strlen(a->status_codes);
3918 int i;
3919 for (i = 0; i < ncodes ; i++) {
3920 if (status == a->status_codes[i])
3921 break;
3923 if (i == ncodes) {
3924 /* Do not delete files in non-matching status. */
3925 free(ondisk_path);
3926 return NULL;
3928 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3929 a->status_codes[i] != GOT_STATUS_MISSING) {
3930 static char msg[64];
3931 snprintf(msg, sizeof(msg),
3932 "invalid status code '%c'", a->status_codes[i]);
3933 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3934 goto done;
3938 if (status != GOT_STATUS_NO_CHANGE) {
3939 if (status == GOT_STATUS_DELETE)
3940 goto done;
3941 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3942 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3943 goto done;
3945 if (status != GOT_STATUS_MODIFY &&
3946 status != GOT_STATUS_MISSING) {
3947 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3948 goto done;
3952 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3953 size_t root_len;
3955 if (dirfd != -1) {
3956 if (unlinkat(dirfd, de_name, 0) != 0) {
3957 err = got_error_from_errno2("unlinkat",
3958 ondisk_path);
3959 goto done;
3961 } else if (unlink(ondisk_path) != 0) {
3962 err = got_error_from_errno2("unlink", ondisk_path);
3963 goto done;
3966 root_len = strlen(a->worktree->root_path);
3967 do {
3968 char *parent;
3969 err = got_path_dirname(&parent, ondisk_path);
3970 if (err)
3971 goto done;
3972 free(ondisk_path);
3973 ondisk_path = parent;
3974 if (rmdir(ondisk_path) == -1) {
3975 if (errno != ENOTEMPTY)
3976 err = got_error_from_errno2("rmdir",
3977 ondisk_path);
3978 break;
3980 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3981 strlen(ondisk_path), root_len) != 0);
3984 got_fileindex_entry_mark_deleted_from_disk(ie);
3985 done:
3986 free(ondisk_path);
3987 if (err)
3988 return err;
3989 if (status == GOT_STATUS_DELETE)
3990 return NULL;
3991 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3992 staged_status, relpath);
3995 const struct got_error *
3996 got_worktree_schedule_delete(struct got_worktree *worktree,
3997 struct got_pathlist_head *paths, int delete_local_mods,
3998 const char *status_codes,
3999 got_worktree_delete_cb progress_cb, void *progress_arg,
4000 struct got_repository *repo, int keep_on_disk)
4002 struct got_fileindex *fileindex = NULL;
4003 char *fileindex_path = NULL;
4004 const struct got_error *err = NULL, *sync_err, *unlockerr;
4005 struct got_pathlist_entry *pe;
4006 struct schedule_deletion_args sda;
4008 err = lock_worktree(worktree, LOCK_EX);
4009 if (err)
4010 return err;
4012 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4013 if (err)
4014 goto done;
4016 sda.worktree = worktree;
4017 sda.fileindex = fileindex;
4018 sda.progress_cb = progress_cb;
4019 sda.progress_arg = progress_arg;
4020 sda.repo = repo;
4021 sda.delete_local_mods = delete_local_mods;
4022 sda.keep_on_disk = keep_on_disk;
4023 sda.status_codes = status_codes;
4025 TAILQ_FOREACH(pe, paths, entry) {
4026 err = worktree_status(worktree, pe->path, fileindex, repo,
4027 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4028 if (err)
4029 break;
4031 sync_err = sync_fileindex(fileindex, fileindex_path);
4032 if (sync_err && err == NULL)
4033 err = sync_err;
4034 done:
4035 free(fileindex_path);
4036 if (fileindex)
4037 got_fileindex_free(fileindex);
4038 unlockerr = lock_worktree(worktree, LOCK_SH);
4039 if (unlockerr && err == NULL)
4040 err = unlockerr;
4041 return err;
4044 static const struct got_error *
4045 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4047 const struct got_error *err = NULL;
4048 char *line = NULL;
4049 size_t linesize = 0, n;
4050 ssize_t linelen;
4052 linelen = getline(&line, &linesize, infile);
4053 if (linelen == -1) {
4054 if (ferror(infile)) {
4055 err = got_error_from_errno("getline");
4056 goto done;
4058 return NULL;
4060 if (outfile) {
4061 n = fwrite(line, 1, linelen, outfile);
4062 if (n != linelen) {
4063 err = got_ferror(outfile, GOT_ERR_IO);
4064 goto done;
4067 if (rejectfile) {
4068 n = fwrite(line, 1, linelen, rejectfile);
4069 if (n != linelen)
4070 err = got_ferror(outfile, GOT_ERR_IO);
4072 done:
4073 free(line);
4074 return err;
4077 static const struct got_error *
4078 skip_one_line(FILE *f)
4080 char *line = NULL;
4081 size_t linesize = 0;
4082 ssize_t linelen;
4084 linelen = getline(&line, &linesize, f);
4085 if (linelen == -1) {
4086 if (ferror(f))
4087 return got_error_from_errno("getline");
4088 return NULL;
4090 free(line);
4091 return NULL;
4094 static const struct got_error *
4095 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4096 int start_old, int end_old, int start_new, int end_new,
4097 FILE *outfile, FILE *rejectfile)
4099 const struct got_error *err;
4101 /* Copy old file's lines leading up to patch. */
4102 while (!feof(f1) && *line_cur1 < start_old) {
4103 err = copy_one_line(f1, outfile, NULL);
4104 if (err)
4105 return err;
4106 (*line_cur1)++;
4108 /* Skip new file's lines leading up to patch. */
4109 while (!feof(f2) && *line_cur2 < start_new) {
4110 if (rejectfile)
4111 err = copy_one_line(f2, NULL, rejectfile);
4112 else
4113 err = skip_one_line(f2);
4114 if (err)
4115 return err;
4116 (*line_cur2)++;
4118 /* Copy patched lines. */
4119 while (!feof(f2) && *line_cur2 <= end_new) {
4120 err = copy_one_line(f2, outfile, NULL);
4121 if (err)
4122 return err;
4123 (*line_cur2)++;
4125 /* Skip over old file's replaced lines. */
4126 while (!feof(f1) && *line_cur1 <= end_old) {
4127 if (rejectfile)
4128 err = copy_one_line(f1, NULL, rejectfile);
4129 else
4130 err = skip_one_line(f1);
4131 if (err)
4132 return err;
4133 (*line_cur1)++;
4136 return NULL;
4139 static const struct got_error *
4140 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4141 FILE *outfile, FILE *rejectfile)
4143 const struct got_error *err;
4145 if (outfile) {
4146 /* Copy old file's lines until EOF. */
4147 while (!feof(f1)) {
4148 err = copy_one_line(f1, outfile, NULL);
4149 if (err)
4150 return err;
4151 (*line_cur1)++;
4154 if (rejectfile) {
4155 /* Copy new file's lines until EOF. */
4156 while (!feof(f2)) {
4157 err = copy_one_line(f2, NULL, rejectfile);
4158 if (err)
4159 return err;
4160 (*line_cur2)++;
4164 return NULL;
4167 static const struct got_error *
4168 apply_or_reject_change(int *choice, int *nchunks_used,
4169 struct diff_result *diff_result, int n,
4170 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4171 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4172 got_worktree_patch_cb patch_cb, void *patch_arg)
4174 const struct got_error *err = NULL;
4175 struct diff_chunk_context cc = {};
4176 int start_old, end_old, start_new, end_new;
4177 FILE *hunkfile;
4178 struct diff_output_unidiff_state *diff_state;
4179 struct diff_input_info diff_info;
4180 int rc;
4182 *choice = GOT_PATCH_CHOICE_NONE;
4184 /* Get changed line numbers without context lines for copy_change(). */
4185 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4186 start_old = cc.left.start;
4187 end_old = cc.left.end;
4188 start_new = cc.right.start;
4189 end_new = cc.right.end;
4191 /* Get the same change with context lines for display. */
4192 memset(&cc, 0, sizeof(cc));
4193 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4195 memset(&diff_info, 0, sizeof(diff_info));
4196 diff_info.left_path = relpath;
4197 diff_info.right_path = relpath;
4199 diff_state = diff_output_unidiff_state_alloc();
4200 if (diff_state == NULL)
4201 return got_error_set_errno(ENOMEM,
4202 "diff_output_unidiff_state_alloc");
4204 hunkfile = got_opentemp();
4205 if (hunkfile == NULL) {
4206 err = got_error_from_errno("got_opentemp");
4207 goto done;
4210 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4211 diff_result, &cc);
4212 if (rc != DIFF_RC_OK) {
4213 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4214 goto done;
4217 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4218 err = got_ferror(hunkfile, GOT_ERR_IO);
4219 goto done;
4222 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4223 hunkfile, changeno, nchanges);
4224 if (err)
4225 goto done;
4227 switch (*choice) {
4228 case GOT_PATCH_CHOICE_YES:
4229 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4230 end_old, start_new, end_new, outfile, rejectfile);
4231 break;
4232 case GOT_PATCH_CHOICE_NO:
4233 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4234 end_old, start_new, end_new, rejectfile, outfile);
4235 break;
4236 case GOT_PATCH_CHOICE_QUIT:
4237 break;
4238 default:
4239 err = got_error(GOT_ERR_PATCH_CHOICE);
4240 break;
4242 done:
4243 diff_output_unidiff_state_free(diff_state);
4244 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4245 err = got_error_from_errno("fclose");
4246 return err;
4249 struct revert_file_args {
4250 struct got_worktree *worktree;
4251 struct got_fileindex *fileindex;
4252 got_worktree_checkout_cb progress_cb;
4253 void *progress_arg;
4254 got_worktree_patch_cb patch_cb;
4255 void *patch_arg;
4256 struct got_repository *repo;
4259 static const struct got_error *
4260 create_patched_content(char **path_outfile, int reverse_patch,
4261 struct got_object_id *blob_id, const char *path2,
4262 int dirfd2, const char *de_name2,
4263 const char *relpath, struct got_repository *repo,
4264 got_worktree_patch_cb patch_cb, void *patch_arg)
4266 const struct got_error *err, *free_err;
4267 struct got_blob_object *blob = NULL;
4268 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4269 int fd2 = -1;
4270 char link_target[PATH_MAX];
4271 ssize_t link_len = 0;
4272 char *path1 = NULL, *id_str = NULL;
4273 struct stat sb2;
4274 struct got_diffreg_result *diffreg_result = NULL;
4275 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4276 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4278 *path_outfile = NULL;
4280 err = got_object_id_str(&id_str, blob_id);
4281 if (err)
4282 return err;
4284 if (dirfd2 != -1) {
4285 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4286 if (fd2 == -1) {
4287 if (errno != ELOOP) {
4288 err = got_error_from_errno2("openat", path2);
4289 goto done;
4291 link_len = readlinkat(dirfd2, de_name2,
4292 link_target, sizeof(link_target));
4293 if (link_len == -1)
4294 return got_error_from_errno2("readlinkat", path2);
4295 sb2.st_mode = S_IFLNK;
4296 sb2.st_size = link_len;
4298 } else {
4299 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4300 if (fd2 == -1) {
4301 if (errno != ELOOP) {
4302 err = got_error_from_errno2("open", path2);
4303 goto done;
4305 link_len = readlink(path2, link_target,
4306 sizeof(link_target));
4307 if (link_len == -1)
4308 return got_error_from_errno2("readlink", path2);
4309 sb2.st_mode = S_IFLNK;
4310 sb2.st_size = link_len;
4313 if (fd2 != -1) {
4314 if (fstat(fd2, &sb2) == -1) {
4315 err = got_error_from_errno2("fstat", path2);
4316 goto done;
4319 f2 = fdopen(fd2, "r");
4320 if (f2 == NULL) {
4321 err = got_error_from_errno2("fdopen", path2);
4322 goto done;
4324 fd2 = -1;
4325 } else {
4326 size_t n;
4327 f2 = got_opentemp();
4328 if (f2 == NULL) {
4329 err = got_error_from_errno2("got_opentemp", path2);
4330 goto done;
4332 n = fwrite(link_target, 1, link_len, f2);
4333 if (n != link_len) {
4334 err = got_ferror(f2, GOT_ERR_IO);
4335 goto done;
4337 if (fflush(f2) == EOF) {
4338 err = got_error_from_errno("fflush");
4339 goto done;
4341 rewind(f2);
4344 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4345 if (err)
4346 goto done;
4348 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4349 if (err)
4350 goto done;
4352 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4353 if (err)
4354 goto done;
4356 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4357 NULL);
4358 if (err)
4359 goto done;
4361 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4362 if (err)
4363 goto done;
4365 if (fseek(f1, 0L, SEEK_SET) == -1)
4366 return got_ferror(f1, GOT_ERR_IO);
4367 if (fseek(f2, 0L, SEEK_SET) == -1)
4368 return got_ferror(f2, GOT_ERR_IO);
4370 /* Count the number of actual changes in the diff result. */
4371 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4372 struct diff_chunk_context cc = {};
4373 diff_chunk_context_load_change(&cc, &nchunks_used,
4374 diffreg_result->result, n, 0);
4375 nchanges++;
4377 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4378 int choice;
4379 err = apply_or_reject_change(&choice, &nchunks_used,
4380 diffreg_result->result, n, relpath, f1, f2,
4381 &line_cur1, &line_cur2,
4382 reverse_patch ? NULL : outfile,
4383 reverse_patch ? outfile : NULL,
4384 ++i, nchanges, patch_cb, patch_arg);
4385 if (err)
4386 goto done;
4387 if (choice == GOT_PATCH_CHOICE_YES)
4388 have_content = 1;
4389 else if (choice == GOT_PATCH_CHOICE_QUIT)
4390 break;
4392 if (have_content) {
4393 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4394 reverse_patch ? NULL : outfile,
4395 reverse_patch ? outfile : NULL);
4396 if (err)
4397 goto done;
4399 if (!S_ISLNK(sb2.st_mode)) {
4400 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4401 err = got_error_from_errno2("fchmod", path2);
4402 goto done;
4406 done:
4407 free(id_str);
4408 if (blob)
4409 got_object_blob_close(blob);
4410 free_err = got_diffreg_result_free(diffreg_result);
4411 if (err == NULL)
4412 err = free_err;
4413 if (f1 && fclose(f1) == EOF && err == NULL)
4414 err = got_error_from_errno2("fclose", path1);
4415 if (f2 && fclose(f2) == EOF && err == NULL)
4416 err = got_error_from_errno2("fclose", path2);
4417 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4418 err = got_error_from_errno2("close", path2);
4419 if (outfile && fclose(outfile) == EOF && err == NULL)
4420 err = got_error_from_errno2("fclose", *path_outfile);
4421 if (path1 && unlink(path1) == -1 && err == NULL)
4422 err = got_error_from_errno2("unlink", path1);
4423 if (err || !have_content) {
4424 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4425 err = got_error_from_errno2("unlink", *path_outfile);
4426 free(*path_outfile);
4427 *path_outfile = NULL;
4429 free(path1);
4430 return err;
4433 static const struct got_error *
4434 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4435 const char *relpath, struct got_object_id *blob_id,
4436 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4437 int dirfd, const char *de_name)
4439 struct revert_file_args *a = arg;
4440 const struct got_error *err = NULL;
4441 char *parent_path = NULL;
4442 struct got_fileindex_entry *ie;
4443 struct got_tree_object *tree = NULL;
4444 struct got_object_id *tree_id = NULL;
4445 const struct got_tree_entry *te = NULL;
4446 char *tree_path = NULL, *te_name;
4447 char *ondisk_path = NULL, *path_content = NULL;
4448 struct got_blob_object *blob = NULL;
4450 /* Reverting a staged deletion is a no-op. */
4451 if (status == GOT_STATUS_DELETE &&
4452 staged_status != GOT_STATUS_NO_CHANGE)
4453 return NULL;
4455 if (status == GOT_STATUS_UNVERSIONED)
4456 return (*a->progress_cb)(a->progress_arg,
4457 GOT_STATUS_UNVERSIONED, relpath);
4459 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4460 if (ie == NULL)
4461 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4463 /* Construct in-repository path of tree which contains this blob. */
4464 err = got_path_dirname(&parent_path, ie->path);
4465 if (err) {
4466 if (err->code != GOT_ERR_BAD_PATH)
4467 goto done;
4468 parent_path = strdup("/");
4469 if (parent_path == NULL) {
4470 err = got_error_from_errno("strdup");
4471 goto done;
4474 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4475 tree_path = strdup(parent_path);
4476 if (tree_path == NULL) {
4477 err = got_error_from_errno("strdup");
4478 goto done;
4480 } else {
4481 if (got_path_is_root_dir(parent_path)) {
4482 tree_path = strdup(a->worktree->path_prefix);
4483 if (tree_path == NULL) {
4484 err = got_error_from_errno("strdup");
4485 goto done;
4487 } else {
4488 if (asprintf(&tree_path, "%s/%s",
4489 a->worktree->path_prefix, parent_path) == -1) {
4490 err = got_error_from_errno("asprintf");
4491 goto done;
4496 err = got_object_id_by_path(&tree_id, a->repo,
4497 a->worktree->base_commit_id, tree_path);
4498 if (err) {
4499 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4500 (status == GOT_STATUS_ADD ||
4501 staged_status == GOT_STATUS_ADD)))
4502 goto done;
4503 } else {
4504 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4505 if (err)
4506 goto done;
4508 err = got_path_basename(&te_name, ie->path);
4509 if (err)
4510 goto done;
4512 te = got_object_tree_find_entry(tree, te_name);
4513 free(te_name);
4514 if (te == NULL && status != GOT_STATUS_ADD &&
4515 staged_status != GOT_STATUS_ADD) {
4516 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4517 goto done;
4521 switch (status) {
4522 case GOT_STATUS_ADD:
4523 if (a->patch_cb) {
4524 int choice = GOT_PATCH_CHOICE_NONE;
4525 err = (*a->patch_cb)(&choice, a->patch_arg,
4526 status, ie->path, NULL, 1, 1);
4527 if (err)
4528 goto done;
4529 if (choice != GOT_PATCH_CHOICE_YES)
4530 break;
4532 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4533 ie->path);
4534 if (err)
4535 goto done;
4536 got_fileindex_entry_remove(a->fileindex, ie);
4537 break;
4538 case GOT_STATUS_DELETE:
4539 if (a->patch_cb) {
4540 int choice = GOT_PATCH_CHOICE_NONE;
4541 err = (*a->patch_cb)(&choice, a->patch_arg,
4542 status, ie->path, NULL, 1, 1);
4543 if (err)
4544 goto done;
4545 if (choice != GOT_PATCH_CHOICE_YES)
4546 break;
4548 /* fall through */
4549 case GOT_STATUS_MODIFY:
4550 case GOT_STATUS_MODE_CHANGE:
4551 case GOT_STATUS_CONFLICT:
4552 case GOT_STATUS_MISSING: {
4553 struct got_object_id id;
4554 if (staged_status == GOT_STATUS_ADD ||
4555 staged_status == GOT_STATUS_MODIFY) {
4556 memcpy(id.sha1, ie->staged_blob_sha1,
4557 SHA1_DIGEST_LENGTH);
4558 } else
4559 memcpy(id.sha1, ie->blob_sha1,
4560 SHA1_DIGEST_LENGTH);
4561 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4562 if (err)
4563 goto done;
4565 if (asprintf(&ondisk_path, "%s/%s",
4566 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4567 err = got_error_from_errno("asprintf");
4568 goto done;
4571 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4572 status == GOT_STATUS_CONFLICT)) {
4573 int is_bad_symlink = 0;
4574 err = create_patched_content(&path_content, 1, &id,
4575 ondisk_path, dirfd, de_name, ie->path, a->repo,
4576 a->patch_cb, a->patch_arg);
4577 if (err || path_content == NULL)
4578 break;
4579 if (te && S_ISLNK(te->mode)) {
4580 if (unlink(path_content) == -1) {
4581 err = got_error_from_errno2("unlink",
4582 path_content);
4583 break;
4585 err = install_symlink(&is_bad_symlink,
4586 a->worktree, ondisk_path, ie->path,
4587 blob, 0, 1, 0, a->repo,
4588 a->progress_cb, a->progress_arg);
4589 } else {
4590 if (rename(path_content, ondisk_path) == -1) {
4591 err = got_error_from_errno3("rename",
4592 path_content, ondisk_path);
4593 goto done;
4596 } else {
4597 int is_bad_symlink = 0;
4598 if (te && S_ISLNK(te->mode)) {
4599 err = install_symlink(&is_bad_symlink,
4600 a->worktree, ondisk_path, ie->path,
4601 blob, 0, 1, 0, a->repo,
4602 a->progress_cb, a->progress_arg);
4603 } else {
4604 err = install_blob(a->worktree, ondisk_path,
4605 ie->path,
4606 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4607 got_fileindex_perms_to_st(ie), blob,
4608 0, 1, 0, 0, a->repo,
4609 a->progress_cb, a->progress_arg);
4611 if (err)
4612 goto done;
4613 if (status == GOT_STATUS_DELETE ||
4614 status == GOT_STATUS_MODE_CHANGE) {
4615 err = got_fileindex_entry_update(ie,
4616 a->worktree->root_fd, relpath,
4617 blob->id.sha1,
4618 a->worktree->base_commit_id->sha1, 1);
4619 if (err)
4620 goto done;
4622 if (is_bad_symlink) {
4623 got_fileindex_entry_filetype_set(ie,
4624 GOT_FILEIDX_MODE_BAD_SYMLINK);
4627 break;
4629 default:
4630 break;
4632 done:
4633 free(ondisk_path);
4634 free(path_content);
4635 free(parent_path);
4636 free(tree_path);
4637 if (blob)
4638 got_object_blob_close(blob);
4639 if (tree)
4640 got_object_tree_close(tree);
4641 free(tree_id);
4642 return err;
4645 const struct got_error *
4646 got_worktree_revert(struct got_worktree *worktree,
4647 struct got_pathlist_head *paths,
4648 got_worktree_checkout_cb progress_cb, void *progress_arg,
4649 got_worktree_patch_cb patch_cb, void *patch_arg,
4650 struct got_repository *repo)
4652 struct got_fileindex *fileindex = NULL;
4653 char *fileindex_path = NULL;
4654 const struct got_error *err = NULL, *unlockerr = NULL;
4655 const struct got_error *sync_err = NULL;
4656 struct got_pathlist_entry *pe;
4657 struct revert_file_args rfa;
4659 err = lock_worktree(worktree, LOCK_EX);
4660 if (err)
4661 return err;
4663 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4664 if (err)
4665 goto done;
4667 rfa.worktree = worktree;
4668 rfa.fileindex = fileindex;
4669 rfa.progress_cb = progress_cb;
4670 rfa.progress_arg = progress_arg;
4671 rfa.patch_cb = patch_cb;
4672 rfa.patch_arg = patch_arg;
4673 rfa.repo = repo;
4674 TAILQ_FOREACH(pe, paths, entry) {
4675 err = worktree_status(worktree, pe->path, fileindex, repo,
4676 revert_file, &rfa, NULL, NULL, 0, 0);
4677 if (err)
4678 break;
4680 sync_err = sync_fileindex(fileindex, fileindex_path);
4681 if (sync_err && err == NULL)
4682 err = sync_err;
4683 done:
4684 free(fileindex_path);
4685 if (fileindex)
4686 got_fileindex_free(fileindex);
4687 unlockerr = lock_worktree(worktree, LOCK_SH);
4688 if (unlockerr && err == NULL)
4689 err = unlockerr;
4690 return err;
4693 static void
4694 free_commitable(struct got_commitable *ct)
4696 free(ct->path);
4697 free(ct->in_repo_path);
4698 free(ct->ondisk_path);
4699 free(ct->blob_id);
4700 free(ct->base_blob_id);
4701 free(ct->staged_blob_id);
4702 free(ct->base_commit_id);
4703 free(ct);
4706 struct collect_commitables_arg {
4707 struct got_pathlist_head *commitable_paths;
4708 struct got_repository *repo;
4709 struct got_worktree *worktree;
4710 struct got_fileindex *fileindex;
4711 int have_staged_files;
4712 int allow_bad_symlinks;
4715 static const struct got_error *
4716 collect_commitables(void *arg, unsigned char status,
4717 unsigned char staged_status, const char *relpath,
4718 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4719 struct got_object_id *commit_id, int dirfd, const char *de_name)
4721 struct collect_commitables_arg *a = arg;
4722 const struct got_error *err = NULL;
4723 struct got_commitable *ct = NULL;
4724 struct got_pathlist_entry *new = NULL;
4725 char *parent_path = NULL, *path = NULL;
4726 struct stat sb;
4728 if (a->have_staged_files) {
4729 if (staged_status != GOT_STATUS_MODIFY &&
4730 staged_status != GOT_STATUS_ADD &&
4731 staged_status != GOT_STATUS_DELETE)
4732 return NULL;
4733 } else {
4734 if (status == GOT_STATUS_CONFLICT)
4735 return got_error(GOT_ERR_COMMIT_CONFLICT);
4737 if (status != GOT_STATUS_MODIFY &&
4738 status != GOT_STATUS_MODE_CHANGE &&
4739 status != GOT_STATUS_ADD &&
4740 status != GOT_STATUS_DELETE)
4741 return NULL;
4744 if (asprintf(&path, "/%s", relpath) == -1) {
4745 err = got_error_from_errno("asprintf");
4746 goto done;
4748 if (strcmp(path, "/") == 0) {
4749 parent_path = strdup("");
4750 if (parent_path == NULL)
4751 return got_error_from_errno("strdup");
4752 } else {
4753 err = got_path_dirname(&parent_path, path);
4754 if (err)
4755 return err;
4758 ct = calloc(1, sizeof(*ct));
4759 if (ct == NULL) {
4760 err = got_error_from_errno("calloc");
4761 goto done;
4764 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4765 relpath) == -1) {
4766 err = got_error_from_errno("asprintf");
4767 goto done;
4770 if (staged_status == GOT_STATUS_ADD ||
4771 staged_status == GOT_STATUS_MODIFY) {
4772 struct got_fileindex_entry *ie;
4773 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4774 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4775 case GOT_FILEIDX_MODE_REGULAR_FILE:
4776 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4777 ct->mode = S_IFREG;
4778 break;
4779 case GOT_FILEIDX_MODE_SYMLINK:
4780 ct->mode = S_IFLNK;
4781 break;
4782 default:
4783 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4784 goto done;
4786 ct->mode |= got_fileindex_entry_perms_get(ie);
4787 } else if (status != GOT_STATUS_DELETE &&
4788 staged_status != GOT_STATUS_DELETE) {
4789 if (dirfd != -1) {
4790 if (fstatat(dirfd, de_name, &sb,
4791 AT_SYMLINK_NOFOLLOW) == -1) {
4792 err = got_error_from_errno2("fstatat",
4793 ct->ondisk_path);
4794 goto done;
4796 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4797 err = got_error_from_errno2("lstat", ct->ondisk_path);
4798 goto done;
4800 ct->mode = sb.st_mode;
4803 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4804 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4805 relpath) == -1) {
4806 err = got_error_from_errno("asprintf");
4807 goto done;
4810 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4811 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4812 int is_bad_symlink;
4813 char target_path[PATH_MAX];
4814 ssize_t target_len;
4815 target_len = readlink(ct->ondisk_path, target_path,
4816 sizeof(target_path));
4817 if (target_len == -1) {
4818 err = got_error_from_errno2("readlink",
4819 ct->ondisk_path);
4820 goto done;
4822 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4823 target_len, ct->ondisk_path, a->worktree->root_path);
4824 if (err)
4825 goto done;
4826 if (is_bad_symlink) {
4827 err = got_error_path(ct->ondisk_path,
4828 GOT_ERR_BAD_SYMLINK);
4829 goto done;
4834 ct->status = status;
4835 ct->staged_status = staged_status;
4836 ct->blob_id = NULL; /* will be filled in when blob gets created */
4837 if (ct->status != GOT_STATUS_ADD &&
4838 ct->staged_status != GOT_STATUS_ADD) {
4839 ct->base_blob_id = got_object_id_dup(blob_id);
4840 if (ct->base_blob_id == NULL) {
4841 err = got_error_from_errno("got_object_id_dup");
4842 goto done;
4844 ct->base_commit_id = got_object_id_dup(commit_id);
4845 if (ct->base_commit_id == NULL) {
4846 err = got_error_from_errno("got_object_id_dup");
4847 goto done;
4850 if (ct->staged_status == GOT_STATUS_ADD ||
4851 ct->staged_status == GOT_STATUS_MODIFY) {
4852 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4853 if (ct->staged_blob_id == NULL) {
4854 err = got_error_from_errno("got_object_id_dup");
4855 goto done;
4858 ct->path = strdup(path);
4859 if (ct->path == NULL) {
4860 err = got_error_from_errno("strdup");
4861 goto done;
4863 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4864 done:
4865 if (ct && (err || new == NULL))
4866 free_commitable(ct);
4867 free(parent_path);
4868 free(path);
4869 return err;
4872 static const struct got_error *write_tree(struct got_object_id **, int *,
4873 struct got_tree_object *, const char *, struct got_pathlist_head *,
4874 got_worktree_status_cb status_cb, void *status_arg,
4875 struct got_repository *);
4877 static const struct got_error *
4878 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4879 struct got_tree_entry *te, const char *parent_path,
4880 struct got_pathlist_head *commitable_paths,
4881 got_worktree_status_cb status_cb, void *status_arg,
4882 struct got_repository *repo)
4884 const struct got_error *err = NULL;
4885 struct got_tree_object *subtree;
4886 char *subpath;
4888 if (asprintf(&subpath, "%s%s%s", parent_path,
4889 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4890 return got_error_from_errno("asprintf");
4892 err = got_object_open_as_tree(&subtree, repo, &te->id);
4893 if (err)
4894 return err;
4896 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4897 commitable_paths, status_cb, status_arg, repo);
4898 got_object_tree_close(subtree);
4899 free(subpath);
4900 return err;
4903 static const struct got_error *
4904 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4906 const struct got_error *err = NULL;
4907 char *ct_parent_path = NULL;
4909 *match = 0;
4911 if (strchr(ct->in_repo_path, '/') == NULL) {
4912 *match = got_path_is_root_dir(path);
4913 return NULL;
4916 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4917 if (err)
4918 return err;
4919 *match = (strcmp(path, ct_parent_path) == 0);
4920 free(ct_parent_path);
4921 return err;
4924 static mode_t
4925 get_ct_file_mode(struct got_commitable *ct)
4927 if (S_ISLNK(ct->mode))
4928 return S_IFLNK;
4930 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4933 static const struct got_error *
4934 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4935 struct got_tree_entry *te, struct got_commitable *ct)
4937 const struct got_error *err = NULL;
4939 *new_te = NULL;
4941 err = got_object_tree_entry_dup(new_te, te);
4942 if (err)
4943 goto done;
4945 (*new_te)->mode = get_ct_file_mode(ct);
4947 if (ct->staged_status == GOT_STATUS_MODIFY)
4948 memcpy(&(*new_te)->id, ct->staged_blob_id,
4949 sizeof((*new_te)->id));
4950 else
4951 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4952 done:
4953 if (err && *new_te) {
4954 free(*new_te);
4955 *new_te = NULL;
4957 return err;
4960 static const struct got_error *
4961 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4962 struct got_commitable *ct)
4964 const struct got_error *err = NULL;
4965 char *ct_name = NULL;
4967 *new_te = NULL;
4969 *new_te = calloc(1, sizeof(**new_te));
4970 if (*new_te == NULL)
4971 return got_error_from_errno("calloc");
4973 err = got_path_basename(&ct_name, ct->path);
4974 if (err)
4975 goto done;
4976 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4977 sizeof((*new_te)->name)) {
4978 err = got_error(GOT_ERR_NO_SPACE);
4979 goto done;
4982 (*new_te)->mode = get_ct_file_mode(ct);
4984 if (ct->staged_status == GOT_STATUS_ADD)
4985 memcpy(&(*new_te)->id, ct->staged_blob_id,
4986 sizeof((*new_te)->id));
4987 else
4988 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4989 done:
4990 free(ct_name);
4991 if (err && *new_te) {
4992 free(*new_te);
4993 *new_te = NULL;
4995 return err;
4998 static const struct got_error *
4999 insert_tree_entry(struct got_tree_entry *new_te,
5000 struct got_pathlist_head *paths)
5002 const struct got_error *err = NULL;
5003 struct got_pathlist_entry *new_pe;
5005 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5006 if (err)
5007 return err;
5008 if (new_pe == NULL)
5009 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5010 return NULL;
5013 static const struct got_error *
5014 report_ct_status(struct got_commitable *ct,
5015 got_worktree_status_cb status_cb, void *status_arg)
5017 const char *ct_path = ct->path;
5018 unsigned char status;
5020 while (ct_path[0] == '/')
5021 ct_path++;
5023 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5024 status = ct->staged_status;
5025 else
5026 status = ct->status;
5028 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5029 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5032 static const struct got_error *
5033 match_modified_subtree(int *modified, struct got_tree_entry *te,
5034 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5036 const struct got_error *err = NULL;
5037 struct got_pathlist_entry *pe;
5038 char *te_path;
5040 *modified = 0;
5042 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5043 got_path_is_root_dir(base_tree_path) ? "" : "/",
5044 te->name) == -1)
5045 return got_error_from_errno("asprintf");
5047 TAILQ_FOREACH(pe, commitable_paths, entry) {
5048 struct got_commitable *ct = pe->data;
5049 *modified = got_path_is_child(ct->in_repo_path, te_path,
5050 strlen(te_path));
5051 if (*modified)
5052 break;
5055 free(te_path);
5056 return err;
5059 static const struct got_error *
5060 match_deleted_or_modified_ct(struct got_commitable **ctp,
5061 struct got_tree_entry *te, const char *base_tree_path,
5062 struct got_pathlist_head *commitable_paths)
5064 const struct got_error *err = NULL;
5065 struct got_pathlist_entry *pe;
5067 *ctp = NULL;
5069 TAILQ_FOREACH(pe, commitable_paths, entry) {
5070 struct got_commitable *ct = pe->data;
5071 char *ct_name = NULL;
5072 int path_matches;
5074 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5075 if (ct->status != GOT_STATUS_MODIFY &&
5076 ct->status != GOT_STATUS_MODE_CHANGE &&
5077 ct->status != GOT_STATUS_DELETE)
5078 continue;
5079 } else {
5080 if (ct->staged_status != GOT_STATUS_MODIFY &&
5081 ct->staged_status != GOT_STATUS_DELETE)
5082 continue;
5085 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5086 continue;
5088 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5089 if (err)
5090 return err;
5091 if (!path_matches)
5092 continue;
5094 err = got_path_basename(&ct_name, pe->path);
5095 if (err)
5096 return err;
5098 if (strcmp(te->name, ct_name) != 0) {
5099 free(ct_name);
5100 continue;
5102 free(ct_name);
5104 *ctp = ct;
5105 break;
5108 return err;
5111 static const struct got_error *
5112 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5113 const char *child_path, const char *path_base_tree,
5114 struct got_pathlist_head *commitable_paths,
5115 got_worktree_status_cb status_cb, void *status_arg,
5116 struct got_repository *repo)
5118 const struct got_error *err = NULL;
5119 struct got_tree_entry *new_te;
5120 char *subtree_path;
5121 struct got_object_id *id = NULL;
5122 int nentries;
5124 *new_tep = NULL;
5126 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5127 got_path_is_root_dir(path_base_tree) ? "" : "/",
5128 child_path) == -1)
5129 return got_error_from_errno("asprintf");
5131 new_te = calloc(1, sizeof(*new_te));
5132 if (new_te == NULL)
5133 return got_error_from_errno("calloc");
5134 new_te->mode = S_IFDIR;
5136 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5137 sizeof(new_te->name)) {
5138 err = got_error(GOT_ERR_NO_SPACE);
5139 goto done;
5141 err = write_tree(&id, &nentries, NULL, subtree_path,
5142 commitable_paths, status_cb, status_arg, repo);
5143 if (err) {
5144 free(new_te);
5145 goto done;
5147 memcpy(&new_te->id, id, sizeof(new_te->id));
5148 done:
5149 free(id);
5150 free(subtree_path);
5151 if (err == NULL)
5152 *new_tep = new_te;
5153 return err;
5156 static const struct got_error *
5157 write_tree(struct got_object_id **new_tree_id, int *nentries,
5158 struct got_tree_object *base_tree, const char *path_base_tree,
5159 struct got_pathlist_head *commitable_paths,
5160 got_worktree_status_cb status_cb, void *status_arg,
5161 struct got_repository *repo)
5163 const struct got_error *err = NULL;
5164 struct got_pathlist_head paths;
5165 struct got_tree_entry *te, *new_te = NULL;
5166 struct got_pathlist_entry *pe;
5168 TAILQ_INIT(&paths);
5169 *nentries = 0;
5171 /* Insert, and recurse into, newly added entries first. */
5172 TAILQ_FOREACH(pe, commitable_paths, entry) {
5173 struct got_commitable *ct = pe->data;
5174 char *child_path = NULL, *slash;
5176 if ((ct->status != GOT_STATUS_ADD &&
5177 ct->staged_status != GOT_STATUS_ADD) ||
5178 (ct->flags & GOT_COMMITABLE_ADDED))
5179 continue;
5181 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5182 strlen(path_base_tree)))
5183 continue;
5185 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5186 ct->in_repo_path);
5187 if (err)
5188 goto done;
5190 slash = strchr(child_path, '/');
5191 if (slash == NULL) {
5192 err = alloc_added_blob_tree_entry(&new_te, ct);
5193 if (err)
5194 goto done;
5195 err = report_ct_status(ct, status_cb, status_arg);
5196 if (err)
5197 goto done;
5198 ct->flags |= GOT_COMMITABLE_ADDED;
5199 err = insert_tree_entry(new_te, &paths);
5200 if (err)
5201 goto done;
5202 (*nentries)++;
5203 } else {
5204 *slash = '\0'; /* trim trailing path components */
5205 if (base_tree == NULL ||
5206 got_object_tree_find_entry(base_tree, child_path)
5207 == NULL) {
5208 err = make_subtree_for_added_blob(&new_te,
5209 child_path, path_base_tree,
5210 commitable_paths, status_cb, status_arg,
5211 repo);
5212 if (err)
5213 goto done;
5214 err = insert_tree_entry(new_te, &paths);
5215 if (err)
5216 goto done;
5217 (*nentries)++;
5222 if (base_tree) {
5223 int i, nbase_entries;
5224 /* Handle modified and deleted entries. */
5225 nbase_entries = got_object_tree_get_nentries(base_tree);
5226 for (i = 0; i < nbase_entries; i++) {
5227 struct got_commitable *ct = NULL;
5229 te = got_object_tree_get_entry(base_tree, i);
5230 if (got_object_tree_entry_is_submodule(te)) {
5231 /* Entry is a submodule; just copy it. */
5232 err = got_object_tree_entry_dup(&new_te, te);
5233 if (err)
5234 goto done;
5235 err = insert_tree_entry(new_te, &paths);
5236 if (err)
5237 goto done;
5238 (*nentries)++;
5239 continue;
5242 if (S_ISDIR(te->mode)) {
5243 int modified;
5244 err = got_object_tree_entry_dup(&new_te, te);
5245 if (err)
5246 goto done;
5247 err = match_modified_subtree(&modified, te,
5248 path_base_tree, commitable_paths);
5249 if (err)
5250 goto done;
5251 /* Avoid recursion into unmodified subtrees. */
5252 if (modified) {
5253 struct got_object_id *new_id;
5254 int nsubentries;
5255 err = write_subtree(&new_id,
5256 &nsubentries, te,
5257 path_base_tree, commitable_paths,
5258 status_cb, status_arg, repo);
5259 if (err)
5260 goto done;
5261 if (nsubentries == 0) {
5262 /* All entries were deleted. */
5263 free(new_id);
5264 continue;
5266 memcpy(&new_te->id, new_id,
5267 sizeof(new_te->id));
5268 free(new_id);
5270 err = insert_tree_entry(new_te, &paths);
5271 if (err)
5272 goto done;
5273 (*nentries)++;
5274 continue;
5277 err = match_deleted_or_modified_ct(&ct, te,
5278 path_base_tree, commitable_paths);
5279 if (err)
5280 goto done;
5281 if (ct) {
5282 /* NB: Deleted entries get dropped here. */
5283 if (ct->status == GOT_STATUS_MODIFY ||
5284 ct->status == GOT_STATUS_MODE_CHANGE ||
5285 ct->staged_status == GOT_STATUS_MODIFY) {
5286 err = alloc_modified_blob_tree_entry(
5287 &new_te, te, ct);
5288 if (err)
5289 goto done;
5290 err = insert_tree_entry(new_te, &paths);
5291 if (err)
5292 goto done;
5293 (*nentries)++;
5295 err = report_ct_status(ct, status_cb,
5296 status_arg);
5297 if (err)
5298 goto done;
5299 } else {
5300 /* Entry is unchanged; just copy it. */
5301 err = got_object_tree_entry_dup(&new_te, te);
5302 if (err)
5303 goto done;
5304 err = insert_tree_entry(new_te, &paths);
5305 if (err)
5306 goto done;
5307 (*nentries)++;
5312 /* Write new list of entries; deleted entries have been dropped. */
5313 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5314 done:
5315 got_pathlist_free(&paths);
5316 return err;
5319 static const struct got_error *
5320 update_fileindex_after_commit(struct got_worktree *worktree,
5321 struct got_pathlist_head *commitable_paths,
5322 struct got_object_id *new_base_commit_id,
5323 struct got_fileindex *fileindex, int have_staged_files)
5325 const struct got_error *err = NULL;
5326 struct got_pathlist_entry *pe;
5327 char *relpath = NULL;
5329 TAILQ_FOREACH(pe, commitable_paths, entry) {
5330 struct got_fileindex_entry *ie;
5331 struct got_commitable *ct = pe->data;
5333 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5335 err = got_path_skip_common_ancestor(&relpath,
5336 worktree->root_path, ct->ondisk_path);
5337 if (err)
5338 goto done;
5340 if (ie) {
5341 if (ct->status == GOT_STATUS_DELETE ||
5342 ct->staged_status == GOT_STATUS_DELETE) {
5343 got_fileindex_entry_remove(fileindex, ie);
5344 } else if (ct->staged_status == GOT_STATUS_ADD ||
5345 ct->staged_status == GOT_STATUS_MODIFY) {
5346 got_fileindex_entry_stage_set(ie,
5347 GOT_FILEIDX_STAGE_NONE);
5348 got_fileindex_entry_staged_filetype_set(ie, 0);
5350 err = got_fileindex_entry_update(ie,
5351 worktree->root_fd, relpath,
5352 ct->staged_blob_id->sha1,
5353 new_base_commit_id->sha1,
5354 !have_staged_files);
5355 } else
5356 err = got_fileindex_entry_update(ie,
5357 worktree->root_fd, relpath,
5358 ct->blob_id->sha1,
5359 new_base_commit_id->sha1,
5360 !have_staged_files);
5361 } else {
5362 err = got_fileindex_entry_alloc(&ie, pe->path);
5363 if (err)
5364 goto done;
5365 err = got_fileindex_entry_update(ie,
5366 worktree->root_fd, relpath, ct->blob_id->sha1,
5367 new_base_commit_id->sha1, 1);
5368 if (err) {
5369 got_fileindex_entry_free(ie);
5370 goto done;
5372 err = got_fileindex_entry_add(fileindex, ie);
5373 if (err) {
5374 got_fileindex_entry_free(ie);
5375 goto done;
5378 free(relpath);
5379 relpath = NULL;
5381 done:
5382 free(relpath);
5383 return err;
5387 static const struct got_error *
5388 check_out_of_date(const char *in_repo_path, unsigned char status,
5389 unsigned char staged_status, struct got_object_id *base_blob_id,
5390 struct got_object_id *base_commit_id,
5391 struct got_object_id *head_commit_id, struct got_repository *repo,
5392 int ood_errcode)
5394 const struct got_error *err = NULL;
5395 struct got_object_id *id = NULL;
5397 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5398 /* Trivial case: base commit == head commit */
5399 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5400 return NULL;
5402 * Ensure file content which local changes were based
5403 * on matches file content in the branch head.
5405 err = got_object_id_by_path(&id, repo, head_commit_id,
5406 in_repo_path);
5407 if (err) {
5408 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5409 err = got_error(ood_errcode);
5410 goto done;
5411 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5412 err = got_error(ood_errcode);
5413 } else {
5414 /* Require that added files don't exist in the branch head. */
5415 err = got_object_id_by_path(&id, repo, head_commit_id,
5416 in_repo_path);
5417 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5418 goto done;
5419 err = id ? got_error(ood_errcode) : NULL;
5421 done:
5422 free(id);
5423 return err;
5426 const struct got_error *
5427 commit_worktree(struct got_object_id **new_commit_id,
5428 struct got_pathlist_head *commitable_paths,
5429 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5430 const char *author, const char *committer,
5431 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5432 got_worktree_status_cb status_cb, void *status_arg,
5433 struct got_repository *repo)
5435 const struct got_error *err = NULL, *unlockerr = NULL;
5436 struct got_pathlist_entry *pe;
5437 const char *head_ref_name = NULL;
5438 struct got_commit_object *head_commit = NULL;
5439 struct got_reference *head_ref2 = NULL;
5440 struct got_object_id *head_commit_id2 = NULL;
5441 struct got_tree_object *head_tree = NULL;
5442 struct got_object_id *new_tree_id = NULL;
5443 int nentries;
5444 struct got_object_id_queue parent_ids;
5445 struct got_object_qid *pid = NULL;
5446 char *logmsg = NULL;
5448 *new_commit_id = NULL;
5450 SIMPLEQ_INIT(&parent_ids);
5452 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5453 if (err)
5454 goto done;
5456 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5457 if (err)
5458 goto done;
5460 if (commit_msg_cb != NULL) {
5461 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5462 if (err)
5463 goto done;
5466 if (logmsg == NULL || strlen(logmsg) == 0) {
5467 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5468 goto done;
5471 /* Create blobs from added and modified files and record their IDs. */
5472 TAILQ_FOREACH(pe, commitable_paths, entry) {
5473 struct got_commitable *ct = pe->data;
5474 char *ondisk_path;
5476 /* Blobs for staged files already exist. */
5477 if (ct->staged_status == GOT_STATUS_ADD ||
5478 ct->staged_status == GOT_STATUS_MODIFY)
5479 continue;
5481 if (ct->status != GOT_STATUS_ADD &&
5482 ct->status != GOT_STATUS_MODIFY &&
5483 ct->status != GOT_STATUS_MODE_CHANGE)
5484 continue;
5486 if (asprintf(&ondisk_path, "%s/%s",
5487 worktree->root_path, pe->path) == -1) {
5488 err = got_error_from_errno("asprintf");
5489 goto done;
5491 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5492 free(ondisk_path);
5493 if (err)
5494 goto done;
5497 /* Recursively write new tree objects. */
5498 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5499 commitable_paths, status_cb, status_arg, repo);
5500 if (err)
5501 goto done;
5503 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5504 if (err)
5505 goto done;
5506 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5507 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5508 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5509 got_object_qid_free(pid);
5510 if (logmsg != NULL)
5511 free(logmsg);
5512 if (err)
5513 goto done;
5515 /* Check if a concurrent commit to our branch has occurred. */
5516 head_ref_name = got_worktree_get_head_ref_name(worktree);
5517 if (head_ref_name == NULL) {
5518 err = got_error_from_errno("got_worktree_get_head_ref_name");
5519 goto done;
5521 /* Lock the reference here to prevent concurrent modification. */
5522 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5523 if (err)
5524 goto done;
5525 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5526 if (err)
5527 goto done;
5528 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5529 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5530 goto done;
5532 /* Update branch head in repository. */
5533 err = got_ref_change_ref(head_ref2, *new_commit_id);
5534 if (err)
5535 goto done;
5536 err = got_ref_write(head_ref2, repo);
5537 if (err)
5538 goto done;
5540 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5541 if (err)
5542 goto done;
5544 err = ref_base_commit(worktree, repo);
5545 if (err)
5546 goto done;
5547 done:
5548 if (head_tree)
5549 got_object_tree_close(head_tree);
5550 if (head_commit)
5551 got_object_commit_close(head_commit);
5552 free(head_commit_id2);
5553 if (head_ref2) {
5554 unlockerr = got_ref_unlock(head_ref2);
5555 if (unlockerr && err == NULL)
5556 err = unlockerr;
5557 got_ref_close(head_ref2);
5559 return err;
5562 static const struct got_error *
5563 check_path_is_commitable(const char *path,
5564 struct got_pathlist_head *commitable_paths)
5566 struct got_pathlist_entry *cpe = NULL;
5567 size_t path_len = strlen(path);
5569 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5570 struct got_commitable *ct = cpe->data;
5571 const char *ct_path = ct->path;
5573 while (ct_path[0] == '/')
5574 ct_path++;
5576 if (strcmp(path, ct_path) == 0 ||
5577 got_path_is_child(ct_path, path, path_len))
5578 break;
5581 if (cpe == NULL)
5582 return got_error_path(path, GOT_ERR_BAD_PATH);
5584 return NULL;
5587 static const struct got_error *
5588 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5590 int *have_staged_files = arg;
5592 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5593 *have_staged_files = 1;
5594 return got_error(GOT_ERR_CANCELLED);
5597 return NULL;
5600 static const struct got_error *
5601 check_non_staged_files(struct got_fileindex *fileindex,
5602 struct got_pathlist_head *paths)
5604 struct got_pathlist_entry *pe;
5605 struct got_fileindex_entry *ie;
5607 TAILQ_FOREACH(pe, paths, entry) {
5608 if (pe->path[0] == '\0')
5609 continue;
5610 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5611 if (ie == NULL)
5612 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5613 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5614 return got_error_path(pe->path,
5615 GOT_ERR_FILE_NOT_STAGED);
5618 return NULL;
5621 const struct got_error *
5622 got_worktree_commit(struct got_object_id **new_commit_id,
5623 struct got_worktree *worktree, struct got_pathlist_head *paths,
5624 const char *author, const char *committer, int allow_bad_symlinks,
5625 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5626 got_worktree_status_cb status_cb, void *status_arg,
5627 struct got_repository *repo)
5629 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5630 struct got_fileindex *fileindex = NULL;
5631 char *fileindex_path = NULL;
5632 struct got_pathlist_head commitable_paths;
5633 struct collect_commitables_arg cc_arg;
5634 struct got_pathlist_entry *pe;
5635 struct got_reference *head_ref = NULL;
5636 struct got_object_id *head_commit_id = NULL;
5637 int have_staged_files = 0;
5639 *new_commit_id = NULL;
5641 TAILQ_INIT(&commitable_paths);
5643 err = lock_worktree(worktree, LOCK_EX);
5644 if (err)
5645 goto done;
5647 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5648 if (err)
5649 goto done;
5651 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5652 if (err)
5653 goto done;
5655 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5656 if (err)
5657 goto done;
5659 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5660 &have_staged_files);
5661 if (err && err->code != GOT_ERR_CANCELLED)
5662 goto done;
5663 if (have_staged_files) {
5664 err = check_non_staged_files(fileindex, paths);
5665 if (err)
5666 goto done;
5669 cc_arg.commitable_paths = &commitable_paths;
5670 cc_arg.worktree = worktree;
5671 cc_arg.fileindex = fileindex;
5672 cc_arg.repo = repo;
5673 cc_arg.have_staged_files = have_staged_files;
5674 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5675 TAILQ_FOREACH(pe, paths, entry) {
5676 err = worktree_status(worktree, pe->path, fileindex, repo,
5677 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5678 if (err)
5679 goto done;
5682 if (TAILQ_EMPTY(&commitable_paths)) {
5683 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5684 goto done;
5687 TAILQ_FOREACH(pe, paths, entry) {
5688 err = check_path_is_commitable(pe->path, &commitable_paths);
5689 if (err)
5690 goto done;
5693 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5694 struct got_commitable *ct = pe->data;
5695 const char *ct_path = ct->in_repo_path;
5697 while (ct_path[0] == '/')
5698 ct_path++;
5699 err = check_out_of_date(ct_path, ct->status,
5700 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5701 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5702 if (err)
5703 goto done;
5707 err = commit_worktree(new_commit_id, &commitable_paths,
5708 head_commit_id, worktree, author, committer,
5709 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5710 if (err)
5711 goto done;
5713 err = update_fileindex_after_commit(worktree, &commitable_paths,
5714 *new_commit_id, fileindex, have_staged_files);
5715 sync_err = sync_fileindex(fileindex, fileindex_path);
5716 if (sync_err && err == NULL)
5717 err = sync_err;
5718 done:
5719 if (fileindex)
5720 got_fileindex_free(fileindex);
5721 free(fileindex_path);
5722 unlockerr = lock_worktree(worktree, LOCK_SH);
5723 if (unlockerr && err == NULL)
5724 err = unlockerr;
5725 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5726 struct got_commitable *ct = pe->data;
5727 free_commitable(ct);
5729 got_pathlist_free(&commitable_paths);
5730 return err;
5733 const char *
5734 got_commitable_get_path(struct got_commitable *ct)
5736 return ct->path;
5739 unsigned int
5740 got_commitable_get_status(struct got_commitable *ct)
5742 return ct->status;
5745 struct check_rebase_ok_arg {
5746 struct got_worktree *worktree;
5747 struct got_repository *repo;
5750 static const struct got_error *
5751 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5753 const struct got_error *err = NULL;
5754 struct check_rebase_ok_arg *a = arg;
5755 unsigned char status;
5756 struct stat sb;
5757 char *ondisk_path;
5759 /* Reject rebase of a work tree with mixed base commits. */
5760 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5761 SHA1_DIGEST_LENGTH))
5762 return got_error(GOT_ERR_MIXED_COMMITS);
5764 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5765 == -1)
5766 return got_error_from_errno("asprintf");
5768 /* Reject rebase of a work tree with modified or staged files. */
5769 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5770 free(ondisk_path);
5771 if (err)
5772 return err;
5774 if (status != GOT_STATUS_NO_CHANGE)
5775 return got_error(GOT_ERR_MODIFIED);
5776 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5777 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5779 return NULL;
5782 const struct got_error *
5783 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5784 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5785 struct got_worktree *worktree, struct got_reference *branch,
5786 struct got_repository *repo)
5788 const struct got_error *err = NULL;
5789 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5790 char *branch_ref_name = NULL;
5791 char *fileindex_path = NULL;
5792 struct check_rebase_ok_arg ok_arg;
5793 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5794 struct got_object_id *wt_branch_tip = NULL;
5796 *new_base_branch_ref = NULL;
5797 *tmp_branch = NULL;
5798 *fileindex = NULL;
5800 err = lock_worktree(worktree, LOCK_EX);
5801 if (err)
5802 return err;
5804 err = open_fileindex(fileindex, &fileindex_path, worktree);
5805 if (err)
5806 goto done;
5808 ok_arg.worktree = worktree;
5809 ok_arg.repo = repo;
5810 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5811 &ok_arg);
5812 if (err)
5813 goto done;
5815 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5816 if (err)
5817 goto done;
5819 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5820 if (err)
5821 goto done;
5823 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5824 if (err)
5825 goto done;
5827 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5828 0);
5829 if (err)
5830 goto done;
5832 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5833 if (err)
5834 goto done;
5835 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5836 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5837 goto done;
5840 err = got_ref_alloc_symref(new_base_branch_ref,
5841 new_base_branch_ref_name, wt_branch);
5842 if (err)
5843 goto done;
5844 err = got_ref_write(*new_base_branch_ref, repo);
5845 if (err)
5846 goto done;
5848 /* TODO Lock original branch's ref while rebasing? */
5850 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5851 if (err)
5852 goto done;
5854 err = got_ref_write(branch_ref, repo);
5855 if (err)
5856 goto done;
5858 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5859 worktree->base_commit_id);
5860 if (err)
5861 goto done;
5862 err = got_ref_write(*tmp_branch, repo);
5863 if (err)
5864 goto done;
5866 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5867 if (err)
5868 goto done;
5869 done:
5870 free(fileindex_path);
5871 free(tmp_branch_name);
5872 free(new_base_branch_ref_name);
5873 free(branch_ref_name);
5874 if (branch_ref)
5875 got_ref_close(branch_ref);
5876 if (wt_branch)
5877 got_ref_close(wt_branch);
5878 free(wt_branch_tip);
5879 if (err) {
5880 if (*new_base_branch_ref) {
5881 got_ref_close(*new_base_branch_ref);
5882 *new_base_branch_ref = NULL;
5884 if (*tmp_branch) {
5885 got_ref_close(*tmp_branch);
5886 *tmp_branch = NULL;
5888 if (*fileindex) {
5889 got_fileindex_free(*fileindex);
5890 *fileindex = NULL;
5892 lock_worktree(worktree, LOCK_SH);
5894 return err;
5897 const struct got_error *
5898 got_worktree_rebase_continue(struct got_object_id **commit_id,
5899 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5900 struct got_reference **branch, struct got_fileindex **fileindex,
5901 struct got_worktree *worktree, struct got_repository *repo)
5903 const struct got_error *err;
5904 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5905 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5906 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5907 char *fileindex_path = NULL;
5908 int have_staged_files = 0;
5910 *commit_id = NULL;
5911 *new_base_branch = NULL;
5912 *tmp_branch = NULL;
5913 *branch = NULL;
5914 *fileindex = NULL;
5916 err = lock_worktree(worktree, LOCK_EX);
5917 if (err)
5918 return err;
5920 err = open_fileindex(fileindex, &fileindex_path, worktree);
5921 if (err)
5922 goto done;
5924 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5925 &have_staged_files);
5926 if (err && err->code != GOT_ERR_CANCELLED)
5927 goto done;
5928 if (have_staged_files) {
5929 err = got_error(GOT_ERR_STAGED_PATHS);
5930 goto done;
5933 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5934 if (err)
5935 goto done;
5937 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5938 if (err)
5939 goto done;
5941 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5942 if (err)
5943 goto done;
5945 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5946 if (err)
5947 goto done;
5949 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5950 if (err)
5951 goto done;
5953 err = got_ref_open(branch, repo,
5954 got_ref_get_symref_target(branch_ref), 0);
5955 if (err)
5956 goto done;
5958 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5959 if (err)
5960 goto done;
5962 err = got_ref_resolve(commit_id, repo, commit_ref);
5963 if (err)
5964 goto done;
5966 err = got_ref_open(new_base_branch, repo,
5967 new_base_branch_ref_name, 0);
5968 if (err)
5969 goto done;
5971 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5972 if (err)
5973 goto done;
5974 done:
5975 free(commit_ref_name);
5976 free(branch_ref_name);
5977 free(fileindex_path);
5978 if (commit_ref)
5979 got_ref_close(commit_ref);
5980 if (branch_ref)
5981 got_ref_close(branch_ref);
5982 if (err) {
5983 free(*commit_id);
5984 *commit_id = NULL;
5985 if (*tmp_branch) {
5986 got_ref_close(*tmp_branch);
5987 *tmp_branch = NULL;
5989 if (*new_base_branch) {
5990 got_ref_close(*new_base_branch);
5991 *new_base_branch = NULL;
5993 if (*branch) {
5994 got_ref_close(*branch);
5995 *branch = NULL;
5997 if (*fileindex) {
5998 got_fileindex_free(*fileindex);
5999 *fileindex = NULL;
6001 lock_worktree(worktree, LOCK_SH);
6003 return err;
6006 const struct got_error *
6007 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6009 const struct got_error *err;
6010 char *tmp_branch_name = NULL;
6012 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6013 if (err)
6014 return err;
6016 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6017 free(tmp_branch_name);
6018 return NULL;
6021 static const struct got_error *
6022 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6023 char **logmsg, void *arg)
6025 *logmsg = arg;
6026 return NULL;
6029 static const struct got_error *
6030 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6031 const char *path, struct got_object_id *blob_id,
6032 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6033 int dirfd, const char *de_name)
6035 return NULL;
6038 struct collect_merged_paths_arg {
6039 got_worktree_checkout_cb progress_cb;
6040 void *progress_arg;
6041 struct got_pathlist_head *merged_paths;
6044 static const struct got_error *
6045 collect_merged_paths(void *arg, unsigned char status, const char *path)
6047 const struct got_error *err;
6048 struct collect_merged_paths_arg *a = arg;
6049 char *p;
6050 struct got_pathlist_entry *new;
6052 err = (*a->progress_cb)(a->progress_arg, status, path);
6053 if (err)
6054 return err;
6056 if (status != GOT_STATUS_MERGE &&
6057 status != GOT_STATUS_ADD &&
6058 status != GOT_STATUS_DELETE &&
6059 status != GOT_STATUS_CONFLICT)
6060 return NULL;
6062 p = strdup(path);
6063 if (p == NULL)
6064 return got_error_from_errno("strdup");
6066 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6067 if (err || new == NULL)
6068 free(p);
6069 return err;
6072 void
6073 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6075 struct got_pathlist_entry *pe;
6077 TAILQ_FOREACH(pe, merged_paths, entry)
6078 free((char *)pe->path);
6080 got_pathlist_free(merged_paths);
6083 static const struct got_error *
6084 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6085 int is_rebase, struct got_repository *repo)
6087 const struct got_error *err;
6088 struct got_reference *commit_ref = NULL;
6090 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6091 if (err) {
6092 if (err->code != GOT_ERR_NOT_REF)
6093 goto done;
6094 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6095 if (err)
6096 goto done;
6097 err = got_ref_write(commit_ref, repo);
6098 if (err)
6099 goto done;
6100 } else if (is_rebase) {
6101 struct got_object_id *stored_id;
6102 int cmp;
6104 err = got_ref_resolve(&stored_id, repo, commit_ref);
6105 if (err)
6106 goto done;
6107 cmp = got_object_id_cmp(commit_id, stored_id);
6108 free(stored_id);
6109 if (cmp != 0) {
6110 err = got_error(GOT_ERR_REBASE_COMMITID);
6111 goto done;
6114 done:
6115 if (commit_ref)
6116 got_ref_close(commit_ref);
6117 return err;
6120 static const struct got_error *
6121 rebase_merge_files(struct got_pathlist_head *merged_paths,
6122 const char *commit_ref_name, struct got_worktree *worktree,
6123 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6124 struct got_object_id *commit_id, struct got_repository *repo,
6125 got_worktree_checkout_cb progress_cb, void *progress_arg,
6126 got_cancel_cb cancel_cb, void *cancel_arg)
6128 const struct got_error *err;
6129 struct got_reference *commit_ref = NULL;
6130 struct collect_merged_paths_arg cmp_arg;
6131 char *fileindex_path;
6133 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6135 err = get_fileindex_path(&fileindex_path, worktree);
6136 if (err)
6137 return err;
6139 cmp_arg.progress_cb = progress_cb;
6140 cmp_arg.progress_arg = progress_arg;
6141 cmp_arg.merged_paths = merged_paths;
6142 err = merge_files(worktree, fileindex, fileindex_path,
6143 parent_commit_id, commit_id, repo, collect_merged_paths,
6144 &cmp_arg, cancel_cb, cancel_arg);
6145 if (commit_ref)
6146 got_ref_close(commit_ref);
6147 return err;
6150 const struct got_error *
6151 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6152 struct got_worktree *worktree, struct got_fileindex *fileindex,
6153 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6154 struct got_repository *repo,
6155 got_worktree_checkout_cb progress_cb, void *progress_arg,
6156 got_cancel_cb cancel_cb, void *cancel_arg)
6158 const struct got_error *err;
6159 char *commit_ref_name;
6161 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6162 if (err)
6163 return err;
6165 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6166 if (err)
6167 goto done;
6169 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6170 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6171 progress_arg, cancel_cb, cancel_arg);
6172 done:
6173 free(commit_ref_name);
6174 return err;
6177 const struct got_error *
6178 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6179 struct got_worktree *worktree, struct got_fileindex *fileindex,
6180 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6181 struct got_repository *repo,
6182 got_worktree_checkout_cb progress_cb, void *progress_arg,
6183 got_cancel_cb cancel_cb, void *cancel_arg)
6185 const struct got_error *err;
6186 char *commit_ref_name;
6188 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6189 if (err)
6190 return err;
6192 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6193 if (err)
6194 goto done;
6196 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6197 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6198 progress_arg, cancel_cb, cancel_arg);
6199 done:
6200 free(commit_ref_name);
6201 return err;
6204 static const struct got_error *
6205 rebase_commit(struct got_object_id **new_commit_id,
6206 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6207 struct got_worktree *worktree, struct got_fileindex *fileindex,
6208 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6209 const char *new_logmsg, struct got_repository *repo)
6211 const struct got_error *err, *sync_err;
6212 struct got_pathlist_head commitable_paths;
6213 struct collect_commitables_arg cc_arg;
6214 char *fileindex_path = NULL;
6215 struct got_reference *head_ref = NULL;
6216 struct got_object_id *head_commit_id = NULL;
6217 char *logmsg = NULL;
6219 TAILQ_INIT(&commitable_paths);
6220 *new_commit_id = NULL;
6222 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6224 err = get_fileindex_path(&fileindex_path, worktree);
6225 if (err)
6226 return err;
6228 cc_arg.commitable_paths = &commitable_paths;
6229 cc_arg.worktree = worktree;
6230 cc_arg.repo = repo;
6231 cc_arg.have_staged_files = 0;
6233 * If possible get the status of individual files directly to
6234 * avoid crawling the entire work tree once per rebased commit.
6235 * TODO: Ideally, merged_paths would contain a list of commitables
6236 * we could use so we could skip worktree_status() entirely.
6238 if (merged_paths) {
6239 struct got_pathlist_entry *pe;
6240 TAILQ_FOREACH(pe, merged_paths, entry) {
6241 err = worktree_status(worktree, pe->path, fileindex,
6242 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6243 0);
6244 if (err)
6245 goto done;
6247 } else {
6248 err = worktree_status(worktree, "", fileindex, repo,
6249 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6250 if (err)
6251 goto done;
6254 if (TAILQ_EMPTY(&commitable_paths)) {
6255 /* No-op change; commit will be elided. */
6256 err = got_ref_delete(commit_ref, repo);
6257 if (err)
6258 goto done;
6259 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6260 goto done;
6263 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6264 if (err)
6265 goto done;
6267 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6268 if (err)
6269 goto done;
6271 if (new_logmsg) {
6272 logmsg = strdup(new_logmsg);
6273 if (logmsg == NULL) {
6274 err = got_error_from_errno("strdup");
6275 goto done;
6277 } else {
6278 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6279 if (err)
6280 goto done;
6283 /* NB: commit_worktree will call free(logmsg) */
6284 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6285 worktree, got_object_commit_get_author(orig_commit),
6286 got_object_commit_get_committer(orig_commit),
6287 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6288 if (err)
6289 goto done;
6291 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6292 if (err)
6293 goto done;
6295 err = got_ref_delete(commit_ref, repo);
6296 if (err)
6297 goto done;
6299 err = update_fileindex_after_commit(worktree, &commitable_paths,
6300 *new_commit_id, fileindex, 0);
6301 sync_err = sync_fileindex(fileindex, fileindex_path);
6302 if (sync_err && err == NULL)
6303 err = sync_err;
6304 done:
6305 free(fileindex_path);
6306 free(head_commit_id);
6307 if (head_ref)
6308 got_ref_close(head_ref);
6309 if (err) {
6310 free(*new_commit_id);
6311 *new_commit_id = NULL;
6313 return err;
6316 const struct got_error *
6317 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6318 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6319 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6320 struct got_commit_object *orig_commit,
6321 struct got_object_id *orig_commit_id, struct got_repository *repo)
6323 const struct got_error *err;
6324 char *commit_ref_name;
6325 struct got_reference *commit_ref = NULL;
6326 struct got_object_id *commit_id = NULL;
6328 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6329 if (err)
6330 return err;
6332 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6333 if (err)
6334 goto done;
6335 err = got_ref_resolve(&commit_id, repo, commit_ref);
6336 if (err)
6337 goto done;
6338 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6339 err = got_error(GOT_ERR_REBASE_COMMITID);
6340 goto done;
6343 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6344 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6345 done:
6346 if (commit_ref)
6347 got_ref_close(commit_ref);
6348 free(commit_ref_name);
6349 free(commit_id);
6350 return err;
6353 const struct got_error *
6354 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6355 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6356 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6357 struct got_commit_object *orig_commit,
6358 struct got_object_id *orig_commit_id, const char *new_logmsg,
6359 struct got_repository *repo)
6361 const struct got_error *err;
6362 char *commit_ref_name;
6363 struct got_reference *commit_ref = NULL;
6365 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6366 if (err)
6367 return err;
6369 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6370 if (err)
6371 goto done;
6373 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6374 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6375 done:
6376 if (commit_ref)
6377 got_ref_close(commit_ref);
6378 free(commit_ref_name);
6379 return err;
6382 const struct got_error *
6383 got_worktree_rebase_postpone(struct got_worktree *worktree,
6384 struct got_fileindex *fileindex)
6386 if (fileindex)
6387 got_fileindex_free(fileindex);
6388 return lock_worktree(worktree, LOCK_SH);
6391 static const struct got_error *
6392 delete_ref(const char *name, struct got_repository *repo)
6394 const struct got_error *err;
6395 struct got_reference *ref;
6397 err = got_ref_open(&ref, repo, name, 0);
6398 if (err) {
6399 if (err->code == GOT_ERR_NOT_REF)
6400 return NULL;
6401 return err;
6404 err = got_ref_delete(ref, repo);
6405 got_ref_close(ref);
6406 return err;
6409 static const struct got_error *
6410 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6412 const struct got_error *err;
6413 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6414 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6416 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6417 if (err)
6418 goto done;
6419 err = delete_ref(tmp_branch_name, repo);
6420 if (err)
6421 goto done;
6423 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6424 if (err)
6425 goto done;
6426 err = delete_ref(new_base_branch_ref_name, repo);
6427 if (err)
6428 goto done;
6430 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6431 if (err)
6432 goto done;
6433 err = delete_ref(branch_ref_name, repo);
6434 if (err)
6435 goto done;
6437 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6438 if (err)
6439 goto done;
6440 err = delete_ref(commit_ref_name, repo);
6441 if (err)
6442 goto done;
6444 done:
6445 free(tmp_branch_name);
6446 free(new_base_branch_ref_name);
6447 free(branch_ref_name);
6448 free(commit_ref_name);
6449 return err;
6452 const struct got_error *
6453 got_worktree_rebase_complete(struct got_worktree *worktree,
6454 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6455 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6456 struct got_repository *repo)
6458 const struct got_error *err, *unlockerr, *sync_err;
6459 struct got_object_id *new_head_commit_id = NULL;
6460 char *fileindex_path = NULL;
6462 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6463 if (err)
6464 return err;
6466 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6467 if (err)
6468 goto done;
6470 err = got_ref_write(rebased_branch, repo);
6471 if (err)
6472 goto done;
6474 err = got_worktree_set_head_ref(worktree, rebased_branch);
6475 if (err)
6476 goto done;
6478 err = delete_rebase_refs(worktree, repo);
6479 if (err)
6480 goto done;
6482 err = get_fileindex_path(&fileindex_path, worktree);
6483 if (err)
6484 goto done;
6485 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6486 sync_err = sync_fileindex(fileindex, fileindex_path);
6487 if (sync_err && err == NULL)
6488 err = sync_err;
6489 done:
6490 got_fileindex_free(fileindex);
6491 free(fileindex_path);
6492 free(new_head_commit_id);
6493 unlockerr = lock_worktree(worktree, LOCK_SH);
6494 if (unlockerr && err == NULL)
6495 err = unlockerr;
6496 return err;
6499 const struct got_error *
6500 got_worktree_rebase_abort(struct got_worktree *worktree,
6501 struct got_fileindex *fileindex, struct got_repository *repo,
6502 struct got_reference *new_base_branch,
6503 got_worktree_checkout_cb progress_cb, void *progress_arg)
6505 const struct got_error *err, *unlockerr, *sync_err;
6506 struct got_reference *resolved = NULL;
6507 struct got_object_id *commit_id = NULL;
6508 char *fileindex_path = NULL;
6509 struct revert_file_args rfa;
6510 struct got_object_id *tree_id = NULL;
6512 err = lock_worktree(worktree, LOCK_EX);
6513 if (err)
6514 return err;
6516 err = got_ref_open(&resolved, repo,
6517 got_ref_get_symref_target(new_base_branch), 0);
6518 if (err)
6519 goto done;
6521 err = got_worktree_set_head_ref(worktree, resolved);
6522 if (err)
6523 goto done;
6526 * XXX commits to the base branch could have happened while
6527 * we were busy rebasing; should we store the original commit ID
6528 * when rebase begins and read it back here?
6530 err = got_ref_resolve(&commit_id, repo, resolved);
6531 if (err)
6532 goto done;
6534 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6535 if (err)
6536 goto done;
6538 err = got_object_id_by_path(&tree_id, repo,
6539 worktree->base_commit_id, worktree->path_prefix);
6540 if (err)
6541 goto done;
6543 err = delete_rebase_refs(worktree, repo);
6544 if (err)
6545 goto done;
6547 err = get_fileindex_path(&fileindex_path, worktree);
6548 if (err)
6549 goto done;
6551 rfa.worktree = worktree;
6552 rfa.fileindex = fileindex;
6553 rfa.progress_cb = progress_cb;
6554 rfa.progress_arg = progress_arg;
6555 rfa.patch_cb = NULL;
6556 rfa.patch_arg = NULL;
6557 rfa.repo = repo;
6558 err = worktree_status(worktree, "", fileindex, repo,
6559 revert_file, &rfa, NULL, NULL, 0, 0);
6560 if (err)
6561 goto sync;
6563 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6564 repo, progress_cb, progress_arg, NULL, NULL);
6565 sync:
6566 sync_err = sync_fileindex(fileindex, fileindex_path);
6567 if (sync_err && err == NULL)
6568 err = sync_err;
6569 done:
6570 got_ref_close(resolved);
6571 free(tree_id);
6572 free(commit_id);
6573 if (fileindex)
6574 got_fileindex_free(fileindex);
6575 free(fileindex_path);
6577 unlockerr = lock_worktree(worktree, LOCK_SH);
6578 if (unlockerr && err == NULL)
6579 err = unlockerr;
6580 return err;
6583 const struct got_error *
6584 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6585 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6586 struct got_fileindex **fileindex, struct got_worktree *worktree,
6587 struct got_repository *repo)
6589 const struct got_error *err = NULL;
6590 char *tmp_branch_name = NULL;
6591 char *branch_ref_name = NULL;
6592 char *base_commit_ref_name = NULL;
6593 char *fileindex_path = NULL;
6594 struct check_rebase_ok_arg ok_arg;
6595 struct got_reference *wt_branch = NULL;
6596 struct got_reference *base_commit_ref = NULL;
6598 *tmp_branch = NULL;
6599 *branch_ref = NULL;
6600 *base_commit_id = NULL;
6601 *fileindex = NULL;
6603 err = lock_worktree(worktree, LOCK_EX);
6604 if (err)
6605 return err;
6607 err = open_fileindex(fileindex, &fileindex_path, worktree);
6608 if (err)
6609 goto done;
6611 ok_arg.worktree = worktree;
6612 ok_arg.repo = repo;
6613 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6614 &ok_arg);
6615 if (err)
6616 goto done;
6618 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6619 if (err)
6620 goto done;
6622 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6623 if (err)
6624 goto done;
6626 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6627 worktree);
6628 if (err)
6629 goto done;
6631 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6632 0);
6633 if (err)
6634 goto done;
6636 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6637 if (err)
6638 goto done;
6640 err = got_ref_write(*branch_ref, repo);
6641 if (err)
6642 goto done;
6644 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6645 worktree->base_commit_id);
6646 if (err)
6647 goto done;
6648 err = got_ref_write(base_commit_ref, repo);
6649 if (err)
6650 goto done;
6651 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6652 if (*base_commit_id == NULL) {
6653 err = got_error_from_errno("got_object_id_dup");
6654 goto done;
6657 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6658 worktree->base_commit_id);
6659 if (err)
6660 goto done;
6661 err = got_ref_write(*tmp_branch, repo);
6662 if (err)
6663 goto done;
6665 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6666 if (err)
6667 goto done;
6668 done:
6669 free(fileindex_path);
6670 free(tmp_branch_name);
6671 free(branch_ref_name);
6672 free(base_commit_ref_name);
6673 if (wt_branch)
6674 got_ref_close(wt_branch);
6675 if (err) {
6676 if (*branch_ref) {
6677 got_ref_close(*branch_ref);
6678 *branch_ref = NULL;
6680 if (*tmp_branch) {
6681 got_ref_close(*tmp_branch);
6682 *tmp_branch = NULL;
6684 free(*base_commit_id);
6685 if (*fileindex) {
6686 got_fileindex_free(*fileindex);
6687 *fileindex = NULL;
6689 lock_worktree(worktree, LOCK_SH);
6691 return err;
6694 const struct got_error *
6695 got_worktree_histedit_postpone(struct got_worktree *worktree,
6696 struct got_fileindex *fileindex)
6698 if (fileindex)
6699 got_fileindex_free(fileindex);
6700 return lock_worktree(worktree, LOCK_SH);
6703 const struct got_error *
6704 got_worktree_histedit_in_progress(int *in_progress,
6705 struct got_worktree *worktree)
6707 const struct got_error *err;
6708 char *tmp_branch_name = NULL;
6710 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6711 if (err)
6712 return err;
6714 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6715 free(tmp_branch_name);
6716 return NULL;
6719 const struct got_error *
6720 got_worktree_histedit_continue(struct got_object_id **commit_id,
6721 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6722 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6723 struct got_worktree *worktree, struct got_repository *repo)
6725 const struct got_error *err;
6726 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6727 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6728 struct got_reference *commit_ref = NULL;
6729 struct got_reference *base_commit_ref = NULL;
6730 char *fileindex_path = NULL;
6731 int have_staged_files = 0;
6733 *commit_id = NULL;
6734 *tmp_branch = NULL;
6735 *base_commit_id = NULL;
6736 *fileindex = NULL;
6738 err = lock_worktree(worktree, LOCK_EX);
6739 if (err)
6740 return err;
6742 err = open_fileindex(fileindex, &fileindex_path, worktree);
6743 if (err)
6744 goto done;
6746 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6747 &have_staged_files);
6748 if (err && err->code != GOT_ERR_CANCELLED)
6749 goto done;
6750 if (have_staged_files) {
6751 err = got_error(GOT_ERR_STAGED_PATHS);
6752 goto done;
6755 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6756 if (err)
6757 goto done;
6759 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6760 if (err)
6761 goto done;
6763 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6764 if (err)
6765 goto done;
6767 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6768 worktree);
6769 if (err)
6770 goto done;
6772 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6773 if (err)
6774 goto done;
6776 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6777 if (err)
6778 goto done;
6779 err = got_ref_resolve(commit_id, repo, commit_ref);
6780 if (err)
6781 goto done;
6783 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6784 if (err)
6785 goto done;
6786 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6787 if (err)
6788 goto done;
6790 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6791 if (err)
6792 goto done;
6793 done:
6794 free(commit_ref_name);
6795 free(branch_ref_name);
6796 free(fileindex_path);
6797 if (commit_ref)
6798 got_ref_close(commit_ref);
6799 if (base_commit_ref)
6800 got_ref_close(base_commit_ref);
6801 if (err) {
6802 free(*commit_id);
6803 *commit_id = NULL;
6804 free(*base_commit_id);
6805 *base_commit_id = NULL;
6806 if (*tmp_branch) {
6807 got_ref_close(*tmp_branch);
6808 *tmp_branch = NULL;
6810 if (*fileindex) {
6811 got_fileindex_free(*fileindex);
6812 *fileindex = NULL;
6814 lock_worktree(worktree, LOCK_EX);
6816 return err;
6819 static const struct got_error *
6820 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6822 const struct got_error *err;
6823 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6824 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6826 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6827 if (err)
6828 goto done;
6829 err = delete_ref(tmp_branch_name, repo);
6830 if (err)
6831 goto done;
6833 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6834 worktree);
6835 if (err)
6836 goto done;
6837 err = delete_ref(base_commit_ref_name, repo);
6838 if (err)
6839 goto done;
6841 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6842 if (err)
6843 goto done;
6844 err = delete_ref(branch_ref_name, repo);
6845 if (err)
6846 goto done;
6848 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6849 if (err)
6850 goto done;
6851 err = delete_ref(commit_ref_name, repo);
6852 if (err)
6853 goto done;
6854 done:
6855 free(tmp_branch_name);
6856 free(base_commit_ref_name);
6857 free(branch_ref_name);
6858 free(commit_ref_name);
6859 return err;
6862 const struct got_error *
6863 got_worktree_histedit_abort(struct got_worktree *worktree,
6864 struct got_fileindex *fileindex, struct got_repository *repo,
6865 struct got_reference *branch, struct got_object_id *base_commit_id,
6866 got_worktree_checkout_cb progress_cb, void *progress_arg)
6868 const struct got_error *err, *unlockerr, *sync_err;
6869 struct got_reference *resolved = NULL;
6870 char *fileindex_path = NULL;
6871 struct got_object_id *tree_id = NULL;
6872 struct revert_file_args rfa;
6874 err = lock_worktree(worktree, LOCK_EX);
6875 if (err)
6876 return err;
6878 err = got_ref_open(&resolved, repo,
6879 got_ref_get_symref_target(branch), 0);
6880 if (err)
6881 goto done;
6883 err = got_worktree_set_head_ref(worktree, resolved);
6884 if (err)
6885 goto done;
6887 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6888 if (err)
6889 goto done;
6891 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6892 worktree->path_prefix);
6893 if (err)
6894 goto done;
6896 err = delete_histedit_refs(worktree, repo);
6897 if (err)
6898 goto done;
6900 err = get_fileindex_path(&fileindex_path, worktree);
6901 if (err)
6902 goto done;
6904 rfa.worktree = worktree;
6905 rfa.fileindex = fileindex;
6906 rfa.progress_cb = progress_cb;
6907 rfa.progress_arg = progress_arg;
6908 rfa.patch_cb = NULL;
6909 rfa.patch_arg = NULL;
6910 rfa.repo = repo;
6911 err = worktree_status(worktree, "", fileindex, repo,
6912 revert_file, &rfa, NULL, NULL, 0, 0);
6913 if (err)
6914 goto sync;
6916 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6917 repo, progress_cb, progress_arg, NULL, NULL);
6918 sync:
6919 sync_err = sync_fileindex(fileindex, fileindex_path);
6920 if (sync_err && err == NULL)
6921 err = sync_err;
6922 done:
6923 got_ref_close(resolved);
6924 free(tree_id);
6925 free(fileindex_path);
6927 unlockerr = lock_worktree(worktree, LOCK_SH);
6928 if (unlockerr && err == NULL)
6929 err = unlockerr;
6930 return err;
6933 const struct got_error *
6934 got_worktree_histedit_complete(struct got_worktree *worktree,
6935 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6936 struct got_reference *edited_branch, struct got_repository *repo)
6938 const struct got_error *err, *unlockerr, *sync_err;
6939 struct got_object_id *new_head_commit_id = NULL;
6940 struct got_reference *resolved = NULL;
6941 char *fileindex_path = NULL;
6943 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6944 if (err)
6945 return err;
6947 err = got_ref_open(&resolved, repo,
6948 got_ref_get_symref_target(edited_branch), 0);
6949 if (err)
6950 goto done;
6952 err = got_ref_change_ref(resolved, new_head_commit_id);
6953 if (err)
6954 goto done;
6956 err = got_ref_write(resolved, repo);
6957 if (err)
6958 goto done;
6960 err = got_worktree_set_head_ref(worktree, resolved);
6961 if (err)
6962 goto done;
6964 err = delete_histedit_refs(worktree, repo);
6965 if (err)
6966 goto done;
6968 err = get_fileindex_path(&fileindex_path, worktree);
6969 if (err)
6970 goto done;
6971 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6972 sync_err = sync_fileindex(fileindex, fileindex_path);
6973 if (sync_err && err == NULL)
6974 err = sync_err;
6975 done:
6976 got_fileindex_free(fileindex);
6977 free(fileindex_path);
6978 free(new_head_commit_id);
6979 unlockerr = lock_worktree(worktree, LOCK_SH);
6980 if (unlockerr && err == NULL)
6981 err = unlockerr;
6982 return err;
6985 const struct got_error *
6986 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6987 struct got_object_id *commit_id, struct got_repository *repo)
6989 const struct got_error *err;
6990 char *commit_ref_name;
6992 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6993 if (err)
6994 return err;
6996 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6997 if (err)
6998 goto done;
7000 err = delete_ref(commit_ref_name, repo);
7001 done:
7002 free(commit_ref_name);
7003 return err;
7006 const struct got_error *
7007 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7008 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7009 struct got_worktree *worktree, const char *refname,
7010 struct got_repository *repo)
7012 const struct got_error *err = NULL;
7013 char *fileindex_path = NULL;
7014 struct check_rebase_ok_arg ok_arg;
7016 *fileindex = NULL;
7017 *branch_ref = NULL;
7018 *base_branch_ref = NULL;
7020 err = lock_worktree(worktree, LOCK_EX);
7021 if (err)
7022 return err;
7024 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7025 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7026 "cannot integrate a branch into itself; "
7027 "update -b or different branch name required");
7028 goto done;
7031 err = open_fileindex(fileindex, &fileindex_path, worktree);
7032 if (err)
7033 goto done;
7035 /* Preconditions are the same as for rebase. */
7036 ok_arg.worktree = worktree;
7037 ok_arg.repo = repo;
7038 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7039 &ok_arg);
7040 if (err)
7041 goto done;
7043 err = got_ref_open(branch_ref, repo, refname, 1);
7044 if (err)
7045 goto done;
7047 err = got_ref_open(base_branch_ref, repo,
7048 got_worktree_get_head_ref_name(worktree), 1);
7049 done:
7050 if (err) {
7051 if (*branch_ref) {
7052 got_ref_close(*branch_ref);
7053 *branch_ref = NULL;
7055 if (*base_branch_ref) {
7056 got_ref_close(*base_branch_ref);
7057 *base_branch_ref = NULL;
7059 if (*fileindex) {
7060 got_fileindex_free(*fileindex);
7061 *fileindex = NULL;
7063 lock_worktree(worktree, LOCK_SH);
7065 return err;
7068 const struct got_error *
7069 got_worktree_integrate_continue(struct got_worktree *worktree,
7070 struct got_fileindex *fileindex, struct got_repository *repo,
7071 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7072 got_worktree_checkout_cb progress_cb, void *progress_arg,
7073 got_cancel_cb cancel_cb, void *cancel_arg)
7075 const struct got_error *err = NULL, *sync_err, *unlockerr;
7076 char *fileindex_path = NULL;
7077 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7079 err = get_fileindex_path(&fileindex_path, worktree);
7080 if (err)
7081 goto done;
7083 err = got_ref_resolve(&commit_id, repo, branch_ref);
7084 if (err)
7085 goto done;
7087 err = got_object_id_by_path(&tree_id, repo, commit_id,
7088 worktree->path_prefix);
7089 if (err)
7090 goto done;
7092 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7093 if (err)
7094 goto done;
7096 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7097 progress_cb, progress_arg, cancel_cb, cancel_arg);
7098 if (err)
7099 goto sync;
7101 err = got_ref_change_ref(base_branch_ref, commit_id);
7102 if (err)
7103 goto sync;
7105 err = got_ref_write(base_branch_ref, repo);
7106 sync:
7107 sync_err = sync_fileindex(fileindex, fileindex_path);
7108 if (sync_err && err == NULL)
7109 err = sync_err;
7111 done:
7112 unlockerr = got_ref_unlock(branch_ref);
7113 if (unlockerr && err == NULL)
7114 err = unlockerr;
7115 got_ref_close(branch_ref);
7117 unlockerr = got_ref_unlock(base_branch_ref);
7118 if (unlockerr && err == NULL)
7119 err = unlockerr;
7120 got_ref_close(base_branch_ref);
7122 got_fileindex_free(fileindex);
7123 free(fileindex_path);
7124 free(tree_id);
7126 unlockerr = lock_worktree(worktree, LOCK_SH);
7127 if (unlockerr && err == NULL)
7128 err = unlockerr;
7129 return err;
7132 const struct got_error *
7133 got_worktree_integrate_abort(struct got_worktree *worktree,
7134 struct got_fileindex *fileindex, struct got_repository *repo,
7135 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7137 const struct got_error *err = NULL, *unlockerr = NULL;
7139 got_fileindex_free(fileindex);
7141 err = lock_worktree(worktree, LOCK_SH);
7143 unlockerr = got_ref_unlock(branch_ref);
7144 if (unlockerr && err == NULL)
7145 err = unlockerr;
7146 got_ref_close(branch_ref);
7148 unlockerr = got_ref_unlock(base_branch_ref);
7149 if (unlockerr && err == NULL)
7150 err = unlockerr;
7151 got_ref_close(base_branch_ref);
7153 return err;
7156 struct check_stage_ok_arg {
7157 struct got_object_id *head_commit_id;
7158 struct got_worktree *worktree;
7159 struct got_fileindex *fileindex;
7160 struct got_repository *repo;
7161 int have_changes;
7164 const struct got_error *
7165 check_stage_ok(void *arg, unsigned char status,
7166 unsigned char staged_status, const char *relpath,
7167 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7168 struct got_object_id *commit_id, int dirfd, const char *de_name)
7170 struct check_stage_ok_arg *a = arg;
7171 const struct got_error *err = NULL;
7172 struct got_fileindex_entry *ie;
7173 struct got_object_id base_commit_id;
7174 struct got_object_id *base_commit_idp = NULL;
7175 char *in_repo_path = NULL, *p;
7177 if (status == GOT_STATUS_UNVERSIONED ||
7178 status == GOT_STATUS_NO_CHANGE)
7179 return NULL;
7180 if (status == GOT_STATUS_NONEXISTENT)
7181 return got_error_set_errno(ENOENT, relpath);
7183 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7184 if (ie == NULL)
7185 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7187 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7188 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7189 relpath) == -1)
7190 return got_error_from_errno("asprintf");
7192 if (got_fileindex_entry_has_commit(ie)) {
7193 memcpy(base_commit_id.sha1, ie->commit_sha1,
7194 SHA1_DIGEST_LENGTH);
7195 base_commit_idp = &base_commit_id;
7198 if (status == GOT_STATUS_CONFLICT) {
7199 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7200 goto done;
7201 } else if (status != GOT_STATUS_ADD &&
7202 status != GOT_STATUS_MODIFY &&
7203 status != GOT_STATUS_DELETE) {
7204 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7205 goto done;
7208 a->have_changes = 1;
7210 p = in_repo_path;
7211 while (p[0] == '/')
7212 p++;
7213 err = check_out_of_date(p, status, staged_status,
7214 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7215 GOT_ERR_STAGE_OUT_OF_DATE);
7216 done:
7217 free(in_repo_path);
7218 return err;
7221 struct stage_path_arg {
7222 struct got_worktree *worktree;
7223 struct got_fileindex *fileindex;
7224 struct got_repository *repo;
7225 got_worktree_status_cb status_cb;
7226 void *status_arg;
7227 got_worktree_patch_cb patch_cb;
7228 void *patch_arg;
7229 int staged_something;
7230 int allow_bad_symlinks;
7233 static const struct got_error *
7234 stage_path(void *arg, unsigned char status,
7235 unsigned char staged_status, const char *relpath,
7236 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7237 struct got_object_id *commit_id, int dirfd, const char *de_name)
7239 struct stage_path_arg *a = arg;
7240 const struct got_error *err = NULL;
7241 struct got_fileindex_entry *ie;
7242 char *ondisk_path = NULL, *path_content = NULL;
7243 uint32_t stage;
7244 struct got_object_id *new_staged_blob_id = NULL;
7245 struct stat sb;
7247 if (status == GOT_STATUS_UNVERSIONED)
7248 return NULL;
7250 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7251 if (ie == NULL)
7252 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7254 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7255 relpath)== -1)
7256 return got_error_from_errno("asprintf");
7258 switch (status) {
7259 case GOT_STATUS_ADD:
7260 case GOT_STATUS_MODIFY:
7261 /* XXX could sb.st_mode be passed in by our caller? */
7262 if (lstat(ondisk_path, &sb) == -1) {
7263 err = got_error_from_errno2("lstat", ondisk_path);
7264 break;
7266 if (a->patch_cb) {
7267 if (status == GOT_STATUS_ADD) {
7268 int choice = GOT_PATCH_CHOICE_NONE;
7269 err = (*a->patch_cb)(&choice, a->patch_arg,
7270 status, ie->path, NULL, 1, 1);
7271 if (err)
7272 break;
7273 if (choice != GOT_PATCH_CHOICE_YES)
7274 break;
7275 } else {
7276 err = create_patched_content(&path_content, 0,
7277 staged_blob_id ? staged_blob_id : blob_id,
7278 ondisk_path, dirfd, de_name, ie->path,
7279 a->repo, a->patch_cb, a->patch_arg);
7280 if (err || path_content == NULL)
7281 break;
7284 err = got_object_blob_create(&new_staged_blob_id,
7285 path_content ? path_content : ondisk_path, a->repo);
7286 if (err)
7287 break;
7288 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7289 SHA1_DIGEST_LENGTH);
7290 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7291 stage = GOT_FILEIDX_STAGE_ADD;
7292 else
7293 stage = GOT_FILEIDX_STAGE_MODIFY;
7294 got_fileindex_entry_stage_set(ie, stage);
7295 if (S_ISLNK(sb.st_mode)) {
7296 int is_bad_symlink = 0;
7297 if (!a->allow_bad_symlinks) {
7298 char target_path[PATH_MAX];
7299 ssize_t target_len;
7300 target_len = readlink(ondisk_path, target_path,
7301 sizeof(target_path));
7302 if (target_len == -1) {
7303 err = got_error_from_errno2("readlink",
7304 ondisk_path);
7305 break;
7307 err = is_bad_symlink_target(&is_bad_symlink,
7308 target_path, target_len, ondisk_path,
7309 a->worktree->root_path);
7310 if (err)
7311 break;
7312 if (is_bad_symlink) {
7313 err = got_error_path(ondisk_path,
7314 GOT_ERR_BAD_SYMLINK);
7315 break;
7318 if (is_bad_symlink)
7319 got_fileindex_entry_staged_filetype_set(ie,
7320 GOT_FILEIDX_MODE_BAD_SYMLINK);
7321 else
7322 got_fileindex_entry_staged_filetype_set(ie,
7323 GOT_FILEIDX_MODE_SYMLINK);
7324 } else {
7325 got_fileindex_entry_staged_filetype_set(ie,
7326 GOT_FILEIDX_MODE_REGULAR_FILE);
7328 a->staged_something = 1;
7329 if (a->status_cb == NULL)
7330 break;
7331 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7332 get_staged_status(ie), relpath, blob_id,
7333 new_staged_blob_id, NULL, dirfd, de_name);
7334 break;
7335 case GOT_STATUS_DELETE:
7336 if (staged_status == GOT_STATUS_DELETE)
7337 break;
7338 if (a->patch_cb) {
7339 int choice = GOT_PATCH_CHOICE_NONE;
7340 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7341 ie->path, NULL, 1, 1);
7342 if (err)
7343 break;
7344 if (choice == GOT_PATCH_CHOICE_NO)
7345 break;
7346 if (choice != GOT_PATCH_CHOICE_YES) {
7347 err = got_error(GOT_ERR_PATCH_CHOICE);
7348 break;
7351 stage = GOT_FILEIDX_STAGE_DELETE;
7352 got_fileindex_entry_stage_set(ie, stage);
7353 a->staged_something = 1;
7354 if (a->status_cb == NULL)
7355 break;
7356 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7357 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7358 de_name);
7359 break;
7360 case GOT_STATUS_NO_CHANGE:
7361 break;
7362 case GOT_STATUS_CONFLICT:
7363 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7364 break;
7365 case GOT_STATUS_NONEXISTENT:
7366 err = got_error_set_errno(ENOENT, relpath);
7367 break;
7368 default:
7369 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7370 break;
7373 if (path_content && unlink(path_content) == -1 && err == NULL)
7374 err = got_error_from_errno2("unlink", path_content);
7375 free(path_content);
7376 free(ondisk_path);
7377 free(new_staged_blob_id);
7378 return err;
7381 const struct got_error *
7382 got_worktree_stage(struct got_worktree *worktree,
7383 struct got_pathlist_head *paths,
7384 got_worktree_status_cb status_cb, void *status_arg,
7385 got_worktree_patch_cb patch_cb, void *patch_arg,
7386 int allow_bad_symlinks, struct got_repository *repo)
7388 const struct got_error *err = NULL, *sync_err, *unlockerr;
7389 struct got_pathlist_entry *pe;
7390 struct got_fileindex *fileindex = NULL;
7391 char *fileindex_path = NULL;
7392 struct got_reference *head_ref = NULL;
7393 struct got_object_id *head_commit_id = NULL;
7394 struct check_stage_ok_arg oka;
7395 struct stage_path_arg spa;
7397 err = lock_worktree(worktree, LOCK_EX);
7398 if (err)
7399 return err;
7401 err = got_ref_open(&head_ref, repo,
7402 got_worktree_get_head_ref_name(worktree), 0);
7403 if (err)
7404 goto done;
7405 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7406 if (err)
7407 goto done;
7408 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7409 if (err)
7410 goto done;
7412 /* Check pre-conditions before staging anything. */
7413 oka.head_commit_id = head_commit_id;
7414 oka.worktree = worktree;
7415 oka.fileindex = fileindex;
7416 oka.repo = repo;
7417 oka.have_changes = 0;
7418 TAILQ_FOREACH(pe, paths, entry) {
7419 err = worktree_status(worktree, pe->path, fileindex, repo,
7420 check_stage_ok, &oka, NULL, NULL, 0, 0);
7421 if (err)
7422 goto done;
7424 if (!oka.have_changes) {
7425 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7426 goto done;
7429 spa.worktree = worktree;
7430 spa.fileindex = fileindex;
7431 spa.repo = repo;
7432 spa.patch_cb = patch_cb;
7433 spa.patch_arg = patch_arg;
7434 spa.status_cb = status_cb;
7435 spa.status_arg = status_arg;
7436 spa.staged_something = 0;
7437 spa.allow_bad_symlinks = allow_bad_symlinks;
7438 TAILQ_FOREACH(pe, paths, entry) {
7439 err = worktree_status(worktree, pe->path, fileindex, repo,
7440 stage_path, &spa, NULL, NULL, 0, 0);
7441 if (err)
7442 goto done;
7444 if (!spa.staged_something) {
7445 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7446 goto done;
7449 sync_err = sync_fileindex(fileindex, fileindex_path);
7450 if (sync_err && err == NULL)
7451 err = sync_err;
7452 done:
7453 if (head_ref)
7454 got_ref_close(head_ref);
7455 free(head_commit_id);
7456 free(fileindex_path);
7457 if (fileindex)
7458 got_fileindex_free(fileindex);
7459 unlockerr = lock_worktree(worktree, LOCK_SH);
7460 if (unlockerr && err == NULL)
7461 err = unlockerr;
7462 return err;
7465 struct unstage_path_arg {
7466 struct got_worktree *worktree;
7467 struct got_fileindex *fileindex;
7468 struct got_repository *repo;
7469 got_worktree_checkout_cb progress_cb;
7470 void *progress_arg;
7471 got_worktree_patch_cb patch_cb;
7472 void *patch_arg;
7475 static const struct got_error *
7476 create_unstaged_content(char **path_unstaged_content,
7477 char **path_new_staged_content, struct got_object_id *blob_id,
7478 struct got_object_id *staged_blob_id, const char *relpath,
7479 struct got_repository *repo,
7480 got_worktree_patch_cb patch_cb, void *patch_arg)
7482 const struct got_error *err, *free_err;
7483 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7484 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7485 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7486 struct got_diffreg_result *diffreg_result = NULL;
7487 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7488 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7490 *path_unstaged_content = NULL;
7491 *path_new_staged_content = NULL;
7493 err = got_object_id_str(&label1, blob_id);
7494 if (err)
7495 return err;
7496 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7497 if (err)
7498 goto done;
7500 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7501 if (err)
7502 goto done;
7504 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7505 if (err)
7506 goto done;
7508 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7509 if (err)
7510 goto done;
7512 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7513 if (err)
7514 goto done;
7516 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7517 if (err)
7518 goto done;
7520 err = got_diff_files(&diffreg_result, f1, label1, f2,
7521 path2, 3, 0, 1, NULL);
7522 if (err)
7523 goto done;
7525 err = got_opentemp_named(path_unstaged_content, &outfile,
7526 "got-unstaged-content");
7527 if (err)
7528 goto done;
7529 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7530 "got-new-staged-content");
7531 if (err)
7532 goto done;
7534 if (fseek(f1, 0L, SEEK_SET) == -1) {
7535 err = got_ferror(f1, GOT_ERR_IO);
7536 goto done;
7538 if (fseek(f2, 0L, SEEK_SET) == -1) {
7539 err = got_ferror(f2, GOT_ERR_IO);
7540 goto done;
7542 /* Count the number of actual changes in the diff result. */
7543 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7544 struct diff_chunk_context cc = {};
7545 diff_chunk_context_load_change(&cc, &nchunks_used,
7546 diffreg_result->result, n, 0);
7547 nchanges++;
7549 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7550 int choice;
7551 err = apply_or_reject_change(&choice, &nchunks_used,
7552 diffreg_result->result, n, relpath, f1, f2,
7553 &line_cur1, &line_cur2,
7554 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7555 if (err)
7556 goto done;
7557 if (choice == GOT_PATCH_CHOICE_YES)
7558 have_content = 1;
7559 else
7560 have_rejected_content = 1;
7561 if (choice == GOT_PATCH_CHOICE_QUIT)
7562 break;
7564 if (have_content || have_rejected_content)
7565 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7566 outfile, rejectfile);
7567 done:
7568 free(label1);
7569 if (blob)
7570 got_object_blob_close(blob);
7571 if (staged_blob)
7572 got_object_blob_close(staged_blob);
7573 free_err = got_diffreg_result_free(diffreg_result);
7574 if (free_err && err == NULL)
7575 err = free_err;
7576 if (f1 && fclose(f1) == EOF && err == NULL)
7577 err = got_error_from_errno2("fclose", path1);
7578 if (f2 && fclose(f2) == EOF && err == NULL)
7579 err = got_error_from_errno2("fclose", path2);
7580 if (outfile && fclose(outfile) == EOF && err == NULL)
7581 err = got_error_from_errno2("fclose", *path_unstaged_content);
7582 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7583 err = got_error_from_errno2("fclose", *path_new_staged_content);
7584 if (path1 && unlink(path1) == -1 && err == NULL)
7585 err = got_error_from_errno2("unlink", path1);
7586 if (path2 && unlink(path2) == -1 && err == NULL)
7587 err = got_error_from_errno2("unlink", path2);
7588 if (err || !have_content) {
7589 if (*path_unstaged_content &&
7590 unlink(*path_unstaged_content) == -1 && err == NULL)
7591 err = got_error_from_errno2("unlink",
7592 *path_unstaged_content);
7593 free(*path_unstaged_content);
7594 *path_unstaged_content = NULL;
7596 if (err || !have_content || !have_rejected_content) {
7597 if (*path_new_staged_content &&
7598 unlink(*path_new_staged_content) == -1 && err == NULL)
7599 err = got_error_from_errno2("unlink",
7600 *path_new_staged_content);
7601 free(*path_new_staged_content);
7602 *path_new_staged_content = NULL;
7604 free(path1);
7605 free(path2);
7606 return err;
7609 static const struct got_error *
7610 unstage_hunks(struct got_object_id *staged_blob_id,
7611 struct got_blob_object *blob_base,
7612 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7613 const char *ondisk_path, const char *label_orig,
7614 struct got_worktree *worktree, struct got_repository *repo,
7615 got_worktree_patch_cb patch_cb, void *patch_arg,
7616 got_worktree_checkout_cb progress_cb, void *progress_arg)
7618 const struct got_error *err = NULL;
7619 char *path_unstaged_content = NULL;
7620 char *path_new_staged_content = NULL;
7621 struct got_object_id *new_staged_blob_id = NULL;
7622 FILE *f = NULL;
7623 struct stat sb;
7625 err = create_unstaged_content(&path_unstaged_content,
7626 &path_new_staged_content, blob_id, staged_blob_id,
7627 ie->path, repo, patch_cb, patch_arg);
7628 if (err)
7629 return err;
7631 if (path_unstaged_content == NULL)
7632 return NULL;
7634 if (path_new_staged_content) {
7635 err = got_object_blob_create(&new_staged_blob_id,
7636 path_new_staged_content, repo);
7637 if (err)
7638 goto done;
7641 f = fopen(path_unstaged_content, "r");
7642 if (f == NULL) {
7643 err = got_error_from_errno2("fopen",
7644 path_unstaged_content);
7645 goto done;
7647 if (fstat(fileno(f), &sb) == -1) {
7648 err = got_error_from_errno2("fstat", path_unstaged_content);
7649 goto done;
7651 if (got_fileindex_entry_staged_filetype_get(ie) ==
7652 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7653 char link_target[PATH_MAX];
7654 size_t r;
7655 r = fread(link_target, 1, sizeof(link_target), f);
7656 if (r == 0 && ferror(f)) {
7657 err = got_error_from_errno("fread");
7658 goto done;
7660 if (r >= sizeof(link_target)) { /* should not happen */
7661 err = got_error(GOT_ERR_NO_SPACE);
7662 goto done;
7664 link_target[r] = '\0';
7665 err = merge_symlink(worktree, blob_base,
7666 ondisk_path, ie->path, label_orig, link_target,
7667 worktree->base_commit_id, repo, progress_cb,
7668 progress_arg);
7669 } else {
7670 int local_changes_subsumed;
7671 err = merge_file(&local_changes_subsumed, worktree,
7672 blob_base, ondisk_path, ie->path,
7673 got_fileindex_perms_to_st(ie),
7674 path_unstaged_content, label_orig, "unstaged",
7675 repo, progress_cb, progress_arg);
7677 if (err)
7678 goto done;
7680 if (new_staged_blob_id) {
7681 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7682 SHA1_DIGEST_LENGTH);
7683 } else {
7684 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7685 got_fileindex_entry_staged_filetype_set(ie, 0);
7687 done:
7688 free(new_staged_blob_id);
7689 if (path_unstaged_content &&
7690 unlink(path_unstaged_content) == -1 && err == NULL)
7691 err = got_error_from_errno2("unlink", path_unstaged_content);
7692 if (path_new_staged_content &&
7693 unlink(path_new_staged_content) == -1 && err == NULL)
7694 err = got_error_from_errno2("unlink", path_new_staged_content);
7695 if (f && fclose(f) != 0 && err == NULL)
7696 err = got_error_from_errno2("fclose", path_unstaged_content);
7697 free(path_unstaged_content);
7698 free(path_new_staged_content);
7699 return err;
7702 static const struct got_error *
7703 unstage_path(void *arg, unsigned char status,
7704 unsigned char staged_status, const char *relpath,
7705 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7706 struct got_object_id *commit_id, int dirfd, const char *de_name)
7708 const struct got_error *err = NULL;
7709 struct unstage_path_arg *a = arg;
7710 struct got_fileindex_entry *ie;
7711 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7712 char *ondisk_path = NULL;
7713 char *id_str = NULL, *label_orig = NULL;
7714 int local_changes_subsumed;
7715 struct stat sb;
7717 if (staged_status != GOT_STATUS_ADD &&
7718 staged_status != GOT_STATUS_MODIFY &&
7719 staged_status != GOT_STATUS_DELETE)
7720 return NULL;
7722 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7723 if (ie == NULL)
7724 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7726 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7727 == -1)
7728 return got_error_from_errno("asprintf");
7730 err = got_object_id_str(&id_str,
7731 commit_id ? commit_id : a->worktree->base_commit_id);
7732 if (err)
7733 goto done;
7734 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7735 id_str) == -1) {
7736 err = got_error_from_errno("asprintf");
7737 goto done;
7740 switch (staged_status) {
7741 case GOT_STATUS_MODIFY:
7742 err = got_object_open_as_blob(&blob_base, a->repo,
7743 blob_id, 8192);
7744 if (err)
7745 break;
7746 /* fall through */
7747 case GOT_STATUS_ADD:
7748 if (a->patch_cb) {
7749 if (staged_status == GOT_STATUS_ADD) {
7750 int choice = GOT_PATCH_CHOICE_NONE;
7751 err = (*a->patch_cb)(&choice, a->patch_arg,
7752 staged_status, ie->path, NULL, 1, 1);
7753 if (err)
7754 break;
7755 if (choice != GOT_PATCH_CHOICE_YES)
7756 break;
7757 } else {
7758 err = unstage_hunks(staged_blob_id,
7759 blob_base, blob_id, ie, ondisk_path,
7760 label_orig, a->worktree, a->repo,
7761 a->patch_cb, a->patch_arg,
7762 a->progress_cb, a->progress_arg);
7763 break; /* Done with this file. */
7766 err = got_object_open_as_blob(&blob_staged, a->repo,
7767 staged_blob_id, 8192);
7768 if (err)
7769 break;
7770 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7771 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7772 case GOT_FILEIDX_MODE_REGULAR_FILE:
7773 err = merge_blob(&local_changes_subsumed, a->worktree,
7774 blob_base, ondisk_path, relpath,
7775 got_fileindex_perms_to_st(ie), label_orig,
7776 blob_staged, commit_id ? commit_id :
7777 a->worktree->base_commit_id, a->repo,
7778 a->progress_cb, a->progress_arg);
7779 break;
7780 case GOT_FILEIDX_MODE_SYMLINK:
7781 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7782 char *staged_target;
7783 err = got_object_blob_read_to_str(
7784 &staged_target, blob_staged);
7785 if (err)
7786 goto done;
7787 err = merge_symlink(a->worktree, blob_base,
7788 ondisk_path, relpath, label_orig,
7789 staged_target, commit_id ? commit_id :
7790 a->worktree->base_commit_id,
7791 a->repo, a->progress_cb, a->progress_arg);
7792 free(staged_target);
7793 } else {
7794 err = merge_blob(&local_changes_subsumed,
7795 a->worktree, blob_base, ondisk_path,
7796 relpath, got_fileindex_perms_to_st(ie),
7797 label_orig, blob_staged,
7798 commit_id ? commit_id :
7799 a->worktree->base_commit_id, a->repo,
7800 a->progress_cb, a->progress_arg);
7802 break;
7803 default:
7804 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7805 break;
7807 if (err == NULL) {
7808 got_fileindex_entry_stage_set(ie,
7809 GOT_FILEIDX_STAGE_NONE);
7810 got_fileindex_entry_staged_filetype_set(ie, 0);
7812 break;
7813 case GOT_STATUS_DELETE:
7814 if (a->patch_cb) {
7815 int choice = GOT_PATCH_CHOICE_NONE;
7816 err = (*a->patch_cb)(&choice, a->patch_arg,
7817 staged_status, ie->path, NULL, 1, 1);
7818 if (err)
7819 break;
7820 if (choice == GOT_PATCH_CHOICE_NO)
7821 break;
7822 if (choice != GOT_PATCH_CHOICE_YES) {
7823 err = got_error(GOT_ERR_PATCH_CHOICE);
7824 break;
7827 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7828 got_fileindex_entry_staged_filetype_set(ie, 0);
7829 err = get_file_status(&status, &sb, ie, ondisk_path,
7830 dirfd, de_name, a->repo);
7831 if (err)
7832 break;
7833 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7834 break;
7836 done:
7837 free(ondisk_path);
7838 if (blob_base)
7839 got_object_blob_close(blob_base);
7840 if (blob_staged)
7841 got_object_blob_close(blob_staged);
7842 free(id_str);
7843 free(label_orig);
7844 return err;
7847 const struct got_error *
7848 got_worktree_unstage(struct got_worktree *worktree,
7849 struct got_pathlist_head *paths,
7850 got_worktree_checkout_cb progress_cb, void *progress_arg,
7851 got_worktree_patch_cb patch_cb, void *patch_arg,
7852 struct got_repository *repo)
7854 const struct got_error *err = NULL, *sync_err, *unlockerr;
7855 struct got_pathlist_entry *pe;
7856 struct got_fileindex *fileindex = NULL;
7857 char *fileindex_path = NULL;
7858 struct unstage_path_arg upa;
7860 err = lock_worktree(worktree, LOCK_EX);
7861 if (err)
7862 return err;
7864 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7865 if (err)
7866 goto done;
7868 upa.worktree = worktree;
7869 upa.fileindex = fileindex;
7870 upa.repo = repo;
7871 upa.progress_cb = progress_cb;
7872 upa.progress_arg = progress_arg;
7873 upa.patch_cb = patch_cb;
7874 upa.patch_arg = patch_arg;
7875 TAILQ_FOREACH(pe, paths, entry) {
7876 err = worktree_status(worktree, pe->path, fileindex, repo,
7877 unstage_path, &upa, NULL, NULL, 0, 0);
7878 if (err)
7879 goto done;
7882 sync_err = sync_fileindex(fileindex, fileindex_path);
7883 if (sync_err && err == NULL)
7884 err = sync_err;
7885 done:
7886 free(fileindex_path);
7887 if (fileindex)
7888 got_fileindex_free(fileindex);
7889 unlockerr = lock_worktree(worktree, LOCK_SH);
7890 if (unlockerr && err == NULL)
7891 err = unlockerr;
7892 return err;
7895 struct report_file_info_arg {
7896 struct got_worktree *worktree;
7897 got_worktree_path_info_cb info_cb;
7898 void *info_arg;
7899 struct got_pathlist_head *paths;
7900 got_cancel_cb cancel_cb;
7901 void *cancel_arg;
7904 static const struct got_error *
7905 report_file_info(void *arg, struct got_fileindex_entry *ie)
7907 struct report_file_info_arg *a = arg;
7908 struct got_pathlist_entry *pe;
7909 struct got_object_id blob_id, staged_blob_id, commit_id;
7910 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7911 struct got_object_id *commit_idp = NULL;
7912 int stage;
7914 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7915 return got_error(GOT_ERR_CANCELLED);
7917 TAILQ_FOREACH(pe, a->paths, entry) {
7918 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7919 got_path_is_child(ie->path, pe->path, pe->path_len))
7920 break;
7922 if (pe == NULL) /* not found */
7923 return NULL;
7925 if (got_fileindex_entry_has_blob(ie)) {
7926 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7927 blob_idp = &blob_id;
7929 stage = got_fileindex_entry_stage_get(ie);
7930 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7931 stage == GOT_FILEIDX_STAGE_ADD) {
7932 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7933 SHA1_DIGEST_LENGTH);
7934 staged_blob_idp = &staged_blob_id;
7937 if (got_fileindex_entry_has_commit(ie)) {
7938 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7939 commit_idp = &commit_id;
7942 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7943 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7946 const struct got_error *
7947 got_worktree_path_info(struct got_worktree *worktree,
7948 struct got_pathlist_head *paths,
7949 got_worktree_path_info_cb info_cb, void *info_arg,
7950 got_cancel_cb cancel_cb, void *cancel_arg)
7953 const struct got_error *err = NULL, *unlockerr;
7954 struct got_fileindex *fileindex = NULL;
7955 char *fileindex_path = NULL;
7956 struct report_file_info_arg arg;
7958 err = lock_worktree(worktree, LOCK_SH);
7959 if (err)
7960 return err;
7962 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7963 if (err)
7964 goto done;
7966 arg.worktree = worktree;
7967 arg.info_cb = info_cb;
7968 arg.info_arg = info_arg;
7969 arg.paths = paths;
7970 arg.cancel_cb = cancel_cb;
7971 arg.cancel_arg = cancel_arg;
7972 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7973 &arg);
7974 done:
7975 free(fileindex_path);
7976 if (fileindex)
7977 got_fileindex_free(fileindex);
7978 unlockerr = lock_worktree(worktree, LOCK_UN);
7979 if (unlockerr && err == NULL)
7980 err = unlockerr;
7981 return err;