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 if (ie)
1955 got_fileindex_entry_mark_skipped(ie);
1956 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1957 path);
1958 goto done;
1961 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1962 (S_ISLNK(te->mode) ||
1963 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1965 * This is a regular file or an installed bad symlink.
1966 * If the file index indicates that this file is already
1967 * up-to-date with respect to the repository we can skip
1968 * updating contents of this file.
1970 if (got_fileindex_entry_has_commit(ie) &&
1971 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1972 SHA1_DIGEST_LENGTH) == 0) {
1973 /* Same commit. */
1974 err = sync_timestamps(worktree->root_fd,
1975 path, status, ie, &sb);
1976 if (err)
1977 goto done;
1978 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1979 path);
1980 goto done;
1982 if (got_fileindex_entry_has_blob(ie) &&
1983 memcmp(ie->blob_sha1, te->id.sha1,
1984 SHA1_DIGEST_LENGTH) == 0) {
1985 /* Different commit but the same blob. */
1986 err = sync_timestamps(worktree->root_fd,
1987 path, status, ie, &sb);
1988 if (err)
1989 goto done;
1990 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1991 path);
1992 goto done;
1996 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1997 if (err)
1998 goto done;
2000 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2001 int update_timestamps;
2002 struct got_blob_object *blob2 = NULL;
2003 char *label_orig = NULL;
2004 if (got_fileindex_entry_has_blob(ie)) {
2005 struct got_object_id id2;
2006 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2007 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2008 if (err)
2009 goto done;
2011 if (got_fileindex_entry_has_commit(ie)) {
2012 char id_str[SHA1_DIGEST_STRING_LENGTH];
2013 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2014 sizeof(id_str)) == NULL) {
2015 err = got_error_path(id_str,
2016 GOT_ERR_BAD_OBJ_ID_STR);
2017 goto done;
2019 if (asprintf(&label_orig, "%s: commit %s",
2020 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2021 err = got_error_from_errno("asprintf");
2022 goto done;
2025 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2026 char *link_target;
2027 err = got_object_blob_read_to_str(&link_target, blob);
2028 if (err)
2029 goto done;
2030 err = merge_symlink(worktree, blob2, ondisk_path, path,
2031 label_orig, link_target, worktree->base_commit_id,
2032 repo, progress_cb, progress_arg);
2033 free(link_target);
2034 } else {
2035 err = merge_blob(&update_timestamps, worktree, blob2,
2036 ondisk_path, path, sb.st_mode, label_orig, blob,
2037 worktree->base_commit_id, repo,
2038 progress_cb, progress_arg);
2040 free(label_orig);
2041 if (blob2)
2042 got_object_blob_close(blob2);
2043 if (err)
2044 goto done;
2046 * Do not update timestamps of files with local changes.
2047 * Otherwise, a future status walk would treat them as
2048 * unmodified files again.
2050 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2051 blob->id.sha1, worktree->base_commit_id->sha1,
2052 update_timestamps);
2053 } else if (status == GOT_STATUS_MODE_CHANGE) {
2054 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2055 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2056 } else if (status == GOT_STATUS_DELETE) {
2057 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2058 if (err)
2059 goto done;
2060 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2061 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2062 if (err)
2063 goto done;
2064 } else {
2065 int is_bad_symlink = 0;
2066 if (S_ISLNK(te->mode)) {
2067 err = install_symlink(&is_bad_symlink, worktree,
2068 ondisk_path, path, blob,
2069 status == GOT_STATUS_MISSING, 0,
2070 status == GOT_STATUS_UNVERSIONED, repo,
2071 progress_cb, progress_arg);
2072 } else {
2073 err = install_blob(worktree, ondisk_path, path,
2074 te->mode, sb.st_mode, blob,
2075 status == GOT_STATUS_MISSING, 0, 0,
2076 status == GOT_STATUS_UNVERSIONED, repo,
2077 progress_cb, progress_arg);
2079 if (err)
2080 goto done;
2082 if (ie) {
2083 err = got_fileindex_entry_update(ie,
2084 worktree->root_fd, path, blob->id.sha1,
2085 worktree->base_commit_id->sha1, 1);
2086 } else {
2087 err = create_fileindex_entry(&ie, fileindex,
2088 worktree->base_commit_id, worktree->root_fd, path,
2089 &blob->id);
2091 if (err)
2092 goto done;
2094 if (is_bad_symlink) {
2095 got_fileindex_entry_filetype_set(ie,
2096 GOT_FILEIDX_MODE_BAD_SYMLINK);
2099 got_object_blob_close(blob);
2100 done:
2101 free(ondisk_path);
2102 return err;
2105 static const struct got_error *
2106 remove_ondisk_file(const char *root_path, const char *path)
2108 const struct got_error *err = NULL;
2109 char *ondisk_path = NULL, *parent = NULL;
2111 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2112 return got_error_from_errno("asprintf");
2114 if (unlink(ondisk_path) == -1) {
2115 if (errno != ENOENT)
2116 err = got_error_from_errno2("unlink", ondisk_path);
2117 } else {
2118 size_t root_len = strlen(root_path);
2119 err = got_path_dirname(&parent, ondisk_path);
2120 if (err)
2121 goto done;
2122 while (got_path_cmp(parent, root_path,
2123 strlen(parent), root_len) != 0) {
2124 free(ondisk_path);
2125 ondisk_path = parent;
2126 parent = NULL;
2127 if (rmdir(ondisk_path) == -1) {
2128 if (errno != ENOTEMPTY)
2129 err = got_error_from_errno2("rmdir",
2130 ondisk_path);
2131 break;
2133 err = got_path_dirname(&parent, ondisk_path);
2134 if (err)
2135 break;
2138 done:
2139 free(ondisk_path);
2140 free(parent);
2141 return err;
2144 static const struct got_error *
2145 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2146 struct got_fileindex_entry *ie, struct got_repository *repo,
2147 got_worktree_checkout_cb progress_cb, void *progress_arg)
2149 const struct got_error *err = NULL;
2150 unsigned char status;
2151 struct stat sb;
2152 char *ondisk_path;
2154 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2155 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2157 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2158 == -1)
2159 return got_error_from_errno("asprintf");
2161 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2162 if (err)
2163 goto done;
2165 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2166 char ondisk_target[PATH_MAX];
2167 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2168 sizeof(ondisk_target));
2169 if (ondisk_len == -1) {
2170 err = got_error_from_errno2("readlink", ondisk_path);
2171 goto done;
2173 ondisk_target[ondisk_len] = '\0';
2174 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2175 NULL, NULL, /* XXX pass common ancestor info? */
2176 ondisk_target, ondisk_path);
2177 if (err)
2178 goto done;
2179 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2180 ie->path);
2181 goto done;
2184 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2185 status == GOT_STATUS_ADD) {
2186 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2187 if (err)
2188 goto done;
2190 * Preserve the working file and change the deleted blob's
2191 * entry into a schedule-add entry.
2193 err = got_fileindex_entry_update(ie, worktree->root_fd,
2194 ie->path, NULL, NULL, 0);
2195 } else {
2196 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2197 if (err)
2198 goto done;
2199 if (status == GOT_STATUS_NO_CHANGE) {
2200 err = remove_ondisk_file(worktree->root_path, ie->path);
2201 if (err)
2202 goto done;
2204 got_fileindex_entry_remove(fileindex, ie);
2206 done:
2207 free(ondisk_path);
2208 return err;
2211 struct diff_cb_arg {
2212 struct got_fileindex *fileindex;
2213 struct got_worktree *worktree;
2214 struct got_repository *repo;
2215 got_worktree_checkout_cb progress_cb;
2216 void *progress_arg;
2217 got_cancel_cb cancel_cb;
2218 void *cancel_arg;
2221 static const struct got_error *
2222 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2223 struct got_tree_entry *te, const char *parent_path)
2225 struct diff_cb_arg *a = arg;
2227 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2228 return got_error(GOT_ERR_CANCELLED);
2230 return update_blob(a->worktree, a->fileindex, ie, te,
2231 ie->path, a->repo, a->progress_cb, a->progress_arg);
2234 static const struct got_error *
2235 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2237 struct diff_cb_arg *a = arg;
2239 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2240 return got_error(GOT_ERR_CANCELLED);
2242 return delete_blob(a->worktree, a->fileindex, ie,
2243 a->repo, a->progress_cb, a->progress_arg);
2246 static const struct got_error *
2247 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2249 struct diff_cb_arg *a = arg;
2250 const struct got_error *err;
2251 char *path;
2253 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2254 return got_error(GOT_ERR_CANCELLED);
2256 if (got_object_tree_entry_is_submodule(te))
2257 return NULL;
2259 if (asprintf(&path, "%s%s%s", parent_path,
2260 parent_path[0] ? "/" : "", te->name)
2261 == -1)
2262 return got_error_from_errno("asprintf");
2264 if (S_ISDIR(te->mode))
2265 err = add_dir_on_disk(a->worktree, path);
2266 else
2267 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2268 a->repo, a->progress_cb, a->progress_arg);
2270 free(path);
2271 return err;
2274 const struct got_error *
2275 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2277 uint32_t uuid_status;
2279 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2280 if (uuid_status != uuid_s_ok) {
2281 *uuidstr = NULL;
2282 return got_error_uuid(uuid_status, "uuid_to_string");
2285 return NULL;
2288 static const struct got_error *
2289 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2291 const struct got_error *err = NULL;
2292 char *uuidstr = NULL;
2294 *refname = NULL;
2296 err = got_worktree_get_uuid(&uuidstr, worktree);
2297 if (err)
2298 return err;
2300 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2301 err = got_error_from_errno("asprintf");
2302 *refname = NULL;
2304 free(uuidstr);
2305 return err;
2308 const struct got_error *
2309 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2311 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2314 static const struct got_error *
2315 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2317 return get_ref_name(refname, worktree,
2318 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2321 static const struct got_error *
2322 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2324 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2327 static const struct got_error *
2328 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2330 return get_ref_name(refname, worktree,
2331 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2334 static const struct got_error *
2335 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2337 return get_ref_name(refname, worktree,
2338 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2341 static const struct got_error *
2342 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2344 return get_ref_name(refname, worktree,
2345 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2348 static const struct got_error *
2349 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2351 return get_ref_name(refname, worktree,
2352 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2355 static const struct got_error *
2356 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2358 return get_ref_name(refname, worktree,
2359 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2362 static const struct got_error *
2363 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2365 return get_ref_name(refname, worktree,
2366 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2369 const struct got_error *
2370 got_worktree_get_histedit_script_path(char **path,
2371 struct got_worktree *worktree)
2373 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2374 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2375 *path = NULL;
2376 return got_error_from_errno("asprintf");
2378 return NULL;
2382 * Prevent Git's garbage collector from deleting our base commit by
2383 * setting a reference to our base commit's ID.
2385 static const struct got_error *
2386 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2388 const struct got_error *err = NULL;
2389 struct got_reference *ref = NULL;
2390 char *refname;
2392 err = got_worktree_get_base_ref_name(&refname, worktree);
2393 if (err)
2394 return err;
2396 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2397 if (err)
2398 goto done;
2400 err = got_ref_write(ref, repo);
2401 done:
2402 free(refname);
2403 if (ref)
2404 got_ref_close(ref);
2405 return err;
2408 static const struct got_error *
2409 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2411 const struct got_error *err = NULL;
2413 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2414 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2415 err = got_error_from_errno("asprintf");
2416 *fileindex_path = NULL;
2418 return err;
2422 static const struct got_error *
2423 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2424 struct got_worktree *worktree)
2426 const struct got_error *err = NULL;
2427 FILE *index = NULL;
2429 *fileindex_path = NULL;
2430 *fileindex = got_fileindex_alloc();
2431 if (*fileindex == NULL)
2432 return got_error_from_errno("got_fileindex_alloc");
2434 err = get_fileindex_path(fileindex_path, worktree);
2435 if (err)
2436 goto done;
2438 index = fopen(*fileindex_path, "rb");
2439 if (index == NULL) {
2440 if (errno != ENOENT)
2441 err = got_error_from_errno2("fopen", *fileindex_path);
2442 } else {
2443 err = got_fileindex_read(*fileindex, index);
2444 if (fclose(index) == EOF && err == NULL)
2445 err = got_error_from_errno("fclose");
2447 done:
2448 if (err) {
2449 free(*fileindex_path);
2450 *fileindex_path = NULL;
2451 got_fileindex_free(*fileindex);
2452 *fileindex = NULL;
2454 return err;
2457 struct bump_base_commit_id_arg {
2458 struct got_object_id *base_commit_id;
2459 const char *path;
2460 size_t path_len;
2461 const char *entry_name;
2462 got_worktree_checkout_cb progress_cb;
2463 void *progress_arg;
2466 /* Bump base commit ID of all files within an updated part of the work tree. */
2467 static const struct got_error *
2468 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2470 const struct got_error *err;
2471 struct bump_base_commit_id_arg *a = arg;
2473 if (a->entry_name) {
2474 if (strcmp(ie->path, a->path) != 0)
2475 return NULL;
2476 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2477 return NULL;
2479 if (got_fileindex_entry_was_skipped(ie))
2480 return NULL;
2482 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2483 SHA1_DIGEST_LENGTH) == 0)
2484 return NULL;
2486 if (a->progress_cb) {
2487 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2488 ie->path);
2489 if (err)
2490 return err;
2492 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2493 return NULL;
2496 static const struct got_error *
2497 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2498 struct got_fileindex *fileindex,
2499 got_worktree_checkout_cb progress_cb, void *progress_arg)
2501 struct bump_base_commit_id_arg bbc_arg;
2503 bbc_arg.base_commit_id = worktree->base_commit_id;
2504 bbc_arg.entry_name = NULL;
2505 bbc_arg.path = "";
2506 bbc_arg.path_len = 0;
2507 bbc_arg.progress_cb = progress_cb;
2508 bbc_arg.progress_arg = progress_arg;
2510 return got_fileindex_for_each_entry_safe(fileindex,
2511 bump_base_commit_id, &bbc_arg);
2514 static const struct got_error *
2515 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2517 const struct got_error *err = NULL;
2518 char *new_fileindex_path = NULL;
2519 FILE *new_index = NULL;
2520 struct timespec timeout;
2522 err = got_opentemp_named(&new_fileindex_path, &new_index,
2523 fileindex_path);
2524 if (err)
2525 goto done;
2527 err = got_fileindex_write(fileindex, new_index);
2528 if (err)
2529 goto done;
2531 if (rename(new_fileindex_path, fileindex_path) != 0) {
2532 err = got_error_from_errno3("rename", new_fileindex_path,
2533 fileindex_path);
2534 unlink(new_fileindex_path);
2538 * Sleep for a short amount of time to ensure that files modified after
2539 * this program exits have a different time stamp from the one which
2540 * was recorded in the file index.
2542 timeout.tv_sec = 0;
2543 timeout.tv_nsec = 1;
2544 nanosleep(&timeout, NULL);
2545 done:
2546 if (new_index)
2547 fclose(new_index);
2548 free(new_fileindex_path);
2549 return err;
2552 static const struct got_error *
2553 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2554 struct got_object_id **tree_id, const char *wt_relpath,
2555 struct got_worktree *worktree, struct got_repository *repo)
2557 const struct got_error *err = NULL;
2558 struct got_object_id *id = NULL;
2559 char *in_repo_path = NULL;
2560 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2562 *entry_type = GOT_OBJ_TYPE_ANY;
2563 *tree_relpath = NULL;
2564 *tree_id = NULL;
2566 if (wt_relpath[0] == '\0') {
2567 /* Check out all files within the work tree. */
2568 *entry_type = GOT_OBJ_TYPE_TREE;
2569 *tree_relpath = strdup("");
2570 if (*tree_relpath == NULL) {
2571 err = got_error_from_errno("strdup");
2572 goto done;
2574 err = got_object_id_by_path(tree_id, repo,
2575 worktree->base_commit_id, worktree->path_prefix);
2576 if (err)
2577 goto done;
2578 return NULL;
2581 /* Check out a subset of files in the work tree. */
2583 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2584 is_root_wt ? "" : "/", wt_relpath) == -1) {
2585 err = got_error_from_errno("asprintf");
2586 goto done;
2589 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2590 in_repo_path);
2591 if (err)
2592 goto done;
2594 free(in_repo_path);
2595 in_repo_path = NULL;
2597 err = got_object_get_type(entry_type, repo, id);
2598 if (err)
2599 goto done;
2601 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2602 /* Check out a single file. */
2603 if (strchr(wt_relpath, '/') == NULL) {
2604 /* Check out a single file in work tree's root dir. */
2605 in_repo_path = strdup(worktree->path_prefix);
2606 if (in_repo_path == NULL) {
2607 err = got_error_from_errno("strdup");
2608 goto done;
2610 *tree_relpath = strdup("");
2611 if (*tree_relpath == NULL) {
2612 err = got_error_from_errno("strdup");
2613 goto done;
2615 } else {
2616 /* Check out a single file in a subdirectory. */
2617 err = got_path_dirname(tree_relpath, wt_relpath);
2618 if (err)
2619 return err;
2620 if (asprintf(&in_repo_path, "%s%s%s",
2621 worktree->path_prefix, is_root_wt ? "" : "/",
2622 *tree_relpath) == -1) {
2623 err = got_error_from_errno("asprintf");
2624 goto done;
2627 err = got_object_id_by_path(tree_id, repo,
2628 worktree->base_commit_id, in_repo_path);
2629 } else {
2630 /* Check out all files within a subdirectory. */
2631 *tree_id = got_object_id_dup(id);
2632 if (*tree_id == NULL) {
2633 err = got_error_from_errno("got_object_id_dup");
2634 goto done;
2636 *tree_relpath = strdup(wt_relpath);
2637 if (*tree_relpath == NULL) {
2638 err = got_error_from_errno("strdup");
2639 goto done;
2642 done:
2643 free(id);
2644 free(in_repo_path);
2645 if (err) {
2646 *entry_type = GOT_OBJ_TYPE_ANY;
2647 free(*tree_relpath);
2648 *tree_relpath = NULL;
2649 free(*tree_id);
2650 *tree_id = NULL;
2652 return err;
2655 static const struct got_error *
2656 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2657 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2658 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2659 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2661 const struct got_error *err = NULL;
2662 struct got_commit_object *commit = NULL;
2663 struct got_tree_object *tree = NULL;
2664 struct got_fileindex_diff_tree_cb diff_cb;
2665 struct diff_cb_arg arg;
2667 err = ref_base_commit(worktree, repo);
2668 if (err) {
2669 if (!(err->code == GOT_ERR_ERRNO &&
2670 (errno == EACCES || errno == EROFS)))
2671 goto done;
2672 err = (*progress_cb)(progress_arg,
2673 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2674 if (err)
2675 return err;
2678 err = got_object_open_as_commit(&commit, repo,
2679 worktree->base_commit_id);
2680 if (err)
2681 goto done;
2683 err = got_object_open_as_tree(&tree, repo, tree_id);
2684 if (err)
2685 goto done;
2687 if (entry_name &&
2688 got_object_tree_find_entry(tree, entry_name) == NULL) {
2689 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2690 goto done;
2693 diff_cb.diff_old_new = diff_old_new;
2694 diff_cb.diff_old = diff_old;
2695 diff_cb.diff_new = diff_new;
2696 arg.fileindex = fileindex;
2697 arg.worktree = worktree;
2698 arg.repo = repo;
2699 arg.progress_cb = progress_cb;
2700 arg.progress_arg = progress_arg;
2701 arg.cancel_cb = cancel_cb;
2702 arg.cancel_arg = cancel_arg;
2703 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2704 entry_name, repo, &diff_cb, &arg);
2705 done:
2706 if (tree)
2707 got_object_tree_close(tree);
2708 if (commit)
2709 got_object_commit_close(commit);
2710 return err;
2713 const struct got_error *
2714 got_worktree_checkout_files(struct got_worktree *worktree,
2715 struct got_pathlist_head *paths, struct got_repository *repo,
2716 got_worktree_checkout_cb progress_cb, void *progress_arg,
2717 got_cancel_cb cancel_cb, void *cancel_arg)
2719 const struct got_error *err = NULL, *sync_err, *unlockerr;
2720 struct got_commit_object *commit = NULL;
2721 struct got_tree_object *tree = NULL;
2722 struct got_fileindex *fileindex = NULL;
2723 char *fileindex_path = NULL;
2724 struct got_pathlist_entry *pe;
2725 struct tree_path_data {
2726 STAILQ_ENTRY(tree_path_data) entry;
2727 struct got_object_id *tree_id;
2728 int entry_type;
2729 char *relpath;
2730 char *entry_name;
2731 } *tpd = NULL;
2732 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2734 STAILQ_INIT(&tree_paths);
2736 err = lock_worktree(worktree, LOCK_EX);
2737 if (err)
2738 return err;
2740 /* Map all specified paths to in-repository trees. */
2741 TAILQ_FOREACH(pe, paths, entry) {
2742 tpd = malloc(sizeof(*tpd));
2743 if (tpd == NULL) {
2744 err = got_error_from_errno("malloc");
2745 goto done;
2748 err = find_tree_entry_for_checkout(&tpd->entry_type,
2749 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2750 if (err) {
2751 free(tpd);
2752 goto done;
2755 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2756 err = got_path_basename(&tpd->entry_name, pe->path);
2757 if (err) {
2758 free(tpd->relpath);
2759 free(tpd->tree_id);
2760 free(tpd);
2761 goto done;
2763 } else
2764 tpd->entry_name = NULL;
2766 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2770 * Read the file index.
2771 * Checking out files is supposed to be an idempotent operation.
2772 * If the on-disk file index is incomplete we will try to complete it.
2774 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2775 if (err)
2776 goto done;
2778 tpd = STAILQ_FIRST(&tree_paths);
2779 TAILQ_FOREACH(pe, paths, entry) {
2780 struct bump_base_commit_id_arg bbc_arg;
2782 err = checkout_files(worktree, fileindex, tpd->relpath,
2783 tpd->tree_id, tpd->entry_name, repo,
2784 progress_cb, progress_arg, cancel_cb, cancel_arg);
2785 if (err)
2786 break;
2788 bbc_arg.base_commit_id = worktree->base_commit_id;
2789 bbc_arg.entry_name = tpd->entry_name;
2790 bbc_arg.path = pe->path;
2791 bbc_arg.path_len = pe->path_len;
2792 bbc_arg.progress_cb = progress_cb;
2793 bbc_arg.progress_arg = progress_arg;
2794 err = got_fileindex_for_each_entry_safe(fileindex,
2795 bump_base_commit_id, &bbc_arg);
2796 if (err)
2797 break;
2799 tpd = STAILQ_NEXT(tpd, entry);
2801 sync_err = sync_fileindex(fileindex, fileindex_path);
2802 if (sync_err && err == NULL)
2803 err = sync_err;
2804 done:
2805 free(fileindex_path);
2806 if (tree)
2807 got_object_tree_close(tree);
2808 if (commit)
2809 got_object_commit_close(commit);
2810 if (fileindex)
2811 got_fileindex_free(fileindex);
2812 while (!STAILQ_EMPTY(&tree_paths)) {
2813 tpd = STAILQ_FIRST(&tree_paths);
2814 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2815 free(tpd->relpath);
2816 free(tpd->tree_id);
2817 free(tpd);
2819 unlockerr = lock_worktree(worktree, LOCK_SH);
2820 if (unlockerr && err == NULL)
2821 err = unlockerr;
2822 return err;
2825 struct merge_file_cb_arg {
2826 struct got_worktree *worktree;
2827 struct got_fileindex *fileindex;
2828 got_worktree_checkout_cb progress_cb;
2829 void *progress_arg;
2830 got_cancel_cb cancel_cb;
2831 void *cancel_arg;
2832 const char *label_orig;
2833 struct got_object_id *commit_id2;
2836 static const struct got_error *
2837 merge_file_cb(void *arg, struct got_blob_object *blob1,
2838 struct got_blob_object *blob2, struct got_object_id *id1,
2839 struct got_object_id *id2, const char *path1, const char *path2,
2840 mode_t mode1, mode_t mode2, struct got_repository *repo)
2842 static const struct got_error *err = NULL;
2843 struct merge_file_cb_arg *a = arg;
2844 struct got_fileindex_entry *ie;
2845 char *ondisk_path = NULL;
2846 struct stat sb;
2847 unsigned char status;
2848 int local_changes_subsumed;
2849 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2850 char *id_str = NULL, *label_deriv2 = NULL;
2852 if (blob1 && blob2) {
2853 ie = got_fileindex_entry_get(a->fileindex, path2,
2854 strlen(path2));
2855 if (ie == NULL)
2856 return (*a->progress_cb)(a->progress_arg,
2857 GOT_STATUS_MISSING, path2);
2859 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2860 path2) == -1)
2861 return got_error_from_errno("asprintf");
2863 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2864 repo);
2865 if (err)
2866 goto done;
2868 if (status == GOT_STATUS_DELETE) {
2869 err = (*a->progress_cb)(a->progress_arg,
2870 GOT_STATUS_MERGE, path2);
2871 goto done;
2873 if (status != GOT_STATUS_NO_CHANGE &&
2874 status != GOT_STATUS_MODIFY &&
2875 status != GOT_STATUS_CONFLICT &&
2876 status != GOT_STATUS_ADD) {
2877 err = (*a->progress_cb)(a->progress_arg, status, path2);
2878 goto done;
2881 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2882 char *link_target2;
2883 err = got_object_blob_read_to_str(&link_target2, blob2);
2884 if (err)
2885 goto done;
2886 err = merge_symlink(a->worktree, blob1, ondisk_path,
2887 path2, a->label_orig, link_target2, a->commit_id2,
2888 repo, a->progress_cb, a->progress_arg);
2889 free(link_target2);
2890 } else {
2891 int fd;
2893 f_orig = got_opentemp();
2894 if (f_orig == NULL) {
2895 err = got_error_from_errno("got_opentemp");
2896 goto done;
2898 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2899 f_orig, blob1);
2900 if (err)
2901 goto done;
2903 f_deriv2 = got_opentemp();
2904 if (f_deriv2 == NULL)
2905 goto done;
2906 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2907 f_deriv2, blob2);
2908 if (err)
2909 goto done;
2911 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2912 if (fd == -1) {
2913 err = got_error_from_errno2("open",
2914 ondisk_path);
2915 goto done;
2917 f_deriv = fdopen(fd, "r");
2918 if (f_deriv == NULL) {
2919 err = got_error_from_errno2("fdopen",
2920 ondisk_path);
2921 close(fd);
2922 goto done;
2924 err = got_object_id_str(&id_str, a->commit_id2);
2925 if (err)
2926 goto done;
2927 if (asprintf(&label_deriv2, "%s: commit %s",
2928 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2929 err = got_error_from_errno("asprintf");
2930 goto done;
2932 err = merge_file(&local_changes_subsumed, a->worktree,
2933 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2934 sb.st_mode, a->label_orig, NULL, label_deriv2,
2935 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2936 a->progress_cb, a->progress_arg);
2938 } else if (blob1) {
2939 ie = got_fileindex_entry_get(a->fileindex, path1,
2940 strlen(path1));
2941 if (ie == NULL)
2942 return (*a->progress_cb)(a->progress_arg,
2943 GOT_STATUS_MISSING, path1);
2945 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2946 path1) == -1)
2947 return got_error_from_errno("asprintf");
2949 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2950 repo);
2951 if (err)
2952 goto done;
2954 switch (status) {
2955 case GOT_STATUS_NO_CHANGE:
2956 err = (*a->progress_cb)(a->progress_arg,
2957 GOT_STATUS_DELETE, path1);
2958 if (err)
2959 goto done;
2960 err = remove_ondisk_file(a->worktree->root_path, path1);
2961 if (err)
2962 goto done;
2963 if (ie)
2964 got_fileindex_entry_mark_deleted_from_disk(ie);
2965 break;
2966 case GOT_STATUS_DELETE:
2967 case GOT_STATUS_MISSING:
2968 err = (*a->progress_cb)(a->progress_arg,
2969 GOT_STATUS_DELETE, path1);
2970 if (err)
2971 goto done;
2972 if (ie)
2973 got_fileindex_entry_mark_deleted_from_disk(ie);
2974 break;
2975 case GOT_STATUS_ADD: {
2976 struct got_object_id *id;
2977 FILE *blob1_f;
2979 * Delete the added file only if its content already
2980 * exists in the repository.
2982 err = got_object_blob_file_create(&id, &blob1_f, path1);
2983 if (err)
2984 goto done;
2985 if (got_object_id_cmp(id, id1) == 0) {
2986 err = (*a->progress_cb)(a->progress_arg,
2987 GOT_STATUS_DELETE, path1);
2988 if (err)
2989 goto done;
2990 err = remove_ondisk_file(a->worktree->root_path,
2991 path1);
2992 if (err)
2993 goto done;
2994 if (ie)
2995 got_fileindex_entry_remove(a->fileindex,
2996 ie);
2997 } else {
2998 err = (*a->progress_cb)(a->progress_arg,
2999 GOT_STATUS_CANNOT_DELETE, path1);
3001 if (fclose(blob1_f) == EOF && err == NULL)
3002 err = got_error_from_errno("fclose");
3003 free(id);
3004 if (err)
3005 goto done;
3006 break;
3008 case GOT_STATUS_MODIFY:
3009 case GOT_STATUS_CONFLICT:
3010 err = (*a->progress_cb)(a->progress_arg,
3011 GOT_STATUS_CANNOT_DELETE, path1);
3012 if (err)
3013 goto done;
3014 break;
3015 case GOT_STATUS_OBSTRUCTED:
3016 err = (*a->progress_cb)(a->progress_arg, status, path1);
3017 if (err)
3018 goto done;
3019 break;
3020 default:
3021 break;
3023 } else if (blob2) {
3024 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3025 path2) == -1)
3026 return got_error_from_errno("asprintf");
3027 ie = got_fileindex_entry_get(a->fileindex, path2,
3028 strlen(path2));
3029 if (ie) {
3030 err = get_file_status(&status, &sb, ie, ondisk_path,
3031 -1, NULL, repo);
3032 if (err)
3033 goto done;
3034 if (status != GOT_STATUS_NO_CHANGE &&
3035 status != GOT_STATUS_MODIFY &&
3036 status != GOT_STATUS_CONFLICT &&
3037 status != GOT_STATUS_ADD) {
3038 err = (*a->progress_cb)(a->progress_arg,
3039 status, path2);
3040 goto done;
3042 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3043 char *link_target2;
3044 err = got_object_blob_read_to_str(&link_target2,
3045 blob2);
3046 if (err)
3047 goto done;
3048 err = merge_symlink(a->worktree, NULL,
3049 ondisk_path, path2, a->label_orig,
3050 link_target2, a->commit_id2, repo,
3051 a->progress_cb, a->progress_arg);
3052 free(link_target2);
3053 } else if (S_ISREG(sb.st_mode)) {
3054 err = merge_blob(&local_changes_subsumed,
3055 a->worktree, NULL, ondisk_path, path2,
3056 sb.st_mode, a->label_orig, blob2,
3057 a->commit_id2, repo, a->progress_cb,
3058 a->progress_arg);
3059 } else {
3060 err = got_error_path(ondisk_path,
3061 GOT_ERR_FILE_OBSTRUCTED);
3063 if (err)
3064 goto done;
3065 if (status == GOT_STATUS_DELETE) {
3066 err = got_fileindex_entry_update(ie,
3067 a->worktree->root_fd, path2, blob2->id.sha1,
3068 a->worktree->base_commit_id->sha1, 0);
3069 if (err)
3070 goto done;
3072 } else {
3073 int is_bad_symlink = 0;
3074 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3075 if (S_ISLNK(mode2)) {
3076 err = install_symlink(&is_bad_symlink,
3077 a->worktree, ondisk_path, path2, blob2, 0,
3078 0, 1, repo, a->progress_cb, a->progress_arg);
3079 } else {
3080 err = install_blob(a->worktree, ondisk_path, path2,
3081 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3082 a->progress_cb, a->progress_arg);
3084 if (err)
3085 goto done;
3086 err = got_fileindex_entry_alloc(&ie, path2);
3087 if (err)
3088 goto done;
3089 err = got_fileindex_entry_update(ie,
3090 a->worktree->root_fd, path2, NULL, NULL, 1);
3091 if (err) {
3092 got_fileindex_entry_free(ie);
3093 goto done;
3095 err = got_fileindex_entry_add(a->fileindex, ie);
3096 if (err) {
3097 got_fileindex_entry_free(ie);
3098 goto done;
3100 if (is_bad_symlink) {
3101 got_fileindex_entry_filetype_set(ie,
3102 GOT_FILEIDX_MODE_BAD_SYMLINK);
3106 done:
3107 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3108 err = got_error_from_errno("fclose");
3109 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3110 err = got_error_from_errno("fclose");
3111 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3112 err = got_error_from_errno("fclose");
3113 free(id_str);
3114 free(label_deriv2);
3115 free(ondisk_path);
3116 return err;
3119 struct check_merge_ok_arg {
3120 struct got_worktree *worktree;
3121 struct got_repository *repo;
3124 static const struct got_error *
3125 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3127 const struct got_error *err = NULL;
3128 struct check_merge_ok_arg *a = arg;
3129 unsigned char status;
3130 struct stat sb;
3131 char *ondisk_path;
3133 /* Reject merges into a work tree with mixed base commits. */
3134 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3135 SHA1_DIGEST_LENGTH))
3136 return got_error(GOT_ERR_MIXED_COMMITS);
3138 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3139 == -1)
3140 return got_error_from_errno("asprintf");
3142 /* Reject merges into a work tree with conflicted files. */
3143 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3144 if (err)
3145 return err;
3146 if (status == GOT_STATUS_CONFLICT)
3147 return got_error(GOT_ERR_CONFLICTS);
3149 return NULL;
3152 static const struct got_error *
3153 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3154 const char *fileindex_path, struct got_object_id *commit_id1,
3155 struct got_object_id *commit_id2, struct got_repository *repo,
3156 got_worktree_checkout_cb progress_cb, void *progress_arg,
3157 got_cancel_cb cancel_cb, void *cancel_arg)
3159 const struct got_error *err = NULL, *sync_err;
3160 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3161 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3162 struct merge_file_cb_arg arg;
3163 char *label_orig = NULL;
3165 if (commit_id1) {
3166 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3167 worktree->path_prefix);
3168 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3169 goto done;
3171 if (tree_id1) {
3172 char *id_str;
3174 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3175 if (err)
3176 goto done;
3178 err = got_object_id_str(&id_str, commit_id1);
3179 if (err)
3180 goto done;
3182 if (asprintf(&label_orig, "%s: commit %s",
3183 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3184 err = got_error_from_errno("asprintf");
3185 free(id_str);
3186 goto done;
3188 free(id_str);
3191 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3192 worktree->path_prefix);
3193 if (err)
3194 goto done;
3196 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3197 if (err)
3198 goto done;
3200 arg.worktree = worktree;
3201 arg.fileindex = fileindex;
3202 arg.progress_cb = progress_cb;
3203 arg.progress_arg = progress_arg;
3204 arg.cancel_cb = cancel_cb;
3205 arg.cancel_arg = cancel_arg;
3206 arg.label_orig = label_orig;
3207 arg.commit_id2 = commit_id2;
3208 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3209 sync_err = sync_fileindex(fileindex, fileindex_path);
3210 if (sync_err && err == NULL)
3211 err = sync_err;
3212 done:
3213 if (tree1)
3214 got_object_tree_close(tree1);
3215 if (tree2)
3216 got_object_tree_close(tree2);
3217 free(label_orig);
3218 return err;
3221 const struct got_error *
3222 got_worktree_merge_files(struct got_worktree *worktree,
3223 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3224 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3225 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3227 const struct got_error *err, *unlockerr;
3228 char *fileindex_path = NULL;
3229 struct got_fileindex *fileindex = NULL;
3230 struct check_merge_ok_arg mok_arg;
3232 err = lock_worktree(worktree, LOCK_EX);
3233 if (err)
3234 return err;
3236 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3237 if (err)
3238 goto done;
3240 mok_arg.worktree = worktree;
3241 mok_arg.repo = repo;
3242 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3243 &mok_arg);
3244 if (err)
3245 goto done;
3247 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3248 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3249 done:
3250 if (fileindex)
3251 got_fileindex_free(fileindex);
3252 free(fileindex_path);
3253 unlockerr = lock_worktree(worktree, LOCK_SH);
3254 if (unlockerr && err == NULL)
3255 err = unlockerr;
3256 return err;
3259 struct diff_dir_cb_arg {
3260 struct got_fileindex *fileindex;
3261 struct got_worktree *worktree;
3262 const char *status_path;
3263 size_t status_path_len;
3264 struct got_repository *repo;
3265 got_worktree_status_cb status_cb;
3266 void *status_arg;
3267 got_cancel_cb cancel_cb;
3268 void *cancel_arg;
3269 /* A pathlist containing per-directory pathlists of ignore patterns. */
3270 struct got_pathlist_head ignores;
3271 int report_unchanged;
3272 int no_ignores;
3275 static const struct got_error *
3276 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3277 int dirfd, const char *de_name,
3278 got_worktree_status_cb status_cb, void *status_arg,
3279 struct got_repository *repo, int report_unchanged)
3281 const struct got_error *err = NULL;
3282 unsigned char status = GOT_STATUS_NO_CHANGE;
3283 unsigned char staged_status = get_staged_status(ie);
3284 struct stat sb;
3285 struct got_object_id blob_id, commit_id, staged_blob_id;
3286 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3287 struct got_object_id *staged_blob_idp = NULL;
3289 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3290 if (err)
3291 return err;
3293 if (status == GOT_STATUS_NO_CHANGE &&
3294 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3295 return NULL;
3297 if (got_fileindex_entry_has_blob(ie)) {
3298 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3299 blob_idp = &blob_id;
3301 if (got_fileindex_entry_has_commit(ie)) {
3302 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3303 commit_idp = &commit_id;
3305 if (staged_status == GOT_STATUS_ADD ||
3306 staged_status == GOT_STATUS_MODIFY) {
3307 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3308 SHA1_DIGEST_LENGTH);
3309 staged_blob_idp = &staged_blob_id;
3312 return (*status_cb)(status_arg, status, staged_status,
3313 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3316 static const struct got_error *
3317 status_old_new(void *arg, struct got_fileindex_entry *ie,
3318 struct dirent *de, const char *parent_path, int dirfd)
3320 const struct got_error *err = NULL;
3321 struct diff_dir_cb_arg *a = arg;
3322 char *abspath;
3324 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3325 return got_error(GOT_ERR_CANCELLED);
3327 if (got_path_cmp(parent_path, a->status_path,
3328 strlen(parent_path), a->status_path_len) != 0 &&
3329 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3330 return NULL;
3332 if (parent_path[0]) {
3333 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3334 parent_path, de->d_name) == -1)
3335 return got_error_from_errno("asprintf");
3336 } else {
3337 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3338 de->d_name) == -1)
3339 return got_error_from_errno("asprintf");
3342 err = report_file_status(ie, abspath, dirfd, de->d_name,
3343 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3344 free(abspath);
3345 return err;
3348 static const struct got_error *
3349 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3351 struct diff_dir_cb_arg *a = arg;
3352 struct got_object_id blob_id, commit_id;
3353 unsigned char status;
3355 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3356 return got_error(GOT_ERR_CANCELLED);
3358 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3359 return NULL;
3361 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3362 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3363 if (got_fileindex_entry_has_file_on_disk(ie))
3364 status = GOT_STATUS_MISSING;
3365 else
3366 status = GOT_STATUS_DELETE;
3367 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3368 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3371 void
3372 free_ignorelist(struct got_pathlist_head *ignorelist)
3374 struct got_pathlist_entry *pe;
3376 TAILQ_FOREACH(pe, ignorelist, entry)
3377 free((char *)pe->path);
3378 got_pathlist_free(ignorelist);
3381 void
3382 free_ignores(struct got_pathlist_head *ignores)
3384 struct got_pathlist_entry *pe;
3386 TAILQ_FOREACH(pe, ignores, entry) {
3387 struct got_pathlist_head *ignorelist = pe->data;
3388 free_ignorelist(ignorelist);
3389 free((char *)pe->path);
3391 got_pathlist_free(ignores);
3394 static const struct got_error *
3395 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3397 const struct got_error *err = NULL;
3398 struct got_pathlist_entry *pe = NULL;
3399 struct got_pathlist_head *ignorelist;
3400 char *line = NULL, *pattern, *dirpath = NULL;
3401 size_t linesize = 0;
3402 ssize_t linelen;
3404 ignorelist = calloc(1, sizeof(*ignorelist));
3405 if (ignorelist == NULL)
3406 return got_error_from_errno("calloc");
3407 TAILQ_INIT(ignorelist);
3409 while ((linelen = getline(&line, &linesize, f)) != -1) {
3410 if (linelen > 0 && line[linelen - 1] == '\n')
3411 line[linelen - 1] = '\0';
3413 /* Git's ignores may contain comments. */
3414 if (line[0] == '#')
3415 continue;
3417 /* Git's negated patterns are not (yet?) supported. */
3418 if (line[0] == '!')
3419 continue;
3421 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3422 line) == -1) {
3423 err = got_error_from_errno("asprintf");
3424 goto done;
3426 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3427 if (err)
3428 goto done;
3430 if (ferror(f)) {
3431 err = got_error_from_errno("getline");
3432 goto done;
3435 dirpath = strdup(path);
3436 if (dirpath == NULL) {
3437 err = got_error_from_errno("strdup");
3438 goto done;
3440 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3441 done:
3442 free(line);
3443 if (err || pe == NULL) {
3444 free(dirpath);
3445 free_ignorelist(ignorelist);
3447 return err;
3450 int
3451 match_ignores(struct got_pathlist_head *ignores, const char *path)
3453 struct got_pathlist_entry *pe;
3455 /* Handle patterns which match in all directories. */
3456 TAILQ_FOREACH(pe, ignores, entry) {
3457 struct got_pathlist_head *ignorelist = pe->data;
3458 struct got_pathlist_entry *pi;
3460 TAILQ_FOREACH(pi, ignorelist, entry) {
3461 const char *p, *pattern = pi->path;
3463 if (strncmp(pattern, "**/", 3) != 0)
3464 continue;
3465 pattern += 3;
3466 p = path;
3467 while (*p) {
3468 if (fnmatch(pattern, p,
3469 FNM_PATHNAME | FNM_LEADING_DIR)) {
3470 /* Retry in next directory. */
3471 while (*p && *p != '/')
3472 p++;
3473 while (*p == '/')
3474 p++;
3475 continue;
3477 return 1;
3483 * The ignores pathlist contains ignore lists from children before
3484 * parents, so we can find the most specific ignorelist by walking
3485 * ignores backwards.
3487 pe = TAILQ_LAST(ignores, got_pathlist_head);
3488 while (pe) {
3489 if (got_path_is_child(path, pe->path, pe->path_len)) {
3490 struct got_pathlist_head *ignorelist = pe->data;
3491 struct got_pathlist_entry *pi;
3492 TAILQ_FOREACH(pi, ignorelist, entry) {
3493 const char *pattern = pi->path;
3494 int flags = FNM_LEADING_DIR;
3495 if (strstr(pattern, "/**/") == NULL)
3496 flags |= FNM_PATHNAME;
3497 if (fnmatch(pattern, path, flags))
3498 continue;
3499 return 1;
3502 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3505 return 0;
3508 static const struct got_error *
3509 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3510 const char *path, int dirfd, const char *ignores_filename)
3512 const struct got_error *err = NULL;
3513 char *ignorespath;
3514 int fd = -1;
3515 FILE *ignoresfile = NULL;
3517 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3518 path[0] ? "/" : "", ignores_filename) == -1)
3519 return got_error_from_errno("asprintf");
3521 if (dirfd != -1) {
3522 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3523 if (fd == -1) {
3524 if (errno != ENOENT && errno != EACCES)
3525 err = got_error_from_errno2("openat",
3526 ignorespath);
3527 } else {
3528 ignoresfile = fdopen(fd, "r");
3529 if (ignoresfile == NULL)
3530 err = got_error_from_errno2("fdopen",
3531 ignorespath);
3532 else {
3533 fd = -1;
3534 err = read_ignores(ignores, path, ignoresfile);
3537 } else {
3538 ignoresfile = fopen(ignorespath, "r");
3539 if (ignoresfile == NULL) {
3540 if (errno != ENOENT && errno != EACCES)
3541 err = got_error_from_errno2("fopen",
3542 ignorespath);
3543 } else
3544 err = read_ignores(ignores, path, ignoresfile);
3547 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3548 err = got_error_from_errno2("fclose", path);
3549 if (fd != -1 && close(fd) == -1 && err == NULL)
3550 err = got_error_from_errno2("close", path);
3551 free(ignorespath);
3552 return err;
3555 static const struct got_error *
3556 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3558 const struct got_error *err = NULL;
3559 struct diff_dir_cb_arg *a = arg;
3560 char *path = NULL;
3562 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3563 return got_error(GOT_ERR_CANCELLED);
3565 if (parent_path[0]) {
3566 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3567 return got_error_from_errno("asprintf");
3568 } else {
3569 path = de->d_name;
3572 if (de->d_type != DT_DIR &&
3573 got_path_is_child(path, a->status_path, a->status_path_len)
3574 && !match_ignores(&a->ignores, path))
3575 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3576 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3577 if (parent_path[0])
3578 free(path);
3579 return err;
3582 static const struct got_error *
3583 status_traverse(void *arg, const char *path, int dirfd)
3585 const struct got_error *err = NULL;
3586 struct diff_dir_cb_arg *a = arg;
3588 if (a->no_ignores)
3589 return NULL;
3591 err = add_ignores(&a->ignores, a->worktree->root_path,
3592 path, dirfd, ".cvsignore");
3593 if (err)
3594 return err;
3596 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3597 dirfd, ".gitignore");
3599 return err;
3602 static const struct got_error *
3603 report_single_file_status(const char *path, const char *ondisk_path,
3604 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3605 void *status_arg, struct got_repository *repo, int report_unchanged)
3607 struct got_fileindex_entry *ie;
3608 struct stat sb;
3610 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3611 if (ie)
3612 return report_file_status(ie, ondisk_path, -1, NULL,
3613 status_cb, status_arg, repo, report_unchanged);
3615 if (lstat(ondisk_path, &sb) == -1) {
3616 if (errno != ENOENT)
3617 return got_error_from_errno2("lstat", ondisk_path);
3618 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3619 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3620 return NULL;
3623 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3624 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3625 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3627 return NULL;
3630 static const struct got_error *
3631 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3632 const char *root_path, const char *path)
3634 const struct got_error *err;
3635 char *parent_path, *next_parent_path = NULL;
3637 err = add_ignores(ignores, root_path, "", -1,
3638 ".cvsignore");
3639 if (err)
3640 return err;
3642 err = add_ignores(ignores, root_path, "", -1,
3643 ".gitignore");
3644 if (err)
3645 return err;
3647 err = got_path_dirname(&parent_path, path);
3648 if (err) {
3649 if (err->code == GOT_ERR_BAD_PATH)
3650 return NULL; /* cannot traverse parent */
3651 return err;
3653 for (;;) {
3654 err = add_ignores(ignores, root_path, parent_path, -1,
3655 ".cvsignore");
3656 if (err)
3657 break;
3658 err = add_ignores(ignores, root_path, parent_path, -1,
3659 ".gitignore");
3660 if (err)
3661 break;
3662 err = got_path_dirname(&next_parent_path, parent_path);
3663 if (err) {
3664 if (err->code == GOT_ERR_BAD_PATH)
3665 err = NULL; /* traversed everything */
3666 break;
3668 free(parent_path);
3669 parent_path = next_parent_path;
3670 next_parent_path = NULL;
3673 free(parent_path);
3674 free(next_parent_path);
3675 return err;
3678 static const struct got_error *
3679 worktree_status(struct got_worktree *worktree, const char *path,
3680 struct got_fileindex *fileindex, struct got_repository *repo,
3681 got_worktree_status_cb status_cb, void *status_arg,
3682 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3683 int report_unchanged)
3685 const struct got_error *err = NULL;
3686 int fd = -1;
3687 struct got_fileindex_diff_dir_cb fdiff_cb;
3688 struct diff_dir_cb_arg arg;
3689 char *ondisk_path = NULL;
3691 TAILQ_INIT(&arg.ignores);
3693 if (asprintf(&ondisk_path, "%s%s%s",
3694 worktree->root_path, path[0] ? "/" : "", path) == -1)
3695 return got_error_from_errno("asprintf");
3697 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3698 if (fd == -1) {
3699 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3700 errno != ELOOP)
3701 err = got_error_from_errno2("open", ondisk_path);
3702 else
3703 err = report_single_file_status(path, ondisk_path,
3704 fileindex, status_cb, status_arg, repo,
3705 report_unchanged);
3706 } else {
3707 fdiff_cb.diff_old_new = status_old_new;
3708 fdiff_cb.diff_old = status_old;
3709 fdiff_cb.diff_new = status_new;
3710 fdiff_cb.diff_traverse = status_traverse;
3711 arg.fileindex = fileindex;
3712 arg.worktree = worktree;
3713 arg.status_path = path;
3714 arg.status_path_len = strlen(path);
3715 arg.repo = repo;
3716 arg.status_cb = status_cb;
3717 arg.status_arg = status_arg;
3718 arg.cancel_cb = cancel_cb;
3719 arg.cancel_arg = cancel_arg;
3720 arg.report_unchanged = report_unchanged;
3721 arg.no_ignores = no_ignores;
3722 if (!no_ignores) {
3723 err = add_ignores_from_parent_paths(&arg.ignores,
3724 worktree->root_path, path);
3725 if (err)
3726 goto done;
3728 err = got_fileindex_diff_dir(fileindex, fd,
3729 worktree->root_path, path, repo, &fdiff_cb, &arg);
3731 done:
3732 free_ignores(&arg.ignores);
3733 if (fd != -1 && close(fd) == -1 && err == NULL)
3734 err = got_error_from_errno("close");
3735 free(ondisk_path);
3736 return err;
3739 const struct got_error *
3740 got_worktree_status(struct got_worktree *worktree,
3741 struct got_pathlist_head *paths, struct got_repository *repo,
3742 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3743 got_cancel_cb cancel_cb, void *cancel_arg)
3745 const struct got_error *err = NULL;
3746 char *fileindex_path = NULL;
3747 struct got_fileindex *fileindex = NULL;
3748 struct got_pathlist_entry *pe;
3750 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3751 if (err)
3752 return err;
3754 TAILQ_FOREACH(pe, paths, entry) {
3755 err = worktree_status(worktree, pe->path, fileindex, repo,
3756 status_cb, status_arg, cancel_cb, cancel_arg,
3757 no_ignores, 0);
3758 if (err)
3759 break;
3761 free(fileindex_path);
3762 got_fileindex_free(fileindex);
3763 return err;
3766 const struct got_error *
3767 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3768 const char *arg)
3770 const struct got_error *err = NULL;
3771 char *resolved = NULL, *cwd = NULL, *path = NULL;
3772 size_t len;
3773 struct stat sb;
3774 char *abspath = NULL;
3775 char canonpath[PATH_MAX];
3777 *wt_path = NULL;
3779 cwd = getcwd(NULL, 0);
3780 if (cwd == NULL)
3781 return got_error_from_errno("getcwd");
3783 if (lstat(arg, &sb) == -1) {
3784 if (errno != ENOENT) {
3785 err = got_error_from_errno2("lstat", arg);
3786 goto done;
3788 sb.st_mode = 0;
3790 if (S_ISLNK(sb.st_mode)) {
3792 * We cannot use realpath(3) with symlinks since we want to
3793 * operate on the symlink itself.
3794 * But we can make the path absolute, assuming it is relative
3795 * to the current working directory, and then canonicalize it.
3797 if (!got_path_is_absolute(arg)) {
3798 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3799 err = got_error_from_errno("asprintf");
3800 goto done;
3804 err = got_canonpath(abspath ? abspath : arg, canonpath,
3805 sizeof(canonpath));
3806 if (err)
3807 goto done;
3808 resolved = strdup(canonpath);
3809 if (resolved == NULL) {
3810 err = got_error_from_errno("strdup");
3811 goto done;
3813 } else {
3814 resolved = realpath(arg, NULL);
3815 if (resolved == NULL) {
3816 if (errno != ENOENT) {
3817 err = got_error_from_errno2("realpath", arg);
3818 goto done;
3820 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3821 err = got_error_from_errno("asprintf");
3822 goto done;
3824 err = got_canonpath(abspath, canonpath,
3825 sizeof(canonpath));
3826 if (err)
3827 goto done;
3828 resolved = strdup(canonpath);
3829 if (resolved == NULL) {
3830 err = got_error_from_errno("strdup");
3831 goto done;
3836 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3837 strlen(got_worktree_get_root_path(worktree)))) {
3838 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3839 goto done;
3842 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3843 err = got_path_skip_common_ancestor(&path,
3844 got_worktree_get_root_path(worktree), resolved);
3845 if (err)
3846 goto done;
3847 } else {
3848 path = strdup("");
3849 if (path == NULL) {
3850 err = got_error_from_errno("strdup");
3851 goto done;
3855 /* XXX status walk can't deal with trailing slash! */
3856 len = strlen(path);
3857 while (len > 0 && path[len - 1] == '/') {
3858 path[len - 1] = '\0';
3859 len--;
3861 done:
3862 free(abspath);
3863 free(resolved);
3864 free(cwd);
3865 if (err == NULL)
3866 *wt_path = path;
3867 else
3868 free(path);
3869 return err;
3872 struct schedule_addition_args {
3873 struct got_worktree *worktree;
3874 struct got_fileindex *fileindex;
3875 got_worktree_checkout_cb progress_cb;
3876 void *progress_arg;
3877 struct got_repository *repo;
3880 static const struct got_error *
3881 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3882 const char *relpath, struct got_object_id *blob_id,
3883 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3884 int dirfd, const char *de_name)
3886 struct schedule_addition_args *a = arg;
3887 const struct got_error *err = NULL;
3888 struct got_fileindex_entry *ie;
3889 struct stat sb;
3890 char *ondisk_path;
3892 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3893 relpath) == -1)
3894 return got_error_from_errno("asprintf");
3896 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3897 if (ie) {
3898 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3899 de_name, a->repo);
3900 if (err)
3901 goto done;
3902 /* Re-adding an existing entry is a no-op. */
3903 if (status == GOT_STATUS_ADD)
3904 goto done;
3905 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3906 if (err)
3907 goto done;
3910 if (status != GOT_STATUS_UNVERSIONED) {
3911 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3912 goto done;
3915 err = got_fileindex_entry_alloc(&ie, relpath);
3916 if (err)
3917 goto done;
3918 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3919 relpath, NULL, NULL, 1);
3920 if (err) {
3921 got_fileindex_entry_free(ie);
3922 goto done;
3924 err = got_fileindex_entry_add(a->fileindex, ie);
3925 if (err) {
3926 got_fileindex_entry_free(ie);
3927 goto done;
3929 done:
3930 free(ondisk_path);
3931 if (err)
3932 return err;
3933 if (status == GOT_STATUS_ADD)
3934 return NULL;
3935 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3938 const struct got_error *
3939 got_worktree_schedule_add(struct got_worktree *worktree,
3940 struct got_pathlist_head *paths,
3941 got_worktree_checkout_cb progress_cb, void *progress_arg,
3942 struct got_repository *repo, int no_ignores)
3944 struct got_fileindex *fileindex = NULL;
3945 char *fileindex_path = NULL;
3946 const struct got_error *err = NULL, *sync_err, *unlockerr;
3947 struct got_pathlist_entry *pe;
3948 struct schedule_addition_args saa;
3950 err = lock_worktree(worktree, LOCK_EX);
3951 if (err)
3952 return err;
3954 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3955 if (err)
3956 goto done;
3958 saa.worktree = worktree;
3959 saa.fileindex = fileindex;
3960 saa.progress_cb = progress_cb;
3961 saa.progress_arg = progress_arg;
3962 saa.repo = repo;
3964 TAILQ_FOREACH(pe, paths, entry) {
3965 err = worktree_status(worktree, pe->path, fileindex, repo,
3966 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3967 if (err)
3968 break;
3970 sync_err = sync_fileindex(fileindex, fileindex_path);
3971 if (sync_err && err == NULL)
3972 err = sync_err;
3973 done:
3974 free(fileindex_path);
3975 if (fileindex)
3976 got_fileindex_free(fileindex);
3977 unlockerr = lock_worktree(worktree, LOCK_SH);
3978 if (unlockerr && err == NULL)
3979 err = unlockerr;
3980 return err;
3983 struct schedule_deletion_args {
3984 struct got_worktree *worktree;
3985 struct got_fileindex *fileindex;
3986 got_worktree_delete_cb progress_cb;
3987 void *progress_arg;
3988 struct got_repository *repo;
3989 int delete_local_mods;
3990 int keep_on_disk;
3991 const char *status_codes;
3994 static const struct got_error *
3995 schedule_for_deletion(void *arg, unsigned char status,
3996 unsigned char staged_status, const char *relpath,
3997 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3998 struct got_object_id *commit_id, int dirfd, const char *de_name)
4000 struct schedule_deletion_args *a = arg;
4001 const struct got_error *err = NULL;
4002 struct got_fileindex_entry *ie = NULL;
4003 struct stat sb;
4004 char *ondisk_path;
4006 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4007 if (ie == NULL)
4008 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4010 staged_status = get_staged_status(ie);
4011 if (staged_status != GOT_STATUS_NO_CHANGE) {
4012 if (staged_status == GOT_STATUS_DELETE)
4013 return NULL;
4014 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4017 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4018 relpath) == -1)
4019 return got_error_from_errno("asprintf");
4021 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4022 a->repo);
4023 if (err)
4024 goto done;
4026 if (a->status_codes) {
4027 size_t ncodes = strlen(a->status_codes);
4028 int i;
4029 for (i = 0; i < ncodes ; i++) {
4030 if (status == a->status_codes[i])
4031 break;
4033 if (i == ncodes) {
4034 /* Do not delete files in non-matching status. */
4035 free(ondisk_path);
4036 return NULL;
4038 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4039 a->status_codes[i] != GOT_STATUS_MISSING) {
4040 static char msg[64];
4041 snprintf(msg, sizeof(msg),
4042 "invalid status code '%c'", a->status_codes[i]);
4043 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4044 goto done;
4048 if (status != GOT_STATUS_NO_CHANGE) {
4049 if (status == GOT_STATUS_DELETE)
4050 goto done;
4051 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4052 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4053 goto done;
4055 if (status != GOT_STATUS_MODIFY &&
4056 status != GOT_STATUS_MISSING) {
4057 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4058 goto done;
4062 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4063 size_t root_len;
4065 if (dirfd != -1) {
4066 if (unlinkat(dirfd, de_name, 0) != 0) {
4067 err = got_error_from_errno2("unlinkat",
4068 ondisk_path);
4069 goto done;
4071 } else if (unlink(ondisk_path) != 0) {
4072 err = got_error_from_errno2("unlink", ondisk_path);
4073 goto done;
4076 root_len = strlen(a->worktree->root_path);
4077 do {
4078 char *parent;
4079 err = got_path_dirname(&parent, ondisk_path);
4080 if (err)
4081 goto done;
4082 free(ondisk_path);
4083 ondisk_path = parent;
4084 if (rmdir(ondisk_path) == -1) {
4085 if (errno != ENOTEMPTY)
4086 err = got_error_from_errno2("rmdir",
4087 ondisk_path);
4088 break;
4090 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4091 strlen(ondisk_path), root_len) != 0);
4094 got_fileindex_entry_mark_deleted_from_disk(ie);
4095 done:
4096 free(ondisk_path);
4097 if (err)
4098 return err;
4099 if (status == GOT_STATUS_DELETE)
4100 return NULL;
4101 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4102 staged_status, relpath);
4105 const struct got_error *
4106 got_worktree_schedule_delete(struct got_worktree *worktree,
4107 struct got_pathlist_head *paths, int delete_local_mods,
4108 const char *status_codes,
4109 got_worktree_delete_cb progress_cb, void *progress_arg,
4110 struct got_repository *repo, int keep_on_disk)
4112 struct got_fileindex *fileindex = NULL;
4113 char *fileindex_path = NULL;
4114 const struct got_error *err = NULL, *sync_err, *unlockerr;
4115 struct got_pathlist_entry *pe;
4116 struct schedule_deletion_args sda;
4118 err = lock_worktree(worktree, LOCK_EX);
4119 if (err)
4120 return err;
4122 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4123 if (err)
4124 goto done;
4126 sda.worktree = worktree;
4127 sda.fileindex = fileindex;
4128 sda.progress_cb = progress_cb;
4129 sda.progress_arg = progress_arg;
4130 sda.repo = repo;
4131 sda.delete_local_mods = delete_local_mods;
4132 sda.keep_on_disk = keep_on_disk;
4133 sda.status_codes = status_codes;
4135 TAILQ_FOREACH(pe, paths, entry) {
4136 err = worktree_status(worktree, pe->path, fileindex, repo,
4137 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4138 if (err)
4139 break;
4141 sync_err = sync_fileindex(fileindex, fileindex_path);
4142 if (sync_err && err == NULL)
4143 err = sync_err;
4144 done:
4145 free(fileindex_path);
4146 if (fileindex)
4147 got_fileindex_free(fileindex);
4148 unlockerr = lock_worktree(worktree, LOCK_SH);
4149 if (unlockerr && err == NULL)
4150 err = unlockerr;
4151 return err;
4154 static const struct got_error *
4155 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4157 const struct got_error *err = NULL;
4158 char *line = NULL;
4159 size_t linesize = 0, n;
4160 ssize_t linelen;
4162 linelen = getline(&line, &linesize, infile);
4163 if (linelen == -1) {
4164 if (ferror(infile)) {
4165 err = got_error_from_errno("getline");
4166 goto done;
4168 return NULL;
4170 if (outfile) {
4171 n = fwrite(line, 1, linelen, outfile);
4172 if (n != linelen) {
4173 err = got_ferror(outfile, GOT_ERR_IO);
4174 goto done;
4177 if (rejectfile) {
4178 n = fwrite(line, 1, linelen, rejectfile);
4179 if (n != linelen)
4180 err = got_ferror(outfile, GOT_ERR_IO);
4182 done:
4183 free(line);
4184 return err;
4187 static const struct got_error *
4188 skip_one_line(FILE *f)
4190 char *line = NULL;
4191 size_t linesize = 0;
4192 ssize_t linelen;
4194 linelen = getline(&line, &linesize, f);
4195 if (linelen == -1) {
4196 if (ferror(f))
4197 return got_error_from_errno("getline");
4198 return NULL;
4200 free(line);
4201 return NULL;
4204 static const struct got_error *
4205 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4206 int start_old, int end_old, int start_new, int end_new,
4207 FILE *outfile, FILE *rejectfile)
4209 const struct got_error *err;
4211 /* Copy old file's lines leading up to patch. */
4212 while (!feof(f1) && *line_cur1 < start_old) {
4213 err = copy_one_line(f1, outfile, NULL);
4214 if (err)
4215 return err;
4216 (*line_cur1)++;
4218 /* Skip new file's lines leading up to patch. */
4219 while (!feof(f2) && *line_cur2 < start_new) {
4220 if (rejectfile)
4221 err = copy_one_line(f2, NULL, rejectfile);
4222 else
4223 err = skip_one_line(f2);
4224 if (err)
4225 return err;
4226 (*line_cur2)++;
4228 /* Copy patched lines. */
4229 while (!feof(f2) && *line_cur2 <= end_new) {
4230 err = copy_one_line(f2, outfile, NULL);
4231 if (err)
4232 return err;
4233 (*line_cur2)++;
4235 /* Skip over old file's replaced lines. */
4236 while (!feof(f1) && *line_cur1 <= end_old) {
4237 if (rejectfile)
4238 err = copy_one_line(f1, NULL, rejectfile);
4239 else
4240 err = skip_one_line(f1);
4241 if (err)
4242 return err;
4243 (*line_cur1)++;
4246 return NULL;
4249 static const struct got_error *
4250 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4251 FILE *outfile, FILE *rejectfile)
4253 const struct got_error *err;
4255 if (outfile) {
4256 /* Copy old file's lines until EOF. */
4257 while (!feof(f1)) {
4258 err = copy_one_line(f1, outfile, NULL);
4259 if (err)
4260 return err;
4261 (*line_cur1)++;
4264 if (rejectfile) {
4265 /* Copy new file's lines until EOF. */
4266 while (!feof(f2)) {
4267 err = copy_one_line(f2, NULL, rejectfile);
4268 if (err)
4269 return err;
4270 (*line_cur2)++;
4274 return NULL;
4277 static const struct got_error *
4278 apply_or_reject_change(int *choice, int *nchunks_used,
4279 struct diff_result *diff_result, int n,
4280 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4281 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4282 got_worktree_patch_cb patch_cb, void *patch_arg)
4284 const struct got_error *err = NULL;
4285 struct diff_chunk_context cc = {};
4286 int start_old, end_old, start_new, end_new;
4287 FILE *hunkfile;
4288 struct diff_output_unidiff_state *diff_state;
4289 struct diff_input_info diff_info;
4290 int rc;
4292 *choice = GOT_PATCH_CHOICE_NONE;
4294 /* Get changed line numbers without context lines for copy_change(). */
4295 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4296 start_old = cc.left.start;
4297 end_old = cc.left.end;
4298 start_new = cc.right.start;
4299 end_new = cc.right.end;
4301 /* Get the same change with context lines for display. */
4302 memset(&cc, 0, sizeof(cc));
4303 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4305 memset(&diff_info, 0, sizeof(diff_info));
4306 diff_info.left_path = relpath;
4307 diff_info.right_path = relpath;
4309 diff_state = diff_output_unidiff_state_alloc();
4310 if (diff_state == NULL)
4311 return got_error_set_errno(ENOMEM,
4312 "diff_output_unidiff_state_alloc");
4314 hunkfile = got_opentemp();
4315 if (hunkfile == NULL) {
4316 err = got_error_from_errno("got_opentemp");
4317 goto done;
4320 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4321 diff_result, &cc);
4322 if (rc != DIFF_RC_OK) {
4323 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4324 goto done;
4327 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4328 err = got_ferror(hunkfile, GOT_ERR_IO);
4329 goto done;
4332 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4333 hunkfile, changeno, nchanges);
4334 if (err)
4335 goto done;
4337 switch (*choice) {
4338 case GOT_PATCH_CHOICE_YES:
4339 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4340 end_old, start_new, end_new, outfile, rejectfile);
4341 break;
4342 case GOT_PATCH_CHOICE_NO:
4343 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4344 end_old, start_new, end_new, rejectfile, outfile);
4345 break;
4346 case GOT_PATCH_CHOICE_QUIT:
4347 break;
4348 default:
4349 err = got_error(GOT_ERR_PATCH_CHOICE);
4350 break;
4352 done:
4353 diff_output_unidiff_state_free(diff_state);
4354 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4355 err = got_error_from_errno("fclose");
4356 return err;
4359 struct revert_file_args {
4360 struct got_worktree *worktree;
4361 struct got_fileindex *fileindex;
4362 got_worktree_checkout_cb progress_cb;
4363 void *progress_arg;
4364 got_worktree_patch_cb patch_cb;
4365 void *patch_arg;
4366 struct got_repository *repo;
4369 static const struct got_error *
4370 create_patched_content(char **path_outfile, int reverse_patch,
4371 struct got_object_id *blob_id, const char *path2,
4372 int dirfd2, const char *de_name2,
4373 const char *relpath, struct got_repository *repo,
4374 got_worktree_patch_cb patch_cb, void *patch_arg)
4376 const struct got_error *err, *free_err;
4377 struct got_blob_object *blob = NULL;
4378 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4379 int fd2 = -1;
4380 char link_target[PATH_MAX];
4381 ssize_t link_len = 0;
4382 char *path1 = NULL, *id_str = NULL;
4383 struct stat sb2;
4384 struct got_diffreg_result *diffreg_result = NULL;
4385 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4386 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4388 *path_outfile = NULL;
4390 err = got_object_id_str(&id_str, blob_id);
4391 if (err)
4392 return err;
4394 if (dirfd2 != -1) {
4395 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4396 if (fd2 == -1) {
4397 if (errno != ELOOP) {
4398 err = got_error_from_errno2("openat", path2);
4399 goto done;
4401 link_len = readlinkat(dirfd2, de_name2,
4402 link_target, sizeof(link_target));
4403 if (link_len == -1)
4404 return got_error_from_errno2("readlinkat", path2);
4405 sb2.st_mode = S_IFLNK;
4406 sb2.st_size = link_len;
4408 } else {
4409 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4410 if (fd2 == -1) {
4411 if (errno != ELOOP) {
4412 err = got_error_from_errno2("open", path2);
4413 goto done;
4415 link_len = readlink(path2, link_target,
4416 sizeof(link_target));
4417 if (link_len == -1)
4418 return got_error_from_errno2("readlink", path2);
4419 sb2.st_mode = S_IFLNK;
4420 sb2.st_size = link_len;
4423 if (fd2 != -1) {
4424 if (fstat(fd2, &sb2) == -1) {
4425 err = got_error_from_errno2("fstat", path2);
4426 goto done;
4429 f2 = fdopen(fd2, "r");
4430 if (f2 == NULL) {
4431 err = got_error_from_errno2("fdopen", path2);
4432 goto done;
4434 fd2 = -1;
4435 } else {
4436 size_t n;
4437 f2 = got_opentemp();
4438 if (f2 == NULL) {
4439 err = got_error_from_errno2("got_opentemp", path2);
4440 goto done;
4442 n = fwrite(link_target, 1, link_len, f2);
4443 if (n != link_len) {
4444 err = got_ferror(f2, GOT_ERR_IO);
4445 goto done;
4447 if (fflush(f2) == EOF) {
4448 err = got_error_from_errno("fflush");
4449 goto done;
4451 rewind(f2);
4454 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4455 if (err)
4456 goto done;
4458 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4459 if (err)
4460 goto done;
4462 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4463 if (err)
4464 goto done;
4466 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4467 NULL);
4468 if (err)
4469 goto done;
4471 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4472 if (err)
4473 goto done;
4475 if (fseek(f1, 0L, SEEK_SET) == -1)
4476 return got_ferror(f1, GOT_ERR_IO);
4477 if (fseek(f2, 0L, SEEK_SET) == -1)
4478 return got_ferror(f2, GOT_ERR_IO);
4480 /* Count the number of actual changes in the diff result. */
4481 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4482 struct diff_chunk_context cc = {};
4483 diff_chunk_context_load_change(&cc, &nchunks_used,
4484 diffreg_result->result, n, 0);
4485 nchanges++;
4487 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4488 int choice;
4489 err = apply_or_reject_change(&choice, &nchunks_used,
4490 diffreg_result->result, n, relpath, f1, f2,
4491 &line_cur1, &line_cur2,
4492 reverse_patch ? NULL : outfile,
4493 reverse_patch ? outfile : NULL,
4494 ++i, nchanges, patch_cb, patch_arg);
4495 if (err)
4496 goto done;
4497 if (choice == GOT_PATCH_CHOICE_YES)
4498 have_content = 1;
4499 else if (choice == GOT_PATCH_CHOICE_QUIT)
4500 break;
4502 if (have_content) {
4503 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4504 reverse_patch ? NULL : outfile,
4505 reverse_patch ? outfile : NULL);
4506 if (err)
4507 goto done;
4509 if (!S_ISLNK(sb2.st_mode)) {
4510 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4511 err = got_error_from_errno2("fchmod", path2);
4512 goto done;
4516 done:
4517 free(id_str);
4518 if (blob)
4519 got_object_blob_close(blob);
4520 free_err = got_diffreg_result_free(diffreg_result);
4521 if (err == NULL)
4522 err = free_err;
4523 if (f1 && fclose(f1) == EOF && err == NULL)
4524 err = got_error_from_errno2("fclose", path1);
4525 if (f2 && fclose(f2) == EOF && err == NULL)
4526 err = got_error_from_errno2("fclose", path2);
4527 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4528 err = got_error_from_errno2("close", path2);
4529 if (outfile && fclose(outfile) == EOF && err == NULL)
4530 err = got_error_from_errno2("fclose", *path_outfile);
4531 if (path1 && unlink(path1) == -1 && err == NULL)
4532 err = got_error_from_errno2("unlink", path1);
4533 if (err || !have_content) {
4534 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4535 err = got_error_from_errno2("unlink", *path_outfile);
4536 free(*path_outfile);
4537 *path_outfile = NULL;
4539 free(path1);
4540 return err;
4543 static const struct got_error *
4544 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4545 const char *relpath, struct got_object_id *blob_id,
4546 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4547 int dirfd, const char *de_name)
4549 struct revert_file_args *a = arg;
4550 const struct got_error *err = NULL;
4551 char *parent_path = NULL;
4552 struct got_fileindex_entry *ie;
4553 struct got_tree_object *tree = NULL;
4554 struct got_object_id *tree_id = NULL;
4555 const struct got_tree_entry *te = NULL;
4556 char *tree_path = NULL, *te_name;
4557 char *ondisk_path = NULL, *path_content = NULL;
4558 struct got_blob_object *blob = NULL;
4560 /* Reverting a staged deletion is a no-op. */
4561 if (status == GOT_STATUS_DELETE &&
4562 staged_status != GOT_STATUS_NO_CHANGE)
4563 return NULL;
4565 if (status == GOT_STATUS_UNVERSIONED)
4566 return (*a->progress_cb)(a->progress_arg,
4567 GOT_STATUS_UNVERSIONED, relpath);
4569 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4570 if (ie == NULL)
4571 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4573 /* Construct in-repository path of tree which contains this blob. */
4574 err = got_path_dirname(&parent_path, ie->path);
4575 if (err) {
4576 if (err->code != GOT_ERR_BAD_PATH)
4577 goto done;
4578 parent_path = strdup("/");
4579 if (parent_path == NULL) {
4580 err = got_error_from_errno("strdup");
4581 goto done;
4584 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4585 tree_path = strdup(parent_path);
4586 if (tree_path == NULL) {
4587 err = got_error_from_errno("strdup");
4588 goto done;
4590 } else {
4591 if (got_path_is_root_dir(parent_path)) {
4592 tree_path = strdup(a->worktree->path_prefix);
4593 if (tree_path == NULL) {
4594 err = got_error_from_errno("strdup");
4595 goto done;
4597 } else {
4598 if (asprintf(&tree_path, "%s/%s",
4599 a->worktree->path_prefix, parent_path) == -1) {
4600 err = got_error_from_errno("asprintf");
4601 goto done;
4606 err = got_object_id_by_path(&tree_id, a->repo,
4607 a->worktree->base_commit_id, tree_path);
4608 if (err) {
4609 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4610 (status == GOT_STATUS_ADD ||
4611 staged_status == GOT_STATUS_ADD)))
4612 goto done;
4613 } else {
4614 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4615 if (err)
4616 goto done;
4618 err = got_path_basename(&te_name, ie->path);
4619 if (err)
4620 goto done;
4622 te = got_object_tree_find_entry(tree, te_name);
4623 free(te_name);
4624 if (te == NULL && status != GOT_STATUS_ADD &&
4625 staged_status != GOT_STATUS_ADD) {
4626 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4627 goto done;
4631 switch (status) {
4632 case GOT_STATUS_ADD:
4633 if (a->patch_cb) {
4634 int choice = GOT_PATCH_CHOICE_NONE;
4635 err = (*a->patch_cb)(&choice, a->patch_arg,
4636 status, ie->path, NULL, 1, 1);
4637 if (err)
4638 goto done;
4639 if (choice != GOT_PATCH_CHOICE_YES)
4640 break;
4642 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4643 ie->path);
4644 if (err)
4645 goto done;
4646 got_fileindex_entry_remove(a->fileindex, ie);
4647 break;
4648 case GOT_STATUS_DELETE:
4649 if (a->patch_cb) {
4650 int choice = GOT_PATCH_CHOICE_NONE;
4651 err = (*a->patch_cb)(&choice, a->patch_arg,
4652 status, ie->path, NULL, 1, 1);
4653 if (err)
4654 goto done;
4655 if (choice != GOT_PATCH_CHOICE_YES)
4656 break;
4658 /* fall through */
4659 case GOT_STATUS_MODIFY:
4660 case GOT_STATUS_MODE_CHANGE:
4661 case GOT_STATUS_CONFLICT:
4662 case GOT_STATUS_MISSING: {
4663 struct got_object_id id;
4664 if (staged_status == GOT_STATUS_ADD ||
4665 staged_status == GOT_STATUS_MODIFY) {
4666 memcpy(id.sha1, ie->staged_blob_sha1,
4667 SHA1_DIGEST_LENGTH);
4668 } else
4669 memcpy(id.sha1, ie->blob_sha1,
4670 SHA1_DIGEST_LENGTH);
4671 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4672 if (err)
4673 goto done;
4675 if (asprintf(&ondisk_path, "%s/%s",
4676 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4677 err = got_error_from_errno("asprintf");
4678 goto done;
4681 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4682 status == GOT_STATUS_CONFLICT)) {
4683 int is_bad_symlink = 0;
4684 err = create_patched_content(&path_content, 1, &id,
4685 ondisk_path, dirfd, de_name, ie->path, a->repo,
4686 a->patch_cb, a->patch_arg);
4687 if (err || path_content == NULL)
4688 break;
4689 if (te && S_ISLNK(te->mode)) {
4690 if (unlink(path_content) == -1) {
4691 err = got_error_from_errno2("unlink",
4692 path_content);
4693 break;
4695 err = install_symlink(&is_bad_symlink,
4696 a->worktree, ondisk_path, ie->path,
4697 blob, 0, 1, 0, a->repo,
4698 a->progress_cb, a->progress_arg);
4699 } else {
4700 if (rename(path_content, ondisk_path) == -1) {
4701 err = got_error_from_errno3("rename",
4702 path_content, ondisk_path);
4703 goto done;
4706 } else {
4707 int is_bad_symlink = 0;
4708 if (te && S_ISLNK(te->mode)) {
4709 err = install_symlink(&is_bad_symlink,
4710 a->worktree, ondisk_path, ie->path,
4711 blob, 0, 1, 0, a->repo,
4712 a->progress_cb, a->progress_arg);
4713 } else {
4714 err = install_blob(a->worktree, ondisk_path,
4715 ie->path,
4716 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4717 got_fileindex_perms_to_st(ie), blob,
4718 0, 1, 0, 0, a->repo,
4719 a->progress_cb, a->progress_arg);
4721 if (err)
4722 goto done;
4723 if (status == GOT_STATUS_DELETE ||
4724 status == GOT_STATUS_MODE_CHANGE) {
4725 err = got_fileindex_entry_update(ie,
4726 a->worktree->root_fd, relpath,
4727 blob->id.sha1,
4728 a->worktree->base_commit_id->sha1, 1);
4729 if (err)
4730 goto done;
4732 if (is_bad_symlink) {
4733 got_fileindex_entry_filetype_set(ie,
4734 GOT_FILEIDX_MODE_BAD_SYMLINK);
4737 break;
4739 default:
4740 break;
4742 done:
4743 free(ondisk_path);
4744 free(path_content);
4745 free(parent_path);
4746 free(tree_path);
4747 if (blob)
4748 got_object_blob_close(blob);
4749 if (tree)
4750 got_object_tree_close(tree);
4751 free(tree_id);
4752 return err;
4755 const struct got_error *
4756 got_worktree_revert(struct got_worktree *worktree,
4757 struct got_pathlist_head *paths,
4758 got_worktree_checkout_cb progress_cb, void *progress_arg,
4759 got_worktree_patch_cb patch_cb, void *patch_arg,
4760 struct got_repository *repo)
4762 struct got_fileindex *fileindex = NULL;
4763 char *fileindex_path = NULL;
4764 const struct got_error *err = NULL, *unlockerr = NULL;
4765 const struct got_error *sync_err = NULL;
4766 struct got_pathlist_entry *pe;
4767 struct revert_file_args rfa;
4769 err = lock_worktree(worktree, LOCK_EX);
4770 if (err)
4771 return err;
4773 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4774 if (err)
4775 goto done;
4777 rfa.worktree = worktree;
4778 rfa.fileindex = fileindex;
4779 rfa.progress_cb = progress_cb;
4780 rfa.progress_arg = progress_arg;
4781 rfa.patch_cb = patch_cb;
4782 rfa.patch_arg = patch_arg;
4783 rfa.repo = repo;
4784 TAILQ_FOREACH(pe, paths, entry) {
4785 err = worktree_status(worktree, pe->path, fileindex, repo,
4786 revert_file, &rfa, NULL, NULL, 0, 0);
4787 if (err)
4788 break;
4790 sync_err = sync_fileindex(fileindex, fileindex_path);
4791 if (sync_err && err == NULL)
4792 err = sync_err;
4793 done:
4794 free(fileindex_path);
4795 if (fileindex)
4796 got_fileindex_free(fileindex);
4797 unlockerr = lock_worktree(worktree, LOCK_SH);
4798 if (unlockerr && err == NULL)
4799 err = unlockerr;
4800 return err;
4803 static void
4804 free_commitable(struct got_commitable *ct)
4806 free(ct->path);
4807 free(ct->in_repo_path);
4808 free(ct->ondisk_path);
4809 free(ct->blob_id);
4810 free(ct->base_blob_id);
4811 free(ct->staged_blob_id);
4812 free(ct->base_commit_id);
4813 free(ct);
4816 struct collect_commitables_arg {
4817 struct got_pathlist_head *commitable_paths;
4818 struct got_repository *repo;
4819 struct got_worktree *worktree;
4820 struct got_fileindex *fileindex;
4821 int have_staged_files;
4822 int allow_bad_symlinks;
4825 static const struct got_error *
4826 collect_commitables(void *arg, unsigned char status,
4827 unsigned char staged_status, const char *relpath,
4828 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4829 struct got_object_id *commit_id, int dirfd, const char *de_name)
4831 struct collect_commitables_arg *a = arg;
4832 const struct got_error *err = NULL;
4833 struct got_commitable *ct = NULL;
4834 struct got_pathlist_entry *new = NULL;
4835 char *parent_path = NULL, *path = NULL;
4836 struct stat sb;
4838 if (a->have_staged_files) {
4839 if (staged_status != GOT_STATUS_MODIFY &&
4840 staged_status != GOT_STATUS_ADD &&
4841 staged_status != GOT_STATUS_DELETE)
4842 return NULL;
4843 } else {
4844 if (status == GOT_STATUS_CONFLICT)
4845 return got_error(GOT_ERR_COMMIT_CONFLICT);
4847 if (status != GOT_STATUS_MODIFY &&
4848 status != GOT_STATUS_MODE_CHANGE &&
4849 status != GOT_STATUS_ADD &&
4850 status != GOT_STATUS_DELETE)
4851 return NULL;
4854 if (asprintf(&path, "/%s", relpath) == -1) {
4855 err = got_error_from_errno("asprintf");
4856 goto done;
4858 if (strcmp(path, "/") == 0) {
4859 parent_path = strdup("");
4860 if (parent_path == NULL)
4861 return got_error_from_errno("strdup");
4862 } else {
4863 err = got_path_dirname(&parent_path, path);
4864 if (err)
4865 return err;
4868 ct = calloc(1, sizeof(*ct));
4869 if (ct == NULL) {
4870 err = got_error_from_errno("calloc");
4871 goto done;
4874 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4875 relpath) == -1) {
4876 err = got_error_from_errno("asprintf");
4877 goto done;
4880 if (staged_status == GOT_STATUS_ADD ||
4881 staged_status == GOT_STATUS_MODIFY) {
4882 struct got_fileindex_entry *ie;
4883 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4884 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4885 case GOT_FILEIDX_MODE_REGULAR_FILE:
4886 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4887 ct->mode = S_IFREG;
4888 break;
4889 case GOT_FILEIDX_MODE_SYMLINK:
4890 ct->mode = S_IFLNK;
4891 break;
4892 default:
4893 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4894 goto done;
4896 ct->mode |= got_fileindex_entry_perms_get(ie);
4897 } else if (status != GOT_STATUS_DELETE &&
4898 staged_status != GOT_STATUS_DELETE) {
4899 if (dirfd != -1) {
4900 if (fstatat(dirfd, de_name, &sb,
4901 AT_SYMLINK_NOFOLLOW) == -1) {
4902 err = got_error_from_errno2("fstatat",
4903 ct->ondisk_path);
4904 goto done;
4906 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4907 err = got_error_from_errno2("lstat", ct->ondisk_path);
4908 goto done;
4910 ct->mode = sb.st_mode;
4913 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4914 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4915 relpath) == -1) {
4916 err = got_error_from_errno("asprintf");
4917 goto done;
4920 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4921 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4922 int is_bad_symlink;
4923 char target_path[PATH_MAX];
4924 ssize_t target_len;
4925 target_len = readlink(ct->ondisk_path, target_path,
4926 sizeof(target_path));
4927 if (target_len == -1) {
4928 err = got_error_from_errno2("readlink",
4929 ct->ondisk_path);
4930 goto done;
4932 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4933 target_len, ct->ondisk_path, a->worktree->root_path);
4934 if (err)
4935 goto done;
4936 if (is_bad_symlink) {
4937 err = got_error_path(ct->ondisk_path,
4938 GOT_ERR_BAD_SYMLINK);
4939 goto done;
4944 ct->status = status;
4945 ct->staged_status = staged_status;
4946 ct->blob_id = NULL; /* will be filled in when blob gets created */
4947 if (ct->status != GOT_STATUS_ADD &&
4948 ct->staged_status != GOT_STATUS_ADD) {
4949 ct->base_blob_id = got_object_id_dup(blob_id);
4950 if (ct->base_blob_id == NULL) {
4951 err = got_error_from_errno("got_object_id_dup");
4952 goto done;
4954 ct->base_commit_id = got_object_id_dup(commit_id);
4955 if (ct->base_commit_id == NULL) {
4956 err = got_error_from_errno("got_object_id_dup");
4957 goto done;
4960 if (ct->staged_status == GOT_STATUS_ADD ||
4961 ct->staged_status == GOT_STATUS_MODIFY) {
4962 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4963 if (ct->staged_blob_id == NULL) {
4964 err = got_error_from_errno("got_object_id_dup");
4965 goto done;
4968 ct->path = strdup(path);
4969 if (ct->path == NULL) {
4970 err = got_error_from_errno("strdup");
4971 goto done;
4973 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4974 done:
4975 if (ct && (err || new == NULL))
4976 free_commitable(ct);
4977 free(parent_path);
4978 free(path);
4979 return err;
4982 static const struct got_error *write_tree(struct got_object_id **, int *,
4983 struct got_tree_object *, const char *, struct got_pathlist_head *,
4984 got_worktree_status_cb status_cb, void *status_arg,
4985 struct got_repository *);
4987 static const struct got_error *
4988 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4989 struct got_tree_entry *te, const char *parent_path,
4990 struct got_pathlist_head *commitable_paths,
4991 got_worktree_status_cb status_cb, void *status_arg,
4992 struct got_repository *repo)
4994 const struct got_error *err = NULL;
4995 struct got_tree_object *subtree;
4996 char *subpath;
4998 if (asprintf(&subpath, "%s%s%s", parent_path,
4999 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5000 return got_error_from_errno("asprintf");
5002 err = got_object_open_as_tree(&subtree, repo, &te->id);
5003 if (err)
5004 return err;
5006 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5007 commitable_paths, status_cb, status_arg, repo);
5008 got_object_tree_close(subtree);
5009 free(subpath);
5010 return err;
5013 static const struct got_error *
5014 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5016 const struct got_error *err = NULL;
5017 char *ct_parent_path = NULL;
5019 *match = 0;
5021 if (strchr(ct->in_repo_path, '/') == NULL) {
5022 *match = got_path_is_root_dir(path);
5023 return NULL;
5026 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5027 if (err)
5028 return err;
5029 *match = (strcmp(path, ct_parent_path) == 0);
5030 free(ct_parent_path);
5031 return err;
5034 static mode_t
5035 get_ct_file_mode(struct got_commitable *ct)
5037 if (S_ISLNK(ct->mode))
5038 return S_IFLNK;
5040 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5043 static const struct got_error *
5044 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5045 struct got_tree_entry *te, struct got_commitable *ct)
5047 const struct got_error *err = NULL;
5049 *new_te = NULL;
5051 err = got_object_tree_entry_dup(new_te, te);
5052 if (err)
5053 goto done;
5055 (*new_te)->mode = get_ct_file_mode(ct);
5057 if (ct->staged_status == GOT_STATUS_MODIFY)
5058 memcpy(&(*new_te)->id, ct->staged_blob_id,
5059 sizeof((*new_te)->id));
5060 else
5061 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5062 done:
5063 if (err && *new_te) {
5064 free(*new_te);
5065 *new_te = NULL;
5067 return err;
5070 static const struct got_error *
5071 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5072 struct got_commitable *ct)
5074 const struct got_error *err = NULL;
5075 char *ct_name = NULL;
5077 *new_te = NULL;
5079 *new_te = calloc(1, sizeof(**new_te));
5080 if (*new_te == NULL)
5081 return got_error_from_errno("calloc");
5083 err = got_path_basename(&ct_name, ct->path);
5084 if (err)
5085 goto done;
5086 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5087 sizeof((*new_te)->name)) {
5088 err = got_error(GOT_ERR_NO_SPACE);
5089 goto done;
5092 (*new_te)->mode = get_ct_file_mode(ct);
5094 if (ct->staged_status == GOT_STATUS_ADD)
5095 memcpy(&(*new_te)->id, ct->staged_blob_id,
5096 sizeof((*new_te)->id));
5097 else
5098 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5099 done:
5100 free(ct_name);
5101 if (err && *new_te) {
5102 free(*new_te);
5103 *new_te = NULL;
5105 return err;
5108 static const struct got_error *
5109 insert_tree_entry(struct got_tree_entry *new_te,
5110 struct got_pathlist_head *paths)
5112 const struct got_error *err = NULL;
5113 struct got_pathlist_entry *new_pe;
5115 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5116 if (err)
5117 return err;
5118 if (new_pe == NULL)
5119 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5120 return NULL;
5123 static const struct got_error *
5124 report_ct_status(struct got_commitable *ct,
5125 got_worktree_status_cb status_cb, void *status_arg)
5127 const char *ct_path = ct->path;
5128 unsigned char status;
5130 while (ct_path[0] == '/')
5131 ct_path++;
5133 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5134 status = ct->staged_status;
5135 else
5136 status = ct->status;
5138 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5139 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5142 static const struct got_error *
5143 match_modified_subtree(int *modified, struct got_tree_entry *te,
5144 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5146 const struct got_error *err = NULL;
5147 struct got_pathlist_entry *pe;
5148 char *te_path;
5150 *modified = 0;
5152 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5153 got_path_is_root_dir(base_tree_path) ? "" : "/",
5154 te->name) == -1)
5155 return got_error_from_errno("asprintf");
5157 TAILQ_FOREACH(pe, commitable_paths, entry) {
5158 struct got_commitable *ct = pe->data;
5159 *modified = got_path_is_child(ct->in_repo_path, te_path,
5160 strlen(te_path));
5161 if (*modified)
5162 break;
5165 free(te_path);
5166 return err;
5169 static const struct got_error *
5170 match_deleted_or_modified_ct(struct got_commitable **ctp,
5171 struct got_tree_entry *te, const char *base_tree_path,
5172 struct got_pathlist_head *commitable_paths)
5174 const struct got_error *err = NULL;
5175 struct got_pathlist_entry *pe;
5177 *ctp = NULL;
5179 TAILQ_FOREACH(pe, commitable_paths, entry) {
5180 struct got_commitable *ct = pe->data;
5181 char *ct_name = NULL;
5182 int path_matches;
5184 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5185 if (ct->status != GOT_STATUS_MODIFY &&
5186 ct->status != GOT_STATUS_MODE_CHANGE &&
5187 ct->status != GOT_STATUS_DELETE)
5188 continue;
5189 } else {
5190 if (ct->staged_status != GOT_STATUS_MODIFY &&
5191 ct->staged_status != GOT_STATUS_DELETE)
5192 continue;
5195 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5196 continue;
5198 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5199 if (err)
5200 return err;
5201 if (!path_matches)
5202 continue;
5204 err = got_path_basename(&ct_name, pe->path);
5205 if (err)
5206 return err;
5208 if (strcmp(te->name, ct_name) != 0) {
5209 free(ct_name);
5210 continue;
5212 free(ct_name);
5214 *ctp = ct;
5215 break;
5218 return err;
5221 static const struct got_error *
5222 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5223 const char *child_path, const char *path_base_tree,
5224 struct got_pathlist_head *commitable_paths,
5225 got_worktree_status_cb status_cb, void *status_arg,
5226 struct got_repository *repo)
5228 const struct got_error *err = NULL;
5229 struct got_tree_entry *new_te;
5230 char *subtree_path;
5231 struct got_object_id *id = NULL;
5232 int nentries;
5234 *new_tep = NULL;
5236 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5237 got_path_is_root_dir(path_base_tree) ? "" : "/",
5238 child_path) == -1)
5239 return got_error_from_errno("asprintf");
5241 new_te = calloc(1, sizeof(*new_te));
5242 if (new_te == NULL)
5243 return got_error_from_errno("calloc");
5244 new_te->mode = S_IFDIR;
5246 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5247 sizeof(new_te->name)) {
5248 err = got_error(GOT_ERR_NO_SPACE);
5249 goto done;
5251 err = write_tree(&id, &nentries, NULL, subtree_path,
5252 commitable_paths, status_cb, status_arg, repo);
5253 if (err) {
5254 free(new_te);
5255 goto done;
5257 memcpy(&new_te->id, id, sizeof(new_te->id));
5258 done:
5259 free(id);
5260 free(subtree_path);
5261 if (err == NULL)
5262 *new_tep = new_te;
5263 return err;
5266 static const struct got_error *
5267 write_tree(struct got_object_id **new_tree_id, int *nentries,
5268 struct got_tree_object *base_tree, const char *path_base_tree,
5269 struct got_pathlist_head *commitable_paths,
5270 got_worktree_status_cb status_cb, void *status_arg,
5271 struct got_repository *repo)
5273 const struct got_error *err = NULL;
5274 struct got_pathlist_head paths;
5275 struct got_tree_entry *te, *new_te = NULL;
5276 struct got_pathlist_entry *pe;
5278 TAILQ_INIT(&paths);
5279 *nentries = 0;
5281 /* Insert, and recurse into, newly added entries first. */
5282 TAILQ_FOREACH(pe, commitable_paths, entry) {
5283 struct got_commitable *ct = pe->data;
5284 char *child_path = NULL, *slash;
5286 if ((ct->status != GOT_STATUS_ADD &&
5287 ct->staged_status != GOT_STATUS_ADD) ||
5288 (ct->flags & GOT_COMMITABLE_ADDED))
5289 continue;
5291 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5292 strlen(path_base_tree)))
5293 continue;
5295 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5296 ct->in_repo_path);
5297 if (err)
5298 goto done;
5300 slash = strchr(child_path, '/');
5301 if (slash == NULL) {
5302 err = alloc_added_blob_tree_entry(&new_te, ct);
5303 if (err)
5304 goto done;
5305 err = report_ct_status(ct, status_cb, status_arg);
5306 if (err)
5307 goto done;
5308 ct->flags |= GOT_COMMITABLE_ADDED;
5309 err = insert_tree_entry(new_te, &paths);
5310 if (err)
5311 goto done;
5312 (*nentries)++;
5313 } else {
5314 *slash = '\0'; /* trim trailing path components */
5315 if (base_tree == NULL ||
5316 got_object_tree_find_entry(base_tree, child_path)
5317 == NULL) {
5318 err = make_subtree_for_added_blob(&new_te,
5319 child_path, path_base_tree,
5320 commitable_paths, status_cb, status_arg,
5321 repo);
5322 if (err)
5323 goto done;
5324 err = insert_tree_entry(new_te, &paths);
5325 if (err)
5326 goto done;
5327 (*nentries)++;
5332 if (base_tree) {
5333 int i, nbase_entries;
5334 /* Handle modified and deleted entries. */
5335 nbase_entries = got_object_tree_get_nentries(base_tree);
5336 for (i = 0; i < nbase_entries; i++) {
5337 struct got_commitable *ct = NULL;
5339 te = got_object_tree_get_entry(base_tree, i);
5340 if (got_object_tree_entry_is_submodule(te)) {
5341 /* Entry is a submodule; just copy it. */
5342 err = got_object_tree_entry_dup(&new_te, te);
5343 if (err)
5344 goto done;
5345 err = insert_tree_entry(new_te, &paths);
5346 if (err)
5347 goto done;
5348 (*nentries)++;
5349 continue;
5352 if (S_ISDIR(te->mode)) {
5353 int modified;
5354 err = got_object_tree_entry_dup(&new_te, te);
5355 if (err)
5356 goto done;
5357 err = match_modified_subtree(&modified, te,
5358 path_base_tree, commitable_paths);
5359 if (err)
5360 goto done;
5361 /* Avoid recursion into unmodified subtrees. */
5362 if (modified) {
5363 struct got_object_id *new_id;
5364 int nsubentries;
5365 err = write_subtree(&new_id,
5366 &nsubentries, te,
5367 path_base_tree, commitable_paths,
5368 status_cb, status_arg, repo);
5369 if (err)
5370 goto done;
5371 if (nsubentries == 0) {
5372 /* All entries were deleted. */
5373 free(new_id);
5374 continue;
5376 memcpy(&new_te->id, new_id,
5377 sizeof(new_te->id));
5378 free(new_id);
5380 err = insert_tree_entry(new_te, &paths);
5381 if (err)
5382 goto done;
5383 (*nentries)++;
5384 continue;
5387 err = match_deleted_or_modified_ct(&ct, te,
5388 path_base_tree, commitable_paths);
5389 if (err)
5390 goto done;
5391 if (ct) {
5392 /* NB: Deleted entries get dropped here. */
5393 if (ct->status == GOT_STATUS_MODIFY ||
5394 ct->status == GOT_STATUS_MODE_CHANGE ||
5395 ct->staged_status == GOT_STATUS_MODIFY) {
5396 err = alloc_modified_blob_tree_entry(
5397 &new_te, te, ct);
5398 if (err)
5399 goto done;
5400 err = insert_tree_entry(new_te, &paths);
5401 if (err)
5402 goto done;
5403 (*nentries)++;
5405 err = report_ct_status(ct, status_cb,
5406 status_arg);
5407 if (err)
5408 goto done;
5409 } else {
5410 /* Entry is unchanged; just copy it. */
5411 err = got_object_tree_entry_dup(&new_te, te);
5412 if (err)
5413 goto done;
5414 err = insert_tree_entry(new_te, &paths);
5415 if (err)
5416 goto done;
5417 (*nentries)++;
5422 /* Write new list of entries; deleted entries have been dropped. */
5423 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5424 done:
5425 got_pathlist_free(&paths);
5426 return err;
5429 static const struct got_error *
5430 update_fileindex_after_commit(struct got_worktree *worktree,
5431 struct got_pathlist_head *commitable_paths,
5432 struct got_object_id *new_base_commit_id,
5433 struct got_fileindex *fileindex, int have_staged_files)
5435 const struct got_error *err = NULL;
5436 struct got_pathlist_entry *pe;
5437 char *relpath = NULL;
5439 TAILQ_FOREACH(pe, commitable_paths, entry) {
5440 struct got_fileindex_entry *ie;
5441 struct got_commitable *ct = pe->data;
5443 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5445 err = got_path_skip_common_ancestor(&relpath,
5446 worktree->root_path, ct->ondisk_path);
5447 if (err)
5448 goto done;
5450 if (ie) {
5451 if (ct->status == GOT_STATUS_DELETE ||
5452 ct->staged_status == GOT_STATUS_DELETE) {
5453 got_fileindex_entry_remove(fileindex, ie);
5454 } else if (ct->staged_status == GOT_STATUS_ADD ||
5455 ct->staged_status == GOT_STATUS_MODIFY) {
5456 got_fileindex_entry_stage_set(ie,
5457 GOT_FILEIDX_STAGE_NONE);
5458 got_fileindex_entry_staged_filetype_set(ie, 0);
5460 err = got_fileindex_entry_update(ie,
5461 worktree->root_fd, relpath,
5462 ct->staged_blob_id->sha1,
5463 new_base_commit_id->sha1,
5464 !have_staged_files);
5465 } else
5466 err = got_fileindex_entry_update(ie,
5467 worktree->root_fd, relpath,
5468 ct->blob_id->sha1,
5469 new_base_commit_id->sha1,
5470 !have_staged_files);
5471 } else {
5472 err = got_fileindex_entry_alloc(&ie, pe->path);
5473 if (err)
5474 goto done;
5475 err = got_fileindex_entry_update(ie,
5476 worktree->root_fd, relpath, ct->blob_id->sha1,
5477 new_base_commit_id->sha1, 1);
5478 if (err) {
5479 got_fileindex_entry_free(ie);
5480 goto done;
5482 err = got_fileindex_entry_add(fileindex, ie);
5483 if (err) {
5484 got_fileindex_entry_free(ie);
5485 goto done;
5488 free(relpath);
5489 relpath = NULL;
5491 done:
5492 free(relpath);
5493 return err;
5497 static const struct got_error *
5498 check_out_of_date(const char *in_repo_path, unsigned char status,
5499 unsigned char staged_status, struct got_object_id *base_blob_id,
5500 struct got_object_id *base_commit_id,
5501 struct got_object_id *head_commit_id, struct got_repository *repo,
5502 int ood_errcode)
5504 const struct got_error *err = NULL;
5505 struct got_object_id *id = NULL;
5507 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5508 /* Trivial case: base commit == head commit */
5509 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5510 return NULL;
5512 * Ensure file content which local changes were based
5513 * on matches file content in the branch head.
5515 err = got_object_id_by_path(&id, repo, head_commit_id,
5516 in_repo_path);
5517 if (err) {
5518 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5519 err = got_error(ood_errcode);
5520 goto done;
5521 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5522 err = got_error(ood_errcode);
5523 } else {
5524 /* Require that added files don't exist in the branch head. */
5525 err = got_object_id_by_path(&id, repo, head_commit_id,
5526 in_repo_path);
5527 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5528 goto done;
5529 err = id ? got_error(ood_errcode) : NULL;
5531 done:
5532 free(id);
5533 return err;
5536 const struct got_error *
5537 commit_worktree(struct got_object_id **new_commit_id,
5538 struct got_pathlist_head *commitable_paths,
5539 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5540 const char *author, const char *committer,
5541 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5542 got_worktree_status_cb status_cb, void *status_arg,
5543 struct got_repository *repo)
5545 const struct got_error *err = NULL, *unlockerr = NULL;
5546 struct got_pathlist_entry *pe;
5547 const char *head_ref_name = NULL;
5548 struct got_commit_object *head_commit = NULL;
5549 struct got_reference *head_ref2 = NULL;
5550 struct got_object_id *head_commit_id2 = NULL;
5551 struct got_tree_object *head_tree = NULL;
5552 struct got_object_id *new_tree_id = NULL;
5553 int nentries;
5554 struct got_object_id_queue parent_ids;
5555 struct got_object_qid *pid = NULL;
5556 char *logmsg = NULL;
5558 *new_commit_id = NULL;
5560 STAILQ_INIT(&parent_ids);
5562 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5563 if (err)
5564 goto done;
5566 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5567 if (err)
5568 goto done;
5570 if (commit_msg_cb != NULL) {
5571 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5572 if (err)
5573 goto done;
5576 if (logmsg == NULL || strlen(logmsg) == 0) {
5577 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5578 goto done;
5581 /* Create blobs from added and modified files and record their IDs. */
5582 TAILQ_FOREACH(pe, commitable_paths, entry) {
5583 struct got_commitable *ct = pe->data;
5584 char *ondisk_path;
5586 /* Blobs for staged files already exist. */
5587 if (ct->staged_status == GOT_STATUS_ADD ||
5588 ct->staged_status == GOT_STATUS_MODIFY)
5589 continue;
5591 if (ct->status != GOT_STATUS_ADD &&
5592 ct->status != GOT_STATUS_MODIFY &&
5593 ct->status != GOT_STATUS_MODE_CHANGE)
5594 continue;
5596 if (asprintf(&ondisk_path, "%s/%s",
5597 worktree->root_path, pe->path) == -1) {
5598 err = got_error_from_errno("asprintf");
5599 goto done;
5601 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5602 free(ondisk_path);
5603 if (err)
5604 goto done;
5607 /* Recursively write new tree objects. */
5608 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5609 commitable_paths, status_cb, status_arg, repo);
5610 if (err)
5611 goto done;
5613 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5614 if (err)
5615 goto done;
5616 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5617 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5618 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5619 got_object_qid_free(pid);
5620 if (logmsg != NULL)
5621 free(logmsg);
5622 if (err)
5623 goto done;
5625 /* Check if a concurrent commit to our branch has occurred. */
5626 head_ref_name = got_worktree_get_head_ref_name(worktree);
5627 if (head_ref_name == NULL) {
5628 err = got_error_from_errno("got_worktree_get_head_ref_name");
5629 goto done;
5631 /* Lock the reference here to prevent concurrent modification. */
5632 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5633 if (err)
5634 goto done;
5635 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5636 if (err)
5637 goto done;
5638 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5639 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5640 goto done;
5642 /* Update branch head in repository. */
5643 err = got_ref_change_ref(head_ref2, *new_commit_id);
5644 if (err)
5645 goto done;
5646 err = got_ref_write(head_ref2, repo);
5647 if (err)
5648 goto done;
5650 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5651 if (err)
5652 goto done;
5654 err = ref_base_commit(worktree, repo);
5655 if (err)
5656 goto done;
5657 done:
5658 if (head_tree)
5659 got_object_tree_close(head_tree);
5660 if (head_commit)
5661 got_object_commit_close(head_commit);
5662 free(head_commit_id2);
5663 if (head_ref2) {
5664 unlockerr = got_ref_unlock(head_ref2);
5665 if (unlockerr && err == NULL)
5666 err = unlockerr;
5667 got_ref_close(head_ref2);
5669 return err;
5672 static const struct got_error *
5673 check_path_is_commitable(const char *path,
5674 struct got_pathlist_head *commitable_paths)
5676 struct got_pathlist_entry *cpe = NULL;
5677 size_t path_len = strlen(path);
5679 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5680 struct got_commitable *ct = cpe->data;
5681 const char *ct_path = ct->path;
5683 while (ct_path[0] == '/')
5684 ct_path++;
5686 if (strcmp(path, ct_path) == 0 ||
5687 got_path_is_child(ct_path, path, path_len))
5688 break;
5691 if (cpe == NULL)
5692 return got_error_path(path, GOT_ERR_BAD_PATH);
5694 return NULL;
5697 static const struct got_error *
5698 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5700 int *have_staged_files = arg;
5702 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5703 *have_staged_files = 1;
5704 return got_error(GOT_ERR_CANCELLED);
5707 return NULL;
5710 static const struct got_error *
5711 check_non_staged_files(struct got_fileindex *fileindex,
5712 struct got_pathlist_head *paths)
5714 struct got_pathlist_entry *pe;
5715 struct got_fileindex_entry *ie;
5717 TAILQ_FOREACH(pe, paths, entry) {
5718 if (pe->path[0] == '\0')
5719 continue;
5720 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5721 if (ie == NULL)
5722 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5723 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5724 return got_error_path(pe->path,
5725 GOT_ERR_FILE_NOT_STAGED);
5728 return NULL;
5731 const struct got_error *
5732 got_worktree_commit(struct got_object_id **new_commit_id,
5733 struct got_worktree *worktree, struct got_pathlist_head *paths,
5734 const char *author, const char *committer, int allow_bad_symlinks,
5735 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5736 got_worktree_status_cb status_cb, void *status_arg,
5737 struct got_repository *repo)
5739 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5740 struct got_fileindex *fileindex = NULL;
5741 char *fileindex_path = NULL;
5742 struct got_pathlist_head commitable_paths;
5743 struct collect_commitables_arg cc_arg;
5744 struct got_pathlist_entry *pe;
5745 struct got_reference *head_ref = NULL;
5746 struct got_object_id *head_commit_id = NULL;
5747 int have_staged_files = 0;
5749 *new_commit_id = NULL;
5751 TAILQ_INIT(&commitable_paths);
5753 err = lock_worktree(worktree, LOCK_EX);
5754 if (err)
5755 goto done;
5757 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5758 if (err)
5759 goto done;
5761 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5762 if (err)
5763 goto done;
5765 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5766 if (err)
5767 goto done;
5769 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5770 &have_staged_files);
5771 if (err && err->code != GOT_ERR_CANCELLED)
5772 goto done;
5773 if (have_staged_files) {
5774 err = check_non_staged_files(fileindex, paths);
5775 if (err)
5776 goto done;
5779 cc_arg.commitable_paths = &commitable_paths;
5780 cc_arg.worktree = worktree;
5781 cc_arg.fileindex = fileindex;
5782 cc_arg.repo = repo;
5783 cc_arg.have_staged_files = have_staged_files;
5784 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5785 TAILQ_FOREACH(pe, paths, entry) {
5786 err = worktree_status(worktree, pe->path, fileindex, repo,
5787 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5788 if (err)
5789 goto done;
5792 if (TAILQ_EMPTY(&commitable_paths)) {
5793 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5794 goto done;
5797 TAILQ_FOREACH(pe, paths, entry) {
5798 err = check_path_is_commitable(pe->path, &commitable_paths);
5799 if (err)
5800 goto done;
5803 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5804 struct got_commitable *ct = pe->data;
5805 const char *ct_path = ct->in_repo_path;
5807 while (ct_path[0] == '/')
5808 ct_path++;
5809 err = check_out_of_date(ct_path, ct->status,
5810 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5811 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5812 if (err)
5813 goto done;
5817 err = commit_worktree(new_commit_id, &commitable_paths,
5818 head_commit_id, worktree, author, committer,
5819 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5820 if (err)
5821 goto done;
5823 err = update_fileindex_after_commit(worktree, &commitable_paths,
5824 *new_commit_id, fileindex, have_staged_files);
5825 sync_err = sync_fileindex(fileindex, fileindex_path);
5826 if (sync_err && err == NULL)
5827 err = sync_err;
5828 done:
5829 if (fileindex)
5830 got_fileindex_free(fileindex);
5831 free(fileindex_path);
5832 unlockerr = lock_worktree(worktree, LOCK_SH);
5833 if (unlockerr && err == NULL)
5834 err = unlockerr;
5835 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5836 struct got_commitable *ct = pe->data;
5837 free_commitable(ct);
5839 got_pathlist_free(&commitable_paths);
5840 return err;
5843 const char *
5844 got_commitable_get_path(struct got_commitable *ct)
5846 return ct->path;
5849 unsigned int
5850 got_commitable_get_status(struct got_commitable *ct)
5852 return ct->status;
5855 struct check_rebase_ok_arg {
5856 struct got_worktree *worktree;
5857 struct got_repository *repo;
5860 static const struct got_error *
5861 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5863 const struct got_error *err = NULL;
5864 struct check_rebase_ok_arg *a = arg;
5865 unsigned char status;
5866 struct stat sb;
5867 char *ondisk_path;
5869 /* Reject rebase of a work tree with mixed base commits. */
5870 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5871 SHA1_DIGEST_LENGTH))
5872 return got_error(GOT_ERR_MIXED_COMMITS);
5874 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5875 == -1)
5876 return got_error_from_errno("asprintf");
5878 /* Reject rebase of a work tree with modified or staged files. */
5879 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5880 free(ondisk_path);
5881 if (err)
5882 return err;
5884 if (status != GOT_STATUS_NO_CHANGE)
5885 return got_error(GOT_ERR_MODIFIED);
5886 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5887 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5889 return NULL;
5892 const struct got_error *
5893 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5894 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5895 struct got_worktree *worktree, struct got_reference *branch,
5896 struct got_repository *repo)
5898 const struct got_error *err = NULL;
5899 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5900 char *branch_ref_name = NULL;
5901 char *fileindex_path = NULL;
5902 struct check_rebase_ok_arg ok_arg;
5903 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5904 struct got_object_id *wt_branch_tip = NULL;
5906 *new_base_branch_ref = NULL;
5907 *tmp_branch = NULL;
5908 *fileindex = NULL;
5910 err = lock_worktree(worktree, LOCK_EX);
5911 if (err)
5912 return err;
5914 err = open_fileindex(fileindex, &fileindex_path, worktree);
5915 if (err)
5916 goto done;
5918 ok_arg.worktree = worktree;
5919 ok_arg.repo = repo;
5920 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5921 &ok_arg);
5922 if (err)
5923 goto done;
5925 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5926 if (err)
5927 goto done;
5929 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5930 if (err)
5931 goto done;
5933 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5934 if (err)
5935 goto done;
5937 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5938 0);
5939 if (err)
5940 goto done;
5942 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5943 if (err)
5944 goto done;
5945 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5946 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5947 goto done;
5950 err = got_ref_alloc_symref(new_base_branch_ref,
5951 new_base_branch_ref_name, wt_branch);
5952 if (err)
5953 goto done;
5954 err = got_ref_write(*new_base_branch_ref, repo);
5955 if (err)
5956 goto done;
5958 /* TODO Lock original branch's ref while rebasing? */
5960 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5961 if (err)
5962 goto done;
5964 err = got_ref_write(branch_ref, repo);
5965 if (err)
5966 goto done;
5968 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5969 worktree->base_commit_id);
5970 if (err)
5971 goto done;
5972 err = got_ref_write(*tmp_branch, repo);
5973 if (err)
5974 goto done;
5976 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5977 if (err)
5978 goto done;
5979 done:
5980 free(fileindex_path);
5981 free(tmp_branch_name);
5982 free(new_base_branch_ref_name);
5983 free(branch_ref_name);
5984 if (branch_ref)
5985 got_ref_close(branch_ref);
5986 if (wt_branch)
5987 got_ref_close(wt_branch);
5988 free(wt_branch_tip);
5989 if (err) {
5990 if (*new_base_branch_ref) {
5991 got_ref_close(*new_base_branch_ref);
5992 *new_base_branch_ref = NULL;
5994 if (*tmp_branch) {
5995 got_ref_close(*tmp_branch);
5996 *tmp_branch = NULL;
5998 if (*fileindex) {
5999 got_fileindex_free(*fileindex);
6000 *fileindex = NULL;
6002 lock_worktree(worktree, LOCK_SH);
6004 return err;
6007 const struct got_error *
6008 got_worktree_rebase_continue(struct got_object_id **commit_id,
6009 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6010 struct got_reference **branch, struct got_fileindex **fileindex,
6011 struct got_worktree *worktree, struct got_repository *repo)
6013 const struct got_error *err;
6014 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6015 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6016 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6017 char *fileindex_path = NULL;
6018 int have_staged_files = 0;
6020 *commit_id = NULL;
6021 *new_base_branch = NULL;
6022 *tmp_branch = NULL;
6023 *branch = NULL;
6024 *fileindex = NULL;
6026 err = lock_worktree(worktree, LOCK_EX);
6027 if (err)
6028 return err;
6030 err = open_fileindex(fileindex, &fileindex_path, worktree);
6031 if (err)
6032 goto done;
6034 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6035 &have_staged_files);
6036 if (err && err->code != GOT_ERR_CANCELLED)
6037 goto done;
6038 if (have_staged_files) {
6039 err = got_error(GOT_ERR_STAGED_PATHS);
6040 goto done;
6043 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6044 if (err)
6045 goto done;
6047 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6048 if (err)
6049 goto done;
6051 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6052 if (err)
6053 goto done;
6055 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6056 if (err)
6057 goto done;
6059 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6060 if (err)
6061 goto done;
6063 err = got_ref_open(branch, repo,
6064 got_ref_get_symref_target(branch_ref), 0);
6065 if (err)
6066 goto done;
6068 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6069 if (err)
6070 goto done;
6072 err = got_ref_resolve(commit_id, repo, commit_ref);
6073 if (err)
6074 goto done;
6076 err = got_ref_open(new_base_branch, repo,
6077 new_base_branch_ref_name, 0);
6078 if (err)
6079 goto done;
6081 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6082 if (err)
6083 goto done;
6084 done:
6085 free(commit_ref_name);
6086 free(branch_ref_name);
6087 free(fileindex_path);
6088 if (commit_ref)
6089 got_ref_close(commit_ref);
6090 if (branch_ref)
6091 got_ref_close(branch_ref);
6092 if (err) {
6093 free(*commit_id);
6094 *commit_id = NULL;
6095 if (*tmp_branch) {
6096 got_ref_close(*tmp_branch);
6097 *tmp_branch = NULL;
6099 if (*new_base_branch) {
6100 got_ref_close(*new_base_branch);
6101 *new_base_branch = NULL;
6103 if (*branch) {
6104 got_ref_close(*branch);
6105 *branch = NULL;
6107 if (*fileindex) {
6108 got_fileindex_free(*fileindex);
6109 *fileindex = NULL;
6111 lock_worktree(worktree, LOCK_SH);
6113 return err;
6116 const struct got_error *
6117 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6119 const struct got_error *err;
6120 char *tmp_branch_name = NULL;
6122 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6123 if (err)
6124 return err;
6126 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6127 free(tmp_branch_name);
6128 return NULL;
6131 static const struct got_error *
6132 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6133 char **logmsg, void *arg)
6135 *logmsg = arg;
6136 return NULL;
6139 static const struct got_error *
6140 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6141 const char *path, struct got_object_id *blob_id,
6142 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6143 int dirfd, const char *de_name)
6145 return NULL;
6148 struct collect_merged_paths_arg {
6149 got_worktree_checkout_cb progress_cb;
6150 void *progress_arg;
6151 struct got_pathlist_head *merged_paths;
6154 static const struct got_error *
6155 collect_merged_paths(void *arg, unsigned char status, const char *path)
6157 const struct got_error *err;
6158 struct collect_merged_paths_arg *a = arg;
6159 char *p;
6160 struct got_pathlist_entry *new;
6162 err = (*a->progress_cb)(a->progress_arg, status, path);
6163 if (err)
6164 return err;
6166 if (status != GOT_STATUS_MERGE &&
6167 status != GOT_STATUS_ADD &&
6168 status != GOT_STATUS_DELETE &&
6169 status != GOT_STATUS_CONFLICT)
6170 return NULL;
6172 p = strdup(path);
6173 if (p == NULL)
6174 return got_error_from_errno("strdup");
6176 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6177 if (err || new == NULL)
6178 free(p);
6179 return err;
6182 void
6183 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6185 struct got_pathlist_entry *pe;
6187 TAILQ_FOREACH(pe, merged_paths, entry)
6188 free((char *)pe->path);
6190 got_pathlist_free(merged_paths);
6193 static const struct got_error *
6194 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6195 int is_rebase, struct got_repository *repo)
6197 const struct got_error *err;
6198 struct got_reference *commit_ref = NULL;
6200 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6201 if (err) {
6202 if (err->code != GOT_ERR_NOT_REF)
6203 goto done;
6204 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6205 if (err)
6206 goto done;
6207 err = got_ref_write(commit_ref, repo);
6208 if (err)
6209 goto done;
6210 } else if (is_rebase) {
6211 struct got_object_id *stored_id;
6212 int cmp;
6214 err = got_ref_resolve(&stored_id, repo, commit_ref);
6215 if (err)
6216 goto done;
6217 cmp = got_object_id_cmp(commit_id, stored_id);
6218 free(stored_id);
6219 if (cmp != 0) {
6220 err = got_error(GOT_ERR_REBASE_COMMITID);
6221 goto done;
6224 done:
6225 if (commit_ref)
6226 got_ref_close(commit_ref);
6227 return err;
6230 static const struct got_error *
6231 rebase_merge_files(struct got_pathlist_head *merged_paths,
6232 const char *commit_ref_name, struct got_worktree *worktree,
6233 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6234 struct got_object_id *commit_id, struct got_repository *repo,
6235 got_worktree_checkout_cb progress_cb, void *progress_arg,
6236 got_cancel_cb cancel_cb, void *cancel_arg)
6238 const struct got_error *err;
6239 struct got_reference *commit_ref = NULL;
6240 struct collect_merged_paths_arg cmp_arg;
6241 char *fileindex_path;
6243 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6245 err = get_fileindex_path(&fileindex_path, worktree);
6246 if (err)
6247 return err;
6249 cmp_arg.progress_cb = progress_cb;
6250 cmp_arg.progress_arg = progress_arg;
6251 cmp_arg.merged_paths = merged_paths;
6252 err = merge_files(worktree, fileindex, fileindex_path,
6253 parent_commit_id, commit_id, repo, collect_merged_paths,
6254 &cmp_arg, cancel_cb, cancel_arg);
6255 if (commit_ref)
6256 got_ref_close(commit_ref);
6257 return err;
6260 const struct got_error *
6261 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6262 struct got_worktree *worktree, struct got_fileindex *fileindex,
6263 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6264 struct got_repository *repo,
6265 got_worktree_checkout_cb progress_cb, void *progress_arg,
6266 got_cancel_cb cancel_cb, void *cancel_arg)
6268 const struct got_error *err;
6269 char *commit_ref_name;
6271 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6272 if (err)
6273 return err;
6275 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6276 if (err)
6277 goto done;
6279 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6280 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6281 progress_arg, cancel_cb, cancel_arg);
6282 done:
6283 free(commit_ref_name);
6284 return err;
6287 const struct got_error *
6288 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6289 struct got_worktree *worktree, struct got_fileindex *fileindex,
6290 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6291 struct got_repository *repo,
6292 got_worktree_checkout_cb progress_cb, void *progress_arg,
6293 got_cancel_cb cancel_cb, void *cancel_arg)
6295 const struct got_error *err;
6296 char *commit_ref_name;
6298 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6299 if (err)
6300 return err;
6302 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6303 if (err)
6304 goto done;
6306 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6307 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6308 progress_arg, cancel_cb, cancel_arg);
6309 done:
6310 free(commit_ref_name);
6311 return err;
6314 static const struct got_error *
6315 rebase_commit(struct got_object_id **new_commit_id,
6316 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6317 struct got_worktree *worktree, struct got_fileindex *fileindex,
6318 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6319 const char *new_logmsg, struct got_repository *repo)
6321 const struct got_error *err, *sync_err;
6322 struct got_pathlist_head commitable_paths;
6323 struct collect_commitables_arg cc_arg;
6324 char *fileindex_path = NULL;
6325 struct got_reference *head_ref = NULL;
6326 struct got_object_id *head_commit_id = NULL;
6327 char *logmsg = NULL;
6329 TAILQ_INIT(&commitable_paths);
6330 *new_commit_id = NULL;
6332 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6334 err = get_fileindex_path(&fileindex_path, worktree);
6335 if (err)
6336 return err;
6338 cc_arg.commitable_paths = &commitable_paths;
6339 cc_arg.worktree = worktree;
6340 cc_arg.repo = repo;
6341 cc_arg.have_staged_files = 0;
6343 * If possible get the status of individual files directly to
6344 * avoid crawling the entire work tree once per rebased commit.
6345 * TODO: Ideally, merged_paths would contain a list of commitables
6346 * we could use so we could skip worktree_status() entirely.
6348 if (merged_paths) {
6349 struct got_pathlist_entry *pe;
6350 TAILQ_FOREACH(pe, merged_paths, entry) {
6351 err = worktree_status(worktree, pe->path, fileindex,
6352 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6353 0);
6354 if (err)
6355 goto done;
6357 } else {
6358 err = worktree_status(worktree, "", fileindex, repo,
6359 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6360 if (err)
6361 goto done;
6364 if (TAILQ_EMPTY(&commitable_paths)) {
6365 /* No-op change; commit will be elided. */
6366 err = got_ref_delete(commit_ref, repo);
6367 if (err)
6368 goto done;
6369 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6370 goto done;
6373 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6374 if (err)
6375 goto done;
6377 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6378 if (err)
6379 goto done;
6381 if (new_logmsg) {
6382 logmsg = strdup(new_logmsg);
6383 if (logmsg == NULL) {
6384 err = got_error_from_errno("strdup");
6385 goto done;
6387 } else {
6388 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6389 if (err)
6390 goto done;
6393 /* NB: commit_worktree will call free(logmsg) */
6394 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6395 worktree, got_object_commit_get_author(orig_commit),
6396 got_object_commit_get_committer(orig_commit),
6397 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6398 if (err)
6399 goto done;
6401 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6402 if (err)
6403 goto done;
6405 err = got_ref_delete(commit_ref, repo);
6406 if (err)
6407 goto done;
6409 err = update_fileindex_after_commit(worktree, &commitable_paths,
6410 *new_commit_id, fileindex, 0);
6411 sync_err = sync_fileindex(fileindex, fileindex_path);
6412 if (sync_err && err == NULL)
6413 err = sync_err;
6414 done:
6415 free(fileindex_path);
6416 free(head_commit_id);
6417 if (head_ref)
6418 got_ref_close(head_ref);
6419 if (err) {
6420 free(*new_commit_id);
6421 *new_commit_id = NULL;
6423 return err;
6426 const struct got_error *
6427 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6428 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6429 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6430 struct got_commit_object *orig_commit,
6431 struct got_object_id *orig_commit_id, struct got_repository *repo)
6433 const struct got_error *err;
6434 char *commit_ref_name;
6435 struct got_reference *commit_ref = NULL;
6436 struct got_object_id *commit_id = NULL;
6438 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6439 if (err)
6440 return err;
6442 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6443 if (err)
6444 goto done;
6445 err = got_ref_resolve(&commit_id, repo, commit_ref);
6446 if (err)
6447 goto done;
6448 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6449 err = got_error(GOT_ERR_REBASE_COMMITID);
6450 goto done;
6453 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6454 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6455 done:
6456 if (commit_ref)
6457 got_ref_close(commit_ref);
6458 free(commit_ref_name);
6459 free(commit_id);
6460 return err;
6463 const struct got_error *
6464 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6465 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6466 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6467 struct got_commit_object *orig_commit,
6468 struct got_object_id *orig_commit_id, const char *new_logmsg,
6469 struct got_repository *repo)
6471 const struct got_error *err;
6472 char *commit_ref_name;
6473 struct got_reference *commit_ref = NULL;
6475 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6476 if (err)
6477 return err;
6479 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6480 if (err)
6481 goto done;
6483 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6484 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6485 done:
6486 if (commit_ref)
6487 got_ref_close(commit_ref);
6488 free(commit_ref_name);
6489 return err;
6492 const struct got_error *
6493 got_worktree_rebase_postpone(struct got_worktree *worktree,
6494 struct got_fileindex *fileindex)
6496 if (fileindex)
6497 got_fileindex_free(fileindex);
6498 return lock_worktree(worktree, LOCK_SH);
6501 static const struct got_error *
6502 delete_ref(const char *name, struct got_repository *repo)
6504 const struct got_error *err;
6505 struct got_reference *ref;
6507 err = got_ref_open(&ref, repo, name, 0);
6508 if (err) {
6509 if (err->code == GOT_ERR_NOT_REF)
6510 return NULL;
6511 return err;
6514 err = got_ref_delete(ref, repo);
6515 got_ref_close(ref);
6516 return err;
6519 static const struct got_error *
6520 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6522 const struct got_error *err;
6523 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6524 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6526 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6527 if (err)
6528 goto done;
6529 err = delete_ref(tmp_branch_name, repo);
6530 if (err)
6531 goto done;
6533 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6534 if (err)
6535 goto done;
6536 err = delete_ref(new_base_branch_ref_name, repo);
6537 if (err)
6538 goto done;
6540 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6541 if (err)
6542 goto done;
6543 err = delete_ref(branch_ref_name, repo);
6544 if (err)
6545 goto done;
6547 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6548 if (err)
6549 goto done;
6550 err = delete_ref(commit_ref_name, repo);
6551 if (err)
6552 goto done;
6554 done:
6555 free(tmp_branch_name);
6556 free(new_base_branch_ref_name);
6557 free(branch_ref_name);
6558 free(commit_ref_name);
6559 return err;
6562 const struct got_error *
6563 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6564 struct got_object_id *new_commit_id, struct got_repository *repo)
6566 const struct got_error *err;
6567 struct got_reference *ref = NULL;
6568 struct got_object_id *old_commit_id = NULL;
6569 const char *branch_name = NULL;
6570 char *new_id_str = NULL;
6571 char *refname = NULL;
6573 branch_name = got_ref_get_name(branch);
6574 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6575 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6576 branch_name += 11;
6578 err = got_object_id_str(&new_id_str, new_commit_id);
6579 if (err)
6580 return err;
6582 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6583 new_id_str) == -1) {
6584 err = got_error_from_errno("asprintf");
6585 goto done;
6588 err = got_ref_resolve(&old_commit_id, repo, branch);
6589 if (err)
6590 goto done;
6592 err = got_ref_alloc(&ref, refname, old_commit_id);
6593 if (err)
6594 goto done;
6596 err = got_ref_write(ref, repo);
6597 done:
6598 free(new_id_str);
6599 free(refname);
6600 free(old_commit_id);
6601 if (ref)
6602 got_ref_close(ref);
6603 return err;
6606 const struct got_error *
6607 got_worktree_rebase_complete(struct got_worktree *worktree,
6608 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6609 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6610 struct got_repository *repo, int create_backup)
6612 const struct got_error *err, *unlockerr, *sync_err;
6613 struct got_object_id *new_head_commit_id = NULL;
6614 char *fileindex_path = NULL;
6616 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6617 if (err)
6618 return err;
6620 if (create_backup) {
6621 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6622 rebased_branch, new_head_commit_id, repo);
6623 if (err)
6624 goto done;
6627 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6628 if (err)
6629 goto done;
6631 err = got_ref_write(rebased_branch, repo);
6632 if (err)
6633 goto done;
6635 err = got_worktree_set_head_ref(worktree, rebased_branch);
6636 if (err)
6637 goto done;
6639 err = delete_rebase_refs(worktree, repo);
6640 if (err)
6641 goto done;
6643 err = get_fileindex_path(&fileindex_path, worktree);
6644 if (err)
6645 goto done;
6646 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6647 sync_err = sync_fileindex(fileindex, fileindex_path);
6648 if (sync_err && err == NULL)
6649 err = sync_err;
6650 done:
6651 got_fileindex_free(fileindex);
6652 free(fileindex_path);
6653 free(new_head_commit_id);
6654 unlockerr = lock_worktree(worktree, LOCK_SH);
6655 if (unlockerr && err == NULL)
6656 err = unlockerr;
6657 return err;
6660 const struct got_error *
6661 got_worktree_rebase_abort(struct got_worktree *worktree,
6662 struct got_fileindex *fileindex, struct got_repository *repo,
6663 struct got_reference *new_base_branch,
6664 got_worktree_checkout_cb progress_cb, void *progress_arg)
6666 const struct got_error *err, *unlockerr, *sync_err;
6667 struct got_reference *resolved = NULL;
6668 struct got_object_id *commit_id = NULL;
6669 char *fileindex_path = NULL;
6670 struct revert_file_args rfa;
6671 struct got_object_id *tree_id = NULL;
6673 err = lock_worktree(worktree, LOCK_EX);
6674 if (err)
6675 return err;
6677 err = got_ref_open(&resolved, repo,
6678 got_ref_get_symref_target(new_base_branch), 0);
6679 if (err)
6680 goto done;
6682 err = got_worktree_set_head_ref(worktree, resolved);
6683 if (err)
6684 goto done;
6687 * XXX commits to the base branch could have happened while
6688 * we were busy rebasing; should we store the original commit ID
6689 * when rebase begins and read it back here?
6691 err = got_ref_resolve(&commit_id, repo, resolved);
6692 if (err)
6693 goto done;
6695 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6696 if (err)
6697 goto done;
6699 err = got_object_id_by_path(&tree_id, repo,
6700 worktree->base_commit_id, worktree->path_prefix);
6701 if (err)
6702 goto done;
6704 err = delete_rebase_refs(worktree, repo);
6705 if (err)
6706 goto done;
6708 err = get_fileindex_path(&fileindex_path, worktree);
6709 if (err)
6710 goto done;
6712 rfa.worktree = worktree;
6713 rfa.fileindex = fileindex;
6714 rfa.progress_cb = progress_cb;
6715 rfa.progress_arg = progress_arg;
6716 rfa.patch_cb = NULL;
6717 rfa.patch_arg = NULL;
6718 rfa.repo = repo;
6719 err = worktree_status(worktree, "", fileindex, repo,
6720 revert_file, &rfa, NULL, NULL, 0, 0);
6721 if (err)
6722 goto sync;
6724 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6725 repo, progress_cb, progress_arg, NULL, NULL);
6726 sync:
6727 sync_err = sync_fileindex(fileindex, fileindex_path);
6728 if (sync_err && err == NULL)
6729 err = sync_err;
6730 done:
6731 got_ref_close(resolved);
6732 free(tree_id);
6733 free(commit_id);
6734 if (fileindex)
6735 got_fileindex_free(fileindex);
6736 free(fileindex_path);
6738 unlockerr = lock_worktree(worktree, LOCK_SH);
6739 if (unlockerr && err == NULL)
6740 err = unlockerr;
6741 return err;
6744 const struct got_error *
6745 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6746 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6747 struct got_fileindex **fileindex, struct got_worktree *worktree,
6748 struct got_repository *repo)
6750 const struct got_error *err = NULL;
6751 char *tmp_branch_name = NULL;
6752 char *branch_ref_name = NULL;
6753 char *base_commit_ref_name = NULL;
6754 char *fileindex_path = NULL;
6755 struct check_rebase_ok_arg ok_arg;
6756 struct got_reference *wt_branch = NULL;
6757 struct got_reference *base_commit_ref = NULL;
6759 *tmp_branch = NULL;
6760 *branch_ref = NULL;
6761 *base_commit_id = NULL;
6762 *fileindex = NULL;
6764 err = lock_worktree(worktree, LOCK_EX);
6765 if (err)
6766 return err;
6768 err = open_fileindex(fileindex, &fileindex_path, worktree);
6769 if (err)
6770 goto done;
6772 ok_arg.worktree = worktree;
6773 ok_arg.repo = repo;
6774 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6775 &ok_arg);
6776 if (err)
6777 goto done;
6779 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6780 if (err)
6781 goto done;
6783 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6784 if (err)
6785 goto done;
6787 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6788 worktree);
6789 if (err)
6790 goto done;
6792 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6793 0);
6794 if (err)
6795 goto done;
6797 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6798 if (err)
6799 goto done;
6801 err = got_ref_write(*branch_ref, repo);
6802 if (err)
6803 goto done;
6805 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6806 worktree->base_commit_id);
6807 if (err)
6808 goto done;
6809 err = got_ref_write(base_commit_ref, repo);
6810 if (err)
6811 goto done;
6812 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6813 if (*base_commit_id == NULL) {
6814 err = got_error_from_errno("got_object_id_dup");
6815 goto done;
6818 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6819 worktree->base_commit_id);
6820 if (err)
6821 goto done;
6822 err = got_ref_write(*tmp_branch, repo);
6823 if (err)
6824 goto done;
6826 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6827 if (err)
6828 goto done;
6829 done:
6830 free(fileindex_path);
6831 free(tmp_branch_name);
6832 free(branch_ref_name);
6833 free(base_commit_ref_name);
6834 if (wt_branch)
6835 got_ref_close(wt_branch);
6836 if (err) {
6837 if (*branch_ref) {
6838 got_ref_close(*branch_ref);
6839 *branch_ref = NULL;
6841 if (*tmp_branch) {
6842 got_ref_close(*tmp_branch);
6843 *tmp_branch = NULL;
6845 free(*base_commit_id);
6846 if (*fileindex) {
6847 got_fileindex_free(*fileindex);
6848 *fileindex = NULL;
6850 lock_worktree(worktree, LOCK_SH);
6852 return err;
6855 const struct got_error *
6856 got_worktree_histedit_postpone(struct got_worktree *worktree,
6857 struct got_fileindex *fileindex)
6859 if (fileindex)
6860 got_fileindex_free(fileindex);
6861 return lock_worktree(worktree, LOCK_SH);
6864 const struct got_error *
6865 got_worktree_histedit_in_progress(int *in_progress,
6866 struct got_worktree *worktree)
6868 const struct got_error *err;
6869 char *tmp_branch_name = NULL;
6871 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6872 if (err)
6873 return err;
6875 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6876 free(tmp_branch_name);
6877 return NULL;
6880 const struct got_error *
6881 got_worktree_histedit_continue(struct got_object_id **commit_id,
6882 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6883 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6884 struct got_worktree *worktree, struct got_repository *repo)
6886 const struct got_error *err;
6887 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6888 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6889 struct got_reference *commit_ref = NULL;
6890 struct got_reference *base_commit_ref = NULL;
6891 char *fileindex_path = NULL;
6892 int have_staged_files = 0;
6894 *commit_id = NULL;
6895 *tmp_branch = NULL;
6896 *base_commit_id = NULL;
6897 *fileindex = NULL;
6899 err = lock_worktree(worktree, LOCK_EX);
6900 if (err)
6901 return err;
6903 err = open_fileindex(fileindex, &fileindex_path, worktree);
6904 if (err)
6905 goto done;
6907 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6908 &have_staged_files);
6909 if (err && err->code != GOT_ERR_CANCELLED)
6910 goto done;
6911 if (have_staged_files) {
6912 err = got_error(GOT_ERR_STAGED_PATHS);
6913 goto done;
6916 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6917 if (err)
6918 goto done;
6920 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6921 if (err)
6922 goto done;
6924 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6925 if (err)
6926 goto done;
6928 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6929 worktree);
6930 if (err)
6931 goto done;
6933 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6934 if (err)
6935 goto done;
6937 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6938 if (err)
6939 goto done;
6940 err = got_ref_resolve(commit_id, repo, commit_ref);
6941 if (err)
6942 goto done;
6944 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6945 if (err)
6946 goto done;
6947 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6948 if (err)
6949 goto done;
6951 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6952 if (err)
6953 goto done;
6954 done:
6955 free(commit_ref_name);
6956 free(branch_ref_name);
6957 free(fileindex_path);
6958 if (commit_ref)
6959 got_ref_close(commit_ref);
6960 if (base_commit_ref)
6961 got_ref_close(base_commit_ref);
6962 if (err) {
6963 free(*commit_id);
6964 *commit_id = NULL;
6965 free(*base_commit_id);
6966 *base_commit_id = NULL;
6967 if (*tmp_branch) {
6968 got_ref_close(*tmp_branch);
6969 *tmp_branch = NULL;
6971 if (*fileindex) {
6972 got_fileindex_free(*fileindex);
6973 *fileindex = NULL;
6975 lock_worktree(worktree, LOCK_EX);
6977 return err;
6980 static const struct got_error *
6981 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6983 const struct got_error *err;
6984 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6985 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6987 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6988 if (err)
6989 goto done;
6990 err = delete_ref(tmp_branch_name, repo);
6991 if (err)
6992 goto done;
6994 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6995 worktree);
6996 if (err)
6997 goto done;
6998 err = delete_ref(base_commit_ref_name, repo);
6999 if (err)
7000 goto done;
7002 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7003 if (err)
7004 goto done;
7005 err = delete_ref(branch_ref_name, repo);
7006 if (err)
7007 goto done;
7009 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7010 if (err)
7011 goto done;
7012 err = delete_ref(commit_ref_name, repo);
7013 if (err)
7014 goto done;
7015 done:
7016 free(tmp_branch_name);
7017 free(base_commit_ref_name);
7018 free(branch_ref_name);
7019 free(commit_ref_name);
7020 return err;
7023 const struct got_error *
7024 got_worktree_histedit_abort(struct got_worktree *worktree,
7025 struct got_fileindex *fileindex, struct got_repository *repo,
7026 struct got_reference *branch, struct got_object_id *base_commit_id,
7027 got_worktree_checkout_cb progress_cb, void *progress_arg)
7029 const struct got_error *err, *unlockerr, *sync_err;
7030 struct got_reference *resolved = NULL;
7031 char *fileindex_path = NULL;
7032 struct got_object_id *tree_id = NULL;
7033 struct revert_file_args rfa;
7035 err = lock_worktree(worktree, LOCK_EX);
7036 if (err)
7037 return err;
7039 err = got_ref_open(&resolved, repo,
7040 got_ref_get_symref_target(branch), 0);
7041 if (err)
7042 goto done;
7044 err = got_worktree_set_head_ref(worktree, resolved);
7045 if (err)
7046 goto done;
7048 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7049 if (err)
7050 goto done;
7052 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7053 worktree->path_prefix);
7054 if (err)
7055 goto done;
7057 err = delete_histedit_refs(worktree, repo);
7058 if (err)
7059 goto done;
7061 err = get_fileindex_path(&fileindex_path, worktree);
7062 if (err)
7063 goto done;
7065 rfa.worktree = worktree;
7066 rfa.fileindex = fileindex;
7067 rfa.progress_cb = progress_cb;
7068 rfa.progress_arg = progress_arg;
7069 rfa.patch_cb = NULL;
7070 rfa.patch_arg = NULL;
7071 rfa.repo = repo;
7072 err = worktree_status(worktree, "", fileindex, repo,
7073 revert_file, &rfa, NULL, NULL, 0, 0);
7074 if (err)
7075 goto sync;
7077 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7078 repo, progress_cb, progress_arg, NULL, NULL);
7079 sync:
7080 sync_err = sync_fileindex(fileindex, fileindex_path);
7081 if (sync_err && err == NULL)
7082 err = sync_err;
7083 done:
7084 got_ref_close(resolved);
7085 free(tree_id);
7086 free(fileindex_path);
7088 unlockerr = lock_worktree(worktree, LOCK_SH);
7089 if (unlockerr && err == NULL)
7090 err = unlockerr;
7091 return err;
7094 const struct got_error *
7095 got_worktree_histedit_complete(struct got_worktree *worktree,
7096 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7097 struct got_reference *edited_branch, struct got_repository *repo)
7099 const struct got_error *err, *unlockerr, *sync_err;
7100 struct got_object_id *new_head_commit_id = NULL;
7101 struct got_reference *resolved = NULL;
7102 char *fileindex_path = NULL;
7104 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7105 if (err)
7106 return err;
7108 err = got_ref_open(&resolved, repo,
7109 got_ref_get_symref_target(edited_branch), 0);
7110 if (err)
7111 goto done;
7113 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7114 resolved, new_head_commit_id, repo);
7115 if (err)
7116 goto done;
7118 err = got_ref_change_ref(resolved, new_head_commit_id);
7119 if (err)
7120 goto done;
7122 err = got_ref_write(resolved, repo);
7123 if (err)
7124 goto done;
7126 err = got_worktree_set_head_ref(worktree, resolved);
7127 if (err)
7128 goto done;
7130 err = delete_histedit_refs(worktree, repo);
7131 if (err)
7132 goto done;
7134 err = get_fileindex_path(&fileindex_path, worktree);
7135 if (err)
7136 goto done;
7137 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7138 sync_err = sync_fileindex(fileindex, fileindex_path);
7139 if (sync_err && err == NULL)
7140 err = sync_err;
7141 done:
7142 got_fileindex_free(fileindex);
7143 free(fileindex_path);
7144 free(new_head_commit_id);
7145 unlockerr = lock_worktree(worktree, LOCK_SH);
7146 if (unlockerr && err == NULL)
7147 err = unlockerr;
7148 return err;
7151 const struct got_error *
7152 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7153 struct got_object_id *commit_id, struct got_repository *repo)
7155 const struct got_error *err;
7156 char *commit_ref_name;
7158 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7159 if (err)
7160 return err;
7162 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7163 if (err)
7164 goto done;
7166 err = delete_ref(commit_ref_name, repo);
7167 done:
7168 free(commit_ref_name);
7169 return err;
7172 const struct got_error *
7173 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7174 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7175 struct got_worktree *worktree, const char *refname,
7176 struct got_repository *repo)
7178 const struct got_error *err = NULL;
7179 char *fileindex_path = NULL;
7180 struct check_rebase_ok_arg ok_arg;
7182 *fileindex = NULL;
7183 *branch_ref = NULL;
7184 *base_branch_ref = NULL;
7186 err = lock_worktree(worktree, LOCK_EX);
7187 if (err)
7188 return err;
7190 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7191 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7192 "cannot integrate a branch into itself; "
7193 "update -b or different branch name required");
7194 goto done;
7197 err = open_fileindex(fileindex, &fileindex_path, worktree);
7198 if (err)
7199 goto done;
7201 /* Preconditions are the same as for rebase. */
7202 ok_arg.worktree = worktree;
7203 ok_arg.repo = repo;
7204 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7205 &ok_arg);
7206 if (err)
7207 goto done;
7209 err = got_ref_open(branch_ref, repo, refname, 1);
7210 if (err)
7211 goto done;
7213 err = got_ref_open(base_branch_ref, repo,
7214 got_worktree_get_head_ref_name(worktree), 1);
7215 done:
7216 if (err) {
7217 if (*branch_ref) {
7218 got_ref_close(*branch_ref);
7219 *branch_ref = NULL;
7221 if (*base_branch_ref) {
7222 got_ref_close(*base_branch_ref);
7223 *base_branch_ref = NULL;
7225 if (*fileindex) {
7226 got_fileindex_free(*fileindex);
7227 *fileindex = NULL;
7229 lock_worktree(worktree, LOCK_SH);
7231 return err;
7234 const struct got_error *
7235 got_worktree_integrate_continue(struct got_worktree *worktree,
7236 struct got_fileindex *fileindex, struct got_repository *repo,
7237 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7238 got_worktree_checkout_cb progress_cb, void *progress_arg,
7239 got_cancel_cb cancel_cb, void *cancel_arg)
7241 const struct got_error *err = NULL, *sync_err, *unlockerr;
7242 char *fileindex_path = NULL;
7243 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7245 err = get_fileindex_path(&fileindex_path, worktree);
7246 if (err)
7247 goto done;
7249 err = got_ref_resolve(&commit_id, repo, branch_ref);
7250 if (err)
7251 goto done;
7253 err = got_object_id_by_path(&tree_id, repo, commit_id,
7254 worktree->path_prefix);
7255 if (err)
7256 goto done;
7258 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7259 if (err)
7260 goto done;
7262 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7263 progress_cb, progress_arg, cancel_cb, cancel_arg);
7264 if (err)
7265 goto sync;
7267 err = got_ref_change_ref(base_branch_ref, commit_id);
7268 if (err)
7269 goto sync;
7271 err = got_ref_write(base_branch_ref, repo);
7272 if (err)
7273 goto sync;
7275 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7276 sync:
7277 sync_err = sync_fileindex(fileindex, fileindex_path);
7278 if (sync_err && err == NULL)
7279 err = sync_err;
7281 done:
7282 unlockerr = got_ref_unlock(branch_ref);
7283 if (unlockerr && err == NULL)
7284 err = unlockerr;
7285 got_ref_close(branch_ref);
7287 unlockerr = got_ref_unlock(base_branch_ref);
7288 if (unlockerr && err == NULL)
7289 err = unlockerr;
7290 got_ref_close(base_branch_ref);
7292 got_fileindex_free(fileindex);
7293 free(fileindex_path);
7294 free(tree_id);
7296 unlockerr = lock_worktree(worktree, LOCK_SH);
7297 if (unlockerr && err == NULL)
7298 err = unlockerr;
7299 return err;
7302 const struct got_error *
7303 got_worktree_integrate_abort(struct got_worktree *worktree,
7304 struct got_fileindex *fileindex, struct got_repository *repo,
7305 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7307 const struct got_error *err = NULL, *unlockerr = NULL;
7309 got_fileindex_free(fileindex);
7311 err = lock_worktree(worktree, LOCK_SH);
7313 unlockerr = got_ref_unlock(branch_ref);
7314 if (unlockerr && err == NULL)
7315 err = unlockerr;
7316 got_ref_close(branch_ref);
7318 unlockerr = got_ref_unlock(base_branch_ref);
7319 if (unlockerr && err == NULL)
7320 err = unlockerr;
7321 got_ref_close(base_branch_ref);
7323 return err;
7326 struct check_stage_ok_arg {
7327 struct got_object_id *head_commit_id;
7328 struct got_worktree *worktree;
7329 struct got_fileindex *fileindex;
7330 struct got_repository *repo;
7331 int have_changes;
7334 const struct got_error *
7335 check_stage_ok(void *arg, unsigned char status,
7336 unsigned char staged_status, const char *relpath,
7337 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7338 struct got_object_id *commit_id, int dirfd, const char *de_name)
7340 struct check_stage_ok_arg *a = arg;
7341 const struct got_error *err = NULL;
7342 struct got_fileindex_entry *ie;
7343 struct got_object_id base_commit_id;
7344 struct got_object_id *base_commit_idp = NULL;
7345 char *in_repo_path = NULL, *p;
7347 if (status == GOT_STATUS_UNVERSIONED ||
7348 status == GOT_STATUS_NO_CHANGE)
7349 return NULL;
7350 if (status == GOT_STATUS_NONEXISTENT)
7351 return got_error_set_errno(ENOENT, relpath);
7353 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7354 if (ie == NULL)
7355 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7357 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7358 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7359 relpath) == -1)
7360 return got_error_from_errno("asprintf");
7362 if (got_fileindex_entry_has_commit(ie)) {
7363 memcpy(base_commit_id.sha1, ie->commit_sha1,
7364 SHA1_DIGEST_LENGTH);
7365 base_commit_idp = &base_commit_id;
7368 if (status == GOT_STATUS_CONFLICT) {
7369 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7370 goto done;
7371 } else if (status != GOT_STATUS_ADD &&
7372 status != GOT_STATUS_MODIFY &&
7373 status != GOT_STATUS_DELETE) {
7374 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7375 goto done;
7378 a->have_changes = 1;
7380 p = in_repo_path;
7381 while (p[0] == '/')
7382 p++;
7383 err = check_out_of_date(p, status, staged_status,
7384 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7385 GOT_ERR_STAGE_OUT_OF_DATE);
7386 done:
7387 free(in_repo_path);
7388 return err;
7391 struct stage_path_arg {
7392 struct got_worktree *worktree;
7393 struct got_fileindex *fileindex;
7394 struct got_repository *repo;
7395 got_worktree_status_cb status_cb;
7396 void *status_arg;
7397 got_worktree_patch_cb patch_cb;
7398 void *patch_arg;
7399 int staged_something;
7400 int allow_bad_symlinks;
7403 static const struct got_error *
7404 stage_path(void *arg, unsigned char status,
7405 unsigned char staged_status, const char *relpath,
7406 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7407 struct got_object_id *commit_id, int dirfd, const char *de_name)
7409 struct stage_path_arg *a = arg;
7410 const struct got_error *err = NULL;
7411 struct got_fileindex_entry *ie;
7412 char *ondisk_path = NULL, *path_content = NULL;
7413 uint32_t stage;
7414 struct got_object_id *new_staged_blob_id = NULL;
7415 struct stat sb;
7417 if (status == GOT_STATUS_UNVERSIONED)
7418 return NULL;
7420 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7421 if (ie == NULL)
7422 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7424 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7425 relpath)== -1)
7426 return got_error_from_errno("asprintf");
7428 switch (status) {
7429 case GOT_STATUS_ADD:
7430 case GOT_STATUS_MODIFY:
7431 /* XXX could sb.st_mode be passed in by our caller? */
7432 if (lstat(ondisk_path, &sb) == -1) {
7433 err = got_error_from_errno2("lstat", ondisk_path);
7434 break;
7436 if (a->patch_cb) {
7437 if (status == GOT_STATUS_ADD) {
7438 int choice = GOT_PATCH_CHOICE_NONE;
7439 err = (*a->patch_cb)(&choice, a->patch_arg,
7440 status, ie->path, NULL, 1, 1);
7441 if (err)
7442 break;
7443 if (choice != GOT_PATCH_CHOICE_YES)
7444 break;
7445 } else {
7446 err = create_patched_content(&path_content, 0,
7447 staged_blob_id ? staged_blob_id : blob_id,
7448 ondisk_path, dirfd, de_name, ie->path,
7449 a->repo, a->patch_cb, a->patch_arg);
7450 if (err || path_content == NULL)
7451 break;
7454 err = got_object_blob_create(&new_staged_blob_id,
7455 path_content ? path_content : ondisk_path, a->repo);
7456 if (err)
7457 break;
7458 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7459 SHA1_DIGEST_LENGTH);
7460 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7461 stage = GOT_FILEIDX_STAGE_ADD;
7462 else
7463 stage = GOT_FILEIDX_STAGE_MODIFY;
7464 got_fileindex_entry_stage_set(ie, stage);
7465 if (S_ISLNK(sb.st_mode)) {
7466 int is_bad_symlink = 0;
7467 if (!a->allow_bad_symlinks) {
7468 char target_path[PATH_MAX];
7469 ssize_t target_len;
7470 target_len = readlink(ondisk_path, target_path,
7471 sizeof(target_path));
7472 if (target_len == -1) {
7473 err = got_error_from_errno2("readlink",
7474 ondisk_path);
7475 break;
7477 err = is_bad_symlink_target(&is_bad_symlink,
7478 target_path, target_len, ondisk_path,
7479 a->worktree->root_path);
7480 if (err)
7481 break;
7482 if (is_bad_symlink) {
7483 err = got_error_path(ondisk_path,
7484 GOT_ERR_BAD_SYMLINK);
7485 break;
7488 if (is_bad_symlink)
7489 got_fileindex_entry_staged_filetype_set(ie,
7490 GOT_FILEIDX_MODE_BAD_SYMLINK);
7491 else
7492 got_fileindex_entry_staged_filetype_set(ie,
7493 GOT_FILEIDX_MODE_SYMLINK);
7494 } else {
7495 got_fileindex_entry_staged_filetype_set(ie,
7496 GOT_FILEIDX_MODE_REGULAR_FILE);
7498 a->staged_something = 1;
7499 if (a->status_cb == NULL)
7500 break;
7501 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7502 get_staged_status(ie), relpath, blob_id,
7503 new_staged_blob_id, NULL, dirfd, de_name);
7504 break;
7505 case GOT_STATUS_DELETE:
7506 if (staged_status == GOT_STATUS_DELETE)
7507 break;
7508 if (a->patch_cb) {
7509 int choice = GOT_PATCH_CHOICE_NONE;
7510 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7511 ie->path, NULL, 1, 1);
7512 if (err)
7513 break;
7514 if (choice == GOT_PATCH_CHOICE_NO)
7515 break;
7516 if (choice != GOT_PATCH_CHOICE_YES) {
7517 err = got_error(GOT_ERR_PATCH_CHOICE);
7518 break;
7521 stage = GOT_FILEIDX_STAGE_DELETE;
7522 got_fileindex_entry_stage_set(ie, stage);
7523 a->staged_something = 1;
7524 if (a->status_cb == NULL)
7525 break;
7526 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7527 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7528 de_name);
7529 break;
7530 case GOT_STATUS_NO_CHANGE:
7531 break;
7532 case GOT_STATUS_CONFLICT:
7533 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7534 break;
7535 case GOT_STATUS_NONEXISTENT:
7536 err = got_error_set_errno(ENOENT, relpath);
7537 break;
7538 default:
7539 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7540 break;
7543 if (path_content && unlink(path_content) == -1 && err == NULL)
7544 err = got_error_from_errno2("unlink", path_content);
7545 free(path_content);
7546 free(ondisk_path);
7547 free(new_staged_blob_id);
7548 return err;
7551 const struct got_error *
7552 got_worktree_stage(struct got_worktree *worktree,
7553 struct got_pathlist_head *paths,
7554 got_worktree_status_cb status_cb, void *status_arg,
7555 got_worktree_patch_cb patch_cb, void *patch_arg,
7556 int allow_bad_symlinks, struct got_repository *repo)
7558 const struct got_error *err = NULL, *sync_err, *unlockerr;
7559 struct got_pathlist_entry *pe;
7560 struct got_fileindex *fileindex = NULL;
7561 char *fileindex_path = NULL;
7562 struct got_reference *head_ref = NULL;
7563 struct got_object_id *head_commit_id = NULL;
7564 struct check_stage_ok_arg oka;
7565 struct stage_path_arg spa;
7567 err = lock_worktree(worktree, LOCK_EX);
7568 if (err)
7569 return err;
7571 err = got_ref_open(&head_ref, repo,
7572 got_worktree_get_head_ref_name(worktree), 0);
7573 if (err)
7574 goto done;
7575 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7576 if (err)
7577 goto done;
7578 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7579 if (err)
7580 goto done;
7582 /* Check pre-conditions before staging anything. */
7583 oka.head_commit_id = head_commit_id;
7584 oka.worktree = worktree;
7585 oka.fileindex = fileindex;
7586 oka.repo = repo;
7587 oka.have_changes = 0;
7588 TAILQ_FOREACH(pe, paths, entry) {
7589 err = worktree_status(worktree, pe->path, fileindex, repo,
7590 check_stage_ok, &oka, NULL, NULL, 0, 0);
7591 if (err)
7592 goto done;
7594 if (!oka.have_changes) {
7595 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7596 goto done;
7599 spa.worktree = worktree;
7600 spa.fileindex = fileindex;
7601 spa.repo = repo;
7602 spa.patch_cb = patch_cb;
7603 spa.patch_arg = patch_arg;
7604 spa.status_cb = status_cb;
7605 spa.status_arg = status_arg;
7606 spa.staged_something = 0;
7607 spa.allow_bad_symlinks = allow_bad_symlinks;
7608 TAILQ_FOREACH(pe, paths, entry) {
7609 err = worktree_status(worktree, pe->path, fileindex, repo,
7610 stage_path, &spa, NULL, NULL, 0, 0);
7611 if (err)
7612 goto done;
7614 if (!spa.staged_something) {
7615 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7616 goto done;
7619 sync_err = sync_fileindex(fileindex, fileindex_path);
7620 if (sync_err && err == NULL)
7621 err = sync_err;
7622 done:
7623 if (head_ref)
7624 got_ref_close(head_ref);
7625 free(head_commit_id);
7626 free(fileindex_path);
7627 if (fileindex)
7628 got_fileindex_free(fileindex);
7629 unlockerr = lock_worktree(worktree, LOCK_SH);
7630 if (unlockerr && err == NULL)
7631 err = unlockerr;
7632 return err;
7635 struct unstage_path_arg {
7636 struct got_worktree *worktree;
7637 struct got_fileindex *fileindex;
7638 struct got_repository *repo;
7639 got_worktree_checkout_cb progress_cb;
7640 void *progress_arg;
7641 got_worktree_patch_cb patch_cb;
7642 void *patch_arg;
7645 static const struct got_error *
7646 create_unstaged_content(char **path_unstaged_content,
7647 char **path_new_staged_content, struct got_object_id *blob_id,
7648 struct got_object_id *staged_blob_id, const char *relpath,
7649 struct got_repository *repo,
7650 got_worktree_patch_cb patch_cb, void *patch_arg)
7652 const struct got_error *err, *free_err;
7653 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7654 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7655 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7656 struct got_diffreg_result *diffreg_result = NULL;
7657 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7658 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7660 *path_unstaged_content = NULL;
7661 *path_new_staged_content = NULL;
7663 err = got_object_id_str(&label1, blob_id);
7664 if (err)
7665 return err;
7666 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7667 if (err)
7668 goto done;
7670 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7671 if (err)
7672 goto done;
7674 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7675 if (err)
7676 goto done;
7678 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7679 if (err)
7680 goto done;
7682 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7683 if (err)
7684 goto done;
7686 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7687 if (err)
7688 goto done;
7690 err = got_diff_files(&diffreg_result, f1, label1, f2,
7691 path2, 3, 0, 1, NULL);
7692 if (err)
7693 goto done;
7695 err = got_opentemp_named(path_unstaged_content, &outfile,
7696 "got-unstaged-content");
7697 if (err)
7698 goto done;
7699 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7700 "got-new-staged-content");
7701 if (err)
7702 goto done;
7704 if (fseek(f1, 0L, SEEK_SET) == -1) {
7705 err = got_ferror(f1, GOT_ERR_IO);
7706 goto done;
7708 if (fseek(f2, 0L, SEEK_SET) == -1) {
7709 err = got_ferror(f2, GOT_ERR_IO);
7710 goto done;
7712 /* Count the number of actual changes in the diff result. */
7713 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7714 struct diff_chunk_context cc = {};
7715 diff_chunk_context_load_change(&cc, &nchunks_used,
7716 diffreg_result->result, n, 0);
7717 nchanges++;
7719 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7720 int choice;
7721 err = apply_or_reject_change(&choice, &nchunks_used,
7722 diffreg_result->result, n, relpath, f1, f2,
7723 &line_cur1, &line_cur2,
7724 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7725 if (err)
7726 goto done;
7727 if (choice == GOT_PATCH_CHOICE_YES)
7728 have_content = 1;
7729 else
7730 have_rejected_content = 1;
7731 if (choice == GOT_PATCH_CHOICE_QUIT)
7732 break;
7734 if (have_content || have_rejected_content)
7735 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7736 outfile, rejectfile);
7737 done:
7738 free(label1);
7739 if (blob)
7740 got_object_blob_close(blob);
7741 if (staged_blob)
7742 got_object_blob_close(staged_blob);
7743 free_err = got_diffreg_result_free(diffreg_result);
7744 if (free_err && err == NULL)
7745 err = free_err;
7746 if (f1 && fclose(f1) == EOF && err == NULL)
7747 err = got_error_from_errno2("fclose", path1);
7748 if (f2 && fclose(f2) == EOF && err == NULL)
7749 err = got_error_from_errno2("fclose", path2);
7750 if (outfile && fclose(outfile) == EOF && err == NULL)
7751 err = got_error_from_errno2("fclose", *path_unstaged_content);
7752 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7753 err = got_error_from_errno2("fclose", *path_new_staged_content);
7754 if (path1 && unlink(path1) == -1 && err == NULL)
7755 err = got_error_from_errno2("unlink", path1);
7756 if (path2 && unlink(path2) == -1 && err == NULL)
7757 err = got_error_from_errno2("unlink", path2);
7758 if (err || !have_content) {
7759 if (*path_unstaged_content &&
7760 unlink(*path_unstaged_content) == -1 && err == NULL)
7761 err = got_error_from_errno2("unlink",
7762 *path_unstaged_content);
7763 free(*path_unstaged_content);
7764 *path_unstaged_content = NULL;
7766 if (err || !have_content || !have_rejected_content) {
7767 if (*path_new_staged_content &&
7768 unlink(*path_new_staged_content) == -1 && err == NULL)
7769 err = got_error_from_errno2("unlink",
7770 *path_new_staged_content);
7771 free(*path_new_staged_content);
7772 *path_new_staged_content = NULL;
7774 free(path1);
7775 free(path2);
7776 return err;
7779 static const struct got_error *
7780 unstage_hunks(struct got_object_id *staged_blob_id,
7781 struct got_blob_object *blob_base,
7782 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7783 const char *ondisk_path, const char *label_orig,
7784 struct got_worktree *worktree, struct got_repository *repo,
7785 got_worktree_patch_cb patch_cb, void *patch_arg,
7786 got_worktree_checkout_cb progress_cb, void *progress_arg)
7788 const struct got_error *err = NULL;
7789 char *path_unstaged_content = NULL;
7790 char *path_new_staged_content = NULL;
7791 char *parent = NULL, *base_path = NULL;
7792 char *blob_base_path = NULL;
7793 struct got_object_id *new_staged_blob_id = NULL;
7794 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
7795 struct stat sb;
7797 err = create_unstaged_content(&path_unstaged_content,
7798 &path_new_staged_content, blob_id, staged_blob_id,
7799 ie->path, repo, patch_cb, patch_arg);
7800 if (err)
7801 return err;
7803 if (path_unstaged_content == NULL)
7804 return NULL;
7806 if (path_new_staged_content) {
7807 err = got_object_blob_create(&new_staged_blob_id,
7808 path_new_staged_content, repo);
7809 if (err)
7810 goto done;
7813 f = fopen(path_unstaged_content, "r");
7814 if (f == NULL) {
7815 err = got_error_from_errno2("fopen",
7816 path_unstaged_content);
7817 goto done;
7819 if (fstat(fileno(f), &sb) == -1) {
7820 err = got_error_from_errno2("fstat", path_unstaged_content);
7821 goto done;
7823 if (got_fileindex_entry_staged_filetype_get(ie) ==
7824 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7825 char link_target[PATH_MAX];
7826 size_t r;
7827 r = fread(link_target, 1, sizeof(link_target), f);
7828 if (r == 0 && ferror(f)) {
7829 err = got_error_from_errno("fread");
7830 goto done;
7832 if (r >= sizeof(link_target)) { /* should not happen */
7833 err = got_error(GOT_ERR_NO_SPACE);
7834 goto done;
7836 link_target[r] = '\0';
7837 err = merge_symlink(worktree, blob_base,
7838 ondisk_path, ie->path, label_orig, link_target,
7839 worktree->base_commit_id, repo, progress_cb,
7840 progress_arg);
7841 } else {
7842 int local_changes_subsumed;
7844 err = got_path_dirname(&parent, ondisk_path);
7845 if (err)
7846 return err;
7848 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
7849 parent) == -1) {
7850 err = got_error_from_errno("asprintf");
7851 base_path = NULL;
7852 goto done;
7855 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
7856 if (err)
7857 goto done;
7858 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
7859 blob_base);
7860 if (err)
7861 goto done;
7864 * In order the run a 3-way merge with a symlink we copy the symlink's
7865 * target path into a temporary file and use that file with diff3.
7867 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7868 err = dump_symlink_target_path_to_file(&f_deriv2,
7869 ondisk_path);
7870 if (err)
7871 goto done;
7872 } else {
7873 int fd;
7874 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
7875 if (fd == -1) {
7876 err = got_error_from_errno2("open", ondisk_path);
7877 goto done;
7879 f_deriv2 = fdopen(fd, "r");
7880 if (f_deriv2 == NULL) {
7881 err = got_error_from_errno2("fdopen", ondisk_path);
7882 close(fd);
7883 goto done;
7887 err = merge_file(&local_changes_subsumed, worktree,
7888 f_base, f, f_deriv2, ondisk_path, ie->path,
7889 got_fileindex_perms_to_st(ie),
7890 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
7891 repo, progress_cb, progress_arg);
7893 if (err)
7894 goto done;
7896 if (new_staged_blob_id) {
7897 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7898 SHA1_DIGEST_LENGTH);
7899 } else {
7900 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7901 got_fileindex_entry_staged_filetype_set(ie, 0);
7903 done:
7904 free(new_staged_blob_id);
7905 if (path_unstaged_content &&
7906 unlink(path_unstaged_content) == -1 && err == NULL)
7907 err = got_error_from_errno2("unlink", path_unstaged_content);
7908 if (path_new_staged_content &&
7909 unlink(path_new_staged_content) == -1 && err == NULL)
7910 err = got_error_from_errno2("unlink", path_new_staged_content);
7911 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
7912 err = got_error_from_errno2("unlink", blob_base_path);
7913 if (f_base && fclose(f_base) == EOF && err == NULL)
7914 err = got_error_from_errno2("fclose", path_unstaged_content);
7915 if (f && fclose(f) == EOF && err == NULL)
7916 err = got_error_from_errno2("fclose", path_unstaged_content);
7917 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
7918 err = got_error_from_errno2("fclose", ondisk_path);
7919 free(path_unstaged_content);
7920 free(path_new_staged_content);
7921 free(blob_base_path);
7922 free(parent);
7923 free(base_path);
7924 return err;
7927 static const struct got_error *
7928 unstage_path(void *arg, unsigned char status,
7929 unsigned char staged_status, const char *relpath,
7930 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7931 struct got_object_id *commit_id, int dirfd, const char *de_name)
7933 const struct got_error *err = NULL;
7934 struct unstage_path_arg *a = arg;
7935 struct got_fileindex_entry *ie;
7936 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7937 char *ondisk_path = NULL;
7938 char *id_str = NULL, *label_orig = NULL;
7939 int local_changes_subsumed;
7940 struct stat sb;
7942 if (staged_status != GOT_STATUS_ADD &&
7943 staged_status != GOT_STATUS_MODIFY &&
7944 staged_status != GOT_STATUS_DELETE)
7945 return NULL;
7947 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7948 if (ie == NULL)
7949 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7951 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7952 == -1)
7953 return got_error_from_errno("asprintf");
7955 err = got_object_id_str(&id_str,
7956 commit_id ? commit_id : a->worktree->base_commit_id);
7957 if (err)
7958 goto done;
7959 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7960 id_str) == -1) {
7961 err = got_error_from_errno("asprintf");
7962 goto done;
7965 switch (staged_status) {
7966 case GOT_STATUS_MODIFY:
7967 err = got_object_open_as_blob(&blob_base, a->repo,
7968 blob_id, 8192);
7969 if (err)
7970 break;
7971 /* fall through */
7972 case GOT_STATUS_ADD:
7973 if (a->patch_cb) {
7974 if (staged_status == GOT_STATUS_ADD) {
7975 int choice = GOT_PATCH_CHOICE_NONE;
7976 err = (*a->patch_cb)(&choice, a->patch_arg,
7977 staged_status, ie->path, NULL, 1, 1);
7978 if (err)
7979 break;
7980 if (choice != GOT_PATCH_CHOICE_YES)
7981 break;
7982 } else {
7983 err = unstage_hunks(staged_blob_id,
7984 blob_base, blob_id, ie, ondisk_path,
7985 label_orig, a->worktree, a->repo,
7986 a->patch_cb, a->patch_arg,
7987 a->progress_cb, a->progress_arg);
7988 break; /* Done with this file. */
7991 err = got_object_open_as_blob(&blob_staged, a->repo,
7992 staged_blob_id, 8192);
7993 if (err)
7994 break;
7995 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7996 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7997 case GOT_FILEIDX_MODE_REGULAR_FILE:
7998 err = merge_blob(&local_changes_subsumed, a->worktree,
7999 blob_base, ondisk_path, relpath,
8000 got_fileindex_perms_to_st(ie), label_orig,
8001 blob_staged, commit_id ? commit_id :
8002 a->worktree->base_commit_id, a->repo,
8003 a->progress_cb, a->progress_arg);
8004 break;
8005 case GOT_FILEIDX_MODE_SYMLINK:
8006 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8007 char *staged_target;
8008 err = got_object_blob_read_to_str(
8009 &staged_target, blob_staged);
8010 if (err)
8011 goto done;
8012 err = merge_symlink(a->worktree, blob_base,
8013 ondisk_path, relpath, label_orig,
8014 staged_target, commit_id ? commit_id :
8015 a->worktree->base_commit_id,
8016 a->repo, a->progress_cb, a->progress_arg);
8017 free(staged_target);
8018 } else {
8019 err = merge_blob(&local_changes_subsumed,
8020 a->worktree, blob_base, ondisk_path,
8021 relpath, got_fileindex_perms_to_st(ie),
8022 label_orig, blob_staged,
8023 commit_id ? commit_id :
8024 a->worktree->base_commit_id, a->repo,
8025 a->progress_cb, a->progress_arg);
8027 break;
8028 default:
8029 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8030 break;
8032 if (err == NULL) {
8033 got_fileindex_entry_stage_set(ie,
8034 GOT_FILEIDX_STAGE_NONE);
8035 got_fileindex_entry_staged_filetype_set(ie, 0);
8037 break;
8038 case GOT_STATUS_DELETE:
8039 if (a->patch_cb) {
8040 int choice = GOT_PATCH_CHOICE_NONE;
8041 err = (*a->patch_cb)(&choice, a->patch_arg,
8042 staged_status, ie->path, NULL, 1, 1);
8043 if (err)
8044 break;
8045 if (choice == GOT_PATCH_CHOICE_NO)
8046 break;
8047 if (choice != GOT_PATCH_CHOICE_YES) {
8048 err = got_error(GOT_ERR_PATCH_CHOICE);
8049 break;
8052 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8053 got_fileindex_entry_staged_filetype_set(ie, 0);
8054 err = get_file_status(&status, &sb, ie, ondisk_path,
8055 dirfd, de_name, a->repo);
8056 if (err)
8057 break;
8058 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8059 break;
8061 done:
8062 free(ondisk_path);
8063 if (blob_base)
8064 got_object_blob_close(blob_base);
8065 if (blob_staged)
8066 got_object_blob_close(blob_staged);
8067 free(id_str);
8068 free(label_orig);
8069 return err;
8072 const struct got_error *
8073 got_worktree_unstage(struct got_worktree *worktree,
8074 struct got_pathlist_head *paths,
8075 got_worktree_checkout_cb progress_cb, void *progress_arg,
8076 got_worktree_patch_cb patch_cb, void *patch_arg,
8077 struct got_repository *repo)
8079 const struct got_error *err = NULL, *sync_err, *unlockerr;
8080 struct got_pathlist_entry *pe;
8081 struct got_fileindex *fileindex = NULL;
8082 char *fileindex_path = NULL;
8083 struct unstage_path_arg upa;
8085 err = lock_worktree(worktree, LOCK_EX);
8086 if (err)
8087 return err;
8089 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8090 if (err)
8091 goto done;
8093 upa.worktree = worktree;
8094 upa.fileindex = fileindex;
8095 upa.repo = repo;
8096 upa.progress_cb = progress_cb;
8097 upa.progress_arg = progress_arg;
8098 upa.patch_cb = patch_cb;
8099 upa.patch_arg = patch_arg;
8100 TAILQ_FOREACH(pe, paths, entry) {
8101 err = worktree_status(worktree, pe->path, fileindex, repo,
8102 unstage_path, &upa, NULL, NULL, 0, 0);
8103 if (err)
8104 goto done;
8107 sync_err = sync_fileindex(fileindex, fileindex_path);
8108 if (sync_err && err == NULL)
8109 err = sync_err;
8110 done:
8111 free(fileindex_path);
8112 if (fileindex)
8113 got_fileindex_free(fileindex);
8114 unlockerr = lock_worktree(worktree, LOCK_SH);
8115 if (unlockerr && err == NULL)
8116 err = unlockerr;
8117 return err;
8120 struct report_file_info_arg {
8121 struct got_worktree *worktree;
8122 got_worktree_path_info_cb info_cb;
8123 void *info_arg;
8124 struct got_pathlist_head *paths;
8125 got_cancel_cb cancel_cb;
8126 void *cancel_arg;
8129 static const struct got_error *
8130 report_file_info(void *arg, struct got_fileindex_entry *ie)
8132 struct report_file_info_arg *a = arg;
8133 struct got_pathlist_entry *pe;
8134 struct got_object_id blob_id, staged_blob_id, commit_id;
8135 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8136 struct got_object_id *commit_idp = NULL;
8137 int stage;
8139 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8140 return got_error(GOT_ERR_CANCELLED);
8142 TAILQ_FOREACH(pe, a->paths, entry) {
8143 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8144 got_path_is_child(ie->path, pe->path, pe->path_len))
8145 break;
8147 if (pe == NULL) /* not found */
8148 return NULL;
8150 if (got_fileindex_entry_has_blob(ie)) {
8151 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8152 blob_idp = &blob_id;
8154 stage = got_fileindex_entry_stage_get(ie);
8155 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8156 stage == GOT_FILEIDX_STAGE_ADD) {
8157 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8158 SHA1_DIGEST_LENGTH);
8159 staged_blob_idp = &staged_blob_id;
8162 if (got_fileindex_entry_has_commit(ie)) {
8163 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8164 commit_idp = &commit_id;
8167 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8168 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8171 const struct got_error *
8172 got_worktree_path_info(struct got_worktree *worktree,
8173 struct got_pathlist_head *paths,
8174 got_worktree_path_info_cb info_cb, void *info_arg,
8175 got_cancel_cb cancel_cb, void *cancel_arg)
8178 const struct got_error *err = NULL, *unlockerr;
8179 struct got_fileindex *fileindex = NULL;
8180 char *fileindex_path = NULL;
8181 struct report_file_info_arg arg;
8183 err = lock_worktree(worktree, LOCK_SH);
8184 if (err)
8185 return err;
8187 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8188 if (err)
8189 goto done;
8191 arg.worktree = worktree;
8192 arg.info_cb = info_cb;
8193 arg.info_arg = info_arg;
8194 arg.paths = paths;
8195 arg.cancel_cb = cancel_cb;
8196 arg.cancel_arg = cancel_arg;
8197 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8198 &arg);
8199 done:
8200 free(fileindex_path);
8201 if (fileindex)
8202 got_fileindex_free(fileindex);
8203 unlockerr = lock_worktree(worktree, LOCK_UN);
8204 if (unlockerr && err == NULL)
8205 err = unlockerr;
8206 return err;