Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
69 {
70 const struct got_error *err = NULL;
71 char *path;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
77 free(path);
78 return err;
79 }
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
83 {
84 const struct got_error *err = NULL;
85 FILE *tmpfile = NULL;
86 char *tmppath = NULL;
87 char *path = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
91 path = NULL;
92 goto done;
93 }
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
96 if (err)
97 goto done;
99 if (content) {
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
103 goto done;
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
109 unlink(tmppath);
110 goto done;
113 done:
114 if (fclose(tmpfile) == EOF && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
116 free(tmppath);
117 return err;
120 static const struct got_error *
121 read_meta_file(char **content, const char *path_got, const char *name)
123 const struct got_error *err = NULL;
124 char *path;
125 int fd = -1;
126 ssize_t n;
127 struct stat sb;
129 *content = NULL;
131 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 err = got_error_from_errno("asprintf");
133 path = NULL;
134 goto done;
137 fd = open(path, O_RDONLY | O_NOFOLLOW);
138 if (fd == -1) {
139 if (errno == ENOENT)
140 err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 else
142 err = got_error_from_errno2("open", path);
143 goto done;
145 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 : got_error_from_errno2("flock", path));
148 goto done;
151 if (fstat(fd, &sb) != 0) {
152 err = got_error_from_errno2("fstat", path);
153 goto done;
155 *content = calloc(1, sb.st_size);
156 if (*content == NULL) {
157 err = got_error_from_errno("calloc");
158 goto done;
161 n = read(fd, *content, sb.st_size);
162 if (n != sb.st_size) {
163 err = (n == -1 ? got_error_from_errno2("read", path) :
164 got_error_path(path, GOT_ERR_WORKTREE_META));
165 goto done;
167 if ((*content)[sb.st_size - 1] != '\n') {
168 err = got_error_path(path, GOT_ERR_WORKTREE_META);
169 goto done;
171 (*content)[sb.st_size - 1] = '\0';
173 done:
174 if (fd != -1 && close(fd) == -1 && err == NULL)
175 err = got_error_from_errno2("close", path_got);
176 free(path);
177 if (err) {
178 free(*content);
179 *content = NULL;
181 return err;
184 static const struct got_error *
185 write_head_ref(const char *path_got, struct got_reference *head_ref)
187 const struct got_error *err = NULL;
188 char *refstr = NULL;
190 if (got_ref_is_symbolic(head_ref)) {
191 refstr = got_ref_to_str(head_ref);
192 if (refstr == NULL)
193 return got_error_from_errno("got_ref_to_str");
194 } else {
195 refstr = strdup(got_ref_get_name(head_ref));
196 if (refstr == NULL)
197 return got_error_from_errno("strdup");
199 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
200 free(refstr);
201 return err;
204 const struct got_error *
205 got_worktree_init(const char *path, struct got_reference *head_ref,
206 const char *prefix, struct got_repository *repo)
208 const struct got_error *err = NULL;
209 struct got_object_id *commit_id = NULL;
210 uuid_t uuid;
211 uint32_t uuid_status;
212 int obj_type;
213 char *path_got = NULL;
214 char *formatstr = NULL;
215 char *absprefix = NULL;
216 char *basestr = NULL;
217 char *uuidstr = NULL;
219 if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 err = got_error(GOT_ERR_WORKTREE_REPO);
221 goto done;
224 err = got_ref_resolve(&commit_id, repo, head_ref);
225 if (err)
226 return err;
227 err = got_object_get_type(&obj_type, repo, commit_id);
228 if (err)
229 return err;
230 if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 return got_error(GOT_ERR_OBJ_TYPE);
233 if (!got_path_is_absolute(prefix)) {
234 if (asprintf(&absprefix, "/%s", prefix) == -1)
235 return got_error_from_errno("asprintf");
238 /* Create top-level directory (may already exist). */
239 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 err = got_error_from_errno2("mkdir", path);
241 goto done;
244 /* Create .got directory (may already exist). */
245 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 err = got_error_from_errno("asprintf");
247 goto done;
249 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 err = got_error_from_errno2("mkdir", path_got);
251 goto done;
254 /* Create an empty lock file. */
255 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
256 if (err)
257 goto done;
259 /* Create an empty file index. */
260 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
261 if (err)
262 goto done;
264 /* Write the HEAD reference. */
265 err = write_head_ref(path_got, head_ref);
266 if (err)
267 goto done;
269 /* Record our base commit. */
270 err = got_object_id_str(&basestr, commit_id);
271 if (err)
272 goto done;
273 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
274 if (err)
275 goto done;
277 /* Store path to repository. */
278 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 got_repo_get_path(repo));
280 if (err)
281 goto done;
283 /* Store in-repository path prefix. */
284 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 absprefix ? absprefix : prefix);
286 if (err)
287 goto done;
289 /* Generate UUID. */
290 uuid_create(&uuid, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status, "uuid_create");
293 goto done;
295 uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 if (uuid_status != uuid_s_ok) {
297 err = got_error_uuid(uuid_status, "uuid_to_string");
298 goto done;
300 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
301 if (err)
302 goto done;
304 /* Stamp work tree with format file. */
305 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 err = got_error_from_errno("asprintf");
307 goto done;
309 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
310 if (err)
311 goto done;
313 done:
314 free(commit_id);
315 free(path_got);
316 free(formatstr);
317 free(absprefix);
318 free(basestr);
319 free(uuidstr);
320 return err;
323 static const struct got_error *
324 open_worktree(struct got_worktree **worktree, const char *path)
326 const struct got_error *err = NULL;
327 char *path_got;
328 char *formatstr = NULL;
329 char *uuidstr = NULL;
330 char *path_lock = NULL;
331 char *base_commit_id_str = NULL;
332 int version, fd = -1;
333 const char *errstr;
334 struct got_repository *repo = NULL;
335 uint32_t uuid_status;
337 *worktree = NULL;
339 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 err = got_error_from_errno("asprintf");
341 path_got = NULL;
342 goto done;
345 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 err = got_error_from_errno("asprintf");
347 path_lock = NULL;
348 goto done;
351 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 if (fd == -1) {
353 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 : got_error_from_errno2("open", path_lock));
355 goto done;
358 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
359 if (err)
360 goto done;
362 version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 if (errstr) {
364 err = got_error_msg(GOT_ERR_WORKTREE_META,
365 "could not parse work tree format version number");
366 goto done;
368 if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 err = got_error(GOT_ERR_WORKTREE_VERS);
370 goto done;
373 *worktree = calloc(1, sizeof(**worktree));
374 if (*worktree == NULL) {
375 err = got_error_from_errno("calloc");
376 goto done;
378 (*worktree)->lockfd = -1;
380 (*worktree)->root_path = realpath(path, NULL);
381 if ((*worktree)->root_path == NULL) {
382 err = got_error_from_errno2("realpath", path);
383 goto done;
385 err = read_meta_file(&(*worktree)->repo_path, path_got,
386 GOT_WORKTREE_REPOSITORY);
387 if (err)
388 goto done;
390 err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 GOT_WORKTREE_PATH_PREFIX);
392 if (err)
393 goto done;
395 err = read_meta_file(&base_commit_id_str, path_got,
396 GOT_WORKTREE_BASE_COMMIT);
397 if (err)
398 goto done;
400 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
401 if (err)
402 goto done;
403 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 if (uuid_status != uuid_s_ok) {
405 err = got_error_uuid(uuid_status, "uuid_from_string");
406 goto done;
409 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
410 if (err)
411 goto done;
413 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 base_commit_id_str);
415 if (err)
416 goto done;
418 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 GOT_WORKTREE_HEAD_REF);
420 if (err)
421 goto done;
423 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 (*worktree)->root_path,
425 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 err = got_error_from_errno("asprintf");
427 goto done;
430 err = got_gotconfig_read(&(*worktree)->gotconfig,
431 (*worktree)->gotconfig_path);
433 (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
434 if ((*worktree)->root_fd == -1) {
435 err = got_error_from_errno2("open", (*worktree)->root_path);
436 goto done;
438 done:
439 if (repo) {
440 const struct got_error *close_err = got_repo_close(repo);
441 if (err == NULL)
442 err = close_err;
444 free(path_got);
445 free(path_lock);
446 free(base_commit_id_str);
447 free(uuidstr);
448 free(formatstr);
449 if (err) {
450 if (fd != -1)
451 close(fd);
452 if (*worktree != NULL)
453 got_worktree_close(*worktree);
454 *worktree = NULL;
455 } else
456 (*worktree)->lockfd = fd;
458 return err;
461 const struct got_error *
462 got_worktree_open(struct got_worktree **worktree, const char *path)
464 const struct got_error *err = NULL;
465 char *worktree_path;
467 worktree_path = strdup(path);
468 if (worktree_path == NULL)
469 return got_error_from_errno("strdup");
471 for (;;) {
472 char *parent_path;
474 err = open_worktree(worktree, worktree_path);
475 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
476 free(worktree_path);
477 return err;
479 if (*worktree) {
480 free(worktree_path);
481 return NULL;
483 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
484 break;
485 err = got_path_dirname(&parent_path, worktree_path);
486 if (err) {
487 if (err->code != GOT_ERR_BAD_PATH) {
488 free(worktree_path);
489 return err;
491 break;
493 free(worktree_path);
494 worktree_path = parent_path;
497 free(worktree_path);
498 return got_error(GOT_ERR_NOT_WORKTREE);
501 const struct got_error *
502 got_worktree_close(struct got_worktree *worktree)
504 const struct got_error *err = NULL;
506 if (worktree->lockfd != -1) {
507 if (close(worktree->lockfd) == -1)
508 err = got_error_from_errno2("close",
509 got_worktree_get_root_path(worktree));
511 if (close(worktree->root_fd) == -1 && err == NULL)
512 err = got_error_from_errno2("close",
513 got_worktree_get_root_path(worktree));
514 free(worktree->repo_path);
515 free(worktree->path_prefix);
516 free(worktree->base_commit_id);
517 free(worktree->head_ref_name);
518 free(worktree->root_path);
519 free(worktree->gotconfig_path);
520 got_gotconfig_free(worktree->gotconfig);
521 free(worktree);
522 return err;
525 const char *
526 got_worktree_get_root_path(struct got_worktree *worktree)
528 return worktree->root_path;
531 const char *
532 got_worktree_get_repo_path(struct got_worktree *worktree)
534 return worktree->repo_path;
536 const char *
537 got_worktree_get_path_prefix(struct got_worktree *worktree)
539 return worktree->path_prefix;
542 const struct got_error *
543 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
544 const char *path_prefix)
546 char *absprefix = NULL;
548 if (!got_path_is_absolute(path_prefix)) {
549 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
550 return got_error_from_errno("asprintf");
552 *match = (strcmp(absprefix ? absprefix : path_prefix,
553 worktree->path_prefix) == 0);
554 free(absprefix);
555 return NULL;
558 const char *
559 got_worktree_get_head_ref_name(struct got_worktree *worktree)
561 return worktree->head_ref_name;
564 const struct got_error *
565 got_worktree_set_head_ref(struct got_worktree *worktree,
566 struct got_reference *head_ref)
568 const struct got_error *err = NULL;
569 char *path_got = NULL, *head_ref_name = NULL;
571 if (asprintf(&path_got, "%s/%s", worktree->root_path,
572 GOT_WORKTREE_GOT_DIR) == -1) {
573 err = got_error_from_errno("asprintf");
574 path_got = NULL;
575 goto done;
578 head_ref_name = strdup(got_ref_get_name(head_ref));
579 if (head_ref_name == NULL) {
580 err = got_error_from_errno("strdup");
581 goto done;
584 err = write_head_ref(path_got, head_ref);
585 if (err)
586 goto done;
588 free(worktree->head_ref_name);
589 worktree->head_ref_name = head_ref_name;
590 done:
591 free(path_got);
592 if (err)
593 free(head_ref_name);
594 return err;
597 struct got_object_id *
598 got_worktree_get_base_commit_id(struct got_worktree *worktree)
600 return worktree->base_commit_id;
603 const struct got_error *
604 got_worktree_set_base_commit_id(struct got_worktree *worktree,
605 struct got_repository *repo, struct got_object_id *commit_id)
607 const struct got_error *err;
608 struct got_object *obj = NULL;
609 char *id_str = NULL;
610 char *path_got = NULL;
612 if (asprintf(&path_got, "%s/%s", worktree->root_path,
613 GOT_WORKTREE_GOT_DIR) == -1) {
614 err = got_error_from_errno("asprintf");
615 path_got = NULL;
616 goto done;
619 err = got_object_open(&obj, repo, commit_id);
620 if (err)
621 return err;
623 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
624 err = got_error(GOT_ERR_OBJ_TYPE);
625 goto done;
628 /* Record our base commit. */
629 err = got_object_id_str(&id_str, commit_id);
630 if (err)
631 goto done;
632 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
633 if (err)
634 goto done;
636 free(worktree->base_commit_id);
637 worktree->base_commit_id = got_object_id_dup(commit_id);
638 if (worktree->base_commit_id == NULL) {
639 err = got_error_from_errno("got_object_id_dup");
640 goto done;
642 done:
643 if (obj)
644 got_object_close(obj);
645 free(id_str);
646 free(path_got);
647 return err;
650 const struct got_gotconfig *
651 got_worktree_get_gotconfig(struct got_worktree *worktree)
653 return worktree->gotconfig;
656 static const struct got_error *
657 lock_worktree(struct got_worktree *worktree, int operation)
659 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
660 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
661 : got_error_from_errno2("flock",
662 got_worktree_get_root_path(worktree)));
663 return NULL;
666 static const struct got_error *
667 add_dir_on_disk(struct got_worktree *worktree, const char *path)
669 const struct got_error *err = NULL;
670 char *abspath;
672 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
673 return got_error_from_errno("asprintf");
675 err = got_path_mkdir(abspath);
676 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
677 struct stat sb;
678 err = NULL;
679 if (lstat(abspath, &sb) == -1) {
680 err = got_error_from_errno2("lstat", abspath);
681 } else if (!S_ISDIR(sb.st_mode)) {
682 /* TODO directory is obstructed; do something */
683 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
686 free(abspath);
687 return err;
690 static const struct got_error *
691 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
693 const struct got_error *err = NULL;
694 uint8_t fbuf1[8192];
695 uint8_t fbuf2[8192];
696 size_t flen1 = 0, flen2 = 0;
698 *same = 1;
700 for (;;) {
701 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
702 if (flen1 == 0 && ferror(f1)) {
703 err = got_error_from_errno("fread");
704 break;
706 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
707 if (flen2 == 0 && ferror(f2)) {
708 err = got_error_from_errno("fread");
709 break;
711 if (flen1 == 0) {
712 if (flen2 != 0)
713 *same = 0;
714 break;
715 } else if (flen2 == 0) {
716 if (flen1 != 0)
717 *same = 0;
718 break;
719 } else if (flen1 == flen2) {
720 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
721 *same = 0;
722 break;
724 } else {
725 *same = 0;
726 break;
730 return err;
733 static const struct got_error *
734 check_files_equal(int *same, FILE *f1, FILE *f2)
736 struct stat sb;
737 size_t size1, size2;
739 *same = 1;
741 if (fstat(fileno(f1), &sb) != 0)
742 return got_error_from_errno("fstat");
743 size1 = sb.st_size;
745 if (fstat(fileno(f2), &sb) != 0)
746 return got_error_from_errno("fstat");
747 size2 = sb.st_size;
749 if (size1 != size2) {
750 *same = 0;
751 return NULL;
754 if (fseek(f1, 0L, SEEK_SET) == -1)
755 return got_ferror(f1, GOT_ERR_IO);
756 if (fseek(f2, 0L, SEEK_SET) == -1)
757 return got_ferror(f2, GOT_ERR_IO);
759 return check_file_contents_equal(same, f1, f2);
762 /*
763 * Perform a 3-way merge where the file f_orig acts as the common
764 * ancestor, the file f_deriv acts as the first derived version,
765 * and the file f_deriv2 acts as the second derived version.
766 * The merge result will be written to a new file at ondisk_path; any
767 * existing file at this path will be replaced.
768 */
769 static const struct got_error *
770 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
771 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
772 const char *path, uint16_t st_mode,
773 const char *label_orig, const char *label_deriv, const char *label_deriv2,
774 enum got_diff_algorithm diff_algo, struct got_repository *repo,
775 got_worktree_checkout_cb progress_cb, void *progress_arg)
777 const struct got_error *err = NULL;
778 int merged_fd = -1;
779 FILE *f_merged = NULL;
780 char *merged_path = NULL, *base_path = NULL;
781 int overlapcnt = 0;
782 char *parent = NULL;
784 *local_changes_subsumed = 0;
786 err = got_path_dirname(&parent, ondisk_path);
787 if (err)
788 return err;
790 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
791 err = got_error_from_errno("asprintf");
792 goto done;
795 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
796 if (err)
797 goto done;
799 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
800 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
801 if (err)
802 goto done;
804 err = (*progress_cb)(progress_arg,
805 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
806 if (err)
807 goto done;
809 if (fsync(merged_fd) != 0) {
810 err = got_error_from_errno("fsync");
811 goto done;
814 f_merged = fdopen(merged_fd, "r");
815 if (f_merged == NULL) {
816 err = got_error_from_errno("fdopen");
817 goto done;
819 merged_fd = -1;
821 /* Check if a clean merge has subsumed all local changes. */
822 if (overlapcnt == 0) {
823 err = check_files_equal(local_changes_subsumed, f_deriv,
824 f_merged);
825 if (err)
826 goto done;
829 if (fchmod(fileno(f_merged), st_mode) != 0) {
830 err = got_error_from_errno2("fchmod", merged_path);
831 goto done;
834 if (rename(merged_path, ondisk_path) != 0) {
835 err = got_error_from_errno3("rename", merged_path,
836 ondisk_path);
837 goto done;
839 done:
840 if (err) {
841 if (merged_path)
842 unlink(merged_path);
844 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
845 err = got_error_from_errno("close");
846 if (f_merged && fclose(f_merged) == EOF && err == NULL)
847 err = got_error_from_errno("fclose");
848 free(merged_path);
849 free(base_path);
850 free(parent);
851 return err;
854 static const struct got_error *
855 update_symlink(const char *ondisk_path, const char *target_path,
856 size_t target_len)
858 /* This is not atomic but matches what 'ln -sf' does. */
859 if (unlink(ondisk_path) == -1)
860 return got_error_from_errno2("unlink", ondisk_path);
861 if (symlink(target_path, ondisk_path) == -1)
862 return got_error_from_errno3("symlink", target_path,
863 ondisk_path);
864 return NULL;
867 /*
868 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
869 * in the work tree with a file that contains conflict markers and the
870 * conflicting target paths of the original version, a "derived version"
871 * of a symlink from an incoming change, and a local version of the symlink.
873 * The original versions's target path can be NULL if it is not available,
874 * such as if both derived versions added a new symlink at the same path.
876 * The incoming derived symlink target is NULL in case the incoming change
877 * has deleted this symlink.
878 */
879 static const struct got_error *
880 install_symlink_conflict(const char *deriv_target,
881 struct got_object_id *deriv_base_commit_id, const char *orig_target,
882 const char *label_orig, const char *local_target, const char *ondisk_path)
884 const struct got_error *err;
885 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
886 FILE *f = NULL;
888 err = got_object_id_str(&id_str, deriv_base_commit_id);
889 if (err)
890 return got_error_from_errno("asprintf");
892 if (asprintf(&label_deriv, "%s: commit %s",
893 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
894 err = got_error_from_errno("asprintf");
895 goto done;
898 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
899 if (err)
900 goto done;
902 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
903 err = got_error_from_errno2("fchmod", path);
904 goto done;
907 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
908 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
909 deriv_target ? deriv_target : "(symlink was deleted)",
910 orig_target ? label_orig : "",
911 orig_target ? "\n" : "",
912 orig_target ? orig_target : "",
913 orig_target ? "\n" : "",
914 GOT_DIFF_CONFLICT_MARKER_SEP,
915 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
916 err = got_error_from_errno2("fprintf", path);
917 goto done;
920 if (unlink(ondisk_path) == -1) {
921 err = got_error_from_errno2("unlink", ondisk_path);
922 goto done;
924 if (rename(path, ondisk_path) == -1) {
925 err = got_error_from_errno3("rename", path, ondisk_path);
926 goto done;
928 done:
929 if (f != NULL && fclose(f) == EOF && err == NULL)
930 err = got_error_from_errno2("fclose", path);
931 free(path);
932 free(id_str);
933 free(label_deriv);
934 return err;
937 /* forward declaration */
938 static const struct got_error *
939 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
940 const char *, const char *, uint16_t, const char *,
941 struct got_blob_object *, struct got_object_id *,
942 struct got_repository *, got_worktree_checkout_cb, void *);
944 /*
945 * Merge a symlink into the work tree, where blob_orig acts as the common
946 * ancestor, deriv_target is the link target of the first derived version,
947 * and the symlink on disk acts as the second derived version.
948 * Assume that contents of both blobs represent symlinks.
949 */
950 static const struct got_error *
951 merge_symlink(struct got_worktree *worktree,
952 struct got_blob_object *blob_orig, const char *ondisk_path,
953 const char *path, const char *label_orig, const char *deriv_target,
954 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
955 got_worktree_checkout_cb progress_cb, void *progress_arg)
957 const struct got_error *err = NULL;
958 char *ancestor_target = NULL;
959 struct stat sb;
960 ssize_t ondisk_len, deriv_len;
961 char ondisk_target[PATH_MAX];
962 int have_local_change = 0;
963 int have_incoming_change = 0;
965 if (lstat(ondisk_path, &sb) == -1)
966 return got_error_from_errno2("lstat", ondisk_path);
968 ondisk_len = readlink(ondisk_path, ondisk_target,
969 sizeof(ondisk_target));
970 if (ondisk_len == -1) {
971 err = got_error_from_errno2("readlink",
972 ondisk_path);
973 goto done;
975 ondisk_target[ondisk_len] = '\0';
977 if (blob_orig) {
978 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
979 if (err)
980 goto done;
983 if (ancestor_target == NULL ||
984 (ondisk_len != strlen(ancestor_target) ||
985 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
986 have_local_change = 1;
988 deriv_len = strlen(deriv_target);
989 if (ancestor_target == NULL ||
990 (deriv_len != strlen(ancestor_target) ||
991 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
992 have_incoming_change = 1;
994 if (!have_local_change && !have_incoming_change) {
995 if (ancestor_target) {
996 /* Both sides made the same change. */
997 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
998 path);
999 } else if (deriv_len == ondisk_len &&
1000 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1001 /* Both sides added the same symlink. */
1002 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1003 path);
1004 } else {
1005 /* Both sides added symlinks which don't match. */
1006 err = install_symlink_conflict(deriv_target,
1007 deriv_base_commit_id, ancestor_target,
1008 label_orig, ondisk_target, ondisk_path);
1009 if (err)
1010 goto done;
1011 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1012 path);
1014 } else if (!have_local_change && have_incoming_change) {
1015 /* Apply the incoming change. */
1016 err = update_symlink(ondisk_path, deriv_target,
1017 strlen(deriv_target));
1018 if (err)
1019 goto done;
1020 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1021 } else if (have_local_change && have_incoming_change) {
1022 if (deriv_len == ondisk_len &&
1023 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1024 /* Both sides made the same change. */
1025 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1026 path);
1027 } else {
1028 err = install_symlink_conflict(deriv_target,
1029 deriv_base_commit_id, ancestor_target, label_orig,
1030 ondisk_target, ondisk_path);
1031 if (err)
1032 goto done;
1033 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1034 path);
1038 done:
1039 free(ancestor_target);
1040 return err;
1043 static const struct got_error *
1044 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
1046 const struct got_error *err = NULL;
1047 char target_path[PATH_MAX];
1048 ssize_t target_len;
1049 size_t n;
1050 FILE *f;
1052 *outfile = NULL;
1054 f = got_opentemp();
1055 if (f == NULL)
1056 return got_error_from_errno("got_opentemp");
1057 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
1058 if (target_len == -1) {
1059 err = got_error_from_errno2("readlink", ondisk_path);
1060 goto done;
1062 n = fwrite(target_path, 1, target_len, f);
1063 if (n != target_len) {
1064 err = got_ferror(f, GOT_ERR_IO);
1065 goto done;
1067 if (fflush(f) == EOF) {
1068 err = got_error_from_errno("fflush");
1069 goto done;
1071 if (fseek(f, 0L, SEEK_SET) == -1) {
1072 err = got_ferror(f, GOT_ERR_IO);
1073 goto done;
1075 done:
1076 if (err)
1077 fclose(f);
1078 else
1079 *outfile = f;
1080 return err;
1084 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1085 * blob_deriv acts as the first derived version, and the file on disk
1086 * acts as the second derived version.
1088 static const struct got_error *
1089 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1090 struct got_blob_object *blob_orig, const char *ondisk_path,
1091 const char *path, uint16_t st_mode, const char *label_orig,
1092 struct got_blob_object *blob_deriv,
1093 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1094 got_worktree_checkout_cb progress_cb, void *progress_arg)
1096 const struct got_error *err = NULL;
1097 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
1098 char *blob_orig_path = NULL;
1099 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1100 char *label_deriv = NULL, *parent = NULL;
1102 *local_changes_subsumed = 0;
1104 err = got_path_dirname(&parent, ondisk_path);
1105 if (err)
1106 return err;
1108 if (blob_orig) {
1109 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1110 parent) == -1) {
1111 err = got_error_from_errno("asprintf");
1112 base_path = NULL;
1113 goto done;
1116 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
1117 if (err)
1118 goto done;
1119 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1120 blob_orig);
1121 if (err)
1122 goto done;
1123 free(base_path);
1124 } else {
1126 * No common ancestor exists. This is an "add vs add" conflict
1127 * and we simply use an empty ancestor file to make both files
1128 * appear in the merged result in their entirety.
1130 f_orig = got_opentemp();
1131 if (f_orig == NULL) {
1132 err = got_error_from_errno("got_opentemp");
1133 goto done;
1137 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1138 err = got_error_from_errno("asprintf");
1139 base_path = NULL;
1140 goto done;
1143 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1144 if (err)
1145 goto done;
1146 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1147 blob_deriv);
1148 if (err)
1149 goto done;
1151 err = got_object_id_str(&id_str, deriv_base_commit_id);
1152 if (err)
1153 goto done;
1154 if (asprintf(&label_deriv, "%s: commit %s",
1155 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1156 err = got_error_from_errno("asprintf");
1157 goto done;
1161 * In order the run a 3-way merge with a symlink we copy the symlink's
1162 * target path into a temporary file and use that file with diff3.
1164 if (S_ISLNK(st_mode)) {
1165 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1166 if (err)
1167 goto done;
1168 } else {
1169 int fd;
1170 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
1171 if (fd == -1) {
1172 err = got_error_from_errno2("open", ondisk_path);
1173 goto done;
1175 f_deriv2 = fdopen(fd, "r");
1176 if (f_deriv2 == NULL) {
1177 err = got_error_from_errno2("fdopen", ondisk_path);
1178 close(fd);
1179 goto done;
1183 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1184 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1185 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1186 done:
1187 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1188 err = got_error_from_errno("fclose");
1189 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1190 err = got_error_from_errno("fclose");
1191 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1192 err = got_error_from_errno("fclose");
1193 free(base_path);
1194 if (blob_orig_path) {
1195 unlink(blob_orig_path);
1196 free(blob_orig_path);
1198 if (blob_deriv_path) {
1199 unlink(blob_deriv_path);
1200 free(blob_deriv_path);
1202 free(id_str);
1203 free(label_deriv);
1204 free(parent);
1205 return err;
1208 static const struct got_error *
1209 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1210 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1211 int wt_fd, const char *path, struct got_object_id *blob_id)
1213 const struct got_error *err = NULL;
1214 struct got_fileindex_entry *new_ie;
1216 *new_iep = NULL;
1218 err = got_fileindex_entry_alloc(&new_ie, path);
1219 if (err)
1220 return err;
1222 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1223 blob_id->sha1, base_commit_id->sha1, 1);
1224 if (err)
1225 goto done;
1227 err = got_fileindex_entry_add(fileindex, new_ie);
1228 done:
1229 if (err)
1230 got_fileindex_entry_free(new_ie);
1231 else
1232 *new_iep = new_ie;
1233 return err;
1236 static mode_t
1237 get_ondisk_perms(int executable, mode_t st_mode)
1239 mode_t xbits = S_IXUSR;
1241 if (executable) {
1242 /* Map read bits to execute bits. */
1243 if (st_mode & S_IRGRP)
1244 xbits |= S_IXGRP;
1245 if (st_mode & S_IROTH)
1246 xbits |= S_IXOTH;
1247 return st_mode | xbits;
1250 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1253 /* forward declaration */
1254 static const struct got_error *
1255 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1256 const char *path, mode_t te_mode, mode_t st_mode,
1257 struct got_blob_object *blob, int restoring_missing_file,
1258 int reverting_versioned_file, int installing_bad_symlink,
1259 int path_is_unversioned, struct got_repository *repo,
1260 got_worktree_checkout_cb progress_cb, void *progress_arg);
1263 * This function assumes that the provided symlink target points at a
1264 * safe location in the work tree!
1266 static const struct got_error *
1267 replace_existing_symlink(int *did_something, const char *ondisk_path,
1268 const char *target_path, size_t target_len)
1270 const struct got_error *err = NULL;
1271 ssize_t elen;
1272 char etarget[PATH_MAX];
1273 int fd;
1275 *did_something = 0;
1278 * "Bad" symlinks (those pointing outside the work tree or into the
1279 * .got directory) are installed in the work tree as a regular file
1280 * which contains the bad symlink target path.
1281 * The new symlink target has already been checked for safety by our
1282 * caller. If we can successfully open a regular file then we simply
1283 * replace this file with a symlink below.
1285 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1286 if (fd == -1) {
1287 if (errno != ELOOP)
1288 return got_error_from_errno2("open", ondisk_path);
1290 /* We are updating an existing on-disk symlink. */
1291 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1292 if (elen == -1)
1293 return got_error_from_errno2("readlink", ondisk_path);
1295 if (elen == target_len &&
1296 memcmp(etarget, target_path, target_len) == 0)
1297 return NULL; /* nothing to do */
1300 *did_something = 1;
1301 err = update_symlink(ondisk_path, target_path, target_len);
1302 if (fd != -1 && close(fd) == -1 && err == NULL)
1303 err = got_error_from_errno2("close", ondisk_path);
1304 return err;
1307 static const struct got_error *
1308 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1309 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1311 const struct got_error *err = NULL;
1312 char canonpath[PATH_MAX];
1313 char *path_got = NULL;
1315 *is_bad_symlink = 0;
1317 if (target_len >= sizeof(canonpath)) {
1318 *is_bad_symlink = 1;
1319 return NULL;
1323 * We do not use realpath(3) to resolve the symlink's target
1324 * path because we don't want to resolve symlinks recursively.
1325 * Instead we make the path absolute and then canonicalize it.
1326 * Relative symlink target lookup should begin at the directory
1327 * in which the blob object is being installed.
1329 if (!got_path_is_absolute(target_path)) {
1330 char *abspath, *parent;
1331 err = got_path_dirname(&parent, ondisk_path);
1332 if (err)
1333 return err;
1334 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1335 free(parent);
1336 return got_error_from_errno("asprintf");
1338 free(parent);
1339 if (strlen(abspath) >= sizeof(canonpath)) {
1340 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1341 free(abspath);
1342 return err;
1344 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1345 free(abspath);
1346 if (err)
1347 return err;
1348 } else {
1349 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1350 if (err)
1351 return err;
1354 /* Only allow symlinks pointing at paths within the work tree. */
1355 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1356 *is_bad_symlink = 1;
1357 return NULL;
1360 /* Do not allow symlinks pointing into the .got directory. */
1361 if (asprintf(&path_got, "%s/%s", wtroot_path,
1362 GOT_WORKTREE_GOT_DIR) == -1)
1363 return got_error_from_errno("asprintf");
1364 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1365 *is_bad_symlink = 1;
1367 free(path_got);
1368 return NULL;
1371 static const struct got_error *
1372 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1373 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1374 int restoring_missing_file, int reverting_versioned_file,
1375 int path_is_unversioned, struct got_repository *repo,
1376 got_worktree_checkout_cb progress_cb, void *progress_arg)
1378 const struct got_error *err = NULL;
1379 char target_path[PATH_MAX];
1380 size_t len, target_len = 0;
1381 char *path_got = NULL;
1382 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1383 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1385 *is_bad_symlink = 0;
1388 * Blob object content specifies the target path of the link.
1389 * If a symbolic link cannot be installed we instead create
1390 * a regular file which contains the link target path stored
1391 * in the blob object.
1393 do {
1394 err = got_object_blob_read_block(&len, blob);
1395 if (len + target_len >= sizeof(target_path)) {
1396 /* Path too long; install as a regular file. */
1397 *is_bad_symlink = 1;
1398 got_object_blob_rewind(blob);
1399 return install_blob(worktree, ondisk_path, path,
1400 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1401 restoring_missing_file, reverting_versioned_file,
1402 1, path_is_unversioned, repo, progress_cb,
1403 progress_arg);
1405 if (len > 0) {
1406 /* Skip blob object header first time around. */
1407 memcpy(target_path + target_len, buf + hdrlen,
1408 len - hdrlen);
1409 target_len += len - hdrlen;
1410 hdrlen = 0;
1412 } while (len != 0);
1413 target_path[target_len] = '\0';
1415 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1416 ondisk_path, worktree->root_path);
1417 if (err)
1418 return err;
1420 if (*is_bad_symlink) {
1421 /* install as a regular file */
1422 got_object_blob_rewind(blob);
1423 err = install_blob(worktree, ondisk_path, path,
1424 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1425 restoring_missing_file, reverting_versioned_file, 1,
1426 path_is_unversioned, repo, progress_cb, progress_arg);
1427 goto done;
1430 if (symlink(target_path, ondisk_path) == -1) {
1431 if (errno == EEXIST) {
1432 int symlink_replaced;
1433 if (path_is_unversioned) {
1434 err = (*progress_cb)(progress_arg,
1435 GOT_STATUS_UNVERSIONED, path);
1436 goto done;
1438 err = replace_existing_symlink(&symlink_replaced,
1439 ondisk_path, target_path, target_len);
1440 if (err)
1441 goto done;
1442 if (progress_cb) {
1443 if (symlink_replaced) {
1444 err = (*progress_cb)(progress_arg,
1445 reverting_versioned_file ?
1446 GOT_STATUS_REVERT :
1447 GOT_STATUS_UPDATE, path);
1448 } else {
1449 err = (*progress_cb)(progress_arg,
1450 GOT_STATUS_EXISTS, path);
1453 goto done; /* Nothing else to do. */
1456 if (errno == ENOENT) {
1457 char *parent;
1458 err = got_path_dirname(&parent, ondisk_path);
1459 if (err)
1460 goto done;
1461 err = add_dir_on_disk(worktree, parent);
1462 free(parent);
1463 if (err)
1464 goto done;
1466 * Retry, and fall through to error handling
1467 * below if this second attempt fails.
1469 if (symlink(target_path, ondisk_path) != -1) {
1470 err = NULL; /* success */
1471 goto done;
1475 /* Handle errors from first or second creation attempt. */
1476 if (errno == ENAMETOOLONG) {
1477 /* bad target path; install as a regular file */
1478 *is_bad_symlink = 1;
1479 got_object_blob_rewind(blob);
1480 err = install_blob(worktree, ondisk_path, path,
1481 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1482 restoring_missing_file, reverting_versioned_file, 1,
1483 path_is_unversioned, repo,
1484 progress_cb, progress_arg);
1485 } else if (errno == ENOTDIR) {
1486 err = got_error_path(ondisk_path,
1487 GOT_ERR_FILE_OBSTRUCTED);
1488 } else {
1489 err = got_error_from_errno3("symlink",
1490 target_path, ondisk_path);
1492 } else if (progress_cb)
1493 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1494 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1495 done:
1496 free(path_got);
1497 return err;
1500 static const struct got_error *
1501 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1502 const char *path, mode_t te_mode, mode_t st_mode,
1503 struct got_blob_object *blob, int restoring_missing_file,
1504 int reverting_versioned_file, int installing_bad_symlink,
1505 int path_is_unversioned, struct got_repository *repo,
1506 got_worktree_checkout_cb progress_cb, void *progress_arg)
1508 const struct got_error *err = NULL;
1509 int fd = -1;
1510 size_t len, hdrlen;
1511 int update = 0;
1512 char *tmppath = NULL;
1514 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1515 GOT_DEFAULT_FILE_MODE);
1516 if (fd == -1) {
1517 if (errno == ENOENT) {
1518 char *parent;
1519 err = got_path_dirname(&parent, path);
1520 if (err)
1521 return err;
1522 err = add_dir_on_disk(worktree, parent);
1523 free(parent);
1524 if (err)
1525 return err;
1526 fd = open(ondisk_path,
1527 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1528 GOT_DEFAULT_FILE_MODE);
1529 if (fd == -1)
1530 return got_error_from_errno2("open",
1531 ondisk_path);
1532 } else if (errno == EEXIST) {
1533 if (path_is_unversioned) {
1534 err = (*progress_cb)(progress_arg,
1535 GOT_STATUS_UNVERSIONED, path);
1536 goto done;
1538 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1539 !S_ISREG(st_mode) && !installing_bad_symlink) {
1540 /* TODO file is obstructed; do something */
1541 err = got_error_path(ondisk_path,
1542 GOT_ERR_FILE_OBSTRUCTED);
1543 goto done;
1544 } else {
1545 err = got_opentemp_named_fd(&tmppath, &fd,
1546 ondisk_path);
1547 if (err)
1548 goto done;
1549 update = 1;
1551 } else
1552 return got_error_from_errno2("open", ondisk_path);
1555 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1556 err = got_error_from_errno2("fchmod",
1557 update ? tmppath : ondisk_path);
1558 goto done;
1561 if (progress_cb) {
1562 if (restoring_missing_file)
1563 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1564 path);
1565 else if (reverting_versioned_file)
1566 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1567 path);
1568 else
1569 err = (*progress_cb)(progress_arg,
1570 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1571 if (err)
1572 goto done;
1575 hdrlen = got_object_blob_get_hdrlen(blob);
1576 do {
1577 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1578 err = got_object_blob_read_block(&len, blob);
1579 if (err)
1580 break;
1581 if (len > 0) {
1582 /* Skip blob object header first time around. */
1583 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1584 if (outlen == -1) {
1585 err = got_error_from_errno("write");
1586 goto done;
1587 } else if (outlen != len - hdrlen) {
1588 err = got_error(GOT_ERR_IO);
1589 goto done;
1591 hdrlen = 0;
1593 } while (len != 0);
1595 if (fsync(fd) != 0) {
1596 err = got_error_from_errno("fsync");
1597 goto done;
1600 if (update) {
1601 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1602 err = got_error_from_errno2("unlink", ondisk_path);
1603 goto done;
1605 if (rename(tmppath, ondisk_path) != 0) {
1606 err = got_error_from_errno3("rename", tmppath,
1607 ondisk_path);
1608 goto done;
1610 free(tmppath);
1611 tmppath = NULL;
1614 done:
1615 if (fd != -1 && close(fd) == -1 && err == NULL)
1616 err = got_error_from_errno("close");
1617 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1618 err = got_error_from_errno2("unlink", tmppath);
1619 free(tmppath);
1620 return err;
1623 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1624 static const struct got_error *
1625 get_modified_file_content_status(unsigned char *status, FILE *f)
1627 const struct got_error *err = NULL;
1628 const char *markers[3] = {
1629 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1630 GOT_DIFF_CONFLICT_MARKER_SEP,
1631 GOT_DIFF_CONFLICT_MARKER_END
1633 int i = 0;
1634 char *line = NULL;
1635 size_t linesize = 0;
1636 ssize_t linelen;
1638 while (*status == GOT_STATUS_MODIFY) {
1639 linelen = getline(&line, &linesize, f);
1640 if (linelen == -1) {
1641 if (feof(f))
1642 break;
1643 err = got_ferror(f, GOT_ERR_IO);
1644 break;
1647 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1648 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1649 == 0)
1650 *status = GOT_STATUS_CONFLICT;
1651 else
1652 i++;
1655 free(line);
1657 return err;
1660 static int
1661 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1663 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1664 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1667 static int
1668 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1670 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1671 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1672 ie->mtime_sec == sb->st_mtim.tv_sec &&
1673 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1674 ie->size == (sb->st_size & 0xffffffff) &&
1675 !xbit_differs(ie, sb->st_mode));
1678 static unsigned char
1679 get_staged_status(struct got_fileindex_entry *ie)
1681 switch (got_fileindex_entry_stage_get(ie)) {
1682 case GOT_FILEIDX_STAGE_ADD:
1683 return GOT_STATUS_ADD;
1684 case GOT_FILEIDX_STAGE_DELETE:
1685 return GOT_STATUS_DELETE;
1686 case GOT_FILEIDX_STAGE_MODIFY:
1687 return GOT_STATUS_MODIFY;
1688 default:
1689 return GOT_STATUS_NO_CHANGE;
1693 static const struct got_error *
1694 get_symlink_modification_status(unsigned char *status,
1695 struct got_fileindex_entry *ie, const char *abspath,
1696 int dirfd, const char *de_name, struct got_blob_object *blob)
1698 const struct got_error *err = NULL;
1699 char target_path[PATH_MAX];
1700 char etarget[PATH_MAX];
1701 ssize_t elen;
1702 size_t len, target_len = 0;
1703 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1704 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1706 *status = GOT_STATUS_NO_CHANGE;
1708 /* Blob object content specifies the target path of the link. */
1709 do {
1710 err = got_object_blob_read_block(&len, blob);
1711 if (err)
1712 return err;
1713 if (len + target_len >= sizeof(target_path)) {
1715 * Should not happen. The blob contents were OK
1716 * when this symlink was installed.
1718 return got_error(GOT_ERR_NO_SPACE);
1720 if (len > 0) {
1721 /* Skip blob object header first time around. */
1722 memcpy(target_path + target_len, buf + hdrlen,
1723 len - hdrlen);
1724 target_len += len - hdrlen;
1725 hdrlen = 0;
1727 } while (len != 0);
1728 target_path[target_len] = '\0';
1730 if (dirfd != -1) {
1731 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1732 if (elen == -1)
1733 return got_error_from_errno2("readlinkat", abspath);
1734 } else {
1735 elen = readlink(abspath, etarget, sizeof(etarget));
1736 if (elen == -1)
1737 return got_error_from_errno2("readlink", abspath);
1740 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1741 *status = GOT_STATUS_MODIFY;
1743 return NULL;
1746 static const struct got_error *
1747 get_file_status(unsigned char *status, struct stat *sb,
1748 struct got_fileindex_entry *ie, const char *abspath,
1749 int dirfd, const char *de_name, struct got_repository *repo)
1751 const struct got_error *err = NULL;
1752 struct got_object_id id;
1753 size_t hdrlen;
1754 int fd = -1;
1755 FILE *f = NULL;
1756 uint8_t fbuf[8192];
1757 struct got_blob_object *blob = NULL;
1758 size_t flen, blen;
1759 unsigned char staged_status = get_staged_status(ie);
1761 *status = GOT_STATUS_NO_CHANGE;
1762 memset(sb, 0, sizeof(*sb));
1765 * Whenever the caller provides a directory descriptor and a
1766 * directory entry name for the file, use them! This prevents
1767 * race conditions if filesystem paths change beneath our feet.
1769 if (dirfd != -1) {
1770 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1771 if (errno == ENOENT) {
1772 if (got_fileindex_entry_has_file_on_disk(ie))
1773 *status = GOT_STATUS_MISSING;
1774 else
1775 *status = GOT_STATUS_DELETE;
1776 goto done;
1778 err = got_error_from_errno2("fstatat", abspath);
1779 goto done;
1781 } else {
1782 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1783 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1784 return got_error_from_errno2("open", abspath);
1785 else if (fd == -1 && errno == ELOOP) {
1786 if (lstat(abspath, sb) == -1)
1787 return got_error_from_errno2("lstat", abspath);
1788 } else if (fd == -1 || fstat(fd, sb) == -1) {
1789 if (errno == ENOENT) {
1790 if (got_fileindex_entry_has_file_on_disk(ie))
1791 *status = GOT_STATUS_MISSING;
1792 else
1793 *status = GOT_STATUS_DELETE;
1794 goto done;
1796 err = got_error_from_errno2("fstat", abspath);
1797 goto done;
1801 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1802 *status = GOT_STATUS_OBSTRUCTED;
1803 goto done;
1806 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1807 *status = GOT_STATUS_DELETE;
1808 goto done;
1809 } else if (!got_fileindex_entry_has_blob(ie) &&
1810 staged_status != GOT_STATUS_ADD) {
1811 *status = GOT_STATUS_ADD;
1812 goto done;
1815 if (!stat_info_differs(ie, sb))
1816 goto done;
1818 if (S_ISLNK(sb->st_mode) &&
1819 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1820 *status = GOT_STATUS_MODIFY;
1821 goto done;
1824 if (staged_status == GOT_STATUS_MODIFY ||
1825 staged_status == GOT_STATUS_ADD)
1826 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1827 else
1828 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1830 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1831 if (err)
1832 goto done;
1834 if (S_ISLNK(sb->st_mode)) {
1835 err = get_symlink_modification_status(status, ie,
1836 abspath, dirfd, de_name, blob);
1837 goto done;
1840 if (dirfd != -1) {
1841 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1842 if (fd == -1) {
1843 err = got_error_from_errno2("openat", abspath);
1844 goto done;
1848 f = fdopen(fd, "r");
1849 if (f == NULL) {
1850 err = got_error_from_errno2("fdopen", abspath);
1851 goto done;
1853 fd = -1;
1854 hdrlen = got_object_blob_get_hdrlen(blob);
1855 for (;;) {
1856 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1857 err = got_object_blob_read_block(&blen, blob);
1858 if (err)
1859 goto done;
1860 /* Skip length of blob object header first time around. */
1861 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1862 if (flen == 0 && ferror(f)) {
1863 err = got_error_from_errno("fread");
1864 goto done;
1866 if (blen - hdrlen == 0) {
1867 if (flen != 0)
1868 *status = GOT_STATUS_MODIFY;
1869 break;
1870 } else if (flen == 0) {
1871 if (blen - hdrlen != 0)
1872 *status = GOT_STATUS_MODIFY;
1873 break;
1874 } else if (blen - hdrlen == flen) {
1875 /* Skip blob object header first time around. */
1876 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1877 *status = GOT_STATUS_MODIFY;
1878 break;
1880 } else {
1881 *status = GOT_STATUS_MODIFY;
1882 break;
1884 hdrlen = 0;
1887 if (*status == GOT_STATUS_MODIFY) {
1888 rewind(f);
1889 err = get_modified_file_content_status(status, f);
1890 } else if (xbit_differs(ie, sb->st_mode))
1891 *status = GOT_STATUS_MODE_CHANGE;
1892 done:
1893 if (blob)
1894 got_object_blob_close(blob);
1895 if (f != NULL && fclose(f) == EOF && err == NULL)
1896 err = got_error_from_errno2("fclose", abspath);
1897 if (fd != -1 && close(fd) == -1 && err == NULL)
1898 err = got_error_from_errno2("close", abspath);
1899 return err;
1903 * Update timestamps in the file index if a file is unmodified and
1904 * we had to run a full content comparison to find out.
1906 static const struct got_error *
1907 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1908 struct got_fileindex_entry *ie, struct stat *sb)
1910 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1911 return got_fileindex_entry_update(ie, wt_fd, path,
1912 ie->blob_sha1, ie->commit_sha1, 1);
1914 return NULL;
1917 static const struct got_error *
1918 update_blob(struct got_worktree *worktree,
1919 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1920 struct got_tree_entry *te, const char *path,
1921 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1922 void *progress_arg)
1924 const struct got_error *err = NULL;
1925 struct got_blob_object *blob = NULL;
1926 char *ondisk_path;
1927 unsigned char status = GOT_STATUS_NO_CHANGE;
1928 struct stat sb;
1930 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1931 return got_error_from_errno("asprintf");
1933 if (ie) {
1934 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1935 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1936 goto done;
1938 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1939 repo);
1940 if (err)
1941 goto done;
1942 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1943 sb.st_mode = got_fileindex_perms_to_st(ie);
1944 } else {
1945 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1946 status = GOT_STATUS_UNVERSIONED;
1949 if (status == GOT_STATUS_OBSTRUCTED) {
1950 err = (*progress_cb)(progress_arg, status, path);
1951 goto done;
1953 if (status == GOT_STATUS_CONFLICT) {
1954 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1955 path);
1956 goto done;
1959 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1960 (S_ISLNK(te->mode) ||
1961 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1963 * This is a regular file or an installed bad symlink.
1964 * If the file index indicates that this file is already
1965 * up-to-date with respect to the repository we can skip
1966 * updating contents of this file.
1968 if (got_fileindex_entry_has_commit(ie) &&
1969 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1970 SHA1_DIGEST_LENGTH) == 0) {
1971 /* Same commit. */
1972 err = sync_timestamps(worktree->root_fd,
1973 path, status, ie, &sb);
1974 if (err)
1975 goto done;
1976 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1977 path);
1978 goto done;
1980 if (got_fileindex_entry_has_blob(ie) &&
1981 memcmp(ie->blob_sha1, te->id.sha1,
1982 SHA1_DIGEST_LENGTH) == 0) {
1983 /* Different commit but the same blob. */
1984 err = sync_timestamps(worktree->root_fd,
1985 path, status, ie, &sb);
1986 if (err)
1987 goto done;
1988 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1989 path);
1990 goto done;
1994 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1995 if (err)
1996 goto done;
1998 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1999 int update_timestamps;
2000 struct got_blob_object *blob2 = NULL;
2001 char *label_orig = NULL;
2002 if (got_fileindex_entry_has_blob(ie)) {
2003 struct got_object_id id2;
2004 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2005 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2006 if (err)
2007 goto done;
2009 if (got_fileindex_entry_has_commit(ie)) {
2010 char id_str[SHA1_DIGEST_STRING_LENGTH];
2011 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2012 sizeof(id_str)) == NULL) {
2013 err = got_error_path(id_str,
2014 GOT_ERR_BAD_OBJ_ID_STR);
2015 goto done;
2017 if (asprintf(&label_orig, "%s: commit %s",
2018 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2019 err = got_error_from_errno("asprintf");
2020 goto done;
2023 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2024 char *link_target;
2025 err = got_object_blob_read_to_str(&link_target, blob);
2026 if (err)
2027 goto done;
2028 err = merge_symlink(worktree, blob2, ondisk_path, path,
2029 label_orig, link_target, worktree->base_commit_id,
2030 repo, progress_cb, progress_arg);
2031 free(link_target);
2032 } else {
2033 err = merge_blob(&update_timestamps, worktree, blob2,
2034 ondisk_path, path, sb.st_mode, label_orig, blob,
2035 worktree->base_commit_id, repo,
2036 progress_cb, progress_arg);
2038 free(label_orig);
2039 if (blob2)
2040 got_object_blob_close(blob2);
2041 if (err)
2042 goto done;
2044 * Do not update timestamps of files with local changes.
2045 * Otherwise, a future status walk would treat them as
2046 * unmodified files again.
2048 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2049 blob->id.sha1, worktree->base_commit_id->sha1,
2050 update_timestamps);
2051 } else if (status == GOT_STATUS_MODE_CHANGE) {
2052 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2053 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2054 } else if (status == GOT_STATUS_DELETE) {
2055 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2056 if (err)
2057 goto done;
2058 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2059 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2060 if (err)
2061 goto done;
2062 } else {
2063 int is_bad_symlink = 0;
2064 if (S_ISLNK(te->mode)) {
2065 err = install_symlink(&is_bad_symlink, worktree,
2066 ondisk_path, path, blob,
2067 status == GOT_STATUS_MISSING, 0,
2068 status == GOT_STATUS_UNVERSIONED, repo,
2069 progress_cb, progress_arg);
2070 } else {
2071 err = install_blob(worktree, ondisk_path, path,
2072 te->mode, sb.st_mode, blob,
2073 status == GOT_STATUS_MISSING, 0, 0,
2074 status == GOT_STATUS_UNVERSIONED, repo,
2075 progress_cb, progress_arg);
2077 if (err)
2078 goto done;
2080 if (ie) {
2081 err = got_fileindex_entry_update(ie,
2082 worktree->root_fd, path, blob->id.sha1,
2083 worktree->base_commit_id->sha1, 1);
2084 } else {
2085 err = create_fileindex_entry(&ie, fileindex,
2086 worktree->base_commit_id, worktree->root_fd, path,
2087 &blob->id);
2089 if (err)
2090 goto done;
2092 if (is_bad_symlink) {
2093 got_fileindex_entry_filetype_set(ie,
2094 GOT_FILEIDX_MODE_BAD_SYMLINK);
2097 got_object_blob_close(blob);
2098 done:
2099 free(ondisk_path);
2100 return err;
2103 static const struct got_error *
2104 remove_ondisk_file(const char *root_path, const char *path)
2106 const struct got_error *err = NULL;
2107 char *ondisk_path = NULL, *parent = NULL;
2109 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2110 return got_error_from_errno("asprintf");
2112 if (unlink(ondisk_path) == -1) {
2113 if (errno != ENOENT)
2114 err = got_error_from_errno2("unlink", ondisk_path);
2115 } else {
2116 size_t root_len = strlen(root_path);
2117 err = got_path_dirname(&parent, ondisk_path);
2118 if (err)
2119 goto done;
2120 while (got_path_cmp(parent, root_path,
2121 strlen(parent), root_len) != 0) {
2122 free(ondisk_path);
2123 ondisk_path = parent;
2124 parent = NULL;
2125 if (rmdir(ondisk_path) == -1) {
2126 if (errno != ENOTEMPTY)
2127 err = got_error_from_errno2("rmdir",
2128 ondisk_path);
2129 break;
2131 err = got_path_dirname(&parent, ondisk_path);
2132 if (err)
2133 break;
2136 done:
2137 free(ondisk_path);
2138 free(parent);
2139 return err;
2142 static const struct got_error *
2143 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2144 struct got_fileindex_entry *ie, struct got_repository *repo,
2145 got_worktree_checkout_cb progress_cb, void *progress_arg)
2147 const struct got_error *err = NULL;
2148 unsigned char status;
2149 struct stat sb;
2150 char *ondisk_path;
2152 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2153 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2155 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2156 == -1)
2157 return got_error_from_errno("asprintf");
2159 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2160 if (err)
2161 goto done;
2163 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2164 char ondisk_target[PATH_MAX];
2165 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2166 sizeof(ondisk_target));
2167 if (ondisk_len == -1) {
2168 err = got_error_from_errno2("readlink", ondisk_path);
2169 goto done;
2171 ondisk_target[ondisk_len] = '\0';
2172 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2173 NULL, NULL, /* XXX pass common ancestor info? */
2174 ondisk_target, ondisk_path);
2175 if (err)
2176 goto done;
2177 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2178 ie->path);
2179 goto done;
2182 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2183 status == GOT_STATUS_ADD) {
2184 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2185 if (err)
2186 goto done;
2188 * Preserve the working file and change the deleted blob's
2189 * entry into a schedule-add entry.
2191 err = got_fileindex_entry_update(ie, worktree->root_fd,
2192 ie->path, NULL, NULL, 0);
2193 } else {
2194 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2195 if (err)
2196 goto done;
2197 if (status == GOT_STATUS_NO_CHANGE) {
2198 err = remove_ondisk_file(worktree->root_path, ie->path);
2199 if (err)
2200 goto done;
2202 got_fileindex_entry_remove(fileindex, ie);
2204 done:
2205 free(ondisk_path);
2206 return err;
2209 struct diff_cb_arg {
2210 struct got_fileindex *fileindex;
2211 struct got_worktree *worktree;
2212 struct got_repository *repo;
2213 got_worktree_checkout_cb progress_cb;
2214 void *progress_arg;
2215 got_cancel_cb cancel_cb;
2216 void *cancel_arg;
2219 static const struct got_error *
2220 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2221 struct got_tree_entry *te, const char *parent_path)
2223 struct diff_cb_arg *a = arg;
2225 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2226 return got_error(GOT_ERR_CANCELLED);
2228 return update_blob(a->worktree, a->fileindex, ie, te,
2229 ie->path, a->repo, a->progress_cb, a->progress_arg);
2232 static const struct got_error *
2233 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2235 struct diff_cb_arg *a = arg;
2237 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2238 return got_error(GOT_ERR_CANCELLED);
2240 return delete_blob(a->worktree, a->fileindex, ie,
2241 a->repo, a->progress_cb, a->progress_arg);
2244 static const struct got_error *
2245 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2247 struct diff_cb_arg *a = arg;
2248 const struct got_error *err;
2249 char *path;
2251 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2252 return got_error(GOT_ERR_CANCELLED);
2254 if (got_object_tree_entry_is_submodule(te))
2255 return NULL;
2257 if (asprintf(&path, "%s%s%s", parent_path,
2258 parent_path[0] ? "/" : "", te->name)
2259 == -1)
2260 return got_error_from_errno("asprintf");
2262 if (S_ISDIR(te->mode))
2263 err = add_dir_on_disk(a->worktree, path);
2264 else
2265 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2266 a->repo, a->progress_cb, a->progress_arg);
2268 free(path);
2269 return err;
2272 const struct got_error *
2273 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2275 uint32_t uuid_status;
2277 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2278 if (uuid_status != uuid_s_ok) {
2279 *uuidstr = NULL;
2280 return got_error_uuid(uuid_status, "uuid_to_string");
2283 return NULL;
2286 static const struct got_error *
2287 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2289 const struct got_error *err = NULL;
2290 char *uuidstr = NULL;
2292 *refname = NULL;
2294 err = got_worktree_get_uuid(&uuidstr, worktree);
2295 if (err)
2296 return err;
2298 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2299 err = got_error_from_errno("asprintf");
2300 *refname = NULL;
2302 free(uuidstr);
2303 return err;
2306 const struct got_error *
2307 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2309 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2312 static const struct got_error *
2313 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2315 return get_ref_name(refname, worktree,
2316 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2319 static const struct got_error *
2320 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2322 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2325 static const struct got_error *
2326 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2328 return get_ref_name(refname, worktree,
2329 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2332 static const struct got_error *
2333 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2335 return get_ref_name(refname, worktree,
2336 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2339 static const struct got_error *
2340 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2342 return get_ref_name(refname, worktree,
2343 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2346 static const struct got_error *
2347 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2349 return get_ref_name(refname, worktree,
2350 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2353 static const struct got_error *
2354 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2356 return get_ref_name(refname, worktree,
2357 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2360 static const struct got_error *
2361 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2363 return get_ref_name(refname, worktree,
2364 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2367 const struct got_error *
2368 got_worktree_get_histedit_script_path(char **path,
2369 struct got_worktree *worktree)
2371 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2372 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2373 *path = NULL;
2374 return got_error_from_errno("asprintf");
2376 return NULL;
2380 * Prevent Git's garbage collector from deleting our base commit by
2381 * setting a reference to our base commit's ID.
2383 static const struct got_error *
2384 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2386 const struct got_error *err = NULL;
2387 struct got_reference *ref = NULL;
2388 char *refname;
2390 err = got_worktree_get_base_ref_name(&refname, worktree);
2391 if (err)
2392 return err;
2394 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2395 if (err)
2396 goto done;
2398 err = got_ref_write(ref, repo);
2399 done:
2400 free(refname);
2401 if (ref)
2402 got_ref_close(ref);
2403 return err;
2406 static const struct got_error *
2407 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2409 const struct got_error *err = NULL;
2411 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2412 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2413 err = got_error_from_errno("asprintf");
2414 *fileindex_path = NULL;
2416 return err;
2420 static const struct got_error *
2421 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2422 struct got_worktree *worktree)
2424 const struct got_error *err = NULL;
2425 FILE *index = NULL;
2427 *fileindex_path = NULL;
2428 *fileindex = got_fileindex_alloc();
2429 if (*fileindex == NULL)
2430 return got_error_from_errno("got_fileindex_alloc");
2432 err = get_fileindex_path(fileindex_path, worktree);
2433 if (err)
2434 goto done;
2436 index = fopen(*fileindex_path, "rb");
2437 if (index == NULL) {
2438 if (errno != ENOENT)
2439 err = got_error_from_errno2("fopen", *fileindex_path);
2440 } else {
2441 err = got_fileindex_read(*fileindex, index);
2442 if (fclose(index) == EOF && err == NULL)
2443 err = got_error_from_errno("fclose");
2445 done:
2446 if (err) {
2447 free(*fileindex_path);
2448 *fileindex_path = NULL;
2449 got_fileindex_free(*fileindex);
2450 *fileindex = NULL;
2452 return err;
2455 struct bump_base_commit_id_arg {
2456 struct got_object_id *base_commit_id;
2457 const char *path;
2458 size_t path_len;
2459 const char *entry_name;
2460 got_worktree_checkout_cb progress_cb;
2461 void *progress_arg;
2464 /* Bump base commit ID of all files within an updated part of the work tree. */
2465 static const struct got_error *
2466 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2468 const struct got_error *err;
2469 struct bump_base_commit_id_arg *a = arg;
2471 if (a->entry_name) {
2472 if (strcmp(ie->path, a->path) != 0)
2473 return NULL;
2474 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2475 return NULL;
2477 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2478 SHA1_DIGEST_LENGTH) == 0)
2479 return NULL;
2481 if (a->progress_cb) {
2482 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2483 ie->path);
2484 if (err)
2485 return err;
2487 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2488 return NULL;
2491 static const struct got_error *
2492 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2493 struct got_fileindex *fileindex,
2494 got_worktree_checkout_cb progress_cb, void *progress_arg)
2496 struct bump_base_commit_id_arg bbc_arg;
2498 bbc_arg.base_commit_id = worktree->base_commit_id;
2499 bbc_arg.entry_name = NULL;
2500 bbc_arg.path = "";
2501 bbc_arg.path_len = 0;
2502 bbc_arg.progress_cb = progress_cb;
2503 bbc_arg.progress_arg = progress_arg;
2505 return got_fileindex_for_each_entry_safe(fileindex,
2506 bump_base_commit_id, &bbc_arg);
2509 static const struct got_error *
2510 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2512 const struct got_error *err = NULL;
2513 char *new_fileindex_path = NULL;
2514 FILE *new_index = NULL;
2515 struct timespec timeout;
2517 err = got_opentemp_named(&new_fileindex_path, &new_index,
2518 fileindex_path);
2519 if (err)
2520 goto done;
2522 err = got_fileindex_write(fileindex, new_index);
2523 if (err)
2524 goto done;
2526 if (rename(new_fileindex_path, fileindex_path) != 0) {
2527 err = got_error_from_errno3("rename", new_fileindex_path,
2528 fileindex_path);
2529 unlink(new_fileindex_path);
2533 * Sleep for a short amount of time to ensure that files modified after
2534 * this program exits have a different time stamp from the one which
2535 * was recorded in the file index.
2537 timeout.tv_sec = 0;
2538 timeout.tv_nsec = 1;
2539 nanosleep(&timeout, NULL);
2540 done:
2541 if (new_index)
2542 fclose(new_index);
2543 free(new_fileindex_path);
2544 return err;
2547 static const struct got_error *
2548 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2549 struct got_object_id **tree_id, const char *wt_relpath,
2550 struct got_worktree *worktree, struct got_repository *repo)
2552 const struct got_error *err = NULL;
2553 struct got_object_id *id = NULL;
2554 char *in_repo_path = NULL;
2555 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2557 *entry_type = GOT_OBJ_TYPE_ANY;
2558 *tree_relpath = NULL;
2559 *tree_id = NULL;
2561 if (wt_relpath[0] == '\0') {
2562 /* Check out all files within the work tree. */
2563 *entry_type = GOT_OBJ_TYPE_TREE;
2564 *tree_relpath = strdup("");
2565 if (*tree_relpath == NULL) {
2566 err = got_error_from_errno("strdup");
2567 goto done;
2569 err = got_object_id_by_path(tree_id, repo,
2570 worktree->base_commit_id, worktree->path_prefix);
2571 if (err)
2572 goto done;
2573 return NULL;
2576 /* Check out a subset of files in the work tree. */
2578 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2579 is_root_wt ? "" : "/", wt_relpath) == -1) {
2580 err = got_error_from_errno("asprintf");
2581 goto done;
2584 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2585 in_repo_path);
2586 if (err)
2587 goto done;
2589 free(in_repo_path);
2590 in_repo_path = NULL;
2592 err = got_object_get_type(entry_type, repo, id);
2593 if (err)
2594 goto done;
2596 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2597 /* Check out a single file. */
2598 if (strchr(wt_relpath, '/') == NULL) {
2599 /* Check out a single file in work tree's root dir. */
2600 in_repo_path = strdup(worktree->path_prefix);
2601 if (in_repo_path == NULL) {
2602 err = got_error_from_errno("strdup");
2603 goto done;
2605 *tree_relpath = strdup("");
2606 if (*tree_relpath == NULL) {
2607 err = got_error_from_errno("strdup");
2608 goto done;
2610 } else {
2611 /* Check out a single file in a subdirectory. */
2612 err = got_path_dirname(tree_relpath, wt_relpath);
2613 if (err)
2614 return err;
2615 if (asprintf(&in_repo_path, "%s%s%s",
2616 worktree->path_prefix, is_root_wt ? "" : "/",
2617 *tree_relpath) == -1) {
2618 err = got_error_from_errno("asprintf");
2619 goto done;
2622 err = got_object_id_by_path(tree_id, repo,
2623 worktree->base_commit_id, in_repo_path);
2624 } else {
2625 /* Check out all files within a subdirectory. */
2626 *tree_id = got_object_id_dup(id);
2627 if (*tree_id == NULL) {
2628 err = got_error_from_errno("got_object_id_dup");
2629 goto done;
2631 *tree_relpath = strdup(wt_relpath);
2632 if (*tree_relpath == NULL) {
2633 err = got_error_from_errno("strdup");
2634 goto done;
2637 done:
2638 free(id);
2639 free(in_repo_path);
2640 if (err) {
2641 *entry_type = GOT_OBJ_TYPE_ANY;
2642 free(*tree_relpath);
2643 *tree_relpath = NULL;
2644 free(*tree_id);
2645 *tree_id = NULL;
2647 return err;
2650 static const struct got_error *
2651 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2652 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2653 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2654 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2656 const struct got_error *err = NULL;
2657 struct got_commit_object *commit = NULL;
2658 struct got_tree_object *tree = NULL;
2659 struct got_fileindex_diff_tree_cb diff_cb;
2660 struct diff_cb_arg arg;
2662 err = ref_base_commit(worktree, repo);
2663 if (err) {
2664 if (!(err->code == GOT_ERR_ERRNO &&
2665 (errno == EACCES || errno == EROFS)))
2666 goto done;
2667 err = (*progress_cb)(progress_arg,
2668 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2669 if (err)
2670 return err;
2673 err = got_object_open_as_commit(&commit, repo,
2674 worktree->base_commit_id);
2675 if (err)
2676 goto done;
2678 err = got_object_open_as_tree(&tree, repo, tree_id);
2679 if (err)
2680 goto done;
2682 if (entry_name &&
2683 got_object_tree_find_entry(tree, entry_name) == NULL) {
2684 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2685 goto done;
2688 diff_cb.diff_old_new = diff_old_new;
2689 diff_cb.diff_old = diff_old;
2690 diff_cb.diff_new = diff_new;
2691 arg.fileindex = fileindex;
2692 arg.worktree = worktree;
2693 arg.repo = repo;
2694 arg.progress_cb = progress_cb;
2695 arg.progress_arg = progress_arg;
2696 arg.cancel_cb = cancel_cb;
2697 arg.cancel_arg = cancel_arg;
2698 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2699 entry_name, repo, &diff_cb, &arg);
2700 done:
2701 if (tree)
2702 got_object_tree_close(tree);
2703 if (commit)
2704 got_object_commit_close(commit);
2705 return err;
2708 const struct got_error *
2709 got_worktree_checkout_files(struct got_worktree *worktree,
2710 struct got_pathlist_head *paths, struct got_repository *repo,
2711 got_worktree_checkout_cb progress_cb, void *progress_arg,
2712 got_cancel_cb cancel_cb, void *cancel_arg)
2714 const struct got_error *err = NULL, *sync_err, *unlockerr;
2715 struct got_commit_object *commit = NULL;
2716 struct got_tree_object *tree = NULL;
2717 struct got_fileindex *fileindex = NULL;
2718 char *fileindex_path = NULL;
2719 struct got_pathlist_entry *pe;
2720 struct tree_path_data {
2721 SIMPLEQ_ENTRY(tree_path_data) entry;
2722 struct got_object_id *tree_id;
2723 int entry_type;
2724 char *relpath;
2725 char *entry_name;
2726 } *tpd = NULL;
2727 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2729 SIMPLEQ_INIT(&tree_paths);
2731 err = lock_worktree(worktree, LOCK_EX);
2732 if (err)
2733 return err;
2735 /* Map all specified paths to in-repository trees. */
2736 TAILQ_FOREACH(pe, paths, entry) {
2737 tpd = malloc(sizeof(*tpd));
2738 if (tpd == NULL) {
2739 err = got_error_from_errno("malloc");
2740 goto done;
2743 err = find_tree_entry_for_checkout(&tpd->entry_type,
2744 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2745 if (err) {
2746 free(tpd);
2747 goto done;
2750 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2751 err = got_path_basename(&tpd->entry_name, pe->path);
2752 if (err) {
2753 free(tpd->relpath);
2754 free(tpd->tree_id);
2755 free(tpd);
2756 goto done;
2758 } else
2759 tpd->entry_name = NULL;
2761 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2765 * Read the file index.
2766 * Checking out files is supposed to be an idempotent operation.
2767 * If the on-disk file index is incomplete we will try to complete it.
2769 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2770 if (err)
2771 goto done;
2773 tpd = SIMPLEQ_FIRST(&tree_paths);
2774 TAILQ_FOREACH(pe, paths, entry) {
2775 struct bump_base_commit_id_arg bbc_arg;
2777 err = checkout_files(worktree, fileindex, tpd->relpath,
2778 tpd->tree_id, tpd->entry_name, repo,
2779 progress_cb, progress_arg, cancel_cb, cancel_arg);
2780 if (err)
2781 break;
2783 bbc_arg.base_commit_id = worktree->base_commit_id;
2784 bbc_arg.entry_name = tpd->entry_name;
2785 bbc_arg.path = pe->path;
2786 bbc_arg.path_len = pe->path_len;
2787 bbc_arg.progress_cb = progress_cb;
2788 bbc_arg.progress_arg = progress_arg;
2789 err = got_fileindex_for_each_entry_safe(fileindex,
2790 bump_base_commit_id, &bbc_arg);
2791 if (err)
2792 break;
2794 tpd = SIMPLEQ_NEXT(tpd, entry);
2796 sync_err = sync_fileindex(fileindex, fileindex_path);
2797 if (sync_err && err == NULL)
2798 err = sync_err;
2799 done:
2800 free(fileindex_path);
2801 if (tree)
2802 got_object_tree_close(tree);
2803 if (commit)
2804 got_object_commit_close(commit);
2805 if (fileindex)
2806 got_fileindex_free(fileindex);
2807 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2808 tpd = SIMPLEQ_FIRST(&tree_paths);
2809 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2810 free(tpd->relpath);
2811 free(tpd->tree_id);
2812 free(tpd);
2814 unlockerr = lock_worktree(worktree, LOCK_SH);
2815 if (unlockerr && err == NULL)
2816 err = unlockerr;
2817 return err;
2820 struct merge_file_cb_arg {
2821 struct got_worktree *worktree;
2822 struct got_fileindex *fileindex;
2823 got_worktree_checkout_cb progress_cb;
2824 void *progress_arg;
2825 got_cancel_cb cancel_cb;
2826 void *cancel_arg;
2827 const char *label_orig;
2828 struct got_object_id *commit_id2;
2831 static const struct got_error *
2832 merge_file_cb(void *arg, struct got_blob_object *blob1,
2833 struct got_blob_object *blob2, struct got_object_id *id1,
2834 struct got_object_id *id2, const char *path1, const char *path2,
2835 mode_t mode1, mode_t mode2, struct got_repository *repo)
2837 static const struct got_error *err = NULL;
2838 struct merge_file_cb_arg *a = arg;
2839 struct got_fileindex_entry *ie;
2840 char *ondisk_path = NULL;
2841 struct stat sb;
2842 unsigned char status;
2843 int local_changes_subsumed;
2844 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2845 char *id_str = NULL, *label_deriv2 = NULL;
2847 if (blob1 && blob2) {
2848 ie = got_fileindex_entry_get(a->fileindex, path2,
2849 strlen(path2));
2850 if (ie == NULL)
2851 return (*a->progress_cb)(a->progress_arg,
2852 GOT_STATUS_MISSING, path2);
2854 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2855 path2) == -1)
2856 return got_error_from_errno("asprintf");
2858 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2859 repo);
2860 if (err)
2861 goto done;
2863 if (status == GOT_STATUS_DELETE) {
2864 err = (*a->progress_cb)(a->progress_arg,
2865 GOT_STATUS_MERGE, path2);
2866 goto done;
2868 if (status != GOT_STATUS_NO_CHANGE &&
2869 status != GOT_STATUS_MODIFY &&
2870 status != GOT_STATUS_CONFLICT &&
2871 status != GOT_STATUS_ADD) {
2872 err = (*a->progress_cb)(a->progress_arg, status, path2);
2873 goto done;
2876 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2877 char *link_target2;
2878 err = got_object_blob_read_to_str(&link_target2, blob2);
2879 if (err)
2880 goto done;
2881 err = merge_symlink(a->worktree, blob1, ondisk_path,
2882 path2, a->label_orig, link_target2, a->commit_id2,
2883 repo, a->progress_cb, a->progress_arg);
2884 free(link_target2);
2885 } else {
2886 int fd;
2888 f_orig = got_opentemp();
2889 if (f_orig == NULL) {
2890 err = got_error_from_errno("got_opentemp");
2891 goto done;
2893 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2894 f_orig, blob1);
2895 if (err)
2896 goto done;
2898 f_deriv2 = got_opentemp();
2899 if (f_deriv2 == NULL)
2900 goto done;
2901 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2902 f_deriv2, blob2);
2903 if (err)
2904 goto done;
2906 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2907 if (fd == -1) {
2908 err = got_error_from_errno2("open",
2909 ondisk_path);
2910 goto done;
2912 f_deriv = fdopen(fd, "r");
2913 if (f_deriv == NULL) {
2914 err = got_error_from_errno2("fdopen",
2915 ondisk_path);
2916 close(fd);
2917 goto done;
2919 err = got_object_id_str(&id_str, a->commit_id2);
2920 if (err)
2921 goto done;
2922 if (asprintf(&label_deriv2, "%s: commit %s",
2923 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2924 err = got_error_from_errno("asprintf");
2925 goto done;
2927 err = merge_file(&local_changes_subsumed, a->worktree,
2928 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2929 sb.st_mode, a->label_orig, NULL, label_deriv2,
2930 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2931 a->progress_cb, a->progress_arg);
2933 } else if (blob1) {
2934 ie = got_fileindex_entry_get(a->fileindex, path1,
2935 strlen(path1));
2936 if (ie == NULL)
2937 return (*a->progress_cb)(a->progress_arg,
2938 GOT_STATUS_MISSING, path1);
2940 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2941 path1) == -1)
2942 return got_error_from_errno("asprintf");
2944 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2945 repo);
2946 if (err)
2947 goto done;
2949 switch (status) {
2950 case GOT_STATUS_NO_CHANGE:
2951 err = (*a->progress_cb)(a->progress_arg,
2952 GOT_STATUS_DELETE, path1);
2953 if (err)
2954 goto done;
2955 err = remove_ondisk_file(a->worktree->root_path, path1);
2956 if (err)
2957 goto done;
2958 if (ie)
2959 got_fileindex_entry_mark_deleted_from_disk(ie);
2960 break;
2961 case GOT_STATUS_DELETE:
2962 case GOT_STATUS_MISSING:
2963 err = (*a->progress_cb)(a->progress_arg,
2964 GOT_STATUS_DELETE, path1);
2965 if (err)
2966 goto done;
2967 if (ie)
2968 got_fileindex_entry_mark_deleted_from_disk(ie);
2969 break;
2970 case GOT_STATUS_ADD: {
2971 struct got_object_id *id;
2972 FILE *blob1_f;
2974 * Delete the added file only if its content already
2975 * exists in the repository.
2977 err = got_object_blob_file_create(&id, &blob1_f, path1);
2978 if (err)
2979 goto done;
2980 if (got_object_id_cmp(id, id1) == 0) {
2981 err = (*a->progress_cb)(a->progress_arg,
2982 GOT_STATUS_DELETE, path1);
2983 if (err)
2984 goto done;
2985 err = remove_ondisk_file(a->worktree->root_path,
2986 path1);
2987 if (err)
2988 goto done;
2989 if (ie)
2990 got_fileindex_entry_remove(a->fileindex,
2991 ie);
2992 } else {
2993 err = (*a->progress_cb)(a->progress_arg,
2994 GOT_STATUS_CANNOT_DELETE, path1);
2996 if (fclose(blob1_f) == EOF && err == NULL)
2997 err = got_error_from_errno("fclose");
2998 free(id);
2999 if (err)
3000 goto done;
3001 break;
3003 case GOT_STATUS_MODIFY:
3004 case GOT_STATUS_CONFLICT:
3005 err = (*a->progress_cb)(a->progress_arg,
3006 GOT_STATUS_CANNOT_DELETE, path1);
3007 if (err)
3008 goto done;
3009 break;
3010 case GOT_STATUS_OBSTRUCTED:
3011 err = (*a->progress_cb)(a->progress_arg, status, path1);
3012 if (err)
3013 goto done;
3014 break;
3015 default:
3016 break;
3018 } else if (blob2) {
3019 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3020 path2) == -1)
3021 return got_error_from_errno("asprintf");
3022 ie = got_fileindex_entry_get(a->fileindex, path2,
3023 strlen(path2));
3024 if (ie) {
3025 err = get_file_status(&status, &sb, ie, ondisk_path,
3026 -1, NULL, repo);
3027 if (err)
3028 goto done;
3029 if (status != GOT_STATUS_NO_CHANGE &&
3030 status != GOT_STATUS_MODIFY &&
3031 status != GOT_STATUS_CONFLICT &&
3032 status != GOT_STATUS_ADD) {
3033 err = (*a->progress_cb)(a->progress_arg,
3034 status, path2);
3035 goto done;
3037 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3038 char *link_target2;
3039 err = got_object_blob_read_to_str(&link_target2,
3040 blob2);
3041 if (err)
3042 goto done;
3043 err = merge_symlink(a->worktree, NULL,
3044 ondisk_path, path2, a->label_orig,
3045 link_target2, a->commit_id2, repo,
3046 a->progress_cb, a->progress_arg);
3047 free(link_target2);
3048 } else if (S_ISREG(sb.st_mode)) {
3049 err = merge_blob(&local_changes_subsumed,
3050 a->worktree, NULL, ondisk_path, path2,
3051 sb.st_mode, a->label_orig, blob2,
3052 a->commit_id2, repo, a->progress_cb,
3053 a->progress_arg);
3054 } else {
3055 err = got_error_path(ondisk_path,
3056 GOT_ERR_FILE_OBSTRUCTED);
3058 if (err)
3059 goto done;
3060 if (status == GOT_STATUS_DELETE) {
3061 err = got_fileindex_entry_update(ie,
3062 a->worktree->root_fd, path2, blob2->id.sha1,
3063 a->worktree->base_commit_id->sha1, 0);
3064 if (err)
3065 goto done;
3067 } else {
3068 int is_bad_symlink = 0;
3069 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3070 if (S_ISLNK(mode2)) {
3071 err = install_symlink(&is_bad_symlink,
3072 a->worktree, ondisk_path, path2, blob2, 0,
3073 0, 1, repo, a->progress_cb, a->progress_arg);
3074 } else {
3075 err = install_blob(a->worktree, ondisk_path, path2,
3076 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3077 a->progress_cb, a->progress_arg);
3079 if (err)
3080 goto done;
3081 err = got_fileindex_entry_alloc(&ie, path2);
3082 if (err)
3083 goto done;
3084 err = got_fileindex_entry_update(ie,
3085 a->worktree->root_fd, path2, NULL, NULL, 1);
3086 if (err) {
3087 got_fileindex_entry_free(ie);
3088 goto done;
3090 err = got_fileindex_entry_add(a->fileindex, ie);
3091 if (err) {
3092 got_fileindex_entry_free(ie);
3093 goto done;
3095 if (is_bad_symlink) {
3096 got_fileindex_entry_filetype_set(ie,
3097 GOT_FILEIDX_MODE_BAD_SYMLINK);
3101 done:
3102 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3103 err = got_error_from_errno("fclose");
3104 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3105 err = got_error_from_errno("fclose");
3106 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3107 err = got_error_from_errno("fclose");
3108 free(id_str);
3109 free(label_deriv2);
3110 free(ondisk_path);
3111 return err;
3114 struct check_merge_ok_arg {
3115 struct got_worktree *worktree;
3116 struct got_repository *repo;
3119 static const struct got_error *
3120 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3122 const struct got_error *err = NULL;
3123 struct check_merge_ok_arg *a = arg;
3124 unsigned char status;
3125 struct stat sb;
3126 char *ondisk_path;
3128 /* Reject merges into a work tree with mixed base commits. */
3129 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3130 SHA1_DIGEST_LENGTH))
3131 return got_error(GOT_ERR_MIXED_COMMITS);
3133 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3134 == -1)
3135 return got_error_from_errno("asprintf");
3137 /* Reject merges into a work tree with conflicted files. */
3138 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3139 if (err)
3140 return err;
3141 if (status == GOT_STATUS_CONFLICT)
3142 return got_error(GOT_ERR_CONFLICTS);
3144 return NULL;
3147 static const struct got_error *
3148 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3149 const char *fileindex_path, struct got_object_id *commit_id1,
3150 struct got_object_id *commit_id2, struct got_repository *repo,
3151 got_worktree_checkout_cb progress_cb, void *progress_arg,
3152 got_cancel_cb cancel_cb, void *cancel_arg)
3154 const struct got_error *err = NULL, *sync_err;
3155 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3156 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3157 struct merge_file_cb_arg arg;
3158 char *label_orig = NULL;
3160 if (commit_id1) {
3161 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3162 worktree->path_prefix);
3163 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3164 goto done;
3166 if (tree_id1) {
3167 char *id_str;
3169 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3170 if (err)
3171 goto done;
3173 err = got_object_id_str(&id_str, commit_id1);
3174 if (err)
3175 goto done;
3177 if (asprintf(&label_orig, "%s: commit %s",
3178 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3179 err = got_error_from_errno("asprintf");
3180 free(id_str);
3181 goto done;
3183 free(id_str);
3186 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3187 worktree->path_prefix);
3188 if (err)
3189 goto done;
3191 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3192 if (err)
3193 goto done;
3195 arg.worktree = worktree;
3196 arg.fileindex = fileindex;
3197 arg.progress_cb = progress_cb;
3198 arg.progress_arg = progress_arg;
3199 arg.cancel_cb = cancel_cb;
3200 arg.cancel_arg = cancel_arg;
3201 arg.label_orig = label_orig;
3202 arg.commit_id2 = commit_id2;
3203 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3204 sync_err = sync_fileindex(fileindex, fileindex_path);
3205 if (sync_err && err == NULL)
3206 err = sync_err;
3207 done:
3208 if (tree1)
3209 got_object_tree_close(tree1);
3210 if (tree2)
3211 got_object_tree_close(tree2);
3212 free(label_orig);
3213 return err;
3216 const struct got_error *
3217 got_worktree_merge_files(struct got_worktree *worktree,
3218 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3219 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3220 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3222 const struct got_error *err, *unlockerr;
3223 char *fileindex_path = NULL;
3224 struct got_fileindex *fileindex = NULL;
3225 struct check_merge_ok_arg mok_arg;
3227 err = lock_worktree(worktree, LOCK_EX);
3228 if (err)
3229 return err;
3231 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3232 if (err)
3233 goto done;
3235 mok_arg.worktree = worktree;
3236 mok_arg.repo = repo;
3237 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3238 &mok_arg);
3239 if (err)
3240 goto done;
3242 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3243 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3244 done:
3245 if (fileindex)
3246 got_fileindex_free(fileindex);
3247 free(fileindex_path);
3248 unlockerr = lock_worktree(worktree, LOCK_SH);
3249 if (unlockerr && err == NULL)
3250 err = unlockerr;
3251 return err;
3254 struct diff_dir_cb_arg {
3255 struct got_fileindex *fileindex;
3256 struct got_worktree *worktree;
3257 const char *status_path;
3258 size_t status_path_len;
3259 struct got_repository *repo;
3260 got_worktree_status_cb status_cb;
3261 void *status_arg;
3262 got_cancel_cb cancel_cb;
3263 void *cancel_arg;
3264 /* A pathlist containing per-directory pathlists of ignore patterns. */
3265 struct got_pathlist_head ignores;
3266 int report_unchanged;
3267 int no_ignores;
3270 static const struct got_error *
3271 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3272 int dirfd, const char *de_name,
3273 got_worktree_status_cb status_cb, void *status_arg,
3274 struct got_repository *repo, int report_unchanged)
3276 const struct got_error *err = NULL;
3277 unsigned char status = GOT_STATUS_NO_CHANGE;
3278 unsigned char staged_status = get_staged_status(ie);
3279 struct stat sb;
3280 struct got_object_id blob_id, commit_id, staged_blob_id;
3281 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3282 struct got_object_id *staged_blob_idp = NULL;
3284 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3285 if (err)
3286 return err;
3288 if (status == GOT_STATUS_NO_CHANGE &&
3289 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3290 return NULL;
3292 if (got_fileindex_entry_has_blob(ie)) {
3293 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3294 blob_idp = &blob_id;
3296 if (got_fileindex_entry_has_commit(ie)) {
3297 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3298 commit_idp = &commit_id;
3300 if (staged_status == GOT_STATUS_ADD ||
3301 staged_status == GOT_STATUS_MODIFY) {
3302 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3303 SHA1_DIGEST_LENGTH);
3304 staged_blob_idp = &staged_blob_id;
3307 return (*status_cb)(status_arg, status, staged_status,
3308 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3311 static const struct got_error *
3312 status_old_new(void *arg, struct got_fileindex_entry *ie,
3313 struct dirent *de, const char *parent_path, int dirfd)
3315 const struct got_error *err = NULL;
3316 struct diff_dir_cb_arg *a = arg;
3317 char *abspath;
3319 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3320 return got_error(GOT_ERR_CANCELLED);
3322 if (got_path_cmp(parent_path, a->status_path,
3323 strlen(parent_path), a->status_path_len) != 0 &&
3324 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3325 return NULL;
3327 if (parent_path[0]) {
3328 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3329 parent_path, de->d_name) == -1)
3330 return got_error_from_errno("asprintf");
3331 } else {
3332 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3333 de->d_name) == -1)
3334 return got_error_from_errno("asprintf");
3337 err = report_file_status(ie, abspath, dirfd, de->d_name,
3338 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3339 free(abspath);
3340 return err;
3343 static const struct got_error *
3344 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3346 struct diff_dir_cb_arg *a = arg;
3347 struct got_object_id blob_id, commit_id;
3348 unsigned char status;
3350 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3351 return got_error(GOT_ERR_CANCELLED);
3353 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3354 return NULL;
3356 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3357 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3358 if (got_fileindex_entry_has_file_on_disk(ie))
3359 status = GOT_STATUS_MISSING;
3360 else
3361 status = GOT_STATUS_DELETE;
3362 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3363 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3366 void
3367 free_ignorelist(struct got_pathlist_head *ignorelist)
3369 struct got_pathlist_entry *pe;
3371 TAILQ_FOREACH(pe, ignorelist, entry)
3372 free((char *)pe->path);
3373 got_pathlist_free(ignorelist);
3376 void
3377 free_ignores(struct got_pathlist_head *ignores)
3379 struct got_pathlist_entry *pe;
3381 TAILQ_FOREACH(pe, ignores, entry) {
3382 struct got_pathlist_head *ignorelist = pe->data;
3383 free_ignorelist(ignorelist);
3384 free((char *)pe->path);
3386 got_pathlist_free(ignores);
3389 static const struct got_error *
3390 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3392 const struct got_error *err = NULL;
3393 struct got_pathlist_entry *pe = NULL;
3394 struct got_pathlist_head *ignorelist;
3395 char *line = NULL, *pattern, *dirpath = NULL;
3396 size_t linesize = 0;
3397 ssize_t linelen;
3399 ignorelist = calloc(1, sizeof(*ignorelist));
3400 if (ignorelist == NULL)
3401 return got_error_from_errno("calloc");
3402 TAILQ_INIT(ignorelist);
3404 while ((linelen = getline(&line, &linesize, f)) != -1) {
3405 if (linelen > 0 && line[linelen - 1] == '\n')
3406 line[linelen - 1] = '\0';
3408 /* Git's ignores may contain comments. */
3409 if (line[0] == '#')
3410 continue;
3412 /* Git's negated patterns are not (yet?) supported. */
3413 if (line[0] == '!')
3414 continue;
3416 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3417 line) == -1) {
3418 err = got_error_from_errno("asprintf");
3419 goto done;
3421 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3422 if (err)
3423 goto done;
3425 if (ferror(f)) {
3426 err = got_error_from_errno("getline");
3427 goto done;
3430 dirpath = strdup(path);
3431 if (dirpath == NULL) {
3432 err = got_error_from_errno("strdup");
3433 goto done;
3435 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3436 done:
3437 free(line);
3438 if (err || pe == NULL) {
3439 free(dirpath);
3440 free_ignorelist(ignorelist);
3442 return err;
3445 int
3446 match_ignores(struct got_pathlist_head *ignores, const char *path)
3448 struct got_pathlist_entry *pe;
3450 /* Handle patterns which match in all directories. */
3451 TAILQ_FOREACH(pe, ignores, entry) {
3452 struct got_pathlist_head *ignorelist = pe->data;
3453 struct got_pathlist_entry *pi;
3455 TAILQ_FOREACH(pi, ignorelist, entry) {
3456 const char *p, *pattern = pi->path;
3458 if (strncmp(pattern, "**/", 3) != 0)
3459 continue;
3460 pattern += 3;
3461 p = path;
3462 while (*p) {
3463 if (fnmatch(pattern, p,
3464 FNM_PATHNAME | FNM_LEADING_DIR)) {
3465 /* Retry in next directory. */
3466 while (*p && *p != '/')
3467 p++;
3468 while (*p == '/')
3469 p++;
3470 continue;
3472 return 1;
3478 * The ignores pathlist contains ignore lists from children before
3479 * parents, so we can find the most specific ignorelist by walking
3480 * ignores backwards.
3482 pe = TAILQ_LAST(ignores, got_pathlist_head);
3483 while (pe) {
3484 if (got_path_is_child(path, pe->path, pe->path_len)) {
3485 struct got_pathlist_head *ignorelist = pe->data;
3486 struct got_pathlist_entry *pi;
3487 TAILQ_FOREACH(pi, ignorelist, entry) {
3488 const char *pattern = pi->path;
3489 int flags = FNM_LEADING_DIR;
3490 if (strstr(pattern, "/**/") == NULL)
3491 flags |= FNM_PATHNAME;
3492 if (fnmatch(pattern, path, flags))
3493 continue;
3494 return 1;
3497 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3500 return 0;
3503 static const struct got_error *
3504 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3505 const char *path, int dirfd, const char *ignores_filename)
3507 const struct got_error *err = NULL;
3508 char *ignorespath;
3509 int fd = -1;
3510 FILE *ignoresfile = NULL;
3512 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3513 path[0] ? "/" : "", ignores_filename) == -1)
3514 return got_error_from_errno("asprintf");
3516 if (dirfd != -1) {
3517 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3518 if (fd == -1) {
3519 if (errno != ENOENT && errno != EACCES)
3520 err = got_error_from_errno2("openat",
3521 ignorespath);
3522 } else {
3523 ignoresfile = fdopen(fd, "r");
3524 if (ignoresfile == NULL)
3525 err = got_error_from_errno2("fdopen",
3526 ignorespath);
3527 else {
3528 fd = -1;
3529 err = read_ignores(ignores, path, ignoresfile);
3532 } else {
3533 ignoresfile = fopen(ignorespath, "r");
3534 if (ignoresfile == NULL) {
3535 if (errno != ENOENT && errno != EACCES)
3536 err = got_error_from_errno2("fopen",
3537 ignorespath);
3538 } else
3539 err = read_ignores(ignores, path, ignoresfile);
3542 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3543 err = got_error_from_errno2("fclose", path);
3544 if (fd != -1 && close(fd) == -1 && err == NULL)
3545 err = got_error_from_errno2("close", path);
3546 free(ignorespath);
3547 return err;
3550 static const struct got_error *
3551 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3553 const struct got_error *err = NULL;
3554 struct diff_dir_cb_arg *a = arg;
3555 char *path = NULL;
3557 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3558 return got_error(GOT_ERR_CANCELLED);
3560 if (parent_path[0]) {
3561 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3562 return got_error_from_errno("asprintf");
3563 } else {
3564 path = de->d_name;
3567 if (de->d_type != DT_DIR &&
3568 got_path_is_child(path, a->status_path, a->status_path_len)
3569 && !match_ignores(&a->ignores, path))
3570 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3571 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3572 if (parent_path[0])
3573 free(path);
3574 return err;
3577 static const struct got_error *
3578 status_traverse(void *arg, const char *path, int dirfd)
3580 const struct got_error *err = NULL;
3581 struct diff_dir_cb_arg *a = arg;
3583 if (a->no_ignores)
3584 return NULL;
3586 err = add_ignores(&a->ignores, a->worktree->root_path,
3587 path, dirfd, ".cvsignore");
3588 if (err)
3589 return err;
3591 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3592 dirfd, ".gitignore");
3594 return err;
3597 static const struct got_error *
3598 report_single_file_status(const char *path, const char *ondisk_path,
3599 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3600 void *status_arg, struct got_repository *repo, int report_unchanged)
3602 struct got_fileindex_entry *ie;
3603 struct stat sb;
3605 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3606 if (ie)
3607 return report_file_status(ie, ondisk_path, -1, NULL,
3608 status_cb, status_arg, repo, report_unchanged);
3610 if (lstat(ondisk_path, &sb) == -1) {
3611 if (errno != ENOENT)
3612 return got_error_from_errno2("lstat", ondisk_path);
3613 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3614 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3615 return NULL;
3618 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3619 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3620 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3622 return NULL;
3625 static const struct got_error *
3626 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3627 const char *root_path, const char *path)
3629 const struct got_error *err;
3630 char *parent_path, *next_parent_path = NULL;
3632 err = add_ignores(ignores, root_path, "", -1,
3633 ".cvsignore");
3634 if (err)
3635 return err;
3637 err = add_ignores(ignores, root_path, "", -1,
3638 ".gitignore");
3639 if (err)
3640 return err;
3642 err = got_path_dirname(&parent_path, path);
3643 if (err) {
3644 if (err->code == GOT_ERR_BAD_PATH)
3645 return NULL; /* cannot traverse parent */
3646 return err;
3648 for (;;) {
3649 err = add_ignores(ignores, root_path, parent_path, -1,
3650 ".cvsignore");
3651 if (err)
3652 break;
3653 err = add_ignores(ignores, root_path, parent_path, -1,
3654 ".gitignore");
3655 if (err)
3656 break;
3657 err = got_path_dirname(&next_parent_path, parent_path);
3658 if (err) {
3659 if (err->code == GOT_ERR_BAD_PATH)
3660 err = NULL; /* traversed everything */
3661 break;
3663 free(parent_path);
3664 parent_path = next_parent_path;
3665 next_parent_path = NULL;
3668 free(parent_path);
3669 free(next_parent_path);
3670 return err;
3673 static const struct got_error *
3674 worktree_status(struct got_worktree *worktree, const char *path,
3675 struct got_fileindex *fileindex, struct got_repository *repo,
3676 got_worktree_status_cb status_cb, void *status_arg,
3677 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3678 int report_unchanged)
3680 const struct got_error *err = NULL;
3681 int fd = -1;
3682 struct got_fileindex_diff_dir_cb fdiff_cb;
3683 struct diff_dir_cb_arg arg;
3684 char *ondisk_path = NULL;
3686 TAILQ_INIT(&arg.ignores);
3688 if (asprintf(&ondisk_path, "%s%s%s",
3689 worktree->root_path, path[0] ? "/" : "", path) == -1)
3690 return got_error_from_errno("asprintf");
3692 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3693 if (fd == -1) {
3694 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3695 errno != ELOOP)
3696 err = got_error_from_errno2("open", ondisk_path);
3697 else
3698 err = report_single_file_status(path, ondisk_path,
3699 fileindex, status_cb, status_arg, repo,
3700 report_unchanged);
3701 } else {
3702 fdiff_cb.diff_old_new = status_old_new;
3703 fdiff_cb.diff_old = status_old;
3704 fdiff_cb.diff_new = status_new;
3705 fdiff_cb.diff_traverse = status_traverse;
3706 arg.fileindex = fileindex;
3707 arg.worktree = worktree;
3708 arg.status_path = path;
3709 arg.status_path_len = strlen(path);
3710 arg.repo = repo;
3711 arg.status_cb = status_cb;
3712 arg.status_arg = status_arg;
3713 arg.cancel_cb = cancel_cb;
3714 arg.cancel_arg = cancel_arg;
3715 arg.report_unchanged = report_unchanged;
3716 arg.no_ignores = no_ignores;
3717 if (!no_ignores) {
3718 err = add_ignores_from_parent_paths(&arg.ignores,
3719 worktree->root_path, path);
3720 if (err)
3721 goto done;
3723 err = got_fileindex_diff_dir(fileindex, fd,
3724 worktree->root_path, path, repo, &fdiff_cb, &arg);
3726 done:
3727 free_ignores(&arg.ignores);
3728 if (fd != -1 && close(fd) == -1 && err == NULL)
3729 err = got_error_from_errno("close");
3730 free(ondisk_path);
3731 return err;
3734 const struct got_error *
3735 got_worktree_status(struct got_worktree *worktree,
3736 struct got_pathlist_head *paths, struct got_repository *repo,
3737 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3738 got_cancel_cb cancel_cb, void *cancel_arg)
3740 const struct got_error *err = NULL;
3741 char *fileindex_path = NULL;
3742 struct got_fileindex *fileindex = NULL;
3743 struct got_pathlist_entry *pe;
3745 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3746 if (err)
3747 return err;
3749 TAILQ_FOREACH(pe, paths, entry) {
3750 err = worktree_status(worktree, pe->path, fileindex, repo,
3751 status_cb, status_arg, cancel_cb, cancel_arg,
3752 no_ignores, 0);
3753 if (err)
3754 break;
3756 free(fileindex_path);
3757 got_fileindex_free(fileindex);
3758 return err;
3761 const struct got_error *
3762 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3763 const char *arg)
3765 const struct got_error *err = NULL;
3766 char *resolved = NULL, *cwd = NULL, *path = NULL;
3767 size_t len;
3768 struct stat sb;
3769 char *abspath = NULL;
3770 char canonpath[PATH_MAX];
3772 *wt_path = NULL;
3774 cwd = getcwd(NULL, 0);
3775 if (cwd == NULL)
3776 return got_error_from_errno("getcwd");
3778 if (lstat(arg, &sb) == -1) {
3779 if (errno != ENOENT) {
3780 err = got_error_from_errno2("lstat", arg);
3781 goto done;
3783 sb.st_mode = 0;
3785 if (S_ISLNK(sb.st_mode)) {
3787 * We cannot use realpath(3) with symlinks since we want to
3788 * operate on the symlink itself.
3789 * But we can make the path absolute, assuming it is relative
3790 * to the current working directory, and then canonicalize it.
3792 if (!got_path_is_absolute(arg)) {
3793 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3794 err = got_error_from_errno("asprintf");
3795 goto done;
3799 err = got_canonpath(abspath ? abspath : arg, canonpath,
3800 sizeof(canonpath));
3801 if (err)
3802 goto done;
3803 resolved = strdup(canonpath);
3804 if (resolved == NULL) {
3805 err = got_error_from_errno("strdup");
3806 goto done;
3808 } else {
3809 resolved = realpath(arg, NULL);
3810 if (resolved == NULL) {
3811 if (errno != ENOENT) {
3812 err = got_error_from_errno2("realpath", arg);
3813 goto done;
3815 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3816 err = got_error_from_errno("asprintf");
3817 goto done;
3819 err = got_canonpath(abspath, canonpath,
3820 sizeof(canonpath));
3821 if (err)
3822 goto done;
3823 resolved = strdup(canonpath);
3824 if (resolved == NULL) {
3825 err = got_error_from_errno("strdup");
3826 goto done;
3831 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3832 strlen(got_worktree_get_root_path(worktree)))) {
3833 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3834 goto done;
3837 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3838 err = got_path_skip_common_ancestor(&path,
3839 got_worktree_get_root_path(worktree), resolved);
3840 if (err)
3841 goto done;
3842 } else {
3843 path = strdup("");
3844 if (path == NULL) {
3845 err = got_error_from_errno("strdup");
3846 goto done;
3850 /* XXX status walk can't deal with trailing slash! */
3851 len = strlen(path);
3852 while (len > 0 && path[len - 1] == '/') {
3853 path[len - 1] = '\0';
3854 len--;
3856 done:
3857 free(abspath);
3858 free(resolved);
3859 free(cwd);
3860 if (err == NULL)
3861 *wt_path = path;
3862 else
3863 free(path);
3864 return err;
3867 struct schedule_addition_args {
3868 struct got_worktree *worktree;
3869 struct got_fileindex *fileindex;
3870 got_worktree_checkout_cb progress_cb;
3871 void *progress_arg;
3872 struct got_repository *repo;
3875 static const struct got_error *
3876 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3877 const char *relpath, struct got_object_id *blob_id,
3878 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3879 int dirfd, const char *de_name)
3881 struct schedule_addition_args *a = arg;
3882 const struct got_error *err = NULL;
3883 struct got_fileindex_entry *ie;
3884 struct stat sb;
3885 char *ondisk_path;
3887 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3888 relpath) == -1)
3889 return got_error_from_errno("asprintf");
3891 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3892 if (ie) {
3893 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3894 de_name, a->repo);
3895 if (err)
3896 goto done;
3897 /* Re-adding an existing entry is a no-op. */
3898 if (status == GOT_STATUS_ADD)
3899 goto done;
3900 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3901 if (err)
3902 goto done;
3905 if (status != GOT_STATUS_UNVERSIONED) {
3906 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3907 goto done;
3910 err = got_fileindex_entry_alloc(&ie, relpath);
3911 if (err)
3912 goto done;
3913 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3914 relpath, NULL, NULL, 1);
3915 if (err) {
3916 got_fileindex_entry_free(ie);
3917 goto done;
3919 err = got_fileindex_entry_add(a->fileindex, ie);
3920 if (err) {
3921 got_fileindex_entry_free(ie);
3922 goto done;
3924 done:
3925 free(ondisk_path);
3926 if (err)
3927 return err;
3928 if (status == GOT_STATUS_ADD)
3929 return NULL;
3930 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3933 const struct got_error *
3934 got_worktree_schedule_add(struct got_worktree *worktree,
3935 struct got_pathlist_head *paths,
3936 got_worktree_checkout_cb progress_cb, void *progress_arg,
3937 struct got_repository *repo, int no_ignores)
3939 struct got_fileindex *fileindex = NULL;
3940 char *fileindex_path = NULL;
3941 const struct got_error *err = NULL, *sync_err, *unlockerr;
3942 struct got_pathlist_entry *pe;
3943 struct schedule_addition_args saa;
3945 err = lock_worktree(worktree, LOCK_EX);
3946 if (err)
3947 return err;
3949 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3950 if (err)
3951 goto done;
3953 saa.worktree = worktree;
3954 saa.fileindex = fileindex;
3955 saa.progress_cb = progress_cb;
3956 saa.progress_arg = progress_arg;
3957 saa.repo = repo;
3959 TAILQ_FOREACH(pe, paths, entry) {
3960 err = worktree_status(worktree, pe->path, fileindex, repo,
3961 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3962 if (err)
3963 break;
3965 sync_err = sync_fileindex(fileindex, fileindex_path);
3966 if (sync_err && err == NULL)
3967 err = sync_err;
3968 done:
3969 free(fileindex_path);
3970 if (fileindex)
3971 got_fileindex_free(fileindex);
3972 unlockerr = lock_worktree(worktree, LOCK_SH);
3973 if (unlockerr && err == NULL)
3974 err = unlockerr;
3975 return err;
3978 struct schedule_deletion_args {
3979 struct got_worktree *worktree;
3980 struct got_fileindex *fileindex;
3981 got_worktree_delete_cb progress_cb;
3982 void *progress_arg;
3983 struct got_repository *repo;
3984 int delete_local_mods;
3985 int keep_on_disk;
3986 const char *status_codes;
3989 static const struct got_error *
3990 schedule_for_deletion(void *arg, unsigned char status,
3991 unsigned char staged_status, const char *relpath,
3992 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3993 struct got_object_id *commit_id, int dirfd, const char *de_name)
3995 struct schedule_deletion_args *a = arg;
3996 const struct got_error *err = NULL;
3997 struct got_fileindex_entry *ie = NULL;
3998 struct stat sb;
3999 char *ondisk_path;
4001 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4002 if (ie == NULL)
4003 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4005 staged_status = get_staged_status(ie);
4006 if (staged_status != GOT_STATUS_NO_CHANGE) {
4007 if (staged_status == GOT_STATUS_DELETE)
4008 return NULL;
4009 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4012 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4013 relpath) == -1)
4014 return got_error_from_errno("asprintf");
4016 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4017 a->repo);
4018 if (err)
4019 goto done;
4021 if (a->status_codes) {
4022 size_t ncodes = strlen(a->status_codes);
4023 int i;
4024 for (i = 0; i < ncodes ; i++) {
4025 if (status == a->status_codes[i])
4026 break;
4028 if (i == ncodes) {
4029 /* Do not delete files in non-matching status. */
4030 free(ondisk_path);
4031 return NULL;
4033 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4034 a->status_codes[i] != GOT_STATUS_MISSING) {
4035 static char msg[64];
4036 snprintf(msg, sizeof(msg),
4037 "invalid status code '%c'", a->status_codes[i]);
4038 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4039 goto done;
4043 if (status != GOT_STATUS_NO_CHANGE) {
4044 if (status == GOT_STATUS_DELETE)
4045 goto done;
4046 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4047 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4048 goto done;
4050 if (status != GOT_STATUS_MODIFY &&
4051 status != GOT_STATUS_MISSING) {
4052 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4053 goto done;
4057 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4058 size_t root_len;
4060 if (dirfd != -1) {
4061 if (unlinkat(dirfd, de_name, 0) != 0) {
4062 err = got_error_from_errno2("unlinkat",
4063 ondisk_path);
4064 goto done;
4066 } else if (unlink(ondisk_path) != 0) {
4067 err = got_error_from_errno2("unlink", ondisk_path);
4068 goto done;
4071 root_len = strlen(a->worktree->root_path);
4072 do {
4073 char *parent;
4074 err = got_path_dirname(&parent, ondisk_path);
4075 if (err)
4076 goto done;
4077 free(ondisk_path);
4078 ondisk_path = parent;
4079 if (rmdir(ondisk_path) == -1) {
4080 if (errno != ENOTEMPTY)
4081 err = got_error_from_errno2("rmdir",
4082 ondisk_path);
4083 break;
4085 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4086 strlen(ondisk_path), root_len) != 0);
4089 got_fileindex_entry_mark_deleted_from_disk(ie);
4090 done:
4091 free(ondisk_path);
4092 if (err)
4093 return err;
4094 if (status == GOT_STATUS_DELETE)
4095 return NULL;
4096 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4097 staged_status, relpath);
4100 const struct got_error *
4101 got_worktree_schedule_delete(struct got_worktree *worktree,
4102 struct got_pathlist_head *paths, int delete_local_mods,
4103 const char *status_codes,
4104 got_worktree_delete_cb progress_cb, void *progress_arg,
4105 struct got_repository *repo, int keep_on_disk)
4107 struct got_fileindex *fileindex = NULL;
4108 char *fileindex_path = NULL;
4109 const struct got_error *err = NULL, *sync_err, *unlockerr;
4110 struct got_pathlist_entry *pe;
4111 struct schedule_deletion_args sda;
4113 err = lock_worktree(worktree, LOCK_EX);
4114 if (err)
4115 return err;
4117 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4118 if (err)
4119 goto done;
4121 sda.worktree = worktree;
4122 sda.fileindex = fileindex;
4123 sda.progress_cb = progress_cb;
4124 sda.progress_arg = progress_arg;
4125 sda.repo = repo;
4126 sda.delete_local_mods = delete_local_mods;
4127 sda.keep_on_disk = keep_on_disk;
4128 sda.status_codes = status_codes;
4130 TAILQ_FOREACH(pe, paths, entry) {
4131 err = worktree_status(worktree, pe->path, fileindex, repo,
4132 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4133 if (err)
4134 break;
4136 sync_err = sync_fileindex(fileindex, fileindex_path);
4137 if (sync_err && err == NULL)
4138 err = sync_err;
4139 done:
4140 free(fileindex_path);
4141 if (fileindex)
4142 got_fileindex_free(fileindex);
4143 unlockerr = lock_worktree(worktree, LOCK_SH);
4144 if (unlockerr && err == NULL)
4145 err = unlockerr;
4146 return err;
4149 static const struct got_error *
4150 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4152 const struct got_error *err = NULL;
4153 char *line = NULL;
4154 size_t linesize = 0, n;
4155 ssize_t linelen;
4157 linelen = getline(&line, &linesize, infile);
4158 if (linelen == -1) {
4159 if (ferror(infile)) {
4160 err = got_error_from_errno("getline");
4161 goto done;
4163 return NULL;
4165 if (outfile) {
4166 n = fwrite(line, 1, linelen, outfile);
4167 if (n != linelen) {
4168 err = got_ferror(outfile, GOT_ERR_IO);
4169 goto done;
4172 if (rejectfile) {
4173 n = fwrite(line, 1, linelen, rejectfile);
4174 if (n != linelen)
4175 err = got_ferror(outfile, GOT_ERR_IO);
4177 done:
4178 free(line);
4179 return err;
4182 static const struct got_error *
4183 skip_one_line(FILE *f)
4185 char *line = NULL;
4186 size_t linesize = 0;
4187 ssize_t linelen;
4189 linelen = getline(&line, &linesize, f);
4190 if (linelen == -1) {
4191 if (ferror(f))
4192 return got_error_from_errno("getline");
4193 return NULL;
4195 free(line);
4196 return NULL;
4199 static const struct got_error *
4200 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4201 int start_old, int end_old, int start_new, int end_new,
4202 FILE *outfile, FILE *rejectfile)
4204 const struct got_error *err;
4206 /* Copy old file's lines leading up to patch. */
4207 while (!feof(f1) && *line_cur1 < start_old) {
4208 err = copy_one_line(f1, outfile, NULL);
4209 if (err)
4210 return err;
4211 (*line_cur1)++;
4213 /* Skip new file's lines leading up to patch. */
4214 while (!feof(f2) && *line_cur2 < start_new) {
4215 if (rejectfile)
4216 err = copy_one_line(f2, NULL, rejectfile);
4217 else
4218 err = skip_one_line(f2);
4219 if (err)
4220 return err;
4221 (*line_cur2)++;
4223 /* Copy patched lines. */
4224 while (!feof(f2) && *line_cur2 <= end_new) {
4225 err = copy_one_line(f2, outfile, NULL);
4226 if (err)
4227 return err;
4228 (*line_cur2)++;
4230 /* Skip over old file's replaced lines. */
4231 while (!feof(f1) && *line_cur1 <= end_old) {
4232 if (rejectfile)
4233 err = copy_one_line(f1, NULL, rejectfile);
4234 else
4235 err = skip_one_line(f1);
4236 if (err)
4237 return err;
4238 (*line_cur1)++;
4241 return NULL;
4244 static const struct got_error *
4245 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4246 FILE *outfile, FILE *rejectfile)
4248 const struct got_error *err;
4250 if (outfile) {
4251 /* Copy old file's lines until EOF. */
4252 while (!feof(f1)) {
4253 err = copy_one_line(f1, outfile, NULL);
4254 if (err)
4255 return err;
4256 (*line_cur1)++;
4259 if (rejectfile) {
4260 /* Copy new file's lines until EOF. */
4261 while (!feof(f2)) {
4262 err = copy_one_line(f2, NULL, rejectfile);
4263 if (err)
4264 return err;
4265 (*line_cur2)++;
4269 return NULL;
4272 static const struct got_error *
4273 apply_or_reject_change(int *choice, int *nchunks_used,
4274 struct diff_result *diff_result, int n,
4275 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4276 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4277 got_worktree_patch_cb patch_cb, void *patch_arg)
4279 const struct got_error *err = NULL;
4280 struct diff_chunk_context cc = {};
4281 int start_old, end_old, start_new, end_new;
4282 FILE *hunkfile;
4283 struct diff_output_unidiff_state *diff_state;
4284 struct diff_input_info diff_info;
4285 int rc;
4287 *choice = GOT_PATCH_CHOICE_NONE;
4289 /* Get changed line numbers without context lines for copy_change(). */
4290 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4291 start_old = cc.left.start;
4292 end_old = cc.left.end;
4293 start_new = cc.right.start;
4294 end_new = cc.right.end;
4296 /* Get the same change with context lines for display. */
4297 memset(&cc, 0, sizeof(cc));
4298 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4300 memset(&diff_info, 0, sizeof(diff_info));
4301 diff_info.left_path = relpath;
4302 diff_info.right_path = relpath;
4304 diff_state = diff_output_unidiff_state_alloc();
4305 if (diff_state == NULL)
4306 return got_error_set_errno(ENOMEM,
4307 "diff_output_unidiff_state_alloc");
4309 hunkfile = got_opentemp();
4310 if (hunkfile == NULL) {
4311 err = got_error_from_errno("got_opentemp");
4312 goto done;
4315 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4316 diff_result, &cc);
4317 if (rc != DIFF_RC_OK) {
4318 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4319 goto done;
4322 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4323 err = got_ferror(hunkfile, GOT_ERR_IO);
4324 goto done;
4327 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4328 hunkfile, changeno, nchanges);
4329 if (err)
4330 goto done;
4332 switch (*choice) {
4333 case GOT_PATCH_CHOICE_YES:
4334 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4335 end_old, start_new, end_new, outfile, rejectfile);
4336 break;
4337 case GOT_PATCH_CHOICE_NO:
4338 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4339 end_old, start_new, end_new, rejectfile, outfile);
4340 break;
4341 case GOT_PATCH_CHOICE_QUIT:
4342 break;
4343 default:
4344 err = got_error(GOT_ERR_PATCH_CHOICE);
4345 break;
4347 done:
4348 diff_output_unidiff_state_free(diff_state);
4349 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4350 err = got_error_from_errno("fclose");
4351 return err;
4354 struct revert_file_args {
4355 struct got_worktree *worktree;
4356 struct got_fileindex *fileindex;
4357 got_worktree_checkout_cb progress_cb;
4358 void *progress_arg;
4359 got_worktree_patch_cb patch_cb;
4360 void *patch_arg;
4361 struct got_repository *repo;
4364 static const struct got_error *
4365 create_patched_content(char **path_outfile, int reverse_patch,
4366 struct got_object_id *blob_id, const char *path2,
4367 int dirfd2, const char *de_name2,
4368 const char *relpath, struct got_repository *repo,
4369 got_worktree_patch_cb patch_cb, void *patch_arg)
4371 const struct got_error *err, *free_err;
4372 struct got_blob_object *blob = NULL;
4373 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4374 int fd2 = -1;
4375 char link_target[PATH_MAX];
4376 ssize_t link_len = 0;
4377 char *path1 = NULL, *id_str = NULL;
4378 struct stat sb2;
4379 struct got_diffreg_result *diffreg_result = NULL;
4380 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4381 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4383 *path_outfile = NULL;
4385 err = got_object_id_str(&id_str, blob_id);
4386 if (err)
4387 return err;
4389 if (dirfd2 != -1) {
4390 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4391 if (fd2 == -1) {
4392 if (errno != ELOOP) {
4393 err = got_error_from_errno2("openat", path2);
4394 goto done;
4396 link_len = readlinkat(dirfd2, de_name2,
4397 link_target, sizeof(link_target));
4398 if (link_len == -1)
4399 return got_error_from_errno2("readlinkat", path2);
4400 sb2.st_mode = S_IFLNK;
4401 sb2.st_size = link_len;
4403 } else {
4404 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4405 if (fd2 == -1) {
4406 if (errno != ELOOP) {
4407 err = got_error_from_errno2("open", path2);
4408 goto done;
4410 link_len = readlink(path2, link_target,
4411 sizeof(link_target));
4412 if (link_len == -1)
4413 return got_error_from_errno2("readlink", path2);
4414 sb2.st_mode = S_IFLNK;
4415 sb2.st_size = link_len;
4418 if (fd2 != -1) {
4419 if (fstat(fd2, &sb2) == -1) {
4420 err = got_error_from_errno2("fstat", path2);
4421 goto done;
4424 f2 = fdopen(fd2, "r");
4425 if (f2 == NULL) {
4426 err = got_error_from_errno2("fdopen", path2);
4427 goto done;
4429 fd2 = -1;
4430 } else {
4431 size_t n;
4432 f2 = got_opentemp();
4433 if (f2 == NULL) {
4434 err = got_error_from_errno2("got_opentemp", path2);
4435 goto done;
4437 n = fwrite(link_target, 1, link_len, f2);
4438 if (n != link_len) {
4439 err = got_ferror(f2, GOT_ERR_IO);
4440 goto done;
4442 if (fflush(f2) == EOF) {
4443 err = got_error_from_errno("fflush");
4444 goto done;
4446 rewind(f2);
4449 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4450 if (err)
4451 goto done;
4453 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4454 if (err)
4455 goto done;
4457 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4458 if (err)
4459 goto done;
4461 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4462 NULL);
4463 if (err)
4464 goto done;
4466 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4467 if (err)
4468 goto done;
4470 if (fseek(f1, 0L, SEEK_SET) == -1)
4471 return got_ferror(f1, GOT_ERR_IO);
4472 if (fseek(f2, 0L, SEEK_SET) == -1)
4473 return got_ferror(f2, GOT_ERR_IO);
4475 /* Count the number of actual changes in the diff result. */
4476 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4477 struct diff_chunk_context cc = {};
4478 diff_chunk_context_load_change(&cc, &nchunks_used,
4479 diffreg_result->result, n, 0);
4480 nchanges++;
4482 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4483 int choice;
4484 err = apply_or_reject_change(&choice, &nchunks_used,
4485 diffreg_result->result, n, relpath, f1, f2,
4486 &line_cur1, &line_cur2,
4487 reverse_patch ? NULL : outfile,
4488 reverse_patch ? outfile : NULL,
4489 ++i, nchanges, patch_cb, patch_arg);
4490 if (err)
4491 goto done;
4492 if (choice == GOT_PATCH_CHOICE_YES)
4493 have_content = 1;
4494 else if (choice == GOT_PATCH_CHOICE_QUIT)
4495 break;
4497 if (have_content) {
4498 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4499 reverse_patch ? NULL : outfile,
4500 reverse_patch ? outfile : NULL);
4501 if (err)
4502 goto done;
4504 if (!S_ISLNK(sb2.st_mode)) {
4505 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4506 err = got_error_from_errno2("fchmod", path2);
4507 goto done;
4511 done:
4512 free(id_str);
4513 if (blob)
4514 got_object_blob_close(blob);
4515 free_err = got_diffreg_result_free(diffreg_result);
4516 if (err == NULL)
4517 err = free_err;
4518 if (f1 && fclose(f1) == EOF && err == NULL)
4519 err = got_error_from_errno2("fclose", path1);
4520 if (f2 && fclose(f2) == EOF && err == NULL)
4521 err = got_error_from_errno2("fclose", path2);
4522 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4523 err = got_error_from_errno2("close", path2);
4524 if (outfile && fclose(outfile) == EOF && err == NULL)
4525 err = got_error_from_errno2("fclose", *path_outfile);
4526 if (path1 && unlink(path1) == -1 && err == NULL)
4527 err = got_error_from_errno2("unlink", path1);
4528 if (err || !have_content) {
4529 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4530 err = got_error_from_errno2("unlink", *path_outfile);
4531 free(*path_outfile);
4532 *path_outfile = NULL;
4534 free(path1);
4535 return err;
4538 static const struct got_error *
4539 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4540 const char *relpath, struct got_object_id *blob_id,
4541 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4542 int dirfd, const char *de_name)
4544 struct revert_file_args *a = arg;
4545 const struct got_error *err = NULL;
4546 char *parent_path = NULL;
4547 struct got_fileindex_entry *ie;
4548 struct got_tree_object *tree = NULL;
4549 struct got_object_id *tree_id = NULL;
4550 const struct got_tree_entry *te = NULL;
4551 char *tree_path = NULL, *te_name;
4552 char *ondisk_path = NULL, *path_content = NULL;
4553 struct got_blob_object *blob = NULL;
4555 /* Reverting a staged deletion is a no-op. */
4556 if (status == GOT_STATUS_DELETE &&
4557 staged_status != GOT_STATUS_NO_CHANGE)
4558 return NULL;
4560 if (status == GOT_STATUS_UNVERSIONED)
4561 return (*a->progress_cb)(a->progress_arg,
4562 GOT_STATUS_UNVERSIONED, relpath);
4564 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4565 if (ie == NULL)
4566 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4568 /* Construct in-repository path of tree which contains this blob. */
4569 err = got_path_dirname(&parent_path, ie->path);
4570 if (err) {
4571 if (err->code != GOT_ERR_BAD_PATH)
4572 goto done;
4573 parent_path = strdup("/");
4574 if (parent_path == NULL) {
4575 err = got_error_from_errno("strdup");
4576 goto done;
4579 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4580 tree_path = strdup(parent_path);
4581 if (tree_path == NULL) {
4582 err = got_error_from_errno("strdup");
4583 goto done;
4585 } else {
4586 if (got_path_is_root_dir(parent_path)) {
4587 tree_path = strdup(a->worktree->path_prefix);
4588 if (tree_path == NULL) {
4589 err = got_error_from_errno("strdup");
4590 goto done;
4592 } else {
4593 if (asprintf(&tree_path, "%s/%s",
4594 a->worktree->path_prefix, parent_path) == -1) {
4595 err = got_error_from_errno("asprintf");
4596 goto done;
4601 err = got_object_id_by_path(&tree_id, a->repo,
4602 a->worktree->base_commit_id, tree_path);
4603 if (err) {
4604 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4605 (status == GOT_STATUS_ADD ||
4606 staged_status == GOT_STATUS_ADD)))
4607 goto done;
4608 } else {
4609 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4610 if (err)
4611 goto done;
4613 err = got_path_basename(&te_name, ie->path);
4614 if (err)
4615 goto done;
4617 te = got_object_tree_find_entry(tree, te_name);
4618 free(te_name);
4619 if (te == NULL && status != GOT_STATUS_ADD &&
4620 staged_status != GOT_STATUS_ADD) {
4621 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4622 goto done;
4626 switch (status) {
4627 case GOT_STATUS_ADD:
4628 if (a->patch_cb) {
4629 int choice = GOT_PATCH_CHOICE_NONE;
4630 err = (*a->patch_cb)(&choice, a->patch_arg,
4631 status, ie->path, NULL, 1, 1);
4632 if (err)
4633 goto done;
4634 if (choice != GOT_PATCH_CHOICE_YES)
4635 break;
4637 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4638 ie->path);
4639 if (err)
4640 goto done;
4641 got_fileindex_entry_remove(a->fileindex, ie);
4642 break;
4643 case GOT_STATUS_DELETE:
4644 if (a->patch_cb) {
4645 int choice = GOT_PATCH_CHOICE_NONE;
4646 err = (*a->patch_cb)(&choice, a->patch_arg,
4647 status, ie->path, NULL, 1, 1);
4648 if (err)
4649 goto done;
4650 if (choice != GOT_PATCH_CHOICE_YES)
4651 break;
4653 /* fall through */
4654 case GOT_STATUS_MODIFY:
4655 case GOT_STATUS_MODE_CHANGE:
4656 case GOT_STATUS_CONFLICT:
4657 case GOT_STATUS_MISSING: {
4658 struct got_object_id id;
4659 if (staged_status == GOT_STATUS_ADD ||
4660 staged_status == GOT_STATUS_MODIFY) {
4661 memcpy(id.sha1, ie->staged_blob_sha1,
4662 SHA1_DIGEST_LENGTH);
4663 } else
4664 memcpy(id.sha1, ie->blob_sha1,
4665 SHA1_DIGEST_LENGTH);
4666 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4667 if (err)
4668 goto done;
4670 if (asprintf(&ondisk_path, "%s/%s",
4671 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4672 err = got_error_from_errno("asprintf");
4673 goto done;
4676 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4677 status == GOT_STATUS_CONFLICT)) {
4678 int is_bad_symlink = 0;
4679 err = create_patched_content(&path_content, 1, &id,
4680 ondisk_path, dirfd, de_name, ie->path, a->repo,
4681 a->patch_cb, a->patch_arg);
4682 if (err || path_content == NULL)
4683 break;
4684 if (te && S_ISLNK(te->mode)) {
4685 if (unlink(path_content) == -1) {
4686 err = got_error_from_errno2("unlink",
4687 path_content);
4688 break;
4690 err = install_symlink(&is_bad_symlink,
4691 a->worktree, ondisk_path, ie->path,
4692 blob, 0, 1, 0, a->repo,
4693 a->progress_cb, a->progress_arg);
4694 } else {
4695 if (rename(path_content, ondisk_path) == -1) {
4696 err = got_error_from_errno3("rename",
4697 path_content, ondisk_path);
4698 goto done;
4701 } else {
4702 int is_bad_symlink = 0;
4703 if (te && S_ISLNK(te->mode)) {
4704 err = install_symlink(&is_bad_symlink,
4705 a->worktree, ondisk_path, ie->path,
4706 blob, 0, 1, 0, a->repo,
4707 a->progress_cb, a->progress_arg);
4708 } else {
4709 err = install_blob(a->worktree, ondisk_path,
4710 ie->path,
4711 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4712 got_fileindex_perms_to_st(ie), blob,
4713 0, 1, 0, 0, a->repo,
4714 a->progress_cb, a->progress_arg);
4716 if (err)
4717 goto done;
4718 if (status == GOT_STATUS_DELETE ||
4719 status == GOT_STATUS_MODE_CHANGE) {
4720 err = got_fileindex_entry_update(ie,
4721 a->worktree->root_fd, relpath,
4722 blob->id.sha1,
4723 a->worktree->base_commit_id->sha1, 1);
4724 if (err)
4725 goto done;
4727 if (is_bad_symlink) {
4728 got_fileindex_entry_filetype_set(ie,
4729 GOT_FILEIDX_MODE_BAD_SYMLINK);
4732 break;
4734 default:
4735 break;
4737 done:
4738 free(ondisk_path);
4739 free(path_content);
4740 free(parent_path);
4741 free(tree_path);
4742 if (blob)
4743 got_object_blob_close(blob);
4744 if (tree)
4745 got_object_tree_close(tree);
4746 free(tree_id);
4747 return err;
4750 const struct got_error *
4751 got_worktree_revert(struct got_worktree *worktree,
4752 struct got_pathlist_head *paths,
4753 got_worktree_checkout_cb progress_cb, void *progress_arg,
4754 got_worktree_patch_cb patch_cb, void *patch_arg,
4755 struct got_repository *repo)
4757 struct got_fileindex *fileindex = NULL;
4758 char *fileindex_path = NULL;
4759 const struct got_error *err = NULL, *unlockerr = NULL;
4760 const struct got_error *sync_err = NULL;
4761 struct got_pathlist_entry *pe;
4762 struct revert_file_args rfa;
4764 err = lock_worktree(worktree, LOCK_EX);
4765 if (err)
4766 return err;
4768 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4769 if (err)
4770 goto done;
4772 rfa.worktree = worktree;
4773 rfa.fileindex = fileindex;
4774 rfa.progress_cb = progress_cb;
4775 rfa.progress_arg = progress_arg;
4776 rfa.patch_cb = patch_cb;
4777 rfa.patch_arg = patch_arg;
4778 rfa.repo = repo;
4779 TAILQ_FOREACH(pe, paths, entry) {
4780 err = worktree_status(worktree, pe->path, fileindex, repo,
4781 revert_file, &rfa, NULL, NULL, 0, 0);
4782 if (err)
4783 break;
4785 sync_err = sync_fileindex(fileindex, fileindex_path);
4786 if (sync_err && err == NULL)
4787 err = sync_err;
4788 done:
4789 free(fileindex_path);
4790 if (fileindex)
4791 got_fileindex_free(fileindex);
4792 unlockerr = lock_worktree(worktree, LOCK_SH);
4793 if (unlockerr && err == NULL)
4794 err = unlockerr;
4795 return err;
4798 static void
4799 free_commitable(struct got_commitable *ct)
4801 free(ct->path);
4802 free(ct->in_repo_path);
4803 free(ct->ondisk_path);
4804 free(ct->blob_id);
4805 free(ct->base_blob_id);
4806 free(ct->staged_blob_id);
4807 free(ct->base_commit_id);
4808 free(ct);
4811 struct collect_commitables_arg {
4812 struct got_pathlist_head *commitable_paths;
4813 struct got_repository *repo;
4814 struct got_worktree *worktree;
4815 struct got_fileindex *fileindex;
4816 int have_staged_files;
4817 int allow_bad_symlinks;
4820 static const struct got_error *
4821 collect_commitables(void *arg, unsigned char status,
4822 unsigned char staged_status, const char *relpath,
4823 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4824 struct got_object_id *commit_id, int dirfd, const char *de_name)
4826 struct collect_commitables_arg *a = arg;
4827 const struct got_error *err = NULL;
4828 struct got_commitable *ct = NULL;
4829 struct got_pathlist_entry *new = NULL;
4830 char *parent_path = NULL, *path = NULL;
4831 struct stat sb;
4833 if (a->have_staged_files) {
4834 if (staged_status != GOT_STATUS_MODIFY &&
4835 staged_status != GOT_STATUS_ADD &&
4836 staged_status != GOT_STATUS_DELETE)
4837 return NULL;
4838 } else {
4839 if (status == GOT_STATUS_CONFLICT)
4840 return got_error(GOT_ERR_COMMIT_CONFLICT);
4842 if (status != GOT_STATUS_MODIFY &&
4843 status != GOT_STATUS_MODE_CHANGE &&
4844 status != GOT_STATUS_ADD &&
4845 status != GOT_STATUS_DELETE)
4846 return NULL;
4849 if (asprintf(&path, "/%s", relpath) == -1) {
4850 err = got_error_from_errno("asprintf");
4851 goto done;
4853 if (strcmp(path, "/") == 0) {
4854 parent_path = strdup("");
4855 if (parent_path == NULL)
4856 return got_error_from_errno("strdup");
4857 } else {
4858 err = got_path_dirname(&parent_path, path);
4859 if (err)
4860 return err;
4863 ct = calloc(1, sizeof(*ct));
4864 if (ct == NULL) {
4865 err = got_error_from_errno("calloc");
4866 goto done;
4869 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4870 relpath) == -1) {
4871 err = got_error_from_errno("asprintf");
4872 goto done;
4875 if (staged_status == GOT_STATUS_ADD ||
4876 staged_status == GOT_STATUS_MODIFY) {
4877 struct got_fileindex_entry *ie;
4878 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4879 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4880 case GOT_FILEIDX_MODE_REGULAR_FILE:
4881 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4882 ct->mode = S_IFREG;
4883 break;
4884 case GOT_FILEIDX_MODE_SYMLINK:
4885 ct->mode = S_IFLNK;
4886 break;
4887 default:
4888 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4889 goto done;
4891 ct->mode |= got_fileindex_entry_perms_get(ie);
4892 } else if (status != GOT_STATUS_DELETE &&
4893 staged_status != GOT_STATUS_DELETE) {
4894 if (dirfd != -1) {
4895 if (fstatat(dirfd, de_name, &sb,
4896 AT_SYMLINK_NOFOLLOW) == -1) {
4897 err = got_error_from_errno2("fstatat",
4898 ct->ondisk_path);
4899 goto done;
4901 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4902 err = got_error_from_errno2("lstat", ct->ondisk_path);
4903 goto done;
4905 ct->mode = sb.st_mode;
4908 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4909 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4910 relpath) == -1) {
4911 err = got_error_from_errno("asprintf");
4912 goto done;
4915 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4916 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4917 int is_bad_symlink;
4918 char target_path[PATH_MAX];
4919 ssize_t target_len;
4920 target_len = readlink(ct->ondisk_path, target_path,
4921 sizeof(target_path));
4922 if (target_len == -1) {
4923 err = got_error_from_errno2("readlink",
4924 ct->ondisk_path);
4925 goto done;
4927 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4928 target_len, ct->ondisk_path, a->worktree->root_path);
4929 if (err)
4930 goto done;
4931 if (is_bad_symlink) {
4932 err = got_error_path(ct->ondisk_path,
4933 GOT_ERR_BAD_SYMLINK);
4934 goto done;
4939 ct->status = status;
4940 ct->staged_status = staged_status;
4941 ct->blob_id = NULL; /* will be filled in when blob gets created */
4942 if (ct->status != GOT_STATUS_ADD &&
4943 ct->staged_status != GOT_STATUS_ADD) {
4944 ct->base_blob_id = got_object_id_dup(blob_id);
4945 if (ct->base_blob_id == NULL) {
4946 err = got_error_from_errno("got_object_id_dup");
4947 goto done;
4949 ct->base_commit_id = got_object_id_dup(commit_id);
4950 if (ct->base_commit_id == NULL) {
4951 err = got_error_from_errno("got_object_id_dup");
4952 goto done;
4955 if (ct->staged_status == GOT_STATUS_ADD ||
4956 ct->staged_status == GOT_STATUS_MODIFY) {
4957 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4958 if (ct->staged_blob_id == NULL) {
4959 err = got_error_from_errno("got_object_id_dup");
4960 goto done;
4963 ct->path = strdup(path);
4964 if (ct->path == NULL) {
4965 err = got_error_from_errno("strdup");
4966 goto done;
4968 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4969 done:
4970 if (ct && (err || new == NULL))
4971 free_commitable(ct);
4972 free(parent_path);
4973 free(path);
4974 return err;
4977 static const struct got_error *write_tree(struct got_object_id **, int *,
4978 struct got_tree_object *, const char *, struct got_pathlist_head *,
4979 got_worktree_status_cb status_cb, void *status_arg,
4980 struct got_repository *);
4982 static const struct got_error *
4983 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4984 struct got_tree_entry *te, const char *parent_path,
4985 struct got_pathlist_head *commitable_paths,
4986 got_worktree_status_cb status_cb, void *status_arg,
4987 struct got_repository *repo)
4989 const struct got_error *err = NULL;
4990 struct got_tree_object *subtree;
4991 char *subpath;
4993 if (asprintf(&subpath, "%s%s%s", parent_path,
4994 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4995 return got_error_from_errno("asprintf");
4997 err = got_object_open_as_tree(&subtree, repo, &te->id);
4998 if (err)
4999 return err;
5001 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5002 commitable_paths, status_cb, status_arg, repo);
5003 got_object_tree_close(subtree);
5004 free(subpath);
5005 return err;
5008 static const struct got_error *
5009 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5011 const struct got_error *err = NULL;
5012 char *ct_parent_path = NULL;
5014 *match = 0;
5016 if (strchr(ct->in_repo_path, '/') == NULL) {
5017 *match = got_path_is_root_dir(path);
5018 return NULL;
5021 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5022 if (err)
5023 return err;
5024 *match = (strcmp(path, ct_parent_path) == 0);
5025 free(ct_parent_path);
5026 return err;
5029 static mode_t
5030 get_ct_file_mode(struct got_commitable *ct)
5032 if (S_ISLNK(ct->mode))
5033 return S_IFLNK;
5035 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5038 static const struct got_error *
5039 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5040 struct got_tree_entry *te, struct got_commitable *ct)
5042 const struct got_error *err = NULL;
5044 *new_te = NULL;
5046 err = got_object_tree_entry_dup(new_te, te);
5047 if (err)
5048 goto done;
5050 (*new_te)->mode = get_ct_file_mode(ct);
5052 if (ct->staged_status == GOT_STATUS_MODIFY)
5053 memcpy(&(*new_te)->id, ct->staged_blob_id,
5054 sizeof((*new_te)->id));
5055 else
5056 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5057 done:
5058 if (err && *new_te) {
5059 free(*new_te);
5060 *new_te = NULL;
5062 return err;
5065 static const struct got_error *
5066 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5067 struct got_commitable *ct)
5069 const struct got_error *err = NULL;
5070 char *ct_name = NULL;
5072 *new_te = NULL;
5074 *new_te = calloc(1, sizeof(**new_te));
5075 if (*new_te == NULL)
5076 return got_error_from_errno("calloc");
5078 err = got_path_basename(&ct_name, ct->path);
5079 if (err)
5080 goto done;
5081 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5082 sizeof((*new_te)->name)) {
5083 err = got_error(GOT_ERR_NO_SPACE);
5084 goto done;
5087 (*new_te)->mode = get_ct_file_mode(ct);
5089 if (ct->staged_status == GOT_STATUS_ADD)
5090 memcpy(&(*new_te)->id, ct->staged_blob_id,
5091 sizeof((*new_te)->id));
5092 else
5093 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5094 done:
5095 free(ct_name);
5096 if (err && *new_te) {
5097 free(*new_te);
5098 *new_te = NULL;
5100 return err;
5103 static const struct got_error *
5104 insert_tree_entry(struct got_tree_entry *new_te,
5105 struct got_pathlist_head *paths)
5107 const struct got_error *err = NULL;
5108 struct got_pathlist_entry *new_pe;
5110 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5111 if (err)
5112 return err;
5113 if (new_pe == NULL)
5114 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5115 return NULL;
5118 static const struct got_error *
5119 report_ct_status(struct got_commitable *ct,
5120 got_worktree_status_cb status_cb, void *status_arg)
5122 const char *ct_path = ct->path;
5123 unsigned char status;
5125 while (ct_path[0] == '/')
5126 ct_path++;
5128 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5129 status = ct->staged_status;
5130 else
5131 status = ct->status;
5133 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5134 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5137 static const struct got_error *
5138 match_modified_subtree(int *modified, struct got_tree_entry *te,
5139 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5141 const struct got_error *err = NULL;
5142 struct got_pathlist_entry *pe;
5143 char *te_path;
5145 *modified = 0;
5147 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5148 got_path_is_root_dir(base_tree_path) ? "" : "/",
5149 te->name) == -1)
5150 return got_error_from_errno("asprintf");
5152 TAILQ_FOREACH(pe, commitable_paths, entry) {
5153 struct got_commitable *ct = pe->data;
5154 *modified = got_path_is_child(ct->in_repo_path, te_path,
5155 strlen(te_path));
5156 if (*modified)
5157 break;
5160 free(te_path);
5161 return err;
5164 static const struct got_error *
5165 match_deleted_or_modified_ct(struct got_commitable **ctp,
5166 struct got_tree_entry *te, const char *base_tree_path,
5167 struct got_pathlist_head *commitable_paths)
5169 const struct got_error *err = NULL;
5170 struct got_pathlist_entry *pe;
5172 *ctp = NULL;
5174 TAILQ_FOREACH(pe, commitable_paths, entry) {
5175 struct got_commitable *ct = pe->data;
5176 char *ct_name = NULL;
5177 int path_matches;
5179 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5180 if (ct->status != GOT_STATUS_MODIFY &&
5181 ct->status != GOT_STATUS_MODE_CHANGE &&
5182 ct->status != GOT_STATUS_DELETE)
5183 continue;
5184 } else {
5185 if (ct->staged_status != GOT_STATUS_MODIFY &&
5186 ct->staged_status != GOT_STATUS_DELETE)
5187 continue;
5190 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5191 continue;
5193 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5194 if (err)
5195 return err;
5196 if (!path_matches)
5197 continue;
5199 err = got_path_basename(&ct_name, pe->path);
5200 if (err)
5201 return err;
5203 if (strcmp(te->name, ct_name) != 0) {
5204 free(ct_name);
5205 continue;
5207 free(ct_name);
5209 *ctp = ct;
5210 break;
5213 return err;
5216 static const struct got_error *
5217 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5218 const char *child_path, const char *path_base_tree,
5219 struct got_pathlist_head *commitable_paths,
5220 got_worktree_status_cb status_cb, void *status_arg,
5221 struct got_repository *repo)
5223 const struct got_error *err = NULL;
5224 struct got_tree_entry *new_te;
5225 char *subtree_path;
5226 struct got_object_id *id = NULL;
5227 int nentries;
5229 *new_tep = NULL;
5231 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5232 got_path_is_root_dir(path_base_tree) ? "" : "/",
5233 child_path) == -1)
5234 return got_error_from_errno("asprintf");
5236 new_te = calloc(1, sizeof(*new_te));
5237 if (new_te == NULL)
5238 return got_error_from_errno("calloc");
5239 new_te->mode = S_IFDIR;
5241 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5242 sizeof(new_te->name)) {
5243 err = got_error(GOT_ERR_NO_SPACE);
5244 goto done;
5246 err = write_tree(&id, &nentries, NULL, subtree_path,
5247 commitable_paths, status_cb, status_arg, repo);
5248 if (err) {
5249 free(new_te);
5250 goto done;
5252 memcpy(&new_te->id, id, sizeof(new_te->id));
5253 done:
5254 free(id);
5255 free(subtree_path);
5256 if (err == NULL)
5257 *new_tep = new_te;
5258 return err;
5261 static const struct got_error *
5262 write_tree(struct got_object_id **new_tree_id, int *nentries,
5263 struct got_tree_object *base_tree, const char *path_base_tree,
5264 struct got_pathlist_head *commitable_paths,
5265 got_worktree_status_cb status_cb, void *status_arg,
5266 struct got_repository *repo)
5268 const struct got_error *err = NULL;
5269 struct got_pathlist_head paths;
5270 struct got_tree_entry *te, *new_te = NULL;
5271 struct got_pathlist_entry *pe;
5273 TAILQ_INIT(&paths);
5274 *nentries = 0;
5276 /* Insert, and recurse into, newly added entries first. */
5277 TAILQ_FOREACH(pe, commitable_paths, entry) {
5278 struct got_commitable *ct = pe->data;
5279 char *child_path = NULL, *slash;
5281 if ((ct->status != GOT_STATUS_ADD &&
5282 ct->staged_status != GOT_STATUS_ADD) ||
5283 (ct->flags & GOT_COMMITABLE_ADDED))
5284 continue;
5286 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5287 strlen(path_base_tree)))
5288 continue;
5290 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5291 ct->in_repo_path);
5292 if (err)
5293 goto done;
5295 slash = strchr(child_path, '/');
5296 if (slash == NULL) {
5297 err = alloc_added_blob_tree_entry(&new_te, ct);
5298 if (err)
5299 goto done;
5300 err = report_ct_status(ct, status_cb, status_arg);
5301 if (err)
5302 goto done;
5303 ct->flags |= GOT_COMMITABLE_ADDED;
5304 err = insert_tree_entry(new_te, &paths);
5305 if (err)
5306 goto done;
5307 (*nentries)++;
5308 } else {
5309 *slash = '\0'; /* trim trailing path components */
5310 if (base_tree == NULL ||
5311 got_object_tree_find_entry(base_tree, child_path)
5312 == NULL) {
5313 err = make_subtree_for_added_blob(&new_te,
5314 child_path, path_base_tree,
5315 commitable_paths, status_cb, status_arg,
5316 repo);
5317 if (err)
5318 goto done;
5319 err = insert_tree_entry(new_te, &paths);
5320 if (err)
5321 goto done;
5322 (*nentries)++;
5327 if (base_tree) {
5328 int i, nbase_entries;
5329 /* Handle modified and deleted entries. */
5330 nbase_entries = got_object_tree_get_nentries(base_tree);
5331 for (i = 0; i < nbase_entries; i++) {
5332 struct got_commitable *ct = NULL;
5334 te = got_object_tree_get_entry(base_tree, i);
5335 if (got_object_tree_entry_is_submodule(te)) {
5336 /* Entry is a submodule; just copy it. */
5337 err = got_object_tree_entry_dup(&new_te, te);
5338 if (err)
5339 goto done;
5340 err = insert_tree_entry(new_te, &paths);
5341 if (err)
5342 goto done;
5343 (*nentries)++;
5344 continue;
5347 if (S_ISDIR(te->mode)) {
5348 int modified;
5349 err = got_object_tree_entry_dup(&new_te, te);
5350 if (err)
5351 goto done;
5352 err = match_modified_subtree(&modified, te,
5353 path_base_tree, commitable_paths);
5354 if (err)
5355 goto done;
5356 /* Avoid recursion into unmodified subtrees. */
5357 if (modified) {
5358 struct got_object_id *new_id;
5359 int nsubentries;
5360 err = write_subtree(&new_id,
5361 &nsubentries, te,
5362 path_base_tree, commitable_paths,
5363 status_cb, status_arg, repo);
5364 if (err)
5365 goto done;
5366 if (nsubentries == 0) {
5367 /* All entries were deleted. */
5368 free(new_id);
5369 continue;
5371 memcpy(&new_te->id, new_id,
5372 sizeof(new_te->id));
5373 free(new_id);
5375 err = insert_tree_entry(new_te, &paths);
5376 if (err)
5377 goto done;
5378 (*nentries)++;
5379 continue;
5382 err = match_deleted_or_modified_ct(&ct, te,
5383 path_base_tree, commitable_paths);
5384 if (err)
5385 goto done;
5386 if (ct) {
5387 /* NB: Deleted entries get dropped here. */
5388 if (ct->status == GOT_STATUS_MODIFY ||
5389 ct->status == GOT_STATUS_MODE_CHANGE ||
5390 ct->staged_status == GOT_STATUS_MODIFY) {
5391 err = alloc_modified_blob_tree_entry(
5392 &new_te, te, ct);
5393 if (err)
5394 goto done;
5395 err = insert_tree_entry(new_te, &paths);
5396 if (err)
5397 goto done;
5398 (*nentries)++;
5400 err = report_ct_status(ct, status_cb,
5401 status_arg);
5402 if (err)
5403 goto done;
5404 } else {
5405 /* Entry is unchanged; just copy it. */
5406 err = got_object_tree_entry_dup(&new_te, te);
5407 if (err)
5408 goto done;
5409 err = insert_tree_entry(new_te, &paths);
5410 if (err)
5411 goto done;
5412 (*nentries)++;
5417 /* Write new list of entries; deleted entries have been dropped. */
5418 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5419 done:
5420 got_pathlist_free(&paths);
5421 return err;
5424 static const struct got_error *
5425 update_fileindex_after_commit(struct got_worktree *worktree,
5426 struct got_pathlist_head *commitable_paths,
5427 struct got_object_id *new_base_commit_id,
5428 struct got_fileindex *fileindex, int have_staged_files)
5430 const struct got_error *err = NULL;
5431 struct got_pathlist_entry *pe;
5432 char *relpath = NULL;
5434 TAILQ_FOREACH(pe, commitable_paths, entry) {
5435 struct got_fileindex_entry *ie;
5436 struct got_commitable *ct = pe->data;
5438 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5440 err = got_path_skip_common_ancestor(&relpath,
5441 worktree->root_path, ct->ondisk_path);
5442 if (err)
5443 goto done;
5445 if (ie) {
5446 if (ct->status == GOT_STATUS_DELETE ||
5447 ct->staged_status == GOT_STATUS_DELETE) {
5448 got_fileindex_entry_remove(fileindex, ie);
5449 } else if (ct->staged_status == GOT_STATUS_ADD ||
5450 ct->staged_status == GOT_STATUS_MODIFY) {
5451 got_fileindex_entry_stage_set(ie,
5452 GOT_FILEIDX_STAGE_NONE);
5453 got_fileindex_entry_staged_filetype_set(ie, 0);
5455 err = got_fileindex_entry_update(ie,
5456 worktree->root_fd, relpath,
5457 ct->staged_blob_id->sha1,
5458 new_base_commit_id->sha1,
5459 !have_staged_files);
5460 } else
5461 err = got_fileindex_entry_update(ie,
5462 worktree->root_fd, relpath,
5463 ct->blob_id->sha1,
5464 new_base_commit_id->sha1,
5465 !have_staged_files);
5466 } else {
5467 err = got_fileindex_entry_alloc(&ie, pe->path);
5468 if (err)
5469 goto done;
5470 err = got_fileindex_entry_update(ie,
5471 worktree->root_fd, relpath, ct->blob_id->sha1,
5472 new_base_commit_id->sha1, 1);
5473 if (err) {
5474 got_fileindex_entry_free(ie);
5475 goto done;
5477 err = got_fileindex_entry_add(fileindex, ie);
5478 if (err) {
5479 got_fileindex_entry_free(ie);
5480 goto done;
5483 free(relpath);
5484 relpath = NULL;
5486 done:
5487 free(relpath);
5488 return err;
5492 static const struct got_error *
5493 check_out_of_date(const char *in_repo_path, unsigned char status,
5494 unsigned char staged_status, struct got_object_id *base_blob_id,
5495 struct got_object_id *base_commit_id,
5496 struct got_object_id *head_commit_id, struct got_repository *repo,
5497 int ood_errcode)
5499 const struct got_error *err = NULL;
5500 struct got_object_id *id = NULL;
5502 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5503 /* Trivial case: base commit == head commit */
5504 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5505 return NULL;
5507 * Ensure file content which local changes were based
5508 * on matches file content in the branch head.
5510 err = got_object_id_by_path(&id, repo, head_commit_id,
5511 in_repo_path);
5512 if (err) {
5513 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5514 err = got_error(ood_errcode);
5515 goto done;
5516 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5517 err = got_error(ood_errcode);
5518 } else {
5519 /* Require that added files don't exist in the branch head. */
5520 err = got_object_id_by_path(&id, repo, head_commit_id,
5521 in_repo_path);
5522 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5523 goto done;
5524 err = id ? got_error(ood_errcode) : NULL;
5526 done:
5527 free(id);
5528 return err;
5531 const struct got_error *
5532 commit_worktree(struct got_object_id **new_commit_id,
5533 struct got_pathlist_head *commitable_paths,
5534 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5535 const char *author, const char *committer,
5536 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5537 got_worktree_status_cb status_cb, void *status_arg,
5538 struct got_repository *repo)
5540 const struct got_error *err = NULL, *unlockerr = NULL;
5541 struct got_pathlist_entry *pe;
5542 const char *head_ref_name = NULL;
5543 struct got_commit_object *head_commit = NULL;
5544 struct got_reference *head_ref2 = NULL;
5545 struct got_object_id *head_commit_id2 = NULL;
5546 struct got_tree_object *head_tree = NULL;
5547 struct got_object_id *new_tree_id = NULL;
5548 int nentries;
5549 struct got_object_id_queue parent_ids;
5550 struct got_object_qid *pid = NULL;
5551 char *logmsg = NULL;
5553 *new_commit_id = NULL;
5555 SIMPLEQ_INIT(&parent_ids);
5557 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5558 if (err)
5559 goto done;
5561 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5562 if (err)
5563 goto done;
5565 if (commit_msg_cb != NULL) {
5566 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5567 if (err)
5568 goto done;
5571 if (logmsg == NULL || strlen(logmsg) == 0) {
5572 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5573 goto done;
5576 /* Create blobs from added and modified files and record their IDs. */
5577 TAILQ_FOREACH(pe, commitable_paths, entry) {
5578 struct got_commitable *ct = pe->data;
5579 char *ondisk_path;
5581 /* Blobs for staged files already exist. */
5582 if (ct->staged_status == GOT_STATUS_ADD ||
5583 ct->staged_status == GOT_STATUS_MODIFY)
5584 continue;
5586 if (ct->status != GOT_STATUS_ADD &&
5587 ct->status != GOT_STATUS_MODIFY &&
5588 ct->status != GOT_STATUS_MODE_CHANGE)
5589 continue;
5591 if (asprintf(&ondisk_path, "%s/%s",
5592 worktree->root_path, pe->path) == -1) {
5593 err = got_error_from_errno("asprintf");
5594 goto done;
5596 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5597 free(ondisk_path);
5598 if (err)
5599 goto done;
5602 /* Recursively write new tree objects. */
5603 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5604 commitable_paths, status_cb, status_arg, repo);
5605 if (err)
5606 goto done;
5608 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5609 if (err)
5610 goto done;
5611 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5612 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5613 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5614 got_object_qid_free(pid);
5615 if (logmsg != NULL)
5616 free(logmsg);
5617 if (err)
5618 goto done;
5620 /* Check if a concurrent commit to our branch has occurred. */
5621 head_ref_name = got_worktree_get_head_ref_name(worktree);
5622 if (head_ref_name == NULL) {
5623 err = got_error_from_errno("got_worktree_get_head_ref_name");
5624 goto done;
5626 /* Lock the reference here to prevent concurrent modification. */
5627 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5628 if (err)
5629 goto done;
5630 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5631 if (err)
5632 goto done;
5633 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5634 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5635 goto done;
5637 /* Update branch head in repository. */
5638 err = got_ref_change_ref(head_ref2, *new_commit_id);
5639 if (err)
5640 goto done;
5641 err = got_ref_write(head_ref2, repo);
5642 if (err)
5643 goto done;
5645 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5646 if (err)
5647 goto done;
5649 err = ref_base_commit(worktree, repo);
5650 if (err)
5651 goto done;
5652 done:
5653 if (head_tree)
5654 got_object_tree_close(head_tree);
5655 if (head_commit)
5656 got_object_commit_close(head_commit);
5657 free(head_commit_id2);
5658 if (head_ref2) {
5659 unlockerr = got_ref_unlock(head_ref2);
5660 if (unlockerr && err == NULL)
5661 err = unlockerr;
5662 got_ref_close(head_ref2);
5664 return err;
5667 static const struct got_error *
5668 check_path_is_commitable(const char *path,
5669 struct got_pathlist_head *commitable_paths)
5671 struct got_pathlist_entry *cpe = NULL;
5672 size_t path_len = strlen(path);
5674 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5675 struct got_commitable *ct = cpe->data;
5676 const char *ct_path = ct->path;
5678 while (ct_path[0] == '/')
5679 ct_path++;
5681 if (strcmp(path, ct_path) == 0 ||
5682 got_path_is_child(ct_path, path, path_len))
5683 break;
5686 if (cpe == NULL)
5687 return got_error_path(path, GOT_ERR_BAD_PATH);
5689 return NULL;
5692 static const struct got_error *
5693 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5695 int *have_staged_files = arg;
5697 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5698 *have_staged_files = 1;
5699 return got_error(GOT_ERR_CANCELLED);
5702 return NULL;
5705 static const struct got_error *
5706 check_non_staged_files(struct got_fileindex *fileindex,
5707 struct got_pathlist_head *paths)
5709 struct got_pathlist_entry *pe;
5710 struct got_fileindex_entry *ie;
5712 TAILQ_FOREACH(pe, paths, entry) {
5713 if (pe->path[0] == '\0')
5714 continue;
5715 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5716 if (ie == NULL)
5717 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5718 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5719 return got_error_path(pe->path,
5720 GOT_ERR_FILE_NOT_STAGED);
5723 return NULL;
5726 const struct got_error *
5727 got_worktree_commit(struct got_object_id **new_commit_id,
5728 struct got_worktree *worktree, struct got_pathlist_head *paths,
5729 const char *author, const char *committer, int allow_bad_symlinks,
5730 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5731 got_worktree_status_cb status_cb, void *status_arg,
5732 struct got_repository *repo)
5734 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5735 struct got_fileindex *fileindex = NULL;
5736 char *fileindex_path = NULL;
5737 struct got_pathlist_head commitable_paths;
5738 struct collect_commitables_arg cc_arg;
5739 struct got_pathlist_entry *pe;
5740 struct got_reference *head_ref = NULL;
5741 struct got_object_id *head_commit_id = NULL;
5742 int have_staged_files = 0;
5744 *new_commit_id = NULL;
5746 TAILQ_INIT(&commitable_paths);
5748 err = lock_worktree(worktree, LOCK_EX);
5749 if (err)
5750 goto done;
5752 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5753 if (err)
5754 goto done;
5756 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5757 if (err)
5758 goto done;
5760 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5761 if (err)
5762 goto done;
5764 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5765 &have_staged_files);
5766 if (err && err->code != GOT_ERR_CANCELLED)
5767 goto done;
5768 if (have_staged_files) {
5769 err = check_non_staged_files(fileindex, paths);
5770 if (err)
5771 goto done;
5774 cc_arg.commitable_paths = &commitable_paths;
5775 cc_arg.worktree = worktree;
5776 cc_arg.fileindex = fileindex;
5777 cc_arg.repo = repo;
5778 cc_arg.have_staged_files = have_staged_files;
5779 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5780 TAILQ_FOREACH(pe, paths, entry) {
5781 err = worktree_status(worktree, pe->path, fileindex, repo,
5782 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5783 if (err)
5784 goto done;
5787 if (TAILQ_EMPTY(&commitable_paths)) {
5788 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5789 goto done;
5792 TAILQ_FOREACH(pe, paths, entry) {
5793 err = check_path_is_commitable(pe->path, &commitable_paths);
5794 if (err)
5795 goto done;
5798 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5799 struct got_commitable *ct = pe->data;
5800 const char *ct_path = ct->in_repo_path;
5802 while (ct_path[0] == '/')
5803 ct_path++;
5804 err = check_out_of_date(ct_path, ct->status,
5805 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5806 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5807 if (err)
5808 goto done;
5812 err = commit_worktree(new_commit_id, &commitable_paths,
5813 head_commit_id, worktree, author, committer,
5814 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5815 if (err)
5816 goto done;
5818 err = update_fileindex_after_commit(worktree, &commitable_paths,
5819 *new_commit_id, fileindex, have_staged_files);
5820 sync_err = sync_fileindex(fileindex, fileindex_path);
5821 if (sync_err && err == NULL)
5822 err = sync_err;
5823 done:
5824 if (fileindex)
5825 got_fileindex_free(fileindex);
5826 free(fileindex_path);
5827 unlockerr = lock_worktree(worktree, LOCK_SH);
5828 if (unlockerr && err == NULL)
5829 err = unlockerr;
5830 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5831 struct got_commitable *ct = pe->data;
5832 free_commitable(ct);
5834 got_pathlist_free(&commitable_paths);
5835 return err;
5838 const char *
5839 got_commitable_get_path(struct got_commitable *ct)
5841 return ct->path;
5844 unsigned int
5845 got_commitable_get_status(struct got_commitable *ct)
5847 return ct->status;
5850 struct check_rebase_ok_arg {
5851 struct got_worktree *worktree;
5852 struct got_repository *repo;
5855 static const struct got_error *
5856 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5858 const struct got_error *err = NULL;
5859 struct check_rebase_ok_arg *a = arg;
5860 unsigned char status;
5861 struct stat sb;
5862 char *ondisk_path;
5864 /* Reject rebase of a work tree with mixed base commits. */
5865 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5866 SHA1_DIGEST_LENGTH))
5867 return got_error(GOT_ERR_MIXED_COMMITS);
5869 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5870 == -1)
5871 return got_error_from_errno("asprintf");
5873 /* Reject rebase of a work tree with modified or staged files. */
5874 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5875 free(ondisk_path);
5876 if (err)
5877 return err;
5879 if (status != GOT_STATUS_NO_CHANGE)
5880 return got_error(GOT_ERR_MODIFIED);
5881 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5882 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5884 return NULL;
5887 const struct got_error *
5888 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5889 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5890 struct got_worktree *worktree, struct got_reference *branch,
5891 struct got_repository *repo)
5893 const struct got_error *err = NULL;
5894 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5895 char *branch_ref_name = NULL;
5896 char *fileindex_path = NULL;
5897 struct check_rebase_ok_arg ok_arg;
5898 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5899 struct got_object_id *wt_branch_tip = NULL;
5901 *new_base_branch_ref = NULL;
5902 *tmp_branch = NULL;
5903 *fileindex = NULL;
5905 err = lock_worktree(worktree, LOCK_EX);
5906 if (err)
5907 return err;
5909 err = open_fileindex(fileindex, &fileindex_path, worktree);
5910 if (err)
5911 goto done;
5913 ok_arg.worktree = worktree;
5914 ok_arg.repo = repo;
5915 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5916 &ok_arg);
5917 if (err)
5918 goto done;
5920 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5921 if (err)
5922 goto done;
5924 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5925 if (err)
5926 goto done;
5928 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5929 if (err)
5930 goto done;
5932 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5933 0);
5934 if (err)
5935 goto done;
5937 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5938 if (err)
5939 goto done;
5940 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5941 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5942 goto done;
5945 err = got_ref_alloc_symref(new_base_branch_ref,
5946 new_base_branch_ref_name, wt_branch);
5947 if (err)
5948 goto done;
5949 err = got_ref_write(*new_base_branch_ref, repo);
5950 if (err)
5951 goto done;
5953 /* TODO Lock original branch's ref while rebasing? */
5955 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5956 if (err)
5957 goto done;
5959 err = got_ref_write(branch_ref, repo);
5960 if (err)
5961 goto done;
5963 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5964 worktree->base_commit_id);
5965 if (err)
5966 goto done;
5967 err = got_ref_write(*tmp_branch, repo);
5968 if (err)
5969 goto done;
5971 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5972 if (err)
5973 goto done;
5974 done:
5975 free(fileindex_path);
5976 free(tmp_branch_name);
5977 free(new_base_branch_ref_name);
5978 free(branch_ref_name);
5979 if (branch_ref)
5980 got_ref_close(branch_ref);
5981 if (wt_branch)
5982 got_ref_close(wt_branch);
5983 free(wt_branch_tip);
5984 if (err) {
5985 if (*new_base_branch_ref) {
5986 got_ref_close(*new_base_branch_ref);
5987 *new_base_branch_ref = NULL;
5989 if (*tmp_branch) {
5990 got_ref_close(*tmp_branch);
5991 *tmp_branch = NULL;
5993 if (*fileindex) {
5994 got_fileindex_free(*fileindex);
5995 *fileindex = NULL;
5997 lock_worktree(worktree, LOCK_SH);
5999 return err;
6002 const struct got_error *
6003 got_worktree_rebase_continue(struct got_object_id **commit_id,
6004 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6005 struct got_reference **branch, struct got_fileindex **fileindex,
6006 struct got_worktree *worktree, struct got_repository *repo)
6008 const struct got_error *err;
6009 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6010 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6011 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6012 char *fileindex_path = NULL;
6013 int have_staged_files = 0;
6015 *commit_id = NULL;
6016 *new_base_branch = NULL;
6017 *tmp_branch = NULL;
6018 *branch = NULL;
6019 *fileindex = NULL;
6021 err = lock_worktree(worktree, LOCK_EX);
6022 if (err)
6023 return err;
6025 err = open_fileindex(fileindex, &fileindex_path, worktree);
6026 if (err)
6027 goto done;
6029 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6030 &have_staged_files);
6031 if (err && err->code != GOT_ERR_CANCELLED)
6032 goto done;
6033 if (have_staged_files) {
6034 err = got_error(GOT_ERR_STAGED_PATHS);
6035 goto done;
6038 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6039 if (err)
6040 goto done;
6042 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6043 if (err)
6044 goto done;
6046 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6047 if (err)
6048 goto done;
6050 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6051 if (err)
6052 goto done;
6054 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6055 if (err)
6056 goto done;
6058 err = got_ref_open(branch, repo,
6059 got_ref_get_symref_target(branch_ref), 0);
6060 if (err)
6061 goto done;
6063 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6064 if (err)
6065 goto done;
6067 err = got_ref_resolve(commit_id, repo, commit_ref);
6068 if (err)
6069 goto done;
6071 err = got_ref_open(new_base_branch, repo,
6072 new_base_branch_ref_name, 0);
6073 if (err)
6074 goto done;
6076 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6077 if (err)
6078 goto done;
6079 done:
6080 free(commit_ref_name);
6081 free(branch_ref_name);
6082 free(fileindex_path);
6083 if (commit_ref)
6084 got_ref_close(commit_ref);
6085 if (branch_ref)
6086 got_ref_close(branch_ref);
6087 if (err) {
6088 free(*commit_id);
6089 *commit_id = NULL;
6090 if (*tmp_branch) {
6091 got_ref_close(*tmp_branch);
6092 *tmp_branch = NULL;
6094 if (*new_base_branch) {
6095 got_ref_close(*new_base_branch);
6096 *new_base_branch = NULL;
6098 if (*branch) {
6099 got_ref_close(*branch);
6100 *branch = NULL;
6102 if (*fileindex) {
6103 got_fileindex_free(*fileindex);
6104 *fileindex = NULL;
6106 lock_worktree(worktree, LOCK_SH);
6108 return err;
6111 const struct got_error *
6112 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6114 const struct got_error *err;
6115 char *tmp_branch_name = NULL;
6117 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6118 if (err)
6119 return err;
6121 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6122 free(tmp_branch_name);
6123 return NULL;
6126 static const struct got_error *
6127 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6128 char **logmsg, void *arg)
6130 *logmsg = arg;
6131 return NULL;
6134 static const struct got_error *
6135 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6136 const char *path, struct got_object_id *blob_id,
6137 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6138 int dirfd, const char *de_name)
6140 return NULL;
6143 struct collect_merged_paths_arg {
6144 got_worktree_checkout_cb progress_cb;
6145 void *progress_arg;
6146 struct got_pathlist_head *merged_paths;
6149 static const struct got_error *
6150 collect_merged_paths(void *arg, unsigned char status, const char *path)
6152 const struct got_error *err;
6153 struct collect_merged_paths_arg *a = arg;
6154 char *p;
6155 struct got_pathlist_entry *new;
6157 err = (*a->progress_cb)(a->progress_arg, status, path);
6158 if (err)
6159 return err;
6161 if (status != GOT_STATUS_MERGE &&
6162 status != GOT_STATUS_ADD &&
6163 status != GOT_STATUS_DELETE &&
6164 status != GOT_STATUS_CONFLICT)
6165 return NULL;
6167 p = strdup(path);
6168 if (p == NULL)
6169 return got_error_from_errno("strdup");
6171 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6172 if (err || new == NULL)
6173 free(p);
6174 return err;
6177 void
6178 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6180 struct got_pathlist_entry *pe;
6182 TAILQ_FOREACH(pe, merged_paths, entry)
6183 free((char *)pe->path);
6185 got_pathlist_free(merged_paths);
6188 static const struct got_error *
6189 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6190 int is_rebase, struct got_repository *repo)
6192 const struct got_error *err;
6193 struct got_reference *commit_ref = NULL;
6195 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6196 if (err) {
6197 if (err->code != GOT_ERR_NOT_REF)
6198 goto done;
6199 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6200 if (err)
6201 goto done;
6202 err = got_ref_write(commit_ref, repo);
6203 if (err)
6204 goto done;
6205 } else if (is_rebase) {
6206 struct got_object_id *stored_id;
6207 int cmp;
6209 err = got_ref_resolve(&stored_id, repo, commit_ref);
6210 if (err)
6211 goto done;
6212 cmp = got_object_id_cmp(commit_id, stored_id);
6213 free(stored_id);
6214 if (cmp != 0) {
6215 err = got_error(GOT_ERR_REBASE_COMMITID);
6216 goto done;
6219 done:
6220 if (commit_ref)
6221 got_ref_close(commit_ref);
6222 return err;
6225 static const struct got_error *
6226 rebase_merge_files(struct got_pathlist_head *merged_paths,
6227 const char *commit_ref_name, struct got_worktree *worktree,
6228 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6229 struct got_object_id *commit_id, struct got_repository *repo,
6230 got_worktree_checkout_cb progress_cb, void *progress_arg,
6231 got_cancel_cb cancel_cb, void *cancel_arg)
6233 const struct got_error *err;
6234 struct got_reference *commit_ref = NULL;
6235 struct collect_merged_paths_arg cmp_arg;
6236 char *fileindex_path;
6238 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6240 err = get_fileindex_path(&fileindex_path, worktree);
6241 if (err)
6242 return err;
6244 cmp_arg.progress_cb = progress_cb;
6245 cmp_arg.progress_arg = progress_arg;
6246 cmp_arg.merged_paths = merged_paths;
6247 err = merge_files(worktree, fileindex, fileindex_path,
6248 parent_commit_id, commit_id, repo, collect_merged_paths,
6249 &cmp_arg, cancel_cb, cancel_arg);
6250 if (commit_ref)
6251 got_ref_close(commit_ref);
6252 return err;
6255 const struct got_error *
6256 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6257 struct got_worktree *worktree, struct got_fileindex *fileindex,
6258 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6259 struct got_repository *repo,
6260 got_worktree_checkout_cb progress_cb, void *progress_arg,
6261 got_cancel_cb cancel_cb, void *cancel_arg)
6263 const struct got_error *err;
6264 char *commit_ref_name;
6266 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6267 if (err)
6268 return err;
6270 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6271 if (err)
6272 goto done;
6274 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6275 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6276 progress_arg, cancel_cb, cancel_arg);
6277 done:
6278 free(commit_ref_name);
6279 return err;
6282 const struct got_error *
6283 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6284 struct got_worktree *worktree, struct got_fileindex *fileindex,
6285 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6286 struct got_repository *repo,
6287 got_worktree_checkout_cb progress_cb, void *progress_arg,
6288 got_cancel_cb cancel_cb, void *cancel_arg)
6290 const struct got_error *err;
6291 char *commit_ref_name;
6293 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6294 if (err)
6295 return err;
6297 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6298 if (err)
6299 goto done;
6301 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6302 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6303 progress_arg, cancel_cb, cancel_arg);
6304 done:
6305 free(commit_ref_name);
6306 return err;
6309 static const struct got_error *
6310 rebase_commit(struct got_object_id **new_commit_id,
6311 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6312 struct got_worktree *worktree, struct got_fileindex *fileindex,
6313 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6314 const char *new_logmsg, struct got_repository *repo)
6316 const struct got_error *err, *sync_err;
6317 struct got_pathlist_head commitable_paths;
6318 struct collect_commitables_arg cc_arg;
6319 char *fileindex_path = NULL;
6320 struct got_reference *head_ref = NULL;
6321 struct got_object_id *head_commit_id = NULL;
6322 char *logmsg = NULL;
6324 TAILQ_INIT(&commitable_paths);
6325 *new_commit_id = NULL;
6327 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6329 err = get_fileindex_path(&fileindex_path, worktree);
6330 if (err)
6331 return err;
6333 cc_arg.commitable_paths = &commitable_paths;
6334 cc_arg.worktree = worktree;
6335 cc_arg.repo = repo;
6336 cc_arg.have_staged_files = 0;
6338 * If possible get the status of individual files directly to
6339 * avoid crawling the entire work tree once per rebased commit.
6340 * TODO: Ideally, merged_paths would contain a list of commitables
6341 * we could use so we could skip worktree_status() entirely.
6343 if (merged_paths) {
6344 struct got_pathlist_entry *pe;
6345 TAILQ_FOREACH(pe, merged_paths, entry) {
6346 err = worktree_status(worktree, pe->path, fileindex,
6347 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6348 0);
6349 if (err)
6350 goto done;
6352 } else {
6353 err = worktree_status(worktree, "", fileindex, repo,
6354 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6355 if (err)
6356 goto done;
6359 if (TAILQ_EMPTY(&commitable_paths)) {
6360 /* No-op change; commit will be elided. */
6361 err = got_ref_delete(commit_ref, repo);
6362 if (err)
6363 goto done;
6364 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6365 goto done;
6368 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6369 if (err)
6370 goto done;
6372 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6373 if (err)
6374 goto done;
6376 if (new_logmsg) {
6377 logmsg = strdup(new_logmsg);
6378 if (logmsg == NULL) {
6379 err = got_error_from_errno("strdup");
6380 goto done;
6382 } else {
6383 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6384 if (err)
6385 goto done;
6388 /* NB: commit_worktree will call free(logmsg) */
6389 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6390 worktree, got_object_commit_get_author(orig_commit),
6391 got_object_commit_get_committer(orig_commit),
6392 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6393 if (err)
6394 goto done;
6396 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6397 if (err)
6398 goto done;
6400 err = got_ref_delete(commit_ref, repo);
6401 if (err)
6402 goto done;
6404 err = update_fileindex_after_commit(worktree, &commitable_paths,
6405 *new_commit_id, fileindex, 0);
6406 sync_err = sync_fileindex(fileindex, fileindex_path);
6407 if (sync_err && err == NULL)
6408 err = sync_err;
6409 done:
6410 free(fileindex_path);
6411 free(head_commit_id);
6412 if (head_ref)
6413 got_ref_close(head_ref);
6414 if (err) {
6415 free(*new_commit_id);
6416 *new_commit_id = NULL;
6418 return err;
6421 const struct got_error *
6422 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6423 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6424 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6425 struct got_commit_object *orig_commit,
6426 struct got_object_id *orig_commit_id, struct got_repository *repo)
6428 const struct got_error *err;
6429 char *commit_ref_name;
6430 struct got_reference *commit_ref = NULL;
6431 struct got_object_id *commit_id = NULL;
6433 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6434 if (err)
6435 return err;
6437 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6438 if (err)
6439 goto done;
6440 err = got_ref_resolve(&commit_id, repo, commit_ref);
6441 if (err)
6442 goto done;
6443 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6444 err = got_error(GOT_ERR_REBASE_COMMITID);
6445 goto done;
6448 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6449 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6450 done:
6451 if (commit_ref)
6452 got_ref_close(commit_ref);
6453 free(commit_ref_name);
6454 free(commit_id);
6455 return err;
6458 const struct got_error *
6459 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6460 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6461 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6462 struct got_commit_object *orig_commit,
6463 struct got_object_id *orig_commit_id, const char *new_logmsg,
6464 struct got_repository *repo)
6466 const struct got_error *err;
6467 char *commit_ref_name;
6468 struct got_reference *commit_ref = NULL;
6470 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6471 if (err)
6472 return err;
6474 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6475 if (err)
6476 goto done;
6478 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6479 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6480 done:
6481 if (commit_ref)
6482 got_ref_close(commit_ref);
6483 free(commit_ref_name);
6484 return err;
6487 const struct got_error *
6488 got_worktree_rebase_postpone(struct got_worktree *worktree,
6489 struct got_fileindex *fileindex)
6491 if (fileindex)
6492 got_fileindex_free(fileindex);
6493 return lock_worktree(worktree, LOCK_SH);
6496 static const struct got_error *
6497 delete_ref(const char *name, struct got_repository *repo)
6499 const struct got_error *err;
6500 struct got_reference *ref;
6502 err = got_ref_open(&ref, repo, name, 0);
6503 if (err) {
6504 if (err->code == GOT_ERR_NOT_REF)
6505 return NULL;
6506 return err;
6509 err = got_ref_delete(ref, repo);
6510 got_ref_close(ref);
6511 return err;
6514 static const struct got_error *
6515 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6517 const struct got_error *err;
6518 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6519 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6521 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6522 if (err)
6523 goto done;
6524 err = delete_ref(tmp_branch_name, repo);
6525 if (err)
6526 goto done;
6528 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6529 if (err)
6530 goto done;
6531 err = delete_ref(new_base_branch_ref_name, repo);
6532 if (err)
6533 goto done;
6535 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6536 if (err)
6537 goto done;
6538 err = delete_ref(branch_ref_name, repo);
6539 if (err)
6540 goto done;
6542 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6543 if (err)
6544 goto done;
6545 err = delete_ref(commit_ref_name, repo);
6546 if (err)
6547 goto done;
6549 done:
6550 free(tmp_branch_name);
6551 free(new_base_branch_ref_name);
6552 free(branch_ref_name);
6553 free(commit_ref_name);
6554 return err;
6557 const struct got_error *
6558 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6559 struct got_object_id *new_commit_id, struct got_repository *repo)
6561 const struct got_error *err;
6562 struct got_reference *ref = NULL;
6563 struct got_object_id *old_commit_id = NULL;
6564 const char *branch_name = NULL;
6565 char *new_id_str = NULL;
6566 char *refname = NULL;
6568 branch_name = got_ref_get_name(branch);
6569 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6570 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6571 branch_name += 11;
6573 err = got_object_id_str(&new_id_str, new_commit_id);
6574 if (err)
6575 return err;
6577 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6578 new_id_str) == -1) {
6579 err = got_error_from_errno("asprintf");
6580 goto done;
6583 err = got_ref_resolve(&old_commit_id, repo, branch);
6584 if (err)
6585 goto done;
6587 err = got_ref_alloc(&ref, refname, old_commit_id);
6588 if (err)
6589 goto done;
6591 err = got_ref_write(ref, repo);
6592 done:
6593 free(new_id_str);
6594 free(refname);
6595 free(old_commit_id);
6596 if (ref)
6597 got_ref_close(ref);
6598 return err;
6601 const struct got_error *
6602 got_worktree_rebase_complete(struct got_worktree *worktree,
6603 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6604 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6605 struct got_repository *repo, int create_backup)
6607 const struct got_error *err, *unlockerr, *sync_err;
6608 struct got_object_id *new_head_commit_id = NULL;
6609 char *fileindex_path = NULL;
6611 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6612 if (err)
6613 return err;
6615 if (create_backup) {
6616 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6617 rebased_branch, new_head_commit_id, repo);
6618 if (err)
6619 goto done;
6622 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6623 if (err)
6624 goto done;
6626 err = got_ref_write(rebased_branch, repo);
6627 if (err)
6628 goto done;
6630 err = got_worktree_set_head_ref(worktree, rebased_branch);
6631 if (err)
6632 goto done;
6634 err = delete_rebase_refs(worktree, repo);
6635 if (err)
6636 goto done;
6638 err = get_fileindex_path(&fileindex_path, worktree);
6639 if (err)
6640 goto done;
6641 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6642 sync_err = sync_fileindex(fileindex, fileindex_path);
6643 if (sync_err && err == NULL)
6644 err = sync_err;
6645 done:
6646 got_fileindex_free(fileindex);
6647 free(fileindex_path);
6648 free(new_head_commit_id);
6649 unlockerr = lock_worktree(worktree, LOCK_SH);
6650 if (unlockerr && err == NULL)
6651 err = unlockerr;
6652 return err;
6655 const struct got_error *
6656 got_worktree_rebase_abort(struct got_worktree *worktree,
6657 struct got_fileindex *fileindex, struct got_repository *repo,
6658 struct got_reference *new_base_branch,
6659 got_worktree_checkout_cb progress_cb, void *progress_arg)
6661 const struct got_error *err, *unlockerr, *sync_err;
6662 struct got_reference *resolved = NULL;
6663 struct got_object_id *commit_id = NULL;
6664 char *fileindex_path = NULL;
6665 struct revert_file_args rfa;
6666 struct got_object_id *tree_id = NULL;
6668 err = lock_worktree(worktree, LOCK_EX);
6669 if (err)
6670 return err;
6672 err = got_ref_open(&resolved, repo,
6673 got_ref_get_symref_target(new_base_branch), 0);
6674 if (err)
6675 goto done;
6677 err = got_worktree_set_head_ref(worktree, resolved);
6678 if (err)
6679 goto done;
6682 * XXX commits to the base branch could have happened while
6683 * we were busy rebasing; should we store the original commit ID
6684 * when rebase begins and read it back here?
6686 err = got_ref_resolve(&commit_id, repo, resolved);
6687 if (err)
6688 goto done;
6690 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6691 if (err)
6692 goto done;
6694 err = got_object_id_by_path(&tree_id, repo,
6695 worktree->base_commit_id, worktree->path_prefix);
6696 if (err)
6697 goto done;
6699 err = delete_rebase_refs(worktree, repo);
6700 if (err)
6701 goto done;
6703 err = get_fileindex_path(&fileindex_path, worktree);
6704 if (err)
6705 goto done;
6707 rfa.worktree = worktree;
6708 rfa.fileindex = fileindex;
6709 rfa.progress_cb = progress_cb;
6710 rfa.progress_arg = progress_arg;
6711 rfa.patch_cb = NULL;
6712 rfa.patch_arg = NULL;
6713 rfa.repo = repo;
6714 err = worktree_status(worktree, "", fileindex, repo,
6715 revert_file, &rfa, NULL, NULL, 0, 0);
6716 if (err)
6717 goto sync;
6719 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6720 repo, progress_cb, progress_arg, NULL, NULL);
6721 sync:
6722 sync_err = sync_fileindex(fileindex, fileindex_path);
6723 if (sync_err && err == NULL)
6724 err = sync_err;
6725 done:
6726 got_ref_close(resolved);
6727 free(tree_id);
6728 free(commit_id);
6729 if (fileindex)
6730 got_fileindex_free(fileindex);
6731 free(fileindex_path);
6733 unlockerr = lock_worktree(worktree, LOCK_SH);
6734 if (unlockerr && err == NULL)
6735 err = unlockerr;
6736 return err;
6739 const struct got_error *
6740 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6741 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6742 struct got_fileindex **fileindex, struct got_worktree *worktree,
6743 struct got_repository *repo)
6745 const struct got_error *err = NULL;
6746 char *tmp_branch_name = NULL;
6747 char *branch_ref_name = NULL;
6748 char *base_commit_ref_name = NULL;
6749 char *fileindex_path = NULL;
6750 struct check_rebase_ok_arg ok_arg;
6751 struct got_reference *wt_branch = NULL;
6752 struct got_reference *base_commit_ref = NULL;
6754 *tmp_branch = NULL;
6755 *branch_ref = NULL;
6756 *base_commit_id = NULL;
6757 *fileindex = NULL;
6759 err = lock_worktree(worktree, LOCK_EX);
6760 if (err)
6761 return err;
6763 err = open_fileindex(fileindex, &fileindex_path, worktree);
6764 if (err)
6765 goto done;
6767 ok_arg.worktree = worktree;
6768 ok_arg.repo = repo;
6769 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6770 &ok_arg);
6771 if (err)
6772 goto done;
6774 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6775 if (err)
6776 goto done;
6778 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6779 if (err)
6780 goto done;
6782 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6783 worktree);
6784 if (err)
6785 goto done;
6787 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6788 0);
6789 if (err)
6790 goto done;
6792 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6793 if (err)
6794 goto done;
6796 err = got_ref_write(*branch_ref, repo);
6797 if (err)
6798 goto done;
6800 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6801 worktree->base_commit_id);
6802 if (err)
6803 goto done;
6804 err = got_ref_write(base_commit_ref, repo);
6805 if (err)
6806 goto done;
6807 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6808 if (*base_commit_id == NULL) {
6809 err = got_error_from_errno("got_object_id_dup");
6810 goto done;
6813 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6814 worktree->base_commit_id);
6815 if (err)
6816 goto done;
6817 err = got_ref_write(*tmp_branch, repo);
6818 if (err)
6819 goto done;
6821 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6822 if (err)
6823 goto done;
6824 done:
6825 free(fileindex_path);
6826 free(tmp_branch_name);
6827 free(branch_ref_name);
6828 free(base_commit_ref_name);
6829 if (wt_branch)
6830 got_ref_close(wt_branch);
6831 if (err) {
6832 if (*branch_ref) {
6833 got_ref_close(*branch_ref);
6834 *branch_ref = NULL;
6836 if (*tmp_branch) {
6837 got_ref_close(*tmp_branch);
6838 *tmp_branch = NULL;
6840 free(*base_commit_id);
6841 if (*fileindex) {
6842 got_fileindex_free(*fileindex);
6843 *fileindex = NULL;
6845 lock_worktree(worktree, LOCK_SH);
6847 return err;
6850 const struct got_error *
6851 got_worktree_histedit_postpone(struct got_worktree *worktree,
6852 struct got_fileindex *fileindex)
6854 if (fileindex)
6855 got_fileindex_free(fileindex);
6856 return lock_worktree(worktree, LOCK_SH);
6859 const struct got_error *
6860 got_worktree_histedit_in_progress(int *in_progress,
6861 struct got_worktree *worktree)
6863 const struct got_error *err;
6864 char *tmp_branch_name = NULL;
6866 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6867 if (err)
6868 return err;
6870 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6871 free(tmp_branch_name);
6872 return NULL;
6875 const struct got_error *
6876 got_worktree_histedit_continue(struct got_object_id **commit_id,
6877 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6878 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6879 struct got_worktree *worktree, struct got_repository *repo)
6881 const struct got_error *err;
6882 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6883 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6884 struct got_reference *commit_ref = NULL;
6885 struct got_reference *base_commit_ref = NULL;
6886 char *fileindex_path = NULL;
6887 int have_staged_files = 0;
6889 *commit_id = NULL;
6890 *tmp_branch = NULL;
6891 *base_commit_id = NULL;
6892 *fileindex = NULL;
6894 err = lock_worktree(worktree, LOCK_EX);
6895 if (err)
6896 return err;
6898 err = open_fileindex(fileindex, &fileindex_path, worktree);
6899 if (err)
6900 goto done;
6902 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6903 &have_staged_files);
6904 if (err && err->code != GOT_ERR_CANCELLED)
6905 goto done;
6906 if (have_staged_files) {
6907 err = got_error(GOT_ERR_STAGED_PATHS);
6908 goto done;
6911 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6912 if (err)
6913 goto done;
6915 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6916 if (err)
6917 goto done;
6919 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6920 if (err)
6921 goto done;
6923 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6924 worktree);
6925 if (err)
6926 goto done;
6928 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6929 if (err)
6930 goto done;
6932 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6933 if (err)
6934 goto done;
6935 err = got_ref_resolve(commit_id, repo, commit_ref);
6936 if (err)
6937 goto done;
6939 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6940 if (err)
6941 goto done;
6942 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6943 if (err)
6944 goto done;
6946 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6947 if (err)
6948 goto done;
6949 done:
6950 free(commit_ref_name);
6951 free(branch_ref_name);
6952 free(fileindex_path);
6953 if (commit_ref)
6954 got_ref_close(commit_ref);
6955 if (base_commit_ref)
6956 got_ref_close(base_commit_ref);
6957 if (err) {
6958 free(*commit_id);
6959 *commit_id = NULL;
6960 free(*base_commit_id);
6961 *base_commit_id = NULL;
6962 if (*tmp_branch) {
6963 got_ref_close(*tmp_branch);
6964 *tmp_branch = NULL;
6966 if (*fileindex) {
6967 got_fileindex_free(*fileindex);
6968 *fileindex = NULL;
6970 lock_worktree(worktree, LOCK_EX);
6972 return err;
6975 static const struct got_error *
6976 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6978 const struct got_error *err;
6979 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6980 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6982 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6983 if (err)
6984 goto done;
6985 err = delete_ref(tmp_branch_name, repo);
6986 if (err)
6987 goto done;
6989 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6990 worktree);
6991 if (err)
6992 goto done;
6993 err = delete_ref(base_commit_ref_name, repo);
6994 if (err)
6995 goto done;
6997 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6998 if (err)
6999 goto done;
7000 err = delete_ref(branch_ref_name, repo);
7001 if (err)
7002 goto done;
7004 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7005 if (err)
7006 goto done;
7007 err = delete_ref(commit_ref_name, repo);
7008 if (err)
7009 goto done;
7010 done:
7011 free(tmp_branch_name);
7012 free(base_commit_ref_name);
7013 free(branch_ref_name);
7014 free(commit_ref_name);
7015 return err;
7018 const struct got_error *
7019 got_worktree_histedit_abort(struct got_worktree *worktree,
7020 struct got_fileindex *fileindex, struct got_repository *repo,
7021 struct got_reference *branch, struct got_object_id *base_commit_id,
7022 got_worktree_checkout_cb progress_cb, void *progress_arg)
7024 const struct got_error *err, *unlockerr, *sync_err;
7025 struct got_reference *resolved = NULL;
7026 char *fileindex_path = NULL;
7027 struct got_object_id *tree_id = NULL;
7028 struct revert_file_args rfa;
7030 err = lock_worktree(worktree, LOCK_EX);
7031 if (err)
7032 return err;
7034 err = got_ref_open(&resolved, repo,
7035 got_ref_get_symref_target(branch), 0);
7036 if (err)
7037 goto done;
7039 err = got_worktree_set_head_ref(worktree, resolved);
7040 if (err)
7041 goto done;
7043 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7044 if (err)
7045 goto done;
7047 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7048 worktree->path_prefix);
7049 if (err)
7050 goto done;
7052 err = delete_histedit_refs(worktree, repo);
7053 if (err)
7054 goto done;
7056 err = get_fileindex_path(&fileindex_path, worktree);
7057 if (err)
7058 goto done;
7060 rfa.worktree = worktree;
7061 rfa.fileindex = fileindex;
7062 rfa.progress_cb = progress_cb;
7063 rfa.progress_arg = progress_arg;
7064 rfa.patch_cb = NULL;
7065 rfa.patch_arg = NULL;
7066 rfa.repo = repo;
7067 err = worktree_status(worktree, "", fileindex, repo,
7068 revert_file, &rfa, NULL, NULL, 0, 0);
7069 if (err)
7070 goto sync;
7072 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7073 repo, progress_cb, progress_arg, NULL, NULL);
7074 sync:
7075 sync_err = sync_fileindex(fileindex, fileindex_path);
7076 if (sync_err && err == NULL)
7077 err = sync_err;
7078 done:
7079 got_ref_close(resolved);
7080 free(tree_id);
7081 free(fileindex_path);
7083 unlockerr = lock_worktree(worktree, LOCK_SH);
7084 if (unlockerr && err == NULL)
7085 err = unlockerr;
7086 return err;
7089 const struct got_error *
7090 got_worktree_histedit_complete(struct got_worktree *worktree,
7091 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7092 struct got_reference *edited_branch, struct got_repository *repo)
7094 const struct got_error *err, *unlockerr, *sync_err;
7095 struct got_object_id *new_head_commit_id = NULL;
7096 struct got_reference *resolved = NULL;
7097 char *fileindex_path = NULL;
7099 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7100 if (err)
7101 return err;
7103 err = got_ref_open(&resolved, repo,
7104 got_ref_get_symref_target(edited_branch), 0);
7105 if (err)
7106 goto done;
7108 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7109 resolved, new_head_commit_id, repo);
7110 if (err)
7111 goto done;
7113 err = got_ref_change_ref(resolved, new_head_commit_id);
7114 if (err)
7115 goto done;
7117 err = got_ref_write(resolved, repo);
7118 if (err)
7119 goto done;
7121 err = got_worktree_set_head_ref(worktree, resolved);
7122 if (err)
7123 goto done;
7125 err = delete_histedit_refs(worktree, repo);
7126 if (err)
7127 goto done;
7129 err = get_fileindex_path(&fileindex_path, worktree);
7130 if (err)
7131 goto done;
7132 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7133 sync_err = sync_fileindex(fileindex, fileindex_path);
7134 if (sync_err && err == NULL)
7135 err = sync_err;
7136 done:
7137 got_fileindex_free(fileindex);
7138 free(fileindex_path);
7139 free(new_head_commit_id);
7140 unlockerr = lock_worktree(worktree, LOCK_SH);
7141 if (unlockerr && err == NULL)
7142 err = unlockerr;
7143 return err;
7146 const struct got_error *
7147 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7148 struct got_object_id *commit_id, struct got_repository *repo)
7150 const struct got_error *err;
7151 char *commit_ref_name;
7153 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7154 if (err)
7155 return err;
7157 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7158 if (err)
7159 goto done;
7161 err = delete_ref(commit_ref_name, repo);
7162 done:
7163 free(commit_ref_name);
7164 return err;
7167 const struct got_error *
7168 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7169 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7170 struct got_worktree *worktree, const char *refname,
7171 struct got_repository *repo)
7173 const struct got_error *err = NULL;
7174 char *fileindex_path = NULL;
7175 struct check_rebase_ok_arg ok_arg;
7177 *fileindex = NULL;
7178 *branch_ref = NULL;
7179 *base_branch_ref = NULL;
7181 err = lock_worktree(worktree, LOCK_EX);
7182 if (err)
7183 return err;
7185 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7186 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7187 "cannot integrate a branch into itself; "
7188 "update -b or different branch name required");
7189 goto done;
7192 err = open_fileindex(fileindex, &fileindex_path, worktree);
7193 if (err)
7194 goto done;
7196 /* Preconditions are the same as for rebase. */
7197 ok_arg.worktree = worktree;
7198 ok_arg.repo = repo;
7199 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7200 &ok_arg);
7201 if (err)
7202 goto done;
7204 err = got_ref_open(branch_ref, repo, refname, 1);
7205 if (err)
7206 goto done;
7208 err = got_ref_open(base_branch_ref, repo,
7209 got_worktree_get_head_ref_name(worktree), 1);
7210 done:
7211 if (err) {
7212 if (*branch_ref) {
7213 got_ref_close(*branch_ref);
7214 *branch_ref = NULL;
7216 if (*base_branch_ref) {
7217 got_ref_close(*base_branch_ref);
7218 *base_branch_ref = NULL;
7220 if (*fileindex) {
7221 got_fileindex_free(*fileindex);
7222 *fileindex = NULL;
7224 lock_worktree(worktree, LOCK_SH);
7226 return err;
7229 const struct got_error *
7230 got_worktree_integrate_continue(struct got_worktree *worktree,
7231 struct got_fileindex *fileindex, struct got_repository *repo,
7232 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7233 got_worktree_checkout_cb progress_cb, void *progress_arg,
7234 got_cancel_cb cancel_cb, void *cancel_arg)
7236 const struct got_error *err = NULL, *sync_err, *unlockerr;
7237 char *fileindex_path = NULL;
7238 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7240 err = get_fileindex_path(&fileindex_path, worktree);
7241 if (err)
7242 goto done;
7244 err = got_ref_resolve(&commit_id, repo, branch_ref);
7245 if (err)
7246 goto done;
7248 err = got_object_id_by_path(&tree_id, repo, commit_id,
7249 worktree->path_prefix);
7250 if (err)
7251 goto done;
7253 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7254 if (err)
7255 goto done;
7257 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7258 progress_cb, progress_arg, cancel_cb, cancel_arg);
7259 if (err)
7260 goto sync;
7262 err = got_ref_change_ref(base_branch_ref, commit_id);
7263 if (err)
7264 goto sync;
7266 err = got_ref_write(base_branch_ref, repo);
7267 if (err)
7268 goto sync;
7270 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7271 sync:
7272 sync_err = sync_fileindex(fileindex, fileindex_path);
7273 if (sync_err && err == NULL)
7274 err = sync_err;
7276 done:
7277 unlockerr = got_ref_unlock(branch_ref);
7278 if (unlockerr && err == NULL)
7279 err = unlockerr;
7280 got_ref_close(branch_ref);
7282 unlockerr = got_ref_unlock(base_branch_ref);
7283 if (unlockerr && err == NULL)
7284 err = unlockerr;
7285 got_ref_close(base_branch_ref);
7287 got_fileindex_free(fileindex);
7288 free(fileindex_path);
7289 free(tree_id);
7291 unlockerr = lock_worktree(worktree, LOCK_SH);
7292 if (unlockerr && err == NULL)
7293 err = unlockerr;
7294 return err;
7297 const struct got_error *
7298 got_worktree_integrate_abort(struct got_worktree *worktree,
7299 struct got_fileindex *fileindex, struct got_repository *repo,
7300 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7302 const struct got_error *err = NULL, *unlockerr = NULL;
7304 got_fileindex_free(fileindex);
7306 err = lock_worktree(worktree, LOCK_SH);
7308 unlockerr = got_ref_unlock(branch_ref);
7309 if (unlockerr && err == NULL)
7310 err = unlockerr;
7311 got_ref_close(branch_ref);
7313 unlockerr = got_ref_unlock(base_branch_ref);
7314 if (unlockerr && err == NULL)
7315 err = unlockerr;
7316 got_ref_close(base_branch_ref);
7318 return err;
7321 struct check_stage_ok_arg {
7322 struct got_object_id *head_commit_id;
7323 struct got_worktree *worktree;
7324 struct got_fileindex *fileindex;
7325 struct got_repository *repo;
7326 int have_changes;
7329 const struct got_error *
7330 check_stage_ok(void *arg, unsigned char status,
7331 unsigned char staged_status, const char *relpath,
7332 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7333 struct got_object_id *commit_id, int dirfd, const char *de_name)
7335 struct check_stage_ok_arg *a = arg;
7336 const struct got_error *err = NULL;
7337 struct got_fileindex_entry *ie;
7338 struct got_object_id base_commit_id;
7339 struct got_object_id *base_commit_idp = NULL;
7340 char *in_repo_path = NULL, *p;
7342 if (status == GOT_STATUS_UNVERSIONED ||
7343 status == GOT_STATUS_NO_CHANGE)
7344 return NULL;
7345 if (status == GOT_STATUS_NONEXISTENT)
7346 return got_error_set_errno(ENOENT, relpath);
7348 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7349 if (ie == NULL)
7350 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7352 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7353 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7354 relpath) == -1)
7355 return got_error_from_errno("asprintf");
7357 if (got_fileindex_entry_has_commit(ie)) {
7358 memcpy(base_commit_id.sha1, ie->commit_sha1,
7359 SHA1_DIGEST_LENGTH);
7360 base_commit_idp = &base_commit_id;
7363 if (status == GOT_STATUS_CONFLICT) {
7364 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7365 goto done;
7366 } else if (status != GOT_STATUS_ADD &&
7367 status != GOT_STATUS_MODIFY &&
7368 status != GOT_STATUS_DELETE) {
7369 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7370 goto done;
7373 a->have_changes = 1;
7375 p = in_repo_path;
7376 while (p[0] == '/')
7377 p++;
7378 err = check_out_of_date(p, status, staged_status,
7379 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7380 GOT_ERR_STAGE_OUT_OF_DATE);
7381 done:
7382 free(in_repo_path);
7383 return err;
7386 struct stage_path_arg {
7387 struct got_worktree *worktree;
7388 struct got_fileindex *fileindex;
7389 struct got_repository *repo;
7390 got_worktree_status_cb status_cb;
7391 void *status_arg;
7392 got_worktree_patch_cb patch_cb;
7393 void *patch_arg;
7394 int staged_something;
7395 int allow_bad_symlinks;
7398 static const struct got_error *
7399 stage_path(void *arg, unsigned char status,
7400 unsigned char staged_status, const char *relpath,
7401 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7402 struct got_object_id *commit_id, int dirfd, const char *de_name)
7404 struct stage_path_arg *a = arg;
7405 const struct got_error *err = NULL;
7406 struct got_fileindex_entry *ie;
7407 char *ondisk_path = NULL, *path_content = NULL;
7408 uint32_t stage;
7409 struct got_object_id *new_staged_blob_id = NULL;
7410 struct stat sb;
7412 if (status == GOT_STATUS_UNVERSIONED)
7413 return NULL;
7415 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7416 if (ie == NULL)
7417 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7419 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7420 relpath)== -1)
7421 return got_error_from_errno("asprintf");
7423 switch (status) {
7424 case GOT_STATUS_ADD:
7425 case GOT_STATUS_MODIFY:
7426 /* XXX could sb.st_mode be passed in by our caller? */
7427 if (lstat(ondisk_path, &sb) == -1) {
7428 err = got_error_from_errno2("lstat", ondisk_path);
7429 break;
7431 if (a->patch_cb) {
7432 if (status == GOT_STATUS_ADD) {
7433 int choice = GOT_PATCH_CHOICE_NONE;
7434 err = (*a->patch_cb)(&choice, a->patch_arg,
7435 status, ie->path, NULL, 1, 1);
7436 if (err)
7437 break;
7438 if (choice != GOT_PATCH_CHOICE_YES)
7439 break;
7440 } else {
7441 err = create_patched_content(&path_content, 0,
7442 staged_blob_id ? staged_blob_id : blob_id,
7443 ondisk_path, dirfd, de_name, ie->path,
7444 a->repo, a->patch_cb, a->patch_arg);
7445 if (err || path_content == NULL)
7446 break;
7449 err = got_object_blob_create(&new_staged_blob_id,
7450 path_content ? path_content : ondisk_path, a->repo);
7451 if (err)
7452 break;
7453 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7454 SHA1_DIGEST_LENGTH);
7455 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7456 stage = GOT_FILEIDX_STAGE_ADD;
7457 else
7458 stage = GOT_FILEIDX_STAGE_MODIFY;
7459 got_fileindex_entry_stage_set(ie, stage);
7460 if (S_ISLNK(sb.st_mode)) {
7461 int is_bad_symlink = 0;
7462 if (!a->allow_bad_symlinks) {
7463 char target_path[PATH_MAX];
7464 ssize_t target_len;
7465 target_len = readlink(ondisk_path, target_path,
7466 sizeof(target_path));
7467 if (target_len == -1) {
7468 err = got_error_from_errno2("readlink",
7469 ondisk_path);
7470 break;
7472 err = is_bad_symlink_target(&is_bad_symlink,
7473 target_path, target_len, ondisk_path,
7474 a->worktree->root_path);
7475 if (err)
7476 break;
7477 if (is_bad_symlink) {
7478 err = got_error_path(ondisk_path,
7479 GOT_ERR_BAD_SYMLINK);
7480 break;
7483 if (is_bad_symlink)
7484 got_fileindex_entry_staged_filetype_set(ie,
7485 GOT_FILEIDX_MODE_BAD_SYMLINK);
7486 else
7487 got_fileindex_entry_staged_filetype_set(ie,
7488 GOT_FILEIDX_MODE_SYMLINK);
7489 } else {
7490 got_fileindex_entry_staged_filetype_set(ie,
7491 GOT_FILEIDX_MODE_REGULAR_FILE);
7493 a->staged_something = 1;
7494 if (a->status_cb == NULL)
7495 break;
7496 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7497 get_staged_status(ie), relpath, blob_id,
7498 new_staged_blob_id, NULL, dirfd, de_name);
7499 break;
7500 case GOT_STATUS_DELETE:
7501 if (staged_status == GOT_STATUS_DELETE)
7502 break;
7503 if (a->patch_cb) {
7504 int choice = GOT_PATCH_CHOICE_NONE;
7505 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7506 ie->path, NULL, 1, 1);
7507 if (err)
7508 break;
7509 if (choice == GOT_PATCH_CHOICE_NO)
7510 break;
7511 if (choice != GOT_PATCH_CHOICE_YES) {
7512 err = got_error(GOT_ERR_PATCH_CHOICE);
7513 break;
7516 stage = GOT_FILEIDX_STAGE_DELETE;
7517 got_fileindex_entry_stage_set(ie, stage);
7518 a->staged_something = 1;
7519 if (a->status_cb == NULL)
7520 break;
7521 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7522 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7523 de_name);
7524 break;
7525 case GOT_STATUS_NO_CHANGE:
7526 break;
7527 case GOT_STATUS_CONFLICT:
7528 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7529 break;
7530 case GOT_STATUS_NONEXISTENT:
7531 err = got_error_set_errno(ENOENT, relpath);
7532 break;
7533 default:
7534 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7535 break;
7538 if (path_content && unlink(path_content) == -1 && err == NULL)
7539 err = got_error_from_errno2("unlink", path_content);
7540 free(path_content);
7541 free(ondisk_path);
7542 free(new_staged_blob_id);
7543 return err;
7546 const struct got_error *
7547 got_worktree_stage(struct got_worktree *worktree,
7548 struct got_pathlist_head *paths,
7549 got_worktree_status_cb status_cb, void *status_arg,
7550 got_worktree_patch_cb patch_cb, void *patch_arg,
7551 int allow_bad_symlinks, struct got_repository *repo)
7553 const struct got_error *err = NULL, *sync_err, *unlockerr;
7554 struct got_pathlist_entry *pe;
7555 struct got_fileindex *fileindex = NULL;
7556 char *fileindex_path = NULL;
7557 struct got_reference *head_ref = NULL;
7558 struct got_object_id *head_commit_id = NULL;
7559 struct check_stage_ok_arg oka;
7560 struct stage_path_arg spa;
7562 err = lock_worktree(worktree, LOCK_EX);
7563 if (err)
7564 return err;
7566 err = got_ref_open(&head_ref, repo,
7567 got_worktree_get_head_ref_name(worktree), 0);
7568 if (err)
7569 goto done;
7570 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7571 if (err)
7572 goto done;
7573 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7574 if (err)
7575 goto done;
7577 /* Check pre-conditions before staging anything. */
7578 oka.head_commit_id = head_commit_id;
7579 oka.worktree = worktree;
7580 oka.fileindex = fileindex;
7581 oka.repo = repo;
7582 oka.have_changes = 0;
7583 TAILQ_FOREACH(pe, paths, entry) {
7584 err = worktree_status(worktree, pe->path, fileindex, repo,
7585 check_stage_ok, &oka, NULL, NULL, 0, 0);
7586 if (err)
7587 goto done;
7589 if (!oka.have_changes) {
7590 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7591 goto done;
7594 spa.worktree = worktree;
7595 spa.fileindex = fileindex;
7596 spa.repo = repo;
7597 spa.patch_cb = patch_cb;
7598 spa.patch_arg = patch_arg;
7599 spa.status_cb = status_cb;
7600 spa.status_arg = status_arg;
7601 spa.staged_something = 0;
7602 spa.allow_bad_symlinks = allow_bad_symlinks;
7603 TAILQ_FOREACH(pe, paths, entry) {
7604 err = worktree_status(worktree, pe->path, fileindex, repo,
7605 stage_path, &spa, NULL, NULL, 0, 0);
7606 if (err)
7607 goto done;
7609 if (!spa.staged_something) {
7610 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7611 goto done;
7614 sync_err = sync_fileindex(fileindex, fileindex_path);
7615 if (sync_err && err == NULL)
7616 err = sync_err;
7617 done:
7618 if (head_ref)
7619 got_ref_close(head_ref);
7620 free(head_commit_id);
7621 free(fileindex_path);
7622 if (fileindex)
7623 got_fileindex_free(fileindex);
7624 unlockerr = lock_worktree(worktree, LOCK_SH);
7625 if (unlockerr && err == NULL)
7626 err = unlockerr;
7627 return err;
7630 struct unstage_path_arg {
7631 struct got_worktree *worktree;
7632 struct got_fileindex *fileindex;
7633 struct got_repository *repo;
7634 got_worktree_checkout_cb progress_cb;
7635 void *progress_arg;
7636 got_worktree_patch_cb patch_cb;
7637 void *patch_arg;
7640 static const struct got_error *
7641 create_unstaged_content(char **path_unstaged_content,
7642 char **path_new_staged_content, struct got_object_id *blob_id,
7643 struct got_object_id *staged_blob_id, const char *relpath,
7644 struct got_repository *repo,
7645 got_worktree_patch_cb patch_cb, void *patch_arg)
7647 const struct got_error *err, *free_err;
7648 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7649 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7650 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7651 struct got_diffreg_result *diffreg_result = NULL;
7652 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7653 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7655 *path_unstaged_content = NULL;
7656 *path_new_staged_content = NULL;
7658 err = got_object_id_str(&label1, blob_id);
7659 if (err)
7660 return err;
7661 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7662 if (err)
7663 goto done;
7665 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7666 if (err)
7667 goto done;
7669 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7670 if (err)
7671 goto done;
7673 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7674 if (err)
7675 goto done;
7677 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7678 if (err)
7679 goto done;
7681 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7682 if (err)
7683 goto done;
7685 err = got_diff_files(&diffreg_result, f1, label1, f2,
7686 path2, 3, 0, 1, NULL);
7687 if (err)
7688 goto done;
7690 err = got_opentemp_named(path_unstaged_content, &outfile,
7691 "got-unstaged-content");
7692 if (err)
7693 goto done;
7694 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7695 "got-new-staged-content");
7696 if (err)
7697 goto done;
7699 if (fseek(f1, 0L, SEEK_SET) == -1) {
7700 err = got_ferror(f1, GOT_ERR_IO);
7701 goto done;
7703 if (fseek(f2, 0L, SEEK_SET) == -1) {
7704 err = got_ferror(f2, GOT_ERR_IO);
7705 goto done;
7707 /* Count the number of actual changes in the diff result. */
7708 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7709 struct diff_chunk_context cc = {};
7710 diff_chunk_context_load_change(&cc, &nchunks_used,
7711 diffreg_result->result, n, 0);
7712 nchanges++;
7714 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7715 int choice;
7716 err = apply_or_reject_change(&choice, &nchunks_used,
7717 diffreg_result->result, n, relpath, f1, f2,
7718 &line_cur1, &line_cur2,
7719 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7720 if (err)
7721 goto done;
7722 if (choice == GOT_PATCH_CHOICE_YES)
7723 have_content = 1;
7724 else
7725 have_rejected_content = 1;
7726 if (choice == GOT_PATCH_CHOICE_QUIT)
7727 break;
7729 if (have_content || have_rejected_content)
7730 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7731 outfile, rejectfile);
7732 done:
7733 free(label1);
7734 if (blob)
7735 got_object_blob_close(blob);
7736 if (staged_blob)
7737 got_object_blob_close(staged_blob);
7738 free_err = got_diffreg_result_free(diffreg_result);
7739 if (free_err && err == NULL)
7740 err = free_err;
7741 if (f1 && fclose(f1) == EOF && err == NULL)
7742 err = got_error_from_errno2("fclose", path1);
7743 if (f2 && fclose(f2) == EOF && err == NULL)
7744 err = got_error_from_errno2("fclose", path2);
7745 if (outfile && fclose(outfile) == EOF && err == NULL)
7746 err = got_error_from_errno2("fclose", *path_unstaged_content);
7747 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7748 err = got_error_from_errno2("fclose", *path_new_staged_content);
7749 if (path1 && unlink(path1) == -1 && err == NULL)
7750 err = got_error_from_errno2("unlink", path1);
7751 if (path2 && unlink(path2) == -1 && err == NULL)
7752 err = got_error_from_errno2("unlink", path2);
7753 if (err || !have_content) {
7754 if (*path_unstaged_content &&
7755 unlink(*path_unstaged_content) == -1 && err == NULL)
7756 err = got_error_from_errno2("unlink",
7757 *path_unstaged_content);
7758 free(*path_unstaged_content);
7759 *path_unstaged_content = NULL;
7761 if (err || !have_content || !have_rejected_content) {
7762 if (*path_new_staged_content &&
7763 unlink(*path_new_staged_content) == -1 && err == NULL)
7764 err = got_error_from_errno2("unlink",
7765 *path_new_staged_content);
7766 free(*path_new_staged_content);
7767 *path_new_staged_content = NULL;
7769 free(path1);
7770 free(path2);
7771 return err;
7774 static const struct got_error *
7775 unstage_hunks(struct got_object_id *staged_blob_id,
7776 struct got_blob_object *blob_base,
7777 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7778 const char *ondisk_path, const char *label_orig,
7779 struct got_worktree *worktree, struct got_repository *repo,
7780 got_worktree_patch_cb patch_cb, void *patch_arg,
7781 got_worktree_checkout_cb progress_cb, void *progress_arg)
7783 const struct got_error *err = NULL;
7784 char *path_unstaged_content = NULL;
7785 char *path_new_staged_content = NULL;
7786 char *parent = NULL, *base_path = NULL;
7787 char *blob_base_path = NULL;
7788 struct got_object_id *new_staged_blob_id = NULL;
7789 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
7790 struct stat sb;
7792 err = create_unstaged_content(&path_unstaged_content,
7793 &path_new_staged_content, blob_id, staged_blob_id,
7794 ie->path, repo, patch_cb, patch_arg);
7795 if (err)
7796 return err;
7798 if (path_unstaged_content == NULL)
7799 return NULL;
7801 if (path_new_staged_content) {
7802 err = got_object_blob_create(&new_staged_blob_id,
7803 path_new_staged_content, repo);
7804 if (err)
7805 goto done;
7808 f = fopen(path_unstaged_content, "r");
7809 if (f == NULL) {
7810 err = got_error_from_errno2("fopen",
7811 path_unstaged_content);
7812 goto done;
7814 if (fstat(fileno(f), &sb) == -1) {
7815 err = got_error_from_errno2("fstat", path_unstaged_content);
7816 goto done;
7818 if (got_fileindex_entry_staged_filetype_get(ie) ==
7819 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7820 char link_target[PATH_MAX];
7821 size_t r;
7822 r = fread(link_target, 1, sizeof(link_target), f);
7823 if (r == 0 && ferror(f)) {
7824 err = got_error_from_errno("fread");
7825 goto done;
7827 if (r >= sizeof(link_target)) { /* should not happen */
7828 err = got_error(GOT_ERR_NO_SPACE);
7829 goto done;
7831 link_target[r] = '\0';
7832 err = merge_symlink(worktree, blob_base,
7833 ondisk_path, ie->path, label_orig, link_target,
7834 worktree->base_commit_id, repo, progress_cb,
7835 progress_arg);
7836 } else {
7837 int local_changes_subsumed;
7839 err = got_path_dirname(&parent, ondisk_path);
7840 if (err)
7841 return err;
7843 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
7844 parent) == -1) {
7845 err = got_error_from_errno("asprintf");
7846 base_path = NULL;
7847 goto done;
7850 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
7851 if (err)
7852 goto done;
7853 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
7854 blob_base);
7855 if (err)
7856 goto done;
7859 * In order the run a 3-way merge with a symlink we copy the symlink's
7860 * target path into a temporary file and use that file with diff3.
7862 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7863 err = dump_symlink_target_path_to_file(&f_deriv2,
7864 ondisk_path);
7865 if (err)
7866 goto done;
7867 } else {
7868 int fd;
7869 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
7870 if (fd == -1) {
7871 err = got_error_from_errno2("open", ondisk_path);
7872 goto done;
7874 f_deriv2 = fdopen(fd, "r");
7875 if (f_deriv2 == NULL) {
7876 err = got_error_from_errno2("fdopen", ondisk_path);
7877 close(fd);
7878 goto done;
7882 err = merge_file(&local_changes_subsumed, worktree,
7883 f_base, f, f_deriv2, ondisk_path, ie->path,
7884 got_fileindex_perms_to_st(ie),
7885 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
7886 repo, progress_cb, progress_arg);
7888 if (err)
7889 goto done;
7891 if (new_staged_blob_id) {
7892 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7893 SHA1_DIGEST_LENGTH);
7894 } else {
7895 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7896 got_fileindex_entry_staged_filetype_set(ie, 0);
7898 done:
7899 free(new_staged_blob_id);
7900 if (path_unstaged_content &&
7901 unlink(path_unstaged_content) == -1 && err == NULL)
7902 err = got_error_from_errno2("unlink", path_unstaged_content);
7903 if (path_new_staged_content &&
7904 unlink(path_new_staged_content) == -1 && err == NULL)
7905 err = got_error_from_errno2("unlink", path_new_staged_content);
7906 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
7907 err = got_error_from_errno2("unlink", blob_base_path);
7908 if (f_base && fclose(f_base) == EOF && err == NULL)
7909 err = got_error_from_errno2("fclose", path_unstaged_content);
7910 if (f && fclose(f) == EOF && err == NULL)
7911 err = got_error_from_errno2("fclose", path_unstaged_content);
7912 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
7913 err = got_error_from_errno2("fclose", ondisk_path);
7914 free(path_unstaged_content);
7915 free(path_new_staged_content);
7916 free(blob_base_path);
7917 free(parent);
7918 free(base_path);
7919 return err;
7922 static const struct got_error *
7923 unstage_path(void *arg, unsigned char status,
7924 unsigned char staged_status, const char *relpath,
7925 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7926 struct got_object_id *commit_id, int dirfd, const char *de_name)
7928 const struct got_error *err = NULL;
7929 struct unstage_path_arg *a = arg;
7930 struct got_fileindex_entry *ie;
7931 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7932 char *ondisk_path = NULL;
7933 char *id_str = NULL, *label_orig = NULL;
7934 int local_changes_subsumed;
7935 struct stat sb;
7937 if (staged_status != GOT_STATUS_ADD &&
7938 staged_status != GOT_STATUS_MODIFY &&
7939 staged_status != GOT_STATUS_DELETE)
7940 return NULL;
7942 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7943 if (ie == NULL)
7944 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7946 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7947 == -1)
7948 return got_error_from_errno("asprintf");
7950 err = got_object_id_str(&id_str,
7951 commit_id ? commit_id : a->worktree->base_commit_id);
7952 if (err)
7953 goto done;
7954 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7955 id_str) == -1) {
7956 err = got_error_from_errno("asprintf");
7957 goto done;
7960 switch (staged_status) {
7961 case GOT_STATUS_MODIFY:
7962 err = got_object_open_as_blob(&blob_base, a->repo,
7963 blob_id, 8192);
7964 if (err)
7965 break;
7966 /* fall through */
7967 case GOT_STATUS_ADD:
7968 if (a->patch_cb) {
7969 if (staged_status == GOT_STATUS_ADD) {
7970 int choice = GOT_PATCH_CHOICE_NONE;
7971 err = (*a->patch_cb)(&choice, a->patch_arg,
7972 staged_status, ie->path, NULL, 1, 1);
7973 if (err)
7974 break;
7975 if (choice != GOT_PATCH_CHOICE_YES)
7976 break;
7977 } else {
7978 err = unstage_hunks(staged_blob_id,
7979 blob_base, blob_id, ie, ondisk_path,
7980 label_orig, a->worktree, a->repo,
7981 a->patch_cb, a->patch_arg,
7982 a->progress_cb, a->progress_arg);
7983 break; /* Done with this file. */
7986 err = got_object_open_as_blob(&blob_staged, a->repo,
7987 staged_blob_id, 8192);
7988 if (err)
7989 break;
7990 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7991 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7992 case GOT_FILEIDX_MODE_REGULAR_FILE:
7993 err = merge_blob(&local_changes_subsumed, a->worktree,
7994 blob_base, ondisk_path, relpath,
7995 got_fileindex_perms_to_st(ie), label_orig,
7996 blob_staged, commit_id ? commit_id :
7997 a->worktree->base_commit_id, a->repo,
7998 a->progress_cb, a->progress_arg);
7999 break;
8000 case GOT_FILEIDX_MODE_SYMLINK:
8001 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8002 char *staged_target;
8003 err = got_object_blob_read_to_str(
8004 &staged_target, blob_staged);
8005 if (err)
8006 goto done;
8007 err = merge_symlink(a->worktree, blob_base,
8008 ondisk_path, relpath, label_orig,
8009 staged_target, commit_id ? commit_id :
8010 a->worktree->base_commit_id,
8011 a->repo, a->progress_cb, a->progress_arg);
8012 free(staged_target);
8013 } else {
8014 err = merge_blob(&local_changes_subsumed,
8015 a->worktree, blob_base, ondisk_path,
8016 relpath, got_fileindex_perms_to_st(ie),
8017 label_orig, blob_staged,
8018 commit_id ? commit_id :
8019 a->worktree->base_commit_id, a->repo,
8020 a->progress_cb, a->progress_arg);
8022 break;
8023 default:
8024 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8025 break;
8027 if (err == NULL) {
8028 got_fileindex_entry_stage_set(ie,
8029 GOT_FILEIDX_STAGE_NONE);
8030 got_fileindex_entry_staged_filetype_set(ie, 0);
8032 break;
8033 case GOT_STATUS_DELETE:
8034 if (a->patch_cb) {
8035 int choice = GOT_PATCH_CHOICE_NONE;
8036 err = (*a->patch_cb)(&choice, a->patch_arg,
8037 staged_status, ie->path, NULL, 1, 1);
8038 if (err)
8039 break;
8040 if (choice == GOT_PATCH_CHOICE_NO)
8041 break;
8042 if (choice != GOT_PATCH_CHOICE_YES) {
8043 err = got_error(GOT_ERR_PATCH_CHOICE);
8044 break;
8047 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8048 got_fileindex_entry_staged_filetype_set(ie, 0);
8049 err = get_file_status(&status, &sb, ie, ondisk_path,
8050 dirfd, de_name, a->repo);
8051 if (err)
8052 break;
8053 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8054 break;
8056 done:
8057 free(ondisk_path);
8058 if (blob_base)
8059 got_object_blob_close(blob_base);
8060 if (blob_staged)
8061 got_object_blob_close(blob_staged);
8062 free(id_str);
8063 free(label_orig);
8064 return err;
8067 const struct got_error *
8068 got_worktree_unstage(struct got_worktree *worktree,
8069 struct got_pathlist_head *paths,
8070 got_worktree_checkout_cb progress_cb, void *progress_arg,
8071 got_worktree_patch_cb patch_cb, void *patch_arg,
8072 struct got_repository *repo)
8074 const struct got_error *err = NULL, *sync_err, *unlockerr;
8075 struct got_pathlist_entry *pe;
8076 struct got_fileindex *fileindex = NULL;
8077 char *fileindex_path = NULL;
8078 struct unstage_path_arg upa;
8080 err = lock_worktree(worktree, LOCK_EX);
8081 if (err)
8082 return err;
8084 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8085 if (err)
8086 goto done;
8088 upa.worktree = worktree;
8089 upa.fileindex = fileindex;
8090 upa.repo = repo;
8091 upa.progress_cb = progress_cb;
8092 upa.progress_arg = progress_arg;
8093 upa.patch_cb = patch_cb;
8094 upa.patch_arg = patch_arg;
8095 TAILQ_FOREACH(pe, paths, entry) {
8096 err = worktree_status(worktree, pe->path, fileindex, repo,
8097 unstage_path, &upa, NULL, NULL, 0, 0);
8098 if (err)
8099 goto done;
8102 sync_err = sync_fileindex(fileindex, fileindex_path);
8103 if (sync_err && err == NULL)
8104 err = sync_err;
8105 done:
8106 free(fileindex_path);
8107 if (fileindex)
8108 got_fileindex_free(fileindex);
8109 unlockerr = lock_worktree(worktree, LOCK_SH);
8110 if (unlockerr && err == NULL)
8111 err = unlockerr;
8112 return err;
8115 struct report_file_info_arg {
8116 struct got_worktree *worktree;
8117 got_worktree_path_info_cb info_cb;
8118 void *info_arg;
8119 struct got_pathlist_head *paths;
8120 got_cancel_cb cancel_cb;
8121 void *cancel_arg;
8124 static const struct got_error *
8125 report_file_info(void *arg, struct got_fileindex_entry *ie)
8127 struct report_file_info_arg *a = arg;
8128 struct got_pathlist_entry *pe;
8129 struct got_object_id blob_id, staged_blob_id, commit_id;
8130 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8131 struct got_object_id *commit_idp = NULL;
8132 int stage;
8134 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8135 return got_error(GOT_ERR_CANCELLED);
8137 TAILQ_FOREACH(pe, a->paths, entry) {
8138 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8139 got_path_is_child(ie->path, pe->path, pe->path_len))
8140 break;
8142 if (pe == NULL) /* not found */
8143 return NULL;
8145 if (got_fileindex_entry_has_blob(ie)) {
8146 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8147 blob_idp = &blob_id;
8149 stage = got_fileindex_entry_stage_get(ie);
8150 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8151 stage == GOT_FILEIDX_STAGE_ADD) {
8152 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8153 SHA1_DIGEST_LENGTH);
8154 staged_blob_idp = &staged_blob_id;
8157 if (got_fileindex_entry_has_commit(ie)) {
8158 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8159 commit_idp = &commit_id;
8162 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8163 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8166 const struct got_error *
8167 got_worktree_path_info(struct got_worktree *worktree,
8168 struct got_pathlist_head *paths,
8169 got_worktree_path_info_cb info_cb, void *info_arg,
8170 got_cancel_cb cancel_cb, void *cancel_arg)
8173 const struct got_error *err = NULL, *unlockerr;
8174 struct got_fileindex *fileindex = NULL;
8175 char *fileindex_path = NULL;
8176 struct report_file_info_arg arg;
8178 err = lock_worktree(worktree, LOCK_SH);
8179 if (err)
8180 return err;
8182 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8183 if (err)
8184 goto done;
8186 arg.worktree = worktree;
8187 arg.info_cb = info_cb;
8188 arg.info_arg = info_arg;
8189 arg.paths = paths;
8190 arg.cancel_cb = cancel_cb;
8191 arg.cancel_arg = cancel_arg;
8192 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8193 &arg);
8194 done:
8195 free(fileindex_path);
8196 if (fileindex)
8197 got_fileindex_free(fileindex);
8198 unlockerr = lock_worktree(worktree, LOCK_UN);
8199 if (unlockerr && err == NULL)
8200 err = unlockerr;
8201 return err;