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;
502 free(worktree->repo_path);
503 free(worktree->path_prefix);
504 free(worktree->base_commit_id);
505 free(worktree->head_ref_name);
506 if (worktree->lockfd != -1) {
507 if (close(worktree->lockfd) != 0)
508 err = got_error_from_errno2("close",
509 got_worktree_get_root_path(worktree));
511 free(worktree->root_path);
512 free(worktree->gotconfig_path);
513 got_gotconfig_free(worktree->gotconfig);
514 close(worktree->root_fd);
515 free(worktree);
516 return err;
519 const char *
520 got_worktree_get_root_path(struct got_worktree *worktree)
522 return worktree->root_path;
525 const char *
526 got_worktree_get_repo_path(struct got_worktree *worktree)
528 return worktree->repo_path;
530 const char *
531 got_worktree_get_path_prefix(struct got_worktree *worktree)
533 return worktree->path_prefix;
536 const struct got_error *
537 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
538 const char *path_prefix)
540 char *absprefix = NULL;
542 if (!got_path_is_absolute(path_prefix)) {
543 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
544 return got_error_from_errno("asprintf");
546 *match = (strcmp(absprefix ? absprefix : path_prefix,
547 worktree->path_prefix) == 0);
548 free(absprefix);
549 return NULL;
552 const char *
553 got_worktree_get_head_ref_name(struct got_worktree *worktree)
555 return worktree->head_ref_name;
558 const struct got_error *
559 got_worktree_set_head_ref(struct got_worktree *worktree,
560 struct got_reference *head_ref)
562 const struct got_error *err = NULL;
563 char *path_got = NULL, *head_ref_name = NULL;
565 if (asprintf(&path_got, "%s/%s", worktree->root_path,
566 GOT_WORKTREE_GOT_DIR) == -1) {
567 err = got_error_from_errno("asprintf");
568 path_got = NULL;
569 goto done;
572 head_ref_name = strdup(got_ref_get_name(head_ref));
573 if (head_ref_name == NULL) {
574 err = got_error_from_errno("strdup");
575 goto done;
578 err = write_head_ref(path_got, head_ref);
579 if (err)
580 goto done;
582 free(worktree->head_ref_name);
583 worktree->head_ref_name = head_ref_name;
584 done:
585 free(path_got);
586 if (err)
587 free(head_ref_name);
588 return err;
591 struct got_object_id *
592 got_worktree_get_base_commit_id(struct got_worktree *worktree)
594 return worktree->base_commit_id;
597 const struct got_error *
598 got_worktree_set_base_commit_id(struct got_worktree *worktree,
599 struct got_repository *repo, struct got_object_id *commit_id)
601 const struct got_error *err;
602 struct got_object *obj = NULL;
603 char *id_str = NULL;
604 char *path_got = NULL;
606 if (asprintf(&path_got, "%s/%s", worktree->root_path,
607 GOT_WORKTREE_GOT_DIR) == -1) {
608 err = got_error_from_errno("asprintf");
609 path_got = NULL;
610 goto done;
613 err = got_object_open(&obj, repo, commit_id);
614 if (err)
615 return err;
617 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
618 err = got_error(GOT_ERR_OBJ_TYPE);
619 goto done;
622 /* Record our base commit. */
623 err = got_object_id_str(&id_str, commit_id);
624 if (err)
625 goto done;
626 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
627 if (err)
628 goto done;
630 free(worktree->base_commit_id);
631 worktree->base_commit_id = got_object_id_dup(commit_id);
632 if (worktree->base_commit_id == NULL) {
633 err = got_error_from_errno("got_object_id_dup");
634 goto done;
636 done:
637 if (obj)
638 got_object_close(obj);
639 free(id_str);
640 free(path_got);
641 return err;
644 const struct got_gotconfig *
645 got_worktree_get_gotconfig(struct got_worktree *worktree)
647 return worktree->gotconfig;
650 static const struct got_error *
651 lock_worktree(struct got_worktree *worktree, int operation)
653 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
654 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
655 : got_error_from_errno2("flock",
656 got_worktree_get_root_path(worktree)));
657 return NULL;
660 static const struct got_error *
661 add_dir_on_disk(struct got_worktree *worktree, const char *path)
663 const struct got_error *err = NULL;
664 char *abspath;
666 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
667 return got_error_from_errno("asprintf");
669 err = got_path_mkdir(abspath);
670 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
671 struct stat sb;
672 err = NULL;
673 if (lstat(abspath, &sb) == -1) {
674 err = got_error_from_errno2("lstat", abspath);
675 } else if (!S_ISDIR(sb.st_mode)) {
676 /* TODO directory is obstructed; do something */
677 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
680 free(abspath);
681 return err;
684 static const struct got_error *
685 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
687 const struct got_error *err = NULL;
688 uint8_t fbuf1[8192];
689 uint8_t fbuf2[8192];
690 size_t flen1 = 0, flen2 = 0;
692 *same = 1;
694 for (;;) {
695 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
696 if (flen1 == 0 && ferror(f1)) {
697 err = got_error_from_errno("fread");
698 break;
700 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
701 if (flen2 == 0 && ferror(f2)) {
702 err = got_error_from_errno("fread");
703 break;
705 if (flen1 == 0) {
706 if (flen2 != 0)
707 *same = 0;
708 break;
709 } else if (flen2 == 0) {
710 if (flen1 != 0)
711 *same = 0;
712 break;
713 } else if (flen1 == flen2) {
714 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
715 *same = 0;
716 break;
718 } else {
719 *same = 0;
720 break;
724 return err;
727 static const struct got_error *
728 check_files_equal(int *same, const char *f1_path, const char *f2_path)
730 const struct got_error *err = NULL;
731 struct stat sb;
732 size_t size1, size2;
733 FILE *f1 = NULL, *f2 = NULL;
735 *same = 1;
737 if (lstat(f1_path, &sb) != 0) {
738 err = got_error_from_errno2("lstat", f1_path);
739 goto done;
741 size1 = sb.st_size;
743 if (lstat(f2_path, &sb) != 0) {
744 err = got_error_from_errno2("lstat", f2_path);
745 goto done;
747 size2 = sb.st_size;
749 if (size1 != size2) {
750 *same = 0;
751 return NULL;
754 f1 = fopen(f1_path, "r");
755 if (f1 == NULL)
756 return got_error_from_errno2("fopen", f1_path);
758 f2 = fopen(f2_path, "r");
759 if (f2 == NULL) {
760 err = got_error_from_errno2("fopen", f2_path);
761 goto done;
764 err = check_file_contents_equal(same, f1, f2);
765 done:
766 if (f1 && fclose(f1) == EOF && err == NULL)
767 err = got_error_from_errno("fclose");
768 if (f2 && fclose(f2) == EOF && err == NULL)
769 err = got_error_from_errno("fclose");
771 return err;
774 /*
775 * Perform a 3-way merge where blob_orig acts as the common ancestor,
776 * the file at deriv_path acts as the first derived version, and the
777 * file on disk acts as the second derived version.
778 */
779 static const struct got_error *
780 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
781 struct got_blob_object *blob_orig, const char *ondisk_path,
782 const char *path, uint16_t st_mode, const char *deriv_path,
783 const char *label_orig, const char *label_deriv,
784 struct got_repository *repo,
785 got_worktree_checkout_cb progress_cb, void *progress_arg)
787 const struct got_error *err = NULL;
788 int merged_fd = -1;
789 FILE *f_orig = NULL;
790 char *blob_orig_path = NULL;
791 char *merged_path = NULL, *base_path = NULL;
792 int overlapcnt = 0;
793 char *parent = NULL;
794 char *symlink_path = NULL;
795 FILE *symlinkf = NULL;
797 *local_changes_subsumed = 0;
799 err = got_path_dirname(&parent, ondisk_path);
800 if (err)
801 return err;
803 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
804 err = got_error_from_errno("asprintf");
805 goto done;
808 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
809 if (err)
810 goto done;
812 free(base_path);
813 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
814 err = got_error_from_errno("asprintf");
815 base_path = NULL;
816 goto done;
819 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
820 if (err)
821 goto done;
822 if (blob_orig) {
823 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
824 blob_orig);
825 if (err)
826 goto done;
827 } else {
828 /*
829 * If the file has no blob, this is an "add vs add" conflict,
830 * and we simply use an empty ancestor file to make both files
831 * appear in the merged result in their entirety.
832 */
835 /*
836 * In order the run a 3-way merge with a symlink we copy the symlink's
837 * target path into a temporary file and use that file with diff3.
838 */
839 if (S_ISLNK(st_mode)) {
840 char target_path[PATH_MAX];
841 ssize_t target_len;
842 size_t n;
844 free(base_path);
845 if (asprintf(&base_path, "%s/got-symlink-merge",
846 parent) == -1) {
847 err = got_error_from_errno("asprintf");
848 base_path = NULL;
849 goto done;
851 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
852 if (err)
853 goto done;
854 target_len = readlink(ondisk_path, target_path,
855 sizeof(target_path));
856 if (target_len == -1) {
857 err = got_error_from_errno2("readlink", ondisk_path);
858 goto done;
860 n = fwrite(target_path, 1, target_len, symlinkf);
861 if (n != target_len) {
862 err = got_ferror(symlinkf, GOT_ERR_IO);
863 goto done;
865 if (fflush(symlinkf) == EOF) {
866 err = got_error_from_errno2("fflush", symlink_path);
867 goto done;
871 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
872 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
873 label_deriv, label_orig, NULL);
874 if (err)
875 goto done;
877 err = (*progress_cb)(progress_arg,
878 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
879 if (err)
880 goto done;
882 if (fsync(merged_fd) != 0) {
883 err = got_error_from_errno("fsync");
884 goto done;
887 /* Check if a clean merge has subsumed all local changes. */
888 if (overlapcnt == 0) {
889 err = check_files_equal(local_changes_subsumed, deriv_path,
890 merged_path);
891 if (err)
892 goto done;
895 if (fchmod(merged_fd, st_mode) != 0) {
896 err = got_error_from_errno2("fchmod", merged_path);
897 goto done;
900 if (rename(merged_path, ondisk_path) != 0) {
901 err = got_error_from_errno3("rename", merged_path,
902 ondisk_path);
903 goto done;
905 done:
906 if (err) {
907 if (merged_path)
908 unlink(merged_path);
910 if (symlink_path) {
911 if (unlink(symlink_path) == -1 && err == NULL)
912 err = got_error_from_errno2("unlink", symlink_path);
914 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
915 err = got_error_from_errno2("fclose", symlink_path);
916 free(symlink_path);
917 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
918 err = got_error_from_errno("close");
919 if (f_orig && fclose(f_orig) == EOF && err == NULL)
920 err = got_error_from_errno("fclose");
921 free(merged_path);
922 free(base_path);
923 if (blob_orig_path) {
924 unlink(blob_orig_path);
925 free(blob_orig_path);
927 free(parent);
928 return err;
931 static const struct got_error *
932 update_symlink(const char *ondisk_path, const char *target_path,
933 size_t target_len)
935 /* This is not atomic but matches what 'ln -sf' does. */
936 if (unlink(ondisk_path) == -1)
937 return got_error_from_errno2("unlink", ondisk_path);
938 if (symlink(target_path, ondisk_path) == -1)
939 return got_error_from_errno3("symlink", target_path,
940 ondisk_path);
941 return NULL;
944 /*
945 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
946 * in the work tree with a file that contains conflict markers and the
947 * conflicting target paths of the original version, a "derived version"
948 * of a symlink from an incoming change, and a local version of the symlink.
950 * The original versions's target path can be NULL if it is not available,
951 * such as if both derived versions added a new symlink at the same path.
953 * The incoming derived symlink target is NULL in case the incoming change
954 * has deleted this symlink.
955 */
956 static const struct got_error *
957 install_symlink_conflict(const char *deriv_target,
958 struct got_object_id *deriv_base_commit_id, const char *orig_target,
959 const char *label_orig, const char *local_target, const char *ondisk_path)
961 const struct got_error *err;
962 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
963 FILE *f = NULL;
965 err = got_object_id_str(&id_str, deriv_base_commit_id);
966 if (err)
967 return got_error_from_errno("asprintf");
969 if (asprintf(&label_deriv, "%s: commit %s",
970 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
971 err = got_error_from_errno("asprintf");
972 goto done;
975 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
976 if (err)
977 goto done;
979 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
980 err = got_error_from_errno2("fchmod", path);
981 goto done;
984 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
985 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
986 deriv_target ? deriv_target : "(symlink was deleted)",
987 orig_target ? label_orig : "",
988 orig_target ? "\n" : "",
989 orig_target ? orig_target : "",
990 orig_target ? "\n" : "",
991 GOT_DIFF_CONFLICT_MARKER_SEP,
992 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
993 err = got_error_from_errno2("fprintf", path);
994 goto done;
997 if (unlink(ondisk_path) == -1) {
998 err = got_error_from_errno2("unlink", ondisk_path);
999 goto done;
1001 if (rename(path, ondisk_path) == -1) {
1002 err = got_error_from_errno3("rename", path, ondisk_path);
1003 goto done;
1005 done:
1006 if (f != NULL && fclose(f) == EOF && err == NULL)
1007 err = got_error_from_errno2("fclose", path);
1008 free(path);
1009 free(id_str);
1010 free(label_deriv);
1011 return err;
1014 /* forward declaration */
1015 static const struct got_error *
1016 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1017 const char *, const char *, uint16_t, const char *,
1018 struct got_blob_object *, struct got_object_id *,
1019 struct got_repository *, got_worktree_checkout_cb, void *);
1022 * Merge a symlink into the work tree, where blob_orig acts as the common
1023 * ancestor, deriv_target is the link target of the first derived version,
1024 * and the symlink on disk acts as the second derived version.
1025 * Assume that contents of both blobs represent symlinks.
1027 static const struct got_error *
1028 merge_symlink(struct got_worktree *worktree,
1029 struct got_blob_object *blob_orig, const char *ondisk_path,
1030 const char *path, const char *label_orig, const char *deriv_target,
1031 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1032 got_worktree_checkout_cb progress_cb, void *progress_arg)
1034 const struct got_error *err = NULL;
1035 char *ancestor_target = NULL;
1036 struct stat sb;
1037 ssize_t ondisk_len, deriv_len;
1038 char ondisk_target[PATH_MAX];
1039 int have_local_change = 0;
1040 int have_incoming_change = 0;
1042 if (lstat(ondisk_path, &sb) == -1)
1043 return got_error_from_errno2("lstat", ondisk_path);
1045 ondisk_len = readlink(ondisk_path, ondisk_target,
1046 sizeof(ondisk_target));
1047 if (ondisk_len == -1) {
1048 err = got_error_from_errno2("readlink",
1049 ondisk_path);
1050 goto done;
1052 ondisk_target[ondisk_len] = '\0';
1054 if (blob_orig) {
1055 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1056 if (err)
1057 goto done;
1060 if (ancestor_target == NULL ||
1061 (ondisk_len != strlen(ancestor_target) ||
1062 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1063 have_local_change = 1;
1065 deriv_len = strlen(deriv_target);
1066 if (ancestor_target == NULL ||
1067 (deriv_len != strlen(ancestor_target) ||
1068 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1069 have_incoming_change = 1;
1071 if (!have_local_change && !have_incoming_change) {
1072 if (ancestor_target) {
1073 /* Both sides made the same change. */
1074 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1075 path);
1076 } else if (deriv_len == ondisk_len &&
1077 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1078 /* Both sides added the same symlink. */
1079 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1080 path);
1081 } else {
1082 /* Both sides added symlinks which don't match. */
1083 err = install_symlink_conflict(deriv_target,
1084 deriv_base_commit_id, ancestor_target,
1085 label_orig, ondisk_target, ondisk_path);
1086 if (err)
1087 goto done;
1088 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1089 path);
1091 } else if (!have_local_change && have_incoming_change) {
1092 /* Apply the incoming change. */
1093 err = update_symlink(ondisk_path, deriv_target,
1094 strlen(deriv_target));
1095 if (err)
1096 goto done;
1097 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1098 } else if (have_local_change && have_incoming_change) {
1099 if (deriv_len == ondisk_len &&
1100 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1101 /* Both sides made the same change. */
1102 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1103 path);
1104 } else {
1105 err = install_symlink_conflict(deriv_target,
1106 deriv_base_commit_id, ancestor_target, label_orig,
1107 ondisk_target, ondisk_path);
1108 if (err)
1109 goto done;
1110 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1111 path);
1115 done:
1116 free(ancestor_target);
1117 return err;
1121 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1122 * blob_deriv acts as the first derived version, and the file on disk
1123 * acts as the second derived version.
1125 static const struct got_error *
1126 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1127 struct got_blob_object *blob_orig, const char *ondisk_path,
1128 const char *path, uint16_t st_mode, const char *label_orig,
1129 struct got_blob_object *blob_deriv,
1130 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1131 got_worktree_checkout_cb progress_cb, void *progress_arg)
1133 const struct got_error *err = NULL;
1134 FILE *f_deriv = NULL;
1135 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1136 char *label_deriv = NULL, *parent = NULL;
1138 *local_changes_subsumed = 0;
1140 err = got_path_dirname(&parent, ondisk_path);
1141 if (err)
1142 return err;
1144 free(base_path);
1145 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1146 err = got_error_from_errno("asprintf");
1147 base_path = NULL;
1148 goto done;
1151 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1152 if (err)
1153 goto done;
1154 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1155 blob_deriv);
1156 if (err)
1157 goto done;
1159 err = got_object_id_str(&id_str, deriv_base_commit_id);
1160 if (err)
1161 goto done;
1162 if (asprintf(&label_deriv, "%s: commit %s",
1163 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1164 err = got_error_from_errno("asprintf");
1165 goto done;
1168 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1169 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1170 label_deriv, repo, progress_cb, progress_arg);
1171 done:
1172 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1173 err = got_error_from_errno("fclose");
1174 free(base_path);
1175 if (blob_deriv_path) {
1176 unlink(blob_deriv_path);
1177 free(blob_deriv_path);
1179 free(id_str);
1180 free(label_deriv);
1181 free(parent);
1182 return err;
1185 static const struct got_error *
1186 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1187 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1188 int wt_fd, const char *path, struct got_object_id *blob_id)
1190 const struct got_error *err = NULL;
1191 struct got_fileindex_entry *new_ie;
1193 *new_iep = NULL;
1195 err = got_fileindex_entry_alloc(&new_ie, path);
1196 if (err)
1197 return err;
1199 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1200 blob_id->sha1, base_commit_id->sha1, 1);
1201 if (err)
1202 goto done;
1204 err = got_fileindex_entry_add(fileindex, new_ie);
1205 done:
1206 if (err)
1207 got_fileindex_entry_free(new_ie);
1208 else
1209 *new_iep = new_ie;
1210 return err;
1213 static mode_t
1214 get_ondisk_perms(int executable, mode_t st_mode)
1216 mode_t xbits = S_IXUSR;
1218 if (executable) {
1219 /* Map read bits to execute bits. */
1220 if (st_mode & S_IRGRP)
1221 xbits |= S_IXGRP;
1222 if (st_mode & S_IROTH)
1223 xbits |= S_IXOTH;
1224 return st_mode | xbits;
1227 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1230 /* forward declaration */
1231 static const struct got_error *
1232 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1233 const char *path, mode_t te_mode, mode_t st_mode,
1234 struct got_blob_object *blob, int restoring_missing_file,
1235 int reverting_versioned_file, int installing_bad_symlink,
1236 int path_is_unversioned, struct got_repository *repo,
1237 got_worktree_checkout_cb progress_cb, void *progress_arg);
1240 * This function assumes that the provided symlink target points at a
1241 * safe location in the work tree!
1243 static const struct got_error *
1244 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1245 size_t target_len)
1247 const struct got_error *err = NULL;
1248 ssize_t elen;
1249 char etarget[PATH_MAX];
1250 int fd;
1253 * "Bad" symlinks (those pointing outside the work tree or into the
1254 * .got directory) are installed in the work tree as a regular file
1255 * which contains the bad symlink target path.
1256 * The new symlink target has already been checked for safety by our
1257 * caller. If we can successfully open a regular file then we simply
1258 * replace this file with a symlink below.
1260 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1261 if (fd == -1) {
1262 if (errno != ELOOP)
1263 return got_error_from_errno2("open", ondisk_path);
1265 /* We are updating an existing on-disk symlink. */
1266 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1267 if (elen == -1)
1268 return got_error_from_errno2("readlink", ondisk_path);
1270 if (elen == target_len &&
1271 memcmp(etarget, target_path, target_len) == 0)
1272 return NULL; /* nothing to do */
1275 err = update_symlink(ondisk_path, target_path, target_len);
1276 if (fd != -1 && close(fd) == -1 && err == NULL)
1277 err = got_error_from_errno2("close", ondisk_path);
1278 return err;
1281 static const struct got_error *
1282 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1283 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1285 const struct got_error *err = NULL;
1286 char canonpath[PATH_MAX];
1287 char *path_got = NULL;
1289 *is_bad_symlink = 0;
1291 if (target_len >= sizeof(canonpath)) {
1292 *is_bad_symlink = 1;
1293 return NULL;
1297 * We do not use realpath(3) to resolve the symlink's target
1298 * path because we don't want to resolve symlinks recursively.
1299 * Instead we make the path absolute and then canonicalize it.
1300 * Relative symlink target lookup should begin at the directory
1301 * in which the blob object is being installed.
1303 if (!got_path_is_absolute(target_path)) {
1304 char *abspath, *parent;
1305 err = got_path_dirname(&parent, ondisk_path);
1306 if (err)
1307 return err;
1308 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1309 free(parent);
1310 return got_error_from_errno("asprintf");
1312 free(parent);
1313 if (strlen(abspath) >= sizeof(canonpath)) {
1314 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1315 free(abspath);
1316 return err;
1318 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1319 free(abspath);
1320 if (err)
1321 return err;
1322 } else {
1323 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1324 if (err)
1325 return err;
1328 /* Only allow symlinks pointing at paths within the work tree. */
1329 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1330 *is_bad_symlink = 1;
1331 return NULL;
1334 /* Do not allow symlinks pointing into the .got directory. */
1335 if (asprintf(&path_got, "%s/%s", wtroot_path,
1336 GOT_WORKTREE_GOT_DIR) == -1)
1337 return got_error_from_errno("asprintf");
1338 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1339 *is_bad_symlink = 1;
1341 free(path_got);
1342 return NULL;
1345 static const struct got_error *
1346 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1347 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1348 int restoring_missing_file, int reverting_versioned_file,
1349 int path_is_unversioned, struct got_repository *repo,
1350 got_worktree_checkout_cb progress_cb, void *progress_arg)
1352 const struct got_error *err = NULL;
1353 char target_path[PATH_MAX];
1354 size_t len, target_len = 0;
1355 char *path_got = NULL;
1356 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1357 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1359 *is_bad_symlink = 0;
1362 * Blob object content specifies the target path of the link.
1363 * If a symbolic link cannot be installed we instead create
1364 * a regular file which contains the link target path stored
1365 * in the blob object.
1367 do {
1368 err = got_object_blob_read_block(&len, blob);
1369 if (len + target_len >= sizeof(target_path)) {
1370 /* Path too long; install as a regular file. */
1371 *is_bad_symlink = 1;
1372 got_object_blob_rewind(blob);
1373 return install_blob(worktree, ondisk_path, path,
1374 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1375 restoring_missing_file, reverting_versioned_file,
1376 1, path_is_unversioned, repo, progress_cb,
1377 progress_arg);
1379 if (len > 0) {
1380 /* Skip blob object header first time around. */
1381 memcpy(target_path + target_len, buf + hdrlen,
1382 len - hdrlen);
1383 target_len += len - hdrlen;
1384 hdrlen = 0;
1386 } while (len != 0);
1387 target_path[target_len] = '\0';
1389 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1390 ondisk_path, worktree->root_path);
1391 if (err)
1392 return err;
1394 if (*is_bad_symlink) {
1395 /* install as a regular file */
1396 *is_bad_symlink = 1;
1397 got_object_blob_rewind(blob);
1398 err = install_blob(worktree, ondisk_path, path,
1399 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1400 restoring_missing_file, reverting_versioned_file, 1,
1401 path_is_unversioned, repo, progress_cb, progress_arg);
1402 goto done;
1405 if (symlink(target_path, ondisk_path) == -1) {
1406 if (errno == EEXIST) {
1407 if (path_is_unversioned) {
1408 err = (*progress_cb)(progress_arg,
1409 GOT_STATUS_UNVERSIONED, path);
1410 goto done;
1412 err = replace_existing_symlink(ondisk_path,
1413 target_path, target_len);
1414 if (err)
1415 goto done;
1416 if (progress_cb) {
1417 err = (*progress_cb)(progress_arg,
1418 reverting_versioned_file ?
1419 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1420 path);
1422 goto done; /* Nothing else to do. */
1425 if (errno == ENOENT) {
1426 char *parent;
1427 err = got_path_dirname(&parent, ondisk_path);
1428 if (err)
1429 goto done;
1430 err = add_dir_on_disk(worktree, parent);
1431 free(parent);
1432 if (err)
1433 goto done;
1435 * Retry, and fall through to error handling
1436 * below if this second attempt fails.
1438 if (symlink(target_path, ondisk_path) != -1) {
1439 err = NULL; /* success */
1440 goto done;
1444 /* Handle errors from first or second creation attempt. */
1445 if (errno == ENAMETOOLONG) {
1446 /* bad target path; install as a regular file */
1447 *is_bad_symlink = 1;
1448 got_object_blob_rewind(blob);
1449 err = install_blob(worktree, ondisk_path, path,
1450 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1451 restoring_missing_file, reverting_versioned_file, 1,
1452 path_is_unversioned, repo,
1453 progress_cb, progress_arg);
1454 } else if (errno == ENOTDIR) {
1455 err = got_error_path(ondisk_path,
1456 GOT_ERR_FILE_OBSTRUCTED);
1457 } else {
1458 err = got_error_from_errno3("symlink",
1459 target_path, ondisk_path);
1461 } else if (progress_cb)
1462 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1463 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1464 done:
1465 free(path_got);
1466 return err;
1469 static const struct got_error *
1470 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1471 const char *path, mode_t te_mode, mode_t st_mode,
1472 struct got_blob_object *blob, int restoring_missing_file,
1473 int reverting_versioned_file, int installing_bad_symlink,
1474 int path_is_unversioned, struct got_repository *repo,
1475 got_worktree_checkout_cb progress_cb, void *progress_arg)
1477 const struct got_error *err = NULL;
1478 int fd = -1;
1479 size_t len, hdrlen;
1480 int update = 0;
1481 char *tmppath = NULL;
1483 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1484 GOT_DEFAULT_FILE_MODE);
1485 if (fd == -1) {
1486 if (errno == ENOENT) {
1487 char *parent;
1488 err = got_path_dirname(&parent, path);
1489 if (err)
1490 return err;
1491 err = add_dir_on_disk(worktree, parent);
1492 free(parent);
1493 if (err)
1494 return err;
1495 fd = open(ondisk_path,
1496 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1497 GOT_DEFAULT_FILE_MODE);
1498 if (fd == -1)
1499 return got_error_from_errno2("open",
1500 ondisk_path);
1501 } else if (errno == EEXIST) {
1502 if (path_is_unversioned) {
1503 err = (*progress_cb)(progress_arg,
1504 GOT_STATUS_UNVERSIONED, path);
1505 goto done;
1507 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1508 !S_ISREG(st_mode) && !installing_bad_symlink) {
1509 /* TODO file is obstructed; do something */
1510 err = got_error_path(ondisk_path,
1511 GOT_ERR_FILE_OBSTRUCTED);
1512 goto done;
1513 } else {
1514 err = got_opentemp_named_fd(&tmppath, &fd,
1515 ondisk_path);
1516 if (err)
1517 goto done;
1518 update = 1;
1520 } else
1521 return got_error_from_errno2("open", ondisk_path);
1524 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1525 err = got_error_from_errno2("fchmod",
1526 update ? tmppath : ondisk_path);
1527 goto done;
1530 if (progress_cb) {
1531 if (restoring_missing_file)
1532 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1533 path);
1534 else if (reverting_versioned_file)
1535 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1536 path);
1537 else
1538 err = (*progress_cb)(progress_arg,
1539 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1540 if (err)
1541 goto done;
1544 hdrlen = got_object_blob_get_hdrlen(blob);
1545 do {
1546 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1547 err = got_object_blob_read_block(&len, blob);
1548 if (err)
1549 break;
1550 if (len > 0) {
1551 /* Skip blob object header first time around. */
1552 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1553 if (outlen == -1) {
1554 err = got_error_from_errno("write");
1555 goto done;
1556 } else if (outlen != len - hdrlen) {
1557 err = got_error(GOT_ERR_IO);
1558 goto done;
1560 hdrlen = 0;
1562 } while (len != 0);
1564 if (fsync(fd) != 0) {
1565 err = got_error_from_errno("fsync");
1566 goto done;
1569 if (update) {
1570 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1571 err = got_error_from_errno2("unlink", ondisk_path);
1572 goto done;
1574 if (rename(tmppath, ondisk_path) != 0) {
1575 err = got_error_from_errno3("rename", tmppath,
1576 ondisk_path);
1577 goto done;
1579 free(tmppath);
1580 tmppath = NULL;
1583 done:
1584 if (fd != -1 && close(fd) != 0 && err == NULL)
1585 err = got_error_from_errno("close");
1586 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1587 err = got_error_from_errno2("unlink", tmppath);
1588 free(tmppath);
1589 return err;
1592 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1593 static const struct got_error *
1594 get_modified_file_content_status(unsigned char *status, FILE *f)
1596 const struct got_error *err = NULL;
1597 const char *markers[3] = {
1598 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1599 GOT_DIFF_CONFLICT_MARKER_SEP,
1600 GOT_DIFF_CONFLICT_MARKER_END
1602 int i = 0;
1603 char *line = NULL;
1604 size_t linesize = 0;
1605 ssize_t linelen;
1607 while (*status == GOT_STATUS_MODIFY) {
1608 linelen = getline(&line, &linesize, f);
1609 if (linelen == -1) {
1610 if (feof(f))
1611 break;
1612 err = got_ferror(f, GOT_ERR_IO);
1613 break;
1616 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1617 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1618 == 0)
1619 *status = GOT_STATUS_CONFLICT;
1620 else
1621 i++;
1624 free(line);
1626 return err;
1629 static int
1630 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1632 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1633 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1636 static int
1637 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1639 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1640 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1641 ie->mtime_sec == sb->st_mtim.tv_sec &&
1642 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1643 ie->size == (sb->st_size & 0xffffffff) &&
1644 !xbit_differs(ie, sb->st_mode));
1647 static unsigned char
1648 get_staged_status(struct got_fileindex_entry *ie)
1650 switch (got_fileindex_entry_stage_get(ie)) {
1651 case GOT_FILEIDX_STAGE_ADD:
1652 return GOT_STATUS_ADD;
1653 case GOT_FILEIDX_STAGE_DELETE:
1654 return GOT_STATUS_DELETE;
1655 case GOT_FILEIDX_STAGE_MODIFY:
1656 return GOT_STATUS_MODIFY;
1657 default:
1658 return GOT_STATUS_NO_CHANGE;
1662 static const struct got_error *
1663 get_symlink_modification_status(unsigned char *status,
1664 struct got_fileindex_entry *ie, const char *abspath,
1665 int dirfd, const char *de_name, struct got_blob_object *blob)
1667 const struct got_error *err = NULL;
1668 char target_path[PATH_MAX];
1669 char etarget[PATH_MAX];
1670 ssize_t elen;
1671 size_t len, target_len = 0;
1672 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1673 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1675 *status = GOT_STATUS_NO_CHANGE;
1677 /* Blob object content specifies the target path of the link. */
1678 do {
1679 err = got_object_blob_read_block(&len, blob);
1680 if (err)
1681 return err;
1682 if (len + target_len >= sizeof(target_path)) {
1684 * Should not happen. The blob contents were OK
1685 * when this symlink was installed.
1687 return got_error(GOT_ERR_NO_SPACE);
1689 if (len > 0) {
1690 /* Skip blob object header first time around. */
1691 memcpy(target_path + target_len, buf + hdrlen,
1692 len - hdrlen);
1693 target_len += len - hdrlen;
1694 hdrlen = 0;
1696 } while (len != 0);
1697 target_path[target_len] = '\0';
1699 if (dirfd != -1) {
1700 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1701 if (elen == -1)
1702 return got_error_from_errno2("readlinkat", abspath);
1703 } else {
1704 elen = readlink(abspath, etarget, sizeof(etarget));
1705 if (elen == -1)
1706 return got_error_from_errno2("readlink", abspath);
1709 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1710 *status = GOT_STATUS_MODIFY;
1712 return NULL;
1715 static const struct got_error *
1716 get_file_status(unsigned char *status, struct stat *sb,
1717 struct got_fileindex_entry *ie, const char *abspath,
1718 int dirfd, const char *de_name, struct got_repository *repo)
1720 const struct got_error *err = NULL;
1721 struct got_object_id id;
1722 size_t hdrlen;
1723 int fd = -1;
1724 FILE *f = NULL;
1725 uint8_t fbuf[8192];
1726 struct got_blob_object *blob = NULL;
1727 size_t flen, blen;
1728 unsigned char staged_status = get_staged_status(ie);
1730 *status = GOT_STATUS_NO_CHANGE;
1733 * Whenever the caller provides a directory descriptor and a
1734 * directory entry name for the file, use them! This prevents
1735 * race conditions if filesystem paths change beneath our feet.
1737 if (dirfd != -1) {
1738 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1739 if (errno == ENOENT) {
1740 if (got_fileindex_entry_has_file_on_disk(ie))
1741 *status = GOT_STATUS_MISSING;
1742 else
1743 *status = GOT_STATUS_DELETE;
1744 goto done;
1746 err = got_error_from_errno2("fstatat", abspath);
1747 goto done;
1749 } else {
1750 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1751 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1752 return got_error_from_errno2("open", abspath);
1753 else if (fd == -1 && errno == ELOOP) {
1754 if (lstat(abspath, sb) == -1)
1755 return got_error_from_errno2("lstat", abspath);
1756 } else if (fd == -1 || fstat(fd, sb) == -1) {
1757 if (errno == ENOENT) {
1758 if (got_fileindex_entry_has_file_on_disk(ie))
1759 *status = GOT_STATUS_MISSING;
1760 else
1761 *status = GOT_STATUS_DELETE;
1762 goto done;
1764 err = got_error_from_errno2("fstat", abspath);
1765 goto done;
1769 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1770 *status = GOT_STATUS_OBSTRUCTED;
1771 goto done;
1774 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1775 *status = GOT_STATUS_DELETE;
1776 goto done;
1777 } else if (!got_fileindex_entry_has_blob(ie) &&
1778 staged_status != GOT_STATUS_ADD) {
1779 *status = GOT_STATUS_ADD;
1780 goto done;
1783 if (!stat_info_differs(ie, sb))
1784 goto done;
1786 if (S_ISLNK(sb->st_mode) &&
1787 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1788 *status = GOT_STATUS_MODIFY;
1789 goto done;
1792 if (staged_status == GOT_STATUS_MODIFY ||
1793 staged_status == GOT_STATUS_ADD)
1794 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1795 else
1796 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1798 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1799 if (err)
1800 goto done;
1802 if (S_ISLNK(sb->st_mode)) {
1803 err = get_symlink_modification_status(status, ie,
1804 abspath, dirfd, de_name, blob);
1805 goto done;
1808 if (dirfd != -1) {
1809 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1810 if (fd == -1) {
1811 err = got_error_from_errno2("openat", abspath);
1812 goto done;
1816 f = fdopen(fd, "r");
1817 if (f == NULL) {
1818 err = got_error_from_errno2("fdopen", abspath);
1819 goto done;
1821 fd = -1;
1822 hdrlen = got_object_blob_get_hdrlen(blob);
1823 for (;;) {
1824 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1825 err = got_object_blob_read_block(&blen, blob);
1826 if (err)
1827 goto done;
1828 /* Skip length of blob object header first time around. */
1829 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1830 if (flen == 0 && ferror(f)) {
1831 err = got_error_from_errno("fread");
1832 goto done;
1834 if (blen - hdrlen == 0) {
1835 if (flen != 0)
1836 *status = GOT_STATUS_MODIFY;
1837 break;
1838 } else if (flen == 0) {
1839 if (blen - hdrlen != 0)
1840 *status = GOT_STATUS_MODIFY;
1841 break;
1842 } else if (blen - hdrlen == flen) {
1843 /* Skip blob object header first time around. */
1844 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1845 *status = GOT_STATUS_MODIFY;
1846 break;
1848 } else {
1849 *status = GOT_STATUS_MODIFY;
1850 break;
1852 hdrlen = 0;
1855 if (*status == GOT_STATUS_MODIFY) {
1856 rewind(f);
1857 err = get_modified_file_content_status(status, f);
1858 } else if (xbit_differs(ie, sb->st_mode))
1859 *status = GOT_STATUS_MODE_CHANGE;
1860 done:
1861 if (blob)
1862 got_object_blob_close(blob);
1863 if (f != NULL && fclose(f) == EOF && err == NULL)
1864 err = got_error_from_errno2("fclose", abspath);
1865 if (fd != -1 && close(fd) == -1 && err == NULL)
1866 err = got_error_from_errno2("close", abspath);
1867 return err;
1871 * Update timestamps in the file index if a file is unmodified and
1872 * we had to run a full content comparison to find out.
1874 static const struct got_error *
1875 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1876 struct got_fileindex_entry *ie, struct stat *sb)
1878 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1879 return got_fileindex_entry_update(ie, wt_fd, path,
1880 ie->blob_sha1, ie->commit_sha1, 1);
1882 return NULL;
1885 static const struct got_error *
1886 update_blob(struct got_worktree *worktree,
1887 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1888 struct got_tree_entry *te, const char *path,
1889 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1890 void *progress_arg)
1892 const struct got_error *err = NULL;
1893 struct got_blob_object *blob = NULL;
1894 char *ondisk_path;
1895 unsigned char status = GOT_STATUS_NO_CHANGE;
1896 struct stat sb;
1898 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1899 return got_error_from_errno("asprintf");
1901 if (ie) {
1902 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1903 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1904 goto done;
1906 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1907 repo);
1908 if (err)
1909 goto done;
1910 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1911 sb.st_mode = got_fileindex_perms_to_st(ie);
1912 } else {
1913 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1914 status = GOT_STATUS_UNVERSIONED;
1917 if (status == GOT_STATUS_OBSTRUCTED) {
1918 err = (*progress_cb)(progress_arg, status, path);
1919 goto done;
1921 if (status == GOT_STATUS_CONFLICT) {
1922 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1923 path);
1924 goto done;
1927 if (ie && status != GOT_STATUS_MISSING &&
1928 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1929 if (got_fileindex_entry_has_commit(ie) &&
1930 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1931 SHA1_DIGEST_LENGTH) == 0) {
1932 err = sync_timestamps(worktree->root_fd,
1933 path, status, ie, &sb);
1934 if (err)
1935 goto done;
1936 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1937 path);
1938 goto done;
1940 if (got_fileindex_entry_has_blob(ie) &&
1941 memcmp(ie->blob_sha1, te->id.sha1,
1942 SHA1_DIGEST_LENGTH) == 0) {
1943 err = sync_timestamps(worktree->root_fd,
1944 path, status, ie, &sb);
1945 goto done;
1949 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1950 if (err)
1951 goto done;
1953 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1954 int update_timestamps;
1955 struct got_blob_object *blob2 = NULL;
1956 char *label_orig = NULL;
1957 if (got_fileindex_entry_has_blob(ie)) {
1958 struct got_object_id id2;
1959 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1960 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1961 if (err)
1962 goto done;
1964 if (got_fileindex_entry_has_commit(ie)) {
1965 char id_str[SHA1_DIGEST_STRING_LENGTH];
1966 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1967 sizeof(id_str)) == NULL) {
1968 err = got_error_path(id_str,
1969 GOT_ERR_BAD_OBJ_ID_STR);
1970 goto done;
1972 if (asprintf(&label_orig, "%s: commit %s",
1973 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1974 err = got_error_from_errno("asprintf");
1975 goto done;
1978 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1979 char *link_target;
1980 err = got_object_blob_read_to_str(&link_target, blob);
1981 if (err)
1982 goto done;
1983 err = merge_symlink(worktree, blob2, ondisk_path, path,
1984 label_orig, link_target, worktree->base_commit_id,
1985 repo, progress_cb, progress_arg);
1986 free(link_target);
1987 } else {
1988 err = merge_blob(&update_timestamps, worktree, blob2,
1989 ondisk_path, path, sb.st_mode, label_orig, blob,
1990 worktree->base_commit_id, repo,
1991 progress_cb, progress_arg);
1993 free(label_orig);
1994 if (blob2)
1995 got_object_blob_close(blob2);
1996 if (err)
1997 goto done;
1999 * Do not update timestamps of files with local changes.
2000 * Otherwise, a future status walk would treat them as
2001 * unmodified files again.
2003 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2004 blob->id.sha1, worktree->base_commit_id->sha1,
2005 update_timestamps);
2006 } else if (status == GOT_STATUS_MODE_CHANGE) {
2007 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2008 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2009 } else if (status == GOT_STATUS_DELETE) {
2010 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2011 if (err)
2012 goto done;
2013 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2014 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2015 if (err)
2016 goto done;
2017 } else {
2018 int is_bad_symlink = 0;
2019 if (S_ISLNK(te->mode)) {
2020 err = install_symlink(&is_bad_symlink, worktree,
2021 ondisk_path, path, blob,
2022 status == GOT_STATUS_MISSING, 0,
2023 status == GOT_STATUS_UNVERSIONED, repo,
2024 progress_cb, progress_arg);
2025 } else {
2026 err = install_blob(worktree, ondisk_path, path,
2027 te->mode, sb.st_mode, blob,
2028 status == GOT_STATUS_MISSING, 0, 0,
2029 status == GOT_STATUS_UNVERSIONED, repo,
2030 progress_cb, progress_arg);
2032 if (err)
2033 goto done;
2035 if (ie) {
2036 err = got_fileindex_entry_update(ie,
2037 worktree->root_fd, path, blob->id.sha1,
2038 worktree->base_commit_id->sha1, 1);
2039 } else {
2040 err = create_fileindex_entry(&ie, fileindex,
2041 worktree->base_commit_id, worktree->root_fd, path,
2042 &blob->id);
2044 if (err)
2045 goto done;
2047 if (is_bad_symlink) {
2048 got_fileindex_entry_filetype_set(ie,
2049 GOT_FILEIDX_MODE_BAD_SYMLINK);
2052 got_object_blob_close(blob);
2053 done:
2054 free(ondisk_path);
2055 return err;
2058 static const struct got_error *
2059 remove_ondisk_file(const char *root_path, const char *path)
2061 const struct got_error *err = NULL;
2062 char *ondisk_path = NULL;
2064 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2065 return got_error_from_errno("asprintf");
2067 if (unlink(ondisk_path) == -1) {
2068 if (errno != ENOENT)
2069 err = got_error_from_errno2("unlink", ondisk_path);
2070 } else {
2071 size_t root_len = strlen(root_path);
2072 do {
2073 char *parent;
2074 err = got_path_dirname(&parent, ondisk_path);
2075 if (err)
2076 break;
2077 free(ondisk_path);
2078 ondisk_path = parent;
2079 if (rmdir(ondisk_path) == -1) {
2080 if (errno != ENOTEMPTY)
2081 err = got_error_from_errno2("rmdir",
2082 ondisk_path);
2083 break;
2085 } while (got_path_cmp(ondisk_path, root_path,
2086 strlen(ondisk_path), root_len) != 0);
2088 free(ondisk_path);
2089 return err;
2092 static const struct got_error *
2093 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2094 struct got_fileindex_entry *ie, struct got_repository *repo,
2095 got_worktree_checkout_cb progress_cb, void *progress_arg)
2097 const struct got_error *err = NULL;
2098 unsigned char status;
2099 struct stat sb;
2100 char *ondisk_path;
2102 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2103 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2105 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2106 == -1)
2107 return got_error_from_errno("asprintf");
2109 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2110 if (err)
2111 goto done;
2113 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2114 char ondisk_target[PATH_MAX];
2115 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2116 sizeof(ondisk_target));
2117 if (ondisk_len == -1) {
2118 err = got_error_from_errno2("readlink", ondisk_path);
2119 goto done;
2121 ondisk_target[ondisk_len] = '\0';
2122 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2123 NULL, NULL, /* XXX pass common ancestor info? */
2124 ondisk_target, ondisk_path);
2125 if (err)
2126 goto done;
2127 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2128 ie->path);
2129 goto done;
2132 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2133 status == GOT_STATUS_ADD) {
2134 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2135 if (err)
2136 goto done;
2138 * Preserve the working file and change the deleted blob's
2139 * entry into a schedule-add entry.
2141 err = got_fileindex_entry_update(ie, worktree->root_fd,
2142 ie->path, NULL, NULL, 0);
2143 } else {
2144 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2145 if (err)
2146 goto done;
2147 if (status == GOT_STATUS_NO_CHANGE) {
2148 err = remove_ondisk_file(worktree->root_path, ie->path);
2149 if (err)
2150 goto done;
2152 got_fileindex_entry_remove(fileindex, ie);
2154 done:
2155 free(ondisk_path);
2156 return err;
2159 struct diff_cb_arg {
2160 struct got_fileindex *fileindex;
2161 struct got_worktree *worktree;
2162 struct got_repository *repo;
2163 got_worktree_checkout_cb progress_cb;
2164 void *progress_arg;
2165 got_cancel_cb cancel_cb;
2166 void *cancel_arg;
2169 static const struct got_error *
2170 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2171 struct got_tree_entry *te, const char *parent_path)
2173 struct diff_cb_arg *a = arg;
2175 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2176 return got_error(GOT_ERR_CANCELLED);
2178 return update_blob(a->worktree, a->fileindex, ie, te,
2179 ie->path, a->repo, a->progress_cb, a->progress_arg);
2182 static const struct got_error *
2183 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2185 struct diff_cb_arg *a = arg;
2187 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2188 return got_error(GOT_ERR_CANCELLED);
2190 return delete_blob(a->worktree, a->fileindex, ie,
2191 a->repo, a->progress_cb, a->progress_arg);
2194 static const struct got_error *
2195 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2197 struct diff_cb_arg *a = arg;
2198 const struct got_error *err;
2199 char *path;
2201 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2202 return got_error(GOT_ERR_CANCELLED);
2204 if (got_object_tree_entry_is_submodule(te))
2205 return NULL;
2207 if (asprintf(&path, "%s%s%s", parent_path,
2208 parent_path[0] ? "/" : "", te->name)
2209 == -1)
2210 return got_error_from_errno("asprintf");
2212 if (S_ISDIR(te->mode))
2213 err = add_dir_on_disk(a->worktree, path);
2214 else
2215 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2216 a->repo, a->progress_cb, a->progress_arg);
2218 free(path);
2219 return err;
2222 const struct got_error *
2223 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2225 uint32_t uuid_status;
2227 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2228 if (uuid_status != uuid_s_ok) {
2229 *uuidstr = NULL;
2230 return got_error_uuid(uuid_status, "uuid_to_string");
2233 return NULL;
2236 static const struct got_error *
2237 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2239 const struct got_error *err = NULL;
2240 char *uuidstr = NULL;
2242 *refname = NULL;
2244 err = got_worktree_get_uuid(&uuidstr, worktree);
2245 if (err)
2246 return err;
2248 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2249 err = got_error_from_errno("asprintf");
2250 *refname = NULL;
2252 free(uuidstr);
2253 return err;
2256 const struct got_error *
2257 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2259 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2262 static const struct got_error *
2263 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2265 return get_ref_name(refname, worktree,
2266 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2269 static const struct got_error *
2270 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2272 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2275 static const struct got_error *
2276 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2278 return get_ref_name(refname, worktree,
2279 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2282 static const struct got_error *
2283 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2285 return get_ref_name(refname, worktree,
2286 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2289 static const struct got_error *
2290 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2292 return get_ref_name(refname, worktree,
2293 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2296 static const struct got_error *
2297 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2299 return get_ref_name(refname, worktree,
2300 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2303 static const struct got_error *
2304 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2306 return get_ref_name(refname, worktree,
2307 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2310 static const struct got_error *
2311 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2313 return get_ref_name(refname, worktree,
2314 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2317 const struct got_error *
2318 got_worktree_get_histedit_script_path(char **path,
2319 struct got_worktree *worktree)
2321 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2322 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2323 *path = NULL;
2324 return got_error_from_errno("asprintf");
2326 return NULL;
2330 * Prevent Git's garbage collector from deleting our base commit by
2331 * setting a reference to our base commit's ID.
2333 static const struct got_error *
2334 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2336 const struct got_error *err = NULL;
2337 struct got_reference *ref = NULL;
2338 char *refname;
2340 err = got_worktree_get_base_ref_name(&refname, worktree);
2341 if (err)
2342 return err;
2344 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2345 if (err)
2346 goto done;
2348 err = got_ref_write(ref, repo);
2349 done:
2350 free(refname);
2351 if (ref)
2352 got_ref_close(ref);
2353 return err;
2356 static const struct got_error *
2357 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2359 const struct got_error *err = NULL;
2361 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2362 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2363 err = got_error_from_errno("asprintf");
2364 *fileindex_path = NULL;
2366 return err;
2370 static const struct got_error *
2371 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2372 struct got_worktree *worktree)
2374 const struct got_error *err = NULL;
2375 FILE *index = NULL;
2377 *fileindex_path = NULL;
2378 *fileindex = got_fileindex_alloc();
2379 if (*fileindex == NULL)
2380 return got_error_from_errno("got_fileindex_alloc");
2382 err = get_fileindex_path(fileindex_path, worktree);
2383 if (err)
2384 goto done;
2386 index = fopen(*fileindex_path, "rb");
2387 if (index == NULL) {
2388 if (errno != ENOENT)
2389 err = got_error_from_errno2("fopen", *fileindex_path);
2390 } else {
2391 err = got_fileindex_read(*fileindex, index);
2392 if (fclose(index) == EOF && err == NULL)
2393 err = got_error_from_errno("fclose");
2395 done:
2396 if (err) {
2397 free(*fileindex_path);
2398 *fileindex_path = NULL;
2399 got_fileindex_free(*fileindex);
2400 *fileindex = NULL;
2402 return err;
2405 struct bump_base_commit_id_arg {
2406 struct got_object_id *base_commit_id;
2407 const char *path;
2408 size_t path_len;
2409 const char *entry_name;
2410 got_worktree_checkout_cb progress_cb;
2411 void *progress_arg;
2414 /* Bump base commit ID of all files within an updated part of the work tree. */
2415 static const struct got_error *
2416 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2418 const struct got_error *err;
2419 struct bump_base_commit_id_arg *a = arg;
2421 if (a->entry_name) {
2422 if (strcmp(ie->path, a->path) != 0)
2423 return NULL;
2424 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2425 return NULL;
2427 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2428 SHA1_DIGEST_LENGTH) == 0)
2429 return NULL;
2431 if (a->progress_cb) {
2432 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2433 ie->path);
2434 if (err)
2435 return err;
2437 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2438 return NULL;
2441 static const struct got_error *
2442 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2443 struct got_fileindex *fileindex,
2444 got_worktree_checkout_cb progress_cb, void *progress_arg)
2446 struct bump_base_commit_id_arg bbc_arg;
2448 bbc_arg.base_commit_id = worktree->base_commit_id;
2449 bbc_arg.entry_name = NULL;
2450 bbc_arg.path = "";
2451 bbc_arg.path_len = 0;
2452 bbc_arg.progress_cb = progress_cb;
2453 bbc_arg.progress_arg = progress_arg;
2455 return got_fileindex_for_each_entry_safe(fileindex,
2456 bump_base_commit_id, &bbc_arg);
2459 static const struct got_error *
2460 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2462 const struct got_error *err = NULL;
2463 char *new_fileindex_path = NULL;
2464 FILE *new_index = NULL;
2465 struct timespec timeout;
2467 err = got_opentemp_named(&new_fileindex_path, &new_index,
2468 fileindex_path);
2469 if (err)
2470 goto done;
2472 err = got_fileindex_write(fileindex, new_index);
2473 if (err)
2474 goto done;
2476 if (rename(new_fileindex_path, fileindex_path) != 0) {
2477 err = got_error_from_errno3("rename", new_fileindex_path,
2478 fileindex_path);
2479 unlink(new_fileindex_path);
2483 * Sleep for a short amount of time to ensure that files modified after
2484 * this program exits have a different time stamp from the one which
2485 * was recorded in the file index.
2487 timeout.tv_sec = 0;
2488 timeout.tv_nsec = 1;
2489 nanosleep(&timeout, NULL);
2490 done:
2491 if (new_index)
2492 fclose(new_index);
2493 free(new_fileindex_path);
2494 return err;
2497 static const struct got_error *
2498 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2499 struct got_object_id **tree_id, const char *wt_relpath,
2500 struct got_worktree *worktree, struct got_repository *repo)
2502 const struct got_error *err = NULL;
2503 struct got_object_id *id = NULL;
2504 char *in_repo_path = NULL;
2505 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2507 *entry_type = GOT_OBJ_TYPE_ANY;
2508 *tree_relpath = NULL;
2509 *tree_id = NULL;
2511 if (wt_relpath[0] == '\0') {
2512 /* Check out all files within the work tree. */
2513 *entry_type = GOT_OBJ_TYPE_TREE;
2514 *tree_relpath = strdup("");
2515 if (*tree_relpath == NULL) {
2516 err = got_error_from_errno("strdup");
2517 goto done;
2519 err = got_object_id_by_path(tree_id, repo,
2520 worktree->base_commit_id, worktree->path_prefix);
2521 if (err)
2522 goto done;
2523 return NULL;
2526 /* Check out a subset of files in the work tree. */
2528 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2529 is_root_wt ? "" : "/", wt_relpath) == -1) {
2530 err = got_error_from_errno("asprintf");
2531 goto done;
2534 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2535 in_repo_path);
2536 if (err)
2537 goto done;
2539 free(in_repo_path);
2540 in_repo_path = NULL;
2542 err = got_object_get_type(entry_type, repo, id);
2543 if (err)
2544 goto done;
2546 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2547 /* Check out a single file. */
2548 if (strchr(wt_relpath, '/') == NULL) {
2549 /* Check out a single file in work tree's root dir. */
2550 in_repo_path = strdup(worktree->path_prefix);
2551 if (in_repo_path == NULL) {
2552 err = got_error_from_errno("strdup");
2553 goto done;
2555 *tree_relpath = strdup("");
2556 if (*tree_relpath == NULL) {
2557 err = got_error_from_errno("strdup");
2558 goto done;
2560 } else {
2561 /* Check out a single file in a subdirectory. */
2562 err = got_path_dirname(tree_relpath, wt_relpath);
2563 if (err)
2564 return err;
2565 if (asprintf(&in_repo_path, "%s%s%s",
2566 worktree->path_prefix, is_root_wt ? "" : "/",
2567 *tree_relpath) == -1) {
2568 err = got_error_from_errno("asprintf");
2569 goto done;
2572 err = got_object_id_by_path(tree_id, repo,
2573 worktree->base_commit_id, in_repo_path);
2574 } else {
2575 /* Check out all files within a subdirectory. */
2576 *tree_id = got_object_id_dup(id);
2577 if (*tree_id == NULL) {
2578 err = got_error_from_errno("got_object_id_dup");
2579 goto done;
2581 *tree_relpath = strdup(wt_relpath);
2582 if (*tree_relpath == NULL) {
2583 err = got_error_from_errno("strdup");
2584 goto done;
2587 done:
2588 free(id);
2589 free(in_repo_path);
2590 if (err) {
2591 *entry_type = GOT_OBJ_TYPE_ANY;
2592 free(*tree_relpath);
2593 *tree_relpath = NULL;
2594 free(*tree_id);
2595 *tree_id = NULL;
2597 return err;
2600 static const struct got_error *
2601 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2602 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2603 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2604 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2606 const struct got_error *err = NULL;
2607 struct got_commit_object *commit = NULL;
2608 struct got_tree_object *tree = NULL;
2609 struct got_fileindex_diff_tree_cb diff_cb;
2610 struct diff_cb_arg arg;
2612 err = ref_base_commit(worktree, repo);
2613 if (err) {
2614 if (!(err->code == GOT_ERR_ERRNO &&
2615 (errno == EACCES || errno == EROFS)))
2616 goto done;
2617 err = (*progress_cb)(progress_arg,
2618 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2619 if (err)
2620 return err;
2623 err = got_object_open_as_commit(&commit, repo,
2624 worktree->base_commit_id);
2625 if (err)
2626 goto done;
2628 err = got_object_open_as_tree(&tree, repo, tree_id);
2629 if (err)
2630 goto done;
2632 if (entry_name &&
2633 got_object_tree_find_entry(tree, entry_name) == NULL) {
2634 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2635 goto done;
2638 diff_cb.diff_old_new = diff_old_new;
2639 diff_cb.diff_old = diff_old;
2640 diff_cb.diff_new = diff_new;
2641 arg.fileindex = fileindex;
2642 arg.worktree = worktree;
2643 arg.repo = repo;
2644 arg.progress_cb = progress_cb;
2645 arg.progress_arg = progress_arg;
2646 arg.cancel_cb = cancel_cb;
2647 arg.cancel_arg = cancel_arg;
2648 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2649 entry_name, repo, &diff_cb, &arg);
2650 done:
2651 if (tree)
2652 got_object_tree_close(tree);
2653 if (commit)
2654 got_object_commit_close(commit);
2655 return err;
2658 const struct got_error *
2659 got_worktree_checkout_files(struct got_worktree *worktree,
2660 struct got_pathlist_head *paths, struct got_repository *repo,
2661 got_worktree_checkout_cb progress_cb, void *progress_arg,
2662 got_cancel_cb cancel_cb, void *cancel_arg)
2664 const struct got_error *err = NULL, *sync_err, *unlockerr;
2665 struct got_commit_object *commit = NULL;
2666 struct got_tree_object *tree = NULL;
2667 struct got_fileindex *fileindex = NULL;
2668 char *fileindex_path = NULL;
2669 struct got_pathlist_entry *pe;
2670 struct tree_path_data {
2671 SIMPLEQ_ENTRY(tree_path_data) entry;
2672 struct got_object_id *tree_id;
2673 int entry_type;
2674 char *relpath;
2675 char *entry_name;
2676 } *tpd = NULL;
2677 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2679 SIMPLEQ_INIT(&tree_paths);
2681 err = lock_worktree(worktree, LOCK_EX);
2682 if (err)
2683 return err;
2685 /* Map all specified paths to in-repository trees. */
2686 TAILQ_FOREACH(pe, paths, entry) {
2687 tpd = malloc(sizeof(*tpd));
2688 if (tpd == NULL) {
2689 err = got_error_from_errno("malloc");
2690 goto done;
2693 err = find_tree_entry_for_checkout(&tpd->entry_type,
2694 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2695 if (err) {
2696 free(tpd);
2697 goto done;
2700 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2701 err = got_path_basename(&tpd->entry_name, pe->path);
2702 if (err) {
2703 free(tpd->relpath);
2704 free(tpd->tree_id);
2705 free(tpd);
2706 goto done;
2708 } else
2709 tpd->entry_name = NULL;
2711 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2715 * Read the file index.
2716 * Checking out files is supposed to be an idempotent operation.
2717 * If the on-disk file index is incomplete we will try to complete it.
2719 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2720 if (err)
2721 goto done;
2723 tpd = SIMPLEQ_FIRST(&tree_paths);
2724 TAILQ_FOREACH(pe, paths, entry) {
2725 struct bump_base_commit_id_arg bbc_arg;
2727 err = checkout_files(worktree, fileindex, tpd->relpath,
2728 tpd->tree_id, tpd->entry_name, repo,
2729 progress_cb, progress_arg, cancel_cb, cancel_arg);
2730 if (err)
2731 break;
2733 bbc_arg.base_commit_id = worktree->base_commit_id;
2734 bbc_arg.entry_name = tpd->entry_name;
2735 bbc_arg.path = pe->path;
2736 bbc_arg.path_len = pe->path_len;
2737 bbc_arg.progress_cb = progress_cb;
2738 bbc_arg.progress_arg = progress_arg;
2739 err = got_fileindex_for_each_entry_safe(fileindex,
2740 bump_base_commit_id, &bbc_arg);
2741 if (err)
2742 break;
2744 tpd = SIMPLEQ_NEXT(tpd, entry);
2746 sync_err = sync_fileindex(fileindex, fileindex_path);
2747 if (sync_err && err == NULL)
2748 err = sync_err;
2749 done:
2750 free(fileindex_path);
2751 if (tree)
2752 got_object_tree_close(tree);
2753 if (commit)
2754 got_object_commit_close(commit);
2755 if (fileindex)
2756 got_fileindex_free(fileindex);
2757 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2758 tpd = SIMPLEQ_FIRST(&tree_paths);
2759 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2760 free(tpd->relpath);
2761 free(tpd->tree_id);
2762 free(tpd);
2764 unlockerr = lock_worktree(worktree, LOCK_SH);
2765 if (unlockerr && err == NULL)
2766 err = unlockerr;
2767 return err;
2770 struct merge_file_cb_arg {
2771 struct got_worktree *worktree;
2772 struct got_fileindex *fileindex;
2773 got_worktree_checkout_cb progress_cb;
2774 void *progress_arg;
2775 got_cancel_cb cancel_cb;
2776 void *cancel_arg;
2777 const char *label_orig;
2778 struct got_object_id *commit_id2;
2781 static const struct got_error *
2782 merge_file_cb(void *arg, struct got_blob_object *blob1,
2783 struct got_blob_object *blob2, struct got_object_id *id1,
2784 struct got_object_id *id2, const char *path1, const char *path2,
2785 mode_t mode1, mode_t mode2, struct got_repository *repo)
2787 static const struct got_error *err = NULL;
2788 struct merge_file_cb_arg *a = arg;
2789 struct got_fileindex_entry *ie;
2790 char *ondisk_path = NULL;
2791 struct stat sb;
2792 unsigned char status;
2793 int local_changes_subsumed;
2795 if (blob1 && blob2) {
2796 ie = got_fileindex_entry_get(a->fileindex, path2,
2797 strlen(path2));
2798 if (ie == NULL)
2799 return (*a->progress_cb)(a->progress_arg,
2800 GOT_STATUS_MISSING, path2);
2802 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2803 path2) == -1)
2804 return got_error_from_errno("asprintf");
2806 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2807 repo);
2808 if (err)
2809 goto done;
2811 if (status == GOT_STATUS_DELETE) {
2812 err = (*a->progress_cb)(a->progress_arg,
2813 GOT_STATUS_MERGE, path2);
2814 goto done;
2816 if (status != GOT_STATUS_NO_CHANGE &&
2817 status != GOT_STATUS_MODIFY &&
2818 status != GOT_STATUS_CONFLICT &&
2819 status != GOT_STATUS_ADD) {
2820 err = (*a->progress_cb)(a->progress_arg, status, path2);
2821 goto done;
2824 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2825 char *link_target2;
2826 err = got_object_blob_read_to_str(&link_target2, blob2);
2827 if (err)
2828 goto done;
2829 err = merge_symlink(a->worktree, blob1, ondisk_path,
2830 path2, a->label_orig, link_target2, a->commit_id2,
2831 repo, a->progress_cb, a->progress_arg);
2832 free(link_target2);
2833 } else {
2834 err = merge_blob(&local_changes_subsumed, a->worktree,
2835 blob1, ondisk_path, path2, sb.st_mode,
2836 a->label_orig, blob2, a->commit_id2, repo,
2837 a->progress_cb, a->progress_arg);
2839 } else if (blob1) {
2840 ie = got_fileindex_entry_get(a->fileindex, path1,
2841 strlen(path1));
2842 if (ie == NULL)
2843 return (*a->progress_cb)(a->progress_arg,
2844 GOT_STATUS_MISSING, path1);
2846 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2847 path1) == -1)
2848 return got_error_from_errno("asprintf");
2850 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2851 repo);
2852 if (err)
2853 goto done;
2855 switch (status) {
2856 case GOT_STATUS_NO_CHANGE:
2857 err = (*a->progress_cb)(a->progress_arg,
2858 GOT_STATUS_DELETE, path1);
2859 if (err)
2860 goto done;
2861 err = remove_ondisk_file(a->worktree->root_path, path1);
2862 if (err)
2863 goto done;
2864 if (ie)
2865 got_fileindex_entry_mark_deleted_from_disk(ie);
2866 break;
2867 case GOT_STATUS_DELETE:
2868 case GOT_STATUS_MISSING:
2869 err = (*a->progress_cb)(a->progress_arg,
2870 GOT_STATUS_DELETE, path1);
2871 if (err)
2872 goto done;
2873 if (ie)
2874 got_fileindex_entry_mark_deleted_from_disk(ie);
2875 break;
2876 case GOT_STATUS_ADD: {
2877 struct got_object_id *id;
2878 FILE *blob1_f;
2880 * Delete the added file only if its content already
2881 * exists in the repository.
2883 err = got_object_blob_file_create(&id, &blob1_f, path1);
2884 if (err)
2885 goto done;
2886 if (got_object_id_cmp(id, id1) == 0) {
2887 err = (*a->progress_cb)(a->progress_arg,
2888 GOT_STATUS_DELETE, path1);
2889 if (err)
2890 goto done;
2891 err = remove_ondisk_file(a->worktree->root_path,
2892 path1);
2893 if (err)
2894 goto done;
2895 if (ie)
2896 got_fileindex_entry_remove(a->fileindex,
2897 ie);
2898 } else {
2899 err = (*a->progress_cb)(a->progress_arg,
2900 GOT_STATUS_CANNOT_DELETE, path1);
2902 if (fclose(blob1_f) == EOF && err == NULL)
2903 err = got_error_from_errno("fclose");
2904 free(id);
2905 if (err)
2906 goto done;
2907 break;
2909 case GOT_STATUS_MODIFY:
2910 case GOT_STATUS_CONFLICT:
2911 err = (*a->progress_cb)(a->progress_arg,
2912 GOT_STATUS_CANNOT_DELETE, path1);
2913 if (err)
2914 goto done;
2915 break;
2916 case GOT_STATUS_OBSTRUCTED:
2917 err = (*a->progress_cb)(a->progress_arg, status, path1);
2918 if (err)
2919 goto done;
2920 break;
2921 default:
2922 break;
2924 } else if (blob2) {
2925 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2926 path2) == -1)
2927 return got_error_from_errno("asprintf");
2928 ie = got_fileindex_entry_get(a->fileindex, path2,
2929 strlen(path2));
2930 if (ie) {
2931 err = get_file_status(&status, &sb, ie, ondisk_path,
2932 -1, NULL, repo);
2933 if (err)
2934 goto done;
2935 if (status != GOT_STATUS_NO_CHANGE &&
2936 status != GOT_STATUS_MODIFY &&
2937 status != GOT_STATUS_CONFLICT &&
2938 status != GOT_STATUS_ADD) {
2939 err = (*a->progress_cb)(a->progress_arg,
2940 status, path2);
2941 goto done;
2943 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2944 char *link_target2;
2945 err = got_object_blob_read_to_str(&link_target2,
2946 blob2);
2947 if (err)
2948 goto done;
2949 err = merge_symlink(a->worktree, NULL,
2950 ondisk_path, path2, a->label_orig,
2951 link_target2, a->commit_id2, repo,
2952 a->progress_cb, a->progress_arg);
2953 free(link_target2);
2954 } else if (S_ISREG(sb.st_mode)) {
2955 err = merge_blob(&local_changes_subsumed,
2956 a->worktree, NULL, ondisk_path, path2,
2957 sb.st_mode, a->label_orig, blob2,
2958 a->commit_id2, repo, a->progress_cb,
2959 a->progress_arg);
2960 } else {
2961 err = got_error_path(ondisk_path,
2962 GOT_ERR_FILE_OBSTRUCTED);
2964 if (err)
2965 goto done;
2966 if (status == GOT_STATUS_DELETE) {
2967 err = got_fileindex_entry_update(ie,
2968 a->worktree->root_fd, path2, blob2->id.sha1,
2969 a->worktree->base_commit_id->sha1, 0);
2970 if (err)
2971 goto done;
2973 } else {
2974 int is_bad_symlink = 0;
2975 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2976 if (S_ISLNK(mode2)) {
2977 err = install_symlink(&is_bad_symlink,
2978 a->worktree, ondisk_path, path2, blob2, 0,
2979 0, 1, repo, a->progress_cb, a->progress_arg);
2980 } else {
2981 err = install_blob(a->worktree, ondisk_path, path2,
2982 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2983 a->progress_cb, a->progress_arg);
2985 if (err)
2986 goto done;
2987 err = got_fileindex_entry_alloc(&ie, path2);
2988 if (err)
2989 goto done;
2990 err = got_fileindex_entry_update(ie,
2991 a->worktree->root_fd, path2, NULL, NULL, 1);
2992 if (err) {
2993 got_fileindex_entry_free(ie);
2994 goto done;
2996 err = got_fileindex_entry_add(a->fileindex, ie);
2997 if (err) {
2998 got_fileindex_entry_free(ie);
2999 goto done;
3001 if (is_bad_symlink) {
3002 got_fileindex_entry_filetype_set(ie,
3003 GOT_FILEIDX_MODE_BAD_SYMLINK);
3007 done:
3008 free(ondisk_path);
3009 return err;
3012 struct check_merge_ok_arg {
3013 struct got_worktree *worktree;
3014 struct got_repository *repo;
3017 static const struct got_error *
3018 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3020 const struct got_error *err = NULL;
3021 struct check_merge_ok_arg *a = arg;
3022 unsigned char status;
3023 struct stat sb;
3024 char *ondisk_path;
3026 /* Reject merges into a work tree with mixed base commits. */
3027 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3028 SHA1_DIGEST_LENGTH))
3029 return got_error(GOT_ERR_MIXED_COMMITS);
3031 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3032 == -1)
3033 return got_error_from_errno("asprintf");
3035 /* Reject merges into a work tree with conflicted files. */
3036 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3037 if (err)
3038 return err;
3039 if (status == GOT_STATUS_CONFLICT)
3040 return got_error(GOT_ERR_CONFLICTS);
3042 return NULL;
3045 static const struct got_error *
3046 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3047 const char *fileindex_path, struct got_object_id *commit_id1,
3048 struct got_object_id *commit_id2, struct got_repository *repo,
3049 got_worktree_checkout_cb progress_cb, void *progress_arg,
3050 got_cancel_cb cancel_cb, void *cancel_arg)
3052 const struct got_error *err = NULL, *sync_err;
3053 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3054 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3055 struct merge_file_cb_arg arg;
3056 char *label_orig = NULL;
3058 if (commit_id1) {
3059 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3060 worktree->path_prefix);
3061 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3062 goto done;
3064 if (tree_id1) {
3065 char *id_str;
3067 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3068 if (err)
3069 goto done;
3071 err = got_object_id_str(&id_str, commit_id1);
3072 if (err)
3073 goto done;
3075 if (asprintf(&label_orig, "%s: commit %s",
3076 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3077 err = got_error_from_errno("asprintf");
3078 free(id_str);
3079 goto done;
3081 free(id_str);
3084 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3085 worktree->path_prefix);
3086 if (err)
3087 goto done;
3089 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3090 if (err)
3091 goto done;
3093 arg.worktree = worktree;
3094 arg.fileindex = fileindex;
3095 arg.progress_cb = progress_cb;
3096 arg.progress_arg = progress_arg;
3097 arg.cancel_cb = cancel_cb;
3098 arg.cancel_arg = cancel_arg;
3099 arg.label_orig = label_orig;
3100 arg.commit_id2 = commit_id2;
3101 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3102 sync_err = sync_fileindex(fileindex, fileindex_path);
3103 if (sync_err && err == NULL)
3104 err = sync_err;
3105 done:
3106 if (tree1)
3107 got_object_tree_close(tree1);
3108 if (tree2)
3109 got_object_tree_close(tree2);
3110 free(label_orig);
3111 return err;
3114 const struct got_error *
3115 got_worktree_merge_files(struct got_worktree *worktree,
3116 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3117 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3118 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3120 const struct got_error *err, *unlockerr;
3121 char *fileindex_path = NULL;
3122 struct got_fileindex *fileindex = NULL;
3123 struct check_merge_ok_arg mok_arg;
3125 err = lock_worktree(worktree, LOCK_EX);
3126 if (err)
3127 return err;
3129 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3130 if (err)
3131 goto done;
3133 mok_arg.worktree = worktree;
3134 mok_arg.repo = repo;
3135 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3136 &mok_arg);
3137 if (err)
3138 goto done;
3140 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3141 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3142 done:
3143 if (fileindex)
3144 got_fileindex_free(fileindex);
3145 free(fileindex_path);
3146 unlockerr = lock_worktree(worktree, LOCK_SH);
3147 if (unlockerr && err == NULL)
3148 err = unlockerr;
3149 return err;
3152 struct diff_dir_cb_arg {
3153 struct got_fileindex *fileindex;
3154 struct got_worktree *worktree;
3155 const char *status_path;
3156 size_t status_path_len;
3157 struct got_repository *repo;
3158 got_worktree_status_cb status_cb;
3159 void *status_arg;
3160 got_cancel_cb cancel_cb;
3161 void *cancel_arg;
3162 /* A pathlist containing per-directory pathlists of ignore patterns. */
3163 struct got_pathlist_head ignores;
3164 int report_unchanged;
3165 int no_ignores;
3168 static const struct got_error *
3169 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3170 int dirfd, const char *de_name,
3171 got_worktree_status_cb status_cb, void *status_arg,
3172 struct got_repository *repo, int report_unchanged)
3174 const struct got_error *err = NULL;
3175 unsigned char status = GOT_STATUS_NO_CHANGE;
3176 unsigned char staged_status = get_staged_status(ie);
3177 struct stat sb;
3178 struct got_object_id blob_id, commit_id, staged_blob_id;
3179 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3180 struct got_object_id *staged_blob_idp = NULL;
3182 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3183 if (err)
3184 return err;
3186 if (status == GOT_STATUS_NO_CHANGE &&
3187 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3188 return NULL;
3190 if (got_fileindex_entry_has_blob(ie)) {
3191 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3192 blob_idp = &blob_id;
3194 if (got_fileindex_entry_has_commit(ie)) {
3195 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3196 commit_idp = &commit_id;
3198 if (staged_status == GOT_STATUS_ADD ||
3199 staged_status == GOT_STATUS_MODIFY) {
3200 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3201 SHA1_DIGEST_LENGTH);
3202 staged_blob_idp = &staged_blob_id;
3205 return (*status_cb)(status_arg, status, staged_status,
3206 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3209 static const struct got_error *
3210 status_old_new(void *arg, struct got_fileindex_entry *ie,
3211 struct dirent *de, const char *parent_path, int dirfd)
3213 const struct got_error *err = NULL;
3214 struct diff_dir_cb_arg *a = arg;
3215 char *abspath;
3217 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3218 return got_error(GOT_ERR_CANCELLED);
3220 if (got_path_cmp(parent_path, a->status_path,
3221 strlen(parent_path), a->status_path_len) != 0 &&
3222 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3223 return NULL;
3225 if (parent_path[0]) {
3226 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3227 parent_path, de->d_name) == -1)
3228 return got_error_from_errno("asprintf");
3229 } else {
3230 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3231 de->d_name) == -1)
3232 return got_error_from_errno("asprintf");
3235 err = report_file_status(ie, abspath, dirfd, de->d_name,
3236 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3237 free(abspath);
3238 return err;
3241 static const struct got_error *
3242 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3244 struct diff_dir_cb_arg *a = arg;
3245 struct got_object_id blob_id, commit_id;
3246 unsigned char status;
3248 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3249 return got_error(GOT_ERR_CANCELLED);
3251 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3252 return NULL;
3254 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3255 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3256 if (got_fileindex_entry_has_file_on_disk(ie))
3257 status = GOT_STATUS_MISSING;
3258 else
3259 status = GOT_STATUS_DELETE;
3260 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3261 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3264 void
3265 free_ignorelist(struct got_pathlist_head *ignorelist)
3267 struct got_pathlist_entry *pe;
3269 TAILQ_FOREACH(pe, ignorelist, entry)
3270 free((char *)pe->path);
3271 got_pathlist_free(ignorelist);
3274 void
3275 free_ignores(struct got_pathlist_head *ignores)
3277 struct got_pathlist_entry *pe;
3279 TAILQ_FOREACH(pe, ignores, entry) {
3280 struct got_pathlist_head *ignorelist = pe->data;
3281 free_ignorelist(ignorelist);
3282 free((char *)pe->path);
3284 got_pathlist_free(ignores);
3287 static const struct got_error *
3288 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3290 const struct got_error *err = NULL;
3291 struct got_pathlist_entry *pe = NULL;
3292 struct got_pathlist_head *ignorelist;
3293 char *line = NULL, *pattern, *dirpath = NULL;
3294 size_t linesize = 0;
3295 ssize_t linelen;
3297 ignorelist = calloc(1, sizeof(*ignorelist));
3298 if (ignorelist == NULL)
3299 return got_error_from_errno("calloc");
3300 TAILQ_INIT(ignorelist);
3302 while ((linelen = getline(&line, &linesize, f)) != -1) {
3303 if (linelen > 0 && line[linelen - 1] == '\n')
3304 line[linelen - 1] = '\0';
3306 /* Git's ignores may contain comments. */
3307 if (line[0] == '#')
3308 continue;
3310 /* Git's negated patterns are not (yet?) supported. */
3311 if (line[0] == '!')
3312 continue;
3314 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3315 line) == -1) {
3316 err = got_error_from_errno("asprintf");
3317 goto done;
3319 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3320 if (err)
3321 goto done;
3323 if (ferror(f)) {
3324 err = got_error_from_errno("getline");
3325 goto done;
3328 dirpath = strdup(path);
3329 if (dirpath == NULL) {
3330 err = got_error_from_errno("strdup");
3331 goto done;
3333 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3334 done:
3335 free(line);
3336 if (err || pe == NULL) {
3337 free(dirpath);
3338 free_ignorelist(ignorelist);
3340 return err;
3343 int
3344 match_ignores(struct got_pathlist_head *ignores, const char *path)
3346 struct got_pathlist_entry *pe;
3348 /* Handle patterns which match in all directories. */
3349 TAILQ_FOREACH(pe, ignores, entry) {
3350 struct got_pathlist_head *ignorelist = pe->data;
3351 struct got_pathlist_entry *pi;
3353 TAILQ_FOREACH(pi, ignorelist, entry) {
3354 const char *p, *pattern = pi->path;
3356 if (strncmp(pattern, "**/", 3) != 0)
3357 continue;
3358 pattern += 3;
3359 p = path;
3360 while (*p) {
3361 if (fnmatch(pattern, p,
3362 FNM_PATHNAME | FNM_LEADING_DIR)) {
3363 /* Retry in next directory. */
3364 while (*p && *p != '/')
3365 p++;
3366 while (*p == '/')
3367 p++;
3368 continue;
3370 return 1;
3376 * The ignores pathlist contains ignore lists from children before
3377 * parents, so we can find the most specific ignorelist by walking
3378 * ignores backwards.
3380 pe = TAILQ_LAST(ignores, got_pathlist_head);
3381 while (pe) {
3382 if (got_path_is_child(path, pe->path, pe->path_len)) {
3383 struct got_pathlist_head *ignorelist = pe->data;
3384 struct got_pathlist_entry *pi;
3385 TAILQ_FOREACH(pi, ignorelist, entry) {
3386 const char *pattern = pi->path;
3387 int flags = FNM_LEADING_DIR;
3388 if (strstr(pattern, "/**/") == NULL)
3389 flags |= FNM_PATHNAME;
3390 if (fnmatch(pattern, path, flags))
3391 continue;
3392 return 1;
3395 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3398 return 0;
3401 static const struct got_error *
3402 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3403 const char *path, int dirfd, const char *ignores_filename)
3405 const struct got_error *err = NULL;
3406 char *ignorespath;
3407 int fd = -1;
3408 FILE *ignoresfile = NULL;
3410 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3411 path[0] ? "/" : "", ignores_filename) == -1)
3412 return got_error_from_errno("asprintf");
3414 if (dirfd != -1) {
3415 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3416 if (fd == -1) {
3417 if (errno != ENOENT && errno != EACCES)
3418 err = got_error_from_errno2("openat",
3419 ignorespath);
3420 } else {
3421 ignoresfile = fdopen(fd, "r");
3422 if (ignoresfile == NULL)
3423 err = got_error_from_errno2("fdopen",
3424 ignorespath);
3425 else {
3426 fd = -1;
3427 err = read_ignores(ignores, path, ignoresfile);
3430 } else {
3431 ignoresfile = fopen(ignorespath, "r");
3432 if (ignoresfile == NULL) {
3433 if (errno != ENOENT && errno != EACCES)
3434 err = got_error_from_errno2("fopen",
3435 ignorespath);
3436 } else
3437 err = read_ignores(ignores, path, ignoresfile);
3440 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3441 err = got_error_from_errno2("fclose", path);
3442 if (fd != -1 && close(fd) == -1 && err == NULL)
3443 err = got_error_from_errno2("close", path);
3444 free(ignorespath);
3445 return err;
3448 static const struct got_error *
3449 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3451 const struct got_error *err = NULL;
3452 struct diff_dir_cb_arg *a = arg;
3453 char *path = NULL;
3455 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3456 return got_error(GOT_ERR_CANCELLED);
3458 if (parent_path[0]) {
3459 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3460 return got_error_from_errno("asprintf");
3461 } else {
3462 path = de->d_name;
3465 if (de->d_type != DT_DIR &&
3466 got_path_is_child(path, a->status_path, a->status_path_len)
3467 && !match_ignores(&a->ignores, path))
3468 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3469 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3470 if (parent_path[0])
3471 free(path);
3472 return err;
3475 static const struct got_error *
3476 status_traverse(void *arg, const char *path, int dirfd)
3478 const struct got_error *err = NULL;
3479 struct diff_dir_cb_arg *a = arg;
3481 if (a->no_ignores)
3482 return NULL;
3484 err = add_ignores(&a->ignores, a->worktree->root_path,
3485 path, dirfd, ".cvsignore");
3486 if (err)
3487 return err;
3489 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3490 dirfd, ".gitignore");
3492 return err;
3495 static const struct got_error *
3496 report_single_file_status(const char *path, const char *ondisk_path,
3497 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3498 void *status_arg, struct got_repository *repo, int report_unchanged)
3500 struct got_fileindex_entry *ie;
3501 struct stat sb;
3503 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3504 if (ie)
3505 return report_file_status(ie, ondisk_path, -1, NULL,
3506 status_cb, status_arg, repo, report_unchanged);
3508 if (lstat(ondisk_path, &sb) == -1) {
3509 if (errno != ENOENT)
3510 return got_error_from_errno2("lstat", ondisk_path);
3511 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3512 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3513 return NULL;
3516 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3517 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3518 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3520 return NULL;
3523 static const struct got_error *
3524 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3525 const char *root_path, const char *path)
3527 const struct got_error *err;
3528 char *parent_path, *next_parent_path = NULL;
3530 err = add_ignores(ignores, root_path, "", -1,
3531 ".cvsignore");
3532 if (err)
3533 return err;
3535 err = add_ignores(ignores, root_path, "", -1,
3536 ".gitignore");
3537 if (err)
3538 return err;
3540 err = got_path_dirname(&parent_path, path);
3541 if (err) {
3542 if (err->code == GOT_ERR_BAD_PATH)
3543 return NULL; /* cannot traverse parent */
3544 return err;
3546 for (;;) {
3547 err = add_ignores(ignores, root_path, parent_path, -1,
3548 ".cvsignore");
3549 if (err)
3550 break;
3551 err = add_ignores(ignores, root_path, parent_path, -1,
3552 ".gitignore");
3553 if (err)
3554 break;
3555 err = got_path_dirname(&next_parent_path, parent_path);
3556 if (err) {
3557 if (err->code == GOT_ERR_BAD_PATH)
3558 err = NULL; /* traversed everything */
3559 break;
3561 free(parent_path);
3562 parent_path = next_parent_path;
3563 next_parent_path = NULL;
3566 free(parent_path);
3567 free(next_parent_path);
3568 return err;
3571 static const struct got_error *
3572 worktree_status(struct got_worktree *worktree, const char *path,
3573 struct got_fileindex *fileindex, struct got_repository *repo,
3574 got_worktree_status_cb status_cb, void *status_arg,
3575 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3576 int report_unchanged)
3578 const struct got_error *err = NULL;
3579 int fd = -1;
3580 struct got_fileindex_diff_dir_cb fdiff_cb;
3581 struct diff_dir_cb_arg arg;
3582 char *ondisk_path = NULL;
3584 TAILQ_INIT(&arg.ignores);
3586 if (asprintf(&ondisk_path, "%s%s%s",
3587 worktree->root_path, path[0] ? "/" : "", path) == -1)
3588 return got_error_from_errno("asprintf");
3590 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3591 if (fd == -1) {
3592 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3593 errno != ELOOP)
3594 err = got_error_from_errno2("open", ondisk_path);
3595 else
3596 err = report_single_file_status(path, ondisk_path,
3597 fileindex, status_cb, status_arg, repo,
3598 report_unchanged);
3599 } else {
3600 fdiff_cb.diff_old_new = status_old_new;
3601 fdiff_cb.diff_old = status_old;
3602 fdiff_cb.diff_new = status_new;
3603 fdiff_cb.diff_traverse = status_traverse;
3604 arg.fileindex = fileindex;
3605 arg.worktree = worktree;
3606 arg.status_path = path;
3607 arg.status_path_len = strlen(path);
3608 arg.repo = repo;
3609 arg.status_cb = status_cb;
3610 arg.status_arg = status_arg;
3611 arg.cancel_cb = cancel_cb;
3612 arg.cancel_arg = cancel_arg;
3613 arg.report_unchanged = report_unchanged;
3614 arg.no_ignores = no_ignores;
3615 if (!no_ignores) {
3616 err = add_ignores_from_parent_paths(&arg.ignores,
3617 worktree->root_path, path);
3618 if (err)
3619 goto done;
3621 err = got_fileindex_diff_dir(fileindex, fd,
3622 worktree->root_path, path, repo, &fdiff_cb, &arg);
3624 done:
3625 free_ignores(&arg.ignores);
3626 if (fd != -1 && close(fd) != 0 && err == NULL)
3627 err = got_error_from_errno("close");
3628 free(ondisk_path);
3629 return err;
3632 const struct got_error *
3633 got_worktree_status(struct got_worktree *worktree,
3634 struct got_pathlist_head *paths, struct got_repository *repo,
3635 got_worktree_status_cb status_cb, void *status_arg,
3636 got_cancel_cb cancel_cb, void *cancel_arg)
3638 const struct got_error *err = NULL;
3639 char *fileindex_path = NULL;
3640 struct got_fileindex *fileindex = NULL;
3641 struct got_pathlist_entry *pe;
3643 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3644 if (err)
3645 return err;
3647 TAILQ_FOREACH(pe, paths, entry) {
3648 err = worktree_status(worktree, pe->path, fileindex, repo,
3649 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3650 if (err)
3651 break;
3653 free(fileindex_path);
3654 got_fileindex_free(fileindex);
3655 return err;
3658 const struct got_error *
3659 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3660 const char *arg)
3662 const struct got_error *err = NULL;
3663 char *resolved = NULL, *cwd = NULL, *path = NULL;
3664 size_t len;
3665 struct stat sb;
3666 char *abspath = NULL;
3667 char canonpath[PATH_MAX];
3669 *wt_path = NULL;
3671 cwd = getcwd(NULL, 0);
3672 if (cwd == NULL)
3673 return got_error_from_errno("getcwd");
3675 if (lstat(arg, &sb) == -1) {
3676 if (errno != ENOENT) {
3677 err = got_error_from_errno2("lstat", arg);
3678 goto done;
3680 sb.st_mode = 0;
3682 if (S_ISLNK(sb.st_mode)) {
3684 * We cannot use realpath(3) with symlinks since we want to
3685 * operate on the symlink itself.
3686 * But we can make the path absolute, assuming it is relative
3687 * to the current working directory, and then canonicalize it.
3689 if (!got_path_is_absolute(arg)) {
3690 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3691 err = got_error_from_errno("asprintf");
3692 goto done;
3696 err = got_canonpath(abspath ? abspath : arg, canonpath,
3697 sizeof(canonpath));
3698 if (err)
3699 goto done;
3700 resolved = strdup(canonpath);
3701 if (resolved == NULL) {
3702 err = got_error_from_errno("strdup");
3703 goto done;
3705 } else {
3706 resolved = realpath(arg, NULL);
3707 if (resolved == NULL) {
3708 if (errno != ENOENT) {
3709 err = got_error_from_errno2("realpath", arg);
3710 goto done;
3712 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3713 err = got_error_from_errno("asprintf");
3714 goto done;
3716 err = got_canonpath(abspath, canonpath,
3717 sizeof(canonpath));
3718 if (err)
3719 goto done;
3720 resolved = strdup(canonpath);
3721 if (resolved == NULL) {
3722 err = got_error_from_errno("strdup");
3723 goto done;
3728 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3729 strlen(got_worktree_get_root_path(worktree)))) {
3730 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3731 goto done;
3734 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3735 err = got_path_skip_common_ancestor(&path,
3736 got_worktree_get_root_path(worktree), resolved);
3737 if (err)
3738 goto done;
3739 } else {
3740 path = strdup("");
3741 if (path == NULL) {
3742 err = got_error_from_errno("strdup");
3743 goto done;
3747 /* XXX status walk can't deal with trailing slash! */
3748 len = strlen(path);
3749 while (len > 0 && path[len - 1] == '/') {
3750 path[len - 1] = '\0';
3751 len--;
3753 done:
3754 free(abspath);
3755 free(resolved);
3756 free(cwd);
3757 if (err == NULL)
3758 *wt_path = path;
3759 else
3760 free(path);
3761 return err;
3764 struct schedule_addition_args {
3765 struct got_worktree *worktree;
3766 struct got_fileindex *fileindex;
3767 got_worktree_checkout_cb progress_cb;
3768 void *progress_arg;
3769 struct got_repository *repo;
3772 static const struct got_error *
3773 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3774 const char *relpath, struct got_object_id *blob_id,
3775 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3776 int dirfd, const char *de_name)
3778 struct schedule_addition_args *a = arg;
3779 const struct got_error *err = NULL;
3780 struct got_fileindex_entry *ie;
3781 struct stat sb;
3782 char *ondisk_path;
3784 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3785 relpath) == -1)
3786 return got_error_from_errno("asprintf");
3788 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3789 if (ie) {
3790 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3791 de_name, a->repo);
3792 if (err)
3793 goto done;
3794 /* Re-adding an existing entry is a no-op. */
3795 if (status == GOT_STATUS_ADD)
3796 goto done;
3797 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3798 if (err)
3799 goto done;
3802 if (status != GOT_STATUS_UNVERSIONED) {
3803 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3804 goto done;
3807 err = got_fileindex_entry_alloc(&ie, relpath);
3808 if (err)
3809 goto done;
3810 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3811 relpath, NULL, NULL, 1);
3812 if (err) {
3813 got_fileindex_entry_free(ie);
3814 goto done;
3816 err = got_fileindex_entry_add(a->fileindex, ie);
3817 if (err) {
3818 got_fileindex_entry_free(ie);
3819 goto done;
3821 done:
3822 free(ondisk_path);
3823 if (err)
3824 return err;
3825 if (status == GOT_STATUS_ADD)
3826 return NULL;
3827 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3830 const struct got_error *
3831 got_worktree_schedule_add(struct got_worktree *worktree,
3832 struct got_pathlist_head *paths,
3833 got_worktree_checkout_cb progress_cb, void *progress_arg,
3834 struct got_repository *repo, int no_ignores)
3836 struct got_fileindex *fileindex = NULL;
3837 char *fileindex_path = NULL;
3838 const struct got_error *err = NULL, *sync_err, *unlockerr;
3839 struct got_pathlist_entry *pe;
3840 struct schedule_addition_args saa;
3842 err = lock_worktree(worktree, LOCK_EX);
3843 if (err)
3844 return err;
3846 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3847 if (err)
3848 goto done;
3850 saa.worktree = worktree;
3851 saa.fileindex = fileindex;
3852 saa.progress_cb = progress_cb;
3853 saa.progress_arg = progress_arg;
3854 saa.repo = repo;
3856 TAILQ_FOREACH(pe, paths, entry) {
3857 err = worktree_status(worktree, pe->path, fileindex, repo,
3858 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3859 if (err)
3860 break;
3862 sync_err = sync_fileindex(fileindex, fileindex_path);
3863 if (sync_err && err == NULL)
3864 err = sync_err;
3865 done:
3866 free(fileindex_path);
3867 if (fileindex)
3868 got_fileindex_free(fileindex);
3869 unlockerr = lock_worktree(worktree, LOCK_SH);
3870 if (unlockerr && err == NULL)
3871 err = unlockerr;
3872 return err;
3875 struct schedule_deletion_args {
3876 struct got_worktree *worktree;
3877 struct got_fileindex *fileindex;
3878 got_worktree_delete_cb progress_cb;
3879 void *progress_arg;
3880 struct got_repository *repo;
3881 int delete_local_mods;
3882 int keep_on_disk;
3883 const char *status_codes;
3886 static const struct got_error *
3887 schedule_for_deletion(void *arg, unsigned char status,
3888 unsigned char staged_status, const char *relpath,
3889 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3890 struct got_object_id *commit_id, int dirfd, const char *de_name)
3892 struct schedule_deletion_args *a = arg;
3893 const struct got_error *err = NULL;
3894 struct got_fileindex_entry *ie = NULL;
3895 struct stat sb;
3896 char *ondisk_path;
3898 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3899 if (ie == NULL)
3900 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3902 staged_status = get_staged_status(ie);
3903 if (staged_status != GOT_STATUS_NO_CHANGE) {
3904 if (staged_status == GOT_STATUS_DELETE)
3905 return NULL;
3906 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3909 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3910 relpath) == -1)
3911 return got_error_from_errno("asprintf");
3913 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3914 a->repo);
3915 if (err)
3916 goto done;
3918 if (a->status_codes) {
3919 size_t ncodes = strlen(a->status_codes);
3920 int i;
3921 for (i = 0; i < ncodes ; i++) {
3922 if (status == a->status_codes[i])
3923 break;
3925 if (i == ncodes) {
3926 /* Do not delete files in non-matching status. */
3927 free(ondisk_path);
3928 return NULL;
3930 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3931 a->status_codes[i] != GOT_STATUS_MISSING) {
3932 static char msg[64];
3933 snprintf(msg, sizeof(msg),
3934 "invalid status code '%c'", a->status_codes[i]);
3935 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3936 goto done;
3940 if (status != GOT_STATUS_NO_CHANGE) {
3941 if (status == GOT_STATUS_DELETE)
3942 goto done;
3943 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3944 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3945 goto done;
3947 if (status != GOT_STATUS_MODIFY &&
3948 status != GOT_STATUS_MISSING) {
3949 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3950 goto done;
3954 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3955 size_t root_len;
3957 if (dirfd != -1) {
3958 if (unlinkat(dirfd, de_name, 0) != 0) {
3959 err = got_error_from_errno2("unlinkat",
3960 ondisk_path);
3961 goto done;
3963 } else if (unlink(ondisk_path) != 0) {
3964 err = got_error_from_errno2("unlink", ondisk_path);
3965 goto done;
3968 root_len = strlen(a->worktree->root_path);
3969 do {
3970 char *parent;
3971 err = got_path_dirname(&parent, ondisk_path);
3972 if (err)
3973 goto done;
3974 free(ondisk_path);
3975 ondisk_path = parent;
3976 if (rmdir(ondisk_path) == -1) {
3977 if (errno != ENOTEMPTY)
3978 err = got_error_from_errno2("rmdir",
3979 ondisk_path);
3980 break;
3982 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3983 strlen(ondisk_path), root_len) != 0);
3986 got_fileindex_entry_mark_deleted_from_disk(ie);
3987 done:
3988 free(ondisk_path);
3989 if (err)
3990 return err;
3991 if (status == GOT_STATUS_DELETE)
3992 return NULL;
3993 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3994 staged_status, relpath);
3997 const struct got_error *
3998 got_worktree_schedule_delete(struct got_worktree *worktree,
3999 struct got_pathlist_head *paths, int delete_local_mods,
4000 const char *status_codes,
4001 got_worktree_delete_cb progress_cb, void *progress_arg,
4002 struct got_repository *repo, int keep_on_disk)
4004 struct got_fileindex *fileindex = NULL;
4005 char *fileindex_path = NULL;
4006 const struct got_error *err = NULL, *sync_err, *unlockerr;
4007 struct got_pathlist_entry *pe;
4008 struct schedule_deletion_args sda;
4010 err = lock_worktree(worktree, LOCK_EX);
4011 if (err)
4012 return err;
4014 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4015 if (err)
4016 goto done;
4018 sda.worktree = worktree;
4019 sda.fileindex = fileindex;
4020 sda.progress_cb = progress_cb;
4021 sda.progress_arg = progress_arg;
4022 sda.repo = repo;
4023 sda.delete_local_mods = delete_local_mods;
4024 sda.keep_on_disk = keep_on_disk;
4025 sda.status_codes = status_codes;
4027 TAILQ_FOREACH(pe, paths, entry) {
4028 err = worktree_status(worktree, pe->path, fileindex, repo,
4029 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4030 if (err)
4031 break;
4033 sync_err = sync_fileindex(fileindex, fileindex_path);
4034 if (sync_err && err == NULL)
4035 err = sync_err;
4036 done:
4037 free(fileindex_path);
4038 if (fileindex)
4039 got_fileindex_free(fileindex);
4040 unlockerr = lock_worktree(worktree, LOCK_SH);
4041 if (unlockerr && err == NULL)
4042 err = unlockerr;
4043 return err;
4046 static const struct got_error *
4047 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4049 const struct got_error *err = NULL;
4050 char *line = NULL;
4051 size_t linesize = 0, n;
4052 ssize_t linelen;
4054 linelen = getline(&line, &linesize, infile);
4055 if (linelen == -1) {
4056 if (ferror(infile)) {
4057 err = got_error_from_errno("getline");
4058 goto done;
4060 return NULL;
4062 if (outfile) {
4063 n = fwrite(line, 1, linelen, outfile);
4064 if (n != linelen) {
4065 err = got_ferror(outfile, GOT_ERR_IO);
4066 goto done;
4069 if (rejectfile) {
4070 n = fwrite(line, 1, linelen, rejectfile);
4071 if (n != linelen)
4072 err = got_ferror(outfile, GOT_ERR_IO);
4074 done:
4075 free(line);
4076 return err;
4079 static const struct got_error *
4080 skip_one_line(FILE *f)
4082 char *line = NULL;
4083 size_t linesize = 0;
4084 ssize_t linelen;
4086 linelen = getline(&line, &linesize, f);
4087 if (linelen == -1) {
4088 if (ferror(f))
4089 return got_error_from_errno("getline");
4090 return NULL;
4092 free(line);
4093 return NULL;
4096 static const struct got_error *
4097 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4098 int start_old, int end_old, int start_new, int end_new,
4099 FILE *outfile, FILE *rejectfile)
4101 const struct got_error *err;
4103 /* Copy old file's lines leading up to patch. */
4104 while (!feof(f1) && *line_cur1 < start_old) {
4105 err = copy_one_line(f1, outfile, NULL);
4106 if (err)
4107 return err;
4108 (*line_cur1)++;
4110 /* Skip new file's lines leading up to patch. */
4111 while (!feof(f2) && *line_cur2 < start_new) {
4112 if (rejectfile)
4113 err = copy_one_line(f2, NULL, rejectfile);
4114 else
4115 err = skip_one_line(f2);
4116 if (err)
4117 return err;
4118 (*line_cur2)++;
4120 /* Copy patched lines. */
4121 while (!feof(f2) && *line_cur2 <= end_new) {
4122 err = copy_one_line(f2, outfile, NULL);
4123 if (err)
4124 return err;
4125 (*line_cur2)++;
4127 /* Skip over old file's replaced lines. */
4128 while (!feof(f1) && *line_cur1 <= end_old) {
4129 if (rejectfile)
4130 err = copy_one_line(f1, NULL, rejectfile);
4131 else
4132 err = skip_one_line(f1);
4133 if (err)
4134 return err;
4135 (*line_cur1)++;
4138 return NULL;
4141 static const struct got_error *
4142 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4143 FILE *outfile, FILE *rejectfile)
4145 const struct got_error *err;
4147 if (outfile) {
4148 /* Copy old file's lines until EOF. */
4149 while (!feof(f1)) {
4150 err = copy_one_line(f1, outfile, NULL);
4151 if (err)
4152 return err;
4153 (*line_cur1)++;
4156 if (rejectfile) {
4157 /* Copy new file's lines until EOF. */
4158 while (!feof(f2)) {
4159 err = copy_one_line(f2, NULL, rejectfile);
4160 if (err)
4161 return err;
4162 (*line_cur2)++;
4166 return NULL;
4169 static const struct got_error *
4170 apply_or_reject_change(int *choice, int *nchunks_used,
4171 struct diff_result *diff_result, int n,
4172 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4173 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4174 got_worktree_patch_cb patch_cb, void *patch_arg)
4176 const struct got_error *err = NULL;
4177 struct diff_chunk_context cc = {};
4178 int start_old, end_old, start_new, end_new;
4179 FILE *hunkfile;
4180 struct diff_output_unidiff_state *diff_state;
4181 struct diff_input_info diff_info;
4182 int rc;
4184 *choice = GOT_PATCH_CHOICE_NONE;
4186 /* Get changed line numbers without context lines for copy_change(). */
4187 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4188 start_old = cc.left.start;
4189 end_old = cc.left.end;
4190 start_new = cc.right.start;
4191 end_new = cc.right.end;
4193 /* Get the same change with context lines for display. */
4194 memset(&cc, 0, sizeof(cc));
4195 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4197 memset(&diff_info, 0, sizeof(diff_info));
4198 diff_info.left_path = relpath;
4199 diff_info.right_path = relpath;
4201 diff_state = diff_output_unidiff_state_alloc();
4202 if (diff_state == NULL)
4203 return got_error_set_errno(ENOMEM,
4204 "diff_output_unidiff_state_alloc");
4206 hunkfile = got_opentemp();
4207 if (hunkfile == NULL) {
4208 err = got_error_from_errno("got_opentemp");
4209 goto done;
4212 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4213 diff_result, &cc);
4214 if (rc != DIFF_RC_OK) {
4215 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4216 goto done;
4219 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4220 err = got_ferror(hunkfile, GOT_ERR_IO);
4221 goto done;
4224 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4225 hunkfile, changeno, nchanges);
4226 if (err)
4227 goto done;
4229 switch (*choice) {
4230 case GOT_PATCH_CHOICE_YES:
4231 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4232 end_old, start_new, end_new, outfile, rejectfile);
4233 break;
4234 case GOT_PATCH_CHOICE_NO:
4235 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4236 end_old, start_new, end_new, rejectfile, outfile);
4237 break;
4238 case GOT_PATCH_CHOICE_QUIT:
4239 break;
4240 default:
4241 err = got_error(GOT_ERR_PATCH_CHOICE);
4242 break;
4244 done:
4245 diff_output_unidiff_state_free(diff_state);
4246 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4247 err = got_error_from_errno("fclose");
4248 return err;
4251 struct revert_file_args {
4252 struct got_worktree *worktree;
4253 struct got_fileindex *fileindex;
4254 got_worktree_checkout_cb progress_cb;
4255 void *progress_arg;
4256 got_worktree_patch_cb patch_cb;
4257 void *patch_arg;
4258 struct got_repository *repo;
4261 static const struct got_error *
4262 create_patched_content(char **path_outfile, int reverse_patch,
4263 struct got_object_id *blob_id, const char *path2,
4264 int dirfd2, const char *de_name2,
4265 const char *relpath, struct got_repository *repo,
4266 got_worktree_patch_cb patch_cb, void *patch_arg)
4268 const struct got_error *err, *free_err;
4269 struct got_blob_object *blob = NULL;
4270 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4271 int fd2 = -1;
4272 char link_target[PATH_MAX];
4273 ssize_t link_len = 0;
4274 char *path1 = NULL, *id_str = NULL;
4275 struct stat sb2;
4276 struct got_diffreg_result *diffreg_result = NULL;
4277 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4278 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4280 *path_outfile = NULL;
4282 err = got_object_id_str(&id_str, blob_id);
4283 if (err)
4284 return err;
4286 if (dirfd2 != -1) {
4287 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4288 if (fd2 == -1) {
4289 if (errno != ELOOP) {
4290 err = got_error_from_errno2("openat", path2);
4291 goto done;
4293 link_len = readlinkat(dirfd2, de_name2,
4294 link_target, sizeof(link_target));
4295 if (link_len == -1)
4296 return got_error_from_errno2("readlinkat", path2);
4297 sb2.st_mode = S_IFLNK;
4298 sb2.st_size = link_len;
4300 } else {
4301 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4302 if (fd2 == -1) {
4303 if (errno != ELOOP) {
4304 err = got_error_from_errno2("open", path2);
4305 goto done;
4307 link_len = readlink(path2, link_target,
4308 sizeof(link_target));
4309 if (link_len == -1)
4310 return got_error_from_errno2("readlink", path2);
4311 sb2.st_mode = S_IFLNK;
4312 sb2.st_size = link_len;
4315 if (fd2 != -1) {
4316 if (fstat(fd2, &sb2) == -1) {
4317 err = got_error_from_errno2("fstat", path2);
4318 goto done;
4321 f2 = fdopen(fd2, "r");
4322 if (f2 == NULL) {
4323 err = got_error_from_errno2("fdopen", path2);
4324 goto done;
4326 fd2 = -1;
4327 } else {
4328 size_t n;
4329 f2 = got_opentemp();
4330 if (f2 == NULL) {
4331 err = got_error_from_errno2("got_opentemp", path2);
4332 goto done;
4334 n = fwrite(link_target, 1, link_len, f2);
4335 if (n != link_len) {
4336 err = got_ferror(f2, GOT_ERR_IO);
4337 goto done;
4339 if (fflush(f2) == EOF) {
4340 err = got_error_from_errno("fflush");
4341 goto done;
4343 rewind(f2);
4346 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4347 if (err)
4348 goto done;
4350 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4351 if (err)
4352 goto done;
4354 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4355 if (err)
4356 goto done;
4358 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4359 NULL);
4360 if (err)
4361 goto done;
4363 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4364 if (err)
4365 goto done;
4367 if (fseek(f1, 0L, SEEK_SET) == -1)
4368 return got_ferror(f1, GOT_ERR_IO);
4369 if (fseek(f2, 0L, SEEK_SET) == -1)
4370 return got_ferror(f2, GOT_ERR_IO);
4372 /* Count the number of actual changes in the diff result. */
4373 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4374 struct diff_chunk_context cc = {};
4375 diff_chunk_context_load_change(&cc, &nchunks_used,
4376 diffreg_result->result, n, 0);
4377 nchanges++;
4379 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4380 int choice;
4381 err = apply_or_reject_change(&choice, &nchunks_used,
4382 diffreg_result->result, n, relpath, f1, f2,
4383 &line_cur1, &line_cur2,
4384 reverse_patch ? NULL : outfile,
4385 reverse_patch ? outfile : NULL,
4386 ++i, nchanges, patch_cb, patch_arg);
4387 if (err)
4388 goto done;
4389 if (choice == GOT_PATCH_CHOICE_YES)
4390 have_content = 1;
4391 else if (choice == GOT_PATCH_CHOICE_QUIT)
4392 break;
4394 if (have_content) {
4395 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4396 reverse_patch ? NULL : outfile,
4397 reverse_patch ? outfile : NULL);
4398 if (err)
4399 goto done;
4401 if (!S_ISLNK(sb2.st_mode)) {
4402 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4403 err = got_error_from_errno2("fchmod", path2);
4404 goto done;
4408 done:
4409 free(id_str);
4410 if (blob)
4411 got_object_blob_close(blob);
4412 free_err = got_diffreg_result_free(diffreg_result);
4413 if (err == NULL)
4414 err = free_err;
4415 if (f1 && fclose(f1) == EOF && err == NULL)
4416 err = got_error_from_errno2("fclose", path1);
4417 if (f2 && fclose(f2) == EOF && err == NULL)
4418 err = got_error_from_errno2("fclose", path2);
4419 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4420 err = got_error_from_errno2("close", path2);
4421 if (outfile && fclose(outfile) == EOF && err == NULL)
4422 err = got_error_from_errno2("fclose", *path_outfile);
4423 if (path1 && unlink(path1) == -1 && err == NULL)
4424 err = got_error_from_errno2("unlink", path1);
4425 if (err || !have_content) {
4426 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4427 err = got_error_from_errno2("unlink", *path_outfile);
4428 free(*path_outfile);
4429 *path_outfile = NULL;
4431 free(path1);
4432 return err;
4435 static const struct got_error *
4436 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4437 const char *relpath, struct got_object_id *blob_id,
4438 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4439 int dirfd, const char *de_name)
4441 struct revert_file_args *a = arg;
4442 const struct got_error *err = NULL;
4443 char *parent_path = NULL;
4444 struct got_fileindex_entry *ie;
4445 struct got_tree_object *tree = NULL;
4446 struct got_object_id *tree_id = NULL;
4447 const struct got_tree_entry *te = NULL;
4448 char *tree_path = NULL, *te_name;
4449 char *ondisk_path = NULL, *path_content = NULL;
4450 struct got_blob_object *blob = NULL;
4452 /* Reverting a staged deletion is a no-op. */
4453 if (status == GOT_STATUS_DELETE &&
4454 staged_status != GOT_STATUS_NO_CHANGE)
4455 return NULL;
4457 if (status == GOT_STATUS_UNVERSIONED)
4458 return (*a->progress_cb)(a->progress_arg,
4459 GOT_STATUS_UNVERSIONED, relpath);
4461 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4462 if (ie == NULL)
4463 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4465 /* Construct in-repository path of tree which contains this blob. */
4466 err = got_path_dirname(&parent_path, ie->path);
4467 if (err) {
4468 if (err->code != GOT_ERR_BAD_PATH)
4469 goto done;
4470 parent_path = strdup("/");
4471 if (parent_path == NULL) {
4472 err = got_error_from_errno("strdup");
4473 goto done;
4476 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4477 tree_path = strdup(parent_path);
4478 if (tree_path == NULL) {
4479 err = got_error_from_errno("strdup");
4480 goto done;
4482 } else {
4483 if (got_path_is_root_dir(parent_path)) {
4484 tree_path = strdup(a->worktree->path_prefix);
4485 if (tree_path == NULL) {
4486 err = got_error_from_errno("strdup");
4487 goto done;
4489 } else {
4490 if (asprintf(&tree_path, "%s/%s",
4491 a->worktree->path_prefix, parent_path) == -1) {
4492 err = got_error_from_errno("asprintf");
4493 goto done;
4498 err = got_object_id_by_path(&tree_id, a->repo,
4499 a->worktree->base_commit_id, tree_path);
4500 if (err) {
4501 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4502 (status == GOT_STATUS_ADD ||
4503 staged_status == GOT_STATUS_ADD)))
4504 goto done;
4505 } else {
4506 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4507 if (err)
4508 goto done;
4510 err = got_path_basename(&te_name, ie->path);
4511 if (err)
4512 goto done;
4514 te = got_object_tree_find_entry(tree, te_name);
4515 free(te_name);
4516 if (te == NULL && status != GOT_STATUS_ADD &&
4517 staged_status != GOT_STATUS_ADD) {
4518 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4519 goto done;
4523 switch (status) {
4524 case GOT_STATUS_ADD:
4525 if (a->patch_cb) {
4526 int choice = GOT_PATCH_CHOICE_NONE;
4527 err = (*a->patch_cb)(&choice, a->patch_arg,
4528 status, ie->path, NULL, 1, 1);
4529 if (err)
4530 goto done;
4531 if (choice != GOT_PATCH_CHOICE_YES)
4532 break;
4534 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4535 ie->path);
4536 if (err)
4537 goto done;
4538 got_fileindex_entry_remove(a->fileindex, ie);
4539 break;
4540 case GOT_STATUS_DELETE:
4541 if (a->patch_cb) {
4542 int choice = GOT_PATCH_CHOICE_NONE;
4543 err = (*a->patch_cb)(&choice, a->patch_arg,
4544 status, ie->path, NULL, 1, 1);
4545 if (err)
4546 goto done;
4547 if (choice != GOT_PATCH_CHOICE_YES)
4548 break;
4550 /* fall through */
4551 case GOT_STATUS_MODIFY:
4552 case GOT_STATUS_MODE_CHANGE:
4553 case GOT_STATUS_CONFLICT:
4554 case GOT_STATUS_MISSING: {
4555 struct got_object_id id;
4556 if (staged_status == GOT_STATUS_ADD ||
4557 staged_status == GOT_STATUS_MODIFY) {
4558 memcpy(id.sha1, ie->staged_blob_sha1,
4559 SHA1_DIGEST_LENGTH);
4560 } else
4561 memcpy(id.sha1, ie->blob_sha1,
4562 SHA1_DIGEST_LENGTH);
4563 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4564 if (err)
4565 goto done;
4567 if (asprintf(&ondisk_path, "%s/%s",
4568 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4569 err = got_error_from_errno("asprintf");
4570 goto done;
4573 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4574 status == GOT_STATUS_CONFLICT)) {
4575 int is_bad_symlink = 0;
4576 err = create_patched_content(&path_content, 1, &id,
4577 ondisk_path, dirfd, de_name, ie->path, a->repo,
4578 a->patch_cb, a->patch_arg);
4579 if (err || path_content == NULL)
4580 break;
4581 if (te && S_ISLNK(te->mode)) {
4582 if (unlink(path_content) == -1) {
4583 err = got_error_from_errno2("unlink",
4584 path_content);
4585 break;
4587 err = install_symlink(&is_bad_symlink,
4588 a->worktree, ondisk_path, ie->path,
4589 blob, 0, 1, 0, a->repo,
4590 a->progress_cb, a->progress_arg);
4591 } else {
4592 if (rename(path_content, ondisk_path) == -1) {
4593 err = got_error_from_errno3("rename",
4594 path_content, ondisk_path);
4595 goto done;
4598 } else {
4599 int is_bad_symlink = 0;
4600 if (te && S_ISLNK(te->mode)) {
4601 err = install_symlink(&is_bad_symlink,
4602 a->worktree, ondisk_path, ie->path,
4603 blob, 0, 1, 0, a->repo,
4604 a->progress_cb, a->progress_arg);
4605 } else {
4606 err = install_blob(a->worktree, ondisk_path,
4607 ie->path,
4608 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4609 got_fileindex_perms_to_st(ie), blob,
4610 0, 1, 0, 0, a->repo,
4611 a->progress_cb, a->progress_arg);
4613 if (err)
4614 goto done;
4615 if (status == GOT_STATUS_DELETE ||
4616 status == GOT_STATUS_MODE_CHANGE) {
4617 err = got_fileindex_entry_update(ie,
4618 a->worktree->root_fd, relpath,
4619 blob->id.sha1,
4620 a->worktree->base_commit_id->sha1, 1);
4621 if (err)
4622 goto done;
4624 if (is_bad_symlink) {
4625 got_fileindex_entry_filetype_set(ie,
4626 GOT_FILEIDX_MODE_BAD_SYMLINK);
4629 break;
4631 default:
4632 break;
4634 done:
4635 free(ondisk_path);
4636 free(path_content);
4637 free(parent_path);
4638 free(tree_path);
4639 if (blob)
4640 got_object_blob_close(blob);
4641 if (tree)
4642 got_object_tree_close(tree);
4643 free(tree_id);
4644 return err;
4647 const struct got_error *
4648 got_worktree_revert(struct got_worktree *worktree,
4649 struct got_pathlist_head *paths,
4650 got_worktree_checkout_cb progress_cb, void *progress_arg,
4651 got_worktree_patch_cb patch_cb, void *patch_arg,
4652 struct got_repository *repo)
4654 struct got_fileindex *fileindex = NULL;
4655 char *fileindex_path = NULL;
4656 const struct got_error *err = NULL, *unlockerr = NULL;
4657 const struct got_error *sync_err = NULL;
4658 struct got_pathlist_entry *pe;
4659 struct revert_file_args rfa;
4661 err = lock_worktree(worktree, LOCK_EX);
4662 if (err)
4663 return err;
4665 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4666 if (err)
4667 goto done;
4669 rfa.worktree = worktree;
4670 rfa.fileindex = fileindex;
4671 rfa.progress_cb = progress_cb;
4672 rfa.progress_arg = progress_arg;
4673 rfa.patch_cb = patch_cb;
4674 rfa.patch_arg = patch_arg;
4675 rfa.repo = repo;
4676 TAILQ_FOREACH(pe, paths, entry) {
4677 err = worktree_status(worktree, pe->path, fileindex, repo,
4678 revert_file, &rfa, NULL, NULL, 0, 0);
4679 if (err)
4680 break;
4682 sync_err = sync_fileindex(fileindex, fileindex_path);
4683 if (sync_err && err == NULL)
4684 err = sync_err;
4685 done:
4686 free(fileindex_path);
4687 if (fileindex)
4688 got_fileindex_free(fileindex);
4689 unlockerr = lock_worktree(worktree, LOCK_SH);
4690 if (unlockerr && err == NULL)
4691 err = unlockerr;
4692 return err;
4695 static void
4696 free_commitable(struct got_commitable *ct)
4698 free(ct->path);
4699 free(ct->in_repo_path);
4700 free(ct->ondisk_path);
4701 free(ct->blob_id);
4702 free(ct->base_blob_id);
4703 free(ct->staged_blob_id);
4704 free(ct->base_commit_id);
4705 free(ct);
4708 struct collect_commitables_arg {
4709 struct got_pathlist_head *commitable_paths;
4710 struct got_repository *repo;
4711 struct got_worktree *worktree;
4712 struct got_fileindex *fileindex;
4713 int have_staged_files;
4714 int allow_bad_symlinks;
4717 static const struct got_error *
4718 collect_commitables(void *arg, unsigned char status,
4719 unsigned char staged_status, const char *relpath,
4720 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4721 struct got_object_id *commit_id, int dirfd, const char *de_name)
4723 struct collect_commitables_arg *a = arg;
4724 const struct got_error *err = NULL;
4725 struct got_commitable *ct = NULL;
4726 struct got_pathlist_entry *new = NULL;
4727 char *parent_path = NULL, *path = NULL;
4728 struct stat sb;
4730 if (a->have_staged_files) {
4731 if (staged_status != GOT_STATUS_MODIFY &&
4732 staged_status != GOT_STATUS_ADD &&
4733 staged_status != GOT_STATUS_DELETE)
4734 return NULL;
4735 } else {
4736 if (status == GOT_STATUS_CONFLICT)
4737 return got_error(GOT_ERR_COMMIT_CONFLICT);
4739 if (status != GOT_STATUS_MODIFY &&
4740 status != GOT_STATUS_MODE_CHANGE &&
4741 status != GOT_STATUS_ADD &&
4742 status != GOT_STATUS_DELETE)
4743 return NULL;
4746 if (asprintf(&path, "/%s", relpath) == -1) {
4747 err = got_error_from_errno("asprintf");
4748 goto done;
4750 if (strcmp(path, "/") == 0) {
4751 parent_path = strdup("");
4752 if (parent_path == NULL)
4753 return got_error_from_errno("strdup");
4754 } else {
4755 err = got_path_dirname(&parent_path, path);
4756 if (err)
4757 return err;
4760 ct = calloc(1, sizeof(*ct));
4761 if (ct == NULL) {
4762 err = got_error_from_errno("calloc");
4763 goto done;
4766 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4767 relpath) == -1) {
4768 err = got_error_from_errno("asprintf");
4769 goto done;
4772 if (staged_status == GOT_STATUS_ADD ||
4773 staged_status == GOT_STATUS_MODIFY) {
4774 struct got_fileindex_entry *ie;
4775 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4776 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4777 case GOT_FILEIDX_MODE_REGULAR_FILE:
4778 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4779 ct->mode = S_IFREG;
4780 break;
4781 case GOT_FILEIDX_MODE_SYMLINK:
4782 ct->mode = S_IFLNK;
4783 break;
4784 default:
4785 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4786 goto done;
4788 ct->mode |= got_fileindex_entry_perms_get(ie);
4789 } else if (status != GOT_STATUS_DELETE &&
4790 staged_status != GOT_STATUS_DELETE) {
4791 if (dirfd != -1) {
4792 if (fstatat(dirfd, de_name, &sb,
4793 AT_SYMLINK_NOFOLLOW) == -1) {
4794 err = got_error_from_errno2("fstatat",
4795 ct->ondisk_path);
4796 goto done;
4798 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4799 err = got_error_from_errno2("lstat", ct->ondisk_path);
4800 goto done;
4802 ct->mode = sb.st_mode;
4805 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4806 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4807 relpath) == -1) {
4808 err = got_error_from_errno("asprintf");
4809 goto done;
4812 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4813 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4814 int is_bad_symlink;
4815 char target_path[PATH_MAX];
4816 ssize_t target_len;
4817 target_len = readlink(ct->ondisk_path, target_path,
4818 sizeof(target_path));
4819 if (target_len == -1) {
4820 err = got_error_from_errno2("readlink",
4821 ct->ondisk_path);
4822 goto done;
4824 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4825 target_len, ct->ondisk_path, a->worktree->root_path);
4826 if (err)
4827 goto done;
4828 if (is_bad_symlink) {
4829 err = got_error_path(ct->ondisk_path,
4830 GOT_ERR_BAD_SYMLINK);
4831 goto done;
4836 ct->status = status;
4837 ct->staged_status = staged_status;
4838 ct->blob_id = NULL; /* will be filled in when blob gets created */
4839 if (ct->status != GOT_STATUS_ADD &&
4840 ct->staged_status != GOT_STATUS_ADD) {
4841 ct->base_blob_id = got_object_id_dup(blob_id);
4842 if (ct->base_blob_id == NULL) {
4843 err = got_error_from_errno("got_object_id_dup");
4844 goto done;
4846 ct->base_commit_id = got_object_id_dup(commit_id);
4847 if (ct->base_commit_id == NULL) {
4848 err = got_error_from_errno("got_object_id_dup");
4849 goto done;
4852 if (ct->staged_status == GOT_STATUS_ADD ||
4853 ct->staged_status == GOT_STATUS_MODIFY) {
4854 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4855 if (ct->staged_blob_id == NULL) {
4856 err = got_error_from_errno("got_object_id_dup");
4857 goto done;
4860 ct->path = strdup(path);
4861 if (ct->path == NULL) {
4862 err = got_error_from_errno("strdup");
4863 goto done;
4865 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4866 done:
4867 if (ct && (err || new == NULL))
4868 free_commitable(ct);
4869 free(parent_path);
4870 free(path);
4871 return err;
4874 static const struct got_error *write_tree(struct got_object_id **, int *,
4875 struct got_tree_object *, const char *, struct got_pathlist_head *,
4876 got_worktree_status_cb status_cb, void *status_arg,
4877 struct got_repository *);
4879 static const struct got_error *
4880 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4881 struct got_tree_entry *te, const char *parent_path,
4882 struct got_pathlist_head *commitable_paths,
4883 got_worktree_status_cb status_cb, void *status_arg,
4884 struct got_repository *repo)
4886 const struct got_error *err = NULL;
4887 struct got_tree_object *subtree;
4888 char *subpath;
4890 if (asprintf(&subpath, "%s%s%s", parent_path,
4891 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4892 return got_error_from_errno("asprintf");
4894 err = got_object_open_as_tree(&subtree, repo, &te->id);
4895 if (err)
4896 return err;
4898 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4899 commitable_paths, status_cb, status_arg, repo);
4900 got_object_tree_close(subtree);
4901 free(subpath);
4902 return err;
4905 static const struct got_error *
4906 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4908 const struct got_error *err = NULL;
4909 char *ct_parent_path = NULL;
4911 *match = 0;
4913 if (strchr(ct->in_repo_path, '/') == NULL) {
4914 *match = got_path_is_root_dir(path);
4915 return NULL;
4918 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4919 if (err)
4920 return err;
4921 *match = (strcmp(path, ct_parent_path) == 0);
4922 free(ct_parent_path);
4923 return err;
4926 static mode_t
4927 get_ct_file_mode(struct got_commitable *ct)
4929 if (S_ISLNK(ct->mode))
4930 return S_IFLNK;
4932 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4935 static const struct got_error *
4936 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4937 struct got_tree_entry *te, struct got_commitable *ct)
4939 const struct got_error *err = NULL;
4941 *new_te = NULL;
4943 err = got_object_tree_entry_dup(new_te, te);
4944 if (err)
4945 goto done;
4947 (*new_te)->mode = get_ct_file_mode(ct);
4949 if (ct->staged_status == GOT_STATUS_MODIFY)
4950 memcpy(&(*new_te)->id, ct->staged_blob_id,
4951 sizeof((*new_te)->id));
4952 else
4953 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4954 done:
4955 if (err && *new_te) {
4956 free(*new_te);
4957 *new_te = NULL;
4959 return err;
4962 static const struct got_error *
4963 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4964 struct got_commitable *ct)
4966 const struct got_error *err = NULL;
4967 char *ct_name = NULL;
4969 *new_te = NULL;
4971 *new_te = calloc(1, sizeof(**new_te));
4972 if (*new_te == NULL)
4973 return got_error_from_errno("calloc");
4975 err = got_path_basename(&ct_name, ct->path);
4976 if (err)
4977 goto done;
4978 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4979 sizeof((*new_te)->name)) {
4980 err = got_error(GOT_ERR_NO_SPACE);
4981 goto done;
4984 (*new_te)->mode = get_ct_file_mode(ct);
4986 if (ct->staged_status == GOT_STATUS_ADD)
4987 memcpy(&(*new_te)->id, ct->staged_blob_id,
4988 sizeof((*new_te)->id));
4989 else
4990 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4991 done:
4992 free(ct_name);
4993 if (err && *new_te) {
4994 free(*new_te);
4995 *new_te = NULL;
4997 return err;
5000 static const struct got_error *
5001 insert_tree_entry(struct got_tree_entry *new_te,
5002 struct got_pathlist_head *paths)
5004 const struct got_error *err = NULL;
5005 struct got_pathlist_entry *new_pe;
5007 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5008 if (err)
5009 return err;
5010 if (new_pe == NULL)
5011 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5012 return NULL;
5015 static const struct got_error *
5016 report_ct_status(struct got_commitable *ct,
5017 got_worktree_status_cb status_cb, void *status_arg)
5019 const char *ct_path = ct->path;
5020 unsigned char status;
5022 while (ct_path[0] == '/')
5023 ct_path++;
5025 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5026 status = ct->staged_status;
5027 else
5028 status = ct->status;
5030 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5031 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5034 static const struct got_error *
5035 match_modified_subtree(int *modified, struct got_tree_entry *te,
5036 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5038 const struct got_error *err = NULL;
5039 struct got_pathlist_entry *pe;
5040 char *te_path;
5042 *modified = 0;
5044 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5045 got_path_is_root_dir(base_tree_path) ? "" : "/",
5046 te->name) == -1)
5047 return got_error_from_errno("asprintf");
5049 TAILQ_FOREACH(pe, commitable_paths, entry) {
5050 struct got_commitable *ct = pe->data;
5051 *modified = got_path_is_child(ct->in_repo_path, te_path,
5052 strlen(te_path));
5053 if (*modified)
5054 break;
5057 free(te_path);
5058 return err;
5061 static const struct got_error *
5062 match_deleted_or_modified_ct(struct got_commitable **ctp,
5063 struct got_tree_entry *te, const char *base_tree_path,
5064 struct got_pathlist_head *commitable_paths)
5066 const struct got_error *err = NULL;
5067 struct got_pathlist_entry *pe;
5069 *ctp = NULL;
5071 TAILQ_FOREACH(pe, commitable_paths, entry) {
5072 struct got_commitable *ct = pe->data;
5073 char *ct_name = NULL;
5074 int path_matches;
5076 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5077 if (ct->status != GOT_STATUS_MODIFY &&
5078 ct->status != GOT_STATUS_MODE_CHANGE &&
5079 ct->status != GOT_STATUS_DELETE)
5080 continue;
5081 } else {
5082 if (ct->staged_status != GOT_STATUS_MODIFY &&
5083 ct->staged_status != GOT_STATUS_DELETE)
5084 continue;
5087 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5088 continue;
5090 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5091 if (err)
5092 return err;
5093 if (!path_matches)
5094 continue;
5096 err = got_path_basename(&ct_name, pe->path);
5097 if (err)
5098 return err;
5100 if (strcmp(te->name, ct_name) != 0) {
5101 free(ct_name);
5102 continue;
5104 free(ct_name);
5106 *ctp = ct;
5107 break;
5110 return err;
5113 static const struct got_error *
5114 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5115 const char *child_path, const char *path_base_tree,
5116 struct got_pathlist_head *commitable_paths,
5117 got_worktree_status_cb status_cb, void *status_arg,
5118 struct got_repository *repo)
5120 const struct got_error *err = NULL;
5121 struct got_tree_entry *new_te;
5122 char *subtree_path;
5123 struct got_object_id *id = NULL;
5124 int nentries;
5126 *new_tep = NULL;
5128 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5129 got_path_is_root_dir(path_base_tree) ? "" : "/",
5130 child_path) == -1)
5131 return got_error_from_errno("asprintf");
5133 new_te = calloc(1, sizeof(*new_te));
5134 if (new_te == NULL)
5135 return got_error_from_errno("calloc");
5136 new_te->mode = S_IFDIR;
5138 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5139 sizeof(new_te->name)) {
5140 err = got_error(GOT_ERR_NO_SPACE);
5141 goto done;
5143 err = write_tree(&id, &nentries, NULL, subtree_path,
5144 commitable_paths, status_cb, status_arg, repo);
5145 if (err) {
5146 free(new_te);
5147 goto done;
5149 memcpy(&new_te->id, id, sizeof(new_te->id));
5150 done:
5151 free(id);
5152 free(subtree_path);
5153 if (err == NULL)
5154 *new_tep = new_te;
5155 return err;
5158 static const struct got_error *
5159 write_tree(struct got_object_id **new_tree_id, int *nentries,
5160 struct got_tree_object *base_tree, const char *path_base_tree,
5161 struct got_pathlist_head *commitable_paths,
5162 got_worktree_status_cb status_cb, void *status_arg,
5163 struct got_repository *repo)
5165 const struct got_error *err = NULL;
5166 struct got_pathlist_head paths;
5167 struct got_tree_entry *te, *new_te = NULL;
5168 struct got_pathlist_entry *pe;
5170 TAILQ_INIT(&paths);
5171 *nentries = 0;
5173 /* Insert, and recurse into, newly added entries first. */
5174 TAILQ_FOREACH(pe, commitable_paths, entry) {
5175 struct got_commitable *ct = pe->data;
5176 char *child_path = NULL, *slash;
5178 if ((ct->status != GOT_STATUS_ADD &&
5179 ct->staged_status != GOT_STATUS_ADD) ||
5180 (ct->flags & GOT_COMMITABLE_ADDED))
5181 continue;
5183 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5184 strlen(path_base_tree)))
5185 continue;
5187 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5188 ct->in_repo_path);
5189 if (err)
5190 goto done;
5192 slash = strchr(child_path, '/');
5193 if (slash == NULL) {
5194 err = alloc_added_blob_tree_entry(&new_te, ct);
5195 if (err)
5196 goto done;
5197 err = report_ct_status(ct, status_cb, status_arg);
5198 if (err)
5199 goto done;
5200 ct->flags |= GOT_COMMITABLE_ADDED;
5201 err = insert_tree_entry(new_te, &paths);
5202 if (err)
5203 goto done;
5204 (*nentries)++;
5205 } else {
5206 *slash = '\0'; /* trim trailing path components */
5207 if (base_tree == NULL ||
5208 got_object_tree_find_entry(base_tree, child_path)
5209 == NULL) {
5210 err = make_subtree_for_added_blob(&new_te,
5211 child_path, path_base_tree,
5212 commitable_paths, status_cb, status_arg,
5213 repo);
5214 if (err)
5215 goto done;
5216 err = insert_tree_entry(new_te, &paths);
5217 if (err)
5218 goto done;
5219 (*nentries)++;
5224 if (base_tree) {
5225 int i, nbase_entries;
5226 /* Handle modified and deleted entries. */
5227 nbase_entries = got_object_tree_get_nentries(base_tree);
5228 for (i = 0; i < nbase_entries; i++) {
5229 struct got_commitable *ct = NULL;
5231 te = got_object_tree_get_entry(base_tree, i);
5232 if (got_object_tree_entry_is_submodule(te)) {
5233 /* Entry is a submodule; just copy it. */
5234 err = got_object_tree_entry_dup(&new_te, te);
5235 if (err)
5236 goto done;
5237 err = insert_tree_entry(new_te, &paths);
5238 if (err)
5239 goto done;
5240 (*nentries)++;
5241 continue;
5244 if (S_ISDIR(te->mode)) {
5245 int modified;
5246 err = got_object_tree_entry_dup(&new_te, te);
5247 if (err)
5248 goto done;
5249 err = match_modified_subtree(&modified, te,
5250 path_base_tree, commitable_paths);
5251 if (err)
5252 goto done;
5253 /* Avoid recursion into unmodified subtrees. */
5254 if (modified) {
5255 struct got_object_id *new_id;
5256 int nsubentries;
5257 err = write_subtree(&new_id,
5258 &nsubentries, te,
5259 path_base_tree, commitable_paths,
5260 status_cb, status_arg, repo);
5261 if (err)
5262 goto done;
5263 if (nsubentries == 0) {
5264 /* All entries were deleted. */
5265 free(new_id);
5266 continue;
5268 memcpy(&new_te->id, new_id,
5269 sizeof(new_te->id));
5270 free(new_id);
5272 err = insert_tree_entry(new_te, &paths);
5273 if (err)
5274 goto done;
5275 (*nentries)++;
5276 continue;
5279 err = match_deleted_or_modified_ct(&ct, te,
5280 path_base_tree, commitable_paths);
5281 if (err)
5282 goto done;
5283 if (ct) {
5284 /* NB: Deleted entries get dropped here. */
5285 if (ct->status == GOT_STATUS_MODIFY ||
5286 ct->status == GOT_STATUS_MODE_CHANGE ||
5287 ct->staged_status == GOT_STATUS_MODIFY) {
5288 err = alloc_modified_blob_tree_entry(
5289 &new_te, te, ct);
5290 if (err)
5291 goto done;
5292 err = insert_tree_entry(new_te, &paths);
5293 if (err)
5294 goto done;
5295 (*nentries)++;
5297 err = report_ct_status(ct, status_cb,
5298 status_arg);
5299 if (err)
5300 goto done;
5301 } else {
5302 /* Entry is unchanged; just copy it. */
5303 err = got_object_tree_entry_dup(&new_te, te);
5304 if (err)
5305 goto done;
5306 err = insert_tree_entry(new_te, &paths);
5307 if (err)
5308 goto done;
5309 (*nentries)++;
5314 /* Write new list of entries; deleted entries have been dropped. */
5315 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5316 done:
5317 got_pathlist_free(&paths);
5318 return err;
5321 static const struct got_error *
5322 update_fileindex_after_commit(struct got_worktree *worktree,
5323 struct got_pathlist_head *commitable_paths,
5324 struct got_object_id *new_base_commit_id,
5325 struct got_fileindex *fileindex, int have_staged_files)
5327 const struct got_error *err = NULL;
5328 struct got_pathlist_entry *pe;
5329 char *relpath = NULL;
5331 TAILQ_FOREACH(pe, commitable_paths, entry) {
5332 struct got_fileindex_entry *ie;
5333 struct got_commitable *ct = pe->data;
5335 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5337 err = got_path_skip_common_ancestor(&relpath,
5338 worktree->root_path, ct->ondisk_path);
5339 if (err)
5340 goto done;
5342 if (ie) {
5343 if (ct->status == GOT_STATUS_DELETE ||
5344 ct->staged_status == GOT_STATUS_DELETE) {
5345 got_fileindex_entry_remove(fileindex, ie);
5346 } else if (ct->staged_status == GOT_STATUS_ADD ||
5347 ct->staged_status == GOT_STATUS_MODIFY) {
5348 got_fileindex_entry_stage_set(ie,
5349 GOT_FILEIDX_STAGE_NONE);
5350 got_fileindex_entry_staged_filetype_set(ie, 0);
5352 err = got_fileindex_entry_update(ie,
5353 worktree->root_fd, relpath,
5354 ct->staged_blob_id->sha1,
5355 new_base_commit_id->sha1,
5356 !have_staged_files);
5357 } else
5358 err = got_fileindex_entry_update(ie,
5359 worktree->root_fd, relpath,
5360 ct->blob_id->sha1,
5361 new_base_commit_id->sha1,
5362 !have_staged_files);
5363 } else {
5364 err = got_fileindex_entry_alloc(&ie, pe->path);
5365 if (err)
5366 goto done;
5367 err = got_fileindex_entry_update(ie,
5368 worktree->root_fd, relpath, ct->blob_id->sha1,
5369 new_base_commit_id->sha1, 1);
5370 if (err) {
5371 got_fileindex_entry_free(ie);
5372 goto done;
5374 err = got_fileindex_entry_add(fileindex, ie);
5375 if (err) {
5376 got_fileindex_entry_free(ie);
5377 goto done;
5380 free(relpath);
5381 relpath = NULL;
5383 done:
5384 free(relpath);
5385 return err;
5389 static const struct got_error *
5390 check_out_of_date(const char *in_repo_path, unsigned char status,
5391 unsigned char staged_status, struct got_object_id *base_blob_id,
5392 struct got_object_id *base_commit_id,
5393 struct got_object_id *head_commit_id, struct got_repository *repo,
5394 int ood_errcode)
5396 const struct got_error *err = NULL;
5397 struct got_object_id *id = NULL;
5399 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5400 /* Trivial case: base commit == head commit */
5401 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5402 return NULL;
5404 * Ensure file content which local changes were based
5405 * on matches file content in the branch head.
5407 err = got_object_id_by_path(&id, repo, head_commit_id,
5408 in_repo_path);
5409 if (err) {
5410 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5411 err = got_error(ood_errcode);
5412 goto done;
5413 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5414 err = got_error(ood_errcode);
5415 } else {
5416 /* Require that added files don't exist in the branch head. */
5417 err = got_object_id_by_path(&id, repo, head_commit_id,
5418 in_repo_path);
5419 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5420 goto done;
5421 err = id ? got_error(ood_errcode) : NULL;
5423 done:
5424 free(id);
5425 return err;
5428 const struct got_error *
5429 commit_worktree(struct got_object_id **new_commit_id,
5430 struct got_pathlist_head *commitable_paths,
5431 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5432 const char *author, const char *committer,
5433 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5434 got_worktree_status_cb status_cb, void *status_arg,
5435 struct got_repository *repo)
5437 const struct got_error *err = NULL, *unlockerr = NULL;
5438 struct got_pathlist_entry *pe;
5439 const char *head_ref_name = NULL;
5440 struct got_commit_object *head_commit = NULL;
5441 struct got_reference *head_ref2 = NULL;
5442 struct got_object_id *head_commit_id2 = NULL;
5443 struct got_tree_object *head_tree = NULL;
5444 struct got_object_id *new_tree_id = NULL;
5445 int nentries;
5446 struct got_object_id_queue parent_ids;
5447 struct got_object_qid *pid = NULL;
5448 char *logmsg = NULL;
5450 *new_commit_id = NULL;
5452 SIMPLEQ_INIT(&parent_ids);
5454 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5455 if (err)
5456 goto done;
5458 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5459 if (err)
5460 goto done;
5462 if (commit_msg_cb != NULL) {
5463 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5464 if (err)
5465 goto done;
5468 if (logmsg == NULL || strlen(logmsg) == 0) {
5469 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5470 goto done;
5473 /* Create blobs from added and modified files and record their IDs. */
5474 TAILQ_FOREACH(pe, commitable_paths, entry) {
5475 struct got_commitable *ct = pe->data;
5476 char *ondisk_path;
5478 /* Blobs for staged files already exist. */
5479 if (ct->staged_status == GOT_STATUS_ADD ||
5480 ct->staged_status == GOT_STATUS_MODIFY)
5481 continue;
5483 if (ct->status != GOT_STATUS_ADD &&
5484 ct->status != GOT_STATUS_MODIFY &&
5485 ct->status != GOT_STATUS_MODE_CHANGE)
5486 continue;
5488 if (asprintf(&ondisk_path, "%s/%s",
5489 worktree->root_path, pe->path) == -1) {
5490 err = got_error_from_errno("asprintf");
5491 goto done;
5493 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5494 free(ondisk_path);
5495 if (err)
5496 goto done;
5499 /* Recursively write new tree objects. */
5500 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5501 commitable_paths, status_cb, status_arg, repo);
5502 if (err)
5503 goto done;
5505 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5506 if (err)
5507 goto done;
5508 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5509 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5510 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5511 got_object_qid_free(pid);
5512 if (logmsg != NULL)
5513 free(logmsg);
5514 if (err)
5515 goto done;
5517 /* Check if a concurrent commit to our branch has occurred. */
5518 head_ref_name = got_worktree_get_head_ref_name(worktree);
5519 if (head_ref_name == NULL) {
5520 err = got_error_from_errno("got_worktree_get_head_ref_name");
5521 goto done;
5523 /* Lock the reference here to prevent concurrent modification. */
5524 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5525 if (err)
5526 goto done;
5527 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5528 if (err)
5529 goto done;
5530 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5531 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5532 goto done;
5534 /* Update branch head in repository. */
5535 err = got_ref_change_ref(head_ref2, *new_commit_id);
5536 if (err)
5537 goto done;
5538 err = got_ref_write(head_ref2, repo);
5539 if (err)
5540 goto done;
5542 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5543 if (err)
5544 goto done;
5546 err = ref_base_commit(worktree, repo);
5547 if (err)
5548 goto done;
5549 done:
5550 if (head_tree)
5551 got_object_tree_close(head_tree);
5552 if (head_commit)
5553 got_object_commit_close(head_commit);
5554 free(head_commit_id2);
5555 if (head_ref2) {
5556 unlockerr = got_ref_unlock(head_ref2);
5557 if (unlockerr && err == NULL)
5558 err = unlockerr;
5559 got_ref_close(head_ref2);
5561 return err;
5564 static const struct got_error *
5565 check_path_is_commitable(const char *path,
5566 struct got_pathlist_head *commitable_paths)
5568 struct got_pathlist_entry *cpe = NULL;
5569 size_t path_len = strlen(path);
5571 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5572 struct got_commitable *ct = cpe->data;
5573 const char *ct_path = ct->path;
5575 while (ct_path[0] == '/')
5576 ct_path++;
5578 if (strcmp(path, ct_path) == 0 ||
5579 got_path_is_child(ct_path, path, path_len))
5580 break;
5583 if (cpe == NULL)
5584 return got_error_path(path, GOT_ERR_BAD_PATH);
5586 return NULL;
5589 static const struct got_error *
5590 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5592 int *have_staged_files = arg;
5594 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5595 *have_staged_files = 1;
5596 return got_error(GOT_ERR_CANCELLED);
5599 return NULL;
5602 static const struct got_error *
5603 check_non_staged_files(struct got_fileindex *fileindex,
5604 struct got_pathlist_head *paths)
5606 struct got_pathlist_entry *pe;
5607 struct got_fileindex_entry *ie;
5609 TAILQ_FOREACH(pe, paths, entry) {
5610 if (pe->path[0] == '\0')
5611 continue;
5612 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5613 if (ie == NULL)
5614 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5615 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5616 return got_error_path(pe->path,
5617 GOT_ERR_FILE_NOT_STAGED);
5620 return NULL;
5623 const struct got_error *
5624 got_worktree_commit(struct got_object_id **new_commit_id,
5625 struct got_worktree *worktree, struct got_pathlist_head *paths,
5626 const char *author, const char *committer, int allow_bad_symlinks,
5627 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5628 got_worktree_status_cb status_cb, void *status_arg,
5629 struct got_repository *repo)
5631 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5632 struct got_fileindex *fileindex = NULL;
5633 char *fileindex_path = NULL;
5634 struct got_pathlist_head commitable_paths;
5635 struct collect_commitables_arg cc_arg;
5636 struct got_pathlist_entry *pe;
5637 struct got_reference *head_ref = NULL;
5638 struct got_object_id *head_commit_id = NULL;
5639 int have_staged_files = 0;
5641 *new_commit_id = NULL;
5643 TAILQ_INIT(&commitable_paths);
5645 err = lock_worktree(worktree, LOCK_EX);
5646 if (err)
5647 goto done;
5649 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5650 if (err)
5651 goto done;
5653 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5654 if (err)
5655 goto done;
5657 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5658 if (err)
5659 goto done;
5661 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5662 &have_staged_files);
5663 if (err && err->code != GOT_ERR_CANCELLED)
5664 goto done;
5665 if (have_staged_files) {
5666 err = check_non_staged_files(fileindex, paths);
5667 if (err)
5668 goto done;
5671 cc_arg.commitable_paths = &commitable_paths;
5672 cc_arg.worktree = worktree;
5673 cc_arg.fileindex = fileindex;
5674 cc_arg.repo = repo;
5675 cc_arg.have_staged_files = have_staged_files;
5676 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5677 TAILQ_FOREACH(pe, paths, entry) {
5678 err = worktree_status(worktree, pe->path, fileindex, repo,
5679 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5680 if (err)
5681 goto done;
5684 if (TAILQ_EMPTY(&commitable_paths)) {
5685 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5686 goto done;
5689 TAILQ_FOREACH(pe, paths, entry) {
5690 err = check_path_is_commitable(pe->path, &commitable_paths);
5691 if (err)
5692 goto done;
5695 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5696 struct got_commitable *ct = pe->data;
5697 const char *ct_path = ct->in_repo_path;
5699 while (ct_path[0] == '/')
5700 ct_path++;
5701 err = check_out_of_date(ct_path, ct->status,
5702 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5703 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5704 if (err)
5705 goto done;
5709 err = commit_worktree(new_commit_id, &commitable_paths,
5710 head_commit_id, worktree, author, committer,
5711 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5712 if (err)
5713 goto done;
5715 err = update_fileindex_after_commit(worktree, &commitable_paths,
5716 *new_commit_id, fileindex, have_staged_files);
5717 sync_err = sync_fileindex(fileindex, fileindex_path);
5718 if (sync_err && err == NULL)
5719 err = sync_err;
5720 done:
5721 if (fileindex)
5722 got_fileindex_free(fileindex);
5723 free(fileindex_path);
5724 unlockerr = lock_worktree(worktree, LOCK_SH);
5725 if (unlockerr && err == NULL)
5726 err = unlockerr;
5727 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5728 struct got_commitable *ct = pe->data;
5729 free_commitable(ct);
5731 got_pathlist_free(&commitable_paths);
5732 return err;
5735 const char *
5736 got_commitable_get_path(struct got_commitable *ct)
5738 return ct->path;
5741 unsigned int
5742 got_commitable_get_status(struct got_commitable *ct)
5744 return ct->status;
5747 struct check_rebase_ok_arg {
5748 struct got_worktree *worktree;
5749 struct got_repository *repo;
5752 static const struct got_error *
5753 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5755 const struct got_error *err = NULL;
5756 struct check_rebase_ok_arg *a = arg;
5757 unsigned char status;
5758 struct stat sb;
5759 char *ondisk_path;
5761 /* Reject rebase of a work tree with mixed base commits. */
5762 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5763 SHA1_DIGEST_LENGTH))
5764 return got_error(GOT_ERR_MIXED_COMMITS);
5766 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5767 == -1)
5768 return got_error_from_errno("asprintf");
5770 /* Reject rebase of a work tree with modified or staged files. */
5771 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5772 free(ondisk_path);
5773 if (err)
5774 return err;
5776 if (status != GOT_STATUS_NO_CHANGE)
5777 return got_error(GOT_ERR_MODIFIED);
5778 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5779 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5781 return NULL;
5784 const struct got_error *
5785 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5786 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5787 struct got_worktree *worktree, struct got_reference *branch,
5788 struct got_repository *repo)
5790 const struct got_error *err = NULL;
5791 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5792 char *branch_ref_name = NULL;
5793 char *fileindex_path = NULL;
5794 struct check_rebase_ok_arg ok_arg;
5795 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5796 struct got_object_id *wt_branch_tip = NULL;
5798 *new_base_branch_ref = NULL;
5799 *tmp_branch = NULL;
5800 *fileindex = NULL;
5802 err = lock_worktree(worktree, LOCK_EX);
5803 if (err)
5804 return err;
5806 err = open_fileindex(fileindex, &fileindex_path, worktree);
5807 if (err)
5808 goto done;
5810 ok_arg.worktree = worktree;
5811 ok_arg.repo = repo;
5812 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5813 &ok_arg);
5814 if (err)
5815 goto done;
5817 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5818 if (err)
5819 goto done;
5821 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5822 if (err)
5823 goto done;
5825 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5826 if (err)
5827 goto done;
5829 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5830 0);
5831 if (err)
5832 goto done;
5834 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5835 if (err)
5836 goto done;
5837 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5838 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5839 goto done;
5842 err = got_ref_alloc_symref(new_base_branch_ref,
5843 new_base_branch_ref_name, wt_branch);
5844 if (err)
5845 goto done;
5846 err = got_ref_write(*new_base_branch_ref, repo);
5847 if (err)
5848 goto done;
5850 /* TODO Lock original branch's ref while rebasing? */
5852 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5853 if (err)
5854 goto done;
5856 err = got_ref_write(branch_ref, repo);
5857 if (err)
5858 goto done;
5860 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5861 worktree->base_commit_id);
5862 if (err)
5863 goto done;
5864 err = got_ref_write(*tmp_branch, repo);
5865 if (err)
5866 goto done;
5868 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5869 if (err)
5870 goto done;
5871 done:
5872 free(fileindex_path);
5873 free(tmp_branch_name);
5874 free(new_base_branch_ref_name);
5875 free(branch_ref_name);
5876 if (branch_ref)
5877 got_ref_close(branch_ref);
5878 if (wt_branch)
5879 got_ref_close(wt_branch);
5880 free(wt_branch_tip);
5881 if (err) {
5882 if (*new_base_branch_ref) {
5883 got_ref_close(*new_base_branch_ref);
5884 *new_base_branch_ref = NULL;
5886 if (*tmp_branch) {
5887 got_ref_close(*tmp_branch);
5888 *tmp_branch = NULL;
5890 if (*fileindex) {
5891 got_fileindex_free(*fileindex);
5892 *fileindex = NULL;
5894 lock_worktree(worktree, LOCK_SH);
5896 return err;
5899 const struct got_error *
5900 got_worktree_rebase_continue(struct got_object_id **commit_id,
5901 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5902 struct got_reference **branch, struct got_fileindex **fileindex,
5903 struct got_worktree *worktree, struct got_repository *repo)
5905 const struct got_error *err;
5906 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5907 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5908 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5909 char *fileindex_path = NULL;
5910 int have_staged_files = 0;
5912 *commit_id = NULL;
5913 *new_base_branch = NULL;
5914 *tmp_branch = NULL;
5915 *branch = NULL;
5916 *fileindex = NULL;
5918 err = lock_worktree(worktree, LOCK_EX);
5919 if (err)
5920 return err;
5922 err = open_fileindex(fileindex, &fileindex_path, worktree);
5923 if (err)
5924 goto done;
5926 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5927 &have_staged_files);
5928 if (err && err->code != GOT_ERR_CANCELLED)
5929 goto done;
5930 if (have_staged_files) {
5931 err = got_error(GOT_ERR_STAGED_PATHS);
5932 goto done;
5935 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5936 if (err)
5937 goto done;
5939 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5940 if (err)
5941 goto done;
5943 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5944 if (err)
5945 goto done;
5947 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5948 if (err)
5949 goto done;
5951 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5952 if (err)
5953 goto done;
5955 err = got_ref_open(branch, repo,
5956 got_ref_get_symref_target(branch_ref), 0);
5957 if (err)
5958 goto done;
5960 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5961 if (err)
5962 goto done;
5964 err = got_ref_resolve(commit_id, repo, commit_ref);
5965 if (err)
5966 goto done;
5968 err = got_ref_open(new_base_branch, repo,
5969 new_base_branch_ref_name, 0);
5970 if (err)
5971 goto done;
5973 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5974 if (err)
5975 goto done;
5976 done:
5977 free(commit_ref_name);
5978 free(branch_ref_name);
5979 free(fileindex_path);
5980 if (commit_ref)
5981 got_ref_close(commit_ref);
5982 if (branch_ref)
5983 got_ref_close(branch_ref);
5984 if (err) {
5985 free(*commit_id);
5986 *commit_id = NULL;
5987 if (*tmp_branch) {
5988 got_ref_close(*tmp_branch);
5989 *tmp_branch = NULL;
5991 if (*new_base_branch) {
5992 got_ref_close(*new_base_branch);
5993 *new_base_branch = NULL;
5995 if (*branch) {
5996 got_ref_close(*branch);
5997 *branch = NULL;
5999 if (*fileindex) {
6000 got_fileindex_free(*fileindex);
6001 *fileindex = NULL;
6003 lock_worktree(worktree, LOCK_SH);
6005 return err;
6008 const struct got_error *
6009 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6011 const struct got_error *err;
6012 char *tmp_branch_name = NULL;
6014 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6015 if (err)
6016 return err;
6018 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6019 free(tmp_branch_name);
6020 return NULL;
6023 static const struct got_error *
6024 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6025 char **logmsg, void *arg)
6027 *logmsg = arg;
6028 return NULL;
6031 static const struct got_error *
6032 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6033 const char *path, struct got_object_id *blob_id,
6034 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6035 int dirfd, const char *de_name)
6037 return NULL;
6040 struct collect_merged_paths_arg {
6041 got_worktree_checkout_cb progress_cb;
6042 void *progress_arg;
6043 struct got_pathlist_head *merged_paths;
6046 static const struct got_error *
6047 collect_merged_paths(void *arg, unsigned char status, const char *path)
6049 const struct got_error *err;
6050 struct collect_merged_paths_arg *a = arg;
6051 char *p;
6052 struct got_pathlist_entry *new;
6054 err = (*a->progress_cb)(a->progress_arg, status, path);
6055 if (err)
6056 return err;
6058 if (status != GOT_STATUS_MERGE &&
6059 status != GOT_STATUS_ADD &&
6060 status != GOT_STATUS_DELETE &&
6061 status != GOT_STATUS_CONFLICT)
6062 return NULL;
6064 p = strdup(path);
6065 if (p == NULL)
6066 return got_error_from_errno("strdup");
6068 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6069 if (err || new == NULL)
6070 free(p);
6071 return err;
6074 void
6075 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6077 struct got_pathlist_entry *pe;
6079 TAILQ_FOREACH(pe, merged_paths, entry)
6080 free((char *)pe->path);
6082 got_pathlist_free(merged_paths);
6085 static const struct got_error *
6086 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6087 int is_rebase, struct got_repository *repo)
6089 const struct got_error *err;
6090 struct got_reference *commit_ref = NULL;
6092 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6093 if (err) {
6094 if (err->code != GOT_ERR_NOT_REF)
6095 goto done;
6096 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6097 if (err)
6098 goto done;
6099 err = got_ref_write(commit_ref, repo);
6100 if (err)
6101 goto done;
6102 } else if (is_rebase) {
6103 struct got_object_id *stored_id;
6104 int cmp;
6106 err = got_ref_resolve(&stored_id, repo, commit_ref);
6107 if (err)
6108 goto done;
6109 cmp = got_object_id_cmp(commit_id, stored_id);
6110 free(stored_id);
6111 if (cmp != 0) {
6112 err = got_error(GOT_ERR_REBASE_COMMITID);
6113 goto done;
6116 done:
6117 if (commit_ref)
6118 got_ref_close(commit_ref);
6119 return err;
6122 static const struct got_error *
6123 rebase_merge_files(struct got_pathlist_head *merged_paths,
6124 const char *commit_ref_name, struct got_worktree *worktree,
6125 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6126 struct got_object_id *commit_id, struct got_repository *repo,
6127 got_worktree_checkout_cb progress_cb, void *progress_arg,
6128 got_cancel_cb cancel_cb, void *cancel_arg)
6130 const struct got_error *err;
6131 struct got_reference *commit_ref = NULL;
6132 struct collect_merged_paths_arg cmp_arg;
6133 char *fileindex_path;
6135 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6137 err = get_fileindex_path(&fileindex_path, worktree);
6138 if (err)
6139 return err;
6141 cmp_arg.progress_cb = progress_cb;
6142 cmp_arg.progress_arg = progress_arg;
6143 cmp_arg.merged_paths = merged_paths;
6144 err = merge_files(worktree, fileindex, fileindex_path,
6145 parent_commit_id, commit_id, repo, collect_merged_paths,
6146 &cmp_arg, cancel_cb, cancel_arg);
6147 if (commit_ref)
6148 got_ref_close(commit_ref);
6149 return err;
6152 const struct got_error *
6153 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6154 struct got_worktree *worktree, struct got_fileindex *fileindex,
6155 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6156 struct got_repository *repo,
6157 got_worktree_checkout_cb progress_cb, void *progress_arg,
6158 got_cancel_cb cancel_cb, void *cancel_arg)
6160 const struct got_error *err;
6161 char *commit_ref_name;
6163 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6164 if (err)
6165 return err;
6167 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6168 if (err)
6169 goto done;
6171 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6172 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6173 progress_arg, cancel_cb, cancel_arg);
6174 done:
6175 free(commit_ref_name);
6176 return err;
6179 const struct got_error *
6180 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6181 struct got_worktree *worktree, struct got_fileindex *fileindex,
6182 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6183 struct got_repository *repo,
6184 got_worktree_checkout_cb progress_cb, void *progress_arg,
6185 got_cancel_cb cancel_cb, void *cancel_arg)
6187 const struct got_error *err;
6188 char *commit_ref_name;
6190 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6191 if (err)
6192 return err;
6194 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6195 if (err)
6196 goto done;
6198 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6199 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6200 progress_arg, cancel_cb, cancel_arg);
6201 done:
6202 free(commit_ref_name);
6203 return err;
6206 static const struct got_error *
6207 rebase_commit(struct got_object_id **new_commit_id,
6208 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6209 struct got_worktree *worktree, struct got_fileindex *fileindex,
6210 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6211 const char *new_logmsg, struct got_repository *repo)
6213 const struct got_error *err, *sync_err;
6214 struct got_pathlist_head commitable_paths;
6215 struct collect_commitables_arg cc_arg;
6216 char *fileindex_path = NULL;
6217 struct got_reference *head_ref = NULL;
6218 struct got_object_id *head_commit_id = NULL;
6219 char *logmsg = NULL;
6221 TAILQ_INIT(&commitable_paths);
6222 *new_commit_id = NULL;
6224 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6226 err = get_fileindex_path(&fileindex_path, worktree);
6227 if (err)
6228 return err;
6230 cc_arg.commitable_paths = &commitable_paths;
6231 cc_arg.worktree = worktree;
6232 cc_arg.repo = repo;
6233 cc_arg.have_staged_files = 0;
6235 * If possible get the status of individual files directly to
6236 * avoid crawling the entire work tree once per rebased commit.
6237 * TODO: Ideally, merged_paths would contain a list of commitables
6238 * we could use so we could skip worktree_status() entirely.
6240 if (merged_paths) {
6241 struct got_pathlist_entry *pe;
6242 TAILQ_FOREACH(pe, merged_paths, entry) {
6243 err = worktree_status(worktree, pe->path, fileindex,
6244 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6245 0);
6246 if (err)
6247 goto done;
6249 } else {
6250 err = worktree_status(worktree, "", fileindex, repo,
6251 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6252 if (err)
6253 goto done;
6256 if (TAILQ_EMPTY(&commitable_paths)) {
6257 /* No-op change; commit will be elided. */
6258 err = got_ref_delete(commit_ref, repo);
6259 if (err)
6260 goto done;
6261 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6262 goto done;
6265 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6266 if (err)
6267 goto done;
6269 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6270 if (err)
6271 goto done;
6273 if (new_logmsg) {
6274 logmsg = strdup(new_logmsg);
6275 if (logmsg == NULL) {
6276 err = got_error_from_errno("strdup");
6277 goto done;
6279 } else {
6280 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6281 if (err)
6282 goto done;
6285 /* NB: commit_worktree will call free(logmsg) */
6286 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6287 worktree, got_object_commit_get_author(orig_commit),
6288 got_object_commit_get_committer(orig_commit),
6289 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6290 if (err)
6291 goto done;
6293 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6294 if (err)
6295 goto done;
6297 err = got_ref_delete(commit_ref, repo);
6298 if (err)
6299 goto done;
6301 err = update_fileindex_after_commit(worktree, &commitable_paths,
6302 *new_commit_id, fileindex, 0);
6303 sync_err = sync_fileindex(fileindex, fileindex_path);
6304 if (sync_err && err == NULL)
6305 err = sync_err;
6306 done:
6307 free(fileindex_path);
6308 free(head_commit_id);
6309 if (head_ref)
6310 got_ref_close(head_ref);
6311 if (err) {
6312 free(*new_commit_id);
6313 *new_commit_id = NULL;
6315 return err;
6318 const struct got_error *
6319 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6320 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6321 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6322 struct got_commit_object *orig_commit,
6323 struct got_object_id *orig_commit_id, struct got_repository *repo)
6325 const struct got_error *err;
6326 char *commit_ref_name;
6327 struct got_reference *commit_ref = NULL;
6328 struct got_object_id *commit_id = NULL;
6330 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6331 if (err)
6332 return err;
6334 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6335 if (err)
6336 goto done;
6337 err = got_ref_resolve(&commit_id, repo, commit_ref);
6338 if (err)
6339 goto done;
6340 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6341 err = got_error(GOT_ERR_REBASE_COMMITID);
6342 goto done;
6345 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6346 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6347 done:
6348 if (commit_ref)
6349 got_ref_close(commit_ref);
6350 free(commit_ref_name);
6351 free(commit_id);
6352 return err;
6355 const struct got_error *
6356 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6357 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6358 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6359 struct got_commit_object *orig_commit,
6360 struct got_object_id *orig_commit_id, const char *new_logmsg,
6361 struct got_repository *repo)
6363 const struct got_error *err;
6364 char *commit_ref_name;
6365 struct got_reference *commit_ref = NULL;
6367 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6368 if (err)
6369 return err;
6371 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6372 if (err)
6373 goto done;
6375 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6376 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6377 done:
6378 if (commit_ref)
6379 got_ref_close(commit_ref);
6380 free(commit_ref_name);
6381 return err;
6384 const struct got_error *
6385 got_worktree_rebase_postpone(struct got_worktree *worktree,
6386 struct got_fileindex *fileindex)
6388 if (fileindex)
6389 got_fileindex_free(fileindex);
6390 return lock_worktree(worktree, LOCK_SH);
6393 static const struct got_error *
6394 delete_ref(const char *name, struct got_repository *repo)
6396 const struct got_error *err;
6397 struct got_reference *ref;
6399 err = got_ref_open(&ref, repo, name, 0);
6400 if (err) {
6401 if (err->code == GOT_ERR_NOT_REF)
6402 return NULL;
6403 return err;
6406 err = got_ref_delete(ref, repo);
6407 got_ref_close(ref);
6408 return err;
6411 static const struct got_error *
6412 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6414 const struct got_error *err;
6415 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6416 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6418 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6419 if (err)
6420 goto done;
6421 err = delete_ref(tmp_branch_name, repo);
6422 if (err)
6423 goto done;
6425 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6426 if (err)
6427 goto done;
6428 err = delete_ref(new_base_branch_ref_name, repo);
6429 if (err)
6430 goto done;
6432 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6433 if (err)
6434 goto done;
6435 err = delete_ref(branch_ref_name, repo);
6436 if (err)
6437 goto done;
6439 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6440 if (err)
6441 goto done;
6442 err = delete_ref(commit_ref_name, repo);
6443 if (err)
6444 goto done;
6446 done:
6447 free(tmp_branch_name);
6448 free(new_base_branch_ref_name);
6449 free(branch_ref_name);
6450 free(commit_ref_name);
6451 return err;
6454 const struct got_error *
6455 got_worktree_rebase_complete(struct got_worktree *worktree,
6456 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6457 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6458 struct got_repository *repo)
6460 const struct got_error *err, *unlockerr, *sync_err;
6461 struct got_object_id *new_head_commit_id = NULL;
6462 char *fileindex_path = NULL;
6464 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6465 if (err)
6466 return err;
6468 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6469 if (err)
6470 goto done;
6472 err = got_ref_write(rebased_branch, repo);
6473 if (err)
6474 goto done;
6476 err = got_worktree_set_head_ref(worktree, rebased_branch);
6477 if (err)
6478 goto done;
6480 err = delete_rebase_refs(worktree, repo);
6481 if (err)
6482 goto done;
6484 err = get_fileindex_path(&fileindex_path, worktree);
6485 if (err)
6486 goto done;
6487 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6488 sync_err = sync_fileindex(fileindex, fileindex_path);
6489 if (sync_err && err == NULL)
6490 err = sync_err;
6491 done:
6492 got_fileindex_free(fileindex);
6493 free(fileindex_path);
6494 free(new_head_commit_id);
6495 unlockerr = lock_worktree(worktree, LOCK_SH);
6496 if (unlockerr && err == NULL)
6497 err = unlockerr;
6498 return err;
6501 const struct got_error *
6502 got_worktree_rebase_abort(struct got_worktree *worktree,
6503 struct got_fileindex *fileindex, struct got_repository *repo,
6504 struct got_reference *new_base_branch,
6505 got_worktree_checkout_cb progress_cb, void *progress_arg)
6507 const struct got_error *err, *unlockerr, *sync_err;
6508 struct got_reference *resolved = NULL;
6509 struct got_object_id *commit_id = NULL;
6510 char *fileindex_path = NULL;
6511 struct revert_file_args rfa;
6512 struct got_object_id *tree_id = NULL;
6514 err = lock_worktree(worktree, LOCK_EX);
6515 if (err)
6516 return err;
6518 err = got_ref_open(&resolved, repo,
6519 got_ref_get_symref_target(new_base_branch), 0);
6520 if (err)
6521 goto done;
6523 err = got_worktree_set_head_ref(worktree, resolved);
6524 if (err)
6525 goto done;
6528 * XXX commits to the base branch could have happened while
6529 * we were busy rebasing; should we store the original commit ID
6530 * when rebase begins and read it back here?
6532 err = got_ref_resolve(&commit_id, repo, resolved);
6533 if (err)
6534 goto done;
6536 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6537 if (err)
6538 goto done;
6540 err = got_object_id_by_path(&tree_id, repo,
6541 worktree->base_commit_id, worktree->path_prefix);
6542 if (err)
6543 goto done;
6545 err = delete_rebase_refs(worktree, repo);
6546 if (err)
6547 goto done;
6549 err = get_fileindex_path(&fileindex_path, worktree);
6550 if (err)
6551 goto done;
6553 rfa.worktree = worktree;
6554 rfa.fileindex = fileindex;
6555 rfa.progress_cb = progress_cb;
6556 rfa.progress_arg = progress_arg;
6557 rfa.patch_cb = NULL;
6558 rfa.patch_arg = NULL;
6559 rfa.repo = repo;
6560 err = worktree_status(worktree, "", fileindex, repo,
6561 revert_file, &rfa, NULL, NULL, 0, 0);
6562 if (err)
6563 goto sync;
6565 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6566 repo, progress_cb, progress_arg, NULL, NULL);
6567 sync:
6568 sync_err = sync_fileindex(fileindex, fileindex_path);
6569 if (sync_err && err == NULL)
6570 err = sync_err;
6571 done:
6572 got_ref_close(resolved);
6573 free(tree_id);
6574 free(commit_id);
6575 if (fileindex)
6576 got_fileindex_free(fileindex);
6577 free(fileindex_path);
6579 unlockerr = lock_worktree(worktree, LOCK_SH);
6580 if (unlockerr && err == NULL)
6581 err = unlockerr;
6582 return err;
6585 const struct got_error *
6586 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6587 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6588 struct got_fileindex **fileindex, struct got_worktree *worktree,
6589 struct got_repository *repo)
6591 const struct got_error *err = NULL;
6592 char *tmp_branch_name = NULL;
6593 char *branch_ref_name = NULL;
6594 char *base_commit_ref_name = NULL;
6595 char *fileindex_path = NULL;
6596 struct check_rebase_ok_arg ok_arg;
6597 struct got_reference *wt_branch = NULL;
6598 struct got_reference *base_commit_ref = NULL;
6600 *tmp_branch = NULL;
6601 *branch_ref = NULL;
6602 *base_commit_id = NULL;
6603 *fileindex = NULL;
6605 err = lock_worktree(worktree, LOCK_EX);
6606 if (err)
6607 return err;
6609 err = open_fileindex(fileindex, &fileindex_path, worktree);
6610 if (err)
6611 goto done;
6613 ok_arg.worktree = worktree;
6614 ok_arg.repo = repo;
6615 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6616 &ok_arg);
6617 if (err)
6618 goto done;
6620 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6621 if (err)
6622 goto done;
6624 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6625 if (err)
6626 goto done;
6628 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6629 worktree);
6630 if (err)
6631 goto done;
6633 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6634 0);
6635 if (err)
6636 goto done;
6638 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6639 if (err)
6640 goto done;
6642 err = got_ref_write(*branch_ref, repo);
6643 if (err)
6644 goto done;
6646 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6647 worktree->base_commit_id);
6648 if (err)
6649 goto done;
6650 err = got_ref_write(base_commit_ref, repo);
6651 if (err)
6652 goto done;
6653 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6654 if (*base_commit_id == NULL) {
6655 err = got_error_from_errno("got_object_id_dup");
6656 goto done;
6659 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6660 worktree->base_commit_id);
6661 if (err)
6662 goto done;
6663 err = got_ref_write(*tmp_branch, repo);
6664 if (err)
6665 goto done;
6667 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6668 if (err)
6669 goto done;
6670 done:
6671 free(fileindex_path);
6672 free(tmp_branch_name);
6673 free(branch_ref_name);
6674 free(base_commit_ref_name);
6675 if (wt_branch)
6676 got_ref_close(wt_branch);
6677 if (err) {
6678 if (*branch_ref) {
6679 got_ref_close(*branch_ref);
6680 *branch_ref = NULL;
6682 if (*tmp_branch) {
6683 got_ref_close(*tmp_branch);
6684 *tmp_branch = NULL;
6686 free(*base_commit_id);
6687 if (*fileindex) {
6688 got_fileindex_free(*fileindex);
6689 *fileindex = NULL;
6691 lock_worktree(worktree, LOCK_SH);
6693 return err;
6696 const struct got_error *
6697 got_worktree_histedit_postpone(struct got_worktree *worktree,
6698 struct got_fileindex *fileindex)
6700 if (fileindex)
6701 got_fileindex_free(fileindex);
6702 return lock_worktree(worktree, LOCK_SH);
6705 const struct got_error *
6706 got_worktree_histedit_in_progress(int *in_progress,
6707 struct got_worktree *worktree)
6709 const struct got_error *err;
6710 char *tmp_branch_name = NULL;
6712 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6713 if (err)
6714 return err;
6716 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6717 free(tmp_branch_name);
6718 return NULL;
6721 const struct got_error *
6722 got_worktree_histedit_continue(struct got_object_id **commit_id,
6723 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6724 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6725 struct got_worktree *worktree, struct got_repository *repo)
6727 const struct got_error *err;
6728 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6729 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6730 struct got_reference *commit_ref = NULL;
6731 struct got_reference *base_commit_ref = NULL;
6732 char *fileindex_path = NULL;
6733 int have_staged_files = 0;
6735 *commit_id = NULL;
6736 *tmp_branch = NULL;
6737 *base_commit_id = NULL;
6738 *fileindex = NULL;
6740 err = lock_worktree(worktree, LOCK_EX);
6741 if (err)
6742 return err;
6744 err = open_fileindex(fileindex, &fileindex_path, worktree);
6745 if (err)
6746 goto done;
6748 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6749 &have_staged_files);
6750 if (err && err->code != GOT_ERR_CANCELLED)
6751 goto done;
6752 if (have_staged_files) {
6753 err = got_error(GOT_ERR_STAGED_PATHS);
6754 goto done;
6757 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6758 if (err)
6759 goto done;
6761 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6762 if (err)
6763 goto done;
6765 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6766 if (err)
6767 goto done;
6769 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6770 worktree);
6771 if (err)
6772 goto done;
6774 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6775 if (err)
6776 goto done;
6778 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6779 if (err)
6780 goto done;
6781 err = got_ref_resolve(commit_id, repo, commit_ref);
6782 if (err)
6783 goto done;
6785 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6786 if (err)
6787 goto done;
6788 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6789 if (err)
6790 goto done;
6792 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6793 if (err)
6794 goto done;
6795 done:
6796 free(commit_ref_name);
6797 free(branch_ref_name);
6798 free(fileindex_path);
6799 if (commit_ref)
6800 got_ref_close(commit_ref);
6801 if (base_commit_ref)
6802 got_ref_close(base_commit_ref);
6803 if (err) {
6804 free(*commit_id);
6805 *commit_id = NULL;
6806 free(*base_commit_id);
6807 *base_commit_id = NULL;
6808 if (*tmp_branch) {
6809 got_ref_close(*tmp_branch);
6810 *tmp_branch = NULL;
6812 if (*fileindex) {
6813 got_fileindex_free(*fileindex);
6814 *fileindex = NULL;
6816 lock_worktree(worktree, LOCK_EX);
6818 return err;
6821 static const struct got_error *
6822 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6824 const struct got_error *err;
6825 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6826 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6828 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6829 if (err)
6830 goto done;
6831 err = delete_ref(tmp_branch_name, repo);
6832 if (err)
6833 goto done;
6835 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6836 worktree);
6837 if (err)
6838 goto done;
6839 err = delete_ref(base_commit_ref_name, repo);
6840 if (err)
6841 goto done;
6843 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6844 if (err)
6845 goto done;
6846 err = delete_ref(branch_ref_name, repo);
6847 if (err)
6848 goto done;
6850 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6851 if (err)
6852 goto done;
6853 err = delete_ref(commit_ref_name, repo);
6854 if (err)
6855 goto done;
6856 done:
6857 free(tmp_branch_name);
6858 free(base_commit_ref_name);
6859 free(branch_ref_name);
6860 free(commit_ref_name);
6861 return err;
6864 const struct got_error *
6865 got_worktree_histedit_abort(struct got_worktree *worktree,
6866 struct got_fileindex *fileindex, struct got_repository *repo,
6867 struct got_reference *branch, struct got_object_id *base_commit_id,
6868 got_worktree_checkout_cb progress_cb, void *progress_arg)
6870 const struct got_error *err, *unlockerr, *sync_err;
6871 struct got_reference *resolved = NULL;
6872 char *fileindex_path = NULL;
6873 struct got_object_id *tree_id = NULL;
6874 struct revert_file_args rfa;
6876 err = lock_worktree(worktree, LOCK_EX);
6877 if (err)
6878 return err;
6880 err = got_ref_open(&resolved, repo,
6881 got_ref_get_symref_target(branch), 0);
6882 if (err)
6883 goto done;
6885 err = got_worktree_set_head_ref(worktree, resolved);
6886 if (err)
6887 goto done;
6889 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6890 if (err)
6891 goto done;
6893 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6894 worktree->path_prefix);
6895 if (err)
6896 goto done;
6898 err = delete_histedit_refs(worktree, repo);
6899 if (err)
6900 goto done;
6902 err = get_fileindex_path(&fileindex_path, worktree);
6903 if (err)
6904 goto done;
6906 rfa.worktree = worktree;
6907 rfa.fileindex = fileindex;
6908 rfa.progress_cb = progress_cb;
6909 rfa.progress_arg = progress_arg;
6910 rfa.patch_cb = NULL;
6911 rfa.patch_arg = NULL;
6912 rfa.repo = repo;
6913 err = worktree_status(worktree, "", fileindex, repo,
6914 revert_file, &rfa, NULL, NULL, 0, 0);
6915 if (err)
6916 goto sync;
6918 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6919 repo, progress_cb, progress_arg, NULL, NULL);
6920 sync:
6921 sync_err = sync_fileindex(fileindex, fileindex_path);
6922 if (sync_err && err == NULL)
6923 err = sync_err;
6924 done:
6925 got_ref_close(resolved);
6926 free(tree_id);
6927 free(fileindex_path);
6929 unlockerr = lock_worktree(worktree, LOCK_SH);
6930 if (unlockerr && err == NULL)
6931 err = unlockerr;
6932 return err;
6935 const struct got_error *
6936 got_worktree_histedit_complete(struct got_worktree *worktree,
6937 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6938 struct got_reference *edited_branch, struct got_repository *repo)
6940 const struct got_error *err, *unlockerr, *sync_err;
6941 struct got_object_id *new_head_commit_id = NULL;
6942 struct got_reference *resolved = NULL;
6943 char *fileindex_path = NULL;
6945 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6946 if (err)
6947 return err;
6949 err = got_ref_open(&resolved, repo,
6950 got_ref_get_symref_target(edited_branch), 0);
6951 if (err)
6952 goto done;
6954 err = got_ref_change_ref(resolved, new_head_commit_id);
6955 if (err)
6956 goto done;
6958 err = got_ref_write(resolved, repo);
6959 if (err)
6960 goto done;
6962 err = got_worktree_set_head_ref(worktree, resolved);
6963 if (err)
6964 goto done;
6966 err = delete_histedit_refs(worktree, repo);
6967 if (err)
6968 goto done;
6970 err = get_fileindex_path(&fileindex_path, worktree);
6971 if (err)
6972 goto done;
6973 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6974 sync_err = sync_fileindex(fileindex, fileindex_path);
6975 if (sync_err && err == NULL)
6976 err = sync_err;
6977 done:
6978 got_fileindex_free(fileindex);
6979 free(fileindex_path);
6980 free(new_head_commit_id);
6981 unlockerr = lock_worktree(worktree, LOCK_SH);
6982 if (unlockerr && err == NULL)
6983 err = unlockerr;
6984 return err;
6987 const struct got_error *
6988 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6989 struct got_object_id *commit_id, struct got_repository *repo)
6991 const struct got_error *err;
6992 char *commit_ref_name;
6994 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6995 if (err)
6996 return err;
6998 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6999 if (err)
7000 goto done;
7002 err = delete_ref(commit_ref_name, repo);
7003 done:
7004 free(commit_ref_name);
7005 return err;
7008 const struct got_error *
7009 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7010 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7011 struct got_worktree *worktree, const char *refname,
7012 struct got_repository *repo)
7014 const struct got_error *err = NULL;
7015 char *fileindex_path = NULL;
7016 struct check_rebase_ok_arg ok_arg;
7018 *fileindex = NULL;
7019 *branch_ref = NULL;
7020 *base_branch_ref = NULL;
7022 err = lock_worktree(worktree, LOCK_EX);
7023 if (err)
7024 return err;
7026 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7027 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7028 "cannot integrate a branch into itself; "
7029 "update -b or different branch name required");
7030 goto done;
7033 err = open_fileindex(fileindex, &fileindex_path, worktree);
7034 if (err)
7035 goto done;
7037 /* Preconditions are the same as for rebase. */
7038 ok_arg.worktree = worktree;
7039 ok_arg.repo = repo;
7040 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7041 &ok_arg);
7042 if (err)
7043 goto done;
7045 err = got_ref_open(branch_ref, repo, refname, 1);
7046 if (err)
7047 goto done;
7049 err = got_ref_open(base_branch_ref, repo,
7050 got_worktree_get_head_ref_name(worktree), 1);
7051 done:
7052 if (err) {
7053 if (*branch_ref) {
7054 got_ref_close(*branch_ref);
7055 *branch_ref = NULL;
7057 if (*base_branch_ref) {
7058 got_ref_close(*base_branch_ref);
7059 *base_branch_ref = NULL;
7061 if (*fileindex) {
7062 got_fileindex_free(*fileindex);
7063 *fileindex = NULL;
7065 lock_worktree(worktree, LOCK_SH);
7067 return err;
7070 const struct got_error *
7071 got_worktree_integrate_continue(struct got_worktree *worktree,
7072 struct got_fileindex *fileindex, struct got_repository *repo,
7073 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7074 got_worktree_checkout_cb progress_cb, void *progress_arg,
7075 got_cancel_cb cancel_cb, void *cancel_arg)
7077 const struct got_error *err = NULL, *sync_err, *unlockerr;
7078 char *fileindex_path = NULL;
7079 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7081 err = get_fileindex_path(&fileindex_path, worktree);
7082 if (err)
7083 goto done;
7085 err = got_ref_resolve(&commit_id, repo, branch_ref);
7086 if (err)
7087 goto done;
7089 err = got_object_id_by_path(&tree_id, repo, commit_id,
7090 worktree->path_prefix);
7091 if (err)
7092 goto done;
7094 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7095 if (err)
7096 goto done;
7098 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7099 progress_cb, progress_arg, cancel_cb, cancel_arg);
7100 if (err)
7101 goto sync;
7103 err = got_ref_change_ref(base_branch_ref, commit_id);
7104 if (err)
7105 goto sync;
7107 err = got_ref_write(base_branch_ref, repo);
7108 sync:
7109 sync_err = sync_fileindex(fileindex, fileindex_path);
7110 if (sync_err && err == NULL)
7111 err = sync_err;
7113 done:
7114 unlockerr = got_ref_unlock(branch_ref);
7115 if (unlockerr && err == NULL)
7116 err = unlockerr;
7117 got_ref_close(branch_ref);
7119 unlockerr = got_ref_unlock(base_branch_ref);
7120 if (unlockerr && err == NULL)
7121 err = unlockerr;
7122 got_ref_close(base_branch_ref);
7124 got_fileindex_free(fileindex);
7125 free(fileindex_path);
7126 free(tree_id);
7128 unlockerr = lock_worktree(worktree, LOCK_SH);
7129 if (unlockerr && err == NULL)
7130 err = unlockerr;
7131 return err;
7134 const struct got_error *
7135 got_worktree_integrate_abort(struct got_worktree *worktree,
7136 struct got_fileindex *fileindex, struct got_repository *repo,
7137 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7139 const struct got_error *err = NULL, *unlockerr = NULL;
7141 got_fileindex_free(fileindex);
7143 err = lock_worktree(worktree, LOCK_SH);
7145 unlockerr = got_ref_unlock(branch_ref);
7146 if (unlockerr && err == NULL)
7147 err = unlockerr;
7148 got_ref_close(branch_ref);
7150 unlockerr = got_ref_unlock(base_branch_ref);
7151 if (unlockerr && err == NULL)
7152 err = unlockerr;
7153 got_ref_close(base_branch_ref);
7155 return err;
7158 struct check_stage_ok_arg {
7159 struct got_object_id *head_commit_id;
7160 struct got_worktree *worktree;
7161 struct got_fileindex *fileindex;
7162 struct got_repository *repo;
7163 int have_changes;
7166 const struct got_error *
7167 check_stage_ok(void *arg, unsigned char status,
7168 unsigned char staged_status, const char *relpath,
7169 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7170 struct got_object_id *commit_id, int dirfd, const char *de_name)
7172 struct check_stage_ok_arg *a = arg;
7173 const struct got_error *err = NULL;
7174 struct got_fileindex_entry *ie;
7175 struct got_object_id base_commit_id;
7176 struct got_object_id *base_commit_idp = NULL;
7177 char *in_repo_path = NULL, *p;
7179 if (status == GOT_STATUS_UNVERSIONED ||
7180 status == GOT_STATUS_NO_CHANGE)
7181 return NULL;
7182 if (status == GOT_STATUS_NONEXISTENT)
7183 return got_error_set_errno(ENOENT, relpath);
7185 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7186 if (ie == NULL)
7187 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7189 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7190 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7191 relpath) == -1)
7192 return got_error_from_errno("asprintf");
7194 if (got_fileindex_entry_has_commit(ie)) {
7195 memcpy(base_commit_id.sha1, ie->commit_sha1,
7196 SHA1_DIGEST_LENGTH);
7197 base_commit_idp = &base_commit_id;
7200 if (status == GOT_STATUS_CONFLICT) {
7201 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7202 goto done;
7203 } else if (status != GOT_STATUS_ADD &&
7204 status != GOT_STATUS_MODIFY &&
7205 status != GOT_STATUS_DELETE) {
7206 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7207 goto done;
7210 a->have_changes = 1;
7212 p = in_repo_path;
7213 while (p[0] == '/')
7214 p++;
7215 err = check_out_of_date(p, status, staged_status,
7216 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7217 GOT_ERR_STAGE_OUT_OF_DATE);
7218 done:
7219 free(in_repo_path);
7220 return err;
7223 struct stage_path_arg {
7224 struct got_worktree *worktree;
7225 struct got_fileindex *fileindex;
7226 struct got_repository *repo;
7227 got_worktree_status_cb status_cb;
7228 void *status_arg;
7229 got_worktree_patch_cb patch_cb;
7230 void *patch_arg;
7231 int staged_something;
7232 int allow_bad_symlinks;
7235 static const struct got_error *
7236 stage_path(void *arg, unsigned char status,
7237 unsigned char staged_status, const char *relpath,
7238 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7239 struct got_object_id *commit_id, int dirfd, const char *de_name)
7241 struct stage_path_arg *a = arg;
7242 const struct got_error *err = NULL;
7243 struct got_fileindex_entry *ie;
7244 char *ondisk_path = NULL, *path_content = NULL;
7245 uint32_t stage;
7246 struct got_object_id *new_staged_blob_id = NULL;
7247 struct stat sb;
7249 if (status == GOT_STATUS_UNVERSIONED)
7250 return NULL;
7252 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7253 if (ie == NULL)
7254 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7256 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7257 relpath)== -1)
7258 return got_error_from_errno("asprintf");
7260 switch (status) {
7261 case GOT_STATUS_ADD:
7262 case GOT_STATUS_MODIFY:
7263 /* XXX could sb.st_mode be passed in by our caller? */
7264 if (lstat(ondisk_path, &sb) == -1) {
7265 err = got_error_from_errno2("lstat", ondisk_path);
7266 break;
7268 if (a->patch_cb) {
7269 if (status == GOT_STATUS_ADD) {
7270 int choice = GOT_PATCH_CHOICE_NONE;
7271 err = (*a->patch_cb)(&choice, a->patch_arg,
7272 status, ie->path, NULL, 1, 1);
7273 if (err)
7274 break;
7275 if (choice != GOT_PATCH_CHOICE_YES)
7276 break;
7277 } else {
7278 err = create_patched_content(&path_content, 0,
7279 staged_blob_id ? staged_blob_id : blob_id,
7280 ondisk_path, dirfd, de_name, ie->path,
7281 a->repo, a->patch_cb, a->patch_arg);
7282 if (err || path_content == NULL)
7283 break;
7286 err = got_object_blob_create(&new_staged_blob_id,
7287 path_content ? path_content : ondisk_path, a->repo);
7288 if (err)
7289 break;
7290 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7291 SHA1_DIGEST_LENGTH);
7292 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7293 stage = GOT_FILEIDX_STAGE_ADD;
7294 else
7295 stage = GOT_FILEIDX_STAGE_MODIFY;
7296 got_fileindex_entry_stage_set(ie, stage);
7297 if (S_ISLNK(sb.st_mode)) {
7298 int is_bad_symlink = 0;
7299 if (!a->allow_bad_symlinks) {
7300 char target_path[PATH_MAX];
7301 ssize_t target_len;
7302 target_len = readlink(ondisk_path, target_path,
7303 sizeof(target_path));
7304 if (target_len == -1) {
7305 err = got_error_from_errno2("readlink",
7306 ondisk_path);
7307 break;
7309 err = is_bad_symlink_target(&is_bad_symlink,
7310 target_path, target_len, ondisk_path,
7311 a->worktree->root_path);
7312 if (err)
7313 break;
7314 if (is_bad_symlink) {
7315 err = got_error_path(ondisk_path,
7316 GOT_ERR_BAD_SYMLINK);
7317 break;
7320 if (is_bad_symlink)
7321 got_fileindex_entry_staged_filetype_set(ie,
7322 GOT_FILEIDX_MODE_BAD_SYMLINK);
7323 else
7324 got_fileindex_entry_staged_filetype_set(ie,
7325 GOT_FILEIDX_MODE_SYMLINK);
7326 } else {
7327 got_fileindex_entry_staged_filetype_set(ie,
7328 GOT_FILEIDX_MODE_REGULAR_FILE);
7330 a->staged_something = 1;
7331 if (a->status_cb == NULL)
7332 break;
7333 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7334 get_staged_status(ie), relpath, blob_id,
7335 new_staged_blob_id, NULL, dirfd, de_name);
7336 break;
7337 case GOT_STATUS_DELETE:
7338 if (staged_status == GOT_STATUS_DELETE)
7339 break;
7340 if (a->patch_cb) {
7341 int choice = GOT_PATCH_CHOICE_NONE;
7342 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7343 ie->path, NULL, 1, 1);
7344 if (err)
7345 break;
7346 if (choice == GOT_PATCH_CHOICE_NO)
7347 break;
7348 if (choice != GOT_PATCH_CHOICE_YES) {
7349 err = got_error(GOT_ERR_PATCH_CHOICE);
7350 break;
7353 stage = GOT_FILEIDX_STAGE_DELETE;
7354 got_fileindex_entry_stage_set(ie, stage);
7355 a->staged_something = 1;
7356 if (a->status_cb == NULL)
7357 break;
7358 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7359 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7360 de_name);
7361 break;
7362 case GOT_STATUS_NO_CHANGE:
7363 break;
7364 case GOT_STATUS_CONFLICT:
7365 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7366 break;
7367 case GOT_STATUS_NONEXISTENT:
7368 err = got_error_set_errno(ENOENT, relpath);
7369 break;
7370 default:
7371 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7372 break;
7375 if (path_content && unlink(path_content) == -1 && err == NULL)
7376 err = got_error_from_errno2("unlink", path_content);
7377 free(path_content);
7378 free(ondisk_path);
7379 free(new_staged_blob_id);
7380 return err;
7383 const struct got_error *
7384 got_worktree_stage(struct got_worktree *worktree,
7385 struct got_pathlist_head *paths,
7386 got_worktree_status_cb status_cb, void *status_arg,
7387 got_worktree_patch_cb patch_cb, void *patch_arg,
7388 int allow_bad_symlinks, struct got_repository *repo)
7390 const struct got_error *err = NULL, *sync_err, *unlockerr;
7391 struct got_pathlist_entry *pe;
7392 struct got_fileindex *fileindex = NULL;
7393 char *fileindex_path = NULL;
7394 struct got_reference *head_ref = NULL;
7395 struct got_object_id *head_commit_id = NULL;
7396 struct check_stage_ok_arg oka;
7397 struct stage_path_arg spa;
7399 err = lock_worktree(worktree, LOCK_EX);
7400 if (err)
7401 return err;
7403 err = got_ref_open(&head_ref, repo,
7404 got_worktree_get_head_ref_name(worktree), 0);
7405 if (err)
7406 goto done;
7407 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7408 if (err)
7409 goto done;
7410 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7411 if (err)
7412 goto done;
7414 /* Check pre-conditions before staging anything. */
7415 oka.head_commit_id = head_commit_id;
7416 oka.worktree = worktree;
7417 oka.fileindex = fileindex;
7418 oka.repo = repo;
7419 oka.have_changes = 0;
7420 TAILQ_FOREACH(pe, paths, entry) {
7421 err = worktree_status(worktree, pe->path, fileindex, repo,
7422 check_stage_ok, &oka, NULL, NULL, 0, 0);
7423 if (err)
7424 goto done;
7426 if (!oka.have_changes) {
7427 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7428 goto done;
7431 spa.worktree = worktree;
7432 spa.fileindex = fileindex;
7433 spa.repo = repo;
7434 spa.patch_cb = patch_cb;
7435 spa.patch_arg = patch_arg;
7436 spa.status_cb = status_cb;
7437 spa.status_arg = status_arg;
7438 spa.staged_something = 0;
7439 spa.allow_bad_symlinks = allow_bad_symlinks;
7440 TAILQ_FOREACH(pe, paths, entry) {
7441 err = worktree_status(worktree, pe->path, fileindex, repo,
7442 stage_path, &spa, NULL, NULL, 0, 0);
7443 if (err)
7444 goto done;
7446 if (!spa.staged_something) {
7447 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7448 goto done;
7451 sync_err = sync_fileindex(fileindex, fileindex_path);
7452 if (sync_err && err == NULL)
7453 err = sync_err;
7454 done:
7455 if (head_ref)
7456 got_ref_close(head_ref);
7457 free(head_commit_id);
7458 free(fileindex_path);
7459 if (fileindex)
7460 got_fileindex_free(fileindex);
7461 unlockerr = lock_worktree(worktree, LOCK_SH);
7462 if (unlockerr && err == NULL)
7463 err = unlockerr;
7464 return err;
7467 struct unstage_path_arg {
7468 struct got_worktree *worktree;
7469 struct got_fileindex *fileindex;
7470 struct got_repository *repo;
7471 got_worktree_checkout_cb progress_cb;
7472 void *progress_arg;
7473 got_worktree_patch_cb patch_cb;
7474 void *patch_arg;
7477 static const struct got_error *
7478 create_unstaged_content(char **path_unstaged_content,
7479 char **path_new_staged_content, struct got_object_id *blob_id,
7480 struct got_object_id *staged_blob_id, const char *relpath,
7481 struct got_repository *repo,
7482 got_worktree_patch_cb patch_cb, void *patch_arg)
7484 const struct got_error *err, *free_err;
7485 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7486 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7487 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7488 struct got_diffreg_result *diffreg_result = NULL;
7489 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7490 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7492 *path_unstaged_content = NULL;
7493 *path_new_staged_content = NULL;
7495 err = got_object_id_str(&label1, blob_id);
7496 if (err)
7497 return err;
7498 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7499 if (err)
7500 goto done;
7502 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7503 if (err)
7504 goto done;
7506 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7507 if (err)
7508 goto done;
7510 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7511 if (err)
7512 goto done;
7514 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7515 if (err)
7516 goto done;
7518 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7519 if (err)
7520 goto done;
7522 err = got_diff_files(&diffreg_result, f1, label1, f2,
7523 path2, 3, 0, 1, NULL);
7524 if (err)
7525 goto done;
7527 err = got_opentemp_named(path_unstaged_content, &outfile,
7528 "got-unstaged-content");
7529 if (err)
7530 goto done;
7531 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7532 "got-new-staged-content");
7533 if (err)
7534 goto done;
7536 if (fseek(f1, 0L, SEEK_SET) == -1) {
7537 err = got_ferror(f1, GOT_ERR_IO);
7538 goto done;
7540 if (fseek(f2, 0L, SEEK_SET) == -1) {
7541 err = got_ferror(f2, GOT_ERR_IO);
7542 goto done;
7544 /* Count the number of actual changes in the diff result. */
7545 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7546 struct diff_chunk_context cc = {};
7547 diff_chunk_context_load_change(&cc, &nchunks_used,
7548 diffreg_result->result, n, 0);
7549 nchanges++;
7551 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7552 int choice;
7553 err = apply_or_reject_change(&choice, &nchunks_used,
7554 diffreg_result->result, n, relpath, f1, f2,
7555 &line_cur1, &line_cur2,
7556 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7557 if (err)
7558 goto done;
7559 if (choice == GOT_PATCH_CHOICE_YES)
7560 have_content = 1;
7561 else
7562 have_rejected_content = 1;
7563 if (choice == GOT_PATCH_CHOICE_QUIT)
7564 break;
7566 if (have_content || have_rejected_content)
7567 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7568 outfile, rejectfile);
7569 done:
7570 free(label1);
7571 if (blob)
7572 got_object_blob_close(blob);
7573 if (staged_blob)
7574 got_object_blob_close(staged_blob);
7575 free_err = got_diffreg_result_free(diffreg_result);
7576 if (free_err && err == NULL)
7577 err = free_err;
7578 if (f1 && fclose(f1) == EOF && err == NULL)
7579 err = got_error_from_errno2("fclose", path1);
7580 if (f2 && fclose(f2) == EOF && err == NULL)
7581 err = got_error_from_errno2("fclose", path2);
7582 if (outfile && fclose(outfile) == EOF && err == NULL)
7583 err = got_error_from_errno2("fclose", *path_unstaged_content);
7584 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7585 err = got_error_from_errno2("fclose", *path_new_staged_content);
7586 if (path1 && unlink(path1) == -1 && err == NULL)
7587 err = got_error_from_errno2("unlink", path1);
7588 if (path2 && unlink(path2) == -1 && err == NULL)
7589 err = got_error_from_errno2("unlink", path2);
7590 if (err || !have_content) {
7591 if (*path_unstaged_content &&
7592 unlink(*path_unstaged_content) == -1 && err == NULL)
7593 err = got_error_from_errno2("unlink",
7594 *path_unstaged_content);
7595 free(*path_unstaged_content);
7596 *path_unstaged_content = NULL;
7598 if (err || !have_content || !have_rejected_content) {
7599 if (*path_new_staged_content &&
7600 unlink(*path_new_staged_content) == -1 && err == NULL)
7601 err = got_error_from_errno2("unlink",
7602 *path_new_staged_content);
7603 free(*path_new_staged_content);
7604 *path_new_staged_content = NULL;
7606 free(path1);
7607 free(path2);
7608 return err;
7611 static const struct got_error *
7612 unstage_hunks(struct got_object_id *staged_blob_id,
7613 struct got_blob_object *blob_base,
7614 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7615 const char *ondisk_path, const char *label_orig,
7616 struct got_worktree *worktree, struct got_repository *repo,
7617 got_worktree_patch_cb patch_cb, void *patch_arg,
7618 got_worktree_checkout_cb progress_cb, void *progress_arg)
7620 const struct got_error *err = NULL;
7621 char *path_unstaged_content = NULL;
7622 char *path_new_staged_content = NULL;
7623 struct got_object_id *new_staged_blob_id = NULL;
7624 FILE *f = NULL;
7625 struct stat sb;
7627 err = create_unstaged_content(&path_unstaged_content,
7628 &path_new_staged_content, blob_id, staged_blob_id,
7629 ie->path, repo, patch_cb, patch_arg);
7630 if (err)
7631 return err;
7633 if (path_unstaged_content == NULL)
7634 return NULL;
7636 if (path_new_staged_content) {
7637 err = got_object_blob_create(&new_staged_blob_id,
7638 path_new_staged_content, repo);
7639 if (err)
7640 goto done;
7643 f = fopen(path_unstaged_content, "r");
7644 if (f == NULL) {
7645 err = got_error_from_errno2("fopen",
7646 path_unstaged_content);
7647 goto done;
7649 if (fstat(fileno(f), &sb) == -1) {
7650 err = got_error_from_errno2("fstat", path_unstaged_content);
7651 goto done;
7653 if (got_fileindex_entry_staged_filetype_get(ie) ==
7654 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7655 char link_target[PATH_MAX];
7656 size_t r;
7657 r = fread(link_target, 1, sizeof(link_target), f);
7658 if (r == 0 && ferror(f)) {
7659 err = got_error_from_errno("fread");
7660 goto done;
7662 if (r >= sizeof(link_target)) { /* should not happen */
7663 err = got_error(GOT_ERR_NO_SPACE);
7664 goto done;
7666 link_target[r] = '\0';
7667 err = merge_symlink(worktree, blob_base,
7668 ondisk_path, ie->path, label_orig, link_target,
7669 worktree->base_commit_id, repo, progress_cb,
7670 progress_arg);
7671 } else {
7672 int local_changes_subsumed;
7673 err = merge_file(&local_changes_subsumed, worktree,
7674 blob_base, ondisk_path, ie->path,
7675 got_fileindex_perms_to_st(ie),
7676 path_unstaged_content, label_orig, "unstaged",
7677 repo, progress_cb, progress_arg);
7679 if (err)
7680 goto done;
7682 if (new_staged_blob_id) {
7683 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7684 SHA1_DIGEST_LENGTH);
7685 } else {
7686 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7687 got_fileindex_entry_staged_filetype_set(ie, 0);
7689 done:
7690 free(new_staged_blob_id);
7691 if (path_unstaged_content &&
7692 unlink(path_unstaged_content) == -1 && err == NULL)
7693 err = got_error_from_errno2("unlink", path_unstaged_content);
7694 if (path_new_staged_content &&
7695 unlink(path_new_staged_content) == -1 && err == NULL)
7696 err = got_error_from_errno2("unlink", path_new_staged_content);
7697 if (f && fclose(f) == EOF && err == NULL)
7698 err = got_error_from_errno2("fclose", path_unstaged_content);
7699 free(path_unstaged_content);
7700 free(path_new_staged_content);
7701 return err;
7704 static const struct got_error *
7705 unstage_path(void *arg, unsigned char status,
7706 unsigned char staged_status, const char *relpath,
7707 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7708 struct got_object_id *commit_id, int dirfd, const char *de_name)
7710 const struct got_error *err = NULL;
7711 struct unstage_path_arg *a = arg;
7712 struct got_fileindex_entry *ie;
7713 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7714 char *ondisk_path = NULL;
7715 char *id_str = NULL, *label_orig = NULL;
7716 int local_changes_subsumed;
7717 struct stat sb;
7719 if (staged_status != GOT_STATUS_ADD &&
7720 staged_status != GOT_STATUS_MODIFY &&
7721 staged_status != GOT_STATUS_DELETE)
7722 return NULL;
7724 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7725 if (ie == NULL)
7726 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7728 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7729 == -1)
7730 return got_error_from_errno("asprintf");
7732 err = got_object_id_str(&id_str,
7733 commit_id ? commit_id : a->worktree->base_commit_id);
7734 if (err)
7735 goto done;
7736 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7737 id_str) == -1) {
7738 err = got_error_from_errno("asprintf");
7739 goto done;
7742 switch (staged_status) {
7743 case GOT_STATUS_MODIFY:
7744 err = got_object_open_as_blob(&blob_base, a->repo,
7745 blob_id, 8192);
7746 if (err)
7747 break;
7748 /* fall through */
7749 case GOT_STATUS_ADD:
7750 if (a->patch_cb) {
7751 if (staged_status == GOT_STATUS_ADD) {
7752 int choice = GOT_PATCH_CHOICE_NONE;
7753 err = (*a->patch_cb)(&choice, a->patch_arg,
7754 staged_status, ie->path, NULL, 1, 1);
7755 if (err)
7756 break;
7757 if (choice != GOT_PATCH_CHOICE_YES)
7758 break;
7759 } else {
7760 err = unstage_hunks(staged_blob_id,
7761 blob_base, blob_id, ie, ondisk_path,
7762 label_orig, a->worktree, a->repo,
7763 a->patch_cb, a->patch_arg,
7764 a->progress_cb, a->progress_arg);
7765 break; /* Done with this file. */
7768 err = got_object_open_as_blob(&blob_staged, a->repo,
7769 staged_blob_id, 8192);
7770 if (err)
7771 break;
7772 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7773 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7774 case GOT_FILEIDX_MODE_REGULAR_FILE:
7775 err = merge_blob(&local_changes_subsumed, a->worktree,
7776 blob_base, ondisk_path, relpath,
7777 got_fileindex_perms_to_st(ie), label_orig,
7778 blob_staged, commit_id ? commit_id :
7779 a->worktree->base_commit_id, a->repo,
7780 a->progress_cb, a->progress_arg);
7781 break;
7782 case GOT_FILEIDX_MODE_SYMLINK:
7783 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7784 char *staged_target;
7785 err = got_object_blob_read_to_str(
7786 &staged_target, blob_staged);
7787 if (err)
7788 goto done;
7789 err = merge_symlink(a->worktree, blob_base,
7790 ondisk_path, relpath, label_orig,
7791 staged_target, commit_id ? commit_id :
7792 a->worktree->base_commit_id,
7793 a->repo, a->progress_cb, a->progress_arg);
7794 free(staged_target);
7795 } else {
7796 err = merge_blob(&local_changes_subsumed,
7797 a->worktree, blob_base, ondisk_path,
7798 relpath, got_fileindex_perms_to_st(ie),
7799 label_orig, blob_staged,
7800 commit_id ? commit_id :
7801 a->worktree->base_commit_id, a->repo,
7802 a->progress_cb, a->progress_arg);
7804 break;
7805 default:
7806 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7807 break;
7809 if (err == NULL) {
7810 got_fileindex_entry_stage_set(ie,
7811 GOT_FILEIDX_STAGE_NONE);
7812 got_fileindex_entry_staged_filetype_set(ie, 0);
7814 break;
7815 case GOT_STATUS_DELETE:
7816 if (a->patch_cb) {
7817 int choice = GOT_PATCH_CHOICE_NONE;
7818 err = (*a->patch_cb)(&choice, a->patch_arg,
7819 staged_status, ie->path, NULL, 1, 1);
7820 if (err)
7821 break;
7822 if (choice == GOT_PATCH_CHOICE_NO)
7823 break;
7824 if (choice != GOT_PATCH_CHOICE_YES) {
7825 err = got_error(GOT_ERR_PATCH_CHOICE);
7826 break;
7829 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7830 got_fileindex_entry_staged_filetype_set(ie, 0);
7831 err = get_file_status(&status, &sb, ie, ondisk_path,
7832 dirfd, de_name, a->repo);
7833 if (err)
7834 break;
7835 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7836 break;
7838 done:
7839 free(ondisk_path);
7840 if (blob_base)
7841 got_object_blob_close(blob_base);
7842 if (blob_staged)
7843 got_object_blob_close(blob_staged);
7844 free(id_str);
7845 free(label_orig);
7846 return err;
7849 const struct got_error *
7850 got_worktree_unstage(struct got_worktree *worktree,
7851 struct got_pathlist_head *paths,
7852 got_worktree_checkout_cb progress_cb, void *progress_arg,
7853 got_worktree_patch_cb patch_cb, void *patch_arg,
7854 struct got_repository *repo)
7856 const struct got_error *err = NULL, *sync_err, *unlockerr;
7857 struct got_pathlist_entry *pe;
7858 struct got_fileindex *fileindex = NULL;
7859 char *fileindex_path = NULL;
7860 struct unstage_path_arg upa;
7862 err = lock_worktree(worktree, LOCK_EX);
7863 if (err)
7864 return err;
7866 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7867 if (err)
7868 goto done;
7870 upa.worktree = worktree;
7871 upa.fileindex = fileindex;
7872 upa.repo = repo;
7873 upa.progress_cb = progress_cb;
7874 upa.progress_arg = progress_arg;
7875 upa.patch_cb = patch_cb;
7876 upa.patch_arg = patch_arg;
7877 TAILQ_FOREACH(pe, paths, entry) {
7878 err = worktree_status(worktree, pe->path, fileindex, repo,
7879 unstage_path, &upa, NULL, NULL, 0, 0);
7880 if (err)
7881 goto done;
7884 sync_err = sync_fileindex(fileindex, fileindex_path);
7885 if (sync_err && err == NULL)
7886 err = sync_err;
7887 done:
7888 free(fileindex_path);
7889 if (fileindex)
7890 got_fileindex_free(fileindex);
7891 unlockerr = lock_worktree(worktree, LOCK_SH);
7892 if (unlockerr && err == NULL)
7893 err = unlockerr;
7894 return err;
7897 struct report_file_info_arg {
7898 struct got_worktree *worktree;
7899 got_worktree_path_info_cb info_cb;
7900 void *info_arg;
7901 struct got_pathlist_head *paths;
7902 got_cancel_cb cancel_cb;
7903 void *cancel_arg;
7906 static const struct got_error *
7907 report_file_info(void *arg, struct got_fileindex_entry *ie)
7909 struct report_file_info_arg *a = arg;
7910 struct got_pathlist_entry *pe;
7911 struct got_object_id blob_id, staged_blob_id, commit_id;
7912 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7913 struct got_object_id *commit_idp = NULL;
7914 int stage;
7916 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7917 return got_error(GOT_ERR_CANCELLED);
7919 TAILQ_FOREACH(pe, a->paths, entry) {
7920 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7921 got_path_is_child(ie->path, pe->path, pe->path_len))
7922 break;
7924 if (pe == NULL) /* not found */
7925 return NULL;
7927 if (got_fileindex_entry_has_blob(ie)) {
7928 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7929 blob_idp = &blob_id;
7931 stage = got_fileindex_entry_stage_get(ie);
7932 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7933 stage == GOT_FILEIDX_STAGE_ADD) {
7934 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7935 SHA1_DIGEST_LENGTH);
7936 staged_blob_idp = &staged_blob_id;
7939 if (got_fileindex_entry_has_commit(ie)) {
7940 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7941 commit_idp = &commit_id;
7944 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7945 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7948 const struct got_error *
7949 got_worktree_path_info(struct got_worktree *worktree,
7950 struct got_pathlist_head *paths,
7951 got_worktree_path_info_cb info_cb, void *info_arg,
7952 got_cancel_cb cancel_cb, void *cancel_arg)
7955 const struct got_error *err = NULL, *unlockerr;
7956 struct got_fileindex *fileindex = NULL;
7957 char *fileindex_path = NULL;
7958 struct report_file_info_arg arg;
7960 err = lock_worktree(worktree, LOCK_SH);
7961 if (err)
7962 return err;
7964 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7965 if (err)
7966 goto done;
7968 arg.worktree = worktree;
7969 arg.info_cb = info_cb;
7970 arg.info_arg = info_arg;
7971 arg.paths = paths;
7972 arg.cancel_cb = cancel_cb;
7973 arg.cancel_arg = cancel_arg;
7974 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7975 &arg);
7976 done:
7977 free(fileindex_path);
7978 if (fileindex)
7979 got_fileindex_free(fileindex);
7980 unlockerr = lock_worktree(worktree, LOCK_UN);
7981 if (unlockerr && err == NULL)
7982 err = unlockerr;
7983 return err;