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, const char *f1_path, const char *f2_path)
733 const struct got_error *err = NULL;
734 struct stat sb;
735 size_t size1, size2;
736 FILE *f1 = NULL, *f2 = NULL;
738 *same = 1;
740 if (lstat(f1_path, &sb) != 0) {
741 err = got_error_from_errno2("lstat", f1_path);
742 goto done;
744 size1 = sb.st_size;
746 if (lstat(f2_path, &sb) != 0) {
747 err = got_error_from_errno2("lstat", f2_path);
748 goto done;
750 size2 = sb.st_size;
752 if (size1 != size2) {
753 *same = 0;
754 return NULL;
757 f1 = fopen(f1_path, "r");
758 if (f1 == NULL)
759 return got_error_from_errno2("fopen", f1_path);
761 f2 = fopen(f2_path, "r");
762 if (f2 == NULL) {
763 err = got_error_from_errno2("fopen", f2_path);
764 goto done;
767 err = check_file_contents_equal(same, f1, f2);
768 done:
769 if (f1 && fclose(f1) == EOF && err == NULL)
770 err = got_error_from_errno("fclose");
771 if (f2 && fclose(f2) == EOF && err == NULL)
772 err = got_error_from_errno("fclose");
774 return err;
777 /*
778 * Perform a 3-way merge where blob_orig acts as the common ancestor,
779 * the file at deriv_path acts as the first derived version, and the
780 * file on disk acts as the second derived version.
781 */
782 static const struct got_error *
783 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
784 struct got_blob_object *blob_orig, const char *ondisk_path,
785 const char *path, uint16_t st_mode, const char *deriv_path,
786 const char *label_orig, const char *label_deriv,
787 struct got_repository *repo,
788 got_worktree_checkout_cb progress_cb, void *progress_arg)
790 const struct got_error *err = NULL;
791 int merged_fd = -1;
792 FILE *f_orig = NULL;
793 char *blob_orig_path = NULL;
794 char *merged_path = NULL, *base_path = NULL;
795 int overlapcnt = 0;
796 char *parent = NULL;
797 char *symlink_path = NULL;
798 FILE *symlinkf = NULL;
800 *local_changes_subsumed = 0;
802 err = got_path_dirname(&parent, ondisk_path);
803 if (err)
804 return err;
806 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
807 err = got_error_from_errno("asprintf");
808 goto done;
811 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
812 if (err)
813 goto done;
815 free(base_path);
816 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
817 err = got_error_from_errno("asprintf");
818 base_path = NULL;
819 goto done;
822 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
823 if (err)
824 goto done;
825 if (blob_orig) {
826 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
827 blob_orig);
828 if (err)
829 goto done;
830 } else {
831 /*
832 * If the file has no blob, this is an "add vs add" conflict,
833 * and we simply use an empty ancestor file to make both files
834 * appear in the merged result in their entirety.
835 */
838 /*
839 * In order the run a 3-way merge with a symlink we copy the symlink's
840 * target path into a temporary file and use that file with diff3.
841 */
842 if (S_ISLNK(st_mode)) {
843 char target_path[PATH_MAX];
844 ssize_t target_len;
845 size_t n;
847 free(base_path);
848 if (asprintf(&base_path, "%s/got-symlink-merge",
849 parent) == -1) {
850 err = got_error_from_errno("asprintf");
851 base_path = NULL;
852 goto done;
854 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
855 if (err)
856 goto done;
857 target_len = readlink(ondisk_path, target_path,
858 sizeof(target_path));
859 if (target_len == -1) {
860 err = got_error_from_errno2("readlink", ondisk_path);
861 goto done;
863 n = fwrite(target_path, 1, target_len, symlinkf);
864 if (n != target_len) {
865 err = got_ferror(symlinkf, GOT_ERR_IO);
866 goto done;
868 if (fflush(symlinkf) == EOF) {
869 err = got_error_from_errno2("fflush", symlink_path);
870 goto done;
874 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
875 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
876 label_deriv, label_orig, NULL);
877 if (err)
878 goto done;
880 err = (*progress_cb)(progress_arg,
881 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
882 if (err)
883 goto done;
885 if (fsync(merged_fd) != 0) {
886 err = got_error_from_errno("fsync");
887 goto done;
890 /* Check if a clean merge has subsumed all local changes. */
891 if (overlapcnt == 0) {
892 err = check_files_equal(local_changes_subsumed, deriv_path,
893 merged_path);
894 if (err)
895 goto done;
898 if (fchmod(merged_fd, st_mode) != 0) {
899 err = got_error_from_errno2("fchmod", merged_path);
900 goto done;
903 if (rename(merged_path, ondisk_path) != 0) {
904 err = got_error_from_errno3("rename", merged_path,
905 ondisk_path);
906 goto done;
908 done:
909 if (err) {
910 if (merged_path)
911 unlink(merged_path);
913 if (symlink_path) {
914 if (unlink(symlink_path) == -1 && err == NULL)
915 err = got_error_from_errno2("unlink", symlink_path);
917 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
918 err = got_error_from_errno2("fclose", symlink_path);
919 free(symlink_path);
920 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
921 err = got_error_from_errno("close");
922 if (f_orig && fclose(f_orig) == EOF && err == NULL)
923 err = got_error_from_errno("fclose");
924 free(merged_path);
925 free(base_path);
926 if (blob_orig_path) {
927 unlink(blob_orig_path);
928 free(blob_orig_path);
930 free(parent);
931 return err;
934 static const struct got_error *
935 update_symlink(const char *ondisk_path, const char *target_path,
936 size_t target_len)
938 /* This is not atomic but matches what 'ln -sf' does. */
939 if (unlink(ondisk_path) == -1)
940 return got_error_from_errno2("unlink", ondisk_path);
941 if (symlink(target_path, ondisk_path) == -1)
942 return got_error_from_errno3("symlink", target_path,
943 ondisk_path);
944 return NULL;
947 /*
948 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
949 * in the work tree with a file that contains conflict markers and the
950 * conflicting target paths of the original version, a "derived version"
951 * of a symlink from an incoming change, and a local version of the symlink.
953 * The original versions's target path can be NULL if it is not available,
954 * such as if both derived versions added a new symlink at the same path.
956 * The incoming derived symlink target is NULL in case the incoming change
957 * has deleted this symlink.
958 */
959 static const struct got_error *
960 install_symlink_conflict(const char *deriv_target,
961 struct got_object_id *deriv_base_commit_id, const char *orig_target,
962 const char *label_orig, const char *local_target, const char *ondisk_path)
964 const struct got_error *err;
965 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
966 FILE *f = NULL;
968 err = got_object_id_str(&id_str, deriv_base_commit_id);
969 if (err)
970 return got_error_from_errno("asprintf");
972 if (asprintf(&label_deriv, "%s: commit %s",
973 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
974 err = got_error_from_errno("asprintf");
975 goto done;
978 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
979 if (err)
980 goto done;
982 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
983 err = got_error_from_errno2("fchmod", path);
984 goto done;
987 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
988 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
989 deriv_target ? deriv_target : "(symlink was deleted)",
990 orig_target ? label_orig : "",
991 orig_target ? "\n" : "",
992 orig_target ? orig_target : "",
993 orig_target ? "\n" : "",
994 GOT_DIFF_CONFLICT_MARKER_SEP,
995 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
996 err = got_error_from_errno2("fprintf", path);
997 goto done;
1000 if (unlink(ondisk_path) == -1) {
1001 err = got_error_from_errno2("unlink", ondisk_path);
1002 goto done;
1004 if (rename(path, ondisk_path) == -1) {
1005 err = got_error_from_errno3("rename", path, ondisk_path);
1006 goto done;
1008 done:
1009 if (f != NULL && fclose(f) == EOF && err == NULL)
1010 err = got_error_from_errno2("fclose", path);
1011 free(path);
1012 free(id_str);
1013 free(label_deriv);
1014 return err;
1017 /* forward declaration */
1018 static const struct got_error *
1019 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1020 const char *, const char *, uint16_t, const char *,
1021 struct got_blob_object *, struct got_object_id *,
1022 struct got_repository *, got_worktree_checkout_cb, void *);
1025 * Merge a symlink into the work tree, where blob_orig acts as the common
1026 * ancestor, deriv_target is the link target of the first derived version,
1027 * and the symlink on disk acts as the second derived version.
1028 * Assume that contents of both blobs represent symlinks.
1030 static const struct got_error *
1031 merge_symlink(struct got_worktree *worktree,
1032 struct got_blob_object *blob_orig, const char *ondisk_path,
1033 const char *path, const char *label_orig, const char *deriv_target,
1034 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1035 got_worktree_checkout_cb progress_cb, void *progress_arg)
1037 const struct got_error *err = NULL;
1038 char *ancestor_target = NULL;
1039 struct stat sb;
1040 ssize_t ondisk_len, deriv_len;
1041 char ondisk_target[PATH_MAX];
1042 int have_local_change = 0;
1043 int have_incoming_change = 0;
1045 if (lstat(ondisk_path, &sb) == -1)
1046 return got_error_from_errno2("lstat", ondisk_path);
1048 ondisk_len = readlink(ondisk_path, ondisk_target,
1049 sizeof(ondisk_target));
1050 if (ondisk_len == -1) {
1051 err = got_error_from_errno2("readlink",
1052 ondisk_path);
1053 goto done;
1055 ondisk_target[ondisk_len] = '\0';
1057 if (blob_orig) {
1058 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1059 if (err)
1060 goto done;
1063 if (ancestor_target == NULL ||
1064 (ondisk_len != strlen(ancestor_target) ||
1065 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1066 have_local_change = 1;
1068 deriv_len = strlen(deriv_target);
1069 if (ancestor_target == NULL ||
1070 (deriv_len != strlen(ancestor_target) ||
1071 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1072 have_incoming_change = 1;
1074 if (!have_local_change && !have_incoming_change) {
1075 if (ancestor_target) {
1076 /* Both sides made the same change. */
1077 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1078 path);
1079 } else if (deriv_len == ondisk_len &&
1080 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1081 /* Both sides added the same symlink. */
1082 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1083 path);
1084 } else {
1085 /* Both sides added symlinks which don't match. */
1086 err = install_symlink_conflict(deriv_target,
1087 deriv_base_commit_id, ancestor_target,
1088 label_orig, ondisk_target, ondisk_path);
1089 if (err)
1090 goto done;
1091 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1092 path);
1094 } else if (!have_local_change && have_incoming_change) {
1095 /* Apply the incoming change. */
1096 err = update_symlink(ondisk_path, deriv_target,
1097 strlen(deriv_target));
1098 if (err)
1099 goto done;
1100 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1101 } else if (have_local_change && have_incoming_change) {
1102 if (deriv_len == ondisk_len &&
1103 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1104 /* Both sides made the same change. */
1105 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1106 path);
1107 } else {
1108 err = install_symlink_conflict(deriv_target,
1109 deriv_base_commit_id, ancestor_target, label_orig,
1110 ondisk_target, ondisk_path);
1111 if (err)
1112 goto done;
1113 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1114 path);
1118 done:
1119 free(ancestor_target);
1120 return err;
1124 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1125 * blob_deriv acts as the first derived version, and the file on disk
1126 * acts as the second derived version.
1128 static const struct got_error *
1129 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1130 struct got_blob_object *blob_orig, const char *ondisk_path,
1131 const char *path, uint16_t st_mode, const char *label_orig,
1132 struct got_blob_object *blob_deriv,
1133 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1134 got_worktree_checkout_cb progress_cb, void *progress_arg)
1136 const struct got_error *err = NULL;
1137 FILE *f_deriv = NULL;
1138 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1139 char *label_deriv = NULL, *parent = NULL;
1141 *local_changes_subsumed = 0;
1143 err = got_path_dirname(&parent, ondisk_path);
1144 if (err)
1145 return err;
1147 free(base_path);
1148 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1149 err = got_error_from_errno("asprintf");
1150 base_path = NULL;
1151 goto done;
1154 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1155 if (err)
1156 goto done;
1157 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1158 blob_deriv);
1159 if (err)
1160 goto done;
1162 err = got_object_id_str(&id_str, deriv_base_commit_id);
1163 if (err)
1164 goto done;
1165 if (asprintf(&label_deriv, "%s: commit %s",
1166 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1167 err = got_error_from_errno("asprintf");
1168 goto done;
1171 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1172 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1173 label_deriv, repo, progress_cb, progress_arg);
1174 done:
1175 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1176 err = got_error_from_errno("fclose");
1177 free(base_path);
1178 if (blob_deriv_path) {
1179 unlink(blob_deriv_path);
1180 free(blob_deriv_path);
1182 free(id_str);
1183 free(label_deriv);
1184 free(parent);
1185 return err;
1188 static const struct got_error *
1189 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1190 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1191 int wt_fd, const char *path, struct got_object_id *blob_id)
1193 const struct got_error *err = NULL;
1194 struct got_fileindex_entry *new_ie;
1196 *new_iep = NULL;
1198 err = got_fileindex_entry_alloc(&new_ie, path);
1199 if (err)
1200 return err;
1202 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1203 blob_id->sha1, base_commit_id->sha1, 1);
1204 if (err)
1205 goto done;
1207 err = got_fileindex_entry_add(fileindex, new_ie);
1208 done:
1209 if (err)
1210 got_fileindex_entry_free(new_ie);
1211 else
1212 *new_iep = new_ie;
1213 return err;
1216 static mode_t
1217 get_ondisk_perms(int executable, mode_t st_mode)
1219 mode_t xbits = S_IXUSR;
1221 if (executable) {
1222 /* Map read bits to execute bits. */
1223 if (st_mode & S_IRGRP)
1224 xbits |= S_IXGRP;
1225 if (st_mode & S_IROTH)
1226 xbits |= S_IXOTH;
1227 return st_mode | xbits;
1230 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1233 /* forward declaration */
1234 static const struct got_error *
1235 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1236 const char *path, mode_t te_mode, mode_t st_mode,
1237 struct got_blob_object *blob, int restoring_missing_file,
1238 int reverting_versioned_file, int installing_bad_symlink,
1239 int path_is_unversioned, struct got_repository *repo,
1240 got_worktree_checkout_cb progress_cb, void *progress_arg);
1243 * This function assumes that the provided symlink target points at a
1244 * safe location in the work tree!
1246 static const struct got_error *
1247 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1248 size_t target_len)
1250 const struct got_error *err = NULL;
1251 ssize_t elen;
1252 char etarget[PATH_MAX];
1253 int fd;
1256 * "Bad" symlinks (those pointing outside the work tree or into the
1257 * .got directory) are installed in the work tree as a regular file
1258 * which contains the bad symlink target path.
1259 * The new symlink target has already been checked for safety by our
1260 * caller. If we can successfully open a regular file then we simply
1261 * replace this file with a symlink below.
1263 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1264 if (fd == -1) {
1265 if (errno != ELOOP)
1266 return got_error_from_errno2("open", ondisk_path);
1268 /* We are updating an existing on-disk symlink. */
1269 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1270 if (elen == -1)
1271 return got_error_from_errno2("readlink", ondisk_path);
1273 if (elen == target_len &&
1274 memcmp(etarget, target_path, target_len) == 0)
1275 return NULL; /* nothing to do */
1278 err = update_symlink(ondisk_path, target_path, target_len);
1279 if (fd != -1 && close(fd) == -1 && err == NULL)
1280 err = got_error_from_errno2("close", ondisk_path);
1281 return err;
1284 static const struct got_error *
1285 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1286 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1288 const struct got_error *err = NULL;
1289 char canonpath[PATH_MAX];
1290 char *path_got = NULL;
1292 *is_bad_symlink = 0;
1294 if (target_len >= sizeof(canonpath)) {
1295 *is_bad_symlink = 1;
1296 return NULL;
1300 * We do not use realpath(3) to resolve the symlink's target
1301 * path because we don't want to resolve symlinks recursively.
1302 * Instead we make the path absolute and then canonicalize it.
1303 * Relative symlink target lookup should begin at the directory
1304 * in which the blob object is being installed.
1306 if (!got_path_is_absolute(target_path)) {
1307 char *abspath, *parent;
1308 err = got_path_dirname(&parent, ondisk_path);
1309 if (err)
1310 return err;
1311 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1312 free(parent);
1313 return got_error_from_errno("asprintf");
1315 free(parent);
1316 if (strlen(abspath) >= sizeof(canonpath)) {
1317 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1318 free(abspath);
1319 return err;
1321 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1322 free(abspath);
1323 if (err)
1324 return err;
1325 } else {
1326 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1327 if (err)
1328 return err;
1331 /* Only allow symlinks pointing at paths within the work tree. */
1332 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1333 *is_bad_symlink = 1;
1334 return NULL;
1337 /* Do not allow symlinks pointing into the .got directory. */
1338 if (asprintf(&path_got, "%s/%s", wtroot_path,
1339 GOT_WORKTREE_GOT_DIR) == -1)
1340 return got_error_from_errno("asprintf");
1341 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1342 *is_bad_symlink = 1;
1344 free(path_got);
1345 return NULL;
1348 static const struct got_error *
1349 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1350 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1351 int restoring_missing_file, int reverting_versioned_file,
1352 int path_is_unversioned, struct got_repository *repo,
1353 got_worktree_checkout_cb progress_cb, void *progress_arg)
1355 const struct got_error *err = NULL;
1356 char target_path[PATH_MAX];
1357 size_t len, target_len = 0;
1358 char *path_got = NULL;
1359 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1360 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1362 *is_bad_symlink = 0;
1365 * Blob object content specifies the target path of the link.
1366 * If a symbolic link cannot be installed we instead create
1367 * a regular file which contains the link target path stored
1368 * in the blob object.
1370 do {
1371 err = got_object_blob_read_block(&len, blob);
1372 if (len + target_len >= sizeof(target_path)) {
1373 /* Path too long; install as a regular file. */
1374 *is_bad_symlink = 1;
1375 got_object_blob_rewind(blob);
1376 return install_blob(worktree, ondisk_path, path,
1377 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1378 restoring_missing_file, reverting_versioned_file,
1379 1, path_is_unversioned, repo, progress_cb,
1380 progress_arg);
1382 if (len > 0) {
1383 /* Skip blob object header first time around. */
1384 memcpy(target_path + target_len, buf + hdrlen,
1385 len - hdrlen);
1386 target_len += len - hdrlen;
1387 hdrlen = 0;
1389 } while (len != 0);
1390 target_path[target_len] = '\0';
1392 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1393 ondisk_path, worktree->root_path);
1394 if (err)
1395 return err;
1397 if (*is_bad_symlink) {
1398 /* install as a regular file */
1399 *is_bad_symlink = 1;
1400 got_object_blob_rewind(blob);
1401 err = install_blob(worktree, ondisk_path, path,
1402 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1403 restoring_missing_file, reverting_versioned_file, 1,
1404 path_is_unversioned, repo, progress_cb, progress_arg);
1405 goto done;
1408 if (symlink(target_path, ondisk_path) == -1) {
1409 if (errno == EEXIST) {
1410 if (path_is_unversioned) {
1411 err = (*progress_cb)(progress_arg,
1412 GOT_STATUS_UNVERSIONED, path);
1413 goto done;
1415 err = replace_existing_symlink(ondisk_path,
1416 target_path, target_len);
1417 if (err)
1418 goto done;
1419 if (progress_cb) {
1420 err = (*progress_cb)(progress_arg,
1421 reverting_versioned_file ?
1422 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1423 path);
1425 goto done; /* Nothing else to do. */
1428 if (errno == ENOENT) {
1429 char *parent;
1430 err = got_path_dirname(&parent, ondisk_path);
1431 if (err)
1432 goto done;
1433 err = add_dir_on_disk(worktree, parent);
1434 free(parent);
1435 if (err)
1436 goto done;
1438 * Retry, and fall through to error handling
1439 * below if this second attempt fails.
1441 if (symlink(target_path, ondisk_path) != -1) {
1442 err = NULL; /* success */
1443 goto done;
1447 /* Handle errors from first or second creation attempt. */
1448 if (errno == ENAMETOOLONG) {
1449 /* bad target path; install as a regular file */
1450 *is_bad_symlink = 1;
1451 got_object_blob_rewind(blob);
1452 err = install_blob(worktree, ondisk_path, path,
1453 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1454 restoring_missing_file, reverting_versioned_file, 1,
1455 path_is_unversioned, repo,
1456 progress_cb, progress_arg);
1457 } else if (errno == ENOTDIR) {
1458 err = got_error_path(ondisk_path,
1459 GOT_ERR_FILE_OBSTRUCTED);
1460 } else {
1461 err = got_error_from_errno3("symlink",
1462 target_path, ondisk_path);
1464 } else if (progress_cb)
1465 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1466 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1467 done:
1468 free(path_got);
1469 return err;
1472 static const struct got_error *
1473 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1474 const char *path, mode_t te_mode, mode_t st_mode,
1475 struct got_blob_object *blob, int restoring_missing_file,
1476 int reverting_versioned_file, int installing_bad_symlink,
1477 int path_is_unversioned, struct got_repository *repo,
1478 got_worktree_checkout_cb progress_cb, void *progress_arg)
1480 const struct got_error *err = NULL;
1481 int fd = -1;
1482 size_t len, hdrlen;
1483 int update = 0;
1484 char *tmppath = NULL;
1486 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1487 GOT_DEFAULT_FILE_MODE);
1488 if (fd == -1) {
1489 if (errno == ENOENT) {
1490 char *parent;
1491 err = got_path_dirname(&parent, path);
1492 if (err)
1493 return err;
1494 err = add_dir_on_disk(worktree, parent);
1495 free(parent);
1496 if (err)
1497 return err;
1498 fd = open(ondisk_path,
1499 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1500 GOT_DEFAULT_FILE_MODE);
1501 if (fd == -1)
1502 return got_error_from_errno2("open",
1503 ondisk_path);
1504 } else if (errno == EEXIST) {
1505 if (path_is_unversioned) {
1506 err = (*progress_cb)(progress_arg,
1507 GOT_STATUS_UNVERSIONED, path);
1508 goto done;
1510 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1511 !S_ISREG(st_mode) && !installing_bad_symlink) {
1512 /* TODO file is obstructed; do something */
1513 err = got_error_path(ondisk_path,
1514 GOT_ERR_FILE_OBSTRUCTED);
1515 goto done;
1516 } else {
1517 err = got_opentemp_named_fd(&tmppath, &fd,
1518 ondisk_path);
1519 if (err)
1520 goto done;
1521 update = 1;
1523 } else
1524 return got_error_from_errno2("open", ondisk_path);
1527 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1528 err = got_error_from_errno2("fchmod",
1529 update ? tmppath : ondisk_path);
1530 goto done;
1533 if (progress_cb) {
1534 if (restoring_missing_file)
1535 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1536 path);
1537 else if (reverting_versioned_file)
1538 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1539 path);
1540 else
1541 err = (*progress_cb)(progress_arg,
1542 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1543 if (err)
1544 goto done;
1547 hdrlen = got_object_blob_get_hdrlen(blob);
1548 do {
1549 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1550 err = got_object_blob_read_block(&len, blob);
1551 if (err)
1552 break;
1553 if (len > 0) {
1554 /* Skip blob object header first time around. */
1555 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1556 if (outlen == -1) {
1557 err = got_error_from_errno("write");
1558 goto done;
1559 } else if (outlen != len - hdrlen) {
1560 err = got_error(GOT_ERR_IO);
1561 goto done;
1563 hdrlen = 0;
1565 } while (len != 0);
1567 if (fsync(fd) != 0) {
1568 err = got_error_from_errno("fsync");
1569 goto done;
1572 if (update) {
1573 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1574 err = got_error_from_errno2("unlink", ondisk_path);
1575 goto done;
1577 if (rename(tmppath, ondisk_path) != 0) {
1578 err = got_error_from_errno3("rename", tmppath,
1579 ondisk_path);
1580 goto done;
1582 free(tmppath);
1583 tmppath = NULL;
1586 done:
1587 if (fd != -1 && close(fd) == -1 && err == NULL)
1588 err = got_error_from_errno("close");
1589 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1590 err = got_error_from_errno2("unlink", tmppath);
1591 free(tmppath);
1592 return err;
1595 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1596 static const struct got_error *
1597 get_modified_file_content_status(unsigned char *status, FILE *f)
1599 const struct got_error *err = NULL;
1600 const char *markers[3] = {
1601 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1602 GOT_DIFF_CONFLICT_MARKER_SEP,
1603 GOT_DIFF_CONFLICT_MARKER_END
1605 int i = 0;
1606 char *line = NULL;
1607 size_t linesize = 0;
1608 ssize_t linelen;
1610 while (*status == GOT_STATUS_MODIFY) {
1611 linelen = getline(&line, &linesize, f);
1612 if (linelen == -1) {
1613 if (feof(f))
1614 break;
1615 err = got_ferror(f, GOT_ERR_IO);
1616 break;
1619 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1620 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1621 == 0)
1622 *status = GOT_STATUS_CONFLICT;
1623 else
1624 i++;
1627 free(line);
1629 return err;
1632 static int
1633 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1635 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1636 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1639 static int
1640 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1642 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1643 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1644 ie->mtime_sec == sb->st_mtim.tv_sec &&
1645 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1646 ie->size == (sb->st_size & 0xffffffff) &&
1647 !xbit_differs(ie, sb->st_mode));
1650 static unsigned char
1651 get_staged_status(struct got_fileindex_entry *ie)
1653 switch (got_fileindex_entry_stage_get(ie)) {
1654 case GOT_FILEIDX_STAGE_ADD:
1655 return GOT_STATUS_ADD;
1656 case GOT_FILEIDX_STAGE_DELETE:
1657 return GOT_STATUS_DELETE;
1658 case GOT_FILEIDX_STAGE_MODIFY:
1659 return GOT_STATUS_MODIFY;
1660 default:
1661 return GOT_STATUS_NO_CHANGE;
1665 static const struct got_error *
1666 get_symlink_modification_status(unsigned char *status,
1667 struct got_fileindex_entry *ie, const char *abspath,
1668 int dirfd, const char *de_name, struct got_blob_object *blob)
1670 const struct got_error *err = NULL;
1671 char target_path[PATH_MAX];
1672 char etarget[PATH_MAX];
1673 ssize_t elen;
1674 size_t len, target_len = 0;
1675 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1676 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1678 *status = GOT_STATUS_NO_CHANGE;
1680 /* Blob object content specifies the target path of the link. */
1681 do {
1682 err = got_object_blob_read_block(&len, blob);
1683 if (err)
1684 return err;
1685 if (len + target_len >= sizeof(target_path)) {
1687 * Should not happen. The blob contents were OK
1688 * when this symlink was installed.
1690 return got_error(GOT_ERR_NO_SPACE);
1692 if (len > 0) {
1693 /* Skip blob object header first time around. */
1694 memcpy(target_path + target_len, buf + hdrlen,
1695 len - hdrlen);
1696 target_len += len - hdrlen;
1697 hdrlen = 0;
1699 } while (len != 0);
1700 target_path[target_len] = '\0';
1702 if (dirfd != -1) {
1703 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1704 if (elen == -1)
1705 return got_error_from_errno2("readlinkat", abspath);
1706 } else {
1707 elen = readlink(abspath, etarget, sizeof(etarget));
1708 if (elen == -1)
1709 return got_error_from_errno2("readlink", abspath);
1712 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1713 *status = GOT_STATUS_MODIFY;
1715 return NULL;
1718 static const struct got_error *
1719 get_file_status(unsigned char *status, struct stat *sb,
1720 struct got_fileindex_entry *ie, const char *abspath,
1721 int dirfd, const char *de_name, struct got_repository *repo)
1723 const struct got_error *err = NULL;
1724 struct got_object_id id;
1725 size_t hdrlen;
1726 int fd = -1;
1727 FILE *f = NULL;
1728 uint8_t fbuf[8192];
1729 struct got_blob_object *blob = NULL;
1730 size_t flen, blen;
1731 unsigned char staged_status = get_staged_status(ie);
1733 *status = GOT_STATUS_NO_CHANGE;
1734 memset(sb, 0, sizeof(*sb));
1737 * Whenever the caller provides a directory descriptor and a
1738 * directory entry name for the file, use them! This prevents
1739 * race conditions if filesystem paths change beneath our feet.
1741 if (dirfd != -1) {
1742 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1743 if (errno == ENOENT) {
1744 if (got_fileindex_entry_has_file_on_disk(ie))
1745 *status = GOT_STATUS_MISSING;
1746 else
1747 *status = GOT_STATUS_DELETE;
1748 goto done;
1750 err = got_error_from_errno2("fstatat", abspath);
1751 goto done;
1753 } else {
1754 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1755 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1756 return got_error_from_errno2("open", abspath);
1757 else if (fd == -1 && errno == ELOOP) {
1758 if (lstat(abspath, sb) == -1)
1759 return got_error_from_errno2("lstat", abspath);
1760 } else if (fd == -1 || fstat(fd, sb) == -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("fstat", abspath);
1769 goto done;
1773 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1774 *status = GOT_STATUS_OBSTRUCTED;
1775 goto done;
1778 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1779 *status = GOT_STATUS_DELETE;
1780 goto done;
1781 } else if (!got_fileindex_entry_has_blob(ie) &&
1782 staged_status != GOT_STATUS_ADD) {
1783 *status = GOT_STATUS_ADD;
1784 goto done;
1787 if (!stat_info_differs(ie, sb))
1788 goto done;
1790 if (S_ISLNK(sb->st_mode) &&
1791 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1792 *status = GOT_STATUS_MODIFY;
1793 goto done;
1796 if (staged_status == GOT_STATUS_MODIFY ||
1797 staged_status == GOT_STATUS_ADD)
1798 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1799 else
1800 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1802 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1803 if (err)
1804 goto done;
1806 if (S_ISLNK(sb->st_mode)) {
1807 err = get_symlink_modification_status(status, ie,
1808 abspath, dirfd, de_name, blob);
1809 goto done;
1812 if (dirfd != -1) {
1813 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1814 if (fd == -1) {
1815 err = got_error_from_errno2("openat", abspath);
1816 goto done;
1820 f = fdopen(fd, "r");
1821 if (f == NULL) {
1822 err = got_error_from_errno2("fdopen", abspath);
1823 goto done;
1825 fd = -1;
1826 hdrlen = got_object_blob_get_hdrlen(blob);
1827 for (;;) {
1828 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1829 err = got_object_blob_read_block(&blen, blob);
1830 if (err)
1831 goto done;
1832 /* Skip length of blob object header first time around. */
1833 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1834 if (flen == 0 && ferror(f)) {
1835 err = got_error_from_errno("fread");
1836 goto done;
1838 if (blen - hdrlen == 0) {
1839 if (flen != 0)
1840 *status = GOT_STATUS_MODIFY;
1841 break;
1842 } else if (flen == 0) {
1843 if (blen - hdrlen != 0)
1844 *status = GOT_STATUS_MODIFY;
1845 break;
1846 } else if (blen - hdrlen == flen) {
1847 /* Skip blob object header first time around. */
1848 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1849 *status = GOT_STATUS_MODIFY;
1850 break;
1852 } else {
1853 *status = GOT_STATUS_MODIFY;
1854 break;
1856 hdrlen = 0;
1859 if (*status == GOT_STATUS_MODIFY) {
1860 rewind(f);
1861 err = get_modified_file_content_status(status, f);
1862 } else if (xbit_differs(ie, sb->st_mode))
1863 *status = GOT_STATUS_MODE_CHANGE;
1864 done:
1865 if (blob)
1866 got_object_blob_close(blob);
1867 if (f != NULL && fclose(f) == EOF && err == NULL)
1868 err = got_error_from_errno2("fclose", abspath);
1869 if (fd != -1 && close(fd) == -1 && err == NULL)
1870 err = got_error_from_errno2("close", abspath);
1871 return err;
1875 * Update timestamps in the file index if a file is unmodified and
1876 * we had to run a full content comparison to find out.
1878 static const struct got_error *
1879 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1880 struct got_fileindex_entry *ie, struct stat *sb)
1882 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1883 return got_fileindex_entry_update(ie, wt_fd, path,
1884 ie->blob_sha1, ie->commit_sha1, 1);
1886 return NULL;
1889 static const struct got_error *
1890 update_blob(struct got_worktree *worktree,
1891 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1892 struct got_tree_entry *te, const char *path,
1893 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1894 void *progress_arg)
1896 const struct got_error *err = NULL;
1897 struct got_blob_object *blob = NULL;
1898 char *ondisk_path;
1899 unsigned char status = GOT_STATUS_NO_CHANGE;
1900 struct stat sb;
1902 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1903 return got_error_from_errno("asprintf");
1905 if (ie) {
1906 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1907 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1908 goto done;
1910 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1911 repo);
1912 if (err)
1913 goto done;
1914 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1915 sb.st_mode = got_fileindex_perms_to_st(ie);
1916 } else {
1917 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1918 status = GOT_STATUS_UNVERSIONED;
1921 if (status == GOT_STATUS_OBSTRUCTED) {
1922 err = (*progress_cb)(progress_arg, status, path);
1923 goto done;
1925 if (status == GOT_STATUS_CONFLICT) {
1926 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1927 path);
1928 goto done;
1931 if (ie && status != GOT_STATUS_MISSING &&
1932 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1933 if (got_fileindex_entry_has_commit(ie) &&
1934 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1935 SHA1_DIGEST_LENGTH) == 0) {
1936 err = sync_timestamps(worktree->root_fd,
1937 path, status, ie, &sb);
1938 if (err)
1939 goto done;
1940 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1941 path);
1942 goto done;
1944 if (got_fileindex_entry_has_blob(ie) &&
1945 memcmp(ie->blob_sha1, te->id.sha1,
1946 SHA1_DIGEST_LENGTH) == 0) {
1947 err = sync_timestamps(worktree->root_fd,
1948 path, status, ie, &sb);
1949 goto done;
1953 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1954 if (err)
1955 goto done;
1957 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1958 int update_timestamps;
1959 struct got_blob_object *blob2 = NULL;
1960 char *label_orig = NULL;
1961 if (got_fileindex_entry_has_blob(ie)) {
1962 struct got_object_id id2;
1963 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1964 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1965 if (err)
1966 goto done;
1968 if (got_fileindex_entry_has_commit(ie)) {
1969 char id_str[SHA1_DIGEST_STRING_LENGTH];
1970 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1971 sizeof(id_str)) == NULL) {
1972 err = got_error_path(id_str,
1973 GOT_ERR_BAD_OBJ_ID_STR);
1974 goto done;
1976 if (asprintf(&label_orig, "%s: commit %s",
1977 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1978 err = got_error_from_errno("asprintf");
1979 goto done;
1982 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1983 char *link_target;
1984 err = got_object_blob_read_to_str(&link_target, blob);
1985 if (err)
1986 goto done;
1987 err = merge_symlink(worktree, blob2, ondisk_path, path,
1988 label_orig, link_target, worktree->base_commit_id,
1989 repo, progress_cb, progress_arg);
1990 free(link_target);
1991 } else {
1992 err = merge_blob(&update_timestamps, worktree, blob2,
1993 ondisk_path, path, sb.st_mode, label_orig, blob,
1994 worktree->base_commit_id, repo,
1995 progress_cb, progress_arg);
1997 free(label_orig);
1998 if (blob2)
1999 got_object_blob_close(blob2);
2000 if (err)
2001 goto done;
2003 * Do not update timestamps of files with local changes.
2004 * Otherwise, a future status walk would treat them as
2005 * unmodified files again.
2007 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2008 blob->id.sha1, worktree->base_commit_id->sha1,
2009 update_timestamps);
2010 } else if (status == GOT_STATUS_MODE_CHANGE) {
2011 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2012 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2013 } else if (status == GOT_STATUS_DELETE) {
2014 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2015 if (err)
2016 goto done;
2017 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2018 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2019 if (err)
2020 goto done;
2021 } else {
2022 int is_bad_symlink = 0;
2023 if (S_ISLNK(te->mode)) {
2024 err = install_symlink(&is_bad_symlink, worktree,
2025 ondisk_path, path, blob,
2026 status == GOT_STATUS_MISSING, 0,
2027 status == GOT_STATUS_UNVERSIONED, repo,
2028 progress_cb, progress_arg);
2029 } else {
2030 err = install_blob(worktree, ondisk_path, path,
2031 te->mode, sb.st_mode, blob,
2032 status == GOT_STATUS_MISSING, 0, 0,
2033 status == GOT_STATUS_UNVERSIONED, repo,
2034 progress_cb, progress_arg);
2036 if (err)
2037 goto done;
2039 if (ie) {
2040 err = got_fileindex_entry_update(ie,
2041 worktree->root_fd, path, blob->id.sha1,
2042 worktree->base_commit_id->sha1, 1);
2043 } else {
2044 err = create_fileindex_entry(&ie, fileindex,
2045 worktree->base_commit_id, worktree->root_fd, path,
2046 &blob->id);
2048 if (err)
2049 goto done;
2051 if (is_bad_symlink) {
2052 got_fileindex_entry_filetype_set(ie,
2053 GOT_FILEIDX_MODE_BAD_SYMLINK);
2056 got_object_blob_close(blob);
2057 done:
2058 free(ondisk_path);
2059 return err;
2062 static const struct got_error *
2063 remove_ondisk_file(const char *root_path, const char *path)
2065 const struct got_error *err = NULL;
2066 char *ondisk_path = NULL;
2068 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2069 return got_error_from_errno("asprintf");
2071 if (unlink(ondisk_path) == -1) {
2072 if (errno != ENOENT)
2073 err = got_error_from_errno2("unlink", ondisk_path);
2074 } else {
2075 size_t root_len = strlen(root_path);
2076 do {
2077 char *parent;
2078 err = got_path_dirname(&parent, ondisk_path);
2079 if (err)
2080 break;
2081 free(ondisk_path);
2082 ondisk_path = parent;
2083 if (rmdir(ondisk_path) == -1) {
2084 if (errno != ENOTEMPTY)
2085 err = got_error_from_errno2("rmdir",
2086 ondisk_path);
2087 break;
2089 } while (got_path_cmp(ondisk_path, root_path,
2090 strlen(ondisk_path), root_len) != 0);
2092 free(ondisk_path);
2093 return err;
2096 static const struct got_error *
2097 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2098 struct got_fileindex_entry *ie, struct got_repository *repo,
2099 got_worktree_checkout_cb progress_cb, void *progress_arg)
2101 const struct got_error *err = NULL;
2102 unsigned char status;
2103 struct stat sb;
2104 char *ondisk_path;
2106 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2107 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2109 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2110 == -1)
2111 return got_error_from_errno("asprintf");
2113 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2114 if (err)
2115 goto done;
2117 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2118 char ondisk_target[PATH_MAX];
2119 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2120 sizeof(ondisk_target));
2121 if (ondisk_len == -1) {
2122 err = got_error_from_errno2("readlink", ondisk_path);
2123 goto done;
2125 ondisk_target[ondisk_len] = '\0';
2126 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2127 NULL, NULL, /* XXX pass common ancestor info? */
2128 ondisk_target, ondisk_path);
2129 if (err)
2130 goto done;
2131 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2132 ie->path);
2133 goto done;
2136 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2137 status == GOT_STATUS_ADD) {
2138 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2139 if (err)
2140 goto done;
2142 * Preserve the working file and change the deleted blob's
2143 * entry into a schedule-add entry.
2145 err = got_fileindex_entry_update(ie, worktree->root_fd,
2146 ie->path, NULL, NULL, 0);
2147 } else {
2148 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2149 if (err)
2150 goto done;
2151 if (status == GOT_STATUS_NO_CHANGE) {
2152 err = remove_ondisk_file(worktree->root_path, ie->path);
2153 if (err)
2154 goto done;
2156 got_fileindex_entry_remove(fileindex, ie);
2158 done:
2159 free(ondisk_path);
2160 return err;
2163 struct diff_cb_arg {
2164 struct got_fileindex *fileindex;
2165 struct got_worktree *worktree;
2166 struct got_repository *repo;
2167 got_worktree_checkout_cb progress_cb;
2168 void *progress_arg;
2169 got_cancel_cb cancel_cb;
2170 void *cancel_arg;
2173 static const struct got_error *
2174 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2175 struct got_tree_entry *te, const char *parent_path)
2177 struct diff_cb_arg *a = arg;
2179 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2180 return got_error(GOT_ERR_CANCELLED);
2182 return update_blob(a->worktree, a->fileindex, ie, te,
2183 ie->path, a->repo, a->progress_cb, a->progress_arg);
2186 static const struct got_error *
2187 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2189 struct diff_cb_arg *a = arg;
2191 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2192 return got_error(GOT_ERR_CANCELLED);
2194 return delete_blob(a->worktree, a->fileindex, ie,
2195 a->repo, a->progress_cb, a->progress_arg);
2198 static const struct got_error *
2199 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2201 struct diff_cb_arg *a = arg;
2202 const struct got_error *err;
2203 char *path;
2205 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2206 return got_error(GOT_ERR_CANCELLED);
2208 if (got_object_tree_entry_is_submodule(te))
2209 return NULL;
2211 if (asprintf(&path, "%s%s%s", parent_path,
2212 parent_path[0] ? "/" : "", te->name)
2213 == -1)
2214 return got_error_from_errno("asprintf");
2216 if (S_ISDIR(te->mode))
2217 err = add_dir_on_disk(a->worktree, path);
2218 else
2219 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2220 a->repo, a->progress_cb, a->progress_arg);
2222 free(path);
2223 return err;
2226 const struct got_error *
2227 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2229 uint32_t uuid_status;
2231 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2232 if (uuid_status != uuid_s_ok) {
2233 *uuidstr = NULL;
2234 return got_error_uuid(uuid_status, "uuid_to_string");
2237 return NULL;
2240 static const struct got_error *
2241 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2243 const struct got_error *err = NULL;
2244 char *uuidstr = NULL;
2246 *refname = NULL;
2248 err = got_worktree_get_uuid(&uuidstr, worktree);
2249 if (err)
2250 return err;
2252 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2253 err = got_error_from_errno("asprintf");
2254 *refname = NULL;
2256 free(uuidstr);
2257 return err;
2260 const struct got_error *
2261 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2263 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2266 static const struct got_error *
2267 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2269 return get_ref_name(refname, worktree,
2270 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2273 static const struct got_error *
2274 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2276 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2279 static const struct got_error *
2280 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2282 return get_ref_name(refname, worktree,
2283 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2286 static const struct got_error *
2287 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2289 return get_ref_name(refname, worktree,
2290 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2293 static const struct got_error *
2294 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2296 return get_ref_name(refname, worktree,
2297 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2300 static const struct got_error *
2301 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2303 return get_ref_name(refname, worktree,
2304 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2307 static const struct got_error *
2308 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2310 return get_ref_name(refname, worktree,
2311 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2314 static const struct got_error *
2315 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2317 return get_ref_name(refname, worktree,
2318 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2321 const struct got_error *
2322 got_worktree_get_histedit_script_path(char **path,
2323 struct got_worktree *worktree)
2325 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2326 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2327 *path = NULL;
2328 return got_error_from_errno("asprintf");
2330 return NULL;
2334 * Prevent Git's garbage collector from deleting our base commit by
2335 * setting a reference to our base commit's ID.
2337 static const struct got_error *
2338 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2340 const struct got_error *err = NULL;
2341 struct got_reference *ref = NULL;
2342 char *refname;
2344 err = got_worktree_get_base_ref_name(&refname, worktree);
2345 if (err)
2346 return err;
2348 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2349 if (err)
2350 goto done;
2352 err = got_ref_write(ref, repo);
2353 done:
2354 free(refname);
2355 if (ref)
2356 got_ref_close(ref);
2357 return err;
2360 static const struct got_error *
2361 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2363 const struct got_error *err = NULL;
2365 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2366 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2367 err = got_error_from_errno("asprintf");
2368 *fileindex_path = NULL;
2370 return err;
2374 static const struct got_error *
2375 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2376 struct got_worktree *worktree)
2378 const struct got_error *err = NULL;
2379 FILE *index = NULL;
2381 *fileindex_path = NULL;
2382 *fileindex = got_fileindex_alloc();
2383 if (*fileindex == NULL)
2384 return got_error_from_errno("got_fileindex_alloc");
2386 err = get_fileindex_path(fileindex_path, worktree);
2387 if (err)
2388 goto done;
2390 index = fopen(*fileindex_path, "rb");
2391 if (index == NULL) {
2392 if (errno != ENOENT)
2393 err = got_error_from_errno2("fopen", *fileindex_path);
2394 } else {
2395 err = got_fileindex_read(*fileindex, index);
2396 if (fclose(index) == EOF && err == NULL)
2397 err = got_error_from_errno("fclose");
2399 done:
2400 if (err) {
2401 free(*fileindex_path);
2402 *fileindex_path = NULL;
2403 got_fileindex_free(*fileindex);
2404 *fileindex = NULL;
2406 return err;
2409 struct bump_base_commit_id_arg {
2410 struct got_object_id *base_commit_id;
2411 const char *path;
2412 size_t path_len;
2413 const char *entry_name;
2414 got_worktree_checkout_cb progress_cb;
2415 void *progress_arg;
2418 /* Bump base commit ID of all files within an updated part of the work tree. */
2419 static const struct got_error *
2420 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2422 const struct got_error *err;
2423 struct bump_base_commit_id_arg *a = arg;
2425 if (a->entry_name) {
2426 if (strcmp(ie->path, a->path) != 0)
2427 return NULL;
2428 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2429 return NULL;
2431 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2432 SHA1_DIGEST_LENGTH) == 0)
2433 return NULL;
2435 if (a->progress_cb) {
2436 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2437 ie->path);
2438 if (err)
2439 return err;
2441 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2442 return NULL;
2445 static const struct got_error *
2446 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2447 struct got_fileindex *fileindex,
2448 got_worktree_checkout_cb progress_cb, void *progress_arg)
2450 struct bump_base_commit_id_arg bbc_arg;
2452 bbc_arg.base_commit_id = worktree->base_commit_id;
2453 bbc_arg.entry_name = NULL;
2454 bbc_arg.path = "";
2455 bbc_arg.path_len = 0;
2456 bbc_arg.progress_cb = progress_cb;
2457 bbc_arg.progress_arg = progress_arg;
2459 return got_fileindex_for_each_entry_safe(fileindex,
2460 bump_base_commit_id, &bbc_arg);
2463 static const struct got_error *
2464 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2466 const struct got_error *err = NULL;
2467 char *new_fileindex_path = NULL;
2468 FILE *new_index = NULL;
2469 struct timespec timeout;
2471 err = got_opentemp_named(&new_fileindex_path, &new_index,
2472 fileindex_path);
2473 if (err)
2474 goto done;
2476 err = got_fileindex_write(fileindex, new_index);
2477 if (err)
2478 goto done;
2480 if (rename(new_fileindex_path, fileindex_path) != 0) {
2481 err = got_error_from_errno3("rename", new_fileindex_path,
2482 fileindex_path);
2483 unlink(new_fileindex_path);
2487 * Sleep for a short amount of time to ensure that files modified after
2488 * this program exits have a different time stamp from the one which
2489 * was recorded in the file index.
2491 timeout.tv_sec = 0;
2492 timeout.tv_nsec = 1;
2493 nanosleep(&timeout, NULL);
2494 done:
2495 if (new_index)
2496 fclose(new_index);
2497 free(new_fileindex_path);
2498 return err;
2501 static const struct got_error *
2502 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2503 struct got_object_id **tree_id, const char *wt_relpath,
2504 struct got_worktree *worktree, struct got_repository *repo)
2506 const struct got_error *err = NULL;
2507 struct got_object_id *id = NULL;
2508 char *in_repo_path = NULL;
2509 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2511 *entry_type = GOT_OBJ_TYPE_ANY;
2512 *tree_relpath = NULL;
2513 *tree_id = NULL;
2515 if (wt_relpath[0] == '\0') {
2516 /* Check out all files within the work tree. */
2517 *entry_type = GOT_OBJ_TYPE_TREE;
2518 *tree_relpath = strdup("");
2519 if (*tree_relpath == NULL) {
2520 err = got_error_from_errno("strdup");
2521 goto done;
2523 err = got_object_id_by_path(tree_id, repo,
2524 worktree->base_commit_id, worktree->path_prefix);
2525 if (err)
2526 goto done;
2527 return NULL;
2530 /* Check out a subset of files in the work tree. */
2532 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2533 is_root_wt ? "" : "/", wt_relpath) == -1) {
2534 err = got_error_from_errno("asprintf");
2535 goto done;
2538 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2539 in_repo_path);
2540 if (err)
2541 goto done;
2543 free(in_repo_path);
2544 in_repo_path = NULL;
2546 err = got_object_get_type(entry_type, repo, id);
2547 if (err)
2548 goto done;
2550 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2551 /* Check out a single file. */
2552 if (strchr(wt_relpath, '/') == NULL) {
2553 /* Check out a single file in work tree's root dir. */
2554 in_repo_path = strdup(worktree->path_prefix);
2555 if (in_repo_path == NULL) {
2556 err = got_error_from_errno("strdup");
2557 goto done;
2559 *tree_relpath = strdup("");
2560 if (*tree_relpath == NULL) {
2561 err = got_error_from_errno("strdup");
2562 goto done;
2564 } else {
2565 /* Check out a single file in a subdirectory. */
2566 err = got_path_dirname(tree_relpath, wt_relpath);
2567 if (err)
2568 return err;
2569 if (asprintf(&in_repo_path, "%s%s%s",
2570 worktree->path_prefix, is_root_wt ? "" : "/",
2571 *tree_relpath) == -1) {
2572 err = got_error_from_errno("asprintf");
2573 goto done;
2576 err = got_object_id_by_path(tree_id, repo,
2577 worktree->base_commit_id, in_repo_path);
2578 } else {
2579 /* Check out all files within a subdirectory. */
2580 *tree_id = got_object_id_dup(id);
2581 if (*tree_id == NULL) {
2582 err = got_error_from_errno("got_object_id_dup");
2583 goto done;
2585 *tree_relpath = strdup(wt_relpath);
2586 if (*tree_relpath == NULL) {
2587 err = got_error_from_errno("strdup");
2588 goto done;
2591 done:
2592 free(id);
2593 free(in_repo_path);
2594 if (err) {
2595 *entry_type = GOT_OBJ_TYPE_ANY;
2596 free(*tree_relpath);
2597 *tree_relpath = NULL;
2598 free(*tree_id);
2599 *tree_id = NULL;
2601 return err;
2604 static const struct got_error *
2605 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2606 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2607 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2608 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2610 const struct got_error *err = NULL;
2611 struct got_commit_object *commit = NULL;
2612 struct got_tree_object *tree = NULL;
2613 struct got_fileindex_diff_tree_cb diff_cb;
2614 struct diff_cb_arg arg;
2616 err = ref_base_commit(worktree, repo);
2617 if (err) {
2618 if (!(err->code == GOT_ERR_ERRNO &&
2619 (errno == EACCES || errno == EROFS)))
2620 goto done;
2621 err = (*progress_cb)(progress_arg,
2622 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2623 if (err)
2624 return err;
2627 err = got_object_open_as_commit(&commit, repo,
2628 worktree->base_commit_id);
2629 if (err)
2630 goto done;
2632 err = got_object_open_as_tree(&tree, repo, tree_id);
2633 if (err)
2634 goto done;
2636 if (entry_name &&
2637 got_object_tree_find_entry(tree, entry_name) == NULL) {
2638 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2639 goto done;
2642 diff_cb.diff_old_new = diff_old_new;
2643 diff_cb.diff_old = diff_old;
2644 diff_cb.diff_new = diff_new;
2645 arg.fileindex = fileindex;
2646 arg.worktree = worktree;
2647 arg.repo = repo;
2648 arg.progress_cb = progress_cb;
2649 arg.progress_arg = progress_arg;
2650 arg.cancel_cb = cancel_cb;
2651 arg.cancel_arg = cancel_arg;
2652 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2653 entry_name, repo, &diff_cb, &arg);
2654 done:
2655 if (tree)
2656 got_object_tree_close(tree);
2657 if (commit)
2658 got_object_commit_close(commit);
2659 return err;
2662 const struct got_error *
2663 got_worktree_checkout_files(struct got_worktree *worktree,
2664 struct got_pathlist_head *paths, struct got_repository *repo,
2665 got_worktree_checkout_cb progress_cb, void *progress_arg,
2666 got_cancel_cb cancel_cb, void *cancel_arg)
2668 const struct got_error *err = NULL, *sync_err, *unlockerr;
2669 struct got_commit_object *commit = NULL;
2670 struct got_tree_object *tree = NULL;
2671 struct got_fileindex *fileindex = NULL;
2672 char *fileindex_path = NULL;
2673 struct got_pathlist_entry *pe;
2674 struct tree_path_data {
2675 SIMPLEQ_ENTRY(tree_path_data) entry;
2676 struct got_object_id *tree_id;
2677 int entry_type;
2678 char *relpath;
2679 char *entry_name;
2680 } *tpd = NULL;
2681 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2683 SIMPLEQ_INIT(&tree_paths);
2685 err = lock_worktree(worktree, LOCK_EX);
2686 if (err)
2687 return err;
2689 /* Map all specified paths to in-repository trees. */
2690 TAILQ_FOREACH(pe, paths, entry) {
2691 tpd = malloc(sizeof(*tpd));
2692 if (tpd == NULL) {
2693 err = got_error_from_errno("malloc");
2694 goto done;
2697 err = find_tree_entry_for_checkout(&tpd->entry_type,
2698 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2699 if (err) {
2700 free(tpd);
2701 goto done;
2704 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2705 err = got_path_basename(&tpd->entry_name, pe->path);
2706 if (err) {
2707 free(tpd->relpath);
2708 free(tpd->tree_id);
2709 free(tpd);
2710 goto done;
2712 } else
2713 tpd->entry_name = NULL;
2715 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2719 * Read the file index.
2720 * Checking out files is supposed to be an idempotent operation.
2721 * If the on-disk file index is incomplete we will try to complete it.
2723 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2724 if (err)
2725 goto done;
2727 tpd = SIMPLEQ_FIRST(&tree_paths);
2728 TAILQ_FOREACH(pe, paths, entry) {
2729 struct bump_base_commit_id_arg bbc_arg;
2731 err = checkout_files(worktree, fileindex, tpd->relpath,
2732 tpd->tree_id, tpd->entry_name, repo,
2733 progress_cb, progress_arg, cancel_cb, cancel_arg);
2734 if (err)
2735 break;
2737 bbc_arg.base_commit_id = worktree->base_commit_id;
2738 bbc_arg.entry_name = tpd->entry_name;
2739 bbc_arg.path = pe->path;
2740 bbc_arg.path_len = pe->path_len;
2741 bbc_arg.progress_cb = progress_cb;
2742 bbc_arg.progress_arg = progress_arg;
2743 err = got_fileindex_for_each_entry_safe(fileindex,
2744 bump_base_commit_id, &bbc_arg);
2745 if (err)
2746 break;
2748 tpd = SIMPLEQ_NEXT(tpd, entry);
2750 sync_err = sync_fileindex(fileindex, fileindex_path);
2751 if (sync_err && err == NULL)
2752 err = sync_err;
2753 done:
2754 free(fileindex_path);
2755 if (tree)
2756 got_object_tree_close(tree);
2757 if (commit)
2758 got_object_commit_close(commit);
2759 if (fileindex)
2760 got_fileindex_free(fileindex);
2761 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2762 tpd = SIMPLEQ_FIRST(&tree_paths);
2763 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2764 free(tpd->relpath);
2765 free(tpd->tree_id);
2766 free(tpd);
2768 unlockerr = lock_worktree(worktree, LOCK_SH);
2769 if (unlockerr && err == NULL)
2770 err = unlockerr;
2771 return err;
2774 struct merge_file_cb_arg {
2775 struct got_worktree *worktree;
2776 struct got_fileindex *fileindex;
2777 got_worktree_checkout_cb progress_cb;
2778 void *progress_arg;
2779 got_cancel_cb cancel_cb;
2780 void *cancel_arg;
2781 const char *label_orig;
2782 struct got_object_id *commit_id2;
2785 static const struct got_error *
2786 merge_file_cb(void *arg, struct got_blob_object *blob1,
2787 struct got_blob_object *blob2, struct got_object_id *id1,
2788 struct got_object_id *id2, const char *path1, const char *path2,
2789 mode_t mode1, mode_t mode2, struct got_repository *repo)
2791 static const struct got_error *err = NULL;
2792 struct merge_file_cb_arg *a = arg;
2793 struct got_fileindex_entry *ie;
2794 char *ondisk_path = NULL;
2795 struct stat sb;
2796 unsigned char status;
2797 int local_changes_subsumed;
2799 if (blob1 && blob2) {
2800 ie = got_fileindex_entry_get(a->fileindex, path2,
2801 strlen(path2));
2802 if (ie == NULL)
2803 return (*a->progress_cb)(a->progress_arg,
2804 GOT_STATUS_MISSING, path2);
2806 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2807 path2) == -1)
2808 return got_error_from_errno("asprintf");
2810 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2811 repo);
2812 if (err)
2813 goto done;
2815 if (status == GOT_STATUS_DELETE) {
2816 err = (*a->progress_cb)(a->progress_arg,
2817 GOT_STATUS_MERGE, path2);
2818 goto done;
2820 if (status != GOT_STATUS_NO_CHANGE &&
2821 status != GOT_STATUS_MODIFY &&
2822 status != GOT_STATUS_CONFLICT &&
2823 status != GOT_STATUS_ADD) {
2824 err = (*a->progress_cb)(a->progress_arg, status, path2);
2825 goto done;
2828 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2829 char *link_target2;
2830 err = got_object_blob_read_to_str(&link_target2, blob2);
2831 if (err)
2832 goto done;
2833 err = merge_symlink(a->worktree, blob1, ondisk_path,
2834 path2, a->label_orig, link_target2, a->commit_id2,
2835 repo, a->progress_cb, a->progress_arg);
2836 free(link_target2);
2837 } else {
2838 err = merge_blob(&local_changes_subsumed, a->worktree,
2839 blob1, ondisk_path, path2, sb.st_mode,
2840 a->label_orig, blob2, a->commit_id2, repo,
2841 a->progress_cb, a->progress_arg);
2843 } else if (blob1) {
2844 ie = got_fileindex_entry_get(a->fileindex, path1,
2845 strlen(path1));
2846 if (ie == NULL)
2847 return (*a->progress_cb)(a->progress_arg,
2848 GOT_STATUS_MISSING, path1);
2850 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2851 path1) == -1)
2852 return got_error_from_errno("asprintf");
2854 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2855 repo);
2856 if (err)
2857 goto done;
2859 switch (status) {
2860 case GOT_STATUS_NO_CHANGE:
2861 err = (*a->progress_cb)(a->progress_arg,
2862 GOT_STATUS_DELETE, path1);
2863 if (err)
2864 goto done;
2865 err = remove_ondisk_file(a->worktree->root_path, path1);
2866 if (err)
2867 goto done;
2868 if (ie)
2869 got_fileindex_entry_mark_deleted_from_disk(ie);
2870 break;
2871 case GOT_STATUS_DELETE:
2872 case GOT_STATUS_MISSING:
2873 err = (*a->progress_cb)(a->progress_arg,
2874 GOT_STATUS_DELETE, path1);
2875 if (err)
2876 goto done;
2877 if (ie)
2878 got_fileindex_entry_mark_deleted_from_disk(ie);
2879 break;
2880 case GOT_STATUS_ADD: {
2881 struct got_object_id *id;
2882 FILE *blob1_f;
2884 * Delete the added file only if its content already
2885 * exists in the repository.
2887 err = got_object_blob_file_create(&id, &blob1_f, path1);
2888 if (err)
2889 goto done;
2890 if (got_object_id_cmp(id, id1) == 0) {
2891 err = (*a->progress_cb)(a->progress_arg,
2892 GOT_STATUS_DELETE, path1);
2893 if (err)
2894 goto done;
2895 err = remove_ondisk_file(a->worktree->root_path,
2896 path1);
2897 if (err)
2898 goto done;
2899 if (ie)
2900 got_fileindex_entry_remove(a->fileindex,
2901 ie);
2902 } else {
2903 err = (*a->progress_cb)(a->progress_arg,
2904 GOT_STATUS_CANNOT_DELETE, path1);
2906 if (fclose(blob1_f) == EOF && err == NULL)
2907 err = got_error_from_errno("fclose");
2908 free(id);
2909 if (err)
2910 goto done;
2911 break;
2913 case GOT_STATUS_MODIFY:
2914 case GOT_STATUS_CONFLICT:
2915 err = (*a->progress_cb)(a->progress_arg,
2916 GOT_STATUS_CANNOT_DELETE, path1);
2917 if (err)
2918 goto done;
2919 break;
2920 case GOT_STATUS_OBSTRUCTED:
2921 err = (*a->progress_cb)(a->progress_arg, status, path1);
2922 if (err)
2923 goto done;
2924 break;
2925 default:
2926 break;
2928 } else if (blob2) {
2929 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2930 path2) == -1)
2931 return got_error_from_errno("asprintf");
2932 ie = got_fileindex_entry_get(a->fileindex, path2,
2933 strlen(path2));
2934 if (ie) {
2935 err = get_file_status(&status, &sb, ie, ondisk_path,
2936 -1, NULL, repo);
2937 if (err)
2938 goto done;
2939 if (status != GOT_STATUS_NO_CHANGE &&
2940 status != GOT_STATUS_MODIFY &&
2941 status != GOT_STATUS_CONFLICT &&
2942 status != GOT_STATUS_ADD) {
2943 err = (*a->progress_cb)(a->progress_arg,
2944 status, path2);
2945 goto done;
2947 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2948 char *link_target2;
2949 err = got_object_blob_read_to_str(&link_target2,
2950 blob2);
2951 if (err)
2952 goto done;
2953 err = merge_symlink(a->worktree, NULL,
2954 ondisk_path, path2, a->label_orig,
2955 link_target2, a->commit_id2, repo,
2956 a->progress_cb, a->progress_arg);
2957 free(link_target2);
2958 } else if (S_ISREG(sb.st_mode)) {
2959 err = merge_blob(&local_changes_subsumed,
2960 a->worktree, NULL, ondisk_path, path2,
2961 sb.st_mode, a->label_orig, blob2,
2962 a->commit_id2, repo, a->progress_cb,
2963 a->progress_arg);
2964 } else {
2965 err = got_error_path(ondisk_path,
2966 GOT_ERR_FILE_OBSTRUCTED);
2968 if (err)
2969 goto done;
2970 if (status == GOT_STATUS_DELETE) {
2971 err = got_fileindex_entry_update(ie,
2972 a->worktree->root_fd, path2, blob2->id.sha1,
2973 a->worktree->base_commit_id->sha1, 0);
2974 if (err)
2975 goto done;
2977 } else {
2978 int is_bad_symlink = 0;
2979 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2980 if (S_ISLNK(mode2)) {
2981 err = install_symlink(&is_bad_symlink,
2982 a->worktree, ondisk_path, path2, blob2, 0,
2983 0, 1, repo, a->progress_cb, a->progress_arg);
2984 } else {
2985 err = install_blob(a->worktree, ondisk_path, path2,
2986 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2987 a->progress_cb, a->progress_arg);
2989 if (err)
2990 goto done;
2991 err = got_fileindex_entry_alloc(&ie, path2);
2992 if (err)
2993 goto done;
2994 err = got_fileindex_entry_update(ie,
2995 a->worktree->root_fd, path2, NULL, NULL, 1);
2996 if (err) {
2997 got_fileindex_entry_free(ie);
2998 goto done;
3000 err = got_fileindex_entry_add(a->fileindex, ie);
3001 if (err) {
3002 got_fileindex_entry_free(ie);
3003 goto done;
3005 if (is_bad_symlink) {
3006 got_fileindex_entry_filetype_set(ie,
3007 GOT_FILEIDX_MODE_BAD_SYMLINK);
3011 done:
3012 free(ondisk_path);
3013 return err;
3016 struct check_merge_ok_arg {
3017 struct got_worktree *worktree;
3018 struct got_repository *repo;
3021 static const struct got_error *
3022 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3024 const struct got_error *err = NULL;
3025 struct check_merge_ok_arg *a = arg;
3026 unsigned char status;
3027 struct stat sb;
3028 char *ondisk_path;
3030 /* Reject merges into a work tree with mixed base commits. */
3031 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3032 SHA1_DIGEST_LENGTH))
3033 return got_error(GOT_ERR_MIXED_COMMITS);
3035 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3036 == -1)
3037 return got_error_from_errno("asprintf");
3039 /* Reject merges into a work tree with conflicted files. */
3040 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3041 if (err)
3042 return err;
3043 if (status == GOT_STATUS_CONFLICT)
3044 return got_error(GOT_ERR_CONFLICTS);
3046 return NULL;
3049 static const struct got_error *
3050 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3051 const char *fileindex_path, struct got_object_id *commit_id1,
3052 struct got_object_id *commit_id2, struct got_repository *repo,
3053 got_worktree_checkout_cb progress_cb, void *progress_arg,
3054 got_cancel_cb cancel_cb, void *cancel_arg)
3056 const struct got_error *err = NULL, *sync_err;
3057 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3058 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3059 struct merge_file_cb_arg arg;
3060 char *label_orig = NULL;
3062 if (commit_id1) {
3063 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3064 worktree->path_prefix);
3065 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3066 goto done;
3068 if (tree_id1) {
3069 char *id_str;
3071 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3072 if (err)
3073 goto done;
3075 err = got_object_id_str(&id_str, commit_id1);
3076 if (err)
3077 goto done;
3079 if (asprintf(&label_orig, "%s: commit %s",
3080 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3081 err = got_error_from_errno("asprintf");
3082 free(id_str);
3083 goto done;
3085 free(id_str);
3088 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3089 worktree->path_prefix);
3090 if (err)
3091 goto done;
3093 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3094 if (err)
3095 goto done;
3097 arg.worktree = worktree;
3098 arg.fileindex = fileindex;
3099 arg.progress_cb = progress_cb;
3100 arg.progress_arg = progress_arg;
3101 arg.cancel_cb = cancel_cb;
3102 arg.cancel_arg = cancel_arg;
3103 arg.label_orig = label_orig;
3104 arg.commit_id2 = commit_id2;
3105 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3106 sync_err = sync_fileindex(fileindex, fileindex_path);
3107 if (sync_err && err == NULL)
3108 err = sync_err;
3109 done:
3110 if (tree1)
3111 got_object_tree_close(tree1);
3112 if (tree2)
3113 got_object_tree_close(tree2);
3114 free(label_orig);
3115 return err;
3118 const struct got_error *
3119 got_worktree_merge_files(struct got_worktree *worktree,
3120 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3121 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3122 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3124 const struct got_error *err, *unlockerr;
3125 char *fileindex_path = NULL;
3126 struct got_fileindex *fileindex = NULL;
3127 struct check_merge_ok_arg mok_arg;
3129 err = lock_worktree(worktree, LOCK_EX);
3130 if (err)
3131 return err;
3133 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3134 if (err)
3135 goto done;
3137 mok_arg.worktree = worktree;
3138 mok_arg.repo = repo;
3139 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3140 &mok_arg);
3141 if (err)
3142 goto done;
3144 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3145 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3146 done:
3147 if (fileindex)
3148 got_fileindex_free(fileindex);
3149 free(fileindex_path);
3150 unlockerr = lock_worktree(worktree, LOCK_SH);
3151 if (unlockerr && err == NULL)
3152 err = unlockerr;
3153 return err;
3156 struct diff_dir_cb_arg {
3157 struct got_fileindex *fileindex;
3158 struct got_worktree *worktree;
3159 const char *status_path;
3160 size_t status_path_len;
3161 struct got_repository *repo;
3162 got_worktree_status_cb status_cb;
3163 void *status_arg;
3164 got_cancel_cb cancel_cb;
3165 void *cancel_arg;
3166 /* A pathlist containing per-directory pathlists of ignore patterns. */
3167 struct got_pathlist_head ignores;
3168 int report_unchanged;
3169 int no_ignores;
3172 static const struct got_error *
3173 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3174 int dirfd, const char *de_name,
3175 got_worktree_status_cb status_cb, void *status_arg,
3176 struct got_repository *repo, int report_unchanged)
3178 const struct got_error *err = NULL;
3179 unsigned char status = GOT_STATUS_NO_CHANGE;
3180 unsigned char staged_status = get_staged_status(ie);
3181 struct stat sb;
3182 struct got_object_id blob_id, commit_id, staged_blob_id;
3183 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3184 struct got_object_id *staged_blob_idp = NULL;
3186 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3187 if (err)
3188 return err;
3190 if (status == GOT_STATUS_NO_CHANGE &&
3191 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3192 return NULL;
3194 if (got_fileindex_entry_has_blob(ie)) {
3195 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3196 blob_idp = &blob_id;
3198 if (got_fileindex_entry_has_commit(ie)) {
3199 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3200 commit_idp = &commit_id;
3202 if (staged_status == GOT_STATUS_ADD ||
3203 staged_status == GOT_STATUS_MODIFY) {
3204 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3205 SHA1_DIGEST_LENGTH);
3206 staged_blob_idp = &staged_blob_id;
3209 return (*status_cb)(status_arg, status, staged_status,
3210 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3213 static const struct got_error *
3214 status_old_new(void *arg, struct got_fileindex_entry *ie,
3215 struct dirent *de, const char *parent_path, int dirfd)
3217 const struct got_error *err = NULL;
3218 struct diff_dir_cb_arg *a = arg;
3219 char *abspath;
3221 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3222 return got_error(GOT_ERR_CANCELLED);
3224 if (got_path_cmp(parent_path, a->status_path,
3225 strlen(parent_path), a->status_path_len) != 0 &&
3226 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3227 return NULL;
3229 if (parent_path[0]) {
3230 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3231 parent_path, de->d_name) == -1)
3232 return got_error_from_errno("asprintf");
3233 } else {
3234 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3235 de->d_name) == -1)
3236 return got_error_from_errno("asprintf");
3239 err = report_file_status(ie, abspath, dirfd, de->d_name,
3240 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3241 free(abspath);
3242 return err;
3245 static const struct got_error *
3246 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3248 struct diff_dir_cb_arg *a = arg;
3249 struct got_object_id blob_id, commit_id;
3250 unsigned char status;
3252 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3253 return got_error(GOT_ERR_CANCELLED);
3255 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3256 return NULL;
3258 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3259 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3260 if (got_fileindex_entry_has_file_on_disk(ie))
3261 status = GOT_STATUS_MISSING;
3262 else
3263 status = GOT_STATUS_DELETE;
3264 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3265 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3268 void
3269 free_ignorelist(struct got_pathlist_head *ignorelist)
3271 struct got_pathlist_entry *pe;
3273 TAILQ_FOREACH(pe, ignorelist, entry)
3274 free((char *)pe->path);
3275 got_pathlist_free(ignorelist);
3278 void
3279 free_ignores(struct got_pathlist_head *ignores)
3281 struct got_pathlist_entry *pe;
3283 TAILQ_FOREACH(pe, ignores, entry) {
3284 struct got_pathlist_head *ignorelist = pe->data;
3285 free_ignorelist(ignorelist);
3286 free((char *)pe->path);
3288 got_pathlist_free(ignores);
3291 static const struct got_error *
3292 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3294 const struct got_error *err = NULL;
3295 struct got_pathlist_entry *pe = NULL;
3296 struct got_pathlist_head *ignorelist;
3297 char *line = NULL, *pattern, *dirpath = NULL;
3298 size_t linesize = 0;
3299 ssize_t linelen;
3301 ignorelist = calloc(1, sizeof(*ignorelist));
3302 if (ignorelist == NULL)
3303 return got_error_from_errno("calloc");
3304 TAILQ_INIT(ignorelist);
3306 while ((linelen = getline(&line, &linesize, f)) != -1) {
3307 if (linelen > 0 && line[linelen - 1] == '\n')
3308 line[linelen - 1] = '\0';
3310 /* Git's ignores may contain comments. */
3311 if (line[0] == '#')
3312 continue;
3314 /* Git's negated patterns are not (yet?) supported. */
3315 if (line[0] == '!')
3316 continue;
3318 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3319 line) == -1) {
3320 err = got_error_from_errno("asprintf");
3321 goto done;
3323 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3324 if (err)
3325 goto done;
3327 if (ferror(f)) {
3328 err = got_error_from_errno("getline");
3329 goto done;
3332 dirpath = strdup(path);
3333 if (dirpath == NULL) {
3334 err = got_error_from_errno("strdup");
3335 goto done;
3337 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3338 done:
3339 free(line);
3340 if (err || pe == NULL) {
3341 free(dirpath);
3342 free_ignorelist(ignorelist);
3344 return err;
3347 int
3348 match_ignores(struct got_pathlist_head *ignores, const char *path)
3350 struct got_pathlist_entry *pe;
3352 /* Handle patterns which match in all directories. */
3353 TAILQ_FOREACH(pe, ignores, entry) {
3354 struct got_pathlist_head *ignorelist = pe->data;
3355 struct got_pathlist_entry *pi;
3357 TAILQ_FOREACH(pi, ignorelist, entry) {
3358 const char *p, *pattern = pi->path;
3360 if (strncmp(pattern, "**/", 3) != 0)
3361 continue;
3362 pattern += 3;
3363 p = path;
3364 while (*p) {
3365 if (fnmatch(pattern, p,
3366 FNM_PATHNAME | FNM_LEADING_DIR)) {
3367 /* Retry in next directory. */
3368 while (*p && *p != '/')
3369 p++;
3370 while (*p == '/')
3371 p++;
3372 continue;
3374 return 1;
3380 * The ignores pathlist contains ignore lists from children before
3381 * parents, so we can find the most specific ignorelist by walking
3382 * ignores backwards.
3384 pe = TAILQ_LAST(ignores, got_pathlist_head);
3385 while (pe) {
3386 if (got_path_is_child(path, pe->path, pe->path_len)) {
3387 struct got_pathlist_head *ignorelist = pe->data;
3388 struct got_pathlist_entry *pi;
3389 TAILQ_FOREACH(pi, ignorelist, entry) {
3390 const char *pattern = pi->path;
3391 int flags = FNM_LEADING_DIR;
3392 if (strstr(pattern, "/**/") == NULL)
3393 flags |= FNM_PATHNAME;
3394 if (fnmatch(pattern, path, flags))
3395 continue;
3396 return 1;
3399 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3402 return 0;
3405 static const struct got_error *
3406 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3407 const char *path, int dirfd, const char *ignores_filename)
3409 const struct got_error *err = NULL;
3410 char *ignorespath;
3411 int fd = -1;
3412 FILE *ignoresfile = NULL;
3414 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3415 path[0] ? "/" : "", ignores_filename) == -1)
3416 return got_error_from_errno("asprintf");
3418 if (dirfd != -1) {
3419 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3420 if (fd == -1) {
3421 if (errno != ENOENT && errno != EACCES)
3422 err = got_error_from_errno2("openat",
3423 ignorespath);
3424 } else {
3425 ignoresfile = fdopen(fd, "r");
3426 if (ignoresfile == NULL)
3427 err = got_error_from_errno2("fdopen",
3428 ignorespath);
3429 else {
3430 fd = -1;
3431 err = read_ignores(ignores, path, ignoresfile);
3434 } else {
3435 ignoresfile = fopen(ignorespath, "r");
3436 if (ignoresfile == NULL) {
3437 if (errno != ENOENT && errno != EACCES)
3438 err = got_error_from_errno2("fopen",
3439 ignorespath);
3440 } else
3441 err = read_ignores(ignores, path, ignoresfile);
3444 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3445 err = got_error_from_errno2("fclose", path);
3446 if (fd != -1 && close(fd) == -1 && err == NULL)
3447 err = got_error_from_errno2("close", path);
3448 free(ignorespath);
3449 return err;
3452 static const struct got_error *
3453 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3455 const struct got_error *err = NULL;
3456 struct diff_dir_cb_arg *a = arg;
3457 char *path = NULL;
3459 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3460 return got_error(GOT_ERR_CANCELLED);
3462 if (parent_path[0]) {
3463 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3464 return got_error_from_errno("asprintf");
3465 } else {
3466 path = de->d_name;
3469 if (de->d_type != DT_DIR &&
3470 got_path_is_child(path, a->status_path, a->status_path_len)
3471 && !match_ignores(&a->ignores, path))
3472 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3473 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3474 if (parent_path[0])
3475 free(path);
3476 return err;
3479 static const struct got_error *
3480 status_traverse(void *arg, const char *path, int dirfd)
3482 const struct got_error *err = NULL;
3483 struct diff_dir_cb_arg *a = arg;
3485 if (a->no_ignores)
3486 return NULL;
3488 err = add_ignores(&a->ignores, a->worktree->root_path,
3489 path, dirfd, ".cvsignore");
3490 if (err)
3491 return err;
3493 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3494 dirfd, ".gitignore");
3496 return err;
3499 static const struct got_error *
3500 report_single_file_status(const char *path, const char *ondisk_path,
3501 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3502 void *status_arg, struct got_repository *repo, int report_unchanged)
3504 struct got_fileindex_entry *ie;
3505 struct stat sb;
3507 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3508 if (ie)
3509 return report_file_status(ie, ondisk_path, -1, NULL,
3510 status_cb, status_arg, repo, report_unchanged);
3512 if (lstat(ondisk_path, &sb) == -1) {
3513 if (errno != ENOENT)
3514 return got_error_from_errno2("lstat", ondisk_path);
3515 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3516 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3517 return NULL;
3520 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3521 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3522 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3524 return NULL;
3527 static const struct got_error *
3528 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3529 const char *root_path, const char *path)
3531 const struct got_error *err;
3532 char *parent_path, *next_parent_path = NULL;
3534 err = add_ignores(ignores, root_path, "", -1,
3535 ".cvsignore");
3536 if (err)
3537 return err;
3539 err = add_ignores(ignores, root_path, "", -1,
3540 ".gitignore");
3541 if (err)
3542 return err;
3544 err = got_path_dirname(&parent_path, path);
3545 if (err) {
3546 if (err->code == GOT_ERR_BAD_PATH)
3547 return NULL; /* cannot traverse parent */
3548 return err;
3550 for (;;) {
3551 err = add_ignores(ignores, root_path, parent_path, -1,
3552 ".cvsignore");
3553 if (err)
3554 break;
3555 err = add_ignores(ignores, root_path, parent_path, -1,
3556 ".gitignore");
3557 if (err)
3558 break;
3559 err = got_path_dirname(&next_parent_path, parent_path);
3560 if (err) {
3561 if (err->code == GOT_ERR_BAD_PATH)
3562 err = NULL; /* traversed everything */
3563 break;
3565 free(parent_path);
3566 parent_path = next_parent_path;
3567 next_parent_path = NULL;
3570 free(parent_path);
3571 free(next_parent_path);
3572 return err;
3575 static const struct got_error *
3576 worktree_status(struct got_worktree *worktree, const char *path,
3577 struct got_fileindex *fileindex, struct got_repository *repo,
3578 got_worktree_status_cb status_cb, void *status_arg,
3579 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3580 int report_unchanged)
3582 const struct got_error *err = NULL;
3583 int fd = -1;
3584 struct got_fileindex_diff_dir_cb fdiff_cb;
3585 struct diff_dir_cb_arg arg;
3586 char *ondisk_path = NULL;
3588 TAILQ_INIT(&arg.ignores);
3590 if (asprintf(&ondisk_path, "%s%s%s",
3591 worktree->root_path, path[0] ? "/" : "", path) == -1)
3592 return got_error_from_errno("asprintf");
3594 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3595 if (fd == -1) {
3596 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3597 errno != ELOOP)
3598 err = got_error_from_errno2("open", ondisk_path);
3599 else
3600 err = report_single_file_status(path, ondisk_path,
3601 fileindex, status_cb, status_arg, repo,
3602 report_unchanged);
3603 } else {
3604 fdiff_cb.diff_old_new = status_old_new;
3605 fdiff_cb.diff_old = status_old;
3606 fdiff_cb.diff_new = status_new;
3607 fdiff_cb.diff_traverse = status_traverse;
3608 arg.fileindex = fileindex;
3609 arg.worktree = worktree;
3610 arg.status_path = path;
3611 arg.status_path_len = strlen(path);
3612 arg.repo = repo;
3613 arg.status_cb = status_cb;
3614 arg.status_arg = status_arg;
3615 arg.cancel_cb = cancel_cb;
3616 arg.cancel_arg = cancel_arg;
3617 arg.report_unchanged = report_unchanged;
3618 arg.no_ignores = no_ignores;
3619 if (!no_ignores) {
3620 err = add_ignores_from_parent_paths(&arg.ignores,
3621 worktree->root_path, path);
3622 if (err)
3623 goto done;
3625 err = got_fileindex_diff_dir(fileindex, fd,
3626 worktree->root_path, path, repo, &fdiff_cb, &arg);
3628 done:
3629 free_ignores(&arg.ignores);
3630 if (fd != -1 && close(fd) == -1 && err == NULL)
3631 err = got_error_from_errno("close");
3632 free(ondisk_path);
3633 return err;
3636 const struct got_error *
3637 got_worktree_status(struct got_worktree *worktree,
3638 struct got_pathlist_head *paths, struct got_repository *repo,
3639 got_worktree_status_cb status_cb, void *status_arg,
3640 got_cancel_cb cancel_cb, void *cancel_arg)
3642 const struct got_error *err = NULL;
3643 char *fileindex_path = NULL;
3644 struct got_fileindex *fileindex = NULL;
3645 struct got_pathlist_entry *pe;
3647 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3648 if (err)
3649 return err;
3651 TAILQ_FOREACH(pe, paths, entry) {
3652 err = worktree_status(worktree, pe->path, fileindex, repo,
3653 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3654 if (err)
3655 break;
3657 free(fileindex_path);
3658 got_fileindex_free(fileindex);
3659 return err;
3662 const struct got_error *
3663 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3664 const char *arg)
3666 const struct got_error *err = NULL;
3667 char *resolved = NULL, *cwd = NULL, *path = NULL;
3668 size_t len;
3669 struct stat sb;
3670 char *abspath = NULL;
3671 char canonpath[PATH_MAX];
3673 *wt_path = NULL;
3675 cwd = getcwd(NULL, 0);
3676 if (cwd == NULL)
3677 return got_error_from_errno("getcwd");
3679 if (lstat(arg, &sb) == -1) {
3680 if (errno != ENOENT) {
3681 err = got_error_from_errno2("lstat", arg);
3682 goto done;
3684 sb.st_mode = 0;
3686 if (S_ISLNK(sb.st_mode)) {
3688 * We cannot use realpath(3) with symlinks since we want to
3689 * operate on the symlink itself.
3690 * But we can make the path absolute, assuming it is relative
3691 * to the current working directory, and then canonicalize it.
3693 if (!got_path_is_absolute(arg)) {
3694 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3695 err = got_error_from_errno("asprintf");
3696 goto done;
3700 err = got_canonpath(abspath ? abspath : arg, canonpath,
3701 sizeof(canonpath));
3702 if (err)
3703 goto done;
3704 resolved = strdup(canonpath);
3705 if (resolved == NULL) {
3706 err = got_error_from_errno("strdup");
3707 goto done;
3709 } else {
3710 resolved = realpath(arg, NULL);
3711 if (resolved == NULL) {
3712 if (errno != ENOENT) {
3713 err = got_error_from_errno2("realpath", arg);
3714 goto done;
3716 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3717 err = got_error_from_errno("asprintf");
3718 goto done;
3720 err = got_canonpath(abspath, canonpath,
3721 sizeof(canonpath));
3722 if (err)
3723 goto done;
3724 resolved = strdup(canonpath);
3725 if (resolved == NULL) {
3726 err = got_error_from_errno("strdup");
3727 goto done;
3732 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3733 strlen(got_worktree_get_root_path(worktree)))) {
3734 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3735 goto done;
3738 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3739 err = got_path_skip_common_ancestor(&path,
3740 got_worktree_get_root_path(worktree), resolved);
3741 if (err)
3742 goto done;
3743 } else {
3744 path = strdup("");
3745 if (path == NULL) {
3746 err = got_error_from_errno("strdup");
3747 goto done;
3751 /* XXX status walk can't deal with trailing slash! */
3752 len = strlen(path);
3753 while (len > 0 && path[len - 1] == '/') {
3754 path[len - 1] = '\0';
3755 len--;
3757 done:
3758 free(abspath);
3759 free(resolved);
3760 free(cwd);
3761 if (err == NULL)
3762 *wt_path = path;
3763 else
3764 free(path);
3765 return err;
3768 struct schedule_addition_args {
3769 struct got_worktree *worktree;
3770 struct got_fileindex *fileindex;
3771 got_worktree_checkout_cb progress_cb;
3772 void *progress_arg;
3773 struct got_repository *repo;
3776 static const struct got_error *
3777 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3778 const char *relpath, struct got_object_id *blob_id,
3779 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3780 int dirfd, const char *de_name)
3782 struct schedule_addition_args *a = arg;
3783 const struct got_error *err = NULL;
3784 struct got_fileindex_entry *ie;
3785 struct stat sb;
3786 char *ondisk_path;
3788 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3789 relpath) == -1)
3790 return got_error_from_errno("asprintf");
3792 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3793 if (ie) {
3794 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3795 de_name, a->repo);
3796 if (err)
3797 goto done;
3798 /* Re-adding an existing entry is a no-op. */
3799 if (status == GOT_STATUS_ADD)
3800 goto done;
3801 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3802 if (err)
3803 goto done;
3806 if (status != GOT_STATUS_UNVERSIONED) {
3807 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3808 goto done;
3811 err = got_fileindex_entry_alloc(&ie, relpath);
3812 if (err)
3813 goto done;
3814 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3815 relpath, NULL, NULL, 1);
3816 if (err) {
3817 got_fileindex_entry_free(ie);
3818 goto done;
3820 err = got_fileindex_entry_add(a->fileindex, ie);
3821 if (err) {
3822 got_fileindex_entry_free(ie);
3823 goto done;
3825 done:
3826 free(ondisk_path);
3827 if (err)
3828 return err;
3829 if (status == GOT_STATUS_ADD)
3830 return NULL;
3831 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3834 const struct got_error *
3835 got_worktree_schedule_add(struct got_worktree *worktree,
3836 struct got_pathlist_head *paths,
3837 got_worktree_checkout_cb progress_cb, void *progress_arg,
3838 struct got_repository *repo, int no_ignores)
3840 struct got_fileindex *fileindex = NULL;
3841 char *fileindex_path = NULL;
3842 const struct got_error *err = NULL, *sync_err, *unlockerr;
3843 struct got_pathlist_entry *pe;
3844 struct schedule_addition_args saa;
3846 err = lock_worktree(worktree, LOCK_EX);
3847 if (err)
3848 return err;
3850 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3851 if (err)
3852 goto done;
3854 saa.worktree = worktree;
3855 saa.fileindex = fileindex;
3856 saa.progress_cb = progress_cb;
3857 saa.progress_arg = progress_arg;
3858 saa.repo = repo;
3860 TAILQ_FOREACH(pe, paths, entry) {
3861 err = worktree_status(worktree, pe->path, fileindex, repo,
3862 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3863 if (err)
3864 break;
3866 sync_err = sync_fileindex(fileindex, fileindex_path);
3867 if (sync_err && err == NULL)
3868 err = sync_err;
3869 done:
3870 free(fileindex_path);
3871 if (fileindex)
3872 got_fileindex_free(fileindex);
3873 unlockerr = lock_worktree(worktree, LOCK_SH);
3874 if (unlockerr && err == NULL)
3875 err = unlockerr;
3876 return err;
3879 struct schedule_deletion_args {
3880 struct got_worktree *worktree;
3881 struct got_fileindex *fileindex;
3882 got_worktree_delete_cb progress_cb;
3883 void *progress_arg;
3884 struct got_repository *repo;
3885 int delete_local_mods;
3886 int keep_on_disk;
3887 const char *status_codes;
3890 static const struct got_error *
3891 schedule_for_deletion(void *arg, unsigned char status,
3892 unsigned char staged_status, const char *relpath,
3893 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3894 struct got_object_id *commit_id, int dirfd, const char *de_name)
3896 struct schedule_deletion_args *a = arg;
3897 const struct got_error *err = NULL;
3898 struct got_fileindex_entry *ie = NULL;
3899 struct stat sb;
3900 char *ondisk_path;
3902 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3903 if (ie == NULL)
3904 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3906 staged_status = get_staged_status(ie);
3907 if (staged_status != GOT_STATUS_NO_CHANGE) {
3908 if (staged_status == GOT_STATUS_DELETE)
3909 return NULL;
3910 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3913 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3914 relpath) == -1)
3915 return got_error_from_errno("asprintf");
3917 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3918 a->repo);
3919 if (err)
3920 goto done;
3922 if (a->status_codes) {
3923 size_t ncodes = strlen(a->status_codes);
3924 int i;
3925 for (i = 0; i < ncodes ; i++) {
3926 if (status == a->status_codes[i])
3927 break;
3929 if (i == ncodes) {
3930 /* Do not delete files in non-matching status. */
3931 free(ondisk_path);
3932 return NULL;
3934 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3935 a->status_codes[i] != GOT_STATUS_MISSING) {
3936 static char msg[64];
3937 snprintf(msg, sizeof(msg),
3938 "invalid status code '%c'", a->status_codes[i]);
3939 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3940 goto done;
3944 if (status != GOT_STATUS_NO_CHANGE) {
3945 if (status == GOT_STATUS_DELETE)
3946 goto done;
3947 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3948 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3949 goto done;
3951 if (status != GOT_STATUS_MODIFY &&
3952 status != GOT_STATUS_MISSING) {
3953 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3954 goto done;
3958 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3959 size_t root_len;
3961 if (dirfd != -1) {
3962 if (unlinkat(dirfd, de_name, 0) != 0) {
3963 err = got_error_from_errno2("unlinkat",
3964 ondisk_path);
3965 goto done;
3967 } else if (unlink(ondisk_path) != 0) {
3968 err = got_error_from_errno2("unlink", ondisk_path);
3969 goto done;
3972 root_len = strlen(a->worktree->root_path);
3973 do {
3974 char *parent;
3975 err = got_path_dirname(&parent, ondisk_path);
3976 if (err)
3977 goto done;
3978 free(ondisk_path);
3979 ondisk_path = parent;
3980 if (rmdir(ondisk_path) == -1) {
3981 if (errno != ENOTEMPTY)
3982 err = got_error_from_errno2("rmdir",
3983 ondisk_path);
3984 break;
3986 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3987 strlen(ondisk_path), root_len) != 0);
3990 got_fileindex_entry_mark_deleted_from_disk(ie);
3991 done:
3992 free(ondisk_path);
3993 if (err)
3994 return err;
3995 if (status == GOT_STATUS_DELETE)
3996 return NULL;
3997 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3998 staged_status, relpath);
4001 const struct got_error *
4002 got_worktree_schedule_delete(struct got_worktree *worktree,
4003 struct got_pathlist_head *paths, int delete_local_mods,
4004 const char *status_codes,
4005 got_worktree_delete_cb progress_cb, void *progress_arg,
4006 struct got_repository *repo, int keep_on_disk)
4008 struct got_fileindex *fileindex = NULL;
4009 char *fileindex_path = NULL;
4010 const struct got_error *err = NULL, *sync_err, *unlockerr;
4011 struct got_pathlist_entry *pe;
4012 struct schedule_deletion_args sda;
4014 err = lock_worktree(worktree, LOCK_EX);
4015 if (err)
4016 return err;
4018 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4019 if (err)
4020 goto done;
4022 sda.worktree = worktree;
4023 sda.fileindex = fileindex;
4024 sda.progress_cb = progress_cb;
4025 sda.progress_arg = progress_arg;
4026 sda.repo = repo;
4027 sda.delete_local_mods = delete_local_mods;
4028 sda.keep_on_disk = keep_on_disk;
4029 sda.status_codes = status_codes;
4031 TAILQ_FOREACH(pe, paths, entry) {
4032 err = worktree_status(worktree, pe->path, fileindex, repo,
4033 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4034 if (err)
4035 break;
4037 sync_err = sync_fileindex(fileindex, fileindex_path);
4038 if (sync_err && err == NULL)
4039 err = sync_err;
4040 done:
4041 free(fileindex_path);
4042 if (fileindex)
4043 got_fileindex_free(fileindex);
4044 unlockerr = lock_worktree(worktree, LOCK_SH);
4045 if (unlockerr && err == NULL)
4046 err = unlockerr;
4047 return err;
4050 static const struct got_error *
4051 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4053 const struct got_error *err = NULL;
4054 char *line = NULL;
4055 size_t linesize = 0, n;
4056 ssize_t linelen;
4058 linelen = getline(&line, &linesize, infile);
4059 if (linelen == -1) {
4060 if (ferror(infile)) {
4061 err = got_error_from_errno("getline");
4062 goto done;
4064 return NULL;
4066 if (outfile) {
4067 n = fwrite(line, 1, linelen, outfile);
4068 if (n != linelen) {
4069 err = got_ferror(outfile, GOT_ERR_IO);
4070 goto done;
4073 if (rejectfile) {
4074 n = fwrite(line, 1, linelen, rejectfile);
4075 if (n != linelen)
4076 err = got_ferror(outfile, GOT_ERR_IO);
4078 done:
4079 free(line);
4080 return err;
4083 static const struct got_error *
4084 skip_one_line(FILE *f)
4086 char *line = NULL;
4087 size_t linesize = 0;
4088 ssize_t linelen;
4090 linelen = getline(&line, &linesize, f);
4091 if (linelen == -1) {
4092 if (ferror(f))
4093 return got_error_from_errno("getline");
4094 return NULL;
4096 free(line);
4097 return NULL;
4100 static const struct got_error *
4101 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4102 int start_old, int end_old, int start_new, int end_new,
4103 FILE *outfile, FILE *rejectfile)
4105 const struct got_error *err;
4107 /* Copy old file's lines leading up to patch. */
4108 while (!feof(f1) && *line_cur1 < start_old) {
4109 err = copy_one_line(f1, outfile, NULL);
4110 if (err)
4111 return err;
4112 (*line_cur1)++;
4114 /* Skip new file's lines leading up to patch. */
4115 while (!feof(f2) && *line_cur2 < start_new) {
4116 if (rejectfile)
4117 err = copy_one_line(f2, NULL, rejectfile);
4118 else
4119 err = skip_one_line(f2);
4120 if (err)
4121 return err;
4122 (*line_cur2)++;
4124 /* Copy patched lines. */
4125 while (!feof(f2) && *line_cur2 <= end_new) {
4126 err = copy_one_line(f2, outfile, NULL);
4127 if (err)
4128 return err;
4129 (*line_cur2)++;
4131 /* Skip over old file's replaced lines. */
4132 while (!feof(f1) && *line_cur1 <= end_old) {
4133 if (rejectfile)
4134 err = copy_one_line(f1, NULL, rejectfile);
4135 else
4136 err = skip_one_line(f1);
4137 if (err)
4138 return err;
4139 (*line_cur1)++;
4142 return NULL;
4145 static const struct got_error *
4146 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4147 FILE *outfile, FILE *rejectfile)
4149 const struct got_error *err;
4151 if (outfile) {
4152 /* Copy old file's lines until EOF. */
4153 while (!feof(f1)) {
4154 err = copy_one_line(f1, outfile, NULL);
4155 if (err)
4156 return err;
4157 (*line_cur1)++;
4160 if (rejectfile) {
4161 /* Copy new file's lines until EOF. */
4162 while (!feof(f2)) {
4163 err = copy_one_line(f2, NULL, rejectfile);
4164 if (err)
4165 return err;
4166 (*line_cur2)++;
4170 return NULL;
4173 static const struct got_error *
4174 apply_or_reject_change(int *choice, int *nchunks_used,
4175 struct diff_result *diff_result, int n,
4176 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4177 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4178 got_worktree_patch_cb patch_cb, void *patch_arg)
4180 const struct got_error *err = NULL;
4181 struct diff_chunk_context cc = {};
4182 int start_old, end_old, start_new, end_new;
4183 FILE *hunkfile;
4184 struct diff_output_unidiff_state *diff_state;
4185 struct diff_input_info diff_info;
4186 int rc;
4188 *choice = GOT_PATCH_CHOICE_NONE;
4190 /* Get changed line numbers without context lines for copy_change(). */
4191 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4192 start_old = cc.left.start;
4193 end_old = cc.left.end;
4194 start_new = cc.right.start;
4195 end_new = cc.right.end;
4197 /* Get the same change with context lines for display. */
4198 memset(&cc, 0, sizeof(cc));
4199 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4201 memset(&diff_info, 0, sizeof(diff_info));
4202 diff_info.left_path = relpath;
4203 diff_info.right_path = relpath;
4205 diff_state = diff_output_unidiff_state_alloc();
4206 if (diff_state == NULL)
4207 return got_error_set_errno(ENOMEM,
4208 "diff_output_unidiff_state_alloc");
4210 hunkfile = got_opentemp();
4211 if (hunkfile == NULL) {
4212 err = got_error_from_errno("got_opentemp");
4213 goto done;
4216 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4217 diff_result, &cc);
4218 if (rc != DIFF_RC_OK) {
4219 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4220 goto done;
4223 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4224 err = got_ferror(hunkfile, GOT_ERR_IO);
4225 goto done;
4228 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4229 hunkfile, changeno, nchanges);
4230 if (err)
4231 goto done;
4233 switch (*choice) {
4234 case GOT_PATCH_CHOICE_YES:
4235 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4236 end_old, start_new, end_new, outfile, rejectfile);
4237 break;
4238 case GOT_PATCH_CHOICE_NO:
4239 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4240 end_old, start_new, end_new, rejectfile, outfile);
4241 break;
4242 case GOT_PATCH_CHOICE_QUIT:
4243 break;
4244 default:
4245 err = got_error(GOT_ERR_PATCH_CHOICE);
4246 break;
4248 done:
4249 diff_output_unidiff_state_free(diff_state);
4250 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4251 err = got_error_from_errno("fclose");
4252 return err;
4255 struct revert_file_args {
4256 struct got_worktree *worktree;
4257 struct got_fileindex *fileindex;
4258 got_worktree_checkout_cb progress_cb;
4259 void *progress_arg;
4260 got_worktree_patch_cb patch_cb;
4261 void *patch_arg;
4262 struct got_repository *repo;
4265 static const struct got_error *
4266 create_patched_content(char **path_outfile, int reverse_patch,
4267 struct got_object_id *blob_id, const char *path2,
4268 int dirfd2, const char *de_name2,
4269 const char *relpath, struct got_repository *repo,
4270 got_worktree_patch_cb patch_cb, void *patch_arg)
4272 const struct got_error *err, *free_err;
4273 struct got_blob_object *blob = NULL;
4274 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4275 int fd2 = -1;
4276 char link_target[PATH_MAX];
4277 ssize_t link_len = 0;
4278 char *path1 = NULL, *id_str = NULL;
4279 struct stat sb2;
4280 struct got_diffreg_result *diffreg_result = NULL;
4281 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4282 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4284 *path_outfile = NULL;
4286 err = got_object_id_str(&id_str, blob_id);
4287 if (err)
4288 return err;
4290 if (dirfd2 != -1) {
4291 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4292 if (fd2 == -1) {
4293 if (errno != ELOOP) {
4294 err = got_error_from_errno2("openat", path2);
4295 goto done;
4297 link_len = readlinkat(dirfd2, de_name2,
4298 link_target, sizeof(link_target));
4299 if (link_len == -1)
4300 return got_error_from_errno2("readlinkat", path2);
4301 sb2.st_mode = S_IFLNK;
4302 sb2.st_size = link_len;
4304 } else {
4305 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4306 if (fd2 == -1) {
4307 if (errno != ELOOP) {
4308 err = got_error_from_errno2("open", path2);
4309 goto done;
4311 link_len = readlink(path2, link_target,
4312 sizeof(link_target));
4313 if (link_len == -1)
4314 return got_error_from_errno2("readlink", path2);
4315 sb2.st_mode = S_IFLNK;
4316 sb2.st_size = link_len;
4319 if (fd2 != -1) {
4320 if (fstat(fd2, &sb2) == -1) {
4321 err = got_error_from_errno2("fstat", path2);
4322 goto done;
4325 f2 = fdopen(fd2, "r");
4326 if (f2 == NULL) {
4327 err = got_error_from_errno2("fdopen", path2);
4328 goto done;
4330 fd2 = -1;
4331 } else {
4332 size_t n;
4333 f2 = got_opentemp();
4334 if (f2 == NULL) {
4335 err = got_error_from_errno2("got_opentemp", path2);
4336 goto done;
4338 n = fwrite(link_target, 1, link_len, f2);
4339 if (n != link_len) {
4340 err = got_ferror(f2, GOT_ERR_IO);
4341 goto done;
4343 if (fflush(f2) == EOF) {
4344 err = got_error_from_errno("fflush");
4345 goto done;
4347 rewind(f2);
4350 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4351 if (err)
4352 goto done;
4354 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4355 if (err)
4356 goto done;
4358 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4359 if (err)
4360 goto done;
4362 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4363 NULL);
4364 if (err)
4365 goto done;
4367 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4368 if (err)
4369 goto done;
4371 if (fseek(f1, 0L, SEEK_SET) == -1)
4372 return got_ferror(f1, GOT_ERR_IO);
4373 if (fseek(f2, 0L, SEEK_SET) == -1)
4374 return got_ferror(f2, GOT_ERR_IO);
4376 /* Count the number of actual changes in the diff result. */
4377 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4378 struct diff_chunk_context cc = {};
4379 diff_chunk_context_load_change(&cc, &nchunks_used,
4380 diffreg_result->result, n, 0);
4381 nchanges++;
4383 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4384 int choice;
4385 err = apply_or_reject_change(&choice, &nchunks_used,
4386 diffreg_result->result, n, relpath, f1, f2,
4387 &line_cur1, &line_cur2,
4388 reverse_patch ? NULL : outfile,
4389 reverse_patch ? outfile : NULL,
4390 ++i, nchanges, patch_cb, patch_arg);
4391 if (err)
4392 goto done;
4393 if (choice == GOT_PATCH_CHOICE_YES)
4394 have_content = 1;
4395 else if (choice == GOT_PATCH_CHOICE_QUIT)
4396 break;
4398 if (have_content) {
4399 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4400 reverse_patch ? NULL : outfile,
4401 reverse_patch ? outfile : NULL);
4402 if (err)
4403 goto done;
4405 if (!S_ISLNK(sb2.st_mode)) {
4406 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4407 err = got_error_from_errno2("fchmod", path2);
4408 goto done;
4412 done:
4413 free(id_str);
4414 if (blob)
4415 got_object_blob_close(blob);
4416 free_err = got_diffreg_result_free(diffreg_result);
4417 if (err == NULL)
4418 err = free_err;
4419 if (f1 && fclose(f1) == EOF && err == NULL)
4420 err = got_error_from_errno2("fclose", path1);
4421 if (f2 && fclose(f2) == EOF && err == NULL)
4422 err = got_error_from_errno2("fclose", path2);
4423 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4424 err = got_error_from_errno2("close", path2);
4425 if (outfile && fclose(outfile) == EOF && err == NULL)
4426 err = got_error_from_errno2("fclose", *path_outfile);
4427 if (path1 && unlink(path1) == -1 && err == NULL)
4428 err = got_error_from_errno2("unlink", path1);
4429 if (err || !have_content) {
4430 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4431 err = got_error_from_errno2("unlink", *path_outfile);
4432 free(*path_outfile);
4433 *path_outfile = NULL;
4435 free(path1);
4436 return err;
4439 static const struct got_error *
4440 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4441 const char *relpath, struct got_object_id *blob_id,
4442 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4443 int dirfd, const char *de_name)
4445 struct revert_file_args *a = arg;
4446 const struct got_error *err = NULL;
4447 char *parent_path = NULL;
4448 struct got_fileindex_entry *ie;
4449 struct got_tree_object *tree = NULL;
4450 struct got_object_id *tree_id = NULL;
4451 const struct got_tree_entry *te = NULL;
4452 char *tree_path = NULL, *te_name;
4453 char *ondisk_path = NULL, *path_content = NULL;
4454 struct got_blob_object *blob = NULL;
4456 /* Reverting a staged deletion is a no-op. */
4457 if (status == GOT_STATUS_DELETE &&
4458 staged_status != GOT_STATUS_NO_CHANGE)
4459 return NULL;
4461 if (status == GOT_STATUS_UNVERSIONED)
4462 return (*a->progress_cb)(a->progress_arg,
4463 GOT_STATUS_UNVERSIONED, relpath);
4465 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4466 if (ie == NULL)
4467 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4469 /* Construct in-repository path of tree which contains this blob. */
4470 err = got_path_dirname(&parent_path, ie->path);
4471 if (err) {
4472 if (err->code != GOT_ERR_BAD_PATH)
4473 goto done;
4474 parent_path = strdup("/");
4475 if (parent_path == NULL) {
4476 err = got_error_from_errno("strdup");
4477 goto done;
4480 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4481 tree_path = strdup(parent_path);
4482 if (tree_path == NULL) {
4483 err = got_error_from_errno("strdup");
4484 goto done;
4486 } else {
4487 if (got_path_is_root_dir(parent_path)) {
4488 tree_path = strdup(a->worktree->path_prefix);
4489 if (tree_path == NULL) {
4490 err = got_error_from_errno("strdup");
4491 goto done;
4493 } else {
4494 if (asprintf(&tree_path, "%s/%s",
4495 a->worktree->path_prefix, parent_path) == -1) {
4496 err = got_error_from_errno("asprintf");
4497 goto done;
4502 err = got_object_id_by_path(&tree_id, a->repo,
4503 a->worktree->base_commit_id, tree_path);
4504 if (err) {
4505 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4506 (status == GOT_STATUS_ADD ||
4507 staged_status == GOT_STATUS_ADD)))
4508 goto done;
4509 } else {
4510 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4511 if (err)
4512 goto done;
4514 err = got_path_basename(&te_name, ie->path);
4515 if (err)
4516 goto done;
4518 te = got_object_tree_find_entry(tree, te_name);
4519 free(te_name);
4520 if (te == NULL && status != GOT_STATUS_ADD &&
4521 staged_status != GOT_STATUS_ADD) {
4522 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4523 goto done;
4527 switch (status) {
4528 case GOT_STATUS_ADD:
4529 if (a->patch_cb) {
4530 int choice = GOT_PATCH_CHOICE_NONE;
4531 err = (*a->patch_cb)(&choice, a->patch_arg,
4532 status, ie->path, NULL, 1, 1);
4533 if (err)
4534 goto done;
4535 if (choice != GOT_PATCH_CHOICE_YES)
4536 break;
4538 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4539 ie->path);
4540 if (err)
4541 goto done;
4542 got_fileindex_entry_remove(a->fileindex, ie);
4543 break;
4544 case GOT_STATUS_DELETE:
4545 if (a->patch_cb) {
4546 int choice = GOT_PATCH_CHOICE_NONE;
4547 err = (*a->patch_cb)(&choice, a->patch_arg,
4548 status, ie->path, NULL, 1, 1);
4549 if (err)
4550 goto done;
4551 if (choice != GOT_PATCH_CHOICE_YES)
4552 break;
4554 /* fall through */
4555 case GOT_STATUS_MODIFY:
4556 case GOT_STATUS_MODE_CHANGE:
4557 case GOT_STATUS_CONFLICT:
4558 case GOT_STATUS_MISSING: {
4559 struct got_object_id id;
4560 if (staged_status == GOT_STATUS_ADD ||
4561 staged_status == GOT_STATUS_MODIFY) {
4562 memcpy(id.sha1, ie->staged_blob_sha1,
4563 SHA1_DIGEST_LENGTH);
4564 } else
4565 memcpy(id.sha1, ie->blob_sha1,
4566 SHA1_DIGEST_LENGTH);
4567 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4568 if (err)
4569 goto done;
4571 if (asprintf(&ondisk_path, "%s/%s",
4572 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4573 err = got_error_from_errno("asprintf");
4574 goto done;
4577 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4578 status == GOT_STATUS_CONFLICT)) {
4579 int is_bad_symlink = 0;
4580 err = create_patched_content(&path_content, 1, &id,
4581 ondisk_path, dirfd, de_name, ie->path, a->repo,
4582 a->patch_cb, a->patch_arg);
4583 if (err || path_content == NULL)
4584 break;
4585 if (te && S_ISLNK(te->mode)) {
4586 if (unlink(path_content) == -1) {
4587 err = got_error_from_errno2("unlink",
4588 path_content);
4589 break;
4591 err = install_symlink(&is_bad_symlink,
4592 a->worktree, ondisk_path, ie->path,
4593 blob, 0, 1, 0, a->repo,
4594 a->progress_cb, a->progress_arg);
4595 } else {
4596 if (rename(path_content, ondisk_path) == -1) {
4597 err = got_error_from_errno3("rename",
4598 path_content, ondisk_path);
4599 goto done;
4602 } else {
4603 int is_bad_symlink = 0;
4604 if (te && S_ISLNK(te->mode)) {
4605 err = install_symlink(&is_bad_symlink,
4606 a->worktree, ondisk_path, ie->path,
4607 blob, 0, 1, 0, a->repo,
4608 a->progress_cb, a->progress_arg);
4609 } else {
4610 err = install_blob(a->worktree, ondisk_path,
4611 ie->path,
4612 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4613 got_fileindex_perms_to_st(ie), blob,
4614 0, 1, 0, 0, a->repo,
4615 a->progress_cb, a->progress_arg);
4617 if (err)
4618 goto done;
4619 if (status == GOT_STATUS_DELETE ||
4620 status == GOT_STATUS_MODE_CHANGE) {
4621 err = got_fileindex_entry_update(ie,
4622 a->worktree->root_fd, relpath,
4623 blob->id.sha1,
4624 a->worktree->base_commit_id->sha1, 1);
4625 if (err)
4626 goto done;
4628 if (is_bad_symlink) {
4629 got_fileindex_entry_filetype_set(ie,
4630 GOT_FILEIDX_MODE_BAD_SYMLINK);
4633 break;
4635 default:
4636 break;
4638 done:
4639 free(ondisk_path);
4640 free(path_content);
4641 free(parent_path);
4642 free(tree_path);
4643 if (blob)
4644 got_object_blob_close(blob);
4645 if (tree)
4646 got_object_tree_close(tree);
4647 free(tree_id);
4648 return err;
4651 const struct got_error *
4652 got_worktree_revert(struct got_worktree *worktree,
4653 struct got_pathlist_head *paths,
4654 got_worktree_checkout_cb progress_cb, void *progress_arg,
4655 got_worktree_patch_cb patch_cb, void *patch_arg,
4656 struct got_repository *repo)
4658 struct got_fileindex *fileindex = NULL;
4659 char *fileindex_path = NULL;
4660 const struct got_error *err = NULL, *unlockerr = NULL;
4661 const struct got_error *sync_err = NULL;
4662 struct got_pathlist_entry *pe;
4663 struct revert_file_args rfa;
4665 err = lock_worktree(worktree, LOCK_EX);
4666 if (err)
4667 return err;
4669 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4670 if (err)
4671 goto done;
4673 rfa.worktree = worktree;
4674 rfa.fileindex = fileindex;
4675 rfa.progress_cb = progress_cb;
4676 rfa.progress_arg = progress_arg;
4677 rfa.patch_cb = patch_cb;
4678 rfa.patch_arg = patch_arg;
4679 rfa.repo = repo;
4680 TAILQ_FOREACH(pe, paths, entry) {
4681 err = worktree_status(worktree, pe->path, fileindex, repo,
4682 revert_file, &rfa, NULL, NULL, 0, 0);
4683 if (err)
4684 break;
4686 sync_err = sync_fileindex(fileindex, fileindex_path);
4687 if (sync_err && err == NULL)
4688 err = sync_err;
4689 done:
4690 free(fileindex_path);
4691 if (fileindex)
4692 got_fileindex_free(fileindex);
4693 unlockerr = lock_worktree(worktree, LOCK_SH);
4694 if (unlockerr && err == NULL)
4695 err = unlockerr;
4696 return err;
4699 static void
4700 free_commitable(struct got_commitable *ct)
4702 free(ct->path);
4703 free(ct->in_repo_path);
4704 free(ct->ondisk_path);
4705 free(ct->blob_id);
4706 free(ct->base_blob_id);
4707 free(ct->staged_blob_id);
4708 free(ct->base_commit_id);
4709 free(ct);
4712 struct collect_commitables_arg {
4713 struct got_pathlist_head *commitable_paths;
4714 struct got_repository *repo;
4715 struct got_worktree *worktree;
4716 struct got_fileindex *fileindex;
4717 int have_staged_files;
4718 int allow_bad_symlinks;
4721 static const struct got_error *
4722 collect_commitables(void *arg, unsigned char status,
4723 unsigned char staged_status, const char *relpath,
4724 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4725 struct got_object_id *commit_id, int dirfd, const char *de_name)
4727 struct collect_commitables_arg *a = arg;
4728 const struct got_error *err = NULL;
4729 struct got_commitable *ct = NULL;
4730 struct got_pathlist_entry *new = NULL;
4731 char *parent_path = NULL, *path = NULL;
4732 struct stat sb;
4734 if (a->have_staged_files) {
4735 if (staged_status != GOT_STATUS_MODIFY &&
4736 staged_status != GOT_STATUS_ADD &&
4737 staged_status != GOT_STATUS_DELETE)
4738 return NULL;
4739 } else {
4740 if (status == GOT_STATUS_CONFLICT)
4741 return got_error(GOT_ERR_COMMIT_CONFLICT);
4743 if (status != GOT_STATUS_MODIFY &&
4744 status != GOT_STATUS_MODE_CHANGE &&
4745 status != GOT_STATUS_ADD &&
4746 status != GOT_STATUS_DELETE)
4747 return NULL;
4750 if (asprintf(&path, "/%s", relpath) == -1) {
4751 err = got_error_from_errno("asprintf");
4752 goto done;
4754 if (strcmp(path, "/") == 0) {
4755 parent_path = strdup("");
4756 if (parent_path == NULL)
4757 return got_error_from_errno("strdup");
4758 } else {
4759 err = got_path_dirname(&parent_path, path);
4760 if (err)
4761 return err;
4764 ct = calloc(1, sizeof(*ct));
4765 if (ct == NULL) {
4766 err = got_error_from_errno("calloc");
4767 goto done;
4770 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4771 relpath) == -1) {
4772 err = got_error_from_errno("asprintf");
4773 goto done;
4776 if (staged_status == GOT_STATUS_ADD ||
4777 staged_status == GOT_STATUS_MODIFY) {
4778 struct got_fileindex_entry *ie;
4779 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4780 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4781 case GOT_FILEIDX_MODE_REGULAR_FILE:
4782 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4783 ct->mode = S_IFREG;
4784 break;
4785 case GOT_FILEIDX_MODE_SYMLINK:
4786 ct->mode = S_IFLNK;
4787 break;
4788 default:
4789 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4790 goto done;
4792 ct->mode |= got_fileindex_entry_perms_get(ie);
4793 } else if (status != GOT_STATUS_DELETE &&
4794 staged_status != GOT_STATUS_DELETE) {
4795 if (dirfd != -1) {
4796 if (fstatat(dirfd, de_name, &sb,
4797 AT_SYMLINK_NOFOLLOW) == -1) {
4798 err = got_error_from_errno2("fstatat",
4799 ct->ondisk_path);
4800 goto done;
4802 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4803 err = got_error_from_errno2("lstat", ct->ondisk_path);
4804 goto done;
4806 ct->mode = sb.st_mode;
4809 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4810 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4811 relpath) == -1) {
4812 err = got_error_from_errno("asprintf");
4813 goto done;
4816 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4817 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4818 int is_bad_symlink;
4819 char target_path[PATH_MAX];
4820 ssize_t target_len;
4821 target_len = readlink(ct->ondisk_path, target_path,
4822 sizeof(target_path));
4823 if (target_len == -1) {
4824 err = got_error_from_errno2("readlink",
4825 ct->ondisk_path);
4826 goto done;
4828 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4829 target_len, ct->ondisk_path, a->worktree->root_path);
4830 if (err)
4831 goto done;
4832 if (is_bad_symlink) {
4833 err = got_error_path(ct->ondisk_path,
4834 GOT_ERR_BAD_SYMLINK);
4835 goto done;
4840 ct->status = status;
4841 ct->staged_status = staged_status;
4842 ct->blob_id = NULL; /* will be filled in when blob gets created */
4843 if (ct->status != GOT_STATUS_ADD &&
4844 ct->staged_status != GOT_STATUS_ADD) {
4845 ct->base_blob_id = got_object_id_dup(blob_id);
4846 if (ct->base_blob_id == NULL) {
4847 err = got_error_from_errno("got_object_id_dup");
4848 goto done;
4850 ct->base_commit_id = got_object_id_dup(commit_id);
4851 if (ct->base_commit_id == NULL) {
4852 err = got_error_from_errno("got_object_id_dup");
4853 goto done;
4856 if (ct->staged_status == GOT_STATUS_ADD ||
4857 ct->staged_status == GOT_STATUS_MODIFY) {
4858 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4859 if (ct->staged_blob_id == NULL) {
4860 err = got_error_from_errno("got_object_id_dup");
4861 goto done;
4864 ct->path = strdup(path);
4865 if (ct->path == NULL) {
4866 err = got_error_from_errno("strdup");
4867 goto done;
4869 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4870 done:
4871 if (ct && (err || new == NULL))
4872 free_commitable(ct);
4873 free(parent_path);
4874 free(path);
4875 return err;
4878 static const struct got_error *write_tree(struct got_object_id **, int *,
4879 struct got_tree_object *, const char *, struct got_pathlist_head *,
4880 got_worktree_status_cb status_cb, void *status_arg,
4881 struct got_repository *);
4883 static const struct got_error *
4884 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4885 struct got_tree_entry *te, const char *parent_path,
4886 struct got_pathlist_head *commitable_paths,
4887 got_worktree_status_cb status_cb, void *status_arg,
4888 struct got_repository *repo)
4890 const struct got_error *err = NULL;
4891 struct got_tree_object *subtree;
4892 char *subpath;
4894 if (asprintf(&subpath, "%s%s%s", parent_path,
4895 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4896 return got_error_from_errno("asprintf");
4898 err = got_object_open_as_tree(&subtree, repo, &te->id);
4899 if (err)
4900 return err;
4902 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4903 commitable_paths, status_cb, status_arg, repo);
4904 got_object_tree_close(subtree);
4905 free(subpath);
4906 return err;
4909 static const struct got_error *
4910 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4912 const struct got_error *err = NULL;
4913 char *ct_parent_path = NULL;
4915 *match = 0;
4917 if (strchr(ct->in_repo_path, '/') == NULL) {
4918 *match = got_path_is_root_dir(path);
4919 return NULL;
4922 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4923 if (err)
4924 return err;
4925 *match = (strcmp(path, ct_parent_path) == 0);
4926 free(ct_parent_path);
4927 return err;
4930 static mode_t
4931 get_ct_file_mode(struct got_commitable *ct)
4933 if (S_ISLNK(ct->mode))
4934 return S_IFLNK;
4936 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4939 static const struct got_error *
4940 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4941 struct got_tree_entry *te, struct got_commitable *ct)
4943 const struct got_error *err = NULL;
4945 *new_te = NULL;
4947 err = got_object_tree_entry_dup(new_te, te);
4948 if (err)
4949 goto done;
4951 (*new_te)->mode = get_ct_file_mode(ct);
4953 if (ct->staged_status == GOT_STATUS_MODIFY)
4954 memcpy(&(*new_te)->id, ct->staged_blob_id,
4955 sizeof((*new_te)->id));
4956 else
4957 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4958 done:
4959 if (err && *new_te) {
4960 free(*new_te);
4961 *new_te = NULL;
4963 return err;
4966 static const struct got_error *
4967 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4968 struct got_commitable *ct)
4970 const struct got_error *err = NULL;
4971 char *ct_name = NULL;
4973 *new_te = NULL;
4975 *new_te = calloc(1, sizeof(**new_te));
4976 if (*new_te == NULL)
4977 return got_error_from_errno("calloc");
4979 err = got_path_basename(&ct_name, ct->path);
4980 if (err)
4981 goto done;
4982 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4983 sizeof((*new_te)->name)) {
4984 err = got_error(GOT_ERR_NO_SPACE);
4985 goto done;
4988 (*new_te)->mode = get_ct_file_mode(ct);
4990 if (ct->staged_status == GOT_STATUS_ADD)
4991 memcpy(&(*new_te)->id, ct->staged_blob_id,
4992 sizeof((*new_te)->id));
4993 else
4994 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4995 done:
4996 free(ct_name);
4997 if (err && *new_te) {
4998 free(*new_te);
4999 *new_te = NULL;
5001 return err;
5004 static const struct got_error *
5005 insert_tree_entry(struct got_tree_entry *new_te,
5006 struct got_pathlist_head *paths)
5008 const struct got_error *err = NULL;
5009 struct got_pathlist_entry *new_pe;
5011 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5012 if (err)
5013 return err;
5014 if (new_pe == NULL)
5015 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5016 return NULL;
5019 static const struct got_error *
5020 report_ct_status(struct got_commitable *ct,
5021 got_worktree_status_cb status_cb, void *status_arg)
5023 const char *ct_path = ct->path;
5024 unsigned char status;
5026 while (ct_path[0] == '/')
5027 ct_path++;
5029 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5030 status = ct->staged_status;
5031 else
5032 status = ct->status;
5034 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5035 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5038 static const struct got_error *
5039 match_modified_subtree(int *modified, struct got_tree_entry *te,
5040 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5042 const struct got_error *err = NULL;
5043 struct got_pathlist_entry *pe;
5044 char *te_path;
5046 *modified = 0;
5048 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5049 got_path_is_root_dir(base_tree_path) ? "" : "/",
5050 te->name) == -1)
5051 return got_error_from_errno("asprintf");
5053 TAILQ_FOREACH(pe, commitable_paths, entry) {
5054 struct got_commitable *ct = pe->data;
5055 *modified = got_path_is_child(ct->in_repo_path, te_path,
5056 strlen(te_path));
5057 if (*modified)
5058 break;
5061 free(te_path);
5062 return err;
5065 static const struct got_error *
5066 match_deleted_or_modified_ct(struct got_commitable **ctp,
5067 struct got_tree_entry *te, const char *base_tree_path,
5068 struct got_pathlist_head *commitable_paths)
5070 const struct got_error *err = NULL;
5071 struct got_pathlist_entry *pe;
5073 *ctp = NULL;
5075 TAILQ_FOREACH(pe, commitable_paths, entry) {
5076 struct got_commitable *ct = pe->data;
5077 char *ct_name = NULL;
5078 int path_matches;
5080 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5081 if (ct->status != GOT_STATUS_MODIFY &&
5082 ct->status != GOT_STATUS_MODE_CHANGE &&
5083 ct->status != GOT_STATUS_DELETE)
5084 continue;
5085 } else {
5086 if (ct->staged_status != GOT_STATUS_MODIFY &&
5087 ct->staged_status != GOT_STATUS_DELETE)
5088 continue;
5091 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5092 continue;
5094 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5095 if (err)
5096 return err;
5097 if (!path_matches)
5098 continue;
5100 err = got_path_basename(&ct_name, pe->path);
5101 if (err)
5102 return err;
5104 if (strcmp(te->name, ct_name) != 0) {
5105 free(ct_name);
5106 continue;
5108 free(ct_name);
5110 *ctp = ct;
5111 break;
5114 return err;
5117 static const struct got_error *
5118 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5119 const char *child_path, const char *path_base_tree,
5120 struct got_pathlist_head *commitable_paths,
5121 got_worktree_status_cb status_cb, void *status_arg,
5122 struct got_repository *repo)
5124 const struct got_error *err = NULL;
5125 struct got_tree_entry *new_te;
5126 char *subtree_path;
5127 struct got_object_id *id = NULL;
5128 int nentries;
5130 *new_tep = NULL;
5132 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5133 got_path_is_root_dir(path_base_tree) ? "" : "/",
5134 child_path) == -1)
5135 return got_error_from_errno("asprintf");
5137 new_te = calloc(1, sizeof(*new_te));
5138 if (new_te == NULL)
5139 return got_error_from_errno("calloc");
5140 new_te->mode = S_IFDIR;
5142 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5143 sizeof(new_te->name)) {
5144 err = got_error(GOT_ERR_NO_SPACE);
5145 goto done;
5147 err = write_tree(&id, &nentries, NULL, subtree_path,
5148 commitable_paths, status_cb, status_arg, repo);
5149 if (err) {
5150 free(new_te);
5151 goto done;
5153 memcpy(&new_te->id, id, sizeof(new_te->id));
5154 done:
5155 free(id);
5156 free(subtree_path);
5157 if (err == NULL)
5158 *new_tep = new_te;
5159 return err;
5162 static const struct got_error *
5163 write_tree(struct got_object_id **new_tree_id, int *nentries,
5164 struct got_tree_object *base_tree, const char *path_base_tree,
5165 struct got_pathlist_head *commitable_paths,
5166 got_worktree_status_cb status_cb, void *status_arg,
5167 struct got_repository *repo)
5169 const struct got_error *err = NULL;
5170 struct got_pathlist_head paths;
5171 struct got_tree_entry *te, *new_te = NULL;
5172 struct got_pathlist_entry *pe;
5174 TAILQ_INIT(&paths);
5175 *nentries = 0;
5177 /* Insert, and recurse into, newly added entries first. */
5178 TAILQ_FOREACH(pe, commitable_paths, entry) {
5179 struct got_commitable *ct = pe->data;
5180 char *child_path = NULL, *slash;
5182 if ((ct->status != GOT_STATUS_ADD &&
5183 ct->staged_status != GOT_STATUS_ADD) ||
5184 (ct->flags & GOT_COMMITABLE_ADDED))
5185 continue;
5187 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5188 strlen(path_base_tree)))
5189 continue;
5191 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5192 ct->in_repo_path);
5193 if (err)
5194 goto done;
5196 slash = strchr(child_path, '/');
5197 if (slash == NULL) {
5198 err = alloc_added_blob_tree_entry(&new_te, ct);
5199 if (err)
5200 goto done;
5201 err = report_ct_status(ct, status_cb, status_arg);
5202 if (err)
5203 goto done;
5204 ct->flags |= GOT_COMMITABLE_ADDED;
5205 err = insert_tree_entry(new_te, &paths);
5206 if (err)
5207 goto done;
5208 (*nentries)++;
5209 } else {
5210 *slash = '\0'; /* trim trailing path components */
5211 if (base_tree == NULL ||
5212 got_object_tree_find_entry(base_tree, child_path)
5213 == NULL) {
5214 err = make_subtree_for_added_blob(&new_te,
5215 child_path, path_base_tree,
5216 commitable_paths, status_cb, status_arg,
5217 repo);
5218 if (err)
5219 goto done;
5220 err = insert_tree_entry(new_te, &paths);
5221 if (err)
5222 goto done;
5223 (*nentries)++;
5228 if (base_tree) {
5229 int i, nbase_entries;
5230 /* Handle modified and deleted entries. */
5231 nbase_entries = got_object_tree_get_nentries(base_tree);
5232 for (i = 0; i < nbase_entries; i++) {
5233 struct got_commitable *ct = NULL;
5235 te = got_object_tree_get_entry(base_tree, i);
5236 if (got_object_tree_entry_is_submodule(te)) {
5237 /* Entry is a submodule; just copy it. */
5238 err = got_object_tree_entry_dup(&new_te, te);
5239 if (err)
5240 goto done;
5241 err = insert_tree_entry(new_te, &paths);
5242 if (err)
5243 goto done;
5244 (*nentries)++;
5245 continue;
5248 if (S_ISDIR(te->mode)) {
5249 int modified;
5250 err = got_object_tree_entry_dup(&new_te, te);
5251 if (err)
5252 goto done;
5253 err = match_modified_subtree(&modified, te,
5254 path_base_tree, commitable_paths);
5255 if (err)
5256 goto done;
5257 /* Avoid recursion into unmodified subtrees. */
5258 if (modified) {
5259 struct got_object_id *new_id;
5260 int nsubentries;
5261 err = write_subtree(&new_id,
5262 &nsubentries, te,
5263 path_base_tree, commitable_paths,
5264 status_cb, status_arg, repo);
5265 if (err)
5266 goto done;
5267 if (nsubentries == 0) {
5268 /* All entries were deleted. */
5269 free(new_id);
5270 continue;
5272 memcpy(&new_te->id, new_id,
5273 sizeof(new_te->id));
5274 free(new_id);
5276 err = insert_tree_entry(new_te, &paths);
5277 if (err)
5278 goto done;
5279 (*nentries)++;
5280 continue;
5283 err = match_deleted_or_modified_ct(&ct, te,
5284 path_base_tree, commitable_paths);
5285 if (err)
5286 goto done;
5287 if (ct) {
5288 /* NB: Deleted entries get dropped here. */
5289 if (ct->status == GOT_STATUS_MODIFY ||
5290 ct->status == GOT_STATUS_MODE_CHANGE ||
5291 ct->staged_status == GOT_STATUS_MODIFY) {
5292 err = alloc_modified_blob_tree_entry(
5293 &new_te, te, ct);
5294 if (err)
5295 goto done;
5296 err = insert_tree_entry(new_te, &paths);
5297 if (err)
5298 goto done;
5299 (*nentries)++;
5301 err = report_ct_status(ct, status_cb,
5302 status_arg);
5303 if (err)
5304 goto done;
5305 } else {
5306 /* Entry is unchanged; just copy it. */
5307 err = got_object_tree_entry_dup(&new_te, te);
5308 if (err)
5309 goto done;
5310 err = insert_tree_entry(new_te, &paths);
5311 if (err)
5312 goto done;
5313 (*nentries)++;
5318 /* Write new list of entries; deleted entries have been dropped. */
5319 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5320 done:
5321 got_pathlist_free(&paths);
5322 return err;
5325 static const struct got_error *
5326 update_fileindex_after_commit(struct got_worktree *worktree,
5327 struct got_pathlist_head *commitable_paths,
5328 struct got_object_id *new_base_commit_id,
5329 struct got_fileindex *fileindex, int have_staged_files)
5331 const struct got_error *err = NULL;
5332 struct got_pathlist_entry *pe;
5333 char *relpath = NULL;
5335 TAILQ_FOREACH(pe, commitable_paths, entry) {
5336 struct got_fileindex_entry *ie;
5337 struct got_commitable *ct = pe->data;
5339 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5341 err = got_path_skip_common_ancestor(&relpath,
5342 worktree->root_path, ct->ondisk_path);
5343 if (err)
5344 goto done;
5346 if (ie) {
5347 if (ct->status == GOT_STATUS_DELETE ||
5348 ct->staged_status == GOT_STATUS_DELETE) {
5349 got_fileindex_entry_remove(fileindex, ie);
5350 } else if (ct->staged_status == GOT_STATUS_ADD ||
5351 ct->staged_status == GOT_STATUS_MODIFY) {
5352 got_fileindex_entry_stage_set(ie,
5353 GOT_FILEIDX_STAGE_NONE);
5354 got_fileindex_entry_staged_filetype_set(ie, 0);
5356 err = got_fileindex_entry_update(ie,
5357 worktree->root_fd, relpath,
5358 ct->staged_blob_id->sha1,
5359 new_base_commit_id->sha1,
5360 !have_staged_files);
5361 } else
5362 err = got_fileindex_entry_update(ie,
5363 worktree->root_fd, relpath,
5364 ct->blob_id->sha1,
5365 new_base_commit_id->sha1,
5366 !have_staged_files);
5367 } else {
5368 err = got_fileindex_entry_alloc(&ie, pe->path);
5369 if (err)
5370 goto done;
5371 err = got_fileindex_entry_update(ie,
5372 worktree->root_fd, relpath, ct->blob_id->sha1,
5373 new_base_commit_id->sha1, 1);
5374 if (err) {
5375 got_fileindex_entry_free(ie);
5376 goto done;
5378 err = got_fileindex_entry_add(fileindex, ie);
5379 if (err) {
5380 got_fileindex_entry_free(ie);
5381 goto done;
5384 free(relpath);
5385 relpath = NULL;
5387 done:
5388 free(relpath);
5389 return err;
5393 static const struct got_error *
5394 check_out_of_date(const char *in_repo_path, unsigned char status,
5395 unsigned char staged_status, struct got_object_id *base_blob_id,
5396 struct got_object_id *base_commit_id,
5397 struct got_object_id *head_commit_id, struct got_repository *repo,
5398 int ood_errcode)
5400 const struct got_error *err = NULL;
5401 struct got_object_id *id = NULL;
5403 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5404 /* Trivial case: base commit == head commit */
5405 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5406 return NULL;
5408 * Ensure file content which local changes were based
5409 * on matches file content in the branch head.
5411 err = got_object_id_by_path(&id, repo, head_commit_id,
5412 in_repo_path);
5413 if (err) {
5414 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5415 err = got_error(ood_errcode);
5416 goto done;
5417 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5418 err = got_error(ood_errcode);
5419 } else {
5420 /* Require that added files don't exist in the branch head. */
5421 err = got_object_id_by_path(&id, repo, head_commit_id,
5422 in_repo_path);
5423 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5424 goto done;
5425 err = id ? got_error(ood_errcode) : NULL;
5427 done:
5428 free(id);
5429 return err;
5432 const struct got_error *
5433 commit_worktree(struct got_object_id **new_commit_id,
5434 struct got_pathlist_head *commitable_paths,
5435 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5436 const char *author, const char *committer,
5437 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5438 got_worktree_status_cb status_cb, void *status_arg,
5439 struct got_repository *repo)
5441 const struct got_error *err = NULL, *unlockerr = NULL;
5442 struct got_pathlist_entry *pe;
5443 const char *head_ref_name = NULL;
5444 struct got_commit_object *head_commit = NULL;
5445 struct got_reference *head_ref2 = NULL;
5446 struct got_object_id *head_commit_id2 = NULL;
5447 struct got_tree_object *head_tree = NULL;
5448 struct got_object_id *new_tree_id = NULL;
5449 int nentries;
5450 struct got_object_id_queue parent_ids;
5451 struct got_object_qid *pid = NULL;
5452 char *logmsg = NULL;
5454 *new_commit_id = NULL;
5456 SIMPLEQ_INIT(&parent_ids);
5458 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5459 if (err)
5460 goto done;
5462 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5463 if (err)
5464 goto done;
5466 if (commit_msg_cb != NULL) {
5467 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5468 if (err)
5469 goto done;
5472 if (logmsg == NULL || strlen(logmsg) == 0) {
5473 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5474 goto done;
5477 /* Create blobs from added and modified files and record their IDs. */
5478 TAILQ_FOREACH(pe, commitable_paths, entry) {
5479 struct got_commitable *ct = pe->data;
5480 char *ondisk_path;
5482 /* Blobs for staged files already exist. */
5483 if (ct->staged_status == GOT_STATUS_ADD ||
5484 ct->staged_status == GOT_STATUS_MODIFY)
5485 continue;
5487 if (ct->status != GOT_STATUS_ADD &&
5488 ct->status != GOT_STATUS_MODIFY &&
5489 ct->status != GOT_STATUS_MODE_CHANGE)
5490 continue;
5492 if (asprintf(&ondisk_path, "%s/%s",
5493 worktree->root_path, pe->path) == -1) {
5494 err = got_error_from_errno("asprintf");
5495 goto done;
5497 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5498 free(ondisk_path);
5499 if (err)
5500 goto done;
5503 /* Recursively write new tree objects. */
5504 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5505 commitable_paths, status_cb, status_arg, repo);
5506 if (err)
5507 goto done;
5509 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5510 if (err)
5511 goto done;
5512 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5513 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5514 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5515 got_object_qid_free(pid);
5516 if (logmsg != NULL)
5517 free(logmsg);
5518 if (err)
5519 goto done;
5521 /* Check if a concurrent commit to our branch has occurred. */
5522 head_ref_name = got_worktree_get_head_ref_name(worktree);
5523 if (head_ref_name == NULL) {
5524 err = got_error_from_errno("got_worktree_get_head_ref_name");
5525 goto done;
5527 /* Lock the reference here to prevent concurrent modification. */
5528 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5529 if (err)
5530 goto done;
5531 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5532 if (err)
5533 goto done;
5534 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5535 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5536 goto done;
5538 /* Update branch head in repository. */
5539 err = got_ref_change_ref(head_ref2, *new_commit_id);
5540 if (err)
5541 goto done;
5542 err = got_ref_write(head_ref2, repo);
5543 if (err)
5544 goto done;
5546 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5547 if (err)
5548 goto done;
5550 err = ref_base_commit(worktree, repo);
5551 if (err)
5552 goto done;
5553 done:
5554 if (head_tree)
5555 got_object_tree_close(head_tree);
5556 if (head_commit)
5557 got_object_commit_close(head_commit);
5558 free(head_commit_id2);
5559 if (head_ref2) {
5560 unlockerr = got_ref_unlock(head_ref2);
5561 if (unlockerr && err == NULL)
5562 err = unlockerr;
5563 got_ref_close(head_ref2);
5565 return err;
5568 static const struct got_error *
5569 check_path_is_commitable(const char *path,
5570 struct got_pathlist_head *commitable_paths)
5572 struct got_pathlist_entry *cpe = NULL;
5573 size_t path_len = strlen(path);
5575 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5576 struct got_commitable *ct = cpe->data;
5577 const char *ct_path = ct->path;
5579 while (ct_path[0] == '/')
5580 ct_path++;
5582 if (strcmp(path, ct_path) == 0 ||
5583 got_path_is_child(ct_path, path, path_len))
5584 break;
5587 if (cpe == NULL)
5588 return got_error_path(path, GOT_ERR_BAD_PATH);
5590 return NULL;
5593 static const struct got_error *
5594 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5596 int *have_staged_files = arg;
5598 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5599 *have_staged_files = 1;
5600 return got_error(GOT_ERR_CANCELLED);
5603 return NULL;
5606 static const struct got_error *
5607 check_non_staged_files(struct got_fileindex *fileindex,
5608 struct got_pathlist_head *paths)
5610 struct got_pathlist_entry *pe;
5611 struct got_fileindex_entry *ie;
5613 TAILQ_FOREACH(pe, paths, entry) {
5614 if (pe->path[0] == '\0')
5615 continue;
5616 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5617 if (ie == NULL)
5618 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5619 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5620 return got_error_path(pe->path,
5621 GOT_ERR_FILE_NOT_STAGED);
5624 return NULL;
5627 const struct got_error *
5628 got_worktree_commit(struct got_object_id **new_commit_id,
5629 struct got_worktree *worktree, struct got_pathlist_head *paths,
5630 const char *author, const char *committer, int allow_bad_symlinks,
5631 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5632 got_worktree_status_cb status_cb, void *status_arg,
5633 struct got_repository *repo)
5635 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5636 struct got_fileindex *fileindex = NULL;
5637 char *fileindex_path = NULL;
5638 struct got_pathlist_head commitable_paths;
5639 struct collect_commitables_arg cc_arg;
5640 struct got_pathlist_entry *pe;
5641 struct got_reference *head_ref = NULL;
5642 struct got_object_id *head_commit_id = NULL;
5643 int have_staged_files = 0;
5645 *new_commit_id = NULL;
5647 TAILQ_INIT(&commitable_paths);
5649 err = lock_worktree(worktree, LOCK_EX);
5650 if (err)
5651 goto done;
5653 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5654 if (err)
5655 goto done;
5657 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5658 if (err)
5659 goto done;
5661 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5662 if (err)
5663 goto done;
5665 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5666 &have_staged_files);
5667 if (err && err->code != GOT_ERR_CANCELLED)
5668 goto done;
5669 if (have_staged_files) {
5670 err = check_non_staged_files(fileindex, paths);
5671 if (err)
5672 goto done;
5675 cc_arg.commitable_paths = &commitable_paths;
5676 cc_arg.worktree = worktree;
5677 cc_arg.fileindex = fileindex;
5678 cc_arg.repo = repo;
5679 cc_arg.have_staged_files = have_staged_files;
5680 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5681 TAILQ_FOREACH(pe, paths, entry) {
5682 err = worktree_status(worktree, pe->path, fileindex, repo,
5683 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5684 if (err)
5685 goto done;
5688 if (TAILQ_EMPTY(&commitable_paths)) {
5689 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5690 goto done;
5693 TAILQ_FOREACH(pe, paths, entry) {
5694 err = check_path_is_commitable(pe->path, &commitable_paths);
5695 if (err)
5696 goto done;
5699 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5700 struct got_commitable *ct = pe->data;
5701 const char *ct_path = ct->in_repo_path;
5703 while (ct_path[0] == '/')
5704 ct_path++;
5705 err = check_out_of_date(ct_path, ct->status,
5706 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5707 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5708 if (err)
5709 goto done;
5713 err = commit_worktree(new_commit_id, &commitable_paths,
5714 head_commit_id, worktree, author, committer,
5715 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5716 if (err)
5717 goto done;
5719 err = update_fileindex_after_commit(worktree, &commitable_paths,
5720 *new_commit_id, fileindex, have_staged_files);
5721 sync_err = sync_fileindex(fileindex, fileindex_path);
5722 if (sync_err && err == NULL)
5723 err = sync_err;
5724 done:
5725 if (fileindex)
5726 got_fileindex_free(fileindex);
5727 free(fileindex_path);
5728 unlockerr = lock_worktree(worktree, LOCK_SH);
5729 if (unlockerr && err == NULL)
5730 err = unlockerr;
5731 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5732 struct got_commitable *ct = pe->data;
5733 free_commitable(ct);
5735 got_pathlist_free(&commitable_paths);
5736 return err;
5739 const char *
5740 got_commitable_get_path(struct got_commitable *ct)
5742 return ct->path;
5745 unsigned int
5746 got_commitable_get_status(struct got_commitable *ct)
5748 return ct->status;
5751 struct check_rebase_ok_arg {
5752 struct got_worktree *worktree;
5753 struct got_repository *repo;
5756 static const struct got_error *
5757 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5759 const struct got_error *err = NULL;
5760 struct check_rebase_ok_arg *a = arg;
5761 unsigned char status;
5762 struct stat sb;
5763 char *ondisk_path;
5765 /* Reject rebase of a work tree with mixed base commits. */
5766 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5767 SHA1_DIGEST_LENGTH))
5768 return got_error(GOT_ERR_MIXED_COMMITS);
5770 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5771 == -1)
5772 return got_error_from_errno("asprintf");
5774 /* Reject rebase of a work tree with modified or staged files. */
5775 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5776 free(ondisk_path);
5777 if (err)
5778 return err;
5780 if (status != GOT_STATUS_NO_CHANGE)
5781 return got_error(GOT_ERR_MODIFIED);
5782 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5783 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5785 return NULL;
5788 const struct got_error *
5789 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5790 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5791 struct got_worktree *worktree, struct got_reference *branch,
5792 struct got_repository *repo)
5794 const struct got_error *err = NULL;
5795 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5796 char *branch_ref_name = NULL;
5797 char *fileindex_path = NULL;
5798 struct check_rebase_ok_arg ok_arg;
5799 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5800 struct got_object_id *wt_branch_tip = NULL;
5802 *new_base_branch_ref = NULL;
5803 *tmp_branch = NULL;
5804 *fileindex = NULL;
5806 err = lock_worktree(worktree, LOCK_EX);
5807 if (err)
5808 return err;
5810 err = open_fileindex(fileindex, &fileindex_path, worktree);
5811 if (err)
5812 goto done;
5814 ok_arg.worktree = worktree;
5815 ok_arg.repo = repo;
5816 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5817 &ok_arg);
5818 if (err)
5819 goto done;
5821 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5822 if (err)
5823 goto done;
5825 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5826 if (err)
5827 goto done;
5829 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5830 if (err)
5831 goto done;
5833 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5834 0);
5835 if (err)
5836 goto done;
5838 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5839 if (err)
5840 goto done;
5841 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5842 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5843 goto done;
5846 err = got_ref_alloc_symref(new_base_branch_ref,
5847 new_base_branch_ref_name, wt_branch);
5848 if (err)
5849 goto done;
5850 err = got_ref_write(*new_base_branch_ref, repo);
5851 if (err)
5852 goto done;
5854 /* TODO Lock original branch's ref while rebasing? */
5856 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5857 if (err)
5858 goto done;
5860 err = got_ref_write(branch_ref, repo);
5861 if (err)
5862 goto done;
5864 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5865 worktree->base_commit_id);
5866 if (err)
5867 goto done;
5868 err = got_ref_write(*tmp_branch, repo);
5869 if (err)
5870 goto done;
5872 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5873 if (err)
5874 goto done;
5875 done:
5876 free(fileindex_path);
5877 free(tmp_branch_name);
5878 free(new_base_branch_ref_name);
5879 free(branch_ref_name);
5880 if (branch_ref)
5881 got_ref_close(branch_ref);
5882 if (wt_branch)
5883 got_ref_close(wt_branch);
5884 free(wt_branch_tip);
5885 if (err) {
5886 if (*new_base_branch_ref) {
5887 got_ref_close(*new_base_branch_ref);
5888 *new_base_branch_ref = NULL;
5890 if (*tmp_branch) {
5891 got_ref_close(*tmp_branch);
5892 *tmp_branch = NULL;
5894 if (*fileindex) {
5895 got_fileindex_free(*fileindex);
5896 *fileindex = NULL;
5898 lock_worktree(worktree, LOCK_SH);
5900 return err;
5903 const struct got_error *
5904 got_worktree_rebase_continue(struct got_object_id **commit_id,
5905 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5906 struct got_reference **branch, struct got_fileindex **fileindex,
5907 struct got_worktree *worktree, struct got_repository *repo)
5909 const struct got_error *err;
5910 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5911 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5912 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5913 char *fileindex_path = NULL;
5914 int have_staged_files = 0;
5916 *commit_id = NULL;
5917 *new_base_branch = NULL;
5918 *tmp_branch = NULL;
5919 *branch = NULL;
5920 *fileindex = NULL;
5922 err = lock_worktree(worktree, LOCK_EX);
5923 if (err)
5924 return err;
5926 err = open_fileindex(fileindex, &fileindex_path, worktree);
5927 if (err)
5928 goto done;
5930 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5931 &have_staged_files);
5932 if (err && err->code != GOT_ERR_CANCELLED)
5933 goto done;
5934 if (have_staged_files) {
5935 err = got_error(GOT_ERR_STAGED_PATHS);
5936 goto done;
5939 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5940 if (err)
5941 goto done;
5943 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5944 if (err)
5945 goto done;
5947 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5948 if (err)
5949 goto done;
5951 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5952 if (err)
5953 goto done;
5955 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5956 if (err)
5957 goto done;
5959 err = got_ref_open(branch, repo,
5960 got_ref_get_symref_target(branch_ref), 0);
5961 if (err)
5962 goto done;
5964 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5965 if (err)
5966 goto done;
5968 err = got_ref_resolve(commit_id, repo, commit_ref);
5969 if (err)
5970 goto done;
5972 err = got_ref_open(new_base_branch, repo,
5973 new_base_branch_ref_name, 0);
5974 if (err)
5975 goto done;
5977 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5978 if (err)
5979 goto done;
5980 done:
5981 free(commit_ref_name);
5982 free(branch_ref_name);
5983 free(fileindex_path);
5984 if (commit_ref)
5985 got_ref_close(commit_ref);
5986 if (branch_ref)
5987 got_ref_close(branch_ref);
5988 if (err) {
5989 free(*commit_id);
5990 *commit_id = NULL;
5991 if (*tmp_branch) {
5992 got_ref_close(*tmp_branch);
5993 *tmp_branch = NULL;
5995 if (*new_base_branch) {
5996 got_ref_close(*new_base_branch);
5997 *new_base_branch = NULL;
5999 if (*branch) {
6000 got_ref_close(*branch);
6001 *branch = NULL;
6003 if (*fileindex) {
6004 got_fileindex_free(*fileindex);
6005 *fileindex = NULL;
6007 lock_worktree(worktree, LOCK_SH);
6009 return err;
6012 const struct got_error *
6013 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6015 const struct got_error *err;
6016 char *tmp_branch_name = NULL;
6018 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6019 if (err)
6020 return err;
6022 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6023 free(tmp_branch_name);
6024 return NULL;
6027 static const struct got_error *
6028 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6029 char **logmsg, void *arg)
6031 *logmsg = arg;
6032 return NULL;
6035 static const struct got_error *
6036 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6037 const char *path, struct got_object_id *blob_id,
6038 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6039 int dirfd, const char *de_name)
6041 return NULL;
6044 struct collect_merged_paths_arg {
6045 got_worktree_checkout_cb progress_cb;
6046 void *progress_arg;
6047 struct got_pathlist_head *merged_paths;
6050 static const struct got_error *
6051 collect_merged_paths(void *arg, unsigned char status, const char *path)
6053 const struct got_error *err;
6054 struct collect_merged_paths_arg *a = arg;
6055 char *p;
6056 struct got_pathlist_entry *new;
6058 err = (*a->progress_cb)(a->progress_arg, status, path);
6059 if (err)
6060 return err;
6062 if (status != GOT_STATUS_MERGE &&
6063 status != GOT_STATUS_ADD &&
6064 status != GOT_STATUS_DELETE &&
6065 status != GOT_STATUS_CONFLICT)
6066 return NULL;
6068 p = strdup(path);
6069 if (p == NULL)
6070 return got_error_from_errno("strdup");
6072 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6073 if (err || new == NULL)
6074 free(p);
6075 return err;
6078 void
6079 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6081 struct got_pathlist_entry *pe;
6083 TAILQ_FOREACH(pe, merged_paths, entry)
6084 free((char *)pe->path);
6086 got_pathlist_free(merged_paths);
6089 static const struct got_error *
6090 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6091 int is_rebase, struct got_repository *repo)
6093 const struct got_error *err;
6094 struct got_reference *commit_ref = NULL;
6096 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6097 if (err) {
6098 if (err->code != GOT_ERR_NOT_REF)
6099 goto done;
6100 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6101 if (err)
6102 goto done;
6103 err = got_ref_write(commit_ref, repo);
6104 if (err)
6105 goto done;
6106 } else if (is_rebase) {
6107 struct got_object_id *stored_id;
6108 int cmp;
6110 err = got_ref_resolve(&stored_id, repo, commit_ref);
6111 if (err)
6112 goto done;
6113 cmp = got_object_id_cmp(commit_id, stored_id);
6114 free(stored_id);
6115 if (cmp != 0) {
6116 err = got_error(GOT_ERR_REBASE_COMMITID);
6117 goto done;
6120 done:
6121 if (commit_ref)
6122 got_ref_close(commit_ref);
6123 return err;
6126 static const struct got_error *
6127 rebase_merge_files(struct got_pathlist_head *merged_paths,
6128 const char *commit_ref_name, struct got_worktree *worktree,
6129 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6130 struct got_object_id *commit_id, struct got_repository *repo,
6131 got_worktree_checkout_cb progress_cb, void *progress_arg,
6132 got_cancel_cb cancel_cb, void *cancel_arg)
6134 const struct got_error *err;
6135 struct got_reference *commit_ref = NULL;
6136 struct collect_merged_paths_arg cmp_arg;
6137 char *fileindex_path;
6139 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6141 err = get_fileindex_path(&fileindex_path, worktree);
6142 if (err)
6143 return err;
6145 cmp_arg.progress_cb = progress_cb;
6146 cmp_arg.progress_arg = progress_arg;
6147 cmp_arg.merged_paths = merged_paths;
6148 err = merge_files(worktree, fileindex, fileindex_path,
6149 parent_commit_id, commit_id, repo, collect_merged_paths,
6150 &cmp_arg, cancel_cb, cancel_arg);
6151 if (commit_ref)
6152 got_ref_close(commit_ref);
6153 return err;
6156 const struct got_error *
6157 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6158 struct got_worktree *worktree, struct got_fileindex *fileindex,
6159 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6160 struct got_repository *repo,
6161 got_worktree_checkout_cb progress_cb, void *progress_arg,
6162 got_cancel_cb cancel_cb, void *cancel_arg)
6164 const struct got_error *err;
6165 char *commit_ref_name;
6167 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6168 if (err)
6169 return err;
6171 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6172 if (err)
6173 goto done;
6175 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6176 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6177 progress_arg, cancel_cb, cancel_arg);
6178 done:
6179 free(commit_ref_name);
6180 return err;
6183 const struct got_error *
6184 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6185 struct got_worktree *worktree, struct got_fileindex *fileindex,
6186 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6187 struct got_repository *repo,
6188 got_worktree_checkout_cb progress_cb, void *progress_arg,
6189 got_cancel_cb cancel_cb, void *cancel_arg)
6191 const struct got_error *err;
6192 char *commit_ref_name;
6194 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6195 if (err)
6196 return err;
6198 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6199 if (err)
6200 goto done;
6202 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6203 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6204 progress_arg, cancel_cb, cancel_arg);
6205 done:
6206 free(commit_ref_name);
6207 return err;
6210 static const struct got_error *
6211 rebase_commit(struct got_object_id **new_commit_id,
6212 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6213 struct got_worktree *worktree, struct got_fileindex *fileindex,
6214 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6215 const char *new_logmsg, struct got_repository *repo)
6217 const struct got_error *err, *sync_err;
6218 struct got_pathlist_head commitable_paths;
6219 struct collect_commitables_arg cc_arg;
6220 char *fileindex_path = NULL;
6221 struct got_reference *head_ref = NULL;
6222 struct got_object_id *head_commit_id = NULL;
6223 char *logmsg = NULL;
6225 TAILQ_INIT(&commitable_paths);
6226 *new_commit_id = NULL;
6228 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6230 err = get_fileindex_path(&fileindex_path, worktree);
6231 if (err)
6232 return err;
6234 cc_arg.commitable_paths = &commitable_paths;
6235 cc_arg.worktree = worktree;
6236 cc_arg.repo = repo;
6237 cc_arg.have_staged_files = 0;
6239 * If possible get the status of individual files directly to
6240 * avoid crawling the entire work tree once per rebased commit.
6241 * TODO: Ideally, merged_paths would contain a list of commitables
6242 * we could use so we could skip worktree_status() entirely.
6244 if (merged_paths) {
6245 struct got_pathlist_entry *pe;
6246 TAILQ_FOREACH(pe, merged_paths, entry) {
6247 err = worktree_status(worktree, pe->path, fileindex,
6248 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6249 0);
6250 if (err)
6251 goto done;
6253 } else {
6254 err = worktree_status(worktree, "", fileindex, repo,
6255 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6256 if (err)
6257 goto done;
6260 if (TAILQ_EMPTY(&commitable_paths)) {
6261 /* No-op change; commit will be elided. */
6262 err = got_ref_delete(commit_ref, repo);
6263 if (err)
6264 goto done;
6265 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6266 goto done;
6269 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6270 if (err)
6271 goto done;
6273 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6274 if (err)
6275 goto done;
6277 if (new_logmsg) {
6278 logmsg = strdup(new_logmsg);
6279 if (logmsg == NULL) {
6280 err = got_error_from_errno("strdup");
6281 goto done;
6283 } else {
6284 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6285 if (err)
6286 goto done;
6289 /* NB: commit_worktree will call free(logmsg) */
6290 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6291 worktree, got_object_commit_get_author(orig_commit),
6292 got_object_commit_get_committer(orig_commit),
6293 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6294 if (err)
6295 goto done;
6297 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6298 if (err)
6299 goto done;
6301 err = got_ref_delete(commit_ref, repo);
6302 if (err)
6303 goto done;
6305 err = update_fileindex_after_commit(worktree, &commitable_paths,
6306 *new_commit_id, fileindex, 0);
6307 sync_err = sync_fileindex(fileindex, fileindex_path);
6308 if (sync_err && err == NULL)
6309 err = sync_err;
6310 done:
6311 free(fileindex_path);
6312 free(head_commit_id);
6313 if (head_ref)
6314 got_ref_close(head_ref);
6315 if (err) {
6316 free(*new_commit_id);
6317 *new_commit_id = NULL;
6319 return err;
6322 const struct got_error *
6323 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6324 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6325 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6326 struct got_commit_object *orig_commit,
6327 struct got_object_id *orig_commit_id, struct got_repository *repo)
6329 const struct got_error *err;
6330 char *commit_ref_name;
6331 struct got_reference *commit_ref = NULL;
6332 struct got_object_id *commit_id = NULL;
6334 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6335 if (err)
6336 return err;
6338 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6339 if (err)
6340 goto done;
6341 err = got_ref_resolve(&commit_id, repo, commit_ref);
6342 if (err)
6343 goto done;
6344 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6345 err = got_error(GOT_ERR_REBASE_COMMITID);
6346 goto done;
6349 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6350 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6351 done:
6352 if (commit_ref)
6353 got_ref_close(commit_ref);
6354 free(commit_ref_name);
6355 free(commit_id);
6356 return err;
6359 const struct got_error *
6360 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6361 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6362 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6363 struct got_commit_object *orig_commit,
6364 struct got_object_id *orig_commit_id, const char *new_logmsg,
6365 struct got_repository *repo)
6367 const struct got_error *err;
6368 char *commit_ref_name;
6369 struct got_reference *commit_ref = NULL;
6371 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6372 if (err)
6373 return err;
6375 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6376 if (err)
6377 goto done;
6379 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6380 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6381 done:
6382 if (commit_ref)
6383 got_ref_close(commit_ref);
6384 free(commit_ref_name);
6385 return err;
6388 const struct got_error *
6389 got_worktree_rebase_postpone(struct got_worktree *worktree,
6390 struct got_fileindex *fileindex)
6392 if (fileindex)
6393 got_fileindex_free(fileindex);
6394 return lock_worktree(worktree, LOCK_SH);
6397 static const struct got_error *
6398 delete_ref(const char *name, struct got_repository *repo)
6400 const struct got_error *err;
6401 struct got_reference *ref;
6403 err = got_ref_open(&ref, repo, name, 0);
6404 if (err) {
6405 if (err->code == GOT_ERR_NOT_REF)
6406 return NULL;
6407 return err;
6410 err = got_ref_delete(ref, repo);
6411 got_ref_close(ref);
6412 return err;
6415 static const struct got_error *
6416 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6418 const struct got_error *err;
6419 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6420 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6422 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6423 if (err)
6424 goto done;
6425 err = delete_ref(tmp_branch_name, repo);
6426 if (err)
6427 goto done;
6429 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6430 if (err)
6431 goto done;
6432 err = delete_ref(new_base_branch_ref_name, repo);
6433 if (err)
6434 goto done;
6436 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6437 if (err)
6438 goto done;
6439 err = delete_ref(branch_ref_name, repo);
6440 if (err)
6441 goto done;
6443 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6444 if (err)
6445 goto done;
6446 err = delete_ref(commit_ref_name, repo);
6447 if (err)
6448 goto done;
6450 done:
6451 free(tmp_branch_name);
6452 free(new_base_branch_ref_name);
6453 free(branch_ref_name);
6454 free(commit_ref_name);
6455 return err;
6458 const struct got_error *
6459 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6460 struct got_object_id *new_commit_id, struct got_repository *repo)
6462 const struct got_error *err;
6463 struct got_reference *ref = NULL;
6464 struct got_object_id *old_commit_id = NULL;
6465 const char *branch_name = NULL;
6466 char *new_id_str = NULL;
6467 char *refname = NULL;
6469 branch_name = got_ref_get_name(branch);
6470 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6471 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6472 branch_name += 11;
6474 err = got_object_id_str(&new_id_str, new_commit_id);
6475 if (err)
6476 return err;
6478 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6479 new_id_str) == -1) {
6480 err = got_error_from_errno("asprintf");
6481 goto done;
6484 err = got_ref_resolve(&old_commit_id, repo, branch);
6485 if (err)
6486 goto done;
6488 err = got_ref_alloc(&ref, refname, old_commit_id);
6489 if (err)
6490 goto done;
6492 err = got_ref_write(ref, repo);
6493 done:
6494 free(new_id_str);
6495 free(refname);
6496 free(old_commit_id);
6497 if (ref)
6498 got_ref_close(ref);
6499 return err;
6502 const struct got_error *
6503 got_worktree_rebase_complete(struct got_worktree *worktree,
6504 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6505 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6506 struct got_repository *repo, int create_backup)
6508 const struct got_error *err, *unlockerr, *sync_err;
6509 struct got_object_id *new_head_commit_id = NULL;
6510 char *fileindex_path = NULL;
6512 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6513 if (err)
6514 return err;
6516 if (create_backup) {
6517 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6518 rebased_branch, new_head_commit_id, repo);
6519 if (err)
6520 goto done;
6523 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6524 if (err)
6525 goto done;
6527 err = got_ref_write(rebased_branch, repo);
6528 if (err)
6529 goto done;
6531 err = got_worktree_set_head_ref(worktree, rebased_branch);
6532 if (err)
6533 goto done;
6535 err = delete_rebase_refs(worktree, repo);
6536 if (err)
6537 goto done;
6539 err = get_fileindex_path(&fileindex_path, worktree);
6540 if (err)
6541 goto done;
6542 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6543 sync_err = sync_fileindex(fileindex, fileindex_path);
6544 if (sync_err && err == NULL)
6545 err = sync_err;
6546 done:
6547 got_fileindex_free(fileindex);
6548 free(fileindex_path);
6549 free(new_head_commit_id);
6550 unlockerr = lock_worktree(worktree, LOCK_SH);
6551 if (unlockerr && err == NULL)
6552 err = unlockerr;
6553 return err;
6556 const struct got_error *
6557 got_worktree_rebase_abort(struct got_worktree *worktree,
6558 struct got_fileindex *fileindex, struct got_repository *repo,
6559 struct got_reference *new_base_branch,
6560 got_worktree_checkout_cb progress_cb, void *progress_arg)
6562 const struct got_error *err, *unlockerr, *sync_err;
6563 struct got_reference *resolved = NULL;
6564 struct got_object_id *commit_id = NULL;
6565 char *fileindex_path = NULL;
6566 struct revert_file_args rfa;
6567 struct got_object_id *tree_id = NULL;
6569 err = lock_worktree(worktree, LOCK_EX);
6570 if (err)
6571 return err;
6573 err = got_ref_open(&resolved, repo,
6574 got_ref_get_symref_target(new_base_branch), 0);
6575 if (err)
6576 goto done;
6578 err = got_worktree_set_head_ref(worktree, resolved);
6579 if (err)
6580 goto done;
6583 * XXX commits to the base branch could have happened while
6584 * we were busy rebasing; should we store the original commit ID
6585 * when rebase begins and read it back here?
6587 err = got_ref_resolve(&commit_id, repo, resolved);
6588 if (err)
6589 goto done;
6591 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6592 if (err)
6593 goto done;
6595 err = got_object_id_by_path(&tree_id, repo,
6596 worktree->base_commit_id, worktree->path_prefix);
6597 if (err)
6598 goto done;
6600 err = delete_rebase_refs(worktree, repo);
6601 if (err)
6602 goto done;
6604 err = get_fileindex_path(&fileindex_path, worktree);
6605 if (err)
6606 goto done;
6608 rfa.worktree = worktree;
6609 rfa.fileindex = fileindex;
6610 rfa.progress_cb = progress_cb;
6611 rfa.progress_arg = progress_arg;
6612 rfa.patch_cb = NULL;
6613 rfa.patch_arg = NULL;
6614 rfa.repo = repo;
6615 err = worktree_status(worktree, "", fileindex, repo,
6616 revert_file, &rfa, NULL, NULL, 0, 0);
6617 if (err)
6618 goto sync;
6620 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6621 repo, progress_cb, progress_arg, NULL, NULL);
6622 sync:
6623 sync_err = sync_fileindex(fileindex, fileindex_path);
6624 if (sync_err && err == NULL)
6625 err = sync_err;
6626 done:
6627 got_ref_close(resolved);
6628 free(tree_id);
6629 free(commit_id);
6630 if (fileindex)
6631 got_fileindex_free(fileindex);
6632 free(fileindex_path);
6634 unlockerr = lock_worktree(worktree, LOCK_SH);
6635 if (unlockerr && err == NULL)
6636 err = unlockerr;
6637 return err;
6640 const struct got_error *
6641 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6642 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6643 struct got_fileindex **fileindex, struct got_worktree *worktree,
6644 struct got_repository *repo)
6646 const struct got_error *err = NULL;
6647 char *tmp_branch_name = NULL;
6648 char *branch_ref_name = NULL;
6649 char *base_commit_ref_name = NULL;
6650 char *fileindex_path = NULL;
6651 struct check_rebase_ok_arg ok_arg;
6652 struct got_reference *wt_branch = NULL;
6653 struct got_reference *base_commit_ref = NULL;
6655 *tmp_branch = NULL;
6656 *branch_ref = NULL;
6657 *base_commit_id = NULL;
6658 *fileindex = NULL;
6660 err = lock_worktree(worktree, LOCK_EX);
6661 if (err)
6662 return err;
6664 err = open_fileindex(fileindex, &fileindex_path, worktree);
6665 if (err)
6666 goto done;
6668 ok_arg.worktree = worktree;
6669 ok_arg.repo = repo;
6670 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6671 &ok_arg);
6672 if (err)
6673 goto done;
6675 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6676 if (err)
6677 goto done;
6679 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6680 if (err)
6681 goto done;
6683 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6684 worktree);
6685 if (err)
6686 goto done;
6688 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6689 0);
6690 if (err)
6691 goto done;
6693 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6694 if (err)
6695 goto done;
6697 err = got_ref_write(*branch_ref, repo);
6698 if (err)
6699 goto done;
6701 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6702 worktree->base_commit_id);
6703 if (err)
6704 goto done;
6705 err = got_ref_write(base_commit_ref, repo);
6706 if (err)
6707 goto done;
6708 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6709 if (*base_commit_id == NULL) {
6710 err = got_error_from_errno("got_object_id_dup");
6711 goto done;
6714 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6715 worktree->base_commit_id);
6716 if (err)
6717 goto done;
6718 err = got_ref_write(*tmp_branch, repo);
6719 if (err)
6720 goto done;
6722 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6723 if (err)
6724 goto done;
6725 done:
6726 free(fileindex_path);
6727 free(tmp_branch_name);
6728 free(branch_ref_name);
6729 free(base_commit_ref_name);
6730 if (wt_branch)
6731 got_ref_close(wt_branch);
6732 if (err) {
6733 if (*branch_ref) {
6734 got_ref_close(*branch_ref);
6735 *branch_ref = NULL;
6737 if (*tmp_branch) {
6738 got_ref_close(*tmp_branch);
6739 *tmp_branch = NULL;
6741 free(*base_commit_id);
6742 if (*fileindex) {
6743 got_fileindex_free(*fileindex);
6744 *fileindex = NULL;
6746 lock_worktree(worktree, LOCK_SH);
6748 return err;
6751 const struct got_error *
6752 got_worktree_histedit_postpone(struct got_worktree *worktree,
6753 struct got_fileindex *fileindex)
6755 if (fileindex)
6756 got_fileindex_free(fileindex);
6757 return lock_worktree(worktree, LOCK_SH);
6760 const struct got_error *
6761 got_worktree_histedit_in_progress(int *in_progress,
6762 struct got_worktree *worktree)
6764 const struct got_error *err;
6765 char *tmp_branch_name = NULL;
6767 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6768 if (err)
6769 return err;
6771 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6772 free(tmp_branch_name);
6773 return NULL;
6776 const struct got_error *
6777 got_worktree_histedit_continue(struct got_object_id **commit_id,
6778 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6779 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6780 struct got_worktree *worktree, struct got_repository *repo)
6782 const struct got_error *err;
6783 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6784 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6785 struct got_reference *commit_ref = NULL;
6786 struct got_reference *base_commit_ref = NULL;
6787 char *fileindex_path = NULL;
6788 int have_staged_files = 0;
6790 *commit_id = NULL;
6791 *tmp_branch = NULL;
6792 *base_commit_id = NULL;
6793 *fileindex = NULL;
6795 err = lock_worktree(worktree, LOCK_EX);
6796 if (err)
6797 return err;
6799 err = open_fileindex(fileindex, &fileindex_path, worktree);
6800 if (err)
6801 goto done;
6803 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6804 &have_staged_files);
6805 if (err && err->code != GOT_ERR_CANCELLED)
6806 goto done;
6807 if (have_staged_files) {
6808 err = got_error(GOT_ERR_STAGED_PATHS);
6809 goto done;
6812 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6813 if (err)
6814 goto done;
6816 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6817 if (err)
6818 goto done;
6820 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6821 if (err)
6822 goto done;
6824 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6825 worktree);
6826 if (err)
6827 goto done;
6829 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6830 if (err)
6831 goto done;
6833 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6834 if (err)
6835 goto done;
6836 err = got_ref_resolve(commit_id, repo, commit_ref);
6837 if (err)
6838 goto done;
6840 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6841 if (err)
6842 goto done;
6843 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6844 if (err)
6845 goto done;
6847 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6848 if (err)
6849 goto done;
6850 done:
6851 free(commit_ref_name);
6852 free(branch_ref_name);
6853 free(fileindex_path);
6854 if (commit_ref)
6855 got_ref_close(commit_ref);
6856 if (base_commit_ref)
6857 got_ref_close(base_commit_ref);
6858 if (err) {
6859 free(*commit_id);
6860 *commit_id = NULL;
6861 free(*base_commit_id);
6862 *base_commit_id = NULL;
6863 if (*tmp_branch) {
6864 got_ref_close(*tmp_branch);
6865 *tmp_branch = NULL;
6867 if (*fileindex) {
6868 got_fileindex_free(*fileindex);
6869 *fileindex = NULL;
6871 lock_worktree(worktree, LOCK_EX);
6873 return err;
6876 static const struct got_error *
6877 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6879 const struct got_error *err;
6880 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6881 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6883 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6884 if (err)
6885 goto done;
6886 err = delete_ref(tmp_branch_name, repo);
6887 if (err)
6888 goto done;
6890 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6891 worktree);
6892 if (err)
6893 goto done;
6894 err = delete_ref(base_commit_ref_name, repo);
6895 if (err)
6896 goto done;
6898 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6899 if (err)
6900 goto done;
6901 err = delete_ref(branch_ref_name, repo);
6902 if (err)
6903 goto done;
6905 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6906 if (err)
6907 goto done;
6908 err = delete_ref(commit_ref_name, repo);
6909 if (err)
6910 goto done;
6911 done:
6912 free(tmp_branch_name);
6913 free(base_commit_ref_name);
6914 free(branch_ref_name);
6915 free(commit_ref_name);
6916 return err;
6919 const struct got_error *
6920 got_worktree_histedit_abort(struct got_worktree *worktree,
6921 struct got_fileindex *fileindex, struct got_repository *repo,
6922 struct got_reference *branch, struct got_object_id *base_commit_id,
6923 got_worktree_checkout_cb progress_cb, void *progress_arg)
6925 const struct got_error *err, *unlockerr, *sync_err;
6926 struct got_reference *resolved = NULL;
6927 char *fileindex_path = NULL;
6928 struct got_object_id *tree_id = NULL;
6929 struct revert_file_args rfa;
6931 err = lock_worktree(worktree, LOCK_EX);
6932 if (err)
6933 return err;
6935 err = got_ref_open(&resolved, repo,
6936 got_ref_get_symref_target(branch), 0);
6937 if (err)
6938 goto done;
6940 err = got_worktree_set_head_ref(worktree, resolved);
6941 if (err)
6942 goto done;
6944 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6945 if (err)
6946 goto done;
6948 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6949 worktree->path_prefix);
6950 if (err)
6951 goto done;
6953 err = delete_histedit_refs(worktree, repo);
6954 if (err)
6955 goto done;
6957 err = get_fileindex_path(&fileindex_path, worktree);
6958 if (err)
6959 goto done;
6961 rfa.worktree = worktree;
6962 rfa.fileindex = fileindex;
6963 rfa.progress_cb = progress_cb;
6964 rfa.progress_arg = progress_arg;
6965 rfa.patch_cb = NULL;
6966 rfa.patch_arg = NULL;
6967 rfa.repo = repo;
6968 err = worktree_status(worktree, "", fileindex, repo,
6969 revert_file, &rfa, NULL, NULL, 0, 0);
6970 if (err)
6971 goto sync;
6973 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6974 repo, progress_cb, progress_arg, NULL, NULL);
6975 sync:
6976 sync_err = sync_fileindex(fileindex, fileindex_path);
6977 if (sync_err && err == NULL)
6978 err = sync_err;
6979 done:
6980 got_ref_close(resolved);
6981 free(tree_id);
6982 free(fileindex_path);
6984 unlockerr = lock_worktree(worktree, LOCK_SH);
6985 if (unlockerr && err == NULL)
6986 err = unlockerr;
6987 return err;
6990 const struct got_error *
6991 got_worktree_histedit_complete(struct got_worktree *worktree,
6992 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6993 struct got_reference *edited_branch, struct got_repository *repo)
6995 const struct got_error *err, *unlockerr, *sync_err;
6996 struct got_object_id *new_head_commit_id = NULL;
6997 struct got_reference *resolved = NULL;
6998 char *fileindex_path = NULL;
7000 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7001 if (err)
7002 return err;
7004 err = got_ref_open(&resolved, repo,
7005 got_ref_get_symref_target(edited_branch), 0);
7006 if (err)
7007 goto done;
7009 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7010 resolved, new_head_commit_id, repo);
7011 if (err)
7012 goto done;
7014 err = got_ref_change_ref(resolved, new_head_commit_id);
7015 if (err)
7016 goto done;
7018 err = got_ref_write(resolved, repo);
7019 if (err)
7020 goto done;
7022 err = got_worktree_set_head_ref(worktree, resolved);
7023 if (err)
7024 goto done;
7026 err = delete_histedit_refs(worktree, repo);
7027 if (err)
7028 goto done;
7030 err = get_fileindex_path(&fileindex_path, worktree);
7031 if (err)
7032 goto done;
7033 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7034 sync_err = sync_fileindex(fileindex, fileindex_path);
7035 if (sync_err && err == NULL)
7036 err = sync_err;
7037 done:
7038 got_fileindex_free(fileindex);
7039 free(fileindex_path);
7040 free(new_head_commit_id);
7041 unlockerr = lock_worktree(worktree, LOCK_SH);
7042 if (unlockerr && err == NULL)
7043 err = unlockerr;
7044 return err;
7047 const struct got_error *
7048 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7049 struct got_object_id *commit_id, struct got_repository *repo)
7051 const struct got_error *err;
7052 char *commit_ref_name;
7054 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7055 if (err)
7056 return err;
7058 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7059 if (err)
7060 goto done;
7062 err = delete_ref(commit_ref_name, repo);
7063 done:
7064 free(commit_ref_name);
7065 return err;
7068 const struct got_error *
7069 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7070 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7071 struct got_worktree *worktree, const char *refname,
7072 struct got_repository *repo)
7074 const struct got_error *err = NULL;
7075 char *fileindex_path = NULL;
7076 struct check_rebase_ok_arg ok_arg;
7078 *fileindex = NULL;
7079 *branch_ref = NULL;
7080 *base_branch_ref = NULL;
7082 err = lock_worktree(worktree, LOCK_EX);
7083 if (err)
7084 return err;
7086 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7087 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7088 "cannot integrate a branch into itself; "
7089 "update -b or different branch name required");
7090 goto done;
7093 err = open_fileindex(fileindex, &fileindex_path, worktree);
7094 if (err)
7095 goto done;
7097 /* Preconditions are the same as for rebase. */
7098 ok_arg.worktree = worktree;
7099 ok_arg.repo = repo;
7100 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7101 &ok_arg);
7102 if (err)
7103 goto done;
7105 err = got_ref_open(branch_ref, repo, refname, 1);
7106 if (err)
7107 goto done;
7109 err = got_ref_open(base_branch_ref, repo,
7110 got_worktree_get_head_ref_name(worktree), 1);
7111 done:
7112 if (err) {
7113 if (*branch_ref) {
7114 got_ref_close(*branch_ref);
7115 *branch_ref = NULL;
7117 if (*base_branch_ref) {
7118 got_ref_close(*base_branch_ref);
7119 *base_branch_ref = NULL;
7121 if (*fileindex) {
7122 got_fileindex_free(*fileindex);
7123 *fileindex = NULL;
7125 lock_worktree(worktree, LOCK_SH);
7127 return err;
7130 const struct got_error *
7131 got_worktree_integrate_continue(struct got_worktree *worktree,
7132 struct got_fileindex *fileindex, struct got_repository *repo,
7133 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7134 got_worktree_checkout_cb progress_cb, void *progress_arg,
7135 got_cancel_cb cancel_cb, void *cancel_arg)
7137 const struct got_error *err = NULL, *sync_err, *unlockerr;
7138 char *fileindex_path = NULL;
7139 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7141 err = get_fileindex_path(&fileindex_path, worktree);
7142 if (err)
7143 goto done;
7145 err = got_ref_resolve(&commit_id, repo, branch_ref);
7146 if (err)
7147 goto done;
7149 err = got_object_id_by_path(&tree_id, repo, commit_id,
7150 worktree->path_prefix);
7151 if (err)
7152 goto done;
7154 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7155 if (err)
7156 goto done;
7158 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7159 progress_cb, progress_arg, cancel_cb, cancel_arg);
7160 if (err)
7161 goto sync;
7163 err = got_ref_change_ref(base_branch_ref, commit_id);
7164 if (err)
7165 goto sync;
7167 err = got_ref_write(base_branch_ref, repo);
7168 if (err)
7169 goto sync;
7171 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7172 sync:
7173 sync_err = sync_fileindex(fileindex, fileindex_path);
7174 if (sync_err && err == NULL)
7175 err = sync_err;
7177 done:
7178 unlockerr = got_ref_unlock(branch_ref);
7179 if (unlockerr && err == NULL)
7180 err = unlockerr;
7181 got_ref_close(branch_ref);
7183 unlockerr = got_ref_unlock(base_branch_ref);
7184 if (unlockerr && err == NULL)
7185 err = unlockerr;
7186 got_ref_close(base_branch_ref);
7188 got_fileindex_free(fileindex);
7189 free(fileindex_path);
7190 free(tree_id);
7192 unlockerr = lock_worktree(worktree, LOCK_SH);
7193 if (unlockerr && err == NULL)
7194 err = unlockerr;
7195 return err;
7198 const struct got_error *
7199 got_worktree_integrate_abort(struct got_worktree *worktree,
7200 struct got_fileindex *fileindex, struct got_repository *repo,
7201 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7203 const struct got_error *err = NULL, *unlockerr = NULL;
7205 got_fileindex_free(fileindex);
7207 err = lock_worktree(worktree, LOCK_SH);
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 return err;
7222 struct check_stage_ok_arg {
7223 struct got_object_id *head_commit_id;
7224 struct got_worktree *worktree;
7225 struct got_fileindex *fileindex;
7226 struct got_repository *repo;
7227 int have_changes;
7230 const struct got_error *
7231 check_stage_ok(void *arg, unsigned char status,
7232 unsigned char staged_status, const char *relpath,
7233 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7234 struct got_object_id *commit_id, int dirfd, const char *de_name)
7236 struct check_stage_ok_arg *a = arg;
7237 const struct got_error *err = NULL;
7238 struct got_fileindex_entry *ie;
7239 struct got_object_id base_commit_id;
7240 struct got_object_id *base_commit_idp = NULL;
7241 char *in_repo_path = NULL, *p;
7243 if (status == GOT_STATUS_UNVERSIONED ||
7244 status == GOT_STATUS_NO_CHANGE)
7245 return NULL;
7246 if (status == GOT_STATUS_NONEXISTENT)
7247 return got_error_set_errno(ENOENT, relpath);
7249 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7250 if (ie == NULL)
7251 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7253 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7254 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7255 relpath) == -1)
7256 return got_error_from_errno("asprintf");
7258 if (got_fileindex_entry_has_commit(ie)) {
7259 memcpy(base_commit_id.sha1, ie->commit_sha1,
7260 SHA1_DIGEST_LENGTH);
7261 base_commit_idp = &base_commit_id;
7264 if (status == GOT_STATUS_CONFLICT) {
7265 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7266 goto done;
7267 } else if (status != GOT_STATUS_ADD &&
7268 status != GOT_STATUS_MODIFY &&
7269 status != GOT_STATUS_DELETE) {
7270 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7271 goto done;
7274 a->have_changes = 1;
7276 p = in_repo_path;
7277 while (p[0] == '/')
7278 p++;
7279 err = check_out_of_date(p, status, staged_status,
7280 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7281 GOT_ERR_STAGE_OUT_OF_DATE);
7282 done:
7283 free(in_repo_path);
7284 return err;
7287 struct stage_path_arg {
7288 struct got_worktree *worktree;
7289 struct got_fileindex *fileindex;
7290 struct got_repository *repo;
7291 got_worktree_status_cb status_cb;
7292 void *status_arg;
7293 got_worktree_patch_cb patch_cb;
7294 void *patch_arg;
7295 int staged_something;
7296 int allow_bad_symlinks;
7299 static const struct got_error *
7300 stage_path(void *arg, unsigned char status,
7301 unsigned char staged_status, const char *relpath,
7302 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7303 struct got_object_id *commit_id, int dirfd, const char *de_name)
7305 struct stage_path_arg *a = arg;
7306 const struct got_error *err = NULL;
7307 struct got_fileindex_entry *ie;
7308 char *ondisk_path = NULL, *path_content = NULL;
7309 uint32_t stage;
7310 struct got_object_id *new_staged_blob_id = NULL;
7311 struct stat sb;
7313 if (status == GOT_STATUS_UNVERSIONED)
7314 return NULL;
7316 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7317 if (ie == NULL)
7318 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7320 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7321 relpath)== -1)
7322 return got_error_from_errno("asprintf");
7324 switch (status) {
7325 case GOT_STATUS_ADD:
7326 case GOT_STATUS_MODIFY:
7327 /* XXX could sb.st_mode be passed in by our caller? */
7328 if (lstat(ondisk_path, &sb) == -1) {
7329 err = got_error_from_errno2("lstat", ondisk_path);
7330 break;
7332 if (a->patch_cb) {
7333 if (status == GOT_STATUS_ADD) {
7334 int choice = GOT_PATCH_CHOICE_NONE;
7335 err = (*a->patch_cb)(&choice, a->patch_arg,
7336 status, ie->path, NULL, 1, 1);
7337 if (err)
7338 break;
7339 if (choice != GOT_PATCH_CHOICE_YES)
7340 break;
7341 } else {
7342 err = create_patched_content(&path_content, 0,
7343 staged_blob_id ? staged_blob_id : blob_id,
7344 ondisk_path, dirfd, de_name, ie->path,
7345 a->repo, a->patch_cb, a->patch_arg);
7346 if (err || path_content == NULL)
7347 break;
7350 err = got_object_blob_create(&new_staged_blob_id,
7351 path_content ? path_content : ondisk_path, a->repo);
7352 if (err)
7353 break;
7354 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7355 SHA1_DIGEST_LENGTH);
7356 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7357 stage = GOT_FILEIDX_STAGE_ADD;
7358 else
7359 stage = GOT_FILEIDX_STAGE_MODIFY;
7360 got_fileindex_entry_stage_set(ie, stage);
7361 if (S_ISLNK(sb.st_mode)) {
7362 int is_bad_symlink = 0;
7363 if (!a->allow_bad_symlinks) {
7364 char target_path[PATH_MAX];
7365 ssize_t target_len;
7366 target_len = readlink(ondisk_path, target_path,
7367 sizeof(target_path));
7368 if (target_len == -1) {
7369 err = got_error_from_errno2("readlink",
7370 ondisk_path);
7371 break;
7373 err = is_bad_symlink_target(&is_bad_symlink,
7374 target_path, target_len, ondisk_path,
7375 a->worktree->root_path);
7376 if (err)
7377 break;
7378 if (is_bad_symlink) {
7379 err = got_error_path(ondisk_path,
7380 GOT_ERR_BAD_SYMLINK);
7381 break;
7384 if (is_bad_symlink)
7385 got_fileindex_entry_staged_filetype_set(ie,
7386 GOT_FILEIDX_MODE_BAD_SYMLINK);
7387 else
7388 got_fileindex_entry_staged_filetype_set(ie,
7389 GOT_FILEIDX_MODE_SYMLINK);
7390 } else {
7391 got_fileindex_entry_staged_filetype_set(ie,
7392 GOT_FILEIDX_MODE_REGULAR_FILE);
7394 a->staged_something = 1;
7395 if (a->status_cb == NULL)
7396 break;
7397 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7398 get_staged_status(ie), relpath, blob_id,
7399 new_staged_blob_id, NULL, dirfd, de_name);
7400 break;
7401 case GOT_STATUS_DELETE:
7402 if (staged_status == GOT_STATUS_DELETE)
7403 break;
7404 if (a->patch_cb) {
7405 int choice = GOT_PATCH_CHOICE_NONE;
7406 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7407 ie->path, NULL, 1, 1);
7408 if (err)
7409 break;
7410 if (choice == GOT_PATCH_CHOICE_NO)
7411 break;
7412 if (choice != GOT_PATCH_CHOICE_YES) {
7413 err = got_error(GOT_ERR_PATCH_CHOICE);
7414 break;
7417 stage = GOT_FILEIDX_STAGE_DELETE;
7418 got_fileindex_entry_stage_set(ie, stage);
7419 a->staged_something = 1;
7420 if (a->status_cb == NULL)
7421 break;
7422 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7423 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7424 de_name);
7425 break;
7426 case GOT_STATUS_NO_CHANGE:
7427 break;
7428 case GOT_STATUS_CONFLICT:
7429 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7430 break;
7431 case GOT_STATUS_NONEXISTENT:
7432 err = got_error_set_errno(ENOENT, relpath);
7433 break;
7434 default:
7435 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7436 break;
7439 if (path_content && unlink(path_content) == -1 && err == NULL)
7440 err = got_error_from_errno2("unlink", path_content);
7441 free(path_content);
7442 free(ondisk_path);
7443 free(new_staged_blob_id);
7444 return err;
7447 const struct got_error *
7448 got_worktree_stage(struct got_worktree *worktree,
7449 struct got_pathlist_head *paths,
7450 got_worktree_status_cb status_cb, void *status_arg,
7451 got_worktree_patch_cb patch_cb, void *patch_arg,
7452 int allow_bad_symlinks, struct got_repository *repo)
7454 const struct got_error *err = NULL, *sync_err, *unlockerr;
7455 struct got_pathlist_entry *pe;
7456 struct got_fileindex *fileindex = NULL;
7457 char *fileindex_path = NULL;
7458 struct got_reference *head_ref = NULL;
7459 struct got_object_id *head_commit_id = NULL;
7460 struct check_stage_ok_arg oka;
7461 struct stage_path_arg spa;
7463 err = lock_worktree(worktree, LOCK_EX);
7464 if (err)
7465 return err;
7467 err = got_ref_open(&head_ref, repo,
7468 got_worktree_get_head_ref_name(worktree), 0);
7469 if (err)
7470 goto done;
7471 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7472 if (err)
7473 goto done;
7474 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7475 if (err)
7476 goto done;
7478 /* Check pre-conditions before staging anything. */
7479 oka.head_commit_id = head_commit_id;
7480 oka.worktree = worktree;
7481 oka.fileindex = fileindex;
7482 oka.repo = repo;
7483 oka.have_changes = 0;
7484 TAILQ_FOREACH(pe, paths, entry) {
7485 err = worktree_status(worktree, pe->path, fileindex, repo,
7486 check_stage_ok, &oka, NULL, NULL, 0, 0);
7487 if (err)
7488 goto done;
7490 if (!oka.have_changes) {
7491 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7492 goto done;
7495 spa.worktree = worktree;
7496 spa.fileindex = fileindex;
7497 spa.repo = repo;
7498 spa.patch_cb = patch_cb;
7499 spa.patch_arg = patch_arg;
7500 spa.status_cb = status_cb;
7501 spa.status_arg = status_arg;
7502 spa.staged_something = 0;
7503 spa.allow_bad_symlinks = allow_bad_symlinks;
7504 TAILQ_FOREACH(pe, paths, entry) {
7505 err = worktree_status(worktree, pe->path, fileindex, repo,
7506 stage_path, &spa, NULL, NULL, 0, 0);
7507 if (err)
7508 goto done;
7510 if (!spa.staged_something) {
7511 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7512 goto done;
7515 sync_err = sync_fileindex(fileindex, fileindex_path);
7516 if (sync_err && err == NULL)
7517 err = sync_err;
7518 done:
7519 if (head_ref)
7520 got_ref_close(head_ref);
7521 free(head_commit_id);
7522 free(fileindex_path);
7523 if (fileindex)
7524 got_fileindex_free(fileindex);
7525 unlockerr = lock_worktree(worktree, LOCK_SH);
7526 if (unlockerr && err == NULL)
7527 err = unlockerr;
7528 return err;
7531 struct unstage_path_arg {
7532 struct got_worktree *worktree;
7533 struct got_fileindex *fileindex;
7534 struct got_repository *repo;
7535 got_worktree_checkout_cb progress_cb;
7536 void *progress_arg;
7537 got_worktree_patch_cb patch_cb;
7538 void *patch_arg;
7541 static const struct got_error *
7542 create_unstaged_content(char **path_unstaged_content,
7543 char **path_new_staged_content, struct got_object_id *blob_id,
7544 struct got_object_id *staged_blob_id, const char *relpath,
7545 struct got_repository *repo,
7546 got_worktree_patch_cb patch_cb, void *patch_arg)
7548 const struct got_error *err, *free_err;
7549 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7550 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7551 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7552 struct got_diffreg_result *diffreg_result = NULL;
7553 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7554 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7556 *path_unstaged_content = NULL;
7557 *path_new_staged_content = NULL;
7559 err = got_object_id_str(&label1, blob_id);
7560 if (err)
7561 return err;
7562 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7563 if (err)
7564 goto done;
7566 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7567 if (err)
7568 goto done;
7570 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7571 if (err)
7572 goto done;
7574 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7575 if (err)
7576 goto done;
7578 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7579 if (err)
7580 goto done;
7582 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7583 if (err)
7584 goto done;
7586 err = got_diff_files(&diffreg_result, f1, label1, f2,
7587 path2, 3, 0, 1, NULL);
7588 if (err)
7589 goto done;
7591 err = got_opentemp_named(path_unstaged_content, &outfile,
7592 "got-unstaged-content");
7593 if (err)
7594 goto done;
7595 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7596 "got-new-staged-content");
7597 if (err)
7598 goto done;
7600 if (fseek(f1, 0L, SEEK_SET) == -1) {
7601 err = got_ferror(f1, GOT_ERR_IO);
7602 goto done;
7604 if (fseek(f2, 0L, SEEK_SET) == -1) {
7605 err = got_ferror(f2, GOT_ERR_IO);
7606 goto done;
7608 /* Count the number of actual changes in the diff result. */
7609 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7610 struct diff_chunk_context cc = {};
7611 diff_chunk_context_load_change(&cc, &nchunks_used,
7612 diffreg_result->result, n, 0);
7613 nchanges++;
7615 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7616 int choice;
7617 err = apply_or_reject_change(&choice, &nchunks_used,
7618 diffreg_result->result, n, relpath, f1, f2,
7619 &line_cur1, &line_cur2,
7620 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7621 if (err)
7622 goto done;
7623 if (choice == GOT_PATCH_CHOICE_YES)
7624 have_content = 1;
7625 else
7626 have_rejected_content = 1;
7627 if (choice == GOT_PATCH_CHOICE_QUIT)
7628 break;
7630 if (have_content || have_rejected_content)
7631 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7632 outfile, rejectfile);
7633 done:
7634 free(label1);
7635 if (blob)
7636 got_object_blob_close(blob);
7637 if (staged_blob)
7638 got_object_blob_close(staged_blob);
7639 free_err = got_diffreg_result_free(diffreg_result);
7640 if (free_err && err == NULL)
7641 err = free_err;
7642 if (f1 && fclose(f1) == EOF && err == NULL)
7643 err = got_error_from_errno2("fclose", path1);
7644 if (f2 && fclose(f2) == EOF && err == NULL)
7645 err = got_error_from_errno2("fclose", path2);
7646 if (outfile && fclose(outfile) == EOF && err == NULL)
7647 err = got_error_from_errno2("fclose", *path_unstaged_content);
7648 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7649 err = got_error_from_errno2("fclose", *path_new_staged_content);
7650 if (path1 && unlink(path1) == -1 && err == NULL)
7651 err = got_error_from_errno2("unlink", path1);
7652 if (path2 && unlink(path2) == -1 && err == NULL)
7653 err = got_error_from_errno2("unlink", path2);
7654 if (err || !have_content) {
7655 if (*path_unstaged_content &&
7656 unlink(*path_unstaged_content) == -1 && err == NULL)
7657 err = got_error_from_errno2("unlink",
7658 *path_unstaged_content);
7659 free(*path_unstaged_content);
7660 *path_unstaged_content = NULL;
7662 if (err || !have_content || !have_rejected_content) {
7663 if (*path_new_staged_content &&
7664 unlink(*path_new_staged_content) == -1 && err == NULL)
7665 err = got_error_from_errno2("unlink",
7666 *path_new_staged_content);
7667 free(*path_new_staged_content);
7668 *path_new_staged_content = NULL;
7670 free(path1);
7671 free(path2);
7672 return err;
7675 static const struct got_error *
7676 unstage_hunks(struct got_object_id *staged_blob_id,
7677 struct got_blob_object *blob_base,
7678 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7679 const char *ondisk_path, const char *label_orig,
7680 struct got_worktree *worktree, struct got_repository *repo,
7681 got_worktree_patch_cb patch_cb, void *patch_arg,
7682 got_worktree_checkout_cb progress_cb, void *progress_arg)
7684 const struct got_error *err = NULL;
7685 char *path_unstaged_content = NULL;
7686 char *path_new_staged_content = NULL;
7687 struct got_object_id *new_staged_blob_id = NULL;
7688 FILE *f = NULL;
7689 struct stat sb;
7691 err = create_unstaged_content(&path_unstaged_content,
7692 &path_new_staged_content, blob_id, staged_blob_id,
7693 ie->path, repo, patch_cb, patch_arg);
7694 if (err)
7695 return err;
7697 if (path_unstaged_content == NULL)
7698 return NULL;
7700 if (path_new_staged_content) {
7701 err = got_object_blob_create(&new_staged_blob_id,
7702 path_new_staged_content, repo);
7703 if (err)
7704 goto done;
7707 f = fopen(path_unstaged_content, "r");
7708 if (f == NULL) {
7709 err = got_error_from_errno2("fopen",
7710 path_unstaged_content);
7711 goto done;
7713 if (fstat(fileno(f), &sb) == -1) {
7714 err = got_error_from_errno2("fstat", path_unstaged_content);
7715 goto done;
7717 if (got_fileindex_entry_staged_filetype_get(ie) ==
7718 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7719 char link_target[PATH_MAX];
7720 size_t r;
7721 r = fread(link_target, 1, sizeof(link_target), f);
7722 if (r == 0 && ferror(f)) {
7723 err = got_error_from_errno("fread");
7724 goto done;
7726 if (r >= sizeof(link_target)) { /* should not happen */
7727 err = got_error(GOT_ERR_NO_SPACE);
7728 goto done;
7730 link_target[r] = '\0';
7731 err = merge_symlink(worktree, blob_base,
7732 ondisk_path, ie->path, label_orig, link_target,
7733 worktree->base_commit_id, repo, progress_cb,
7734 progress_arg);
7735 } else {
7736 int local_changes_subsumed;
7737 err = merge_file(&local_changes_subsumed, worktree,
7738 blob_base, ondisk_path, ie->path,
7739 got_fileindex_perms_to_st(ie),
7740 path_unstaged_content, label_orig, "unstaged",
7741 repo, progress_cb, progress_arg);
7743 if (err)
7744 goto done;
7746 if (new_staged_blob_id) {
7747 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7748 SHA1_DIGEST_LENGTH);
7749 } else {
7750 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7751 got_fileindex_entry_staged_filetype_set(ie, 0);
7753 done:
7754 free(new_staged_blob_id);
7755 if (path_unstaged_content &&
7756 unlink(path_unstaged_content) == -1 && err == NULL)
7757 err = got_error_from_errno2("unlink", path_unstaged_content);
7758 if (path_new_staged_content &&
7759 unlink(path_new_staged_content) == -1 && err == NULL)
7760 err = got_error_from_errno2("unlink", path_new_staged_content);
7761 if (f && fclose(f) == EOF && err == NULL)
7762 err = got_error_from_errno2("fclose", path_unstaged_content);
7763 free(path_unstaged_content);
7764 free(path_new_staged_content);
7765 return err;
7768 static const struct got_error *
7769 unstage_path(void *arg, unsigned char status,
7770 unsigned char staged_status, const char *relpath,
7771 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7772 struct got_object_id *commit_id, int dirfd, const char *de_name)
7774 const struct got_error *err = NULL;
7775 struct unstage_path_arg *a = arg;
7776 struct got_fileindex_entry *ie;
7777 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7778 char *ondisk_path = NULL;
7779 char *id_str = NULL, *label_orig = NULL;
7780 int local_changes_subsumed;
7781 struct stat sb;
7783 if (staged_status != GOT_STATUS_ADD &&
7784 staged_status != GOT_STATUS_MODIFY &&
7785 staged_status != GOT_STATUS_DELETE)
7786 return NULL;
7788 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7789 if (ie == NULL)
7790 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7792 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7793 == -1)
7794 return got_error_from_errno("asprintf");
7796 err = got_object_id_str(&id_str,
7797 commit_id ? commit_id : a->worktree->base_commit_id);
7798 if (err)
7799 goto done;
7800 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7801 id_str) == -1) {
7802 err = got_error_from_errno("asprintf");
7803 goto done;
7806 switch (staged_status) {
7807 case GOT_STATUS_MODIFY:
7808 err = got_object_open_as_blob(&blob_base, a->repo,
7809 blob_id, 8192);
7810 if (err)
7811 break;
7812 /* fall through */
7813 case GOT_STATUS_ADD:
7814 if (a->patch_cb) {
7815 if (staged_status == GOT_STATUS_ADD) {
7816 int choice = GOT_PATCH_CHOICE_NONE;
7817 err = (*a->patch_cb)(&choice, a->patch_arg,
7818 staged_status, ie->path, NULL, 1, 1);
7819 if (err)
7820 break;
7821 if (choice != GOT_PATCH_CHOICE_YES)
7822 break;
7823 } else {
7824 err = unstage_hunks(staged_blob_id,
7825 blob_base, blob_id, ie, ondisk_path,
7826 label_orig, a->worktree, a->repo,
7827 a->patch_cb, a->patch_arg,
7828 a->progress_cb, a->progress_arg);
7829 break; /* Done with this file. */
7832 err = got_object_open_as_blob(&blob_staged, a->repo,
7833 staged_blob_id, 8192);
7834 if (err)
7835 break;
7836 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7837 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7838 case GOT_FILEIDX_MODE_REGULAR_FILE:
7839 err = merge_blob(&local_changes_subsumed, a->worktree,
7840 blob_base, ondisk_path, relpath,
7841 got_fileindex_perms_to_st(ie), label_orig,
7842 blob_staged, commit_id ? commit_id :
7843 a->worktree->base_commit_id, a->repo,
7844 a->progress_cb, a->progress_arg);
7845 break;
7846 case GOT_FILEIDX_MODE_SYMLINK:
7847 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7848 char *staged_target;
7849 err = got_object_blob_read_to_str(
7850 &staged_target, blob_staged);
7851 if (err)
7852 goto done;
7853 err = merge_symlink(a->worktree, blob_base,
7854 ondisk_path, relpath, label_orig,
7855 staged_target, commit_id ? commit_id :
7856 a->worktree->base_commit_id,
7857 a->repo, a->progress_cb, a->progress_arg);
7858 free(staged_target);
7859 } else {
7860 err = merge_blob(&local_changes_subsumed,
7861 a->worktree, blob_base, ondisk_path,
7862 relpath, got_fileindex_perms_to_st(ie),
7863 label_orig, blob_staged,
7864 commit_id ? commit_id :
7865 a->worktree->base_commit_id, a->repo,
7866 a->progress_cb, a->progress_arg);
7868 break;
7869 default:
7870 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7871 break;
7873 if (err == NULL) {
7874 got_fileindex_entry_stage_set(ie,
7875 GOT_FILEIDX_STAGE_NONE);
7876 got_fileindex_entry_staged_filetype_set(ie, 0);
7878 break;
7879 case GOT_STATUS_DELETE:
7880 if (a->patch_cb) {
7881 int choice = GOT_PATCH_CHOICE_NONE;
7882 err = (*a->patch_cb)(&choice, a->patch_arg,
7883 staged_status, ie->path, NULL, 1, 1);
7884 if (err)
7885 break;
7886 if (choice == GOT_PATCH_CHOICE_NO)
7887 break;
7888 if (choice != GOT_PATCH_CHOICE_YES) {
7889 err = got_error(GOT_ERR_PATCH_CHOICE);
7890 break;
7893 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7894 got_fileindex_entry_staged_filetype_set(ie, 0);
7895 err = get_file_status(&status, &sb, ie, ondisk_path,
7896 dirfd, de_name, a->repo);
7897 if (err)
7898 break;
7899 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7900 break;
7902 done:
7903 free(ondisk_path);
7904 if (blob_base)
7905 got_object_blob_close(blob_base);
7906 if (blob_staged)
7907 got_object_blob_close(blob_staged);
7908 free(id_str);
7909 free(label_orig);
7910 return err;
7913 const struct got_error *
7914 got_worktree_unstage(struct got_worktree *worktree,
7915 struct got_pathlist_head *paths,
7916 got_worktree_checkout_cb progress_cb, void *progress_arg,
7917 got_worktree_patch_cb patch_cb, void *patch_arg,
7918 struct got_repository *repo)
7920 const struct got_error *err = NULL, *sync_err, *unlockerr;
7921 struct got_pathlist_entry *pe;
7922 struct got_fileindex *fileindex = NULL;
7923 char *fileindex_path = NULL;
7924 struct unstage_path_arg upa;
7926 err = lock_worktree(worktree, LOCK_EX);
7927 if (err)
7928 return err;
7930 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7931 if (err)
7932 goto done;
7934 upa.worktree = worktree;
7935 upa.fileindex = fileindex;
7936 upa.repo = repo;
7937 upa.progress_cb = progress_cb;
7938 upa.progress_arg = progress_arg;
7939 upa.patch_cb = patch_cb;
7940 upa.patch_arg = patch_arg;
7941 TAILQ_FOREACH(pe, paths, entry) {
7942 err = worktree_status(worktree, pe->path, fileindex, repo,
7943 unstage_path, &upa, NULL, NULL, 0, 0);
7944 if (err)
7945 goto done;
7948 sync_err = sync_fileindex(fileindex, fileindex_path);
7949 if (sync_err && err == NULL)
7950 err = sync_err;
7951 done:
7952 free(fileindex_path);
7953 if (fileindex)
7954 got_fileindex_free(fileindex);
7955 unlockerr = lock_worktree(worktree, LOCK_SH);
7956 if (unlockerr && err == NULL)
7957 err = unlockerr;
7958 return err;
7961 struct report_file_info_arg {
7962 struct got_worktree *worktree;
7963 got_worktree_path_info_cb info_cb;
7964 void *info_arg;
7965 struct got_pathlist_head *paths;
7966 got_cancel_cb cancel_cb;
7967 void *cancel_arg;
7970 static const struct got_error *
7971 report_file_info(void *arg, struct got_fileindex_entry *ie)
7973 struct report_file_info_arg *a = arg;
7974 struct got_pathlist_entry *pe;
7975 struct got_object_id blob_id, staged_blob_id, commit_id;
7976 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7977 struct got_object_id *commit_idp = NULL;
7978 int stage;
7980 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7981 return got_error(GOT_ERR_CANCELLED);
7983 TAILQ_FOREACH(pe, a->paths, entry) {
7984 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7985 got_path_is_child(ie->path, pe->path, pe->path_len))
7986 break;
7988 if (pe == NULL) /* not found */
7989 return NULL;
7991 if (got_fileindex_entry_has_blob(ie)) {
7992 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7993 blob_idp = &blob_id;
7995 stage = got_fileindex_entry_stage_get(ie);
7996 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7997 stage == GOT_FILEIDX_STAGE_ADD) {
7998 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7999 SHA1_DIGEST_LENGTH);
8000 staged_blob_idp = &staged_blob_id;
8003 if (got_fileindex_entry_has_commit(ie)) {
8004 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8005 commit_idp = &commit_id;
8008 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8009 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8012 const struct got_error *
8013 got_worktree_path_info(struct got_worktree *worktree,
8014 struct got_pathlist_head *paths,
8015 got_worktree_path_info_cb info_cb, void *info_arg,
8016 got_cancel_cb cancel_cb, void *cancel_arg)
8019 const struct got_error *err = NULL, *unlockerr;
8020 struct got_fileindex *fileindex = NULL;
8021 char *fileindex_path = NULL;
8022 struct report_file_info_arg arg;
8024 err = lock_worktree(worktree, LOCK_SH);
8025 if (err)
8026 return err;
8028 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8029 if (err)
8030 goto done;
8032 arg.worktree = worktree;
8033 arg.info_cb = info_cb;
8034 arg.info_arg = info_arg;
8035 arg.paths = paths;
8036 arg.cancel_cb = cancel_cb;
8037 arg.cancel_arg = cancel_arg;
8038 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8039 &arg);
8040 done:
8041 free(fileindex_path);
8042 if (fileindex)
8043 got_fileindex_free(fileindex);
8044 unlockerr = lock_worktree(worktree, LOCK_UN);
8045 if (unlockerr && err == NULL)
8046 err = unlockerr;
8047 return err;