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(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3621 int dirfd)
3623 const struct got_error *err = NULL;
3624 struct diff_dir_cb_arg *a = arg;
3625 char *path = NULL;
3627 if (ignore != NULL)
3628 *ignore = 0;
3630 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3631 return got_error(GOT_ERR_CANCELLED);
3633 if (parent_path[0]) {
3634 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3635 return got_error_from_errno("asprintf");
3636 } else {
3637 path = de->d_name;
3640 if (de->d_type == DT_DIR) {
3641 if (!a->no_ignores && ignore != NULL &&
3642 match_ignores(a->ignores, path))
3643 *ignore = 1;
3644 } else if (!match_ignores(a->ignores, path) &&
3645 got_path_is_child(path, a->status_path, a->status_path_len))
3646 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3647 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3648 if (parent_path[0])
3649 free(path);
3650 return err;
3653 static const struct got_error *
3654 status_traverse(void *arg, const char *path, int dirfd)
3656 const struct got_error *err = NULL;
3657 struct diff_dir_cb_arg *a = arg;
3659 if (a->no_ignores)
3660 return NULL;
3662 err = add_ignores(a->ignores, a->worktree->root_path,
3663 path, dirfd, ".cvsignore");
3664 if (err)
3665 return err;
3667 err = add_ignores(a->ignores, a->worktree->root_path, path,
3668 dirfd, ".gitignore");
3670 return err;
3673 static const struct got_error *
3674 report_single_file_status(const char *path, const char *ondisk_path,
3675 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3676 void *status_arg, struct got_repository *repo, int report_unchanged,
3677 struct got_pathlist_head *ignores, int no_ignores)
3679 struct got_fileindex_entry *ie;
3680 struct stat sb;
3682 if (!no_ignores && match_ignores(ignores, path))
3683 return NULL;
3685 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3686 if (ie)
3687 return report_file_status(ie, ondisk_path, -1, NULL,
3688 status_cb, status_arg, repo, report_unchanged);
3690 if (lstat(ondisk_path, &sb) == -1) {
3691 if (errno != ENOENT)
3692 return got_error_from_errno2("lstat", ondisk_path);
3693 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3694 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3695 return NULL;
3698 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3699 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3700 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3702 return NULL;
3705 static const struct got_error *
3706 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3707 const char *root_path, const char *path)
3709 const struct got_error *err;
3710 char *parent_path, *next_parent_path = NULL;
3712 err = add_ignores(ignores, root_path, "", -1,
3713 ".cvsignore");
3714 if (err)
3715 return err;
3717 err = add_ignores(ignores, root_path, "", -1,
3718 ".gitignore");
3719 if (err)
3720 return err;
3722 err = got_path_dirname(&parent_path, path);
3723 if (err) {
3724 if (err->code == GOT_ERR_BAD_PATH)
3725 return NULL; /* cannot traverse parent */
3726 return err;
3728 for (;;) {
3729 err = add_ignores(ignores, root_path, parent_path, -1,
3730 ".cvsignore");
3731 if (err)
3732 break;
3733 err = add_ignores(ignores, root_path, parent_path, -1,
3734 ".gitignore");
3735 if (err)
3736 break;
3737 err = got_path_dirname(&next_parent_path, parent_path);
3738 if (err) {
3739 if (err->code == GOT_ERR_BAD_PATH)
3740 err = NULL; /* traversed everything */
3741 break;
3743 if (got_path_is_root_dir(parent_path))
3744 break;
3745 free(parent_path);
3746 parent_path = next_parent_path;
3747 next_parent_path = NULL;
3750 free(parent_path);
3751 free(next_parent_path);
3752 return err;
3755 static const struct got_error *
3756 worktree_status(struct got_worktree *worktree, const char *path,
3757 struct got_fileindex *fileindex, struct got_repository *repo,
3758 got_worktree_status_cb status_cb, void *status_arg,
3759 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3760 int report_unchanged)
3762 const struct got_error *err = NULL;
3763 int fd = -1;
3764 struct got_fileindex_diff_dir_cb fdiff_cb;
3765 struct diff_dir_cb_arg arg;
3766 char *ondisk_path = NULL;
3767 struct got_pathlist_head ignores;
3769 TAILQ_INIT(&ignores);
3771 if (asprintf(&ondisk_path, "%s%s%s",
3772 worktree->root_path, path[0] ? "/" : "", path) == -1)
3773 return got_error_from_errno("asprintf");
3775 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3776 if (fd == -1) {
3777 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3778 !got_err_open_nofollow_on_symlink())
3779 err = got_error_from_errno2("open", ondisk_path);
3780 else {
3781 if (!no_ignores) {
3782 err = add_ignores_from_parent_paths(&ignores,
3783 worktree->root_path, ondisk_path);
3784 if (err)
3785 goto done;
3787 err = report_single_file_status(path, ondisk_path,
3788 fileindex, status_cb, status_arg, repo,
3789 report_unchanged, &ignores, no_ignores);
3791 } else {
3792 fdiff_cb.diff_old_new = status_old_new;
3793 fdiff_cb.diff_old = status_old;
3794 fdiff_cb.diff_new = status_new;
3795 fdiff_cb.diff_traverse = status_traverse;
3796 arg.fileindex = fileindex;
3797 arg.worktree = worktree;
3798 arg.status_path = path;
3799 arg.status_path_len = strlen(path);
3800 arg.repo = repo;
3801 arg.status_cb = status_cb;
3802 arg.status_arg = status_arg;
3803 arg.cancel_cb = cancel_cb;
3804 arg.cancel_arg = cancel_arg;
3805 arg.report_unchanged = report_unchanged;
3806 arg.no_ignores = no_ignores;
3807 if (!no_ignores) {
3808 err = add_ignores_from_parent_paths(&ignores,
3809 worktree->root_path, path);
3810 if (err)
3811 goto done;
3813 arg.ignores = &ignores;
3814 err = got_fileindex_diff_dir(fileindex, fd,
3815 worktree->root_path, path, repo, &fdiff_cb, &arg);
3817 done:
3818 free_ignores(&ignores);
3819 if (fd != -1 && close(fd) == -1 && err == NULL)
3820 err = got_error_from_errno("close");
3821 free(ondisk_path);
3822 return err;
3825 const struct got_error *
3826 got_worktree_status(struct got_worktree *worktree,
3827 struct got_pathlist_head *paths, struct got_repository *repo,
3828 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3829 got_cancel_cb cancel_cb, void *cancel_arg)
3831 const struct got_error *err = NULL;
3832 char *fileindex_path = NULL;
3833 struct got_fileindex *fileindex = NULL;
3834 struct got_pathlist_entry *pe;
3836 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3837 if (err)
3838 return err;
3840 TAILQ_FOREACH(pe, paths, entry) {
3841 err = worktree_status(worktree, pe->path, fileindex, repo,
3842 status_cb, status_arg, cancel_cb, cancel_arg,
3843 no_ignores, 0);
3844 if (err)
3845 break;
3847 free(fileindex_path);
3848 got_fileindex_free(fileindex);
3849 return err;
3852 const struct got_error *
3853 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3854 const char *arg)
3856 const struct got_error *err = NULL;
3857 char *resolved = NULL, *cwd = NULL, *path = NULL;
3858 size_t len;
3859 struct stat sb;
3860 char *abspath = NULL;
3861 char canonpath[PATH_MAX];
3863 *wt_path = NULL;
3865 cwd = getcwd(NULL, 0);
3866 if (cwd == NULL)
3867 return got_error_from_errno("getcwd");
3869 if (lstat(arg, &sb) == -1) {
3870 if (errno != ENOENT) {
3871 err = got_error_from_errno2("lstat", arg);
3872 goto done;
3874 sb.st_mode = 0;
3876 if (S_ISLNK(sb.st_mode)) {
3878 * We cannot use realpath(3) with symlinks since we want to
3879 * operate on the symlink itself.
3880 * But we can make the path absolute, assuming it is relative
3881 * to the current working directory, and then canonicalize it.
3883 if (!got_path_is_absolute(arg)) {
3884 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3885 err = got_error_from_errno("asprintf");
3886 goto done;
3890 err = got_canonpath(abspath ? abspath : arg, canonpath,
3891 sizeof(canonpath));
3892 if (err)
3893 goto done;
3894 resolved = strdup(canonpath);
3895 if (resolved == NULL) {
3896 err = got_error_from_errno("strdup");
3897 goto done;
3899 } else {
3900 resolved = realpath(arg, NULL);
3901 if (resolved == NULL) {
3902 if (errno != ENOENT) {
3903 err = got_error_from_errno2("realpath", arg);
3904 goto done;
3906 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3907 err = got_error_from_errno("asprintf");
3908 goto done;
3910 err = got_canonpath(abspath, canonpath,
3911 sizeof(canonpath));
3912 if (err)
3913 goto done;
3914 resolved = strdup(canonpath);
3915 if (resolved == NULL) {
3916 err = got_error_from_errno("strdup");
3917 goto done;
3922 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3923 strlen(got_worktree_get_root_path(worktree)))) {
3924 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3925 goto done;
3928 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3929 err = got_path_skip_common_ancestor(&path,
3930 got_worktree_get_root_path(worktree), resolved);
3931 if (err)
3932 goto done;
3933 } else {
3934 path = strdup("");
3935 if (path == NULL) {
3936 err = got_error_from_errno("strdup");
3937 goto done;
3941 /* XXX status walk can't deal with trailing slash! */
3942 len = strlen(path);
3943 while (len > 0 && path[len - 1] == '/') {
3944 path[len - 1] = '\0';
3945 len--;
3947 done:
3948 free(abspath);
3949 free(resolved);
3950 free(cwd);
3951 if (err == NULL)
3952 *wt_path = path;
3953 else
3954 free(path);
3955 return err;
3958 struct schedule_addition_args {
3959 struct got_worktree *worktree;
3960 struct got_fileindex *fileindex;
3961 got_worktree_checkout_cb progress_cb;
3962 void *progress_arg;
3963 struct got_repository *repo;
3966 static const struct got_error *
3967 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3968 const char *relpath, struct got_object_id *blob_id,
3969 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3970 int dirfd, const char *de_name)
3972 struct schedule_addition_args *a = arg;
3973 const struct got_error *err = NULL;
3974 struct got_fileindex_entry *ie;
3975 struct stat sb;
3976 char *ondisk_path;
3978 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3979 relpath) == -1)
3980 return got_error_from_errno("asprintf");
3982 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3983 if (ie) {
3984 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3985 de_name, a->repo);
3986 if (err)
3987 goto done;
3988 /* Re-adding an existing entry is a no-op. */
3989 if (status == GOT_STATUS_ADD)
3990 goto done;
3991 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3992 if (err)
3993 goto done;
3996 if (status != GOT_STATUS_UNVERSIONED) {
3997 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3998 goto done;
4001 err = got_fileindex_entry_alloc(&ie, relpath);
4002 if (err)
4003 goto done;
4004 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4005 relpath, NULL, NULL, 1);
4006 if (err) {
4007 got_fileindex_entry_free(ie);
4008 goto done;
4010 err = got_fileindex_entry_add(a->fileindex, ie);
4011 if (err) {
4012 got_fileindex_entry_free(ie);
4013 goto done;
4015 done:
4016 free(ondisk_path);
4017 if (err)
4018 return err;
4019 if (status == GOT_STATUS_ADD)
4020 return NULL;
4021 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4024 const struct got_error *
4025 got_worktree_schedule_add(struct got_worktree *worktree,
4026 struct got_pathlist_head *paths,
4027 got_worktree_checkout_cb progress_cb, void *progress_arg,
4028 struct got_repository *repo, int no_ignores)
4030 struct got_fileindex *fileindex = NULL;
4031 char *fileindex_path = NULL;
4032 const struct got_error *err = NULL, *sync_err, *unlockerr;
4033 struct got_pathlist_entry *pe;
4034 struct schedule_addition_args saa;
4036 err = lock_worktree(worktree, LOCK_EX);
4037 if (err)
4038 return err;
4040 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4041 if (err)
4042 goto done;
4044 saa.worktree = worktree;
4045 saa.fileindex = fileindex;
4046 saa.progress_cb = progress_cb;
4047 saa.progress_arg = progress_arg;
4048 saa.repo = repo;
4050 TAILQ_FOREACH(pe, paths, entry) {
4051 err = worktree_status(worktree, pe->path, fileindex, repo,
4052 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4053 if (err)
4054 break;
4056 sync_err = sync_fileindex(fileindex, fileindex_path);
4057 if (sync_err && err == NULL)
4058 err = sync_err;
4059 done:
4060 free(fileindex_path);
4061 if (fileindex)
4062 got_fileindex_free(fileindex);
4063 unlockerr = lock_worktree(worktree, LOCK_SH);
4064 if (unlockerr && err == NULL)
4065 err = unlockerr;
4066 return err;
4069 struct schedule_deletion_args {
4070 struct got_worktree *worktree;
4071 struct got_fileindex *fileindex;
4072 got_worktree_delete_cb progress_cb;
4073 void *progress_arg;
4074 struct got_repository *repo;
4075 int delete_local_mods;
4076 int keep_on_disk;
4077 const char *status_codes;
4080 static const struct got_error *
4081 schedule_for_deletion(void *arg, unsigned char status,
4082 unsigned char staged_status, const char *relpath,
4083 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4084 struct got_object_id *commit_id, int dirfd, const char *de_name)
4086 struct schedule_deletion_args *a = arg;
4087 const struct got_error *err = NULL;
4088 struct got_fileindex_entry *ie = NULL;
4089 struct stat sb;
4090 char *ondisk_path;
4092 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4093 if (ie == NULL)
4094 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4096 staged_status = get_staged_status(ie);
4097 if (staged_status != GOT_STATUS_NO_CHANGE) {
4098 if (staged_status == GOT_STATUS_DELETE)
4099 return NULL;
4100 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4103 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4104 relpath) == -1)
4105 return got_error_from_errno("asprintf");
4107 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4108 a->repo);
4109 if (err)
4110 goto done;
4112 if (a->status_codes) {
4113 size_t ncodes = strlen(a->status_codes);
4114 int i;
4115 for (i = 0; i < ncodes ; i++) {
4116 if (status == a->status_codes[i])
4117 break;
4119 if (i == ncodes) {
4120 /* Do not delete files in non-matching status. */
4121 free(ondisk_path);
4122 return NULL;
4124 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4125 a->status_codes[i] != GOT_STATUS_MISSING) {
4126 static char msg[64];
4127 snprintf(msg, sizeof(msg),
4128 "invalid status code '%c'", a->status_codes[i]);
4129 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4130 goto done;
4134 if (status != GOT_STATUS_NO_CHANGE) {
4135 if (status == GOT_STATUS_DELETE)
4136 goto done;
4137 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4138 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4139 goto done;
4141 if (status != GOT_STATUS_MODIFY &&
4142 status != GOT_STATUS_MISSING) {
4143 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4144 goto done;
4148 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4149 size_t root_len;
4151 if (dirfd != -1) {
4152 if (unlinkat(dirfd, de_name, 0) != 0) {
4153 err = got_error_from_errno2("unlinkat",
4154 ondisk_path);
4155 goto done;
4157 } else if (unlink(ondisk_path) != 0) {
4158 err = got_error_from_errno2("unlink", ondisk_path);
4159 goto done;
4162 root_len = strlen(a->worktree->root_path);
4163 do {
4164 char *parent;
4165 err = got_path_dirname(&parent, ondisk_path);
4166 if (err)
4167 goto done;
4168 free(ondisk_path);
4169 ondisk_path = parent;
4170 if (rmdir(ondisk_path) == -1) {
4171 if (errno != ENOTEMPTY)
4172 err = got_error_from_errno2("rmdir",
4173 ondisk_path);
4174 break;
4176 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4177 strlen(ondisk_path), root_len) != 0);
4180 got_fileindex_entry_mark_deleted_from_disk(ie);
4181 done:
4182 free(ondisk_path);
4183 if (err)
4184 return err;
4185 if (status == GOT_STATUS_DELETE)
4186 return NULL;
4187 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4188 staged_status, relpath);
4191 const struct got_error *
4192 got_worktree_schedule_delete(struct got_worktree *worktree,
4193 struct got_pathlist_head *paths, int delete_local_mods,
4194 const char *status_codes,
4195 got_worktree_delete_cb progress_cb, void *progress_arg,
4196 struct got_repository *repo, int keep_on_disk)
4198 struct got_fileindex *fileindex = NULL;
4199 char *fileindex_path = NULL;
4200 const struct got_error *err = NULL, *sync_err, *unlockerr;
4201 struct got_pathlist_entry *pe;
4202 struct schedule_deletion_args sda;
4204 err = lock_worktree(worktree, LOCK_EX);
4205 if (err)
4206 return err;
4208 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4209 if (err)
4210 goto done;
4212 sda.worktree = worktree;
4213 sda.fileindex = fileindex;
4214 sda.progress_cb = progress_cb;
4215 sda.progress_arg = progress_arg;
4216 sda.repo = repo;
4217 sda.delete_local_mods = delete_local_mods;
4218 sda.keep_on_disk = keep_on_disk;
4219 sda.status_codes = status_codes;
4221 TAILQ_FOREACH(pe, paths, entry) {
4222 err = worktree_status(worktree, pe->path, fileindex, repo,
4223 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4224 if (err)
4225 break;
4227 sync_err = sync_fileindex(fileindex, fileindex_path);
4228 if (sync_err && err == NULL)
4229 err = sync_err;
4230 done:
4231 free(fileindex_path);
4232 if (fileindex)
4233 got_fileindex_free(fileindex);
4234 unlockerr = lock_worktree(worktree, LOCK_SH);
4235 if (unlockerr && err == NULL)
4236 err = unlockerr;
4237 return err;
4240 static const struct got_error *
4241 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4243 const struct got_error *err = NULL;
4244 char *line = NULL;
4245 size_t linesize = 0, n;
4246 ssize_t linelen;
4248 linelen = getline(&line, &linesize, infile);
4249 if (linelen == -1) {
4250 if (ferror(infile)) {
4251 err = got_error_from_errno("getline");
4252 goto done;
4254 return NULL;
4256 if (outfile) {
4257 n = fwrite(line, 1, linelen, outfile);
4258 if (n != linelen) {
4259 err = got_ferror(outfile, GOT_ERR_IO);
4260 goto done;
4263 if (rejectfile) {
4264 n = fwrite(line, 1, linelen, rejectfile);
4265 if (n != linelen)
4266 err = got_ferror(outfile, GOT_ERR_IO);
4268 done:
4269 free(line);
4270 return err;
4273 static const struct got_error *
4274 skip_one_line(FILE *f)
4276 char *line = NULL;
4277 size_t linesize = 0;
4278 ssize_t linelen;
4280 linelen = getline(&line, &linesize, f);
4281 if (linelen == -1) {
4282 if (ferror(f))
4283 return got_error_from_errno("getline");
4284 return NULL;
4286 free(line);
4287 return NULL;
4290 static const struct got_error *
4291 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4292 int start_old, int end_old, int start_new, int end_new,
4293 FILE *outfile, FILE *rejectfile)
4295 const struct got_error *err;
4297 /* Copy old file's lines leading up to patch. */
4298 while (!feof(f1) && *line_cur1 < start_old) {
4299 err = copy_one_line(f1, outfile, NULL);
4300 if (err)
4301 return err;
4302 (*line_cur1)++;
4304 /* Skip new file's lines leading up to patch. */
4305 while (!feof(f2) && *line_cur2 < start_new) {
4306 if (rejectfile)
4307 err = copy_one_line(f2, NULL, rejectfile);
4308 else
4309 err = skip_one_line(f2);
4310 if (err)
4311 return err;
4312 (*line_cur2)++;
4314 /* Copy patched lines. */
4315 while (!feof(f2) && *line_cur2 <= end_new) {
4316 err = copy_one_line(f2, outfile, NULL);
4317 if (err)
4318 return err;
4319 (*line_cur2)++;
4321 /* Skip over old file's replaced lines. */
4322 while (!feof(f1) && *line_cur1 <= end_old) {
4323 if (rejectfile)
4324 err = copy_one_line(f1, NULL, rejectfile);
4325 else
4326 err = skip_one_line(f1);
4327 if (err)
4328 return err;
4329 (*line_cur1)++;
4332 return NULL;
4335 static const struct got_error *
4336 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4337 FILE *outfile, FILE *rejectfile)
4339 const struct got_error *err;
4341 if (outfile) {
4342 /* Copy old file's lines until EOF. */
4343 while (!feof(f1)) {
4344 err = copy_one_line(f1, outfile, NULL);
4345 if (err)
4346 return err;
4347 (*line_cur1)++;
4350 if (rejectfile) {
4351 /* Copy new file's lines until EOF. */
4352 while (!feof(f2)) {
4353 err = copy_one_line(f2, NULL, rejectfile);
4354 if (err)
4355 return err;
4356 (*line_cur2)++;
4360 return NULL;
4363 static const struct got_error *
4364 apply_or_reject_change(int *choice, int *nchunks_used,
4365 struct diff_result *diff_result, int n,
4366 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4367 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4368 got_worktree_patch_cb patch_cb, void *patch_arg)
4370 const struct got_error *err = NULL;
4371 struct diff_chunk_context cc = {};
4372 int start_old, end_old, start_new, end_new;
4373 FILE *hunkfile;
4374 struct diff_output_unidiff_state *diff_state;
4375 struct diff_input_info diff_info;
4376 int rc;
4378 *choice = GOT_PATCH_CHOICE_NONE;
4380 /* Get changed line numbers without context lines for copy_change(). */
4381 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4382 start_old = cc.left.start;
4383 end_old = cc.left.end;
4384 start_new = cc.right.start;
4385 end_new = cc.right.end;
4387 /* Get the same change with context lines for display. */
4388 memset(&cc, 0, sizeof(cc));
4389 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4391 memset(&diff_info, 0, sizeof(diff_info));
4392 diff_info.left_path = relpath;
4393 diff_info.right_path = relpath;
4395 diff_state = diff_output_unidiff_state_alloc();
4396 if (diff_state == NULL)
4397 return got_error_set_errno(ENOMEM,
4398 "diff_output_unidiff_state_alloc");
4400 hunkfile = got_opentemp();
4401 if (hunkfile == NULL) {
4402 err = got_error_from_errno("got_opentemp");
4403 goto done;
4406 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4407 diff_result, &cc);
4408 if (rc != DIFF_RC_OK) {
4409 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4410 goto done;
4413 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4414 err = got_ferror(hunkfile, GOT_ERR_IO);
4415 goto done;
4418 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4419 hunkfile, changeno, nchanges);
4420 if (err)
4421 goto done;
4423 switch (*choice) {
4424 case GOT_PATCH_CHOICE_YES:
4425 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4426 end_old, start_new, end_new, outfile, rejectfile);
4427 break;
4428 case GOT_PATCH_CHOICE_NO:
4429 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4430 end_old, start_new, end_new, rejectfile, outfile);
4431 break;
4432 case GOT_PATCH_CHOICE_QUIT:
4433 break;
4434 default:
4435 err = got_error(GOT_ERR_PATCH_CHOICE);
4436 break;
4438 done:
4439 diff_output_unidiff_state_free(diff_state);
4440 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4441 err = got_error_from_errno("fclose");
4442 return err;
4445 struct revert_file_args {
4446 struct got_worktree *worktree;
4447 struct got_fileindex *fileindex;
4448 got_worktree_checkout_cb progress_cb;
4449 void *progress_arg;
4450 got_worktree_patch_cb patch_cb;
4451 void *patch_arg;
4452 struct got_repository *repo;
4453 int unlink_added_files;
4456 static const struct got_error *
4457 create_patched_content(char **path_outfile, int reverse_patch,
4458 struct got_object_id *blob_id, const char *path2,
4459 int dirfd2, const char *de_name2,
4460 const char *relpath, struct got_repository *repo,
4461 got_worktree_patch_cb patch_cb, void *patch_arg)
4463 const struct got_error *err, *free_err;
4464 struct got_blob_object *blob = NULL;
4465 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4466 int fd2 = -1;
4467 char link_target[PATH_MAX];
4468 ssize_t link_len = 0;
4469 char *path1 = NULL, *id_str = NULL;
4470 struct stat sb2;
4471 struct got_diffreg_result *diffreg_result = NULL;
4472 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4473 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4475 *path_outfile = NULL;
4477 err = got_object_id_str(&id_str, blob_id);
4478 if (err)
4479 return err;
4481 if (dirfd2 != -1) {
4482 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4483 if (fd2 == -1) {
4484 if (!got_err_open_nofollow_on_symlink()) {
4485 err = got_error_from_errno2("openat", path2);
4486 goto done;
4488 link_len = readlinkat(dirfd2, de_name2,
4489 link_target, sizeof(link_target));
4490 if (link_len == -1)
4491 return got_error_from_errno2("readlinkat", path2);
4492 sb2.st_mode = S_IFLNK;
4493 sb2.st_size = link_len;
4495 } else {
4496 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4497 if (fd2 == -1) {
4498 if (!got_err_open_nofollow_on_symlink()) {
4499 err = got_error_from_errno2("open", path2);
4500 goto done;
4502 link_len = readlink(path2, link_target,
4503 sizeof(link_target));
4504 if (link_len == -1)
4505 return got_error_from_errno2("readlink", path2);
4506 sb2.st_mode = S_IFLNK;
4507 sb2.st_size = link_len;
4510 if (fd2 != -1) {
4511 if (fstat(fd2, &sb2) == -1) {
4512 err = got_error_from_errno2("fstat", path2);
4513 goto done;
4516 f2 = fdopen(fd2, "r");
4517 if (f2 == NULL) {
4518 err = got_error_from_errno2("fdopen", path2);
4519 goto done;
4521 fd2 = -1;
4522 } else {
4523 size_t n;
4524 f2 = got_opentemp();
4525 if (f2 == NULL) {
4526 err = got_error_from_errno2("got_opentemp", path2);
4527 goto done;
4529 n = fwrite(link_target, 1, link_len, f2);
4530 if (n != link_len) {
4531 err = got_ferror(f2, GOT_ERR_IO);
4532 goto done;
4534 if (fflush(f2) == EOF) {
4535 err = got_error_from_errno("fflush");
4536 goto done;
4538 rewind(f2);
4541 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4542 if (err)
4543 goto done;
4545 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4546 if (err)
4547 goto done;
4549 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4550 if (err)
4551 goto done;
4553 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4554 NULL);
4555 if (err)
4556 goto done;
4558 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4559 if (err)
4560 goto done;
4562 if (fseek(f1, 0L, SEEK_SET) == -1)
4563 return got_ferror(f1, GOT_ERR_IO);
4564 if (fseek(f2, 0L, SEEK_SET) == -1)
4565 return got_ferror(f2, GOT_ERR_IO);
4567 /* Count the number of actual changes in the diff result. */
4568 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4569 struct diff_chunk_context cc = {};
4570 diff_chunk_context_load_change(&cc, &nchunks_used,
4571 diffreg_result->result, n, 0);
4572 nchanges++;
4574 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4575 int choice;
4576 err = apply_or_reject_change(&choice, &nchunks_used,
4577 diffreg_result->result, n, relpath, f1, f2,
4578 &line_cur1, &line_cur2,
4579 reverse_patch ? NULL : outfile,
4580 reverse_patch ? outfile : NULL,
4581 ++i, nchanges, patch_cb, patch_arg);
4582 if (err)
4583 goto done;
4584 if (choice == GOT_PATCH_CHOICE_YES)
4585 have_content = 1;
4586 else if (choice == GOT_PATCH_CHOICE_QUIT)
4587 break;
4589 if (have_content) {
4590 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4591 reverse_patch ? NULL : outfile,
4592 reverse_patch ? outfile : NULL);
4593 if (err)
4594 goto done;
4596 if (!S_ISLNK(sb2.st_mode)) {
4597 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4598 err = got_error_from_errno2("fchmod", path2);
4599 goto done;
4603 done:
4604 free(id_str);
4605 if (blob)
4606 got_object_blob_close(blob);
4607 free_err = got_diffreg_result_free(diffreg_result);
4608 if (err == NULL)
4609 err = free_err;
4610 if (f1 && fclose(f1) == EOF && err == NULL)
4611 err = got_error_from_errno2("fclose", path1);
4612 if (f2 && fclose(f2) == EOF && err == NULL)
4613 err = got_error_from_errno2("fclose", path2);
4614 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4615 err = got_error_from_errno2("close", path2);
4616 if (outfile && fclose(outfile) == EOF && err == NULL)
4617 err = got_error_from_errno2("fclose", *path_outfile);
4618 if (path1 && unlink(path1) == -1 && err == NULL)
4619 err = got_error_from_errno2("unlink", path1);
4620 if (err || !have_content) {
4621 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4622 err = got_error_from_errno2("unlink", *path_outfile);
4623 free(*path_outfile);
4624 *path_outfile = NULL;
4626 free(path1);
4627 return err;
4630 static const struct got_error *
4631 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4632 const char *relpath, struct got_object_id *blob_id,
4633 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4634 int dirfd, const char *de_name)
4636 struct revert_file_args *a = arg;
4637 const struct got_error *err = NULL;
4638 char *parent_path = NULL;
4639 struct got_fileindex_entry *ie;
4640 struct got_tree_object *tree = NULL;
4641 struct got_object_id *tree_id = NULL;
4642 const struct got_tree_entry *te = NULL;
4643 char *tree_path = NULL, *te_name;
4644 char *ondisk_path = NULL, *path_content = NULL;
4645 struct got_blob_object *blob = NULL;
4647 /* Reverting a staged deletion is a no-op. */
4648 if (status == GOT_STATUS_DELETE &&
4649 staged_status != GOT_STATUS_NO_CHANGE)
4650 return NULL;
4652 if (status == GOT_STATUS_UNVERSIONED)
4653 return (*a->progress_cb)(a->progress_arg,
4654 GOT_STATUS_UNVERSIONED, relpath);
4656 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4657 if (ie == NULL)
4658 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4660 /* Construct in-repository path of tree which contains this blob. */
4661 err = got_path_dirname(&parent_path, ie->path);
4662 if (err) {
4663 if (err->code != GOT_ERR_BAD_PATH)
4664 goto done;
4665 parent_path = strdup("/");
4666 if (parent_path == NULL) {
4667 err = got_error_from_errno("strdup");
4668 goto done;
4671 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4672 tree_path = strdup(parent_path);
4673 if (tree_path == NULL) {
4674 err = got_error_from_errno("strdup");
4675 goto done;
4677 } else {
4678 if (got_path_is_root_dir(parent_path)) {
4679 tree_path = strdup(a->worktree->path_prefix);
4680 if (tree_path == NULL) {
4681 err = got_error_from_errno("strdup");
4682 goto done;
4684 } else {
4685 if (asprintf(&tree_path, "%s/%s",
4686 a->worktree->path_prefix, parent_path) == -1) {
4687 err = got_error_from_errno("asprintf");
4688 goto done;
4693 err = got_object_id_by_path(&tree_id, a->repo,
4694 a->worktree->base_commit_id, tree_path);
4695 if (err) {
4696 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4697 (status == GOT_STATUS_ADD ||
4698 staged_status == GOT_STATUS_ADD)))
4699 goto done;
4700 } else {
4701 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4702 if (err)
4703 goto done;
4705 err = got_path_basename(&te_name, ie->path);
4706 if (err)
4707 goto done;
4709 te = got_object_tree_find_entry(tree, te_name);
4710 free(te_name);
4711 if (te == NULL && status != GOT_STATUS_ADD &&
4712 staged_status != GOT_STATUS_ADD) {
4713 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4714 goto done;
4718 switch (status) {
4719 case GOT_STATUS_ADD:
4720 if (a->patch_cb) {
4721 int choice = GOT_PATCH_CHOICE_NONE;
4722 err = (*a->patch_cb)(&choice, a->patch_arg,
4723 status, ie->path, NULL, 1, 1);
4724 if (err)
4725 goto done;
4726 if (choice != GOT_PATCH_CHOICE_YES)
4727 break;
4729 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4730 ie->path);
4731 if (err)
4732 goto done;
4733 got_fileindex_entry_remove(a->fileindex, ie);
4734 if (a->unlink_added_files) {
4735 if (asprintf(&ondisk_path, "%s/%s",
4736 got_worktree_get_root_path(a->worktree),
4737 relpath) == -1) {
4738 err = got_error_from_errno("asprintf");
4739 goto done;
4741 if (unlink(ondisk_path) == -1) {
4742 err = got_error_from_errno2("unlink",
4743 ondisk_path);
4744 break;
4747 break;
4748 case GOT_STATUS_DELETE:
4749 if (a->patch_cb) {
4750 int choice = GOT_PATCH_CHOICE_NONE;
4751 err = (*a->patch_cb)(&choice, a->patch_arg,
4752 status, ie->path, NULL, 1, 1);
4753 if (err)
4754 goto done;
4755 if (choice != GOT_PATCH_CHOICE_YES)
4756 break;
4758 /* fall through */
4759 case GOT_STATUS_MODIFY:
4760 case GOT_STATUS_MODE_CHANGE:
4761 case GOT_STATUS_CONFLICT:
4762 case GOT_STATUS_MISSING: {
4763 struct got_object_id id;
4764 if (staged_status == GOT_STATUS_ADD ||
4765 staged_status == GOT_STATUS_MODIFY) {
4766 memcpy(id.sha1, ie->staged_blob_sha1,
4767 SHA1_DIGEST_LENGTH);
4768 } else
4769 memcpy(id.sha1, ie->blob_sha1,
4770 SHA1_DIGEST_LENGTH);
4771 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4772 if (err)
4773 goto done;
4775 if (asprintf(&ondisk_path, "%s/%s",
4776 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4777 err = got_error_from_errno("asprintf");
4778 goto done;
4781 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4782 status == GOT_STATUS_CONFLICT)) {
4783 int is_bad_symlink = 0;
4784 err = create_patched_content(&path_content, 1, &id,
4785 ondisk_path, dirfd, de_name, ie->path, a->repo,
4786 a->patch_cb, a->patch_arg);
4787 if (err || path_content == NULL)
4788 break;
4789 if (te && S_ISLNK(te->mode)) {
4790 if (unlink(path_content) == -1) {
4791 err = got_error_from_errno2("unlink",
4792 path_content);
4793 break;
4795 err = install_symlink(&is_bad_symlink,
4796 a->worktree, ondisk_path, ie->path,
4797 blob, 0, 1, 0, 0, a->repo,
4798 a->progress_cb, a->progress_arg);
4799 } else {
4800 if (rename(path_content, ondisk_path) == -1) {
4801 err = got_error_from_errno3("rename",
4802 path_content, ondisk_path);
4803 goto done;
4806 } else {
4807 int is_bad_symlink = 0;
4808 if (te && S_ISLNK(te->mode)) {
4809 err = install_symlink(&is_bad_symlink,
4810 a->worktree, ondisk_path, ie->path,
4811 blob, 0, 1, 0, 0, a->repo,
4812 a->progress_cb, a->progress_arg);
4813 } else {
4814 err = install_blob(a->worktree, ondisk_path,
4815 ie->path,
4816 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4817 got_fileindex_perms_to_st(ie), blob,
4818 0, 1, 0, 0, a->repo,
4819 a->progress_cb, a->progress_arg);
4821 if (err)
4822 goto done;
4823 if (status == GOT_STATUS_DELETE ||
4824 status == GOT_STATUS_MODE_CHANGE) {
4825 err = got_fileindex_entry_update(ie,
4826 a->worktree->root_fd, relpath,
4827 blob->id.sha1,
4828 a->worktree->base_commit_id->sha1, 1);
4829 if (err)
4830 goto done;
4832 if (is_bad_symlink) {
4833 got_fileindex_entry_filetype_set(ie,
4834 GOT_FILEIDX_MODE_BAD_SYMLINK);
4837 break;
4839 default:
4840 break;
4842 done:
4843 free(ondisk_path);
4844 free(path_content);
4845 free(parent_path);
4846 free(tree_path);
4847 if (blob)
4848 got_object_blob_close(blob);
4849 if (tree)
4850 got_object_tree_close(tree);
4851 free(tree_id);
4852 return err;
4855 const struct got_error *
4856 got_worktree_revert(struct got_worktree *worktree,
4857 struct got_pathlist_head *paths,
4858 got_worktree_checkout_cb progress_cb, void *progress_arg,
4859 got_worktree_patch_cb patch_cb, void *patch_arg,
4860 struct got_repository *repo)
4862 struct got_fileindex *fileindex = NULL;
4863 char *fileindex_path = NULL;
4864 const struct got_error *err = NULL, *unlockerr = NULL;
4865 const struct got_error *sync_err = NULL;
4866 struct got_pathlist_entry *pe;
4867 struct revert_file_args rfa;
4869 err = lock_worktree(worktree, LOCK_EX);
4870 if (err)
4871 return err;
4873 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4874 if (err)
4875 goto done;
4877 rfa.worktree = worktree;
4878 rfa.fileindex = fileindex;
4879 rfa.progress_cb = progress_cb;
4880 rfa.progress_arg = progress_arg;
4881 rfa.patch_cb = patch_cb;
4882 rfa.patch_arg = patch_arg;
4883 rfa.repo = repo;
4884 rfa.unlink_added_files = 0;
4885 TAILQ_FOREACH(pe, paths, entry) {
4886 err = worktree_status(worktree, pe->path, fileindex, repo,
4887 revert_file, &rfa, NULL, NULL, 1, 0);
4888 if (err)
4889 break;
4891 sync_err = sync_fileindex(fileindex, fileindex_path);
4892 if (sync_err && err == NULL)
4893 err = sync_err;
4894 done:
4895 free(fileindex_path);
4896 if (fileindex)
4897 got_fileindex_free(fileindex);
4898 unlockerr = lock_worktree(worktree, LOCK_SH);
4899 if (unlockerr && err == NULL)
4900 err = unlockerr;
4901 return err;
4904 static void
4905 free_commitable(struct got_commitable *ct)
4907 free(ct->path);
4908 free(ct->in_repo_path);
4909 free(ct->ondisk_path);
4910 free(ct->blob_id);
4911 free(ct->base_blob_id);
4912 free(ct->staged_blob_id);
4913 free(ct->base_commit_id);
4914 free(ct);
4917 struct collect_commitables_arg {
4918 struct got_pathlist_head *commitable_paths;
4919 struct got_repository *repo;
4920 struct got_worktree *worktree;
4921 struct got_fileindex *fileindex;
4922 int have_staged_files;
4923 int allow_bad_symlinks;
4926 static const struct got_error *
4927 collect_commitables(void *arg, unsigned char status,
4928 unsigned char staged_status, const char *relpath,
4929 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4930 struct got_object_id *commit_id, int dirfd, const char *de_name)
4932 struct collect_commitables_arg *a = arg;
4933 const struct got_error *err = NULL;
4934 struct got_commitable *ct = NULL;
4935 struct got_pathlist_entry *new = NULL;
4936 char *parent_path = NULL, *path = NULL;
4937 struct stat sb;
4939 if (a->have_staged_files) {
4940 if (staged_status != GOT_STATUS_MODIFY &&
4941 staged_status != GOT_STATUS_ADD &&
4942 staged_status != GOT_STATUS_DELETE)
4943 return NULL;
4944 } else {
4945 if (status == GOT_STATUS_CONFLICT)
4946 return got_error(GOT_ERR_COMMIT_CONFLICT);
4948 if (status != GOT_STATUS_MODIFY &&
4949 status != GOT_STATUS_MODE_CHANGE &&
4950 status != GOT_STATUS_ADD &&
4951 status != GOT_STATUS_DELETE)
4952 return NULL;
4955 if (asprintf(&path, "/%s", relpath) == -1) {
4956 err = got_error_from_errno("asprintf");
4957 goto done;
4959 if (strcmp(path, "/") == 0) {
4960 parent_path = strdup("");
4961 if (parent_path == NULL)
4962 return got_error_from_errno("strdup");
4963 } else {
4964 err = got_path_dirname(&parent_path, path);
4965 if (err)
4966 return err;
4969 ct = calloc(1, sizeof(*ct));
4970 if (ct == NULL) {
4971 err = got_error_from_errno("calloc");
4972 goto done;
4975 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4976 relpath) == -1) {
4977 err = got_error_from_errno("asprintf");
4978 goto done;
4981 if (staged_status == GOT_STATUS_ADD ||
4982 staged_status == GOT_STATUS_MODIFY) {
4983 struct got_fileindex_entry *ie;
4984 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4985 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4986 case GOT_FILEIDX_MODE_REGULAR_FILE:
4987 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4988 ct->mode = S_IFREG;
4989 break;
4990 case GOT_FILEIDX_MODE_SYMLINK:
4991 ct->mode = S_IFLNK;
4992 break;
4993 default:
4994 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4995 goto done;
4997 ct->mode |= got_fileindex_entry_perms_get(ie);
4998 } else if (status != GOT_STATUS_DELETE &&
4999 staged_status != GOT_STATUS_DELETE) {
5000 if (dirfd != -1) {
5001 if (fstatat(dirfd, de_name, &sb,
5002 AT_SYMLINK_NOFOLLOW) == -1) {
5003 err = got_error_from_errno2("fstatat",
5004 ct->ondisk_path);
5005 goto done;
5007 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5008 err = got_error_from_errno2("lstat", ct->ondisk_path);
5009 goto done;
5011 ct->mode = sb.st_mode;
5014 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5015 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5016 relpath) == -1) {
5017 err = got_error_from_errno("asprintf");
5018 goto done;
5021 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5022 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5023 int is_bad_symlink;
5024 char target_path[PATH_MAX];
5025 ssize_t target_len;
5026 target_len = readlink(ct->ondisk_path, target_path,
5027 sizeof(target_path));
5028 if (target_len == -1) {
5029 err = got_error_from_errno2("readlink",
5030 ct->ondisk_path);
5031 goto done;
5033 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5034 target_len, ct->ondisk_path, a->worktree->root_path);
5035 if (err)
5036 goto done;
5037 if (is_bad_symlink) {
5038 err = got_error_path(ct->ondisk_path,
5039 GOT_ERR_BAD_SYMLINK);
5040 goto done;
5045 ct->status = status;
5046 ct->staged_status = staged_status;
5047 ct->blob_id = NULL; /* will be filled in when blob gets created */
5048 if (ct->status != GOT_STATUS_ADD &&
5049 ct->staged_status != GOT_STATUS_ADD) {
5050 ct->base_blob_id = got_object_id_dup(blob_id);
5051 if (ct->base_blob_id == NULL) {
5052 err = got_error_from_errno("got_object_id_dup");
5053 goto done;
5055 ct->base_commit_id = got_object_id_dup(commit_id);
5056 if (ct->base_commit_id == NULL) {
5057 err = got_error_from_errno("got_object_id_dup");
5058 goto done;
5061 if (ct->staged_status == GOT_STATUS_ADD ||
5062 ct->staged_status == GOT_STATUS_MODIFY) {
5063 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5064 if (ct->staged_blob_id == NULL) {
5065 err = got_error_from_errno("got_object_id_dup");
5066 goto done;
5069 ct->path = strdup(path);
5070 if (ct->path == NULL) {
5071 err = got_error_from_errno("strdup");
5072 goto done;
5074 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5075 done:
5076 if (ct && (err || new == NULL))
5077 free_commitable(ct);
5078 free(parent_path);
5079 free(path);
5080 return err;
5083 static const struct got_error *write_tree(struct got_object_id **, int *,
5084 struct got_tree_object *, const char *, struct got_pathlist_head *,
5085 got_worktree_status_cb status_cb, void *status_arg,
5086 struct got_repository *);
5088 static const struct got_error *
5089 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5090 struct got_tree_entry *te, const char *parent_path,
5091 struct got_pathlist_head *commitable_paths,
5092 got_worktree_status_cb status_cb, void *status_arg,
5093 struct got_repository *repo)
5095 const struct got_error *err = NULL;
5096 struct got_tree_object *subtree;
5097 char *subpath;
5099 if (asprintf(&subpath, "%s%s%s", parent_path,
5100 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5101 return got_error_from_errno("asprintf");
5103 err = got_object_open_as_tree(&subtree, repo, &te->id);
5104 if (err)
5105 return err;
5107 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5108 commitable_paths, status_cb, status_arg, repo);
5109 got_object_tree_close(subtree);
5110 free(subpath);
5111 return err;
5114 static const struct got_error *
5115 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5117 const struct got_error *err = NULL;
5118 char *ct_parent_path = NULL;
5120 *match = 0;
5122 if (strchr(ct->in_repo_path, '/') == NULL) {
5123 *match = got_path_is_root_dir(path);
5124 return NULL;
5127 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5128 if (err)
5129 return err;
5130 *match = (strcmp(path, ct_parent_path) == 0);
5131 free(ct_parent_path);
5132 return err;
5135 static mode_t
5136 get_ct_file_mode(struct got_commitable *ct)
5138 if (S_ISLNK(ct->mode))
5139 return S_IFLNK;
5141 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5144 static const struct got_error *
5145 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5146 struct got_tree_entry *te, struct got_commitable *ct)
5148 const struct got_error *err = NULL;
5150 *new_te = NULL;
5152 err = got_object_tree_entry_dup(new_te, te);
5153 if (err)
5154 goto done;
5156 (*new_te)->mode = get_ct_file_mode(ct);
5158 if (ct->staged_status == GOT_STATUS_MODIFY)
5159 memcpy(&(*new_te)->id, ct->staged_blob_id,
5160 sizeof((*new_te)->id));
5161 else
5162 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5163 done:
5164 if (err && *new_te) {
5165 free(*new_te);
5166 *new_te = NULL;
5168 return err;
5171 static const struct got_error *
5172 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5173 struct got_commitable *ct)
5175 const struct got_error *err = NULL;
5176 char *ct_name = NULL;
5178 *new_te = NULL;
5180 *new_te = calloc(1, sizeof(**new_te));
5181 if (*new_te == NULL)
5182 return got_error_from_errno("calloc");
5184 err = got_path_basename(&ct_name, ct->path);
5185 if (err)
5186 goto done;
5187 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5188 sizeof((*new_te)->name)) {
5189 err = got_error(GOT_ERR_NO_SPACE);
5190 goto done;
5193 (*new_te)->mode = get_ct_file_mode(ct);
5195 if (ct->staged_status == GOT_STATUS_ADD)
5196 memcpy(&(*new_te)->id, ct->staged_blob_id,
5197 sizeof((*new_te)->id));
5198 else
5199 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5200 done:
5201 free(ct_name);
5202 if (err && *new_te) {
5203 free(*new_te);
5204 *new_te = NULL;
5206 return err;
5209 static const struct got_error *
5210 insert_tree_entry(struct got_tree_entry *new_te,
5211 struct got_pathlist_head *paths)
5213 const struct got_error *err = NULL;
5214 struct got_pathlist_entry *new_pe;
5216 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5217 if (err)
5218 return err;
5219 if (new_pe == NULL)
5220 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5221 return NULL;
5224 static const struct got_error *
5225 report_ct_status(struct got_commitable *ct,
5226 got_worktree_status_cb status_cb, void *status_arg)
5228 const char *ct_path = ct->path;
5229 unsigned char status;
5231 if (status_cb == NULL) /* no commit progress output desired */
5232 return NULL;
5234 while (ct_path[0] == '/')
5235 ct_path++;
5237 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5238 status = ct->staged_status;
5239 else
5240 status = ct->status;
5242 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5243 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5246 static const struct got_error *
5247 match_modified_subtree(int *modified, struct got_tree_entry *te,
5248 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5250 const struct got_error *err = NULL;
5251 struct got_pathlist_entry *pe;
5252 char *te_path;
5254 *modified = 0;
5256 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5257 got_path_is_root_dir(base_tree_path) ? "" : "/",
5258 te->name) == -1)
5259 return got_error_from_errno("asprintf");
5261 TAILQ_FOREACH(pe, commitable_paths, entry) {
5262 struct got_commitable *ct = pe->data;
5263 *modified = got_path_is_child(ct->in_repo_path, te_path,
5264 strlen(te_path));
5265 if (*modified)
5266 break;
5269 free(te_path);
5270 return err;
5273 static const struct got_error *
5274 match_deleted_or_modified_ct(struct got_commitable **ctp,
5275 struct got_tree_entry *te, const char *base_tree_path,
5276 struct got_pathlist_head *commitable_paths)
5278 const struct got_error *err = NULL;
5279 struct got_pathlist_entry *pe;
5281 *ctp = NULL;
5283 TAILQ_FOREACH(pe, commitable_paths, entry) {
5284 struct got_commitable *ct = pe->data;
5285 char *ct_name = NULL;
5286 int path_matches;
5288 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5289 if (ct->status != GOT_STATUS_MODIFY &&
5290 ct->status != GOT_STATUS_MODE_CHANGE &&
5291 ct->status != GOT_STATUS_DELETE)
5292 continue;
5293 } else {
5294 if (ct->staged_status != GOT_STATUS_MODIFY &&
5295 ct->staged_status != GOT_STATUS_DELETE)
5296 continue;
5299 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5300 continue;
5302 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5303 if (err)
5304 return err;
5305 if (!path_matches)
5306 continue;
5308 err = got_path_basename(&ct_name, pe->path);
5309 if (err)
5310 return err;
5312 if (strcmp(te->name, ct_name) != 0) {
5313 free(ct_name);
5314 continue;
5316 free(ct_name);
5318 *ctp = ct;
5319 break;
5322 return err;
5325 static const struct got_error *
5326 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5327 const char *child_path, const char *path_base_tree,
5328 struct got_pathlist_head *commitable_paths,
5329 got_worktree_status_cb status_cb, void *status_arg,
5330 struct got_repository *repo)
5332 const struct got_error *err = NULL;
5333 struct got_tree_entry *new_te;
5334 char *subtree_path;
5335 struct got_object_id *id = NULL;
5336 int nentries;
5338 *new_tep = NULL;
5340 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5341 got_path_is_root_dir(path_base_tree) ? "" : "/",
5342 child_path) == -1)
5343 return got_error_from_errno("asprintf");
5345 new_te = calloc(1, sizeof(*new_te));
5346 if (new_te == NULL)
5347 return got_error_from_errno("calloc");
5348 new_te->mode = S_IFDIR;
5350 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5351 sizeof(new_te->name)) {
5352 err = got_error(GOT_ERR_NO_SPACE);
5353 goto done;
5355 err = write_tree(&id, &nentries, NULL, subtree_path,
5356 commitable_paths, status_cb, status_arg, repo);
5357 if (err) {
5358 free(new_te);
5359 goto done;
5361 memcpy(&new_te->id, id, sizeof(new_te->id));
5362 done:
5363 free(id);
5364 free(subtree_path);
5365 if (err == NULL)
5366 *new_tep = new_te;
5367 return err;
5370 static const struct got_error *
5371 write_tree(struct got_object_id **new_tree_id, int *nentries,
5372 struct got_tree_object *base_tree, const char *path_base_tree,
5373 struct got_pathlist_head *commitable_paths,
5374 got_worktree_status_cb status_cb, void *status_arg,
5375 struct got_repository *repo)
5377 const struct got_error *err = NULL;
5378 struct got_pathlist_head paths;
5379 struct got_tree_entry *te, *new_te = NULL;
5380 struct got_pathlist_entry *pe;
5382 TAILQ_INIT(&paths);
5383 *nentries = 0;
5385 /* Insert, and recurse into, newly added entries first. */
5386 TAILQ_FOREACH(pe, commitable_paths, entry) {
5387 struct got_commitable *ct = pe->data;
5388 char *child_path = NULL, *slash;
5390 if ((ct->status != GOT_STATUS_ADD &&
5391 ct->staged_status != GOT_STATUS_ADD) ||
5392 (ct->flags & GOT_COMMITABLE_ADDED))
5393 continue;
5395 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5396 strlen(path_base_tree)))
5397 continue;
5399 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5400 ct->in_repo_path);
5401 if (err)
5402 goto done;
5404 slash = strchr(child_path, '/');
5405 if (slash == NULL) {
5406 err = alloc_added_blob_tree_entry(&new_te, ct);
5407 if (err)
5408 goto done;
5409 err = report_ct_status(ct, status_cb, status_arg);
5410 if (err)
5411 goto done;
5412 ct->flags |= GOT_COMMITABLE_ADDED;
5413 err = insert_tree_entry(new_te, &paths);
5414 if (err)
5415 goto done;
5416 (*nentries)++;
5417 } else {
5418 *slash = '\0'; /* trim trailing path components */
5419 if (base_tree == NULL ||
5420 got_object_tree_find_entry(base_tree, child_path)
5421 == NULL) {
5422 err = make_subtree_for_added_blob(&new_te,
5423 child_path, path_base_tree,
5424 commitable_paths, status_cb, status_arg,
5425 repo);
5426 if (err)
5427 goto done;
5428 err = insert_tree_entry(new_te, &paths);
5429 if (err)
5430 goto done;
5431 (*nentries)++;
5436 if (base_tree) {
5437 int i, nbase_entries;
5438 /* Handle modified and deleted entries. */
5439 nbase_entries = got_object_tree_get_nentries(base_tree);
5440 for (i = 0; i < nbase_entries; i++) {
5441 struct got_commitable *ct = NULL;
5443 te = got_object_tree_get_entry(base_tree, i);
5444 if (got_object_tree_entry_is_submodule(te)) {
5445 /* Entry is a submodule; just copy it. */
5446 err = got_object_tree_entry_dup(&new_te, te);
5447 if (err)
5448 goto done;
5449 err = insert_tree_entry(new_te, &paths);
5450 if (err)
5451 goto done;
5452 (*nentries)++;
5453 continue;
5456 if (S_ISDIR(te->mode)) {
5457 int modified;
5458 err = got_object_tree_entry_dup(&new_te, te);
5459 if (err)
5460 goto done;
5461 err = match_modified_subtree(&modified, te,
5462 path_base_tree, commitable_paths);
5463 if (err)
5464 goto done;
5465 /* Avoid recursion into unmodified subtrees. */
5466 if (modified) {
5467 struct got_object_id *new_id;
5468 int nsubentries;
5469 err = write_subtree(&new_id,
5470 &nsubentries, te,
5471 path_base_tree, commitable_paths,
5472 status_cb, status_arg, repo);
5473 if (err)
5474 goto done;
5475 if (nsubentries == 0) {
5476 /* All entries were deleted. */
5477 free(new_id);
5478 continue;
5480 memcpy(&new_te->id, new_id,
5481 sizeof(new_te->id));
5482 free(new_id);
5484 err = insert_tree_entry(new_te, &paths);
5485 if (err)
5486 goto done;
5487 (*nentries)++;
5488 continue;
5491 err = match_deleted_or_modified_ct(&ct, te,
5492 path_base_tree, commitable_paths);
5493 if (err)
5494 goto done;
5495 if (ct) {
5496 /* NB: Deleted entries get dropped here. */
5497 if (ct->status == GOT_STATUS_MODIFY ||
5498 ct->status == GOT_STATUS_MODE_CHANGE ||
5499 ct->staged_status == GOT_STATUS_MODIFY) {
5500 err = alloc_modified_blob_tree_entry(
5501 &new_te, te, ct);
5502 if (err)
5503 goto done;
5504 err = insert_tree_entry(new_te, &paths);
5505 if (err)
5506 goto done;
5507 (*nentries)++;
5509 err = report_ct_status(ct, status_cb,
5510 status_arg);
5511 if (err)
5512 goto done;
5513 } else {
5514 /* Entry is unchanged; just copy it. */
5515 err = got_object_tree_entry_dup(&new_te, te);
5516 if (err)
5517 goto done;
5518 err = insert_tree_entry(new_te, &paths);
5519 if (err)
5520 goto done;
5521 (*nentries)++;
5526 /* Write new list of entries; deleted entries have been dropped. */
5527 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5528 done:
5529 got_pathlist_free(&paths);
5530 return err;
5533 static const struct got_error *
5534 update_fileindex_after_commit(struct got_worktree *worktree,
5535 struct got_pathlist_head *commitable_paths,
5536 struct got_object_id *new_base_commit_id,
5537 struct got_fileindex *fileindex, int have_staged_files)
5539 const struct got_error *err = NULL;
5540 struct got_pathlist_entry *pe;
5541 char *relpath = NULL;
5543 TAILQ_FOREACH(pe, commitable_paths, entry) {
5544 struct got_fileindex_entry *ie;
5545 struct got_commitable *ct = pe->data;
5547 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5549 err = got_path_skip_common_ancestor(&relpath,
5550 worktree->root_path, ct->ondisk_path);
5551 if (err)
5552 goto done;
5554 if (ie) {
5555 if (ct->status == GOT_STATUS_DELETE ||
5556 ct->staged_status == GOT_STATUS_DELETE) {
5557 got_fileindex_entry_remove(fileindex, ie);
5558 } else if (ct->staged_status == GOT_STATUS_ADD ||
5559 ct->staged_status == GOT_STATUS_MODIFY) {
5560 got_fileindex_entry_stage_set(ie,
5561 GOT_FILEIDX_STAGE_NONE);
5562 got_fileindex_entry_staged_filetype_set(ie, 0);
5564 err = got_fileindex_entry_update(ie,
5565 worktree->root_fd, relpath,
5566 ct->staged_blob_id->sha1,
5567 new_base_commit_id->sha1,
5568 !have_staged_files);
5569 } else
5570 err = got_fileindex_entry_update(ie,
5571 worktree->root_fd, relpath,
5572 ct->blob_id->sha1,
5573 new_base_commit_id->sha1,
5574 !have_staged_files);
5575 } else {
5576 err = got_fileindex_entry_alloc(&ie, pe->path);
5577 if (err)
5578 goto done;
5579 err = got_fileindex_entry_update(ie,
5580 worktree->root_fd, relpath, ct->blob_id->sha1,
5581 new_base_commit_id->sha1, 1);
5582 if (err) {
5583 got_fileindex_entry_free(ie);
5584 goto done;
5586 err = got_fileindex_entry_add(fileindex, ie);
5587 if (err) {
5588 got_fileindex_entry_free(ie);
5589 goto done;
5592 free(relpath);
5593 relpath = NULL;
5595 done:
5596 free(relpath);
5597 return err;
5601 static const struct got_error *
5602 check_out_of_date(const char *in_repo_path, unsigned char status,
5603 unsigned char staged_status, struct got_object_id *base_blob_id,
5604 struct got_object_id *base_commit_id,
5605 struct got_object_id *head_commit_id, struct got_repository *repo,
5606 int ood_errcode)
5608 const struct got_error *err = NULL;
5609 struct got_object_id *id = NULL;
5611 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5612 /* Trivial case: base commit == head commit */
5613 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5614 return NULL;
5616 * Ensure file content which local changes were based
5617 * on matches file content in the branch head.
5619 err = got_object_id_by_path(&id, repo, head_commit_id,
5620 in_repo_path);
5621 if (err) {
5622 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5623 err = got_error(ood_errcode);
5624 goto done;
5625 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5626 err = got_error(ood_errcode);
5627 } else {
5628 /* Require that added files don't exist in the branch head. */
5629 err = got_object_id_by_path(&id, repo, head_commit_id,
5630 in_repo_path);
5631 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5632 goto done;
5633 err = id ? got_error(ood_errcode) : NULL;
5635 done:
5636 free(id);
5637 return err;
5640 const struct got_error *
5641 commit_worktree(struct got_object_id **new_commit_id,
5642 struct got_pathlist_head *commitable_paths,
5643 struct got_object_id *head_commit_id,
5644 struct got_object_id *parent_id2,
5645 struct got_worktree *worktree,
5646 const char *author, const char *committer,
5647 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5648 got_worktree_status_cb status_cb, void *status_arg,
5649 struct got_repository *repo)
5651 const struct got_error *err = NULL, *unlockerr = NULL;
5652 struct got_pathlist_entry *pe;
5653 const char *head_ref_name = NULL;
5654 struct got_commit_object *head_commit = NULL;
5655 struct got_reference *head_ref2 = NULL;
5656 struct got_object_id *head_commit_id2 = NULL;
5657 struct got_tree_object *head_tree = NULL;
5658 struct got_object_id *new_tree_id = NULL;
5659 int nentries, nparents = 0;
5660 struct got_object_id_queue parent_ids;
5661 struct got_object_qid *pid = NULL;
5662 char *logmsg = NULL;
5664 *new_commit_id = NULL;
5666 STAILQ_INIT(&parent_ids);
5668 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5669 if (err)
5670 goto done;
5672 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5673 if (err)
5674 goto done;
5676 if (commit_msg_cb != NULL) {
5677 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5678 if (err)
5679 goto done;
5682 if (logmsg == NULL || strlen(logmsg) == 0) {
5683 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5684 goto done;
5687 /* Create blobs from added and modified files and record their IDs. */
5688 TAILQ_FOREACH(pe, commitable_paths, entry) {
5689 struct got_commitable *ct = pe->data;
5690 char *ondisk_path;
5692 /* Blobs for staged files already exist. */
5693 if (ct->staged_status == GOT_STATUS_ADD ||
5694 ct->staged_status == GOT_STATUS_MODIFY)
5695 continue;
5697 if (ct->status != GOT_STATUS_ADD &&
5698 ct->status != GOT_STATUS_MODIFY &&
5699 ct->status != GOT_STATUS_MODE_CHANGE)
5700 continue;
5702 if (asprintf(&ondisk_path, "%s/%s",
5703 worktree->root_path, pe->path) == -1) {
5704 err = got_error_from_errno("asprintf");
5705 goto done;
5707 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5708 free(ondisk_path);
5709 if (err)
5710 goto done;
5713 /* Recursively write new tree objects. */
5714 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5715 commitable_paths, status_cb, status_arg, repo);
5716 if (err)
5717 goto done;
5719 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5720 if (err)
5721 goto done;
5722 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5723 nparents++;
5724 if (parent_id2) {
5725 err = got_object_qid_alloc(&pid, parent_id2);
5726 if (err)
5727 goto done;
5728 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5729 nparents++;
5731 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5732 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5733 if (logmsg != NULL)
5734 free(logmsg);
5735 if (err)
5736 goto done;
5738 /* Check if a concurrent commit to our branch has occurred. */
5739 head_ref_name = got_worktree_get_head_ref_name(worktree);
5740 if (head_ref_name == NULL) {
5741 err = got_error_from_errno("got_worktree_get_head_ref_name");
5742 goto done;
5744 /* Lock the reference here to prevent concurrent modification. */
5745 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5746 if (err)
5747 goto done;
5748 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5749 if (err)
5750 goto done;
5751 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5752 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5753 goto done;
5755 /* Update branch head in repository. */
5756 err = got_ref_change_ref(head_ref2, *new_commit_id);
5757 if (err)
5758 goto done;
5759 err = got_ref_write(head_ref2, repo);
5760 if (err)
5761 goto done;
5763 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5764 if (err)
5765 goto done;
5767 err = ref_base_commit(worktree, repo);
5768 if (err)
5769 goto done;
5770 done:
5771 got_object_id_queue_free(&parent_ids);
5772 if (head_tree)
5773 got_object_tree_close(head_tree);
5774 if (head_commit)
5775 got_object_commit_close(head_commit);
5776 free(head_commit_id2);
5777 if (head_ref2) {
5778 unlockerr = got_ref_unlock(head_ref2);
5779 if (unlockerr && err == NULL)
5780 err = unlockerr;
5781 got_ref_close(head_ref2);
5783 return err;
5786 static const struct got_error *
5787 check_path_is_commitable(const char *path,
5788 struct got_pathlist_head *commitable_paths)
5790 struct got_pathlist_entry *cpe = NULL;
5791 size_t path_len = strlen(path);
5793 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5794 struct got_commitable *ct = cpe->data;
5795 const char *ct_path = ct->path;
5797 while (ct_path[0] == '/')
5798 ct_path++;
5800 if (strcmp(path, ct_path) == 0 ||
5801 got_path_is_child(ct_path, path, path_len))
5802 break;
5805 if (cpe == NULL)
5806 return got_error_path(path, GOT_ERR_BAD_PATH);
5808 return NULL;
5811 static const struct got_error *
5812 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5814 int *have_staged_files = arg;
5816 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5817 *have_staged_files = 1;
5818 return got_error(GOT_ERR_CANCELLED);
5821 return NULL;
5824 static const struct got_error *
5825 check_non_staged_files(struct got_fileindex *fileindex,
5826 struct got_pathlist_head *paths)
5828 struct got_pathlist_entry *pe;
5829 struct got_fileindex_entry *ie;
5831 TAILQ_FOREACH(pe, paths, entry) {
5832 if (pe->path[0] == '\0')
5833 continue;
5834 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5835 if (ie == NULL)
5836 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5837 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5838 return got_error_path(pe->path,
5839 GOT_ERR_FILE_NOT_STAGED);
5842 return NULL;
5845 const struct got_error *
5846 got_worktree_commit(struct got_object_id **new_commit_id,
5847 struct got_worktree *worktree, struct got_pathlist_head *paths,
5848 const char *author, const char *committer, int allow_bad_symlinks,
5849 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5850 got_worktree_status_cb status_cb, void *status_arg,
5851 struct got_repository *repo)
5853 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5854 struct got_fileindex *fileindex = NULL;
5855 char *fileindex_path = NULL;
5856 struct got_pathlist_head commitable_paths;
5857 struct collect_commitables_arg cc_arg;
5858 struct got_pathlist_entry *pe;
5859 struct got_reference *head_ref = NULL;
5860 struct got_object_id *head_commit_id = NULL;
5861 int have_staged_files = 0;
5863 *new_commit_id = NULL;
5865 TAILQ_INIT(&commitable_paths);
5867 err = lock_worktree(worktree, LOCK_EX);
5868 if (err)
5869 goto done;
5871 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5872 if (err)
5873 goto done;
5875 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5876 if (err)
5877 goto done;
5879 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5880 if (err)
5881 goto done;
5883 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5884 &have_staged_files);
5885 if (err && err->code != GOT_ERR_CANCELLED)
5886 goto done;
5887 if (have_staged_files) {
5888 err = check_non_staged_files(fileindex, paths);
5889 if (err)
5890 goto done;
5893 cc_arg.commitable_paths = &commitable_paths;
5894 cc_arg.worktree = worktree;
5895 cc_arg.fileindex = fileindex;
5896 cc_arg.repo = repo;
5897 cc_arg.have_staged_files = have_staged_files;
5898 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5899 TAILQ_FOREACH(pe, paths, entry) {
5900 err = worktree_status(worktree, pe->path, fileindex, repo,
5901 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5902 if (err)
5903 goto done;
5906 if (TAILQ_EMPTY(&commitable_paths)) {
5907 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5908 goto done;
5911 TAILQ_FOREACH(pe, paths, entry) {
5912 err = check_path_is_commitable(pe->path, &commitable_paths);
5913 if (err)
5914 goto done;
5917 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5918 struct got_commitable *ct = pe->data;
5919 const char *ct_path = ct->in_repo_path;
5921 while (ct_path[0] == '/')
5922 ct_path++;
5923 err = check_out_of_date(ct_path, ct->status,
5924 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5925 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5926 if (err)
5927 goto done;
5931 err = commit_worktree(new_commit_id, &commitable_paths,
5932 head_commit_id, NULL, worktree, author, committer,
5933 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5934 if (err)
5935 goto done;
5937 err = update_fileindex_after_commit(worktree, &commitable_paths,
5938 *new_commit_id, fileindex, have_staged_files);
5939 sync_err = sync_fileindex(fileindex, fileindex_path);
5940 if (sync_err && err == NULL)
5941 err = sync_err;
5942 done:
5943 if (fileindex)
5944 got_fileindex_free(fileindex);
5945 free(fileindex_path);
5946 unlockerr = lock_worktree(worktree, LOCK_SH);
5947 if (unlockerr && err == NULL)
5948 err = unlockerr;
5949 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5950 struct got_commitable *ct = pe->data;
5951 free_commitable(ct);
5953 got_pathlist_free(&commitable_paths);
5954 return err;
5957 const char *
5958 got_commitable_get_path(struct got_commitable *ct)
5960 return ct->path;
5963 unsigned int
5964 got_commitable_get_status(struct got_commitable *ct)
5966 return ct->status;
5969 struct check_rebase_ok_arg {
5970 struct got_worktree *worktree;
5971 struct got_repository *repo;
5974 static const struct got_error *
5975 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5977 const struct got_error *err = NULL;
5978 struct check_rebase_ok_arg *a = arg;
5979 unsigned char status;
5980 struct stat sb;
5981 char *ondisk_path;
5983 /* Reject rebase of a work tree with mixed base commits. */
5984 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5985 SHA1_DIGEST_LENGTH))
5986 return got_error(GOT_ERR_MIXED_COMMITS);
5988 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5989 == -1)
5990 return got_error_from_errno("asprintf");
5992 /* Reject rebase of a work tree with modified or staged files. */
5993 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5994 free(ondisk_path);
5995 if (err)
5996 return err;
5998 if (status != GOT_STATUS_NO_CHANGE)
5999 return got_error(GOT_ERR_MODIFIED);
6000 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6001 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6003 return NULL;
6006 const struct got_error *
6007 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6008 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6009 struct got_worktree *worktree, struct got_reference *branch,
6010 struct got_repository *repo)
6012 const struct got_error *err = NULL;
6013 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6014 char *branch_ref_name = NULL;
6015 char *fileindex_path = NULL;
6016 struct check_rebase_ok_arg ok_arg;
6017 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6018 struct got_object_id *wt_branch_tip = NULL;
6020 *new_base_branch_ref = NULL;
6021 *tmp_branch = NULL;
6022 *fileindex = NULL;
6024 err = lock_worktree(worktree, LOCK_EX);
6025 if (err)
6026 return err;
6028 err = open_fileindex(fileindex, &fileindex_path, worktree);
6029 if (err)
6030 goto done;
6032 ok_arg.worktree = worktree;
6033 ok_arg.repo = repo;
6034 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6035 &ok_arg);
6036 if (err)
6037 goto done;
6039 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6040 if (err)
6041 goto done;
6043 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6044 if (err)
6045 goto done;
6047 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6048 if (err)
6049 goto done;
6051 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6052 0);
6053 if (err)
6054 goto done;
6056 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6057 if (err)
6058 goto done;
6059 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6060 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6061 goto done;
6064 err = got_ref_alloc_symref(new_base_branch_ref,
6065 new_base_branch_ref_name, wt_branch);
6066 if (err)
6067 goto done;
6068 err = got_ref_write(*new_base_branch_ref, repo);
6069 if (err)
6070 goto done;
6072 /* TODO Lock original branch's ref while rebasing? */
6074 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6075 if (err)
6076 goto done;
6078 err = got_ref_write(branch_ref, repo);
6079 if (err)
6080 goto done;
6082 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6083 worktree->base_commit_id);
6084 if (err)
6085 goto done;
6086 err = got_ref_write(*tmp_branch, repo);
6087 if (err)
6088 goto done;
6090 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6091 if (err)
6092 goto done;
6093 done:
6094 free(fileindex_path);
6095 free(tmp_branch_name);
6096 free(new_base_branch_ref_name);
6097 free(branch_ref_name);
6098 if (branch_ref)
6099 got_ref_close(branch_ref);
6100 if (wt_branch)
6101 got_ref_close(wt_branch);
6102 free(wt_branch_tip);
6103 if (err) {
6104 if (*new_base_branch_ref) {
6105 got_ref_close(*new_base_branch_ref);
6106 *new_base_branch_ref = NULL;
6108 if (*tmp_branch) {
6109 got_ref_close(*tmp_branch);
6110 *tmp_branch = NULL;
6112 if (*fileindex) {
6113 got_fileindex_free(*fileindex);
6114 *fileindex = NULL;
6116 lock_worktree(worktree, LOCK_SH);
6118 return err;
6121 const struct got_error *
6122 got_worktree_rebase_continue(struct got_object_id **commit_id,
6123 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6124 struct got_reference **branch, struct got_fileindex **fileindex,
6125 struct got_worktree *worktree, struct got_repository *repo)
6127 const struct got_error *err;
6128 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6129 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6130 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6131 char *fileindex_path = NULL;
6132 int have_staged_files = 0;
6134 *commit_id = NULL;
6135 *new_base_branch = NULL;
6136 *tmp_branch = NULL;
6137 *branch = NULL;
6138 *fileindex = NULL;
6140 err = lock_worktree(worktree, LOCK_EX);
6141 if (err)
6142 return err;
6144 err = open_fileindex(fileindex, &fileindex_path, worktree);
6145 if (err)
6146 goto done;
6148 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6149 &have_staged_files);
6150 if (err && err->code != GOT_ERR_CANCELLED)
6151 goto done;
6152 if (have_staged_files) {
6153 err = got_error(GOT_ERR_STAGED_PATHS);
6154 goto done;
6157 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6158 if (err)
6159 goto done;
6161 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6162 if (err)
6163 goto done;
6165 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6166 if (err)
6167 goto done;
6169 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6170 if (err)
6171 goto done;
6173 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6174 if (err)
6175 goto done;
6177 err = got_ref_open(branch, repo,
6178 got_ref_get_symref_target(branch_ref), 0);
6179 if (err)
6180 goto done;
6182 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6183 if (err)
6184 goto done;
6186 err = got_ref_resolve(commit_id, repo, commit_ref);
6187 if (err)
6188 goto done;
6190 err = got_ref_open(new_base_branch, repo,
6191 new_base_branch_ref_name, 0);
6192 if (err)
6193 goto done;
6195 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6196 if (err)
6197 goto done;
6198 done:
6199 free(commit_ref_name);
6200 free(branch_ref_name);
6201 free(fileindex_path);
6202 if (commit_ref)
6203 got_ref_close(commit_ref);
6204 if (branch_ref)
6205 got_ref_close(branch_ref);
6206 if (err) {
6207 free(*commit_id);
6208 *commit_id = NULL;
6209 if (*tmp_branch) {
6210 got_ref_close(*tmp_branch);
6211 *tmp_branch = NULL;
6213 if (*new_base_branch) {
6214 got_ref_close(*new_base_branch);
6215 *new_base_branch = NULL;
6217 if (*branch) {
6218 got_ref_close(*branch);
6219 *branch = NULL;
6221 if (*fileindex) {
6222 got_fileindex_free(*fileindex);
6223 *fileindex = NULL;
6225 lock_worktree(worktree, LOCK_SH);
6227 return err;
6230 const struct got_error *
6231 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6233 const struct got_error *err;
6234 char *tmp_branch_name = NULL;
6236 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6237 if (err)
6238 return err;
6240 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6241 free(tmp_branch_name);
6242 return NULL;
6245 static const struct got_error *
6246 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6247 char **logmsg, void *arg)
6249 *logmsg = arg;
6250 return NULL;
6253 static const struct got_error *
6254 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6255 const char *path, struct got_object_id *blob_id,
6256 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6257 int dirfd, const char *de_name)
6259 return NULL;
6262 struct collect_merged_paths_arg {
6263 got_worktree_checkout_cb progress_cb;
6264 void *progress_arg;
6265 struct got_pathlist_head *merged_paths;
6268 static const struct got_error *
6269 collect_merged_paths(void *arg, unsigned char status, const char *path)
6271 const struct got_error *err;
6272 struct collect_merged_paths_arg *a = arg;
6273 char *p;
6274 struct got_pathlist_entry *new;
6276 err = (*a->progress_cb)(a->progress_arg, status, path);
6277 if (err)
6278 return err;
6280 if (status != GOT_STATUS_MERGE &&
6281 status != GOT_STATUS_ADD &&
6282 status != GOT_STATUS_DELETE &&
6283 status != GOT_STATUS_CONFLICT)
6284 return NULL;
6286 p = strdup(path);
6287 if (p == NULL)
6288 return got_error_from_errno("strdup");
6290 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6291 if (err || new == NULL)
6292 free(p);
6293 return err;
6296 void
6297 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6299 struct got_pathlist_entry *pe;
6301 TAILQ_FOREACH(pe, merged_paths, entry)
6302 free((char *)pe->path);
6304 got_pathlist_free(merged_paths);
6307 static const struct got_error *
6308 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6309 int is_rebase, struct got_repository *repo)
6311 const struct got_error *err;
6312 struct got_reference *commit_ref = NULL;
6314 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6315 if (err) {
6316 if (err->code != GOT_ERR_NOT_REF)
6317 goto done;
6318 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6319 if (err)
6320 goto done;
6321 err = got_ref_write(commit_ref, repo);
6322 if (err)
6323 goto done;
6324 } else if (is_rebase) {
6325 struct got_object_id *stored_id;
6326 int cmp;
6328 err = got_ref_resolve(&stored_id, repo, commit_ref);
6329 if (err)
6330 goto done;
6331 cmp = got_object_id_cmp(commit_id, stored_id);
6332 free(stored_id);
6333 if (cmp != 0) {
6334 err = got_error(GOT_ERR_REBASE_COMMITID);
6335 goto done;
6338 done:
6339 if (commit_ref)
6340 got_ref_close(commit_ref);
6341 return err;
6344 static const struct got_error *
6345 rebase_merge_files(struct got_pathlist_head *merged_paths,
6346 const char *commit_ref_name, struct got_worktree *worktree,
6347 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6348 struct got_object_id *commit_id, struct got_repository *repo,
6349 got_worktree_checkout_cb progress_cb, void *progress_arg,
6350 got_cancel_cb cancel_cb, void *cancel_arg)
6352 const struct got_error *err;
6353 struct got_reference *commit_ref = NULL;
6354 struct collect_merged_paths_arg cmp_arg;
6355 char *fileindex_path;
6357 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6359 err = get_fileindex_path(&fileindex_path, worktree);
6360 if (err)
6361 return err;
6363 cmp_arg.progress_cb = progress_cb;
6364 cmp_arg.progress_arg = progress_arg;
6365 cmp_arg.merged_paths = merged_paths;
6366 err = merge_files(worktree, fileindex, fileindex_path,
6367 parent_commit_id, commit_id, repo, collect_merged_paths,
6368 &cmp_arg, cancel_cb, cancel_arg);
6369 if (commit_ref)
6370 got_ref_close(commit_ref);
6371 return err;
6374 const struct got_error *
6375 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6376 struct got_worktree *worktree, struct got_fileindex *fileindex,
6377 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6378 struct got_repository *repo,
6379 got_worktree_checkout_cb progress_cb, void *progress_arg,
6380 got_cancel_cb cancel_cb, void *cancel_arg)
6382 const struct got_error *err;
6383 char *commit_ref_name;
6385 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6386 if (err)
6387 return err;
6389 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6390 if (err)
6391 goto done;
6393 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6394 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6395 progress_arg, cancel_cb, cancel_arg);
6396 done:
6397 free(commit_ref_name);
6398 return err;
6401 const struct got_error *
6402 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6403 struct got_worktree *worktree, struct got_fileindex *fileindex,
6404 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6405 struct got_repository *repo,
6406 got_worktree_checkout_cb progress_cb, void *progress_arg,
6407 got_cancel_cb cancel_cb, void *cancel_arg)
6409 const struct got_error *err;
6410 char *commit_ref_name;
6412 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6413 if (err)
6414 return err;
6416 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6417 if (err)
6418 goto done;
6420 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6421 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6422 progress_arg, cancel_cb, cancel_arg);
6423 done:
6424 free(commit_ref_name);
6425 return err;
6428 static const struct got_error *
6429 rebase_commit(struct got_object_id **new_commit_id,
6430 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6431 struct got_worktree *worktree, struct got_fileindex *fileindex,
6432 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6433 const char *new_logmsg, struct got_repository *repo)
6435 const struct got_error *err, *sync_err;
6436 struct got_pathlist_head commitable_paths;
6437 struct collect_commitables_arg cc_arg;
6438 char *fileindex_path = NULL;
6439 struct got_reference *head_ref = NULL;
6440 struct got_object_id *head_commit_id = NULL;
6441 char *logmsg = NULL;
6443 TAILQ_INIT(&commitable_paths);
6444 *new_commit_id = NULL;
6446 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6448 err = get_fileindex_path(&fileindex_path, worktree);
6449 if (err)
6450 return err;
6452 cc_arg.commitable_paths = &commitable_paths;
6453 cc_arg.worktree = worktree;
6454 cc_arg.repo = repo;
6455 cc_arg.have_staged_files = 0;
6457 * If possible get the status of individual files directly to
6458 * avoid crawling the entire work tree once per rebased commit.
6460 * Ideally, merged_paths would contain a list of commitables
6461 * we could use so we could skip worktree_status() entirely.
6462 * However, we would then need carefully keep track of cumulative
6463 * effects of operations such as file additions and deletions
6464 * in 'got histedit -f' (folding multiple commits into one),
6465 * and this extra complexity is not really worth it.
6467 if (merged_paths) {
6468 struct got_pathlist_entry *pe;
6469 TAILQ_FOREACH(pe, merged_paths, entry) {
6470 err = worktree_status(worktree, pe->path, fileindex,
6471 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6472 0);
6473 if (err)
6474 goto done;
6476 } else {
6477 err = worktree_status(worktree, "", fileindex, repo,
6478 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6479 if (err)
6480 goto done;
6483 if (TAILQ_EMPTY(&commitable_paths)) {
6484 /* No-op change; commit will be elided. */
6485 err = got_ref_delete(commit_ref, repo);
6486 if (err)
6487 goto done;
6488 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6489 goto done;
6492 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6493 if (err)
6494 goto done;
6496 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6497 if (err)
6498 goto done;
6500 if (new_logmsg) {
6501 logmsg = strdup(new_logmsg);
6502 if (logmsg == NULL) {
6503 err = got_error_from_errno("strdup");
6504 goto done;
6506 } else {
6507 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6508 if (err)
6509 goto done;
6512 /* NB: commit_worktree will call free(logmsg) */
6513 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6514 NULL, worktree, got_object_commit_get_author(orig_commit),
6515 got_object_commit_get_committer(orig_commit),
6516 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6517 if (err)
6518 goto done;
6520 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6521 if (err)
6522 goto done;
6524 err = got_ref_delete(commit_ref, repo);
6525 if (err)
6526 goto done;
6528 err = update_fileindex_after_commit(worktree, &commitable_paths,
6529 *new_commit_id, fileindex, 0);
6530 sync_err = sync_fileindex(fileindex, fileindex_path);
6531 if (sync_err && err == NULL)
6532 err = sync_err;
6533 done:
6534 free(fileindex_path);
6535 free(head_commit_id);
6536 if (head_ref)
6537 got_ref_close(head_ref);
6538 if (err) {
6539 free(*new_commit_id);
6540 *new_commit_id = NULL;
6542 return err;
6545 const struct got_error *
6546 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6547 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6548 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6549 struct got_commit_object *orig_commit,
6550 struct got_object_id *orig_commit_id, struct got_repository *repo)
6552 const struct got_error *err;
6553 char *commit_ref_name;
6554 struct got_reference *commit_ref = NULL;
6555 struct got_object_id *commit_id = NULL;
6557 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6558 if (err)
6559 return err;
6561 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6562 if (err)
6563 goto done;
6564 err = got_ref_resolve(&commit_id, repo, commit_ref);
6565 if (err)
6566 goto done;
6567 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6568 err = got_error(GOT_ERR_REBASE_COMMITID);
6569 goto done;
6572 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6573 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6574 done:
6575 if (commit_ref)
6576 got_ref_close(commit_ref);
6577 free(commit_ref_name);
6578 free(commit_id);
6579 return err;
6582 const struct got_error *
6583 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6584 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6585 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6586 struct got_commit_object *orig_commit,
6587 struct got_object_id *orig_commit_id, const char *new_logmsg,
6588 struct got_repository *repo)
6590 const struct got_error *err;
6591 char *commit_ref_name;
6592 struct got_reference *commit_ref = NULL;
6594 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6595 if (err)
6596 return err;
6598 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6599 if (err)
6600 goto done;
6602 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6603 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6604 done:
6605 if (commit_ref)
6606 got_ref_close(commit_ref);
6607 free(commit_ref_name);
6608 return err;
6611 const struct got_error *
6612 got_worktree_rebase_postpone(struct got_worktree *worktree,
6613 struct got_fileindex *fileindex)
6615 if (fileindex)
6616 got_fileindex_free(fileindex);
6617 return lock_worktree(worktree, LOCK_SH);
6620 static const struct got_error *
6621 delete_ref(const char *name, struct got_repository *repo)
6623 const struct got_error *err;
6624 struct got_reference *ref;
6626 err = got_ref_open(&ref, repo, name, 0);
6627 if (err) {
6628 if (err->code == GOT_ERR_NOT_REF)
6629 return NULL;
6630 return err;
6633 err = got_ref_delete(ref, repo);
6634 got_ref_close(ref);
6635 return err;
6638 static const struct got_error *
6639 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6641 const struct got_error *err;
6642 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6643 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6645 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6646 if (err)
6647 goto done;
6648 err = delete_ref(tmp_branch_name, repo);
6649 if (err)
6650 goto done;
6652 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6653 if (err)
6654 goto done;
6655 err = delete_ref(new_base_branch_ref_name, repo);
6656 if (err)
6657 goto done;
6659 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6660 if (err)
6661 goto done;
6662 err = delete_ref(branch_ref_name, repo);
6663 if (err)
6664 goto done;
6666 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6667 if (err)
6668 goto done;
6669 err = delete_ref(commit_ref_name, repo);
6670 if (err)
6671 goto done;
6673 done:
6674 free(tmp_branch_name);
6675 free(new_base_branch_ref_name);
6676 free(branch_ref_name);
6677 free(commit_ref_name);
6678 return err;
6681 const struct got_error *
6682 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6683 struct got_object_id *new_commit_id, struct got_repository *repo)
6685 const struct got_error *err;
6686 struct got_reference *ref = NULL;
6687 struct got_object_id *old_commit_id = NULL;
6688 const char *branch_name = NULL;
6689 char *new_id_str = NULL;
6690 char *refname = NULL;
6692 branch_name = got_ref_get_name(branch);
6693 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6694 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6695 branch_name += 11;
6697 err = got_object_id_str(&new_id_str, new_commit_id);
6698 if (err)
6699 return err;
6701 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6702 new_id_str) == -1) {
6703 err = got_error_from_errno("asprintf");
6704 goto done;
6707 err = got_ref_resolve(&old_commit_id, repo, branch);
6708 if (err)
6709 goto done;
6711 err = got_ref_alloc(&ref, refname, old_commit_id);
6712 if (err)
6713 goto done;
6715 err = got_ref_write(ref, repo);
6716 done:
6717 free(new_id_str);
6718 free(refname);
6719 free(old_commit_id);
6720 if (ref)
6721 got_ref_close(ref);
6722 return err;
6725 const struct got_error *
6726 got_worktree_rebase_complete(struct got_worktree *worktree,
6727 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6728 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6729 struct got_repository *repo, int create_backup)
6731 const struct got_error *err, *unlockerr, *sync_err;
6732 struct got_object_id *new_head_commit_id = NULL;
6733 char *fileindex_path = NULL;
6735 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6736 if (err)
6737 return err;
6739 if (create_backup) {
6740 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6741 rebased_branch, new_head_commit_id, repo);
6742 if (err)
6743 goto done;
6746 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6747 if (err)
6748 goto done;
6750 err = got_ref_write(rebased_branch, repo);
6751 if (err)
6752 goto done;
6754 err = got_worktree_set_head_ref(worktree, rebased_branch);
6755 if (err)
6756 goto done;
6758 err = delete_rebase_refs(worktree, repo);
6759 if (err)
6760 goto done;
6762 err = get_fileindex_path(&fileindex_path, worktree);
6763 if (err)
6764 goto done;
6765 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6766 sync_err = sync_fileindex(fileindex, fileindex_path);
6767 if (sync_err && err == NULL)
6768 err = sync_err;
6769 done:
6770 got_fileindex_free(fileindex);
6771 free(fileindex_path);
6772 free(new_head_commit_id);
6773 unlockerr = lock_worktree(worktree, LOCK_SH);
6774 if (unlockerr && err == NULL)
6775 err = unlockerr;
6776 return err;
6779 const struct got_error *
6780 got_worktree_rebase_abort(struct got_worktree *worktree,
6781 struct got_fileindex *fileindex, struct got_repository *repo,
6782 struct got_reference *new_base_branch,
6783 got_worktree_checkout_cb progress_cb, void *progress_arg)
6785 const struct got_error *err, *unlockerr, *sync_err;
6786 struct got_reference *resolved = NULL;
6787 struct got_object_id *commit_id = NULL;
6788 char *fileindex_path = NULL;
6789 struct revert_file_args rfa;
6790 struct got_object_id *tree_id = NULL;
6792 err = lock_worktree(worktree, LOCK_EX);
6793 if (err)
6794 return err;
6796 err = got_ref_open(&resolved, repo,
6797 got_ref_get_symref_target(new_base_branch), 0);
6798 if (err)
6799 goto done;
6801 err = got_worktree_set_head_ref(worktree, resolved);
6802 if (err)
6803 goto done;
6806 * XXX commits to the base branch could have happened while
6807 * we were busy rebasing; should we store the original commit ID
6808 * when rebase begins and read it back here?
6810 err = got_ref_resolve(&commit_id, repo, resolved);
6811 if (err)
6812 goto done;
6814 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6815 if (err)
6816 goto done;
6818 err = got_object_id_by_path(&tree_id, repo,
6819 worktree->base_commit_id, worktree->path_prefix);
6820 if (err)
6821 goto done;
6823 err = delete_rebase_refs(worktree, repo);
6824 if (err)
6825 goto done;
6827 err = get_fileindex_path(&fileindex_path, worktree);
6828 if (err)
6829 goto done;
6831 rfa.worktree = worktree;
6832 rfa.fileindex = fileindex;
6833 rfa.progress_cb = progress_cb;
6834 rfa.progress_arg = progress_arg;
6835 rfa.patch_cb = NULL;
6836 rfa.patch_arg = NULL;
6837 rfa.repo = repo;
6838 rfa.unlink_added_files = 0;
6839 err = worktree_status(worktree, "", fileindex, repo,
6840 revert_file, &rfa, NULL, NULL, 1, 0);
6841 if (err)
6842 goto sync;
6844 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6845 repo, progress_cb, progress_arg, NULL, NULL);
6846 sync:
6847 sync_err = sync_fileindex(fileindex, fileindex_path);
6848 if (sync_err && err == NULL)
6849 err = sync_err;
6850 done:
6851 got_ref_close(resolved);
6852 free(tree_id);
6853 free(commit_id);
6854 if (fileindex)
6855 got_fileindex_free(fileindex);
6856 free(fileindex_path);
6858 unlockerr = lock_worktree(worktree, LOCK_SH);
6859 if (unlockerr && err == NULL)
6860 err = unlockerr;
6861 return err;
6864 const struct got_error *
6865 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6866 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6867 struct got_fileindex **fileindex, struct got_worktree *worktree,
6868 struct got_repository *repo)
6870 const struct got_error *err = NULL;
6871 char *tmp_branch_name = NULL;
6872 char *branch_ref_name = NULL;
6873 char *base_commit_ref_name = NULL;
6874 char *fileindex_path = NULL;
6875 struct check_rebase_ok_arg ok_arg;
6876 struct got_reference *wt_branch = NULL;
6877 struct got_reference *base_commit_ref = NULL;
6879 *tmp_branch = NULL;
6880 *branch_ref = NULL;
6881 *base_commit_id = NULL;
6882 *fileindex = NULL;
6884 err = lock_worktree(worktree, LOCK_EX);
6885 if (err)
6886 return err;
6888 err = open_fileindex(fileindex, &fileindex_path, worktree);
6889 if (err)
6890 goto done;
6892 ok_arg.worktree = worktree;
6893 ok_arg.repo = repo;
6894 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6895 &ok_arg);
6896 if (err)
6897 goto done;
6899 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6900 if (err)
6901 goto done;
6903 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6904 if (err)
6905 goto done;
6907 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6908 worktree);
6909 if (err)
6910 goto done;
6912 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6913 0);
6914 if (err)
6915 goto done;
6917 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6918 if (err)
6919 goto done;
6921 err = got_ref_write(*branch_ref, repo);
6922 if (err)
6923 goto done;
6925 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6926 worktree->base_commit_id);
6927 if (err)
6928 goto done;
6929 err = got_ref_write(base_commit_ref, repo);
6930 if (err)
6931 goto done;
6932 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6933 if (*base_commit_id == NULL) {
6934 err = got_error_from_errno("got_object_id_dup");
6935 goto done;
6938 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6939 worktree->base_commit_id);
6940 if (err)
6941 goto done;
6942 err = got_ref_write(*tmp_branch, repo);
6943 if (err)
6944 goto done;
6946 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6947 if (err)
6948 goto done;
6949 done:
6950 free(fileindex_path);
6951 free(tmp_branch_name);
6952 free(branch_ref_name);
6953 free(base_commit_ref_name);
6954 if (wt_branch)
6955 got_ref_close(wt_branch);
6956 if (err) {
6957 if (*branch_ref) {
6958 got_ref_close(*branch_ref);
6959 *branch_ref = NULL;
6961 if (*tmp_branch) {
6962 got_ref_close(*tmp_branch);
6963 *tmp_branch = NULL;
6965 free(*base_commit_id);
6966 if (*fileindex) {
6967 got_fileindex_free(*fileindex);
6968 *fileindex = NULL;
6970 lock_worktree(worktree, LOCK_SH);
6972 return err;
6975 const struct got_error *
6976 got_worktree_histedit_postpone(struct got_worktree *worktree,
6977 struct got_fileindex *fileindex)
6979 if (fileindex)
6980 got_fileindex_free(fileindex);
6981 return lock_worktree(worktree, LOCK_SH);
6984 const struct got_error *
6985 got_worktree_histedit_in_progress(int *in_progress,
6986 struct got_worktree *worktree)
6988 const struct got_error *err;
6989 char *tmp_branch_name = NULL;
6991 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6992 if (err)
6993 return err;
6995 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6996 free(tmp_branch_name);
6997 return NULL;
7000 const struct got_error *
7001 got_worktree_histedit_continue(struct got_object_id **commit_id,
7002 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7003 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7004 struct got_worktree *worktree, struct got_repository *repo)
7006 const struct got_error *err;
7007 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7008 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7009 struct got_reference *commit_ref = NULL;
7010 struct got_reference *base_commit_ref = NULL;
7011 char *fileindex_path = NULL;
7012 int have_staged_files = 0;
7014 *commit_id = NULL;
7015 *tmp_branch = NULL;
7016 *base_commit_id = NULL;
7017 *fileindex = NULL;
7019 err = lock_worktree(worktree, LOCK_EX);
7020 if (err)
7021 return err;
7023 err = open_fileindex(fileindex, &fileindex_path, worktree);
7024 if (err)
7025 goto done;
7027 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7028 &have_staged_files);
7029 if (err && err->code != GOT_ERR_CANCELLED)
7030 goto done;
7031 if (have_staged_files) {
7032 err = got_error(GOT_ERR_STAGED_PATHS);
7033 goto done;
7036 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7037 if (err)
7038 goto done;
7040 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7041 if (err)
7042 goto done;
7044 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7045 if (err)
7046 goto done;
7048 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7049 worktree);
7050 if (err)
7051 goto done;
7053 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7054 if (err)
7055 goto done;
7057 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7058 if (err)
7059 goto done;
7060 err = got_ref_resolve(commit_id, repo, commit_ref);
7061 if (err)
7062 goto done;
7064 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7065 if (err)
7066 goto done;
7067 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7068 if (err)
7069 goto done;
7071 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7072 if (err)
7073 goto done;
7074 done:
7075 free(commit_ref_name);
7076 free(branch_ref_name);
7077 free(fileindex_path);
7078 if (commit_ref)
7079 got_ref_close(commit_ref);
7080 if (base_commit_ref)
7081 got_ref_close(base_commit_ref);
7082 if (err) {
7083 free(*commit_id);
7084 *commit_id = NULL;
7085 free(*base_commit_id);
7086 *base_commit_id = NULL;
7087 if (*tmp_branch) {
7088 got_ref_close(*tmp_branch);
7089 *tmp_branch = NULL;
7091 if (*fileindex) {
7092 got_fileindex_free(*fileindex);
7093 *fileindex = NULL;
7095 lock_worktree(worktree, LOCK_EX);
7097 return err;
7100 static const struct got_error *
7101 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7103 const struct got_error *err;
7104 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7105 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7107 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7108 if (err)
7109 goto done;
7110 err = delete_ref(tmp_branch_name, repo);
7111 if (err)
7112 goto done;
7114 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7115 worktree);
7116 if (err)
7117 goto done;
7118 err = delete_ref(base_commit_ref_name, repo);
7119 if (err)
7120 goto done;
7122 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7123 if (err)
7124 goto done;
7125 err = delete_ref(branch_ref_name, repo);
7126 if (err)
7127 goto done;
7129 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7130 if (err)
7131 goto done;
7132 err = delete_ref(commit_ref_name, repo);
7133 if (err)
7134 goto done;
7135 done:
7136 free(tmp_branch_name);
7137 free(base_commit_ref_name);
7138 free(branch_ref_name);
7139 free(commit_ref_name);
7140 return err;
7143 const struct got_error *
7144 got_worktree_histedit_abort(struct got_worktree *worktree,
7145 struct got_fileindex *fileindex, struct got_repository *repo,
7146 struct got_reference *branch, struct got_object_id *base_commit_id,
7147 got_worktree_checkout_cb progress_cb, void *progress_arg)
7149 const struct got_error *err, *unlockerr, *sync_err;
7150 struct got_reference *resolved = NULL;
7151 char *fileindex_path = NULL;
7152 struct got_object_id *tree_id = NULL;
7153 struct revert_file_args rfa;
7155 err = lock_worktree(worktree, LOCK_EX);
7156 if (err)
7157 return err;
7159 err = got_ref_open(&resolved, repo,
7160 got_ref_get_symref_target(branch), 0);
7161 if (err)
7162 goto done;
7164 err = got_worktree_set_head_ref(worktree, resolved);
7165 if (err)
7166 goto done;
7168 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7169 if (err)
7170 goto done;
7172 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7173 worktree->path_prefix);
7174 if (err)
7175 goto done;
7177 err = delete_histedit_refs(worktree, repo);
7178 if (err)
7179 goto done;
7181 err = get_fileindex_path(&fileindex_path, worktree);
7182 if (err)
7183 goto done;
7185 rfa.worktree = worktree;
7186 rfa.fileindex = fileindex;
7187 rfa.progress_cb = progress_cb;
7188 rfa.progress_arg = progress_arg;
7189 rfa.patch_cb = NULL;
7190 rfa.patch_arg = NULL;
7191 rfa.repo = repo;
7192 rfa.unlink_added_files = 0;
7193 err = worktree_status(worktree, "", fileindex, repo,
7194 revert_file, &rfa, NULL, NULL, 1, 0);
7195 if (err)
7196 goto sync;
7198 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7199 repo, progress_cb, progress_arg, NULL, NULL);
7200 sync:
7201 sync_err = sync_fileindex(fileindex, fileindex_path);
7202 if (sync_err && err == NULL)
7203 err = sync_err;
7204 done:
7205 got_ref_close(resolved);
7206 free(tree_id);
7207 free(fileindex_path);
7209 unlockerr = lock_worktree(worktree, LOCK_SH);
7210 if (unlockerr && err == NULL)
7211 err = unlockerr;
7212 return err;
7215 const struct got_error *
7216 got_worktree_histedit_complete(struct got_worktree *worktree,
7217 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7218 struct got_reference *edited_branch, struct got_repository *repo)
7220 const struct got_error *err, *unlockerr, *sync_err;
7221 struct got_object_id *new_head_commit_id = NULL;
7222 struct got_reference *resolved = NULL;
7223 char *fileindex_path = NULL;
7225 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7226 if (err)
7227 return err;
7229 err = got_ref_open(&resolved, repo,
7230 got_ref_get_symref_target(edited_branch), 0);
7231 if (err)
7232 goto done;
7234 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7235 resolved, new_head_commit_id, repo);
7236 if (err)
7237 goto done;
7239 err = got_ref_change_ref(resolved, new_head_commit_id);
7240 if (err)
7241 goto done;
7243 err = got_ref_write(resolved, repo);
7244 if (err)
7245 goto done;
7247 err = got_worktree_set_head_ref(worktree, resolved);
7248 if (err)
7249 goto done;
7251 err = delete_histedit_refs(worktree, repo);
7252 if (err)
7253 goto done;
7255 err = get_fileindex_path(&fileindex_path, worktree);
7256 if (err)
7257 goto done;
7258 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7259 sync_err = sync_fileindex(fileindex, fileindex_path);
7260 if (sync_err && err == NULL)
7261 err = sync_err;
7262 done:
7263 got_fileindex_free(fileindex);
7264 free(fileindex_path);
7265 free(new_head_commit_id);
7266 unlockerr = lock_worktree(worktree, LOCK_SH);
7267 if (unlockerr && err == NULL)
7268 err = unlockerr;
7269 return err;
7272 const struct got_error *
7273 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7274 struct got_object_id *commit_id, struct got_repository *repo)
7276 const struct got_error *err;
7277 char *commit_ref_name;
7279 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7280 if (err)
7281 return err;
7283 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7284 if (err)
7285 goto done;
7287 err = delete_ref(commit_ref_name, repo);
7288 done:
7289 free(commit_ref_name);
7290 return err;
7293 const struct got_error *
7294 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7295 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7296 struct got_worktree *worktree, const char *refname,
7297 struct got_repository *repo)
7299 const struct got_error *err = NULL;
7300 char *fileindex_path = NULL;
7301 struct check_rebase_ok_arg ok_arg;
7303 *fileindex = NULL;
7304 *branch_ref = NULL;
7305 *base_branch_ref = NULL;
7307 err = lock_worktree(worktree, LOCK_EX);
7308 if (err)
7309 return err;
7311 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7312 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7313 "cannot integrate a branch into itself; "
7314 "update -b or different branch name required");
7315 goto done;
7318 err = open_fileindex(fileindex, &fileindex_path, worktree);
7319 if (err)
7320 goto done;
7322 /* Preconditions are the same as for rebase. */
7323 ok_arg.worktree = worktree;
7324 ok_arg.repo = repo;
7325 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7326 &ok_arg);
7327 if (err)
7328 goto done;
7330 err = got_ref_open(branch_ref, repo, refname, 1);
7331 if (err)
7332 goto done;
7334 err = got_ref_open(base_branch_ref, repo,
7335 got_worktree_get_head_ref_name(worktree), 1);
7336 done:
7337 if (err) {
7338 if (*branch_ref) {
7339 got_ref_close(*branch_ref);
7340 *branch_ref = NULL;
7342 if (*base_branch_ref) {
7343 got_ref_close(*base_branch_ref);
7344 *base_branch_ref = NULL;
7346 if (*fileindex) {
7347 got_fileindex_free(*fileindex);
7348 *fileindex = NULL;
7350 lock_worktree(worktree, LOCK_SH);
7352 return err;
7355 const struct got_error *
7356 got_worktree_integrate_continue(struct got_worktree *worktree,
7357 struct got_fileindex *fileindex, struct got_repository *repo,
7358 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7359 got_worktree_checkout_cb progress_cb, void *progress_arg,
7360 got_cancel_cb cancel_cb, void *cancel_arg)
7362 const struct got_error *err = NULL, *sync_err, *unlockerr;
7363 char *fileindex_path = NULL;
7364 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7366 err = get_fileindex_path(&fileindex_path, worktree);
7367 if (err)
7368 goto done;
7370 err = got_ref_resolve(&commit_id, repo, branch_ref);
7371 if (err)
7372 goto done;
7374 err = got_object_id_by_path(&tree_id, repo, commit_id,
7375 worktree->path_prefix);
7376 if (err)
7377 goto done;
7379 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7380 if (err)
7381 goto done;
7383 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7384 progress_cb, progress_arg, cancel_cb, cancel_arg);
7385 if (err)
7386 goto sync;
7388 err = got_ref_change_ref(base_branch_ref, commit_id);
7389 if (err)
7390 goto sync;
7392 err = got_ref_write(base_branch_ref, repo);
7393 if (err)
7394 goto sync;
7396 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7397 sync:
7398 sync_err = sync_fileindex(fileindex, fileindex_path);
7399 if (sync_err && err == NULL)
7400 err = sync_err;
7402 done:
7403 unlockerr = got_ref_unlock(branch_ref);
7404 if (unlockerr && err == NULL)
7405 err = unlockerr;
7406 got_ref_close(branch_ref);
7408 unlockerr = got_ref_unlock(base_branch_ref);
7409 if (unlockerr && err == NULL)
7410 err = unlockerr;
7411 got_ref_close(base_branch_ref);
7413 got_fileindex_free(fileindex);
7414 free(fileindex_path);
7415 free(tree_id);
7417 unlockerr = lock_worktree(worktree, LOCK_SH);
7418 if (unlockerr && err == NULL)
7419 err = unlockerr;
7420 return err;
7423 const struct got_error *
7424 got_worktree_integrate_abort(struct got_worktree *worktree,
7425 struct got_fileindex *fileindex, struct got_repository *repo,
7426 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7428 const struct got_error *err = NULL, *unlockerr = NULL;
7430 got_fileindex_free(fileindex);
7432 err = lock_worktree(worktree, LOCK_SH);
7434 unlockerr = got_ref_unlock(branch_ref);
7435 if (unlockerr && err == NULL)
7436 err = unlockerr;
7437 got_ref_close(branch_ref);
7439 unlockerr = got_ref_unlock(base_branch_ref);
7440 if (unlockerr && err == NULL)
7441 err = unlockerr;
7442 got_ref_close(base_branch_ref);
7444 return err;
7447 const struct got_error *
7448 got_worktree_merge_postpone(struct got_worktree *worktree,
7449 struct got_fileindex *fileindex)
7451 const struct got_error *err, *sync_err;
7452 char *fileindex_path = NULL;
7454 err = get_fileindex_path(&fileindex_path, worktree);
7455 if (err)
7456 goto done;
7458 sync_err = sync_fileindex(fileindex, fileindex_path);
7460 err = lock_worktree(worktree, LOCK_SH);
7461 if (sync_err && err == NULL)
7462 err = sync_err;
7463 done:
7464 got_fileindex_free(fileindex);
7465 free(fileindex_path);
7466 return err;
7469 static const struct got_error *
7470 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7472 const struct got_error *err;
7473 char *branch_refname = NULL, *commit_refname = NULL;
7475 err = get_merge_branch_ref_name(&branch_refname, worktree);
7476 if (err)
7477 goto done;
7478 err = delete_ref(branch_refname, repo);
7479 if (err)
7480 goto done;
7482 err = get_merge_commit_ref_name(&commit_refname, worktree);
7483 if (err)
7484 goto done;
7485 err = delete_ref(commit_refname, repo);
7486 if (err)
7487 goto done;
7489 done:
7490 free(branch_refname);
7491 free(commit_refname);
7492 return err;
7495 struct merge_commit_msg_arg {
7496 struct got_worktree *worktree;
7497 const char *branch_name;
7500 static const struct got_error *
7501 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7502 void *arg)
7504 struct merge_commit_msg_arg *a = arg;
7506 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7507 got_worktree_get_head_ref_name(a->worktree)) == -1)
7508 return got_error_from_errno("asprintf");
7510 return NULL;
7514 const struct got_error *
7515 got_worktree_merge_branch(struct got_worktree *worktree,
7516 struct got_fileindex *fileindex,
7517 struct got_object_id *yca_commit_id,
7518 struct got_object_id *branch_tip,
7519 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7520 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7522 const struct got_error *err;
7523 char *fileindex_path = NULL;
7525 err = get_fileindex_path(&fileindex_path, worktree);
7526 if (err)
7527 goto done;
7529 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7530 worktree);
7531 if (err)
7532 goto done;
7534 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7535 branch_tip, repo, progress_cb, progress_arg,
7536 cancel_cb, cancel_arg);
7537 done:
7538 free(fileindex_path);
7539 return err;
7542 const struct got_error *
7543 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7544 struct got_worktree *worktree, struct got_fileindex *fileindex,
7545 const char *author, const char *committer, int allow_bad_symlinks,
7546 struct got_object_id *branch_tip, const char *branch_name,
7547 struct got_repository *repo,
7548 got_worktree_status_cb status_cb, void *status_arg)
7551 const struct got_error *err = NULL, *sync_err;
7552 struct got_pathlist_head commitable_paths;
7553 struct collect_commitables_arg cc_arg;
7554 struct got_pathlist_entry *pe;
7555 struct got_reference *head_ref = NULL;
7556 struct got_object_id *head_commit_id = NULL;
7557 int have_staged_files = 0;
7558 struct merge_commit_msg_arg mcm_arg;
7559 char *fileindex_path = NULL;
7561 *new_commit_id = NULL;
7563 TAILQ_INIT(&commitable_paths);
7565 err = get_fileindex_path(&fileindex_path, worktree);
7566 if (err)
7567 goto done;
7569 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7570 if (err)
7571 goto done;
7573 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7574 if (err)
7575 goto done;
7577 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7578 &have_staged_files);
7579 if (err && err->code != GOT_ERR_CANCELLED)
7580 goto done;
7581 if (have_staged_files) {
7582 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7583 goto done;
7586 cc_arg.commitable_paths = &commitable_paths;
7587 cc_arg.worktree = worktree;
7588 cc_arg.fileindex = fileindex;
7589 cc_arg.repo = repo;
7590 cc_arg.have_staged_files = have_staged_files;
7591 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7592 err = worktree_status(worktree, "", fileindex, repo,
7593 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7594 if (err)
7595 goto done;
7597 if (TAILQ_EMPTY(&commitable_paths)) {
7598 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7599 "merge of %s cannot proceed", branch_name);
7600 goto done;
7603 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7604 struct got_commitable *ct = pe->data;
7605 const char *ct_path = ct->in_repo_path;
7607 while (ct_path[0] == '/')
7608 ct_path++;
7609 err = check_out_of_date(ct_path, ct->status,
7610 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7611 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7612 if (err)
7613 goto done;
7617 mcm_arg.worktree = worktree;
7618 mcm_arg.branch_name = branch_name;
7619 err = commit_worktree(new_commit_id, &commitable_paths,
7620 head_commit_id, branch_tip, worktree, author, committer,
7621 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7622 if (err)
7623 goto done;
7625 err = update_fileindex_after_commit(worktree, &commitable_paths,
7626 *new_commit_id, fileindex, have_staged_files);
7627 sync_err = sync_fileindex(fileindex, fileindex_path);
7628 if (sync_err && err == NULL)
7629 err = sync_err;
7630 done:
7631 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7632 struct got_commitable *ct = pe->data;
7633 free_commitable(ct);
7635 got_pathlist_free(&commitable_paths);
7636 free(fileindex_path);
7637 return err;
7640 const struct got_error *
7641 got_worktree_merge_complete(struct got_worktree *worktree,
7642 struct got_fileindex *fileindex, struct got_repository *repo)
7644 const struct got_error *err, *unlockerr, *sync_err;
7645 char *fileindex_path = NULL;
7647 err = delete_merge_refs(worktree, repo);
7648 if (err)
7649 goto done;
7651 err = get_fileindex_path(&fileindex_path, worktree);
7652 if (err)
7653 goto done;
7654 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7655 sync_err = sync_fileindex(fileindex, fileindex_path);
7656 if (sync_err && err == NULL)
7657 err = sync_err;
7658 done:
7659 got_fileindex_free(fileindex);
7660 free(fileindex_path);
7661 unlockerr = lock_worktree(worktree, LOCK_SH);
7662 if (unlockerr && err == NULL)
7663 err = unlockerr;
7664 return err;
7667 const struct got_error *
7668 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7669 struct got_repository *repo)
7671 const struct got_error *err;
7672 char *branch_refname = NULL;
7673 struct got_reference *branch_ref = NULL;
7675 *in_progress = 0;
7677 err = get_merge_branch_ref_name(&branch_refname, worktree);
7678 if (err)
7679 return err;
7680 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7681 free(branch_refname);
7682 if (err) {
7683 if (err->code != GOT_ERR_NOT_REF)
7684 return err;
7685 } else
7686 *in_progress = 1;
7688 return NULL;
7691 const struct got_error *got_worktree_merge_prepare(
7692 struct got_fileindex **fileindex, struct got_worktree *worktree,
7693 struct got_reference *branch, struct got_repository *repo)
7695 const struct got_error *err = NULL;
7696 char *fileindex_path = NULL;
7697 char *branch_refname = NULL, *commit_refname = NULL;
7698 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7699 struct got_reference *commit_ref = NULL;
7700 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7701 struct check_rebase_ok_arg ok_arg;
7703 *fileindex = NULL;
7705 err = lock_worktree(worktree, LOCK_EX);
7706 if (err)
7707 return err;
7709 err = open_fileindex(fileindex, &fileindex_path, worktree);
7710 if (err)
7711 goto done;
7713 /* Preconditions are the same as for rebase. */
7714 ok_arg.worktree = worktree;
7715 ok_arg.repo = repo;
7716 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7717 &ok_arg);
7718 if (err)
7719 goto done;
7721 err = get_merge_branch_ref_name(&branch_refname, worktree);
7722 if (err)
7723 return err;
7725 err = get_merge_commit_ref_name(&commit_refname, worktree);
7726 if (err)
7727 return err;
7729 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7730 0);
7731 if (err)
7732 goto done;
7734 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7735 if (err)
7736 goto done;
7738 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7739 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7740 goto done;
7743 err = got_ref_resolve(&branch_tip, repo, branch);
7744 if (err)
7745 goto done;
7747 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7748 if (err)
7749 goto done;
7750 err = got_ref_write(branch_ref, repo);
7751 if (err)
7752 goto done;
7754 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7755 if (err)
7756 goto done;
7757 err = got_ref_write(commit_ref, repo);
7758 if (err)
7759 goto done;
7761 done:
7762 free(branch_refname);
7763 free(commit_refname);
7764 free(fileindex_path);
7765 if (branch_ref)
7766 got_ref_close(branch_ref);
7767 if (commit_ref)
7768 got_ref_close(commit_ref);
7769 if (wt_branch)
7770 got_ref_close(wt_branch);
7771 free(wt_branch_tip);
7772 if (err) {
7773 if (*fileindex) {
7774 got_fileindex_free(*fileindex);
7775 *fileindex = NULL;
7777 lock_worktree(worktree, LOCK_SH);
7779 return err;
7782 const struct got_error *
7783 got_worktree_merge_continue(char **branch_name,
7784 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7785 struct got_worktree *worktree, struct got_repository *repo)
7787 const struct got_error *err;
7788 char *commit_refname = NULL, *branch_refname = NULL;
7789 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7790 char *fileindex_path = NULL;
7791 int have_staged_files = 0;
7793 *branch_name = NULL;
7794 *branch_tip = NULL;
7795 *fileindex = NULL;
7797 err = lock_worktree(worktree, LOCK_EX);
7798 if (err)
7799 return err;
7801 err = open_fileindex(fileindex, &fileindex_path, worktree);
7802 if (err)
7803 goto done;
7805 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7806 &have_staged_files);
7807 if (err && err->code != GOT_ERR_CANCELLED)
7808 goto done;
7809 if (have_staged_files) {
7810 err = got_error(GOT_ERR_STAGED_PATHS);
7811 goto done;
7814 err = get_merge_branch_ref_name(&branch_refname, worktree);
7815 if (err)
7816 goto done;
7818 err = get_merge_commit_ref_name(&commit_refname, worktree);
7819 if (err)
7820 goto done;
7822 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7823 if (err)
7824 goto done;
7826 if (!got_ref_is_symbolic(branch_ref)) {
7827 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7828 "%s is not a symbolic reference",
7829 got_ref_get_name(branch_ref));
7830 goto done;
7832 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7833 if (*branch_name == NULL) {
7834 err = got_error_from_errno("strdup");
7835 goto done;
7838 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7839 if (err)
7840 goto done;
7842 err = got_ref_resolve(branch_tip, repo, commit_ref);
7843 if (err)
7844 goto done;
7845 done:
7846 free(commit_refname);
7847 free(branch_refname);
7848 free(fileindex_path);
7849 if (commit_ref)
7850 got_ref_close(commit_ref);
7851 if (branch_ref)
7852 got_ref_close(branch_ref);
7853 if (err) {
7854 if (*branch_name) {
7855 free(*branch_name);
7856 *branch_name = NULL;
7858 free(*branch_tip);
7859 *branch_tip = NULL;
7860 if (*fileindex) {
7861 got_fileindex_free(*fileindex);
7862 *fileindex = NULL;
7864 lock_worktree(worktree, LOCK_SH);
7866 return err;
7869 const struct got_error *
7870 got_worktree_merge_abort(struct got_worktree *worktree,
7871 struct got_fileindex *fileindex, struct got_repository *repo,
7872 got_worktree_checkout_cb progress_cb, void *progress_arg)
7874 const struct got_error *err, *unlockerr, *sync_err;
7875 struct got_object_id *commit_id = NULL;
7876 char *fileindex_path = NULL;
7877 struct revert_file_args rfa;
7878 struct got_object_id *tree_id = NULL;
7880 err = got_object_id_by_path(&tree_id, repo,
7881 worktree->base_commit_id, worktree->path_prefix);
7882 if (err)
7883 goto done;
7885 err = delete_merge_refs(worktree, repo);
7886 if (err)
7887 goto done;
7889 err = get_fileindex_path(&fileindex_path, worktree);
7890 if (err)
7891 goto done;
7893 rfa.worktree = worktree;
7894 rfa.fileindex = fileindex;
7895 rfa.progress_cb = progress_cb;
7896 rfa.progress_arg = progress_arg;
7897 rfa.patch_cb = NULL;
7898 rfa.patch_arg = NULL;
7899 rfa.repo = repo;
7900 rfa.unlink_added_files = 1;
7901 err = worktree_status(worktree, "", fileindex, repo,
7902 revert_file, &rfa, NULL, NULL, 1, 0);
7903 if (err)
7904 goto sync;
7906 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7907 repo, progress_cb, progress_arg, NULL, NULL);
7908 sync:
7909 sync_err = sync_fileindex(fileindex, fileindex_path);
7910 if (sync_err && err == NULL)
7911 err = sync_err;
7912 done:
7913 free(tree_id);
7914 free(commit_id);
7915 if (fileindex)
7916 got_fileindex_free(fileindex);
7917 free(fileindex_path);
7919 unlockerr = lock_worktree(worktree, LOCK_SH);
7920 if (unlockerr && err == NULL)
7921 err = unlockerr;
7922 return err;
7925 struct check_stage_ok_arg {
7926 struct got_object_id *head_commit_id;
7927 struct got_worktree *worktree;
7928 struct got_fileindex *fileindex;
7929 struct got_repository *repo;
7930 int have_changes;
7933 const struct got_error *
7934 check_stage_ok(void *arg, unsigned char status,
7935 unsigned char staged_status, const char *relpath,
7936 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7937 struct got_object_id *commit_id, int dirfd, const char *de_name)
7939 struct check_stage_ok_arg *a = arg;
7940 const struct got_error *err = NULL;
7941 struct got_fileindex_entry *ie;
7942 struct got_object_id base_commit_id;
7943 struct got_object_id *base_commit_idp = NULL;
7944 char *in_repo_path = NULL, *p;
7946 if (status == GOT_STATUS_UNVERSIONED ||
7947 status == GOT_STATUS_NO_CHANGE)
7948 return NULL;
7949 if (status == GOT_STATUS_NONEXISTENT)
7950 return got_error_set_errno(ENOENT, relpath);
7952 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7953 if (ie == NULL)
7954 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7956 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7957 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7958 relpath) == -1)
7959 return got_error_from_errno("asprintf");
7961 if (got_fileindex_entry_has_commit(ie)) {
7962 memcpy(base_commit_id.sha1, ie->commit_sha1,
7963 SHA1_DIGEST_LENGTH);
7964 base_commit_idp = &base_commit_id;
7967 if (status == GOT_STATUS_CONFLICT) {
7968 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7969 goto done;
7970 } else if (status != GOT_STATUS_ADD &&
7971 status != GOT_STATUS_MODIFY &&
7972 status != GOT_STATUS_DELETE) {
7973 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7974 goto done;
7977 a->have_changes = 1;
7979 p = in_repo_path;
7980 while (p[0] == '/')
7981 p++;
7982 err = check_out_of_date(p, status, staged_status,
7983 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7984 GOT_ERR_STAGE_OUT_OF_DATE);
7985 done:
7986 free(in_repo_path);
7987 return err;
7990 struct stage_path_arg {
7991 struct got_worktree *worktree;
7992 struct got_fileindex *fileindex;
7993 struct got_repository *repo;
7994 got_worktree_status_cb status_cb;
7995 void *status_arg;
7996 got_worktree_patch_cb patch_cb;
7997 void *patch_arg;
7998 int staged_something;
7999 int allow_bad_symlinks;
8002 static const struct got_error *
8003 stage_path(void *arg, unsigned char status,
8004 unsigned char staged_status, const char *relpath,
8005 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8006 struct got_object_id *commit_id, int dirfd, const char *de_name)
8008 struct stage_path_arg *a = arg;
8009 const struct got_error *err = NULL;
8010 struct got_fileindex_entry *ie;
8011 char *ondisk_path = NULL, *path_content = NULL;
8012 uint32_t stage;
8013 struct got_object_id *new_staged_blob_id = NULL;
8014 struct stat sb;
8016 if (status == GOT_STATUS_UNVERSIONED)
8017 return NULL;
8019 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8020 if (ie == NULL)
8021 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8023 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8024 relpath)== -1)
8025 return got_error_from_errno("asprintf");
8027 switch (status) {
8028 case GOT_STATUS_ADD:
8029 case GOT_STATUS_MODIFY:
8030 /* XXX could sb.st_mode be passed in by our caller? */
8031 if (lstat(ondisk_path, &sb) == -1) {
8032 err = got_error_from_errno2("lstat", ondisk_path);
8033 break;
8035 if (a->patch_cb) {
8036 if (status == GOT_STATUS_ADD) {
8037 int choice = GOT_PATCH_CHOICE_NONE;
8038 err = (*a->patch_cb)(&choice, a->patch_arg,
8039 status, ie->path, NULL, 1, 1);
8040 if (err)
8041 break;
8042 if (choice != GOT_PATCH_CHOICE_YES)
8043 break;
8044 } else {
8045 err = create_patched_content(&path_content, 0,
8046 staged_blob_id ? staged_blob_id : blob_id,
8047 ondisk_path, dirfd, de_name, ie->path,
8048 a->repo, a->patch_cb, a->patch_arg);
8049 if (err || path_content == NULL)
8050 break;
8053 err = got_object_blob_create(&new_staged_blob_id,
8054 path_content ? path_content : ondisk_path, a->repo);
8055 if (err)
8056 break;
8057 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8058 SHA1_DIGEST_LENGTH);
8059 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8060 stage = GOT_FILEIDX_STAGE_ADD;
8061 else
8062 stage = GOT_FILEIDX_STAGE_MODIFY;
8063 got_fileindex_entry_stage_set(ie, stage);
8064 if (S_ISLNK(sb.st_mode)) {
8065 int is_bad_symlink = 0;
8066 if (!a->allow_bad_symlinks) {
8067 char target_path[PATH_MAX];
8068 ssize_t target_len;
8069 target_len = readlink(ondisk_path, target_path,
8070 sizeof(target_path));
8071 if (target_len == -1) {
8072 err = got_error_from_errno2("readlink",
8073 ondisk_path);
8074 break;
8076 err = is_bad_symlink_target(&is_bad_symlink,
8077 target_path, target_len, ondisk_path,
8078 a->worktree->root_path);
8079 if (err)
8080 break;
8081 if (is_bad_symlink) {
8082 err = got_error_path(ondisk_path,
8083 GOT_ERR_BAD_SYMLINK);
8084 break;
8087 if (is_bad_symlink)
8088 got_fileindex_entry_staged_filetype_set(ie,
8089 GOT_FILEIDX_MODE_BAD_SYMLINK);
8090 else
8091 got_fileindex_entry_staged_filetype_set(ie,
8092 GOT_FILEIDX_MODE_SYMLINK);
8093 } else {
8094 got_fileindex_entry_staged_filetype_set(ie,
8095 GOT_FILEIDX_MODE_REGULAR_FILE);
8097 a->staged_something = 1;
8098 if (a->status_cb == NULL)
8099 break;
8100 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8101 get_staged_status(ie), relpath, blob_id,
8102 new_staged_blob_id, NULL, dirfd, de_name);
8103 break;
8104 case GOT_STATUS_DELETE:
8105 if (staged_status == GOT_STATUS_DELETE)
8106 break;
8107 if (a->patch_cb) {
8108 int choice = GOT_PATCH_CHOICE_NONE;
8109 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8110 ie->path, NULL, 1, 1);
8111 if (err)
8112 break;
8113 if (choice == GOT_PATCH_CHOICE_NO)
8114 break;
8115 if (choice != GOT_PATCH_CHOICE_YES) {
8116 err = got_error(GOT_ERR_PATCH_CHOICE);
8117 break;
8120 stage = GOT_FILEIDX_STAGE_DELETE;
8121 got_fileindex_entry_stage_set(ie, stage);
8122 a->staged_something = 1;
8123 if (a->status_cb == NULL)
8124 break;
8125 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8126 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8127 de_name);
8128 break;
8129 case GOT_STATUS_NO_CHANGE:
8130 break;
8131 case GOT_STATUS_CONFLICT:
8132 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8133 break;
8134 case GOT_STATUS_NONEXISTENT:
8135 err = got_error_set_errno(ENOENT, relpath);
8136 break;
8137 default:
8138 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8139 break;
8142 if (path_content && unlink(path_content) == -1 && err == NULL)
8143 err = got_error_from_errno2("unlink", path_content);
8144 free(path_content);
8145 free(ondisk_path);
8146 free(new_staged_blob_id);
8147 return err;
8150 const struct got_error *
8151 got_worktree_stage(struct got_worktree *worktree,
8152 struct got_pathlist_head *paths,
8153 got_worktree_status_cb status_cb, void *status_arg,
8154 got_worktree_patch_cb patch_cb, void *patch_arg,
8155 int allow_bad_symlinks, struct got_repository *repo)
8157 const struct got_error *err = NULL, *sync_err, *unlockerr;
8158 struct got_pathlist_entry *pe;
8159 struct got_fileindex *fileindex = NULL;
8160 char *fileindex_path = NULL;
8161 struct got_reference *head_ref = NULL;
8162 struct got_object_id *head_commit_id = NULL;
8163 struct check_stage_ok_arg oka;
8164 struct stage_path_arg spa;
8166 err = lock_worktree(worktree, LOCK_EX);
8167 if (err)
8168 return err;
8170 err = got_ref_open(&head_ref, repo,
8171 got_worktree_get_head_ref_name(worktree), 0);
8172 if (err)
8173 goto done;
8174 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8175 if (err)
8176 goto done;
8177 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8178 if (err)
8179 goto done;
8181 /* Check pre-conditions before staging anything. */
8182 oka.head_commit_id = head_commit_id;
8183 oka.worktree = worktree;
8184 oka.fileindex = fileindex;
8185 oka.repo = repo;
8186 oka.have_changes = 0;
8187 TAILQ_FOREACH(pe, paths, entry) {
8188 err = worktree_status(worktree, pe->path, fileindex, repo,
8189 check_stage_ok, &oka, NULL, NULL, 1, 0);
8190 if (err)
8191 goto done;
8193 if (!oka.have_changes) {
8194 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8195 goto done;
8198 spa.worktree = worktree;
8199 spa.fileindex = fileindex;
8200 spa.repo = repo;
8201 spa.patch_cb = patch_cb;
8202 spa.patch_arg = patch_arg;
8203 spa.status_cb = status_cb;
8204 spa.status_arg = status_arg;
8205 spa.staged_something = 0;
8206 spa.allow_bad_symlinks = allow_bad_symlinks;
8207 TAILQ_FOREACH(pe, paths, entry) {
8208 err = worktree_status(worktree, pe->path, fileindex, repo,
8209 stage_path, &spa, NULL, NULL, 1, 0);
8210 if (err)
8211 goto done;
8213 if (!spa.staged_something) {
8214 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8215 goto done;
8218 sync_err = sync_fileindex(fileindex, fileindex_path);
8219 if (sync_err && err == NULL)
8220 err = sync_err;
8221 done:
8222 if (head_ref)
8223 got_ref_close(head_ref);
8224 free(head_commit_id);
8225 free(fileindex_path);
8226 if (fileindex)
8227 got_fileindex_free(fileindex);
8228 unlockerr = lock_worktree(worktree, LOCK_SH);
8229 if (unlockerr && err == NULL)
8230 err = unlockerr;
8231 return err;
8234 struct unstage_path_arg {
8235 struct got_worktree *worktree;
8236 struct got_fileindex *fileindex;
8237 struct got_repository *repo;
8238 got_worktree_checkout_cb progress_cb;
8239 void *progress_arg;
8240 got_worktree_patch_cb patch_cb;
8241 void *patch_arg;
8244 static const struct got_error *
8245 create_unstaged_content(char **path_unstaged_content,
8246 char **path_new_staged_content, struct got_object_id *blob_id,
8247 struct got_object_id *staged_blob_id, const char *relpath,
8248 struct got_repository *repo,
8249 got_worktree_patch_cb patch_cb, void *patch_arg)
8251 const struct got_error *err, *free_err;
8252 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8253 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8254 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8255 struct got_diffreg_result *diffreg_result = NULL;
8256 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8257 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8259 *path_unstaged_content = NULL;
8260 *path_new_staged_content = NULL;
8262 err = got_object_id_str(&label1, blob_id);
8263 if (err)
8264 return err;
8265 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8266 if (err)
8267 goto done;
8269 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8270 if (err)
8271 goto done;
8273 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8274 if (err)
8275 goto done;
8277 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8278 if (err)
8279 goto done;
8281 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8282 if (err)
8283 goto done;
8285 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8286 if (err)
8287 goto done;
8289 err = got_diff_files(&diffreg_result, f1, label1, f2,
8290 path2, 3, 0, 1, NULL);
8291 if (err)
8292 goto done;
8294 err = got_opentemp_named(path_unstaged_content, &outfile,
8295 "got-unstaged-content");
8296 if (err)
8297 goto done;
8298 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8299 "got-new-staged-content");
8300 if (err)
8301 goto done;
8303 if (fseek(f1, 0L, SEEK_SET) == -1) {
8304 err = got_ferror(f1, GOT_ERR_IO);
8305 goto done;
8307 if (fseek(f2, 0L, SEEK_SET) == -1) {
8308 err = got_ferror(f2, GOT_ERR_IO);
8309 goto done;
8311 /* Count the number of actual changes in the diff result. */
8312 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8313 struct diff_chunk_context cc = {};
8314 diff_chunk_context_load_change(&cc, &nchunks_used,
8315 diffreg_result->result, n, 0);
8316 nchanges++;
8318 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8319 int choice;
8320 err = apply_or_reject_change(&choice, &nchunks_used,
8321 diffreg_result->result, n, relpath, f1, f2,
8322 &line_cur1, &line_cur2,
8323 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8324 if (err)
8325 goto done;
8326 if (choice == GOT_PATCH_CHOICE_YES)
8327 have_content = 1;
8328 else
8329 have_rejected_content = 1;
8330 if (choice == GOT_PATCH_CHOICE_QUIT)
8331 break;
8333 if (have_content || have_rejected_content)
8334 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8335 outfile, rejectfile);
8336 done:
8337 free(label1);
8338 if (blob)
8339 got_object_blob_close(blob);
8340 if (staged_blob)
8341 got_object_blob_close(staged_blob);
8342 free_err = got_diffreg_result_free(diffreg_result);
8343 if (free_err && err == NULL)
8344 err = free_err;
8345 if (f1 && fclose(f1) == EOF && err == NULL)
8346 err = got_error_from_errno2("fclose", path1);
8347 if (f2 && fclose(f2) == EOF && err == NULL)
8348 err = got_error_from_errno2("fclose", path2);
8349 if (outfile && fclose(outfile) == EOF && err == NULL)
8350 err = got_error_from_errno2("fclose", *path_unstaged_content);
8351 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8352 err = got_error_from_errno2("fclose", *path_new_staged_content);
8353 if (path1 && unlink(path1) == -1 && err == NULL)
8354 err = got_error_from_errno2("unlink", path1);
8355 if (path2 && unlink(path2) == -1 && err == NULL)
8356 err = got_error_from_errno2("unlink", path2);
8357 if (err || !have_content) {
8358 if (*path_unstaged_content &&
8359 unlink(*path_unstaged_content) == -1 && err == NULL)
8360 err = got_error_from_errno2("unlink",
8361 *path_unstaged_content);
8362 free(*path_unstaged_content);
8363 *path_unstaged_content = NULL;
8365 if (err || !have_content || !have_rejected_content) {
8366 if (*path_new_staged_content &&
8367 unlink(*path_new_staged_content) == -1 && err == NULL)
8368 err = got_error_from_errno2("unlink",
8369 *path_new_staged_content);
8370 free(*path_new_staged_content);
8371 *path_new_staged_content = NULL;
8373 free(path1);
8374 free(path2);
8375 return err;
8378 static const struct got_error *
8379 unstage_hunks(struct got_object_id *staged_blob_id,
8380 struct got_blob_object *blob_base,
8381 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8382 const char *ondisk_path, const char *label_orig,
8383 struct got_worktree *worktree, struct got_repository *repo,
8384 got_worktree_patch_cb patch_cb, void *patch_arg,
8385 got_worktree_checkout_cb progress_cb, void *progress_arg)
8387 const struct got_error *err = NULL;
8388 char *path_unstaged_content = NULL;
8389 char *path_new_staged_content = NULL;
8390 char *parent = NULL, *base_path = NULL;
8391 char *blob_base_path = NULL;
8392 struct got_object_id *new_staged_blob_id = NULL;
8393 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8394 struct stat sb;
8396 err = create_unstaged_content(&path_unstaged_content,
8397 &path_new_staged_content, blob_id, staged_blob_id,
8398 ie->path, repo, patch_cb, patch_arg);
8399 if (err)
8400 return err;
8402 if (path_unstaged_content == NULL)
8403 return NULL;
8405 if (path_new_staged_content) {
8406 err = got_object_blob_create(&new_staged_blob_id,
8407 path_new_staged_content, repo);
8408 if (err)
8409 goto done;
8412 f = fopen(path_unstaged_content, "r");
8413 if (f == NULL) {
8414 err = got_error_from_errno2("fopen",
8415 path_unstaged_content);
8416 goto done;
8418 if (fstat(fileno(f), &sb) == -1) {
8419 err = got_error_from_errno2("fstat", path_unstaged_content);
8420 goto done;
8422 if (got_fileindex_entry_staged_filetype_get(ie) ==
8423 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8424 char link_target[PATH_MAX];
8425 size_t r;
8426 r = fread(link_target, 1, sizeof(link_target), f);
8427 if (r == 0 && ferror(f)) {
8428 err = got_error_from_errno("fread");
8429 goto done;
8431 if (r >= sizeof(link_target)) { /* should not happen */
8432 err = got_error(GOT_ERR_NO_SPACE);
8433 goto done;
8435 link_target[r] = '\0';
8436 err = merge_symlink(worktree, blob_base,
8437 ondisk_path, ie->path, label_orig, link_target,
8438 worktree->base_commit_id, repo, progress_cb,
8439 progress_arg);
8440 } else {
8441 int local_changes_subsumed;
8443 err = got_path_dirname(&parent, ondisk_path);
8444 if (err)
8445 return err;
8447 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8448 parent) == -1) {
8449 err = got_error_from_errno("asprintf");
8450 base_path = NULL;
8451 goto done;
8454 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8455 if (err)
8456 goto done;
8457 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8458 blob_base);
8459 if (err)
8460 goto done;
8463 * In order the run a 3-way merge with a symlink we copy the symlink's
8464 * target path into a temporary file and use that file with diff3.
8466 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8467 err = dump_symlink_target_path_to_file(&f_deriv2,
8468 ondisk_path);
8469 if (err)
8470 goto done;
8471 } else {
8472 int fd;
8473 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
8474 if (fd == -1) {
8475 err = got_error_from_errno2("open", ondisk_path);
8476 goto done;
8478 f_deriv2 = fdopen(fd, "r");
8479 if (f_deriv2 == NULL) {
8480 err = got_error_from_errno2("fdopen", ondisk_path);
8481 close(fd);
8482 goto done;
8486 err = merge_file(&local_changes_subsumed, worktree,
8487 f_base, f, f_deriv2, ondisk_path, ie->path,
8488 got_fileindex_perms_to_st(ie),
8489 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8490 repo, progress_cb, progress_arg);
8492 if (err)
8493 goto done;
8495 if (new_staged_blob_id) {
8496 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8497 SHA1_DIGEST_LENGTH);
8498 } else {
8499 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8500 got_fileindex_entry_staged_filetype_set(ie, 0);
8502 done:
8503 free(new_staged_blob_id);
8504 if (path_unstaged_content &&
8505 unlink(path_unstaged_content) == -1 && err == NULL)
8506 err = got_error_from_errno2("unlink", path_unstaged_content);
8507 if (path_new_staged_content &&
8508 unlink(path_new_staged_content) == -1 && err == NULL)
8509 err = got_error_from_errno2("unlink", path_new_staged_content);
8510 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8511 err = got_error_from_errno2("unlink", blob_base_path);
8512 if (f_base && fclose(f_base) == EOF && err == NULL)
8513 err = got_error_from_errno2("fclose", path_unstaged_content);
8514 if (f && fclose(f) == EOF && err == NULL)
8515 err = got_error_from_errno2("fclose", path_unstaged_content);
8516 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8517 err = got_error_from_errno2("fclose", ondisk_path);
8518 free(path_unstaged_content);
8519 free(path_new_staged_content);
8520 free(blob_base_path);
8521 free(parent);
8522 free(base_path);
8523 return err;
8526 static const struct got_error *
8527 unstage_path(void *arg, unsigned char status,
8528 unsigned char staged_status, const char *relpath,
8529 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8530 struct got_object_id *commit_id, int dirfd, const char *de_name)
8532 const struct got_error *err = NULL;
8533 struct unstage_path_arg *a = arg;
8534 struct got_fileindex_entry *ie;
8535 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8536 char *ondisk_path = NULL;
8537 char *id_str = NULL, *label_orig = NULL;
8538 int local_changes_subsumed;
8539 struct stat sb;
8541 if (staged_status != GOT_STATUS_ADD &&
8542 staged_status != GOT_STATUS_MODIFY &&
8543 staged_status != GOT_STATUS_DELETE)
8544 return NULL;
8546 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8547 if (ie == NULL)
8548 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8550 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8551 == -1)
8552 return got_error_from_errno("asprintf");
8554 err = got_object_id_str(&id_str,
8555 commit_id ? commit_id : a->worktree->base_commit_id);
8556 if (err)
8557 goto done;
8558 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8559 id_str) == -1) {
8560 err = got_error_from_errno("asprintf");
8561 goto done;
8564 switch (staged_status) {
8565 case GOT_STATUS_MODIFY:
8566 err = got_object_open_as_blob(&blob_base, a->repo,
8567 blob_id, 8192);
8568 if (err)
8569 break;
8570 /* fall through */
8571 case GOT_STATUS_ADD:
8572 if (a->patch_cb) {
8573 if (staged_status == GOT_STATUS_ADD) {
8574 int choice = GOT_PATCH_CHOICE_NONE;
8575 err = (*a->patch_cb)(&choice, a->patch_arg,
8576 staged_status, ie->path, NULL, 1, 1);
8577 if (err)
8578 break;
8579 if (choice != GOT_PATCH_CHOICE_YES)
8580 break;
8581 } else {
8582 err = unstage_hunks(staged_blob_id,
8583 blob_base, blob_id, ie, ondisk_path,
8584 label_orig, a->worktree, a->repo,
8585 a->patch_cb, a->patch_arg,
8586 a->progress_cb, a->progress_arg);
8587 break; /* Done with this file. */
8590 err = got_object_open_as_blob(&blob_staged, a->repo,
8591 staged_blob_id, 8192);
8592 if (err)
8593 break;
8594 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8595 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8596 case GOT_FILEIDX_MODE_REGULAR_FILE:
8597 err = merge_blob(&local_changes_subsumed, a->worktree,
8598 blob_base, ondisk_path, relpath,
8599 got_fileindex_perms_to_st(ie), label_orig,
8600 blob_staged, commit_id ? commit_id :
8601 a->worktree->base_commit_id, a->repo,
8602 a->progress_cb, a->progress_arg);
8603 break;
8604 case GOT_FILEIDX_MODE_SYMLINK:
8605 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8606 char *staged_target;
8607 err = got_object_blob_read_to_str(
8608 &staged_target, blob_staged);
8609 if (err)
8610 goto done;
8611 err = merge_symlink(a->worktree, blob_base,
8612 ondisk_path, relpath, label_orig,
8613 staged_target, commit_id ? commit_id :
8614 a->worktree->base_commit_id,
8615 a->repo, a->progress_cb, a->progress_arg);
8616 free(staged_target);
8617 } else {
8618 err = merge_blob(&local_changes_subsumed,
8619 a->worktree, blob_base, ondisk_path,
8620 relpath, got_fileindex_perms_to_st(ie),
8621 label_orig, blob_staged,
8622 commit_id ? commit_id :
8623 a->worktree->base_commit_id, a->repo,
8624 a->progress_cb, a->progress_arg);
8626 break;
8627 default:
8628 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8629 break;
8631 if (err == NULL) {
8632 got_fileindex_entry_stage_set(ie,
8633 GOT_FILEIDX_STAGE_NONE);
8634 got_fileindex_entry_staged_filetype_set(ie, 0);
8636 break;
8637 case GOT_STATUS_DELETE:
8638 if (a->patch_cb) {
8639 int choice = GOT_PATCH_CHOICE_NONE;
8640 err = (*a->patch_cb)(&choice, a->patch_arg,
8641 staged_status, ie->path, NULL, 1, 1);
8642 if (err)
8643 break;
8644 if (choice == GOT_PATCH_CHOICE_NO)
8645 break;
8646 if (choice != GOT_PATCH_CHOICE_YES) {
8647 err = got_error(GOT_ERR_PATCH_CHOICE);
8648 break;
8651 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8652 got_fileindex_entry_staged_filetype_set(ie, 0);
8653 err = get_file_status(&status, &sb, ie, ondisk_path,
8654 dirfd, de_name, a->repo);
8655 if (err)
8656 break;
8657 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8658 break;
8660 done:
8661 free(ondisk_path);
8662 if (blob_base)
8663 got_object_blob_close(blob_base);
8664 if (blob_staged)
8665 got_object_blob_close(blob_staged);
8666 free(id_str);
8667 free(label_orig);
8668 return err;
8671 const struct got_error *
8672 got_worktree_unstage(struct got_worktree *worktree,
8673 struct got_pathlist_head *paths,
8674 got_worktree_checkout_cb progress_cb, void *progress_arg,
8675 got_worktree_patch_cb patch_cb, void *patch_arg,
8676 struct got_repository *repo)
8678 const struct got_error *err = NULL, *sync_err, *unlockerr;
8679 struct got_pathlist_entry *pe;
8680 struct got_fileindex *fileindex = NULL;
8681 char *fileindex_path = NULL;
8682 struct unstage_path_arg upa;
8684 err = lock_worktree(worktree, LOCK_EX);
8685 if (err)
8686 return err;
8688 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8689 if (err)
8690 goto done;
8692 upa.worktree = worktree;
8693 upa.fileindex = fileindex;
8694 upa.repo = repo;
8695 upa.progress_cb = progress_cb;
8696 upa.progress_arg = progress_arg;
8697 upa.patch_cb = patch_cb;
8698 upa.patch_arg = patch_arg;
8699 TAILQ_FOREACH(pe, paths, entry) {
8700 err = worktree_status(worktree, pe->path, fileindex, repo,
8701 unstage_path, &upa, NULL, NULL, 1, 0);
8702 if (err)
8703 goto done;
8706 sync_err = sync_fileindex(fileindex, fileindex_path);
8707 if (sync_err && err == NULL)
8708 err = sync_err;
8709 done:
8710 free(fileindex_path);
8711 if (fileindex)
8712 got_fileindex_free(fileindex);
8713 unlockerr = lock_worktree(worktree, LOCK_SH);
8714 if (unlockerr && err == NULL)
8715 err = unlockerr;
8716 return err;
8719 struct report_file_info_arg {
8720 struct got_worktree *worktree;
8721 got_worktree_path_info_cb info_cb;
8722 void *info_arg;
8723 struct got_pathlist_head *paths;
8724 got_cancel_cb cancel_cb;
8725 void *cancel_arg;
8728 static const struct got_error *
8729 report_file_info(void *arg, struct got_fileindex_entry *ie)
8731 struct report_file_info_arg *a = arg;
8732 struct got_pathlist_entry *pe;
8733 struct got_object_id blob_id, staged_blob_id, commit_id;
8734 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8735 struct got_object_id *commit_idp = NULL;
8736 int stage;
8738 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8739 return got_error(GOT_ERR_CANCELLED);
8741 TAILQ_FOREACH(pe, a->paths, entry) {
8742 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8743 got_path_is_child(ie->path, pe->path, pe->path_len))
8744 break;
8746 if (pe == NULL) /* not found */
8747 return NULL;
8749 if (got_fileindex_entry_has_blob(ie)) {
8750 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8751 blob_idp = &blob_id;
8753 stage = got_fileindex_entry_stage_get(ie);
8754 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8755 stage == GOT_FILEIDX_STAGE_ADD) {
8756 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8757 SHA1_DIGEST_LENGTH);
8758 staged_blob_idp = &staged_blob_id;
8761 if (got_fileindex_entry_has_commit(ie)) {
8762 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8763 commit_idp = &commit_id;
8766 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8767 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8770 const struct got_error *
8771 got_worktree_path_info(struct got_worktree *worktree,
8772 struct got_pathlist_head *paths,
8773 got_worktree_path_info_cb info_cb, void *info_arg,
8774 got_cancel_cb cancel_cb, void *cancel_arg)
8777 const struct got_error *err = NULL, *unlockerr;
8778 struct got_fileindex *fileindex = NULL;
8779 char *fileindex_path = NULL;
8780 struct report_file_info_arg arg;
8782 err = lock_worktree(worktree, LOCK_SH);
8783 if (err)
8784 return err;
8786 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8787 if (err)
8788 goto done;
8790 arg.worktree = worktree;
8791 arg.info_cb = info_cb;
8792 arg.info_arg = info_arg;
8793 arg.paths = paths;
8794 arg.cancel_cb = cancel_cb;
8795 arg.cancel_arg = cancel_arg;
8796 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8797 &arg);
8798 done:
8799 free(fileindex_path);
8800 if (fileindex)
8801 got_fileindex_free(fileindex);
8802 unlockerr = lock_worktree(worktree, LOCK_UN);
8803 if (unlockerr && err == NULL)
8804 err = unlockerr;
8805 return err;