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;
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 do {
2118 char *parent;
2119 err = got_path_dirname(&parent, ondisk_path);
2120 if (err)
2121 break;
2122 free(ondisk_path);
2123 ondisk_path = parent;
2124 if (rmdir(ondisk_path) == -1) {
2125 if (errno != ENOTEMPTY)
2126 err = got_error_from_errno2("rmdir",
2127 ondisk_path);
2128 break;
2130 } while (got_path_cmp(ondisk_path, root_path,
2131 strlen(ondisk_path), root_len) != 0);
2133 free(ondisk_path);
2134 return err;
2137 static const struct got_error *
2138 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2139 struct got_fileindex_entry *ie, struct got_repository *repo,
2140 got_worktree_checkout_cb progress_cb, void *progress_arg)
2142 const struct got_error *err = NULL;
2143 unsigned char status;
2144 struct stat sb;
2145 char *ondisk_path;
2147 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2148 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2150 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2151 == -1)
2152 return got_error_from_errno("asprintf");
2154 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2155 if (err)
2156 goto done;
2158 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2159 char ondisk_target[PATH_MAX];
2160 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2161 sizeof(ondisk_target));
2162 if (ondisk_len == -1) {
2163 err = got_error_from_errno2("readlink", ondisk_path);
2164 goto done;
2166 ondisk_target[ondisk_len] = '\0';
2167 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2168 NULL, NULL, /* XXX pass common ancestor info? */
2169 ondisk_target, ondisk_path);
2170 if (err)
2171 goto done;
2172 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2173 ie->path);
2174 goto done;
2177 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2178 status == GOT_STATUS_ADD) {
2179 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2180 if (err)
2181 goto done;
2183 * Preserve the working file and change the deleted blob's
2184 * entry into a schedule-add entry.
2186 err = got_fileindex_entry_update(ie, worktree->root_fd,
2187 ie->path, NULL, NULL, 0);
2188 } else {
2189 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2190 if (err)
2191 goto done;
2192 if (status == GOT_STATUS_NO_CHANGE) {
2193 err = remove_ondisk_file(worktree->root_path, ie->path);
2194 if (err)
2195 goto done;
2197 got_fileindex_entry_remove(fileindex, ie);
2199 done:
2200 free(ondisk_path);
2201 return err;
2204 struct diff_cb_arg {
2205 struct got_fileindex *fileindex;
2206 struct got_worktree *worktree;
2207 struct got_repository *repo;
2208 got_worktree_checkout_cb progress_cb;
2209 void *progress_arg;
2210 got_cancel_cb cancel_cb;
2211 void *cancel_arg;
2214 static const struct got_error *
2215 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2216 struct got_tree_entry *te, const char *parent_path)
2218 struct diff_cb_arg *a = arg;
2220 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2221 return got_error(GOT_ERR_CANCELLED);
2223 return update_blob(a->worktree, a->fileindex, ie, te,
2224 ie->path, a->repo, a->progress_cb, a->progress_arg);
2227 static const struct got_error *
2228 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2230 struct diff_cb_arg *a = arg;
2232 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2233 return got_error(GOT_ERR_CANCELLED);
2235 return delete_blob(a->worktree, a->fileindex, ie,
2236 a->repo, a->progress_cb, a->progress_arg);
2239 static const struct got_error *
2240 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2242 struct diff_cb_arg *a = arg;
2243 const struct got_error *err;
2244 char *path;
2246 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2247 return got_error(GOT_ERR_CANCELLED);
2249 if (got_object_tree_entry_is_submodule(te))
2250 return NULL;
2252 if (asprintf(&path, "%s%s%s", parent_path,
2253 parent_path[0] ? "/" : "", te->name)
2254 == -1)
2255 return got_error_from_errno("asprintf");
2257 if (S_ISDIR(te->mode))
2258 err = add_dir_on_disk(a->worktree, path);
2259 else
2260 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2261 a->repo, a->progress_cb, a->progress_arg);
2263 free(path);
2264 return err;
2267 const struct got_error *
2268 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2270 uint32_t uuid_status;
2272 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2273 if (uuid_status != uuid_s_ok) {
2274 *uuidstr = NULL;
2275 return got_error_uuid(uuid_status, "uuid_to_string");
2278 return NULL;
2281 static const struct got_error *
2282 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2284 const struct got_error *err = NULL;
2285 char *uuidstr = NULL;
2287 *refname = NULL;
2289 err = got_worktree_get_uuid(&uuidstr, worktree);
2290 if (err)
2291 return err;
2293 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2294 err = got_error_from_errno("asprintf");
2295 *refname = NULL;
2297 free(uuidstr);
2298 return err;
2301 const struct got_error *
2302 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2304 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2307 static const struct got_error *
2308 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2310 return get_ref_name(refname, worktree,
2311 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2314 static const struct got_error *
2315 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2317 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2320 static const struct got_error *
2321 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2323 return get_ref_name(refname, worktree,
2324 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2327 static const struct got_error *
2328 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2330 return get_ref_name(refname, worktree,
2331 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2334 static const struct got_error *
2335 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2337 return get_ref_name(refname, worktree,
2338 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2341 static const struct got_error *
2342 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2344 return get_ref_name(refname, worktree,
2345 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2348 static const struct got_error *
2349 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2351 return get_ref_name(refname, worktree,
2352 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2355 static const struct got_error *
2356 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2358 return get_ref_name(refname, worktree,
2359 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2362 const struct got_error *
2363 got_worktree_get_histedit_script_path(char **path,
2364 struct got_worktree *worktree)
2366 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2367 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2368 *path = NULL;
2369 return got_error_from_errno("asprintf");
2371 return NULL;
2375 * Prevent Git's garbage collector from deleting our base commit by
2376 * setting a reference to our base commit's ID.
2378 static const struct got_error *
2379 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2381 const struct got_error *err = NULL;
2382 struct got_reference *ref = NULL;
2383 char *refname;
2385 err = got_worktree_get_base_ref_name(&refname, worktree);
2386 if (err)
2387 return err;
2389 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2390 if (err)
2391 goto done;
2393 err = got_ref_write(ref, repo);
2394 done:
2395 free(refname);
2396 if (ref)
2397 got_ref_close(ref);
2398 return err;
2401 static const struct got_error *
2402 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2404 const struct got_error *err = NULL;
2406 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2407 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2408 err = got_error_from_errno("asprintf");
2409 *fileindex_path = NULL;
2411 return err;
2415 static const struct got_error *
2416 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2417 struct got_worktree *worktree)
2419 const struct got_error *err = NULL;
2420 FILE *index = NULL;
2422 *fileindex_path = NULL;
2423 *fileindex = got_fileindex_alloc();
2424 if (*fileindex == NULL)
2425 return got_error_from_errno("got_fileindex_alloc");
2427 err = get_fileindex_path(fileindex_path, worktree);
2428 if (err)
2429 goto done;
2431 index = fopen(*fileindex_path, "rb");
2432 if (index == NULL) {
2433 if (errno != ENOENT)
2434 err = got_error_from_errno2("fopen", *fileindex_path);
2435 } else {
2436 err = got_fileindex_read(*fileindex, index);
2437 if (fclose(index) == EOF && err == NULL)
2438 err = got_error_from_errno("fclose");
2440 done:
2441 if (err) {
2442 free(*fileindex_path);
2443 *fileindex_path = NULL;
2444 got_fileindex_free(*fileindex);
2445 *fileindex = NULL;
2447 return err;
2450 struct bump_base_commit_id_arg {
2451 struct got_object_id *base_commit_id;
2452 const char *path;
2453 size_t path_len;
2454 const char *entry_name;
2455 got_worktree_checkout_cb progress_cb;
2456 void *progress_arg;
2459 /* Bump base commit ID of all files within an updated part of the work tree. */
2460 static const struct got_error *
2461 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2463 const struct got_error *err;
2464 struct bump_base_commit_id_arg *a = arg;
2466 if (a->entry_name) {
2467 if (strcmp(ie->path, a->path) != 0)
2468 return NULL;
2469 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2470 return NULL;
2472 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2473 SHA1_DIGEST_LENGTH) == 0)
2474 return NULL;
2476 if (a->progress_cb) {
2477 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2478 ie->path);
2479 if (err)
2480 return err;
2482 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2483 return NULL;
2486 static const struct got_error *
2487 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2488 struct got_fileindex *fileindex,
2489 got_worktree_checkout_cb progress_cb, void *progress_arg)
2491 struct bump_base_commit_id_arg bbc_arg;
2493 bbc_arg.base_commit_id = worktree->base_commit_id;
2494 bbc_arg.entry_name = NULL;
2495 bbc_arg.path = "";
2496 bbc_arg.path_len = 0;
2497 bbc_arg.progress_cb = progress_cb;
2498 bbc_arg.progress_arg = progress_arg;
2500 return got_fileindex_for_each_entry_safe(fileindex,
2501 bump_base_commit_id, &bbc_arg);
2504 static const struct got_error *
2505 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2507 const struct got_error *err = NULL;
2508 char *new_fileindex_path = NULL;
2509 FILE *new_index = NULL;
2510 struct timespec timeout;
2512 err = got_opentemp_named(&new_fileindex_path, &new_index,
2513 fileindex_path);
2514 if (err)
2515 goto done;
2517 err = got_fileindex_write(fileindex, new_index);
2518 if (err)
2519 goto done;
2521 if (rename(new_fileindex_path, fileindex_path) != 0) {
2522 err = got_error_from_errno3("rename", new_fileindex_path,
2523 fileindex_path);
2524 unlink(new_fileindex_path);
2528 * Sleep for a short amount of time to ensure that files modified after
2529 * this program exits have a different time stamp from the one which
2530 * was recorded in the file index.
2532 timeout.tv_sec = 0;
2533 timeout.tv_nsec = 1;
2534 nanosleep(&timeout, NULL);
2535 done:
2536 if (new_index)
2537 fclose(new_index);
2538 free(new_fileindex_path);
2539 return err;
2542 static const struct got_error *
2543 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2544 struct got_object_id **tree_id, const char *wt_relpath,
2545 struct got_worktree *worktree, struct got_repository *repo)
2547 const struct got_error *err = NULL;
2548 struct got_object_id *id = NULL;
2549 char *in_repo_path = NULL;
2550 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2552 *entry_type = GOT_OBJ_TYPE_ANY;
2553 *tree_relpath = NULL;
2554 *tree_id = NULL;
2556 if (wt_relpath[0] == '\0') {
2557 /* Check out all files within the work tree. */
2558 *entry_type = GOT_OBJ_TYPE_TREE;
2559 *tree_relpath = strdup("");
2560 if (*tree_relpath == NULL) {
2561 err = got_error_from_errno("strdup");
2562 goto done;
2564 err = got_object_id_by_path(tree_id, repo,
2565 worktree->base_commit_id, worktree->path_prefix);
2566 if (err)
2567 goto done;
2568 return NULL;
2571 /* Check out a subset of files in the work tree. */
2573 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2574 is_root_wt ? "" : "/", wt_relpath) == -1) {
2575 err = got_error_from_errno("asprintf");
2576 goto done;
2579 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2580 in_repo_path);
2581 if (err)
2582 goto done;
2584 free(in_repo_path);
2585 in_repo_path = NULL;
2587 err = got_object_get_type(entry_type, repo, id);
2588 if (err)
2589 goto done;
2591 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2592 /* Check out a single file. */
2593 if (strchr(wt_relpath, '/') == NULL) {
2594 /* Check out a single file in work tree's root dir. */
2595 in_repo_path = strdup(worktree->path_prefix);
2596 if (in_repo_path == NULL) {
2597 err = got_error_from_errno("strdup");
2598 goto done;
2600 *tree_relpath = strdup("");
2601 if (*tree_relpath == NULL) {
2602 err = got_error_from_errno("strdup");
2603 goto done;
2605 } else {
2606 /* Check out a single file in a subdirectory. */
2607 err = got_path_dirname(tree_relpath, wt_relpath);
2608 if (err)
2609 return err;
2610 if (asprintf(&in_repo_path, "%s%s%s",
2611 worktree->path_prefix, is_root_wt ? "" : "/",
2612 *tree_relpath) == -1) {
2613 err = got_error_from_errno("asprintf");
2614 goto done;
2617 err = got_object_id_by_path(tree_id, repo,
2618 worktree->base_commit_id, in_repo_path);
2619 } else {
2620 /* Check out all files within a subdirectory. */
2621 *tree_id = got_object_id_dup(id);
2622 if (*tree_id == NULL) {
2623 err = got_error_from_errno("got_object_id_dup");
2624 goto done;
2626 *tree_relpath = strdup(wt_relpath);
2627 if (*tree_relpath == NULL) {
2628 err = got_error_from_errno("strdup");
2629 goto done;
2632 done:
2633 free(id);
2634 free(in_repo_path);
2635 if (err) {
2636 *entry_type = GOT_OBJ_TYPE_ANY;
2637 free(*tree_relpath);
2638 *tree_relpath = NULL;
2639 free(*tree_id);
2640 *tree_id = NULL;
2642 return err;
2645 static const struct got_error *
2646 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2647 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2648 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2649 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2651 const struct got_error *err = NULL;
2652 struct got_commit_object *commit = NULL;
2653 struct got_tree_object *tree = NULL;
2654 struct got_fileindex_diff_tree_cb diff_cb;
2655 struct diff_cb_arg arg;
2657 err = ref_base_commit(worktree, repo);
2658 if (err) {
2659 if (!(err->code == GOT_ERR_ERRNO &&
2660 (errno == EACCES || errno == EROFS)))
2661 goto done;
2662 err = (*progress_cb)(progress_arg,
2663 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2664 if (err)
2665 return err;
2668 err = got_object_open_as_commit(&commit, repo,
2669 worktree->base_commit_id);
2670 if (err)
2671 goto done;
2673 err = got_object_open_as_tree(&tree, repo, tree_id);
2674 if (err)
2675 goto done;
2677 if (entry_name &&
2678 got_object_tree_find_entry(tree, entry_name) == NULL) {
2679 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2680 goto done;
2683 diff_cb.diff_old_new = diff_old_new;
2684 diff_cb.diff_old = diff_old;
2685 diff_cb.diff_new = diff_new;
2686 arg.fileindex = fileindex;
2687 arg.worktree = worktree;
2688 arg.repo = repo;
2689 arg.progress_cb = progress_cb;
2690 arg.progress_arg = progress_arg;
2691 arg.cancel_cb = cancel_cb;
2692 arg.cancel_arg = cancel_arg;
2693 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2694 entry_name, repo, &diff_cb, &arg);
2695 done:
2696 if (tree)
2697 got_object_tree_close(tree);
2698 if (commit)
2699 got_object_commit_close(commit);
2700 return err;
2703 const struct got_error *
2704 got_worktree_checkout_files(struct got_worktree *worktree,
2705 struct got_pathlist_head *paths, struct got_repository *repo,
2706 got_worktree_checkout_cb progress_cb, void *progress_arg,
2707 got_cancel_cb cancel_cb, void *cancel_arg)
2709 const struct got_error *err = NULL, *sync_err, *unlockerr;
2710 struct got_commit_object *commit = NULL;
2711 struct got_tree_object *tree = NULL;
2712 struct got_fileindex *fileindex = NULL;
2713 char *fileindex_path = NULL;
2714 struct got_pathlist_entry *pe;
2715 struct tree_path_data {
2716 SIMPLEQ_ENTRY(tree_path_data) entry;
2717 struct got_object_id *tree_id;
2718 int entry_type;
2719 char *relpath;
2720 char *entry_name;
2721 } *tpd = NULL;
2722 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2724 SIMPLEQ_INIT(&tree_paths);
2726 err = lock_worktree(worktree, LOCK_EX);
2727 if (err)
2728 return err;
2730 /* Map all specified paths to in-repository trees. */
2731 TAILQ_FOREACH(pe, paths, entry) {
2732 tpd = malloc(sizeof(*tpd));
2733 if (tpd == NULL) {
2734 err = got_error_from_errno("malloc");
2735 goto done;
2738 err = find_tree_entry_for_checkout(&tpd->entry_type,
2739 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2740 if (err) {
2741 free(tpd);
2742 goto done;
2745 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2746 err = got_path_basename(&tpd->entry_name, pe->path);
2747 if (err) {
2748 free(tpd->relpath);
2749 free(tpd->tree_id);
2750 free(tpd);
2751 goto done;
2753 } else
2754 tpd->entry_name = NULL;
2756 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2760 * Read the file index.
2761 * Checking out files is supposed to be an idempotent operation.
2762 * If the on-disk file index is incomplete we will try to complete it.
2764 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2765 if (err)
2766 goto done;
2768 tpd = SIMPLEQ_FIRST(&tree_paths);
2769 TAILQ_FOREACH(pe, paths, entry) {
2770 struct bump_base_commit_id_arg bbc_arg;
2772 err = checkout_files(worktree, fileindex, tpd->relpath,
2773 tpd->tree_id, tpd->entry_name, repo,
2774 progress_cb, progress_arg, cancel_cb, cancel_arg);
2775 if (err)
2776 break;
2778 bbc_arg.base_commit_id = worktree->base_commit_id;
2779 bbc_arg.entry_name = tpd->entry_name;
2780 bbc_arg.path = pe->path;
2781 bbc_arg.path_len = pe->path_len;
2782 bbc_arg.progress_cb = progress_cb;
2783 bbc_arg.progress_arg = progress_arg;
2784 err = got_fileindex_for_each_entry_safe(fileindex,
2785 bump_base_commit_id, &bbc_arg);
2786 if (err)
2787 break;
2789 tpd = SIMPLEQ_NEXT(tpd, entry);
2791 sync_err = sync_fileindex(fileindex, fileindex_path);
2792 if (sync_err && err == NULL)
2793 err = sync_err;
2794 done:
2795 free(fileindex_path);
2796 if (tree)
2797 got_object_tree_close(tree);
2798 if (commit)
2799 got_object_commit_close(commit);
2800 if (fileindex)
2801 got_fileindex_free(fileindex);
2802 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2803 tpd = SIMPLEQ_FIRST(&tree_paths);
2804 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2805 free(tpd->relpath);
2806 free(tpd->tree_id);
2807 free(tpd);
2809 unlockerr = lock_worktree(worktree, LOCK_SH);
2810 if (unlockerr && err == NULL)
2811 err = unlockerr;
2812 return err;
2815 struct merge_file_cb_arg {
2816 struct got_worktree *worktree;
2817 struct got_fileindex *fileindex;
2818 got_worktree_checkout_cb progress_cb;
2819 void *progress_arg;
2820 got_cancel_cb cancel_cb;
2821 void *cancel_arg;
2822 const char *label_orig;
2823 struct got_object_id *commit_id2;
2826 static const struct got_error *
2827 merge_file_cb(void *arg, struct got_blob_object *blob1,
2828 struct got_blob_object *blob2, struct got_object_id *id1,
2829 struct got_object_id *id2, const char *path1, const char *path2,
2830 mode_t mode1, mode_t mode2, struct got_repository *repo)
2832 static const struct got_error *err = NULL;
2833 struct merge_file_cb_arg *a = arg;
2834 struct got_fileindex_entry *ie;
2835 char *ondisk_path = NULL;
2836 struct stat sb;
2837 unsigned char status;
2838 int local_changes_subsumed;
2839 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2840 char *id_str = NULL, *label_deriv2 = NULL;
2842 if (blob1 && blob2) {
2843 ie = got_fileindex_entry_get(a->fileindex, path2,
2844 strlen(path2));
2845 if (ie == NULL)
2846 return (*a->progress_cb)(a->progress_arg,
2847 GOT_STATUS_MISSING, path2);
2849 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2850 path2) == -1)
2851 return got_error_from_errno("asprintf");
2853 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2854 repo);
2855 if (err)
2856 goto done;
2858 if (status == GOT_STATUS_DELETE) {
2859 err = (*a->progress_cb)(a->progress_arg,
2860 GOT_STATUS_MERGE, path2);
2861 goto done;
2863 if (status != GOT_STATUS_NO_CHANGE &&
2864 status != GOT_STATUS_MODIFY &&
2865 status != GOT_STATUS_CONFLICT &&
2866 status != GOT_STATUS_ADD) {
2867 err = (*a->progress_cb)(a->progress_arg, status, path2);
2868 goto done;
2871 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2872 char *link_target2;
2873 err = got_object_blob_read_to_str(&link_target2, blob2);
2874 if (err)
2875 goto done;
2876 err = merge_symlink(a->worktree, blob1, ondisk_path,
2877 path2, a->label_orig, link_target2, a->commit_id2,
2878 repo, a->progress_cb, a->progress_arg);
2879 free(link_target2);
2880 } else {
2881 int fd;
2883 f_orig = got_opentemp();
2884 if (f_orig == NULL) {
2885 err = got_error_from_errno("got_opentemp");
2886 goto done;
2888 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2889 f_orig, blob1);
2890 if (err)
2891 goto done;
2893 f_deriv2 = got_opentemp();
2894 if (f_deriv2 == NULL)
2895 goto done;
2896 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2897 f_deriv2, blob2);
2898 if (err)
2899 goto done;
2901 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2902 if (fd == -1) {
2903 err = got_error_from_errno2("open",
2904 ondisk_path);
2905 goto done;
2907 f_deriv = fdopen(fd, "r");
2908 if (f_deriv == NULL) {
2909 err = got_error_from_errno2("fdopen",
2910 ondisk_path);
2911 close(fd);
2912 goto done;
2914 err = got_object_id_str(&id_str, a->commit_id2);
2915 if (err)
2916 goto done;
2917 if (asprintf(&label_deriv2, "%s: commit %s",
2918 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2919 err = got_error_from_errno("asprintf");
2920 goto done;
2922 err = merge_file(&local_changes_subsumed, a->worktree,
2923 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2924 sb.st_mode, a->label_orig, NULL, label_deriv2,
2925 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2926 a->progress_cb, a->progress_arg);
2928 } else if (blob1) {
2929 ie = got_fileindex_entry_get(a->fileindex, path1,
2930 strlen(path1));
2931 if (ie == NULL)
2932 return (*a->progress_cb)(a->progress_arg,
2933 GOT_STATUS_MISSING, path1);
2935 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2936 path1) == -1)
2937 return got_error_from_errno("asprintf");
2939 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2940 repo);
2941 if (err)
2942 goto done;
2944 switch (status) {
2945 case GOT_STATUS_NO_CHANGE:
2946 err = (*a->progress_cb)(a->progress_arg,
2947 GOT_STATUS_DELETE, path1);
2948 if (err)
2949 goto done;
2950 err = remove_ondisk_file(a->worktree->root_path, path1);
2951 if (err)
2952 goto done;
2953 if (ie)
2954 got_fileindex_entry_mark_deleted_from_disk(ie);
2955 break;
2956 case GOT_STATUS_DELETE:
2957 case GOT_STATUS_MISSING:
2958 err = (*a->progress_cb)(a->progress_arg,
2959 GOT_STATUS_DELETE, path1);
2960 if (err)
2961 goto done;
2962 if (ie)
2963 got_fileindex_entry_mark_deleted_from_disk(ie);
2964 break;
2965 case GOT_STATUS_ADD: {
2966 struct got_object_id *id;
2967 FILE *blob1_f;
2969 * Delete the added file only if its content already
2970 * exists in the repository.
2972 err = got_object_blob_file_create(&id, &blob1_f, path1);
2973 if (err)
2974 goto done;
2975 if (got_object_id_cmp(id, id1) == 0) {
2976 err = (*a->progress_cb)(a->progress_arg,
2977 GOT_STATUS_DELETE, path1);
2978 if (err)
2979 goto done;
2980 err = remove_ondisk_file(a->worktree->root_path,
2981 path1);
2982 if (err)
2983 goto done;
2984 if (ie)
2985 got_fileindex_entry_remove(a->fileindex,
2986 ie);
2987 } else {
2988 err = (*a->progress_cb)(a->progress_arg,
2989 GOT_STATUS_CANNOT_DELETE, path1);
2991 if (fclose(blob1_f) == EOF && err == NULL)
2992 err = got_error_from_errno("fclose");
2993 free(id);
2994 if (err)
2995 goto done;
2996 break;
2998 case GOT_STATUS_MODIFY:
2999 case GOT_STATUS_CONFLICT:
3000 err = (*a->progress_cb)(a->progress_arg,
3001 GOT_STATUS_CANNOT_DELETE, path1);
3002 if (err)
3003 goto done;
3004 break;
3005 case GOT_STATUS_OBSTRUCTED:
3006 err = (*a->progress_cb)(a->progress_arg, status, path1);
3007 if (err)
3008 goto done;
3009 break;
3010 default:
3011 break;
3013 } else if (blob2) {
3014 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3015 path2) == -1)
3016 return got_error_from_errno("asprintf");
3017 ie = got_fileindex_entry_get(a->fileindex, path2,
3018 strlen(path2));
3019 if (ie) {
3020 err = get_file_status(&status, &sb, ie, ondisk_path,
3021 -1, NULL, repo);
3022 if (err)
3023 goto done;
3024 if (status != GOT_STATUS_NO_CHANGE &&
3025 status != GOT_STATUS_MODIFY &&
3026 status != GOT_STATUS_CONFLICT &&
3027 status != GOT_STATUS_ADD) {
3028 err = (*a->progress_cb)(a->progress_arg,
3029 status, path2);
3030 goto done;
3032 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3033 char *link_target2;
3034 err = got_object_blob_read_to_str(&link_target2,
3035 blob2);
3036 if (err)
3037 goto done;
3038 err = merge_symlink(a->worktree, NULL,
3039 ondisk_path, path2, a->label_orig,
3040 link_target2, a->commit_id2, repo,
3041 a->progress_cb, a->progress_arg);
3042 free(link_target2);
3043 } else if (S_ISREG(sb.st_mode)) {
3044 err = merge_blob(&local_changes_subsumed,
3045 a->worktree, NULL, ondisk_path, path2,
3046 sb.st_mode, a->label_orig, blob2,
3047 a->commit_id2, repo, a->progress_cb,
3048 a->progress_arg);
3049 } else {
3050 err = got_error_path(ondisk_path,
3051 GOT_ERR_FILE_OBSTRUCTED);
3053 if (err)
3054 goto done;
3055 if (status == GOT_STATUS_DELETE) {
3056 err = got_fileindex_entry_update(ie,
3057 a->worktree->root_fd, path2, blob2->id.sha1,
3058 a->worktree->base_commit_id->sha1, 0);
3059 if (err)
3060 goto done;
3062 } else {
3063 int is_bad_symlink = 0;
3064 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3065 if (S_ISLNK(mode2)) {
3066 err = install_symlink(&is_bad_symlink,
3067 a->worktree, ondisk_path, path2, blob2, 0,
3068 0, 1, repo, a->progress_cb, a->progress_arg);
3069 } else {
3070 err = install_blob(a->worktree, ondisk_path, path2,
3071 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3072 a->progress_cb, a->progress_arg);
3074 if (err)
3075 goto done;
3076 err = got_fileindex_entry_alloc(&ie, path2);
3077 if (err)
3078 goto done;
3079 err = got_fileindex_entry_update(ie,
3080 a->worktree->root_fd, path2, NULL, NULL, 1);
3081 if (err) {
3082 got_fileindex_entry_free(ie);
3083 goto done;
3085 err = got_fileindex_entry_add(a->fileindex, ie);
3086 if (err) {
3087 got_fileindex_entry_free(ie);
3088 goto done;
3090 if (is_bad_symlink) {
3091 got_fileindex_entry_filetype_set(ie,
3092 GOT_FILEIDX_MODE_BAD_SYMLINK);
3096 done:
3097 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3098 err = got_error_from_errno("fclose");
3099 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3100 err = got_error_from_errno("fclose");
3101 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3102 err = got_error_from_errno("fclose");
3103 free(id_str);
3104 free(label_deriv2);
3105 free(ondisk_path);
3106 return err;
3109 struct check_merge_ok_arg {
3110 struct got_worktree *worktree;
3111 struct got_repository *repo;
3114 static const struct got_error *
3115 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3117 const struct got_error *err = NULL;
3118 struct check_merge_ok_arg *a = arg;
3119 unsigned char status;
3120 struct stat sb;
3121 char *ondisk_path;
3123 /* Reject merges into a work tree with mixed base commits. */
3124 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3125 SHA1_DIGEST_LENGTH))
3126 return got_error(GOT_ERR_MIXED_COMMITS);
3128 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3129 == -1)
3130 return got_error_from_errno("asprintf");
3132 /* Reject merges into a work tree with conflicted files. */
3133 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3134 if (err)
3135 return err;
3136 if (status == GOT_STATUS_CONFLICT)
3137 return got_error(GOT_ERR_CONFLICTS);
3139 return NULL;
3142 static const struct got_error *
3143 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3144 const char *fileindex_path, struct got_object_id *commit_id1,
3145 struct got_object_id *commit_id2, struct got_repository *repo,
3146 got_worktree_checkout_cb progress_cb, void *progress_arg,
3147 got_cancel_cb cancel_cb, void *cancel_arg)
3149 const struct got_error *err = NULL, *sync_err;
3150 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3151 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3152 struct merge_file_cb_arg arg;
3153 char *label_orig = NULL;
3155 if (commit_id1) {
3156 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3157 worktree->path_prefix);
3158 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3159 goto done;
3161 if (tree_id1) {
3162 char *id_str;
3164 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3165 if (err)
3166 goto done;
3168 err = got_object_id_str(&id_str, commit_id1);
3169 if (err)
3170 goto done;
3172 if (asprintf(&label_orig, "%s: commit %s",
3173 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3174 err = got_error_from_errno("asprintf");
3175 free(id_str);
3176 goto done;
3178 free(id_str);
3181 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3182 worktree->path_prefix);
3183 if (err)
3184 goto done;
3186 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3187 if (err)
3188 goto done;
3190 arg.worktree = worktree;
3191 arg.fileindex = fileindex;
3192 arg.progress_cb = progress_cb;
3193 arg.progress_arg = progress_arg;
3194 arg.cancel_cb = cancel_cb;
3195 arg.cancel_arg = cancel_arg;
3196 arg.label_orig = label_orig;
3197 arg.commit_id2 = commit_id2;
3198 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3199 sync_err = sync_fileindex(fileindex, fileindex_path);
3200 if (sync_err && err == NULL)
3201 err = sync_err;
3202 done:
3203 if (tree1)
3204 got_object_tree_close(tree1);
3205 if (tree2)
3206 got_object_tree_close(tree2);
3207 free(label_orig);
3208 return err;
3211 const struct got_error *
3212 got_worktree_merge_files(struct got_worktree *worktree,
3213 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3214 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3215 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3217 const struct got_error *err, *unlockerr;
3218 char *fileindex_path = NULL;
3219 struct got_fileindex *fileindex = NULL;
3220 struct check_merge_ok_arg mok_arg;
3222 err = lock_worktree(worktree, LOCK_EX);
3223 if (err)
3224 return err;
3226 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3227 if (err)
3228 goto done;
3230 mok_arg.worktree = worktree;
3231 mok_arg.repo = repo;
3232 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3233 &mok_arg);
3234 if (err)
3235 goto done;
3237 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3238 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3239 done:
3240 if (fileindex)
3241 got_fileindex_free(fileindex);
3242 free(fileindex_path);
3243 unlockerr = lock_worktree(worktree, LOCK_SH);
3244 if (unlockerr && err == NULL)
3245 err = unlockerr;
3246 return err;
3249 struct diff_dir_cb_arg {
3250 struct got_fileindex *fileindex;
3251 struct got_worktree *worktree;
3252 const char *status_path;
3253 size_t status_path_len;
3254 struct got_repository *repo;
3255 got_worktree_status_cb status_cb;
3256 void *status_arg;
3257 got_cancel_cb cancel_cb;
3258 void *cancel_arg;
3259 /* A pathlist containing per-directory pathlists of ignore patterns. */
3260 struct got_pathlist_head ignores;
3261 int report_unchanged;
3262 int no_ignores;
3265 static const struct got_error *
3266 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3267 int dirfd, const char *de_name,
3268 got_worktree_status_cb status_cb, void *status_arg,
3269 struct got_repository *repo, int report_unchanged)
3271 const struct got_error *err = NULL;
3272 unsigned char status = GOT_STATUS_NO_CHANGE;
3273 unsigned char staged_status = get_staged_status(ie);
3274 struct stat sb;
3275 struct got_object_id blob_id, commit_id, staged_blob_id;
3276 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3277 struct got_object_id *staged_blob_idp = NULL;
3279 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3280 if (err)
3281 return err;
3283 if (status == GOT_STATUS_NO_CHANGE &&
3284 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3285 return NULL;
3287 if (got_fileindex_entry_has_blob(ie)) {
3288 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3289 blob_idp = &blob_id;
3291 if (got_fileindex_entry_has_commit(ie)) {
3292 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3293 commit_idp = &commit_id;
3295 if (staged_status == GOT_STATUS_ADD ||
3296 staged_status == GOT_STATUS_MODIFY) {
3297 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3298 SHA1_DIGEST_LENGTH);
3299 staged_blob_idp = &staged_blob_id;
3302 return (*status_cb)(status_arg, status, staged_status,
3303 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3306 static const struct got_error *
3307 status_old_new(void *arg, struct got_fileindex_entry *ie,
3308 struct dirent *de, const char *parent_path, int dirfd)
3310 const struct got_error *err = NULL;
3311 struct diff_dir_cb_arg *a = arg;
3312 char *abspath;
3314 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3315 return got_error(GOT_ERR_CANCELLED);
3317 if (got_path_cmp(parent_path, a->status_path,
3318 strlen(parent_path), a->status_path_len) != 0 &&
3319 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3320 return NULL;
3322 if (parent_path[0]) {
3323 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3324 parent_path, de->d_name) == -1)
3325 return got_error_from_errno("asprintf");
3326 } else {
3327 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3328 de->d_name) == -1)
3329 return got_error_from_errno("asprintf");
3332 err = report_file_status(ie, abspath, dirfd, de->d_name,
3333 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3334 free(abspath);
3335 return err;
3338 static const struct got_error *
3339 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3341 struct diff_dir_cb_arg *a = arg;
3342 struct got_object_id blob_id, commit_id;
3343 unsigned char status;
3345 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3346 return got_error(GOT_ERR_CANCELLED);
3348 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3349 return NULL;
3351 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3352 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3353 if (got_fileindex_entry_has_file_on_disk(ie))
3354 status = GOT_STATUS_MISSING;
3355 else
3356 status = GOT_STATUS_DELETE;
3357 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3358 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3361 void
3362 free_ignorelist(struct got_pathlist_head *ignorelist)
3364 struct got_pathlist_entry *pe;
3366 TAILQ_FOREACH(pe, ignorelist, entry)
3367 free((char *)pe->path);
3368 got_pathlist_free(ignorelist);
3371 void
3372 free_ignores(struct got_pathlist_head *ignores)
3374 struct got_pathlist_entry *pe;
3376 TAILQ_FOREACH(pe, ignores, entry) {
3377 struct got_pathlist_head *ignorelist = pe->data;
3378 free_ignorelist(ignorelist);
3379 free((char *)pe->path);
3381 got_pathlist_free(ignores);
3384 static const struct got_error *
3385 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3387 const struct got_error *err = NULL;
3388 struct got_pathlist_entry *pe = NULL;
3389 struct got_pathlist_head *ignorelist;
3390 char *line = NULL, *pattern, *dirpath = NULL;
3391 size_t linesize = 0;
3392 ssize_t linelen;
3394 ignorelist = calloc(1, sizeof(*ignorelist));
3395 if (ignorelist == NULL)
3396 return got_error_from_errno("calloc");
3397 TAILQ_INIT(ignorelist);
3399 while ((linelen = getline(&line, &linesize, f)) != -1) {
3400 if (linelen > 0 && line[linelen - 1] == '\n')
3401 line[linelen - 1] = '\0';
3403 /* Git's ignores may contain comments. */
3404 if (line[0] == '#')
3405 continue;
3407 /* Git's negated patterns are not (yet?) supported. */
3408 if (line[0] == '!')
3409 continue;
3411 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3412 line) == -1) {
3413 err = got_error_from_errno("asprintf");
3414 goto done;
3416 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3417 if (err)
3418 goto done;
3420 if (ferror(f)) {
3421 err = got_error_from_errno("getline");
3422 goto done;
3425 dirpath = strdup(path);
3426 if (dirpath == NULL) {
3427 err = got_error_from_errno("strdup");
3428 goto done;
3430 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3431 done:
3432 free(line);
3433 if (err || pe == NULL) {
3434 free(dirpath);
3435 free_ignorelist(ignorelist);
3437 return err;
3440 int
3441 match_ignores(struct got_pathlist_head *ignores, const char *path)
3443 struct got_pathlist_entry *pe;
3445 /* Handle patterns which match in all directories. */
3446 TAILQ_FOREACH(pe, ignores, entry) {
3447 struct got_pathlist_head *ignorelist = pe->data;
3448 struct got_pathlist_entry *pi;
3450 TAILQ_FOREACH(pi, ignorelist, entry) {
3451 const char *p, *pattern = pi->path;
3453 if (strncmp(pattern, "**/", 3) != 0)
3454 continue;
3455 pattern += 3;
3456 p = path;
3457 while (*p) {
3458 if (fnmatch(pattern, p,
3459 FNM_PATHNAME | FNM_LEADING_DIR)) {
3460 /* Retry in next directory. */
3461 while (*p && *p != '/')
3462 p++;
3463 while (*p == '/')
3464 p++;
3465 continue;
3467 return 1;
3473 * The ignores pathlist contains ignore lists from children before
3474 * parents, so we can find the most specific ignorelist by walking
3475 * ignores backwards.
3477 pe = TAILQ_LAST(ignores, got_pathlist_head);
3478 while (pe) {
3479 if (got_path_is_child(path, pe->path, pe->path_len)) {
3480 struct got_pathlist_head *ignorelist = pe->data;
3481 struct got_pathlist_entry *pi;
3482 TAILQ_FOREACH(pi, ignorelist, entry) {
3483 const char *pattern = pi->path;
3484 int flags = FNM_LEADING_DIR;
3485 if (strstr(pattern, "/**/") == NULL)
3486 flags |= FNM_PATHNAME;
3487 if (fnmatch(pattern, path, flags))
3488 continue;
3489 return 1;
3492 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3495 return 0;
3498 static const struct got_error *
3499 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3500 const char *path, int dirfd, const char *ignores_filename)
3502 const struct got_error *err = NULL;
3503 char *ignorespath;
3504 int fd = -1;
3505 FILE *ignoresfile = NULL;
3507 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3508 path[0] ? "/" : "", ignores_filename) == -1)
3509 return got_error_from_errno("asprintf");
3511 if (dirfd != -1) {
3512 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3513 if (fd == -1) {
3514 if (errno != ENOENT && errno != EACCES)
3515 err = got_error_from_errno2("openat",
3516 ignorespath);
3517 } else {
3518 ignoresfile = fdopen(fd, "r");
3519 if (ignoresfile == NULL)
3520 err = got_error_from_errno2("fdopen",
3521 ignorespath);
3522 else {
3523 fd = -1;
3524 err = read_ignores(ignores, path, ignoresfile);
3527 } else {
3528 ignoresfile = fopen(ignorespath, "r");
3529 if (ignoresfile == NULL) {
3530 if (errno != ENOENT && errno != EACCES)
3531 err = got_error_from_errno2("fopen",
3532 ignorespath);
3533 } else
3534 err = read_ignores(ignores, path, ignoresfile);
3537 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3538 err = got_error_from_errno2("fclose", path);
3539 if (fd != -1 && close(fd) == -1 && err == NULL)
3540 err = got_error_from_errno2("close", path);
3541 free(ignorespath);
3542 return err;
3545 static const struct got_error *
3546 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3548 const struct got_error *err = NULL;
3549 struct diff_dir_cb_arg *a = arg;
3550 char *path = NULL;
3552 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3553 return got_error(GOT_ERR_CANCELLED);
3555 if (parent_path[0]) {
3556 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3557 return got_error_from_errno("asprintf");
3558 } else {
3559 path = de->d_name;
3562 if (de->d_type != DT_DIR &&
3563 got_path_is_child(path, a->status_path, a->status_path_len)
3564 && !match_ignores(&a->ignores, path))
3565 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3566 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3567 if (parent_path[0])
3568 free(path);
3569 return err;
3572 static const struct got_error *
3573 status_traverse(void *arg, const char *path, int dirfd)
3575 const struct got_error *err = NULL;
3576 struct diff_dir_cb_arg *a = arg;
3578 if (a->no_ignores)
3579 return NULL;
3581 err = add_ignores(&a->ignores, a->worktree->root_path,
3582 path, dirfd, ".cvsignore");
3583 if (err)
3584 return err;
3586 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3587 dirfd, ".gitignore");
3589 return err;
3592 static const struct got_error *
3593 report_single_file_status(const char *path, const char *ondisk_path,
3594 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3595 void *status_arg, struct got_repository *repo, int report_unchanged)
3597 struct got_fileindex_entry *ie;
3598 struct stat sb;
3600 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3601 if (ie)
3602 return report_file_status(ie, ondisk_path, -1, NULL,
3603 status_cb, status_arg, repo, report_unchanged);
3605 if (lstat(ondisk_path, &sb) == -1) {
3606 if (errno != ENOENT)
3607 return got_error_from_errno2("lstat", ondisk_path);
3608 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3609 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3610 return NULL;
3613 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3614 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3615 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3617 return NULL;
3620 static const struct got_error *
3621 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3622 const char *root_path, const char *path)
3624 const struct got_error *err;
3625 char *parent_path, *next_parent_path = NULL;
3627 err = add_ignores(ignores, root_path, "", -1,
3628 ".cvsignore");
3629 if (err)
3630 return err;
3632 err = add_ignores(ignores, root_path, "", -1,
3633 ".gitignore");
3634 if (err)
3635 return err;
3637 err = got_path_dirname(&parent_path, path);
3638 if (err) {
3639 if (err->code == GOT_ERR_BAD_PATH)
3640 return NULL; /* cannot traverse parent */
3641 return err;
3643 for (;;) {
3644 err = add_ignores(ignores, root_path, parent_path, -1,
3645 ".cvsignore");
3646 if (err)
3647 break;
3648 err = add_ignores(ignores, root_path, parent_path, -1,
3649 ".gitignore");
3650 if (err)
3651 break;
3652 err = got_path_dirname(&next_parent_path, parent_path);
3653 if (err) {
3654 if (err->code == GOT_ERR_BAD_PATH)
3655 err = NULL; /* traversed everything */
3656 break;
3658 free(parent_path);
3659 parent_path = next_parent_path;
3660 next_parent_path = NULL;
3663 free(parent_path);
3664 free(next_parent_path);
3665 return err;
3668 static const struct got_error *
3669 worktree_status(struct got_worktree *worktree, const char *path,
3670 struct got_fileindex *fileindex, struct got_repository *repo,
3671 got_worktree_status_cb status_cb, void *status_arg,
3672 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3673 int report_unchanged)
3675 const struct got_error *err = NULL;
3676 int fd = -1;
3677 struct got_fileindex_diff_dir_cb fdiff_cb;
3678 struct diff_dir_cb_arg arg;
3679 char *ondisk_path = NULL;
3681 TAILQ_INIT(&arg.ignores);
3683 if (asprintf(&ondisk_path, "%s%s%s",
3684 worktree->root_path, path[0] ? "/" : "", path) == -1)
3685 return got_error_from_errno("asprintf");
3687 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3688 if (fd == -1) {
3689 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3690 errno != ELOOP)
3691 err = got_error_from_errno2("open", ondisk_path);
3692 else
3693 err = report_single_file_status(path, ondisk_path,
3694 fileindex, status_cb, status_arg, repo,
3695 report_unchanged);
3696 } else {
3697 fdiff_cb.diff_old_new = status_old_new;
3698 fdiff_cb.diff_old = status_old;
3699 fdiff_cb.diff_new = status_new;
3700 fdiff_cb.diff_traverse = status_traverse;
3701 arg.fileindex = fileindex;
3702 arg.worktree = worktree;
3703 arg.status_path = path;
3704 arg.status_path_len = strlen(path);
3705 arg.repo = repo;
3706 arg.status_cb = status_cb;
3707 arg.status_arg = status_arg;
3708 arg.cancel_cb = cancel_cb;
3709 arg.cancel_arg = cancel_arg;
3710 arg.report_unchanged = report_unchanged;
3711 arg.no_ignores = no_ignores;
3712 if (!no_ignores) {
3713 err = add_ignores_from_parent_paths(&arg.ignores,
3714 worktree->root_path, path);
3715 if (err)
3716 goto done;
3718 err = got_fileindex_diff_dir(fileindex, fd,
3719 worktree->root_path, path, repo, &fdiff_cb, &arg);
3721 done:
3722 free_ignores(&arg.ignores);
3723 if (fd != -1 && close(fd) == -1 && err == NULL)
3724 err = got_error_from_errno("close");
3725 free(ondisk_path);
3726 return err;
3729 const struct got_error *
3730 got_worktree_status(struct got_worktree *worktree,
3731 struct got_pathlist_head *paths, struct got_repository *repo,
3732 got_worktree_status_cb status_cb, void *status_arg,
3733 got_cancel_cb cancel_cb, void *cancel_arg)
3735 const struct got_error *err = NULL;
3736 char *fileindex_path = NULL;
3737 struct got_fileindex *fileindex = NULL;
3738 struct got_pathlist_entry *pe;
3740 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3741 if (err)
3742 return err;
3744 TAILQ_FOREACH(pe, paths, entry) {
3745 err = worktree_status(worktree, pe->path, fileindex, repo,
3746 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3747 if (err)
3748 break;
3750 free(fileindex_path);
3751 got_fileindex_free(fileindex);
3752 return err;
3755 const struct got_error *
3756 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3757 const char *arg)
3759 const struct got_error *err = NULL;
3760 char *resolved = NULL, *cwd = NULL, *path = NULL;
3761 size_t len;
3762 struct stat sb;
3763 char *abspath = NULL;
3764 char canonpath[PATH_MAX];
3766 *wt_path = NULL;
3768 cwd = getcwd(NULL, 0);
3769 if (cwd == NULL)
3770 return got_error_from_errno("getcwd");
3772 if (lstat(arg, &sb) == -1) {
3773 if (errno != ENOENT) {
3774 err = got_error_from_errno2("lstat", arg);
3775 goto done;
3777 sb.st_mode = 0;
3779 if (S_ISLNK(sb.st_mode)) {
3781 * We cannot use realpath(3) with symlinks since we want to
3782 * operate on the symlink itself.
3783 * But we can make the path absolute, assuming it is relative
3784 * to the current working directory, and then canonicalize it.
3786 if (!got_path_is_absolute(arg)) {
3787 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3788 err = got_error_from_errno("asprintf");
3789 goto done;
3793 err = got_canonpath(abspath ? abspath : arg, canonpath,
3794 sizeof(canonpath));
3795 if (err)
3796 goto done;
3797 resolved = strdup(canonpath);
3798 if (resolved == NULL) {
3799 err = got_error_from_errno("strdup");
3800 goto done;
3802 } else {
3803 resolved = realpath(arg, NULL);
3804 if (resolved == NULL) {
3805 if (errno != ENOENT) {
3806 err = got_error_from_errno2("realpath", arg);
3807 goto done;
3809 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3810 err = got_error_from_errno("asprintf");
3811 goto done;
3813 err = got_canonpath(abspath, canonpath,
3814 sizeof(canonpath));
3815 if (err)
3816 goto done;
3817 resolved = strdup(canonpath);
3818 if (resolved == NULL) {
3819 err = got_error_from_errno("strdup");
3820 goto done;
3825 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3826 strlen(got_worktree_get_root_path(worktree)))) {
3827 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3828 goto done;
3831 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3832 err = got_path_skip_common_ancestor(&path,
3833 got_worktree_get_root_path(worktree), resolved);
3834 if (err)
3835 goto done;
3836 } else {
3837 path = strdup("");
3838 if (path == NULL) {
3839 err = got_error_from_errno("strdup");
3840 goto done;
3844 /* XXX status walk can't deal with trailing slash! */
3845 len = strlen(path);
3846 while (len > 0 && path[len - 1] == '/') {
3847 path[len - 1] = '\0';
3848 len--;
3850 done:
3851 free(abspath);
3852 free(resolved);
3853 free(cwd);
3854 if (err == NULL)
3855 *wt_path = path;
3856 else
3857 free(path);
3858 return err;
3861 struct schedule_addition_args {
3862 struct got_worktree *worktree;
3863 struct got_fileindex *fileindex;
3864 got_worktree_checkout_cb progress_cb;
3865 void *progress_arg;
3866 struct got_repository *repo;
3869 static const struct got_error *
3870 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3871 const char *relpath, struct got_object_id *blob_id,
3872 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3873 int dirfd, const char *de_name)
3875 struct schedule_addition_args *a = arg;
3876 const struct got_error *err = NULL;
3877 struct got_fileindex_entry *ie;
3878 struct stat sb;
3879 char *ondisk_path;
3881 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3882 relpath) == -1)
3883 return got_error_from_errno("asprintf");
3885 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3886 if (ie) {
3887 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3888 de_name, a->repo);
3889 if (err)
3890 goto done;
3891 /* Re-adding an existing entry is a no-op. */
3892 if (status == GOT_STATUS_ADD)
3893 goto done;
3894 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3895 if (err)
3896 goto done;
3899 if (status != GOT_STATUS_UNVERSIONED) {
3900 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3901 goto done;
3904 err = got_fileindex_entry_alloc(&ie, relpath);
3905 if (err)
3906 goto done;
3907 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3908 relpath, NULL, NULL, 1);
3909 if (err) {
3910 got_fileindex_entry_free(ie);
3911 goto done;
3913 err = got_fileindex_entry_add(a->fileindex, ie);
3914 if (err) {
3915 got_fileindex_entry_free(ie);
3916 goto done;
3918 done:
3919 free(ondisk_path);
3920 if (err)
3921 return err;
3922 if (status == GOT_STATUS_ADD)
3923 return NULL;
3924 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3927 const struct got_error *
3928 got_worktree_schedule_add(struct got_worktree *worktree,
3929 struct got_pathlist_head *paths,
3930 got_worktree_checkout_cb progress_cb, void *progress_arg,
3931 struct got_repository *repo, int no_ignores)
3933 struct got_fileindex *fileindex = NULL;
3934 char *fileindex_path = NULL;
3935 const struct got_error *err = NULL, *sync_err, *unlockerr;
3936 struct got_pathlist_entry *pe;
3937 struct schedule_addition_args saa;
3939 err = lock_worktree(worktree, LOCK_EX);
3940 if (err)
3941 return err;
3943 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3944 if (err)
3945 goto done;
3947 saa.worktree = worktree;
3948 saa.fileindex = fileindex;
3949 saa.progress_cb = progress_cb;
3950 saa.progress_arg = progress_arg;
3951 saa.repo = repo;
3953 TAILQ_FOREACH(pe, paths, entry) {
3954 err = worktree_status(worktree, pe->path, fileindex, repo,
3955 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3956 if (err)
3957 break;
3959 sync_err = sync_fileindex(fileindex, fileindex_path);
3960 if (sync_err && err == NULL)
3961 err = sync_err;
3962 done:
3963 free(fileindex_path);
3964 if (fileindex)
3965 got_fileindex_free(fileindex);
3966 unlockerr = lock_worktree(worktree, LOCK_SH);
3967 if (unlockerr && err == NULL)
3968 err = unlockerr;
3969 return err;
3972 struct schedule_deletion_args {
3973 struct got_worktree *worktree;
3974 struct got_fileindex *fileindex;
3975 got_worktree_delete_cb progress_cb;
3976 void *progress_arg;
3977 struct got_repository *repo;
3978 int delete_local_mods;
3979 int keep_on_disk;
3980 const char *status_codes;
3983 static const struct got_error *
3984 schedule_for_deletion(void *arg, unsigned char status,
3985 unsigned char staged_status, const char *relpath,
3986 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3987 struct got_object_id *commit_id, int dirfd, const char *de_name)
3989 struct schedule_deletion_args *a = arg;
3990 const struct got_error *err = NULL;
3991 struct got_fileindex_entry *ie = NULL;
3992 struct stat sb;
3993 char *ondisk_path;
3995 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3996 if (ie == NULL)
3997 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3999 staged_status = get_staged_status(ie);
4000 if (staged_status != GOT_STATUS_NO_CHANGE) {
4001 if (staged_status == GOT_STATUS_DELETE)
4002 return NULL;
4003 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4006 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4007 relpath) == -1)
4008 return got_error_from_errno("asprintf");
4010 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4011 a->repo);
4012 if (err)
4013 goto done;
4015 if (a->status_codes) {
4016 size_t ncodes = strlen(a->status_codes);
4017 int i;
4018 for (i = 0; i < ncodes ; i++) {
4019 if (status == a->status_codes[i])
4020 break;
4022 if (i == ncodes) {
4023 /* Do not delete files in non-matching status. */
4024 free(ondisk_path);
4025 return NULL;
4027 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4028 a->status_codes[i] != GOT_STATUS_MISSING) {
4029 static char msg[64];
4030 snprintf(msg, sizeof(msg),
4031 "invalid status code '%c'", a->status_codes[i]);
4032 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4033 goto done;
4037 if (status != GOT_STATUS_NO_CHANGE) {
4038 if (status == GOT_STATUS_DELETE)
4039 goto done;
4040 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4041 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4042 goto done;
4044 if (status != GOT_STATUS_MODIFY &&
4045 status != GOT_STATUS_MISSING) {
4046 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4047 goto done;
4051 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4052 size_t root_len;
4054 if (dirfd != -1) {
4055 if (unlinkat(dirfd, de_name, 0) != 0) {
4056 err = got_error_from_errno2("unlinkat",
4057 ondisk_path);
4058 goto done;
4060 } else if (unlink(ondisk_path) != 0) {
4061 err = got_error_from_errno2("unlink", ondisk_path);
4062 goto done;
4065 root_len = strlen(a->worktree->root_path);
4066 do {
4067 char *parent;
4068 err = got_path_dirname(&parent, ondisk_path);
4069 if (err)
4070 goto done;
4071 free(ondisk_path);
4072 ondisk_path = parent;
4073 if (rmdir(ondisk_path) == -1) {
4074 if (errno != ENOTEMPTY)
4075 err = got_error_from_errno2("rmdir",
4076 ondisk_path);
4077 break;
4079 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4080 strlen(ondisk_path), root_len) != 0);
4083 got_fileindex_entry_mark_deleted_from_disk(ie);
4084 done:
4085 free(ondisk_path);
4086 if (err)
4087 return err;
4088 if (status == GOT_STATUS_DELETE)
4089 return NULL;
4090 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4091 staged_status, relpath);
4094 const struct got_error *
4095 got_worktree_schedule_delete(struct got_worktree *worktree,
4096 struct got_pathlist_head *paths, int delete_local_mods,
4097 const char *status_codes,
4098 got_worktree_delete_cb progress_cb, void *progress_arg,
4099 struct got_repository *repo, int keep_on_disk)
4101 struct got_fileindex *fileindex = NULL;
4102 char *fileindex_path = NULL;
4103 const struct got_error *err = NULL, *sync_err, *unlockerr;
4104 struct got_pathlist_entry *pe;
4105 struct schedule_deletion_args sda;
4107 err = lock_worktree(worktree, LOCK_EX);
4108 if (err)
4109 return err;
4111 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4112 if (err)
4113 goto done;
4115 sda.worktree = worktree;
4116 sda.fileindex = fileindex;
4117 sda.progress_cb = progress_cb;
4118 sda.progress_arg = progress_arg;
4119 sda.repo = repo;
4120 sda.delete_local_mods = delete_local_mods;
4121 sda.keep_on_disk = keep_on_disk;
4122 sda.status_codes = status_codes;
4124 TAILQ_FOREACH(pe, paths, entry) {
4125 err = worktree_status(worktree, pe->path, fileindex, repo,
4126 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4127 if (err)
4128 break;
4130 sync_err = sync_fileindex(fileindex, fileindex_path);
4131 if (sync_err && err == NULL)
4132 err = sync_err;
4133 done:
4134 free(fileindex_path);
4135 if (fileindex)
4136 got_fileindex_free(fileindex);
4137 unlockerr = lock_worktree(worktree, LOCK_SH);
4138 if (unlockerr && err == NULL)
4139 err = unlockerr;
4140 return err;
4143 static const struct got_error *
4144 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4146 const struct got_error *err = NULL;
4147 char *line = NULL;
4148 size_t linesize = 0, n;
4149 ssize_t linelen;
4151 linelen = getline(&line, &linesize, infile);
4152 if (linelen == -1) {
4153 if (ferror(infile)) {
4154 err = got_error_from_errno("getline");
4155 goto done;
4157 return NULL;
4159 if (outfile) {
4160 n = fwrite(line, 1, linelen, outfile);
4161 if (n != linelen) {
4162 err = got_ferror(outfile, GOT_ERR_IO);
4163 goto done;
4166 if (rejectfile) {
4167 n = fwrite(line, 1, linelen, rejectfile);
4168 if (n != linelen)
4169 err = got_ferror(outfile, GOT_ERR_IO);
4171 done:
4172 free(line);
4173 return err;
4176 static const struct got_error *
4177 skip_one_line(FILE *f)
4179 char *line = NULL;
4180 size_t linesize = 0;
4181 ssize_t linelen;
4183 linelen = getline(&line, &linesize, f);
4184 if (linelen == -1) {
4185 if (ferror(f))
4186 return got_error_from_errno("getline");
4187 return NULL;
4189 free(line);
4190 return NULL;
4193 static const struct got_error *
4194 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4195 int start_old, int end_old, int start_new, int end_new,
4196 FILE *outfile, FILE *rejectfile)
4198 const struct got_error *err;
4200 /* Copy old file's lines leading up to patch. */
4201 while (!feof(f1) && *line_cur1 < start_old) {
4202 err = copy_one_line(f1, outfile, NULL);
4203 if (err)
4204 return err;
4205 (*line_cur1)++;
4207 /* Skip new file's lines leading up to patch. */
4208 while (!feof(f2) && *line_cur2 < start_new) {
4209 if (rejectfile)
4210 err = copy_one_line(f2, NULL, rejectfile);
4211 else
4212 err = skip_one_line(f2);
4213 if (err)
4214 return err;
4215 (*line_cur2)++;
4217 /* Copy patched lines. */
4218 while (!feof(f2) && *line_cur2 <= end_new) {
4219 err = copy_one_line(f2, outfile, NULL);
4220 if (err)
4221 return err;
4222 (*line_cur2)++;
4224 /* Skip over old file's replaced lines. */
4225 while (!feof(f1) && *line_cur1 <= end_old) {
4226 if (rejectfile)
4227 err = copy_one_line(f1, NULL, rejectfile);
4228 else
4229 err = skip_one_line(f1);
4230 if (err)
4231 return err;
4232 (*line_cur1)++;
4235 return NULL;
4238 static const struct got_error *
4239 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4240 FILE *outfile, FILE *rejectfile)
4242 const struct got_error *err;
4244 if (outfile) {
4245 /* Copy old file's lines until EOF. */
4246 while (!feof(f1)) {
4247 err = copy_one_line(f1, outfile, NULL);
4248 if (err)
4249 return err;
4250 (*line_cur1)++;
4253 if (rejectfile) {
4254 /* Copy new file's lines until EOF. */
4255 while (!feof(f2)) {
4256 err = copy_one_line(f2, NULL, rejectfile);
4257 if (err)
4258 return err;
4259 (*line_cur2)++;
4263 return NULL;
4266 static const struct got_error *
4267 apply_or_reject_change(int *choice, int *nchunks_used,
4268 struct diff_result *diff_result, int n,
4269 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4270 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4271 got_worktree_patch_cb patch_cb, void *patch_arg)
4273 const struct got_error *err = NULL;
4274 struct diff_chunk_context cc = {};
4275 int start_old, end_old, start_new, end_new;
4276 FILE *hunkfile;
4277 struct diff_output_unidiff_state *diff_state;
4278 struct diff_input_info diff_info;
4279 int rc;
4281 *choice = GOT_PATCH_CHOICE_NONE;
4283 /* Get changed line numbers without context lines for copy_change(). */
4284 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4285 start_old = cc.left.start;
4286 end_old = cc.left.end;
4287 start_new = cc.right.start;
4288 end_new = cc.right.end;
4290 /* Get the same change with context lines for display. */
4291 memset(&cc, 0, sizeof(cc));
4292 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4294 memset(&diff_info, 0, sizeof(diff_info));
4295 diff_info.left_path = relpath;
4296 diff_info.right_path = relpath;
4298 diff_state = diff_output_unidiff_state_alloc();
4299 if (diff_state == NULL)
4300 return got_error_set_errno(ENOMEM,
4301 "diff_output_unidiff_state_alloc");
4303 hunkfile = got_opentemp();
4304 if (hunkfile == NULL) {
4305 err = got_error_from_errno("got_opentemp");
4306 goto done;
4309 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4310 diff_result, &cc);
4311 if (rc != DIFF_RC_OK) {
4312 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4313 goto done;
4316 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4317 err = got_ferror(hunkfile, GOT_ERR_IO);
4318 goto done;
4321 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4322 hunkfile, changeno, nchanges);
4323 if (err)
4324 goto done;
4326 switch (*choice) {
4327 case GOT_PATCH_CHOICE_YES:
4328 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4329 end_old, start_new, end_new, outfile, rejectfile);
4330 break;
4331 case GOT_PATCH_CHOICE_NO:
4332 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4333 end_old, start_new, end_new, rejectfile, outfile);
4334 break;
4335 case GOT_PATCH_CHOICE_QUIT:
4336 break;
4337 default:
4338 err = got_error(GOT_ERR_PATCH_CHOICE);
4339 break;
4341 done:
4342 diff_output_unidiff_state_free(diff_state);
4343 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4344 err = got_error_from_errno("fclose");
4345 return err;
4348 struct revert_file_args {
4349 struct got_worktree *worktree;
4350 struct got_fileindex *fileindex;
4351 got_worktree_checkout_cb progress_cb;
4352 void *progress_arg;
4353 got_worktree_patch_cb patch_cb;
4354 void *patch_arg;
4355 struct got_repository *repo;
4358 static const struct got_error *
4359 create_patched_content(char **path_outfile, int reverse_patch,
4360 struct got_object_id *blob_id, const char *path2,
4361 int dirfd2, const char *de_name2,
4362 const char *relpath, struct got_repository *repo,
4363 got_worktree_patch_cb patch_cb, void *patch_arg)
4365 const struct got_error *err, *free_err;
4366 struct got_blob_object *blob = NULL;
4367 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4368 int fd2 = -1;
4369 char link_target[PATH_MAX];
4370 ssize_t link_len = 0;
4371 char *path1 = NULL, *id_str = NULL;
4372 struct stat sb2;
4373 struct got_diffreg_result *diffreg_result = NULL;
4374 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4375 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4377 *path_outfile = NULL;
4379 err = got_object_id_str(&id_str, blob_id);
4380 if (err)
4381 return err;
4383 if (dirfd2 != -1) {
4384 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4385 if (fd2 == -1) {
4386 if (errno != ELOOP) {
4387 err = got_error_from_errno2("openat", path2);
4388 goto done;
4390 link_len = readlinkat(dirfd2, de_name2,
4391 link_target, sizeof(link_target));
4392 if (link_len == -1)
4393 return got_error_from_errno2("readlinkat", path2);
4394 sb2.st_mode = S_IFLNK;
4395 sb2.st_size = link_len;
4397 } else {
4398 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4399 if (fd2 == -1) {
4400 if (errno != ELOOP) {
4401 err = got_error_from_errno2("open", path2);
4402 goto done;
4404 link_len = readlink(path2, link_target,
4405 sizeof(link_target));
4406 if (link_len == -1)
4407 return got_error_from_errno2("readlink", path2);
4408 sb2.st_mode = S_IFLNK;
4409 sb2.st_size = link_len;
4412 if (fd2 != -1) {
4413 if (fstat(fd2, &sb2) == -1) {
4414 err = got_error_from_errno2("fstat", path2);
4415 goto done;
4418 f2 = fdopen(fd2, "r");
4419 if (f2 == NULL) {
4420 err = got_error_from_errno2("fdopen", path2);
4421 goto done;
4423 fd2 = -1;
4424 } else {
4425 size_t n;
4426 f2 = got_opentemp();
4427 if (f2 == NULL) {
4428 err = got_error_from_errno2("got_opentemp", path2);
4429 goto done;
4431 n = fwrite(link_target, 1, link_len, f2);
4432 if (n != link_len) {
4433 err = got_ferror(f2, GOT_ERR_IO);
4434 goto done;
4436 if (fflush(f2) == EOF) {
4437 err = got_error_from_errno("fflush");
4438 goto done;
4440 rewind(f2);
4443 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4444 if (err)
4445 goto done;
4447 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4448 if (err)
4449 goto done;
4451 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4452 if (err)
4453 goto done;
4455 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4456 NULL);
4457 if (err)
4458 goto done;
4460 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4461 if (err)
4462 goto done;
4464 if (fseek(f1, 0L, SEEK_SET) == -1)
4465 return got_ferror(f1, GOT_ERR_IO);
4466 if (fseek(f2, 0L, SEEK_SET) == -1)
4467 return got_ferror(f2, GOT_ERR_IO);
4469 /* Count the number of actual changes in the diff result. */
4470 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4471 struct diff_chunk_context cc = {};
4472 diff_chunk_context_load_change(&cc, &nchunks_used,
4473 diffreg_result->result, n, 0);
4474 nchanges++;
4476 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4477 int choice;
4478 err = apply_or_reject_change(&choice, &nchunks_used,
4479 diffreg_result->result, n, relpath, f1, f2,
4480 &line_cur1, &line_cur2,
4481 reverse_patch ? NULL : outfile,
4482 reverse_patch ? outfile : NULL,
4483 ++i, nchanges, patch_cb, patch_arg);
4484 if (err)
4485 goto done;
4486 if (choice == GOT_PATCH_CHOICE_YES)
4487 have_content = 1;
4488 else if (choice == GOT_PATCH_CHOICE_QUIT)
4489 break;
4491 if (have_content) {
4492 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4493 reverse_patch ? NULL : outfile,
4494 reverse_patch ? outfile : NULL);
4495 if (err)
4496 goto done;
4498 if (!S_ISLNK(sb2.st_mode)) {
4499 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4500 err = got_error_from_errno2("fchmod", path2);
4501 goto done;
4505 done:
4506 free(id_str);
4507 if (blob)
4508 got_object_blob_close(blob);
4509 free_err = got_diffreg_result_free(diffreg_result);
4510 if (err == NULL)
4511 err = free_err;
4512 if (f1 && fclose(f1) == EOF && err == NULL)
4513 err = got_error_from_errno2("fclose", path1);
4514 if (f2 && fclose(f2) == EOF && err == NULL)
4515 err = got_error_from_errno2("fclose", path2);
4516 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4517 err = got_error_from_errno2("close", path2);
4518 if (outfile && fclose(outfile) == EOF && err == NULL)
4519 err = got_error_from_errno2("fclose", *path_outfile);
4520 if (path1 && unlink(path1) == -1 && err == NULL)
4521 err = got_error_from_errno2("unlink", path1);
4522 if (err || !have_content) {
4523 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4524 err = got_error_from_errno2("unlink", *path_outfile);
4525 free(*path_outfile);
4526 *path_outfile = NULL;
4528 free(path1);
4529 return err;
4532 static const struct got_error *
4533 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4534 const char *relpath, struct got_object_id *blob_id,
4535 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4536 int dirfd, const char *de_name)
4538 struct revert_file_args *a = arg;
4539 const struct got_error *err = NULL;
4540 char *parent_path = NULL;
4541 struct got_fileindex_entry *ie;
4542 struct got_tree_object *tree = NULL;
4543 struct got_object_id *tree_id = NULL;
4544 const struct got_tree_entry *te = NULL;
4545 char *tree_path = NULL, *te_name;
4546 char *ondisk_path = NULL, *path_content = NULL;
4547 struct got_blob_object *blob = NULL;
4549 /* Reverting a staged deletion is a no-op. */
4550 if (status == GOT_STATUS_DELETE &&
4551 staged_status != GOT_STATUS_NO_CHANGE)
4552 return NULL;
4554 if (status == GOT_STATUS_UNVERSIONED)
4555 return (*a->progress_cb)(a->progress_arg,
4556 GOT_STATUS_UNVERSIONED, relpath);
4558 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4559 if (ie == NULL)
4560 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4562 /* Construct in-repository path of tree which contains this blob. */
4563 err = got_path_dirname(&parent_path, ie->path);
4564 if (err) {
4565 if (err->code != GOT_ERR_BAD_PATH)
4566 goto done;
4567 parent_path = strdup("/");
4568 if (parent_path == NULL) {
4569 err = got_error_from_errno("strdup");
4570 goto done;
4573 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4574 tree_path = strdup(parent_path);
4575 if (tree_path == NULL) {
4576 err = got_error_from_errno("strdup");
4577 goto done;
4579 } else {
4580 if (got_path_is_root_dir(parent_path)) {
4581 tree_path = strdup(a->worktree->path_prefix);
4582 if (tree_path == NULL) {
4583 err = got_error_from_errno("strdup");
4584 goto done;
4586 } else {
4587 if (asprintf(&tree_path, "%s/%s",
4588 a->worktree->path_prefix, parent_path) == -1) {
4589 err = got_error_from_errno("asprintf");
4590 goto done;
4595 err = got_object_id_by_path(&tree_id, a->repo,
4596 a->worktree->base_commit_id, tree_path);
4597 if (err) {
4598 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4599 (status == GOT_STATUS_ADD ||
4600 staged_status == GOT_STATUS_ADD)))
4601 goto done;
4602 } else {
4603 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4604 if (err)
4605 goto done;
4607 err = got_path_basename(&te_name, ie->path);
4608 if (err)
4609 goto done;
4611 te = got_object_tree_find_entry(tree, te_name);
4612 free(te_name);
4613 if (te == NULL && status != GOT_STATUS_ADD &&
4614 staged_status != GOT_STATUS_ADD) {
4615 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4616 goto done;
4620 switch (status) {
4621 case GOT_STATUS_ADD:
4622 if (a->patch_cb) {
4623 int choice = GOT_PATCH_CHOICE_NONE;
4624 err = (*a->patch_cb)(&choice, a->patch_arg,
4625 status, ie->path, NULL, 1, 1);
4626 if (err)
4627 goto done;
4628 if (choice != GOT_PATCH_CHOICE_YES)
4629 break;
4631 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4632 ie->path);
4633 if (err)
4634 goto done;
4635 got_fileindex_entry_remove(a->fileindex, ie);
4636 break;
4637 case GOT_STATUS_DELETE:
4638 if (a->patch_cb) {
4639 int choice = GOT_PATCH_CHOICE_NONE;
4640 err = (*a->patch_cb)(&choice, a->patch_arg,
4641 status, ie->path, NULL, 1, 1);
4642 if (err)
4643 goto done;
4644 if (choice != GOT_PATCH_CHOICE_YES)
4645 break;
4647 /* fall through */
4648 case GOT_STATUS_MODIFY:
4649 case GOT_STATUS_MODE_CHANGE:
4650 case GOT_STATUS_CONFLICT:
4651 case GOT_STATUS_MISSING: {
4652 struct got_object_id id;
4653 if (staged_status == GOT_STATUS_ADD ||
4654 staged_status == GOT_STATUS_MODIFY) {
4655 memcpy(id.sha1, ie->staged_blob_sha1,
4656 SHA1_DIGEST_LENGTH);
4657 } else
4658 memcpy(id.sha1, ie->blob_sha1,
4659 SHA1_DIGEST_LENGTH);
4660 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4661 if (err)
4662 goto done;
4664 if (asprintf(&ondisk_path, "%s/%s",
4665 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4666 err = got_error_from_errno("asprintf");
4667 goto done;
4670 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4671 status == GOT_STATUS_CONFLICT)) {
4672 int is_bad_symlink = 0;
4673 err = create_patched_content(&path_content, 1, &id,
4674 ondisk_path, dirfd, de_name, ie->path, a->repo,
4675 a->patch_cb, a->patch_arg);
4676 if (err || path_content == NULL)
4677 break;
4678 if (te && S_ISLNK(te->mode)) {
4679 if (unlink(path_content) == -1) {
4680 err = got_error_from_errno2("unlink",
4681 path_content);
4682 break;
4684 err = install_symlink(&is_bad_symlink,
4685 a->worktree, ondisk_path, ie->path,
4686 blob, 0, 1, 0, a->repo,
4687 a->progress_cb, a->progress_arg);
4688 } else {
4689 if (rename(path_content, ondisk_path) == -1) {
4690 err = got_error_from_errno3("rename",
4691 path_content, ondisk_path);
4692 goto done;
4695 } else {
4696 int is_bad_symlink = 0;
4697 if (te && S_ISLNK(te->mode)) {
4698 err = install_symlink(&is_bad_symlink,
4699 a->worktree, ondisk_path, ie->path,
4700 blob, 0, 1, 0, a->repo,
4701 a->progress_cb, a->progress_arg);
4702 } else {
4703 err = install_blob(a->worktree, ondisk_path,
4704 ie->path,
4705 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4706 got_fileindex_perms_to_st(ie), blob,
4707 0, 1, 0, 0, a->repo,
4708 a->progress_cb, a->progress_arg);
4710 if (err)
4711 goto done;
4712 if (status == GOT_STATUS_DELETE ||
4713 status == GOT_STATUS_MODE_CHANGE) {
4714 err = got_fileindex_entry_update(ie,
4715 a->worktree->root_fd, relpath,
4716 blob->id.sha1,
4717 a->worktree->base_commit_id->sha1, 1);
4718 if (err)
4719 goto done;
4721 if (is_bad_symlink) {
4722 got_fileindex_entry_filetype_set(ie,
4723 GOT_FILEIDX_MODE_BAD_SYMLINK);
4726 break;
4728 default:
4729 break;
4731 done:
4732 free(ondisk_path);
4733 free(path_content);
4734 free(parent_path);
4735 free(tree_path);
4736 if (blob)
4737 got_object_blob_close(blob);
4738 if (tree)
4739 got_object_tree_close(tree);
4740 free(tree_id);
4741 return err;
4744 const struct got_error *
4745 got_worktree_revert(struct got_worktree *worktree,
4746 struct got_pathlist_head *paths,
4747 got_worktree_checkout_cb progress_cb, void *progress_arg,
4748 got_worktree_patch_cb patch_cb, void *patch_arg,
4749 struct got_repository *repo)
4751 struct got_fileindex *fileindex = NULL;
4752 char *fileindex_path = NULL;
4753 const struct got_error *err = NULL, *unlockerr = NULL;
4754 const struct got_error *sync_err = NULL;
4755 struct got_pathlist_entry *pe;
4756 struct revert_file_args rfa;
4758 err = lock_worktree(worktree, LOCK_EX);
4759 if (err)
4760 return err;
4762 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4763 if (err)
4764 goto done;
4766 rfa.worktree = worktree;
4767 rfa.fileindex = fileindex;
4768 rfa.progress_cb = progress_cb;
4769 rfa.progress_arg = progress_arg;
4770 rfa.patch_cb = patch_cb;
4771 rfa.patch_arg = patch_arg;
4772 rfa.repo = repo;
4773 TAILQ_FOREACH(pe, paths, entry) {
4774 err = worktree_status(worktree, pe->path, fileindex, repo,
4775 revert_file, &rfa, NULL, NULL, 0, 0);
4776 if (err)
4777 break;
4779 sync_err = sync_fileindex(fileindex, fileindex_path);
4780 if (sync_err && err == NULL)
4781 err = sync_err;
4782 done:
4783 free(fileindex_path);
4784 if (fileindex)
4785 got_fileindex_free(fileindex);
4786 unlockerr = lock_worktree(worktree, LOCK_SH);
4787 if (unlockerr && err == NULL)
4788 err = unlockerr;
4789 return err;
4792 static void
4793 free_commitable(struct got_commitable *ct)
4795 free(ct->path);
4796 free(ct->in_repo_path);
4797 free(ct->ondisk_path);
4798 free(ct->blob_id);
4799 free(ct->base_blob_id);
4800 free(ct->staged_blob_id);
4801 free(ct->base_commit_id);
4802 free(ct);
4805 struct collect_commitables_arg {
4806 struct got_pathlist_head *commitable_paths;
4807 struct got_repository *repo;
4808 struct got_worktree *worktree;
4809 struct got_fileindex *fileindex;
4810 int have_staged_files;
4811 int allow_bad_symlinks;
4814 static const struct got_error *
4815 collect_commitables(void *arg, unsigned char status,
4816 unsigned char staged_status, const char *relpath,
4817 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4818 struct got_object_id *commit_id, int dirfd, const char *de_name)
4820 struct collect_commitables_arg *a = arg;
4821 const struct got_error *err = NULL;
4822 struct got_commitable *ct = NULL;
4823 struct got_pathlist_entry *new = NULL;
4824 char *parent_path = NULL, *path = NULL;
4825 struct stat sb;
4827 if (a->have_staged_files) {
4828 if (staged_status != GOT_STATUS_MODIFY &&
4829 staged_status != GOT_STATUS_ADD &&
4830 staged_status != GOT_STATUS_DELETE)
4831 return NULL;
4832 } else {
4833 if (status == GOT_STATUS_CONFLICT)
4834 return got_error(GOT_ERR_COMMIT_CONFLICT);
4836 if (status != GOT_STATUS_MODIFY &&
4837 status != GOT_STATUS_MODE_CHANGE &&
4838 status != GOT_STATUS_ADD &&
4839 status != GOT_STATUS_DELETE)
4840 return NULL;
4843 if (asprintf(&path, "/%s", relpath) == -1) {
4844 err = got_error_from_errno("asprintf");
4845 goto done;
4847 if (strcmp(path, "/") == 0) {
4848 parent_path = strdup("");
4849 if (parent_path == NULL)
4850 return got_error_from_errno("strdup");
4851 } else {
4852 err = got_path_dirname(&parent_path, path);
4853 if (err)
4854 return err;
4857 ct = calloc(1, sizeof(*ct));
4858 if (ct == NULL) {
4859 err = got_error_from_errno("calloc");
4860 goto done;
4863 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4864 relpath) == -1) {
4865 err = got_error_from_errno("asprintf");
4866 goto done;
4869 if (staged_status == GOT_STATUS_ADD ||
4870 staged_status == GOT_STATUS_MODIFY) {
4871 struct got_fileindex_entry *ie;
4872 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4873 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4874 case GOT_FILEIDX_MODE_REGULAR_FILE:
4875 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4876 ct->mode = S_IFREG;
4877 break;
4878 case GOT_FILEIDX_MODE_SYMLINK:
4879 ct->mode = S_IFLNK;
4880 break;
4881 default:
4882 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4883 goto done;
4885 ct->mode |= got_fileindex_entry_perms_get(ie);
4886 } else if (status != GOT_STATUS_DELETE &&
4887 staged_status != GOT_STATUS_DELETE) {
4888 if (dirfd != -1) {
4889 if (fstatat(dirfd, de_name, &sb,
4890 AT_SYMLINK_NOFOLLOW) == -1) {
4891 err = got_error_from_errno2("fstatat",
4892 ct->ondisk_path);
4893 goto done;
4895 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4896 err = got_error_from_errno2("lstat", ct->ondisk_path);
4897 goto done;
4899 ct->mode = sb.st_mode;
4902 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4903 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4904 relpath) == -1) {
4905 err = got_error_from_errno("asprintf");
4906 goto done;
4909 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4910 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4911 int is_bad_symlink;
4912 char target_path[PATH_MAX];
4913 ssize_t target_len;
4914 target_len = readlink(ct->ondisk_path, target_path,
4915 sizeof(target_path));
4916 if (target_len == -1) {
4917 err = got_error_from_errno2("readlink",
4918 ct->ondisk_path);
4919 goto done;
4921 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4922 target_len, ct->ondisk_path, a->worktree->root_path);
4923 if (err)
4924 goto done;
4925 if (is_bad_symlink) {
4926 err = got_error_path(ct->ondisk_path,
4927 GOT_ERR_BAD_SYMLINK);
4928 goto done;
4933 ct->status = status;
4934 ct->staged_status = staged_status;
4935 ct->blob_id = NULL; /* will be filled in when blob gets created */
4936 if (ct->status != GOT_STATUS_ADD &&
4937 ct->staged_status != GOT_STATUS_ADD) {
4938 ct->base_blob_id = got_object_id_dup(blob_id);
4939 if (ct->base_blob_id == NULL) {
4940 err = got_error_from_errno("got_object_id_dup");
4941 goto done;
4943 ct->base_commit_id = got_object_id_dup(commit_id);
4944 if (ct->base_commit_id == NULL) {
4945 err = got_error_from_errno("got_object_id_dup");
4946 goto done;
4949 if (ct->staged_status == GOT_STATUS_ADD ||
4950 ct->staged_status == GOT_STATUS_MODIFY) {
4951 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4952 if (ct->staged_blob_id == NULL) {
4953 err = got_error_from_errno("got_object_id_dup");
4954 goto done;
4957 ct->path = strdup(path);
4958 if (ct->path == NULL) {
4959 err = got_error_from_errno("strdup");
4960 goto done;
4962 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4963 done:
4964 if (ct && (err || new == NULL))
4965 free_commitable(ct);
4966 free(parent_path);
4967 free(path);
4968 return err;
4971 static const struct got_error *write_tree(struct got_object_id **, int *,
4972 struct got_tree_object *, const char *, struct got_pathlist_head *,
4973 got_worktree_status_cb status_cb, void *status_arg,
4974 struct got_repository *);
4976 static const struct got_error *
4977 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4978 struct got_tree_entry *te, const char *parent_path,
4979 struct got_pathlist_head *commitable_paths,
4980 got_worktree_status_cb status_cb, void *status_arg,
4981 struct got_repository *repo)
4983 const struct got_error *err = NULL;
4984 struct got_tree_object *subtree;
4985 char *subpath;
4987 if (asprintf(&subpath, "%s%s%s", parent_path,
4988 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4989 return got_error_from_errno("asprintf");
4991 err = got_object_open_as_tree(&subtree, repo, &te->id);
4992 if (err)
4993 return err;
4995 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4996 commitable_paths, status_cb, status_arg, repo);
4997 got_object_tree_close(subtree);
4998 free(subpath);
4999 return err;
5002 static const struct got_error *
5003 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5005 const struct got_error *err = NULL;
5006 char *ct_parent_path = NULL;
5008 *match = 0;
5010 if (strchr(ct->in_repo_path, '/') == NULL) {
5011 *match = got_path_is_root_dir(path);
5012 return NULL;
5015 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5016 if (err)
5017 return err;
5018 *match = (strcmp(path, ct_parent_path) == 0);
5019 free(ct_parent_path);
5020 return err;
5023 static mode_t
5024 get_ct_file_mode(struct got_commitable *ct)
5026 if (S_ISLNK(ct->mode))
5027 return S_IFLNK;
5029 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5032 static const struct got_error *
5033 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5034 struct got_tree_entry *te, struct got_commitable *ct)
5036 const struct got_error *err = NULL;
5038 *new_te = NULL;
5040 err = got_object_tree_entry_dup(new_te, te);
5041 if (err)
5042 goto done;
5044 (*new_te)->mode = get_ct_file_mode(ct);
5046 if (ct->staged_status == GOT_STATUS_MODIFY)
5047 memcpy(&(*new_te)->id, ct->staged_blob_id,
5048 sizeof((*new_te)->id));
5049 else
5050 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5051 done:
5052 if (err && *new_te) {
5053 free(*new_te);
5054 *new_te = NULL;
5056 return err;
5059 static const struct got_error *
5060 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5061 struct got_commitable *ct)
5063 const struct got_error *err = NULL;
5064 char *ct_name = NULL;
5066 *new_te = NULL;
5068 *new_te = calloc(1, sizeof(**new_te));
5069 if (*new_te == NULL)
5070 return got_error_from_errno("calloc");
5072 err = got_path_basename(&ct_name, ct->path);
5073 if (err)
5074 goto done;
5075 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5076 sizeof((*new_te)->name)) {
5077 err = got_error(GOT_ERR_NO_SPACE);
5078 goto done;
5081 (*new_te)->mode = get_ct_file_mode(ct);
5083 if (ct->staged_status == GOT_STATUS_ADD)
5084 memcpy(&(*new_te)->id, ct->staged_blob_id,
5085 sizeof((*new_te)->id));
5086 else
5087 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5088 done:
5089 free(ct_name);
5090 if (err && *new_te) {
5091 free(*new_te);
5092 *new_te = NULL;
5094 return err;
5097 static const struct got_error *
5098 insert_tree_entry(struct got_tree_entry *new_te,
5099 struct got_pathlist_head *paths)
5101 const struct got_error *err = NULL;
5102 struct got_pathlist_entry *new_pe;
5104 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5105 if (err)
5106 return err;
5107 if (new_pe == NULL)
5108 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5109 return NULL;
5112 static const struct got_error *
5113 report_ct_status(struct got_commitable *ct,
5114 got_worktree_status_cb status_cb, void *status_arg)
5116 const char *ct_path = ct->path;
5117 unsigned char status;
5119 while (ct_path[0] == '/')
5120 ct_path++;
5122 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5123 status = ct->staged_status;
5124 else
5125 status = ct->status;
5127 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5128 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5131 static const struct got_error *
5132 match_modified_subtree(int *modified, struct got_tree_entry *te,
5133 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5135 const struct got_error *err = NULL;
5136 struct got_pathlist_entry *pe;
5137 char *te_path;
5139 *modified = 0;
5141 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5142 got_path_is_root_dir(base_tree_path) ? "" : "/",
5143 te->name) == -1)
5144 return got_error_from_errno("asprintf");
5146 TAILQ_FOREACH(pe, commitable_paths, entry) {
5147 struct got_commitable *ct = pe->data;
5148 *modified = got_path_is_child(ct->in_repo_path, te_path,
5149 strlen(te_path));
5150 if (*modified)
5151 break;
5154 free(te_path);
5155 return err;
5158 static const struct got_error *
5159 match_deleted_or_modified_ct(struct got_commitable **ctp,
5160 struct got_tree_entry *te, const char *base_tree_path,
5161 struct got_pathlist_head *commitable_paths)
5163 const struct got_error *err = NULL;
5164 struct got_pathlist_entry *pe;
5166 *ctp = NULL;
5168 TAILQ_FOREACH(pe, commitable_paths, entry) {
5169 struct got_commitable *ct = pe->data;
5170 char *ct_name = NULL;
5171 int path_matches;
5173 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5174 if (ct->status != GOT_STATUS_MODIFY &&
5175 ct->status != GOT_STATUS_MODE_CHANGE &&
5176 ct->status != GOT_STATUS_DELETE)
5177 continue;
5178 } else {
5179 if (ct->staged_status != GOT_STATUS_MODIFY &&
5180 ct->staged_status != GOT_STATUS_DELETE)
5181 continue;
5184 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5185 continue;
5187 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5188 if (err)
5189 return err;
5190 if (!path_matches)
5191 continue;
5193 err = got_path_basename(&ct_name, pe->path);
5194 if (err)
5195 return err;
5197 if (strcmp(te->name, ct_name) != 0) {
5198 free(ct_name);
5199 continue;
5201 free(ct_name);
5203 *ctp = ct;
5204 break;
5207 return err;
5210 static const struct got_error *
5211 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5212 const char *child_path, const char *path_base_tree,
5213 struct got_pathlist_head *commitable_paths,
5214 got_worktree_status_cb status_cb, void *status_arg,
5215 struct got_repository *repo)
5217 const struct got_error *err = NULL;
5218 struct got_tree_entry *new_te;
5219 char *subtree_path;
5220 struct got_object_id *id = NULL;
5221 int nentries;
5223 *new_tep = NULL;
5225 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5226 got_path_is_root_dir(path_base_tree) ? "" : "/",
5227 child_path) == -1)
5228 return got_error_from_errno("asprintf");
5230 new_te = calloc(1, sizeof(*new_te));
5231 if (new_te == NULL)
5232 return got_error_from_errno("calloc");
5233 new_te->mode = S_IFDIR;
5235 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5236 sizeof(new_te->name)) {
5237 err = got_error(GOT_ERR_NO_SPACE);
5238 goto done;
5240 err = write_tree(&id, &nentries, NULL, subtree_path,
5241 commitable_paths, status_cb, status_arg, repo);
5242 if (err) {
5243 free(new_te);
5244 goto done;
5246 memcpy(&new_te->id, id, sizeof(new_te->id));
5247 done:
5248 free(id);
5249 free(subtree_path);
5250 if (err == NULL)
5251 *new_tep = new_te;
5252 return err;
5255 static const struct got_error *
5256 write_tree(struct got_object_id **new_tree_id, int *nentries,
5257 struct got_tree_object *base_tree, const char *path_base_tree,
5258 struct got_pathlist_head *commitable_paths,
5259 got_worktree_status_cb status_cb, void *status_arg,
5260 struct got_repository *repo)
5262 const struct got_error *err = NULL;
5263 struct got_pathlist_head paths;
5264 struct got_tree_entry *te, *new_te = NULL;
5265 struct got_pathlist_entry *pe;
5267 TAILQ_INIT(&paths);
5268 *nentries = 0;
5270 /* Insert, and recurse into, newly added entries first. */
5271 TAILQ_FOREACH(pe, commitable_paths, entry) {
5272 struct got_commitable *ct = pe->data;
5273 char *child_path = NULL, *slash;
5275 if ((ct->status != GOT_STATUS_ADD &&
5276 ct->staged_status != GOT_STATUS_ADD) ||
5277 (ct->flags & GOT_COMMITABLE_ADDED))
5278 continue;
5280 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5281 strlen(path_base_tree)))
5282 continue;
5284 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5285 ct->in_repo_path);
5286 if (err)
5287 goto done;
5289 slash = strchr(child_path, '/');
5290 if (slash == NULL) {
5291 err = alloc_added_blob_tree_entry(&new_te, ct);
5292 if (err)
5293 goto done;
5294 err = report_ct_status(ct, status_cb, status_arg);
5295 if (err)
5296 goto done;
5297 ct->flags |= GOT_COMMITABLE_ADDED;
5298 err = insert_tree_entry(new_te, &paths);
5299 if (err)
5300 goto done;
5301 (*nentries)++;
5302 } else {
5303 *slash = '\0'; /* trim trailing path components */
5304 if (base_tree == NULL ||
5305 got_object_tree_find_entry(base_tree, child_path)
5306 == NULL) {
5307 err = make_subtree_for_added_blob(&new_te,
5308 child_path, path_base_tree,
5309 commitable_paths, status_cb, status_arg,
5310 repo);
5311 if (err)
5312 goto done;
5313 err = insert_tree_entry(new_te, &paths);
5314 if (err)
5315 goto done;
5316 (*nentries)++;
5321 if (base_tree) {
5322 int i, nbase_entries;
5323 /* Handle modified and deleted entries. */
5324 nbase_entries = got_object_tree_get_nentries(base_tree);
5325 for (i = 0; i < nbase_entries; i++) {
5326 struct got_commitable *ct = NULL;
5328 te = got_object_tree_get_entry(base_tree, i);
5329 if (got_object_tree_entry_is_submodule(te)) {
5330 /* Entry is a submodule; just copy it. */
5331 err = got_object_tree_entry_dup(&new_te, te);
5332 if (err)
5333 goto done;
5334 err = insert_tree_entry(new_te, &paths);
5335 if (err)
5336 goto done;
5337 (*nentries)++;
5338 continue;
5341 if (S_ISDIR(te->mode)) {
5342 int modified;
5343 err = got_object_tree_entry_dup(&new_te, te);
5344 if (err)
5345 goto done;
5346 err = match_modified_subtree(&modified, te,
5347 path_base_tree, commitable_paths);
5348 if (err)
5349 goto done;
5350 /* Avoid recursion into unmodified subtrees. */
5351 if (modified) {
5352 struct got_object_id *new_id;
5353 int nsubentries;
5354 err = write_subtree(&new_id,
5355 &nsubentries, te,
5356 path_base_tree, commitable_paths,
5357 status_cb, status_arg, repo);
5358 if (err)
5359 goto done;
5360 if (nsubentries == 0) {
5361 /* All entries were deleted. */
5362 free(new_id);
5363 continue;
5365 memcpy(&new_te->id, new_id,
5366 sizeof(new_te->id));
5367 free(new_id);
5369 err = insert_tree_entry(new_te, &paths);
5370 if (err)
5371 goto done;
5372 (*nentries)++;
5373 continue;
5376 err = match_deleted_or_modified_ct(&ct, te,
5377 path_base_tree, commitable_paths);
5378 if (err)
5379 goto done;
5380 if (ct) {
5381 /* NB: Deleted entries get dropped here. */
5382 if (ct->status == GOT_STATUS_MODIFY ||
5383 ct->status == GOT_STATUS_MODE_CHANGE ||
5384 ct->staged_status == GOT_STATUS_MODIFY) {
5385 err = alloc_modified_blob_tree_entry(
5386 &new_te, te, ct);
5387 if (err)
5388 goto done;
5389 err = insert_tree_entry(new_te, &paths);
5390 if (err)
5391 goto done;
5392 (*nentries)++;
5394 err = report_ct_status(ct, status_cb,
5395 status_arg);
5396 if (err)
5397 goto done;
5398 } else {
5399 /* Entry is unchanged; just copy it. */
5400 err = got_object_tree_entry_dup(&new_te, te);
5401 if (err)
5402 goto done;
5403 err = insert_tree_entry(new_te, &paths);
5404 if (err)
5405 goto done;
5406 (*nentries)++;
5411 /* Write new list of entries; deleted entries have been dropped. */
5412 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5413 done:
5414 got_pathlist_free(&paths);
5415 return err;
5418 static const struct got_error *
5419 update_fileindex_after_commit(struct got_worktree *worktree,
5420 struct got_pathlist_head *commitable_paths,
5421 struct got_object_id *new_base_commit_id,
5422 struct got_fileindex *fileindex, int have_staged_files)
5424 const struct got_error *err = NULL;
5425 struct got_pathlist_entry *pe;
5426 char *relpath = NULL;
5428 TAILQ_FOREACH(pe, commitable_paths, entry) {
5429 struct got_fileindex_entry *ie;
5430 struct got_commitable *ct = pe->data;
5432 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5434 err = got_path_skip_common_ancestor(&relpath,
5435 worktree->root_path, ct->ondisk_path);
5436 if (err)
5437 goto done;
5439 if (ie) {
5440 if (ct->status == GOT_STATUS_DELETE ||
5441 ct->staged_status == GOT_STATUS_DELETE) {
5442 got_fileindex_entry_remove(fileindex, ie);
5443 } else if (ct->staged_status == GOT_STATUS_ADD ||
5444 ct->staged_status == GOT_STATUS_MODIFY) {
5445 got_fileindex_entry_stage_set(ie,
5446 GOT_FILEIDX_STAGE_NONE);
5447 got_fileindex_entry_staged_filetype_set(ie, 0);
5449 err = got_fileindex_entry_update(ie,
5450 worktree->root_fd, relpath,
5451 ct->staged_blob_id->sha1,
5452 new_base_commit_id->sha1,
5453 !have_staged_files);
5454 } else
5455 err = got_fileindex_entry_update(ie,
5456 worktree->root_fd, relpath,
5457 ct->blob_id->sha1,
5458 new_base_commit_id->sha1,
5459 !have_staged_files);
5460 } else {
5461 err = got_fileindex_entry_alloc(&ie, pe->path);
5462 if (err)
5463 goto done;
5464 err = got_fileindex_entry_update(ie,
5465 worktree->root_fd, relpath, ct->blob_id->sha1,
5466 new_base_commit_id->sha1, 1);
5467 if (err) {
5468 got_fileindex_entry_free(ie);
5469 goto done;
5471 err = got_fileindex_entry_add(fileindex, ie);
5472 if (err) {
5473 got_fileindex_entry_free(ie);
5474 goto done;
5477 free(relpath);
5478 relpath = NULL;
5480 done:
5481 free(relpath);
5482 return err;
5486 static const struct got_error *
5487 check_out_of_date(const char *in_repo_path, unsigned char status,
5488 unsigned char staged_status, struct got_object_id *base_blob_id,
5489 struct got_object_id *base_commit_id,
5490 struct got_object_id *head_commit_id, struct got_repository *repo,
5491 int ood_errcode)
5493 const struct got_error *err = NULL;
5494 struct got_object_id *id = NULL;
5496 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5497 /* Trivial case: base commit == head commit */
5498 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5499 return NULL;
5501 * Ensure file content which local changes were based
5502 * on matches file content in the branch head.
5504 err = got_object_id_by_path(&id, repo, head_commit_id,
5505 in_repo_path);
5506 if (err) {
5507 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5508 err = got_error(ood_errcode);
5509 goto done;
5510 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5511 err = got_error(ood_errcode);
5512 } else {
5513 /* Require that added files don't exist in the branch head. */
5514 err = got_object_id_by_path(&id, repo, head_commit_id,
5515 in_repo_path);
5516 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5517 goto done;
5518 err = id ? got_error(ood_errcode) : NULL;
5520 done:
5521 free(id);
5522 return err;
5525 const struct got_error *
5526 commit_worktree(struct got_object_id **new_commit_id,
5527 struct got_pathlist_head *commitable_paths,
5528 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5529 const char *author, const char *committer,
5530 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5531 got_worktree_status_cb status_cb, void *status_arg,
5532 struct got_repository *repo)
5534 const struct got_error *err = NULL, *unlockerr = NULL;
5535 struct got_pathlist_entry *pe;
5536 const char *head_ref_name = NULL;
5537 struct got_commit_object *head_commit = NULL;
5538 struct got_reference *head_ref2 = NULL;
5539 struct got_object_id *head_commit_id2 = NULL;
5540 struct got_tree_object *head_tree = NULL;
5541 struct got_object_id *new_tree_id = NULL;
5542 int nentries;
5543 struct got_object_id_queue parent_ids;
5544 struct got_object_qid *pid = NULL;
5545 char *logmsg = NULL;
5547 *new_commit_id = NULL;
5549 SIMPLEQ_INIT(&parent_ids);
5551 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5552 if (err)
5553 goto done;
5555 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5556 if (err)
5557 goto done;
5559 if (commit_msg_cb != NULL) {
5560 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5561 if (err)
5562 goto done;
5565 if (logmsg == NULL || strlen(logmsg) == 0) {
5566 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5567 goto done;
5570 /* Create blobs from added and modified files and record their IDs. */
5571 TAILQ_FOREACH(pe, commitable_paths, entry) {
5572 struct got_commitable *ct = pe->data;
5573 char *ondisk_path;
5575 /* Blobs for staged files already exist. */
5576 if (ct->staged_status == GOT_STATUS_ADD ||
5577 ct->staged_status == GOT_STATUS_MODIFY)
5578 continue;
5580 if (ct->status != GOT_STATUS_ADD &&
5581 ct->status != GOT_STATUS_MODIFY &&
5582 ct->status != GOT_STATUS_MODE_CHANGE)
5583 continue;
5585 if (asprintf(&ondisk_path, "%s/%s",
5586 worktree->root_path, pe->path) == -1) {
5587 err = got_error_from_errno("asprintf");
5588 goto done;
5590 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5591 free(ondisk_path);
5592 if (err)
5593 goto done;
5596 /* Recursively write new tree objects. */
5597 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5598 commitable_paths, status_cb, status_arg, repo);
5599 if (err)
5600 goto done;
5602 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5603 if (err)
5604 goto done;
5605 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5606 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5607 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5608 got_object_qid_free(pid);
5609 if (logmsg != NULL)
5610 free(logmsg);
5611 if (err)
5612 goto done;
5614 /* Check if a concurrent commit to our branch has occurred. */
5615 head_ref_name = got_worktree_get_head_ref_name(worktree);
5616 if (head_ref_name == NULL) {
5617 err = got_error_from_errno("got_worktree_get_head_ref_name");
5618 goto done;
5620 /* Lock the reference here to prevent concurrent modification. */
5621 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5622 if (err)
5623 goto done;
5624 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5625 if (err)
5626 goto done;
5627 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5628 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5629 goto done;
5631 /* Update branch head in repository. */
5632 err = got_ref_change_ref(head_ref2, *new_commit_id);
5633 if (err)
5634 goto done;
5635 err = got_ref_write(head_ref2, repo);
5636 if (err)
5637 goto done;
5639 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5640 if (err)
5641 goto done;
5643 err = ref_base_commit(worktree, repo);
5644 if (err)
5645 goto done;
5646 done:
5647 if (head_tree)
5648 got_object_tree_close(head_tree);
5649 if (head_commit)
5650 got_object_commit_close(head_commit);
5651 free(head_commit_id2);
5652 if (head_ref2) {
5653 unlockerr = got_ref_unlock(head_ref2);
5654 if (unlockerr && err == NULL)
5655 err = unlockerr;
5656 got_ref_close(head_ref2);
5658 return err;
5661 static const struct got_error *
5662 check_path_is_commitable(const char *path,
5663 struct got_pathlist_head *commitable_paths)
5665 struct got_pathlist_entry *cpe = NULL;
5666 size_t path_len = strlen(path);
5668 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5669 struct got_commitable *ct = cpe->data;
5670 const char *ct_path = ct->path;
5672 while (ct_path[0] == '/')
5673 ct_path++;
5675 if (strcmp(path, ct_path) == 0 ||
5676 got_path_is_child(ct_path, path, path_len))
5677 break;
5680 if (cpe == NULL)
5681 return got_error_path(path, GOT_ERR_BAD_PATH);
5683 return NULL;
5686 static const struct got_error *
5687 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5689 int *have_staged_files = arg;
5691 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5692 *have_staged_files = 1;
5693 return got_error(GOT_ERR_CANCELLED);
5696 return NULL;
5699 static const struct got_error *
5700 check_non_staged_files(struct got_fileindex *fileindex,
5701 struct got_pathlist_head *paths)
5703 struct got_pathlist_entry *pe;
5704 struct got_fileindex_entry *ie;
5706 TAILQ_FOREACH(pe, paths, entry) {
5707 if (pe->path[0] == '\0')
5708 continue;
5709 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5710 if (ie == NULL)
5711 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5712 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5713 return got_error_path(pe->path,
5714 GOT_ERR_FILE_NOT_STAGED);
5717 return NULL;
5720 const struct got_error *
5721 got_worktree_commit(struct got_object_id **new_commit_id,
5722 struct got_worktree *worktree, struct got_pathlist_head *paths,
5723 const char *author, const char *committer, int allow_bad_symlinks,
5724 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5725 got_worktree_status_cb status_cb, void *status_arg,
5726 struct got_repository *repo)
5728 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5729 struct got_fileindex *fileindex = NULL;
5730 char *fileindex_path = NULL;
5731 struct got_pathlist_head commitable_paths;
5732 struct collect_commitables_arg cc_arg;
5733 struct got_pathlist_entry *pe;
5734 struct got_reference *head_ref = NULL;
5735 struct got_object_id *head_commit_id = NULL;
5736 int have_staged_files = 0;
5738 *new_commit_id = NULL;
5740 TAILQ_INIT(&commitable_paths);
5742 err = lock_worktree(worktree, LOCK_EX);
5743 if (err)
5744 goto done;
5746 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5747 if (err)
5748 goto done;
5750 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5751 if (err)
5752 goto done;
5754 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5755 if (err)
5756 goto done;
5758 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5759 &have_staged_files);
5760 if (err && err->code != GOT_ERR_CANCELLED)
5761 goto done;
5762 if (have_staged_files) {
5763 err = check_non_staged_files(fileindex, paths);
5764 if (err)
5765 goto done;
5768 cc_arg.commitable_paths = &commitable_paths;
5769 cc_arg.worktree = worktree;
5770 cc_arg.fileindex = fileindex;
5771 cc_arg.repo = repo;
5772 cc_arg.have_staged_files = have_staged_files;
5773 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5774 TAILQ_FOREACH(pe, paths, entry) {
5775 err = worktree_status(worktree, pe->path, fileindex, repo,
5776 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5777 if (err)
5778 goto done;
5781 if (TAILQ_EMPTY(&commitable_paths)) {
5782 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5783 goto done;
5786 TAILQ_FOREACH(pe, paths, entry) {
5787 err = check_path_is_commitable(pe->path, &commitable_paths);
5788 if (err)
5789 goto done;
5792 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5793 struct got_commitable *ct = pe->data;
5794 const char *ct_path = ct->in_repo_path;
5796 while (ct_path[0] == '/')
5797 ct_path++;
5798 err = check_out_of_date(ct_path, ct->status,
5799 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5800 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5801 if (err)
5802 goto done;
5806 err = commit_worktree(new_commit_id, &commitable_paths,
5807 head_commit_id, worktree, author, committer,
5808 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5809 if (err)
5810 goto done;
5812 err = update_fileindex_after_commit(worktree, &commitable_paths,
5813 *new_commit_id, fileindex, have_staged_files);
5814 sync_err = sync_fileindex(fileindex, fileindex_path);
5815 if (sync_err && err == NULL)
5816 err = sync_err;
5817 done:
5818 if (fileindex)
5819 got_fileindex_free(fileindex);
5820 free(fileindex_path);
5821 unlockerr = lock_worktree(worktree, LOCK_SH);
5822 if (unlockerr && err == NULL)
5823 err = unlockerr;
5824 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5825 struct got_commitable *ct = pe->data;
5826 free_commitable(ct);
5828 got_pathlist_free(&commitable_paths);
5829 return err;
5832 const char *
5833 got_commitable_get_path(struct got_commitable *ct)
5835 return ct->path;
5838 unsigned int
5839 got_commitable_get_status(struct got_commitable *ct)
5841 return ct->status;
5844 struct check_rebase_ok_arg {
5845 struct got_worktree *worktree;
5846 struct got_repository *repo;
5849 static const struct got_error *
5850 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5852 const struct got_error *err = NULL;
5853 struct check_rebase_ok_arg *a = arg;
5854 unsigned char status;
5855 struct stat sb;
5856 char *ondisk_path;
5858 /* Reject rebase of a work tree with mixed base commits. */
5859 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5860 SHA1_DIGEST_LENGTH))
5861 return got_error(GOT_ERR_MIXED_COMMITS);
5863 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5864 == -1)
5865 return got_error_from_errno("asprintf");
5867 /* Reject rebase of a work tree with modified or staged files. */
5868 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5869 free(ondisk_path);
5870 if (err)
5871 return err;
5873 if (status != GOT_STATUS_NO_CHANGE)
5874 return got_error(GOT_ERR_MODIFIED);
5875 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5876 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5878 return NULL;
5881 const struct got_error *
5882 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5883 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5884 struct got_worktree *worktree, struct got_reference *branch,
5885 struct got_repository *repo)
5887 const struct got_error *err = NULL;
5888 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5889 char *branch_ref_name = NULL;
5890 char *fileindex_path = NULL;
5891 struct check_rebase_ok_arg ok_arg;
5892 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5893 struct got_object_id *wt_branch_tip = NULL;
5895 *new_base_branch_ref = NULL;
5896 *tmp_branch = NULL;
5897 *fileindex = NULL;
5899 err = lock_worktree(worktree, LOCK_EX);
5900 if (err)
5901 return err;
5903 err = open_fileindex(fileindex, &fileindex_path, worktree);
5904 if (err)
5905 goto done;
5907 ok_arg.worktree = worktree;
5908 ok_arg.repo = repo;
5909 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5910 &ok_arg);
5911 if (err)
5912 goto done;
5914 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5915 if (err)
5916 goto done;
5918 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5919 if (err)
5920 goto done;
5922 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5923 if (err)
5924 goto done;
5926 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5927 0);
5928 if (err)
5929 goto done;
5931 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5932 if (err)
5933 goto done;
5934 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5935 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5936 goto done;
5939 err = got_ref_alloc_symref(new_base_branch_ref,
5940 new_base_branch_ref_name, wt_branch);
5941 if (err)
5942 goto done;
5943 err = got_ref_write(*new_base_branch_ref, repo);
5944 if (err)
5945 goto done;
5947 /* TODO Lock original branch's ref while rebasing? */
5949 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5950 if (err)
5951 goto done;
5953 err = got_ref_write(branch_ref, repo);
5954 if (err)
5955 goto done;
5957 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5958 worktree->base_commit_id);
5959 if (err)
5960 goto done;
5961 err = got_ref_write(*tmp_branch, repo);
5962 if (err)
5963 goto done;
5965 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5966 if (err)
5967 goto done;
5968 done:
5969 free(fileindex_path);
5970 free(tmp_branch_name);
5971 free(new_base_branch_ref_name);
5972 free(branch_ref_name);
5973 if (branch_ref)
5974 got_ref_close(branch_ref);
5975 if (wt_branch)
5976 got_ref_close(wt_branch);
5977 free(wt_branch_tip);
5978 if (err) {
5979 if (*new_base_branch_ref) {
5980 got_ref_close(*new_base_branch_ref);
5981 *new_base_branch_ref = NULL;
5983 if (*tmp_branch) {
5984 got_ref_close(*tmp_branch);
5985 *tmp_branch = NULL;
5987 if (*fileindex) {
5988 got_fileindex_free(*fileindex);
5989 *fileindex = NULL;
5991 lock_worktree(worktree, LOCK_SH);
5993 return err;
5996 const struct got_error *
5997 got_worktree_rebase_continue(struct got_object_id **commit_id,
5998 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5999 struct got_reference **branch, struct got_fileindex **fileindex,
6000 struct got_worktree *worktree, struct got_repository *repo)
6002 const struct got_error *err;
6003 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6004 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6005 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6006 char *fileindex_path = NULL;
6007 int have_staged_files = 0;
6009 *commit_id = NULL;
6010 *new_base_branch = NULL;
6011 *tmp_branch = NULL;
6012 *branch = NULL;
6013 *fileindex = NULL;
6015 err = lock_worktree(worktree, LOCK_EX);
6016 if (err)
6017 return err;
6019 err = open_fileindex(fileindex, &fileindex_path, worktree);
6020 if (err)
6021 goto done;
6023 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6024 &have_staged_files);
6025 if (err && err->code != GOT_ERR_CANCELLED)
6026 goto done;
6027 if (have_staged_files) {
6028 err = got_error(GOT_ERR_STAGED_PATHS);
6029 goto done;
6032 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6033 if (err)
6034 goto done;
6036 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6037 if (err)
6038 goto done;
6040 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6041 if (err)
6042 goto done;
6044 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6045 if (err)
6046 goto done;
6048 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6049 if (err)
6050 goto done;
6052 err = got_ref_open(branch, repo,
6053 got_ref_get_symref_target(branch_ref), 0);
6054 if (err)
6055 goto done;
6057 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6058 if (err)
6059 goto done;
6061 err = got_ref_resolve(commit_id, repo, commit_ref);
6062 if (err)
6063 goto done;
6065 err = got_ref_open(new_base_branch, repo,
6066 new_base_branch_ref_name, 0);
6067 if (err)
6068 goto done;
6070 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6071 if (err)
6072 goto done;
6073 done:
6074 free(commit_ref_name);
6075 free(branch_ref_name);
6076 free(fileindex_path);
6077 if (commit_ref)
6078 got_ref_close(commit_ref);
6079 if (branch_ref)
6080 got_ref_close(branch_ref);
6081 if (err) {
6082 free(*commit_id);
6083 *commit_id = NULL;
6084 if (*tmp_branch) {
6085 got_ref_close(*tmp_branch);
6086 *tmp_branch = NULL;
6088 if (*new_base_branch) {
6089 got_ref_close(*new_base_branch);
6090 *new_base_branch = NULL;
6092 if (*branch) {
6093 got_ref_close(*branch);
6094 *branch = NULL;
6096 if (*fileindex) {
6097 got_fileindex_free(*fileindex);
6098 *fileindex = NULL;
6100 lock_worktree(worktree, LOCK_SH);
6102 return err;
6105 const struct got_error *
6106 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6108 const struct got_error *err;
6109 char *tmp_branch_name = NULL;
6111 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6112 if (err)
6113 return err;
6115 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6116 free(tmp_branch_name);
6117 return NULL;
6120 static const struct got_error *
6121 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6122 char **logmsg, void *arg)
6124 *logmsg = arg;
6125 return NULL;
6128 static const struct got_error *
6129 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6130 const char *path, struct got_object_id *blob_id,
6131 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6132 int dirfd, const char *de_name)
6134 return NULL;
6137 struct collect_merged_paths_arg {
6138 got_worktree_checkout_cb progress_cb;
6139 void *progress_arg;
6140 struct got_pathlist_head *merged_paths;
6143 static const struct got_error *
6144 collect_merged_paths(void *arg, unsigned char status, const char *path)
6146 const struct got_error *err;
6147 struct collect_merged_paths_arg *a = arg;
6148 char *p;
6149 struct got_pathlist_entry *new;
6151 err = (*a->progress_cb)(a->progress_arg, status, path);
6152 if (err)
6153 return err;
6155 if (status != GOT_STATUS_MERGE &&
6156 status != GOT_STATUS_ADD &&
6157 status != GOT_STATUS_DELETE &&
6158 status != GOT_STATUS_CONFLICT)
6159 return NULL;
6161 p = strdup(path);
6162 if (p == NULL)
6163 return got_error_from_errno("strdup");
6165 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6166 if (err || new == NULL)
6167 free(p);
6168 return err;
6171 void
6172 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6174 struct got_pathlist_entry *pe;
6176 TAILQ_FOREACH(pe, merged_paths, entry)
6177 free((char *)pe->path);
6179 got_pathlist_free(merged_paths);
6182 static const struct got_error *
6183 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6184 int is_rebase, struct got_repository *repo)
6186 const struct got_error *err;
6187 struct got_reference *commit_ref = NULL;
6189 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6190 if (err) {
6191 if (err->code != GOT_ERR_NOT_REF)
6192 goto done;
6193 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6194 if (err)
6195 goto done;
6196 err = got_ref_write(commit_ref, repo);
6197 if (err)
6198 goto done;
6199 } else if (is_rebase) {
6200 struct got_object_id *stored_id;
6201 int cmp;
6203 err = got_ref_resolve(&stored_id, repo, commit_ref);
6204 if (err)
6205 goto done;
6206 cmp = got_object_id_cmp(commit_id, stored_id);
6207 free(stored_id);
6208 if (cmp != 0) {
6209 err = got_error(GOT_ERR_REBASE_COMMITID);
6210 goto done;
6213 done:
6214 if (commit_ref)
6215 got_ref_close(commit_ref);
6216 return err;
6219 static const struct got_error *
6220 rebase_merge_files(struct got_pathlist_head *merged_paths,
6221 const char *commit_ref_name, struct got_worktree *worktree,
6222 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6223 struct got_object_id *commit_id, struct got_repository *repo,
6224 got_worktree_checkout_cb progress_cb, void *progress_arg,
6225 got_cancel_cb cancel_cb, void *cancel_arg)
6227 const struct got_error *err;
6228 struct got_reference *commit_ref = NULL;
6229 struct collect_merged_paths_arg cmp_arg;
6230 char *fileindex_path;
6232 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6234 err = get_fileindex_path(&fileindex_path, worktree);
6235 if (err)
6236 return err;
6238 cmp_arg.progress_cb = progress_cb;
6239 cmp_arg.progress_arg = progress_arg;
6240 cmp_arg.merged_paths = merged_paths;
6241 err = merge_files(worktree, fileindex, fileindex_path,
6242 parent_commit_id, commit_id, repo, collect_merged_paths,
6243 &cmp_arg, cancel_cb, cancel_arg);
6244 if (commit_ref)
6245 got_ref_close(commit_ref);
6246 return err;
6249 const struct got_error *
6250 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6251 struct got_worktree *worktree, struct got_fileindex *fileindex,
6252 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6253 struct got_repository *repo,
6254 got_worktree_checkout_cb progress_cb, void *progress_arg,
6255 got_cancel_cb cancel_cb, void *cancel_arg)
6257 const struct got_error *err;
6258 char *commit_ref_name;
6260 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6261 if (err)
6262 return err;
6264 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6265 if (err)
6266 goto done;
6268 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6269 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6270 progress_arg, cancel_cb, cancel_arg);
6271 done:
6272 free(commit_ref_name);
6273 return err;
6276 const struct got_error *
6277 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6278 struct got_worktree *worktree, struct got_fileindex *fileindex,
6279 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6280 struct got_repository *repo,
6281 got_worktree_checkout_cb progress_cb, void *progress_arg,
6282 got_cancel_cb cancel_cb, void *cancel_arg)
6284 const struct got_error *err;
6285 char *commit_ref_name;
6287 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6288 if (err)
6289 return err;
6291 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6292 if (err)
6293 goto done;
6295 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6296 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6297 progress_arg, cancel_cb, cancel_arg);
6298 done:
6299 free(commit_ref_name);
6300 return err;
6303 static const struct got_error *
6304 rebase_commit(struct got_object_id **new_commit_id,
6305 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6306 struct got_worktree *worktree, struct got_fileindex *fileindex,
6307 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6308 const char *new_logmsg, struct got_repository *repo)
6310 const struct got_error *err, *sync_err;
6311 struct got_pathlist_head commitable_paths;
6312 struct collect_commitables_arg cc_arg;
6313 char *fileindex_path = NULL;
6314 struct got_reference *head_ref = NULL;
6315 struct got_object_id *head_commit_id = NULL;
6316 char *logmsg = NULL;
6318 TAILQ_INIT(&commitable_paths);
6319 *new_commit_id = NULL;
6321 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6323 err = get_fileindex_path(&fileindex_path, worktree);
6324 if (err)
6325 return err;
6327 cc_arg.commitable_paths = &commitable_paths;
6328 cc_arg.worktree = worktree;
6329 cc_arg.repo = repo;
6330 cc_arg.have_staged_files = 0;
6332 * If possible get the status of individual files directly to
6333 * avoid crawling the entire work tree once per rebased commit.
6334 * TODO: Ideally, merged_paths would contain a list of commitables
6335 * we could use so we could skip worktree_status() entirely.
6337 if (merged_paths) {
6338 struct got_pathlist_entry *pe;
6339 TAILQ_FOREACH(pe, merged_paths, entry) {
6340 err = worktree_status(worktree, pe->path, fileindex,
6341 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6342 0);
6343 if (err)
6344 goto done;
6346 } else {
6347 err = worktree_status(worktree, "", fileindex, repo,
6348 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6349 if (err)
6350 goto done;
6353 if (TAILQ_EMPTY(&commitable_paths)) {
6354 /* No-op change; commit will be elided. */
6355 err = got_ref_delete(commit_ref, repo);
6356 if (err)
6357 goto done;
6358 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6359 goto done;
6362 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6363 if (err)
6364 goto done;
6366 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6367 if (err)
6368 goto done;
6370 if (new_logmsg) {
6371 logmsg = strdup(new_logmsg);
6372 if (logmsg == NULL) {
6373 err = got_error_from_errno("strdup");
6374 goto done;
6376 } else {
6377 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6378 if (err)
6379 goto done;
6382 /* NB: commit_worktree will call free(logmsg) */
6383 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6384 worktree, got_object_commit_get_author(orig_commit),
6385 got_object_commit_get_committer(orig_commit),
6386 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6387 if (err)
6388 goto done;
6390 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6391 if (err)
6392 goto done;
6394 err = got_ref_delete(commit_ref, repo);
6395 if (err)
6396 goto done;
6398 err = update_fileindex_after_commit(worktree, &commitable_paths,
6399 *new_commit_id, fileindex, 0);
6400 sync_err = sync_fileindex(fileindex, fileindex_path);
6401 if (sync_err && err == NULL)
6402 err = sync_err;
6403 done:
6404 free(fileindex_path);
6405 free(head_commit_id);
6406 if (head_ref)
6407 got_ref_close(head_ref);
6408 if (err) {
6409 free(*new_commit_id);
6410 *new_commit_id = NULL;
6412 return err;
6415 const struct got_error *
6416 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6417 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6418 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6419 struct got_commit_object *orig_commit,
6420 struct got_object_id *orig_commit_id, struct got_repository *repo)
6422 const struct got_error *err;
6423 char *commit_ref_name;
6424 struct got_reference *commit_ref = NULL;
6425 struct got_object_id *commit_id = NULL;
6427 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6428 if (err)
6429 return err;
6431 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6432 if (err)
6433 goto done;
6434 err = got_ref_resolve(&commit_id, repo, commit_ref);
6435 if (err)
6436 goto done;
6437 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6438 err = got_error(GOT_ERR_REBASE_COMMITID);
6439 goto done;
6442 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6443 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6444 done:
6445 if (commit_ref)
6446 got_ref_close(commit_ref);
6447 free(commit_ref_name);
6448 free(commit_id);
6449 return err;
6452 const struct got_error *
6453 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6454 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6455 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6456 struct got_commit_object *orig_commit,
6457 struct got_object_id *orig_commit_id, const char *new_logmsg,
6458 struct got_repository *repo)
6460 const struct got_error *err;
6461 char *commit_ref_name;
6462 struct got_reference *commit_ref = NULL;
6464 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6465 if (err)
6466 return err;
6468 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6469 if (err)
6470 goto done;
6472 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6473 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6474 done:
6475 if (commit_ref)
6476 got_ref_close(commit_ref);
6477 free(commit_ref_name);
6478 return err;
6481 const struct got_error *
6482 got_worktree_rebase_postpone(struct got_worktree *worktree,
6483 struct got_fileindex *fileindex)
6485 if (fileindex)
6486 got_fileindex_free(fileindex);
6487 return lock_worktree(worktree, LOCK_SH);
6490 static const struct got_error *
6491 delete_ref(const char *name, struct got_repository *repo)
6493 const struct got_error *err;
6494 struct got_reference *ref;
6496 err = got_ref_open(&ref, repo, name, 0);
6497 if (err) {
6498 if (err->code == GOT_ERR_NOT_REF)
6499 return NULL;
6500 return err;
6503 err = got_ref_delete(ref, repo);
6504 got_ref_close(ref);
6505 return err;
6508 static const struct got_error *
6509 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6511 const struct got_error *err;
6512 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6513 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6515 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6516 if (err)
6517 goto done;
6518 err = delete_ref(tmp_branch_name, repo);
6519 if (err)
6520 goto done;
6522 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6523 if (err)
6524 goto done;
6525 err = delete_ref(new_base_branch_ref_name, repo);
6526 if (err)
6527 goto done;
6529 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6530 if (err)
6531 goto done;
6532 err = delete_ref(branch_ref_name, repo);
6533 if (err)
6534 goto done;
6536 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6537 if (err)
6538 goto done;
6539 err = delete_ref(commit_ref_name, repo);
6540 if (err)
6541 goto done;
6543 done:
6544 free(tmp_branch_name);
6545 free(new_base_branch_ref_name);
6546 free(branch_ref_name);
6547 free(commit_ref_name);
6548 return err;
6551 const struct got_error *
6552 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6553 struct got_object_id *new_commit_id, struct got_repository *repo)
6555 const struct got_error *err;
6556 struct got_reference *ref = NULL;
6557 struct got_object_id *old_commit_id = NULL;
6558 const char *branch_name = NULL;
6559 char *new_id_str = NULL;
6560 char *refname = NULL;
6562 branch_name = got_ref_get_name(branch);
6563 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6564 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6565 branch_name += 11;
6567 err = got_object_id_str(&new_id_str, new_commit_id);
6568 if (err)
6569 return err;
6571 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6572 new_id_str) == -1) {
6573 err = got_error_from_errno("asprintf");
6574 goto done;
6577 err = got_ref_resolve(&old_commit_id, repo, branch);
6578 if (err)
6579 goto done;
6581 err = got_ref_alloc(&ref, refname, old_commit_id);
6582 if (err)
6583 goto done;
6585 err = got_ref_write(ref, repo);
6586 done:
6587 free(new_id_str);
6588 free(refname);
6589 free(old_commit_id);
6590 if (ref)
6591 got_ref_close(ref);
6592 return err;
6595 const struct got_error *
6596 got_worktree_rebase_complete(struct got_worktree *worktree,
6597 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6598 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6599 struct got_repository *repo, int create_backup)
6601 const struct got_error *err, *unlockerr, *sync_err;
6602 struct got_object_id *new_head_commit_id = NULL;
6603 char *fileindex_path = NULL;
6605 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6606 if (err)
6607 return err;
6609 if (create_backup) {
6610 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6611 rebased_branch, new_head_commit_id, repo);
6612 if (err)
6613 goto done;
6616 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6617 if (err)
6618 goto done;
6620 err = got_ref_write(rebased_branch, repo);
6621 if (err)
6622 goto done;
6624 err = got_worktree_set_head_ref(worktree, rebased_branch);
6625 if (err)
6626 goto done;
6628 err = delete_rebase_refs(worktree, repo);
6629 if (err)
6630 goto done;
6632 err = get_fileindex_path(&fileindex_path, worktree);
6633 if (err)
6634 goto done;
6635 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6636 sync_err = sync_fileindex(fileindex, fileindex_path);
6637 if (sync_err && err == NULL)
6638 err = sync_err;
6639 done:
6640 got_fileindex_free(fileindex);
6641 free(fileindex_path);
6642 free(new_head_commit_id);
6643 unlockerr = lock_worktree(worktree, LOCK_SH);
6644 if (unlockerr && err == NULL)
6645 err = unlockerr;
6646 return err;
6649 const struct got_error *
6650 got_worktree_rebase_abort(struct got_worktree *worktree,
6651 struct got_fileindex *fileindex, struct got_repository *repo,
6652 struct got_reference *new_base_branch,
6653 got_worktree_checkout_cb progress_cb, void *progress_arg)
6655 const struct got_error *err, *unlockerr, *sync_err;
6656 struct got_reference *resolved = NULL;
6657 struct got_object_id *commit_id = NULL;
6658 char *fileindex_path = NULL;
6659 struct revert_file_args rfa;
6660 struct got_object_id *tree_id = NULL;
6662 err = lock_worktree(worktree, LOCK_EX);
6663 if (err)
6664 return err;
6666 err = got_ref_open(&resolved, repo,
6667 got_ref_get_symref_target(new_base_branch), 0);
6668 if (err)
6669 goto done;
6671 err = got_worktree_set_head_ref(worktree, resolved);
6672 if (err)
6673 goto done;
6676 * XXX commits to the base branch could have happened while
6677 * we were busy rebasing; should we store the original commit ID
6678 * when rebase begins and read it back here?
6680 err = got_ref_resolve(&commit_id, repo, resolved);
6681 if (err)
6682 goto done;
6684 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6685 if (err)
6686 goto done;
6688 err = got_object_id_by_path(&tree_id, repo,
6689 worktree->base_commit_id, worktree->path_prefix);
6690 if (err)
6691 goto done;
6693 err = delete_rebase_refs(worktree, repo);
6694 if (err)
6695 goto done;
6697 err = get_fileindex_path(&fileindex_path, worktree);
6698 if (err)
6699 goto done;
6701 rfa.worktree = worktree;
6702 rfa.fileindex = fileindex;
6703 rfa.progress_cb = progress_cb;
6704 rfa.progress_arg = progress_arg;
6705 rfa.patch_cb = NULL;
6706 rfa.patch_arg = NULL;
6707 rfa.repo = repo;
6708 err = worktree_status(worktree, "", fileindex, repo,
6709 revert_file, &rfa, NULL, NULL, 0, 0);
6710 if (err)
6711 goto sync;
6713 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6714 repo, progress_cb, progress_arg, NULL, NULL);
6715 sync:
6716 sync_err = sync_fileindex(fileindex, fileindex_path);
6717 if (sync_err && err == NULL)
6718 err = sync_err;
6719 done:
6720 got_ref_close(resolved);
6721 free(tree_id);
6722 free(commit_id);
6723 if (fileindex)
6724 got_fileindex_free(fileindex);
6725 free(fileindex_path);
6727 unlockerr = lock_worktree(worktree, LOCK_SH);
6728 if (unlockerr && err == NULL)
6729 err = unlockerr;
6730 return err;
6733 const struct got_error *
6734 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6735 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6736 struct got_fileindex **fileindex, struct got_worktree *worktree,
6737 struct got_repository *repo)
6739 const struct got_error *err = NULL;
6740 char *tmp_branch_name = NULL;
6741 char *branch_ref_name = NULL;
6742 char *base_commit_ref_name = NULL;
6743 char *fileindex_path = NULL;
6744 struct check_rebase_ok_arg ok_arg;
6745 struct got_reference *wt_branch = NULL;
6746 struct got_reference *base_commit_ref = NULL;
6748 *tmp_branch = NULL;
6749 *branch_ref = NULL;
6750 *base_commit_id = NULL;
6751 *fileindex = NULL;
6753 err = lock_worktree(worktree, LOCK_EX);
6754 if (err)
6755 return err;
6757 err = open_fileindex(fileindex, &fileindex_path, worktree);
6758 if (err)
6759 goto done;
6761 ok_arg.worktree = worktree;
6762 ok_arg.repo = repo;
6763 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6764 &ok_arg);
6765 if (err)
6766 goto done;
6768 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6769 if (err)
6770 goto done;
6772 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6773 if (err)
6774 goto done;
6776 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6777 worktree);
6778 if (err)
6779 goto done;
6781 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6782 0);
6783 if (err)
6784 goto done;
6786 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6787 if (err)
6788 goto done;
6790 err = got_ref_write(*branch_ref, repo);
6791 if (err)
6792 goto done;
6794 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6795 worktree->base_commit_id);
6796 if (err)
6797 goto done;
6798 err = got_ref_write(base_commit_ref, repo);
6799 if (err)
6800 goto done;
6801 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6802 if (*base_commit_id == NULL) {
6803 err = got_error_from_errno("got_object_id_dup");
6804 goto done;
6807 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6808 worktree->base_commit_id);
6809 if (err)
6810 goto done;
6811 err = got_ref_write(*tmp_branch, repo);
6812 if (err)
6813 goto done;
6815 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6816 if (err)
6817 goto done;
6818 done:
6819 free(fileindex_path);
6820 free(tmp_branch_name);
6821 free(branch_ref_name);
6822 free(base_commit_ref_name);
6823 if (wt_branch)
6824 got_ref_close(wt_branch);
6825 if (err) {
6826 if (*branch_ref) {
6827 got_ref_close(*branch_ref);
6828 *branch_ref = NULL;
6830 if (*tmp_branch) {
6831 got_ref_close(*tmp_branch);
6832 *tmp_branch = NULL;
6834 free(*base_commit_id);
6835 if (*fileindex) {
6836 got_fileindex_free(*fileindex);
6837 *fileindex = NULL;
6839 lock_worktree(worktree, LOCK_SH);
6841 return err;
6844 const struct got_error *
6845 got_worktree_histedit_postpone(struct got_worktree *worktree,
6846 struct got_fileindex *fileindex)
6848 if (fileindex)
6849 got_fileindex_free(fileindex);
6850 return lock_worktree(worktree, LOCK_SH);
6853 const struct got_error *
6854 got_worktree_histedit_in_progress(int *in_progress,
6855 struct got_worktree *worktree)
6857 const struct got_error *err;
6858 char *tmp_branch_name = NULL;
6860 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6861 if (err)
6862 return err;
6864 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6865 free(tmp_branch_name);
6866 return NULL;
6869 const struct got_error *
6870 got_worktree_histedit_continue(struct got_object_id **commit_id,
6871 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6872 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6873 struct got_worktree *worktree, struct got_repository *repo)
6875 const struct got_error *err;
6876 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6877 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6878 struct got_reference *commit_ref = NULL;
6879 struct got_reference *base_commit_ref = NULL;
6880 char *fileindex_path = NULL;
6881 int have_staged_files = 0;
6883 *commit_id = NULL;
6884 *tmp_branch = NULL;
6885 *base_commit_id = NULL;
6886 *fileindex = NULL;
6888 err = lock_worktree(worktree, LOCK_EX);
6889 if (err)
6890 return err;
6892 err = open_fileindex(fileindex, &fileindex_path, worktree);
6893 if (err)
6894 goto done;
6896 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6897 &have_staged_files);
6898 if (err && err->code != GOT_ERR_CANCELLED)
6899 goto done;
6900 if (have_staged_files) {
6901 err = got_error(GOT_ERR_STAGED_PATHS);
6902 goto done;
6905 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6906 if (err)
6907 goto done;
6909 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6910 if (err)
6911 goto done;
6913 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6914 if (err)
6915 goto done;
6917 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6918 worktree);
6919 if (err)
6920 goto done;
6922 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6923 if (err)
6924 goto done;
6926 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6927 if (err)
6928 goto done;
6929 err = got_ref_resolve(commit_id, repo, commit_ref);
6930 if (err)
6931 goto done;
6933 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6934 if (err)
6935 goto done;
6936 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6937 if (err)
6938 goto done;
6940 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6941 if (err)
6942 goto done;
6943 done:
6944 free(commit_ref_name);
6945 free(branch_ref_name);
6946 free(fileindex_path);
6947 if (commit_ref)
6948 got_ref_close(commit_ref);
6949 if (base_commit_ref)
6950 got_ref_close(base_commit_ref);
6951 if (err) {
6952 free(*commit_id);
6953 *commit_id = NULL;
6954 free(*base_commit_id);
6955 *base_commit_id = NULL;
6956 if (*tmp_branch) {
6957 got_ref_close(*tmp_branch);
6958 *tmp_branch = NULL;
6960 if (*fileindex) {
6961 got_fileindex_free(*fileindex);
6962 *fileindex = NULL;
6964 lock_worktree(worktree, LOCK_EX);
6966 return err;
6969 static const struct got_error *
6970 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6972 const struct got_error *err;
6973 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6974 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6976 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6977 if (err)
6978 goto done;
6979 err = delete_ref(tmp_branch_name, repo);
6980 if (err)
6981 goto done;
6983 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6984 worktree);
6985 if (err)
6986 goto done;
6987 err = delete_ref(base_commit_ref_name, repo);
6988 if (err)
6989 goto done;
6991 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6992 if (err)
6993 goto done;
6994 err = delete_ref(branch_ref_name, repo);
6995 if (err)
6996 goto done;
6998 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6999 if (err)
7000 goto done;
7001 err = delete_ref(commit_ref_name, repo);
7002 if (err)
7003 goto done;
7004 done:
7005 free(tmp_branch_name);
7006 free(base_commit_ref_name);
7007 free(branch_ref_name);
7008 free(commit_ref_name);
7009 return err;
7012 const struct got_error *
7013 got_worktree_histedit_abort(struct got_worktree *worktree,
7014 struct got_fileindex *fileindex, struct got_repository *repo,
7015 struct got_reference *branch, struct got_object_id *base_commit_id,
7016 got_worktree_checkout_cb progress_cb, void *progress_arg)
7018 const struct got_error *err, *unlockerr, *sync_err;
7019 struct got_reference *resolved = NULL;
7020 char *fileindex_path = NULL;
7021 struct got_object_id *tree_id = NULL;
7022 struct revert_file_args rfa;
7024 err = lock_worktree(worktree, LOCK_EX);
7025 if (err)
7026 return err;
7028 err = got_ref_open(&resolved, repo,
7029 got_ref_get_symref_target(branch), 0);
7030 if (err)
7031 goto done;
7033 err = got_worktree_set_head_ref(worktree, resolved);
7034 if (err)
7035 goto done;
7037 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7038 if (err)
7039 goto done;
7041 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7042 worktree->path_prefix);
7043 if (err)
7044 goto done;
7046 err = delete_histedit_refs(worktree, repo);
7047 if (err)
7048 goto done;
7050 err = get_fileindex_path(&fileindex_path, worktree);
7051 if (err)
7052 goto done;
7054 rfa.worktree = worktree;
7055 rfa.fileindex = fileindex;
7056 rfa.progress_cb = progress_cb;
7057 rfa.progress_arg = progress_arg;
7058 rfa.patch_cb = NULL;
7059 rfa.patch_arg = NULL;
7060 rfa.repo = repo;
7061 err = worktree_status(worktree, "", fileindex, repo,
7062 revert_file, &rfa, NULL, NULL, 0, 0);
7063 if (err)
7064 goto sync;
7066 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7067 repo, progress_cb, progress_arg, NULL, NULL);
7068 sync:
7069 sync_err = sync_fileindex(fileindex, fileindex_path);
7070 if (sync_err && err == NULL)
7071 err = sync_err;
7072 done:
7073 got_ref_close(resolved);
7074 free(tree_id);
7075 free(fileindex_path);
7077 unlockerr = lock_worktree(worktree, LOCK_SH);
7078 if (unlockerr && err == NULL)
7079 err = unlockerr;
7080 return err;
7083 const struct got_error *
7084 got_worktree_histedit_complete(struct got_worktree *worktree,
7085 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7086 struct got_reference *edited_branch, struct got_repository *repo)
7088 const struct got_error *err, *unlockerr, *sync_err;
7089 struct got_object_id *new_head_commit_id = NULL;
7090 struct got_reference *resolved = NULL;
7091 char *fileindex_path = NULL;
7093 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7094 if (err)
7095 return err;
7097 err = got_ref_open(&resolved, repo,
7098 got_ref_get_symref_target(edited_branch), 0);
7099 if (err)
7100 goto done;
7102 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7103 resolved, new_head_commit_id, repo);
7104 if (err)
7105 goto done;
7107 err = got_ref_change_ref(resolved, new_head_commit_id);
7108 if (err)
7109 goto done;
7111 err = got_ref_write(resolved, repo);
7112 if (err)
7113 goto done;
7115 err = got_worktree_set_head_ref(worktree, resolved);
7116 if (err)
7117 goto done;
7119 err = delete_histedit_refs(worktree, repo);
7120 if (err)
7121 goto done;
7123 err = get_fileindex_path(&fileindex_path, worktree);
7124 if (err)
7125 goto done;
7126 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7127 sync_err = sync_fileindex(fileindex, fileindex_path);
7128 if (sync_err && err == NULL)
7129 err = sync_err;
7130 done:
7131 got_fileindex_free(fileindex);
7132 free(fileindex_path);
7133 free(new_head_commit_id);
7134 unlockerr = lock_worktree(worktree, LOCK_SH);
7135 if (unlockerr && err == NULL)
7136 err = unlockerr;
7137 return err;
7140 const struct got_error *
7141 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7142 struct got_object_id *commit_id, struct got_repository *repo)
7144 const struct got_error *err;
7145 char *commit_ref_name;
7147 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7148 if (err)
7149 return err;
7151 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7152 if (err)
7153 goto done;
7155 err = delete_ref(commit_ref_name, repo);
7156 done:
7157 free(commit_ref_name);
7158 return err;
7161 const struct got_error *
7162 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7163 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7164 struct got_worktree *worktree, const char *refname,
7165 struct got_repository *repo)
7167 const struct got_error *err = NULL;
7168 char *fileindex_path = NULL;
7169 struct check_rebase_ok_arg ok_arg;
7171 *fileindex = NULL;
7172 *branch_ref = NULL;
7173 *base_branch_ref = NULL;
7175 err = lock_worktree(worktree, LOCK_EX);
7176 if (err)
7177 return err;
7179 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7180 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7181 "cannot integrate a branch into itself; "
7182 "update -b or different branch name required");
7183 goto done;
7186 err = open_fileindex(fileindex, &fileindex_path, worktree);
7187 if (err)
7188 goto done;
7190 /* Preconditions are the same as for rebase. */
7191 ok_arg.worktree = worktree;
7192 ok_arg.repo = repo;
7193 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7194 &ok_arg);
7195 if (err)
7196 goto done;
7198 err = got_ref_open(branch_ref, repo, refname, 1);
7199 if (err)
7200 goto done;
7202 err = got_ref_open(base_branch_ref, repo,
7203 got_worktree_get_head_ref_name(worktree), 1);
7204 done:
7205 if (err) {
7206 if (*branch_ref) {
7207 got_ref_close(*branch_ref);
7208 *branch_ref = NULL;
7210 if (*base_branch_ref) {
7211 got_ref_close(*base_branch_ref);
7212 *base_branch_ref = NULL;
7214 if (*fileindex) {
7215 got_fileindex_free(*fileindex);
7216 *fileindex = NULL;
7218 lock_worktree(worktree, LOCK_SH);
7220 return err;
7223 const struct got_error *
7224 got_worktree_integrate_continue(struct got_worktree *worktree,
7225 struct got_fileindex *fileindex, struct got_repository *repo,
7226 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7227 got_worktree_checkout_cb progress_cb, void *progress_arg,
7228 got_cancel_cb cancel_cb, void *cancel_arg)
7230 const struct got_error *err = NULL, *sync_err, *unlockerr;
7231 char *fileindex_path = NULL;
7232 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7234 err = get_fileindex_path(&fileindex_path, worktree);
7235 if (err)
7236 goto done;
7238 err = got_ref_resolve(&commit_id, repo, branch_ref);
7239 if (err)
7240 goto done;
7242 err = got_object_id_by_path(&tree_id, repo, commit_id,
7243 worktree->path_prefix);
7244 if (err)
7245 goto done;
7247 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7248 if (err)
7249 goto done;
7251 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7252 progress_cb, progress_arg, cancel_cb, cancel_arg);
7253 if (err)
7254 goto sync;
7256 err = got_ref_change_ref(base_branch_ref, commit_id);
7257 if (err)
7258 goto sync;
7260 err = got_ref_write(base_branch_ref, repo);
7261 if (err)
7262 goto sync;
7264 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7265 sync:
7266 sync_err = sync_fileindex(fileindex, fileindex_path);
7267 if (sync_err && err == NULL)
7268 err = sync_err;
7270 done:
7271 unlockerr = got_ref_unlock(branch_ref);
7272 if (unlockerr && err == NULL)
7273 err = unlockerr;
7274 got_ref_close(branch_ref);
7276 unlockerr = got_ref_unlock(base_branch_ref);
7277 if (unlockerr && err == NULL)
7278 err = unlockerr;
7279 got_ref_close(base_branch_ref);
7281 got_fileindex_free(fileindex);
7282 free(fileindex_path);
7283 free(tree_id);
7285 unlockerr = lock_worktree(worktree, LOCK_SH);
7286 if (unlockerr && err == NULL)
7287 err = unlockerr;
7288 return err;
7291 const struct got_error *
7292 got_worktree_integrate_abort(struct got_worktree *worktree,
7293 struct got_fileindex *fileindex, struct got_repository *repo,
7294 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7296 const struct got_error *err = NULL, *unlockerr = NULL;
7298 got_fileindex_free(fileindex);
7300 err = lock_worktree(worktree, LOCK_SH);
7302 unlockerr = got_ref_unlock(branch_ref);
7303 if (unlockerr && err == NULL)
7304 err = unlockerr;
7305 got_ref_close(branch_ref);
7307 unlockerr = got_ref_unlock(base_branch_ref);
7308 if (unlockerr && err == NULL)
7309 err = unlockerr;
7310 got_ref_close(base_branch_ref);
7312 return err;
7315 struct check_stage_ok_arg {
7316 struct got_object_id *head_commit_id;
7317 struct got_worktree *worktree;
7318 struct got_fileindex *fileindex;
7319 struct got_repository *repo;
7320 int have_changes;
7323 const struct got_error *
7324 check_stage_ok(void *arg, unsigned char status,
7325 unsigned char staged_status, const char *relpath,
7326 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7327 struct got_object_id *commit_id, int dirfd, const char *de_name)
7329 struct check_stage_ok_arg *a = arg;
7330 const struct got_error *err = NULL;
7331 struct got_fileindex_entry *ie;
7332 struct got_object_id base_commit_id;
7333 struct got_object_id *base_commit_idp = NULL;
7334 char *in_repo_path = NULL, *p;
7336 if (status == GOT_STATUS_UNVERSIONED ||
7337 status == GOT_STATUS_NO_CHANGE)
7338 return NULL;
7339 if (status == GOT_STATUS_NONEXISTENT)
7340 return got_error_set_errno(ENOENT, relpath);
7342 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7343 if (ie == NULL)
7344 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7346 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7347 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7348 relpath) == -1)
7349 return got_error_from_errno("asprintf");
7351 if (got_fileindex_entry_has_commit(ie)) {
7352 memcpy(base_commit_id.sha1, ie->commit_sha1,
7353 SHA1_DIGEST_LENGTH);
7354 base_commit_idp = &base_commit_id;
7357 if (status == GOT_STATUS_CONFLICT) {
7358 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7359 goto done;
7360 } else if (status != GOT_STATUS_ADD &&
7361 status != GOT_STATUS_MODIFY &&
7362 status != GOT_STATUS_DELETE) {
7363 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7364 goto done;
7367 a->have_changes = 1;
7369 p = in_repo_path;
7370 while (p[0] == '/')
7371 p++;
7372 err = check_out_of_date(p, status, staged_status,
7373 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7374 GOT_ERR_STAGE_OUT_OF_DATE);
7375 done:
7376 free(in_repo_path);
7377 return err;
7380 struct stage_path_arg {
7381 struct got_worktree *worktree;
7382 struct got_fileindex *fileindex;
7383 struct got_repository *repo;
7384 got_worktree_status_cb status_cb;
7385 void *status_arg;
7386 got_worktree_patch_cb patch_cb;
7387 void *patch_arg;
7388 int staged_something;
7389 int allow_bad_symlinks;
7392 static const struct got_error *
7393 stage_path(void *arg, unsigned char status,
7394 unsigned char staged_status, const char *relpath,
7395 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7396 struct got_object_id *commit_id, int dirfd, const char *de_name)
7398 struct stage_path_arg *a = arg;
7399 const struct got_error *err = NULL;
7400 struct got_fileindex_entry *ie;
7401 char *ondisk_path = NULL, *path_content = NULL;
7402 uint32_t stage;
7403 struct got_object_id *new_staged_blob_id = NULL;
7404 struct stat sb;
7406 if (status == GOT_STATUS_UNVERSIONED)
7407 return NULL;
7409 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7410 if (ie == NULL)
7411 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7413 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7414 relpath)== -1)
7415 return got_error_from_errno("asprintf");
7417 switch (status) {
7418 case GOT_STATUS_ADD:
7419 case GOT_STATUS_MODIFY:
7420 /* XXX could sb.st_mode be passed in by our caller? */
7421 if (lstat(ondisk_path, &sb) == -1) {
7422 err = got_error_from_errno2("lstat", ondisk_path);
7423 break;
7425 if (a->patch_cb) {
7426 if (status == GOT_STATUS_ADD) {
7427 int choice = GOT_PATCH_CHOICE_NONE;
7428 err = (*a->patch_cb)(&choice, a->patch_arg,
7429 status, ie->path, NULL, 1, 1);
7430 if (err)
7431 break;
7432 if (choice != GOT_PATCH_CHOICE_YES)
7433 break;
7434 } else {
7435 err = create_patched_content(&path_content, 0,
7436 staged_blob_id ? staged_blob_id : blob_id,
7437 ondisk_path, dirfd, de_name, ie->path,
7438 a->repo, a->patch_cb, a->patch_arg);
7439 if (err || path_content == NULL)
7440 break;
7443 err = got_object_blob_create(&new_staged_blob_id,
7444 path_content ? path_content : ondisk_path, a->repo);
7445 if (err)
7446 break;
7447 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7448 SHA1_DIGEST_LENGTH);
7449 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7450 stage = GOT_FILEIDX_STAGE_ADD;
7451 else
7452 stage = GOT_FILEIDX_STAGE_MODIFY;
7453 got_fileindex_entry_stage_set(ie, stage);
7454 if (S_ISLNK(sb.st_mode)) {
7455 int is_bad_symlink = 0;
7456 if (!a->allow_bad_symlinks) {
7457 char target_path[PATH_MAX];
7458 ssize_t target_len;
7459 target_len = readlink(ondisk_path, target_path,
7460 sizeof(target_path));
7461 if (target_len == -1) {
7462 err = got_error_from_errno2("readlink",
7463 ondisk_path);
7464 break;
7466 err = is_bad_symlink_target(&is_bad_symlink,
7467 target_path, target_len, ondisk_path,
7468 a->worktree->root_path);
7469 if (err)
7470 break;
7471 if (is_bad_symlink) {
7472 err = got_error_path(ondisk_path,
7473 GOT_ERR_BAD_SYMLINK);
7474 break;
7477 if (is_bad_symlink)
7478 got_fileindex_entry_staged_filetype_set(ie,
7479 GOT_FILEIDX_MODE_BAD_SYMLINK);
7480 else
7481 got_fileindex_entry_staged_filetype_set(ie,
7482 GOT_FILEIDX_MODE_SYMLINK);
7483 } else {
7484 got_fileindex_entry_staged_filetype_set(ie,
7485 GOT_FILEIDX_MODE_REGULAR_FILE);
7487 a->staged_something = 1;
7488 if (a->status_cb == NULL)
7489 break;
7490 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7491 get_staged_status(ie), relpath, blob_id,
7492 new_staged_blob_id, NULL, dirfd, de_name);
7493 break;
7494 case GOT_STATUS_DELETE:
7495 if (staged_status == GOT_STATUS_DELETE)
7496 break;
7497 if (a->patch_cb) {
7498 int choice = GOT_PATCH_CHOICE_NONE;
7499 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7500 ie->path, NULL, 1, 1);
7501 if (err)
7502 break;
7503 if (choice == GOT_PATCH_CHOICE_NO)
7504 break;
7505 if (choice != GOT_PATCH_CHOICE_YES) {
7506 err = got_error(GOT_ERR_PATCH_CHOICE);
7507 break;
7510 stage = GOT_FILEIDX_STAGE_DELETE;
7511 got_fileindex_entry_stage_set(ie, stage);
7512 a->staged_something = 1;
7513 if (a->status_cb == NULL)
7514 break;
7515 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7516 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7517 de_name);
7518 break;
7519 case GOT_STATUS_NO_CHANGE:
7520 break;
7521 case GOT_STATUS_CONFLICT:
7522 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7523 break;
7524 case GOT_STATUS_NONEXISTENT:
7525 err = got_error_set_errno(ENOENT, relpath);
7526 break;
7527 default:
7528 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7529 break;
7532 if (path_content && unlink(path_content) == -1 && err == NULL)
7533 err = got_error_from_errno2("unlink", path_content);
7534 free(path_content);
7535 free(ondisk_path);
7536 free(new_staged_blob_id);
7537 return err;
7540 const struct got_error *
7541 got_worktree_stage(struct got_worktree *worktree,
7542 struct got_pathlist_head *paths,
7543 got_worktree_status_cb status_cb, void *status_arg,
7544 got_worktree_patch_cb patch_cb, void *patch_arg,
7545 int allow_bad_symlinks, struct got_repository *repo)
7547 const struct got_error *err = NULL, *sync_err, *unlockerr;
7548 struct got_pathlist_entry *pe;
7549 struct got_fileindex *fileindex = NULL;
7550 char *fileindex_path = NULL;
7551 struct got_reference *head_ref = NULL;
7552 struct got_object_id *head_commit_id = NULL;
7553 struct check_stage_ok_arg oka;
7554 struct stage_path_arg spa;
7556 err = lock_worktree(worktree, LOCK_EX);
7557 if (err)
7558 return err;
7560 err = got_ref_open(&head_ref, repo,
7561 got_worktree_get_head_ref_name(worktree), 0);
7562 if (err)
7563 goto done;
7564 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7565 if (err)
7566 goto done;
7567 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7568 if (err)
7569 goto done;
7571 /* Check pre-conditions before staging anything. */
7572 oka.head_commit_id = head_commit_id;
7573 oka.worktree = worktree;
7574 oka.fileindex = fileindex;
7575 oka.repo = repo;
7576 oka.have_changes = 0;
7577 TAILQ_FOREACH(pe, paths, entry) {
7578 err = worktree_status(worktree, pe->path, fileindex, repo,
7579 check_stage_ok, &oka, NULL, NULL, 0, 0);
7580 if (err)
7581 goto done;
7583 if (!oka.have_changes) {
7584 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7585 goto done;
7588 spa.worktree = worktree;
7589 spa.fileindex = fileindex;
7590 spa.repo = repo;
7591 spa.patch_cb = patch_cb;
7592 spa.patch_arg = patch_arg;
7593 spa.status_cb = status_cb;
7594 spa.status_arg = status_arg;
7595 spa.staged_something = 0;
7596 spa.allow_bad_symlinks = allow_bad_symlinks;
7597 TAILQ_FOREACH(pe, paths, entry) {
7598 err = worktree_status(worktree, pe->path, fileindex, repo,
7599 stage_path, &spa, NULL, NULL, 0, 0);
7600 if (err)
7601 goto done;
7603 if (!spa.staged_something) {
7604 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7605 goto done;
7608 sync_err = sync_fileindex(fileindex, fileindex_path);
7609 if (sync_err && err == NULL)
7610 err = sync_err;
7611 done:
7612 if (head_ref)
7613 got_ref_close(head_ref);
7614 free(head_commit_id);
7615 free(fileindex_path);
7616 if (fileindex)
7617 got_fileindex_free(fileindex);
7618 unlockerr = lock_worktree(worktree, LOCK_SH);
7619 if (unlockerr && err == NULL)
7620 err = unlockerr;
7621 return err;
7624 struct unstage_path_arg {
7625 struct got_worktree *worktree;
7626 struct got_fileindex *fileindex;
7627 struct got_repository *repo;
7628 got_worktree_checkout_cb progress_cb;
7629 void *progress_arg;
7630 got_worktree_patch_cb patch_cb;
7631 void *patch_arg;
7634 static const struct got_error *
7635 create_unstaged_content(char **path_unstaged_content,
7636 char **path_new_staged_content, struct got_object_id *blob_id,
7637 struct got_object_id *staged_blob_id, const char *relpath,
7638 struct got_repository *repo,
7639 got_worktree_patch_cb patch_cb, void *patch_arg)
7641 const struct got_error *err, *free_err;
7642 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7643 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7644 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7645 struct got_diffreg_result *diffreg_result = NULL;
7646 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7647 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7649 *path_unstaged_content = NULL;
7650 *path_new_staged_content = NULL;
7652 err = got_object_id_str(&label1, blob_id);
7653 if (err)
7654 return err;
7655 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7656 if (err)
7657 goto done;
7659 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7660 if (err)
7661 goto done;
7663 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7664 if (err)
7665 goto done;
7667 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7668 if (err)
7669 goto done;
7671 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7672 if (err)
7673 goto done;
7675 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7676 if (err)
7677 goto done;
7679 err = got_diff_files(&diffreg_result, f1, label1, f2,
7680 path2, 3, 0, 1, NULL);
7681 if (err)
7682 goto done;
7684 err = got_opentemp_named(path_unstaged_content, &outfile,
7685 "got-unstaged-content");
7686 if (err)
7687 goto done;
7688 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7689 "got-new-staged-content");
7690 if (err)
7691 goto done;
7693 if (fseek(f1, 0L, SEEK_SET) == -1) {
7694 err = got_ferror(f1, GOT_ERR_IO);
7695 goto done;
7697 if (fseek(f2, 0L, SEEK_SET) == -1) {
7698 err = got_ferror(f2, GOT_ERR_IO);
7699 goto done;
7701 /* Count the number of actual changes in the diff result. */
7702 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7703 struct diff_chunk_context cc = {};
7704 diff_chunk_context_load_change(&cc, &nchunks_used,
7705 diffreg_result->result, n, 0);
7706 nchanges++;
7708 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7709 int choice;
7710 err = apply_or_reject_change(&choice, &nchunks_used,
7711 diffreg_result->result, n, relpath, f1, f2,
7712 &line_cur1, &line_cur2,
7713 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7714 if (err)
7715 goto done;
7716 if (choice == GOT_PATCH_CHOICE_YES)
7717 have_content = 1;
7718 else
7719 have_rejected_content = 1;
7720 if (choice == GOT_PATCH_CHOICE_QUIT)
7721 break;
7723 if (have_content || have_rejected_content)
7724 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7725 outfile, rejectfile);
7726 done:
7727 free(label1);
7728 if (blob)
7729 got_object_blob_close(blob);
7730 if (staged_blob)
7731 got_object_blob_close(staged_blob);
7732 free_err = got_diffreg_result_free(diffreg_result);
7733 if (free_err && err == NULL)
7734 err = free_err;
7735 if (f1 && fclose(f1) == EOF && err == NULL)
7736 err = got_error_from_errno2("fclose", path1);
7737 if (f2 && fclose(f2) == EOF && err == NULL)
7738 err = got_error_from_errno2("fclose", path2);
7739 if (outfile && fclose(outfile) == EOF && err == NULL)
7740 err = got_error_from_errno2("fclose", *path_unstaged_content);
7741 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7742 err = got_error_from_errno2("fclose", *path_new_staged_content);
7743 if (path1 && unlink(path1) == -1 && err == NULL)
7744 err = got_error_from_errno2("unlink", path1);
7745 if (path2 && unlink(path2) == -1 && err == NULL)
7746 err = got_error_from_errno2("unlink", path2);
7747 if (err || !have_content) {
7748 if (*path_unstaged_content &&
7749 unlink(*path_unstaged_content) == -1 && err == NULL)
7750 err = got_error_from_errno2("unlink",
7751 *path_unstaged_content);
7752 free(*path_unstaged_content);
7753 *path_unstaged_content = NULL;
7755 if (err || !have_content || !have_rejected_content) {
7756 if (*path_new_staged_content &&
7757 unlink(*path_new_staged_content) == -1 && err == NULL)
7758 err = got_error_from_errno2("unlink",
7759 *path_new_staged_content);
7760 free(*path_new_staged_content);
7761 *path_new_staged_content = NULL;
7763 free(path1);
7764 free(path2);
7765 return err;
7768 static const struct got_error *
7769 unstage_hunks(struct got_object_id *staged_blob_id,
7770 struct got_blob_object *blob_base,
7771 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7772 const char *ondisk_path, const char *label_orig,
7773 struct got_worktree *worktree, struct got_repository *repo,
7774 got_worktree_patch_cb patch_cb, void *patch_arg,
7775 got_worktree_checkout_cb progress_cb, void *progress_arg)
7777 const struct got_error *err = NULL;
7778 char *path_unstaged_content = NULL;
7779 char *path_new_staged_content = NULL;
7780 char *parent = NULL, *base_path = NULL;
7781 char *blob_base_path = NULL;
7782 struct got_object_id *new_staged_blob_id = NULL;
7783 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
7784 struct stat sb;
7786 err = create_unstaged_content(&path_unstaged_content,
7787 &path_new_staged_content, blob_id, staged_blob_id,
7788 ie->path, repo, patch_cb, patch_arg);
7789 if (err)
7790 return err;
7792 if (path_unstaged_content == NULL)
7793 return NULL;
7795 if (path_new_staged_content) {
7796 err = got_object_blob_create(&new_staged_blob_id,
7797 path_new_staged_content, repo);
7798 if (err)
7799 goto done;
7802 f = fopen(path_unstaged_content, "r");
7803 if (f == NULL) {
7804 err = got_error_from_errno2("fopen",
7805 path_unstaged_content);
7806 goto done;
7808 if (fstat(fileno(f), &sb) == -1) {
7809 err = got_error_from_errno2("fstat", path_unstaged_content);
7810 goto done;
7812 if (got_fileindex_entry_staged_filetype_get(ie) ==
7813 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7814 char link_target[PATH_MAX];
7815 size_t r;
7816 r = fread(link_target, 1, sizeof(link_target), f);
7817 if (r == 0 && ferror(f)) {
7818 err = got_error_from_errno("fread");
7819 goto done;
7821 if (r >= sizeof(link_target)) { /* should not happen */
7822 err = got_error(GOT_ERR_NO_SPACE);
7823 goto done;
7825 link_target[r] = '\0';
7826 err = merge_symlink(worktree, blob_base,
7827 ondisk_path, ie->path, label_orig, link_target,
7828 worktree->base_commit_id, repo, progress_cb,
7829 progress_arg);
7830 } else {
7831 int local_changes_subsumed;
7833 err = got_path_dirname(&parent, ondisk_path);
7834 if (err)
7835 return err;
7837 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
7838 parent) == -1) {
7839 err = got_error_from_errno("asprintf");
7840 base_path = NULL;
7841 goto done;
7844 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
7845 if (err)
7846 goto done;
7847 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
7848 blob_base);
7849 if (err)
7850 goto done;
7853 * In order the run a 3-way merge with a symlink we copy the symlink's
7854 * target path into a temporary file and use that file with diff3.
7856 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7857 err = dump_symlink_target_path_to_file(&f_deriv2,
7858 ondisk_path);
7859 if (err)
7860 goto done;
7861 } else {
7862 int fd;
7863 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
7864 if (fd == -1) {
7865 err = got_error_from_errno2("open", ondisk_path);
7866 goto done;
7868 f_deriv2 = fdopen(fd, "r");
7869 if (f_deriv2 == NULL) {
7870 err = got_error_from_errno2("fdopen", ondisk_path);
7871 close(fd);
7872 goto done;
7876 err = merge_file(&local_changes_subsumed, worktree,
7877 f_base, f, f_deriv2, ondisk_path, ie->path,
7878 got_fileindex_perms_to_st(ie),
7879 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
7880 repo, progress_cb, progress_arg);
7882 if (err)
7883 goto done;
7885 if (new_staged_blob_id) {
7886 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7887 SHA1_DIGEST_LENGTH);
7888 } else {
7889 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7890 got_fileindex_entry_staged_filetype_set(ie, 0);
7892 done:
7893 free(new_staged_blob_id);
7894 if (path_unstaged_content &&
7895 unlink(path_unstaged_content) == -1 && err == NULL)
7896 err = got_error_from_errno2("unlink", path_unstaged_content);
7897 if (path_new_staged_content &&
7898 unlink(path_new_staged_content) == -1 && err == NULL)
7899 err = got_error_from_errno2("unlink", path_new_staged_content);
7900 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
7901 err = got_error_from_errno2("unlink", blob_base_path);
7902 if (f_base && fclose(f_base) == EOF && err == NULL)
7903 err = got_error_from_errno2("fclose", path_unstaged_content);
7904 if (f && fclose(f) == EOF && err == NULL)
7905 err = got_error_from_errno2("fclose", path_unstaged_content);
7906 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
7907 err = got_error_from_errno2("fclose", ondisk_path);
7908 free(path_unstaged_content);
7909 free(path_new_staged_content);
7910 free(blob_base_path);
7911 free(parent);
7912 free(base_path);
7913 return err;
7916 static const struct got_error *
7917 unstage_path(void *arg, unsigned char status,
7918 unsigned char staged_status, const char *relpath,
7919 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7920 struct got_object_id *commit_id, int dirfd, const char *de_name)
7922 const struct got_error *err = NULL;
7923 struct unstage_path_arg *a = arg;
7924 struct got_fileindex_entry *ie;
7925 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7926 char *ondisk_path = NULL;
7927 char *id_str = NULL, *label_orig = NULL;
7928 int local_changes_subsumed;
7929 struct stat sb;
7931 if (staged_status != GOT_STATUS_ADD &&
7932 staged_status != GOT_STATUS_MODIFY &&
7933 staged_status != GOT_STATUS_DELETE)
7934 return NULL;
7936 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7937 if (ie == NULL)
7938 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7940 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7941 == -1)
7942 return got_error_from_errno("asprintf");
7944 err = got_object_id_str(&id_str,
7945 commit_id ? commit_id : a->worktree->base_commit_id);
7946 if (err)
7947 goto done;
7948 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7949 id_str) == -1) {
7950 err = got_error_from_errno("asprintf");
7951 goto done;
7954 switch (staged_status) {
7955 case GOT_STATUS_MODIFY:
7956 err = got_object_open_as_blob(&blob_base, a->repo,
7957 blob_id, 8192);
7958 if (err)
7959 break;
7960 /* fall through */
7961 case GOT_STATUS_ADD:
7962 if (a->patch_cb) {
7963 if (staged_status == GOT_STATUS_ADD) {
7964 int choice = GOT_PATCH_CHOICE_NONE;
7965 err = (*a->patch_cb)(&choice, a->patch_arg,
7966 staged_status, ie->path, NULL, 1, 1);
7967 if (err)
7968 break;
7969 if (choice != GOT_PATCH_CHOICE_YES)
7970 break;
7971 } else {
7972 err = unstage_hunks(staged_blob_id,
7973 blob_base, blob_id, ie, ondisk_path,
7974 label_orig, a->worktree, a->repo,
7975 a->patch_cb, a->patch_arg,
7976 a->progress_cb, a->progress_arg);
7977 break; /* Done with this file. */
7980 err = got_object_open_as_blob(&blob_staged, a->repo,
7981 staged_blob_id, 8192);
7982 if (err)
7983 break;
7984 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7985 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7986 case GOT_FILEIDX_MODE_REGULAR_FILE:
7987 err = merge_blob(&local_changes_subsumed, a->worktree,
7988 blob_base, ondisk_path, relpath,
7989 got_fileindex_perms_to_st(ie), label_orig,
7990 blob_staged, commit_id ? commit_id :
7991 a->worktree->base_commit_id, a->repo,
7992 a->progress_cb, a->progress_arg);
7993 break;
7994 case GOT_FILEIDX_MODE_SYMLINK:
7995 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7996 char *staged_target;
7997 err = got_object_blob_read_to_str(
7998 &staged_target, blob_staged);
7999 if (err)
8000 goto done;
8001 err = merge_symlink(a->worktree, blob_base,
8002 ondisk_path, relpath, label_orig,
8003 staged_target, commit_id ? commit_id :
8004 a->worktree->base_commit_id,
8005 a->repo, a->progress_cb, a->progress_arg);
8006 free(staged_target);
8007 } else {
8008 err = merge_blob(&local_changes_subsumed,
8009 a->worktree, blob_base, ondisk_path,
8010 relpath, got_fileindex_perms_to_st(ie),
8011 label_orig, blob_staged,
8012 commit_id ? commit_id :
8013 a->worktree->base_commit_id, a->repo,
8014 a->progress_cb, a->progress_arg);
8016 break;
8017 default:
8018 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8019 break;
8021 if (err == NULL) {
8022 got_fileindex_entry_stage_set(ie,
8023 GOT_FILEIDX_STAGE_NONE);
8024 got_fileindex_entry_staged_filetype_set(ie, 0);
8026 break;
8027 case GOT_STATUS_DELETE:
8028 if (a->patch_cb) {
8029 int choice = GOT_PATCH_CHOICE_NONE;
8030 err = (*a->patch_cb)(&choice, a->patch_arg,
8031 staged_status, ie->path, NULL, 1, 1);
8032 if (err)
8033 break;
8034 if (choice == GOT_PATCH_CHOICE_NO)
8035 break;
8036 if (choice != GOT_PATCH_CHOICE_YES) {
8037 err = got_error(GOT_ERR_PATCH_CHOICE);
8038 break;
8041 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8042 got_fileindex_entry_staged_filetype_set(ie, 0);
8043 err = get_file_status(&status, &sb, ie, ondisk_path,
8044 dirfd, de_name, a->repo);
8045 if (err)
8046 break;
8047 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8048 break;
8050 done:
8051 free(ondisk_path);
8052 if (blob_base)
8053 got_object_blob_close(blob_base);
8054 if (blob_staged)
8055 got_object_blob_close(blob_staged);
8056 free(id_str);
8057 free(label_orig);
8058 return err;
8061 const struct got_error *
8062 got_worktree_unstage(struct got_worktree *worktree,
8063 struct got_pathlist_head *paths,
8064 got_worktree_checkout_cb progress_cb, void *progress_arg,
8065 got_worktree_patch_cb patch_cb, void *patch_arg,
8066 struct got_repository *repo)
8068 const struct got_error *err = NULL, *sync_err, *unlockerr;
8069 struct got_pathlist_entry *pe;
8070 struct got_fileindex *fileindex = NULL;
8071 char *fileindex_path = NULL;
8072 struct unstage_path_arg upa;
8074 err = lock_worktree(worktree, LOCK_EX);
8075 if (err)
8076 return err;
8078 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8079 if (err)
8080 goto done;
8082 upa.worktree = worktree;
8083 upa.fileindex = fileindex;
8084 upa.repo = repo;
8085 upa.progress_cb = progress_cb;
8086 upa.progress_arg = progress_arg;
8087 upa.patch_cb = patch_cb;
8088 upa.patch_arg = patch_arg;
8089 TAILQ_FOREACH(pe, paths, entry) {
8090 err = worktree_status(worktree, pe->path, fileindex, repo,
8091 unstage_path, &upa, NULL, NULL, 0, 0);
8092 if (err)
8093 goto done;
8096 sync_err = sync_fileindex(fileindex, fileindex_path);
8097 if (sync_err && err == NULL)
8098 err = sync_err;
8099 done:
8100 free(fileindex_path);
8101 if (fileindex)
8102 got_fileindex_free(fileindex);
8103 unlockerr = lock_worktree(worktree, LOCK_SH);
8104 if (unlockerr && err == NULL)
8105 err = unlockerr;
8106 return err;
8109 struct report_file_info_arg {
8110 struct got_worktree *worktree;
8111 got_worktree_path_info_cb info_cb;
8112 void *info_arg;
8113 struct got_pathlist_head *paths;
8114 got_cancel_cb cancel_cb;
8115 void *cancel_arg;
8118 static const struct got_error *
8119 report_file_info(void *arg, struct got_fileindex_entry *ie)
8121 struct report_file_info_arg *a = arg;
8122 struct got_pathlist_entry *pe;
8123 struct got_object_id blob_id, staged_blob_id, commit_id;
8124 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8125 struct got_object_id *commit_idp = NULL;
8126 int stage;
8128 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8129 return got_error(GOT_ERR_CANCELLED);
8131 TAILQ_FOREACH(pe, a->paths, entry) {
8132 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8133 got_path_is_child(ie->path, pe->path, pe->path_len))
8134 break;
8136 if (pe == NULL) /* not found */
8137 return NULL;
8139 if (got_fileindex_entry_has_blob(ie)) {
8140 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8141 blob_idp = &blob_id;
8143 stage = got_fileindex_entry_stage_get(ie);
8144 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8145 stage == GOT_FILEIDX_STAGE_ADD) {
8146 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8147 SHA1_DIGEST_LENGTH);
8148 staged_blob_idp = &staged_blob_id;
8151 if (got_fileindex_entry_has_commit(ie)) {
8152 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8153 commit_idp = &commit_id;
8156 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8157 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8160 const struct got_error *
8161 got_worktree_path_info(struct got_worktree *worktree,
8162 struct got_pathlist_head *paths,
8163 got_worktree_path_info_cb info_cb, void *info_arg,
8164 got_cancel_cb cancel_cb, void *cancel_arg)
8167 const struct got_error *err = NULL, *unlockerr;
8168 struct got_fileindex *fileindex = NULL;
8169 char *fileindex_path = NULL;
8170 struct report_file_info_arg arg;
8172 err = lock_worktree(worktree, LOCK_SH);
8173 if (err)
8174 return err;
8176 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8177 if (err)
8178 goto done;
8180 arg.worktree = worktree;
8181 arg.info_cb = info_cb;
8182 arg.info_arg = info_arg;
8183 arg.paths = paths;
8184 arg.cancel_cb = cancel_cb;
8185 arg.cancel_arg = cancel_arg;
8186 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8187 &arg);
8188 done:
8189 free(fileindex_path);
8190 if (fileindex)
8191 got_fileindex_free(fileindex);
8192 unlockerr = lock_worktree(worktree, LOCK_UN);
8193 if (unlockerr && err == NULL)
8194 err = unlockerr;
8195 return err;