Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
69 {
70 const struct got_error *err = NULL;
71 char *path;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
77 free(path);
78 return err;
79 }
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
83 {
84 const struct got_error *err = NULL;
85 FILE *tmpfile = NULL;
86 char *tmppath = NULL;
87 char *path = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
91 path = NULL;
92 goto done;
93 }
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
96 if (err)
97 goto done;
99 if (content) {
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
103 goto done;
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
109 unlink(tmppath);
110 goto done;
113 done:
114 if (fclose(tmpfile) == EOF && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
116 free(tmppath);
117 return err;
120 static const struct got_error *
121 read_meta_file(char **content, const char *path_got, const char *name)
123 const struct got_error *err = NULL;
124 char *path;
125 int fd = -1;
126 ssize_t n;
127 struct stat sb;
129 *content = NULL;
131 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 err = got_error_from_errno("asprintf");
133 path = NULL;
134 goto done;
137 fd = open(path, O_RDONLY | O_NOFOLLOW);
138 if (fd == -1) {
139 if (errno == ENOENT)
140 err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 else
142 err = got_error_from_errno2("open", path);
143 goto done;
145 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 : got_error_from_errno2("flock", path));
148 goto done;
151 if (fstat(fd, &sb) != 0) {
152 err = got_error_from_errno2("fstat", path);
153 goto done;
155 *content = calloc(1, sb.st_size);
156 if (*content == NULL) {
157 err = got_error_from_errno("calloc");
158 goto done;
161 n = read(fd, *content, sb.st_size);
162 if (n != sb.st_size) {
163 err = (n == -1 ? got_error_from_errno2("read", path) :
164 got_error_path(path, GOT_ERR_WORKTREE_META));
165 goto done;
167 if ((*content)[sb.st_size - 1] != '\n') {
168 err = got_error_path(path, GOT_ERR_WORKTREE_META);
169 goto done;
171 (*content)[sb.st_size - 1] = '\0';
173 done:
174 if (fd != -1 && close(fd) == -1 && err == NULL)
175 err = got_error_from_errno2("close", path_got);
176 free(path);
177 if (err) {
178 free(*content);
179 *content = NULL;
181 return err;
184 static const struct got_error *
185 write_head_ref(const char *path_got, struct got_reference *head_ref)
187 const struct got_error *err = NULL;
188 char *refstr = NULL;
190 if (got_ref_is_symbolic(head_ref)) {
191 refstr = got_ref_to_str(head_ref);
192 if (refstr == NULL)
193 return got_error_from_errno("got_ref_to_str");
194 } else {
195 refstr = strdup(got_ref_get_name(head_ref));
196 if (refstr == NULL)
197 return got_error_from_errno("strdup");
199 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
200 free(refstr);
201 return err;
204 const struct got_error *
205 got_worktree_init(const char *path, struct got_reference *head_ref,
206 const char *prefix, struct got_repository *repo)
208 const struct got_error *err = NULL;
209 struct got_object_id *commit_id = NULL;
210 uuid_t uuid;
211 uint32_t uuid_status;
212 int obj_type;
213 char *path_got = NULL;
214 char *formatstr = NULL;
215 char *absprefix = NULL;
216 char *basestr = NULL;
217 char *uuidstr = NULL;
219 if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 err = got_error(GOT_ERR_WORKTREE_REPO);
221 goto done;
224 err = got_ref_resolve(&commit_id, repo, head_ref);
225 if (err)
226 return err;
227 err = got_object_get_type(&obj_type, repo, commit_id);
228 if (err)
229 return err;
230 if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 return got_error(GOT_ERR_OBJ_TYPE);
233 if (!got_path_is_absolute(prefix)) {
234 if (asprintf(&absprefix, "/%s", prefix) == -1)
235 return got_error_from_errno("asprintf");
238 /* Create top-level directory (may already exist). */
239 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 err = got_error_from_errno2("mkdir", path);
241 goto done;
244 /* Create .got directory (may already exist). */
245 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 err = got_error_from_errno("asprintf");
247 goto done;
249 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 err = got_error_from_errno2("mkdir", path_got);
251 goto done;
254 /* Create an empty lock file. */
255 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
256 if (err)
257 goto done;
259 /* Create an empty file index. */
260 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
261 if (err)
262 goto done;
264 /* Write the HEAD reference. */
265 err = write_head_ref(path_got, head_ref);
266 if (err)
267 goto done;
269 /* Record our base commit. */
270 err = got_object_id_str(&basestr, commit_id);
271 if (err)
272 goto done;
273 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
274 if (err)
275 goto done;
277 /* Store path to repository. */
278 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 got_repo_get_path(repo));
280 if (err)
281 goto done;
283 /* Store in-repository path prefix. */
284 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 absprefix ? absprefix : prefix);
286 if (err)
287 goto done;
289 /* Generate UUID. */
290 uuid_create(&uuid, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status, "uuid_create");
293 goto done;
295 uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 if (uuid_status != uuid_s_ok) {
297 err = got_error_uuid(uuid_status, "uuid_to_string");
298 goto done;
300 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
301 if (err)
302 goto done;
304 /* Stamp work tree with format file. */
305 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 err = got_error_from_errno("asprintf");
307 goto done;
309 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
310 if (err)
311 goto done;
313 done:
314 free(commit_id);
315 free(path_got);
316 free(formatstr);
317 free(absprefix);
318 free(basestr);
319 free(uuidstr);
320 return err;
323 static const struct got_error *
324 open_worktree(struct got_worktree **worktree, const char *path)
326 const struct got_error *err = NULL;
327 char *path_got;
328 char *formatstr = NULL;
329 char *uuidstr = NULL;
330 char *path_lock = NULL;
331 char *base_commit_id_str = NULL;
332 int version, fd = -1;
333 const char *errstr;
334 struct got_repository *repo = NULL;
335 uint32_t uuid_status;
337 *worktree = NULL;
339 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 err = got_error_from_errno("asprintf");
341 path_got = NULL;
342 goto done;
345 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 err = got_error_from_errno("asprintf");
347 path_lock = NULL;
348 goto done;
351 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 if (fd == -1) {
353 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 : got_error_from_errno2("open", path_lock));
355 goto done;
358 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
359 if (err)
360 goto done;
362 version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 if (errstr) {
364 err = got_error_msg(GOT_ERR_WORKTREE_META,
365 "could not parse work tree format version number");
366 goto done;
368 if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 err = got_error(GOT_ERR_WORKTREE_VERS);
370 goto done;
373 *worktree = calloc(1, sizeof(**worktree));
374 if (*worktree == NULL) {
375 err = got_error_from_errno("calloc");
376 goto done;
378 (*worktree)->lockfd = -1;
380 (*worktree)->root_path = realpath(path, NULL);
381 if ((*worktree)->root_path == NULL) {
382 err = got_error_from_errno2("realpath", path);
383 goto done;
385 err = read_meta_file(&(*worktree)->repo_path, path_got,
386 GOT_WORKTREE_REPOSITORY);
387 if (err)
388 goto done;
390 err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 GOT_WORKTREE_PATH_PREFIX);
392 if (err)
393 goto done;
395 err = read_meta_file(&base_commit_id_str, path_got,
396 GOT_WORKTREE_BASE_COMMIT);
397 if (err)
398 goto done;
400 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
401 if (err)
402 goto done;
403 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 if (uuid_status != uuid_s_ok) {
405 err = got_error_uuid(uuid_status, "uuid_from_string");
406 goto done;
409 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
410 if (err)
411 goto done;
413 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 base_commit_id_str);
415 if (err)
416 goto done;
418 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 GOT_WORKTREE_HEAD_REF);
420 if (err)
421 goto done;
423 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 (*worktree)->root_path,
425 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 err = got_error_from_errno("asprintf");
427 goto done;
430 err = got_gotconfig_read(&(*worktree)->gotconfig,
431 (*worktree)->gotconfig_path);
433 (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
434 if ((*worktree)->root_fd == -1) {
435 err = got_error_from_errno2("open", (*worktree)->root_path);
436 goto done;
438 done:
439 if (repo)
440 got_repo_close(repo);
441 free(path_got);
442 free(path_lock);
443 free(base_commit_id_str);
444 free(uuidstr);
445 free(formatstr);
446 if (err) {
447 if (fd != -1)
448 close(fd);
449 if (*worktree != NULL)
450 got_worktree_close(*worktree);
451 *worktree = NULL;
452 } else
453 (*worktree)->lockfd = fd;
455 return err;
458 const struct got_error *
459 got_worktree_open(struct got_worktree **worktree, const char *path)
461 const struct got_error *err = NULL;
462 char *worktree_path;
464 worktree_path = strdup(path);
465 if (worktree_path == NULL)
466 return got_error_from_errno("strdup");
468 for (;;) {
469 char *parent_path;
471 err = open_worktree(worktree, worktree_path);
472 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
473 free(worktree_path);
474 return err;
476 if (*worktree) {
477 free(worktree_path);
478 return NULL;
480 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
481 break;
482 err = got_path_dirname(&parent_path, worktree_path);
483 if (err) {
484 if (err->code != GOT_ERR_BAD_PATH) {
485 free(worktree_path);
486 return err;
488 break;
490 free(worktree_path);
491 worktree_path = parent_path;
494 free(worktree_path);
495 return got_error(GOT_ERR_NOT_WORKTREE);
498 const struct got_error *
499 got_worktree_close(struct got_worktree *worktree)
501 const struct got_error *err = NULL;
503 if (worktree->lockfd != -1) {
504 if (close(worktree->lockfd) == -1)
505 err = got_error_from_errno2("close",
506 got_worktree_get_root_path(worktree));
508 if (close(worktree->root_fd) == -1 && err == NULL)
509 err = got_error_from_errno2("close",
510 got_worktree_get_root_path(worktree));
511 free(worktree->repo_path);
512 free(worktree->path_prefix);
513 free(worktree->base_commit_id);
514 free(worktree->head_ref_name);
515 free(worktree->root_path);
516 free(worktree->gotconfig_path);
517 got_gotconfig_free(worktree->gotconfig);
518 free(worktree);
519 return err;
522 const char *
523 got_worktree_get_root_path(struct got_worktree *worktree)
525 return worktree->root_path;
528 const char *
529 got_worktree_get_repo_path(struct got_worktree *worktree)
531 return worktree->repo_path;
533 const char *
534 got_worktree_get_path_prefix(struct got_worktree *worktree)
536 return worktree->path_prefix;
539 const struct got_error *
540 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
541 const char *path_prefix)
543 char *absprefix = NULL;
545 if (!got_path_is_absolute(path_prefix)) {
546 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
547 return got_error_from_errno("asprintf");
549 *match = (strcmp(absprefix ? absprefix : path_prefix,
550 worktree->path_prefix) == 0);
551 free(absprefix);
552 return NULL;
555 const char *
556 got_worktree_get_head_ref_name(struct got_worktree *worktree)
558 return worktree->head_ref_name;
561 const struct got_error *
562 got_worktree_set_head_ref(struct got_worktree *worktree,
563 struct got_reference *head_ref)
565 const struct got_error *err = NULL;
566 char *path_got = NULL, *head_ref_name = NULL;
568 if (asprintf(&path_got, "%s/%s", worktree->root_path,
569 GOT_WORKTREE_GOT_DIR) == -1) {
570 err = got_error_from_errno("asprintf");
571 path_got = NULL;
572 goto done;
575 head_ref_name = strdup(got_ref_get_name(head_ref));
576 if (head_ref_name == NULL) {
577 err = got_error_from_errno("strdup");
578 goto done;
581 err = write_head_ref(path_got, head_ref);
582 if (err)
583 goto done;
585 free(worktree->head_ref_name);
586 worktree->head_ref_name = head_ref_name;
587 done:
588 free(path_got);
589 if (err)
590 free(head_ref_name);
591 return err;
594 struct got_object_id *
595 got_worktree_get_base_commit_id(struct got_worktree *worktree)
597 return worktree->base_commit_id;
600 const struct got_error *
601 got_worktree_set_base_commit_id(struct got_worktree *worktree,
602 struct got_repository *repo, struct got_object_id *commit_id)
604 const struct got_error *err;
605 struct got_object *obj = NULL;
606 char *id_str = NULL;
607 char *path_got = NULL;
609 if (asprintf(&path_got, "%s/%s", worktree->root_path,
610 GOT_WORKTREE_GOT_DIR) == -1) {
611 err = got_error_from_errno("asprintf");
612 path_got = NULL;
613 goto done;
616 err = got_object_open(&obj, repo, commit_id);
617 if (err)
618 return err;
620 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
621 err = got_error(GOT_ERR_OBJ_TYPE);
622 goto done;
625 /* Record our base commit. */
626 err = got_object_id_str(&id_str, commit_id);
627 if (err)
628 goto done;
629 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
630 if (err)
631 goto done;
633 free(worktree->base_commit_id);
634 worktree->base_commit_id = got_object_id_dup(commit_id);
635 if (worktree->base_commit_id == NULL) {
636 err = got_error_from_errno("got_object_id_dup");
637 goto done;
639 done:
640 if (obj)
641 got_object_close(obj);
642 free(id_str);
643 free(path_got);
644 return err;
647 const struct got_gotconfig *
648 got_worktree_get_gotconfig(struct got_worktree *worktree)
650 return worktree->gotconfig;
653 static const struct got_error *
654 lock_worktree(struct got_worktree *worktree, int operation)
656 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
657 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
658 : got_error_from_errno2("flock",
659 got_worktree_get_root_path(worktree)));
660 return NULL;
663 static const struct got_error *
664 add_dir_on_disk(struct got_worktree *worktree, const char *path)
666 const struct got_error *err = NULL;
667 char *abspath;
669 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
670 return got_error_from_errno("asprintf");
672 err = got_path_mkdir(abspath);
673 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
674 struct stat sb;
675 err = NULL;
676 if (lstat(abspath, &sb) == -1) {
677 err = got_error_from_errno2("lstat", abspath);
678 } else if (!S_ISDIR(sb.st_mode)) {
679 /* TODO directory is obstructed; do something */
680 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
683 free(abspath);
684 return err;
687 static const struct got_error *
688 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
690 const struct got_error *err = NULL;
691 uint8_t fbuf1[8192];
692 uint8_t fbuf2[8192];
693 size_t flen1 = 0, flen2 = 0;
695 *same = 1;
697 for (;;) {
698 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
699 if (flen1 == 0 && ferror(f1)) {
700 err = got_error_from_errno("fread");
701 break;
703 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
704 if (flen2 == 0 && ferror(f2)) {
705 err = got_error_from_errno("fread");
706 break;
708 if (flen1 == 0) {
709 if (flen2 != 0)
710 *same = 0;
711 break;
712 } else if (flen2 == 0) {
713 if (flen1 != 0)
714 *same = 0;
715 break;
716 } else if (flen1 == flen2) {
717 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
718 *same = 0;
719 break;
721 } else {
722 *same = 0;
723 break;
727 return err;
730 static const struct got_error *
731 check_files_equal(int *same, const char *f1_path, const char *f2_path)
733 const struct got_error *err = NULL;
734 struct stat sb;
735 size_t size1, size2;
736 FILE *f1 = NULL, *f2 = NULL;
738 *same = 1;
740 if (lstat(f1_path, &sb) != 0) {
741 err = got_error_from_errno2("lstat", f1_path);
742 goto done;
744 size1 = sb.st_size;
746 if (lstat(f2_path, &sb) != 0) {
747 err = got_error_from_errno2("lstat", f2_path);
748 goto done;
750 size2 = sb.st_size;
752 if (size1 != size2) {
753 *same = 0;
754 return NULL;
757 f1 = fopen(f1_path, "r");
758 if (f1 == NULL)
759 return got_error_from_errno2("fopen", f1_path);
761 f2 = fopen(f2_path, "r");
762 if (f2 == NULL) {
763 err = got_error_from_errno2("fopen", f2_path);
764 goto done;
767 err = check_file_contents_equal(same, f1, f2);
768 done:
769 if (f1 && fclose(f1) == EOF && err == NULL)
770 err = got_error_from_errno("fclose");
771 if (f2 && fclose(f2) == EOF && err == NULL)
772 err = got_error_from_errno("fclose");
774 return err;
777 /*
778 * Perform a 3-way merge where the file at orig_path acts as the common
779 * ancestor, the file at deriv_path acts as the first derived version,
780 * and the file at ondisk_path acts as the second derived version.
781 */
782 static const struct got_error *
783 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
784 const char *orig_path, const char *ondisk_path,
785 const char *path, uint16_t st_mode, const char *deriv_path,
786 const char *label_orig, const char *label_deriv,
787 struct got_repository *repo,
788 got_worktree_checkout_cb progress_cb, void *progress_arg)
790 const struct got_error *err = NULL;
791 int merged_fd = -1;
792 FILE *f_empty = NULL;
793 char *f_empty_path = NULL;
794 char *merged_path = NULL, *base_path = NULL;
795 int overlapcnt = 0;
796 char *parent = NULL;
797 char *symlink_path = NULL;
798 FILE *symlinkf = NULL;
800 *local_changes_subsumed = 0;
802 err = got_path_dirname(&parent, ondisk_path);
803 if (err)
804 return err;
806 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
807 err = got_error_from_errno("asprintf");
808 goto done;
811 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
812 if (err)
813 goto done;
815 free(base_path);
816 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
817 err = got_error_from_errno("asprintf");
818 base_path = NULL;
819 goto done;
822 if (orig_path == NULL) {
823 /*
824 * If the file has no blob, this is an "add vs add" conflict,
825 * and we simply use an empty ancestor file to make both files
826 * appear in the merged result in their entirety.
827 */
828 err = got_opentemp_named(&f_empty_path, &f_empty, base_path);
829 if (err)
830 goto done;
833 /*
834 * In order the run a 3-way merge with a symlink we copy the symlink's
835 * target path into a temporary file and use that file with diff3.
836 */
837 if (S_ISLNK(st_mode)) {
838 char target_path[PATH_MAX];
839 ssize_t target_len;
840 size_t n;
842 free(base_path);
843 if (asprintf(&base_path, "%s/got-symlink-merge",
844 parent) == -1) {
845 err = got_error_from_errno("asprintf");
846 base_path = NULL;
847 goto done;
849 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
850 if (err)
851 goto done;
852 target_len = readlink(ondisk_path, target_path,
853 sizeof(target_path));
854 if (target_len == -1) {
855 err = got_error_from_errno2("readlink", ondisk_path);
856 goto done;
858 n = fwrite(target_path, 1, target_len, symlinkf);
859 if (n != target_len) {
860 err = got_ferror(symlinkf, GOT_ERR_IO);
861 goto done;
863 if (fflush(symlinkf) == EOF) {
864 err = got_error_from_errno2("fflush", symlink_path);
865 goto done;
869 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
870 orig_path ? orig_path : f_empty_path,
871 symlink_path ? symlink_path : ondisk_path,
872 label_deriv, label_orig, NULL);
873 if (err)
874 goto done;
876 err = (*progress_cb)(progress_arg,
877 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
878 if (err)
879 goto done;
881 if (fsync(merged_fd) != 0) {
882 err = got_error_from_errno("fsync");
883 goto done;
886 /* Check if a clean merge has subsumed all local changes. */
887 if (overlapcnt == 0) {
888 err = check_files_equal(local_changes_subsumed, deriv_path,
889 merged_path);
890 if (err)
891 goto done;
894 if (fchmod(merged_fd, st_mode) != 0) {
895 err = got_error_from_errno2("fchmod", merged_path);
896 goto done;
899 if (rename(merged_path, ondisk_path) != 0) {
900 err = got_error_from_errno3("rename", merged_path,
901 ondisk_path);
902 goto done;
904 done:
905 if (err) {
906 if (merged_path)
907 unlink(merged_path);
909 if (symlink_path) {
910 if (unlink(symlink_path) == -1 && err == NULL)
911 err = got_error_from_errno2("unlink", symlink_path);
913 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
914 err = got_error_from_errno2("fclose", symlink_path);
915 free(symlink_path);
916 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
917 err = got_error_from_errno("close");
918 if (f_empty && fclose(f_empty) == EOF && err == NULL)
919 err = got_error_from_errno("fclose");
920 free(merged_path);
921 free(base_path);
922 if (f_empty_path) {
923 if (unlink(f_empty_path) == -1 && err == NULL)
924 err = got_error_from_errno2("unlink", f_empty_path);
925 free(f_empty_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_orig = NULL, *f_deriv = NULL;
1135 char *blob_orig_path = NULL;
1136 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1137 char *label_deriv = NULL, *parent = NULL;
1139 *local_changes_subsumed = 0;
1141 err = got_path_dirname(&parent, ondisk_path);
1142 if (err)
1143 return err;
1145 if (blob_orig) {
1146 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1147 parent) == -1) {
1148 err = got_error_from_errno("asprintf");
1149 base_path = NULL;
1150 goto done;
1153 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
1154 if (err)
1155 goto done;
1156 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1157 blob_orig);
1158 if (err)
1159 goto done;
1161 free(base_path);
1164 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1165 err = got_error_from_errno("asprintf");
1166 base_path = NULL;
1167 goto done;
1170 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1171 if (err)
1172 goto done;
1173 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1174 blob_deriv);
1175 if (err)
1176 goto done;
1178 err = got_object_id_str(&id_str, deriv_base_commit_id);
1179 if (err)
1180 goto done;
1181 if (asprintf(&label_deriv, "%s: commit %s",
1182 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1183 err = got_error_from_errno("asprintf");
1184 goto done;
1187 err = merge_file(local_changes_subsumed, worktree, blob_orig_path,
1188 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1189 label_deriv, repo, progress_cb, progress_arg);
1190 done:
1191 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1192 err = got_error_from_errno("fclose");
1193 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1194 err = got_error_from_errno("fclose");
1195 free(base_path);
1196 if (blob_orig_path) {
1197 unlink(blob_orig_path);
1198 free(blob_orig_path);
1200 if (blob_deriv_path) {
1201 unlink(blob_deriv_path);
1202 free(blob_deriv_path);
1204 free(id_str);
1205 free(label_deriv);
1206 free(parent);
1207 return err;
1210 static const struct got_error *
1211 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1212 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1213 int wt_fd, const char *path, struct got_object_id *blob_id)
1215 const struct got_error *err = NULL;
1216 struct got_fileindex_entry *new_ie;
1218 *new_iep = NULL;
1220 err = got_fileindex_entry_alloc(&new_ie, path);
1221 if (err)
1222 return err;
1224 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1225 blob_id->sha1, base_commit_id->sha1, 1);
1226 if (err)
1227 goto done;
1229 err = got_fileindex_entry_add(fileindex, new_ie);
1230 done:
1231 if (err)
1232 got_fileindex_entry_free(new_ie);
1233 else
1234 *new_iep = new_ie;
1235 return err;
1238 static mode_t
1239 get_ondisk_perms(int executable, mode_t st_mode)
1241 mode_t xbits = S_IXUSR;
1243 if (executable) {
1244 /* Map read bits to execute bits. */
1245 if (st_mode & S_IRGRP)
1246 xbits |= S_IXGRP;
1247 if (st_mode & S_IROTH)
1248 xbits |= S_IXOTH;
1249 return st_mode | xbits;
1252 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1255 /* forward declaration */
1256 static const struct got_error *
1257 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1258 const char *path, mode_t te_mode, mode_t st_mode,
1259 struct got_blob_object *blob, int restoring_missing_file,
1260 int reverting_versioned_file, int installing_bad_symlink,
1261 int path_is_unversioned, struct got_repository *repo,
1262 got_worktree_checkout_cb progress_cb, void *progress_arg);
1265 * This function assumes that the provided symlink target points at a
1266 * safe location in the work tree!
1268 static const struct got_error *
1269 replace_existing_symlink(int *did_something, const char *ondisk_path,
1270 const char *target_path, size_t target_len)
1272 const struct got_error *err = NULL;
1273 ssize_t elen;
1274 char etarget[PATH_MAX];
1275 int fd;
1277 *did_something = 0;
1280 * "Bad" symlinks (those pointing outside the work tree or into the
1281 * .got directory) are installed in the work tree as a regular file
1282 * which contains the bad symlink target path.
1283 * The new symlink target has already been checked for safety by our
1284 * caller. If we can successfully open a regular file then we simply
1285 * replace this file with a symlink below.
1287 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1288 if (fd == -1) {
1289 if (errno != ELOOP)
1290 return got_error_from_errno2("open", ondisk_path);
1292 /* We are updating an existing on-disk symlink. */
1293 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1294 if (elen == -1)
1295 return got_error_from_errno2("readlink", ondisk_path);
1297 if (elen == target_len &&
1298 memcmp(etarget, target_path, target_len) == 0)
1299 return NULL; /* nothing to do */
1302 *did_something = 1;
1303 err = update_symlink(ondisk_path, target_path, target_len);
1304 if (fd != -1 && close(fd) == -1 && err == NULL)
1305 err = got_error_from_errno2("close", ondisk_path);
1306 return err;
1309 static const struct got_error *
1310 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1311 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1313 const struct got_error *err = NULL;
1314 char canonpath[PATH_MAX];
1315 char *path_got = NULL;
1317 *is_bad_symlink = 0;
1319 if (target_len >= sizeof(canonpath)) {
1320 *is_bad_symlink = 1;
1321 return NULL;
1325 * We do not use realpath(3) to resolve the symlink's target
1326 * path because we don't want to resolve symlinks recursively.
1327 * Instead we make the path absolute and then canonicalize it.
1328 * Relative symlink target lookup should begin at the directory
1329 * in which the blob object is being installed.
1331 if (!got_path_is_absolute(target_path)) {
1332 char *abspath, *parent;
1333 err = got_path_dirname(&parent, ondisk_path);
1334 if (err)
1335 return err;
1336 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1337 free(parent);
1338 return got_error_from_errno("asprintf");
1340 free(parent);
1341 if (strlen(abspath) >= sizeof(canonpath)) {
1342 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1343 free(abspath);
1344 return err;
1346 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1347 free(abspath);
1348 if (err)
1349 return err;
1350 } else {
1351 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1352 if (err)
1353 return err;
1356 /* Only allow symlinks pointing at paths within the work tree. */
1357 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1358 *is_bad_symlink = 1;
1359 return NULL;
1362 /* Do not allow symlinks pointing into the .got directory. */
1363 if (asprintf(&path_got, "%s/%s", wtroot_path,
1364 GOT_WORKTREE_GOT_DIR) == -1)
1365 return got_error_from_errno("asprintf");
1366 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1367 *is_bad_symlink = 1;
1369 free(path_got);
1370 return NULL;
1373 static const struct got_error *
1374 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1375 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1376 int restoring_missing_file, int reverting_versioned_file,
1377 int path_is_unversioned, struct got_repository *repo,
1378 got_worktree_checkout_cb progress_cb, void *progress_arg)
1380 const struct got_error *err = NULL;
1381 char target_path[PATH_MAX];
1382 size_t len, target_len = 0;
1383 char *path_got = NULL;
1384 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1385 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1387 *is_bad_symlink = 0;
1390 * Blob object content specifies the target path of the link.
1391 * If a symbolic link cannot be installed we instead create
1392 * a regular file which contains the link target path stored
1393 * in the blob object.
1395 do {
1396 err = got_object_blob_read_block(&len, blob);
1397 if (len + target_len >= sizeof(target_path)) {
1398 /* Path too long; install as a regular file. */
1399 *is_bad_symlink = 1;
1400 got_object_blob_rewind(blob);
1401 return install_blob(worktree, ondisk_path, path,
1402 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1403 restoring_missing_file, reverting_versioned_file,
1404 1, path_is_unversioned, repo, progress_cb,
1405 progress_arg);
1407 if (len > 0) {
1408 /* Skip blob object header first time around. */
1409 memcpy(target_path + target_len, buf + hdrlen,
1410 len - hdrlen);
1411 target_len += len - hdrlen;
1412 hdrlen = 0;
1414 } while (len != 0);
1415 target_path[target_len] = '\0';
1417 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1418 ondisk_path, worktree->root_path);
1419 if (err)
1420 return err;
1422 if (*is_bad_symlink) {
1423 /* install as a regular file */
1424 got_object_blob_rewind(blob);
1425 err = install_blob(worktree, ondisk_path, path,
1426 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1427 restoring_missing_file, reverting_versioned_file, 1,
1428 path_is_unversioned, repo, progress_cb, progress_arg);
1429 goto done;
1432 if (symlink(target_path, ondisk_path) == -1) {
1433 if (errno == EEXIST) {
1434 int symlink_replaced;
1435 if (path_is_unversioned) {
1436 err = (*progress_cb)(progress_arg,
1437 GOT_STATUS_UNVERSIONED, path);
1438 goto done;
1440 err = replace_existing_symlink(&symlink_replaced,
1441 ondisk_path, target_path, target_len);
1442 if (err)
1443 goto done;
1444 if (progress_cb) {
1445 if (symlink_replaced) {
1446 err = (*progress_cb)(progress_arg,
1447 reverting_versioned_file ?
1448 GOT_STATUS_REVERT :
1449 GOT_STATUS_UPDATE, path);
1450 } else {
1451 err = (*progress_cb)(progress_arg,
1452 GOT_STATUS_EXISTS, path);
1455 goto done; /* Nothing else to do. */
1458 if (errno == ENOENT) {
1459 char *parent;
1460 err = got_path_dirname(&parent, ondisk_path);
1461 if (err)
1462 goto done;
1463 err = add_dir_on_disk(worktree, parent);
1464 free(parent);
1465 if (err)
1466 goto done;
1468 * Retry, and fall through to error handling
1469 * below if this second attempt fails.
1471 if (symlink(target_path, ondisk_path) != -1) {
1472 err = NULL; /* success */
1473 goto done;
1477 /* Handle errors from first or second creation attempt. */
1478 if (errno == ENAMETOOLONG) {
1479 /* bad target path; install as a regular file */
1480 *is_bad_symlink = 1;
1481 got_object_blob_rewind(blob);
1482 err = install_blob(worktree, ondisk_path, path,
1483 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1484 restoring_missing_file, reverting_versioned_file, 1,
1485 path_is_unversioned, repo,
1486 progress_cb, progress_arg);
1487 } else if (errno == ENOTDIR) {
1488 err = got_error_path(ondisk_path,
1489 GOT_ERR_FILE_OBSTRUCTED);
1490 } else {
1491 err = got_error_from_errno3("symlink",
1492 target_path, ondisk_path);
1494 } else if (progress_cb)
1495 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1496 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1497 done:
1498 free(path_got);
1499 return err;
1502 static const struct got_error *
1503 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1504 const char *path, mode_t te_mode, mode_t st_mode,
1505 struct got_blob_object *blob, int restoring_missing_file,
1506 int reverting_versioned_file, int installing_bad_symlink,
1507 int path_is_unversioned, struct got_repository *repo,
1508 got_worktree_checkout_cb progress_cb, void *progress_arg)
1510 const struct got_error *err = NULL;
1511 int fd = -1;
1512 size_t len, hdrlen;
1513 int update = 0;
1514 char *tmppath = NULL;
1516 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1517 GOT_DEFAULT_FILE_MODE);
1518 if (fd == -1) {
1519 if (errno == ENOENT) {
1520 char *parent;
1521 err = got_path_dirname(&parent, path);
1522 if (err)
1523 return err;
1524 err = add_dir_on_disk(worktree, parent);
1525 free(parent);
1526 if (err)
1527 return err;
1528 fd = open(ondisk_path,
1529 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1530 GOT_DEFAULT_FILE_MODE);
1531 if (fd == -1)
1532 return got_error_from_errno2("open",
1533 ondisk_path);
1534 } else if (errno == EEXIST) {
1535 if (path_is_unversioned) {
1536 err = (*progress_cb)(progress_arg,
1537 GOT_STATUS_UNVERSIONED, path);
1538 goto done;
1540 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1541 !S_ISREG(st_mode) && !installing_bad_symlink) {
1542 /* TODO file is obstructed; do something */
1543 err = got_error_path(ondisk_path,
1544 GOT_ERR_FILE_OBSTRUCTED);
1545 goto done;
1546 } else {
1547 err = got_opentemp_named_fd(&tmppath, &fd,
1548 ondisk_path);
1549 if (err)
1550 goto done;
1551 update = 1;
1553 } else
1554 return got_error_from_errno2("open", ondisk_path);
1557 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1558 err = got_error_from_errno2("fchmod",
1559 update ? tmppath : ondisk_path);
1560 goto done;
1563 if (progress_cb) {
1564 if (restoring_missing_file)
1565 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1566 path);
1567 else if (reverting_versioned_file)
1568 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1569 path);
1570 else
1571 err = (*progress_cb)(progress_arg,
1572 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1573 if (err)
1574 goto done;
1577 hdrlen = got_object_blob_get_hdrlen(blob);
1578 do {
1579 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1580 err = got_object_blob_read_block(&len, blob);
1581 if (err)
1582 break;
1583 if (len > 0) {
1584 /* Skip blob object header first time around. */
1585 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1586 if (outlen == -1) {
1587 err = got_error_from_errno("write");
1588 goto done;
1589 } else if (outlen != len - hdrlen) {
1590 err = got_error(GOT_ERR_IO);
1591 goto done;
1593 hdrlen = 0;
1595 } while (len != 0);
1597 if (fsync(fd) != 0) {
1598 err = got_error_from_errno("fsync");
1599 goto done;
1602 if (update) {
1603 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1604 err = got_error_from_errno2("unlink", ondisk_path);
1605 goto done;
1607 if (rename(tmppath, ondisk_path) != 0) {
1608 err = got_error_from_errno3("rename", tmppath,
1609 ondisk_path);
1610 goto done;
1612 free(tmppath);
1613 tmppath = NULL;
1616 done:
1617 if (fd != -1 && close(fd) == -1 && err == NULL)
1618 err = got_error_from_errno("close");
1619 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1620 err = got_error_from_errno2("unlink", tmppath);
1621 free(tmppath);
1622 return err;
1625 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1626 static const struct got_error *
1627 get_modified_file_content_status(unsigned char *status, FILE *f)
1629 const struct got_error *err = NULL;
1630 const char *markers[3] = {
1631 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1632 GOT_DIFF_CONFLICT_MARKER_SEP,
1633 GOT_DIFF_CONFLICT_MARKER_END
1635 int i = 0;
1636 char *line = NULL;
1637 size_t linesize = 0;
1638 ssize_t linelen;
1640 while (*status == GOT_STATUS_MODIFY) {
1641 linelen = getline(&line, &linesize, f);
1642 if (linelen == -1) {
1643 if (feof(f))
1644 break;
1645 err = got_ferror(f, GOT_ERR_IO);
1646 break;
1649 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1650 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1651 == 0)
1652 *status = GOT_STATUS_CONFLICT;
1653 else
1654 i++;
1657 free(line);
1659 return err;
1662 static int
1663 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1665 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1666 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1669 static int
1670 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1672 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1673 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1674 ie->mtime_sec == sb->st_mtim.tv_sec &&
1675 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1676 ie->size == (sb->st_size & 0xffffffff) &&
1677 !xbit_differs(ie, sb->st_mode));
1680 static unsigned char
1681 get_staged_status(struct got_fileindex_entry *ie)
1683 switch (got_fileindex_entry_stage_get(ie)) {
1684 case GOT_FILEIDX_STAGE_ADD:
1685 return GOT_STATUS_ADD;
1686 case GOT_FILEIDX_STAGE_DELETE:
1687 return GOT_STATUS_DELETE;
1688 case GOT_FILEIDX_STAGE_MODIFY:
1689 return GOT_STATUS_MODIFY;
1690 default:
1691 return GOT_STATUS_NO_CHANGE;
1695 static const struct got_error *
1696 get_symlink_modification_status(unsigned char *status,
1697 struct got_fileindex_entry *ie, const char *abspath,
1698 int dirfd, const char *de_name, struct got_blob_object *blob)
1700 const struct got_error *err = NULL;
1701 char target_path[PATH_MAX];
1702 char etarget[PATH_MAX];
1703 ssize_t elen;
1704 size_t len, target_len = 0;
1705 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1706 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1708 *status = GOT_STATUS_NO_CHANGE;
1710 /* Blob object content specifies the target path of the link. */
1711 do {
1712 err = got_object_blob_read_block(&len, blob);
1713 if (err)
1714 return err;
1715 if (len + target_len >= sizeof(target_path)) {
1717 * Should not happen. The blob contents were OK
1718 * when this symlink was installed.
1720 return got_error(GOT_ERR_NO_SPACE);
1722 if (len > 0) {
1723 /* Skip blob object header first time around. */
1724 memcpy(target_path + target_len, buf + hdrlen,
1725 len - hdrlen);
1726 target_len += len - hdrlen;
1727 hdrlen = 0;
1729 } while (len != 0);
1730 target_path[target_len] = '\0';
1732 if (dirfd != -1) {
1733 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1734 if (elen == -1)
1735 return got_error_from_errno2("readlinkat", abspath);
1736 } else {
1737 elen = readlink(abspath, etarget, sizeof(etarget));
1738 if (elen == -1)
1739 return got_error_from_errno2("readlink", abspath);
1742 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1743 *status = GOT_STATUS_MODIFY;
1745 return NULL;
1748 static const struct got_error *
1749 get_file_status(unsigned char *status, struct stat *sb,
1750 struct got_fileindex_entry *ie, const char *abspath,
1751 int dirfd, const char *de_name, struct got_repository *repo)
1753 const struct got_error *err = NULL;
1754 struct got_object_id id;
1755 size_t hdrlen;
1756 int fd = -1;
1757 FILE *f = NULL;
1758 uint8_t fbuf[8192];
1759 struct got_blob_object *blob = NULL;
1760 size_t flen, blen;
1761 unsigned char staged_status = get_staged_status(ie);
1763 *status = GOT_STATUS_NO_CHANGE;
1764 memset(sb, 0, sizeof(*sb));
1767 * Whenever the caller provides a directory descriptor and a
1768 * directory entry name for the file, use them! This prevents
1769 * race conditions if filesystem paths change beneath our feet.
1771 if (dirfd != -1) {
1772 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1773 if (errno == ENOENT) {
1774 if (got_fileindex_entry_has_file_on_disk(ie))
1775 *status = GOT_STATUS_MISSING;
1776 else
1777 *status = GOT_STATUS_DELETE;
1778 goto done;
1780 err = got_error_from_errno2("fstatat", abspath);
1781 goto done;
1783 } else {
1784 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1785 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1786 return got_error_from_errno2("open", abspath);
1787 else if (fd == -1 && errno == ELOOP) {
1788 if (lstat(abspath, sb) == -1)
1789 return got_error_from_errno2("lstat", abspath);
1790 } else if (fd == -1 || fstat(fd, sb) == -1) {
1791 if (errno == ENOENT) {
1792 if (got_fileindex_entry_has_file_on_disk(ie))
1793 *status = GOT_STATUS_MISSING;
1794 else
1795 *status = GOT_STATUS_DELETE;
1796 goto done;
1798 err = got_error_from_errno2("fstat", abspath);
1799 goto done;
1803 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1804 *status = GOT_STATUS_OBSTRUCTED;
1805 goto done;
1808 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1809 *status = GOT_STATUS_DELETE;
1810 goto done;
1811 } else if (!got_fileindex_entry_has_blob(ie) &&
1812 staged_status != GOT_STATUS_ADD) {
1813 *status = GOT_STATUS_ADD;
1814 goto done;
1817 if (!stat_info_differs(ie, sb))
1818 goto done;
1820 if (S_ISLNK(sb->st_mode) &&
1821 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1822 *status = GOT_STATUS_MODIFY;
1823 goto done;
1826 if (staged_status == GOT_STATUS_MODIFY ||
1827 staged_status == GOT_STATUS_ADD)
1828 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1829 else
1830 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1832 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1833 if (err)
1834 goto done;
1836 if (S_ISLNK(sb->st_mode)) {
1837 err = get_symlink_modification_status(status, ie,
1838 abspath, dirfd, de_name, blob);
1839 goto done;
1842 if (dirfd != -1) {
1843 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1844 if (fd == -1) {
1845 err = got_error_from_errno2("openat", abspath);
1846 goto done;
1850 f = fdopen(fd, "r");
1851 if (f == NULL) {
1852 err = got_error_from_errno2("fdopen", abspath);
1853 goto done;
1855 fd = -1;
1856 hdrlen = got_object_blob_get_hdrlen(blob);
1857 for (;;) {
1858 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1859 err = got_object_blob_read_block(&blen, blob);
1860 if (err)
1861 goto done;
1862 /* Skip length of blob object header first time around. */
1863 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1864 if (flen == 0 && ferror(f)) {
1865 err = got_error_from_errno("fread");
1866 goto done;
1868 if (blen - hdrlen == 0) {
1869 if (flen != 0)
1870 *status = GOT_STATUS_MODIFY;
1871 break;
1872 } else if (flen == 0) {
1873 if (blen - hdrlen != 0)
1874 *status = GOT_STATUS_MODIFY;
1875 break;
1876 } else if (blen - hdrlen == flen) {
1877 /* Skip blob object header first time around. */
1878 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1879 *status = GOT_STATUS_MODIFY;
1880 break;
1882 } else {
1883 *status = GOT_STATUS_MODIFY;
1884 break;
1886 hdrlen = 0;
1889 if (*status == GOT_STATUS_MODIFY) {
1890 rewind(f);
1891 err = get_modified_file_content_status(status, f);
1892 } else if (xbit_differs(ie, sb->st_mode))
1893 *status = GOT_STATUS_MODE_CHANGE;
1894 done:
1895 if (blob)
1896 got_object_blob_close(blob);
1897 if (f != NULL && fclose(f) == EOF && err == NULL)
1898 err = got_error_from_errno2("fclose", abspath);
1899 if (fd != -1 && close(fd) == -1 && err == NULL)
1900 err = got_error_from_errno2("close", abspath);
1901 return err;
1905 * Update timestamps in the file index if a file is unmodified and
1906 * we had to run a full content comparison to find out.
1908 static const struct got_error *
1909 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1910 struct got_fileindex_entry *ie, struct stat *sb)
1912 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1913 return got_fileindex_entry_update(ie, wt_fd, path,
1914 ie->blob_sha1, ie->commit_sha1, 1);
1916 return NULL;
1919 static const struct got_error *
1920 update_blob(struct got_worktree *worktree,
1921 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1922 struct got_tree_entry *te, const char *path,
1923 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1924 void *progress_arg)
1926 const struct got_error *err = NULL;
1927 struct got_blob_object *blob = NULL;
1928 char *ondisk_path;
1929 unsigned char status = GOT_STATUS_NO_CHANGE;
1930 struct stat sb;
1932 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1933 return got_error_from_errno("asprintf");
1935 if (ie) {
1936 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1937 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1938 goto done;
1940 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1941 repo);
1942 if (err)
1943 goto done;
1944 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1945 sb.st_mode = got_fileindex_perms_to_st(ie);
1946 } else {
1947 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1948 status = GOT_STATUS_UNVERSIONED;
1951 if (status == GOT_STATUS_OBSTRUCTED) {
1952 err = (*progress_cb)(progress_arg, status, path);
1953 goto done;
1955 if (status == GOT_STATUS_CONFLICT) {
1956 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1957 path);
1958 goto done;
1961 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1962 (S_ISLNK(te->mode) ||
1963 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1965 * This is a regular file or an installed bad symlink.
1966 * If the file index indicates that this file is already
1967 * up-to-date with respect to the repository we can skip
1968 * updating contents of this file.
1970 if (got_fileindex_entry_has_commit(ie) &&
1971 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1972 SHA1_DIGEST_LENGTH) == 0) {
1973 /* Same commit. */
1974 err = sync_timestamps(worktree->root_fd,
1975 path, status, ie, &sb);
1976 if (err)
1977 goto done;
1978 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1979 path);
1980 goto done;
1982 if (got_fileindex_entry_has_blob(ie) &&
1983 memcmp(ie->blob_sha1, te->id.sha1,
1984 SHA1_DIGEST_LENGTH) == 0) {
1985 /* Different commit but the same blob. */
1986 err = sync_timestamps(worktree->root_fd,
1987 path, status, ie, &sb);
1988 if (err)
1989 goto done;
1990 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1991 path);
1992 goto done;
1996 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1997 if (err)
1998 goto done;
2000 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2001 int update_timestamps;
2002 struct got_blob_object *blob2 = NULL;
2003 char *label_orig = NULL;
2004 if (got_fileindex_entry_has_blob(ie)) {
2005 struct got_object_id id2;
2006 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2007 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2008 if (err)
2009 goto done;
2011 if (got_fileindex_entry_has_commit(ie)) {
2012 char id_str[SHA1_DIGEST_STRING_LENGTH];
2013 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2014 sizeof(id_str)) == NULL) {
2015 err = got_error_path(id_str,
2016 GOT_ERR_BAD_OBJ_ID_STR);
2017 goto done;
2019 if (asprintf(&label_orig, "%s: commit %s",
2020 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2021 err = got_error_from_errno("asprintf");
2022 goto done;
2025 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2026 char *link_target;
2027 err = got_object_blob_read_to_str(&link_target, blob);
2028 if (err)
2029 goto done;
2030 err = merge_symlink(worktree, blob2, ondisk_path, path,
2031 label_orig, link_target, worktree->base_commit_id,
2032 repo, progress_cb, progress_arg);
2033 free(link_target);
2034 } else {
2035 err = merge_blob(&update_timestamps, worktree, blob2,
2036 ondisk_path, path, sb.st_mode, label_orig, blob,
2037 worktree->base_commit_id, repo,
2038 progress_cb, progress_arg);
2040 free(label_orig);
2041 if (blob2)
2042 got_object_blob_close(blob2);
2043 if (err)
2044 goto done;
2046 * Do not update timestamps of files with local changes.
2047 * Otherwise, a future status walk would treat them as
2048 * unmodified files again.
2050 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2051 blob->id.sha1, worktree->base_commit_id->sha1,
2052 update_timestamps);
2053 } else if (status == GOT_STATUS_MODE_CHANGE) {
2054 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2055 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2056 } else if (status == GOT_STATUS_DELETE) {
2057 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2058 if (err)
2059 goto done;
2060 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2061 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2062 if (err)
2063 goto done;
2064 } else {
2065 int is_bad_symlink = 0;
2066 if (S_ISLNK(te->mode)) {
2067 err = install_symlink(&is_bad_symlink, worktree,
2068 ondisk_path, path, blob,
2069 status == GOT_STATUS_MISSING, 0,
2070 status == GOT_STATUS_UNVERSIONED, repo,
2071 progress_cb, progress_arg);
2072 } else {
2073 err = install_blob(worktree, ondisk_path, path,
2074 te->mode, sb.st_mode, blob,
2075 status == GOT_STATUS_MISSING, 0, 0,
2076 status == GOT_STATUS_UNVERSIONED, repo,
2077 progress_cb, progress_arg);
2079 if (err)
2080 goto done;
2082 if (ie) {
2083 err = got_fileindex_entry_update(ie,
2084 worktree->root_fd, path, blob->id.sha1,
2085 worktree->base_commit_id->sha1, 1);
2086 } else {
2087 err = create_fileindex_entry(&ie, fileindex,
2088 worktree->base_commit_id, worktree->root_fd, path,
2089 &blob->id);
2091 if (err)
2092 goto done;
2094 if (is_bad_symlink) {
2095 got_fileindex_entry_filetype_set(ie,
2096 GOT_FILEIDX_MODE_BAD_SYMLINK);
2099 got_object_blob_close(blob);
2100 done:
2101 free(ondisk_path);
2102 return err;
2105 static const struct got_error *
2106 remove_ondisk_file(const char *root_path, const char *path)
2108 const struct got_error *err = NULL;
2109 char *ondisk_path = NULL;
2111 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2112 return got_error_from_errno("asprintf");
2114 if (unlink(ondisk_path) == -1) {
2115 if (errno != ENOENT)
2116 err = got_error_from_errno2("unlink", ondisk_path);
2117 } else {
2118 size_t root_len = strlen(root_path);
2119 do {
2120 char *parent;
2121 err = got_path_dirname(&parent, ondisk_path);
2122 if (err)
2123 break;
2124 free(ondisk_path);
2125 ondisk_path = parent;
2126 if (rmdir(ondisk_path) == -1) {
2127 if (errno != ENOTEMPTY)
2128 err = got_error_from_errno2("rmdir",
2129 ondisk_path);
2130 break;
2132 } while (got_path_cmp(ondisk_path, root_path,
2133 strlen(ondisk_path), root_len) != 0);
2135 free(ondisk_path);
2136 return err;
2139 static const struct got_error *
2140 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2141 struct got_fileindex_entry *ie, struct got_repository *repo,
2142 got_worktree_checkout_cb progress_cb, void *progress_arg)
2144 const struct got_error *err = NULL;
2145 unsigned char status;
2146 struct stat sb;
2147 char *ondisk_path;
2149 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2150 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2152 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2153 == -1)
2154 return got_error_from_errno("asprintf");
2156 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2157 if (err)
2158 goto done;
2160 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2161 char ondisk_target[PATH_MAX];
2162 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2163 sizeof(ondisk_target));
2164 if (ondisk_len == -1) {
2165 err = got_error_from_errno2("readlink", ondisk_path);
2166 goto done;
2168 ondisk_target[ondisk_len] = '\0';
2169 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2170 NULL, NULL, /* XXX pass common ancestor info? */
2171 ondisk_target, ondisk_path);
2172 if (err)
2173 goto done;
2174 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2175 ie->path);
2176 goto done;
2179 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2180 status == GOT_STATUS_ADD) {
2181 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2182 if (err)
2183 goto done;
2185 * Preserve the working file and change the deleted blob's
2186 * entry into a schedule-add entry.
2188 err = got_fileindex_entry_update(ie, worktree->root_fd,
2189 ie->path, NULL, NULL, 0);
2190 } else {
2191 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2192 if (err)
2193 goto done;
2194 if (status == GOT_STATUS_NO_CHANGE) {
2195 err = remove_ondisk_file(worktree->root_path, ie->path);
2196 if (err)
2197 goto done;
2199 got_fileindex_entry_remove(fileindex, ie);
2201 done:
2202 free(ondisk_path);
2203 return err;
2206 struct diff_cb_arg {
2207 struct got_fileindex *fileindex;
2208 struct got_worktree *worktree;
2209 struct got_repository *repo;
2210 got_worktree_checkout_cb progress_cb;
2211 void *progress_arg;
2212 got_cancel_cb cancel_cb;
2213 void *cancel_arg;
2216 static const struct got_error *
2217 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2218 struct got_tree_entry *te, const char *parent_path)
2220 struct diff_cb_arg *a = arg;
2222 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2223 return got_error(GOT_ERR_CANCELLED);
2225 return update_blob(a->worktree, a->fileindex, ie, te,
2226 ie->path, a->repo, a->progress_cb, a->progress_arg);
2229 static const struct got_error *
2230 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2232 struct diff_cb_arg *a = arg;
2234 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2235 return got_error(GOT_ERR_CANCELLED);
2237 return delete_blob(a->worktree, a->fileindex, ie,
2238 a->repo, a->progress_cb, a->progress_arg);
2241 static const struct got_error *
2242 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2244 struct diff_cb_arg *a = arg;
2245 const struct got_error *err;
2246 char *path;
2248 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2249 return got_error(GOT_ERR_CANCELLED);
2251 if (got_object_tree_entry_is_submodule(te))
2252 return NULL;
2254 if (asprintf(&path, "%s%s%s", parent_path,
2255 parent_path[0] ? "/" : "", te->name)
2256 == -1)
2257 return got_error_from_errno("asprintf");
2259 if (S_ISDIR(te->mode))
2260 err = add_dir_on_disk(a->worktree, path);
2261 else
2262 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2263 a->repo, a->progress_cb, a->progress_arg);
2265 free(path);
2266 return err;
2269 const struct got_error *
2270 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2272 uint32_t uuid_status;
2274 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2275 if (uuid_status != uuid_s_ok) {
2276 *uuidstr = NULL;
2277 return got_error_uuid(uuid_status, "uuid_to_string");
2280 return NULL;
2283 static const struct got_error *
2284 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2286 const struct got_error *err = NULL;
2287 char *uuidstr = NULL;
2289 *refname = NULL;
2291 err = got_worktree_get_uuid(&uuidstr, worktree);
2292 if (err)
2293 return err;
2295 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2296 err = got_error_from_errno("asprintf");
2297 *refname = NULL;
2299 free(uuidstr);
2300 return err;
2303 const struct got_error *
2304 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2306 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2309 static const struct got_error *
2310 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2312 return get_ref_name(refname, worktree,
2313 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2316 static const struct got_error *
2317 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2319 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2322 static const struct got_error *
2323 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2325 return get_ref_name(refname, worktree,
2326 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2329 static const struct got_error *
2330 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2332 return get_ref_name(refname, worktree,
2333 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2336 static const struct got_error *
2337 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2339 return get_ref_name(refname, worktree,
2340 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2343 static const struct got_error *
2344 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2346 return get_ref_name(refname, worktree,
2347 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2350 static const struct got_error *
2351 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2353 return get_ref_name(refname, worktree,
2354 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2357 static const struct got_error *
2358 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2360 return get_ref_name(refname, worktree,
2361 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2364 const struct got_error *
2365 got_worktree_get_histedit_script_path(char **path,
2366 struct got_worktree *worktree)
2368 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2369 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2370 *path = NULL;
2371 return got_error_from_errno("asprintf");
2373 return NULL;
2377 * Prevent Git's garbage collector from deleting our base commit by
2378 * setting a reference to our base commit's ID.
2380 static const struct got_error *
2381 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2383 const struct got_error *err = NULL;
2384 struct got_reference *ref = NULL;
2385 char *refname;
2387 err = got_worktree_get_base_ref_name(&refname, worktree);
2388 if (err)
2389 return err;
2391 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2392 if (err)
2393 goto done;
2395 err = got_ref_write(ref, repo);
2396 done:
2397 free(refname);
2398 if (ref)
2399 got_ref_close(ref);
2400 return err;
2403 static const struct got_error *
2404 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2406 const struct got_error *err = NULL;
2408 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2409 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2410 err = got_error_from_errno("asprintf");
2411 *fileindex_path = NULL;
2413 return err;
2417 static const struct got_error *
2418 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2419 struct got_worktree *worktree)
2421 const struct got_error *err = NULL;
2422 FILE *index = NULL;
2424 *fileindex_path = NULL;
2425 *fileindex = got_fileindex_alloc();
2426 if (*fileindex == NULL)
2427 return got_error_from_errno("got_fileindex_alloc");
2429 err = get_fileindex_path(fileindex_path, worktree);
2430 if (err)
2431 goto done;
2433 index = fopen(*fileindex_path, "rb");
2434 if (index == NULL) {
2435 if (errno != ENOENT)
2436 err = got_error_from_errno2("fopen", *fileindex_path);
2437 } else {
2438 err = got_fileindex_read(*fileindex, index);
2439 if (fclose(index) == EOF && err == NULL)
2440 err = got_error_from_errno("fclose");
2442 done:
2443 if (err) {
2444 free(*fileindex_path);
2445 *fileindex_path = NULL;
2446 got_fileindex_free(*fileindex);
2447 *fileindex = NULL;
2449 return err;
2452 struct bump_base_commit_id_arg {
2453 struct got_object_id *base_commit_id;
2454 const char *path;
2455 size_t path_len;
2456 const char *entry_name;
2457 got_worktree_checkout_cb progress_cb;
2458 void *progress_arg;
2461 /* Bump base commit ID of all files within an updated part of the work tree. */
2462 static const struct got_error *
2463 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2465 const struct got_error *err;
2466 struct bump_base_commit_id_arg *a = arg;
2468 if (a->entry_name) {
2469 if (strcmp(ie->path, a->path) != 0)
2470 return NULL;
2471 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2472 return NULL;
2474 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2475 SHA1_DIGEST_LENGTH) == 0)
2476 return NULL;
2478 if (a->progress_cb) {
2479 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2480 ie->path);
2481 if (err)
2482 return err;
2484 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2485 return NULL;
2488 static const struct got_error *
2489 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2490 struct got_fileindex *fileindex,
2491 got_worktree_checkout_cb progress_cb, void *progress_arg)
2493 struct bump_base_commit_id_arg bbc_arg;
2495 bbc_arg.base_commit_id = worktree->base_commit_id;
2496 bbc_arg.entry_name = NULL;
2497 bbc_arg.path = "";
2498 bbc_arg.path_len = 0;
2499 bbc_arg.progress_cb = progress_cb;
2500 bbc_arg.progress_arg = progress_arg;
2502 return got_fileindex_for_each_entry_safe(fileindex,
2503 bump_base_commit_id, &bbc_arg);
2506 static const struct got_error *
2507 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2509 const struct got_error *err = NULL;
2510 char *new_fileindex_path = NULL;
2511 FILE *new_index = NULL;
2512 struct timespec timeout;
2514 err = got_opentemp_named(&new_fileindex_path, &new_index,
2515 fileindex_path);
2516 if (err)
2517 goto done;
2519 err = got_fileindex_write(fileindex, new_index);
2520 if (err)
2521 goto done;
2523 if (rename(new_fileindex_path, fileindex_path) != 0) {
2524 err = got_error_from_errno3("rename", new_fileindex_path,
2525 fileindex_path);
2526 unlink(new_fileindex_path);
2530 * Sleep for a short amount of time to ensure that files modified after
2531 * this program exits have a different time stamp from the one which
2532 * was recorded in the file index.
2534 timeout.tv_sec = 0;
2535 timeout.tv_nsec = 1;
2536 nanosleep(&timeout, NULL);
2537 done:
2538 if (new_index)
2539 fclose(new_index);
2540 free(new_fileindex_path);
2541 return err;
2544 static const struct got_error *
2545 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2546 struct got_object_id **tree_id, const char *wt_relpath,
2547 struct got_worktree *worktree, struct got_repository *repo)
2549 const struct got_error *err = NULL;
2550 struct got_object_id *id = NULL;
2551 char *in_repo_path = NULL;
2552 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2554 *entry_type = GOT_OBJ_TYPE_ANY;
2555 *tree_relpath = NULL;
2556 *tree_id = NULL;
2558 if (wt_relpath[0] == '\0') {
2559 /* Check out all files within the work tree. */
2560 *entry_type = GOT_OBJ_TYPE_TREE;
2561 *tree_relpath = strdup("");
2562 if (*tree_relpath == NULL) {
2563 err = got_error_from_errno("strdup");
2564 goto done;
2566 err = got_object_id_by_path(tree_id, repo,
2567 worktree->base_commit_id, worktree->path_prefix);
2568 if (err)
2569 goto done;
2570 return NULL;
2573 /* Check out a subset of files in the work tree. */
2575 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2576 is_root_wt ? "" : "/", wt_relpath) == -1) {
2577 err = got_error_from_errno("asprintf");
2578 goto done;
2581 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2582 in_repo_path);
2583 if (err)
2584 goto done;
2586 free(in_repo_path);
2587 in_repo_path = NULL;
2589 err = got_object_get_type(entry_type, repo, id);
2590 if (err)
2591 goto done;
2593 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2594 /* Check out a single file. */
2595 if (strchr(wt_relpath, '/') == NULL) {
2596 /* Check out a single file in work tree's root dir. */
2597 in_repo_path = strdup(worktree->path_prefix);
2598 if (in_repo_path == NULL) {
2599 err = got_error_from_errno("strdup");
2600 goto done;
2602 *tree_relpath = strdup("");
2603 if (*tree_relpath == NULL) {
2604 err = got_error_from_errno("strdup");
2605 goto done;
2607 } else {
2608 /* Check out a single file in a subdirectory. */
2609 err = got_path_dirname(tree_relpath, wt_relpath);
2610 if (err)
2611 return err;
2612 if (asprintf(&in_repo_path, "%s%s%s",
2613 worktree->path_prefix, is_root_wt ? "" : "/",
2614 *tree_relpath) == -1) {
2615 err = got_error_from_errno("asprintf");
2616 goto done;
2619 err = got_object_id_by_path(tree_id, repo,
2620 worktree->base_commit_id, in_repo_path);
2621 } else {
2622 /* Check out all files within a subdirectory. */
2623 *tree_id = got_object_id_dup(id);
2624 if (*tree_id == NULL) {
2625 err = got_error_from_errno("got_object_id_dup");
2626 goto done;
2628 *tree_relpath = strdup(wt_relpath);
2629 if (*tree_relpath == NULL) {
2630 err = got_error_from_errno("strdup");
2631 goto done;
2634 done:
2635 free(id);
2636 free(in_repo_path);
2637 if (err) {
2638 *entry_type = GOT_OBJ_TYPE_ANY;
2639 free(*tree_relpath);
2640 *tree_relpath = NULL;
2641 free(*tree_id);
2642 *tree_id = NULL;
2644 return err;
2647 static const struct got_error *
2648 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2649 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2650 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2651 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2653 const struct got_error *err = NULL;
2654 struct got_commit_object *commit = NULL;
2655 struct got_tree_object *tree = NULL;
2656 struct got_fileindex_diff_tree_cb diff_cb;
2657 struct diff_cb_arg arg;
2659 err = ref_base_commit(worktree, repo);
2660 if (err) {
2661 if (!(err->code == GOT_ERR_ERRNO &&
2662 (errno == EACCES || errno == EROFS)))
2663 goto done;
2664 err = (*progress_cb)(progress_arg,
2665 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2666 if (err)
2667 return err;
2670 err = got_object_open_as_commit(&commit, repo,
2671 worktree->base_commit_id);
2672 if (err)
2673 goto done;
2675 err = got_object_open_as_tree(&tree, repo, tree_id);
2676 if (err)
2677 goto done;
2679 if (entry_name &&
2680 got_object_tree_find_entry(tree, entry_name) == NULL) {
2681 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2682 goto done;
2685 diff_cb.diff_old_new = diff_old_new;
2686 diff_cb.diff_old = diff_old;
2687 diff_cb.diff_new = diff_new;
2688 arg.fileindex = fileindex;
2689 arg.worktree = worktree;
2690 arg.repo = repo;
2691 arg.progress_cb = progress_cb;
2692 arg.progress_arg = progress_arg;
2693 arg.cancel_cb = cancel_cb;
2694 arg.cancel_arg = cancel_arg;
2695 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2696 entry_name, repo, &diff_cb, &arg);
2697 done:
2698 if (tree)
2699 got_object_tree_close(tree);
2700 if (commit)
2701 got_object_commit_close(commit);
2702 return err;
2705 const struct got_error *
2706 got_worktree_checkout_files(struct got_worktree *worktree,
2707 struct got_pathlist_head *paths, struct got_repository *repo,
2708 got_worktree_checkout_cb progress_cb, void *progress_arg,
2709 got_cancel_cb cancel_cb, void *cancel_arg)
2711 const struct got_error *err = NULL, *sync_err, *unlockerr;
2712 struct got_commit_object *commit = NULL;
2713 struct got_tree_object *tree = NULL;
2714 struct got_fileindex *fileindex = NULL;
2715 char *fileindex_path = NULL;
2716 struct got_pathlist_entry *pe;
2717 struct tree_path_data {
2718 SIMPLEQ_ENTRY(tree_path_data) entry;
2719 struct got_object_id *tree_id;
2720 int entry_type;
2721 char *relpath;
2722 char *entry_name;
2723 } *tpd = NULL;
2724 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2726 SIMPLEQ_INIT(&tree_paths);
2728 err = lock_worktree(worktree, LOCK_EX);
2729 if (err)
2730 return err;
2732 /* Map all specified paths to in-repository trees. */
2733 TAILQ_FOREACH(pe, paths, entry) {
2734 tpd = malloc(sizeof(*tpd));
2735 if (tpd == NULL) {
2736 err = got_error_from_errno("malloc");
2737 goto done;
2740 err = find_tree_entry_for_checkout(&tpd->entry_type,
2741 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2742 if (err) {
2743 free(tpd);
2744 goto done;
2747 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2748 err = got_path_basename(&tpd->entry_name, pe->path);
2749 if (err) {
2750 free(tpd->relpath);
2751 free(tpd->tree_id);
2752 free(tpd);
2753 goto done;
2755 } else
2756 tpd->entry_name = NULL;
2758 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2762 * Read the file index.
2763 * Checking out files is supposed to be an idempotent operation.
2764 * If the on-disk file index is incomplete we will try to complete it.
2766 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2767 if (err)
2768 goto done;
2770 tpd = SIMPLEQ_FIRST(&tree_paths);
2771 TAILQ_FOREACH(pe, paths, entry) {
2772 struct bump_base_commit_id_arg bbc_arg;
2774 err = checkout_files(worktree, fileindex, tpd->relpath,
2775 tpd->tree_id, tpd->entry_name, repo,
2776 progress_cb, progress_arg, cancel_cb, cancel_arg);
2777 if (err)
2778 break;
2780 bbc_arg.base_commit_id = worktree->base_commit_id;
2781 bbc_arg.entry_name = tpd->entry_name;
2782 bbc_arg.path = pe->path;
2783 bbc_arg.path_len = pe->path_len;
2784 bbc_arg.progress_cb = progress_cb;
2785 bbc_arg.progress_arg = progress_arg;
2786 err = got_fileindex_for_each_entry_safe(fileindex,
2787 bump_base_commit_id, &bbc_arg);
2788 if (err)
2789 break;
2791 tpd = SIMPLEQ_NEXT(tpd, entry);
2793 sync_err = sync_fileindex(fileindex, fileindex_path);
2794 if (sync_err && err == NULL)
2795 err = sync_err;
2796 done:
2797 free(fileindex_path);
2798 if (tree)
2799 got_object_tree_close(tree);
2800 if (commit)
2801 got_object_commit_close(commit);
2802 if (fileindex)
2803 got_fileindex_free(fileindex);
2804 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2805 tpd = SIMPLEQ_FIRST(&tree_paths);
2806 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2807 free(tpd->relpath);
2808 free(tpd->tree_id);
2809 free(tpd);
2811 unlockerr = lock_worktree(worktree, LOCK_SH);
2812 if (unlockerr && err == NULL)
2813 err = unlockerr;
2814 return err;
2817 struct merge_file_cb_arg {
2818 struct got_worktree *worktree;
2819 struct got_fileindex *fileindex;
2820 got_worktree_checkout_cb progress_cb;
2821 void *progress_arg;
2822 got_cancel_cb cancel_cb;
2823 void *cancel_arg;
2824 const char *label_orig;
2825 struct got_object_id *commit_id2;
2828 static const struct got_error *
2829 merge_file_cb(void *arg, struct got_blob_object *blob1,
2830 struct got_blob_object *blob2, struct got_object_id *id1,
2831 struct got_object_id *id2, const char *path1, const char *path2,
2832 mode_t mode1, mode_t mode2, struct got_repository *repo)
2834 static const struct got_error *err = NULL;
2835 struct merge_file_cb_arg *a = arg;
2836 struct got_fileindex_entry *ie;
2837 char *ondisk_path = NULL;
2838 struct stat sb;
2839 unsigned char status;
2840 int local_changes_subsumed;
2842 if (blob1 && blob2) {
2843 ie = got_fileindex_entry_get(a->fileindex, path2,
2844 strlen(path2));
2845 if (ie == NULL)
2846 return (*a->progress_cb)(a->progress_arg,
2847 GOT_STATUS_MISSING, path2);
2849 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2850 path2) == -1)
2851 return got_error_from_errno("asprintf");
2853 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2854 repo);
2855 if (err)
2856 goto done;
2858 if (status == GOT_STATUS_DELETE) {
2859 err = (*a->progress_cb)(a->progress_arg,
2860 GOT_STATUS_MERGE, path2);
2861 goto done;
2863 if (status != GOT_STATUS_NO_CHANGE &&
2864 status != GOT_STATUS_MODIFY &&
2865 status != GOT_STATUS_CONFLICT &&
2866 status != GOT_STATUS_ADD) {
2867 err = (*a->progress_cb)(a->progress_arg, status, path2);
2868 goto done;
2871 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2872 char *link_target2;
2873 err = got_object_blob_read_to_str(&link_target2, blob2);
2874 if (err)
2875 goto done;
2876 err = merge_symlink(a->worktree, blob1, ondisk_path,
2877 path2, a->label_orig, link_target2, a->commit_id2,
2878 repo, a->progress_cb, a->progress_arg);
2879 free(link_target2);
2880 } else {
2881 err = merge_blob(&local_changes_subsumed, a->worktree,
2882 blob1, ondisk_path, path2, sb.st_mode,
2883 a->label_orig, blob2, a->commit_id2, repo,
2884 a->progress_cb, a->progress_arg);
2886 } else if (blob1) {
2887 ie = got_fileindex_entry_get(a->fileindex, path1,
2888 strlen(path1));
2889 if (ie == NULL)
2890 return (*a->progress_cb)(a->progress_arg,
2891 GOT_STATUS_MISSING, path1);
2893 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2894 path1) == -1)
2895 return got_error_from_errno("asprintf");
2897 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2898 repo);
2899 if (err)
2900 goto done;
2902 switch (status) {
2903 case GOT_STATUS_NO_CHANGE:
2904 err = (*a->progress_cb)(a->progress_arg,
2905 GOT_STATUS_DELETE, path1);
2906 if (err)
2907 goto done;
2908 err = remove_ondisk_file(a->worktree->root_path, path1);
2909 if (err)
2910 goto done;
2911 if (ie)
2912 got_fileindex_entry_mark_deleted_from_disk(ie);
2913 break;
2914 case GOT_STATUS_DELETE:
2915 case GOT_STATUS_MISSING:
2916 err = (*a->progress_cb)(a->progress_arg,
2917 GOT_STATUS_DELETE, path1);
2918 if (err)
2919 goto done;
2920 if (ie)
2921 got_fileindex_entry_mark_deleted_from_disk(ie);
2922 break;
2923 case GOT_STATUS_ADD: {
2924 struct got_object_id *id;
2925 FILE *blob1_f;
2927 * Delete the added file only if its content already
2928 * exists in the repository.
2930 err = got_object_blob_file_create(&id, &blob1_f, path1);
2931 if (err)
2932 goto done;
2933 if (got_object_id_cmp(id, id1) == 0) {
2934 err = (*a->progress_cb)(a->progress_arg,
2935 GOT_STATUS_DELETE, path1);
2936 if (err)
2937 goto done;
2938 err = remove_ondisk_file(a->worktree->root_path,
2939 path1);
2940 if (err)
2941 goto done;
2942 if (ie)
2943 got_fileindex_entry_remove(a->fileindex,
2944 ie);
2945 } else {
2946 err = (*a->progress_cb)(a->progress_arg,
2947 GOT_STATUS_CANNOT_DELETE, path1);
2949 if (fclose(blob1_f) == EOF && err == NULL)
2950 err = got_error_from_errno("fclose");
2951 free(id);
2952 if (err)
2953 goto done;
2954 break;
2956 case GOT_STATUS_MODIFY:
2957 case GOT_STATUS_CONFLICT:
2958 err = (*a->progress_cb)(a->progress_arg,
2959 GOT_STATUS_CANNOT_DELETE, path1);
2960 if (err)
2961 goto done;
2962 break;
2963 case GOT_STATUS_OBSTRUCTED:
2964 err = (*a->progress_cb)(a->progress_arg, status, path1);
2965 if (err)
2966 goto done;
2967 break;
2968 default:
2969 break;
2971 } else if (blob2) {
2972 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2973 path2) == -1)
2974 return got_error_from_errno("asprintf");
2975 ie = got_fileindex_entry_get(a->fileindex, path2,
2976 strlen(path2));
2977 if (ie) {
2978 err = get_file_status(&status, &sb, ie, ondisk_path,
2979 -1, NULL, repo);
2980 if (err)
2981 goto done;
2982 if (status != GOT_STATUS_NO_CHANGE &&
2983 status != GOT_STATUS_MODIFY &&
2984 status != GOT_STATUS_CONFLICT &&
2985 status != GOT_STATUS_ADD) {
2986 err = (*a->progress_cb)(a->progress_arg,
2987 status, path2);
2988 goto done;
2990 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2991 char *link_target2;
2992 err = got_object_blob_read_to_str(&link_target2,
2993 blob2);
2994 if (err)
2995 goto done;
2996 err = merge_symlink(a->worktree, NULL,
2997 ondisk_path, path2, a->label_orig,
2998 link_target2, a->commit_id2, repo,
2999 a->progress_cb, a->progress_arg);
3000 free(link_target2);
3001 } else if (S_ISREG(sb.st_mode)) {
3002 err = merge_blob(&local_changes_subsumed,
3003 a->worktree, NULL, ondisk_path, path2,
3004 sb.st_mode, a->label_orig, blob2,
3005 a->commit_id2, repo, a->progress_cb,
3006 a->progress_arg);
3007 } else {
3008 err = got_error_path(ondisk_path,
3009 GOT_ERR_FILE_OBSTRUCTED);
3011 if (err)
3012 goto done;
3013 if (status == GOT_STATUS_DELETE) {
3014 err = got_fileindex_entry_update(ie,
3015 a->worktree->root_fd, path2, blob2->id.sha1,
3016 a->worktree->base_commit_id->sha1, 0);
3017 if (err)
3018 goto done;
3020 } else {
3021 int is_bad_symlink = 0;
3022 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3023 if (S_ISLNK(mode2)) {
3024 err = install_symlink(&is_bad_symlink,
3025 a->worktree, ondisk_path, path2, blob2, 0,
3026 0, 1, repo, a->progress_cb, a->progress_arg);
3027 } else {
3028 err = install_blob(a->worktree, ondisk_path, path2,
3029 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3030 a->progress_cb, a->progress_arg);
3032 if (err)
3033 goto done;
3034 err = got_fileindex_entry_alloc(&ie, path2);
3035 if (err)
3036 goto done;
3037 err = got_fileindex_entry_update(ie,
3038 a->worktree->root_fd, path2, NULL, NULL, 1);
3039 if (err) {
3040 got_fileindex_entry_free(ie);
3041 goto done;
3043 err = got_fileindex_entry_add(a->fileindex, ie);
3044 if (err) {
3045 got_fileindex_entry_free(ie);
3046 goto done;
3048 if (is_bad_symlink) {
3049 got_fileindex_entry_filetype_set(ie,
3050 GOT_FILEIDX_MODE_BAD_SYMLINK);
3054 done:
3055 free(ondisk_path);
3056 return err;
3059 struct check_merge_ok_arg {
3060 struct got_worktree *worktree;
3061 struct got_repository *repo;
3064 static const struct got_error *
3065 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3067 const struct got_error *err = NULL;
3068 struct check_merge_ok_arg *a = arg;
3069 unsigned char status;
3070 struct stat sb;
3071 char *ondisk_path;
3073 /* Reject merges into a work tree with mixed base commits. */
3074 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3075 SHA1_DIGEST_LENGTH))
3076 return got_error(GOT_ERR_MIXED_COMMITS);
3078 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3079 == -1)
3080 return got_error_from_errno("asprintf");
3082 /* Reject merges into a work tree with conflicted files. */
3083 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3084 if (err)
3085 return err;
3086 if (status == GOT_STATUS_CONFLICT)
3087 return got_error(GOT_ERR_CONFLICTS);
3089 return NULL;
3092 static const struct got_error *
3093 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3094 const char *fileindex_path, struct got_object_id *commit_id1,
3095 struct got_object_id *commit_id2, struct got_repository *repo,
3096 got_worktree_checkout_cb progress_cb, void *progress_arg,
3097 got_cancel_cb cancel_cb, void *cancel_arg)
3099 const struct got_error *err = NULL, *sync_err;
3100 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3101 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3102 struct merge_file_cb_arg arg;
3103 char *label_orig = NULL;
3105 if (commit_id1) {
3106 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3107 worktree->path_prefix);
3108 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3109 goto done;
3111 if (tree_id1) {
3112 char *id_str;
3114 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3115 if (err)
3116 goto done;
3118 err = got_object_id_str(&id_str, commit_id1);
3119 if (err)
3120 goto done;
3122 if (asprintf(&label_orig, "%s: commit %s",
3123 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3124 err = got_error_from_errno("asprintf");
3125 free(id_str);
3126 goto done;
3128 free(id_str);
3131 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3132 worktree->path_prefix);
3133 if (err)
3134 goto done;
3136 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3137 if (err)
3138 goto done;
3140 arg.worktree = worktree;
3141 arg.fileindex = fileindex;
3142 arg.progress_cb = progress_cb;
3143 arg.progress_arg = progress_arg;
3144 arg.cancel_cb = cancel_cb;
3145 arg.cancel_arg = cancel_arg;
3146 arg.label_orig = label_orig;
3147 arg.commit_id2 = commit_id2;
3148 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3149 sync_err = sync_fileindex(fileindex, fileindex_path);
3150 if (sync_err && err == NULL)
3151 err = sync_err;
3152 done:
3153 if (tree1)
3154 got_object_tree_close(tree1);
3155 if (tree2)
3156 got_object_tree_close(tree2);
3157 free(label_orig);
3158 return err;
3161 const struct got_error *
3162 got_worktree_merge_files(struct got_worktree *worktree,
3163 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3164 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3165 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3167 const struct got_error *err, *unlockerr;
3168 char *fileindex_path = NULL;
3169 struct got_fileindex *fileindex = NULL;
3170 struct check_merge_ok_arg mok_arg;
3172 err = lock_worktree(worktree, LOCK_EX);
3173 if (err)
3174 return err;
3176 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3177 if (err)
3178 goto done;
3180 mok_arg.worktree = worktree;
3181 mok_arg.repo = repo;
3182 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3183 &mok_arg);
3184 if (err)
3185 goto done;
3187 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3188 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3189 done:
3190 if (fileindex)
3191 got_fileindex_free(fileindex);
3192 free(fileindex_path);
3193 unlockerr = lock_worktree(worktree, LOCK_SH);
3194 if (unlockerr && err == NULL)
3195 err = unlockerr;
3196 return err;
3199 struct diff_dir_cb_arg {
3200 struct got_fileindex *fileindex;
3201 struct got_worktree *worktree;
3202 const char *status_path;
3203 size_t status_path_len;
3204 struct got_repository *repo;
3205 got_worktree_status_cb status_cb;
3206 void *status_arg;
3207 got_cancel_cb cancel_cb;
3208 void *cancel_arg;
3209 /* A pathlist containing per-directory pathlists of ignore patterns. */
3210 struct got_pathlist_head ignores;
3211 int report_unchanged;
3212 int no_ignores;
3215 static const struct got_error *
3216 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3217 int dirfd, const char *de_name,
3218 got_worktree_status_cb status_cb, void *status_arg,
3219 struct got_repository *repo, int report_unchanged)
3221 const struct got_error *err = NULL;
3222 unsigned char status = GOT_STATUS_NO_CHANGE;
3223 unsigned char staged_status = get_staged_status(ie);
3224 struct stat sb;
3225 struct got_object_id blob_id, commit_id, staged_blob_id;
3226 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3227 struct got_object_id *staged_blob_idp = NULL;
3229 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3230 if (err)
3231 return err;
3233 if (status == GOT_STATUS_NO_CHANGE &&
3234 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3235 return NULL;
3237 if (got_fileindex_entry_has_blob(ie)) {
3238 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3239 blob_idp = &blob_id;
3241 if (got_fileindex_entry_has_commit(ie)) {
3242 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3243 commit_idp = &commit_id;
3245 if (staged_status == GOT_STATUS_ADD ||
3246 staged_status == GOT_STATUS_MODIFY) {
3247 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3248 SHA1_DIGEST_LENGTH);
3249 staged_blob_idp = &staged_blob_id;
3252 return (*status_cb)(status_arg, status, staged_status,
3253 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3256 static const struct got_error *
3257 status_old_new(void *arg, struct got_fileindex_entry *ie,
3258 struct dirent *de, const char *parent_path, int dirfd)
3260 const struct got_error *err = NULL;
3261 struct diff_dir_cb_arg *a = arg;
3262 char *abspath;
3264 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3265 return got_error(GOT_ERR_CANCELLED);
3267 if (got_path_cmp(parent_path, a->status_path,
3268 strlen(parent_path), a->status_path_len) != 0 &&
3269 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3270 return NULL;
3272 if (parent_path[0]) {
3273 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3274 parent_path, de->d_name) == -1)
3275 return got_error_from_errno("asprintf");
3276 } else {
3277 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3278 de->d_name) == -1)
3279 return got_error_from_errno("asprintf");
3282 err = report_file_status(ie, abspath, dirfd, de->d_name,
3283 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3284 free(abspath);
3285 return err;
3288 static const struct got_error *
3289 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3291 struct diff_dir_cb_arg *a = arg;
3292 struct got_object_id blob_id, commit_id;
3293 unsigned char status;
3295 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3296 return got_error(GOT_ERR_CANCELLED);
3298 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3299 return NULL;
3301 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3302 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3303 if (got_fileindex_entry_has_file_on_disk(ie))
3304 status = GOT_STATUS_MISSING;
3305 else
3306 status = GOT_STATUS_DELETE;
3307 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3308 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3311 void
3312 free_ignorelist(struct got_pathlist_head *ignorelist)
3314 struct got_pathlist_entry *pe;
3316 TAILQ_FOREACH(pe, ignorelist, entry)
3317 free((char *)pe->path);
3318 got_pathlist_free(ignorelist);
3321 void
3322 free_ignores(struct got_pathlist_head *ignores)
3324 struct got_pathlist_entry *pe;
3326 TAILQ_FOREACH(pe, ignores, entry) {
3327 struct got_pathlist_head *ignorelist = pe->data;
3328 free_ignorelist(ignorelist);
3329 free((char *)pe->path);
3331 got_pathlist_free(ignores);
3334 static const struct got_error *
3335 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3337 const struct got_error *err = NULL;
3338 struct got_pathlist_entry *pe = NULL;
3339 struct got_pathlist_head *ignorelist;
3340 char *line = NULL, *pattern, *dirpath = NULL;
3341 size_t linesize = 0;
3342 ssize_t linelen;
3344 ignorelist = calloc(1, sizeof(*ignorelist));
3345 if (ignorelist == NULL)
3346 return got_error_from_errno("calloc");
3347 TAILQ_INIT(ignorelist);
3349 while ((linelen = getline(&line, &linesize, f)) != -1) {
3350 if (linelen > 0 && line[linelen - 1] == '\n')
3351 line[linelen - 1] = '\0';
3353 /* Git's ignores may contain comments. */
3354 if (line[0] == '#')
3355 continue;
3357 /* Git's negated patterns are not (yet?) supported. */
3358 if (line[0] == '!')
3359 continue;
3361 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3362 line) == -1) {
3363 err = got_error_from_errno("asprintf");
3364 goto done;
3366 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3367 if (err)
3368 goto done;
3370 if (ferror(f)) {
3371 err = got_error_from_errno("getline");
3372 goto done;
3375 dirpath = strdup(path);
3376 if (dirpath == NULL) {
3377 err = got_error_from_errno("strdup");
3378 goto done;
3380 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3381 done:
3382 free(line);
3383 if (err || pe == NULL) {
3384 free(dirpath);
3385 free_ignorelist(ignorelist);
3387 return err;
3390 int
3391 match_ignores(struct got_pathlist_head *ignores, const char *path)
3393 struct got_pathlist_entry *pe;
3395 /* Handle patterns which match in all directories. */
3396 TAILQ_FOREACH(pe, ignores, entry) {
3397 struct got_pathlist_head *ignorelist = pe->data;
3398 struct got_pathlist_entry *pi;
3400 TAILQ_FOREACH(pi, ignorelist, entry) {
3401 const char *p, *pattern = pi->path;
3403 if (strncmp(pattern, "**/", 3) != 0)
3404 continue;
3405 pattern += 3;
3406 p = path;
3407 while (*p) {
3408 if (fnmatch(pattern, p,
3409 FNM_PATHNAME | FNM_LEADING_DIR)) {
3410 /* Retry in next directory. */
3411 while (*p && *p != '/')
3412 p++;
3413 while (*p == '/')
3414 p++;
3415 continue;
3417 return 1;
3423 * The ignores pathlist contains ignore lists from children before
3424 * parents, so we can find the most specific ignorelist by walking
3425 * ignores backwards.
3427 pe = TAILQ_LAST(ignores, got_pathlist_head);
3428 while (pe) {
3429 if (got_path_is_child(path, pe->path, pe->path_len)) {
3430 struct got_pathlist_head *ignorelist = pe->data;
3431 struct got_pathlist_entry *pi;
3432 TAILQ_FOREACH(pi, ignorelist, entry) {
3433 const char *pattern = pi->path;
3434 int flags = FNM_LEADING_DIR;
3435 if (strstr(pattern, "/**/") == NULL)
3436 flags |= FNM_PATHNAME;
3437 if (fnmatch(pattern, path, flags))
3438 continue;
3439 return 1;
3442 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3445 return 0;
3448 static const struct got_error *
3449 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3450 const char *path, int dirfd, const char *ignores_filename)
3452 const struct got_error *err = NULL;
3453 char *ignorespath;
3454 int fd = -1;
3455 FILE *ignoresfile = NULL;
3457 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3458 path[0] ? "/" : "", ignores_filename) == -1)
3459 return got_error_from_errno("asprintf");
3461 if (dirfd != -1) {
3462 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3463 if (fd == -1) {
3464 if (errno != ENOENT && errno != EACCES)
3465 err = got_error_from_errno2("openat",
3466 ignorespath);
3467 } else {
3468 ignoresfile = fdopen(fd, "r");
3469 if (ignoresfile == NULL)
3470 err = got_error_from_errno2("fdopen",
3471 ignorespath);
3472 else {
3473 fd = -1;
3474 err = read_ignores(ignores, path, ignoresfile);
3477 } else {
3478 ignoresfile = fopen(ignorespath, "r");
3479 if (ignoresfile == NULL) {
3480 if (errno != ENOENT && errno != EACCES)
3481 err = got_error_from_errno2("fopen",
3482 ignorespath);
3483 } else
3484 err = read_ignores(ignores, path, ignoresfile);
3487 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3488 err = got_error_from_errno2("fclose", path);
3489 if (fd != -1 && close(fd) == -1 && err == NULL)
3490 err = got_error_from_errno2("close", path);
3491 free(ignorespath);
3492 return err;
3495 static const struct got_error *
3496 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3498 const struct got_error *err = NULL;
3499 struct diff_dir_cb_arg *a = arg;
3500 char *path = NULL;
3502 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3503 return got_error(GOT_ERR_CANCELLED);
3505 if (parent_path[0]) {
3506 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3507 return got_error_from_errno("asprintf");
3508 } else {
3509 path = de->d_name;
3512 if (de->d_type != DT_DIR &&
3513 got_path_is_child(path, a->status_path, a->status_path_len)
3514 && !match_ignores(&a->ignores, path))
3515 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3516 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3517 if (parent_path[0])
3518 free(path);
3519 return err;
3522 static const struct got_error *
3523 status_traverse(void *arg, const char *path, int dirfd)
3525 const struct got_error *err = NULL;
3526 struct diff_dir_cb_arg *a = arg;
3528 if (a->no_ignores)
3529 return NULL;
3531 err = add_ignores(&a->ignores, a->worktree->root_path,
3532 path, dirfd, ".cvsignore");
3533 if (err)
3534 return err;
3536 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3537 dirfd, ".gitignore");
3539 return err;
3542 static const struct got_error *
3543 report_single_file_status(const char *path, const char *ondisk_path,
3544 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3545 void *status_arg, struct got_repository *repo, int report_unchanged)
3547 struct got_fileindex_entry *ie;
3548 struct stat sb;
3550 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3551 if (ie)
3552 return report_file_status(ie, ondisk_path, -1, NULL,
3553 status_cb, status_arg, repo, report_unchanged);
3555 if (lstat(ondisk_path, &sb) == -1) {
3556 if (errno != ENOENT)
3557 return got_error_from_errno2("lstat", ondisk_path);
3558 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3559 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3560 return NULL;
3563 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3564 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3565 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3567 return NULL;
3570 static const struct got_error *
3571 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3572 const char *root_path, const char *path)
3574 const struct got_error *err;
3575 char *parent_path, *next_parent_path = NULL;
3577 err = add_ignores(ignores, root_path, "", -1,
3578 ".cvsignore");
3579 if (err)
3580 return err;
3582 err = add_ignores(ignores, root_path, "", -1,
3583 ".gitignore");
3584 if (err)
3585 return err;
3587 err = got_path_dirname(&parent_path, path);
3588 if (err) {
3589 if (err->code == GOT_ERR_BAD_PATH)
3590 return NULL; /* cannot traverse parent */
3591 return err;
3593 for (;;) {
3594 err = add_ignores(ignores, root_path, parent_path, -1,
3595 ".cvsignore");
3596 if (err)
3597 break;
3598 err = add_ignores(ignores, root_path, parent_path, -1,
3599 ".gitignore");
3600 if (err)
3601 break;
3602 err = got_path_dirname(&next_parent_path, parent_path);
3603 if (err) {
3604 if (err->code == GOT_ERR_BAD_PATH)
3605 err = NULL; /* traversed everything */
3606 break;
3608 free(parent_path);
3609 parent_path = next_parent_path;
3610 next_parent_path = NULL;
3613 free(parent_path);
3614 free(next_parent_path);
3615 return err;
3618 static const struct got_error *
3619 worktree_status(struct got_worktree *worktree, const char *path,
3620 struct got_fileindex *fileindex, struct got_repository *repo,
3621 got_worktree_status_cb status_cb, void *status_arg,
3622 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3623 int report_unchanged)
3625 const struct got_error *err = NULL;
3626 int fd = -1;
3627 struct got_fileindex_diff_dir_cb fdiff_cb;
3628 struct diff_dir_cb_arg arg;
3629 char *ondisk_path = NULL;
3631 TAILQ_INIT(&arg.ignores);
3633 if (asprintf(&ondisk_path, "%s%s%s",
3634 worktree->root_path, path[0] ? "/" : "", path) == -1)
3635 return got_error_from_errno("asprintf");
3637 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3638 if (fd == -1) {
3639 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3640 errno != ELOOP)
3641 err = got_error_from_errno2("open", ondisk_path);
3642 else
3643 err = report_single_file_status(path, ondisk_path,
3644 fileindex, status_cb, status_arg, repo,
3645 report_unchanged);
3646 } else {
3647 fdiff_cb.diff_old_new = status_old_new;
3648 fdiff_cb.diff_old = status_old;
3649 fdiff_cb.diff_new = status_new;
3650 fdiff_cb.diff_traverse = status_traverse;
3651 arg.fileindex = fileindex;
3652 arg.worktree = worktree;
3653 arg.status_path = path;
3654 arg.status_path_len = strlen(path);
3655 arg.repo = repo;
3656 arg.status_cb = status_cb;
3657 arg.status_arg = status_arg;
3658 arg.cancel_cb = cancel_cb;
3659 arg.cancel_arg = cancel_arg;
3660 arg.report_unchanged = report_unchanged;
3661 arg.no_ignores = no_ignores;
3662 if (!no_ignores) {
3663 err = add_ignores_from_parent_paths(&arg.ignores,
3664 worktree->root_path, path);
3665 if (err)
3666 goto done;
3668 err = got_fileindex_diff_dir(fileindex, fd,
3669 worktree->root_path, path, repo, &fdiff_cb, &arg);
3671 done:
3672 free_ignores(&arg.ignores);
3673 if (fd != -1 && close(fd) == -1 && err == NULL)
3674 err = got_error_from_errno("close");
3675 free(ondisk_path);
3676 return err;
3679 const struct got_error *
3680 got_worktree_status(struct got_worktree *worktree,
3681 struct got_pathlist_head *paths, struct got_repository *repo,
3682 got_worktree_status_cb status_cb, void *status_arg,
3683 got_cancel_cb cancel_cb, void *cancel_arg)
3685 const struct got_error *err = NULL;
3686 char *fileindex_path = NULL;
3687 struct got_fileindex *fileindex = NULL;
3688 struct got_pathlist_entry *pe;
3690 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3691 if (err)
3692 return err;
3694 TAILQ_FOREACH(pe, paths, entry) {
3695 err = worktree_status(worktree, pe->path, fileindex, repo,
3696 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3697 if (err)
3698 break;
3700 free(fileindex_path);
3701 got_fileindex_free(fileindex);
3702 return err;
3705 const struct got_error *
3706 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3707 const char *arg)
3709 const struct got_error *err = NULL;
3710 char *resolved = NULL, *cwd = NULL, *path = NULL;
3711 size_t len;
3712 struct stat sb;
3713 char *abspath = NULL;
3714 char canonpath[PATH_MAX];
3716 *wt_path = NULL;
3718 cwd = getcwd(NULL, 0);
3719 if (cwd == NULL)
3720 return got_error_from_errno("getcwd");
3722 if (lstat(arg, &sb) == -1) {
3723 if (errno != ENOENT) {
3724 err = got_error_from_errno2("lstat", arg);
3725 goto done;
3727 sb.st_mode = 0;
3729 if (S_ISLNK(sb.st_mode)) {
3731 * We cannot use realpath(3) with symlinks since we want to
3732 * operate on the symlink itself.
3733 * But we can make the path absolute, assuming it is relative
3734 * to the current working directory, and then canonicalize it.
3736 if (!got_path_is_absolute(arg)) {
3737 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3738 err = got_error_from_errno("asprintf");
3739 goto done;
3743 err = got_canonpath(abspath ? abspath : arg, canonpath,
3744 sizeof(canonpath));
3745 if (err)
3746 goto done;
3747 resolved = strdup(canonpath);
3748 if (resolved == NULL) {
3749 err = got_error_from_errno("strdup");
3750 goto done;
3752 } else {
3753 resolved = realpath(arg, NULL);
3754 if (resolved == NULL) {
3755 if (errno != ENOENT) {
3756 err = got_error_from_errno2("realpath", arg);
3757 goto done;
3759 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3760 err = got_error_from_errno("asprintf");
3761 goto done;
3763 err = got_canonpath(abspath, canonpath,
3764 sizeof(canonpath));
3765 if (err)
3766 goto done;
3767 resolved = strdup(canonpath);
3768 if (resolved == NULL) {
3769 err = got_error_from_errno("strdup");
3770 goto done;
3775 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3776 strlen(got_worktree_get_root_path(worktree)))) {
3777 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3778 goto done;
3781 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3782 err = got_path_skip_common_ancestor(&path,
3783 got_worktree_get_root_path(worktree), resolved);
3784 if (err)
3785 goto done;
3786 } else {
3787 path = strdup("");
3788 if (path == NULL) {
3789 err = got_error_from_errno("strdup");
3790 goto done;
3794 /* XXX status walk can't deal with trailing slash! */
3795 len = strlen(path);
3796 while (len > 0 && path[len - 1] == '/') {
3797 path[len - 1] = '\0';
3798 len--;
3800 done:
3801 free(abspath);
3802 free(resolved);
3803 free(cwd);
3804 if (err == NULL)
3805 *wt_path = path;
3806 else
3807 free(path);
3808 return err;
3811 struct schedule_addition_args {
3812 struct got_worktree *worktree;
3813 struct got_fileindex *fileindex;
3814 got_worktree_checkout_cb progress_cb;
3815 void *progress_arg;
3816 struct got_repository *repo;
3819 static const struct got_error *
3820 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3821 const char *relpath, struct got_object_id *blob_id,
3822 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3823 int dirfd, const char *de_name)
3825 struct schedule_addition_args *a = arg;
3826 const struct got_error *err = NULL;
3827 struct got_fileindex_entry *ie;
3828 struct stat sb;
3829 char *ondisk_path;
3831 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3832 relpath) == -1)
3833 return got_error_from_errno("asprintf");
3835 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3836 if (ie) {
3837 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3838 de_name, a->repo);
3839 if (err)
3840 goto done;
3841 /* Re-adding an existing entry is a no-op. */
3842 if (status == GOT_STATUS_ADD)
3843 goto done;
3844 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3845 if (err)
3846 goto done;
3849 if (status != GOT_STATUS_UNVERSIONED) {
3850 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3851 goto done;
3854 err = got_fileindex_entry_alloc(&ie, relpath);
3855 if (err)
3856 goto done;
3857 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3858 relpath, NULL, NULL, 1);
3859 if (err) {
3860 got_fileindex_entry_free(ie);
3861 goto done;
3863 err = got_fileindex_entry_add(a->fileindex, ie);
3864 if (err) {
3865 got_fileindex_entry_free(ie);
3866 goto done;
3868 done:
3869 free(ondisk_path);
3870 if (err)
3871 return err;
3872 if (status == GOT_STATUS_ADD)
3873 return NULL;
3874 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3877 const struct got_error *
3878 got_worktree_schedule_add(struct got_worktree *worktree,
3879 struct got_pathlist_head *paths,
3880 got_worktree_checkout_cb progress_cb, void *progress_arg,
3881 struct got_repository *repo, int no_ignores)
3883 struct got_fileindex *fileindex = NULL;
3884 char *fileindex_path = NULL;
3885 const struct got_error *err = NULL, *sync_err, *unlockerr;
3886 struct got_pathlist_entry *pe;
3887 struct schedule_addition_args saa;
3889 err = lock_worktree(worktree, LOCK_EX);
3890 if (err)
3891 return err;
3893 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3894 if (err)
3895 goto done;
3897 saa.worktree = worktree;
3898 saa.fileindex = fileindex;
3899 saa.progress_cb = progress_cb;
3900 saa.progress_arg = progress_arg;
3901 saa.repo = repo;
3903 TAILQ_FOREACH(pe, paths, entry) {
3904 err = worktree_status(worktree, pe->path, fileindex, repo,
3905 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3906 if (err)
3907 break;
3909 sync_err = sync_fileindex(fileindex, fileindex_path);
3910 if (sync_err && err == NULL)
3911 err = sync_err;
3912 done:
3913 free(fileindex_path);
3914 if (fileindex)
3915 got_fileindex_free(fileindex);
3916 unlockerr = lock_worktree(worktree, LOCK_SH);
3917 if (unlockerr && err == NULL)
3918 err = unlockerr;
3919 return err;
3922 struct schedule_deletion_args {
3923 struct got_worktree *worktree;
3924 struct got_fileindex *fileindex;
3925 got_worktree_delete_cb progress_cb;
3926 void *progress_arg;
3927 struct got_repository *repo;
3928 int delete_local_mods;
3929 int keep_on_disk;
3930 const char *status_codes;
3933 static const struct got_error *
3934 schedule_for_deletion(void *arg, unsigned char status,
3935 unsigned char staged_status, const char *relpath,
3936 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3937 struct got_object_id *commit_id, int dirfd, const char *de_name)
3939 struct schedule_deletion_args *a = arg;
3940 const struct got_error *err = NULL;
3941 struct got_fileindex_entry *ie = NULL;
3942 struct stat sb;
3943 char *ondisk_path;
3945 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3946 if (ie == NULL)
3947 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3949 staged_status = get_staged_status(ie);
3950 if (staged_status != GOT_STATUS_NO_CHANGE) {
3951 if (staged_status == GOT_STATUS_DELETE)
3952 return NULL;
3953 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3956 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3957 relpath) == -1)
3958 return got_error_from_errno("asprintf");
3960 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3961 a->repo);
3962 if (err)
3963 goto done;
3965 if (a->status_codes) {
3966 size_t ncodes = strlen(a->status_codes);
3967 int i;
3968 for (i = 0; i < ncodes ; i++) {
3969 if (status == a->status_codes[i])
3970 break;
3972 if (i == ncodes) {
3973 /* Do not delete files in non-matching status. */
3974 free(ondisk_path);
3975 return NULL;
3977 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3978 a->status_codes[i] != GOT_STATUS_MISSING) {
3979 static char msg[64];
3980 snprintf(msg, sizeof(msg),
3981 "invalid status code '%c'", a->status_codes[i]);
3982 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3983 goto done;
3987 if (status != GOT_STATUS_NO_CHANGE) {
3988 if (status == GOT_STATUS_DELETE)
3989 goto done;
3990 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3991 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3992 goto done;
3994 if (status != GOT_STATUS_MODIFY &&
3995 status != GOT_STATUS_MISSING) {
3996 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3997 goto done;
4001 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4002 size_t root_len;
4004 if (dirfd != -1) {
4005 if (unlinkat(dirfd, de_name, 0) != 0) {
4006 err = got_error_from_errno2("unlinkat",
4007 ondisk_path);
4008 goto done;
4010 } else if (unlink(ondisk_path) != 0) {
4011 err = got_error_from_errno2("unlink", ondisk_path);
4012 goto done;
4015 root_len = strlen(a->worktree->root_path);
4016 do {
4017 char *parent;
4018 err = got_path_dirname(&parent, ondisk_path);
4019 if (err)
4020 goto done;
4021 free(ondisk_path);
4022 ondisk_path = parent;
4023 if (rmdir(ondisk_path) == -1) {
4024 if (errno != ENOTEMPTY)
4025 err = got_error_from_errno2("rmdir",
4026 ondisk_path);
4027 break;
4029 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4030 strlen(ondisk_path), root_len) != 0);
4033 got_fileindex_entry_mark_deleted_from_disk(ie);
4034 done:
4035 free(ondisk_path);
4036 if (err)
4037 return err;
4038 if (status == GOT_STATUS_DELETE)
4039 return NULL;
4040 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4041 staged_status, relpath);
4044 const struct got_error *
4045 got_worktree_schedule_delete(struct got_worktree *worktree,
4046 struct got_pathlist_head *paths, int delete_local_mods,
4047 const char *status_codes,
4048 got_worktree_delete_cb progress_cb, void *progress_arg,
4049 struct got_repository *repo, int keep_on_disk)
4051 struct got_fileindex *fileindex = NULL;
4052 char *fileindex_path = NULL;
4053 const struct got_error *err = NULL, *sync_err, *unlockerr;
4054 struct got_pathlist_entry *pe;
4055 struct schedule_deletion_args sda;
4057 err = lock_worktree(worktree, LOCK_EX);
4058 if (err)
4059 return err;
4061 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4062 if (err)
4063 goto done;
4065 sda.worktree = worktree;
4066 sda.fileindex = fileindex;
4067 sda.progress_cb = progress_cb;
4068 sda.progress_arg = progress_arg;
4069 sda.repo = repo;
4070 sda.delete_local_mods = delete_local_mods;
4071 sda.keep_on_disk = keep_on_disk;
4072 sda.status_codes = status_codes;
4074 TAILQ_FOREACH(pe, paths, entry) {
4075 err = worktree_status(worktree, pe->path, fileindex, repo,
4076 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4077 if (err)
4078 break;
4080 sync_err = sync_fileindex(fileindex, fileindex_path);
4081 if (sync_err && err == NULL)
4082 err = sync_err;
4083 done:
4084 free(fileindex_path);
4085 if (fileindex)
4086 got_fileindex_free(fileindex);
4087 unlockerr = lock_worktree(worktree, LOCK_SH);
4088 if (unlockerr && err == NULL)
4089 err = unlockerr;
4090 return err;
4093 static const struct got_error *
4094 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4096 const struct got_error *err = NULL;
4097 char *line = NULL;
4098 size_t linesize = 0, n;
4099 ssize_t linelen;
4101 linelen = getline(&line, &linesize, infile);
4102 if (linelen == -1) {
4103 if (ferror(infile)) {
4104 err = got_error_from_errno("getline");
4105 goto done;
4107 return NULL;
4109 if (outfile) {
4110 n = fwrite(line, 1, linelen, outfile);
4111 if (n != linelen) {
4112 err = got_ferror(outfile, GOT_ERR_IO);
4113 goto done;
4116 if (rejectfile) {
4117 n = fwrite(line, 1, linelen, rejectfile);
4118 if (n != linelen)
4119 err = got_ferror(outfile, GOT_ERR_IO);
4121 done:
4122 free(line);
4123 return err;
4126 static const struct got_error *
4127 skip_one_line(FILE *f)
4129 char *line = NULL;
4130 size_t linesize = 0;
4131 ssize_t linelen;
4133 linelen = getline(&line, &linesize, f);
4134 if (linelen == -1) {
4135 if (ferror(f))
4136 return got_error_from_errno("getline");
4137 return NULL;
4139 free(line);
4140 return NULL;
4143 static const struct got_error *
4144 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4145 int start_old, int end_old, int start_new, int end_new,
4146 FILE *outfile, FILE *rejectfile)
4148 const struct got_error *err;
4150 /* Copy old file's lines leading up to patch. */
4151 while (!feof(f1) && *line_cur1 < start_old) {
4152 err = copy_one_line(f1, outfile, NULL);
4153 if (err)
4154 return err;
4155 (*line_cur1)++;
4157 /* Skip new file's lines leading up to patch. */
4158 while (!feof(f2) && *line_cur2 < start_new) {
4159 if (rejectfile)
4160 err = copy_one_line(f2, NULL, rejectfile);
4161 else
4162 err = skip_one_line(f2);
4163 if (err)
4164 return err;
4165 (*line_cur2)++;
4167 /* Copy patched lines. */
4168 while (!feof(f2) && *line_cur2 <= end_new) {
4169 err = copy_one_line(f2, outfile, NULL);
4170 if (err)
4171 return err;
4172 (*line_cur2)++;
4174 /* Skip over old file's replaced lines. */
4175 while (!feof(f1) && *line_cur1 <= end_old) {
4176 if (rejectfile)
4177 err = copy_one_line(f1, NULL, rejectfile);
4178 else
4179 err = skip_one_line(f1);
4180 if (err)
4181 return err;
4182 (*line_cur1)++;
4185 return NULL;
4188 static const struct got_error *
4189 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4190 FILE *outfile, FILE *rejectfile)
4192 const struct got_error *err;
4194 if (outfile) {
4195 /* Copy old file's lines until EOF. */
4196 while (!feof(f1)) {
4197 err = copy_one_line(f1, outfile, NULL);
4198 if (err)
4199 return err;
4200 (*line_cur1)++;
4203 if (rejectfile) {
4204 /* Copy new file's lines until EOF. */
4205 while (!feof(f2)) {
4206 err = copy_one_line(f2, NULL, rejectfile);
4207 if (err)
4208 return err;
4209 (*line_cur2)++;
4213 return NULL;
4216 static const struct got_error *
4217 apply_or_reject_change(int *choice, int *nchunks_used,
4218 struct diff_result *diff_result, int n,
4219 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4220 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4221 got_worktree_patch_cb patch_cb, void *patch_arg)
4223 const struct got_error *err = NULL;
4224 struct diff_chunk_context cc = {};
4225 int start_old, end_old, start_new, end_new;
4226 FILE *hunkfile;
4227 struct diff_output_unidiff_state *diff_state;
4228 struct diff_input_info diff_info;
4229 int rc;
4231 *choice = GOT_PATCH_CHOICE_NONE;
4233 /* Get changed line numbers without context lines for copy_change(). */
4234 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4235 start_old = cc.left.start;
4236 end_old = cc.left.end;
4237 start_new = cc.right.start;
4238 end_new = cc.right.end;
4240 /* Get the same change with context lines for display. */
4241 memset(&cc, 0, sizeof(cc));
4242 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4244 memset(&diff_info, 0, sizeof(diff_info));
4245 diff_info.left_path = relpath;
4246 diff_info.right_path = relpath;
4248 diff_state = diff_output_unidiff_state_alloc();
4249 if (diff_state == NULL)
4250 return got_error_set_errno(ENOMEM,
4251 "diff_output_unidiff_state_alloc");
4253 hunkfile = got_opentemp();
4254 if (hunkfile == NULL) {
4255 err = got_error_from_errno("got_opentemp");
4256 goto done;
4259 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4260 diff_result, &cc);
4261 if (rc != DIFF_RC_OK) {
4262 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4263 goto done;
4266 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4267 err = got_ferror(hunkfile, GOT_ERR_IO);
4268 goto done;
4271 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4272 hunkfile, changeno, nchanges);
4273 if (err)
4274 goto done;
4276 switch (*choice) {
4277 case GOT_PATCH_CHOICE_YES:
4278 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4279 end_old, start_new, end_new, outfile, rejectfile);
4280 break;
4281 case GOT_PATCH_CHOICE_NO:
4282 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4283 end_old, start_new, end_new, rejectfile, outfile);
4284 break;
4285 case GOT_PATCH_CHOICE_QUIT:
4286 break;
4287 default:
4288 err = got_error(GOT_ERR_PATCH_CHOICE);
4289 break;
4291 done:
4292 diff_output_unidiff_state_free(diff_state);
4293 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4294 err = got_error_from_errno("fclose");
4295 return err;
4298 struct revert_file_args {
4299 struct got_worktree *worktree;
4300 struct got_fileindex *fileindex;
4301 got_worktree_checkout_cb progress_cb;
4302 void *progress_arg;
4303 got_worktree_patch_cb patch_cb;
4304 void *patch_arg;
4305 struct got_repository *repo;
4308 static const struct got_error *
4309 create_patched_content(char **path_outfile, int reverse_patch,
4310 struct got_object_id *blob_id, const char *path2,
4311 int dirfd2, const char *de_name2,
4312 const char *relpath, struct got_repository *repo,
4313 got_worktree_patch_cb patch_cb, void *patch_arg)
4315 const struct got_error *err, *free_err;
4316 struct got_blob_object *blob = NULL;
4317 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4318 int fd2 = -1;
4319 char link_target[PATH_MAX];
4320 ssize_t link_len = 0;
4321 char *path1 = NULL, *id_str = NULL;
4322 struct stat sb2;
4323 struct got_diffreg_result *diffreg_result = NULL;
4324 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4325 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4327 *path_outfile = NULL;
4329 err = got_object_id_str(&id_str, blob_id);
4330 if (err)
4331 return err;
4333 if (dirfd2 != -1) {
4334 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4335 if (fd2 == -1) {
4336 if (errno != ELOOP) {
4337 err = got_error_from_errno2("openat", path2);
4338 goto done;
4340 link_len = readlinkat(dirfd2, de_name2,
4341 link_target, sizeof(link_target));
4342 if (link_len == -1)
4343 return got_error_from_errno2("readlinkat", path2);
4344 sb2.st_mode = S_IFLNK;
4345 sb2.st_size = link_len;
4347 } else {
4348 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4349 if (fd2 == -1) {
4350 if (errno != ELOOP) {
4351 err = got_error_from_errno2("open", path2);
4352 goto done;
4354 link_len = readlink(path2, link_target,
4355 sizeof(link_target));
4356 if (link_len == -1)
4357 return got_error_from_errno2("readlink", path2);
4358 sb2.st_mode = S_IFLNK;
4359 sb2.st_size = link_len;
4362 if (fd2 != -1) {
4363 if (fstat(fd2, &sb2) == -1) {
4364 err = got_error_from_errno2("fstat", path2);
4365 goto done;
4368 f2 = fdopen(fd2, "r");
4369 if (f2 == NULL) {
4370 err = got_error_from_errno2("fdopen", path2);
4371 goto done;
4373 fd2 = -1;
4374 } else {
4375 size_t n;
4376 f2 = got_opentemp();
4377 if (f2 == NULL) {
4378 err = got_error_from_errno2("got_opentemp", path2);
4379 goto done;
4381 n = fwrite(link_target, 1, link_len, f2);
4382 if (n != link_len) {
4383 err = got_ferror(f2, GOT_ERR_IO);
4384 goto done;
4386 if (fflush(f2) == EOF) {
4387 err = got_error_from_errno("fflush");
4388 goto done;
4390 rewind(f2);
4393 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4394 if (err)
4395 goto done;
4397 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4398 if (err)
4399 goto done;
4401 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4402 if (err)
4403 goto done;
4405 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4406 NULL);
4407 if (err)
4408 goto done;
4410 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4411 if (err)
4412 goto done;
4414 if (fseek(f1, 0L, SEEK_SET) == -1)
4415 return got_ferror(f1, GOT_ERR_IO);
4416 if (fseek(f2, 0L, SEEK_SET) == -1)
4417 return got_ferror(f2, GOT_ERR_IO);
4419 /* Count the number of actual changes in the diff result. */
4420 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4421 struct diff_chunk_context cc = {};
4422 diff_chunk_context_load_change(&cc, &nchunks_used,
4423 diffreg_result->result, n, 0);
4424 nchanges++;
4426 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4427 int choice;
4428 err = apply_or_reject_change(&choice, &nchunks_used,
4429 diffreg_result->result, n, relpath, f1, f2,
4430 &line_cur1, &line_cur2,
4431 reverse_patch ? NULL : outfile,
4432 reverse_patch ? outfile : NULL,
4433 ++i, nchanges, patch_cb, patch_arg);
4434 if (err)
4435 goto done;
4436 if (choice == GOT_PATCH_CHOICE_YES)
4437 have_content = 1;
4438 else if (choice == GOT_PATCH_CHOICE_QUIT)
4439 break;
4441 if (have_content) {
4442 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4443 reverse_patch ? NULL : outfile,
4444 reverse_patch ? outfile : NULL);
4445 if (err)
4446 goto done;
4448 if (!S_ISLNK(sb2.st_mode)) {
4449 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4450 err = got_error_from_errno2("fchmod", path2);
4451 goto done;
4455 done:
4456 free(id_str);
4457 if (blob)
4458 got_object_blob_close(blob);
4459 free_err = got_diffreg_result_free(diffreg_result);
4460 if (err == NULL)
4461 err = free_err;
4462 if (f1 && fclose(f1) == EOF && err == NULL)
4463 err = got_error_from_errno2("fclose", path1);
4464 if (f2 && fclose(f2) == EOF && err == NULL)
4465 err = got_error_from_errno2("fclose", path2);
4466 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4467 err = got_error_from_errno2("close", path2);
4468 if (outfile && fclose(outfile) == EOF && err == NULL)
4469 err = got_error_from_errno2("fclose", *path_outfile);
4470 if (path1 && unlink(path1) == -1 && err == NULL)
4471 err = got_error_from_errno2("unlink", path1);
4472 if (err || !have_content) {
4473 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4474 err = got_error_from_errno2("unlink", *path_outfile);
4475 free(*path_outfile);
4476 *path_outfile = NULL;
4478 free(path1);
4479 return err;
4482 static const struct got_error *
4483 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4484 const char *relpath, struct got_object_id *blob_id,
4485 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4486 int dirfd, const char *de_name)
4488 struct revert_file_args *a = arg;
4489 const struct got_error *err = NULL;
4490 char *parent_path = NULL;
4491 struct got_fileindex_entry *ie;
4492 struct got_tree_object *tree = NULL;
4493 struct got_object_id *tree_id = NULL;
4494 const struct got_tree_entry *te = NULL;
4495 char *tree_path = NULL, *te_name;
4496 char *ondisk_path = NULL, *path_content = NULL;
4497 struct got_blob_object *blob = NULL;
4499 /* Reverting a staged deletion is a no-op. */
4500 if (status == GOT_STATUS_DELETE &&
4501 staged_status != GOT_STATUS_NO_CHANGE)
4502 return NULL;
4504 if (status == GOT_STATUS_UNVERSIONED)
4505 return (*a->progress_cb)(a->progress_arg,
4506 GOT_STATUS_UNVERSIONED, relpath);
4508 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4509 if (ie == NULL)
4510 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4512 /* Construct in-repository path of tree which contains this blob. */
4513 err = got_path_dirname(&parent_path, ie->path);
4514 if (err) {
4515 if (err->code != GOT_ERR_BAD_PATH)
4516 goto done;
4517 parent_path = strdup("/");
4518 if (parent_path == NULL) {
4519 err = got_error_from_errno("strdup");
4520 goto done;
4523 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4524 tree_path = strdup(parent_path);
4525 if (tree_path == NULL) {
4526 err = got_error_from_errno("strdup");
4527 goto done;
4529 } else {
4530 if (got_path_is_root_dir(parent_path)) {
4531 tree_path = strdup(a->worktree->path_prefix);
4532 if (tree_path == NULL) {
4533 err = got_error_from_errno("strdup");
4534 goto done;
4536 } else {
4537 if (asprintf(&tree_path, "%s/%s",
4538 a->worktree->path_prefix, parent_path) == -1) {
4539 err = got_error_from_errno("asprintf");
4540 goto done;
4545 err = got_object_id_by_path(&tree_id, a->repo,
4546 a->worktree->base_commit_id, tree_path);
4547 if (err) {
4548 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4549 (status == GOT_STATUS_ADD ||
4550 staged_status == GOT_STATUS_ADD)))
4551 goto done;
4552 } else {
4553 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4554 if (err)
4555 goto done;
4557 err = got_path_basename(&te_name, ie->path);
4558 if (err)
4559 goto done;
4561 te = got_object_tree_find_entry(tree, te_name);
4562 free(te_name);
4563 if (te == NULL && status != GOT_STATUS_ADD &&
4564 staged_status != GOT_STATUS_ADD) {
4565 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4566 goto done;
4570 switch (status) {
4571 case GOT_STATUS_ADD:
4572 if (a->patch_cb) {
4573 int choice = GOT_PATCH_CHOICE_NONE;
4574 err = (*a->patch_cb)(&choice, a->patch_arg,
4575 status, ie->path, NULL, 1, 1);
4576 if (err)
4577 goto done;
4578 if (choice != GOT_PATCH_CHOICE_YES)
4579 break;
4581 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4582 ie->path);
4583 if (err)
4584 goto done;
4585 got_fileindex_entry_remove(a->fileindex, ie);
4586 break;
4587 case GOT_STATUS_DELETE:
4588 if (a->patch_cb) {
4589 int choice = GOT_PATCH_CHOICE_NONE;
4590 err = (*a->patch_cb)(&choice, a->patch_arg,
4591 status, ie->path, NULL, 1, 1);
4592 if (err)
4593 goto done;
4594 if (choice != GOT_PATCH_CHOICE_YES)
4595 break;
4597 /* fall through */
4598 case GOT_STATUS_MODIFY:
4599 case GOT_STATUS_MODE_CHANGE:
4600 case GOT_STATUS_CONFLICT:
4601 case GOT_STATUS_MISSING: {
4602 struct got_object_id id;
4603 if (staged_status == GOT_STATUS_ADD ||
4604 staged_status == GOT_STATUS_MODIFY) {
4605 memcpy(id.sha1, ie->staged_blob_sha1,
4606 SHA1_DIGEST_LENGTH);
4607 } else
4608 memcpy(id.sha1, ie->blob_sha1,
4609 SHA1_DIGEST_LENGTH);
4610 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4611 if (err)
4612 goto done;
4614 if (asprintf(&ondisk_path, "%s/%s",
4615 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4616 err = got_error_from_errno("asprintf");
4617 goto done;
4620 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4621 status == GOT_STATUS_CONFLICT)) {
4622 int is_bad_symlink = 0;
4623 err = create_patched_content(&path_content, 1, &id,
4624 ondisk_path, dirfd, de_name, ie->path, a->repo,
4625 a->patch_cb, a->patch_arg);
4626 if (err || path_content == NULL)
4627 break;
4628 if (te && S_ISLNK(te->mode)) {
4629 if (unlink(path_content) == -1) {
4630 err = got_error_from_errno2("unlink",
4631 path_content);
4632 break;
4634 err = install_symlink(&is_bad_symlink,
4635 a->worktree, ondisk_path, ie->path,
4636 blob, 0, 1, 0, a->repo,
4637 a->progress_cb, a->progress_arg);
4638 } else {
4639 if (rename(path_content, ondisk_path) == -1) {
4640 err = got_error_from_errno3("rename",
4641 path_content, ondisk_path);
4642 goto done;
4645 } else {
4646 int is_bad_symlink = 0;
4647 if (te && S_ISLNK(te->mode)) {
4648 err = install_symlink(&is_bad_symlink,
4649 a->worktree, ondisk_path, ie->path,
4650 blob, 0, 1, 0, a->repo,
4651 a->progress_cb, a->progress_arg);
4652 } else {
4653 err = install_blob(a->worktree, ondisk_path,
4654 ie->path,
4655 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4656 got_fileindex_perms_to_st(ie), blob,
4657 0, 1, 0, 0, a->repo,
4658 a->progress_cb, a->progress_arg);
4660 if (err)
4661 goto done;
4662 if (status == GOT_STATUS_DELETE ||
4663 status == GOT_STATUS_MODE_CHANGE) {
4664 err = got_fileindex_entry_update(ie,
4665 a->worktree->root_fd, relpath,
4666 blob->id.sha1,
4667 a->worktree->base_commit_id->sha1, 1);
4668 if (err)
4669 goto done;
4671 if (is_bad_symlink) {
4672 got_fileindex_entry_filetype_set(ie,
4673 GOT_FILEIDX_MODE_BAD_SYMLINK);
4676 break;
4678 default:
4679 break;
4681 done:
4682 free(ondisk_path);
4683 free(path_content);
4684 free(parent_path);
4685 free(tree_path);
4686 if (blob)
4687 got_object_blob_close(blob);
4688 if (tree)
4689 got_object_tree_close(tree);
4690 free(tree_id);
4691 return err;
4694 const struct got_error *
4695 got_worktree_revert(struct got_worktree *worktree,
4696 struct got_pathlist_head *paths,
4697 got_worktree_checkout_cb progress_cb, void *progress_arg,
4698 got_worktree_patch_cb patch_cb, void *patch_arg,
4699 struct got_repository *repo)
4701 struct got_fileindex *fileindex = NULL;
4702 char *fileindex_path = NULL;
4703 const struct got_error *err = NULL, *unlockerr = NULL;
4704 const struct got_error *sync_err = NULL;
4705 struct got_pathlist_entry *pe;
4706 struct revert_file_args rfa;
4708 err = lock_worktree(worktree, LOCK_EX);
4709 if (err)
4710 return err;
4712 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4713 if (err)
4714 goto done;
4716 rfa.worktree = worktree;
4717 rfa.fileindex = fileindex;
4718 rfa.progress_cb = progress_cb;
4719 rfa.progress_arg = progress_arg;
4720 rfa.patch_cb = patch_cb;
4721 rfa.patch_arg = patch_arg;
4722 rfa.repo = repo;
4723 TAILQ_FOREACH(pe, paths, entry) {
4724 err = worktree_status(worktree, pe->path, fileindex, repo,
4725 revert_file, &rfa, NULL, NULL, 0, 0);
4726 if (err)
4727 break;
4729 sync_err = sync_fileindex(fileindex, fileindex_path);
4730 if (sync_err && err == NULL)
4731 err = sync_err;
4732 done:
4733 free(fileindex_path);
4734 if (fileindex)
4735 got_fileindex_free(fileindex);
4736 unlockerr = lock_worktree(worktree, LOCK_SH);
4737 if (unlockerr && err == NULL)
4738 err = unlockerr;
4739 return err;
4742 static void
4743 free_commitable(struct got_commitable *ct)
4745 free(ct->path);
4746 free(ct->in_repo_path);
4747 free(ct->ondisk_path);
4748 free(ct->blob_id);
4749 free(ct->base_blob_id);
4750 free(ct->staged_blob_id);
4751 free(ct->base_commit_id);
4752 free(ct);
4755 struct collect_commitables_arg {
4756 struct got_pathlist_head *commitable_paths;
4757 struct got_repository *repo;
4758 struct got_worktree *worktree;
4759 struct got_fileindex *fileindex;
4760 int have_staged_files;
4761 int allow_bad_symlinks;
4764 static const struct got_error *
4765 collect_commitables(void *arg, unsigned char status,
4766 unsigned char staged_status, const char *relpath,
4767 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4768 struct got_object_id *commit_id, int dirfd, const char *de_name)
4770 struct collect_commitables_arg *a = arg;
4771 const struct got_error *err = NULL;
4772 struct got_commitable *ct = NULL;
4773 struct got_pathlist_entry *new = NULL;
4774 char *parent_path = NULL, *path = NULL;
4775 struct stat sb;
4777 if (a->have_staged_files) {
4778 if (staged_status != GOT_STATUS_MODIFY &&
4779 staged_status != GOT_STATUS_ADD &&
4780 staged_status != GOT_STATUS_DELETE)
4781 return NULL;
4782 } else {
4783 if (status == GOT_STATUS_CONFLICT)
4784 return got_error(GOT_ERR_COMMIT_CONFLICT);
4786 if (status != GOT_STATUS_MODIFY &&
4787 status != GOT_STATUS_MODE_CHANGE &&
4788 status != GOT_STATUS_ADD &&
4789 status != GOT_STATUS_DELETE)
4790 return NULL;
4793 if (asprintf(&path, "/%s", relpath) == -1) {
4794 err = got_error_from_errno("asprintf");
4795 goto done;
4797 if (strcmp(path, "/") == 0) {
4798 parent_path = strdup("");
4799 if (parent_path == NULL)
4800 return got_error_from_errno("strdup");
4801 } else {
4802 err = got_path_dirname(&parent_path, path);
4803 if (err)
4804 return err;
4807 ct = calloc(1, sizeof(*ct));
4808 if (ct == NULL) {
4809 err = got_error_from_errno("calloc");
4810 goto done;
4813 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4814 relpath) == -1) {
4815 err = got_error_from_errno("asprintf");
4816 goto done;
4819 if (staged_status == GOT_STATUS_ADD ||
4820 staged_status == GOT_STATUS_MODIFY) {
4821 struct got_fileindex_entry *ie;
4822 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4823 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4824 case GOT_FILEIDX_MODE_REGULAR_FILE:
4825 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4826 ct->mode = S_IFREG;
4827 break;
4828 case GOT_FILEIDX_MODE_SYMLINK:
4829 ct->mode = S_IFLNK;
4830 break;
4831 default:
4832 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4833 goto done;
4835 ct->mode |= got_fileindex_entry_perms_get(ie);
4836 } else if (status != GOT_STATUS_DELETE &&
4837 staged_status != GOT_STATUS_DELETE) {
4838 if (dirfd != -1) {
4839 if (fstatat(dirfd, de_name, &sb,
4840 AT_SYMLINK_NOFOLLOW) == -1) {
4841 err = got_error_from_errno2("fstatat",
4842 ct->ondisk_path);
4843 goto done;
4845 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4846 err = got_error_from_errno2("lstat", ct->ondisk_path);
4847 goto done;
4849 ct->mode = sb.st_mode;
4852 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4853 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4854 relpath) == -1) {
4855 err = got_error_from_errno("asprintf");
4856 goto done;
4859 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4860 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4861 int is_bad_symlink;
4862 char target_path[PATH_MAX];
4863 ssize_t target_len;
4864 target_len = readlink(ct->ondisk_path, target_path,
4865 sizeof(target_path));
4866 if (target_len == -1) {
4867 err = got_error_from_errno2("readlink",
4868 ct->ondisk_path);
4869 goto done;
4871 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4872 target_len, ct->ondisk_path, a->worktree->root_path);
4873 if (err)
4874 goto done;
4875 if (is_bad_symlink) {
4876 err = got_error_path(ct->ondisk_path,
4877 GOT_ERR_BAD_SYMLINK);
4878 goto done;
4883 ct->status = status;
4884 ct->staged_status = staged_status;
4885 ct->blob_id = NULL; /* will be filled in when blob gets created */
4886 if (ct->status != GOT_STATUS_ADD &&
4887 ct->staged_status != GOT_STATUS_ADD) {
4888 ct->base_blob_id = got_object_id_dup(blob_id);
4889 if (ct->base_blob_id == NULL) {
4890 err = got_error_from_errno("got_object_id_dup");
4891 goto done;
4893 ct->base_commit_id = got_object_id_dup(commit_id);
4894 if (ct->base_commit_id == NULL) {
4895 err = got_error_from_errno("got_object_id_dup");
4896 goto done;
4899 if (ct->staged_status == GOT_STATUS_ADD ||
4900 ct->staged_status == GOT_STATUS_MODIFY) {
4901 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4902 if (ct->staged_blob_id == NULL) {
4903 err = got_error_from_errno("got_object_id_dup");
4904 goto done;
4907 ct->path = strdup(path);
4908 if (ct->path == NULL) {
4909 err = got_error_from_errno("strdup");
4910 goto done;
4912 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4913 done:
4914 if (ct && (err || new == NULL))
4915 free_commitable(ct);
4916 free(parent_path);
4917 free(path);
4918 return err;
4921 static const struct got_error *write_tree(struct got_object_id **, int *,
4922 struct got_tree_object *, const char *, struct got_pathlist_head *,
4923 got_worktree_status_cb status_cb, void *status_arg,
4924 struct got_repository *);
4926 static const struct got_error *
4927 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4928 struct got_tree_entry *te, const char *parent_path,
4929 struct got_pathlist_head *commitable_paths,
4930 got_worktree_status_cb status_cb, void *status_arg,
4931 struct got_repository *repo)
4933 const struct got_error *err = NULL;
4934 struct got_tree_object *subtree;
4935 char *subpath;
4937 if (asprintf(&subpath, "%s%s%s", parent_path,
4938 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4939 return got_error_from_errno("asprintf");
4941 err = got_object_open_as_tree(&subtree, repo, &te->id);
4942 if (err)
4943 return err;
4945 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4946 commitable_paths, status_cb, status_arg, repo);
4947 got_object_tree_close(subtree);
4948 free(subpath);
4949 return err;
4952 static const struct got_error *
4953 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4955 const struct got_error *err = NULL;
4956 char *ct_parent_path = NULL;
4958 *match = 0;
4960 if (strchr(ct->in_repo_path, '/') == NULL) {
4961 *match = got_path_is_root_dir(path);
4962 return NULL;
4965 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4966 if (err)
4967 return err;
4968 *match = (strcmp(path, ct_parent_path) == 0);
4969 free(ct_parent_path);
4970 return err;
4973 static mode_t
4974 get_ct_file_mode(struct got_commitable *ct)
4976 if (S_ISLNK(ct->mode))
4977 return S_IFLNK;
4979 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4982 static const struct got_error *
4983 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4984 struct got_tree_entry *te, struct got_commitable *ct)
4986 const struct got_error *err = NULL;
4988 *new_te = NULL;
4990 err = got_object_tree_entry_dup(new_te, te);
4991 if (err)
4992 goto done;
4994 (*new_te)->mode = get_ct_file_mode(ct);
4996 if (ct->staged_status == GOT_STATUS_MODIFY)
4997 memcpy(&(*new_te)->id, ct->staged_blob_id,
4998 sizeof((*new_te)->id));
4999 else
5000 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5001 done:
5002 if (err && *new_te) {
5003 free(*new_te);
5004 *new_te = NULL;
5006 return err;
5009 static const struct got_error *
5010 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5011 struct got_commitable *ct)
5013 const struct got_error *err = NULL;
5014 char *ct_name = NULL;
5016 *new_te = NULL;
5018 *new_te = calloc(1, sizeof(**new_te));
5019 if (*new_te == NULL)
5020 return got_error_from_errno("calloc");
5022 err = got_path_basename(&ct_name, ct->path);
5023 if (err)
5024 goto done;
5025 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5026 sizeof((*new_te)->name)) {
5027 err = got_error(GOT_ERR_NO_SPACE);
5028 goto done;
5031 (*new_te)->mode = get_ct_file_mode(ct);
5033 if (ct->staged_status == GOT_STATUS_ADD)
5034 memcpy(&(*new_te)->id, ct->staged_blob_id,
5035 sizeof((*new_te)->id));
5036 else
5037 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5038 done:
5039 free(ct_name);
5040 if (err && *new_te) {
5041 free(*new_te);
5042 *new_te = NULL;
5044 return err;
5047 static const struct got_error *
5048 insert_tree_entry(struct got_tree_entry *new_te,
5049 struct got_pathlist_head *paths)
5051 const struct got_error *err = NULL;
5052 struct got_pathlist_entry *new_pe;
5054 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5055 if (err)
5056 return err;
5057 if (new_pe == NULL)
5058 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5059 return NULL;
5062 static const struct got_error *
5063 report_ct_status(struct got_commitable *ct,
5064 got_worktree_status_cb status_cb, void *status_arg)
5066 const char *ct_path = ct->path;
5067 unsigned char status;
5069 while (ct_path[0] == '/')
5070 ct_path++;
5072 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5073 status = ct->staged_status;
5074 else
5075 status = ct->status;
5077 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5078 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5081 static const struct got_error *
5082 match_modified_subtree(int *modified, struct got_tree_entry *te,
5083 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5085 const struct got_error *err = NULL;
5086 struct got_pathlist_entry *pe;
5087 char *te_path;
5089 *modified = 0;
5091 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5092 got_path_is_root_dir(base_tree_path) ? "" : "/",
5093 te->name) == -1)
5094 return got_error_from_errno("asprintf");
5096 TAILQ_FOREACH(pe, commitable_paths, entry) {
5097 struct got_commitable *ct = pe->data;
5098 *modified = got_path_is_child(ct->in_repo_path, te_path,
5099 strlen(te_path));
5100 if (*modified)
5101 break;
5104 free(te_path);
5105 return err;
5108 static const struct got_error *
5109 match_deleted_or_modified_ct(struct got_commitable **ctp,
5110 struct got_tree_entry *te, const char *base_tree_path,
5111 struct got_pathlist_head *commitable_paths)
5113 const struct got_error *err = NULL;
5114 struct got_pathlist_entry *pe;
5116 *ctp = NULL;
5118 TAILQ_FOREACH(pe, commitable_paths, entry) {
5119 struct got_commitable *ct = pe->data;
5120 char *ct_name = NULL;
5121 int path_matches;
5123 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5124 if (ct->status != GOT_STATUS_MODIFY &&
5125 ct->status != GOT_STATUS_MODE_CHANGE &&
5126 ct->status != GOT_STATUS_DELETE)
5127 continue;
5128 } else {
5129 if (ct->staged_status != GOT_STATUS_MODIFY &&
5130 ct->staged_status != GOT_STATUS_DELETE)
5131 continue;
5134 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5135 continue;
5137 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5138 if (err)
5139 return err;
5140 if (!path_matches)
5141 continue;
5143 err = got_path_basename(&ct_name, pe->path);
5144 if (err)
5145 return err;
5147 if (strcmp(te->name, ct_name) != 0) {
5148 free(ct_name);
5149 continue;
5151 free(ct_name);
5153 *ctp = ct;
5154 break;
5157 return err;
5160 static const struct got_error *
5161 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5162 const char *child_path, const char *path_base_tree,
5163 struct got_pathlist_head *commitable_paths,
5164 got_worktree_status_cb status_cb, void *status_arg,
5165 struct got_repository *repo)
5167 const struct got_error *err = NULL;
5168 struct got_tree_entry *new_te;
5169 char *subtree_path;
5170 struct got_object_id *id = NULL;
5171 int nentries;
5173 *new_tep = NULL;
5175 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5176 got_path_is_root_dir(path_base_tree) ? "" : "/",
5177 child_path) == -1)
5178 return got_error_from_errno("asprintf");
5180 new_te = calloc(1, sizeof(*new_te));
5181 if (new_te == NULL)
5182 return got_error_from_errno("calloc");
5183 new_te->mode = S_IFDIR;
5185 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5186 sizeof(new_te->name)) {
5187 err = got_error(GOT_ERR_NO_SPACE);
5188 goto done;
5190 err = write_tree(&id, &nentries, NULL, subtree_path,
5191 commitable_paths, status_cb, status_arg, repo);
5192 if (err) {
5193 free(new_te);
5194 goto done;
5196 memcpy(&new_te->id, id, sizeof(new_te->id));
5197 done:
5198 free(id);
5199 free(subtree_path);
5200 if (err == NULL)
5201 *new_tep = new_te;
5202 return err;
5205 static const struct got_error *
5206 write_tree(struct got_object_id **new_tree_id, int *nentries,
5207 struct got_tree_object *base_tree, const char *path_base_tree,
5208 struct got_pathlist_head *commitable_paths,
5209 got_worktree_status_cb status_cb, void *status_arg,
5210 struct got_repository *repo)
5212 const struct got_error *err = NULL;
5213 struct got_pathlist_head paths;
5214 struct got_tree_entry *te, *new_te = NULL;
5215 struct got_pathlist_entry *pe;
5217 TAILQ_INIT(&paths);
5218 *nentries = 0;
5220 /* Insert, and recurse into, newly added entries first. */
5221 TAILQ_FOREACH(pe, commitable_paths, entry) {
5222 struct got_commitable *ct = pe->data;
5223 char *child_path = NULL, *slash;
5225 if ((ct->status != GOT_STATUS_ADD &&
5226 ct->staged_status != GOT_STATUS_ADD) ||
5227 (ct->flags & GOT_COMMITABLE_ADDED))
5228 continue;
5230 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5231 strlen(path_base_tree)))
5232 continue;
5234 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5235 ct->in_repo_path);
5236 if (err)
5237 goto done;
5239 slash = strchr(child_path, '/');
5240 if (slash == NULL) {
5241 err = alloc_added_blob_tree_entry(&new_te, ct);
5242 if (err)
5243 goto done;
5244 err = report_ct_status(ct, status_cb, status_arg);
5245 if (err)
5246 goto done;
5247 ct->flags |= GOT_COMMITABLE_ADDED;
5248 err = insert_tree_entry(new_te, &paths);
5249 if (err)
5250 goto done;
5251 (*nentries)++;
5252 } else {
5253 *slash = '\0'; /* trim trailing path components */
5254 if (base_tree == NULL ||
5255 got_object_tree_find_entry(base_tree, child_path)
5256 == NULL) {
5257 err = make_subtree_for_added_blob(&new_te,
5258 child_path, path_base_tree,
5259 commitable_paths, status_cb, status_arg,
5260 repo);
5261 if (err)
5262 goto done;
5263 err = insert_tree_entry(new_te, &paths);
5264 if (err)
5265 goto done;
5266 (*nentries)++;
5271 if (base_tree) {
5272 int i, nbase_entries;
5273 /* Handle modified and deleted entries. */
5274 nbase_entries = got_object_tree_get_nentries(base_tree);
5275 for (i = 0; i < nbase_entries; i++) {
5276 struct got_commitable *ct = NULL;
5278 te = got_object_tree_get_entry(base_tree, i);
5279 if (got_object_tree_entry_is_submodule(te)) {
5280 /* Entry is a submodule; just copy it. */
5281 err = got_object_tree_entry_dup(&new_te, te);
5282 if (err)
5283 goto done;
5284 err = insert_tree_entry(new_te, &paths);
5285 if (err)
5286 goto done;
5287 (*nentries)++;
5288 continue;
5291 if (S_ISDIR(te->mode)) {
5292 int modified;
5293 err = got_object_tree_entry_dup(&new_te, te);
5294 if (err)
5295 goto done;
5296 err = match_modified_subtree(&modified, te,
5297 path_base_tree, commitable_paths);
5298 if (err)
5299 goto done;
5300 /* Avoid recursion into unmodified subtrees. */
5301 if (modified) {
5302 struct got_object_id *new_id;
5303 int nsubentries;
5304 err = write_subtree(&new_id,
5305 &nsubentries, te,
5306 path_base_tree, commitable_paths,
5307 status_cb, status_arg, repo);
5308 if (err)
5309 goto done;
5310 if (nsubentries == 0) {
5311 /* All entries were deleted. */
5312 free(new_id);
5313 continue;
5315 memcpy(&new_te->id, new_id,
5316 sizeof(new_te->id));
5317 free(new_id);
5319 err = insert_tree_entry(new_te, &paths);
5320 if (err)
5321 goto done;
5322 (*nentries)++;
5323 continue;
5326 err = match_deleted_or_modified_ct(&ct, te,
5327 path_base_tree, commitable_paths);
5328 if (err)
5329 goto done;
5330 if (ct) {
5331 /* NB: Deleted entries get dropped here. */
5332 if (ct->status == GOT_STATUS_MODIFY ||
5333 ct->status == GOT_STATUS_MODE_CHANGE ||
5334 ct->staged_status == GOT_STATUS_MODIFY) {
5335 err = alloc_modified_blob_tree_entry(
5336 &new_te, te, ct);
5337 if (err)
5338 goto done;
5339 err = insert_tree_entry(new_te, &paths);
5340 if (err)
5341 goto done;
5342 (*nentries)++;
5344 err = report_ct_status(ct, status_cb,
5345 status_arg);
5346 if (err)
5347 goto done;
5348 } else {
5349 /* Entry is unchanged; just copy it. */
5350 err = got_object_tree_entry_dup(&new_te, te);
5351 if (err)
5352 goto done;
5353 err = insert_tree_entry(new_te, &paths);
5354 if (err)
5355 goto done;
5356 (*nentries)++;
5361 /* Write new list of entries; deleted entries have been dropped. */
5362 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5363 done:
5364 got_pathlist_free(&paths);
5365 return err;
5368 static const struct got_error *
5369 update_fileindex_after_commit(struct got_worktree *worktree,
5370 struct got_pathlist_head *commitable_paths,
5371 struct got_object_id *new_base_commit_id,
5372 struct got_fileindex *fileindex, int have_staged_files)
5374 const struct got_error *err = NULL;
5375 struct got_pathlist_entry *pe;
5376 char *relpath = NULL;
5378 TAILQ_FOREACH(pe, commitable_paths, entry) {
5379 struct got_fileindex_entry *ie;
5380 struct got_commitable *ct = pe->data;
5382 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5384 err = got_path_skip_common_ancestor(&relpath,
5385 worktree->root_path, ct->ondisk_path);
5386 if (err)
5387 goto done;
5389 if (ie) {
5390 if (ct->status == GOT_STATUS_DELETE ||
5391 ct->staged_status == GOT_STATUS_DELETE) {
5392 got_fileindex_entry_remove(fileindex, ie);
5393 } else if (ct->staged_status == GOT_STATUS_ADD ||
5394 ct->staged_status == GOT_STATUS_MODIFY) {
5395 got_fileindex_entry_stage_set(ie,
5396 GOT_FILEIDX_STAGE_NONE);
5397 got_fileindex_entry_staged_filetype_set(ie, 0);
5399 err = got_fileindex_entry_update(ie,
5400 worktree->root_fd, relpath,
5401 ct->staged_blob_id->sha1,
5402 new_base_commit_id->sha1,
5403 !have_staged_files);
5404 } else
5405 err = got_fileindex_entry_update(ie,
5406 worktree->root_fd, relpath,
5407 ct->blob_id->sha1,
5408 new_base_commit_id->sha1,
5409 !have_staged_files);
5410 } else {
5411 err = got_fileindex_entry_alloc(&ie, pe->path);
5412 if (err)
5413 goto done;
5414 err = got_fileindex_entry_update(ie,
5415 worktree->root_fd, relpath, ct->blob_id->sha1,
5416 new_base_commit_id->sha1, 1);
5417 if (err) {
5418 got_fileindex_entry_free(ie);
5419 goto done;
5421 err = got_fileindex_entry_add(fileindex, ie);
5422 if (err) {
5423 got_fileindex_entry_free(ie);
5424 goto done;
5427 free(relpath);
5428 relpath = NULL;
5430 done:
5431 free(relpath);
5432 return err;
5436 static const struct got_error *
5437 check_out_of_date(const char *in_repo_path, unsigned char status,
5438 unsigned char staged_status, struct got_object_id *base_blob_id,
5439 struct got_object_id *base_commit_id,
5440 struct got_object_id *head_commit_id, struct got_repository *repo,
5441 int ood_errcode)
5443 const struct got_error *err = NULL;
5444 struct got_object_id *id = NULL;
5446 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5447 /* Trivial case: base commit == head commit */
5448 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5449 return NULL;
5451 * Ensure file content which local changes were based
5452 * on matches file content in the branch head.
5454 err = got_object_id_by_path(&id, repo, head_commit_id,
5455 in_repo_path);
5456 if (err) {
5457 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5458 err = got_error(ood_errcode);
5459 goto done;
5460 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5461 err = got_error(ood_errcode);
5462 } else {
5463 /* Require that added files don't exist in the branch head. */
5464 err = got_object_id_by_path(&id, repo, head_commit_id,
5465 in_repo_path);
5466 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5467 goto done;
5468 err = id ? got_error(ood_errcode) : NULL;
5470 done:
5471 free(id);
5472 return err;
5475 const struct got_error *
5476 commit_worktree(struct got_object_id **new_commit_id,
5477 struct got_pathlist_head *commitable_paths,
5478 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5479 const char *author, const char *committer,
5480 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5481 got_worktree_status_cb status_cb, void *status_arg,
5482 struct got_repository *repo)
5484 const struct got_error *err = NULL, *unlockerr = NULL;
5485 struct got_pathlist_entry *pe;
5486 const char *head_ref_name = NULL;
5487 struct got_commit_object *head_commit = NULL;
5488 struct got_reference *head_ref2 = NULL;
5489 struct got_object_id *head_commit_id2 = NULL;
5490 struct got_tree_object *head_tree = NULL;
5491 struct got_object_id *new_tree_id = NULL;
5492 int nentries;
5493 struct got_object_id_queue parent_ids;
5494 struct got_object_qid *pid = NULL;
5495 char *logmsg = NULL;
5497 *new_commit_id = NULL;
5499 SIMPLEQ_INIT(&parent_ids);
5501 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5502 if (err)
5503 goto done;
5505 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5506 if (err)
5507 goto done;
5509 if (commit_msg_cb != NULL) {
5510 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5511 if (err)
5512 goto done;
5515 if (logmsg == NULL || strlen(logmsg) == 0) {
5516 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5517 goto done;
5520 /* Create blobs from added and modified files and record their IDs. */
5521 TAILQ_FOREACH(pe, commitable_paths, entry) {
5522 struct got_commitable *ct = pe->data;
5523 char *ondisk_path;
5525 /* Blobs for staged files already exist. */
5526 if (ct->staged_status == GOT_STATUS_ADD ||
5527 ct->staged_status == GOT_STATUS_MODIFY)
5528 continue;
5530 if (ct->status != GOT_STATUS_ADD &&
5531 ct->status != GOT_STATUS_MODIFY &&
5532 ct->status != GOT_STATUS_MODE_CHANGE)
5533 continue;
5535 if (asprintf(&ondisk_path, "%s/%s",
5536 worktree->root_path, pe->path) == -1) {
5537 err = got_error_from_errno("asprintf");
5538 goto done;
5540 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5541 free(ondisk_path);
5542 if (err)
5543 goto done;
5546 /* Recursively write new tree objects. */
5547 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5548 commitable_paths, status_cb, status_arg, repo);
5549 if (err)
5550 goto done;
5552 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5553 if (err)
5554 goto done;
5555 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5556 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5557 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5558 got_object_qid_free(pid);
5559 if (logmsg != NULL)
5560 free(logmsg);
5561 if (err)
5562 goto done;
5564 /* Check if a concurrent commit to our branch has occurred. */
5565 head_ref_name = got_worktree_get_head_ref_name(worktree);
5566 if (head_ref_name == NULL) {
5567 err = got_error_from_errno("got_worktree_get_head_ref_name");
5568 goto done;
5570 /* Lock the reference here to prevent concurrent modification. */
5571 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5572 if (err)
5573 goto done;
5574 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5575 if (err)
5576 goto done;
5577 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5578 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5579 goto done;
5581 /* Update branch head in repository. */
5582 err = got_ref_change_ref(head_ref2, *new_commit_id);
5583 if (err)
5584 goto done;
5585 err = got_ref_write(head_ref2, repo);
5586 if (err)
5587 goto done;
5589 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5590 if (err)
5591 goto done;
5593 err = ref_base_commit(worktree, repo);
5594 if (err)
5595 goto done;
5596 done:
5597 if (head_tree)
5598 got_object_tree_close(head_tree);
5599 if (head_commit)
5600 got_object_commit_close(head_commit);
5601 free(head_commit_id2);
5602 if (head_ref2) {
5603 unlockerr = got_ref_unlock(head_ref2);
5604 if (unlockerr && err == NULL)
5605 err = unlockerr;
5606 got_ref_close(head_ref2);
5608 return err;
5611 static const struct got_error *
5612 check_path_is_commitable(const char *path,
5613 struct got_pathlist_head *commitable_paths)
5615 struct got_pathlist_entry *cpe = NULL;
5616 size_t path_len = strlen(path);
5618 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5619 struct got_commitable *ct = cpe->data;
5620 const char *ct_path = ct->path;
5622 while (ct_path[0] == '/')
5623 ct_path++;
5625 if (strcmp(path, ct_path) == 0 ||
5626 got_path_is_child(ct_path, path, path_len))
5627 break;
5630 if (cpe == NULL)
5631 return got_error_path(path, GOT_ERR_BAD_PATH);
5633 return NULL;
5636 static const struct got_error *
5637 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5639 int *have_staged_files = arg;
5641 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5642 *have_staged_files = 1;
5643 return got_error(GOT_ERR_CANCELLED);
5646 return NULL;
5649 static const struct got_error *
5650 check_non_staged_files(struct got_fileindex *fileindex,
5651 struct got_pathlist_head *paths)
5653 struct got_pathlist_entry *pe;
5654 struct got_fileindex_entry *ie;
5656 TAILQ_FOREACH(pe, paths, entry) {
5657 if (pe->path[0] == '\0')
5658 continue;
5659 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5660 if (ie == NULL)
5661 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5662 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5663 return got_error_path(pe->path,
5664 GOT_ERR_FILE_NOT_STAGED);
5667 return NULL;
5670 const struct got_error *
5671 got_worktree_commit(struct got_object_id **new_commit_id,
5672 struct got_worktree *worktree, struct got_pathlist_head *paths,
5673 const char *author, const char *committer, int allow_bad_symlinks,
5674 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5675 got_worktree_status_cb status_cb, void *status_arg,
5676 struct got_repository *repo)
5678 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5679 struct got_fileindex *fileindex = NULL;
5680 char *fileindex_path = NULL;
5681 struct got_pathlist_head commitable_paths;
5682 struct collect_commitables_arg cc_arg;
5683 struct got_pathlist_entry *pe;
5684 struct got_reference *head_ref = NULL;
5685 struct got_object_id *head_commit_id = NULL;
5686 int have_staged_files = 0;
5688 *new_commit_id = NULL;
5690 TAILQ_INIT(&commitable_paths);
5692 err = lock_worktree(worktree, LOCK_EX);
5693 if (err)
5694 goto done;
5696 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5697 if (err)
5698 goto done;
5700 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5701 if (err)
5702 goto done;
5704 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5705 if (err)
5706 goto done;
5708 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5709 &have_staged_files);
5710 if (err && err->code != GOT_ERR_CANCELLED)
5711 goto done;
5712 if (have_staged_files) {
5713 err = check_non_staged_files(fileindex, paths);
5714 if (err)
5715 goto done;
5718 cc_arg.commitable_paths = &commitable_paths;
5719 cc_arg.worktree = worktree;
5720 cc_arg.fileindex = fileindex;
5721 cc_arg.repo = repo;
5722 cc_arg.have_staged_files = have_staged_files;
5723 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5724 TAILQ_FOREACH(pe, paths, entry) {
5725 err = worktree_status(worktree, pe->path, fileindex, repo,
5726 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5727 if (err)
5728 goto done;
5731 if (TAILQ_EMPTY(&commitable_paths)) {
5732 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5733 goto done;
5736 TAILQ_FOREACH(pe, paths, entry) {
5737 err = check_path_is_commitable(pe->path, &commitable_paths);
5738 if (err)
5739 goto done;
5742 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5743 struct got_commitable *ct = pe->data;
5744 const char *ct_path = ct->in_repo_path;
5746 while (ct_path[0] == '/')
5747 ct_path++;
5748 err = check_out_of_date(ct_path, ct->status,
5749 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5750 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5751 if (err)
5752 goto done;
5756 err = commit_worktree(new_commit_id, &commitable_paths,
5757 head_commit_id, worktree, author, committer,
5758 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5759 if (err)
5760 goto done;
5762 err = update_fileindex_after_commit(worktree, &commitable_paths,
5763 *new_commit_id, fileindex, have_staged_files);
5764 sync_err = sync_fileindex(fileindex, fileindex_path);
5765 if (sync_err && err == NULL)
5766 err = sync_err;
5767 done:
5768 if (fileindex)
5769 got_fileindex_free(fileindex);
5770 free(fileindex_path);
5771 unlockerr = lock_worktree(worktree, LOCK_SH);
5772 if (unlockerr && err == NULL)
5773 err = unlockerr;
5774 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5775 struct got_commitable *ct = pe->data;
5776 free_commitable(ct);
5778 got_pathlist_free(&commitable_paths);
5779 return err;
5782 const char *
5783 got_commitable_get_path(struct got_commitable *ct)
5785 return ct->path;
5788 unsigned int
5789 got_commitable_get_status(struct got_commitable *ct)
5791 return ct->status;
5794 struct check_rebase_ok_arg {
5795 struct got_worktree *worktree;
5796 struct got_repository *repo;
5799 static const struct got_error *
5800 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5802 const struct got_error *err = NULL;
5803 struct check_rebase_ok_arg *a = arg;
5804 unsigned char status;
5805 struct stat sb;
5806 char *ondisk_path;
5808 /* Reject rebase of a work tree with mixed base commits. */
5809 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5810 SHA1_DIGEST_LENGTH))
5811 return got_error(GOT_ERR_MIXED_COMMITS);
5813 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5814 == -1)
5815 return got_error_from_errno("asprintf");
5817 /* Reject rebase of a work tree with modified or staged files. */
5818 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5819 free(ondisk_path);
5820 if (err)
5821 return err;
5823 if (status != GOT_STATUS_NO_CHANGE)
5824 return got_error(GOT_ERR_MODIFIED);
5825 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5826 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5828 return NULL;
5831 const struct got_error *
5832 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5833 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5834 struct got_worktree *worktree, struct got_reference *branch,
5835 struct got_repository *repo)
5837 const struct got_error *err = NULL;
5838 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5839 char *branch_ref_name = NULL;
5840 char *fileindex_path = NULL;
5841 struct check_rebase_ok_arg ok_arg;
5842 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5843 struct got_object_id *wt_branch_tip = NULL;
5845 *new_base_branch_ref = NULL;
5846 *tmp_branch = NULL;
5847 *fileindex = NULL;
5849 err = lock_worktree(worktree, LOCK_EX);
5850 if (err)
5851 return err;
5853 err = open_fileindex(fileindex, &fileindex_path, worktree);
5854 if (err)
5855 goto done;
5857 ok_arg.worktree = worktree;
5858 ok_arg.repo = repo;
5859 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5860 &ok_arg);
5861 if (err)
5862 goto done;
5864 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5865 if (err)
5866 goto done;
5868 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5869 if (err)
5870 goto done;
5872 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5873 if (err)
5874 goto done;
5876 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5877 0);
5878 if (err)
5879 goto done;
5881 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5882 if (err)
5883 goto done;
5884 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5885 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5886 goto done;
5889 err = got_ref_alloc_symref(new_base_branch_ref,
5890 new_base_branch_ref_name, wt_branch);
5891 if (err)
5892 goto done;
5893 err = got_ref_write(*new_base_branch_ref, repo);
5894 if (err)
5895 goto done;
5897 /* TODO Lock original branch's ref while rebasing? */
5899 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5900 if (err)
5901 goto done;
5903 err = got_ref_write(branch_ref, repo);
5904 if (err)
5905 goto done;
5907 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5908 worktree->base_commit_id);
5909 if (err)
5910 goto done;
5911 err = got_ref_write(*tmp_branch, repo);
5912 if (err)
5913 goto done;
5915 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5916 if (err)
5917 goto done;
5918 done:
5919 free(fileindex_path);
5920 free(tmp_branch_name);
5921 free(new_base_branch_ref_name);
5922 free(branch_ref_name);
5923 if (branch_ref)
5924 got_ref_close(branch_ref);
5925 if (wt_branch)
5926 got_ref_close(wt_branch);
5927 free(wt_branch_tip);
5928 if (err) {
5929 if (*new_base_branch_ref) {
5930 got_ref_close(*new_base_branch_ref);
5931 *new_base_branch_ref = NULL;
5933 if (*tmp_branch) {
5934 got_ref_close(*tmp_branch);
5935 *tmp_branch = NULL;
5937 if (*fileindex) {
5938 got_fileindex_free(*fileindex);
5939 *fileindex = NULL;
5941 lock_worktree(worktree, LOCK_SH);
5943 return err;
5946 const struct got_error *
5947 got_worktree_rebase_continue(struct got_object_id **commit_id,
5948 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5949 struct got_reference **branch, struct got_fileindex **fileindex,
5950 struct got_worktree *worktree, struct got_repository *repo)
5952 const struct got_error *err;
5953 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5954 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5955 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5956 char *fileindex_path = NULL;
5957 int have_staged_files = 0;
5959 *commit_id = NULL;
5960 *new_base_branch = NULL;
5961 *tmp_branch = NULL;
5962 *branch = NULL;
5963 *fileindex = NULL;
5965 err = lock_worktree(worktree, LOCK_EX);
5966 if (err)
5967 return err;
5969 err = open_fileindex(fileindex, &fileindex_path, worktree);
5970 if (err)
5971 goto done;
5973 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5974 &have_staged_files);
5975 if (err && err->code != GOT_ERR_CANCELLED)
5976 goto done;
5977 if (have_staged_files) {
5978 err = got_error(GOT_ERR_STAGED_PATHS);
5979 goto done;
5982 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5983 if (err)
5984 goto done;
5986 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5987 if (err)
5988 goto done;
5990 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5991 if (err)
5992 goto done;
5994 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5995 if (err)
5996 goto done;
5998 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5999 if (err)
6000 goto done;
6002 err = got_ref_open(branch, repo,
6003 got_ref_get_symref_target(branch_ref), 0);
6004 if (err)
6005 goto done;
6007 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6008 if (err)
6009 goto done;
6011 err = got_ref_resolve(commit_id, repo, commit_ref);
6012 if (err)
6013 goto done;
6015 err = got_ref_open(new_base_branch, repo,
6016 new_base_branch_ref_name, 0);
6017 if (err)
6018 goto done;
6020 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6021 if (err)
6022 goto done;
6023 done:
6024 free(commit_ref_name);
6025 free(branch_ref_name);
6026 free(fileindex_path);
6027 if (commit_ref)
6028 got_ref_close(commit_ref);
6029 if (branch_ref)
6030 got_ref_close(branch_ref);
6031 if (err) {
6032 free(*commit_id);
6033 *commit_id = NULL;
6034 if (*tmp_branch) {
6035 got_ref_close(*tmp_branch);
6036 *tmp_branch = NULL;
6038 if (*new_base_branch) {
6039 got_ref_close(*new_base_branch);
6040 *new_base_branch = NULL;
6042 if (*branch) {
6043 got_ref_close(*branch);
6044 *branch = NULL;
6046 if (*fileindex) {
6047 got_fileindex_free(*fileindex);
6048 *fileindex = NULL;
6050 lock_worktree(worktree, LOCK_SH);
6052 return err;
6055 const struct got_error *
6056 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6058 const struct got_error *err;
6059 char *tmp_branch_name = NULL;
6061 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6062 if (err)
6063 return err;
6065 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6066 free(tmp_branch_name);
6067 return NULL;
6070 static const struct got_error *
6071 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6072 char **logmsg, void *arg)
6074 *logmsg = arg;
6075 return NULL;
6078 static const struct got_error *
6079 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6080 const char *path, struct got_object_id *blob_id,
6081 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6082 int dirfd, const char *de_name)
6084 return NULL;
6087 struct collect_merged_paths_arg {
6088 got_worktree_checkout_cb progress_cb;
6089 void *progress_arg;
6090 struct got_pathlist_head *merged_paths;
6093 static const struct got_error *
6094 collect_merged_paths(void *arg, unsigned char status, const char *path)
6096 const struct got_error *err;
6097 struct collect_merged_paths_arg *a = arg;
6098 char *p;
6099 struct got_pathlist_entry *new;
6101 err = (*a->progress_cb)(a->progress_arg, status, path);
6102 if (err)
6103 return err;
6105 if (status != GOT_STATUS_MERGE &&
6106 status != GOT_STATUS_ADD &&
6107 status != GOT_STATUS_DELETE &&
6108 status != GOT_STATUS_CONFLICT)
6109 return NULL;
6111 p = strdup(path);
6112 if (p == NULL)
6113 return got_error_from_errno("strdup");
6115 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6116 if (err || new == NULL)
6117 free(p);
6118 return err;
6121 void
6122 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6124 struct got_pathlist_entry *pe;
6126 TAILQ_FOREACH(pe, merged_paths, entry)
6127 free((char *)pe->path);
6129 got_pathlist_free(merged_paths);
6132 static const struct got_error *
6133 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6134 int is_rebase, struct got_repository *repo)
6136 const struct got_error *err;
6137 struct got_reference *commit_ref = NULL;
6139 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6140 if (err) {
6141 if (err->code != GOT_ERR_NOT_REF)
6142 goto done;
6143 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6144 if (err)
6145 goto done;
6146 err = got_ref_write(commit_ref, repo);
6147 if (err)
6148 goto done;
6149 } else if (is_rebase) {
6150 struct got_object_id *stored_id;
6151 int cmp;
6153 err = got_ref_resolve(&stored_id, repo, commit_ref);
6154 if (err)
6155 goto done;
6156 cmp = got_object_id_cmp(commit_id, stored_id);
6157 free(stored_id);
6158 if (cmp != 0) {
6159 err = got_error(GOT_ERR_REBASE_COMMITID);
6160 goto done;
6163 done:
6164 if (commit_ref)
6165 got_ref_close(commit_ref);
6166 return err;
6169 static const struct got_error *
6170 rebase_merge_files(struct got_pathlist_head *merged_paths,
6171 const char *commit_ref_name, struct got_worktree *worktree,
6172 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6173 struct got_object_id *commit_id, struct got_repository *repo,
6174 got_worktree_checkout_cb progress_cb, void *progress_arg,
6175 got_cancel_cb cancel_cb, void *cancel_arg)
6177 const struct got_error *err;
6178 struct got_reference *commit_ref = NULL;
6179 struct collect_merged_paths_arg cmp_arg;
6180 char *fileindex_path;
6182 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6184 err = get_fileindex_path(&fileindex_path, worktree);
6185 if (err)
6186 return err;
6188 cmp_arg.progress_cb = progress_cb;
6189 cmp_arg.progress_arg = progress_arg;
6190 cmp_arg.merged_paths = merged_paths;
6191 err = merge_files(worktree, fileindex, fileindex_path,
6192 parent_commit_id, commit_id, repo, collect_merged_paths,
6193 &cmp_arg, cancel_cb, cancel_arg);
6194 if (commit_ref)
6195 got_ref_close(commit_ref);
6196 return err;
6199 const struct got_error *
6200 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6201 struct got_worktree *worktree, struct got_fileindex *fileindex,
6202 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6203 struct got_repository *repo,
6204 got_worktree_checkout_cb progress_cb, void *progress_arg,
6205 got_cancel_cb cancel_cb, void *cancel_arg)
6207 const struct got_error *err;
6208 char *commit_ref_name;
6210 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6211 if (err)
6212 return err;
6214 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6215 if (err)
6216 goto done;
6218 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6219 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6220 progress_arg, cancel_cb, cancel_arg);
6221 done:
6222 free(commit_ref_name);
6223 return err;
6226 const struct got_error *
6227 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6228 struct got_worktree *worktree, struct got_fileindex *fileindex,
6229 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6230 struct got_repository *repo,
6231 got_worktree_checkout_cb progress_cb, void *progress_arg,
6232 got_cancel_cb cancel_cb, void *cancel_arg)
6234 const struct got_error *err;
6235 char *commit_ref_name;
6237 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6238 if (err)
6239 return err;
6241 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6242 if (err)
6243 goto done;
6245 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6246 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6247 progress_arg, cancel_cb, cancel_arg);
6248 done:
6249 free(commit_ref_name);
6250 return err;
6253 static const struct got_error *
6254 rebase_commit(struct got_object_id **new_commit_id,
6255 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6256 struct got_worktree *worktree, struct got_fileindex *fileindex,
6257 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6258 const char *new_logmsg, struct got_repository *repo)
6260 const struct got_error *err, *sync_err;
6261 struct got_pathlist_head commitable_paths;
6262 struct collect_commitables_arg cc_arg;
6263 char *fileindex_path = NULL;
6264 struct got_reference *head_ref = NULL;
6265 struct got_object_id *head_commit_id = NULL;
6266 char *logmsg = NULL;
6268 TAILQ_INIT(&commitable_paths);
6269 *new_commit_id = NULL;
6271 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6273 err = get_fileindex_path(&fileindex_path, worktree);
6274 if (err)
6275 return err;
6277 cc_arg.commitable_paths = &commitable_paths;
6278 cc_arg.worktree = worktree;
6279 cc_arg.repo = repo;
6280 cc_arg.have_staged_files = 0;
6282 * If possible get the status of individual files directly to
6283 * avoid crawling the entire work tree once per rebased commit.
6284 * TODO: Ideally, merged_paths would contain a list of commitables
6285 * we could use so we could skip worktree_status() entirely.
6287 if (merged_paths) {
6288 struct got_pathlist_entry *pe;
6289 TAILQ_FOREACH(pe, merged_paths, entry) {
6290 err = worktree_status(worktree, pe->path, fileindex,
6291 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6292 0);
6293 if (err)
6294 goto done;
6296 } else {
6297 err = worktree_status(worktree, "", fileindex, repo,
6298 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6299 if (err)
6300 goto done;
6303 if (TAILQ_EMPTY(&commitable_paths)) {
6304 /* No-op change; commit will be elided. */
6305 err = got_ref_delete(commit_ref, repo);
6306 if (err)
6307 goto done;
6308 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6309 goto done;
6312 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6313 if (err)
6314 goto done;
6316 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6317 if (err)
6318 goto done;
6320 if (new_logmsg) {
6321 logmsg = strdup(new_logmsg);
6322 if (logmsg == NULL) {
6323 err = got_error_from_errno("strdup");
6324 goto done;
6326 } else {
6327 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6328 if (err)
6329 goto done;
6332 /* NB: commit_worktree will call free(logmsg) */
6333 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6334 worktree, got_object_commit_get_author(orig_commit),
6335 got_object_commit_get_committer(orig_commit),
6336 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6337 if (err)
6338 goto done;
6340 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6341 if (err)
6342 goto done;
6344 err = got_ref_delete(commit_ref, repo);
6345 if (err)
6346 goto done;
6348 err = update_fileindex_after_commit(worktree, &commitable_paths,
6349 *new_commit_id, fileindex, 0);
6350 sync_err = sync_fileindex(fileindex, fileindex_path);
6351 if (sync_err && err == NULL)
6352 err = sync_err;
6353 done:
6354 free(fileindex_path);
6355 free(head_commit_id);
6356 if (head_ref)
6357 got_ref_close(head_ref);
6358 if (err) {
6359 free(*new_commit_id);
6360 *new_commit_id = NULL;
6362 return err;
6365 const struct got_error *
6366 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6367 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6368 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6369 struct got_commit_object *orig_commit,
6370 struct got_object_id *orig_commit_id, struct got_repository *repo)
6372 const struct got_error *err;
6373 char *commit_ref_name;
6374 struct got_reference *commit_ref = NULL;
6375 struct got_object_id *commit_id = NULL;
6377 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6378 if (err)
6379 return err;
6381 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6382 if (err)
6383 goto done;
6384 err = got_ref_resolve(&commit_id, repo, commit_ref);
6385 if (err)
6386 goto done;
6387 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6388 err = got_error(GOT_ERR_REBASE_COMMITID);
6389 goto done;
6392 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6393 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6394 done:
6395 if (commit_ref)
6396 got_ref_close(commit_ref);
6397 free(commit_ref_name);
6398 free(commit_id);
6399 return err;
6402 const struct got_error *
6403 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6404 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6405 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6406 struct got_commit_object *orig_commit,
6407 struct got_object_id *orig_commit_id, const char *new_logmsg,
6408 struct got_repository *repo)
6410 const struct got_error *err;
6411 char *commit_ref_name;
6412 struct got_reference *commit_ref = NULL;
6414 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6415 if (err)
6416 return err;
6418 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6419 if (err)
6420 goto done;
6422 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6423 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6424 done:
6425 if (commit_ref)
6426 got_ref_close(commit_ref);
6427 free(commit_ref_name);
6428 return err;
6431 const struct got_error *
6432 got_worktree_rebase_postpone(struct got_worktree *worktree,
6433 struct got_fileindex *fileindex)
6435 if (fileindex)
6436 got_fileindex_free(fileindex);
6437 return lock_worktree(worktree, LOCK_SH);
6440 static const struct got_error *
6441 delete_ref(const char *name, struct got_repository *repo)
6443 const struct got_error *err;
6444 struct got_reference *ref;
6446 err = got_ref_open(&ref, repo, name, 0);
6447 if (err) {
6448 if (err->code == GOT_ERR_NOT_REF)
6449 return NULL;
6450 return err;
6453 err = got_ref_delete(ref, repo);
6454 got_ref_close(ref);
6455 return err;
6458 static const struct got_error *
6459 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6461 const struct got_error *err;
6462 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6463 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6465 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6466 if (err)
6467 goto done;
6468 err = delete_ref(tmp_branch_name, repo);
6469 if (err)
6470 goto done;
6472 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6473 if (err)
6474 goto done;
6475 err = delete_ref(new_base_branch_ref_name, repo);
6476 if (err)
6477 goto done;
6479 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6480 if (err)
6481 goto done;
6482 err = delete_ref(branch_ref_name, repo);
6483 if (err)
6484 goto done;
6486 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6487 if (err)
6488 goto done;
6489 err = delete_ref(commit_ref_name, repo);
6490 if (err)
6491 goto done;
6493 done:
6494 free(tmp_branch_name);
6495 free(new_base_branch_ref_name);
6496 free(branch_ref_name);
6497 free(commit_ref_name);
6498 return err;
6501 const struct got_error *
6502 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6503 struct got_object_id *new_commit_id, struct got_repository *repo)
6505 const struct got_error *err;
6506 struct got_reference *ref = NULL;
6507 struct got_object_id *old_commit_id = NULL;
6508 const char *branch_name = NULL;
6509 char *new_id_str = NULL;
6510 char *refname = NULL;
6512 branch_name = got_ref_get_name(branch);
6513 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6514 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6515 branch_name += 11;
6517 err = got_object_id_str(&new_id_str, new_commit_id);
6518 if (err)
6519 return err;
6521 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6522 new_id_str) == -1) {
6523 err = got_error_from_errno("asprintf");
6524 goto done;
6527 err = got_ref_resolve(&old_commit_id, repo, branch);
6528 if (err)
6529 goto done;
6531 err = got_ref_alloc(&ref, refname, old_commit_id);
6532 if (err)
6533 goto done;
6535 err = got_ref_write(ref, repo);
6536 done:
6537 free(new_id_str);
6538 free(refname);
6539 free(old_commit_id);
6540 if (ref)
6541 got_ref_close(ref);
6542 return err;
6545 const struct got_error *
6546 got_worktree_rebase_complete(struct got_worktree *worktree,
6547 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6548 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6549 struct got_repository *repo, int create_backup)
6551 const struct got_error *err, *unlockerr, *sync_err;
6552 struct got_object_id *new_head_commit_id = NULL;
6553 char *fileindex_path = NULL;
6555 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6556 if (err)
6557 return err;
6559 if (create_backup) {
6560 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6561 rebased_branch, new_head_commit_id, repo);
6562 if (err)
6563 goto done;
6566 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6567 if (err)
6568 goto done;
6570 err = got_ref_write(rebased_branch, repo);
6571 if (err)
6572 goto done;
6574 err = got_worktree_set_head_ref(worktree, rebased_branch);
6575 if (err)
6576 goto done;
6578 err = delete_rebase_refs(worktree, repo);
6579 if (err)
6580 goto done;
6582 err = get_fileindex_path(&fileindex_path, worktree);
6583 if (err)
6584 goto done;
6585 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6586 sync_err = sync_fileindex(fileindex, fileindex_path);
6587 if (sync_err && err == NULL)
6588 err = sync_err;
6589 done:
6590 got_fileindex_free(fileindex);
6591 free(fileindex_path);
6592 free(new_head_commit_id);
6593 unlockerr = lock_worktree(worktree, LOCK_SH);
6594 if (unlockerr && err == NULL)
6595 err = unlockerr;
6596 return err;
6599 const struct got_error *
6600 got_worktree_rebase_abort(struct got_worktree *worktree,
6601 struct got_fileindex *fileindex, struct got_repository *repo,
6602 struct got_reference *new_base_branch,
6603 got_worktree_checkout_cb progress_cb, void *progress_arg)
6605 const struct got_error *err, *unlockerr, *sync_err;
6606 struct got_reference *resolved = NULL;
6607 struct got_object_id *commit_id = NULL;
6608 char *fileindex_path = NULL;
6609 struct revert_file_args rfa;
6610 struct got_object_id *tree_id = NULL;
6612 err = lock_worktree(worktree, LOCK_EX);
6613 if (err)
6614 return err;
6616 err = got_ref_open(&resolved, repo,
6617 got_ref_get_symref_target(new_base_branch), 0);
6618 if (err)
6619 goto done;
6621 err = got_worktree_set_head_ref(worktree, resolved);
6622 if (err)
6623 goto done;
6626 * XXX commits to the base branch could have happened while
6627 * we were busy rebasing; should we store the original commit ID
6628 * when rebase begins and read it back here?
6630 err = got_ref_resolve(&commit_id, repo, resolved);
6631 if (err)
6632 goto done;
6634 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6635 if (err)
6636 goto done;
6638 err = got_object_id_by_path(&tree_id, repo,
6639 worktree->base_commit_id, worktree->path_prefix);
6640 if (err)
6641 goto done;
6643 err = delete_rebase_refs(worktree, repo);
6644 if (err)
6645 goto done;
6647 err = get_fileindex_path(&fileindex_path, worktree);
6648 if (err)
6649 goto done;
6651 rfa.worktree = worktree;
6652 rfa.fileindex = fileindex;
6653 rfa.progress_cb = progress_cb;
6654 rfa.progress_arg = progress_arg;
6655 rfa.patch_cb = NULL;
6656 rfa.patch_arg = NULL;
6657 rfa.repo = repo;
6658 err = worktree_status(worktree, "", fileindex, repo,
6659 revert_file, &rfa, NULL, NULL, 0, 0);
6660 if (err)
6661 goto sync;
6663 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6664 repo, progress_cb, progress_arg, NULL, NULL);
6665 sync:
6666 sync_err = sync_fileindex(fileindex, fileindex_path);
6667 if (sync_err && err == NULL)
6668 err = sync_err;
6669 done:
6670 got_ref_close(resolved);
6671 free(tree_id);
6672 free(commit_id);
6673 if (fileindex)
6674 got_fileindex_free(fileindex);
6675 free(fileindex_path);
6677 unlockerr = lock_worktree(worktree, LOCK_SH);
6678 if (unlockerr && err == NULL)
6679 err = unlockerr;
6680 return err;
6683 const struct got_error *
6684 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6685 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6686 struct got_fileindex **fileindex, struct got_worktree *worktree,
6687 struct got_repository *repo)
6689 const struct got_error *err = NULL;
6690 char *tmp_branch_name = NULL;
6691 char *branch_ref_name = NULL;
6692 char *base_commit_ref_name = NULL;
6693 char *fileindex_path = NULL;
6694 struct check_rebase_ok_arg ok_arg;
6695 struct got_reference *wt_branch = NULL;
6696 struct got_reference *base_commit_ref = NULL;
6698 *tmp_branch = NULL;
6699 *branch_ref = NULL;
6700 *base_commit_id = NULL;
6701 *fileindex = NULL;
6703 err = lock_worktree(worktree, LOCK_EX);
6704 if (err)
6705 return err;
6707 err = open_fileindex(fileindex, &fileindex_path, worktree);
6708 if (err)
6709 goto done;
6711 ok_arg.worktree = worktree;
6712 ok_arg.repo = repo;
6713 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6714 &ok_arg);
6715 if (err)
6716 goto done;
6718 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6719 if (err)
6720 goto done;
6722 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6723 if (err)
6724 goto done;
6726 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6727 worktree);
6728 if (err)
6729 goto done;
6731 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6732 0);
6733 if (err)
6734 goto done;
6736 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6737 if (err)
6738 goto done;
6740 err = got_ref_write(*branch_ref, repo);
6741 if (err)
6742 goto done;
6744 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6745 worktree->base_commit_id);
6746 if (err)
6747 goto done;
6748 err = got_ref_write(base_commit_ref, repo);
6749 if (err)
6750 goto done;
6751 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6752 if (*base_commit_id == NULL) {
6753 err = got_error_from_errno("got_object_id_dup");
6754 goto done;
6757 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6758 worktree->base_commit_id);
6759 if (err)
6760 goto done;
6761 err = got_ref_write(*tmp_branch, repo);
6762 if (err)
6763 goto done;
6765 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6766 if (err)
6767 goto done;
6768 done:
6769 free(fileindex_path);
6770 free(tmp_branch_name);
6771 free(branch_ref_name);
6772 free(base_commit_ref_name);
6773 if (wt_branch)
6774 got_ref_close(wt_branch);
6775 if (err) {
6776 if (*branch_ref) {
6777 got_ref_close(*branch_ref);
6778 *branch_ref = NULL;
6780 if (*tmp_branch) {
6781 got_ref_close(*tmp_branch);
6782 *tmp_branch = NULL;
6784 free(*base_commit_id);
6785 if (*fileindex) {
6786 got_fileindex_free(*fileindex);
6787 *fileindex = NULL;
6789 lock_worktree(worktree, LOCK_SH);
6791 return err;
6794 const struct got_error *
6795 got_worktree_histedit_postpone(struct got_worktree *worktree,
6796 struct got_fileindex *fileindex)
6798 if (fileindex)
6799 got_fileindex_free(fileindex);
6800 return lock_worktree(worktree, LOCK_SH);
6803 const struct got_error *
6804 got_worktree_histedit_in_progress(int *in_progress,
6805 struct got_worktree *worktree)
6807 const struct got_error *err;
6808 char *tmp_branch_name = NULL;
6810 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6811 if (err)
6812 return err;
6814 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6815 free(tmp_branch_name);
6816 return NULL;
6819 const struct got_error *
6820 got_worktree_histedit_continue(struct got_object_id **commit_id,
6821 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6822 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6823 struct got_worktree *worktree, struct got_repository *repo)
6825 const struct got_error *err;
6826 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6827 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6828 struct got_reference *commit_ref = NULL;
6829 struct got_reference *base_commit_ref = NULL;
6830 char *fileindex_path = NULL;
6831 int have_staged_files = 0;
6833 *commit_id = NULL;
6834 *tmp_branch = NULL;
6835 *base_commit_id = NULL;
6836 *fileindex = NULL;
6838 err = lock_worktree(worktree, LOCK_EX);
6839 if (err)
6840 return err;
6842 err = open_fileindex(fileindex, &fileindex_path, worktree);
6843 if (err)
6844 goto done;
6846 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6847 &have_staged_files);
6848 if (err && err->code != GOT_ERR_CANCELLED)
6849 goto done;
6850 if (have_staged_files) {
6851 err = got_error(GOT_ERR_STAGED_PATHS);
6852 goto done;
6855 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6856 if (err)
6857 goto done;
6859 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6860 if (err)
6861 goto done;
6863 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6864 if (err)
6865 goto done;
6867 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6868 worktree);
6869 if (err)
6870 goto done;
6872 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6873 if (err)
6874 goto done;
6876 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6877 if (err)
6878 goto done;
6879 err = got_ref_resolve(commit_id, repo, commit_ref);
6880 if (err)
6881 goto done;
6883 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6884 if (err)
6885 goto done;
6886 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6887 if (err)
6888 goto done;
6890 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6891 if (err)
6892 goto done;
6893 done:
6894 free(commit_ref_name);
6895 free(branch_ref_name);
6896 free(fileindex_path);
6897 if (commit_ref)
6898 got_ref_close(commit_ref);
6899 if (base_commit_ref)
6900 got_ref_close(base_commit_ref);
6901 if (err) {
6902 free(*commit_id);
6903 *commit_id = NULL;
6904 free(*base_commit_id);
6905 *base_commit_id = NULL;
6906 if (*tmp_branch) {
6907 got_ref_close(*tmp_branch);
6908 *tmp_branch = NULL;
6910 if (*fileindex) {
6911 got_fileindex_free(*fileindex);
6912 *fileindex = NULL;
6914 lock_worktree(worktree, LOCK_EX);
6916 return err;
6919 static const struct got_error *
6920 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6922 const struct got_error *err;
6923 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6924 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6926 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6927 if (err)
6928 goto done;
6929 err = delete_ref(tmp_branch_name, repo);
6930 if (err)
6931 goto done;
6933 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6934 worktree);
6935 if (err)
6936 goto done;
6937 err = delete_ref(base_commit_ref_name, repo);
6938 if (err)
6939 goto done;
6941 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6942 if (err)
6943 goto done;
6944 err = delete_ref(branch_ref_name, repo);
6945 if (err)
6946 goto done;
6948 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6949 if (err)
6950 goto done;
6951 err = delete_ref(commit_ref_name, repo);
6952 if (err)
6953 goto done;
6954 done:
6955 free(tmp_branch_name);
6956 free(base_commit_ref_name);
6957 free(branch_ref_name);
6958 free(commit_ref_name);
6959 return err;
6962 const struct got_error *
6963 got_worktree_histedit_abort(struct got_worktree *worktree,
6964 struct got_fileindex *fileindex, struct got_repository *repo,
6965 struct got_reference *branch, struct got_object_id *base_commit_id,
6966 got_worktree_checkout_cb progress_cb, void *progress_arg)
6968 const struct got_error *err, *unlockerr, *sync_err;
6969 struct got_reference *resolved = NULL;
6970 char *fileindex_path = NULL;
6971 struct got_object_id *tree_id = NULL;
6972 struct revert_file_args rfa;
6974 err = lock_worktree(worktree, LOCK_EX);
6975 if (err)
6976 return err;
6978 err = got_ref_open(&resolved, repo,
6979 got_ref_get_symref_target(branch), 0);
6980 if (err)
6981 goto done;
6983 err = got_worktree_set_head_ref(worktree, resolved);
6984 if (err)
6985 goto done;
6987 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6988 if (err)
6989 goto done;
6991 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6992 worktree->path_prefix);
6993 if (err)
6994 goto done;
6996 err = delete_histedit_refs(worktree, repo);
6997 if (err)
6998 goto done;
7000 err = get_fileindex_path(&fileindex_path, worktree);
7001 if (err)
7002 goto done;
7004 rfa.worktree = worktree;
7005 rfa.fileindex = fileindex;
7006 rfa.progress_cb = progress_cb;
7007 rfa.progress_arg = progress_arg;
7008 rfa.patch_cb = NULL;
7009 rfa.patch_arg = NULL;
7010 rfa.repo = repo;
7011 err = worktree_status(worktree, "", fileindex, repo,
7012 revert_file, &rfa, NULL, NULL, 0, 0);
7013 if (err)
7014 goto sync;
7016 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7017 repo, progress_cb, progress_arg, NULL, NULL);
7018 sync:
7019 sync_err = sync_fileindex(fileindex, fileindex_path);
7020 if (sync_err && err == NULL)
7021 err = sync_err;
7022 done:
7023 got_ref_close(resolved);
7024 free(tree_id);
7025 free(fileindex_path);
7027 unlockerr = lock_worktree(worktree, LOCK_SH);
7028 if (unlockerr && err == NULL)
7029 err = unlockerr;
7030 return err;
7033 const struct got_error *
7034 got_worktree_histedit_complete(struct got_worktree *worktree,
7035 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7036 struct got_reference *edited_branch, struct got_repository *repo)
7038 const struct got_error *err, *unlockerr, *sync_err;
7039 struct got_object_id *new_head_commit_id = NULL;
7040 struct got_reference *resolved = NULL;
7041 char *fileindex_path = NULL;
7043 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7044 if (err)
7045 return err;
7047 err = got_ref_open(&resolved, repo,
7048 got_ref_get_symref_target(edited_branch), 0);
7049 if (err)
7050 goto done;
7052 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7053 resolved, new_head_commit_id, repo);
7054 if (err)
7055 goto done;
7057 err = got_ref_change_ref(resolved, new_head_commit_id);
7058 if (err)
7059 goto done;
7061 err = got_ref_write(resolved, repo);
7062 if (err)
7063 goto done;
7065 err = got_worktree_set_head_ref(worktree, resolved);
7066 if (err)
7067 goto done;
7069 err = delete_histedit_refs(worktree, repo);
7070 if (err)
7071 goto done;
7073 err = get_fileindex_path(&fileindex_path, worktree);
7074 if (err)
7075 goto done;
7076 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7077 sync_err = sync_fileindex(fileindex, fileindex_path);
7078 if (sync_err && err == NULL)
7079 err = sync_err;
7080 done:
7081 got_fileindex_free(fileindex);
7082 free(fileindex_path);
7083 free(new_head_commit_id);
7084 unlockerr = lock_worktree(worktree, LOCK_SH);
7085 if (unlockerr && err == NULL)
7086 err = unlockerr;
7087 return err;
7090 const struct got_error *
7091 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7092 struct got_object_id *commit_id, struct got_repository *repo)
7094 const struct got_error *err;
7095 char *commit_ref_name;
7097 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7098 if (err)
7099 return err;
7101 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7102 if (err)
7103 goto done;
7105 err = delete_ref(commit_ref_name, repo);
7106 done:
7107 free(commit_ref_name);
7108 return err;
7111 const struct got_error *
7112 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7113 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7114 struct got_worktree *worktree, const char *refname,
7115 struct got_repository *repo)
7117 const struct got_error *err = NULL;
7118 char *fileindex_path = NULL;
7119 struct check_rebase_ok_arg ok_arg;
7121 *fileindex = NULL;
7122 *branch_ref = NULL;
7123 *base_branch_ref = NULL;
7125 err = lock_worktree(worktree, LOCK_EX);
7126 if (err)
7127 return err;
7129 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7130 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7131 "cannot integrate a branch into itself; "
7132 "update -b or different branch name required");
7133 goto done;
7136 err = open_fileindex(fileindex, &fileindex_path, worktree);
7137 if (err)
7138 goto done;
7140 /* Preconditions are the same as for rebase. */
7141 ok_arg.worktree = worktree;
7142 ok_arg.repo = repo;
7143 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7144 &ok_arg);
7145 if (err)
7146 goto done;
7148 err = got_ref_open(branch_ref, repo, refname, 1);
7149 if (err)
7150 goto done;
7152 err = got_ref_open(base_branch_ref, repo,
7153 got_worktree_get_head_ref_name(worktree), 1);
7154 done:
7155 if (err) {
7156 if (*branch_ref) {
7157 got_ref_close(*branch_ref);
7158 *branch_ref = NULL;
7160 if (*base_branch_ref) {
7161 got_ref_close(*base_branch_ref);
7162 *base_branch_ref = NULL;
7164 if (*fileindex) {
7165 got_fileindex_free(*fileindex);
7166 *fileindex = NULL;
7168 lock_worktree(worktree, LOCK_SH);
7170 return err;
7173 const struct got_error *
7174 got_worktree_integrate_continue(struct got_worktree *worktree,
7175 struct got_fileindex *fileindex, struct got_repository *repo,
7176 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7177 got_worktree_checkout_cb progress_cb, void *progress_arg,
7178 got_cancel_cb cancel_cb, void *cancel_arg)
7180 const struct got_error *err = NULL, *sync_err, *unlockerr;
7181 char *fileindex_path = NULL;
7182 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7184 err = get_fileindex_path(&fileindex_path, worktree);
7185 if (err)
7186 goto done;
7188 err = got_ref_resolve(&commit_id, repo, branch_ref);
7189 if (err)
7190 goto done;
7192 err = got_object_id_by_path(&tree_id, repo, commit_id,
7193 worktree->path_prefix);
7194 if (err)
7195 goto done;
7197 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7198 if (err)
7199 goto done;
7201 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7202 progress_cb, progress_arg, cancel_cb, cancel_arg);
7203 if (err)
7204 goto sync;
7206 err = got_ref_change_ref(base_branch_ref, commit_id);
7207 if (err)
7208 goto sync;
7210 err = got_ref_write(base_branch_ref, repo);
7211 if (err)
7212 goto sync;
7214 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7215 sync:
7216 sync_err = sync_fileindex(fileindex, fileindex_path);
7217 if (sync_err && err == NULL)
7218 err = sync_err;
7220 done:
7221 unlockerr = got_ref_unlock(branch_ref);
7222 if (unlockerr && err == NULL)
7223 err = unlockerr;
7224 got_ref_close(branch_ref);
7226 unlockerr = got_ref_unlock(base_branch_ref);
7227 if (unlockerr && err == NULL)
7228 err = unlockerr;
7229 got_ref_close(base_branch_ref);
7231 got_fileindex_free(fileindex);
7232 free(fileindex_path);
7233 free(tree_id);
7235 unlockerr = lock_worktree(worktree, LOCK_SH);
7236 if (unlockerr && err == NULL)
7237 err = unlockerr;
7238 return err;
7241 const struct got_error *
7242 got_worktree_integrate_abort(struct got_worktree *worktree,
7243 struct got_fileindex *fileindex, struct got_repository *repo,
7244 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7246 const struct got_error *err = NULL, *unlockerr = NULL;
7248 got_fileindex_free(fileindex);
7250 err = lock_worktree(worktree, LOCK_SH);
7252 unlockerr = got_ref_unlock(branch_ref);
7253 if (unlockerr && err == NULL)
7254 err = unlockerr;
7255 got_ref_close(branch_ref);
7257 unlockerr = got_ref_unlock(base_branch_ref);
7258 if (unlockerr && err == NULL)
7259 err = unlockerr;
7260 got_ref_close(base_branch_ref);
7262 return err;
7265 struct check_stage_ok_arg {
7266 struct got_object_id *head_commit_id;
7267 struct got_worktree *worktree;
7268 struct got_fileindex *fileindex;
7269 struct got_repository *repo;
7270 int have_changes;
7273 const struct got_error *
7274 check_stage_ok(void *arg, unsigned char status,
7275 unsigned char staged_status, const char *relpath,
7276 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7277 struct got_object_id *commit_id, int dirfd, const char *de_name)
7279 struct check_stage_ok_arg *a = arg;
7280 const struct got_error *err = NULL;
7281 struct got_fileindex_entry *ie;
7282 struct got_object_id base_commit_id;
7283 struct got_object_id *base_commit_idp = NULL;
7284 char *in_repo_path = NULL, *p;
7286 if (status == GOT_STATUS_UNVERSIONED ||
7287 status == GOT_STATUS_NO_CHANGE)
7288 return NULL;
7289 if (status == GOT_STATUS_NONEXISTENT)
7290 return got_error_set_errno(ENOENT, relpath);
7292 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7293 if (ie == NULL)
7294 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7296 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7297 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7298 relpath) == -1)
7299 return got_error_from_errno("asprintf");
7301 if (got_fileindex_entry_has_commit(ie)) {
7302 memcpy(base_commit_id.sha1, ie->commit_sha1,
7303 SHA1_DIGEST_LENGTH);
7304 base_commit_idp = &base_commit_id;
7307 if (status == GOT_STATUS_CONFLICT) {
7308 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7309 goto done;
7310 } else if (status != GOT_STATUS_ADD &&
7311 status != GOT_STATUS_MODIFY &&
7312 status != GOT_STATUS_DELETE) {
7313 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7314 goto done;
7317 a->have_changes = 1;
7319 p = in_repo_path;
7320 while (p[0] == '/')
7321 p++;
7322 err = check_out_of_date(p, status, staged_status,
7323 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7324 GOT_ERR_STAGE_OUT_OF_DATE);
7325 done:
7326 free(in_repo_path);
7327 return err;
7330 struct stage_path_arg {
7331 struct got_worktree *worktree;
7332 struct got_fileindex *fileindex;
7333 struct got_repository *repo;
7334 got_worktree_status_cb status_cb;
7335 void *status_arg;
7336 got_worktree_patch_cb patch_cb;
7337 void *patch_arg;
7338 int staged_something;
7339 int allow_bad_symlinks;
7342 static const struct got_error *
7343 stage_path(void *arg, unsigned char status,
7344 unsigned char staged_status, const char *relpath,
7345 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7346 struct got_object_id *commit_id, int dirfd, const char *de_name)
7348 struct stage_path_arg *a = arg;
7349 const struct got_error *err = NULL;
7350 struct got_fileindex_entry *ie;
7351 char *ondisk_path = NULL, *path_content = NULL;
7352 uint32_t stage;
7353 struct got_object_id *new_staged_blob_id = NULL;
7354 struct stat sb;
7356 if (status == GOT_STATUS_UNVERSIONED)
7357 return NULL;
7359 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7360 if (ie == NULL)
7361 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7363 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7364 relpath)== -1)
7365 return got_error_from_errno("asprintf");
7367 switch (status) {
7368 case GOT_STATUS_ADD:
7369 case GOT_STATUS_MODIFY:
7370 /* XXX could sb.st_mode be passed in by our caller? */
7371 if (lstat(ondisk_path, &sb) == -1) {
7372 err = got_error_from_errno2("lstat", ondisk_path);
7373 break;
7375 if (a->patch_cb) {
7376 if (status == GOT_STATUS_ADD) {
7377 int choice = GOT_PATCH_CHOICE_NONE;
7378 err = (*a->patch_cb)(&choice, a->patch_arg,
7379 status, ie->path, NULL, 1, 1);
7380 if (err)
7381 break;
7382 if (choice != GOT_PATCH_CHOICE_YES)
7383 break;
7384 } else {
7385 err = create_patched_content(&path_content, 0,
7386 staged_blob_id ? staged_blob_id : blob_id,
7387 ondisk_path, dirfd, de_name, ie->path,
7388 a->repo, a->patch_cb, a->patch_arg);
7389 if (err || path_content == NULL)
7390 break;
7393 err = got_object_blob_create(&new_staged_blob_id,
7394 path_content ? path_content : ondisk_path, a->repo);
7395 if (err)
7396 break;
7397 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7398 SHA1_DIGEST_LENGTH);
7399 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7400 stage = GOT_FILEIDX_STAGE_ADD;
7401 else
7402 stage = GOT_FILEIDX_STAGE_MODIFY;
7403 got_fileindex_entry_stage_set(ie, stage);
7404 if (S_ISLNK(sb.st_mode)) {
7405 int is_bad_symlink = 0;
7406 if (!a->allow_bad_symlinks) {
7407 char target_path[PATH_MAX];
7408 ssize_t target_len;
7409 target_len = readlink(ondisk_path, target_path,
7410 sizeof(target_path));
7411 if (target_len == -1) {
7412 err = got_error_from_errno2("readlink",
7413 ondisk_path);
7414 break;
7416 err = is_bad_symlink_target(&is_bad_symlink,
7417 target_path, target_len, ondisk_path,
7418 a->worktree->root_path);
7419 if (err)
7420 break;
7421 if (is_bad_symlink) {
7422 err = got_error_path(ondisk_path,
7423 GOT_ERR_BAD_SYMLINK);
7424 break;
7427 if (is_bad_symlink)
7428 got_fileindex_entry_staged_filetype_set(ie,
7429 GOT_FILEIDX_MODE_BAD_SYMLINK);
7430 else
7431 got_fileindex_entry_staged_filetype_set(ie,
7432 GOT_FILEIDX_MODE_SYMLINK);
7433 } else {
7434 got_fileindex_entry_staged_filetype_set(ie,
7435 GOT_FILEIDX_MODE_REGULAR_FILE);
7437 a->staged_something = 1;
7438 if (a->status_cb == NULL)
7439 break;
7440 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7441 get_staged_status(ie), relpath, blob_id,
7442 new_staged_blob_id, NULL, dirfd, de_name);
7443 break;
7444 case GOT_STATUS_DELETE:
7445 if (staged_status == GOT_STATUS_DELETE)
7446 break;
7447 if (a->patch_cb) {
7448 int choice = GOT_PATCH_CHOICE_NONE;
7449 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7450 ie->path, NULL, 1, 1);
7451 if (err)
7452 break;
7453 if (choice == GOT_PATCH_CHOICE_NO)
7454 break;
7455 if (choice != GOT_PATCH_CHOICE_YES) {
7456 err = got_error(GOT_ERR_PATCH_CHOICE);
7457 break;
7460 stage = GOT_FILEIDX_STAGE_DELETE;
7461 got_fileindex_entry_stage_set(ie, stage);
7462 a->staged_something = 1;
7463 if (a->status_cb == NULL)
7464 break;
7465 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7466 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7467 de_name);
7468 break;
7469 case GOT_STATUS_NO_CHANGE:
7470 break;
7471 case GOT_STATUS_CONFLICT:
7472 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7473 break;
7474 case GOT_STATUS_NONEXISTENT:
7475 err = got_error_set_errno(ENOENT, relpath);
7476 break;
7477 default:
7478 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7479 break;
7482 if (path_content && unlink(path_content) == -1 && err == NULL)
7483 err = got_error_from_errno2("unlink", path_content);
7484 free(path_content);
7485 free(ondisk_path);
7486 free(new_staged_blob_id);
7487 return err;
7490 const struct got_error *
7491 got_worktree_stage(struct got_worktree *worktree,
7492 struct got_pathlist_head *paths,
7493 got_worktree_status_cb status_cb, void *status_arg,
7494 got_worktree_patch_cb patch_cb, void *patch_arg,
7495 int allow_bad_symlinks, struct got_repository *repo)
7497 const struct got_error *err = NULL, *sync_err, *unlockerr;
7498 struct got_pathlist_entry *pe;
7499 struct got_fileindex *fileindex = NULL;
7500 char *fileindex_path = NULL;
7501 struct got_reference *head_ref = NULL;
7502 struct got_object_id *head_commit_id = NULL;
7503 struct check_stage_ok_arg oka;
7504 struct stage_path_arg spa;
7506 err = lock_worktree(worktree, LOCK_EX);
7507 if (err)
7508 return err;
7510 err = got_ref_open(&head_ref, repo,
7511 got_worktree_get_head_ref_name(worktree), 0);
7512 if (err)
7513 goto done;
7514 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7515 if (err)
7516 goto done;
7517 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7518 if (err)
7519 goto done;
7521 /* Check pre-conditions before staging anything. */
7522 oka.head_commit_id = head_commit_id;
7523 oka.worktree = worktree;
7524 oka.fileindex = fileindex;
7525 oka.repo = repo;
7526 oka.have_changes = 0;
7527 TAILQ_FOREACH(pe, paths, entry) {
7528 err = worktree_status(worktree, pe->path, fileindex, repo,
7529 check_stage_ok, &oka, NULL, NULL, 0, 0);
7530 if (err)
7531 goto done;
7533 if (!oka.have_changes) {
7534 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7535 goto done;
7538 spa.worktree = worktree;
7539 spa.fileindex = fileindex;
7540 spa.repo = repo;
7541 spa.patch_cb = patch_cb;
7542 spa.patch_arg = patch_arg;
7543 spa.status_cb = status_cb;
7544 spa.status_arg = status_arg;
7545 spa.staged_something = 0;
7546 spa.allow_bad_symlinks = allow_bad_symlinks;
7547 TAILQ_FOREACH(pe, paths, entry) {
7548 err = worktree_status(worktree, pe->path, fileindex, repo,
7549 stage_path, &spa, NULL, NULL, 0, 0);
7550 if (err)
7551 goto done;
7553 if (!spa.staged_something) {
7554 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7555 goto done;
7558 sync_err = sync_fileindex(fileindex, fileindex_path);
7559 if (sync_err && err == NULL)
7560 err = sync_err;
7561 done:
7562 if (head_ref)
7563 got_ref_close(head_ref);
7564 free(head_commit_id);
7565 free(fileindex_path);
7566 if (fileindex)
7567 got_fileindex_free(fileindex);
7568 unlockerr = lock_worktree(worktree, LOCK_SH);
7569 if (unlockerr && err == NULL)
7570 err = unlockerr;
7571 return err;
7574 struct unstage_path_arg {
7575 struct got_worktree *worktree;
7576 struct got_fileindex *fileindex;
7577 struct got_repository *repo;
7578 got_worktree_checkout_cb progress_cb;
7579 void *progress_arg;
7580 got_worktree_patch_cb patch_cb;
7581 void *patch_arg;
7584 static const struct got_error *
7585 create_unstaged_content(char **path_unstaged_content,
7586 char **path_new_staged_content, struct got_object_id *blob_id,
7587 struct got_object_id *staged_blob_id, const char *relpath,
7588 struct got_repository *repo,
7589 got_worktree_patch_cb patch_cb, void *patch_arg)
7591 const struct got_error *err, *free_err;
7592 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7593 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7594 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7595 struct got_diffreg_result *diffreg_result = NULL;
7596 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7597 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7599 *path_unstaged_content = NULL;
7600 *path_new_staged_content = NULL;
7602 err = got_object_id_str(&label1, blob_id);
7603 if (err)
7604 return err;
7605 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7606 if (err)
7607 goto done;
7609 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7610 if (err)
7611 goto done;
7613 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7614 if (err)
7615 goto done;
7617 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7618 if (err)
7619 goto done;
7621 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7622 if (err)
7623 goto done;
7625 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7626 if (err)
7627 goto done;
7629 err = got_diff_files(&diffreg_result, f1, label1, f2,
7630 path2, 3, 0, 1, NULL);
7631 if (err)
7632 goto done;
7634 err = got_opentemp_named(path_unstaged_content, &outfile,
7635 "got-unstaged-content");
7636 if (err)
7637 goto done;
7638 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7639 "got-new-staged-content");
7640 if (err)
7641 goto done;
7643 if (fseek(f1, 0L, SEEK_SET) == -1) {
7644 err = got_ferror(f1, GOT_ERR_IO);
7645 goto done;
7647 if (fseek(f2, 0L, SEEK_SET) == -1) {
7648 err = got_ferror(f2, GOT_ERR_IO);
7649 goto done;
7651 /* Count the number of actual changes in the diff result. */
7652 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7653 struct diff_chunk_context cc = {};
7654 diff_chunk_context_load_change(&cc, &nchunks_used,
7655 diffreg_result->result, n, 0);
7656 nchanges++;
7658 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7659 int choice;
7660 err = apply_or_reject_change(&choice, &nchunks_used,
7661 diffreg_result->result, n, relpath, f1, f2,
7662 &line_cur1, &line_cur2,
7663 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7664 if (err)
7665 goto done;
7666 if (choice == GOT_PATCH_CHOICE_YES)
7667 have_content = 1;
7668 else
7669 have_rejected_content = 1;
7670 if (choice == GOT_PATCH_CHOICE_QUIT)
7671 break;
7673 if (have_content || have_rejected_content)
7674 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7675 outfile, rejectfile);
7676 done:
7677 free(label1);
7678 if (blob)
7679 got_object_blob_close(blob);
7680 if (staged_blob)
7681 got_object_blob_close(staged_blob);
7682 free_err = got_diffreg_result_free(diffreg_result);
7683 if (free_err && err == NULL)
7684 err = free_err;
7685 if (f1 && fclose(f1) == EOF && err == NULL)
7686 err = got_error_from_errno2("fclose", path1);
7687 if (f2 && fclose(f2) == EOF && err == NULL)
7688 err = got_error_from_errno2("fclose", path2);
7689 if (outfile && fclose(outfile) == EOF && err == NULL)
7690 err = got_error_from_errno2("fclose", *path_unstaged_content);
7691 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7692 err = got_error_from_errno2("fclose", *path_new_staged_content);
7693 if (path1 && unlink(path1) == -1 && err == NULL)
7694 err = got_error_from_errno2("unlink", path1);
7695 if (path2 && unlink(path2) == -1 && err == NULL)
7696 err = got_error_from_errno2("unlink", path2);
7697 if (err || !have_content) {
7698 if (*path_unstaged_content &&
7699 unlink(*path_unstaged_content) == -1 && err == NULL)
7700 err = got_error_from_errno2("unlink",
7701 *path_unstaged_content);
7702 free(*path_unstaged_content);
7703 *path_unstaged_content = NULL;
7705 if (err || !have_content || !have_rejected_content) {
7706 if (*path_new_staged_content &&
7707 unlink(*path_new_staged_content) == -1 && err == NULL)
7708 err = got_error_from_errno2("unlink",
7709 *path_new_staged_content);
7710 free(*path_new_staged_content);
7711 *path_new_staged_content = NULL;
7713 free(path1);
7714 free(path2);
7715 return err;
7718 static const struct got_error *
7719 unstage_hunks(struct got_object_id *staged_blob_id,
7720 struct got_blob_object *blob_base,
7721 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7722 const char *ondisk_path, const char *label_orig,
7723 struct got_worktree *worktree, struct got_repository *repo,
7724 got_worktree_patch_cb patch_cb, void *patch_arg,
7725 got_worktree_checkout_cb progress_cb, void *progress_arg)
7727 const struct got_error *err = NULL;
7728 char *path_unstaged_content = NULL;
7729 char *path_new_staged_content = NULL;
7730 char *parent = NULL, *base_path = NULL;
7731 char *blob_base_path = NULL;
7732 struct got_object_id *new_staged_blob_id = NULL;
7733 FILE *f = NULL, *f_base = NULL;
7734 struct stat sb;
7736 err = create_unstaged_content(&path_unstaged_content,
7737 &path_new_staged_content, blob_id, staged_blob_id,
7738 ie->path, repo, patch_cb, patch_arg);
7739 if (err)
7740 return err;
7742 if (path_unstaged_content == NULL)
7743 return NULL;
7745 if (path_new_staged_content) {
7746 err = got_object_blob_create(&new_staged_blob_id,
7747 path_new_staged_content, repo);
7748 if (err)
7749 goto done;
7752 f = fopen(path_unstaged_content, "r");
7753 if (f == NULL) {
7754 err = got_error_from_errno2("fopen",
7755 path_unstaged_content);
7756 goto done;
7758 if (fstat(fileno(f), &sb) == -1) {
7759 err = got_error_from_errno2("fstat", path_unstaged_content);
7760 goto done;
7762 if (got_fileindex_entry_staged_filetype_get(ie) ==
7763 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7764 char link_target[PATH_MAX];
7765 size_t r;
7766 r = fread(link_target, 1, sizeof(link_target), f);
7767 if (r == 0 && ferror(f)) {
7768 err = got_error_from_errno("fread");
7769 goto done;
7771 if (r >= sizeof(link_target)) { /* should not happen */
7772 err = got_error(GOT_ERR_NO_SPACE);
7773 goto done;
7775 link_target[r] = '\0';
7776 err = merge_symlink(worktree, blob_base,
7777 ondisk_path, ie->path, label_orig, link_target,
7778 worktree->base_commit_id, repo, progress_cb,
7779 progress_arg);
7780 } else {
7781 int local_changes_subsumed;
7783 err = got_path_dirname(&parent, ondisk_path);
7784 if (err)
7785 return err;
7787 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
7788 parent) == -1) {
7789 err = got_error_from_errno("asprintf");
7790 base_path = NULL;
7791 goto done;
7794 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
7795 if (err)
7796 goto done;
7797 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
7798 blob_base);
7799 if (err)
7800 goto done;
7802 err = merge_file(&local_changes_subsumed, worktree,
7803 blob_base_path, ondisk_path, ie->path,
7804 got_fileindex_perms_to_st(ie),
7805 path_unstaged_content, label_orig, "unstaged",
7806 repo, progress_cb, progress_arg);
7808 if (err)
7809 goto done;
7811 if (new_staged_blob_id) {
7812 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7813 SHA1_DIGEST_LENGTH);
7814 } else {
7815 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7816 got_fileindex_entry_staged_filetype_set(ie, 0);
7818 done:
7819 free(new_staged_blob_id);
7820 if (path_unstaged_content &&
7821 unlink(path_unstaged_content) == -1 && err == NULL)
7822 err = got_error_from_errno2("unlink", path_unstaged_content);
7823 if (path_new_staged_content &&
7824 unlink(path_new_staged_content) == -1 && err == NULL)
7825 err = got_error_from_errno2("unlink", path_new_staged_content);
7826 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
7827 err = got_error_from_errno2("unlink", blob_base_path);
7828 if (f_base && fclose(f_base) == EOF && err == NULL)
7829 err = got_error_from_errno2("fclose", path_unstaged_content);
7830 if (f && fclose(f) == EOF && err == NULL)
7831 err = got_error_from_errno2("fclose", path_unstaged_content);
7832 free(path_unstaged_content);
7833 free(path_new_staged_content);
7834 free(blob_base_path);
7835 free(parent);
7836 free(base_path);
7837 return err;
7840 static const struct got_error *
7841 unstage_path(void *arg, unsigned char status,
7842 unsigned char staged_status, const char *relpath,
7843 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7844 struct got_object_id *commit_id, int dirfd, const char *de_name)
7846 const struct got_error *err = NULL;
7847 struct unstage_path_arg *a = arg;
7848 struct got_fileindex_entry *ie;
7849 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7850 char *ondisk_path = NULL;
7851 char *id_str = NULL, *label_orig = NULL;
7852 int local_changes_subsumed;
7853 struct stat sb;
7855 if (staged_status != GOT_STATUS_ADD &&
7856 staged_status != GOT_STATUS_MODIFY &&
7857 staged_status != GOT_STATUS_DELETE)
7858 return NULL;
7860 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7861 if (ie == NULL)
7862 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7864 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7865 == -1)
7866 return got_error_from_errno("asprintf");
7868 err = got_object_id_str(&id_str,
7869 commit_id ? commit_id : a->worktree->base_commit_id);
7870 if (err)
7871 goto done;
7872 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7873 id_str) == -1) {
7874 err = got_error_from_errno("asprintf");
7875 goto done;
7878 switch (staged_status) {
7879 case GOT_STATUS_MODIFY:
7880 err = got_object_open_as_blob(&blob_base, a->repo,
7881 blob_id, 8192);
7882 if (err)
7883 break;
7884 /* fall through */
7885 case GOT_STATUS_ADD:
7886 if (a->patch_cb) {
7887 if (staged_status == GOT_STATUS_ADD) {
7888 int choice = GOT_PATCH_CHOICE_NONE;
7889 err = (*a->patch_cb)(&choice, a->patch_arg,
7890 staged_status, ie->path, NULL, 1, 1);
7891 if (err)
7892 break;
7893 if (choice != GOT_PATCH_CHOICE_YES)
7894 break;
7895 } else {
7896 err = unstage_hunks(staged_blob_id,
7897 blob_base, blob_id, ie, ondisk_path,
7898 label_orig, a->worktree, a->repo,
7899 a->patch_cb, a->patch_arg,
7900 a->progress_cb, a->progress_arg);
7901 break; /* Done with this file. */
7904 err = got_object_open_as_blob(&blob_staged, a->repo,
7905 staged_blob_id, 8192);
7906 if (err)
7907 break;
7908 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7909 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7910 case GOT_FILEIDX_MODE_REGULAR_FILE:
7911 err = merge_blob(&local_changes_subsumed, a->worktree,
7912 blob_base, ondisk_path, relpath,
7913 got_fileindex_perms_to_st(ie), label_orig,
7914 blob_staged, commit_id ? commit_id :
7915 a->worktree->base_commit_id, a->repo,
7916 a->progress_cb, a->progress_arg);
7917 break;
7918 case GOT_FILEIDX_MODE_SYMLINK:
7919 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7920 char *staged_target;
7921 err = got_object_blob_read_to_str(
7922 &staged_target, blob_staged);
7923 if (err)
7924 goto done;
7925 err = merge_symlink(a->worktree, blob_base,
7926 ondisk_path, relpath, label_orig,
7927 staged_target, commit_id ? commit_id :
7928 a->worktree->base_commit_id,
7929 a->repo, a->progress_cb, a->progress_arg);
7930 free(staged_target);
7931 } else {
7932 err = merge_blob(&local_changes_subsumed,
7933 a->worktree, blob_base, ondisk_path,
7934 relpath, got_fileindex_perms_to_st(ie),
7935 label_orig, blob_staged,
7936 commit_id ? commit_id :
7937 a->worktree->base_commit_id, a->repo,
7938 a->progress_cb, a->progress_arg);
7940 break;
7941 default:
7942 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7943 break;
7945 if (err == NULL) {
7946 got_fileindex_entry_stage_set(ie,
7947 GOT_FILEIDX_STAGE_NONE);
7948 got_fileindex_entry_staged_filetype_set(ie, 0);
7950 break;
7951 case GOT_STATUS_DELETE:
7952 if (a->patch_cb) {
7953 int choice = GOT_PATCH_CHOICE_NONE;
7954 err = (*a->patch_cb)(&choice, a->patch_arg,
7955 staged_status, ie->path, NULL, 1, 1);
7956 if (err)
7957 break;
7958 if (choice == GOT_PATCH_CHOICE_NO)
7959 break;
7960 if (choice != GOT_PATCH_CHOICE_YES) {
7961 err = got_error(GOT_ERR_PATCH_CHOICE);
7962 break;
7965 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7966 got_fileindex_entry_staged_filetype_set(ie, 0);
7967 err = get_file_status(&status, &sb, ie, ondisk_path,
7968 dirfd, de_name, a->repo);
7969 if (err)
7970 break;
7971 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7972 break;
7974 done:
7975 free(ondisk_path);
7976 if (blob_base)
7977 got_object_blob_close(blob_base);
7978 if (blob_staged)
7979 got_object_blob_close(blob_staged);
7980 free(id_str);
7981 free(label_orig);
7982 return err;
7985 const struct got_error *
7986 got_worktree_unstage(struct got_worktree *worktree,
7987 struct got_pathlist_head *paths,
7988 got_worktree_checkout_cb progress_cb, void *progress_arg,
7989 got_worktree_patch_cb patch_cb, void *patch_arg,
7990 struct got_repository *repo)
7992 const struct got_error *err = NULL, *sync_err, *unlockerr;
7993 struct got_pathlist_entry *pe;
7994 struct got_fileindex *fileindex = NULL;
7995 char *fileindex_path = NULL;
7996 struct unstage_path_arg upa;
7998 err = lock_worktree(worktree, LOCK_EX);
7999 if (err)
8000 return err;
8002 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8003 if (err)
8004 goto done;
8006 upa.worktree = worktree;
8007 upa.fileindex = fileindex;
8008 upa.repo = repo;
8009 upa.progress_cb = progress_cb;
8010 upa.progress_arg = progress_arg;
8011 upa.patch_cb = patch_cb;
8012 upa.patch_arg = patch_arg;
8013 TAILQ_FOREACH(pe, paths, entry) {
8014 err = worktree_status(worktree, pe->path, fileindex, repo,
8015 unstage_path, &upa, NULL, NULL, 0, 0);
8016 if (err)
8017 goto done;
8020 sync_err = sync_fileindex(fileindex, fileindex_path);
8021 if (sync_err && err == NULL)
8022 err = sync_err;
8023 done:
8024 free(fileindex_path);
8025 if (fileindex)
8026 got_fileindex_free(fileindex);
8027 unlockerr = lock_worktree(worktree, LOCK_SH);
8028 if (unlockerr && err == NULL)
8029 err = unlockerr;
8030 return err;
8033 struct report_file_info_arg {
8034 struct got_worktree *worktree;
8035 got_worktree_path_info_cb info_cb;
8036 void *info_arg;
8037 struct got_pathlist_head *paths;
8038 got_cancel_cb cancel_cb;
8039 void *cancel_arg;
8042 static const struct got_error *
8043 report_file_info(void *arg, struct got_fileindex_entry *ie)
8045 struct report_file_info_arg *a = arg;
8046 struct got_pathlist_entry *pe;
8047 struct got_object_id blob_id, staged_blob_id, commit_id;
8048 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8049 struct got_object_id *commit_idp = NULL;
8050 int stage;
8052 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8053 return got_error(GOT_ERR_CANCELLED);
8055 TAILQ_FOREACH(pe, a->paths, entry) {
8056 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8057 got_path_is_child(ie->path, pe->path, pe->path_len))
8058 break;
8060 if (pe == NULL) /* not found */
8061 return NULL;
8063 if (got_fileindex_entry_has_blob(ie)) {
8064 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8065 blob_idp = &blob_id;
8067 stage = got_fileindex_entry_stage_get(ie);
8068 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8069 stage == GOT_FILEIDX_STAGE_ADD) {
8070 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8071 SHA1_DIGEST_LENGTH);
8072 staged_blob_idp = &staged_blob_id;
8075 if (got_fileindex_entry_has_commit(ie)) {
8076 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8077 commit_idp = &commit_id;
8080 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8081 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8084 const struct got_error *
8085 got_worktree_path_info(struct got_worktree *worktree,
8086 struct got_pathlist_head *paths,
8087 got_worktree_path_info_cb info_cb, void *info_arg,
8088 got_cancel_cb cancel_cb, void *cancel_arg)
8091 const struct got_error *err = NULL, *unlockerr;
8092 struct got_fileindex *fileindex = NULL;
8093 char *fileindex_path = NULL;
8094 struct report_file_info_arg arg;
8096 err = lock_worktree(worktree, LOCK_SH);
8097 if (err)
8098 return err;
8100 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8101 if (err)
8102 goto done;
8104 arg.worktree = worktree;
8105 arg.info_cb = info_cb;
8106 arg.info_arg = info_arg;
8107 arg.paths = paths;
8108 arg.cancel_cb = cancel_cb;
8109 arg.cancel_arg = cancel_arg;
8110 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8111 &arg);
8112 done:
8113 free(fileindex_path);
8114 if (fileindex)
8115 got_fileindex_free(fileindex);
8116 unlockerr = lock_worktree(worktree, LOCK_UN);
8117 if (unlockerr && err == NULL)
8118 err = unlockerr;
8119 return err;