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 if (stat(ondisk_path, &sb) == -1) {
1946 if (errno != ENOENT) {
1947 err = got_error_from_errno2("stat",
1948 ondisk_path);
1949 goto done;
1951 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1952 status = GOT_STATUS_UNVERSIONED;
1953 } else {
1954 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1955 status = GOT_STATUS_UNVERSIONED;
1956 else
1957 status = GOT_STATUS_OBSTRUCTED;
1961 if (status == GOT_STATUS_OBSTRUCTED) {
1962 if (ie)
1963 got_fileindex_entry_mark_skipped(ie);
1964 err = (*progress_cb)(progress_arg, status, path);
1965 goto done;
1967 if (status == GOT_STATUS_CONFLICT) {
1968 if (ie)
1969 got_fileindex_entry_mark_skipped(ie);
1970 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1971 path);
1972 goto done;
1975 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1976 (S_ISLNK(te->mode) ||
1977 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1979 * This is a regular file or an installed bad symlink.
1980 * If the file index indicates that this file is already
1981 * up-to-date with respect to the repository we can skip
1982 * updating contents of this file.
1984 if (got_fileindex_entry_has_commit(ie) &&
1985 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1986 SHA1_DIGEST_LENGTH) == 0) {
1987 /* Same commit. */
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;
1996 if (got_fileindex_entry_has_blob(ie) &&
1997 memcmp(ie->blob_sha1, te->id.sha1,
1998 SHA1_DIGEST_LENGTH) == 0) {
1999 /* Different commit but the same blob. */
2000 err = sync_timestamps(worktree->root_fd,
2001 path, status, ie, &sb);
2002 if (err)
2003 goto done;
2004 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2005 path);
2006 goto done;
2010 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
2011 if (err)
2012 goto done;
2014 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2015 int update_timestamps;
2016 struct got_blob_object *blob2 = NULL;
2017 char *label_orig = NULL;
2018 if (got_fileindex_entry_has_blob(ie)) {
2019 struct got_object_id id2;
2020 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2021 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2022 if (err)
2023 goto done;
2025 if (got_fileindex_entry_has_commit(ie)) {
2026 char id_str[SHA1_DIGEST_STRING_LENGTH];
2027 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2028 sizeof(id_str)) == NULL) {
2029 err = got_error_path(id_str,
2030 GOT_ERR_BAD_OBJ_ID_STR);
2031 goto done;
2033 if (asprintf(&label_orig, "%s: commit %s",
2034 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2035 err = got_error_from_errno("asprintf");
2036 goto done;
2039 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2040 char *link_target;
2041 err = got_object_blob_read_to_str(&link_target, blob);
2042 if (err)
2043 goto done;
2044 err = merge_symlink(worktree, blob2, ondisk_path, path,
2045 label_orig, link_target, worktree->base_commit_id,
2046 repo, progress_cb, progress_arg);
2047 free(link_target);
2048 } else {
2049 err = merge_blob(&update_timestamps, worktree, blob2,
2050 ondisk_path, path, sb.st_mode, label_orig, blob,
2051 worktree->base_commit_id, repo,
2052 progress_cb, progress_arg);
2054 free(label_orig);
2055 if (blob2)
2056 got_object_blob_close(blob2);
2057 if (err)
2058 goto done;
2060 * Do not update timestamps of files with local changes.
2061 * Otherwise, a future status walk would treat them as
2062 * unmodified files again.
2064 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2065 blob->id.sha1, worktree->base_commit_id->sha1,
2066 update_timestamps);
2067 } else if (status == GOT_STATUS_MODE_CHANGE) {
2068 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2069 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2070 } else if (status == GOT_STATUS_DELETE) {
2071 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2072 if (err)
2073 goto done;
2074 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2075 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2076 if (err)
2077 goto done;
2078 } else {
2079 int is_bad_symlink = 0;
2080 if (S_ISLNK(te->mode)) {
2081 err = install_symlink(&is_bad_symlink, worktree,
2082 ondisk_path, path, blob,
2083 status == GOT_STATUS_MISSING, 0,
2084 status == GOT_STATUS_UNVERSIONED, repo,
2085 progress_cb, progress_arg);
2086 } else {
2087 err = install_blob(worktree, ondisk_path, path,
2088 te->mode, sb.st_mode, blob,
2089 status == GOT_STATUS_MISSING, 0, 0,
2090 status == GOT_STATUS_UNVERSIONED, repo,
2091 progress_cb, progress_arg);
2093 if (err)
2094 goto done;
2096 if (ie) {
2097 err = got_fileindex_entry_update(ie,
2098 worktree->root_fd, path, blob->id.sha1,
2099 worktree->base_commit_id->sha1, 1);
2100 } else {
2101 err = create_fileindex_entry(&ie, fileindex,
2102 worktree->base_commit_id, worktree->root_fd, path,
2103 &blob->id);
2105 if (err)
2106 goto done;
2108 if (is_bad_symlink) {
2109 got_fileindex_entry_filetype_set(ie,
2110 GOT_FILEIDX_MODE_BAD_SYMLINK);
2113 got_object_blob_close(blob);
2114 done:
2115 free(ondisk_path);
2116 return err;
2119 static const struct got_error *
2120 remove_ondisk_file(const char *root_path, const char *path)
2122 const struct got_error *err = NULL;
2123 char *ondisk_path = NULL, *parent = NULL;
2125 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2126 return got_error_from_errno("asprintf");
2128 if (unlink(ondisk_path) == -1) {
2129 if (errno != ENOENT)
2130 err = got_error_from_errno2("unlink", ondisk_path);
2131 } else {
2132 size_t root_len = strlen(root_path);
2133 err = got_path_dirname(&parent, ondisk_path);
2134 if (err)
2135 goto done;
2136 while (got_path_cmp(parent, root_path,
2137 strlen(parent), root_len) != 0) {
2138 free(ondisk_path);
2139 ondisk_path = parent;
2140 parent = NULL;
2141 if (rmdir(ondisk_path) == -1) {
2142 if (errno != ENOTEMPTY)
2143 err = got_error_from_errno2("rmdir",
2144 ondisk_path);
2145 break;
2147 err = got_path_dirname(&parent, ondisk_path);
2148 if (err)
2149 break;
2152 done:
2153 free(ondisk_path);
2154 free(parent);
2155 return err;
2158 static const struct got_error *
2159 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2160 struct got_fileindex_entry *ie, struct got_repository *repo,
2161 got_worktree_checkout_cb progress_cb, void *progress_arg)
2163 const struct got_error *err = NULL;
2164 unsigned char status;
2165 struct stat sb;
2166 char *ondisk_path;
2168 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2169 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2171 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2172 == -1)
2173 return got_error_from_errno("asprintf");
2175 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2176 if (err)
2177 goto done;
2179 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2180 char ondisk_target[PATH_MAX];
2181 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2182 sizeof(ondisk_target));
2183 if (ondisk_len == -1) {
2184 err = got_error_from_errno2("readlink", ondisk_path);
2185 goto done;
2187 ondisk_target[ondisk_len] = '\0';
2188 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2189 NULL, NULL, /* XXX pass common ancestor info? */
2190 ondisk_target, ondisk_path);
2191 if (err)
2192 goto done;
2193 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2194 ie->path);
2195 goto done;
2198 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2199 status == GOT_STATUS_ADD) {
2200 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2201 if (err)
2202 goto done;
2204 * Preserve the working file and change the deleted blob's
2205 * entry into a schedule-add entry.
2207 err = got_fileindex_entry_update(ie, worktree->root_fd,
2208 ie->path, NULL, NULL, 0);
2209 } else {
2210 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2211 if (err)
2212 goto done;
2213 if (status == GOT_STATUS_NO_CHANGE) {
2214 err = remove_ondisk_file(worktree->root_path, ie->path);
2215 if (err)
2216 goto done;
2218 got_fileindex_entry_remove(fileindex, ie);
2220 done:
2221 free(ondisk_path);
2222 return err;
2225 struct diff_cb_arg {
2226 struct got_fileindex *fileindex;
2227 struct got_worktree *worktree;
2228 struct got_repository *repo;
2229 got_worktree_checkout_cb progress_cb;
2230 void *progress_arg;
2231 got_cancel_cb cancel_cb;
2232 void *cancel_arg;
2235 static const struct got_error *
2236 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2237 struct got_tree_entry *te, 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 update_blob(a->worktree, a->fileindex, ie, te,
2245 ie->path, a->repo, a->progress_cb, a->progress_arg);
2248 static const struct got_error *
2249 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2251 struct diff_cb_arg *a = arg;
2253 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2254 return got_error(GOT_ERR_CANCELLED);
2256 return delete_blob(a->worktree, a->fileindex, ie,
2257 a->repo, a->progress_cb, a->progress_arg);
2260 static const struct got_error *
2261 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2263 struct diff_cb_arg *a = arg;
2264 const struct got_error *err;
2265 char *path;
2267 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2268 return got_error(GOT_ERR_CANCELLED);
2270 if (got_object_tree_entry_is_submodule(te))
2271 return NULL;
2273 if (asprintf(&path, "%s%s%s", parent_path,
2274 parent_path[0] ? "/" : "", te->name)
2275 == -1)
2276 return got_error_from_errno("asprintf");
2278 if (S_ISDIR(te->mode))
2279 err = add_dir_on_disk(a->worktree, path);
2280 else
2281 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2282 a->repo, a->progress_cb, a->progress_arg);
2284 free(path);
2285 return err;
2288 const struct got_error *
2289 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2291 uint32_t uuid_status;
2293 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2294 if (uuid_status != uuid_s_ok) {
2295 *uuidstr = NULL;
2296 return got_error_uuid(uuid_status, "uuid_to_string");
2299 return NULL;
2302 static const struct got_error *
2303 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2305 const struct got_error *err = NULL;
2306 char *uuidstr = NULL;
2308 *refname = NULL;
2310 err = got_worktree_get_uuid(&uuidstr, worktree);
2311 if (err)
2312 return err;
2314 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2315 err = got_error_from_errno("asprintf");
2316 *refname = NULL;
2318 free(uuidstr);
2319 return err;
2322 const struct got_error *
2323 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2325 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2328 static const struct got_error *
2329 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2331 return get_ref_name(refname, worktree,
2332 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2335 static const struct got_error *
2336 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2338 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2341 static const struct got_error *
2342 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2344 return get_ref_name(refname, worktree,
2345 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2348 static const struct got_error *
2349 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2351 return get_ref_name(refname, worktree,
2352 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2355 static const struct got_error *
2356 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2358 return get_ref_name(refname, worktree,
2359 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2362 static const struct got_error *
2363 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2365 return get_ref_name(refname, worktree,
2366 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2369 static const struct got_error *
2370 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2372 return get_ref_name(refname, worktree,
2373 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2376 static const struct got_error *
2377 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2379 return get_ref_name(refname, worktree,
2380 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2383 const struct got_error *
2384 got_worktree_get_histedit_script_path(char **path,
2385 struct got_worktree *worktree)
2387 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2388 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2389 *path = NULL;
2390 return got_error_from_errno("asprintf");
2392 return NULL;
2395 static const struct got_error *
2396 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2398 return get_ref_name(refname, worktree,
2399 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2402 static const struct got_error *
2403 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2405 return get_ref_name(refname, worktree,
2406 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2410 * Prevent Git's garbage collector from deleting our base commit by
2411 * setting a reference to our base commit's ID.
2413 static const struct got_error *
2414 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2416 const struct got_error *err = NULL;
2417 struct got_reference *ref = NULL;
2418 char *refname;
2420 err = got_worktree_get_base_ref_name(&refname, worktree);
2421 if (err)
2422 return err;
2424 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2425 if (err)
2426 goto done;
2428 err = got_ref_write(ref, repo);
2429 done:
2430 free(refname);
2431 if (ref)
2432 got_ref_close(ref);
2433 return err;
2436 static const struct got_error *
2437 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2439 const struct got_error *err = NULL;
2441 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2442 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2443 err = got_error_from_errno("asprintf");
2444 *fileindex_path = NULL;
2446 return err;
2450 static const struct got_error *
2451 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2452 struct got_worktree *worktree)
2454 const struct got_error *err = NULL;
2455 FILE *index = NULL;
2457 *fileindex_path = NULL;
2458 *fileindex = got_fileindex_alloc();
2459 if (*fileindex == NULL)
2460 return got_error_from_errno("got_fileindex_alloc");
2462 err = get_fileindex_path(fileindex_path, worktree);
2463 if (err)
2464 goto done;
2466 index = fopen(*fileindex_path, "rb");
2467 if (index == NULL) {
2468 if (errno != ENOENT)
2469 err = got_error_from_errno2("fopen", *fileindex_path);
2470 } else {
2471 err = got_fileindex_read(*fileindex, index);
2472 if (fclose(index) == EOF && err == NULL)
2473 err = got_error_from_errno("fclose");
2475 done:
2476 if (err) {
2477 free(*fileindex_path);
2478 *fileindex_path = NULL;
2479 got_fileindex_free(*fileindex);
2480 *fileindex = NULL;
2482 return err;
2485 struct bump_base_commit_id_arg {
2486 struct got_object_id *base_commit_id;
2487 const char *path;
2488 size_t path_len;
2489 const char *entry_name;
2490 got_worktree_checkout_cb progress_cb;
2491 void *progress_arg;
2494 /* Bump base commit ID of all files within an updated part of the work tree. */
2495 static const struct got_error *
2496 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2498 const struct got_error *err;
2499 struct bump_base_commit_id_arg *a = arg;
2501 if (a->entry_name) {
2502 if (strcmp(ie->path, a->path) != 0)
2503 return NULL;
2504 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2505 return NULL;
2507 if (got_fileindex_entry_was_skipped(ie))
2508 return NULL;
2510 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2511 SHA1_DIGEST_LENGTH) == 0)
2512 return NULL;
2514 if (a->progress_cb) {
2515 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2516 ie->path);
2517 if (err)
2518 return err;
2520 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2521 return NULL;
2524 static const struct got_error *
2525 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2526 struct got_fileindex *fileindex,
2527 got_worktree_checkout_cb progress_cb, void *progress_arg)
2529 struct bump_base_commit_id_arg bbc_arg;
2531 bbc_arg.base_commit_id = worktree->base_commit_id;
2532 bbc_arg.entry_name = NULL;
2533 bbc_arg.path = "";
2534 bbc_arg.path_len = 0;
2535 bbc_arg.progress_cb = progress_cb;
2536 bbc_arg.progress_arg = progress_arg;
2538 return got_fileindex_for_each_entry_safe(fileindex,
2539 bump_base_commit_id, &bbc_arg);
2542 static const struct got_error *
2543 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2545 const struct got_error *err = NULL;
2546 char *new_fileindex_path = NULL;
2547 FILE *new_index = NULL;
2548 struct timespec timeout;
2550 err = got_opentemp_named(&new_fileindex_path, &new_index,
2551 fileindex_path);
2552 if (err)
2553 goto done;
2555 err = got_fileindex_write(fileindex, new_index);
2556 if (err)
2557 goto done;
2559 if (rename(new_fileindex_path, fileindex_path) != 0) {
2560 err = got_error_from_errno3("rename", new_fileindex_path,
2561 fileindex_path);
2562 unlink(new_fileindex_path);
2566 * Sleep for a short amount of time to ensure that files modified after
2567 * this program exits have a different time stamp from the one which
2568 * was recorded in the file index.
2570 timeout.tv_sec = 0;
2571 timeout.tv_nsec = 1;
2572 nanosleep(&timeout, NULL);
2573 done:
2574 if (new_index)
2575 fclose(new_index);
2576 free(new_fileindex_path);
2577 return err;
2580 static const struct got_error *
2581 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2582 struct got_object_id **tree_id, const char *wt_relpath,
2583 struct got_worktree *worktree, struct got_repository *repo)
2585 const struct got_error *err = NULL;
2586 struct got_object_id *id = NULL;
2587 char *in_repo_path = NULL;
2588 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2590 *entry_type = GOT_OBJ_TYPE_ANY;
2591 *tree_relpath = NULL;
2592 *tree_id = NULL;
2594 if (wt_relpath[0] == '\0') {
2595 /* Check out all files within the work tree. */
2596 *entry_type = GOT_OBJ_TYPE_TREE;
2597 *tree_relpath = strdup("");
2598 if (*tree_relpath == NULL) {
2599 err = got_error_from_errno("strdup");
2600 goto done;
2602 err = got_object_id_by_path(tree_id, repo,
2603 worktree->base_commit_id, worktree->path_prefix);
2604 if (err)
2605 goto done;
2606 return NULL;
2609 /* Check out a subset of files in the work tree. */
2611 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2612 is_root_wt ? "" : "/", wt_relpath) == -1) {
2613 err = got_error_from_errno("asprintf");
2614 goto done;
2617 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2618 in_repo_path);
2619 if (err)
2620 goto done;
2622 free(in_repo_path);
2623 in_repo_path = NULL;
2625 err = got_object_get_type(entry_type, repo, id);
2626 if (err)
2627 goto done;
2629 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2630 /* Check out a single file. */
2631 if (strchr(wt_relpath, '/') == NULL) {
2632 /* Check out a single file in work tree's root dir. */
2633 in_repo_path = strdup(worktree->path_prefix);
2634 if (in_repo_path == NULL) {
2635 err = got_error_from_errno("strdup");
2636 goto done;
2638 *tree_relpath = strdup("");
2639 if (*tree_relpath == NULL) {
2640 err = got_error_from_errno("strdup");
2641 goto done;
2643 } else {
2644 /* Check out a single file in a subdirectory. */
2645 err = got_path_dirname(tree_relpath, wt_relpath);
2646 if (err)
2647 return err;
2648 if (asprintf(&in_repo_path, "%s%s%s",
2649 worktree->path_prefix, is_root_wt ? "" : "/",
2650 *tree_relpath) == -1) {
2651 err = got_error_from_errno("asprintf");
2652 goto done;
2655 err = got_object_id_by_path(tree_id, repo,
2656 worktree->base_commit_id, in_repo_path);
2657 } else {
2658 /* Check out all files within a subdirectory. */
2659 *tree_id = got_object_id_dup(id);
2660 if (*tree_id == NULL) {
2661 err = got_error_from_errno("got_object_id_dup");
2662 goto done;
2664 *tree_relpath = strdup(wt_relpath);
2665 if (*tree_relpath == NULL) {
2666 err = got_error_from_errno("strdup");
2667 goto done;
2670 done:
2671 free(id);
2672 free(in_repo_path);
2673 if (err) {
2674 *entry_type = GOT_OBJ_TYPE_ANY;
2675 free(*tree_relpath);
2676 *tree_relpath = NULL;
2677 free(*tree_id);
2678 *tree_id = NULL;
2680 return err;
2683 static const struct got_error *
2684 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2685 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2686 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2687 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2689 const struct got_error *err = NULL;
2690 struct got_commit_object *commit = NULL;
2691 struct got_tree_object *tree = NULL;
2692 struct got_fileindex_diff_tree_cb diff_cb;
2693 struct diff_cb_arg arg;
2695 err = ref_base_commit(worktree, repo);
2696 if (err) {
2697 if (!(err->code == GOT_ERR_ERRNO &&
2698 (errno == EACCES || errno == EROFS)))
2699 goto done;
2700 err = (*progress_cb)(progress_arg,
2701 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2702 if (err)
2703 return err;
2706 err = got_object_open_as_commit(&commit, repo,
2707 worktree->base_commit_id);
2708 if (err)
2709 goto done;
2711 err = got_object_open_as_tree(&tree, repo, tree_id);
2712 if (err)
2713 goto done;
2715 if (entry_name &&
2716 got_object_tree_find_entry(tree, entry_name) == NULL) {
2717 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2718 goto done;
2721 diff_cb.diff_old_new = diff_old_new;
2722 diff_cb.diff_old = diff_old;
2723 diff_cb.diff_new = diff_new;
2724 arg.fileindex = fileindex;
2725 arg.worktree = worktree;
2726 arg.repo = repo;
2727 arg.progress_cb = progress_cb;
2728 arg.progress_arg = progress_arg;
2729 arg.cancel_cb = cancel_cb;
2730 arg.cancel_arg = cancel_arg;
2731 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2732 entry_name, repo, &diff_cb, &arg);
2733 done:
2734 if (tree)
2735 got_object_tree_close(tree);
2736 if (commit)
2737 got_object_commit_close(commit);
2738 return err;
2741 const struct got_error *
2742 got_worktree_checkout_files(struct got_worktree *worktree,
2743 struct got_pathlist_head *paths, struct got_repository *repo,
2744 got_worktree_checkout_cb progress_cb, void *progress_arg,
2745 got_cancel_cb cancel_cb, void *cancel_arg)
2747 const struct got_error *err = NULL, *sync_err, *unlockerr;
2748 struct got_commit_object *commit = NULL;
2749 struct got_tree_object *tree = NULL;
2750 struct got_fileindex *fileindex = NULL;
2751 char *fileindex_path = NULL;
2752 struct got_pathlist_entry *pe;
2753 struct tree_path_data {
2754 STAILQ_ENTRY(tree_path_data) entry;
2755 struct got_object_id *tree_id;
2756 int entry_type;
2757 char *relpath;
2758 char *entry_name;
2759 } *tpd = NULL;
2760 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2762 STAILQ_INIT(&tree_paths);
2764 err = lock_worktree(worktree, LOCK_EX);
2765 if (err)
2766 return err;
2768 /* Map all specified paths to in-repository trees. */
2769 TAILQ_FOREACH(pe, paths, entry) {
2770 tpd = malloc(sizeof(*tpd));
2771 if (tpd == NULL) {
2772 err = got_error_from_errno("malloc");
2773 goto done;
2776 err = find_tree_entry_for_checkout(&tpd->entry_type,
2777 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2778 if (err) {
2779 free(tpd);
2780 goto done;
2783 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2784 err = got_path_basename(&tpd->entry_name, pe->path);
2785 if (err) {
2786 free(tpd->relpath);
2787 free(tpd->tree_id);
2788 free(tpd);
2789 goto done;
2791 } else
2792 tpd->entry_name = NULL;
2794 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2798 * Read the file index.
2799 * Checking out files is supposed to be an idempotent operation.
2800 * If the on-disk file index is incomplete we will try to complete it.
2802 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2803 if (err)
2804 goto done;
2806 tpd = STAILQ_FIRST(&tree_paths);
2807 TAILQ_FOREACH(pe, paths, entry) {
2808 struct bump_base_commit_id_arg bbc_arg;
2810 err = checkout_files(worktree, fileindex, tpd->relpath,
2811 tpd->tree_id, tpd->entry_name, repo,
2812 progress_cb, progress_arg, cancel_cb, cancel_arg);
2813 if (err)
2814 break;
2816 bbc_arg.base_commit_id = worktree->base_commit_id;
2817 bbc_arg.entry_name = tpd->entry_name;
2818 bbc_arg.path = pe->path;
2819 bbc_arg.path_len = pe->path_len;
2820 bbc_arg.progress_cb = progress_cb;
2821 bbc_arg.progress_arg = progress_arg;
2822 err = got_fileindex_for_each_entry_safe(fileindex,
2823 bump_base_commit_id, &bbc_arg);
2824 if (err)
2825 break;
2827 tpd = STAILQ_NEXT(tpd, entry);
2829 sync_err = sync_fileindex(fileindex, fileindex_path);
2830 if (sync_err && err == NULL)
2831 err = sync_err;
2832 done:
2833 free(fileindex_path);
2834 if (tree)
2835 got_object_tree_close(tree);
2836 if (commit)
2837 got_object_commit_close(commit);
2838 if (fileindex)
2839 got_fileindex_free(fileindex);
2840 while (!STAILQ_EMPTY(&tree_paths)) {
2841 tpd = STAILQ_FIRST(&tree_paths);
2842 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2843 free(tpd->relpath);
2844 free(tpd->tree_id);
2845 free(tpd);
2847 unlockerr = lock_worktree(worktree, LOCK_SH);
2848 if (unlockerr && err == NULL)
2849 err = unlockerr;
2850 return err;
2853 struct merge_file_cb_arg {
2854 struct got_worktree *worktree;
2855 struct got_fileindex *fileindex;
2856 got_worktree_checkout_cb progress_cb;
2857 void *progress_arg;
2858 got_cancel_cb cancel_cb;
2859 void *cancel_arg;
2860 const char *label_orig;
2861 struct got_object_id *commit_id2;
2864 static const struct got_error *
2865 merge_file_cb(void *arg, struct got_blob_object *blob1,
2866 struct got_blob_object *blob2, struct got_object_id *id1,
2867 struct got_object_id *id2, const char *path1, const char *path2,
2868 mode_t mode1, mode_t mode2, struct got_repository *repo)
2870 static const struct got_error *err = NULL;
2871 struct merge_file_cb_arg *a = arg;
2872 struct got_fileindex_entry *ie;
2873 char *ondisk_path = NULL;
2874 struct stat sb;
2875 unsigned char status;
2876 int local_changes_subsumed;
2877 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2878 char *id_str = NULL, *label_deriv2 = NULL;
2880 if (blob1 && blob2) {
2881 ie = got_fileindex_entry_get(a->fileindex, path2,
2882 strlen(path2));
2883 if (ie == NULL)
2884 return (*a->progress_cb)(a->progress_arg,
2885 GOT_STATUS_MISSING, path2);
2887 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2888 path2) == -1)
2889 return got_error_from_errno("asprintf");
2891 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2892 repo);
2893 if (err)
2894 goto done;
2896 if (status == GOT_STATUS_DELETE) {
2897 err = (*a->progress_cb)(a->progress_arg,
2898 GOT_STATUS_MERGE, path2);
2899 goto done;
2901 if (status != GOT_STATUS_NO_CHANGE &&
2902 status != GOT_STATUS_MODIFY &&
2903 status != GOT_STATUS_CONFLICT &&
2904 status != GOT_STATUS_ADD) {
2905 err = (*a->progress_cb)(a->progress_arg, status, path2);
2906 goto done;
2909 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2910 char *link_target2;
2911 err = got_object_blob_read_to_str(&link_target2, blob2);
2912 if (err)
2913 goto done;
2914 err = merge_symlink(a->worktree, blob1, ondisk_path,
2915 path2, a->label_orig, link_target2, a->commit_id2,
2916 repo, a->progress_cb, a->progress_arg);
2917 free(link_target2);
2918 } else {
2919 int fd;
2921 f_orig = got_opentemp();
2922 if (f_orig == NULL) {
2923 err = got_error_from_errno("got_opentemp");
2924 goto done;
2926 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2927 f_orig, blob1);
2928 if (err)
2929 goto done;
2931 f_deriv2 = got_opentemp();
2932 if (f_deriv2 == NULL)
2933 goto done;
2934 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2935 f_deriv2, blob2);
2936 if (err)
2937 goto done;
2939 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2940 if (fd == -1) {
2941 err = got_error_from_errno2("open",
2942 ondisk_path);
2943 goto done;
2945 f_deriv = fdopen(fd, "r");
2946 if (f_deriv == NULL) {
2947 err = got_error_from_errno2("fdopen",
2948 ondisk_path);
2949 close(fd);
2950 goto done;
2952 err = got_object_id_str(&id_str, a->commit_id2);
2953 if (err)
2954 goto done;
2955 if (asprintf(&label_deriv2, "%s: commit %s",
2956 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2957 err = got_error_from_errno("asprintf");
2958 goto done;
2960 err = merge_file(&local_changes_subsumed, a->worktree,
2961 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2962 sb.st_mode, a->label_orig, NULL, label_deriv2,
2963 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2964 a->progress_cb, a->progress_arg);
2966 } else if (blob1) {
2967 ie = got_fileindex_entry_get(a->fileindex, path1,
2968 strlen(path1));
2969 if (ie == NULL)
2970 return (*a->progress_cb)(a->progress_arg,
2971 GOT_STATUS_MISSING, path1);
2973 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2974 path1) == -1)
2975 return got_error_from_errno("asprintf");
2977 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2978 repo);
2979 if (err)
2980 goto done;
2982 switch (status) {
2983 case GOT_STATUS_NO_CHANGE:
2984 err = (*a->progress_cb)(a->progress_arg,
2985 GOT_STATUS_DELETE, path1);
2986 if (err)
2987 goto done;
2988 err = remove_ondisk_file(a->worktree->root_path, path1);
2989 if (err)
2990 goto done;
2991 if (ie)
2992 got_fileindex_entry_mark_deleted_from_disk(ie);
2993 break;
2994 case GOT_STATUS_DELETE:
2995 case GOT_STATUS_MISSING:
2996 err = (*a->progress_cb)(a->progress_arg,
2997 GOT_STATUS_DELETE, path1);
2998 if (err)
2999 goto done;
3000 if (ie)
3001 got_fileindex_entry_mark_deleted_from_disk(ie);
3002 break;
3003 case GOT_STATUS_ADD: {
3004 struct got_object_id *id;
3005 FILE *blob1_f;
3007 * Delete the added file only if its content already
3008 * exists in the repository.
3010 err = got_object_blob_file_create(&id, &blob1_f, path1);
3011 if (err)
3012 goto done;
3013 if (got_object_id_cmp(id, id1) == 0) {
3014 err = (*a->progress_cb)(a->progress_arg,
3015 GOT_STATUS_DELETE, path1);
3016 if (err)
3017 goto done;
3018 err = remove_ondisk_file(a->worktree->root_path,
3019 path1);
3020 if (err)
3021 goto done;
3022 if (ie)
3023 got_fileindex_entry_remove(a->fileindex,
3024 ie);
3025 } else {
3026 err = (*a->progress_cb)(a->progress_arg,
3027 GOT_STATUS_CANNOT_DELETE, path1);
3029 if (fclose(blob1_f) == EOF && err == NULL)
3030 err = got_error_from_errno("fclose");
3031 free(id);
3032 if (err)
3033 goto done;
3034 break;
3036 case GOT_STATUS_MODIFY:
3037 case GOT_STATUS_CONFLICT:
3038 err = (*a->progress_cb)(a->progress_arg,
3039 GOT_STATUS_CANNOT_DELETE, path1);
3040 if (err)
3041 goto done;
3042 break;
3043 case GOT_STATUS_OBSTRUCTED:
3044 err = (*a->progress_cb)(a->progress_arg, status, path1);
3045 if (err)
3046 goto done;
3047 break;
3048 default:
3049 break;
3051 } else if (blob2) {
3052 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3053 path2) == -1)
3054 return got_error_from_errno("asprintf");
3055 ie = got_fileindex_entry_get(a->fileindex, path2,
3056 strlen(path2));
3057 if (ie) {
3058 err = get_file_status(&status, &sb, ie, ondisk_path,
3059 -1, NULL, repo);
3060 if (err)
3061 goto done;
3062 if (status != GOT_STATUS_NO_CHANGE &&
3063 status != GOT_STATUS_MODIFY &&
3064 status != GOT_STATUS_CONFLICT &&
3065 status != GOT_STATUS_ADD) {
3066 err = (*a->progress_cb)(a->progress_arg,
3067 status, path2);
3068 goto done;
3070 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3071 char *link_target2;
3072 err = got_object_blob_read_to_str(&link_target2,
3073 blob2);
3074 if (err)
3075 goto done;
3076 err = merge_symlink(a->worktree, NULL,
3077 ondisk_path, path2, a->label_orig,
3078 link_target2, a->commit_id2, repo,
3079 a->progress_cb, a->progress_arg);
3080 free(link_target2);
3081 } else if (S_ISREG(sb.st_mode)) {
3082 err = merge_blob(&local_changes_subsumed,
3083 a->worktree, NULL, ondisk_path, path2,
3084 sb.st_mode, a->label_orig, blob2,
3085 a->commit_id2, repo, a->progress_cb,
3086 a->progress_arg);
3087 } else {
3088 err = got_error_path(ondisk_path,
3089 GOT_ERR_FILE_OBSTRUCTED);
3091 if (err)
3092 goto done;
3093 if (status == GOT_STATUS_DELETE) {
3094 err = got_fileindex_entry_update(ie,
3095 a->worktree->root_fd, path2, blob2->id.sha1,
3096 a->worktree->base_commit_id->sha1, 0);
3097 if (err)
3098 goto done;
3100 } else {
3101 int is_bad_symlink = 0;
3102 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3103 if (S_ISLNK(mode2)) {
3104 err = install_symlink(&is_bad_symlink,
3105 a->worktree, ondisk_path, path2, blob2, 0,
3106 0, 1, repo, a->progress_cb, a->progress_arg);
3107 } else {
3108 err = install_blob(a->worktree, ondisk_path, path2,
3109 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3110 a->progress_cb, a->progress_arg);
3112 if (err)
3113 goto done;
3114 err = got_fileindex_entry_alloc(&ie, path2);
3115 if (err)
3116 goto done;
3117 err = got_fileindex_entry_update(ie,
3118 a->worktree->root_fd, path2, NULL, NULL, 1);
3119 if (err) {
3120 got_fileindex_entry_free(ie);
3121 goto done;
3123 err = got_fileindex_entry_add(a->fileindex, ie);
3124 if (err) {
3125 got_fileindex_entry_free(ie);
3126 goto done;
3128 if (is_bad_symlink) {
3129 got_fileindex_entry_filetype_set(ie,
3130 GOT_FILEIDX_MODE_BAD_SYMLINK);
3134 done:
3135 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3136 err = got_error_from_errno("fclose");
3137 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3138 err = got_error_from_errno("fclose");
3139 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3140 err = got_error_from_errno("fclose");
3141 free(id_str);
3142 free(label_deriv2);
3143 free(ondisk_path);
3144 return err;
3147 static const struct got_error *
3148 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3150 struct got_worktree *worktree = arg;
3152 /* Reject merges into a work tree with mixed base commits. */
3153 if (got_fileindex_entry_has_commit(ie) &&
3154 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3155 SHA1_DIGEST_LENGTH) != 0)
3156 return got_error(GOT_ERR_MIXED_COMMITS);
3158 return NULL;
3161 struct check_merge_conflicts_arg {
3162 struct got_worktree *worktree;
3163 struct got_fileindex *fileindex;
3164 struct got_repository *repo;
3167 static const struct got_error *
3168 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3169 struct got_blob_object *blob2, struct got_object_id *id1,
3170 struct got_object_id *id2, const char *path1, const char *path2,
3171 mode_t mode1, mode_t mode2, struct got_repository *repo)
3173 const struct got_error *err = NULL;
3174 struct check_merge_conflicts_arg *a = arg;
3175 unsigned char status;
3176 struct stat sb;
3177 struct got_fileindex_entry *ie;
3178 const char *path = path2 ? path2 : path1;
3179 struct got_object_id *id = id2 ? id2 : id1;
3180 char *ondisk_path;
3182 if (id == NULL)
3183 return NULL;
3185 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3186 if (ie == NULL)
3187 return NULL;
3189 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3190 == -1)
3191 return got_error_from_errno("asprintf");
3193 /* Reject merges into a work tree with conflicted files. */
3194 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3195 free(ondisk_path);
3196 if (err)
3197 return err;
3198 if (status == GOT_STATUS_CONFLICT)
3199 return got_error(GOT_ERR_CONFLICTS);
3201 return NULL;
3204 static const struct got_error *
3205 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3206 const char *fileindex_path, struct got_object_id *commit_id1,
3207 struct got_object_id *commit_id2, struct got_repository *repo,
3208 got_worktree_checkout_cb progress_cb, void *progress_arg,
3209 got_cancel_cb cancel_cb, void *cancel_arg)
3211 const struct got_error *err = NULL, *sync_err;
3212 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3213 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3214 struct check_merge_conflicts_arg cmc_arg;
3215 struct merge_file_cb_arg arg;
3216 char *label_orig = NULL;
3218 if (commit_id1) {
3219 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3220 worktree->path_prefix);
3221 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3222 goto done;
3224 if (tree_id1) {
3225 char *id_str;
3227 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3228 if (err)
3229 goto done;
3231 err = got_object_id_str(&id_str, commit_id1);
3232 if (err)
3233 goto done;
3235 if (asprintf(&label_orig, "%s: commit %s",
3236 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3237 err = got_error_from_errno("asprintf");
3238 free(id_str);
3239 goto done;
3241 free(id_str);
3244 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3245 worktree->path_prefix);
3246 if (err)
3247 goto done;
3249 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3250 if (err)
3251 goto done;
3253 cmc_arg.worktree = worktree;
3254 cmc_arg.fileindex = fileindex;
3255 cmc_arg.repo = repo;
3256 err = got_diff_tree(tree1, tree2, "", "", repo,
3257 check_merge_conflicts, &cmc_arg, 0);
3258 if (err)
3259 goto done;
3261 arg.worktree = worktree;
3262 arg.fileindex = fileindex;
3263 arg.progress_cb = progress_cb;
3264 arg.progress_arg = progress_arg;
3265 arg.cancel_cb = cancel_cb;
3266 arg.cancel_arg = cancel_arg;
3267 arg.label_orig = label_orig;
3268 arg.commit_id2 = commit_id2;
3269 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3270 sync_err = sync_fileindex(fileindex, fileindex_path);
3271 if (sync_err && err == NULL)
3272 err = sync_err;
3273 done:
3274 if (tree1)
3275 got_object_tree_close(tree1);
3276 if (tree2)
3277 got_object_tree_close(tree2);
3278 free(label_orig);
3279 return err;
3282 const struct got_error *
3283 got_worktree_merge_files(struct got_worktree *worktree,
3284 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3285 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3286 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3288 const struct got_error *err, *unlockerr;
3289 char *fileindex_path = NULL;
3290 struct got_fileindex *fileindex = NULL;
3292 err = lock_worktree(worktree, LOCK_EX);
3293 if (err)
3294 return err;
3296 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3297 if (err)
3298 goto done;
3300 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3301 worktree);
3302 if (err)
3303 goto done;
3305 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3306 commit_id2, repo, progress_cb, progress_arg,
3307 cancel_cb, cancel_arg);
3308 done:
3309 if (fileindex)
3310 got_fileindex_free(fileindex);
3311 free(fileindex_path);
3312 unlockerr = lock_worktree(worktree, LOCK_SH);
3313 if (unlockerr && err == NULL)
3314 err = unlockerr;
3315 return err;
3318 struct diff_dir_cb_arg {
3319 struct got_fileindex *fileindex;
3320 struct got_worktree *worktree;
3321 const char *status_path;
3322 size_t status_path_len;
3323 struct got_repository *repo;
3324 got_worktree_status_cb status_cb;
3325 void *status_arg;
3326 got_cancel_cb cancel_cb;
3327 void *cancel_arg;
3328 /* A pathlist containing per-directory pathlists of ignore patterns. */
3329 struct got_pathlist_head *ignores;
3330 int report_unchanged;
3331 int no_ignores;
3334 static const struct got_error *
3335 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3336 int dirfd, const char *de_name,
3337 got_worktree_status_cb status_cb, void *status_arg,
3338 struct got_repository *repo, int report_unchanged)
3340 const struct got_error *err = NULL;
3341 unsigned char status = GOT_STATUS_NO_CHANGE;
3342 unsigned char staged_status = get_staged_status(ie);
3343 struct stat sb;
3344 struct got_object_id blob_id, commit_id, staged_blob_id;
3345 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3346 struct got_object_id *staged_blob_idp = NULL;
3348 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3349 if (err)
3350 return err;
3352 if (status == GOT_STATUS_NO_CHANGE &&
3353 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3354 return NULL;
3356 if (got_fileindex_entry_has_blob(ie)) {
3357 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3358 blob_idp = &blob_id;
3360 if (got_fileindex_entry_has_commit(ie)) {
3361 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3362 commit_idp = &commit_id;
3364 if (staged_status == GOT_STATUS_ADD ||
3365 staged_status == GOT_STATUS_MODIFY) {
3366 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3367 SHA1_DIGEST_LENGTH);
3368 staged_blob_idp = &staged_blob_id;
3371 return (*status_cb)(status_arg, status, staged_status,
3372 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3375 static const struct got_error *
3376 status_old_new(void *arg, struct got_fileindex_entry *ie,
3377 struct dirent *de, const char *parent_path, int dirfd)
3379 const struct got_error *err = NULL;
3380 struct diff_dir_cb_arg *a = arg;
3381 char *abspath;
3383 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3384 return got_error(GOT_ERR_CANCELLED);
3386 if (got_path_cmp(parent_path, a->status_path,
3387 strlen(parent_path), a->status_path_len) != 0 &&
3388 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3389 return NULL;
3391 if (parent_path[0]) {
3392 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3393 parent_path, de->d_name) == -1)
3394 return got_error_from_errno("asprintf");
3395 } else {
3396 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3397 de->d_name) == -1)
3398 return got_error_from_errno("asprintf");
3401 err = report_file_status(ie, abspath, dirfd, de->d_name,
3402 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3403 free(abspath);
3404 return err;
3407 static const struct got_error *
3408 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3410 struct diff_dir_cb_arg *a = arg;
3411 struct got_object_id blob_id, commit_id;
3412 unsigned char status;
3414 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3415 return got_error(GOT_ERR_CANCELLED);
3417 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3418 return NULL;
3420 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3421 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3422 if (got_fileindex_entry_has_file_on_disk(ie))
3423 status = GOT_STATUS_MISSING;
3424 else
3425 status = GOT_STATUS_DELETE;
3426 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3427 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3430 void
3431 free_ignorelist(struct got_pathlist_head *ignorelist)
3433 struct got_pathlist_entry *pe;
3435 TAILQ_FOREACH(pe, ignorelist, entry)
3436 free((char *)pe->path);
3437 got_pathlist_free(ignorelist);
3440 void
3441 free_ignores(struct got_pathlist_head *ignores)
3443 struct got_pathlist_entry *pe;
3445 TAILQ_FOREACH(pe, ignores, entry) {
3446 struct got_pathlist_head *ignorelist = pe->data;
3447 free_ignorelist(ignorelist);
3448 free((char *)pe->path);
3450 got_pathlist_free(ignores);
3453 static const struct got_error *
3454 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3456 const struct got_error *err = NULL;
3457 struct got_pathlist_entry *pe = NULL;
3458 struct got_pathlist_head *ignorelist;
3459 char *line = NULL, *pattern, *dirpath = NULL;
3460 size_t linesize = 0;
3461 ssize_t linelen;
3463 ignorelist = calloc(1, sizeof(*ignorelist));
3464 if (ignorelist == NULL)
3465 return got_error_from_errno("calloc");
3466 TAILQ_INIT(ignorelist);
3468 while ((linelen = getline(&line, &linesize, f)) != -1) {
3469 if (linelen > 0 && line[linelen - 1] == '\n')
3470 line[linelen - 1] = '\0';
3472 /* Git's ignores may contain comments. */
3473 if (line[0] == '#')
3474 continue;
3476 /* Git's negated patterns are not (yet?) supported. */
3477 if (line[0] == '!')
3478 continue;
3480 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3481 line) == -1) {
3482 err = got_error_from_errno("asprintf");
3483 goto done;
3485 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3486 if (err)
3487 goto done;
3489 if (ferror(f)) {
3490 err = got_error_from_errno("getline");
3491 goto done;
3494 dirpath = strdup(path);
3495 if (dirpath == NULL) {
3496 err = got_error_from_errno("strdup");
3497 goto done;
3499 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3500 done:
3501 free(line);
3502 if (err || pe == NULL) {
3503 free(dirpath);
3504 free_ignorelist(ignorelist);
3506 return err;
3509 int
3510 match_ignores(struct got_pathlist_head *ignores, const char *path)
3512 struct got_pathlist_entry *pe;
3514 /* Handle patterns which match in all directories. */
3515 TAILQ_FOREACH(pe, ignores, entry) {
3516 struct got_pathlist_head *ignorelist = pe->data;
3517 struct got_pathlist_entry *pi;
3519 TAILQ_FOREACH(pi, ignorelist, entry) {
3520 const char *p, *pattern = pi->path;
3522 if (strncmp(pattern, "**/", 3) != 0)
3523 continue;
3524 pattern += 3;
3525 p = path;
3526 while (*p) {
3527 if (fnmatch(pattern, p,
3528 FNM_PATHNAME | FNM_LEADING_DIR)) {
3529 /* Retry in next directory. */
3530 while (*p && *p != '/')
3531 p++;
3532 while (*p == '/')
3533 p++;
3534 continue;
3536 return 1;
3542 * The ignores pathlist contains ignore lists from children before
3543 * parents, so we can find the most specific ignorelist by walking
3544 * ignores backwards.
3546 pe = TAILQ_LAST(ignores, got_pathlist_head);
3547 while (pe) {
3548 if (got_path_is_child(path, pe->path, pe->path_len)) {
3549 struct got_pathlist_head *ignorelist = pe->data;
3550 struct got_pathlist_entry *pi;
3551 TAILQ_FOREACH(pi, ignorelist, entry) {
3552 const char *pattern = pi->path;
3553 int flags = FNM_LEADING_DIR;
3554 if (strstr(pattern, "/**/") == NULL)
3555 flags |= FNM_PATHNAME;
3556 if (fnmatch(pattern, path, flags))
3557 continue;
3558 return 1;
3561 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3564 return 0;
3567 static const struct got_error *
3568 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3569 const char *path, int dirfd, const char *ignores_filename)
3571 const struct got_error *err = NULL;
3572 char *ignorespath;
3573 int fd = -1;
3574 FILE *ignoresfile = NULL;
3576 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3577 path[0] ? "/" : "", ignores_filename) == -1)
3578 return got_error_from_errno("asprintf");
3580 if (dirfd != -1) {
3581 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3582 if (fd == -1) {
3583 if (errno != ENOENT && errno != EACCES)
3584 err = got_error_from_errno2("openat",
3585 ignorespath);
3586 } else {
3587 ignoresfile = fdopen(fd, "r");
3588 if (ignoresfile == NULL)
3589 err = got_error_from_errno2("fdopen",
3590 ignorespath);
3591 else {
3592 fd = -1;
3593 err = read_ignores(ignores, path, ignoresfile);
3596 } else {
3597 ignoresfile = fopen(ignorespath, "r");
3598 if (ignoresfile == NULL) {
3599 if (errno != ENOENT && errno != EACCES)
3600 err = got_error_from_errno2("fopen",
3601 ignorespath);
3602 } else
3603 err = read_ignores(ignores, path, ignoresfile);
3606 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3607 err = got_error_from_errno2("fclose", path);
3608 if (fd != -1 && close(fd) == -1 && err == NULL)
3609 err = got_error_from_errno2("close", path);
3610 free(ignorespath);
3611 return err;
3614 static const struct got_error *
3615 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3617 const struct got_error *err = NULL;
3618 struct diff_dir_cb_arg *a = arg;
3619 char *path = NULL;
3621 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3622 return got_error(GOT_ERR_CANCELLED);
3624 if (parent_path[0]) {
3625 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3626 return got_error_from_errno("asprintf");
3627 } else {
3628 path = de->d_name;
3631 if (de->d_type != DT_DIR &&
3632 got_path_is_child(path, a->status_path, a->status_path_len)
3633 && !match_ignores(a->ignores, path))
3634 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3635 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3636 if (parent_path[0])
3637 free(path);
3638 return err;
3641 static const struct got_error *
3642 status_traverse(void *arg, const char *path, int dirfd)
3644 const struct got_error *err = NULL;
3645 struct diff_dir_cb_arg *a = arg;
3647 if (a->no_ignores)
3648 return NULL;
3650 err = add_ignores(a->ignores, a->worktree->root_path,
3651 path, dirfd, ".cvsignore");
3652 if (err)
3653 return err;
3655 err = add_ignores(a->ignores, a->worktree->root_path, path,
3656 dirfd, ".gitignore");
3658 return err;
3661 static const struct got_error *
3662 report_single_file_status(const char *path, const char *ondisk_path,
3663 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3664 void *status_arg, struct got_repository *repo, int report_unchanged,
3665 struct got_pathlist_head *ignores, int no_ignores)
3667 struct got_fileindex_entry *ie;
3668 struct stat sb;
3670 if (!no_ignores && match_ignores(ignores, path))
3671 return NULL;
3673 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3674 if (ie)
3675 return report_file_status(ie, ondisk_path, -1, NULL,
3676 status_cb, status_arg, repo, report_unchanged);
3678 if (lstat(ondisk_path, &sb) == -1) {
3679 if (errno != ENOENT)
3680 return got_error_from_errno2("lstat", ondisk_path);
3681 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3682 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3683 return NULL;
3686 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3687 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3688 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3690 return NULL;
3693 static const struct got_error *
3694 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3695 const char *root_path, const char *path)
3697 const struct got_error *err;
3698 char *parent_path, *next_parent_path = NULL;
3700 err = add_ignores(ignores, root_path, "", -1,
3701 ".cvsignore");
3702 if (err)
3703 return err;
3705 err = add_ignores(ignores, root_path, "", -1,
3706 ".gitignore");
3707 if (err)
3708 return err;
3710 err = got_path_dirname(&parent_path, path);
3711 if (err) {
3712 if (err->code == GOT_ERR_BAD_PATH)
3713 return NULL; /* cannot traverse parent */
3714 return err;
3716 for (;;) {
3717 err = add_ignores(ignores, root_path, parent_path, -1,
3718 ".cvsignore");
3719 if (err)
3720 break;
3721 err = add_ignores(ignores, root_path, parent_path, -1,
3722 ".gitignore");
3723 if (err)
3724 break;
3725 err = got_path_dirname(&next_parent_path, parent_path);
3726 if (err) {
3727 if (err->code == GOT_ERR_BAD_PATH)
3728 err = NULL; /* traversed everything */
3729 break;
3731 if (got_path_is_root_dir(parent_path))
3732 break;
3733 free(parent_path);
3734 parent_path = next_parent_path;
3735 next_parent_path = NULL;
3738 free(parent_path);
3739 free(next_parent_path);
3740 return err;
3743 static const struct got_error *
3744 worktree_status(struct got_worktree *worktree, const char *path,
3745 struct got_fileindex *fileindex, struct got_repository *repo,
3746 got_worktree_status_cb status_cb, void *status_arg,
3747 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3748 int report_unchanged)
3750 const struct got_error *err = NULL;
3751 int fd = -1;
3752 struct got_fileindex_diff_dir_cb fdiff_cb;
3753 struct diff_dir_cb_arg arg;
3754 char *ondisk_path = NULL;
3755 struct got_pathlist_head ignores;
3757 TAILQ_INIT(&ignores);
3759 if (asprintf(&ondisk_path, "%s%s%s",
3760 worktree->root_path, path[0] ? "/" : "", path) == -1)
3761 return got_error_from_errno("asprintf");
3763 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3764 if (fd == -1) {
3765 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3766 errno != ELOOP)
3767 err = got_error_from_errno2("open", ondisk_path);
3768 else {
3769 if (!no_ignores) {
3770 err = add_ignores_from_parent_paths(&ignores,
3771 worktree->root_path, ondisk_path);
3772 if (err)
3773 goto done;
3775 err = report_single_file_status(path, ondisk_path,
3776 fileindex, status_cb, status_arg, repo,
3777 report_unchanged, &ignores, no_ignores);
3779 } else {
3780 fdiff_cb.diff_old_new = status_old_new;
3781 fdiff_cb.diff_old = status_old;
3782 fdiff_cb.diff_new = status_new;
3783 fdiff_cb.diff_traverse = status_traverse;
3784 arg.fileindex = fileindex;
3785 arg.worktree = worktree;
3786 arg.status_path = path;
3787 arg.status_path_len = strlen(path);
3788 arg.repo = repo;
3789 arg.status_cb = status_cb;
3790 arg.status_arg = status_arg;
3791 arg.cancel_cb = cancel_cb;
3792 arg.cancel_arg = cancel_arg;
3793 arg.report_unchanged = report_unchanged;
3794 arg.no_ignores = no_ignores;
3795 if (!no_ignores) {
3796 err = add_ignores_from_parent_paths(&ignores,
3797 worktree->root_path, path);
3798 if (err)
3799 goto done;
3801 arg.ignores = &ignores;
3802 err = got_fileindex_diff_dir(fileindex, fd,
3803 worktree->root_path, path, repo, &fdiff_cb, &arg);
3805 done:
3806 free_ignores(&ignores);
3807 if (fd != -1 && close(fd) == -1 && err == NULL)
3808 err = got_error_from_errno("close");
3809 free(ondisk_path);
3810 return err;
3813 const struct got_error *
3814 got_worktree_status(struct got_worktree *worktree,
3815 struct got_pathlist_head *paths, struct got_repository *repo,
3816 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3817 got_cancel_cb cancel_cb, void *cancel_arg)
3819 const struct got_error *err = NULL;
3820 char *fileindex_path = NULL;
3821 struct got_fileindex *fileindex = NULL;
3822 struct got_pathlist_entry *pe;
3824 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3825 if (err)
3826 return err;
3828 TAILQ_FOREACH(pe, paths, entry) {
3829 err = worktree_status(worktree, pe->path, fileindex, repo,
3830 status_cb, status_arg, cancel_cb, cancel_arg,
3831 no_ignores, 0);
3832 if (err)
3833 break;
3835 free(fileindex_path);
3836 got_fileindex_free(fileindex);
3837 return err;
3840 const struct got_error *
3841 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3842 const char *arg)
3844 const struct got_error *err = NULL;
3845 char *resolved = NULL, *cwd = NULL, *path = NULL;
3846 size_t len;
3847 struct stat sb;
3848 char *abspath = NULL;
3849 char canonpath[PATH_MAX];
3851 *wt_path = NULL;
3853 cwd = getcwd(NULL, 0);
3854 if (cwd == NULL)
3855 return got_error_from_errno("getcwd");
3857 if (lstat(arg, &sb) == -1) {
3858 if (errno != ENOENT) {
3859 err = got_error_from_errno2("lstat", arg);
3860 goto done;
3862 sb.st_mode = 0;
3864 if (S_ISLNK(sb.st_mode)) {
3866 * We cannot use realpath(3) with symlinks since we want to
3867 * operate on the symlink itself.
3868 * But we can make the path absolute, assuming it is relative
3869 * to the current working directory, and then canonicalize it.
3871 if (!got_path_is_absolute(arg)) {
3872 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3873 err = got_error_from_errno("asprintf");
3874 goto done;
3878 err = got_canonpath(abspath ? abspath : arg, canonpath,
3879 sizeof(canonpath));
3880 if (err)
3881 goto done;
3882 resolved = strdup(canonpath);
3883 if (resolved == NULL) {
3884 err = got_error_from_errno("strdup");
3885 goto done;
3887 } else {
3888 resolved = realpath(arg, NULL);
3889 if (resolved == NULL) {
3890 if (errno != ENOENT) {
3891 err = got_error_from_errno2("realpath", arg);
3892 goto done;
3894 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3895 err = got_error_from_errno("asprintf");
3896 goto done;
3898 err = got_canonpath(abspath, canonpath,
3899 sizeof(canonpath));
3900 if (err)
3901 goto done;
3902 resolved = strdup(canonpath);
3903 if (resolved == NULL) {
3904 err = got_error_from_errno("strdup");
3905 goto done;
3910 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3911 strlen(got_worktree_get_root_path(worktree)))) {
3912 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3913 goto done;
3916 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3917 err = got_path_skip_common_ancestor(&path,
3918 got_worktree_get_root_path(worktree), resolved);
3919 if (err)
3920 goto done;
3921 } else {
3922 path = strdup("");
3923 if (path == NULL) {
3924 err = got_error_from_errno("strdup");
3925 goto done;
3929 /* XXX status walk can't deal with trailing slash! */
3930 len = strlen(path);
3931 while (len > 0 && path[len - 1] == '/') {
3932 path[len - 1] = '\0';
3933 len--;
3935 done:
3936 free(abspath);
3937 free(resolved);
3938 free(cwd);
3939 if (err == NULL)
3940 *wt_path = path;
3941 else
3942 free(path);
3943 return err;
3946 struct schedule_addition_args {
3947 struct got_worktree *worktree;
3948 struct got_fileindex *fileindex;
3949 got_worktree_checkout_cb progress_cb;
3950 void *progress_arg;
3951 struct got_repository *repo;
3954 static const struct got_error *
3955 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3956 const char *relpath, struct got_object_id *blob_id,
3957 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3958 int dirfd, const char *de_name)
3960 struct schedule_addition_args *a = arg;
3961 const struct got_error *err = NULL;
3962 struct got_fileindex_entry *ie;
3963 struct stat sb;
3964 char *ondisk_path;
3966 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3967 relpath) == -1)
3968 return got_error_from_errno("asprintf");
3970 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3971 if (ie) {
3972 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3973 de_name, a->repo);
3974 if (err)
3975 goto done;
3976 /* Re-adding an existing entry is a no-op. */
3977 if (status == GOT_STATUS_ADD)
3978 goto done;
3979 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3980 if (err)
3981 goto done;
3984 if (status != GOT_STATUS_UNVERSIONED) {
3985 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3986 goto done;
3989 err = got_fileindex_entry_alloc(&ie, relpath);
3990 if (err)
3991 goto done;
3992 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3993 relpath, NULL, NULL, 1);
3994 if (err) {
3995 got_fileindex_entry_free(ie);
3996 goto done;
3998 err = got_fileindex_entry_add(a->fileindex, ie);
3999 if (err) {
4000 got_fileindex_entry_free(ie);
4001 goto done;
4003 done:
4004 free(ondisk_path);
4005 if (err)
4006 return err;
4007 if (status == GOT_STATUS_ADD)
4008 return NULL;
4009 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4012 const struct got_error *
4013 got_worktree_schedule_add(struct got_worktree *worktree,
4014 struct got_pathlist_head *paths,
4015 got_worktree_checkout_cb progress_cb, void *progress_arg,
4016 struct got_repository *repo, int no_ignores)
4018 struct got_fileindex *fileindex = NULL;
4019 char *fileindex_path = NULL;
4020 const struct got_error *err = NULL, *sync_err, *unlockerr;
4021 struct got_pathlist_entry *pe;
4022 struct schedule_addition_args saa;
4024 err = lock_worktree(worktree, LOCK_EX);
4025 if (err)
4026 return err;
4028 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4029 if (err)
4030 goto done;
4032 saa.worktree = worktree;
4033 saa.fileindex = fileindex;
4034 saa.progress_cb = progress_cb;
4035 saa.progress_arg = progress_arg;
4036 saa.repo = repo;
4038 TAILQ_FOREACH(pe, paths, entry) {
4039 err = worktree_status(worktree, pe->path, fileindex, repo,
4040 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4041 if (err)
4042 break;
4044 sync_err = sync_fileindex(fileindex, fileindex_path);
4045 if (sync_err && err == NULL)
4046 err = sync_err;
4047 done:
4048 free(fileindex_path);
4049 if (fileindex)
4050 got_fileindex_free(fileindex);
4051 unlockerr = lock_worktree(worktree, LOCK_SH);
4052 if (unlockerr && err == NULL)
4053 err = unlockerr;
4054 return err;
4057 struct schedule_deletion_args {
4058 struct got_worktree *worktree;
4059 struct got_fileindex *fileindex;
4060 got_worktree_delete_cb progress_cb;
4061 void *progress_arg;
4062 struct got_repository *repo;
4063 int delete_local_mods;
4064 int keep_on_disk;
4065 const char *status_codes;
4068 static const struct got_error *
4069 schedule_for_deletion(void *arg, unsigned char status,
4070 unsigned char staged_status, const char *relpath,
4071 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4072 struct got_object_id *commit_id, int dirfd, const char *de_name)
4074 struct schedule_deletion_args *a = arg;
4075 const struct got_error *err = NULL;
4076 struct got_fileindex_entry *ie = NULL;
4077 struct stat sb;
4078 char *ondisk_path;
4080 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4081 if (ie == NULL)
4082 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4084 staged_status = get_staged_status(ie);
4085 if (staged_status != GOT_STATUS_NO_CHANGE) {
4086 if (staged_status == GOT_STATUS_DELETE)
4087 return NULL;
4088 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4091 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4092 relpath) == -1)
4093 return got_error_from_errno("asprintf");
4095 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4096 a->repo);
4097 if (err)
4098 goto done;
4100 if (a->status_codes) {
4101 size_t ncodes = strlen(a->status_codes);
4102 int i;
4103 for (i = 0; i < ncodes ; i++) {
4104 if (status == a->status_codes[i])
4105 break;
4107 if (i == ncodes) {
4108 /* Do not delete files in non-matching status. */
4109 free(ondisk_path);
4110 return NULL;
4112 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4113 a->status_codes[i] != GOT_STATUS_MISSING) {
4114 static char msg[64];
4115 snprintf(msg, sizeof(msg),
4116 "invalid status code '%c'", a->status_codes[i]);
4117 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4118 goto done;
4122 if (status != GOT_STATUS_NO_CHANGE) {
4123 if (status == GOT_STATUS_DELETE)
4124 goto done;
4125 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4126 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4127 goto done;
4129 if (status != GOT_STATUS_MODIFY &&
4130 status != GOT_STATUS_MISSING) {
4131 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4132 goto done;
4136 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4137 size_t root_len;
4139 if (dirfd != -1) {
4140 if (unlinkat(dirfd, de_name, 0) != 0) {
4141 err = got_error_from_errno2("unlinkat",
4142 ondisk_path);
4143 goto done;
4145 } else if (unlink(ondisk_path) != 0) {
4146 err = got_error_from_errno2("unlink", ondisk_path);
4147 goto done;
4150 root_len = strlen(a->worktree->root_path);
4151 do {
4152 char *parent;
4153 err = got_path_dirname(&parent, ondisk_path);
4154 if (err)
4155 goto done;
4156 free(ondisk_path);
4157 ondisk_path = parent;
4158 if (rmdir(ondisk_path) == -1) {
4159 if (errno != ENOTEMPTY)
4160 err = got_error_from_errno2("rmdir",
4161 ondisk_path);
4162 break;
4164 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4165 strlen(ondisk_path), root_len) != 0);
4168 got_fileindex_entry_mark_deleted_from_disk(ie);
4169 done:
4170 free(ondisk_path);
4171 if (err)
4172 return err;
4173 if (status == GOT_STATUS_DELETE)
4174 return NULL;
4175 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4176 staged_status, relpath);
4179 const struct got_error *
4180 got_worktree_schedule_delete(struct got_worktree *worktree,
4181 struct got_pathlist_head *paths, int delete_local_mods,
4182 const char *status_codes,
4183 got_worktree_delete_cb progress_cb, void *progress_arg,
4184 struct got_repository *repo, int keep_on_disk)
4186 struct got_fileindex *fileindex = NULL;
4187 char *fileindex_path = NULL;
4188 const struct got_error *err = NULL, *sync_err, *unlockerr;
4189 struct got_pathlist_entry *pe;
4190 struct schedule_deletion_args sda;
4192 err = lock_worktree(worktree, LOCK_EX);
4193 if (err)
4194 return err;
4196 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4197 if (err)
4198 goto done;
4200 sda.worktree = worktree;
4201 sda.fileindex = fileindex;
4202 sda.progress_cb = progress_cb;
4203 sda.progress_arg = progress_arg;
4204 sda.repo = repo;
4205 sda.delete_local_mods = delete_local_mods;
4206 sda.keep_on_disk = keep_on_disk;
4207 sda.status_codes = status_codes;
4209 TAILQ_FOREACH(pe, paths, entry) {
4210 err = worktree_status(worktree, pe->path, fileindex, repo,
4211 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4212 if (err)
4213 break;
4215 sync_err = sync_fileindex(fileindex, fileindex_path);
4216 if (sync_err && err == NULL)
4217 err = sync_err;
4218 done:
4219 free(fileindex_path);
4220 if (fileindex)
4221 got_fileindex_free(fileindex);
4222 unlockerr = lock_worktree(worktree, LOCK_SH);
4223 if (unlockerr && err == NULL)
4224 err = unlockerr;
4225 return err;
4228 static const struct got_error *
4229 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4231 const struct got_error *err = NULL;
4232 char *line = NULL;
4233 size_t linesize = 0, n;
4234 ssize_t linelen;
4236 linelen = getline(&line, &linesize, infile);
4237 if (linelen == -1) {
4238 if (ferror(infile)) {
4239 err = got_error_from_errno("getline");
4240 goto done;
4242 return NULL;
4244 if (outfile) {
4245 n = fwrite(line, 1, linelen, outfile);
4246 if (n != linelen) {
4247 err = got_ferror(outfile, GOT_ERR_IO);
4248 goto done;
4251 if (rejectfile) {
4252 n = fwrite(line, 1, linelen, rejectfile);
4253 if (n != linelen)
4254 err = got_ferror(outfile, GOT_ERR_IO);
4256 done:
4257 free(line);
4258 return err;
4261 static const struct got_error *
4262 skip_one_line(FILE *f)
4264 char *line = NULL;
4265 size_t linesize = 0;
4266 ssize_t linelen;
4268 linelen = getline(&line, &linesize, f);
4269 if (linelen == -1) {
4270 if (ferror(f))
4271 return got_error_from_errno("getline");
4272 return NULL;
4274 free(line);
4275 return NULL;
4278 static const struct got_error *
4279 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4280 int start_old, int end_old, int start_new, int end_new,
4281 FILE *outfile, FILE *rejectfile)
4283 const struct got_error *err;
4285 /* Copy old file's lines leading up to patch. */
4286 while (!feof(f1) && *line_cur1 < start_old) {
4287 err = copy_one_line(f1, outfile, NULL);
4288 if (err)
4289 return err;
4290 (*line_cur1)++;
4292 /* Skip new file's lines leading up to patch. */
4293 while (!feof(f2) && *line_cur2 < start_new) {
4294 if (rejectfile)
4295 err = copy_one_line(f2, NULL, rejectfile);
4296 else
4297 err = skip_one_line(f2);
4298 if (err)
4299 return err;
4300 (*line_cur2)++;
4302 /* Copy patched lines. */
4303 while (!feof(f2) && *line_cur2 <= end_new) {
4304 err = copy_one_line(f2, outfile, NULL);
4305 if (err)
4306 return err;
4307 (*line_cur2)++;
4309 /* Skip over old file's replaced lines. */
4310 while (!feof(f1) && *line_cur1 <= end_old) {
4311 if (rejectfile)
4312 err = copy_one_line(f1, NULL, rejectfile);
4313 else
4314 err = skip_one_line(f1);
4315 if (err)
4316 return err;
4317 (*line_cur1)++;
4320 return NULL;
4323 static const struct got_error *
4324 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4325 FILE *outfile, FILE *rejectfile)
4327 const struct got_error *err;
4329 if (outfile) {
4330 /* Copy old file's lines until EOF. */
4331 while (!feof(f1)) {
4332 err = copy_one_line(f1, outfile, NULL);
4333 if (err)
4334 return err;
4335 (*line_cur1)++;
4338 if (rejectfile) {
4339 /* Copy new file's lines until EOF. */
4340 while (!feof(f2)) {
4341 err = copy_one_line(f2, NULL, rejectfile);
4342 if (err)
4343 return err;
4344 (*line_cur2)++;
4348 return NULL;
4351 static const struct got_error *
4352 apply_or_reject_change(int *choice, int *nchunks_used,
4353 struct diff_result *diff_result, int n,
4354 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4355 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4356 got_worktree_patch_cb patch_cb, void *patch_arg)
4358 const struct got_error *err = NULL;
4359 struct diff_chunk_context cc = {};
4360 int start_old, end_old, start_new, end_new;
4361 FILE *hunkfile;
4362 struct diff_output_unidiff_state *diff_state;
4363 struct diff_input_info diff_info;
4364 int rc;
4366 *choice = GOT_PATCH_CHOICE_NONE;
4368 /* Get changed line numbers without context lines for copy_change(). */
4369 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4370 start_old = cc.left.start;
4371 end_old = cc.left.end;
4372 start_new = cc.right.start;
4373 end_new = cc.right.end;
4375 /* Get the same change with context lines for display. */
4376 memset(&cc, 0, sizeof(cc));
4377 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4379 memset(&diff_info, 0, sizeof(diff_info));
4380 diff_info.left_path = relpath;
4381 diff_info.right_path = relpath;
4383 diff_state = diff_output_unidiff_state_alloc();
4384 if (diff_state == NULL)
4385 return got_error_set_errno(ENOMEM,
4386 "diff_output_unidiff_state_alloc");
4388 hunkfile = got_opentemp();
4389 if (hunkfile == NULL) {
4390 err = got_error_from_errno("got_opentemp");
4391 goto done;
4394 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4395 diff_result, &cc);
4396 if (rc != DIFF_RC_OK) {
4397 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4398 goto done;
4401 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4402 err = got_ferror(hunkfile, GOT_ERR_IO);
4403 goto done;
4406 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4407 hunkfile, changeno, nchanges);
4408 if (err)
4409 goto done;
4411 switch (*choice) {
4412 case GOT_PATCH_CHOICE_YES:
4413 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4414 end_old, start_new, end_new, outfile, rejectfile);
4415 break;
4416 case GOT_PATCH_CHOICE_NO:
4417 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4418 end_old, start_new, end_new, rejectfile, outfile);
4419 break;
4420 case GOT_PATCH_CHOICE_QUIT:
4421 break;
4422 default:
4423 err = got_error(GOT_ERR_PATCH_CHOICE);
4424 break;
4426 done:
4427 diff_output_unidiff_state_free(diff_state);
4428 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4429 err = got_error_from_errno("fclose");
4430 return err;
4433 struct revert_file_args {
4434 struct got_worktree *worktree;
4435 struct got_fileindex *fileindex;
4436 got_worktree_checkout_cb progress_cb;
4437 void *progress_arg;
4438 got_worktree_patch_cb patch_cb;
4439 void *patch_arg;
4440 struct got_repository *repo;
4441 int unlink_added_files;
4444 static const struct got_error *
4445 create_patched_content(char **path_outfile, int reverse_patch,
4446 struct got_object_id *blob_id, const char *path2,
4447 int dirfd2, const char *de_name2,
4448 const char *relpath, struct got_repository *repo,
4449 got_worktree_patch_cb patch_cb, void *patch_arg)
4451 const struct got_error *err, *free_err;
4452 struct got_blob_object *blob = NULL;
4453 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4454 int fd2 = -1;
4455 char link_target[PATH_MAX];
4456 ssize_t link_len = 0;
4457 char *path1 = NULL, *id_str = NULL;
4458 struct stat sb2;
4459 struct got_diffreg_result *diffreg_result = NULL;
4460 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4461 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4463 *path_outfile = NULL;
4465 err = got_object_id_str(&id_str, blob_id);
4466 if (err)
4467 return err;
4469 if (dirfd2 != -1) {
4470 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4471 if (fd2 == -1) {
4472 if (errno != ELOOP) {
4473 err = got_error_from_errno2("openat", path2);
4474 goto done;
4476 link_len = readlinkat(dirfd2, de_name2,
4477 link_target, sizeof(link_target));
4478 if (link_len == -1)
4479 return got_error_from_errno2("readlinkat", path2);
4480 sb2.st_mode = S_IFLNK;
4481 sb2.st_size = link_len;
4483 } else {
4484 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4485 if (fd2 == -1) {
4486 if (errno != ELOOP) {
4487 err = got_error_from_errno2("open", path2);
4488 goto done;
4490 link_len = readlink(path2, link_target,
4491 sizeof(link_target));
4492 if (link_len == -1)
4493 return got_error_from_errno2("readlink", path2);
4494 sb2.st_mode = S_IFLNK;
4495 sb2.st_size = link_len;
4498 if (fd2 != -1) {
4499 if (fstat(fd2, &sb2) == -1) {
4500 err = got_error_from_errno2("fstat", path2);
4501 goto done;
4504 f2 = fdopen(fd2, "r");
4505 if (f2 == NULL) {
4506 err = got_error_from_errno2("fdopen", path2);
4507 goto done;
4509 fd2 = -1;
4510 } else {
4511 size_t n;
4512 f2 = got_opentemp();
4513 if (f2 == NULL) {
4514 err = got_error_from_errno2("got_opentemp", path2);
4515 goto done;
4517 n = fwrite(link_target, 1, link_len, f2);
4518 if (n != link_len) {
4519 err = got_ferror(f2, GOT_ERR_IO);
4520 goto done;
4522 if (fflush(f2) == EOF) {
4523 err = got_error_from_errno("fflush");
4524 goto done;
4526 rewind(f2);
4529 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4530 if (err)
4531 goto done;
4533 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4534 if (err)
4535 goto done;
4537 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4538 if (err)
4539 goto done;
4541 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4542 NULL);
4543 if (err)
4544 goto done;
4546 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4547 if (err)
4548 goto done;
4550 if (fseek(f1, 0L, SEEK_SET) == -1)
4551 return got_ferror(f1, GOT_ERR_IO);
4552 if (fseek(f2, 0L, SEEK_SET) == -1)
4553 return got_ferror(f2, GOT_ERR_IO);
4555 /* Count the number of actual changes in the diff result. */
4556 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4557 struct diff_chunk_context cc = {};
4558 diff_chunk_context_load_change(&cc, &nchunks_used,
4559 diffreg_result->result, n, 0);
4560 nchanges++;
4562 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4563 int choice;
4564 err = apply_or_reject_change(&choice, &nchunks_used,
4565 diffreg_result->result, n, relpath, f1, f2,
4566 &line_cur1, &line_cur2,
4567 reverse_patch ? NULL : outfile,
4568 reverse_patch ? outfile : NULL,
4569 ++i, nchanges, patch_cb, patch_arg);
4570 if (err)
4571 goto done;
4572 if (choice == GOT_PATCH_CHOICE_YES)
4573 have_content = 1;
4574 else if (choice == GOT_PATCH_CHOICE_QUIT)
4575 break;
4577 if (have_content) {
4578 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4579 reverse_patch ? NULL : outfile,
4580 reverse_patch ? outfile : NULL);
4581 if (err)
4582 goto done;
4584 if (!S_ISLNK(sb2.st_mode)) {
4585 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4586 err = got_error_from_errno2("fchmod", path2);
4587 goto done;
4591 done:
4592 free(id_str);
4593 if (blob)
4594 got_object_blob_close(blob);
4595 free_err = got_diffreg_result_free(diffreg_result);
4596 if (err == NULL)
4597 err = free_err;
4598 if (f1 && fclose(f1) == EOF && err == NULL)
4599 err = got_error_from_errno2("fclose", path1);
4600 if (f2 && fclose(f2) == EOF && err == NULL)
4601 err = got_error_from_errno2("fclose", path2);
4602 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4603 err = got_error_from_errno2("close", path2);
4604 if (outfile && fclose(outfile) == EOF && err == NULL)
4605 err = got_error_from_errno2("fclose", *path_outfile);
4606 if (path1 && unlink(path1) == -1 && err == NULL)
4607 err = got_error_from_errno2("unlink", path1);
4608 if (err || !have_content) {
4609 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4610 err = got_error_from_errno2("unlink", *path_outfile);
4611 free(*path_outfile);
4612 *path_outfile = NULL;
4614 free(path1);
4615 return err;
4618 static const struct got_error *
4619 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4620 const char *relpath, struct got_object_id *blob_id,
4621 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4622 int dirfd, const char *de_name)
4624 struct revert_file_args *a = arg;
4625 const struct got_error *err = NULL;
4626 char *parent_path = NULL;
4627 struct got_fileindex_entry *ie;
4628 struct got_tree_object *tree = NULL;
4629 struct got_object_id *tree_id = NULL;
4630 const struct got_tree_entry *te = NULL;
4631 char *tree_path = NULL, *te_name;
4632 char *ondisk_path = NULL, *path_content = NULL;
4633 struct got_blob_object *blob = NULL;
4635 /* Reverting a staged deletion is a no-op. */
4636 if (status == GOT_STATUS_DELETE &&
4637 staged_status != GOT_STATUS_NO_CHANGE)
4638 return NULL;
4640 if (status == GOT_STATUS_UNVERSIONED)
4641 return (*a->progress_cb)(a->progress_arg,
4642 GOT_STATUS_UNVERSIONED, relpath);
4644 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4645 if (ie == NULL)
4646 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4648 /* Construct in-repository path of tree which contains this blob. */
4649 err = got_path_dirname(&parent_path, ie->path);
4650 if (err) {
4651 if (err->code != GOT_ERR_BAD_PATH)
4652 goto done;
4653 parent_path = strdup("/");
4654 if (parent_path == NULL) {
4655 err = got_error_from_errno("strdup");
4656 goto done;
4659 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4660 tree_path = strdup(parent_path);
4661 if (tree_path == NULL) {
4662 err = got_error_from_errno("strdup");
4663 goto done;
4665 } else {
4666 if (got_path_is_root_dir(parent_path)) {
4667 tree_path = strdup(a->worktree->path_prefix);
4668 if (tree_path == NULL) {
4669 err = got_error_from_errno("strdup");
4670 goto done;
4672 } else {
4673 if (asprintf(&tree_path, "%s/%s",
4674 a->worktree->path_prefix, parent_path) == -1) {
4675 err = got_error_from_errno("asprintf");
4676 goto done;
4681 err = got_object_id_by_path(&tree_id, a->repo,
4682 a->worktree->base_commit_id, tree_path);
4683 if (err) {
4684 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4685 (status == GOT_STATUS_ADD ||
4686 staged_status == GOT_STATUS_ADD)))
4687 goto done;
4688 } else {
4689 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4690 if (err)
4691 goto done;
4693 err = got_path_basename(&te_name, ie->path);
4694 if (err)
4695 goto done;
4697 te = got_object_tree_find_entry(tree, te_name);
4698 free(te_name);
4699 if (te == NULL && status != GOT_STATUS_ADD &&
4700 staged_status != GOT_STATUS_ADD) {
4701 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4702 goto done;
4706 switch (status) {
4707 case GOT_STATUS_ADD:
4708 if (a->patch_cb) {
4709 int choice = GOT_PATCH_CHOICE_NONE;
4710 err = (*a->patch_cb)(&choice, a->patch_arg,
4711 status, ie->path, NULL, 1, 1);
4712 if (err)
4713 goto done;
4714 if (choice != GOT_PATCH_CHOICE_YES)
4715 break;
4717 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4718 ie->path);
4719 if (err)
4720 goto done;
4721 got_fileindex_entry_remove(a->fileindex, ie);
4722 if (a->unlink_added_files) {
4723 if (asprintf(&ondisk_path, "%s/%s",
4724 got_worktree_get_root_path(a->worktree),
4725 relpath) == -1) {
4726 err = got_error_from_errno("asprintf");
4727 goto done;
4729 if (unlink(ondisk_path) == -1) {
4730 err = got_error_from_errno2("unlink",
4731 ondisk_path);
4732 break;
4735 break;
4736 case GOT_STATUS_DELETE:
4737 if (a->patch_cb) {
4738 int choice = GOT_PATCH_CHOICE_NONE;
4739 err = (*a->patch_cb)(&choice, a->patch_arg,
4740 status, ie->path, NULL, 1, 1);
4741 if (err)
4742 goto done;
4743 if (choice != GOT_PATCH_CHOICE_YES)
4744 break;
4746 /* fall through */
4747 case GOT_STATUS_MODIFY:
4748 case GOT_STATUS_MODE_CHANGE:
4749 case GOT_STATUS_CONFLICT:
4750 case GOT_STATUS_MISSING: {
4751 struct got_object_id id;
4752 if (staged_status == GOT_STATUS_ADD ||
4753 staged_status == GOT_STATUS_MODIFY) {
4754 memcpy(id.sha1, ie->staged_blob_sha1,
4755 SHA1_DIGEST_LENGTH);
4756 } else
4757 memcpy(id.sha1, ie->blob_sha1,
4758 SHA1_DIGEST_LENGTH);
4759 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4760 if (err)
4761 goto done;
4763 if (asprintf(&ondisk_path, "%s/%s",
4764 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4765 err = got_error_from_errno("asprintf");
4766 goto done;
4769 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4770 status == GOT_STATUS_CONFLICT)) {
4771 int is_bad_symlink = 0;
4772 err = create_patched_content(&path_content, 1, &id,
4773 ondisk_path, dirfd, de_name, ie->path, a->repo,
4774 a->patch_cb, a->patch_arg);
4775 if (err || path_content == NULL)
4776 break;
4777 if (te && S_ISLNK(te->mode)) {
4778 if (unlink(path_content) == -1) {
4779 err = got_error_from_errno2("unlink",
4780 path_content);
4781 break;
4783 err = install_symlink(&is_bad_symlink,
4784 a->worktree, ondisk_path, ie->path,
4785 blob, 0, 1, 0, a->repo,
4786 a->progress_cb, a->progress_arg);
4787 } else {
4788 if (rename(path_content, ondisk_path) == -1) {
4789 err = got_error_from_errno3("rename",
4790 path_content, ondisk_path);
4791 goto done;
4794 } else {
4795 int is_bad_symlink = 0;
4796 if (te && S_ISLNK(te->mode)) {
4797 err = install_symlink(&is_bad_symlink,
4798 a->worktree, ondisk_path, ie->path,
4799 blob, 0, 1, 0, a->repo,
4800 a->progress_cb, a->progress_arg);
4801 } else {
4802 err = install_blob(a->worktree, ondisk_path,
4803 ie->path,
4804 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4805 got_fileindex_perms_to_st(ie), blob,
4806 0, 1, 0, 0, a->repo,
4807 a->progress_cb, a->progress_arg);
4809 if (err)
4810 goto done;
4811 if (status == GOT_STATUS_DELETE ||
4812 status == GOT_STATUS_MODE_CHANGE) {
4813 err = got_fileindex_entry_update(ie,
4814 a->worktree->root_fd, relpath,
4815 blob->id.sha1,
4816 a->worktree->base_commit_id->sha1, 1);
4817 if (err)
4818 goto done;
4820 if (is_bad_symlink) {
4821 got_fileindex_entry_filetype_set(ie,
4822 GOT_FILEIDX_MODE_BAD_SYMLINK);
4825 break;
4827 default:
4828 break;
4830 done:
4831 free(ondisk_path);
4832 free(path_content);
4833 free(parent_path);
4834 free(tree_path);
4835 if (blob)
4836 got_object_blob_close(blob);
4837 if (tree)
4838 got_object_tree_close(tree);
4839 free(tree_id);
4840 return err;
4843 const struct got_error *
4844 got_worktree_revert(struct got_worktree *worktree,
4845 struct got_pathlist_head *paths,
4846 got_worktree_checkout_cb progress_cb, void *progress_arg,
4847 got_worktree_patch_cb patch_cb, void *patch_arg,
4848 struct got_repository *repo)
4850 struct got_fileindex *fileindex = NULL;
4851 char *fileindex_path = NULL;
4852 const struct got_error *err = NULL, *unlockerr = NULL;
4853 const struct got_error *sync_err = NULL;
4854 struct got_pathlist_entry *pe;
4855 struct revert_file_args rfa;
4857 err = lock_worktree(worktree, LOCK_EX);
4858 if (err)
4859 return err;
4861 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4862 if (err)
4863 goto done;
4865 rfa.worktree = worktree;
4866 rfa.fileindex = fileindex;
4867 rfa.progress_cb = progress_cb;
4868 rfa.progress_arg = progress_arg;
4869 rfa.patch_cb = patch_cb;
4870 rfa.patch_arg = patch_arg;
4871 rfa.repo = repo;
4872 rfa.unlink_added_files = 0;
4873 TAILQ_FOREACH(pe, paths, entry) {
4874 err = worktree_status(worktree, pe->path, fileindex, repo,
4875 revert_file, &rfa, NULL, NULL, 0, 0);
4876 if (err)
4877 break;
4879 sync_err = sync_fileindex(fileindex, fileindex_path);
4880 if (sync_err && err == NULL)
4881 err = sync_err;
4882 done:
4883 free(fileindex_path);
4884 if (fileindex)
4885 got_fileindex_free(fileindex);
4886 unlockerr = lock_worktree(worktree, LOCK_SH);
4887 if (unlockerr && err == NULL)
4888 err = unlockerr;
4889 return err;
4892 static void
4893 free_commitable(struct got_commitable *ct)
4895 free(ct->path);
4896 free(ct->in_repo_path);
4897 free(ct->ondisk_path);
4898 free(ct->blob_id);
4899 free(ct->base_blob_id);
4900 free(ct->staged_blob_id);
4901 free(ct->base_commit_id);
4902 free(ct);
4905 struct collect_commitables_arg {
4906 struct got_pathlist_head *commitable_paths;
4907 struct got_repository *repo;
4908 struct got_worktree *worktree;
4909 struct got_fileindex *fileindex;
4910 int have_staged_files;
4911 int allow_bad_symlinks;
4914 static const struct got_error *
4915 collect_commitables(void *arg, unsigned char status,
4916 unsigned char staged_status, const char *relpath,
4917 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4918 struct got_object_id *commit_id, int dirfd, const char *de_name)
4920 struct collect_commitables_arg *a = arg;
4921 const struct got_error *err = NULL;
4922 struct got_commitable *ct = NULL;
4923 struct got_pathlist_entry *new = NULL;
4924 char *parent_path = NULL, *path = NULL;
4925 struct stat sb;
4927 if (a->have_staged_files) {
4928 if (staged_status != GOT_STATUS_MODIFY &&
4929 staged_status != GOT_STATUS_ADD &&
4930 staged_status != GOT_STATUS_DELETE)
4931 return NULL;
4932 } else {
4933 if (status == GOT_STATUS_CONFLICT)
4934 return got_error(GOT_ERR_COMMIT_CONFLICT);
4936 if (status != GOT_STATUS_MODIFY &&
4937 status != GOT_STATUS_MODE_CHANGE &&
4938 status != GOT_STATUS_ADD &&
4939 status != GOT_STATUS_DELETE)
4940 return NULL;
4943 if (asprintf(&path, "/%s", relpath) == -1) {
4944 err = got_error_from_errno("asprintf");
4945 goto done;
4947 if (strcmp(path, "/") == 0) {
4948 parent_path = strdup("");
4949 if (parent_path == NULL)
4950 return got_error_from_errno("strdup");
4951 } else {
4952 err = got_path_dirname(&parent_path, path);
4953 if (err)
4954 return err;
4957 ct = calloc(1, sizeof(*ct));
4958 if (ct == NULL) {
4959 err = got_error_from_errno("calloc");
4960 goto done;
4963 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4964 relpath) == -1) {
4965 err = got_error_from_errno("asprintf");
4966 goto done;
4969 if (staged_status == GOT_STATUS_ADD ||
4970 staged_status == GOT_STATUS_MODIFY) {
4971 struct got_fileindex_entry *ie;
4972 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4973 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4974 case GOT_FILEIDX_MODE_REGULAR_FILE:
4975 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4976 ct->mode = S_IFREG;
4977 break;
4978 case GOT_FILEIDX_MODE_SYMLINK:
4979 ct->mode = S_IFLNK;
4980 break;
4981 default:
4982 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4983 goto done;
4985 ct->mode |= got_fileindex_entry_perms_get(ie);
4986 } else if (status != GOT_STATUS_DELETE &&
4987 staged_status != GOT_STATUS_DELETE) {
4988 if (dirfd != -1) {
4989 if (fstatat(dirfd, de_name, &sb,
4990 AT_SYMLINK_NOFOLLOW) == -1) {
4991 err = got_error_from_errno2("fstatat",
4992 ct->ondisk_path);
4993 goto done;
4995 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4996 err = got_error_from_errno2("lstat", ct->ondisk_path);
4997 goto done;
4999 ct->mode = sb.st_mode;
5002 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5003 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5004 relpath) == -1) {
5005 err = got_error_from_errno("asprintf");
5006 goto done;
5009 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5010 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5011 int is_bad_symlink;
5012 char target_path[PATH_MAX];
5013 ssize_t target_len;
5014 target_len = readlink(ct->ondisk_path, target_path,
5015 sizeof(target_path));
5016 if (target_len == -1) {
5017 err = got_error_from_errno2("readlink",
5018 ct->ondisk_path);
5019 goto done;
5021 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5022 target_len, ct->ondisk_path, a->worktree->root_path);
5023 if (err)
5024 goto done;
5025 if (is_bad_symlink) {
5026 err = got_error_path(ct->ondisk_path,
5027 GOT_ERR_BAD_SYMLINK);
5028 goto done;
5033 ct->status = status;
5034 ct->staged_status = staged_status;
5035 ct->blob_id = NULL; /* will be filled in when blob gets created */
5036 if (ct->status != GOT_STATUS_ADD &&
5037 ct->staged_status != GOT_STATUS_ADD) {
5038 ct->base_blob_id = got_object_id_dup(blob_id);
5039 if (ct->base_blob_id == NULL) {
5040 err = got_error_from_errno("got_object_id_dup");
5041 goto done;
5043 ct->base_commit_id = got_object_id_dup(commit_id);
5044 if (ct->base_commit_id == NULL) {
5045 err = got_error_from_errno("got_object_id_dup");
5046 goto done;
5049 if (ct->staged_status == GOT_STATUS_ADD ||
5050 ct->staged_status == GOT_STATUS_MODIFY) {
5051 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5052 if (ct->staged_blob_id == NULL) {
5053 err = got_error_from_errno("got_object_id_dup");
5054 goto done;
5057 ct->path = strdup(path);
5058 if (ct->path == NULL) {
5059 err = got_error_from_errno("strdup");
5060 goto done;
5062 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5063 done:
5064 if (ct && (err || new == NULL))
5065 free_commitable(ct);
5066 free(parent_path);
5067 free(path);
5068 return err;
5071 static const struct got_error *write_tree(struct got_object_id **, int *,
5072 struct got_tree_object *, const char *, struct got_pathlist_head *,
5073 got_worktree_status_cb status_cb, void *status_arg,
5074 struct got_repository *);
5076 static const struct got_error *
5077 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5078 struct got_tree_entry *te, const char *parent_path,
5079 struct got_pathlist_head *commitable_paths,
5080 got_worktree_status_cb status_cb, void *status_arg,
5081 struct got_repository *repo)
5083 const struct got_error *err = NULL;
5084 struct got_tree_object *subtree;
5085 char *subpath;
5087 if (asprintf(&subpath, "%s%s%s", parent_path,
5088 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5089 return got_error_from_errno("asprintf");
5091 err = got_object_open_as_tree(&subtree, repo, &te->id);
5092 if (err)
5093 return err;
5095 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5096 commitable_paths, status_cb, status_arg, repo);
5097 got_object_tree_close(subtree);
5098 free(subpath);
5099 return err;
5102 static const struct got_error *
5103 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5105 const struct got_error *err = NULL;
5106 char *ct_parent_path = NULL;
5108 *match = 0;
5110 if (strchr(ct->in_repo_path, '/') == NULL) {
5111 *match = got_path_is_root_dir(path);
5112 return NULL;
5115 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5116 if (err)
5117 return err;
5118 *match = (strcmp(path, ct_parent_path) == 0);
5119 free(ct_parent_path);
5120 return err;
5123 static mode_t
5124 get_ct_file_mode(struct got_commitable *ct)
5126 if (S_ISLNK(ct->mode))
5127 return S_IFLNK;
5129 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5132 static const struct got_error *
5133 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5134 struct got_tree_entry *te, struct got_commitable *ct)
5136 const struct got_error *err = NULL;
5138 *new_te = NULL;
5140 err = got_object_tree_entry_dup(new_te, te);
5141 if (err)
5142 goto done;
5144 (*new_te)->mode = get_ct_file_mode(ct);
5146 if (ct->staged_status == GOT_STATUS_MODIFY)
5147 memcpy(&(*new_te)->id, ct->staged_blob_id,
5148 sizeof((*new_te)->id));
5149 else
5150 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5151 done:
5152 if (err && *new_te) {
5153 free(*new_te);
5154 *new_te = NULL;
5156 return err;
5159 static const struct got_error *
5160 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5161 struct got_commitable *ct)
5163 const struct got_error *err = NULL;
5164 char *ct_name = NULL;
5166 *new_te = NULL;
5168 *new_te = calloc(1, sizeof(**new_te));
5169 if (*new_te == NULL)
5170 return got_error_from_errno("calloc");
5172 err = got_path_basename(&ct_name, ct->path);
5173 if (err)
5174 goto done;
5175 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5176 sizeof((*new_te)->name)) {
5177 err = got_error(GOT_ERR_NO_SPACE);
5178 goto done;
5181 (*new_te)->mode = get_ct_file_mode(ct);
5183 if (ct->staged_status == GOT_STATUS_ADD)
5184 memcpy(&(*new_te)->id, ct->staged_blob_id,
5185 sizeof((*new_te)->id));
5186 else
5187 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5188 done:
5189 free(ct_name);
5190 if (err && *new_te) {
5191 free(*new_te);
5192 *new_te = NULL;
5194 return err;
5197 static const struct got_error *
5198 insert_tree_entry(struct got_tree_entry *new_te,
5199 struct got_pathlist_head *paths)
5201 const struct got_error *err = NULL;
5202 struct got_pathlist_entry *new_pe;
5204 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5205 if (err)
5206 return err;
5207 if (new_pe == NULL)
5208 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5209 return NULL;
5212 static const struct got_error *
5213 report_ct_status(struct got_commitable *ct,
5214 got_worktree_status_cb status_cb, void *status_arg)
5216 const char *ct_path = ct->path;
5217 unsigned char status;
5219 while (ct_path[0] == '/')
5220 ct_path++;
5222 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5223 status = ct->staged_status;
5224 else
5225 status = ct->status;
5227 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5228 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5231 static const struct got_error *
5232 match_modified_subtree(int *modified, struct got_tree_entry *te,
5233 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5235 const struct got_error *err = NULL;
5236 struct got_pathlist_entry *pe;
5237 char *te_path;
5239 *modified = 0;
5241 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5242 got_path_is_root_dir(base_tree_path) ? "" : "/",
5243 te->name) == -1)
5244 return got_error_from_errno("asprintf");
5246 TAILQ_FOREACH(pe, commitable_paths, entry) {
5247 struct got_commitable *ct = pe->data;
5248 *modified = got_path_is_child(ct->in_repo_path, te_path,
5249 strlen(te_path));
5250 if (*modified)
5251 break;
5254 free(te_path);
5255 return err;
5258 static const struct got_error *
5259 match_deleted_or_modified_ct(struct got_commitable **ctp,
5260 struct got_tree_entry *te, const char *base_tree_path,
5261 struct got_pathlist_head *commitable_paths)
5263 const struct got_error *err = NULL;
5264 struct got_pathlist_entry *pe;
5266 *ctp = NULL;
5268 TAILQ_FOREACH(pe, commitable_paths, entry) {
5269 struct got_commitable *ct = pe->data;
5270 char *ct_name = NULL;
5271 int path_matches;
5273 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5274 if (ct->status != GOT_STATUS_MODIFY &&
5275 ct->status != GOT_STATUS_MODE_CHANGE &&
5276 ct->status != GOT_STATUS_DELETE)
5277 continue;
5278 } else {
5279 if (ct->staged_status != GOT_STATUS_MODIFY &&
5280 ct->staged_status != GOT_STATUS_DELETE)
5281 continue;
5284 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5285 continue;
5287 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5288 if (err)
5289 return err;
5290 if (!path_matches)
5291 continue;
5293 err = got_path_basename(&ct_name, pe->path);
5294 if (err)
5295 return err;
5297 if (strcmp(te->name, ct_name) != 0) {
5298 free(ct_name);
5299 continue;
5301 free(ct_name);
5303 *ctp = ct;
5304 break;
5307 return err;
5310 static const struct got_error *
5311 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5312 const char *child_path, const char *path_base_tree,
5313 struct got_pathlist_head *commitable_paths,
5314 got_worktree_status_cb status_cb, void *status_arg,
5315 struct got_repository *repo)
5317 const struct got_error *err = NULL;
5318 struct got_tree_entry *new_te;
5319 char *subtree_path;
5320 struct got_object_id *id = NULL;
5321 int nentries;
5323 *new_tep = NULL;
5325 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5326 got_path_is_root_dir(path_base_tree) ? "" : "/",
5327 child_path) == -1)
5328 return got_error_from_errno("asprintf");
5330 new_te = calloc(1, sizeof(*new_te));
5331 if (new_te == NULL)
5332 return got_error_from_errno("calloc");
5333 new_te->mode = S_IFDIR;
5335 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5336 sizeof(new_te->name)) {
5337 err = got_error(GOT_ERR_NO_SPACE);
5338 goto done;
5340 err = write_tree(&id, &nentries, NULL, subtree_path,
5341 commitable_paths, status_cb, status_arg, repo);
5342 if (err) {
5343 free(new_te);
5344 goto done;
5346 memcpy(&new_te->id, id, sizeof(new_te->id));
5347 done:
5348 free(id);
5349 free(subtree_path);
5350 if (err == NULL)
5351 *new_tep = new_te;
5352 return err;
5355 static const struct got_error *
5356 write_tree(struct got_object_id **new_tree_id, int *nentries,
5357 struct got_tree_object *base_tree, const char *path_base_tree,
5358 struct got_pathlist_head *commitable_paths,
5359 got_worktree_status_cb status_cb, void *status_arg,
5360 struct got_repository *repo)
5362 const struct got_error *err = NULL;
5363 struct got_pathlist_head paths;
5364 struct got_tree_entry *te, *new_te = NULL;
5365 struct got_pathlist_entry *pe;
5367 TAILQ_INIT(&paths);
5368 *nentries = 0;
5370 /* Insert, and recurse into, newly added entries first. */
5371 TAILQ_FOREACH(pe, commitable_paths, entry) {
5372 struct got_commitable *ct = pe->data;
5373 char *child_path = NULL, *slash;
5375 if ((ct->status != GOT_STATUS_ADD &&
5376 ct->staged_status != GOT_STATUS_ADD) ||
5377 (ct->flags & GOT_COMMITABLE_ADDED))
5378 continue;
5380 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5381 strlen(path_base_tree)))
5382 continue;
5384 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5385 ct->in_repo_path);
5386 if (err)
5387 goto done;
5389 slash = strchr(child_path, '/');
5390 if (slash == NULL) {
5391 err = alloc_added_blob_tree_entry(&new_te, ct);
5392 if (err)
5393 goto done;
5394 err = report_ct_status(ct, status_cb, status_arg);
5395 if (err)
5396 goto done;
5397 ct->flags |= GOT_COMMITABLE_ADDED;
5398 err = insert_tree_entry(new_te, &paths);
5399 if (err)
5400 goto done;
5401 (*nentries)++;
5402 } else {
5403 *slash = '\0'; /* trim trailing path components */
5404 if (base_tree == NULL ||
5405 got_object_tree_find_entry(base_tree, child_path)
5406 == NULL) {
5407 err = make_subtree_for_added_blob(&new_te,
5408 child_path, path_base_tree,
5409 commitable_paths, status_cb, status_arg,
5410 repo);
5411 if (err)
5412 goto done;
5413 err = insert_tree_entry(new_te, &paths);
5414 if (err)
5415 goto done;
5416 (*nentries)++;
5421 if (base_tree) {
5422 int i, nbase_entries;
5423 /* Handle modified and deleted entries. */
5424 nbase_entries = got_object_tree_get_nentries(base_tree);
5425 for (i = 0; i < nbase_entries; i++) {
5426 struct got_commitable *ct = NULL;
5428 te = got_object_tree_get_entry(base_tree, i);
5429 if (got_object_tree_entry_is_submodule(te)) {
5430 /* Entry is a submodule; just copy it. */
5431 err = got_object_tree_entry_dup(&new_te, te);
5432 if (err)
5433 goto done;
5434 err = insert_tree_entry(new_te, &paths);
5435 if (err)
5436 goto done;
5437 (*nentries)++;
5438 continue;
5441 if (S_ISDIR(te->mode)) {
5442 int modified;
5443 err = got_object_tree_entry_dup(&new_te, te);
5444 if (err)
5445 goto done;
5446 err = match_modified_subtree(&modified, te,
5447 path_base_tree, commitable_paths);
5448 if (err)
5449 goto done;
5450 /* Avoid recursion into unmodified subtrees. */
5451 if (modified) {
5452 struct got_object_id *new_id;
5453 int nsubentries;
5454 err = write_subtree(&new_id,
5455 &nsubentries, te,
5456 path_base_tree, commitable_paths,
5457 status_cb, status_arg, repo);
5458 if (err)
5459 goto done;
5460 if (nsubentries == 0) {
5461 /* All entries were deleted. */
5462 free(new_id);
5463 continue;
5465 memcpy(&new_te->id, new_id,
5466 sizeof(new_te->id));
5467 free(new_id);
5469 err = insert_tree_entry(new_te, &paths);
5470 if (err)
5471 goto done;
5472 (*nentries)++;
5473 continue;
5476 err = match_deleted_or_modified_ct(&ct, te,
5477 path_base_tree, commitable_paths);
5478 if (err)
5479 goto done;
5480 if (ct) {
5481 /* NB: Deleted entries get dropped here. */
5482 if (ct->status == GOT_STATUS_MODIFY ||
5483 ct->status == GOT_STATUS_MODE_CHANGE ||
5484 ct->staged_status == GOT_STATUS_MODIFY) {
5485 err = alloc_modified_blob_tree_entry(
5486 &new_te, te, ct);
5487 if (err)
5488 goto done;
5489 err = insert_tree_entry(new_te, &paths);
5490 if (err)
5491 goto done;
5492 (*nentries)++;
5494 err = report_ct_status(ct, status_cb,
5495 status_arg);
5496 if (err)
5497 goto done;
5498 } else {
5499 /* Entry is unchanged; just copy it. */
5500 err = got_object_tree_entry_dup(&new_te, te);
5501 if (err)
5502 goto done;
5503 err = insert_tree_entry(new_te, &paths);
5504 if (err)
5505 goto done;
5506 (*nentries)++;
5511 /* Write new list of entries; deleted entries have been dropped. */
5512 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5513 done:
5514 got_pathlist_free(&paths);
5515 return err;
5518 static const struct got_error *
5519 update_fileindex_after_commit(struct got_worktree *worktree,
5520 struct got_pathlist_head *commitable_paths,
5521 struct got_object_id *new_base_commit_id,
5522 struct got_fileindex *fileindex, int have_staged_files)
5524 const struct got_error *err = NULL;
5525 struct got_pathlist_entry *pe;
5526 char *relpath = NULL;
5528 TAILQ_FOREACH(pe, commitable_paths, entry) {
5529 struct got_fileindex_entry *ie;
5530 struct got_commitable *ct = pe->data;
5532 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5534 err = got_path_skip_common_ancestor(&relpath,
5535 worktree->root_path, ct->ondisk_path);
5536 if (err)
5537 goto done;
5539 if (ie) {
5540 if (ct->status == GOT_STATUS_DELETE ||
5541 ct->staged_status == GOT_STATUS_DELETE) {
5542 got_fileindex_entry_remove(fileindex, ie);
5543 } else if (ct->staged_status == GOT_STATUS_ADD ||
5544 ct->staged_status == GOT_STATUS_MODIFY) {
5545 got_fileindex_entry_stage_set(ie,
5546 GOT_FILEIDX_STAGE_NONE);
5547 got_fileindex_entry_staged_filetype_set(ie, 0);
5549 err = got_fileindex_entry_update(ie,
5550 worktree->root_fd, relpath,
5551 ct->staged_blob_id->sha1,
5552 new_base_commit_id->sha1,
5553 !have_staged_files);
5554 } else
5555 err = got_fileindex_entry_update(ie,
5556 worktree->root_fd, relpath,
5557 ct->blob_id->sha1,
5558 new_base_commit_id->sha1,
5559 !have_staged_files);
5560 } else {
5561 err = got_fileindex_entry_alloc(&ie, pe->path);
5562 if (err)
5563 goto done;
5564 err = got_fileindex_entry_update(ie,
5565 worktree->root_fd, relpath, ct->blob_id->sha1,
5566 new_base_commit_id->sha1, 1);
5567 if (err) {
5568 got_fileindex_entry_free(ie);
5569 goto done;
5571 err = got_fileindex_entry_add(fileindex, ie);
5572 if (err) {
5573 got_fileindex_entry_free(ie);
5574 goto done;
5577 free(relpath);
5578 relpath = NULL;
5580 done:
5581 free(relpath);
5582 return err;
5586 static const struct got_error *
5587 check_out_of_date(const char *in_repo_path, unsigned char status,
5588 unsigned char staged_status, struct got_object_id *base_blob_id,
5589 struct got_object_id *base_commit_id,
5590 struct got_object_id *head_commit_id, struct got_repository *repo,
5591 int ood_errcode)
5593 const struct got_error *err = NULL;
5594 struct got_object_id *id = NULL;
5596 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5597 /* Trivial case: base commit == head commit */
5598 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5599 return NULL;
5601 * Ensure file content which local changes were based
5602 * on matches file content in the branch head.
5604 err = got_object_id_by_path(&id, repo, head_commit_id,
5605 in_repo_path);
5606 if (err) {
5607 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5608 err = got_error(ood_errcode);
5609 goto done;
5610 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5611 err = got_error(ood_errcode);
5612 } else {
5613 /* Require that added files don't exist in the branch head. */
5614 err = got_object_id_by_path(&id, repo, head_commit_id,
5615 in_repo_path);
5616 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5617 goto done;
5618 err = id ? got_error(ood_errcode) : NULL;
5620 done:
5621 free(id);
5622 return err;
5625 const struct got_error *
5626 commit_worktree(struct got_object_id **new_commit_id,
5627 struct got_pathlist_head *commitable_paths,
5628 struct got_object_id *head_commit_id,
5629 struct got_object_id *parent_id2,
5630 struct got_worktree *worktree,
5631 const char *author, const char *committer,
5632 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5633 got_worktree_status_cb status_cb, void *status_arg,
5634 struct got_repository *repo)
5636 const struct got_error *err = NULL, *unlockerr = NULL;
5637 struct got_pathlist_entry *pe;
5638 const char *head_ref_name = NULL;
5639 struct got_commit_object *head_commit = NULL;
5640 struct got_reference *head_ref2 = NULL;
5641 struct got_object_id *head_commit_id2 = NULL;
5642 struct got_tree_object *head_tree = NULL;
5643 struct got_object_id *new_tree_id = NULL;
5644 int nentries, nparents = 0;
5645 struct got_object_id_queue parent_ids;
5646 struct got_object_qid *pid = NULL;
5647 char *logmsg = NULL;
5649 *new_commit_id = NULL;
5651 STAILQ_INIT(&parent_ids);
5653 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5654 if (err)
5655 goto done;
5657 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5658 if (err)
5659 goto done;
5661 if (commit_msg_cb != NULL) {
5662 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5663 if (err)
5664 goto done;
5667 if (logmsg == NULL || strlen(logmsg) == 0) {
5668 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5669 goto done;
5672 /* Create blobs from added and modified files and record their IDs. */
5673 TAILQ_FOREACH(pe, commitable_paths, entry) {
5674 struct got_commitable *ct = pe->data;
5675 char *ondisk_path;
5677 /* Blobs for staged files already exist. */
5678 if (ct->staged_status == GOT_STATUS_ADD ||
5679 ct->staged_status == GOT_STATUS_MODIFY)
5680 continue;
5682 if (ct->status != GOT_STATUS_ADD &&
5683 ct->status != GOT_STATUS_MODIFY &&
5684 ct->status != GOT_STATUS_MODE_CHANGE)
5685 continue;
5687 if (asprintf(&ondisk_path, "%s/%s",
5688 worktree->root_path, pe->path) == -1) {
5689 err = got_error_from_errno("asprintf");
5690 goto done;
5692 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5693 free(ondisk_path);
5694 if (err)
5695 goto done;
5698 /* Recursively write new tree objects. */
5699 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5700 commitable_paths, status_cb, status_arg, repo);
5701 if (err)
5702 goto done;
5704 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5705 if (err)
5706 goto done;
5707 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5708 nparents++;
5709 if (parent_id2) {
5710 err = got_object_qid_alloc(&pid, parent_id2);
5711 if (err)
5712 goto done;
5713 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5714 nparents++;
5716 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5717 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5718 if (logmsg != NULL)
5719 free(logmsg);
5720 if (err)
5721 goto done;
5723 /* Check if a concurrent commit to our branch has occurred. */
5724 head_ref_name = got_worktree_get_head_ref_name(worktree);
5725 if (head_ref_name == NULL) {
5726 err = got_error_from_errno("got_worktree_get_head_ref_name");
5727 goto done;
5729 /* Lock the reference here to prevent concurrent modification. */
5730 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5731 if (err)
5732 goto done;
5733 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5734 if (err)
5735 goto done;
5736 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5737 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5738 goto done;
5740 /* Update branch head in repository. */
5741 err = got_ref_change_ref(head_ref2, *new_commit_id);
5742 if (err)
5743 goto done;
5744 err = got_ref_write(head_ref2, repo);
5745 if (err)
5746 goto done;
5748 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5749 if (err)
5750 goto done;
5752 err = ref_base_commit(worktree, repo);
5753 if (err)
5754 goto done;
5755 done:
5756 got_object_id_queue_free(&parent_ids);
5757 if (head_tree)
5758 got_object_tree_close(head_tree);
5759 if (head_commit)
5760 got_object_commit_close(head_commit);
5761 free(head_commit_id2);
5762 if (head_ref2) {
5763 unlockerr = got_ref_unlock(head_ref2);
5764 if (unlockerr && err == NULL)
5765 err = unlockerr;
5766 got_ref_close(head_ref2);
5768 return err;
5771 static const struct got_error *
5772 check_path_is_commitable(const char *path,
5773 struct got_pathlist_head *commitable_paths)
5775 struct got_pathlist_entry *cpe = NULL;
5776 size_t path_len = strlen(path);
5778 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5779 struct got_commitable *ct = cpe->data;
5780 const char *ct_path = ct->path;
5782 while (ct_path[0] == '/')
5783 ct_path++;
5785 if (strcmp(path, ct_path) == 0 ||
5786 got_path_is_child(ct_path, path, path_len))
5787 break;
5790 if (cpe == NULL)
5791 return got_error_path(path, GOT_ERR_BAD_PATH);
5793 return NULL;
5796 static const struct got_error *
5797 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5799 int *have_staged_files = arg;
5801 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5802 *have_staged_files = 1;
5803 return got_error(GOT_ERR_CANCELLED);
5806 return NULL;
5809 static const struct got_error *
5810 check_non_staged_files(struct got_fileindex *fileindex,
5811 struct got_pathlist_head *paths)
5813 struct got_pathlist_entry *pe;
5814 struct got_fileindex_entry *ie;
5816 TAILQ_FOREACH(pe, paths, entry) {
5817 if (pe->path[0] == '\0')
5818 continue;
5819 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5820 if (ie == NULL)
5821 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5822 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5823 return got_error_path(pe->path,
5824 GOT_ERR_FILE_NOT_STAGED);
5827 return NULL;
5830 const struct got_error *
5831 got_worktree_commit(struct got_object_id **new_commit_id,
5832 struct got_worktree *worktree, struct got_pathlist_head *paths,
5833 const char *author, const char *committer, int allow_bad_symlinks,
5834 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5835 got_worktree_status_cb status_cb, void *status_arg,
5836 struct got_repository *repo)
5838 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5839 struct got_fileindex *fileindex = NULL;
5840 char *fileindex_path = NULL;
5841 struct got_pathlist_head commitable_paths;
5842 struct collect_commitables_arg cc_arg;
5843 struct got_pathlist_entry *pe;
5844 struct got_reference *head_ref = NULL;
5845 struct got_object_id *head_commit_id = NULL;
5846 int have_staged_files = 0;
5848 *new_commit_id = NULL;
5850 TAILQ_INIT(&commitable_paths);
5852 err = lock_worktree(worktree, LOCK_EX);
5853 if (err)
5854 goto done;
5856 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5857 if (err)
5858 goto done;
5860 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5861 if (err)
5862 goto done;
5864 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5865 if (err)
5866 goto done;
5868 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5869 &have_staged_files);
5870 if (err && err->code != GOT_ERR_CANCELLED)
5871 goto done;
5872 if (have_staged_files) {
5873 err = check_non_staged_files(fileindex, paths);
5874 if (err)
5875 goto done;
5878 cc_arg.commitable_paths = &commitable_paths;
5879 cc_arg.worktree = worktree;
5880 cc_arg.fileindex = fileindex;
5881 cc_arg.repo = repo;
5882 cc_arg.have_staged_files = have_staged_files;
5883 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5884 TAILQ_FOREACH(pe, paths, entry) {
5885 err = worktree_status(worktree, pe->path, fileindex, repo,
5886 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5887 if (err)
5888 goto done;
5891 if (TAILQ_EMPTY(&commitable_paths)) {
5892 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5893 goto done;
5896 TAILQ_FOREACH(pe, paths, entry) {
5897 err = check_path_is_commitable(pe->path, &commitable_paths);
5898 if (err)
5899 goto done;
5902 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5903 struct got_commitable *ct = pe->data;
5904 const char *ct_path = ct->in_repo_path;
5906 while (ct_path[0] == '/')
5907 ct_path++;
5908 err = check_out_of_date(ct_path, ct->status,
5909 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5910 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5911 if (err)
5912 goto done;
5916 err = commit_worktree(new_commit_id, &commitable_paths,
5917 head_commit_id, NULL, worktree, author, committer,
5918 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5919 if (err)
5920 goto done;
5922 err = update_fileindex_after_commit(worktree, &commitable_paths,
5923 *new_commit_id, fileindex, have_staged_files);
5924 sync_err = sync_fileindex(fileindex, fileindex_path);
5925 if (sync_err && err == NULL)
5926 err = sync_err;
5927 done:
5928 if (fileindex)
5929 got_fileindex_free(fileindex);
5930 free(fileindex_path);
5931 unlockerr = lock_worktree(worktree, LOCK_SH);
5932 if (unlockerr && err == NULL)
5933 err = unlockerr;
5934 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5935 struct got_commitable *ct = pe->data;
5936 free_commitable(ct);
5938 got_pathlist_free(&commitable_paths);
5939 return err;
5942 const char *
5943 got_commitable_get_path(struct got_commitable *ct)
5945 return ct->path;
5948 unsigned int
5949 got_commitable_get_status(struct got_commitable *ct)
5951 return ct->status;
5954 struct check_rebase_ok_arg {
5955 struct got_worktree *worktree;
5956 struct got_repository *repo;
5959 static const struct got_error *
5960 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5962 const struct got_error *err = NULL;
5963 struct check_rebase_ok_arg *a = arg;
5964 unsigned char status;
5965 struct stat sb;
5966 char *ondisk_path;
5968 /* Reject rebase of a work tree with mixed base commits. */
5969 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5970 SHA1_DIGEST_LENGTH))
5971 return got_error(GOT_ERR_MIXED_COMMITS);
5973 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5974 == -1)
5975 return got_error_from_errno("asprintf");
5977 /* Reject rebase of a work tree with modified or staged files. */
5978 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5979 free(ondisk_path);
5980 if (err)
5981 return err;
5983 if (status != GOT_STATUS_NO_CHANGE)
5984 return got_error(GOT_ERR_MODIFIED);
5985 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5986 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5988 return NULL;
5991 const struct got_error *
5992 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5993 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5994 struct got_worktree *worktree, struct got_reference *branch,
5995 struct got_repository *repo)
5997 const struct got_error *err = NULL;
5998 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5999 char *branch_ref_name = NULL;
6000 char *fileindex_path = NULL;
6001 struct check_rebase_ok_arg ok_arg;
6002 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6003 struct got_object_id *wt_branch_tip = NULL;
6005 *new_base_branch_ref = NULL;
6006 *tmp_branch = NULL;
6007 *fileindex = NULL;
6009 err = lock_worktree(worktree, LOCK_EX);
6010 if (err)
6011 return err;
6013 err = open_fileindex(fileindex, &fileindex_path, worktree);
6014 if (err)
6015 goto done;
6017 ok_arg.worktree = worktree;
6018 ok_arg.repo = repo;
6019 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6020 &ok_arg);
6021 if (err)
6022 goto done;
6024 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6025 if (err)
6026 goto done;
6028 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6029 if (err)
6030 goto done;
6032 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6033 if (err)
6034 goto done;
6036 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6037 0);
6038 if (err)
6039 goto done;
6041 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6042 if (err)
6043 goto done;
6044 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6045 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6046 goto done;
6049 err = got_ref_alloc_symref(new_base_branch_ref,
6050 new_base_branch_ref_name, wt_branch);
6051 if (err)
6052 goto done;
6053 err = got_ref_write(*new_base_branch_ref, repo);
6054 if (err)
6055 goto done;
6057 /* TODO Lock original branch's ref while rebasing? */
6059 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6060 if (err)
6061 goto done;
6063 err = got_ref_write(branch_ref, repo);
6064 if (err)
6065 goto done;
6067 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6068 worktree->base_commit_id);
6069 if (err)
6070 goto done;
6071 err = got_ref_write(*tmp_branch, repo);
6072 if (err)
6073 goto done;
6075 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6076 if (err)
6077 goto done;
6078 done:
6079 free(fileindex_path);
6080 free(tmp_branch_name);
6081 free(new_base_branch_ref_name);
6082 free(branch_ref_name);
6083 if (branch_ref)
6084 got_ref_close(branch_ref);
6085 if (wt_branch)
6086 got_ref_close(wt_branch);
6087 free(wt_branch_tip);
6088 if (err) {
6089 if (*new_base_branch_ref) {
6090 got_ref_close(*new_base_branch_ref);
6091 *new_base_branch_ref = NULL;
6093 if (*tmp_branch) {
6094 got_ref_close(*tmp_branch);
6095 *tmp_branch = NULL;
6097 if (*fileindex) {
6098 got_fileindex_free(*fileindex);
6099 *fileindex = NULL;
6101 lock_worktree(worktree, LOCK_SH);
6103 return err;
6106 const struct got_error *
6107 got_worktree_rebase_continue(struct got_object_id **commit_id,
6108 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6109 struct got_reference **branch, struct got_fileindex **fileindex,
6110 struct got_worktree *worktree, struct got_repository *repo)
6112 const struct got_error *err;
6113 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6114 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6115 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6116 char *fileindex_path = NULL;
6117 int have_staged_files = 0;
6119 *commit_id = NULL;
6120 *new_base_branch = NULL;
6121 *tmp_branch = NULL;
6122 *branch = NULL;
6123 *fileindex = NULL;
6125 err = lock_worktree(worktree, LOCK_EX);
6126 if (err)
6127 return err;
6129 err = open_fileindex(fileindex, &fileindex_path, worktree);
6130 if (err)
6131 goto done;
6133 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6134 &have_staged_files);
6135 if (err && err->code != GOT_ERR_CANCELLED)
6136 goto done;
6137 if (have_staged_files) {
6138 err = got_error(GOT_ERR_STAGED_PATHS);
6139 goto done;
6142 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6143 if (err)
6144 goto done;
6146 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6147 if (err)
6148 goto done;
6150 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6151 if (err)
6152 goto done;
6154 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6155 if (err)
6156 goto done;
6158 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6159 if (err)
6160 goto done;
6162 err = got_ref_open(branch, repo,
6163 got_ref_get_symref_target(branch_ref), 0);
6164 if (err)
6165 goto done;
6167 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6168 if (err)
6169 goto done;
6171 err = got_ref_resolve(commit_id, repo, commit_ref);
6172 if (err)
6173 goto done;
6175 err = got_ref_open(new_base_branch, repo,
6176 new_base_branch_ref_name, 0);
6177 if (err)
6178 goto done;
6180 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6181 if (err)
6182 goto done;
6183 done:
6184 free(commit_ref_name);
6185 free(branch_ref_name);
6186 free(fileindex_path);
6187 if (commit_ref)
6188 got_ref_close(commit_ref);
6189 if (branch_ref)
6190 got_ref_close(branch_ref);
6191 if (err) {
6192 free(*commit_id);
6193 *commit_id = NULL;
6194 if (*tmp_branch) {
6195 got_ref_close(*tmp_branch);
6196 *tmp_branch = NULL;
6198 if (*new_base_branch) {
6199 got_ref_close(*new_base_branch);
6200 *new_base_branch = NULL;
6202 if (*branch) {
6203 got_ref_close(*branch);
6204 *branch = NULL;
6206 if (*fileindex) {
6207 got_fileindex_free(*fileindex);
6208 *fileindex = NULL;
6210 lock_worktree(worktree, LOCK_SH);
6212 return err;
6215 const struct got_error *
6216 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6218 const struct got_error *err;
6219 char *tmp_branch_name = NULL;
6221 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6222 if (err)
6223 return err;
6225 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6226 free(tmp_branch_name);
6227 return NULL;
6230 static const struct got_error *
6231 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6232 char **logmsg, void *arg)
6234 *logmsg = arg;
6235 return NULL;
6238 static const struct got_error *
6239 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6240 const char *path, struct got_object_id *blob_id,
6241 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6242 int dirfd, const char *de_name)
6244 return NULL;
6247 struct collect_merged_paths_arg {
6248 got_worktree_checkout_cb progress_cb;
6249 void *progress_arg;
6250 struct got_pathlist_head *merged_paths;
6253 static const struct got_error *
6254 collect_merged_paths(void *arg, unsigned char status, const char *path)
6256 const struct got_error *err;
6257 struct collect_merged_paths_arg *a = arg;
6258 char *p;
6259 struct got_pathlist_entry *new;
6261 err = (*a->progress_cb)(a->progress_arg, status, path);
6262 if (err)
6263 return err;
6265 if (status != GOT_STATUS_MERGE &&
6266 status != GOT_STATUS_ADD &&
6267 status != GOT_STATUS_DELETE &&
6268 status != GOT_STATUS_CONFLICT)
6269 return NULL;
6271 p = strdup(path);
6272 if (p == NULL)
6273 return got_error_from_errno("strdup");
6275 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6276 if (err || new == NULL)
6277 free(p);
6278 return err;
6281 void
6282 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6284 struct got_pathlist_entry *pe;
6286 TAILQ_FOREACH(pe, merged_paths, entry)
6287 free((char *)pe->path);
6289 got_pathlist_free(merged_paths);
6292 static const struct got_error *
6293 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6294 int is_rebase, struct got_repository *repo)
6296 const struct got_error *err;
6297 struct got_reference *commit_ref = NULL;
6299 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6300 if (err) {
6301 if (err->code != GOT_ERR_NOT_REF)
6302 goto done;
6303 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6304 if (err)
6305 goto done;
6306 err = got_ref_write(commit_ref, repo);
6307 if (err)
6308 goto done;
6309 } else if (is_rebase) {
6310 struct got_object_id *stored_id;
6311 int cmp;
6313 err = got_ref_resolve(&stored_id, repo, commit_ref);
6314 if (err)
6315 goto done;
6316 cmp = got_object_id_cmp(commit_id, stored_id);
6317 free(stored_id);
6318 if (cmp != 0) {
6319 err = got_error(GOT_ERR_REBASE_COMMITID);
6320 goto done;
6323 done:
6324 if (commit_ref)
6325 got_ref_close(commit_ref);
6326 return err;
6329 static const struct got_error *
6330 rebase_merge_files(struct got_pathlist_head *merged_paths,
6331 const char *commit_ref_name, struct got_worktree *worktree,
6332 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6333 struct got_object_id *commit_id, struct got_repository *repo,
6334 got_worktree_checkout_cb progress_cb, void *progress_arg,
6335 got_cancel_cb cancel_cb, void *cancel_arg)
6337 const struct got_error *err;
6338 struct got_reference *commit_ref = NULL;
6339 struct collect_merged_paths_arg cmp_arg;
6340 char *fileindex_path;
6342 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6344 err = get_fileindex_path(&fileindex_path, worktree);
6345 if (err)
6346 return err;
6348 cmp_arg.progress_cb = progress_cb;
6349 cmp_arg.progress_arg = progress_arg;
6350 cmp_arg.merged_paths = merged_paths;
6351 err = merge_files(worktree, fileindex, fileindex_path,
6352 parent_commit_id, commit_id, repo, collect_merged_paths,
6353 &cmp_arg, cancel_cb, cancel_arg);
6354 if (commit_ref)
6355 got_ref_close(commit_ref);
6356 return err;
6359 const struct got_error *
6360 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6361 struct got_worktree *worktree, struct got_fileindex *fileindex,
6362 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6363 struct got_repository *repo,
6364 got_worktree_checkout_cb progress_cb, void *progress_arg,
6365 got_cancel_cb cancel_cb, void *cancel_arg)
6367 const struct got_error *err;
6368 char *commit_ref_name;
6370 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6371 if (err)
6372 return err;
6374 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6375 if (err)
6376 goto done;
6378 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6379 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6380 progress_arg, cancel_cb, cancel_arg);
6381 done:
6382 free(commit_ref_name);
6383 return err;
6386 const struct got_error *
6387 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6388 struct got_worktree *worktree, struct got_fileindex *fileindex,
6389 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6390 struct got_repository *repo,
6391 got_worktree_checkout_cb progress_cb, void *progress_arg,
6392 got_cancel_cb cancel_cb, void *cancel_arg)
6394 const struct got_error *err;
6395 char *commit_ref_name;
6397 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6398 if (err)
6399 return err;
6401 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6402 if (err)
6403 goto done;
6405 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6406 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6407 progress_arg, cancel_cb, cancel_arg);
6408 done:
6409 free(commit_ref_name);
6410 return err;
6413 static const struct got_error *
6414 rebase_commit(struct got_object_id **new_commit_id,
6415 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6416 struct got_worktree *worktree, struct got_fileindex *fileindex,
6417 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6418 const char *new_logmsg, struct got_repository *repo)
6420 const struct got_error *err, *sync_err;
6421 struct got_pathlist_head commitable_paths;
6422 struct collect_commitables_arg cc_arg;
6423 char *fileindex_path = NULL;
6424 struct got_reference *head_ref = NULL;
6425 struct got_object_id *head_commit_id = NULL;
6426 char *logmsg = NULL;
6428 TAILQ_INIT(&commitable_paths);
6429 *new_commit_id = NULL;
6431 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6433 err = get_fileindex_path(&fileindex_path, worktree);
6434 if (err)
6435 return err;
6437 cc_arg.commitable_paths = &commitable_paths;
6438 cc_arg.worktree = worktree;
6439 cc_arg.repo = repo;
6440 cc_arg.have_staged_files = 0;
6442 * If possible get the status of individual files directly to
6443 * avoid crawling the entire work tree once per rebased commit.
6445 * Ideally, merged_paths would contain a list of commitables
6446 * we could use so we could skip worktree_status() entirely.
6447 * However, we would then need carefully keep track of cumulative
6448 * effects of operations such as file additions and deletions
6449 * in 'got histedit -f' (folding multiple commits into one),
6450 * and this extra complexity is not really worth it.
6452 if (merged_paths) {
6453 struct got_pathlist_entry *pe;
6454 TAILQ_FOREACH(pe, merged_paths, entry) {
6455 err = worktree_status(worktree, pe->path, fileindex,
6456 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6457 0);
6458 if (err)
6459 goto done;
6461 } else {
6462 err = worktree_status(worktree, "", fileindex, repo,
6463 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6464 if (err)
6465 goto done;
6468 if (TAILQ_EMPTY(&commitable_paths)) {
6469 /* No-op change; commit will be elided. */
6470 err = got_ref_delete(commit_ref, repo);
6471 if (err)
6472 goto done;
6473 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6474 goto done;
6477 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6478 if (err)
6479 goto done;
6481 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6482 if (err)
6483 goto done;
6485 if (new_logmsg) {
6486 logmsg = strdup(new_logmsg);
6487 if (logmsg == NULL) {
6488 err = got_error_from_errno("strdup");
6489 goto done;
6491 } else {
6492 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6493 if (err)
6494 goto done;
6497 /* NB: commit_worktree will call free(logmsg) */
6498 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6499 NULL, worktree, got_object_commit_get_author(orig_commit),
6500 got_object_commit_get_committer(orig_commit),
6501 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6502 if (err)
6503 goto done;
6505 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6506 if (err)
6507 goto done;
6509 err = got_ref_delete(commit_ref, repo);
6510 if (err)
6511 goto done;
6513 err = update_fileindex_after_commit(worktree, &commitable_paths,
6514 *new_commit_id, fileindex, 0);
6515 sync_err = sync_fileindex(fileindex, fileindex_path);
6516 if (sync_err && err == NULL)
6517 err = sync_err;
6518 done:
6519 free(fileindex_path);
6520 free(head_commit_id);
6521 if (head_ref)
6522 got_ref_close(head_ref);
6523 if (err) {
6524 free(*new_commit_id);
6525 *new_commit_id = NULL;
6527 return err;
6530 const struct got_error *
6531 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6532 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6533 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6534 struct got_commit_object *orig_commit,
6535 struct got_object_id *orig_commit_id, struct got_repository *repo)
6537 const struct got_error *err;
6538 char *commit_ref_name;
6539 struct got_reference *commit_ref = NULL;
6540 struct got_object_id *commit_id = NULL;
6542 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6543 if (err)
6544 return err;
6546 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6547 if (err)
6548 goto done;
6549 err = got_ref_resolve(&commit_id, repo, commit_ref);
6550 if (err)
6551 goto done;
6552 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6553 err = got_error(GOT_ERR_REBASE_COMMITID);
6554 goto done;
6557 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6558 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6559 done:
6560 if (commit_ref)
6561 got_ref_close(commit_ref);
6562 free(commit_ref_name);
6563 free(commit_id);
6564 return err;
6567 const struct got_error *
6568 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6569 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6570 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6571 struct got_commit_object *orig_commit,
6572 struct got_object_id *orig_commit_id, const char *new_logmsg,
6573 struct got_repository *repo)
6575 const struct got_error *err;
6576 char *commit_ref_name;
6577 struct got_reference *commit_ref = NULL;
6579 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6580 if (err)
6581 return err;
6583 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6584 if (err)
6585 goto done;
6587 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6588 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6589 done:
6590 if (commit_ref)
6591 got_ref_close(commit_ref);
6592 free(commit_ref_name);
6593 return err;
6596 const struct got_error *
6597 got_worktree_rebase_postpone(struct got_worktree *worktree,
6598 struct got_fileindex *fileindex)
6600 if (fileindex)
6601 got_fileindex_free(fileindex);
6602 return lock_worktree(worktree, LOCK_SH);
6605 static const struct got_error *
6606 delete_ref(const char *name, struct got_repository *repo)
6608 const struct got_error *err;
6609 struct got_reference *ref;
6611 err = got_ref_open(&ref, repo, name, 0);
6612 if (err) {
6613 if (err->code == GOT_ERR_NOT_REF)
6614 return NULL;
6615 return err;
6618 err = got_ref_delete(ref, repo);
6619 got_ref_close(ref);
6620 return err;
6623 static const struct got_error *
6624 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6626 const struct got_error *err;
6627 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6628 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6630 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6631 if (err)
6632 goto done;
6633 err = delete_ref(tmp_branch_name, repo);
6634 if (err)
6635 goto done;
6637 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6638 if (err)
6639 goto done;
6640 err = delete_ref(new_base_branch_ref_name, repo);
6641 if (err)
6642 goto done;
6644 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6645 if (err)
6646 goto done;
6647 err = delete_ref(branch_ref_name, repo);
6648 if (err)
6649 goto done;
6651 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6652 if (err)
6653 goto done;
6654 err = delete_ref(commit_ref_name, repo);
6655 if (err)
6656 goto done;
6658 done:
6659 free(tmp_branch_name);
6660 free(new_base_branch_ref_name);
6661 free(branch_ref_name);
6662 free(commit_ref_name);
6663 return err;
6666 const struct got_error *
6667 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6668 struct got_object_id *new_commit_id, struct got_repository *repo)
6670 const struct got_error *err;
6671 struct got_reference *ref = NULL;
6672 struct got_object_id *old_commit_id = NULL;
6673 const char *branch_name = NULL;
6674 char *new_id_str = NULL;
6675 char *refname = NULL;
6677 branch_name = got_ref_get_name(branch);
6678 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6679 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6680 branch_name += 11;
6682 err = got_object_id_str(&new_id_str, new_commit_id);
6683 if (err)
6684 return err;
6686 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6687 new_id_str) == -1) {
6688 err = got_error_from_errno("asprintf");
6689 goto done;
6692 err = got_ref_resolve(&old_commit_id, repo, branch);
6693 if (err)
6694 goto done;
6696 err = got_ref_alloc(&ref, refname, old_commit_id);
6697 if (err)
6698 goto done;
6700 err = got_ref_write(ref, repo);
6701 done:
6702 free(new_id_str);
6703 free(refname);
6704 free(old_commit_id);
6705 if (ref)
6706 got_ref_close(ref);
6707 return err;
6710 const struct got_error *
6711 got_worktree_rebase_complete(struct got_worktree *worktree,
6712 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6713 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6714 struct got_repository *repo, int create_backup)
6716 const struct got_error *err, *unlockerr, *sync_err;
6717 struct got_object_id *new_head_commit_id = NULL;
6718 char *fileindex_path = NULL;
6720 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6721 if (err)
6722 return err;
6724 if (create_backup) {
6725 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6726 rebased_branch, new_head_commit_id, repo);
6727 if (err)
6728 goto done;
6731 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6732 if (err)
6733 goto done;
6735 err = got_ref_write(rebased_branch, repo);
6736 if (err)
6737 goto done;
6739 err = got_worktree_set_head_ref(worktree, rebased_branch);
6740 if (err)
6741 goto done;
6743 err = delete_rebase_refs(worktree, repo);
6744 if (err)
6745 goto done;
6747 err = get_fileindex_path(&fileindex_path, worktree);
6748 if (err)
6749 goto done;
6750 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6751 sync_err = sync_fileindex(fileindex, fileindex_path);
6752 if (sync_err && err == NULL)
6753 err = sync_err;
6754 done:
6755 got_fileindex_free(fileindex);
6756 free(fileindex_path);
6757 free(new_head_commit_id);
6758 unlockerr = lock_worktree(worktree, LOCK_SH);
6759 if (unlockerr && err == NULL)
6760 err = unlockerr;
6761 return err;
6764 const struct got_error *
6765 got_worktree_rebase_abort(struct got_worktree *worktree,
6766 struct got_fileindex *fileindex, struct got_repository *repo,
6767 struct got_reference *new_base_branch,
6768 got_worktree_checkout_cb progress_cb, void *progress_arg)
6770 const struct got_error *err, *unlockerr, *sync_err;
6771 struct got_reference *resolved = NULL;
6772 struct got_object_id *commit_id = NULL;
6773 char *fileindex_path = NULL;
6774 struct revert_file_args rfa;
6775 struct got_object_id *tree_id = NULL;
6777 err = lock_worktree(worktree, LOCK_EX);
6778 if (err)
6779 return err;
6781 err = got_ref_open(&resolved, repo,
6782 got_ref_get_symref_target(new_base_branch), 0);
6783 if (err)
6784 goto done;
6786 err = got_worktree_set_head_ref(worktree, resolved);
6787 if (err)
6788 goto done;
6791 * XXX commits to the base branch could have happened while
6792 * we were busy rebasing; should we store the original commit ID
6793 * when rebase begins and read it back here?
6795 err = got_ref_resolve(&commit_id, repo, resolved);
6796 if (err)
6797 goto done;
6799 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6800 if (err)
6801 goto done;
6803 err = got_object_id_by_path(&tree_id, repo,
6804 worktree->base_commit_id, worktree->path_prefix);
6805 if (err)
6806 goto done;
6808 err = delete_rebase_refs(worktree, repo);
6809 if (err)
6810 goto done;
6812 err = get_fileindex_path(&fileindex_path, worktree);
6813 if (err)
6814 goto done;
6816 rfa.worktree = worktree;
6817 rfa.fileindex = fileindex;
6818 rfa.progress_cb = progress_cb;
6819 rfa.progress_arg = progress_arg;
6820 rfa.patch_cb = NULL;
6821 rfa.patch_arg = NULL;
6822 rfa.repo = repo;
6823 rfa.unlink_added_files = 0;
6824 err = worktree_status(worktree, "", fileindex, repo,
6825 revert_file, &rfa, NULL, NULL, 0, 0);
6826 if (err)
6827 goto sync;
6829 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6830 repo, progress_cb, progress_arg, NULL, NULL);
6831 sync:
6832 sync_err = sync_fileindex(fileindex, fileindex_path);
6833 if (sync_err && err == NULL)
6834 err = sync_err;
6835 done:
6836 got_ref_close(resolved);
6837 free(tree_id);
6838 free(commit_id);
6839 if (fileindex)
6840 got_fileindex_free(fileindex);
6841 free(fileindex_path);
6843 unlockerr = lock_worktree(worktree, LOCK_SH);
6844 if (unlockerr && err == NULL)
6845 err = unlockerr;
6846 return err;
6849 const struct got_error *
6850 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6851 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6852 struct got_fileindex **fileindex, struct got_worktree *worktree,
6853 struct got_repository *repo)
6855 const struct got_error *err = NULL;
6856 char *tmp_branch_name = NULL;
6857 char *branch_ref_name = NULL;
6858 char *base_commit_ref_name = NULL;
6859 char *fileindex_path = NULL;
6860 struct check_rebase_ok_arg ok_arg;
6861 struct got_reference *wt_branch = NULL;
6862 struct got_reference *base_commit_ref = NULL;
6864 *tmp_branch = NULL;
6865 *branch_ref = NULL;
6866 *base_commit_id = NULL;
6867 *fileindex = NULL;
6869 err = lock_worktree(worktree, LOCK_EX);
6870 if (err)
6871 return err;
6873 err = open_fileindex(fileindex, &fileindex_path, worktree);
6874 if (err)
6875 goto done;
6877 ok_arg.worktree = worktree;
6878 ok_arg.repo = repo;
6879 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6880 &ok_arg);
6881 if (err)
6882 goto done;
6884 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6885 if (err)
6886 goto done;
6888 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6889 if (err)
6890 goto done;
6892 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6893 worktree);
6894 if (err)
6895 goto done;
6897 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6898 0);
6899 if (err)
6900 goto done;
6902 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6903 if (err)
6904 goto done;
6906 err = got_ref_write(*branch_ref, repo);
6907 if (err)
6908 goto done;
6910 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6911 worktree->base_commit_id);
6912 if (err)
6913 goto done;
6914 err = got_ref_write(base_commit_ref, repo);
6915 if (err)
6916 goto done;
6917 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6918 if (*base_commit_id == NULL) {
6919 err = got_error_from_errno("got_object_id_dup");
6920 goto done;
6923 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6924 worktree->base_commit_id);
6925 if (err)
6926 goto done;
6927 err = got_ref_write(*tmp_branch, repo);
6928 if (err)
6929 goto done;
6931 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6932 if (err)
6933 goto done;
6934 done:
6935 free(fileindex_path);
6936 free(tmp_branch_name);
6937 free(branch_ref_name);
6938 free(base_commit_ref_name);
6939 if (wt_branch)
6940 got_ref_close(wt_branch);
6941 if (err) {
6942 if (*branch_ref) {
6943 got_ref_close(*branch_ref);
6944 *branch_ref = NULL;
6946 if (*tmp_branch) {
6947 got_ref_close(*tmp_branch);
6948 *tmp_branch = NULL;
6950 free(*base_commit_id);
6951 if (*fileindex) {
6952 got_fileindex_free(*fileindex);
6953 *fileindex = NULL;
6955 lock_worktree(worktree, LOCK_SH);
6957 return err;
6960 const struct got_error *
6961 got_worktree_histedit_postpone(struct got_worktree *worktree,
6962 struct got_fileindex *fileindex)
6964 if (fileindex)
6965 got_fileindex_free(fileindex);
6966 return lock_worktree(worktree, LOCK_SH);
6969 const struct got_error *
6970 got_worktree_histedit_in_progress(int *in_progress,
6971 struct got_worktree *worktree)
6973 const struct got_error *err;
6974 char *tmp_branch_name = NULL;
6976 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6977 if (err)
6978 return err;
6980 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6981 free(tmp_branch_name);
6982 return NULL;
6985 const struct got_error *
6986 got_worktree_histedit_continue(struct got_object_id **commit_id,
6987 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6988 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6989 struct got_worktree *worktree, struct got_repository *repo)
6991 const struct got_error *err;
6992 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6993 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6994 struct got_reference *commit_ref = NULL;
6995 struct got_reference *base_commit_ref = NULL;
6996 char *fileindex_path = NULL;
6997 int have_staged_files = 0;
6999 *commit_id = NULL;
7000 *tmp_branch = NULL;
7001 *base_commit_id = NULL;
7002 *fileindex = NULL;
7004 err = lock_worktree(worktree, LOCK_EX);
7005 if (err)
7006 return err;
7008 err = open_fileindex(fileindex, &fileindex_path, worktree);
7009 if (err)
7010 goto done;
7012 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7013 &have_staged_files);
7014 if (err && err->code != GOT_ERR_CANCELLED)
7015 goto done;
7016 if (have_staged_files) {
7017 err = got_error(GOT_ERR_STAGED_PATHS);
7018 goto done;
7021 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7022 if (err)
7023 goto done;
7025 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7026 if (err)
7027 goto done;
7029 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7030 if (err)
7031 goto done;
7033 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7034 worktree);
7035 if (err)
7036 goto done;
7038 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7039 if (err)
7040 goto done;
7042 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7043 if (err)
7044 goto done;
7045 err = got_ref_resolve(commit_id, repo, commit_ref);
7046 if (err)
7047 goto done;
7049 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7050 if (err)
7051 goto done;
7052 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7053 if (err)
7054 goto done;
7056 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7057 if (err)
7058 goto done;
7059 done:
7060 free(commit_ref_name);
7061 free(branch_ref_name);
7062 free(fileindex_path);
7063 if (commit_ref)
7064 got_ref_close(commit_ref);
7065 if (base_commit_ref)
7066 got_ref_close(base_commit_ref);
7067 if (err) {
7068 free(*commit_id);
7069 *commit_id = NULL;
7070 free(*base_commit_id);
7071 *base_commit_id = NULL;
7072 if (*tmp_branch) {
7073 got_ref_close(*tmp_branch);
7074 *tmp_branch = NULL;
7076 if (*fileindex) {
7077 got_fileindex_free(*fileindex);
7078 *fileindex = NULL;
7080 lock_worktree(worktree, LOCK_EX);
7082 return err;
7085 static const struct got_error *
7086 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7088 const struct got_error *err;
7089 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7090 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7092 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7093 if (err)
7094 goto done;
7095 err = delete_ref(tmp_branch_name, repo);
7096 if (err)
7097 goto done;
7099 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7100 worktree);
7101 if (err)
7102 goto done;
7103 err = delete_ref(base_commit_ref_name, repo);
7104 if (err)
7105 goto done;
7107 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7108 if (err)
7109 goto done;
7110 err = delete_ref(branch_ref_name, repo);
7111 if (err)
7112 goto done;
7114 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7115 if (err)
7116 goto done;
7117 err = delete_ref(commit_ref_name, repo);
7118 if (err)
7119 goto done;
7120 done:
7121 free(tmp_branch_name);
7122 free(base_commit_ref_name);
7123 free(branch_ref_name);
7124 free(commit_ref_name);
7125 return err;
7128 const struct got_error *
7129 got_worktree_histedit_abort(struct got_worktree *worktree,
7130 struct got_fileindex *fileindex, struct got_repository *repo,
7131 struct got_reference *branch, struct got_object_id *base_commit_id,
7132 got_worktree_checkout_cb progress_cb, void *progress_arg)
7134 const struct got_error *err, *unlockerr, *sync_err;
7135 struct got_reference *resolved = NULL;
7136 char *fileindex_path = NULL;
7137 struct got_object_id *tree_id = NULL;
7138 struct revert_file_args rfa;
7140 err = lock_worktree(worktree, LOCK_EX);
7141 if (err)
7142 return err;
7144 err = got_ref_open(&resolved, repo,
7145 got_ref_get_symref_target(branch), 0);
7146 if (err)
7147 goto done;
7149 err = got_worktree_set_head_ref(worktree, resolved);
7150 if (err)
7151 goto done;
7153 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7154 if (err)
7155 goto done;
7157 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7158 worktree->path_prefix);
7159 if (err)
7160 goto done;
7162 err = delete_histedit_refs(worktree, repo);
7163 if (err)
7164 goto done;
7166 err = get_fileindex_path(&fileindex_path, worktree);
7167 if (err)
7168 goto done;
7170 rfa.worktree = worktree;
7171 rfa.fileindex = fileindex;
7172 rfa.progress_cb = progress_cb;
7173 rfa.progress_arg = progress_arg;
7174 rfa.patch_cb = NULL;
7175 rfa.patch_arg = NULL;
7176 rfa.repo = repo;
7177 rfa.unlink_added_files = 0;
7178 err = worktree_status(worktree, "", fileindex, repo,
7179 revert_file, &rfa, NULL, NULL, 0, 0);
7180 if (err)
7181 goto sync;
7183 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7184 repo, progress_cb, progress_arg, NULL, NULL);
7185 sync:
7186 sync_err = sync_fileindex(fileindex, fileindex_path);
7187 if (sync_err && err == NULL)
7188 err = sync_err;
7189 done:
7190 got_ref_close(resolved);
7191 free(tree_id);
7192 free(fileindex_path);
7194 unlockerr = lock_worktree(worktree, LOCK_SH);
7195 if (unlockerr && err == NULL)
7196 err = unlockerr;
7197 return err;
7200 const struct got_error *
7201 got_worktree_histedit_complete(struct got_worktree *worktree,
7202 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7203 struct got_reference *edited_branch, struct got_repository *repo)
7205 const struct got_error *err, *unlockerr, *sync_err;
7206 struct got_object_id *new_head_commit_id = NULL;
7207 struct got_reference *resolved = NULL;
7208 char *fileindex_path = NULL;
7210 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7211 if (err)
7212 return err;
7214 err = got_ref_open(&resolved, repo,
7215 got_ref_get_symref_target(edited_branch), 0);
7216 if (err)
7217 goto done;
7219 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7220 resolved, new_head_commit_id, repo);
7221 if (err)
7222 goto done;
7224 err = got_ref_change_ref(resolved, new_head_commit_id);
7225 if (err)
7226 goto done;
7228 err = got_ref_write(resolved, repo);
7229 if (err)
7230 goto done;
7232 err = got_worktree_set_head_ref(worktree, resolved);
7233 if (err)
7234 goto done;
7236 err = delete_histedit_refs(worktree, repo);
7237 if (err)
7238 goto done;
7240 err = get_fileindex_path(&fileindex_path, worktree);
7241 if (err)
7242 goto done;
7243 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7244 sync_err = sync_fileindex(fileindex, fileindex_path);
7245 if (sync_err && err == NULL)
7246 err = sync_err;
7247 done:
7248 got_fileindex_free(fileindex);
7249 free(fileindex_path);
7250 free(new_head_commit_id);
7251 unlockerr = lock_worktree(worktree, LOCK_SH);
7252 if (unlockerr && err == NULL)
7253 err = unlockerr;
7254 return err;
7257 const struct got_error *
7258 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7259 struct got_object_id *commit_id, struct got_repository *repo)
7261 const struct got_error *err;
7262 char *commit_ref_name;
7264 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7265 if (err)
7266 return err;
7268 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7269 if (err)
7270 goto done;
7272 err = delete_ref(commit_ref_name, repo);
7273 done:
7274 free(commit_ref_name);
7275 return err;
7278 const struct got_error *
7279 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7280 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7281 struct got_worktree *worktree, const char *refname,
7282 struct got_repository *repo)
7284 const struct got_error *err = NULL;
7285 char *fileindex_path = NULL;
7286 struct check_rebase_ok_arg ok_arg;
7288 *fileindex = NULL;
7289 *branch_ref = NULL;
7290 *base_branch_ref = NULL;
7292 err = lock_worktree(worktree, LOCK_EX);
7293 if (err)
7294 return err;
7296 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7297 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7298 "cannot integrate a branch into itself; "
7299 "update -b or different branch name required");
7300 goto done;
7303 err = open_fileindex(fileindex, &fileindex_path, worktree);
7304 if (err)
7305 goto done;
7307 /* Preconditions are the same as for rebase. */
7308 ok_arg.worktree = worktree;
7309 ok_arg.repo = repo;
7310 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7311 &ok_arg);
7312 if (err)
7313 goto done;
7315 err = got_ref_open(branch_ref, repo, refname, 1);
7316 if (err)
7317 goto done;
7319 err = got_ref_open(base_branch_ref, repo,
7320 got_worktree_get_head_ref_name(worktree), 1);
7321 done:
7322 if (err) {
7323 if (*branch_ref) {
7324 got_ref_close(*branch_ref);
7325 *branch_ref = NULL;
7327 if (*base_branch_ref) {
7328 got_ref_close(*base_branch_ref);
7329 *base_branch_ref = NULL;
7331 if (*fileindex) {
7332 got_fileindex_free(*fileindex);
7333 *fileindex = NULL;
7335 lock_worktree(worktree, LOCK_SH);
7337 return err;
7340 const struct got_error *
7341 got_worktree_integrate_continue(struct got_worktree *worktree,
7342 struct got_fileindex *fileindex, struct got_repository *repo,
7343 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7344 got_worktree_checkout_cb progress_cb, void *progress_arg,
7345 got_cancel_cb cancel_cb, void *cancel_arg)
7347 const struct got_error *err = NULL, *sync_err, *unlockerr;
7348 char *fileindex_path = NULL;
7349 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7351 err = get_fileindex_path(&fileindex_path, worktree);
7352 if (err)
7353 goto done;
7355 err = got_ref_resolve(&commit_id, repo, branch_ref);
7356 if (err)
7357 goto done;
7359 err = got_object_id_by_path(&tree_id, repo, commit_id,
7360 worktree->path_prefix);
7361 if (err)
7362 goto done;
7364 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7365 if (err)
7366 goto done;
7368 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7369 progress_cb, progress_arg, cancel_cb, cancel_arg);
7370 if (err)
7371 goto sync;
7373 err = got_ref_change_ref(base_branch_ref, commit_id);
7374 if (err)
7375 goto sync;
7377 err = got_ref_write(base_branch_ref, repo);
7378 if (err)
7379 goto sync;
7381 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7382 sync:
7383 sync_err = sync_fileindex(fileindex, fileindex_path);
7384 if (sync_err && err == NULL)
7385 err = sync_err;
7387 done:
7388 unlockerr = got_ref_unlock(branch_ref);
7389 if (unlockerr && err == NULL)
7390 err = unlockerr;
7391 got_ref_close(branch_ref);
7393 unlockerr = got_ref_unlock(base_branch_ref);
7394 if (unlockerr && err == NULL)
7395 err = unlockerr;
7396 got_ref_close(base_branch_ref);
7398 got_fileindex_free(fileindex);
7399 free(fileindex_path);
7400 free(tree_id);
7402 unlockerr = lock_worktree(worktree, LOCK_SH);
7403 if (unlockerr && err == NULL)
7404 err = unlockerr;
7405 return err;
7408 const struct got_error *
7409 got_worktree_integrate_abort(struct got_worktree *worktree,
7410 struct got_fileindex *fileindex, struct got_repository *repo,
7411 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7413 const struct got_error *err = NULL, *unlockerr = NULL;
7415 got_fileindex_free(fileindex);
7417 err = lock_worktree(worktree, LOCK_SH);
7419 unlockerr = got_ref_unlock(branch_ref);
7420 if (unlockerr && err == NULL)
7421 err = unlockerr;
7422 got_ref_close(branch_ref);
7424 unlockerr = got_ref_unlock(base_branch_ref);
7425 if (unlockerr && err == NULL)
7426 err = unlockerr;
7427 got_ref_close(base_branch_ref);
7429 return err;
7432 const struct got_error *
7433 got_worktree_merge_postpone(struct got_worktree *worktree,
7434 struct got_fileindex *fileindex)
7436 const struct got_error *err, *sync_err;
7437 char *fileindex_path = NULL;
7439 err = get_fileindex_path(&fileindex_path, worktree);
7440 if (err)
7441 goto done;
7443 sync_err = sync_fileindex(fileindex, fileindex_path);
7445 err = lock_worktree(worktree, LOCK_SH);
7446 if (sync_err && err == NULL)
7447 err = sync_err;
7448 done:
7449 got_fileindex_free(fileindex);
7450 free(fileindex_path);
7451 return err;
7454 static const struct got_error *
7455 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7457 const struct got_error *err;
7458 char *branch_refname = NULL, *commit_refname = NULL;
7460 err = get_merge_branch_ref_name(&branch_refname, worktree);
7461 if (err)
7462 goto done;
7463 err = delete_ref(branch_refname, repo);
7464 if (err)
7465 goto done;
7467 err = get_merge_commit_ref_name(&commit_refname, worktree);
7468 if (err)
7469 goto done;
7470 err = delete_ref(commit_refname, repo);
7471 if (err)
7472 goto done;
7474 done:
7475 free(branch_refname);
7476 free(commit_refname);
7477 return err;
7480 struct merge_commit_msg_arg {
7481 struct got_worktree *worktree;
7482 const char *branch_name;
7485 static const struct got_error *
7486 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7487 void *arg)
7489 struct merge_commit_msg_arg *a = arg;
7491 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7492 got_worktree_get_head_ref_name(a->worktree)) == -1)
7493 return got_error_from_errno("asprintf");
7495 return NULL;
7498 static const struct got_error *
7499 merge_status_cb(void *arg, unsigned char status, unsigned char staged_status,
7500 const char *path, struct got_object_id *blob_id,
7501 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7502 int dirfd, const char *de_name)
7504 return NULL;
7507 const struct got_error *
7508 got_worktree_merge_branch(struct got_worktree *worktree,
7509 struct got_fileindex *fileindex,
7510 struct got_object_id *yca_commit_id,
7511 struct got_object_id *branch_tip,
7512 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7513 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7515 const struct got_error *err;
7516 char *fileindex_path = NULL;
7518 err = get_fileindex_path(&fileindex_path, worktree);
7519 if (err)
7520 goto done;
7522 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7523 worktree);
7524 if (err)
7525 goto done;
7527 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7528 branch_tip, repo, progress_cb, progress_arg,
7529 cancel_cb, cancel_arg);
7530 done:
7531 free(fileindex_path);
7532 return err;
7535 const struct got_error *
7536 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7537 struct got_worktree *worktree, struct got_fileindex *fileindex,
7538 const char *author, const char *committer, int allow_bad_symlinks,
7539 struct got_object_id *branch_tip, const char *branch_name,
7540 struct got_repository *repo)
7542 const struct got_error *err = NULL, *sync_err;
7543 struct got_pathlist_head commitable_paths;
7544 struct collect_commitables_arg cc_arg;
7545 struct got_pathlist_entry *pe;
7546 struct got_reference *head_ref = NULL;
7547 struct got_object_id *head_commit_id = NULL;
7548 int have_staged_files = 0;
7549 struct merge_commit_msg_arg mcm_arg;
7550 char *fileindex_path = NULL;
7552 *new_commit_id = NULL;
7554 TAILQ_INIT(&commitable_paths);
7556 err = get_fileindex_path(&fileindex_path, worktree);
7557 if (err)
7558 goto done;
7560 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7561 if (err)
7562 goto done;
7564 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7565 if (err)
7566 goto done;
7568 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7569 &have_staged_files);
7570 if (err && err->code != GOT_ERR_CANCELLED)
7571 goto done;
7572 if (have_staged_files) {
7573 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7574 goto done;
7577 cc_arg.commitable_paths = &commitable_paths;
7578 cc_arg.worktree = worktree;
7579 cc_arg.fileindex = fileindex;
7580 cc_arg.repo = repo;
7581 cc_arg.have_staged_files = have_staged_files;
7582 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7583 err = worktree_status(worktree, "", fileindex, repo,
7584 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
7585 if (err)
7586 goto done;
7588 if (TAILQ_EMPTY(&commitable_paths)) {
7589 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7590 "merge of %s cannot proceed", branch_name);
7591 goto done;
7594 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7595 struct got_commitable *ct = pe->data;
7596 const char *ct_path = ct->in_repo_path;
7598 while (ct_path[0] == '/')
7599 ct_path++;
7600 err = check_out_of_date(ct_path, ct->status,
7601 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7602 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7603 if (err)
7604 goto done;
7608 mcm_arg.worktree = worktree;
7609 mcm_arg.branch_name = branch_name;
7610 err = commit_worktree(new_commit_id, &commitable_paths,
7611 head_commit_id, branch_tip, worktree, author, committer,
7612 merge_commit_msg_cb, &mcm_arg, merge_status_cb, NULL, repo);
7613 if (err)
7614 goto done;
7616 err = update_fileindex_after_commit(worktree, &commitable_paths,
7617 *new_commit_id, fileindex, have_staged_files);
7618 sync_err = sync_fileindex(fileindex, fileindex_path);
7619 if (sync_err && err == NULL)
7620 err = sync_err;
7621 done:
7622 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7623 struct got_commitable *ct = pe->data;
7624 free_commitable(ct);
7626 got_pathlist_free(&commitable_paths);
7627 free(fileindex_path);
7628 return err;
7631 const struct got_error *
7632 got_worktree_merge_complete(struct got_worktree *worktree,
7633 struct got_fileindex *fileindex, struct got_repository *repo)
7635 const struct got_error *err, *unlockerr, *sync_err;
7636 char *fileindex_path = NULL;
7638 err = delete_merge_refs(worktree, repo);
7639 if (err)
7640 goto done;
7642 err = get_fileindex_path(&fileindex_path, worktree);
7643 if (err)
7644 goto done;
7645 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7646 sync_err = sync_fileindex(fileindex, fileindex_path);
7647 if (sync_err && err == NULL)
7648 err = sync_err;
7649 done:
7650 got_fileindex_free(fileindex);
7651 free(fileindex_path);
7652 unlockerr = lock_worktree(worktree, LOCK_SH);
7653 if (unlockerr && err == NULL)
7654 err = unlockerr;
7655 return err;
7658 const struct got_error *
7659 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7660 struct got_repository *repo)
7662 const struct got_error *err;
7663 char *branch_refname = NULL;
7664 struct got_reference *branch_ref = NULL;
7666 *in_progress = 0;
7668 err = get_merge_branch_ref_name(&branch_refname, worktree);
7669 if (err)
7670 return err;
7671 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7672 free(branch_refname);
7673 if (err) {
7674 if (err->code != GOT_ERR_NOT_REF)
7675 return err;
7676 } else
7677 *in_progress = 1;
7679 return NULL;
7682 const struct got_error *got_worktree_merge_prepare(
7683 struct got_fileindex **fileindex, struct got_worktree *worktree,
7684 struct got_reference *branch, struct got_repository *repo)
7686 const struct got_error *err = NULL;
7687 char *fileindex_path = NULL;
7688 char *branch_refname = NULL, *commit_refname = NULL;
7689 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7690 struct got_reference *commit_ref = NULL;
7691 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7692 struct check_rebase_ok_arg ok_arg;
7694 *fileindex = NULL;
7696 err = lock_worktree(worktree, LOCK_EX);
7697 if (err)
7698 return err;
7700 err = open_fileindex(fileindex, &fileindex_path, worktree);
7701 if (err)
7702 goto done;
7704 /* Preconditions are the same as for rebase. */
7705 ok_arg.worktree = worktree;
7706 ok_arg.repo = repo;
7707 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7708 &ok_arg);
7709 if (err)
7710 goto done;
7712 err = get_merge_branch_ref_name(&branch_refname, worktree);
7713 if (err)
7714 return err;
7716 err = get_merge_commit_ref_name(&commit_refname, worktree);
7717 if (err)
7718 return err;
7720 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7721 0);
7722 if (err)
7723 goto done;
7725 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7726 if (err)
7727 goto done;
7729 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7730 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7731 goto done;
7734 err = got_ref_resolve(&branch_tip, repo, branch);
7735 if (err)
7736 goto done;
7738 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7739 if (err)
7740 goto done;
7741 err = got_ref_write(branch_ref, repo);
7742 if (err)
7743 goto done;
7745 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7746 if (err)
7747 goto done;
7748 err = got_ref_write(commit_ref, repo);
7749 if (err)
7750 goto done;
7752 done:
7753 free(branch_refname);
7754 free(commit_refname);
7755 free(fileindex_path);
7756 if (branch_ref)
7757 got_ref_close(branch_ref);
7758 if (commit_ref)
7759 got_ref_close(commit_ref);
7760 if (wt_branch)
7761 got_ref_close(wt_branch);
7762 free(wt_branch_tip);
7763 if (err) {
7764 if (*fileindex) {
7765 got_fileindex_free(*fileindex);
7766 *fileindex = NULL;
7768 lock_worktree(worktree, LOCK_SH);
7770 return err;
7773 const struct got_error *
7774 got_worktree_merge_continue(char **branch_name,
7775 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7776 struct got_worktree *worktree, struct got_repository *repo)
7778 const struct got_error *err;
7779 char *commit_refname = NULL, *branch_refname = NULL;
7780 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7781 char *fileindex_path = NULL;
7782 int have_staged_files = 0;
7784 *branch_name = NULL;
7785 *branch_tip = NULL;
7786 *fileindex = NULL;
7788 err = lock_worktree(worktree, LOCK_EX);
7789 if (err)
7790 return err;
7792 err = open_fileindex(fileindex, &fileindex_path, worktree);
7793 if (err)
7794 goto done;
7796 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7797 &have_staged_files);
7798 if (err && err->code != GOT_ERR_CANCELLED)
7799 goto done;
7800 if (have_staged_files) {
7801 err = got_error(GOT_ERR_STAGED_PATHS);
7802 goto done;
7805 err = get_merge_branch_ref_name(&branch_refname, worktree);
7806 if (err)
7807 goto done;
7809 err = get_merge_commit_ref_name(&commit_refname, worktree);
7810 if (err)
7811 goto done;
7813 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7814 if (err)
7815 goto done;
7817 if (!got_ref_is_symbolic(branch_ref)) {
7818 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7819 "%s is not a symbolic reference",
7820 got_ref_get_name(branch_ref));
7821 goto done;
7823 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7824 if (*branch_name == NULL) {
7825 err = got_error_from_errno("strdup");
7826 goto done;
7829 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7830 if (err)
7831 goto done;
7833 err = got_ref_resolve(branch_tip, repo, commit_ref);
7834 if (err)
7835 goto done;
7836 done:
7837 free(commit_refname);
7838 free(branch_refname);
7839 free(fileindex_path);
7840 if (commit_ref)
7841 got_ref_close(commit_ref);
7842 if (branch_ref)
7843 got_ref_close(branch_ref);
7844 if (err) {
7845 if (*branch_name) {
7846 free(*branch_name);
7847 *branch_name = NULL;
7849 free(*branch_tip);
7850 *branch_tip = NULL;
7851 if (*fileindex) {
7852 got_fileindex_free(*fileindex);
7853 *fileindex = NULL;
7855 lock_worktree(worktree, LOCK_SH);
7857 return err;
7860 const struct got_error *
7861 got_worktree_merge_abort(struct got_worktree *worktree,
7862 struct got_fileindex *fileindex, struct got_repository *repo,
7863 got_worktree_checkout_cb progress_cb, void *progress_arg)
7865 const struct got_error *err, *unlockerr, *sync_err;
7866 struct got_object_id *commit_id = NULL;
7867 char *fileindex_path = NULL;
7868 struct revert_file_args rfa;
7869 struct got_object_id *tree_id = NULL;
7871 err = got_object_id_by_path(&tree_id, repo,
7872 worktree->base_commit_id, worktree->path_prefix);
7873 if (err)
7874 goto done;
7876 err = delete_merge_refs(worktree, repo);
7877 if (err)
7878 goto done;
7880 err = get_fileindex_path(&fileindex_path, worktree);
7881 if (err)
7882 goto done;
7884 rfa.worktree = worktree;
7885 rfa.fileindex = fileindex;
7886 rfa.progress_cb = progress_cb;
7887 rfa.progress_arg = progress_arg;
7888 rfa.patch_cb = NULL;
7889 rfa.patch_arg = NULL;
7890 rfa.repo = repo;
7891 rfa.unlink_added_files = 1;
7892 err = worktree_status(worktree, "", fileindex, repo,
7893 revert_file, &rfa, NULL, NULL, 0, 0);
7894 if (err)
7895 goto sync;
7897 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7898 repo, progress_cb, progress_arg, NULL, NULL);
7899 sync:
7900 sync_err = sync_fileindex(fileindex, fileindex_path);
7901 if (sync_err && err == NULL)
7902 err = sync_err;
7903 done:
7904 free(tree_id);
7905 free(commit_id);
7906 if (fileindex)
7907 got_fileindex_free(fileindex);
7908 free(fileindex_path);
7910 unlockerr = lock_worktree(worktree, LOCK_SH);
7911 if (unlockerr && err == NULL)
7912 err = unlockerr;
7913 return err;
7916 struct check_stage_ok_arg {
7917 struct got_object_id *head_commit_id;
7918 struct got_worktree *worktree;
7919 struct got_fileindex *fileindex;
7920 struct got_repository *repo;
7921 int have_changes;
7924 const struct got_error *
7925 check_stage_ok(void *arg, unsigned char status,
7926 unsigned char staged_status, const char *relpath,
7927 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7928 struct got_object_id *commit_id, int dirfd, const char *de_name)
7930 struct check_stage_ok_arg *a = arg;
7931 const struct got_error *err = NULL;
7932 struct got_fileindex_entry *ie;
7933 struct got_object_id base_commit_id;
7934 struct got_object_id *base_commit_idp = NULL;
7935 char *in_repo_path = NULL, *p;
7937 if (status == GOT_STATUS_UNVERSIONED ||
7938 status == GOT_STATUS_NO_CHANGE)
7939 return NULL;
7940 if (status == GOT_STATUS_NONEXISTENT)
7941 return got_error_set_errno(ENOENT, relpath);
7943 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7944 if (ie == NULL)
7945 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7947 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7948 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7949 relpath) == -1)
7950 return got_error_from_errno("asprintf");
7952 if (got_fileindex_entry_has_commit(ie)) {
7953 memcpy(base_commit_id.sha1, ie->commit_sha1,
7954 SHA1_DIGEST_LENGTH);
7955 base_commit_idp = &base_commit_id;
7958 if (status == GOT_STATUS_CONFLICT) {
7959 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7960 goto done;
7961 } else if (status != GOT_STATUS_ADD &&
7962 status != GOT_STATUS_MODIFY &&
7963 status != GOT_STATUS_DELETE) {
7964 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7965 goto done;
7968 a->have_changes = 1;
7970 p = in_repo_path;
7971 while (p[0] == '/')
7972 p++;
7973 err = check_out_of_date(p, status, staged_status,
7974 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7975 GOT_ERR_STAGE_OUT_OF_DATE);
7976 done:
7977 free(in_repo_path);
7978 return err;
7981 struct stage_path_arg {
7982 struct got_worktree *worktree;
7983 struct got_fileindex *fileindex;
7984 struct got_repository *repo;
7985 got_worktree_status_cb status_cb;
7986 void *status_arg;
7987 got_worktree_patch_cb patch_cb;
7988 void *patch_arg;
7989 int staged_something;
7990 int allow_bad_symlinks;
7993 static const struct got_error *
7994 stage_path(void *arg, unsigned char status,
7995 unsigned char staged_status, const char *relpath,
7996 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7997 struct got_object_id *commit_id, int dirfd, const char *de_name)
7999 struct stage_path_arg *a = arg;
8000 const struct got_error *err = NULL;
8001 struct got_fileindex_entry *ie;
8002 char *ondisk_path = NULL, *path_content = NULL;
8003 uint32_t stage;
8004 struct got_object_id *new_staged_blob_id = NULL;
8005 struct stat sb;
8007 if (status == GOT_STATUS_UNVERSIONED)
8008 return NULL;
8010 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8011 if (ie == NULL)
8012 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8014 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8015 relpath)== -1)
8016 return got_error_from_errno("asprintf");
8018 switch (status) {
8019 case GOT_STATUS_ADD:
8020 case GOT_STATUS_MODIFY:
8021 /* XXX could sb.st_mode be passed in by our caller? */
8022 if (lstat(ondisk_path, &sb) == -1) {
8023 err = got_error_from_errno2("lstat", ondisk_path);
8024 break;
8026 if (a->patch_cb) {
8027 if (status == GOT_STATUS_ADD) {
8028 int choice = GOT_PATCH_CHOICE_NONE;
8029 err = (*a->patch_cb)(&choice, a->patch_arg,
8030 status, ie->path, NULL, 1, 1);
8031 if (err)
8032 break;
8033 if (choice != GOT_PATCH_CHOICE_YES)
8034 break;
8035 } else {
8036 err = create_patched_content(&path_content, 0,
8037 staged_blob_id ? staged_blob_id : blob_id,
8038 ondisk_path, dirfd, de_name, ie->path,
8039 a->repo, a->patch_cb, a->patch_arg);
8040 if (err || path_content == NULL)
8041 break;
8044 err = got_object_blob_create(&new_staged_blob_id,
8045 path_content ? path_content : ondisk_path, a->repo);
8046 if (err)
8047 break;
8048 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8049 SHA1_DIGEST_LENGTH);
8050 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8051 stage = GOT_FILEIDX_STAGE_ADD;
8052 else
8053 stage = GOT_FILEIDX_STAGE_MODIFY;
8054 got_fileindex_entry_stage_set(ie, stage);
8055 if (S_ISLNK(sb.st_mode)) {
8056 int is_bad_symlink = 0;
8057 if (!a->allow_bad_symlinks) {
8058 char target_path[PATH_MAX];
8059 ssize_t target_len;
8060 target_len = readlink(ondisk_path, target_path,
8061 sizeof(target_path));
8062 if (target_len == -1) {
8063 err = got_error_from_errno2("readlink",
8064 ondisk_path);
8065 break;
8067 err = is_bad_symlink_target(&is_bad_symlink,
8068 target_path, target_len, ondisk_path,
8069 a->worktree->root_path);
8070 if (err)
8071 break;
8072 if (is_bad_symlink) {
8073 err = got_error_path(ondisk_path,
8074 GOT_ERR_BAD_SYMLINK);
8075 break;
8078 if (is_bad_symlink)
8079 got_fileindex_entry_staged_filetype_set(ie,
8080 GOT_FILEIDX_MODE_BAD_SYMLINK);
8081 else
8082 got_fileindex_entry_staged_filetype_set(ie,
8083 GOT_FILEIDX_MODE_SYMLINK);
8084 } else {
8085 got_fileindex_entry_staged_filetype_set(ie,
8086 GOT_FILEIDX_MODE_REGULAR_FILE);
8088 a->staged_something = 1;
8089 if (a->status_cb == NULL)
8090 break;
8091 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8092 get_staged_status(ie), relpath, blob_id,
8093 new_staged_blob_id, NULL, dirfd, de_name);
8094 break;
8095 case GOT_STATUS_DELETE:
8096 if (staged_status == GOT_STATUS_DELETE)
8097 break;
8098 if (a->patch_cb) {
8099 int choice = GOT_PATCH_CHOICE_NONE;
8100 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8101 ie->path, NULL, 1, 1);
8102 if (err)
8103 break;
8104 if (choice == GOT_PATCH_CHOICE_NO)
8105 break;
8106 if (choice != GOT_PATCH_CHOICE_YES) {
8107 err = got_error(GOT_ERR_PATCH_CHOICE);
8108 break;
8111 stage = GOT_FILEIDX_STAGE_DELETE;
8112 got_fileindex_entry_stage_set(ie, stage);
8113 a->staged_something = 1;
8114 if (a->status_cb == NULL)
8115 break;
8116 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8117 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8118 de_name);
8119 break;
8120 case GOT_STATUS_NO_CHANGE:
8121 break;
8122 case GOT_STATUS_CONFLICT:
8123 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8124 break;
8125 case GOT_STATUS_NONEXISTENT:
8126 err = got_error_set_errno(ENOENT, relpath);
8127 break;
8128 default:
8129 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8130 break;
8133 if (path_content && unlink(path_content) == -1 && err == NULL)
8134 err = got_error_from_errno2("unlink", path_content);
8135 free(path_content);
8136 free(ondisk_path);
8137 free(new_staged_blob_id);
8138 return err;
8141 const struct got_error *
8142 got_worktree_stage(struct got_worktree *worktree,
8143 struct got_pathlist_head *paths,
8144 got_worktree_status_cb status_cb, void *status_arg,
8145 got_worktree_patch_cb patch_cb, void *patch_arg,
8146 int allow_bad_symlinks, struct got_repository *repo)
8148 const struct got_error *err = NULL, *sync_err, *unlockerr;
8149 struct got_pathlist_entry *pe;
8150 struct got_fileindex *fileindex = NULL;
8151 char *fileindex_path = NULL;
8152 struct got_reference *head_ref = NULL;
8153 struct got_object_id *head_commit_id = NULL;
8154 struct check_stage_ok_arg oka;
8155 struct stage_path_arg spa;
8157 err = lock_worktree(worktree, LOCK_EX);
8158 if (err)
8159 return err;
8161 err = got_ref_open(&head_ref, repo,
8162 got_worktree_get_head_ref_name(worktree), 0);
8163 if (err)
8164 goto done;
8165 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8166 if (err)
8167 goto done;
8168 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8169 if (err)
8170 goto done;
8172 /* Check pre-conditions before staging anything. */
8173 oka.head_commit_id = head_commit_id;
8174 oka.worktree = worktree;
8175 oka.fileindex = fileindex;
8176 oka.repo = repo;
8177 oka.have_changes = 0;
8178 TAILQ_FOREACH(pe, paths, entry) {
8179 err = worktree_status(worktree, pe->path, fileindex, repo,
8180 check_stage_ok, &oka, NULL, NULL, 0, 0);
8181 if (err)
8182 goto done;
8184 if (!oka.have_changes) {
8185 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8186 goto done;
8189 spa.worktree = worktree;
8190 spa.fileindex = fileindex;
8191 spa.repo = repo;
8192 spa.patch_cb = patch_cb;
8193 spa.patch_arg = patch_arg;
8194 spa.status_cb = status_cb;
8195 spa.status_arg = status_arg;
8196 spa.staged_something = 0;
8197 spa.allow_bad_symlinks = allow_bad_symlinks;
8198 TAILQ_FOREACH(pe, paths, entry) {
8199 err = worktree_status(worktree, pe->path, fileindex, repo,
8200 stage_path, &spa, NULL, NULL, 0, 0);
8201 if (err)
8202 goto done;
8204 if (!spa.staged_something) {
8205 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8206 goto done;
8209 sync_err = sync_fileindex(fileindex, fileindex_path);
8210 if (sync_err && err == NULL)
8211 err = sync_err;
8212 done:
8213 if (head_ref)
8214 got_ref_close(head_ref);
8215 free(head_commit_id);
8216 free(fileindex_path);
8217 if (fileindex)
8218 got_fileindex_free(fileindex);
8219 unlockerr = lock_worktree(worktree, LOCK_SH);
8220 if (unlockerr && err == NULL)
8221 err = unlockerr;
8222 return err;
8225 struct unstage_path_arg {
8226 struct got_worktree *worktree;
8227 struct got_fileindex *fileindex;
8228 struct got_repository *repo;
8229 got_worktree_checkout_cb progress_cb;
8230 void *progress_arg;
8231 got_worktree_patch_cb patch_cb;
8232 void *patch_arg;
8235 static const struct got_error *
8236 create_unstaged_content(char **path_unstaged_content,
8237 char **path_new_staged_content, struct got_object_id *blob_id,
8238 struct got_object_id *staged_blob_id, const char *relpath,
8239 struct got_repository *repo,
8240 got_worktree_patch_cb patch_cb, void *patch_arg)
8242 const struct got_error *err, *free_err;
8243 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8244 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8245 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8246 struct got_diffreg_result *diffreg_result = NULL;
8247 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8248 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8250 *path_unstaged_content = NULL;
8251 *path_new_staged_content = NULL;
8253 err = got_object_id_str(&label1, blob_id);
8254 if (err)
8255 return err;
8256 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8257 if (err)
8258 goto done;
8260 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8261 if (err)
8262 goto done;
8264 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8265 if (err)
8266 goto done;
8268 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8269 if (err)
8270 goto done;
8272 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8273 if (err)
8274 goto done;
8276 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8277 if (err)
8278 goto done;
8280 err = got_diff_files(&diffreg_result, f1, label1, f2,
8281 path2, 3, 0, 1, NULL);
8282 if (err)
8283 goto done;
8285 err = got_opentemp_named(path_unstaged_content, &outfile,
8286 "got-unstaged-content");
8287 if (err)
8288 goto done;
8289 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8290 "got-new-staged-content");
8291 if (err)
8292 goto done;
8294 if (fseek(f1, 0L, SEEK_SET) == -1) {
8295 err = got_ferror(f1, GOT_ERR_IO);
8296 goto done;
8298 if (fseek(f2, 0L, SEEK_SET) == -1) {
8299 err = got_ferror(f2, GOT_ERR_IO);
8300 goto done;
8302 /* Count the number of actual changes in the diff result. */
8303 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8304 struct diff_chunk_context cc = {};
8305 diff_chunk_context_load_change(&cc, &nchunks_used,
8306 diffreg_result->result, n, 0);
8307 nchanges++;
8309 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8310 int choice;
8311 err = apply_or_reject_change(&choice, &nchunks_used,
8312 diffreg_result->result, n, relpath, f1, f2,
8313 &line_cur1, &line_cur2,
8314 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8315 if (err)
8316 goto done;
8317 if (choice == GOT_PATCH_CHOICE_YES)
8318 have_content = 1;
8319 else
8320 have_rejected_content = 1;
8321 if (choice == GOT_PATCH_CHOICE_QUIT)
8322 break;
8324 if (have_content || have_rejected_content)
8325 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8326 outfile, rejectfile);
8327 done:
8328 free(label1);
8329 if (blob)
8330 got_object_blob_close(blob);
8331 if (staged_blob)
8332 got_object_blob_close(staged_blob);
8333 free_err = got_diffreg_result_free(diffreg_result);
8334 if (free_err && err == NULL)
8335 err = free_err;
8336 if (f1 && fclose(f1) == EOF && err == NULL)
8337 err = got_error_from_errno2("fclose", path1);
8338 if (f2 && fclose(f2) == EOF && err == NULL)
8339 err = got_error_from_errno2("fclose", path2);
8340 if (outfile && fclose(outfile) == EOF && err == NULL)
8341 err = got_error_from_errno2("fclose", *path_unstaged_content);
8342 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8343 err = got_error_from_errno2("fclose", *path_new_staged_content);
8344 if (path1 && unlink(path1) == -1 && err == NULL)
8345 err = got_error_from_errno2("unlink", path1);
8346 if (path2 && unlink(path2) == -1 && err == NULL)
8347 err = got_error_from_errno2("unlink", path2);
8348 if (err || !have_content) {
8349 if (*path_unstaged_content &&
8350 unlink(*path_unstaged_content) == -1 && err == NULL)
8351 err = got_error_from_errno2("unlink",
8352 *path_unstaged_content);
8353 free(*path_unstaged_content);
8354 *path_unstaged_content = NULL;
8356 if (err || !have_content || !have_rejected_content) {
8357 if (*path_new_staged_content &&
8358 unlink(*path_new_staged_content) == -1 && err == NULL)
8359 err = got_error_from_errno2("unlink",
8360 *path_new_staged_content);
8361 free(*path_new_staged_content);
8362 *path_new_staged_content = NULL;
8364 free(path1);
8365 free(path2);
8366 return err;
8369 static const struct got_error *
8370 unstage_hunks(struct got_object_id *staged_blob_id,
8371 struct got_blob_object *blob_base,
8372 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8373 const char *ondisk_path, const char *label_orig,
8374 struct got_worktree *worktree, struct got_repository *repo,
8375 got_worktree_patch_cb patch_cb, void *patch_arg,
8376 got_worktree_checkout_cb progress_cb, void *progress_arg)
8378 const struct got_error *err = NULL;
8379 char *path_unstaged_content = NULL;
8380 char *path_new_staged_content = NULL;
8381 char *parent = NULL, *base_path = NULL;
8382 char *blob_base_path = NULL;
8383 struct got_object_id *new_staged_blob_id = NULL;
8384 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8385 struct stat sb;
8387 err = create_unstaged_content(&path_unstaged_content,
8388 &path_new_staged_content, blob_id, staged_blob_id,
8389 ie->path, repo, patch_cb, patch_arg);
8390 if (err)
8391 return err;
8393 if (path_unstaged_content == NULL)
8394 return NULL;
8396 if (path_new_staged_content) {
8397 err = got_object_blob_create(&new_staged_blob_id,
8398 path_new_staged_content, repo);
8399 if (err)
8400 goto done;
8403 f = fopen(path_unstaged_content, "r");
8404 if (f == NULL) {
8405 err = got_error_from_errno2("fopen",
8406 path_unstaged_content);
8407 goto done;
8409 if (fstat(fileno(f), &sb) == -1) {
8410 err = got_error_from_errno2("fstat", path_unstaged_content);
8411 goto done;
8413 if (got_fileindex_entry_staged_filetype_get(ie) ==
8414 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8415 char link_target[PATH_MAX];
8416 size_t r;
8417 r = fread(link_target, 1, sizeof(link_target), f);
8418 if (r == 0 && ferror(f)) {
8419 err = got_error_from_errno("fread");
8420 goto done;
8422 if (r >= sizeof(link_target)) { /* should not happen */
8423 err = got_error(GOT_ERR_NO_SPACE);
8424 goto done;
8426 link_target[r] = '\0';
8427 err = merge_symlink(worktree, blob_base,
8428 ondisk_path, ie->path, label_orig, link_target,
8429 worktree->base_commit_id, repo, progress_cb,
8430 progress_arg);
8431 } else {
8432 int local_changes_subsumed;
8434 err = got_path_dirname(&parent, ondisk_path);
8435 if (err)
8436 return err;
8438 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8439 parent) == -1) {
8440 err = got_error_from_errno("asprintf");
8441 base_path = NULL;
8442 goto done;
8445 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8446 if (err)
8447 goto done;
8448 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8449 blob_base);
8450 if (err)
8451 goto done;
8454 * In order the run a 3-way merge with a symlink we copy the symlink's
8455 * target path into a temporary file and use that file with diff3.
8457 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8458 err = dump_symlink_target_path_to_file(&f_deriv2,
8459 ondisk_path);
8460 if (err)
8461 goto done;
8462 } else {
8463 int fd;
8464 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
8465 if (fd == -1) {
8466 err = got_error_from_errno2("open", ondisk_path);
8467 goto done;
8469 f_deriv2 = fdopen(fd, "r");
8470 if (f_deriv2 == NULL) {
8471 err = got_error_from_errno2("fdopen", ondisk_path);
8472 close(fd);
8473 goto done;
8477 err = merge_file(&local_changes_subsumed, worktree,
8478 f_base, f, f_deriv2, ondisk_path, ie->path,
8479 got_fileindex_perms_to_st(ie),
8480 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8481 repo, progress_cb, progress_arg);
8483 if (err)
8484 goto done;
8486 if (new_staged_blob_id) {
8487 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8488 SHA1_DIGEST_LENGTH);
8489 } else {
8490 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8491 got_fileindex_entry_staged_filetype_set(ie, 0);
8493 done:
8494 free(new_staged_blob_id);
8495 if (path_unstaged_content &&
8496 unlink(path_unstaged_content) == -1 && err == NULL)
8497 err = got_error_from_errno2("unlink", path_unstaged_content);
8498 if (path_new_staged_content &&
8499 unlink(path_new_staged_content) == -1 && err == NULL)
8500 err = got_error_from_errno2("unlink", path_new_staged_content);
8501 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8502 err = got_error_from_errno2("unlink", blob_base_path);
8503 if (f_base && fclose(f_base) == EOF && err == NULL)
8504 err = got_error_from_errno2("fclose", path_unstaged_content);
8505 if (f && fclose(f) == EOF && err == NULL)
8506 err = got_error_from_errno2("fclose", path_unstaged_content);
8507 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8508 err = got_error_from_errno2("fclose", ondisk_path);
8509 free(path_unstaged_content);
8510 free(path_new_staged_content);
8511 free(blob_base_path);
8512 free(parent);
8513 free(base_path);
8514 return err;
8517 static const struct got_error *
8518 unstage_path(void *arg, unsigned char status,
8519 unsigned char staged_status, const char *relpath,
8520 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8521 struct got_object_id *commit_id, int dirfd, const char *de_name)
8523 const struct got_error *err = NULL;
8524 struct unstage_path_arg *a = arg;
8525 struct got_fileindex_entry *ie;
8526 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8527 char *ondisk_path = NULL;
8528 char *id_str = NULL, *label_orig = NULL;
8529 int local_changes_subsumed;
8530 struct stat sb;
8532 if (staged_status != GOT_STATUS_ADD &&
8533 staged_status != GOT_STATUS_MODIFY &&
8534 staged_status != GOT_STATUS_DELETE)
8535 return NULL;
8537 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8538 if (ie == NULL)
8539 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8541 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8542 == -1)
8543 return got_error_from_errno("asprintf");
8545 err = got_object_id_str(&id_str,
8546 commit_id ? commit_id : a->worktree->base_commit_id);
8547 if (err)
8548 goto done;
8549 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8550 id_str) == -1) {
8551 err = got_error_from_errno("asprintf");
8552 goto done;
8555 switch (staged_status) {
8556 case GOT_STATUS_MODIFY:
8557 err = got_object_open_as_blob(&blob_base, a->repo,
8558 blob_id, 8192);
8559 if (err)
8560 break;
8561 /* fall through */
8562 case GOT_STATUS_ADD:
8563 if (a->patch_cb) {
8564 if (staged_status == GOT_STATUS_ADD) {
8565 int choice = GOT_PATCH_CHOICE_NONE;
8566 err = (*a->patch_cb)(&choice, a->patch_arg,
8567 staged_status, ie->path, NULL, 1, 1);
8568 if (err)
8569 break;
8570 if (choice != GOT_PATCH_CHOICE_YES)
8571 break;
8572 } else {
8573 err = unstage_hunks(staged_blob_id,
8574 blob_base, blob_id, ie, ondisk_path,
8575 label_orig, a->worktree, a->repo,
8576 a->patch_cb, a->patch_arg,
8577 a->progress_cb, a->progress_arg);
8578 break; /* Done with this file. */
8581 err = got_object_open_as_blob(&blob_staged, a->repo,
8582 staged_blob_id, 8192);
8583 if (err)
8584 break;
8585 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8586 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8587 case GOT_FILEIDX_MODE_REGULAR_FILE:
8588 err = merge_blob(&local_changes_subsumed, a->worktree,
8589 blob_base, ondisk_path, relpath,
8590 got_fileindex_perms_to_st(ie), label_orig,
8591 blob_staged, commit_id ? commit_id :
8592 a->worktree->base_commit_id, a->repo,
8593 a->progress_cb, a->progress_arg);
8594 break;
8595 case GOT_FILEIDX_MODE_SYMLINK:
8596 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8597 char *staged_target;
8598 err = got_object_blob_read_to_str(
8599 &staged_target, blob_staged);
8600 if (err)
8601 goto done;
8602 err = merge_symlink(a->worktree, blob_base,
8603 ondisk_path, relpath, label_orig,
8604 staged_target, commit_id ? commit_id :
8605 a->worktree->base_commit_id,
8606 a->repo, a->progress_cb, a->progress_arg);
8607 free(staged_target);
8608 } else {
8609 err = merge_blob(&local_changes_subsumed,
8610 a->worktree, blob_base, ondisk_path,
8611 relpath, got_fileindex_perms_to_st(ie),
8612 label_orig, blob_staged,
8613 commit_id ? commit_id :
8614 a->worktree->base_commit_id, a->repo,
8615 a->progress_cb, a->progress_arg);
8617 break;
8618 default:
8619 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8620 break;
8622 if (err == NULL) {
8623 got_fileindex_entry_stage_set(ie,
8624 GOT_FILEIDX_STAGE_NONE);
8625 got_fileindex_entry_staged_filetype_set(ie, 0);
8627 break;
8628 case GOT_STATUS_DELETE:
8629 if (a->patch_cb) {
8630 int choice = GOT_PATCH_CHOICE_NONE;
8631 err = (*a->patch_cb)(&choice, a->patch_arg,
8632 staged_status, ie->path, NULL, 1, 1);
8633 if (err)
8634 break;
8635 if (choice == GOT_PATCH_CHOICE_NO)
8636 break;
8637 if (choice != GOT_PATCH_CHOICE_YES) {
8638 err = got_error(GOT_ERR_PATCH_CHOICE);
8639 break;
8642 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8643 got_fileindex_entry_staged_filetype_set(ie, 0);
8644 err = get_file_status(&status, &sb, ie, ondisk_path,
8645 dirfd, de_name, a->repo);
8646 if (err)
8647 break;
8648 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8649 break;
8651 done:
8652 free(ondisk_path);
8653 if (blob_base)
8654 got_object_blob_close(blob_base);
8655 if (blob_staged)
8656 got_object_blob_close(blob_staged);
8657 free(id_str);
8658 free(label_orig);
8659 return err;
8662 const struct got_error *
8663 got_worktree_unstage(struct got_worktree *worktree,
8664 struct got_pathlist_head *paths,
8665 got_worktree_checkout_cb progress_cb, void *progress_arg,
8666 got_worktree_patch_cb patch_cb, void *patch_arg,
8667 struct got_repository *repo)
8669 const struct got_error *err = NULL, *sync_err, *unlockerr;
8670 struct got_pathlist_entry *pe;
8671 struct got_fileindex *fileindex = NULL;
8672 char *fileindex_path = NULL;
8673 struct unstage_path_arg upa;
8675 err = lock_worktree(worktree, LOCK_EX);
8676 if (err)
8677 return err;
8679 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8680 if (err)
8681 goto done;
8683 upa.worktree = worktree;
8684 upa.fileindex = fileindex;
8685 upa.repo = repo;
8686 upa.progress_cb = progress_cb;
8687 upa.progress_arg = progress_arg;
8688 upa.patch_cb = patch_cb;
8689 upa.patch_arg = patch_arg;
8690 TAILQ_FOREACH(pe, paths, entry) {
8691 err = worktree_status(worktree, pe->path, fileindex, repo,
8692 unstage_path, &upa, NULL, NULL, 0, 0);
8693 if (err)
8694 goto done;
8697 sync_err = sync_fileindex(fileindex, fileindex_path);
8698 if (sync_err && err == NULL)
8699 err = sync_err;
8700 done:
8701 free(fileindex_path);
8702 if (fileindex)
8703 got_fileindex_free(fileindex);
8704 unlockerr = lock_worktree(worktree, LOCK_SH);
8705 if (unlockerr && err == NULL)
8706 err = unlockerr;
8707 return err;
8710 struct report_file_info_arg {
8711 struct got_worktree *worktree;
8712 got_worktree_path_info_cb info_cb;
8713 void *info_arg;
8714 struct got_pathlist_head *paths;
8715 got_cancel_cb cancel_cb;
8716 void *cancel_arg;
8719 static const struct got_error *
8720 report_file_info(void *arg, struct got_fileindex_entry *ie)
8722 struct report_file_info_arg *a = arg;
8723 struct got_pathlist_entry *pe;
8724 struct got_object_id blob_id, staged_blob_id, commit_id;
8725 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8726 struct got_object_id *commit_idp = NULL;
8727 int stage;
8729 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8730 return got_error(GOT_ERR_CANCELLED);
8732 TAILQ_FOREACH(pe, a->paths, entry) {
8733 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8734 got_path_is_child(ie->path, pe->path, pe->path_len))
8735 break;
8737 if (pe == NULL) /* not found */
8738 return NULL;
8740 if (got_fileindex_entry_has_blob(ie)) {
8741 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8742 blob_idp = &blob_id;
8744 stage = got_fileindex_entry_stage_get(ie);
8745 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8746 stage == GOT_FILEIDX_STAGE_ADD) {
8747 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8748 SHA1_DIGEST_LENGTH);
8749 staged_blob_idp = &staged_blob_id;
8752 if (got_fileindex_entry_has_commit(ie)) {
8753 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8754 commit_idp = &commit_id;
8757 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8758 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8761 const struct got_error *
8762 got_worktree_path_info(struct got_worktree *worktree,
8763 struct got_pathlist_head *paths,
8764 got_worktree_path_info_cb info_cb, void *info_arg,
8765 got_cancel_cb cancel_cb, void *cancel_arg)
8768 const struct got_error *err = NULL, *unlockerr;
8769 struct got_fileindex *fileindex = NULL;
8770 char *fileindex_path = NULL;
8771 struct report_file_info_arg arg;
8773 err = lock_worktree(worktree, LOCK_SH);
8774 if (err)
8775 return err;
8777 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8778 if (err)
8779 goto done;
8781 arg.worktree = worktree;
8782 arg.info_cb = info_cb;
8783 arg.info_arg = info_arg;
8784 arg.paths = paths;
8785 arg.cancel_cb = cancel_cb;
8786 arg.cancel_arg = cancel_arg;
8787 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8788 &arg);
8789 done:
8790 free(fileindex_path);
8791 if (fileindex)
8792 got_fileindex_free(fileindex);
8793 unlockerr = lock_worktree(worktree, LOCK_UN);
8794 if (unlockerr && err == NULL)
8795 err = unlockerr;
8796 return err;