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 (errno != ELOOP)
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, struct got_repository *repo,
1376 got_worktree_checkout_cb progress_cb, void *progress_arg)
1378 const struct got_error *err = NULL;
1379 char target_path[PATH_MAX];
1380 size_t len, target_len = 0;
1381 char *path_got = NULL;
1382 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1383 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1385 *is_bad_symlink = 0;
1388 * Blob object content specifies the target path of the link.
1389 * If a symbolic link cannot be installed we instead create
1390 * a regular file which contains the link target path stored
1391 * in the blob object.
1393 do {
1394 err = got_object_blob_read_block(&len, blob);
1395 if (len + target_len >= sizeof(target_path)) {
1396 /* Path too long; install as a regular file. */
1397 *is_bad_symlink = 1;
1398 got_object_blob_rewind(blob);
1399 return install_blob(worktree, ondisk_path, path,
1400 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1401 restoring_missing_file, reverting_versioned_file,
1402 1, path_is_unversioned, repo, progress_cb,
1403 progress_arg);
1405 if (len > 0) {
1406 /* Skip blob object header first time around. */
1407 memcpy(target_path + target_len, buf + hdrlen,
1408 len - hdrlen);
1409 target_len += len - hdrlen;
1410 hdrlen = 0;
1412 } while (len != 0);
1413 target_path[target_len] = '\0';
1415 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1416 ondisk_path, worktree->root_path);
1417 if (err)
1418 return err;
1420 if (*is_bad_symlink) {
1421 /* install as a regular file */
1422 got_object_blob_rewind(blob);
1423 err = install_blob(worktree, ondisk_path, path,
1424 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1425 restoring_missing_file, reverting_versioned_file, 1,
1426 path_is_unversioned, repo, progress_cb, progress_arg);
1427 goto done;
1430 if (symlink(target_path, ondisk_path) == -1) {
1431 if (errno == EEXIST) {
1432 int symlink_replaced;
1433 if (path_is_unversioned) {
1434 err = (*progress_cb)(progress_arg,
1435 GOT_STATUS_UNVERSIONED, path);
1436 goto done;
1438 err = replace_existing_symlink(&symlink_replaced,
1439 ondisk_path, target_path, target_len);
1440 if (err)
1441 goto done;
1442 if (progress_cb) {
1443 if (symlink_replaced) {
1444 err = (*progress_cb)(progress_arg,
1445 reverting_versioned_file ?
1446 GOT_STATUS_REVERT :
1447 GOT_STATUS_UPDATE, path);
1448 } else {
1449 err = (*progress_cb)(progress_arg,
1450 GOT_STATUS_EXISTS, path);
1453 goto done; /* Nothing else to do. */
1456 if (errno == ENOENT) {
1457 char *parent;
1458 err = got_path_dirname(&parent, ondisk_path);
1459 if (err)
1460 goto done;
1461 err = add_dir_on_disk(worktree, parent);
1462 free(parent);
1463 if (err)
1464 goto done;
1466 * Retry, and fall through to error handling
1467 * below if this second attempt fails.
1469 if (symlink(target_path, ondisk_path) != -1) {
1470 err = NULL; /* success */
1471 goto done;
1475 /* Handle errors from first or second creation attempt. */
1476 if (errno == ENAMETOOLONG) {
1477 /* bad target path; install as a regular file */
1478 *is_bad_symlink = 1;
1479 got_object_blob_rewind(blob);
1480 err = install_blob(worktree, ondisk_path, path,
1481 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1482 restoring_missing_file, reverting_versioned_file, 1,
1483 path_is_unversioned, repo,
1484 progress_cb, progress_arg);
1485 } else if (errno == ENOTDIR) {
1486 err = got_error_path(ondisk_path,
1487 GOT_ERR_FILE_OBSTRUCTED);
1488 } else {
1489 err = got_error_from_errno3("symlink",
1490 target_path, ondisk_path);
1492 } else if (progress_cb)
1493 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1494 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1495 done:
1496 free(path_got);
1497 return err;
1500 static const struct got_error *
1501 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1502 const char *path, mode_t te_mode, mode_t st_mode,
1503 struct got_blob_object *blob, int restoring_missing_file,
1504 int reverting_versioned_file, int installing_bad_symlink,
1505 int path_is_unversioned, struct got_repository *repo,
1506 got_worktree_checkout_cb progress_cb, void *progress_arg)
1508 const struct got_error *err = NULL;
1509 int fd = -1;
1510 size_t len, hdrlen;
1511 int update = 0;
1512 char *tmppath = NULL;
1514 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1515 GOT_DEFAULT_FILE_MODE);
1516 if (fd == -1) {
1517 if (errno == ENOENT) {
1518 char *parent;
1519 err = got_path_dirname(&parent, path);
1520 if (err)
1521 return err;
1522 err = add_dir_on_disk(worktree, parent);
1523 free(parent);
1524 if (err)
1525 return err;
1526 fd = open(ondisk_path,
1527 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1528 GOT_DEFAULT_FILE_MODE);
1529 if (fd == -1)
1530 return got_error_from_errno2("open",
1531 ondisk_path);
1532 } else if (errno == EEXIST) {
1533 if (path_is_unversioned) {
1534 err = (*progress_cb)(progress_arg,
1535 GOT_STATUS_UNVERSIONED, path);
1536 goto done;
1538 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1539 !S_ISREG(st_mode) && !installing_bad_symlink) {
1540 /* TODO file is obstructed; do something */
1541 err = got_error_path(ondisk_path,
1542 GOT_ERR_FILE_OBSTRUCTED);
1543 goto done;
1544 } else {
1545 err = got_opentemp_named_fd(&tmppath, &fd,
1546 ondisk_path);
1547 if (err)
1548 goto done;
1549 update = 1;
1551 } else
1552 return got_error_from_errno2("open", ondisk_path);
1555 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1556 err = got_error_from_errno2("fchmod",
1557 update ? tmppath : ondisk_path);
1558 goto done;
1561 if (progress_cb) {
1562 if (restoring_missing_file)
1563 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1564 path);
1565 else if (reverting_versioned_file)
1566 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1567 path);
1568 else
1569 err = (*progress_cb)(progress_arg,
1570 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1571 if (err)
1572 goto done;
1575 hdrlen = got_object_blob_get_hdrlen(blob);
1576 do {
1577 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1578 err = got_object_blob_read_block(&len, blob);
1579 if (err)
1580 break;
1581 if (len > 0) {
1582 /* Skip blob object header first time around. */
1583 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1584 if (outlen == -1) {
1585 err = got_error_from_errno("write");
1586 goto done;
1587 } else if (outlen != len - hdrlen) {
1588 err = got_error(GOT_ERR_IO);
1589 goto done;
1591 hdrlen = 0;
1593 } while (len != 0);
1595 if (fsync(fd) != 0) {
1596 err = got_error_from_errno("fsync");
1597 goto done;
1600 if (update) {
1601 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1602 err = got_error_from_errno2("unlink", ondisk_path);
1603 goto done;
1605 if (rename(tmppath, ondisk_path) != 0) {
1606 err = got_error_from_errno3("rename", tmppath,
1607 ondisk_path);
1608 goto done;
1610 free(tmppath);
1611 tmppath = NULL;
1614 done:
1615 if (fd != -1 && close(fd) == -1 && err == NULL)
1616 err = got_error_from_errno("close");
1617 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1618 err = got_error_from_errno2("unlink", tmppath);
1619 free(tmppath);
1620 return err;
1623 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1624 static const struct got_error *
1625 get_modified_file_content_status(unsigned char *status, FILE *f)
1627 const struct got_error *err = NULL;
1628 const char *markers[3] = {
1629 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1630 GOT_DIFF_CONFLICT_MARKER_SEP,
1631 GOT_DIFF_CONFLICT_MARKER_END
1633 int i = 0;
1634 char *line = NULL;
1635 size_t linesize = 0;
1636 ssize_t linelen;
1638 while (*status == GOT_STATUS_MODIFY) {
1639 linelen = getline(&line, &linesize, f);
1640 if (linelen == -1) {
1641 if (feof(f))
1642 break;
1643 err = got_ferror(f, GOT_ERR_IO);
1644 break;
1647 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1648 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1649 == 0)
1650 *status = GOT_STATUS_CONFLICT;
1651 else
1652 i++;
1655 free(line);
1657 return err;
1660 static int
1661 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1663 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1664 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1667 static int
1668 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1670 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1671 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1672 ie->mtime_sec == sb->st_mtim.tv_sec &&
1673 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1674 ie->size == (sb->st_size & 0xffffffff) &&
1675 !xbit_differs(ie, sb->st_mode));
1678 static unsigned char
1679 get_staged_status(struct got_fileindex_entry *ie)
1681 switch (got_fileindex_entry_stage_get(ie)) {
1682 case GOT_FILEIDX_STAGE_ADD:
1683 return GOT_STATUS_ADD;
1684 case GOT_FILEIDX_STAGE_DELETE:
1685 return GOT_STATUS_DELETE;
1686 case GOT_FILEIDX_STAGE_MODIFY:
1687 return GOT_STATUS_MODIFY;
1688 default:
1689 return GOT_STATUS_NO_CHANGE;
1693 static const struct got_error *
1694 get_symlink_modification_status(unsigned char *status,
1695 struct got_fileindex_entry *ie, const char *abspath,
1696 int dirfd, const char *de_name, struct got_blob_object *blob)
1698 const struct got_error *err = NULL;
1699 char target_path[PATH_MAX];
1700 char etarget[PATH_MAX];
1701 ssize_t elen;
1702 size_t len, target_len = 0;
1703 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1704 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1706 *status = GOT_STATUS_NO_CHANGE;
1708 /* Blob object content specifies the target path of the link. */
1709 do {
1710 err = got_object_blob_read_block(&len, blob);
1711 if (err)
1712 return err;
1713 if (len + target_len >= sizeof(target_path)) {
1715 * Should not happen. The blob contents were OK
1716 * when this symlink was installed.
1718 return got_error(GOT_ERR_NO_SPACE);
1720 if (len > 0) {
1721 /* Skip blob object header first time around. */
1722 memcpy(target_path + target_len, buf + hdrlen,
1723 len - hdrlen);
1724 target_len += len - hdrlen;
1725 hdrlen = 0;
1727 } while (len != 0);
1728 target_path[target_len] = '\0';
1730 if (dirfd != -1) {
1731 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1732 if (elen == -1)
1733 return got_error_from_errno2("readlinkat", abspath);
1734 } else {
1735 elen = readlink(abspath, etarget, sizeof(etarget));
1736 if (elen == -1)
1737 return got_error_from_errno2("readlink", abspath);
1740 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1741 *status = GOT_STATUS_MODIFY;
1743 return NULL;
1746 static const struct got_error *
1747 get_file_status(unsigned char *status, struct stat *sb,
1748 struct got_fileindex_entry *ie, const char *abspath,
1749 int dirfd, const char *de_name, struct got_repository *repo)
1751 const struct got_error *err = NULL;
1752 struct got_object_id id;
1753 size_t hdrlen;
1754 int fd = -1;
1755 FILE *f = NULL;
1756 uint8_t fbuf[8192];
1757 struct got_blob_object *blob = NULL;
1758 size_t flen, blen;
1759 unsigned char staged_status = get_staged_status(ie);
1761 *status = GOT_STATUS_NO_CHANGE;
1762 memset(sb, 0, sizeof(*sb));
1765 * Whenever the caller provides a directory descriptor and a
1766 * directory entry name for the file, use them! This prevents
1767 * race conditions if filesystem paths change beneath our feet.
1769 if (dirfd != -1) {
1770 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1771 if (errno == ENOENT) {
1772 if (got_fileindex_entry_has_file_on_disk(ie))
1773 *status = GOT_STATUS_MISSING;
1774 else
1775 *status = GOT_STATUS_DELETE;
1776 goto done;
1778 err = got_error_from_errno2("fstatat", abspath);
1779 goto done;
1781 } else {
1782 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1783 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1784 return got_error_from_errno2("open", abspath);
1785 else if (fd == -1 && errno == ELOOP) {
1786 if (lstat(abspath, sb) == -1)
1787 return got_error_from_errno2("lstat", abspath);
1788 } else if (fd == -1 || fstat(fd, sb) == -1) {
1789 if (errno == ENOENT) {
1790 if (got_fileindex_entry_has_file_on_disk(ie))
1791 *status = GOT_STATUS_MISSING;
1792 else
1793 *status = GOT_STATUS_DELETE;
1794 goto done;
1796 err = got_error_from_errno2("fstat", abspath);
1797 goto done;
1801 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1802 *status = GOT_STATUS_OBSTRUCTED;
1803 goto done;
1806 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1807 *status = GOT_STATUS_DELETE;
1808 goto done;
1809 } else if (!got_fileindex_entry_has_blob(ie) &&
1810 staged_status != GOT_STATUS_ADD) {
1811 *status = GOT_STATUS_ADD;
1812 goto done;
1815 if (!stat_info_differs(ie, sb))
1816 goto done;
1818 if (S_ISLNK(sb->st_mode) &&
1819 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1820 *status = GOT_STATUS_MODIFY;
1821 goto done;
1824 if (staged_status == GOT_STATUS_MODIFY ||
1825 staged_status == GOT_STATUS_ADD)
1826 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1827 else
1828 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1830 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1831 if (err)
1832 goto done;
1834 if (S_ISLNK(sb->st_mode)) {
1835 err = get_symlink_modification_status(status, ie,
1836 abspath, dirfd, de_name, blob);
1837 goto done;
1840 if (dirfd != -1) {
1841 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1842 if (fd == -1) {
1843 err = got_error_from_errno2("openat", abspath);
1844 goto done;
1848 f = fdopen(fd, "r");
1849 if (f == NULL) {
1850 err = got_error_from_errno2("fdopen", abspath);
1851 goto done;
1853 fd = -1;
1854 hdrlen = got_object_blob_get_hdrlen(blob);
1855 for (;;) {
1856 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1857 err = got_object_blob_read_block(&blen, blob);
1858 if (err)
1859 goto done;
1860 /* Skip length of blob object header first time around. */
1861 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1862 if (flen == 0 && ferror(f)) {
1863 err = got_error_from_errno("fread");
1864 goto done;
1866 if (blen - hdrlen == 0) {
1867 if (flen != 0)
1868 *status = GOT_STATUS_MODIFY;
1869 break;
1870 } else if (flen == 0) {
1871 if (blen - hdrlen != 0)
1872 *status = GOT_STATUS_MODIFY;
1873 break;
1874 } else if (blen - hdrlen == flen) {
1875 /* Skip blob object header first time around. */
1876 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1877 *status = GOT_STATUS_MODIFY;
1878 break;
1880 } else {
1881 *status = GOT_STATUS_MODIFY;
1882 break;
1884 hdrlen = 0;
1887 if (*status == GOT_STATUS_MODIFY) {
1888 rewind(f);
1889 err = get_modified_file_content_status(status, f);
1890 } else if (xbit_differs(ie, sb->st_mode))
1891 *status = GOT_STATUS_MODE_CHANGE;
1892 done:
1893 if (blob)
1894 got_object_blob_close(blob);
1895 if (f != NULL && fclose(f) == EOF && err == NULL)
1896 err = got_error_from_errno2("fclose", abspath);
1897 if (fd != -1 && close(fd) == -1 && err == NULL)
1898 err = got_error_from_errno2("close", abspath);
1899 return err;
1903 * Update timestamps in the file index if a file is unmodified and
1904 * we had to run a full content comparison to find out.
1906 static const struct got_error *
1907 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1908 struct got_fileindex_entry *ie, struct stat *sb)
1910 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1911 return got_fileindex_entry_update(ie, wt_fd, path,
1912 ie->blob_sha1, ie->commit_sha1, 1);
1914 return NULL;
1917 static const struct got_error *
1918 update_blob(struct got_worktree *worktree,
1919 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1920 struct got_tree_entry *te, const char *path,
1921 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1922 void *progress_arg)
1924 const struct got_error *err = NULL;
1925 struct got_blob_object *blob = NULL;
1926 char *ondisk_path;
1927 unsigned char status = GOT_STATUS_NO_CHANGE;
1928 struct stat sb;
1930 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1931 return got_error_from_errno("asprintf");
1933 if (ie) {
1934 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1935 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1936 goto done;
1938 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1939 repo);
1940 if (err)
1941 goto done;
1942 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1943 sb.st_mode = got_fileindex_perms_to_st(ie);
1944 } else {
1945 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1946 status = GOT_STATUS_UNVERSIONED;
1949 if (status == GOT_STATUS_OBSTRUCTED) {
1950 if (ie)
1951 got_fileindex_entry_mark_skipped(ie);
1952 err = (*progress_cb)(progress_arg, status, path);
1953 goto done;
1955 if (status == GOT_STATUS_CONFLICT) {
1956 if (ie)
1957 got_fileindex_entry_mark_skipped(ie);
1958 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1959 path);
1960 goto done;
1963 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1964 (S_ISLNK(te->mode) ||
1965 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1967 * This is a regular file or an installed bad symlink.
1968 * If the file index indicates that this file is already
1969 * up-to-date with respect to the repository we can skip
1970 * updating contents of this file.
1972 if (got_fileindex_entry_has_commit(ie) &&
1973 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1974 SHA1_DIGEST_LENGTH) == 0) {
1975 /* Same commit. */
1976 err = sync_timestamps(worktree->root_fd,
1977 path, status, ie, &sb);
1978 if (err)
1979 goto done;
1980 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1981 path);
1982 goto done;
1984 if (got_fileindex_entry_has_blob(ie) &&
1985 memcmp(ie->blob_sha1, te->id.sha1,
1986 SHA1_DIGEST_LENGTH) == 0) {
1987 /* Different commit but the same blob. */
1988 err = sync_timestamps(worktree->root_fd,
1989 path, status, ie, &sb);
1990 if (err)
1991 goto done;
1992 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1993 path);
1994 goto done;
1998 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1999 if (err)
2000 goto done;
2002 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2003 int update_timestamps;
2004 struct got_blob_object *blob2 = NULL;
2005 char *label_orig = NULL;
2006 if (got_fileindex_entry_has_blob(ie)) {
2007 struct got_object_id id2;
2008 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2009 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2010 if (err)
2011 goto done;
2013 if (got_fileindex_entry_has_commit(ie)) {
2014 char id_str[SHA1_DIGEST_STRING_LENGTH];
2015 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2016 sizeof(id_str)) == NULL) {
2017 err = got_error_path(id_str,
2018 GOT_ERR_BAD_OBJ_ID_STR);
2019 goto done;
2021 if (asprintf(&label_orig, "%s: commit %s",
2022 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2023 err = got_error_from_errno("asprintf");
2024 goto done;
2027 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2028 char *link_target;
2029 err = got_object_blob_read_to_str(&link_target, blob);
2030 if (err)
2031 goto done;
2032 err = merge_symlink(worktree, blob2, ondisk_path, path,
2033 label_orig, link_target, worktree->base_commit_id,
2034 repo, progress_cb, progress_arg);
2035 free(link_target);
2036 } else {
2037 err = merge_blob(&update_timestamps, worktree, blob2,
2038 ondisk_path, path, sb.st_mode, label_orig, blob,
2039 worktree->base_commit_id, repo,
2040 progress_cb, progress_arg);
2042 free(label_orig);
2043 if (blob2)
2044 got_object_blob_close(blob2);
2045 if (err)
2046 goto done;
2048 * Do not update timestamps of files with local changes.
2049 * Otherwise, a future status walk would treat them as
2050 * unmodified files again.
2052 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2053 blob->id.sha1, worktree->base_commit_id->sha1,
2054 update_timestamps);
2055 } else if (status == GOT_STATUS_MODE_CHANGE) {
2056 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2057 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2058 } else if (status == GOT_STATUS_DELETE) {
2059 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2060 if (err)
2061 goto done;
2062 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2063 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2064 if (err)
2065 goto done;
2066 } else {
2067 int is_bad_symlink = 0;
2068 if (S_ISLNK(te->mode)) {
2069 err = install_symlink(&is_bad_symlink, worktree,
2070 ondisk_path, path, blob,
2071 status == GOT_STATUS_MISSING, 0,
2072 status == GOT_STATUS_UNVERSIONED, repo,
2073 progress_cb, progress_arg);
2074 } else {
2075 err = install_blob(worktree, ondisk_path, path,
2076 te->mode, sb.st_mode, blob,
2077 status == GOT_STATUS_MISSING, 0, 0,
2078 status == GOT_STATUS_UNVERSIONED, repo,
2079 progress_cb, progress_arg);
2081 if (err)
2082 goto done;
2084 if (ie) {
2085 err = got_fileindex_entry_update(ie,
2086 worktree->root_fd, path, blob->id.sha1,
2087 worktree->base_commit_id->sha1, 1);
2088 } else {
2089 err = create_fileindex_entry(&ie, fileindex,
2090 worktree->base_commit_id, worktree->root_fd, path,
2091 &blob->id);
2093 if (err)
2094 goto done;
2096 if (is_bad_symlink) {
2097 got_fileindex_entry_filetype_set(ie,
2098 GOT_FILEIDX_MODE_BAD_SYMLINK);
2101 got_object_blob_close(blob);
2102 done:
2103 free(ondisk_path);
2104 return err;
2107 static const struct got_error *
2108 remove_ondisk_file(const char *root_path, const char *path)
2110 const struct got_error *err = NULL;
2111 char *ondisk_path = NULL, *parent = NULL;
2113 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2114 return got_error_from_errno("asprintf");
2116 if (unlink(ondisk_path) == -1) {
2117 if (errno != ENOENT)
2118 err = got_error_from_errno2("unlink", ondisk_path);
2119 } else {
2120 size_t root_len = strlen(root_path);
2121 err = got_path_dirname(&parent, ondisk_path);
2122 if (err)
2123 goto done;
2124 while (got_path_cmp(parent, root_path,
2125 strlen(parent), root_len) != 0) {
2126 free(ondisk_path);
2127 ondisk_path = parent;
2128 parent = NULL;
2129 if (rmdir(ondisk_path) == -1) {
2130 if (errno != ENOTEMPTY)
2131 err = got_error_from_errno2("rmdir",
2132 ondisk_path);
2133 break;
2135 err = got_path_dirname(&parent, ondisk_path);
2136 if (err)
2137 break;
2140 done:
2141 free(ondisk_path);
2142 free(parent);
2143 return err;
2146 static const struct got_error *
2147 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2148 struct got_fileindex_entry *ie, struct got_repository *repo,
2149 got_worktree_checkout_cb progress_cb, void *progress_arg)
2151 const struct got_error *err = NULL;
2152 unsigned char status;
2153 struct stat sb;
2154 char *ondisk_path;
2156 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2157 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2159 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2160 == -1)
2161 return got_error_from_errno("asprintf");
2163 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2164 if (err)
2165 goto done;
2167 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2168 char ondisk_target[PATH_MAX];
2169 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2170 sizeof(ondisk_target));
2171 if (ondisk_len == -1) {
2172 err = got_error_from_errno2("readlink", ondisk_path);
2173 goto done;
2175 ondisk_target[ondisk_len] = '\0';
2176 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2177 NULL, NULL, /* XXX pass common ancestor info? */
2178 ondisk_target, ondisk_path);
2179 if (err)
2180 goto done;
2181 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2182 ie->path);
2183 goto done;
2186 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2187 status == GOT_STATUS_ADD) {
2188 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2189 if (err)
2190 goto done;
2192 * Preserve the working file and change the deleted blob's
2193 * entry into a schedule-add entry.
2195 err = got_fileindex_entry_update(ie, worktree->root_fd,
2196 ie->path, NULL, NULL, 0);
2197 } else {
2198 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2199 if (err)
2200 goto done;
2201 if (status == GOT_STATUS_NO_CHANGE) {
2202 err = remove_ondisk_file(worktree->root_path, ie->path);
2203 if (err)
2204 goto done;
2206 got_fileindex_entry_remove(fileindex, ie);
2208 done:
2209 free(ondisk_path);
2210 return err;
2213 struct diff_cb_arg {
2214 struct got_fileindex *fileindex;
2215 struct got_worktree *worktree;
2216 struct got_repository *repo;
2217 got_worktree_checkout_cb progress_cb;
2218 void *progress_arg;
2219 got_cancel_cb cancel_cb;
2220 void *cancel_arg;
2223 static const struct got_error *
2224 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2225 struct got_tree_entry *te, const char *parent_path)
2227 struct diff_cb_arg *a = arg;
2229 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2230 return got_error(GOT_ERR_CANCELLED);
2232 return update_blob(a->worktree, a->fileindex, ie, te,
2233 ie->path, a->repo, a->progress_cb, a->progress_arg);
2236 static const struct got_error *
2237 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2239 struct diff_cb_arg *a = arg;
2241 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2242 return got_error(GOT_ERR_CANCELLED);
2244 return delete_blob(a->worktree, a->fileindex, ie,
2245 a->repo, a->progress_cb, a->progress_arg);
2248 static const struct got_error *
2249 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2251 struct diff_cb_arg *a = arg;
2252 const struct got_error *err;
2253 char *path;
2255 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2256 return got_error(GOT_ERR_CANCELLED);
2258 if (got_object_tree_entry_is_submodule(te))
2259 return NULL;
2261 if (asprintf(&path, "%s%s%s", parent_path,
2262 parent_path[0] ? "/" : "", te->name)
2263 == -1)
2264 return got_error_from_errno("asprintf");
2266 if (S_ISDIR(te->mode))
2267 err = add_dir_on_disk(a->worktree, path);
2268 else
2269 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2270 a->repo, a->progress_cb, a->progress_arg);
2272 free(path);
2273 return err;
2276 const struct got_error *
2277 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2279 uint32_t uuid_status;
2281 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2282 if (uuid_status != uuid_s_ok) {
2283 *uuidstr = NULL;
2284 return got_error_uuid(uuid_status, "uuid_to_string");
2287 return NULL;
2290 static const struct got_error *
2291 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2293 const struct got_error *err = NULL;
2294 char *uuidstr = NULL;
2296 *refname = NULL;
2298 err = got_worktree_get_uuid(&uuidstr, worktree);
2299 if (err)
2300 return err;
2302 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2303 err = got_error_from_errno("asprintf");
2304 *refname = NULL;
2306 free(uuidstr);
2307 return err;
2310 const struct got_error *
2311 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2313 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2316 static const struct got_error *
2317 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2319 return get_ref_name(refname, worktree,
2320 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2323 static const struct got_error *
2324 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2326 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2329 static const struct got_error *
2330 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2332 return get_ref_name(refname, worktree,
2333 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2336 static const struct got_error *
2337 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2339 return get_ref_name(refname, worktree,
2340 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2343 static const struct got_error *
2344 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2346 return get_ref_name(refname, worktree,
2347 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2350 static const struct got_error *
2351 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2353 return get_ref_name(refname, worktree,
2354 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2357 static const struct got_error *
2358 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2360 return get_ref_name(refname, worktree,
2361 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2364 static const struct got_error *
2365 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2367 return get_ref_name(refname, worktree,
2368 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2371 const struct got_error *
2372 got_worktree_get_histedit_script_path(char **path,
2373 struct got_worktree *worktree)
2375 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2376 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2377 *path = NULL;
2378 return got_error_from_errno("asprintf");
2380 return NULL;
2383 static const struct got_error *
2384 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2386 return get_ref_name(refname, worktree,
2387 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2390 static const struct got_error *
2391 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2393 return get_ref_name(refname, worktree,
2394 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2398 * Prevent Git's garbage collector from deleting our base commit by
2399 * setting a reference to our base commit's ID.
2401 static const struct got_error *
2402 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2404 const struct got_error *err = NULL;
2405 struct got_reference *ref = NULL;
2406 char *refname;
2408 err = got_worktree_get_base_ref_name(&refname, worktree);
2409 if (err)
2410 return err;
2412 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2413 if (err)
2414 goto done;
2416 err = got_ref_write(ref, repo);
2417 done:
2418 free(refname);
2419 if (ref)
2420 got_ref_close(ref);
2421 return err;
2424 static const struct got_error *
2425 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2427 const struct got_error *err = NULL;
2429 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2430 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2431 err = got_error_from_errno("asprintf");
2432 *fileindex_path = NULL;
2434 return err;
2438 static const struct got_error *
2439 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2440 struct got_worktree *worktree)
2442 const struct got_error *err = NULL;
2443 FILE *index = NULL;
2445 *fileindex_path = NULL;
2446 *fileindex = got_fileindex_alloc();
2447 if (*fileindex == NULL)
2448 return got_error_from_errno("got_fileindex_alloc");
2450 err = get_fileindex_path(fileindex_path, worktree);
2451 if (err)
2452 goto done;
2454 index = fopen(*fileindex_path, "rb");
2455 if (index == NULL) {
2456 if (errno != ENOENT)
2457 err = got_error_from_errno2("fopen", *fileindex_path);
2458 } else {
2459 err = got_fileindex_read(*fileindex, index);
2460 if (fclose(index) == EOF && err == NULL)
2461 err = got_error_from_errno("fclose");
2463 done:
2464 if (err) {
2465 free(*fileindex_path);
2466 *fileindex_path = NULL;
2467 got_fileindex_free(*fileindex);
2468 *fileindex = NULL;
2470 return err;
2473 struct bump_base_commit_id_arg {
2474 struct got_object_id *base_commit_id;
2475 const char *path;
2476 size_t path_len;
2477 const char *entry_name;
2478 got_worktree_checkout_cb progress_cb;
2479 void *progress_arg;
2482 /* Bump base commit ID of all files within an updated part of the work tree. */
2483 static const struct got_error *
2484 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2486 const struct got_error *err;
2487 struct bump_base_commit_id_arg *a = arg;
2489 if (a->entry_name) {
2490 if (strcmp(ie->path, a->path) != 0)
2491 return NULL;
2492 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2493 return NULL;
2495 if (got_fileindex_entry_was_skipped(ie))
2496 return NULL;
2498 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2499 SHA1_DIGEST_LENGTH) == 0)
2500 return NULL;
2502 if (a->progress_cb) {
2503 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2504 ie->path);
2505 if (err)
2506 return err;
2508 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2509 return NULL;
2512 static const struct got_error *
2513 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2514 struct got_fileindex *fileindex,
2515 got_worktree_checkout_cb progress_cb, void *progress_arg)
2517 struct bump_base_commit_id_arg bbc_arg;
2519 bbc_arg.base_commit_id = worktree->base_commit_id;
2520 bbc_arg.entry_name = NULL;
2521 bbc_arg.path = "";
2522 bbc_arg.path_len = 0;
2523 bbc_arg.progress_cb = progress_cb;
2524 bbc_arg.progress_arg = progress_arg;
2526 return got_fileindex_for_each_entry_safe(fileindex,
2527 bump_base_commit_id, &bbc_arg);
2530 static const struct got_error *
2531 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2533 const struct got_error *err = NULL;
2534 char *new_fileindex_path = NULL;
2535 FILE *new_index = NULL;
2536 struct timespec timeout;
2538 err = got_opentemp_named(&new_fileindex_path, &new_index,
2539 fileindex_path);
2540 if (err)
2541 goto done;
2543 err = got_fileindex_write(fileindex, new_index);
2544 if (err)
2545 goto done;
2547 if (rename(new_fileindex_path, fileindex_path) != 0) {
2548 err = got_error_from_errno3("rename", new_fileindex_path,
2549 fileindex_path);
2550 unlink(new_fileindex_path);
2554 * Sleep for a short amount of time to ensure that files modified after
2555 * this program exits have a different time stamp from the one which
2556 * was recorded in the file index.
2558 timeout.tv_sec = 0;
2559 timeout.tv_nsec = 1;
2560 nanosleep(&timeout, NULL);
2561 done:
2562 if (new_index)
2563 fclose(new_index);
2564 free(new_fileindex_path);
2565 return err;
2568 static const struct got_error *
2569 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2570 struct got_object_id **tree_id, const char *wt_relpath,
2571 struct got_worktree *worktree, struct got_repository *repo)
2573 const struct got_error *err = NULL;
2574 struct got_object_id *id = NULL;
2575 char *in_repo_path = NULL;
2576 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2578 *entry_type = GOT_OBJ_TYPE_ANY;
2579 *tree_relpath = NULL;
2580 *tree_id = NULL;
2582 if (wt_relpath[0] == '\0') {
2583 /* Check out all files within the work tree. */
2584 *entry_type = GOT_OBJ_TYPE_TREE;
2585 *tree_relpath = strdup("");
2586 if (*tree_relpath == NULL) {
2587 err = got_error_from_errno("strdup");
2588 goto done;
2590 err = got_object_id_by_path(tree_id, repo,
2591 worktree->base_commit_id, worktree->path_prefix);
2592 if (err)
2593 goto done;
2594 return NULL;
2597 /* Check out a subset of files in the work tree. */
2599 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2600 is_root_wt ? "" : "/", wt_relpath) == -1) {
2601 err = got_error_from_errno("asprintf");
2602 goto done;
2605 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2606 in_repo_path);
2607 if (err)
2608 goto done;
2610 free(in_repo_path);
2611 in_repo_path = NULL;
2613 err = got_object_get_type(entry_type, repo, id);
2614 if (err)
2615 goto done;
2617 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2618 /* Check out a single file. */
2619 if (strchr(wt_relpath, '/') == NULL) {
2620 /* Check out a single file in work tree's root dir. */
2621 in_repo_path = strdup(worktree->path_prefix);
2622 if (in_repo_path == NULL) {
2623 err = got_error_from_errno("strdup");
2624 goto done;
2626 *tree_relpath = strdup("");
2627 if (*tree_relpath == NULL) {
2628 err = got_error_from_errno("strdup");
2629 goto done;
2631 } else {
2632 /* Check out a single file in a subdirectory. */
2633 err = got_path_dirname(tree_relpath, wt_relpath);
2634 if (err)
2635 return err;
2636 if (asprintf(&in_repo_path, "%s%s%s",
2637 worktree->path_prefix, is_root_wt ? "" : "/",
2638 *tree_relpath) == -1) {
2639 err = got_error_from_errno("asprintf");
2640 goto done;
2643 err = got_object_id_by_path(tree_id, repo,
2644 worktree->base_commit_id, in_repo_path);
2645 } else {
2646 /* Check out all files within a subdirectory. */
2647 *tree_id = got_object_id_dup(id);
2648 if (*tree_id == NULL) {
2649 err = got_error_from_errno("got_object_id_dup");
2650 goto done;
2652 *tree_relpath = strdup(wt_relpath);
2653 if (*tree_relpath == NULL) {
2654 err = got_error_from_errno("strdup");
2655 goto done;
2658 done:
2659 free(id);
2660 free(in_repo_path);
2661 if (err) {
2662 *entry_type = GOT_OBJ_TYPE_ANY;
2663 free(*tree_relpath);
2664 *tree_relpath = NULL;
2665 free(*tree_id);
2666 *tree_id = NULL;
2668 return err;
2671 static const struct got_error *
2672 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2673 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2674 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2675 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2677 const struct got_error *err = NULL;
2678 struct got_commit_object *commit = NULL;
2679 struct got_tree_object *tree = NULL;
2680 struct got_fileindex_diff_tree_cb diff_cb;
2681 struct diff_cb_arg arg;
2683 err = ref_base_commit(worktree, repo);
2684 if (err) {
2685 if (!(err->code == GOT_ERR_ERRNO &&
2686 (errno == EACCES || errno == EROFS)))
2687 goto done;
2688 err = (*progress_cb)(progress_arg,
2689 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2690 if (err)
2691 return err;
2694 err = got_object_open_as_commit(&commit, repo,
2695 worktree->base_commit_id);
2696 if (err)
2697 goto done;
2699 err = got_object_open_as_tree(&tree, repo, tree_id);
2700 if (err)
2701 goto done;
2703 if (entry_name &&
2704 got_object_tree_find_entry(tree, entry_name) == NULL) {
2705 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2706 goto done;
2709 diff_cb.diff_old_new = diff_old_new;
2710 diff_cb.diff_old = diff_old;
2711 diff_cb.diff_new = diff_new;
2712 arg.fileindex = fileindex;
2713 arg.worktree = worktree;
2714 arg.repo = repo;
2715 arg.progress_cb = progress_cb;
2716 arg.progress_arg = progress_arg;
2717 arg.cancel_cb = cancel_cb;
2718 arg.cancel_arg = cancel_arg;
2719 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2720 entry_name, repo, &diff_cb, &arg);
2721 done:
2722 if (tree)
2723 got_object_tree_close(tree);
2724 if (commit)
2725 got_object_commit_close(commit);
2726 return err;
2729 const struct got_error *
2730 got_worktree_checkout_files(struct got_worktree *worktree,
2731 struct got_pathlist_head *paths, struct got_repository *repo,
2732 got_worktree_checkout_cb progress_cb, void *progress_arg,
2733 got_cancel_cb cancel_cb, void *cancel_arg)
2735 const struct got_error *err = NULL, *sync_err, *unlockerr;
2736 struct got_commit_object *commit = NULL;
2737 struct got_tree_object *tree = NULL;
2738 struct got_fileindex *fileindex = NULL;
2739 char *fileindex_path = NULL;
2740 struct got_pathlist_entry *pe;
2741 struct tree_path_data {
2742 STAILQ_ENTRY(tree_path_data) entry;
2743 struct got_object_id *tree_id;
2744 int entry_type;
2745 char *relpath;
2746 char *entry_name;
2747 } *tpd = NULL;
2748 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2750 STAILQ_INIT(&tree_paths);
2752 err = lock_worktree(worktree, LOCK_EX);
2753 if (err)
2754 return err;
2756 /* Map all specified paths to in-repository trees. */
2757 TAILQ_FOREACH(pe, paths, entry) {
2758 tpd = malloc(sizeof(*tpd));
2759 if (tpd == NULL) {
2760 err = got_error_from_errno("malloc");
2761 goto done;
2764 err = find_tree_entry_for_checkout(&tpd->entry_type,
2765 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2766 if (err) {
2767 free(tpd);
2768 goto done;
2771 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2772 err = got_path_basename(&tpd->entry_name, pe->path);
2773 if (err) {
2774 free(tpd->relpath);
2775 free(tpd->tree_id);
2776 free(tpd);
2777 goto done;
2779 } else
2780 tpd->entry_name = NULL;
2782 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2786 * Read the file index.
2787 * Checking out files is supposed to be an idempotent operation.
2788 * If the on-disk file index is incomplete we will try to complete it.
2790 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2791 if (err)
2792 goto done;
2794 tpd = STAILQ_FIRST(&tree_paths);
2795 TAILQ_FOREACH(pe, paths, entry) {
2796 struct bump_base_commit_id_arg bbc_arg;
2798 err = checkout_files(worktree, fileindex, tpd->relpath,
2799 tpd->tree_id, tpd->entry_name, repo,
2800 progress_cb, progress_arg, cancel_cb, cancel_arg);
2801 if (err)
2802 break;
2804 bbc_arg.base_commit_id = worktree->base_commit_id;
2805 bbc_arg.entry_name = tpd->entry_name;
2806 bbc_arg.path = pe->path;
2807 bbc_arg.path_len = pe->path_len;
2808 bbc_arg.progress_cb = progress_cb;
2809 bbc_arg.progress_arg = progress_arg;
2810 err = got_fileindex_for_each_entry_safe(fileindex,
2811 bump_base_commit_id, &bbc_arg);
2812 if (err)
2813 break;
2815 tpd = STAILQ_NEXT(tpd, entry);
2817 sync_err = sync_fileindex(fileindex, fileindex_path);
2818 if (sync_err && err == NULL)
2819 err = sync_err;
2820 done:
2821 free(fileindex_path);
2822 if (tree)
2823 got_object_tree_close(tree);
2824 if (commit)
2825 got_object_commit_close(commit);
2826 if (fileindex)
2827 got_fileindex_free(fileindex);
2828 while (!STAILQ_EMPTY(&tree_paths)) {
2829 tpd = STAILQ_FIRST(&tree_paths);
2830 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2831 free(tpd->relpath);
2832 free(tpd->tree_id);
2833 free(tpd);
2835 unlockerr = lock_worktree(worktree, LOCK_SH);
2836 if (unlockerr && err == NULL)
2837 err = unlockerr;
2838 return err;
2841 struct merge_file_cb_arg {
2842 struct got_worktree *worktree;
2843 struct got_fileindex *fileindex;
2844 got_worktree_checkout_cb progress_cb;
2845 void *progress_arg;
2846 got_cancel_cb cancel_cb;
2847 void *cancel_arg;
2848 const char *label_orig;
2849 struct got_object_id *commit_id2;
2852 static const struct got_error *
2853 merge_file_cb(void *arg, struct got_blob_object *blob1,
2854 struct got_blob_object *blob2, struct got_object_id *id1,
2855 struct got_object_id *id2, const char *path1, const char *path2,
2856 mode_t mode1, mode_t mode2, struct got_repository *repo)
2858 static const struct got_error *err = NULL;
2859 struct merge_file_cb_arg *a = arg;
2860 struct got_fileindex_entry *ie;
2861 char *ondisk_path = NULL;
2862 struct stat sb;
2863 unsigned char status;
2864 int local_changes_subsumed;
2865 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2866 char *id_str = NULL, *label_deriv2 = NULL;
2868 if (blob1 && blob2) {
2869 ie = got_fileindex_entry_get(a->fileindex, path2,
2870 strlen(path2));
2871 if (ie == NULL)
2872 return (*a->progress_cb)(a->progress_arg,
2873 GOT_STATUS_MISSING, path2);
2875 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2876 path2) == -1)
2877 return got_error_from_errno("asprintf");
2879 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2880 repo);
2881 if (err)
2882 goto done;
2884 if (status == GOT_STATUS_DELETE) {
2885 err = (*a->progress_cb)(a->progress_arg,
2886 GOT_STATUS_MERGE, path2);
2887 goto done;
2889 if (status != GOT_STATUS_NO_CHANGE &&
2890 status != GOT_STATUS_MODIFY &&
2891 status != GOT_STATUS_CONFLICT &&
2892 status != GOT_STATUS_ADD) {
2893 err = (*a->progress_cb)(a->progress_arg, status, path2);
2894 goto done;
2897 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2898 char *link_target2;
2899 err = got_object_blob_read_to_str(&link_target2, blob2);
2900 if (err)
2901 goto done;
2902 err = merge_symlink(a->worktree, blob1, ondisk_path,
2903 path2, a->label_orig, link_target2, a->commit_id2,
2904 repo, a->progress_cb, a->progress_arg);
2905 free(link_target2);
2906 } else {
2907 int fd;
2909 f_orig = got_opentemp();
2910 if (f_orig == NULL) {
2911 err = got_error_from_errno("got_opentemp");
2912 goto done;
2914 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2915 f_orig, blob1);
2916 if (err)
2917 goto done;
2919 f_deriv2 = got_opentemp();
2920 if (f_deriv2 == NULL)
2921 goto done;
2922 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2923 f_deriv2, blob2);
2924 if (err)
2925 goto done;
2927 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2928 if (fd == -1) {
2929 err = got_error_from_errno2("open",
2930 ondisk_path);
2931 goto done;
2933 f_deriv = fdopen(fd, "r");
2934 if (f_deriv == NULL) {
2935 err = got_error_from_errno2("fdopen",
2936 ondisk_path);
2937 close(fd);
2938 goto done;
2940 err = got_object_id_str(&id_str, a->commit_id2);
2941 if (err)
2942 goto done;
2943 if (asprintf(&label_deriv2, "%s: commit %s",
2944 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2945 err = got_error_from_errno("asprintf");
2946 goto done;
2948 err = merge_file(&local_changes_subsumed, a->worktree,
2949 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2950 sb.st_mode, a->label_orig, NULL, label_deriv2,
2951 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2952 a->progress_cb, a->progress_arg);
2954 } else if (blob1) {
2955 ie = got_fileindex_entry_get(a->fileindex, path1,
2956 strlen(path1));
2957 if (ie == NULL)
2958 return (*a->progress_cb)(a->progress_arg,
2959 GOT_STATUS_MISSING, path1);
2961 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2962 path1) == -1)
2963 return got_error_from_errno("asprintf");
2965 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2966 repo);
2967 if (err)
2968 goto done;
2970 switch (status) {
2971 case GOT_STATUS_NO_CHANGE:
2972 err = (*a->progress_cb)(a->progress_arg,
2973 GOT_STATUS_DELETE, path1);
2974 if (err)
2975 goto done;
2976 err = remove_ondisk_file(a->worktree->root_path, path1);
2977 if (err)
2978 goto done;
2979 if (ie)
2980 got_fileindex_entry_mark_deleted_from_disk(ie);
2981 break;
2982 case GOT_STATUS_DELETE:
2983 case GOT_STATUS_MISSING:
2984 err = (*a->progress_cb)(a->progress_arg,
2985 GOT_STATUS_DELETE, path1);
2986 if (err)
2987 goto done;
2988 if (ie)
2989 got_fileindex_entry_mark_deleted_from_disk(ie);
2990 break;
2991 case GOT_STATUS_ADD: {
2992 struct got_object_id *id;
2993 FILE *blob1_f;
2995 * Delete the added file only if its content already
2996 * exists in the repository.
2998 err = got_object_blob_file_create(&id, &blob1_f, path1);
2999 if (err)
3000 goto done;
3001 if (got_object_id_cmp(id, id1) == 0) {
3002 err = (*a->progress_cb)(a->progress_arg,
3003 GOT_STATUS_DELETE, path1);
3004 if (err)
3005 goto done;
3006 err = remove_ondisk_file(a->worktree->root_path,
3007 path1);
3008 if (err)
3009 goto done;
3010 if (ie)
3011 got_fileindex_entry_remove(a->fileindex,
3012 ie);
3013 } else {
3014 err = (*a->progress_cb)(a->progress_arg,
3015 GOT_STATUS_CANNOT_DELETE, path1);
3017 if (fclose(blob1_f) == EOF && err == NULL)
3018 err = got_error_from_errno("fclose");
3019 free(id);
3020 if (err)
3021 goto done;
3022 break;
3024 case GOT_STATUS_MODIFY:
3025 case GOT_STATUS_CONFLICT:
3026 err = (*a->progress_cb)(a->progress_arg,
3027 GOT_STATUS_CANNOT_DELETE, path1);
3028 if (err)
3029 goto done;
3030 break;
3031 case GOT_STATUS_OBSTRUCTED:
3032 err = (*a->progress_cb)(a->progress_arg, status, path1);
3033 if (err)
3034 goto done;
3035 break;
3036 default:
3037 break;
3039 } else if (blob2) {
3040 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3041 path2) == -1)
3042 return got_error_from_errno("asprintf");
3043 ie = got_fileindex_entry_get(a->fileindex, path2,
3044 strlen(path2));
3045 if (ie) {
3046 err = get_file_status(&status, &sb, ie, ondisk_path,
3047 -1, NULL, repo);
3048 if (err)
3049 goto done;
3050 if (status != GOT_STATUS_NO_CHANGE &&
3051 status != GOT_STATUS_MODIFY &&
3052 status != GOT_STATUS_CONFLICT &&
3053 status != GOT_STATUS_ADD) {
3054 err = (*a->progress_cb)(a->progress_arg,
3055 status, path2);
3056 goto done;
3058 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3059 char *link_target2;
3060 err = got_object_blob_read_to_str(&link_target2,
3061 blob2);
3062 if (err)
3063 goto done;
3064 err = merge_symlink(a->worktree, NULL,
3065 ondisk_path, path2, a->label_orig,
3066 link_target2, a->commit_id2, repo,
3067 a->progress_cb, a->progress_arg);
3068 free(link_target2);
3069 } else if (S_ISREG(sb.st_mode)) {
3070 err = merge_blob(&local_changes_subsumed,
3071 a->worktree, NULL, ondisk_path, path2,
3072 sb.st_mode, a->label_orig, blob2,
3073 a->commit_id2, repo, a->progress_cb,
3074 a->progress_arg);
3075 } else {
3076 err = got_error_path(ondisk_path,
3077 GOT_ERR_FILE_OBSTRUCTED);
3079 if (err)
3080 goto done;
3081 if (status == GOT_STATUS_DELETE) {
3082 err = got_fileindex_entry_update(ie,
3083 a->worktree->root_fd, path2, blob2->id.sha1,
3084 a->worktree->base_commit_id->sha1, 0);
3085 if (err)
3086 goto done;
3088 } else {
3089 int is_bad_symlink = 0;
3090 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3091 if (S_ISLNK(mode2)) {
3092 err = install_symlink(&is_bad_symlink,
3093 a->worktree, ondisk_path, path2, blob2, 0,
3094 0, 1, repo, a->progress_cb, a->progress_arg);
3095 } else {
3096 err = install_blob(a->worktree, ondisk_path, path2,
3097 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3098 a->progress_cb, a->progress_arg);
3100 if (err)
3101 goto done;
3102 err = got_fileindex_entry_alloc(&ie, path2);
3103 if (err)
3104 goto done;
3105 err = got_fileindex_entry_update(ie,
3106 a->worktree->root_fd, path2, NULL, NULL, 1);
3107 if (err) {
3108 got_fileindex_entry_free(ie);
3109 goto done;
3111 err = got_fileindex_entry_add(a->fileindex, ie);
3112 if (err) {
3113 got_fileindex_entry_free(ie);
3114 goto done;
3116 if (is_bad_symlink) {
3117 got_fileindex_entry_filetype_set(ie,
3118 GOT_FILEIDX_MODE_BAD_SYMLINK);
3122 done:
3123 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3124 err = got_error_from_errno("fclose");
3125 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3126 err = got_error_from_errno("fclose");
3127 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3128 err = got_error_from_errno("fclose");
3129 free(id_str);
3130 free(label_deriv2);
3131 free(ondisk_path);
3132 return err;
3135 static const struct got_error *
3136 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3138 struct got_worktree *worktree = arg;
3140 /* Reject merges into a work tree with mixed base commits. */
3141 if (got_fileindex_entry_has_commit(ie) &&
3142 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3143 SHA1_DIGEST_LENGTH) != 0)
3144 return got_error(GOT_ERR_MIXED_COMMITS);
3146 return NULL;
3149 struct check_merge_conflicts_arg {
3150 struct got_worktree *worktree;
3151 struct got_fileindex *fileindex;
3152 struct got_repository *repo;
3155 static const struct got_error *
3156 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3157 struct got_blob_object *blob2, struct got_object_id *id1,
3158 struct got_object_id *id2, const char *path1, const char *path2,
3159 mode_t mode1, mode_t mode2, struct got_repository *repo)
3161 const struct got_error *err = NULL;
3162 struct check_merge_conflicts_arg *a = arg;
3163 unsigned char status;
3164 struct stat sb;
3165 struct got_fileindex_entry *ie;
3166 const char *path = path2 ? path2 : path1;
3167 struct got_object_id *id = id2 ? id2 : id1;
3168 char *ondisk_path;
3170 if (id == NULL)
3171 return NULL;
3173 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3174 if (ie == NULL)
3175 return NULL;
3177 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3178 == -1)
3179 return got_error_from_errno("asprintf");
3181 /* Reject merges into a work tree with conflicted files. */
3182 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3183 free(ondisk_path);
3184 if (err)
3185 return err;
3186 if (status == GOT_STATUS_CONFLICT)
3187 return got_error(GOT_ERR_CONFLICTS);
3189 return NULL;
3192 static const struct got_error *
3193 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3194 const char *fileindex_path, struct got_object_id *commit_id1,
3195 struct got_object_id *commit_id2, struct got_repository *repo,
3196 got_worktree_checkout_cb progress_cb, void *progress_arg,
3197 got_cancel_cb cancel_cb, void *cancel_arg)
3199 const struct got_error *err = NULL, *sync_err;
3200 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3201 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3202 struct check_merge_conflicts_arg cmc_arg;
3203 struct merge_file_cb_arg arg;
3204 char *label_orig = NULL;
3206 if (commit_id1) {
3207 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3208 worktree->path_prefix);
3209 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3210 goto done;
3212 if (tree_id1) {
3213 char *id_str;
3215 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3216 if (err)
3217 goto done;
3219 err = got_object_id_str(&id_str, commit_id1);
3220 if (err)
3221 goto done;
3223 if (asprintf(&label_orig, "%s: commit %s",
3224 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3225 err = got_error_from_errno("asprintf");
3226 free(id_str);
3227 goto done;
3229 free(id_str);
3232 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3233 worktree->path_prefix);
3234 if (err)
3235 goto done;
3237 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3238 if (err)
3239 goto done;
3241 cmc_arg.worktree = worktree;
3242 cmc_arg.fileindex = fileindex;
3243 cmc_arg.repo = repo;
3244 err = got_diff_tree(tree1, tree2, "", "", repo,
3245 check_merge_conflicts, &cmc_arg, 0);
3246 if (err)
3247 goto done;
3249 arg.worktree = worktree;
3250 arg.fileindex = fileindex;
3251 arg.progress_cb = progress_cb;
3252 arg.progress_arg = progress_arg;
3253 arg.cancel_cb = cancel_cb;
3254 arg.cancel_arg = cancel_arg;
3255 arg.label_orig = label_orig;
3256 arg.commit_id2 = commit_id2;
3257 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3258 sync_err = sync_fileindex(fileindex, fileindex_path);
3259 if (sync_err && err == NULL)
3260 err = sync_err;
3261 done:
3262 if (tree1)
3263 got_object_tree_close(tree1);
3264 if (tree2)
3265 got_object_tree_close(tree2);
3266 free(label_orig);
3267 return err;
3270 const struct got_error *
3271 got_worktree_merge_files(struct got_worktree *worktree,
3272 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3273 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3274 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3276 const struct got_error *err, *unlockerr;
3277 char *fileindex_path = NULL;
3278 struct got_fileindex *fileindex = NULL;
3280 err = lock_worktree(worktree, LOCK_EX);
3281 if (err)
3282 return err;
3284 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3285 if (err)
3286 goto done;
3288 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3289 worktree);
3290 if (err)
3291 goto done;
3293 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3294 commit_id2, repo, progress_cb, progress_arg,
3295 cancel_cb, cancel_arg);
3296 done:
3297 if (fileindex)
3298 got_fileindex_free(fileindex);
3299 free(fileindex_path);
3300 unlockerr = lock_worktree(worktree, LOCK_SH);
3301 if (unlockerr && err == NULL)
3302 err = unlockerr;
3303 return err;
3306 struct diff_dir_cb_arg {
3307 struct got_fileindex *fileindex;
3308 struct got_worktree *worktree;
3309 const char *status_path;
3310 size_t status_path_len;
3311 struct got_repository *repo;
3312 got_worktree_status_cb status_cb;
3313 void *status_arg;
3314 got_cancel_cb cancel_cb;
3315 void *cancel_arg;
3316 /* A pathlist containing per-directory pathlists of ignore patterns. */
3317 struct got_pathlist_head *ignores;
3318 int report_unchanged;
3319 int no_ignores;
3322 static const struct got_error *
3323 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3324 int dirfd, const char *de_name,
3325 got_worktree_status_cb status_cb, void *status_arg,
3326 struct got_repository *repo, int report_unchanged)
3328 const struct got_error *err = NULL;
3329 unsigned char status = GOT_STATUS_NO_CHANGE;
3330 unsigned char staged_status = get_staged_status(ie);
3331 struct stat sb;
3332 struct got_object_id blob_id, commit_id, staged_blob_id;
3333 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3334 struct got_object_id *staged_blob_idp = NULL;
3336 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3337 if (err)
3338 return err;
3340 if (status == GOT_STATUS_NO_CHANGE &&
3341 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3342 return NULL;
3344 if (got_fileindex_entry_has_blob(ie)) {
3345 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3346 blob_idp = &blob_id;
3348 if (got_fileindex_entry_has_commit(ie)) {
3349 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3350 commit_idp = &commit_id;
3352 if (staged_status == GOT_STATUS_ADD ||
3353 staged_status == GOT_STATUS_MODIFY) {
3354 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3355 SHA1_DIGEST_LENGTH);
3356 staged_blob_idp = &staged_blob_id;
3359 return (*status_cb)(status_arg, status, staged_status,
3360 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3363 static const struct got_error *
3364 status_old_new(void *arg, struct got_fileindex_entry *ie,
3365 struct dirent *de, const char *parent_path, int dirfd)
3367 const struct got_error *err = NULL;
3368 struct diff_dir_cb_arg *a = arg;
3369 char *abspath;
3371 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3372 return got_error(GOT_ERR_CANCELLED);
3374 if (got_path_cmp(parent_path, a->status_path,
3375 strlen(parent_path), a->status_path_len) != 0 &&
3376 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3377 return NULL;
3379 if (parent_path[0]) {
3380 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3381 parent_path, de->d_name) == -1)
3382 return got_error_from_errno("asprintf");
3383 } else {
3384 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3385 de->d_name) == -1)
3386 return got_error_from_errno("asprintf");
3389 err = report_file_status(ie, abspath, dirfd, de->d_name,
3390 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3391 free(abspath);
3392 return err;
3395 static const struct got_error *
3396 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3398 struct diff_dir_cb_arg *a = arg;
3399 struct got_object_id blob_id, commit_id;
3400 unsigned char status;
3402 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3403 return got_error(GOT_ERR_CANCELLED);
3405 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3406 return NULL;
3408 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3409 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3410 if (got_fileindex_entry_has_file_on_disk(ie))
3411 status = GOT_STATUS_MISSING;
3412 else
3413 status = GOT_STATUS_DELETE;
3414 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3415 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3418 void
3419 free_ignorelist(struct got_pathlist_head *ignorelist)
3421 struct got_pathlist_entry *pe;
3423 TAILQ_FOREACH(pe, ignorelist, entry)
3424 free((char *)pe->path);
3425 got_pathlist_free(ignorelist);
3428 void
3429 free_ignores(struct got_pathlist_head *ignores)
3431 struct got_pathlist_entry *pe;
3433 TAILQ_FOREACH(pe, ignores, entry) {
3434 struct got_pathlist_head *ignorelist = pe->data;
3435 free_ignorelist(ignorelist);
3436 free((char *)pe->path);
3438 got_pathlist_free(ignores);
3441 static const struct got_error *
3442 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3444 const struct got_error *err = NULL;
3445 struct got_pathlist_entry *pe = NULL;
3446 struct got_pathlist_head *ignorelist;
3447 char *line = NULL, *pattern, *dirpath = NULL;
3448 size_t linesize = 0;
3449 ssize_t linelen;
3451 ignorelist = calloc(1, sizeof(*ignorelist));
3452 if (ignorelist == NULL)
3453 return got_error_from_errno("calloc");
3454 TAILQ_INIT(ignorelist);
3456 while ((linelen = getline(&line, &linesize, f)) != -1) {
3457 if (linelen > 0 && line[linelen - 1] == '\n')
3458 line[linelen - 1] = '\0';
3460 /* Git's ignores may contain comments. */
3461 if (line[0] == '#')
3462 continue;
3464 /* Git's negated patterns are not (yet?) supported. */
3465 if (line[0] == '!')
3466 continue;
3468 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3469 line) == -1) {
3470 err = got_error_from_errno("asprintf");
3471 goto done;
3473 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3474 if (err)
3475 goto done;
3477 if (ferror(f)) {
3478 err = got_error_from_errno("getline");
3479 goto done;
3482 dirpath = strdup(path);
3483 if (dirpath == NULL) {
3484 err = got_error_from_errno("strdup");
3485 goto done;
3487 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3488 done:
3489 free(line);
3490 if (err || pe == NULL) {
3491 free(dirpath);
3492 free_ignorelist(ignorelist);
3494 return err;
3497 int
3498 match_ignores(struct got_pathlist_head *ignores, const char *path)
3500 struct got_pathlist_entry *pe;
3502 /* Handle patterns which match in all directories. */
3503 TAILQ_FOREACH(pe, ignores, entry) {
3504 struct got_pathlist_head *ignorelist = pe->data;
3505 struct got_pathlist_entry *pi;
3507 TAILQ_FOREACH(pi, ignorelist, entry) {
3508 const char *p, *pattern = pi->path;
3510 if (strncmp(pattern, "**/", 3) != 0)
3511 continue;
3512 pattern += 3;
3513 p = path;
3514 while (*p) {
3515 if (fnmatch(pattern, p,
3516 FNM_PATHNAME | FNM_LEADING_DIR)) {
3517 /* Retry in next directory. */
3518 while (*p && *p != '/')
3519 p++;
3520 while (*p == '/')
3521 p++;
3522 continue;
3524 return 1;
3530 * The ignores pathlist contains ignore lists from children before
3531 * parents, so we can find the most specific ignorelist by walking
3532 * ignores backwards.
3534 pe = TAILQ_LAST(ignores, got_pathlist_head);
3535 while (pe) {
3536 if (got_path_is_child(path, pe->path, pe->path_len)) {
3537 struct got_pathlist_head *ignorelist = pe->data;
3538 struct got_pathlist_entry *pi;
3539 TAILQ_FOREACH(pi, ignorelist, entry) {
3540 const char *pattern = pi->path;
3541 int flags = FNM_LEADING_DIR;
3542 if (strstr(pattern, "/**/") == NULL)
3543 flags |= FNM_PATHNAME;
3544 if (fnmatch(pattern, path, flags))
3545 continue;
3546 return 1;
3549 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3552 return 0;
3555 static const struct got_error *
3556 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3557 const char *path, int dirfd, const char *ignores_filename)
3559 const struct got_error *err = NULL;
3560 char *ignorespath;
3561 int fd = -1;
3562 FILE *ignoresfile = NULL;
3564 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3565 path[0] ? "/" : "", ignores_filename) == -1)
3566 return got_error_from_errno("asprintf");
3568 if (dirfd != -1) {
3569 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3570 if (fd == -1) {
3571 if (errno != ENOENT && errno != EACCES)
3572 err = got_error_from_errno2("openat",
3573 ignorespath);
3574 } else {
3575 ignoresfile = fdopen(fd, "r");
3576 if (ignoresfile == NULL)
3577 err = got_error_from_errno2("fdopen",
3578 ignorespath);
3579 else {
3580 fd = -1;
3581 err = read_ignores(ignores, path, ignoresfile);
3584 } else {
3585 ignoresfile = fopen(ignorespath, "r");
3586 if (ignoresfile == NULL) {
3587 if (errno != ENOENT && errno != EACCES)
3588 err = got_error_from_errno2("fopen",
3589 ignorespath);
3590 } else
3591 err = read_ignores(ignores, path, ignoresfile);
3594 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3595 err = got_error_from_errno2("fclose", path);
3596 if (fd != -1 && close(fd) == -1 && err == NULL)
3597 err = got_error_from_errno2("close", path);
3598 free(ignorespath);
3599 return err;
3602 static const struct got_error *
3603 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3605 const struct got_error *err = NULL;
3606 struct diff_dir_cb_arg *a = arg;
3607 char *path = NULL;
3609 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3610 return got_error(GOT_ERR_CANCELLED);
3612 if (parent_path[0]) {
3613 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3614 return got_error_from_errno("asprintf");
3615 } else {
3616 path = de->d_name;
3619 if (de->d_type != DT_DIR &&
3620 got_path_is_child(path, a->status_path, a->status_path_len)
3621 && !match_ignores(a->ignores, path))
3622 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3623 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3624 if (parent_path[0])
3625 free(path);
3626 return err;
3629 static const struct got_error *
3630 status_traverse(void *arg, const char *path, int dirfd)
3632 const struct got_error *err = NULL;
3633 struct diff_dir_cb_arg *a = arg;
3635 if (a->no_ignores)
3636 return NULL;
3638 err = add_ignores(a->ignores, a->worktree->root_path,
3639 path, dirfd, ".cvsignore");
3640 if (err)
3641 return err;
3643 err = add_ignores(a->ignores, a->worktree->root_path, path,
3644 dirfd, ".gitignore");
3646 return err;
3649 static const struct got_error *
3650 report_single_file_status(const char *path, const char *ondisk_path,
3651 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3652 void *status_arg, struct got_repository *repo, int report_unchanged,
3653 struct got_pathlist_head *ignores, int no_ignores)
3655 struct got_fileindex_entry *ie;
3656 struct stat sb;
3658 if (!no_ignores && match_ignores(ignores, path))
3659 return NULL;
3661 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3662 if (ie)
3663 return report_file_status(ie, ondisk_path, -1, NULL,
3664 status_cb, status_arg, repo, report_unchanged);
3666 if (lstat(ondisk_path, &sb) == -1) {
3667 if (errno != ENOENT)
3668 return got_error_from_errno2("lstat", ondisk_path);
3669 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3670 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3671 return NULL;
3674 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3675 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3676 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3678 return NULL;
3681 static const struct got_error *
3682 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3683 const char *root_path, const char *path)
3685 const struct got_error *err;
3686 char *parent_path, *next_parent_path = NULL;
3688 err = add_ignores(ignores, root_path, "", -1,
3689 ".cvsignore");
3690 if (err)
3691 return err;
3693 err = add_ignores(ignores, root_path, "", -1,
3694 ".gitignore");
3695 if (err)
3696 return err;
3698 err = got_path_dirname(&parent_path, path);
3699 if (err) {
3700 if (err->code == GOT_ERR_BAD_PATH)
3701 return NULL; /* cannot traverse parent */
3702 return err;
3704 for (;;) {
3705 err = add_ignores(ignores, root_path, parent_path, -1,
3706 ".cvsignore");
3707 if (err)
3708 break;
3709 err = add_ignores(ignores, root_path, parent_path, -1,
3710 ".gitignore");
3711 if (err)
3712 break;
3713 err = got_path_dirname(&next_parent_path, parent_path);
3714 if (err) {
3715 if (err->code == GOT_ERR_BAD_PATH)
3716 err = NULL; /* traversed everything */
3717 break;
3719 if (got_path_is_root_dir(parent_path))
3720 break;
3721 free(parent_path);
3722 parent_path = next_parent_path;
3723 next_parent_path = NULL;
3726 free(parent_path);
3727 free(next_parent_path);
3728 return err;
3731 static const struct got_error *
3732 worktree_status(struct got_worktree *worktree, const char *path,
3733 struct got_fileindex *fileindex, struct got_repository *repo,
3734 got_worktree_status_cb status_cb, void *status_arg,
3735 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3736 int report_unchanged)
3738 const struct got_error *err = NULL;
3739 int fd = -1;
3740 struct got_fileindex_diff_dir_cb fdiff_cb;
3741 struct diff_dir_cb_arg arg;
3742 char *ondisk_path = NULL;
3743 struct got_pathlist_head ignores;
3745 TAILQ_INIT(&ignores);
3747 if (asprintf(&ondisk_path, "%s%s%s",
3748 worktree->root_path, path[0] ? "/" : "", path) == -1)
3749 return got_error_from_errno("asprintf");
3751 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3752 if (fd == -1) {
3753 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3754 errno != ELOOP)
3755 err = got_error_from_errno2("open", ondisk_path);
3756 else {
3757 if (!no_ignores) {
3758 err = add_ignores_from_parent_paths(&ignores,
3759 worktree->root_path, ondisk_path);
3760 if (err)
3761 goto done;
3763 err = report_single_file_status(path, ondisk_path,
3764 fileindex, status_cb, status_arg, repo,
3765 report_unchanged, &ignores, no_ignores);
3767 } else {
3768 fdiff_cb.diff_old_new = status_old_new;
3769 fdiff_cb.diff_old = status_old;
3770 fdiff_cb.diff_new = status_new;
3771 fdiff_cb.diff_traverse = status_traverse;
3772 arg.fileindex = fileindex;
3773 arg.worktree = worktree;
3774 arg.status_path = path;
3775 arg.status_path_len = strlen(path);
3776 arg.repo = repo;
3777 arg.status_cb = status_cb;
3778 arg.status_arg = status_arg;
3779 arg.cancel_cb = cancel_cb;
3780 arg.cancel_arg = cancel_arg;
3781 arg.report_unchanged = report_unchanged;
3782 arg.no_ignores = no_ignores;
3783 if (!no_ignores) {
3784 err = add_ignores_from_parent_paths(&ignores,
3785 worktree->root_path, path);
3786 if (err)
3787 goto done;
3789 arg.ignores = &ignores;
3790 err = got_fileindex_diff_dir(fileindex, fd,
3791 worktree->root_path, path, repo, &fdiff_cb, &arg);
3793 done:
3794 free_ignores(&ignores);
3795 if (fd != -1 && close(fd) == -1 && err == NULL)
3796 err = got_error_from_errno("close");
3797 free(ondisk_path);
3798 return err;
3801 const struct got_error *
3802 got_worktree_status(struct got_worktree *worktree,
3803 struct got_pathlist_head *paths, struct got_repository *repo,
3804 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3805 got_cancel_cb cancel_cb, void *cancel_arg)
3807 const struct got_error *err = NULL;
3808 char *fileindex_path = NULL;
3809 struct got_fileindex *fileindex = NULL;
3810 struct got_pathlist_entry *pe;
3812 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3813 if (err)
3814 return err;
3816 TAILQ_FOREACH(pe, paths, entry) {
3817 err = worktree_status(worktree, pe->path, fileindex, repo,
3818 status_cb, status_arg, cancel_cb, cancel_arg,
3819 no_ignores, 0);
3820 if (err)
3821 break;
3823 free(fileindex_path);
3824 got_fileindex_free(fileindex);
3825 return err;
3828 const struct got_error *
3829 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3830 const char *arg)
3832 const struct got_error *err = NULL;
3833 char *resolved = NULL, *cwd = NULL, *path = NULL;
3834 size_t len;
3835 struct stat sb;
3836 char *abspath = NULL;
3837 char canonpath[PATH_MAX];
3839 *wt_path = NULL;
3841 cwd = getcwd(NULL, 0);
3842 if (cwd == NULL)
3843 return got_error_from_errno("getcwd");
3845 if (lstat(arg, &sb) == -1) {
3846 if (errno != ENOENT) {
3847 err = got_error_from_errno2("lstat", arg);
3848 goto done;
3850 sb.st_mode = 0;
3852 if (S_ISLNK(sb.st_mode)) {
3854 * We cannot use realpath(3) with symlinks since we want to
3855 * operate on the symlink itself.
3856 * But we can make the path absolute, assuming it is relative
3857 * to the current working directory, and then canonicalize it.
3859 if (!got_path_is_absolute(arg)) {
3860 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3861 err = got_error_from_errno("asprintf");
3862 goto done;
3866 err = got_canonpath(abspath ? abspath : arg, canonpath,
3867 sizeof(canonpath));
3868 if (err)
3869 goto done;
3870 resolved = strdup(canonpath);
3871 if (resolved == NULL) {
3872 err = got_error_from_errno("strdup");
3873 goto done;
3875 } else {
3876 resolved = realpath(arg, NULL);
3877 if (resolved == NULL) {
3878 if (errno != ENOENT) {
3879 err = got_error_from_errno2("realpath", arg);
3880 goto done;
3882 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3883 err = got_error_from_errno("asprintf");
3884 goto done;
3886 err = got_canonpath(abspath, canonpath,
3887 sizeof(canonpath));
3888 if (err)
3889 goto done;
3890 resolved = strdup(canonpath);
3891 if (resolved == NULL) {
3892 err = got_error_from_errno("strdup");
3893 goto done;
3898 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3899 strlen(got_worktree_get_root_path(worktree)))) {
3900 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3901 goto done;
3904 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3905 err = got_path_skip_common_ancestor(&path,
3906 got_worktree_get_root_path(worktree), resolved);
3907 if (err)
3908 goto done;
3909 } else {
3910 path = strdup("");
3911 if (path == NULL) {
3912 err = got_error_from_errno("strdup");
3913 goto done;
3917 /* XXX status walk can't deal with trailing slash! */
3918 len = strlen(path);
3919 while (len > 0 && path[len - 1] == '/') {
3920 path[len - 1] = '\0';
3921 len--;
3923 done:
3924 free(abspath);
3925 free(resolved);
3926 free(cwd);
3927 if (err == NULL)
3928 *wt_path = path;
3929 else
3930 free(path);
3931 return err;
3934 struct schedule_addition_args {
3935 struct got_worktree *worktree;
3936 struct got_fileindex *fileindex;
3937 got_worktree_checkout_cb progress_cb;
3938 void *progress_arg;
3939 struct got_repository *repo;
3942 static const struct got_error *
3943 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3944 const char *relpath, struct got_object_id *blob_id,
3945 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3946 int dirfd, const char *de_name)
3948 struct schedule_addition_args *a = arg;
3949 const struct got_error *err = NULL;
3950 struct got_fileindex_entry *ie;
3951 struct stat sb;
3952 char *ondisk_path;
3954 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3955 relpath) == -1)
3956 return got_error_from_errno("asprintf");
3958 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3959 if (ie) {
3960 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3961 de_name, a->repo);
3962 if (err)
3963 goto done;
3964 /* Re-adding an existing entry is a no-op. */
3965 if (status == GOT_STATUS_ADD)
3966 goto done;
3967 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3968 if (err)
3969 goto done;
3972 if (status != GOT_STATUS_UNVERSIONED) {
3973 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3974 goto done;
3977 err = got_fileindex_entry_alloc(&ie, relpath);
3978 if (err)
3979 goto done;
3980 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3981 relpath, NULL, NULL, 1);
3982 if (err) {
3983 got_fileindex_entry_free(ie);
3984 goto done;
3986 err = got_fileindex_entry_add(a->fileindex, ie);
3987 if (err) {
3988 got_fileindex_entry_free(ie);
3989 goto done;
3991 done:
3992 free(ondisk_path);
3993 if (err)
3994 return err;
3995 if (status == GOT_STATUS_ADD)
3996 return NULL;
3997 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4000 const struct got_error *
4001 got_worktree_schedule_add(struct got_worktree *worktree,
4002 struct got_pathlist_head *paths,
4003 got_worktree_checkout_cb progress_cb, void *progress_arg,
4004 struct got_repository *repo, int no_ignores)
4006 struct got_fileindex *fileindex = NULL;
4007 char *fileindex_path = NULL;
4008 const struct got_error *err = NULL, *sync_err, *unlockerr;
4009 struct got_pathlist_entry *pe;
4010 struct schedule_addition_args saa;
4012 err = lock_worktree(worktree, LOCK_EX);
4013 if (err)
4014 return err;
4016 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4017 if (err)
4018 goto done;
4020 saa.worktree = worktree;
4021 saa.fileindex = fileindex;
4022 saa.progress_cb = progress_cb;
4023 saa.progress_arg = progress_arg;
4024 saa.repo = repo;
4026 TAILQ_FOREACH(pe, paths, entry) {
4027 err = worktree_status(worktree, pe->path, fileindex, repo,
4028 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4029 if (err)
4030 break;
4032 sync_err = sync_fileindex(fileindex, fileindex_path);
4033 if (sync_err && err == NULL)
4034 err = sync_err;
4035 done:
4036 free(fileindex_path);
4037 if (fileindex)
4038 got_fileindex_free(fileindex);
4039 unlockerr = lock_worktree(worktree, LOCK_SH);
4040 if (unlockerr && err == NULL)
4041 err = unlockerr;
4042 return err;
4045 struct schedule_deletion_args {
4046 struct got_worktree *worktree;
4047 struct got_fileindex *fileindex;
4048 got_worktree_delete_cb progress_cb;
4049 void *progress_arg;
4050 struct got_repository *repo;
4051 int delete_local_mods;
4052 int keep_on_disk;
4053 const char *status_codes;
4056 static const struct got_error *
4057 schedule_for_deletion(void *arg, unsigned char status,
4058 unsigned char staged_status, const char *relpath,
4059 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4060 struct got_object_id *commit_id, int dirfd, const char *de_name)
4062 struct schedule_deletion_args *a = arg;
4063 const struct got_error *err = NULL;
4064 struct got_fileindex_entry *ie = NULL;
4065 struct stat sb;
4066 char *ondisk_path;
4068 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4069 if (ie == NULL)
4070 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4072 staged_status = get_staged_status(ie);
4073 if (staged_status != GOT_STATUS_NO_CHANGE) {
4074 if (staged_status == GOT_STATUS_DELETE)
4075 return NULL;
4076 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4079 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4080 relpath) == -1)
4081 return got_error_from_errno("asprintf");
4083 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4084 a->repo);
4085 if (err)
4086 goto done;
4088 if (a->status_codes) {
4089 size_t ncodes = strlen(a->status_codes);
4090 int i;
4091 for (i = 0; i < ncodes ; i++) {
4092 if (status == a->status_codes[i])
4093 break;
4095 if (i == ncodes) {
4096 /* Do not delete files in non-matching status. */
4097 free(ondisk_path);
4098 return NULL;
4100 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4101 a->status_codes[i] != GOT_STATUS_MISSING) {
4102 static char msg[64];
4103 snprintf(msg, sizeof(msg),
4104 "invalid status code '%c'", a->status_codes[i]);
4105 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4106 goto done;
4110 if (status != GOT_STATUS_NO_CHANGE) {
4111 if (status == GOT_STATUS_DELETE)
4112 goto done;
4113 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4114 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4115 goto done;
4117 if (status != GOT_STATUS_MODIFY &&
4118 status != GOT_STATUS_MISSING) {
4119 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4120 goto done;
4124 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4125 size_t root_len;
4127 if (dirfd != -1) {
4128 if (unlinkat(dirfd, de_name, 0) != 0) {
4129 err = got_error_from_errno2("unlinkat",
4130 ondisk_path);
4131 goto done;
4133 } else if (unlink(ondisk_path) != 0) {
4134 err = got_error_from_errno2("unlink", ondisk_path);
4135 goto done;
4138 root_len = strlen(a->worktree->root_path);
4139 do {
4140 char *parent;
4141 err = got_path_dirname(&parent, ondisk_path);
4142 if (err)
4143 goto done;
4144 free(ondisk_path);
4145 ondisk_path = parent;
4146 if (rmdir(ondisk_path) == -1) {
4147 if (errno != ENOTEMPTY)
4148 err = got_error_from_errno2("rmdir",
4149 ondisk_path);
4150 break;
4152 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4153 strlen(ondisk_path), root_len) != 0);
4156 got_fileindex_entry_mark_deleted_from_disk(ie);
4157 done:
4158 free(ondisk_path);
4159 if (err)
4160 return err;
4161 if (status == GOT_STATUS_DELETE)
4162 return NULL;
4163 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4164 staged_status, relpath);
4167 const struct got_error *
4168 got_worktree_schedule_delete(struct got_worktree *worktree,
4169 struct got_pathlist_head *paths, int delete_local_mods,
4170 const char *status_codes,
4171 got_worktree_delete_cb progress_cb, void *progress_arg,
4172 struct got_repository *repo, int keep_on_disk)
4174 struct got_fileindex *fileindex = NULL;
4175 char *fileindex_path = NULL;
4176 const struct got_error *err = NULL, *sync_err, *unlockerr;
4177 struct got_pathlist_entry *pe;
4178 struct schedule_deletion_args sda;
4180 err = lock_worktree(worktree, LOCK_EX);
4181 if (err)
4182 return err;
4184 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4185 if (err)
4186 goto done;
4188 sda.worktree = worktree;
4189 sda.fileindex = fileindex;
4190 sda.progress_cb = progress_cb;
4191 sda.progress_arg = progress_arg;
4192 sda.repo = repo;
4193 sda.delete_local_mods = delete_local_mods;
4194 sda.keep_on_disk = keep_on_disk;
4195 sda.status_codes = status_codes;
4197 TAILQ_FOREACH(pe, paths, entry) {
4198 err = worktree_status(worktree, pe->path, fileindex, repo,
4199 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4200 if (err)
4201 break;
4203 sync_err = sync_fileindex(fileindex, fileindex_path);
4204 if (sync_err && err == NULL)
4205 err = sync_err;
4206 done:
4207 free(fileindex_path);
4208 if (fileindex)
4209 got_fileindex_free(fileindex);
4210 unlockerr = lock_worktree(worktree, LOCK_SH);
4211 if (unlockerr && err == NULL)
4212 err = unlockerr;
4213 return err;
4216 static const struct got_error *
4217 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4219 const struct got_error *err = NULL;
4220 char *line = NULL;
4221 size_t linesize = 0, n;
4222 ssize_t linelen;
4224 linelen = getline(&line, &linesize, infile);
4225 if (linelen == -1) {
4226 if (ferror(infile)) {
4227 err = got_error_from_errno("getline");
4228 goto done;
4230 return NULL;
4232 if (outfile) {
4233 n = fwrite(line, 1, linelen, outfile);
4234 if (n != linelen) {
4235 err = got_ferror(outfile, GOT_ERR_IO);
4236 goto done;
4239 if (rejectfile) {
4240 n = fwrite(line, 1, linelen, rejectfile);
4241 if (n != linelen)
4242 err = got_ferror(outfile, GOT_ERR_IO);
4244 done:
4245 free(line);
4246 return err;
4249 static const struct got_error *
4250 skip_one_line(FILE *f)
4252 char *line = NULL;
4253 size_t linesize = 0;
4254 ssize_t linelen;
4256 linelen = getline(&line, &linesize, f);
4257 if (linelen == -1) {
4258 if (ferror(f))
4259 return got_error_from_errno("getline");
4260 return NULL;
4262 free(line);
4263 return NULL;
4266 static const struct got_error *
4267 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4268 int start_old, int end_old, int start_new, int end_new,
4269 FILE *outfile, FILE *rejectfile)
4271 const struct got_error *err;
4273 /* Copy old file's lines leading up to patch. */
4274 while (!feof(f1) && *line_cur1 < start_old) {
4275 err = copy_one_line(f1, outfile, NULL);
4276 if (err)
4277 return err;
4278 (*line_cur1)++;
4280 /* Skip new file's lines leading up to patch. */
4281 while (!feof(f2) && *line_cur2 < start_new) {
4282 if (rejectfile)
4283 err = copy_one_line(f2, NULL, rejectfile);
4284 else
4285 err = skip_one_line(f2);
4286 if (err)
4287 return err;
4288 (*line_cur2)++;
4290 /* Copy patched lines. */
4291 while (!feof(f2) && *line_cur2 <= end_new) {
4292 err = copy_one_line(f2, outfile, NULL);
4293 if (err)
4294 return err;
4295 (*line_cur2)++;
4297 /* Skip over old file's replaced lines. */
4298 while (!feof(f1) && *line_cur1 <= end_old) {
4299 if (rejectfile)
4300 err = copy_one_line(f1, NULL, rejectfile);
4301 else
4302 err = skip_one_line(f1);
4303 if (err)
4304 return err;
4305 (*line_cur1)++;
4308 return NULL;
4311 static const struct got_error *
4312 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4313 FILE *outfile, FILE *rejectfile)
4315 const struct got_error *err;
4317 if (outfile) {
4318 /* Copy old file's lines until EOF. */
4319 while (!feof(f1)) {
4320 err = copy_one_line(f1, outfile, NULL);
4321 if (err)
4322 return err;
4323 (*line_cur1)++;
4326 if (rejectfile) {
4327 /* Copy new file's lines until EOF. */
4328 while (!feof(f2)) {
4329 err = copy_one_line(f2, NULL, rejectfile);
4330 if (err)
4331 return err;
4332 (*line_cur2)++;
4336 return NULL;
4339 static const struct got_error *
4340 apply_or_reject_change(int *choice, int *nchunks_used,
4341 struct diff_result *diff_result, int n,
4342 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4343 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4344 got_worktree_patch_cb patch_cb, void *patch_arg)
4346 const struct got_error *err = NULL;
4347 struct diff_chunk_context cc = {};
4348 int start_old, end_old, start_new, end_new;
4349 FILE *hunkfile;
4350 struct diff_output_unidiff_state *diff_state;
4351 struct diff_input_info diff_info;
4352 int rc;
4354 *choice = GOT_PATCH_CHOICE_NONE;
4356 /* Get changed line numbers without context lines for copy_change(). */
4357 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4358 start_old = cc.left.start;
4359 end_old = cc.left.end;
4360 start_new = cc.right.start;
4361 end_new = cc.right.end;
4363 /* Get the same change with context lines for display. */
4364 memset(&cc, 0, sizeof(cc));
4365 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4367 memset(&diff_info, 0, sizeof(diff_info));
4368 diff_info.left_path = relpath;
4369 diff_info.right_path = relpath;
4371 diff_state = diff_output_unidiff_state_alloc();
4372 if (diff_state == NULL)
4373 return got_error_set_errno(ENOMEM,
4374 "diff_output_unidiff_state_alloc");
4376 hunkfile = got_opentemp();
4377 if (hunkfile == NULL) {
4378 err = got_error_from_errno("got_opentemp");
4379 goto done;
4382 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4383 diff_result, &cc);
4384 if (rc != DIFF_RC_OK) {
4385 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4386 goto done;
4389 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4390 err = got_ferror(hunkfile, GOT_ERR_IO);
4391 goto done;
4394 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4395 hunkfile, changeno, nchanges);
4396 if (err)
4397 goto done;
4399 switch (*choice) {
4400 case GOT_PATCH_CHOICE_YES:
4401 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4402 end_old, start_new, end_new, outfile, rejectfile);
4403 break;
4404 case GOT_PATCH_CHOICE_NO:
4405 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4406 end_old, start_new, end_new, rejectfile, outfile);
4407 break;
4408 case GOT_PATCH_CHOICE_QUIT:
4409 break;
4410 default:
4411 err = got_error(GOT_ERR_PATCH_CHOICE);
4412 break;
4414 done:
4415 diff_output_unidiff_state_free(diff_state);
4416 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4417 err = got_error_from_errno("fclose");
4418 return err;
4421 struct revert_file_args {
4422 struct got_worktree *worktree;
4423 struct got_fileindex *fileindex;
4424 got_worktree_checkout_cb progress_cb;
4425 void *progress_arg;
4426 got_worktree_patch_cb patch_cb;
4427 void *patch_arg;
4428 struct got_repository *repo;
4429 int unlink_added_files;
4432 static const struct got_error *
4433 create_patched_content(char **path_outfile, int reverse_patch,
4434 struct got_object_id *blob_id, const char *path2,
4435 int dirfd2, const char *de_name2,
4436 const char *relpath, struct got_repository *repo,
4437 got_worktree_patch_cb patch_cb, void *patch_arg)
4439 const struct got_error *err, *free_err;
4440 struct got_blob_object *blob = NULL;
4441 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4442 int fd2 = -1;
4443 char link_target[PATH_MAX];
4444 ssize_t link_len = 0;
4445 char *path1 = NULL, *id_str = NULL;
4446 struct stat sb2;
4447 struct got_diffreg_result *diffreg_result = NULL;
4448 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4449 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4451 *path_outfile = NULL;
4453 err = got_object_id_str(&id_str, blob_id);
4454 if (err)
4455 return err;
4457 if (dirfd2 != -1) {
4458 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4459 if (fd2 == -1) {
4460 if (errno != ELOOP) {
4461 err = got_error_from_errno2("openat", path2);
4462 goto done;
4464 link_len = readlinkat(dirfd2, de_name2,
4465 link_target, sizeof(link_target));
4466 if (link_len == -1)
4467 return got_error_from_errno2("readlinkat", path2);
4468 sb2.st_mode = S_IFLNK;
4469 sb2.st_size = link_len;
4471 } else {
4472 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4473 if (fd2 == -1) {
4474 if (errno != ELOOP) {
4475 err = got_error_from_errno2("open", path2);
4476 goto done;
4478 link_len = readlink(path2, link_target,
4479 sizeof(link_target));
4480 if (link_len == -1)
4481 return got_error_from_errno2("readlink", path2);
4482 sb2.st_mode = S_IFLNK;
4483 sb2.st_size = link_len;
4486 if (fd2 != -1) {
4487 if (fstat(fd2, &sb2) == -1) {
4488 err = got_error_from_errno2("fstat", path2);
4489 goto done;
4492 f2 = fdopen(fd2, "r");
4493 if (f2 == NULL) {
4494 err = got_error_from_errno2("fdopen", path2);
4495 goto done;
4497 fd2 = -1;
4498 } else {
4499 size_t n;
4500 f2 = got_opentemp();
4501 if (f2 == NULL) {
4502 err = got_error_from_errno2("got_opentemp", path2);
4503 goto done;
4505 n = fwrite(link_target, 1, link_len, f2);
4506 if (n != link_len) {
4507 err = got_ferror(f2, GOT_ERR_IO);
4508 goto done;
4510 if (fflush(f2) == EOF) {
4511 err = got_error_from_errno("fflush");
4512 goto done;
4514 rewind(f2);
4517 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4518 if (err)
4519 goto done;
4521 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4522 if (err)
4523 goto done;
4525 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4526 if (err)
4527 goto done;
4529 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4530 NULL);
4531 if (err)
4532 goto done;
4534 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4535 if (err)
4536 goto done;
4538 if (fseek(f1, 0L, SEEK_SET) == -1)
4539 return got_ferror(f1, GOT_ERR_IO);
4540 if (fseek(f2, 0L, SEEK_SET) == -1)
4541 return got_ferror(f2, GOT_ERR_IO);
4543 /* Count the number of actual changes in the diff result. */
4544 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4545 struct diff_chunk_context cc = {};
4546 diff_chunk_context_load_change(&cc, &nchunks_used,
4547 diffreg_result->result, n, 0);
4548 nchanges++;
4550 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4551 int choice;
4552 err = apply_or_reject_change(&choice, &nchunks_used,
4553 diffreg_result->result, n, relpath, f1, f2,
4554 &line_cur1, &line_cur2,
4555 reverse_patch ? NULL : outfile,
4556 reverse_patch ? outfile : NULL,
4557 ++i, nchanges, patch_cb, patch_arg);
4558 if (err)
4559 goto done;
4560 if (choice == GOT_PATCH_CHOICE_YES)
4561 have_content = 1;
4562 else if (choice == GOT_PATCH_CHOICE_QUIT)
4563 break;
4565 if (have_content) {
4566 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4567 reverse_patch ? NULL : outfile,
4568 reverse_patch ? outfile : NULL);
4569 if (err)
4570 goto done;
4572 if (!S_ISLNK(sb2.st_mode)) {
4573 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4574 err = got_error_from_errno2("fchmod", path2);
4575 goto done;
4579 done:
4580 free(id_str);
4581 if (blob)
4582 got_object_blob_close(blob);
4583 free_err = got_diffreg_result_free(diffreg_result);
4584 if (err == NULL)
4585 err = free_err;
4586 if (f1 && fclose(f1) == EOF && err == NULL)
4587 err = got_error_from_errno2("fclose", path1);
4588 if (f2 && fclose(f2) == EOF && err == NULL)
4589 err = got_error_from_errno2("fclose", path2);
4590 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4591 err = got_error_from_errno2("close", path2);
4592 if (outfile && fclose(outfile) == EOF && err == NULL)
4593 err = got_error_from_errno2("fclose", *path_outfile);
4594 if (path1 && unlink(path1) == -1 && err == NULL)
4595 err = got_error_from_errno2("unlink", path1);
4596 if (err || !have_content) {
4597 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4598 err = got_error_from_errno2("unlink", *path_outfile);
4599 free(*path_outfile);
4600 *path_outfile = NULL;
4602 free(path1);
4603 return err;
4606 static const struct got_error *
4607 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4608 const char *relpath, struct got_object_id *blob_id,
4609 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4610 int dirfd, const char *de_name)
4612 struct revert_file_args *a = arg;
4613 const struct got_error *err = NULL;
4614 char *parent_path = NULL;
4615 struct got_fileindex_entry *ie;
4616 struct got_tree_object *tree = NULL;
4617 struct got_object_id *tree_id = NULL;
4618 const struct got_tree_entry *te = NULL;
4619 char *tree_path = NULL, *te_name;
4620 char *ondisk_path = NULL, *path_content = NULL;
4621 struct got_blob_object *blob = NULL;
4623 /* Reverting a staged deletion is a no-op. */
4624 if (status == GOT_STATUS_DELETE &&
4625 staged_status != GOT_STATUS_NO_CHANGE)
4626 return NULL;
4628 if (status == GOT_STATUS_UNVERSIONED)
4629 return (*a->progress_cb)(a->progress_arg,
4630 GOT_STATUS_UNVERSIONED, relpath);
4632 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4633 if (ie == NULL)
4634 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4636 /* Construct in-repository path of tree which contains this blob. */
4637 err = got_path_dirname(&parent_path, ie->path);
4638 if (err) {
4639 if (err->code != GOT_ERR_BAD_PATH)
4640 goto done;
4641 parent_path = strdup("/");
4642 if (parent_path == NULL) {
4643 err = got_error_from_errno("strdup");
4644 goto done;
4647 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4648 tree_path = strdup(parent_path);
4649 if (tree_path == NULL) {
4650 err = got_error_from_errno("strdup");
4651 goto done;
4653 } else {
4654 if (got_path_is_root_dir(parent_path)) {
4655 tree_path = strdup(a->worktree->path_prefix);
4656 if (tree_path == NULL) {
4657 err = got_error_from_errno("strdup");
4658 goto done;
4660 } else {
4661 if (asprintf(&tree_path, "%s/%s",
4662 a->worktree->path_prefix, parent_path) == -1) {
4663 err = got_error_from_errno("asprintf");
4664 goto done;
4669 err = got_object_id_by_path(&tree_id, a->repo,
4670 a->worktree->base_commit_id, tree_path);
4671 if (err) {
4672 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4673 (status == GOT_STATUS_ADD ||
4674 staged_status == GOT_STATUS_ADD)))
4675 goto done;
4676 } else {
4677 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4678 if (err)
4679 goto done;
4681 err = got_path_basename(&te_name, ie->path);
4682 if (err)
4683 goto done;
4685 te = got_object_tree_find_entry(tree, te_name);
4686 free(te_name);
4687 if (te == NULL && status != GOT_STATUS_ADD &&
4688 staged_status != GOT_STATUS_ADD) {
4689 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4690 goto done;
4694 switch (status) {
4695 case GOT_STATUS_ADD:
4696 if (a->patch_cb) {
4697 int choice = GOT_PATCH_CHOICE_NONE;
4698 err = (*a->patch_cb)(&choice, a->patch_arg,
4699 status, ie->path, NULL, 1, 1);
4700 if (err)
4701 goto done;
4702 if (choice != GOT_PATCH_CHOICE_YES)
4703 break;
4705 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4706 ie->path);
4707 if (err)
4708 goto done;
4709 got_fileindex_entry_remove(a->fileindex, ie);
4710 if (a->unlink_added_files) {
4711 if (asprintf(&ondisk_path, "%s/%s",
4712 got_worktree_get_root_path(a->worktree),
4713 relpath) == -1) {
4714 err = got_error_from_errno("asprintf");
4715 goto done;
4717 if (unlink(ondisk_path) == -1) {
4718 err = got_error_from_errno2("unlink",
4719 ondisk_path);
4720 break;
4723 break;
4724 case GOT_STATUS_DELETE:
4725 if (a->patch_cb) {
4726 int choice = GOT_PATCH_CHOICE_NONE;
4727 err = (*a->patch_cb)(&choice, a->patch_arg,
4728 status, ie->path, NULL, 1, 1);
4729 if (err)
4730 goto done;
4731 if (choice != GOT_PATCH_CHOICE_YES)
4732 break;
4734 /* fall through */
4735 case GOT_STATUS_MODIFY:
4736 case GOT_STATUS_MODE_CHANGE:
4737 case GOT_STATUS_CONFLICT:
4738 case GOT_STATUS_MISSING: {
4739 struct got_object_id id;
4740 if (staged_status == GOT_STATUS_ADD ||
4741 staged_status == GOT_STATUS_MODIFY) {
4742 memcpy(id.sha1, ie->staged_blob_sha1,
4743 SHA1_DIGEST_LENGTH);
4744 } else
4745 memcpy(id.sha1, ie->blob_sha1,
4746 SHA1_DIGEST_LENGTH);
4747 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4748 if (err)
4749 goto done;
4751 if (asprintf(&ondisk_path, "%s/%s",
4752 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4753 err = got_error_from_errno("asprintf");
4754 goto done;
4757 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4758 status == GOT_STATUS_CONFLICT)) {
4759 int is_bad_symlink = 0;
4760 err = create_patched_content(&path_content, 1, &id,
4761 ondisk_path, dirfd, de_name, ie->path, a->repo,
4762 a->patch_cb, a->patch_arg);
4763 if (err || path_content == NULL)
4764 break;
4765 if (te && S_ISLNK(te->mode)) {
4766 if (unlink(path_content) == -1) {
4767 err = got_error_from_errno2("unlink",
4768 path_content);
4769 break;
4771 err = install_symlink(&is_bad_symlink,
4772 a->worktree, ondisk_path, ie->path,
4773 blob, 0, 1, 0, a->repo,
4774 a->progress_cb, a->progress_arg);
4775 } else {
4776 if (rename(path_content, ondisk_path) == -1) {
4777 err = got_error_from_errno3("rename",
4778 path_content, ondisk_path);
4779 goto done;
4782 } else {
4783 int is_bad_symlink = 0;
4784 if (te && S_ISLNK(te->mode)) {
4785 err = install_symlink(&is_bad_symlink,
4786 a->worktree, ondisk_path, ie->path,
4787 blob, 0, 1, 0, a->repo,
4788 a->progress_cb, a->progress_arg);
4789 } else {
4790 err = install_blob(a->worktree, ondisk_path,
4791 ie->path,
4792 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4793 got_fileindex_perms_to_st(ie), blob,
4794 0, 1, 0, 0, a->repo,
4795 a->progress_cb, a->progress_arg);
4797 if (err)
4798 goto done;
4799 if (status == GOT_STATUS_DELETE ||
4800 status == GOT_STATUS_MODE_CHANGE) {
4801 err = got_fileindex_entry_update(ie,
4802 a->worktree->root_fd, relpath,
4803 blob->id.sha1,
4804 a->worktree->base_commit_id->sha1, 1);
4805 if (err)
4806 goto done;
4808 if (is_bad_symlink) {
4809 got_fileindex_entry_filetype_set(ie,
4810 GOT_FILEIDX_MODE_BAD_SYMLINK);
4813 break;
4815 default:
4816 break;
4818 done:
4819 free(ondisk_path);
4820 free(path_content);
4821 free(parent_path);
4822 free(tree_path);
4823 if (blob)
4824 got_object_blob_close(blob);
4825 if (tree)
4826 got_object_tree_close(tree);
4827 free(tree_id);
4828 return err;
4831 const struct got_error *
4832 got_worktree_revert(struct got_worktree *worktree,
4833 struct got_pathlist_head *paths,
4834 got_worktree_checkout_cb progress_cb, void *progress_arg,
4835 got_worktree_patch_cb patch_cb, void *patch_arg,
4836 struct got_repository *repo)
4838 struct got_fileindex *fileindex = NULL;
4839 char *fileindex_path = NULL;
4840 const struct got_error *err = NULL, *unlockerr = NULL;
4841 const struct got_error *sync_err = NULL;
4842 struct got_pathlist_entry *pe;
4843 struct revert_file_args rfa;
4845 err = lock_worktree(worktree, LOCK_EX);
4846 if (err)
4847 return err;
4849 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4850 if (err)
4851 goto done;
4853 rfa.worktree = worktree;
4854 rfa.fileindex = fileindex;
4855 rfa.progress_cb = progress_cb;
4856 rfa.progress_arg = progress_arg;
4857 rfa.patch_cb = patch_cb;
4858 rfa.patch_arg = patch_arg;
4859 rfa.repo = repo;
4860 rfa.unlink_added_files = 0;
4861 TAILQ_FOREACH(pe, paths, entry) {
4862 err = worktree_status(worktree, pe->path, fileindex, repo,
4863 revert_file, &rfa, NULL, NULL, 0, 0);
4864 if (err)
4865 break;
4867 sync_err = sync_fileindex(fileindex, fileindex_path);
4868 if (sync_err && err == NULL)
4869 err = sync_err;
4870 done:
4871 free(fileindex_path);
4872 if (fileindex)
4873 got_fileindex_free(fileindex);
4874 unlockerr = lock_worktree(worktree, LOCK_SH);
4875 if (unlockerr && err == NULL)
4876 err = unlockerr;
4877 return err;
4880 static void
4881 free_commitable(struct got_commitable *ct)
4883 free(ct->path);
4884 free(ct->in_repo_path);
4885 free(ct->ondisk_path);
4886 free(ct->blob_id);
4887 free(ct->base_blob_id);
4888 free(ct->staged_blob_id);
4889 free(ct->base_commit_id);
4890 free(ct);
4893 struct collect_commitables_arg {
4894 struct got_pathlist_head *commitable_paths;
4895 struct got_repository *repo;
4896 struct got_worktree *worktree;
4897 struct got_fileindex *fileindex;
4898 int have_staged_files;
4899 int allow_bad_symlinks;
4902 static const struct got_error *
4903 collect_commitables(void *arg, unsigned char status,
4904 unsigned char staged_status, const char *relpath,
4905 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4906 struct got_object_id *commit_id, int dirfd, const char *de_name)
4908 struct collect_commitables_arg *a = arg;
4909 const struct got_error *err = NULL;
4910 struct got_commitable *ct = NULL;
4911 struct got_pathlist_entry *new = NULL;
4912 char *parent_path = NULL, *path = NULL;
4913 struct stat sb;
4915 if (a->have_staged_files) {
4916 if (staged_status != GOT_STATUS_MODIFY &&
4917 staged_status != GOT_STATUS_ADD &&
4918 staged_status != GOT_STATUS_DELETE)
4919 return NULL;
4920 } else {
4921 if (status == GOT_STATUS_CONFLICT)
4922 return got_error(GOT_ERR_COMMIT_CONFLICT);
4924 if (status != GOT_STATUS_MODIFY &&
4925 status != GOT_STATUS_MODE_CHANGE &&
4926 status != GOT_STATUS_ADD &&
4927 status != GOT_STATUS_DELETE)
4928 return NULL;
4931 if (asprintf(&path, "/%s", relpath) == -1) {
4932 err = got_error_from_errno("asprintf");
4933 goto done;
4935 if (strcmp(path, "/") == 0) {
4936 parent_path = strdup("");
4937 if (parent_path == NULL)
4938 return got_error_from_errno("strdup");
4939 } else {
4940 err = got_path_dirname(&parent_path, path);
4941 if (err)
4942 return err;
4945 ct = calloc(1, sizeof(*ct));
4946 if (ct == NULL) {
4947 err = got_error_from_errno("calloc");
4948 goto done;
4951 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4952 relpath) == -1) {
4953 err = got_error_from_errno("asprintf");
4954 goto done;
4957 if (staged_status == GOT_STATUS_ADD ||
4958 staged_status == GOT_STATUS_MODIFY) {
4959 struct got_fileindex_entry *ie;
4960 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4961 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4962 case GOT_FILEIDX_MODE_REGULAR_FILE:
4963 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4964 ct->mode = S_IFREG;
4965 break;
4966 case GOT_FILEIDX_MODE_SYMLINK:
4967 ct->mode = S_IFLNK;
4968 break;
4969 default:
4970 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4971 goto done;
4973 ct->mode |= got_fileindex_entry_perms_get(ie);
4974 } else if (status != GOT_STATUS_DELETE &&
4975 staged_status != GOT_STATUS_DELETE) {
4976 if (dirfd != -1) {
4977 if (fstatat(dirfd, de_name, &sb,
4978 AT_SYMLINK_NOFOLLOW) == -1) {
4979 err = got_error_from_errno2("fstatat",
4980 ct->ondisk_path);
4981 goto done;
4983 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4984 err = got_error_from_errno2("lstat", ct->ondisk_path);
4985 goto done;
4987 ct->mode = sb.st_mode;
4990 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4991 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4992 relpath) == -1) {
4993 err = got_error_from_errno("asprintf");
4994 goto done;
4997 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4998 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4999 int is_bad_symlink;
5000 char target_path[PATH_MAX];
5001 ssize_t target_len;
5002 target_len = readlink(ct->ondisk_path, target_path,
5003 sizeof(target_path));
5004 if (target_len == -1) {
5005 err = got_error_from_errno2("readlink",
5006 ct->ondisk_path);
5007 goto done;
5009 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5010 target_len, ct->ondisk_path, a->worktree->root_path);
5011 if (err)
5012 goto done;
5013 if (is_bad_symlink) {
5014 err = got_error_path(ct->ondisk_path,
5015 GOT_ERR_BAD_SYMLINK);
5016 goto done;
5021 ct->status = status;
5022 ct->staged_status = staged_status;
5023 ct->blob_id = NULL; /* will be filled in when blob gets created */
5024 if (ct->status != GOT_STATUS_ADD &&
5025 ct->staged_status != GOT_STATUS_ADD) {
5026 ct->base_blob_id = got_object_id_dup(blob_id);
5027 if (ct->base_blob_id == NULL) {
5028 err = got_error_from_errno("got_object_id_dup");
5029 goto done;
5031 ct->base_commit_id = got_object_id_dup(commit_id);
5032 if (ct->base_commit_id == NULL) {
5033 err = got_error_from_errno("got_object_id_dup");
5034 goto done;
5037 if (ct->staged_status == GOT_STATUS_ADD ||
5038 ct->staged_status == GOT_STATUS_MODIFY) {
5039 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5040 if (ct->staged_blob_id == NULL) {
5041 err = got_error_from_errno("got_object_id_dup");
5042 goto done;
5045 ct->path = strdup(path);
5046 if (ct->path == NULL) {
5047 err = got_error_from_errno("strdup");
5048 goto done;
5050 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5051 done:
5052 if (ct && (err || new == NULL))
5053 free_commitable(ct);
5054 free(parent_path);
5055 free(path);
5056 return err;
5059 static const struct got_error *write_tree(struct got_object_id **, int *,
5060 struct got_tree_object *, const char *, struct got_pathlist_head *,
5061 got_worktree_status_cb status_cb, void *status_arg,
5062 struct got_repository *);
5064 static const struct got_error *
5065 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5066 struct got_tree_entry *te, const char *parent_path,
5067 struct got_pathlist_head *commitable_paths,
5068 got_worktree_status_cb status_cb, void *status_arg,
5069 struct got_repository *repo)
5071 const struct got_error *err = NULL;
5072 struct got_tree_object *subtree;
5073 char *subpath;
5075 if (asprintf(&subpath, "%s%s%s", parent_path,
5076 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5077 return got_error_from_errno("asprintf");
5079 err = got_object_open_as_tree(&subtree, repo, &te->id);
5080 if (err)
5081 return err;
5083 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5084 commitable_paths, status_cb, status_arg, repo);
5085 got_object_tree_close(subtree);
5086 free(subpath);
5087 return err;
5090 static const struct got_error *
5091 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5093 const struct got_error *err = NULL;
5094 char *ct_parent_path = NULL;
5096 *match = 0;
5098 if (strchr(ct->in_repo_path, '/') == NULL) {
5099 *match = got_path_is_root_dir(path);
5100 return NULL;
5103 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5104 if (err)
5105 return err;
5106 *match = (strcmp(path, ct_parent_path) == 0);
5107 free(ct_parent_path);
5108 return err;
5111 static mode_t
5112 get_ct_file_mode(struct got_commitable *ct)
5114 if (S_ISLNK(ct->mode))
5115 return S_IFLNK;
5117 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5120 static const struct got_error *
5121 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5122 struct got_tree_entry *te, struct got_commitable *ct)
5124 const struct got_error *err = NULL;
5126 *new_te = NULL;
5128 err = got_object_tree_entry_dup(new_te, te);
5129 if (err)
5130 goto done;
5132 (*new_te)->mode = get_ct_file_mode(ct);
5134 if (ct->staged_status == GOT_STATUS_MODIFY)
5135 memcpy(&(*new_te)->id, ct->staged_blob_id,
5136 sizeof((*new_te)->id));
5137 else
5138 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5139 done:
5140 if (err && *new_te) {
5141 free(*new_te);
5142 *new_te = NULL;
5144 return err;
5147 static const struct got_error *
5148 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5149 struct got_commitable *ct)
5151 const struct got_error *err = NULL;
5152 char *ct_name = NULL;
5154 *new_te = NULL;
5156 *new_te = calloc(1, sizeof(**new_te));
5157 if (*new_te == NULL)
5158 return got_error_from_errno("calloc");
5160 err = got_path_basename(&ct_name, ct->path);
5161 if (err)
5162 goto done;
5163 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5164 sizeof((*new_te)->name)) {
5165 err = got_error(GOT_ERR_NO_SPACE);
5166 goto done;
5169 (*new_te)->mode = get_ct_file_mode(ct);
5171 if (ct->staged_status == GOT_STATUS_ADD)
5172 memcpy(&(*new_te)->id, ct->staged_blob_id,
5173 sizeof((*new_te)->id));
5174 else
5175 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5176 done:
5177 free(ct_name);
5178 if (err && *new_te) {
5179 free(*new_te);
5180 *new_te = NULL;
5182 return err;
5185 static const struct got_error *
5186 insert_tree_entry(struct got_tree_entry *new_te,
5187 struct got_pathlist_head *paths)
5189 const struct got_error *err = NULL;
5190 struct got_pathlist_entry *new_pe;
5192 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5193 if (err)
5194 return err;
5195 if (new_pe == NULL)
5196 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5197 return NULL;
5200 static const struct got_error *
5201 report_ct_status(struct got_commitable *ct,
5202 got_worktree_status_cb status_cb, void *status_arg)
5204 const char *ct_path = ct->path;
5205 unsigned char status;
5207 while (ct_path[0] == '/')
5208 ct_path++;
5210 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5211 status = ct->staged_status;
5212 else
5213 status = ct->status;
5215 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5216 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5219 static const struct got_error *
5220 match_modified_subtree(int *modified, struct got_tree_entry *te,
5221 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5223 const struct got_error *err = NULL;
5224 struct got_pathlist_entry *pe;
5225 char *te_path;
5227 *modified = 0;
5229 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5230 got_path_is_root_dir(base_tree_path) ? "" : "/",
5231 te->name) == -1)
5232 return got_error_from_errno("asprintf");
5234 TAILQ_FOREACH(pe, commitable_paths, entry) {
5235 struct got_commitable *ct = pe->data;
5236 *modified = got_path_is_child(ct->in_repo_path, te_path,
5237 strlen(te_path));
5238 if (*modified)
5239 break;
5242 free(te_path);
5243 return err;
5246 static const struct got_error *
5247 match_deleted_or_modified_ct(struct got_commitable **ctp,
5248 struct got_tree_entry *te, const char *base_tree_path,
5249 struct got_pathlist_head *commitable_paths)
5251 const struct got_error *err = NULL;
5252 struct got_pathlist_entry *pe;
5254 *ctp = NULL;
5256 TAILQ_FOREACH(pe, commitable_paths, entry) {
5257 struct got_commitable *ct = pe->data;
5258 char *ct_name = NULL;
5259 int path_matches;
5261 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5262 if (ct->status != GOT_STATUS_MODIFY &&
5263 ct->status != GOT_STATUS_MODE_CHANGE &&
5264 ct->status != GOT_STATUS_DELETE)
5265 continue;
5266 } else {
5267 if (ct->staged_status != GOT_STATUS_MODIFY &&
5268 ct->staged_status != GOT_STATUS_DELETE)
5269 continue;
5272 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5273 continue;
5275 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5276 if (err)
5277 return err;
5278 if (!path_matches)
5279 continue;
5281 err = got_path_basename(&ct_name, pe->path);
5282 if (err)
5283 return err;
5285 if (strcmp(te->name, ct_name) != 0) {
5286 free(ct_name);
5287 continue;
5289 free(ct_name);
5291 *ctp = ct;
5292 break;
5295 return err;
5298 static const struct got_error *
5299 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5300 const char *child_path, const char *path_base_tree,
5301 struct got_pathlist_head *commitable_paths,
5302 got_worktree_status_cb status_cb, void *status_arg,
5303 struct got_repository *repo)
5305 const struct got_error *err = NULL;
5306 struct got_tree_entry *new_te;
5307 char *subtree_path;
5308 struct got_object_id *id = NULL;
5309 int nentries;
5311 *new_tep = NULL;
5313 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5314 got_path_is_root_dir(path_base_tree) ? "" : "/",
5315 child_path) == -1)
5316 return got_error_from_errno("asprintf");
5318 new_te = calloc(1, sizeof(*new_te));
5319 if (new_te == NULL)
5320 return got_error_from_errno("calloc");
5321 new_te->mode = S_IFDIR;
5323 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5324 sizeof(new_te->name)) {
5325 err = got_error(GOT_ERR_NO_SPACE);
5326 goto done;
5328 err = write_tree(&id, &nentries, NULL, subtree_path,
5329 commitable_paths, status_cb, status_arg, repo);
5330 if (err) {
5331 free(new_te);
5332 goto done;
5334 memcpy(&new_te->id, id, sizeof(new_te->id));
5335 done:
5336 free(id);
5337 free(subtree_path);
5338 if (err == NULL)
5339 *new_tep = new_te;
5340 return err;
5343 static const struct got_error *
5344 write_tree(struct got_object_id **new_tree_id, int *nentries,
5345 struct got_tree_object *base_tree, const char *path_base_tree,
5346 struct got_pathlist_head *commitable_paths,
5347 got_worktree_status_cb status_cb, void *status_arg,
5348 struct got_repository *repo)
5350 const struct got_error *err = NULL;
5351 struct got_pathlist_head paths;
5352 struct got_tree_entry *te, *new_te = NULL;
5353 struct got_pathlist_entry *pe;
5355 TAILQ_INIT(&paths);
5356 *nentries = 0;
5358 /* Insert, and recurse into, newly added entries first. */
5359 TAILQ_FOREACH(pe, commitable_paths, entry) {
5360 struct got_commitable *ct = pe->data;
5361 char *child_path = NULL, *slash;
5363 if ((ct->status != GOT_STATUS_ADD &&
5364 ct->staged_status != GOT_STATUS_ADD) ||
5365 (ct->flags & GOT_COMMITABLE_ADDED))
5366 continue;
5368 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5369 strlen(path_base_tree)))
5370 continue;
5372 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5373 ct->in_repo_path);
5374 if (err)
5375 goto done;
5377 slash = strchr(child_path, '/');
5378 if (slash == NULL) {
5379 err = alloc_added_blob_tree_entry(&new_te, ct);
5380 if (err)
5381 goto done;
5382 err = report_ct_status(ct, status_cb, status_arg);
5383 if (err)
5384 goto done;
5385 ct->flags |= GOT_COMMITABLE_ADDED;
5386 err = insert_tree_entry(new_te, &paths);
5387 if (err)
5388 goto done;
5389 (*nentries)++;
5390 } else {
5391 *slash = '\0'; /* trim trailing path components */
5392 if (base_tree == NULL ||
5393 got_object_tree_find_entry(base_tree, child_path)
5394 == NULL) {
5395 err = make_subtree_for_added_blob(&new_te,
5396 child_path, path_base_tree,
5397 commitable_paths, status_cb, status_arg,
5398 repo);
5399 if (err)
5400 goto done;
5401 err = insert_tree_entry(new_te, &paths);
5402 if (err)
5403 goto done;
5404 (*nentries)++;
5409 if (base_tree) {
5410 int i, nbase_entries;
5411 /* Handle modified and deleted entries. */
5412 nbase_entries = got_object_tree_get_nentries(base_tree);
5413 for (i = 0; i < nbase_entries; i++) {
5414 struct got_commitable *ct = NULL;
5416 te = got_object_tree_get_entry(base_tree, i);
5417 if (got_object_tree_entry_is_submodule(te)) {
5418 /* Entry is a submodule; just copy it. */
5419 err = got_object_tree_entry_dup(&new_te, te);
5420 if (err)
5421 goto done;
5422 err = insert_tree_entry(new_te, &paths);
5423 if (err)
5424 goto done;
5425 (*nentries)++;
5426 continue;
5429 if (S_ISDIR(te->mode)) {
5430 int modified;
5431 err = got_object_tree_entry_dup(&new_te, te);
5432 if (err)
5433 goto done;
5434 err = match_modified_subtree(&modified, te,
5435 path_base_tree, commitable_paths);
5436 if (err)
5437 goto done;
5438 /* Avoid recursion into unmodified subtrees. */
5439 if (modified) {
5440 struct got_object_id *new_id;
5441 int nsubentries;
5442 err = write_subtree(&new_id,
5443 &nsubentries, te,
5444 path_base_tree, commitable_paths,
5445 status_cb, status_arg, repo);
5446 if (err)
5447 goto done;
5448 if (nsubentries == 0) {
5449 /* All entries were deleted. */
5450 free(new_id);
5451 continue;
5453 memcpy(&new_te->id, new_id,
5454 sizeof(new_te->id));
5455 free(new_id);
5457 err = insert_tree_entry(new_te, &paths);
5458 if (err)
5459 goto done;
5460 (*nentries)++;
5461 continue;
5464 err = match_deleted_or_modified_ct(&ct, te,
5465 path_base_tree, commitable_paths);
5466 if (err)
5467 goto done;
5468 if (ct) {
5469 /* NB: Deleted entries get dropped here. */
5470 if (ct->status == GOT_STATUS_MODIFY ||
5471 ct->status == GOT_STATUS_MODE_CHANGE ||
5472 ct->staged_status == GOT_STATUS_MODIFY) {
5473 err = alloc_modified_blob_tree_entry(
5474 &new_te, te, ct);
5475 if (err)
5476 goto done;
5477 err = insert_tree_entry(new_te, &paths);
5478 if (err)
5479 goto done;
5480 (*nentries)++;
5482 err = report_ct_status(ct, status_cb,
5483 status_arg);
5484 if (err)
5485 goto done;
5486 } else {
5487 /* Entry is unchanged; just copy it. */
5488 err = got_object_tree_entry_dup(&new_te, te);
5489 if (err)
5490 goto done;
5491 err = insert_tree_entry(new_te, &paths);
5492 if (err)
5493 goto done;
5494 (*nentries)++;
5499 /* Write new list of entries; deleted entries have been dropped. */
5500 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5501 done:
5502 got_pathlist_free(&paths);
5503 return err;
5506 static const struct got_error *
5507 update_fileindex_after_commit(struct got_worktree *worktree,
5508 struct got_pathlist_head *commitable_paths,
5509 struct got_object_id *new_base_commit_id,
5510 struct got_fileindex *fileindex, int have_staged_files)
5512 const struct got_error *err = NULL;
5513 struct got_pathlist_entry *pe;
5514 char *relpath = NULL;
5516 TAILQ_FOREACH(pe, commitable_paths, entry) {
5517 struct got_fileindex_entry *ie;
5518 struct got_commitable *ct = pe->data;
5520 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5522 err = got_path_skip_common_ancestor(&relpath,
5523 worktree->root_path, ct->ondisk_path);
5524 if (err)
5525 goto done;
5527 if (ie) {
5528 if (ct->status == GOT_STATUS_DELETE ||
5529 ct->staged_status == GOT_STATUS_DELETE) {
5530 got_fileindex_entry_remove(fileindex, ie);
5531 } else if (ct->staged_status == GOT_STATUS_ADD ||
5532 ct->staged_status == GOT_STATUS_MODIFY) {
5533 got_fileindex_entry_stage_set(ie,
5534 GOT_FILEIDX_STAGE_NONE);
5535 got_fileindex_entry_staged_filetype_set(ie, 0);
5537 err = got_fileindex_entry_update(ie,
5538 worktree->root_fd, relpath,
5539 ct->staged_blob_id->sha1,
5540 new_base_commit_id->sha1,
5541 !have_staged_files);
5542 } else
5543 err = got_fileindex_entry_update(ie,
5544 worktree->root_fd, relpath,
5545 ct->blob_id->sha1,
5546 new_base_commit_id->sha1,
5547 !have_staged_files);
5548 } else {
5549 err = got_fileindex_entry_alloc(&ie, pe->path);
5550 if (err)
5551 goto done;
5552 err = got_fileindex_entry_update(ie,
5553 worktree->root_fd, relpath, ct->blob_id->sha1,
5554 new_base_commit_id->sha1, 1);
5555 if (err) {
5556 got_fileindex_entry_free(ie);
5557 goto done;
5559 err = got_fileindex_entry_add(fileindex, ie);
5560 if (err) {
5561 got_fileindex_entry_free(ie);
5562 goto done;
5565 free(relpath);
5566 relpath = NULL;
5568 done:
5569 free(relpath);
5570 return err;
5574 static const struct got_error *
5575 check_out_of_date(const char *in_repo_path, unsigned char status,
5576 unsigned char staged_status, struct got_object_id *base_blob_id,
5577 struct got_object_id *base_commit_id,
5578 struct got_object_id *head_commit_id, struct got_repository *repo,
5579 int ood_errcode)
5581 const struct got_error *err = NULL;
5582 struct got_object_id *id = NULL;
5584 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5585 /* Trivial case: base commit == head commit */
5586 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5587 return NULL;
5589 * Ensure file content which local changes were based
5590 * on matches file content in the branch head.
5592 err = got_object_id_by_path(&id, repo, head_commit_id,
5593 in_repo_path);
5594 if (err) {
5595 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5596 err = got_error(ood_errcode);
5597 goto done;
5598 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5599 err = got_error(ood_errcode);
5600 } else {
5601 /* Require that added files don't exist in the branch head. */
5602 err = got_object_id_by_path(&id, repo, head_commit_id,
5603 in_repo_path);
5604 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5605 goto done;
5606 err = id ? got_error(ood_errcode) : NULL;
5608 done:
5609 free(id);
5610 return err;
5613 const struct got_error *
5614 commit_worktree(struct got_object_id **new_commit_id,
5615 struct got_pathlist_head *commitable_paths,
5616 struct got_object_id *head_commit_id,
5617 struct got_object_id *parent_id2,
5618 struct got_worktree *worktree,
5619 const char *author, const char *committer,
5620 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5621 got_worktree_status_cb status_cb, void *status_arg,
5622 struct got_repository *repo)
5624 const struct got_error *err = NULL, *unlockerr = NULL;
5625 struct got_pathlist_entry *pe;
5626 const char *head_ref_name = NULL;
5627 struct got_commit_object *head_commit = NULL;
5628 struct got_reference *head_ref2 = NULL;
5629 struct got_object_id *head_commit_id2 = NULL;
5630 struct got_tree_object *head_tree = NULL;
5631 struct got_object_id *new_tree_id = NULL;
5632 int nentries, nparents = 0;
5633 struct got_object_id_queue parent_ids;
5634 struct got_object_qid *pid = NULL;
5635 char *logmsg = NULL;
5637 *new_commit_id = NULL;
5639 STAILQ_INIT(&parent_ids);
5641 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5642 if (err)
5643 goto done;
5645 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5646 if (err)
5647 goto done;
5649 if (commit_msg_cb != NULL) {
5650 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5651 if (err)
5652 goto done;
5655 if (logmsg == NULL || strlen(logmsg) == 0) {
5656 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5657 goto done;
5660 /* Create blobs from added and modified files and record their IDs. */
5661 TAILQ_FOREACH(pe, commitable_paths, entry) {
5662 struct got_commitable *ct = pe->data;
5663 char *ondisk_path;
5665 /* Blobs for staged files already exist. */
5666 if (ct->staged_status == GOT_STATUS_ADD ||
5667 ct->staged_status == GOT_STATUS_MODIFY)
5668 continue;
5670 if (ct->status != GOT_STATUS_ADD &&
5671 ct->status != GOT_STATUS_MODIFY &&
5672 ct->status != GOT_STATUS_MODE_CHANGE)
5673 continue;
5675 if (asprintf(&ondisk_path, "%s/%s",
5676 worktree->root_path, pe->path) == -1) {
5677 err = got_error_from_errno("asprintf");
5678 goto done;
5680 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5681 free(ondisk_path);
5682 if (err)
5683 goto done;
5686 /* Recursively write new tree objects. */
5687 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5688 commitable_paths, status_cb, status_arg, repo);
5689 if (err)
5690 goto done;
5692 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5693 if (err)
5694 goto done;
5695 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5696 nparents++;
5697 if (parent_id2) {
5698 err = got_object_qid_alloc(&pid, parent_id2);
5699 if (err)
5700 goto done;
5701 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5702 nparents++;
5704 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5705 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5706 if (logmsg != NULL)
5707 free(logmsg);
5708 if (err)
5709 goto done;
5711 /* Check if a concurrent commit to our branch has occurred. */
5712 head_ref_name = got_worktree_get_head_ref_name(worktree);
5713 if (head_ref_name == NULL) {
5714 err = got_error_from_errno("got_worktree_get_head_ref_name");
5715 goto done;
5717 /* Lock the reference here to prevent concurrent modification. */
5718 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5719 if (err)
5720 goto done;
5721 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5722 if (err)
5723 goto done;
5724 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5725 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5726 goto done;
5728 /* Update branch head in repository. */
5729 err = got_ref_change_ref(head_ref2, *new_commit_id);
5730 if (err)
5731 goto done;
5732 err = got_ref_write(head_ref2, repo);
5733 if (err)
5734 goto done;
5736 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5737 if (err)
5738 goto done;
5740 err = ref_base_commit(worktree, repo);
5741 if (err)
5742 goto done;
5743 done:
5744 got_object_id_queue_free(&parent_ids);
5745 if (head_tree)
5746 got_object_tree_close(head_tree);
5747 if (head_commit)
5748 got_object_commit_close(head_commit);
5749 free(head_commit_id2);
5750 if (head_ref2) {
5751 unlockerr = got_ref_unlock(head_ref2);
5752 if (unlockerr && err == NULL)
5753 err = unlockerr;
5754 got_ref_close(head_ref2);
5756 return err;
5759 static const struct got_error *
5760 check_path_is_commitable(const char *path,
5761 struct got_pathlist_head *commitable_paths)
5763 struct got_pathlist_entry *cpe = NULL;
5764 size_t path_len = strlen(path);
5766 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5767 struct got_commitable *ct = cpe->data;
5768 const char *ct_path = ct->path;
5770 while (ct_path[0] == '/')
5771 ct_path++;
5773 if (strcmp(path, ct_path) == 0 ||
5774 got_path_is_child(ct_path, path, path_len))
5775 break;
5778 if (cpe == NULL)
5779 return got_error_path(path, GOT_ERR_BAD_PATH);
5781 return NULL;
5784 static const struct got_error *
5785 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5787 int *have_staged_files = arg;
5789 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5790 *have_staged_files = 1;
5791 return got_error(GOT_ERR_CANCELLED);
5794 return NULL;
5797 static const struct got_error *
5798 check_non_staged_files(struct got_fileindex *fileindex,
5799 struct got_pathlist_head *paths)
5801 struct got_pathlist_entry *pe;
5802 struct got_fileindex_entry *ie;
5804 TAILQ_FOREACH(pe, paths, entry) {
5805 if (pe->path[0] == '\0')
5806 continue;
5807 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5808 if (ie == NULL)
5809 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5810 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5811 return got_error_path(pe->path,
5812 GOT_ERR_FILE_NOT_STAGED);
5815 return NULL;
5818 const struct got_error *
5819 got_worktree_commit(struct got_object_id **new_commit_id,
5820 struct got_worktree *worktree, struct got_pathlist_head *paths,
5821 const char *author, const char *committer, int allow_bad_symlinks,
5822 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5823 got_worktree_status_cb status_cb, void *status_arg,
5824 struct got_repository *repo)
5826 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5827 struct got_fileindex *fileindex = NULL;
5828 char *fileindex_path = NULL;
5829 struct got_pathlist_head commitable_paths;
5830 struct collect_commitables_arg cc_arg;
5831 struct got_pathlist_entry *pe;
5832 struct got_reference *head_ref = NULL;
5833 struct got_object_id *head_commit_id = NULL;
5834 int have_staged_files = 0;
5836 *new_commit_id = NULL;
5838 TAILQ_INIT(&commitable_paths);
5840 err = lock_worktree(worktree, LOCK_EX);
5841 if (err)
5842 goto done;
5844 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5845 if (err)
5846 goto done;
5848 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5849 if (err)
5850 goto done;
5852 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5853 if (err)
5854 goto done;
5856 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5857 &have_staged_files);
5858 if (err && err->code != GOT_ERR_CANCELLED)
5859 goto done;
5860 if (have_staged_files) {
5861 err = check_non_staged_files(fileindex, paths);
5862 if (err)
5863 goto done;
5866 cc_arg.commitable_paths = &commitable_paths;
5867 cc_arg.worktree = worktree;
5868 cc_arg.fileindex = fileindex;
5869 cc_arg.repo = repo;
5870 cc_arg.have_staged_files = have_staged_files;
5871 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5872 TAILQ_FOREACH(pe, paths, entry) {
5873 err = worktree_status(worktree, pe->path, fileindex, repo,
5874 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5875 if (err)
5876 goto done;
5879 if (TAILQ_EMPTY(&commitable_paths)) {
5880 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5881 goto done;
5884 TAILQ_FOREACH(pe, paths, entry) {
5885 err = check_path_is_commitable(pe->path, &commitable_paths);
5886 if (err)
5887 goto done;
5890 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5891 struct got_commitable *ct = pe->data;
5892 const char *ct_path = ct->in_repo_path;
5894 while (ct_path[0] == '/')
5895 ct_path++;
5896 err = check_out_of_date(ct_path, ct->status,
5897 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5898 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5899 if (err)
5900 goto done;
5904 err = commit_worktree(new_commit_id, &commitable_paths,
5905 head_commit_id, NULL, worktree, author, committer,
5906 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5907 if (err)
5908 goto done;
5910 err = update_fileindex_after_commit(worktree, &commitable_paths,
5911 *new_commit_id, fileindex, have_staged_files);
5912 sync_err = sync_fileindex(fileindex, fileindex_path);
5913 if (sync_err && err == NULL)
5914 err = sync_err;
5915 done:
5916 if (fileindex)
5917 got_fileindex_free(fileindex);
5918 free(fileindex_path);
5919 unlockerr = lock_worktree(worktree, LOCK_SH);
5920 if (unlockerr && err == NULL)
5921 err = unlockerr;
5922 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5923 struct got_commitable *ct = pe->data;
5924 free_commitable(ct);
5926 got_pathlist_free(&commitable_paths);
5927 return err;
5930 const char *
5931 got_commitable_get_path(struct got_commitable *ct)
5933 return ct->path;
5936 unsigned int
5937 got_commitable_get_status(struct got_commitable *ct)
5939 return ct->status;
5942 struct check_rebase_ok_arg {
5943 struct got_worktree *worktree;
5944 struct got_repository *repo;
5947 static const struct got_error *
5948 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5950 const struct got_error *err = NULL;
5951 struct check_rebase_ok_arg *a = arg;
5952 unsigned char status;
5953 struct stat sb;
5954 char *ondisk_path;
5956 /* Reject rebase of a work tree with mixed base commits. */
5957 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5958 SHA1_DIGEST_LENGTH))
5959 return got_error(GOT_ERR_MIXED_COMMITS);
5961 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5962 == -1)
5963 return got_error_from_errno("asprintf");
5965 /* Reject rebase of a work tree with modified or staged files. */
5966 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5967 free(ondisk_path);
5968 if (err)
5969 return err;
5971 if (status != GOT_STATUS_NO_CHANGE)
5972 return got_error(GOT_ERR_MODIFIED);
5973 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5974 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5976 return NULL;
5979 const struct got_error *
5980 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5981 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5982 struct got_worktree *worktree, struct got_reference *branch,
5983 struct got_repository *repo)
5985 const struct got_error *err = NULL;
5986 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5987 char *branch_ref_name = NULL;
5988 char *fileindex_path = NULL;
5989 struct check_rebase_ok_arg ok_arg;
5990 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5991 struct got_object_id *wt_branch_tip = NULL;
5993 *new_base_branch_ref = NULL;
5994 *tmp_branch = NULL;
5995 *fileindex = NULL;
5997 err = lock_worktree(worktree, LOCK_EX);
5998 if (err)
5999 return err;
6001 err = open_fileindex(fileindex, &fileindex_path, worktree);
6002 if (err)
6003 goto done;
6005 ok_arg.worktree = worktree;
6006 ok_arg.repo = repo;
6007 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6008 &ok_arg);
6009 if (err)
6010 goto done;
6012 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6013 if (err)
6014 goto done;
6016 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6017 if (err)
6018 goto done;
6020 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6021 if (err)
6022 goto done;
6024 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6025 0);
6026 if (err)
6027 goto done;
6029 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6030 if (err)
6031 goto done;
6032 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6033 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6034 goto done;
6037 err = got_ref_alloc_symref(new_base_branch_ref,
6038 new_base_branch_ref_name, wt_branch);
6039 if (err)
6040 goto done;
6041 err = got_ref_write(*new_base_branch_ref, repo);
6042 if (err)
6043 goto done;
6045 /* TODO Lock original branch's ref while rebasing? */
6047 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6048 if (err)
6049 goto done;
6051 err = got_ref_write(branch_ref, repo);
6052 if (err)
6053 goto done;
6055 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6056 worktree->base_commit_id);
6057 if (err)
6058 goto done;
6059 err = got_ref_write(*tmp_branch, repo);
6060 if (err)
6061 goto done;
6063 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6064 if (err)
6065 goto done;
6066 done:
6067 free(fileindex_path);
6068 free(tmp_branch_name);
6069 free(new_base_branch_ref_name);
6070 free(branch_ref_name);
6071 if (branch_ref)
6072 got_ref_close(branch_ref);
6073 if (wt_branch)
6074 got_ref_close(wt_branch);
6075 free(wt_branch_tip);
6076 if (err) {
6077 if (*new_base_branch_ref) {
6078 got_ref_close(*new_base_branch_ref);
6079 *new_base_branch_ref = NULL;
6081 if (*tmp_branch) {
6082 got_ref_close(*tmp_branch);
6083 *tmp_branch = NULL;
6085 if (*fileindex) {
6086 got_fileindex_free(*fileindex);
6087 *fileindex = NULL;
6089 lock_worktree(worktree, LOCK_SH);
6091 return err;
6094 const struct got_error *
6095 got_worktree_rebase_continue(struct got_object_id **commit_id,
6096 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6097 struct got_reference **branch, struct got_fileindex **fileindex,
6098 struct got_worktree *worktree, struct got_repository *repo)
6100 const struct got_error *err;
6101 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6102 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6103 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6104 char *fileindex_path = NULL;
6105 int have_staged_files = 0;
6107 *commit_id = NULL;
6108 *new_base_branch = NULL;
6109 *tmp_branch = NULL;
6110 *branch = NULL;
6111 *fileindex = NULL;
6113 err = lock_worktree(worktree, LOCK_EX);
6114 if (err)
6115 return err;
6117 err = open_fileindex(fileindex, &fileindex_path, worktree);
6118 if (err)
6119 goto done;
6121 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6122 &have_staged_files);
6123 if (err && err->code != GOT_ERR_CANCELLED)
6124 goto done;
6125 if (have_staged_files) {
6126 err = got_error(GOT_ERR_STAGED_PATHS);
6127 goto done;
6130 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6131 if (err)
6132 goto done;
6134 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6135 if (err)
6136 goto done;
6138 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6139 if (err)
6140 goto done;
6142 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6143 if (err)
6144 goto done;
6146 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6147 if (err)
6148 goto done;
6150 err = got_ref_open(branch, repo,
6151 got_ref_get_symref_target(branch_ref), 0);
6152 if (err)
6153 goto done;
6155 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6156 if (err)
6157 goto done;
6159 err = got_ref_resolve(commit_id, repo, commit_ref);
6160 if (err)
6161 goto done;
6163 err = got_ref_open(new_base_branch, repo,
6164 new_base_branch_ref_name, 0);
6165 if (err)
6166 goto done;
6168 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6169 if (err)
6170 goto done;
6171 done:
6172 free(commit_ref_name);
6173 free(branch_ref_name);
6174 free(fileindex_path);
6175 if (commit_ref)
6176 got_ref_close(commit_ref);
6177 if (branch_ref)
6178 got_ref_close(branch_ref);
6179 if (err) {
6180 free(*commit_id);
6181 *commit_id = NULL;
6182 if (*tmp_branch) {
6183 got_ref_close(*tmp_branch);
6184 *tmp_branch = NULL;
6186 if (*new_base_branch) {
6187 got_ref_close(*new_base_branch);
6188 *new_base_branch = NULL;
6190 if (*branch) {
6191 got_ref_close(*branch);
6192 *branch = NULL;
6194 if (*fileindex) {
6195 got_fileindex_free(*fileindex);
6196 *fileindex = NULL;
6198 lock_worktree(worktree, LOCK_SH);
6200 return err;
6203 const struct got_error *
6204 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6206 const struct got_error *err;
6207 char *tmp_branch_name = NULL;
6209 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6210 if (err)
6211 return err;
6213 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6214 free(tmp_branch_name);
6215 return NULL;
6218 static const struct got_error *
6219 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6220 char **logmsg, void *arg)
6222 *logmsg = arg;
6223 return NULL;
6226 static const struct got_error *
6227 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6228 const char *path, struct got_object_id *blob_id,
6229 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6230 int dirfd, const char *de_name)
6232 return NULL;
6235 struct collect_merged_paths_arg {
6236 got_worktree_checkout_cb progress_cb;
6237 void *progress_arg;
6238 struct got_pathlist_head *merged_paths;
6241 static const struct got_error *
6242 collect_merged_paths(void *arg, unsigned char status, const char *path)
6244 const struct got_error *err;
6245 struct collect_merged_paths_arg *a = arg;
6246 char *p;
6247 struct got_pathlist_entry *new;
6249 err = (*a->progress_cb)(a->progress_arg, status, path);
6250 if (err)
6251 return err;
6253 if (status != GOT_STATUS_MERGE &&
6254 status != GOT_STATUS_ADD &&
6255 status != GOT_STATUS_DELETE &&
6256 status != GOT_STATUS_CONFLICT)
6257 return NULL;
6259 p = strdup(path);
6260 if (p == NULL)
6261 return got_error_from_errno("strdup");
6263 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6264 if (err || new == NULL)
6265 free(p);
6266 return err;
6269 void
6270 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6272 struct got_pathlist_entry *pe;
6274 TAILQ_FOREACH(pe, merged_paths, entry)
6275 free((char *)pe->path);
6277 got_pathlist_free(merged_paths);
6280 static const struct got_error *
6281 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6282 int is_rebase, struct got_repository *repo)
6284 const struct got_error *err;
6285 struct got_reference *commit_ref = NULL;
6287 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6288 if (err) {
6289 if (err->code != GOT_ERR_NOT_REF)
6290 goto done;
6291 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6292 if (err)
6293 goto done;
6294 err = got_ref_write(commit_ref, repo);
6295 if (err)
6296 goto done;
6297 } else if (is_rebase) {
6298 struct got_object_id *stored_id;
6299 int cmp;
6301 err = got_ref_resolve(&stored_id, repo, commit_ref);
6302 if (err)
6303 goto done;
6304 cmp = got_object_id_cmp(commit_id, stored_id);
6305 free(stored_id);
6306 if (cmp != 0) {
6307 err = got_error(GOT_ERR_REBASE_COMMITID);
6308 goto done;
6311 done:
6312 if (commit_ref)
6313 got_ref_close(commit_ref);
6314 return err;
6317 static const struct got_error *
6318 rebase_merge_files(struct got_pathlist_head *merged_paths,
6319 const char *commit_ref_name, struct got_worktree *worktree,
6320 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6321 struct got_object_id *commit_id, struct got_repository *repo,
6322 got_worktree_checkout_cb progress_cb, void *progress_arg,
6323 got_cancel_cb cancel_cb, void *cancel_arg)
6325 const struct got_error *err;
6326 struct got_reference *commit_ref = NULL;
6327 struct collect_merged_paths_arg cmp_arg;
6328 char *fileindex_path;
6330 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6332 err = get_fileindex_path(&fileindex_path, worktree);
6333 if (err)
6334 return err;
6336 cmp_arg.progress_cb = progress_cb;
6337 cmp_arg.progress_arg = progress_arg;
6338 cmp_arg.merged_paths = merged_paths;
6339 err = merge_files(worktree, fileindex, fileindex_path,
6340 parent_commit_id, commit_id, repo, collect_merged_paths,
6341 &cmp_arg, cancel_cb, cancel_arg);
6342 if (commit_ref)
6343 got_ref_close(commit_ref);
6344 return err;
6347 const struct got_error *
6348 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6349 struct got_worktree *worktree, struct got_fileindex *fileindex,
6350 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6351 struct got_repository *repo,
6352 got_worktree_checkout_cb progress_cb, void *progress_arg,
6353 got_cancel_cb cancel_cb, void *cancel_arg)
6355 const struct got_error *err;
6356 char *commit_ref_name;
6358 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6359 if (err)
6360 return err;
6362 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6363 if (err)
6364 goto done;
6366 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6367 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6368 progress_arg, cancel_cb, cancel_arg);
6369 done:
6370 free(commit_ref_name);
6371 return err;
6374 const struct got_error *
6375 got_worktree_histedit_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_histedit_commit_ref_name(&commit_ref_name, worktree);
6386 if (err)
6387 return err;
6389 err = store_commit_id(commit_ref_name, commit_id, 0, 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 static const struct got_error *
6402 rebase_commit(struct got_object_id **new_commit_id,
6403 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6404 struct got_worktree *worktree, struct got_fileindex *fileindex,
6405 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6406 const char *new_logmsg, struct got_repository *repo)
6408 const struct got_error *err, *sync_err;
6409 struct got_pathlist_head commitable_paths;
6410 struct collect_commitables_arg cc_arg;
6411 char *fileindex_path = NULL;
6412 struct got_reference *head_ref = NULL;
6413 struct got_object_id *head_commit_id = NULL;
6414 char *logmsg = NULL;
6416 TAILQ_INIT(&commitable_paths);
6417 *new_commit_id = NULL;
6419 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6421 err = get_fileindex_path(&fileindex_path, worktree);
6422 if (err)
6423 return err;
6425 cc_arg.commitable_paths = &commitable_paths;
6426 cc_arg.worktree = worktree;
6427 cc_arg.repo = repo;
6428 cc_arg.have_staged_files = 0;
6430 * If possible get the status of individual files directly to
6431 * avoid crawling the entire work tree once per rebased commit.
6433 * Ideally, merged_paths would contain a list of commitables
6434 * we could use so we could skip worktree_status() entirely.
6435 * However, we would then need carefully keep track of cumulative
6436 * effects of operations such as file additions and deletions
6437 * in 'got histedit -f' (folding multiple commits into one),
6438 * and this extra complexity is not really worth it.
6440 if (merged_paths) {
6441 struct got_pathlist_entry *pe;
6442 TAILQ_FOREACH(pe, merged_paths, entry) {
6443 err = worktree_status(worktree, pe->path, fileindex,
6444 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6445 0);
6446 if (err)
6447 goto done;
6449 } else {
6450 err = worktree_status(worktree, "", fileindex, repo,
6451 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6452 if (err)
6453 goto done;
6456 if (TAILQ_EMPTY(&commitable_paths)) {
6457 /* No-op change; commit will be elided. */
6458 err = got_ref_delete(commit_ref, repo);
6459 if (err)
6460 goto done;
6461 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6462 goto done;
6465 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6466 if (err)
6467 goto done;
6469 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6470 if (err)
6471 goto done;
6473 if (new_logmsg) {
6474 logmsg = strdup(new_logmsg);
6475 if (logmsg == NULL) {
6476 err = got_error_from_errno("strdup");
6477 goto done;
6479 } else {
6480 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6481 if (err)
6482 goto done;
6485 /* NB: commit_worktree will call free(logmsg) */
6486 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6487 NULL, worktree, got_object_commit_get_author(orig_commit),
6488 got_object_commit_get_committer(orig_commit),
6489 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6490 if (err)
6491 goto done;
6493 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6494 if (err)
6495 goto done;
6497 err = got_ref_delete(commit_ref, repo);
6498 if (err)
6499 goto done;
6501 err = update_fileindex_after_commit(worktree, &commitable_paths,
6502 *new_commit_id, fileindex, 0);
6503 sync_err = sync_fileindex(fileindex, fileindex_path);
6504 if (sync_err && err == NULL)
6505 err = sync_err;
6506 done:
6507 free(fileindex_path);
6508 free(head_commit_id);
6509 if (head_ref)
6510 got_ref_close(head_ref);
6511 if (err) {
6512 free(*new_commit_id);
6513 *new_commit_id = NULL;
6515 return err;
6518 const struct got_error *
6519 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6520 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6521 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6522 struct got_commit_object *orig_commit,
6523 struct got_object_id *orig_commit_id, struct got_repository *repo)
6525 const struct got_error *err;
6526 char *commit_ref_name;
6527 struct got_reference *commit_ref = NULL;
6528 struct got_object_id *commit_id = NULL;
6530 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6531 if (err)
6532 return err;
6534 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6535 if (err)
6536 goto done;
6537 err = got_ref_resolve(&commit_id, repo, commit_ref);
6538 if (err)
6539 goto done;
6540 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6541 err = got_error(GOT_ERR_REBASE_COMMITID);
6542 goto done;
6545 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6546 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6547 done:
6548 if (commit_ref)
6549 got_ref_close(commit_ref);
6550 free(commit_ref_name);
6551 free(commit_id);
6552 return err;
6555 const struct got_error *
6556 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6557 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6558 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6559 struct got_commit_object *orig_commit,
6560 struct got_object_id *orig_commit_id, const char *new_logmsg,
6561 struct got_repository *repo)
6563 const struct got_error *err;
6564 char *commit_ref_name;
6565 struct got_reference *commit_ref = NULL;
6567 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6568 if (err)
6569 return err;
6571 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6572 if (err)
6573 goto done;
6575 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6576 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6577 done:
6578 if (commit_ref)
6579 got_ref_close(commit_ref);
6580 free(commit_ref_name);
6581 return err;
6584 const struct got_error *
6585 got_worktree_rebase_postpone(struct got_worktree *worktree,
6586 struct got_fileindex *fileindex)
6588 if (fileindex)
6589 got_fileindex_free(fileindex);
6590 return lock_worktree(worktree, LOCK_SH);
6593 static const struct got_error *
6594 delete_ref(const char *name, struct got_repository *repo)
6596 const struct got_error *err;
6597 struct got_reference *ref;
6599 err = got_ref_open(&ref, repo, name, 0);
6600 if (err) {
6601 if (err->code == GOT_ERR_NOT_REF)
6602 return NULL;
6603 return err;
6606 err = got_ref_delete(ref, repo);
6607 got_ref_close(ref);
6608 return err;
6611 static const struct got_error *
6612 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6614 const struct got_error *err;
6615 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6616 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6618 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6619 if (err)
6620 goto done;
6621 err = delete_ref(tmp_branch_name, repo);
6622 if (err)
6623 goto done;
6625 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6626 if (err)
6627 goto done;
6628 err = delete_ref(new_base_branch_ref_name, repo);
6629 if (err)
6630 goto done;
6632 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6633 if (err)
6634 goto done;
6635 err = delete_ref(branch_ref_name, repo);
6636 if (err)
6637 goto done;
6639 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6640 if (err)
6641 goto done;
6642 err = delete_ref(commit_ref_name, repo);
6643 if (err)
6644 goto done;
6646 done:
6647 free(tmp_branch_name);
6648 free(new_base_branch_ref_name);
6649 free(branch_ref_name);
6650 free(commit_ref_name);
6651 return err;
6654 const struct got_error *
6655 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6656 struct got_object_id *new_commit_id, struct got_repository *repo)
6658 const struct got_error *err;
6659 struct got_reference *ref = NULL;
6660 struct got_object_id *old_commit_id = NULL;
6661 const char *branch_name = NULL;
6662 char *new_id_str = NULL;
6663 char *refname = NULL;
6665 branch_name = got_ref_get_name(branch);
6666 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6667 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6668 branch_name += 11;
6670 err = got_object_id_str(&new_id_str, new_commit_id);
6671 if (err)
6672 return err;
6674 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6675 new_id_str) == -1) {
6676 err = got_error_from_errno("asprintf");
6677 goto done;
6680 err = got_ref_resolve(&old_commit_id, repo, branch);
6681 if (err)
6682 goto done;
6684 err = got_ref_alloc(&ref, refname, old_commit_id);
6685 if (err)
6686 goto done;
6688 err = got_ref_write(ref, repo);
6689 done:
6690 free(new_id_str);
6691 free(refname);
6692 free(old_commit_id);
6693 if (ref)
6694 got_ref_close(ref);
6695 return err;
6698 const struct got_error *
6699 got_worktree_rebase_complete(struct got_worktree *worktree,
6700 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6701 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6702 struct got_repository *repo, int create_backup)
6704 const struct got_error *err, *unlockerr, *sync_err;
6705 struct got_object_id *new_head_commit_id = NULL;
6706 char *fileindex_path = NULL;
6708 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6709 if (err)
6710 return err;
6712 if (create_backup) {
6713 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6714 rebased_branch, new_head_commit_id, repo);
6715 if (err)
6716 goto done;
6719 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6720 if (err)
6721 goto done;
6723 err = got_ref_write(rebased_branch, repo);
6724 if (err)
6725 goto done;
6727 err = got_worktree_set_head_ref(worktree, rebased_branch);
6728 if (err)
6729 goto done;
6731 err = delete_rebase_refs(worktree, repo);
6732 if (err)
6733 goto done;
6735 err = get_fileindex_path(&fileindex_path, worktree);
6736 if (err)
6737 goto done;
6738 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6739 sync_err = sync_fileindex(fileindex, fileindex_path);
6740 if (sync_err && err == NULL)
6741 err = sync_err;
6742 done:
6743 got_fileindex_free(fileindex);
6744 free(fileindex_path);
6745 free(new_head_commit_id);
6746 unlockerr = lock_worktree(worktree, LOCK_SH);
6747 if (unlockerr && err == NULL)
6748 err = unlockerr;
6749 return err;
6752 const struct got_error *
6753 got_worktree_rebase_abort(struct got_worktree *worktree,
6754 struct got_fileindex *fileindex, struct got_repository *repo,
6755 struct got_reference *new_base_branch,
6756 got_worktree_checkout_cb progress_cb, void *progress_arg)
6758 const struct got_error *err, *unlockerr, *sync_err;
6759 struct got_reference *resolved = NULL;
6760 struct got_object_id *commit_id = NULL;
6761 char *fileindex_path = NULL;
6762 struct revert_file_args rfa;
6763 struct got_object_id *tree_id = NULL;
6765 err = lock_worktree(worktree, LOCK_EX);
6766 if (err)
6767 return err;
6769 err = got_ref_open(&resolved, repo,
6770 got_ref_get_symref_target(new_base_branch), 0);
6771 if (err)
6772 goto done;
6774 err = got_worktree_set_head_ref(worktree, resolved);
6775 if (err)
6776 goto done;
6779 * XXX commits to the base branch could have happened while
6780 * we were busy rebasing; should we store the original commit ID
6781 * when rebase begins and read it back here?
6783 err = got_ref_resolve(&commit_id, repo, resolved);
6784 if (err)
6785 goto done;
6787 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6788 if (err)
6789 goto done;
6791 err = got_object_id_by_path(&tree_id, repo,
6792 worktree->base_commit_id, worktree->path_prefix);
6793 if (err)
6794 goto done;
6796 err = delete_rebase_refs(worktree, repo);
6797 if (err)
6798 goto done;
6800 err = get_fileindex_path(&fileindex_path, worktree);
6801 if (err)
6802 goto done;
6804 rfa.worktree = worktree;
6805 rfa.fileindex = fileindex;
6806 rfa.progress_cb = progress_cb;
6807 rfa.progress_arg = progress_arg;
6808 rfa.patch_cb = NULL;
6809 rfa.patch_arg = NULL;
6810 rfa.repo = repo;
6811 rfa.unlink_added_files = 0;
6812 err = worktree_status(worktree, "", fileindex, repo,
6813 revert_file, &rfa, NULL, NULL, 0, 0);
6814 if (err)
6815 goto sync;
6817 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6818 repo, progress_cb, progress_arg, NULL, NULL);
6819 sync:
6820 sync_err = sync_fileindex(fileindex, fileindex_path);
6821 if (sync_err && err == NULL)
6822 err = sync_err;
6823 done:
6824 got_ref_close(resolved);
6825 free(tree_id);
6826 free(commit_id);
6827 if (fileindex)
6828 got_fileindex_free(fileindex);
6829 free(fileindex_path);
6831 unlockerr = lock_worktree(worktree, LOCK_SH);
6832 if (unlockerr && err == NULL)
6833 err = unlockerr;
6834 return err;
6837 const struct got_error *
6838 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6839 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6840 struct got_fileindex **fileindex, struct got_worktree *worktree,
6841 struct got_repository *repo)
6843 const struct got_error *err = NULL;
6844 char *tmp_branch_name = NULL;
6845 char *branch_ref_name = NULL;
6846 char *base_commit_ref_name = NULL;
6847 char *fileindex_path = NULL;
6848 struct check_rebase_ok_arg ok_arg;
6849 struct got_reference *wt_branch = NULL;
6850 struct got_reference *base_commit_ref = NULL;
6852 *tmp_branch = NULL;
6853 *branch_ref = NULL;
6854 *base_commit_id = NULL;
6855 *fileindex = NULL;
6857 err = lock_worktree(worktree, LOCK_EX);
6858 if (err)
6859 return err;
6861 err = open_fileindex(fileindex, &fileindex_path, worktree);
6862 if (err)
6863 goto done;
6865 ok_arg.worktree = worktree;
6866 ok_arg.repo = repo;
6867 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6868 &ok_arg);
6869 if (err)
6870 goto done;
6872 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6873 if (err)
6874 goto done;
6876 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6877 if (err)
6878 goto done;
6880 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6881 worktree);
6882 if (err)
6883 goto done;
6885 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6886 0);
6887 if (err)
6888 goto done;
6890 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6891 if (err)
6892 goto done;
6894 err = got_ref_write(*branch_ref, repo);
6895 if (err)
6896 goto done;
6898 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6899 worktree->base_commit_id);
6900 if (err)
6901 goto done;
6902 err = got_ref_write(base_commit_ref, repo);
6903 if (err)
6904 goto done;
6905 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6906 if (*base_commit_id == NULL) {
6907 err = got_error_from_errno("got_object_id_dup");
6908 goto done;
6911 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6912 worktree->base_commit_id);
6913 if (err)
6914 goto done;
6915 err = got_ref_write(*tmp_branch, repo);
6916 if (err)
6917 goto done;
6919 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6920 if (err)
6921 goto done;
6922 done:
6923 free(fileindex_path);
6924 free(tmp_branch_name);
6925 free(branch_ref_name);
6926 free(base_commit_ref_name);
6927 if (wt_branch)
6928 got_ref_close(wt_branch);
6929 if (err) {
6930 if (*branch_ref) {
6931 got_ref_close(*branch_ref);
6932 *branch_ref = NULL;
6934 if (*tmp_branch) {
6935 got_ref_close(*tmp_branch);
6936 *tmp_branch = NULL;
6938 free(*base_commit_id);
6939 if (*fileindex) {
6940 got_fileindex_free(*fileindex);
6941 *fileindex = NULL;
6943 lock_worktree(worktree, LOCK_SH);
6945 return err;
6948 const struct got_error *
6949 got_worktree_histedit_postpone(struct got_worktree *worktree,
6950 struct got_fileindex *fileindex)
6952 if (fileindex)
6953 got_fileindex_free(fileindex);
6954 return lock_worktree(worktree, LOCK_SH);
6957 const struct got_error *
6958 got_worktree_histedit_in_progress(int *in_progress,
6959 struct got_worktree *worktree)
6961 const struct got_error *err;
6962 char *tmp_branch_name = NULL;
6964 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6965 if (err)
6966 return err;
6968 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6969 free(tmp_branch_name);
6970 return NULL;
6973 const struct got_error *
6974 got_worktree_histedit_continue(struct got_object_id **commit_id,
6975 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6976 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6977 struct got_worktree *worktree, struct got_repository *repo)
6979 const struct got_error *err;
6980 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6981 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6982 struct got_reference *commit_ref = NULL;
6983 struct got_reference *base_commit_ref = NULL;
6984 char *fileindex_path = NULL;
6985 int have_staged_files = 0;
6987 *commit_id = NULL;
6988 *tmp_branch = NULL;
6989 *base_commit_id = NULL;
6990 *fileindex = NULL;
6992 err = lock_worktree(worktree, LOCK_EX);
6993 if (err)
6994 return err;
6996 err = open_fileindex(fileindex, &fileindex_path, worktree);
6997 if (err)
6998 goto done;
7000 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7001 &have_staged_files);
7002 if (err && err->code != GOT_ERR_CANCELLED)
7003 goto done;
7004 if (have_staged_files) {
7005 err = got_error(GOT_ERR_STAGED_PATHS);
7006 goto done;
7009 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7010 if (err)
7011 goto done;
7013 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7014 if (err)
7015 goto done;
7017 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7018 if (err)
7019 goto done;
7021 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7022 worktree);
7023 if (err)
7024 goto done;
7026 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7027 if (err)
7028 goto done;
7030 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7031 if (err)
7032 goto done;
7033 err = got_ref_resolve(commit_id, repo, commit_ref);
7034 if (err)
7035 goto done;
7037 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7038 if (err)
7039 goto done;
7040 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7041 if (err)
7042 goto done;
7044 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7045 if (err)
7046 goto done;
7047 done:
7048 free(commit_ref_name);
7049 free(branch_ref_name);
7050 free(fileindex_path);
7051 if (commit_ref)
7052 got_ref_close(commit_ref);
7053 if (base_commit_ref)
7054 got_ref_close(base_commit_ref);
7055 if (err) {
7056 free(*commit_id);
7057 *commit_id = NULL;
7058 free(*base_commit_id);
7059 *base_commit_id = NULL;
7060 if (*tmp_branch) {
7061 got_ref_close(*tmp_branch);
7062 *tmp_branch = NULL;
7064 if (*fileindex) {
7065 got_fileindex_free(*fileindex);
7066 *fileindex = NULL;
7068 lock_worktree(worktree, LOCK_EX);
7070 return err;
7073 static const struct got_error *
7074 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7076 const struct got_error *err;
7077 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7078 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7080 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7081 if (err)
7082 goto done;
7083 err = delete_ref(tmp_branch_name, repo);
7084 if (err)
7085 goto done;
7087 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7088 worktree);
7089 if (err)
7090 goto done;
7091 err = delete_ref(base_commit_ref_name, repo);
7092 if (err)
7093 goto done;
7095 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7096 if (err)
7097 goto done;
7098 err = delete_ref(branch_ref_name, repo);
7099 if (err)
7100 goto done;
7102 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7103 if (err)
7104 goto done;
7105 err = delete_ref(commit_ref_name, repo);
7106 if (err)
7107 goto done;
7108 done:
7109 free(tmp_branch_name);
7110 free(base_commit_ref_name);
7111 free(branch_ref_name);
7112 free(commit_ref_name);
7113 return err;
7116 const struct got_error *
7117 got_worktree_histedit_abort(struct got_worktree *worktree,
7118 struct got_fileindex *fileindex, struct got_repository *repo,
7119 struct got_reference *branch, struct got_object_id *base_commit_id,
7120 got_worktree_checkout_cb progress_cb, void *progress_arg)
7122 const struct got_error *err, *unlockerr, *sync_err;
7123 struct got_reference *resolved = NULL;
7124 char *fileindex_path = NULL;
7125 struct got_object_id *tree_id = NULL;
7126 struct revert_file_args rfa;
7128 err = lock_worktree(worktree, LOCK_EX);
7129 if (err)
7130 return err;
7132 err = got_ref_open(&resolved, repo,
7133 got_ref_get_symref_target(branch), 0);
7134 if (err)
7135 goto done;
7137 err = got_worktree_set_head_ref(worktree, resolved);
7138 if (err)
7139 goto done;
7141 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7142 if (err)
7143 goto done;
7145 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7146 worktree->path_prefix);
7147 if (err)
7148 goto done;
7150 err = delete_histedit_refs(worktree, repo);
7151 if (err)
7152 goto done;
7154 err = get_fileindex_path(&fileindex_path, worktree);
7155 if (err)
7156 goto done;
7158 rfa.worktree = worktree;
7159 rfa.fileindex = fileindex;
7160 rfa.progress_cb = progress_cb;
7161 rfa.progress_arg = progress_arg;
7162 rfa.patch_cb = NULL;
7163 rfa.patch_arg = NULL;
7164 rfa.repo = repo;
7165 rfa.unlink_added_files = 0;
7166 err = worktree_status(worktree, "", fileindex, repo,
7167 revert_file, &rfa, NULL, NULL, 0, 0);
7168 if (err)
7169 goto sync;
7171 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7172 repo, progress_cb, progress_arg, NULL, NULL);
7173 sync:
7174 sync_err = sync_fileindex(fileindex, fileindex_path);
7175 if (sync_err && err == NULL)
7176 err = sync_err;
7177 done:
7178 got_ref_close(resolved);
7179 free(tree_id);
7180 free(fileindex_path);
7182 unlockerr = lock_worktree(worktree, LOCK_SH);
7183 if (unlockerr && err == NULL)
7184 err = unlockerr;
7185 return err;
7188 const struct got_error *
7189 got_worktree_histedit_complete(struct got_worktree *worktree,
7190 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7191 struct got_reference *edited_branch, struct got_repository *repo)
7193 const struct got_error *err, *unlockerr, *sync_err;
7194 struct got_object_id *new_head_commit_id = NULL;
7195 struct got_reference *resolved = NULL;
7196 char *fileindex_path = NULL;
7198 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7199 if (err)
7200 return err;
7202 err = got_ref_open(&resolved, repo,
7203 got_ref_get_symref_target(edited_branch), 0);
7204 if (err)
7205 goto done;
7207 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7208 resolved, new_head_commit_id, repo);
7209 if (err)
7210 goto done;
7212 err = got_ref_change_ref(resolved, new_head_commit_id);
7213 if (err)
7214 goto done;
7216 err = got_ref_write(resolved, repo);
7217 if (err)
7218 goto done;
7220 err = got_worktree_set_head_ref(worktree, resolved);
7221 if (err)
7222 goto done;
7224 err = delete_histedit_refs(worktree, repo);
7225 if (err)
7226 goto done;
7228 err = get_fileindex_path(&fileindex_path, worktree);
7229 if (err)
7230 goto done;
7231 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7232 sync_err = sync_fileindex(fileindex, fileindex_path);
7233 if (sync_err && err == NULL)
7234 err = sync_err;
7235 done:
7236 got_fileindex_free(fileindex);
7237 free(fileindex_path);
7238 free(new_head_commit_id);
7239 unlockerr = lock_worktree(worktree, LOCK_SH);
7240 if (unlockerr && err == NULL)
7241 err = unlockerr;
7242 return err;
7245 const struct got_error *
7246 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7247 struct got_object_id *commit_id, struct got_repository *repo)
7249 const struct got_error *err;
7250 char *commit_ref_name;
7252 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7253 if (err)
7254 return err;
7256 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7257 if (err)
7258 goto done;
7260 err = delete_ref(commit_ref_name, repo);
7261 done:
7262 free(commit_ref_name);
7263 return err;
7266 const struct got_error *
7267 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7268 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7269 struct got_worktree *worktree, const char *refname,
7270 struct got_repository *repo)
7272 const struct got_error *err = NULL;
7273 char *fileindex_path = NULL;
7274 struct check_rebase_ok_arg ok_arg;
7276 *fileindex = NULL;
7277 *branch_ref = NULL;
7278 *base_branch_ref = NULL;
7280 err = lock_worktree(worktree, LOCK_EX);
7281 if (err)
7282 return err;
7284 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7285 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7286 "cannot integrate a branch into itself; "
7287 "update -b or different branch name required");
7288 goto done;
7291 err = open_fileindex(fileindex, &fileindex_path, worktree);
7292 if (err)
7293 goto done;
7295 /* Preconditions are the same as for rebase. */
7296 ok_arg.worktree = worktree;
7297 ok_arg.repo = repo;
7298 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7299 &ok_arg);
7300 if (err)
7301 goto done;
7303 err = got_ref_open(branch_ref, repo, refname, 1);
7304 if (err)
7305 goto done;
7307 err = got_ref_open(base_branch_ref, repo,
7308 got_worktree_get_head_ref_name(worktree), 1);
7309 done:
7310 if (err) {
7311 if (*branch_ref) {
7312 got_ref_close(*branch_ref);
7313 *branch_ref = NULL;
7315 if (*base_branch_ref) {
7316 got_ref_close(*base_branch_ref);
7317 *base_branch_ref = NULL;
7319 if (*fileindex) {
7320 got_fileindex_free(*fileindex);
7321 *fileindex = NULL;
7323 lock_worktree(worktree, LOCK_SH);
7325 return err;
7328 const struct got_error *
7329 got_worktree_integrate_continue(struct got_worktree *worktree,
7330 struct got_fileindex *fileindex, struct got_repository *repo,
7331 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7332 got_worktree_checkout_cb progress_cb, void *progress_arg,
7333 got_cancel_cb cancel_cb, void *cancel_arg)
7335 const struct got_error *err = NULL, *sync_err, *unlockerr;
7336 char *fileindex_path = NULL;
7337 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7339 err = get_fileindex_path(&fileindex_path, worktree);
7340 if (err)
7341 goto done;
7343 err = got_ref_resolve(&commit_id, repo, branch_ref);
7344 if (err)
7345 goto done;
7347 err = got_object_id_by_path(&tree_id, repo, commit_id,
7348 worktree->path_prefix);
7349 if (err)
7350 goto done;
7352 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7353 if (err)
7354 goto done;
7356 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7357 progress_cb, progress_arg, cancel_cb, cancel_arg);
7358 if (err)
7359 goto sync;
7361 err = got_ref_change_ref(base_branch_ref, commit_id);
7362 if (err)
7363 goto sync;
7365 err = got_ref_write(base_branch_ref, repo);
7366 if (err)
7367 goto sync;
7369 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7370 sync:
7371 sync_err = sync_fileindex(fileindex, fileindex_path);
7372 if (sync_err && err == NULL)
7373 err = sync_err;
7375 done:
7376 unlockerr = got_ref_unlock(branch_ref);
7377 if (unlockerr && err == NULL)
7378 err = unlockerr;
7379 got_ref_close(branch_ref);
7381 unlockerr = got_ref_unlock(base_branch_ref);
7382 if (unlockerr && err == NULL)
7383 err = unlockerr;
7384 got_ref_close(base_branch_ref);
7386 got_fileindex_free(fileindex);
7387 free(fileindex_path);
7388 free(tree_id);
7390 unlockerr = lock_worktree(worktree, LOCK_SH);
7391 if (unlockerr && err == NULL)
7392 err = unlockerr;
7393 return err;
7396 const struct got_error *
7397 got_worktree_integrate_abort(struct got_worktree *worktree,
7398 struct got_fileindex *fileindex, struct got_repository *repo,
7399 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7401 const struct got_error *err = NULL, *unlockerr = NULL;
7403 got_fileindex_free(fileindex);
7405 err = lock_worktree(worktree, LOCK_SH);
7407 unlockerr = got_ref_unlock(branch_ref);
7408 if (unlockerr && err == NULL)
7409 err = unlockerr;
7410 got_ref_close(branch_ref);
7412 unlockerr = got_ref_unlock(base_branch_ref);
7413 if (unlockerr && err == NULL)
7414 err = unlockerr;
7415 got_ref_close(base_branch_ref);
7417 return err;
7420 const struct got_error *
7421 got_worktree_merge_postpone(struct got_worktree *worktree,
7422 struct got_fileindex *fileindex)
7424 const struct got_error *err, *sync_err;
7425 char *fileindex_path = NULL;
7427 err = get_fileindex_path(&fileindex_path, worktree);
7428 if (err)
7429 goto done;
7431 sync_err = sync_fileindex(fileindex, fileindex_path);
7433 err = lock_worktree(worktree, LOCK_SH);
7434 if (sync_err && err == NULL)
7435 err = sync_err;
7436 done:
7437 got_fileindex_free(fileindex);
7438 free(fileindex_path);
7439 return err;
7442 static const struct got_error *
7443 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7445 const struct got_error *err;
7446 char *branch_refname = NULL, *commit_refname = NULL;
7448 err = get_merge_branch_ref_name(&branch_refname, worktree);
7449 if (err)
7450 goto done;
7451 err = delete_ref(branch_refname, repo);
7452 if (err)
7453 goto done;
7455 err = get_merge_commit_ref_name(&commit_refname, worktree);
7456 if (err)
7457 goto done;
7458 err = delete_ref(commit_refname, repo);
7459 if (err)
7460 goto done;
7462 done:
7463 free(branch_refname);
7464 free(commit_refname);
7465 return err;
7468 struct merge_commit_msg_arg {
7469 struct got_worktree *worktree;
7470 const char *branch_name;
7473 static const struct got_error *
7474 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7475 void *arg)
7477 struct merge_commit_msg_arg *a = arg;
7479 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7480 got_worktree_get_head_ref_name(a->worktree)) == -1)
7481 return got_error_from_errno("asprintf");
7483 return NULL;
7486 static const struct got_error *
7487 merge_status_cb(void *arg, unsigned char status, unsigned char staged_status,
7488 const char *path, struct got_object_id *blob_id,
7489 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7490 int dirfd, const char *de_name)
7492 return NULL;
7495 const struct got_error *
7496 got_worktree_merge_branch(struct got_worktree *worktree,
7497 struct got_fileindex *fileindex,
7498 struct got_object_id *yca_commit_id,
7499 struct got_object_id *branch_tip,
7500 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7501 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7503 const struct got_error *err;
7504 char *fileindex_path = NULL;
7506 err = get_fileindex_path(&fileindex_path, worktree);
7507 if (err)
7508 goto done;
7510 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7511 worktree);
7512 if (err)
7513 goto done;
7515 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7516 branch_tip, repo, progress_cb, progress_arg,
7517 cancel_cb, cancel_arg);
7518 done:
7519 free(fileindex_path);
7520 return err;
7523 const struct got_error *
7524 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7525 struct got_worktree *worktree, struct got_fileindex *fileindex,
7526 const char *author, const char *committer, int allow_bad_symlinks,
7527 struct got_object_id *branch_tip, const char *branch_name,
7528 struct got_repository *repo)
7530 const struct got_error *err = NULL, *sync_err;
7531 struct got_pathlist_head commitable_paths;
7532 struct collect_commitables_arg cc_arg;
7533 struct got_pathlist_entry *pe;
7534 struct got_reference *head_ref = NULL;
7535 struct got_object_id *head_commit_id = NULL;
7536 int have_staged_files = 0;
7537 struct merge_commit_msg_arg mcm_arg;
7538 char *fileindex_path = NULL;
7540 *new_commit_id = NULL;
7542 TAILQ_INIT(&commitable_paths);
7544 err = get_fileindex_path(&fileindex_path, worktree);
7545 if (err)
7546 goto done;
7548 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7549 if (err)
7550 goto done;
7552 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7553 if (err)
7554 goto done;
7556 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7557 &have_staged_files);
7558 if (err && err->code != GOT_ERR_CANCELLED)
7559 goto done;
7560 if (have_staged_files) {
7561 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7562 goto done;
7565 cc_arg.commitable_paths = &commitable_paths;
7566 cc_arg.worktree = worktree;
7567 cc_arg.fileindex = fileindex;
7568 cc_arg.repo = repo;
7569 cc_arg.have_staged_files = have_staged_files;
7570 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7571 err = worktree_status(worktree, "", fileindex, repo,
7572 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
7573 if (err)
7574 goto done;
7576 if (TAILQ_EMPTY(&commitable_paths)) {
7577 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7578 "merge of %s cannot proceed", branch_name);
7579 goto done;
7582 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7583 struct got_commitable *ct = pe->data;
7584 const char *ct_path = ct->in_repo_path;
7586 while (ct_path[0] == '/')
7587 ct_path++;
7588 err = check_out_of_date(ct_path, ct->status,
7589 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7590 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7591 if (err)
7592 goto done;
7596 mcm_arg.worktree = worktree;
7597 mcm_arg.branch_name = branch_name;
7598 err = commit_worktree(new_commit_id, &commitable_paths,
7599 head_commit_id, branch_tip, worktree, author, committer,
7600 merge_commit_msg_cb, &mcm_arg, merge_status_cb, NULL, repo);
7601 if (err)
7602 goto done;
7604 err = update_fileindex_after_commit(worktree, &commitable_paths,
7605 *new_commit_id, fileindex, have_staged_files);
7606 sync_err = sync_fileindex(fileindex, fileindex_path);
7607 if (sync_err && err == NULL)
7608 err = sync_err;
7609 done:
7610 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7611 struct got_commitable *ct = pe->data;
7612 free_commitable(ct);
7614 got_pathlist_free(&commitable_paths);
7615 free(fileindex_path);
7616 return err;
7619 const struct got_error *
7620 got_worktree_merge_complete(struct got_worktree *worktree,
7621 struct got_fileindex *fileindex, struct got_repository *repo)
7623 const struct got_error *err, *unlockerr, *sync_err;
7624 char *fileindex_path = NULL;
7626 err = delete_merge_refs(worktree, repo);
7627 if (err)
7628 goto done;
7630 err = get_fileindex_path(&fileindex_path, worktree);
7631 if (err)
7632 goto done;
7633 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7634 sync_err = sync_fileindex(fileindex, fileindex_path);
7635 if (sync_err && err == NULL)
7636 err = sync_err;
7637 done:
7638 got_fileindex_free(fileindex);
7639 free(fileindex_path);
7640 unlockerr = lock_worktree(worktree, LOCK_SH);
7641 if (unlockerr && err == NULL)
7642 err = unlockerr;
7643 return err;
7646 const struct got_error *
7647 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7648 struct got_repository *repo)
7650 const struct got_error *err;
7651 char *branch_refname = NULL;
7652 struct got_reference *branch_ref = NULL;
7654 *in_progress = 0;
7656 err = get_merge_branch_ref_name(&branch_refname, worktree);
7657 if (err)
7658 return err;
7659 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7660 free(branch_refname);
7661 if (err) {
7662 if (err->code != GOT_ERR_NOT_REF)
7663 return err;
7664 } else
7665 *in_progress = 1;
7667 return NULL;
7670 const struct got_error *got_worktree_merge_prepare(
7671 struct got_fileindex **fileindex, struct got_worktree *worktree,
7672 struct got_reference *branch, struct got_repository *repo)
7674 const struct got_error *err = NULL;
7675 char *fileindex_path = NULL;
7676 char *branch_refname = NULL, *commit_refname = NULL;
7677 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7678 struct got_reference *commit_ref = NULL;
7679 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7680 struct check_rebase_ok_arg ok_arg;
7682 *fileindex = NULL;
7684 err = lock_worktree(worktree, LOCK_EX);
7685 if (err)
7686 return err;
7688 err = open_fileindex(fileindex, &fileindex_path, worktree);
7689 if (err)
7690 goto done;
7692 /* Preconditions are the same as for rebase. */
7693 ok_arg.worktree = worktree;
7694 ok_arg.repo = repo;
7695 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7696 &ok_arg);
7697 if (err)
7698 goto done;
7700 err = get_merge_branch_ref_name(&branch_refname, worktree);
7701 if (err)
7702 return err;
7704 err = get_merge_commit_ref_name(&commit_refname, worktree);
7705 if (err)
7706 return err;
7708 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7709 0);
7710 if (err)
7711 goto done;
7713 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7714 if (err)
7715 goto done;
7717 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7718 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7719 goto done;
7722 err = got_ref_resolve(&branch_tip, repo, branch);
7723 if (err)
7724 goto done;
7726 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7727 if (err)
7728 goto done;
7729 err = got_ref_write(branch_ref, repo);
7730 if (err)
7731 goto done;
7733 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7734 if (err)
7735 goto done;
7736 err = got_ref_write(commit_ref, repo);
7737 if (err)
7738 goto done;
7740 done:
7741 free(branch_refname);
7742 free(commit_refname);
7743 free(fileindex_path);
7744 if (branch_ref)
7745 got_ref_close(branch_ref);
7746 if (commit_ref)
7747 got_ref_close(commit_ref);
7748 if (wt_branch)
7749 got_ref_close(wt_branch);
7750 free(wt_branch_tip);
7751 if (err) {
7752 if (*fileindex) {
7753 got_fileindex_free(*fileindex);
7754 *fileindex = NULL;
7756 lock_worktree(worktree, LOCK_SH);
7758 return err;
7761 const struct got_error *
7762 got_worktree_merge_continue(char **branch_name,
7763 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7764 struct got_worktree *worktree, struct got_repository *repo)
7766 const struct got_error *err;
7767 char *commit_refname = NULL, *branch_refname = NULL;
7768 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7769 char *fileindex_path = NULL;
7770 int have_staged_files = 0;
7772 *branch_name = NULL;
7773 *branch_tip = NULL;
7774 *fileindex = NULL;
7776 err = lock_worktree(worktree, LOCK_EX);
7777 if (err)
7778 return err;
7780 err = open_fileindex(fileindex, &fileindex_path, worktree);
7781 if (err)
7782 goto done;
7784 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7785 &have_staged_files);
7786 if (err && err->code != GOT_ERR_CANCELLED)
7787 goto done;
7788 if (have_staged_files) {
7789 err = got_error(GOT_ERR_STAGED_PATHS);
7790 goto done;
7793 err = get_merge_branch_ref_name(&branch_refname, worktree);
7794 if (err)
7795 goto done;
7797 err = get_merge_commit_ref_name(&commit_refname, worktree);
7798 if (err)
7799 goto done;
7801 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7802 if (err)
7803 goto done;
7805 if (!got_ref_is_symbolic(branch_ref)) {
7806 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7807 "%s is not a symbolic reference",
7808 got_ref_get_name(branch_ref));
7809 goto done;
7811 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7812 if (*branch_name == NULL) {
7813 err = got_error_from_errno("strdup");
7814 goto done;
7817 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7818 if (err)
7819 goto done;
7821 err = got_ref_resolve(branch_tip, repo, commit_ref);
7822 if (err)
7823 goto done;
7824 done:
7825 free(commit_refname);
7826 free(branch_refname);
7827 free(fileindex_path);
7828 if (commit_ref)
7829 got_ref_close(commit_ref);
7830 if (branch_ref)
7831 got_ref_close(branch_ref);
7832 if (err) {
7833 if (*branch_name) {
7834 free(*branch_name);
7835 *branch_name = NULL;
7837 free(*branch_tip);
7838 *branch_tip = NULL;
7839 if (*fileindex) {
7840 got_fileindex_free(*fileindex);
7841 *fileindex = NULL;
7843 lock_worktree(worktree, LOCK_SH);
7845 return err;
7848 const struct got_error *
7849 got_worktree_merge_abort(struct got_worktree *worktree,
7850 struct got_fileindex *fileindex, struct got_repository *repo,
7851 got_worktree_checkout_cb progress_cb, void *progress_arg)
7853 const struct got_error *err, *unlockerr, *sync_err;
7854 struct got_object_id *commit_id = NULL;
7855 char *fileindex_path = NULL;
7856 struct revert_file_args rfa;
7857 struct got_object_id *tree_id = NULL;
7859 err = got_object_id_by_path(&tree_id, repo,
7860 worktree->base_commit_id, worktree->path_prefix);
7861 if (err)
7862 goto done;
7864 err = delete_merge_refs(worktree, repo);
7865 if (err)
7866 goto done;
7868 err = get_fileindex_path(&fileindex_path, worktree);
7869 if (err)
7870 goto done;
7872 rfa.worktree = worktree;
7873 rfa.fileindex = fileindex;
7874 rfa.progress_cb = progress_cb;
7875 rfa.progress_arg = progress_arg;
7876 rfa.patch_cb = NULL;
7877 rfa.patch_arg = NULL;
7878 rfa.repo = repo;
7879 rfa.unlink_added_files = 1;
7880 err = worktree_status(worktree, "", fileindex, repo,
7881 revert_file, &rfa, NULL, NULL, 0, 0);
7882 if (err)
7883 goto sync;
7885 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7886 repo, progress_cb, progress_arg, NULL, NULL);
7887 sync:
7888 sync_err = sync_fileindex(fileindex, fileindex_path);
7889 if (sync_err && err == NULL)
7890 err = sync_err;
7891 done:
7892 free(tree_id);
7893 free(commit_id);
7894 if (fileindex)
7895 got_fileindex_free(fileindex);
7896 free(fileindex_path);
7898 unlockerr = lock_worktree(worktree, LOCK_SH);
7899 if (unlockerr && err == NULL)
7900 err = unlockerr;
7901 return err;
7904 struct check_stage_ok_arg {
7905 struct got_object_id *head_commit_id;
7906 struct got_worktree *worktree;
7907 struct got_fileindex *fileindex;
7908 struct got_repository *repo;
7909 int have_changes;
7912 const struct got_error *
7913 check_stage_ok(void *arg, unsigned char status,
7914 unsigned char staged_status, const char *relpath,
7915 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7916 struct got_object_id *commit_id, int dirfd, const char *de_name)
7918 struct check_stage_ok_arg *a = arg;
7919 const struct got_error *err = NULL;
7920 struct got_fileindex_entry *ie;
7921 struct got_object_id base_commit_id;
7922 struct got_object_id *base_commit_idp = NULL;
7923 char *in_repo_path = NULL, *p;
7925 if (status == GOT_STATUS_UNVERSIONED ||
7926 status == GOT_STATUS_NO_CHANGE)
7927 return NULL;
7928 if (status == GOT_STATUS_NONEXISTENT)
7929 return got_error_set_errno(ENOENT, relpath);
7931 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7932 if (ie == NULL)
7933 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7935 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7936 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7937 relpath) == -1)
7938 return got_error_from_errno("asprintf");
7940 if (got_fileindex_entry_has_commit(ie)) {
7941 memcpy(base_commit_id.sha1, ie->commit_sha1,
7942 SHA1_DIGEST_LENGTH);
7943 base_commit_idp = &base_commit_id;
7946 if (status == GOT_STATUS_CONFLICT) {
7947 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7948 goto done;
7949 } else if (status != GOT_STATUS_ADD &&
7950 status != GOT_STATUS_MODIFY &&
7951 status != GOT_STATUS_DELETE) {
7952 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7953 goto done;
7956 a->have_changes = 1;
7958 p = in_repo_path;
7959 while (p[0] == '/')
7960 p++;
7961 err = check_out_of_date(p, status, staged_status,
7962 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7963 GOT_ERR_STAGE_OUT_OF_DATE);
7964 done:
7965 free(in_repo_path);
7966 return err;
7969 struct stage_path_arg {
7970 struct got_worktree *worktree;
7971 struct got_fileindex *fileindex;
7972 struct got_repository *repo;
7973 got_worktree_status_cb status_cb;
7974 void *status_arg;
7975 got_worktree_patch_cb patch_cb;
7976 void *patch_arg;
7977 int staged_something;
7978 int allow_bad_symlinks;
7981 static const struct got_error *
7982 stage_path(void *arg, unsigned char status,
7983 unsigned char staged_status, const char *relpath,
7984 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7985 struct got_object_id *commit_id, int dirfd, const char *de_name)
7987 struct stage_path_arg *a = arg;
7988 const struct got_error *err = NULL;
7989 struct got_fileindex_entry *ie;
7990 char *ondisk_path = NULL, *path_content = NULL;
7991 uint32_t stage;
7992 struct got_object_id *new_staged_blob_id = NULL;
7993 struct stat sb;
7995 if (status == GOT_STATUS_UNVERSIONED)
7996 return NULL;
7998 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7999 if (ie == NULL)
8000 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8002 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8003 relpath)== -1)
8004 return got_error_from_errno("asprintf");
8006 switch (status) {
8007 case GOT_STATUS_ADD:
8008 case GOT_STATUS_MODIFY:
8009 /* XXX could sb.st_mode be passed in by our caller? */
8010 if (lstat(ondisk_path, &sb) == -1) {
8011 err = got_error_from_errno2("lstat", ondisk_path);
8012 break;
8014 if (a->patch_cb) {
8015 if (status == GOT_STATUS_ADD) {
8016 int choice = GOT_PATCH_CHOICE_NONE;
8017 err = (*a->patch_cb)(&choice, a->patch_arg,
8018 status, ie->path, NULL, 1, 1);
8019 if (err)
8020 break;
8021 if (choice != GOT_PATCH_CHOICE_YES)
8022 break;
8023 } else {
8024 err = create_patched_content(&path_content, 0,
8025 staged_blob_id ? staged_blob_id : blob_id,
8026 ondisk_path, dirfd, de_name, ie->path,
8027 a->repo, a->patch_cb, a->patch_arg);
8028 if (err || path_content == NULL)
8029 break;
8032 err = got_object_blob_create(&new_staged_blob_id,
8033 path_content ? path_content : ondisk_path, a->repo);
8034 if (err)
8035 break;
8036 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8037 SHA1_DIGEST_LENGTH);
8038 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8039 stage = GOT_FILEIDX_STAGE_ADD;
8040 else
8041 stage = GOT_FILEIDX_STAGE_MODIFY;
8042 got_fileindex_entry_stage_set(ie, stage);
8043 if (S_ISLNK(sb.st_mode)) {
8044 int is_bad_symlink = 0;
8045 if (!a->allow_bad_symlinks) {
8046 char target_path[PATH_MAX];
8047 ssize_t target_len;
8048 target_len = readlink(ondisk_path, target_path,
8049 sizeof(target_path));
8050 if (target_len == -1) {
8051 err = got_error_from_errno2("readlink",
8052 ondisk_path);
8053 break;
8055 err = is_bad_symlink_target(&is_bad_symlink,
8056 target_path, target_len, ondisk_path,
8057 a->worktree->root_path);
8058 if (err)
8059 break;
8060 if (is_bad_symlink) {
8061 err = got_error_path(ondisk_path,
8062 GOT_ERR_BAD_SYMLINK);
8063 break;
8066 if (is_bad_symlink)
8067 got_fileindex_entry_staged_filetype_set(ie,
8068 GOT_FILEIDX_MODE_BAD_SYMLINK);
8069 else
8070 got_fileindex_entry_staged_filetype_set(ie,
8071 GOT_FILEIDX_MODE_SYMLINK);
8072 } else {
8073 got_fileindex_entry_staged_filetype_set(ie,
8074 GOT_FILEIDX_MODE_REGULAR_FILE);
8076 a->staged_something = 1;
8077 if (a->status_cb == NULL)
8078 break;
8079 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8080 get_staged_status(ie), relpath, blob_id,
8081 new_staged_blob_id, NULL, dirfd, de_name);
8082 break;
8083 case GOT_STATUS_DELETE:
8084 if (staged_status == GOT_STATUS_DELETE)
8085 break;
8086 if (a->patch_cb) {
8087 int choice = GOT_PATCH_CHOICE_NONE;
8088 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8089 ie->path, NULL, 1, 1);
8090 if (err)
8091 break;
8092 if (choice == GOT_PATCH_CHOICE_NO)
8093 break;
8094 if (choice != GOT_PATCH_CHOICE_YES) {
8095 err = got_error(GOT_ERR_PATCH_CHOICE);
8096 break;
8099 stage = GOT_FILEIDX_STAGE_DELETE;
8100 got_fileindex_entry_stage_set(ie, stage);
8101 a->staged_something = 1;
8102 if (a->status_cb == NULL)
8103 break;
8104 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8105 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8106 de_name);
8107 break;
8108 case GOT_STATUS_NO_CHANGE:
8109 break;
8110 case GOT_STATUS_CONFLICT:
8111 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8112 break;
8113 case GOT_STATUS_NONEXISTENT:
8114 err = got_error_set_errno(ENOENT, relpath);
8115 break;
8116 default:
8117 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8118 break;
8121 if (path_content && unlink(path_content) == -1 && err == NULL)
8122 err = got_error_from_errno2("unlink", path_content);
8123 free(path_content);
8124 free(ondisk_path);
8125 free(new_staged_blob_id);
8126 return err;
8129 const struct got_error *
8130 got_worktree_stage(struct got_worktree *worktree,
8131 struct got_pathlist_head *paths,
8132 got_worktree_status_cb status_cb, void *status_arg,
8133 got_worktree_patch_cb patch_cb, void *patch_arg,
8134 int allow_bad_symlinks, struct got_repository *repo)
8136 const struct got_error *err = NULL, *sync_err, *unlockerr;
8137 struct got_pathlist_entry *pe;
8138 struct got_fileindex *fileindex = NULL;
8139 char *fileindex_path = NULL;
8140 struct got_reference *head_ref = NULL;
8141 struct got_object_id *head_commit_id = NULL;
8142 struct check_stage_ok_arg oka;
8143 struct stage_path_arg spa;
8145 err = lock_worktree(worktree, LOCK_EX);
8146 if (err)
8147 return err;
8149 err = got_ref_open(&head_ref, repo,
8150 got_worktree_get_head_ref_name(worktree), 0);
8151 if (err)
8152 goto done;
8153 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8154 if (err)
8155 goto done;
8156 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8157 if (err)
8158 goto done;
8160 /* Check pre-conditions before staging anything. */
8161 oka.head_commit_id = head_commit_id;
8162 oka.worktree = worktree;
8163 oka.fileindex = fileindex;
8164 oka.repo = repo;
8165 oka.have_changes = 0;
8166 TAILQ_FOREACH(pe, paths, entry) {
8167 err = worktree_status(worktree, pe->path, fileindex, repo,
8168 check_stage_ok, &oka, NULL, NULL, 0, 0);
8169 if (err)
8170 goto done;
8172 if (!oka.have_changes) {
8173 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8174 goto done;
8177 spa.worktree = worktree;
8178 spa.fileindex = fileindex;
8179 spa.repo = repo;
8180 spa.patch_cb = patch_cb;
8181 spa.patch_arg = patch_arg;
8182 spa.status_cb = status_cb;
8183 spa.status_arg = status_arg;
8184 spa.staged_something = 0;
8185 spa.allow_bad_symlinks = allow_bad_symlinks;
8186 TAILQ_FOREACH(pe, paths, entry) {
8187 err = worktree_status(worktree, pe->path, fileindex, repo,
8188 stage_path, &spa, NULL, NULL, 0, 0);
8189 if (err)
8190 goto done;
8192 if (!spa.staged_something) {
8193 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8194 goto done;
8197 sync_err = sync_fileindex(fileindex, fileindex_path);
8198 if (sync_err && err == NULL)
8199 err = sync_err;
8200 done:
8201 if (head_ref)
8202 got_ref_close(head_ref);
8203 free(head_commit_id);
8204 free(fileindex_path);
8205 if (fileindex)
8206 got_fileindex_free(fileindex);
8207 unlockerr = lock_worktree(worktree, LOCK_SH);
8208 if (unlockerr && err == NULL)
8209 err = unlockerr;
8210 return err;
8213 struct unstage_path_arg {
8214 struct got_worktree *worktree;
8215 struct got_fileindex *fileindex;
8216 struct got_repository *repo;
8217 got_worktree_checkout_cb progress_cb;
8218 void *progress_arg;
8219 got_worktree_patch_cb patch_cb;
8220 void *patch_arg;
8223 static const struct got_error *
8224 create_unstaged_content(char **path_unstaged_content,
8225 char **path_new_staged_content, struct got_object_id *blob_id,
8226 struct got_object_id *staged_blob_id, const char *relpath,
8227 struct got_repository *repo,
8228 got_worktree_patch_cb patch_cb, void *patch_arg)
8230 const struct got_error *err, *free_err;
8231 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8232 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8233 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8234 struct got_diffreg_result *diffreg_result = NULL;
8235 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8236 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8238 *path_unstaged_content = NULL;
8239 *path_new_staged_content = NULL;
8241 err = got_object_id_str(&label1, blob_id);
8242 if (err)
8243 return err;
8244 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8245 if (err)
8246 goto done;
8248 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8249 if (err)
8250 goto done;
8252 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8253 if (err)
8254 goto done;
8256 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8257 if (err)
8258 goto done;
8260 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8261 if (err)
8262 goto done;
8264 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8265 if (err)
8266 goto done;
8268 err = got_diff_files(&diffreg_result, f1, label1, f2,
8269 path2, 3, 0, 1, NULL);
8270 if (err)
8271 goto done;
8273 err = got_opentemp_named(path_unstaged_content, &outfile,
8274 "got-unstaged-content");
8275 if (err)
8276 goto done;
8277 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8278 "got-new-staged-content");
8279 if (err)
8280 goto done;
8282 if (fseek(f1, 0L, SEEK_SET) == -1) {
8283 err = got_ferror(f1, GOT_ERR_IO);
8284 goto done;
8286 if (fseek(f2, 0L, SEEK_SET) == -1) {
8287 err = got_ferror(f2, GOT_ERR_IO);
8288 goto done;
8290 /* Count the number of actual changes in the diff result. */
8291 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8292 struct diff_chunk_context cc = {};
8293 diff_chunk_context_load_change(&cc, &nchunks_used,
8294 diffreg_result->result, n, 0);
8295 nchanges++;
8297 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8298 int choice;
8299 err = apply_or_reject_change(&choice, &nchunks_used,
8300 diffreg_result->result, n, relpath, f1, f2,
8301 &line_cur1, &line_cur2,
8302 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8303 if (err)
8304 goto done;
8305 if (choice == GOT_PATCH_CHOICE_YES)
8306 have_content = 1;
8307 else
8308 have_rejected_content = 1;
8309 if (choice == GOT_PATCH_CHOICE_QUIT)
8310 break;
8312 if (have_content || have_rejected_content)
8313 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8314 outfile, rejectfile);
8315 done:
8316 free(label1);
8317 if (blob)
8318 got_object_blob_close(blob);
8319 if (staged_blob)
8320 got_object_blob_close(staged_blob);
8321 free_err = got_diffreg_result_free(diffreg_result);
8322 if (free_err && err == NULL)
8323 err = free_err;
8324 if (f1 && fclose(f1) == EOF && err == NULL)
8325 err = got_error_from_errno2("fclose", path1);
8326 if (f2 && fclose(f2) == EOF && err == NULL)
8327 err = got_error_from_errno2("fclose", path2);
8328 if (outfile && fclose(outfile) == EOF && err == NULL)
8329 err = got_error_from_errno2("fclose", *path_unstaged_content);
8330 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8331 err = got_error_from_errno2("fclose", *path_new_staged_content);
8332 if (path1 && unlink(path1) == -1 && err == NULL)
8333 err = got_error_from_errno2("unlink", path1);
8334 if (path2 && unlink(path2) == -1 && err == NULL)
8335 err = got_error_from_errno2("unlink", path2);
8336 if (err || !have_content) {
8337 if (*path_unstaged_content &&
8338 unlink(*path_unstaged_content) == -1 && err == NULL)
8339 err = got_error_from_errno2("unlink",
8340 *path_unstaged_content);
8341 free(*path_unstaged_content);
8342 *path_unstaged_content = NULL;
8344 if (err || !have_content || !have_rejected_content) {
8345 if (*path_new_staged_content &&
8346 unlink(*path_new_staged_content) == -1 && err == NULL)
8347 err = got_error_from_errno2("unlink",
8348 *path_new_staged_content);
8349 free(*path_new_staged_content);
8350 *path_new_staged_content = NULL;
8352 free(path1);
8353 free(path2);
8354 return err;
8357 static const struct got_error *
8358 unstage_hunks(struct got_object_id *staged_blob_id,
8359 struct got_blob_object *blob_base,
8360 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8361 const char *ondisk_path, const char *label_orig,
8362 struct got_worktree *worktree, struct got_repository *repo,
8363 got_worktree_patch_cb patch_cb, void *patch_arg,
8364 got_worktree_checkout_cb progress_cb, void *progress_arg)
8366 const struct got_error *err = NULL;
8367 char *path_unstaged_content = NULL;
8368 char *path_new_staged_content = NULL;
8369 char *parent = NULL, *base_path = NULL;
8370 char *blob_base_path = NULL;
8371 struct got_object_id *new_staged_blob_id = NULL;
8372 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8373 struct stat sb;
8375 err = create_unstaged_content(&path_unstaged_content,
8376 &path_new_staged_content, blob_id, staged_blob_id,
8377 ie->path, repo, patch_cb, patch_arg);
8378 if (err)
8379 return err;
8381 if (path_unstaged_content == NULL)
8382 return NULL;
8384 if (path_new_staged_content) {
8385 err = got_object_blob_create(&new_staged_blob_id,
8386 path_new_staged_content, repo);
8387 if (err)
8388 goto done;
8391 f = fopen(path_unstaged_content, "r");
8392 if (f == NULL) {
8393 err = got_error_from_errno2("fopen",
8394 path_unstaged_content);
8395 goto done;
8397 if (fstat(fileno(f), &sb) == -1) {
8398 err = got_error_from_errno2("fstat", path_unstaged_content);
8399 goto done;
8401 if (got_fileindex_entry_staged_filetype_get(ie) ==
8402 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8403 char link_target[PATH_MAX];
8404 size_t r;
8405 r = fread(link_target, 1, sizeof(link_target), f);
8406 if (r == 0 && ferror(f)) {
8407 err = got_error_from_errno("fread");
8408 goto done;
8410 if (r >= sizeof(link_target)) { /* should not happen */
8411 err = got_error(GOT_ERR_NO_SPACE);
8412 goto done;
8414 link_target[r] = '\0';
8415 err = merge_symlink(worktree, blob_base,
8416 ondisk_path, ie->path, label_orig, link_target,
8417 worktree->base_commit_id, repo, progress_cb,
8418 progress_arg);
8419 } else {
8420 int local_changes_subsumed;
8422 err = got_path_dirname(&parent, ondisk_path);
8423 if (err)
8424 return err;
8426 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8427 parent) == -1) {
8428 err = got_error_from_errno("asprintf");
8429 base_path = NULL;
8430 goto done;
8433 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8434 if (err)
8435 goto done;
8436 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8437 blob_base);
8438 if (err)
8439 goto done;
8442 * In order the run a 3-way merge with a symlink we copy the symlink's
8443 * target path into a temporary file and use that file with diff3.
8445 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8446 err = dump_symlink_target_path_to_file(&f_deriv2,
8447 ondisk_path);
8448 if (err)
8449 goto done;
8450 } else {
8451 int fd;
8452 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
8453 if (fd == -1) {
8454 err = got_error_from_errno2("open", ondisk_path);
8455 goto done;
8457 f_deriv2 = fdopen(fd, "r");
8458 if (f_deriv2 == NULL) {
8459 err = got_error_from_errno2("fdopen", ondisk_path);
8460 close(fd);
8461 goto done;
8465 err = merge_file(&local_changes_subsumed, worktree,
8466 f_base, f, f_deriv2, ondisk_path, ie->path,
8467 got_fileindex_perms_to_st(ie),
8468 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8469 repo, progress_cb, progress_arg);
8471 if (err)
8472 goto done;
8474 if (new_staged_blob_id) {
8475 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8476 SHA1_DIGEST_LENGTH);
8477 } else {
8478 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8479 got_fileindex_entry_staged_filetype_set(ie, 0);
8481 done:
8482 free(new_staged_blob_id);
8483 if (path_unstaged_content &&
8484 unlink(path_unstaged_content) == -1 && err == NULL)
8485 err = got_error_from_errno2("unlink", path_unstaged_content);
8486 if (path_new_staged_content &&
8487 unlink(path_new_staged_content) == -1 && err == NULL)
8488 err = got_error_from_errno2("unlink", path_new_staged_content);
8489 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8490 err = got_error_from_errno2("unlink", blob_base_path);
8491 if (f_base && fclose(f_base) == EOF && err == NULL)
8492 err = got_error_from_errno2("fclose", path_unstaged_content);
8493 if (f && fclose(f) == EOF && err == NULL)
8494 err = got_error_from_errno2("fclose", path_unstaged_content);
8495 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8496 err = got_error_from_errno2("fclose", ondisk_path);
8497 free(path_unstaged_content);
8498 free(path_new_staged_content);
8499 free(blob_base_path);
8500 free(parent);
8501 free(base_path);
8502 return err;
8505 static const struct got_error *
8506 unstage_path(void *arg, unsigned char status,
8507 unsigned char staged_status, const char *relpath,
8508 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8509 struct got_object_id *commit_id, int dirfd, const char *de_name)
8511 const struct got_error *err = NULL;
8512 struct unstage_path_arg *a = arg;
8513 struct got_fileindex_entry *ie;
8514 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8515 char *ondisk_path = NULL;
8516 char *id_str = NULL, *label_orig = NULL;
8517 int local_changes_subsumed;
8518 struct stat sb;
8520 if (staged_status != GOT_STATUS_ADD &&
8521 staged_status != GOT_STATUS_MODIFY &&
8522 staged_status != GOT_STATUS_DELETE)
8523 return NULL;
8525 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8526 if (ie == NULL)
8527 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8529 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8530 == -1)
8531 return got_error_from_errno("asprintf");
8533 err = got_object_id_str(&id_str,
8534 commit_id ? commit_id : a->worktree->base_commit_id);
8535 if (err)
8536 goto done;
8537 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8538 id_str) == -1) {
8539 err = got_error_from_errno("asprintf");
8540 goto done;
8543 switch (staged_status) {
8544 case GOT_STATUS_MODIFY:
8545 err = got_object_open_as_blob(&blob_base, a->repo,
8546 blob_id, 8192);
8547 if (err)
8548 break;
8549 /* fall through */
8550 case GOT_STATUS_ADD:
8551 if (a->patch_cb) {
8552 if (staged_status == GOT_STATUS_ADD) {
8553 int choice = GOT_PATCH_CHOICE_NONE;
8554 err = (*a->patch_cb)(&choice, a->patch_arg,
8555 staged_status, ie->path, NULL, 1, 1);
8556 if (err)
8557 break;
8558 if (choice != GOT_PATCH_CHOICE_YES)
8559 break;
8560 } else {
8561 err = unstage_hunks(staged_blob_id,
8562 blob_base, blob_id, ie, ondisk_path,
8563 label_orig, a->worktree, a->repo,
8564 a->patch_cb, a->patch_arg,
8565 a->progress_cb, a->progress_arg);
8566 break; /* Done with this file. */
8569 err = got_object_open_as_blob(&blob_staged, a->repo,
8570 staged_blob_id, 8192);
8571 if (err)
8572 break;
8573 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8574 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8575 case GOT_FILEIDX_MODE_REGULAR_FILE:
8576 err = merge_blob(&local_changes_subsumed, a->worktree,
8577 blob_base, ondisk_path, relpath,
8578 got_fileindex_perms_to_st(ie), label_orig,
8579 blob_staged, commit_id ? commit_id :
8580 a->worktree->base_commit_id, a->repo,
8581 a->progress_cb, a->progress_arg);
8582 break;
8583 case GOT_FILEIDX_MODE_SYMLINK:
8584 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8585 char *staged_target;
8586 err = got_object_blob_read_to_str(
8587 &staged_target, blob_staged);
8588 if (err)
8589 goto done;
8590 err = merge_symlink(a->worktree, blob_base,
8591 ondisk_path, relpath, label_orig,
8592 staged_target, commit_id ? commit_id :
8593 a->worktree->base_commit_id,
8594 a->repo, a->progress_cb, a->progress_arg);
8595 free(staged_target);
8596 } else {
8597 err = merge_blob(&local_changes_subsumed,
8598 a->worktree, blob_base, ondisk_path,
8599 relpath, got_fileindex_perms_to_st(ie),
8600 label_orig, blob_staged,
8601 commit_id ? commit_id :
8602 a->worktree->base_commit_id, a->repo,
8603 a->progress_cb, a->progress_arg);
8605 break;
8606 default:
8607 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8608 break;
8610 if (err == NULL) {
8611 got_fileindex_entry_stage_set(ie,
8612 GOT_FILEIDX_STAGE_NONE);
8613 got_fileindex_entry_staged_filetype_set(ie, 0);
8615 break;
8616 case GOT_STATUS_DELETE:
8617 if (a->patch_cb) {
8618 int choice = GOT_PATCH_CHOICE_NONE;
8619 err = (*a->patch_cb)(&choice, a->patch_arg,
8620 staged_status, ie->path, NULL, 1, 1);
8621 if (err)
8622 break;
8623 if (choice == GOT_PATCH_CHOICE_NO)
8624 break;
8625 if (choice != GOT_PATCH_CHOICE_YES) {
8626 err = got_error(GOT_ERR_PATCH_CHOICE);
8627 break;
8630 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8631 got_fileindex_entry_staged_filetype_set(ie, 0);
8632 err = get_file_status(&status, &sb, ie, ondisk_path,
8633 dirfd, de_name, a->repo);
8634 if (err)
8635 break;
8636 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8637 break;
8639 done:
8640 free(ondisk_path);
8641 if (blob_base)
8642 got_object_blob_close(blob_base);
8643 if (blob_staged)
8644 got_object_blob_close(blob_staged);
8645 free(id_str);
8646 free(label_orig);
8647 return err;
8650 const struct got_error *
8651 got_worktree_unstage(struct got_worktree *worktree,
8652 struct got_pathlist_head *paths,
8653 got_worktree_checkout_cb progress_cb, void *progress_arg,
8654 got_worktree_patch_cb patch_cb, void *patch_arg,
8655 struct got_repository *repo)
8657 const struct got_error *err = NULL, *sync_err, *unlockerr;
8658 struct got_pathlist_entry *pe;
8659 struct got_fileindex *fileindex = NULL;
8660 char *fileindex_path = NULL;
8661 struct unstage_path_arg upa;
8663 err = lock_worktree(worktree, LOCK_EX);
8664 if (err)
8665 return err;
8667 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8668 if (err)
8669 goto done;
8671 upa.worktree = worktree;
8672 upa.fileindex = fileindex;
8673 upa.repo = repo;
8674 upa.progress_cb = progress_cb;
8675 upa.progress_arg = progress_arg;
8676 upa.patch_cb = patch_cb;
8677 upa.patch_arg = patch_arg;
8678 TAILQ_FOREACH(pe, paths, entry) {
8679 err = worktree_status(worktree, pe->path, fileindex, repo,
8680 unstage_path, &upa, NULL, NULL, 0, 0);
8681 if (err)
8682 goto done;
8685 sync_err = sync_fileindex(fileindex, fileindex_path);
8686 if (sync_err && err == NULL)
8687 err = sync_err;
8688 done:
8689 free(fileindex_path);
8690 if (fileindex)
8691 got_fileindex_free(fileindex);
8692 unlockerr = lock_worktree(worktree, LOCK_SH);
8693 if (unlockerr && err == NULL)
8694 err = unlockerr;
8695 return err;
8698 struct report_file_info_arg {
8699 struct got_worktree *worktree;
8700 got_worktree_path_info_cb info_cb;
8701 void *info_arg;
8702 struct got_pathlist_head *paths;
8703 got_cancel_cb cancel_cb;
8704 void *cancel_arg;
8707 static const struct got_error *
8708 report_file_info(void *arg, struct got_fileindex_entry *ie)
8710 struct report_file_info_arg *a = arg;
8711 struct got_pathlist_entry *pe;
8712 struct got_object_id blob_id, staged_blob_id, commit_id;
8713 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8714 struct got_object_id *commit_idp = NULL;
8715 int stage;
8717 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8718 return got_error(GOT_ERR_CANCELLED);
8720 TAILQ_FOREACH(pe, a->paths, entry) {
8721 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8722 got_path_is_child(ie->path, pe->path, pe->path_len))
8723 break;
8725 if (pe == NULL) /* not found */
8726 return NULL;
8728 if (got_fileindex_entry_has_blob(ie)) {
8729 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8730 blob_idp = &blob_id;
8732 stage = got_fileindex_entry_stage_get(ie);
8733 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8734 stage == GOT_FILEIDX_STAGE_ADD) {
8735 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8736 SHA1_DIGEST_LENGTH);
8737 staged_blob_idp = &staged_blob_id;
8740 if (got_fileindex_entry_has_commit(ie)) {
8741 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8742 commit_idp = &commit_id;
8745 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8746 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8749 const struct got_error *
8750 got_worktree_path_info(struct got_worktree *worktree,
8751 struct got_pathlist_head *paths,
8752 got_worktree_path_info_cb info_cb, void *info_arg,
8753 got_cancel_cb cancel_cb, void *cancel_arg)
8756 const struct got_error *err = NULL, *unlockerr;
8757 struct got_fileindex *fileindex = NULL;
8758 char *fileindex_path = NULL;
8759 struct report_file_info_arg arg;
8761 err = lock_worktree(worktree, LOCK_SH);
8762 if (err)
8763 return err;
8765 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8766 if (err)
8767 goto done;
8769 arg.worktree = worktree;
8770 arg.info_cb = info_cb;
8771 arg.info_arg = info_arg;
8772 arg.paths = paths;
8773 arg.cancel_cb = cancel_cb;
8774 arg.cancel_arg = cancel_arg;
8775 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8776 &arg);
8777 done:
8778 free(fileindex_path);
8779 if (fileindex)
8780 got_fileindex_free(fileindex);
8781 unlockerr = lock_worktree(worktree, LOCK_UN);
8782 if (unlockerr && err == NULL)
8783 err = unlockerr;
8784 return err;