Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
69 {
70 const struct got_error *err = NULL;
71 char *path;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
77 free(path);
78 return err;
79 }
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
83 {
84 const struct got_error *err = NULL;
85 FILE *tmpfile = NULL;
86 char *tmppath = NULL;
87 char *path = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
91 path = NULL;
92 goto done;
93 }
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
96 if (err)
97 goto done;
99 if (content) {
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
103 goto done;
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
109 unlink(tmppath);
110 goto done;
113 done:
114 if (fclose(tmpfile) == EOF && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
116 free(tmppath);
117 return err;
120 static const struct got_error *
121 read_meta_file(char **content, const char *path_got, const char *name)
123 const struct got_error *err = NULL;
124 char *path;
125 int fd = -1;
126 ssize_t n;
127 struct stat sb;
129 *content = NULL;
131 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 err = got_error_from_errno("asprintf");
133 path = NULL;
134 goto done;
137 fd = open(path, O_RDONLY | O_NOFOLLOW);
138 if (fd == -1) {
139 if (errno == ENOENT)
140 err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 else
142 err = got_error_from_errno2("open", path);
143 goto done;
145 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 : got_error_from_errno2("flock", path));
148 goto done;
151 if (fstat(fd, &sb) != 0) {
152 err = got_error_from_errno2("fstat", path);
153 goto done;
155 *content = calloc(1, sb.st_size);
156 if (*content == NULL) {
157 err = got_error_from_errno("calloc");
158 goto done;
161 n = read(fd, *content, sb.st_size);
162 if (n != sb.st_size) {
163 err = (n == -1 ? got_error_from_errno2("read", path) :
164 got_error_path(path, GOT_ERR_WORKTREE_META));
165 goto done;
167 if ((*content)[sb.st_size - 1] != '\n') {
168 err = got_error_path(path, GOT_ERR_WORKTREE_META);
169 goto done;
171 (*content)[sb.st_size - 1] = '\0';
173 done:
174 if (fd != -1 && close(fd) == -1 && err == NULL)
175 err = got_error_from_errno2("close", path_got);
176 free(path);
177 if (err) {
178 free(*content);
179 *content = NULL;
181 return err;
184 static const struct got_error *
185 write_head_ref(const char *path_got, struct got_reference *head_ref)
187 const struct got_error *err = NULL;
188 char *refstr = NULL;
190 if (got_ref_is_symbolic(head_ref)) {
191 refstr = got_ref_to_str(head_ref);
192 if (refstr == NULL)
193 return got_error_from_errno("got_ref_to_str");
194 } else {
195 refstr = strdup(got_ref_get_name(head_ref));
196 if (refstr == NULL)
197 return got_error_from_errno("strdup");
199 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
200 free(refstr);
201 return err;
204 const struct got_error *
205 got_worktree_init(const char *path, struct got_reference *head_ref,
206 const char *prefix, struct got_repository *repo)
208 const struct got_error *err = NULL;
209 struct got_object_id *commit_id = NULL;
210 uuid_t uuid;
211 uint32_t uuid_status;
212 int obj_type;
213 char *path_got = NULL;
214 char *formatstr = NULL;
215 char *absprefix = NULL;
216 char *basestr = NULL;
217 char *uuidstr = NULL;
219 if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 err = got_error(GOT_ERR_WORKTREE_REPO);
221 goto done;
224 err = got_ref_resolve(&commit_id, repo, head_ref);
225 if (err)
226 return err;
227 err = got_object_get_type(&obj_type, repo, commit_id);
228 if (err)
229 return err;
230 if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 return got_error(GOT_ERR_OBJ_TYPE);
233 if (!got_path_is_absolute(prefix)) {
234 if (asprintf(&absprefix, "/%s", prefix) == -1)
235 return got_error_from_errno("asprintf");
238 /* Create top-level directory (may already exist). */
239 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 err = got_error_from_errno2("mkdir", path);
241 goto done;
244 /* Create .got directory (may already exist). */
245 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 err = got_error_from_errno("asprintf");
247 goto done;
249 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 err = got_error_from_errno2("mkdir", path_got);
251 goto done;
254 /* Create an empty lock file. */
255 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
256 if (err)
257 goto done;
259 /* Create an empty file index. */
260 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
261 if (err)
262 goto done;
264 /* Write the HEAD reference. */
265 err = write_head_ref(path_got, head_ref);
266 if (err)
267 goto done;
269 /* Record our base commit. */
270 err = got_object_id_str(&basestr, commit_id);
271 if (err)
272 goto done;
273 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
274 if (err)
275 goto done;
277 /* Store path to repository. */
278 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 got_repo_get_path(repo));
280 if (err)
281 goto done;
283 /* Store in-repository path prefix. */
284 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 absprefix ? absprefix : prefix);
286 if (err)
287 goto done;
289 /* Generate UUID. */
290 uuid_create(&uuid, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status, "uuid_create");
293 goto done;
295 uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 if (uuid_status != uuid_s_ok) {
297 err = got_error_uuid(uuid_status, "uuid_to_string");
298 goto done;
300 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
301 if (err)
302 goto done;
304 /* Stamp work tree with format file. */
305 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 err = got_error_from_errno("asprintf");
307 goto done;
309 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
310 if (err)
311 goto done;
313 done:
314 free(commit_id);
315 free(path_got);
316 free(formatstr);
317 free(absprefix);
318 free(basestr);
319 free(uuidstr);
320 return err;
323 static const struct got_error *
324 open_worktree(struct got_worktree **worktree, const char *path)
326 const struct got_error *err = NULL;
327 char *path_got;
328 char *formatstr = NULL;
329 char *uuidstr = NULL;
330 char *path_lock = NULL;
331 char *base_commit_id_str = NULL;
332 int version, fd = -1;
333 const char *errstr;
334 struct got_repository *repo = NULL;
335 uint32_t uuid_status;
337 *worktree = NULL;
339 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 err = got_error_from_errno("asprintf");
341 path_got = NULL;
342 goto done;
345 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 err = got_error_from_errno("asprintf");
347 path_lock = NULL;
348 goto done;
351 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 if (fd == -1) {
353 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 : got_error_from_errno2("open", path_lock));
355 goto done;
358 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
359 if (err)
360 goto done;
362 version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 if (errstr) {
364 err = got_error_msg(GOT_ERR_WORKTREE_META,
365 "could not parse work tree format version number");
366 goto done;
368 if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 err = got_error(GOT_ERR_WORKTREE_VERS);
370 goto done;
373 *worktree = calloc(1, sizeof(**worktree));
374 if (*worktree == NULL) {
375 err = got_error_from_errno("calloc");
376 goto done;
378 (*worktree)->lockfd = -1;
380 (*worktree)->root_path = realpath(path, NULL);
381 if ((*worktree)->root_path == NULL) {
382 err = got_error_from_errno2("realpath", path);
383 goto done;
385 err = read_meta_file(&(*worktree)->repo_path, path_got,
386 GOT_WORKTREE_REPOSITORY);
387 if (err)
388 goto done;
390 err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 GOT_WORKTREE_PATH_PREFIX);
392 if (err)
393 goto done;
395 err = read_meta_file(&base_commit_id_str, path_got,
396 GOT_WORKTREE_BASE_COMMIT);
397 if (err)
398 goto done;
400 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
401 if (err)
402 goto done;
403 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 if (uuid_status != uuid_s_ok) {
405 err = got_error_uuid(uuid_status, "uuid_from_string");
406 goto done;
409 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
410 if (err)
411 goto done;
413 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 base_commit_id_str);
415 if (err)
416 goto done;
418 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 GOT_WORKTREE_HEAD_REF);
420 if (err)
421 goto done;
423 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 (*worktree)->root_path,
425 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 err = got_error_from_errno("asprintf");
427 goto done;
430 err = got_gotconfig_read(&(*worktree)->gotconfig,
431 (*worktree)->gotconfig_path);
433 (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
434 if ((*worktree)->root_fd == -1) {
435 err = got_error_from_errno2("open", (*worktree)->root_path);
436 goto done;
438 done:
439 if (repo) {
440 const struct got_error *close_err = got_repo_close(repo);
441 if (err == NULL)
442 err = close_err;
444 free(path_got);
445 free(path_lock);
446 free(base_commit_id_str);
447 free(uuidstr);
448 free(formatstr);
449 if (err) {
450 if (fd != -1)
451 close(fd);
452 if (*worktree != NULL)
453 got_worktree_close(*worktree);
454 *worktree = NULL;
455 } else
456 (*worktree)->lockfd = fd;
458 return err;
461 const struct got_error *
462 got_worktree_open(struct got_worktree **worktree, const char *path)
464 const struct got_error *err = NULL;
465 char *worktree_path;
467 worktree_path = strdup(path);
468 if (worktree_path == NULL)
469 return got_error_from_errno("strdup");
471 for (;;) {
472 char *parent_path;
474 err = open_worktree(worktree, worktree_path);
475 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
476 free(worktree_path);
477 return err;
479 if (*worktree) {
480 free(worktree_path);
481 return NULL;
483 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
484 break;
485 err = got_path_dirname(&parent_path, worktree_path);
486 if (err) {
487 if (err->code != GOT_ERR_BAD_PATH) {
488 free(worktree_path);
489 return err;
491 break;
493 free(worktree_path);
494 worktree_path = parent_path;
497 free(worktree_path);
498 return got_error(GOT_ERR_NOT_WORKTREE);
501 const struct got_error *
502 got_worktree_close(struct got_worktree *worktree)
504 const struct got_error *err = NULL;
506 if (worktree->lockfd != -1) {
507 if (close(worktree->lockfd) == -1)
508 err = got_error_from_errno2("close",
509 got_worktree_get_root_path(worktree));
511 if (close(worktree->root_fd) == -1 && err == NULL)
512 err = got_error_from_errno2("close",
513 got_worktree_get_root_path(worktree));
514 free(worktree->repo_path);
515 free(worktree->path_prefix);
516 free(worktree->base_commit_id);
517 free(worktree->head_ref_name);
518 free(worktree->root_path);
519 free(worktree->gotconfig_path);
520 got_gotconfig_free(worktree->gotconfig);
521 free(worktree);
522 return err;
525 const char *
526 got_worktree_get_root_path(struct got_worktree *worktree)
528 return worktree->root_path;
531 const char *
532 got_worktree_get_repo_path(struct got_worktree *worktree)
534 return worktree->repo_path;
536 const char *
537 got_worktree_get_path_prefix(struct got_worktree *worktree)
539 return worktree->path_prefix;
542 const struct got_error *
543 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
544 const char *path_prefix)
546 char *absprefix = NULL;
548 if (!got_path_is_absolute(path_prefix)) {
549 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
550 return got_error_from_errno("asprintf");
552 *match = (strcmp(absprefix ? absprefix : path_prefix,
553 worktree->path_prefix) == 0);
554 free(absprefix);
555 return NULL;
558 const char *
559 got_worktree_get_head_ref_name(struct got_worktree *worktree)
561 return worktree->head_ref_name;
564 const struct got_error *
565 got_worktree_set_head_ref(struct got_worktree *worktree,
566 struct got_reference *head_ref)
568 const struct got_error *err = NULL;
569 char *path_got = NULL, *head_ref_name = NULL;
571 if (asprintf(&path_got, "%s/%s", worktree->root_path,
572 GOT_WORKTREE_GOT_DIR) == -1) {
573 err = got_error_from_errno("asprintf");
574 path_got = NULL;
575 goto done;
578 head_ref_name = strdup(got_ref_get_name(head_ref));
579 if (head_ref_name == NULL) {
580 err = got_error_from_errno("strdup");
581 goto done;
584 err = write_head_ref(path_got, head_ref);
585 if (err)
586 goto done;
588 free(worktree->head_ref_name);
589 worktree->head_ref_name = head_ref_name;
590 done:
591 free(path_got);
592 if (err)
593 free(head_ref_name);
594 return err;
597 struct got_object_id *
598 got_worktree_get_base_commit_id(struct got_worktree *worktree)
600 return worktree->base_commit_id;
603 const struct got_error *
604 got_worktree_set_base_commit_id(struct got_worktree *worktree,
605 struct got_repository *repo, struct got_object_id *commit_id)
607 const struct got_error *err;
608 struct got_object *obj = NULL;
609 char *id_str = NULL;
610 char *path_got = NULL;
612 if (asprintf(&path_got, "%s/%s", worktree->root_path,
613 GOT_WORKTREE_GOT_DIR) == -1) {
614 err = got_error_from_errno("asprintf");
615 path_got = NULL;
616 goto done;
619 err = got_object_open(&obj, repo, commit_id);
620 if (err)
621 return err;
623 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
624 err = got_error(GOT_ERR_OBJ_TYPE);
625 goto done;
628 /* Record our base commit. */
629 err = got_object_id_str(&id_str, commit_id);
630 if (err)
631 goto done;
632 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
633 if (err)
634 goto done;
636 free(worktree->base_commit_id);
637 worktree->base_commit_id = got_object_id_dup(commit_id);
638 if (worktree->base_commit_id == NULL) {
639 err = got_error_from_errno("got_object_id_dup");
640 goto done;
642 done:
643 if (obj)
644 got_object_close(obj);
645 free(id_str);
646 free(path_got);
647 return err;
650 const struct got_gotconfig *
651 got_worktree_get_gotconfig(struct got_worktree *worktree)
653 return worktree->gotconfig;
656 static const struct got_error *
657 lock_worktree(struct got_worktree *worktree, int operation)
659 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
660 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
661 : got_error_from_errno2("flock",
662 got_worktree_get_root_path(worktree)));
663 return NULL;
666 static const struct got_error *
667 add_dir_on_disk(struct got_worktree *worktree, const char *path)
669 const struct got_error *err = NULL;
670 char *abspath;
672 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
673 return got_error_from_errno("asprintf");
675 err = got_path_mkdir(abspath);
676 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
677 struct stat sb;
678 err = NULL;
679 if (lstat(abspath, &sb) == -1) {
680 err = got_error_from_errno2("lstat", abspath);
681 } else if (!S_ISDIR(sb.st_mode)) {
682 /* TODO directory is obstructed; do something */
683 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
686 free(abspath);
687 return err;
690 static const struct got_error *
691 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
693 const struct got_error *err = NULL;
694 uint8_t fbuf1[8192];
695 uint8_t fbuf2[8192];
696 size_t flen1 = 0, flen2 = 0;
698 *same = 1;
700 for (;;) {
701 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
702 if (flen1 == 0 && ferror(f1)) {
703 err = got_error_from_errno("fread");
704 break;
706 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
707 if (flen2 == 0 && ferror(f2)) {
708 err = got_error_from_errno("fread");
709 break;
711 if (flen1 == 0) {
712 if (flen2 != 0)
713 *same = 0;
714 break;
715 } else if (flen2 == 0) {
716 if (flen1 != 0)
717 *same = 0;
718 break;
719 } else if (flen1 == flen2) {
720 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
721 *same = 0;
722 break;
724 } else {
725 *same = 0;
726 break;
730 return err;
733 static const struct got_error *
734 check_files_equal(int *same, FILE *f1, FILE *f2)
736 struct stat sb;
737 size_t size1, size2;
739 *same = 1;
741 if (fstat(fileno(f1), &sb) != 0)
742 return got_error_from_errno("fstat");
743 size1 = sb.st_size;
745 if (fstat(fileno(f2), &sb) != 0)
746 return got_error_from_errno("fstat");
747 size2 = sb.st_size;
749 if (size1 != size2) {
750 *same = 0;
751 return NULL;
754 if (fseek(f1, 0L, SEEK_SET) == -1)
755 return got_ferror(f1, GOT_ERR_IO);
756 if (fseek(f2, 0L, SEEK_SET) == -1)
757 return got_ferror(f2, GOT_ERR_IO);
759 return check_file_contents_equal(same, f1, f2);
762 /*
763 * Perform a 3-way merge where the file f_orig acts as the common
764 * ancestor, the file f_deriv acts as the first derived version,
765 * and the file f_deriv2 acts as the second derived version.
766 * The merge result will be written to a new file at ondisk_path; any
767 * existing file at this path will be replaced.
768 */
769 static const struct got_error *
770 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
771 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
772 const char *path, uint16_t st_mode,
773 const char *label_orig, const char *label_deriv, const char *label_deriv2,
774 enum got_diff_algorithm diff_algo, struct got_repository *repo,
775 got_worktree_checkout_cb progress_cb, void *progress_arg)
777 const struct got_error *err = NULL;
778 int merged_fd = -1;
779 FILE *f_merged = NULL;
780 char *merged_path = NULL, *base_path = NULL;
781 int overlapcnt = 0;
782 char *parent = NULL;
784 *local_changes_subsumed = 0;
786 err = got_path_dirname(&parent, ondisk_path);
787 if (err)
788 return err;
790 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
791 err = got_error_from_errno("asprintf");
792 goto done;
795 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
796 if (err)
797 goto done;
799 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
800 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
801 if (err)
802 goto done;
804 err = (*progress_cb)(progress_arg,
805 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
806 if (err)
807 goto done;
809 if (fsync(merged_fd) != 0) {
810 err = got_error_from_errno("fsync");
811 goto done;
814 f_merged = fdopen(merged_fd, "r");
815 if (f_merged == NULL) {
816 err = got_error_from_errno("fdopen");
817 goto done;
819 merged_fd = -1;
821 /* Check if a clean merge has subsumed all local changes. */
822 if (overlapcnt == 0) {
823 err = check_files_equal(local_changes_subsumed, f_deriv,
824 f_merged);
825 if (err)
826 goto done;
829 if (fchmod(fileno(f_merged), st_mode) != 0) {
830 err = got_error_from_errno2("fchmod", merged_path);
831 goto done;
834 if (rename(merged_path, ondisk_path) != 0) {
835 err = got_error_from_errno3("rename", merged_path,
836 ondisk_path);
837 goto done;
839 done:
840 if (err) {
841 if (merged_path)
842 unlink(merged_path);
844 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
845 err = got_error_from_errno("close");
846 if (f_merged && fclose(f_merged) == EOF && err == NULL)
847 err = got_error_from_errno("fclose");
848 free(merged_path);
849 free(base_path);
850 free(parent);
851 return err;
854 static const struct got_error *
855 update_symlink(const char *ondisk_path, const char *target_path,
856 size_t target_len)
858 /* This is not atomic but matches what 'ln -sf' does. */
859 if (unlink(ondisk_path) == -1)
860 return got_error_from_errno2("unlink", ondisk_path);
861 if (symlink(target_path, ondisk_path) == -1)
862 return got_error_from_errno3("symlink", target_path,
863 ondisk_path);
864 return NULL;
867 /*
868 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
869 * in the work tree with a file that contains conflict markers and the
870 * conflicting target paths of the original version, a "derived version"
871 * of a symlink from an incoming change, and a local version of the symlink.
873 * The original versions's target path can be NULL if it is not available,
874 * such as if both derived versions added a new symlink at the same path.
876 * The incoming derived symlink target is NULL in case the incoming change
877 * has deleted this symlink.
878 */
879 static const struct got_error *
880 install_symlink_conflict(const char *deriv_target,
881 struct got_object_id *deriv_base_commit_id, const char *orig_target,
882 const char *label_orig, const char *local_target, const char *ondisk_path)
884 const struct got_error *err;
885 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
886 FILE *f = NULL;
888 err = got_object_id_str(&id_str, deriv_base_commit_id);
889 if (err)
890 return got_error_from_errno("asprintf");
892 if (asprintf(&label_deriv, "%s: commit %s",
893 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
894 err = got_error_from_errno("asprintf");
895 goto done;
898 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
899 if (err)
900 goto done;
902 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
903 err = got_error_from_errno2("fchmod", path);
904 goto done;
907 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
908 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
909 deriv_target ? deriv_target : "(symlink was deleted)",
910 orig_target ? label_orig : "",
911 orig_target ? "\n" : "",
912 orig_target ? orig_target : "",
913 orig_target ? "\n" : "",
914 GOT_DIFF_CONFLICT_MARKER_SEP,
915 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
916 err = got_error_from_errno2("fprintf", path);
917 goto done;
920 if (unlink(ondisk_path) == -1) {
921 err = got_error_from_errno2("unlink", ondisk_path);
922 goto done;
924 if (rename(path, ondisk_path) == -1) {
925 err = got_error_from_errno3("rename", path, ondisk_path);
926 goto done;
928 done:
929 if (f != NULL && fclose(f) == EOF && err == NULL)
930 err = got_error_from_errno2("fclose", path);
931 free(path);
932 free(id_str);
933 free(label_deriv);
934 return err;
937 /* forward declaration */
938 static const struct got_error *
939 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
940 const char *, const char *, uint16_t, const char *,
941 struct got_blob_object *, struct got_object_id *,
942 struct got_repository *, got_worktree_checkout_cb, void *);
944 /*
945 * Merge a symlink into the work tree, where blob_orig acts as the common
946 * ancestor, deriv_target is the link target of the first derived version,
947 * and the symlink on disk acts as the second derived version.
948 * Assume that contents of both blobs represent symlinks.
949 */
950 static const struct got_error *
951 merge_symlink(struct got_worktree *worktree,
952 struct got_blob_object *blob_orig, const char *ondisk_path,
953 const char *path, const char *label_orig, const char *deriv_target,
954 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
955 got_worktree_checkout_cb progress_cb, void *progress_arg)
957 const struct got_error *err = NULL;
958 char *ancestor_target = NULL;
959 struct stat sb;
960 ssize_t ondisk_len, deriv_len;
961 char ondisk_target[PATH_MAX];
962 int have_local_change = 0;
963 int have_incoming_change = 0;
965 if (lstat(ondisk_path, &sb) == -1)
966 return got_error_from_errno2("lstat", ondisk_path);
968 ondisk_len = readlink(ondisk_path, ondisk_target,
969 sizeof(ondisk_target));
970 if (ondisk_len == -1) {
971 err = got_error_from_errno2("readlink",
972 ondisk_path);
973 goto done;
975 ondisk_target[ondisk_len] = '\0';
977 if (blob_orig) {
978 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
979 if (err)
980 goto done;
983 if (ancestor_target == NULL ||
984 (ondisk_len != strlen(ancestor_target) ||
985 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
986 have_local_change = 1;
988 deriv_len = strlen(deriv_target);
989 if (ancestor_target == NULL ||
990 (deriv_len != strlen(ancestor_target) ||
991 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
992 have_incoming_change = 1;
994 if (!have_local_change && !have_incoming_change) {
995 if (ancestor_target) {
996 /* Both sides made the same change. */
997 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
998 path);
999 } else if (deriv_len == ondisk_len &&
1000 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1001 /* Both sides added the same symlink. */
1002 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1003 path);
1004 } else {
1005 /* Both sides added symlinks which don't match. */
1006 err = install_symlink_conflict(deriv_target,
1007 deriv_base_commit_id, ancestor_target,
1008 label_orig, ondisk_target, ondisk_path);
1009 if (err)
1010 goto done;
1011 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1012 path);
1014 } else if (!have_local_change && have_incoming_change) {
1015 /* Apply the incoming change. */
1016 err = update_symlink(ondisk_path, deriv_target,
1017 strlen(deriv_target));
1018 if (err)
1019 goto done;
1020 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1021 } else if (have_local_change && have_incoming_change) {
1022 if (deriv_len == ondisk_len &&
1023 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1024 /* Both sides made the same change. */
1025 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1026 path);
1027 } else {
1028 err = install_symlink_conflict(deriv_target,
1029 deriv_base_commit_id, ancestor_target, label_orig,
1030 ondisk_target, ondisk_path);
1031 if (err)
1032 goto done;
1033 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1034 path);
1038 done:
1039 free(ancestor_target);
1040 return err;
1043 static const struct got_error *
1044 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
1046 const struct got_error *err = NULL;
1047 char target_path[PATH_MAX];
1048 ssize_t target_len;
1049 size_t n;
1050 FILE *f;
1052 *outfile = NULL;
1054 f = got_opentemp();
1055 if (f == NULL)
1056 return got_error_from_errno("got_opentemp");
1057 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
1058 if (target_len == -1) {
1059 err = got_error_from_errno2("readlink", ondisk_path);
1060 goto done;
1062 n = fwrite(target_path, 1, target_len, f);
1063 if (n != target_len) {
1064 err = got_ferror(f, GOT_ERR_IO);
1065 goto done;
1067 if (fflush(f) == EOF) {
1068 err = got_error_from_errno("fflush");
1069 goto done;
1071 if (fseek(f, 0L, SEEK_SET) == -1) {
1072 err = got_ferror(f, GOT_ERR_IO);
1073 goto done;
1075 done:
1076 if (err)
1077 fclose(f);
1078 else
1079 *outfile = f;
1080 return err;
1084 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1085 * blob_deriv acts as the first derived version, and the file on disk
1086 * acts as the second derived version.
1088 static const struct got_error *
1089 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1090 struct got_blob_object *blob_orig, const char *ondisk_path,
1091 const char *path, uint16_t st_mode, const char *label_orig,
1092 struct got_blob_object *blob_deriv,
1093 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1094 got_worktree_checkout_cb progress_cb, void *progress_arg)
1096 const struct got_error *err = NULL;
1097 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
1098 char *blob_orig_path = NULL;
1099 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1100 char *label_deriv = NULL, *parent = NULL;
1102 *local_changes_subsumed = 0;
1104 err = got_path_dirname(&parent, ondisk_path);
1105 if (err)
1106 return err;
1108 if (blob_orig) {
1109 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1110 parent) == -1) {
1111 err = got_error_from_errno("asprintf");
1112 base_path = NULL;
1113 goto done;
1116 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
1117 if (err)
1118 goto done;
1119 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1120 blob_orig);
1121 if (err)
1122 goto done;
1123 free(base_path);
1124 } else {
1126 * No common ancestor exists. This is an "add vs add" conflict
1127 * and we simply use an empty ancestor file to make both files
1128 * appear in the merged result in their entirety.
1130 f_orig = got_opentemp();
1131 if (f_orig == NULL) {
1132 err = got_error_from_errno("got_opentemp");
1133 goto done;
1137 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1138 err = got_error_from_errno("asprintf");
1139 base_path = NULL;
1140 goto done;
1143 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1144 if (err)
1145 goto done;
1146 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1147 blob_deriv);
1148 if (err)
1149 goto done;
1151 err = got_object_id_str(&id_str, deriv_base_commit_id);
1152 if (err)
1153 goto done;
1154 if (asprintf(&label_deriv, "%s: commit %s",
1155 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1156 err = got_error_from_errno("asprintf");
1157 goto done;
1161 * In order the run a 3-way merge with a symlink we copy the symlink's
1162 * target path into a temporary file and use that file with diff3.
1164 if (S_ISLNK(st_mode)) {
1165 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1166 if (err)
1167 goto done;
1168 } else {
1169 int fd;
1170 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
1171 if (fd == -1) {
1172 err = got_error_from_errno2("open", ondisk_path);
1173 goto done;
1175 f_deriv2 = fdopen(fd, "r");
1176 if (f_deriv2 == NULL) {
1177 err = got_error_from_errno2("fdopen", ondisk_path);
1178 close(fd);
1179 goto done;
1183 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1184 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1185 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1186 done:
1187 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1188 err = got_error_from_errno("fclose");
1189 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1190 err = got_error_from_errno("fclose");
1191 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1192 err = got_error_from_errno("fclose");
1193 free(base_path);
1194 if (blob_orig_path) {
1195 unlink(blob_orig_path);
1196 free(blob_orig_path);
1198 if (blob_deriv_path) {
1199 unlink(blob_deriv_path);
1200 free(blob_deriv_path);
1202 free(id_str);
1203 free(label_deriv);
1204 free(parent);
1205 return err;
1208 static const struct got_error *
1209 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1210 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1211 int wt_fd, const char *path, struct got_object_id *blob_id)
1213 const struct got_error *err = NULL;
1214 struct got_fileindex_entry *new_ie;
1216 *new_iep = NULL;
1218 err = got_fileindex_entry_alloc(&new_ie, path);
1219 if (err)
1220 return err;
1222 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1223 blob_id->sha1, base_commit_id->sha1, 1);
1224 if (err)
1225 goto done;
1227 err = got_fileindex_entry_add(fileindex, new_ie);
1228 done:
1229 if (err)
1230 got_fileindex_entry_free(new_ie);
1231 else
1232 *new_iep = new_ie;
1233 return err;
1236 static mode_t
1237 get_ondisk_perms(int executable, mode_t st_mode)
1239 mode_t xbits = S_IXUSR;
1241 if (executable) {
1242 /* Map read bits to execute bits. */
1243 if (st_mode & S_IRGRP)
1244 xbits |= S_IXGRP;
1245 if (st_mode & S_IROTH)
1246 xbits |= S_IXOTH;
1247 return st_mode | xbits;
1250 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1253 /* forward declaration */
1254 static const struct got_error *
1255 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1256 const char *path, mode_t te_mode, mode_t st_mode,
1257 struct got_blob_object *blob, int restoring_missing_file,
1258 int reverting_versioned_file, int installing_bad_symlink,
1259 int path_is_unversioned, struct got_repository *repo,
1260 got_worktree_checkout_cb progress_cb, void *progress_arg);
1263 * This function assumes that the provided symlink target points at a
1264 * safe location in the work tree!
1266 static const struct got_error *
1267 replace_existing_symlink(int *did_something, const char *ondisk_path,
1268 const char *target_path, size_t target_len)
1270 const struct got_error *err = NULL;
1271 ssize_t elen;
1272 char etarget[PATH_MAX];
1273 int fd;
1275 *did_something = 0;
1278 * "Bad" symlinks (those pointing outside the work tree or into the
1279 * .got directory) are installed in the work tree as a regular file
1280 * which contains the bad symlink target path.
1281 * The new symlink target has already been checked for safety by our
1282 * caller. If we can successfully open a regular file then we simply
1283 * replace this file with a symlink below.
1285 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1286 if (fd == -1) {
1287 if (errno != ELOOP)
1288 return got_error_from_errno2("open", ondisk_path);
1290 /* We are updating an existing on-disk symlink. */
1291 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1292 if (elen == -1)
1293 return got_error_from_errno2("readlink", ondisk_path);
1295 if (elen == target_len &&
1296 memcmp(etarget, target_path, target_len) == 0)
1297 return NULL; /* nothing to do */
1300 *did_something = 1;
1301 err = update_symlink(ondisk_path, target_path, target_len);
1302 if (fd != -1 && close(fd) == -1 && err == NULL)
1303 err = got_error_from_errno2("close", ondisk_path);
1304 return err;
1307 static const struct got_error *
1308 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1309 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1311 const struct got_error *err = NULL;
1312 char canonpath[PATH_MAX];
1313 char *path_got = NULL;
1315 *is_bad_symlink = 0;
1317 if (target_len >= sizeof(canonpath)) {
1318 *is_bad_symlink = 1;
1319 return NULL;
1323 * We do not use realpath(3) to resolve the symlink's target
1324 * path because we don't want to resolve symlinks recursively.
1325 * Instead we make the path absolute and then canonicalize it.
1326 * Relative symlink target lookup should begin at the directory
1327 * in which the blob object is being installed.
1329 if (!got_path_is_absolute(target_path)) {
1330 char *abspath, *parent;
1331 err = got_path_dirname(&parent, ondisk_path);
1332 if (err)
1333 return err;
1334 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1335 free(parent);
1336 return got_error_from_errno("asprintf");
1338 free(parent);
1339 if (strlen(abspath) >= sizeof(canonpath)) {
1340 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1341 free(abspath);
1342 return err;
1344 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1345 free(abspath);
1346 if (err)
1347 return err;
1348 } else {
1349 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1350 if (err)
1351 return err;
1354 /* Only allow symlinks pointing at paths within the work tree. */
1355 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1356 *is_bad_symlink = 1;
1357 return NULL;
1360 /* Do not allow symlinks pointing into the .got directory. */
1361 if (asprintf(&path_got, "%s/%s", wtroot_path,
1362 GOT_WORKTREE_GOT_DIR) == -1)
1363 return got_error_from_errno("asprintf");
1364 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1365 *is_bad_symlink = 1;
1367 free(path_got);
1368 return NULL;
1371 static const struct got_error *
1372 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1373 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1374 int restoring_missing_file, int reverting_versioned_file,
1375 int path_is_unversioned, struct got_repository *repo,
1376 got_worktree_checkout_cb progress_cb, void *progress_arg)
1378 const struct got_error *err = NULL;
1379 char target_path[PATH_MAX];
1380 size_t len, target_len = 0;
1381 char *path_got = NULL;
1382 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1383 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1385 *is_bad_symlink = 0;
1388 * Blob object content specifies the target path of the link.
1389 * If a symbolic link cannot be installed we instead create
1390 * a regular file which contains the link target path stored
1391 * in the blob object.
1393 do {
1394 err = got_object_blob_read_block(&len, blob);
1395 if (len + target_len >= sizeof(target_path)) {
1396 /* Path too long; install as a regular file. */
1397 *is_bad_symlink = 1;
1398 got_object_blob_rewind(blob);
1399 return install_blob(worktree, ondisk_path, path,
1400 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1401 restoring_missing_file, reverting_versioned_file,
1402 1, path_is_unversioned, repo, progress_cb,
1403 progress_arg);
1405 if (len > 0) {
1406 /* Skip blob object header first time around. */
1407 memcpy(target_path + target_len, buf + hdrlen,
1408 len - hdrlen);
1409 target_len += len - hdrlen;
1410 hdrlen = 0;
1412 } while (len != 0);
1413 target_path[target_len] = '\0';
1415 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1416 ondisk_path, worktree->root_path);
1417 if (err)
1418 return err;
1420 if (*is_bad_symlink) {
1421 /* install as a regular file */
1422 got_object_blob_rewind(blob);
1423 err = install_blob(worktree, ondisk_path, path,
1424 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1425 restoring_missing_file, reverting_versioned_file, 1,
1426 path_is_unversioned, repo, progress_cb, progress_arg);
1427 goto done;
1430 if (symlink(target_path, ondisk_path) == -1) {
1431 if (errno == EEXIST) {
1432 int symlink_replaced;
1433 if (path_is_unversioned) {
1434 err = (*progress_cb)(progress_arg,
1435 GOT_STATUS_UNVERSIONED, path);
1436 goto done;
1438 err = replace_existing_symlink(&symlink_replaced,
1439 ondisk_path, target_path, target_len);
1440 if (err)
1441 goto done;
1442 if (progress_cb) {
1443 if (symlink_replaced) {
1444 err = (*progress_cb)(progress_arg,
1445 reverting_versioned_file ?
1446 GOT_STATUS_REVERT :
1447 GOT_STATUS_UPDATE, path);
1448 } else {
1449 err = (*progress_cb)(progress_arg,
1450 GOT_STATUS_EXISTS, path);
1453 goto done; /* Nothing else to do. */
1456 if (errno == ENOENT) {
1457 char *parent;
1458 err = got_path_dirname(&parent, ondisk_path);
1459 if (err)
1460 goto done;
1461 err = add_dir_on_disk(worktree, parent);
1462 free(parent);
1463 if (err)
1464 goto done;
1466 * Retry, and fall through to error handling
1467 * below if this second attempt fails.
1469 if (symlink(target_path, ondisk_path) != -1) {
1470 err = NULL; /* success */
1471 goto done;
1475 /* Handle errors from first or second creation attempt. */
1476 if (errno == ENAMETOOLONG) {
1477 /* bad target path; install as a regular file */
1478 *is_bad_symlink = 1;
1479 got_object_blob_rewind(blob);
1480 err = install_blob(worktree, ondisk_path, path,
1481 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1482 restoring_missing_file, reverting_versioned_file, 1,
1483 path_is_unversioned, repo,
1484 progress_cb, progress_arg);
1485 } else if (errno == ENOTDIR) {
1486 err = got_error_path(ondisk_path,
1487 GOT_ERR_FILE_OBSTRUCTED);
1488 } else {
1489 err = got_error_from_errno3("symlink",
1490 target_path, ondisk_path);
1492 } else if (progress_cb)
1493 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1494 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1495 done:
1496 free(path_got);
1497 return err;
1500 static const struct got_error *
1501 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1502 const char *path, mode_t te_mode, mode_t st_mode,
1503 struct got_blob_object *blob, int restoring_missing_file,
1504 int reverting_versioned_file, int installing_bad_symlink,
1505 int path_is_unversioned, struct got_repository *repo,
1506 got_worktree_checkout_cb progress_cb, void *progress_arg)
1508 const struct got_error *err = NULL;
1509 int fd = -1;
1510 size_t len, hdrlen;
1511 int update = 0;
1512 char *tmppath = NULL;
1514 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1515 GOT_DEFAULT_FILE_MODE);
1516 if (fd == -1) {
1517 if (errno == ENOENT) {
1518 char *parent;
1519 err = got_path_dirname(&parent, path);
1520 if (err)
1521 return err;
1522 err = add_dir_on_disk(worktree, parent);
1523 free(parent);
1524 if (err)
1525 return err;
1526 fd = open(ondisk_path,
1527 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1528 GOT_DEFAULT_FILE_MODE);
1529 if (fd == -1)
1530 return got_error_from_errno2("open",
1531 ondisk_path);
1532 } else if (errno == EEXIST) {
1533 if (path_is_unversioned) {
1534 err = (*progress_cb)(progress_arg,
1535 GOT_STATUS_UNVERSIONED, path);
1536 goto done;
1538 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1539 !S_ISREG(st_mode) && !installing_bad_symlink) {
1540 /* TODO file is obstructed; do something */
1541 err = got_error_path(ondisk_path,
1542 GOT_ERR_FILE_OBSTRUCTED);
1543 goto done;
1544 } else {
1545 err = got_opentemp_named_fd(&tmppath, &fd,
1546 ondisk_path);
1547 if (err)
1548 goto done;
1549 update = 1;
1551 } else
1552 return got_error_from_errno2("open", ondisk_path);
1555 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1556 err = got_error_from_errno2("fchmod",
1557 update ? tmppath : ondisk_path);
1558 goto done;
1561 if (progress_cb) {
1562 if (restoring_missing_file)
1563 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1564 path);
1565 else if (reverting_versioned_file)
1566 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1567 path);
1568 else
1569 err = (*progress_cb)(progress_arg,
1570 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1571 if (err)
1572 goto done;
1575 hdrlen = got_object_blob_get_hdrlen(blob);
1576 do {
1577 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1578 err = got_object_blob_read_block(&len, blob);
1579 if (err)
1580 break;
1581 if (len > 0) {
1582 /* Skip blob object header first time around. */
1583 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1584 if (outlen == -1) {
1585 err = got_error_from_errno("write");
1586 goto done;
1587 } else if (outlen != len - hdrlen) {
1588 err = got_error(GOT_ERR_IO);
1589 goto done;
1591 hdrlen = 0;
1593 } while (len != 0);
1595 if (fsync(fd) != 0) {
1596 err = got_error_from_errno("fsync");
1597 goto done;
1600 if (update) {
1601 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1602 err = got_error_from_errno2("unlink", ondisk_path);
1603 goto done;
1605 if (rename(tmppath, ondisk_path) != 0) {
1606 err = got_error_from_errno3("rename", tmppath,
1607 ondisk_path);
1608 goto done;
1610 free(tmppath);
1611 tmppath = NULL;
1614 done:
1615 if (fd != -1 && close(fd) == -1 && err == NULL)
1616 err = got_error_from_errno("close");
1617 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1618 err = got_error_from_errno2("unlink", tmppath);
1619 free(tmppath);
1620 return err;
1623 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1624 static const struct got_error *
1625 get_modified_file_content_status(unsigned char *status, FILE *f)
1627 const struct got_error *err = NULL;
1628 const char *markers[3] = {
1629 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1630 GOT_DIFF_CONFLICT_MARKER_SEP,
1631 GOT_DIFF_CONFLICT_MARKER_END
1633 int i = 0;
1634 char *line = NULL;
1635 size_t linesize = 0;
1636 ssize_t linelen;
1638 while (*status == GOT_STATUS_MODIFY) {
1639 linelen = getline(&line, &linesize, f);
1640 if (linelen == -1) {
1641 if (feof(f))
1642 break;
1643 err = got_ferror(f, GOT_ERR_IO);
1644 break;
1647 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1648 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1649 == 0)
1650 *status = GOT_STATUS_CONFLICT;
1651 else
1652 i++;
1655 free(line);
1657 return err;
1660 static int
1661 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1663 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1664 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1667 static int
1668 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1670 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1671 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1672 ie->mtime_sec == sb->st_mtim.tv_sec &&
1673 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1674 ie->size == (sb->st_size & 0xffffffff) &&
1675 !xbit_differs(ie, sb->st_mode));
1678 static unsigned char
1679 get_staged_status(struct got_fileindex_entry *ie)
1681 switch (got_fileindex_entry_stage_get(ie)) {
1682 case GOT_FILEIDX_STAGE_ADD:
1683 return GOT_STATUS_ADD;
1684 case GOT_FILEIDX_STAGE_DELETE:
1685 return GOT_STATUS_DELETE;
1686 case GOT_FILEIDX_STAGE_MODIFY:
1687 return GOT_STATUS_MODIFY;
1688 default:
1689 return GOT_STATUS_NO_CHANGE;
1693 static const struct got_error *
1694 get_symlink_modification_status(unsigned char *status,
1695 struct got_fileindex_entry *ie, const char *abspath,
1696 int dirfd, const char *de_name, struct got_blob_object *blob)
1698 const struct got_error *err = NULL;
1699 char target_path[PATH_MAX];
1700 char etarget[PATH_MAX];
1701 ssize_t elen;
1702 size_t len, target_len = 0;
1703 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1704 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1706 *status = GOT_STATUS_NO_CHANGE;
1708 /* Blob object content specifies the target path of the link. */
1709 do {
1710 err = got_object_blob_read_block(&len, blob);
1711 if (err)
1712 return err;
1713 if (len + target_len >= sizeof(target_path)) {
1715 * Should not happen. The blob contents were OK
1716 * when this symlink was installed.
1718 return got_error(GOT_ERR_NO_SPACE);
1720 if (len > 0) {
1721 /* Skip blob object header first time around. */
1722 memcpy(target_path + target_len, buf + hdrlen,
1723 len - hdrlen);
1724 target_len += len - hdrlen;
1725 hdrlen = 0;
1727 } while (len != 0);
1728 target_path[target_len] = '\0';
1730 if (dirfd != -1) {
1731 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1732 if (elen == -1)
1733 return got_error_from_errno2("readlinkat", abspath);
1734 } else {
1735 elen = readlink(abspath, etarget, sizeof(etarget));
1736 if (elen == -1)
1737 return got_error_from_errno2("readlink", abspath);
1740 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1741 *status = GOT_STATUS_MODIFY;
1743 return NULL;
1746 static const struct got_error *
1747 get_file_status(unsigned char *status, struct stat *sb,
1748 struct got_fileindex_entry *ie, const char *abspath,
1749 int dirfd, const char *de_name, struct got_repository *repo)
1751 const struct got_error *err = NULL;
1752 struct got_object_id id;
1753 size_t hdrlen;
1754 int fd = -1;
1755 FILE *f = NULL;
1756 uint8_t fbuf[8192];
1757 struct got_blob_object *blob = NULL;
1758 size_t flen, blen;
1759 unsigned char staged_status = get_staged_status(ie);
1761 *status = GOT_STATUS_NO_CHANGE;
1762 memset(sb, 0, sizeof(*sb));
1765 * Whenever the caller provides a directory descriptor and a
1766 * directory entry name for the file, use them! This prevents
1767 * race conditions if filesystem paths change beneath our feet.
1769 if (dirfd != -1) {
1770 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1771 if (errno == ENOENT) {
1772 if (got_fileindex_entry_has_file_on_disk(ie))
1773 *status = GOT_STATUS_MISSING;
1774 else
1775 *status = GOT_STATUS_DELETE;
1776 goto done;
1778 err = got_error_from_errno2("fstatat", abspath);
1779 goto done;
1781 } else {
1782 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1783 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1784 return got_error_from_errno2("open", abspath);
1785 else if (fd == -1 && errno == ELOOP) {
1786 if (lstat(abspath, sb) == -1)
1787 return got_error_from_errno2("lstat", abspath);
1788 } else if (fd == -1 || fstat(fd, sb) == -1) {
1789 if (errno == ENOENT) {
1790 if (got_fileindex_entry_has_file_on_disk(ie))
1791 *status = GOT_STATUS_MISSING;
1792 else
1793 *status = GOT_STATUS_DELETE;
1794 goto done;
1796 err = got_error_from_errno2("fstat", abspath);
1797 goto done;
1801 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1802 *status = GOT_STATUS_OBSTRUCTED;
1803 goto done;
1806 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1807 *status = GOT_STATUS_DELETE;
1808 goto done;
1809 } else if (!got_fileindex_entry_has_blob(ie) &&
1810 staged_status != GOT_STATUS_ADD) {
1811 *status = GOT_STATUS_ADD;
1812 goto done;
1815 if (!stat_info_differs(ie, sb))
1816 goto done;
1818 if (S_ISLNK(sb->st_mode) &&
1819 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1820 *status = GOT_STATUS_MODIFY;
1821 goto done;
1824 if (staged_status == GOT_STATUS_MODIFY ||
1825 staged_status == GOT_STATUS_ADD)
1826 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1827 else
1828 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1830 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1831 if (err)
1832 goto done;
1834 if (S_ISLNK(sb->st_mode)) {
1835 err = get_symlink_modification_status(status, ie,
1836 abspath, dirfd, de_name, blob);
1837 goto done;
1840 if (dirfd != -1) {
1841 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1842 if (fd == -1) {
1843 err = got_error_from_errno2("openat", abspath);
1844 goto done;
1848 f = fdopen(fd, "r");
1849 if (f == NULL) {
1850 err = got_error_from_errno2("fdopen", abspath);
1851 goto done;
1853 fd = -1;
1854 hdrlen = got_object_blob_get_hdrlen(blob);
1855 for (;;) {
1856 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1857 err = got_object_blob_read_block(&blen, blob);
1858 if (err)
1859 goto done;
1860 /* Skip length of blob object header first time around. */
1861 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1862 if (flen == 0 && ferror(f)) {
1863 err = got_error_from_errno("fread");
1864 goto done;
1866 if (blen - hdrlen == 0) {
1867 if (flen != 0)
1868 *status = GOT_STATUS_MODIFY;
1869 break;
1870 } else if (flen == 0) {
1871 if (blen - hdrlen != 0)
1872 *status = GOT_STATUS_MODIFY;
1873 break;
1874 } else if (blen - hdrlen == flen) {
1875 /* Skip blob object header first time around. */
1876 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1877 *status = GOT_STATUS_MODIFY;
1878 break;
1880 } else {
1881 *status = GOT_STATUS_MODIFY;
1882 break;
1884 hdrlen = 0;
1887 if (*status == GOT_STATUS_MODIFY) {
1888 rewind(f);
1889 err = get_modified_file_content_status(status, f);
1890 } else if (xbit_differs(ie, sb->st_mode))
1891 *status = GOT_STATUS_MODE_CHANGE;
1892 done:
1893 if (blob)
1894 got_object_blob_close(blob);
1895 if (f != NULL && fclose(f) == EOF && err == NULL)
1896 err = got_error_from_errno2("fclose", abspath);
1897 if (fd != -1 && close(fd) == -1 && err == NULL)
1898 err = got_error_from_errno2("close", abspath);
1899 return err;
1903 * Update timestamps in the file index if a file is unmodified and
1904 * we had to run a full content comparison to find out.
1906 static const struct got_error *
1907 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1908 struct got_fileindex_entry *ie, struct stat *sb)
1910 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1911 return got_fileindex_entry_update(ie, wt_fd, path,
1912 ie->blob_sha1, ie->commit_sha1, 1);
1914 return NULL;
1917 static const struct got_error *
1918 update_blob(struct got_worktree *worktree,
1919 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1920 struct got_tree_entry *te, const char *path,
1921 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1922 void *progress_arg)
1924 const struct got_error *err = NULL;
1925 struct got_blob_object *blob = NULL;
1926 char *ondisk_path;
1927 unsigned char status = GOT_STATUS_NO_CHANGE;
1928 struct stat sb;
1930 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1931 return got_error_from_errno("asprintf");
1933 if (ie) {
1934 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1935 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1936 goto done;
1938 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1939 repo);
1940 if (err)
1941 goto done;
1942 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1943 sb.st_mode = got_fileindex_perms_to_st(ie);
1944 } else {
1945 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1946 status = GOT_STATUS_UNVERSIONED;
1949 if (status == GOT_STATUS_OBSTRUCTED) {
1950 if (ie)
1951 got_fileindex_entry_mark_skipped(ie);
1952 err = (*progress_cb)(progress_arg, status, path);
1953 goto done;
1955 if (status == GOT_STATUS_CONFLICT) {
1956 if (ie)
1957 got_fileindex_entry_mark_skipped(ie);
1958 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1959 path);
1960 goto done;
1963 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1964 (S_ISLNK(te->mode) ||
1965 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1967 * This is a regular file or an installed bad symlink.
1968 * If the file index indicates that this file is already
1969 * up-to-date with respect to the repository we can skip
1970 * updating contents of this file.
1972 if (got_fileindex_entry_has_commit(ie) &&
1973 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1974 SHA1_DIGEST_LENGTH) == 0) {
1975 /* Same commit. */
1976 err = sync_timestamps(worktree->root_fd,
1977 path, status, ie, &sb);
1978 if (err)
1979 goto done;
1980 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1981 path);
1982 goto done;
1984 if (got_fileindex_entry_has_blob(ie) &&
1985 memcmp(ie->blob_sha1, te->id.sha1,
1986 SHA1_DIGEST_LENGTH) == 0) {
1987 /* Different commit but the same blob. */
1988 err = sync_timestamps(worktree->root_fd,
1989 path, status, ie, &sb);
1990 if (err)
1991 goto done;
1992 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1993 path);
1994 goto done;
1998 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1999 if (err)
2000 goto done;
2002 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2003 int update_timestamps;
2004 struct got_blob_object *blob2 = NULL;
2005 char *label_orig = NULL;
2006 if (got_fileindex_entry_has_blob(ie)) {
2007 struct got_object_id id2;
2008 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2009 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2010 if (err)
2011 goto done;
2013 if (got_fileindex_entry_has_commit(ie)) {
2014 char id_str[SHA1_DIGEST_STRING_LENGTH];
2015 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2016 sizeof(id_str)) == NULL) {
2017 err = got_error_path(id_str,
2018 GOT_ERR_BAD_OBJ_ID_STR);
2019 goto done;
2021 if (asprintf(&label_orig, "%s: commit %s",
2022 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2023 err = got_error_from_errno("asprintf");
2024 goto done;
2027 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2028 char *link_target;
2029 err = got_object_blob_read_to_str(&link_target, blob);
2030 if (err)
2031 goto done;
2032 err = merge_symlink(worktree, blob2, ondisk_path, path,
2033 label_orig, link_target, worktree->base_commit_id,
2034 repo, progress_cb, progress_arg);
2035 free(link_target);
2036 } else {
2037 err = merge_blob(&update_timestamps, worktree, blob2,
2038 ondisk_path, path, sb.st_mode, label_orig, blob,
2039 worktree->base_commit_id, repo,
2040 progress_cb, progress_arg);
2042 free(label_orig);
2043 if (blob2)
2044 got_object_blob_close(blob2);
2045 if (err)
2046 goto done;
2048 * Do not update timestamps of files with local changes.
2049 * Otherwise, a future status walk would treat them as
2050 * unmodified files again.
2052 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2053 blob->id.sha1, worktree->base_commit_id->sha1,
2054 update_timestamps);
2055 } else if (status == GOT_STATUS_MODE_CHANGE) {
2056 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2057 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2058 } else if (status == GOT_STATUS_DELETE) {
2059 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2060 if (err)
2061 goto done;
2062 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2063 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2064 if (err)
2065 goto done;
2066 } else {
2067 int is_bad_symlink = 0;
2068 if (S_ISLNK(te->mode)) {
2069 err = install_symlink(&is_bad_symlink, worktree,
2070 ondisk_path, path, blob,
2071 status == GOT_STATUS_MISSING, 0,
2072 status == GOT_STATUS_UNVERSIONED, repo,
2073 progress_cb, progress_arg);
2074 } else {
2075 err = install_blob(worktree, ondisk_path, path,
2076 te->mode, sb.st_mode, blob,
2077 status == GOT_STATUS_MISSING, 0, 0,
2078 status == GOT_STATUS_UNVERSIONED, repo,
2079 progress_cb, progress_arg);
2081 if (err)
2082 goto done;
2084 if (ie) {
2085 err = got_fileindex_entry_update(ie,
2086 worktree->root_fd, path, blob->id.sha1,
2087 worktree->base_commit_id->sha1, 1);
2088 } else {
2089 err = create_fileindex_entry(&ie, fileindex,
2090 worktree->base_commit_id, worktree->root_fd, path,
2091 &blob->id);
2093 if (err)
2094 goto done;
2096 if (is_bad_symlink) {
2097 got_fileindex_entry_filetype_set(ie,
2098 GOT_FILEIDX_MODE_BAD_SYMLINK);
2101 got_object_blob_close(blob);
2102 done:
2103 free(ondisk_path);
2104 return err;
2107 static const struct got_error *
2108 remove_ondisk_file(const char *root_path, const char *path)
2110 const struct got_error *err = NULL;
2111 char *ondisk_path = NULL, *parent = NULL;
2113 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2114 return got_error_from_errno("asprintf");
2116 if (unlink(ondisk_path) == -1) {
2117 if (errno != ENOENT)
2118 err = got_error_from_errno2("unlink", ondisk_path);
2119 } else {
2120 size_t root_len = strlen(root_path);
2121 err = got_path_dirname(&parent, ondisk_path);
2122 if (err)
2123 goto done;
2124 while (got_path_cmp(parent, root_path,
2125 strlen(parent), root_len) != 0) {
2126 free(ondisk_path);
2127 ondisk_path = parent;
2128 parent = NULL;
2129 if (rmdir(ondisk_path) == -1) {
2130 if (errno != ENOTEMPTY)
2131 err = got_error_from_errno2("rmdir",
2132 ondisk_path);
2133 break;
2135 err = got_path_dirname(&parent, ondisk_path);
2136 if (err)
2137 break;
2140 done:
2141 free(ondisk_path);
2142 free(parent);
2143 return err;
2146 static const struct got_error *
2147 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2148 struct got_fileindex_entry *ie, struct got_repository *repo,
2149 got_worktree_checkout_cb progress_cb, void *progress_arg)
2151 const struct got_error *err = NULL;
2152 unsigned char status;
2153 struct stat sb;
2154 char *ondisk_path;
2156 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2157 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2159 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2160 == -1)
2161 return got_error_from_errno("asprintf");
2163 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2164 if (err)
2165 goto done;
2167 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2168 char ondisk_target[PATH_MAX];
2169 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2170 sizeof(ondisk_target));
2171 if (ondisk_len == -1) {
2172 err = got_error_from_errno2("readlink", ondisk_path);
2173 goto done;
2175 ondisk_target[ondisk_len] = '\0';
2176 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2177 NULL, NULL, /* XXX pass common ancestor info? */
2178 ondisk_target, ondisk_path);
2179 if (err)
2180 goto done;
2181 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2182 ie->path);
2183 goto done;
2186 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2187 status == GOT_STATUS_ADD) {
2188 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2189 if (err)
2190 goto done;
2192 * Preserve the working file and change the deleted blob's
2193 * entry into a schedule-add entry.
2195 err = got_fileindex_entry_update(ie, worktree->root_fd,
2196 ie->path, NULL, NULL, 0);
2197 } else {
2198 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2199 if (err)
2200 goto done;
2201 if (status == GOT_STATUS_NO_CHANGE) {
2202 err = remove_ondisk_file(worktree->root_path, ie->path);
2203 if (err)
2204 goto done;
2206 got_fileindex_entry_remove(fileindex, ie);
2208 done:
2209 free(ondisk_path);
2210 return err;
2213 struct diff_cb_arg {
2214 struct got_fileindex *fileindex;
2215 struct got_worktree *worktree;
2216 struct got_repository *repo;
2217 got_worktree_checkout_cb progress_cb;
2218 void *progress_arg;
2219 got_cancel_cb cancel_cb;
2220 void *cancel_arg;
2223 static const struct got_error *
2224 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2225 struct got_tree_entry *te, const char *parent_path)
2227 struct diff_cb_arg *a = arg;
2229 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2230 return got_error(GOT_ERR_CANCELLED);
2232 return update_blob(a->worktree, a->fileindex, ie, te,
2233 ie->path, a->repo, a->progress_cb, a->progress_arg);
2236 static const struct got_error *
2237 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2239 struct diff_cb_arg *a = arg;
2241 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2242 return got_error(GOT_ERR_CANCELLED);
2244 return delete_blob(a->worktree, a->fileindex, ie,
2245 a->repo, a->progress_cb, a->progress_arg);
2248 static const struct got_error *
2249 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2251 struct diff_cb_arg *a = arg;
2252 const struct got_error *err;
2253 char *path;
2255 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2256 return got_error(GOT_ERR_CANCELLED);
2258 if (got_object_tree_entry_is_submodule(te))
2259 return NULL;
2261 if (asprintf(&path, "%s%s%s", parent_path,
2262 parent_path[0] ? "/" : "", te->name)
2263 == -1)
2264 return got_error_from_errno("asprintf");
2266 if (S_ISDIR(te->mode))
2267 err = add_dir_on_disk(a->worktree, path);
2268 else
2269 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2270 a->repo, a->progress_cb, a->progress_arg);
2272 free(path);
2273 return err;
2276 const struct got_error *
2277 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2279 uint32_t uuid_status;
2281 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2282 if (uuid_status != uuid_s_ok) {
2283 *uuidstr = NULL;
2284 return got_error_uuid(uuid_status, "uuid_to_string");
2287 return NULL;
2290 static const struct got_error *
2291 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2293 const struct got_error *err = NULL;
2294 char *uuidstr = NULL;
2296 *refname = NULL;
2298 err = got_worktree_get_uuid(&uuidstr, worktree);
2299 if (err)
2300 return err;
2302 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2303 err = got_error_from_errno("asprintf");
2304 *refname = NULL;
2306 free(uuidstr);
2307 return err;
2310 const struct got_error *
2311 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2313 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2316 static const struct got_error *
2317 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2319 return get_ref_name(refname, worktree,
2320 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2323 static const struct got_error *
2324 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2326 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2329 static const struct got_error *
2330 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2332 return get_ref_name(refname, worktree,
2333 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2336 static const struct got_error *
2337 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2339 return get_ref_name(refname, worktree,
2340 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2343 static const struct got_error *
2344 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2346 return get_ref_name(refname, worktree,
2347 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2350 static const struct got_error *
2351 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2353 return get_ref_name(refname, worktree,
2354 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2357 static const struct got_error *
2358 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2360 return get_ref_name(refname, worktree,
2361 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2364 static const struct got_error *
2365 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2367 return get_ref_name(refname, worktree,
2368 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2371 const struct got_error *
2372 got_worktree_get_histedit_script_path(char **path,
2373 struct got_worktree *worktree)
2375 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2376 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2377 *path = NULL;
2378 return got_error_from_errno("asprintf");
2380 return NULL;
2384 * Prevent Git's garbage collector from deleting our base commit by
2385 * setting a reference to our base commit's ID.
2387 static const struct got_error *
2388 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2390 const struct got_error *err = NULL;
2391 struct got_reference *ref = NULL;
2392 char *refname;
2394 err = got_worktree_get_base_ref_name(&refname, worktree);
2395 if (err)
2396 return err;
2398 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2399 if (err)
2400 goto done;
2402 err = got_ref_write(ref, repo);
2403 done:
2404 free(refname);
2405 if (ref)
2406 got_ref_close(ref);
2407 return err;
2410 static const struct got_error *
2411 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2413 const struct got_error *err = NULL;
2415 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2416 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2417 err = got_error_from_errno("asprintf");
2418 *fileindex_path = NULL;
2420 return err;
2424 static const struct got_error *
2425 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2426 struct got_worktree *worktree)
2428 const struct got_error *err = NULL;
2429 FILE *index = NULL;
2431 *fileindex_path = NULL;
2432 *fileindex = got_fileindex_alloc();
2433 if (*fileindex == NULL)
2434 return got_error_from_errno("got_fileindex_alloc");
2436 err = get_fileindex_path(fileindex_path, worktree);
2437 if (err)
2438 goto done;
2440 index = fopen(*fileindex_path, "rb");
2441 if (index == NULL) {
2442 if (errno != ENOENT)
2443 err = got_error_from_errno2("fopen", *fileindex_path);
2444 } else {
2445 err = got_fileindex_read(*fileindex, index);
2446 if (fclose(index) == EOF && err == NULL)
2447 err = got_error_from_errno("fclose");
2449 done:
2450 if (err) {
2451 free(*fileindex_path);
2452 *fileindex_path = NULL;
2453 got_fileindex_free(*fileindex);
2454 *fileindex = NULL;
2456 return err;
2459 struct bump_base_commit_id_arg {
2460 struct got_object_id *base_commit_id;
2461 const char *path;
2462 size_t path_len;
2463 const char *entry_name;
2464 got_worktree_checkout_cb progress_cb;
2465 void *progress_arg;
2468 /* Bump base commit ID of all files within an updated part of the work tree. */
2469 static const struct got_error *
2470 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2472 const struct got_error *err;
2473 struct bump_base_commit_id_arg *a = arg;
2475 if (a->entry_name) {
2476 if (strcmp(ie->path, a->path) != 0)
2477 return NULL;
2478 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2479 return NULL;
2481 if (got_fileindex_entry_was_skipped(ie))
2482 return NULL;
2484 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2485 SHA1_DIGEST_LENGTH) == 0)
2486 return NULL;
2488 if (a->progress_cb) {
2489 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2490 ie->path);
2491 if (err)
2492 return err;
2494 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2495 return NULL;
2498 static const struct got_error *
2499 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2500 struct got_fileindex *fileindex,
2501 got_worktree_checkout_cb progress_cb, void *progress_arg)
2503 struct bump_base_commit_id_arg bbc_arg;
2505 bbc_arg.base_commit_id = worktree->base_commit_id;
2506 bbc_arg.entry_name = NULL;
2507 bbc_arg.path = "";
2508 bbc_arg.path_len = 0;
2509 bbc_arg.progress_cb = progress_cb;
2510 bbc_arg.progress_arg = progress_arg;
2512 return got_fileindex_for_each_entry_safe(fileindex,
2513 bump_base_commit_id, &bbc_arg);
2516 static const struct got_error *
2517 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2519 const struct got_error *err = NULL;
2520 char *new_fileindex_path = NULL;
2521 FILE *new_index = NULL;
2522 struct timespec timeout;
2524 err = got_opentemp_named(&new_fileindex_path, &new_index,
2525 fileindex_path);
2526 if (err)
2527 goto done;
2529 err = got_fileindex_write(fileindex, new_index);
2530 if (err)
2531 goto done;
2533 if (rename(new_fileindex_path, fileindex_path) != 0) {
2534 err = got_error_from_errno3("rename", new_fileindex_path,
2535 fileindex_path);
2536 unlink(new_fileindex_path);
2540 * Sleep for a short amount of time to ensure that files modified after
2541 * this program exits have a different time stamp from the one which
2542 * was recorded in the file index.
2544 timeout.tv_sec = 0;
2545 timeout.tv_nsec = 1;
2546 nanosleep(&timeout, NULL);
2547 done:
2548 if (new_index)
2549 fclose(new_index);
2550 free(new_fileindex_path);
2551 return err;
2554 static const struct got_error *
2555 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2556 struct got_object_id **tree_id, const char *wt_relpath,
2557 struct got_worktree *worktree, struct got_repository *repo)
2559 const struct got_error *err = NULL;
2560 struct got_object_id *id = NULL;
2561 char *in_repo_path = NULL;
2562 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2564 *entry_type = GOT_OBJ_TYPE_ANY;
2565 *tree_relpath = NULL;
2566 *tree_id = NULL;
2568 if (wt_relpath[0] == '\0') {
2569 /* Check out all files within the work tree. */
2570 *entry_type = GOT_OBJ_TYPE_TREE;
2571 *tree_relpath = strdup("");
2572 if (*tree_relpath == NULL) {
2573 err = got_error_from_errno("strdup");
2574 goto done;
2576 err = got_object_id_by_path(tree_id, repo,
2577 worktree->base_commit_id, worktree->path_prefix);
2578 if (err)
2579 goto done;
2580 return NULL;
2583 /* Check out a subset of files in the work tree. */
2585 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2586 is_root_wt ? "" : "/", wt_relpath) == -1) {
2587 err = got_error_from_errno("asprintf");
2588 goto done;
2591 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2592 in_repo_path);
2593 if (err)
2594 goto done;
2596 free(in_repo_path);
2597 in_repo_path = NULL;
2599 err = got_object_get_type(entry_type, repo, id);
2600 if (err)
2601 goto done;
2603 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2604 /* Check out a single file. */
2605 if (strchr(wt_relpath, '/') == NULL) {
2606 /* Check out a single file in work tree's root dir. */
2607 in_repo_path = strdup(worktree->path_prefix);
2608 if (in_repo_path == NULL) {
2609 err = got_error_from_errno("strdup");
2610 goto done;
2612 *tree_relpath = strdup("");
2613 if (*tree_relpath == NULL) {
2614 err = got_error_from_errno("strdup");
2615 goto done;
2617 } else {
2618 /* Check out a single file in a subdirectory. */
2619 err = got_path_dirname(tree_relpath, wt_relpath);
2620 if (err)
2621 return err;
2622 if (asprintf(&in_repo_path, "%s%s%s",
2623 worktree->path_prefix, is_root_wt ? "" : "/",
2624 *tree_relpath) == -1) {
2625 err = got_error_from_errno("asprintf");
2626 goto done;
2629 err = got_object_id_by_path(tree_id, repo,
2630 worktree->base_commit_id, in_repo_path);
2631 } else {
2632 /* Check out all files within a subdirectory. */
2633 *tree_id = got_object_id_dup(id);
2634 if (*tree_id == NULL) {
2635 err = got_error_from_errno("got_object_id_dup");
2636 goto done;
2638 *tree_relpath = strdup(wt_relpath);
2639 if (*tree_relpath == NULL) {
2640 err = got_error_from_errno("strdup");
2641 goto done;
2644 done:
2645 free(id);
2646 free(in_repo_path);
2647 if (err) {
2648 *entry_type = GOT_OBJ_TYPE_ANY;
2649 free(*tree_relpath);
2650 *tree_relpath = NULL;
2651 free(*tree_id);
2652 *tree_id = NULL;
2654 return err;
2657 static const struct got_error *
2658 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2659 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2660 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2661 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2663 const struct got_error *err = NULL;
2664 struct got_commit_object *commit = NULL;
2665 struct got_tree_object *tree = NULL;
2666 struct got_fileindex_diff_tree_cb diff_cb;
2667 struct diff_cb_arg arg;
2669 err = ref_base_commit(worktree, repo);
2670 if (err) {
2671 if (!(err->code == GOT_ERR_ERRNO &&
2672 (errno == EACCES || errno == EROFS)))
2673 goto done;
2674 err = (*progress_cb)(progress_arg,
2675 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2676 if (err)
2677 return err;
2680 err = got_object_open_as_commit(&commit, repo,
2681 worktree->base_commit_id);
2682 if (err)
2683 goto done;
2685 err = got_object_open_as_tree(&tree, repo, tree_id);
2686 if (err)
2687 goto done;
2689 if (entry_name &&
2690 got_object_tree_find_entry(tree, entry_name) == NULL) {
2691 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2692 goto done;
2695 diff_cb.diff_old_new = diff_old_new;
2696 diff_cb.diff_old = diff_old;
2697 diff_cb.diff_new = diff_new;
2698 arg.fileindex = fileindex;
2699 arg.worktree = worktree;
2700 arg.repo = repo;
2701 arg.progress_cb = progress_cb;
2702 arg.progress_arg = progress_arg;
2703 arg.cancel_cb = cancel_cb;
2704 arg.cancel_arg = cancel_arg;
2705 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2706 entry_name, repo, &diff_cb, &arg);
2707 done:
2708 if (tree)
2709 got_object_tree_close(tree);
2710 if (commit)
2711 got_object_commit_close(commit);
2712 return err;
2715 const struct got_error *
2716 got_worktree_checkout_files(struct got_worktree *worktree,
2717 struct got_pathlist_head *paths, struct got_repository *repo,
2718 got_worktree_checkout_cb progress_cb, void *progress_arg,
2719 got_cancel_cb cancel_cb, void *cancel_arg)
2721 const struct got_error *err = NULL, *sync_err, *unlockerr;
2722 struct got_commit_object *commit = NULL;
2723 struct got_tree_object *tree = NULL;
2724 struct got_fileindex *fileindex = NULL;
2725 char *fileindex_path = NULL;
2726 struct got_pathlist_entry *pe;
2727 struct tree_path_data {
2728 STAILQ_ENTRY(tree_path_data) entry;
2729 struct got_object_id *tree_id;
2730 int entry_type;
2731 char *relpath;
2732 char *entry_name;
2733 } *tpd = NULL;
2734 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2736 STAILQ_INIT(&tree_paths);
2738 err = lock_worktree(worktree, LOCK_EX);
2739 if (err)
2740 return err;
2742 /* Map all specified paths to in-repository trees. */
2743 TAILQ_FOREACH(pe, paths, entry) {
2744 tpd = malloc(sizeof(*tpd));
2745 if (tpd == NULL) {
2746 err = got_error_from_errno("malloc");
2747 goto done;
2750 err = find_tree_entry_for_checkout(&tpd->entry_type,
2751 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2752 if (err) {
2753 free(tpd);
2754 goto done;
2757 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2758 err = got_path_basename(&tpd->entry_name, pe->path);
2759 if (err) {
2760 free(tpd->relpath);
2761 free(tpd->tree_id);
2762 free(tpd);
2763 goto done;
2765 } else
2766 tpd->entry_name = NULL;
2768 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2772 * Read the file index.
2773 * Checking out files is supposed to be an idempotent operation.
2774 * If the on-disk file index is incomplete we will try to complete it.
2776 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2777 if (err)
2778 goto done;
2780 tpd = STAILQ_FIRST(&tree_paths);
2781 TAILQ_FOREACH(pe, paths, entry) {
2782 struct bump_base_commit_id_arg bbc_arg;
2784 err = checkout_files(worktree, fileindex, tpd->relpath,
2785 tpd->tree_id, tpd->entry_name, repo,
2786 progress_cb, progress_arg, cancel_cb, cancel_arg);
2787 if (err)
2788 break;
2790 bbc_arg.base_commit_id = worktree->base_commit_id;
2791 bbc_arg.entry_name = tpd->entry_name;
2792 bbc_arg.path = pe->path;
2793 bbc_arg.path_len = pe->path_len;
2794 bbc_arg.progress_cb = progress_cb;
2795 bbc_arg.progress_arg = progress_arg;
2796 err = got_fileindex_for_each_entry_safe(fileindex,
2797 bump_base_commit_id, &bbc_arg);
2798 if (err)
2799 break;
2801 tpd = STAILQ_NEXT(tpd, entry);
2803 sync_err = sync_fileindex(fileindex, fileindex_path);
2804 if (sync_err && err == NULL)
2805 err = sync_err;
2806 done:
2807 free(fileindex_path);
2808 if (tree)
2809 got_object_tree_close(tree);
2810 if (commit)
2811 got_object_commit_close(commit);
2812 if (fileindex)
2813 got_fileindex_free(fileindex);
2814 while (!STAILQ_EMPTY(&tree_paths)) {
2815 tpd = STAILQ_FIRST(&tree_paths);
2816 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2817 free(tpd->relpath);
2818 free(tpd->tree_id);
2819 free(tpd);
2821 unlockerr = lock_worktree(worktree, LOCK_SH);
2822 if (unlockerr && err == NULL)
2823 err = unlockerr;
2824 return err;
2827 struct merge_file_cb_arg {
2828 struct got_worktree *worktree;
2829 struct got_fileindex *fileindex;
2830 got_worktree_checkout_cb progress_cb;
2831 void *progress_arg;
2832 got_cancel_cb cancel_cb;
2833 void *cancel_arg;
2834 const char *label_orig;
2835 struct got_object_id *commit_id2;
2838 static const struct got_error *
2839 merge_file_cb(void *arg, struct got_blob_object *blob1,
2840 struct got_blob_object *blob2, struct got_object_id *id1,
2841 struct got_object_id *id2, const char *path1, const char *path2,
2842 mode_t mode1, mode_t mode2, struct got_repository *repo)
2844 static const struct got_error *err = NULL;
2845 struct merge_file_cb_arg *a = arg;
2846 struct got_fileindex_entry *ie;
2847 char *ondisk_path = NULL;
2848 struct stat sb;
2849 unsigned char status;
2850 int local_changes_subsumed;
2851 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2852 char *id_str = NULL, *label_deriv2 = NULL;
2854 if (blob1 && blob2) {
2855 ie = got_fileindex_entry_get(a->fileindex, path2,
2856 strlen(path2));
2857 if (ie == NULL)
2858 return (*a->progress_cb)(a->progress_arg,
2859 GOT_STATUS_MISSING, path2);
2861 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2862 path2) == -1)
2863 return got_error_from_errno("asprintf");
2865 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2866 repo);
2867 if (err)
2868 goto done;
2870 if (status == GOT_STATUS_DELETE) {
2871 err = (*a->progress_cb)(a->progress_arg,
2872 GOT_STATUS_MERGE, path2);
2873 goto done;
2875 if (status != GOT_STATUS_NO_CHANGE &&
2876 status != GOT_STATUS_MODIFY &&
2877 status != GOT_STATUS_CONFLICT &&
2878 status != GOT_STATUS_ADD) {
2879 err = (*a->progress_cb)(a->progress_arg, status, path2);
2880 goto done;
2883 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2884 char *link_target2;
2885 err = got_object_blob_read_to_str(&link_target2, blob2);
2886 if (err)
2887 goto done;
2888 err = merge_symlink(a->worktree, blob1, ondisk_path,
2889 path2, a->label_orig, link_target2, a->commit_id2,
2890 repo, a->progress_cb, a->progress_arg);
2891 free(link_target2);
2892 } else {
2893 int fd;
2895 f_orig = got_opentemp();
2896 if (f_orig == NULL) {
2897 err = got_error_from_errno("got_opentemp");
2898 goto done;
2900 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2901 f_orig, blob1);
2902 if (err)
2903 goto done;
2905 f_deriv2 = got_opentemp();
2906 if (f_deriv2 == NULL)
2907 goto done;
2908 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2909 f_deriv2, blob2);
2910 if (err)
2911 goto done;
2913 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2914 if (fd == -1) {
2915 err = got_error_from_errno2("open",
2916 ondisk_path);
2917 goto done;
2919 f_deriv = fdopen(fd, "r");
2920 if (f_deriv == NULL) {
2921 err = got_error_from_errno2("fdopen",
2922 ondisk_path);
2923 close(fd);
2924 goto done;
2926 err = got_object_id_str(&id_str, a->commit_id2);
2927 if (err)
2928 goto done;
2929 if (asprintf(&label_deriv2, "%s: commit %s",
2930 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2931 err = got_error_from_errno("asprintf");
2932 goto done;
2934 err = merge_file(&local_changes_subsumed, a->worktree,
2935 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2936 sb.st_mode, a->label_orig, NULL, label_deriv2,
2937 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2938 a->progress_cb, a->progress_arg);
2940 } else if (blob1) {
2941 ie = got_fileindex_entry_get(a->fileindex, path1,
2942 strlen(path1));
2943 if (ie == NULL)
2944 return (*a->progress_cb)(a->progress_arg,
2945 GOT_STATUS_MISSING, path1);
2947 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2948 path1) == -1)
2949 return got_error_from_errno("asprintf");
2951 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2952 repo);
2953 if (err)
2954 goto done;
2956 switch (status) {
2957 case GOT_STATUS_NO_CHANGE:
2958 err = (*a->progress_cb)(a->progress_arg,
2959 GOT_STATUS_DELETE, path1);
2960 if (err)
2961 goto done;
2962 err = remove_ondisk_file(a->worktree->root_path, path1);
2963 if (err)
2964 goto done;
2965 if (ie)
2966 got_fileindex_entry_mark_deleted_from_disk(ie);
2967 break;
2968 case GOT_STATUS_DELETE:
2969 case GOT_STATUS_MISSING:
2970 err = (*a->progress_cb)(a->progress_arg,
2971 GOT_STATUS_DELETE, path1);
2972 if (err)
2973 goto done;
2974 if (ie)
2975 got_fileindex_entry_mark_deleted_from_disk(ie);
2976 break;
2977 case GOT_STATUS_ADD: {
2978 struct got_object_id *id;
2979 FILE *blob1_f;
2981 * Delete the added file only if its content already
2982 * exists in the repository.
2984 err = got_object_blob_file_create(&id, &blob1_f, path1);
2985 if (err)
2986 goto done;
2987 if (got_object_id_cmp(id, id1) == 0) {
2988 err = (*a->progress_cb)(a->progress_arg,
2989 GOT_STATUS_DELETE, path1);
2990 if (err)
2991 goto done;
2992 err = remove_ondisk_file(a->worktree->root_path,
2993 path1);
2994 if (err)
2995 goto done;
2996 if (ie)
2997 got_fileindex_entry_remove(a->fileindex,
2998 ie);
2999 } else {
3000 err = (*a->progress_cb)(a->progress_arg,
3001 GOT_STATUS_CANNOT_DELETE, path1);
3003 if (fclose(blob1_f) == EOF && err == NULL)
3004 err = got_error_from_errno("fclose");
3005 free(id);
3006 if (err)
3007 goto done;
3008 break;
3010 case GOT_STATUS_MODIFY:
3011 case GOT_STATUS_CONFLICT:
3012 err = (*a->progress_cb)(a->progress_arg,
3013 GOT_STATUS_CANNOT_DELETE, path1);
3014 if (err)
3015 goto done;
3016 break;
3017 case GOT_STATUS_OBSTRUCTED:
3018 err = (*a->progress_cb)(a->progress_arg, status, path1);
3019 if (err)
3020 goto done;
3021 break;
3022 default:
3023 break;
3025 } else if (blob2) {
3026 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3027 path2) == -1)
3028 return got_error_from_errno("asprintf");
3029 ie = got_fileindex_entry_get(a->fileindex, path2,
3030 strlen(path2));
3031 if (ie) {
3032 err = get_file_status(&status, &sb, ie, ondisk_path,
3033 -1, NULL, repo);
3034 if (err)
3035 goto done;
3036 if (status != GOT_STATUS_NO_CHANGE &&
3037 status != GOT_STATUS_MODIFY &&
3038 status != GOT_STATUS_CONFLICT &&
3039 status != GOT_STATUS_ADD) {
3040 err = (*a->progress_cb)(a->progress_arg,
3041 status, path2);
3042 goto done;
3044 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3045 char *link_target2;
3046 err = got_object_blob_read_to_str(&link_target2,
3047 blob2);
3048 if (err)
3049 goto done;
3050 err = merge_symlink(a->worktree, NULL,
3051 ondisk_path, path2, a->label_orig,
3052 link_target2, a->commit_id2, repo,
3053 a->progress_cb, a->progress_arg);
3054 free(link_target2);
3055 } else if (S_ISREG(sb.st_mode)) {
3056 err = merge_blob(&local_changes_subsumed,
3057 a->worktree, NULL, ondisk_path, path2,
3058 sb.st_mode, a->label_orig, blob2,
3059 a->commit_id2, repo, a->progress_cb,
3060 a->progress_arg);
3061 } else {
3062 err = got_error_path(ondisk_path,
3063 GOT_ERR_FILE_OBSTRUCTED);
3065 if (err)
3066 goto done;
3067 if (status == GOT_STATUS_DELETE) {
3068 err = got_fileindex_entry_update(ie,
3069 a->worktree->root_fd, path2, blob2->id.sha1,
3070 a->worktree->base_commit_id->sha1, 0);
3071 if (err)
3072 goto done;
3074 } else {
3075 int is_bad_symlink = 0;
3076 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3077 if (S_ISLNK(mode2)) {
3078 err = install_symlink(&is_bad_symlink,
3079 a->worktree, ondisk_path, path2, blob2, 0,
3080 0, 1, repo, a->progress_cb, a->progress_arg);
3081 } else {
3082 err = install_blob(a->worktree, ondisk_path, path2,
3083 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3084 a->progress_cb, a->progress_arg);
3086 if (err)
3087 goto done;
3088 err = got_fileindex_entry_alloc(&ie, path2);
3089 if (err)
3090 goto done;
3091 err = got_fileindex_entry_update(ie,
3092 a->worktree->root_fd, path2, NULL, NULL, 1);
3093 if (err) {
3094 got_fileindex_entry_free(ie);
3095 goto done;
3097 err = got_fileindex_entry_add(a->fileindex, ie);
3098 if (err) {
3099 got_fileindex_entry_free(ie);
3100 goto done;
3102 if (is_bad_symlink) {
3103 got_fileindex_entry_filetype_set(ie,
3104 GOT_FILEIDX_MODE_BAD_SYMLINK);
3108 done:
3109 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3110 err = got_error_from_errno("fclose");
3111 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3112 err = got_error_from_errno("fclose");
3113 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3114 err = got_error_from_errno("fclose");
3115 free(id_str);
3116 free(label_deriv2);
3117 free(ondisk_path);
3118 return err;
3121 static const struct got_error *
3122 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3124 struct got_worktree *worktree = arg;
3126 /* Reject merges into a work tree with mixed base commits. */
3127 if (got_fileindex_entry_has_commit(ie) &&
3128 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3129 SHA1_DIGEST_LENGTH) != 0)
3130 return got_error(GOT_ERR_MIXED_COMMITS);
3132 return NULL;
3135 struct check_merge_conflicts_arg {
3136 struct got_worktree *worktree;
3137 struct got_fileindex *fileindex;
3138 struct got_repository *repo;
3141 static const struct got_error *
3142 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3143 struct got_blob_object *blob2, struct got_object_id *id1,
3144 struct got_object_id *id2, const char *path1, const char *path2,
3145 mode_t mode1, mode_t mode2, struct got_repository *repo)
3147 const struct got_error *err = NULL;
3148 struct check_merge_conflicts_arg *a = arg;
3149 unsigned char status;
3150 struct stat sb;
3151 struct got_fileindex_entry *ie;
3152 const char *path = path2 ? path2 : path1;
3153 struct got_object_id *id = id2 ? id2 : id1;
3154 char *ondisk_path;
3156 if (id == NULL)
3157 return NULL;
3159 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3160 if (ie == NULL)
3161 return NULL;
3163 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3164 == -1)
3165 return got_error_from_errno("asprintf");
3167 /* Reject merges into a work tree with conflicted files. */
3168 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3169 free(ondisk_path);
3170 if (err)
3171 return err;
3172 if (status == GOT_STATUS_CONFLICT)
3173 return got_error(GOT_ERR_CONFLICTS);
3175 return NULL;
3178 static const struct got_error *
3179 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3180 const char *fileindex_path, struct got_object_id *commit_id1,
3181 struct got_object_id *commit_id2, struct got_repository *repo,
3182 got_worktree_checkout_cb progress_cb, void *progress_arg,
3183 got_cancel_cb cancel_cb, void *cancel_arg)
3185 const struct got_error *err = NULL, *sync_err;
3186 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3187 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3188 struct check_merge_conflicts_arg cmc_arg;
3189 struct merge_file_cb_arg arg;
3190 char *label_orig = NULL;
3192 if (commit_id1) {
3193 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3194 worktree->path_prefix);
3195 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3196 goto done;
3198 if (tree_id1) {
3199 char *id_str;
3201 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3202 if (err)
3203 goto done;
3205 err = got_object_id_str(&id_str, commit_id1);
3206 if (err)
3207 goto done;
3209 if (asprintf(&label_orig, "%s: commit %s",
3210 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3211 err = got_error_from_errno("asprintf");
3212 free(id_str);
3213 goto done;
3215 free(id_str);
3218 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3219 worktree->path_prefix);
3220 if (err)
3221 goto done;
3223 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3224 if (err)
3225 goto done;
3227 cmc_arg.worktree = worktree;
3228 cmc_arg.fileindex = fileindex;
3229 cmc_arg.repo = repo;
3230 err = got_diff_tree(tree1, tree2, "", "", repo,
3231 check_merge_conflicts, &cmc_arg, 0);
3232 if (err)
3233 goto done;
3235 arg.worktree = worktree;
3236 arg.fileindex = fileindex;
3237 arg.progress_cb = progress_cb;
3238 arg.progress_arg = progress_arg;
3239 arg.cancel_cb = cancel_cb;
3240 arg.cancel_arg = cancel_arg;
3241 arg.label_orig = label_orig;
3242 arg.commit_id2 = commit_id2;
3243 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3244 sync_err = sync_fileindex(fileindex, fileindex_path);
3245 if (sync_err && err == NULL)
3246 err = sync_err;
3247 done:
3248 if (tree1)
3249 got_object_tree_close(tree1);
3250 if (tree2)
3251 got_object_tree_close(tree2);
3252 free(label_orig);
3253 return err;
3256 const struct got_error *
3257 got_worktree_merge_files(struct got_worktree *worktree,
3258 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3259 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3260 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3262 const struct got_error *err, *unlockerr;
3263 char *fileindex_path = NULL;
3264 struct got_fileindex *fileindex = NULL;
3266 err = lock_worktree(worktree, LOCK_EX);
3267 if (err)
3268 return err;
3270 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3271 if (err)
3272 goto done;
3274 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3275 worktree);
3276 if (err)
3277 goto done;
3279 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3280 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3281 done:
3282 if (fileindex)
3283 got_fileindex_free(fileindex);
3284 free(fileindex_path);
3285 unlockerr = lock_worktree(worktree, LOCK_SH);
3286 if (unlockerr && err == NULL)
3287 err = unlockerr;
3288 return err;
3291 struct diff_dir_cb_arg {
3292 struct got_fileindex *fileindex;
3293 struct got_worktree *worktree;
3294 const char *status_path;
3295 size_t status_path_len;
3296 struct got_repository *repo;
3297 got_worktree_status_cb status_cb;
3298 void *status_arg;
3299 got_cancel_cb cancel_cb;
3300 void *cancel_arg;
3301 /* A pathlist containing per-directory pathlists of ignore patterns. */
3302 struct got_pathlist_head *ignores;
3303 int report_unchanged;
3304 int no_ignores;
3307 static const struct got_error *
3308 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3309 int dirfd, const char *de_name,
3310 got_worktree_status_cb status_cb, void *status_arg,
3311 struct got_repository *repo, int report_unchanged)
3313 const struct got_error *err = NULL;
3314 unsigned char status = GOT_STATUS_NO_CHANGE;
3315 unsigned char staged_status = get_staged_status(ie);
3316 struct stat sb;
3317 struct got_object_id blob_id, commit_id, staged_blob_id;
3318 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3319 struct got_object_id *staged_blob_idp = NULL;
3321 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3322 if (err)
3323 return err;
3325 if (status == GOT_STATUS_NO_CHANGE &&
3326 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3327 return NULL;
3329 if (got_fileindex_entry_has_blob(ie)) {
3330 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3331 blob_idp = &blob_id;
3333 if (got_fileindex_entry_has_commit(ie)) {
3334 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3335 commit_idp = &commit_id;
3337 if (staged_status == GOT_STATUS_ADD ||
3338 staged_status == GOT_STATUS_MODIFY) {
3339 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3340 SHA1_DIGEST_LENGTH);
3341 staged_blob_idp = &staged_blob_id;
3344 return (*status_cb)(status_arg, status, staged_status,
3345 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3348 static const struct got_error *
3349 status_old_new(void *arg, struct got_fileindex_entry *ie,
3350 struct dirent *de, const char *parent_path, int dirfd)
3352 const struct got_error *err = NULL;
3353 struct diff_dir_cb_arg *a = arg;
3354 char *abspath;
3356 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3357 return got_error(GOT_ERR_CANCELLED);
3359 if (got_path_cmp(parent_path, a->status_path,
3360 strlen(parent_path), a->status_path_len) != 0 &&
3361 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3362 return NULL;
3364 if (parent_path[0]) {
3365 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3366 parent_path, de->d_name) == -1)
3367 return got_error_from_errno("asprintf");
3368 } else {
3369 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3370 de->d_name) == -1)
3371 return got_error_from_errno("asprintf");
3374 err = report_file_status(ie, abspath, dirfd, de->d_name,
3375 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3376 free(abspath);
3377 return err;
3380 static const struct got_error *
3381 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3383 struct diff_dir_cb_arg *a = arg;
3384 struct got_object_id blob_id, commit_id;
3385 unsigned char status;
3387 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3388 return got_error(GOT_ERR_CANCELLED);
3390 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3391 return NULL;
3393 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3394 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3395 if (got_fileindex_entry_has_file_on_disk(ie))
3396 status = GOT_STATUS_MISSING;
3397 else
3398 status = GOT_STATUS_DELETE;
3399 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3400 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3403 void
3404 free_ignorelist(struct got_pathlist_head *ignorelist)
3406 struct got_pathlist_entry *pe;
3408 TAILQ_FOREACH(pe, ignorelist, entry)
3409 free((char *)pe->path);
3410 got_pathlist_free(ignorelist);
3413 void
3414 free_ignores(struct got_pathlist_head *ignores)
3416 struct got_pathlist_entry *pe;
3418 TAILQ_FOREACH(pe, ignores, entry) {
3419 struct got_pathlist_head *ignorelist = pe->data;
3420 free_ignorelist(ignorelist);
3421 free((char *)pe->path);
3423 got_pathlist_free(ignores);
3426 static const struct got_error *
3427 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3429 const struct got_error *err = NULL;
3430 struct got_pathlist_entry *pe = NULL;
3431 struct got_pathlist_head *ignorelist;
3432 char *line = NULL, *pattern, *dirpath = NULL;
3433 size_t linesize = 0;
3434 ssize_t linelen;
3436 ignorelist = calloc(1, sizeof(*ignorelist));
3437 if (ignorelist == NULL)
3438 return got_error_from_errno("calloc");
3439 TAILQ_INIT(ignorelist);
3441 while ((linelen = getline(&line, &linesize, f)) != -1) {
3442 if (linelen > 0 && line[linelen - 1] == '\n')
3443 line[linelen - 1] = '\0';
3445 /* Git's ignores may contain comments. */
3446 if (line[0] == '#')
3447 continue;
3449 /* Git's negated patterns are not (yet?) supported. */
3450 if (line[0] == '!')
3451 continue;
3453 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3454 line) == -1) {
3455 err = got_error_from_errno("asprintf");
3456 goto done;
3458 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3459 if (err)
3460 goto done;
3462 if (ferror(f)) {
3463 err = got_error_from_errno("getline");
3464 goto done;
3467 dirpath = strdup(path);
3468 if (dirpath == NULL) {
3469 err = got_error_from_errno("strdup");
3470 goto done;
3472 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3473 done:
3474 free(line);
3475 if (err || pe == NULL) {
3476 free(dirpath);
3477 free_ignorelist(ignorelist);
3479 return err;
3482 int
3483 match_ignores(struct got_pathlist_head *ignores, const char *path)
3485 struct got_pathlist_entry *pe;
3487 /* Handle patterns which match in all directories. */
3488 TAILQ_FOREACH(pe, ignores, entry) {
3489 struct got_pathlist_head *ignorelist = pe->data;
3490 struct got_pathlist_entry *pi;
3492 TAILQ_FOREACH(pi, ignorelist, entry) {
3493 const char *p, *pattern = pi->path;
3495 if (strncmp(pattern, "**/", 3) != 0)
3496 continue;
3497 pattern += 3;
3498 p = path;
3499 while (*p) {
3500 if (fnmatch(pattern, p,
3501 FNM_PATHNAME | FNM_LEADING_DIR)) {
3502 /* Retry in next directory. */
3503 while (*p && *p != '/')
3504 p++;
3505 while (*p == '/')
3506 p++;
3507 continue;
3509 return 1;
3515 * The ignores pathlist contains ignore lists from children before
3516 * parents, so we can find the most specific ignorelist by walking
3517 * ignores backwards.
3519 pe = TAILQ_LAST(ignores, got_pathlist_head);
3520 while (pe) {
3521 if (got_path_is_child(path, pe->path, pe->path_len)) {
3522 struct got_pathlist_head *ignorelist = pe->data;
3523 struct got_pathlist_entry *pi;
3524 TAILQ_FOREACH(pi, ignorelist, entry) {
3525 const char *pattern = pi->path;
3526 int flags = FNM_LEADING_DIR;
3527 if (strstr(pattern, "/**/") == NULL)
3528 flags |= FNM_PATHNAME;
3529 if (fnmatch(pattern, path, flags))
3530 continue;
3531 return 1;
3534 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3537 return 0;
3540 static const struct got_error *
3541 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3542 const char *path, int dirfd, const char *ignores_filename)
3544 const struct got_error *err = NULL;
3545 char *ignorespath;
3546 int fd = -1;
3547 FILE *ignoresfile = NULL;
3549 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3550 path[0] ? "/" : "", ignores_filename) == -1)
3551 return got_error_from_errno("asprintf");
3553 if (dirfd != -1) {
3554 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3555 if (fd == -1) {
3556 if (errno != ENOENT && errno != EACCES)
3557 err = got_error_from_errno2("openat",
3558 ignorespath);
3559 } else {
3560 ignoresfile = fdopen(fd, "r");
3561 if (ignoresfile == NULL)
3562 err = got_error_from_errno2("fdopen",
3563 ignorespath);
3564 else {
3565 fd = -1;
3566 err = read_ignores(ignores, path, ignoresfile);
3569 } else {
3570 ignoresfile = fopen(ignorespath, "r");
3571 if (ignoresfile == NULL) {
3572 if (errno != ENOENT && errno != EACCES)
3573 err = got_error_from_errno2("fopen",
3574 ignorespath);
3575 } else
3576 err = read_ignores(ignores, path, ignoresfile);
3579 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3580 err = got_error_from_errno2("fclose", path);
3581 if (fd != -1 && close(fd) == -1 && err == NULL)
3582 err = got_error_from_errno2("close", path);
3583 free(ignorespath);
3584 return err;
3587 static const struct got_error *
3588 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3590 const struct got_error *err = NULL;
3591 struct diff_dir_cb_arg *a = arg;
3592 char *path = NULL;
3594 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3595 return got_error(GOT_ERR_CANCELLED);
3597 if (parent_path[0]) {
3598 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3599 return got_error_from_errno("asprintf");
3600 } else {
3601 path = de->d_name;
3604 if (de->d_type != DT_DIR &&
3605 got_path_is_child(path, a->status_path, a->status_path_len)
3606 && !match_ignores(a->ignores, path))
3607 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3608 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3609 if (parent_path[0])
3610 free(path);
3611 return err;
3614 static const struct got_error *
3615 status_traverse(void *arg, const char *path, int dirfd)
3617 const struct got_error *err = NULL;
3618 struct diff_dir_cb_arg *a = arg;
3620 if (a->no_ignores)
3621 return NULL;
3623 err = add_ignores(a->ignores, a->worktree->root_path,
3624 path, dirfd, ".cvsignore");
3625 if (err)
3626 return err;
3628 err = add_ignores(a->ignores, a->worktree->root_path, path,
3629 dirfd, ".gitignore");
3631 return err;
3634 static const struct got_error *
3635 report_single_file_status(const char *path, const char *ondisk_path,
3636 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3637 void *status_arg, struct got_repository *repo, int report_unchanged,
3638 struct got_pathlist_head *ignores, int no_ignores)
3640 struct got_fileindex_entry *ie;
3641 struct stat sb;
3643 if (!no_ignores && match_ignores(ignores, path))
3644 return NULL;
3646 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3647 if (ie)
3648 return report_file_status(ie, ondisk_path, -1, NULL,
3649 status_cb, status_arg, repo, report_unchanged);
3651 if (lstat(ondisk_path, &sb) == -1) {
3652 if (errno != ENOENT)
3653 return got_error_from_errno2("lstat", ondisk_path);
3654 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3655 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3656 return NULL;
3659 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3660 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3661 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3663 return NULL;
3666 static const struct got_error *
3667 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3668 const char *root_path, const char *path)
3670 const struct got_error *err;
3671 char *parent_path, *next_parent_path = NULL;
3673 err = add_ignores(ignores, root_path, "", -1,
3674 ".cvsignore");
3675 if (err)
3676 return err;
3678 err = add_ignores(ignores, root_path, "", -1,
3679 ".gitignore");
3680 if (err)
3681 return err;
3683 err = got_path_dirname(&parent_path, path);
3684 if (err) {
3685 if (err->code == GOT_ERR_BAD_PATH)
3686 return NULL; /* cannot traverse parent */
3687 return err;
3689 for (;;) {
3690 err = add_ignores(ignores, root_path, parent_path, -1,
3691 ".cvsignore");
3692 if (err)
3693 break;
3694 err = add_ignores(ignores, root_path, parent_path, -1,
3695 ".gitignore");
3696 if (err)
3697 break;
3698 err = got_path_dirname(&next_parent_path, parent_path);
3699 if (err) {
3700 if (err->code == GOT_ERR_BAD_PATH)
3701 err = NULL; /* traversed everything */
3702 break;
3704 if (got_path_is_root_dir(parent_path))
3705 break;
3706 free(parent_path);
3707 parent_path = next_parent_path;
3708 next_parent_path = NULL;
3711 free(parent_path);
3712 free(next_parent_path);
3713 return err;
3716 static const struct got_error *
3717 worktree_status(struct got_worktree *worktree, const char *path,
3718 struct got_fileindex *fileindex, struct got_repository *repo,
3719 got_worktree_status_cb status_cb, void *status_arg,
3720 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3721 int report_unchanged)
3723 const struct got_error *err = NULL;
3724 int fd = -1;
3725 struct got_fileindex_diff_dir_cb fdiff_cb;
3726 struct diff_dir_cb_arg arg;
3727 char *ondisk_path = NULL;
3728 struct got_pathlist_head ignores;
3730 TAILQ_INIT(&ignores);
3732 if (asprintf(&ondisk_path, "%s%s%s",
3733 worktree->root_path, path[0] ? "/" : "", path) == -1)
3734 return got_error_from_errno("asprintf");
3736 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3737 if (fd == -1) {
3738 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3739 errno != ELOOP)
3740 err = got_error_from_errno2("open", ondisk_path);
3741 else {
3742 if (!no_ignores) {
3743 err = add_ignores_from_parent_paths(&ignores,
3744 worktree->root_path, ondisk_path);
3745 if (err)
3746 goto done;
3748 err = report_single_file_status(path, ondisk_path,
3749 fileindex, status_cb, status_arg, repo,
3750 report_unchanged, &ignores, no_ignores);
3752 } else {
3753 fdiff_cb.diff_old_new = status_old_new;
3754 fdiff_cb.diff_old = status_old;
3755 fdiff_cb.diff_new = status_new;
3756 fdiff_cb.diff_traverse = status_traverse;
3757 arg.fileindex = fileindex;
3758 arg.worktree = worktree;
3759 arg.status_path = path;
3760 arg.status_path_len = strlen(path);
3761 arg.repo = repo;
3762 arg.status_cb = status_cb;
3763 arg.status_arg = status_arg;
3764 arg.cancel_cb = cancel_cb;
3765 arg.cancel_arg = cancel_arg;
3766 arg.report_unchanged = report_unchanged;
3767 arg.no_ignores = no_ignores;
3768 if (!no_ignores) {
3769 err = add_ignores_from_parent_paths(&ignores,
3770 worktree->root_path, path);
3771 if (err)
3772 goto done;
3774 arg.ignores = &ignores;
3775 err = got_fileindex_diff_dir(fileindex, fd,
3776 worktree->root_path, path, repo, &fdiff_cb, &arg);
3778 done:
3779 free_ignores(&ignores);
3780 if (fd != -1 && close(fd) == -1 && err == NULL)
3781 err = got_error_from_errno("close");
3782 free(ondisk_path);
3783 return err;
3786 const struct got_error *
3787 got_worktree_status(struct got_worktree *worktree,
3788 struct got_pathlist_head *paths, struct got_repository *repo,
3789 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3790 got_cancel_cb cancel_cb, void *cancel_arg)
3792 const struct got_error *err = NULL;
3793 char *fileindex_path = NULL;
3794 struct got_fileindex *fileindex = NULL;
3795 struct got_pathlist_entry *pe;
3797 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3798 if (err)
3799 return err;
3801 TAILQ_FOREACH(pe, paths, entry) {
3802 err = worktree_status(worktree, pe->path, fileindex, repo,
3803 status_cb, status_arg, cancel_cb, cancel_arg,
3804 no_ignores, 0);
3805 if (err)
3806 break;
3808 free(fileindex_path);
3809 got_fileindex_free(fileindex);
3810 return err;
3813 const struct got_error *
3814 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3815 const char *arg)
3817 const struct got_error *err = NULL;
3818 char *resolved = NULL, *cwd = NULL, *path = NULL;
3819 size_t len;
3820 struct stat sb;
3821 char *abspath = NULL;
3822 char canonpath[PATH_MAX];
3824 *wt_path = NULL;
3826 cwd = getcwd(NULL, 0);
3827 if (cwd == NULL)
3828 return got_error_from_errno("getcwd");
3830 if (lstat(arg, &sb) == -1) {
3831 if (errno != ENOENT) {
3832 err = got_error_from_errno2("lstat", arg);
3833 goto done;
3835 sb.st_mode = 0;
3837 if (S_ISLNK(sb.st_mode)) {
3839 * We cannot use realpath(3) with symlinks since we want to
3840 * operate on the symlink itself.
3841 * But we can make the path absolute, assuming it is relative
3842 * to the current working directory, and then canonicalize it.
3844 if (!got_path_is_absolute(arg)) {
3845 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3846 err = got_error_from_errno("asprintf");
3847 goto done;
3851 err = got_canonpath(abspath ? abspath : arg, canonpath,
3852 sizeof(canonpath));
3853 if (err)
3854 goto done;
3855 resolved = strdup(canonpath);
3856 if (resolved == NULL) {
3857 err = got_error_from_errno("strdup");
3858 goto done;
3860 } else {
3861 resolved = realpath(arg, NULL);
3862 if (resolved == NULL) {
3863 if (errno != ENOENT) {
3864 err = got_error_from_errno2("realpath", arg);
3865 goto done;
3867 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3868 err = got_error_from_errno("asprintf");
3869 goto done;
3871 err = got_canonpath(abspath, canonpath,
3872 sizeof(canonpath));
3873 if (err)
3874 goto done;
3875 resolved = strdup(canonpath);
3876 if (resolved == NULL) {
3877 err = got_error_from_errno("strdup");
3878 goto done;
3883 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3884 strlen(got_worktree_get_root_path(worktree)))) {
3885 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3886 goto done;
3889 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3890 err = got_path_skip_common_ancestor(&path,
3891 got_worktree_get_root_path(worktree), resolved);
3892 if (err)
3893 goto done;
3894 } else {
3895 path = strdup("");
3896 if (path == NULL) {
3897 err = got_error_from_errno("strdup");
3898 goto done;
3902 /* XXX status walk can't deal with trailing slash! */
3903 len = strlen(path);
3904 while (len > 0 && path[len - 1] == '/') {
3905 path[len - 1] = '\0';
3906 len--;
3908 done:
3909 free(abspath);
3910 free(resolved);
3911 free(cwd);
3912 if (err == NULL)
3913 *wt_path = path;
3914 else
3915 free(path);
3916 return err;
3919 struct schedule_addition_args {
3920 struct got_worktree *worktree;
3921 struct got_fileindex *fileindex;
3922 got_worktree_checkout_cb progress_cb;
3923 void *progress_arg;
3924 struct got_repository *repo;
3927 static const struct got_error *
3928 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3929 const char *relpath, struct got_object_id *blob_id,
3930 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3931 int dirfd, const char *de_name)
3933 struct schedule_addition_args *a = arg;
3934 const struct got_error *err = NULL;
3935 struct got_fileindex_entry *ie;
3936 struct stat sb;
3937 char *ondisk_path;
3939 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3940 relpath) == -1)
3941 return got_error_from_errno("asprintf");
3943 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3944 if (ie) {
3945 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3946 de_name, a->repo);
3947 if (err)
3948 goto done;
3949 /* Re-adding an existing entry is a no-op. */
3950 if (status == GOT_STATUS_ADD)
3951 goto done;
3952 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3953 if (err)
3954 goto done;
3957 if (status != GOT_STATUS_UNVERSIONED) {
3958 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3959 goto done;
3962 err = got_fileindex_entry_alloc(&ie, relpath);
3963 if (err)
3964 goto done;
3965 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3966 relpath, NULL, NULL, 1);
3967 if (err) {
3968 got_fileindex_entry_free(ie);
3969 goto done;
3971 err = got_fileindex_entry_add(a->fileindex, ie);
3972 if (err) {
3973 got_fileindex_entry_free(ie);
3974 goto done;
3976 done:
3977 free(ondisk_path);
3978 if (err)
3979 return err;
3980 if (status == GOT_STATUS_ADD)
3981 return NULL;
3982 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3985 const struct got_error *
3986 got_worktree_schedule_add(struct got_worktree *worktree,
3987 struct got_pathlist_head *paths,
3988 got_worktree_checkout_cb progress_cb, void *progress_arg,
3989 struct got_repository *repo, int no_ignores)
3991 struct got_fileindex *fileindex = NULL;
3992 char *fileindex_path = NULL;
3993 const struct got_error *err = NULL, *sync_err, *unlockerr;
3994 struct got_pathlist_entry *pe;
3995 struct schedule_addition_args saa;
3997 err = lock_worktree(worktree, LOCK_EX);
3998 if (err)
3999 return err;
4001 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4002 if (err)
4003 goto done;
4005 saa.worktree = worktree;
4006 saa.fileindex = fileindex;
4007 saa.progress_cb = progress_cb;
4008 saa.progress_arg = progress_arg;
4009 saa.repo = repo;
4011 TAILQ_FOREACH(pe, paths, entry) {
4012 err = worktree_status(worktree, pe->path, fileindex, repo,
4013 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4014 if (err)
4015 break;
4017 sync_err = sync_fileindex(fileindex, fileindex_path);
4018 if (sync_err && err == NULL)
4019 err = sync_err;
4020 done:
4021 free(fileindex_path);
4022 if (fileindex)
4023 got_fileindex_free(fileindex);
4024 unlockerr = lock_worktree(worktree, LOCK_SH);
4025 if (unlockerr && err == NULL)
4026 err = unlockerr;
4027 return err;
4030 struct schedule_deletion_args {
4031 struct got_worktree *worktree;
4032 struct got_fileindex *fileindex;
4033 got_worktree_delete_cb progress_cb;
4034 void *progress_arg;
4035 struct got_repository *repo;
4036 int delete_local_mods;
4037 int keep_on_disk;
4038 const char *status_codes;
4041 static const struct got_error *
4042 schedule_for_deletion(void *arg, unsigned char status,
4043 unsigned char staged_status, const char *relpath,
4044 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4045 struct got_object_id *commit_id, int dirfd, const char *de_name)
4047 struct schedule_deletion_args *a = arg;
4048 const struct got_error *err = NULL;
4049 struct got_fileindex_entry *ie = NULL;
4050 struct stat sb;
4051 char *ondisk_path;
4053 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4054 if (ie == NULL)
4055 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4057 staged_status = get_staged_status(ie);
4058 if (staged_status != GOT_STATUS_NO_CHANGE) {
4059 if (staged_status == GOT_STATUS_DELETE)
4060 return NULL;
4061 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4064 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4065 relpath) == -1)
4066 return got_error_from_errno("asprintf");
4068 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4069 a->repo);
4070 if (err)
4071 goto done;
4073 if (a->status_codes) {
4074 size_t ncodes = strlen(a->status_codes);
4075 int i;
4076 for (i = 0; i < ncodes ; i++) {
4077 if (status == a->status_codes[i])
4078 break;
4080 if (i == ncodes) {
4081 /* Do not delete files in non-matching status. */
4082 free(ondisk_path);
4083 return NULL;
4085 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4086 a->status_codes[i] != GOT_STATUS_MISSING) {
4087 static char msg[64];
4088 snprintf(msg, sizeof(msg),
4089 "invalid status code '%c'", a->status_codes[i]);
4090 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4091 goto done;
4095 if (status != GOT_STATUS_NO_CHANGE) {
4096 if (status == GOT_STATUS_DELETE)
4097 goto done;
4098 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4099 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4100 goto done;
4102 if (status != GOT_STATUS_MODIFY &&
4103 status != GOT_STATUS_MISSING) {
4104 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4105 goto done;
4109 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4110 size_t root_len;
4112 if (dirfd != -1) {
4113 if (unlinkat(dirfd, de_name, 0) != 0) {
4114 err = got_error_from_errno2("unlinkat",
4115 ondisk_path);
4116 goto done;
4118 } else if (unlink(ondisk_path) != 0) {
4119 err = got_error_from_errno2("unlink", ondisk_path);
4120 goto done;
4123 root_len = strlen(a->worktree->root_path);
4124 do {
4125 char *parent;
4126 err = got_path_dirname(&parent, ondisk_path);
4127 if (err)
4128 goto done;
4129 free(ondisk_path);
4130 ondisk_path = parent;
4131 if (rmdir(ondisk_path) == -1) {
4132 if (errno != ENOTEMPTY)
4133 err = got_error_from_errno2("rmdir",
4134 ondisk_path);
4135 break;
4137 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4138 strlen(ondisk_path), root_len) != 0);
4141 got_fileindex_entry_mark_deleted_from_disk(ie);
4142 done:
4143 free(ondisk_path);
4144 if (err)
4145 return err;
4146 if (status == GOT_STATUS_DELETE)
4147 return NULL;
4148 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4149 staged_status, relpath);
4152 const struct got_error *
4153 got_worktree_schedule_delete(struct got_worktree *worktree,
4154 struct got_pathlist_head *paths, int delete_local_mods,
4155 const char *status_codes,
4156 got_worktree_delete_cb progress_cb, void *progress_arg,
4157 struct got_repository *repo, int keep_on_disk)
4159 struct got_fileindex *fileindex = NULL;
4160 char *fileindex_path = NULL;
4161 const struct got_error *err = NULL, *sync_err, *unlockerr;
4162 struct got_pathlist_entry *pe;
4163 struct schedule_deletion_args sda;
4165 err = lock_worktree(worktree, LOCK_EX);
4166 if (err)
4167 return err;
4169 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4170 if (err)
4171 goto done;
4173 sda.worktree = worktree;
4174 sda.fileindex = fileindex;
4175 sda.progress_cb = progress_cb;
4176 sda.progress_arg = progress_arg;
4177 sda.repo = repo;
4178 sda.delete_local_mods = delete_local_mods;
4179 sda.keep_on_disk = keep_on_disk;
4180 sda.status_codes = status_codes;
4182 TAILQ_FOREACH(pe, paths, entry) {
4183 err = worktree_status(worktree, pe->path, fileindex, repo,
4184 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4185 if (err)
4186 break;
4188 sync_err = sync_fileindex(fileindex, fileindex_path);
4189 if (sync_err && err == NULL)
4190 err = sync_err;
4191 done:
4192 free(fileindex_path);
4193 if (fileindex)
4194 got_fileindex_free(fileindex);
4195 unlockerr = lock_worktree(worktree, LOCK_SH);
4196 if (unlockerr && err == NULL)
4197 err = unlockerr;
4198 return err;
4201 static const struct got_error *
4202 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4204 const struct got_error *err = NULL;
4205 char *line = NULL;
4206 size_t linesize = 0, n;
4207 ssize_t linelen;
4209 linelen = getline(&line, &linesize, infile);
4210 if (linelen == -1) {
4211 if (ferror(infile)) {
4212 err = got_error_from_errno("getline");
4213 goto done;
4215 return NULL;
4217 if (outfile) {
4218 n = fwrite(line, 1, linelen, outfile);
4219 if (n != linelen) {
4220 err = got_ferror(outfile, GOT_ERR_IO);
4221 goto done;
4224 if (rejectfile) {
4225 n = fwrite(line, 1, linelen, rejectfile);
4226 if (n != linelen)
4227 err = got_ferror(outfile, GOT_ERR_IO);
4229 done:
4230 free(line);
4231 return err;
4234 static const struct got_error *
4235 skip_one_line(FILE *f)
4237 char *line = NULL;
4238 size_t linesize = 0;
4239 ssize_t linelen;
4241 linelen = getline(&line, &linesize, f);
4242 if (linelen == -1) {
4243 if (ferror(f))
4244 return got_error_from_errno("getline");
4245 return NULL;
4247 free(line);
4248 return NULL;
4251 static const struct got_error *
4252 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4253 int start_old, int end_old, int start_new, int end_new,
4254 FILE *outfile, FILE *rejectfile)
4256 const struct got_error *err;
4258 /* Copy old file's lines leading up to patch. */
4259 while (!feof(f1) && *line_cur1 < start_old) {
4260 err = copy_one_line(f1, outfile, NULL);
4261 if (err)
4262 return err;
4263 (*line_cur1)++;
4265 /* Skip new file's lines leading up to patch. */
4266 while (!feof(f2) && *line_cur2 < start_new) {
4267 if (rejectfile)
4268 err = copy_one_line(f2, NULL, rejectfile);
4269 else
4270 err = skip_one_line(f2);
4271 if (err)
4272 return err;
4273 (*line_cur2)++;
4275 /* Copy patched lines. */
4276 while (!feof(f2) && *line_cur2 <= end_new) {
4277 err = copy_one_line(f2, outfile, NULL);
4278 if (err)
4279 return err;
4280 (*line_cur2)++;
4282 /* Skip over old file's replaced lines. */
4283 while (!feof(f1) && *line_cur1 <= end_old) {
4284 if (rejectfile)
4285 err = copy_one_line(f1, NULL, rejectfile);
4286 else
4287 err = skip_one_line(f1);
4288 if (err)
4289 return err;
4290 (*line_cur1)++;
4293 return NULL;
4296 static const struct got_error *
4297 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4298 FILE *outfile, FILE *rejectfile)
4300 const struct got_error *err;
4302 if (outfile) {
4303 /* Copy old file's lines until EOF. */
4304 while (!feof(f1)) {
4305 err = copy_one_line(f1, outfile, NULL);
4306 if (err)
4307 return err;
4308 (*line_cur1)++;
4311 if (rejectfile) {
4312 /* Copy new file's lines until EOF. */
4313 while (!feof(f2)) {
4314 err = copy_one_line(f2, NULL, rejectfile);
4315 if (err)
4316 return err;
4317 (*line_cur2)++;
4321 return NULL;
4324 static const struct got_error *
4325 apply_or_reject_change(int *choice, int *nchunks_used,
4326 struct diff_result *diff_result, int n,
4327 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4328 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4329 got_worktree_patch_cb patch_cb, void *patch_arg)
4331 const struct got_error *err = NULL;
4332 struct diff_chunk_context cc = {};
4333 int start_old, end_old, start_new, end_new;
4334 FILE *hunkfile;
4335 struct diff_output_unidiff_state *diff_state;
4336 struct diff_input_info diff_info;
4337 int rc;
4339 *choice = GOT_PATCH_CHOICE_NONE;
4341 /* Get changed line numbers without context lines for copy_change(). */
4342 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4343 start_old = cc.left.start;
4344 end_old = cc.left.end;
4345 start_new = cc.right.start;
4346 end_new = cc.right.end;
4348 /* Get the same change with context lines for display. */
4349 memset(&cc, 0, sizeof(cc));
4350 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4352 memset(&diff_info, 0, sizeof(diff_info));
4353 diff_info.left_path = relpath;
4354 diff_info.right_path = relpath;
4356 diff_state = diff_output_unidiff_state_alloc();
4357 if (diff_state == NULL)
4358 return got_error_set_errno(ENOMEM,
4359 "diff_output_unidiff_state_alloc");
4361 hunkfile = got_opentemp();
4362 if (hunkfile == NULL) {
4363 err = got_error_from_errno("got_opentemp");
4364 goto done;
4367 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4368 diff_result, &cc);
4369 if (rc != DIFF_RC_OK) {
4370 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4371 goto done;
4374 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4375 err = got_ferror(hunkfile, GOT_ERR_IO);
4376 goto done;
4379 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4380 hunkfile, changeno, nchanges);
4381 if (err)
4382 goto done;
4384 switch (*choice) {
4385 case GOT_PATCH_CHOICE_YES:
4386 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4387 end_old, start_new, end_new, outfile, rejectfile);
4388 break;
4389 case GOT_PATCH_CHOICE_NO:
4390 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4391 end_old, start_new, end_new, rejectfile, outfile);
4392 break;
4393 case GOT_PATCH_CHOICE_QUIT:
4394 break;
4395 default:
4396 err = got_error(GOT_ERR_PATCH_CHOICE);
4397 break;
4399 done:
4400 diff_output_unidiff_state_free(diff_state);
4401 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4402 err = got_error_from_errno("fclose");
4403 return err;
4406 struct revert_file_args {
4407 struct got_worktree *worktree;
4408 struct got_fileindex *fileindex;
4409 got_worktree_checkout_cb progress_cb;
4410 void *progress_arg;
4411 got_worktree_patch_cb patch_cb;
4412 void *patch_arg;
4413 struct got_repository *repo;
4416 static const struct got_error *
4417 create_patched_content(char **path_outfile, int reverse_patch,
4418 struct got_object_id *blob_id, const char *path2,
4419 int dirfd2, const char *de_name2,
4420 const char *relpath, struct got_repository *repo,
4421 got_worktree_patch_cb patch_cb, void *patch_arg)
4423 const struct got_error *err, *free_err;
4424 struct got_blob_object *blob = NULL;
4425 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4426 int fd2 = -1;
4427 char link_target[PATH_MAX];
4428 ssize_t link_len = 0;
4429 char *path1 = NULL, *id_str = NULL;
4430 struct stat sb2;
4431 struct got_diffreg_result *diffreg_result = NULL;
4432 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4433 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4435 *path_outfile = NULL;
4437 err = got_object_id_str(&id_str, blob_id);
4438 if (err)
4439 return err;
4441 if (dirfd2 != -1) {
4442 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4443 if (fd2 == -1) {
4444 if (errno != ELOOP) {
4445 err = got_error_from_errno2("openat", path2);
4446 goto done;
4448 link_len = readlinkat(dirfd2, de_name2,
4449 link_target, sizeof(link_target));
4450 if (link_len == -1)
4451 return got_error_from_errno2("readlinkat", path2);
4452 sb2.st_mode = S_IFLNK;
4453 sb2.st_size = link_len;
4455 } else {
4456 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4457 if (fd2 == -1) {
4458 if (errno != ELOOP) {
4459 err = got_error_from_errno2("open", path2);
4460 goto done;
4462 link_len = readlink(path2, link_target,
4463 sizeof(link_target));
4464 if (link_len == -1)
4465 return got_error_from_errno2("readlink", path2);
4466 sb2.st_mode = S_IFLNK;
4467 sb2.st_size = link_len;
4470 if (fd2 != -1) {
4471 if (fstat(fd2, &sb2) == -1) {
4472 err = got_error_from_errno2("fstat", path2);
4473 goto done;
4476 f2 = fdopen(fd2, "r");
4477 if (f2 == NULL) {
4478 err = got_error_from_errno2("fdopen", path2);
4479 goto done;
4481 fd2 = -1;
4482 } else {
4483 size_t n;
4484 f2 = got_opentemp();
4485 if (f2 == NULL) {
4486 err = got_error_from_errno2("got_opentemp", path2);
4487 goto done;
4489 n = fwrite(link_target, 1, link_len, f2);
4490 if (n != link_len) {
4491 err = got_ferror(f2, GOT_ERR_IO);
4492 goto done;
4494 if (fflush(f2) == EOF) {
4495 err = got_error_from_errno("fflush");
4496 goto done;
4498 rewind(f2);
4501 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4502 if (err)
4503 goto done;
4505 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4506 if (err)
4507 goto done;
4509 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4510 if (err)
4511 goto done;
4513 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4514 NULL);
4515 if (err)
4516 goto done;
4518 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4519 if (err)
4520 goto done;
4522 if (fseek(f1, 0L, SEEK_SET) == -1)
4523 return got_ferror(f1, GOT_ERR_IO);
4524 if (fseek(f2, 0L, SEEK_SET) == -1)
4525 return got_ferror(f2, GOT_ERR_IO);
4527 /* Count the number of actual changes in the diff result. */
4528 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4529 struct diff_chunk_context cc = {};
4530 diff_chunk_context_load_change(&cc, &nchunks_used,
4531 diffreg_result->result, n, 0);
4532 nchanges++;
4534 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4535 int choice;
4536 err = apply_or_reject_change(&choice, &nchunks_used,
4537 diffreg_result->result, n, relpath, f1, f2,
4538 &line_cur1, &line_cur2,
4539 reverse_patch ? NULL : outfile,
4540 reverse_patch ? outfile : NULL,
4541 ++i, nchanges, patch_cb, patch_arg);
4542 if (err)
4543 goto done;
4544 if (choice == GOT_PATCH_CHOICE_YES)
4545 have_content = 1;
4546 else if (choice == GOT_PATCH_CHOICE_QUIT)
4547 break;
4549 if (have_content) {
4550 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4551 reverse_patch ? NULL : outfile,
4552 reverse_patch ? outfile : NULL);
4553 if (err)
4554 goto done;
4556 if (!S_ISLNK(sb2.st_mode)) {
4557 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4558 err = got_error_from_errno2("fchmod", path2);
4559 goto done;
4563 done:
4564 free(id_str);
4565 if (blob)
4566 got_object_blob_close(blob);
4567 free_err = got_diffreg_result_free(diffreg_result);
4568 if (err == NULL)
4569 err = free_err;
4570 if (f1 && fclose(f1) == EOF && err == NULL)
4571 err = got_error_from_errno2("fclose", path1);
4572 if (f2 && fclose(f2) == EOF && err == NULL)
4573 err = got_error_from_errno2("fclose", path2);
4574 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4575 err = got_error_from_errno2("close", path2);
4576 if (outfile && fclose(outfile) == EOF && err == NULL)
4577 err = got_error_from_errno2("fclose", *path_outfile);
4578 if (path1 && unlink(path1) == -1 && err == NULL)
4579 err = got_error_from_errno2("unlink", path1);
4580 if (err || !have_content) {
4581 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4582 err = got_error_from_errno2("unlink", *path_outfile);
4583 free(*path_outfile);
4584 *path_outfile = NULL;
4586 free(path1);
4587 return err;
4590 static const struct got_error *
4591 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4592 const char *relpath, struct got_object_id *blob_id,
4593 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4594 int dirfd, const char *de_name)
4596 struct revert_file_args *a = arg;
4597 const struct got_error *err = NULL;
4598 char *parent_path = NULL;
4599 struct got_fileindex_entry *ie;
4600 struct got_tree_object *tree = NULL;
4601 struct got_object_id *tree_id = NULL;
4602 const struct got_tree_entry *te = NULL;
4603 char *tree_path = NULL, *te_name;
4604 char *ondisk_path = NULL, *path_content = NULL;
4605 struct got_blob_object *blob = NULL;
4607 /* Reverting a staged deletion is a no-op. */
4608 if (status == GOT_STATUS_DELETE &&
4609 staged_status != GOT_STATUS_NO_CHANGE)
4610 return NULL;
4612 if (status == GOT_STATUS_UNVERSIONED)
4613 return (*a->progress_cb)(a->progress_arg,
4614 GOT_STATUS_UNVERSIONED, relpath);
4616 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4617 if (ie == NULL)
4618 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4620 /* Construct in-repository path of tree which contains this blob. */
4621 err = got_path_dirname(&parent_path, ie->path);
4622 if (err) {
4623 if (err->code != GOT_ERR_BAD_PATH)
4624 goto done;
4625 parent_path = strdup("/");
4626 if (parent_path == NULL) {
4627 err = got_error_from_errno("strdup");
4628 goto done;
4631 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4632 tree_path = strdup(parent_path);
4633 if (tree_path == NULL) {
4634 err = got_error_from_errno("strdup");
4635 goto done;
4637 } else {
4638 if (got_path_is_root_dir(parent_path)) {
4639 tree_path = strdup(a->worktree->path_prefix);
4640 if (tree_path == NULL) {
4641 err = got_error_from_errno("strdup");
4642 goto done;
4644 } else {
4645 if (asprintf(&tree_path, "%s/%s",
4646 a->worktree->path_prefix, parent_path) == -1) {
4647 err = got_error_from_errno("asprintf");
4648 goto done;
4653 err = got_object_id_by_path(&tree_id, a->repo,
4654 a->worktree->base_commit_id, tree_path);
4655 if (err) {
4656 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4657 (status == GOT_STATUS_ADD ||
4658 staged_status == GOT_STATUS_ADD)))
4659 goto done;
4660 } else {
4661 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4662 if (err)
4663 goto done;
4665 err = got_path_basename(&te_name, ie->path);
4666 if (err)
4667 goto done;
4669 te = got_object_tree_find_entry(tree, te_name);
4670 free(te_name);
4671 if (te == NULL && status != GOT_STATUS_ADD &&
4672 staged_status != GOT_STATUS_ADD) {
4673 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4674 goto done;
4678 switch (status) {
4679 case GOT_STATUS_ADD:
4680 if (a->patch_cb) {
4681 int choice = GOT_PATCH_CHOICE_NONE;
4682 err = (*a->patch_cb)(&choice, a->patch_arg,
4683 status, ie->path, NULL, 1, 1);
4684 if (err)
4685 goto done;
4686 if (choice != GOT_PATCH_CHOICE_YES)
4687 break;
4689 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4690 ie->path);
4691 if (err)
4692 goto done;
4693 got_fileindex_entry_remove(a->fileindex, ie);
4694 break;
4695 case GOT_STATUS_DELETE:
4696 if (a->patch_cb) {
4697 int choice = GOT_PATCH_CHOICE_NONE;
4698 err = (*a->patch_cb)(&choice, a->patch_arg,
4699 status, ie->path, NULL, 1, 1);
4700 if (err)
4701 goto done;
4702 if (choice != GOT_PATCH_CHOICE_YES)
4703 break;
4705 /* fall through */
4706 case GOT_STATUS_MODIFY:
4707 case GOT_STATUS_MODE_CHANGE:
4708 case GOT_STATUS_CONFLICT:
4709 case GOT_STATUS_MISSING: {
4710 struct got_object_id id;
4711 if (staged_status == GOT_STATUS_ADD ||
4712 staged_status == GOT_STATUS_MODIFY) {
4713 memcpy(id.sha1, ie->staged_blob_sha1,
4714 SHA1_DIGEST_LENGTH);
4715 } else
4716 memcpy(id.sha1, ie->blob_sha1,
4717 SHA1_DIGEST_LENGTH);
4718 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4719 if (err)
4720 goto done;
4722 if (asprintf(&ondisk_path, "%s/%s",
4723 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4724 err = got_error_from_errno("asprintf");
4725 goto done;
4728 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4729 status == GOT_STATUS_CONFLICT)) {
4730 int is_bad_symlink = 0;
4731 err = create_patched_content(&path_content, 1, &id,
4732 ondisk_path, dirfd, de_name, ie->path, a->repo,
4733 a->patch_cb, a->patch_arg);
4734 if (err || path_content == NULL)
4735 break;
4736 if (te && S_ISLNK(te->mode)) {
4737 if (unlink(path_content) == -1) {
4738 err = got_error_from_errno2("unlink",
4739 path_content);
4740 break;
4742 err = install_symlink(&is_bad_symlink,
4743 a->worktree, ondisk_path, ie->path,
4744 blob, 0, 1, 0, a->repo,
4745 a->progress_cb, a->progress_arg);
4746 } else {
4747 if (rename(path_content, ondisk_path) == -1) {
4748 err = got_error_from_errno3("rename",
4749 path_content, ondisk_path);
4750 goto done;
4753 } else {
4754 int is_bad_symlink = 0;
4755 if (te && S_ISLNK(te->mode)) {
4756 err = install_symlink(&is_bad_symlink,
4757 a->worktree, ondisk_path, ie->path,
4758 blob, 0, 1, 0, a->repo,
4759 a->progress_cb, a->progress_arg);
4760 } else {
4761 err = install_blob(a->worktree, ondisk_path,
4762 ie->path,
4763 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4764 got_fileindex_perms_to_st(ie), blob,
4765 0, 1, 0, 0, a->repo,
4766 a->progress_cb, a->progress_arg);
4768 if (err)
4769 goto done;
4770 if (status == GOT_STATUS_DELETE ||
4771 status == GOT_STATUS_MODE_CHANGE) {
4772 err = got_fileindex_entry_update(ie,
4773 a->worktree->root_fd, relpath,
4774 blob->id.sha1,
4775 a->worktree->base_commit_id->sha1, 1);
4776 if (err)
4777 goto done;
4779 if (is_bad_symlink) {
4780 got_fileindex_entry_filetype_set(ie,
4781 GOT_FILEIDX_MODE_BAD_SYMLINK);
4784 break;
4786 default:
4787 break;
4789 done:
4790 free(ondisk_path);
4791 free(path_content);
4792 free(parent_path);
4793 free(tree_path);
4794 if (blob)
4795 got_object_blob_close(blob);
4796 if (tree)
4797 got_object_tree_close(tree);
4798 free(tree_id);
4799 return err;
4802 const struct got_error *
4803 got_worktree_revert(struct got_worktree *worktree,
4804 struct got_pathlist_head *paths,
4805 got_worktree_checkout_cb progress_cb, void *progress_arg,
4806 got_worktree_patch_cb patch_cb, void *patch_arg,
4807 struct got_repository *repo)
4809 struct got_fileindex *fileindex = NULL;
4810 char *fileindex_path = NULL;
4811 const struct got_error *err = NULL, *unlockerr = NULL;
4812 const struct got_error *sync_err = NULL;
4813 struct got_pathlist_entry *pe;
4814 struct revert_file_args rfa;
4816 err = lock_worktree(worktree, LOCK_EX);
4817 if (err)
4818 return err;
4820 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4821 if (err)
4822 goto done;
4824 rfa.worktree = worktree;
4825 rfa.fileindex = fileindex;
4826 rfa.progress_cb = progress_cb;
4827 rfa.progress_arg = progress_arg;
4828 rfa.patch_cb = patch_cb;
4829 rfa.patch_arg = patch_arg;
4830 rfa.repo = repo;
4831 TAILQ_FOREACH(pe, paths, entry) {
4832 err = worktree_status(worktree, pe->path, fileindex, repo,
4833 revert_file, &rfa, NULL, NULL, 0, 0);
4834 if (err)
4835 break;
4837 sync_err = sync_fileindex(fileindex, fileindex_path);
4838 if (sync_err && err == NULL)
4839 err = sync_err;
4840 done:
4841 free(fileindex_path);
4842 if (fileindex)
4843 got_fileindex_free(fileindex);
4844 unlockerr = lock_worktree(worktree, LOCK_SH);
4845 if (unlockerr && err == NULL)
4846 err = unlockerr;
4847 return err;
4850 static void
4851 free_commitable(struct got_commitable *ct)
4853 free(ct->path);
4854 free(ct->in_repo_path);
4855 free(ct->ondisk_path);
4856 free(ct->blob_id);
4857 free(ct->base_blob_id);
4858 free(ct->staged_blob_id);
4859 free(ct->base_commit_id);
4860 free(ct);
4863 struct collect_commitables_arg {
4864 struct got_pathlist_head *commitable_paths;
4865 struct got_repository *repo;
4866 struct got_worktree *worktree;
4867 struct got_fileindex *fileindex;
4868 int have_staged_files;
4869 int allow_bad_symlinks;
4872 static const struct got_error *
4873 collect_commitables(void *arg, unsigned char status,
4874 unsigned char staged_status, const char *relpath,
4875 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4876 struct got_object_id *commit_id, int dirfd, const char *de_name)
4878 struct collect_commitables_arg *a = arg;
4879 const struct got_error *err = NULL;
4880 struct got_commitable *ct = NULL;
4881 struct got_pathlist_entry *new = NULL;
4882 char *parent_path = NULL, *path = NULL;
4883 struct stat sb;
4885 if (a->have_staged_files) {
4886 if (staged_status != GOT_STATUS_MODIFY &&
4887 staged_status != GOT_STATUS_ADD &&
4888 staged_status != GOT_STATUS_DELETE)
4889 return NULL;
4890 } else {
4891 if (status == GOT_STATUS_CONFLICT)
4892 return got_error(GOT_ERR_COMMIT_CONFLICT);
4894 if (status != GOT_STATUS_MODIFY &&
4895 status != GOT_STATUS_MODE_CHANGE &&
4896 status != GOT_STATUS_ADD &&
4897 status != GOT_STATUS_DELETE)
4898 return NULL;
4901 if (asprintf(&path, "/%s", relpath) == -1) {
4902 err = got_error_from_errno("asprintf");
4903 goto done;
4905 if (strcmp(path, "/") == 0) {
4906 parent_path = strdup("");
4907 if (parent_path == NULL)
4908 return got_error_from_errno("strdup");
4909 } else {
4910 err = got_path_dirname(&parent_path, path);
4911 if (err)
4912 return err;
4915 ct = calloc(1, sizeof(*ct));
4916 if (ct == NULL) {
4917 err = got_error_from_errno("calloc");
4918 goto done;
4921 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4922 relpath) == -1) {
4923 err = got_error_from_errno("asprintf");
4924 goto done;
4927 if (staged_status == GOT_STATUS_ADD ||
4928 staged_status == GOT_STATUS_MODIFY) {
4929 struct got_fileindex_entry *ie;
4930 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4931 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4932 case GOT_FILEIDX_MODE_REGULAR_FILE:
4933 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4934 ct->mode = S_IFREG;
4935 break;
4936 case GOT_FILEIDX_MODE_SYMLINK:
4937 ct->mode = S_IFLNK;
4938 break;
4939 default:
4940 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4941 goto done;
4943 ct->mode |= got_fileindex_entry_perms_get(ie);
4944 } else if (status != GOT_STATUS_DELETE &&
4945 staged_status != GOT_STATUS_DELETE) {
4946 if (dirfd != -1) {
4947 if (fstatat(dirfd, de_name, &sb,
4948 AT_SYMLINK_NOFOLLOW) == -1) {
4949 err = got_error_from_errno2("fstatat",
4950 ct->ondisk_path);
4951 goto done;
4953 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4954 err = got_error_from_errno2("lstat", ct->ondisk_path);
4955 goto done;
4957 ct->mode = sb.st_mode;
4960 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4961 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4962 relpath) == -1) {
4963 err = got_error_from_errno("asprintf");
4964 goto done;
4967 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4968 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4969 int is_bad_symlink;
4970 char target_path[PATH_MAX];
4971 ssize_t target_len;
4972 target_len = readlink(ct->ondisk_path, target_path,
4973 sizeof(target_path));
4974 if (target_len == -1) {
4975 err = got_error_from_errno2("readlink",
4976 ct->ondisk_path);
4977 goto done;
4979 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4980 target_len, ct->ondisk_path, a->worktree->root_path);
4981 if (err)
4982 goto done;
4983 if (is_bad_symlink) {
4984 err = got_error_path(ct->ondisk_path,
4985 GOT_ERR_BAD_SYMLINK);
4986 goto done;
4991 ct->status = status;
4992 ct->staged_status = staged_status;
4993 ct->blob_id = NULL; /* will be filled in when blob gets created */
4994 if (ct->status != GOT_STATUS_ADD &&
4995 ct->staged_status != GOT_STATUS_ADD) {
4996 ct->base_blob_id = got_object_id_dup(blob_id);
4997 if (ct->base_blob_id == NULL) {
4998 err = got_error_from_errno("got_object_id_dup");
4999 goto done;
5001 ct->base_commit_id = got_object_id_dup(commit_id);
5002 if (ct->base_commit_id == NULL) {
5003 err = got_error_from_errno("got_object_id_dup");
5004 goto done;
5007 if (ct->staged_status == GOT_STATUS_ADD ||
5008 ct->staged_status == GOT_STATUS_MODIFY) {
5009 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5010 if (ct->staged_blob_id == NULL) {
5011 err = got_error_from_errno("got_object_id_dup");
5012 goto done;
5015 ct->path = strdup(path);
5016 if (ct->path == NULL) {
5017 err = got_error_from_errno("strdup");
5018 goto done;
5020 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5021 done:
5022 if (ct && (err || new == NULL))
5023 free_commitable(ct);
5024 free(parent_path);
5025 free(path);
5026 return err;
5029 static const struct got_error *write_tree(struct got_object_id **, int *,
5030 struct got_tree_object *, const char *, struct got_pathlist_head *,
5031 got_worktree_status_cb status_cb, void *status_arg,
5032 struct got_repository *);
5034 static const struct got_error *
5035 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5036 struct got_tree_entry *te, const char *parent_path,
5037 struct got_pathlist_head *commitable_paths,
5038 got_worktree_status_cb status_cb, void *status_arg,
5039 struct got_repository *repo)
5041 const struct got_error *err = NULL;
5042 struct got_tree_object *subtree;
5043 char *subpath;
5045 if (asprintf(&subpath, "%s%s%s", parent_path,
5046 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5047 return got_error_from_errno("asprintf");
5049 err = got_object_open_as_tree(&subtree, repo, &te->id);
5050 if (err)
5051 return err;
5053 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5054 commitable_paths, status_cb, status_arg, repo);
5055 got_object_tree_close(subtree);
5056 free(subpath);
5057 return err;
5060 static const struct got_error *
5061 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5063 const struct got_error *err = NULL;
5064 char *ct_parent_path = NULL;
5066 *match = 0;
5068 if (strchr(ct->in_repo_path, '/') == NULL) {
5069 *match = got_path_is_root_dir(path);
5070 return NULL;
5073 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5074 if (err)
5075 return err;
5076 *match = (strcmp(path, ct_parent_path) == 0);
5077 free(ct_parent_path);
5078 return err;
5081 static mode_t
5082 get_ct_file_mode(struct got_commitable *ct)
5084 if (S_ISLNK(ct->mode))
5085 return S_IFLNK;
5087 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5090 static const struct got_error *
5091 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5092 struct got_tree_entry *te, struct got_commitable *ct)
5094 const struct got_error *err = NULL;
5096 *new_te = NULL;
5098 err = got_object_tree_entry_dup(new_te, te);
5099 if (err)
5100 goto done;
5102 (*new_te)->mode = get_ct_file_mode(ct);
5104 if (ct->staged_status == GOT_STATUS_MODIFY)
5105 memcpy(&(*new_te)->id, ct->staged_blob_id,
5106 sizeof((*new_te)->id));
5107 else
5108 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5109 done:
5110 if (err && *new_te) {
5111 free(*new_te);
5112 *new_te = NULL;
5114 return err;
5117 static const struct got_error *
5118 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5119 struct got_commitable *ct)
5121 const struct got_error *err = NULL;
5122 char *ct_name = NULL;
5124 *new_te = NULL;
5126 *new_te = calloc(1, sizeof(**new_te));
5127 if (*new_te == NULL)
5128 return got_error_from_errno("calloc");
5130 err = got_path_basename(&ct_name, ct->path);
5131 if (err)
5132 goto done;
5133 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5134 sizeof((*new_te)->name)) {
5135 err = got_error(GOT_ERR_NO_SPACE);
5136 goto done;
5139 (*new_te)->mode = get_ct_file_mode(ct);
5141 if (ct->staged_status == GOT_STATUS_ADD)
5142 memcpy(&(*new_te)->id, ct->staged_blob_id,
5143 sizeof((*new_te)->id));
5144 else
5145 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5146 done:
5147 free(ct_name);
5148 if (err && *new_te) {
5149 free(*new_te);
5150 *new_te = NULL;
5152 return err;
5155 static const struct got_error *
5156 insert_tree_entry(struct got_tree_entry *new_te,
5157 struct got_pathlist_head *paths)
5159 const struct got_error *err = NULL;
5160 struct got_pathlist_entry *new_pe;
5162 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5163 if (err)
5164 return err;
5165 if (new_pe == NULL)
5166 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5167 return NULL;
5170 static const struct got_error *
5171 report_ct_status(struct got_commitable *ct,
5172 got_worktree_status_cb status_cb, void *status_arg)
5174 const char *ct_path = ct->path;
5175 unsigned char status;
5177 while (ct_path[0] == '/')
5178 ct_path++;
5180 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5181 status = ct->staged_status;
5182 else
5183 status = ct->status;
5185 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5186 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5189 static const struct got_error *
5190 match_modified_subtree(int *modified, struct got_tree_entry *te,
5191 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5193 const struct got_error *err = NULL;
5194 struct got_pathlist_entry *pe;
5195 char *te_path;
5197 *modified = 0;
5199 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5200 got_path_is_root_dir(base_tree_path) ? "" : "/",
5201 te->name) == -1)
5202 return got_error_from_errno("asprintf");
5204 TAILQ_FOREACH(pe, commitable_paths, entry) {
5205 struct got_commitable *ct = pe->data;
5206 *modified = got_path_is_child(ct->in_repo_path, te_path,
5207 strlen(te_path));
5208 if (*modified)
5209 break;
5212 free(te_path);
5213 return err;
5216 static const struct got_error *
5217 match_deleted_or_modified_ct(struct got_commitable **ctp,
5218 struct got_tree_entry *te, const char *base_tree_path,
5219 struct got_pathlist_head *commitable_paths)
5221 const struct got_error *err = NULL;
5222 struct got_pathlist_entry *pe;
5224 *ctp = NULL;
5226 TAILQ_FOREACH(pe, commitable_paths, entry) {
5227 struct got_commitable *ct = pe->data;
5228 char *ct_name = NULL;
5229 int path_matches;
5231 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5232 if (ct->status != GOT_STATUS_MODIFY &&
5233 ct->status != GOT_STATUS_MODE_CHANGE &&
5234 ct->status != GOT_STATUS_DELETE)
5235 continue;
5236 } else {
5237 if (ct->staged_status != GOT_STATUS_MODIFY &&
5238 ct->staged_status != GOT_STATUS_DELETE)
5239 continue;
5242 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5243 continue;
5245 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5246 if (err)
5247 return err;
5248 if (!path_matches)
5249 continue;
5251 err = got_path_basename(&ct_name, pe->path);
5252 if (err)
5253 return err;
5255 if (strcmp(te->name, ct_name) != 0) {
5256 free(ct_name);
5257 continue;
5259 free(ct_name);
5261 *ctp = ct;
5262 break;
5265 return err;
5268 static const struct got_error *
5269 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5270 const char *child_path, const char *path_base_tree,
5271 struct got_pathlist_head *commitable_paths,
5272 got_worktree_status_cb status_cb, void *status_arg,
5273 struct got_repository *repo)
5275 const struct got_error *err = NULL;
5276 struct got_tree_entry *new_te;
5277 char *subtree_path;
5278 struct got_object_id *id = NULL;
5279 int nentries;
5281 *new_tep = NULL;
5283 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5284 got_path_is_root_dir(path_base_tree) ? "" : "/",
5285 child_path) == -1)
5286 return got_error_from_errno("asprintf");
5288 new_te = calloc(1, sizeof(*new_te));
5289 if (new_te == NULL)
5290 return got_error_from_errno("calloc");
5291 new_te->mode = S_IFDIR;
5293 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5294 sizeof(new_te->name)) {
5295 err = got_error(GOT_ERR_NO_SPACE);
5296 goto done;
5298 err = write_tree(&id, &nentries, NULL, subtree_path,
5299 commitable_paths, status_cb, status_arg, repo);
5300 if (err) {
5301 free(new_te);
5302 goto done;
5304 memcpy(&new_te->id, id, sizeof(new_te->id));
5305 done:
5306 free(id);
5307 free(subtree_path);
5308 if (err == NULL)
5309 *new_tep = new_te;
5310 return err;
5313 static const struct got_error *
5314 write_tree(struct got_object_id **new_tree_id, int *nentries,
5315 struct got_tree_object *base_tree, const char *path_base_tree,
5316 struct got_pathlist_head *commitable_paths,
5317 got_worktree_status_cb status_cb, void *status_arg,
5318 struct got_repository *repo)
5320 const struct got_error *err = NULL;
5321 struct got_pathlist_head paths;
5322 struct got_tree_entry *te, *new_te = NULL;
5323 struct got_pathlist_entry *pe;
5325 TAILQ_INIT(&paths);
5326 *nentries = 0;
5328 /* Insert, and recurse into, newly added entries first. */
5329 TAILQ_FOREACH(pe, commitable_paths, entry) {
5330 struct got_commitable *ct = pe->data;
5331 char *child_path = NULL, *slash;
5333 if ((ct->status != GOT_STATUS_ADD &&
5334 ct->staged_status != GOT_STATUS_ADD) ||
5335 (ct->flags & GOT_COMMITABLE_ADDED))
5336 continue;
5338 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5339 strlen(path_base_tree)))
5340 continue;
5342 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5343 ct->in_repo_path);
5344 if (err)
5345 goto done;
5347 slash = strchr(child_path, '/');
5348 if (slash == NULL) {
5349 err = alloc_added_blob_tree_entry(&new_te, ct);
5350 if (err)
5351 goto done;
5352 err = report_ct_status(ct, status_cb, status_arg);
5353 if (err)
5354 goto done;
5355 ct->flags |= GOT_COMMITABLE_ADDED;
5356 err = insert_tree_entry(new_te, &paths);
5357 if (err)
5358 goto done;
5359 (*nentries)++;
5360 } else {
5361 *slash = '\0'; /* trim trailing path components */
5362 if (base_tree == NULL ||
5363 got_object_tree_find_entry(base_tree, child_path)
5364 == NULL) {
5365 err = make_subtree_for_added_blob(&new_te,
5366 child_path, path_base_tree,
5367 commitable_paths, status_cb, status_arg,
5368 repo);
5369 if (err)
5370 goto done;
5371 err = insert_tree_entry(new_te, &paths);
5372 if (err)
5373 goto done;
5374 (*nentries)++;
5379 if (base_tree) {
5380 int i, nbase_entries;
5381 /* Handle modified and deleted entries. */
5382 nbase_entries = got_object_tree_get_nentries(base_tree);
5383 for (i = 0; i < nbase_entries; i++) {
5384 struct got_commitable *ct = NULL;
5386 te = got_object_tree_get_entry(base_tree, i);
5387 if (got_object_tree_entry_is_submodule(te)) {
5388 /* Entry is a submodule; just copy it. */
5389 err = got_object_tree_entry_dup(&new_te, te);
5390 if (err)
5391 goto done;
5392 err = insert_tree_entry(new_te, &paths);
5393 if (err)
5394 goto done;
5395 (*nentries)++;
5396 continue;
5399 if (S_ISDIR(te->mode)) {
5400 int modified;
5401 err = got_object_tree_entry_dup(&new_te, te);
5402 if (err)
5403 goto done;
5404 err = match_modified_subtree(&modified, te,
5405 path_base_tree, commitable_paths);
5406 if (err)
5407 goto done;
5408 /* Avoid recursion into unmodified subtrees. */
5409 if (modified) {
5410 struct got_object_id *new_id;
5411 int nsubentries;
5412 err = write_subtree(&new_id,
5413 &nsubentries, te,
5414 path_base_tree, commitable_paths,
5415 status_cb, status_arg, repo);
5416 if (err)
5417 goto done;
5418 if (nsubentries == 0) {
5419 /* All entries were deleted. */
5420 free(new_id);
5421 continue;
5423 memcpy(&new_te->id, new_id,
5424 sizeof(new_te->id));
5425 free(new_id);
5427 err = insert_tree_entry(new_te, &paths);
5428 if (err)
5429 goto done;
5430 (*nentries)++;
5431 continue;
5434 err = match_deleted_or_modified_ct(&ct, te,
5435 path_base_tree, commitable_paths);
5436 if (err)
5437 goto done;
5438 if (ct) {
5439 /* NB: Deleted entries get dropped here. */
5440 if (ct->status == GOT_STATUS_MODIFY ||
5441 ct->status == GOT_STATUS_MODE_CHANGE ||
5442 ct->staged_status == GOT_STATUS_MODIFY) {
5443 err = alloc_modified_blob_tree_entry(
5444 &new_te, te, ct);
5445 if (err)
5446 goto done;
5447 err = insert_tree_entry(new_te, &paths);
5448 if (err)
5449 goto done;
5450 (*nentries)++;
5452 err = report_ct_status(ct, status_cb,
5453 status_arg);
5454 if (err)
5455 goto done;
5456 } else {
5457 /* Entry is unchanged; just copy it. */
5458 err = got_object_tree_entry_dup(&new_te, te);
5459 if (err)
5460 goto done;
5461 err = insert_tree_entry(new_te, &paths);
5462 if (err)
5463 goto done;
5464 (*nentries)++;
5469 /* Write new list of entries; deleted entries have been dropped. */
5470 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5471 done:
5472 got_pathlist_free(&paths);
5473 return err;
5476 static const struct got_error *
5477 update_fileindex_after_commit(struct got_worktree *worktree,
5478 struct got_pathlist_head *commitable_paths,
5479 struct got_object_id *new_base_commit_id,
5480 struct got_fileindex *fileindex, int have_staged_files)
5482 const struct got_error *err = NULL;
5483 struct got_pathlist_entry *pe;
5484 char *relpath = NULL;
5486 TAILQ_FOREACH(pe, commitable_paths, entry) {
5487 struct got_fileindex_entry *ie;
5488 struct got_commitable *ct = pe->data;
5490 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5492 err = got_path_skip_common_ancestor(&relpath,
5493 worktree->root_path, ct->ondisk_path);
5494 if (err)
5495 goto done;
5497 if (ie) {
5498 if (ct->status == GOT_STATUS_DELETE ||
5499 ct->staged_status == GOT_STATUS_DELETE) {
5500 got_fileindex_entry_remove(fileindex, ie);
5501 } else if (ct->staged_status == GOT_STATUS_ADD ||
5502 ct->staged_status == GOT_STATUS_MODIFY) {
5503 got_fileindex_entry_stage_set(ie,
5504 GOT_FILEIDX_STAGE_NONE);
5505 got_fileindex_entry_staged_filetype_set(ie, 0);
5507 err = got_fileindex_entry_update(ie,
5508 worktree->root_fd, relpath,
5509 ct->staged_blob_id->sha1,
5510 new_base_commit_id->sha1,
5511 !have_staged_files);
5512 } else
5513 err = got_fileindex_entry_update(ie,
5514 worktree->root_fd, relpath,
5515 ct->blob_id->sha1,
5516 new_base_commit_id->sha1,
5517 !have_staged_files);
5518 } else {
5519 err = got_fileindex_entry_alloc(&ie, pe->path);
5520 if (err)
5521 goto done;
5522 err = got_fileindex_entry_update(ie,
5523 worktree->root_fd, relpath, ct->blob_id->sha1,
5524 new_base_commit_id->sha1, 1);
5525 if (err) {
5526 got_fileindex_entry_free(ie);
5527 goto done;
5529 err = got_fileindex_entry_add(fileindex, ie);
5530 if (err) {
5531 got_fileindex_entry_free(ie);
5532 goto done;
5535 free(relpath);
5536 relpath = NULL;
5538 done:
5539 free(relpath);
5540 return err;
5544 static const struct got_error *
5545 check_out_of_date(const char *in_repo_path, unsigned char status,
5546 unsigned char staged_status, struct got_object_id *base_blob_id,
5547 struct got_object_id *base_commit_id,
5548 struct got_object_id *head_commit_id, struct got_repository *repo,
5549 int ood_errcode)
5551 const struct got_error *err = NULL;
5552 struct got_object_id *id = NULL;
5554 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5555 /* Trivial case: base commit == head commit */
5556 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5557 return NULL;
5559 * Ensure file content which local changes were based
5560 * on matches file content in the branch head.
5562 err = got_object_id_by_path(&id, repo, head_commit_id,
5563 in_repo_path);
5564 if (err) {
5565 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5566 err = got_error(ood_errcode);
5567 goto done;
5568 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5569 err = got_error(ood_errcode);
5570 } else {
5571 /* Require that added files don't exist in the branch head. */
5572 err = got_object_id_by_path(&id, repo, head_commit_id,
5573 in_repo_path);
5574 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5575 goto done;
5576 err = id ? got_error(ood_errcode) : NULL;
5578 done:
5579 free(id);
5580 return err;
5583 const struct got_error *
5584 commit_worktree(struct got_object_id **new_commit_id,
5585 struct got_pathlist_head *commitable_paths,
5586 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5587 const char *author, const char *committer,
5588 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5589 got_worktree_status_cb status_cb, void *status_arg,
5590 struct got_repository *repo)
5592 const struct got_error *err = NULL, *unlockerr = NULL;
5593 struct got_pathlist_entry *pe;
5594 const char *head_ref_name = NULL;
5595 struct got_commit_object *head_commit = NULL;
5596 struct got_reference *head_ref2 = NULL;
5597 struct got_object_id *head_commit_id2 = NULL;
5598 struct got_tree_object *head_tree = NULL;
5599 struct got_object_id *new_tree_id = NULL;
5600 int nentries;
5601 struct got_object_id_queue parent_ids;
5602 struct got_object_qid *pid = NULL;
5603 char *logmsg = NULL;
5605 *new_commit_id = NULL;
5607 STAILQ_INIT(&parent_ids);
5609 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5610 if (err)
5611 goto done;
5613 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5614 if (err)
5615 goto done;
5617 if (commit_msg_cb != NULL) {
5618 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5619 if (err)
5620 goto done;
5623 if (logmsg == NULL || strlen(logmsg) == 0) {
5624 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5625 goto done;
5628 /* Create blobs from added and modified files and record their IDs. */
5629 TAILQ_FOREACH(pe, commitable_paths, entry) {
5630 struct got_commitable *ct = pe->data;
5631 char *ondisk_path;
5633 /* Blobs for staged files already exist. */
5634 if (ct->staged_status == GOT_STATUS_ADD ||
5635 ct->staged_status == GOT_STATUS_MODIFY)
5636 continue;
5638 if (ct->status != GOT_STATUS_ADD &&
5639 ct->status != GOT_STATUS_MODIFY &&
5640 ct->status != GOT_STATUS_MODE_CHANGE)
5641 continue;
5643 if (asprintf(&ondisk_path, "%s/%s",
5644 worktree->root_path, pe->path) == -1) {
5645 err = got_error_from_errno("asprintf");
5646 goto done;
5648 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5649 free(ondisk_path);
5650 if (err)
5651 goto done;
5654 /* Recursively write new tree objects. */
5655 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5656 commitable_paths, status_cb, status_arg, repo);
5657 if (err)
5658 goto done;
5660 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5661 if (err)
5662 goto done;
5663 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5664 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5665 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5666 got_object_qid_free(pid);
5667 if (logmsg != NULL)
5668 free(logmsg);
5669 if (err)
5670 goto done;
5672 /* Check if a concurrent commit to our branch has occurred. */
5673 head_ref_name = got_worktree_get_head_ref_name(worktree);
5674 if (head_ref_name == NULL) {
5675 err = got_error_from_errno("got_worktree_get_head_ref_name");
5676 goto done;
5678 /* Lock the reference here to prevent concurrent modification. */
5679 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5680 if (err)
5681 goto done;
5682 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5683 if (err)
5684 goto done;
5685 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5686 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5687 goto done;
5689 /* Update branch head in repository. */
5690 err = got_ref_change_ref(head_ref2, *new_commit_id);
5691 if (err)
5692 goto done;
5693 err = got_ref_write(head_ref2, repo);
5694 if (err)
5695 goto done;
5697 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5698 if (err)
5699 goto done;
5701 err = ref_base_commit(worktree, repo);
5702 if (err)
5703 goto done;
5704 done:
5705 if (head_tree)
5706 got_object_tree_close(head_tree);
5707 if (head_commit)
5708 got_object_commit_close(head_commit);
5709 free(head_commit_id2);
5710 if (head_ref2) {
5711 unlockerr = got_ref_unlock(head_ref2);
5712 if (unlockerr && err == NULL)
5713 err = unlockerr;
5714 got_ref_close(head_ref2);
5716 return err;
5719 static const struct got_error *
5720 check_path_is_commitable(const char *path,
5721 struct got_pathlist_head *commitable_paths)
5723 struct got_pathlist_entry *cpe = NULL;
5724 size_t path_len = strlen(path);
5726 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5727 struct got_commitable *ct = cpe->data;
5728 const char *ct_path = ct->path;
5730 while (ct_path[0] == '/')
5731 ct_path++;
5733 if (strcmp(path, ct_path) == 0 ||
5734 got_path_is_child(ct_path, path, path_len))
5735 break;
5738 if (cpe == NULL)
5739 return got_error_path(path, GOT_ERR_BAD_PATH);
5741 return NULL;
5744 static const struct got_error *
5745 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5747 int *have_staged_files = arg;
5749 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5750 *have_staged_files = 1;
5751 return got_error(GOT_ERR_CANCELLED);
5754 return NULL;
5757 static const struct got_error *
5758 check_non_staged_files(struct got_fileindex *fileindex,
5759 struct got_pathlist_head *paths)
5761 struct got_pathlist_entry *pe;
5762 struct got_fileindex_entry *ie;
5764 TAILQ_FOREACH(pe, paths, entry) {
5765 if (pe->path[0] == '\0')
5766 continue;
5767 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5768 if (ie == NULL)
5769 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5770 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5771 return got_error_path(pe->path,
5772 GOT_ERR_FILE_NOT_STAGED);
5775 return NULL;
5778 const struct got_error *
5779 got_worktree_commit(struct got_object_id **new_commit_id,
5780 struct got_worktree *worktree, struct got_pathlist_head *paths,
5781 const char *author, const char *committer, int allow_bad_symlinks,
5782 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5783 got_worktree_status_cb status_cb, void *status_arg,
5784 struct got_repository *repo)
5786 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5787 struct got_fileindex *fileindex = NULL;
5788 char *fileindex_path = NULL;
5789 struct got_pathlist_head commitable_paths;
5790 struct collect_commitables_arg cc_arg;
5791 struct got_pathlist_entry *pe;
5792 struct got_reference *head_ref = NULL;
5793 struct got_object_id *head_commit_id = NULL;
5794 int have_staged_files = 0;
5796 *new_commit_id = NULL;
5798 TAILQ_INIT(&commitable_paths);
5800 err = lock_worktree(worktree, LOCK_EX);
5801 if (err)
5802 goto done;
5804 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5805 if (err)
5806 goto done;
5808 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5809 if (err)
5810 goto done;
5812 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5813 if (err)
5814 goto done;
5816 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5817 &have_staged_files);
5818 if (err && err->code != GOT_ERR_CANCELLED)
5819 goto done;
5820 if (have_staged_files) {
5821 err = check_non_staged_files(fileindex, paths);
5822 if (err)
5823 goto done;
5826 cc_arg.commitable_paths = &commitable_paths;
5827 cc_arg.worktree = worktree;
5828 cc_arg.fileindex = fileindex;
5829 cc_arg.repo = repo;
5830 cc_arg.have_staged_files = have_staged_files;
5831 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5832 TAILQ_FOREACH(pe, paths, entry) {
5833 err = worktree_status(worktree, pe->path, fileindex, repo,
5834 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5835 if (err)
5836 goto done;
5839 if (TAILQ_EMPTY(&commitable_paths)) {
5840 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5841 goto done;
5844 TAILQ_FOREACH(pe, paths, entry) {
5845 err = check_path_is_commitable(pe->path, &commitable_paths);
5846 if (err)
5847 goto done;
5850 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5851 struct got_commitable *ct = pe->data;
5852 const char *ct_path = ct->in_repo_path;
5854 while (ct_path[0] == '/')
5855 ct_path++;
5856 err = check_out_of_date(ct_path, ct->status,
5857 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5858 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5859 if (err)
5860 goto done;
5864 err = commit_worktree(new_commit_id, &commitable_paths,
5865 head_commit_id, worktree, author, committer,
5866 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5867 if (err)
5868 goto done;
5870 err = update_fileindex_after_commit(worktree, &commitable_paths,
5871 *new_commit_id, fileindex, have_staged_files);
5872 sync_err = sync_fileindex(fileindex, fileindex_path);
5873 if (sync_err && err == NULL)
5874 err = sync_err;
5875 done:
5876 if (fileindex)
5877 got_fileindex_free(fileindex);
5878 free(fileindex_path);
5879 unlockerr = lock_worktree(worktree, LOCK_SH);
5880 if (unlockerr && err == NULL)
5881 err = unlockerr;
5882 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5883 struct got_commitable *ct = pe->data;
5884 free_commitable(ct);
5886 got_pathlist_free(&commitable_paths);
5887 return err;
5890 const char *
5891 got_commitable_get_path(struct got_commitable *ct)
5893 return ct->path;
5896 unsigned int
5897 got_commitable_get_status(struct got_commitable *ct)
5899 return ct->status;
5902 struct check_rebase_ok_arg {
5903 struct got_worktree *worktree;
5904 struct got_repository *repo;
5907 static const struct got_error *
5908 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5910 const struct got_error *err = NULL;
5911 struct check_rebase_ok_arg *a = arg;
5912 unsigned char status;
5913 struct stat sb;
5914 char *ondisk_path;
5916 /* Reject rebase of a work tree with mixed base commits. */
5917 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5918 SHA1_DIGEST_LENGTH))
5919 return got_error(GOT_ERR_MIXED_COMMITS);
5921 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5922 == -1)
5923 return got_error_from_errno("asprintf");
5925 /* Reject rebase of a work tree with modified or staged files. */
5926 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5927 free(ondisk_path);
5928 if (err)
5929 return err;
5931 if (status != GOT_STATUS_NO_CHANGE)
5932 return got_error(GOT_ERR_MODIFIED);
5933 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5934 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5936 return NULL;
5939 const struct got_error *
5940 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5941 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5942 struct got_worktree *worktree, struct got_reference *branch,
5943 struct got_repository *repo)
5945 const struct got_error *err = NULL;
5946 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5947 char *branch_ref_name = NULL;
5948 char *fileindex_path = NULL;
5949 struct check_rebase_ok_arg ok_arg;
5950 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5951 struct got_object_id *wt_branch_tip = NULL;
5953 *new_base_branch_ref = NULL;
5954 *tmp_branch = NULL;
5955 *fileindex = NULL;
5957 err = lock_worktree(worktree, LOCK_EX);
5958 if (err)
5959 return err;
5961 err = open_fileindex(fileindex, &fileindex_path, worktree);
5962 if (err)
5963 goto done;
5965 ok_arg.worktree = worktree;
5966 ok_arg.repo = repo;
5967 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5968 &ok_arg);
5969 if (err)
5970 goto done;
5972 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5973 if (err)
5974 goto done;
5976 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5977 if (err)
5978 goto done;
5980 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5981 if (err)
5982 goto done;
5984 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5985 0);
5986 if (err)
5987 goto done;
5989 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5990 if (err)
5991 goto done;
5992 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5993 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5994 goto done;
5997 err = got_ref_alloc_symref(new_base_branch_ref,
5998 new_base_branch_ref_name, wt_branch);
5999 if (err)
6000 goto done;
6001 err = got_ref_write(*new_base_branch_ref, repo);
6002 if (err)
6003 goto done;
6005 /* TODO Lock original branch's ref while rebasing? */
6007 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6008 if (err)
6009 goto done;
6011 err = got_ref_write(branch_ref, repo);
6012 if (err)
6013 goto done;
6015 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6016 worktree->base_commit_id);
6017 if (err)
6018 goto done;
6019 err = got_ref_write(*tmp_branch, repo);
6020 if (err)
6021 goto done;
6023 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6024 if (err)
6025 goto done;
6026 done:
6027 free(fileindex_path);
6028 free(tmp_branch_name);
6029 free(new_base_branch_ref_name);
6030 free(branch_ref_name);
6031 if (branch_ref)
6032 got_ref_close(branch_ref);
6033 if (wt_branch)
6034 got_ref_close(wt_branch);
6035 free(wt_branch_tip);
6036 if (err) {
6037 if (*new_base_branch_ref) {
6038 got_ref_close(*new_base_branch_ref);
6039 *new_base_branch_ref = NULL;
6041 if (*tmp_branch) {
6042 got_ref_close(*tmp_branch);
6043 *tmp_branch = NULL;
6045 if (*fileindex) {
6046 got_fileindex_free(*fileindex);
6047 *fileindex = NULL;
6049 lock_worktree(worktree, LOCK_SH);
6051 return err;
6054 const struct got_error *
6055 got_worktree_rebase_continue(struct got_object_id **commit_id,
6056 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6057 struct got_reference **branch, struct got_fileindex **fileindex,
6058 struct got_worktree *worktree, struct got_repository *repo)
6060 const struct got_error *err;
6061 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6062 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6063 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6064 char *fileindex_path = NULL;
6065 int have_staged_files = 0;
6067 *commit_id = NULL;
6068 *new_base_branch = NULL;
6069 *tmp_branch = NULL;
6070 *branch = NULL;
6071 *fileindex = NULL;
6073 err = lock_worktree(worktree, LOCK_EX);
6074 if (err)
6075 return err;
6077 err = open_fileindex(fileindex, &fileindex_path, worktree);
6078 if (err)
6079 goto done;
6081 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6082 &have_staged_files);
6083 if (err && err->code != GOT_ERR_CANCELLED)
6084 goto done;
6085 if (have_staged_files) {
6086 err = got_error(GOT_ERR_STAGED_PATHS);
6087 goto done;
6090 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6091 if (err)
6092 goto done;
6094 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6095 if (err)
6096 goto done;
6098 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6099 if (err)
6100 goto done;
6102 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6103 if (err)
6104 goto done;
6106 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6107 if (err)
6108 goto done;
6110 err = got_ref_open(branch, repo,
6111 got_ref_get_symref_target(branch_ref), 0);
6112 if (err)
6113 goto done;
6115 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6116 if (err)
6117 goto done;
6119 err = got_ref_resolve(commit_id, repo, commit_ref);
6120 if (err)
6121 goto done;
6123 err = got_ref_open(new_base_branch, repo,
6124 new_base_branch_ref_name, 0);
6125 if (err)
6126 goto done;
6128 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6129 if (err)
6130 goto done;
6131 done:
6132 free(commit_ref_name);
6133 free(branch_ref_name);
6134 free(fileindex_path);
6135 if (commit_ref)
6136 got_ref_close(commit_ref);
6137 if (branch_ref)
6138 got_ref_close(branch_ref);
6139 if (err) {
6140 free(*commit_id);
6141 *commit_id = NULL;
6142 if (*tmp_branch) {
6143 got_ref_close(*tmp_branch);
6144 *tmp_branch = NULL;
6146 if (*new_base_branch) {
6147 got_ref_close(*new_base_branch);
6148 *new_base_branch = NULL;
6150 if (*branch) {
6151 got_ref_close(*branch);
6152 *branch = NULL;
6154 if (*fileindex) {
6155 got_fileindex_free(*fileindex);
6156 *fileindex = NULL;
6158 lock_worktree(worktree, LOCK_SH);
6160 return err;
6163 const struct got_error *
6164 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6166 const struct got_error *err;
6167 char *tmp_branch_name = NULL;
6169 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6170 if (err)
6171 return err;
6173 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6174 free(tmp_branch_name);
6175 return NULL;
6178 static const struct got_error *
6179 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6180 char **logmsg, void *arg)
6182 *logmsg = arg;
6183 return NULL;
6186 static const struct got_error *
6187 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6188 const char *path, struct got_object_id *blob_id,
6189 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6190 int dirfd, const char *de_name)
6192 return NULL;
6195 struct collect_merged_paths_arg {
6196 got_worktree_checkout_cb progress_cb;
6197 void *progress_arg;
6198 struct got_pathlist_head *merged_paths;
6201 static const struct got_error *
6202 collect_merged_paths(void *arg, unsigned char status, const char *path)
6204 const struct got_error *err;
6205 struct collect_merged_paths_arg *a = arg;
6206 char *p;
6207 struct got_pathlist_entry *new;
6209 err = (*a->progress_cb)(a->progress_arg, status, path);
6210 if (err)
6211 return err;
6213 if (status != GOT_STATUS_MERGE &&
6214 status != GOT_STATUS_ADD &&
6215 status != GOT_STATUS_DELETE &&
6216 status != GOT_STATUS_CONFLICT)
6217 return NULL;
6219 p = strdup(path);
6220 if (p == NULL)
6221 return got_error_from_errno("strdup");
6223 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6224 if (err || new == NULL)
6225 free(p);
6226 return err;
6229 void
6230 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6232 struct got_pathlist_entry *pe;
6234 TAILQ_FOREACH(pe, merged_paths, entry)
6235 free((char *)pe->path);
6237 got_pathlist_free(merged_paths);
6240 static const struct got_error *
6241 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6242 int is_rebase, struct got_repository *repo)
6244 const struct got_error *err;
6245 struct got_reference *commit_ref = NULL;
6247 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6248 if (err) {
6249 if (err->code != GOT_ERR_NOT_REF)
6250 goto done;
6251 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6252 if (err)
6253 goto done;
6254 err = got_ref_write(commit_ref, repo);
6255 if (err)
6256 goto done;
6257 } else if (is_rebase) {
6258 struct got_object_id *stored_id;
6259 int cmp;
6261 err = got_ref_resolve(&stored_id, repo, commit_ref);
6262 if (err)
6263 goto done;
6264 cmp = got_object_id_cmp(commit_id, stored_id);
6265 free(stored_id);
6266 if (cmp != 0) {
6267 err = got_error(GOT_ERR_REBASE_COMMITID);
6268 goto done;
6271 done:
6272 if (commit_ref)
6273 got_ref_close(commit_ref);
6274 return err;
6277 static const struct got_error *
6278 rebase_merge_files(struct got_pathlist_head *merged_paths,
6279 const char *commit_ref_name, struct got_worktree *worktree,
6280 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6281 struct got_object_id *commit_id, struct got_repository *repo,
6282 got_worktree_checkout_cb progress_cb, void *progress_arg,
6283 got_cancel_cb cancel_cb, void *cancel_arg)
6285 const struct got_error *err;
6286 struct got_reference *commit_ref = NULL;
6287 struct collect_merged_paths_arg cmp_arg;
6288 char *fileindex_path;
6290 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6292 err = get_fileindex_path(&fileindex_path, worktree);
6293 if (err)
6294 return err;
6296 cmp_arg.progress_cb = progress_cb;
6297 cmp_arg.progress_arg = progress_arg;
6298 cmp_arg.merged_paths = merged_paths;
6299 err = merge_files(worktree, fileindex, fileindex_path,
6300 parent_commit_id, commit_id, repo, collect_merged_paths,
6301 &cmp_arg, cancel_cb, cancel_arg);
6302 if (commit_ref)
6303 got_ref_close(commit_ref);
6304 return err;
6307 const struct got_error *
6308 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6309 struct got_worktree *worktree, struct got_fileindex *fileindex,
6310 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6311 struct got_repository *repo,
6312 got_worktree_checkout_cb progress_cb, void *progress_arg,
6313 got_cancel_cb cancel_cb, void *cancel_arg)
6315 const struct got_error *err;
6316 char *commit_ref_name;
6318 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6319 if (err)
6320 return err;
6322 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6323 if (err)
6324 goto done;
6326 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6327 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6328 progress_arg, cancel_cb, cancel_arg);
6329 done:
6330 free(commit_ref_name);
6331 return err;
6334 const struct got_error *
6335 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6336 struct got_worktree *worktree, struct got_fileindex *fileindex,
6337 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6338 struct got_repository *repo,
6339 got_worktree_checkout_cb progress_cb, void *progress_arg,
6340 got_cancel_cb cancel_cb, void *cancel_arg)
6342 const struct got_error *err;
6343 char *commit_ref_name;
6345 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6346 if (err)
6347 return err;
6349 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6350 if (err)
6351 goto done;
6353 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6354 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6355 progress_arg, cancel_cb, cancel_arg);
6356 done:
6357 free(commit_ref_name);
6358 return err;
6361 static const struct got_error *
6362 rebase_commit(struct got_object_id **new_commit_id,
6363 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6364 struct got_worktree *worktree, struct got_fileindex *fileindex,
6365 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6366 const char *new_logmsg, struct got_repository *repo)
6368 const struct got_error *err, *sync_err;
6369 struct got_pathlist_head commitable_paths;
6370 struct collect_commitables_arg cc_arg;
6371 char *fileindex_path = NULL;
6372 struct got_reference *head_ref = NULL;
6373 struct got_object_id *head_commit_id = NULL;
6374 char *logmsg = NULL;
6376 TAILQ_INIT(&commitable_paths);
6377 *new_commit_id = NULL;
6379 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6381 err = get_fileindex_path(&fileindex_path, worktree);
6382 if (err)
6383 return err;
6385 cc_arg.commitable_paths = &commitable_paths;
6386 cc_arg.worktree = worktree;
6387 cc_arg.repo = repo;
6388 cc_arg.have_staged_files = 0;
6390 * If possible get the status of individual files directly to
6391 * avoid crawling the entire work tree once per rebased commit.
6393 * Ideally, merged_paths would contain a list of commitables
6394 * we could use so we could skip worktree_status() entirely.
6395 * However, we would then need carefully keep track of cumulative
6396 * effects of operations such as file additions and deletions
6397 * in 'got histedit -f' (folding multiple commits into one),
6398 * and this extra complexity is not really worth it.
6400 if (merged_paths) {
6401 struct got_pathlist_entry *pe;
6402 TAILQ_FOREACH(pe, merged_paths, entry) {
6403 err = worktree_status(worktree, pe->path, fileindex,
6404 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6405 0);
6406 if (err)
6407 goto done;
6409 } else {
6410 err = worktree_status(worktree, "", fileindex, repo,
6411 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6412 if (err)
6413 goto done;
6416 if (TAILQ_EMPTY(&commitable_paths)) {
6417 /* No-op change; commit will be elided. */
6418 err = got_ref_delete(commit_ref, repo);
6419 if (err)
6420 goto done;
6421 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6422 goto done;
6425 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6426 if (err)
6427 goto done;
6429 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6430 if (err)
6431 goto done;
6433 if (new_logmsg) {
6434 logmsg = strdup(new_logmsg);
6435 if (logmsg == NULL) {
6436 err = got_error_from_errno("strdup");
6437 goto done;
6439 } else {
6440 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6441 if (err)
6442 goto done;
6445 /* NB: commit_worktree will call free(logmsg) */
6446 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6447 worktree, got_object_commit_get_author(orig_commit),
6448 got_object_commit_get_committer(orig_commit),
6449 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6450 if (err)
6451 goto done;
6453 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6454 if (err)
6455 goto done;
6457 err = got_ref_delete(commit_ref, repo);
6458 if (err)
6459 goto done;
6461 err = update_fileindex_after_commit(worktree, &commitable_paths,
6462 *new_commit_id, fileindex, 0);
6463 sync_err = sync_fileindex(fileindex, fileindex_path);
6464 if (sync_err && err == NULL)
6465 err = sync_err;
6466 done:
6467 free(fileindex_path);
6468 free(head_commit_id);
6469 if (head_ref)
6470 got_ref_close(head_ref);
6471 if (err) {
6472 free(*new_commit_id);
6473 *new_commit_id = NULL;
6475 return err;
6478 const struct got_error *
6479 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6480 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6481 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6482 struct got_commit_object *orig_commit,
6483 struct got_object_id *orig_commit_id, struct got_repository *repo)
6485 const struct got_error *err;
6486 char *commit_ref_name;
6487 struct got_reference *commit_ref = NULL;
6488 struct got_object_id *commit_id = NULL;
6490 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6491 if (err)
6492 return err;
6494 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6495 if (err)
6496 goto done;
6497 err = got_ref_resolve(&commit_id, repo, commit_ref);
6498 if (err)
6499 goto done;
6500 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6501 err = got_error(GOT_ERR_REBASE_COMMITID);
6502 goto done;
6505 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6506 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6507 done:
6508 if (commit_ref)
6509 got_ref_close(commit_ref);
6510 free(commit_ref_name);
6511 free(commit_id);
6512 return err;
6515 const struct got_error *
6516 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6517 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6518 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6519 struct got_commit_object *orig_commit,
6520 struct got_object_id *orig_commit_id, const char *new_logmsg,
6521 struct got_repository *repo)
6523 const struct got_error *err;
6524 char *commit_ref_name;
6525 struct got_reference *commit_ref = NULL;
6527 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6528 if (err)
6529 return err;
6531 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6532 if (err)
6533 goto done;
6535 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6536 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6537 done:
6538 if (commit_ref)
6539 got_ref_close(commit_ref);
6540 free(commit_ref_name);
6541 return err;
6544 const struct got_error *
6545 got_worktree_rebase_postpone(struct got_worktree *worktree,
6546 struct got_fileindex *fileindex)
6548 if (fileindex)
6549 got_fileindex_free(fileindex);
6550 return lock_worktree(worktree, LOCK_SH);
6553 static const struct got_error *
6554 delete_ref(const char *name, struct got_repository *repo)
6556 const struct got_error *err;
6557 struct got_reference *ref;
6559 err = got_ref_open(&ref, repo, name, 0);
6560 if (err) {
6561 if (err->code == GOT_ERR_NOT_REF)
6562 return NULL;
6563 return err;
6566 err = got_ref_delete(ref, repo);
6567 got_ref_close(ref);
6568 return err;
6571 static const struct got_error *
6572 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6574 const struct got_error *err;
6575 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6576 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6578 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6579 if (err)
6580 goto done;
6581 err = delete_ref(tmp_branch_name, repo);
6582 if (err)
6583 goto done;
6585 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6586 if (err)
6587 goto done;
6588 err = delete_ref(new_base_branch_ref_name, repo);
6589 if (err)
6590 goto done;
6592 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6593 if (err)
6594 goto done;
6595 err = delete_ref(branch_ref_name, repo);
6596 if (err)
6597 goto done;
6599 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6600 if (err)
6601 goto done;
6602 err = delete_ref(commit_ref_name, repo);
6603 if (err)
6604 goto done;
6606 done:
6607 free(tmp_branch_name);
6608 free(new_base_branch_ref_name);
6609 free(branch_ref_name);
6610 free(commit_ref_name);
6611 return err;
6614 const struct got_error *
6615 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6616 struct got_object_id *new_commit_id, struct got_repository *repo)
6618 const struct got_error *err;
6619 struct got_reference *ref = NULL;
6620 struct got_object_id *old_commit_id = NULL;
6621 const char *branch_name = NULL;
6622 char *new_id_str = NULL;
6623 char *refname = NULL;
6625 branch_name = got_ref_get_name(branch);
6626 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6627 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6628 branch_name += 11;
6630 err = got_object_id_str(&new_id_str, new_commit_id);
6631 if (err)
6632 return err;
6634 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6635 new_id_str) == -1) {
6636 err = got_error_from_errno("asprintf");
6637 goto done;
6640 err = got_ref_resolve(&old_commit_id, repo, branch);
6641 if (err)
6642 goto done;
6644 err = got_ref_alloc(&ref, refname, old_commit_id);
6645 if (err)
6646 goto done;
6648 err = got_ref_write(ref, repo);
6649 done:
6650 free(new_id_str);
6651 free(refname);
6652 free(old_commit_id);
6653 if (ref)
6654 got_ref_close(ref);
6655 return err;
6658 const struct got_error *
6659 got_worktree_rebase_complete(struct got_worktree *worktree,
6660 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6661 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6662 struct got_repository *repo, int create_backup)
6664 const struct got_error *err, *unlockerr, *sync_err;
6665 struct got_object_id *new_head_commit_id = NULL;
6666 char *fileindex_path = NULL;
6668 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6669 if (err)
6670 return err;
6672 if (create_backup) {
6673 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6674 rebased_branch, new_head_commit_id, repo);
6675 if (err)
6676 goto done;
6679 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6680 if (err)
6681 goto done;
6683 err = got_ref_write(rebased_branch, repo);
6684 if (err)
6685 goto done;
6687 err = got_worktree_set_head_ref(worktree, rebased_branch);
6688 if (err)
6689 goto done;
6691 err = delete_rebase_refs(worktree, repo);
6692 if (err)
6693 goto done;
6695 err = get_fileindex_path(&fileindex_path, worktree);
6696 if (err)
6697 goto done;
6698 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6699 sync_err = sync_fileindex(fileindex, fileindex_path);
6700 if (sync_err && err == NULL)
6701 err = sync_err;
6702 done:
6703 got_fileindex_free(fileindex);
6704 free(fileindex_path);
6705 free(new_head_commit_id);
6706 unlockerr = lock_worktree(worktree, LOCK_SH);
6707 if (unlockerr && err == NULL)
6708 err = unlockerr;
6709 return err;
6712 const struct got_error *
6713 got_worktree_rebase_abort(struct got_worktree *worktree,
6714 struct got_fileindex *fileindex, struct got_repository *repo,
6715 struct got_reference *new_base_branch,
6716 got_worktree_checkout_cb progress_cb, void *progress_arg)
6718 const struct got_error *err, *unlockerr, *sync_err;
6719 struct got_reference *resolved = NULL;
6720 struct got_object_id *commit_id = NULL;
6721 char *fileindex_path = NULL;
6722 struct revert_file_args rfa;
6723 struct got_object_id *tree_id = NULL;
6725 err = lock_worktree(worktree, LOCK_EX);
6726 if (err)
6727 return err;
6729 err = got_ref_open(&resolved, repo,
6730 got_ref_get_symref_target(new_base_branch), 0);
6731 if (err)
6732 goto done;
6734 err = got_worktree_set_head_ref(worktree, resolved);
6735 if (err)
6736 goto done;
6739 * XXX commits to the base branch could have happened while
6740 * we were busy rebasing; should we store the original commit ID
6741 * when rebase begins and read it back here?
6743 err = got_ref_resolve(&commit_id, repo, resolved);
6744 if (err)
6745 goto done;
6747 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6748 if (err)
6749 goto done;
6751 err = got_object_id_by_path(&tree_id, repo,
6752 worktree->base_commit_id, worktree->path_prefix);
6753 if (err)
6754 goto done;
6756 err = delete_rebase_refs(worktree, repo);
6757 if (err)
6758 goto done;
6760 err = get_fileindex_path(&fileindex_path, worktree);
6761 if (err)
6762 goto done;
6764 rfa.worktree = worktree;
6765 rfa.fileindex = fileindex;
6766 rfa.progress_cb = progress_cb;
6767 rfa.progress_arg = progress_arg;
6768 rfa.patch_cb = NULL;
6769 rfa.patch_arg = NULL;
6770 rfa.repo = repo;
6771 err = worktree_status(worktree, "", fileindex, repo,
6772 revert_file, &rfa, NULL, NULL, 0, 0);
6773 if (err)
6774 goto sync;
6776 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6777 repo, progress_cb, progress_arg, NULL, NULL);
6778 sync:
6779 sync_err = sync_fileindex(fileindex, fileindex_path);
6780 if (sync_err && err == NULL)
6781 err = sync_err;
6782 done:
6783 got_ref_close(resolved);
6784 free(tree_id);
6785 free(commit_id);
6786 if (fileindex)
6787 got_fileindex_free(fileindex);
6788 free(fileindex_path);
6790 unlockerr = lock_worktree(worktree, LOCK_SH);
6791 if (unlockerr && err == NULL)
6792 err = unlockerr;
6793 return err;
6796 const struct got_error *
6797 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6798 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6799 struct got_fileindex **fileindex, struct got_worktree *worktree,
6800 struct got_repository *repo)
6802 const struct got_error *err = NULL;
6803 char *tmp_branch_name = NULL;
6804 char *branch_ref_name = NULL;
6805 char *base_commit_ref_name = NULL;
6806 char *fileindex_path = NULL;
6807 struct check_rebase_ok_arg ok_arg;
6808 struct got_reference *wt_branch = NULL;
6809 struct got_reference *base_commit_ref = NULL;
6811 *tmp_branch = NULL;
6812 *branch_ref = NULL;
6813 *base_commit_id = NULL;
6814 *fileindex = NULL;
6816 err = lock_worktree(worktree, LOCK_EX);
6817 if (err)
6818 return err;
6820 err = open_fileindex(fileindex, &fileindex_path, worktree);
6821 if (err)
6822 goto done;
6824 ok_arg.worktree = worktree;
6825 ok_arg.repo = repo;
6826 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6827 &ok_arg);
6828 if (err)
6829 goto done;
6831 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6832 if (err)
6833 goto done;
6835 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6836 if (err)
6837 goto done;
6839 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6840 worktree);
6841 if (err)
6842 goto done;
6844 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6845 0);
6846 if (err)
6847 goto done;
6849 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6850 if (err)
6851 goto done;
6853 err = got_ref_write(*branch_ref, repo);
6854 if (err)
6855 goto done;
6857 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6858 worktree->base_commit_id);
6859 if (err)
6860 goto done;
6861 err = got_ref_write(base_commit_ref, repo);
6862 if (err)
6863 goto done;
6864 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6865 if (*base_commit_id == NULL) {
6866 err = got_error_from_errno("got_object_id_dup");
6867 goto done;
6870 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6871 worktree->base_commit_id);
6872 if (err)
6873 goto done;
6874 err = got_ref_write(*tmp_branch, repo);
6875 if (err)
6876 goto done;
6878 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6879 if (err)
6880 goto done;
6881 done:
6882 free(fileindex_path);
6883 free(tmp_branch_name);
6884 free(branch_ref_name);
6885 free(base_commit_ref_name);
6886 if (wt_branch)
6887 got_ref_close(wt_branch);
6888 if (err) {
6889 if (*branch_ref) {
6890 got_ref_close(*branch_ref);
6891 *branch_ref = NULL;
6893 if (*tmp_branch) {
6894 got_ref_close(*tmp_branch);
6895 *tmp_branch = NULL;
6897 free(*base_commit_id);
6898 if (*fileindex) {
6899 got_fileindex_free(*fileindex);
6900 *fileindex = NULL;
6902 lock_worktree(worktree, LOCK_SH);
6904 return err;
6907 const struct got_error *
6908 got_worktree_histedit_postpone(struct got_worktree *worktree,
6909 struct got_fileindex *fileindex)
6911 if (fileindex)
6912 got_fileindex_free(fileindex);
6913 return lock_worktree(worktree, LOCK_SH);
6916 const struct got_error *
6917 got_worktree_histedit_in_progress(int *in_progress,
6918 struct got_worktree *worktree)
6920 const struct got_error *err;
6921 char *tmp_branch_name = NULL;
6923 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6924 if (err)
6925 return err;
6927 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6928 free(tmp_branch_name);
6929 return NULL;
6932 const struct got_error *
6933 got_worktree_histedit_continue(struct got_object_id **commit_id,
6934 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6935 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6936 struct got_worktree *worktree, struct got_repository *repo)
6938 const struct got_error *err;
6939 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6940 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6941 struct got_reference *commit_ref = NULL;
6942 struct got_reference *base_commit_ref = NULL;
6943 char *fileindex_path = NULL;
6944 int have_staged_files = 0;
6946 *commit_id = NULL;
6947 *tmp_branch = NULL;
6948 *base_commit_id = NULL;
6949 *fileindex = NULL;
6951 err = lock_worktree(worktree, LOCK_EX);
6952 if (err)
6953 return err;
6955 err = open_fileindex(fileindex, &fileindex_path, worktree);
6956 if (err)
6957 goto done;
6959 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6960 &have_staged_files);
6961 if (err && err->code != GOT_ERR_CANCELLED)
6962 goto done;
6963 if (have_staged_files) {
6964 err = got_error(GOT_ERR_STAGED_PATHS);
6965 goto done;
6968 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6969 if (err)
6970 goto done;
6972 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6973 if (err)
6974 goto done;
6976 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6977 if (err)
6978 goto done;
6980 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6981 worktree);
6982 if (err)
6983 goto done;
6985 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6986 if (err)
6987 goto done;
6989 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6990 if (err)
6991 goto done;
6992 err = got_ref_resolve(commit_id, repo, commit_ref);
6993 if (err)
6994 goto done;
6996 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6997 if (err)
6998 goto done;
6999 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7000 if (err)
7001 goto done;
7003 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7004 if (err)
7005 goto done;
7006 done:
7007 free(commit_ref_name);
7008 free(branch_ref_name);
7009 free(fileindex_path);
7010 if (commit_ref)
7011 got_ref_close(commit_ref);
7012 if (base_commit_ref)
7013 got_ref_close(base_commit_ref);
7014 if (err) {
7015 free(*commit_id);
7016 *commit_id = NULL;
7017 free(*base_commit_id);
7018 *base_commit_id = NULL;
7019 if (*tmp_branch) {
7020 got_ref_close(*tmp_branch);
7021 *tmp_branch = NULL;
7023 if (*fileindex) {
7024 got_fileindex_free(*fileindex);
7025 *fileindex = NULL;
7027 lock_worktree(worktree, LOCK_EX);
7029 return err;
7032 static const struct got_error *
7033 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7035 const struct got_error *err;
7036 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7037 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7039 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7040 if (err)
7041 goto done;
7042 err = delete_ref(tmp_branch_name, repo);
7043 if (err)
7044 goto done;
7046 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7047 worktree);
7048 if (err)
7049 goto done;
7050 err = delete_ref(base_commit_ref_name, repo);
7051 if (err)
7052 goto done;
7054 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7055 if (err)
7056 goto done;
7057 err = delete_ref(branch_ref_name, repo);
7058 if (err)
7059 goto done;
7061 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7062 if (err)
7063 goto done;
7064 err = delete_ref(commit_ref_name, repo);
7065 if (err)
7066 goto done;
7067 done:
7068 free(tmp_branch_name);
7069 free(base_commit_ref_name);
7070 free(branch_ref_name);
7071 free(commit_ref_name);
7072 return err;
7075 const struct got_error *
7076 got_worktree_histedit_abort(struct got_worktree *worktree,
7077 struct got_fileindex *fileindex, struct got_repository *repo,
7078 struct got_reference *branch, struct got_object_id *base_commit_id,
7079 got_worktree_checkout_cb progress_cb, void *progress_arg)
7081 const struct got_error *err, *unlockerr, *sync_err;
7082 struct got_reference *resolved = NULL;
7083 char *fileindex_path = NULL;
7084 struct got_object_id *tree_id = NULL;
7085 struct revert_file_args rfa;
7087 err = lock_worktree(worktree, LOCK_EX);
7088 if (err)
7089 return err;
7091 err = got_ref_open(&resolved, repo,
7092 got_ref_get_symref_target(branch), 0);
7093 if (err)
7094 goto done;
7096 err = got_worktree_set_head_ref(worktree, resolved);
7097 if (err)
7098 goto done;
7100 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7101 if (err)
7102 goto done;
7104 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7105 worktree->path_prefix);
7106 if (err)
7107 goto done;
7109 err = delete_histedit_refs(worktree, repo);
7110 if (err)
7111 goto done;
7113 err = get_fileindex_path(&fileindex_path, worktree);
7114 if (err)
7115 goto done;
7117 rfa.worktree = worktree;
7118 rfa.fileindex = fileindex;
7119 rfa.progress_cb = progress_cb;
7120 rfa.progress_arg = progress_arg;
7121 rfa.patch_cb = NULL;
7122 rfa.patch_arg = NULL;
7123 rfa.repo = repo;
7124 err = worktree_status(worktree, "", fileindex, repo,
7125 revert_file, &rfa, NULL, NULL, 0, 0);
7126 if (err)
7127 goto sync;
7129 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7130 repo, progress_cb, progress_arg, NULL, NULL);
7131 sync:
7132 sync_err = sync_fileindex(fileindex, fileindex_path);
7133 if (sync_err && err == NULL)
7134 err = sync_err;
7135 done:
7136 got_ref_close(resolved);
7137 free(tree_id);
7138 free(fileindex_path);
7140 unlockerr = lock_worktree(worktree, LOCK_SH);
7141 if (unlockerr && err == NULL)
7142 err = unlockerr;
7143 return err;
7146 const struct got_error *
7147 got_worktree_histedit_complete(struct got_worktree *worktree,
7148 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7149 struct got_reference *edited_branch, struct got_repository *repo)
7151 const struct got_error *err, *unlockerr, *sync_err;
7152 struct got_object_id *new_head_commit_id = NULL;
7153 struct got_reference *resolved = NULL;
7154 char *fileindex_path = NULL;
7156 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7157 if (err)
7158 return err;
7160 err = got_ref_open(&resolved, repo,
7161 got_ref_get_symref_target(edited_branch), 0);
7162 if (err)
7163 goto done;
7165 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7166 resolved, new_head_commit_id, repo);
7167 if (err)
7168 goto done;
7170 err = got_ref_change_ref(resolved, new_head_commit_id);
7171 if (err)
7172 goto done;
7174 err = got_ref_write(resolved, repo);
7175 if (err)
7176 goto done;
7178 err = got_worktree_set_head_ref(worktree, resolved);
7179 if (err)
7180 goto done;
7182 err = delete_histedit_refs(worktree, repo);
7183 if (err)
7184 goto done;
7186 err = get_fileindex_path(&fileindex_path, worktree);
7187 if (err)
7188 goto done;
7189 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7190 sync_err = sync_fileindex(fileindex, fileindex_path);
7191 if (sync_err && err == NULL)
7192 err = sync_err;
7193 done:
7194 got_fileindex_free(fileindex);
7195 free(fileindex_path);
7196 free(new_head_commit_id);
7197 unlockerr = lock_worktree(worktree, LOCK_SH);
7198 if (unlockerr && err == NULL)
7199 err = unlockerr;
7200 return err;
7203 const struct got_error *
7204 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7205 struct got_object_id *commit_id, struct got_repository *repo)
7207 const struct got_error *err;
7208 char *commit_ref_name;
7210 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7211 if (err)
7212 return err;
7214 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7215 if (err)
7216 goto done;
7218 err = delete_ref(commit_ref_name, repo);
7219 done:
7220 free(commit_ref_name);
7221 return err;
7224 const struct got_error *
7225 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7226 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7227 struct got_worktree *worktree, const char *refname,
7228 struct got_repository *repo)
7230 const struct got_error *err = NULL;
7231 char *fileindex_path = NULL;
7232 struct check_rebase_ok_arg ok_arg;
7234 *fileindex = NULL;
7235 *branch_ref = NULL;
7236 *base_branch_ref = NULL;
7238 err = lock_worktree(worktree, LOCK_EX);
7239 if (err)
7240 return err;
7242 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7243 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7244 "cannot integrate a branch into itself; "
7245 "update -b or different branch name required");
7246 goto done;
7249 err = open_fileindex(fileindex, &fileindex_path, worktree);
7250 if (err)
7251 goto done;
7253 /* Preconditions are the same as for rebase. */
7254 ok_arg.worktree = worktree;
7255 ok_arg.repo = repo;
7256 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7257 &ok_arg);
7258 if (err)
7259 goto done;
7261 err = got_ref_open(branch_ref, repo, refname, 1);
7262 if (err)
7263 goto done;
7265 err = got_ref_open(base_branch_ref, repo,
7266 got_worktree_get_head_ref_name(worktree), 1);
7267 done:
7268 if (err) {
7269 if (*branch_ref) {
7270 got_ref_close(*branch_ref);
7271 *branch_ref = NULL;
7273 if (*base_branch_ref) {
7274 got_ref_close(*base_branch_ref);
7275 *base_branch_ref = NULL;
7277 if (*fileindex) {
7278 got_fileindex_free(*fileindex);
7279 *fileindex = NULL;
7281 lock_worktree(worktree, LOCK_SH);
7283 return err;
7286 const struct got_error *
7287 got_worktree_integrate_continue(struct got_worktree *worktree,
7288 struct got_fileindex *fileindex, struct got_repository *repo,
7289 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7290 got_worktree_checkout_cb progress_cb, void *progress_arg,
7291 got_cancel_cb cancel_cb, void *cancel_arg)
7293 const struct got_error *err = NULL, *sync_err, *unlockerr;
7294 char *fileindex_path = NULL;
7295 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7297 err = get_fileindex_path(&fileindex_path, worktree);
7298 if (err)
7299 goto done;
7301 err = got_ref_resolve(&commit_id, repo, branch_ref);
7302 if (err)
7303 goto done;
7305 err = got_object_id_by_path(&tree_id, repo, commit_id,
7306 worktree->path_prefix);
7307 if (err)
7308 goto done;
7310 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7311 if (err)
7312 goto done;
7314 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7315 progress_cb, progress_arg, cancel_cb, cancel_arg);
7316 if (err)
7317 goto sync;
7319 err = got_ref_change_ref(base_branch_ref, commit_id);
7320 if (err)
7321 goto sync;
7323 err = got_ref_write(base_branch_ref, repo);
7324 if (err)
7325 goto sync;
7327 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7328 sync:
7329 sync_err = sync_fileindex(fileindex, fileindex_path);
7330 if (sync_err && err == NULL)
7331 err = sync_err;
7333 done:
7334 unlockerr = got_ref_unlock(branch_ref);
7335 if (unlockerr && err == NULL)
7336 err = unlockerr;
7337 got_ref_close(branch_ref);
7339 unlockerr = got_ref_unlock(base_branch_ref);
7340 if (unlockerr && err == NULL)
7341 err = unlockerr;
7342 got_ref_close(base_branch_ref);
7344 got_fileindex_free(fileindex);
7345 free(fileindex_path);
7346 free(tree_id);
7348 unlockerr = lock_worktree(worktree, LOCK_SH);
7349 if (unlockerr && err == NULL)
7350 err = unlockerr;
7351 return err;
7354 const struct got_error *
7355 got_worktree_integrate_abort(struct got_worktree *worktree,
7356 struct got_fileindex *fileindex, struct got_repository *repo,
7357 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7359 const struct got_error *err = NULL, *unlockerr = NULL;
7361 got_fileindex_free(fileindex);
7363 err = lock_worktree(worktree, LOCK_SH);
7365 unlockerr = got_ref_unlock(branch_ref);
7366 if (unlockerr && err == NULL)
7367 err = unlockerr;
7368 got_ref_close(branch_ref);
7370 unlockerr = got_ref_unlock(base_branch_ref);
7371 if (unlockerr && err == NULL)
7372 err = unlockerr;
7373 got_ref_close(base_branch_ref);
7375 return err;
7378 struct check_stage_ok_arg {
7379 struct got_object_id *head_commit_id;
7380 struct got_worktree *worktree;
7381 struct got_fileindex *fileindex;
7382 struct got_repository *repo;
7383 int have_changes;
7386 const struct got_error *
7387 check_stage_ok(void *arg, unsigned char status,
7388 unsigned char staged_status, const char *relpath,
7389 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7390 struct got_object_id *commit_id, int dirfd, const char *de_name)
7392 struct check_stage_ok_arg *a = arg;
7393 const struct got_error *err = NULL;
7394 struct got_fileindex_entry *ie;
7395 struct got_object_id base_commit_id;
7396 struct got_object_id *base_commit_idp = NULL;
7397 char *in_repo_path = NULL, *p;
7399 if (status == GOT_STATUS_UNVERSIONED ||
7400 status == GOT_STATUS_NO_CHANGE)
7401 return NULL;
7402 if (status == GOT_STATUS_NONEXISTENT)
7403 return got_error_set_errno(ENOENT, relpath);
7405 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7406 if (ie == NULL)
7407 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7409 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7410 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7411 relpath) == -1)
7412 return got_error_from_errno("asprintf");
7414 if (got_fileindex_entry_has_commit(ie)) {
7415 memcpy(base_commit_id.sha1, ie->commit_sha1,
7416 SHA1_DIGEST_LENGTH);
7417 base_commit_idp = &base_commit_id;
7420 if (status == GOT_STATUS_CONFLICT) {
7421 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7422 goto done;
7423 } else if (status != GOT_STATUS_ADD &&
7424 status != GOT_STATUS_MODIFY &&
7425 status != GOT_STATUS_DELETE) {
7426 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7427 goto done;
7430 a->have_changes = 1;
7432 p = in_repo_path;
7433 while (p[0] == '/')
7434 p++;
7435 err = check_out_of_date(p, status, staged_status,
7436 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7437 GOT_ERR_STAGE_OUT_OF_DATE);
7438 done:
7439 free(in_repo_path);
7440 return err;
7443 struct stage_path_arg {
7444 struct got_worktree *worktree;
7445 struct got_fileindex *fileindex;
7446 struct got_repository *repo;
7447 got_worktree_status_cb status_cb;
7448 void *status_arg;
7449 got_worktree_patch_cb patch_cb;
7450 void *patch_arg;
7451 int staged_something;
7452 int allow_bad_symlinks;
7455 static const struct got_error *
7456 stage_path(void *arg, unsigned char status,
7457 unsigned char staged_status, const char *relpath,
7458 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7459 struct got_object_id *commit_id, int dirfd, const char *de_name)
7461 struct stage_path_arg *a = arg;
7462 const struct got_error *err = NULL;
7463 struct got_fileindex_entry *ie;
7464 char *ondisk_path = NULL, *path_content = NULL;
7465 uint32_t stage;
7466 struct got_object_id *new_staged_blob_id = NULL;
7467 struct stat sb;
7469 if (status == GOT_STATUS_UNVERSIONED)
7470 return NULL;
7472 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7473 if (ie == NULL)
7474 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7476 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7477 relpath)== -1)
7478 return got_error_from_errno("asprintf");
7480 switch (status) {
7481 case GOT_STATUS_ADD:
7482 case GOT_STATUS_MODIFY:
7483 /* XXX could sb.st_mode be passed in by our caller? */
7484 if (lstat(ondisk_path, &sb) == -1) {
7485 err = got_error_from_errno2("lstat", ondisk_path);
7486 break;
7488 if (a->patch_cb) {
7489 if (status == GOT_STATUS_ADD) {
7490 int choice = GOT_PATCH_CHOICE_NONE;
7491 err = (*a->patch_cb)(&choice, a->patch_arg,
7492 status, ie->path, NULL, 1, 1);
7493 if (err)
7494 break;
7495 if (choice != GOT_PATCH_CHOICE_YES)
7496 break;
7497 } else {
7498 err = create_patched_content(&path_content, 0,
7499 staged_blob_id ? staged_blob_id : blob_id,
7500 ondisk_path, dirfd, de_name, ie->path,
7501 a->repo, a->patch_cb, a->patch_arg);
7502 if (err || path_content == NULL)
7503 break;
7506 err = got_object_blob_create(&new_staged_blob_id,
7507 path_content ? path_content : ondisk_path, a->repo);
7508 if (err)
7509 break;
7510 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7511 SHA1_DIGEST_LENGTH);
7512 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7513 stage = GOT_FILEIDX_STAGE_ADD;
7514 else
7515 stage = GOT_FILEIDX_STAGE_MODIFY;
7516 got_fileindex_entry_stage_set(ie, stage);
7517 if (S_ISLNK(sb.st_mode)) {
7518 int is_bad_symlink = 0;
7519 if (!a->allow_bad_symlinks) {
7520 char target_path[PATH_MAX];
7521 ssize_t target_len;
7522 target_len = readlink(ondisk_path, target_path,
7523 sizeof(target_path));
7524 if (target_len == -1) {
7525 err = got_error_from_errno2("readlink",
7526 ondisk_path);
7527 break;
7529 err = is_bad_symlink_target(&is_bad_symlink,
7530 target_path, target_len, ondisk_path,
7531 a->worktree->root_path);
7532 if (err)
7533 break;
7534 if (is_bad_symlink) {
7535 err = got_error_path(ondisk_path,
7536 GOT_ERR_BAD_SYMLINK);
7537 break;
7540 if (is_bad_symlink)
7541 got_fileindex_entry_staged_filetype_set(ie,
7542 GOT_FILEIDX_MODE_BAD_SYMLINK);
7543 else
7544 got_fileindex_entry_staged_filetype_set(ie,
7545 GOT_FILEIDX_MODE_SYMLINK);
7546 } else {
7547 got_fileindex_entry_staged_filetype_set(ie,
7548 GOT_FILEIDX_MODE_REGULAR_FILE);
7550 a->staged_something = 1;
7551 if (a->status_cb == NULL)
7552 break;
7553 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7554 get_staged_status(ie), relpath, blob_id,
7555 new_staged_blob_id, NULL, dirfd, de_name);
7556 break;
7557 case GOT_STATUS_DELETE:
7558 if (staged_status == GOT_STATUS_DELETE)
7559 break;
7560 if (a->patch_cb) {
7561 int choice = GOT_PATCH_CHOICE_NONE;
7562 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7563 ie->path, NULL, 1, 1);
7564 if (err)
7565 break;
7566 if (choice == GOT_PATCH_CHOICE_NO)
7567 break;
7568 if (choice != GOT_PATCH_CHOICE_YES) {
7569 err = got_error(GOT_ERR_PATCH_CHOICE);
7570 break;
7573 stage = GOT_FILEIDX_STAGE_DELETE;
7574 got_fileindex_entry_stage_set(ie, stage);
7575 a->staged_something = 1;
7576 if (a->status_cb == NULL)
7577 break;
7578 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7579 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7580 de_name);
7581 break;
7582 case GOT_STATUS_NO_CHANGE:
7583 break;
7584 case GOT_STATUS_CONFLICT:
7585 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7586 break;
7587 case GOT_STATUS_NONEXISTENT:
7588 err = got_error_set_errno(ENOENT, relpath);
7589 break;
7590 default:
7591 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7592 break;
7595 if (path_content && unlink(path_content) == -1 && err == NULL)
7596 err = got_error_from_errno2("unlink", path_content);
7597 free(path_content);
7598 free(ondisk_path);
7599 free(new_staged_blob_id);
7600 return err;
7603 const struct got_error *
7604 got_worktree_stage(struct got_worktree *worktree,
7605 struct got_pathlist_head *paths,
7606 got_worktree_status_cb status_cb, void *status_arg,
7607 got_worktree_patch_cb patch_cb, void *patch_arg,
7608 int allow_bad_symlinks, struct got_repository *repo)
7610 const struct got_error *err = NULL, *sync_err, *unlockerr;
7611 struct got_pathlist_entry *pe;
7612 struct got_fileindex *fileindex = NULL;
7613 char *fileindex_path = NULL;
7614 struct got_reference *head_ref = NULL;
7615 struct got_object_id *head_commit_id = NULL;
7616 struct check_stage_ok_arg oka;
7617 struct stage_path_arg spa;
7619 err = lock_worktree(worktree, LOCK_EX);
7620 if (err)
7621 return err;
7623 err = got_ref_open(&head_ref, repo,
7624 got_worktree_get_head_ref_name(worktree), 0);
7625 if (err)
7626 goto done;
7627 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7628 if (err)
7629 goto done;
7630 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7631 if (err)
7632 goto done;
7634 /* Check pre-conditions before staging anything. */
7635 oka.head_commit_id = head_commit_id;
7636 oka.worktree = worktree;
7637 oka.fileindex = fileindex;
7638 oka.repo = repo;
7639 oka.have_changes = 0;
7640 TAILQ_FOREACH(pe, paths, entry) {
7641 err = worktree_status(worktree, pe->path, fileindex, repo,
7642 check_stage_ok, &oka, NULL, NULL, 0, 0);
7643 if (err)
7644 goto done;
7646 if (!oka.have_changes) {
7647 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7648 goto done;
7651 spa.worktree = worktree;
7652 spa.fileindex = fileindex;
7653 spa.repo = repo;
7654 spa.patch_cb = patch_cb;
7655 spa.patch_arg = patch_arg;
7656 spa.status_cb = status_cb;
7657 spa.status_arg = status_arg;
7658 spa.staged_something = 0;
7659 spa.allow_bad_symlinks = allow_bad_symlinks;
7660 TAILQ_FOREACH(pe, paths, entry) {
7661 err = worktree_status(worktree, pe->path, fileindex, repo,
7662 stage_path, &spa, NULL, NULL, 0, 0);
7663 if (err)
7664 goto done;
7666 if (!spa.staged_something) {
7667 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7668 goto done;
7671 sync_err = sync_fileindex(fileindex, fileindex_path);
7672 if (sync_err && err == NULL)
7673 err = sync_err;
7674 done:
7675 if (head_ref)
7676 got_ref_close(head_ref);
7677 free(head_commit_id);
7678 free(fileindex_path);
7679 if (fileindex)
7680 got_fileindex_free(fileindex);
7681 unlockerr = lock_worktree(worktree, LOCK_SH);
7682 if (unlockerr && err == NULL)
7683 err = unlockerr;
7684 return err;
7687 struct unstage_path_arg {
7688 struct got_worktree *worktree;
7689 struct got_fileindex *fileindex;
7690 struct got_repository *repo;
7691 got_worktree_checkout_cb progress_cb;
7692 void *progress_arg;
7693 got_worktree_patch_cb patch_cb;
7694 void *patch_arg;
7697 static const struct got_error *
7698 create_unstaged_content(char **path_unstaged_content,
7699 char **path_new_staged_content, struct got_object_id *blob_id,
7700 struct got_object_id *staged_blob_id, const char *relpath,
7701 struct got_repository *repo,
7702 got_worktree_patch_cb patch_cb, void *patch_arg)
7704 const struct got_error *err, *free_err;
7705 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7706 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7707 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7708 struct got_diffreg_result *diffreg_result = NULL;
7709 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7710 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7712 *path_unstaged_content = NULL;
7713 *path_new_staged_content = NULL;
7715 err = got_object_id_str(&label1, blob_id);
7716 if (err)
7717 return err;
7718 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7719 if (err)
7720 goto done;
7722 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7723 if (err)
7724 goto done;
7726 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7727 if (err)
7728 goto done;
7730 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7731 if (err)
7732 goto done;
7734 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7735 if (err)
7736 goto done;
7738 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7739 if (err)
7740 goto done;
7742 err = got_diff_files(&diffreg_result, f1, label1, f2,
7743 path2, 3, 0, 1, NULL);
7744 if (err)
7745 goto done;
7747 err = got_opentemp_named(path_unstaged_content, &outfile,
7748 "got-unstaged-content");
7749 if (err)
7750 goto done;
7751 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7752 "got-new-staged-content");
7753 if (err)
7754 goto done;
7756 if (fseek(f1, 0L, SEEK_SET) == -1) {
7757 err = got_ferror(f1, GOT_ERR_IO);
7758 goto done;
7760 if (fseek(f2, 0L, SEEK_SET) == -1) {
7761 err = got_ferror(f2, GOT_ERR_IO);
7762 goto done;
7764 /* Count the number of actual changes in the diff result. */
7765 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7766 struct diff_chunk_context cc = {};
7767 diff_chunk_context_load_change(&cc, &nchunks_used,
7768 diffreg_result->result, n, 0);
7769 nchanges++;
7771 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7772 int choice;
7773 err = apply_or_reject_change(&choice, &nchunks_used,
7774 diffreg_result->result, n, relpath, f1, f2,
7775 &line_cur1, &line_cur2,
7776 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7777 if (err)
7778 goto done;
7779 if (choice == GOT_PATCH_CHOICE_YES)
7780 have_content = 1;
7781 else
7782 have_rejected_content = 1;
7783 if (choice == GOT_PATCH_CHOICE_QUIT)
7784 break;
7786 if (have_content || have_rejected_content)
7787 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7788 outfile, rejectfile);
7789 done:
7790 free(label1);
7791 if (blob)
7792 got_object_blob_close(blob);
7793 if (staged_blob)
7794 got_object_blob_close(staged_blob);
7795 free_err = got_diffreg_result_free(diffreg_result);
7796 if (free_err && err == NULL)
7797 err = free_err;
7798 if (f1 && fclose(f1) == EOF && err == NULL)
7799 err = got_error_from_errno2("fclose", path1);
7800 if (f2 && fclose(f2) == EOF && err == NULL)
7801 err = got_error_from_errno2("fclose", path2);
7802 if (outfile && fclose(outfile) == EOF && err == NULL)
7803 err = got_error_from_errno2("fclose", *path_unstaged_content);
7804 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7805 err = got_error_from_errno2("fclose", *path_new_staged_content);
7806 if (path1 && unlink(path1) == -1 && err == NULL)
7807 err = got_error_from_errno2("unlink", path1);
7808 if (path2 && unlink(path2) == -1 && err == NULL)
7809 err = got_error_from_errno2("unlink", path2);
7810 if (err || !have_content) {
7811 if (*path_unstaged_content &&
7812 unlink(*path_unstaged_content) == -1 && err == NULL)
7813 err = got_error_from_errno2("unlink",
7814 *path_unstaged_content);
7815 free(*path_unstaged_content);
7816 *path_unstaged_content = NULL;
7818 if (err || !have_content || !have_rejected_content) {
7819 if (*path_new_staged_content &&
7820 unlink(*path_new_staged_content) == -1 && err == NULL)
7821 err = got_error_from_errno2("unlink",
7822 *path_new_staged_content);
7823 free(*path_new_staged_content);
7824 *path_new_staged_content = NULL;
7826 free(path1);
7827 free(path2);
7828 return err;
7831 static const struct got_error *
7832 unstage_hunks(struct got_object_id *staged_blob_id,
7833 struct got_blob_object *blob_base,
7834 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7835 const char *ondisk_path, const char *label_orig,
7836 struct got_worktree *worktree, struct got_repository *repo,
7837 got_worktree_patch_cb patch_cb, void *patch_arg,
7838 got_worktree_checkout_cb progress_cb, void *progress_arg)
7840 const struct got_error *err = NULL;
7841 char *path_unstaged_content = NULL;
7842 char *path_new_staged_content = NULL;
7843 char *parent = NULL, *base_path = NULL;
7844 char *blob_base_path = NULL;
7845 struct got_object_id *new_staged_blob_id = NULL;
7846 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
7847 struct stat sb;
7849 err = create_unstaged_content(&path_unstaged_content,
7850 &path_new_staged_content, blob_id, staged_blob_id,
7851 ie->path, repo, patch_cb, patch_arg);
7852 if (err)
7853 return err;
7855 if (path_unstaged_content == NULL)
7856 return NULL;
7858 if (path_new_staged_content) {
7859 err = got_object_blob_create(&new_staged_blob_id,
7860 path_new_staged_content, repo);
7861 if (err)
7862 goto done;
7865 f = fopen(path_unstaged_content, "r");
7866 if (f == NULL) {
7867 err = got_error_from_errno2("fopen",
7868 path_unstaged_content);
7869 goto done;
7871 if (fstat(fileno(f), &sb) == -1) {
7872 err = got_error_from_errno2("fstat", path_unstaged_content);
7873 goto done;
7875 if (got_fileindex_entry_staged_filetype_get(ie) ==
7876 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7877 char link_target[PATH_MAX];
7878 size_t r;
7879 r = fread(link_target, 1, sizeof(link_target), f);
7880 if (r == 0 && ferror(f)) {
7881 err = got_error_from_errno("fread");
7882 goto done;
7884 if (r >= sizeof(link_target)) { /* should not happen */
7885 err = got_error(GOT_ERR_NO_SPACE);
7886 goto done;
7888 link_target[r] = '\0';
7889 err = merge_symlink(worktree, blob_base,
7890 ondisk_path, ie->path, label_orig, link_target,
7891 worktree->base_commit_id, repo, progress_cb,
7892 progress_arg);
7893 } else {
7894 int local_changes_subsumed;
7896 err = got_path_dirname(&parent, ondisk_path);
7897 if (err)
7898 return err;
7900 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
7901 parent) == -1) {
7902 err = got_error_from_errno("asprintf");
7903 base_path = NULL;
7904 goto done;
7907 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
7908 if (err)
7909 goto done;
7910 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
7911 blob_base);
7912 if (err)
7913 goto done;
7916 * In order the run a 3-way merge with a symlink we copy the symlink's
7917 * target path into a temporary file and use that file with diff3.
7919 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7920 err = dump_symlink_target_path_to_file(&f_deriv2,
7921 ondisk_path);
7922 if (err)
7923 goto done;
7924 } else {
7925 int fd;
7926 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
7927 if (fd == -1) {
7928 err = got_error_from_errno2("open", ondisk_path);
7929 goto done;
7931 f_deriv2 = fdopen(fd, "r");
7932 if (f_deriv2 == NULL) {
7933 err = got_error_from_errno2("fdopen", ondisk_path);
7934 close(fd);
7935 goto done;
7939 err = merge_file(&local_changes_subsumed, worktree,
7940 f_base, f, f_deriv2, ondisk_path, ie->path,
7941 got_fileindex_perms_to_st(ie),
7942 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
7943 repo, progress_cb, progress_arg);
7945 if (err)
7946 goto done;
7948 if (new_staged_blob_id) {
7949 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7950 SHA1_DIGEST_LENGTH);
7951 } else {
7952 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7953 got_fileindex_entry_staged_filetype_set(ie, 0);
7955 done:
7956 free(new_staged_blob_id);
7957 if (path_unstaged_content &&
7958 unlink(path_unstaged_content) == -1 && err == NULL)
7959 err = got_error_from_errno2("unlink", path_unstaged_content);
7960 if (path_new_staged_content &&
7961 unlink(path_new_staged_content) == -1 && err == NULL)
7962 err = got_error_from_errno2("unlink", path_new_staged_content);
7963 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
7964 err = got_error_from_errno2("unlink", blob_base_path);
7965 if (f_base && fclose(f_base) == EOF && err == NULL)
7966 err = got_error_from_errno2("fclose", path_unstaged_content);
7967 if (f && fclose(f) == EOF && err == NULL)
7968 err = got_error_from_errno2("fclose", path_unstaged_content);
7969 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
7970 err = got_error_from_errno2("fclose", ondisk_path);
7971 free(path_unstaged_content);
7972 free(path_new_staged_content);
7973 free(blob_base_path);
7974 free(parent);
7975 free(base_path);
7976 return err;
7979 static const struct got_error *
7980 unstage_path(void *arg, unsigned char status,
7981 unsigned char staged_status, const char *relpath,
7982 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7983 struct got_object_id *commit_id, int dirfd, const char *de_name)
7985 const struct got_error *err = NULL;
7986 struct unstage_path_arg *a = arg;
7987 struct got_fileindex_entry *ie;
7988 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7989 char *ondisk_path = NULL;
7990 char *id_str = NULL, *label_orig = NULL;
7991 int local_changes_subsumed;
7992 struct stat sb;
7994 if (staged_status != GOT_STATUS_ADD &&
7995 staged_status != GOT_STATUS_MODIFY &&
7996 staged_status != GOT_STATUS_DELETE)
7997 return NULL;
7999 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8000 if (ie == NULL)
8001 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8003 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8004 == -1)
8005 return got_error_from_errno("asprintf");
8007 err = got_object_id_str(&id_str,
8008 commit_id ? commit_id : a->worktree->base_commit_id);
8009 if (err)
8010 goto done;
8011 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8012 id_str) == -1) {
8013 err = got_error_from_errno("asprintf");
8014 goto done;
8017 switch (staged_status) {
8018 case GOT_STATUS_MODIFY:
8019 err = got_object_open_as_blob(&blob_base, a->repo,
8020 blob_id, 8192);
8021 if (err)
8022 break;
8023 /* fall through */
8024 case GOT_STATUS_ADD:
8025 if (a->patch_cb) {
8026 if (staged_status == GOT_STATUS_ADD) {
8027 int choice = GOT_PATCH_CHOICE_NONE;
8028 err = (*a->patch_cb)(&choice, a->patch_arg,
8029 staged_status, ie->path, NULL, 1, 1);
8030 if (err)
8031 break;
8032 if (choice != GOT_PATCH_CHOICE_YES)
8033 break;
8034 } else {
8035 err = unstage_hunks(staged_blob_id,
8036 blob_base, blob_id, ie, ondisk_path,
8037 label_orig, a->worktree, a->repo,
8038 a->patch_cb, a->patch_arg,
8039 a->progress_cb, a->progress_arg);
8040 break; /* Done with this file. */
8043 err = got_object_open_as_blob(&blob_staged, a->repo,
8044 staged_blob_id, 8192);
8045 if (err)
8046 break;
8047 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8048 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8049 case GOT_FILEIDX_MODE_REGULAR_FILE:
8050 err = merge_blob(&local_changes_subsumed, a->worktree,
8051 blob_base, ondisk_path, relpath,
8052 got_fileindex_perms_to_st(ie), label_orig,
8053 blob_staged, commit_id ? commit_id :
8054 a->worktree->base_commit_id, a->repo,
8055 a->progress_cb, a->progress_arg);
8056 break;
8057 case GOT_FILEIDX_MODE_SYMLINK:
8058 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8059 char *staged_target;
8060 err = got_object_blob_read_to_str(
8061 &staged_target, blob_staged);
8062 if (err)
8063 goto done;
8064 err = merge_symlink(a->worktree, blob_base,
8065 ondisk_path, relpath, label_orig,
8066 staged_target, commit_id ? commit_id :
8067 a->worktree->base_commit_id,
8068 a->repo, a->progress_cb, a->progress_arg);
8069 free(staged_target);
8070 } else {
8071 err = merge_blob(&local_changes_subsumed,
8072 a->worktree, blob_base, ondisk_path,
8073 relpath, got_fileindex_perms_to_st(ie),
8074 label_orig, blob_staged,
8075 commit_id ? commit_id :
8076 a->worktree->base_commit_id, a->repo,
8077 a->progress_cb, a->progress_arg);
8079 break;
8080 default:
8081 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8082 break;
8084 if (err == NULL) {
8085 got_fileindex_entry_stage_set(ie,
8086 GOT_FILEIDX_STAGE_NONE);
8087 got_fileindex_entry_staged_filetype_set(ie, 0);
8089 break;
8090 case GOT_STATUS_DELETE:
8091 if (a->patch_cb) {
8092 int choice = GOT_PATCH_CHOICE_NONE;
8093 err = (*a->patch_cb)(&choice, a->patch_arg,
8094 staged_status, ie->path, NULL, 1, 1);
8095 if (err)
8096 break;
8097 if (choice == GOT_PATCH_CHOICE_NO)
8098 break;
8099 if (choice != GOT_PATCH_CHOICE_YES) {
8100 err = got_error(GOT_ERR_PATCH_CHOICE);
8101 break;
8104 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8105 got_fileindex_entry_staged_filetype_set(ie, 0);
8106 err = get_file_status(&status, &sb, ie, ondisk_path,
8107 dirfd, de_name, a->repo);
8108 if (err)
8109 break;
8110 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8111 break;
8113 done:
8114 free(ondisk_path);
8115 if (blob_base)
8116 got_object_blob_close(blob_base);
8117 if (blob_staged)
8118 got_object_blob_close(blob_staged);
8119 free(id_str);
8120 free(label_orig);
8121 return err;
8124 const struct got_error *
8125 got_worktree_unstage(struct got_worktree *worktree,
8126 struct got_pathlist_head *paths,
8127 got_worktree_checkout_cb progress_cb, void *progress_arg,
8128 got_worktree_patch_cb patch_cb, void *patch_arg,
8129 struct got_repository *repo)
8131 const struct got_error *err = NULL, *sync_err, *unlockerr;
8132 struct got_pathlist_entry *pe;
8133 struct got_fileindex *fileindex = NULL;
8134 char *fileindex_path = NULL;
8135 struct unstage_path_arg upa;
8137 err = lock_worktree(worktree, LOCK_EX);
8138 if (err)
8139 return err;
8141 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8142 if (err)
8143 goto done;
8145 upa.worktree = worktree;
8146 upa.fileindex = fileindex;
8147 upa.repo = repo;
8148 upa.progress_cb = progress_cb;
8149 upa.progress_arg = progress_arg;
8150 upa.patch_cb = patch_cb;
8151 upa.patch_arg = patch_arg;
8152 TAILQ_FOREACH(pe, paths, entry) {
8153 err = worktree_status(worktree, pe->path, fileindex, repo,
8154 unstage_path, &upa, NULL, NULL, 0, 0);
8155 if (err)
8156 goto done;
8159 sync_err = sync_fileindex(fileindex, fileindex_path);
8160 if (sync_err && err == NULL)
8161 err = sync_err;
8162 done:
8163 free(fileindex_path);
8164 if (fileindex)
8165 got_fileindex_free(fileindex);
8166 unlockerr = lock_worktree(worktree, LOCK_SH);
8167 if (unlockerr && err == NULL)
8168 err = unlockerr;
8169 return err;
8172 struct report_file_info_arg {
8173 struct got_worktree *worktree;
8174 got_worktree_path_info_cb info_cb;
8175 void *info_arg;
8176 struct got_pathlist_head *paths;
8177 got_cancel_cb cancel_cb;
8178 void *cancel_arg;
8181 static const struct got_error *
8182 report_file_info(void *arg, struct got_fileindex_entry *ie)
8184 struct report_file_info_arg *a = arg;
8185 struct got_pathlist_entry *pe;
8186 struct got_object_id blob_id, staged_blob_id, commit_id;
8187 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8188 struct got_object_id *commit_idp = NULL;
8189 int stage;
8191 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8192 return got_error(GOT_ERR_CANCELLED);
8194 TAILQ_FOREACH(pe, a->paths, entry) {
8195 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8196 got_path_is_child(ie->path, pe->path, pe->path_len))
8197 break;
8199 if (pe == NULL) /* not found */
8200 return NULL;
8202 if (got_fileindex_entry_has_blob(ie)) {
8203 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8204 blob_idp = &blob_id;
8206 stage = got_fileindex_entry_stage_get(ie);
8207 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8208 stage == GOT_FILEIDX_STAGE_ADD) {
8209 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8210 SHA1_DIGEST_LENGTH);
8211 staged_blob_idp = &staged_blob_id;
8214 if (got_fileindex_entry_has_commit(ie)) {
8215 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8216 commit_idp = &commit_id;
8219 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8220 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8223 const struct got_error *
8224 got_worktree_path_info(struct got_worktree *worktree,
8225 struct got_pathlist_head *paths,
8226 got_worktree_path_info_cb info_cb, void *info_arg,
8227 got_cancel_cb cancel_cb, void *cancel_arg)
8230 const struct got_error *err = NULL, *unlockerr;
8231 struct got_fileindex *fileindex = NULL;
8232 char *fileindex_path = NULL;
8233 struct report_file_info_arg arg;
8235 err = lock_worktree(worktree, LOCK_SH);
8236 if (err)
8237 return err;
8239 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8240 if (err)
8241 goto done;
8243 arg.worktree = worktree;
8244 arg.info_cb = info_cb;
8245 arg.info_arg = info_arg;
8246 arg.paths = paths;
8247 arg.cancel_cb = cancel_cb;
8248 arg.cancel_arg = cancel_arg;
8249 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8250 &arg);
8251 done:
8252 free(fileindex_path);
8253 if (fileindex)
8254 got_fileindex_free(fileindex);
8255 unlockerr = lock_worktree(worktree, LOCK_UN);
8256 if (unlockerr && err == NULL)
8257 err = unlockerr;
8258 return err;