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 const struct got_error *close_err = got_repo_close(repo);
441 if (err == NULL)
442 err = close_err;
444 free(path_got);
445 free(path_lock);
446 free(base_commit_id_str);
447 free(uuidstr);
448 free(formatstr);
449 if (err) {
450 if (fd != -1)
451 close(fd);
452 if (*worktree != NULL)
453 got_worktree_close(*worktree);
454 *worktree = NULL;
455 } else
456 (*worktree)->lockfd = fd;
458 return err;
461 const struct got_error *
462 got_worktree_open(struct got_worktree **worktree, const char *path)
464 const struct got_error *err = NULL;
465 char *worktree_path;
467 worktree_path = strdup(path);
468 if (worktree_path == NULL)
469 return got_error_from_errno("strdup");
471 for (;;) {
472 char *parent_path;
474 err = open_worktree(worktree, worktree_path);
475 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
476 free(worktree_path);
477 return err;
479 if (*worktree) {
480 free(worktree_path);
481 return NULL;
483 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
484 break;
485 err = got_path_dirname(&parent_path, worktree_path);
486 if (err) {
487 if (err->code != GOT_ERR_BAD_PATH) {
488 free(worktree_path);
489 return err;
491 break;
493 free(worktree_path);
494 worktree_path = parent_path;
497 free(worktree_path);
498 return got_error(GOT_ERR_NOT_WORKTREE);
501 const struct got_error *
502 got_worktree_close(struct got_worktree *worktree)
504 const struct got_error *err = NULL;
506 if (worktree->lockfd != -1) {
507 if (close(worktree->lockfd) == -1)
508 err = got_error_from_errno2("close",
509 got_worktree_get_root_path(worktree));
511 if (close(worktree->root_fd) == -1 && err == NULL)
512 err = got_error_from_errno2("close",
513 got_worktree_get_root_path(worktree));
514 free(worktree->repo_path);
515 free(worktree->path_prefix);
516 free(worktree->base_commit_id);
517 free(worktree->head_ref_name);
518 free(worktree->root_path);
519 free(worktree->gotconfig_path);
520 got_gotconfig_free(worktree->gotconfig);
521 free(worktree);
522 return err;
525 const char *
526 got_worktree_get_root_path(struct got_worktree *worktree)
528 return worktree->root_path;
531 const char *
532 got_worktree_get_repo_path(struct got_worktree *worktree)
534 return worktree->repo_path;
536 const char *
537 got_worktree_get_path_prefix(struct got_worktree *worktree)
539 return worktree->path_prefix;
542 const struct got_error *
543 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
544 const char *path_prefix)
546 char *absprefix = NULL;
548 if (!got_path_is_absolute(path_prefix)) {
549 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
550 return got_error_from_errno("asprintf");
552 *match = (strcmp(absprefix ? absprefix : path_prefix,
553 worktree->path_prefix) == 0);
554 free(absprefix);
555 return NULL;
558 const char *
559 got_worktree_get_head_ref_name(struct got_worktree *worktree)
561 return worktree->head_ref_name;
564 const struct got_error *
565 got_worktree_set_head_ref(struct got_worktree *worktree,
566 struct got_reference *head_ref)
568 const struct got_error *err = NULL;
569 char *path_got = NULL, *head_ref_name = NULL;
571 if (asprintf(&path_got, "%s/%s", worktree->root_path,
572 GOT_WORKTREE_GOT_DIR) == -1) {
573 err = got_error_from_errno("asprintf");
574 path_got = NULL;
575 goto done;
578 head_ref_name = strdup(got_ref_get_name(head_ref));
579 if (head_ref_name == NULL) {
580 err = got_error_from_errno("strdup");
581 goto done;
584 err = write_head_ref(path_got, head_ref);
585 if (err)
586 goto done;
588 free(worktree->head_ref_name);
589 worktree->head_ref_name = head_ref_name;
590 done:
591 free(path_got);
592 if (err)
593 free(head_ref_name);
594 return err;
597 struct got_object_id *
598 got_worktree_get_base_commit_id(struct got_worktree *worktree)
600 return worktree->base_commit_id;
603 const struct got_error *
604 got_worktree_set_base_commit_id(struct got_worktree *worktree,
605 struct got_repository *repo, struct got_object_id *commit_id)
607 const struct got_error *err;
608 struct got_object *obj = NULL;
609 char *id_str = NULL;
610 char *path_got = NULL;
612 if (asprintf(&path_got, "%s/%s", worktree->root_path,
613 GOT_WORKTREE_GOT_DIR) == -1) {
614 err = got_error_from_errno("asprintf");
615 path_got = NULL;
616 goto done;
619 err = got_object_open(&obj, repo, commit_id);
620 if (err)
621 return err;
623 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
624 err = got_error(GOT_ERR_OBJ_TYPE);
625 goto done;
628 /* Record our base commit. */
629 err = got_object_id_str(&id_str, commit_id);
630 if (err)
631 goto done;
632 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
633 if (err)
634 goto done;
636 free(worktree->base_commit_id);
637 worktree->base_commit_id = got_object_id_dup(commit_id);
638 if (worktree->base_commit_id == NULL) {
639 err = got_error_from_errno("got_object_id_dup");
640 goto done;
642 done:
643 if (obj)
644 got_object_close(obj);
645 free(id_str);
646 free(path_got);
647 return err;
650 const struct got_gotconfig *
651 got_worktree_get_gotconfig(struct got_worktree *worktree)
653 return worktree->gotconfig;
656 static const struct got_error *
657 lock_worktree(struct got_worktree *worktree, int operation)
659 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
660 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
661 : got_error_from_errno2("flock",
662 got_worktree_get_root_path(worktree)));
663 return NULL;
666 static const struct got_error *
667 add_dir_on_disk(struct got_worktree *worktree, const char *path)
669 const struct got_error *err = NULL;
670 char *abspath;
672 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
673 return got_error_from_errno("asprintf");
675 err = got_path_mkdir(abspath);
676 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
677 struct stat sb;
678 err = NULL;
679 if (lstat(abspath, &sb) == -1) {
680 err = got_error_from_errno2("lstat", abspath);
681 } else if (!S_ISDIR(sb.st_mode)) {
682 /* TODO directory is obstructed; do something */
683 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
686 free(abspath);
687 return err;
690 static const struct got_error *
691 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
693 const struct got_error *err = NULL;
694 uint8_t fbuf1[8192];
695 uint8_t fbuf2[8192];
696 size_t flen1 = 0, flen2 = 0;
698 *same = 1;
700 for (;;) {
701 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
702 if (flen1 == 0 && ferror(f1)) {
703 err = got_error_from_errno("fread");
704 break;
706 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
707 if (flen2 == 0 && ferror(f2)) {
708 err = got_error_from_errno("fread");
709 break;
711 if (flen1 == 0) {
712 if (flen2 != 0)
713 *same = 0;
714 break;
715 } else if (flen2 == 0) {
716 if (flen1 != 0)
717 *same = 0;
718 break;
719 } else if (flen1 == flen2) {
720 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
721 *same = 0;
722 break;
724 } else {
725 *same = 0;
726 break;
730 return err;
733 static const struct got_error *
734 check_files_equal(int *same, FILE *f1, FILE *f2)
736 struct stat sb;
737 size_t size1, size2;
739 *same = 1;
741 if (fstat(fileno(f1), &sb) != 0)
742 return got_error_from_errno("fstat");
743 size1 = sb.st_size;
745 if (fstat(fileno(f2), &sb) != 0)
746 return got_error_from_errno("fstat");
747 size2 = sb.st_size;
749 if (size1 != size2) {
750 *same = 0;
751 return NULL;
754 if (fseek(f1, 0L, SEEK_SET) == -1)
755 return got_ferror(f1, GOT_ERR_IO);
756 if (fseek(f2, 0L, SEEK_SET) == -1)
757 return got_ferror(f2, GOT_ERR_IO);
759 return check_file_contents_equal(same, f1, f2);
762 /*
763 * Perform a 3-way merge where the file f_orig acts as the common
764 * ancestor, the file f_deriv acts as the first derived version,
765 * and the file f_deriv2 acts as the second derived version.
766 * The merge result will be written to a new file at ondisk_path; any
767 * existing file at this path will be replaced.
768 */
769 static const struct got_error *
770 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
771 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
772 const char *path, uint16_t st_mode,
773 const char *label_orig, const char *label_deriv, const char *label_deriv2,
774 enum got_diff_algorithm diff_algo, struct got_repository *repo,
775 got_worktree_checkout_cb progress_cb, void *progress_arg)
777 const struct got_error *err = NULL;
778 int merged_fd = -1;
779 FILE *f_merged = NULL;
780 char *merged_path = NULL, *base_path = NULL;
781 int overlapcnt = 0;
782 char *parent = NULL;
784 *local_changes_subsumed = 0;
786 err = got_path_dirname(&parent, ondisk_path);
787 if (err)
788 return err;
790 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
791 err = got_error_from_errno("asprintf");
792 goto done;
795 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
796 if (err)
797 goto done;
799 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
800 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
801 if (err)
802 goto done;
804 err = (*progress_cb)(progress_arg,
805 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
806 if (err)
807 goto done;
809 if (fsync(merged_fd) != 0) {
810 err = got_error_from_errno("fsync");
811 goto done;
814 f_merged = fdopen(merged_fd, "r");
815 if (f_merged == NULL) {
816 err = got_error_from_errno("fdopen");
817 goto done;
819 merged_fd = -1;
821 /* Check if a clean merge has subsumed all local changes. */
822 if (overlapcnt == 0) {
823 err = check_files_equal(local_changes_subsumed, f_deriv,
824 f_merged);
825 if (err)
826 goto done;
829 if (fchmod(fileno(f_merged), st_mode) != 0) {
830 err = got_error_from_errno2("fchmod", merged_path);
831 goto done;
834 if (rename(merged_path, ondisk_path) != 0) {
835 err = got_error_from_errno3("rename", merged_path,
836 ondisk_path);
837 goto done;
839 done:
840 if (err) {
841 if (merged_path)
842 unlink(merged_path);
844 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
845 err = got_error_from_errno("close");
846 if (f_merged && fclose(f_merged) == EOF && err == NULL)
847 err = got_error_from_errno("fclose");
848 free(merged_path);
849 free(base_path);
850 free(parent);
851 return err;
854 static const struct got_error *
855 update_symlink(const char *ondisk_path, const char *target_path,
856 size_t target_len)
858 /* This is not atomic but matches what 'ln -sf' does. */
859 if (unlink(ondisk_path) == -1)
860 return got_error_from_errno2("unlink", ondisk_path);
861 if (symlink(target_path, ondisk_path) == -1)
862 return got_error_from_errno3("symlink", target_path,
863 ondisk_path);
864 return NULL;
867 /*
868 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
869 * in the work tree with a file that contains conflict markers and the
870 * conflicting target paths of the original version, a "derived version"
871 * of a symlink from an incoming change, and a local version of the symlink.
873 * The original versions's target path can be NULL if it is not available,
874 * such as if both derived versions added a new symlink at the same path.
876 * The incoming derived symlink target is NULL in case the incoming change
877 * has deleted this symlink.
878 */
879 static const struct got_error *
880 install_symlink_conflict(const char *deriv_target,
881 struct got_object_id *deriv_base_commit_id, const char *orig_target,
882 const char *label_orig, const char *local_target, const char *ondisk_path)
884 const struct got_error *err;
885 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
886 FILE *f = NULL;
888 err = got_object_id_str(&id_str, deriv_base_commit_id);
889 if (err)
890 return got_error_from_errno("asprintf");
892 if (asprintf(&label_deriv, "%s: commit %s",
893 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
894 err = got_error_from_errno("asprintf");
895 goto done;
898 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
899 if (err)
900 goto done;
902 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
903 err = got_error_from_errno2("fchmod", path);
904 goto done;
907 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
908 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
909 deriv_target ? deriv_target : "(symlink was deleted)",
910 orig_target ? label_orig : "",
911 orig_target ? "\n" : "",
912 orig_target ? orig_target : "",
913 orig_target ? "\n" : "",
914 GOT_DIFF_CONFLICT_MARKER_SEP,
915 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
916 err = got_error_from_errno2("fprintf", path);
917 goto done;
920 if (unlink(ondisk_path) == -1) {
921 err = got_error_from_errno2("unlink", ondisk_path);
922 goto done;
924 if (rename(path, ondisk_path) == -1) {
925 err = got_error_from_errno3("rename", path, ondisk_path);
926 goto done;
928 done:
929 if (f != NULL && fclose(f) == EOF && err == NULL)
930 err = got_error_from_errno2("fclose", path);
931 free(path);
932 free(id_str);
933 free(label_deriv);
934 return err;
937 /* forward declaration */
938 static const struct got_error *
939 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
940 const char *, const char *, uint16_t, const char *,
941 struct got_blob_object *, struct got_object_id *,
942 struct got_repository *, got_worktree_checkout_cb, void *);
944 /*
945 * Merge a symlink into the work tree, where blob_orig acts as the common
946 * ancestor, deriv_target is the link target of the first derived version,
947 * and the symlink on disk acts as the second derived version.
948 * Assume that contents of both blobs represent symlinks.
949 */
950 static const struct got_error *
951 merge_symlink(struct got_worktree *worktree,
952 struct got_blob_object *blob_orig, const char *ondisk_path,
953 const char *path, const char *label_orig, const char *deriv_target,
954 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
955 got_worktree_checkout_cb progress_cb, void *progress_arg)
957 const struct got_error *err = NULL;
958 char *ancestor_target = NULL;
959 struct stat sb;
960 ssize_t ondisk_len, deriv_len;
961 char ondisk_target[PATH_MAX];
962 int have_local_change = 0;
963 int have_incoming_change = 0;
965 if (lstat(ondisk_path, &sb) == -1)
966 return got_error_from_errno2("lstat", ondisk_path);
968 ondisk_len = readlink(ondisk_path, ondisk_target,
969 sizeof(ondisk_target));
970 if (ondisk_len == -1) {
971 err = got_error_from_errno2("readlink",
972 ondisk_path);
973 goto done;
975 ondisk_target[ondisk_len] = '\0';
977 if (blob_orig) {
978 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
979 if (err)
980 goto done;
983 if (ancestor_target == NULL ||
984 (ondisk_len != strlen(ancestor_target) ||
985 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
986 have_local_change = 1;
988 deriv_len = strlen(deriv_target);
989 if (ancestor_target == NULL ||
990 (deriv_len != strlen(ancestor_target) ||
991 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
992 have_incoming_change = 1;
994 if (!have_local_change && !have_incoming_change) {
995 if (ancestor_target) {
996 /* Both sides made the same change. */
997 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
998 path);
999 } else if (deriv_len == ondisk_len &&
1000 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1001 /* Both sides added the same symlink. */
1002 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1003 path);
1004 } else {
1005 /* Both sides added symlinks which don't match. */
1006 err = install_symlink_conflict(deriv_target,
1007 deriv_base_commit_id, ancestor_target,
1008 label_orig, ondisk_target, ondisk_path);
1009 if (err)
1010 goto done;
1011 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1012 path);
1014 } else if (!have_local_change && have_incoming_change) {
1015 /* Apply the incoming change. */
1016 err = update_symlink(ondisk_path, deriv_target,
1017 strlen(deriv_target));
1018 if (err)
1019 goto done;
1020 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1021 } else if (have_local_change && have_incoming_change) {
1022 if (deriv_len == ondisk_len &&
1023 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1024 /* Both sides made the same change. */
1025 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1026 path);
1027 } else {
1028 err = install_symlink_conflict(deriv_target,
1029 deriv_base_commit_id, ancestor_target, label_orig,
1030 ondisk_target, ondisk_path);
1031 if (err)
1032 goto done;
1033 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1034 path);
1038 done:
1039 free(ancestor_target);
1040 return err;
1043 static const struct got_error *
1044 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
1046 const struct got_error *err = NULL;
1047 char target_path[PATH_MAX];
1048 ssize_t target_len;
1049 size_t n;
1050 FILE *f;
1052 *outfile = NULL;
1054 f = got_opentemp();
1055 if (f == NULL)
1056 return got_error_from_errno("got_opentemp");
1057 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
1058 if (target_len == -1) {
1059 err = got_error_from_errno2("readlink", ondisk_path);
1060 goto done;
1062 n = fwrite(target_path, 1, target_len, f);
1063 if (n != target_len) {
1064 err = got_ferror(f, GOT_ERR_IO);
1065 goto done;
1067 if (fflush(f) == EOF) {
1068 err = got_error_from_errno("fflush");
1069 goto done;
1071 if (fseek(f, 0L, SEEK_SET) == -1) {
1072 err = got_ferror(f, GOT_ERR_IO);
1073 goto done;
1075 done:
1076 if (err)
1077 fclose(f);
1078 else
1079 *outfile = f;
1080 return err;
1084 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1085 * blob_deriv acts as the first derived version, and the file on disk
1086 * acts as the second derived version.
1088 static const struct got_error *
1089 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1090 struct got_blob_object *blob_orig, const char *ondisk_path,
1091 const char *path, uint16_t st_mode, const char *label_orig,
1092 struct got_blob_object *blob_deriv,
1093 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1094 got_worktree_checkout_cb progress_cb, void *progress_arg)
1096 const struct got_error *err = NULL;
1097 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
1098 char *blob_orig_path = NULL;
1099 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1100 char *label_deriv = NULL, *parent = NULL;
1102 *local_changes_subsumed = 0;
1104 err = got_path_dirname(&parent, ondisk_path);
1105 if (err)
1106 return err;
1108 if (blob_orig) {
1109 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1110 parent) == -1) {
1111 err = got_error_from_errno("asprintf");
1112 base_path = NULL;
1113 goto done;
1116 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
1117 if (err)
1118 goto done;
1119 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1120 blob_orig);
1121 if (err)
1122 goto done;
1123 free(base_path);
1124 } else {
1126 * No common ancestor exists. This is an "add vs add" conflict
1127 * and we simply use an empty ancestor file to make both files
1128 * appear in the merged result in their entirety.
1130 f_orig = got_opentemp();
1131 if (f_orig == NULL) {
1132 err = got_error_from_errno("got_opentemp");
1133 goto done;
1137 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1138 err = got_error_from_errno("asprintf");
1139 base_path = NULL;
1140 goto done;
1143 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1144 if (err)
1145 goto done;
1146 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1147 blob_deriv);
1148 if (err)
1149 goto done;
1151 err = got_object_id_str(&id_str, deriv_base_commit_id);
1152 if (err)
1153 goto done;
1154 if (asprintf(&label_deriv, "%s: commit %s",
1155 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1156 err = got_error_from_errno("asprintf");
1157 goto done;
1161 * In order the run a 3-way merge with a symlink we copy the symlink's
1162 * target path into a temporary file and use that file with diff3.
1164 if (S_ISLNK(st_mode)) {
1165 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1166 if (err)
1167 goto done;
1168 } else {
1169 int fd;
1170 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
1171 if (fd == -1) {
1172 err = got_error_from_errno2("open", ondisk_path);
1173 goto done;
1175 f_deriv2 = fdopen(fd, "r");
1176 if (f_deriv2 == NULL) {
1177 err = got_error_from_errno2("fdopen", ondisk_path);
1178 close(fd);
1179 goto done;
1183 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1184 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1185 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1186 done:
1187 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1188 err = got_error_from_errno("fclose");
1189 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1190 err = got_error_from_errno("fclose");
1191 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1192 err = got_error_from_errno("fclose");
1193 free(base_path);
1194 if (blob_orig_path) {
1195 unlink(blob_orig_path);
1196 free(blob_orig_path);
1198 if (blob_deriv_path) {
1199 unlink(blob_deriv_path);
1200 free(blob_deriv_path);
1202 free(id_str);
1203 free(label_deriv);
1204 free(parent);
1205 return err;
1208 static const struct got_error *
1209 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1210 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1211 int wt_fd, const char *path, struct got_object_id *blob_id)
1213 const struct got_error *err = NULL;
1214 struct got_fileindex_entry *new_ie;
1216 *new_iep = NULL;
1218 err = got_fileindex_entry_alloc(&new_ie, path);
1219 if (err)
1220 return err;
1222 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1223 blob_id->sha1, base_commit_id->sha1, 1);
1224 if (err)
1225 goto done;
1227 err = got_fileindex_entry_add(fileindex, new_ie);
1228 done:
1229 if (err)
1230 got_fileindex_entry_free(new_ie);
1231 else
1232 *new_iep = new_ie;
1233 return err;
1236 static mode_t
1237 get_ondisk_perms(int executable, mode_t st_mode)
1239 mode_t xbits = S_IXUSR;
1241 if (executable) {
1242 /* Map read bits to execute bits. */
1243 if (st_mode & S_IRGRP)
1244 xbits |= S_IXGRP;
1245 if (st_mode & S_IROTH)
1246 xbits |= S_IXOTH;
1247 return st_mode | xbits;
1250 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1253 /* forward declaration */
1254 static const struct got_error *
1255 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1256 const char *path, mode_t te_mode, mode_t st_mode,
1257 struct got_blob_object *blob, int restoring_missing_file,
1258 int reverting_versioned_file, int installing_bad_symlink,
1259 int path_is_unversioned, struct got_repository *repo,
1260 got_worktree_checkout_cb progress_cb, void *progress_arg);
1263 * This function assumes that the provided symlink target points at a
1264 * safe location in the work tree!
1266 static const struct got_error *
1267 replace_existing_symlink(int *did_something, const char *ondisk_path,
1268 const char *target_path, size_t target_len)
1270 const struct got_error *err = NULL;
1271 ssize_t elen;
1272 char etarget[PATH_MAX];
1273 int fd;
1275 *did_something = 0;
1278 * "Bad" symlinks (those pointing outside the work tree or into the
1279 * .got directory) are installed in the work tree as a regular file
1280 * which contains the bad symlink target path.
1281 * The new symlink target has already been checked for safety by our
1282 * caller. If we can successfully open a regular file then we simply
1283 * replace this file with a symlink below.
1285 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1286 if (fd == -1) {
1287 if (!got_err_open_nofollow_on_symlink())
1288 return got_error_from_errno2("open", ondisk_path);
1290 /* We are updating an existing on-disk symlink. */
1291 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1292 if (elen == -1)
1293 return got_error_from_errno2("readlink", ondisk_path);
1295 if (elen == target_len &&
1296 memcmp(etarget, target_path, target_len) == 0)
1297 return NULL; /* nothing to do */
1300 *did_something = 1;
1301 err = update_symlink(ondisk_path, target_path, target_len);
1302 if (fd != -1 && close(fd) == -1 && err == NULL)
1303 err = got_error_from_errno2("close", ondisk_path);
1304 return err;
1307 static const struct got_error *
1308 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1309 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1311 const struct got_error *err = NULL;
1312 char canonpath[PATH_MAX];
1313 char *path_got = NULL;
1315 *is_bad_symlink = 0;
1317 if (target_len >= sizeof(canonpath)) {
1318 *is_bad_symlink = 1;
1319 return NULL;
1323 * We do not use realpath(3) to resolve the symlink's target
1324 * path because we don't want to resolve symlinks recursively.
1325 * Instead we make the path absolute and then canonicalize it.
1326 * Relative symlink target lookup should begin at the directory
1327 * in which the blob object is being installed.
1329 if (!got_path_is_absolute(target_path)) {
1330 char *abspath, *parent;
1331 err = got_path_dirname(&parent, ondisk_path);
1332 if (err)
1333 return err;
1334 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1335 free(parent);
1336 return got_error_from_errno("asprintf");
1338 free(parent);
1339 if (strlen(abspath) >= sizeof(canonpath)) {
1340 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1341 free(abspath);
1342 return err;
1344 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1345 free(abspath);
1346 if (err)
1347 return err;
1348 } else {
1349 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1350 if (err)
1351 return err;
1354 /* Only allow symlinks pointing at paths within the work tree. */
1355 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1356 *is_bad_symlink = 1;
1357 return NULL;
1360 /* Do not allow symlinks pointing into the .got directory. */
1361 if (asprintf(&path_got, "%s/%s", wtroot_path,
1362 GOT_WORKTREE_GOT_DIR) == -1)
1363 return got_error_from_errno("asprintf");
1364 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1365 *is_bad_symlink = 1;
1367 free(path_got);
1368 return NULL;
1371 static const struct got_error *
1372 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1373 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1374 int restoring_missing_file, int reverting_versioned_file,
1375 int path_is_unversioned, int allow_bad_symlinks,
1376 struct got_repository *repo,
1377 got_worktree_checkout_cb progress_cb, void *progress_arg)
1379 const struct got_error *err = NULL;
1380 char target_path[PATH_MAX];
1381 size_t len, target_len = 0;
1382 char *path_got = NULL;
1383 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1384 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1386 *is_bad_symlink = 0;
1389 * Blob object content specifies the target path of the link.
1390 * If a symbolic link cannot be installed we instead create
1391 * a regular file which contains the link target path stored
1392 * in the blob object.
1394 do {
1395 err = got_object_blob_read_block(&len, blob);
1396 if (len + target_len >= sizeof(target_path)) {
1397 /* Path too long; install as a regular file. */
1398 *is_bad_symlink = 1;
1399 got_object_blob_rewind(blob);
1400 return install_blob(worktree, ondisk_path, path,
1401 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1402 restoring_missing_file, reverting_versioned_file,
1403 1, path_is_unversioned, repo, progress_cb,
1404 progress_arg);
1406 if (len > 0) {
1407 /* Skip blob object header first time around. */
1408 memcpy(target_path + target_len, buf + hdrlen,
1409 len - hdrlen);
1410 target_len += len - hdrlen;
1411 hdrlen = 0;
1413 } while (len != 0);
1414 target_path[target_len] = '\0';
1416 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1417 ondisk_path, worktree->root_path);
1418 if (err)
1419 return err;
1421 if (*is_bad_symlink && !allow_bad_symlinks) {
1422 /* install as a regular file */
1423 got_object_blob_rewind(blob);
1424 err = install_blob(worktree, ondisk_path, path,
1425 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1426 restoring_missing_file, reverting_versioned_file, 1,
1427 path_is_unversioned, repo, progress_cb, progress_arg);
1428 goto done;
1431 if (symlink(target_path, ondisk_path) == -1) {
1432 if (errno == EEXIST) {
1433 int symlink_replaced;
1434 if (path_is_unversioned) {
1435 err = (*progress_cb)(progress_arg,
1436 GOT_STATUS_UNVERSIONED, path);
1437 goto done;
1439 err = replace_existing_symlink(&symlink_replaced,
1440 ondisk_path, target_path, target_len);
1441 if (err)
1442 goto done;
1443 if (progress_cb) {
1444 if (symlink_replaced) {
1445 err = (*progress_cb)(progress_arg,
1446 reverting_versioned_file ?
1447 GOT_STATUS_REVERT :
1448 GOT_STATUS_UPDATE, path);
1449 } else {
1450 err = (*progress_cb)(progress_arg,
1451 GOT_STATUS_EXISTS, path);
1454 goto done; /* Nothing else to do. */
1457 if (errno == ENOENT) {
1458 char *parent;
1459 err = got_path_dirname(&parent, ondisk_path);
1460 if (err)
1461 goto done;
1462 err = add_dir_on_disk(worktree, parent);
1463 free(parent);
1464 if (err)
1465 goto done;
1467 * Retry, and fall through to error handling
1468 * below if this second attempt fails.
1470 if (symlink(target_path, ondisk_path) != -1) {
1471 err = NULL; /* success */
1472 goto done;
1476 /* Handle errors from first or second creation attempt. */
1477 if (errno == ENAMETOOLONG) {
1478 /* bad target path; install as a regular file */
1479 *is_bad_symlink = 1;
1480 got_object_blob_rewind(blob);
1481 err = install_blob(worktree, ondisk_path, path,
1482 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1483 restoring_missing_file, reverting_versioned_file, 1,
1484 path_is_unversioned, repo,
1485 progress_cb, progress_arg);
1486 } else if (errno == ENOTDIR) {
1487 err = got_error_path(ondisk_path,
1488 GOT_ERR_FILE_OBSTRUCTED);
1489 } else {
1490 err = got_error_from_errno3("symlink",
1491 target_path, ondisk_path);
1493 } else if (progress_cb)
1494 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1495 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1496 done:
1497 free(path_got);
1498 return err;
1501 static const struct got_error *
1502 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1503 const char *path, mode_t te_mode, mode_t st_mode,
1504 struct got_blob_object *blob, int restoring_missing_file,
1505 int reverting_versioned_file, int installing_bad_symlink,
1506 int path_is_unversioned, struct got_repository *repo,
1507 got_worktree_checkout_cb progress_cb, void *progress_arg)
1509 const struct got_error *err = NULL;
1510 int fd = -1;
1511 size_t len, hdrlen;
1512 int update = 0;
1513 char *tmppath = NULL;
1515 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1516 GOT_DEFAULT_FILE_MODE);
1517 if (fd == -1) {
1518 if (errno == ENOENT) {
1519 char *parent;
1520 err = got_path_dirname(&parent, path);
1521 if (err)
1522 return err;
1523 err = add_dir_on_disk(worktree, parent);
1524 free(parent);
1525 if (err)
1526 return err;
1527 fd = open(ondisk_path,
1528 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1529 GOT_DEFAULT_FILE_MODE);
1530 if (fd == -1)
1531 return got_error_from_errno2("open",
1532 ondisk_path);
1533 } else if (errno == EEXIST) {
1534 if (path_is_unversioned) {
1535 err = (*progress_cb)(progress_arg,
1536 GOT_STATUS_UNVERSIONED, path);
1537 goto done;
1539 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1540 !S_ISREG(st_mode) && !installing_bad_symlink) {
1541 /* TODO file is obstructed; do something */
1542 err = got_error_path(ondisk_path,
1543 GOT_ERR_FILE_OBSTRUCTED);
1544 goto done;
1545 } else {
1546 err = got_opentemp_named_fd(&tmppath, &fd,
1547 ondisk_path);
1548 if (err)
1549 goto done;
1550 update = 1;
1552 } else
1553 return got_error_from_errno2("open", ondisk_path);
1556 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1557 err = got_error_from_errno2("fchmod",
1558 update ? tmppath : ondisk_path);
1559 goto done;
1562 if (progress_cb) {
1563 if (restoring_missing_file)
1564 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1565 path);
1566 else if (reverting_versioned_file)
1567 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1568 path);
1569 else
1570 err = (*progress_cb)(progress_arg,
1571 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1572 if (err)
1573 goto done;
1576 hdrlen = got_object_blob_get_hdrlen(blob);
1577 do {
1578 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1579 err = got_object_blob_read_block(&len, blob);
1580 if (err)
1581 break;
1582 if (len > 0) {
1583 /* Skip blob object header first time around. */
1584 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1585 if (outlen == -1) {
1586 err = got_error_from_errno("write");
1587 goto done;
1588 } else if (outlen != len - hdrlen) {
1589 err = got_error(GOT_ERR_IO);
1590 goto done;
1592 hdrlen = 0;
1594 } while (len != 0);
1596 if (fsync(fd) != 0) {
1597 err = got_error_from_errno("fsync");
1598 goto done;
1601 if (update) {
1602 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1603 err = got_error_from_errno2("unlink", ondisk_path);
1604 goto done;
1606 if (rename(tmppath, ondisk_path) != 0) {
1607 err = got_error_from_errno3("rename", tmppath,
1608 ondisk_path);
1609 goto done;
1611 free(tmppath);
1612 tmppath = NULL;
1615 done:
1616 if (fd != -1 && close(fd) == -1 && err == NULL)
1617 err = got_error_from_errno("close");
1618 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1619 err = got_error_from_errno2("unlink", tmppath);
1620 free(tmppath);
1621 return err;
1624 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1625 static const struct got_error *
1626 get_modified_file_content_status(unsigned char *status, FILE *f)
1628 const struct got_error *err = NULL;
1629 const char *markers[3] = {
1630 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1631 GOT_DIFF_CONFLICT_MARKER_SEP,
1632 GOT_DIFF_CONFLICT_MARKER_END
1634 int i = 0;
1635 char *line = NULL;
1636 size_t linesize = 0;
1637 ssize_t linelen;
1639 while (*status == GOT_STATUS_MODIFY) {
1640 linelen = getline(&line, &linesize, f);
1641 if (linelen == -1) {
1642 if (feof(f))
1643 break;
1644 err = got_ferror(f, GOT_ERR_IO);
1645 break;
1648 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1649 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1650 == 0)
1651 *status = GOT_STATUS_CONFLICT;
1652 else
1653 i++;
1656 free(line);
1658 return err;
1661 static int
1662 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1664 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1665 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1668 static int
1669 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1671 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1672 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1673 ie->mtime_sec == sb->st_mtim.tv_sec &&
1674 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1675 ie->size == (sb->st_size & 0xffffffff) &&
1676 !xbit_differs(ie, sb->st_mode));
1679 static unsigned char
1680 get_staged_status(struct got_fileindex_entry *ie)
1682 switch (got_fileindex_entry_stage_get(ie)) {
1683 case GOT_FILEIDX_STAGE_ADD:
1684 return GOT_STATUS_ADD;
1685 case GOT_FILEIDX_STAGE_DELETE:
1686 return GOT_STATUS_DELETE;
1687 case GOT_FILEIDX_STAGE_MODIFY:
1688 return GOT_STATUS_MODIFY;
1689 default:
1690 return GOT_STATUS_NO_CHANGE;
1694 static const struct got_error *
1695 get_symlink_modification_status(unsigned char *status,
1696 struct got_fileindex_entry *ie, const char *abspath,
1697 int dirfd, const char *de_name, struct got_blob_object *blob)
1699 const struct got_error *err = NULL;
1700 char target_path[PATH_MAX];
1701 char etarget[PATH_MAX];
1702 ssize_t elen;
1703 size_t len, target_len = 0;
1704 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1705 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1707 *status = GOT_STATUS_NO_CHANGE;
1709 /* Blob object content specifies the target path of the link. */
1710 do {
1711 err = got_object_blob_read_block(&len, blob);
1712 if (err)
1713 return err;
1714 if (len + target_len >= sizeof(target_path)) {
1716 * Should not happen. The blob contents were OK
1717 * when this symlink was installed.
1719 return got_error(GOT_ERR_NO_SPACE);
1721 if (len > 0) {
1722 /* Skip blob object header first time around. */
1723 memcpy(target_path + target_len, buf + hdrlen,
1724 len - hdrlen);
1725 target_len += len - hdrlen;
1726 hdrlen = 0;
1728 } while (len != 0);
1729 target_path[target_len] = '\0';
1731 if (dirfd != -1) {
1732 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1733 if (elen == -1)
1734 return got_error_from_errno2("readlinkat", abspath);
1735 } else {
1736 elen = readlink(abspath, etarget, sizeof(etarget));
1737 if (elen == -1)
1738 return got_error_from_errno2("readlink", abspath);
1741 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1742 *status = GOT_STATUS_MODIFY;
1744 return NULL;
1747 static const struct got_error *
1748 get_file_status(unsigned char *status, struct stat *sb,
1749 struct got_fileindex_entry *ie, const char *abspath,
1750 int dirfd, const char *de_name, struct got_repository *repo)
1752 const struct got_error *err = NULL;
1753 struct got_object_id id;
1754 size_t hdrlen;
1755 int fd = -1;
1756 FILE *f = NULL;
1757 uint8_t fbuf[8192];
1758 struct got_blob_object *blob = NULL;
1759 size_t flen, blen;
1760 unsigned char staged_status = get_staged_status(ie);
1762 *status = GOT_STATUS_NO_CHANGE;
1763 memset(sb, 0, sizeof(*sb));
1766 * Whenever the caller provides a directory descriptor and a
1767 * directory entry name for the file, use them! This prevents
1768 * race conditions if filesystem paths change beneath our feet.
1770 if (dirfd != -1) {
1771 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1772 if (errno == ENOENT) {
1773 if (got_fileindex_entry_has_file_on_disk(ie))
1774 *status = GOT_STATUS_MISSING;
1775 else
1776 *status = GOT_STATUS_DELETE;
1777 goto done;
1779 err = got_error_from_errno2("fstatat", abspath);
1780 goto done;
1782 } else {
1783 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1784 if (fd == -1 && errno != ENOENT &&
1785 !got_err_open_nofollow_on_symlink())
1786 return got_error_from_errno2("open", abspath);
1787 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
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 if (stat(ondisk_path, &sb) == -1) {
1948 if (errno != ENOENT) {
1949 err = got_error_from_errno2("stat",
1950 ondisk_path);
1951 goto done;
1953 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1954 status = GOT_STATUS_UNVERSIONED;
1955 } else {
1956 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1957 status = GOT_STATUS_UNVERSIONED;
1958 else
1959 status = GOT_STATUS_OBSTRUCTED;
1963 if (status == GOT_STATUS_OBSTRUCTED) {
1964 if (ie)
1965 got_fileindex_entry_mark_skipped(ie);
1966 err = (*progress_cb)(progress_arg, status, path);
1967 goto done;
1969 if (status == GOT_STATUS_CONFLICT) {
1970 if (ie)
1971 got_fileindex_entry_mark_skipped(ie);
1972 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1973 path);
1974 goto done;
1977 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1978 (S_ISLNK(te->mode) ||
1979 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1981 * This is a regular file or an installed bad symlink.
1982 * If the file index indicates that this file is already
1983 * up-to-date with respect to the repository we can skip
1984 * updating contents of this file.
1986 if (got_fileindex_entry_has_commit(ie) &&
1987 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1988 SHA1_DIGEST_LENGTH) == 0) {
1989 /* Same commit. */
1990 err = sync_timestamps(worktree->root_fd,
1991 path, status, ie, &sb);
1992 if (err)
1993 goto done;
1994 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1995 path);
1996 goto done;
1998 if (got_fileindex_entry_has_blob(ie) &&
1999 memcmp(ie->blob_sha1, te->id.sha1,
2000 SHA1_DIGEST_LENGTH) == 0) {
2001 /* Different commit but the same blob. */
2002 err = sync_timestamps(worktree->root_fd,
2003 path, status, ie, &sb);
2004 if (err)
2005 goto done;
2006 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2007 path);
2008 goto done;
2012 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
2013 if (err)
2014 goto done;
2016 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2017 int update_timestamps;
2018 struct got_blob_object *blob2 = NULL;
2019 char *label_orig = NULL;
2020 if (got_fileindex_entry_has_blob(ie)) {
2021 struct got_object_id id2;
2022 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2023 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2024 if (err)
2025 goto done;
2027 if (got_fileindex_entry_has_commit(ie)) {
2028 char id_str[SHA1_DIGEST_STRING_LENGTH];
2029 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2030 sizeof(id_str)) == NULL) {
2031 err = got_error_path(id_str,
2032 GOT_ERR_BAD_OBJ_ID_STR);
2033 goto done;
2035 if (asprintf(&label_orig, "%s: commit %s",
2036 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2037 err = got_error_from_errno("asprintf");
2038 goto done;
2041 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2042 char *link_target;
2043 err = got_object_blob_read_to_str(&link_target, blob);
2044 if (err)
2045 goto done;
2046 err = merge_symlink(worktree, blob2, ondisk_path, path,
2047 label_orig, link_target, worktree->base_commit_id,
2048 repo, progress_cb, progress_arg);
2049 free(link_target);
2050 } else {
2051 err = merge_blob(&update_timestamps, worktree, blob2,
2052 ondisk_path, path, sb.st_mode, label_orig, blob,
2053 worktree->base_commit_id, repo,
2054 progress_cb, progress_arg);
2056 free(label_orig);
2057 if (blob2)
2058 got_object_blob_close(blob2);
2059 if (err)
2060 goto done;
2062 * Do not update timestamps of files with local changes.
2063 * Otherwise, a future status walk would treat them as
2064 * unmodified files again.
2066 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2067 blob->id.sha1, worktree->base_commit_id->sha1,
2068 update_timestamps);
2069 } else if (status == GOT_STATUS_MODE_CHANGE) {
2070 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2071 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2072 } else if (status == GOT_STATUS_DELETE) {
2073 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2074 if (err)
2075 goto done;
2076 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2077 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2078 if (err)
2079 goto done;
2080 } else {
2081 int is_bad_symlink = 0;
2082 if (S_ISLNK(te->mode)) {
2083 err = install_symlink(&is_bad_symlink, worktree,
2084 ondisk_path, path, blob,
2085 status == GOT_STATUS_MISSING, 0,
2086 status == GOT_STATUS_UNVERSIONED, 0,
2087 repo, progress_cb, progress_arg);
2088 } else {
2089 err = install_blob(worktree, ondisk_path, path,
2090 te->mode, sb.st_mode, blob,
2091 status == GOT_STATUS_MISSING, 0, 0,
2092 status == GOT_STATUS_UNVERSIONED, repo,
2093 progress_cb, progress_arg);
2095 if (err)
2096 goto done;
2098 if (ie) {
2099 err = got_fileindex_entry_update(ie,
2100 worktree->root_fd, path, blob->id.sha1,
2101 worktree->base_commit_id->sha1, 1);
2102 } else {
2103 err = create_fileindex_entry(&ie, fileindex,
2104 worktree->base_commit_id, worktree->root_fd, path,
2105 &blob->id);
2107 if (err)
2108 goto done;
2110 if (is_bad_symlink) {
2111 got_fileindex_entry_filetype_set(ie,
2112 GOT_FILEIDX_MODE_BAD_SYMLINK);
2115 got_object_blob_close(blob);
2116 done:
2117 free(ondisk_path);
2118 return err;
2121 static const struct got_error *
2122 remove_ondisk_file(const char *root_path, const char *path)
2124 const struct got_error *err = NULL;
2125 char *ondisk_path = NULL, *parent = NULL;
2127 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2128 return got_error_from_errno("asprintf");
2130 if (unlink(ondisk_path) == -1) {
2131 if (errno != ENOENT)
2132 err = got_error_from_errno2("unlink", ondisk_path);
2133 } else {
2134 size_t root_len = strlen(root_path);
2135 err = got_path_dirname(&parent, ondisk_path);
2136 if (err)
2137 goto done;
2138 while (got_path_cmp(parent, root_path,
2139 strlen(parent), root_len) != 0) {
2140 free(ondisk_path);
2141 ondisk_path = parent;
2142 parent = NULL;
2143 if (rmdir(ondisk_path) == -1) {
2144 if (errno != ENOTEMPTY)
2145 err = got_error_from_errno2("rmdir",
2146 ondisk_path);
2147 break;
2149 err = got_path_dirname(&parent, ondisk_path);
2150 if (err)
2151 break;
2154 done:
2155 free(ondisk_path);
2156 free(parent);
2157 return err;
2160 static const struct got_error *
2161 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2162 struct got_fileindex_entry *ie, struct got_repository *repo,
2163 got_worktree_checkout_cb progress_cb, void *progress_arg)
2165 const struct got_error *err = NULL;
2166 unsigned char status;
2167 struct stat sb;
2168 char *ondisk_path;
2170 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2171 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2173 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2174 == -1)
2175 return got_error_from_errno("asprintf");
2177 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2178 if (err)
2179 goto done;
2181 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2182 char ondisk_target[PATH_MAX];
2183 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2184 sizeof(ondisk_target));
2185 if (ondisk_len == -1) {
2186 err = got_error_from_errno2("readlink", ondisk_path);
2187 goto done;
2189 ondisk_target[ondisk_len] = '\0';
2190 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2191 NULL, NULL, /* XXX pass common ancestor info? */
2192 ondisk_target, ondisk_path);
2193 if (err)
2194 goto done;
2195 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2196 ie->path);
2197 goto done;
2200 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2201 status == GOT_STATUS_ADD) {
2202 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2203 if (err)
2204 goto done;
2206 * Preserve the working file and change the deleted blob's
2207 * entry into a schedule-add entry.
2209 err = got_fileindex_entry_update(ie, worktree->root_fd,
2210 ie->path, NULL, NULL, 0);
2211 } else {
2212 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2213 if (err)
2214 goto done;
2215 if (status == GOT_STATUS_NO_CHANGE) {
2216 err = remove_ondisk_file(worktree->root_path, ie->path);
2217 if (err)
2218 goto done;
2220 got_fileindex_entry_remove(fileindex, ie);
2222 done:
2223 free(ondisk_path);
2224 return err;
2227 struct diff_cb_arg {
2228 struct got_fileindex *fileindex;
2229 struct got_worktree *worktree;
2230 struct got_repository *repo;
2231 got_worktree_checkout_cb progress_cb;
2232 void *progress_arg;
2233 got_cancel_cb cancel_cb;
2234 void *cancel_arg;
2237 static const struct got_error *
2238 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2239 struct got_tree_entry *te, const char *parent_path)
2241 struct diff_cb_arg *a = arg;
2243 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2244 return got_error(GOT_ERR_CANCELLED);
2246 return update_blob(a->worktree, a->fileindex, ie, te,
2247 ie->path, a->repo, a->progress_cb, a->progress_arg);
2250 static const struct got_error *
2251 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2253 struct diff_cb_arg *a = arg;
2255 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2256 return got_error(GOT_ERR_CANCELLED);
2258 return delete_blob(a->worktree, a->fileindex, ie,
2259 a->repo, a->progress_cb, a->progress_arg);
2262 static const struct got_error *
2263 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2265 struct diff_cb_arg *a = arg;
2266 const struct got_error *err;
2267 char *path;
2269 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2270 return got_error(GOT_ERR_CANCELLED);
2272 if (got_object_tree_entry_is_submodule(te))
2273 return NULL;
2275 if (asprintf(&path, "%s%s%s", parent_path,
2276 parent_path[0] ? "/" : "", te->name)
2277 == -1)
2278 return got_error_from_errno("asprintf");
2280 if (S_ISDIR(te->mode))
2281 err = add_dir_on_disk(a->worktree, path);
2282 else
2283 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2284 a->repo, a->progress_cb, a->progress_arg);
2286 free(path);
2287 return err;
2290 const struct got_error *
2291 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2293 uint32_t uuid_status;
2295 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2296 if (uuid_status != uuid_s_ok) {
2297 *uuidstr = NULL;
2298 return got_error_uuid(uuid_status, "uuid_to_string");
2301 return NULL;
2304 static const struct got_error *
2305 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2307 const struct got_error *err = NULL;
2308 char *uuidstr = NULL;
2310 *refname = NULL;
2312 err = got_worktree_get_uuid(&uuidstr, worktree);
2313 if (err)
2314 return err;
2316 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2317 err = got_error_from_errno("asprintf");
2318 *refname = NULL;
2320 free(uuidstr);
2321 return err;
2324 const struct got_error *
2325 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2327 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2330 static const struct got_error *
2331 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2333 return get_ref_name(refname, worktree,
2334 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2337 static const struct got_error *
2338 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2340 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2343 static const struct got_error *
2344 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2346 return get_ref_name(refname, worktree,
2347 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2350 static const struct got_error *
2351 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2353 return get_ref_name(refname, worktree,
2354 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2357 static const struct got_error *
2358 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2360 return get_ref_name(refname, worktree,
2361 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2364 static const struct got_error *
2365 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2367 return get_ref_name(refname, worktree,
2368 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2371 static const struct got_error *
2372 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2374 return get_ref_name(refname, worktree,
2375 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2378 static const struct got_error *
2379 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2381 return get_ref_name(refname, worktree,
2382 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2385 const struct got_error *
2386 got_worktree_get_histedit_script_path(char **path,
2387 struct got_worktree *worktree)
2389 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2390 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2391 *path = NULL;
2392 return got_error_from_errno("asprintf");
2394 return NULL;
2397 static const struct got_error *
2398 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2400 return get_ref_name(refname, worktree,
2401 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2404 static const struct got_error *
2405 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2407 return get_ref_name(refname, worktree,
2408 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2412 * Prevent Git's garbage collector from deleting our base commit by
2413 * setting a reference to our base commit's ID.
2415 static const struct got_error *
2416 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2418 const struct got_error *err = NULL;
2419 struct got_reference *ref = NULL;
2420 char *refname;
2422 err = got_worktree_get_base_ref_name(&refname, worktree);
2423 if (err)
2424 return err;
2426 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2427 if (err)
2428 goto done;
2430 err = got_ref_write(ref, repo);
2431 done:
2432 free(refname);
2433 if (ref)
2434 got_ref_close(ref);
2435 return err;
2438 static const struct got_error *
2439 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2441 const struct got_error *err = NULL;
2443 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2444 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2445 err = got_error_from_errno("asprintf");
2446 *fileindex_path = NULL;
2448 return err;
2452 static const struct got_error *
2453 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2454 struct got_worktree *worktree)
2456 const struct got_error *err = NULL;
2457 FILE *index = NULL;
2459 *fileindex_path = NULL;
2460 *fileindex = got_fileindex_alloc();
2461 if (*fileindex == NULL)
2462 return got_error_from_errno("got_fileindex_alloc");
2464 err = get_fileindex_path(fileindex_path, worktree);
2465 if (err)
2466 goto done;
2468 index = fopen(*fileindex_path, "rb");
2469 if (index == NULL) {
2470 if (errno != ENOENT)
2471 err = got_error_from_errno2("fopen", *fileindex_path);
2472 } else {
2473 err = got_fileindex_read(*fileindex, index);
2474 if (fclose(index) == EOF && err == NULL)
2475 err = got_error_from_errno("fclose");
2477 done:
2478 if (err) {
2479 free(*fileindex_path);
2480 *fileindex_path = NULL;
2481 got_fileindex_free(*fileindex);
2482 *fileindex = NULL;
2484 return err;
2487 struct bump_base_commit_id_arg {
2488 struct got_object_id *base_commit_id;
2489 const char *path;
2490 size_t path_len;
2491 const char *entry_name;
2492 got_worktree_checkout_cb progress_cb;
2493 void *progress_arg;
2496 /* Bump base commit ID of all files within an updated part of the work tree. */
2497 static const struct got_error *
2498 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2500 const struct got_error *err;
2501 struct bump_base_commit_id_arg *a = arg;
2503 if (a->entry_name) {
2504 if (strcmp(ie->path, a->path) != 0)
2505 return NULL;
2506 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2507 return NULL;
2509 if (got_fileindex_entry_was_skipped(ie))
2510 return NULL;
2512 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2513 SHA1_DIGEST_LENGTH) == 0)
2514 return NULL;
2516 if (a->progress_cb) {
2517 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2518 ie->path);
2519 if (err)
2520 return err;
2522 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2523 return NULL;
2526 static const struct got_error *
2527 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2528 struct got_fileindex *fileindex,
2529 got_worktree_checkout_cb progress_cb, void *progress_arg)
2531 struct bump_base_commit_id_arg bbc_arg;
2533 bbc_arg.base_commit_id = worktree->base_commit_id;
2534 bbc_arg.entry_name = NULL;
2535 bbc_arg.path = "";
2536 bbc_arg.path_len = 0;
2537 bbc_arg.progress_cb = progress_cb;
2538 bbc_arg.progress_arg = progress_arg;
2540 return got_fileindex_for_each_entry_safe(fileindex,
2541 bump_base_commit_id, &bbc_arg);
2544 static const struct got_error *
2545 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2547 const struct got_error *err = NULL;
2548 char *new_fileindex_path = NULL;
2549 FILE *new_index = NULL;
2550 struct timespec timeout;
2552 err = got_opentemp_named(&new_fileindex_path, &new_index,
2553 fileindex_path);
2554 if (err)
2555 goto done;
2557 err = got_fileindex_write(fileindex, new_index);
2558 if (err)
2559 goto done;
2561 if (rename(new_fileindex_path, fileindex_path) != 0) {
2562 err = got_error_from_errno3("rename", new_fileindex_path,
2563 fileindex_path);
2564 unlink(new_fileindex_path);
2568 * Sleep for a short amount of time to ensure that files modified after
2569 * this program exits have a different time stamp from the one which
2570 * was recorded in the file index.
2572 timeout.tv_sec = 0;
2573 timeout.tv_nsec = 1;
2574 nanosleep(&timeout, NULL);
2575 done:
2576 if (new_index)
2577 fclose(new_index);
2578 free(new_fileindex_path);
2579 return err;
2582 static const struct got_error *
2583 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2584 struct got_object_id **tree_id, const char *wt_relpath,
2585 struct got_worktree *worktree, struct got_repository *repo)
2587 const struct got_error *err = NULL;
2588 struct got_object_id *id = NULL;
2589 char *in_repo_path = NULL;
2590 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2592 *entry_type = GOT_OBJ_TYPE_ANY;
2593 *tree_relpath = NULL;
2594 *tree_id = NULL;
2596 if (wt_relpath[0] == '\0') {
2597 /* Check out all files within the work tree. */
2598 *entry_type = GOT_OBJ_TYPE_TREE;
2599 *tree_relpath = strdup("");
2600 if (*tree_relpath == NULL) {
2601 err = got_error_from_errno("strdup");
2602 goto done;
2604 err = got_object_id_by_path(tree_id, repo,
2605 worktree->base_commit_id, worktree->path_prefix);
2606 if (err)
2607 goto done;
2608 return NULL;
2611 /* Check out a subset of files in the work tree. */
2613 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2614 is_root_wt ? "" : "/", wt_relpath) == -1) {
2615 err = got_error_from_errno("asprintf");
2616 goto done;
2619 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2620 in_repo_path);
2621 if (err)
2622 goto done;
2624 free(in_repo_path);
2625 in_repo_path = NULL;
2627 err = got_object_get_type(entry_type, repo, id);
2628 if (err)
2629 goto done;
2631 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2632 /* Check out a single file. */
2633 if (strchr(wt_relpath, '/') == NULL) {
2634 /* Check out a single file in work tree's root dir. */
2635 in_repo_path = strdup(worktree->path_prefix);
2636 if (in_repo_path == NULL) {
2637 err = got_error_from_errno("strdup");
2638 goto done;
2640 *tree_relpath = strdup("");
2641 if (*tree_relpath == NULL) {
2642 err = got_error_from_errno("strdup");
2643 goto done;
2645 } else {
2646 /* Check out a single file in a subdirectory. */
2647 err = got_path_dirname(tree_relpath, wt_relpath);
2648 if (err)
2649 return err;
2650 if (asprintf(&in_repo_path, "%s%s%s",
2651 worktree->path_prefix, is_root_wt ? "" : "/",
2652 *tree_relpath) == -1) {
2653 err = got_error_from_errno("asprintf");
2654 goto done;
2657 err = got_object_id_by_path(tree_id, repo,
2658 worktree->base_commit_id, in_repo_path);
2659 } else {
2660 /* Check out all files within a subdirectory. */
2661 *tree_id = got_object_id_dup(id);
2662 if (*tree_id == NULL) {
2663 err = got_error_from_errno("got_object_id_dup");
2664 goto done;
2666 *tree_relpath = strdup(wt_relpath);
2667 if (*tree_relpath == NULL) {
2668 err = got_error_from_errno("strdup");
2669 goto done;
2672 done:
2673 free(id);
2674 free(in_repo_path);
2675 if (err) {
2676 *entry_type = GOT_OBJ_TYPE_ANY;
2677 free(*tree_relpath);
2678 *tree_relpath = NULL;
2679 free(*tree_id);
2680 *tree_id = NULL;
2682 return err;
2685 static const struct got_error *
2686 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2687 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2688 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2689 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2691 const struct got_error *err = NULL;
2692 struct got_commit_object *commit = NULL;
2693 struct got_tree_object *tree = NULL;
2694 struct got_fileindex_diff_tree_cb diff_cb;
2695 struct diff_cb_arg arg;
2697 err = ref_base_commit(worktree, repo);
2698 if (err) {
2699 if (!(err->code == GOT_ERR_ERRNO &&
2700 (errno == EACCES || errno == EROFS)))
2701 goto done;
2702 err = (*progress_cb)(progress_arg,
2703 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2704 if (err)
2705 return err;
2708 err = got_object_open_as_commit(&commit, repo,
2709 worktree->base_commit_id);
2710 if (err)
2711 goto done;
2713 err = got_object_open_as_tree(&tree, repo, tree_id);
2714 if (err)
2715 goto done;
2717 if (entry_name &&
2718 got_object_tree_find_entry(tree, entry_name) == NULL) {
2719 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2720 goto done;
2723 diff_cb.diff_old_new = diff_old_new;
2724 diff_cb.diff_old = diff_old;
2725 diff_cb.diff_new = diff_new;
2726 arg.fileindex = fileindex;
2727 arg.worktree = worktree;
2728 arg.repo = repo;
2729 arg.progress_cb = progress_cb;
2730 arg.progress_arg = progress_arg;
2731 arg.cancel_cb = cancel_cb;
2732 arg.cancel_arg = cancel_arg;
2733 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2734 entry_name, repo, &diff_cb, &arg);
2735 done:
2736 if (tree)
2737 got_object_tree_close(tree);
2738 if (commit)
2739 got_object_commit_close(commit);
2740 return err;
2743 const struct got_error *
2744 got_worktree_checkout_files(struct got_worktree *worktree,
2745 struct got_pathlist_head *paths, struct got_repository *repo,
2746 got_worktree_checkout_cb progress_cb, void *progress_arg,
2747 got_cancel_cb cancel_cb, void *cancel_arg)
2749 const struct got_error *err = NULL, *sync_err, *unlockerr;
2750 struct got_commit_object *commit = NULL;
2751 struct got_tree_object *tree = NULL;
2752 struct got_fileindex *fileindex = NULL;
2753 char *fileindex_path = NULL;
2754 struct got_pathlist_entry *pe;
2755 struct tree_path_data {
2756 STAILQ_ENTRY(tree_path_data) entry;
2757 struct got_object_id *tree_id;
2758 int entry_type;
2759 char *relpath;
2760 char *entry_name;
2761 } *tpd = NULL;
2762 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2764 STAILQ_INIT(&tree_paths);
2766 err = lock_worktree(worktree, LOCK_EX);
2767 if (err)
2768 return err;
2770 /* Map all specified paths to in-repository trees. */
2771 TAILQ_FOREACH(pe, paths, entry) {
2772 tpd = malloc(sizeof(*tpd));
2773 if (tpd == NULL) {
2774 err = got_error_from_errno("malloc");
2775 goto done;
2778 err = find_tree_entry_for_checkout(&tpd->entry_type,
2779 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2780 if (err) {
2781 free(tpd);
2782 goto done;
2785 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2786 err = got_path_basename(&tpd->entry_name, pe->path);
2787 if (err) {
2788 free(tpd->relpath);
2789 free(tpd->tree_id);
2790 free(tpd);
2791 goto done;
2793 } else
2794 tpd->entry_name = NULL;
2796 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2800 * Read the file index.
2801 * Checking out files is supposed to be an idempotent operation.
2802 * If the on-disk file index is incomplete we will try to complete it.
2804 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2805 if (err)
2806 goto done;
2808 tpd = STAILQ_FIRST(&tree_paths);
2809 TAILQ_FOREACH(pe, paths, entry) {
2810 struct bump_base_commit_id_arg bbc_arg;
2812 err = checkout_files(worktree, fileindex, tpd->relpath,
2813 tpd->tree_id, tpd->entry_name, repo,
2814 progress_cb, progress_arg, cancel_cb, cancel_arg);
2815 if (err)
2816 break;
2818 bbc_arg.base_commit_id = worktree->base_commit_id;
2819 bbc_arg.entry_name = tpd->entry_name;
2820 bbc_arg.path = pe->path;
2821 bbc_arg.path_len = pe->path_len;
2822 bbc_arg.progress_cb = progress_cb;
2823 bbc_arg.progress_arg = progress_arg;
2824 err = got_fileindex_for_each_entry_safe(fileindex,
2825 bump_base_commit_id, &bbc_arg);
2826 if (err)
2827 break;
2829 tpd = STAILQ_NEXT(tpd, entry);
2831 sync_err = sync_fileindex(fileindex, fileindex_path);
2832 if (sync_err && err == NULL)
2833 err = sync_err;
2834 done:
2835 free(fileindex_path);
2836 if (tree)
2837 got_object_tree_close(tree);
2838 if (commit)
2839 got_object_commit_close(commit);
2840 if (fileindex)
2841 got_fileindex_free(fileindex);
2842 while (!STAILQ_EMPTY(&tree_paths)) {
2843 tpd = STAILQ_FIRST(&tree_paths);
2844 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2845 free(tpd->relpath);
2846 free(tpd->tree_id);
2847 free(tpd);
2849 unlockerr = lock_worktree(worktree, LOCK_SH);
2850 if (unlockerr && err == NULL)
2851 err = unlockerr;
2852 return err;
2855 struct merge_file_cb_arg {
2856 struct got_worktree *worktree;
2857 struct got_fileindex *fileindex;
2858 got_worktree_checkout_cb progress_cb;
2859 void *progress_arg;
2860 got_cancel_cb cancel_cb;
2861 void *cancel_arg;
2862 const char *label_orig;
2863 struct got_object_id *commit_id2;
2864 int allow_bad_symlinks;
2867 static const struct got_error *
2868 merge_file_cb(void *arg, struct got_blob_object *blob1,
2869 struct got_blob_object *blob2, struct got_object_id *id1,
2870 struct got_object_id *id2, const char *path1, const char *path2,
2871 mode_t mode1, mode_t mode2, struct got_repository *repo)
2873 static const struct got_error *err = NULL;
2874 struct merge_file_cb_arg *a = arg;
2875 struct got_fileindex_entry *ie;
2876 char *ondisk_path = NULL;
2877 struct stat sb;
2878 unsigned char status;
2879 int local_changes_subsumed;
2880 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2881 char *id_str = NULL, *label_deriv2 = NULL;
2883 if (blob1 && blob2) {
2884 ie = got_fileindex_entry_get(a->fileindex, path2,
2885 strlen(path2));
2886 if (ie == NULL)
2887 return (*a->progress_cb)(a->progress_arg,
2888 GOT_STATUS_MISSING, path2);
2890 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2891 path2) == -1)
2892 return got_error_from_errno("asprintf");
2894 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2895 repo);
2896 if (err)
2897 goto done;
2899 if (status == GOT_STATUS_DELETE) {
2900 err = (*a->progress_cb)(a->progress_arg,
2901 GOT_STATUS_MERGE, path2);
2902 goto done;
2904 if (status != GOT_STATUS_NO_CHANGE &&
2905 status != GOT_STATUS_MODIFY &&
2906 status != GOT_STATUS_CONFLICT &&
2907 status != GOT_STATUS_ADD) {
2908 err = (*a->progress_cb)(a->progress_arg, status, path2);
2909 goto done;
2912 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2913 char *link_target2;
2914 err = got_object_blob_read_to_str(&link_target2, blob2);
2915 if (err)
2916 goto done;
2917 err = merge_symlink(a->worktree, blob1, ondisk_path,
2918 path2, a->label_orig, link_target2, a->commit_id2,
2919 repo, a->progress_cb, a->progress_arg);
2920 free(link_target2);
2921 } else {
2922 int fd;
2924 f_orig = got_opentemp();
2925 if (f_orig == NULL) {
2926 err = got_error_from_errno("got_opentemp");
2927 goto done;
2929 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2930 f_orig, blob1);
2931 if (err)
2932 goto done;
2934 f_deriv2 = got_opentemp();
2935 if (f_deriv2 == NULL)
2936 goto done;
2937 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2938 f_deriv2, blob2);
2939 if (err)
2940 goto done;
2942 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2943 if (fd == -1) {
2944 err = got_error_from_errno2("open",
2945 ondisk_path);
2946 goto done;
2948 f_deriv = fdopen(fd, "r");
2949 if (f_deriv == NULL) {
2950 err = got_error_from_errno2("fdopen",
2951 ondisk_path);
2952 close(fd);
2953 goto done;
2955 err = got_object_id_str(&id_str, a->commit_id2);
2956 if (err)
2957 goto done;
2958 if (asprintf(&label_deriv2, "%s: commit %s",
2959 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2960 err = got_error_from_errno("asprintf");
2961 goto done;
2963 err = merge_file(&local_changes_subsumed, a->worktree,
2964 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2965 sb.st_mode, a->label_orig, NULL, label_deriv2,
2966 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2967 a->progress_cb, a->progress_arg);
2969 } else if (blob1) {
2970 ie = got_fileindex_entry_get(a->fileindex, path1,
2971 strlen(path1));
2972 if (ie == NULL)
2973 return (*a->progress_cb)(a->progress_arg,
2974 GOT_STATUS_MISSING, path1);
2976 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2977 path1) == -1)
2978 return got_error_from_errno("asprintf");
2980 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2981 repo);
2982 if (err)
2983 goto done;
2985 switch (status) {
2986 case GOT_STATUS_NO_CHANGE:
2987 err = (*a->progress_cb)(a->progress_arg,
2988 GOT_STATUS_DELETE, path1);
2989 if (err)
2990 goto done;
2991 err = remove_ondisk_file(a->worktree->root_path, path1);
2992 if (err)
2993 goto done;
2994 if (ie)
2995 got_fileindex_entry_mark_deleted_from_disk(ie);
2996 break;
2997 case GOT_STATUS_DELETE:
2998 case GOT_STATUS_MISSING:
2999 err = (*a->progress_cb)(a->progress_arg,
3000 GOT_STATUS_DELETE, path1);
3001 if (err)
3002 goto done;
3003 if (ie)
3004 got_fileindex_entry_mark_deleted_from_disk(ie);
3005 break;
3006 case GOT_STATUS_ADD: {
3007 struct got_object_id *id;
3008 FILE *blob1_f;
3010 * Delete the added file only if its content already
3011 * exists in the repository.
3013 err = got_object_blob_file_create(&id, &blob1_f, path1);
3014 if (err)
3015 goto done;
3016 if (got_object_id_cmp(id, id1) == 0) {
3017 err = (*a->progress_cb)(a->progress_arg,
3018 GOT_STATUS_DELETE, path1);
3019 if (err)
3020 goto done;
3021 err = remove_ondisk_file(a->worktree->root_path,
3022 path1);
3023 if (err)
3024 goto done;
3025 if (ie)
3026 got_fileindex_entry_remove(a->fileindex,
3027 ie);
3028 } else {
3029 err = (*a->progress_cb)(a->progress_arg,
3030 GOT_STATUS_CANNOT_DELETE, path1);
3032 if (fclose(blob1_f) == EOF && err == NULL)
3033 err = got_error_from_errno("fclose");
3034 free(id);
3035 if (err)
3036 goto done;
3037 break;
3039 case GOT_STATUS_MODIFY:
3040 case GOT_STATUS_CONFLICT:
3041 err = (*a->progress_cb)(a->progress_arg,
3042 GOT_STATUS_CANNOT_DELETE, path1);
3043 if (err)
3044 goto done;
3045 break;
3046 case GOT_STATUS_OBSTRUCTED:
3047 err = (*a->progress_cb)(a->progress_arg, status, path1);
3048 if (err)
3049 goto done;
3050 break;
3051 default:
3052 break;
3054 } else if (blob2) {
3055 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3056 path2) == -1)
3057 return got_error_from_errno("asprintf");
3058 ie = got_fileindex_entry_get(a->fileindex, path2,
3059 strlen(path2));
3060 if (ie) {
3061 err = get_file_status(&status, &sb, ie, ondisk_path,
3062 -1, NULL, repo);
3063 if (err)
3064 goto done;
3065 if (status != GOT_STATUS_NO_CHANGE &&
3066 status != GOT_STATUS_MODIFY &&
3067 status != GOT_STATUS_CONFLICT &&
3068 status != GOT_STATUS_ADD) {
3069 err = (*a->progress_cb)(a->progress_arg,
3070 status, path2);
3071 goto done;
3073 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3074 char *link_target2;
3075 err = got_object_blob_read_to_str(&link_target2,
3076 blob2);
3077 if (err)
3078 goto done;
3079 err = merge_symlink(a->worktree, NULL,
3080 ondisk_path, path2, a->label_orig,
3081 link_target2, a->commit_id2, repo,
3082 a->progress_cb, a->progress_arg);
3083 free(link_target2);
3084 } else if (S_ISREG(sb.st_mode)) {
3085 err = merge_blob(&local_changes_subsumed,
3086 a->worktree, NULL, ondisk_path, path2,
3087 sb.st_mode, a->label_orig, blob2,
3088 a->commit_id2, repo, a->progress_cb,
3089 a->progress_arg);
3090 } else {
3091 err = got_error_path(ondisk_path,
3092 GOT_ERR_FILE_OBSTRUCTED);
3094 if (err)
3095 goto done;
3096 if (status == GOT_STATUS_DELETE) {
3097 err = got_fileindex_entry_update(ie,
3098 a->worktree->root_fd, path2, blob2->id.sha1,
3099 a->worktree->base_commit_id->sha1, 0);
3100 if (err)
3101 goto done;
3103 } else {
3104 int is_bad_symlink = 0;
3105 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3106 if (S_ISLNK(mode2)) {
3107 err = install_symlink(&is_bad_symlink,
3108 a->worktree, ondisk_path, path2, blob2, 0,
3109 0, 1, a->allow_bad_symlinks, repo,
3110 a->progress_cb, a->progress_arg);
3111 } else {
3112 err = install_blob(a->worktree, ondisk_path, path2,
3113 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3114 a->progress_cb, a->progress_arg);
3116 if (err)
3117 goto done;
3118 err = got_fileindex_entry_alloc(&ie, path2);
3119 if (err)
3120 goto done;
3121 err = got_fileindex_entry_update(ie,
3122 a->worktree->root_fd, path2, NULL, NULL, 1);
3123 if (err) {
3124 got_fileindex_entry_free(ie);
3125 goto done;
3127 err = got_fileindex_entry_add(a->fileindex, ie);
3128 if (err) {
3129 got_fileindex_entry_free(ie);
3130 goto done;
3132 if (is_bad_symlink) {
3133 got_fileindex_entry_filetype_set(ie,
3134 GOT_FILEIDX_MODE_BAD_SYMLINK);
3138 done:
3139 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3140 err = got_error_from_errno("fclose");
3141 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3142 err = got_error_from_errno("fclose");
3143 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3144 err = got_error_from_errno("fclose");
3145 free(id_str);
3146 free(label_deriv2);
3147 free(ondisk_path);
3148 return err;
3151 static const struct got_error *
3152 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3154 struct got_worktree *worktree = arg;
3156 /* Reject merges into a work tree with mixed base commits. */
3157 if (got_fileindex_entry_has_commit(ie) &&
3158 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3159 SHA1_DIGEST_LENGTH) != 0)
3160 return got_error(GOT_ERR_MIXED_COMMITS);
3162 return NULL;
3165 struct check_merge_conflicts_arg {
3166 struct got_worktree *worktree;
3167 struct got_fileindex *fileindex;
3168 struct got_repository *repo;
3171 static const struct got_error *
3172 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3173 struct got_blob_object *blob2, struct got_object_id *id1,
3174 struct got_object_id *id2, const char *path1, const char *path2,
3175 mode_t mode1, mode_t mode2, struct got_repository *repo)
3177 const struct got_error *err = NULL;
3178 struct check_merge_conflicts_arg *a = arg;
3179 unsigned char status;
3180 struct stat sb;
3181 struct got_fileindex_entry *ie;
3182 const char *path = path2 ? path2 : path1;
3183 struct got_object_id *id = id2 ? id2 : id1;
3184 char *ondisk_path;
3186 if (id == NULL)
3187 return NULL;
3189 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3190 if (ie == NULL)
3191 return NULL;
3193 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3194 == -1)
3195 return got_error_from_errno("asprintf");
3197 /* Reject merges into a work tree with conflicted files. */
3198 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3199 free(ondisk_path);
3200 if (err)
3201 return err;
3202 if (status == GOT_STATUS_CONFLICT)
3203 return got_error(GOT_ERR_CONFLICTS);
3205 return NULL;
3208 static const struct got_error *
3209 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3210 const char *fileindex_path, struct got_object_id *commit_id1,
3211 struct got_object_id *commit_id2, struct got_repository *repo,
3212 got_worktree_checkout_cb progress_cb, void *progress_arg,
3213 got_cancel_cb cancel_cb, void *cancel_arg)
3215 const struct got_error *err = NULL, *sync_err;
3216 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3217 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3218 struct check_merge_conflicts_arg cmc_arg;
3219 struct merge_file_cb_arg arg;
3220 char *label_orig = NULL;
3222 if (commit_id1) {
3223 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3224 worktree->path_prefix);
3225 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3226 goto done;
3228 if (tree_id1) {
3229 char *id_str;
3231 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3232 if (err)
3233 goto done;
3235 err = got_object_id_str(&id_str, commit_id1);
3236 if (err)
3237 goto done;
3239 if (asprintf(&label_orig, "%s: commit %s",
3240 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3241 err = got_error_from_errno("asprintf");
3242 free(id_str);
3243 goto done;
3245 free(id_str);
3248 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3249 worktree->path_prefix);
3250 if (err)
3251 goto done;
3253 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3254 if (err)
3255 goto done;
3257 cmc_arg.worktree = worktree;
3258 cmc_arg.fileindex = fileindex;
3259 cmc_arg.repo = repo;
3260 err = got_diff_tree(tree1, tree2, "", "", repo,
3261 check_merge_conflicts, &cmc_arg, 0);
3262 if (err)
3263 goto done;
3265 arg.worktree = worktree;
3266 arg.fileindex = fileindex;
3267 arg.progress_cb = progress_cb;
3268 arg.progress_arg = progress_arg;
3269 arg.cancel_cb = cancel_cb;
3270 arg.cancel_arg = cancel_arg;
3271 arg.label_orig = label_orig;
3272 arg.commit_id2 = commit_id2;
3273 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3274 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3275 sync_err = sync_fileindex(fileindex, fileindex_path);
3276 if (sync_err && err == NULL)
3277 err = sync_err;
3278 done:
3279 if (tree1)
3280 got_object_tree_close(tree1);
3281 if (tree2)
3282 got_object_tree_close(tree2);
3283 free(label_orig);
3284 return err;
3287 const struct got_error *
3288 got_worktree_merge_files(struct got_worktree *worktree,
3289 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3290 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3291 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3293 const struct got_error *err, *unlockerr;
3294 char *fileindex_path = NULL;
3295 struct got_fileindex *fileindex = NULL;
3297 err = lock_worktree(worktree, LOCK_EX);
3298 if (err)
3299 return err;
3301 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3302 if (err)
3303 goto done;
3305 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3306 worktree);
3307 if (err)
3308 goto done;
3310 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3311 commit_id2, repo, progress_cb, progress_arg,
3312 cancel_cb, cancel_arg);
3313 done:
3314 if (fileindex)
3315 got_fileindex_free(fileindex);
3316 free(fileindex_path);
3317 unlockerr = lock_worktree(worktree, LOCK_SH);
3318 if (unlockerr && err == NULL)
3319 err = unlockerr;
3320 return err;
3323 struct diff_dir_cb_arg {
3324 struct got_fileindex *fileindex;
3325 struct got_worktree *worktree;
3326 const char *status_path;
3327 size_t status_path_len;
3328 struct got_repository *repo;
3329 got_worktree_status_cb status_cb;
3330 void *status_arg;
3331 got_cancel_cb cancel_cb;
3332 void *cancel_arg;
3333 /* A pathlist containing per-directory pathlists of ignore patterns. */
3334 struct got_pathlist_head *ignores;
3335 int report_unchanged;
3336 int no_ignores;
3339 static const struct got_error *
3340 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3341 int dirfd, const char *de_name,
3342 got_worktree_status_cb status_cb, void *status_arg,
3343 struct got_repository *repo, int report_unchanged)
3345 const struct got_error *err = NULL;
3346 unsigned char status = GOT_STATUS_NO_CHANGE;
3347 unsigned char staged_status = get_staged_status(ie);
3348 struct stat sb;
3349 struct got_object_id blob_id, commit_id, staged_blob_id;
3350 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3351 struct got_object_id *staged_blob_idp = NULL;
3353 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3354 if (err)
3355 return err;
3357 if (status == GOT_STATUS_NO_CHANGE &&
3358 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3359 return NULL;
3361 if (got_fileindex_entry_has_blob(ie)) {
3362 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3363 blob_idp = &blob_id;
3365 if (got_fileindex_entry_has_commit(ie)) {
3366 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3367 commit_idp = &commit_id;
3369 if (staged_status == GOT_STATUS_ADD ||
3370 staged_status == GOT_STATUS_MODIFY) {
3371 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3372 SHA1_DIGEST_LENGTH);
3373 staged_blob_idp = &staged_blob_id;
3376 return (*status_cb)(status_arg, status, staged_status,
3377 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3380 static const struct got_error *
3381 status_old_new(void *arg, struct got_fileindex_entry *ie,
3382 struct dirent *de, const char *parent_path, int dirfd)
3384 const struct got_error *err = NULL;
3385 struct diff_dir_cb_arg *a = arg;
3386 char *abspath;
3388 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3389 return got_error(GOT_ERR_CANCELLED);
3391 if (got_path_cmp(parent_path, a->status_path,
3392 strlen(parent_path), a->status_path_len) != 0 &&
3393 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3394 return NULL;
3396 if (parent_path[0]) {
3397 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3398 parent_path, de->d_name) == -1)
3399 return got_error_from_errno("asprintf");
3400 } else {
3401 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3402 de->d_name) == -1)
3403 return got_error_from_errno("asprintf");
3406 err = report_file_status(ie, abspath, dirfd, de->d_name,
3407 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3408 free(abspath);
3409 return err;
3412 static const struct got_error *
3413 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3415 struct diff_dir_cb_arg *a = arg;
3416 struct got_object_id blob_id, commit_id;
3417 unsigned char status;
3419 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3420 return got_error(GOT_ERR_CANCELLED);
3422 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3423 return NULL;
3425 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3426 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3427 if (got_fileindex_entry_has_file_on_disk(ie))
3428 status = GOT_STATUS_MISSING;
3429 else
3430 status = GOT_STATUS_DELETE;
3431 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3432 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3435 void
3436 free_ignorelist(struct got_pathlist_head *ignorelist)
3438 struct got_pathlist_entry *pe;
3440 TAILQ_FOREACH(pe, ignorelist, entry)
3441 free((char *)pe->path);
3442 got_pathlist_free(ignorelist);
3445 void
3446 free_ignores(struct got_pathlist_head *ignores)
3448 struct got_pathlist_entry *pe;
3450 TAILQ_FOREACH(pe, ignores, entry) {
3451 struct got_pathlist_head *ignorelist = pe->data;
3452 free_ignorelist(ignorelist);
3453 free((char *)pe->path);
3455 got_pathlist_free(ignores);
3458 static const struct got_error *
3459 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3461 const struct got_error *err = NULL;
3462 struct got_pathlist_entry *pe = NULL;
3463 struct got_pathlist_head *ignorelist;
3464 char *line = NULL, *pattern, *dirpath = NULL;
3465 size_t linesize = 0;
3466 ssize_t linelen;
3468 ignorelist = calloc(1, sizeof(*ignorelist));
3469 if (ignorelist == NULL)
3470 return got_error_from_errno("calloc");
3471 TAILQ_INIT(ignorelist);
3473 while ((linelen = getline(&line, &linesize, f)) != -1) {
3474 if (linelen > 0 && line[linelen - 1] == '\n')
3475 line[linelen - 1] = '\0';
3477 /* Git's ignores may contain comments. */
3478 if (line[0] == '#')
3479 continue;
3481 /* Git's negated patterns are not (yet?) supported. */
3482 if (line[0] == '!')
3483 continue;
3485 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3486 line) == -1) {
3487 err = got_error_from_errno("asprintf");
3488 goto done;
3490 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3491 if (err)
3492 goto done;
3494 if (ferror(f)) {
3495 err = got_error_from_errno("getline");
3496 goto done;
3499 dirpath = strdup(path);
3500 if (dirpath == NULL) {
3501 err = got_error_from_errno("strdup");
3502 goto done;
3504 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3505 done:
3506 free(line);
3507 if (err || pe == NULL) {
3508 free(dirpath);
3509 free_ignorelist(ignorelist);
3511 return err;
3514 int
3515 match_ignores(struct got_pathlist_head *ignores, const char *path)
3517 struct got_pathlist_entry *pe;
3519 /* Handle patterns which match in all directories. */
3520 TAILQ_FOREACH(pe, ignores, entry) {
3521 struct got_pathlist_head *ignorelist = pe->data;
3522 struct got_pathlist_entry *pi;
3524 TAILQ_FOREACH(pi, ignorelist, entry) {
3525 const char *p, *pattern = pi->path;
3527 if (strncmp(pattern, "**/", 3) != 0)
3528 continue;
3529 pattern += 3;
3530 p = path;
3531 while (*p) {
3532 if (fnmatch(pattern, p,
3533 FNM_PATHNAME | FNM_LEADING_DIR)) {
3534 /* Retry in next directory. */
3535 while (*p && *p != '/')
3536 p++;
3537 while (*p == '/')
3538 p++;
3539 continue;
3541 return 1;
3547 * The ignores pathlist contains ignore lists from children before
3548 * parents, so we can find the most specific ignorelist by walking
3549 * ignores backwards.
3551 pe = TAILQ_LAST(ignores, got_pathlist_head);
3552 while (pe) {
3553 if (got_path_is_child(path, pe->path, pe->path_len)) {
3554 struct got_pathlist_head *ignorelist = pe->data;
3555 struct got_pathlist_entry *pi;
3556 TAILQ_FOREACH(pi, ignorelist, entry) {
3557 const char *pattern = pi->path;
3558 int flags = FNM_LEADING_DIR;
3559 if (strstr(pattern, "/**/") == NULL)
3560 flags |= FNM_PATHNAME;
3561 if (fnmatch(pattern, path, flags))
3562 continue;
3563 return 1;
3566 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3569 return 0;
3572 static const struct got_error *
3573 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3574 const char *path, int dirfd, const char *ignores_filename)
3576 const struct got_error *err = NULL;
3577 char *ignorespath;
3578 int fd = -1;
3579 FILE *ignoresfile = NULL;
3581 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3582 path[0] ? "/" : "", ignores_filename) == -1)
3583 return got_error_from_errno("asprintf");
3585 if (dirfd != -1) {
3586 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3587 if (fd == -1) {
3588 if (errno != ENOENT && errno != EACCES)
3589 err = got_error_from_errno2("openat",
3590 ignorespath);
3591 } else {
3592 ignoresfile = fdopen(fd, "r");
3593 if (ignoresfile == NULL)
3594 err = got_error_from_errno2("fdopen",
3595 ignorespath);
3596 else {
3597 fd = -1;
3598 err = read_ignores(ignores, path, ignoresfile);
3601 } else {
3602 ignoresfile = fopen(ignorespath, "r");
3603 if (ignoresfile == NULL) {
3604 if (errno != ENOENT && errno != EACCES)
3605 err = got_error_from_errno2("fopen",
3606 ignorespath);
3607 } else
3608 err = read_ignores(ignores, path, ignoresfile);
3611 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3612 err = got_error_from_errno2("fclose", path);
3613 if (fd != -1 && close(fd) == -1 && err == NULL)
3614 err = got_error_from_errno2("close", path);
3615 free(ignorespath);
3616 return err;
3619 static const struct got_error *
3620 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3622 const struct got_error *err = NULL;
3623 struct diff_dir_cb_arg *a = arg;
3624 char *path = NULL;
3626 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3627 return got_error(GOT_ERR_CANCELLED);
3629 if (parent_path[0]) {
3630 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3631 return got_error_from_errno("asprintf");
3632 } else {
3633 path = de->d_name;
3636 if (de->d_type != DT_DIR &&
3637 got_path_is_child(path, a->status_path, a->status_path_len)
3638 && !match_ignores(a->ignores, path))
3639 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3640 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3641 if (parent_path[0])
3642 free(path);
3643 return err;
3646 static const struct got_error *
3647 status_traverse(void *arg, const char *path, int dirfd)
3649 const struct got_error *err = NULL;
3650 struct diff_dir_cb_arg *a = arg;
3652 if (a->no_ignores)
3653 return NULL;
3655 err = add_ignores(a->ignores, a->worktree->root_path,
3656 path, dirfd, ".cvsignore");
3657 if (err)
3658 return err;
3660 err = add_ignores(a->ignores, a->worktree->root_path, path,
3661 dirfd, ".gitignore");
3663 return err;
3666 static const struct got_error *
3667 report_single_file_status(const char *path, const char *ondisk_path,
3668 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3669 void *status_arg, struct got_repository *repo, int report_unchanged,
3670 struct got_pathlist_head *ignores, int no_ignores)
3672 struct got_fileindex_entry *ie;
3673 struct stat sb;
3675 if (!no_ignores && match_ignores(ignores, path))
3676 return NULL;
3678 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3679 if (ie)
3680 return report_file_status(ie, ondisk_path, -1, NULL,
3681 status_cb, status_arg, repo, report_unchanged);
3683 if (lstat(ondisk_path, &sb) == -1) {
3684 if (errno != ENOENT)
3685 return got_error_from_errno2("lstat", ondisk_path);
3686 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3687 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3688 return NULL;
3691 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3692 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3693 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3695 return NULL;
3698 static const struct got_error *
3699 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3700 const char *root_path, const char *path)
3702 const struct got_error *err;
3703 char *parent_path, *next_parent_path = NULL;
3705 err = add_ignores(ignores, root_path, "", -1,
3706 ".cvsignore");
3707 if (err)
3708 return err;
3710 err = add_ignores(ignores, root_path, "", -1,
3711 ".gitignore");
3712 if (err)
3713 return err;
3715 err = got_path_dirname(&parent_path, path);
3716 if (err) {
3717 if (err->code == GOT_ERR_BAD_PATH)
3718 return NULL; /* cannot traverse parent */
3719 return err;
3721 for (;;) {
3722 err = add_ignores(ignores, root_path, parent_path, -1,
3723 ".cvsignore");
3724 if (err)
3725 break;
3726 err = add_ignores(ignores, root_path, parent_path, -1,
3727 ".gitignore");
3728 if (err)
3729 break;
3730 err = got_path_dirname(&next_parent_path, parent_path);
3731 if (err) {
3732 if (err->code == GOT_ERR_BAD_PATH)
3733 err = NULL; /* traversed everything */
3734 break;
3736 if (got_path_is_root_dir(parent_path))
3737 break;
3738 free(parent_path);
3739 parent_path = next_parent_path;
3740 next_parent_path = NULL;
3743 free(parent_path);
3744 free(next_parent_path);
3745 return err;
3748 static const struct got_error *
3749 worktree_status(struct got_worktree *worktree, const char *path,
3750 struct got_fileindex *fileindex, struct got_repository *repo,
3751 got_worktree_status_cb status_cb, void *status_arg,
3752 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3753 int report_unchanged)
3755 const struct got_error *err = NULL;
3756 int fd = -1;
3757 struct got_fileindex_diff_dir_cb fdiff_cb;
3758 struct diff_dir_cb_arg arg;
3759 char *ondisk_path = NULL;
3760 struct got_pathlist_head ignores;
3762 TAILQ_INIT(&ignores);
3764 if (asprintf(&ondisk_path, "%s%s%s",
3765 worktree->root_path, path[0] ? "/" : "", path) == -1)
3766 return got_error_from_errno("asprintf");
3768 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3769 if (fd == -1) {
3770 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3771 !got_err_open_nofollow_on_symlink())
3772 err = got_error_from_errno2("open", ondisk_path);
3773 else {
3774 if (!no_ignores) {
3775 err = add_ignores_from_parent_paths(&ignores,
3776 worktree->root_path, ondisk_path);
3777 if (err)
3778 goto done;
3780 err = report_single_file_status(path, ondisk_path,
3781 fileindex, status_cb, status_arg, repo,
3782 report_unchanged, &ignores, no_ignores);
3784 } else {
3785 fdiff_cb.diff_old_new = status_old_new;
3786 fdiff_cb.diff_old = status_old;
3787 fdiff_cb.diff_new = status_new;
3788 fdiff_cb.diff_traverse = status_traverse;
3789 arg.fileindex = fileindex;
3790 arg.worktree = worktree;
3791 arg.status_path = path;
3792 arg.status_path_len = strlen(path);
3793 arg.repo = repo;
3794 arg.status_cb = status_cb;
3795 arg.status_arg = status_arg;
3796 arg.cancel_cb = cancel_cb;
3797 arg.cancel_arg = cancel_arg;
3798 arg.report_unchanged = report_unchanged;
3799 arg.no_ignores = no_ignores;
3800 if (!no_ignores) {
3801 err = add_ignores_from_parent_paths(&ignores,
3802 worktree->root_path, path);
3803 if (err)
3804 goto done;
3806 arg.ignores = &ignores;
3807 err = got_fileindex_diff_dir(fileindex, fd,
3808 worktree->root_path, path, repo, &fdiff_cb, &arg);
3810 done:
3811 free_ignores(&ignores);
3812 if (fd != -1 && close(fd) == -1 && err == NULL)
3813 err = got_error_from_errno("close");
3814 free(ondisk_path);
3815 return err;
3818 const struct got_error *
3819 got_worktree_status(struct got_worktree *worktree,
3820 struct got_pathlist_head *paths, struct got_repository *repo,
3821 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3822 got_cancel_cb cancel_cb, void *cancel_arg)
3824 const struct got_error *err = NULL;
3825 char *fileindex_path = NULL;
3826 struct got_fileindex *fileindex = NULL;
3827 struct got_pathlist_entry *pe;
3829 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3830 if (err)
3831 return err;
3833 TAILQ_FOREACH(pe, paths, entry) {
3834 err = worktree_status(worktree, pe->path, fileindex, repo,
3835 status_cb, status_arg, cancel_cb, cancel_arg,
3836 no_ignores, 0);
3837 if (err)
3838 break;
3840 free(fileindex_path);
3841 got_fileindex_free(fileindex);
3842 return err;
3845 const struct got_error *
3846 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3847 const char *arg)
3849 const struct got_error *err = NULL;
3850 char *resolved = NULL, *cwd = NULL, *path = NULL;
3851 size_t len;
3852 struct stat sb;
3853 char *abspath = NULL;
3854 char canonpath[PATH_MAX];
3856 *wt_path = NULL;
3858 cwd = getcwd(NULL, 0);
3859 if (cwd == NULL)
3860 return got_error_from_errno("getcwd");
3862 if (lstat(arg, &sb) == -1) {
3863 if (errno != ENOENT) {
3864 err = got_error_from_errno2("lstat", arg);
3865 goto done;
3867 sb.st_mode = 0;
3869 if (S_ISLNK(sb.st_mode)) {
3871 * We cannot use realpath(3) with symlinks since we want to
3872 * operate on the symlink itself.
3873 * But we can make the path absolute, assuming it is relative
3874 * to the current working directory, and then canonicalize it.
3876 if (!got_path_is_absolute(arg)) {
3877 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3878 err = got_error_from_errno("asprintf");
3879 goto done;
3883 err = got_canonpath(abspath ? abspath : arg, canonpath,
3884 sizeof(canonpath));
3885 if (err)
3886 goto done;
3887 resolved = strdup(canonpath);
3888 if (resolved == NULL) {
3889 err = got_error_from_errno("strdup");
3890 goto done;
3892 } else {
3893 resolved = realpath(arg, NULL);
3894 if (resolved == NULL) {
3895 if (errno != ENOENT) {
3896 err = got_error_from_errno2("realpath", arg);
3897 goto done;
3899 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3900 err = got_error_from_errno("asprintf");
3901 goto done;
3903 err = got_canonpath(abspath, canonpath,
3904 sizeof(canonpath));
3905 if (err)
3906 goto done;
3907 resolved = strdup(canonpath);
3908 if (resolved == NULL) {
3909 err = got_error_from_errno("strdup");
3910 goto done;
3915 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3916 strlen(got_worktree_get_root_path(worktree)))) {
3917 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3918 goto done;
3921 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3922 err = got_path_skip_common_ancestor(&path,
3923 got_worktree_get_root_path(worktree), resolved);
3924 if (err)
3925 goto done;
3926 } else {
3927 path = strdup("");
3928 if (path == NULL) {
3929 err = got_error_from_errno("strdup");
3930 goto done;
3934 /* XXX status walk can't deal with trailing slash! */
3935 len = strlen(path);
3936 while (len > 0 && path[len - 1] == '/') {
3937 path[len - 1] = '\0';
3938 len--;
3940 done:
3941 free(abspath);
3942 free(resolved);
3943 free(cwd);
3944 if (err == NULL)
3945 *wt_path = path;
3946 else
3947 free(path);
3948 return err;
3951 struct schedule_addition_args {
3952 struct got_worktree *worktree;
3953 struct got_fileindex *fileindex;
3954 got_worktree_checkout_cb progress_cb;
3955 void *progress_arg;
3956 struct got_repository *repo;
3959 static const struct got_error *
3960 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3961 const char *relpath, struct got_object_id *blob_id,
3962 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3963 int dirfd, const char *de_name)
3965 struct schedule_addition_args *a = arg;
3966 const struct got_error *err = NULL;
3967 struct got_fileindex_entry *ie;
3968 struct stat sb;
3969 char *ondisk_path;
3971 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3972 relpath) == -1)
3973 return got_error_from_errno("asprintf");
3975 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3976 if (ie) {
3977 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3978 de_name, a->repo);
3979 if (err)
3980 goto done;
3981 /* Re-adding an existing entry is a no-op. */
3982 if (status == GOT_STATUS_ADD)
3983 goto done;
3984 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3985 if (err)
3986 goto done;
3989 if (status != GOT_STATUS_UNVERSIONED) {
3990 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3991 goto done;
3994 err = got_fileindex_entry_alloc(&ie, relpath);
3995 if (err)
3996 goto done;
3997 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3998 relpath, NULL, NULL, 1);
3999 if (err) {
4000 got_fileindex_entry_free(ie);
4001 goto done;
4003 err = got_fileindex_entry_add(a->fileindex, ie);
4004 if (err) {
4005 got_fileindex_entry_free(ie);
4006 goto done;
4008 done:
4009 free(ondisk_path);
4010 if (err)
4011 return err;
4012 if (status == GOT_STATUS_ADD)
4013 return NULL;
4014 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4017 const struct got_error *
4018 got_worktree_schedule_add(struct got_worktree *worktree,
4019 struct got_pathlist_head *paths,
4020 got_worktree_checkout_cb progress_cb, void *progress_arg,
4021 struct got_repository *repo, int no_ignores)
4023 struct got_fileindex *fileindex = NULL;
4024 char *fileindex_path = NULL;
4025 const struct got_error *err = NULL, *sync_err, *unlockerr;
4026 struct got_pathlist_entry *pe;
4027 struct schedule_addition_args saa;
4029 err = lock_worktree(worktree, LOCK_EX);
4030 if (err)
4031 return err;
4033 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4034 if (err)
4035 goto done;
4037 saa.worktree = worktree;
4038 saa.fileindex = fileindex;
4039 saa.progress_cb = progress_cb;
4040 saa.progress_arg = progress_arg;
4041 saa.repo = repo;
4043 TAILQ_FOREACH(pe, paths, entry) {
4044 err = worktree_status(worktree, pe->path, fileindex, repo,
4045 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4046 if (err)
4047 break;
4049 sync_err = sync_fileindex(fileindex, fileindex_path);
4050 if (sync_err && err == NULL)
4051 err = sync_err;
4052 done:
4053 free(fileindex_path);
4054 if (fileindex)
4055 got_fileindex_free(fileindex);
4056 unlockerr = lock_worktree(worktree, LOCK_SH);
4057 if (unlockerr && err == NULL)
4058 err = unlockerr;
4059 return err;
4062 struct schedule_deletion_args {
4063 struct got_worktree *worktree;
4064 struct got_fileindex *fileindex;
4065 got_worktree_delete_cb progress_cb;
4066 void *progress_arg;
4067 struct got_repository *repo;
4068 int delete_local_mods;
4069 int keep_on_disk;
4070 const char *status_codes;
4073 static const struct got_error *
4074 schedule_for_deletion(void *arg, unsigned char status,
4075 unsigned char staged_status, const char *relpath,
4076 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4077 struct got_object_id *commit_id, int dirfd, const char *de_name)
4079 struct schedule_deletion_args *a = arg;
4080 const struct got_error *err = NULL;
4081 struct got_fileindex_entry *ie = NULL;
4082 struct stat sb;
4083 char *ondisk_path;
4085 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4086 if (ie == NULL)
4087 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4089 staged_status = get_staged_status(ie);
4090 if (staged_status != GOT_STATUS_NO_CHANGE) {
4091 if (staged_status == GOT_STATUS_DELETE)
4092 return NULL;
4093 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4096 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4097 relpath) == -1)
4098 return got_error_from_errno("asprintf");
4100 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4101 a->repo);
4102 if (err)
4103 goto done;
4105 if (a->status_codes) {
4106 size_t ncodes = strlen(a->status_codes);
4107 int i;
4108 for (i = 0; i < ncodes ; i++) {
4109 if (status == a->status_codes[i])
4110 break;
4112 if (i == ncodes) {
4113 /* Do not delete files in non-matching status. */
4114 free(ondisk_path);
4115 return NULL;
4117 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4118 a->status_codes[i] != GOT_STATUS_MISSING) {
4119 static char msg[64];
4120 snprintf(msg, sizeof(msg),
4121 "invalid status code '%c'", a->status_codes[i]);
4122 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4123 goto done;
4127 if (status != GOT_STATUS_NO_CHANGE) {
4128 if (status == GOT_STATUS_DELETE)
4129 goto done;
4130 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4131 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4132 goto done;
4134 if (status != GOT_STATUS_MODIFY &&
4135 status != GOT_STATUS_MISSING) {
4136 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4137 goto done;
4141 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4142 size_t root_len;
4144 if (dirfd != -1) {
4145 if (unlinkat(dirfd, de_name, 0) != 0) {
4146 err = got_error_from_errno2("unlinkat",
4147 ondisk_path);
4148 goto done;
4150 } else if (unlink(ondisk_path) != 0) {
4151 err = got_error_from_errno2("unlink", ondisk_path);
4152 goto done;
4155 root_len = strlen(a->worktree->root_path);
4156 do {
4157 char *parent;
4158 err = got_path_dirname(&parent, ondisk_path);
4159 if (err)
4160 goto done;
4161 free(ondisk_path);
4162 ondisk_path = parent;
4163 if (rmdir(ondisk_path) == -1) {
4164 if (errno != ENOTEMPTY)
4165 err = got_error_from_errno2("rmdir",
4166 ondisk_path);
4167 break;
4169 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4170 strlen(ondisk_path), root_len) != 0);
4173 got_fileindex_entry_mark_deleted_from_disk(ie);
4174 done:
4175 free(ondisk_path);
4176 if (err)
4177 return err;
4178 if (status == GOT_STATUS_DELETE)
4179 return NULL;
4180 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4181 staged_status, relpath);
4184 const struct got_error *
4185 got_worktree_schedule_delete(struct got_worktree *worktree,
4186 struct got_pathlist_head *paths, int delete_local_mods,
4187 const char *status_codes,
4188 got_worktree_delete_cb progress_cb, void *progress_arg,
4189 struct got_repository *repo, int keep_on_disk)
4191 struct got_fileindex *fileindex = NULL;
4192 char *fileindex_path = NULL;
4193 const struct got_error *err = NULL, *sync_err, *unlockerr;
4194 struct got_pathlist_entry *pe;
4195 struct schedule_deletion_args sda;
4197 err = lock_worktree(worktree, LOCK_EX);
4198 if (err)
4199 return err;
4201 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4202 if (err)
4203 goto done;
4205 sda.worktree = worktree;
4206 sda.fileindex = fileindex;
4207 sda.progress_cb = progress_cb;
4208 sda.progress_arg = progress_arg;
4209 sda.repo = repo;
4210 sda.delete_local_mods = delete_local_mods;
4211 sda.keep_on_disk = keep_on_disk;
4212 sda.status_codes = status_codes;
4214 TAILQ_FOREACH(pe, paths, entry) {
4215 err = worktree_status(worktree, pe->path, fileindex, repo,
4216 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4217 if (err)
4218 break;
4220 sync_err = sync_fileindex(fileindex, fileindex_path);
4221 if (sync_err && err == NULL)
4222 err = sync_err;
4223 done:
4224 free(fileindex_path);
4225 if (fileindex)
4226 got_fileindex_free(fileindex);
4227 unlockerr = lock_worktree(worktree, LOCK_SH);
4228 if (unlockerr && err == NULL)
4229 err = unlockerr;
4230 return err;
4233 static const struct got_error *
4234 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4236 const struct got_error *err = NULL;
4237 char *line = NULL;
4238 size_t linesize = 0, n;
4239 ssize_t linelen;
4241 linelen = getline(&line, &linesize, infile);
4242 if (linelen == -1) {
4243 if (ferror(infile)) {
4244 err = got_error_from_errno("getline");
4245 goto done;
4247 return NULL;
4249 if (outfile) {
4250 n = fwrite(line, 1, linelen, outfile);
4251 if (n != linelen) {
4252 err = got_ferror(outfile, GOT_ERR_IO);
4253 goto done;
4256 if (rejectfile) {
4257 n = fwrite(line, 1, linelen, rejectfile);
4258 if (n != linelen)
4259 err = got_ferror(outfile, GOT_ERR_IO);
4261 done:
4262 free(line);
4263 return err;
4266 static const struct got_error *
4267 skip_one_line(FILE *f)
4269 char *line = NULL;
4270 size_t linesize = 0;
4271 ssize_t linelen;
4273 linelen = getline(&line, &linesize, f);
4274 if (linelen == -1) {
4275 if (ferror(f))
4276 return got_error_from_errno("getline");
4277 return NULL;
4279 free(line);
4280 return NULL;
4283 static const struct got_error *
4284 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4285 int start_old, int end_old, int start_new, int end_new,
4286 FILE *outfile, FILE *rejectfile)
4288 const struct got_error *err;
4290 /* Copy old file's lines leading up to patch. */
4291 while (!feof(f1) && *line_cur1 < start_old) {
4292 err = copy_one_line(f1, outfile, NULL);
4293 if (err)
4294 return err;
4295 (*line_cur1)++;
4297 /* Skip new file's lines leading up to patch. */
4298 while (!feof(f2) && *line_cur2 < start_new) {
4299 if (rejectfile)
4300 err = copy_one_line(f2, NULL, rejectfile);
4301 else
4302 err = skip_one_line(f2);
4303 if (err)
4304 return err;
4305 (*line_cur2)++;
4307 /* Copy patched lines. */
4308 while (!feof(f2) && *line_cur2 <= end_new) {
4309 err = copy_one_line(f2, outfile, NULL);
4310 if (err)
4311 return err;
4312 (*line_cur2)++;
4314 /* Skip over old file's replaced lines. */
4315 while (!feof(f1) && *line_cur1 <= end_old) {
4316 if (rejectfile)
4317 err = copy_one_line(f1, NULL, rejectfile);
4318 else
4319 err = skip_one_line(f1);
4320 if (err)
4321 return err;
4322 (*line_cur1)++;
4325 return NULL;
4328 static const struct got_error *
4329 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4330 FILE *outfile, FILE *rejectfile)
4332 const struct got_error *err;
4334 if (outfile) {
4335 /* Copy old file's lines until EOF. */
4336 while (!feof(f1)) {
4337 err = copy_one_line(f1, outfile, NULL);
4338 if (err)
4339 return err;
4340 (*line_cur1)++;
4343 if (rejectfile) {
4344 /* Copy new file's lines until EOF. */
4345 while (!feof(f2)) {
4346 err = copy_one_line(f2, NULL, rejectfile);
4347 if (err)
4348 return err;
4349 (*line_cur2)++;
4353 return NULL;
4356 static const struct got_error *
4357 apply_or_reject_change(int *choice, int *nchunks_used,
4358 struct diff_result *diff_result, int n,
4359 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4360 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4361 got_worktree_patch_cb patch_cb, void *patch_arg)
4363 const struct got_error *err = NULL;
4364 struct diff_chunk_context cc = {};
4365 int start_old, end_old, start_new, end_new;
4366 FILE *hunkfile;
4367 struct diff_output_unidiff_state *diff_state;
4368 struct diff_input_info diff_info;
4369 int rc;
4371 *choice = GOT_PATCH_CHOICE_NONE;
4373 /* Get changed line numbers without context lines for copy_change(). */
4374 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4375 start_old = cc.left.start;
4376 end_old = cc.left.end;
4377 start_new = cc.right.start;
4378 end_new = cc.right.end;
4380 /* Get the same change with context lines for display. */
4381 memset(&cc, 0, sizeof(cc));
4382 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4384 memset(&diff_info, 0, sizeof(diff_info));
4385 diff_info.left_path = relpath;
4386 diff_info.right_path = relpath;
4388 diff_state = diff_output_unidiff_state_alloc();
4389 if (diff_state == NULL)
4390 return got_error_set_errno(ENOMEM,
4391 "diff_output_unidiff_state_alloc");
4393 hunkfile = got_opentemp();
4394 if (hunkfile == NULL) {
4395 err = got_error_from_errno("got_opentemp");
4396 goto done;
4399 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4400 diff_result, &cc);
4401 if (rc != DIFF_RC_OK) {
4402 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4403 goto done;
4406 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4407 err = got_ferror(hunkfile, GOT_ERR_IO);
4408 goto done;
4411 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4412 hunkfile, changeno, nchanges);
4413 if (err)
4414 goto done;
4416 switch (*choice) {
4417 case GOT_PATCH_CHOICE_YES:
4418 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4419 end_old, start_new, end_new, outfile, rejectfile);
4420 break;
4421 case GOT_PATCH_CHOICE_NO:
4422 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4423 end_old, start_new, end_new, rejectfile, outfile);
4424 break;
4425 case GOT_PATCH_CHOICE_QUIT:
4426 break;
4427 default:
4428 err = got_error(GOT_ERR_PATCH_CHOICE);
4429 break;
4431 done:
4432 diff_output_unidiff_state_free(diff_state);
4433 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4434 err = got_error_from_errno("fclose");
4435 return err;
4438 struct revert_file_args {
4439 struct got_worktree *worktree;
4440 struct got_fileindex *fileindex;
4441 got_worktree_checkout_cb progress_cb;
4442 void *progress_arg;
4443 got_worktree_patch_cb patch_cb;
4444 void *patch_arg;
4445 struct got_repository *repo;
4446 int unlink_added_files;
4449 static const struct got_error *
4450 create_patched_content(char **path_outfile, int reverse_patch,
4451 struct got_object_id *blob_id, const char *path2,
4452 int dirfd2, const char *de_name2,
4453 const char *relpath, struct got_repository *repo,
4454 got_worktree_patch_cb patch_cb, void *patch_arg)
4456 const struct got_error *err, *free_err;
4457 struct got_blob_object *blob = NULL;
4458 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4459 int fd2 = -1;
4460 char link_target[PATH_MAX];
4461 ssize_t link_len = 0;
4462 char *path1 = NULL, *id_str = NULL;
4463 struct stat sb2;
4464 struct got_diffreg_result *diffreg_result = NULL;
4465 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4466 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4468 *path_outfile = NULL;
4470 err = got_object_id_str(&id_str, blob_id);
4471 if (err)
4472 return err;
4474 if (dirfd2 != -1) {
4475 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4476 if (fd2 == -1) {
4477 if (!got_err_open_nofollow_on_symlink()) {
4478 err = got_error_from_errno2("openat", path2);
4479 goto done;
4481 link_len = readlinkat(dirfd2, de_name2,
4482 link_target, sizeof(link_target));
4483 if (link_len == -1)
4484 return got_error_from_errno2("readlinkat", path2);
4485 sb2.st_mode = S_IFLNK;
4486 sb2.st_size = link_len;
4488 } else {
4489 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4490 if (fd2 == -1) {
4491 if (!got_err_open_nofollow_on_symlink()) {
4492 err = got_error_from_errno2("open", path2);
4493 goto done;
4495 link_len = readlink(path2, link_target,
4496 sizeof(link_target));
4497 if (link_len == -1)
4498 return got_error_from_errno2("readlink", path2);
4499 sb2.st_mode = S_IFLNK;
4500 sb2.st_size = link_len;
4503 if (fd2 != -1) {
4504 if (fstat(fd2, &sb2) == -1) {
4505 err = got_error_from_errno2("fstat", path2);
4506 goto done;
4509 f2 = fdopen(fd2, "r");
4510 if (f2 == NULL) {
4511 err = got_error_from_errno2("fdopen", path2);
4512 goto done;
4514 fd2 = -1;
4515 } else {
4516 size_t n;
4517 f2 = got_opentemp();
4518 if (f2 == NULL) {
4519 err = got_error_from_errno2("got_opentemp", path2);
4520 goto done;
4522 n = fwrite(link_target, 1, link_len, f2);
4523 if (n != link_len) {
4524 err = got_ferror(f2, GOT_ERR_IO);
4525 goto done;
4527 if (fflush(f2) == EOF) {
4528 err = got_error_from_errno("fflush");
4529 goto done;
4531 rewind(f2);
4534 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4535 if (err)
4536 goto done;
4538 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4539 if (err)
4540 goto done;
4542 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4543 if (err)
4544 goto done;
4546 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4547 NULL);
4548 if (err)
4549 goto done;
4551 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4552 if (err)
4553 goto done;
4555 if (fseek(f1, 0L, SEEK_SET) == -1)
4556 return got_ferror(f1, GOT_ERR_IO);
4557 if (fseek(f2, 0L, SEEK_SET) == -1)
4558 return got_ferror(f2, GOT_ERR_IO);
4560 /* Count the number of actual changes in the diff result. */
4561 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4562 struct diff_chunk_context cc = {};
4563 diff_chunk_context_load_change(&cc, &nchunks_used,
4564 diffreg_result->result, n, 0);
4565 nchanges++;
4567 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4568 int choice;
4569 err = apply_or_reject_change(&choice, &nchunks_used,
4570 diffreg_result->result, n, relpath, f1, f2,
4571 &line_cur1, &line_cur2,
4572 reverse_patch ? NULL : outfile,
4573 reverse_patch ? outfile : NULL,
4574 ++i, nchanges, patch_cb, patch_arg);
4575 if (err)
4576 goto done;
4577 if (choice == GOT_PATCH_CHOICE_YES)
4578 have_content = 1;
4579 else if (choice == GOT_PATCH_CHOICE_QUIT)
4580 break;
4582 if (have_content) {
4583 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4584 reverse_patch ? NULL : outfile,
4585 reverse_patch ? outfile : NULL);
4586 if (err)
4587 goto done;
4589 if (!S_ISLNK(sb2.st_mode)) {
4590 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4591 err = got_error_from_errno2("fchmod", path2);
4592 goto done;
4596 done:
4597 free(id_str);
4598 if (blob)
4599 got_object_blob_close(blob);
4600 free_err = got_diffreg_result_free(diffreg_result);
4601 if (err == NULL)
4602 err = free_err;
4603 if (f1 && fclose(f1) == EOF && err == NULL)
4604 err = got_error_from_errno2("fclose", path1);
4605 if (f2 && fclose(f2) == EOF && err == NULL)
4606 err = got_error_from_errno2("fclose", path2);
4607 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4608 err = got_error_from_errno2("close", path2);
4609 if (outfile && fclose(outfile) == EOF && err == NULL)
4610 err = got_error_from_errno2("fclose", *path_outfile);
4611 if (path1 && unlink(path1) == -1 && err == NULL)
4612 err = got_error_from_errno2("unlink", path1);
4613 if (err || !have_content) {
4614 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4615 err = got_error_from_errno2("unlink", *path_outfile);
4616 free(*path_outfile);
4617 *path_outfile = NULL;
4619 free(path1);
4620 return err;
4623 static const struct got_error *
4624 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4625 const char *relpath, struct got_object_id *blob_id,
4626 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4627 int dirfd, const char *de_name)
4629 struct revert_file_args *a = arg;
4630 const struct got_error *err = NULL;
4631 char *parent_path = NULL;
4632 struct got_fileindex_entry *ie;
4633 struct got_tree_object *tree = NULL;
4634 struct got_object_id *tree_id = NULL;
4635 const struct got_tree_entry *te = NULL;
4636 char *tree_path = NULL, *te_name;
4637 char *ondisk_path = NULL, *path_content = NULL;
4638 struct got_blob_object *blob = NULL;
4640 /* Reverting a staged deletion is a no-op. */
4641 if (status == GOT_STATUS_DELETE &&
4642 staged_status != GOT_STATUS_NO_CHANGE)
4643 return NULL;
4645 if (status == GOT_STATUS_UNVERSIONED)
4646 return (*a->progress_cb)(a->progress_arg,
4647 GOT_STATUS_UNVERSIONED, relpath);
4649 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4650 if (ie == NULL)
4651 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4653 /* Construct in-repository path of tree which contains this blob. */
4654 err = got_path_dirname(&parent_path, ie->path);
4655 if (err) {
4656 if (err->code != GOT_ERR_BAD_PATH)
4657 goto done;
4658 parent_path = strdup("/");
4659 if (parent_path == NULL) {
4660 err = got_error_from_errno("strdup");
4661 goto done;
4664 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4665 tree_path = strdup(parent_path);
4666 if (tree_path == NULL) {
4667 err = got_error_from_errno("strdup");
4668 goto done;
4670 } else {
4671 if (got_path_is_root_dir(parent_path)) {
4672 tree_path = strdup(a->worktree->path_prefix);
4673 if (tree_path == NULL) {
4674 err = got_error_from_errno("strdup");
4675 goto done;
4677 } else {
4678 if (asprintf(&tree_path, "%s/%s",
4679 a->worktree->path_prefix, parent_path) == -1) {
4680 err = got_error_from_errno("asprintf");
4681 goto done;
4686 err = got_object_id_by_path(&tree_id, a->repo,
4687 a->worktree->base_commit_id, tree_path);
4688 if (err) {
4689 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4690 (status == GOT_STATUS_ADD ||
4691 staged_status == GOT_STATUS_ADD)))
4692 goto done;
4693 } else {
4694 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4695 if (err)
4696 goto done;
4698 err = got_path_basename(&te_name, ie->path);
4699 if (err)
4700 goto done;
4702 te = got_object_tree_find_entry(tree, te_name);
4703 free(te_name);
4704 if (te == NULL && status != GOT_STATUS_ADD &&
4705 staged_status != GOT_STATUS_ADD) {
4706 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4707 goto done;
4711 switch (status) {
4712 case GOT_STATUS_ADD:
4713 if (a->patch_cb) {
4714 int choice = GOT_PATCH_CHOICE_NONE;
4715 err = (*a->patch_cb)(&choice, a->patch_arg,
4716 status, ie->path, NULL, 1, 1);
4717 if (err)
4718 goto done;
4719 if (choice != GOT_PATCH_CHOICE_YES)
4720 break;
4722 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4723 ie->path);
4724 if (err)
4725 goto done;
4726 got_fileindex_entry_remove(a->fileindex, ie);
4727 if (a->unlink_added_files) {
4728 if (asprintf(&ondisk_path, "%s/%s",
4729 got_worktree_get_root_path(a->worktree),
4730 relpath) == -1) {
4731 err = got_error_from_errno("asprintf");
4732 goto done;
4734 if (unlink(ondisk_path) == -1) {
4735 err = got_error_from_errno2("unlink",
4736 ondisk_path);
4737 break;
4740 break;
4741 case GOT_STATUS_DELETE:
4742 if (a->patch_cb) {
4743 int choice = GOT_PATCH_CHOICE_NONE;
4744 err = (*a->patch_cb)(&choice, a->patch_arg,
4745 status, ie->path, NULL, 1, 1);
4746 if (err)
4747 goto done;
4748 if (choice != GOT_PATCH_CHOICE_YES)
4749 break;
4751 /* fall through */
4752 case GOT_STATUS_MODIFY:
4753 case GOT_STATUS_MODE_CHANGE:
4754 case GOT_STATUS_CONFLICT:
4755 case GOT_STATUS_MISSING: {
4756 struct got_object_id id;
4757 if (staged_status == GOT_STATUS_ADD ||
4758 staged_status == GOT_STATUS_MODIFY) {
4759 memcpy(id.sha1, ie->staged_blob_sha1,
4760 SHA1_DIGEST_LENGTH);
4761 } else
4762 memcpy(id.sha1, ie->blob_sha1,
4763 SHA1_DIGEST_LENGTH);
4764 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4765 if (err)
4766 goto done;
4768 if (asprintf(&ondisk_path, "%s/%s",
4769 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4770 err = got_error_from_errno("asprintf");
4771 goto done;
4774 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4775 status == GOT_STATUS_CONFLICT)) {
4776 int is_bad_symlink = 0;
4777 err = create_patched_content(&path_content, 1, &id,
4778 ondisk_path, dirfd, de_name, ie->path, a->repo,
4779 a->patch_cb, a->patch_arg);
4780 if (err || path_content == NULL)
4781 break;
4782 if (te && S_ISLNK(te->mode)) {
4783 if (unlink(path_content) == -1) {
4784 err = got_error_from_errno2("unlink",
4785 path_content);
4786 break;
4788 err = install_symlink(&is_bad_symlink,
4789 a->worktree, ondisk_path, ie->path,
4790 blob, 0, 1, 0, 0, a->repo,
4791 a->progress_cb, a->progress_arg);
4792 } else {
4793 if (rename(path_content, ondisk_path) == -1) {
4794 err = got_error_from_errno3("rename",
4795 path_content, ondisk_path);
4796 goto done;
4799 } else {
4800 int is_bad_symlink = 0;
4801 if (te && S_ISLNK(te->mode)) {
4802 err = install_symlink(&is_bad_symlink,
4803 a->worktree, ondisk_path, ie->path,
4804 blob, 0, 1, 0, 0, a->repo,
4805 a->progress_cb, a->progress_arg);
4806 } else {
4807 err = install_blob(a->worktree, ondisk_path,
4808 ie->path,
4809 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4810 got_fileindex_perms_to_st(ie), blob,
4811 0, 1, 0, 0, a->repo,
4812 a->progress_cb, a->progress_arg);
4814 if (err)
4815 goto done;
4816 if (status == GOT_STATUS_DELETE ||
4817 status == GOT_STATUS_MODE_CHANGE) {
4818 err = got_fileindex_entry_update(ie,
4819 a->worktree->root_fd, relpath,
4820 blob->id.sha1,
4821 a->worktree->base_commit_id->sha1, 1);
4822 if (err)
4823 goto done;
4825 if (is_bad_symlink) {
4826 got_fileindex_entry_filetype_set(ie,
4827 GOT_FILEIDX_MODE_BAD_SYMLINK);
4830 break;
4832 default:
4833 break;
4835 done:
4836 free(ondisk_path);
4837 free(path_content);
4838 free(parent_path);
4839 free(tree_path);
4840 if (blob)
4841 got_object_blob_close(blob);
4842 if (tree)
4843 got_object_tree_close(tree);
4844 free(tree_id);
4845 return err;
4848 const struct got_error *
4849 got_worktree_revert(struct got_worktree *worktree,
4850 struct got_pathlist_head *paths,
4851 got_worktree_checkout_cb progress_cb, void *progress_arg,
4852 got_worktree_patch_cb patch_cb, void *patch_arg,
4853 struct got_repository *repo)
4855 struct got_fileindex *fileindex = NULL;
4856 char *fileindex_path = NULL;
4857 const struct got_error *err = NULL, *unlockerr = NULL;
4858 const struct got_error *sync_err = NULL;
4859 struct got_pathlist_entry *pe;
4860 struct revert_file_args rfa;
4862 err = lock_worktree(worktree, LOCK_EX);
4863 if (err)
4864 return err;
4866 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4867 if (err)
4868 goto done;
4870 rfa.worktree = worktree;
4871 rfa.fileindex = fileindex;
4872 rfa.progress_cb = progress_cb;
4873 rfa.progress_arg = progress_arg;
4874 rfa.patch_cb = patch_cb;
4875 rfa.patch_arg = patch_arg;
4876 rfa.repo = repo;
4877 rfa.unlink_added_files = 0;
4878 TAILQ_FOREACH(pe, paths, entry) {
4879 err = worktree_status(worktree, pe->path, fileindex, repo,
4880 revert_file, &rfa, NULL, NULL, 0, 0);
4881 if (err)
4882 break;
4884 sync_err = sync_fileindex(fileindex, fileindex_path);
4885 if (sync_err && err == NULL)
4886 err = sync_err;
4887 done:
4888 free(fileindex_path);
4889 if (fileindex)
4890 got_fileindex_free(fileindex);
4891 unlockerr = lock_worktree(worktree, LOCK_SH);
4892 if (unlockerr && err == NULL)
4893 err = unlockerr;
4894 return err;
4897 static void
4898 free_commitable(struct got_commitable *ct)
4900 free(ct->path);
4901 free(ct->in_repo_path);
4902 free(ct->ondisk_path);
4903 free(ct->blob_id);
4904 free(ct->base_blob_id);
4905 free(ct->staged_blob_id);
4906 free(ct->base_commit_id);
4907 free(ct);
4910 struct collect_commitables_arg {
4911 struct got_pathlist_head *commitable_paths;
4912 struct got_repository *repo;
4913 struct got_worktree *worktree;
4914 struct got_fileindex *fileindex;
4915 int have_staged_files;
4916 int allow_bad_symlinks;
4919 static const struct got_error *
4920 collect_commitables(void *arg, unsigned char status,
4921 unsigned char staged_status, const char *relpath,
4922 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4923 struct got_object_id *commit_id, int dirfd, const char *de_name)
4925 struct collect_commitables_arg *a = arg;
4926 const struct got_error *err = NULL;
4927 struct got_commitable *ct = NULL;
4928 struct got_pathlist_entry *new = NULL;
4929 char *parent_path = NULL, *path = NULL;
4930 struct stat sb;
4932 if (a->have_staged_files) {
4933 if (staged_status != GOT_STATUS_MODIFY &&
4934 staged_status != GOT_STATUS_ADD &&
4935 staged_status != GOT_STATUS_DELETE)
4936 return NULL;
4937 } else {
4938 if (status == GOT_STATUS_CONFLICT)
4939 return got_error(GOT_ERR_COMMIT_CONFLICT);
4941 if (status != GOT_STATUS_MODIFY &&
4942 status != GOT_STATUS_MODE_CHANGE &&
4943 status != GOT_STATUS_ADD &&
4944 status != GOT_STATUS_DELETE)
4945 return NULL;
4948 if (asprintf(&path, "/%s", relpath) == -1) {
4949 err = got_error_from_errno("asprintf");
4950 goto done;
4952 if (strcmp(path, "/") == 0) {
4953 parent_path = strdup("");
4954 if (parent_path == NULL)
4955 return got_error_from_errno("strdup");
4956 } else {
4957 err = got_path_dirname(&parent_path, path);
4958 if (err)
4959 return err;
4962 ct = calloc(1, sizeof(*ct));
4963 if (ct == NULL) {
4964 err = got_error_from_errno("calloc");
4965 goto done;
4968 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4969 relpath) == -1) {
4970 err = got_error_from_errno("asprintf");
4971 goto done;
4974 if (staged_status == GOT_STATUS_ADD ||
4975 staged_status == GOT_STATUS_MODIFY) {
4976 struct got_fileindex_entry *ie;
4977 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4978 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4979 case GOT_FILEIDX_MODE_REGULAR_FILE:
4980 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4981 ct->mode = S_IFREG;
4982 break;
4983 case GOT_FILEIDX_MODE_SYMLINK:
4984 ct->mode = S_IFLNK;
4985 break;
4986 default:
4987 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4988 goto done;
4990 ct->mode |= got_fileindex_entry_perms_get(ie);
4991 } else if (status != GOT_STATUS_DELETE &&
4992 staged_status != GOT_STATUS_DELETE) {
4993 if (dirfd != -1) {
4994 if (fstatat(dirfd, de_name, &sb,
4995 AT_SYMLINK_NOFOLLOW) == -1) {
4996 err = got_error_from_errno2("fstatat",
4997 ct->ondisk_path);
4998 goto done;
5000 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5001 err = got_error_from_errno2("lstat", ct->ondisk_path);
5002 goto done;
5004 ct->mode = sb.st_mode;
5007 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5008 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5009 relpath) == -1) {
5010 err = got_error_from_errno("asprintf");
5011 goto done;
5014 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5015 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5016 int is_bad_symlink;
5017 char target_path[PATH_MAX];
5018 ssize_t target_len;
5019 target_len = readlink(ct->ondisk_path, target_path,
5020 sizeof(target_path));
5021 if (target_len == -1) {
5022 err = got_error_from_errno2("readlink",
5023 ct->ondisk_path);
5024 goto done;
5026 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5027 target_len, ct->ondisk_path, a->worktree->root_path);
5028 if (err)
5029 goto done;
5030 if (is_bad_symlink) {
5031 err = got_error_path(ct->ondisk_path,
5032 GOT_ERR_BAD_SYMLINK);
5033 goto done;
5038 ct->status = status;
5039 ct->staged_status = staged_status;
5040 ct->blob_id = NULL; /* will be filled in when blob gets created */
5041 if (ct->status != GOT_STATUS_ADD &&
5042 ct->staged_status != GOT_STATUS_ADD) {
5043 ct->base_blob_id = got_object_id_dup(blob_id);
5044 if (ct->base_blob_id == NULL) {
5045 err = got_error_from_errno("got_object_id_dup");
5046 goto done;
5048 ct->base_commit_id = got_object_id_dup(commit_id);
5049 if (ct->base_commit_id == NULL) {
5050 err = got_error_from_errno("got_object_id_dup");
5051 goto done;
5054 if (ct->staged_status == GOT_STATUS_ADD ||
5055 ct->staged_status == GOT_STATUS_MODIFY) {
5056 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5057 if (ct->staged_blob_id == NULL) {
5058 err = got_error_from_errno("got_object_id_dup");
5059 goto done;
5062 ct->path = strdup(path);
5063 if (ct->path == NULL) {
5064 err = got_error_from_errno("strdup");
5065 goto done;
5067 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5068 done:
5069 if (ct && (err || new == NULL))
5070 free_commitable(ct);
5071 free(parent_path);
5072 free(path);
5073 return err;
5076 static const struct got_error *write_tree(struct got_object_id **, int *,
5077 struct got_tree_object *, const char *, struct got_pathlist_head *,
5078 got_worktree_status_cb status_cb, void *status_arg,
5079 struct got_repository *);
5081 static const struct got_error *
5082 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5083 struct got_tree_entry *te, const char *parent_path,
5084 struct got_pathlist_head *commitable_paths,
5085 got_worktree_status_cb status_cb, void *status_arg,
5086 struct got_repository *repo)
5088 const struct got_error *err = NULL;
5089 struct got_tree_object *subtree;
5090 char *subpath;
5092 if (asprintf(&subpath, "%s%s%s", parent_path,
5093 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5094 return got_error_from_errno("asprintf");
5096 err = got_object_open_as_tree(&subtree, repo, &te->id);
5097 if (err)
5098 return err;
5100 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5101 commitable_paths, status_cb, status_arg, repo);
5102 got_object_tree_close(subtree);
5103 free(subpath);
5104 return err;
5107 static const struct got_error *
5108 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5110 const struct got_error *err = NULL;
5111 char *ct_parent_path = NULL;
5113 *match = 0;
5115 if (strchr(ct->in_repo_path, '/') == NULL) {
5116 *match = got_path_is_root_dir(path);
5117 return NULL;
5120 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5121 if (err)
5122 return err;
5123 *match = (strcmp(path, ct_parent_path) == 0);
5124 free(ct_parent_path);
5125 return err;
5128 static mode_t
5129 get_ct_file_mode(struct got_commitable *ct)
5131 if (S_ISLNK(ct->mode))
5132 return S_IFLNK;
5134 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5137 static const struct got_error *
5138 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5139 struct got_tree_entry *te, struct got_commitable *ct)
5141 const struct got_error *err = NULL;
5143 *new_te = NULL;
5145 err = got_object_tree_entry_dup(new_te, te);
5146 if (err)
5147 goto done;
5149 (*new_te)->mode = get_ct_file_mode(ct);
5151 if (ct->staged_status == GOT_STATUS_MODIFY)
5152 memcpy(&(*new_te)->id, ct->staged_blob_id,
5153 sizeof((*new_te)->id));
5154 else
5155 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5156 done:
5157 if (err && *new_te) {
5158 free(*new_te);
5159 *new_te = NULL;
5161 return err;
5164 static const struct got_error *
5165 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5166 struct got_commitable *ct)
5168 const struct got_error *err = NULL;
5169 char *ct_name = NULL;
5171 *new_te = NULL;
5173 *new_te = calloc(1, sizeof(**new_te));
5174 if (*new_te == NULL)
5175 return got_error_from_errno("calloc");
5177 err = got_path_basename(&ct_name, ct->path);
5178 if (err)
5179 goto done;
5180 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5181 sizeof((*new_te)->name)) {
5182 err = got_error(GOT_ERR_NO_SPACE);
5183 goto done;
5186 (*new_te)->mode = get_ct_file_mode(ct);
5188 if (ct->staged_status == GOT_STATUS_ADD)
5189 memcpy(&(*new_te)->id, ct->staged_blob_id,
5190 sizeof((*new_te)->id));
5191 else
5192 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5193 done:
5194 free(ct_name);
5195 if (err && *new_te) {
5196 free(*new_te);
5197 *new_te = NULL;
5199 return err;
5202 static const struct got_error *
5203 insert_tree_entry(struct got_tree_entry *new_te,
5204 struct got_pathlist_head *paths)
5206 const struct got_error *err = NULL;
5207 struct got_pathlist_entry *new_pe;
5209 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5210 if (err)
5211 return err;
5212 if (new_pe == NULL)
5213 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5214 return NULL;
5217 static const struct got_error *
5218 report_ct_status(struct got_commitable *ct,
5219 got_worktree_status_cb status_cb, void *status_arg)
5221 const char *ct_path = ct->path;
5222 unsigned char status;
5224 while (ct_path[0] == '/')
5225 ct_path++;
5227 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5228 status = ct->staged_status;
5229 else
5230 status = ct->status;
5232 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5233 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5236 static const struct got_error *
5237 match_modified_subtree(int *modified, struct got_tree_entry *te,
5238 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5240 const struct got_error *err = NULL;
5241 struct got_pathlist_entry *pe;
5242 char *te_path;
5244 *modified = 0;
5246 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5247 got_path_is_root_dir(base_tree_path) ? "" : "/",
5248 te->name) == -1)
5249 return got_error_from_errno("asprintf");
5251 TAILQ_FOREACH(pe, commitable_paths, entry) {
5252 struct got_commitable *ct = pe->data;
5253 *modified = got_path_is_child(ct->in_repo_path, te_path,
5254 strlen(te_path));
5255 if (*modified)
5256 break;
5259 free(te_path);
5260 return err;
5263 static const struct got_error *
5264 match_deleted_or_modified_ct(struct got_commitable **ctp,
5265 struct got_tree_entry *te, const char *base_tree_path,
5266 struct got_pathlist_head *commitable_paths)
5268 const struct got_error *err = NULL;
5269 struct got_pathlist_entry *pe;
5271 *ctp = NULL;
5273 TAILQ_FOREACH(pe, commitable_paths, entry) {
5274 struct got_commitable *ct = pe->data;
5275 char *ct_name = NULL;
5276 int path_matches;
5278 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5279 if (ct->status != GOT_STATUS_MODIFY &&
5280 ct->status != GOT_STATUS_MODE_CHANGE &&
5281 ct->status != GOT_STATUS_DELETE)
5282 continue;
5283 } else {
5284 if (ct->staged_status != GOT_STATUS_MODIFY &&
5285 ct->staged_status != GOT_STATUS_DELETE)
5286 continue;
5289 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5290 continue;
5292 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5293 if (err)
5294 return err;
5295 if (!path_matches)
5296 continue;
5298 err = got_path_basename(&ct_name, pe->path);
5299 if (err)
5300 return err;
5302 if (strcmp(te->name, ct_name) != 0) {
5303 free(ct_name);
5304 continue;
5306 free(ct_name);
5308 *ctp = ct;
5309 break;
5312 return err;
5315 static const struct got_error *
5316 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5317 const char *child_path, const char *path_base_tree,
5318 struct got_pathlist_head *commitable_paths,
5319 got_worktree_status_cb status_cb, void *status_arg,
5320 struct got_repository *repo)
5322 const struct got_error *err = NULL;
5323 struct got_tree_entry *new_te;
5324 char *subtree_path;
5325 struct got_object_id *id = NULL;
5326 int nentries;
5328 *new_tep = NULL;
5330 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5331 got_path_is_root_dir(path_base_tree) ? "" : "/",
5332 child_path) == -1)
5333 return got_error_from_errno("asprintf");
5335 new_te = calloc(1, sizeof(*new_te));
5336 if (new_te == NULL)
5337 return got_error_from_errno("calloc");
5338 new_te->mode = S_IFDIR;
5340 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5341 sizeof(new_te->name)) {
5342 err = got_error(GOT_ERR_NO_SPACE);
5343 goto done;
5345 err = write_tree(&id, &nentries, NULL, subtree_path,
5346 commitable_paths, status_cb, status_arg, repo);
5347 if (err) {
5348 free(new_te);
5349 goto done;
5351 memcpy(&new_te->id, id, sizeof(new_te->id));
5352 done:
5353 free(id);
5354 free(subtree_path);
5355 if (err == NULL)
5356 *new_tep = new_te;
5357 return err;
5360 static const struct got_error *
5361 write_tree(struct got_object_id **new_tree_id, int *nentries,
5362 struct got_tree_object *base_tree, const char *path_base_tree,
5363 struct got_pathlist_head *commitable_paths,
5364 got_worktree_status_cb status_cb, void *status_arg,
5365 struct got_repository *repo)
5367 const struct got_error *err = NULL;
5368 struct got_pathlist_head paths;
5369 struct got_tree_entry *te, *new_te = NULL;
5370 struct got_pathlist_entry *pe;
5372 TAILQ_INIT(&paths);
5373 *nentries = 0;
5375 /* Insert, and recurse into, newly added entries first. */
5376 TAILQ_FOREACH(pe, commitable_paths, entry) {
5377 struct got_commitable *ct = pe->data;
5378 char *child_path = NULL, *slash;
5380 if ((ct->status != GOT_STATUS_ADD &&
5381 ct->staged_status != GOT_STATUS_ADD) ||
5382 (ct->flags & GOT_COMMITABLE_ADDED))
5383 continue;
5385 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5386 strlen(path_base_tree)))
5387 continue;
5389 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5390 ct->in_repo_path);
5391 if (err)
5392 goto done;
5394 slash = strchr(child_path, '/');
5395 if (slash == NULL) {
5396 err = alloc_added_blob_tree_entry(&new_te, ct);
5397 if (err)
5398 goto done;
5399 err = report_ct_status(ct, status_cb, status_arg);
5400 if (err)
5401 goto done;
5402 ct->flags |= GOT_COMMITABLE_ADDED;
5403 err = insert_tree_entry(new_te, &paths);
5404 if (err)
5405 goto done;
5406 (*nentries)++;
5407 } else {
5408 *slash = '\0'; /* trim trailing path components */
5409 if (base_tree == NULL ||
5410 got_object_tree_find_entry(base_tree, child_path)
5411 == NULL) {
5412 err = make_subtree_for_added_blob(&new_te,
5413 child_path, path_base_tree,
5414 commitable_paths, status_cb, status_arg,
5415 repo);
5416 if (err)
5417 goto done;
5418 err = insert_tree_entry(new_te, &paths);
5419 if (err)
5420 goto done;
5421 (*nentries)++;
5426 if (base_tree) {
5427 int i, nbase_entries;
5428 /* Handle modified and deleted entries. */
5429 nbase_entries = got_object_tree_get_nentries(base_tree);
5430 for (i = 0; i < nbase_entries; i++) {
5431 struct got_commitable *ct = NULL;
5433 te = got_object_tree_get_entry(base_tree, i);
5434 if (got_object_tree_entry_is_submodule(te)) {
5435 /* Entry is a submodule; just copy it. */
5436 err = got_object_tree_entry_dup(&new_te, te);
5437 if (err)
5438 goto done;
5439 err = insert_tree_entry(new_te, &paths);
5440 if (err)
5441 goto done;
5442 (*nentries)++;
5443 continue;
5446 if (S_ISDIR(te->mode)) {
5447 int modified;
5448 err = got_object_tree_entry_dup(&new_te, te);
5449 if (err)
5450 goto done;
5451 err = match_modified_subtree(&modified, te,
5452 path_base_tree, commitable_paths);
5453 if (err)
5454 goto done;
5455 /* Avoid recursion into unmodified subtrees. */
5456 if (modified) {
5457 struct got_object_id *new_id;
5458 int nsubentries;
5459 err = write_subtree(&new_id,
5460 &nsubentries, te,
5461 path_base_tree, commitable_paths,
5462 status_cb, status_arg, repo);
5463 if (err)
5464 goto done;
5465 if (nsubentries == 0) {
5466 /* All entries were deleted. */
5467 free(new_id);
5468 continue;
5470 memcpy(&new_te->id, new_id,
5471 sizeof(new_te->id));
5472 free(new_id);
5474 err = insert_tree_entry(new_te, &paths);
5475 if (err)
5476 goto done;
5477 (*nentries)++;
5478 continue;
5481 err = match_deleted_or_modified_ct(&ct, te,
5482 path_base_tree, commitable_paths);
5483 if (err)
5484 goto done;
5485 if (ct) {
5486 /* NB: Deleted entries get dropped here. */
5487 if (ct->status == GOT_STATUS_MODIFY ||
5488 ct->status == GOT_STATUS_MODE_CHANGE ||
5489 ct->staged_status == GOT_STATUS_MODIFY) {
5490 err = alloc_modified_blob_tree_entry(
5491 &new_te, te, ct);
5492 if (err)
5493 goto done;
5494 err = insert_tree_entry(new_te, &paths);
5495 if (err)
5496 goto done;
5497 (*nentries)++;
5499 err = report_ct_status(ct, status_cb,
5500 status_arg);
5501 if (err)
5502 goto done;
5503 } else {
5504 /* Entry is unchanged; just copy it. */
5505 err = got_object_tree_entry_dup(&new_te, te);
5506 if (err)
5507 goto done;
5508 err = insert_tree_entry(new_te, &paths);
5509 if (err)
5510 goto done;
5511 (*nentries)++;
5516 /* Write new list of entries; deleted entries have been dropped. */
5517 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5518 done:
5519 got_pathlist_free(&paths);
5520 return err;
5523 static const struct got_error *
5524 update_fileindex_after_commit(struct got_worktree *worktree,
5525 struct got_pathlist_head *commitable_paths,
5526 struct got_object_id *new_base_commit_id,
5527 struct got_fileindex *fileindex, int have_staged_files)
5529 const struct got_error *err = NULL;
5530 struct got_pathlist_entry *pe;
5531 char *relpath = NULL;
5533 TAILQ_FOREACH(pe, commitable_paths, entry) {
5534 struct got_fileindex_entry *ie;
5535 struct got_commitable *ct = pe->data;
5537 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5539 err = got_path_skip_common_ancestor(&relpath,
5540 worktree->root_path, ct->ondisk_path);
5541 if (err)
5542 goto done;
5544 if (ie) {
5545 if (ct->status == GOT_STATUS_DELETE ||
5546 ct->staged_status == GOT_STATUS_DELETE) {
5547 got_fileindex_entry_remove(fileindex, ie);
5548 } else if (ct->staged_status == GOT_STATUS_ADD ||
5549 ct->staged_status == GOT_STATUS_MODIFY) {
5550 got_fileindex_entry_stage_set(ie,
5551 GOT_FILEIDX_STAGE_NONE);
5552 got_fileindex_entry_staged_filetype_set(ie, 0);
5554 err = got_fileindex_entry_update(ie,
5555 worktree->root_fd, relpath,
5556 ct->staged_blob_id->sha1,
5557 new_base_commit_id->sha1,
5558 !have_staged_files);
5559 } else
5560 err = got_fileindex_entry_update(ie,
5561 worktree->root_fd, relpath,
5562 ct->blob_id->sha1,
5563 new_base_commit_id->sha1,
5564 !have_staged_files);
5565 } else {
5566 err = got_fileindex_entry_alloc(&ie, pe->path);
5567 if (err)
5568 goto done;
5569 err = got_fileindex_entry_update(ie,
5570 worktree->root_fd, relpath, ct->blob_id->sha1,
5571 new_base_commit_id->sha1, 1);
5572 if (err) {
5573 got_fileindex_entry_free(ie);
5574 goto done;
5576 err = got_fileindex_entry_add(fileindex, ie);
5577 if (err) {
5578 got_fileindex_entry_free(ie);
5579 goto done;
5582 free(relpath);
5583 relpath = NULL;
5585 done:
5586 free(relpath);
5587 return err;
5591 static const struct got_error *
5592 check_out_of_date(const char *in_repo_path, unsigned char status,
5593 unsigned char staged_status, struct got_object_id *base_blob_id,
5594 struct got_object_id *base_commit_id,
5595 struct got_object_id *head_commit_id, struct got_repository *repo,
5596 int ood_errcode)
5598 const struct got_error *err = NULL;
5599 struct got_object_id *id = NULL;
5601 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5602 /* Trivial case: base commit == head commit */
5603 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5604 return NULL;
5606 * Ensure file content which local changes were based
5607 * on matches file content in the branch head.
5609 err = got_object_id_by_path(&id, repo, head_commit_id,
5610 in_repo_path);
5611 if (err) {
5612 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5613 err = got_error(ood_errcode);
5614 goto done;
5615 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5616 err = got_error(ood_errcode);
5617 } else {
5618 /* Require that added files don't exist in the branch head. */
5619 err = got_object_id_by_path(&id, repo, head_commit_id,
5620 in_repo_path);
5621 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5622 goto done;
5623 err = id ? got_error(ood_errcode) : NULL;
5625 done:
5626 free(id);
5627 return err;
5630 const struct got_error *
5631 commit_worktree(struct got_object_id **new_commit_id,
5632 struct got_pathlist_head *commitable_paths,
5633 struct got_object_id *head_commit_id,
5634 struct got_object_id *parent_id2,
5635 struct got_worktree *worktree,
5636 const char *author, const char *committer,
5637 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5638 got_worktree_status_cb status_cb, void *status_arg,
5639 struct got_repository *repo)
5641 const struct got_error *err = NULL, *unlockerr = NULL;
5642 struct got_pathlist_entry *pe;
5643 const char *head_ref_name = NULL;
5644 struct got_commit_object *head_commit = NULL;
5645 struct got_reference *head_ref2 = NULL;
5646 struct got_object_id *head_commit_id2 = NULL;
5647 struct got_tree_object *head_tree = NULL;
5648 struct got_object_id *new_tree_id = NULL;
5649 int nentries, nparents = 0;
5650 struct got_object_id_queue parent_ids;
5651 struct got_object_qid *pid = NULL;
5652 char *logmsg = NULL;
5654 *new_commit_id = NULL;
5656 STAILQ_INIT(&parent_ids);
5658 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5659 if (err)
5660 goto done;
5662 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5663 if (err)
5664 goto done;
5666 if (commit_msg_cb != NULL) {
5667 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5668 if (err)
5669 goto done;
5672 if (logmsg == NULL || strlen(logmsg) == 0) {
5673 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5674 goto done;
5677 /* Create blobs from added and modified files and record their IDs. */
5678 TAILQ_FOREACH(pe, commitable_paths, entry) {
5679 struct got_commitable *ct = pe->data;
5680 char *ondisk_path;
5682 /* Blobs for staged files already exist. */
5683 if (ct->staged_status == GOT_STATUS_ADD ||
5684 ct->staged_status == GOT_STATUS_MODIFY)
5685 continue;
5687 if (ct->status != GOT_STATUS_ADD &&
5688 ct->status != GOT_STATUS_MODIFY &&
5689 ct->status != GOT_STATUS_MODE_CHANGE)
5690 continue;
5692 if (asprintf(&ondisk_path, "%s/%s",
5693 worktree->root_path, pe->path) == -1) {
5694 err = got_error_from_errno("asprintf");
5695 goto done;
5697 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5698 free(ondisk_path);
5699 if (err)
5700 goto done;
5703 /* Recursively write new tree objects. */
5704 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5705 commitable_paths, status_cb, status_arg, repo);
5706 if (err)
5707 goto done;
5709 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5710 if (err)
5711 goto done;
5712 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5713 nparents++;
5714 if (parent_id2) {
5715 err = got_object_qid_alloc(&pid, parent_id2);
5716 if (err)
5717 goto done;
5718 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5719 nparents++;
5721 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5722 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5723 if (logmsg != NULL)
5724 free(logmsg);
5725 if (err)
5726 goto done;
5728 /* Check if a concurrent commit to our branch has occurred. */
5729 head_ref_name = got_worktree_get_head_ref_name(worktree);
5730 if (head_ref_name == NULL) {
5731 err = got_error_from_errno("got_worktree_get_head_ref_name");
5732 goto done;
5734 /* Lock the reference here to prevent concurrent modification. */
5735 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5736 if (err)
5737 goto done;
5738 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5739 if (err)
5740 goto done;
5741 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5742 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5743 goto done;
5745 /* Update branch head in repository. */
5746 err = got_ref_change_ref(head_ref2, *new_commit_id);
5747 if (err)
5748 goto done;
5749 err = got_ref_write(head_ref2, repo);
5750 if (err)
5751 goto done;
5753 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5754 if (err)
5755 goto done;
5757 err = ref_base_commit(worktree, repo);
5758 if (err)
5759 goto done;
5760 done:
5761 got_object_id_queue_free(&parent_ids);
5762 if (head_tree)
5763 got_object_tree_close(head_tree);
5764 if (head_commit)
5765 got_object_commit_close(head_commit);
5766 free(head_commit_id2);
5767 if (head_ref2) {
5768 unlockerr = got_ref_unlock(head_ref2);
5769 if (unlockerr && err == NULL)
5770 err = unlockerr;
5771 got_ref_close(head_ref2);
5773 return err;
5776 static const struct got_error *
5777 check_path_is_commitable(const char *path,
5778 struct got_pathlist_head *commitable_paths)
5780 struct got_pathlist_entry *cpe = NULL;
5781 size_t path_len = strlen(path);
5783 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5784 struct got_commitable *ct = cpe->data;
5785 const char *ct_path = ct->path;
5787 while (ct_path[0] == '/')
5788 ct_path++;
5790 if (strcmp(path, ct_path) == 0 ||
5791 got_path_is_child(ct_path, path, path_len))
5792 break;
5795 if (cpe == NULL)
5796 return got_error_path(path, GOT_ERR_BAD_PATH);
5798 return NULL;
5801 static const struct got_error *
5802 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5804 int *have_staged_files = arg;
5806 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5807 *have_staged_files = 1;
5808 return got_error(GOT_ERR_CANCELLED);
5811 return NULL;
5814 static const struct got_error *
5815 check_non_staged_files(struct got_fileindex *fileindex,
5816 struct got_pathlist_head *paths)
5818 struct got_pathlist_entry *pe;
5819 struct got_fileindex_entry *ie;
5821 TAILQ_FOREACH(pe, paths, entry) {
5822 if (pe->path[0] == '\0')
5823 continue;
5824 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5825 if (ie == NULL)
5826 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5827 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5828 return got_error_path(pe->path,
5829 GOT_ERR_FILE_NOT_STAGED);
5832 return NULL;
5835 const struct got_error *
5836 got_worktree_commit(struct got_object_id **new_commit_id,
5837 struct got_worktree *worktree, struct got_pathlist_head *paths,
5838 const char *author, const char *committer, int allow_bad_symlinks,
5839 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5840 got_worktree_status_cb status_cb, void *status_arg,
5841 struct got_repository *repo)
5843 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5844 struct got_fileindex *fileindex = NULL;
5845 char *fileindex_path = NULL;
5846 struct got_pathlist_head commitable_paths;
5847 struct collect_commitables_arg cc_arg;
5848 struct got_pathlist_entry *pe;
5849 struct got_reference *head_ref = NULL;
5850 struct got_object_id *head_commit_id = NULL;
5851 int have_staged_files = 0;
5853 *new_commit_id = NULL;
5855 TAILQ_INIT(&commitable_paths);
5857 err = lock_worktree(worktree, LOCK_EX);
5858 if (err)
5859 goto done;
5861 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5862 if (err)
5863 goto done;
5865 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5866 if (err)
5867 goto done;
5869 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5870 if (err)
5871 goto done;
5873 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5874 &have_staged_files);
5875 if (err && err->code != GOT_ERR_CANCELLED)
5876 goto done;
5877 if (have_staged_files) {
5878 err = check_non_staged_files(fileindex, paths);
5879 if (err)
5880 goto done;
5883 cc_arg.commitable_paths = &commitable_paths;
5884 cc_arg.worktree = worktree;
5885 cc_arg.fileindex = fileindex;
5886 cc_arg.repo = repo;
5887 cc_arg.have_staged_files = have_staged_files;
5888 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5889 TAILQ_FOREACH(pe, paths, entry) {
5890 err = worktree_status(worktree, pe->path, fileindex, repo,
5891 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5892 if (err)
5893 goto done;
5896 if (TAILQ_EMPTY(&commitable_paths)) {
5897 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5898 goto done;
5901 TAILQ_FOREACH(pe, paths, entry) {
5902 err = check_path_is_commitable(pe->path, &commitable_paths);
5903 if (err)
5904 goto done;
5907 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5908 struct got_commitable *ct = pe->data;
5909 const char *ct_path = ct->in_repo_path;
5911 while (ct_path[0] == '/')
5912 ct_path++;
5913 err = check_out_of_date(ct_path, ct->status,
5914 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5915 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5916 if (err)
5917 goto done;
5921 err = commit_worktree(new_commit_id, &commitable_paths,
5922 head_commit_id, NULL, worktree, author, committer,
5923 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5924 if (err)
5925 goto done;
5927 err = update_fileindex_after_commit(worktree, &commitable_paths,
5928 *new_commit_id, fileindex, have_staged_files);
5929 sync_err = sync_fileindex(fileindex, fileindex_path);
5930 if (sync_err && err == NULL)
5931 err = sync_err;
5932 done:
5933 if (fileindex)
5934 got_fileindex_free(fileindex);
5935 free(fileindex_path);
5936 unlockerr = lock_worktree(worktree, LOCK_SH);
5937 if (unlockerr && err == NULL)
5938 err = unlockerr;
5939 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5940 struct got_commitable *ct = pe->data;
5941 free_commitable(ct);
5943 got_pathlist_free(&commitable_paths);
5944 return err;
5947 const char *
5948 got_commitable_get_path(struct got_commitable *ct)
5950 return ct->path;
5953 unsigned int
5954 got_commitable_get_status(struct got_commitable *ct)
5956 return ct->status;
5959 struct check_rebase_ok_arg {
5960 struct got_worktree *worktree;
5961 struct got_repository *repo;
5964 static const struct got_error *
5965 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5967 const struct got_error *err = NULL;
5968 struct check_rebase_ok_arg *a = arg;
5969 unsigned char status;
5970 struct stat sb;
5971 char *ondisk_path;
5973 /* Reject rebase of a work tree with mixed base commits. */
5974 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5975 SHA1_DIGEST_LENGTH))
5976 return got_error(GOT_ERR_MIXED_COMMITS);
5978 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5979 == -1)
5980 return got_error_from_errno("asprintf");
5982 /* Reject rebase of a work tree with modified or staged files. */
5983 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5984 free(ondisk_path);
5985 if (err)
5986 return err;
5988 if (status != GOT_STATUS_NO_CHANGE)
5989 return got_error(GOT_ERR_MODIFIED);
5990 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5991 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5993 return NULL;
5996 const struct got_error *
5997 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5998 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5999 struct got_worktree *worktree, struct got_reference *branch,
6000 struct got_repository *repo)
6002 const struct got_error *err = NULL;
6003 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6004 char *branch_ref_name = NULL;
6005 char *fileindex_path = NULL;
6006 struct check_rebase_ok_arg ok_arg;
6007 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6008 struct got_object_id *wt_branch_tip = NULL;
6010 *new_base_branch_ref = NULL;
6011 *tmp_branch = NULL;
6012 *fileindex = NULL;
6014 err = lock_worktree(worktree, LOCK_EX);
6015 if (err)
6016 return err;
6018 err = open_fileindex(fileindex, &fileindex_path, worktree);
6019 if (err)
6020 goto done;
6022 ok_arg.worktree = worktree;
6023 ok_arg.repo = repo;
6024 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6025 &ok_arg);
6026 if (err)
6027 goto done;
6029 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6030 if (err)
6031 goto done;
6033 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6034 if (err)
6035 goto done;
6037 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6038 if (err)
6039 goto done;
6041 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6042 0);
6043 if (err)
6044 goto done;
6046 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6047 if (err)
6048 goto done;
6049 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6050 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6051 goto done;
6054 err = got_ref_alloc_symref(new_base_branch_ref,
6055 new_base_branch_ref_name, wt_branch);
6056 if (err)
6057 goto done;
6058 err = got_ref_write(*new_base_branch_ref, repo);
6059 if (err)
6060 goto done;
6062 /* TODO Lock original branch's ref while rebasing? */
6064 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6065 if (err)
6066 goto done;
6068 err = got_ref_write(branch_ref, repo);
6069 if (err)
6070 goto done;
6072 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6073 worktree->base_commit_id);
6074 if (err)
6075 goto done;
6076 err = got_ref_write(*tmp_branch, repo);
6077 if (err)
6078 goto done;
6080 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6081 if (err)
6082 goto done;
6083 done:
6084 free(fileindex_path);
6085 free(tmp_branch_name);
6086 free(new_base_branch_ref_name);
6087 free(branch_ref_name);
6088 if (branch_ref)
6089 got_ref_close(branch_ref);
6090 if (wt_branch)
6091 got_ref_close(wt_branch);
6092 free(wt_branch_tip);
6093 if (err) {
6094 if (*new_base_branch_ref) {
6095 got_ref_close(*new_base_branch_ref);
6096 *new_base_branch_ref = NULL;
6098 if (*tmp_branch) {
6099 got_ref_close(*tmp_branch);
6100 *tmp_branch = NULL;
6102 if (*fileindex) {
6103 got_fileindex_free(*fileindex);
6104 *fileindex = NULL;
6106 lock_worktree(worktree, LOCK_SH);
6108 return err;
6111 const struct got_error *
6112 got_worktree_rebase_continue(struct got_object_id **commit_id,
6113 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6114 struct got_reference **branch, struct got_fileindex **fileindex,
6115 struct got_worktree *worktree, struct got_repository *repo)
6117 const struct got_error *err;
6118 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6119 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6120 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6121 char *fileindex_path = NULL;
6122 int have_staged_files = 0;
6124 *commit_id = NULL;
6125 *new_base_branch = NULL;
6126 *tmp_branch = NULL;
6127 *branch = NULL;
6128 *fileindex = NULL;
6130 err = lock_worktree(worktree, LOCK_EX);
6131 if (err)
6132 return err;
6134 err = open_fileindex(fileindex, &fileindex_path, worktree);
6135 if (err)
6136 goto done;
6138 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6139 &have_staged_files);
6140 if (err && err->code != GOT_ERR_CANCELLED)
6141 goto done;
6142 if (have_staged_files) {
6143 err = got_error(GOT_ERR_STAGED_PATHS);
6144 goto done;
6147 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6148 if (err)
6149 goto done;
6151 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6152 if (err)
6153 goto done;
6155 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6156 if (err)
6157 goto done;
6159 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6160 if (err)
6161 goto done;
6163 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6164 if (err)
6165 goto done;
6167 err = got_ref_open(branch, repo,
6168 got_ref_get_symref_target(branch_ref), 0);
6169 if (err)
6170 goto done;
6172 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6173 if (err)
6174 goto done;
6176 err = got_ref_resolve(commit_id, repo, commit_ref);
6177 if (err)
6178 goto done;
6180 err = got_ref_open(new_base_branch, repo,
6181 new_base_branch_ref_name, 0);
6182 if (err)
6183 goto done;
6185 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6186 if (err)
6187 goto done;
6188 done:
6189 free(commit_ref_name);
6190 free(branch_ref_name);
6191 free(fileindex_path);
6192 if (commit_ref)
6193 got_ref_close(commit_ref);
6194 if (branch_ref)
6195 got_ref_close(branch_ref);
6196 if (err) {
6197 free(*commit_id);
6198 *commit_id = NULL;
6199 if (*tmp_branch) {
6200 got_ref_close(*tmp_branch);
6201 *tmp_branch = NULL;
6203 if (*new_base_branch) {
6204 got_ref_close(*new_base_branch);
6205 *new_base_branch = NULL;
6207 if (*branch) {
6208 got_ref_close(*branch);
6209 *branch = NULL;
6211 if (*fileindex) {
6212 got_fileindex_free(*fileindex);
6213 *fileindex = NULL;
6215 lock_worktree(worktree, LOCK_SH);
6217 return err;
6220 const struct got_error *
6221 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6223 const struct got_error *err;
6224 char *tmp_branch_name = NULL;
6226 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6227 if (err)
6228 return err;
6230 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6231 free(tmp_branch_name);
6232 return NULL;
6235 static const struct got_error *
6236 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6237 char **logmsg, void *arg)
6239 *logmsg = arg;
6240 return NULL;
6243 static const struct got_error *
6244 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6245 const char *path, struct got_object_id *blob_id,
6246 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6247 int dirfd, const char *de_name)
6249 return NULL;
6252 struct collect_merged_paths_arg {
6253 got_worktree_checkout_cb progress_cb;
6254 void *progress_arg;
6255 struct got_pathlist_head *merged_paths;
6258 static const struct got_error *
6259 collect_merged_paths(void *arg, unsigned char status, const char *path)
6261 const struct got_error *err;
6262 struct collect_merged_paths_arg *a = arg;
6263 char *p;
6264 struct got_pathlist_entry *new;
6266 err = (*a->progress_cb)(a->progress_arg, status, path);
6267 if (err)
6268 return err;
6270 if (status != GOT_STATUS_MERGE &&
6271 status != GOT_STATUS_ADD &&
6272 status != GOT_STATUS_DELETE &&
6273 status != GOT_STATUS_CONFLICT)
6274 return NULL;
6276 p = strdup(path);
6277 if (p == NULL)
6278 return got_error_from_errno("strdup");
6280 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6281 if (err || new == NULL)
6282 free(p);
6283 return err;
6286 void
6287 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6289 struct got_pathlist_entry *pe;
6291 TAILQ_FOREACH(pe, merged_paths, entry)
6292 free((char *)pe->path);
6294 got_pathlist_free(merged_paths);
6297 static const struct got_error *
6298 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6299 int is_rebase, struct got_repository *repo)
6301 const struct got_error *err;
6302 struct got_reference *commit_ref = NULL;
6304 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6305 if (err) {
6306 if (err->code != GOT_ERR_NOT_REF)
6307 goto done;
6308 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6309 if (err)
6310 goto done;
6311 err = got_ref_write(commit_ref, repo);
6312 if (err)
6313 goto done;
6314 } else if (is_rebase) {
6315 struct got_object_id *stored_id;
6316 int cmp;
6318 err = got_ref_resolve(&stored_id, repo, commit_ref);
6319 if (err)
6320 goto done;
6321 cmp = got_object_id_cmp(commit_id, stored_id);
6322 free(stored_id);
6323 if (cmp != 0) {
6324 err = got_error(GOT_ERR_REBASE_COMMITID);
6325 goto done;
6328 done:
6329 if (commit_ref)
6330 got_ref_close(commit_ref);
6331 return err;
6334 static const struct got_error *
6335 rebase_merge_files(struct got_pathlist_head *merged_paths,
6336 const char *commit_ref_name, struct got_worktree *worktree,
6337 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6338 struct got_object_id *commit_id, struct got_repository *repo,
6339 got_worktree_checkout_cb progress_cb, void *progress_arg,
6340 got_cancel_cb cancel_cb, void *cancel_arg)
6342 const struct got_error *err;
6343 struct got_reference *commit_ref = NULL;
6344 struct collect_merged_paths_arg cmp_arg;
6345 char *fileindex_path;
6347 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6349 err = get_fileindex_path(&fileindex_path, worktree);
6350 if (err)
6351 return err;
6353 cmp_arg.progress_cb = progress_cb;
6354 cmp_arg.progress_arg = progress_arg;
6355 cmp_arg.merged_paths = merged_paths;
6356 err = merge_files(worktree, fileindex, fileindex_path,
6357 parent_commit_id, commit_id, repo, collect_merged_paths,
6358 &cmp_arg, cancel_cb, cancel_arg);
6359 if (commit_ref)
6360 got_ref_close(commit_ref);
6361 return err;
6364 const struct got_error *
6365 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6366 struct got_worktree *worktree, struct got_fileindex *fileindex,
6367 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6368 struct got_repository *repo,
6369 got_worktree_checkout_cb progress_cb, void *progress_arg,
6370 got_cancel_cb cancel_cb, void *cancel_arg)
6372 const struct got_error *err;
6373 char *commit_ref_name;
6375 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6376 if (err)
6377 return err;
6379 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6380 if (err)
6381 goto done;
6383 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6384 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6385 progress_arg, cancel_cb, cancel_arg);
6386 done:
6387 free(commit_ref_name);
6388 return err;
6391 const struct got_error *
6392 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6393 struct got_worktree *worktree, struct got_fileindex *fileindex,
6394 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6395 struct got_repository *repo,
6396 got_worktree_checkout_cb progress_cb, void *progress_arg,
6397 got_cancel_cb cancel_cb, void *cancel_arg)
6399 const struct got_error *err;
6400 char *commit_ref_name;
6402 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6403 if (err)
6404 return err;
6406 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6407 if (err)
6408 goto done;
6410 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6411 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6412 progress_arg, cancel_cb, cancel_arg);
6413 done:
6414 free(commit_ref_name);
6415 return err;
6418 static const struct got_error *
6419 rebase_commit(struct got_object_id **new_commit_id,
6420 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6421 struct got_worktree *worktree, struct got_fileindex *fileindex,
6422 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6423 const char *new_logmsg, struct got_repository *repo)
6425 const struct got_error *err, *sync_err;
6426 struct got_pathlist_head commitable_paths;
6427 struct collect_commitables_arg cc_arg;
6428 char *fileindex_path = NULL;
6429 struct got_reference *head_ref = NULL;
6430 struct got_object_id *head_commit_id = NULL;
6431 char *logmsg = NULL;
6433 TAILQ_INIT(&commitable_paths);
6434 *new_commit_id = NULL;
6436 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6438 err = get_fileindex_path(&fileindex_path, worktree);
6439 if (err)
6440 return err;
6442 cc_arg.commitable_paths = &commitable_paths;
6443 cc_arg.worktree = worktree;
6444 cc_arg.repo = repo;
6445 cc_arg.have_staged_files = 0;
6447 * If possible get the status of individual files directly to
6448 * avoid crawling the entire work tree once per rebased commit.
6450 * Ideally, merged_paths would contain a list of commitables
6451 * we could use so we could skip worktree_status() entirely.
6452 * However, we would then need carefully keep track of cumulative
6453 * effects of operations such as file additions and deletions
6454 * in 'got histedit -f' (folding multiple commits into one),
6455 * and this extra complexity is not really worth it.
6457 if (merged_paths) {
6458 struct got_pathlist_entry *pe;
6459 TAILQ_FOREACH(pe, merged_paths, entry) {
6460 err = worktree_status(worktree, pe->path, fileindex,
6461 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6462 0);
6463 if (err)
6464 goto done;
6466 } else {
6467 err = worktree_status(worktree, "", fileindex, repo,
6468 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6469 if (err)
6470 goto done;
6473 if (TAILQ_EMPTY(&commitable_paths)) {
6474 /* No-op change; commit will be elided. */
6475 err = got_ref_delete(commit_ref, repo);
6476 if (err)
6477 goto done;
6478 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6479 goto done;
6482 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6483 if (err)
6484 goto done;
6486 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6487 if (err)
6488 goto done;
6490 if (new_logmsg) {
6491 logmsg = strdup(new_logmsg);
6492 if (logmsg == NULL) {
6493 err = got_error_from_errno("strdup");
6494 goto done;
6496 } else {
6497 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6498 if (err)
6499 goto done;
6502 /* NB: commit_worktree will call free(logmsg) */
6503 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6504 NULL, worktree, got_object_commit_get_author(orig_commit),
6505 got_object_commit_get_committer(orig_commit),
6506 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6507 if (err)
6508 goto done;
6510 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6511 if (err)
6512 goto done;
6514 err = got_ref_delete(commit_ref, repo);
6515 if (err)
6516 goto done;
6518 err = update_fileindex_after_commit(worktree, &commitable_paths,
6519 *new_commit_id, fileindex, 0);
6520 sync_err = sync_fileindex(fileindex, fileindex_path);
6521 if (sync_err && err == NULL)
6522 err = sync_err;
6523 done:
6524 free(fileindex_path);
6525 free(head_commit_id);
6526 if (head_ref)
6527 got_ref_close(head_ref);
6528 if (err) {
6529 free(*new_commit_id);
6530 *new_commit_id = NULL;
6532 return err;
6535 const struct got_error *
6536 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6537 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6538 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6539 struct got_commit_object *orig_commit,
6540 struct got_object_id *orig_commit_id, struct got_repository *repo)
6542 const struct got_error *err;
6543 char *commit_ref_name;
6544 struct got_reference *commit_ref = NULL;
6545 struct got_object_id *commit_id = NULL;
6547 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6548 if (err)
6549 return err;
6551 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6552 if (err)
6553 goto done;
6554 err = got_ref_resolve(&commit_id, repo, commit_ref);
6555 if (err)
6556 goto done;
6557 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6558 err = got_error(GOT_ERR_REBASE_COMMITID);
6559 goto done;
6562 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6563 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6564 done:
6565 if (commit_ref)
6566 got_ref_close(commit_ref);
6567 free(commit_ref_name);
6568 free(commit_id);
6569 return err;
6572 const struct got_error *
6573 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6574 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6575 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6576 struct got_commit_object *orig_commit,
6577 struct got_object_id *orig_commit_id, const char *new_logmsg,
6578 struct got_repository *repo)
6580 const struct got_error *err;
6581 char *commit_ref_name;
6582 struct got_reference *commit_ref = NULL;
6584 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6585 if (err)
6586 return err;
6588 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6589 if (err)
6590 goto done;
6592 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6593 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6594 done:
6595 if (commit_ref)
6596 got_ref_close(commit_ref);
6597 free(commit_ref_name);
6598 return err;
6601 const struct got_error *
6602 got_worktree_rebase_postpone(struct got_worktree *worktree,
6603 struct got_fileindex *fileindex)
6605 if (fileindex)
6606 got_fileindex_free(fileindex);
6607 return lock_worktree(worktree, LOCK_SH);
6610 static const struct got_error *
6611 delete_ref(const char *name, struct got_repository *repo)
6613 const struct got_error *err;
6614 struct got_reference *ref;
6616 err = got_ref_open(&ref, repo, name, 0);
6617 if (err) {
6618 if (err->code == GOT_ERR_NOT_REF)
6619 return NULL;
6620 return err;
6623 err = got_ref_delete(ref, repo);
6624 got_ref_close(ref);
6625 return err;
6628 static const struct got_error *
6629 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6631 const struct got_error *err;
6632 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6633 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6635 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6636 if (err)
6637 goto done;
6638 err = delete_ref(tmp_branch_name, repo);
6639 if (err)
6640 goto done;
6642 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6643 if (err)
6644 goto done;
6645 err = delete_ref(new_base_branch_ref_name, repo);
6646 if (err)
6647 goto done;
6649 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6650 if (err)
6651 goto done;
6652 err = delete_ref(branch_ref_name, repo);
6653 if (err)
6654 goto done;
6656 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6657 if (err)
6658 goto done;
6659 err = delete_ref(commit_ref_name, repo);
6660 if (err)
6661 goto done;
6663 done:
6664 free(tmp_branch_name);
6665 free(new_base_branch_ref_name);
6666 free(branch_ref_name);
6667 free(commit_ref_name);
6668 return err;
6671 const struct got_error *
6672 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6673 struct got_object_id *new_commit_id, struct got_repository *repo)
6675 const struct got_error *err;
6676 struct got_reference *ref = NULL;
6677 struct got_object_id *old_commit_id = NULL;
6678 const char *branch_name = NULL;
6679 char *new_id_str = NULL;
6680 char *refname = NULL;
6682 branch_name = got_ref_get_name(branch);
6683 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6684 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6685 branch_name += 11;
6687 err = got_object_id_str(&new_id_str, new_commit_id);
6688 if (err)
6689 return err;
6691 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6692 new_id_str) == -1) {
6693 err = got_error_from_errno("asprintf");
6694 goto done;
6697 err = got_ref_resolve(&old_commit_id, repo, branch);
6698 if (err)
6699 goto done;
6701 err = got_ref_alloc(&ref, refname, old_commit_id);
6702 if (err)
6703 goto done;
6705 err = got_ref_write(ref, repo);
6706 done:
6707 free(new_id_str);
6708 free(refname);
6709 free(old_commit_id);
6710 if (ref)
6711 got_ref_close(ref);
6712 return err;
6715 const struct got_error *
6716 got_worktree_rebase_complete(struct got_worktree *worktree,
6717 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6718 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6719 struct got_repository *repo, int create_backup)
6721 const struct got_error *err, *unlockerr, *sync_err;
6722 struct got_object_id *new_head_commit_id = NULL;
6723 char *fileindex_path = NULL;
6725 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6726 if (err)
6727 return err;
6729 if (create_backup) {
6730 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6731 rebased_branch, new_head_commit_id, repo);
6732 if (err)
6733 goto done;
6736 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6737 if (err)
6738 goto done;
6740 err = got_ref_write(rebased_branch, repo);
6741 if (err)
6742 goto done;
6744 err = got_worktree_set_head_ref(worktree, rebased_branch);
6745 if (err)
6746 goto done;
6748 err = delete_rebase_refs(worktree, repo);
6749 if (err)
6750 goto done;
6752 err = get_fileindex_path(&fileindex_path, worktree);
6753 if (err)
6754 goto done;
6755 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6756 sync_err = sync_fileindex(fileindex, fileindex_path);
6757 if (sync_err && err == NULL)
6758 err = sync_err;
6759 done:
6760 got_fileindex_free(fileindex);
6761 free(fileindex_path);
6762 free(new_head_commit_id);
6763 unlockerr = lock_worktree(worktree, LOCK_SH);
6764 if (unlockerr && err == NULL)
6765 err = unlockerr;
6766 return err;
6769 const struct got_error *
6770 got_worktree_rebase_abort(struct got_worktree *worktree,
6771 struct got_fileindex *fileindex, struct got_repository *repo,
6772 struct got_reference *new_base_branch,
6773 got_worktree_checkout_cb progress_cb, void *progress_arg)
6775 const struct got_error *err, *unlockerr, *sync_err;
6776 struct got_reference *resolved = NULL;
6777 struct got_object_id *commit_id = NULL;
6778 char *fileindex_path = NULL;
6779 struct revert_file_args rfa;
6780 struct got_object_id *tree_id = NULL;
6782 err = lock_worktree(worktree, LOCK_EX);
6783 if (err)
6784 return err;
6786 err = got_ref_open(&resolved, repo,
6787 got_ref_get_symref_target(new_base_branch), 0);
6788 if (err)
6789 goto done;
6791 err = got_worktree_set_head_ref(worktree, resolved);
6792 if (err)
6793 goto done;
6796 * XXX commits to the base branch could have happened while
6797 * we were busy rebasing; should we store the original commit ID
6798 * when rebase begins and read it back here?
6800 err = got_ref_resolve(&commit_id, repo, resolved);
6801 if (err)
6802 goto done;
6804 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6805 if (err)
6806 goto done;
6808 err = got_object_id_by_path(&tree_id, repo,
6809 worktree->base_commit_id, worktree->path_prefix);
6810 if (err)
6811 goto done;
6813 err = delete_rebase_refs(worktree, repo);
6814 if (err)
6815 goto done;
6817 err = get_fileindex_path(&fileindex_path, worktree);
6818 if (err)
6819 goto done;
6821 rfa.worktree = worktree;
6822 rfa.fileindex = fileindex;
6823 rfa.progress_cb = progress_cb;
6824 rfa.progress_arg = progress_arg;
6825 rfa.patch_cb = NULL;
6826 rfa.patch_arg = NULL;
6827 rfa.repo = repo;
6828 rfa.unlink_added_files = 0;
6829 err = worktree_status(worktree, "", fileindex, repo,
6830 revert_file, &rfa, NULL, NULL, 0, 0);
6831 if (err)
6832 goto sync;
6834 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6835 repo, progress_cb, progress_arg, NULL, NULL);
6836 sync:
6837 sync_err = sync_fileindex(fileindex, fileindex_path);
6838 if (sync_err && err == NULL)
6839 err = sync_err;
6840 done:
6841 got_ref_close(resolved);
6842 free(tree_id);
6843 free(commit_id);
6844 if (fileindex)
6845 got_fileindex_free(fileindex);
6846 free(fileindex_path);
6848 unlockerr = lock_worktree(worktree, LOCK_SH);
6849 if (unlockerr && err == NULL)
6850 err = unlockerr;
6851 return err;
6854 const struct got_error *
6855 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6856 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6857 struct got_fileindex **fileindex, struct got_worktree *worktree,
6858 struct got_repository *repo)
6860 const struct got_error *err = NULL;
6861 char *tmp_branch_name = NULL;
6862 char *branch_ref_name = NULL;
6863 char *base_commit_ref_name = NULL;
6864 char *fileindex_path = NULL;
6865 struct check_rebase_ok_arg ok_arg;
6866 struct got_reference *wt_branch = NULL;
6867 struct got_reference *base_commit_ref = NULL;
6869 *tmp_branch = NULL;
6870 *branch_ref = NULL;
6871 *base_commit_id = NULL;
6872 *fileindex = NULL;
6874 err = lock_worktree(worktree, LOCK_EX);
6875 if (err)
6876 return err;
6878 err = open_fileindex(fileindex, &fileindex_path, worktree);
6879 if (err)
6880 goto done;
6882 ok_arg.worktree = worktree;
6883 ok_arg.repo = repo;
6884 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6885 &ok_arg);
6886 if (err)
6887 goto done;
6889 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6890 if (err)
6891 goto done;
6893 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6894 if (err)
6895 goto done;
6897 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6898 worktree);
6899 if (err)
6900 goto done;
6902 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6903 0);
6904 if (err)
6905 goto done;
6907 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6908 if (err)
6909 goto done;
6911 err = got_ref_write(*branch_ref, repo);
6912 if (err)
6913 goto done;
6915 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6916 worktree->base_commit_id);
6917 if (err)
6918 goto done;
6919 err = got_ref_write(base_commit_ref, repo);
6920 if (err)
6921 goto done;
6922 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6923 if (*base_commit_id == NULL) {
6924 err = got_error_from_errno("got_object_id_dup");
6925 goto done;
6928 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6929 worktree->base_commit_id);
6930 if (err)
6931 goto done;
6932 err = got_ref_write(*tmp_branch, repo);
6933 if (err)
6934 goto done;
6936 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6937 if (err)
6938 goto done;
6939 done:
6940 free(fileindex_path);
6941 free(tmp_branch_name);
6942 free(branch_ref_name);
6943 free(base_commit_ref_name);
6944 if (wt_branch)
6945 got_ref_close(wt_branch);
6946 if (err) {
6947 if (*branch_ref) {
6948 got_ref_close(*branch_ref);
6949 *branch_ref = NULL;
6951 if (*tmp_branch) {
6952 got_ref_close(*tmp_branch);
6953 *tmp_branch = NULL;
6955 free(*base_commit_id);
6956 if (*fileindex) {
6957 got_fileindex_free(*fileindex);
6958 *fileindex = NULL;
6960 lock_worktree(worktree, LOCK_SH);
6962 return err;
6965 const struct got_error *
6966 got_worktree_histedit_postpone(struct got_worktree *worktree,
6967 struct got_fileindex *fileindex)
6969 if (fileindex)
6970 got_fileindex_free(fileindex);
6971 return lock_worktree(worktree, LOCK_SH);
6974 const struct got_error *
6975 got_worktree_histedit_in_progress(int *in_progress,
6976 struct got_worktree *worktree)
6978 const struct got_error *err;
6979 char *tmp_branch_name = NULL;
6981 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6982 if (err)
6983 return err;
6985 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6986 free(tmp_branch_name);
6987 return NULL;
6990 const struct got_error *
6991 got_worktree_histedit_continue(struct got_object_id **commit_id,
6992 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6993 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6994 struct got_worktree *worktree, struct got_repository *repo)
6996 const struct got_error *err;
6997 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6998 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6999 struct got_reference *commit_ref = NULL;
7000 struct got_reference *base_commit_ref = NULL;
7001 char *fileindex_path = NULL;
7002 int have_staged_files = 0;
7004 *commit_id = NULL;
7005 *tmp_branch = NULL;
7006 *base_commit_id = NULL;
7007 *fileindex = NULL;
7009 err = lock_worktree(worktree, LOCK_EX);
7010 if (err)
7011 return err;
7013 err = open_fileindex(fileindex, &fileindex_path, worktree);
7014 if (err)
7015 goto done;
7017 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7018 &have_staged_files);
7019 if (err && err->code != GOT_ERR_CANCELLED)
7020 goto done;
7021 if (have_staged_files) {
7022 err = got_error(GOT_ERR_STAGED_PATHS);
7023 goto done;
7026 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7027 if (err)
7028 goto done;
7030 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7031 if (err)
7032 goto done;
7034 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7035 if (err)
7036 goto done;
7038 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7039 worktree);
7040 if (err)
7041 goto done;
7043 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7044 if (err)
7045 goto done;
7047 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7048 if (err)
7049 goto done;
7050 err = got_ref_resolve(commit_id, repo, commit_ref);
7051 if (err)
7052 goto done;
7054 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7055 if (err)
7056 goto done;
7057 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7058 if (err)
7059 goto done;
7061 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7062 if (err)
7063 goto done;
7064 done:
7065 free(commit_ref_name);
7066 free(branch_ref_name);
7067 free(fileindex_path);
7068 if (commit_ref)
7069 got_ref_close(commit_ref);
7070 if (base_commit_ref)
7071 got_ref_close(base_commit_ref);
7072 if (err) {
7073 free(*commit_id);
7074 *commit_id = NULL;
7075 free(*base_commit_id);
7076 *base_commit_id = NULL;
7077 if (*tmp_branch) {
7078 got_ref_close(*tmp_branch);
7079 *tmp_branch = NULL;
7081 if (*fileindex) {
7082 got_fileindex_free(*fileindex);
7083 *fileindex = NULL;
7085 lock_worktree(worktree, LOCK_EX);
7087 return err;
7090 static const struct got_error *
7091 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7093 const struct got_error *err;
7094 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7095 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7097 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7098 if (err)
7099 goto done;
7100 err = delete_ref(tmp_branch_name, repo);
7101 if (err)
7102 goto done;
7104 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7105 worktree);
7106 if (err)
7107 goto done;
7108 err = delete_ref(base_commit_ref_name, repo);
7109 if (err)
7110 goto done;
7112 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7113 if (err)
7114 goto done;
7115 err = delete_ref(branch_ref_name, repo);
7116 if (err)
7117 goto done;
7119 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7120 if (err)
7121 goto done;
7122 err = delete_ref(commit_ref_name, repo);
7123 if (err)
7124 goto done;
7125 done:
7126 free(tmp_branch_name);
7127 free(base_commit_ref_name);
7128 free(branch_ref_name);
7129 free(commit_ref_name);
7130 return err;
7133 const struct got_error *
7134 got_worktree_histedit_abort(struct got_worktree *worktree,
7135 struct got_fileindex *fileindex, struct got_repository *repo,
7136 struct got_reference *branch, struct got_object_id *base_commit_id,
7137 got_worktree_checkout_cb progress_cb, void *progress_arg)
7139 const struct got_error *err, *unlockerr, *sync_err;
7140 struct got_reference *resolved = NULL;
7141 char *fileindex_path = NULL;
7142 struct got_object_id *tree_id = NULL;
7143 struct revert_file_args rfa;
7145 err = lock_worktree(worktree, LOCK_EX);
7146 if (err)
7147 return err;
7149 err = got_ref_open(&resolved, repo,
7150 got_ref_get_symref_target(branch), 0);
7151 if (err)
7152 goto done;
7154 err = got_worktree_set_head_ref(worktree, resolved);
7155 if (err)
7156 goto done;
7158 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7159 if (err)
7160 goto done;
7162 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7163 worktree->path_prefix);
7164 if (err)
7165 goto done;
7167 err = delete_histedit_refs(worktree, repo);
7168 if (err)
7169 goto done;
7171 err = get_fileindex_path(&fileindex_path, worktree);
7172 if (err)
7173 goto done;
7175 rfa.worktree = worktree;
7176 rfa.fileindex = fileindex;
7177 rfa.progress_cb = progress_cb;
7178 rfa.progress_arg = progress_arg;
7179 rfa.patch_cb = NULL;
7180 rfa.patch_arg = NULL;
7181 rfa.repo = repo;
7182 rfa.unlink_added_files = 0;
7183 err = worktree_status(worktree, "", fileindex, repo,
7184 revert_file, &rfa, NULL, NULL, 0, 0);
7185 if (err)
7186 goto sync;
7188 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7189 repo, progress_cb, progress_arg, NULL, NULL);
7190 sync:
7191 sync_err = sync_fileindex(fileindex, fileindex_path);
7192 if (sync_err && err == NULL)
7193 err = sync_err;
7194 done:
7195 got_ref_close(resolved);
7196 free(tree_id);
7197 free(fileindex_path);
7199 unlockerr = lock_worktree(worktree, LOCK_SH);
7200 if (unlockerr && err == NULL)
7201 err = unlockerr;
7202 return err;
7205 const struct got_error *
7206 got_worktree_histedit_complete(struct got_worktree *worktree,
7207 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7208 struct got_reference *edited_branch, struct got_repository *repo)
7210 const struct got_error *err, *unlockerr, *sync_err;
7211 struct got_object_id *new_head_commit_id = NULL;
7212 struct got_reference *resolved = NULL;
7213 char *fileindex_path = NULL;
7215 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7216 if (err)
7217 return err;
7219 err = got_ref_open(&resolved, repo,
7220 got_ref_get_symref_target(edited_branch), 0);
7221 if (err)
7222 goto done;
7224 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7225 resolved, new_head_commit_id, repo);
7226 if (err)
7227 goto done;
7229 err = got_ref_change_ref(resolved, new_head_commit_id);
7230 if (err)
7231 goto done;
7233 err = got_ref_write(resolved, repo);
7234 if (err)
7235 goto done;
7237 err = got_worktree_set_head_ref(worktree, resolved);
7238 if (err)
7239 goto done;
7241 err = delete_histedit_refs(worktree, repo);
7242 if (err)
7243 goto done;
7245 err = get_fileindex_path(&fileindex_path, worktree);
7246 if (err)
7247 goto done;
7248 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7249 sync_err = sync_fileindex(fileindex, fileindex_path);
7250 if (sync_err && err == NULL)
7251 err = sync_err;
7252 done:
7253 got_fileindex_free(fileindex);
7254 free(fileindex_path);
7255 free(new_head_commit_id);
7256 unlockerr = lock_worktree(worktree, LOCK_SH);
7257 if (unlockerr && err == NULL)
7258 err = unlockerr;
7259 return err;
7262 const struct got_error *
7263 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7264 struct got_object_id *commit_id, struct got_repository *repo)
7266 const struct got_error *err;
7267 char *commit_ref_name;
7269 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7270 if (err)
7271 return err;
7273 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7274 if (err)
7275 goto done;
7277 err = delete_ref(commit_ref_name, repo);
7278 done:
7279 free(commit_ref_name);
7280 return err;
7283 const struct got_error *
7284 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7285 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7286 struct got_worktree *worktree, const char *refname,
7287 struct got_repository *repo)
7289 const struct got_error *err = NULL;
7290 char *fileindex_path = NULL;
7291 struct check_rebase_ok_arg ok_arg;
7293 *fileindex = NULL;
7294 *branch_ref = NULL;
7295 *base_branch_ref = NULL;
7297 err = lock_worktree(worktree, LOCK_EX);
7298 if (err)
7299 return err;
7301 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7302 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7303 "cannot integrate a branch into itself; "
7304 "update -b or different branch name required");
7305 goto done;
7308 err = open_fileindex(fileindex, &fileindex_path, worktree);
7309 if (err)
7310 goto done;
7312 /* Preconditions are the same as for rebase. */
7313 ok_arg.worktree = worktree;
7314 ok_arg.repo = repo;
7315 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7316 &ok_arg);
7317 if (err)
7318 goto done;
7320 err = got_ref_open(branch_ref, repo, refname, 1);
7321 if (err)
7322 goto done;
7324 err = got_ref_open(base_branch_ref, repo,
7325 got_worktree_get_head_ref_name(worktree), 1);
7326 done:
7327 if (err) {
7328 if (*branch_ref) {
7329 got_ref_close(*branch_ref);
7330 *branch_ref = NULL;
7332 if (*base_branch_ref) {
7333 got_ref_close(*base_branch_ref);
7334 *base_branch_ref = NULL;
7336 if (*fileindex) {
7337 got_fileindex_free(*fileindex);
7338 *fileindex = NULL;
7340 lock_worktree(worktree, LOCK_SH);
7342 return err;
7345 const struct got_error *
7346 got_worktree_integrate_continue(struct got_worktree *worktree,
7347 struct got_fileindex *fileindex, struct got_repository *repo,
7348 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7349 got_worktree_checkout_cb progress_cb, void *progress_arg,
7350 got_cancel_cb cancel_cb, void *cancel_arg)
7352 const struct got_error *err = NULL, *sync_err, *unlockerr;
7353 char *fileindex_path = NULL;
7354 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7356 err = get_fileindex_path(&fileindex_path, worktree);
7357 if (err)
7358 goto done;
7360 err = got_ref_resolve(&commit_id, repo, branch_ref);
7361 if (err)
7362 goto done;
7364 err = got_object_id_by_path(&tree_id, repo, commit_id,
7365 worktree->path_prefix);
7366 if (err)
7367 goto done;
7369 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7370 if (err)
7371 goto done;
7373 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7374 progress_cb, progress_arg, cancel_cb, cancel_arg);
7375 if (err)
7376 goto sync;
7378 err = got_ref_change_ref(base_branch_ref, commit_id);
7379 if (err)
7380 goto sync;
7382 err = got_ref_write(base_branch_ref, repo);
7383 if (err)
7384 goto sync;
7386 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7387 sync:
7388 sync_err = sync_fileindex(fileindex, fileindex_path);
7389 if (sync_err && err == NULL)
7390 err = sync_err;
7392 done:
7393 unlockerr = got_ref_unlock(branch_ref);
7394 if (unlockerr && err == NULL)
7395 err = unlockerr;
7396 got_ref_close(branch_ref);
7398 unlockerr = got_ref_unlock(base_branch_ref);
7399 if (unlockerr && err == NULL)
7400 err = unlockerr;
7401 got_ref_close(base_branch_ref);
7403 got_fileindex_free(fileindex);
7404 free(fileindex_path);
7405 free(tree_id);
7407 unlockerr = lock_worktree(worktree, LOCK_SH);
7408 if (unlockerr && err == NULL)
7409 err = unlockerr;
7410 return err;
7413 const struct got_error *
7414 got_worktree_integrate_abort(struct got_worktree *worktree,
7415 struct got_fileindex *fileindex, struct got_repository *repo,
7416 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7418 const struct got_error *err = NULL, *unlockerr = NULL;
7420 got_fileindex_free(fileindex);
7422 err = lock_worktree(worktree, LOCK_SH);
7424 unlockerr = got_ref_unlock(branch_ref);
7425 if (unlockerr && err == NULL)
7426 err = unlockerr;
7427 got_ref_close(branch_ref);
7429 unlockerr = got_ref_unlock(base_branch_ref);
7430 if (unlockerr && err == NULL)
7431 err = unlockerr;
7432 got_ref_close(base_branch_ref);
7434 return err;
7437 const struct got_error *
7438 got_worktree_merge_postpone(struct got_worktree *worktree,
7439 struct got_fileindex *fileindex)
7441 const struct got_error *err, *sync_err;
7442 char *fileindex_path = NULL;
7444 err = get_fileindex_path(&fileindex_path, worktree);
7445 if (err)
7446 goto done;
7448 sync_err = sync_fileindex(fileindex, fileindex_path);
7450 err = lock_worktree(worktree, LOCK_SH);
7451 if (sync_err && err == NULL)
7452 err = sync_err;
7453 done:
7454 got_fileindex_free(fileindex);
7455 free(fileindex_path);
7456 return err;
7459 static const struct got_error *
7460 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7462 const struct got_error *err;
7463 char *branch_refname = NULL, *commit_refname = NULL;
7465 err = get_merge_branch_ref_name(&branch_refname, worktree);
7466 if (err)
7467 goto done;
7468 err = delete_ref(branch_refname, repo);
7469 if (err)
7470 goto done;
7472 err = get_merge_commit_ref_name(&commit_refname, worktree);
7473 if (err)
7474 goto done;
7475 err = delete_ref(commit_refname, repo);
7476 if (err)
7477 goto done;
7479 done:
7480 free(branch_refname);
7481 free(commit_refname);
7482 return err;
7485 struct merge_commit_msg_arg {
7486 struct got_worktree *worktree;
7487 const char *branch_name;
7490 static const struct got_error *
7491 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7492 void *arg)
7494 struct merge_commit_msg_arg *a = arg;
7496 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7497 got_worktree_get_head_ref_name(a->worktree)) == -1)
7498 return got_error_from_errno("asprintf");
7500 return NULL;
7503 static const struct got_error *
7504 merge_status_cb(void *arg, unsigned char status, unsigned char staged_status,
7505 const char *path, struct got_object_id *blob_id,
7506 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7507 int dirfd, const char *de_name)
7509 return NULL;
7512 const struct got_error *
7513 got_worktree_merge_branch(struct got_worktree *worktree,
7514 struct got_fileindex *fileindex,
7515 struct got_object_id *yca_commit_id,
7516 struct got_object_id *branch_tip,
7517 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7518 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7520 const struct got_error *err;
7521 char *fileindex_path = NULL;
7523 err = get_fileindex_path(&fileindex_path, worktree);
7524 if (err)
7525 goto done;
7527 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7528 worktree);
7529 if (err)
7530 goto done;
7532 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7533 branch_tip, repo, progress_cb, progress_arg,
7534 cancel_cb, cancel_arg);
7535 done:
7536 free(fileindex_path);
7537 return err;
7540 const struct got_error *
7541 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7542 struct got_worktree *worktree, struct got_fileindex *fileindex,
7543 const char *author, const char *committer, int allow_bad_symlinks,
7544 struct got_object_id *branch_tip, const char *branch_name,
7545 struct got_repository *repo)
7547 const struct got_error *err = NULL, *sync_err;
7548 struct got_pathlist_head commitable_paths;
7549 struct collect_commitables_arg cc_arg;
7550 struct got_pathlist_entry *pe;
7551 struct got_reference *head_ref = NULL;
7552 struct got_object_id *head_commit_id = NULL;
7553 int have_staged_files = 0;
7554 struct merge_commit_msg_arg mcm_arg;
7555 char *fileindex_path = NULL;
7557 *new_commit_id = NULL;
7559 TAILQ_INIT(&commitable_paths);
7561 err = get_fileindex_path(&fileindex_path, worktree);
7562 if (err)
7563 goto done;
7565 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7566 if (err)
7567 goto done;
7569 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7570 if (err)
7571 goto done;
7573 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7574 &have_staged_files);
7575 if (err && err->code != GOT_ERR_CANCELLED)
7576 goto done;
7577 if (have_staged_files) {
7578 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7579 goto done;
7582 cc_arg.commitable_paths = &commitable_paths;
7583 cc_arg.worktree = worktree;
7584 cc_arg.fileindex = fileindex;
7585 cc_arg.repo = repo;
7586 cc_arg.have_staged_files = have_staged_files;
7587 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7588 err = worktree_status(worktree, "", fileindex, repo,
7589 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
7590 if (err)
7591 goto done;
7593 if (TAILQ_EMPTY(&commitable_paths)) {
7594 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7595 "merge of %s cannot proceed", branch_name);
7596 goto done;
7599 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7600 struct got_commitable *ct = pe->data;
7601 const char *ct_path = ct->in_repo_path;
7603 while (ct_path[0] == '/')
7604 ct_path++;
7605 err = check_out_of_date(ct_path, ct->status,
7606 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7607 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7608 if (err)
7609 goto done;
7613 mcm_arg.worktree = worktree;
7614 mcm_arg.branch_name = branch_name;
7615 err = commit_worktree(new_commit_id, &commitable_paths,
7616 head_commit_id, branch_tip, worktree, author, committer,
7617 merge_commit_msg_cb, &mcm_arg, merge_status_cb, NULL, repo);
7618 if (err)
7619 goto done;
7621 err = update_fileindex_after_commit(worktree, &commitable_paths,
7622 *new_commit_id, fileindex, have_staged_files);
7623 sync_err = sync_fileindex(fileindex, fileindex_path);
7624 if (sync_err && err == NULL)
7625 err = sync_err;
7626 done:
7627 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7628 struct got_commitable *ct = pe->data;
7629 free_commitable(ct);
7631 got_pathlist_free(&commitable_paths);
7632 free(fileindex_path);
7633 return err;
7636 const struct got_error *
7637 got_worktree_merge_complete(struct got_worktree *worktree,
7638 struct got_fileindex *fileindex, struct got_repository *repo)
7640 const struct got_error *err, *unlockerr, *sync_err;
7641 char *fileindex_path = NULL;
7643 err = delete_merge_refs(worktree, repo);
7644 if (err)
7645 goto done;
7647 err = get_fileindex_path(&fileindex_path, worktree);
7648 if (err)
7649 goto done;
7650 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7651 sync_err = sync_fileindex(fileindex, fileindex_path);
7652 if (sync_err && err == NULL)
7653 err = sync_err;
7654 done:
7655 got_fileindex_free(fileindex);
7656 free(fileindex_path);
7657 unlockerr = lock_worktree(worktree, LOCK_SH);
7658 if (unlockerr && err == NULL)
7659 err = unlockerr;
7660 return err;
7663 const struct got_error *
7664 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7665 struct got_repository *repo)
7667 const struct got_error *err;
7668 char *branch_refname = NULL;
7669 struct got_reference *branch_ref = NULL;
7671 *in_progress = 0;
7673 err = get_merge_branch_ref_name(&branch_refname, worktree);
7674 if (err)
7675 return err;
7676 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7677 free(branch_refname);
7678 if (err) {
7679 if (err->code != GOT_ERR_NOT_REF)
7680 return err;
7681 } else
7682 *in_progress = 1;
7684 return NULL;
7687 const struct got_error *got_worktree_merge_prepare(
7688 struct got_fileindex **fileindex, struct got_worktree *worktree,
7689 struct got_reference *branch, struct got_repository *repo)
7691 const struct got_error *err = NULL;
7692 char *fileindex_path = NULL;
7693 char *branch_refname = NULL, *commit_refname = NULL;
7694 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7695 struct got_reference *commit_ref = NULL;
7696 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7697 struct check_rebase_ok_arg ok_arg;
7699 *fileindex = NULL;
7701 err = lock_worktree(worktree, LOCK_EX);
7702 if (err)
7703 return err;
7705 err = open_fileindex(fileindex, &fileindex_path, worktree);
7706 if (err)
7707 goto done;
7709 /* Preconditions are the same as for rebase. */
7710 ok_arg.worktree = worktree;
7711 ok_arg.repo = repo;
7712 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7713 &ok_arg);
7714 if (err)
7715 goto done;
7717 err = get_merge_branch_ref_name(&branch_refname, worktree);
7718 if (err)
7719 return err;
7721 err = get_merge_commit_ref_name(&commit_refname, worktree);
7722 if (err)
7723 return err;
7725 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7726 0);
7727 if (err)
7728 goto done;
7730 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7731 if (err)
7732 goto done;
7734 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7735 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7736 goto done;
7739 err = got_ref_resolve(&branch_tip, repo, branch);
7740 if (err)
7741 goto done;
7743 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7744 if (err)
7745 goto done;
7746 err = got_ref_write(branch_ref, repo);
7747 if (err)
7748 goto done;
7750 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7751 if (err)
7752 goto done;
7753 err = got_ref_write(commit_ref, repo);
7754 if (err)
7755 goto done;
7757 done:
7758 free(branch_refname);
7759 free(commit_refname);
7760 free(fileindex_path);
7761 if (branch_ref)
7762 got_ref_close(branch_ref);
7763 if (commit_ref)
7764 got_ref_close(commit_ref);
7765 if (wt_branch)
7766 got_ref_close(wt_branch);
7767 free(wt_branch_tip);
7768 if (err) {
7769 if (*fileindex) {
7770 got_fileindex_free(*fileindex);
7771 *fileindex = NULL;
7773 lock_worktree(worktree, LOCK_SH);
7775 return err;
7778 const struct got_error *
7779 got_worktree_merge_continue(char **branch_name,
7780 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7781 struct got_worktree *worktree, struct got_repository *repo)
7783 const struct got_error *err;
7784 char *commit_refname = NULL, *branch_refname = NULL;
7785 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7786 char *fileindex_path = NULL;
7787 int have_staged_files = 0;
7789 *branch_name = NULL;
7790 *branch_tip = NULL;
7791 *fileindex = NULL;
7793 err = lock_worktree(worktree, LOCK_EX);
7794 if (err)
7795 return err;
7797 err = open_fileindex(fileindex, &fileindex_path, worktree);
7798 if (err)
7799 goto done;
7801 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7802 &have_staged_files);
7803 if (err && err->code != GOT_ERR_CANCELLED)
7804 goto done;
7805 if (have_staged_files) {
7806 err = got_error(GOT_ERR_STAGED_PATHS);
7807 goto done;
7810 err = get_merge_branch_ref_name(&branch_refname, worktree);
7811 if (err)
7812 goto done;
7814 err = get_merge_commit_ref_name(&commit_refname, worktree);
7815 if (err)
7816 goto done;
7818 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7819 if (err)
7820 goto done;
7822 if (!got_ref_is_symbolic(branch_ref)) {
7823 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7824 "%s is not a symbolic reference",
7825 got_ref_get_name(branch_ref));
7826 goto done;
7828 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7829 if (*branch_name == NULL) {
7830 err = got_error_from_errno("strdup");
7831 goto done;
7834 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7835 if (err)
7836 goto done;
7838 err = got_ref_resolve(branch_tip, repo, commit_ref);
7839 if (err)
7840 goto done;
7841 done:
7842 free(commit_refname);
7843 free(branch_refname);
7844 free(fileindex_path);
7845 if (commit_ref)
7846 got_ref_close(commit_ref);
7847 if (branch_ref)
7848 got_ref_close(branch_ref);
7849 if (err) {
7850 if (*branch_name) {
7851 free(*branch_name);
7852 *branch_name = NULL;
7854 free(*branch_tip);
7855 *branch_tip = NULL;
7856 if (*fileindex) {
7857 got_fileindex_free(*fileindex);
7858 *fileindex = NULL;
7860 lock_worktree(worktree, LOCK_SH);
7862 return err;
7865 const struct got_error *
7866 got_worktree_merge_abort(struct got_worktree *worktree,
7867 struct got_fileindex *fileindex, struct got_repository *repo,
7868 got_worktree_checkout_cb progress_cb, void *progress_arg)
7870 const struct got_error *err, *unlockerr, *sync_err;
7871 struct got_object_id *commit_id = NULL;
7872 char *fileindex_path = NULL;
7873 struct revert_file_args rfa;
7874 struct got_object_id *tree_id = NULL;
7876 err = got_object_id_by_path(&tree_id, repo,
7877 worktree->base_commit_id, worktree->path_prefix);
7878 if (err)
7879 goto done;
7881 err = delete_merge_refs(worktree, repo);
7882 if (err)
7883 goto done;
7885 err = get_fileindex_path(&fileindex_path, worktree);
7886 if (err)
7887 goto done;
7889 rfa.worktree = worktree;
7890 rfa.fileindex = fileindex;
7891 rfa.progress_cb = progress_cb;
7892 rfa.progress_arg = progress_arg;
7893 rfa.patch_cb = NULL;
7894 rfa.patch_arg = NULL;
7895 rfa.repo = repo;
7896 rfa.unlink_added_files = 1;
7897 err = worktree_status(worktree, "", fileindex, repo,
7898 revert_file, &rfa, NULL, NULL, 0, 0);
7899 if (err)
7900 goto sync;
7902 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7903 repo, progress_cb, progress_arg, NULL, NULL);
7904 sync:
7905 sync_err = sync_fileindex(fileindex, fileindex_path);
7906 if (sync_err && err == NULL)
7907 err = sync_err;
7908 done:
7909 free(tree_id);
7910 free(commit_id);
7911 if (fileindex)
7912 got_fileindex_free(fileindex);
7913 free(fileindex_path);
7915 unlockerr = lock_worktree(worktree, LOCK_SH);
7916 if (unlockerr && err == NULL)
7917 err = unlockerr;
7918 return err;
7921 struct check_stage_ok_arg {
7922 struct got_object_id *head_commit_id;
7923 struct got_worktree *worktree;
7924 struct got_fileindex *fileindex;
7925 struct got_repository *repo;
7926 int have_changes;
7929 const struct got_error *
7930 check_stage_ok(void *arg, unsigned char status,
7931 unsigned char staged_status, const char *relpath,
7932 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7933 struct got_object_id *commit_id, int dirfd, const char *de_name)
7935 struct check_stage_ok_arg *a = arg;
7936 const struct got_error *err = NULL;
7937 struct got_fileindex_entry *ie;
7938 struct got_object_id base_commit_id;
7939 struct got_object_id *base_commit_idp = NULL;
7940 char *in_repo_path = NULL, *p;
7942 if (status == GOT_STATUS_UNVERSIONED ||
7943 status == GOT_STATUS_NO_CHANGE)
7944 return NULL;
7945 if (status == GOT_STATUS_NONEXISTENT)
7946 return got_error_set_errno(ENOENT, relpath);
7948 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7949 if (ie == NULL)
7950 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7952 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7953 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7954 relpath) == -1)
7955 return got_error_from_errno("asprintf");
7957 if (got_fileindex_entry_has_commit(ie)) {
7958 memcpy(base_commit_id.sha1, ie->commit_sha1,
7959 SHA1_DIGEST_LENGTH);
7960 base_commit_idp = &base_commit_id;
7963 if (status == GOT_STATUS_CONFLICT) {
7964 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7965 goto done;
7966 } else if (status != GOT_STATUS_ADD &&
7967 status != GOT_STATUS_MODIFY &&
7968 status != GOT_STATUS_DELETE) {
7969 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7970 goto done;
7973 a->have_changes = 1;
7975 p = in_repo_path;
7976 while (p[0] == '/')
7977 p++;
7978 err = check_out_of_date(p, status, staged_status,
7979 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7980 GOT_ERR_STAGE_OUT_OF_DATE);
7981 done:
7982 free(in_repo_path);
7983 return err;
7986 struct stage_path_arg {
7987 struct got_worktree *worktree;
7988 struct got_fileindex *fileindex;
7989 struct got_repository *repo;
7990 got_worktree_status_cb status_cb;
7991 void *status_arg;
7992 got_worktree_patch_cb patch_cb;
7993 void *patch_arg;
7994 int staged_something;
7995 int allow_bad_symlinks;
7998 static const struct got_error *
7999 stage_path(void *arg, unsigned char status,
8000 unsigned char staged_status, const char *relpath,
8001 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8002 struct got_object_id *commit_id, int dirfd, const char *de_name)
8004 struct stage_path_arg *a = arg;
8005 const struct got_error *err = NULL;
8006 struct got_fileindex_entry *ie;
8007 char *ondisk_path = NULL, *path_content = NULL;
8008 uint32_t stage;
8009 struct got_object_id *new_staged_blob_id = NULL;
8010 struct stat sb;
8012 if (status == GOT_STATUS_UNVERSIONED)
8013 return NULL;
8015 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8016 if (ie == NULL)
8017 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8019 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8020 relpath)== -1)
8021 return got_error_from_errno("asprintf");
8023 switch (status) {
8024 case GOT_STATUS_ADD:
8025 case GOT_STATUS_MODIFY:
8026 /* XXX could sb.st_mode be passed in by our caller? */
8027 if (lstat(ondisk_path, &sb) == -1) {
8028 err = got_error_from_errno2("lstat", ondisk_path);
8029 break;
8031 if (a->patch_cb) {
8032 if (status == GOT_STATUS_ADD) {
8033 int choice = GOT_PATCH_CHOICE_NONE;
8034 err = (*a->patch_cb)(&choice, a->patch_arg,
8035 status, ie->path, NULL, 1, 1);
8036 if (err)
8037 break;
8038 if (choice != GOT_PATCH_CHOICE_YES)
8039 break;
8040 } else {
8041 err = create_patched_content(&path_content, 0,
8042 staged_blob_id ? staged_blob_id : blob_id,
8043 ondisk_path, dirfd, de_name, ie->path,
8044 a->repo, a->patch_cb, a->patch_arg);
8045 if (err || path_content == NULL)
8046 break;
8049 err = got_object_blob_create(&new_staged_blob_id,
8050 path_content ? path_content : ondisk_path, a->repo);
8051 if (err)
8052 break;
8053 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8054 SHA1_DIGEST_LENGTH);
8055 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8056 stage = GOT_FILEIDX_STAGE_ADD;
8057 else
8058 stage = GOT_FILEIDX_STAGE_MODIFY;
8059 got_fileindex_entry_stage_set(ie, stage);
8060 if (S_ISLNK(sb.st_mode)) {
8061 int is_bad_symlink = 0;
8062 if (!a->allow_bad_symlinks) {
8063 char target_path[PATH_MAX];
8064 ssize_t target_len;
8065 target_len = readlink(ondisk_path, target_path,
8066 sizeof(target_path));
8067 if (target_len == -1) {
8068 err = got_error_from_errno2("readlink",
8069 ondisk_path);
8070 break;
8072 err = is_bad_symlink_target(&is_bad_symlink,
8073 target_path, target_len, ondisk_path,
8074 a->worktree->root_path);
8075 if (err)
8076 break;
8077 if (is_bad_symlink) {
8078 err = got_error_path(ondisk_path,
8079 GOT_ERR_BAD_SYMLINK);
8080 break;
8083 if (is_bad_symlink)
8084 got_fileindex_entry_staged_filetype_set(ie,
8085 GOT_FILEIDX_MODE_BAD_SYMLINK);
8086 else
8087 got_fileindex_entry_staged_filetype_set(ie,
8088 GOT_FILEIDX_MODE_SYMLINK);
8089 } else {
8090 got_fileindex_entry_staged_filetype_set(ie,
8091 GOT_FILEIDX_MODE_REGULAR_FILE);
8093 a->staged_something = 1;
8094 if (a->status_cb == NULL)
8095 break;
8096 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8097 get_staged_status(ie), relpath, blob_id,
8098 new_staged_blob_id, NULL, dirfd, de_name);
8099 break;
8100 case GOT_STATUS_DELETE:
8101 if (staged_status == GOT_STATUS_DELETE)
8102 break;
8103 if (a->patch_cb) {
8104 int choice = GOT_PATCH_CHOICE_NONE;
8105 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8106 ie->path, NULL, 1, 1);
8107 if (err)
8108 break;
8109 if (choice == GOT_PATCH_CHOICE_NO)
8110 break;
8111 if (choice != GOT_PATCH_CHOICE_YES) {
8112 err = got_error(GOT_ERR_PATCH_CHOICE);
8113 break;
8116 stage = GOT_FILEIDX_STAGE_DELETE;
8117 got_fileindex_entry_stage_set(ie, stage);
8118 a->staged_something = 1;
8119 if (a->status_cb == NULL)
8120 break;
8121 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8122 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8123 de_name);
8124 break;
8125 case GOT_STATUS_NO_CHANGE:
8126 break;
8127 case GOT_STATUS_CONFLICT:
8128 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8129 break;
8130 case GOT_STATUS_NONEXISTENT:
8131 err = got_error_set_errno(ENOENT, relpath);
8132 break;
8133 default:
8134 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8135 break;
8138 if (path_content && unlink(path_content) == -1 && err == NULL)
8139 err = got_error_from_errno2("unlink", path_content);
8140 free(path_content);
8141 free(ondisk_path);
8142 free(new_staged_blob_id);
8143 return err;
8146 const struct got_error *
8147 got_worktree_stage(struct got_worktree *worktree,
8148 struct got_pathlist_head *paths,
8149 got_worktree_status_cb status_cb, void *status_arg,
8150 got_worktree_patch_cb patch_cb, void *patch_arg,
8151 int allow_bad_symlinks, struct got_repository *repo)
8153 const struct got_error *err = NULL, *sync_err, *unlockerr;
8154 struct got_pathlist_entry *pe;
8155 struct got_fileindex *fileindex = NULL;
8156 char *fileindex_path = NULL;
8157 struct got_reference *head_ref = NULL;
8158 struct got_object_id *head_commit_id = NULL;
8159 struct check_stage_ok_arg oka;
8160 struct stage_path_arg spa;
8162 err = lock_worktree(worktree, LOCK_EX);
8163 if (err)
8164 return err;
8166 err = got_ref_open(&head_ref, repo,
8167 got_worktree_get_head_ref_name(worktree), 0);
8168 if (err)
8169 goto done;
8170 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8171 if (err)
8172 goto done;
8173 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8174 if (err)
8175 goto done;
8177 /* Check pre-conditions before staging anything. */
8178 oka.head_commit_id = head_commit_id;
8179 oka.worktree = worktree;
8180 oka.fileindex = fileindex;
8181 oka.repo = repo;
8182 oka.have_changes = 0;
8183 TAILQ_FOREACH(pe, paths, entry) {
8184 err = worktree_status(worktree, pe->path, fileindex, repo,
8185 check_stage_ok, &oka, NULL, NULL, 0, 0);
8186 if (err)
8187 goto done;
8189 if (!oka.have_changes) {
8190 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8191 goto done;
8194 spa.worktree = worktree;
8195 spa.fileindex = fileindex;
8196 spa.repo = repo;
8197 spa.patch_cb = patch_cb;
8198 spa.patch_arg = patch_arg;
8199 spa.status_cb = status_cb;
8200 spa.status_arg = status_arg;
8201 spa.staged_something = 0;
8202 spa.allow_bad_symlinks = allow_bad_symlinks;
8203 TAILQ_FOREACH(pe, paths, entry) {
8204 err = worktree_status(worktree, pe->path, fileindex, repo,
8205 stage_path, &spa, NULL, NULL, 0, 0);
8206 if (err)
8207 goto done;
8209 if (!spa.staged_something) {
8210 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8211 goto done;
8214 sync_err = sync_fileindex(fileindex, fileindex_path);
8215 if (sync_err && err == NULL)
8216 err = sync_err;
8217 done:
8218 if (head_ref)
8219 got_ref_close(head_ref);
8220 free(head_commit_id);
8221 free(fileindex_path);
8222 if (fileindex)
8223 got_fileindex_free(fileindex);
8224 unlockerr = lock_worktree(worktree, LOCK_SH);
8225 if (unlockerr && err == NULL)
8226 err = unlockerr;
8227 return err;
8230 struct unstage_path_arg {
8231 struct got_worktree *worktree;
8232 struct got_fileindex *fileindex;
8233 struct got_repository *repo;
8234 got_worktree_checkout_cb progress_cb;
8235 void *progress_arg;
8236 got_worktree_patch_cb patch_cb;
8237 void *patch_arg;
8240 static const struct got_error *
8241 create_unstaged_content(char **path_unstaged_content,
8242 char **path_new_staged_content, struct got_object_id *blob_id,
8243 struct got_object_id *staged_blob_id, const char *relpath,
8244 struct got_repository *repo,
8245 got_worktree_patch_cb patch_cb, void *patch_arg)
8247 const struct got_error *err, *free_err;
8248 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8249 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8250 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8251 struct got_diffreg_result *diffreg_result = NULL;
8252 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8253 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8255 *path_unstaged_content = NULL;
8256 *path_new_staged_content = NULL;
8258 err = got_object_id_str(&label1, blob_id);
8259 if (err)
8260 return err;
8261 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8262 if (err)
8263 goto done;
8265 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8266 if (err)
8267 goto done;
8269 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8270 if (err)
8271 goto done;
8273 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8274 if (err)
8275 goto done;
8277 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8278 if (err)
8279 goto done;
8281 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8282 if (err)
8283 goto done;
8285 err = got_diff_files(&diffreg_result, f1, label1, f2,
8286 path2, 3, 0, 1, NULL);
8287 if (err)
8288 goto done;
8290 err = got_opentemp_named(path_unstaged_content, &outfile,
8291 "got-unstaged-content");
8292 if (err)
8293 goto done;
8294 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8295 "got-new-staged-content");
8296 if (err)
8297 goto done;
8299 if (fseek(f1, 0L, SEEK_SET) == -1) {
8300 err = got_ferror(f1, GOT_ERR_IO);
8301 goto done;
8303 if (fseek(f2, 0L, SEEK_SET) == -1) {
8304 err = got_ferror(f2, GOT_ERR_IO);
8305 goto done;
8307 /* Count the number of actual changes in the diff result. */
8308 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8309 struct diff_chunk_context cc = {};
8310 diff_chunk_context_load_change(&cc, &nchunks_used,
8311 diffreg_result->result, n, 0);
8312 nchanges++;
8314 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8315 int choice;
8316 err = apply_or_reject_change(&choice, &nchunks_used,
8317 diffreg_result->result, n, relpath, f1, f2,
8318 &line_cur1, &line_cur2,
8319 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8320 if (err)
8321 goto done;
8322 if (choice == GOT_PATCH_CHOICE_YES)
8323 have_content = 1;
8324 else
8325 have_rejected_content = 1;
8326 if (choice == GOT_PATCH_CHOICE_QUIT)
8327 break;
8329 if (have_content || have_rejected_content)
8330 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8331 outfile, rejectfile);
8332 done:
8333 free(label1);
8334 if (blob)
8335 got_object_blob_close(blob);
8336 if (staged_blob)
8337 got_object_blob_close(staged_blob);
8338 free_err = got_diffreg_result_free(diffreg_result);
8339 if (free_err && err == NULL)
8340 err = free_err;
8341 if (f1 && fclose(f1) == EOF && err == NULL)
8342 err = got_error_from_errno2("fclose", path1);
8343 if (f2 && fclose(f2) == EOF && err == NULL)
8344 err = got_error_from_errno2("fclose", path2);
8345 if (outfile && fclose(outfile) == EOF && err == NULL)
8346 err = got_error_from_errno2("fclose", *path_unstaged_content);
8347 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8348 err = got_error_from_errno2("fclose", *path_new_staged_content);
8349 if (path1 && unlink(path1) == -1 && err == NULL)
8350 err = got_error_from_errno2("unlink", path1);
8351 if (path2 && unlink(path2) == -1 && err == NULL)
8352 err = got_error_from_errno2("unlink", path2);
8353 if (err || !have_content) {
8354 if (*path_unstaged_content &&
8355 unlink(*path_unstaged_content) == -1 && err == NULL)
8356 err = got_error_from_errno2("unlink",
8357 *path_unstaged_content);
8358 free(*path_unstaged_content);
8359 *path_unstaged_content = NULL;
8361 if (err || !have_content || !have_rejected_content) {
8362 if (*path_new_staged_content &&
8363 unlink(*path_new_staged_content) == -1 && err == NULL)
8364 err = got_error_from_errno2("unlink",
8365 *path_new_staged_content);
8366 free(*path_new_staged_content);
8367 *path_new_staged_content = NULL;
8369 free(path1);
8370 free(path2);
8371 return err;
8374 static const struct got_error *
8375 unstage_hunks(struct got_object_id *staged_blob_id,
8376 struct got_blob_object *blob_base,
8377 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8378 const char *ondisk_path, const char *label_orig,
8379 struct got_worktree *worktree, struct got_repository *repo,
8380 got_worktree_patch_cb patch_cb, void *patch_arg,
8381 got_worktree_checkout_cb progress_cb, void *progress_arg)
8383 const struct got_error *err = NULL;
8384 char *path_unstaged_content = NULL;
8385 char *path_new_staged_content = NULL;
8386 char *parent = NULL, *base_path = NULL;
8387 char *blob_base_path = NULL;
8388 struct got_object_id *new_staged_blob_id = NULL;
8389 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8390 struct stat sb;
8392 err = create_unstaged_content(&path_unstaged_content,
8393 &path_new_staged_content, blob_id, staged_blob_id,
8394 ie->path, repo, patch_cb, patch_arg);
8395 if (err)
8396 return err;
8398 if (path_unstaged_content == NULL)
8399 return NULL;
8401 if (path_new_staged_content) {
8402 err = got_object_blob_create(&new_staged_blob_id,
8403 path_new_staged_content, repo);
8404 if (err)
8405 goto done;
8408 f = fopen(path_unstaged_content, "r");
8409 if (f == NULL) {
8410 err = got_error_from_errno2("fopen",
8411 path_unstaged_content);
8412 goto done;
8414 if (fstat(fileno(f), &sb) == -1) {
8415 err = got_error_from_errno2("fstat", path_unstaged_content);
8416 goto done;
8418 if (got_fileindex_entry_staged_filetype_get(ie) ==
8419 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8420 char link_target[PATH_MAX];
8421 size_t r;
8422 r = fread(link_target, 1, sizeof(link_target), f);
8423 if (r == 0 && ferror(f)) {
8424 err = got_error_from_errno("fread");
8425 goto done;
8427 if (r >= sizeof(link_target)) { /* should not happen */
8428 err = got_error(GOT_ERR_NO_SPACE);
8429 goto done;
8431 link_target[r] = '\0';
8432 err = merge_symlink(worktree, blob_base,
8433 ondisk_path, ie->path, label_orig, link_target,
8434 worktree->base_commit_id, repo, progress_cb,
8435 progress_arg);
8436 } else {
8437 int local_changes_subsumed;
8439 err = got_path_dirname(&parent, ondisk_path);
8440 if (err)
8441 return err;
8443 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8444 parent) == -1) {
8445 err = got_error_from_errno("asprintf");
8446 base_path = NULL;
8447 goto done;
8450 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8451 if (err)
8452 goto done;
8453 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8454 blob_base);
8455 if (err)
8456 goto done;
8459 * In order the run a 3-way merge with a symlink we copy the symlink's
8460 * target path into a temporary file and use that file with diff3.
8462 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8463 err = dump_symlink_target_path_to_file(&f_deriv2,
8464 ondisk_path);
8465 if (err)
8466 goto done;
8467 } else {
8468 int fd;
8469 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
8470 if (fd == -1) {
8471 err = got_error_from_errno2("open", ondisk_path);
8472 goto done;
8474 f_deriv2 = fdopen(fd, "r");
8475 if (f_deriv2 == NULL) {
8476 err = got_error_from_errno2("fdopen", ondisk_path);
8477 close(fd);
8478 goto done;
8482 err = merge_file(&local_changes_subsumed, worktree,
8483 f_base, f, f_deriv2, ondisk_path, ie->path,
8484 got_fileindex_perms_to_st(ie),
8485 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8486 repo, progress_cb, progress_arg);
8488 if (err)
8489 goto done;
8491 if (new_staged_blob_id) {
8492 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8493 SHA1_DIGEST_LENGTH);
8494 } else {
8495 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8496 got_fileindex_entry_staged_filetype_set(ie, 0);
8498 done:
8499 free(new_staged_blob_id);
8500 if (path_unstaged_content &&
8501 unlink(path_unstaged_content) == -1 && err == NULL)
8502 err = got_error_from_errno2("unlink", path_unstaged_content);
8503 if (path_new_staged_content &&
8504 unlink(path_new_staged_content) == -1 && err == NULL)
8505 err = got_error_from_errno2("unlink", path_new_staged_content);
8506 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8507 err = got_error_from_errno2("unlink", blob_base_path);
8508 if (f_base && fclose(f_base) == EOF && err == NULL)
8509 err = got_error_from_errno2("fclose", path_unstaged_content);
8510 if (f && fclose(f) == EOF && err == NULL)
8511 err = got_error_from_errno2("fclose", path_unstaged_content);
8512 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8513 err = got_error_from_errno2("fclose", ondisk_path);
8514 free(path_unstaged_content);
8515 free(path_new_staged_content);
8516 free(blob_base_path);
8517 free(parent);
8518 free(base_path);
8519 return err;
8522 static const struct got_error *
8523 unstage_path(void *arg, unsigned char status,
8524 unsigned char staged_status, const char *relpath,
8525 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8526 struct got_object_id *commit_id, int dirfd, const char *de_name)
8528 const struct got_error *err = NULL;
8529 struct unstage_path_arg *a = arg;
8530 struct got_fileindex_entry *ie;
8531 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8532 char *ondisk_path = NULL;
8533 char *id_str = NULL, *label_orig = NULL;
8534 int local_changes_subsumed;
8535 struct stat sb;
8537 if (staged_status != GOT_STATUS_ADD &&
8538 staged_status != GOT_STATUS_MODIFY &&
8539 staged_status != GOT_STATUS_DELETE)
8540 return NULL;
8542 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8543 if (ie == NULL)
8544 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8546 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8547 == -1)
8548 return got_error_from_errno("asprintf");
8550 err = got_object_id_str(&id_str,
8551 commit_id ? commit_id : a->worktree->base_commit_id);
8552 if (err)
8553 goto done;
8554 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8555 id_str) == -1) {
8556 err = got_error_from_errno("asprintf");
8557 goto done;
8560 switch (staged_status) {
8561 case GOT_STATUS_MODIFY:
8562 err = got_object_open_as_blob(&blob_base, a->repo,
8563 blob_id, 8192);
8564 if (err)
8565 break;
8566 /* fall through */
8567 case GOT_STATUS_ADD:
8568 if (a->patch_cb) {
8569 if (staged_status == GOT_STATUS_ADD) {
8570 int choice = GOT_PATCH_CHOICE_NONE;
8571 err = (*a->patch_cb)(&choice, a->patch_arg,
8572 staged_status, ie->path, NULL, 1, 1);
8573 if (err)
8574 break;
8575 if (choice != GOT_PATCH_CHOICE_YES)
8576 break;
8577 } else {
8578 err = unstage_hunks(staged_blob_id,
8579 blob_base, blob_id, ie, ondisk_path,
8580 label_orig, a->worktree, a->repo,
8581 a->patch_cb, a->patch_arg,
8582 a->progress_cb, a->progress_arg);
8583 break; /* Done with this file. */
8586 err = got_object_open_as_blob(&blob_staged, a->repo,
8587 staged_blob_id, 8192);
8588 if (err)
8589 break;
8590 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8591 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8592 case GOT_FILEIDX_MODE_REGULAR_FILE:
8593 err = merge_blob(&local_changes_subsumed, a->worktree,
8594 blob_base, ondisk_path, relpath,
8595 got_fileindex_perms_to_st(ie), label_orig,
8596 blob_staged, commit_id ? commit_id :
8597 a->worktree->base_commit_id, a->repo,
8598 a->progress_cb, a->progress_arg);
8599 break;
8600 case GOT_FILEIDX_MODE_SYMLINK:
8601 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8602 char *staged_target;
8603 err = got_object_blob_read_to_str(
8604 &staged_target, blob_staged);
8605 if (err)
8606 goto done;
8607 err = merge_symlink(a->worktree, blob_base,
8608 ondisk_path, relpath, label_orig,
8609 staged_target, commit_id ? commit_id :
8610 a->worktree->base_commit_id,
8611 a->repo, a->progress_cb, a->progress_arg);
8612 free(staged_target);
8613 } else {
8614 err = merge_blob(&local_changes_subsumed,
8615 a->worktree, blob_base, ondisk_path,
8616 relpath, got_fileindex_perms_to_st(ie),
8617 label_orig, blob_staged,
8618 commit_id ? commit_id :
8619 a->worktree->base_commit_id, a->repo,
8620 a->progress_cb, a->progress_arg);
8622 break;
8623 default:
8624 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8625 break;
8627 if (err == NULL) {
8628 got_fileindex_entry_stage_set(ie,
8629 GOT_FILEIDX_STAGE_NONE);
8630 got_fileindex_entry_staged_filetype_set(ie, 0);
8632 break;
8633 case GOT_STATUS_DELETE:
8634 if (a->patch_cb) {
8635 int choice = GOT_PATCH_CHOICE_NONE;
8636 err = (*a->patch_cb)(&choice, a->patch_arg,
8637 staged_status, ie->path, NULL, 1, 1);
8638 if (err)
8639 break;
8640 if (choice == GOT_PATCH_CHOICE_NO)
8641 break;
8642 if (choice != GOT_PATCH_CHOICE_YES) {
8643 err = got_error(GOT_ERR_PATCH_CHOICE);
8644 break;
8647 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8648 got_fileindex_entry_staged_filetype_set(ie, 0);
8649 err = get_file_status(&status, &sb, ie, ondisk_path,
8650 dirfd, de_name, a->repo);
8651 if (err)
8652 break;
8653 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8654 break;
8656 done:
8657 free(ondisk_path);
8658 if (blob_base)
8659 got_object_blob_close(blob_base);
8660 if (blob_staged)
8661 got_object_blob_close(blob_staged);
8662 free(id_str);
8663 free(label_orig);
8664 return err;
8667 const struct got_error *
8668 got_worktree_unstage(struct got_worktree *worktree,
8669 struct got_pathlist_head *paths,
8670 got_worktree_checkout_cb progress_cb, void *progress_arg,
8671 got_worktree_patch_cb patch_cb, void *patch_arg,
8672 struct got_repository *repo)
8674 const struct got_error *err = NULL, *sync_err, *unlockerr;
8675 struct got_pathlist_entry *pe;
8676 struct got_fileindex *fileindex = NULL;
8677 char *fileindex_path = NULL;
8678 struct unstage_path_arg upa;
8680 err = lock_worktree(worktree, LOCK_EX);
8681 if (err)
8682 return err;
8684 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8685 if (err)
8686 goto done;
8688 upa.worktree = worktree;
8689 upa.fileindex = fileindex;
8690 upa.repo = repo;
8691 upa.progress_cb = progress_cb;
8692 upa.progress_arg = progress_arg;
8693 upa.patch_cb = patch_cb;
8694 upa.patch_arg = patch_arg;
8695 TAILQ_FOREACH(pe, paths, entry) {
8696 err = worktree_status(worktree, pe->path, fileindex, repo,
8697 unstage_path, &upa, NULL, NULL, 0, 0);
8698 if (err)
8699 goto done;
8702 sync_err = sync_fileindex(fileindex, fileindex_path);
8703 if (sync_err && err == NULL)
8704 err = sync_err;
8705 done:
8706 free(fileindex_path);
8707 if (fileindex)
8708 got_fileindex_free(fileindex);
8709 unlockerr = lock_worktree(worktree, LOCK_SH);
8710 if (unlockerr && err == NULL)
8711 err = unlockerr;
8712 return err;
8715 struct report_file_info_arg {
8716 struct got_worktree *worktree;
8717 got_worktree_path_info_cb info_cb;
8718 void *info_arg;
8719 struct got_pathlist_head *paths;
8720 got_cancel_cb cancel_cb;
8721 void *cancel_arg;
8724 static const struct got_error *
8725 report_file_info(void *arg, struct got_fileindex_entry *ie)
8727 struct report_file_info_arg *a = arg;
8728 struct got_pathlist_entry *pe;
8729 struct got_object_id blob_id, staged_blob_id, commit_id;
8730 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8731 struct got_object_id *commit_idp = NULL;
8732 int stage;
8734 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8735 return got_error(GOT_ERR_CANCELLED);
8737 TAILQ_FOREACH(pe, a->paths, entry) {
8738 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8739 got_path_is_child(ie->path, pe->path, pe->path_len))
8740 break;
8742 if (pe == NULL) /* not found */
8743 return NULL;
8745 if (got_fileindex_entry_has_blob(ie)) {
8746 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8747 blob_idp = &blob_id;
8749 stage = got_fileindex_entry_stage_get(ie);
8750 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8751 stage == GOT_FILEIDX_STAGE_ADD) {
8752 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8753 SHA1_DIGEST_LENGTH);
8754 staged_blob_idp = &staged_blob_id;
8757 if (got_fileindex_entry_has_commit(ie)) {
8758 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8759 commit_idp = &commit_id;
8762 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8763 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8766 const struct got_error *
8767 got_worktree_path_info(struct got_worktree *worktree,
8768 struct got_pathlist_head *paths,
8769 got_worktree_path_info_cb info_cb, void *info_arg,
8770 got_cancel_cb cancel_cb, void *cancel_arg)
8773 const struct got_error *err = NULL, *unlockerr;
8774 struct got_fileindex *fileindex = NULL;
8775 char *fileindex_path = NULL;
8776 struct report_file_info_arg arg;
8778 err = lock_worktree(worktree, LOCK_SH);
8779 if (err)
8780 return err;
8782 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8783 if (err)
8784 goto done;
8786 arg.worktree = worktree;
8787 arg.info_cb = info_cb;
8788 arg.info_arg = info_arg;
8789 arg.paths = paths;
8790 arg.cancel_cb = cancel_cb;
8791 arg.cancel_arg = cancel_arg;
8792 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8793 &arg);
8794 done:
8795 free(fileindex_path);
8796 if (fileindex)
8797 got_fileindex_free(fileindex);
8798 unlockerr = lock_worktree(worktree, LOCK_UN);
8799 if (unlockerr && err == NULL)
8800 err = unlockerr;
8801 return err;