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) == EOF && 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;
503 if (worktree->lockfd != -1) {
504 if (close(worktree->lockfd) == -1)
505 err = got_error_from_errno2("close",
506 got_worktree_get_root_path(worktree));
508 if (close(worktree->root_fd) == -1 && err == NULL)
509 err = got_error_from_errno2("close",
510 got_worktree_get_root_path(worktree));
511 free(worktree->repo_path);
512 free(worktree->path_prefix);
513 free(worktree->base_commit_id);
514 free(worktree->head_ref_name);
515 free(worktree->root_path);
516 free(worktree->gotconfig_path);
517 got_gotconfig_free(worktree->gotconfig);
518 free(worktree);
519 return err;
522 const char *
523 got_worktree_get_root_path(struct got_worktree *worktree)
525 return worktree->root_path;
528 const char *
529 got_worktree_get_repo_path(struct got_worktree *worktree)
531 return worktree->repo_path;
533 const char *
534 got_worktree_get_path_prefix(struct got_worktree *worktree)
536 return worktree->path_prefix;
539 const struct got_error *
540 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
541 const char *path_prefix)
543 char *absprefix = NULL;
545 if (!got_path_is_absolute(path_prefix)) {
546 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
547 return got_error_from_errno("asprintf");
549 *match = (strcmp(absprefix ? absprefix : path_prefix,
550 worktree->path_prefix) == 0);
551 free(absprefix);
552 return NULL;
555 const char *
556 got_worktree_get_head_ref_name(struct got_worktree *worktree)
558 return worktree->head_ref_name;
561 const struct got_error *
562 got_worktree_set_head_ref(struct got_worktree *worktree,
563 struct got_reference *head_ref)
565 const struct got_error *err = NULL;
566 char *path_got = NULL, *head_ref_name = NULL;
568 if (asprintf(&path_got, "%s/%s", worktree->root_path,
569 GOT_WORKTREE_GOT_DIR) == -1) {
570 err = got_error_from_errno("asprintf");
571 path_got = NULL;
572 goto done;
575 head_ref_name = strdup(got_ref_get_name(head_ref));
576 if (head_ref_name == NULL) {
577 err = got_error_from_errno("strdup");
578 goto done;
581 err = write_head_ref(path_got, head_ref);
582 if (err)
583 goto done;
585 free(worktree->head_ref_name);
586 worktree->head_ref_name = head_ref_name;
587 done:
588 free(path_got);
589 if (err)
590 free(head_ref_name);
591 return err;
594 struct got_object_id *
595 got_worktree_get_base_commit_id(struct got_worktree *worktree)
597 return worktree->base_commit_id;
600 const struct got_error *
601 got_worktree_set_base_commit_id(struct got_worktree *worktree,
602 struct got_repository *repo, struct got_object_id *commit_id)
604 const struct got_error *err;
605 struct got_object *obj = NULL;
606 char *id_str = NULL;
607 char *path_got = NULL;
609 if (asprintf(&path_got, "%s/%s", worktree->root_path,
610 GOT_WORKTREE_GOT_DIR) == -1) {
611 err = got_error_from_errno("asprintf");
612 path_got = NULL;
613 goto done;
616 err = got_object_open(&obj, repo, commit_id);
617 if (err)
618 return err;
620 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
621 err = got_error(GOT_ERR_OBJ_TYPE);
622 goto done;
625 /* Record our base commit. */
626 err = got_object_id_str(&id_str, commit_id);
627 if (err)
628 goto done;
629 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
630 if (err)
631 goto done;
633 free(worktree->base_commit_id);
634 worktree->base_commit_id = got_object_id_dup(commit_id);
635 if (worktree->base_commit_id == NULL) {
636 err = got_error_from_errno("got_object_id_dup");
637 goto done;
639 done:
640 if (obj)
641 got_object_close(obj);
642 free(id_str);
643 free(path_got);
644 return err;
647 const struct got_gotconfig *
648 got_worktree_get_gotconfig(struct got_worktree *worktree)
650 return worktree->gotconfig;
653 static const struct got_error *
654 lock_worktree(struct got_worktree *worktree, int operation)
656 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
657 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
658 : got_error_from_errno2("flock",
659 got_worktree_get_root_path(worktree)));
660 return NULL;
663 static const struct got_error *
664 add_dir_on_disk(struct got_worktree *worktree, const char *path)
666 const struct got_error *err = NULL;
667 char *abspath;
669 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
670 return got_error_from_errno("asprintf");
672 err = got_path_mkdir(abspath);
673 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
674 struct stat sb;
675 err = NULL;
676 if (lstat(abspath, &sb) == -1) {
677 err = got_error_from_errno2("lstat", abspath);
678 } else if (!S_ISDIR(sb.st_mode)) {
679 /* TODO directory is obstructed; do something */
680 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
683 free(abspath);
684 return err;
687 static const struct got_error *
688 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
690 const struct got_error *err = NULL;
691 uint8_t fbuf1[8192];
692 uint8_t fbuf2[8192];
693 size_t flen1 = 0, flen2 = 0;
695 *same = 1;
697 for (;;) {
698 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
699 if (flen1 == 0 && ferror(f1)) {
700 err = got_error_from_errno("fread");
701 break;
703 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
704 if (flen2 == 0 && ferror(f2)) {
705 err = got_error_from_errno("fread");
706 break;
708 if (flen1 == 0) {
709 if (flen2 != 0)
710 *same = 0;
711 break;
712 } else if (flen2 == 0) {
713 if (flen1 != 0)
714 *same = 0;
715 break;
716 } else if (flen1 == flen2) {
717 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
718 *same = 0;
719 break;
721 } else {
722 *same = 0;
723 break;
727 return err;
730 static const struct got_error *
731 check_files_equal(int *same, FILE *f1, FILE *f2)
733 struct stat sb;
734 size_t size1, size2;
736 *same = 1;
738 if (fstat(fileno(f1), &sb) != 0)
739 return got_error_from_errno("fstat");
740 size1 = sb.st_size;
742 if (fstat(fileno(f2), &sb) != 0)
743 return got_error_from_errno("fstat");
744 size2 = sb.st_size;
746 if (size1 != size2) {
747 *same = 0;
748 return NULL;
751 if (fseek(f1, 0L, SEEK_SET) == -1)
752 return got_ferror(f1, GOT_ERR_IO);
753 if (fseek(f2, 0L, SEEK_SET) == -1)
754 return got_ferror(f2, GOT_ERR_IO);
756 return check_file_contents_equal(same, f1, f2);
759 /*
760 * Perform a 3-way merge where the file f_orig acts as the common
761 * ancestor, the file f_deriv acts as the first derived version,
762 * and the file at ondisk_path acts as both the target and the
763 * second derived version
764 */
765 static const struct got_error *
766 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
767 FILE *f_orig, const char *ondisk_path,
768 const char *path, uint16_t st_mode, FILE *f_deriv,
769 const char *label_orig, const char *label_deriv,
770 struct got_repository *repo,
771 got_worktree_checkout_cb progress_cb, void *progress_arg)
773 const struct got_error *err = NULL;
774 int merged_fd = -1;
775 FILE *f_merged = NULL;
776 char *merged_path = NULL, *base_path = NULL;
777 int overlapcnt = 0;
778 char *parent = NULL;
779 FILE *f = NULL;
781 *local_changes_subsumed = 0;
783 err = got_path_dirname(&parent, ondisk_path);
784 if (err)
785 return err;
787 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
788 err = got_error_from_errno("asprintf");
789 goto done;
792 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
793 if (err)
794 goto done;
796 /*
797 * Open the merge target file.
798 * In order the run a 3-way merge with a symlink we copy the symlink's
799 * target path into a temporary file and use that file with diff3.
800 */
801 if (S_ISLNK(st_mode)) {
802 char target_path[PATH_MAX];
803 ssize_t target_len;
804 size_t n;
806 free(base_path);
807 if (asprintf(&base_path, "%s/got-symlink-merge",
808 parent) == -1) {
809 err = got_error_from_errno("asprintf");
810 base_path = NULL;
811 goto done;
813 f = got_opentemp();
814 if (f == NULL) {
815 err = got_error_from_errno("got_opentemp");
816 goto done;
818 target_len = readlink(ondisk_path, target_path,
819 sizeof(target_path));
820 if (target_len == -1) {
821 err = got_error_from_errno2("readlink", ondisk_path);
822 goto done;
824 n = fwrite(target_path, 1, target_len, f);
825 if (n != target_len) {
826 err = got_ferror(f, GOT_ERR_IO);
827 goto done;
829 if (fflush(f) == EOF) {
830 err = got_error_from_errno("fflush");
831 goto done;
833 if (fseek(f, 0L, SEEK_SET) == -1) {
834 err = got_ferror(f, GOT_ERR_IO);
835 goto done;
837 } else {
838 int fd;
839 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
840 if (fd == -1) {
841 err = got_error_from_errno2("open", ondisk_path);
842 goto done;
844 f = fdopen(fd, "r");
845 if (f == NULL) {
846 err = got_error_from_errno2("fdopen", ondisk_path);
847 close(fd);
848 goto done;
852 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig, f,
853 label_deriv, label_orig, NULL);
854 if (err)
855 goto done;
857 err = (*progress_cb)(progress_arg,
858 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
859 if (err)
860 goto done;
862 if (fsync(merged_fd) != 0) {
863 err = got_error_from_errno("fsync");
864 goto done;
867 f_merged = fdopen(merged_fd, "r");
868 if (f_merged == NULL) {
869 err = got_error_from_errno("fdopen");
870 goto done;
872 merged_fd = -1;
874 /* Check if a clean merge has subsumed all local changes. */
875 if (overlapcnt == 0) {
876 err = check_files_equal(local_changes_subsumed, f_deriv,
877 f_merged);
878 if (err)
879 goto done;
882 if (fchmod(fileno(f_merged), st_mode) != 0) {
883 err = got_error_from_errno2("fchmod", merged_path);
884 goto done;
887 if (rename(merged_path, ondisk_path) != 0) {
888 err = got_error_from_errno3("rename", merged_path,
889 ondisk_path);
890 goto done;
892 done:
893 if (err) {
894 if (merged_path)
895 unlink(merged_path);
897 if (f && fclose(f) == EOF && err == NULL)
898 err = got_error_from_errno("fclose");
899 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
900 err = got_error_from_errno("close");
901 if (f_merged && fclose(f_merged) == EOF && err == NULL)
902 err = got_error_from_errno("fclose");
903 free(merged_path);
904 free(base_path);
905 free(parent);
906 return err;
909 static const struct got_error *
910 update_symlink(const char *ondisk_path, const char *target_path,
911 size_t target_len)
913 /* This is not atomic but matches what 'ln -sf' does. */
914 if (unlink(ondisk_path) == -1)
915 return got_error_from_errno2("unlink", ondisk_path);
916 if (symlink(target_path, ondisk_path) == -1)
917 return got_error_from_errno3("symlink", target_path,
918 ondisk_path);
919 return NULL;
922 /*
923 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
924 * in the work tree with a file that contains conflict markers and the
925 * conflicting target paths of the original version, a "derived version"
926 * of a symlink from an incoming change, and a local version of the symlink.
928 * The original versions's target path can be NULL if it is not available,
929 * such as if both derived versions added a new symlink at the same path.
931 * The incoming derived symlink target is NULL in case the incoming change
932 * has deleted this symlink.
933 */
934 static const struct got_error *
935 install_symlink_conflict(const char *deriv_target,
936 struct got_object_id *deriv_base_commit_id, const char *orig_target,
937 const char *label_orig, const char *local_target, const char *ondisk_path)
939 const struct got_error *err;
940 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
941 FILE *f = NULL;
943 err = got_object_id_str(&id_str, deriv_base_commit_id);
944 if (err)
945 return got_error_from_errno("asprintf");
947 if (asprintf(&label_deriv, "%s: commit %s",
948 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
949 err = got_error_from_errno("asprintf");
950 goto done;
953 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
954 if (err)
955 goto done;
957 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
958 err = got_error_from_errno2("fchmod", path);
959 goto done;
962 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
963 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
964 deriv_target ? deriv_target : "(symlink was deleted)",
965 orig_target ? label_orig : "",
966 orig_target ? "\n" : "",
967 orig_target ? orig_target : "",
968 orig_target ? "\n" : "",
969 GOT_DIFF_CONFLICT_MARKER_SEP,
970 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
971 err = got_error_from_errno2("fprintf", path);
972 goto done;
975 if (unlink(ondisk_path) == -1) {
976 err = got_error_from_errno2("unlink", ondisk_path);
977 goto done;
979 if (rename(path, ondisk_path) == -1) {
980 err = got_error_from_errno3("rename", path, ondisk_path);
981 goto done;
983 done:
984 if (f != NULL && fclose(f) == EOF && err == NULL)
985 err = got_error_from_errno2("fclose", path);
986 free(path);
987 free(id_str);
988 free(label_deriv);
989 return err;
992 /* forward declaration */
993 static const struct got_error *
994 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
995 const char *, const char *, uint16_t, const char *,
996 struct got_blob_object *, struct got_object_id *,
997 struct got_repository *, got_worktree_checkout_cb, void *);
999 /*
1000 * Merge a symlink into the work tree, where blob_orig acts as the common
1001 * ancestor, deriv_target is the link target of the first derived version,
1002 * and the symlink on disk acts as the second derived version.
1003 * Assume that contents of both blobs represent symlinks.
1005 static const struct got_error *
1006 merge_symlink(struct got_worktree *worktree,
1007 struct got_blob_object *blob_orig, const char *ondisk_path,
1008 const char *path, const char *label_orig, const char *deriv_target,
1009 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1010 got_worktree_checkout_cb progress_cb, void *progress_arg)
1012 const struct got_error *err = NULL;
1013 char *ancestor_target = NULL;
1014 struct stat sb;
1015 ssize_t ondisk_len, deriv_len;
1016 char ondisk_target[PATH_MAX];
1017 int have_local_change = 0;
1018 int have_incoming_change = 0;
1020 if (lstat(ondisk_path, &sb) == -1)
1021 return got_error_from_errno2("lstat", ondisk_path);
1023 ondisk_len = readlink(ondisk_path, ondisk_target,
1024 sizeof(ondisk_target));
1025 if (ondisk_len == -1) {
1026 err = got_error_from_errno2("readlink",
1027 ondisk_path);
1028 goto done;
1030 ondisk_target[ondisk_len] = '\0';
1032 if (blob_orig) {
1033 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1034 if (err)
1035 goto done;
1038 if (ancestor_target == NULL ||
1039 (ondisk_len != strlen(ancestor_target) ||
1040 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1041 have_local_change = 1;
1043 deriv_len = strlen(deriv_target);
1044 if (ancestor_target == NULL ||
1045 (deriv_len != strlen(ancestor_target) ||
1046 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1047 have_incoming_change = 1;
1049 if (!have_local_change && !have_incoming_change) {
1050 if (ancestor_target) {
1051 /* Both sides made the same change. */
1052 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1053 path);
1054 } else if (deriv_len == ondisk_len &&
1055 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1056 /* Both sides added the same symlink. */
1057 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1058 path);
1059 } else {
1060 /* Both sides added symlinks which don't match. */
1061 err = install_symlink_conflict(deriv_target,
1062 deriv_base_commit_id, ancestor_target,
1063 label_orig, ondisk_target, ondisk_path);
1064 if (err)
1065 goto done;
1066 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1067 path);
1069 } else if (!have_local_change && have_incoming_change) {
1070 /* Apply the incoming change. */
1071 err = update_symlink(ondisk_path, deriv_target,
1072 strlen(deriv_target));
1073 if (err)
1074 goto done;
1075 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1076 } else if (have_local_change && have_incoming_change) {
1077 if (deriv_len == ondisk_len &&
1078 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1079 /* Both sides made the same change. */
1080 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1081 path);
1082 } else {
1083 err = install_symlink_conflict(deriv_target,
1084 deriv_base_commit_id, ancestor_target, label_orig,
1085 ondisk_target, ondisk_path);
1086 if (err)
1087 goto done;
1088 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1089 path);
1093 done:
1094 free(ancestor_target);
1095 return err;
1099 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1100 * blob_deriv acts as the first derived version, and the file on disk
1101 * acts as the second derived version.
1103 static const struct got_error *
1104 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1105 struct got_blob_object *blob_orig, const char *ondisk_path,
1106 const char *path, uint16_t st_mode, const char *label_orig,
1107 struct got_blob_object *blob_deriv,
1108 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1109 got_worktree_checkout_cb progress_cb, void *progress_arg)
1111 const struct got_error *err = NULL;
1112 FILE *f_orig = NULL, *f_deriv = NULL;
1113 char *blob_orig_path = NULL;
1114 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1115 char *label_deriv = NULL, *parent = NULL;
1117 *local_changes_subsumed = 0;
1119 err = got_path_dirname(&parent, ondisk_path);
1120 if (err)
1121 return err;
1123 if (blob_orig) {
1124 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1125 parent) == -1) {
1126 err = got_error_from_errno("asprintf");
1127 base_path = NULL;
1128 goto done;
1131 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
1132 if (err)
1133 goto done;
1134 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1135 blob_orig);
1136 if (err)
1137 goto done;
1138 free(base_path);
1139 } else {
1141 * No common ancestor exists. This is an "add vs add" conflict
1142 * and we simply use an empty ancestor file to make both files
1143 * appear in the merged result in their entirety.
1145 f_orig = got_opentemp();
1146 if (f_orig == NULL) {
1147 err = got_error_from_errno("got_opentemp");
1148 goto done;
1152 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1153 err = got_error_from_errno("asprintf");
1154 base_path = NULL;
1155 goto done;
1158 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1159 if (err)
1160 goto done;
1161 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1162 blob_deriv);
1163 if (err)
1164 goto done;
1166 err = got_object_id_str(&id_str, deriv_base_commit_id);
1167 if (err)
1168 goto done;
1169 if (asprintf(&label_deriv, "%s: commit %s",
1170 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1171 err = got_error_from_errno("asprintf");
1172 goto done;
1175 err = merge_file(local_changes_subsumed, worktree, f_orig,
1176 ondisk_path, path, st_mode, f_deriv, label_orig, label_deriv,
1177 repo, progress_cb, progress_arg);
1178 done:
1179 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1180 err = got_error_from_errno("fclose");
1181 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1182 err = got_error_from_errno("fclose");
1183 free(base_path);
1184 if (blob_orig_path) {
1185 unlink(blob_orig_path);
1186 free(blob_orig_path);
1188 if (blob_deriv_path) {
1189 unlink(blob_deriv_path);
1190 free(blob_deriv_path);
1192 free(id_str);
1193 free(label_deriv);
1194 free(parent);
1195 return err;
1198 static const struct got_error *
1199 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1200 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1201 int wt_fd, const char *path, struct got_object_id *blob_id)
1203 const struct got_error *err = NULL;
1204 struct got_fileindex_entry *new_ie;
1206 *new_iep = NULL;
1208 err = got_fileindex_entry_alloc(&new_ie, path);
1209 if (err)
1210 return err;
1212 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1213 blob_id->sha1, base_commit_id->sha1, 1);
1214 if (err)
1215 goto done;
1217 err = got_fileindex_entry_add(fileindex, new_ie);
1218 done:
1219 if (err)
1220 got_fileindex_entry_free(new_ie);
1221 else
1222 *new_iep = new_ie;
1223 return err;
1226 static mode_t
1227 get_ondisk_perms(int executable, mode_t st_mode)
1229 mode_t xbits = S_IXUSR;
1231 if (executable) {
1232 /* Map read bits to execute bits. */
1233 if (st_mode & S_IRGRP)
1234 xbits |= S_IXGRP;
1235 if (st_mode & S_IROTH)
1236 xbits |= S_IXOTH;
1237 return st_mode | xbits;
1240 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1243 /* forward declaration */
1244 static const struct got_error *
1245 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1246 const char *path, mode_t te_mode, mode_t st_mode,
1247 struct got_blob_object *blob, int restoring_missing_file,
1248 int reverting_versioned_file, int installing_bad_symlink,
1249 int path_is_unversioned, struct got_repository *repo,
1250 got_worktree_checkout_cb progress_cb, void *progress_arg);
1253 * This function assumes that the provided symlink target points at a
1254 * safe location in the work tree!
1256 static const struct got_error *
1257 replace_existing_symlink(int *did_something, const char *ondisk_path,
1258 const char *target_path, size_t target_len)
1260 const struct got_error *err = NULL;
1261 ssize_t elen;
1262 char etarget[PATH_MAX];
1263 int fd;
1265 *did_something = 0;
1268 * "Bad" symlinks (those pointing outside the work tree or into the
1269 * .got directory) are installed in the work tree as a regular file
1270 * which contains the bad symlink target path.
1271 * The new symlink target has already been checked for safety by our
1272 * caller. If we can successfully open a regular file then we simply
1273 * replace this file with a symlink below.
1275 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1276 if (fd == -1) {
1277 if (errno != ELOOP)
1278 return got_error_from_errno2("open", ondisk_path);
1280 /* We are updating an existing on-disk symlink. */
1281 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1282 if (elen == -1)
1283 return got_error_from_errno2("readlink", ondisk_path);
1285 if (elen == target_len &&
1286 memcmp(etarget, target_path, target_len) == 0)
1287 return NULL; /* nothing to do */
1290 *did_something = 1;
1291 err = update_symlink(ondisk_path, target_path, target_len);
1292 if (fd != -1 && close(fd) == -1 && err == NULL)
1293 err = got_error_from_errno2("close", ondisk_path);
1294 return err;
1297 static const struct got_error *
1298 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1299 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1301 const struct got_error *err = NULL;
1302 char canonpath[PATH_MAX];
1303 char *path_got = NULL;
1305 *is_bad_symlink = 0;
1307 if (target_len >= sizeof(canonpath)) {
1308 *is_bad_symlink = 1;
1309 return NULL;
1313 * We do not use realpath(3) to resolve the symlink's target
1314 * path because we don't want to resolve symlinks recursively.
1315 * Instead we make the path absolute and then canonicalize it.
1316 * Relative symlink target lookup should begin at the directory
1317 * in which the blob object is being installed.
1319 if (!got_path_is_absolute(target_path)) {
1320 char *abspath, *parent;
1321 err = got_path_dirname(&parent, ondisk_path);
1322 if (err)
1323 return err;
1324 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1325 free(parent);
1326 return got_error_from_errno("asprintf");
1328 free(parent);
1329 if (strlen(abspath) >= sizeof(canonpath)) {
1330 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1331 free(abspath);
1332 return err;
1334 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1335 free(abspath);
1336 if (err)
1337 return err;
1338 } else {
1339 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1340 if (err)
1341 return err;
1344 /* Only allow symlinks pointing at paths within the work tree. */
1345 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1346 *is_bad_symlink = 1;
1347 return NULL;
1350 /* Do not allow symlinks pointing into the .got directory. */
1351 if (asprintf(&path_got, "%s/%s", wtroot_path,
1352 GOT_WORKTREE_GOT_DIR) == -1)
1353 return got_error_from_errno("asprintf");
1354 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1355 *is_bad_symlink = 1;
1357 free(path_got);
1358 return NULL;
1361 static const struct got_error *
1362 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1363 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1364 int restoring_missing_file, int reverting_versioned_file,
1365 int path_is_unversioned, struct got_repository *repo,
1366 got_worktree_checkout_cb progress_cb, void *progress_arg)
1368 const struct got_error *err = NULL;
1369 char target_path[PATH_MAX];
1370 size_t len, target_len = 0;
1371 char *path_got = NULL;
1372 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1373 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1375 *is_bad_symlink = 0;
1378 * Blob object content specifies the target path of the link.
1379 * If a symbolic link cannot be installed we instead create
1380 * a regular file which contains the link target path stored
1381 * in the blob object.
1383 do {
1384 err = got_object_blob_read_block(&len, blob);
1385 if (len + target_len >= sizeof(target_path)) {
1386 /* Path too long; install as a regular file. */
1387 *is_bad_symlink = 1;
1388 got_object_blob_rewind(blob);
1389 return install_blob(worktree, ondisk_path, path,
1390 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1391 restoring_missing_file, reverting_versioned_file,
1392 1, path_is_unversioned, repo, progress_cb,
1393 progress_arg);
1395 if (len > 0) {
1396 /* Skip blob object header first time around. */
1397 memcpy(target_path + target_len, buf + hdrlen,
1398 len - hdrlen);
1399 target_len += len - hdrlen;
1400 hdrlen = 0;
1402 } while (len != 0);
1403 target_path[target_len] = '\0';
1405 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1406 ondisk_path, worktree->root_path);
1407 if (err)
1408 return err;
1410 if (*is_bad_symlink) {
1411 /* install as a regular file */
1412 got_object_blob_rewind(blob);
1413 err = install_blob(worktree, ondisk_path, path,
1414 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1415 restoring_missing_file, reverting_versioned_file, 1,
1416 path_is_unversioned, repo, progress_cb, progress_arg);
1417 goto done;
1420 if (symlink(target_path, ondisk_path) == -1) {
1421 if (errno == EEXIST) {
1422 int symlink_replaced;
1423 if (path_is_unversioned) {
1424 err = (*progress_cb)(progress_arg,
1425 GOT_STATUS_UNVERSIONED, path);
1426 goto done;
1428 err = replace_existing_symlink(&symlink_replaced,
1429 ondisk_path, target_path, target_len);
1430 if (err)
1431 goto done;
1432 if (progress_cb) {
1433 if (symlink_replaced) {
1434 err = (*progress_cb)(progress_arg,
1435 reverting_versioned_file ?
1436 GOT_STATUS_REVERT :
1437 GOT_STATUS_UPDATE, path);
1438 } else {
1439 err = (*progress_cb)(progress_arg,
1440 GOT_STATUS_EXISTS, path);
1443 goto done; /* Nothing else to do. */
1446 if (errno == ENOENT) {
1447 char *parent;
1448 err = got_path_dirname(&parent, ondisk_path);
1449 if (err)
1450 goto done;
1451 err = add_dir_on_disk(worktree, parent);
1452 free(parent);
1453 if (err)
1454 goto done;
1456 * Retry, and fall through to error handling
1457 * below if this second attempt fails.
1459 if (symlink(target_path, ondisk_path) != -1) {
1460 err = NULL; /* success */
1461 goto done;
1465 /* Handle errors from first or second creation attempt. */
1466 if (errno == ENAMETOOLONG) {
1467 /* bad target path; install as a regular file */
1468 *is_bad_symlink = 1;
1469 got_object_blob_rewind(blob);
1470 err = install_blob(worktree, ondisk_path, path,
1471 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1472 restoring_missing_file, reverting_versioned_file, 1,
1473 path_is_unversioned, repo,
1474 progress_cb, progress_arg);
1475 } else if (errno == ENOTDIR) {
1476 err = got_error_path(ondisk_path,
1477 GOT_ERR_FILE_OBSTRUCTED);
1478 } else {
1479 err = got_error_from_errno3("symlink",
1480 target_path, ondisk_path);
1482 } else if (progress_cb)
1483 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1484 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1485 done:
1486 free(path_got);
1487 return err;
1490 static const struct got_error *
1491 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1492 const char *path, mode_t te_mode, mode_t st_mode,
1493 struct got_blob_object *blob, int restoring_missing_file,
1494 int reverting_versioned_file, int installing_bad_symlink,
1495 int path_is_unversioned, struct got_repository *repo,
1496 got_worktree_checkout_cb progress_cb, void *progress_arg)
1498 const struct got_error *err = NULL;
1499 int fd = -1;
1500 size_t len, hdrlen;
1501 int update = 0;
1502 char *tmppath = NULL;
1504 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1505 GOT_DEFAULT_FILE_MODE);
1506 if (fd == -1) {
1507 if (errno == ENOENT) {
1508 char *parent;
1509 err = got_path_dirname(&parent, path);
1510 if (err)
1511 return err;
1512 err = add_dir_on_disk(worktree, parent);
1513 free(parent);
1514 if (err)
1515 return err;
1516 fd = open(ondisk_path,
1517 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1518 GOT_DEFAULT_FILE_MODE);
1519 if (fd == -1)
1520 return got_error_from_errno2("open",
1521 ondisk_path);
1522 } else if (errno == EEXIST) {
1523 if (path_is_unversioned) {
1524 err = (*progress_cb)(progress_arg,
1525 GOT_STATUS_UNVERSIONED, path);
1526 goto done;
1528 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1529 !S_ISREG(st_mode) && !installing_bad_symlink) {
1530 /* TODO file is obstructed; do something */
1531 err = got_error_path(ondisk_path,
1532 GOT_ERR_FILE_OBSTRUCTED);
1533 goto done;
1534 } else {
1535 err = got_opentemp_named_fd(&tmppath, &fd,
1536 ondisk_path);
1537 if (err)
1538 goto done;
1539 update = 1;
1541 } else
1542 return got_error_from_errno2("open", ondisk_path);
1545 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1546 err = got_error_from_errno2("fchmod",
1547 update ? tmppath : ondisk_path);
1548 goto done;
1551 if (progress_cb) {
1552 if (restoring_missing_file)
1553 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1554 path);
1555 else if (reverting_versioned_file)
1556 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1557 path);
1558 else
1559 err = (*progress_cb)(progress_arg,
1560 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1561 if (err)
1562 goto done;
1565 hdrlen = got_object_blob_get_hdrlen(blob);
1566 do {
1567 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1568 err = got_object_blob_read_block(&len, blob);
1569 if (err)
1570 break;
1571 if (len > 0) {
1572 /* Skip blob object header first time around. */
1573 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1574 if (outlen == -1) {
1575 err = got_error_from_errno("write");
1576 goto done;
1577 } else if (outlen != len - hdrlen) {
1578 err = got_error(GOT_ERR_IO);
1579 goto done;
1581 hdrlen = 0;
1583 } while (len != 0);
1585 if (fsync(fd) != 0) {
1586 err = got_error_from_errno("fsync");
1587 goto done;
1590 if (update) {
1591 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1592 err = got_error_from_errno2("unlink", ondisk_path);
1593 goto done;
1595 if (rename(tmppath, ondisk_path) != 0) {
1596 err = got_error_from_errno3("rename", tmppath,
1597 ondisk_path);
1598 goto done;
1600 free(tmppath);
1601 tmppath = NULL;
1604 done:
1605 if (fd != -1 && close(fd) == -1 && err == NULL)
1606 err = got_error_from_errno("close");
1607 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1608 err = got_error_from_errno2("unlink", tmppath);
1609 free(tmppath);
1610 return err;
1613 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1614 static const struct got_error *
1615 get_modified_file_content_status(unsigned char *status, FILE *f)
1617 const struct got_error *err = NULL;
1618 const char *markers[3] = {
1619 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1620 GOT_DIFF_CONFLICT_MARKER_SEP,
1621 GOT_DIFF_CONFLICT_MARKER_END
1623 int i = 0;
1624 char *line = NULL;
1625 size_t linesize = 0;
1626 ssize_t linelen;
1628 while (*status == GOT_STATUS_MODIFY) {
1629 linelen = getline(&line, &linesize, f);
1630 if (linelen == -1) {
1631 if (feof(f))
1632 break;
1633 err = got_ferror(f, GOT_ERR_IO);
1634 break;
1637 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1638 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1639 == 0)
1640 *status = GOT_STATUS_CONFLICT;
1641 else
1642 i++;
1645 free(line);
1647 return err;
1650 static int
1651 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1653 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1654 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1657 static int
1658 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1660 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1661 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1662 ie->mtime_sec == sb->st_mtim.tv_sec &&
1663 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1664 ie->size == (sb->st_size & 0xffffffff) &&
1665 !xbit_differs(ie, sb->st_mode));
1668 static unsigned char
1669 get_staged_status(struct got_fileindex_entry *ie)
1671 switch (got_fileindex_entry_stage_get(ie)) {
1672 case GOT_FILEIDX_STAGE_ADD:
1673 return GOT_STATUS_ADD;
1674 case GOT_FILEIDX_STAGE_DELETE:
1675 return GOT_STATUS_DELETE;
1676 case GOT_FILEIDX_STAGE_MODIFY:
1677 return GOT_STATUS_MODIFY;
1678 default:
1679 return GOT_STATUS_NO_CHANGE;
1683 static const struct got_error *
1684 get_symlink_modification_status(unsigned char *status,
1685 struct got_fileindex_entry *ie, const char *abspath,
1686 int dirfd, const char *de_name, struct got_blob_object *blob)
1688 const struct got_error *err = NULL;
1689 char target_path[PATH_MAX];
1690 char etarget[PATH_MAX];
1691 ssize_t elen;
1692 size_t len, target_len = 0;
1693 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1694 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1696 *status = GOT_STATUS_NO_CHANGE;
1698 /* Blob object content specifies the target path of the link. */
1699 do {
1700 err = got_object_blob_read_block(&len, blob);
1701 if (err)
1702 return err;
1703 if (len + target_len >= sizeof(target_path)) {
1705 * Should not happen. The blob contents were OK
1706 * when this symlink was installed.
1708 return got_error(GOT_ERR_NO_SPACE);
1710 if (len > 0) {
1711 /* Skip blob object header first time around. */
1712 memcpy(target_path + target_len, buf + hdrlen,
1713 len - hdrlen);
1714 target_len += len - hdrlen;
1715 hdrlen = 0;
1717 } while (len != 0);
1718 target_path[target_len] = '\0';
1720 if (dirfd != -1) {
1721 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1722 if (elen == -1)
1723 return got_error_from_errno2("readlinkat", abspath);
1724 } else {
1725 elen = readlink(abspath, etarget, sizeof(etarget));
1726 if (elen == -1)
1727 return got_error_from_errno2("readlink", abspath);
1730 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1731 *status = GOT_STATUS_MODIFY;
1733 return NULL;
1736 static const struct got_error *
1737 get_file_status(unsigned char *status, struct stat *sb,
1738 struct got_fileindex_entry *ie, const char *abspath,
1739 int dirfd, const char *de_name, struct got_repository *repo)
1741 const struct got_error *err = NULL;
1742 struct got_object_id id;
1743 size_t hdrlen;
1744 int fd = -1;
1745 FILE *f = NULL;
1746 uint8_t fbuf[8192];
1747 struct got_blob_object *blob = NULL;
1748 size_t flen, blen;
1749 unsigned char staged_status = get_staged_status(ie);
1751 *status = GOT_STATUS_NO_CHANGE;
1752 memset(sb, 0, sizeof(*sb));
1755 * Whenever the caller provides a directory descriptor and a
1756 * directory entry name for the file, use them! This prevents
1757 * race conditions if filesystem paths change beneath our feet.
1759 if (dirfd != -1) {
1760 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1761 if (errno == ENOENT) {
1762 if (got_fileindex_entry_has_file_on_disk(ie))
1763 *status = GOT_STATUS_MISSING;
1764 else
1765 *status = GOT_STATUS_DELETE;
1766 goto done;
1768 err = got_error_from_errno2("fstatat", abspath);
1769 goto done;
1771 } else {
1772 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1773 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1774 return got_error_from_errno2("open", abspath);
1775 else if (fd == -1 && errno == ELOOP) {
1776 if (lstat(abspath, sb) == -1)
1777 return got_error_from_errno2("lstat", abspath);
1778 } else if (fd == -1 || fstat(fd, sb) == -1) {
1779 if (errno == ENOENT) {
1780 if (got_fileindex_entry_has_file_on_disk(ie))
1781 *status = GOT_STATUS_MISSING;
1782 else
1783 *status = GOT_STATUS_DELETE;
1784 goto done;
1786 err = got_error_from_errno2("fstat", abspath);
1787 goto done;
1791 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1792 *status = GOT_STATUS_OBSTRUCTED;
1793 goto done;
1796 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1797 *status = GOT_STATUS_DELETE;
1798 goto done;
1799 } else if (!got_fileindex_entry_has_blob(ie) &&
1800 staged_status != GOT_STATUS_ADD) {
1801 *status = GOT_STATUS_ADD;
1802 goto done;
1805 if (!stat_info_differs(ie, sb))
1806 goto done;
1808 if (S_ISLNK(sb->st_mode) &&
1809 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1810 *status = GOT_STATUS_MODIFY;
1811 goto done;
1814 if (staged_status == GOT_STATUS_MODIFY ||
1815 staged_status == GOT_STATUS_ADD)
1816 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1817 else
1818 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1820 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1821 if (err)
1822 goto done;
1824 if (S_ISLNK(sb->st_mode)) {
1825 err = get_symlink_modification_status(status, ie,
1826 abspath, dirfd, de_name, blob);
1827 goto done;
1830 if (dirfd != -1) {
1831 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1832 if (fd == -1) {
1833 err = got_error_from_errno2("openat", abspath);
1834 goto done;
1838 f = fdopen(fd, "r");
1839 if (f == NULL) {
1840 err = got_error_from_errno2("fdopen", abspath);
1841 goto done;
1843 fd = -1;
1844 hdrlen = got_object_blob_get_hdrlen(blob);
1845 for (;;) {
1846 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1847 err = got_object_blob_read_block(&blen, blob);
1848 if (err)
1849 goto done;
1850 /* Skip length of blob object header first time around. */
1851 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1852 if (flen == 0 && ferror(f)) {
1853 err = got_error_from_errno("fread");
1854 goto done;
1856 if (blen - hdrlen == 0) {
1857 if (flen != 0)
1858 *status = GOT_STATUS_MODIFY;
1859 break;
1860 } else if (flen == 0) {
1861 if (blen - hdrlen != 0)
1862 *status = GOT_STATUS_MODIFY;
1863 break;
1864 } else if (blen - hdrlen == flen) {
1865 /* Skip blob object header first time around. */
1866 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1867 *status = GOT_STATUS_MODIFY;
1868 break;
1870 } else {
1871 *status = GOT_STATUS_MODIFY;
1872 break;
1874 hdrlen = 0;
1877 if (*status == GOT_STATUS_MODIFY) {
1878 rewind(f);
1879 err = get_modified_file_content_status(status, f);
1880 } else if (xbit_differs(ie, sb->st_mode))
1881 *status = GOT_STATUS_MODE_CHANGE;
1882 done:
1883 if (blob)
1884 got_object_blob_close(blob);
1885 if (f != NULL && fclose(f) == EOF && err == NULL)
1886 err = got_error_from_errno2("fclose", abspath);
1887 if (fd != -1 && close(fd) == -1 && err == NULL)
1888 err = got_error_from_errno2("close", abspath);
1889 return err;
1893 * Update timestamps in the file index if a file is unmodified and
1894 * we had to run a full content comparison to find out.
1896 static const struct got_error *
1897 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1898 struct got_fileindex_entry *ie, struct stat *sb)
1900 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1901 return got_fileindex_entry_update(ie, wt_fd, path,
1902 ie->blob_sha1, ie->commit_sha1, 1);
1904 return NULL;
1907 static const struct got_error *
1908 update_blob(struct got_worktree *worktree,
1909 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1910 struct got_tree_entry *te, const char *path,
1911 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1912 void *progress_arg)
1914 const struct got_error *err = NULL;
1915 struct got_blob_object *blob = NULL;
1916 char *ondisk_path;
1917 unsigned char status = GOT_STATUS_NO_CHANGE;
1918 struct stat sb;
1920 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1921 return got_error_from_errno("asprintf");
1923 if (ie) {
1924 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1925 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1926 goto done;
1928 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1929 repo);
1930 if (err)
1931 goto done;
1932 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1933 sb.st_mode = got_fileindex_perms_to_st(ie);
1934 } else {
1935 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1936 status = GOT_STATUS_UNVERSIONED;
1939 if (status == GOT_STATUS_OBSTRUCTED) {
1940 err = (*progress_cb)(progress_arg, status, path);
1941 goto done;
1943 if (status == GOT_STATUS_CONFLICT) {
1944 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1945 path);
1946 goto done;
1949 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1950 (S_ISLNK(te->mode) ||
1951 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1953 * This is a regular file or an installed bad symlink.
1954 * If the file index indicates that this file is already
1955 * up-to-date with respect to the repository we can skip
1956 * updating contents of this file.
1958 if (got_fileindex_entry_has_commit(ie) &&
1959 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1960 SHA1_DIGEST_LENGTH) == 0) {
1961 /* Same commit. */
1962 err = sync_timestamps(worktree->root_fd,
1963 path, status, ie, &sb);
1964 if (err)
1965 goto done;
1966 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1967 path);
1968 goto done;
1970 if (got_fileindex_entry_has_blob(ie) &&
1971 memcmp(ie->blob_sha1, te->id.sha1,
1972 SHA1_DIGEST_LENGTH) == 0) {
1973 /* Different commit but the same blob. */
1974 err = sync_timestamps(worktree->root_fd,
1975 path, status, ie, &sb);
1976 if (err)
1977 goto done;
1978 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1979 path);
1980 goto done;
1984 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1985 if (err)
1986 goto done;
1988 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1989 int update_timestamps;
1990 struct got_blob_object *blob2 = NULL;
1991 char *label_orig = NULL;
1992 if (got_fileindex_entry_has_blob(ie)) {
1993 struct got_object_id id2;
1994 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1995 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1996 if (err)
1997 goto done;
1999 if (got_fileindex_entry_has_commit(ie)) {
2000 char id_str[SHA1_DIGEST_STRING_LENGTH];
2001 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2002 sizeof(id_str)) == NULL) {
2003 err = got_error_path(id_str,
2004 GOT_ERR_BAD_OBJ_ID_STR);
2005 goto done;
2007 if (asprintf(&label_orig, "%s: commit %s",
2008 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2009 err = got_error_from_errno("asprintf");
2010 goto done;
2013 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2014 char *link_target;
2015 err = got_object_blob_read_to_str(&link_target, blob);
2016 if (err)
2017 goto done;
2018 err = merge_symlink(worktree, blob2, ondisk_path, path,
2019 label_orig, link_target, worktree->base_commit_id,
2020 repo, progress_cb, progress_arg);
2021 free(link_target);
2022 } else {
2023 err = merge_blob(&update_timestamps, worktree, blob2,
2024 ondisk_path, path, sb.st_mode, label_orig, blob,
2025 worktree->base_commit_id, repo,
2026 progress_cb, progress_arg);
2028 free(label_orig);
2029 if (blob2)
2030 got_object_blob_close(blob2);
2031 if (err)
2032 goto done;
2034 * Do not update timestamps of files with local changes.
2035 * Otherwise, a future status walk would treat them as
2036 * unmodified files again.
2038 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2039 blob->id.sha1, worktree->base_commit_id->sha1,
2040 update_timestamps);
2041 } else if (status == GOT_STATUS_MODE_CHANGE) {
2042 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2043 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2044 } else if (status == GOT_STATUS_DELETE) {
2045 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2046 if (err)
2047 goto done;
2048 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2049 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2050 if (err)
2051 goto done;
2052 } else {
2053 int is_bad_symlink = 0;
2054 if (S_ISLNK(te->mode)) {
2055 err = install_symlink(&is_bad_symlink, worktree,
2056 ondisk_path, path, blob,
2057 status == GOT_STATUS_MISSING, 0,
2058 status == GOT_STATUS_UNVERSIONED, repo,
2059 progress_cb, progress_arg);
2060 } else {
2061 err = install_blob(worktree, ondisk_path, path,
2062 te->mode, sb.st_mode, blob,
2063 status == GOT_STATUS_MISSING, 0, 0,
2064 status == GOT_STATUS_UNVERSIONED, repo,
2065 progress_cb, progress_arg);
2067 if (err)
2068 goto done;
2070 if (ie) {
2071 err = got_fileindex_entry_update(ie,
2072 worktree->root_fd, path, blob->id.sha1,
2073 worktree->base_commit_id->sha1, 1);
2074 } else {
2075 err = create_fileindex_entry(&ie, fileindex,
2076 worktree->base_commit_id, worktree->root_fd, path,
2077 &blob->id);
2079 if (err)
2080 goto done;
2082 if (is_bad_symlink) {
2083 got_fileindex_entry_filetype_set(ie,
2084 GOT_FILEIDX_MODE_BAD_SYMLINK);
2087 got_object_blob_close(blob);
2088 done:
2089 free(ondisk_path);
2090 return err;
2093 static const struct got_error *
2094 remove_ondisk_file(const char *root_path, const char *path)
2096 const struct got_error *err = NULL;
2097 char *ondisk_path = NULL;
2099 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2100 return got_error_from_errno("asprintf");
2102 if (unlink(ondisk_path) == -1) {
2103 if (errno != ENOENT)
2104 err = got_error_from_errno2("unlink", ondisk_path);
2105 } else {
2106 size_t root_len = strlen(root_path);
2107 do {
2108 char *parent;
2109 err = got_path_dirname(&parent, ondisk_path);
2110 if (err)
2111 break;
2112 free(ondisk_path);
2113 ondisk_path = parent;
2114 if (rmdir(ondisk_path) == -1) {
2115 if (errno != ENOTEMPTY)
2116 err = got_error_from_errno2("rmdir",
2117 ondisk_path);
2118 break;
2120 } while (got_path_cmp(ondisk_path, root_path,
2121 strlen(ondisk_path), root_len) != 0);
2123 free(ondisk_path);
2124 return err;
2127 static const struct got_error *
2128 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2129 struct got_fileindex_entry *ie, struct got_repository *repo,
2130 got_worktree_checkout_cb progress_cb, void *progress_arg)
2132 const struct got_error *err = NULL;
2133 unsigned char status;
2134 struct stat sb;
2135 char *ondisk_path;
2137 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2138 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2140 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2141 == -1)
2142 return got_error_from_errno("asprintf");
2144 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2145 if (err)
2146 goto done;
2148 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2149 char ondisk_target[PATH_MAX];
2150 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2151 sizeof(ondisk_target));
2152 if (ondisk_len == -1) {
2153 err = got_error_from_errno2("readlink", ondisk_path);
2154 goto done;
2156 ondisk_target[ondisk_len] = '\0';
2157 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2158 NULL, NULL, /* XXX pass common ancestor info? */
2159 ondisk_target, ondisk_path);
2160 if (err)
2161 goto done;
2162 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2163 ie->path);
2164 goto done;
2167 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2168 status == GOT_STATUS_ADD) {
2169 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2170 if (err)
2171 goto done;
2173 * Preserve the working file and change the deleted blob's
2174 * entry into a schedule-add entry.
2176 err = got_fileindex_entry_update(ie, worktree->root_fd,
2177 ie->path, NULL, NULL, 0);
2178 } else {
2179 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2180 if (err)
2181 goto done;
2182 if (status == GOT_STATUS_NO_CHANGE) {
2183 err = remove_ondisk_file(worktree->root_path, ie->path);
2184 if (err)
2185 goto done;
2187 got_fileindex_entry_remove(fileindex, ie);
2189 done:
2190 free(ondisk_path);
2191 return err;
2194 struct diff_cb_arg {
2195 struct got_fileindex *fileindex;
2196 struct got_worktree *worktree;
2197 struct got_repository *repo;
2198 got_worktree_checkout_cb progress_cb;
2199 void *progress_arg;
2200 got_cancel_cb cancel_cb;
2201 void *cancel_arg;
2204 static const struct got_error *
2205 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2206 struct got_tree_entry *te, const char *parent_path)
2208 struct diff_cb_arg *a = arg;
2210 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2211 return got_error(GOT_ERR_CANCELLED);
2213 return update_blob(a->worktree, a->fileindex, ie, te,
2214 ie->path, a->repo, a->progress_cb, a->progress_arg);
2217 static const struct got_error *
2218 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2220 struct diff_cb_arg *a = arg;
2222 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2223 return got_error(GOT_ERR_CANCELLED);
2225 return delete_blob(a->worktree, a->fileindex, ie,
2226 a->repo, a->progress_cb, a->progress_arg);
2229 static const struct got_error *
2230 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2232 struct diff_cb_arg *a = arg;
2233 const struct got_error *err;
2234 char *path;
2236 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2237 return got_error(GOT_ERR_CANCELLED);
2239 if (got_object_tree_entry_is_submodule(te))
2240 return NULL;
2242 if (asprintf(&path, "%s%s%s", parent_path,
2243 parent_path[0] ? "/" : "", te->name)
2244 == -1)
2245 return got_error_from_errno("asprintf");
2247 if (S_ISDIR(te->mode))
2248 err = add_dir_on_disk(a->worktree, path);
2249 else
2250 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2251 a->repo, a->progress_cb, a->progress_arg);
2253 free(path);
2254 return err;
2257 const struct got_error *
2258 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2260 uint32_t uuid_status;
2262 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2263 if (uuid_status != uuid_s_ok) {
2264 *uuidstr = NULL;
2265 return got_error_uuid(uuid_status, "uuid_to_string");
2268 return NULL;
2271 static const struct got_error *
2272 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2274 const struct got_error *err = NULL;
2275 char *uuidstr = NULL;
2277 *refname = NULL;
2279 err = got_worktree_get_uuid(&uuidstr, worktree);
2280 if (err)
2281 return err;
2283 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2284 err = got_error_from_errno("asprintf");
2285 *refname = NULL;
2287 free(uuidstr);
2288 return err;
2291 const struct got_error *
2292 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2294 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2297 static const struct got_error *
2298 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2300 return get_ref_name(refname, worktree,
2301 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2304 static const struct got_error *
2305 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2307 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2310 static const struct got_error *
2311 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2313 return get_ref_name(refname, worktree,
2314 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2317 static const struct got_error *
2318 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2320 return get_ref_name(refname, worktree,
2321 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2324 static const struct got_error *
2325 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2327 return get_ref_name(refname, worktree,
2328 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2331 static const struct got_error *
2332 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2334 return get_ref_name(refname, worktree,
2335 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2338 static const struct got_error *
2339 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2341 return get_ref_name(refname, worktree,
2342 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2345 static const struct got_error *
2346 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2348 return get_ref_name(refname, worktree,
2349 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2352 const struct got_error *
2353 got_worktree_get_histedit_script_path(char **path,
2354 struct got_worktree *worktree)
2356 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2357 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2358 *path = NULL;
2359 return got_error_from_errno("asprintf");
2361 return NULL;
2365 * Prevent Git's garbage collector from deleting our base commit by
2366 * setting a reference to our base commit's ID.
2368 static const struct got_error *
2369 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2371 const struct got_error *err = NULL;
2372 struct got_reference *ref = NULL;
2373 char *refname;
2375 err = got_worktree_get_base_ref_name(&refname, worktree);
2376 if (err)
2377 return err;
2379 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2380 if (err)
2381 goto done;
2383 err = got_ref_write(ref, repo);
2384 done:
2385 free(refname);
2386 if (ref)
2387 got_ref_close(ref);
2388 return err;
2391 static const struct got_error *
2392 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2394 const struct got_error *err = NULL;
2396 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2397 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2398 err = got_error_from_errno("asprintf");
2399 *fileindex_path = NULL;
2401 return err;
2405 static const struct got_error *
2406 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2407 struct got_worktree *worktree)
2409 const struct got_error *err = NULL;
2410 FILE *index = NULL;
2412 *fileindex_path = NULL;
2413 *fileindex = got_fileindex_alloc();
2414 if (*fileindex == NULL)
2415 return got_error_from_errno("got_fileindex_alloc");
2417 err = get_fileindex_path(fileindex_path, worktree);
2418 if (err)
2419 goto done;
2421 index = fopen(*fileindex_path, "rb");
2422 if (index == NULL) {
2423 if (errno != ENOENT)
2424 err = got_error_from_errno2("fopen", *fileindex_path);
2425 } else {
2426 err = got_fileindex_read(*fileindex, index);
2427 if (fclose(index) == EOF && err == NULL)
2428 err = got_error_from_errno("fclose");
2430 done:
2431 if (err) {
2432 free(*fileindex_path);
2433 *fileindex_path = NULL;
2434 got_fileindex_free(*fileindex);
2435 *fileindex = NULL;
2437 return err;
2440 struct bump_base_commit_id_arg {
2441 struct got_object_id *base_commit_id;
2442 const char *path;
2443 size_t path_len;
2444 const char *entry_name;
2445 got_worktree_checkout_cb progress_cb;
2446 void *progress_arg;
2449 /* Bump base commit ID of all files within an updated part of the work tree. */
2450 static const struct got_error *
2451 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2453 const struct got_error *err;
2454 struct bump_base_commit_id_arg *a = arg;
2456 if (a->entry_name) {
2457 if (strcmp(ie->path, a->path) != 0)
2458 return NULL;
2459 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2460 return NULL;
2462 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2463 SHA1_DIGEST_LENGTH) == 0)
2464 return NULL;
2466 if (a->progress_cb) {
2467 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2468 ie->path);
2469 if (err)
2470 return err;
2472 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2473 return NULL;
2476 static const struct got_error *
2477 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2478 struct got_fileindex *fileindex,
2479 got_worktree_checkout_cb progress_cb, void *progress_arg)
2481 struct bump_base_commit_id_arg bbc_arg;
2483 bbc_arg.base_commit_id = worktree->base_commit_id;
2484 bbc_arg.entry_name = NULL;
2485 bbc_arg.path = "";
2486 bbc_arg.path_len = 0;
2487 bbc_arg.progress_cb = progress_cb;
2488 bbc_arg.progress_arg = progress_arg;
2490 return got_fileindex_for_each_entry_safe(fileindex,
2491 bump_base_commit_id, &bbc_arg);
2494 static const struct got_error *
2495 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2497 const struct got_error *err = NULL;
2498 char *new_fileindex_path = NULL;
2499 FILE *new_index = NULL;
2500 struct timespec timeout;
2502 err = got_opentemp_named(&new_fileindex_path, &new_index,
2503 fileindex_path);
2504 if (err)
2505 goto done;
2507 err = got_fileindex_write(fileindex, new_index);
2508 if (err)
2509 goto done;
2511 if (rename(new_fileindex_path, fileindex_path) != 0) {
2512 err = got_error_from_errno3("rename", new_fileindex_path,
2513 fileindex_path);
2514 unlink(new_fileindex_path);
2518 * Sleep for a short amount of time to ensure that files modified after
2519 * this program exits have a different time stamp from the one which
2520 * was recorded in the file index.
2522 timeout.tv_sec = 0;
2523 timeout.tv_nsec = 1;
2524 nanosleep(&timeout, NULL);
2525 done:
2526 if (new_index)
2527 fclose(new_index);
2528 free(new_fileindex_path);
2529 return err;
2532 static const struct got_error *
2533 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2534 struct got_object_id **tree_id, const char *wt_relpath,
2535 struct got_worktree *worktree, struct got_repository *repo)
2537 const struct got_error *err = NULL;
2538 struct got_object_id *id = NULL;
2539 char *in_repo_path = NULL;
2540 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2542 *entry_type = GOT_OBJ_TYPE_ANY;
2543 *tree_relpath = NULL;
2544 *tree_id = NULL;
2546 if (wt_relpath[0] == '\0') {
2547 /* Check out all files within the work tree. */
2548 *entry_type = GOT_OBJ_TYPE_TREE;
2549 *tree_relpath = strdup("");
2550 if (*tree_relpath == NULL) {
2551 err = got_error_from_errno("strdup");
2552 goto done;
2554 err = got_object_id_by_path(tree_id, repo,
2555 worktree->base_commit_id, worktree->path_prefix);
2556 if (err)
2557 goto done;
2558 return NULL;
2561 /* Check out a subset of files in the work tree. */
2563 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2564 is_root_wt ? "" : "/", wt_relpath) == -1) {
2565 err = got_error_from_errno("asprintf");
2566 goto done;
2569 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2570 in_repo_path);
2571 if (err)
2572 goto done;
2574 free(in_repo_path);
2575 in_repo_path = NULL;
2577 err = got_object_get_type(entry_type, repo, id);
2578 if (err)
2579 goto done;
2581 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2582 /* Check out a single file. */
2583 if (strchr(wt_relpath, '/') == NULL) {
2584 /* Check out a single file in work tree's root dir. */
2585 in_repo_path = strdup(worktree->path_prefix);
2586 if (in_repo_path == NULL) {
2587 err = got_error_from_errno("strdup");
2588 goto done;
2590 *tree_relpath = strdup("");
2591 if (*tree_relpath == NULL) {
2592 err = got_error_from_errno("strdup");
2593 goto done;
2595 } else {
2596 /* Check out a single file in a subdirectory. */
2597 err = got_path_dirname(tree_relpath, wt_relpath);
2598 if (err)
2599 return err;
2600 if (asprintf(&in_repo_path, "%s%s%s",
2601 worktree->path_prefix, is_root_wt ? "" : "/",
2602 *tree_relpath) == -1) {
2603 err = got_error_from_errno("asprintf");
2604 goto done;
2607 err = got_object_id_by_path(tree_id, repo,
2608 worktree->base_commit_id, in_repo_path);
2609 } else {
2610 /* Check out all files within a subdirectory. */
2611 *tree_id = got_object_id_dup(id);
2612 if (*tree_id == NULL) {
2613 err = got_error_from_errno("got_object_id_dup");
2614 goto done;
2616 *tree_relpath = strdup(wt_relpath);
2617 if (*tree_relpath == NULL) {
2618 err = got_error_from_errno("strdup");
2619 goto done;
2622 done:
2623 free(id);
2624 free(in_repo_path);
2625 if (err) {
2626 *entry_type = GOT_OBJ_TYPE_ANY;
2627 free(*tree_relpath);
2628 *tree_relpath = NULL;
2629 free(*tree_id);
2630 *tree_id = NULL;
2632 return err;
2635 static const struct got_error *
2636 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2637 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2638 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2639 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2641 const struct got_error *err = NULL;
2642 struct got_commit_object *commit = NULL;
2643 struct got_tree_object *tree = NULL;
2644 struct got_fileindex_diff_tree_cb diff_cb;
2645 struct diff_cb_arg arg;
2647 err = ref_base_commit(worktree, repo);
2648 if (err) {
2649 if (!(err->code == GOT_ERR_ERRNO &&
2650 (errno == EACCES || errno == EROFS)))
2651 goto done;
2652 err = (*progress_cb)(progress_arg,
2653 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2654 if (err)
2655 return err;
2658 err = got_object_open_as_commit(&commit, repo,
2659 worktree->base_commit_id);
2660 if (err)
2661 goto done;
2663 err = got_object_open_as_tree(&tree, repo, tree_id);
2664 if (err)
2665 goto done;
2667 if (entry_name &&
2668 got_object_tree_find_entry(tree, entry_name) == NULL) {
2669 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2670 goto done;
2673 diff_cb.diff_old_new = diff_old_new;
2674 diff_cb.diff_old = diff_old;
2675 diff_cb.diff_new = diff_new;
2676 arg.fileindex = fileindex;
2677 arg.worktree = worktree;
2678 arg.repo = repo;
2679 arg.progress_cb = progress_cb;
2680 arg.progress_arg = progress_arg;
2681 arg.cancel_cb = cancel_cb;
2682 arg.cancel_arg = cancel_arg;
2683 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2684 entry_name, repo, &diff_cb, &arg);
2685 done:
2686 if (tree)
2687 got_object_tree_close(tree);
2688 if (commit)
2689 got_object_commit_close(commit);
2690 return err;
2693 const struct got_error *
2694 got_worktree_checkout_files(struct got_worktree *worktree,
2695 struct got_pathlist_head *paths, struct got_repository *repo,
2696 got_worktree_checkout_cb progress_cb, void *progress_arg,
2697 got_cancel_cb cancel_cb, void *cancel_arg)
2699 const struct got_error *err = NULL, *sync_err, *unlockerr;
2700 struct got_commit_object *commit = NULL;
2701 struct got_tree_object *tree = NULL;
2702 struct got_fileindex *fileindex = NULL;
2703 char *fileindex_path = NULL;
2704 struct got_pathlist_entry *pe;
2705 struct tree_path_data {
2706 SIMPLEQ_ENTRY(tree_path_data) entry;
2707 struct got_object_id *tree_id;
2708 int entry_type;
2709 char *relpath;
2710 char *entry_name;
2711 } *tpd = NULL;
2712 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2714 SIMPLEQ_INIT(&tree_paths);
2716 err = lock_worktree(worktree, LOCK_EX);
2717 if (err)
2718 return err;
2720 /* Map all specified paths to in-repository trees. */
2721 TAILQ_FOREACH(pe, paths, entry) {
2722 tpd = malloc(sizeof(*tpd));
2723 if (tpd == NULL) {
2724 err = got_error_from_errno("malloc");
2725 goto done;
2728 err = find_tree_entry_for_checkout(&tpd->entry_type,
2729 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2730 if (err) {
2731 free(tpd);
2732 goto done;
2735 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2736 err = got_path_basename(&tpd->entry_name, pe->path);
2737 if (err) {
2738 free(tpd->relpath);
2739 free(tpd->tree_id);
2740 free(tpd);
2741 goto done;
2743 } else
2744 tpd->entry_name = NULL;
2746 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2750 * Read the file index.
2751 * Checking out files is supposed to be an idempotent operation.
2752 * If the on-disk file index is incomplete we will try to complete it.
2754 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2755 if (err)
2756 goto done;
2758 tpd = SIMPLEQ_FIRST(&tree_paths);
2759 TAILQ_FOREACH(pe, paths, entry) {
2760 struct bump_base_commit_id_arg bbc_arg;
2762 err = checkout_files(worktree, fileindex, tpd->relpath,
2763 tpd->tree_id, tpd->entry_name, repo,
2764 progress_cb, progress_arg, cancel_cb, cancel_arg);
2765 if (err)
2766 break;
2768 bbc_arg.base_commit_id = worktree->base_commit_id;
2769 bbc_arg.entry_name = tpd->entry_name;
2770 bbc_arg.path = pe->path;
2771 bbc_arg.path_len = pe->path_len;
2772 bbc_arg.progress_cb = progress_cb;
2773 bbc_arg.progress_arg = progress_arg;
2774 err = got_fileindex_for_each_entry_safe(fileindex,
2775 bump_base_commit_id, &bbc_arg);
2776 if (err)
2777 break;
2779 tpd = SIMPLEQ_NEXT(tpd, entry);
2781 sync_err = sync_fileindex(fileindex, fileindex_path);
2782 if (sync_err && err == NULL)
2783 err = sync_err;
2784 done:
2785 free(fileindex_path);
2786 if (tree)
2787 got_object_tree_close(tree);
2788 if (commit)
2789 got_object_commit_close(commit);
2790 if (fileindex)
2791 got_fileindex_free(fileindex);
2792 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2793 tpd = SIMPLEQ_FIRST(&tree_paths);
2794 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2795 free(tpd->relpath);
2796 free(tpd->tree_id);
2797 free(tpd);
2799 unlockerr = lock_worktree(worktree, LOCK_SH);
2800 if (unlockerr && err == NULL)
2801 err = unlockerr;
2802 return err;
2805 struct merge_file_cb_arg {
2806 struct got_worktree *worktree;
2807 struct got_fileindex *fileindex;
2808 got_worktree_checkout_cb progress_cb;
2809 void *progress_arg;
2810 got_cancel_cb cancel_cb;
2811 void *cancel_arg;
2812 const char *label_orig;
2813 struct got_object_id *commit_id2;
2816 static const struct got_error *
2817 merge_file_cb(void *arg, struct got_blob_object *blob1,
2818 struct got_blob_object *blob2, struct got_object_id *id1,
2819 struct got_object_id *id2, const char *path1, const char *path2,
2820 mode_t mode1, mode_t mode2, struct got_repository *repo)
2822 static const struct got_error *err = NULL;
2823 struct merge_file_cb_arg *a = arg;
2824 struct got_fileindex_entry *ie;
2825 char *ondisk_path = NULL;
2826 struct stat sb;
2827 unsigned char status;
2828 int local_changes_subsumed;
2830 if (blob1 && blob2) {
2831 ie = got_fileindex_entry_get(a->fileindex, path2,
2832 strlen(path2));
2833 if (ie == NULL)
2834 return (*a->progress_cb)(a->progress_arg,
2835 GOT_STATUS_MISSING, path2);
2837 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2838 path2) == -1)
2839 return got_error_from_errno("asprintf");
2841 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2842 repo);
2843 if (err)
2844 goto done;
2846 if (status == GOT_STATUS_DELETE) {
2847 err = (*a->progress_cb)(a->progress_arg,
2848 GOT_STATUS_MERGE, path2);
2849 goto done;
2851 if (status != GOT_STATUS_NO_CHANGE &&
2852 status != GOT_STATUS_MODIFY &&
2853 status != GOT_STATUS_CONFLICT &&
2854 status != GOT_STATUS_ADD) {
2855 err = (*a->progress_cb)(a->progress_arg, status, path2);
2856 goto done;
2859 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2860 char *link_target2;
2861 err = got_object_blob_read_to_str(&link_target2, blob2);
2862 if (err)
2863 goto done;
2864 err = merge_symlink(a->worktree, blob1, ondisk_path,
2865 path2, a->label_orig, link_target2, a->commit_id2,
2866 repo, a->progress_cb, a->progress_arg);
2867 free(link_target2);
2868 } else {
2869 err = merge_blob(&local_changes_subsumed, a->worktree,
2870 blob1, ondisk_path, path2, sb.st_mode,
2871 a->label_orig, blob2, a->commit_id2, repo,
2872 a->progress_cb, a->progress_arg);
2874 } else if (blob1) {
2875 ie = got_fileindex_entry_get(a->fileindex, path1,
2876 strlen(path1));
2877 if (ie == NULL)
2878 return (*a->progress_cb)(a->progress_arg,
2879 GOT_STATUS_MISSING, path1);
2881 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2882 path1) == -1)
2883 return got_error_from_errno("asprintf");
2885 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2886 repo);
2887 if (err)
2888 goto done;
2890 switch (status) {
2891 case GOT_STATUS_NO_CHANGE:
2892 err = (*a->progress_cb)(a->progress_arg,
2893 GOT_STATUS_DELETE, path1);
2894 if (err)
2895 goto done;
2896 err = remove_ondisk_file(a->worktree->root_path, path1);
2897 if (err)
2898 goto done;
2899 if (ie)
2900 got_fileindex_entry_mark_deleted_from_disk(ie);
2901 break;
2902 case GOT_STATUS_DELETE:
2903 case GOT_STATUS_MISSING:
2904 err = (*a->progress_cb)(a->progress_arg,
2905 GOT_STATUS_DELETE, path1);
2906 if (err)
2907 goto done;
2908 if (ie)
2909 got_fileindex_entry_mark_deleted_from_disk(ie);
2910 break;
2911 case GOT_STATUS_ADD: {
2912 struct got_object_id *id;
2913 FILE *blob1_f;
2915 * Delete the added file only if its content already
2916 * exists in the repository.
2918 err = got_object_blob_file_create(&id, &blob1_f, path1);
2919 if (err)
2920 goto done;
2921 if (got_object_id_cmp(id, id1) == 0) {
2922 err = (*a->progress_cb)(a->progress_arg,
2923 GOT_STATUS_DELETE, path1);
2924 if (err)
2925 goto done;
2926 err = remove_ondisk_file(a->worktree->root_path,
2927 path1);
2928 if (err)
2929 goto done;
2930 if (ie)
2931 got_fileindex_entry_remove(a->fileindex,
2932 ie);
2933 } else {
2934 err = (*a->progress_cb)(a->progress_arg,
2935 GOT_STATUS_CANNOT_DELETE, path1);
2937 if (fclose(blob1_f) == EOF && err == NULL)
2938 err = got_error_from_errno("fclose");
2939 free(id);
2940 if (err)
2941 goto done;
2942 break;
2944 case GOT_STATUS_MODIFY:
2945 case GOT_STATUS_CONFLICT:
2946 err = (*a->progress_cb)(a->progress_arg,
2947 GOT_STATUS_CANNOT_DELETE, path1);
2948 if (err)
2949 goto done;
2950 break;
2951 case GOT_STATUS_OBSTRUCTED:
2952 err = (*a->progress_cb)(a->progress_arg, status, path1);
2953 if (err)
2954 goto done;
2955 break;
2956 default:
2957 break;
2959 } else if (blob2) {
2960 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2961 path2) == -1)
2962 return got_error_from_errno("asprintf");
2963 ie = got_fileindex_entry_get(a->fileindex, path2,
2964 strlen(path2));
2965 if (ie) {
2966 err = get_file_status(&status, &sb, ie, ondisk_path,
2967 -1, NULL, repo);
2968 if (err)
2969 goto done;
2970 if (status != GOT_STATUS_NO_CHANGE &&
2971 status != GOT_STATUS_MODIFY &&
2972 status != GOT_STATUS_CONFLICT &&
2973 status != GOT_STATUS_ADD) {
2974 err = (*a->progress_cb)(a->progress_arg,
2975 status, path2);
2976 goto done;
2978 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2979 char *link_target2;
2980 err = got_object_blob_read_to_str(&link_target2,
2981 blob2);
2982 if (err)
2983 goto done;
2984 err = merge_symlink(a->worktree, NULL,
2985 ondisk_path, path2, a->label_orig,
2986 link_target2, a->commit_id2, repo,
2987 a->progress_cb, a->progress_arg);
2988 free(link_target2);
2989 } else if (S_ISREG(sb.st_mode)) {
2990 err = merge_blob(&local_changes_subsumed,
2991 a->worktree, NULL, ondisk_path, path2,
2992 sb.st_mode, a->label_orig, blob2,
2993 a->commit_id2, repo, a->progress_cb,
2994 a->progress_arg);
2995 } else {
2996 err = got_error_path(ondisk_path,
2997 GOT_ERR_FILE_OBSTRUCTED);
2999 if (err)
3000 goto done;
3001 if (status == GOT_STATUS_DELETE) {
3002 err = got_fileindex_entry_update(ie,
3003 a->worktree->root_fd, path2, blob2->id.sha1,
3004 a->worktree->base_commit_id->sha1, 0);
3005 if (err)
3006 goto done;
3008 } else {
3009 int is_bad_symlink = 0;
3010 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3011 if (S_ISLNK(mode2)) {
3012 err = install_symlink(&is_bad_symlink,
3013 a->worktree, ondisk_path, path2, blob2, 0,
3014 0, 1, repo, a->progress_cb, a->progress_arg);
3015 } else {
3016 err = install_blob(a->worktree, ondisk_path, path2,
3017 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3018 a->progress_cb, a->progress_arg);
3020 if (err)
3021 goto done;
3022 err = got_fileindex_entry_alloc(&ie, path2);
3023 if (err)
3024 goto done;
3025 err = got_fileindex_entry_update(ie,
3026 a->worktree->root_fd, path2, NULL, NULL, 1);
3027 if (err) {
3028 got_fileindex_entry_free(ie);
3029 goto done;
3031 err = got_fileindex_entry_add(a->fileindex, ie);
3032 if (err) {
3033 got_fileindex_entry_free(ie);
3034 goto done;
3036 if (is_bad_symlink) {
3037 got_fileindex_entry_filetype_set(ie,
3038 GOT_FILEIDX_MODE_BAD_SYMLINK);
3042 done:
3043 free(ondisk_path);
3044 return err;
3047 struct check_merge_ok_arg {
3048 struct got_worktree *worktree;
3049 struct got_repository *repo;
3052 static const struct got_error *
3053 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3055 const struct got_error *err = NULL;
3056 struct check_merge_ok_arg *a = arg;
3057 unsigned char status;
3058 struct stat sb;
3059 char *ondisk_path;
3061 /* Reject merges into a work tree with mixed base commits. */
3062 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3063 SHA1_DIGEST_LENGTH))
3064 return got_error(GOT_ERR_MIXED_COMMITS);
3066 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3067 == -1)
3068 return got_error_from_errno("asprintf");
3070 /* Reject merges into a work tree with conflicted files. */
3071 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3072 if (err)
3073 return err;
3074 if (status == GOT_STATUS_CONFLICT)
3075 return got_error(GOT_ERR_CONFLICTS);
3077 return NULL;
3080 static const struct got_error *
3081 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3082 const char *fileindex_path, struct got_object_id *commit_id1,
3083 struct got_object_id *commit_id2, struct got_repository *repo,
3084 got_worktree_checkout_cb progress_cb, void *progress_arg,
3085 got_cancel_cb cancel_cb, void *cancel_arg)
3087 const struct got_error *err = NULL, *sync_err;
3088 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3089 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3090 struct merge_file_cb_arg arg;
3091 char *label_orig = NULL;
3093 if (commit_id1) {
3094 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3095 worktree->path_prefix);
3096 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3097 goto done;
3099 if (tree_id1) {
3100 char *id_str;
3102 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3103 if (err)
3104 goto done;
3106 err = got_object_id_str(&id_str, commit_id1);
3107 if (err)
3108 goto done;
3110 if (asprintf(&label_orig, "%s: commit %s",
3111 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3112 err = got_error_from_errno("asprintf");
3113 free(id_str);
3114 goto done;
3116 free(id_str);
3119 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3120 worktree->path_prefix);
3121 if (err)
3122 goto done;
3124 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3125 if (err)
3126 goto done;
3128 arg.worktree = worktree;
3129 arg.fileindex = fileindex;
3130 arg.progress_cb = progress_cb;
3131 arg.progress_arg = progress_arg;
3132 arg.cancel_cb = cancel_cb;
3133 arg.cancel_arg = cancel_arg;
3134 arg.label_orig = label_orig;
3135 arg.commit_id2 = commit_id2;
3136 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3137 sync_err = sync_fileindex(fileindex, fileindex_path);
3138 if (sync_err && err == NULL)
3139 err = sync_err;
3140 done:
3141 if (tree1)
3142 got_object_tree_close(tree1);
3143 if (tree2)
3144 got_object_tree_close(tree2);
3145 free(label_orig);
3146 return err;
3149 const struct got_error *
3150 got_worktree_merge_files(struct got_worktree *worktree,
3151 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3152 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3153 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3155 const struct got_error *err, *unlockerr;
3156 char *fileindex_path = NULL;
3157 struct got_fileindex *fileindex = NULL;
3158 struct check_merge_ok_arg mok_arg;
3160 err = lock_worktree(worktree, LOCK_EX);
3161 if (err)
3162 return err;
3164 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3165 if (err)
3166 goto done;
3168 mok_arg.worktree = worktree;
3169 mok_arg.repo = repo;
3170 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3171 &mok_arg);
3172 if (err)
3173 goto done;
3175 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3176 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3177 done:
3178 if (fileindex)
3179 got_fileindex_free(fileindex);
3180 free(fileindex_path);
3181 unlockerr = lock_worktree(worktree, LOCK_SH);
3182 if (unlockerr && err == NULL)
3183 err = unlockerr;
3184 return err;
3187 struct diff_dir_cb_arg {
3188 struct got_fileindex *fileindex;
3189 struct got_worktree *worktree;
3190 const char *status_path;
3191 size_t status_path_len;
3192 struct got_repository *repo;
3193 got_worktree_status_cb status_cb;
3194 void *status_arg;
3195 got_cancel_cb cancel_cb;
3196 void *cancel_arg;
3197 /* A pathlist containing per-directory pathlists of ignore patterns. */
3198 struct got_pathlist_head ignores;
3199 int report_unchanged;
3200 int no_ignores;
3203 static const struct got_error *
3204 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3205 int dirfd, const char *de_name,
3206 got_worktree_status_cb status_cb, void *status_arg,
3207 struct got_repository *repo, int report_unchanged)
3209 const struct got_error *err = NULL;
3210 unsigned char status = GOT_STATUS_NO_CHANGE;
3211 unsigned char staged_status = get_staged_status(ie);
3212 struct stat sb;
3213 struct got_object_id blob_id, commit_id, staged_blob_id;
3214 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3215 struct got_object_id *staged_blob_idp = NULL;
3217 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3218 if (err)
3219 return err;
3221 if (status == GOT_STATUS_NO_CHANGE &&
3222 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3223 return NULL;
3225 if (got_fileindex_entry_has_blob(ie)) {
3226 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3227 blob_idp = &blob_id;
3229 if (got_fileindex_entry_has_commit(ie)) {
3230 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3231 commit_idp = &commit_id;
3233 if (staged_status == GOT_STATUS_ADD ||
3234 staged_status == GOT_STATUS_MODIFY) {
3235 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3236 SHA1_DIGEST_LENGTH);
3237 staged_blob_idp = &staged_blob_id;
3240 return (*status_cb)(status_arg, status, staged_status,
3241 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3244 static const struct got_error *
3245 status_old_new(void *arg, struct got_fileindex_entry *ie,
3246 struct dirent *de, const char *parent_path, int dirfd)
3248 const struct got_error *err = NULL;
3249 struct diff_dir_cb_arg *a = arg;
3250 char *abspath;
3252 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3253 return got_error(GOT_ERR_CANCELLED);
3255 if (got_path_cmp(parent_path, a->status_path,
3256 strlen(parent_path), a->status_path_len) != 0 &&
3257 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3258 return NULL;
3260 if (parent_path[0]) {
3261 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3262 parent_path, de->d_name) == -1)
3263 return got_error_from_errno("asprintf");
3264 } else {
3265 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3266 de->d_name) == -1)
3267 return got_error_from_errno("asprintf");
3270 err = report_file_status(ie, abspath, dirfd, de->d_name,
3271 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3272 free(abspath);
3273 return err;
3276 static const struct got_error *
3277 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3279 struct diff_dir_cb_arg *a = arg;
3280 struct got_object_id blob_id, commit_id;
3281 unsigned char status;
3283 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3284 return got_error(GOT_ERR_CANCELLED);
3286 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3287 return NULL;
3289 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3290 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3291 if (got_fileindex_entry_has_file_on_disk(ie))
3292 status = GOT_STATUS_MISSING;
3293 else
3294 status = GOT_STATUS_DELETE;
3295 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3296 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3299 void
3300 free_ignorelist(struct got_pathlist_head *ignorelist)
3302 struct got_pathlist_entry *pe;
3304 TAILQ_FOREACH(pe, ignorelist, entry)
3305 free((char *)pe->path);
3306 got_pathlist_free(ignorelist);
3309 void
3310 free_ignores(struct got_pathlist_head *ignores)
3312 struct got_pathlist_entry *pe;
3314 TAILQ_FOREACH(pe, ignores, entry) {
3315 struct got_pathlist_head *ignorelist = pe->data;
3316 free_ignorelist(ignorelist);
3317 free((char *)pe->path);
3319 got_pathlist_free(ignores);
3322 static const struct got_error *
3323 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3325 const struct got_error *err = NULL;
3326 struct got_pathlist_entry *pe = NULL;
3327 struct got_pathlist_head *ignorelist;
3328 char *line = NULL, *pattern, *dirpath = NULL;
3329 size_t linesize = 0;
3330 ssize_t linelen;
3332 ignorelist = calloc(1, sizeof(*ignorelist));
3333 if (ignorelist == NULL)
3334 return got_error_from_errno("calloc");
3335 TAILQ_INIT(ignorelist);
3337 while ((linelen = getline(&line, &linesize, f)) != -1) {
3338 if (linelen > 0 && line[linelen - 1] == '\n')
3339 line[linelen - 1] = '\0';
3341 /* Git's ignores may contain comments. */
3342 if (line[0] == '#')
3343 continue;
3345 /* Git's negated patterns are not (yet?) supported. */
3346 if (line[0] == '!')
3347 continue;
3349 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3350 line) == -1) {
3351 err = got_error_from_errno("asprintf");
3352 goto done;
3354 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3355 if (err)
3356 goto done;
3358 if (ferror(f)) {
3359 err = got_error_from_errno("getline");
3360 goto done;
3363 dirpath = strdup(path);
3364 if (dirpath == NULL) {
3365 err = got_error_from_errno("strdup");
3366 goto done;
3368 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3369 done:
3370 free(line);
3371 if (err || pe == NULL) {
3372 free(dirpath);
3373 free_ignorelist(ignorelist);
3375 return err;
3378 int
3379 match_ignores(struct got_pathlist_head *ignores, const char *path)
3381 struct got_pathlist_entry *pe;
3383 /* Handle patterns which match in all directories. */
3384 TAILQ_FOREACH(pe, ignores, entry) {
3385 struct got_pathlist_head *ignorelist = pe->data;
3386 struct got_pathlist_entry *pi;
3388 TAILQ_FOREACH(pi, ignorelist, entry) {
3389 const char *p, *pattern = pi->path;
3391 if (strncmp(pattern, "**/", 3) != 0)
3392 continue;
3393 pattern += 3;
3394 p = path;
3395 while (*p) {
3396 if (fnmatch(pattern, p,
3397 FNM_PATHNAME | FNM_LEADING_DIR)) {
3398 /* Retry in next directory. */
3399 while (*p && *p != '/')
3400 p++;
3401 while (*p == '/')
3402 p++;
3403 continue;
3405 return 1;
3411 * The ignores pathlist contains ignore lists from children before
3412 * parents, so we can find the most specific ignorelist by walking
3413 * ignores backwards.
3415 pe = TAILQ_LAST(ignores, got_pathlist_head);
3416 while (pe) {
3417 if (got_path_is_child(path, pe->path, pe->path_len)) {
3418 struct got_pathlist_head *ignorelist = pe->data;
3419 struct got_pathlist_entry *pi;
3420 TAILQ_FOREACH(pi, ignorelist, entry) {
3421 const char *pattern = pi->path;
3422 int flags = FNM_LEADING_DIR;
3423 if (strstr(pattern, "/**/") == NULL)
3424 flags |= FNM_PATHNAME;
3425 if (fnmatch(pattern, path, flags))
3426 continue;
3427 return 1;
3430 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3433 return 0;
3436 static const struct got_error *
3437 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3438 const char *path, int dirfd, const char *ignores_filename)
3440 const struct got_error *err = NULL;
3441 char *ignorespath;
3442 int fd = -1;
3443 FILE *ignoresfile = NULL;
3445 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3446 path[0] ? "/" : "", ignores_filename) == -1)
3447 return got_error_from_errno("asprintf");
3449 if (dirfd != -1) {
3450 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3451 if (fd == -1) {
3452 if (errno != ENOENT && errno != EACCES)
3453 err = got_error_from_errno2("openat",
3454 ignorespath);
3455 } else {
3456 ignoresfile = fdopen(fd, "r");
3457 if (ignoresfile == NULL)
3458 err = got_error_from_errno2("fdopen",
3459 ignorespath);
3460 else {
3461 fd = -1;
3462 err = read_ignores(ignores, path, ignoresfile);
3465 } else {
3466 ignoresfile = fopen(ignorespath, "r");
3467 if (ignoresfile == NULL) {
3468 if (errno != ENOENT && errno != EACCES)
3469 err = got_error_from_errno2("fopen",
3470 ignorespath);
3471 } else
3472 err = read_ignores(ignores, path, ignoresfile);
3475 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3476 err = got_error_from_errno2("fclose", path);
3477 if (fd != -1 && close(fd) == -1 && err == NULL)
3478 err = got_error_from_errno2("close", path);
3479 free(ignorespath);
3480 return err;
3483 static const struct got_error *
3484 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3486 const struct got_error *err = NULL;
3487 struct diff_dir_cb_arg *a = arg;
3488 char *path = NULL;
3490 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3491 return got_error(GOT_ERR_CANCELLED);
3493 if (parent_path[0]) {
3494 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3495 return got_error_from_errno("asprintf");
3496 } else {
3497 path = de->d_name;
3500 if (de->d_type != DT_DIR &&
3501 got_path_is_child(path, a->status_path, a->status_path_len)
3502 && !match_ignores(&a->ignores, path))
3503 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3504 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3505 if (parent_path[0])
3506 free(path);
3507 return err;
3510 static const struct got_error *
3511 status_traverse(void *arg, const char *path, int dirfd)
3513 const struct got_error *err = NULL;
3514 struct diff_dir_cb_arg *a = arg;
3516 if (a->no_ignores)
3517 return NULL;
3519 err = add_ignores(&a->ignores, a->worktree->root_path,
3520 path, dirfd, ".cvsignore");
3521 if (err)
3522 return err;
3524 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3525 dirfd, ".gitignore");
3527 return err;
3530 static const struct got_error *
3531 report_single_file_status(const char *path, const char *ondisk_path,
3532 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3533 void *status_arg, struct got_repository *repo, int report_unchanged)
3535 struct got_fileindex_entry *ie;
3536 struct stat sb;
3538 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3539 if (ie)
3540 return report_file_status(ie, ondisk_path, -1, NULL,
3541 status_cb, status_arg, repo, report_unchanged);
3543 if (lstat(ondisk_path, &sb) == -1) {
3544 if (errno != ENOENT)
3545 return got_error_from_errno2("lstat", ondisk_path);
3546 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3547 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3548 return NULL;
3551 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3552 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3553 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3555 return NULL;
3558 static const struct got_error *
3559 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3560 const char *root_path, const char *path)
3562 const struct got_error *err;
3563 char *parent_path, *next_parent_path = NULL;
3565 err = add_ignores(ignores, root_path, "", -1,
3566 ".cvsignore");
3567 if (err)
3568 return err;
3570 err = add_ignores(ignores, root_path, "", -1,
3571 ".gitignore");
3572 if (err)
3573 return err;
3575 err = got_path_dirname(&parent_path, path);
3576 if (err) {
3577 if (err->code == GOT_ERR_BAD_PATH)
3578 return NULL; /* cannot traverse parent */
3579 return err;
3581 for (;;) {
3582 err = add_ignores(ignores, root_path, parent_path, -1,
3583 ".cvsignore");
3584 if (err)
3585 break;
3586 err = add_ignores(ignores, root_path, parent_path, -1,
3587 ".gitignore");
3588 if (err)
3589 break;
3590 err = got_path_dirname(&next_parent_path, parent_path);
3591 if (err) {
3592 if (err->code == GOT_ERR_BAD_PATH)
3593 err = NULL; /* traversed everything */
3594 break;
3596 free(parent_path);
3597 parent_path = next_parent_path;
3598 next_parent_path = NULL;
3601 free(parent_path);
3602 free(next_parent_path);
3603 return err;
3606 static const struct got_error *
3607 worktree_status(struct got_worktree *worktree, const char *path,
3608 struct got_fileindex *fileindex, struct got_repository *repo,
3609 got_worktree_status_cb status_cb, void *status_arg,
3610 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3611 int report_unchanged)
3613 const struct got_error *err = NULL;
3614 int fd = -1;
3615 struct got_fileindex_diff_dir_cb fdiff_cb;
3616 struct diff_dir_cb_arg arg;
3617 char *ondisk_path = NULL;
3619 TAILQ_INIT(&arg.ignores);
3621 if (asprintf(&ondisk_path, "%s%s%s",
3622 worktree->root_path, path[0] ? "/" : "", path) == -1)
3623 return got_error_from_errno("asprintf");
3625 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3626 if (fd == -1) {
3627 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3628 errno != ELOOP)
3629 err = got_error_from_errno2("open", ondisk_path);
3630 else
3631 err = report_single_file_status(path, ondisk_path,
3632 fileindex, status_cb, status_arg, repo,
3633 report_unchanged);
3634 } else {
3635 fdiff_cb.diff_old_new = status_old_new;
3636 fdiff_cb.diff_old = status_old;
3637 fdiff_cb.diff_new = status_new;
3638 fdiff_cb.diff_traverse = status_traverse;
3639 arg.fileindex = fileindex;
3640 arg.worktree = worktree;
3641 arg.status_path = path;
3642 arg.status_path_len = strlen(path);
3643 arg.repo = repo;
3644 arg.status_cb = status_cb;
3645 arg.status_arg = status_arg;
3646 arg.cancel_cb = cancel_cb;
3647 arg.cancel_arg = cancel_arg;
3648 arg.report_unchanged = report_unchanged;
3649 arg.no_ignores = no_ignores;
3650 if (!no_ignores) {
3651 err = add_ignores_from_parent_paths(&arg.ignores,
3652 worktree->root_path, path);
3653 if (err)
3654 goto done;
3656 err = got_fileindex_diff_dir(fileindex, fd,
3657 worktree->root_path, path, repo, &fdiff_cb, &arg);
3659 done:
3660 free_ignores(&arg.ignores);
3661 if (fd != -1 && close(fd) == -1 && err == NULL)
3662 err = got_error_from_errno("close");
3663 free(ondisk_path);
3664 return err;
3667 const struct got_error *
3668 got_worktree_status(struct got_worktree *worktree,
3669 struct got_pathlist_head *paths, struct got_repository *repo,
3670 got_worktree_status_cb status_cb, void *status_arg,
3671 got_cancel_cb cancel_cb, void *cancel_arg)
3673 const struct got_error *err = NULL;
3674 char *fileindex_path = NULL;
3675 struct got_fileindex *fileindex = NULL;
3676 struct got_pathlist_entry *pe;
3678 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3679 if (err)
3680 return err;
3682 TAILQ_FOREACH(pe, paths, entry) {
3683 err = worktree_status(worktree, pe->path, fileindex, repo,
3684 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3685 if (err)
3686 break;
3688 free(fileindex_path);
3689 got_fileindex_free(fileindex);
3690 return err;
3693 const struct got_error *
3694 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3695 const char *arg)
3697 const struct got_error *err = NULL;
3698 char *resolved = NULL, *cwd = NULL, *path = NULL;
3699 size_t len;
3700 struct stat sb;
3701 char *abspath = NULL;
3702 char canonpath[PATH_MAX];
3704 *wt_path = NULL;
3706 cwd = getcwd(NULL, 0);
3707 if (cwd == NULL)
3708 return got_error_from_errno("getcwd");
3710 if (lstat(arg, &sb) == -1) {
3711 if (errno != ENOENT) {
3712 err = got_error_from_errno2("lstat", arg);
3713 goto done;
3715 sb.st_mode = 0;
3717 if (S_ISLNK(sb.st_mode)) {
3719 * We cannot use realpath(3) with symlinks since we want to
3720 * operate on the symlink itself.
3721 * But we can make the path absolute, assuming it is relative
3722 * to the current working directory, and then canonicalize it.
3724 if (!got_path_is_absolute(arg)) {
3725 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3726 err = got_error_from_errno("asprintf");
3727 goto done;
3731 err = got_canonpath(abspath ? abspath : arg, canonpath,
3732 sizeof(canonpath));
3733 if (err)
3734 goto done;
3735 resolved = strdup(canonpath);
3736 if (resolved == NULL) {
3737 err = got_error_from_errno("strdup");
3738 goto done;
3740 } else {
3741 resolved = realpath(arg, NULL);
3742 if (resolved == NULL) {
3743 if (errno != ENOENT) {
3744 err = got_error_from_errno2("realpath", arg);
3745 goto done;
3747 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3748 err = got_error_from_errno("asprintf");
3749 goto done;
3751 err = got_canonpath(abspath, canonpath,
3752 sizeof(canonpath));
3753 if (err)
3754 goto done;
3755 resolved = strdup(canonpath);
3756 if (resolved == NULL) {
3757 err = got_error_from_errno("strdup");
3758 goto done;
3763 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3764 strlen(got_worktree_get_root_path(worktree)))) {
3765 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3766 goto done;
3769 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3770 err = got_path_skip_common_ancestor(&path,
3771 got_worktree_get_root_path(worktree), resolved);
3772 if (err)
3773 goto done;
3774 } else {
3775 path = strdup("");
3776 if (path == NULL) {
3777 err = got_error_from_errno("strdup");
3778 goto done;
3782 /* XXX status walk can't deal with trailing slash! */
3783 len = strlen(path);
3784 while (len > 0 && path[len - 1] == '/') {
3785 path[len - 1] = '\0';
3786 len--;
3788 done:
3789 free(abspath);
3790 free(resolved);
3791 free(cwd);
3792 if (err == NULL)
3793 *wt_path = path;
3794 else
3795 free(path);
3796 return err;
3799 struct schedule_addition_args {
3800 struct got_worktree *worktree;
3801 struct got_fileindex *fileindex;
3802 got_worktree_checkout_cb progress_cb;
3803 void *progress_arg;
3804 struct got_repository *repo;
3807 static const struct got_error *
3808 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3809 const char *relpath, struct got_object_id *blob_id,
3810 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3811 int dirfd, const char *de_name)
3813 struct schedule_addition_args *a = arg;
3814 const struct got_error *err = NULL;
3815 struct got_fileindex_entry *ie;
3816 struct stat sb;
3817 char *ondisk_path;
3819 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3820 relpath) == -1)
3821 return got_error_from_errno("asprintf");
3823 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3824 if (ie) {
3825 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3826 de_name, a->repo);
3827 if (err)
3828 goto done;
3829 /* Re-adding an existing entry is a no-op. */
3830 if (status == GOT_STATUS_ADD)
3831 goto done;
3832 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3833 if (err)
3834 goto done;
3837 if (status != GOT_STATUS_UNVERSIONED) {
3838 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3839 goto done;
3842 err = got_fileindex_entry_alloc(&ie, relpath);
3843 if (err)
3844 goto done;
3845 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3846 relpath, NULL, NULL, 1);
3847 if (err) {
3848 got_fileindex_entry_free(ie);
3849 goto done;
3851 err = got_fileindex_entry_add(a->fileindex, ie);
3852 if (err) {
3853 got_fileindex_entry_free(ie);
3854 goto done;
3856 done:
3857 free(ondisk_path);
3858 if (err)
3859 return err;
3860 if (status == GOT_STATUS_ADD)
3861 return NULL;
3862 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3865 const struct got_error *
3866 got_worktree_schedule_add(struct got_worktree *worktree,
3867 struct got_pathlist_head *paths,
3868 got_worktree_checkout_cb progress_cb, void *progress_arg,
3869 struct got_repository *repo, int no_ignores)
3871 struct got_fileindex *fileindex = NULL;
3872 char *fileindex_path = NULL;
3873 const struct got_error *err = NULL, *sync_err, *unlockerr;
3874 struct got_pathlist_entry *pe;
3875 struct schedule_addition_args saa;
3877 err = lock_worktree(worktree, LOCK_EX);
3878 if (err)
3879 return err;
3881 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3882 if (err)
3883 goto done;
3885 saa.worktree = worktree;
3886 saa.fileindex = fileindex;
3887 saa.progress_cb = progress_cb;
3888 saa.progress_arg = progress_arg;
3889 saa.repo = repo;
3891 TAILQ_FOREACH(pe, paths, entry) {
3892 err = worktree_status(worktree, pe->path, fileindex, repo,
3893 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3894 if (err)
3895 break;
3897 sync_err = sync_fileindex(fileindex, fileindex_path);
3898 if (sync_err && err == NULL)
3899 err = sync_err;
3900 done:
3901 free(fileindex_path);
3902 if (fileindex)
3903 got_fileindex_free(fileindex);
3904 unlockerr = lock_worktree(worktree, LOCK_SH);
3905 if (unlockerr && err == NULL)
3906 err = unlockerr;
3907 return err;
3910 struct schedule_deletion_args {
3911 struct got_worktree *worktree;
3912 struct got_fileindex *fileindex;
3913 got_worktree_delete_cb progress_cb;
3914 void *progress_arg;
3915 struct got_repository *repo;
3916 int delete_local_mods;
3917 int keep_on_disk;
3918 const char *status_codes;
3921 static const struct got_error *
3922 schedule_for_deletion(void *arg, unsigned char status,
3923 unsigned char staged_status, const char *relpath,
3924 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3925 struct got_object_id *commit_id, int dirfd, const char *de_name)
3927 struct schedule_deletion_args *a = arg;
3928 const struct got_error *err = NULL;
3929 struct got_fileindex_entry *ie = NULL;
3930 struct stat sb;
3931 char *ondisk_path;
3933 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3934 if (ie == NULL)
3935 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3937 staged_status = get_staged_status(ie);
3938 if (staged_status != GOT_STATUS_NO_CHANGE) {
3939 if (staged_status == GOT_STATUS_DELETE)
3940 return NULL;
3941 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3944 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3945 relpath) == -1)
3946 return got_error_from_errno("asprintf");
3948 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3949 a->repo);
3950 if (err)
3951 goto done;
3953 if (a->status_codes) {
3954 size_t ncodes = strlen(a->status_codes);
3955 int i;
3956 for (i = 0; i < ncodes ; i++) {
3957 if (status == a->status_codes[i])
3958 break;
3960 if (i == ncodes) {
3961 /* Do not delete files in non-matching status. */
3962 free(ondisk_path);
3963 return NULL;
3965 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3966 a->status_codes[i] != GOT_STATUS_MISSING) {
3967 static char msg[64];
3968 snprintf(msg, sizeof(msg),
3969 "invalid status code '%c'", a->status_codes[i]);
3970 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3971 goto done;
3975 if (status != GOT_STATUS_NO_CHANGE) {
3976 if (status == GOT_STATUS_DELETE)
3977 goto done;
3978 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3979 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3980 goto done;
3982 if (status != GOT_STATUS_MODIFY &&
3983 status != GOT_STATUS_MISSING) {
3984 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3985 goto done;
3989 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3990 size_t root_len;
3992 if (dirfd != -1) {
3993 if (unlinkat(dirfd, de_name, 0) != 0) {
3994 err = got_error_from_errno2("unlinkat",
3995 ondisk_path);
3996 goto done;
3998 } else if (unlink(ondisk_path) != 0) {
3999 err = got_error_from_errno2("unlink", ondisk_path);
4000 goto done;
4003 root_len = strlen(a->worktree->root_path);
4004 do {
4005 char *parent;
4006 err = got_path_dirname(&parent, ondisk_path);
4007 if (err)
4008 goto done;
4009 free(ondisk_path);
4010 ondisk_path = parent;
4011 if (rmdir(ondisk_path) == -1) {
4012 if (errno != ENOTEMPTY)
4013 err = got_error_from_errno2("rmdir",
4014 ondisk_path);
4015 break;
4017 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4018 strlen(ondisk_path), root_len) != 0);
4021 got_fileindex_entry_mark_deleted_from_disk(ie);
4022 done:
4023 free(ondisk_path);
4024 if (err)
4025 return err;
4026 if (status == GOT_STATUS_DELETE)
4027 return NULL;
4028 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4029 staged_status, relpath);
4032 const struct got_error *
4033 got_worktree_schedule_delete(struct got_worktree *worktree,
4034 struct got_pathlist_head *paths, int delete_local_mods,
4035 const char *status_codes,
4036 got_worktree_delete_cb progress_cb, void *progress_arg,
4037 struct got_repository *repo, int keep_on_disk)
4039 struct got_fileindex *fileindex = NULL;
4040 char *fileindex_path = NULL;
4041 const struct got_error *err = NULL, *sync_err, *unlockerr;
4042 struct got_pathlist_entry *pe;
4043 struct schedule_deletion_args sda;
4045 err = lock_worktree(worktree, LOCK_EX);
4046 if (err)
4047 return err;
4049 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4050 if (err)
4051 goto done;
4053 sda.worktree = worktree;
4054 sda.fileindex = fileindex;
4055 sda.progress_cb = progress_cb;
4056 sda.progress_arg = progress_arg;
4057 sda.repo = repo;
4058 sda.delete_local_mods = delete_local_mods;
4059 sda.keep_on_disk = keep_on_disk;
4060 sda.status_codes = status_codes;
4062 TAILQ_FOREACH(pe, paths, entry) {
4063 err = worktree_status(worktree, pe->path, fileindex, repo,
4064 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4065 if (err)
4066 break;
4068 sync_err = sync_fileindex(fileindex, fileindex_path);
4069 if (sync_err && err == NULL)
4070 err = sync_err;
4071 done:
4072 free(fileindex_path);
4073 if (fileindex)
4074 got_fileindex_free(fileindex);
4075 unlockerr = lock_worktree(worktree, LOCK_SH);
4076 if (unlockerr && err == NULL)
4077 err = unlockerr;
4078 return err;
4081 static const struct got_error *
4082 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4084 const struct got_error *err = NULL;
4085 char *line = NULL;
4086 size_t linesize = 0, n;
4087 ssize_t linelen;
4089 linelen = getline(&line, &linesize, infile);
4090 if (linelen == -1) {
4091 if (ferror(infile)) {
4092 err = got_error_from_errno("getline");
4093 goto done;
4095 return NULL;
4097 if (outfile) {
4098 n = fwrite(line, 1, linelen, outfile);
4099 if (n != linelen) {
4100 err = got_ferror(outfile, GOT_ERR_IO);
4101 goto done;
4104 if (rejectfile) {
4105 n = fwrite(line, 1, linelen, rejectfile);
4106 if (n != linelen)
4107 err = got_ferror(outfile, GOT_ERR_IO);
4109 done:
4110 free(line);
4111 return err;
4114 static const struct got_error *
4115 skip_one_line(FILE *f)
4117 char *line = NULL;
4118 size_t linesize = 0;
4119 ssize_t linelen;
4121 linelen = getline(&line, &linesize, f);
4122 if (linelen == -1) {
4123 if (ferror(f))
4124 return got_error_from_errno("getline");
4125 return NULL;
4127 free(line);
4128 return NULL;
4131 static const struct got_error *
4132 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4133 int start_old, int end_old, int start_new, int end_new,
4134 FILE *outfile, FILE *rejectfile)
4136 const struct got_error *err;
4138 /* Copy old file's lines leading up to patch. */
4139 while (!feof(f1) && *line_cur1 < start_old) {
4140 err = copy_one_line(f1, outfile, NULL);
4141 if (err)
4142 return err;
4143 (*line_cur1)++;
4145 /* Skip new file's lines leading up to patch. */
4146 while (!feof(f2) && *line_cur2 < start_new) {
4147 if (rejectfile)
4148 err = copy_one_line(f2, NULL, rejectfile);
4149 else
4150 err = skip_one_line(f2);
4151 if (err)
4152 return err;
4153 (*line_cur2)++;
4155 /* Copy patched lines. */
4156 while (!feof(f2) && *line_cur2 <= end_new) {
4157 err = copy_one_line(f2, outfile, NULL);
4158 if (err)
4159 return err;
4160 (*line_cur2)++;
4162 /* Skip over old file's replaced lines. */
4163 while (!feof(f1) && *line_cur1 <= end_old) {
4164 if (rejectfile)
4165 err = copy_one_line(f1, NULL, rejectfile);
4166 else
4167 err = skip_one_line(f1);
4168 if (err)
4169 return err;
4170 (*line_cur1)++;
4173 return NULL;
4176 static const struct got_error *
4177 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4178 FILE *outfile, FILE *rejectfile)
4180 const struct got_error *err;
4182 if (outfile) {
4183 /* Copy old file's lines until EOF. */
4184 while (!feof(f1)) {
4185 err = copy_one_line(f1, outfile, NULL);
4186 if (err)
4187 return err;
4188 (*line_cur1)++;
4191 if (rejectfile) {
4192 /* Copy new file's lines until EOF. */
4193 while (!feof(f2)) {
4194 err = copy_one_line(f2, NULL, rejectfile);
4195 if (err)
4196 return err;
4197 (*line_cur2)++;
4201 return NULL;
4204 static const struct got_error *
4205 apply_or_reject_change(int *choice, int *nchunks_used,
4206 struct diff_result *diff_result, int n,
4207 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4208 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4209 got_worktree_patch_cb patch_cb, void *patch_arg)
4211 const struct got_error *err = NULL;
4212 struct diff_chunk_context cc = {};
4213 int start_old, end_old, start_new, end_new;
4214 FILE *hunkfile;
4215 struct diff_output_unidiff_state *diff_state;
4216 struct diff_input_info diff_info;
4217 int rc;
4219 *choice = GOT_PATCH_CHOICE_NONE;
4221 /* Get changed line numbers without context lines for copy_change(). */
4222 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4223 start_old = cc.left.start;
4224 end_old = cc.left.end;
4225 start_new = cc.right.start;
4226 end_new = cc.right.end;
4228 /* Get the same change with context lines for display. */
4229 memset(&cc, 0, sizeof(cc));
4230 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4232 memset(&diff_info, 0, sizeof(diff_info));
4233 diff_info.left_path = relpath;
4234 diff_info.right_path = relpath;
4236 diff_state = diff_output_unidiff_state_alloc();
4237 if (diff_state == NULL)
4238 return got_error_set_errno(ENOMEM,
4239 "diff_output_unidiff_state_alloc");
4241 hunkfile = got_opentemp();
4242 if (hunkfile == NULL) {
4243 err = got_error_from_errno("got_opentemp");
4244 goto done;
4247 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4248 diff_result, &cc);
4249 if (rc != DIFF_RC_OK) {
4250 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4251 goto done;
4254 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4255 err = got_ferror(hunkfile, GOT_ERR_IO);
4256 goto done;
4259 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4260 hunkfile, changeno, nchanges);
4261 if (err)
4262 goto done;
4264 switch (*choice) {
4265 case GOT_PATCH_CHOICE_YES:
4266 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4267 end_old, start_new, end_new, outfile, rejectfile);
4268 break;
4269 case GOT_PATCH_CHOICE_NO:
4270 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4271 end_old, start_new, end_new, rejectfile, outfile);
4272 break;
4273 case GOT_PATCH_CHOICE_QUIT:
4274 break;
4275 default:
4276 err = got_error(GOT_ERR_PATCH_CHOICE);
4277 break;
4279 done:
4280 diff_output_unidiff_state_free(diff_state);
4281 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4282 err = got_error_from_errno("fclose");
4283 return err;
4286 struct revert_file_args {
4287 struct got_worktree *worktree;
4288 struct got_fileindex *fileindex;
4289 got_worktree_checkout_cb progress_cb;
4290 void *progress_arg;
4291 got_worktree_patch_cb patch_cb;
4292 void *patch_arg;
4293 struct got_repository *repo;
4296 static const struct got_error *
4297 create_patched_content(char **path_outfile, int reverse_patch,
4298 struct got_object_id *blob_id, const char *path2,
4299 int dirfd2, const char *de_name2,
4300 const char *relpath, struct got_repository *repo,
4301 got_worktree_patch_cb patch_cb, void *patch_arg)
4303 const struct got_error *err, *free_err;
4304 struct got_blob_object *blob = NULL;
4305 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4306 int fd2 = -1;
4307 char link_target[PATH_MAX];
4308 ssize_t link_len = 0;
4309 char *path1 = NULL, *id_str = NULL;
4310 struct stat sb2;
4311 struct got_diffreg_result *diffreg_result = NULL;
4312 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4313 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4315 *path_outfile = NULL;
4317 err = got_object_id_str(&id_str, blob_id);
4318 if (err)
4319 return err;
4321 if (dirfd2 != -1) {
4322 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4323 if (fd2 == -1) {
4324 if (errno != ELOOP) {
4325 err = got_error_from_errno2("openat", path2);
4326 goto done;
4328 link_len = readlinkat(dirfd2, de_name2,
4329 link_target, sizeof(link_target));
4330 if (link_len == -1)
4331 return got_error_from_errno2("readlinkat", path2);
4332 sb2.st_mode = S_IFLNK;
4333 sb2.st_size = link_len;
4335 } else {
4336 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4337 if (fd2 == -1) {
4338 if (errno != ELOOP) {
4339 err = got_error_from_errno2("open", path2);
4340 goto done;
4342 link_len = readlink(path2, link_target,
4343 sizeof(link_target));
4344 if (link_len == -1)
4345 return got_error_from_errno2("readlink", path2);
4346 sb2.st_mode = S_IFLNK;
4347 sb2.st_size = link_len;
4350 if (fd2 != -1) {
4351 if (fstat(fd2, &sb2) == -1) {
4352 err = got_error_from_errno2("fstat", path2);
4353 goto done;
4356 f2 = fdopen(fd2, "r");
4357 if (f2 == NULL) {
4358 err = got_error_from_errno2("fdopen", path2);
4359 goto done;
4361 fd2 = -1;
4362 } else {
4363 size_t n;
4364 f2 = got_opentemp();
4365 if (f2 == NULL) {
4366 err = got_error_from_errno2("got_opentemp", path2);
4367 goto done;
4369 n = fwrite(link_target, 1, link_len, f2);
4370 if (n != link_len) {
4371 err = got_ferror(f2, GOT_ERR_IO);
4372 goto done;
4374 if (fflush(f2) == EOF) {
4375 err = got_error_from_errno("fflush");
4376 goto done;
4378 rewind(f2);
4381 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4382 if (err)
4383 goto done;
4385 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4386 if (err)
4387 goto done;
4389 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4390 if (err)
4391 goto done;
4393 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4394 NULL);
4395 if (err)
4396 goto done;
4398 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4399 if (err)
4400 goto done;
4402 if (fseek(f1, 0L, SEEK_SET) == -1)
4403 return got_ferror(f1, GOT_ERR_IO);
4404 if (fseek(f2, 0L, SEEK_SET) == -1)
4405 return got_ferror(f2, GOT_ERR_IO);
4407 /* Count the number of actual changes in the diff result. */
4408 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4409 struct diff_chunk_context cc = {};
4410 diff_chunk_context_load_change(&cc, &nchunks_used,
4411 diffreg_result->result, n, 0);
4412 nchanges++;
4414 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4415 int choice;
4416 err = apply_or_reject_change(&choice, &nchunks_used,
4417 diffreg_result->result, n, relpath, f1, f2,
4418 &line_cur1, &line_cur2,
4419 reverse_patch ? NULL : outfile,
4420 reverse_patch ? outfile : NULL,
4421 ++i, nchanges, patch_cb, patch_arg);
4422 if (err)
4423 goto done;
4424 if (choice == GOT_PATCH_CHOICE_YES)
4425 have_content = 1;
4426 else if (choice == GOT_PATCH_CHOICE_QUIT)
4427 break;
4429 if (have_content) {
4430 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4431 reverse_patch ? NULL : outfile,
4432 reverse_patch ? outfile : NULL);
4433 if (err)
4434 goto done;
4436 if (!S_ISLNK(sb2.st_mode)) {
4437 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4438 err = got_error_from_errno2("fchmod", path2);
4439 goto done;
4443 done:
4444 free(id_str);
4445 if (blob)
4446 got_object_blob_close(blob);
4447 free_err = got_diffreg_result_free(diffreg_result);
4448 if (err == NULL)
4449 err = free_err;
4450 if (f1 && fclose(f1) == EOF && err == NULL)
4451 err = got_error_from_errno2("fclose", path1);
4452 if (f2 && fclose(f2) == EOF && err == NULL)
4453 err = got_error_from_errno2("fclose", path2);
4454 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4455 err = got_error_from_errno2("close", path2);
4456 if (outfile && fclose(outfile) == EOF && err == NULL)
4457 err = got_error_from_errno2("fclose", *path_outfile);
4458 if (path1 && unlink(path1) == -1 && err == NULL)
4459 err = got_error_from_errno2("unlink", path1);
4460 if (err || !have_content) {
4461 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4462 err = got_error_from_errno2("unlink", *path_outfile);
4463 free(*path_outfile);
4464 *path_outfile = NULL;
4466 free(path1);
4467 return err;
4470 static const struct got_error *
4471 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4472 const char *relpath, struct got_object_id *blob_id,
4473 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4474 int dirfd, const char *de_name)
4476 struct revert_file_args *a = arg;
4477 const struct got_error *err = NULL;
4478 char *parent_path = NULL;
4479 struct got_fileindex_entry *ie;
4480 struct got_tree_object *tree = NULL;
4481 struct got_object_id *tree_id = NULL;
4482 const struct got_tree_entry *te = NULL;
4483 char *tree_path = NULL, *te_name;
4484 char *ondisk_path = NULL, *path_content = NULL;
4485 struct got_blob_object *blob = NULL;
4487 /* Reverting a staged deletion is a no-op. */
4488 if (status == GOT_STATUS_DELETE &&
4489 staged_status != GOT_STATUS_NO_CHANGE)
4490 return NULL;
4492 if (status == GOT_STATUS_UNVERSIONED)
4493 return (*a->progress_cb)(a->progress_arg,
4494 GOT_STATUS_UNVERSIONED, relpath);
4496 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4497 if (ie == NULL)
4498 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4500 /* Construct in-repository path of tree which contains this blob. */
4501 err = got_path_dirname(&parent_path, ie->path);
4502 if (err) {
4503 if (err->code != GOT_ERR_BAD_PATH)
4504 goto done;
4505 parent_path = strdup("/");
4506 if (parent_path == NULL) {
4507 err = got_error_from_errno("strdup");
4508 goto done;
4511 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4512 tree_path = strdup(parent_path);
4513 if (tree_path == NULL) {
4514 err = got_error_from_errno("strdup");
4515 goto done;
4517 } else {
4518 if (got_path_is_root_dir(parent_path)) {
4519 tree_path = strdup(a->worktree->path_prefix);
4520 if (tree_path == NULL) {
4521 err = got_error_from_errno("strdup");
4522 goto done;
4524 } else {
4525 if (asprintf(&tree_path, "%s/%s",
4526 a->worktree->path_prefix, parent_path) == -1) {
4527 err = got_error_from_errno("asprintf");
4528 goto done;
4533 err = got_object_id_by_path(&tree_id, a->repo,
4534 a->worktree->base_commit_id, tree_path);
4535 if (err) {
4536 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4537 (status == GOT_STATUS_ADD ||
4538 staged_status == GOT_STATUS_ADD)))
4539 goto done;
4540 } else {
4541 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4542 if (err)
4543 goto done;
4545 err = got_path_basename(&te_name, ie->path);
4546 if (err)
4547 goto done;
4549 te = got_object_tree_find_entry(tree, te_name);
4550 free(te_name);
4551 if (te == NULL && status != GOT_STATUS_ADD &&
4552 staged_status != GOT_STATUS_ADD) {
4553 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4554 goto done;
4558 switch (status) {
4559 case GOT_STATUS_ADD:
4560 if (a->patch_cb) {
4561 int choice = GOT_PATCH_CHOICE_NONE;
4562 err = (*a->patch_cb)(&choice, a->patch_arg,
4563 status, ie->path, NULL, 1, 1);
4564 if (err)
4565 goto done;
4566 if (choice != GOT_PATCH_CHOICE_YES)
4567 break;
4569 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4570 ie->path);
4571 if (err)
4572 goto done;
4573 got_fileindex_entry_remove(a->fileindex, ie);
4574 break;
4575 case GOT_STATUS_DELETE:
4576 if (a->patch_cb) {
4577 int choice = GOT_PATCH_CHOICE_NONE;
4578 err = (*a->patch_cb)(&choice, a->patch_arg,
4579 status, ie->path, NULL, 1, 1);
4580 if (err)
4581 goto done;
4582 if (choice != GOT_PATCH_CHOICE_YES)
4583 break;
4585 /* fall through */
4586 case GOT_STATUS_MODIFY:
4587 case GOT_STATUS_MODE_CHANGE:
4588 case GOT_STATUS_CONFLICT:
4589 case GOT_STATUS_MISSING: {
4590 struct got_object_id id;
4591 if (staged_status == GOT_STATUS_ADD ||
4592 staged_status == GOT_STATUS_MODIFY) {
4593 memcpy(id.sha1, ie->staged_blob_sha1,
4594 SHA1_DIGEST_LENGTH);
4595 } else
4596 memcpy(id.sha1, ie->blob_sha1,
4597 SHA1_DIGEST_LENGTH);
4598 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4599 if (err)
4600 goto done;
4602 if (asprintf(&ondisk_path, "%s/%s",
4603 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4604 err = got_error_from_errno("asprintf");
4605 goto done;
4608 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4609 status == GOT_STATUS_CONFLICT)) {
4610 int is_bad_symlink = 0;
4611 err = create_patched_content(&path_content, 1, &id,
4612 ondisk_path, dirfd, de_name, ie->path, a->repo,
4613 a->patch_cb, a->patch_arg);
4614 if (err || path_content == NULL)
4615 break;
4616 if (te && S_ISLNK(te->mode)) {
4617 if (unlink(path_content) == -1) {
4618 err = got_error_from_errno2("unlink",
4619 path_content);
4620 break;
4622 err = install_symlink(&is_bad_symlink,
4623 a->worktree, ondisk_path, ie->path,
4624 blob, 0, 1, 0, a->repo,
4625 a->progress_cb, a->progress_arg);
4626 } else {
4627 if (rename(path_content, ondisk_path) == -1) {
4628 err = got_error_from_errno3("rename",
4629 path_content, ondisk_path);
4630 goto done;
4633 } else {
4634 int is_bad_symlink = 0;
4635 if (te && S_ISLNK(te->mode)) {
4636 err = install_symlink(&is_bad_symlink,
4637 a->worktree, ondisk_path, ie->path,
4638 blob, 0, 1, 0, a->repo,
4639 a->progress_cb, a->progress_arg);
4640 } else {
4641 err = install_blob(a->worktree, ondisk_path,
4642 ie->path,
4643 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4644 got_fileindex_perms_to_st(ie), blob,
4645 0, 1, 0, 0, a->repo,
4646 a->progress_cb, a->progress_arg);
4648 if (err)
4649 goto done;
4650 if (status == GOT_STATUS_DELETE ||
4651 status == GOT_STATUS_MODE_CHANGE) {
4652 err = got_fileindex_entry_update(ie,
4653 a->worktree->root_fd, relpath,
4654 blob->id.sha1,
4655 a->worktree->base_commit_id->sha1, 1);
4656 if (err)
4657 goto done;
4659 if (is_bad_symlink) {
4660 got_fileindex_entry_filetype_set(ie,
4661 GOT_FILEIDX_MODE_BAD_SYMLINK);
4664 break;
4666 default:
4667 break;
4669 done:
4670 free(ondisk_path);
4671 free(path_content);
4672 free(parent_path);
4673 free(tree_path);
4674 if (blob)
4675 got_object_blob_close(blob);
4676 if (tree)
4677 got_object_tree_close(tree);
4678 free(tree_id);
4679 return err;
4682 const struct got_error *
4683 got_worktree_revert(struct got_worktree *worktree,
4684 struct got_pathlist_head *paths,
4685 got_worktree_checkout_cb progress_cb, void *progress_arg,
4686 got_worktree_patch_cb patch_cb, void *patch_arg,
4687 struct got_repository *repo)
4689 struct got_fileindex *fileindex = NULL;
4690 char *fileindex_path = NULL;
4691 const struct got_error *err = NULL, *unlockerr = NULL;
4692 const struct got_error *sync_err = NULL;
4693 struct got_pathlist_entry *pe;
4694 struct revert_file_args rfa;
4696 err = lock_worktree(worktree, LOCK_EX);
4697 if (err)
4698 return err;
4700 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4701 if (err)
4702 goto done;
4704 rfa.worktree = worktree;
4705 rfa.fileindex = fileindex;
4706 rfa.progress_cb = progress_cb;
4707 rfa.progress_arg = progress_arg;
4708 rfa.patch_cb = patch_cb;
4709 rfa.patch_arg = patch_arg;
4710 rfa.repo = repo;
4711 TAILQ_FOREACH(pe, paths, entry) {
4712 err = worktree_status(worktree, pe->path, fileindex, repo,
4713 revert_file, &rfa, NULL, NULL, 0, 0);
4714 if (err)
4715 break;
4717 sync_err = sync_fileindex(fileindex, fileindex_path);
4718 if (sync_err && err == NULL)
4719 err = sync_err;
4720 done:
4721 free(fileindex_path);
4722 if (fileindex)
4723 got_fileindex_free(fileindex);
4724 unlockerr = lock_worktree(worktree, LOCK_SH);
4725 if (unlockerr && err == NULL)
4726 err = unlockerr;
4727 return err;
4730 static void
4731 free_commitable(struct got_commitable *ct)
4733 free(ct->path);
4734 free(ct->in_repo_path);
4735 free(ct->ondisk_path);
4736 free(ct->blob_id);
4737 free(ct->base_blob_id);
4738 free(ct->staged_blob_id);
4739 free(ct->base_commit_id);
4740 free(ct);
4743 struct collect_commitables_arg {
4744 struct got_pathlist_head *commitable_paths;
4745 struct got_repository *repo;
4746 struct got_worktree *worktree;
4747 struct got_fileindex *fileindex;
4748 int have_staged_files;
4749 int allow_bad_symlinks;
4752 static const struct got_error *
4753 collect_commitables(void *arg, unsigned char status,
4754 unsigned char staged_status, const char *relpath,
4755 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4756 struct got_object_id *commit_id, int dirfd, const char *de_name)
4758 struct collect_commitables_arg *a = arg;
4759 const struct got_error *err = NULL;
4760 struct got_commitable *ct = NULL;
4761 struct got_pathlist_entry *new = NULL;
4762 char *parent_path = NULL, *path = NULL;
4763 struct stat sb;
4765 if (a->have_staged_files) {
4766 if (staged_status != GOT_STATUS_MODIFY &&
4767 staged_status != GOT_STATUS_ADD &&
4768 staged_status != GOT_STATUS_DELETE)
4769 return NULL;
4770 } else {
4771 if (status == GOT_STATUS_CONFLICT)
4772 return got_error(GOT_ERR_COMMIT_CONFLICT);
4774 if (status != GOT_STATUS_MODIFY &&
4775 status != GOT_STATUS_MODE_CHANGE &&
4776 status != GOT_STATUS_ADD &&
4777 status != GOT_STATUS_DELETE)
4778 return NULL;
4781 if (asprintf(&path, "/%s", relpath) == -1) {
4782 err = got_error_from_errno("asprintf");
4783 goto done;
4785 if (strcmp(path, "/") == 0) {
4786 parent_path = strdup("");
4787 if (parent_path == NULL)
4788 return got_error_from_errno("strdup");
4789 } else {
4790 err = got_path_dirname(&parent_path, path);
4791 if (err)
4792 return err;
4795 ct = calloc(1, sizeof(*ct));
4796 if (ct == NULL) {
4797 err = got_error_from_errno("calloc");
4798 goto done;
4801 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4802 relpath) == -1) {
4803 err = got_error_from_errno("asprintf");
4804 goto done;
4807 if (staged_status == GOT_STATUS_ADD ||
4808 staged_status == GOT_STATUS_MODIFY) {
4809 struct got_fileindex_entry *ie;
4810 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4811 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4812 case GOT_FILEIDX_MODE_REGULAR_FILE:
4813 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4814 ct->mode = S_IFREG;
4815 break;
4816 case GOT_FILEIDX_MODE_SYMLINK:
4817 ct->mode = S_IFLNK;
4818 break;
4819 default:
4820 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4821 goto done;
4823 ct->mode |= got_fileindex_entry_perms_get(ie);
4824 } else if (status != GOT_STATUS_DELETE &&
4825 staged_status != GOT_STATUS_DELETE) {
4826 if (dirfd != -1) {
4827 if (fstatat(dirfd, de_name, &sb,
4828 AT_SYMLINK_NOFOLLOW) == -1) {
4829 err = got_error_from_errno2("fstatat",
4830 ct->ondisk_path);
4831 goto done;
4833 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4834 err = got_error_from_errno2("lstat", ct->ondisk_path);
4835 goto done;
4837 ct->mode = sb.st_mode;
4840 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4841 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4842 relpath) == -1) {
4843 err = got_error_from_errno("asprintf");
4844 goto done;
4847 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4848 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4849 int is_bad_symlink;
4850 char target_path[PATH_MAX];
4851 ssize_t target_len;
4852 target_len = readlink(ct->ondisk_path, target_path,
4853 sizeof(target_path));
4854 if (target_len == -1) {
4855 err = got_error_from_errno2("readlink",
4856 ct->ondisk_path);
4857 goto done;
4859 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4860 target_len, ct->ondisk_path, a->worktree->root_path);
4861 if (err)
4862 goto done;
4863 if (is_bad_symlink) {
4864 err = got_error_path(ct->ondisk_path,
4865 GOT_ERR_BAD_SYMLINK);
4866 goto done;
4871 ct->status = status;
4872 ct->staged_status = staged_status;
4873 ct->blob_id = NULL; /* will be filled in when blob gets created */
4874 if (ct->status != GOT_STATUS_ADD &&
4875 ct->staged_status != GOT_STATUS_ADD) {
4876 ct->base_blob_id = got_object_id_dup(blob_id);
4877 if (ct->base_blob_id == NULL) {
4878 err = got_error_from_errno("got_object_id_dup");
4879 goto done;
4881 ct->base_commit_id = got_object_id_dup(commit_id);
4882 if (ct->base_commit_id == NULL) {
4883 err = got_error_from_errno("got_object_id_dup");
4884 goto done;
4887 if (ct->staged_status == GOT_STATUS_ADD ||
4888 ct->staged_status == GOT_STATUS_MODIFY) {
4889 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4890 if (ct->staged_blob_id == NULL) {
4891 err = got_error_from_errno("got_object_id_dup");
4892 goto done;
4895 ct->path = strdup(path);
4896 if (ct->path == NULL) {
4897 err = got_error_from_errno("strdup");
4898 goto done;
4900 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4901 done:
4902 if (ct && (err || new == NULL))
4903 free_commitable(ct);
4904 free(parent_path);
4905 free(path);
4906 return err;
4909 static const struct got_error *write_tree(struct got_object_id **, int *,
4910 struct got_tree_object *, const char *, struct got_pathlist_head *,
4911 got_worktree_status_cb status_cb, void *status_arg,
4912 struct got_repository *);
4914 static const struct got_error *
4915 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4916 struct got_tree_entry *te, const char *parent_path,
4917 struct got_pathlist_head *commitable_paths,
4918 got_worktree_status_cb status_cb, void *status_arg,
4919 struct got_repository *repo)
4921 const struct got_error *err = NULL;
4922 struct got_tree_object *subtree;
4923 char *subpath;
4925 if (asprintf(&subpath, "%s%s%s", parent_path,
4926 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4927 return got_error_from_errno("asprintf");
4929 err = got_object_open_as_tree(&subtree, repo, &te->id);
4930 if (err)
4931 return err;
4933 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4934 commitable_paths, status_cb, status_arg, repo);
4935 got_object_tree_close(subtree);
4936 free(subpath);
4937 return err;
4940 static const struct got_error *
4941 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4943 const struct got_error *err = NULL;
4944 char *ct_parent_path = NULL;
4946 *match = 0;
4948 if (strchr(ct->in_repo_path, '/') == NULL) {
4949 *match = got_path_is_root_dir(path);
4950 return NULL;
4953 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4954 if (err)
4955 return err;
4956 *match = (strcmp(path, ct_parent_path) == 0);
4957 free(ct_parent_path);
4958 return err;
4961 static mode_t
4962 get_ct_file_mode(struct got_commitable *ct)
4964 if (S_ISLNK(ct->mode))
4965 return S_IFLNK;
4967 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4970 static const struct got_error *
4971 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4972 struct got_tree_entry *te, struct got_commitable *ct)
4974 const struct got_error *err = NULL;
4976 *new_te = NULL;
4978 err = got_object_tree_entry_dup(new_te, te);
4979 if (err)
4980 goto done;
4982 (*new_te)->mode = get_ct_file_mode(ct);
4984 if (ct->staged_status == GOT_STATUS_MODIFY)
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 if (err && *new_te) {
4991 free(*new_te);
4992 *new_te = NULL;
4994 return err;
4997 static const struct got_error *
4998 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4999 struct got_commitable *ct)
5001 const struct got_error *err = NULL;
5002 char *ct_name = NULL;
5004 *new_te = NULL;
5006 *new_te = calloc(1, sizeof(**new_te));
5007 if (*new_te == NULL)
5008 return got_error_from_errno("calloc");
5010 err = got_path_basename(&ct_name, ct->path);
5011 if (err)
5012 goto done;
5013 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5014 sizeof((*new_te)->name)) {
5015 err = got_error(GOT_ERR_NO_SPACE);
5016 goto done;
5019 (*new_te)->mode = get_ct_file_mode(ct);
5021 if (ct->staged_status == GOT_STATUS_ADD)
5022 memcpy(&(*new_te)->id, ct->staged_blob_id,
5023 sizeof((*new_te)->id));
5024 else
5025 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5026 done:
5027 free(ct_name);
5028 if (err && *new_te) {
5029 free(*new_te);
5030 *new_te = NULL;
5032 return err;
5035 static const struct got_error *
5036 insert_tree_entry(struct got_tree_entry *new_te,
5037 struct got_pathlist_head *paths)
5039 const struct got_error *err = NULL;
5040 struct got_pathlist_entry *new_pe;
5042 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5043 if (err)
5044 return err;
5045 if (new_pe == NULL)
5046 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5047 return NULL;
5050 static const struct got_error *
5051 report_ct_status(struct got_commitable *ct,
5052 got_worktree_status_cb status_cb, void *status_arg)
5054 const char *ct_path = ct->path;
5055 unsigned char status;
5057 while (ct_path[0] == '/')
5058 ct_path++;
5060 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5061 status = ct->staged_status;
5062 else
5063 status = ct->status;
5065 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5066 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5069 static const struct got_error *
5070 match_modified_subtree(int *modified, struct got_tree_entry *te,
5071 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5073 const struct got_error *err = NULL;
5074 struct got_pathlist_entry *pe;
5075 char *te_path;
5077 *modified = 0;
5079 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5080 got_path_is_root_dir(base_tree_path) ? "" : "/",
5081 te->name) == -1)
5082 return got_error_from_errno("asprintf");
5084 TAILQ_FOREACH(pe, commitable_paths, entry) {
5085 struct got_commitable *ct = pe->data;
5086 *modified = got_path_is_child(ct->in_repo_path, te_path,
5087 strlen(te_path));
5088 if (*modified)
5089 break;
5092 free(te_path);
5093 return err;
5096 static const struct got_error *
5097 match_deleted_or_modified_ct(struct got_commitable **ctp,
5098 struct got_tree_entry *te, const char *base_tree_path,
5099 struct got_pathlist_head *commitable_paths)
5101 const struct got_error *err = NULL;
5102 struct got_pathlist_entry *pe;
5104 *ctp = NULL;
5106 TAILQ_FOREACH(pe, commitable_paths, entry) {
5107 struct got_commitable *ct = pe->data;
5108 char *ct_name = NULL;
5109 int path_matches;
5111 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5112 if (ct->status != GOT_STATUS_MODIFY &&
5113 ct->status != GOT_STATUS_MODE_CHANGE &&
5114 ct->status != GOT_STATUS_DELETE)
5115 continue;
5116 } else {
5117 if (ct->staged_status != GOT_STATUS_MODIFY &&
5118 ct->staged_status != GOT_STATUS_DELETE)
5119 continue;
5122 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5123 continue;
5125 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5126 if (err)
5127 return err;
5128 if (!path_matches)
5129 continue;
5131 err = got_path_basename(&ct_name, pe->path);
5132 if (err)
5133 return err;
5135 if (strcmp(te->name, ct_name) != 0) {
5136 free(ct_name);
5137 continue;
5139 free(ct_name);
5141 *ctp = ct;
5142 break;
5145 return err;
5148 static const struct got_error *
5149 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5150 const char *child_path, const char *path_base_tree,
5151 struct got_pathlist_head *commitable_paths,
5152 got_worktree_status_cb status_cb, void *status_arg,
5153 struct got_repository *repo)
5155 const struct got_error *err = NULL;
5156 struct got_tree_entry *new_te;
5157 char *subtree_path;
5158 struct got_object_id *id = NULL;
5159 int nentries;
5161 *new_tep = NULL;
5163 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5164 got_path_is_root_dir(path_base_tree) ? "" : "/",
5165 child_path) == -1)
5166 return got_error_from_errno("asprintf");
5168 new_te = calloc(1, sizeof(*new_te));
5169 if (new_te == NULL)
5170 return got_error_from_errno("calloc");
5171 new_te->mode = S_IFDIR;
5173 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5174 sizeof(new_te->name)) {
5175 err = got_error(GOT_ERR_NO_SPACE);
5176 goto done;
5178 err = write_tree(&id, &nentries, NULL, subtree_path,
5179 commitable_paths, status_cb, status_arg, repo);
5180 if (err) {
5181 free(new_te);
5182 goto done;
5184 memcpy(&new_te->id, id, sizeof(new_te->id));
5185 done:
5186 free(id);
5187 free(subtree_path);
5188 if (err == NULL)
5189 *new_tep = new_te;
5190 return err;
5193 static const struct got_error *
5194 write_tree(struct got_object_id **new_tree_id, int *nentries,
5195 struct got_tree_object *base_tree, const char *path_base_tree,
5196 struct got_pathlist_head *commitable_paths,
5197 got_worktree_status_cb status_cb, void *status_arg,
5198 struct got_repository *repo)
5200 const struct got_error *err = NULL;
5201 struct got_pathlist_head paths;
5202 struct got_tree_entry *te, *new_te = NULL;
5203 struct got_pathlist_entry *pe;
5205 TAILQ_INIT(&paths);
5206 *nentries = 0;
5208 /* Insert, and recurse into, newly added entries first. */
5209 TAILQ_FOREACH(pe, commitable_paths, entry) {
5210 struct got_commitable *ct = pe->data;
5211 char *child_path = NULL, *slash;
5213 if ((ct->status != GOT_STATUS_ADD &&
5214 ct->staged_status != GOT_STATUS_ADD) ||
5215 (ct->flags & GOT_COMMITABLE_ADDED))
5216 continue;
5218 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5219 strlen(path_base_tree)))
5220 continue;
5222 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5223 ct->in_repo_path);
5224 if (err)
5225 goto done;
5227 slash = strchr(child_path, '/');
5228 if (slash == NULL) {
5229 err = alloc_added_blob_tree_entry(&new_te, ct);
5230 if (err)
5231 goto done;
5232 err = report_ct_status(ct, status_cb, status_arg);
5233 if (err)
5234 goto done;
5235 ct->flags |= GOT_COMMITABLE_ADDED;
5236 err = insert_tree_entry(new_te, &paths);
5237 if (err)
5238 goto done;
5239 (*nentries)++;
5240 } else {
5241 *slash = '\0'; /* trim trailing path components */
5242 if (base_tree == NULL ||
5243 got_object_tree_find_entry(base_tree, child_path)
5244 == NULL) {
5245 err = make_subtree_for_added_blob(&new_te,
5246 child_path, path_base_tree,
5247 commitable_paths, status_cb, status_arg,
5248 repo);
5249 if (err)
5250 goto done;
5251 err = insert_tree_entry(new_te, &paths);
5252 if (err)
5253 goto done;
5254 (*nentries)++;
5259 if (base_tree) {
5260 int i, nbase_entries;
5261 /* Handle modified and deleted entries. */
5262 nbase_entries = got_object_tree_get_nentries(base_tree);
5263 for (i = 0; i < nbase_entries; i++) {
5264 struct got_commitable *ct = NULL;
5266 te = got_object_tree_get_entry(base_tree, i);
5267 if (got_object_tree_entry_is_submodule(te)) {
5268 /* Entry is a submodule; just copy it. */
5269 err = got_object_tree_entry_dup(&new_te, te);
5270 if (err)
5271 goto done;
5272 err = insert_tree_entry(new_te, &paths);
5273 if (err)
5274 goto done;
5275 (*nentries)++;
5276 continue;
5279 if (S_ISDIR(te->mode)) {
5280 int modified;
5281 err = got_object_tree_entry_dup(&new_te, te);
5282 if (err)
5283 goto done;
5284 err = match_modified_subtree(&modified, te,
5285 path_base_tree, commitable_paths);
5286 if (err)
5287 goto done;
5288 /* Avoid recursion into unmodified subtrees. */
5289 if (modified) {
5290 struct got_object_id *new_id;
5291 int nsubentries;
5292 err = write_subtree(&new_id,
5293 &nsubentries, te,
5294 path_base_tree, commitable_paths,
5295 status_cb, status_arg, repo);
5296 if (err)
5297 goto done;
5298 if (nsubentries == 0) {
5299 /* All entries were deleted. */
5300 free(new_id);
5301 continue;
5303 memcpy(&new_te->id, new_id,
5304 sizeof(new_te->id));
5305 free(new_id);
5307 err = insert_tree_entry(new_te, &paths);
5308 if (err)
5309 goto done;
5310 (*nentries)++;
5311 continue;
5314 err = match_deleted_or_modified_ct(&ct, te,
5315 path_base_tree, commitable_paths);
5316 if (err)
5317 goto done;
5318 if (ct) {
5319 /* NB: Deleted entries get dropped here. */
5320 if (ct->status == GOT_STATUS_MODIFY ||
5321 ct->status == GOT_STATUS_MODE_CHANGE ||
5322 ct->staged_status == GOT_STATUS_MODIFY) {
5323 err = alloc_modified_blob_tree_entry(
5324 &new_te, te, ct);
5325 if (err)
5326 goto done;
5327 err = insert_tree_entry(new_te, &paths);
5328 if (err)
5329 goto done;
5330 (*nentries)++;
5332 err = report_ct_status(ct, status_cb,
5333 status_arg);
5334 if (err)
5335 goto done;
5336 } else {
5337 /* Entry is unchanged; just copy it. */
5338 err = got_object_tree_entry_dup(&new_te, te);
5339 if (err)
5340 goto done;
5341 err = insert_tree_entry(new_te, &paths);
5342 if (err)
5343 goto done;
5344 (*nentries)++;
5349 /* Write new list of entries; deleted entries have been dropped. */
5350 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5351 done:
5352 got_pathlist_free(&paths);
5353 return err;
5356 static const struct got_error *
5357 update_fileindex_after_commit(struct got_worktree *worktree,
5358 struct got_pathlist_head *commitable_paths,
5359 struct got_object_id *new_base_commit_id,
5360 struct got_fileindex *fileindex, int have_staged_files)
5362 const struct got_error *err = NULL;
5363 struct got_pathlist_entry *pe;
5364 char *relpath = NULL;
5366 TAILQ_FOREACH(pe, commitable_paths, entry) {
5367 struct got_fileindex_entry *ie;
5368 struct got_commitable *ct = pe->data;
5370 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5372 err = got_path_skip_common_ancestor(&relpath,
5373 worktree->root_path, ct->ondisk_path);
5374 if (err)
5375 goto done;
5377 if (ie) {
5378 if (ct->status == GOT_STATUS_DELETE ||
5379 ct->staged_status == GOT_STATUS_DELETE) {
5380 got_fileindex_entry_remove(fileindex, ie);
5381 } else if (ct->staged_status == GOT_STATUS_ADD ||
5382 ct->staged_status == GOT_STATUS_MODIFY) {
5383 got_fileindex_entry_stage_set(ie,
5384 GOT_FILEIDX_STAGE_NONE);
5385 got_fileindex_entry_staged_filetype_set(ie, 0);
5387 err = got_fileindex_entry_update(ie,
5388 worktree->root_fd, relpath,
5389 ct->staged_blob_id->sha1,
5390 new_base_commit_id->sha1,
5391 !have_staged_files);
5392 } else
5393 err = got_fileindex_entry_update(ie,
5394 worktree->root_fd, relpath,
5395 ct->blob_id->sha1,
5396 new_base_commit_id->sha1,
5397 !have_staged_files);
5398 } else {
5399 err = got_fileindex_entry_alloc(&ie, pe->path);
5400 if (err)
5401 goto done;
5402 err = got_fileindex_entry_update(ie,
5403 worktree->root_fd, relpath, ct->blob_id->sha1,
5404 new_base_commit_id->sha1, 1);
5405 if (err) {
5406 got_fileindex_entry_free(ie);
5407 goto done;
5409 err = got_fileindex_entry_add(fileindex, ie);
5410 if (err) {
5411 got_fileindex_entry_free(ie);
5412 goto done;
5415 free(relpath);
5416 relpath = NULL;
5418 done:
5419 free(relpath);
5420 return err;
5424 static const struct got_error *
5425 check_out_of_date(const char *in_repo_path, unsigned char status,
5426 unsigned char staged_status, struct got_object_id *base_blob_id,
5427 struct got_object_id *base_commit_id,
5428 struct got_object_id *head_commit_id, struct got_repository *repo,
5429 int ood_errcode)
5431 const struct got_error *err = NULL;
5432 struct got_object_id *id = NULL;
5434 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5435 /* Trivial case: base commit == head commit */
5436 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5437 return NULL;
5439 * Ensure file content which local changes were based
5440 * on matches file content in the branch head.
5442 err = got_object_id_by_path(&id, repo, head_commit_id,
5443 in_repo_path);
5444 if (err) {
5445 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5446 err = got_error(ood_errcode);
5447 goto done;
5448 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5449 err = got_error(ood_errcode);
5450 } else {
5451 /* Require that added files don't exist in the branch head. */
5452 err = got_object_id_by_path(&id, repo, head_commit_id,
5453 in_repo_path);
5454 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5455 goto done;
5456 err = id ? got_error(ood_errcode) : NULL;
5458 done:
5459 free(id);
5460 return err;
5463 const struct got_error *
5464 commit_worktree(struct got_object_id **new_commit_id,
5465 struct got_pathlist_head *commitable_paths,
5466 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5467 const char *author, const char *committer,
5468 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5469 got_worktree_status_cb status_cb, void *status_arg,
5470 struct got_repository *repo)
5472 const struct got_error *err = NULL, *unlockerr = NULL;
5473 struct got_pathlist_entry *pe;
5474 const char *head_ref_name = NULL;
5475 struct got_commit_object *head_commit = NULL;
5476 struct got_reference *head_ref2 = NULL;
5477 struct got_object_id *head_commit_id2 = NULL;
5478 struct got_tree_object *head_tree = NULL;
5479 struct got_object_id *new_tree_id = NULL;
5480 int nentries;
5481 struct got_object_id_queue parent_ids;
5482 struct got_object_qid *pid = NULL;
5483 char *logmsg = NULL;
5485 *new_commit_id = NULL;
5487 SIMPLEQ_INIT(&parent_ids);
5489 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5490 if (err)
5491 goto done;
5493 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5494 if (err)
5495 goto done;
5497 if (commit_msg_cb != NULL) {
5498 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5499 if (err)
5500 goto done;
5503 if (logmsg == NULL || strlen(logmsg) == 0) {
5504 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5505 goto done;
5508 /* Create blobs from added and modified files and record their IDs. */
5509 TAILQ_FOREACH(pe, commitable_paths, entry) {
5510 struct got_commitable *ct = pe->data;
5511 char *ondisk_path;
5513 /* Blobs for staged files already exist. */
5514 if (ct->staged_status == GOT_STATUS_ADD ||
5515 ct->staged_status == GOT_STATUS_MODIFY)
5516 continue;
5518 if (ct->status != GOT_STATUS_ADD &&
5519 ct->status != GOT_STATUS_MODIFY &&
5520 ct->status != GOT_STATUS_MODE_CHANGE)
5521 continue;
5523 if (asprintf(&ondisk_path, "%s/%s",
5524 worktree->root_path, pe->path) == -1) {
5525 err = got_error_from_errno("asprintf");
5526 goto done;
5528 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5529 free(ondisk_path);
5530 if (err)
5531 goto done;
5534 /* Recursively write new tree objects. */
5535 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5536 commitable_paths, status_cb, status_arg, repo);
5537 if (err)
5538 goto done;
5540 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5541 if (err)
5542 goto done;
5543 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5544 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5545 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5546 got_object_qid_free(pid);
5547 if (logmsg != NULL)
5548 free(logmsg);
5549 if (err)
5550 goto done;
5552 /* Check if a concurrent commit to our branch has occurred. */
5553 head_ref_name = got_worktree_get_head_ref_name(worktree);
5554 if (head_ref_name == NULL) {
5555 err = got_error_from_errno("got_worktree_get_head_ref_name");
5556 goto done;
5558 /* Lock the reference here to prevent concurrent modification. */
5559 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5560 if (err)
5561 goto done;
5562 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5563 if (err)
5564 goto done;
5565 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5566 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5567 goto done;
5569 /* Update branch head in repository. */
5570 err = got_ref_change_ref(head_ref2, *new_commit_id);
5571 if (err)
5572 goto done;
5573 err = got_ref_write(head_ref2, repo);
5574 if (err)
5575 goto done;
5577 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5578 if (err)
5579 goto done;
5581 err = ref_base_commit(worktree, repo);
5582 if (err)
5583 goto done;
5584 done:
5585 if (head_tree)
5586 got_object_tree_close(head_tree);
5587 if (head_commit)
5588 got_object_commit_close(head_commit);
5589 free(head_commit_id2);
5590 if (head_ref2) {
5591 unlockerr = got_ref_unlock(head_ref2);
5592 if (unlockerr && err == NULL)
5593 err = unlockerr;
5594 got_ref_close(head_ref2);
5596 return err;
5599 static const struct got_error *
5600 check_path_is_commitable(const char *path,
5601 struct got_pathlist_head *commitable_paths)
5603 struct got_pathlist_entry *cpe = NULL;
5604 size_t path_len = strlen(path);
5606 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5607 struct got_commitable *ct = cpe->data;
5608 const char *ct_path = ct->path;
5610 while (ct_path[0] == '/')
5611 ct_path++;
5613 if (strcmp(path, ct_path) == 0 ||
5614 got_path_is_child(ct_path, path, path_len))
5615 break;
5618 if (cpe == NULL)
5619 return got_error_path(path, GOT_ERR_BAD_PATH);
5621 return NULL;
5624 static const struct got_error *
5625 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5627 int *have_staged_files = arg;
5629 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5630 *have_staged_files = 1;
5631 return got_error(GOT_ERR_CANCELLED);
5634 return NULL;
5637 static const struct got_error *
5638 check_non_staged_files(struct got_fileindex *fileindex,
5639 struct got_pathlist_head *paths)
5641 struct got_pathlist_entry *pe;
5642 struct got_fileindex_entry *ie;
5644 TAILQ_FOREACH(pe, paths, entry) {
5645 if (pe->path[0] == '\0')
5646 continue;
5647 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5648 if (ie == NULL)
5649 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5650 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5651 return got_error_path(pe->path,
5652 GOT_ERR_FILE_NOT_STAGED);
5655 return NULL;
5658 const struct got_error *
5659 got_worktree_commit(struct got_object_id **new_commit_id,
5660 struct got_worktree *worktree, struct got_pathlist_head *paths,
5661 const char *author, const char *committer, int allow_bad_symlinks,
5662 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5663 got_worktree_status_cb status_cb, void *status_arg,
5664 struct got_repository *repo)
5666 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5667 struct got_fileindex *fileindex = NULL;
5668 char *fileindex_path = NULL;
5669 struct got_pathlist_head commitable_paths;
5670 struct collect_commitables_arg cc_arg;
5671 struct got_pathlist_entry *pe;
5672 struct got_reference *head_ref = NULL;
5673 struct got_object_id *head_commit_id = NULL;
5674 int have_staged_files = 0;
5676 *new_commit_id = NULL;
5678 TAILQ_INIT(&commitable_paths);
5680 err = lock_worktree(worktree, LOCK_EX);
5681 if (err)
5682 goto done;
5684 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5685 if (err)
5686 goto done;
5688 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5689 if (err)
5690 goto done;
5692 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5693 if (err)
5694 goto done;
5696 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5697 &have_staged_files);
5698 if (err && err->code != GOT_ERR_CANCELLED)
5699 goto done;
5700 if (have_staged_files) {
5701 err = check_non_staged_files(fileindex, paths);
5702 if (err)
5703 goto done;
5706 cc_arg.commitable_paths = &commitable_paths;
5707 cc_arg.worktree = worktree;
5708 cc_arg.fileindex = fileindex;
5709 cc_arg.repo = repo;
5710 cc_arg.have_staged_files = have_staged_files;
5711 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5712 TAILQ_FOREACH(pe, paths, entry) {
5713 err = worktree_status(worktree, pe->path, fileindex, repo,
5714 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5715 if (err)
5716 goto done;
5719 if (TAILQ_EMPTY(&commitable_paths)) {
5720 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5721 goto done;
5724 TAILQ_FOREACH(pe, paths, entry) {
5725 err = check_path_is_commitable(pe->path, &commitable_paths);
5726 if (err)
5727 goto done;
5730 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5731 struct got_commitable *ct = pe->data;
5732 const char *ct_path = ct->in_repo_path;
5734 while (ct_path[0] == '/')
5735 ct_path++;
5736 err = check_out_of_date(ct_path, ct->status,
5737 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5738 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5739 if (err)
5740 goto done;
5744 err = commit_worktree(new_commit_id, &commitable_paths,
5745 head_commit_id, worktree, author, committer,
5746 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5747 if (err)
5748 goto done;
5750 err = update_fileindex_after_commit(worktree, &commitable_paths,
5751 *new_commit_id, fileindex, have_staged_files);
5752 sync_err = sync_fileindex(fileindex, fileindex_path);
5753 if (sync_err && err == NULL)
5754 err = sync_err;
5755 done:
5756 if (fileindex)
5757 got_fileindex_free(fileindex);
5758 free(fileindex_path);
5759 unlockerr = lock_worktree(worktree, LOCK_SH);
5760 if (unlockerr && err == NULL)
5761 err = unlockerr;
5762 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5763 struct got_commitable *ct = pe->data;
5764 free_commitable(ct);
5766 got_pathlist_free(&commitable_paths);
5767 return err;
5770 const char *
5771 got_commitable_get_path(struct got_commitable *ct)
5773 return ct->path;
5776 unsigned int
5777 got_commitable_get_status(struct got_commitable *ct)
5779 return ct->status;
5782 struct check_rebase_ok_arg {
5783 struct got_worktree *worktree;
5784 struct got_repository *repo;
5787 static const struct got_error *
5788 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5790 const struct got_error *err = NULL;
5791 struct check_rebase_ok_arg *a = arg;
5792 unsigned char status;
5793 struct stat sb;
5794 char *ondisk_path;
5796 /* Reject rebase of a work tree with mixed base commits. */
5797 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5798 SHA1_DIGEST_LENGTH))
5799 return got_error(GOT_ERR_MIXED_COMMITS);
5801 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5802 == -1)
5803 return got_error_from_errno("asprintf");
5805 /* Reject rebase of a work tree with modified or staged files. */
5806 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5807 free(ondisk_path);
5808 if (err)
5809 return err;
5811 if (status != GOT_STATUS_NO_CHANGE)
5812 return got_error(GOT_ERR_MODIFIED);
5813 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5814 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5816 return NULL;
5819 const struct got_error *
5820 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5821 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5822 struct got_worktree *worktree, struct got_reference *branch,
5823 struct got_repository *repo)
5825 const struct got_error *err = NULL;
5826 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5827 char *branch_ref_name = NULL;
5828 char *fileindex_path = NULL;
5829 struct check_rebase_ok_arg ok_arg;
5830 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5831 struct got_object_id *wt_branch_tip = NULL;
5833 *new_base_branch_ref = NULL;
5834 *tmp_branch = NULL;
5835 *fileindex = NULL;
5837 err = lock_worktree(worktree, LOCK_EX);
5838 if (err)
5839 return err;
5841 err = open_fileindex(fileindex, &fileindex_path, worktree);
5842 if (err)
5843 goto done;
5845 ok_arg.worktree = worktree;
5846 ok_arg.repo = repo;
5847 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5848 &ok_arg);
5849 if (err)
5850 goto done;
5852 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5853 if (err)
5854 goto done;
5856 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5857 if (err)
5858 goto done;
5860 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5861 if (err)
5862 goto done;
5864 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5865 0);
5866 if (err)
5867 goto done;
5869 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5870 if (err)
5871 goto done;
5872 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5873 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5874 goto done;
5877 err = got_ref_alloc_symref(new_base_branch_ref,
5878 new_base_branch_ref_name, wt_branch);
5879 if (err)
5880 goto done;
5881 err = got_ref_write(*new_base_branch_ref, repo);
5882 if (err)
5883 goto done;
5885 /* TODO Lock original branch's ref while rebasing? */
5887 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5888 if (err)
5889 goto done;
5891 err = got_ref_write(branch_ref, repo);
5892 if (err)
5893 goto done;
5895 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5896 worktree->base_commit_id);
5897 if (err)
5898 goto done;
5899 err = got_ref_write(*tmp_branch, repo);
5900 if (err)
5901 goto done;
5903 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5904 if (err)
5905 goto done;
5906 done:
5907 free(fileindex_path);
5908 free(tmp_branch_name);
5909 free(new_base_branch_ref_name);
5910 free(branch_ref_name);
5911 if (branch_ref)
5912 got_ref_close(branch_ref);
5913 if (wt_branch)
5914 got_ref_close(wt_branch);
5915 free(wt_branch_tip);
5916 if (err) {
5917 if (*new_base_branch_ref) {
5918 got_ref_close(*new_base_branch_ref);
5919 *new_base_branch_ref = NULL;
5921 if (*tmp_branch) {
5922 got_ref_close(*tmp_branch);
5923 *tmp_branch = NULL;
5925 if (*fileindex) {
5926 got_fileindex_free(*fileindex);
5927 *fileindex = NULL;
5929 lock_worktree(worktree, LOCK_SH);
5931 return err;
5934 const struct got_error *
5935 got_worktree_rebase_continue(struct got_object_id **commit_id,
5936 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5937 struct got_reference **branch, struct got_fileindex **fileindex,
5938 struct got_worktree *worktree, struct got_repository *repo)
5940 const struct got_error *err;
5941 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5942 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5943 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5944 char *fileindex_path = NULL;
5945 int have_staged_files = 0;
5947 *commit_id = NULL;
5948 *new_base_branch = NULL;
5949 *tmp_branch = NULL;
5950 *branch = NULL;
5951 *fileindex = NULL;
5953 err = lock_worktree(worktree, LOCK_EX);
5954 if (err)
5955 return err;
5957 err = open_fileindex(fileindex, &fileindex_path, worktree);
5958 if (err)
5959 goto done;
5961 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5962 &have_staged_files);
5963 if (err && err->code != GOT_ERR_CANCELLED)
5964 goto done;
5965 if (have_staged_files) {
5966 err = got_error(GOT_ERR_STAGED_PATHS);
5967 goto done;
5970 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5971 if (err)
5972 goto done;
5974 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5975 if (err)
5976 goto done;
5978 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5979 if (err)
5980 goto done;
5982 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5983 if (err)
5984 goto done;
5986 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5987 if (err)
5988 goto done;
5990 err = got_ref_open(branch, repo,
5991 got_ref_get_symref_target(branch_ref), 0);
5992 if (err)
5993 goto done;
5995 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5996 if (err)
5997 goto done;
5999 err = got_ref_resolve(commit_id, repo, commit_ref);
6000 if (err)
6001 goto done;
6003 err = got_ref_open(new_base_branch, repo,
6004 new_base_branch_ref_name, 0);
6005 if (err)
6006 goto done;
6008 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6009 if (err)
6010 goto done;
6011 done:
6012 free(commit_ref_name);
6013 free(branch_ref_name);
6014 free(fileindex_path);
6015 if (commit_ref)
6016 got_ref_close(commit_ref);
6017 if (branch_ref)
6018 got_ref_close(branch_ref);
6019 if (err) {
6020 free(*commit_id);
6021 *commit_id = NULL;
6022 if (*tmp_branch) {
6023 got_ref_close(*tmp_branch);
6024 *tmp_branch = NULL;
6026 if (*new_base_branch) {
6027 got_ref_close(*new_base_branch);
6028 *new_base_branch = NULL;
6030 if (*branch) {
6031 got_ref_close(*branch);
6032 *branch = NULL;
6034 if (*fileindex) {
6035 got_fileindex_free(*fileindex);
6036 *fileindex = NULL;
6038 lock_worktree(worktree, LOCK_SH);
6040 return err;
6043 const struct got_error *
6044 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6046 const struct got_error *err;
6047 char *tmp_branch_name = NULL;
6049 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6050 if (err)
6051 return err;
6053 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6054 free(tmp_branch_name);
6055 return NULL;
6058 static const struct got_error *
6059 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6060 char **logmsg, void *arg)
6062 *logmsg = arg;
6063 return NULL;
6066 static const struct got_error *
6067 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6068 const char *path, struct got_object_id *blob_id,
6069 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6070 int dirfd, const char *de_name)
6072 return NULL;
6075 struct collect_merged_paths_arg {
6076 got_worktree_checkout_cb progress_cb;
6077 void *progress_arg;
6078 struct got_pathlist_head *merged_paths;
6081 static const struct got_error *
6082 collect_merged_paths(void *arg, unsigned char status, const char *path)
6084 const struct got_error *err;
6085 struct collect_merged_paths_arg *a = arg;
6086 char *p;
6087 struct got_pathlist_entry *new;
6089 err = (*a->progress_cb)(a->progress_arg, status, path);
6090 if (err)
6091 return err;
6093 if (status != GOT_STATUS_MERGE &&
6094 status != GOT_STATUS_ADD &&
6095 status != GOT_STATUS_DELETE &&
6096 status != GOT_STATUS_CONFLICT)
6097 return NULL;
6099 p = strdup(path);
6100 if (p == NULL)
6101 return got_error_from_errno("strdup");
6103 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6104 if (err || new == NULL)
6105 free(p);
6106 return err;
6109 void
6110 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6112 struct got_pathlist_entry *pe;
6114 TAILQ_FOREACH(pe, merged_paths, entry)
6115 free((char *)pe->path);
6117 got_pathlist_free(merged_paths);
6120 static const struct got_error *
6121 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6122 int is_rebase, struct got_repository *repo)
6124 const struct got_error *err;
6125 struct got_reference *commit_ref = NULL;
6127 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6128 if (err) {
6129 if (err->code != GOT_ERR_NOT_REF)
6130 goto done;
6131 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6132 if (err)
6133 goto done;
6134 err = got_ref_write(commit_ref, repo);
6135 if (err)
6136 goto done;
6137 } else if (is_rebase) {
6138 struct got_object_id *stored_id;
6139 int cmp;
6141 err = got_ref_resolve(&stored_id, repo, commit_ref);
6142 if (err)
6143 goto done;
6144 cmp = got_object_id_cmp(commit_id, stored_id);
6145 free(stored_id);
6146 if (cmp != 0) {
6147 err = got_error(GOT_ERR_REBASE_COMMITID);
6148 goto done;
6151 done:
6152 if (commit_ref)
6153 got_ref_close(commit_ref);
6154 return err;
6157 static const struct got_error *
6158 rebase_merge_files(struct got_pathlist_head *merged_paths,
6159 const char *commit_ref_name, struct got_worktree *worktree,
6160 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6161 struct got_object_id *commit_id, struct got_repository *repo,
6162 got_worktree_checkout_cb progress_cb, void *progress_arg,
6163 got_cancel_cb cancel_cb, void *cancel_arg)
6165 const struct got_error *err;
6166 struct got_reference *commit_ref = NULL;
6167 struct collect_merged_paths_arg cmp_arg;
6168 char *fileindex_path;
6170 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6172 err = get_fileindex_path(&fileindex_path, worktree);
6173 if (err)
6174 return err;
6176 cmp_arg.progress_cb = progress_cb;
6177 cmp_arg.progress_arg = progress_arg;
6178 cmp_arg.merged_paths = merged_paths;
6179 err = merge_files(worktree, fileindex, fileindex_path,
6180 parent_commit_id, commit_id, repo, collect_merged_paths,
6181 &cmp_arg, cancel_cb, cancel_arg);
6182 if (commit_ref)
6183 got_ref_close(commit_ref);
6184 return err;
6187 const struct got_error *
6188 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6189 struct got_worktree *worktree, struct got_fileindex *fileindex,
6190 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6191 struct got_repository *repo,
6192 got_worktree_checkout_cb progress_cb, void *progress_arg,
6193 got_cancel_cb cancel_cb, void *cancel_arg)
6195 const struct got_error *err;
6196 char *commit_ref_name;
6198 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6199 if (err)
6200 return err;
6202 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6203 if (err)
6204 goto done;
6206 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6207 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6208 progress_arg, cancel_cb, cancel_arg);
6209 done:
6210 free(commit_ref_name);
6211 return err;
6214 const struct got_error *
6215 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6216 struct got_worktree *worktree, struct got_fileindex *fileindex,
6217 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6218 struct got_repository *repo,
6219 got_worktree_checkout_cb progress_cb, void *progress_arg,
6220 got_cancel_cb cancel_cb, void *cancel_arg)
6222 const struct got_error *err;
6223 char *commit_ref_name;
6225 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6226 if (err)
6227 return err;
6229 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6230 if (err)
6231 goto done;
6233 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6234 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6235 progress_arg, cancel_cb, cancel_arg);
6236 done:
6237 free(commit_ref_name);
6238 return err;
6241 static const struct got_error *
6242 rebase_commit(struct got_object_id **new_commit_id,
6243 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6244 struct got_worktree *worktree, struct got_fileindex *fileindex,
6245 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6246 const char *new_logmsg, struct got_repository *repo)
6248 const struct got_error *err, *sync_err;
6249 struct got_pathlist_head commitable_paths;
6250 struct collect_commitables_arg cc_arg;
6251 char *fileindex_path = NULL;
6252 struct got_reference *head_ref = NULL;
6253 struct got_object_id *head_commit_id = NULL;
6254 char *logmsg = NULL;
6256 TAILQ_INIT(&commitable_paths);
6257 *new_commit_id = NULL;
6259 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6261 err = get_fileindex_path(&fileindex_path, worktree);
6262 if (err)
6263 return err;
6265 cc_arg.commitable_paths = &commitable_paths;
6266 cc_arg.worktree = worktree;
6267 cc_arg.repo = repo;
6268 cc_arg.have_staged_files = 0;
6270 * If possible get the status of individual files directly to
6271 * avoid crawling the entire work tree once per rebased commit.
6272 * TODO: Ideally, merged_paths would contain a list of commitables
6273 * we could use so we could skip worktree_status() entirely.
6275 if (merged_paths) {
6276 struct got_pathlist_entry *pe;
6277 TAILQ_FOREACH(pe, merged_paths, entry) {
6278 err = worktree_status(worktree, pe->path, fileindex,
6279 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6280 0);
6281 if (err)
6282 goto done;
6284 } else {
6285 err = worktree_status(worktree, "", fileindex, repo,
6286 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6287 if (err)
6288 goto done;
6291 if (TAILQ_EMPTY(&commitable_paths)) {
6292 /* No-op change; commit will be elided. */
6293 err = got_ref_delete(commit_ref, repo);
6294 if (err)
6295 goto done;
6296 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6297 goto done;
6300 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6301 if (err)
6302 goto done;
6304 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6305 if (err)
6306 goto done;
6308 if (new_logmsg) {
6309 logmsg = strdup(new_logmsg);
6310 if (logmsg == NULL) {
6311 err = got_error_from_errno("strdup");
6312 goto done;
6314 } else {
6315 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6316 if (err)
6317 goto done;
6320 /* NB: commit_worktree will call free(logmsg) */
6321 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6322 worktree, got_object_commit_get_author(orig_commit),
6323 got_object_commit_get_committer(orig_commit),
6324 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6325 if (err)
6326 goto done;
6328 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6329 if (err)
6330 goto done;
6332 err = got_ref_delete(commit_ref, repo);
6333 if (err)
6334 goto done;
6336 err = update_fileindex_after_commit(worktree, &commitable_paths,
6337 *new_commit_id, fileindex, 0);
6338 sync_err = sync_fileindex(fileindex, fileindex_path);
6339 if (sync_err && err == NULL)
6340 err = sync_err;
6341 done:
6342 free(fileindex_path);
6343 free(head_commit_id);
6344 if (head_ref)
6345 got_ref_close(head_ref);
6346 if (err) {
6347 free(*new_commit_id);
6348 *new_commit_id = NULL;
6350 return err;
6353 const struct got_error *
6354 got_worktree_rebase_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, struct got_repository *repo)
6360 const struct got_error *err;
6361 char *commit_ref_name;
6362 struct got_reference *commit_ref = NULL;
6363 struct got_object_id *commit_id = NULL;
6365 err = get_rebase_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;
6372 err = got_ref_resolve(&commit_id, repo, commit_ref);
6373 if (err)
6374 goto done;
6375 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6376 err = got_error(GOT_ERR_REBASE_COMMITID);
6377 goto done;
6380 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6381 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6382 done:
6383 if (commit_ref)
6384 got_ref_close(commit_ref);
6385 free(commit_ref_name);
6386 free(commit_id);
6387 return err;
6390 const struct got_error *
6391 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6392 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6393 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6394 struct got_commit_object *orig_commit,
6395 struct got_object_id *orig_commit_id, const char *new_logmsg,
6396 struct got_repository *repo)
6398 const struct got_error *err;
6399 char *commit_ref_name;
6400 struct got_reference *commit_ref = NULL;
6402 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6403 if (err)
6404 return err;
6406 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6407 if (err)
6408 goto done;
6410 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6411 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6412 done:
6413 if (commit_ref)
6414 got_ref_close(commit_ref);
6415 free(commit_ref_name);
6416 return err;
6419 const struct got_error *
6420 got_worktree_rebase_postpone(struct got_worktree *worktree,
6421 struct got_fileindex *fileindex)
6423 if (fileindex)
6424 got_fileindex_free(fileindex);
6425 return lock_worktree(worktree, LOCK_SH);
6428 static const struct got_error *
6429 delete_ref(const char *name, struct got_repository *repo)
6431 const struct got_error *err;
6432 struct got_reference *ref;
6434 err = got_ref_open(&ref, repo, name, 0);
6435 if (err) {
6436 if (err->code == GOT_ERR_NOT_REF)
6437 return NULL;
6438 return err;
6441 err = got_ref_delete(ref, repo);
6442 got_ref_close(ref);
6443 return err;
6446 static const struct got_error *
6447 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6449 const struct got_error *err;
6450 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6451 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6453 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6454 if (err)
6455 goto done;
6456 err = delete_ref(tmp_branch_name, repo);
6457 if (err)
6458 goto done;
6460 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6461 if (err)
6462 goto done;
6463 err = delete_ref(new_base_branch_ref_name, repo);
6464 if (err)
6465 goto done;
6467 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6468 if (err)
6469 goto done;
6470 err = delete_ref(branch_ref_name, repo);
6471 if (err)
6472 goto done;
6474 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6475 if (err)
6476 goto done;
6477 err = delete_ref(commit_ref_name, repo);
6478 if (err)
6479 goto done;
6481 done:
6482 free(tmp_branch_name);
6483 free(new_base_branch_ref_name);
6484 free(branch_ref_name);
6485 free(commit_ref_name);
6486 return err;
6489 const struct got_error *
6490 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6491 struct got_object_id *new_commit_id, struct got_repository *repo)
6493 const struct got_error *err;
6494 struct got_reference *ref = NULL;
6495 struct got_object_id *old_commit_id = NULL;
6496 const char *branch_name = NULL;
6497 char *new_id_str = NULL;
6498 char *refname = NULL;
6500 branch_name = got_ref_get_name(branch);
6501 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6502 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6503 branch_name += 11;
6505 err = got_object_id_str(&new_id_str, new_commit_id);
6506 if (err)
6507 return err;
6509 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6510 new_id_str) == -1) {
6511 err = got_error_from_errno("asprintf");
6512 goto done;
6515 err = got_ref_resolve(&old_commit_id, repo, branch);
6516 if (err)
6517 goto done;
6519 err = got_ref_alloc(&ref, refname, old_commit_id);
6520 if (err)
6521 goto done;
6523 err = got_ref_write(ref, repo);
6524 done:
6525 free(new_id_str);
6526 free(refname);
6527 free(old_commit_id);
6528 if (ref)
6529 got_ref_close(ref);
6530 return err;
6533 const struct got_error *
6534 got_worktree_rebase_complete(struct got_worktree *worktree,
6535 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6536 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6537 struct got_repository *repo, int create_backup)
6539 const struct got_error *err, *unlockerr, *sync_err;
6540 struct got_object_id *new_head_commit_id = NULL;
6541 char *fileindex_path = NULL;
6543 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6544 if (err)
6545 return err;
6547 if (create_backup) {
6548 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6549 rebased_branch, new_head_commit_id, repo);
6550 if (err)
6551 goto done;
6554 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6555 if (err)
6556 goto done;
6558 err = got_ref_write(rebased_branch, repo);
6559 if (err)
6560 goto done;
6562 err = got_worktree_set_head_ref(worktree, rebased_branch);
6563 if (err)
6564 goto done;
6566 err = delete_rebase_refs(worktree, repo);
6567 if (err)
6568 goto done;
6570 err = get_fileindex_path(&fileindex_path, worktree);
6571 if (err)
6572 goto done;
6573 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6574 sync_err = sync_fileindex(fileindex, fileindex_path);
6575 if (sync_err && err == NULL)
6576 err = sync_err;
6577 done:
6578 got_fileindex_free(fileindex);
6579 free(fileindex_path);
6580 free(new_head_commit_id);
6581 unlockerr = lock_worktree(worktree, LOCK_SH);
6582 if (unlockerr && err == NULL)
6583 err = unlockerr;
6584 return err;
6587 const struct got_error *
6588 got_worktree_rebase_abort(struct got_worktree *worktree,
6589 struct got_fileindex *fileindex, struct got_repository *repo,
6590 struct got_reference *new_base_branch,
6591 got_worktree_checkout_cb progress_cb, void *progress_arg)
6593 const struct got_error *err, *unlockerr, *sync_err;
6594 struct got_reference *resolved = NULL;
6595 struct got_object_id *commit_id = NULL;
6596 char *fileindex_path = NULL;
6597 struct revert_file_args rfa;
6598 struct got_object_id *tree_id = NULL;
6600 err = lock_worktree(worktree, LOCK_EX);
6601 if (err)
6602 return err;
6604 err = got_ref_open(&resolved, repo,
6605 got_ref_get_symref_target(new_base_branch), 0);
6606 if (err)
6607 goto done;
6609 err = got_worktree_set_head_ref(worktree, resolved);
6610 if (err)
6611 goto done;
6614 * XXX commits to the base branch could have happened while
6615 * we were busy rebasing; should we store the original commit ID
6616 * when rebase begins and read it back here?
6618 err = got_ref_resolve(&commit_id, repo, resolved);
6619 if (err)
6620 goto done;
6622 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6623 if (err)
6624 goto done;
6626 err = got_object_id_by_path(&tree_id, repo,
6627 worktree->base_commit_id, worktree->path_prefix);
6628 if (err)
6629 goto done;
6631 err = delete_rebase_refs(worktree, repo);
6632 if (err)
6633 goto done;
6635 err = get_fileindex_path(&fileindex_path, worktree);
6636 if (err)
6637 goto done;
6639 rfa.worktree = worktree;
6640 rfa.fileindex = fileindex;
6641 rfa.progress_cb = progress_cb;
6642 rfa.progress_arg = progress_arg;
6643 rfa.patch_cb = NULL;
6644 rfa.patch_arg = NULL;
6645 rfa.repo = repo;
6646 err = worktree_status(worktree, "", fileindex, repo,
6647 revert_file, &rfa, NULL, NULL, 0, 0);
6648 if (err)
6649 goto sync;
6651 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6652 repo, progress_cb, progress_arg, NULL, NULL);
6653 sync:
6654 sync_err = sync_fileindex(fileindex, fileindex_path);
6655 if (sync_err && err == NULL)
6656 err = sync_err;
6657 done:
6658 got_ref_close(resolved);
6659 free(tree_id);
6660 free(commit_id);
6661 if (fileindex)
6662 got_fileindex_free(fileindex);
6663 free(fileindex_path);
6665 unlockerr = lock_worktree(worktree, LOCK_SH);
6666 if (unlockerr && err == NULL)
6667 err = unlockerr;
6668 return err;
6671 const struct got_error *
6672 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6673 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6674 struct got_fileindex **fileindex, struct got_worktree *worktree,
6675 struct got_repository *repo)
6677 const struct got_error *err = NULL;
6678 char *tmp_branch_name = NULL;
6679 char *branch_ref_name = NULL;
6680 char *base_commit_ref_name = NULL;
6681 char *fileindex_path = NULL;
6682 struct check_rebase_ok_arg ok_arg;
6683 struct got_reference *wt_branch = NULL;
6684 struct got_reference *base_commit_ref = NULL;
6686 *tmp_branch = NULL;
6687 *branch_ref = NULL;
6688 *base_commit_id = NULL;
6689 *fileindex = NULL;
6691 err = lock_worktree(worktree, LOCK_EX);
6692 if (err)
6693 return err;
6695 err = open_fileindex(fileindex, &fileindex_path, worktree);
6696 if (err)
6697 goto done;
6699 ok_arg.worktree = worktree;
6700 ok_arg.repo = repo;
6701 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6702 &ok_arg);
6703 if (err)
6704 goto done;
6706 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6707 if (err)
6708 goto done;
6710 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6711 if (err)
6712 goto done;
6714 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6715 worktree);
6716 if (err)
6717 goto done;
6719 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6720 0);
6721 if (err)
6722 goto done;
6724 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6725 if (err)
6726 goto done;
6728 err = got_ref_write(*branch_ref, repo);
6729 if (err)
6730 goto done;
6732 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6733 worktree->base_commit_id);
6734 if (err)
6735 goto done;
6736 err = got_ref_write(base_commit_ref, repo);
6737 if (err)
6738 goto done;
6739 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6740 if (*base_commit_id == NULL) {
6741 err = got_error_from_errno("got_object_id_dup");
6742 goto done;
6745 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6746 worktree->base_commit_id);
6747 if (err)
6748 goto done;
6749 err = got_ref_write(*tmp_branch, repo);
6750 if (err)
6751 goto done;
6753 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6754 if (err)
6755 goto done;
6756 done:
6757 free(fileindex_path);
6758 free(tmp_branch_name);
6759 free(branch_ref_name);
6760 free(base_commit_ref_name);
6761 if (wt_branch)
6762 got_ref_close(wt_branch);
6763 if (err) {
6764 if (*branch_ref) {
6765 got_ref_close(*branch_ref);
6766 *branch_ref = NULL;
6768 if (*tmp_branch) {
6769 got_ref_close(*tmp_branch);
6770 *tmp_branch = NULL;
6772 free(*base_commit_id);
6773 if (*fileindex) {
6774 got_fileindex_free(*fileindex);
6775 *fileindex = NULL;
6777 lock_worktree(worktree, LOCK_SH);
6779 return err;
6782 const struct got_error *
6783 got_worktree_histedit_postpone(struct got_worktree *worktree,
6784 struct got_fileindex *fileindex)
6786 if (fileindex)
6787 got_fileindex_free(fileindex);
6788 return lock_worktree(worktree, LOCK_SH);
6791 const struct got_error *
6792 got_worktree_histedit_in_progress(int *in_progress,
6793 struct got_worktree *worktree)
6795 const struct got_error *err;
6796 char *tmp_branch_name = NULL;
6798 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6799 if (err)
6800 return err;
6802 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6803 free(tmp_branch_name);
6804 return NULL;
6807 const struct got_error *
6808 got_worktree_histedit_continue(struct got_object_id **commit_id,
6809 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6810 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6811 struct got_worktree *worktree, struct got_repository *repo)
6813 const struct got_error *err;
6814 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6815 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6816 struct got_reference *commit_ref = NULL;
6817 struct got_reference *base_commit_ref = NULL;
6818 char *fileindex_path = NULL;
6819 int have_staged_files = 0;
6821 *commit_id = NULL;
6822 *tmp_branch = NULL;
6823 *base_commit_id = NULL;
6824 *fileindex = NULL;
6826 err = lock_worktree(worktree, LOCK_EX);
6827 if (err)
6828 return err;
6830 err = open_fileindex(fileindex, &fileindex_path, worktree);
6831 if (err)
6832 goto done;
6834 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6835 &have_staged_files);
6836 if (err && err->code != GOT_ERR_CANCELLED)
6837 goto done;
6838 if (have_staged_files) {
6839 err = got_error(GOT_ERR_STAGED_PATHS);
6840 goto done;
6843 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6844 if (err)
6845 goto done;
6847 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6848 if (err)
6849 goto done;
6851 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6852 if (err)
6853 goto done;
6855 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6856 worktree);
6857 if (err)
6858 goto done;
6860 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6861 if (err)
6862 goto done;
6864 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6865 if (err)
6866 goto done;
6867 err = got_ref_resolve(commit_id, repo, commit_ref);
6868 if (err)
6869 goto done;
6871 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6872 if (err)
6873 goto done;
6874 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6875 if (err)
6876 goto done;
6878 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6879 if (err)
6880 goto done;
6881 done:
6882 free(commit_ref_name);
6883 free(branch_ref_name);
6884 free(fileindex_path);
6885 if (commit_ref)
6886 got_ref_close(commit_ref);
6887 if (base_commit_ref)
6888 got_ref_close(base_commit_ref);
6889 if (err) {
6890 free(*commit_id);
6891 *commit_id = NULL;
6892 free(*base_commit_id);
6893 *base_commit_id = NULL;
6894 if (*tmp_branch) {
6895 got_ref_close(*tmp_branch);
6896 *tmp_branch = NULL;
6898 if (*fileindex) {
6899 got_fileindex_free(*fileindex);
6900 *fileindex = NULL;
6902 lock_worktree(worktree, LOCK_EX);
6904 return err;
6907 static const struct got_error *
6908 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6910 const struct got_error *err;
6911 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6912 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6914 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6915 if (err)
6916 goto done;
6917 err = delete_ref(tmp_branch_name, repo);
6918 if (err)
6919 goto done;
6921 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6922 worktree);
6923 if (err)
6924 goto done;
6925 err = delete_ref(base_commit_ref_name, repo);
6926 if (err)
6927 goto done;
6929 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6930 if (err)
6931 goto done;
6932 err = delete_ref(branch_ref_name, repo);
6933 if (err)
6934 goto done;
6936 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6937 if (err)
6938 goto done;
6939 err = delete_ref(commit_ref_name, repo);
6940 if (err)
6941 goto done;
6942 done:
6943 free(tmp_branch_name);
6944 free(base_commit_ref_name);
6945 free(branch_ref_name);
6946 free(commit_ref_name);
6947 return err;
6950 const struct got_error *
6951 got_worktree_histedit_abort(struct got_worktree *worktree,
6952 struct got_fileindex *fileindex, struct got_repository *repo,
6953 struct got_reference *branch, struct got_object_id *base_commit_id,
6954 got_worktree_checkout_cb progress_cb, void *progress_arg)
6956 const struct got_error *err, *unlockerr, *sync_err;
6957 struct got_reference *resolved = NULL;
6958 char *fileindex_path = NULL;
6959 struct got_object_id *tree_id = NULL;
6960 struct revert_file_args rfa;
6962 err = lock_worktree(worktree, LOCK_EX);
6963 if (err)
6964 return err;
6966 err = got_ref_open(&resolved, repo,
6967 got_ref_get_symref_target(branch), 0);
6968 if (err)
6969 goto done;
6971 err = got_worktree_set_head_ref(worktree, resolved);
6972 if (err)
6973 goto done;
6975 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6976 if (err)
6977 goto done;
6979 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6980 worktree->path_prefix);
6981 if (err)
6982 goto done;
6984 err = delete_histedit_refs(worktree, repo);
6985 if (err)
6986 goto done;
6988 err = get_fileindex_path(&fileindex_path, worktree);
6989 if (err)
6990 goto done;
6992 rfa.worktree = worktree;
6993 rfa.fileindex = fileindex;
6994 rfa.progress_cb = progress_cb;
6995 rfa.progress_arg = progress_arg;
6996 rfa.patch_cb = NULL;
6997 rfa.patch_arg = NULL;
6998 rfa.repo = repo;
6999 err = worktree_status(worktree, "", fileindex, repo,
7000 revert_file, &rfa, NULL, NULL, 0, 0);
7001 if (err)
7002 goto sync;
7004 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7005 repo, progress_cb, progress_arg, NULL, NULL);
7006 sync:
7007 sync_err = sync_fileindex(fileindex, fileindex_path);
7008 if (sync_err && err == NULL)
7009 err = sync_err;
7010 done:
7011 got_ref_close(resolved);
7012 free(tree_id);
7013 free(fileindex_path);
7015 unlockerr = lock_worktree(worktree, LOCK_SH);
7016 if (unlockerr && err == NULL)
7017 err = unlockerr;
7018 return err;
7021 const struct got_error *
7022 got_worktree_histedit_complete(struct got_worktree *worktree,
7023 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7024 struct got_reference *edited_branch, struct got_repository *repo)
7026 const struct got_error *err, *unlockerr, *sync_err;
7027 struct got_object_id *new_head_commit_id = NULL;
7028 struct got_reference *resolved = NULL;
7029 char *fileindex_path = NULL;
7031 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7032 if (err)
7033 return err;
7035 err = got_ref_open(&resolved, repo,
7036 got_ref_get_symref_target(edited_branch), 0);
7037 if (err)
7038 goto done;
7040 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7041 resolved, new_head_commit_id, repo);
7042 if (err)
7043 goto done;
7045 err = got_ref_change_ref(resolved, new_head_commit_id);
7046 if (err)
7047 goto done;
7049 err = got_ref_write(resolved, repo);
7050 if (err)
7051 goto done;
7053 err = got_worktree_set_head_ref(worktree, resolved);
7054 if (err)
7055 goto done;
7057 err = delete_histedit_refs(worktree, repo);
7058 if (err)
7059 goto done;
7061 err = get_fileindex_path(&fileindex_path, worktree);
7062 if (err)
7063 goto done;
7064 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7065 sync_err = sync_fileindex(fileindex, fileindex_path);
7066 if (sync_err && err == NULL)
7067 err = sync_err;
7068 done:
7069 got_fileindex_free(fileindex);
7070 free(fileindex_path);
7071 free(new_head_commit_id);
7072 unlockerr = lock_worktree(worktree, LOCK_SH);
7073 if (unlockerr && err == NULL)
7074 err = unlockerr;
7075 return err;
7078 const struct got_error *
7079 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7080 struct got_object_id *commit_id, struct got_repository *repo)
7082 const struct got_error *err;
7083 char *commit_ref_name;
7085 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7086 if (err)
7087 return err;
7089 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7090 if (err)
7091 goto done;
7093 err = delete_ref(commit_ref_name, repo);
7094 done:
7095 free(commit_ref_name);
7096 return err;
7099 const struct got_error *
7100 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7101 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7102 struct got_worktree *worktree, const char *refname,
7103 struct got_repository *repo)
7105 const struct got_error *err = NULL;
7106 char *fileindex_path = NULL;
7107 struct check_rebase_ok_arg ok_arg;
7109 *fileindex = NULL;
7110 *branch_ref = NULL;
7111 *base_branch_ref = NULL;
7113 err = lock_worktree(worktree, LOCK_EX);
7114 if (err)
7115 return err;
7117 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7118 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7119 "cannot integrate a branch into itself; "
7120 "update -b or different branch name required");
7121 goto done;
7124 err = open_fileindex(fileindex, &fileindex_path, worktree);
7125 if (err)
7126 goto done;
7128 /* Preconditions are the same as for rebase. */
7129 ok_arg.worktree = worktree;
7130 ok_arg.repo = repo;
7131 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7132 &ok_arg);
7133 if (err)
7134 goto done;
7136 err = got_ref_open(branch_ref, repo, refname, 1);
7137 if (err)
7138 goto done;
7140 err = got_ref_open(base_branch_ref, repo,
7141 got_worktree_get_head_ref_name(worktree), 1);
7142 done:
7143 if (err) {
7144 if (*branch_ref) {
7145 got_ref_close(*branch_ref);
7146 *branch_ref = NULL;
7148 if (*base_branch_ref) {
7149 got_ref_close(*base_branch_ref);
7150 *base_branch_ref = NULL;
7152 if (*fileindex) {
7153 got_fileindex_free(*fileindex);
7154 *fileindex = NULL;
7156 lock_worktree(worktree, LOCK_SH);
7158 return err;
7161 const struct got_error *
7162 got_worktree_integrate_continue(struct got_worktree *worktree,
7163 struct got_fileindex *fileindex, struct got_repository *repo,
7164 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7165 got_worktree_checkout_cb progress_cb, void *progress_arg,
7166 got_cancel_cb cancel_cb, void *cancel_arg)
7168 const struct got_error *err = NULL, *sync_err, *unlockerr;
7169 char *fileindex_path = NULL;
7170 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7172 err = get_fileindex_path(&fileindex_path, worktree);
7173 if (err)
7174 goto done;
7176 err = got_ref_resolve(&commit_id, repo, branch_ref);
7177 if (err)
7178 goto done;
7180 err = got_object_id_by_path(&tree_id, repo, commit_id,
7181 worktree->path_prefix);
7182 if (err)
7183 goto done;
7185 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7186 if (err)
7187 goto done;
7189 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7190 progress_cb, progress_arg, cancel_cb, cancel_arg);
7191 if (err)
7192 goto sync;
7194 err = got_ref_change_ref(base_branch_ref, commit_id);
7195 if (err)
7196 goto sync;
7198 err = got_ref_write(base_branch_ref, repo);
7199 if (err)
7200 goto sync;
7202 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7203 sync:
7204 sync_err = sync_fileindex(fileindex, fileindex_path);
7205 if (sync_err && err == NULL)
7206 err = sync_err;
7208 done:
7209 unlockerr = got_ref_unlock(branch_ref);
7210 if (unlockerr && err == NULL)
7211 err = unlockerr;
7212 got_ref_close(branch_ref);
7214 unlockerr = got_ref_unlock(base_branch_ref);
7215 if (unlockerr && err == NULL)
7216 err = unlockerr;
7217 got_ref_close(base_branch_ref);
7219 got_fileindex_free(fileindex);
7220 free(fileindex_path);
7221 free(tree_id);
7223 unlockerr = lock_worktree(worktree, LOCK_SH);
7224 if (unlockerr && err == NULL)
7225 err = unlockerr;
7226 return err;
7229 const struct got_error *
7230 got_worktree_integrate_abort(struct got_worktree *worktree,
7231 struct got_fileindex *fileindex, struct got_repository *repo,
7232 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7234 const struct got_error *err = NULL, *unlockerr = NULL;
7236 got_fileindex_free(fileindex);
7238 err = lock_worktree(worktree, LOCK_SH);
7240 unlockerr = got_ref_unlock(branch_ref);
7241 if (unlockerr && err == NULL)
7242 err = unlockerr;
7243 got_ref_close(branch_ref);
7245 unlockerr = got_ref_unlock(base_branch_ref);
7246 if (unlockerr && err == NULL)
7247 err = unlockerr;
7248 got_ref_close(base_branch_ref);
7250 return err;
7253 struct check_stage_ok_arg {
7254 struct got_object_id *head_commit_id;
7255 struct got_worktree *worktree;
7256 struct got_fileindex *fileindex;
7257 struct got_repository *repo;
7258 int have_changes;
7261 const struct got_error *
7262 check_stage_ok(void *arg, unsigned char status,
7263 unsigned char staged_status, const char *relpath,
7264 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7265 struct got_object_id *commit_id, int dirfd, const char *de_name)
7267 struct check_stage_ok_arg *a = arg;
7268 const struct got_error *err = NULL;
7269 struct got_fileindex_entry *ie;
7270 struct got_object_id base_commit_id;
7271 struct got_object_id *base_commit_idp = NULL;
7272 char *in_repo_path = NULL, *p;
7274 if (status == GOT_STATUS_UNVERSIONED ||
7275 status == GOT_STATUS_NO_CHANGE)
7276 return NULL;
7277 if (status == GOT_STATUS_NONEXISTENT)
7278 return got_error_set_errno(ENOENT, relpath);
7280 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7281 if (ie == NULL)
7282 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7284 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7285 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7286 relpath) == -1)
7287 return got_error_from_errno("asprintf");
7289 if (got_fileindex_entry_has_commit(ie)) {
7290 memcpy(base_commit_id.sha1, ie->commit_sha1,
7291 SHA1_DIGEST_LENGTH);
7292 base_commit_idp = &base_commit_id;
7295 if (status == GOT_STATUS_CONFLICT) {
7296 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7297 goto done;
7298 } else if (status != GOT_STATUS_ADD &&
7299 status != GOT_STATUS_MODIFY &&
7300 status != GOT_STATUS_DELETE) {
7301 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7302 goto done;
7305 a->have_changes = 1;
7307 p = in_repo_path;
7308 while (p[0] == '/')
7309 p++;
7310 err = check_out_of_date(p, status, staged_status,
7311 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7312 GOT_ERR_STAGE_OUT_OF_DATE);
7313 done:
7314 free(in_repo_path);
7315 return err;
7318 struct stage_path_arg {
7319 struct got_worktree *worktree;
7320 struct got_fileindex *fileindex;
7321 struct got_repository *repo;
7322 got_worktree_status_cb status_cb;
7323 void *status_arg;
7324 got_worktree_patch_cb patch_cb;
7325 void *patch_arg;
7326 int staged_something;
7327 int allow_bad_symlinks;
7330 static const struct got_error *
7331 stage_path(void *arg, unsigned char status,
7332 unsigned char staged_status, const char *relpath,
7333 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7334 struct got_object_id *commit_id, int dirfd, const char *de_name)
7336 struct stage_path_arg *a = arg;
7337 const struct got_error *err = NULL;
7338 struct got_fileindex_entry *ie;
7339 char *ondisk_path = NULL, *path_content = NULL;
7340 uint32_t stage;
7341 struct got_object_id *new_staged_blob_id = NULL;
7342 struct stat sb;
7344 if (status == GOT_STATUS_UNVERSIONED)
7345 return NULL;
7347 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7348 if (ie == NULL)
7349 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7351 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7352 relpath)== -1)
7353 return got_error_from_errno("asprintf");
7355 switch (status) {
7356 case GOT_STATUS_ADD:
7357 case GOT_STATUS_MODIFY:
7358 /* XXX could sb.st_mode be passed in by our caller? */
7359 if (lstat(ondisk_path, &sb) == -1) {
7360 err = got_error_from_errno2("lstat", ondisk_path);
7361 break;
7363 if (a->patch_cb) {
7364 if (status == GOT_STATUS_ADD) {
7365 int choice = GOT_PATCH_CHOICE_NONE;
7366 err = (*a->patch_cb)(&choice, a->patch_arg,
7367 status, ie->path, NULL, 1, 1);
7368 if (err)
7369 break;
7370 if (choice != GOT_PATCH_CHOICE_YES)
7371 break;
7372 } else {
7373 err = create_patched_content(&path_content, 0,
7374 staged_blob_id ? staged_blob_id : blob_id,
7375 ondisk_path, dirfd, de_name, ie->path,
7376 a->repo, a->patch_cb, a->patch_arg);
7377 if (err || path_content == NULL)
7378 break;
7381 err = got_object_blob_create(&new_staged_blob_id,
7382 path_content ? path_content : ondisk_path, a->repo);
7383 if (err)
7384 break;
7385 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7386 SHA1_DIGEST_LENGTH);
7387 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7388 stage = GOT_FILEIDX_STAGE_ADD;
7389 else
7390 stage = GOT_FILEIDX_STAGE_MODIFY;
7391 got_fileindex_entry_stage_set(ie, stage);
7392 if (S_ISLNK(sb.st_mode)) {
7393 int is_bad_symlink = 0;
7394 if (!a->allow_bad_symlinks) {
7395 char target_path[PATH_MAX];
7396 ssize_t target_len;
7397 target_len = readlink(ondisk_path, target_path,
7398 sizeof(target_path));
7399 if (target_len == -1) {
7400 err = got_error_from_errno2("readlink",
7401 ondisk_path);
7402 break;
7404 err = is_bad_symlink_target(&is_bad_symlink,
7405 target_path, target_len, ondisk_path,
7406 a->worktree->root_path);
7407 if (err)
7408 break;
7409 if (is_bad_symlink) {
7410 err = got_error_path(ondisk_path,
7411 GOT_ERR_BAD_SYMLINK);
7412 break;
7415 if (is_bad_symlink)
7416 got_fileindex_entry_staged_filetype_set(ie,
7417 GOT_FILEIDX_MODE_BAD_SYMLINK);
7418 else
7419 got_fileindex_entry_staged_filetype_set(ie,
7420 GOT_FILEIDX_MODE_SYMLINK);
7421 } else {
7422 got_fileindex_entry_staged_filetype_set(ie,
7423 GOT_FILEIDX_MODE_REGULAR_FILE);
7425 a->staged_something = 1;
7426 if (a->status_cb == NULL)
7427 break;
7428 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7429 get_staged_status(ie), relpath, blob_id,
7430 new_staged_blob_id, NULL, dirfd, de_name);
7431 break;
7432 case GOT_STATUS_DELETE:
7433 if (staged_status == GOT_STATUS_DELETE)
7434 break;
7435 if (a->patch_cb) {
7436 int choice = GOT_PATCH_CHOICE_NONE;
7437 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7438 ie->path, NULL, 1, 1);
7439 if (err)
7440 break;
7441 if (choice == GOT_PATCH_CHOICE_NO)
7442 break;
7443 if (choice != GOT_PATCH_CHOICE_YES) {
7444 err = got_error(GOT_ERR_PATCH_CHOICE);
7445 break;
7448 stage = GOT_FILEIDX_STAGE_DELETE;
7449 got_fileindex_entry_stage_set(ie, stage);
7450 a->staged_something = 1;
7451 if (a->status_cb == NULL)
7452 break;
7453 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7454 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7455 de_name);
7456 break;
7457 case GOT_STATUS_NO_CHANGE:
7458 break;
7459 case GOT_STATUS_CONFLICT:
7460 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7461 break;
7462 case GOT_STATUS_NONEXISTENT:
7463 err = got_error_set_errno(ENOENT, relpath);
7464 break;
7465 default:
7466 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7467 break;
7470 if (path_content && unlink(path_content) == -1 && err == NULL)
7471 err = got_error_from_errno2("unlink", path_content);
7472 free(path_content);
7473 free(ondisk_path);
7474 free(new_staged_blob_id);
7475 return err;
7478 const struct got_error *
7479 got_worktree_stage(struct got_worktree *worktree,
7480 struct got_pathlist_head *paths,
7481 got_worktree_status_cb status_cb, void *status_arg,
7482 got_worktree_patch_cb patch_cb, void *patch_arg,
7483 int allow_bad_symlinks, struct got_repository *repo)
7485 const struct got_error *err = NULL, *sync_err, *unlockerr;
7486 struct got_pathlist_entry *pe;
7487 struct got_fileindex *fileindex = NULL;
7488 char *fileindex_path = NULL;
7489 struct got_reference *head_ref = NULL;
7490 struct got_object_id *head_commit_id = NULL;
7491 struct check_stage_ok_arg oka;
7492 struct stage_path_arg spa;
7494 err = lock_worktree(worktree, LOCK_EX);
7495 if (err)
7496 return err;
7498 err = got_ref_open(&head_ref, repo,
7499 got_worktree_get_head_ref_name(worktree), 0);
7500 if (err)
7501 goto done;
7502 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7503 if (err)
7504 goto done;
7505 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7506 if (err)
7507 goto done;
7509 /* Check pre-conditions before staging anything. */
7510 oka.head_commit_id = head_commit_id;
7511 oka.worktree = worktree;
7512 oka.fileindex = fileindex;
7513 oka.repo = repo;
7514 oka.have_changes = 0;
7515 TAILQ_FOREACH(pe, paths, entry) {
7516 err = worktree_status(worktree, pe->path, fileindex, repo,
7517 check_stage_ok, &oka, NULL, NULL, 0, 0);
7518 if (err)
7519 goto done;
7521 if (!oka.have_changes) {
7522 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7523 goto done;
7526 spa.worktree = worktree;
7527 spa.fileindex = fileindex;
7528 spa.repo = repo;
7529 spa.patch_cb = patch_cb;
7530 spa.patch_arg = patch_arg;
7531 spa.status_cb = status_cb;
7532 spa.status_arg = status_arg;
7533 spa.staged_something = 0;
7534 spa.allow_bad_symlinks = allow_bad_symlinks;
7535 TAILQ_FOREACH(pe, paths, entry) {
7536 err = worktree_status(worktree, pe->path, fileindex, repo,
7537 stage_path, &spa, NULL, NULL, 0, 0);
7538 if (err)
7539 goto done;
7541 if (!spa.staged_something) {
7542 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7543 goto done;
7546 sync_err = sync_fileindex(fileindex, fileindex_path);
7547 if (sync_err && err == NULL)
7548 err = sync_err;
7549 done:
7550 if (head_ref)
7551 got_ref_close(head_ref);
7552 free(head_commit_id);
7553 free(fileindex_path);
7554 if (fileindex)
7555 got_fileindex_free(fileindex);
7556 unlockerr = lock_worktree(worktree, LOCK_SH);
7557 if (unlockerr && err == NULL)
7558 err = unlockerr;
7559 return err;
7562 struct unstage_path_arg {
7563 struct got_worktree *worktree;
7564 struct got_fileindex *fileindex;
7565 struct got_repository *repo;
7566 got_worktree_checkout_cb progress_cb;
7567 void *progress_arg;
7568 got_worktree_patch_cb patch_cb;
7569 void *patch_arg;
7572 static const struct got_error *
7573 create_unstaged_content(char **path_unstaged_content,
7574 char **path_new_staged_content, struct got_object_id *blob_id,
7575 struct got_object_id *staged_blob_id, const char *relpath,
7576 struct got_repository *repo,
7577 got_worktree_patch_cb patch_cb, void *patch_arg)
7579 const struct got_error *err, *free_err;
7580 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7581 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7582 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7583 struct got_diffreg_result *diffreg_result = NULL;
7584 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7585 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7587 *path_unstaged_content = NULL;
7588 *path_new_staged_content = NULL;
7590 err = got_object_id_str(&label1, blob_id);
7591 if (err)
7592 return err;
7593 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7594 if (err)
7595 goto done;
7597 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7598 if (err)
7599 goto done;
7601 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7602 if (err)
7603 goto done;
7605 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7606 if (err)
7607 goto done;
7609 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7610 if (err)
7611 goto done;
7613 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7614 if (err)
7615 goto done;
7617 err = got_diff_files(&diffreg_result, f1, label1, f2,
7618 path2, 3, 0, 1, NULL);
7619 if (err)
7620 goto done;
7622 err = got_opentemp_named(path_unstaged_content, &outfile,
7623 "got-unstaged-content");
7624 if (err)
7625 goto done;
7626 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7627 "got-new-staged-content");
7628 if (err)
7629 goto done;
7631 if (fseek(f1, 0L, SEEK_SET) == -1) {
7632 err = got_ferror(f1, GOT_ERR_IO);
7633 goto done;
7635 if (fseek(f2, 0L, SEEK_SET) == -1) {
7636 err = got_ferror(f2, GOT_ERR_IO);
7637 goto done;
7639 /* Count the number of actual changes in the diff result. */
7640 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7641 struct diff_chunk_context cc = {};
7642 diff_chunk_context_load_change(&cc, &nchunks_used,
7643 diffreg_result->result, n, 0);
7644 nchanges++;
7646 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7647 int choice;
7648 err = apply_or_reject_change(&choice, &nchunks_used,
7649 diffreg_result->result, n, relpath, f1, f2,
7650 &line_cur1, &line_cur2,
7651 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7652 if (err)
7653 goto done;
7654 if (choice == GOT_PATCH_CHOICE_YES)
7655 have_content = 1;
7656 else
7657 have_rejected_content = 1;
7658 if (choice == GOT_PATCH_CHOICE_QUIT)
7659 break;
7661 if (have_content || have_rejected_content)
7662 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7663 outfile, rejectfile);
7664 done:
7665 free(label1);
7666 if (blob)
7667 got_object_blob_close(blob);
7668 if (staged_blob)
7669 got_object_blob_close(staged_blob);
7670 free_err = got_diffreg_result_free(diffreg_result);
7671 if (free_err && err == NULL)
7672 err = free_err;
7673 if (f1 && fclose(f1) == EOF && err == NULL)
7674 err = got_error_from_errno2("fclose", path1);
7675 if (f2 && fclose(f2) == EOF && err == NULL)
7676 err = got_error_from_errno2("fclose", path2);
7677 if (outfile && fclose(outfile) == EOF && err == NULL)
7678 err = got_error_from_errno2("fclose", *path_unstaged_content);
7679 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7680 err = got_error_from_errno2("fclose", *path_new_staged_content);
7681 if (path1 && unlink(path1) == -1 && err == NULL)
7682 err = got_error_from_errno2("unlink", path1);
7683 if (path2 && unlink(path2) == -1 && err == NULL)
7684 err = got_error_from_errno2("unlink", path2);
7685 if (err || !have_content) {
7686 if (*path_unstaged_content &&
7687 unlink(*path_unstaged_content) == -1 && err == NULL)
7688 err = got_error_from_errno2("unlink",
7689 *path_unstaged_content);
7690 free(*path_unstaged_content);
7691 *path_unstaged_content = NULL;
7693 if (err || !have_content || !have_rejected_content) {
7694 if (*path_new_staged_content &&
7695 unlink(*path_new_staged_content) == -1 && err == NULL)
7696 err = got_error_from_errno2("unlink",
7697 *path_new_staged_content);
7698 free(*path_new_staged_content);
7699 *path_new_staged_content = NULL;
7701 free(path1);
7702 free(path2);
7703 return err;
7706 static const struct got_error *
7707 unstage_hunks(struct got_object_id *staged_blob_id,
7708 struct got_blob_object *blob_base,
7709 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7710 const char *ondisk_path, const char *label_orig,
7711 struct got_worktree *worktree, struct got_repository *repo,
7712 got_worktree_patch_cb patch_cb, void *patch_arg,
7713 got_worktree_checkout_cb progress_cb, void *progress_arg)
7715 const struct got_error *err = NULL;
7716 char *path_unstaged_content = NULL;
7717 char *path_new_staged_content = NULL;
7718 char *parent = NULL, *base_path = NULL;
7719 char *blob_base_path = NULL;
7720 struct got_object_id *new_staged_blob_id = NULL;
7721 FILE *f = NULL, *f_base = NULL;
7722 struct stat sb;
7724 err = create_unstaged_content(&path_unstaged_content,
7725 &path_new_staged_content, blob_id, staged_blob_id,
7726 ie->path, repo, patch_cb, patch_arg);
7727 if (err)
7728 return err;
7730 if (path_unstaged_content == NULL)
7731 return NULL;
7733 if (path_new_staged_content) {
7734 err = got_object_blob_create(&new_staged_blob_id,
7735 path_new_staged_content, repo);
7736 if (err)
7737 goto done;
7740 f = fopen(path_unstaged_content, "r");
7741 if (f == NULL) {
7742 err = got_error_from_errno2("fopen",
7743 path_unstaged_content);
7744 goto done;
7746 if (fstat(fileno(f), &sb) == -1) {
7747 err = got_error_from_errno2("fstat", path_unstaged_content);
7748 goto done;
7750 if (got_fileindex_entry_staged_filetype_get(ie) ==
7751 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7752 char link_target[PATH_MAX];
7753 size_t r;
7754 r = fread(link_target, 1, sizeof(link_target), f);
7755 if (r == 0 && ferror(f)) {
7756 err = got_error_from_errno("fread");
7757 goto done;
7759 if (r >= sizeof(link_target)) { /* should not happen */
7760 err = got_error(GOT_ERR_NO_SPACE);
7761 goto done;
7763 link_target[r] = '\0';
7764 err = merge_symlink(worktree, blob_base,
7765 ondisk_path, ie->path, label_orig, link_target,
7766 worktree->base_commit_id, repo, progress_cb,
7767 progress_arg);
7768 } else {
7769 int local_changes_subsumed;
7771 err = got_path_dirname(&parent, ondisk_path);
7772 if (err)
7773 return err;
7775 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
7776 parent) == -1) {
7777 err = got_error_from_errno("asprintf");
7778 base_path = NULL;
7779 goto done;
7782 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
7783 if (err)
7784 goto done;
7785 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
7786 blob_base);
7787 if (err)
7788 goto done;
7790 err = merge_file(&local_changes_subsumed, worktree,
7791 f_base, ondisk_path, ie->path,
7792 got_fileindex_perms_to_st(ie),
7793 f, label_orig, "unstaged",
7794 repo, progress_cb, progress_arg);
7796 if (err)
7797 goto done;
7799 if (new_staged_blob_id) {
7800 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7801 SHA1_DIGEST_LENGTH);
7802 } else {
7803 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7804 got_fileindex_entry_staged_filetype_set(ie, 0);
7806 done:
7807 free(new_staged_blob_id);
7808 if (path_unstaged_content &&
7809 unlink(path_unstaged_content) == -1 && err == NULL)
7810 err = got_error_from_errno2("unlink", path_unstaged_content);
7811 if (path_new_staged_content &&
7812 unlink(path_new_staged_content) == -1 && err == NULL)
7813 err = got_error_from_errno2("unlink", path_new_staged_content);
7814 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
7815 err = got_error_from_errno2("unlink", blob_base_path);
7816 if (f_base && fclose(f_base) == EOF && err == NULL)
7817 err = got_error_from_errno2("fclose", path_unstaged_content);
7818 if (f && fclose(f) == EOF && err == NULL)
7819 err = got_error_from_errno2("fclose", path_unstaged_content);
7820 free(path_unstaged_content);
7821 free(path_new_staged_content);
7822 free(blob_base_path);
7823 free(parent);
7824 free(base_path);
7825 return err;
7828 static const struct got_error *
7829 unstage_path(void *arg, unsigned char status,
7830 unsigned char staged_status, const char *relpath,
7831 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7832 struct got_object_id *commit_id, int dirfd, const char *de_name)
7834 const struct got_error *err = NULL;
7835 struct unstage_path_arg *a = arg;
7836 struct got_fileindex_entry *ie;
7837 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7838 char *ondisk_path = NULL;
7839 char *id_str = NULL, *label_orig = NULL;
7840 int local_changes_subsumed;
7841 struct stat sb;
7843 if (staged_status != GOT_STATUS_ADD &&
7844 staged_status != GOT_STATUS_MODIFY &&
7845 staged_status != GOT_STATUS_DELETE)
7846 return NULL;
7848 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7849 if (ie == NULL)
7850 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7852 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7853 == -1)
7854 return got_error_from_errno("asprintf");
7856 err = got_object_id_str(&id_str,
7857 commit_id ? commit_id : a->worktree->base_commit_id);
7858 if (err)
7859 goto done;
7860 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7861 id_str) == -1) {
7862 err = got_error_from_errno("asprintf");
7863 goto done;
7866 switch (staged_status) {
7867 case GOT_STATUS_MODIFY:
7868 err = got_object_open_as_blob(&blob_base, a->repo,
7869 blob_id, 8192);
7870 if (err)
7871 break;
7872 /* fall through */
7873 case GOT_STATUS_ADD:
7874 if (a->patch_cb) {
7875 if (staged_status == GOT_STATUS_ADD) {
7876 int choice = GOT_PATCH_CHOICE_NONE;
7877 err = (*a->patch_cb)(&choice, a->patch_arg,
7878 staged_status, ie->path, NULL, 1, 1);
7879 if (err)
7880 break;
7881 if (choice != GOT_PATCH_CHOICE_YES)
7882 break;
7883 } else {
7884 err = unstage_hunks(staged_blob_id,
7885 blob_base, blob_id, ie, ondisk_path,
7886 label_orig, a->worktree, a->repo,
7887 a->patch_cb, a->patch_arg,
7888 a->progress_cb, a->progress_arg);
7889 break; /* Done with this file. */
7892 err = got_object_open_as_blob(&blob_staged, a->repo,
7893 staged_blob_id, 8192);
7894 if (err)
7895 break;
7896 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7897 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7898 case GOT_FILEIDX_MODE_REGULAR_FILE:
7899 err = merge_blob(&local_changes_subsumed, a->worktree,
7900 blob_base, ondisk_path, relpath,
7901 got_fileindex_perms_to_st(ie), label_orig,
7902 blob_staged, commit_id ? commit_id :
7903 a->worktree->base_commit_id, a->repo,
7904 a->progress_cb, a->progress_arg);
7905 break;
7906 case GOT_FILEIDX_MODE_SYMLINK:
7907 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7908 char *staged_target;
7909 err = got_object_blob_read_to_str(
7910 &staged_target, blob_staged);
7911 if (err)
7912 goto done;
7913 err = merge_symlink(a->worktree, blob_base,
7914 ondisk_path, relpath, label_orig,
7915 staged_target, commit_id ? commit_id :
7916 a->worktree->base_commit_id,
7917 a->repo, a->progress_cb, a->progress_arg);
7918 free(staged_target);
7919 } else {
7920 err = merge_blob(&local_changes_subsumed,
7921 a->worktree, blob_base, ondisk_path,
7922 relpath, got_fileindex_perms_to_st(ie),
7923 label_orig, blob_staged,
7924 commit_id ? commit_id :
7925 a->worktree->base_commit_id, a->repo,
7926 a->progress_cb, a->progress_arg);
7928 break;
7929 default:
7930 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7931 break;
7933 if (err == NULL) {
7934 got_fileindex_entry_stage_set(ie,
7935 GOT_FILEIDX_STAGE_NONE);
7936 got_fileindex_entry_staged_filetype_set(ie, 0);
7938 break;
7939 case GOT_STATUS_DELETE:
7940 if (a->patch_cb) {
7941 int choice = GOT_PATCH_CHOICE_NONE;
7942 err = (*a->patch_cb)(&choice, a->patch_arg,
7943 staged_status, ie->path, NULL, 1, 1);
7944 if (err)
7945 break;
7946 if (choice == GOT_PATCH_CHOICE_NO)
7947 break;
7948 if (choice != GOT_PATCH_CHOICE_YES) {
7949 err = got_error(GOT_ERR_PATCH_CHOICE);
7950 break;
7953 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7954 got_fileindex_entry_staged_filetype_set(ie, 0);
7955 err = get_file_status(&status, &sb, ie, ondisk_path,
7956 dirfd, de_name, a->repo);
7957 if (err)
7958 break;
7959 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7960 break;
7962 done:
7963 free(ondisk_path);
7964 if (blob_base)
7965 got_object_blob_close(blob_base);
7966 if (blob_staged)
7967 got_object_blob_close(blob_staged);
7968 free(id_str);
7969 free(label_orig);
7970 return err;
7973 const struct got_error *
7974 got_worktree_unstage(struct got_worktree *worktree,
7975 struct got_pathlist_head *paths,
7976 got_worktree_checkout_cb progress_cb, void *progress_arg,
7977 got_worktree_patch_cb patch_cb, void *patch_arg,
7978 struct got_repository *repo)
7980 const struct got_error *err = NULL, *sync_err, *unlockerr;
7981 struct got_pathlist_entry *pe;
7982 struct got_fileindex *fileindex = NULL;
7983 char *fileindex_path = NULL;
7984 struct unstage_path_arg upa;
7986 err = lock_worktree(worktree, LOCK_EX);
7987 if (err)
7988 return err;
7990 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7991 if (err)
7992 goto done;
7994 upa.worktree = worktree;
7995 upa.fileindex = fileindex;
7996 upa.repo = repo;
7997 upa.progress_cb = progress_cb;
7998 upa.progress_arg = progress_arg;
7999 upa.patch_cb = patch_cb;
8000 upa.patch_arg = patch_arg;
8001 TAILQ_FOREACH(pe, paths, entry) {
8002 err = worktree_status(worktree, pe->path, fileindex, repo,
8003 unstage_path, &upa, NULL, NULL, 0, 0);
8004 if (err)
8005 goto done;
8008 sync_err = sync_fileindex(fileindex, fileindex_path);
8009 if (sync_err && err == NULL)
8010 err = sync_err;
8011 done:
8012 free(fileindex_path);
8013 if (fileindex)
8014 got_fileindex_free(fileindex);
8015 unlockerr = lock_worktree(worktree, LOCK_SH);
8016 if (unlockerr && err == NULL)
8017 err = unlockerr;
8018 return err;
8021 struct report_file_info_arg {
8022 struct got_worktree *worktree;
8023 got_worktree_path_info_cb info_cb;
8024 void *info_arg;
8025 struct got_pathlist_head *paths;
8026 got_cancel_cb cancel_cb;
8027 void *cancel_arg;
8030 static const struct got_error *
8031 report_file_info(void *arg, struct got_fileindex_entry *ie)
8033 struct report_file_info_arg *a = arg;
8034 struct got_pathlist_entry *pe;
8035 struct got_object_id blob_id, staged_blob_id, commit_id;
8036 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8037 struct got_object_id *commit_idp = NULL;
8038 int stage;
8040 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8041 return got_error(GOT_ERR_CANCELLED);
8043 TAILQ_FOREACH(pe, a->paths, entry) {
8044 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8045 got_path_is_child(ie->path, pe->path, pe->path_len))
8046 break;
8048 if (pe == NULL) /* not found */
8049 return NULL;
8051 if (got_fileindex_entry_has_blob(ie)) {
8052 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8053 blob_idp = &blob_id;
8055 stage = got_fileindex_entry_stage_get(ie);
8056 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8057 stage == GOT_FILEIDX_STAGE_ADD) {
8058 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8059 SHA1_DIGEST_LENGTH);
8060 staged_blob_idp = &staged_blob_id;
8063 if (got_fileindex_entry_has_commit(ie)) {
8064 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8065 commit_idp = &commit_id;
8068 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8069 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8072 const struct got_error *
8073 got_worktree_path_info(struct got_worktree *worktree,
8074 struct got_pathlist_head *paths,
8075 got_worktree_path_info_cb info_cb, void *info_arg,
8076 got_cancel_cb cancel_cb, void *cancel_arg)
8079 const struct got_error *err = NULL, *unlockerr;
8080 struct got_fileindex *fileindex = NULL;
8081 char *fileindex_path = NULL;
8082 struct report_file_info_arg arg;
8084 err = lock_worktree(worktree, LOCK_SH);
8085 if (err)
8086 return err;
8088 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8089 if (err)
8090 goto done;
8092 arg.worktree = worktree;
8093 arg.info_cb = info_cb;
8094 arg.info_arg = info_arg;
8095 arg.paths = paths;
8096 arg.cancel_cb = cancel_cb;
8097 arg.cancel_arg = cancel_arg;
8098 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8099 &arg);
8100 done:
8101 free(fileindex_path);
8102 if (fileindex)
8103 got_fileindex_free(fileindex);
8104 unlockerr = lock_worktree(worktree, LOCK_UN);
8105 if (unlockerr && err == NULL)
8106 err = unlockerr;
8107 return err;