Blob


1 /*
2 * Copyright (c) 2018, 2019 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"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
68 {
69 const struct got_error *err = NULL;
70 char *path;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
76 free(path);
77 return err;
78 }
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
82 {
83 const struct got_error *err = NULL;
84 FILE *tmpfile = NULL;
85 char *tmppath = NULL;
86 char *path = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
90 path = NULL;
91 goto done;
92 }
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
95 if (err)
96 goto done;
98 if (content) {
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
102 goto done;
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
108 unlink(tmppath);
109 goto done;
112 done:
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
115 free(tmppath);
116 return err;
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
123 char *path;
124 int fd = -1;
125 ssize_t n;
126 struct stat sb;
128 *content = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
132 path = NULL;
133 goto done;
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
137 if (fd == -1) {
138 if (errno == ENOENT)
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
140 else
141 err = got_error_from_errno2("open", path);
142 goto done;
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
147 goto done;
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
152 goto done;
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
157 goto done;
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
164 goto done;
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
168 goto done;
170 (*content)[sb.st_size - 1] = '\0';
172 done:
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
175 free(path);
176 if (err) {
177 free(*content);
178 *content = NULL;
180 return err;
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
187 char *refstr = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
191 if (refstr == NULL)
192 return got_error_from_errno("got_ref_to_str");
193 } else {
194 refstr = strdup(got_ref_get_name(head_ref));
195 if (refstr == NULL)
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 free(refstr);
200 return err;
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
209 uuid_t uuid;
210 uint32_t uuid_status;
211 int obj_type;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
220 goto done;
223 err = got_ref_resolve(&commit_id, repo, head_ref);
224 if (err)
225 return err;
226 err = got_object_get_type(&obj_type, repo, commit_id);
227 if (err)
228 return err;
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
240 goto done;
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
246 goto done;
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
250 goto done;
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
255 if (err)
256 goto done;
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
260 if (err)
261 goto done;
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
265 if (err)
266 goto done;
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
270 if (err)
271 goto done;
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 if (err)
274 goto done;
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
279 if (err)
280 goto done;
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
285 if (err)
286 goto done;
288 /* Generate UUID. */
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
292 goto done;
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
297 goto done;
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 if (err)
301 goto done;
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
306 goto done;
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
309 if (err)
310 goto done;
312 done:
313 free(commit_id);
314 free(path_got);
315 free(formatstr);
316 free(absprefix);
317 free(basestr);
318 free(uuidstr);
319 return err;
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
326 char *path_got;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
332 const char *errstr;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
336 *worktree = NULL;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
340 path_got = NULL;
341 goto done;
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
346 path_lock = NULL;
347 goto done;
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 if (fd == -1) {
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
354 goto done;
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 if (err)
359 goto done;
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 if (errstr) {
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
365 goto done;
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
369 goto done;
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
375 goto done;
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = strdup(path);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno("strdup");
382 goto done;
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
386 if (err)
387 goto done;
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
391 if (err)
392 goto done;
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
396 if (err)
397 goto done;
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 if (err)
401 goto done;
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
405 goto done;
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
409 if (err)
410 goto done;
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 base_commit_id_str);
414 if (err)
415 goto done;
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
419 done:
420 if (repo)
421 got_repo_close(repo);
422 free(path_got);
423 free(path_lock);
424 free(base_commit_id_str);
425 free(uuidstr);
426 free(formatstr);
427 if (err) {
428 if (fd != -1)
429 close(fd);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
432 *worktree = NULL;
433 } else
434 (*worktree)->lockfd = fd;
436 return err;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
444 do {
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 return err;
448 if (*worktree)
449 return NULL;
450 path = dirname(path);
451 if (path == NULL)
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
471 free(worktree);
472 return err;
475 const char *
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
481 const char *
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
487 const char *
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
505 free(absprefix);
506 return NULL;
509 const char *
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
525 path_got = NULL;
526 goto done;
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
532 goto done;
535 err = write_head_ref(path_got, head_ref);
536 if (err)
537 goto done;
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
541 done:
542 free(path_got);
543 if (err)
544 free(head_ref_name);
545 return err;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
560 char *id_str = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
566 path_got = NULL;
567 goto done;
570 err = got_object_open(&obj, repo, commit_id);
571 if (err)
572 return err;
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
576 goto done;
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
581 if (err)
582 goto done;
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
584 if (err)
585 goto done;
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
591 goto done;
593 done:
594 if (obj)
595 got_object_close(obj);
596 free(id_str);
597 free(path_got);
598 return err;
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
608 return NULL;
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
615 char *abspath;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 struct stat sb;
623 err = NULL;
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
631 free(abspath);
632 return err;
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
639 uint8_t fbuf1[8192];
640 uint8_t fbuf2[8192];
641 size_t flen1 = 0, flen2 = 0;
643 *same = 1;
645 for (;;) {
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
649 break;
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
654 break;
656 if (flen1 == 0) {
657 if (flen2 != 0)
658 *same = 0;
659 break;
660 } else if (flen2 == 0) {
661 if (flen1 != 0)
662 *same = 0;
663 break;
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
666 *same = 0;
667 break;
669 } else {
670 *same = 0;
671 break;
675 return err;
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
682 struct stat sb;
683 size_t size1, size2;
684 FILE *f1 = NULL, *f2 = NULL;
686 *same = 1;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
690 goto done;
692 size1 = sb.st_size;
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
696 goto done;
698 size2 = sb.st_size;
700 if (size1 != size2) {
701 *same = 0;
702 return NULL;
705 f1 = fopen(f1_path, "r");
706 if (f1 == NULL)
707 return got_error_from_errno2("open", f1_path);
709 f2 = fopen(f2_path, "r");
710 if (f2 == NULL) {
711 err = got_error_from_errno2("open", f2_path);
712 goto done;
715 err = check_file_contents_equal(same, f1, f2);
716 done:
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
722 return err;
725 /*
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
729 */
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
739 int merged_fd = -1;
740 FILE *f_orig = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
743 int overlapcnt = 0;
744 char *parent;
746 *local_changes_subsumed = 0;
748 parent = dirname(ondisk_path);
749 if (parent == NULL)
750 return got_error_from_errno2("dirname", ondisk_path);
752 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 return got_error_from_errno("asprintf");
755 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
756 if (err)
757 goto done;
759 free(base_path);
760 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 err = got_error_from_errno("asprintf");
762 base_path = NULL;
763 goto done;
766 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
767 if (err)
768 goto done;
769 if (blob_orig) {
770 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
771 blob_orig);
772 if (err)
773 goto done;
774 } else {
775 /*
776 * If the file has no blob, this is an "add vs add" conflict,
777 * and we simply use an empty ancestor file to make both files
778 * appear in the merged result in their entirety.
779 */
782 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
784 if (err)
785 goto done;
787 err = (*progress_cb)(progress_arg,
788 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
789 if (err)
790 goto done;
792 if (fsync(merged_fd) != 0) {
793 err = got_error_from_errno("fsync");
794 goto done;
797 /* Check if a clean merge has subsumed all local changes. */
798 if (overlapcnt == 0) {
799 err = check_files_equal(local_changes_subsumed, deriv_path,
800 merged_path);
801 if (err)
802 goto done;
805 if (chmod(merged_path, st_mode) != 0) {
806 err = got_error_from_errno2("chmod", merged_path);
807 goto done;
810 if (rename(merged_path, ondisk_path) != 0) {
811 err = got_error_from_errno3("rename", merged_path,
812 ondisk_path);
813 unlink(merged_path);
814 goto done;
817 done:
818 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
819 err = got_error_from_errno("close");
820 if (f_orig && fclose(f_orig) != 0 && err == NULL)
821 err = got_error_from_errno("fclose");
822 free(merged_path);
823 free(base_path);
824 if (blob_orig_path) {
825 unlink(blob_orig_path);
826 free(blob_orig_path);
828 return err;
831 /*
832 * Perform a 3-way merge where blob_orig acts as the common ancestor,
833 * blob_deriv acts as the first derived version, and the file on disk
834 * acts as the second derived version.
835 */
836 static const struct got_error *
837 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
838 struct got_blob_object *blob_orig, const char *ondisk_path,
839 const char *path, uint16_t st_mode, const char *label_orig,
840 struct got_blob_object *blob_deriv,
841 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
842 got_worktree_checkout_cb progress_cb, void *progress_arg)
844 const struct got_error *err = NULL;
845 FILE *f_deriv = NULL;
846 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
847 char *label_deriv = NULL, *parent;
849 *local_changes_subsumed = 0;
851 parent = dirname(ondisk_path);
852 if (parent == NULL)
853 return got_error_from_errno2("dirname", ondisk_path);
855 free(base_path);
856 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
857 err = got_error_from_errno("asprintf");
858 base_path = NULL;
859 goto done;
862 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
863 if (err)
864 goto done;
865 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
866 blob_deriv);
867 if (err)
868 goto done;
870 err = got_object_id_str(&id_str, deriv_base_commit_id);
871 if (err)
872 goto done;
873 if (asprintf(&label_deriv, "%s: commit %s",
874 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
875 err = got_error_from_errno("asprintf");
876 goto done;
879 err = merge_file(local_changes_subsumed, worktree, blob_orig,
880 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
881 label_deriv, repo, progress_cb, progress_arg);
882 done:
883 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
884 err = got_error_from_errno("fclose");
885 free(base_path);
886 if (blob_deriv_path) {
887 unlink(blob_deriv_path);
888 free(blob_deriv_path);
890 free(id_str);
891 free(label_deriv);
892 return err;
895 static const struct got_error *
896 update_blob_fileindex_entry(struct got_worktree *worktree,
897 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
898 const char *ondisk_path, const char *path, struct got_blob_object *blob,
899 int update_timestamps)
901 const struct got_error *err = NULL;
903 if (ie == NULL)
904 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
905 if (ie)
906 err = got_fileindex_entry_update(ie, ondisk_path,
907 blob->id.sha1, worktree->base_commit_id->sha1,
908 update_timestamps);
909 else {
910 struct got_fileindex_entry *new_ie;
911 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
912 path, blob->id.sha1, worktree->base_commit_id->sha1);
913 if (!err)
914 err = got_fileindex_entry_add(fileindex, new_ie);
916 return err;
919 static mode_t
920 get_ondisk_perms(int executable, mode_t st_mode)
922 mode_t xbits = S_IXUSR;
924 if (executable) {
925 /* Map read bits to execute bits. */
926 if (st_mode & S_IRGRP)
927 xbits |= S_IXGRP;
928 if (st_mode & S_IROTH)
929 xbits |= S_IXOTH;
930 return st_mode | xbits;
933 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
936 static const struct got_error *
937 install_blob(struct got_worktree *worktree, const char *ondisk_path,
938 const char *path, uint16_t te_mode, uint16_t st_mode,
939 struct got_blob_object *blob, int restoring_missing_file,
940 int reverting_versioned_file, struct got_repository *repo,
941 got_worktree_checkout_cb progress_cb, void *progress_arg)
943 const struct got_error *err = NULL;
944 int fd = -1;
945 size_t len, hdrlen;
946 int update = 0;
947 char *tmppath = NULL;
949 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
950 GOT_DEFAULT_FILE_MODE);
951 if (fd == -1) {
952 if (errno == ENOENT) {
953 char *parent = dirname(path);
954 if (parent == NULL)
955 return got_error_from_errno2("dirname", path);
956 err = add_dir_on_disk(worktree, parent);
957 if (err)
958 return err;
959 fd = open(ondisk_path,
960 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
961 GOT_DEFAULT_FILE_MODE);
962 if (fd == -1)
963 return got_error_from_errno2("open",
964 ondisk_path);
965 } else if (errno == EEXIST) {
966 if (!S_ISREG(st_mode)) {
967 /* TODO file is obstructed; do something */
968 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
969 goto done;
970 } else {
971 err = got_opentemp_named_fd(&tmppath, &fd,
972 ondisk_path);
973 if (err)
974 goto done;
975 update = 1;
977 } else
978 return got_error_from_errno2("open", ondisk_path);
981 if (restoring_missing_file)
982 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
983 else if (reverting_versioned_file)
984 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
985 else
986 err = (*progress_cb)(progress_arg,
987 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
988 if (err)
989 goto done;
991 hdrlen = got_object_blob_get_hdrlen(blob);
992 do {
993 const uint8_t *buf = got_object_blob_get_read_buf(blob);
994 err = got_object_blob_read_block(&len, blob);
995 if (err)
996 break;
997 if (len > 0) {
998 /* Skip blob object header first time around. */
999 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1000 if (outlen == -1) {
1001 err = got_error_from_errno("write");
1002 goto done;
1003 } else if (outlen != len - hdrlen) {
1004 err = got_error(GOT_ERR_IO);
1005 goto done;
1007 hdrlen = 0;
1009 } while (len != 0);
1011 if (fsync(fd) != 0) {
1012 err = got_error_from_errno("fsync");
1013 goto done;
1016 if (update) {
1017 if (rename(tmppath, ondisk_path) != 0) {
1018 err = got_error_from_errno3("rename", tmppath,
1019 ondisk_path);
1020 unlink(tmppath);
1021 goto done;
1025 if (chmod(ondisk_path,
1026 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1027 err = got_error_from_errno2("chmod", ondisk_path);
1028 goto done;
1031 done:
1032 if (fd != -1 && close(fd) != 0 && err == NULL)
1033 err = got_error_from_errno("close");
1034 free(tmppath);
1035 return err;
1038 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1039 static const struct got_error *
1040 get_modified_file_content_status(unsigned char *status, FILE *f)
1042 const struct got_error *err = NULL;
1043 const char *markers[3] = {
1044 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1045 GOT_DIFF_CONFLICT_MARKER_SEP,
1046 GOT_DIFF_CONFLICT_MARKER_END
1048 int i = 0;
1049 char *line;
1050 size_t len;
1051 const char delim[3] = {'\0', '\0', '\0'};
1053 while (*status == GOT_STATUS_MODIFY) {
1054 line = fparseln(f, &len, NULL, delim, 0);
1055 if (line == NULL) {
1056 if (feof(f))
1057 break;
1058 err = got_ferror(f, GOT_ERR_IO);
1059 break;
1062 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1063 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1064 == 0)
1065 *status = GOT_STATUS_CONFLICT;
1066 else
1067 i++;
1071 return err;
1074 static int
1075 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1077 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1078 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1081 static int
1082 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1084 return !(ie->ctime_sec == sb->st_ctime &&
1085 ie->ctime_nsec == sb->st_ctimensec &&
1086 ie->mtime_sec == sb->st_mtime &&
1087 ie->mtime_nsec == sb->st_mtimensec &&
1088 ie->size == (sb->st_size & 0xffffffff) &&
1089 !xbit_differs(ie, sb->st_mode));
1092 static unsigned char
1093 get_staged_status(struct got_fileindex_entry *ie)
1095 switch (got_fileindex_entry_stage_get(ie)) {
1096 case GOT_FILEIDX_STAGE_ADD:
1097 return GOT_STATUS_ADD;
1098 case GOT_FILEIDX_STAGE_DELETE:
1099 return GOT_STATUS_DELETE;
1100 case GOT_FILEIDX_STAGE_MODIFY:
1101 return GOT_STATUS_MODIFY;
1102 default:
1103 return GOT_STATUS_NO_CHANGE;
1107 static const struct got_error *
1108 get_file_status(unsigned char *status, struct stat *sb,
1109 struct got_fileindex_entry *ie, const char *abspath,
1110 struct got_repository *repo)
1112 const struct got_error *err = NULL;
1113 struct got_object_id id;
1114 size_t hdrlen;
1115 FILE *f = NULL;
1116 uint8_t fbuf[8192];
1117 struct got_blob_object *blob = NULL;
1118 size_t flen, blen;
1119 unsigned char staged_status = get_staged_status(ie);
1121 *status = GOT_STATUS_NO_CHANGE;
1123 if (lstat(abspath, sb) == -1) {
1124 if (errno == ENOENT) {
1125 if (got_fileindex_entry_has_file_on_disk(ie))
1126 *status = GOT_STATUS_MISSING;
1127 else
1128 *status = GOT_STATUS_DELETE;
1129 return NULL;
1131 return got_error_from_errno2("lstat", abspath);
1134 if (!S_ISREG(sb->st_mode)) {
1135 *status = GOT_STATUS_OBSTRUCTED;
1136 return NULL;
1139 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1140 *status = GOT_STATUS_DELETE;
1141 return NULL;
1142 } else if (!got_fileindex_entry_has_blob(ie) &&
1143 staged_status != GOT_STATUS_ADD) {
1144 *status = GOT_STATUS_ADD;
1145 return NULL;
1148 if (!stat_info_differs(ie, sb))
1149 return NULL;
1151 if (staged_status == GOT_STATUS_MODIFY ||
1152 staged_status == GOT_STATUS_ADD)
1153 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1154 else
1155 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1157 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1158 if (err)
1159 return err;
1161 f = fopen(abspath, "r");
1162 if (f == NULL) {
1163 err = got_error_from_errno2("fopen", abspath);
1164 goto done;
1166 hdrlen = got_object_blob_get_hdrlen(blob);
1167 for (;;) {
1168 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1169 err = got_object_blob_read_block(&blen, blob);
1170 if (err)
1171 goto done;
1172 /* Skip length of blob object header first time around. */
1173 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1174 if (flen == 0 && ferror(f)) {
1175 err = got_error_from_errno("fread");
1176 goto done;
1178 if (blen == 0) {
1179 if (flen != 0)
1180 *status = GOT_STATUS_MODIFY;
1181 break;
1182 } else if (flen == 0) {
1183 if (blen != 0)
1184 *status = GOT_STATUS_MODIFY;
1185 break;
1186 } else if (blen - hdrlen == flen) {
1187 /* Skip blob object header first time around. */
1188 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1189 *status = GOT_STATUS_MODIFY;
1190 break;
1192 } else {
1193 *status = GOT_STATUS_MODIFY;
1194 break;
1196 hdrlen = 0;
1199 if (*status == GOT_STATUS_MODIFY) {
1200 rewind(f);
1201 err = get_modified_file_content_status(status, f);
1202 } else if (xbit_differs(ie, sb->st_mode))
1203 *status = GOT_STATUS_MODE_CHANGE;
1204 done:
1205 if (blob)
1206 got_object_blob_close(blob);
1207 if (f)
1208 fclose(f);
1209 return err;
1213 * Update timestamps in the file index if a file is unmodified and
1214 * we had to run a full content comparison to find out.
1216 static const struct got_error *
1217 sync_timestamps(char *ondisk_path, unsigned char status,
1218 struct got_fileindex_entry *ie, struct stat *sb)
1220 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1221 return got_fileindex_entry_update(ie, ondisk_path,
1222 ie->blob_sha1, ie->commit_sha1, 1);
1224 return NULL;
1227 static const struct got_error *
1228 update_blob(struct got_worktree *worktree,
1229 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1230 struct got_tree_entry *te, const char *path,
1231 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1232 void *progress_arg)
1234 const struct got_error *err = NULL;
1235 struct got_blob_object *blob = NULL;
1236 char *ondisk_path;
1237 unsigned char status = GOT_STATUS_NO_CHANGE;
1238 struct stat sb;
1240 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1241 return got_error_from_errno("asprintf");
1243 if (ie) {
1244 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1245 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1246 goto done;
1248 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1249 if (err)
1250 goto done;
1251 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1252 sb.st_mode = got_fileindex_perms_to_st(ie);
1253 } else
1254 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1256 if (status == GOT_STATUS_OBSTRUCTED) {
1257 err = (*progress_cb)(progress_arg, status, path);
1258 goto done;
1261 if (ie && status != GOT_STATUS_MISSING &&
1262 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1263 if (got_fileindex_entry_has_commit(ie) &&
1264 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1265 SHA1_DIGEST_LENGTH) == 0) {
1266 err = sync_timestamps(ondisk_path, status, ie, &sb);
1267 if (err)
1268 goto done;
1269 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1270 path);
1271 goto done;
1273 if (got_fileindex_entry_has_blob(ie) &&
1274 memcmp(ie->blob_sha1, te->id.sha1,
1275 SHA1_DIGEST_LENGTH) == 0) {
1276 err = sync_timestamps(ondisk_path, status, ie, &sb);
1277 goto done;
1281 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1282 if (err)
1283 goto done;
1285 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1286 int update_timestamps;
1287 struct got_blob_object *blob2 = NULL;
1288 char *label_orig = NULL;
1289 if (got_fileindex_entry_has_blob(ie)) {
1290 struct got_object_id id2;
1291 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1292 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1293 if (err)
1294 goto done;
1296 if (got_fileindex_entry_has_commit(ie)) {
1297 char id_str[SHA1_DIGEST_STRING_LENGTH];
1298 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1299 sizeof(id_str)) == NULL) {
1300 err = got_error_path(id_str,
1301 GOT_ERR_BAD_OBJ_ID_STR);
1302 goto done;
1304 if (asprintf(&label_orig, "%s: commit %s",
1305 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1306 err = got_error_from_errno("asprintf");
1307 goto done;
1310 err = merge_blob(&update_timestamps, worktree, blob2,
1311 ondisk_path, path, sb.st_mode, label_orig, blob,
1312 worktree->base_commit_id, repo,
1313 progress_cb, progress_arg);
1314 free(label_orig);
1315 if (blob2)
1316 got_object_blob_close(blob2);
1317 if (err)
1318 goto done;
1320 * Do not update timestamps of files with local changes.
1321 * Otherwise, a future status walk would treat them as
1322 * unmodified files again.
1324 err = got_fileindex_entry_update(ie, ondisk_path,
1325 blob->id.sha1, worktree->base_commit_id->sha1,
1326 update_timestamps);
1327 } else if (status == GOT_STATUS_MODE_CHANGE) {
1328 err = got_fileindex_entry_update(ie, ondisk_path,
1329 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1330 } else if (status == GOT_STATUS_DELETE) {
1331 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1332 if (err)
1333 goto done;
1334 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1335 ondisk_path, path, blob, 0);
1336 if (err)
1337 goto done;
1338 } else {
1339 err = install_blob(worktree, ondisk_path, path, te->mode,
1340 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1341 repo, progress_cb, progress_arg);
1342 if (err)
1343 goto done;
1344 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1345 ondisk_path, path, blob, 1);
1346 if (err)
1347 goto done;
1349 got_object_blob_close(blob);
1350 done:
1351 free(ondisk_path);
1352 return err;
1355 static const struct got_error *
1356 remove_ondisk_file(const char *root_path, const char *path)
1358 const struct got_error *err = NULL;
1359 char *ondisk_path = NULL;
1361 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1362 return got_error_from_errno("asprintf");
1364 if (unlink(ondisk_path) == -1) {
1365 if (errno != ENOENT)
1366 err = got_error_from_errno2("unlink", ondisk_path);
1367 } else {
1368 char *parent = dirname(ondisk_path);
1369 while (parent && strcmp(parent, root_path) != 0) {
1370 if (rmdir(parent) == -1) {
1371 if (errno != ENOTEMPTY)
1372 err = got_error_from_errno2("rmdir",
1373 parent);
1374 break;
1376 parent = dirname(parent);
1379 free(ondisk_path);
1380 return err;
1383 static const struct got_error *
1384 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1385 struct got_fileindex_entry *ie, struct got_repository *repo,
1386 got_worktree_checkout_cb progress_cb, void *progress_arg)
1388 const struct got_error *err = NULL;
1389 unsigned char status;
1390 struct stat sb;
1391 char *ondisk_path;
1393 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1394 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1396 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1397 == -1)
1398 return got_error_from_errno("asprintf");
1400 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1401 if (err)
1402 return err;
1404 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1405 status == GOT_STATUS_ADD) {
1406 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1407 if (err)
1408 return err;
1410 * Preserve the working file and change the deleted blob's
1411 * entry into a schedule-add entry.
1413 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1414 0);
1415 if (err)
1416 return err;
1417 } else {
1418 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1419 if (err)
1420 return err;
1421 if (status == GOT_STATUS_NO_CHANGE) {
1422 err = remove_ondisk_file(worktree->root_path, ie->path);
1423 if (err)
1424 return err;
1426 got_fileindex_entry_remove(fileindex, ie);
1429 return err;
1432 struct diff_cb_arg {
1433 struct got_fileindex *fileindex;
1434 struct got_worktree *worktree;
1435 struct got_repository *repo;
1436 got_worktree_checkout_cb progress_cb;
1437 void *progress_arg;
1438 got_cancel_cb cancel_cb;
1439 void *cancel_arg;
1442 static const struct got_error *
1443 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1444 struct got_tree_entry *te, const char *parent_path)
1446 struct diff_cb_arg *a = arg;
1448 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1449 return got_error(GOT_ERR_CANCELLED);
1451 return update_blob(a->worktree, a->fileindex, ie, te,
1452 ie->path, a->repo, a->progress_cb, a->progress_arg);
1455 static const struct got_error *
1456 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1458 struct diff_cb_arg *a = arg;
1460 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1461 return got_error(GOT_ERR_CANCELLED);
1463 return delete_blob(a->worktree, a->fileindex, ie,
1464 a->repo, a->progress_cb, a->progress_arg);
1467 static const struct got_error *
1468 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1470 struct diff_cb_arg *a = arg;
1471 const struct got_error *err;
1472 char *path;
1474 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1475 return got_error(GOT_ERR_CANCELLED);
1477 if (got_object_tree_entry_is_submodule(te))
1478 return NULL;
1480 if (asprintf(&path, "%s%s%s", parent_path,
1481 parent_path[0] ? "/" : "", te->name)
1482 == -1)
1483 return got_error_from_errno("asprintf");
1485 if (S_ISDIR(te->mode))
1486 err = add_dir_on_disk(a->worktree, path);
1487 else
1488 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1489 a->repo, a->progress_cb, a->progress_arg);
1491 free(path);
1492 return err;
1495 static const struct got_error *
1496 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1498 const struct got_error *err = NULL;
1499 char *uuidstr = NULL;
1500 uint32_t uuid_status;
1502 *refname = NULL;
1504 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1505 if (uuid_status != uuid_s_ok)
1506 return got_error_uuid(uuid_status, "uuid_to_string");
1508 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1509 == -1) {
1510 err = got_error_from_errno("asprintf");
1511 *refname = NULL;
1513 free(uuidstr);
1514 return err;
1517 const struct got_error *
1518 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1520 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1523 static const struct got_error *
1524 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1526 return get_ref_name(refname, worktree,
1527 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1530 static const struct got_error *
1531 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1533 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1536 static const struct got_error *
1537 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1539 return get_ref_name(refname, worktree,
1540 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1543 static const struct got_error *
1544 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1546 return get_ref_name(refname, worktree,
1547 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1550 static const struct got_error *
1551 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1553 return get_ref_name(refname, worktree,
1554 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1557 static const struct got_error *
1558 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1560 return get_ref_name(refname, worktree,
1561 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1564 static const struct got_error *
1565 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1567 return get_ref_name(refname, worktree,
1568 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1571 static const struct got_error *
1572 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1574 return get_ref_name(refname, worktree,
1575 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1578 const struct got_error *
1579 got_worktree_get_histedit_script_path(char **path,
1580 struct got_worktree *worktree)
1582 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1583 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1584 *path = NULL;
1585 return got_error_from_errno("asprintf");
1587 return NULL;
1591 * Prevent Git's garbage collector from deleting our base commit by
1592 * setting a reference to our base commit's ID.
1594 static const struct got_error *
1595 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1597 const struct got_error *err = NULL;
1598 struct got_reference *ref = NULL;
1599 char *refname;
1601 err = got_worktree_get_base_ref_name(&refname, worktree);
1602 if (err)
1603 return err;
1605 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1606 if (err)
1607 goto done;
1609 err = got_ref_write(ref, repo);
1610 done:
1611 free(refname);
1612 if (ref)
1613 got_ref_close(ref);
1614 return err;
1617 static const struct got_error *
1618 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1620 const struct got_error *err = NULL;
1622 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1623 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1624 err = got_error_from_errno("asprintf");
1625 *fileindex_path = NULL;
1627 return err;
1631 static const struct got_error *
1632 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1633 struct got_worktree *worktree)
1635 const struct got_error *err = NULL;
1636 FILE *index = NULL;
1638 *fileindex_path = NULL;
1639 *fileindex = got_fileindex_alloc();
1640 if (*fileindex == NULL)
1641 return got_error_from_errno("got_fileindex_alloc");
1643 err = get_fileindex_path(fileindex_path, worktree);
1644 if (err)
1645 goto done;
1647 index = fopen(*fileindex_path, "rb");
1648 if (index == NULL) {
1649 if (errno != ENOENT)
1650 err = got_error_from_errno2("fopen", *fileindex_path);
1651 } else {
1652 err = got_fileindex_read(*fileindex, index);
1653 if (fclose(index) != 0 && err == NULL)
1654 err = got_error_from_errno("fclose");
1656 done:
1657 if (err) {
1658 free(*fileindex_path);
1659 *fileindex_path = NULL;
1660 got_fileindex_free(*fileindex);
1661 *fileindex = NULL;
1663 return err;
1666 struct bump_base_commit_id_arg {
1667 struct got_object_id *base_commit_id;
1668 const char *path;
1669 size_t path_len;
1670 const char *entry_name;
1671 got_worktree_checkout_cb progress_cb;
1672 void *progress_arg;
1675 /* Bump base commit ID of all files within an updated part of the work tree. */
1676 static const struct got_error *
1677 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1679 const struct got_error *err;
1680 struct bump_base_commit_id_arg *a = arg;
1682 if (a->entry_name) {
1683 if (strcmp(ie->path, a->path) != 0)
1684 return NULL;
1685 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1686 return NULL;
1688 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1689 SHA1_DIGEST_LENGTH) == 0)
1690 return NULL;
1692 if (a->progress_cb) {
1693 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1694 ie->path);
1695 if (err)
1696 return err;
1698 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1699 return NULL;
1702 static const struct got_error *
1703 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1705 const struct got_error *err = NULL;
1706 char *new_fileindex_path = NULL;
1707 FILE *new_index = NULL;
1709 err = got_opentemp_named(&new_fileindex_path, &new_index,
1710 fileindex_path);
1711 if (err)
1712 goto done;
1714 err = got_fileindex_write(fileindex, new_index);
1715 if (err)
1716 goto done;
1718 if (rename(new_fileindex_path, fileindex_path) != 0) {
1719 err = got_error_from_errno3("rename", new_fileindex_path,
1720 fileindex_path);
1721 unlink(new_fileindex_path);
1723 done:
1724 if (new_index)
1725 fclose(new_index);
1726 free(new_fileindex_path);
1727 return err;
1730 static const struct got_error *
1731 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1732 struct got_object_id **tree_id, const char *wt_relpath,
1733 struct got_worktree *worktree, struct got_repository *repo)
1735 const struct got_error *err = NULL;
1736 struct got_object_id *id = NULL;
1737 char *in_repo_path = NULL;
1738 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1740 *entry_type = GOT_OBJ_TYPE_ANY;
1741 *tree_relpath = NULL;
1742 *tree_id = NULL;
1744 if (wt_relpath[0] == '\0') {
1745 /* Check out all files within the work tree. */
1746 *entry_type = GOT_OBJ_TYPE_TREE;
1747 *tree_relpath = strdup("");
1748 if (*tree_relpath == NULL) {
1749 err = got_error_from_errno("strdup");
1750 goto done;
1752 err = got_object_id_by_path(tree_id, repo,
1753 worktree->base_commit_id, worktree->path_prefix);
1754 if (err)
1755 goto done;
1756 return NULL;
1759 /* Check out a subset of files in the work tree. */
1761 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1762 is_root_wt ? "" : "/", wt_relpath) == -1) {
1763 err = got_error_from_errno("asprintf");
1764 goto done;
1767 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1768 in_repo_path);
1769 if (err)
1770 goto done;
1772 free(in_repo_path);
1773 in_repo_path = NULL;
1775 err = got_object_get_type(entry_type, repo, id);
1776 if (err)
1777 goto done;
1779 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1780 /* Check out a single file. */
1781 if (strchr(wt_relpath, '/') == NULL) {
1782 /* Check out a single file in work tree's root dir. */
1783 in_repo_path = strdup(worktree->path_prefix);
1784 if (in_repo_path == NULL) {
1785 err = got_error_from_errno("strdup");
1786 goto done;
1788 *tree_relpath = strdup("");
1789 if (*tree_relpath == NULL) {
1790 err = got_error_from_errno("strdup");
1791 goto done;
1793 } else {
1794 /* Check out a single file in a subdirectory. */
1795 err = got_path_dirname(tree_relpath, wt_relpath);
1796 if (err)
1797 return err;
1798 if (asprintf(&in_repo_path, "%s%s%s",
1799 worktree->path_prefix, is_root_wt ? "" : "/",
1800 *tree_relpath) == -1) {
1801 err = got_error_from_errno("asprintf");
1802 goto done;
1805 err = got_object_id_by_path(tree_id, repo,
1806 worktree->base_commit_id, in_repo_path);
1807 } else {
1808 /* Check out all files within a subdirectory. */
1809 *tree_id = got_object_id_dup(id);
1810 if (*tree_id == NULL) {
1811 err = got_error_from_errno("got_object_id_dup");
1812 goto done;
1814 *tree_relpath = strdup(wt_relpath);
1815 if (*tree_relpath == NULL) {
1816 err = got_error_from_errno("strdup");
1817 goto done;
1820 done:
1821 free(id);
1822 free(in_repo_path);
1823 if (err) {
1824 *entry_type = GOT_OBJ_TYPE_ANY;
1825 free(*tree_relpath);
1826 *tree_relpath = NULL;
1827 free(*tree_id);
1828 *tree_id = NULL;
1830 return err;
1833 static const struct got_error *
1834 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1835 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1836 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1837 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
1839 const struct got_error *err = NULL;
1840 struct got_commit_object *commit = NULL;
1841 struct got_tree_object *tree = NULL;
1842 struct got_fileindex_diff_tree_cb diff_cb;
1843 struct diff_cb_arg arg;
1845 err = ref_base_commit(worktree, repo);
1846 if (err)
1847 goto done;
1849 err = got_object_open_as_commit(&commit, repo,
1850 worktree->base_commit_id);
1851 if (err)
1852 goto done;
1854 err = got_object_open_as_tree(&tree, repo, tree_id);
1855 if (err)
1856 goto done;
1858 if (entry_name &&
1859 got_object_tree_find_entry(tree, entry_name) == NULL) {
1860 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1861 goto done;
1864 diff_cb.diff_old_new = diff_old_new;
1865 diff_cb.diff_old = diff_old;
1866 diff_cb.diff_new = diff_new;
1867 arg.fileindex = fileindex;
1868 arg.worktree = worktree;
1869 arg.repo = repo;
1870 arg.progress_cb = progress_cb;
1871 arg.progress_arg = progress_arg;
1872 arg.cancel_cb = cancel_cb;
1873 arg.cancel_arg = cancel_arg;
1874 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1875 entry_name, repo, &diff_cb, &arg);
1876 done:
1877 if (tree)
1878 got_object_tree_close(tree);
1879 if (commit)
1880 got_object_commit_close(commit);
1881 return err;
1884 const struct got_error *
1885 got_worktree_checkout_files(struct got_worktree *worktree,
1886 struct got_pathlist_head *paths, struct got_repository *repo,
1887 got_worktree_checkout_cb progress_cb, void *progress_arg,
1888 got_cancel_cb cancel_cb, void *cancel_arg)
1890 const struct got_error *err = NULL, *sync_err, *unlockerr;
1891 struct got_commit_object *commit = NULL;
1892 struct got_tree_object *tree = NULL;
1893 struct got_fileindex *fileindex = NULL;
1894 char *fileindex_path = NULL;
1895 struct got_pathlist_entry *pe;
1896 struct tree_path_data {
1897 SIMPLEQ_ENTRY(tree_path_data) entry;
1898 struct got_object_id *tree_id;
1899 int entry_type;
1900 char *relpath;
1901 char *entry_name;
1902 } *tpd = NULL;
1903 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1905 SIMPLEQ_INIT(&tree_paths);
1907 err = lock_worktree(worktree, LOCK_EX);
1908 if (err)
1909 return err;
1911 /* Map all specified paths to in-repository trees. */
1912 TAILQ_FOREACH(pe, paths, entry) {
1913 tpd = malloc(sizeof(*tpd));
1914 if (tpd == NULL) {
1915 err = got_error_from_errno("malloc");
1916 goto done;
1919 err = find_tree_entry_for_checkout(&tpd->entry_type,
1920 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1921 if (err) {
1922 free(tpd);
1923 goto done;
1926 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1927 err = got_path_basename(&tpd->entry_name, pe->path);
1928 if (err) {
1929 free(tpd->relpath);
1930 free(tpd->tree_id);
1931 free(tpd);
1932 goto done;
1934 } else
1935 tpd->entry_name = NULL;
1937 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1941 * Read the file index.
1942 * Checking out files is supposed to be an idempotent operation.
1943 * If the on-disk file index is incomplete we will try to complete it.
1945 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1946 if (err)
1947 goto done;
1949 tpd = SIMPLEQ_FIRST(&tree_paths);
1950 TAILQ_FOREACH(pe, paths, entry) {
1951 struct bump_base_commit_id_arg bbc_arg;
1953 err = checkout_files(worktree, fileindex, tpd->relpath,
1954 tpd->tree_id, tpd->entry_name, repo,
1955 progress_cb, progress_arg, cancel_cb, cancel_arg);
1956 if (err)
1957 break;
1959 bbc_arg.base_commit_id = worktree->base_commit_id;
1960 bbc_arg.entry_name = tpd->entry_name;
1961 bbc_arg.path = pe->path;
1962 bbc_arg.path_len = pe->path_len;
1963 bbc_arg.progress_cb = progress_cb;
1964 bbc_arg.progress_arg = progress_arg;
1965 err = got_fileindex_for_each_entry_safe(fileindex,
1966 bump_base_commit_id, &bbc_arg);
1967 if (err)
1968 break;
1970 tpd = SIMPLEQ_NEXT(tpd, entry);
1972 sync_err = sync_fileindex(fileindex, fileindex_path);
1973 if (sync_err && err == NULL)
1974 err = sync_err;
1975 done:
1976 free(fileindex_path);
1977 if (tree)
1978 got_object_tree_close(tree);
1979 if (commit)
1980 got_object_commit_close(commit);
1981 if (fileindex)
1982 got_fileindex_free(fileindex);
1983 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1984 tpd = SIMPLEQ_FIRST(&tree_paths);
1985 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1986 free(tpd->relpath);
1987 free(tpd->tree_id);
1988 free(tpd);
1990 unlockerr = lock_worktree(worktree, LOCK_SH);
1991 if (unlockerr && err == NULL)
1992 err = unlockerr;
1993 return err;
1996 struct merge_file_cb_arg {
1997 struct got_worktree *worktree;
1998 struct got_fileindex *fileindex;
1999 got_worktree_checkout_cb progress_cb;
2000 void *progress_arg;
2001 got_cancel_cb cancel_cb;
2002 void *cancel_arg;
2003 const char *label_orig;
2004 struct got_object_id *commit_id2;
2007 static const struct got_error *
2008 merge_file_cb(void *arg, struct got_blob_object *blob1,
2009 struct got_blob_object *blob2, struct got_object_id *id1,
2010 struct got_object_id *id2, const char *path1, const char *path2,
2011 mode_t mode1, mode_t mode2, struct got_repository *repo)
2013 static const struct got_error *err = NULL;
2014 struct merge_file_cb_arg *a = arg;
2015 struct got_fileindex_entry *ie;
2016 char *ondisk_path = NULL;
2017 struct stat sb;
2018 unsigned char status;
2019 int local_changes_subsumed;
2021 if (blob1 && blob2) {
2022 ie = got_fileindex_entry_get(a->fileindex, path2,
2023 strlen(path2));
2024 if (ie == NULL)
2025 return (*a->progress_cb)(a->progress_arg,
2026 GOT_STATUS_MISSING, path2);
2028 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2029 path2) == -1)
2030 return got_error_from_errno("asprintf");
2032 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2033 if (err)
2034 goto done;
2036 if (status == GOT_STATUS_DELETE) {
2037 err = (*a->progress_cb)(a->progress_arg,
2038 GOT_STATUS_MERGE, path2);
2039 goto done;
2041 if (status != GOT_STATUS_NO_CHANGE &&
2042 status != GOT_STATUS_MODIFY &&
2043 status != GOT_STATUS_CONFLICT &&
2044 status != GOT_STATUS_ADD) {
2045 err = (*a->progress_cb)(a->progress_arg, status, path2);
2046 goto done;
2049 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2050 ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2051 a->commit_id2, repo, a->progress_cb, a->progress_arg);
2052 } else if (blob1) {
2053 ie = got_fileindex_entry_get(a->fileindex, path1,
2054 strlen(path1));
2055 if (ie == NULL)
2056 return (*a->progress_cb)(a->progress_arg,
2057 GOT_STATUS_MISSING, path2);
2059 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2060 path1) == -1)
2061 return got_error_from_errno("asprintf");
2063 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2064 if (err)
2065 goto done;
2067 switch (status) {
2068 case GOT_STATUS_NO_CHANGE:
2069 err = (*a->progress_cb)(a->progress_arg,
2070 GOT_STATUS_DELETE, path1);
2071 if (err)
2072 goto done;
2073 err = remove_ondisk_file(a->worktree->root_path, path1);
2074 if (err)
2075 goto done;
2076 if (ie)
2077 got_fileindex_entry_mark_deleted_from_disk(ie);
2078 break;
2079 case GOT_STATUS_DELETE:
2080 case GOT_STATUS_MISSING:
2081 err = (*a->progress_cb)(a->progress_arg,
2082 GOT_STATUS_DELETE, path1);
2083 if (err)
2084 goto done;
2085 if (ie)
2086 got_fileindex_entry_mark_deleted_from_disk(ie);
2087 break;
2088 case GOT_STATUS_ADD:
2089 case GOT_STATUS_MODIFY:
2090 case GOT_STATUS_CONFLICT:
2091 err = (*a->progress_cb)(a->progress_arg,
2092 GOT_STATUS_CANNOT_DELETE, path1);
2093 if (err)
2094 goto done;
2095 break;
2096 case GOT_STATUS_OBSTRUCTED:
2097 err = (*a->progress_cb)(a->progress_arg, status, path1);
2098 if (err)
2099 goto done;
2100 break;
2101 default:
2102 break;
2104 } else if (blob2) {
2105 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2106 path2) == -1)
2107 return got_error_from_errno("asprintf");
2108 ie = got_fileindex_entry_get(a->fileindex, path2,
2109 strlen(path2));
2110 if (ie) {
2111 err = get_file_status(&status, &sb, ie, ondisk_path,
2112 repo);
2113 if (err)
2114 goto done;
2115 if (status != GOT_STATUS_NO_CHANGE &&
2116 status != GOT_STATUS_MODIFY &&
2117 status != GOT_STATUS_CONFLICT &&
2118 status != GOT_STATUS_ADD) {
2119 err = (*a->progress_cb)(a->progress_arg,
2120 status, path2);
2121 goto done;
2123 err = merge_blob(&local_changes_subsumed, a->worktree,
2124 NULL, ondisk_path, path2, sb.st_mode,
2125 a->label_orig, blob2, a->commit_id2, repo,
2126 a->progress_cb,
2127 a->progress_arg);
2128 if (status == GOT_STATUS_DELETE) {
2129 err = update_blob_fileindex_entry(a->worktree,
2130 a->fileindex, ie, ondisk_path, ie->path,
2131 blob2, 0);
2132 if (err)
2133 goto done;
2135 } else {
2136 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2137 err = install_blob(a->worktree, ondisk_path, path2,
2138 /* XXX get this from parent tree! */
2139 GOT_DEFAULT_FILE_MODE,
2140 sb.st_mode, blob2, 0, 0, repo,
2141 a->progress_cb, a->progress_arg);
2142 if (err)
2143 goto done;
2144 err = got_fileindex_entry_alloc(&ie,
2145 ondisk_path, path2, NULL, NULL);
2146 if (err)
2147 goto done;
2148 err = got_fileindex_entry_add(a->fileindex, ie);
2149 if (err) {
2150 got_fileindex_entry_free(ie);
2151 goto done;
2155 done:
2156 free(ondisk_path);
2157 return err;
2160 struct check_merge_ok_arg {
2161 struct got_worktree *worktree;
2162 struct got_repository *repo;
2165 static const struct got_error *
2166 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2168 const struct got_error *err = NULL;
2169 struct check_merge_ok_arg *a = arg;
2170 unsigned char status;
2171 struct stat sb;
2172 char *ondisk_path;
2174 /* Reject merges into a work tree with mixed base commits. */
2175 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2176 SHA1_DIGEST_LENGTH))
2177 return got_error(GOT_ERR_MIXED_COMMITS);
2179 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2180 == -1)
2181 return got_error_from_errno("asprintf");
2183 /* Reject merges into a work tree with conflicted files. */
2184 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2185 if (err)
2186 return err;
2187 if (status == GOT_STATUS_CONFLICT)
2188 return got_error(GOT_ERR_CONFLICTS);
2190 return NULL;
2193 static const struct got_error *
2194 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2195 const char *fileindex_path, struct got_object_id *commit_id1,
2196 struct got_object_id *commit_id2, struct got_repository *repo,
2197 got_worktree_checkout_cb progress_cb, void *progress_arg,
2198 got_cancel_cb cancel_cb, void *cancel_arg)
2200 const struct got_error *err = NULL, *sync_err;
2201 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2202 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2203 struct merge_file_cb_arg arg;
2204 char *label_orig = NULL;
2206 if (commit_id1) {
2207 char *id_str;
2209 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2210 worktree->path_prefix);
2211 if (err)
2212 goto done;
2214 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2215 if (err)
2216 goto done;
2218 err = got_object_id_str(&id_str, commit_id1);
2219 if (err)
2220 goto done;
2222 if (asprintf(&label_orig, "%s: commit %s",
2223 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2224 err = got_error_from_errno("asprintf");
2225 free(id_str);
2226 goto done;
2228 free(id_str);
2231 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2232 worktree->path_prefix);
2233 if (err)
2234 goto done;
2236 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2237 if (err)
2238 goto done;
2240 arg.worktree = worktree;
2241 arg.fileindex = fileindex;
2242 arg.progress_cb = progress_cb;
2243 arg.progress_arg = progress_arg;
2244 arg.cancel_cb = cancel_cb;
2245 arg.cancel_arg = cancel_arg;
2246 arg.label_orig = label_orig;
2247 arg.commit_id2 = commit_id2;
2248 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2249 sync_err = sync_fileindex(fileindex, fileindex_path);
2250 if (sync_err && err == NULL)
2251 err = sync_err;
2252 done:
2253 if (tree1)
2254 got_object_tree_close(tree1);
2255 if (tree2)
2256 got_object_tree_close(tree2);
2257 free(label_orig);
2258 return err;
2261 const struct got_error *
2262 got_worktree_merge_files(struct got_worktree *worktree,
2263 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2264 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2265 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2267 const struct got_error *err, *unlockerr;
2268 char *fileindex_path = NULL;
2269 struct got_fileindex *fileindex = NULL;
2270 struct check_merge_ok_arg mok_arg;
2272 err = lock_worktree(worktree, LOCK_EX);
2273 if (err)
2274 return err;
2276 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2277 if (err)
2278 goto done;
2280 mok_arg.worktree = worktree;
2281 mok_arg.repo = repo;
2282 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2283 &mok_arg);
2284 if (err)
2285 goto done;
2287 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2288 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2289 done:
2290 if (fileindex)
2291 got_fileindex_free(fileindex);
2292 free(fileindex_path);
2293 unlockerr = lock_worktree(worktree, LOCK_SH);
2294 if (unlockerr && err == NULL)
2295 err = unlockerr;
2296 return err;
2299 struct diff_dir_cb_arg {
2300 struct got_fileindex *fileindex;
2301 struct got_worktree *worktree;
2302 const char *status_path;
2303 size_t status_path_len;
2304 struct got_repository *repo;
2305 got_worktree_status_cb status_cb;
2306 void *status_arg;
2307 got_cancel_cb cancel_cb;
2308 void *cancel_arg;
2309 /* A pathlist containing per-directory pathlists of ignore patterns. */
2310 struct got_pathlist_head ignores;
2313 static const struct got_error *
2314 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2315 got_worktree_status_cb status_cb, void *status_arg,
2316 struct got_repository *repo)
2318 const struct got_error *err = NULL;
2319 unsigned char status = GOT_STATUS_NO_CHANGE;
2320 unsigned char staged_status = get_staged_status(ie);
2321 struct stat sb;
2322 struct got_object_id blob_id, commit_id, staged_blob_id;
2323 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2324 struct got_object_id *staged_blob_idp = NULL;
2326 err = get_file_status(&status, &sb, ie, abspath, repo);
2327 if (err)
2328 return err;
2330 if (status == GOT_STATUS_NO_CHANGE &&
2331 staged_status == GOT_STATUS_NO_CHANGE)
2332 return NULL;
2334 if (got_fileindex_entry_has_blob(ie)) {
2335 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2336 blob_idp = &blob_id;
2338 if (got_fileindex_entry_has_commit(ie)) {
2339 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2340 commit_idp = &commit_id;
2342 if (staged_status == GOT_STATUS_ADD ||
2343 staged_status == GOT_STATUS_MODIFY) {
2344 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2345 SHA1_DIGEST_LENGTH);
2346 staged_blob_idp = &staged_blob_id;
2349 return (*status_cb)(status_arg, status, staged_status,
2350 ie->path, blob_idp, staged_blob_idp, commit_idp);
2353 static const struct got_error *
2354 status_old_new(void *arg, struct got_fileindex_entry *ie,
2355 struct dirent *de, const char *parent_path)
2357 const struct got_error *err = NULL;
2358 struct diff_dir_cb_arg *a = arg;
2359 char *abspath;
2361 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2362 return got_error(GOT_ERR_CANCELLED);
2364 if (got_path_cmp(parent_path, a->status_path,
2365 strlen(parent_path), a->status_path_len) != 0 &&
2366 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2367 return NULL;
2369 if (parent_path[0]) {
2370 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2371 parent_path, de->d_name) == -1)
2372 return got_error_from_errno("asprintf");
2373 } else {
2374 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2375 de->d_name) == -1)
2376 return got_error_from_errno("asprintf");
2379 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2380 a->repo);
2381 free(abspath);
2382 return err;
2385 static const struct got_error *
2386 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2388 struct diff_dir_cb_arg *a = arg;
2389 struct got_object_id blob_id, commit_id;
2390 unsigned char status;
2392 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2393 return got_error(GOT_ERR_CANCELLED);
2395 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2396 return NULL;
2398 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2399 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2400 if (got_fileindex_entry_has_file_on_disk(ie))
2401 status = GOT_STATUS_MISSING;
2402 else
2403 status = GOT_STATUS_DELETE;
2404 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2405 ie->path, &blob_id, NULL, &commit_id);
2408 void
2409 free_ignorelist(struct got_pathlist_head *ignorelist)
2411 struct got_pathlist_entry *pe;
2413 TAILQ_FOREACH(pe, ignorelist, entry)
2414 free((char *)pe->path);
2415 got_pathlist_free(ignorelist);
2418 void
2419 free_ignores(struct got_pathlist_head *ignores)
2421 struct got_pathlist_entry *pe;
2423 TAILQ_FOREACH(pe, ignores, entry) {
2424 struct got_pathlist_head *ignorelist = pe->data;
2425 free_ignorelist(ignorelist);
2426 free((char *)pe->path);
2428 got_pathlist_free(ignores);
2431 static const struct got_error *
2432 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2434 const struct got_error *err = NULL;
2435 struct got_pathlist_entry *pe = NULL;
2436 struct got_pathlist_head *ignorelist;
2437 char *line = NULL, *pattern, *dirpath = NULL;
2438 size_t linesize = 0;
2439 ssize_t linelen;
2441 ignorelist = calloc(1, sizeof(*ignorelist));
2442 if (ignorelist == NULL)
2443 return got_error_from_errno("calloc");
2444 TAILQ_INIT(ignorelist);
2446 while ((linelen = getline(&line, &linesize, f)) != -1) {
2447 if (linelen > 0 && line[linelen - 1] == '\n')
2448 line[linelen - 1] = '\0';
2450 /* Git's ignores may contain comments. */
2451 if (line[0] == '#')
2452 continue;
2454 /* Git's negated patterns are not (yet?) supported. */
2455 if (line[0] == '!')
2456 continue;
2458 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2459 line) == -1) {
2460 err = got_error_from_errno("asprintf");
2461 goto done;
2463 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2464 if (err)
2465 goto done;
2467 if (ferror(f)) {
2468 err = got_error_from_errno("getline");
2469 goto done;
2472 dirpath = strdup(path);
2473 if (dirpath == NULL) {
2474 err = got_error_from_errno("strdup");
2475 goto done;
2477 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2478 done:
2479 free(line);
2480 if (err || pe == NULL) {
2481 free(dirpath);
2482 free_ignorelist(ignorelist);
2484 return err;
2487 int
2488 match_ignores(struct got_pathlist_head *ignores, const char *path)
2490 struct got_pathlist_entry *pe;
2492 /* Handle patterns which match in all directories. */
2493 TAILQ_FOREACH(pe, ignores, entry) {
2494 struct got_pathlist_head *ignorelist = pe->data;
2495 struct got_pathlist_entry *pi;
2497 TAILQ_FOREACH(pi, ignorelist, entry) {
2498 const char *p, *pattern = pi->path;
2500 if (strncmp(pattern, "**/", 3) != 0)
2501 continue;
2502 pattern += 3;
2503 p = path;
2504 while (*p) {
2505 if (fnmatch(pattern, p,
2506 FNM_PATHNAME | FNM_LEADING_DIR)) {
2507 /* Retry in next directory. */
2508 while (*p && *p != '/')
2509 p++;
2510 while (*p == '/')
2511 p++;
2512 continue;
2514 return 1;
2520 * The ignores pathlist contains ignore lists from children before
2521 * parents, so we can find the most specific ignorelist by walking
2522 * ignores backwards.
2524 pe = TAILQ_LAST(ignores, got_pathlist_head);
2525 while (pe) {
2526 if (got_path_is_child(path, pe->path, pe->path_len)) {
2527 struct got_pathlist_head *ignorelist = pe->data;
2528 struct got_pathlist_entry *pi;
2529 TAILQ_FOREACH(pi, ignorelist, entry) {
2530 const char *pattern = pi->path;
2531 int flags = FNM_LEADING_DIR;
2532 if (strstr(pattern, "/**/") == NULL)
2533 flags |= FNM_PATHNAME;
2534 if (fnmatch(pattern, path, flags))
2535 continue;
2536 return 1;
2539 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2542 return 0;
2545 static const struct got_error *
2546 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2547 const char *path, const char *ignores_filename)
2549 const struct got_error *err = NULL;
2550 char *ignorespath;
2551 FILE *ignoresfile = NULL;
2553 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2554 path[0] ? "/" : "", ignores_filename) == -1)
2555 return got_error_from_errno("asprintf");
2557 ignoresfile = fopen(ignorespath, "r");
2558 if (ignoresfile == NULL) {
2559 if (errno != ENOENT && errno != EACCES)
2560 err = got_error_from_errno2("fopen",
2561 ignorespath);
2562 } else
2563 err = read_ignores(ignores, path, ignoresfile);
2565 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2566 err = got_error_from_errno2("fclose", path);
2567 free(ignorespath);
2568 return err;
2571 static const struct got_error *
2572 status_new(void *arg, struct dirent *de, const char *parent_path)
2574 const struct got_error *err = NULL;
2575 struct diff_dir_cb_arg *a = arg;
2576 char *path = NULL;
2578 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2579 return got_error(GOT_ERR_CANCELLED);
2581 /* XXX ignore symlinks for now */
2582 if (de->d_type == DT_LNK)
2583 return NULL;
2585 if (parent_path[0]) {
2586 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2587 return got_error_from_errno("asprintf");
2588 } else {
2589 path = de->d_name;
2592 if (de->d_type == DT_DIR) {
2593 err = add_ignores(&a->ignores, a->worktree->root_path, path,
2594 ".cvsignore");
2595 if (err == NULL)
2596 err = add_ignores(&a->ignores, a->worktree->root_path,
2597 path, ".gitignore");
2599 else if (got_path_is_child(path, a->status_path, a->status_path_len)
2600 && !match_ignores(&a->ignores, path))
2601 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2602 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2603 if (parent_path[0])
2604 free(path);
2605 return err;
2608 static const struct got_error *
2609 report_single_file_status(const char *path, const char *ondisk_path,
2610 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2611 void *status_arg, struct got_repository *repo)
2613 struct got_fileindex_entry *ie;
2614 struct stat sb;
2616 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2617 if (ie)
2618 return report_file_status(ie, ondisk_path, status_cb,
2619 status_arg, repo);
2621 if (lstat(ondisk_path, &sb) == -1) {
2622 if (errno != ENOENT)
2623 return got_error_from_errno2("lstat", ondisk_path);
2624 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2625 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2626 return NULL;
2629 if (S_ISREG(sb.st_mode))
2630 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2631 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2633 return NULL;
2636 static const struct got_error *
2637 worktree_status(struct got_worktree *worktree, const char *path,
2638 struct got_fileindex *fileindex, struct got_repository *repo,
2639 got_worktree_status_cb status_cb, void *status_arg,
2640 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores)
2642 const struct got_error *err = NULL;
2643 DIR *workdir = NULL;
2644 struct got_fileindex_diff_dir_cb fdiff_cb;
2645 struct diff_dir_cb_arg arg;
2646 char *ondisk_path = NULL;
2648 if (asprintf(&ondisk_path, "%s%s%s",
2649 worktree->root_path, path[0] ? "/" : "", path) == -1)
2650 return got_error_from_errno("asprintf");
2652 workdir = opendir(ondisk_path);
2653 if (workdir == NULL) {
2654 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES)
2655 err = got_error_from_errno2("opendir", ondisk_path);
2656 else
2657 err = report_single_file_status(path, ondisk_path,
2658 fileindex, status_cb, status_arg, repo);
2659 } else {
2660 fdiff_cb.diff_old_new = status_old_new;
2661 fdiff_cb.diff_old = status_old;
2662 fdiff_cb.diff_new = status_new;
2663 arg.fileindex = fileindex;
2664 arg.worktree = worktree;
2665 arg.status_path = path;
2666 arg.status_path_len = strlen(path);
2667 arg.repo = repo;
2668 arg.status_cb = status_cb;
2669 arg.status_arg = status_arg;
2670 arg.cancel_cb = cancel_cb;
2671 arg.cancel_arg = cancel_arg;
2672 TAILQ_INIT(&arg.ignores);
2673 if (!no_ignores) {
2674 err = add_ignores(&arg.ignores, worktree->root_path,
2675 path, ".cvsignore");
2676 if (err == NULL)
2677 err = add_ignores(&arg.ignores,
2678 worktree->root_path, path, ".gitignore");
2680 if (err == NULL)
2681 err = got_fileindex_diff_dir(fileindex, workdir,
2682 worktree->root_path, path, repo, &fdiff_cb, &arg);
2683 free_ignores(&arg.ignores);
2686 if (workdir)
2687 closedir(workdir);
2688 free(ondisk_path);
2689 return err;
2692 const struct got_error *
2693 got_worktree_status(struct got_worktree *worktree,
2694 struct got_pathlist_head *paths, struct got_repository *repo,
2695 got_worktree_status_cb status_cb, void *status_arg,
2696 got_cancel_cb cancel_cb, void *cancel_arg)
2698 const struct got_error *err = NULL;
2699 char *fileindex_path = NULL;
2700 struct got_fileindex *fileindex = NULL;
2701 struct got_pathlist_entry *pe;
2703 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2704 if (err)
2705 return err;
2707 TAILQ_FOREACH(pe, paths, entry) {
2708 err = worktree_status(worktree, pe->path, fileindex, repo,
2709 status_cb, status_arg, cancel_cb, cancel_arg, 0);
2710 if (err)
2711 break;
2713 free(fileindex_path);
2714 got_fileindex_free(fileindex);
2715 return err;
2718 const struct got_error *
2719 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2720 const char *arg)
2722 const struct got_error *err = NULL;
2723 char *resolved, *cwd = NULL, *path = NULL;
2724 size_t len;
2726 *wt_path = NULL;
2728 resolved = realpath(arg, NULL);
2729 if (resolved == NULL) {
2730 if (errno != ENOENT)
2731 return got_error_from_errno2("realpath", arg);
2732 cwd = getcwd(NULL, 0);
2733 if (cwd == NULL)
2734 return got_error_from_errno("getcwd");
2735 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2736 err = got_error_from_errno("asprintf");
2737 goto done;
2741 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2742 strlen(got_worktree_get_root_path(worktree)))) {
2743 err = got_error(GOT_ERR_BAD_PATH);
2744 goto done;
2747 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2748 err = got_path_skip_common_ancestor(&path,
2749 got_worktree_get_root_path(worktree), resolved);
2750 if (err)
2751 goto done;
2752 } else {
2753 path = strdup("");
2754 if (path == NULL) {
2755 err = got_error_from_errno("strdup");
2756 goto done;
2760 /* XXX status walk can't deal with trailing slash! */
2761 len = strlen(path);
2762 while (len > 0 && path[len - 1] == '/') {
2763 path[len - 1] = '\0';
2764 len--;
2766 done:
2767 free(resolved);
2768 free(cwd);
2769 if (err == NULL)
2770 *wt_path = path;
2771 else
2772 free(path);
2773 return err;
2776 struct schedule_addition_args {
2777 struct got_worktree *worktree;
2778 struct got_fileindex *fileindex;
2779 got_worktree_checkout_cb progress_cb;
2780 void *progress_arg;
2781 struct got_repository *repo;
2784 static const struct got_error *
2785 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
2786 const char *relpath, struct got_object_id *blob_id,
2787 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2789 struct schedule_addition_args *a = arg;
2790 const struct got_error *err = NULL;
2791 struct got_fileindex_entry *ie;
2792 struct stat sb;
2793 char *ondisk_path;
2795 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2796 relpath) == -1)
2797 return got_error_from_errno("asprintf");
2799 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
2800 if (ie) {
2801 err = get_file_status(&status, &sb, ie, ondisk_path,
2802 a->repo);
2803 if (err)
2804 goto done;
2805 /* Re-adding an existing entry is a no-op. */
2806 if (status == GOT_STATUS_ADD)
2807 goto done;
2808 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
2809 if (err)
2810 goto done;
2813 if (status != GOT_STATUS_UNVERSIONED) {
2814 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
2815 goto done;
2818 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2819 if (err)
2820 goto done;
2822 err = got_fileindex_entry_add(a->fileindex, ie);
2823 if (err) {
2824 got_fileindex_entry_free(ie);
2825 goto done;
2827 done:
2828 free(ondisk_path);
2829 if (err)
2830 return err;
2831 if (status == GOT_STATUS_ADD)
2832 return NULL;
2833 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
2836 const struct got_error *
2837 got_worktree_schedule_add(struct got_worktree *worktree,
2838 struct got_pathlist_head *paths,
2839 got_worktree_checkout_cb progress_cb, void *progress_arg,
2840 struct got_repository *repo, int no_ignores)
2842 struct got_fileindex *fileindex = NULL;
2843 char *fileindex_path = NULL;
2844 const struct got_error *err = NULL, *sync_err, *unlockerr;
2845 struct got_pathlist_entry *pe;
2846 struct schedule_addition_args saa;
2848 err = lock_worktree(worktree, LOCK_EX);
2849 if (err)
2850 return err;
2852 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2853 if (err)
2854 goto done;
2856 saa.worktree = worktree;
2857 saa.fileindex = fileindex;
2858 saa.progress_cb = progress_cb;
2859 saa.progress_arg = progress_arg;
2860 saa.repo = repo;
2862 TAILQ_FOREACH(pe, paths, entry) {
2863 err = worktree_status(worktree, pe->path, fileindex, repo,
2864 schedule_addition, &saa, NULL, NULL, no_ignores);
2865 if (err)
2866 break;
2868 sync_err = sync_fileindex(fileindex, fileindex_path);
2869 if (sync_err && err == NULL)
2870 err = sync_err;
2871 done:
2872 free(fileindex_path);
2873 if (fileindex)
2874 got_fileindex_free(fileindex);
2875 unlockerr = lock_worktree(worktree, LOCK_SH);
2876 if (unlockerr && err == NULL)
2877 err = unlockerr;
2878 return err;
2881 static const struct got_error *
2882 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2883 const char *relpath, int delete_local_mods,
2884 got_worktree_status_cb status_cb, void *status_arg,
2885 struct got_repository *repo)
2887 const struct got_error *err = NULL;
2888 struct got_fileindex_entry *ie = NULL;
2889 unsigned char status, staged_status;
2890 struct stat sb;
2892 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2893 if (ie == NULL)
2894 return got_error(GOT_ERR_BAD_PATH);
2896 staged_status = get_staged_status(ie);
2897 if (staged_status != GOT_STATUS_NO_CHANGE) {
2898 if (staged_status == GOT_STATUS_DELETE)
2899 return NULL;
2900 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
2903 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2904 if (err)
2905 return err;
2907 if (status != GOT_STATUS_NO_CHANGE) {
2908 if (status == GOT_STATUS_DELETE)
2909 return NULL;
2910 if (status == GOT_STATUS_MODIFY && !delete_local_mods)
2911 return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
2912 if (status != GOT_STATUS_MODIFY &&
2913 status != GOT_STATUS_MISSING)
2914 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
2917 if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
2918 return got_error_from_errno2("unlink", ondisk_path);
2920 got_fileindex_entry_mark_deleted_from_disk(ie);
2921 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2924 const struct got_error *
2925 got_worktree_schedule_delete(struct got_worktree *worktree,
2926 struct got_pathlist_head *paths, int delete_local_mods,
2927 got_worktree_status_cb status_cb, void *status_arg,
2928 struct got_repository *repo)
2930 struct got_fileindex *fileindex = NULL;
2931 char *fileindex_path = NULL;
2932 const struct got_error *err = NULL, *sync_err, *unlockerr;
2933 struct got_pathlist_entry *pe;
2935 err = lock_worktree(worktree, LOCK_EX);
2936 if (err)
2937 return err;
2939 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2940 if (err)
2941 goto done;
2943 TAILQ_FOREACH(pe, paths, entry) {
2944 char *ondisk_path;
2945 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
2946 pe->path) == -1)
2947 return got_error_from_errno("asprintf");
2948 err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
2949 delete_local_mods, status_cb, status_arg, repo);
2950 free(ondisk_path);
2951 if (err)
2952 break;
2954 sync_err = sync_fileindex(fileindex, fileindex_path);
2955 if (sync_err && err == NULL)
2956 err = sync_err;
2957 done:
2958 free(fileindex_path);
2959 if (fileindex)
2960 got_fileindex_free(fileindex);
2961 unlockerr = lock_worktree(worktree, LOCK_SH);
2962 if (unlockerr && err == NULL)
2963 err = unlockerr;
2964 return err;
2967 static const struct got_error *
2968 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
2970 const struct got_error *err = NULL;
2971 char *line = NULL;
2972 size_t linesize = 0, n;
2973 ssize_t linelen;
2975 linelen = getline(&line, &linesize, infile);
2976 if (linelen == -1) {
2977 if (ferror(infile)) {
2978 err = got_error_from_errno("getline");
2979 goto done;
2981 return NULL;
2983 if (outfile) {
2984 n = fwrite(line, 1, linelen, outfile);
2985 if (n != linelen) {
2986 err = got_ferror(outfile, GOT_ERR_IO);
2987 goto done;
2990 if (rejectfile) {
2991 n = fwrite(line, 1, linelen, rejectfile);
2992 if (n != linelen)
2993 err = got_ferror(outfile, GOT_ERR_IO);
2995 done:
2996 free(line);
2997 return err;
3000 static const struct got_error *
3001 skip_one_line(FILE *f)
3003 char *line = NULL;
3004 size_t linesize = 0;
3005 ssize_t linelen;
3007 linelen = getline(&line, &linesize, f);
3008 if (linelen == -1) {
3009 if (ferror(f))
3010 return got_error_from_errno("getline");
3011 return NULL;
3013 free(line);
3014 return NULL;
3017 static const struct got_error *
3018 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3019 int start_old, int end_old, int start_new, int end_new,
3020 FILE *outfile, FILE *rejectfile)
3022 const struct got_error *err;
3024 /* Copy old file's lines leading up to patch. */
3025 while (!feof(f1) && *line_cur1 < start_old) {
3026 err = copy_one_line(f1, outfile, NULL);
3027 if (err)
3028 return err;
3029 (*line_cur1)++;
3031 /* Skip new file's lines leading up to patch. */
3032 while (!feof(f2) && *line_cur2 < start_new) {
3033 if (rejectfile)
3034 err = copy_one_line(f2, NULL, rejectfile);
3035 else
3036 err = skip_one_line(f2);
3037 if (err)
3038 return err;
3039 (*line_cur2)++;
3041 /* Copy patched lines. */
3042 while (!feof(f2) && *line_cur2 <= end_new) {
3043 err = copy_one_line(f2, outfile, NULL);
3044 if (err)
3045 return err;
3046 (*line_cur2)++;
3048 /* Skip over old file's replaced lines. */
3049 while (!feof(f1) && *line_cur1 <= end_old) {
3050 if (rejectfile)
3051 err = copy_one_line(f1, NULL, rejectfile);
3052 else
3053 err = skip_one_line(f1);
3054 if (err)
3055 return err;
3056 (*line_cur1)++;
3059 return NULL;
3062 static const struct got_error *
3063 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3064 FILE *outfile, FILE *rejectfile)
3066 const struct got_error *err;
3068 if (outfile) {
3069 /* Copy old file's lines until EOF. */
3070 while (!feof(f1)) {
3071 err = copy_one_line(f1, outfile, NULL);
3072 if (err)
3073 return err;
3074 (*line_cur1)++;
3077 if (rejectfile) {
3078 /* Copy new file's lines until EOF. */
3079 while (!feof(f2)) {
3080 err = copy_one_line(f2, NULL, rejectfile);
3081 if (err)
3082 return err;
3083 (*line_cur2)++;
3087 return NULL;
3090 static const struct got_error *
3091 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3092 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3093 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3094 int *line_cur2, FILE *outfile, FILE *rejectfile,
3095 got_worktree_patch_cb patch_cb, void *patch_arg)
3097 const struct got_error *err = NULL;
3098 int start_old = change->cv.a;
3099 int end_old = change->cv.b;
3100 int start_new = change->cv.c;
3101 int end_new = change->cv.d;
3102 long pos1, pos2;
3103 FILE *hunkfile;
3105 *choice = GOT_PATCH_CHOICE_NONE;
3107 hunkfile = got_opentemp();
3108 if (hunkfile == NULL)
3109 return got_error_from_errno("got_opentemp");
3111 pos1 = ftell(f1);
3112 pos2 = ftell(f2);
3114 /* XXX TODO needs error checking */
3115 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3117 if (fseek(f1, pos1, SEEK_SET) == -1) {
3118 err = got_ferror(f1, GOT_ERR_IO);
3119 goto done;
3121 if (fseek(f2, pos2, SEEK_SET) == -1) {
3122 err = got_ferror(f1, GOT_ERR_IO);
3123 goto done;
3125 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3126 err = got_ferror(hunkfile, GOT_ERR_IO);
3127 goto done;
3130 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3131 hunkfile, n, nchanges);
3132 if (err)
3133 goto done;
3135 switch (*choice) {
3136 case GOT_PATCH_CHOICE_YES:
3137 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3138 end_old, start_new, end_new, outfile, rejectfile);
3139 break;
3140 case GOT_PATCH_CHOICE_NO:
3141 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3142 end_old, start_new, end_new, rejectfile, outfile);
3143 break;
3144 case GOT_PATCH_CHOICE_QUIT:
3145 break;
3146 default:
3147 err = got_error(GOT_ERR_PATCH_CHOICE);
3148 break;
3150 done:
3151 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3152 err = got_error_from_errno("fclose");
3153 return err;
3156 struct revert_file_args {
3157 struct got_worktree *worktree;
3158 struct got_fileindex *fileindex;
3159 got_worktree_checkout_cb progress_cb;
3160 void *progress_arg;
3161 got_worktree_patch_cb patch_cb;
3162 void *patch_arg;
3163 struct got_repository *repo;
3166 static const struct got_error *
3167 create_patched_content(char **path_outfile, int reverse_patch,
3168 struct got_object_id *blob_id, const char *path2,
3169 const char *relpath, struct got_repository *repo,
3170 got_worktree_patch_cb patch_cb, void *patch_arg)
3172 const struct got_error *err;
3173 struct got_blob_object *blob = NULL;
3174 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3175 int fd2 = -1;
3176 char *path1 = NULL, *id_str = NULL;
3177 struct stat sb1, sb2;
3178 struct got_diff_changes *changes = NULL;
3179 struct got_diff_state *ds = NULL;
3180 struct got_diff_args *args = NULL;
3181 struct got_diff_change *change;
3182 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3183 int n = 0;
3185 *path_outfile = NULL;
3187 err = got_object_id_str(&id_str, blob_id);
3188 if (err)
3189 return err;
3191 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3192 if (fd2 == -1) {
3193 err = got_error_from_errno2("open", path2);
3194 goto done;
3196 if (fstat(fd2, &sb2) == -1) {
3197 err = got_error_from_errno2("fstat", path2);
3198 goto done;
3201 f2 = fdopen(fd2, "r");
3202 if (f2 == NULL) {
3203 err = got_error_from_errno2("fopen", path2);
3204 goto done;
3206 fd2 = -1;
3208 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3209 if (err)
3210 goto done;
3212 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3213 if (err)
3214 goto done;
3216 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3217 if (err)
3218 goto done;
3220 if (stat(path1, &sb1) == -1) {
3221 err = got_error_from_errno2("stat", path1);
3222 goto done;
3225 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3226 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3227 if (err)
3228 goto done;
3230 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3231 if (err)
3232 goto done;
3234 if (fseek(f1, 0L, SEEK_SET) == -1)
3235 return got_ferror(f1, GOT_ERR_IO);
3236 if (fseek(f2, 0L, SEEK_SET) == -1)
3237 return got_ferror(f2, GOT_ERR_IO);
3238 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3239 int choice;
3240 err = apply_or_reject_change(&choice, change, ++n,
3241 changes->nchanges, ds, args, diff_flags, relpath,
3242 f1, f2, &line_cur1, &line_cur2,
3243 reverse_patch ? NULL : outfile,
3244 reverse_patch ? outfile : NULL,
3245 patch_cb, patch_arg);
3246 if (err)
3247 goto done;
3248 if (choice == GOT_PATCH_CHOICE_YES)
3249 have_content = 1;
3250 else if (choice == GOT_PATCH_CHOICE_QUIT)
3251 break;
3253 if (have_content) {
3254 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3255 reverse_patch ? NULL : outfile,
3256 reverse_patch ? outfile : NULL);
3257 if (err)
3258 goto done;
3260 if (chmod(*path_outfile, sb2.st_mode) == -1) {
3261 err = got_error_from_errno2("chmod", path2);
3262 goto done;
3265 done:
3266 free(id_str);
3267 if (blob)
3268 got_object_blob_close(blob);
3269 if (f1 && fclose(f1) == EOF && err == NULL)
3270 err = got_error_from_errno2("fclose", path1);
3271 if (f2 && fclose(f2) == EOF && err == NULL)
3272 err = got_error_from_errno2("fclose", path2);
3273 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3274 err = got_error_from_errno2("close", path2);
3275 if (outfile && fclose(outfile) == EOF && err == NULL)
3276 err = got_error_from_errno2("fclose", *path_outfile);
3277 if (path1 && unlink(path1) == -1 && err == NULL)
3278 err = got_error_from_errno2("unlink", path1);
3279 if (err || !have_content) {
3280 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3281 err = got_error_from_errno2("unlink", *path_outfile);
3282 free(*path_outfile);
3283 *path_outfile = NULL;
3285 free(args);
3286 if (ds) {
3287 got_diff_state_free(ds);
3288 free(ds);
3290 if (changes)
3291 got_diff_free_changes(changes);
3292 free(path1);
3293 return err;
3296 static const struct got_error *
3297 revert_file(void *arg, unsigned char status, unsigned char staged_status,
3298 const char *relpath, struct got_object_id *blob_id,
3299 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3301 struct revert_file_args *a = arg;
3302 const struct got_error *err = NULL;
3303 char *parent_path = NULL;
3304 struct got_fileindex_entry *ie;
3305 struct got_tree_object *tree = NULL;
3306 struct got_object_id *tree_id = NULL;
3307 const struct got_tree_entry *te = NULL;
3308 char *tree_path = NULL, *te_name;
3309 char *ondisk_path = NULL, *path_content = NULL;
3310 struct got_blob_object *blob = NULL;
3312 /* Reverting a staged deletion is a no-op. */
3313 if (status == GOT_STATUS_DELETE &&
3314 staged_status != GOT_STATUS_NO_CHANGE)
3315 return NULL;
3317 if (status == GOT_STATUS_UNVERSIONED)
3318 return (*a->progress_cb)(a->progress_arg,
3319 GOT_STATUS_UNVERSIONED, relpath);
3321 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3322 if (ie == NULL)
3323 return got_error(GOT_ERR_BAD_PATH);
3325 /* Construct in-repository path of tree which contains this blob. */
3326 err = got_path_dirname(&parent_path, ie->path);
3327 if (err) {
3328 if (err->code != GOT_ERR_BAD_PATH)
3329 goto done;
3330 parent_path = strdup("/");
3331 if (parent_path == NULL) {
3332 err = got_error_from_errno("strdup");
3333 goto done;
3336 if (got_path_is_root_dir(a->worktree->path_prefix)) {
3337 tree_path = strdup(parent_path);
3338 if (tree_path == NULL) {
3339 err = got_error_from_errno("strdup");
3340 goto done;
3342 } else {
3343 if (got_path_is_root_dir(parent_path)) {
3344 tree_path = strdup(a->worktree->path_prefix);
3345 if (tree_path == NULL) {
3346 err = got_error_from_errno("strdup");
3347 goto done;
3349 } else {
3350 if (asprintf(&tree_path, "%s/%s",
3351 a->worktree->path_prefix, parent_path) == -1) {
3352 err = got_error_from_errno("asprintf");
3353 goto done;
3358 err = got_object_id_by_path(&tree_id, a->repo,
3359 a->worktree->base_commit_id, tree_path);
3360 if (err) {
3361 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3362 (status == GOT_STATUS_ADD ||
3363 staged_status == GOT_STATUS_ADD)))
3364 goto done;
3365 } else {
3366 err = got_object_open_as_tree(&tree, a->repo, tree_id);
3367 if (err)
3368 goto done;
3370 te_name = basename(ie->path);
3371 if (te_name == NULL) {
3372 err = got_error_from_errno2("basename", ie->path);
3373 goto done;
3376 te = got_object_tree_find_entry(tree, te_name);
3377 if (te == NULL && status != GOT_STATUS_ADD &&
3378 staged_status != GOT_STATUS_ADD) {
3379 err = got_error(GOT_ERR_NO_TREE_ENTRY);
3380 goto done;
3384 switch (status) {
3385 case GOT_STATUS_ADD:
3386 if (a->patch_cb) {
3387 int choice = GOT_PATCH_CHOICE_NONE;
3388 err = (*a->patch_cb)(&choice, a->patch_arg,
3389 status, ie->path, NULL, 1, 1);
3390 if (err)
3391 goto done;
3392 if (choice != GOT_PATCH_CHOICE_YES)
3393 break;
3395 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3396 ie->path);
3397 if (err)
3398 goto done;
3399 got_fileindex_entry_remove(a->fileindex, ie);
3400 break;
3401 case GOT_STATUS_DELETE:
3402 if (a->patch_cb) {
3403 int choice = GOT_PATCH_CHOICE_NONE;
3404 err = (*a->patch_cb)(&choice, a->patch_arg,
3405 status, ie->path, NULL, 1, 1);
3406 if (err)
3407 goto done;
3408 if (choice != GOT_PATCH_CHOICE_YES)
3409 break;
3411 /* fall through */
3412 case GOT_STATUS_MODIFY:
3413 case GOT_STATUS_MODE_CHANGE:
3414 case GOT_STATUS_CONFLICT:
3415 case GOT_STATUS_MISSING: {
3416 struct got_object_id id;
3417 if (staged_status == GOT_STATUS_ADD ||
3418 staged_status == GOT_STATUS_MODIFY) {
3419 memcpy(id.sha1, ie->staged_blob_sha1,
3420 SHA1_DIGEST_LENGTH);
3421 } else
3422 memcpy(id.sha1, ie->blob_sha1,
3423 SHA1_DIGEST_LENGTH);
3424 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3425 if (err)
3426 goto done;
3428 if (asprintf(&ondisk_path, "%s/%s",
3429 got_worktree_get_root_path(a->worktree), relpath) == -1) {
3430 err = got_error_from_errno("asprintf");
3431 goto done;
3434 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3435 status == GOT_STATUS_CONFLICT)) {
3436 err = create_patched_content(&path_content, 1, &id,
3437 ondisk_path, ie->path, a->repo,
3438 a->patch_cb, a->patch_arg);
3439 if (err || path_content == NULL)
3440 break;
3441 if (rename(path_content, ondisk_path) == -1) {
3442 err = got_error_from_errno3("rename",
3443 path_content, ondisk_path);
3444 goto done;
3446 } else {
3447 err = install_blob(a->worktree, ondisk_path, ie->path,
3448 te ? te->mode : GOT_DEFAULT_FILE_MODE,
3449 got_fileindex_perms_to_st(ie), blob, 0, 1,
3450 a->repo, a->progress_cb, a->progress_arg);
3451 if (err)
3452 goto done;
3453 if (status == GOT_STATUS_DELETE ||
3454 status == GOT_STATUS_MODE_CHANGE) {
3455 err = update_blob_fileindex_entry(a->worktree,
3456 a->fileindex, ie, ondisk_path, ie->path,
3457 blob, 1);
3458 if (err)
3459 goto done;
3462 break;
3464 default:
3465 break;
3467 done:
3468 free(ondisk_path);
3469 free(path_content);
3470 free(parent_path);
3471 free(tree_path);
3472 if (blob)
3473 got_object_blob_close(blob);
3474 if (tree)
3475 got_object_tree_close(tree);
3476 free(tree_id);
3477 return err;
3480 const struct got_error *
3481 got_worktree_revert(struct got_worktree *worktree,
3482 struct got_pathlist_head *paths,
3483 got_worktree_checkout_cb progress_cb, void *progress_arg,
3484 got_worktree_patch_cb patch_cb, void *patch_arg,
3485 struct got_repository *repo)
3487 struct got_fileindex *fileindex = NULL;
3488 char *fileindex_path = NULL;
3489 const struct got_error *err = NULL, *unlockerr = NULL;
3490 const struct got_error *sync_err = NULL;
3491 struct got_pathlist_entry *pe;
3492 struct revert_file_args rfa;
3494 err = lock_worktree(worktree, LOCK_EX);
3495 if (err)
3496 return err;
3498 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3499 if (err)
3500 goto done;
3502 rfa.worktree = worktree;
3503 rfa.fileindex = fileindex;
3504 rfa.progress_cb = progress_cb;
3505 rfa.progress_arg = progress_arg;
3506 rfa.patch_cb = patch_cb;
3507 rfa.patch_arg = patch_arg;
3508 rfa.repo = repo;
3509 TAILQ_FOREACH(pe, paths, entry) {
3510 err = worktree_status(worktree, pe->path, fileindex, repo,
3511 revert_file, &rfa, NULL, NULL, 0);
3512 if (err)
3513 break;
3515 sync_err = sync_fileindex(fileindex, fileindex_path);
3516 if (sync_err && err == NULL)
3517 err = sync_err;
3518 done:
3519 free(fileindex_path);
3520 if (fileindex)
3521 got_fileindex_free(fileindex);
3522 unlockerr = lock_worktree(worktree, LOCK_SH);
3523 if (unlockerr && err == NULL)
3524 err = unlockerr;
3525 return err;
3528 static void
3529 free_commitable(struct got_commitable *ct)
3531 free(ct->path);
3532 free(ct->in_repo_path);
3533 free(ct->ondisk_path);
3534 free(ct->blob_id);
3535 free(ct->base_blob_id);
3536 free(ct->staged_blob_id);
3537 free(ct->base_commit_id);
3538 free(ct);
3541 struct collect_commitables_arg {
3542 struct got_pathlist_head *commitable_paths;
3543 struct got_repository *repo;
3544 struct got_worktree *worktree;
3545 int have_staged_files;
3548 static const struct got_error *
3549 collect_commitables(void *arg, unsigned char status,
3550 unsigned char staged_status, const char *relpath,
3551 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3552 struct got_object_id *commit_id)
3554 struct collect_commitables_arg *a = arg;
3555 const struct got_error *err = NULL;
3556 struct got_commitable *ct = NULL;
3557 struct got_pathlist_entry *new = NULL;
3558 char *parent_path = NULL, *path = NULL;
3559 struct stat sb;
3561 if (a->have_staged_files) {
3562 if (staged_status != GOT_STATUS_MODIFY &&
3563 staged_status != GOT_STATUS_ADD &&
3564 staged_status != GOT_STATUS_DELETE)
3565 return NULL;
3566 } else {
3567 if (status == GOT_STATUS_CONFLICT)
3568 return got_error(GOT_ERR_COMMIT_CONFLICT);
3570 if (status != GOT_STATUS_MODIFY &&
3571 status != GOT_STATUS_MODE_CHANGE &&
3572 status != GOT_STATUS_ADD &&
3573 status != GOT_STATUS_DELETE)
3574 return NULL;
3577 if (asprintf(&path, "/%s", relpath) == -1) {
3578 err = got_error_from_errno("asprintf");
3579 goto done;
3581 if (strcmp(path, "/") == 0) {
3582 parent_path = strdup("");
3583 if (parent_path == NULL)
3584 return got_error_from_errno("strdup");
3585 } else {
3586 err = got_path_dirname(&parent_path, path);
3587 if (err)
3588 return err;
3591 ct = calloc(1, sizeof(*ct));
3592 if (ct == NULL) {
3593 err = got_error_from_errno("calloc");
3594 goto done;
3597 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
3598 relpath) == -1) {
3599 err = got_error_from_errno("asprintf");
3600 goto done;
3602 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
3603 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3604 } else {
3605 if (lstat(ct->ondisk_path, &sb) != 0) {
3606 err = got_error_from_errno2("lstat", ct->ondisk_path);
3607 goto done;
3609 ct->mode = sb.st_mode;
3612 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
3613 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
3614 relpath) == -1) {
3615 err = got_error_from_errno("asprintf");
3616 goto done;
3619 ct->status = status;
3620 ct->staged_status = staged_status;
3621 ct->blob_id = NULL; /* will be filled in when blob gets created */
3622 if (ct->status != GOT_STATUS_ADD &&
3623 ct->staged_status != GOT_STATUS_ADD) {
3624 ct->base_blob_id = got_object_id_dup(blob_id);
3625 if (ct->base_blob_id == NULL) {
3626 err = got_error_from_errno("got_object_id_dup");
3627 goto done;
3629 ct->base_commit_id = got_object_id_dup(commit_id);
3630 if (ct->base_commit_id == NULL) {
3631 err = got_error_from_errno("got_object_id_dup");
3632 goto done;
3635 if (ct->staged_status == GOT_STATUS_ADD ||
3636 ct->staged_status == GOT_STATUS_MODIFY) {
3637 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
3638 if (ct->staged_blob_id == NULL) {
3639 err = got_error_from_errno("got_object_id_dup");
3640 goto done;
3643 ct->path = strdup(path);
3644 if (ct->path == NULL) {
3645 err = got_error_from_errno("strdup");
3646 goto done;
3648 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
3649 done:
3650 if (ct && (err || new == NULL))
3651 free_commitable(ct);
3652 free(parent_path);
3653 free(path);
3654 return err;
3657 static const struct got_error *write_tree(struct got_object_id **,
3658 struct got_tree_object *, const char *, struct got_pathlist_head *,
3659 got_worktree_status_cb status_cb, void *status_arg,
3660 struct got_repository *);
3662 static const struct got_error *
3663 write_subtree(struct got_object_id **new_subtree_id,
3664 struct got_tree_entry *te, const char *parent_path,
3665 struct got_pathlist_head *commitable_paths,
3666 got_worktree_status_cb status_cb, void *status_arg,
3667 struct got_repository *repo)
3669 const struct got_error *err = NULL;
3670 struct got_tree_object *subtree;
3671 char *subpath;
3673 if (asprintf(&subpath, "%s%s%s", parent_path,
3674 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
3675 return got_error_from_errno("asprintf");
3677 err = got_object_open_as_tree(&subtree, repo, &te->id);
3678 if (err)
3679 return err;
3681 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
3682 status_cb, status_arg, repo);
3683 got_object_tree_close(subtree);
3684 free(subpath);
3685 return err;
3688 static const struct got_error *
3689 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
3691 const struct got_error *err = NULL;
3692 char *ct_parent_path = NULL;
3694 *match = 0;
3696 if (strchr(ct->in_repo_path, '/') == NULL) {
3697 *match = got_path_is_root_dir(path);
3698 return NULL;
3701 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
3702 if (err)
3703 return err;
3704 *match = (strcmp(path, ct_parent_path) == 0);
3705 free(ct_parent_path);
3706 return err;
3709 static mode_t
3710 get_ct_file_mode(struct got_commitable *ct)
3712 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
3715 static const struct got_error *
3716 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
3717 struct got_tree_entry *te, struct got_commitable *ct)
3719 const struct got_error *err = NULL;
3721 *new_te = NULL;
3723 err = got_object_tree_entry_dup(new_te, te);
3724 if (err)
3725 goto done;
3727 (*new_te)->mode = get_ct_file_mode(ct);
3729 if (ct->staged_status == GOT_STATUS_MODIFY)
3730 memcpy(&(*new_te)->id, ct->staged_blob_id,
3731 sizeof((*new_te)->id));
3732 else
3733 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3734 done:
3735 if (err && *new_te) {
3736 free(*new_te);
3737 *new_te = NULL;
3739 return err;
3742 static const struct got_error *
3743 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
3744 struct got_commitable *ct)
3746 const struct got_error *err = NULL;
3747 char *ct_name;
3749 *new_te = NULL;
3751 *new_te = calloc(1, sizeof(**new_te));
3752 if (*new_te == NULL)
3753 return got_error_from_errno("calloc");
3755 ct_name = basename(ct->path);
3756 if (ct_name == NULL) {
3757 err = got_error_from_errno2("basename", ct->path);
3758 goto done;
3760 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
3761 sizeof((*new_te)->name)) {
3762 err = got_error(GOT_ERR_NO_SPACE);
3763 goto done;
3766 (*new_te)->mode = get_ct_file_mode(ct);
3768 if (ct->staged_status == GOT_STATUS_ADD)
3769 memcpy(&(*new_te)->id, ct->staged_blob_id,
3770 sizeof((*new_te)->id));
3771 else
3772 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
3773 done:
3774 if (err && *new_te) {
3775 free(*new_te);
3776 *new_te = NULL;
3778 return err;
3781 static const struct got_error *
3782 insert_tree_entry(struct got_tree_entry *new_te,
3783 struct got_pathlist_head *paths)
3785 const struct got_error *err = NULL;
3786 struct got_pathlist_entry *new_pe;
3788 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3789 if (err)
3790 return err;
3791 if (new_pe == NULL)
3792 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3793 return NULL;
3796 static const struct got_error *
3797 report_ct_status(struct got_commitable *ct,
3798 got_worktree_status_cb status_cb, void *status_arg)
3800 const char *ct_path = ct->path;
3801 unsigned char status;
3803 while (ct_path[0] == '/')
3804 ct_path++;
3806 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
3807 status = ct->staged_status;
3808 else
3809 status = ct->status;
3811 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
3812 ct_path, ct->blob_id, NULL, NULL);
3815 static const struct got_error *
3816 match_modified_subtree(int *modified, struct got_tree_entry *te,
3817 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3819 const struct got_error *err = NULL;
3820 struct got_pathlist_entry *pe;
3821 char *te_path;
3823 *modified = 0;
3825 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3826 got_path_is_root_dir(base_tree_path) ? "" : "/",
3827 te->name) == -1)
3828 return got_error_from_errno("asprintf");
3830 TAILQ_FOREACH(pe, commitable_paths, entry) {
3831 struct got_commitable *ct = pe->data;
3832 *modified = got_path_is_child(ct->in_repo_path, te_path,
3833 strlen(te_path));
3834 if (*modified)
3835 break;
3838 free(te_path);
3839 return err;
3842 static const struct got_error *
3843 match_deleted_or_modified_ct(struct got_commitable **ctp,
3844 struct got_tree_entry *te, const char *base_tree_path,
3845 struct got_pathlist_head *commitable_paths)
3847 const struct got_error *err = NULL;
3848 struct got_pathlist_entry *pe;
3850 *ctp = NULL;
3852 TAILQ_FOREACH(pe, commitable_paths, entry) {
3853 struct got_commitable *ct = pe->data;
3854 char *ct_name = NULL;
3855 int path_matches;
3857 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
3858 if (ct->status != GOT_STATUS_MODIFY &&
3859 ct->status != GOT_STATUS_MODE_CHANGE &&
3860 ct->status != GOT_STATUS_DELETE)
3861 continue;
3862 } else {
3863 if (ct->staged_status != GOT_STATUS_MODIFY &&
3864 ct->staged_status != GOT_STATUS_DELETE)
3865 continue;
3868 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
3869 continue;
3871 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3872 if (err)
3873 return err;
3874 if (!path_matches)
3875 continue;
3877 ct_name = basename(pe->path);
3878 if (ct_name == NULL)
3879 return got_error_from_errno2("basename", pe->path);
3881 if (strcmp(te->name, ct_name) != 0)
3882 continue;
3884 *ctp = ct;
3885 break;
3888 return err;
3891 static const struct got_error *
3892 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3893 const char *child_path, const char *path_base_tree,
3894 struct got_pathlist_head *commitable_paths,
3895 got_worktree_status_cb status_cb, void *status_arg,
3896 struct got_repository *repo)
3898 const struct got_error *err = NULL;
3899 struct got_tree_entry *new_te;
3900 char *subtree_path;
3901 struct got_object_id *id = NULL;
3903 *new_tep = NULL;
3905 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3906 got_path_is_root_dir(path_base_tree) ? "" : "/",
3907 child_path) == -1)
3908 return got_error_from_errno("asprintf");
3910 new_te = calloc(1, sizeof(*new_te));
3911 if (new_te == NULL)
3912 return got_error_from_errno("calloc");
3913 new_te->mode = S_IFDIR;
3915 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
3916 sizeof(new_te->name)) {
3917 err = got_error(GOT_ERR_NO_SPACE);
3918 goto done;
3920 err = write_tree(&id, NULL, subtree_path,
3921 commitable_paths, status_cb, status_arg, repo);
3922 if (err) {
3923 free(new_te);
3924 goto done;
3926 memcpy(&new_te->id, id, sizeof(new_te->id));
3927 done:
3928 free(id);
3929 free(subtree_path);
3930 if (err == NULL)
3931 *new_tep = new_te;
3932 return err;
3935 static const struct got_error *
3936 write_tree(struct got_object_id **new_tree_id,
3937 struct got_tree_object *base_tree, const char *path_base_tree,
3938 struct got_pathlist_head *commitable_paths,
3939 got_worktree_status_cb status_cb, void *status_arg,
3940 struct got_repository *repo)
3942 const struct got_error *err = NULL;
3943 struct got_pathlist_head paths;
3944 struct got_tree_entry *te, *new_te = NULL;
3945 struct got_pathlist_entry *pe;
3946 int nentries = 0;
3948 TAILQ_INIT(&paths);
3950 /* Insert, and recurse into, newly added entries first. */
3951 TAILQ_FOREACH(pe, commitable_paths, entry) {
3952 struct got_commitable *ct = pe->data;
3953 char *child_path = NULL, *slash;
3955 if ((ct->status != GOT_STATUS_ADD &&
3956 ct->staged_status != GOT_STATUS_ADD) ||
3957 (ct->flags & GOT_COMMITABLE_ADDED))
3958 continue;
3960 if (!got_path_is_child(pe->path, path_base_tree,
3961 strlen(path_base_tree)))
3962 continue;
3964 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3965 pe->path);
3966 if (err)
3967 goto done;
3969 slash = strchr(child_path, '/');
3970 if (slash == NULL) {
3971 err = alloc_added_blob_tree_entry(&new_te, ct);
3972 if (err)
3973 goto done;
3974 err = report_ct_status(ct, status_cb, status_arg);
3975 if (err)
3976 goto done;
3977 ct->flags |= GOT_COMMITABLE_ADDED;
3978 err = insert_tree_entry(new_te, &paths);
3979 if (err)
3980 goto done;
3981 nentries++;
3982 } else {
3983 *slash = '\0'; /* trim trailing path components */
3984 if (base_tree == NULL ||
3985 got_object_tree_find_entry(base_tree, child_path)
3986 == NULL) {
3987 err = make_subtree_for_added_blob(&new_te,
3988 child_path, path_base_tree,
3989 commitable_paths, status_cb, status_arg,
3990 repo);
3991 if (err)
3992 goto done;
3993 err = insert_tree_entry(new_te, &paths);
3994 if (err)
3995 goto done;
3996 nentries++;
4001 if (base_tree) {
4002 int i, nbase_entries;
4003 /* Handle modified and deleted entries. */
4004 nbase_entries = got_object_tree_get_nentries(base_tree);
4005 for (i = 0; i < nbase_entries; i++) {
4006 struct got_commitable *ct = NULL;
4008 te = got_object_tree_get_entry(base_tree, i);
4009 if (got_object_tree_entry_is_submodule(te)) {
4010 /* Entry is a submodule; just copy it. */
4011 err = got_object_tree_entry_dup(&new_te, te);
4012 if (err)
4013 goto done;
4014 err = insert_tree_entry(new_te, &paths);
4015 if (err)
4016 goto done;
4017 nentries++;
4018 continue;
4021 if (S_ISDIR(te->mode)) {
4022 int modified;
4023 err = got_object_tree_entry_dup(&new_te, te);
4024 if (err)
4025 goto done;
4026 err = match_modified_subtree(&modified, te,
4027 path_base_tree, commitable_paths);
4028 if (err)
4029 goto done;
4030 /* Avoid recursion into unmodified subtrees. */
4031 if (modified) {
4032 struct got_object_id *new_id;
4033 err = write_subtree(&new_id, te,
4034 path_base_tree, commitable_paths,
4035 status_cb, status_arg, repo);
4036 if (err)
4037 goto done;
4038 memcpy(&new_te->id, new_id,
4039 sizeof(new_te->id));
4040 free(new_id);
4042 err = insert_tree_entry(new_te, &paths);
4043 if (err)
4044 goto done;
4045 nentries++;
4046 continue;
4049 err = match_deleted_or_modified_ct(&ct, te,
4050 path_base_tree, commitable_paths);
4051 if (err)
4052 goto done;
4053 if (ct) {
4054 /* NB: Deleted entries get dropped here. */
4055 if (ct->status == GOT_STATUS_MODIFY ||
4056 ct->status == GOT_STATUS_MODE_CHANGE ||
4057 ct->staged_status == GOT_STATUS_MODIFY) {
4058 err = alloc_modified_blob_tree_entry(
4059 &new_te, te, ct);
4060 if (err)
4061 goto done;
4062 err = insert_tree_entry(new_te, &paths);
4063 if (err)
4064 goto done;
4065 nentries++;
4067 err = report_ct_status(ct, status_cb,
4068 status_arg);
4069 if (err)
4070 goto done;
4071 } else {
4072 /* Entry is unchanged; just copy it. */
4073 err = got_object_tree_entry_dup(&new_te, te);
4074 if (err)
4075 goto done;
4076 err = insert_tree_entry(new_te, &paths);
4077 if (err)
4078 goto done;
4079 nentries++;
4084 /* Write new list of entries; deleted entries have been dropped. */
4085 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
4086 done:
4087 got_pathlist_free(&paths);
4088 return err;
4091 static const struct got_error *
4092 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4093 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4094 int have_staged_files)
4096 const struct got_error *err = NULL;
4097 struct got_pathlist_entry *pe;
4099 TAILQ_FOREACH(pe, commitable_paths, entry) {
4100 struct got_fileindex_entry *ie;
4101 struct got_commitable *ct = pe->data;
4103 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4104 if (ie) {
4105 if (ct->status == GOT_STATUS_DELETE ||
4106 ct->staged_status == GOT_STATUS_DELETE) {
4107 got_fileindex_entry_remove(fileindex, ie);
4108 got_fileindex_entry_free(ie);
4109 } else if (ct->staged_status == GOT_STATUS_ADD ||
4110 ct->staged_status == GOT_STATUS_MODIFY) {
4111 got_fileindex_entry_stage_set(ie,
4112 GOT_FILEIDX_STAGE_NONE);
4113 err = got_fileindex_entry_update(ie,
4114 ct->ondisk_path, ct->staged_blob_id->sha1,
4115 new_base_commit_id->sha1,
4116 !have_staged_files);
4117 } else
4118 err = got_fileindex_entry_update(ie,
4119 ct->ondisk_path, ct->blob_id->sha1,
4120 new_base_commit_id->sha1,
4121 !have_staged_files);
4122 } else {
4123 err = got_fileindex_entry_alloc(&ie,
4124 ct->ondisk_path, pe->path, ct->blob_id->sha1,
4125 new_base_commit_id->sha1);
4126 if (err)
4127 break;
4128 err = got_fileindex_entry_add(fileindex, ie);
4129 if (err)
4130 break;
4133 return err;
4137 static const struct got_error *
4138 check_out_of_date(const char *in_repo_path, unsigned char status,
4139 unsigned char staged_status, struct got_object_id *base_blob_id,
4140 struct got_object_id *base_commit_id,
4141 struct got_object_id *head_commit_id, struct got_repository *repo,
4142 int ood_errcode)
4144 const struct got_error *err = NULL;
4145 struct got_object_id *id = NULL;
4147 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4148 /* Trivial case: base commit == head commit */
4149 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4150 return NULL;
4152 * Ensure file content which local changes were based
4153 * on matches file content in the branch head.
4155 err = got_object_id_by_path(&id, repo, head_commit_id,
4156 in_repo_path);
4157 if (err) {
4158 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4159 err = got_error(ood_errcode);
4160 goto done;
4161 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4162 err = got_error(ood_errcode);
4163 } else {
4164 /* Require that added files don't exist in the branch head. */
4165 err = got_object_id_by_path(&id, repo, head_commit_id,
4166 in_repo_path);
4167 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4168 goto done;
4169 err = id ? got_error(ood_errcode) : NULL;
4171 done:
4172 free(id);
4173 return err;
4176 const struct got_error *
4177 commit_worktree(struct got_object_id **new_commit_id,
4178 struct got_pathlist_head *commitable_paths,
4179 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4180 const char *author, const char *committer,
4181 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4182 got_worktree_status_cb status_cb, void *status_arg,
4183 struct got_repository *repo)
4185 const struct got_error *err = NULL, *unlockerr = NULL;
4186 struct got_pathlist_entry *pe;
4187 const char *head_ref_name = NULL;
4188 struct got_commit_object *head_commit = NULL;
4189 struct got_reference *head_ref2 = NULL;
4190 struct got_object_id *head_commit_id2 = NULL;
4191 struct got_tree_object *head_tree = NULL;
4192 struct got_object_id *new_tree_id = NULL;
4193 struct got_object_id_queue parent_ids;
4194 struct got_object_qid *pid = NULL;
4195 char *logmsg = NULL;
4197 *new_commit_id = NULL;
4199 SIMPLEQ_INIT(&parent_ids);
4201 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4202 if (err)
4203 goto done;
4205 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4206 if (err)
4207 goto done;
4209 if (commit_msg_cb != NULL) {
4210 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4211 if (err)
4212 goto done;
4215 if (logmsg == NULL || strlen(logmsg) == 0) {
4216 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4217 goto done;
4220 /* Create blobs from added and modified files and record their IDs. */
4221 TAILQ_FOREACH(pe, commitable_paths, entry) {
4222 struct got_commitable *ct = pe->data;
4223 char *ondisk_path;
4225 /* Blobs for staged files already exist. */
4226 if (ct->staged_status == GOT_STATUS_ADD ||
4227 ct->staged_status == GOT_STATUS_MODIFY)
4228 continue;
4230 if (ct->status != GOT_STATUS_ADD &&
4231 ct->status != GOT_STATUS_MODIFY &&
4232 ct->status != GOT_STATUS_MODE_CHANGE)
4233 continue;
4235 if (asprintf(&ondisk_path, "%s/%s",
4236 worktree->root_path, pe->path) == -1) {
4237 err = got_error_from_errno("asprintf");
4238 goto done;
4240 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4241 free(ondisk_path);
4242 if (err)
4243 goto done;
4246 /* Recursively write new tree objects. */
4247 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
4248 status_cb, status_arg, repo);
4249 if (err)
4250 goto done;
4252 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4253 if (err)
4254 goto done;
4255 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4256 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4257 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4258 got_object_qid_free(pid);
4259 if (logmsg != NULL)
4260 free(logmsg);
4261 if (err)
4262 goto done;
4264 /* Check if a concurrent commit to our branch has occurred. */
4265 head_ref_name = got_worktree_get_head_ref_name(worktree);
4266 if (head_ref_name == NULL) {
4267 err = got_error_from_errno("got_worktree_get_head_ref_name");
4268 goto done;
4270 /* Lock the reference here to prevent concurrent modification. */
4271 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4272 if (err)
4273 goto done;
4274 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4275 if (err)
4276 goto done;
4277 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4278 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4279 goto done;
4281 /* Update branch head in repository. */
4282 err = got_ref_change_ref(head_ref2, *new_commit_id);
4283 if (err)
4284 goto done;
4285 err = got_ref_write(head_ref2, repo);
4286 if (err)
4287 goto done;
4289 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4290 if (err)
4291 goto done;
4293 err = ref_base_commit(worktree, repo);
4294 if (err)
4295 goto done;
4296 done:
4297 if (head_tree)
4298 got_object_tree_close(head_tree);
4299 if (head_commit)
4300 got_object_commit_close(head_commit);
4301 free(head_commit_id2);
4302 if (head_ref2) {
4303 unlockerr = got_ref_unlock(head_ref2);
4304 if (unlockerr && err == NULL)
4305 err = unlockerr;
4306 got_ref_close(head_ref2);
4308 return err;
4311 static const struct got_error *
4312 check_path_is_commitable(const char *path,
4313 struct got_pathlist_head *commitable_paths)
4315 struct got_pathlist_entry *cpe = NULL;
4316 size_t path_len = strlen(path);
4318 TAILQ_FOREACH(cpe, commitable_paths, entry) {
4319 struct got_commitable *ct = cpe->data;
4320 const char *ct_path = ct->path;
4322 while (ct_path[0] == '/')
4323 ct_path++;
4325 if (strcmp(path, ct_path) == 0 ||
4326 got_path_is_child(ct_path, path, path_len))
4327 break;
4330 if (cpe == NULL)
4331 return got_error_path(path, GOT_ERR_BAD_PATH);
4333 return NULL;
4336 static const struct got_error *
4337 check_staged_file(void *arg, struct got_fileindex_entry *ie)
4339 int *have_staged_files = arg;
4341 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4342 *have_staged_files = 1;
4343 return got_error(GOT_ERR_CANCELLED);
4346 return NULL;
4349 static const struct got_error *
4350 check_non_staged_files(struct got_fileindex *fileindex,
4351 struct got_pathlist_head *paths)
4353 struct got_pathlist_entry *pe;
4354 struct got_fileindex_entry *ie;
4356 TAILQ_FOREACH(pe, paths, entry) {
4357 if (pe->path[0] == '\0')
4358 continue;
4359 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4360 if (ie == NULL)
4361 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4362 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4363 return got_error_path(pe->path,
4364 GOT_ERR_FILE_NOT_STAGED);
4367 return NULL;
4370 const struct got_error *
4371 got_worktree_commit(struct got_object_id **new_commit_id,
4372 struct got_worktree *worktree, struct got_pathlist_head *paths,
4373 const char *author, const char *committer,
4374 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4375 got_worktree_status_cb status_cb, void *status_arg,
4376 struct got_repository *repo)
4378 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4379 struct got_fileindex *fileindex = NULL;
4380 char *fileindex_path = NULL;
4381 struct got_pathlist_head commitable_paths;
4382 struct collect_commitables_arg cc_arg;
4383 struct got_pathlist_entry *pe;
4384 struct got_reference *head_ref = NULL;
4385 struct got_object_id *head_commit_id = NULL;
4386 int have_staged_files = 0;
4388 *new_commit_id = NULL;
4390 TAILQ_INIT(&commitable_paths);
4392 err = lock_worktree(worktree, LOCK_EX);
4393 if (err)
4394 goto done;
4396 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4397 if (err)
4398 goto done;
4400 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4401 if (err)
4402 goto done;
4404 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4405 if (err)
4406 goto done;
4408 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4409 &have_staged_files);
4410 if (err && err->code != GOT_ERR_CANCELLED)
4411 goto done;
4412 if (have_staged_files) {
4413 err = check_non_staged_files(fileindex, paths);
4414 if (err)
4415 goto done;
4418 cc_arg.commitable_paths = &commitable_paths;
4419 cc_arg.worktree = worktree;
4420 cc_arg.repo = repo;
4421 cc_arg.have_staged_files = have_staged_files;
4422 TAILQ_FOREACH(pe, paths, entry) {
4423 err = worktree_status(worktree, pe->path, fileindex, repo,
4424 collect_commitables, &cc_arg, NULL, NULL, 0);
4425 if (err)
4426 goto done;
4429 if (TAILQ_EMPTY(&commitable_paths)) {
4430 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4431 goto done;
4434 TAILQ_FOREACH(pe, paths, entry) {
4435 err = check_path_is_commitable(pe->path, &commitable_paths);
4436 if (err)
4437 goto done;
4440 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4441 struct got_commitable *ct = pe->data;
4442 const char *ct_path = ct->in_repo_path;
4444 while (ct_path[0] == '/')
4445 ct_path++;
4446 err = check_out_of_date(ct_path, ct->status,
4447 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4448 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4449 if (err)
4450 goto done;
4454 err = commit_worktree(new_commit_id, &commitable_paths,
4455 head_commit_id, worktree, author, committer,
4456 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4457 if (err)
4458 goto done;
4460 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4461 fileindex, have_staged_files);
4462 sync_err = sync_fileindex(fileindex, fileindex_path);
4463 if (sync_err && err == NULL)
4464 err = sync_err;
4465 done:
4466 if (fileindex)
4467 got_fileindex_free(fileindex);
4468 free(fileindex_path);
4469 unlockerr = lock_worktree(worktree, LOCK_SH);
4470 if (unlockerr && err == NULL)
4471 err = unlockerr;
4472 TAILQ_FOREACH(pe, &commitable_paths, entry) {
4473 struct got_commitable *ct = pe->data;
4474 free_commitable(ct);
4476 got_pathlist_free(&commitable_paths);
4477 return err;
4480 const char *
4481 got_commitable_get_path(struct got_commitable *ct)
4483 return ct->path;
4486 unsigned int
4487 got_commitable_get_status(struct got_commitable *ct)
4489 return ct->status;
4492 struct check_rebase_ok_arg {
4493 struct got_worktree *worktree;
4494 struct got_repository *repo;
4497 static const struct got_error *
4498 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4500 const struct got_error *err = NULL;
4501 struct check_rebase_ok_arg *a = arg;
4502 unsigned char status;
4503 struct stat sb;
4504 char *ondisk_path;
4506 /* Reject rebase of a work tree with mixed base commits. */
4507 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4508 SHA1_DIGEST_LENGTH))
4509 return got_error(GOT_ERR_MIXED_COMMITS);
4511 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4512 == -1)
4513 return got_error_from_errno("asprintf");
4515 /* Reject rebase of a work tree with modified or staged files. */
4516 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
4517 free(ondisk_path);
4518 if (err)
4519 return err;
4521 if (status != GOT_STATUS_NO_CHANGE)
4522 return got_error(GOT_ERR_MODIFIED);
4523 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4524 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4526 return NULL;
4529 const struct got_error *
4530 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
4531 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
4532 struct got_worktree *worktree, struct got_reference *branch,
4533 struct got_repository *repo)
4535 const struct got_error *err = NULL;
4536 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4537 char *branch_ref_name = NULL;
4538 char *fileindex_path = NULL;
4539 struct check_rebase_ok_arg ok_arg;
4540 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
4542 *new_base_branch_ref = NULL;
4543 *tmp_branch = NULL;
4544 *fileindex = NULL;
4546 err = lock_worktree(worktree, LOCK_EX);
4547 if (err)
4548 return err;
4550 err = open_fileindex(fileindex, &fileindex_path, worktree);
4551 if (err)
4552 goto done;
4554 ok_arg.worktree = worktree;
4555 ok_arg.repo = repo;
4556 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4557 &ok_arg);
4558 if (err)
4559 goto done;
4561 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4562 if (err)
4563 goto done;
4565 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4566 if (err)
4567 goto done;
4569 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4570 if (err)
4571 goto done;
4573 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4574 0);
4575 if (err)
4576 goto done;
4578 err = got_ref_alloc_symref(new_base_branch_ref,
4579 new_base_branch_ref_name, wt_branch);
4580 if (err)
4581 goto done;
4582 err = got_ref_write(*new_base_branch_ref, repo);
4583 if (err)
4584 goto done;
4586 /* TODO Lock original branch's ref while rebasing? */
4588 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
4589 if (err)
4590 goto done;
4592 err = got_ref_write(branch_ref, repo);
4593 if (err)
4594 goto done;
4596 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4597 worktree->base_commit_id);
4598 if (err)
4599 goto done;
4600 err = got_ref_write(*tmp_branch, repo);
4601 if (err)
4602 goto done;
4604 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4605 if (err)
4606 goto done;
4607 done:
4608 free(fileindex_path);
4609 free(tmp_branch_name);
4610 free(new_base_branch_ref_name);
4611 free(branch_ref_name);
4612 if (branch_ref)
4613 got_ref_close(branch_ref);
4614 if (wt_branch)
4615 got_ref_close(wt_branch);
4616 if (err) {
4617 if (*new_base_branch_ref) {
4618 got_ref_close(*new_base_branch_ref);
4619 *new_base_branch_ref = NULL;
4621 if (*tmp_branch) {
4622 got_ref_close(*tmp_branch);
4623 *tmp_branch = NULL;
4625 if (*fileindex) {
4626 got_fileindex_free(*fileindex);
4627 *fileindex = NULL;
4629 lock_worktree(worktree, LOCK_SH);
4631 return err;
4634 const struct got_error *
4635 got_worktree_rebase_continue(struct got_object_id **commit_id,
4636 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
4637 struct got_reference **branch, struct got_fileindex **fileindex,
4638 struct got_worktree *worktree, struct got_repository *repo)
4640 const struct got_error *err;
4641 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
4642 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4643 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
4644 char *fileindex_path = NULL;
4645 int have_staged_files = 0;
4647 *commit_id = NULL;
4648 *new_base_branch = NULL;
4649 *tmp_branch = NULL;
4650 *branch = NULL;
4651 *fileindex = NULL;
4653 err = lock_worktree(worktree, LOCK_EX);
4654 if (err)
4655 return err;
4657 err = open_fileindex(fileindex, &fileindex_path, worktree);
4658 if (err)
4659 goto done;
4661 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
4662 &have_staged_files);
4663 if (err && err->code != GOT_ERR_CANCELLED)
4664 goto done;
4665 if (have_staged_files) {
4666 err = got_error(GOT_ERR_STAGED_PATHS);
4667 goto done;
4670 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4671 if (err)
4672 goto done;
4674 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4675 if (err)
4676 goto done;
4678 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4679 if (err)
4680 goto done;
4682 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4683 if (err)
4684 goto done;
4686 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
4687 if (err)
4688 goto done;
4690 err = got_ref_open(branch, repo,
4691 got_ref_get_symref_target(branch_ref), 0);
4692 if (err)
4693 goto done;
4695 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4696 if (err)
4697 goto done;
4699 err = got_ref_resolve(commit_id, repo, commit_ref);
4700 if (err)
4701 goto done;
4703 err = got_ref_open(new_base_branch, repo,
4704 new_base_branch_ref_name, 0);
4705 if (err)
4706 goto done;
4708 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4709 if (err)
4710 goto done;
4711 done:
4712 free(commit_ref_name);
4713 free(branch_ref_name);
4714 free(fileindex_path);
4715 if (commit_ref)
4716 got_ref_close(commit_ref);
4717 if (branch_ref)
4718 got_ref_close(branch_ref);
4719 if (err) {
4720 free(*commit_id);
4721 *commit_id = NULL;
4722 if (*tmp_branch) {
4723 got_ref_close(*tmp_branch);
4724 *tmp_branch = NULL;
4726 if (*new_base_branch) {
4727 got_ref_close(*new_base_branch);
4728 *new_base_branch = NULL;
4730 if (*branch) {
4731 got_ref_close(*branch);
4732 *branch = NULL;
4734 if (*fileindex) {
4735 got_fileindex_free(*fileindex);
4736 *fileindex = NULL;
4738 lock_worktree(worktree, LOCK_SH);
4740 return err;
4743 const struct got_error *
4744 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
4746 const struct got_error *err;
4747 char *tmp_branch_name = NULL;
4749 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4750 if (err)
4751 return err;
4753 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4754 free(tmp_branch_name);
4755 return NULL;
4758 static const struct got_error *
4759 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
4760 char **logmsg, void *arg)
4762 *logmsg = arg;
4763 return NULL;
4766 static const struct got_error *
4767 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
4768 const char *path, struct got_object_id *blob_id,
4769 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
4771 return NULL;
4774 struct collect_merged_paths_arg {
4775 got_worktree_checkout_cb progress_cb;
4776 void *progress_arg;
4777 struct got_pathlist_head *merged_paths;
4780 static const struct got_error *
4781 collect_merged_paths(void *arg, unsigned char status, const char *path)
4783 const struct got_error *err;
4784 struct collect_merged_paths_arg *a = arg;
4785 char *p;
4786 struct got_pathlist_entry *new;
4788 err = (*a->progress_cb)(a->progress_arg, status, path);
4789 if (err)
4790 return err;
4792 if (status != GOT_STATUS_MERGE &&
4793 status != GOT_STATUS_ADD &&
4794 status != GOT_STATUS_DELETE &&
4795 status != GOT_STATUS_CONFLICT)
4796 return NULL;
4798 p = strdup(path);
4799 if (p == NULL)
4800 return got_error_from_errno("strdup");
4802 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
4803 if (err || new == NULL)
4804 free(p);
4805 return err;
4808 void
4809 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
4811 struct got_pathlist_entry *pe;
4813 TAILQ_FOREACH(pe, merged_paths, entry)
4814 free((char *)pe->path);
4816 got_pathlist_free(merged_paths);
4819 static const struct got_error *
4820 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
4821 struct got_repository *repo)
4823 const struct got_error *err;
4824 struct got_reference *commit_ref = NULL;
4826 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4827 if (err) {
4828 if (err->code != GOT_ERR_NOT_REF)
4829 goto done;
4830 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
4831 if (err)
4832 goto done;
4833 err = got_ref_write(commit_ref, repo);
4834 if (err)
4835 goto done;
4836 } else {
4837 struct got_object_id *stored_id;
4838 int cmp;
4840 err = got_ref_resolve(&stored_id, repo, commit_ref);
4841 if (err)
4842 goto done;
4843 cmp = got_object_id_cmp(commit_id, stored_id);
4844 free(stored_id);
4845 if (cmp != 0) {
4846 err = got_error(GOT_ERR_REBASE_COMMITID);
4847 goto done;
4850 done:
4851 if (commit_ref)
4852 got_ref_close(commit_ref);
4853 return err;
4856 static const struct got_error *
4857 rebase_merge_files(struct got_pathlist_head *merged_paths,
4858 const char *commit_ref_name, struct got_worktree *worktree,
4859 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
4860 struct got_object_id *commit_id, struct got_repository *repo,
4861 got_worktree_checkout_cb progress_cb, void *progress_arg,
4862 got_cancel_cb cancel_cb, void *cancel_arg)
4864 const struct got_error *err;
4865 struct got_reference *commit_ref = NULL;
4866 struct collect_merged_paths_arg cmp_arg;
4867 char *fileindex_path;
4869 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4871 err = get_fileindex_path(&fileindex_path, worktree);
4872 if (err)
4873 return err;
4875 cmp_arg.progress_cb = progress_cb;
4876 cmp_arg.progress_arg = progress_arg;
4877 cmp_arg.merged_paths = merged_paths;
4878 err = merge_files(worktree, fileindex, fileindex_path,
4879 parent_commit_id, commit_id, repo, collect_merged_paths,
4880 &cmp_arg, cancel_cb, cancel_arg);
4881 if (commit_ref)
4882 got_ref_close(commit_ref);
4883 return err;
4886 const struct got_error *
4887 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4888 struct got_worktree *worktree, struct got_fileindex *fileindex,
4889 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4890 struct got_repository *repo,
4891 got_worktree_checkout_cb progress_cb, void *progress_arg,
4892 got_cancel_cb cancel_cb, void *cancel_arg)
4894 const struct got_error *err;
4895 char *commit_ref_name;
4897 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4898 if (err)
4899 return err;
4901 err = store_commit_id(commit_ref_name, commit_id, repo);
4902 if (err)
4903 goto done;
4905 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4906 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4907 progress_arg, cancel_cb, cancel_arg);
4908 done:
4909 free(commit_ref_name);
4910 return err;
4913 const struct got_error *
4914 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4915 struct got_worktree *worktree, struct got_fileindex *fileindex,
4916 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4917 struct got_repository *repo,
4918 got_worktree_checkout_cb progress_cb, void *progress_arg,
4919 got_cancel_cb cancel_cb, void *cancel_arg)
4921 const struct got_error *err;
4922 char *commit_ref_name;
4924 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4925 if (err)
4926 return err;
4928 err = store_commit_id(commit_ref_name, commit_id, repo);
4929 if (err)
4930 goto done;
4932 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4933 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4934 progress_arg, cancel_cb, cancel_arg);
4935 done:
4936 free(commit_ref_name);
4937 return err;
4940 static const struct got_error *
4941 rebase_commit(struct got_object_id **new_commit_id,
4942 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4943 struct got_worktree *worktree, struct got_fileindex *fileindex,
4944 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4945 const char *new_logmsg, struct got_repository *repo)
4947 const struct got_error *err, *sync_err;
4948 struct got_pathlist_head commitable_paths;
4949 struct collect_commitables_arg cc_arg;
4950 char *fileindex_path = NULL;
4951 struct got_reference *head_ref = NULL;
4952 struct got_object_id *head_commit_id = NULL;
4953 char *logmsg = NULL;
4955 TAILQ_INIT(&commitable_paths);
4956 *new_commit_id = NULL;
4958 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4960 err = get_fileindex_path(&fileindex_path, worktree);
4961 if (err)
4962 return err;
4964 cc_arg.commitable_paths = &commitable_paths;
4965 cc_arg.worktree = worktree;
4966 cc_arg.repo = repo;
4967 cc_arg.have_staged_files = 0;
4969 * If possible get the status of individual files directly to
4970 * avoid crawling the entire work tree once per rebased commit.
4971 * TODO: Ideally, merged_paths would contain a list of commitables
4972 * we could use so we could skip worktree_status() entirely.
4974 if (merged_paths) {
4975 struct got_pathlist_entry *pe;
4976 if (TAILQ_EMPTY(merged_paths)) {
4977 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4978 goto done;
4980 TAILQ_FOREACH(pe, merged_paths, entry) {
4981 err = worktree_status(worktree, pe->path, fileindex,
4982 repo, collect_commitables, &cc_arg, NULL, NULL, 0);
4983 if (err)
4984 goto done;
4986 } else {
4987 err = worktree_status(worktree, "", fileindex, repo,
4988 collect_commitables, &cc_arg, NULL, NULL, 0);
4989 if (err)
4990 goto done;
4993 if (TAILQ_EMPTY(&commitable_paths)) {
4994 /* No-op change; commit will be elided. */
4995 err = got_ref_delete(commit_ref, repo);
4996 if (err)
4997 goto done;
4998 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4999 goto done;
5002 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5003 if (err)
5004 goto done;
5006 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5007 if (err)
5008 goto done;
5010 if (new_logmsg) {
5011 logmsg = strdup(new_logmsg);
5012 if (logmsg == NULL) {
5013 err = got_error_from_errno("strdup");
5014 goto done;
5016 } else {
5017 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5018 if (err)
5019 goto done;
5022 /* NB: commit_worktree will call free(logmsg) */
5023 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5024 worktree, got_object_commit_get_author(orig_commit),
5025 got_object_commit_get_committer(orig_commit),
5026 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5027 if (err)
5028 goto done;
5030 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5031 if (err)
5032 goto done;
5034 err = got_ref_delete(commit_ref, repo);
5035 if (err)
5036 goto done;
5038 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5039 fileindex, 0);
5040 sync_err = sync_fileindex(fileindex, fileindex_path);
5041 if (sync_err && err == NULL)
5042 err = sync_err;
5043 done:
5044 free(fileindex_path);
5045 free(head_commit_id);
5046 if (head_ref)
5047 got_ref_close(head_ref);
5048 if (err) {
5049 free(*new_commit_id);
5050 *new_commit_id = NULL;
5052 return err;
5055 const struct got_error *
5056 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5057 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5058 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5059 struct got_commit_object *orig_commit,
5060 struct got_object_id *orig_commit_id, struct got_repository *repo)
5062 const struct got_error *err;
5063 char *commit_ref_name;
5064 struct got_reference *commit_ref = NULL;
5065 struct got_object_id *commit_id = NULL;
5067 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5068 if (err)
5069 return err;
5071 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5072 if (err)
5073 goto done;
5074 err = got_ref_resolve(&commit_id, repo, commit_ref);
5075 if (err)
5076 goto done;
5077 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5078 err = got_error(GOT_ERR_REBASE_COMMITID);
5079 goto done;
5082 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5083 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5084 done:
5085 if (commit_ref)
5086 got_ref_close(commit_ref);
5087 free(commit_ref_name);
5088 free(commit_id);
5089 return err;
5092 const struct got_error *
5093 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5094 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5095 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5096 struct got_commit_object *orig_commit,
5097 struct got_object_id *orig_commit_id, const char *new_logmsg,
5098 struct got_repository *repo)
5100 const struct got_error *err;
5101 char *commit_ref_name;
5102 struct got_reference *commit_ref = NULL;
5103 struct got_object_id *commit_id = NULL;
5105 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5106 if (err)
5107 return err;
5109 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5110 if (err)
5111 goto done;
5112 err = got_ref_resolve(&commit_id, repo, commit_ref);
5113 if (err)
5114 goto done;
5115 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5116 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
5117 goto done;
5120 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5121 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5122 done:
5123 if (commit_ref)
5124 got_ref_close(commit_ref);
5125 free(commit_ref_name);
5126 free(commit_id);
5127 return err;
5130 const struct got_error *
5131 got_worktree_rebase_postpone(struct got_worktree *worktree,
5132 struct got_fileindex *fileindex)
5134 if (fileindex)
5135 got_fileindex_free(fileindex);
5136 return lock_worktree(worktree, LOCK_SH);
5139 static const struct got_error *
5140 delete_ref(const char *name, struct got_repository *repo)
5142 const struct got_error *err;
5143 struct got_reference *ref;
5145 err = got_ref_open(&ref, repo, name, 0);
5146 if (err) {
5147 if (err->code == GOT_ERR_NOT_REF)
5148 return NULL;
5149 return err;
5152 err = got_ref_delete(ref, repo);
5153 got_ref_close(ref);
5154 return err;
5157 static const struct got_error *
5158 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5160 const struct got_error *err;
5161 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5162 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5164 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5165 if (err)
5166 goto done;
5167 err = delete_ref(tmp_branch_name, repo);
5168 if (err)
5169 goto done;
5171 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5172 if (err)
5173 goto done;
5174 err = delete_ref(new_base_branch_ref_name, repo);
5175 if (err)
5176 goto done;
5178 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5179 if (err)
5180 goto done;
5181 err = delete_ref(branch_ref_name, repo);
5182 if (err)
5183 goto done;
5185 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5186 if (err)
5187 goto done;
5188 err = delete_ref(commit_ref_name, repo);
5189 if (err)
5190 goto done;
5192 done:
5193 free(tmp_branch_name);
5194 free(new_base_branch_ref_name);
5195 free(branch_ref_name);
5196 free(commit_ref_name);
5197 return err;
5200 const struct got_error *
5201 got_worktree_rebase_complete(struct got_worktree *worktree,
5202 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5203 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5204 struct got_repository *repo)
5206 const struct got_error *err, *unlockerr;
5207 struct got_object_id *new_head_commit_id = NULL;
5209 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5210 if (err)
5211 return err;
5213 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5214 if (err)
5215 goto done;
5217 err = got_ref_write(rebased_branch, repo);
5218 if (err)
5219 goto done;
5221 err = got_worktree_set_head_ref(worktree, rebased_branch);
5222 if (err)
5223 goto done;
5225 err = delete_rebase_refs(worktree, repo);
5226 done:
5227 if (fileindex)
5228 got_fileindex_free(fileindex);
5229 free(new_head_commit_id);
5230 unlockerr = lock_worktree(worktree, LOCK_SH);
5231 if (unlockerr && err == NULL)
5232 err = unlockerr;
5233 return err;
5236 const struct got_error *
5237 got_worktree_rebase_abort(struct got_worktree *worktree,
5238 struct got_fileindex *fileindex, struct got_repository *repo,
5239 struct got_reference *new_base_branch,
5240 got_worktree_checkout_cb progress_cb, void *progress_arg)
5242 const struct got_error *err, *unlockerr, *sync_err;
5243 struct got_reference *resolved = NULL;
5244 struct got_object_id *commit_id = NULL;
5245 char *fileindex_path = NULL;
5246 struct revert_file_args rfa;
5247 struct got_object_id *tree_id = NULL;
5249 err = lock_worktree(worktree, LOCK_EX);
5250 if (err)
5251 return err;
5253 err = got_ref_open(&resolved, repo,
5254 got_ref_get_symref_target(new_base_branch), 0);
5255 if (err)
5256 goto done;
5258 err = got_worktree_set_head_ref(worktree, resolved);
5259 if (err)
5260 goto done;
5263 * XXX commits to the base branch could have happened while
5264 * we were busy rebasing; should we store the original commit ID
5265 * when rebase begins and read it back here?
5267 err = got_ref_resolve(&commit_id, repo, resolved);
5268 if (err)
5269 goto done;
5271 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5272 if (err)
5273 goto done;
5275 err = got_object_id_by_path(&tree_id, repo,
5276 worktree->base_commit_id, worktree->path_prefix);
5277 if (err)
5278 goto done;
5280 err = delete_rebase_refs(worktree, repo);
5281 if (err)
5282 goto done;
5284 err = get_fileindex_path(&fileindex_path, worktree);
5285 if (err)
5286 goto done;
5288 rfa.worktree = worktree;
5289 rfa.fileindex = fileindex;
5290 rfa.progress_cb = progress_cb;
5291 rfa.progress_arg = progress_arg;
5292 rfa.patch_cb = NULL;
5293 rfa.patch_arg = NULL;
5294 rfa.repo = repo;
5295 err = worktree_status(worktree, "", fileindex, repo,
5296 revert_file, &rfa, NULL, NULL, 0);
5297 if (err)
5298 goto sync;
5300 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5301 repo, progress_cb, progress_arg, NULL, NULL);
5302 sync:
5303 sync_err = sync_fileindex(fileindex, fileindex_path);
5304 if (sync_err && err == NULL)
5305 err = sync_err;
5306 done:
5307 got_ref_close(resolved);
5308 free(tree_id);
5309 free(commit_id);
5310 if (fileindex)
5311 got_fileindex_free(fileindex);
5312 free(fileindex_path);
5314 unlockerr = lock_worktree(worktree, LOCK_SH);
5315 if (unlockerr && err == NULL)
5316 err = unlockerr;
5317 return err;
5320 const struct got_error *
5321 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5322 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5323 struct got_fileindex **fileindex, struct got_worktree *worktree,
5324 struct got_repository *repo)
5326 const struct got_error *err = NULL;
5327 char *tmp_branch_name = NULL;
5328 char *branch_ref_name = NULL;
5329 char *base_commit_ref_name = NULL;
5330 char *fileindex_path = NULL;
5331 struct check_rebase_ok_arg ok_arg;
5332 struct got_reference *wt_branch = NULL;
5333 struct got_reference *base_commit_ref = NULL;
5335 *tmp_branch = NULL;
5336 *branch_ref = NULL;
5337 *base_commit_id = NULL;
5338 *fileindex = NULL;
5340 err = lock_worktree(worktree, LOCK_EX);
5341 if (err)
5342 return err;
5344 err = open_fileindex(fileindex, &fileindex_path, worktree);
5345 if (err)
5346 goto done;
5348 ok_arg.worktree = worktree;
5349 ok_arg.repo = repo;
5350 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5351 &ok_arg);
5352 if (err)
5353 goto done;
5355 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5356 if (err)
5357 goto done;
5359 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5360 if (err)
5361 goto done;
5363 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5364 worktree);
5365 if (err)
5366 goto done;
5368 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5369 0);
5370 if (err)
5371 goto done;
5373 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5374 if (err)
5375 goto done;
5377 err = got_ref_write(*branch_ref, repo);
5378 if (err)
5379 goto done;
5381 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5382 worktree->base_commit_id);
5383 if (err)
5384 goto done;
5385 err = got_ref_write(base_commit_ref, repo);
5386 if (err)
5387 goto done;
5388 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5389 if (*base_commit_id == NULL) {
5390 err = got_error_from_errno("got_object_id_dup");
5391 goto done;
5394 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5395 worktree->base_commit_id);
5396 if (err)
5397 goto done;
5398 err = got_ref_write(*tmp_branch, repo);
5399 if (err)
5400 goto done;
5402 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5403 if (err)
5404 goto done;
5405 done:
5406 free(fileindex_path);
5407 free(tmp_branch_name);
5408 free(branch_ref_name);
5409 free(base_commit_ref_name);
5410 if (wt_branch)
5411 got_ref_close(wt_branch);
5412 if (err) {
5413 if (*branch_ref) {
5414 got_ref_close(*branch_ref);
5415 *branch_ref = NULL;
5417 if (*tmp_branch) {
5418 got_ref_close(*tmp_branch);
5419 *tmp_branch = NULL;
5421 free(*base_commit_id);
5422 if (*fileindex) {
5423 got_fileindex_free(*fileindex);
5424 *fileindex = NULL;
5426 lock_worktree(worktree, LOCK_SH);
5428 return err;
5431 const struct got_error *
5432 got_worktree_histedit_postpone(struct got_worktree *worktree,
5433 struct got_fileindex *fileindex)
5435 if (fileindex)
5436 got_fileindex_free(fileindex);
5437 return lock_worktree(worktree, LOCK_SH);
5440 const struct got_error *
5441 got_worktree_histedit_in_progress(int *in_progress,
5442 struct got_worktree *worktree)
5444 const struct got_error *err;
5445 char *tmp_branch_name = NULL;
5447 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5448 if (err)
5449 return err;
5451 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5452 free(tmp_branch_name);
5453 return NULL;
5456 const struct got_error *
5457 got_worktree_histedit_continue(struct got_object_id **commit_id,
5458 struct got_reference **tmp_branch, struct got_reference **branch_ref,
5459 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5460 struct got_worktree *worktree, struct got_repository *repo)
5462 const struct got_error *err;
5463 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5464 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5465 struct got_reference *commit_ref = NULL;
5466 struct got_reference *base_commit_ref = NULL;
5467 char *fileindex_path = NULL;
5468 int have_staged_files = 0;
5470 *commit_id = NULL;
5471 *tmp_branch = NULL;
5472 *base_commit_id = NULL;
5473 *fileindex = NULL;
5475 err = lock_worktree(worktree, LOCK_EX);
5476 if (err)
5477 return err;
5479 err = open_fileindex(fileindex, &fileindex_path, worktree);
5480 if (err)
5481 goto done;
5483 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5484 &have_staged_files);
5485 if (err && err->code != GOT_ERR_CANCELLED)
5486 goto done;
5487 if (have_staged_files) {
5488 err = got_error(GOT_ERR_STAGED_PATHS);
5489 goto done;
5492 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5493 if (err)
5494 goto done;
5496 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5497 if (err)
5498 goto done;
5500 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5501 if (err)
5502 goto done;
5504 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5505 worktree);
5506 if (err)
5507 goto done;
5509 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5510 if (err)
5511 goto done;
5513 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5514 if (err)
5515 goto done;
5516 err = got_ref_resolve(commit_id, repo, commit_ref);
5517 if (err)
5518 goto done;
5520 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5521 if (err)
5522 goto done;
5523 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5524 if (err)
5525 goto done;
5527 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5528 if (err)
5529 goto done;
5530 done:
5531 free(commit_ref_name);
5532 free(branch_ref_name);
5533 free(fileindex_path);
5534 if (commit_ref)
5535 got_ref_close(commit_ref);
5536 if (base_commit_ref)
5537 got_ref_close(base_commit_ref);
5538 if (err) {
5539 free(*commit_id);
5540 *commit_id = NULL;
5541 free(*base_commit_id);
5542 *base_commit_id = NULL;
5543 if (*tmp_branch) {
5544 got_ref_close(*tmp_branch);
5545 *tmp_branch = NULL;
5547 if (*fileindex) {
5548 got_fileindex_free(*fileindex);
5549 *fileindex = NULL;
5551 lock_worktree(worktree, LOCK_EX);
5553 return err;
5556 static const struct got_error *
5557 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
5559 const struct got_error *err;
5560 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
5561 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5563 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5564 if (err)
5565 goto done;
5566 err = delete_ref(tmp_branch_name, repo);
5567 if (err)
5568 goto done;
5570 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5571 worktree);
5572 if (err)
5573 goto done;
5574 err = delete_ref(base_commit_ref_name, repo);
5575 if (err)
5576 goto done;
5578 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5579 if (err)
5580 goto done;
5581 err = delete_ref(branch_ref_name, repo);
5582 if (err)
5583 goto done;
5585 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5586 if (err)
5587 goto done;
5588 err = delete_ref(commit_ref_name, repo);
5589 if (err)
5590 goto done;
5591 done:
5592 free(tmp_branch_name);
5593 free(base_commit_ref_name);
5594 free(branch_ref_name);
5595 free(commit_ref_name);
5596 return err;
5599 const struct got_error *
5600 got_worktree_histedit_abort(struct got_worktree *worktree,
5601 struct got_fileindex *fileindex, struct got_repository *repo,
5602 struct got_reference *branch, struct got_object_id *base_commit_id,
5603 got_worktree_checkout_cb progress_cb, void *progress_arg)
5605 const struct got_error *err, *unlockerr, *sync_err;
5606 struct got_reference *resolved = NULL;
5607 char *fileindex_path = NULL;
5608 struct got_object_id *tree_id = NULL;
5609 struct revert_file_args rfa;
5611 err = lock_worktree(worktree, LOCK_EX);
5612 if (err)
5613 return err;
5615 err = got_ref_open(&resolved, repo,
5616 got_ref_get_symref_target(branch), 0);
5617 if (err)
5618 goto done;
5620 err = got_worktree_set_head_ref(worktree, resolved);
5621 if (err)
5622 goto done;
5624 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
5625 if (err)
5626 goto done;
5628 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
5629 worktree->path_prefix);
5630 if (err)
5631 goto done;
5633 err = delete_histedit_refs(worktree, repo);
5634 if (err)
5635 goto done;
5637 err = get_fileindex_path(&fileindex_path, worktree);
5638 if (err)
5639 goto done;
5641 rfa.worktree = worktree;
5642 rfa.fileindex = fileindex;
5643 rfa.progress_cb = progress_cb;
5644 rfa.progress_arg = progress_arg;
5645 rfa.patch_cb = NULL;
5646 rfa.patch_arg = NULL;
5647 rfa.repo = repo;
5648 err = worktree_status(worktree, "", fileindex, repo,
5649 revert_file, &rfa, NULL, NULL, 0);
5650 if (err)
5651 goto sync;
5653 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5654 repo, progress_cb, progress_arg, NULL, NULL);
5655 sync:
5656 sync_err = sync_fileindex(fileindex, fileindex_path);
5657 if (sync_err && err == NULL)
5658 err = sync_err;
5659 done:
5660 got_ref_close(resolved);
5661 free(tree_id);
5662 free(fileindex_path);
5664 unlockerr = lock_worktree(worktree, LOCK_SH);
5665 if (unlockerr && err == NULL)
5666 err = unlockerr;
5667 return err;
5670 const struct got_error *
5671 got_worktree_histedit_complete(struct got_worktree *worktree,
5672 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5673 struct got_reference *edited_branch, struct got_repository *repo)
5675 const struct got_error *err, *unlockerr;
5676 struct got_object_id *new_head_commit_id = NULL;
5677 struct got_reference *resolved = NULL;
5679 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5680 if (err)
5681 return err;
5683 err = got_ref_open(&resolved, repo,
5684 got_ref_get_symref_target(edited_branch), 0);
5685 if (err)
5686 goto done;
5688 err = got_ref_change_ref(resolved, new_head_commit_id);
5689 if (err)
5690 goto done;
5692 err = got_ref_write(resolved, repo);
5693 if (err)
5694 goto done;
5696 err = got_worktree_set_head_ref(worktree, resolved);
5697 if (err)
5698 goto done;
5700 err = delete_histedit_refs(worktree, repo);
5701 done:
5702 if (fileindex)
5703 got_fileindex_free(fileindex);
5704 free(new_head_commit_id);
5705 unlockerr = lock_worktree(worktree, LOCK_SH);
5706 if (unlockerr && err == NULL)
5707 err = unlockerr;
5708 return err;
5711 const struct got_error *
5712 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
5713 struct got_object_id *commit_id, struct got_repository *repo)
5715 const struct got_error *err;
5716 char *commit_ref_name;
5718 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5719 if (err)
5720 return err;
5722 err = store_commit_id(commit_ref_name, commit_id, repo);
5723 if (err)
5724 goto done;
5726 err = delete_ref(commit_ref_name, repo);
5727 done:
5728 free(commit_ref_name);
5729 return err;
5732 const struct got_error *
5733 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
5734 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
5735 struct got_worktree *worktree, const char *refname,
5736 struct got_repository *repo)
5738 const struct got_error *err = NULL;
5739 char *fileindex_path = NULL;
5740 struct check_rebase_ok_arg ok_arg;
5742 *fileindex = NULL;
5743 *branch_ref = NULL;
5744 *base_branch_ref = NULL;
5746 err = lock_worktree(worktree, LOCK_EX);
5747 if (err)
5748 return err;
5750 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
5751 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5752 "cannot integrate a branch into itself; "
5753 "update -b or different branch name required");
5754 goto done;
5757 err = open_fileindex(fileindex, &fileindex_path, worktree);
5758 if (err)
5759 goto done;
5761 /* Preconditions are the same as for rebase. */
5762 ok_arg.worktree = worktree;
5763 ok_arg.repo = repo;
5764 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5765 &ok_arg);
5766 if (err)
5767 goto done;
5769 err = got_ref_open(branch_ref, repo, refname, 1);
5770 if (err)
5771 goto done;
5773 err = got_ref_open(base_branch_ref, repo,
5774 got_worktree_get_head_ref_name(worktree), 1);
5775 done:
5776 if (err) {
5777 if (*branch_ref) {
5778 got_ref_close(*branch_ref);
5779 *branch_ref = NULL;
5781 if (*base_branch_ref) {
5782 got_ref_close(*base_branch_ref);
5783 *base_branch_ref = NULL;
5785 if (*fileindex) {
5786 got_fileindex_free(*fileindex);
5787 *fileindex = NULL;
5789 lock_worktree(worktree, LOCK_SH);
5791 return err;
5794 const struct got_error *
5795 got_worktree_integrate_continue(struct got_worktree *worktree,
5796 struct got_fileindex *fileindex, struct got_repository *repo,
5797 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
5798 got_worktree_checkout_cb progress_cb, void *progress_arg,
5799 got_cancel_cb cancel_cb, void *cancel_arg)
5801 const struct got_error *err = NULL, *sync_err, *unlockerr;
5802 char *fileindex_path = NULL;
5803 struct got_object_id *tree_id = NULL, *commit_id = NULL;
5805 err = get_fileindex_path(&fileindex_path, worktree);
5806 if (err)
5807 goto done;
5809 err = got_ref_resolve(&commit_id, repo, branch_ref);
5810 if (err)
5811 goto done;
5813 err = got_object_id_by_path(&tree_id, repo, commit_id,
5814 worktree->path_prefix);
5815 if (err)
5816 goto done;
5818 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5819 if (err)
5820 goto done;
5822 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
5823 progress_cb, progress_arg, cancel_cb, cancel_arg);
5824 if (err)
5825 goto sync;
5827 err = got_ref_change_ref(base_branch_ref, commit_id);
5828 if (err)
5829 goto sync;
5831 err = got_ref_write(base_branch_ref, repo);
5832 sync:
5833 sync_err = sync_fileindex(fileindex, fileindex_path);
5834 if (sync_err && err == NULL)
5835 err = sync_err;
5837 done:
5838 unlockerr = got_ref_unlock(branch_ref);
5839 if (unlockerr && err == NULL)
5840 err = unlockerr;
5841 got_ref_close(branch_ref);
5843 unlockerr = got_ref_unlock(base_branch_ref);
5844 if (unlockerr && err == NULL)
5845 err = unlockerr;
5846 got_ref_close(base_branch_ref);
5848 got_fileindex_free(fileindex);
5849 free(fileindex_path);
5850 free(tree_id);
5852 unlockerr = lock_worktree(worktree, LOCK_SH);
5853 if (unlockerr && err == NULL)
5854 err = unlockerr;
5855 return err;
5858 const struct got_error *
5859 got_worktree_integrate_abort(struct got_worktree *worktree,
5860 struct got_fileindex *fileindex, struct got_repository *repo,
5861 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
5863 const struct got_error *err = NULL, *unlockerr = NULL;
5865 got_fileindex_free(fileindex);
5867 err = lock_worktree(worktree, LOCK_SH);
5869 unlockerr = got_ref_unlock(branch_ref);
5870 if (unlockerr && err == NULL)
5871 err = unlockerr;
5872 got_ref_close(branch_ref);
5874 unlockerr = got_ref_unlock(base_branch_ref);
5875 if (unlockerr && err == NULL)
5876 err = unlockerr;
5877 got_ref_close(base_branch_ref);
5879 return err;
5882 struct check_stage_ok_arg {
5883 struct got_object_id *head_commit_id;
5884 struct got_worktree *worktree;
5885 struct got_fileindex *fileindex;
5886 struct got_repository *repo;
5887 int have_changes;
5890 const struct got_error *
5891 check_stage_ok(void *arg, unsigned char status,
5892 unsigned char staged_status, const char *relpath,
5893 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5894 struct got_object_id *commit_id)
5896 struct check_stage_ok_arg *a = arg;
5897 const struct got_error *err = NULL;
5898 struct got_fileindex_entry *ie;
5899 struct got_object_id base_commit_id;
5900 struct got_object_id *base_commit_idp = NULL;
5901 char *in_repo_path = NULL, *p;
5903 if (status == GOT_STATUS_UNVERSIONED ||
5904 status == GOT_STATUS_NO_CHANGE)
5905 return NULL;
5906 if (status == GOT_STATUS_NONEXISTENT)
5907 return got_error_set_errno(ENOENT, relpath);
5909 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5910 if (ie == NULL)
5911 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5913 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
5914 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5915 relpath) == -1)
5916 return got_error_from_errno("asprintf");
5918 if (got_fileindex_entry_has_commit(ie)) {
5919 memcpy(base_commit_id.sha1, ie->commit_sha1,
5920 SHA1_DIGEST_LENGTH);
5921 base_commit_idp = &base_commit_id;
5924 if (status == GOT_STATUS_CONFLICT) {
5925 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
5926 goto done;
5927 } else if (status != GOT_STATUS_ADD &&
5928 status != GOT_STATUS_MODIFY &&
5929 status != GOT_STATUS_DELETE) {
5930 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
5931 goto done;
5934 a->have_changes = 1;
5936 p = in_repo_path;
5937 while (p[0] == '/')
5938 p++;
5939 err = check_out_of_date(p, status, staged_status,
5940 blob_id, base_commit_idp, a->head_commit_id, a->repo,
5941 GOT_ERR_STAGE_OUT_OF_DATE);
5942 done:
5943 free(in_repo_path);
5944 return err;
5947 struct stage_path_arg {
5948 struct got_worktree *worktree;
5949 struct got_fileindex *fileindex;
5950 struct got_repository *repo;
5951 got_worktree_status_cb status_cb;
5952 void *status_arg;
5953 got_worktree_patch_cb patch_cb;
5954 void *patch_arg;
5955 int staged_something;
5958 static const struct got_error *
5959 stage_path(void *arg, unsigned char status,
5960 unsigned char staged_status, const char *relpath,
5961 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5962 struct got_object_id *commit_id)
5964 struct stage_path_arg *a = arg;
5965 const struct got_error *err = NULL;
5966 struct got_fileindex_entry *ie;
5967 char *ondisk_path = NULL, *path_content = NULL;
5968 uint32_t stage;
5969 struct got_object_id *new_staged_blob_id = NULL;
5971 if (status == GOT_STATUS_UNVERSIONED)
5972 return NULL;
5974 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
5975 if (ie == NULL)
5976 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
5978 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
5979 relpath)== -1)
5980 return got_error_from_errno("asprintf");
5982 switch (status) {
5983 case GOT_STATUS_ADD:
5984 case GOT_STATUS_MODIFY:
5985 if (a->patch_cb) {
5986 if (status == GOT_STATUS_ADD) {
5987 int choice = GOT_PATCH_CHOICE_NONE;
5988 err = (*a->patch_cb)(&choice, a->patch_arg,
5989 status, ie->path, NULL, 1, 1);
5990 if (err)
5991 break;
5992 if (choice != GOT_PATCH_CHOICE_YES)
5993 break;
5994 } else {
5995 err = create_patched_content(&path_content, 0,
5996 staged_blob_id ? staged_blob_id : blob_id,
5997 ondisk_path, ie->path, a->repo,
5998 a->patch_cb, a->patch_arg);
5999 if (err || path_content == NULL)
6000 break;
6003 err = got_object_blob_create(&new_staged_blob_id,
6004 path_content ? path_content : ondisk_path, a->repo);
6005 if (err)
6006 break;
6007 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6008 SHA1_DIGEST_LENGTH);
6009 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6010 stage = GOT_FILEIDX_STAGE_ADD;
6011 else
6012 stage = GOT_FILEIDX_STAGE_MODIFY;
6013 got_fileindex_entry_stage_set(ie, stage);
6014 a->staged_something = 1;
6015 if (a->status_cb == NULL)
6016 break;
6017 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6018 get_staged_status(ie), relpath, blob_id,
6019 new_staged_blob_id, NULL);
6020 break;
6021 case GOT_STATUS_DELETE:
6022 if (staged_status == GOT_STATUS_DELETE)
6023 break;
6024 if (a->patch_cb) {
6025 int choice = GOT_PATCH_CHOICE_NONE;
6026 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6027 ie->path, NULL, 1, 1);
6028 if (err)
6029 break;
6030 if (choice == GOT_PATCH_CHOICE_NO)
6031 break;
6032 if (choice != GOT_PATCH_CHOICE_YES) {
6033 err = got_error(GOT_ERR_PATCH_CHOICE);
6034 break;
6037 stage = GOT_FILEIDX_STAGE_DELETE;
6038 got_fileindex_entry_stage_set(ie, stage);
6039 a->staged_something = 1;
6040 if (a->status_cb == NULL)
6041 break;
6042 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6043 get_staged_status(ie), relpath, NULL, NULL, NULL);
6044 break;
6045 case GOT_STATUS_NO_CHANGE:
6046 break;
6047 case GOT_STATUS_CONFLICT:
6048 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6049 break;
6050 case GOT_STATUS_NONEXISTENT:
6051 err = got_error_set_errno(ENOENT, relpath);
6052 break;
6053 default:
6054 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6055 break;
6058 if (path_content && unlink(path_content) == -1 && err == NULL)
6059 err = got_error_from_errno2("unlink", path_content);
6060 free(path_content);
6061 free(ondisk_path);
6062 free(new_staged_blob_id);
6063 return err;
6066 const struct got_error *
6067 got_worktree_stage(struct got_worktree *worktree,
6068 struct got_pathlist_head *paths,
6069 got_worktree_status_cb status_cb, void *status_arg,
6070 got_worktree_patch_cb patch_cb, void *patch_arg,
6071 struct got_repository *repo)
6073 const struct got_error *err = NULL, *sync_err, *unlockerr;
6074 struct got_pathlist_entry *pe;
6075 struct got_fileindex *fileindex = NULL;
6076 char *fileindex_path = NULL;
6077 struct got_reference *head_ref = NULL;
6078 struct got_object_id *head_commit_id = NULL;
6079 struct check_stage_ok_arg oka;
6080 struct stage_path_arg spa;
6082 err = lock_worktree(worktree, LOCK_EX);
6083 if (err)
6084 return err;
6086 err = got_ref_open(&head_ref, repo,
6087 got_worktree_get_head_ref_name(worktree), 0);
6088 if (err)
6089 goto done;
6090 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6091 if (err)
6092 goto done;
6093 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6094 if (err)
6095 goto done;
6097 /* Check pre-conditions before staging anything. */
6098 oka.head_commit_id = head_commit_id;
6099 oka.worktree = worktree;
6100 oka.fileindex = fileindex;
6101 oka.repo = repo;
6102 oka.have_changes = 0;
6103 TAILQ_FOREACH(pe, paths, entry) {
6104 err = worktree_status(worktree, pe->path, fileindex, repo,
6105 check_stage_ok, &oka, NULL, NULL, 0);
6106 if (err)
6107 goto done;
6109 if (!oka.have_changes) {
6110 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6111 goto done;
6114 spa.worktree = worktree;
6115 spa.fileindex = fileindex;
6116 spa.repo = repo;
6117 spa.patch_cb = patch_cb;
6118 spa.patch_arg = patch_arg;
6119 spa.status_cb = status_cb;
6120 spa.status_arg = status_arg;
6121 spa.staged_something = 0;
6122 TAILQ_FOREACH(pe, paths, entry) {
6123 err = worktree_status(worktree, pe->path, fileindex, repo,
6124 stage_path, &spa, NULL, NULL, 0);
6125 if (err)
6126 goto done;
6128 if (!spa.staged_something) {
6129 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6130 goto done;
6133 sync_err = sync_fileindex(fileindex, fileindex_path);
6134 if (sync_err && err == NULL)
6135 err = sync_err;
6136 done:
6137 if (head_ref)
6138 got_ref_close(head_ref);
6139 free(head_commit_id);
6140 free(fileindex_path);
6141 if (fileindex)
6142 got_fileindex_free(fileindex);
6143 unlockerr = lock_worktree(worktree, LOCK_SH);
6144 if (unlockerr && err == NULL)
6145 err = unlockerr;
6146 return err;
6149 struct unstage_path_arg {
6150 struct got_worktree *worktree;
6151 struct got_fileindex *fileindex;
6152 struct got_repository *repo;
6153 got_worktree_checkout_cb progress_cb;
6154 void *progress_arg;
6155 got_worktree_patch_cb patch_cb;
6156 void *patch_arg;
6159 static const struct got_error *
6160 create_unstaged_content(char **path_unstaged_content,
6161 char **path_new_staged_content, struct got_object_id *blob_id,
6162 struct got_object_id *staged_blob_id, const char *relpath,
6163 struct got_repository *repo,
6164 got_worktree_patch_cb patch_cb, void *patch_arg)
6166 const struct got_error *err;
6167 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6168 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6169 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6170 struct stat sb1, sb2;
6171 struct got_diff_changes *changes = NULL;
6172 struct got_diff_state *ds = NULL;
6173 struct got_diff_args *args = NULL;
6174 struct got_diff_change *change;
6175 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6176 int have_content = 0, have_rejected_content = 0;
6178 *path_unstaged_content = NULL;
6179 *path_new_staged_content = NULL;
6181 err = got_object_id_str(&label1, blob_id);
6182 if (err)
6183 return err;
6184 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6185 if (err)
6186 goto done;
6188 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6189 if (err)
6190 goto done;
6192 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6193 if (err)
6194 goto done;
6196 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6197 if (err)
6198 goto done;
6200 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6201 if (err)
6202 goto done;
6204 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6205 if (err)
6206 goto done;
6208 if (stat(path1, &sb1) == -1) {
6209 err = got_error_from_errno2("stat", path1);
6210 goto done;
6213 if (stat(path2, &sb2) == -1) {
6214 err = got_error_from_errno2("stat", path2);
6215 goto done;
6218 err = got_diff_files(&changes, &ds, &args, &diff_flags,
6219 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6220 if (err)
6221 goto done;
6223 err = got_opentemp_named(path_unstaged_content, &outfile,
6224 "got-unstaged-content");
6225 if (err)
6226 goto done;
6227 err = got_opentemp_named(path_new_staged_content, &rejectfile,
6228 "got-new-staged-content");
6229 if (err)
6230 goto done;
6232 if (fseek(f1, 0L, SEEK_SET) == -1) {
6233 err = got_ferror(f1, GOT_ERR_IO);
6234 goto done;
6236 if (fseek(f2, 0L, SEEK_SET) == -1) {
6237 err = got_ferror(f2, GOT_ERR_IO);
6238 goto done;
6240 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6241 int choice;
6242 err = apply_or_reject_change(&choice, change, ++n,
6243 changes->nchanges, ds, args, diff_flags, relpath,
6244 f1, f2, &line_cur1, &line_cur2,
6245 outfile, rejectfile, patch_cb, patch_arg);
6246 if (err)
6247 goto done;
6248 if (choice == GOT_PATCH_CHOICE_YES)
6249 have_content = 1;
6250 else
6251 have_rejected_content = 1;
6252 if (choice == GOT_PATCH_CHOICE_QUIT)
6253 break;
6255 if (have_content || have_rejected_content)
6256 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6257 outfile, rejectfile);
6258 done:
6259 free(label1);
6260 if (blob)
6261 got_object_blob_close(blob);
6262 if (staged_blob)
6263 got_object_blob_close(staged_blob);
6264 if (f1 && fclose(f1) == EOF && err == NULL)
6265 err = got_error_from_errno2("fclose", path1);
6266 if (f2 && fclose(f2) == EOF && err == NULL)
6267 err = got_error_from_errno2("fclose", path2);
6268 if (outfile && fclose(outfile) == EOF && err == NULL)
6269 err = got_error_from_errno2("fclose", *path_unstaged_content);
6270 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6271 err = got_error_from_errno2("fclose", *path_new_staged_content);
6272 if (path1 && unlink(path1) == -1 && err == NULL)
6273 err = got_error_from_errno2("unlink", path1);
6274 if (path2 && unlink(path2) == -1 && err == NULL)
6275 err = got_error_from_errno2("unlink", path2);
6276 if (err || !have_content) {
6277 if (*path_unstaged_content &&
6278 unlink(*path_unstaged_content) == -1 && err == NULL)
6279 err = got_error_from_errno2("unlink",
6280 *path_unstaged_content);
6281 free(*path_unstaged_content);
6282 *path_unstaged_content = NULL;
6284 if (err || !have_rejected_content) {
6285 if (*path_new_staged_content &&
6286 unlink(*path_new_staged_content) == -1 && err == NULL)
6287 err = got_error_from_errno2("unlink",
6288 *path_new_staged_content);
6289 free(*path_new_staged_content);
6290 *path_new_staged_content = NULL;
6292 free(args);
6293 if (ds) {
6294 got_diff_state_free(ds);
6295 free(ds);
6297 if (changes)
6298 got_diff_free_changes(changes);
6299 free(path1);
6300 free(path2);
6301 return err;
6304 static const struct got_error *
6305 unstage_path(void *arg, unsigned char status,
6306 unsigned char staged_status, const char *relpath,
6307 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6308 struct got_object_id *commit_id)
6310 const struct got_error *err = NULL;
6311 struct unstage_path_arg *a = arg;
6312 struct got_fileindex_entry *ie;
6313 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6314 char *ondisk_path = NULL, *path_unstaged_content = NULL;
6315 char *path_new_staged_content = NULL;
6316 char *id_str = NULL, *label_orig = NULL;
6317 int local_changes_subsumed;
6318 struct stat sb;
6320 if (staged_status != GOT_STATUS_ADD &&
6321 staged_status != GOT_STATUS_MODIFY &&
6322 staged_status != GOT_STATUS_DELETE)
6323 return NULL;
6325 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6326 if (ie == NULL)
6327 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6329 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6330 == -1)
6331 return got_error_from_errno("asprintf");
6333 err = got_object_id_str(&id_str,
6334 commit_id ? commit_id : a->worktree->base_commit_id);
6335 if (err)
6336 goto done;
6337 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6338 id_str) == -1) {
6339 err = got_error_from_errno("asprintf");
6340 goto done;
6343 switch (staged_status) {
6344 case GOT_STATUS_MODIFY:
6345 err = got_object_open_as_blob(&blob_base, a->repo,
6346 blob_id, 8192);
6347 if (err)
6348 break;
6349 /* fall through */
6350 case GOT_STATUS_ADD:
6351 if (a->patch_cb) {
6352 if (staged_status == GOT_STATUS_ADD) {
6353 int choice = GOT_PATCH_CHOICE_NONE;
6354 err = (*a->patch_cb)(&choice, a->patch_arg,
6355 staged_status, ie->path, NULL, 1, 1);
6356 if (err)
6357 break;
6358 if (choice != GOT_PATCH_CHOICE_YES)
6359 break;
6360 } else {
6361 err = create_unstaged_content(
6362 &path_unstaged_content,
6363 &path_new_staged_content, blob_id,
6364 staged_blob_id, ie->path, a->repo,
6365 a->patch_cb, a->patch_arg);
6366 if (err || path_unstaged_content == NULL)
6367 break;
6368 if (path_new_staged_content) {
6369 err = got_object_blob_create(
6370 &staged_blob_id,
6371 path_new_staged_content,
6372 a->repo);
6373 if (err)
6374 break;
6375 memcpy(ie->staged_blob_sha1,
6376 staged_blob_id->sha1,
6377 SHA1_DIGEST_LENGTH);
6379 err = merge_file(&local_changes_subsumed,
6380 a->worktree, blob_base, ondisk_path,
6381 relpath, got_fileindex_perms_to_st(ie),
6382 path_unstaged_content, label_orig,
6383 "unstaged", a->repo, a->progress_cb,
6384 a->progress_arg);
6385 if (err == NULL &&
6386 path_new_staged_content == NULL)
6387 got_fileindex_entry_stage_set(ie,
6388 GOT_FILEIDX_STAGE_NONE);
6389 break; /* Done with this file. */
6392 err = got_object_open_as_blob(&blob_staged, a->repo,
6393 staged_blob_id, 8192);
6394 if (err)
6395 break;
6396 err = merge_blob(&local_changes_subsumed, a->worktree,
6397 blob_base, ondisk_path, relpath,
6398 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6399 commit_id ? commit_id : a->worktree->base_commit_id,
6400 a->repo, a->progress_cb, a->progress_arg);
6401 if (err == NULL)
6402 got_fileindex_entry_stage_set(ie,
6403 GOT_FILEIDX_STAGE_NONE);
6404 break;
6405 case GOT_STATUS_DELETE:
6406 if (a->patch_cb) {
6407 int choice = GOT_PATCH_CHOICE_NONE;
6408 err = (*a->patch_cb)(&choice, a->patch_arg,
6409 staged_status, ie->path, NULL, 1, 1);
6410 if (err)
6411 break;
6412 if (choice == GOT_PATCH_CHOICE_NO)
6413 break;
6414 if (choice != GOT_PATCH_CHOICE_YES) {
6415 err = got_error(GOT_ERR_PATCH_CHOICE);
6416 break;
6419 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6420 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
6421 if (err)
6422 break;
6423 err = (*a->progress_cb)(a->progress_arg, status, relpath);
6424 break;
6426 done:
6427 free(ondisk_path);
6428 if (path_unstaged_content &&
6429 unlink(path_unstaged_content) == -1 && err == NULL)
6430 err = got_error_from_errno2("unlink", path_unstaged_content);
6431 if (path_new_staged_content &&
6432 unlink(path_new_staged_content) == -1 && err == NULL)
6433 err = got_error_from_errno2("unlink", path_new_staged_content);
6434 free(path_unstaged_content);
6435 free(path_new_staged_content);
6436 if (blob_base)
6437 got_object_blob_close(blob_base);
6438 if (blob_staged)
6439 got_object_blob_close(blob_staged);
6440 free(id_str);
6441 free(label_orig);
6442 return err;
6445 const struct got_error *
6446 got_worktree_unstage(struct got_worktree *worktree,
6447 struct got_pathlist_head *paths,
6448 got_worktree_checkout_cb progress_cb, void *progress_arg,
6449 got_worktree_patch_cb patch_cb, void *patch_arg,
6450 struct got_repository *repo)
6452 const struct got_error *err = NULL, *sync_err, *unlockerr;
6453 struct got_pathlist_entry *pe;
6454 struct got_fileindex *fileindex = NULL;
6455 char *fileindex_path = NULL;
6456 struct unstage_path_arg upa;
6458 err = lock_worktree(worktree, LOCK_EX);
6459 if (err)
6460 return err;
6462 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6463 if (err)
6464 goto done;
6466 upa.worktree = worktree;
6467 upa.fileindex = fileindex;
6468 upa.repo = repo;
6469 upa.progress_cb = progress_cb;
6470 upa.progress_arg = progress_arg;
6471 upa.patch_cb = patch_cb;
6472 upa.patch_arg = patch_arg;
6473 TAILQ_FOREACH(pe, paths, entry) {
6474 err = worktree_status(worktree, pe->path, fileindex, repo,
6475 unstage_path, &upa, NULL, NULL, 0);
6476 if (err)
6477 goto done;
6480 sync_err = sync_fileindex(fileindex, fileindex_path);
6481 if (sync_err && err == NULL)
6482 err = sync_err;
6483 done:
6484 free(fileindex_path);
6485 if (fileindex)
6486 got_fileindex_free(fileindex);
6487 unlockerr = lock_worktree(worktree, LOCK_SH);
6488 if (unlockerr && err == NULL)
6489 err = unlockerr;
6490 return err;